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>
32 #ifdef INITGROUPS_HEADER
33 #include INITGROUPS_HEADER
35 /* On AIX 4.1, initgroups() is defined and even documented (giving the parameter
36 types as char* and int), but doesn't have a prototype in any of the system
37 header files. AIX 4.3 and SunOS 4.1.3 have the same problem. */
38 extern int initgroups(char*, int);
42 #ifdef HAVE_DB1_NDBM_H
52 # define UTMP_FILE _PATH_UTMP
54 # define UTMP_FILE "/etc/utmp"
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 RETSIGTYPE 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 return done (status != -1 ? RCV_MOK : RCV_MBX);
407 * Main routine for delivering message.
411 localmail (int fd, char *mdlvr)
413 /* check if this message is a duplicate */
415 suppress_duplicates(fd, mdlvr ? mdlvr : ".maildelivery") == DONE)
418 /* delivery according to personal Maildelivery file */
419 if (usr_delivery (fd, mdlvr ? mdlvr : ".maildelivery", 0) != -1)
422 /* delivery according to global Maildelivery file */
423 if (usr_delivery (fd, maildelivery, 1) != -1)
427 verbose_printf ("(delivering to standard mail spool)\n");
429 /* last resort - deliver to standard mail spool */
431 return usr_file (fd, mbox, MBOX_FORMAT);
433 return usr_file (fd, mbox, MMDF_FORMAT);
438 #define matches(a,b) (stringdex (b, a) >= 0)
441 * Parse the delivery file, and process incoming message.
445 usr_delivery (int fd, char *delivery, int su)
447 int i, accept, status, won, vecp, next;
448 char *field, *pattern, *action, *result, *string;
449 char buffer[BUFSIZ], tmpbuf[BUFSIZ];
450 char *cp, *vec[NVEC];
455 /* open the delivery file */
456 if ((fp = fopen (delivery, "r")) == NULL)
459 /* check if delivery file has bad ownership or permissions */
460 if (fstat (fileno (fp), &st) == -1
461 || (st.st_uid != 0 && (su || st.st_uid != pw->pw_uid))
462 || st.st_mode & (S_IWGRP|S_IWOTH)) {
464 verbose_printf ("WARNING: %s has bad ownership/modes (su=%d,uid=%d,owner=%d,mode=0%o)\n",
465 delivery, su, (int) pw->pw_uid, (int) st.st_uid, (int) st.st_mode);
473 /* read and process delivery file */
474 while (fgets (buffer, sizeof(buffer), fp)) {
475 /* skip comments and empty lines */
476 if (*buffer == '#' || *buffer == '\n')
479 /* zap trailing newline */
480 if ((cp = strchr(buffer, '\n')))
483 /* split buffer into fields */
484 vecp = split (buffer, vec);
486 /* check for too few fields */
489 debug_printf ("WARNING: entry with only %d fields, skipping.\n", vecp);
494 for (i = 0; vec[i]; i++)
495 debug_printf ("vec[%d]: \"%s\"\n", i, trim(vec[i]));
504 /* find out how to perform the action */
509 * If previous condition failed, don't
510 * do this - else fall through
513 continue; /* else fall */
517 * If already delivered, skip this action. Else
518 * consider delivered if action is successful.
521 continue; /* else fall */
526 * Take action, and consider delivered if
527 * action is successful.
536 * Take action, but don't consider delivered, even
537 * if action is successful
544 if (!strcasecmp (vec[5], "select")) {
545 if (logged_in () != -1)
547 if (vecp > 7 && timely (vec[6], vec[7]) == -1)
552 /* check if the field matches */
560 * "default" matches only if the message hasn't
561 * been delivered yet.
563 if (!strcasecmp (field, "default")) {
570 /* parse message and build lookup table */
571 if (!parsed && parse (fd) == -1) {
576 * find header field in lookup table, and
577 * see if the pattern matches.
579 if ((p = lookup (hdrs, field)) && (p->p_value != NULL)
580 && matches (p->p_value, pattern)) {
589 /* find out the action to perform */
592 /* deliver to quoted pipe */
593 if (strcasecmp (action, "qpipe"))
594 continue; /* else fall */
596 expand (tmpbuf, string, fd);
597 if (split (tmpbuf, vec) < 1)
599 status = usr_pipe (fd, tmpbuf, vec[0], vec, 0);
603 /* deliver to pipe */
604 if (strcasecmp (action, "pipe"))
605 continue; /* else fall */
609 expand (tmpbuf, string, fd);
612 status = usr_pipe (fd, tmpbuf, "/bin/sh", vec + 2, 0);
617 if (!strcasecmp (action, "file")) {
618 status = usr_file (fd, string, MBOX_FORMAT);
621 /* deliver to nmh folder */
622 else if (strcasecmp (action, "folder"))
623 continue; /* else fall */
625 status = usr_folder (fd, string);
630 if (!strcasecmp (action, "mmdf")) {
631 status = usr_file (fd, string, MMDF_FORMAT);
635 else if (strcasecmp (action, "mbox"))
636 continue; /* else fall */
640 status = usr_file (fd, string, MBOX_FORMAT);
645 if (strcasecmp (action, "destroy"))
651 if (accept && status == 0)
656 return (won ? 0 : -1);
663 * Split buffer into fields (delimited by whitespace or
664 * comma's). Return the number of fields found.
668 split (char *cp, char **vec)
675 /* split into a maximum of NVEC fields */
676 for (i = 0; i <= NVEC;) {
679 /* zap any whitespace and comma's */
680 while (isspace (*s) || *s == ',')
683 /* end of buffer, time to leave */
687 /* get double quote text as a single field */
689 for (vec[i++] = ++s; *s && *s != '"'; s++) {
691 * Check for escaped double quote. We need
692 * to shift the string to remove slash.
700 if (*s == '"') /* zap trailing double quote */
705 if (*s == QUOTE && *++s != '"')
709 /* move forward to next field delimiter */
710 while (*s && !isspace (*s) && *s != ',')
720 * Parse the headers of a message, and build the
721 * lookup table for matching fields and patterns.
730 char name[NAMESZ], field[BUFSIZ];
737 /* get a new FILE pointer to message */
738 if ((fd1 = dup (fd)) == -1)
740 if ((in = fdopen (fd1, "r")) == NULL) {
746 /* add special entries to lookup table */
747 if ((p = lookup (hdrs, "source")))
748 p->p_value = getcpy (sender);
749 if ((p = lookup (hdrs, "addr")))
750 p->p_value = getcpy (addr);
753 * Scan the headers of the message and build
756 for (i = 0, state = FLD;;) {
757 switch (state = m_getfld (state, name, field, sizeof(field), in)) {
761 lp = add (field, NULL);
762 while (state == FLDPLUS) {
763 state = m_getfld (state, name, field, sizeof(field), in);
764 lp = add (field, lp);
766 for (p = hdrs; p->p_name; p++) {
767 if (!strcasecmp (p->p_name, name)) {
768 if (!(p->p_flags & P_HID)) {
769 if ((cp = p->p_value)) {
770 if (p->p_flags & P_ADR) {
771 dp = cp + strlen (cp) - 1;
774 cp = add (",\n\t", cp);
779 p->p_value = add (lp, cp);
785 if (p->p_name == NULL && i < NVEC) {
786 p->p_name = getcpy (name);
803 advise (NULL, "format error in message");
807 advise (NULL, "internal error in m_getfld");
815 if ((p = lookup (vars, "reply-to"))) {
816 if ((q = lookup (hdrs, "reply-to")) == NULL || q->p_value == NULL)
817 q = lookup (hdrs, "from");
818 p->p_value = getcpy (q ? q->p_value : "");
819 p->p_flags &= ~P_CHK;
821 debug_printf ("vars[%d]: name=\"%s\" value=\"%s\"\n",
822 p - vars, p->p_name, trim(p->p_value));
825 for (p = hdrs; p->p_name; p++)
826 debug_printf ("hdrs[%d]: name=\"%s\" value=\"%s\"\n",
827 p - hdrs, p->p_name, p->p_value ? trim(p->p_value) : "");
838 * Expand any builtin variables such as $(sender),
839 * $(address), etc., in a string.
843 expand (char *s1, char *s2, int fd)
851 while ((c = *s2++)) {
852 if (c != '$' || *s2 != LPAREN) {
855 for (cp = ++s2; *s2 && *s2 != RPAREN; s2++)
862 if ((p = lookup (vars, cp))) {
863 if (!parsed && (p->p_flags & P_CHK))
866 strcpy (s1, p->p_value);
876 * Fill in the information missing from the "vars"
877 * table, which is necessary to expand any builtin
878 * variables in the string for a "pipe" or "qpipe"
892 if ((p = lookup (vars, "sender")))
893 p->p_value = getcpy (sender);
894 if ((p = lookup (vars, "address")))
895 p->p_value = getcpy (addr);
896 if ((p = lookup (vars, "size"))) {
897 snprintf (buffer, sizeof(buffer), "%d",
898 fstat (fd, &st) != -1 ? (int) st.st_size : 0);
899 p->p_value = getcpy (buffer);
901 if ((p = lookup (vars, "info")))
902 p->p_value = getcpy (info);
905 for (p = vars; p->p_name; p++)
906 debug_printf ("vars[%d]: name=\"%s\" value=\"%s\"\n",
907 p - vars, p->p_name, trim(p->p_value));
913 * Find a matching name in a lookup table. If found,
914 * return the "pairs" entry, else return NULL.
918 lookup (struct pair *pairs, char *key)
920 for (; pairs->p_name; pairs++)
921 if (!strcasecmp (pairs->p_name, key))
929 * Check utmp(x) file to see if user is currently
942 if ((uf = fopen (UTMP_FILE, "r")) == NULL)
945 while (fread ((char *) &ut, sizeof(ut), 1, uf) == 1) {
946 if (ut.ut_name[0] != 0
947 && strncmp (user, ut.ut_name, sizeof(ut.ut_name)) == 0) {
951 return (utmped = DONE);
956 return (utmped = NOTOK);
960 #define check(t,a,b) if (t < a || t > b) return -1
961 #define cmpar(h1,m1,h2,m2) if (h1 < h2 || (h1 == h2 && m1 < m2)) return 0
964 timely (char *t1, char *t2)
966 int t1hours, t1mins, t2hours, t2mins;
968 if (sscanf (t1, "%d:%d", &t1hours, &t1mins) != 2)
970 check (t1hours, 0, 23);
971 check (t1mins, 0, 59);
973 if (sscanf (t2, "%d:%d", &t2hours, &t2mins) != 2)
975 check (t2hours, 0, 23);
976 check (t2mins, 0, 59);
978 cmpar (now->tw_hour, now->tw_min, t1hours, t1mins);
979 cmpar (t2hours, t2mins, now->tw_hour, now->tw_min);
986 * Deliver message by appending to a file.
990 usr_file (int fd, char *mailbox, int mbx_style)
995 verbose_printf ("delivering to file \"%s\"", mailbox);
997 if (mbx_style == MBOX_FORMAT) {
999 verbose_printf (" (mbox style)");
1003 verbose_printf (" (mmdf style)");
1007 /* open and lock the file */
1008 if ((md = mbx_open (mailbox, mbx_style, pw->pw_uid, pw->pw_gid, m_gmprot())) == -1) {
1010 adorn ("", "unable to open:");
1014 lseek (fd, (off_t) 0, SEEK_SET);
1016 /* append message to file */
1017 if (mbx_copy (mailbox, mbx_style, md, fd, mapping, NULL, verbose) == -1) {
1019 adorn ("", "error writing to:");
1023 /* close and unlock file */
1024 mbx_close (mailbox, md);
1027 verbose_printf (", success.\n");
1033 * Deliver message to a nmh folder.
1037 usr_folder (int fd, char *string)
1040 char folder[BUFSIZ], *vec[3];
1042 /* get folder name ready */
1044 strncpy(folder, string, sizeof(folder));
1046 snprintf(folder, sizeof(folder), "+%s", string);
1049 verbose_printf ("delivering to folder \"%s\"", folder + 1);
1051 vec[0] = "rcvstore";
1055 /* use rcvstore to put message in folder */
1056 status = usr_pipe (fd, "rcvstore", rcvstoreproc, vec, 1);
1060 * Currently, verbose status messages are handled by usr_pipe().
1064 verbose_printf (", success.\n");
1066 verbose_printf (", failed.\n");
1074 * Deliver message to a process.
1078 usr_pipe (int fd, char *cmd, char *pgm, char **vec, int suppress)
1081 int i, bytes, seconds, status;
1084 if (verbose && !suppress)
1085 verbose_printf ("delivering to pipe \"%s\"", cmd);
1087 lseek (fd, (off_t) 0, SEEK_SET);
1089 for (i = 0; (child_id = fork()) == -1 && i < 5; i++)
1096 adorn ("fork", "unable to");
1103 freopen ("/dev/null", "w", stdout);
1104 freopen ("/dev/null", "w", stderr);
1110 if ((fd = open ("/dev/tty", O_RDWR)) != -1) {
1111 ioctl (fd, TIOCNOTTY, NULL);
1114 #endif /* TIOCNOTTY */
1116 setpgid ((pid_t) 0, getpid ()); /* put in own process group */
1119 m_putenv ("USER", pw->pw_name);
1120 m_putenv ("HOME", pw->pw_dir);
1121 m_putenv ("SHELL", pw->pw_shell);
1127 /* parent process */
1128 if (!setjmp (myctx)) {
1129 SIGNAL (SIGALRM, alrmser);
1130 bytes = fstat (fd, &st) != -1 ? (int) st.st_size : 100;
1132 /* amount of time to wait depends on message size */
1134 /* give at least 5 minutes */
1136 } else if (bytes >= 90000) {
1137 /* a half hour is long enough */
1140 seconds = (bytes / 60) + 300;
1142 alarm ((unsigned int) seconds);
1143 status = pidwait (child_id, 0);
1147 if (status == RP_MOK || status == RP_OK)
1152 verbose_printf (", success.\n");
1154 if ((status & 0xff00) == 0xff00)
1155 verbose_printf (", system error\n");
1157 pidstatus (status, stdout, ", failed");
1159 return (status == 0 ? 0 : -1);
1162 * Ruthlessly kill the child and anything
1163 * else in its process group.
1165 KILLPG(child_id, SIGKILL);
1167 verbose_printf (", timed-out; terminated\n");
1177 #ifndef RELIABLE_SIGNALS
1178 SIGNAL (SIGALRM, alrmser);
1181 longjmp (myctx, DONE);
1186 * Get the `sender' from the envelope
1187 * information ("From " line).
1191 get_sender (char *envelope, char **sender)
1195 char buffer[BUFSIZ];
1197 if (envelope == NULL) {
1198 *sender = getcpy ("");
1202 i = strlen ("From ");
1203 strncpy (buffer, envelope + i, sizeof(buffer));
1204 if ((cp = strchr(buffer, '\n'))) {
1214 for (cp = buffer + strlen (buffer) - 1; cp >= buffer; cp--)
1219 *sender = getcpy (buffer);
1224 * Copy message into a temporary file.
1225 * While copying, it will do some header processing
1226 * including the extraction of the envelope information.
1230 copy_message (int qd, char *tmpfil, int fold)
1232 int i, first = 1, fd1, fd2;
1233 char buffer[BUFSIZ];
1236 strcpy (tmpfil, m_tmpfil (invo_name));
1238 /* open temporary file to put message in */
1239 if ((fd1 = open (tmpfil, O_RDWR | O_CREAT | O_TRUNC, 0600)) == -1)
1243 while ((i = read (qd, buffer, sizeof(buffer))) > 0)
1244 if (write (fd1, buffer, i) != i) {
1252 lseek (fd1, (off_t) 0, SEEK_SET);
1256 /* dup the fd for incoming message */
1257 if ((fd2 = dup (qd)) == -1) {
1262 /* now create a FILE pointer for it */
1263 if ((qfp = fdopen (fd2, "r")) == NULL) {
1269 /* dup the fd for temporary file */
1270 if ((fd2 = dup (fd1)) == -1) {
1276 /* now create a FILE pointer for it */
1277 if ((ffp = fdopen (fd2, "r+")) == NULL) {
1285 * copy message into temporary file
1286 * and massage the headers. Save
1287 * a copy of the "From " line for later.
1289 i = strlen ("From ");
1290 while (fgets (buffer, sizeof(buffer), qfp)) {
1293 if (!strncmp (buffer, "From ", i)) {
1295 char *fp, *cp, *hp, *ep;
1297 /* get copy of envelope information ("From " line) */
1298 envelope = getcpy (buffer);
1301 /* First go ahead and put "From " line in message */
1302 fputs (buffer, ffp);
1309 * Now create a "Return-Path:" line
1310 * from the "From " line.
1312 hp = cp = strchr(fp = envelope + i, ' ');
1313 while ((hp = strchr(++hp, 'r')))
1314 if (uprf (hp, "remote from")) {
1315 hp = strrchr(hp, ' ');
1319 /* return path for UUCP style addressing */
1320 ep = strchr(++hp, '\n');
1321 snprintf (buffer, sizeof(buffer), "Return-Path: %.*s!%.*s\n",
1322 ep - hp, hp, cp - fp, fp);
1324 /* return path for standard domain addressing */
1325 snprintf (buffer, sizeof(buffer), "Return-Path: %.*s\n",
1329 /* Add Return-Path header to message */
1330 fputs (buffer, ffp);
1334 /* Put the delivery date in message */
1343 fputs (buffer, ffp);
1355 lseek (fd1, (off_t) 0, SEEK_SET);
1367 * Trim strings for pretty printing of debugging output
1373 char buffer[BUFSIZ*4];
1379 /* copy string into temp buffer */
1380 strncpy (buffer, cp, sizeof(buffer));
1383 /* skip over leading whitespace */
1384 while (isspace(*bp))
1387 /* start at the end and zap trailing whitespace */
1388 for (sp = bp + strlen(bp) - 1; sp >= bp; sp--) {
1395 /* replace remaining whitespace with spaces */
1396 for (sp = bp; *sp; sp++)
1400 /* now return a copy */
1405 * Function for printing `verbose' messages.
1409 verbose_printf (char *fmt, ...)
1414 vfprintf (stdout, fmt, ap);
1417 fflush (stdout); /* now flush output */
1422 * Function for printing `verbose' delivery
1427 adorn (char *what, char *fmt, ...)
1433 eindex = errno; /* save the errno */
1434 fprintf (stdout, ", ");
1437 vfprintf (stdout, fmt, ap);
1442 fprintf (stdout, " %s: ", what);
1443 if ((s = strerror (eindex)))
1444 fprintf (stdout, "%s", s);
1446 fprintf (stdout, "Error %d", eindex);
1449 fputc ('\n', stdout);
1455 * Function for printing `debug' messages.
1459 debug_printf (char *fmt, ...)
1464 vfprintf (stderr, fmt, ap);
1470 * Check ndbm/db file(s) to see if the Message-Id of this
1471 * message matches the Message-Id of a previous message,
1472 * so we can discard it. If it doesn't match, we add the
1473 * Message-Id of this message to the ndbm/db file.
1476 suppress_duplicates (int fd, char *file)
1478 int fd1, lockfd, state, result;
1479 char *cp, buf[BUFSIZ], name[NAMESZ];
1484 if ((fd1 = dup (fd)) == -1)
1486 if (!(in = fdopen (fd1, "r"))) {
1492 for (state = FLD;;) {
1493 state = m_getfld (state, name, buf, sizeof(buf), in);
1498 /* Search for the message ID */
1499 if (strcasecmp (name, "Message-ID")) {
1500 while (state == FLDPLUS)
1501 state = m_getfld (state, name, buf, sizeof(buf), in);
1505 cp = add (buf, NULL);
1506 while (state == FLDPLUS) {
1507 state = m_getfld (state, name, buf, sizeof(buf), in);
1510 key.dptr = trimcpy (cp);
1511 key.dsize = strlen (key.dptr) + 1;
1515 if (!(db = dbm_open (file, O_RDWR | O_CREAT, 0600))) {
1516 advise (file, "unable to perform dbm_open on");
1522 * Since it is difficult to portable lock a ndbm file,
1523 * we will open and lock the Maildelivery file instead.
1524 * This will fail if your Maildelivery file doesn't
1527 if ((lockfd = lkopen(file, O_RDWR, 0)) == -1) {
1528 advise (file, "unable to perform file locking on");
1533 value = dbm_fetch (db, key);
1536 verbose_printf ("Message-ID: %s\n already received on %s",
1540 value.dptr = ddate + sizeof("Delivery-Date:");
1541 value.dsize = strlen(value.dptr) + 1;
1542 if (dbm_store (db, key, value, DBM_INSERT))
1543 advise (file, "possibly corrupt file");
1548 lkclose(lockfd, file);