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