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), "%s [switches]", invo_name);
226 print_help (buf, switches, 0);
229 print_version(invo_name);
233 if (!(addr = *argp++))/* allow -xyz arguments */
234 adios (NULL, "missing argument to %s", argp[-2]);
237 if (!(info = *argp++))/* allow -xyz arguments */
238 adios (NULL, "missing argument to %s", argp[-2]);
241 if (!(user = *argp++))/* allow -xyz arguments */
242 adios (NULL, "missing argument to %s", argp[-2]);
245 if (!(file = *argp++) || *file == '-')
246 adios (NULL, "missing argument to %s", argp[-2]);
249 if (!(sender = *argp++))/* allow -xyz arguments */
250 adios (NULL, "missing argument to %s", argp[-2]);
253 if (!(mbox = *argp++) || *mbox == '-')
254 adios (NULL, "missing argument to %s", argp[-2]);
257 if (!(home = *argp++) || *home == '-')
258 adios (NULL, "missing argument to %s", argp[-2]);
262 if (!(cp = *argp++) || *cp == '-')
263 adios (NULL, "missing argument to %s", argp[-2]);
265 adios (NULL, "only one maildelivery file at a time!");
287 adios (NULL, "only switch arguments are supported");
292 addr = getusername ();
294 user = (cp = strchr(addr, '.')) ? ++cp : addr;
295 if ((pw = getpwnam (user)) == NULL)
296 adios (NULL, "no such local user as %s", user);
298 if (chdir (pw->pw_dir) == -1)
302 if (geteuid() == 0) {
304 initgroups (pw->pw_name, pw->pw_gid);
311 setbuf (stdin, NULL);
313 /* Record the delivery time */
314 if ((now = dlocaltimenow ()) == NULL)
315 adios (NULL, "unable to ascertain local time");
316 snprintf (ddate, sizeof(ddate), "Delivery-Date: %s\n", dtimenow (0));
319 * Copy the message to a temporary file
324 /* getting message from file */
325 if ((tempfd = open (file, O_RDONLY)) == -1)
326 adios (file, "unable to open");
328 debug_printf ("retrieving message from file \"%s\"\n", file);
329 if ((fd = copy_message (tempfd, tmpfil, 1)) == -1)
330 adios (NULL, "unable to create temporary file");
333 /* getting message from stdin */
335 debug_printf ("retrieving message from stdin\n");
336 if ((fd = copy_message (fileno (stdin), tmpfil, 1)) == -1)
337 adios (NULL, "unable to create temporary file");
341 debug_printf ("temporary file=\"%s\"\n", tmpfil);
343 /* Delete the temp file now or a copy of every single message passed through
344 slocal will be left in the /tmp directory until deleted manually! This
345 unlink() used to be under an 'else' of the 'if (debug)' above, but since
346 some people like to always run slocal with -debug and log the results,
347 the /tmp directory would get choked over time. Of course, now that we
348 always delete the temp file, the "temporary file=" message above is
349 somewhat pointless -- someone watching debug output wouldn't have a
350 chance to 'tail -f' or 'ln' the temp file before it's unlinked. The best
351 thing would be to delay this unlink() until later if debug == 1, but I'll
352 leave that for someone who cares about the temp-file-accessing
353 functionality (they'll have to watch out for cases where we adios()). */
356 if (!(fp = fdopen (fd, "r+")))
357 adios (NULL, "unable to access temporary file");
360 * If no sender given, extract it
361 * from envelope information. */
363 get_sender (envelope, &sender);
366 snprintf (mailbox, sizeof(mailbox), "%s/%s",
367 mmdfldir[0] ? mmdfldir : pw->pw_dir,
368 mmdflfil[0] ? mmdflfil : pw->pw_name);
375 debug_printf ("addr=\"%s\"\n", trim(addr));
376 debug_printf ("user=\"%s\"\n", trim(user));
377 debug_printf ("info=\"%s\"\n", trim(info));
378 debug_printf ("sender=\"%s\"\n", trim(sender));
379 debug_printf ("envelope=\"%s\"\n", envelope ? trim(envelope) : "");
380 debug_printf ("mbox=\"%s\"\n", trim(mbox));
381 debug_printf ("home=\"%s\"\n", trim(home));
382 debug_printf ("ddate=\"%s\"\n", trim(ddate));
383 debug_printf ("now=%02d:%02d\n\n", now->tw_hour, now->tw_min);
386 /* deliver the message */
387 status = localmail (fd, mdlvr);
389 done (status != -1 ? RCV_MOK : RCV_MBX);
395 * Main routine for delivering message.
399 localmail (int fd, char *mdlvr)
401 /* check if this message is a duplicate */
403 suppress_duplicates(fd, mdlvr ? mdlvr : ".maildelivery") == DONE)
406 /* delivery according to personal Maildelivery file */
407 if (usr_delivery (fd, mdlvr ? mdlvr : ".maildelivery", 0) != -1)
410 /* delivery according to global Maildelivery file */
411 if (usr_delivery (fd, maildelivery, 1) != -1)
415 verbose_printf ("(delivering to standard mail spool)\n");
417 /* last resort - deliver to standard mail spool */
418 return usr_file (fd, mbox, MBOX_FORMAT);
422 #define matches(a,b) (stringdex (b, a) >= 0)
425 * Parse the delivery file, and process incoming message.
429 usr_delivery (int fd, char *delivery, int su)
431 int i, accept, status=1, won, vecp, next;
432 char *field, *pattern, *action, *result, *string;
433 char buffer[BUFSIZ], tmpbuf[BUFSIZ];
434 char *cp, *vec[NVEC];
439 /* open the delivery file */
440 if ((fp = fopen (delivery, "r")) == NULL)
443 /* check if delivery file has bad ownership or permissions */
444 if (fstat (fileno (fp), &st) == -1
445 || (st.st_uid != 0 && (su || st.st_uid != pw->pw_uid))
446 || st.st_mode & (S_IWGRP|S_IWOTH)) {
448 verbose_printf ("WARNING: %s has bad ownership/modes (su=%d,uid=%d,owner=%d,mode=0%o)\n",
449 delivery, su, (int) pw->pw_uid, (int) st.st_uid, (int) st.st_mode);
457 /* read and process delivery file */
458 while (fgets (buffer, sizeof(buffer), fp)) {
459 /* skip comments and empty lines */
460 if (*buffer == '#' || *buffer == '\n')
463 /* zap trailing newline */
464 if ((cp = strchr(buffer, '\n')))
467 /* split buffer into fields */
468 vecp = split (buffer, vec);
470 /* check for too few fields */
473 debug_printf ("WARNING: entry with only %d fields, skipping.\n", vecp);
478 for (i = 0; vec[i]; i++)
479 debug_printf ("vec[%d]: \"%s\"\n", i, trim(vec[i]));
488 /* find out how to perform the action */
493 * If previous condition failed, don't
494 * do this - else fall through
497 continue; /* else fall */
501 * If already delivered, skip this action. Else
502 * consider delivered if action is successful.
505 continue; /* else fall */
510 * Take action, and consider delivered if
511 * action is successful.
520 * Take action, but don't consider delivered, even
521 * if action is successful
528 if (!mh_strcasecmp (vec[5], "select")) {
529 if (logged_in () != -1)
531 if (vecp > 7 && timely (vec[6], vec[7]) == -1)
536 /* check if the field matches */
544 * "default" matches only if the message hasn't
545 * been delivered yet.
547 if (!mh_strcasecmp (field, "default")) {
554 /* parse message and build lookup table */
555 if (!parsed && parse (fd) == -1) {
560 * find header field in lookup table, and
561 * see if the pattern matches.
563 if ((p = lookup (hdrs, field)) && (p->p_value != NULL)
564 && matches (p->p_value, pattern)) {
573 /* find out the action to perform */
576 /* deliver to quoted pipe */
577 if (mh_strcasecmp (action, "qpipe"))
578 continue; /* else fall */
580 expand (tmpbuf, string, fd);
581 if (split (tmpbuf, vec) < 1)
583 status = usr_pipe (fd, tmpbuf, vec[0], vec, 0);
587 /* deliver to pipe */
588 if (mh_strcasecmp (action, "pipe"))
589 continue; /* else fall */
593 expand (tmpbuf, string, fd);
596 status = usr_pipe (fd, tmpbuf, "/bin/sh", vec + 2, 0);
601 if (!mh_strcasecmp (action, "file")) {
602 status = usr_file (fd, string, MBOX_FORMAT);
605 /* deliver to nmh folder */
606 else if (mh_strcasecmp (action, "folder"))
607 continue; /* else fall */
609 status = usr_folder (fd, string);
614 if (!mh_strcasecmp (action, "mmdf")) {
615 status = usr_file (fd, string, MMDF_FORMAT);
619 else if (mh_strcasecmp (action, "mbox"))
620 continue; /* else fall */
624 status = usr_file (fd, string, MBOX_FORMAT);
629 if (mh_strcasecmp (action, "destroy"))
635 if (status) next = 0; /* action failed, mark for 'N' result */
637 if (accept && status == 0)
642 return (won ? 0 : -1);
649 * Split buffer into fields (delimited by whitespace or
650 * comma's). Return the number of fields found.
654 split (char *cp, char **vec)
661 /* split into a maximum of NVEC fields */
662 for (i = 0; i <= NVEC;) {
665 /* zap any whitespace and comma's */
666 while (isspace (*s) || *s == ',')
669 /* end of buffer, time to leave */
673 /* get double quote text as a single field */
675 for (vec[i++] = ++s; *s && *s != '"'; s++) {
677 * Check for escaped double quote. We need
678 * to shift the string to remove slash.
686 if (*s == '"') /* zap trailing double quote */
691 if (*s == QUOTE && *++s != '"')
695 /* move forward to next field delimiter */
696 while (*s && !isspace (*s) && *s != ',')
706 * Parse the headers of a message, and build the
707 * lookup table for matching fields and patterns.
716 char name[NAMESZ], field[BUFSIZ];
723 /* get a new FILE pointer to message */
724 if ((fd1 = dup (fd)) == -1)
726 if ((in = fdopen (fd1, "r")) == NULL) {
732 /* add special entries to lookup table */
733 if ((p = lookup (hdrs, "source")))
734 p->p_value = getcpy (sender);
735 if ((p = lookup (hdrs, "addr")))
736 p->p_value = getcpy (addr);
739 * Scan the headers of the message and build
742 for (i = 0, state = FLD;;) {
743 switch (state = m_getfld (state, name, field, sizeof(field), in)) {
747 lp = add (field, NULL);
748 while (state == FLDPLUS) {
749 state = m_getfld (state, name, field, sizeof(field), in);
750 lp = add (field, lp);
752 for (p = hdrs; p->p_name; p++) {
753 if (!mh_strcasecmp (p->p_name, name)) {
754 if (!(p->p_flags & P_HID)) {
755 if ((cp = p->p_value)) {
756 if (p->p_flags & P_ADR) {
757 dp = cp + strlen (cp) - 1;
760 cp = add (",\n\t", cp);
765 p->p_value = add (lp, cp);
771 if (p->p_name == NULL && i < NVEC) {
772 p->p_name = getcpy (name);
789 advise (NULL, "format error in message");
793 advise (NULL, "internal error in m_getfld");
801 if ((p = lookup (vars, "reply-to"))) {
802 if ((q = lookup (hdrs, "reply-to")) == NULL || q->p_value == NULL)
803 q = lookup (hdrs, "from");
804 p->p_value = getcpy (q ? q->p_value : "");
805 p->p_flags &= ~P_CHK;
807 debug_printf ("vars[%d]: name=\"%s\" value=\"%s\"\n",
808 p - vars, p->p_name, trim(p->p_value));
811 for (p = hdrs; p->p_name; p++)
812 debug_printf ("hdrs[%d]: name=\"%s\" value=\"%s\"\n",
813 p - hdrs, p->p_name, p->p_value ? trim(p->p_value) : "");
824 * Expand any builtin variables such as $(sender),
825 * $(address), etc., in a string.
829 expand (char *s1, char *s2, int fd)
837 while ((c = *s2++)) {
838 if (c != '$' || *s2 != LPAREN) {
841 for (cp = ++s2; *s2 && *s2 != RPAREN; s2++)
848 if ((p = lookup (vars, cp))) {
849 if (!parsed && (p->p_flags & P_CHK))
852 strcpy (s1, p->p_value);
862 * Fill in the information missing from the "vars"
863 * table, which is necessary to expand any builtin
864 * variables in the string for a "pipe" or "qpipe"
878 if ((p = lookup (vars, "sender")))
879 p->p_value = getcpy (sender);
880 if ((p = lookup (vars, "address")))
881 p->p_value = getcpy (addr);
882 if ((p = lookup (vars, "size"))) {
883 snprintf (buffer, sizeof(buffer), "%d",
884 fstat (fd, &st) != -1 ? (int) st.st_size : 0);
885 p->p_value = getcpy (buffer);
887 if ((p = lookup (vars, "info")))
888 p->p_value = getcpy (info);
891 for (p = vars; p->p_name; p++)
892 debug_printf ("vars[%d]: name=\"%s\" value=\"%s\"\n",
893 p - vars, p->p_name, trim(p->p_value));
899 * Find a matching name in a lookup table. If found,
900 * return the "pairs" entry, else return NULL.
904 lookup (struct pair *pairs, char *key)
906 for (; pairs->p_name; pairs++)
907 if (!mh_strcasecmp (pairs->p_name, key))
915 * Check utmp(x) file to see if user is currently
930 while ((utp = getutxent()) != NULL) {
931 if ( utp->ut_type == USER_PROCESS && utp->ut_user[0] != 0
932 && strncmp (user, utp->ut_user, sizeof(utp->ut_user)) == 0) {
936 return (utmped = DONE);
941 #endif /* HAVE_GETUTXENT */
942 return (utmped = NOTOK);
945 #define check(t,a,b) if (t < a || t > b) return -1
946 #define cmpar(h1,m1,h2,m2) if (h1 < h2 || (h1 == h2 && m1 < m2)) return 0
949 timely (char *t1, char *t2)
951 int t1hours, t1mins, t2hours, t2mins;
953 if (sscanf (t1, "%d:%d", &t1hours, &t1mins) != 2)
955 check (t1hours, 0, 23);
956 check (t1mins, 0, 59);
958 if (sscanf (t2, "%d:%d", &t2hours, &t2mins) != 2)
960 check (t2hours, 0, 23);
961 check (t2mins, 0, 59);
963 cmpar (now->tw_hour, now->tw_min, t1hours, t1mins);
964 cmpar (t2hours, t2mins, now->tw_hour, now->tw_min);
971 * Deliver message by appending to a file.
975 usr_file (int fd, char *mailbox, int mbx_style)
980 verbose_printf ("delivering to file \"%s\"", mailbox);
982 if (mbx_style == MBOX_FORMAT) {
984 verbose_printf (" (mbox style)");
988 verbose_printf (" (mmdf style)");
992 /* open and lock the file */
993 if ((md = mbx_open (mailbox, mbx_style, pw->pw_uid, pw->pw_gid, m_gmprot())) == -1) {
995 adorn ("", "unable to open:");
999 lseek (fd, (off_t) 0, SEEK_SET);
1001 /* append message to file */
1002 if (mbx_copy (mailbox, mbx_style, md, fd, mapping, NULL, verbose) == -1) {
1004 adorn ("", "error writing to:");
1008 /* close and unlock file */
1009 if (mbx_close (mailbox, md) == NOTOK) {
1011 adorn ("", "error closing:");
1016 verbose_printf (", success.\n");
1022 * Deliver message to a nmh folder.
1026 usr_folder (int fd, char *string)
1029 char folder[BUFSIZ], *vec[3];
1031 /* get folder name ready */
1033 strncpy(folder, string, sizeof(folder));
1035 snprintf(folder, sizeof(folder), "+%s", string);
1038 verbose_printf ("delivering to folder \"%s\"", folder + 1);
1040 vec[0] = "rcvstore";
1044 /* use rcvstore to put message in folder */
1045 status = usr_pipe (fd, "rcvstore", rcvstoreproc, vec, 1);
1049 * Currently, verbose status messages are handled by usr_pipe().
1053 verbose_printf (", success.\n");
1055 verbose_printf (", failed.\n");
1063 * Deliver message to a process.
1067 usr_pipe (int fd, char *cmd, char *pgm, char **vec, int suppress)
1070 int i, bytes, seconds, status;
1073 if (verbose && !suppress)
1074 verbose_printf ("delivering to pipe \"%s\"", cmd);
1076 lseek (fd, (off_t) 0, SEEK_SET);
1078 for (i = 0; (child_id = fork()) == -1 && i < 5; i++)
1085 adorn ("fork", "unable to");
1092 freopen ("/dev/null", "w", stdout);
1093 freopen ("/dev/null", "w", stderr);
1099 if ((fd = open ("/dev/tty", O_RDWR)) != -1) {
1100 ioctl (fd, TIOCNOTTY, NULL);
1103 #endif /* TIOCNOTTY */
1105 setpgid ((pid_t) 0, getpid ()); /* put in own process group */
1108 m_putenv ("USER", pw->pw_name);
1109 m_putenv ("HOME", pw->pw_dir);
1110 m_putenv ("SHELL", pw->pw_shell);
1116 /* parent process */
1117 if (! setjmp (myctx)) {
1118 SIGNAL (SIGALRM, alrmser);
1119 bytes = fstat (fd, &st) != -1 ? (int) st.st_size : 100;
1121 /* amount of time to wait depends on message size */
1123 /* give at least 5 minutes */
1125 } else if (bytes >= 90000) {
1126 /* a half hour is long enough */
1129 seconds = (bytes / 60) + 300;
1131 alarm ((unsigned int) seconds);
1132 status = pidwait (child_id, 0);
1137 verbose_printf (", success.\n");
1139 if ((status & 0xff00) == 0xff00)
1140 verbose_printf (", system error\n");
1142 pidstatus (status, stdout, ", failed");
1144 return (status == 0 ? 0 : -1);
1147 * Ruthlessly kill the child and anything
1148 * else in its process group.
1150 killpg(child_id, SIGKILL);
1152 verbose_printf (", timed-out; terminated\n");
1164 longjmp (myctx, DONE);
1169 * Get the `sender' from the envelope
1170 * information ("From " line).
1174 get_sender (char *envelope, char **sender)
1178 unsigned char buffer[BUFSIZ];
1180 if (envelope == NULL) {
1181 *sender = getcpy ("");
1185 i = strlen ("From ");
1186 strncpy (buffer, envelope + i, sizeof(buffer));
1187 if ((cp = strchr(buffer, '\n'))) {
1197 for (cp = buffer + strlen (buffer) - 1; cp >= buffer; cp--)
1202 *sender = getcpy (buffer);
1207 * Copy message into a temporary file.
1208 * While copying, it will do some header processing
1209 * including the extraction of the envelope information.
1213 copy_message (int qd, char *tmpfil, int fold)
1215 int i, first = 1, fd1, fd2;
1216 char buffer[BUFSIZ];
1220 tfile = m_mktemp2(NULL, invo_name, &fd1, NULL);
1221 if (tfile == NULL) return -1;
1223 strncpy (tmpfil, tfile, BUFSIZ);
1226 while ((i = read (qd, buffer, sizeof(buffer))) > 0)
1227 if (write (fd1, buffer, i) != i) {
1235 lseek (fd1, (off_t) 0, SEEK_SET);
1239 /* dup the fd for incoming message */
1240 if ((fd2 = dup (qd)) == -1) {
1245 /* now create a FILE pointer for it */
1246 if ((qfp = fdopen (fd2, "r")) == NULL) {
1252 /* dup the fd for temporary file */
1253 if ((fd2 = dup (fd1)) == -1) {
1259 /* now create a FILE pointer for it */
1260 if ((ffp = fdopen (fd2, "r+")) == NULL) {
1268 * copy message into temporary file
1269 * and massage the headers. Save
1270 * a copy of the "From " line for later.
1272 i = strlen ("From ");
1273 while (fgets (buffer, sizeof(buffer), qfp)) {
1276 if (!strncmp (buffer, "From ", i)) {
1277 /* get copy of envelope information ("From " line) */
1278 envelope = getcpy (buffer);
1281 /* First go ahead and put "From " line in message */
1282 fputs (buffer, ffp);
1287 /* Put the delivery date in message */
1296 fputs (buffer, ffp);
1308 lseek (fd1, (off_t) 0, SEEK_SET);
1320 * Trim strings for pretty printing of debugging output
1326 char buffer[BUFSIZ*4];
1327 unsigned char *bp, *sp;
1332 /* copy string into temp buffer */
1333 strncpy (buffer, cp, sizeof(buffer));
1336 /* skip over leading whitespace */
1337 while (isspace(*bp))
1340 /* start at the end and zap trailing whitespace */
1341 for (sp = bp + strlen(bp) - 1; sp >= bp; sp--) {
1348 /* replace remaining whitespace with spaces */
1349 for (sp = bp; *sp; sp++)
1353 /* now return a copy */
1358 * Function for printing `verbose' messages.
1362 verbose_printf (char *fmt, ...)
1367 vfprintf (stdout, fmt, ap);
1370 fflush (stdout); /* now flush output */
1375 * Function for printing `verbose' delivery
1380 adorn (char *what, char *fmt, ...)
1386 eindex = errno; /* save the errno */
1387 fprintf (stdout, ", ");
1390 vfprintf (stdout, fmt, ap);
1395 fprintf (stdout, " %s: ", what);
1396 if ((s = strerror (eindex)))
1397 fprintf (stdout, "%s", s);
1399 fprintf (stdout, "Error %d", eindex);
1402 fputc ('\n', stdout);
1408 * Function for printing `debug' messages.
1412 debug_printf (char *fmt, ...)
1417 vfprintf (stderr, fmt, ap);
1423 * Check ndbm/db file(s) to see if the Message-Id of this
1424 * message matches the Message-Id of a previous message,
1425 * so we can discard it. If it doesn't match, we add the
1426 * Message-Id of this message to the ndbm/db file.
1429 suppress_duplicates (int fd, char *file)
1431 int fd1, lockfd, state, result;
1432 char *cp, buf[BUFSIZ], name[NAMESZ];
1437 if ((fd1 = dup (fd)) == -1)
1439 if (!(in = fdopen (fd1, "r"))) {
1445 for (state = FLD;;) {
1446 state = m_getfld (state, name, buf, sizeof(buf), in);
1451 /* Search for the message ID */
1452 if (mh_strcasecmp (name, "Message-ID")) {
1453 while (state == FLDPLUS)
1454 state = m_getfld (state, name, buf, sizeof(buf), in);
1458 cp = add (buf, NULL);
1459 while (state == FLDPLUS) {
1460 state = m_getfld (state, name, buf, sizeof(buf), in);
1463 key.dptr = trimcpy (cp);
1464 key.dsize = strlen (key.dptr) + 1;
1468 if (!(db = dbm_open (file, O_RDWR | O_CREAT, 0600))) {
1469 advise (file, "unable to perform dbm_open on");
1475 * Since it is difficult to portable lock a ndbm file,
1476 * we will open and lock the Maildelivery file instead.
1477 * This will fail if your Maildelivery file doesn't
1480 if ((lockfd = lkopen(file, O_RDWR, 0)) == -1) {
1481 advise (file, "unable to perform file locking on");
1486 value = dbm_fetch (db, key);
1489 verbose_printf ("Message-ID: %s\n already received on %s",
1493 value.dptr = ddate + sizeof("Delivery-Date:");
1494 value.dsize = strlen(value.dptr) + 1;
1495 if (dbm_store (db, key, value, DBM_INSERT))
1496 advise (file, "possibly corrupt file");
1501 lkclose(lockfd, file);