]>
diplodocus.org Git - nmh/blob - uip/slocal.c
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 HAVE_DB1_NDBM_H
42 # define UTMP_FILE _PATH_UTMP
44 # define UTMP_FILE "/etc/utmp"
48 static struct swit switches
[] = {
50 { "addr address", 0 },
56 { "sender address", 0 },
58 { "mailbox file", 0 },
60 { "home directory", -4 },
64 { "maildelivery file", 0 },
69 #define SUPPRESSDUP 10
71 #define NSUPPRESSDUP 11
72 { "nosuppressdup", 0 },
82 static int globbed
= 0; /* have we built "vars" table yet? */
83 static int parsed
= 0; /* have we built header field table yet */
84 static int utmped
= 0; /* have we scanned umtp(x) file yet */
85 static int suppressdup
= 0; /* are we suppressing duplicate messages? */
87 static int verbose
= 0;
90 static char *addr
= NULL
;
91 static char *user
= NULL
;
92 static char *info
= NULL
;
93 static char *file
= NULL
;
94 static char *sender
= NULL
;
95 static char *envelope
= NULL
; /* envelope information ("From " line) */
96 static char *mbox
= NULL
;
97 static char *home
= NULL
;
99 static struct passwd
*pw
; /* passwd file entry */
101 static char ddate
[BUFSIZ
]; /* record the delivery date */
104 static jmp_buf myctx
;
106 /* flags for pair->p_flags */
108 #define P_ADR 0x01 /* field is address */
109 #define P_HID 0x02 /* special (fake) field */
121 * Lookup table for matching fields and patterns
122 * in messages. The rest of the table is added
123 * when the message is parsed.
125 static struct pair hdrs
[NVEC
+ 1] = {
126 { "source", NULL
, P_HID
},
127 { "addr", NULL
, P_HID
},
128 { "Return-Path", NULL
, P_ADR
},
129 { "Reply-To", NULL
, P_ADR
},
130 { "From", NULL
, P_ADR
},
131 { "Sender", NULL
, P_ADR
},
132 { "To", NULL
, P_ADR
},
133 { "cc", NULL
, P_ADR
},
134 { "Resent-Reply-To", NULL
, P_ADR
},
135 { "Resent-From", NULL
, P_ADR
},
136 { "Resent-Sender", NULL
, P_ADR
},
137 { "Resent-To", NULL
, P_ADR
},
138 { "Resent-cc", NULL
, P_ADR
},
143 * The list of builtin variables to expand in a string
144 * before it is executed by the "pipe" or "qpipe" action.
146 static struct pair vars
[] = {
147 { "sender", NULL
, P_NIL
},
148 { "address", NULL
, P_NIL
},
149 { "size", NULL
, P_NIL
},
150 { "reply-to", NULL
, P_CHK
},
151 { "info", NULL
, P_NIL
},
155 extern char **environ
;
160 static int localmail (int, char *);
161 static int usr_delivery (int, char *, int);
162 static int split (char *, char **);
163 static int parse (int);
164 static void expand (char *, char *, int);
165 static void glob (int);
166 static struct pair
*lookup (struct pair
*, char *);
167 static int logged_in (void);
168 static int timely (char *, char *);
169 static int usr_file (int, char *, int);
170 static int usr_pipe (int, char *, char *, char **, int);
171 static int usr_folder (int, char *);
172 static RETSIGTYPE
alrmser (int);
173 static void get_sender (char *, char **);
174 static int copy_message (int, char *, int);
175 static void verbose_printf (char *fmt
, ...);
176 static void adorn (char *, char *, ...);
177 static void debug_printf (char *fmt
, ...);
178 static int suppress_duplicates (int, char *);
179 static char *trim (char *);
183 main (int argc
, char **argv
)
187 char *cp
, *mdlvr
= NULL
, buf
[BUFSIZ
];
188 char mailbox
[BUFSIZ
], tmpfil
[BUFSIZ
];
189 char **argp
, **arguments
;
192 setlocale(LC_ALL
, "");
194 invo_name
= r1bindex (*argv
, '/');
196 /* foil search of user profile/context */
197 if (context_foil (NULL
) == -1)
200 mts_init (invo_name
);
201 arguments
= getarguments (invo_name
, argc
, argv
, 0);
204 /* Parse arguments */
205 while ((cp
= *argp
++)) {
207 switch (smatch (++cp
, switches
)) {
209 ambigsw (cp
, switches
);
212 adios (NULL
, "-%s unknown", cp
);
215 snprintf (buf
, sizeof(buf
),
216 "%s [switches] [address info sender]", invo_name
);
217 print_help (buf
, switches
, 0);
220 print_version(invo_name
);
224 if (!(addr
= *argp
++))/* allow -xyz arguments */
225 adios (NULL
, "missing argument to %s", argp
[-2]);
228 if (!(info
= *argp
++))/* allow -xyz arguments */
229 adios (NULL
, "missing argument to %s", argp
[-2]);
232 if (!(user
= *argp
++))/* allow -xyz arguments */
233 adios (NULL
, "missing argument to %s", argp
[-2]);
236 if (!(file
= *argp
++) || *file
== '-')
237 adios (NULL
, "missing argument to %s", argp
[-2]);
240 if (!(sender
= *argp
++))/* allow -xyz arguments */
241 adios (NULL
, "missing argument to %s", argp
[-2]);
244 if (!(mbox
= *argp
++) || *mbox
== '-')
245 adios (NULL
, "missing argument to %s", argp
[-2]);
248 if (!(home
= *argp
++) || *home
== '-')
249 adios (NULL
, "missing argument to %s", argp
[-2]);
253 if (!(cp
= *argp
++) || *cp
== '-')
254 adios (NULL
, "missing argument to %s", argp
[-2]);
256 adios (NULL
, "only one maildelivery file at a time!");
279 switch (argp
- (argv
+ 1)) {
295 addr
= getusername ();
297 user
= (cp
= strchr(addr
, '.')) ? ++cp
: addr
;
298 if ((pw
= getpwnam (user
)) == NULL
)
299 adios (NULL
, "no such local user as %s", user
);
301 if (chdir (pw
->pw_dir
) == -1)
305 if (geteuid() == 0) {
307 initgroups (pw
->pw_name
, pw
->pw_gid
);
314 setbuf (stdin
, NULL
);
316 /* Record the delivery time */
317 if ((now
= dlocaltimenow ()) == NULL
)
318 adios (NULL
, "unable to ascertain local time");
319 snprintf (ddate
, sizeof(ddate
), "Delivery-Date: %s\n", dtimenow (0));
322 * Copy the message to a temporary file
327 /* getting message from file */
328 if ((tempfd
= open (file
, O_RDONLY
)) == -1)
329 adios (file
, "unable to open");
331 debug_printf ("retrieving message from file \"%s\"\n", file
);
332 if ((fd
= copy_message (tempfd
, tmpfil
, 1)) == -1)
333 adios (NULL
, "unable to create temporary file");
336 /* getting message from stdin */
338 debug_printf ("retrieving message from stdin\n");
339 if ((fd
= copy_message (fileno (stdin
), tmpfil
, 1)) == -1)
340 adios (NULL
, "unable to create temporary file");
344 debug_printf ("temporary file=\"%s\"\n", tmpfil
);
346 /* Delete the temp file now or a copy of every single message passed through
347 slocal will be left in the /tmp directory until deleted manually! This
348 unlink() used to be under an 'else' of the 'if (debug)' above, but since
349 some people like to always run slocal with -debug and log the results,
350 the /tmp directory would get choked over time. Of course, now that we
351 always delete the temp file, the "temporary file=" message above is
352 somewhat pointless -- someone watching debug output wouldn't have a
353 chance to 'tail -f' or 'ln' the temp file before it's unlinked. The best
354 thing would be to delay this unlink() until later if debug == 1, but I'll
355 leave that for someone who cares about the temp-file-accessing
356 functionality (they'll have to watch out for cases where we adios()). */
359 if (!(fp
= fdopen (fd
, "r+")))
360 adios (NULL
, "unable to access temporary file");
363 * If no sender given, extract it
364 * from envelope information. */
366 get_sender (envelope
, &sender
);
369 snprintf (mailbox
, sizeof(mailbox
), "%s/%s",
370 mmdfldir
[0] ? mmdfldir
: pw
->pw_dir
,
371 mmdflfil
[0] ? mmdflfil
: pw
->pw_name
);
378 debug_printf ("addr=\"%s\"\n", trim(addr
));
379 debug_printf ("user=\"%s\"\n", trim(user
));
380 debug_printf ("info=\"%s\"\n", trim(info
));
381 debug_printf ("sender=\"%s\"\n", trim(sender
));
382 debug_printf ("envelope=\"%s\"\n", envelope
? trim(envelope
) : "");
383 debug_printf ("mbox=\"%s\"\n", trim(mbox
));
384 debug_printf ("home=\"%s\"\n", trim(home
));
385 debug_printf ("ddate=\"%s\"\n", trim(ddate
));
386 debug_printf ("now=%02d:%02d\n\n", now
->tw_hour
, now
->tw_min
);
389 /* deliver the message */
390 status
= localmail (fd
, mdlvr
);
392 return done (status
!= -1 ? RCV_MOK
: RCV_MBX
);
397 * Main routine for delivering message.
401 localmail (int fd
, char *mdlvr
)
403 /* check if this message is a duplicate */
405 suppress_duplicates(fd
, mdlvr
? mdlvr
: ".maildelivery") == DONE
)
408 /* delivery according to personal Maildelivery file */
409 if (usr_delivery (fd
, mdlvr
? mdlvr
: ".maildelivery", 0) != -1)
412 /* delivery according to global Maildelivery file */
413 if (usr_delivery (fd
, maildelivery
, 1) != -1)
417 verbose_printf ("(delivering to standard mail spool)\n");
419 /* last resort - deliver to standard mail spool */
421 return usr_file (fd
, mbox
, MBOX_FORMAT
);
423 return usr_file (fd
, mbox
, MMDF_FORMAT
);
428 #define matches(a,b) (stringdex (b, a) >= 0)
431 * Parse the delivery file, and process incoming message.
435 usr_delivery (int fd
, char *delivery
, int su
)
437 int i
, accept
, status
, won
, vecp
, next
;
438 char *field
, *pattern
, *action
, *result
, *string
;
439 char buffer
[BUFSIZ
], tmpbuf
[BUFSIZ
];
440 char *cp
, *vec
[NVEC
];
445 /* open the delivery file */
446 if ((fp
= fopen (delivery
, "r")) == NULL
)
449 /* check if delivery file has bad ownership or permissions */
450 if (fstat (fileno (fp
), &st
) == -1
451 || (st
.st_uid
!= 0 && (su
|| st
.st_uid
!= pw
->pw_uid
))
452 || st
.st_mode
& (S_IWGRP
|S_IWOTH
)) {
454 verbose_printf ("WARNING: %s has bad ownership/modes (su=%d,uid=%d,owner=%d,mode=0%o)\n",
455 delivery
, su
, (int) pw
->pw_uid
, (int) st
.st_uid
, (int) st
.st_mode
);
463 /* read and process delivery file */
464 while (fgets (buffer
, sizeof(buffer
), fp
)) {
465 /* skip comments and empty lines */
466 if (*buffer
== '#' || *buffer
== '\n')
469 /* zap trailing newline */
470 if ((cp
= strchr(buffer
, '\n')))
473 /* split buffer into fields */
474 vecp
= split (buffer
, vec
);
476 /* check for too few fields */
479 debug_printf ("WARNING: entry with only %d fields, skipping.\n", vecp
);
484 for (i
= 0; vec
[i
]; i
++)
485 debug_printf ("vec[%d]: \"%s\"\n", i
, trim(vec
[i
]));
494 /* find out how to perform the action */
499 * If previous condition failed, don't
500 * do this - else fall through
503 continue; /* else fall */
507 * If already delivered, skip this action. Else
508 * consider delivered if action is successful.
511 continue; /* else fall */
516 * Take action, and consider delivered if
517 * action is successful.
526 * Take action, but don't consider delivered, even
527 * if action is successful
534 if (!strcasecmp (vec
[5], "select")) {
535 if (logged_in () != -1)
537 if (vecp
> 7 && timely (vec
[6], vec
[7]) == -1)
542 /* check if the field matches */
550 * "default" matches only if the message hasn't
551 * been delivered yet.
553 if (!strcasecmp (field
, "default")) {
560 /* parse message and build lookup table */
561 if (!parsed
&& parse (fd
) == -1) {
566 * find header field in lookup table, and
567 * see if the pattern matches.
569 if ((p
= lookup (hdrs
, field
)) && (p
->p_value
!= NULL
)
570 && matches (p
->p_value
, pattern
)) {
579 /* find out the action to perform */
582 /* deliver to quoted pipe */
583 if (strcasecmp (action
, "qpipe"))
584 continue; /* else fall */
586 expand (tmpbuf
, string
, fd
);
587 if (split (tmpbuf
, vec
) < 1)
589 status
= usr_pipe (fd
, tmpbuf
, vec
[0], vec
, 0);
593 /* deliver to pipe */
594 if (strcasecmp (action
, "pipe"))
595 continue; /* else fall */
599 expand (tmpbuf
, string
, fd
);
602 status
= usr_pipe (fd
, tmpbuf
, "/bin/sh", vec
+ 2, 0);
607 if (!strcasecmp (action
, "file")) {
608 status
= usr_file (fd
, string
, MBOX_FORMAT
);
611 /* deliver to nmh folder */
612 else if (strcasecmp (action
, "folder"))
613 continue; /* else fall */
615 status
= usr_folder (fd
, string
);
620 if (!strcasecmp (action
, "mmdf")) {
621 status
= usr_file (fd
, string
, MMDF_FORMAT
);
625 else if (strcasecmp (action
, "mbox"))
626 continue; /* else fall */
630 status
= usr_file (fd
, string
, MBOX_FORMAT
);
635 if (strcasecmp (action
, "destroy"))
641 if (accept
&& status
== 0)
646 return (won
? 0 : -1);
653 * Split buffer into fields (delimited by whitespace or
654 * comma's). Return the number of fields found.
658 split (char *cp
, char **vec
)
665 /* split into a maximum of NVEC fields */
666 for (i
= 0; i
<= NVEC
;) {
669 /* zap any whitespace and comma's */
670 while (isspace (*s
) || *s
== ',')
673 /* end of buffer, time to leave */
677 /* get double quote text as a single field */
679 for (vec
[i
++] = ++s
; *s
&& *s
!= '"'; s
++) {
681 * Check for escaped double quote. We need
682 * to shift the string to remove slash.
690 if (*s
== '"') /* zap trailing double quote */
695 if (*s
== QUOTE
&& *++s
!= '"')
699 /* move forward to next field delimiter */
700 while (*s
&& !isspace (*s
) && *s
!= ',')
710 * Parse the headers of a message, and build the
711 * lookup table for matching fields and patterns.
720 char name
[NAMESZ
], field
[BUFSIZ
];
727 /* get a new FILE pointer to message */
728 if ((fd1
= dup (fd
)) == -1)
730 if ((in
= fdopen (fd1
, "r")) == NULL
) {
736 /* add special entries to lookup table */
737 if ((p
= lookup (hdrs
, "source")))
738 p
->p_value
= getcpy (sender
);
739 if ((p
= lookup (hdrs
, "addr")))
740 p
->p_value
= getcpy (addr
);
743 * Scan the headers of the message and build
746 for (i
= 0, state
= FLD
;;) {
747 switch (state
= m_getfld (state
, name
, field
, sizeof(field
), in
)) {
751 lp
= add (field
, NULL
);
752 while (state
== FLDPLUS
) {
753 state
= m_getfld (state
, name
, field
, sizeof(field
), in
);
754 lp
= add (field
, lp
);
756 for (p
= hdrs
; p
->p_name
; p
++) {
757 if (!strcasecmp (p
->p_name
, name
)) {
758 if (!(p
->p_flags
& P_HID
)) {
759 if ((cp
= p
->p_value
)) {
760 if (p
->p_flags
& P_ADR
) {
761 dp
= cp
+ strlen (cp
) - 1;
764 cp
= add (",\n\t", cp
);
769 p
->p_value
= add (lp
, cp
);
775 if (p
->p_name
== NULL
&& i
< NVEC
) {
776 p
->p_name
= getcpy (name
);
793 advise (NULL
, "format error in message");
797 advise (NULL
, "internal error in m_getfld");
805 if ((p
= lookup (vars
, "reply-to"))) {
806 if ((q
= lookup (hdrs
, "reply-to")) == NULL
|| q
->p_value
== NULL
)
807 q
= lookup (hdrs
, "from");
808 p
->p_value
= getcpy (q
? q
->p_value
: "");
809 p
->p_flags
&= ~P_CHK
;
811 debug_printf ("vars[%d]: name=\"%s\" value=\"%s\"\n",
812 p
- vars
, p
->p_name
, trim(p
->p_value
));
815 for (p
= hdrs
; p
->p_name
; p
++)
816 debug_printf ("hdrs[%d]: name=\"%s\" value=\"%s\"\n",
817 p
- hdrs
, p
->p_name
, p
->p_value
? trim(p
->p_value
) : "");
828 * Expand any builtin variables such as $(sender),
829 * $(address), etc., in a string.
833 expand (char *s1
, char *s2
, int fd
)
841 while ((c
= *s2
++)) {
842 if (c
!= '$' || *s2
!= LPAREN
) {
845 for (cp
= ++s2
; *s2
&& *s2
!= RPAREN
; s2
++)
852 if ((p
= lookup (vars
, cp
))) {
853 if (!parsed
&& (p
->p_flags
& P_CHK
))
856 strcpy (s1
, p
->p_value
);
866 * Fill in the information missing from the "vars"
867 * table, which is necessary to expand any builtin
868 * variables in the string for a "pipe" or "qpipe"
882 if ((p
= lookup (vars
, "sender")))
883 p
->p_value
= getcpy (sender
);
884 if ((p
= lookup (vars
, "address")))
885 p
->p_value
= getcpy (addr
);
886 if ((p
= lookup (vars
, "size"))) {
887 snprintf (buffer
, sizeof(buffer
), "%d",
888 fstat (fd
, &st
) != -1 ? (int) st
.st_size
: 0);
889 p
->p_value
= getcpy (buffer
);
891 if ((p
= lookup (vars
, "info")))
892 p
->p_value
= getcpy (info
);
895 for (p
= vars
; p
->p_name
; p
++)
896 debug_printf ("vars[%d]: name=\"%s\" value=\"%s\"\n",
897 p
- vars
, p
->p_name
, trim(p
->p_value
));
903 * Find a matching name in a lookup table. If found,
904 * return the "pairs" entry, else return NULL.
908 lookup (struct pair
*pairs
, char *key
)
910 for (; pairs
->p_name
; pairs
++)
911 if (!strcasecmp (pairs
->p_name
, key
))
919 * Check utmp(x) file to see if user is currently
932 if ((uf
= fopen (UTMP_FILE
, "r")) == NULL
)
935 while (fread ((char *) &ut
, sizeof(ut
), 1, uf
) == 1) {
936 if (ut
.ut_name
[0] != 0
937 && strncmp (user
, ut
.ut_name
, sizeof(ut
.ut_name
)) == 0) {
941 return (utmped
= DONE
);
946 return (utmped
= NOTOK
);
950 #define check(t,a,b) if (t < a || t > b) return -1
951 #define cmpar(h1,m1,h2,m2) if (h1 < h2 || (h1 == h2 && m1 < m2)) return 0
954 timely (char *t1
, char *t2
)
956 int t1hours
, t1mins
, t2hours
, t2mins
;
958 if (sscanf (t1
, "%d:%d", &t1hours
, &t1mins
) != 2)
960 check (t1hours
, 0, 23);
961 check (t1mins
, 0, 59);
963 if (sscanf (t2
, "%d:%d", &t2hours
, &t2mins
) != 2)
965 check (t2hours
, 0, 23);
966 check (t2mins
, 0, 59);
968 cmpar (now
->tw_hour
, now
->tw_min
, t1hours
, t1mins
);
969 cmpar (t2hours
, t2mins
, now
->tw_hour
, now
->tw_min
);
976 * Deliver message by appending to a file.
980 usr_file (int fd
, char *mailbox
, int mbx_style
)
985 verbose_printf ("delivering to file \"%s\"", mailbox
);
987 if (mbx_style
== MBOX_FORMAT
) {
989 verbose_printf (" (mbox style)");
993 verbose_printf (" (mmdf style)");
997 /* open and lock the file */
998 if ((md
= mbx_open (mailbox
, mbx_style
, pw
->pw_uid
, pw
->pw_gid
, m_gmprot())) == -1) {
1000 adorn ("", "unable to open:");
1004 lseek (fd
, (off_t
) 0, SEEK_SET
);
1006 /* append message to file */
1007 if (mbx_copy (mailbox
, mbx_style
, md
, fd
, mapping
, NULL
, verbose
) == -1) {
1009 adorn ("", "error writing to:");
1013 /* close and unlock file */
1014 mbx_close (mailbox
, md
);
1017 verbose_printf (", success.\n");
1023 * Deliver message to a nmh folder.
1027 usr_folder (int fd
, char *string
)
1030 char folder
[BUFSIZ
], *vec
[3];
1032 /* get folder name ready */
1034 strncpy(folder
, string
, sizeof(folder
));
1036 snprintf(folder
, sizeof(folder
), "+%s", string
);
1039 verbose_printf ("delivering to folder \"%s\"", folder
+ 1);
1041 vec
[0] = "rcvstore";
1045 /* use rcvstore to put message in folder */
1046 status
= usr_pipe (fd
, "rcvstore", rcvstoreproc
, vec
, 1);
1050 * Currently, verbose status messages are handled by usr_pipe().
1054 verbose_printf (", success.\n");
1056 verbose_printf (", failed.\n");
1064 * Deliver message to a process.
1068 usr_pipe (int fd
, char *cmd
, char *pgm
, char **vec
, int suppress
)
1071 int i
, bytes
, seconds
, status
;
1074 if (verbose
&& !suppress
)
1075 verbose_printf ("delivering to pipe \"%s\"", cmd
);
1077 lseek (fd
, (off_t
) 0, SEEK_SET
);
1079 for (i
= 0; (child_id
= fork()) == -1 && i
< 5; i
++)
1086 adorn ("fork", "unable to");
1093 freopen ("/dev/null", "w", stdout
);
1094 freopen ("/dev/null", "w", stderr
);
1100 if ((fd
= open ("/dev/tty", O_RDWR
)) != -1) {
1101 ioctl (fd
, TIOCNOTTY
, NULL
);
1104 #endif /* TIOCNOTTY */
1106 setpgid ((pid_t
) 0, getpid ()); /* put in own process group */
1109 m_putenv ("USER", pw
->pw_name
);
1110 m_putenv ("HOME", pw
->pw_dir
);
1111 m_putenv ("SHELL", pw
->pw_shell
);
1117 /* parent process */
1118 if (!setjmp (myctx
)) {
1119 SIGNAL (SIGALRM
, alrmser
);
1120 bytes
= fstat (fd
, &st
) != -1 ? (int) st
.st_size
: 100;
1122 /* amount of time to wait depends on message size */
1124 /* give at least 5 minutes */
1126 } else if (bytes
>= 90000) {
1127 /* a half hour is long enough */
1130 seconds
= (bytes
/ 60) + 300;
1132 alarm ((unsigned int) seconds
);
1133 status
= pidwait (child_id
, 0);
1137 if (status
== RP_MOK
|| status
== RP_OK
)
1142 verbose_printf (", success.\n");
1144 if ((status
& 0xff00) == 0xff00)
1145 verbose_printf (", system error\n");
1147 pidstatus (status
, stdout
, ", failed");
1149 return (status
== 0 ? 0 : -1);
1152 * Ruthlessly kill the child and anything
1153 * else in its process group.
1155 KILLPG(child_id
, SIGKILL
);
1157 verbose_printf (", timed-out; terminated\n");
1167 #ifndef RELIABLE_SIGNALS
1168 SIGNAL (SIGALRM
, alrmser
);
1171 longjmp (myctx
, DONE
);
1176 * Get the `sender' from the envelope
1177 * information ("From " line).
1181 get_sender (char *envelope
, char **sender
)
1185 char buffer
[BUFSIZ
];
1187 if (envelope
== NULL
) {
1188 *sender
= getcpy ("");
1192 i
= strlen ("From ");
1193 strncpy (buffer
, envelope
+ i
, sizeof(buffer
));
1194 if ((cp
= strchr(buffer
, '\n'))) {
1204 for (cp
= buffer
+ strlen (buffer
) - 1; cp
>= buffer
; cp
--)
1209 *sender
= getcpy (buffer
);
1214 * Copy message into a temporary file.
1215 * While copying, it will do some header processing
1216 * including the extraction of the envelope information.
1220 copy_message (int qd
, char *tmpfil
, int fold
)
1222 int i
, first
= 1, fd1
, fd2
;
1223 char buffer
[BUFSIZ
];
1226 strcpy (tmpfil
, m_tmpfil (invo_name
));
1228 /* open temporary file to put message in */
1229 if ((fd1
= open (tmpfil
, O_RDWR
| O_CREAT
| O_TRUNC
, 0600)) == -1)
1233 while ((i
= read (qd
, buffer
, sizeof(buffer
))) > 0)
1234 if (write (fd1
, buffer
, i
) != i
) {
1242 lseek (fd1
, (off_t
) 0, SEEK_SET
);
1246 /* dup the fd for incoming message */
1247 if ((fd2
= dup (qd
)) == -1) {
1252 /* now create a FILE pointer for it */
1253 if ((qfp
= fdopen (fd2
, "r")) == NULL
) {
1259 /* dup the fd for temporary file */
1260 if ((fd2
= dup (fd1
)) == -1) {
1266 /* now create a FILE pointer for it */
1267 if ((ffp
= fdopen (fd2
, "r+")) == NULL
) {
1275 * copy message into temporary file
1276 * and massage the headers. Save
1277 * a copy of the "From " line for later.
1279 i
= strlen ("From ");
1280 while (fgets (buffer
, sizeof(buffer
), qfp
)) {
1283 if (!strncmp (buffer
, "From ", i
)) {
1285 char *fp
, *cp
, *hp
, *ep
;
1287 /* get copy of envelope information ("From " line) */
1288 envelope
= getcpy (buffer
);
1291 /* First go ahead and put "From " line in message */
1292 fputs (buffer
, ffp
);
1299 * Now create a "Return-Path:" line
1300 * from the "From " line.
1302 hp
= cp
= strchr(fp
= envelope
+ i
, ' ');
1303 while ((hp
= strchr(++hp
, 'r')))
1304 if (uprf (hp
, "remote from")) {
1305 hp
= strrchr(hp
, ' ');
1309 /* return path for UUCP style addressing */
1310 ep
= strchr(++hp
, '\n');
1311 snprintf (buffer
, sizeof(buffer
), "Return-Path: %.*s!%.*s\n",
1312 ep
- hp
, hp
, cp
- fp
, fp
);
1314 /* return path for standard domain addressing */
1315 snprintf (buffer
, sizeof(buffer
), "Return-Path: %.*s\n",
1319 /* Add Return-Path header to message */
1320 fputs (buffer
, ffp
);
1324 /* Put the delivery date in message */
1333 fputs (buffer
, ffp
);
1345 lseek (fd1
, (off_t
) 0, SEEK_SET
);
1357 * Trim strings for pretty printing of debugging output
1363 char buffer
[BUFSIZ
*4];
1369 /* copy string into temp buffer */
1370 strncpy (buffer
, cp
, sizeof(buffer
));
1373 /* skip over leading whitespace */
1374 while (isspace(*bp
))
1377 /* start at the end and zap trailing whitespace */
1378 for (sp
= bp
+ strlen(bp
) - 1; sp
>= bp
; sp
--) {
1385 /* replace remaining whitespace with spaces */
1386 for (sp
= bp
; *sp
; sp
++)
1390 /* now return a copy */
1395 * Function for printing `verbose' messages.
1399 verbose_printf (char *fmt
, ...)
1404 vfprintf (stdout
, fmt
, ap
);
1407 fflush (stdout
); /* now flush output */
1412 * Function for printing `verbose' delivery
1417 adorn (char *what
, char *fmt
, ...)
1423 eindex
= errno
; /* save the errno */
1424 fprintf (stdout
, ", ");
1427 vfprintf (stdout
, fmt
, ap
);
1432 fprintf (stdout
, " %s: ", what
);
1433 if ((s
= strerror (eindex
)))
1434 fprintf (stdout
, "%s", s
);
1436 fprintf (stdout
, "Error %d", eindex
);
1439 fputc ('\n', stdout
);
1445 * Function for printing `debug' messages.
1449 debug_printf (char *fmt
, ...)
1454 vfprintf (stderr
, fmt
, ap
);
1460 * Check ndbm/db file(s) to see if the Message-Id of this
1461 * message matches the Message-Id of a previous message,
1462 * so we can discard it. If it doesn't match, we add the
1463 * Message-Id of this message to the ndbm/db file.
1466 suppress_duplicates (int fd
, char *file
)
1468 int fd1
, lockfd
, state
, result
;
1469 char *cp
, buf
[BUFSIZ
], name
[NAMESZ
];
1474 if ((fd1
= dup (fd
)) == -1)
1476 if (!(in
= fdopen (fd1
, "r"))) {
1482 for (state
= FLD
;;) {
1483 state
= m_getfld (state
, name
, buf
, sizeof(buf
), in
);
1488 /* Search for the message ID */
1489 if (strcasecmp (name
, "Message-ID")) {
1490 while (state
== FLDPLUS
)
1491 state
= m_getfld (state
, name
, buf
, sizeof(buf
), in
);
1495 cp
= add (buf
, NULL
);
1496 while (state
== FLDPLUS
) {
1497 state
= m_getfld (state
, name
, buf
, sizeof(buf
), in
);
1500 key
.dptr
= trimcpy (cp
);
1501 key
.dsize
= strlen (key
.dptr
) + 1;
1505 if (!(db
= dbm_open (file
, O_RDWR
| O_CREAT
, 0600))) {
1506 advise (file
, "unable to perform dbm_open on");
1512 * Since it is difficult to portable lock a ndbm file,
1513 * we will open and lock the Maildelivery file instead.
1514 * This will fail if your Maildelivery file doesn't
1517 if ((lockfd
= lkopen(file
, O_RDWR
, 0)) == -1) {
1518 advise (file
, "unable to perform file locking on");
1523 value
= dbm_fetch (db
, key
);
1526 verbose_printf ("Message-ID: %s\n already received on %s",
1530 value
.dptr
= ddate
+ sizeof("Delivery-Date:");
1531 value
.dsize
= strlen(value
.dptr
) + 1;
1532 if (dbm_store (db
, key
, value
, DBM_INSERT
))
1533 advise (file
, "possibly corrupt file");
1538 lkclose(lockfd
, file
);