Relayouted all switch statements: case aligns with switch.
[mmh] / uip / slocal.c
1 /*
2 ** slocal.c -- asynchronously filter and deliver new mail
3 **
4 ** This code is Copyright (c) 2002, by the authors of nmh.  See the
5 ** COPYRIGHT file in the root directory of the nmh distribution for
6 ** complete copyright information.
7 */
8
9 /*
10 **  Under sendmail, users should add the line
11 **
12 **      "| /usr/local/nmh/lib/slocal"
13 **
14 **  to their $HOME/.forward file.
15 **
16 */
17
18 /* Changed to use getutent() and friends.  Assumes that when getutent() exists,
19 ** a number of other things also exist.  Please check.
20 ** Ruud de Rooij <ruud@ruud.org>  Sun, 28 May 2000 17:28:55 +0200
21 */
22
23 #include <h/mh.h>
24 #include <h/dropsbr.h>
25 #include <h/rcvmail.h>
26 #include <h/signals.h>
27 #include <h/tws.h>
28 #include <h/mts.h>
29 #include <h/utils.h>
30
31 #include <pwd.h>
32 #include <signal.h>
33 #include <sys/ioctl.h>
34 #include <fcntl.h>
35
36 #ifdef INITGROUPS_HEADER
37 #include INITGROUPS_HEADER
38 #else
39 /*
40 ** On AIX 4.1, initgroups() is defined and even documented (giving the
41 ** parameter types as char* and int), but doesn't have a prototype in any
42 ** of the system header files.  AIX 4.3, SunOS 4.1.3, and ULTRIX 4.2A have
43 ** the same problem.
44 */
45 extern int  initgroups(char*, int);
46 #endif
47
48 /*
49 ** This define is needed for Berkeley db v2 and above to
50 ** make the header file expose the 'historical' ndbm APIs.
51 ** We define it unconditionally because this is simple and
52 ** harmless.
53 */
54 #define DB_DBM_HSEARCH 1
55 #ifdef NDBM_HEADER
56 #include NDBM_HEADER
57 #endif
58
59 #include <utmp.h>
60
61 #ifndef HAVE_GETUTENT
62 # ifndef UTMP_FILE
63 #  ifdef _PATH_UTMP
64 #   define UTMP_FILE _PATH_UTMP
65 #  else
66 #   define UTMP_FILE "/etc/utmp"
67 #  endif
68 # endif
69 #endif
70
71 static struct swit switches[] = {
72 #define ADDRSW  0
73         { "addr address", 0 },
74 #define USERSW  1
75         { "user name", 0 },
76 #define FILESW  2
77         { "file file", 0 },
78 #define SENDERSW  3
79         { "sender address", 0 },
80 #define MAILBOXSW  4
81         { "mailbox file", 0 },
82 #define HOMESW  5
83         { "home directory", -4 },
84 #define INFOSW  6
85         { "info data", 0 },
86 #define MAILSW  7
87         { "maildelivery file", 0 },
88 #define VERBSW  8
89         { "verbose", 0 },
90 #define NVERBSW  9
91         { "noverbose", 0 },
92 #define SUPPRESSDUP   10
93         { "suppressdup", 0 },
94 #define NSUPPRESSDUP 11
95         { "nosuppressdup", 0 },
96 #define DEBUGSW  12
97         { "debug", 0 },
98 #define VERSIONSW  13
99         { "version", 0 },
100 #define HELPSW  14
101         { "help", 0 },
102         { NULL, 0 }
103 };
104
105 static int globbed = 0;  /* have we built "vars" table yet? */
106 static int parsed = 0;  /* have we built header field table yet   */
107 static int utmped = 0;  /* have we scanned umtp(x) file yet */
108 static int suppressdup = 0;  /* are we suppressing duplicate messages? */
109
110 static int verbose = 0;
111 static int debug = 0;
112
113 static char *addr = NULL;
114 static char *user = NULL;
115 static char *info = NULL;
116 static char *file = NULL;
117 static char *sender = NULL;
118 static char *envelope = NULL;  /* envelope information ("From " line)  */
119 static char *mbox = NULL;
120 static char *home = NULL;
121
122 static struct passwd *pw;  /* passwd file entry */
123
124 static char ddate[BUFSIZ];  /* record the delivery date */
125 struct tws *now;
126
127 static jmp_buf myctx;
128
129 /* flags for pair->p_flags */
130 #define P_NIL  0x00
131 #define P_ADR  0x01  /* field is address */
132 #define P_HID  0x02  /* special (fake) field */
133 #define P_CHK  0x04
134
135 struct pair {
136         char *p_name;
137         char *p_value;
138         char  p_flags;
139 };
140
141 #define NVEC 100
142
143 /*
144 ** Lookup table for matching fields and patterns
145 ** in messages.  The rest of the table is added
146 ** when the message is parsed.
147 */
148 static struct pair hdrs[NVEC + 1] = {
149         { "source",  NULL, P_HID },
150         { "addr",  NULL, P_HID },
151         { "Return-Path",  NULL, P_ADR },
152         { "Reply-To",  NULL, P_ADR },
153         { "From",  NULL, P_ADR },
154         { "Sender",  NULL, P_ADR },
155         { "To",  NULL, P_ADR },
156         { "cc",  NULL, P_ADR },
157         { "Resent-Reply-To", NULL, P_ADR },
158         { "Resent-From",  NULL, P_ADR },
159         { "Resent-Sender",   NULL, P_ADR },
160         { "Resent-To",  NULL, P_ADR },
161         { "Resent-Cc",  NULL, P_ADR },
162         { NULL, NULL, 0 }
163 };
164
165 /*
166 ** The list of builtin variables to expand in a string
167 ** before it is executed by the "pipe" or "qpipe" action.
168 */
169 static struct pair vars[] = {
170         { "sender",   NULL, P_NIL },
171         { "address",  NULL, P_NIL },
172         { "size",  NULL, P_NIL },
173         { "reply-to", NULL, P_CHK },
174         { "info",  NULL, P_NIL },
175         { NULL, NULL, 0 }
176 };
177
178 extern char **environ;
179
180 /*
181 ** static prototypes
182 */
183 static int localmail(int, char *);
184 static int usr_delivery(int, char *, int);
185 static int split(char *, char **);
186 static int parse(int);
187 static void expand(char *, char *, int);
188 static void glob(int);
189 static struct pair *lookup(struct pair *, char *);
190 static int logged_in(void);
191 static int timely(char *, char *);
192 static int usr_file(int, char *, int);
193 static int usr_pipe(int, char *, char *, char **, int);
194 static int usr_folder(int, char *);
195 static RETSIGTYPE alrmser(int);
196 static void get_sender(char *, char **);
197 static int copy_message(int, char *, int);
198 static void verbose_printf(char *fmt, ...);
199 static void adorn(char *, char *, ...);
200 static void debug_printf(char *fmt, ...);
201 static int suppress_duplicates(int, char *);
202 static char *trim(char *);
203
204
205 int
206 main(int argc, char **argv)
207 {
208         int fd, status;
209         FILE *fp = stdin;
210         char *cp, *mdlvr = NULL, buf[BUFSIZ];
211         char mailbox[BUFSIZ], tmpfil[BUFSIZ];
212         char **argp, **arguments;
213
214 #ifdef LOCALE
215         setlocale(LC_ALL, "");
216 #endif
217         invo_name = mhbasename(*argv);
218
219         /* foil search of user profile/context */
220         if (context_foil(NULL) == -1)
221                 done(1);
222
223         mts_init(invo_name);
224         arguments = getarguments(invo_name, argc, argv, 0);
225         argp = arguments;
226
227         /* Parse arguments */
228         while ((cp = *argp++)) {
229                 if (*cp == '-') {
230                         switch (smatch(++cp, switches)) {
231                         case AMBIGSW:
232                                 ambigsw(cp, switches);
233                                 done(1);
234                         case UNKWNSW:
235                                 adios(NULL, "-%s unknown", cp);
236
237                         case HELPSW:
238                                 snprintf(buf, sizeof(buf), "%s [switches] [address info sender]", invo_name);
239                                 print_help(buf, switches, 0);
240                                 done(1);
241                         case VERSIONSW:
242                                 print_version(invo_name);
243                                 done(1);
244
245                         case ADDRSW:
246                                 if (!(addr = *argp++))
247                                         /* allow -xyz arguments */
248                                         adios(NULL, "missing argument to %s",
249                                                         argp[-2]);
250                                 continue;
251                         case INFOSW:
252                                 if (!(info = *argp++))
253                                         /* allow -xyz arguments */
254                                         adios(NULL, "missing argument to %s",
255                                                         argp[-2]);
256                                 continue;
257                         case USERSW:
258                                 if (!(user = *argp++))
259                                         /* allow -xyz arguments */
260                                         adios(NULL, "missing argument to %s",
261                                                         argp[-2]);
262                                 continue;
263                         case FILESW:
264                                 if (!(file = *argp++) || *file == '-')
265                                         adios(NULL, "missing argument to %s",
266                                                         argp[-2]);
267                                 continue;
268                         case SENDERSW:
269                                 if (!(sender = *argp++))
270                                         /* allow -xyz arguments */
271                                         adios(NULL, "missing argument to %s",
272                                                         argp[-2]);
273                                 continue;
274                         case MAILBOXSW:
275                                 if (!(mbox = *argp++) || *mbox == '-')
276                                         adios(NULL, "missing argument to %s",
277                                                         argp[-2]);
278                                 continue;
279                         case HOMESW:
280                                 if (!(home = *argp++) || *home == '-')
281                                         adios(NULL, "missing argument to %s",
282                                                         argp[-2]);
283                                 continue;
284
285                         case MAILSW:
286                                 if (!(cp = *argp++) || *cp == '-')
287                                         adios(NULL, "missing argument to %s",
288                                                         argp[-2]);
289                                 if (mdlvr)
290                                         adios(NULL, "only one maildelivery file at a time!");
291                                 mdlvr = cp;
292                                 continue;
293
294                         case VERBSW:
295                                 verbose++;
296                                 continue;
297                         case NVERBSW:
298                                 verbose = 0;
299                                 continue;
300
301                         case SUPPRESSDUP:
302                                 suppressdup++;
303                                 continue;
304                         case NSUPPRESSDUP:
305                                 suppressdup = 0;
306                                 continue;
307                         case DEBUGSW:
308                                 debug++;
309                                 continue;
310                         }
311                 }
312
313                 switch (argp - (argv + 1)) {
314                 case 1:
315                         addr = cp;
316                         break;
317                 case 2:
318                         info = cp;
319                         break;
320                 case 3:
321                         sender = cp;
322                         break;
323                 }
324         }
325
326         if (addr == NULL)
327                 addr = getusername();
328         if (user == NULL)
329                 user = (cp = strchr(addr, '.')) ? ++cp : addr;
330         if ((pw = getpwnam(user)) == NULL)
331                 adios(NULL, "no such local user as %s", user);
332
333         if (chdir(pw->pw_dir) == -1)
334                 chdir("/");
335         umask(0077);
336
337         if (geteuid() == 0) {
338                 setgid(pw->pw_gid);
339                 initgroups(pw->pw_name, pw->pw_gid);
340                 setuid(pw->pw_uid);
341         }
342
343         if (info == NULL)
344                 info = "";
345
346         setbuf(stdin, NULL);
347
348         /* Record the delivery time */
349         if ((now = dlocaltimenow()) == NULL)
350                 adios(NULL, "unable to ascertain local time");
351         snprintf(ddate, sizeof(ddate), "Delivery-Date: %s\n", dtimenow(0));
352
353         /*
354         ** Copy the message to a temporary file
355         */
356         if (file) {
357                 int tempfd;
358
359                 /* getting message from file */
360                 if ((tempfd = open(file, O_RDONLY)) == -1)
361                         adios(file, "unable to open");
362                 if (debug)
363                         debug_printf("retrieving message from file \"%s\"\n",
364                                         file);
365                 if ((fd = copy_message(tempfd, tmpfil, 1)) == -1)
366                         adios(NULL, "unable to create temporary file");
367                 close(tempfd);
368         } else {
369                 /* getting message from stdin */
370                 if (debug)
371                         debug_printf("retrieving message from stdin\n");
372                 if ((fd = copy_message(fileno(stdin), tmpfil, 1)) == -1)
373                         adios(NULL, "unable to create temporary file");
374         }
375
376         if (debug)
377                 debug_printf("temporary file=\"%s\"\n", tmpfil);
378
379         /*
380         ** Delete the temp file now or a copy of every single message
381         ** passed through slocal will be left in the /tmp directory until
382         ** deleted manually!  This unlink() used to be under an 'else'
383         ** of the 'if (debug)' above, but since some people like to
384         ** always run slocal with -debug and log the results, the /tmp
385         ** directory would get choked over time.  Of course, now that
386         ** we always delete the temp file, the "temporary file=" message
387         ** above is somewhat pointless -- someone watching debug output
388         ** wouldn't have a chance to 'tail -f' or 'ln' the temp file
389         ** before it's unlinked.  The best thing would be to delay this
390         ** unlink() until later if debug == 1, but I'll leave that for
391         ** someone who cares about the temp-file-accessing functionality
392         ** (they'll have to watch out for cases where we adios()).
393         */
394         unlink(tmpfil);
395
396         if (!(fp = fdopen(fd, "r+")))
397                 adios(NULL, "unable to access temporary file");
398
399         /*
400         ** If no sender given, extract it
401         ** from envelope information.
402         */
403         if (sender == NULL)
404                 get_sender(envelope, &sender);
405
406         if (mbox == NULL) {
407                 snprintf(mailbox, sizeof(mailbox), "%s/%s",
408                                 mmdfldir[0] ? mmdfldir : pw->pw_dir,
409                                 mmdflfil[0] ? mmdflfil : pw->pw_name);
410                 mbox = mailbox;
411         }
412         if (home == NULL)
413                 home = pw->pw_dir;
414
415         if (debug) {
416                 debug_printf("addr=\"%s\"\n", trim(addr));
417                 debug_printf("user=\"%s\"\n", trim(user));
418                 debug_printf("info=\"%s\"\n", trim(info));
419                 debug_printf("sender=\"%s\"\n", trim(sender));
420                 debug_printf("envelope=\"%s\"\n",
421                                 envelope ? trim(envelope) : "");
422                 debug_printf("mbox=\"%s\"\n", trim(mbox));
423                 debug_printf("home=\"%s\"\n", trim(home));
424                 debug_printf("ddate=\"%s\"\n", trim(ddate));
425                 debug_printf("now=%02d:%02d\n\n", now->tw_hour, now->tw_min);
426         }
427
428         /* deliver the message */
429         status = localmail(fd, mdlvr);
430
431         done(status != -1 ? RCV_MOK : RCV_MBX);
432         return 1;
433 }
434
435
436 /*
437 ** Main routine for delivering message.
438 */
439
440 static int
441 localmail(int fd, char *mdlvr)
442 {
443         /* check if this message is a duplicate */
444         if (suppressdup && suppress_duplicates(fd, mdlvr ?
445                         mdlvr : ".maildelivery") == DONE)
446                 return 0;
447
448         /* delivery according to personal Maildelivery file */
449         if (usr_delivery(fd, mdlvr ? mdlvr : ".maildelivery", 0) != -1)
450                 return 0;
451
452         /* delivery according to global Maildelivery file */
453         if (usr_delivery(fd, maildelivery, 1) != -1)
454                 return 0;
455
456         if (verbose)
457                 verbose_printf("(delivering to standard mail spool)\n");
458
459         /* last resort - deliver to standard mail spool */
460 #ifdef SLOCAL_MBOX
461         return usr_file(fd, mbox, MBOX_FORMAT);
462 #else
463         return usr_file(fd, mbox, MMDF_FORMAT);
464 #endif
465 }
466
467
468 #define matches(a,b) (stringdex(b, a) >= 0)
469
470 /*
471 ** Parse the delivery file, and process incoming message.
472 */
473
474 static int
475 usr_delivery(int fd, char *delivery, int su)
476 {
477         int i, accept, status=1, won, vecp, next;
478         char *field, *pattern, *action, *result, *string;
479         char buffer[BUFSIZ], tmpbuf[BUFSIZ];
480         char *cp, *vec[NVEC];
481         struct stat st;
482         struct pair *p;
483         FILE *fp;
484
485         /* open the delivery file */
486         if ((fp = fopen(delivery, "r")) == NULL)
487                 return -1;
488
489         /* check if delivery file has bad ownership or permissions */
490         if (fstat(fileno(fp), &st) == -1 ||
491                         (st.st_uid != 0 && (su || st.st_uid != pw->pw_uid)) ||
492                         st.st_mode & (S_IWGRP|S_IWOTH)) {
493                 if (verbose) {
494                         verbose_printf("WARNING: %s has bad ownership/modes (su=%d,uid=%d,owner=%d,mode=0%o)\n", delivery, su, (int) pw->pw_uid, (int) st.st_uid, (int) st.st_mode);
495                 }
496                 return -1;
497         }
498
499         won = 0;
500         next = 1;
501
502         /* read and process delivery file */
503         while (fgets(buffer, sizeof(buffer), fp)) {
504                 /* skip comments and empty lines */
505                 if (*buffer == '#' || *buffer == '\n')
506                         continue;
507
508                 /* zap trailing newline */
509                 if ((cp = strchr(buffer, '\n')))
510                         *cp = 0;
511
512                 /* split buffer into fields */
513                 vecp = split(buffer, vec);
514
515                 /* check for too few fields */
516                 if (vecp < 5) {
517                         if (debug)
518                                 debug_printf("WARNING: entry with only %d fields, skipping.\n", vecp);
519                         continue;
520                 }
521
522                 if (debug) {
523                         for (i = 0; vec[i]; i++)
524                                 debug_printf("vec[%d]: \"%s\"\n",
525                                                 i, trim(vec[i]));
526                 }
527
528                 field   = vec[0];
529                 pattern = vec[1];
530                 action  = vec[2];
531                 result  = vec[3];
532                 string  = vec[4];
533
534                 /* find out how to perform the action */
535                 switch (result[0]) {
536                 case 'N':
537                 case 'n':
538                         /*
539                         ** If previous condition failed, don't
540                         ** do this - else fall through
541                         */
542                          if (!next)
543                                 continue;  /* else fall */
544
545                 case '?':
546                         /*
547                         ** If already delivered, skip this action.
548                         ** Else consider delivered if action is
549                         ** successful.
550                         */
551                         if (won)
552                                 continue;  /* else fall */
553
554                 case 'A':
555                 case 'a':
556                         /*
557                         ** Take action, and consider delivered if
558                         ** action is successful.
559                         */
560                         accept = 1;
561                         break;
562
563                 case 'R':
564                 case 'r':
565                 default:
566                         /*
567                         ** Take action, but don't consider delivered,
568                         ** even if action is successful
569                         */
570                         accept = 0;
571                         break;
572                 }
573
574                 if (vecp > 5) {
575                         if (!mh_strcasecmp(vec[5], "select")) {
576                                 if (logged_in() != -1)
577                                         continue;
578                                 if (vecp > 7 && timely(vec[6], vec[7]) == -1)
579                                         continue;
580                         }
581                 }
582
583                 /* check if the field matches */
584                 switch (*field) {
585                 case '*':
586                 /* always matches */
587                         break;
588
589                 case 'd':
590                 /*
591                 ** "default" matches only if the message hasn't
592                 ** been delivered yet.
593                 */
594                         if (!mh_strcasecmp(field, "default")) {
595                                 if (won)
596                                         continue;
597                                 break;
598                         }  /* else fall */
599
600                 default:
601                         /* parse message and build lookup table */
602                         if (!parsed && parse(fd) == -1) {
603                                 fclose(fp);
604                                 return -1;
605                         }
606                         /*
607                         ** find header field in lookup table, and
608                         ** see if the pattern matches.
609                         */
610                         if ((p = lookup(hdrs, field)) && (p->p_value != NULL)
611                                         && matches(p->p_value, pattern)) {
612                                 next = 1;
613                         } else {
614                                 next = 0;
615                                 continue;
616                         }
617                         break;
618                 }
619
620                 /* find out the action to perform */
621                 switch (*action) {
622                 case 'q':
623                         /* deliver to quoted pipe */
624                         if (mh_strcasecmp(action, "qpipe"))
625                                 continue;  /* else fall */
626                 case '^':
627                         expand(tmpbuf, string, fd);
628                         if (split(tmpbuf, vec) < 1)
629                                 continue;
630                         status = usr_pipe(fd, tmpbuf, vec[0], vec, 0);
631                         break;
632
633                 case 'p':
634                         /* deliver to pipe */
635                         if (mh_strcasecmp(action, "pipe"))
636                                 continue;  /* else fall */
637                 case '|':
638                         vec[2] = "sh";
639                         vec[3] = "-c";
640                         expand(tmpbuf, string, fd);
641                         vec[4] = tmpbuf;
642                         vec[5] = NULL;
643                         status = usr_pipe(fd, tmpbuf, "/bin/sh",
644                                         vec + 2, 0);
645                         break;
646
647                 case 'f':
648                         /* mbox format */
649                         if (!mh_strcasecmp(action, "file")) {
650                                 status = usr_file(fd, string,
651                                                 MBOX_FORMAT);
652                                 break;
653                         }
654                         /* deliver to nmh folder */
655                         else if (mh_strcasecmp(action, "folder"))
656                                 continue;  /* else fall */
657                 case '+':
658                         status = usr_folder(fd, string);
659                         break;
660
661                 case 'm':
662                         /* mmdf format */
663                         if (!mh_strcasecmp(action, "mmdf")) {
664                                 status = usr_file(fd, string,
665                                                 MMDF_FORMAT);
666                                 break;
667                         }
668                         /* mbox format */
669                         else if (mh_strcasecmp(action, "mbox"))
670                                 continue;  /* else fall */
671
672                 case '>':
673                         /* mbox format */
674                         status = usr_file(fd, string, MBOX_FORMAT);
675                         break;
676
677                 case 'd':
678                         /* ignore message */
679                         if (mh_strcasecmp(action, "destroy"))
680                                 continue;
681                         status = 0;
682                         break;
683                 }
684
685                 if (status)
686                         next = 0;  /* action failed, mark for 'N' result */
687
688                 if (accept && status == 0)
689                         won++;
690         }
691
692         fclose(fp);
693         return (won ? 0 : -1);
694 }
695
696
697 #define QUOTE  '\\'
698
699 /*
700 ** Split buffer into fields (delimited by whitespace or
701 ** comma's).  Return the number of fields found.
702 */
703
704 static int
705 split(char *cp, char **vec)
706 {
707         int i;
708         unsigned char *s;
709
710         s = cp;
711
712         /* split into a maximum of NVEC fields */
713         for (i = 0; i <= NVEC;) {
714                 vec[i] = NULL;
715
716                 /* zap any whitespace and comma's */
717                 while (isspace(*s) || *s == ',')
718                         *s++ = 0;
719
720                 /* end of buffer, time to leave */
721                 if (*s == 0)
722                         break;
723
724                 /* get double quote text as a single field */
725                 if (*s == '"') {
726                         for (vec[i++] = ++s; *s && *s != '"'; s++) {
727                                 /*
728                                 ** Check for escaped double quote.  We need
729                                 ** to shift the string to remove slash.
730                                 */
731                                 if (*s == QUOTE) {
732                                         if (*++s == '"')
733                                                 strcpy(s - 1, s);
734                                         s--;
735                                 }
736                         }
737                         if (*s == '"')  /* zap trailing double quote */
738                                 *s++ = 0;
739                         continue;
740                 }
741
742                 if (*s == QUOTE && *++s != '"')
743                         s--;
744                 vec[i++] = s++;
745
746                 /* move forward to next field delimiter */
747                 while (*s && !isspace(*s) && *s != ',')
748                         s++;
749         }
750         vec[i] = NULL;
751
752         return i;
753 }
754
755
756 /*
757 ** Parse the headers of a message, and build the
758 ** lookup table for matching fields and patterns.
759 */
760
761 static int
762 parse(int fd)
763 {
764         int i, state;
765         int fd1;
766         char *cp, *dp, *lp;
767         char name[NAMESZ], field[BUFSIZ];
768         struct pair *p, *q;
769         FILE  *in;
770
771         if (parsed++)
772                 return 0;
773
774         /* get a new FILE pointer to message */
775         if ((fd1 = dup(fd)) == -1)
776                 return -1;
777         if ((in = fdopen(fd1, "r")) == NULL) {
778                 close(fd1);
779                 return -1;
780         }
781         rewind(in);
782
783         /* add special entries to lookup table */
784         if ((p = lookup(hdrs, "source")))
785                 p->p_value = getcpy(sender);
786         if ((p = lookup(hdrs, "addr")))
787                 p->p_value = getcpy(addr);
788
789         /*
790          * Scan the headers of the message and build
791          * a lookup table.
792          */
793         for (i = 0, state = FLD;;) {
794                 switch (state = m_getfld(state, name, field, sizeof(field),
795                                 in)) {
796                 case FLD:
797                 case FLDEOF:
798                 case FLDPLUS:
799                         lp = getcpy(field);
800                         while (state == FLDPLUS) {
801                                 state = m_getfld(state, name, field,
802                                                 sizeof(field), in);
803                                 lp = add(field, lp);
804                         }
805                         for (p = hdrs; p->p_name; p++) {
806                                 if (!mh_strcasecmp(p->p_name, name)) {
807                                         if (!(p->p_flags & P_HID)) {
808                                                 if ((cp = p->p_value)) {
809                                                         if (p->p_flags & P_ADR) {
810                                                                 dp = cp + strlen(cp) - 1;
811                                                                 if (*dp == '\n')
812                                                                         *dp = 0;
813                                                                 cp = add(",\n\t", cp);
814                                                         } else {
815                                                                 cp = add("\t", cp);
816                                                         }
817                                                 }
818                                                 p->p_value = add(lp, cp);
819                                         }
820                                         free(lp);
821                                         break;
822                                 }
823                         }
824                         if (p->p_name == NULL && i < NVEC) {
825                                 p->p_name = getcpy(name);
826                                 p->p_value = lp;
827                                 p->p_flags = P_NIL;
828                                 p++, i++;
829                                 p->p_name = NULL;
830                         }
831                         if (state != FLDEOF)
832                                 continue;
833                         break;
834
835                 case BODY:
836                 case BODYEOF:
837                 case FILEEOF:
838                         break;
839
840                 case LENERR:
841                 case FMTERR:
842                         advise(NULL, "format error in message");
843                         break;
844
845                 default:
846                         advise(NULL, "internal error in m_getfld");
847                         fclose(in);
848                         return -1;
849                 }
850                 break;
851         }
852         fclose(in);
853
854         if ((p = lookup(vars, "reply-to"))) {
855                 if ((q = lookup(hdrs, "reply-to")) == NULL ||
856                                 q->p_value == NULL)
857                         q = lookup(hdrs, "from");
858                 p->p_value = getcpy(q ? q->p_value : "");
859                 p->p_flags &= ~P_CHK;
860                 if (debug)
861                         debug_printf("vars[%d]: name=\"%s\" value=\"%s\"\n",
862                                         p - vars, p->p_name, trim(p->p_value));
863         }
864         if (debug) {
865                 for (p = hdrs; p->p_name; p++)
866                         debug_printf("hdrs[%d]: name=\"%s\" value=\"%s\"\n",
867                                 p - hdrs, p->p_name,
868                                 p->p_value ? trim(p->p_value) : "");
869         }
870
871         return 0;
872 }
873
874
875 #define LPAREN '('
876 #define RPAREN ')'
877
878 /*
879 ** Expand any builtin variables such as $(sender),
880 ** $(address), etc., in a string.
881 */
882
883 static void
884 expand(char *s1, char *s2, int fd)
885 {
886         char c, *cp;
887         struct pair *p;
888
889         if (!globbed)
890                 glob(fd);
891
892         while ((c = *s2++)) {
893                 if (c != '$' || *s2 != LPAREN) {
894                         *s1++ = c;
895                 } else {
896                         for (cp = ++s2; *s2 && *s2 != RPAREN; s2++)
897                                 continue;
898                         if (*s2 != RPAREN) {
899                                 s2 = --cp;
900                                 continue;
901                         }
902                         *s2++ = 0;
903                         if ((p = lookup(vars, cp))) {
904                                 if (!parsed && (p->p_flags & P_CHK))
905                                         parse(fd);
906
907                                 strcpy(s1, p->p_value);
908                                 s1 += strlen(s1);
909                         }
910                 }
911         }
912         *s1 = 0;
913 }
914
915
916 /*
917 ** Fill in the information missing from the "vars"
918 ** table, which is necessary to expand any builtin
919 ** variables in the string for a "pipe" or "qpipe"
920 ** action.
921 */
922
923 static void
924 glob(int fd)
925 {
926         char buffer[BUFSIZ];
927         struct stat st;
928         struct pair *p;
929
930         if (globbed++)
931                 return;
932
933         if ((p = lookup(vars, "sender")))
934                 p->p_value = getcpy(sender);
935         if ((p = lookup(vars, "address")))
936                 p->p_value = getcpy(addr);
937         if ((p = lookup(vars, "size"))) {
938                 snprintf(buffer, sizeof(buffer), "%d",
939                                 fstat(fd, &st) != -1 ? (int) st.st_size : 0);
940                 p->p_value = getcpy(buffer);
941         }
942         if ((p = lookup(vars, "info")))
943                 p->p_value = getcpy(info);
944
945         if (debug) {
946                 for (p = vars; p->p_name; p++)
947                         debug_printf("vars[%d]: name=\"%s\" value=\"%s\"\n",
948                                         p - vars, p->p_name, trim(p->p_value));
949         }
950 }
951
952
953 /*
954 ** Find a matching name in a lookup table.  If found,
955 ** return the "pairs" entry, else return NULL.
956 */
957
958 static struct pair *
959 lookup(struct pair *pairs, char *key)
960 {
961         for (; pairs->p_name; pairs++)
962                 if (!mh_strcasecmp(pairs->p_name, key))
963                         return pairs;
964
965         return NULL;
966 }
967
968
969 /*
970 ** Check utmp(x) file to see if user is currently
971 ** logged in.
972 */
973
974 #ifdef HAVE_GETUTENT
975 static int
976 logged_in(void)
977 {
978         struct utmp * utp;
979
980         if (utmped)
981                 return utmped;
982
983         setutent();
984
985         while ((utp = getutent()) != NULL) {
986                 if (
987 #ifdef HAVE_STRUCT_UTMP_UT_TYPE
988                                 utp->ut_type == USER_PROCESS &&
989 #endif
990                                 utp->ut_name[0] != 0 &&
991                                 strncmp(user, utp->ut_name,
992                                 sizeof(utp->ut_name)) == 0) {
993                         if (debug)
994                                 continue;
995                         endutent();
996                         return (utmped = DONE);
997                 }
998         }
999
1000         endutent();
1001         return (utmped = NOTOK);
1002 }
1003 #else
1004 static int
1005 logged_in(void)
1006 {
1007         struct utmp ut;
1008         FILE *uf;
1009
1010         if (utmped)
1011                 return utmped;
1012
1013         if ((uf = fopen(UTMP_FILE, "r")) == NULL)
1014                 return NOTOK;
1015
1016         while (fread((char *) &ut, sizeof(ut), 1, uf) == 1) {
1017                 if (ut.ut_name[0] != 0 &&
1018                                 strncmp(user, ut.ut_name, sizeof(ut.ut_name))
1019                                 == 0) {
1020                         if (debug)
1021                                 continue;
1022                         fclose(uf);
1023                         return (utmped = DONE);
1024                 }
1025         }
1026
1027         fclose(uf);
1028         return (utmped = NOTOK);
1029 }
1030 #endif
1031
1032 #define check(t,a,b)  if (t < a || t > b) return -1
1033 #define cmpar(h1,m1,h2,m2)  if (h1 < h2 || (h1 == h2 && m1 < m2)) return 0
1034
1035 static int
1036 timely(char *t1, char *t2)
1037 {
1038         int t1hours, t1mins, t2hours, t2mins;
1039
1040         if (sscanf(t1, "%d:%d", &t1hours, &t1mins) != 2)
1041                 return -1;
1042         check(t1hours, 0, 23);
1043         check(t1mins, 0, 59);
1044
1045         if (sscanf(t2, "%d:%d", &t2hours, &t2mins) != 2)
1046                 return -1;
1047         check(t2hours, 0, 23);
1048         check(t2mins, 0, 59);
1049
1050         cmpar(now->tw_hour, now->tw_min, t1hours, t1mins);
1051         cmpar(t2hours, t2mins, now->tw_hour, now->tw_min);
1052
1053         return -1;
1054 }
1055
1056
1057 /*
1058 ** Deliver message by appending to a file.
1059 */
1060
1061 static int
1062 usr_file(int fd, char *mailbox, int mbx_style)
1063 {
1064         int md, mapping;
1065
1066         if (verbose)
1067                 verbose_printf("delivering to file \"%s\"", mailbox);
1068
1069         if (mbx_style == MBOX_FORMAT) {
1070                 if (verbose)
1071                         verbose_printf(" (mbox style)");
1072                 mapping = 0;
1073         } else {
1074                 if (verbose)
1075                         verbose_printf(" (mmdf style)");
1076                 mapping = 1;
1077         }
1078
1079         /* open and lock the file */
1080         if ((md = mbx_open(mailbox, mbx_style, pw->pw_uid, pw->pw_gid,
1081                         m_gmprot())) == -1) {
1082                 if (verbose)
1083                         adorn("", "unable to open:");
1084                 return -1;
1085         }
1086
1087         lseek(fd, (off_t) 0, SEEK_SET);
1088
1089         /* append message to file */
1090         if (mbx_copy(mailbox, mbx_style, md, fd, mapping, NULL, verbose)
1091                         == -1) {
1092                 if (verbose)
1093                         adorn("", "error writing to:");
1094                 return -1;
1095         }
1096
1097         /* close and unlock file */
1098         if (mbx_close(mailbox, md) == NOTOK) {
1099                 if (verbose)
1100                         adorn("", "error closing:");
1101                 return -1;
1102         }
1103
1104         if (verbose)
1105                 verbose_printf(", success.\n");
1106         return 0;
1107 }
1108
1109
1110 /*
1111 ** Deliver message to a nmh folder.
1112 */
1113
1114 static int
1115 usr_folder(int fd, char *string)
1116 {
1117         int status;
1118         char folder[BUFSIZ], *vec[3];
1119
1120         /* get folder name ready */
1121         if (*string == '+')
1122                 strncpy(folder, string, sizeof(folder));
1123         else
1124                 snprintf(folder, sizeof(folder), "+%s", string);
1125
1126         if (verbose)
1127                 verbose_printf("delivering to folder \"%s\"", folder + 1);
1128
1129         vec[0] = "rcvstore";
1130         vec[1] = folder;
1131         vec[2] = NULL;
1132
1133         /* use rcvstore to put message in folder */
1134         status = usr_pipe(fd, "rcvstore", rcvstoreproc, vec, 1);
1135
1136 #if 0
1137         /*
1138         ** Currently, verbose status messages are handled by usr_pipe().
1139         */
1140         if (verbose) {
1141                 if (status == 0)
1142                         verbose_printf(", success.\n");
1143                 else
1144                         verbose_printf(", failed.\n");
1145         }
1146 #endif
1147
1148         return status;
1149 }
1150
1151 /*
1152 ** Deliver message to a process.
1153 */
1154
1155 static int
1156 usr_pipe(int fd, char *cmd, char *pgm, char **vec, int suppress)
1157 {
1158         pid_t child_id;
1159         int i, bytes, seconds, status;
1160         struct stat st;
1161
1162         if (verbose && !suppress)
1163                 verbose_printf("delivering to pipe \"%s\"", cmd);
1164
1165         lseek(fd, (off_t) 0, SEEK_SET);
1166
1167         for (i = 0; (child_id = fork()) == -1 && i < 5; i++)
1168                 sleep(5);
1169
1170         switch (child_id) {
1171         case -1:
1172                 /* fork error */
1173                 if (verbose)
1174                         adorn("fork", "unable to");
1175                 return -1;
1176
1177         case 0:
1178                 /* child process */
1179                 if (fd != 0)
1180                         dup2(fd, 0);
1181                 freopen("/dev/null", "w", stdout);
1182                 freopen("/dev/null", "w", stderr);
1183                 if (fd != 3)
1184                         dup2(fd, 3);
1185                 closefds(4);
1186
1187 #ifdef TIOCNOTTY
1188                 if ((fd = open("/dev/tty", O_RDWR)) != -1) {
1189                         ioctl(fd, TIOCNOTTY, NULL);
1190                         close(fd);
1191                 }
1192 #endif /* TIOCNOTTY */
1193
1194                 /* put in own process group */
1195                 setpgid((pid_t) 0, getpid());
1196
1197                 *environ = NULL;
1198                 m_putenv("USER", pw->pw_name);
1199                 m_putenv("HOME", pw->pw_dir);
1200                 m_putenv("SHELL", pw->pw_shell);
1201
1202                 execvp(pgm, vec);
1203                 _exit(-1);
1204
1205         default:
1206                 /* parent process */
1207                 if (!setjmp(myctx)) {
1208                         SIGNAL(SIGALRM, alrmser);
1209                         bytes = fstat(fd, &st) != -1 ?
1210                                         (int) st.st_size : 100;
1211
1212                         /*
1213                         ** amount of time to wait depends on
1214                         ** message size
1215                         */
1216                         if (bytes <= 100) {
1217                                 /* give at least 5 minutes */
1218                                 seconds = 300;
1219                         } else if (bytes >= 90000) {
1220                                 /* a half hour is long enough */
1221                                 seconds = 1800;
1222                         } else {
1223                                 seconds = (bytes / 60) + 300;
1224                         }
1225                         alarm((unsigned int) seconds);
1226                         status = pidwait(child_id, 0);
1227                         alarm(0);
1228
1229                         if (verbose) {
1230                                 if (status == 0)
1231                                         verbose_printf(", success.\n");
1232                                 else
1233                                         if ((status & 0xff00) == 0xff00)
1234                                                 verbose_printf(", system error\n");
1235                                         else
1236                                                 pidstatus(status, stdout, ", failed");
1237                         }
1238                         return (status == 0 ? 0 : -1);
1239                 } else {
1240                         /*
1241                         ** Ruthlessly kill the child and anything
1242                         ** else in its process group.
1243                         */
1244                         KILLPG(child_id, SIGKILL);
1245                         if (verbose)
1246                                 verbose_printf(", timed-out; terminated\n");
1247                         return -1;
1248                 }
1249         }
1250 }
1251
1252
1253 static RETSIGTYPE
1254 alrmser(int i)
1255 {
1256 #ifndef RELIABLE_SIGNALS
1257         SIGNAL(SIGALRM, alrmser);
1258 #endif
1259
1260         longjmp(myctx, DONE);
1261 }
1262
1263
1264 /*
1265 ** Get the `sender' from the envelope
1266 ** information ("From " line).
1267 */
1268
1269 static void
1270 get_sender(char *envelope, char **sender)
1271 {
1272         int i;
1273         unsigned char *cp;
1274         unsigned char buffer[BUFSIZ];
1275
1276         if (envelope == NULL) {
1277                 *sender = getcpy("");
1278                 return;
1279         }
1280
1281         i = strlen("From ");
1282         strncpy(buffer, envelope + i, sizeof(buffer));
1283         if ((cp = strchr(buffer, '\n'))) {
1284                 *cp = 0;
1285                 cp -= 24;
1286                 if (cp < buffer)
1287                         cp = buffer;
1288         } else {
1289                 cp = buffer;
1290         }
1291         *cp = 0;
1292
1293         for (cp = buffer + strlen(buffer) - 1; cp >= buffer; cp--)
1294                 if (isspace(*cp))
1295                         *cp = 0;
1296                 else
1297                         break;
1298         *sender = getcpy(buffer);
1299 }
1300
1301
1302 /*
1303 ** Copy message into a temporary file.
1304 ** While copying, it will do some header processing
1305 ** including the extraction of the envelope information.
1306 */
1307
1308 static int
1309 copy_message(int qd, char *tmpfil, int fold)
1310 {
1311         int i, first = 1, fd1, fd2;
1312         char buffer[BUFSIZ];
1313         FILE *qfp, *ffp;
1314         char *tfile = NULL;
1315
1316         tfile = m_mktemp2(NULL, invo_name, &fd1, NULL);
1317         if (tfile == NULL) return -1;
1318         fchmod(fd1, 0600);
1319         strncpy(tmpfil, tfile, BUFSIZ);
1320
1321         if (!fold) {
1322                 while ((i = read(qd, buffer, sizeof(buffer))) > 0)
1323                         if (write(fd1, buffer, i) != i) {
1324 you_lose:
1325                                 close(fd1);
1326                                 unlink(tmpfil);
1327                                 return -1;
1328                         }
1329                 if (i == -1)
1330                         goto you_lose;
1331                 lseek(fd1, (off_t) 0, SEEK_SET);
1332                 return fd1;
1333         }
1334
1335         /* dup the fd for incoming message */
1336         if ((fd2 = dup(qd)) == -1) {
1337                 close(fd1);
1338                 return -1;
1339         }
1340
1341         /* now create a FILE pointer for it */
1342         if ((qfp = fdopen(fd2, "r")) == NULL) {
1343                 close(fd1);
1344                 close(fd2);
1345                 return -1;
1346         }
1347
1348         /* dup the fd for temporary file */
1349         if ((fd2 = dup(fd1)) == -1) {
1350                 close(fd1);
1351                 fclose(qfp);
1352                 return -1;
1353         }
1354
1355         /* now create a FILE pointer for it */
1356         if ((ffp = fdopen(fd2, "r+")) == NULL) {
1357                 close(fd1);
1358                 close(fd2);
1359                 fclose(qfp);
1360                 return -1;
1361         }
1362
1363         /*
1364         ** copy message into temporary file
1365         ** and massage the headers.  Save
1366         ** a copy of the "From " line for later.
1367         */
1368         i = strlen("From ");
1369         while (fgets(buffer, sizeof(buffer), qfp)) {
1370                 if (first) {
1371                         first = 0;
1372                         if (!strncmp(buffer, "From ", i)) {
1373 #ifdef RPATHS
1374                                 char *fp, *cp, *hp, *ep;
1375 #endif
1376                                 /*
1377                                 ** get copy of envelope information
1378                                 ** ("From " line)
1379                                 */
1380                                 envelope = getcpy(buffer);
1381
1382 #if 0
1383                                 /*
1384                                 ** First go ahead and put "From " line
1385                                 ** in message
1386                                 */
1387                                 fputs(buffer, ffp);
1388                                 if (ferror(ffp))
1389                                         goto fputs_error;
1390 #endif
1391
1392 #ifdef RPATHS
1393                                 /*
1394                                 ** Now create a "Return-Path:" line
1395                                 ** from the "From " line.
1396                                 */
1397                                 hp = cp = strchr(fp = envelope + i, ' ');
1398                                 while ((hp = strchr(++hp, 'r')))
1399                                         if (uprf(hp, "remote from")) {
1400                                                 hp = strrchr(hp, ' ');
1401                                                 break;
1402                                         }
1403                                 if (hp) {
1404                                         /*
1405                                         ** return path for UUCP style
1406                                         ** addressing
1407                                         */
1408                                         ep = strchr(++hp, '\n');
1409                                         snprintf(buffer, sizeof(buffer), "Return-Path: %.*s!%.*s\n", (int)(ep - hp), hp, (int)(cp - fp), fp);
1410                                 } else {
1411                                         /*
1412                                         ** return path for standard domain
1413                                         ** addressing
1414                                         */
1415                                         snprintf(buffer, sizeof(buffer), "Return-Path: %.*s\n", (int)(cp - fp), fp);
1416                                 }
1417
1418                                 /* Add Return-Path header to message */
1419                                 fputs(buffer, ffp);
1420                                 if (ferror(ffp))
1421                                         goto fputs_error;
1422 #endif
1423                                 /* Put the delivery date in message */
1424                                 fputs(ddate, ffp);
1425                                 if (ferror(ffp))
1426                                         goto fputs_error;
1427
1428                                 continue;
1429                         }
1430                 }
1431
1432                 fputs(buffer, ffp);
1433                 if (ferror(ffp))
1434                         goto fputs_error;
1435         }
1436
1437         fclose(ffp);
1438         if (ferror(qfp)) {
1439                 close(fd1);
1440                 fclose(qfp);
1441                 return -1;
1442         }
1443         fclose(qfp);
1444         lseek(fd1, (off_t) 0, SEEK_SET);
1445         return fd1;
1446
1447
1448 fputs_error:
1449         close(fd1);
1450         fclose(ffp);
1451         fclose(qfp);
1452         return -1;
1453 }
1454
1455 /*
1456 ** Trim strings for pretty printing of debugging output
1457 */
1458
1459 static char *
1460 trim(char *cp)
1461 {
1462         char buffer[BUFSIZ*4];
1463         unsigned char *bp, *sp;
1464
1465         if (cp == NULL)
1466                 return NULL;
1467
1468         /* copy string into temp buffer */
1469         strncpy(buffer, cp, sizeof(buffer));
1470         bp = buffer;
1471
1472         /* skip over leading whitespace */
1473         while (isspace(*bp))
1474                 bp++;
1475
1476         /* start at the end and zap trailing whitespace */
1477         for (sp = bp + strlen(bp) - 1; sp >= bp; sp--) {
1478                 if (isspace(*sp))
1479                         *sp = 0;
1480                 else
1481                         break;
1482         }
1483
1484         /* replace remaining whitespace with spaces */
1485         for (sp = bp; *sp; sp++)
1486                 if (isspace(*sp))
1487                         *sp = ' ';
1488
1489         /* now return a copy */
1490         return getcpy(bp);
1491 }
1492
1493 /*
1494 ** Function for printing `verbose' messages.
1495 */
1496
1497 static void
1498 verbose_printf(char *fmt, ...)
1499 {
1500         va_list ap;
1501
1502         va_start(ap, fmt);
1503         vfprintf(stdout, fmt, ap);
1504         va_end(ap);
1505
1506         fflush(stdout);  /* now flush output */
1507 }
1508
1509
1510 /*
1511 ** Function for printing `verbose' delivery
1512 ** error messages.
1513 */
1514
1515 static void
1516 adorn(char *what, char *fmt, ...)
1517 {
1518         va_list ap;
1519         int eindex;
1520         char *s;
1521
1522         eindex = errno;  /* save the errno */
1523         fprintf(stdout, ", ");
1524
1525         va_start(ap, fmt);
1526         vfprintf(stdout, fmt, ap);
1527         va_end(ap);
1528
1529         if (what) {
1530                 if (*what)
1531                         fprintf(stdout, " %s: ", what);
1532                 if ((s = strerror(eindex)))
1533                         fprintf(stdout, "%s", s);
1534                 else
1535                         fprintf(stdout, "Error %d", eindex);
1536         }
1537
1538         fputc('\n', stdout);
1539         fflush(stdout);
1540 }
1541
1542
1543 /*
1544 ** Function for printing `debug' messages.
1545 */
1546
1547 static void
1548 debug_printf(char *fmt, ...)
1549 {
1550         va_list ap;
1551
1552         va_start(ap, fmt);
1553         vfprintf(stderr, fmt, ap);
1554         va_end(ap);
1555 }
1556
1557
1558 /*
1559 ** Check ndbm/db file(s) to see if the Message-Id of this
1560 ** message matches the Message-Id of a previous message,
1561 ** so we can discard it.  If it doesn't match, we add the
1562 ** Message-Id of this message to the ndbm/db file.
1563 */
1564 static int
1565 suppress_duplicates(int fd, char *file)
1566 {
1567         int fd1, lockfd, state, result;
1568         char *cp, buf[BUFSIZ], name[NAMESZ];
1569         datum key, value;
1570         DBM *db;
1571         FILE *in;
1572
1573         if ((fd1 = dup(fd)) == -1)
1574                 return -1;
1575         if (!(in = fdopen(fd1, "r"))) {
1576                 close(fd1);
1577                 return -1;
1578         }
1579         rewind(in);
1580
1581         for (state = FLD;;) {
1582                 state = m_getfld(state, name, buf, sizeof(buf), in);
1583                 switch (state) {
1584                 case FLD:
1585                 case FLDPLUS:
1586                 case FLDEOF:
1587                         /* Search for the message ID */
1588                         if (mh_strcasecmp(name, "Message-ID")) {
1589                                 while (state == FLDPLUS)
1590                                         state = m_getfld(state, name, buf,
1591                                                         sizeof(buf), in);
1592                                 continue;
1593                         }
1594
1595                         cp = getcpy(buf);
1596                         while (state == FLDPLUS) {
1597                                 state = m_getfld(state, name, buf,
1598                                                 sizeof(buf), in);
1599                                 cp = add(buf, cp);
1600                         }
1601                         key.dptr = trimcpy(cp);
1602                         key.dsize = strlen(key.dptr) + 1;
1603                         free(cp);
1604                         cp = key.dptr;
1605
1606                         if (!(db = dbm_open(file, O_RDWR | O_CREAT, 0600))) {
1607                                 advise(file, "unable to perform dbm_open on");
1608                                 free(cp);
1609                                 fclose(in);
1610                                 return -1;
1611                         }
1612                         /*
1613                         ** Since it is difficult to portable lock a ndbm
1614                         ** file, we will open and lock the Maildelivery
1615                         ** file instead.  This will fail if your Maildelivery
1616                         ** file doesn't exist.
1617                         */
1618                         if ((lockfd = lkopen(file, O_RDWR, 0)) == -1) {
1619                                 advise(file, "unable to perform file locking on");
1620                                 free(cp);
1621                                 fclose(in);
1622                                 return -1;
1623                         }
1624                         value = dbm_fetch(db, key);
1625                         if (value.dptr) {
1626                                 if (verbose)
1627                                         verbose_printf("Message-ID: %s\n            already received on %s", cp, value.dptr);
1628                                 result = DONE;
1629                         } else {
1630                                 value.dptr  = ddate + sizeof("Delivery-Date:");
1631                                 value.dsize = strlen(value.dptr) + 1;
1632                                 if (dbm_store(db, key, value, DBM_INSERT))
1633                                         advise(file, "possibly corrupt file");
1634                                 result = 0;
1635                         }
1636
1637                         dbm_close(db);
1638                         lkclose(lockfd, file);
1639                         free(cp);
1640                         fclose(in);
1641                         return result;
1642                         break;
1643
1644                 case BODY:
1645                 case BODYEOF:
1646                 case FILEEOF:
1647                         break;
1648
1649                 case LENERR:
1650                 case FMTERR:
1651                 default:
1652                         break;
1653                 }
1654
1655                 break;
1656         }
1657
1658         fclose(in);
1659         return 0;
1660 }