a81afa90eda8fabd305ac535a446c7bc4b2fdc68
[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 *);
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         return usr_file(fd, mbox);
461 }
462
463
464 #define matches(a,b) (stringdex(b, a) >= 0)
465
466 /*
467 ** Parse the delivery file, and process incoming message.
468 */
469
470 static int
471 usr_delivery(int fd, char *delivery, int su)
472 {
473         int i, accept, status=1, won, vecp, next;
474         char *field, *pattern, *action, *result, *string;
475         char buffer[BUFSIZ], tmpbuf[BUFSIZ];
476         char *cp, *vec[NVEC];
477         struct stat st;
478         struct pair *p;
479         FILE *fp;
480
481         /* open the delivery file */
482         if ((fp = fopen(delivery, "r")) == NULL)
483                 return -1;
484
485         /* check if delivery file has bad ownership or permissions */
486         if (fstat(fileno(fp), &st) == -1 ||
487                         (st.st_uid != 0 && (su || st.st_uid != pw->pw_uid)) ||
488                         st.st_mode & (S_IWGRP|S_IWOTH)) {
489                 if (verbose) {
490                         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);
491                 }
492                 return -1;
493         }
494
495         won = 0;
496         next = 1;
497
498         /* read and process delivery file */
499         while (fgets(buffer, sizeof(buffer), fp)) {
500                 /* skip comments and empty lines */
501                 if (*buffer == '#' || *buffer == '\n')
502                         continue;
503
504                 /* zap trailing newline */
505                 if ((cp = strchr(buffer, '\n')))
506                         *cp = 0;
507
508                 /* split buffer into fields */
509                 vecp = split(buffer, vec);
510
511                 /* check for too few fields */
512                 if (vecp < 5) {
513                         if (debug)
514                                 debug_printf("WARNING: entry with only %d fields, skipping.\n", vecp);
515                         continue;
516                 }
517
518                 if (debug) {
519                         for (i = 0; vec[i]; i++)
520                                 debug_printf("vec[%d]: \"%s\"\n",
521                                                 i, trim(vec[i]));
522                 }
523
524                 field   = vec[0];
525                 pattern = vec[1];
526                 action  = vec[2];
527                 result  = vec[3];
528                 string  = vec[4];
529
530                 /* find out how to perform the action */
531                 switch (result[0]) {
532                 case 'N':
533                 case 'n':
534                         /*
535                         ** If previous condition failed, don't
536                         ** do this - else fall through
537                         */
538                          if (!next)
539                                 continue;  /* else fall */
540
541                 case '?':
542                         /*
543                         ** If already delivered, skip this action.
544                         ** Else consider delivered if action is
545                         ** successful.
546                         */
547                         if (won)
548                                 continue;  /* else fall */
549
550                 case 'A':
551                 case 'a':
552                         /*
553                         ** Take action, and consider delivered if
554                         ** action is successful.
555                         */
556                         accept = 1;
557                         break;
558
559                 case 'R':
560                 case 'r':
561                 default:
562                         /*
563                         ** Take action, but don't consider delivered,
564                         ** even if action is successful
565                         */
566                         accept = 0;
567                         break;
568                 }
569
570                 if (vecp > 5) {
571                         if (!mh_strcasecmp(vec[5], "select")) {
572                                 if (logged_in() != -1)
573                                         continue;
574                                 if (vecp > 7 && timely(vec[6], vec[7]) == -1)
575                                         continue;
576                         }
577                 }
578
579                 /* check if the field matches */
580                 switch (*field) {
581                 case '*':
582                 /* always matches */
583                         break;
584
585                 case 'd':
586                 /*
587                 ** "default" matches only if the message hasn't
588                 ** been delivered yet.
589                 */
590                         if (!mh_strcasecmp(field, "default")) {
591                                 if (won)
592                                         continue;
593                                 break;
594                         }  /* else fall */
595
596                 default:
597                         /* parse message and build lookup table */
598                         if (!parsed && parse(fd) == -1) {
599                                 fclose(fp);
600                                 return -1;
601                         }
602                         /*
603                         ** find header field in lookup table, and
604                         ** see if the pattern matches.
605                         */
606                         if ((p = lookup(hdrs, field)) && (p->p_value != NULL)
607                                         && matches(p->p_value, pattern)) {
608                                 next = 1;
609                         } else {
610                                 next = 0;
611                                 continue;
612                         }
613                         break;
614                 }
615
616                 /* find out the action to perform */
617                 switch (*action) {
618                 case 'q':
619                         /* deliver to quoted pipe */
620                         if (mh_strcasecmp(action, "qpipe"))
621                                 continue;  /* else fall */
622                 case '^':
623                         expand(tmpbuf, string, fd);
624                         if (split(tmpbuf, vec) < 1)
625                                 continue;
626                         status = usr_pipe(fd, tmpbuf, vec[0], vec, 0);
627                         break;
628
629                 case 'p':
630                         /* deliver to pipe */
631                         if (mh_strcasecmp(action, "pipe"))
632                                 continue;  /* else fall */
633                 case '|':
634                         vec[2] = "sh";
635                         vec[3] = "-c";
636                         expand(tmpbuf, string, fd);
637                         vec[4] = tmpbuf;
638                         vec[5] = NULL;
639                         status = usr_pipe(fd, tmpbuf, "/bin/sh",
640                                         vec + 2, 0);
641                         break;
642
643                 case 'f':
644                         /* mbox format */
645                         if (!mh_strcasecmp(action, "file")) {
646                                 status = usr_file(fd, string);
647                                 break;
648                         }
649                         /* deliver to nmh folder */
650                         else if (mh_strcasecmp(action, "folder"))
651                                 continue;  /* else fall */
652                 case '+':
653                         status = usr_folder(fd, string);
654                         break;
655
656                 case 'm':
657                         /* mbox format */
658                         if (mh_strcasecmp(action, "mbox"))
659                                 continue;  /* else fall */
660
661                 case '>':
662                         /* mbox format */
663                         status = usr_file(fd, string);
664                         break;
665
666                 case 'd':
667                         /* ignore message */
668                         if (mh_strcasecmp(action, "destroy"))
669                                 continue;
670                         status = 0;
671                         break;
672                 }
673
674                 if (status)
675                         next = 0;  /* action failed, mark for 'N' result */
676
677                 if (accept && status == 0)
678                         won++;
679         }
680
681         fclose(fp);
682         return (won ? 0 : -1);
683 }
684
685
686 #define QUOTE  '\\'
687
688 /*
689 ** Split buffer into fields (delimited by whitespace or
690 ** comma's).  Return the number of fields found.
691 */
692
693 static int
694 split(char *cp, char **vec)
695 {
696         int i;
697         unsigned char *s;
698
699         s = cp;
700
701         /* split into a maximum of NVEC fields */
702         for (i = 0; i <= NVEC;) {
703                 vec[i] = NULL;
704
705                 /* zap any whitespace and comma's */
706                 while (isspace(*s) || *s == ',')
707                         *s++ = 0;
708
709                 /* end of buffer, time to leave */
710                 if (*s == 0)
711                         break;
712
713                 /* get double quote text as a single field */
714                 if (*s == '"') {
715                         for (vec[i++] = ++s; *s && *s != '"'; s++) {
716                                 /*
717                                 ** Check for escaped double quote.  We need
718                                 ** to shift the string to remove slash.
719                                 */
720                                 if (*s == QUOTE) {
721                                         if (*++s == '"')
722                                                 strcpy(s - 1, s);
723                                         s--;
724                                 }
725                         }
726                         if (*s == '"')  /* zap trailing double quote */
727                                 *s++ = 0;
728                         continue;
729                 }
730
731                 if (*s == QUOTE && *++s != '"')
732                         s--;
733                 vec[i++] = s++;
734
735                 /* move forward to next field delimiter */
736                 while (*s && !isspace(*s) && *s != ',')
737                         s++;
738         }
739         vec[i] = NULL;
740
741         return i;
742 }
743
744
745 /*
746 ** Parse the headers of a message, and build the
747 ** lookup table for matching fields and patterns.
748 */
749
750 static int
751 parse(int fd)
752 {
753         int i, state;
754         int fd1;
755         char *cp, *dp, *lp;
756         char name[NAMESZ], field[BUFSIZ];
757         struct pair *p, *q;
758         FILE  *in;
759
760         if (parsed++)
761                 return 0;
762
763         /* get a new FILE pointer to message */
764         if ((fd1 = dup(fd)) == -1)
765                 return -1;
766         if ((in = fdopen(fd1, "r")) == NULL) {
767                 close(fd1);
768                 return -1;
769         }
770         rewind(in);
771
772         /* add special entries to lookup table */
773         if ((p = lookup(hdrs, "source")))
774                 p->p_value = getcpy(sender);
775         if ((p = lookup(hdrs, "addr")))
776                 p->p_value = getcpy(addr);
777
778         /*
779          * Scan the headers of the message and build
780          * a lookup table.
781          */
782         for (i = 0, state = FLD;;) {
783                 switch (state = m_getfld(state, name, field, sizeof(field),
784                                 in)) {
785                 case FLD:
786                 case FLDEOF:
787                 case FLDPLUS:
788                         lp = getcpy(field);
789                         while (state == FLDPLUS) {
790                                 state = m_getfld(state, name, field,
791                                                 sizeof(field), in);
792                                 lp = add(field, lp);
793                         }
794                         for (p = hdrs; p->p_name; p++) {
795                                 if (!mh_strcasecmp(p->p_name, name)) {
796                                         if (!(p->p_flags & P_HID)) {
797                                                 if ((cp = p->p_value)) {
798                                                         if (p->p_flags & P_ADR) {
799                                                                 dp = cp + strlen(cp) - 1;
800                                                                 if (*dp == '\n')
801                                                                         *dp = 0;
802                                                                 cp = add(",\n\t", cp);
803                                                         } else {
804                                                                 cp = add("\t", cp);
805                                                         }
806                                                 }
807                                                 p->p_value = add(lp, cp);
808                                         }
809                                         free(lp);
810                                         break;
811                                 }
812                         }
813                         if (p->p_name == NULL && i < NVEC) {
814                                 p->p_name = getcpy(name);
815                                 p->p_value = lp;
816                                 p->p_flags = P_NIL;
817                                 p++, i++;
818                                 p->p_name = NULL;
819                         }
820                         if (state != FLDEOF)
821                                 continue;
822                         break;
823
824                 case BODY:
825                 case BODYEOF:
826                 case FILEEOF:
827                         break;
828
829                 case LENERR:
830                 case FMTERR:
831                         advise(NULL, "format error in message");
832                         break;
833
834                 default:
835                         advise(NULL, "internal error in m_getfld");
836                         fclose(in);
837                         return -1;
838                 }
839                 break;
840         }
841         fclose(in);
842
843         if ((p = lookup(vars, "reply-to"))) {
844                 if ((q = lookup(hdrs, "reply-to")) == NULL ||
845                                 q->p_value == NULL)
846                         q = lookup(hdrs, "from");
847                 p->p_value = getcpy(q ? q->p_value : "");
848                 p->p_flags &= ~P_CHK;
849                 if (debug)
850                         debug_printf("vars[%d]: name=\"%s\" value=\"%s\"\n",
851                                         p - vars, p->p_name, trim(p->p_value));
852         }
853         if (debug) {
854                 for (p = hdrs; p->p_name; p++)
855                         debug_printf("hdrs[%d]: name=\"%s\" value=\"%s\"\n",
856                                 p - hdrs, p->p_name,
857                                 p->p_value ? trim(p->p_value) : "");
858         }
859
860         return 0;
861 }
862
863
864 #define LPAREN '('
865 #define RPAREN ')'
866
867 /*
868 ** Expand any builtin variables such as $(sender),
869 ** $(address), etc., in a string.
870 */
871
872 static void
873 expand(char *s1, char *s2, int fd)
874 {
875         char c, *cp;
876         struct pair *p;
877
878         if (!globbed)
879                 glob(fd);
880
881         while ((c = *s2++)) {
882                 if (c != '$' || *s2 != LPAREN) {
883                         *s1++ = c;
884                 } else {
885                         for (cp = ++s2; *s2 && *s2 != RPAREN; s2++)
886                                 continue;
887                         if (*s2 != RPAREN) {
888                                 s2 = --cp;
889                                 continue;
890                         }
891                         *s2++ = 0;
892                         if ((p = lookup(vars, cp))) {
893                                 if (!parsed && (p->p_flags & P_CHK))
894                                         parse(fd);
895
896                                 strcpy(s1, p->p_value);
897                                 s1 += strlen(s1);
898                         }
899                 }
900         }
901         *s1 = 0;
902 }
903
904
905 /*
906 ** Fill in the information missing from the "vars"
907 ** table, which is necessary to expand any builtin
908 ** variables in the string for a "pipe" or "qpipe"
909 ** action.
910 */
911
912 static void
913 glob(int fd)
914 {
915         char buffer[BUFSIZ];
916         struct stat st;
917         struct pair *p;
918
919         if (globbed++)
920                 return;
921
922         if ((p = lookup(vars, "sender")))
923                 p->p_value = getcpy(sender);
924         if ((p = lookup(vars, "address")))
925                 p->p_value = getcpy(addr);
926         if ((p = lookup(vars, "size"))) {
927                 snprintf(buffer, sizeof(buffer), "%d",
928                                 fstat(fd, &st) != -1 ? (int) st.st_size : 0);
929                 p->p_value = getcpy(buffer);
930         }
931         if ((p = lookup(vars, "info")))
932                 p->p_value = getcpy(info);
933
934         if (debug) {
935                 for (p = vars; p->p_name; p++)
936                         debug_printf("vars[%d]: name=\"%s\" value=\"%s\"\n",
937                                         p - vars, p->p_name, trim(p->p_value));
938         }
939 }
940
941
942 /*
943 ** Find a matching name in a lookup table.  If found,
944 ** return the "pairs" entry, else return NULL.
945 */
946
947 static struct pair *
948 lookup(struct pair *pairs, char *key)
949 {
950         for (; pairs->p_name; pairs++)
951                 if (!mh_strcasecmp(pairs->p_name, key))
952                         return pairs;
953
954         return NULL;
955 }
956
957
958 /*
959 ** Check utmp(x) file to see if user is currently
960 ** logged in.
961 */
962
963 #ifdef HAVE_GETUTENT
964 static int
965 logged_in(void)
966 {
967         struct utmp * utp;
968
969         if (utmped)
970                 return utmped;
971
972         setutent();
973
974         while ((utp = getutent()) != NULL) {
975                 if (
976 #ifdef HAVE_STRUCT_UTMP_UT_TYPE
977                                 utp->ut_type == USER_PROCESS &&
978 #endif
979                                 utp->ut_name[0] != 0 &&
980                                 strncmp(user, utp->ut_name,
981                                 sizeof(utp->ut_name)) == 0) {
982                         if (debug)
983                                 continue;
984                         endutent();
985                         return (utmped = DONE);
986                 }
987         }
988
989         endutent();
990         return (utmped = NOTOK);
991 }
992 #else
993 static int
994 logged_in(void)
995 {
996         struct utmp ut;
997         FILE *uf;
998
999         if (utmped)
1000                 return utmped;
1001
1002         if ((uf = fopen(UTMP_FILE, "r")) == NULL)
1003                 return NOTOK;
1004
1005         while (fread((char *) &ut, sizeof(ut), 1, uf) == 1) {
1006                 if (ut.ut_name[0] != 0 &&
1007                                 strncmp(user, ut.ut_name, sizeof(ut.ut_name))
1008                                 == 0) {
1009                         if (debug)
1010                                 continue;
1011                         fclose(uf);
1012                         return (utmped = DONE);
1013                 }
1014         }
1015
1016         fclose(uf);
1017         return (utmped = NOTOK);
1018 }
1019 #endif
1020
1021 #define check(t,a,b)  if (t < a || t > b) return -1
1022 #define cmpar(h1,m1,h2,m2)  if (h1 < h2 || (h1 == h2 && m1 < m2)) return 0
1023
1024 static int
1025 timely(char *t1, char *t2)
1026 {
1027         int t1hours, t1mins, t2hours, t2mins;
1028
1029         if (sscanf(t1, "%d:%d", &t1hours, &t1mins) != 2)
1030                 return -1;
1031         check(t1hours, 0, 23);
1032         check(t1mins, 0, 59);
1033
1034         if (sscanf(t2, "%d:%d", &t2hours, &t2mins) != 2)
1035                 return -1;
1036         check(t2hours, 0, 23);
1037         check(t2mins, 0, 59);
1038
1039         cmpar(now->tw_hour, now->tw_min, t1hours, t1mins);
1040         cmpar(t2hours, t2mins, now->tw_hour, now->tw_min);
1041
1042         return -1;
1043 }
1044
1045
1046 /*
1047 ** Deliver message by appending to a file.
1048 */
1049
1050 static int
1051 usr_file(int fd, char *mailbox)
1052 {
1053         int md;
1054
1055         if (verbose)
1056                 verbose_printf("delivering to file \"%s\"", mailbox);
1057
1058         if (verbose)
1059                 verbose_printf(" (mbox style)");
1060
1061         /* open and lock the file */
1062         if ((md = mbx_open(mailbox, pw->pw_uid, pw->pw_gid,
1063                         m_gmprot())) == -1) {
1064                 if (verbose)
1065                         adorn("", "unable to open:");
1066                 return -1;
1067         }
1068
1069         lseek(fd, (off_t) 0, SEEK_SET);
1070
1071         /* append message to file */
1072         if (mbx_copy(mailbox, md, fd, NULL, verbose)
1073                         == -1) {
1074                 if (verbose)
1075                         adorn("", "error writing to:");
1076                 return -1;
1077         }
1078
1079         /* close and unlock file */
1080         if (mbx_close(mailbox, md) == NOTOK) {
1081                 if (verbose)
1082                         adorn("", "error closing:");
1083                 return -1;
1084         }
1085
1086         if (verbose)
1087                 verbose_printf(", success.\n");
1088         return 0;
1089 }
1090
1091
1092 /*
1093 ** Deliver message to a nmh folder.
1094 */
1095
1096 static int
1097 usr_folder(int fd, char *string)
1098 {
1099         int status;
1100         char folder[BUFSIZ], *vec[3];
1101
1102         /* get folder name ready */
1103         if (*string == '+')
1104                 strncpy(folder, string, sizeof(folder));
1105         else
1106                 snprintf(folder, sizeof(folder), "+%s", string);
1107
1108         if (verbose)
1109                 verbose_printf("delivering to folder \"%s\"", folder + 1);
1110
1111         vec[0] = "rcvstore";
1112         vec[1] = folder;
1113         vec[2] = NULL;
1114
1115         /* use rcvstore to put message in folder */
1116         status = usr_pipe(fd, "rcvstore", rcvstoreproc, vec, 1);
1117
1118 #if 0
1119         /*
1120         ** Currently, verbose status messages are handled by usr_pipe().
1121         */
1122         if (verbose) {
1123                 if (status == 0)
1124                         verbose_printf(", success.\n");
1125                 else
1126                         verbose_printf(", failed.\n");
1127         }
1128 #endif
1129
1130         return status;
1131 }
1132
1133 /*
1134 ** Deliver message to a process.
1135 */
1136
1137 static int
1138 usr_pipe(int fd, char *cmd, char *pgm, char **vec, int suppress)
1139 {
1140         pid_t child_id;
1141         int i, bytes, seconds, status;
1142         struct stat st;
1143
1144         if (verbose && !suppress)
1145                 verbose_printf("delivering to pipe \"%s\"", cmd);
1146
1147         lseek(fd, (off_t) 0, SEEK_SET);
1148
1149         for (i = 0; (child_id = fork()) == -1 && i < 5; i++)
1150                 sleep(5);
1151
1152         switch (child_id) {
1153         case -1:
1154                 /* fork error */
1155                 if (verbose)
1156                         adorn("fork", "unable to");
1157                 return -1;
1158
1159         case 0:
1160                 /* child process */
1161                 if (fd != 0)
1162                         dup2(fd, 0);
1163                 freopen("/dev/null", "w", stdout);
1164                 freopen("/dev/null", "w", stderr);
1165                 if (fd != 3)
1166                         dup2(fd, 3);
1167                 closefds(4);
1168
1169 #ifdef TIOCNOTTY
1170                 if ((fd = open("/dev/tty", O_RDWR)) != -1) {
1171                         ioctl(fd, TIOCNOTTY, NULL);
1172                         close(fd);
1173                 }
1174 #endif /* TIOCNOTTY */
1175
1176                 /* put in own process group */
1177                 setpgid((pid_t) 0, getpid());
1178
1179                 *environ = NULL;
1180                 m_putenv("USER", pw->pw_name);
1181                 m_putenv("HOME", pw->pw_dir);
1182                 m_putenv("SHELL", pw->pw_shell);
1183
1184                 execvp(pgm, vec);
1185                 _exit(-1);
1186
1187         default:
1188                 /* parent process */
1189                 if (!setjmp(myctx)) {
1190                         SIGNAL(SIGALRM, alrmser);
1191                         bytes = fstat(fd, &st) != -1 ?
1192                                         (int) st.st_size : 100;
1193
1194                         /*
1195                         ** amount of time to wait depends on
1196                         ** message size
1197                         */
1198                         if (bytes <= 100) {
1199                                 /* give at least 5 minutes */
1200                                 seconds = 300;
1201                         } else if (bytes >= 90000) {
1202                                 /* a half hour is long enough */
1203                                 seconds = 1800;
1204                         } else {
1205                                 seconds = (bytes / 60) + 300;
1206                         }
1207                         alarm((unsigned int) seconds);
1208                         status = pidwait(child_id, 0);
1209                         alarm(0);
1210
1211                         if (verbose) {
1212                                 if (status == 0)
1213                                         verbose_printf(", success.\n");
1214                                 else
1215                                         if ((status & 0xff00) == 0xff00)
1216                                                 verbose_printf(", system error\n");
1217                                         else
1218                                                 pidstatus(status, stdout, ", failed");
1219                         }
1220                         return (status == 0 ? 0 : -1);
1221                 } else {
1222                         /*
1223                         ** Ruthlessly kill the child and anything
1224                         ** else in its process group.
1225                         */
1226                         KILLPG(child_id, SIGKILL);
1227                         if (verbose)
1228                                 verbose_printf(", timed-out; terminated\n");
1229                         return -1;
1230                 }
1231         }
1232 }
1233
1234
1235 static RETSIGTYPE
1236 alrmser(int i)
1237 {
1238 #ifndef RELIABLE_SIGNALS
1239         SIGNAL(SIGALRM, alrmser);
1240 #endif
1241
1242         longjmp(myctx, DONE);
1243 }
1244
1245
1246 /*
1247 ** Get the `sender' from the envelope
1248 ** information ("From " line).
1249 */
1250
1251 static void
1252 get_sender(char *envelope, char **sender)
1253 {
1254         int i;
1255         unsigned char *cp;
1256         unsigned char buffer[BUFSIZ];
1257
1258         if (envelope == NULL) {
1259                 *sender = getcpy("");
1260                 return;
1261         }
1262
1263         i = strlen("From ");
1264         strncpy(buffer, envelope + i, sizeof(buffer));
1265         if ((cp = strchr(buffer, '\n'))) {
1266                 *cp = 0;
1267                 cp -= 24;
1268                 if (cp < buffer)
1269                         cp = buffer;
1270         } else {
1271                 cp = buffer;
1272         }
1273         *cp = 0;
1274
1275         for (cp = buffer + strlen(buffer) - 1; cp >= buffer; cp--)
1276                 if (isspace(*cp))
1277                         *cp = 0;
1278                 else
1279                         break;
1280         *sender = getcpy(buffer);
1281 }
1282
1283
1284 /*
1285 ** Copy message into a temporary file.
1286 ** While copying, it will do some header processing
1287 ** including the extraction of the envelope information.
1288 */
1289
1290 static int
1291 copy_message(int qd, char *tmpfil, int fold)
1292 {
1293         int i, first = 1, fd1, fd2;
1294         char buffer[BUFSIZ];
1295         FILE *qfp, *ffp;
1296         char *tfile = NULL;
1297
1298         tfile = m_mktemp2(NULL, invo_name, &fd1, NULL);
1299         if (tfile == NULL) return -1;
1300         fchmod(fd1, 0600);
1301         strncpy(tmpfil, tfile, BUFSIZ);
1302
1303         if (!fold) {
1304                 while ((i = read(qd, buffer, sizeof(buffer))) > 0)
1305                         if (write(fd1, buffer, i) != i) {
1306 you_lose:
1307                                 close(fd1);
1308                                 unlink(tmpfil);
1309                                 return -1;
1310                         }
1311                 if (i == -1)
1312                         goto you_lose;
1313                 lseek(fd1, (off_t) 0, SEEK_SET);
1314                 return fd1;
1315         }
1316
1317         /* dup the fd for incoming message */
1318         if ((fd2 = dup(qd)) == -1) {
1319                 close(fd1);
1320                 return -1;
1321         }
1322
1323         /* now create a FILE pointer for it */
1324         if ((qfp = fdopen(fd2, "r")) == NULL) {
1325                 close(fd1);
1326                 close(fd2);
1327                 return -1;
1328         }
1329
1330         /* dup the fd for temporary file */
1331         if ((fd2 = dup(fd1)) == -1) {
1332                 close(fd1);
1333                 fclose(qfp);
1334                 return -1;
1335         }
1336
1337         /* now create a FILE pointer for it */
1338         if ((ffp = fdopen(fd2, "r+")) == NULL) {
1339                 close(fd1);
1340                 close(fd2);
1341                 fclose(qfp);
1342                 return -1;
1343         }
1344
1345         /*
1346         ** copy message into temporary file
1347         ** and massage the headers.  Save
1348         ** a copy of the "From " line for later.
1349         */
1350         i = strlen("From ");
1351         while (fgets(buffer, sizeof(buffer), qfp)) {
1352                 if (first) {
1353                         first = 0;
1354                         if (strncmp(buffer, "From ", i)==0) {
1355 #ifdef RPATHS
1356                                 char *fp, *cp, *hp, *ep;
1357 #endif
1358                                 /*
1359                                 ** get copy of envelope information
1360                                 ** ("From " line)
1361                                 */
1362                                 envelope = getcpy(buffer);
1363
1364 #if 0
1365                                 /*
1366                                 ** First go ahead and put "From " line
1367                                 ** in message
1368                                 */
1369                                 fputs(buffer, ffp);
1370                                 if (ferror(ffp))
1371                                         goto fputs_error;
1372 #endif
1373
1374 #ifdef RPATHS
1375                                 /*
1376                                 ** Now create a "Return-Path:" line
1377                                 ** from the "From " line.
1378                                 */
1379                                 hp = cp = strchr(fp = envelope + i, ' ');
1380                                 while ((hp = strchr(++hp, 'r')))
1381                                         if (uprf(hp, "remote from")) {
1382                                                 hp = strrchr(hp, ' ');
1383                                                 break;
1384                                         }
1385                                 if (hp) {
1386                                         /*
1387                                         ** return path for UUCP style
1388                                         ** addressing
1389                                         */
1390                                         ep = strchr(++hp, '\n');
1391                                         snprintf(buffer, sizeof(buffer), "Return-Path: %.*s!%.*s\n", (int)(ep - hp), hp, (int)(cp - fp), fp);
1392                                 } else {
1393                                         /*
1394                                         ** return path for standard domain
1395                                         ** addressing
1396                                         */
1397                                         snprintf(buffer, sizeof(buffer), "Return-Path: %.*s\n", (int)(cp - fp), fp);
1398                                 }
1399
1400                                 /* Add Return-Path header to message */
1401                                 fputs(buffer, ffp);
1402                                 if (ferror(ffp))
1403                                         goto fputs_error;
1404 #endif
1405                                 /* Put the delivery date in message */
1406                                 fputs(ddate, ffp);
1407                                 if (ferror(ffp))
1408                                         goto fputs_error;
1409
1410                                 continue;
1411                         }
1412                 }
1413
1414                 fputs(buffer, ffp);
1415                 if (ferror(ffp))
1416                         goto fputs_error;
1417         }
1418
1419         fclose(ffp);
1420         if (ferror(qfp)) {
1421                 close(fd1);
1422                 fclose(qfp);
1423                 return -1;
1424         }
1425         fclose(qfp);
1426         lseek(fd1, (off_t) 0, SEEK_SET);
1427         return fd1;
1428
1429
1430 fputs_error:
1431         close(fd1);
1432         fclose(ffp);
1433         fclose(qfp);
1434         return -1;
1435 }
1436
1437 /*
1438 ** Trim strings for pretty printing of debugging output
1439 */
1440
1441 static char *
1442 trim(char *cp)
1443 {
1444         char buffer[BUFSIZ*4];
1445         unsigned char *bp, *sp;
1446
1447         if (cp == NULL)
1448                 return NULL;
1449
1450         /* copy string into temp buffer */
1451         strncpy(buffer, cp, sizeof(buffer));
1452         bp = buffer;
1453
1454         /* skip over leading whitespace */
1455         while (isspace(*bp))
1456                 bp++;
1457
1458         /* start at the end and zap trailing whitespace */
1459         for (sp = bp + strlen(bp) - 1; sp >= bp; sp--) {
1460                 if (isspace(*sp))
1461                         *sp = 0;
1462                 else
1463                         break;
1464         }
1465
1466         /* replace remaining whitespace with spaces */
1467         for (sp = bp; *sp; sp++)
1468                 if (isspace(*sp))
1469                         *sp = ' ';
1470
1471         /* now return a copy */
1472         return getcpy(bp);
1473 }
1474
1475 /*
1476 ** Function for printing `verbose' messages.
1477 */
1478
1479 static void
1480 verbose_printf(char *fmt, ...)
1481 {
1482         va_list ap;
1483
1484         va_start(ap, fmt);
1485         vfprintf(stdout, fmt, ap);
1486         va_end(ap);
1487
1488         fflush(stdout);  /* now flush output */
1489 }
1490
1491
1492 /*
1493 ** Function for printing `verbose' delivery
1494 ** error messages.
1495 */
1496
1497 static void
1498 adorn(char *what, char *fmt, ...)
1499 {
1500         va_list ap;
1501         int eindex;
1502         char *s;
1503
1504         eindex = errno;  /* save the errno */
1505         fprintf(stdout, ", ");
1506
1507         va_start(ap, fmt);
1508         vfprintf(stdout, fmt, ap);
1509         va_end(ap);
1510
1511         if (what) {
1512                 if (*what)
1513                         fprintf(stdout, " %s: ", what);
1514                 if ((s = strerror(eindex)))
1515                         fprintf(stdout, "%s", s);
1516                 else
1517                         fprintf(stdout, "Error %d", eindex);
1518         }
1519
1520         fputc('\n', stdout);
1521         fflush(stdout);
1522 }
1523
1524
1525 /*
1526 ** Function for printing `debug' messages.
1527 */
1528
1529 static void
1530 debug_printf(char *fmt, ...)
1531 {
1532         va_list ap;
1533
1534         va_start(ap, fmt);
1535         vfprintf(stderr, fmt, ap);
1536         va_end(ap);
1537 }
1538
1539
1540 /*
1541 ** Check ndbm/db file(s) to see if the Message-Id of this
1542 ** message matches the Message-Id of a previous message,
1543 ** so we can discard it.  If it doesn't match, we add the
1544 ** Message-Id of this message to the ndbm/db file.
1545 */
1546 static int
1547 suppress_duplicates(int fd, char *file)
1548 {
1549         int fd1, lockfd, state, result;
1550         char *cp, buf[BUFSIZ], name[NAMESZ];
1551         datum key, value;
1552         DBM *db;
1553         FILE *in;
1554
1555         if ((fd1 = dup(fd)) == -1)
1556                 return -1;
1557         if (!(in = fdopen(fd1, "r"))) {
1558                 close(fd1);
1559                 return -1;
1560         }
1561         rewind(in);
1562
1563         for (state = FLD;;) {
1564                 state = m_getfld(state, name, buf, sizeof(buf), in);
1565                 switch (state) {
1566                 case FLD:
1567                 case FLDPLUS:
1568                 case FLDEOF:
1569                         /* Search for the message ID */
1570                         if (mh_strcasecmp(name, "Message-ID")) {
1571                                 while (state == FLDPLUS)
1572                                         state = m_getfld(state, name, buf,
1573                                                         sizeof(buf), in);
1574                                 continue;
1575                         }
1576
1577                         cp = getcpy(buf);
1578                         while (state == FLDPLUS) {
1579                                 state = m_getfld(state, name, buf,
1580                                                 sizeof(buf), in);
1581                                 cp = add(buf, cp);
1582                         }
1583                         key.dptr = trimcpy(cp);
1584                         key.dsize = strlen(key.dptr) + 1;
1585                         free(cp);
1586                         cp = key.dptr;
1587
1588                         if (!(db = dbm_open(file, O_RDWR | O_CREAT, 0600))) {
1589                                 advise(file, "unable to perform dbm_open on");
1590                                 free(cp);
1591                                 fclose(in);
1592                                 return -1;
1593                         }
1594                         /*
1595                         ** Since it is difficult to portable lock a ndbm
1596                         ** file, we will open and lock the Maildelivery
1597                         ** file instead.  This will fail if your Maildelivery
1598                         ** file doesn't exist.
1599                         */
1600                         if ((lockfd = lkopen(file, O_RDWR, 0)) == -1) {
1601                                 advise(file, "unable to perform file locking on");
1602                                 free(cp);
1603                                 fclose(in);
1604                                 return -1;
1605                         }
1606                         value = dbm_fetch(db, key);
1607                         if (value.dptr) {
1608                                 if (verbose)
1609                                         verbose_printf("Message-ID: %s\n            already received on %s", cp, value.dptr);
1610                                 result = DONE;
1611                         } else {
1612                                 value.dptr  = ddate + sizeof("Delivery-Date:");
1613                                 value.dsize = strlen(value.dptr) + 1;
1614                                 if (dbm_store(db, key, value, DBM_INSERT))
1615                                         advise(file, "possibly corrupt file");
1616                                 result = 0;
1617                         }
1618
1619                         dbm_close(db);
1620                         lkclose(lockfd, file);
1621                         free(cp);
1622                         fclose(in);
1623                         return result;
1624                         break;
1625
1626                 case BODY:
1627                 case BODYEOF:
1628                 case FILEEOF:
1629                         break;
1630
1631                 case LENERR:
1632                 case FMTERR:
1633                 default:
1634                         break;
1635                 }
1636
1637                 break;
1638         }
1639
1640         fclose(in);
1641         return 0;
1642 }