2 ** slocal.c -- asynchronously filter and deliver new mail
4 ** This code is Copyright (c) 2002, by the authors of nmh. See the
5 ** COPYRIGHT file in the root directory of the nmh distribution for
6 ** complete copyright information.
10 ** Under sendmail, users should add the line
12 ** "| /usr/local/nmh/lib/slocal"
14 ** to their $HOME/.forward file.
18 /* Changed to use getutent() and friends. Assumes that when getutent() exists,
19 ** a number of other things also exist. Please check.
20 ** Ruud de Rooij <ruud@ruud.org> Sun, 28 May 2000 17:28:55 +0200
24 #include <h/dropsbr.h>
25 #include <h/rcvmail.h>
26 #include <h/signals.h>
33 #include <sys/ioctl.h>
36 #ifdef INITGROUPS_HEADER
37 #include INITGROUPS_HEADER
40 ** On AIX 4.1, initgroups() is defined and even documented (giving the
41 ** parameter types as char* and int), but doesn't have a prototype in any
42 ** of the system header files. AIX 4.3, SunOS 4.1.3, and ULTRIX 4.2A have
45 extern int initgroups(char*, int);
49 ** This define is needed for Berkeley db v2 and above to
50 ** make the header file expose the 'historical' ndbm APIs.
51 ** We define it unconditionally because this is simple and
54 #define DB_DBM_HSEARCH 1
64 # define UTMP_FILE _PATH_UTMP
66 # define UTMP_FILE "/etc/utmp"
71 static struct swit switches[] = {
73 { "addr address", 0 },
79 { "sender address", 0 },
81 { "mailbox file", 0 },
83 { "home directory", -4 },
87 { "maildelivery file", 0 },
92 #define SUPPRESSDUP 10
94 #define NSUPPRESSDUP 11
95 { "nosuppressdup", 0 },
105 static int globbed = 0; /* have we built "vars" table yet? */
106 static int parsed = 0; /* have we built header field table yet */
107 static int utmped = 0; /* have we scanned umtp(x) file yet */
108 static int suppressdup = 0; /* are we suppressing duplicate messages? */
110 static int verbose = 0;
111 static int debug = 0;
113 static char *addr = NULL;
114 static char *user = NULL;
115 static char *info = NULL;
116 static char *file = NULL;
117 static char *sender = NULL;
118 static char *envelope = NULL; /* envelope information ("From " line) */
119 static char *mbox = NULL;
120 static char *home = NULL;
122 static struct passwd *pw; /* passwd file entry */
124 static char ddate[BUFSIZ]; /* record the delivery date */
127 static jmp_buf myctx;
129 /* flags for pair->p_flags */
131 #define P_ADR 0x01 /* field is address */
132 #define P_HID 0x02 /* special (fake) field */
144 ** Lookup table for matching fields and patterns
145 ** in messages. The rest of the table is added
146 ** when the message is parsed.
148 static struct pair hdrs[NVEC + 1] = {
149 { "source", NULL, P_HID },
150 { "addr", NULL, P_HID },
151 { "Return-Path", NULL, P_ADR },
152 { "Reply-To", NULL, P_ADR },
153 { "From", NULL, P_ADR },
154 { "Sender", NULL, P_ADR },
155 { "To", NULL, P_ADR },
156 { "cc", NULL, P_ADR },
157 { "Resent-Reply-To", NULL, P_ADR },
158 { "Resent-From", NULL, P_ADR },
159 { "Resent-Sender", NULL, P_ADR },
160 { "Resent-To", NULL, P_ADR },
161 { "Resent-Cc", NULL, P_ADR },
166 ** The list of builtin variables to expand in a string
167 ** before it is executed by the "pipe" or "qpipe" action.
169 static struct pair vars[] = {
170 { "sender", NULL, P_NIL },
171 { "address", NULL, P_NIL },
172 { "size", NULL, P_NIL },
173 { "reply-to", NULL, P_CHK },
174 { "info", NULL, P_NIL },
178 extern char **environ;
183 static int localmail(int, char *);
184 static int usr_delivery(int, char *, int);
185 static int split(char *, char **);
186 static int parse(int);
187 static void expand(char *, char *, int);
188 static void glob(int);
189 static struct pair *lookup(struct pair *, char *);
190 static int logged_in(void);
191 static int timely(char *, char *);
192 static int usr_file(int, char *);
193 static int usr_pipe(int, char *, char *, char **, int);
194 static int usr_folder(int, char *);
195 static RETSIGTYPE alrmser(int);
196 static void get_sender(char *, char **);
197 static int copy_message(int, char *, int);
198 static void verbose_printf(char *fmt, ...);
199 static void adorn(char *, char *, ...);
200 static void debug_printf(char *fmt, ...);
201 static int suppress_duplicates(int, char *);
202 static char *trim(char *);
206 main(int argc, char **argv)
210 char *cp, *mdlvr = NULL, buf[BUFSIZ];
211 char mailbox[BUFSIZ], tmpfil[BUFSIZ];
212 char **argp, **arguments;
215 setlocale(LC_ALL, "");
217 invo_name = mhbasename(*argv);
219 /* foil search of user profile/context */
220 if (context_foil(NULL) == -1)
224 arguments = getarguments(invo_name, argc, argv, 0);
227 /* Parse arguments */
228 while ((cp = *argp++)) {
230 switch (smatch(++cp, switches)) {
232 ambigsw(cp, switches);
235 adios(NULL, "-%s unknown", cp);
238 snprintf(buf, sizeof(buf), "%s [switches] [address info sender]", invo_name);
239 print_help(buf, switches, 0);
242 print_version(invo_name);
246 if (!(addr = *argp++))
247 /* allow -xyz arguments */
248 adios(NULL, "missing argument to %s",
252 if (!(info = *argp++))
253 /* allow -xyz arguments */
254 adios(NULL, "missing argument to %s",
258 if (!(user = *argp++))
259 /* allow -xyz arguments */
260 adios(NULL, "missing argument to %s",
264 if (!(file = *argp++) || *file == '-')
265 adios(NULL, "missing argument to %s",
269 if (!(sender = *argp++))
270 /* allow -xyz arguments */
271 adios(NULL, "missing argument to %s",
275 if (!(mbox = *argp++) || *mbox == '-')
276 adios(NULL, "missing argument to %s",
280 if (!(home = *argp++) || *home == '-')
281 adios(NULL, "missing argument to %s",
286 if (!(cp = *argp++) || *cp == '-')
287 adios(NULL, "missing argument to %s",
290 adios(NULL, "only one maildelivery file at a time!");
313 switch (argp - (argv + 1)) {
327 addr = getusername();
329 user = (cp = strchr(addr, '.')) ? ++cp : addr;
330 if ((pw = getpwnam(user)) == NULL)
331 adios(NULL, "no such local user as %s", user);
333 if (chdir(pw->pw_dir) == -1)
337 if (geteuid() == 0) {
339 initgroups(pw->pw_name, pw->pw_gid);
348 /* Record the delivery time */
349 if ((now = dlocaltimenow()) == NULL)
350 adios(NULL, "unable to ascertain local time");
351 snprintf(ddate, sizeof(ddate), "Delivery-Date: %s\n", dtimenow(0));
354 ** Copy the message to a temporary file
359 /* getting message from file */
360 if ((tempfd = open(file, O_RDONLY)) == -1)
361 adios(file, "unable to open");
363 debug_printf("retrieving message from file \"%s\"\n",
365 if ((fd = copy_message(tempfd, tmpfil, 1)) == -1)
366 adios(NULL, "unable to create temporary file");
369 /* getting message from stdin */
371 debug_printf("retrieving message from stdin\n");
372 if ((fd = copy_message(fileno(stdin), tmpfil, 1)) == -1)
373 adios(NULL, "unable to create temporary file");
377 debug_printf("temporary file=\"%s\"\n", tmpfil);
380 ** Delete the temp file now or a copy of every single message
381 ** passed through slocal will be left in the /tmp directory until
382 ** deleted manually! This unlink() used to be under an 'else'
383 ** of the 'if (debug)' above, but since some people like to
384 ** always run slocal with -debug and log the results, the /tmp
385 ** directory would get choked over time. Of course, now that
386 ** we always delete the temp file, the "temporary file=" message
387 ** above is somewhat pointless -- someone watching debug output
388 ** wouldn't have a chance to 'tail -f' or 'ln' the temp file
389 ** before it's unlinked. The best thing would be to delay this
390 ** unlink() until later if debug == 1, but I'll leave that for
391 ** someone who cares about the temp-file-accessing functionality
392 ** (they'll have to watch out for cases where we adios()).
396 if (!(fp = fdopen(fd, "r+")))
397 adios(NULL, "unable to access temporary file");
400 ** If no sender given, extract it
401 ** from envelope information.
404 get_sender(envelope, &sender);
407 snprintf(mailbox, sizeof(mailbox), "%s/%s",
408 mailspool, pw->pw_name);
415 debug_printf("addr=\"%s\"\n", trim(addr));
416 debug_printf("user=\"%s\"\n", trim(user));
417 debug_printf("info=\"%s\"\n", trim(info));
418 debug_printf("sender=\"%s\"\n", trim(sender));
419 debug_printf("envelope=\"%s\"\n",
420 envelope ? trim(envelope) : "");
421 debug_printf("mbox=\"%s\"\n", trim(mbox));
422 debug_printf("home=\"%s\"\n", trim(home));
423 debug_printf("ddate=\"%s\"\n", trim(ddate));
424 debug_printf("now=%02d:%02d\n\n", now->tw_hour, now->tw_min);
427 /* deliver the message */
428 status = localmail(fd, mdlvr);
430 done(status != -1 ? RCV_MOK : RCV_MBX);
436 ** Main routine for delivering message.
440 localmail(int fd, char *mdlvr)
442 /* check if this message is a duplicate */
443 if (suppressdup && suppress_duplicates(fd, mdlvr ?
444 mdlvr : ".maildelivery") == DONE)
447 /* delivery according to personal Maildelivery file */
448 if (usr_delivery(fd, mdlvr ? mdlvr : ".maildelivery", 0) != -1)
451 /* delivery according to global Maildelivery file */
452 if (usr_delivery(fd, maildelivery, 1) != -1)
456 verbose_printf("(delivering to standard mail spool)\n");
458 /* last resort - deliver to standard mail spool */
459 return usr_file(fd, mbox);
463 #define matches(a,b) (stringdex(b, a) >= 0)
466 ** Parse the delivery file, and process incoming message.
470 usr_delivery(int fd, char *delivery, int su)
472 int i, accept, status=1, won, vecp, next;
473 char *field, *pattern, *action, *result, *string;
474 char buffer[BUFSIZ], tmpbuf[BUFSIZ];
475 char *cp, *vec[NVEC];
480 /* open the delivery file */
481 if ((fp = fopen(delivery, "r")) == NULL)
484 /* check if delivery file has bad ownership or permissions */
485 if (fstat(fileno(fp), &st) == -1 ||
486 (st.st_uid != 0 && (su || st.st_uid != pw->pw_uid)) ||
487 st.st_mode & (S_IWGRP|S_IWOTH)) {
489 verbose_printf("WARNING: %s has bad ownership/modes (su=%d,uid=%d,owner=%d,mode=0%o)\n", delivery, su, (int) pw->pw_uid, (int) st.st_uid, (int) st.st_mode);
497 /* read and process delivery file */
498 while (fgets(buffer, sizeof(buffer), fp)) {
499 /* skip comments and empty lines */
500 if (*buffer == '#' || *buffer == '\n')
503 /* zap trailing newline */
504 if ((cp = strchr(buffer, '\n')))
507 /* split buffer into fields */
508 vecp = split(buffer, vec);
510 /* check for too few fields */
513 debug_printf("WARNING: entry with only %d fields, skipping.\n", vecp);
518 for (i = 0; vec[i]; i++)
519 debug_printf("vec[%d]: \"%s\"\n",
529 /* find out how to perform the action */
534 ** If previous condition failed, don't
535 ** do this - else fall through
538 continue; /* else fall */
542 ** If already delivered, skip this action.
543 ** Else consider delivered if action is
547 continue; /* else fall */
552 ** Take action, and consider delivered if
553 ** action is successful.
562 ** Take action, but don't consider delivered,
563 ** even if action is successful
570 if (!mh_strcasecmp(vec[5], "select")) {
571 if (logged_in() != -1)
573 if (vecp > 7 && timely(vec[6], vec[7]) == -1)
578 /* check if the field matches */
586 ** "default" matches only if the message hasn't
587 ** been delivered yet.
589 if (!mh_strcasecmp(field, "default")) {
596 /* parse message and build lookup table */
597 if (!parsed && parse(fd) == -1) {
602 ** find header field in lookup table, and
603 ** see if the pattern matches.
605 if ((p = lookup(hdrs, field)) && (p->p_value != NULL)
606 && matches(p->p_value, pattern)) {
615 /* find out the action to perform */
618 /* deliver to quoted pipe */
619 if (mh_strcasecmp(action, "qpipe"))
620 continue; /* else fall */
622 expand(tmpbuf, string, fd);
623 if (split(tmpbuf, vec) < 1)
625 status = usr_pipe(fd, tmpbuf, vec[0], vec, 0);
629 /* deliver to pipe */
630 if (mh_strcasecmp(action, "pipe"))
631 continue; /* else fall */
635 expand(tmpbuf, string, fd);
638 status = usr_pipe(fd, tmpbuf, "/bin/sh",
644 if (!mh_strcasecmp(action, "file")) {
645 status = usr_file(fd, string);
648 /* deliver to nmh folder */
649 else if (mh_strcasecmp(action, "folder"))
650 continue; /* else fall */
652 status = usr_folder(fd, string);
657 if (mh_strcasecmp(action, "mbox"))
658 continue; /* else fall */
662 status = usr_file(fd, string);
667 if (mh_strcasecmp(action, "destroy"))
674 next = 0; /* action failed, mark for 'N' result */
676 if (accept && status == 0)
681 return (won ? 0 : -1);
688 ** Split buffer into fields (delimited by whitespace or
689 ** comma's). Return the number of fields found.
693 split(char *cp, char **vec)
700 /* split into a maximum of NVEC fields */
701 for (i = 0; i <= NVEC;) {
704 /* zap any whitespace and comma's */
705 while (isspace(*s) || *s == ',')
708 /* end of buffer, time to leave */
712 /* get double quote text as a single field */
714 for (vec[i++] = ++s; *s && *s != '"'; s++) {
716 ** Check for escaped double quote. We need
717 ** to shift the string to remove slash.
725 if (*s == '"') /* zap trailing double quote */
730 if (*s == QUOTE && *++s != '"')
734 /* move forward to next field delimiter */
735 while (*s && !isspace(*s) && *s != ',')
745 ** Parse the headers of a message, and build the
746 ** lookup table for matching fields and patterns.
755 char name[NAMESZ], field[BUFSIZ];
762 /* get a new FILE pointer to message */
763 if ((fd1 = dup(fd)) == -1)
765 if ((in = fdopen(fd1, "r")) == NULL) {
771 /* add special entries to lookup table */
772 if ((p = lookup(hdrs, "source")))
773 p->p_value = getcpy(sender);
774 if ((p = lookup(hdrs, "addr")))
775 p->p_value = getcpy(addr);
778 * Scan the headers of the message and build
781 for (i = 0, state = FLD;;) {
782 switch (state = m_getfld(state, name, field, sizeof(field),
788 while (state == FLDPLUS) {
789 state = m_getfld(state, name, field,
793 for (p = hdrs; p->p_name; p++) {
794 if (!mh_strcasecmp(p->p_name, name)) {
795 if (!(p->p_flags & P_HID)) {
796 if ((cp = p->p_value)) {
797 if (p->p_flags & P_ADR) {
798 dp = cp + strlen(cp) - 1;
801 cp = add(",\n\t", cp);
806 p->p_value = add(lp, cp);
812 if (p->p_name == NULL && i < NVEC) {
813 p->p_name = getcpy(name);
830 advise(NULL, "format error in message");
834 advise(NULL, "internal error in m_getfld");
842 if ((p = lookup(vars, "reply-to"))) {
843 if ((q = lookup(hdrs, "reply-to")) == NULL ||
845 q = lookup(hdrs, "from");
846 p->p_value = getcpy(q ? q->p_value : "");
847 p->p_flags &= ~P_CHK;
849 debug_printf("vars[%d]: name=\"%s\" value=\"%s\"\n",
850 p - vars, p->p_name, trim(p->p_value));
853 for (p = hdrs; p->p_name; p++)
854 debug_printf("hdrs[%d]: name=\"%s\" value=\"%s\"\n",
856 p->p_value ? trim(p->p_value) : "");
867 ** Expand any builtin variables such as $(sender),
868 ** $(address), etc., in a string.
872 expand(char *s1, char *s2, int fd)
880 while ((c = *s2++)) {
881 if (c != '$' || *s2 != LPAREN) {
884 for (cp = ++s2; *s2 && *s2 != RPAREN; s2++)
891 if ((p = lookup(vars, cp))) {
892 if (!parsed && (p->p_flags & P_CHK))
895 strcpy(s1, p->p_value);
905 ** Fill in the information missing from the "vars"
906 ** table, which is necessary to expand any builtin
907 ** variables in the string for a "pipe" or "qpipe"
921 if ((p = lookup(vars, "sender")))
922 p->p_value = getcpy(sender);
923 if ((p = lookup(vars, "address")))
924 p->p_value = getcpy(addr);
925 if ((p = lookup(vars, "size"))) {
926 snprintf(buffer, sizeof(buffer), "%d",
927 fstat(fd, &st) != -1 ? (int) st.st_size : 0);
928 p->p_value = getcpy(buffer);
930 if ((p = lookup(vars, "info")))
931 p->p_value = getcpy(info);
934 for (p = vars; p->p_name; p++)
935 debug_printf("vars[%d]: name=\"%s\" value=\"%s\"\n",
936 p - vars, p->p_name, trim(p->p_value));
942 ** Find a matching name in a lookup table. If found,
943 ** return the "pairs" entry, else return NULL.
947 lookup(struct pair *pairs, char *key)
949 for (; pairs->p_name; pairs++)
950 if (!mh_strcasecmp(pairs->p_name, key))
958 ** Check utmp(x) file to see if user is currently
973 while ((utp = getutent()) != NULL) {
975 #ifdef HAVE_STRUCT_UTMP_UT_TYPE
976 utp->ut_type == USER_PROCESS &&
978 utp->ut_name[0] != 0 &&
979 strncmp(user, utp->ut_name,
980 sizeof(utp->ut_name)) == 0) {
984 return (utmped = DONE);
989 return (utmped = NOTOK);
1001 if ((uf = fopen(UTMP_FILE, "r")) == NULL)
1004 while (fread((char *) &ut, sizeof(ut), 1, uf) == 1) {
1005 if (ut.ut_name[0] != 0 &&
1006 strncmp(user, ut.ut_name, sizeof(ut.ut_name))
1011 return (utmped = DONE);
1016 return (utmped = NOTOK);
1020 #define check(t,a,b) if (t < a || t > b) return -1
1021 #define cmpar(h1,m1,h2,m2) if (h1 < h2 || (h1 == h2 && m1 < m2)) return 0
1024 timely(char *t1, char *t2)
1026 int t1hours, t1mins, t2hours, t2mins;
1028 if (sscanf(t1, "%d:%d", &t1hours, &t1mins) != 2)
1030 check(t1hours, 0, 23);
1031 check(t1mins, 0, 59);
1033 if (sscanf(t2, "%d:%d", &t2hours, &t2mins) != 2)
1035 check(t2hours, 0, 23);
1036 check(t2mins, 0, 59);
1038 cmpar(now->tw_hour, now->tw_min, t1hours, t1mins);
1039 cmpar(t2hours, t2mins, now->tw_hour, now->tw_min);
1046 ** Deliver message by appending to a file.
1050 usr_file(int fd, char *mailbox)
1055 verbose_printf("delivering to file \"%s\"", mailbox);
1058 verbose_printf(" (mbox style)");
1060 /* open and lock the file */
1061 if ((md = mbx_open(mailbox, pw->pw_uid, pw->pw_gid,
1062 m_gmprot())) == -1) {
1064 adorn("", "unable to open:");
1068 lseek(fd, (off_t) 0, SEEK_SET);
1070 /* append message to file */
1071 if (mbx_copy(mailbox, md, fd, NULL, verbose)
1074 adorn("", "error writing to:");
1078 /* close and unlock file */
1079 if (mbx_close(mailbox, md) == NOTOK) {
1081 adorn("", "error closing:");
1086 verbose_printf(", success.\n");
1092 ** Deliver message to a nmh folder.
1096 usr_folder(int fd, char *string)
1099 char folder[BUFSIZ], *vec[3];
1101 /* get folder name ready */
1103 strncpy(folder, string, sizeof(folder));
1105 snprintf(folder, sizeof(folder), "+%s", string);
1108 verbose_printf("delivering to folder \"%s\"", folder + 1);
1110 vec[0] = "rcvstore";
1114 /* use rcvstore to put message in folder */
1115 status = usr_pipe(fd, "rcvstore", rcvstoreproc, vec, 1);
1119 ** Currently, verbose status messages are handled by usr_pipe().
1123 verbose_printf(", success.\n");
1125 verbose_printf(", failed.\n");
1133 ** Deliver message to a process.
1137 usr_pipe(int fd, char *cmd, char *pgm, char **vec, int suppress)
1140 int i, bytes, seconds, status;
1143 if (verbose && !suppress)
1144 verbose_printf("delivering to pipe \"%s\"", cmd);
1146 lseek(fd, (off_t) 0, SEEK_SET);
1148 for (i = 0; (child_id = fork()) == -1 && i < 5; i++)
1155 adorn("fork", "unable to");
1162 freopen("/dev/null", "w", stdout);
1163 freopen("/dev/null", "w", stderr);
1169 if ((fd = open("/dev/tty", O_RDWR)) != -1) {
1170 ioctl(fd, TIOCNOTTY, NULL);
1173 #endif /* TIOCNOTTY */
1175 /* put in own process group */
1176 setpgid((pid_t) 0, getpid());
1179 m_putenv("USER", pw->pw_name);
1180 m_putenv("HOME", pw->pw_dir);
1181 m_putenv("SHELL", pw->pw_shell);
1187 /* parent process */
1188 if (!setjmp(myctx)) {
1189 SIGNAL(SIGALRM, alrmser);
1190 bytes = fstat(fd, &st) != -1 ?
1191 (int) st.st_size : 100;
1194 ** amount of time to wait depends on
1198 /* give at least 5 minutes */
1200 } else if (bytes >= 90000) {
1201 /* a half hour is long enough */
1204 seconds = (bytes / 60) + 300;
1206 alarm((unsigned int) seconds);
1207 status = pidwait(child_id, 0);
1212 verbose_printf(", success.\n");
1214 if ((status & 0xff00) == 0xff00)
1215 verbose_printf(", system error\n");
1217 pidstatus(status, stdout, ", failed");
1219 return (status == 0 ? 0 : -1);
1222 ** Ruthlessly kill the child and anything
1223 ** else in its process group.
1225 KILLPG(child_id, SIGKILL);
1227 verbose_printf(", timed-out; terminated\n");
1237 #ifndef RELIABLE_SIGNALS
1238 SIGNAL(SIGALRM, alrmser);
1241 longjmp(myctx, DONE);
1246 ** Get the `sender' from the envelope
1247 ** information ("From " line).
1251 get_sender(char *envelope, char **sender)
1255 unsigned char buffer[BUFSIZ];
1257 if (envelope == NULL) {
1258 *sender = getcpy("");
1262 i = strlen("From ");
1263 strncpy(buffer, envelope + i, sizeof(buffer));
1264 if ((cp = strchr(buffer, '\n'))) {
1274 for (cp = buffer + strlen(buffer) - 1; cp >= buffer; cp--)
1279 *sender = getcpy(buffer);
1284 ** Copy message into a temporary file.
1285 ** While copying, it will do some header processing
1286 ** including the extraction of the envelope information.
1290 copy_message(int qd, char *tmpfil, int fold)
1292 int i, first = 1, fd1, fd2;
1293 char buffer[BUFSIZ];
1297 tfile = m_mktemp2(NULL, invo_name, &fd1, NULL);
1298 if (tfile == NULL) return -1;
1300 strncpy(tmpfil, tfile, BUFSIZ);
1303 while ((i = read(qd, buffer, sizeof(buffer))) > 0)
1304 if (write(fd1, buffer, i) != i) {
1312 lseek(fd1, (off_t) 0, SEEK_SET);
1316 /* dup the fd for incoming message */
1317 if ((fd2 = dup(qd)) == -1) {
1322 /* now create a FILE pointer for it */
1323 if ((qfp = fdopen(fd2, "r")) == NULL) {
1329 /* dup the fd for temporary file */
1330 if ((fd2 = dup(fd1)) == -1) {
1336 /* now create a FILE pointer for it */
1337 if ((ffp = fdopen(fd2, "r+")) == NULL) {
1345 ** copy message into temporary file
1346 ** and massage the headers. Save
1347 ** a copy of the "From " line for later.
1349 i = strlen("From ");
1350 while (fgets(buffer, sizeof(buffer), qfp)) {
1353 if (strncmp(buffer, "From ", i)==0) {
1355 char *fp, *cp, *hp, *ep;
1358 ** get copy of envelope information
1361 envelope = getcpy(buffer);
1365 ** First go ahead and put "From " line
1375 ** Now create a "Return-Path:" line
1376 ** from the "From " line.
1378 hp = cp = strchr(fp = envelope + i, ' ');
1379 while ((hp = strchr(++hp, 'r')))
1380 if (uprf(hp, "remote from")) {
1381 hp = strrchr(hp, ' ');
1386 ** return path for UUCP style
1389 ep = strchr(++hp, '\n');
1390 snprintf(buffer, sizeof(buffer), "Return-Path: %.*s!%.*s\n", (int)(ep - hp), hp, (int)(cp - fp), fp);
1393 ** return path for standard domain
1396 snprintf(buffer, sizeof(buffer), "Return-Path: %.*s\n", (int)(cp - fp), fp);
1399 /* Add Return-Path header to message */
1404 /* Put the delivery date in message */
1425 lseek(fd1, (off_t) 0, SEEK_SET);
1437 ** Trim strings for pretty printing of debugging output
1443 char buffer[BUFSIZ*4];
1444 unsigned char *bp, *sp;
1449 /* copy string into temp buffer */
1450 strncpy(buffer, cp, sizeof(buffer));
1453 /* skip over leading whitespace */
1454 while (isspace(*bp))
1457 /* start at the end and zap trailing whitespace */
1458 for (sp = bp + strlen(bp) - 1; sp >= bp; sp--) {
1465 /* replace remaining whitespace with spaces */
1466 for (sp = bp; *sp; sp++)
1470 /* now return a copy */
1475 ** Function for printing `verbose' messages.
1479 verbose_printf(char *fmt, ...)
1484 vfprintf(stdout, fmt, ap);
1487 fflush(stdout); /* now flush output */
1492 ** Function for printing `verbose' delivery
1497 adorn(char *what, char *fmt, ...)
1503 eindex = errno; /* save the errno */
1504 fprintf(stdout, ", ");
1507 vfprintf(stdout, fmt, ap);
1512 fprintf(stdout, " %s: ", what);
1513 if ((s = strerror(eindex)))
1514 fprintf(stdout, "%s", s);
1516 fprintf(stdout, "Error %d", eindex);
1519 fputc('\n', stdout);
1525 ** Function for printing `debug' messages.
1529 debug_printf(char *fmt, ...)
1534 vfprintf(stderr, fmt, ap);
1540 ** Check ndbm/db file(s) to see if the Message-Id of this
1541 ** message matches the Message-Id of a previous message,
1542 ** so we can discard it. If it doesn't match, we add the
1543 ** Message-Id of this message to the ndbm/db file.
1546 suppress_duplicates(int fd, char *file)
1548 int fd1, lockfd, state, result;
1549 char *cp, buf[BUFSIZ], name[NAMESZ];
1554 if ((fd1 = dup(fd)) == -1)
1556 if (!(in = fdopen(fd1, "r"))) {
1562 for (state = FLD;;) {
1563 state = m_getfld(state, name, buf, sizeof(buf), in);
1568 /* Search for the message ID */
1569 if (mh_strcasecmp(name, "Message-ID")) {
1570 while (state == FLDPLUS)
1571 state = m_getfld(state, name, buf,
1577 while (state == FLDPLUS) {
1578 state = m_getfld(state, name, buf,
1582 key.dptr = trimcpy(cp);
1583 key.dsize = strlen(key.dptr) + 1;
1587 if (!(db = dbm_open(file, O_RDWR | O_CREAT, 0600))) {
1588 advise(file, "unable to perform dbm_open on");
1594 ** Since it is difficult to portable lock a ndbm
1595 ** file, we will open and lock the Maildelivery
1596 ** file instead. This will fail if your Maildelivery
1597 ** file doesn't exist.
1599 if ((lockfd = lkopen(file, O_RDWR, 0)) == -1) {
1600 advise(file, "unable to perform file locking on");
1605 value = dbm_fetch(db, key);
1608 verbose_printf("Message-ID: %s\n already received on %s", cp, value.dptr);
1611 value.dptr = ddate + sizeof("Delivery-Date:");
1612 value.dsize = strlen(value.dptr) + 1;
1613 if (dbm_store(db, key, value, DBM_INSERT))
1614 advise(file, "possibly corrupt file");
1619 lkclose(lockfd, file);