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. */
59 # define UTMP_FILE _PATH_UTMP
61 # define UTMP_FILE "/etc/utmp"
66 static struct swit switches[] = {
68 { "addr address", 0 },
74 { "sender address", 0 },
76 { "mailbox file", 0 },
78 { "home directory", -4 },
82 { "maildelivery file", 0 },
87 #define SUPPRESSDUP 10
89 #define NSUPPRESSDUP 11
90 { "nosuppressdup", 0 },
100 static int globbed = 0; /* have we built "vars" table yet? */
101 static int parsed = 0; /* have we built header field table yet */
102 static int utmped = 0; /* have we scanned umtp(x) file yet */
103 static int suppressdup = 0; /* are we suppressing duplicate messages? */
105 static int verbose = 0;
106 static int debug = 0;
108 static char *addr = NULL;
109 static char *user = NULL;
110 static char *info = NULL;
111 static char *file = NULL;
112 static char *sender = NULL;
113 static char *envelope = NULL; /* envelope information ("From " line) */
114 static char *mbox = NULL;
115 static char *home = NULL;
117 static struct passwd *pw; /* passwd file entry */
119 static char ddate[BUFSIZ]; /* record the delivery date */
122 static jmp_buf myctx;
124 /* flags for pair->p_flags */
126 #define P_ADR 0x01 /* field is address */
127 #define P_HID 0x02 /* special (fake) field */
139 * Lookup table for matching fields and patterns
140 * in messages. The rest of the table is added
141 * when the message is parsed.
143 static struct pair hdrs[NVEC + 1] = {
144 { "source", NULL, P_HID },
145 { "addr", NULL, P_HID },
146 { "Return-Path", NULL, P_ADR },
147 { "Reply-To", NULL, P_ADR },
148 { "From", NULL, P_ADR },
149 { "Sender", NULL, P_ADR },
150 { "To", NULL, P_ADR },
151 { "cc", NULL, P_ADR },
152 { "Resent-Reply-To", NULL, P_ADR },
153 { "Resent-From", NULL, P_ADR },
154 { "Resent-Sender", NULL, P_ADR },
155 { "Resent-To", NULL, P_ADR },
156 { "Resent-cc", NULL, P_ADR },
161 * The list of builtin variables to expand in a string
162 * before it is executed by the "pipe" or "qpipe" action.
164 static struct pair vars[] = {
165 { "sender", NULL, P_NIL },
166 { "address", NULL, P_NIL },
167 { "size", NULL, P_NIL },
168 { "reply-to", NULL, P_CHK },
169 { "info", NULL, P_NIL },
173 extern char **environ;
178 static int localmail (int, char *);
179 static int usr_delivery (int, char *, int);
180 static int split (char *, char **);
181 static int parse (int);
182 static void expand (char *, char *, int);
183 static void glob (int);
184 static struct pair *lookup (struct pair *, char *);
185 static int logged_in (void);
186 static int timely (char *, char *);
187 static int usr_file (int, char *, int);
188 static int usr_pipe (int, char *, char *, char **, int);
189 static int usr_folder (int, char *);
190 static void alrmser (int);
191 static void get_sender (char *, char **);
192 static int copy_message (int, char *, int);
193 static void verbose_printf (char *fmt, ...);
194 static void adorn (char *, char *, ...);
195 static void debug_printf (char *fmt, ...);
196 static int suppress_duplicates (int, char *);
197 static char *trim (char *);
201 main (int argc, char **argv)
205 char *cp, *mdlvr = NULL, buf[BUFSIZ];
206 char mailbox[BUFSIZ], tmpfil[BUFSIZ];
207 char **argp, **arguments;
210 setlocale(LC_ALL, "");
212 invo_name = r1bindex (*argv, '/');
214 /* foil search of user profile/context */
215 if (context_foil (NULL) == -1)
218 mts_init (invo_name);
219 arguments = getarguments (invo_name, argc, argv, 0);
222 /* Parse arguments */
223 while ((cp = *argp++)) {
225 switch (smatch (++cp, switches)) {
227 ambigsw (cp, switches);
230 adios (NULL, "-%s unknown", cp);
233 snprintf (buf, sizeof(buf),
234 "%s [switches] [address info sender]", invo_name);
235 print_help (buf, switches, 0);
238 print_version(invo_name);
242 if (!(addr = *argp++))/* allow -xyz arguments */
243 adios (NULL, "missing argument to %s", argp[-2]);
246 if (!(info = *argp++))/* allow -xyz arguments */
247 adios (NULL, "missing argument to %s", argp[-2]);
250 if (!(user = *argp++))/* allow -xyz arguments */
251 adios (NULL, "missing argument to %s", argp[-2]);
254 if (!(file = *argp++) || *file == '-')
255 adios (NULL, "missing argument to %s", argp[-2]);
258 if (!(sender = *argp++))/* allow -xyz arguments */
259 adios (NULL, "missing argument to %s", argp[-2]);
262 if (!(mbox = *argp++) || *mbox == '-')
263 adios (NULL, "missing argument to %s", argp[-2]);
266 if (!(home = *argp++) || *home == '-')
267 adios (NULL, "missing argument to %s", argp[-2]);
271 if (!(cp = *argp++) || *cp == '-')
272 adios (NULL, "missing argument to %s", argp[-2]);
274 adios (NULL, "only one maildelivery file at a time!");
297 switch (argp - (argv + 1)) {
313 addr = getusername ();
315 user = (cp = strchr(addr, '.')) ? ++cp : addr;
316 if ((pw = getpwnam (user)) == NULL)
317 adios (NULL, "no such local user as %s", user);
319 if (chdir (pw->pw_dir) == -1)
323 if (geteuid() == 0) {
325 initgroups (pw->pw_name, pw->pw_gid);
332 setbuf (stdin, NULL);
334 /* Record the delivery time */
335 if ((now = dlocaltimenow ()) == NULL)
336 adios (NULL, "unable to ascertain local time");
337 snprintf (ddate, sizeof(ddate), "Delivery-Date: %s\n", dtimenow (0));
340 * Copy the message to a temporary file
345 /* getting message from file */
346 if ((tempfd = open (file, O_RDONLY)) == -1)
347 adios (file, "unable to open");
349 debug_printf ("retrieving message from file \"%s\"\n", file);
350 if ((fd = copy_message (tempfd, tmpfil, 1)) == -1)
351 adios (NULL, "unable to create temporary file");
354 /* getting message from stdin */
356 debug_printf ("retrieving message from stdin\n");
357 if ((fd = copy_message (fileno (stdin), tmpfil, 1)) == -1)
358 adios (NULL, "unable to create temporary file");
362 debug_printf ("temporary file=\"%s\"\n", tmpfil);
364 /* Delete the temp file now or a copy of every single message passed through
365 slocal will be left in the /tmp directory until deleted manually! This
366 unlink() used to be under an 'else' of the 'if (debug)' above, but since
367 some people like to always run slocal with -debug and log the results,
368 the /tmp directory would get choked over time. Of course, now that we
369 always delete the temp file, the "temporary file=" message above is
370 somewhat pointless -- someone watching debug output wouldn't have a
371 chance to 'tail -f' or 'ln' the temp file before it's unlinked. The best
372 thing would be to delay this unlink() until later if debug == 1, but I'll
373 leave that for someone who cares about the temp-file-accessing
374 functionality (they'll have to watch out for cases where we adios()). */
377 if (!(fp = fdopen (fd, "r+")))
378 adios (NULL, "unable to access temporary file");
381 * If no sender given, extract it
382 * from envelope information. */
384 get_sender (envelope, &sender);
387 snprintf (mailbox, sizeof(mailbox), "%s/%s",
388 mmdfldir[0] ? mmdfldir : pw->pw_dir,
389 mmdflfil[0] ? mmdflfil : pw->pw_name);
396 debug_printf ("addr=\"%s\"\n", trim(addr));
397 debug_printf ("user=\"%s\"\n", trim(user));
398 debug_printf ("info=\"%s\"\n", trim(info));
399 debug_printf ("sender=\"%s\"\n", trim(sender));
400 debug_printf ("envelope=\"%s\"\n", envelope ? trim(envelope) : "");
401 debug_printf ("mbox=\"%s\"\n", trim(mbox));
402 debug_printf ("home=\"%s\"\n", trim(home));
403 debug_printf ("ddate=\"%s\"\n", trim(ddate));
404 debug_printf ("now=%02d:%02d\n\n", now->tw_hour, now->tw_min);
407 /* deliver the message */
408 status = localmail (fd, mdlvr);
410 done (status != -1 ? RCV_MOK : RCV_MBX);
416 * Main routine for delivering message.
420 localmail (int fd, char *mdlvr)
422 /* check if this message is a duplicate */
424 suppress_duplicates(fd, mdlvr ? mdlvr : ".maildelivery") == DONE)
427 /* delivery according to personal Maildelivery file */
428 if (usr_delivery (fd, mdlvr ? mdlvr : ".maildelivery", 0) != -1)
431 /* delivery according to global Maildelivery file */
432 if (usr_delivery (fd, maildelivery, 1) != -1)
436 verbose_printf ("(delivering to standard mail spool)\n");
438 /* last resort - deliver to standard mail spool */
439 return usr_file (fd, mbox, MBOX_FORMAT);
443 #define matches(a,b) (stringdex (b, a) >= 0)
446 * Parse the delivery file, and process incoming message.
450 usr_delivery (int fd, char *delivery, int su)
452 int i, accept, status=1, won, vecp, next;
453 char *field, *pattern, *action, *result, *string;
454 char buffer[BUFSIZ], tmpbuf[BUFSIZ];
455 char *cp, *vec[NVEC];
460 /* open the delivery file */
461 if ((fp = fopen (delivery, "r")) == NULL)
464 /* check if delivery file has bad ownership or permissions */
465 if (fstat (fileno (fp), &st) == -1
466 || (st.st_uid != 0 && (su || st.st_uid != pw->pw_uid))
467 || st.st_mode & (S_IWGRP|S_IWOTH)) {
469 verbose_printf ("WARNING: %s has bad ownership/modes (su=%d,uid=%d,owner=%d,mode=0%o)\n",
470 delivery, su, (int) pw->pw_uid, (int) st.st_uid, (int) st.st_mode);
478 /* read and process delivery file */
479 while (fgets (buffer, sizeof(buffer), fp)) {
480 /* skip comments and empty lines */
481 if (*buffer == '#' || *buffer == '\n')
484 /* zap trailing newline */
485 if ((cp = strchr(buffer, '\n')))
488 /* split buffer into fields */
489 vecp = split (buffer, vec);
491 /* check for too few fields */
494 debug_printf ("WARNING: entry with only %d fields, skipping.\n", vecp);
499 for (i = 0; vec[i]; i++)
500 debug_printf ("vec[%d]: \"%s\"\n", i, trim(vec[i]));
509 /* find out how to perform the action */
514 * If previous condition failed, don't
515 * do this - else fall through
518 continue; /* else fall */
522 * If already delivered, skip this action. Else
523 * consider delivered if action is successful.
526 continue; /* else fall */
531 * Take action, and consider delivered if
532 * action is successful.
541 * Take action, but don't consider delivered, even
542 * if action is successful
549 if (!mh_strcasecmp (vec[5], "select")) {
550 if (logged_in () != -1)
552 if (vecp > 7 && timely (vec[6], vec[7]) == -1)
557 /* check if the field matches */
565 * "default" matches only if the message hasn't
566 * been delivered yet.
568 if (!mh_strcasecmp (field, "default")) {
575 /* parse message and build lookup table */
576 if (!parsed && parse (fd) == -1) {
581 * find header field in lookup table, and
582 * see if the pattern matches.
584 if ((p = lookup (hdrs, field)) && (p->p_value != NULL)
585 && matches (p->p_value, pattern)) {
594 /* find out the action to perform */
597 /* deliver to quoted pipe */
598 if (mh_strcasecmp (action, "qpipe"))
599 continue; /* else fall */
601 expand (tmpbuf, string, fd);
602 if (split (tmpbuf, vec) < 1)
604 status = usr_pipe (fd, tmpbuf, vec[0], vec, 0);
608 /* deliver to pipe */
609 if (mh_strcasecmp (action, "pipe"))
610 continue; /* else fall */
614 expand (tmpbuf, string, fd);
617 status = usr_pipe (fd, tmpbuf, "/bin/sh", vec + 2, 0);
622 if (!mh_strcasecmp (action, "file")) {
623 status = usr_file (fd, string, MBOX_FORMAT);
626 /* deliver to nmh folder */
627 else if (mh_strcasecmp (action, "folder"))
628 continue; /* else fall */
630 status = usr_folder (fd, string);
635 if (!mh_strcasecmp (action, "mmdf")) {
636 status = usr_file (fd, string, MMDF_FORMAT);
640 else if (mh_strcasecmp (action, "mbox"))
641 continue; /* else fall */
645 status = usr_file (fd, string, MBOX_FORMAT);
650 if (mh_strcasecmp (action, "destroy"))
656 if (status) next = 0; /* action failed, mark for 'N' result */
658 if (accept && status == 0)
663 return (won ? 0 : -1);
670 * Split buffer into fields (delimited by whitespace or
671 * comma's). Return the number of fields found.
675 split (char *cp, char **vec)
682 /* split into a maximum of NVEC fields */
683 for (i = 0; i <= NVEC;) {
686 /* zap any whitespace and comma's */
687 while (isspace (*s) || *s == ',')
690 /* end of buffer, time to leave */
694 /* get double quote text as a single field */
696 for (vec[i++] = ++s; *s && *s != '"'; s++) {
698 * Check for escaped double quote. We need
699 * to shift the string to remove slash.
707 if (*s == '"') /* zap trailing double quote */
712 if (*s == QUOTE && *++s != '"')
716 /* move forward to next field delimiter */
717 while (*s && !isspace (*s) && *s != ',')
727 * Parse the headers of a message, and build the
728 * lookup table for matching fields and patterns.
737 char name[NAMESZ], field[BUFSIZ];
744 /* get a new FILE pointer to message */
745 if ((fd1 = dup (fd)) == -1)
747 if ((in = fdopen (fd1, "r")) == NULL) {
753 /* add special entries to lookup table */
754 if ((p = lookup (hdrs, "source")))
755 p->p_value = getcpy (sender);
756 if ((p = lookup (hdrs, "addr")))
757 p->p_value = getcpy (addr);
760 * Scan the headers of the message and build
763 for (i = 0, state = FLD;;) {
764 switch (state = m_getfld (state, name, field, sizeof(field), in)) {
768 lp = add (field, NULL);
769 while (state == FLDPLUS) {
770 state = m_getfld (state, name, field, sizeof(field), in);
771 lp = add (field, lp);
773 for (p = hdrs; p->p_name; p++) {
774 if (!mh_strcasecmp (p->p_name, name)) {
775 if (!(p->p_flags & P_HID)) {
776 if ((cp = p->p_value)) {
777 if (p->p_flags & P_ADR) {
778 dp = cp + strlen (cp) - 1;
781 cp = add (",\n\t", cp);
786 p->p_value = add (lp, cp);
792 if (p->p_name == NULL && i < NVEC) {
793 p->p_name = getcpy (name);
810 advise (NULL, "format error in message");
814 advise (NULL, "internal error in m_getfld");
822 if ((p = lookup (vars, "reply-to"))) {
823 if ((q = lookup (hdrs, "reply-to")) == NULL || q->p_value == NULL)
824 q = lookup (hdrs, "from");
825 p->p_value = getcpy (q ? q->p_value : "");
826 p->p_flags &= ~P_CHK;
828 debug_printf ("vars[%d]: name=\"%s\" value=\"%s\"\n",
829 p - vars, p->p_name, trim(p->p_value));
832 for (p = hdrs; p->p_name; p++)
833 debug_printf ("hdrs[%d]: name=\"%s\" value=\"%s\"\n",
834 p - hdrs, p->p_name, p->p_value ? trim(p->p_value) : "");
845 * Expand any builtin variables such as $(sender),
846 * $(address), etc., in a string.
850 expand (char *s1, char *s2, int fd)
858 while ((c = *s2++)) {
859 if (c != '$' || *s2 != LPAREN) {
862 for (cp = ++s2; *s2 && *s2 != RPAREN; s2++)
869 if ((p = lookup (vars, cp))) {
870 if (!parsed && (p->p_flags & P_CHK))
873 strcpy (s1, p->p_value);
883 * Fill in the information missing from the "vars"
884 * table, which is necessary to expand any builtin
885 * variables in the string for a "pipe" or "qpipe"
899 if ((p = lookup (vars, "sender")))
900 p->p_value = getcpy (sender);
901 if ((p = lookup (vars, "address")))
902 p->p_value = getcpy (addr);
903 if ((p = lookup (vars, "size"))) {
904 snprintf (buffer, sizeof(buffer), "%d",
905 fstat (fd, &st) != -1 ? (int) st.st_size : 0);
906 p->p_value = getcpy (buffer);
908 if ((p = lookup (vars, "info")))
909 p->p_value = getcpy (info);
912 for (p = vars; p->p_name; p++)
913 debug_printf ("vars[%d]: name=\"%s\" value=\"%s\"\n",
914 p - vars, p->p_name, trim(p->p_value));
920 * Find a matching name in a lookup table. If found,
921 * return the "pairs" entry, else return NULL.
925 lookup (struct pair *pairs, char *key)
927 for (; pairs->p_name; pairs++)
928 if (!mh_strcasecmp (pairs->p_name, key))
936 * Check utmp(x) file to see if user is currently
951 while ((utp = getutent()) != NULL) {
953 #ifdef HAVE_STRUCT_UTMP_UT_TYPE
954 utp->ut_type == USER_PROCESS
958 && strncmp (user, utp->ut_name, sizeof(utp->ut_name)) == 0) {
962 return (utmped = DONE);
967 return (utmped = NOTOK);
979 if ((uf = fopen (UTMP_FILE, "r")) == NULL)
982 while (fread ((char *) &ut, sizeof(ut), 1, uf) == 1) {
983 if (ut.ut_name[0] != 0
984 && strncmp (user, ut.ut_name, sizeof(ut.ut_name)) == 0) {
988 return (utmped = DONE);
993 return (utmped = NOTOK);
997 #define check(t,a,b) if (t < a || t > b) return -1
998 #define cmpar(h1,m1,h2,m2) if (h1 < h2 || (h1 == h2 && m1 < m2)) return 0
1001 timely (char *t1, char *t2)
1003 int t1hours, t1mins, t2hours, t2mins;
1005 if (sscanf (t1, "%d:%d", &t1hours, &t1mins) != 2)
1007 check (t1hours, 0, 23);
1008 check (t1mins, 0, 59);
1010 if (sscanf (t2, "%d:%d", &t2hours, &t2mins) != 2)
1012 check (t2hours, 0, 23);
1013 check (t2mins, 0, 59);
1015 cmpar (now->tw_hour, now->tw_min, t1hours, t1mins);
1016 cmpar (t2hours, t2mins, now->tw_hour, now->tw_min);
1023 * Deliver message by appending to a file.
1027 usr_file (int fd, char *mailbox, int mbx_style)
1032 verbose_printf ("delivering to file \"%s\"", mailbox);
1034 if (mbx_style == MBOX_FORMAT) {
1036 verbose_printf (" (mbox style)");
1040 verbose_printf (" (mmdf style)");
1044 /* open and lock the file */
1045 if ((md = mbx_open (mailbox, mbx_style, pw->pw_uid, pw->pw_gid, m_gmprot())) == -1) {
1047 adorn ("", "unable to open:");
1051 lseek (fd, (off_t) 0, SEEK_SET);
1053 /* append message to file */
1054 if (mbx_copy (mailbox, mbx_style, md, fd, mapping, NULL, verbose) == -1) {
1056 adorn ("", "error writing to:");
1060 /* close and unlock file */
1061 if (mbx_close (mailbox, md) == NOTOK) {
1063 adorn ("", "error closing:");
1068 verbose_printf (", success.\n");
1074 * Deliver message to a nmh folder.
1078 usr_folder (int fd, char *string)
1081 char folder[BUFSIZ], *vec[3];
1083 /* get folder name ready */
1085 strncpy(folder, string, sizeof(folder));
1087 snprintf(folder, sizeof(folder), "+%s", string);
1090 verbose_printf ("delivering to folder \"%s\"", folder + 1);
1092 vec[0] = "rcvstore";
1096 /* use rcvstore to put message in folder */
1097 status = usr_pipe (fd, "rcvstore", rcvstoreproc, vec, 1);
1101 * Currently, verbose status messages are handled by usr_pipe().
1105 verbose_printf (", success.\n");
1107 verbose_printf (", failed.\n");
1115 * Deliver message to a process.
1119 usr_pipe (int fd, char *cmd, char *pgm, char **vec, int suppress)
1122 int i, bytes, seconds, status;
1125 if (verbose && !suppress)
1126 verbose_printf ("delivering to pipe \"%s\"", cmd);
1128 lseek (fd, (off_t) 0, SEEK_SET);
1130 for (i = 0; (child_id = fork()) == -1 && i < 5; i++)
1137 adorn ("fork", "unable to");
1144 freopen ("/dev/null", "w", stdout);
1145 freopen ("/dev/null", "w", stderr);
1151 if ((fd = open ("/dev/tty", O_RDWR)) != -1) {
1152 ioctl (fd, TIOCNOTTY, NULL);
1155 #endif /* TIOCNOTTY */
1157 setpgid ((pid_t) 0, getpid ()); /* put in own process group */
1160 m_putenv ("USER", pw->pw_name);
1161 m_putenv ("HOME", pw->pw_dir);
1162 m_putenv ("SHELL", pw->pw_shell);
1168 /* parent process */
1169 if (! m_setjmp (myctx)) {
1170 SIGNAL (SIGALRM, alrmser);
1171 bytes = fstat (fd, &st) != -1 ? (int) st.st_size : 100;
1173 /* amount of time to wait depends on message size */
1175 /* give at least 5 minutes */
1177 } else if (bytes >= 90000) {
1178 /* a half hour is long enough */
1181 seconds = (bytes / 60) + 300;
1183 alarm ((unsigned int) seconds);
1184 status = pidwait (child_id, 0);
1189 verbose_printf (", success.\n");
1191 if ((status & 0xff00) == 0xff00)
1192 verbose_printf (", system error\n");
1194 pidstatus (status, stdout, ", failed");
1196 return (status == 0 ? 0 : -1);
1199 * Ruthlessly kill the child and anything
1200 * else in its process group.
1202 killpg(child_id, SIGKILL);
1204 verbose_printf (", timed-out; terminated\n");
1216 longjmp (myctx, DONE);
1221 * Get the `sender' from the envelope
1222 * information ("From " line).
1226 get_sender (char *envelope, char **sender)
1230 unsigned char buffer[BUFSIZ];
1232 if (envelope == NULL) {
1233 *sender = getcpy ("");
1237 i = strlen ("From ");
1238 strncpy (buffer, envelope + i, sizeof(buffer));
1239 if ((cp = strchr(buffer, '\n'))) {
1249 for (cp = buffer + strlen (buffer) - 1; cp >= buffer; cp--)
1254 *sender = getcpy (buffer);
1259 * Copy message into a temporary file.
1260 * While copying, it will do some header processing
1261 * including the extraction of the envelope information.
1265 copy_message (int qd, char *tmpfil, int fold)
1267 int i, first = 1, fd1, fd2;
1268 char buffer[BUFSIZ];
1272 tfile = m_mktemp2(NULL, invo_name, &fd1, NULL);
1273 if (tfile == NULL) return -1;
1275 strncpy (tmpfil, tfile, BUFSIZ);
1278 while ((i = read (qd, buffer, sizeof(buffer))) > 0)
1279 if (write (fd1, buffer, i) != i) {
1287 lseek (fd1, (off_t) 0, SEEK_SET);
1291 /* dup the fd for incoming message */
1292 if ((fd2 = dup (qd)) == -1) {
1297 /* now create a FILE pointer for it */
1298 if ((qfp = fdopen (fd2, "r")) == NULL) {
1304 /* dup the fd for temporary file */
1305 if ((fd2 = dup (fd1)) == -1) {
1311 /* now create a FILE pointer for it */
1312 if ((ffp = fdopen (fd2, "r+")) == NULL) {
1320 * copy message into temporary file
1321 * and massage the headers. Save
1322 * a copy of the "From " line for later.
1324 i = strlen ("From ");
1325 while (fgets (buffer, sizeof(buffer), qfp)) {
1328 if (!strncmp (buffer, "From ", i)) {
1329 /* get copy of envelope information ("From " line) */
1330 envelope = getcpy (buffer);
1333 /* First go ahead and put "From " line in message */
1334 fputs (buffer, ffp);
1339 /* Put the delivery date in message */
1348 fputs (buffer, ffp);
1360 lseek (fd1, (off_t) 0, SEEK_SET);
1372 * Trim strings for pretty printing of debugging output
1378 char buffer[BUFSIZ*4];
1379 unsigned char *bp, *sp;
1384 /* copy string into temp buffer */
1385 strncpy (buffer, cp, sizeof(buffer));
1388 /* skip over leading whitespace */
1389 while (isspace(*bp))
1392 /* start at the end and zap trailing whitespace */
1393 for (sp = bp + strlen(bp) - 1; sp >= bp; sp--) {
1400 /* replace remaining whitespace with spaces */
1401 for (sp = bp; *sp; sp++)
1405 /* now return a copy */
1410 * Function for printing `verbose' messages.
1414 verbose_printf (char *fmt, ...)
1419 vfprintf (stdout, fmt, ap);
1422 fflush (stdout); /* now flush output */
1427 * Function for printing `verbose' delivery
1432 adorn (char *what, char *fmt, ...)
1438 eindex = errno; /* save the errno */
1439 fprintf (stdout, ", ");
1442 vfprintf (stdout, fmt, ap);
1447 fprintf (stdout, " %s: ", what);
1448 if ((s = strerror (eindex)))
1449 fprintf (stdout, "%s", s);
1451 fprintf (stdout, "Error %d", eindex);
1454 fputc ('\n', stdout);
1460 * Function for printing `debug' messages.
1464 debug_printf (char *fmt, ...)
1469 vfprintf (stderr, fmt, ap);
1475 * Check ndbm/db file(s) to see if the Message-Id of this
1476 * message matches the Message-Id of a previous message,
1477 * so we can discard it. If it doesn't match, we add the
1478 * Message-Id of this message to the ndbm/db file.
1481 suppress_duplicates (int fd, char *file)
1483 int fd1, lockfd, state, result;
1484 char *cp, buf[BUFSIZ], name[NAMESZ];
1489 if ((fd1 = dup (fd)) == -1)
1491 if (!(in = fdopen (fd1, "r"))) {
1497 for (state = FLD;;) {
1498 state = m_getfld (state, name, buf, sizeof(buf), in);
1503 /* Search for the message ID */
1504 if (mh_strcasecmp (name, "Message-ID")) {
1505 while (state == FLDPLUS)
1506 state = m_getfld (state, name, buf, sizeof(buf), in);
1510 cp = add (buf, NULL);
1511 while (state == FLDPLUS) {
1512 state = m_getfld (state, name, buf, sizeof(buf), in);
1515 key.dptr = trimcpy (cp);
1516 key.dsize = strlen (key.dptr) + 1;
1520 if (!(db = dbm_open (file, O_RDWR | O_CREAT, 0600))) {
1521 advise (file, "unable to perform dbm_open on");
1527 * Since it is difficult to portable lock a ndbm file,
1528 * we will open and lock the Maildelivery file instead.
1529 * This will fail if your Maildelivery file doesn't
1532 if ((lockfd = lkopen(file, O_RDWR, 0)) == -1) {
1533 advise (file, "unable to perform file locking on");
1538 value = dbm_fetch (db, key);
1541 verbose_printf ("Message-ID: %s\n already received on %s",
1545 value.dptr = ddate + sizeof("Delivery-Date:");
1546 value.dsize = strlen(value.dptr) + 1;
1547 if (dbm_store (db, key, value, DBM_INSERT))
1548 advise (file, "possibly corrupt file");
1553 lkclose(lockfd, file);