]>
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>
33 #ifdef HAVE_DB1_NDBM_H
41 extern int initgroups(char*, int); /* def'd in libc.a but no .h on AIX 4.1 */
45 # define UTMP_FILE _PATH_UTMP
47 # define UTMP_FILE "/etc/utmp"
51 static struct swit switches
[] = {
53 { "addr address", 0 },
59 { "sender address", 0 },
61 { "mailbox file", 0 },
63 { "home directory", -4 },
67 { "maildelivery file", 0 },
72 #define SUPPRESSDUP 10
74 #define NSUPPRESSDUP 11
75 { "nosuppressdup", 0 },
85 static int globbed
= 0; /* have we built "vars" table yet? */
86 static int parsed
= 0; /* have we built header field table yet */
87 static int utmped
= 0; /* have we scanned umtp(x) file yet */
88 static int suppressdup
= 0; /* are we suppressing duplicate messages? */
90 static int verbose
= 0;
93 static char *addr
= NULL
;
94 static char *user
= NULL
;
95 static char *info
= NULL
;
96 static char *file
= NULL
;
97 static char *sender
= NULL
;
98 static char *envelope
= NULL
; /* envelope information ("From " line) */
99 static char *mbox
= NULL
;
100 static char *home
= NULL
;
102 static struct passwd
*pw
; /* passwd file entry */
104 static char ddate
[BUFSIZ
]; /* record the delivery date */
107 static jmp_buf myctx
;
109 /* flags for pair->p_flags */
111 #define P_ADR 0x01 /* field is address */
112 #define P_HID 0x02 /* special (fake) field */
124 * Lookup table for matching fields and patterns
125 * in messages. The rest of the table is added
126 * when the message is parsed.
128 static struct pair hdrs
[NVEC
+ 1] = {
129 { "source", NULL
, P_HID
},
130 { "addr", NULL
, P_HID
},
131 { "Return-Path", NULL
, P_ADR
},
132 { "Reply-To", NULL
, P_ADR
},
133 { "From", NULL
, P_ADR
},
134 { "Sender", NULL
, P_ADR
},
135 { "To", NULL
, P_ADR
},
136 { "cc", NULL
, P_ADR
},
137 { "Resent-Reply-To", NULL
, P_ADR
},
138 { "Resent-From", NULL
, P_ADR
},
139 { "Resent-Sender", NULL
, P_ADR
},
140 { "Resent-To", NULL
, P_ADR
},
141 { "Resent-cc", NULL
, P_ADR
},
146 * The list of builtin variables to expand in a string
147 * before it is executed by the "pipe" or "qpipe" action.
149 static struct pair vars
[] = {
150 { "sender", NULL
, P_NIL
},
151 { "address", NULL
, P_NIL
},
152 { "size", NULL
, P_NIL
},
153 { "reply-to", NULL
, P_CHK
},
154 { "info", NULL
, P_NIL
},
158 extern char **environ
;
163 static int localmail (int, char *);
164 static int usr_delivery (int, char *, int);
165 static int split (char *, char **);
166 static int parse (int);
167 static void expand (char *, char *, int);
168 static void glob (int);
169 static struct pair
*lookup (struct pair
*, char *);
170 static int logged_in (void);
171 static int timely (char *, char *);
172 static int usr_file (int, char *, int);
173 static int usr_pipe (int, char *, char *, char **, int);
174 static int usr_folder (int, char *);
175 static RETSIGTYPE
alrmser (int);
176 static void get_sender (char *, char **);
177 static int copy_message (int, char *, int);
178 static void verbose_printf (char *fmt
, ...);
179 static void adorn (char *, char *, ...);
180 static void debug_printf (char *fmt
, ...);
181 static int suppress_duplicates (int, char *);
182 static char *trim (char *);
186 main (int argc
, char **argv
)
190 char *cp
, *mdlvr
= NULL
, buf
[BUFSIZ
];
191 char mailbox
[BUFSIZ
], tmpfil
[BUFSIZ
];
192 char **argp
, **arguments
;
195 setlocale(LC_ALL
, "");
197 invo_name
= r1bindex (*argv
, '/');
199 /* foil search of user profile/context */
200 if (context_foil (NULL
) == -1)
203 mts_init (invo_name
);
204 arguments
= getarguments (invo_name
, argc
, argv
, 0);
207 /* Parse arguments */
208 while ((cp
= *argp
++)) {
210 switch (smatch (++cp
, switches
)) {
212 ambigsw (cp
, switches
);
215 adios (NULL
, "-%s unknown", cp
);
218 snprintf (buf
, sizeof(buf
),
219 "%s [switches] [address info sender]", invo_name
);
220 print_help (buf
, switches
, 0);
223 print_version(invo_name
);
227 if (!(addr
= *argp
++))/* allow -xyz arguments */
228 adios (NULL
, "missing argument to %s", argp
[-2]);
231 if (!(info
= *argp
++))/* allow -xyz arguments */
232 adios (NULL
, "missing argument to %s", argp
[-2]);
235 if (!(user
= *argp
++))/* allow -xyz arguments */
236 adios (NULL
, "missing argument to %s", argp
[-2]);
239 if (!(file
= *argp
++) || *file
== '-')
240 adios (NULL
, "missing argument to %s", argp
[-2]);
243 if (!(sender
= *argp
++))/* allow -xyz arguments */
244 adios (NULL
, "missing argument to %s", argp
[-2]);
247 if (!(mbox
= *argp
++) || *mbox
== '-')
248 adios (NULL
, "missing argument to %s", argp
[-2]);
251 if (!(home
= *argp
++) || *home
== '-')
252 adios (NULL
, "missing argument to %s", argp
[-2]);
256 if (!(cp
= *argp
++) || *cp
== '-')
257 adios (NULL
, "missing argument to %s", argp
[-2]);
259 adios (NULL
, "only one maildelivery file at a time!");
282 switch (argp
- (argv
+ 1)) {
298 addr
= getusername ();
300 user
= (cp
= strchr(addr
, '.')) ? ++cp
: addr
;
301 if ((pw
= getpwnam (user
)) == NULL
)
302 adios (NULL
, "no such local user as %s", user
);
304 if (chdir (pw
->pw_dir
) == -1)
308 if (geteuid() == 0) {
310 initgroups (pw
->pw_name
, pw
->pw_gid
);
317 setbuf (stdin
, NULL
);
319 /* Record the delivery time */
320 if ((now
= dlocaltimenow ()) == NULL
)
321 adios (NULL
, "unable to ascertain local time");
322 snprintf (ddate
, sizeof(ddate
), "Delivery-Date: %s\n", dtimenow (0));
325 * Copy the message to a temporary file
330 /* getting message from file */
331 if ((tempfd
= open (file
, O_RDONLY
)) == -1)
332 adios (file
, "unable to open");
334 debug_printf ("retrieving message from file \"%s\"\n", file
);
335 if ((fd
= copy_message (tempfd
, tmpfil
, 1)) == -1)
336 adios (NULL
, "unable to create temporary file");
339 /* getting message from stdin */
341 debug_printf ("retrieving message from stdin\n");
342 if ((fd
= copy_message (fileno (stdin
), tmpfil
, 1)) == -1)
343 adios (NULL
, "unable to create temporary file");
347 debug_printf ("temporary file=\"%s\"\n", tmpfil
);
349 /* Delete the temp file now or a copy of every single message passed through
350 slocal will be left in the /tmp directory until deleted manually! This
351 unlink() used to be under an 'else' of the 'if (debug)' above, but since
352 some people like to always run slocal with -debug and log the results,
353 the /tmp directory would get choked over time. Of course, now that we
354 always delete the temp file, the "temporary file=" message above is
355 somewhat pointless -- someone watching debug output wouldn't have a
356 chance to 'tail -f' or 'ln' the temp file before it's unlinked. The best
357 thing would be to delay this unlink() until later if debug == 1, but I'll
358 leave that for someone who cares about the temp-file-accessing
359 functionality (they'll have to watch out for cases where we adios()). */
362 if (!(fp
= fdopen (fd
, "r+")))
363 adios (NULL
, "unable to access temporary file");
366 * If no sender given, extract it
367 * from envelope information. */
369 get_sender (envelope
, &sender
);
372 snprintf (mailbox
, sizeof(mailbox
), "%s/%s",
373 mmdfldir
[0] ? mmdfldir
: pw
->pw_dir
,
374 mmdflfil
[0] ? mmdflfil
: pw
->pw_name
);
381 debug_printf ("addr=\"%s\"\n", trim(addr
));
382 debug_printf ("user=\"%s\"\n", trim(user
));
383 debug_printf ("info=\"%s\"\n", trim(info
));
384 debug_printf ("sender=\"%s\"\n", trim(sender
));
385 debug_printf ("envelope=\"%s\"\n", envelope
? trim(envelope
) : "");
386 debug_printf ("mbox=\"%s\"\n", trim(mbox
));
387 debug_printf ("home=\"%s\"\n", trim(home
));
388 debug_printf ("ddate=\"%s\"\n", trim(ddate
));
389 debug_printf ("now=%02d:%02d\n\n", now
->tw_hour
, now
->tw_min
);
392 /* deliver the message */
393 status
= localmail (fd
, mdlvr
);
395 return done (status
!= -1 ? RCV_MOK
: RCV_MBX
);
400 * Main routine for delivering message.
404 localmail (int fd
, char *mdlvr
)
406 /* check if this message is a duplicate */
408 suppress_duplicates(fd
, mdlvr
? mdlvr
: ".maildelivery") == DONE
)
411 /* delivery according to personal Maildelivery file */
412 if (usr_delivery (fd
, mdlvr
? mdlvr
: ".maildelivery", 0) != -1)
415 /* delivery according to global Maildelivery file */
416 if (usr_delivery (fd
, maildelivery
, 1) != -1)
420 verbose_printf ("(delivering to standard mail spool)\n");
422 /* last resort - deliver to standard mail spool */
424 return usr_file (fd
, mbox
, MBOX_FORMAT
);
426 return usr_file (fd
, mbox
, MMDF_FORMAT
);
431 #define matches(a,b) (stringdex (b, a) >= 0)
434 * Parse the delivery file, and process incoming message.
438 usr_delivery (int fd
, char *delivery
, int su
)
440 int i
, accept
, status
, won
, vecp
, next
;
441 char *field
, *pattern
, *action
, *result
, *string
;
442 char buffer
[BUFSIZ
], tmpbuf
[BUFSIZ
];
443 char *cp
, *vec
[NVEC
];
448 /* open the delivery file */
449 if ((fp
= fopen (delivery
, "r")) == NULL
)
452 /* check if delivery file has bad ownership or permissions */
453 if (fstat (fileno (fp
), &st
) == -1
454 || (st
.st_uid
!= 0 && (su
|| st
.st_uid
!= pw
->pw_uid
))
455 || st
.st_mode
& (S_IWGRP
|S_IWOTH
)) {
457 verbose_printf ("WARNING: %s has bad ownership/modes (su=%d,uid=%d,owner=%d,mode=0%o)\n",
458 delivery
, su
, (int) pw
->pw_uid
, (int) st
.st_uid
, (int) st
.st_mode
);
466 /* read and process delivery file */
467 while (fgets (buffer
, sizeof(buffer
), fp
)) {
468 /* skip comments and empty lines */
469 if (*buffer
== '#' || *buffer
== '\n')
472 /* zap trailing newline */
473 if ((cp
= strchr(buffer
, '\n')))
476 /* split buffer into fields */
477 vecp
= split (buffer
, vec
);
479 /* check for too few fields */
482 debug_printf ("WARNING: entry with only %d fields, skipping.\n", vecp
);
487 for (i
= 0; vec
[i
]; i
++)
488 debug_printf ("vec[%d]: \"%s\"\n", i
, trim(vec
[i
]));
497 /* find out how to perform the action */
502 * If previous condition failed, don't
503 * do this - else fall through
506 continue; /* else fall */
510 * If already delivered, skip this action. Else
511 * consider delivered if action is successful.
514 continue; /* else fall */
519 * Take action, and consider delivered if
520 * action is successful.
529 * Take action, but don't consider delivered, even
530 * if action is successful
537 if (!strcasecmp (vec
[5], "select")) {
538 if (logged_in () != -1)
540 if (vecp
> 7 && timely (vec
[6], vec
[7]) == -1)
545 /* check if the field matches */
553 * "default" matches only if the message hasn't
554 * been delivered yet.
556 if (!strcasecmp (field
, "default")) {
563 /* parse message and build lookup table */
564 if (!parsed
&& parse (fd
) == -1) {
569 * find header field in lookup table, and
570 * see if the pattern matches.
572 if ((p
= lookup (hdrs
, field
)) && (p
->p_value
!= NULL
)
573 && matches (p
->p_value
, pattern
)) {
582 /* find out the action to perform */
585 /* deliver to quoted pipe */
586 if (strcasecmp (action
, "qpipe"))
587 continue; /* else fall */
589 expand (tmpbuf
, string
, fd
);
590 if (split (tmpbuf
, vec
) < 1)
592 status
= usr_pipe (fd
, tmpbuf
, vec
[0], vec
, 0);
596 /* deliver to pipe */
597 if (strcasecmp (action
, "pipe"))
598 continue; /* else fall */
602 expand (tmpbuf
, string
, fd
);
605 status
= usr_pipe (fd
, tmpbuf
, "/bin/sh", vec
+ 2, 0);
610 if (!strcasecmp (action
, "file")) {
611 status
= usr_file (fd
, string
, MBOX_FORMAT
);
614 /* deliver to nmh folder */
615 else if (strcasecmp (action
, "folder"))
616 continue; /* else fall */
618 status
= usr_folder (fd
, string
);
623 if (!strcasecmp (action
, "mmdf")) {
624 status
= usr_file (fd
, string
, MMDF_FORMAT
);
628 else if (strcasecmp (action
, "mbox"))
629 continue; /* else fall */
633 status
= usr_file (fd
, string
, MBOX_FORMAT
);
638 if (strcasecmp (action
, "destroy"))
644 if (accept
&& status
== 0)
649 return (won
? 0 : -1);
656 * Split buffer into fields (delimited by whitespace or
657 * comma's). Return the number of fields found.
661 split (char *cp
, char **vec
)
668 /* split into a maximum of NVEC fields */
669 for (i
= 0; i
<= NVEC
;) {
672 /* zap any whitespace and comma's */
673 while (isspace (*s
) || *s
== ',')
676 /* end of buffer, time to leave */
680 /* get double quote text as a single field */
682 for (vec
[i
++] = ++s
; *s
&& *s
!= '"'; s
++) {
684 * Check for escaped double quote. We need
685 * to shift the string to remove slash.
693 if (*s
== '"') /* zap trailing double quote */
698 if (*s
== QUOTE
&& *++s
!= '"')
702 /* move forward to next field delimiter */
703 while (*s
&& !isspace (*s
) && *s
!= ',')
713 * Parse the headers of a message, and build the
714 * lookup table for matching fields and patterns.
723 char name
[NAMESZ
], field
[BUFSIZ
];
730 /* get a new FILE pointer to message */
731 if ((fd1
= dup (fd
)) == -1)
733 if ((in
= fdopen (fd1
, "r")) == NULL
) {
739 /* add special entries to lookup table */
740 if ((p
= lookup (hdrs
, "source")))
741 p
->p_value
= getcpy (sender
);
742 if ((p
= lookup (hdrs
, "addr")))
743 p
->p_value
= getcpy (addr
);
746 * Scan the headers of the message and build
749 for (i
= 0, state
= FLD
;;) {
750 switch (state
= m_getfld (state
, name
, field
, sizeof(field
), in
)) {
754 lp
= add (field
, NULL
);
755 while (state
== FLDPLUS
) {
756 state
= m_getfld (state
, name
, field
, sizeof(field
), in
);
757 lp
= add (field
, lp
);
759 for (p
= hdrs
; p
->p_name
; p
++) {
760 if (!strcasecmp (p
->p_name
, name
)) {
761 if (!(p
->p_flags
& P_HID
)) {
762 if ((cp
= p
->p_value
)) {
763 if (p
->p_flags
& P_ADR
) {
764 dp
= cp
+ strlen (cp
) - 1;
767 cp
= add (",\n\t", cp
);
772 p
->p_value
= add (lp
, cp
);
778 if (p
->p_name
== NULL
&& i
< NVEC
) {
779 p
->p_name
= getcpy (name
);
796 advise (NULL
, "format error in message");
800 advise (NULL
, "internal error in m_getfld");
808 if ((p
= lookup (vars
, "reply-to"))) {
809 if ((q
= lookup (hdrs
, "reply-to")) == NULL
|| q
->p_value
== NULL
)
810 q
= lookup (hdrs
, "from");
811 p
->p_value
= getcpy (q
? q
->p_value
: "");
812 p
->p_flags
&= ~P_CHK
;
814 debug_printf ("vars[%d]: name=\"%s\" value=\"%s\"\n",
815 p
- vars
, p
->p_name
, trim(p
->p_value
));
818 for (p
= hdrs
; p
->p_name
; p
++)
819 debug_printf ("hdrs[%d]: name=\"%s\" value=\"%s\"\n",
820 p
- hdrs
, p
->p_name
, p
->p_value
? trim(p
->p_value
) : "");
831 * Expand any builtin variables such as $(sender),
832 * $(address), etc., in a string.
836 expand (char *s1
, char *s2
, int fd
)
844 while ((c
= *s2
++)) {
845 if (c
!= '$' || *s2
!= LPAREN
) {
848 for (cp
= ++s2
; *s2
&& *s2
!= RPAREN
; s2
++)
855 if ((p
= lookup (vars
, cp
))) {
856 if (!parsed
&& (p
->p_flags
& P_CHK
))
859 strcpy (s1
, p
->p_value
);
869 * Fill in the information missing from the "vars"
870 * table, which is necessary to expand any builtin
871 * variables in the string for a "pipe" or "qpipe"
885 if ((p
= lookup (vars
, "sender")))
886 p
->p_value
= getcpy (sender
);
887 if ((p
= lookup (vars
, "address")))
888 p
->p_value
= getcpy (addr
);
889 if ((p
= lookup (vars
, "size"))) {
890 snprintf (buffer
, sizeof(buffer
), "%d",
891 fstat (fd
, &st
) != -1 ? (int) st
.st_size
: 0);
892 p
->p_value
= getcpy (buffer
);
894 if ((p
= lookup (vars
, "info")))
895 p
->p_value
= getcpy (info
);
898 for (p
= vars
; p
->p_name
; p
++)
899 debug_printf ("vars[%d]: name=\"%s\" value=\"%s\"\n",
900 p
- vars
, p
->p_name
, trim(p
->p_value
));
906 * Find a matching name in a lookup table. If found,
907 * return the "pairs" entry, else return NULL.
911 lookup (struct pair
*pairs
, char *key
)
913 for (; pairs
->p_name
; pairs
++)
914 if (!strcasecmp (pairs
->p_name
, key
))
922 * Check utmp(x) file to see if user is currently
935 if ((uf
= fopen (UTMP_FILE
, "r")) == NULL
)
938 while (fread ((char *) &ut
, sizeof(ut
), 1, uf
) == 1) {
939 if (ut
.ut_name
[0] != 0
940 && strncmp (user
, ut
.ut_name
, sizeof(ut
.ut_name
)) == 0) {
944 return (utmped
= DONE
);
949 return (utmped
= NOTOK
);
953 #define check(t,a,b) if (t < a || t > b) return -1
954 #define cmpar(h1,m1,h2,m2) if (h1 < h2 || (h1 == h2 && m1 < m2)) return 0
957 timely (char *t1
, char *t2
)
959 int t1hours
, t1mins
, t2hours
, t2mins
;
961 if (sscanf (t1
, "%d:%d", &t1hours
, &t1mins
) != 2)
963 check (t1hours
, 0, 23);
964 check (t1mins
, 0, 59);
966 if (sscanf (t2
, "%d:%d", &t2hours
, &t2mins
) != 2)
968 check (t2hours
, 0, 23);
969 check (t2mins
, 0, 59);
971 cmpar (now
->tw_hour
, now
->tw_min
, t1hours
, t1mins
);
972 cmpar (t2hours
, t2mins
, now
->tw_hour
, now
->tw_min
);
979 * Deliver message by appending to a file.
983 usr_file (int fd
, char *mailbox
, int mbx_style
)
988 verbose_printf ("delivering to file \"%s\"", mailbox
);
990 if (mbx_style
== MBOX_FORMAT
) {
992 verbose_printf (" (mbox style)");
996 verbose_printf (" (mmdf style)");
1000 /* open and lock the file */
1001 if ((md
= mbx_open (mailbox
, mbx_style
, pw
->pw_uid
, pw
->pw_gid
, m_gmprot())) == -1) {
1003 adorn ("", "unable to open:");
1007 lseek (fd
, (off_t
) 0, SEEK_SET
);
1009 /* append message to file */
1010 if (mbx_copy (mailbox
, mbx_style
, md
, fd
, mapping
, NULL
, verbose
) == -1) {
1012 adorn ("", "error writing to:");
1016 /* close and unlock file */
1017 mbx_close (mailbox
, md
);
1020 verbose_printf (", success.\n");
1026 * Deliver message to a nmh folder.
1030 usr_folder (int fd
, char *string
)
1033 char folder
[BUFSIZ
], *vec
[3];
1035 /* get folder name ready */
1037 strncpy(folder
, string
, sizeof(folder
));
1039 snprintf(folder
, sizeof(folder
), "+%s", string
);
1042 verbose_printf ("delivering to folder \"%s\"", folder
+ 1);
1044 vec
[0] = "rcvstore";
1048 /* use rcvstore to put message in folder */
1049 status
= usr_pipe (fd
, "rcvstore", rcvstoreproc
, vec
, 1);
1053 * Currently, verbose status messages are handled by usr_pipe().
1057 verbose_printf (", success.\n");
1059 verbose_printf (", failed.\n");
1067 * Deliver message to a process.
1071 usr_pipe (int fd
, char *cmd
, char *pgm
, char **vec
, int suppress
)
1074 int i
, bytes
, seconds
, status
;
1077 if (verbose
&& !suppress
)
1078 verbose_printf ("delivering to pipe \"%s\"", cmd
);
1080 lseek (fd
, (off_t
) 0, SEEK_SET
);
1082 for (i
= 0; (child_id
= fork()) == -1 && i
< 5; i
++)
1089 adorn ("fork", "unable to");
1096 freopen ("/dev/null", "w", stdout
);
1097 freopen ("/dev/null", "w", stderr
);
1103 if ((fd
= open ("/dev/tty", O_RDWR
)) != -1) {
1104 ioctl (fd
, TIOCNOTTY
, NULL
);
1107 #endif /* TIOCNOTTY */
1109 setpgid ((pid_t
) 0, getpid ()); /* put in own process group */
1112 m_putenv ("USER", pw
->pw_name
);
1113 m_putenv ("HOME", pw
->pw_dir
);
1114 m_putenv ("SHELL", pw
->pw_shell
);
1120 /* parent process */
1121 if (!setjmp (myctx
)) {
1122 SIGNAL (SIGALRM
, alrmser
);
1123 bytes
= fstat (fd
, &st
) != -1 ? (int) st
.st_size
: 100;
1125 /* amount of time to wait depends on message size */
1127 /* give at least 5 minutes */
1129 } else if (bytes
>= 90000) {
1130 /* a half hour is long enough */
1133 seconds
= (bytes
/ 60) + 300;
1135 alarm ((unsigned int) seconds
);
1136 status
= pidwait (child_id
, 0);
1140 if (status
== RP_MOK
|| status
== RP_OK
)
1145 verbose_printf (", success.\n");
1147 if ((status
& 0xff00) == 0xff00)
1148 verbose_printf (", system error\n");
1150 pidstatus (status
, stdout
, ", failed");
1152 return (status
== 0 ? 0 : -1);
1155 * Ruthlessly kill the child and anything
1156 * else in its process group.
1158 KILLPG(child_id
, SIGKILL
);
1160 verbose_printf (", timed-out; terminated\n");
1170 #ifndef RELIABLE_SIGNALS
1171 SIGNAL (SIGALRM
, alrmser
);
1174 longjmp (myctx
, DONE
);
1179 * Get the `sender' from the envelope
1180 * information ("From " line).
1184 get_sender (char *envelope
, char **sender
)
1188 char buffer
[BUFSIZ
];
1190 if (envelope
== NULL
) {
1191 *sender
= getcpy ("");
1195 i
= strlen ("From ");
1196 strncpy (buffer
, envelope
+ i
, sizeof(buffer
));
1197 if ((cp
= strchr(buffer
, '\n'))) {
1207 for (cp
= buffer
+ strlen (buffer
) - 1; cp
>= buffer
; cp
--)
1212 *sender
= getcpy (buffer
);
1217 * Copy message into a temporary file.
1218 * While copying, it will do some header processing
1219 * including the extraction of the envelope information.
1223 copy_message (int qd
, char *tmpfil
, int fold
)
1225 int i
, first
= 1, fd1
, fd2
;
1226 char buffer
[BUFSIZ
];
1229 strcpy (tmpfil
, m_tmpfil (invo_name
));
1231 /* open temporary file to put message in */
1232 if ((fd1
= open (tmpfil
, O_RDWR
| O_CREAT
| O_TRUNC
, 0600)) == -1)
1236 while ((i
= read (qd
, buffer
, sizeof(buffer
))) > 0)
1237 if (write (fd1
, buffer
, i
) != i
) {
1245 lseek (fd1
, (off_t
) 0, SEEK_SET
);
1249 /* dup the fd for incoming message */
1250 if ((fd2
= dup (qd
)) == -1) {
1255 /* now create a FILE pointer for it */
1256 if ((qfp
= fdopen (fd2
, "r")) == NULL
) {
1262 /* dup the fd for temporary file */
1263 if ((fd2
= dup (fd1
)) == -1) {
1269 /* now create a FILE pointer for it */
1270 if ((ffp
= fdopen (fd2
, "r+")) == NULL
) {
1278 * copy message into temporary file
1279 * and massage the headers. Save
1280 * a copy of the "From " line for later.
1282 i
= strlen ("From ");
1283 while (fgets (buffer
, sizeof(buffer
), qfp
)) {
1286 if (!strncmp (buffer
, "From ", i
)) {
1288 char *fp
, *cp
, *hp
, *ep
;
1290 /* get copy of envelope information ("From " line) */
1291 envelope
= getcpy (buffer
);
1294 /* First go ahead and put "From " line in message */
1295 fputs (buffer
, ffp
);
1302 * Now create a "Return-Path:" line
1303 * from the "From " line.
1305 hp
= cp
= strchr(fp
= envelope
+ i
, ' ');
1306 while ((hp
= strchr(++hp
, 'r')))
1307 if (uprf (hp
, "remote from")) {
1308 hp
= strrchr(hp
, ' ');
1312 /* return path for UUCP style addressing */
1313 ep
= strchr(++hp
, '\n');
1314 snprintf (buffer
, sizeof(buffer
), "Return-Path: %.*s!%.*s\n",
1315 ep
- hp
, hp
, cp
- fp
, fp
);
1317 /* return path for standard domain addressing */
1318 snprintf (buffer
, sizeof(buffer
), "Return-Path: %.*s\n",
1322 /* Add Return-Path header to message */
1323 fputs (buffer
, ffp
);
1327 /* Put the delivery date in message */
1336 fputs (buffer
, ffp
);
1348 lseek (fd1
, (off_t
) 0, SEEK_SET
);
1360 * Trim strings for pretty printing of debugging output
1366 char buffer
[BUFSIZ
*4];
1372 /* copy string into temp buffer */
1373 strncpy (buffer
, cp
, sizeof(buffer
));
1376 /* skip over leading whitespace */
1377 while (isspace(*bp
))
1380 /* start at the end and zap trailing whitespace */
1381 for (sp
= bp
+ strlen(bp
) - 1; sp
>= bp
; sp
--) {
1388 /* replace remaining whitespace with spaces */
1389 for (sp
= bp
; *sp
; sp
++)
1393 /* now return a copy */
1398 * Function for printing `verbose' messages.
1402 verbose_printf (char *fmt
, ...)
1407 vfprintf (stdout
, fmt
, ap
);
1410 fflush (stdout
); /* now flush output */
1415 * Function for printing `verbose' delivery
1420 adorn (char *what
, char *fmt
, ...)
1426 eindex
= errno
; /* save the errno */
1427 fprintf (stdout
, ", ");
1430 vfprintf (stdout
, fmt
, ap
);
1435 fprintf (stdout
, " %s: ", what
);
1436 if ((s
= strerror (eindex
)))
1437 fprintf (stdout
, "%s", s
);
1439 fprintf (stdout
, "Error %d", eindex
);
1442 fputc ('\n', stdout
);
1448 * Function for printing `debug' messages.
1452 debug_printf (char *fmt
, ...)
1457 vfprintf (stderr
, fmt
, ap
);
1463 * Check ndbm/db file(s) to see if the Message-Id of this
1464 * message matches the Message-Id of a previous message,
1465 * so we can discard it. If it doesn't match, we add the
1466 * Message-Id of this message to the ndbm/db file.
1469 suppress_duplicates (int fd
, char *file
)
1471 int fd1
, lockfd
, state
, result
;
1472 char *cp
, buf
[BUFSIZ
], name
[NAMESZ
];
1477 if ((fd1
= dup (fd
)) == -1)
1479 if (!(in
= fdopen (fd1
, "r"))) {
1485 for (state
= FLD
;;) {
1486 state
= m_getfld (state
, name
, buf
, sizeof(buf
), in
);
1491 /* Search for the message ID */
1492 if (strcasecmp (name
, "Message-ID")) {
1493 while (state
== FLDPLUS
)
1494 state
= m_getfld (state
, name
, buf
, sizeof(buf
), in
);
1498 cp
= add (buf
, NULL
);
1499 while (state
== FLDPLUS
) {
1500 state
= m_getfld (state
, name
, buf
, sizeof(buf
), in
);
1503 key
.dptr
= trimcpy (cp
);
1504 key
.dsize
= strlen (key
.dptr
) + 1;
1508 if (!(db
= dbm_open (file
, O_RDWR
| O_CREAT
, 0600))) {
1509 advise (file
, "unable to perform dbm_open on");
1515 * Since it is difficult to portable lock a ndbm file,
1516 * we will open and lock the Maildelivery file instead.
1517 * This will fail if your Maildelivery file doesn't
1520 if ((lockfd
= lkopen(file
, O_RDWR
, 0)) == -1) {
1521 advise (file
, "unable to perform file locking on");
1526 value
= dbm_fetch (db
, key
);
1529 verbose_printf ("Message-ID: %s\n already received on %s",
1533 value
.dptr
= ddate
+ sizeof("Delivery-Date:");
1534 value
.dsize
= strlen(value
.dptr
) + 1;
1535 if (dbm_store (db
, key
, value
, DBM_INSERT
))
1536 advise (file
, "possibly corrupt file");
1541 lkclose(lockfd
, file
);