]>
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>
37 # define UTMP_FILE _PATH_UTMP
39 # define UTMP_FILE "/etc/utmp"
43 static struct swit switches
[] = {
45 { "addr address", 0 },
51 { "sender address", 0 },
53 { "mailbox file", 0 },
55 { "home directory", -4 },
59 { "maildelivery file", 0 },
64 #define SUPPRESSDUP 10
66 #define NSUPPRESSDUP 11
67 { "nosuppressdup", 0 },
77 static int globbed
= 0; /* have we built "vars" table yet? */
78 static int parsed
= 0; /* have we built header field table yet */
79 static int utmped
= 0; /* have we scanned umtp(x) file yet */
80 static int suppressdup
= 0; /* are we suppressing duplicate messages? */
82 static int verbose
= 0;
85 static char *addr
= NULL
;
86 static char *user
= NULL
;
87 static char *info
= NULL
;
88 static char *file
= NULL
;
89 static char *sender
= NULL
;
90 static char *envelope
= NULL
; /* envelope information ("From " line) */
91 static char *mbox
= NULL
;
92 static char *home
= NULL
;
94 static struct passwd
*pw
; /* passwd file entry */
96 static char ddate
[BUFSIZ
]; /* record the delivery date */
101 /* flags for pair->p_flags */
103 #define P_ADR 0x01 /* field is address */
104 #define P_HID 0x02 /* special (fake) field */
116 * Lookup table for matching fields and patterns
117 * in messages. The rest of the table is added
118 * when the message is parsed.
120 static struct pair hdrs
[NVEC
+ 1] = {
121 { "source", NULL
, P_HID
},
122 { "addr", NULL
, P_HID
},
123 { "Return-Path", NULL
, P_ADR
},
124 { "Reply-To", NULL
, P_ADR
},
125 { "From", NULL
, P_ADR
},
126 { "Sender", NULL
, P_ADR
},
127 { "To", NULL
, P_ADR
},
128 { "cc", NULL
, P_ADR
},
129 { "Resent-Reply-To", NULL
, P_ADR
},
130 { "Resent-From", NULL
, P_ADR
},
131 { "Resent-Sender", NULL
, P_ADR
},
132 { "Resent-To", NULL
, P_ADR
},
133 { "Resent-cc", NULL
, P_ADR
},
138 * The list of builtin variables to expand in a string
139 * before it is executed by the "pipe" or "qpipe" action.
141 static struct pair vars
[] = {
142 { "sender", NULL
, P_NIL
},
143 { "address", NULL
, P_NIL
},
144 { "size", NULL
, P_NIL
},
145 { "reply-to", NULL
, P_CHK
},
146 { "info", NULL
, P_NIL
},
150 extern char **environ
;
155 static int localmail (int, char *);
156 static int usr_delivery (int, char *, int);
157 static int split (char *, char **);
158 static int parse (int);
159 static void expand (char *, char *, int);
160 static void glob (int);
161 static struct pair
*lookup (struct pair
*, char *);
162 static int logged_in (void);
163 static int timely (char *, char *);
164 static int usr_file (int, char *, int);
165 static int usr_pipe (int, char *, char *, char **, int);
166 static int usr_folder (int, char *);
167 static RETSIGTYPE
alrmser (int);
168 static void get_sender (char *, char **);
169 static int copy_message (int, char *, int);
170 static void verbose_printf (char *fmt
, ...);
171 static void adorn (char *, char *, ...);
172 static void debug_printf (char *fmt
, ...);
173 static int suppress_duplicates (int, char *);
174 static char *trim (char *);
178 main (int argc
, char **argv
)
182 char *cp
, *mdlvr
= NULL
, buf
[BUFSIZ
];
183 char mailbox
[BUFSIZ
], tmpfil
[BUFSIZ
];
184 char **argp
, **arguments
;
187 setlocale(LC_ALL
, "");
189 invo_name
= r1bindex (*argv
, '/');
191 /* foil search of user profile/context */
192 if (context_foil (NULL
) == -1)
195 mts_init (invo_name
);
196 arguments
= getarguments (invo_name
, argc
, argv
, 0);
199 /* Parse arguments */
200 while ((cp
= *argp
++)) {
202 switch (smatch (++cp
, switches
)) {
204 ambigsw (cp
, switches
);
207 adios (NULL
, "-%s unknown", cp
);
210 snprintf (buf
, sizeof(buf
),
211 "%s [switches] [address info sender]", invo_name
);
212 print_help (buf
, switches
, 0);
215 print_version(invo_name
);
219 if (!(addr
= *argp
++))/* allow -xyz arguments */
220 adios (NULL
, "missing argument to %s", argp
[-2]);
223 if (!(info
= *argp
++))/* allow -xyz arguments */
224 adios (NULL
, "missing argument to %s", argp
[-2]);
227 if (!(user
= *argp
++))/* allow -xyz arguments */
228 adios (NULL
, "missing argument to %s", argp
[-2]);
231 if (!(file
= *argp
++) || *file
== '-')
232 adios (NULL
, "missing argument to %s", argp
[-2]);
235 if (!(sender
= *argp
++))/* allow -xyz arguments */
236 adios (NULL
, "missing argument to %s", argp
[-2]);
239 if (!(mbox
= *argp
++) || *mbox
== '-')
240 adios (NULL
, "missing argument to %s", argp
[-2]);
243 if (!(home
= *argp
++) || *home
== '-')
244 adios (NULL
, "missing argument to %s", argp
[-2]);
248 if (!(cp
= *argp
++) || *cp
== '-')
249 adios (NULL
, "missing argument to %s", argp
[-2]);
251 adios (NULL
, "only one maildelivery file at a time!");
274 switch (argp
- (argv
+ 1)) {
290 addr
= getusername ();
292 user
= (cp
= strchr(addr
, '.')) ? ++cp
: addr
;
293 if ((pw
= getpwnam (user
)) == NULL
)
294 adios (NULL
, "no such local user as %s", user
);
296 if (chdir (pw
->pw_dir
) == -1)
300 if (geteuid() == 0) {
302 initgroups (pw
->pw_name
, pw
->pw_gid
);
309 setbuf (stdin
, NULL
);
311 /* Record the delivery time */
312 if ((now
= dlocaltimenow ()) == NULL
)
313 adios (NULL
, "unable to ascertain local time");
314 snprintf (ddate
, sizeof(ddate
), "Delivery-Date: %s\n", dtimenow (0));
317 * Copy the message to a temporary file
322 /* getting message from file */
323 if ((tempfd
= open (file
, O_RDONLY
)) == -1)
324 adios (file
, "unable to open");
326 debug_printf ("retrieving message from file \"%s\"\n", file
);
327 if ((fd
= copy_message (tempfd
, tmpfil
, 1)) == -1)
328 adios (NULL
, "unable to create temporary file");
331 /* getting message from stdin */
333 debug_printf ("retrieving message from stdin\n");
334 if ((fd
= copy_message (fileno (stdin
), tmpfil
, 1)) == -1)
335 adios (NULL
, "unable to create temporary file");
338 debug_printf ("temporary file=\"%s\"\n", tmpfil
);
342 if (!(fp
= fdopen (fd
, "r+")))
343 adios (NULL
, "unable to access temporary file");
346 * If no sender given, extract it
347 * from envelope information.
350 get_sender (envelope
, &sender
);
353 snprintf (mailbox
, sizeof(mailbox
), "%s/%s",
354 mmdfldir
[0] ? mmdfldir
: pw
->pw_dir
,
355 mmdflfil
[0] ? mmdflfil
: pw
->pw_name
);
362 debug_printf ("addr=\"%s\"\n", trim(addr
));
363 debug_printf ("user=\"%s\"\n", trim(user
));
364 debug_printf ("info=\"%s\"\n", trim(info
));
365 debug_printf ("sender=\"%s\"\n", trim(sender
));
366 debug_printf ("envelope=\"%s\"\n", envelope
? trim(envelope
) : "");
367 debug_printf ("mbox=\"%s\"\n", trim(mbox
));
368 debug_printf ("home=\"%s\"\n", trim(home
));
369 debug_printf ("ddate=\"%s\"\n", trim(ddate
));
370 debug_printf ("now=%02d:%02d\n\n", now
->tw_hour
, now
->tw_min
);
373 /* deliver the message */
374 status
= localmail (fd
, mdlvr
);
376 done (status
!= -1 ? RCV_MOK
: RCV_MBX
);
381 * Main routine for delivering message.
385 localmail (int fd
, char *mdlvr
)
387 /* check if this message is a duplicate */
389 suppress_duplicates(fd
, mdlvr
? mdlvr
: ".maildelivery") == DONE
)
392 /* delivery according to personal Maildelivery file */
393 if (usr_delivery (fd
, mdlvr
? mdlvr
: ".maildelivery", 0) != -1)
396 /* delivery according to global Maildelivery file */
397 if (usr_delivery (fd
, maildelivery
, 1) != -1)
401 verbose_printf ("(delivering to standard mail spool)\n");
403 /* last resort - deliver to standard mail spool */
405 return usr_file (fd
, mbox
, MBOX_FORMAT
);
407 return usr_file (fd
, mbox
, MMDF_FORMAT
);
412 #define matches(a,b) (stringdex (b, a) >= 0)
415 * Parse the delivery file, and process incoming message.
419 usr_delivery (int fd
, char *delivery
, int su
)
421 int i
, accept
, status
, won
, vecp
, next
;
422 char *field
, *pattern
, *action
, *result
, *string
;
423 char buffer
[BUFSIZ
], tmpbuf
[BUFSIZ
];
424 char *cp
, *vec
[NVEC
];
429 /* open the delivery file */
430 if ((fp
= fopen (delivery
, "r")) == NULL
)
433 /* check if delivery file has bad ownership or permissions */
434 if (fstat (fileno (fp
), &st
) == -1
435 || (st
.st_uid
!= 0 && (su
|| st
.st_uid
!= pw
->pw_uid
))
436 || st
.st_mode
& (S_IWGRP
|S_IWOTH
)) {
438 verbose_printf ("WARNING: %s has bad ownership/modes (su=%d,uid=%d,owner=%d,mode=0%o)\n",
439 delivery
, su
, (int) pw
->pw_uid
, (int) st
.st_uid
, (int) st
.st_mode
);
447 /* read and process delivery file */
448 while (fgets (buffer
, sizeof(buffer
), fp
)) {
449 /* skip comments and empty lines */
450 if (*buffer
== '#' || *buffer
== '\n')
453 /* zap trailing newline */
454 if ((cp
= strchr(buffer
, '\n')))
457 /* split buffer into fields */
458 vecp
= split (buffer
, vec
);
460 /* check for too few fields */
463 debug_printf ("WARNING: entry with only %d fields, skipping.\n", vecp
);
468 for (i
= 0; vec
[i
]; i
++)
469 debug_printf ("vec[%d]: \"%s\"\n", i
, trim(vec
[i
]));
478 /* find out how to perform the action */
483 * If previous condition failed, don't
484 * do this - else fall through
487 continue; /* else fall */
491 * If already delivered, skip this action. Else
492 * consider delivered if action is successful.
495 continue; /* else fall */
500 * Take action, and consider delivered if
501 * action is successful.
510 * Take action, but don't consider delivered, even
511 * if action is successful
518 if (!strcasecmp (vec
[5], "select")) {
519 if (logged_in () != -1)
521 if (vecp
> 7 && timely (vec
[6], vec
[7]) == -1)
526 /* check if the field matches */
534 * "default" matches only if the message hasn't
535 * been delivered yet.
537 if (!strcasecmp (field
, "default")) {
544 /* parse message and build lookup table */
545 if (!parsed
&& parse (fd
) == -1) {
550 * find header field in lookup table, and
551 * see if the pattern matches.
553 if ((p
= lookup (hdrs
, field
)) && (p
->p_value
!= NULL
)
554 && matches (p
->p_value
, pattern
)) {
563 /* find out the action to perform */
566 /* deliver to quoted pipe */
567 if (strcasecmp (action
, "qpipe"))
568 continue; /* else fall */
570 expand (tmpbuf
, string
, fd
);
571 if (split (tmpbuf
, vec
) < 1)
573 status
= usr_pipe (fd
, tmpbuf
, vec
[0], vec
, 0);
577 /* deliver to pipe */
578 if (strcasecmp (action
, "pipe"))
579 continue; /* else fall */
583 expand (tmpbuf
, string
, fd
);
586 status
= usr_pipe (fd
, tmpbuf
, "/bin/sh", vec
+ 2, 0);
591 if (!strcasecmp (action
, "file")) {
592 status
= usr_file (fd
, string
, MBOX_FORMAT
);
595 /* deliver to nmh folder */
596 else if (strcasecmp (action
, "folder"))
597 continue; /* else fall */
599 status
= usr_folder (fd
, string
);
604 if (!strcasecmp (action
, "mmdf")) {
605 status
= usr_file (fd
, string
, MMDF_FORMAT
);
609 else if (strcasecmp (action
, "mbox"))
610 continue; /* else fall */
614 status
= usr_file (fd
, string
, MBOX_FORMAT
);
619 if (strcasecmp (action
, "destroy"))
625 if (accept
&& status
== 0)
630 return (won
? 0 : -1);
637 * Split buffer into fields (delimited by whitespace or
638 * comma's). Return the number of fields found.
642 split (char *cp
, char **vec
)
649 /* split into a maximum of NVEC fields */
650 for (i
= 0; i
<= NVEC
;) {
653 /* zap any whitespace and comma's */
654 while (isspace (*s
) || *s
== ',')
657 /* end of buffer, time to leave */
661 /* get double quote text as a single field */
663 for (vec
[i
++] = ++s
; *s
&& *s
!= '"'; s
++) {
665 * Check for escaped double quote. We need
666 * to shift the string to remove slash.
674 if (*s
== '"') /* zap trailing double quote */
679 if (*s
== QUOTE
&& *++s
!= '"')
683 /* move forward to next field delimiter */
684 while (*s
&& !isspace (*s
) && *s
!= ',')
694 * Parse the headers of a message, and build the
695 * lookup table for matching fields and patterns.
704 char name
[NAMESZ
], field
[BUFSIZ
];
711 /* get a new FILE pointer to message */
712 if ((fd1
= dup (fd
)) == -1)
714 if ((in
= fdopen (fd1
, "r")) == NULL
) {
720 /* add special entries to lookup table */
721 if ((p
= lookup (hdrs
, "source")))
722 p
->p_value
= getcpy (sender
);
723 if ((p
= lookup (hdrs
, "addr")))
724 p
->p_value
= getcpy (addr
);
727 * Scan the headers of the message and build
730 for (i
= 0, state
= FLD
;;) {
731 switch (state
= m_getfld (state
, name
, field
, sizeof(field
), in
)) {
735 lp
= add (field
, NULL
);
736 while (state
== FLDPLUS
) {
737 state
= m_getfld (state
, name
, field
, sizeof(field
), in
);
738 lp
= add (field
, lp
);
740 for (p
= hdrs
; p
->p_name
; p
++) {
741 if (!strcasecmp (p
->p_name
, name
)) {
742 if (!(p
->p_flags
& P_HID
)) {
743 if ((cp
= p
->p_value
))
744 if (p
->p_flags
& P_ADR
) {
745 dp
= cp
+ strlen (cp
) - 1;
748 cp
= add (",\n\t", cp
);
752 p
->p_value
= add (lp
, cp
);
758 if (p
->p_name
== NULL
&& i
< NVEC
) {
759 p
->p_name
= getcpy (name
);
776 advise (NULL
, "format error in message");
780 advise (NULL
, "internal error in m_getfld");
788 if ((p
= lookup (vars
, "reply-to"))) {
789 if ((q
= lookup (hdrs
, "reply-to")) == NULL
|| q
->p_value
== NULL
)
790 q
= lookup (hdrs
, "from");
791 p
->p_value
= getcpy (q
? q
->p_value
: "");
792 p
->p_flags
&= ~P_CHK
;
794 debug_printf ("vars[%d]: name=\"%s\" value=\"%s\"\n",
795 p
- vars
, p
->p_name
, trim(p
->p_value
));
798 for (p
= hdrs
; p
->p_name
; p
++)
799 debug_printf ("hdrs[%d]: name=\"%s\" value=\"%s\"\n",
800 p
- hdrs
, p
->p_name
, p
->p_value
? trim(p
->p_value
) : "");
811 * Expand any builtin variables such as $(sender),
812 * $(address), etc., in a string.
816 expand (char *s1
, char *s2
, int fd
)
824 while ((c
= *s2
++)) {
825 if (c
!= '$' || *s2
!= LPAREN
) {
828 for (cp
= ++s2
; *s2
&& *s2
!= RPAREN
; s2
++)
835 if ((p
= lookup (vars
, cp
))) {
836 if (!parsed
&& (p
->p_flags
& P_CHK
))
839 strcpy (s1
, p
->p_value
);
849 * Fill in the information missing from the "vars"
850 * table, which is necessary to expand any builtin
851 * variables in the string for a "pipe" or "qpipe"
865 if ((p
= lookup (vars
, "sender")))
866 p
->p_value
= getcpy (sender
);
867 if ((p
= lookup (vars
, "address")))
868 p
->p_value
= getcpy (addr
);
869 if ((p
= lookup (vars
, "size"))) {
870 snprintf (buffer
, sizeof(buffer
), "%d",
871 fstat (fd
, &st
) != -1 ? (int) st
.st_size
: 0);
872 p
->p_value
= getcpy (buffer
);
874 if ((p
= lookup (vars
, "info")))
875 p
->p_value
= getcpy (info
);
878 for (p
= vars
; p
->p_name
; p
++)
879 debug_printf ("vars[%d]: name=\"%s\" value=\"%s\"\n",
880 p
- vars
, p
->p_name
, trim(p
->p_value
));
886 * Find a matching name in a lookup table. If found,
887 * return the "pairs" entry, else return NULL.
891 lookup (struct pair
*pairs
, char *key
)
893 for (; pairs
->p_name
; pairs
++)
894 if (!strcasecmp (pairs
->p_name
, key
))
902 * Check utmp(x) file to see if user is currently
915 if ((uf
= fopen (UTMP_FILE
, "r")) == NULL
)
918 while (fread ((char *) &ut
, sizeof(ut
), 1, uf
) == 1) {
919 if (ut
.ut_name
[0] != 0
920 && strncmp (user
, ut
.ut_name
, sizeof(ut
.ut_name
)) == 0) {
924 return (utmped
= DONE
);
929 return (utmped
= NOTOK
);
933 #define check(t,a,b) if (t < a || t > b) return -1
934 #define cmpar(h1,m1,h2,m2) if (h1 < h2 || (h1 == h2 && m1 < m2)) return 0
937 timely (char *t1
, char *t2
)
939 int t1hours
, t1mins
, t2hours
, t2mins
;
941 if (sscanf (t1
, "%d:%d", &t1hours
, &t1mins
) != 2)
943 check (t1hours
, 0, 23);
944 check (t1mins
, 0, 59);
946 if (sscanf (t2
, "%d:%d", &t2hours
, &t2mins
) != 2)
948 check (t2hours
, 0, 23);
949 check (t2mins
, 0, 59);
951 cmpar (now
->tw_hour
, now
->tw_min
, t1hours
, t1mins
);
952 cmpar (t2hours
, t2mins
, now
->tw_hour
, now
->tw_min
);
959 * Deliver message by appending to a file.
963 usr_file (int fd
, char *mailbox
, int mbx_style
)
968 verbose_printf ("delivering to file \"%s\"", mailbox
);
970 if (mbx_style
== MBOX_FORMAT
) {
972 verbose_printf (" (mbox style)");
976 verbose_printf (" (mmdf style)");
980 /* open and lock the file */
981 if ((md
= mbx_open (mailbox
, mbx_style
, pw
->pw_uid
, pw
->pw_gid
, m_gmprot())) == -1) {
983 adorn ("", "unable to open:");
987 lseek (fd
, (off_t
) 0, SEEK_SET
);
989 /* append message to file */
990 if (mbx_copy (mailbox
, mbx_style
, md
, fd
, mapping
, NULL
, verbose
) == -1) {
992 adorn ("", "error writing to:");
996 /* close and unlock file */
997 mbx_close (mailbox
, md
);
1000 verbose_printf (", success.\n");
1006 * Deliver message to a nmh folder.
1010 usr_folder (int fd
, char *string
)
1013 char folder
[BUFSIZ
], *vec
[3];
1015 /* get folder name ready */
1017 strncpy(folder
, string
, sizeof(folder
));
1019 snprintf(folder
, sizeof(folder
), "+%s", string
);
1022 verbose_printf ("delivering to folder \"%s\"", folder
+ 1);
1024 vec
[0] = "rcvstore";
1028 /* use rcvstore to put message in folder */
1029 status
= usr_pipe (fd
, "rcvstore", rcvstoreproc
, vec
, 1);
1033 * Currently, verbose status messages are handled by usr_pipe().
1037 verbose_printf (", success.\n");
1039 verbose_printf (", failed.\n");
1047 * Deliver message to a process.
1051 usr_pipe (int fd
, char *cmd
, char *pgm
, char **vec
, int suppress
)
1054 int i
, bytes
, seconds
, status
;
1057 if (verbose
&& !suppress
)
1058 verbose_printf ("delivering to pipe \"%s\"", cmd
);
1060 lseek (fd
, (off_t
) 0, SEEK_SET
);
1062 for (i
= 0; (child_id
= fork()) == -1 && i
< 5; i
++)
1069 adorn ("fork", "unable to");
1076 freopen ("/dev/null", "w", stdout
);
1077 freopen ("/dev/null", "w", stderr
);
1083 if ((fd
= open ("/dev/tty", O_RDWR
)) != -1) {
1084 ioctl (fd
, TIOCNOTTY
, NULL
);
1087 #endif /* TIOCNOTTY */
1089 setpgid ((pid_t
) 0, getpid ()); /* put in own process group */
1092 m_putenv ("USER", pw
->pw_name
);
1093 m_putenv ("HOME", pw
->pw_dir
);
1094 m_putenv ("SHELL", pw
->pw_shell
);
1100 /* parent process */
1101 if (!setjmp (myctx
)) {
1102 SIGNAL (SIGALRM
, alrmser
);
1103 bytes
= fstat (fd
, &st
) != -1 ? (int) st
.st_size
: 100;
1105 /* amount of time to wait depends on message size */
1107 /* give at least 5 minutes */
1109 } else if (bytes
>= 90000) {
1110 /* a half hour is long enough */
1113 seconds
= (bytes
/ 60) + 300;
1115 alarm ((unsigned int) seconds
);
1116 status
= pidwait (child_id
, 0);
1120 if (status
== RP_MOK
|| status
== RP_OK
)
1125 verbose_printf (", success.\n");
1127 if ((status
& 0xff00) == 0xff00)
1128 verbose_printf (", system error\n");
1130 pidstatus (status
, stdout
, ", failed");
1132 return (status
== 0 ? 0 : -1);
1135 * Ruthlessly kill the child and anything
1136 * else in its process group.
1138 KILLPG(child_id
, SIGKILL
);
1140 verbose_printf (", timed-out; terminated\n");
1150 #ifndef RELIABLE_SIGNALS
1151 SIGNAL (SIGALRM
, alrmser
);
1154 longjmp (myctx
, DONE
);
1159 * Get the `sender' from the envelope
1160 * information ("From " line).
1164 get_sender (char *envelope
, char **sender
)
1168 char buffer
[BUFSIZ
];
1170 if (envelope
== NULL
) {
1171 *sender
= getcpy ("");
1175 i
= strlen ("From ");
1176 strncpy (buffer
, envelope
+ i
, sizeof(buffer
));
1177 if ((cp
= strchr(buffer
, '\n'))) {
1187 for (cp
= buffer
+ strlen (buffer
) - 1; cp
>= buffer
; cp
--)
1192 *sender
= getcpy (buffer
);
1197 * Copy message into a temporary file.
1198 * While copying, it will do some header processing
1199 * including the extraction of the envelope information.
1203 copy_message (int qd
, char *tmpfil
, int fold
)
1205 int i
, first
= 1, fd1
, fd2
;
1206 char buffer
[BUFSIZ
];
1209 strcpy (tmpfil
, m_tmpfil (invo_name
));
1211 /* open temporary file to put message in */
1212 if ((fd1
= open (tmpfil
, O_RDWR
| O_CREAT
| O_TRUNC
, 0600)) == -1)
1216 while ((i
= read (qd
, buffer
, sizeof(buffer
))) > 0)
1217 if (write (fd1
, buffer
, i
) != i
) {
1225 lseek (fd1
, (off_t
) 0, SEEK_SET
);
1229 /* dup the fd for incoming message */
1230 if ((fd2
= dup (qd
)) == -1) {
1235 /* now create a FILE pointer for it */
1236 if ((qfp
= fdopen (fd2
, "r")) == NULL
) {
1242 /* dup the fd for temporary file */
1243 if ((fd2
= dup (fd1
)) == -1) {
1249 /* now create a FILE pointer for it */
1250 if ((ffp
= fdopen (fd2
, "r+")) == NULL
) {
1258 * copy message into temporary file
1259 * and massage the headers. Save
1260 * a copy of the "From " line for later.
1262 i
= strlen ("From ");
1263 while (fgets (buffer
, sizeof(buffer
), qfp
)) {
1266 if (!strncmp (buffer
, "From ", i
)) {
1268 char *fp
, *cp
, *hp
, *ep
;
1270 /* get copy of envelope information ("From " line) */
1271 envelope
= getcpy (buffer
);
1274 /* First go ahead and put "From " line in message */
1275 fputs (buffer
, ffp
);
1282 * Now create a "Return-Path:" line
1283 * from the "From " line.
1285 hp
= cp
= strchr(fp
= envelope
+ i
, ' ');
1286 while ((hp
= strchr(++hp
, 'r')))
1287 if (uprf (hp
, "remote from")) {
1288 hp
= strrchr(hp
, ' ');
1292 /* return path for UUCP style addressing */
1293 ep
= strchr(++hp
, '\n');
1294 snprintf (buffer
, sizeof(buffer
), "Return-Path: %.*s!%.*s\n",
1295 ep
- hp
, hp
, cp
- fp
, fp
);
1297 /* return path for standard domain addressing */
1298 snprintf (buffer
, sizeof(buffer
), "Return-Path: %.*s\n",
1302 /* Add Return-Path header to message */
1303 fputs (buffer
, ffp
);
1307 /* Put the delivery date in message */
1316 fputs (buffer
, ffp
);
1328 lseek (fd1
, (off_t
) 0, SEEK_SET
);
1340 * Trim strings for pretty printing of debugging output
1346 char buffer
[BUFSIZ
*4];
1352 /* copy string into temp buffer */
1353 strncpy (buffer
, cp
, sizeof(buffer
));
1356 /* skip over leading whitespace */
1357 while (isspace(*bp
))
1360 /* start at the end and zap trailing whitespace */
1361 for (sp
= bp
+ strlen(bp
) - 1; sp
>= bp
; sp
--) {
1368 /* replace remaining whitespace with spaces */
1369 for (sp
= bp
; *sp
; sp
++)
1373 /* now return a copy */
1378 * Function for printing `verbose' messages.
1382 verbose_printf (char *fmt
, ...)
1387 vfprintf (stdout
, fmt
, ap
);
1390 fflush (stdout
); /* now flush output */
1395 * Function for printing `verbose' delivery
1400 adorn (char *what
, char *fmt
, ...)
1406 eindex
= errno
; /* save the errno */
1407 fprintf (stdout
, ", ");
1410 vfprintf (stdout
, fmt
, ap
);
1415 fprintf (stdout
, " %s: ", what
);
1416 if ((s
= strerror (eindex
)))
1417 fprintf (stdout
, "%s", s
);
1419 fprintf (stdout
, "Error %d", eindex
);
1422 fputc ('\n', stdout
);
1428 * Function for printing `debug' messages.
1432 debug_printf (char *fmt
, ...)
1437 vfprintf (stderr
, fmt
, ap
);
1443 * Check ndbm/db file(s) to see if the Message-Id of this
1444 * message matches the Message-Id of a previous message,
1445 * so we can discard it. If it doesn't match, we add the
1446 * Message-Id of this message to the ndbm/db file.
1449 suppress_duplicates (int fd
, char *file
)
1451 int fd1
, lockfd
, state
, result
;
1452 char *cp
, buf
[BUFSIZ
], name
[NAMESZ
];
1457 if ((fd1
= dup (fd
)) == -1)
1459 if (!(in
= fdopen (fd1
, "r"))) {
1465 for (state
= FLD
;;) {
1466 state
= m_getfld (state
, name
, buf
, sizeof(buf
), in
);
1471 /* Search for the message ID */
1472 if (strcasecmp (name
, "Message-ID")) {
1473 while (state
== FLDPLUS
)
1474 state
= m_getfld (state
, name
, buf
, sizeof(buf
), in
);
1478 cp
= add (buf
, NULL
);
1479 while (state
== FLDPLUS
) {
1480 state
= m_getfld (state
, name
, buf
, sizeof(buf
), in
);
1483 key
.dptr
= trimcpy (cp
);
1484 key
.dsize
= strlen (key
.dptr
) + 1;
1488 if (!(db
= dbm_open (file
, O_RDWR
| O_CREAT
, 0600))) {
1489 advise (file
, "unable to perform dbm_open on");
1495 * Since it is difficult to portable lock a ndbm file,
1496 * we will open and lock the Maildelivery file instead.
1497 * This will fail if your Maildelivery file doesn't
1500 if ((lockfd
= lkopen(file
, O_RDWR
, 0)) == -1) {
1501 advise (file
, "unable to perform file locking on");
1506 value
= dbm_fetch (db
, key
);
1509 verbose_printf ("Message-ID: %s\n already received on %s",
1513 value
.dptr
= ddate
+ sizeof("Delivery-Date:");
1514 value
.dsize
= strlen(value
.dptr
) + 1;
1515 if (dbm_store (db
, key
, value
, DBM_INSERT
))
1516 advise (file
, "possibly corrupt file");
1521 lkclose(lockfd
, file
);