3 * slocal.c -- asynchronously filter and deliver new mail
5 * This code is Copyright (c) 2002, by the authors of nmh. See the
6 * COPYRIGHT file in the root directory of the nmh distribution for
7 * complete copyright information.
11 * Under sendmail, users should add the line
13 * "| /usr/local/nmh/lib/slocal"
15 * to their $HOME/.forward file.
19 /* Changed to use getutent() and friends. Assumes that when getutent() exists,
20 * a number of other things also exist. Please check.
21 * Ruud de Rooij <ruud@ruud.org> Sun, 28 May 2000 17:28:55 +0200
25 #include <h/dropsbr.h>
26 #include <h/rcvmail.h>
27 #include <h/signals.h>
28 #include <h/m_setjmp.h>
35 #include <sys/ioctl.h>
38 /* Hopefully, grp.h declares initgroups(). If we run into a platform
39 where it doesn't, we could consider declaring it here as well. */
42 /* This define is needed for Berkeley db v2 and above to
43 * make the header file expose the 'historical' ndbm APIs.
44 * We define it unconditionally because this is simple and
47 #define DB_DBM_HSEARCH 1
49 #endif /* Use DB_DBM_HSEARCH to prevent warning from gcc -Wunused-macros. */
56 static struct swit switches[] = {
58 { "addr address", 0 },
64 { "sender address", 0 },
66 { "mailbox file", 0 },
68 { "home directory", -4 },
72 { "maildelivery file", 0 },
77 #define SUPPRESSDUP 10
79 #define NSUPPRESSDUP 11
80 { "nosuppressdup", 0 },
90 static int globbed = 0; /* have we built "vars" table yet? */
91 static int parsed = 0; /* have we built header field table yet */
92 static int utmped = 0; /* have we scanned umtp(x) file yet */
93 static int suppressdup = 0; /* are we suppressing duplicate messages? */
95 static int verbose = 0;
98 static char *addr = NULL;
99 static char *user = NULL;
100 static char *info = NULL;
101 static char *file = NULL;
102 static char *sender = NULL;
103 static char *envelope = NULL; /* envelope information ("From " line) */
104 static char *mbox = NULL;
105 static char *home = NULL;
107 static struct passwd *pw; /* passwd file entry */
109 static char ddate[BUFSIZ]; /* record the delivery date */
112 static jmp_buf myctx;
114 /* flags for pair->p_flags */
116 #define P_ADR 0x01 /* field is address */
117 #define P_HID 0x02 /* special (fake) field */
129 * Lookup table for matching fields and patterns
130 * in messages. The rest of the table is added
131 * when the message is parsed.
133 static struct pair hdrs[NVEC + 1] = {
134 { "source", NULL, P_HID },
135 { "addr", NULL, P_HID },
136 { "Return-Path", NULL, P_ADR },
137 { "Reply-To", NULL, P_ADR },
138 { "From", NULL, P_ADR },
139 { "Sender", NULL, P_ADR },
140 { "To", NULL, P_ADR },
141 { "cc", NULL, P_ADR },
142 { "Resent-Reply-To", NULL, P_ADR },
143 { "Resent-From", NULL, P_ADR },
144 { "Resent-Sender", NULL, P_ADR },
145 { "Resent-To", NULL, P_ADR },
146 { "Resent-cc", NULL, P_ADR },
151 * The list of builtin variables to expand in a string
152 * before it is executed by the "pipe" or "qpipe" action.
154 static struct pair vars[] = {
155 { "sender", NULL, P_NIL },
156 { "address", NULL, P_NIL },
157 { "size", NULL, P_NIL },
158 { "reply-to", NULL, P_CHK },
159 { "info", NULL, P_NIL },
163 extern char **environ;
168 static int localmail (int, char *);
169 static int usr_delivery (int, char *, int);
170 static int split (char *, char **);
171 static int parse (int);
172 static void expand (char *, char *, int);
173 static void glob (int);
174 static struct pair *lookup (struct pair *, char *);
175 static int logged_in (void);
176 static int timely (char *, char *);
177 static int usr_file (int, char *, int);
178 static int usr_pipe (int, char *, char *, char **, int);
179 static int usr_folder (int, char *);
180 static void alrmser (int);
181 static void get_sender (char *, char **);
182 static int copy_message (int, char *, int);
183 static void verbose_printf (char *fmt, ...);
184 static void adorn (char *, char *, ...);
185 static void debug_printf (char *fmt, ...);
186 static int suppress_duplicates (int, char *);
187 static char *trim (char *);
191 main (int argc, char **argv)
195 char *cp, *mdlvr = NULL, buf[BUFSIZ];
196 char mailbox[BUFSIZ], tmpfil[BUFSIZ];
197 char **argp, **arguments;
200 setlocale(LC_ALL, "");
202 invo_name = r1bindex (*argv, '/');
204 /* foil search of user profile/context */
205 if (context_foil (NULL) == -1)
208 mts_init (invo_name);
209 arguments = getarguments (invo_name, argc, argv, 0);
212 /* Parse arguments */
213 while ((cp = *argp++)) {
215 switch (smatch (++cp, switches)) {
217 ambigsw (cp, switches);
220 adios (NULL, "-%s unknown", cp);
223 snprintf (buf, sizeof(buf),
224 "%s [switches] [address info sender]", invo_name);
225 print_help (buf, switches, 0);
228 print_version(invo_name);
232 if (!(addr = *argp++))/* allow -xyz arguments */
233 adios (NULL, "missing argument to %s", argp[-2]);
236 if (!(info = *argp++))/* allow -xyz arguments */
237 adios (NULL, "missing argument to %s", argp[-2]);
240 if (!(user = *argp++))/* allow -xyz arguments */
241 adios (NULL, "missing argument to %s", argp[-2]);
244 if (!(file = *argp++) || *file == '-')
245 adios (NULL, "missing argument to %s", argp[-2]);
248 if (!(sender = *argp++))/* allow -xyz arguments */
249 adios (NULL, "missing argument to %s", argp[-2]);
252 if (!(mbox = *argp++) || *mbox == '-')
253 adios (NULL, "missing argument to %s", argp[-2]);
256 if (!(home = *argp++) || *home == '-')
257 adios (NULL, "missing argument to %s", argp[-2]);
261 if (!(cp = *argp++) || *cp == '-')
262 adios (NULL, "missing argument to %s", argp[-2]);
264 adios (NULL, "only one maildelivery file at a time!");
287 switch (argp - (argv + 1)) {
303 addr = getusername ();
305 user = (cp = strchr(addr, '.')) ? ++cp : addr;
306 if ((pw = getpwnam (user)) == NULL)
307 adios (NULL, "no such local user as %s", user);
309 if (chdir (pw->pw_dir) == -1)
313 if (geteuid() == 0) {
315 initgroups (pw->pw_name, pw->pw_gid);
322 setbuf (stdin, NULL);
324 /* Record the delivery time */
325 if ((now = dlocaltimenow ()) == NULL)
326 adios (NULL, "unable to ascertain local time");
327 snprintf (ddate, sizeof(ddate), "Delivery-Date: %s\n", dtimenow (0));
330 * Copy the message to a temporary file
335 /* getting message from file */
336 if ((tempfd = open (file, O_RDONLY)) == -1)
337 adios (file, "unable to open");
339 debug_printf ("retrieving message from file \"%s\"\n", file);
340 if ((fd = copy_message (tempfd, tmpfil, 1)) == -1)
341 adios (NULL, "unable to create temporary file");
344 /* getting message from stdin */
346 debug_printf ("retrieving message from stdin\n");
347 if ((fd = copy_message (fileno (stdin), tmpfil, 1)) == -1)
348 adios (NULL, "unable to create temporary file");
352 debug_printf ("temporary file=\"%s\"\n", tmpfil);
354 /* Delete the temp file now or a copy of every single message passed through
355 slocal will be left in the /tmp directory until deleted manually! This
356 unlink() used to be under an 'else' of the 'if (debug)' above, but since
357 some people like to always run slocal with -debug and log the results,
358 the /tmp directory would get choked over time. Of course, now that we
359 always delete the temp file, the "temporary file=" message above is
360 somewhat pointless -- someone watching debug output wouldn't have a
361 chance to 'tail -f' or 'ln' the temp file before it's unlinked. The best
362 thing would be to delay this unlink() until later if debug == 1, but I'll
363 leave that for someone who cares about the temp-file-accessing
364 functionality (they'll have to watch out for cases where we adios()). */
367 if (!(fp = fdopen (fd, "r+")))
368 adios (NULL, "unable to access temporary file");
371 * If no sender given, extract it
372 * from envelope information. */
374 get_sender (envelope, &sender);
377 snprintf (mailbox, sizeof(mailbox), "%s/%s",
378 mmdfldir[0] ? mmdfldir : pw->pw_dir,
379 mmdflfil[0] ? mmdflfil : pw->pw_name);
386 debug_printf ("addr=\"%s\"\n", trim(addr));
387 debug_printf ("user=\"%s\"\n", trim(user));
388 debug_printf ("info=\"%s\"\n", trim(info));
389 debug_printf ("sender=\"%s\"\n", trim(sender));
390 debug_printf ("envelope=\"%s\"\n", envelope ? trim(envelope) : "");
391 debug_printf ("mbox=\"%s\"\n", trim(mbox));
392 debug_printf ("home=\"%s\"\n", trim(home));
393 debug_printf ("ddate=\"%s\"\n", trim(ddate));
394 debug_printf ("now=%02d:%02d\n\n", now->tw_hour, now->tw_min);
397 /* deliver the message */
398 status = localmail (fd, mdlvr);
400 done (status != -1 ? RCV_MOK : RCV_MBX);
406 * Main routine for delivering message.
410 localmail (int fd, char *mdlvr)
412 /* check if this message is a duplicate */
414 suppress_duplicates(fd, mdlvr ? mdlvr : ".maildelivery") == DONE)
417 /* delivery according to personal Maildelivery file */
418 if (usr_delivery (fd, mdlvr ? mdlvr : ".maildelivery", 0) != -1)
421 /* delivery according to global Maildelivery file */
422 if (usr_delivery (fd, maildelivery, 1) != -1)
426 verbose_printf ("(delivering to standard mail spool)\n");
428 /* last resort - deliver to standard mail spool */
429 return usr_file (fd, mbox, MBOX_FORMAT);
433 #define matches(a,b) (stringdex (b, a) >= 0)
436 * Parse the delivery file, and process incoming message.
440 usr_delivery (int fd, char *delivery, int su)
442 int i, accept, status=1, won, vecp, next;
443 char *field, *pattern, *action, *result, *string;
444 char buffer[BUFSIZ], tmpbuf[BUFSIZ];
445 char *cp, *vec[NVEC];
450 /* open the delivery file */
451 if ((fp = fopen (delivery, "r")) == NULL)
454 /* check if delivery file has bad ownership or permissions */
455 if (fstat (fileno (fp), &st) == -1
456 || (st.st_uid != 0 && (su || st.st_uid != pw->pw_uid))
457 || st.st_mode & (S_IWGRP|S_IWOTH)) {
459 verbose_printf ("WARNING: %s has bad ownership/modes (su=%d,uid=%d,owner=%d,mode=0%o)\n",
460 delivery, su, (int) pw->pw_uid, (int) st.st_uid, (int) st.st_mode);
468 /* read and process delivery file */
469 while (fgets (buffer, sizeof(buffer), fp)) {
470 /* skip comments and empty lines */
471 if (*buffer == '#' || *buffer == '\n')
474 /* zap trailing newline */
475 if ((cp = strchr(buffer, '\n')))
478 /* split buffer into fields */
479 vecp = split (buffer, vec);
481 /* check for too few fields */
484 debug_printf ("WARNING: entry with only %d fields, skipping.\n", vecp);
489 for (i = 0; vec[i]; i++)
490 debug_printf ("vec[%d]: \"%s\"\n", i, trim(vec[i]));
499 /* find out how to perform the action */
504 * If previous condition failed, don't
505 * do this - else fall through
508 continue; /* else fall */
512 * If already delivered, skip this action. Else
513 * consider delivered if action is successful.
516 continue; /* else fall */
521 * Take action, and consider delivered if
522 * action is successful.
531 * Take action, but don't consider delivered, even
532 * if action is successful
539 if (!mh_strcasecmp (vec[5], "select")) {
540 if (logged_in () != -1)
542 if (vecp > 7 && timely (vec[6], vec[7]) == -1)
547 /* check if the field matches */
555 * "default" matches only if the message hasn't
556 * been delivered yet.
558 if (!mh_strcasecmp (field, "default")) {
565 /* parse message and build lookup table */
566 if (!parsed && parse (fd) == -1) {
571 * find header field in lookup table, and
572 * see if the pattern matches.
574 if ((p = lookup (hdrs, field)) && (p->p_value != NULL)
575 && matches (p->p_value, pattern)) {
584 /* find out the action to perform */
587 /* deliver to quoted pipe */
588 if (mh_strcasecmp (action, "qpipe"))
589 continue; /* else fall */
591 expand (tmpbuf, string, fd);
592 if (split (tmpbuf, vec) < 1)
594 status = usr_pipe (fd, tmpbuf, vec[0], vec, 0);
598 /* deliver to pipe */
599 if (mh_strcasecmp (action, "pipe"))
600 continue; /* else fall */
604 expand (tmpbuf, string, fd);
607 status = usr_pipe (fd, tmpbuf, "/bin/sh", vec + 2, 0);
612 if (!mh_strcasecmp (action, "file")) {
613 status = usr_file (fd, string, MBOX_FORMAT);
616 /* deliver to nmh folder */
617 else if (mh_strcasecmp (action, "folder"))
618 continue; /* else fall */
620 status = usr_folder (fd, string);
625 if (!mh_strcasecmp (action, "mmdf")) {
626 status = usr_file (fd, string, MMDF_FORMAT);
630 else if (mh_strcasecmp (action, "mbox"))
631 continue; /* else fall */
635 status = usr_file (fd, string, MBOX_FORMAT);
640 if (mh_strcasecmp (action, "destroy"))
646 if (status) next = 0; /* action failed, mark for 'N' result */
648 if (accept && status == 0)
653 return (won ? 0 : -1);
660 * Split buffer into fields (delimited by whitespace or
661 * comma's). Return the number of fields found.
665 split (char *cp, char **vec)
672 /* split into a maximum of NVEC fields */
673 for (i = 0; i <= NVEC;) {
676 /* zap any whitespace and comma's */
677 while (isspace (*s) || *s == ',')
680 /* end of buffer, time to leave */
684 /* get double quote text as a single field */
686 for (vec[i++] = ++s; *s && *s != '"'; s++) {
688 * Check for escaped double quote. We need
689 * to shift the string to remove slash.
697 if (*s == '"') /* zap trailing double quote */
702 if (*s == QUOTE && *++s != '"')
706 /* move forward to next field delimiter */
707 while (*s && !isspace (*s) && *s != ',')
717 * Parse the headers of a message, and build the
718 * lookup table for matching fields and patterns.
727 char name[NAMESZ], field[BUFSIZ];
734 /* get a new FILE pointer to message */
735 if ((fd1 = dup (fd)) == -1)
737 if ((in = fdopen (fd1, "r")) == NULL) {
743 /* add special entries to lookup table */
744 if ((p = lookup (hdrs, "source")))
745 p->p_value = getcpy (sender);
746 if ((p = lookup (hdrs, "addr")))
747 p->p_value = getcpy (addr);
750 * Scan the headers of the message and build
753 for (i = 0, state = FLD;;) {
754 switch (state = m_getfld (state, name, field, sizeof(field), in)) {
758 lp = add (field, NULL);
759 while (state == FLDPLUS) {
760 state = m_getfld (state, name, field, sizeof(field), in);
761 lp = add (field, lp);
763 for (p = hdrs; p->p_name; p++) {
764 if (!mh_strcasecmp (p->p_name, name)) {
765 if (!(p->p_flags & P_HID)) {
766 if ((cp = p->p_value)) {
767 if (p->p_flags & P_ADR) {
768 dp = cp + strlen (cp) - 1;
771 cp = add (",\n\t", cp);
776 p->p_value = add (lp, cp);
782 if (p->p_name == NULL && i < NVEC) {
783 p->p_name = getcpy (name);
800 advise (NULL, "format error in message");
804 advise (NULL, "internal error in m_getfld");
812 if ((p = lookup (vars, "reply-to"))) {
813 if ((q = lookup (hdrs, "reply-to")) == NULL || q->p_value == NULL)
814 q = lookup (hdrs, "from");
815 p->p_value = getcpy (q ? q->p_value : "");
816 p->p_flags &= ~P_CHK;
818 debug_printf ("vars[%d]: name=\"%s\" value=\"%s\"\n",
819 p - vars, p->p_name, trim(p->p_value));
822 for (p = hdrs; p->p_name; p++)
823 debug_printf ("hdrs[%d]: name=\"%s\" value=\"%s\"\n",
824 p - hdrs, p->p_name, p->p_value ? trim(p->p_value) : "");
835 * Expand any builtin variables such as $(sender),
836 * $(address), etc., in a string.
840 expand (char *s1, char *s2, int fd)
848 while ((c = *s2++)) {
849 if (c != '$' || *s2 != LPAREN) {
852 for (cp = ++s2; *s2 && *s2 != RPAREN; s2++)
859 if ((p = lookup (vars, cp))) {
860 if (!parsed && (p->p_flags & P_CHK))
863 strcpy (s1, p->p_value);
873 * Fill in the information missing from the "vars"
874 * table, which is necessary to expand any builtin
875 * variables in the string for a "pipe" or "qpipe"
889 if ((p = lookup (vars, "sender")))
890 p->p_value = getcpy (sender);
891 if ((p = lookup (vars, "address")))
892 p->p_value = getcpy (addr);
893 if ((p = lookup (vars, "size"))) {
894 snprintf (buffer, sizeof(buffer), "%d",
895 fstat (fd, &st) != -1 ? (int) st.st_size : 0);
896 p->p_value = getcpy (buffer);
898 if ((p = lookup (vars, "info")))
899 p->p_value = getcpy (info);
902 for (p = vars; p->p_name; p++)
903 debug_printf ("vars[%d]: name=\"%s\" value=\"%s\"\n",
904 p - vars, p->p_name, trim(p->p_value));
910 * Find a matching name in a lookup table. If found,
911 * return the "pairs" entry, else return NULL.
915 lookup (struct pair *pairs, char *key)
917 for (; pairs->p_name; pairs++)
918 if (!mh_strcasecmp (pairs->p_name, key))
926 * Check utmp(x) file to see if user is currently
940 while ((utp = getutxent()) != NULL) {
941 if ( utp->ut_type == USER_PROCESS && utp->ut_user[0] != 0
942 && strncmp (user, utp->ut_user, sizeof(utp->ut_user)) == 0) {
946 return (utmped = DONE);
951 return (utmped = NOTOK);
954 #define check(t,a,b) if (t < a || t > b) return -1
955 #define cmpar(h1,m1,h2,m2) if (h1 < h2 || (h1 == h2 && m1 < m2)) return 0
958 timely (char *t1, char *t2)
960 int t1hours, t1mins, t2hours, t2mins;
962 if (sscanf (t1, "%d:%d", &t1hours, &t1mins) != 2)
964 check (t1hours, 0, 23);
965 check (t1mins, 0, 59);
967 if (sscanf (t2, "%d:%d", &t2hours, &t2mins) != 2)
969 check (t2hours, 0, 23);
970 check (t2mins, 0, 59);
972 cmpar (now->tw_hour, now->tw_min, t1hours, t1mins);
973 cmpar (t2hours, t2mins, now->tw_hour, now->tw_min);
980 * Deliver message by appending to a file.
984 usr_file (int fd, char *mailbox, int mbx_style)
989 verbose_printf ("delivering to file \"%s\"", mailbox);
991 if (mbx_style == MBOX_FORMAT) {
993 verbose_printf (" (mbox style)");
997 verbose_printf (" (mmdf style)");
1001 /* open and lock the file */
1002 if ((md = mbx_open (mailbox, mbx_style, pw->pw_uid, pw->pw_gid, m_gmprot())) == -1) {
1004 adorn ("", "unable to open:");
1008 lseek (fd, (off_t) 0, SEEK_SET);
1010 /* append message to file */
1011 if (mbx_copy (mailbox, mbx_style, md, fd, mapping, NULL, verbose) == -1) {
1013 adorn ("", "error writing to:");
1017 /* close and unlock file */
1018 if (mbx_close (mailbox, md) == NOTOK) {
1020 adorn ("", "error closing:");
1025 verbose_printf (", success.\n");
1031 * Deliver message to a nmh folder.
1035 usr_folder (int fd, char *string)
1038 char folder[BUFSIZ], *vec[3];
1040 /* get folder name ready */
1042 strncpy(folder, string, sizeof(folder));
1044 snprintf(folder, sizeof(folder), "+%s", string);
1047 verbose_printf ("delivering to folder \"%s\"", folder + 1);
1049 vec[0] = "rcvstore";
1053 /* use rcvstore to put message in folder */
1054 status = usr_pipe (fd, "rcvstore", rcvstoreproc, vec, 1);
1058 * Currently, verbose status messages are handled by usr_pipe().
1062 verbose_printf (", success.\n");
1064 verbose_printf (", failed.\n");
1072 * Deliver message to a process.
1076 usr_pipe (int fd, char *cmd, char *pgm, char **vec, int suppress)
1079 int i, bytes, seconds, status;
1082 if (verbose && !suppress)
1083 verbose_printf ("delivering to pipe \"%s\"", cmd);
1085 lseek (fd, (off_t) 0, SEEK_SET);
1087 for (i = 0; (child_id = fork()) == -1 && i < 5; i++)
1094 adorn ("fork", "unable to");
1101 freopen ("/dev/null", "w", stdout);
1102 freopen ("/dev/null", "w", stderr);
1108 if ((fd = open ("/dev/tty", O_RDWR)) != -1) {
1109 ioctl (fd, TIOCNOTTY, NULL);
1112 #endif /* TIOCNOTTY */
1114 setpgid ((pid_t) 0, getpid ()); /* put in own process group */
1117 m_putenv ("USER", pw->pw_name);
1118 m_putenv ("HOME", pw->pw_dir);
1119 m_putenv ("SHELL", pw->pw_shell);
1125 /* parent process */
1126 if (! m_setjmp (myctx)) {
1127 SIGNAL (SIGALRM, alrmser);
1128 bytes = fstat (fd, &st) != -1 ? (int) st.st_size : 100;
1130 /* amount of time to wait depends on message size */
1132 /* give at least 5 minutes */
1134 } else if (bytes >= 90000) {
1135 /* a half hour is long enough */
1138 seconds = (bytes / 60) + 300;
1140 alarm ((unsigned int) seconds);
1141 status = pidwait (child_id, 0);
1146 verbose_printf (", success.\n");
1148 if ((status & 0xff00) == 0xff00)
1149 verbose_printf (", system error\n");
1151 pidstatus (status, stdout, ", failed");
1153 return (status == 0 ? 0 : -1);
1156 * Ruthlessly kill the child and anything
1157 * else in its process group.
1159 killpg(child_id, SIGKILL);
1161 verbose_printf (", timed-out; terminated\n");
1173 longjmp (myctx, DONE);
1178 * Get the `sender' from the envelope
1179 * information ("From " line).
1183 get_sender (char *envelope, char **sender)
1187 unsigned char buffer[BUFSIZ];
1189 if (envelope == NULL) {
1190 *sender = getcpy ("");
1194 i = strlen ("From ");
1195 strncpy (buffer, envelope + i, sizeof(buffer));
1196 if ((cp = strchr(buffer, '\n'))) {
1206 for (cp = buffer + strlen (buffer) - 1; cp >= buffer; cp--)
1211 *sender = getcpy (buffer);
1216 * Copy message into a temporary file.
1217 * While copying, it will do some header processing
1218 * including the extraction of the envelope information.
1222 copy_message (int qd, char *tmpfil, int fold)
1224 int i, first = 1, fd1, fd2;
1225 char buffer[BUFSIZ];
1229 tfile = m_mktemp2(NULL, invo_name, &fd1, NULL);
1230 if (tfile == NULL) return -1;
1232 strncpy (tmpfil, tfile, BUFSIZ);
1235 while ((i = read (qd, buffer, sizeof(buffer))) > 0)
1236 if (write (fd1, buffer, i) != i) {
1244 lseek (fd1, (off_t) 0, SEEK_SET);
1248 /* dup the fd for incoming message */
1249 if ((fd2 = dup (qd)) == -1) {
1254 /* now create a FILE pointer for it */
1255 if ((qfp = fdopen (fd2, "r")) == NULL) {
1261 /* dup the fd for temporary file */
1262 if ((fd2 = dup (fd1)) == -1) {
1268 /* now create a FILE pointer for it */
1269 if ((ffp = fdopen (fd2, "r+")) == NULL) {
1277 * copy message into temporary file
1278 * and massage the headers. Save
1279 * a copy of the "From " line for later.
1281 i = strlen ("From ");
1282 while (fgets (buffer, sizeof(buffer), qfp)) {
1285 if (!strncmp (buffer, "From ", i)) {
1286 /* get copy of envelope information ("From " line) */
1287 envelope = getcpy (buffer);
1290 /* First go ahead and put "From " line in message */
1291 fputs (buffer, ffp);
1296 /* Put the delivery date in message */
1305 fputs (buffer, ffp);
1317 lseek (fd1, (off_t) 0, SEEK_SET);
1329 * Trim strings for pretty printing of debugging output
1335 char buffer[BUFSIZ*4];
1336 unsigned char *bp, *sp;
1341 /* copy string into temp buffer */
1342 strncpy (buffer, cp, sizeof(buffer));
1345 /* skip over leading whitespace */
1346 while (isspace(*bp))
1349 /* start at the end and zap trailing whitespace */
1350 for (sp = bp + strlen(bp) - 1; sp >= bp; sp--) {
1357 /* replace remaining whitespace with spaces */
1358 for (sp = bp; *sp; sp++)
1362 /* now return a copy */
1367 * Function for printing `verbose' messages.
1371 verbose_printf (char *fmt, ...)
1376 vfprintf (stdout, fmt, ap);
1379 fflush (stdout); /* now flush output */
1384 * Function for printing `verbose' delivery
1389 adorn (char *what, char *fmt, ...)
1395 eindex = errno; /* save the errno */
1396 fprintf (stdout, ", ");
1399 vfprintf (stdout, fmt, ap);
1404 fprintf (stdout, " %s: ", what);
1405 if ((s = strerror (eindex)))
1406 fprintf (stdout, "%s", s);
1408 fprintf (stdout, "Error %d", eindex);
1411 fputc ('\n', stdout);
1417 * Function for printing `debug' messages.
1421 debug_printf (char *fmt, ...)
1426 vfprintf (stderr, fmt, ap);
1432 * Check ndbm/db file(s) to see if the Message-Id of this
1433 * message matches the Message-Id of a previous message,
1434 * so we can discard it. If it doesn't match, we add the
1435 * Message-Id of this message to the ndbm/db file.
1438 suppress_duplicates (int fd, char *file)
1440 int fd1, lockfd, state, result;
1441 char *cp, buf[BUFSIZ], name[NAMESZ];
1446 if ((fd1 = dup (fd)) == -1)
1448 if (!(in = fdopen (fd1, "r"))) {
1454 for (state = FLD;;) {
1455 state = m_getfld (state, name, buf, sizeof(buf), in);
1460 /* Search for the message ID */
1461 if (mh_strcasecmp (name, "Message-ID")) {
1462 while (state == FLDPLUS)
1463 state = m_getfld (state, name, buf, sizeof(buf), in);
1467 cp = add (buf, NULL);
1468 while (state == FLDPLUS) {
1469 state = m_getfld (state, name, buf, sizeof(buf), in);
1472 key.dptr = trimcpy (cp);
1473 key.dsize = strlen (key.dptr) + 1;
1477 if (!(db = dbm_open (file, O_RDWR | O_CREAT, 0600))) {
1478 advise (file, "unable to perform dbm_open on");
1484 * Since it is difficult to portable lock a ndbm file,
1485 * we will open and lock the Maildelivery file instead.
1486 * This will fail if your Maildelivery file doesn't
1489 if ((lockfd = lkopen(file, O_RDWR, 0)) == -1) {
1490 advise (file, "unable to perform file locking on");
1495 value = dbm_fetch (db, key);
1498 verbose_printf ("Message-ID: %s\n already received on %s",
1502 value.dptr = ddate + sizeof("Delivery-Date:");
1503 value.dsize = strlen(value.dptr) + 1;
1504 if (dbm_store (db, key, value, DBM_INSERT))
1505 advise (file, "possibly corrupt file");
1510 lkclose(lockfd, file);