3 * slocal.c -- asynchronously filter and deliver new mail
9 * Under sendmail, users should add the line
11 * "| /usr/local/nmh/lib/slocal"
13 * to their $HOME/.forward file.
15 * Under MMDF-I, users should (symbolically) link
16 * /usr/local/nmh/lib/slocal to $HOME/bin/rcvmail.
21 #include <h/dropsbr.h>
22 #include <h/rcvmail.h>
23 #include <h/signals.h>
24 #include <zotnet/tws/tws.h>
25 #include <zotnet/mts/mts.h>
29 #include <sys/ioctl.h>
37 # define UTMP_FILE _PATH_UTMP
39 # define UTMP_FILE "/etc/utmp"
43 static struct swit switches[] = {
45 { "addr address", 0 },
51 { "sender address", 0 },
53 { "mailbox file", 0 },
55 { "home directory", -4 },
59 { "maildelivery file", 0 },
64 #define SUPPRESSDUP 10
66 #define NSUPPRESSDUP 11
67 { "nosuppressdup", 0 },
77 static int globbed = 0; /* have we built "vars" table yet? */
78 static int parsed = 0; /* have we built header field table yet */
79 static int utmped = 0; /* have we scanned umtp(x) file yet */
80 static int suppressdup = 0; /* are we suppressing duplicate messages? */
82 static int verbose = 0;
85 static char *addr = NULL;
86 static char *user = NULL;
87 static char *info = NULL;
88 static char *file = NULL;
89 static char *sender = NULL;
90 static char *envelope = NULL; /* envelope information ("From " line) */
91 static char *mbox = NULL;
92 static char *home = NULL;
94 static struct passwd *pw; /* passwd file entry */
96 static char ddate[BUFSIZ]; /* record the delivery date */
101 /* flags for pair->p_flags */
103 #define P_ADR 0x01 /* field is address */
104 #define P_HID 0x02 /* special (fake) field */
116 * Lookup table for matching fields and patterns
117 * in messages. The rest of the table is added
118 * when the message is parsed.
120 static struct pair hdrs[NVEC + 1] = {
121 { "source", NULL, P_HID },
122 { "addr", NULL, P_HID },
123 { "Return-Path", NULL, P_ADR },
124 { "Reply-To", NULL, P_ADR },
125 { "From", NULL, P_ADR },
126 { "Sender", NULL, P_ADR },
127 { "To", NULL, P_ADR },
128 { "cc", NULL, P_ADR },
129 { "Resent-Reply-To", NULL, P_ADR },
130 { "Resent-From", NULL, P_ADR },
131 { "Resent-Sender", NULL, P_ADR },
132 { "Resent-To", NULL, P_ADR },
133 { "Resent-cc", NULL, P_ADR },
138 * The list of builtin variables to expand in a string
139 * before it is executed by the "pipe" or "qpipe" action.
141 static struct pair vars[] = {
142 { "sender", NULL, P_NIL },
143 { "address", NULL, P_NIL },
144 { "size", NULL, P_NIL },
145 { "reply-to", NULL, P_CHK },
146 { "info", NULL, P_NIL },
150 extern char **environ;
155 static int localmail (int, char *);
156 static int usr_delivery (int, char *, int);
157 static int split (char *, char **);
158 static int parse (int);
159 static void expand (char *, char *, int);
160 static void glob (int);
161 static struct pair *lookup (struct pair *, char *);
162 static int logged_in (void);
163 static int timely (char *, char *);
164 static int usr_file (int, char *, int);
165 static int usr_pipe (int, char *, char *, char **, int);
166 static int usr_folder (int, char *);
167 static RETSIGTYPE alrmser (int);
168 static void get_sender (char *, char **);
169 static int copy_message (int, char *, int);
170 static void verbose_printf (char *fmt, ...);
171 static void adorn (char *, char *, ...);
172 static void debug_printf (char *fmt, ...);
173 static int suppress_duplicates (int, char *);
174 static char *trim (char *);
178 main (int argc, char **argv)
182 char *cp, *mdlvr = NULL, buf[BUFSIZ];
183 char mailbox[BUFSIZ], tmpfil[BUFSIZ];
184 char **argp, **arguments;
187 setlocale(LC_ALL, "");
189 invo_name = r1bindex (*argv, '/');
191 /* foil search of user profile/context */
192 if (context_foil (NULL) == -1)
195 mts_init (invo_name);
196 arguments = getarguments (invo_name, argc, argv, 0);
199 /* Parse arguments */
200 while ((cp = *argp++)) {
202 switch (smatch (++cp, switches)) {
204 ambigsw (cp, switches);
207 adios (NULL, "-%s unknown", cp);
210 snprintf (buf, sizeof(buf),
211 "%s [switches] [address info sender]", invo_name);
212 print_help (buf, switches, 0);
215 print_version(invo_name);
219 if (!(addr = *argp++))/* allow -xyz arguments */
220 adios (NULL, "missing argument to %s", argp[-2]);
223 if (!(info = *argp++))/* allow -xyz arguments */
224 adios (NULL, "missing argument to %s", argp[-2]);
227 if (!(user = *argp++))/* allow -xyz arguments */
228 adios (NULL, "missing argument to %s", argp[-2]);
231 if (!(file = *argp++) || *file == '-')
232 adios (NULL, "missing argument to %s", argp[-2]);
235 if (!(sender = *argp++))/* allow -xyz arguments */
236 adios (NULL, "missing argument to %s", argp[-2]);
239 if (!(mbox = *argp++) || *mbox == '-')
240 adios (NULL, "missing argument to %s", argp[-2]);
243 if (!(home = *argp++) || *home == '-')
244 adios (NULL, "missing argument to %s", argp[-2]);
248 if (!(cp = *argp++) || *cp == '-')
249 adios (NULL, "missing argument to %s", argp[-2]);
251 adios (NULL, "only one maildelivery file at a time!");
274 switch (argp - (argv + 1)) {
290 addr = getusername ();
292 user = (cp = strchr(addr, '.')) ? ++cp : addr;
293 if ((pw = getpwnam (user)) == NULL)
294 adios (NULL, "no such local user as %s", user);
296 if (chdir (pw->pw_dir) == -1)
300 if (geteuid() == 0) {
302 initgroups (pw->pw_name, pw->pw_gid);
309 setbuf (stdin, NULL);
311 /* Record the delivery time */
312 if ((now = dlocaltimenow ()) == NULL)
313 adios (NULL, "unable to ascertain local time");
314 snprintf (ddate, sizeof(ddate), "Delivery-Date: %s\n", dtimenow (0));
317 * Copy the message to a temporary file
322 /* getting message from file */
323 if ((tempfd = open (file, O_RDONLY)) == -1)
324 adios (file, "unable to open");
326 debug_printf ("retrieving message from file \"%s\"\n", file);
327 if ((fd = copy_message (tempfd, tmpfil, 1)) == -1)
328 adios (NULL, "unable to create temporary file");
331 /* getting message from stdin */
333 debug_printf ("retrieving message from stdin\n");
334 if ((fd = copy_message (fileno (stdin), tmpfil, 1)) == -1)
335 adios (NULL, "unable to create temporary file");
339 debug_printf ("temporary file=\"%s\"\n", tmpfil);
341 /* Delete the temp file now or a copy of every single message passed through
342 slocal will be left in the /tmp directory until deleted manually! This
343 unlink() used to be under an 'else' of the 'if (debug)' above, but since
344 some people like to always run slocal with -debug and log the results,
345 the /tmp directory would get choked over time. Of course, now that we
346 always delete the temp file, the "temporary file=" message above is
347 somewhat pointless -- someone watching debug output wouldn't have a
348 chance to 'tail -f' or 'ln' the temp file before it's unlinked. The best
349 thing would be to delay this unlink() until later if debug == 1, but I'll
350 leave that for someone who cares about the temp-file-accessing
351 functionality (they'll have to watch out for cases where we adios()). */
354 if (!(fp = fdopen (fd, "r+")))
355 adios (NULL, "unable to access temporary file");
358 * If no sender given, extract it
359 * from envelope information. */
361 get_sender (envelope, &sender);
364 snprintf (mailbox, sizeof(mailbox), "%s/%s",
365 mmdfldir[0] ? mmdfldir : pw->pw_dir,
366 mmdflfil[0] ? mmdflfil : pw->pw_name);
373 debug_printf ("addr=\"%s\"\n", trim(addr));
374 debug_printf ("user=\"%s\"\n", trim(user));
375 debug_printf ("info=\"%s\"\n", trim(info));
376 debug_printf ("sender=\"%s\"\n", trim(sender));
377 debug_printf ("envelope=\"%s\"\n", envelope ? trim(envelope) : "");
378 debug_printf ("mbox=\"%s\"\n", trim(mbox));
379 debug_printf ("home=\"%s\"\n", trim(home));
380 debug_printf ("ddate=\"%s\"\n", trim(ddate));
381 debug_printf ("now=%02d:%02d\n\n", now->tw_hour, now->tw_min);
384 /* deliver the message */
385 status = localmail (fd, mdlvr);
387 return done (status != -1 ? RCV_MOK : RCV_MBX);
392 * Main routine for delivering message.
396 localmail (int fd, char *mdlvr)
398 /* check if this message is a duplicate */
400 suppress_duplicates(fd, mdlvr ? mdlvr : ".maildelivery") == DONE)
403 /* delivery according to personal Maildelivery file */
404 if (usr_delivery (fd, mdlvr ? mdlvr : ".maildelivery", 0) != -1)
407 /* delivery according to global Maildelivery file */
408 if (usr_delivery (fd, maildelivery, 1) != -1)
412 verbose_printf ("(delivering to standard mail spool)\n");
414 /* last resort - deliver to standard mail spool */
416 return usr_file (fd, mbox, MBOX_FORMAT);
418 return usr_file (fd, mbox, MMDF_FORMAT);
423 #define matches(a,b) (stringdex (b, a) >= 0)
426 * Parse the delivery file, and process incoming message.
430 usr_delivery (int fd, char *delivery, int su)
432 int i, accept, status, won, vecp, next;
433 char *field, *pattern, *action, *result, *string;
434 char buffer[BUFSIZ], tmpbuf[BUFSIZ];
435 char *cp, *vec[NVEC];
440 /* open the delivery file */
441 if ((fp = fopen (delivery, "r")) == NULL)
444 /* check if delivery file has bad ownership or permissions */
445 if (fstat (fileno (fp), &st) == -1
446 || (st.st_uid != 0 && (su || st.st_uid != pw->pw_uid))
447 || st.st_mode & (S_IWGRP|S_IWOTH)) {
449 verbose_printf ("WARNING: %s has bad ownership/modes (su=%d,uid=%d,owner=%d,mode=0%o)\n",
450 delivery, su, (int) pw->pw_uid, (int) st.st_uid, (int) st.st_mode);
458 /* read and process delivery file */
459 while (fgets (buffer, sizeof(buffer), fp)) {
460 /* skip comments and empty lines */
461 if (*buffer == '#' || *buffer == '\n')
464 /* zap trailing newline */
465 if ((cp = strchr(buffer, '\n')))
468 /* split buffer into fields */
469 vecp = split (buffer, vec);
471 /* check for too few fields */
474 debug_printf ("WARNING: entry with only %d fields, skipping.\n", vecp);
479 for (i = 0; vec[i]; i++)
480 debug_printf ("vec[%d]: \"%s\"\n", i, trim(vec[i]));
489 /* find out how to perform the action */
494 * If previous condition failed, don't
495 * do this - else fall through
498 continue; /* else fall */
502 * If already delivered, skip this action. Else
503 * consider delivered if action is successful.
506 continue; /* else fall */
511 * Take action, and consider delivered if
512 * action is successful.
521 * Take action, but don't consider delivered, even
522 * if action is successful
529 if (!strcasecmp (vec[5], "select")) {
530 if (logged_in () != -1)
532 if (vecp > 7 && timely (vec[6], vec[7]) == -1)
537 /* check if the field matches */
545 * "default" matches only if the message hasn't
546 * been delivered yet.
548 if (!strcasecmp (field, "default")) {
555 /* parse message and build lookup table */
556 if (!parsed && parse (fd) == -1) {
561 * find header field in lookup table, and
562 * see if the pattern matches.
564 if ((p = lookup (hdrs, field)) && (p->p_value != NULL)
565 && matches (p->p_value, pattern)) {
574 /* find out the action to perform */
577 /* deliver to quoted pipe */
578 if (strcasecmp (action, "qpipe"))
579 continue; /* else fall */
581 expand (tmpbuf, string, fd);
582 if (split (tmpbuf, vec) < 1)
584 status = usr_pipe (fd, tmpbuf, vec[0], vec, 0);
588 /* deliver to pipe */
589 if (strcasecmp (action, "pipe"))
590 continue; /* else fall */
594 expand (tmpbuf, string, fd);
597 status = usr_pipe (fd, tmpbuf, "/bin/sh", vec + 2, 0);
602 if (!strcasecmp (action, "file")) {
603 status = usr_file (fd, string, MBOX_FORMAT);
606 /* deliver to nmh folder */
607 else if (strcasecmp (action, "folder"))
608 continue; /* else fall */
610 status = usr_folder (fd, string);
615 if (!strcasecmp (action, "mmdf")) {
616 status = usr_file (fd, string, MMDF_FORMAT);
620 else if (strcasecmp (action, "mbox"))
621 continue; /* else fall */
625 status = usr_file (fd, string, MBOX_FORMAT);
630 if (strcasecmp (action, "destroy"))
636 if (accept && status == 0)
641 return (won ? 0 : -1);
648 * Split buffer into fields (delimited by whitespace or
649 * comma's). Return the number of fields found.
653 split (char *cp, char **vec)
660 /* split into a maximum of NVEC fields */
661 for (i = 0; i <= NVEC;) {
664 /* zap any whitespace and comma's */
665 while (isspace (*s) || *s == ',')
668 /* end of buffer, time to leave */
672 /* get double quote text as a single field */
674 for (vec[i++] = ++s; *s && *s != '"'; s++) {
676 * Check for escaped double quote. We need
677 * to shift the string to remove slash.
685 if (*s == '"') /* zap trailing double quote */
690 if (*s == QUOTE && *++s != '"')
694 /* move forward to next field delimiter */
695 while (*s && !isspace (*s) && *s != ',')
705 * Parse the headers of a message, and build the
706 * lookup table for matching fields and patterns.
715 char name[NAMESZ], field[BUFSIZ];
722 /* get a new FILE pointer to message */
723 if ((fd1 = dup (fd)) == -1)
725 if ((in = fdopen (fd1, "r")) == NULL) {
731 /* add special entries to lookup table */
732 if ((p = lookup (hdrs, "source")))
733 p->p_value = getcpy (sender);
734 if ((p = lookup (hdrs, "addr")))
735 p->p_value = getcpy (addr);
738 * Scan the headers of the message and build
741 for (i = 0, state = FLD;;) {
742 switch (state = m_getfld (state, name, field, sizeof(field), in)) {
746 lp = add (field, NULL);
747 while (state == FLDPLUS) {
748 state = m_getfld (state, name, field, sizeof(field), in);
749 lp = add (field, lp);
751 for (p = hdrs; p->p_name; p++) {
752 if (!strcasecmp (p->p_name, name)) {
753 if (!(p->p_flags & P_HID)) {
754 if ((cp = p->p_value)) {
755 if (p->p_flags & P_ADR) {
756 dp = cp + strlen (cp) - 1;
759 cp = add (",\n\t", cp);
764 p->p_value = add (lp, cp);
770 if (p->p_name == NULL && i < NVEC) {
771 p->p_name = getcpy (name);
788 advise (NULL, "format error in message");
792 advise (NULL, "internal error in m_getfld");
800 if ((p = lookup (vars, "reply-to"))) {
801 if ((q = lookup (hdrs, "reply-to")) == NULL || q->p_value == NULL)
802 q = lookup (hdrs, "from");
803 p->p_value = getcpy (q ? q->p_value : "");
804 p->p_flags &= ~P_CHK;
806 debug_printf ("vars[%d]: name=\"%s\" value=\"%s\"\n",
807 p - vars, p->p_name, trim(p->p_value));
810 for (p = hdrs; p->p_name; p++)
811 debug_printf ("hdrs[%d]: name=\"%s\" value=\"%s\"\n",
812 p - hdrs, p->p_name, p->p_value ? trim(p->p_value) : "");
823 * Expand any builtin variables such as $(sender),
824 * $(address), etc., in a string.
828 expand (char *s1, char *s2, int fd)
836 while ((c = *s2++)) {
837 if (c != '$' || *s2 != LPAREN) {
840 for (cp = ++s2; *s2 && *s2 != RPAREN; s2++)
847 if ((p = lookup (vars, cp))) {
848 if (!parsed && (p->p_flags & P_CHK))
851 strcpy (s1, p->p_value);
861 * Fill in the information missing from the "vars"
862 * table, which is necessary to expand any builtin
863 * variables in the string for a "pipe" or "qpipe"
877 if ((p = lookup (vars, "sender")))
878 p->p_value = getcpy (sender);
879 if ((p = lookup (vars, "address")))
880 p->p_value = getcpy (addr);
881 if ((p = lookup (vars, "size"))) {
882 snprintf (buffer, sizeof(buffer), "%d",
883 fstat (fd, &st) != -1 ? (int) st.st_size : 0);
884 p->p_value = getcpy (buffer);
886 if ((p = lookup (vars, "info")))
887 p->p_value = getcpy (info);
890 for (p = vars; p->p_name; p++)
891 debug_printf ("vars[%d]: name=\"%s\" value=\"%s\"\n",
892 p - vars, p->p_name, trim(p->p_value));
898 * Find a matching name in a lookup table. If found,
899 * return the "pairs" entry, else return NULL.
903 lookup (struct pair *pairs, char *key)
905 for (; pairs->p_name; pairs++)
906 if (!strcasecmp (pairs->p_name, key))
914 * Check utmp(x) file to see if user is currently
927 if ((uf = fopen (UTMP_FILE, "r")) == NULL)
930 while (fread ((char *) &ut, sizeof(ut), 1, uf) == 1) {
931 if (ut.ut_name[0] != 0
932 && strncmp (user, ut.ut_name, sizeof(ut.ut_name)) == 0) {
936 return (utmped = DONE);
941 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 mbx_close (mailbox, md);
1012 verbose_printf (", success.\n");
1018 * Deliver message to a nmh folder.
1022 usr_folder (int fd, char *string)
1025 char folder[BUFSIZ], *vec[3];
1027 /* get folder name ready */
1029 strncpy(folder, string, sizeof(folder));
1031 snprintf(folder, sizeof(folder), "+%s", string);
1034 verbose_printf ("delivering to folder \"%s\"", folder + 1);
1036 vec[0] = "rcvstore";
1040 /* use rcvstore to put message in folder */
1041 status = usr_pipe (fd, "rcvstore", rcvstoreproc, vec, 1);
1045 * Currently, verbose status messages are handled by usr_pipe().
1049 verbose_printf (", success.\n");
1051 verbose_printf (", failed.\n");
1059 * Deliver message to a process.
1063 usr_pipe (int fd, char *cmd, char *pgm, char **vec, int suppress)
1066 int i, bytes, seconds, status;
1069 if (verbose && !suppress)
1070 verbose_printf ("delivering to pipe \"%s\"", cmd);
1072 lseek (fd, (off_t) 0, SEEK_SET);
1074 for (i = 0; (child_id = fork()) == -1 && i < 5; i++)
1081 adorn ("fork", "unable to");
1088 freopen ("/dev/null", "w", stdout);
1089 freopen ("/dev/null", "w", stderr);
1095 if ((fd = open ("/dev/tty", O_RDWR)) != -1) {
1096 ioctl (fd, TIOCNOTTY, NULL);
1099 #endif /* TIOCNOTTY */
1101 setpgid ((pid_t) 0, getpid ()); /* put in own process group */
1104 m_putenv ("USER", pw->pw_name);
1105 m_putenv ("HOME", pw->pw_dir);
1106 m_putenv ("SHELL", pw->pw_shell);
1112 /* parent process */
1113 if (!setjmp (myctx)) {
1114 SIGNAL (SIGALRM, alrmser);
1115 bytes = fstat (fd, &st) != -1 ? (int) st.st_size : 100;
1117 /* amount of time to wait depends on message size */
1119 /* give at least 5 minutes */
1121 } else if (bytes >= 90000) {
1122 /* a half hour is long enough */
1125 seconds = (bytes / 60) + 300;
1127 alarm ((unsigned int) seconds);
1128 status = pidwait (child_id, 0);
1132 if (status == RP_MOK || status == RP_OK)
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");
1162 #ifndef RELIABLE_SIGNALS
1163 SIGNAL (SIGALRM, alrmser);
1166 longjmp (myctx, DONE);
1171 * Get the `sender' from the envelope
1172 * information ("From " line).
1176 get_sender (char *envelope, char **sender)
1180 char buffer[BUFSIZ];
1182 if (envelope == NULL) {
1183 *sender = getcpy ("");
1187 i = strlen ("From ");
1188 strncpy (buffer, envelope + i, sizeof(buffer));
1189 if ((cp = strchr(buffer, '\n'))) {
1199 for (cp = buffer + strlen (buffer) - 1; cp >= buffer; cp--)
1204 *sender = getcpy (buffer);
1209 * Copy message into a temporary file.
1210 * While copying, it will do some header processing
1211 * including the extraction of the envelope information.
1215 copy_message (int qd, char *tmpfil, int fold)
1217 int i, first = 1, fd1, fd2;
1218 char buffer[BUFSIZ];
1221 strcpy (tmpfil, m_tmpfil (invo_name));
1223 /* open temporary file to put message in */
1224 if ((fd1 = open (tmpfil, O_RDWR | O_CREAT | O_TRUNC, 0600)) == -1)
1228 while ((i = read (qd, buffer, sizeof(buffer))) > 0)
1229 if (write (fd1, buffer, i) != i) {
1237 lseek (fd1, (off_t) 0, SEEK_SET);
1241 /* dup the fd for incoming message */
1242 if ((fd2 = dup (qd)) == -1) {
1247 /* now create a FILE pointer for it */
1248 if ((qfp = fdopen (fd2, "r")) == NULL) {
1254 /* dup the fd for temporary file */
1255 if ((fd2 = dup (fd1)) == -1) {
1261 /* now create a FILE pointer for it */
1262 if ((ffp = fdopen (fd2, "r+")) == NULL) {
1270 * copy message into temporary file
1271 * and massage the headers. Save
1272 * a copy of the "From " line for later.
1274 i = strlen ("From ");
1275 while (fgets (buffer, sizeof(buffer), qfp)) {
1278 if (!strncmp (buffer, "From ", i)) {
1280 char *fp, *cp, *hp, *ep;
1282 /* get copy of envelope information ("From " line) */
1283 envelope = getcpy (buffer);
1286 /* First go ahead and put "From " line in message */
1287 fputs (buffer, ffp);
1294 * Now create a "Return-Path:" line
1295 * from the "From " line.
1297 hp = cp = strchr(fp = envelope + i, ' ');
1298 while ((hp = strchr(++hp, 'r')))
1299 if (uprf (hp, "remote from")) {
1300 hp = strrchr(hp, ' ');
1304 /* return path for UUCP style addressing */
1305 ep = strchr(++hp, '\n');
1306 snprintf (buffer, sizeof(buffer), "Return-Path: %.*s!%.*s\n",
1307 ep - hp, hp, cp - fp, fp);
1309 /* return path for standard domain addressing */
1310 snprintf (buffer, sizeof(buffer), "Return-Path: %.*s\n",
1314 /* Add Return-Path header to message */
1315 fputs (buffer, ffp);
1319 /* Put the delivery date in message */
1328 fputs (buffer, ffp);
1340 lseek (fd1, (off_t) 0, SEEK_SET);
1352 * Trim strings for pretty printing of debugging output
1358 char buffer[BUFSIZ*4];
1364 /* copy string into temp buffer */
1365 strncpy (buffer, cp, sizeof(buffer));
1368 /* skip over leading whitespace */
1369 while (isspace(*bp))
1372 /* start at the end and zap trailing whitespace */
1373 for (sp = bp + strlen(bp) - 1; sp >= bp; sp--) {
1380 /* replace remaining whitespace with spaces */
1381 for (sp = bp; *sp; sp++)
1385 /* now return a copy */
1390 * Function for printing `verbose' messages.
1394 verbose_printf (char *fmt, ...)
1399 vfprintf (stdout, fmt, ap);
1402 fflush (stdout); /* now flush output */
1407 * Function for printing `verbose' delivery
1412 adorn (char *what, char *fmt, ...)
1418 eindex = errno; /* save the errno */
1419 fprintf (stdout, ", ");
1422 vfprintf (stdout, fmt, ap);
1427 fprintf (stdout, " %s: ", what);
1428 if ((s = strerror (eindex)))
1429 fprintf (stdout, "%s", s);
1431 fprintf (stdout, "Error %d", eindex);
1434 fputc ('\n', stdout);
1440 * Function for printing `debug' messages.
1444 debug_printf (char *fmt, ...)
1449 vfprintf (stderr, fmt, ap);
1455 * Check ndbm/db file(s) to see if the Message-Id of this
1456 * message matches the Message-Id of a previous message,
1457 * so we can discard it. If it doesn't match, we add the
1458 * Message-Id of this message to the ndbm/db file.
1461 suppress_duplicates (int fd, char *file)
1463 int fd1, lockfd, state, result;
1464 char *cp, buf[BUFSIZ], name[NAMESZ];
1469 if ((fd1 = dup (fd)) == -1)
1471 if (!(in = fdopen (fd1, "r"))) {
1477 for (state = FLD;;) {
1478 state = m_getfld (state, name, buf, sizeof(buf), in);
1483 /* Search for the message ID */
1484 if (strcasecmp (name, "Message-ID")) {
1485 while (state == FLDPLUS)
1486 state = m_getfld (state, name, buf, sizeof(buf), in);
1490 cp = add (buf, NULL);
1491 while (state == FLDPLUS) {
1492 state = m_getfld (state, name, buf, sizeof(buf), in);
1495 key.dptr = trimcpy (cp);
1496 key.dsize = strlen (key.dptr) + 1;
1500 if (!(db = dbm_open (file, O_RDWR | O_CREAT, 0600))) {
1501 advise (file, "unable to perform dbm_open on");
1507 * Since it is difficult to portable lock a ndbm file,
1508 * we will open and lock the Maildelivery file instead.
1509 * This will fail if your Maildelivery file doesn't
1512 if ((lockfd = lkopen(file, O_RDWR, 0)) == -1) {
1513 advise (file, "unable to perform file locking on");
1518 value = dbm_fetch (db, key);
1521 verbose_printf ("Message-ID: %s\n already received on %s",
1525 value.dptr = ddate + sizeof("Delivery-Date:");
1526 value.dsize = strlen(value.dptr) + 1;
1527 if (dbm_store (db, key, value, DBM_INSERT))
1528 advise (file, "possibly corrupt file");
1533 lkclose(lockfd, file);