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