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 },
106 /* global maildelivery file */
107 char *maildelivery = NMHETCDIR"/maildelivery";
110 static int globbed = 0; /* have we built "vars" table yet? */
111 static int parsed = 0; /* have we built header field table yet */
112 static int utmped = 0; /* have we scanned umtp(x) file yet */
113 static int suppressdup = 0; /* are we suppressing duplicate messages? */
115 static int verbose = 0;
116 static int debug = 0;
118 static char *addr = NULL;
119 static char *user = NULL;
120 static char *info = NULL;
121 static char *file = NULL;
122 static char *sender = NULL;
123 static char *envelope = NULL; /* envelope information ("From " line) */
124 static char *mbox = NULL;
125 static char *home = NULL;
127 static struct passwd *pw; /* passwd file entry */
129 static char ddate[BUFSIZ]; /* record the delivery date */
132 static jmp_buf myctx;
134 /* flags for pair->p_flags */
136 #define P_ADR 0x01 /* field is address */
137 #define P_HID 0x02 /* special (fake) field */
149 ** Lookup table for matching fields and patterns
150 ** in messages. The rest of the table is added
151 ** when the message is parsed.
153 static struct pair hdrs[NVEC + 1] = {
154 { "source", NULL, P_HID },
155 { "addr", NULL, P_HID },
156 { "Return-Path", NULL, P_ADR },
157 { "Reply-To", NULL, P_ADR },
158 { "From", NULL, P_ADR },
159 { "Sender", NULL, P_ADR },
160 { "To", NULL, P_ADR },
161 { "cc", NULL, P_ADR },
162 { "Resent-Reply-To", NULL, P_ADR },
163 { "Resent-From", NULL, P_ADR },
164 { "Resent-Sender", NULL, P_ADR },
165 { "Resent-To", NULL, P_ADR },
166 { "Resent-Cc", NULL, P_ADR },
171 ** The list of builtin variables to expand in a string
172 ** before it is executed by the "pipe" or "qpipe" action.
174 static struct pair vars[] = {
175 { "sender", NULL, P_NIL },
176 { "address", NULL, P_NIL },
177 { "size", NULL, P_NIL },
178 { "reply-to", NULL, P_CHK },
179 { "info", NULL, P_NIL },
183 extern char **environ;
188 static int localmail(int, char *);
189 static int usr_delivery(int, char *, int);
190 static int split(char *, char **);
191 static int parse(int);
192 static void expand(char *, char *, int);
193 static void glob(int);
194 static struct pair *lookup(struct pair *, char *);
195 static int logged_in(void);
196 static int timely(char *, char *);
197 static int usr_file(int, char *);
198 static int usr_pipe(int, char *, char *, char **, int);
199 static int usr_folder(int, char *);
200 static RETSIGTYPE alrmser(int);
201 static void get_sender(char *, char **);
202 static int copy_message(int, char *, int);
203 static void verbose_printf(char *fmt, ...);
204 static void adorn(char *, char *, ...);
205 static void debug_printf(char *fmt, ...);
206 static int suppress_duplicates(int, char *);
207 static char *trim(char *);
211 main(int argc, char **argv)
215 char *cp, *mdlvr = NULL, buf[BUFSIZ];
216 char mailbox[BUFSIZ], tmpfil[BUFSIZ];
217 char **argp, **arguments;
220 setlocale(LC_ALL, "");
222 invo_name = mhbasename(*argv);
224 /* foil search of user profile/context */
225 if (context_foil(NULL) == -1)
228 arguments = getarguments(invo_name, argc, argv, 0);
231 /* Parse arguments */
232 while ((cp = *argp++)) {
234 switch (smatch(++cp, switches)) {
236 ambigsw(cp, switches);
239 adios(NULL, "-%s unknown", cp);
242 snprintf(buf, sizeof(buf), "%s [switches] [address info sender]", invo_name);
243 print_help(buf, switches, 0);
246 print_version(invo_name);
250 if (!(addr = *argp++))
251 /* allow -xyz arguments */
252 adios(NULL, "missing argument to %s",
256 if (!(info = *argp++))
257 /* allow -xyz arguments */
258 adios(NULL, "missing argument to %s",
262 if (!(user = *argp++))
263 /* allow -xyz arguments */
264 adios(NULL, "missing argument to %s",
268 if (!(file = *argp++) || *file == '-')
269 adios(NULL, "missing argument to %s",
273 if (!(sender = *argp++))
274 /* allow -xyz arguments */
275 adios(NULL, "missing argument to %s",
279 if (!(mbox = *argp++) || *mbox == '-')
280 adios(NULL, "missing argument to %s",
284 if (!(home = *argp++) || *home == '-')
285 adios(NULL, "missing argument to %s",
290 if (!(cp = *argp++) || *cp == '-')
291 adios(NULL, "missing argument to %s",
294 adios(NULL, "only one maildelivery file at a time!");
317 switch (argp - (argv + 1)) {
331 addr = getusername();
333 user = (cp = strchr(addr, '.')) ? ++cp : addr;
334 if ((pw = getpwnam(user)) == NULL)
335 adios(NULL, "no such local user as %s", user);
337 if (chdir(pw->pw_dir) == -1)
341 if (geteuid() == 0) {
343 initgroups(pw->pw_name, pw->pw_gid);
352 /* Record the delivery time */
353 if ((now = dlocaltimenow()) == NULL)
354 adios(NULL, "unable to ascertain local time");
355 snprintf(ddate, sizeof(ddate), "Delivery-Date: %s\n", dtimenow(0));
358 ** Copy the message to a temporary file
363 /* getting message from file */
364 if ((tempfd = open(file, O_RDONLY)) == -1)
365 adios(file, "unable to open");
367 debug_printf("retrieving message from file \"%s\"\n",
369 if ((fd = copy_message(tempfd, tmpfil, 1)) == -1)
370 adios(NULL, "unable to create temporary file");
373 /* getting message from stdin */
375 debug_printf("retrieving message from stdin\n");
376 if ((fd = copy_message(fileno(stdin), tmpfil, 1)) == -1)
377 adios(NULL, "unable to create temporary file");
381 debug_printf("temporary file=\"%s\"\n", tmpfil);
384 ** Delete the temp file now or a copy of every single message
385 ** passed through slocal will be left in the /tmp directory until
386 ** deleted manually! This unlink() used to be under an 'else'
387 ** of the 'if (debug)' above, but since some people like to
388 ** always run slocal with -debug and log the results, the /tmp
389 ** directory would get choked over time. Of course, now that
390 ** we always delete the temp file, the "temporary file=" message
391 ** above is somewhat pointless -- someone watching debug output
392 ** wouldn't have a chance to 'tail -f' or 'ln' the temp file
393 ** before it's unlinked. The best thing would be to delay this
394 ** unlink() until later if debug == 1, but I'll leave that for
395 ** someone who cares about the temp-file-accessing functionality
396 ** (they'll have to watch out for cases where we adios()).
400 if (!(fp = fdopen(fd, "r+")))
401 adios(NULL, "unable to access temporary file");
404 ** If no sender given, extract it
405 ** from envelope information.
408 get_sender(envelope, &sender);
411 snprintf(mailbox, sizeof(mailbox), "%s/%s",
412 mailspool, pw->pw_name);
419 debug_printf("addr=\"%s\"\n", trim(addr));
420 debug_printf("user=\"%s\"\n", trim(user));
421 debug_printf("info=\"%s\"\n", trim(info));
422 debug_printf("sender=\"%s\"\n", trim(sender));
423 debug_printf("envelope=\"%s\"\n",
424 envelope ? trim(envelope) : "");
425 debug_printf("mbox=\"%s\"\n", trim(mbox));
426 debug_printf("home=\"%s\"\n", trim(home));
427 debug_printf("ddate=\"%s\"\n", trim(ddate));
428 debug_printf("now=%02d:%02d\n\n", now->tw_hour, now->tw_min);
431 /* deliver the message */
432 status = localmail(fd, mdlvr);
434 done(status != -1 ? RCV_MOK : RCV_MBX);
440 ** Main routine for delivering message.
444 localmail(int fd, char *mdlvr)
446 /* check if this message is a duplicate */
447 if (suppressdup && suppress_duplicates(fd, mdlvr ?
448 mdlvr : ".maildelivery") == DONE)
451 /* delivery according to personal Maildelivery file */
452 if (usr_delivery(fd, mdlvr ? mdlvr : ".maildelivery", 0) != -1)
455 /* delivery according to global Maildelivery file */
456 if (usr_delivery(fd, maildelivery, 1) != -1)
460 verbose_printf("(delivering to standard mail spool)\n");
462 /* last resort - deliver to standard mail spool */
463 return usr_file(fd, mbox);
467 #define matches(a,b) (stringdex(b, a) >= 0)
470 ** Parse the delivery file, and process incoming message.
474 usr_delivery(int fd, char *delivery, int su)
476 int i, accept, status=1, won, vecp, next;
477 char *field, *pattern, *action, *result, *string;
478 char buffer[BUFSIZ], tmpbuf[BUFSIZ];
479 char *cp, *vec[NVEC];
484 /* open the delivery file */
485 if ((fp = fopen(delivery, "r")) == NULL)
488 /* check if delivery file has bad ownership or permissions */
489 if (fstat(fileno(fp), &st) == -1 ||
490 (st.st_uid != 0 && (su || st.st_uid != pw->pw_uid)) ||
491 st.st_mode & (S_IWGRP|S_IWOTH)) {
493 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);
501 /* read and process delivery file */
502 while (fgets(buffer, sizeof(buffer), fp)) {
503 /* skip comments and empty lines */
504 if (*buffer == '#' || *buffer == '\n')
507 /* zap trailing newline */
508 if ((cp = strchr(buffer, '\n')))
511 /* split buffer into fields */
512 vecp = split(buffer, vec);
514 /* check for too few fields */
517 debug_printf("WARNING: entry with only %d fields, skipping.\n", vecp);
522 for (i = 0; vec[i]; i++)
523 debug_printf("vec[%d]: \"%s\"\n",
533 /* find out how to perform the action */
538 ** If previous condition failed, don't
539 ** do this - else fall through
542 continue; /* else fall */
546 ** If already delivered, skip this action.
547 ** Else consider delivered if action is
551 continue; /* else fall */
556 ** Take action, and consider delivered if
557 ** action is successful.
566 ** Take action, but don't consider delivered,
567 ** even if action is successful
574 if (!mh_strcasecmp(vec[5], "select")) {
575 if (logged_in() != -1)
577 if (vecp > 7 && timely(vec[6], vec[7]) == -1)
582 /* check if the field matches */
590 ** "default" matches only if the message hasn't
591 ** been delivered yet.
593 if (!mh_strcasecmp(field, "default")) {
600 /* parse message and build lookup table */
601 if (!parsed && parse(fd) == -1) {
606 ** find header field in lookup table, and
607 ** see if the pattern matches.
609 if ((p = lookup(hdrs, field)) && (p->p_value != NULL)
610 && matches(p->p_value, pattern)) {
619 /* find out the action to perform */
622 /* deliver to quoted pipe */
623 if (mh_strcasecmp(action, "qpipe"))
624 continue; /* else fall */
626 expand(tmpbuf, string, fd);
627 if (split(tmpbuf, vec) < 1)
629 status = usr_pipe(fd, tmpbuf, vec[0], vec, 0);
633 /* deliver to pipe */
634 if (mh_strcasecmp(action, "pipe"))
635 continue; /* else fall */
639 expand(tmpbuf, string, fd);
642 status = usr_pipe(fd, tmpbuf, "/bin/sh",
648 if (!mh_strcasecmp(action, "file")) {
649 status = usr_file(fd, string);
652 /* deliver to nmh folder */
653 else if (mh_strcasecmp(action, "folder"))
654 continue; /* else fall */
656 status = usr_folder(fd, string);
661 if (mh_strcasecmp(action, "mbox"))
662 continue; /* else fall */
666 status = usr_file(fd, string);
671 if (mh_strcasecmp(action, "destroy"))
678 next = 0; /* action failed, mark for 'N' result */
680 if (accept && status == 0)
685 return (won ? 0 : -1);
692 ** Split buffer into fields (delimited by whitespace or
693 ** comma's). Return the number of fields found.
697 split(char *cp, char **vec)
704 /* split into a maximum of NVEC fields */
705 for (i = 0; i <= NVEC;) {
708 /* zap any whitespace and comma's */
709 while (isspace(*s) || *s == ',')
712 /* end of buffer, time to leave */
716 /* get double quote text as a single field */
718 for (vec[i++] = ++s; *s && *s != '"'; s++) {
720 ** Check for escaped double quote. We need
721 ** to shift the string to remove slash.
729 if (*s == '"') /* zap trailing double quote */
734 if (*s == QUOTE && *++s != '"')
738 /* move forward to next field delimiter */
739 while (*s && !isspace(*s) && *s != ',')
749 ** Parse the headers of a message, and build the
750 ** lookup table for matching fields and patterns.
759 char name[NAMESZ], field[BUFSIZ];
766 /* get a new FILE pointer to message */
767 if ((fd1 = dup(fd)) == -1)
769 if ((in = fdopen(fd1, "r")) == NULL) {
775 /* add special entries to lookup table */
776 if ((p = lookup(hdrs, "source")))
777 p->p_value = getcpy(sender);
778 if ((p = lookup(hdrs, "addr")))
779 p->p_value = getcpy(addr);
782 * Scan the headers of the message and build
785 for (i = 0, state = FLD;;) {
786 switch (state = m_getfld(state, name, field, sizeof(field),
792 while (state == FLDPLUS) {
793 state = m_getfld(state, name, field,
797 for (p = hdrs; p->p_name; p++) {
798 if (!mh_strcasecmp(p->p_name, name)) {
799 if (!(p->p_flags & P_HID)) {
800 if ((cp = p->p_value)) {
801 if (p->p_flags & P_ADR) {
802 dp = cp + strlen(cp) - 1;
805 cp = add(",\n\t", cp);
810 p->p_value = add(lp, cp);
816 if (p->p_name == NULL && i < NVEC) {
817 p->p_name = getcpy(name);
834 advise(NULL, "format error in message");
838 advise(NULL, "internal error in m_getfld");
846 if ((p = lookup(vars, "reply-to"))) {
847 if ((q = lookup(hdrs, "reply-to")) == NULL ||
849 q = lookup(hdrs, "from");
850 p->p_value = getcpy(q ? q->p_value : "");
851 p->p_flags &= ~P_CHK;
853 debug_printf("vars[%d]: name=\"%s\" value=\"%s\"\n",
854 p - vars, p->p_name, trim(p->p_value));
857 for (p = hdrs; p->p_name; p++)
858 debug_printf("hdrs[%d]: name=\"%s\" value=\"%s\"\n",
860 p->p_value ? trim(p->p_value) : "");
871 ** Expand any builtin variables such as $(sender),
872 ** $(address), etc., in a string.
876 expand(char *s1, char *s2, int fd)
884 while ((c = *s2++)) {
885 if (c != '$' || *s2 != LPAREN) {
888 for (cp = ++s2; *s2 && *s2 != RPAREN; s2++)
895 if ((p = lookup(vars, cp))) {
896 if (!parsed && (p->p_flags & P_CHK))
899 strcpy(s1, p->p_value);
909 ** Fill in the information missing from the "vars"
910 ** table, which is necessary to expand any builtin
911 ** variables in the string for a "pipe" or "qpipe"
925 if ((p = lookup(vars, "sender")))
926 p->p_value = getcpy(sender);
927 if ((p = lookup(vars, "address")))
928 p->p_value = getcpy(addr);
929 if ((p = lookup(vars, "size"))) {
930 snprintf(buffer, sizeof(buffer), "%d",
931 fstat(fd, &st) != -1 ? (int) st.st_size : 0);
932 p->p_value = getcpy(buffer);
934 if ((p = lookup(vars, "info")))
935 p->p_value = getcpy(info);
938 for (p = vars; p->p_name; p++)
939 debug_printf("vars[%d]: name=\"%s\" value=\"%s\"\n",
940 p - vars, p->p_name, trim(p->p_value));
946 ** Find a matching name in a lookup table. If found,
947 ** return the "pairs" entry, else return NULL.
951 lookup(struct pair *pairs, char *key)
953 for (; pairs->p_name; pairs++)
954 if (!mh_strcasecmp(pairs->p_name, key))
962 ** Check utmp(x) file to see if user is currently
977 while ((utp = getutent()) != NULL) {
979 #ifdef HAVE_STRUCT_UTMP_UT_TYPE
980 utp->ut_type == USER_PROCESS &&
982 utp->ut_name[0] != 0 &&
983 strncmp(user, utp->ut_name,
984 sizeof(utp->ut_name)) == 0) {
988 return (utmped = DONE);
993 return (utmped = NOTOK);
1005 if ((uf = fopen(UTMP_FILE, "r")) == NULL)
1008 while (fread((char *) &ut, sizeof(ut), 1, uf) == 1) {
1009 if (ut.ut_name[0] != 0 &&
1010 strncmp(user, ut.ut_name, sizeof(ut.ut_name))
1015 return (utmped = DONE);
1020 return (utmped = NOTOK);
1024 #define check(t,a,b) if (t < a || t > b) return -1
1025 #define cmpar(h1,m1,h2,m2) if (h1 < h2 || (h1 == h2 && m1 < m2)) return 0
1028 timely(char *t1, char *t2)
1030 int t1hours, t1mins, t2hours, t2mins;
1032 if (sscanf(t1, "%d:%d", &t1hours, &t1mins) != 2)
1034 check(t1hours, 0, 23);
1035 check(t1mins, 0, 59);
1037 if (sscanf(t2, "%d:%d", &t2hours, &t2mins) != 2)
1039 check(t2hours, 0, 23);
1040 check(t2mins, 0, 59);
1042 cmpar(now->tw_hour, now->tw_min, t1hours, t1mins);
1043 cmpar(t2hours, t2mins, now->tw_hour, now->tw_min);
1050 ** Deliver message by appending to a file.
1054 usr_file(int fd, char *mailbox)
1059 verbose_printf("delivering to file \"%s\"", mailbox);
1062 verbose_printf(" (mbox style)");
1064 /* open and lock the file */
1065 if ((md = mbx_open(mailbox, pw->pw_uid, pw->pw_gid,
1066 m_gmprot())) == -1) {
1068 adorn("", "unable to open:");
1072 lseek(fd, (off_t) 0, SEEK_SET);
1074 /* append message to file */
1075 if (mbx_copy(mailbox, md, fd, verbose) == -1) {
1077 adorn("", "error writing to:");
1081 /* close and unlock file */
1082 if (mbx_close(mailbox, md) == NOTOK) {
1084 adorn("", "error closing:");
1089 verbose_printf(", success.\n");
1095 ** Deliver message to a nmh folder.
1099 usr_folder(int fd, char *string)
1102 char folder[BUFSIZ], *vec[3];
1104 /* get folder name ready */
1106 strncpy(folder, string, sizeof(folder));
1108 snprintf(folder, sizeof(folder), "+%s", string);
1111 verbose_printf("delivering to folder \"%s\"", folder + 1);
1113 vec[0] = "rcvstore";
1117 /* use rcvstore to put message in folder */
1118 status = usr_pipe(fd, "rcvstore", rcvstoreproc, vec, 1);
1122 ** Currently, verbose status messages are handled by usr_pipe().
1126 verbose_printf(", success.\n");
1128 verbose_printf(", failed.\n");
1136 ** Deliver message to a process.
1140 usr_pipe(int fd, char *cmd, char *pgm, char **vec, int suppress)
1143 int i, bytes, seconds, status;
1146 if (verbose && !suppress)
1147 verbose_printf("delivering to pipe \"%s\"", cmd);
1149 lseek(fd, (off_t) 0, SEEK_SET);
1151 for (i = 0; (child_id = fork()) == -1 && i < 5; i++)
1158 adorn("fork", "unable to");
1165 freopen("/dev/null", "w", stdout);
1166 freopen("/dev/null", "w", stderr);
1172 if ((fd = open("/dev/tty", O_RDWR)) != -1) {
1173 ioctl(fd, TIOCNOTTY, NULL);
1176 #endif /* TIOCNOTTY */
1178 /* put in own process group */
1179 setpgid((pid_t) 0, getpid());
1182 m_putenv("USER", pw->pw_name);
1183 m_putenv("HOME", pw->pw_dir);
1184 m_putenv("SHELL", pw->pw_shell);
1190 /* parent process */
1191 if (!setjmp(myctx)) {
1192 SIGNAL(SIGALRM, alrmser);
1193 bytes = fstat(fd, &st) != -1 ?
1194 (int) st.st_size : 100;
1197 ** amount of time to wait depends on
1201 /* give at least 5 minutes */
1203 } else if (bytes >= 90000) {
1204 /* a half hour is long enough */
1207 seconds = (bytes / 60) + 300;
1209 alarm((unsigned int) seconds);
1210 status = pidwait(child_id, 0);
1215 verbose_printf(", success.\n");
1217 if ((status & 0xff00) == 0xff00)
1218 verbose_printf(", system error\n");
1220 pidstatus(status, stdout, ", failed");
1222 return (status == 0 ? 0 : -1);
1225 ** Ruthlessly kill the child and anything
1226 ** else in its process group.
1228 KILLPG(child_id, SIGKILL);
1230 verbose_printf(", timed-out; terminated\n");
1240 #ifndef RELIABLE_SIGNALS
1241 SIGNAL(SIGALRM, alrmser);
1244 longjmp(myctx, DONE);
1249 ** Get the `sender' from the envelope
1250 ** information ("From " line).
1254 get_sender(char *envelope, char **sender)
1258 unsigned char buffer[BUFSIZ];
1260 if (envelope == NULL) {
1261 *sender = getcpy("");
1265 i = strlen("From ");
1266 strncpy(buffer, envelope + i, sizeof(buffer));
1267 if ((cp = strchr(buffer, '\n'))) {
1277 for (cp = buffer + strlen(buffer) - 1; cp >= buffer; cp--)
1282 *sender = getcpy(buffer);
1287 ** Copy message into a temporary file.
1288 ** While copying, it will do some header processing
1289 ** including the extraction of the envelope information.
1293 copy_message(int qd, char *tmpfil, int fold)
1295 int i, first = 1, fd1, fd2;
1296 char buffer[BUFSIZ];
1300 tfile = m_mktemp2(NULL, invo_name, &fd1, NULL);
1301 if (tfile == NULL) return -1;
1303 strncpy(tmpfil, tfile, BUFSIZ);
1306 while ((i = read(qd, buffer, sizeof(buffer))) > 0)
1307 if (write(fd1, buffer, i) != i) {
1315 lseek(fd1, (off_t) 0, SEEK_SET);
1319 /* dup the fd for incoming message */
1320 if ((fd2 = dup(qd)) == -1) {
1325 /* now create a FILE pointer for it */
1326 if ((qfp = fdopen(fd2, "r")) == NULL) {
1332 /* dup the fd for temporary file */
1333 if ((fd2 = dup(fd1)) == -1) {
1339 /* now create a FILE pointer for it */
1340 if ((ffp = fdopen(fd2, "r+")) == NULL) {
1348 ** copy message into temporary file
1349 ** and massage the headers. Save
1350 ** a copy of the "From " line for later.
1352 i = strlen("From ");
1353 while (fgets(buffer, sizeof(buffer), qfp)) {
1356 if (strncmp(buffer, "From ", i)==0) {
1358 char *fp, *cp, *hp, *ep;
1361 ** get copy of envelope information
1364 envelope = getcpy(buffer);
1368 ** First go ahead and put "From " line
1378 ** Now create a "Return-Path:" line
1379 ** from the "From " line.
1381 hp = cp = strchr(fp = envelope + i, ' ');
1382 while ((hp = strchr(++hp, 'r')))
1383 if (uprf(hp, "remote from")) {
1384 hp = strrchr(hp, ' ');
1389 ** return path for UUCP style
1392 ep = strchr(++hp, '\n');
1393 snprintf(buffer, sizeof(buffer), "Return-Path: %.*s!%.*s\n", (int)(ep - hp), hp, (int)(cp - fp), fp);
1396 ** return path for standard domain
1399 snprintf(buffer, sizeof(buffer), "Return-Path: %.*s\n", (int)(cp - fp), fp);
1402 /* Add Return-Path header to message */
1407 /* Put the delivery date in message */
1428 lseek(fd1, (off_t) 0, SEEK_SET);
1440 ** Trim strings for pretty printing of debugging output
1446 char buffer[BUFSIZ*4];
1447 unsigned char *bp, *sp;
1452 /* copy string into temp buffer */
1453 strncpy(buffer, cp, sizeof(buffer));
1456 /* skip over leading whitespace */
1457 while (isspace(*bp))
1460 /* start at the end and zap trailing whitespace */
1461 for (sp = bp + strlen(bp) - 1; sp >= bp; sp--) {
1468 /* replace remaining whitespace with spaces */
1469 for (sp = bp; *sp; sp++)
1473 /* now return a copy */
1478 ** Function for printing `verbose' messages.
1482 verbose_printf(char *fmt, ...)
1487 vfprintf(stdout, fmt, ap);
1490 fflush(stdout); /* now flush output */
1495 ** Function for printing `verbose' delivery
1500 adorn(char *what, char *fmt, ...)
1506 eindex = errno; /* save the errno */
1507 fprintf(stdout, ", ");
1510 vfprintf(stdout, fmt, ap);
1515 fprintf(stdout, " %s: ", what);
1516 if ((s = strerror(eindex)))
1517 fprintf(stdout, "%s", s);
1519 fprintf(stdout, "Error %d", eindex);
1522 fputc('\n', stdout);
1528 ** Function for printing `debug' messages.
1532 debug_printf(char *fmt, ...)
1537 vfprintf(stderr, fmt, ap);
1543 ** Check ndbm/db file(s) to see if the Message-Id of this
1544 ** message matches the Message-Id of a previous message,
1545 ** so we can discard it. If it doesn't match, we add the
1546 ** Message-Id of this message to the ndbm/db file.
1549 suppress_duplicates(int fd, char *file)
1551 int fd1, lockfd, state, result;
1552 char *cp, buf[BUFSIZ], name[NAMESZ];
1557 if ((fd1 = dup(fd)) == -1)
1559 if (!(in = fdopen(fd1, "r"))) {
1565 for (state = FLD;;) {
1566 state = m_getfld(state, name, buf, sizeof(buf), in);
1571 /* Search for the message ID */
1572 if (mh_strcasecmp(name, "Message-ID")) {
1573 while (state == FLDPLUS)
1574 state = m_getfld(state, name, buf,
1580 while (state == FLDPLUS) {
1581 state = m_getfld(state, name, buf,
1585 key.dptr = trimcpy(cp);
1586 key.dsize = strlen(key.dptr) + 1;
1590 if (!(db = dbm_open(file, O_RDWR | O_CREAT, 0600))) {
1591 advise(file, "unable to perform dbm_open on");
1597 ** Since it is difficult to portable lock a ndbm
1598 ** file, we will open and lock the Maildelivery
1599 ** file instead. This will fail if your Maildelivery
1600 ** file doesn't exist.
1602 if ((lockfd = lkopen(file, O_RDWR, 0)) == -1) {
1603 advise(file, "unable to perform file locking on");
1608 value = dbm_fetch(db, key);
1611 verbose_printf("Message-ID: %s\n already received on %s", cp, value.dptr);
1614 value.dptr = ddate + sizeof("Delivery-Date:");
1615 value.dsize = strlen(value.dptr) + 1;
1616 if (dbm_store(db, key, value, DBM_INSERT))
1617 advise(file, "possibly corrupt file");
1622 lkclose(lockfd, file);