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>
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 #endif /* HAVE_GETUTXENT */
58 static struct swit switches[] = {
60 { "addr address", 0 },
66 { "sender address", 0 },
68 { "mailbox file", 0 },
70 { "home directory", -4 },
74 { "maildelivery file", 0 },
79 #define SUPPRESSDUP 10
81 #define NSUPPRESSDUP 11
82 { "nosuppressdup", 0 },
92 static int globbed = 0; /* have we built "vars" table yet? */
93 static int parsed = 0; /* have we built header field table yet */
94 static int utmped = 0; /* have we scanned umtp(x) file yet */
95 static int suppressdup = 0; /* are we suppressing duplicate messages? */
97 static int verbose = 0;
100 static char *addr = NULL;
101 static char *user = NULL;
102 static char *info = NULL;
103 static char *file = NULL;
104 static char *sender = NULL;
105 static char *envelope = NULL; /* envelope information ("From " line) */
106 static char *mbox = NULL;
107 static char *home = NULL;
109 static struct passwd *pw; /* passwd file entry */
111 static char ddate[BUFSIZ]; /* record the delivery date */
114 static jmp_buf myctx;
116 /* flags for pair->p_flags */
118 #define P_ADR 0x01 /* field is address */
119 #define P_HID 0x02 /* special (fake) field */
131 * Lookup table for matching fields and patterns
132 * in messages. The rest of the table is added
133 * when the message is parsed.
135 static struct pair hdrs[NVEC + 1] = {
136 { "source", NULL, P_HID },
137 { "addr", NULL, P_HID },
138 { "Return-Path", NULL, P_ADR },
139 { "Reply-To", NULL, P_ADR },
140 { "From", NULL, P_ADR },
141 { "Sender", NULL, P_ADR },
142 { "To", NULL, P_ADR },
143 { "cc", NULL, P_ADR },
144 { "Resent-Reply-To", NULL, P_ADR },
145 { "Resent-From", NULL, P_ADR },
146 { "Resent-Sender", NULL, P_ADR },
147 { "Resent-To", NULL, P_ADR },
148 { "Resent-cc", NULL, P_ADR },
153 * The list of builtin variables to expand in a string
154 * before it is executed by the "pipe" or "qpipe" action.
156 static struct pair vars[] = {
157 { "sender", NULL, P_NIL },
158 { "address", NULL, P_NIL },
159 { "size", NULL, P_NIL },
160 { "reply-to", NULL, P_CHK },
161 { "info", NULL, P_NIL },
165 extern char **environ;
170 static int localmail (int, char *);
171 static int usr_delivery (int, char *, int);
172 static int split (char *, char **);
173 static int parse (int);
174 static void expand (char *, char *, int);
175 static void glob (int);
176 static struct pair *lookup (struct pair *, char *);
177 static int logged_in (void);
178 static int timely (char *, char *);
179 static int usr_file (int, char *, int);
180 static int usr_pipe (int, char *, char *, char **, int);
181 static int usr_folder (int, char *);
182 static void alrmser (int);
183 static void get_sender (char *, char **);
184 static int copy_message (int, char *, int);
185 static void verbose_printf (char *fmt, ...);
186 static void adorn (char *, char *, ...);
187 static void debug_printf (char *fmt, ...);
188 static int suppress_duplicates (int, char *);
189 static char *trim (char *);
193 main (int argc, char **argv)
197 char *cp, *mdlvr = NULL, buf[BUFSIZ];
198 char mailbox[BUFSIZ], tmpfil[BUFSIZ];
199 char **argp, **arguments;
202 setlocale(LC_ALL, "");
204 invo_name = r1bindex (*argv, '/');
206 /* foil search of user profile/context */
207 if (context_foil (NULL) == -1)
210 mts_init (invo_name);
211 arguments = getarguments (invo_name, argc, argv, 0);
214 /* Parse arguments */
215 while ((cp = *argp++)) {
217 switch (smatch (++cp, switches)) {
219 ambigsw (cp, switches);
222 adios (NULL, "-%s unknown", cp);
225 snprintf (buf, sizeof(buf),
226 "%s [switches] [address info sender]", invo_name);
227 print_help (buf, switches, 0);
230 print_version(invo_name);
234 if (!(addr = *argp++))/* allow -xyz arguments */
235 adios (NULL, "missing argument to %s", argp[-2]);
238 if (!(info = *argp++))/* allow -xyz arguments */
239 adios (NULL, "missing argument to %s", argp[-2]);
242 if (!(user = *argp++))/* allow -xyz arguments */
243 adios (NULL, "missing argument to %s", argp[-2]);
246 if (!(file = *argp++) || *file == '-')
247 adios (NULL, "missing argument to %s", argp[-2]);
250 if (!(sender = *argp++))/* allow -xyz arguments */
251 adios (NULL, "missing argument to %s", argp[-2]);
254 if (!(mbox = *argp++) || *mbox == '-')
255 adios (NULL, "missing argument to %s", argp[-2]);
258 if (!(home = *argp++) || *home == '-')
259 adios (NULL, "missing argument to %s", argp[-2]);
263 if (!(cp = *argp++) || *cp == '-')
264 adios (NULL, "missing argument to %s", argp[-2]);
266 adios (NULL, "only one maildelivery file at a time!");
289 switch (argp - (argv + 1)) {
305 addr = getusername ();
307 user = (cp = strchr(addr, '.')) ? ++cp : addr;
308 if ((pw = getpwnam (user)) == NULL)
309 adios (NULL, "no such local user as %s", user);
311 if (chdir (pw->pw_dir) == -1)
315 if (geteuid() == 0) {
317 initgroups (pw->pw_name, pw->pw_gid);
324 setbuf (stdin, NULL);
326 /* Record the delivery time */
327 if ((now = dlocaltimenow ()) == NULL)
328 adios (NULL, "unable to ascertain local time");
329 snprintf (ddate, sizeof(ddate), "Delivery-Date: %s\n", dtimenow (0));
332 * Copy the message to a temporary file
337 /* getting message from file */
338 if ((tempfd = open (file, O_RDONLY)) == -1)
339 adios (file, "unable to open");
341 debug_printf ("retrieving message from file \"%s\"\n", file);
342 if ((fd = copy_message (tempfd, tmpfil, 1)) == -1)
343 adios (NULL, "unable to create temporary file");
346 /* getting message from stdin */
348 debug_printf ("retrieving message from stdin\n");
349 if ((fd = copy_message (fileno (stdin), tmpfil, 1)) == -1)
350 adios (NULL, "unable to create temporary file");
354 debug_printf ("temporary file=\"%s\"\n", tmpfil);
356 /* Delete the temp file now or a copy of every single message passed through
357 slocal will be left in the /tmp directory until deleted manually! This
358 unlink() used to be under an 'else' of the 'if (debug)' above, but since
359 some people like to always run slocal with -debug and log the results,
360 the /tmp directory would get choked over time. Of course, now that we
361 always delete the temp file, the "temporary file=" message above is
362 somewhat pointless -- someone watching debug output wouldn't have a
363 chance to 'tail -f' or 'ln' the temp file before it's unlinked. The best
364 thing would be to delay this unlink() until later if debug == 1, but I'll
365 leave that for someone who cares about the temp-file-accessing
366 functionality (they'll have to watch out for cases where we adios()). */
369 if (!(fp = fdopen (fd, "r+")))
370 adios (NULL, "unable to access temporary file");
373 * If no sender given, extract it
374 * from envelope information. */
376 get_sender (envelope, &sender);
379 snprintf (mailbox, sizeof(mailbox), "%s/%s",
380 mmdfldir[0] ? mmdfldir : pw->pw_dir,
381 mmdflfil[0] ? mmdflfil : pw->pw_name);
388 debug_printf ("addr=\"%s\"\n", trim(addr));
389 debug_printf ("user=\"%s\"\n", trim(user));
390 debug_printf ("info=\"%s\"\n", trim(info));
391 debug_printf ("sender=\"%s\"\n", trim(sender));
392 debug_printf ("envelope=\"%s\"\n", envelope ? trim(envelope) : "");
393 debug_printf ("mbox=\"%s\"\n", trim(mbox));
394 debug_printf ("home=\"%s\"\n", trim(home));
395 debug_printf ("ddate=\"%s\"\n", trim(ddate));
396 debug_printf ("now=%02d:%02d\n\n", now->tw_hour, now->tw_min);
399 /* deliver the message */
400 status = localmail (fd, mdlvr);
402 done (status != -1 ? RCV_MOK : RCV_MBX);
408 * Main routine for delivering message.
412 localmail (int fd, char *mdlvr)
414 /* check if this message is a duplicate */
416 suppress_duplicates(fd, mdlvr ? mdlvr : ".maildelivery") == DONE)
419 /* delivery according to personal Maildelivery file */
420 if (usr_delivery (fd, mdlvr ? mdlvr : ".maildelivery", 0) != -1)
423 /* delivery according to global Maildelivery file */
424 if (usr_delivery (fd, maildelivery, 1) != -1)
428 verbose_printf ("(delivering to standard mail spool)\n");
430 /* last resort - deliver to standard mail spool */
431 return usr_file (fd, mbox, MBOX_FORMAT);
435 #define matches(a,b) (stringdex (b, a) >= 0)
438 * Parse the delivery file, and process incoming message.
442 usr_delivery (int fd, char *delivery, int su)
444 int i, accept, status=1, won, vecp, next;
445 char *field, *pattern, *action, *result, *string;
446 char buffer[BUFSIZ], tmpbuf[BUFSIZ];
447 char *cp, *vec[NVEC];
452 /* open the delivery file */
453 if ((fp = fopen (delivery, "r")) == NULL)
456 /* check if delivery file has bad ownership or permissions */
457 if (fstat (fileno (fp), &st) == -1
458 || (st.st_uid != 0 && (su || st.st_uid != pw->pw_uid))
459 || st.st_mode & (S_IWGRP|S_IWOTH)) {
461 verbose_printf ("WARNING: %s has bad ownership/modes (su=%d,uid=%d,owner=%d,mode=0%o)\n",
462 delivery, su, (int) pw->pw_uid, (int) st.st_uid, (int) st.st_mode);
470 /* read and process delivery file */
471 while (fgets (buffer, sizeof(buffer), fp)) {
472 /* skip comments and empty lines */
473 if (*buffer == '#' || *buffer == '\n')
476 /* zap trailing newline */
477 if ((cp = strchr(buffer, '\n')))
480 /* split buffer into fields */
481 vecp = split (buffer, vec);
483 /* check for too few fields */
486 debug_printf ("WARNING: entry with only %d fields, skipping.\n", vecp);
491 for (i = 0; vec[i]; i++)
492 debug_printf ("vec[%d]: \"%s\"\n", i, trim(vec[i]));
501 /* find out how to perform the action */
506 * If previous condition failed, don't
507 * do this - else fall through
510 continue; /* else fall */
514 * If already delivered, skip this action. Else
515 * consider delivered if action is successful.
518 continue; /* else fall */
523 * Take action, and consider delivered if
524 * action is successful.
533 * Take action, but don't consider delivered, even
534 * if action is successful
541 if (!mh_strcasecmp (vec[5], "select")) {
542 if (logged_in () != -1)
544 if (vecp > 7 && timely (vec[6], vec[7]) == -1)
549 /* check if the field matches */
557 * "default" matches only if the message hasn't
558 * been delivered yet.
560 if (!mh_strcasecmp (field, "default")) {
567 /* parse message and build lookup table */
568 if (!parsed && parse (fd) == -1) {
573 * find header field in lookup table, and
574 * see if the pattern matches.
576 if ((p = lookup (hdrs, field)) && (p->p_value != NULL)
577 && matches (p->p_value, pattern)) {
586 /* find out the action to perform */
589 /* deliver to quoted pipe */
590 if (mh_strcasecmp (action, "qpipe"))
591 continue; /* else fall */
593 expand (tmpbuf, string, fd);
594 if (split (tmpbuf, vec) < 1)
596 status = usr_pipe (fd, tmpbuf, vec[0], vec, 0);
600 /* deliver to pipe */
601 if (mh_strcasecmp (action, "pipe"))
602 continue; /* else fall */
606 expand (tmpbuf, string, fd);
609 status = usr_pipe (fd, tmpbuf, "/bin/sh", vec + 2, 0);
614 if (!mh_strcasecmp (action, "file")) {
615 status = usr_file (fd, string, MBOX_FORMAT);
618 /* deliver to nmh folder */
619 else if (mh_strcasecmp (action, "folder"))
620 continue; /* else fall */
622 status = usr_folder (fd, string);
627 if (!mh_strcasecmp (action, "mmdf")) {
628 status = usr_file (fd, string, MMDF_FORMAT);
632 else if (mh_strcasecmp (action, "mbox"))
633 continue; /* else fall */
637 status = usr_file (fd, string, MBOX_FORMAT);
642 if (mh_strcasecmp (action, "destroy"))
648 if (status) next = 0; /* action failed, mark for 'N' result */
650 if (accept && status == 0)
655 return (won ? 0 : -1);
662 * Split buffer into fields (delimited by whitespace or
663 * comma's). Return the number of fields found.
667 split (char *cp, char **vec)
674 /* split into a maximum of NVEC fields */
675 for (i = 0; i <= NVEC;) {
678 /* zap any whitespace and comma's */
679 while (isspace (*s) || *s == ',')
682 /* end of buffer, time to leave */
686 /* get double quote text as a single field */
688 for (vec[i++] = ++s; *s && *s != '"'; s++) {
690 * Check for escaped double quote. We need
691 * to shift the string to remove slash.
699 if (*s == '"') /* zap trailing double quote */
704 if (*s == QUOTE && *++s != '"')
708 /* move forward to next field delimiter */
709 while (*s && !isspace (*s) && *s != ',')
719 * Parse the headers of a message, and build the
720 * lookup table for matching fields and patterns.
729 char name[NAMESZ], field[BUFSIZ];
736 /* get a new FILE pointer to message */
737 if ((fd1 = dup (fd)) == -1)
739 if ((in = fdopen (fd1, "r")) == NULL) {
745 /* add special entries to lookup table */
746 if ((p = lookup (hdrs, "source")))
747 p->p_value = getcpy (sender);
748 if ((p = lookup (hdrs, "addr")))
749 p->p_value = getcpy (addr);
752 * Scan the headers of the message and build
755 for (i = 0, state = FLD;;) {
756 switch (state = m_getfld (state, name, field, sizeof(field), in)) {
760 lp = add (field, NULL);
761 while (state == FLDPLUS) {
762 state = m_getfld (state, name, field, sizeof(field), in);
763 lp = add (field, lp);
765 for (p = hdrs; p->p_name; p++) {
766 if (!mh_strcasecmp (p->p_name, name)) {
767 if (!(p->p_flags & P_HID)) {
768 if ((cp = p->p_value)) {
769 if (p->p_flags & P_ADR) {
770 dp = cp + strlen (cp) - 1;
773 cp = add (",\n\t", cp);
778 p->p_value = add (lp, cp);
784 if (p->p_name == NULL && i < NVEC) {
785 p->p_name = getcpy (name);
802 advise (NULL, "format error in message");
806 advise (NULL, "internal error in m_getfld");
814 if ((p = lookup (vars, "reply-to"))) {
815 if ((q = lookup (hdrs, "reply-to")) == NULL || q->p_value == NULL)
816 q = lookup (hdrs, "from");
817 p->p_value = getcpy (q ? q->p_value : "");
818 p->p_flags &= ~P_CHK;
820 debug_printf ("vars[%d]: name=\"%s\" value=\"%s\"\n",
821 p - vars, p->p_name, trim(p->p_value));
824 for (p = hdrs; p->p_name; p++)
825 debug_printf ("hdrs[%d]: name=\"%s\" value=\"%s\"\n",
826 p - hdrs, p->p_name, p->p_value ? trim(p->p_value) : "");
837 * Expand any builtin variables such as $(sender),
838 * $(address), etc., in a string.
842 expand (char *s1, char *s2, int fd)
850 while ((c = *s2++)) {
851 if (c != '$' || *s2 != LPAREN) {
854 for (cp = ++s2; *s2 && *s2 != RPAREN; s2++)
861 if ((p = lookup (vars, cp))) {
862 if (!parsed && (p->p_flags & P_CHK))
865 strcpy (s1, p->p_value);
875 * Fill in the information missing from the "vars"
876 * table, which is necessary to expand any builtin
877 * variables in the string for a "pipe" or "qpipe"
891 if ((p = lookup (vars, "sender")))
892 p->p_value = getcpy (sender);
893 if ((p = lookup (vars, "address")))
894 p->p_value = getcpy (addr);
895 if ((p = lookup (vars, "size"))) {
896 snprintf (buffer, sizeof(buffer), "%d",
897 fstat (fd, &st) != -1 ? (int) st.st_size : 0);
898 p->p_value = getcpy (buffer);
900 if ((p = lookup (vars, "info")))
901 p->p_value = getcpy (info);
904 for (p = vars; p->p_name; p++)
905 debug_printf ("vars[%d]: name=\"%s\" value=\"%s\"\n",
906 p - vars, p->p_name, trim(p->p_value));
912 * Find a matching name in a lookup table. If found,
913 * return the "pairs" entry, else return NULL.
917 lookup (struct pair *pairs, char *key)
919 for (; pairs->p_name; pairs++)
920 if (!mh_strcasecmp (pairs->p_name, key))
928 * Check utmp(x) file to see if user is currently
943 while ((utp = getutxent()) != NULL) {
944 if ( utp->ut_type == USER_PROCESS && utp->ut_user[0] != 0
945 && strncmp (user, utp->ut_user, sizeof(utp->ut_user)) == 0) {
949 return (utmped = DONE);
954 #endif /* HAVE_GETUTXENT */
955 return (utmped = NOTOK);
958 #define check(t,a,b) if (t < a || t > b) return -1
959 #define cmpar(h1,m1,h2,m2) if (h1 < h2 || (h1 == h2 && m1 < m2)) return 0
962 timely (char *t1, char *t2)
964 int t1hours, t1mins, t2hours, t2mins;
966 if (sscanf (t1, "%d:%d", &t1hours, &t1mins) != 2)
968 check (t1hours, 0, 23);
969 check (t1mins, 0, 59);
971 if (sscanf (t2, "%d:%d", &t2hours, &t2mins) != 2)
973 check (t2hours, 0, 23);
974 check (t2mins, 0, 59);
976 cmpar (now->tw_hour, now->tw_min, t1hours, t1mins);
977 cmpar (t2hours, t2mins, now->tw_hour, now->tw_min);
984 * Deliver message by appending to a file.
988 usr_file (int fd, char *mailbox, int mbx_style)
993 verbose_printf ("delivering to file \"%s\"", mailbox);
995 if (mbx_style == MBOX_FORMAT) {
997 verbose_printf (" (mbox style)");
1001 verbose_printf (" (mmdf style)");
1005 /* open and lock the file */
1006 if ((md = mbx_open (mailbox, mbx_style, pw->pw_uid, pw->pw_gid, m_gmprot())) == -1) {
1008 adorn ("", "unable to open:");
1012 lseek (fd, (off_t) 0, SEEK_SET);
1014 /* append message to file */
1015 if (mbx_copy (mailbox, mbx_style, md, fd, mapping, NULL, verbose) == -1) {
1017 adorn ("", "error writing to:");
1021 /* close and unlock file */
1022 if (mbx_close (mailbox, md) == NOTOK) {
1024 adorn ("", "error closing:");
1029 verbose_printf (", success.\n");
1035 * Deliver message to a nmh folder.
1039 usr_folder (int fd, char *string)
1042 char folder[BUFSIZ], *vec[3];
1044 /* get folder name ready */
1046 strncpy(folder, string, sizeof(folder));
1048 snprintf(folder, sizeof(folder), "+%s", string);
1051 verbose_printf ("delivering to folder \"%s\"", folder + 1);
1053 vec[0] = "rcvstore";
1057 /* use rcvstore to put message in folder */
1058 status = usr_pipe (fd, "rcvstore", rcvstoreproc, vec, 1);
1062 * Currently, verbose status messages are handled by usr_pipe().
1066 verbose_printf (", success.\n");
1068 verbose_printf (", failed.\n");
1076 * Deliver message to a process.
1080 usr_pipe (int fd, char *cmd, char *pgm, char **vec, int suppress)
1083 int i, bytes, seconds, status;
1086 if (verbose && !suppress)
1087 verbose_printf ("delivering to pipe \"%s\"", cmd);
1089 lseek (fd, (off_t) 0, SEEK_SET);
1091 for (i = 0; (child_id = fork()) == -1 && i < 5; i++)
1098 adorn ("fork", "unable to");
1105 freopen ("/dev/null", "w", stdout);
1106 freopen ("/dev/null", "w", stderr);
1112 if ((fd = open ("/dev/tty", O_RDWR)) != -1) {
1113 ioctl (fd, TIOCNOTTY, NULL);
1116 #endif /* TIOCNOTTY */
1118 setpgid ((pid_t) 0, getpid ()); /* put in own process group */
1121 m_putenv ("USER", pw->pw_name);
1122 m_putenv ("HOME", pw->pw_dir);
1123 m_putenv ("SHELL", pw->pw_shell);
1129 /* parent process */
1130 if (! setjmp (myctx)) {
1131 SIGNAL (SIGALRM, alrmser);
1132 bytes = fstat (fd, &st) != -1 ? (int) st.st_size : 100;
1134 /* amount of time to wait depends on message size */
1136 /* give at least 5 minutes */
1138 } else if (bytes >= 90000) {
1139 /* a half hour is long enough */
1142 seconds = (bytes / 60) + 300;
1144 alarm ((unsigned int) seconds);
1145 status = pidwait (child_id, 0);
1150 verbose_printf (", success.\n");
1152 if ((status & 0xff00) == 0xff00)
1153 verbose_printf (", system error\n");
1155 pidstatus (status, stdout, ", failed");
1157 return (status == 0 ? 0 : -1);
1160 * Ruthlessly kill the child and anything
1161 * else in its process group.
1163 killpg(child_id, SIGKILL);
1165 verbose_printf (", timed-out; terminated\n");
1177 longjmp (myctx, DONE);
1182 * Get the `sender' from the envelope
1183 * information ("From " line).
1187 get_sender (char *envelope, char **sender)
1191 unsigned char buffer[BUFSIZ];
1193 if (envelope == NULL) {
1194 *sender = getcpy ("");
1198 i = strlen ("From ");
1199 strncpy (buffer, envelope + i, sizeof(buffer));
1200 if ((cp = strchr(buffer, '\n'))) {
1210 for (cp = buffer + strlen (buffer) - 1; cp >= buffer; cp--)
1215 *sender = getcpy (buffer);
1220 * Copy message into a temporary file.
1221 * While copying, it will do some header processing
1222 * including the extraction of the envelope information.
1226 copy_message (int qd, char *tmpfil, int fold)
1228 int i, first = 1, fd1, fd2;
1229 char buffer[BUFSIZ];
1233 tfile = m_mktemp2(NULL, invo_name, &fd1, NULL);
1234 if (tfile == NULL) return -1;
1236 strncpy (tmpfil, tfile, BUFSIZ);
1239 while ((i = read (qd, buffer, sizeof(buffer))) > 0)
1240 if (write (fd1, buffer, i) != i) {
1248 lseek (fd1, (off_t) 0, SEEK_SET);
1252 /* dup the fd for incoming message */
1253 if ((fd2 = dup (qd)) == -1) {
1258 /* now create a FILE pointer for it */
1259 if ((qfp = fdopen (fd2, "r")) == NULL) {
1265 /* dup the fd for temporary file */
1266 if ((fd2 = dup (fd1)) == -1) {
1272 /* now create a FILE pointer for it */
1273 if ((ffp = fdopen (fd2, "r+")) == NULL) {
1281 * copy message into temporary file
1282 * and massage the headers. Save
1283 * a copy of the "From " line for later.
1285 i = strlen ("From ");
1286 while (fgets (buffer, sizeof(buffer), qfp)) {
1289 if (!strncmp (buffer, "From ", i)) {
1290 /* get copy of envelope information ("From " line) */
1291 envelope = getcpy (buffer);
1294 /* First go ahead and put "From " line in message */
1295 fputs (buffer, ffp);
1300 /* Put the delivery date in message */
1309 fputs (buffer, ffp);
1321 lseek (fd1, (off_t) 0, SEEK_SET);
1333 * Trim strings for pretty printing of debugging output
1339 char buffer[BUFSIZ*4];
1340 unsigned char *bp, *sp;
1345 /* copy string into temp buffer */
1346 strncpy (buffer, cp, sizeof(buffer));
1349 /* skip over leading whitespace */
1350 while (isspace(*bp))
1353 /* start at the end and zap trailing whitespace */
1354 for (sp = bp + strlen(bp) - 1; sp >= bp; sp--) {
1361 /* replace remaining whitespace with spaces */
1362 for (sp = bp; *sp; sp++)
1366 /* now return a copy */
1371 * Function for printing `verbose' messages.
1375 verbose_printf (char *fmt, ...)
1380 vfprintf (stdout, fmt, ap);
1383 fflush (stdout); /* now flush output */
1388 * Function for printing `verbose' delivery
1393 adorn (char *what, char *fmt, ...)
1399 eindex = errno; /* save the errno */
1400 fprintf (stdout, ", ");
1403 vfprintf (stdout, fmt, ap);
1408 fprintf (stdout, " %s: ", what);
1409 if ((s = strerror (eindex)))
1410 fprintf (stdout, "%s", s);
1412 fprintf (stdout, "Error %d", eindex);
1415 fputc ('\n', stdout);
1421 * Function for printing `debug' messages.
1425 debug_printf (char *fmt, ...)
1430 vfprintf (stderr, fmt, ap);
1436 * Check ndbm/db file(s) to see if the Message-Id of this
1437 * message matches the Message-Id of a previous message,
1438 * so we can discard it. If it doesn't match, we add the
1439 * Message-Id of this message to the ndbm/db file.
1442 suppress_duplicates (int fd, char *file)
1444 int fd1, lockfd, state, result;
1445 char *cp, buf[BUFSIZ], name[NAMESZ];
1450 if ((fd1 = dup (fd)) == -1)
1452 if (!(in = fdopen (fd1, "r"))) {
1458 for (state = FLD;;) {
1459 state = m_getfld (state, name, buf, sizeof(buf), in);
1464 /* Search for the message ID */
1465 if (mh_strcasecmp (name, "Message-ID")) {
1466 while (state == FLDPLUS)
1467 state = m_getfld (state, name, buf, sizeof(buf), in);
1471 cp = add (buf, NULL);
1472 while (state == FLDPLUS) {
1473 state = m_getfld (state, name, buf, sizeof(buf), in);
1476 key.dptr = trimcpy (cp);
1477 key.dsize = strlen (key.dptr) + 1;
1481 if (!(db = dbm_open (file, O_RDWR | O_CREAT, 0600))) {
1482 advise (file, "unable to perform dbm_open on");
1488 * Since it is difficult to portable lock a ndbm file,
1489 * we will open and lock the Maildelivery file instead.
1490 * This will fail if your Maildelivery file doesn't
1493 if ((lockfd = lkopen(file, O_RDWR, 0)) == -1) {
1494 advise (file, "unable to perform file locking on");
1499 value = dbm_fetch (db, key);
1502 verbose_printf ("Message-ID: %s\n already received on %s",
1506 value.dptr = ddate + sizeof("Delivery-Date:");
1507 value.dsize = strlen(value.dptr) + 1;
1508 if (dbm_store (db, key, value, DBM_INSERT))
1509 advise (file, "possibly corrupt file");
1514 lkclose(lockfd, file);