]>
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
43 # define UTMP_FILE _PATH_UTMP
45 # define UTMP_FILE "/etc/utmp"
49 static struct swit switches
[] = {
51 { "addr address", 0 },
57 { "sender address", 0 },
59 { "mailbox file", 0 },
61 { "home directory", -4 },
65 { "maildelivery file", 0 },
70 #define SUPPRESSDUP 10
72 #define NSUPPRESSDUP 11
73 { "nosuppressdup", 0 },
83 static int globbed
= 0; /* have we built "vars" table yet? */
84 static int parsed
= 0; /* have we built header field table yet */
85 static int utmped
= 0; /* have we scanned umtp(x) file yet */
86 static int suppressdup
= 0; /* are we suppressing duplicate messages? */
88 static int verbose
= 0;
91 static char *addr
= NULL
;
92 static char *user
= NULL
;
93 static char *info
= NULL
;
94 static char *file
= NULL
;
95 static char *sender
= NULL
;
96 static char *envelope
= NULL
; /* envelope information ("From " line) */
97 static char *mbox
= NULL
;
98 static char *home
= NULL
;
100 static struct passwd
*pw
; /* passwd file entry */
102 static char ddate
[BUFSIZ
]; /* record the delivery date */
105 static jmp_buf myctx
;
107 /* flags for pair->p_flags */
109 #define P_ADR 0x01 /* field is address */
110 #define P_HID 0x02 /* special (fake) field */
122 * Lookup table for matching fields and patterns
123 * in messages. The rest of the table is added
124 * when the message is parsed.
126 static struct pair hdrs
[NVEC
+ 1] = {
127 { "source", NULL
, P_HID
},
128 { "addr", NULL
, P_HID
},
129 { "Return-Path", NULL
, P_ADR
},
130 { "Reply-To", NULL
, P_ADR
},
131 { "From", NULL
, P_ADR
},
132 { "Sender", NULL
, P_ADR
},
133 { "To", NULL
, P_ADR
},
134 { "cc", NULL
, P_ADR
},
135 { "Resent-Reply-To", NULL
, P_ADR
},
136 { "Resent-From", NULL
, P_ADR
},
137 { "Resent-Sender", NULL
, P_ADR
},
138 { "Resent-To", NULL
, P_ADR
},
139 { "Resent-cc", NULL
, P_ADR
},
144 * The list of builtin variables to expand in a string
145 * before it is executed by the "pipe" or "qpipe" action.
147 static struct pair vars
[] = {
148 { "sender", NULL
, P_NIL
},
149 { "address", NULL
, P_NIL
},
150 { "size", NULL
, P_NIL
},
151 { "reply-to", NULL
, P_CHK
},
152 { "info", NULL
, P_NIL
},
156 extern char **environ
;
161 static int localmail (int, char *);
162 static int usr_delivery (int, char *, int);
163 static int split (char *, char **);
164 static int parse (int);
165 static void expand (char *, char *, int);
166 static void glob (int);
167 static struct pair
*lookup (struct pair
*, char *);
168 static int logged_in (void);
169 static int timely (char *, char *);
170 static int usr_file (int, char *, int);
171 static int usr_pipe (int, char *, char *, char **, int);
172 static int usr_folder (int, char *);
173 static RETSIGTYPE
alrmser (int);
174 static void get_sender (char *, char **);
175 static int copy_message (int, char *, int);
176 static void verbose_printf (char *fmt
, ...);
177 static void adorn (char *, char *, ...);
178 static void debug_printf (char *fmt
, ...);
179 static int suppress_duplicates (int, char *);
180 static char *trim (char *);
184 main (int argc
, char **argv
)
188 char *cp
, *mdlvr
= NULL
, buf
[BUFSIZ
];
189 char mailbox
[BUFSIZ
], tmpfil
[BUFSIZ
];
190 char **argp
, **arguments
;
193 setlocale(LC_ALL
, "");
195 invo_name
= r1bindex (*argv
, '/');
197 /* foil search of user profile/context */
198 if (context_foil (NULL
) == -1)
201 mts_init (invo_name
);
202 arguments
= getarguments (invo_name
, argc
, argv
, 0);
205 /* Parse arguments */
206 while ((cp
= *argp
++)) {
208 switch (smatch (++cp
, switches
)) {
210 ambigsw (cp
, switches
);
213 adios (NULL
, "-%s unknown", cp
);
216 snprintf (buf
, sizeof(buf
),
217 "%s [switches] [address info sender]", invo_name
);
218 print_help (buf
, switches
, 0);
221 print_version(invo_name
);
225 if (!(addr
= *argp
++))/* allow -xyz arguments */
226 adios (NULL
, "missing argument to %s", argp
[-2]);
229 if (!(info
= *argp
++))/* allow -xyz arguments */
230 adios (NULL
, "missing argument to %s", argp
[-2]);
233 if (!(user
= *argp
++))/* allow -xyz arguments */
234 adios (NULL
, "missing argument to %s", argp
[-2]);
237 if (!(file
= *argp
++) || *file
== '-')
238 adios (NULL
, "missing argument to %s", argp
[-2]);
241 if (!(sender
= *argp
++))/* allow -xyz arguments */
242 adios (NULL
, "missing argument to %s", argp
[-2]);
245 if (!(mbox
= *argp
++) || *mbox
== '-')
246 adios (NULL
, "missing argument to %s", argp
[-2]);
249 if (!(home
= *argp
++) || *home
== '-')
250 adios (NULL
, "missing argument to %s", argp
[-2]);
254 if (!(cp
= *argp
++) || *cp
== '-')
255 adios (NULL
, "missing argument to %s", argp
[-2]);
257 adios (NULL
, "only one maildelivery file at a time!");
280 switch (argp
- (argv
+ 1)) {
296 addr
= getusername ();
298 user
= (cp
= strchr(addr
, '.')) ? ++cp
: addr
;
299 if ((pw
= getpwnam (user
)) == NULL
)
300 adios (NULL
, "no such local user as %s", user
);
302 if (chdir (pw
->pw_dir
) == -1)
306 if (geteuid() == 0) {
308 initgroups (pw
->pw_name
, pw
->pw_gid
);
315 setbuf (stdin
, NULL
);
317 /* Record the delivery time */
318 if ((now
= dlocaltimenow ()) == NULL
)
319 adios (NULL
, "unable to ascertain local time");
320 snprintf (ddate
, sizeof(ddate
), "Delivery-Date: %s\n", dtimenow (0));
323 * Copy the message to a temporary file
328 /* getting message from file */
329 if ((tempfd
= open (file
, O_RDONLY
)) == -1)
330 adios (file
, "unable to open");
332 debug_printf ("retrieving message from file \"%s\"\n", file
);
333 if ((fd
= copy_message (tempfd
, tmpfil
, 1)) == -1)
334 adios (NULL
, "unable to create temporary file");
337 /* getting message from stdin */
339 debug_printf ("retrieving message from stdin\n");
340 if ((fd
= copy_message (fileno (stdin
), tmpfil
, 1)) == -1)
341 adios (NULL
, "unable to create temporary file");
345 debug_printf ("temporary file=\"%s\"\n", tmpfil
);
347 /* Delete the temp file now or a copy of every single message passed through
348 slocal will be left in the /tmp directory until deleted manually! This
349 unlink() used to be under an 'else' of the 'if (debug)' above, but since
350 some people like to always run slocal with -debug and log the results,
351 the /tmp directory would get choked over time. Of course, now that we
352 always delete the temp file, the "temporary file=" message above is
353 somewhat pointless -- someone watching debug output wouldn't have a
354 chance to 'tail -f' or 'ln' the temp file before it's unlinked. The best
355 thing would be to delay this unlink() until later if debug == 1, but I'll
356 leave that for someone who cares about the temp-file-accessing
357 functionality (they'll have to watch out for cases where we adios()). */
360 if (!(fp
= fdopen (fd
, "r+")))
361 adios (NULL
, "unable to access temporary file");
364 * If no sender given, extract it
365 * from envelope information. */
367 get_sender (envelope
, &sender
);
370 snprintf (mailbox
, sizeof(mailbox
), "%s/%s",
371 mmdfldir
[0] ? mmdfldir
: pw
->pw_dir
,
372 mmdflfil
[0] ? mmdflfil
: pw
->pw_name
);
379 debug_printf ("addr=\"%s\"\n", trim(addr
));
380 debug_printf ("user=\"%s\"\n", trim(user
));
381 debug_printf ("info=\"%s\"\n", trim(info
));
382 debug_printf ("sender=\"%s\"\n", trim(sender
));
383 debug_printf ("envelope=\"%s\"\n", envelope
? trim(envelope
) : "");
384 debug_printf ("mbox=\"%s\"\n", trim(mbox
));
385 debug_printf ("home=\"%s\"\n", trim(home
));
386 debug_printf ("ddate=\"%s\"\n", trim(ddate
));
387 debug_printf ("now=%02d:%02d\n\n", now
->tw_hour
, now
->tw_min
);
390 /* deliver the message */
391 status
= localmail (fd
, mdlvr
);
393 return done (status
!= -1 ? RCV_MOK
: RCV_MBX
);
398 * Main routine for delivering message.
402 localmail (int fd
, char *mdlvr
)
404 /* check if this message is a duplicate */
406 suppress_duplicates(fd
, mdlvr
? mdlvr
: ".maildelivery") == DONE
)
409 /* delivery according to personal Maildelivery file */
410 if (usr_delivery (fd
, mdlvr
? mdlvr
: ".maildelivery", 0) != -1)
413 /* delivery according to global Maildelivery file */
414 if (usr_delivery (fd
, maildelivery
, 1) != -1)
418 verbose_printf ("(delivering to standard mail spool)\n");
420 /* last resort - deliver to standard mail spool */
422 return usr_file (fd
, mbox
, MBOX_FORMAT
);
424 return usr_file (fd
, mbox
, MMDF_FORMAT
);
429 #define matches(a,b) (stringdex (b, a) >= 0)
432 * Parse the delivery file, and process incoming message.
436 usr_delivery (int fd
, char *delivery
, int su
)
438 int i
, accept
, status
, won
, vecp
, next
;
439 char *field
, *pattern
, *action
, *result
, *string
;
440 char buffer
[BUFSIZ
], tmpbuf
[BUFSIZ
];
441 char *cp
, *vec
[NVEC
];
446 /* open the delivery file */
447 if ((fp
= fopen (delivery
, "r")) == NULL
)
450 /* check if delivery file has bad ownership or permissions */
451 if (fstat (fileno (fp
), &st
) == -1
452 || (st
.st_uid
!= 0 && (su
|| st
.st_uid
!= pw
->pw_uid
))
453 || st
.st_mode
& (S_IWGRP
|S_IWOTH
)) {
455 verbose_printf ("WARNING: %s has bad ownership/modes (su=%d,uid=%d,owner=%d,mode=0%o)\n",
456 delivery
, su
, (int) pw
->pw_uid
, (int) st
.st_uid
, (int) st
.st_mode
);
464 /* read and process delivery file */
465 while (fgets (buffer
, sizeof(buffer
), fp
)) {
466 /* skip comments and empty lines */
467 if (*buffer
== '#' || *buffer
== '\n')
470 /* zap trailing newline */
471 if ((cp
= strchr(buffer
, '\n')))
474 /* split buffer into fields */
475 vecp
= split (buffer
, vec
);
477 /* check for too few fields */
480 debug_printf ("WARNING: entry with only %d fields, skipping.\n", vecp
);
485 for (i
= 0; vec
[i
]; i
++)
486 debug_printf ("vec[%d]: \"%s\"\n", i
, trim(vec
[i
]));
495 /* find out how to perform the action */
500 * If previous condition failed, don't
501 * do this - else fall through
504 continue; /* else fall */
508 * If already delivered, skip this action. Else
509 * consider delivered if action is successful.
512 continue; /* else fall */
517 * Take action, and consider delivered if
518 * action is successful.
527 * Take action, but don't consider delivered, even
528 * if action is successful
535 if (!strcasecmp (vec
[5], "select")) {
536 if (logged_in () != -1)
538 if (vecp
> 7 && timely (vec
[6], vec
[7]) == -1)
543 /* check if the field matches */
551 * "default" matches only if the message hasn't
552 * been delivered yet.
554 if (!strcasecmp (field
, "default")) {
561 /* parse message and build lookup table */
562 if (!parsed
&& parse (fd
) == -1) {
567 * find header field in lookup table, and
568 * see if the pattern matches.
570 if ((p
= lookup (hdrs
, field
)) && (p
->p_value
!= NULL
)
571 && matches (p
->p_value
, pattern
)) {
580 /* find out the action to perform */
583 /* deliver to quoted pipe */
584 if (strcasecmp (action
, "qpipe"))
585 continue; /* else fall */
587 expand (tmpbuf
, string
, fd
);
588 if (split (tmpbuf
, vec
) < 1)
590 status
= usr_pipe (fd
, tmpbuf
, vec
[0], vec
, 0);
594 /* deliver to pipe */
595 if (strcasecmp (action
, "pipe"))
596 continue; /* else fall */
600 expand (tmpbuf
, string
, fd
);
603 status
= usr_pipe (fd
, tmpbuf
, "/bin/sh", vec
+ 2, 0);
608 if (!strcasecmp (action
, "file")) {
609 status
= usr_file (fd
, string
, MBOX_FORMAT
);
612 /* deliver to nmh folder */
613 else if (strcasecmp (action
, "folder"))
614 continue; /* else fall */
616 status
= usr_folder (fd
, string
);
621 if (!strcasecmp (action
, "mmdf")) {
622 status
= usr_file (fd
, string
, MMDF_FORMAT
);
626 else if (strcasecmp (action
, "mbox"))
627 continue; /* else fall */
631 status
= usr_file (fd
, string
, MBOX_FORMAT
);
636 if (strcasecmp (action
, "destroy"))
642 if (accept
&& status
== 0)
647 return (won
? 0 : -1);
654 * Split buffer into fields (delimited by whitespace or
655 * comma's). Return the number of fields found.
659 split (char *cp
, char **vec
)
666 /* split into a maximum of NVEC fields */
667 for (i
= 0; i
<= NVEC
;) {
670 /* zap any whitespace and comma's */
671 while (isspace (*s
) || *s
== ',')
674 /* end of buffer, time to leave */
678 /* get double quote text as a single field */
680 for (vec
[i
++] = ++s
; *s
&& *s
!= '"'; s
++) {
682 * Check for escaped double quote. We need
683 * to shift the string to remove slash.
691 if (*s
== '"') /* zap trailing double quote */
696 if (*s
== QUOTE
&& *++s
!= '"')
700 /* move forward to next field delimiter */
701 while (*s
&& !isspace (*s
) && *s
!= ',')
711 * Parse the headers of a message, and build the
712 * lookup table for matching fields and patterns.
721 char name
[NAMESZ
], field
[BUFSIZ
];
728 /* get a new FILE pointer to message */
729 if ((fd1
= dup (fd
)) == -1)
731 if ((in
= fdopen (fd1
, "r")) == NULL
) {
737 /* add special entries to lookup table */
738 if ((p
= lookup (hdrs
, "source")))
739 p
->p_value
= getcpy (sender
);
740 if ((p
= lookup (hdrs
, "addr")))
741 p
->p_value
= getcpy (addr
);
744 * Scan the headers of the message and build
747 for (i
= 0, state
= FLD
;;) {
748 switch (state
= m_getfld (state
, name
, field
, sizeof(field
), in
)) {
752 lp
= add (field
, NULL
);
753 while (state
== FLDPLUS
) {
754 state
= m_getfld (state
, name
, field
, sizeof(field
), in
);
755 lp
= add (field
, lp
);
757 for (p
= hdrs
; p
->p_name
; p
++) {
758 if (!strcasecmp (p
->p_name
, name
)) {
759 if (!(p
->p_flags
& P_HID
)) {
760 if ((cp
= p
->p_value
)) {
761 if (p
->p_flags
& P_ADR
) {
762 dp
= cp
+ strlen (cp
) - 1;
765 cp
= add (",\n\t", cp
);
770 p
->p_value
= add (lp
, cp
);
776 if (p
->p_name
== NULL
&& i
< NVEC
) {
777 p
->p_name
= getcpy (name
);
794 advise (NULL
, "format error in message");
798 advise (NULL
, "internal error in m_getfld");
806 if ((p
= lookup (vars
, "reply-to"))) {
807 if ((q
= lookup (hdrs
, "reply-to")) == NULL
|| q
->p_value
== NULL
)
808 q
= lookup (hdrs
, "from");
809 p
->p_value
= getcpy (q
? q
->p_value
: "");
810 p
->p_flags
&= ~P_CHK
;
812 debug_printf ("vars[%d]: name=\"%s\" value=\"%s\"\n",
813 p
- vars
, p
->p_name
, trim(p
->p_value
));
816 for (p
= hdrs
; p
->p_name
; p
++)
817 debug_printf ("hdrs[%d]: name=\"%s\" value=\"%s\"\n",
818 p
- hdrs
, p
->p_name
, p
->p_value
? trim(p
->p_value
) : "");
829 * Expand any builtin variables such as $(sender),
830 * $(address), etc., in a string.
834 expand (char *s1
, char *s2
, int fd
)
842 while ((c
= *s2
++)) {
843 if (c
!= '$' || *s2
!= LPAREN
) {
846 for (cp
= ++s2
; *s2
&& *s2
!= RPAREN
; s2
++)
853 if ((p
= lookup (vars
, cp
))) {
854 if (!parsed
&& (p
->p_flags
& P_CHK
))
857 strcpy (s1
, p
->p_value
);
867 * Fill in the information missing from the "vars"
868 * table, which is necessary to expand any builtin
869 * variables in the string for a "pipe" or "qpipe"
883 if ((p
= lookup (vars
, "sender")))
884 p
->p_value
= getcpy (sender
);
885 if ((p
= lookup (vars
, "address")))
886 p
->p_value
= getcpy (addr
);
887 if ((p
= lookup (vars
, "size"))) {
888 snprintf (buffer
, sizeof(buffer
), "%d",
889 fstat (fd
, &st
) != -1 ? (int) st
.st_size
: 0);
890 p
->p_value
= getcpy (buffer
);
892 if ((p
= lookup (vars
, "info")))
893 p
->p_value
= getcpy (info
);
896 for (p
= vars
; p
->p_name
; p
++)
897 debug_printf ("vars[%d]: name=\"%s\" value=\"%s\"\n",
898 p
- vars
, p
->p_name
, trim(p
->p_value
));
904 * Find a matching name in a lookup table. If found,
905 * return the "pairs" entry, else return NULL.
909 lookup (struct pair
*pairs
, char *key
)
911 for (; pairs
->p_name
; pairs
++)
912 if (!strcasecmp (pairs
->p_name
, key
))
920 * Check utmp(x) file to see if user is currently
933 if ((uf
= fopen (UTMP_FILE
, "r")) == NULL
)
936 while (fread ((char *) &ut
, sizeof(ut
), 1, uf
) == 1) {
937 if (ut
.ut_name
[0] != 0
938 && strncmp (user
, ut
.ut_name
, sizeof(ut
.ut_name
)) == 0) {
942 return (utmped
= DONE
);
947 return (utmped
= NOTOK
);
951 #define check(t,a,b) if (t < a || t > b) return -1
952 #define cmpar(h1,m1,h2,m2) if (h1 < h2 || (h1 == h2 && m1 < m2)) return 0
955 timely (char *t1
, char *t2
)
957 int t1hours
, t1mins
, t2hours
, t2mins
;
959 if (sscanf (t1
, "%d:%d", &t1hours
, &t1mins
) != 2)
961 check (t1hours
, 0, 23);
962 check (t1mins
, 0, 59);
964 if (sscanf (t2
, "%d:%d", &t2hours
, &t2mins
) != 2)
966 check (t2hours
, 0, 23);
967 check (t2mins
, 0, 59);
969 cmpar (now
->tw_hour
, now
->tw_min
, t1hours
, t1mins
);
970 cmpar (t2hours
, t2mins
, now
->tw_hour
, now
->tw_min
);
977 * Deliver message by appending to a file.
981 usr_file (int fd
, char *mailbox
, int mbx_style
)
986 verbose_printf ("delivering to file \"%s\"", mailbox
);
988 if (mbx_style
== MBOX_FORMAT
) {
990 verbose_printf (" (mbox style)");
994 verbose_printf (" (mmdf style)");
998 /* open and lock the file */
999 if ((md
= mbx_open (mailbox
, mbx_style
, pw
->pw_uid
, pw
->pw_gid
, m_gmprot())) == -1) {
1001 adorn ("", "unable to open:");
1005 lseek (fd
, (off_t
) 0, SEEK_SET
);
1007 /* append message to file */
1008 if (mbx_copy (mailbox
, mbx_style
, md
, fd
, mapping
, NULL
, verbose
) == -1) {
1010 adorn ("", "error writing to:");
1014 /* close and unlock file */
1015 mbx_close (mailbox
, md
);
1018 verbose_printf (", success.\n");
1024 * Deliver message to a nmh folder.
1028 usr_folder (int fd
, char *string
)
1031 char folder
[BUFSIZ
], *vec
[3];
1033 /* get folder name ready */
1035 strncpy(folder
, string
, sizeof(folder
));
1037 snprintf(folder
, sizeof(folder
), "+%s", string
);
1040 verbose_printf ("delivering to folder \"%s\"", folder
+ 1);
1042 vec
[0] = "rcvstore";
1046 /* use rcvstore to put message in folder */
1047 status
= usr_pipe (fd
, "rcvstore", rcvstoreproc
, vec
, 1);
1051 * Currently, verbose status messages are handled by usr_pipe().
1055 verbose_printf (", success.\n");
1057 verbose_printf (", failed.\n");
1065 * Deliver message to a process.
1069 usr_pipe (int fd
, char *cmd
, char *pgm
, char **vec
, int suppress
)
1072 int i
, bytes
, seconds
, status
;
1075 if (verbose
&& !suppress
)
1076 verbose_printf ("delivering to pipe \"%s\"", cmd
);
1078 lseek (fd
, (off_t
) 0, SEEK_SET
);
1080 for (i
= 0; (child_id
= fork()) == -1 && i
< 5; i
++)
1087 adorn ("fork", "unable to");
1094 freopen ("/dev/null", "w", stdout
);
1095 freopen ("/dev/null", "w", stderr
);
1101 if ((fd
= open ("/dev/tty", O_RDWR
)) != -1) {
1102 ioctl (fd
, TIOCNOTTY
, NULL
);
1105 #endif /* TIOCNOTTY */
1107 setpgid ((pid_t
) 0, getpid ()); /* put in own process group */
1110 m_putenv ("USER", pw
->pw_name
);
1111 m_putenv ("HOME", pw
->pw_dir
);
1112 m_putenv ("SHELL", pw
->pw_shell
);
1118 /* parent process */
1119 if (!setjmp (myctx
)) {
1120 SIGNAL (SIGALRM
, alrmser
);
1121 bytes
= fstat (fd
, &st
) != -1 ? (int) st
.st_size
: 100;
1123 /* amount of time to wait depends on message size */
1125 /* give at least 5 minutes */
1127 } else if (bytes
>= 90000) {
1128 /* a half hour is long enough */
1131 seconds
= (bytes
/ 60) + 300;
1133 alarm ((unsigned int) seconds
);
1134 status
= pidwait (child_id
, 0);
1138 if (status
== RP_MOK
|| status
== RP_OK
)
1143 verbose_printf (", success.\n");
1145 if ((status
& 0xff00) == 0xff00)
1146 verbose_printf (", system error\n");
1148 pidstatus (status
, stdout
, ", failed");
1150 return (status
== 0 ? 0 : -1);
1153 * Ruthlessly kill the child and anything
1154 * else in its process group.
1156 KILLPG(child_id
, SIGKILL
);
1158 verbose_printf (", timed-out; terminated\n");
1168 #ifndef RELIABLE_SIGNALS
1169 SIGNAL (SIGALRM
, alrmser
);
1172 longjmp (myctx
, DONE
);
1177 * Get the `sender' from the envelope
1178 * information ("From " line).
1182 get_sender (char *envelope
, char **sender
)
1186 char buffer
[BUFSIZ
];
1188 if (envelope
== NULL
) {
1189 *sender
= getcpy ("");
1193 i
= strlen ("From ");
1194 strncpy (buffer
, envelope
+ i
, sizeof(buffer
));
1195 if ((cp
= strchr(buffer
, '\n'))) {
1205 for (cp
= buffer
+ strlen (buffer
) - 1; cp
>= buffer
; cp
--)
1210 *sender
= getcpy (buffer
);
1215 * Copy message into a temporary file.
1216 * While copying, it will do some header processing
1217 * including the extraction of the envelope information.
1221 copy_message (int qd
, char *tmpfil
, int fold
)
1223 int i
, first
= 1, fd1
, fd2
;
1224 char buffer
[BUFSIZ
];
1227 strcpy (tmpfil
, m_tmpfil (invo_name
));
1229 /* open temporary file to put message in */
1230 if ((fd1
= open (tmpfil
, O_RDWR
| O_CREAT
| O_TRUNC
, 0600)) == -1)
1234 while ((i
= read (qd
, buffer
, sizeof(buffer
))) > 0)
1235 if (write (fd1
, buffer
, i
) != i
) {
1243 lseek (fd1
, (off_t
) 0, SEEK_SET
);
1247 /* dup the fd for incoming message */
1248 if ((fd2
= dup (qd
)) == -1) {
1253 /* now create a FILE pointer for it */
1254 if ((qfp
= fdopen (fd2
, "r")) == NULL
) {
1260 /* dup the fd for temporary file */
1261 if ((fd2
= dup (fd1
)) == -1) {
1267 /* now create a FILE pointer for it */
1268 if ((ffp
= fdopen (fd2
, "r+")) == NULL
) {
1276 * copy message into temporary file
1277 * and massage the headers. Save
1278 * a copy of the "From " line for later.
1280 i
= strlen ("From ");
1281 while (fgets (buffer
, sizeof(buffer
), qfp
)) {
1284 if (!strncmp (buffer
, "From ", i
)) {
1286 char *fp
, *cp
, *hp
, *ep
;
1288 /* get copy of envelope information ("From " line) */
1289 envelope
= getcpy (buffer
);
1292 /* First go ahead and put "From " line in message */
1293 fputs (buffer
, ffp
);
1300 * Now create a "Return-Path:" line
1301 * from the "From " line.
1303 hp
= cp
= strchr(fp
= envelope
+ i
, ' ');
1304 while ((hp
= strchr(++hp
, 'r')))
1305 if (uprf (hp
, "remote from")) {
1306 hp
= strrchr(hp
, ' ');
1310 /* return path for UUCP style addressing */
1311 ep
= strchr(++hp
, '\n');
1312 snprintf (buffer
, sizeof(buffer
), "Return-Path: %.*s!%.*s\n",
1313 ep
- hp
, hp
, cp
- fp
, fp
);
1315 /* return path for standard domain addressing */
1316 snprintf (buffer
, sizeof(buffer
), "Return-Path: %.*s\n",
1320 /* Add Return-Path header to message */
1321 fputs (buffer
, ffp
);
1325 /* Put the delivery date in message */
1334 fputs (buffer
, ffp
);
1346 lseek (fd1
, (off_t
) 0, SEEK_SET
);
1358 * Trim strings for pretty printing of debugging output
1364 char buffer
[BUFSIZ
*4];
1370 /* copy string into temp buffer */
1371 strncpy (buffer
, cp
, sizeof(buffer
));
1374 /* skip over leading whitespace */
1375 while (isspace(*bp
))
1378 /* start at the end and zap trailing whitespace */
1379 for (sp
= bp
+ strlen(bp
) - 1; sp
>= bp
; sp
--) {
1386 /* replace remaining whitespace with spaces */
1387 for (sp
= bp
; *sp
; sp
++)
1391 /* now return a copy */
1396 * Function for printing `verbose' messages.
1400 verbose_printf (char *fmt
, ...)
1405 vfprintf (stdout
, fmt
, ap
);
1408 fflush (stdout
); /* now flush output */
1413 * Function for printing `verbose' delivery
1418 adorn (char *what
, char *fmt
, ...)
1424 eindex
= errno
; /* save the errno */
1425 fprintf (stdout
, ", ");
1428 vfprintf (stdout
, fmt
, ap
);
1433 fprintf (stdout
, " %s: ", what
);
1434 if ((s
= strerror (eindex
)))
1435 fprintf (stdout
, "%s", s
);
1437 fprintf (stdout
, "Error %d", eindex
);
1440 fputc ('\n', stdout
);
1446 * Function for printing `debug' messages.
1450 debug_printf (char *fmt
, ...)
1455 vfprintf (stderr
, fmt
, ap
);
1461 * Check ndbm/db file(s) to see if the Message-Id of this
1462 * message matches the Message-Id of a previous message,
1463 * so we can discard it. If it doesn't match, we add the
1464 * Message-Id of this message to the ndbm/db file.
1467 suppress_duplicates (int fd
, char *file
)
1469 int fd1
, lockfd
, state
, result
;
1470 char *cp
, buf
[BUFSIZ
], name
[NAMESZ
];
1475 if ((fd1
= dup (fd
)) == -1)
1477 if (!(in
= fdopen (fd1
, "r"))) {
1483 for (state
= FLD
;;) {
1484 state
= m_getfld (state
, name
, buf
, sizeof(buf
), in
);
1489 /* Search for the message ID */
1490 if (strcasecmp (name
, "Message-ID")) {
1491 while (state
== FLDPLUS
)
1492 state
= m_getfld (state
, name
, buf
, sizeof(buf
), in
);
1496 cp
= add (buf
, NULL
);
1497 while (state
== FLDPLUS
) {
1498 state
= m_getfld (state
, name
, buf
, sizeof(buf
), in
);
1501 key
.dptr
= trimcpy (cp
);
1502 key
.dsize
= strlen (key
.dptr
) + 1;
1506 if (!(db
= dbm_open (file
, O_RDWR
| O_CREAT
, 0600))) {
1507 advise (file
, "unable to perform dbm_open on");
1513 * Since it is difficult to portable lock a ndbm file,
1514 * we will open and lock the Maildelivery file instead.
1515 * This will fail if your Maildelivery file doesn't
1518 if ((lockfd
= lkopen(file
, O_RDWR
, 0)) == -1) {
1519 advise (file
, "unable to perform file locking on");
1524 value
= dbm_fetch (db
, key
);
1527 verbose_printf ("Message-ID: %s\n already received on %s",
1531 value
.dptr
= ddate
+ sizeof("Delivery-Date:");
1532 value
.dsize
= strlen(value
.dptr
) + 1;
1533 if (dbm_store (db
, key
, value
, DBM_INSERT
))
1534 advise (file
, "possibly corrupt file");
1539 lkclose(lockfd
, file
);