]>
diplodocus.org Git - nmh/blob - uip/slocal.c
1 /* slocal.c -- asynchronously filter and deliver new mail
3 * This code is Copyright (c) 2002, by the authors of nmh. See the
4 * COPYRIGHT file in the root directory of the nmh distribution for
5 * complete copyright information.
9 * Under sendmail, users should add the line
11 * "| /usr/local/nmh/lib/slocal"
13 * to their $HOME/.forward file.
17 /* Changed to use getutent() and friends. Assumes that when getutent() exists,
18 * a number of other things also exist. Please check.
19 * Ruud de Rooij <ruud@ruud.org> Sun, 28 May 2000 17:28:55 +0200
23 #include "sbr/pidstatus.h"
24 #include "sbr/print_version.h"
25 #include "sbr/print_help.h"
26 #include "sbr/error.h"
27 #include "h/dropsbr.h"
28 #include "h/rcvmail.h"
29 #include "h/signals.h"
35 #include "sbr/lock_file.h"
36 #include "sbr/m_mktemp.h"
39 #include <sys/ioctl.h>
42 /* Hopefully, grp.h declares initgroups(). If we run into a platform
43 where it doesn't, we could consider declaring it here as well. */
46 /* This define is needed for Berkeley db v2 and above to
47 * make the header file expose the 'historical' ndbm APIs.
48 * We define it unconditionally because this is simple and
51 #define DB_DBM_HSEARCH 1
53 #endif /* Use DB_DBM_HSEARCH to prevent warning from gcc -Wunused-macros. */
60 #endif /* HAVE_GETUTXENT */
62 #define SLOCAL_SWITCHES \
63 X("addr address", 0, ADDRSW) \
64 X("user name", 0, USERSW) \
65 X("file file", 0, FILESW) \
66 X("sender address", 0, SENDERSW) \
67 X("mailbox file", 0, MAILBOXSW) \
68 X("home directory", -4, HOMESW) \
69 X("info data", 0, INFOSW) \
70 X("maildelivery file", 0, MAILSW) \
71 X("verbose", 0, VERBSW) \
72 X("noverbose", 0, NVERBSW) \
73 X("suppressdup", 0, SUPPRESSDUP) \
74 X("nosuppressdup", 0, NSUPPRESSDUP) \
75 X("debug", 0, DEBUGSW) \
76 X("version", 0, VERSIONSW) \
77 X("help", 0, HELPSW) \
79 #define X(sw, minchars, id) id,
80 DEFINE_SWITCH_ENUM(SLOCAL
);
83 #define X(sw, minchars, id) { sw, minchars, id },
84 DEFINE_SWITCH_ARRAY(SLOCAL
, switches
);
87 static int globbed
= 0; /* have we built "vars" table yet? */
88 static int parsed
= 0; /* have we built header field table yet */
89 static int utmped
= 0; /* have we scanned umtp(x) file yet */
90 static bool suppressdup
; /* are we suppressing duplicate messages? */
95 static char *addr
= NULL
;
96 static char *user
= NULL
;
97 static char *info
= NULL
;
98 static char *file
= NULL
;
99 static char *sender
= NULL
;
100 static char *envelope
= NULL
; /* envelope information ("From " line) */
101 static char *mbox
= NULL
;
102 static char *home
= NULL
;
104 static struct passwd
*pw
; /* passwd file entry */
106 static char ddate
[BUFSIZ
]; /* record the delivery date */
109 static jmp_buf myctx
;
111 /* flags for pair->p_flags */
113 #define P_ADR 0x01 /* field is address */
114 #define P_HID 0x02 /* special (fake) field */
126 * Lookup table for matching fields and patterns
127 * in messages. The rest of the table is added
128 * when the message is parsed.
130 static struct pair hdrs
[NVEC
+ 1] = {
131 { "source", NULL
, P_HID
},
132 { "addr", NULL
, P_HID
},
133 { "Return-Path", NULL
, P_ADR
},
134 { "Reply-To", NULL
, P_ADR
},
135 { "From", NULL
, P_ADR
},
136 { "Sender", NULL
, P_ADR
},
137 { "To", NULL
, P_ADR
},
138 { "cc", NULL
, P_ADR
},
139 { "Resent-Reply-To", NULL
, P_ADR
},
140 { "Resent-From", NULL
, P_ADR
},
141 { "Resent-Sender", NULL
, P_ADR
},
142 { "Resent-To", NULL
, P_ADR
},
143 { "Resent-cc", NULL
, P_ADR
},
148 * The list of builtin variables to expand in a string
149 * before it is executed by the "pipe" or "qpipe" action.
151 static struct pair vars
[] = {
152 { "sender", NULL
, P_NIL
},
153 { "address", NULL
, P_NIL
},
154 { "size", NULL
, P_NIL
},
155 { "reply-to", NULL
, P_CHK
},
156 { "info", NULL
, P_NIL
},
160 extern char **environ
;
165 static int localmail (int, char *);
166 static int usr_delivery (int, char *, int);
167 static int split (char *, char **);
168 static int parse (int);
169 static void expand (char *, char *, int);
170 static void glob (int);
171 static struct pair
*lookup (struct pair
*, char *) PURE
;
172 static int logged_in (void);
173 static int timely (char *, char *);
174 static int usr_file (int, char *, int);
175 static int usr_pipe (int, char *, char *, char **, int);
176 static int usr_folder (int, char *);
177 static void alrmser (int);
178 static void get_sender (char *, char **);
179 static int copy_message (int, char *, int);
180 static void verbose_printf (char *fmt
, ...) CHECK_PRINTF(1, 2);
181 static void adorn (char *, char *, ...) CHECK_PRINTF(2, 3);
182 static void debug_printf (char *fmt
, ...) CHECK_PRINTF(1, 2);
183 static int suppress_duplicates (int, char *);
184 static char *trim (char *);
188 main (int argc
, char **argv
)
192 char *cp
, *mdlvr
= NULL
, buf
[BUFSIZ
];
193 char mailbox
[BUFSIZ
], tmpfil
[BUFSIZ
];
194 char **argp
, **arguments
;
196 if (nmh_init(argv
[0], false, false)) { return 1; }
199 arguments
= getarguments (invo_name
, argc
, argv
, 0);
202 /* Parse arguments */
203 while ((cp
= *argp
++)) {
205 switch (smatch (++cp
, switches
)) {
207 ambigsw (cp
, switches
);
210 die("-%s unknown", cp
);
213 snprintf (buf
, sizeof(buf
), "%s [switches]", invo_name
);
214 print_help (buf
, switches
, 0);
217 print_version(invo_name
);
221 if (!(addr
= *argp
++))/* allow -xyz arguments */
222 die("missing argument to %s", argp
[-2]);
225 if (!(info
= *argp
++))/* allow -xyz arguments */
226 die("missing argument to %s", argp
[-2]);
229 if (!(user
= *argp
++))/* allow -xyz arguments */
230 die("missing argument to %s", argp
[-2]);
233 if (!(file
= *argp
++) || *file
== '-')
234 die("missing argument to %s", argp
[-2]);
237 if (!(sender
= *argp
++))/* allow -xyz arguments */
238 die("missing argument to %s", argp
[-2]);
241 if (!(mbox
= *argp
++) || *mbox
== '-')
242 die("missing argument to %s", argp
[-2]);
245 if (!(home
= *argp
++) || *home
== '-')
246 die("missing argument to %s", argp
[-2]);
250 if (!(cp
= *argp
++) || *cp
== '-')
251 die("missing argument to %s", argp
[-2]);
253 die("only one maildelivery file at a time!");
275 die("only switch arguments are supported");
280 addr
= getusername ();
282 user
= getusername ();
284 if ((pw
= getpwnam (user
)) == NULL
)
285 die("no such local user as %s", user
);
287 if (chdir (pw
->pw_dir
) == -1)
288 if (chdir ("/") < 0) {
289 advise ("/", "chdir");
293 if (geteuid() == 0) {
294 if (setgid (pw
->pw_gid
) != 0) {
295 adios ("setgid", "unable to set group to %ld", (long) pw
->pw_gid
);
297 initgroups (pw
->pw_name
, pw
->pw_gid
);
298 if (setuid (pw
->pw_uid
) != 0) {
299 adios ("setuid", "unable to set user to %ld", (long) pw
->pw_uid
);
306 setbuf (stdin
, NULL
);
308 /* Record the delivery time */
309 if ((now
= dlocaltimenow ()) == NULL
)
310 die("unable to ascertain local time");
311 snprintf (ddate
, sizeof(ddate
), "Delivery-Date: %s\n", dtimenow (0));
314 * Copy the message to a temporary file
319 /* getting message from file */
320 if ((tempfd
= open (file
, O_RDONLY
)) == -1)
321 adios (file
, "unable to open");
323 debug_printf ("retrieving message from file \"%s\"\n", file
);
324 if ((fd
= copy_message (tempfd
, tmpfil
, 1)) == -1)
325 die("unable to create temporary file in %s",
329 /* getting message from stdin */
331 debug_printf ("retrieving message from stdin\n");
332 if ((fd
= copy_message (fileno (stdin
), tmpfil
, 1)) == -1)
333 die("unable to create temporary file in %s",
338 debug_printf ("temporary file=\"%s\"\n", tmpfil
);
340 /* Delete the temp file now or a copy of every single message passed through
341 slocal will be left in the /tmp directory until deleted manually! This
342 unlink() used to be under an 'else' of the 'if (debug)' above, but since
343 some people like to always run slocal with -debug and log the results,
344 the /tmp directory would get choked over time. Of course, now that we
345 always delete the temp file, the "temporary file=" message above is
346 somewhat pointless -- someone watching debug output wouldn't have a
347 chance to 'tail -f' or 'ln' the temp file before it's unlinked. The best
348 thing would be to delay this unlink() until later if debug == 1, but I'll
349 leave that for someone who cares about the temp-file-accessing
350 functionality (they'll have to watch out for cases where we adios()). */
351 (void) m_unlink (tmpfil
);
353 if (!(fp
= fdopen (fd
, "r+")))
354 die("unable to access temporary file");
357 * If no sender given, extract it
358 * from envelope information. */
360 get_sender (envelope
, &sender
);
363 snprintf (mailbox
, sizeof(mailbox
), "%s/%s",
364 mmdfldir
[0] ? mmdfldir
: pw
->pw_dir
,
365 mmdflfil
[0] ? mmdflfil
: pw
->pw_name
);
372 debug_printf ("addr=\"%s\"\n", trim(addr
));
373 debug_printf ("user=\"%s\"\n", trim(user
));
374 debug_printf ("info=\"%s\"\n", trim(info
));
375 debug_printf ("sender=\"%s\"\n", trim(sender
));
376 debug_printf ("envelope=\"%s\"\n", envelope
? trim(envelope
) : "");
377 debug_printf ("mbox=\"%s\"\n", trim(mbox
));
378 debug_printf ("home=\"%s\"\n", trim(home
));
379 debug_printf ("ddate=\"%s\"\n", trim(ddate
));
380 debug_printf ("now=%02d:%02d\n\n", now
->tw_hour
, now
->tw_min
);
383 /* deliver the message */
384 status
= localmail (fd
, mdlvr
);
386 done (status
!= -1 ? RCV_MOK
: RCV_MBX
);
392 * Main routine for delivering message.
396 localmail (int fd
, char *mdlvr
)
398 /* check if this message is a duplicate */
400 suppress_duplicates(fd
, mdlvr
? mdlvr
: ".maildelivery") == DONE
)
403 /* delivery according to personal Maildelivery file */
404 if (usr_delivery (fd
, mdlvr
? mdlvr
: ".maildelivery", 0) != -1)
407 /* delivery according to global Maildelivery file */
408 if (usr_delivery (fd
, maildelivery
, 1) != -1)
412 verbose_printf ("(delivering to standard mail spool)\n");
414 /* last resort - deliver to standard mail spool */
415 return usr_file (fd
, mbox
, MBOX_FORMAT
);
419 #define matches(a,b) (stringdex (b, a) >= 0)
422 * Parse the delivery file, and process incoming message.
426 usr_delivery (int fd
, char *delivery
, int su
)
434 char *field
, *pattern
, *action
, *result
, *string
;
435 char buffer
[BUFSIZ
], tmpbuf
[BUFSIZ
];
441 /* open the delivery file */
442 if ((fp
= fopen (delivery
, "r")) == NULL
)
445 /* check if delivery file has bad ownership or permissions */
446 if (fstat (fileno (fp
), &st
) == -1
447 || (st
.st_uid
!= 0 && (su
|| st
.st_uid
!= pw
->pw_uid
))
448 || st
.st_mode
& (S_IWGRP
|S_IWOTH
)) {
450 verbose_printf ("WARNING: %s has bad ownership/modes (su=%d,uid=%d,owner=%d,mode=0%o)\n",
451 delivery
, su
, (int) pw
->pw_uid
, (int) st
.st_uid
, (int) st
.st_mode
);
459 /* read and process delivery file */
460 while (fgets (buffer
, sizeof(buffer
), fp
)) {
461 /* skip comments and empty lines */
462 if (*buffer
== '#' || *buffer
== '\n')
465 trim_suffix_c(buffer
, '\n');
467 /* split buffer into fields */
468 vecp
= split (buffer
, vec
);
470 /* check for too few fields */
473 debug_printf ("WARNING: entry with only %d fields, skipping.\n", vecp
);
478 for (i
= 0; vec
[i
]; i
++)
479 debug_printf ("vec[%d]: \"%s\"\n", i
, trim(vec
[i
]));
488 /* find out how to perform the action */
493 * If previous condition failed, don't
494 * do this - else fall through
502 * If already delivered, skip this action. Else
503 * consider delivered if action is successful.
512 * Take action, and consider delivered if
513 * action is successful.
522 * Take action, but don't consider delivered, even
523 * if action is successful
530 if (!strcasecmp (vec
[5], "select")) {
531 if (logged_in () != -1)
533 if (vecp
> 7 && timely (vec
[6], vec
[7]) == -1)
538 /* check if the field matches */
546 * "default" matches only if the message hasn't
547 * been delivered yet.
549 if (!strcasecmp (field
, "default")) {
557 /* parse message and build lookup table */
558 if (!parsed
&& parse (fd
) == -1) {
563 * find header field in lookup table, and
564 * see if the pattern matches.
566 if ((p
= lookup (hdrs
, field
)) && (p
->p_value
!= NULL
)
567 && matches (p
->p_value
, pattern
)) {
576 /* find out the action to perform */
579 /* deliver to quoted pipe */
580 if (strcasecmp (action
, "qpipe"))
584 expand (tmpbuf
, string
, fd
);
585 if (split (tmpbuf
, vec
) < 1)
587 status
= usr_pipe (fd
, tmpbuf
, vec
[0], vec
, 0);
591 /* deliver to pipe */
592 if (strcasecmp (action
, "pipe"))
598 expand (tmpbuf
, string
, fd
);
601 status
= usr_pipe (fd
, tmpbuf
, "/bin/sh", vec
+ 2, 0);
606 if (!strcasecmp (action
, "file")) {
607 status
= usr_file (fd
, string
, MBOX_FORMAT
);
610 /* deliver to nmh folder */
611 else if (strcasecmp (action
, "folder"))
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"))
631 status
= usr_file (fd
, string
, MBOX_FORMAT
);
636 if (strcasecmp (action
, "destroy"))
642 if (status
) next
= false; /* action failed, mark for 'N' result */
644 if (accept
&& status
== 0)
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 ((unsigned char) *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 ((unsigned char) *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
[NMH_BUFSIZ
];
726 m_getfld_state_t gstate
;
731 /* get a new FILE pointer to message */
732 if ((fd1
= dup (fd
)) == -1)
734 if ((in
= fdopen (fd1
, "r")) == NULL
) {
740 /* add special entries to lookup table */
741 if ((p
= lookup (hdrs
, "source")))
742 p
->p_value
= getcpy (sender
);
743 if ((p
= lookup (hdrs
, "addr")))
744 p
->p_value
= getcpy (addr
);
747 * Scan the headers of the message and build
750 gstate
= m_getfld_state_init(in
);
752 int fieldsz
= sizeof field
;
753 switch (state
= m_getfld2(&gstate
, name
, field
, &fieldsz
)) {
756 lp
= mh_xstrdup(field
);
757 while (state
== FLDPLUS
) {
758 fieldsz
= sizeof field
;
759 state
= m_getfld2(&gstate
, name
, field
, &fieldsz
);
760 lp
= add (field
, lp
);
762 for (p
= hdrs
; p
->p_name
; p
++) {
763 if (!strcasecmp (p
->p_name
, name
)) {
764 if (!(p
->p_flags
& P_HID
)) {
765 if ((cp
= p
->p_value
)) {
766 if (p
->p_flags
& P_ADR
) {
767 dp
= cp
+ strlen (cp
) - 1;
770 cp
= add (",\n\t", cp
);
775 p
->p_value
= add (lp
, cp
);
781 if (p
->p_name
== NULL
&& i
< NVEC
) {
782 p
->p_name
= mh_xstrdup(name
);
796 inform("format error in message");
800 inform("internal error in m_getfld2");
806 m_getfld_state_destroy (&gstate
);
809 if ((p
= lookup (vars
, "reply-to"))) {
810 if ((q
= lookup (hdrs
, "reply-to")) == NULL
|| q
->p_value
== NULL
)
811 q
= lookup (hdrs
, "from");
812 p
->p_value
= getcpy (q
? q
->p_value
: "");
813 p
->p_flags
&= ~P_CHK
;
815 debug_printf ("vars[%td]: name=\"%s\" value=\"%s\"\n",
816 p
- vars
, p
->p_name
, trim(p
->p_value
));
819 for (p
= hdrs
; p
->p_name
; p
++)
820 debug_printf ("hdrs[%td]: name=\"%s\" value=\"%s\"\n",
821 p
- hdrs
, p
->p_name
, p
->p_value
? trim(p
->p_value
) : "");
832 * Expand any builtin variables such as $(sender),
833 * $(address), etc., in a string.
837 expand (char *s1
, char *s2
, int fd
)
845 while ((c
= *s2
++)) {
846 if (c
!= '$' || *s2
!= LPAREN
) {
849 for (cp
= ++s2
; *s2
&& *s2
!= RPAREN
; s2
++)
856 if ((p
= lookup (vars
, cp
))) {
857 if (!parsed
&& (p
->p_flags
& P_CHK
))
860 strcpy (s1
, p
->p_value
);
870 * Fill in the information missing from the "vars"
871 * table, which is necessary to expand any builtin
872 * variables in the string for a "pipe" or "qpipe"
886 if ((p
= lookup (vars
, "sender")))
887 p
->p_value
= getcpy (sender
);
888 if ((p
= lookup (vars
, "address")))
889 p
->p_value
= getcpy (addr
);
890 if ((p
= lookup (vars
, "size"))) {
891 snprintf (buffer
, sizeof(buffer
), "%d",
892 fstat (fd
, &st
) != -1 ? (int) st
.st_size
: 0);
893 p
->p_value
= mh_xstrdup(buffer
);
895 if ((p
= lookup (vars
, "info")))
896 p
->p_value
= getcpy (info
);
899 for (p
= vars
; p
->p_name
; p
++)
900 debug_printf ("vars[%td]: name=\"%s\" value=\"%s\"\n",
901 p
- vars
, p
->p_name
, trim(p
->p_value
));
907 * Find a matching name in a lookup table. If found,
908 * return the "pairs" entry, else return NULL.
912 lookup (struct pair
*pairs
, char *key
)
914 for (; pairs
->p_name
; pairs
++)
915 if (!strcasecmp (pairs
->p_name
, key
))
923 * Check utmp(x) file to see if user is currently
938 while ((utp
= getutxent()) != NULL
) {
939 if ( utp
->ut_type
== USER_PROCESS
&& utp
->ut_user
[0] != 0
940 && strncmp (user
, utp
->ut_user
, sizeof(utp
->ut_user
)) == 0) {
944 return utmped
= DONE
;
949 #endif /* HAVE_GETUTXENT */
950 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\" (%s style)", mailbox
,
989 mbx_style
== MBOX_FORMAT
? "mbox" : "mmdf");
992 /* open and lock the file */
993 if ((md
= mbx_open (mailbox
, mbx_style
, pw
->pw_uid
, pw
->pw_gid
, m_gmprot())) == -1) {
995 adorn ("", "unable to open:");
999 lseek(fd
, 0, SEEK_SET
);
1001 /* append message to file */
1002 if (mbx_copy (mailbox
, mbx_style
, md
, fd
, NULL
) == -1) {
1004 adorn ("", "error writing to:");
1008 /* close and unlock file */
1009 if (mbx_close (mailbox
, md
) == NOTOK
) {
1011 adorn ("", "error closing:");
1016 verbose_printf (", success.\n");
1022 * Deliver message to a nmh folder.
1026 usr_folder (int fd
, char *string
)
1029 char folder
[BUFSIZ
], *vec
[3];
1031 /* get folder name ready */
1033 strncpy(folder
, string
, sizeof(folder
));
1035 snprintf(folder
, sizeof(folder
), "+%s", string
);
1038 verbose_printf ("delivering to folder \"%s\"", folder
+ 1);
1040 vec
[0] = "rcvstore";
1044 /* use rcvstore to put message in folder */
1045 status
= usr_pipe (fd
, "rcvstore", rcvstoreproc
, vec
, 1);
1051 * Deliver message to a process.
1055 usr_pipe (int fd_arg
, char *cmd
, char *pgm
, char **vec
, int suppress
)
1057 volatile int fd
= fd_arg
;
1059 int bytes
, seconds
, status
;
1062 if (verbose
&& !suppress
)
1063 verbose_printf ("delivering to pipe \"%s\"", cmd
);
1065 lseek(fd
, 0, SEEK_SET
);
1072 adorn ("fork", "unable to");
1079 if (freopen ("/dev/null", "w", stdout
) == NULL
) {
1080 advise ("stdout", "freopen");
1082 if (freopen ("/dev/null", "w", stderr
) == NULL
) {
1083 advise ("stderr", "freopen");
1090 if ((fd
= open ("/dev/tty", O_RDWR
)) != -1) {
1091 ioctl (fd
, TIOCNOTTY
, NULL
);
1094 #endif /* TIOCNOTTY */
1096 setpgid(0, getpid()); /* put in own process group */
1099 setenv("USER", pw
->pw_name
, 1);
1100 setenv("HOME", pw
->pw_dir
, 1);
1101 setenv("SHELL", pw
->pw_shell
, 1);
1107 /* parent process */
1108 if (! setjmp (myctx
)) {
1109 SIGNAL (SIGALRM
, alrmser
);
1110 bytes
= fstat (fd
, &st
) != -1 ? (int) st
.st_size
: 100;
1112 /* amount of time to wait depends on message size */
1114 /* give at least 5 minutes */
1116 } else if (bytes
>= 90000) {
1117 /* a half hour is long enough */
1120 seconds
= (bytes
/ 60) + 300;
1122 alarm ((unsigned int) seconds
);
1123 status
= pidwait (child_id
, 0);
1128 verbose_printf (", success.\n");
1129 else if ((status
& 0xff00) == 0xff00)
1130 verbose_printf (", system error\n");
1132 pidstatus (status
, stdout
, ", failed");
1134 return status
== 0 ? 0 : -1;
1137 * Ruthlessly kill the child and anything
1138 * else in its process group.
1140 killpg(child_id
, SIGKILL
);
1142 verbose_printf (", timed-out; terminated\n");
1153 longjmp (myctx
, DONE
);
1158 * Get the `sender' from the envelope
1159 * information ("From " line).
1163 get_sender (char *envelope
, char **sender
)
1167 char buffer
[BUFSIZ
];
1169 if (envelope
== NULL
) {
1170 *sender
= mh_xstrdup("");
1175 strncpy (buffer
, envelope
+ i
, sizeof(buffer
));
1176 if ((cp
= strchr(buffer
, '\n'))) {
1186 for (cp
= buffer
+ strlen (buffer
) - 1; cp
>= buffer
; cp
--)
1187 if (isspace ((unsigned char) *cp
))
1191 *sender
= mh_xstrdup(buffer
);
1196 * Copy message into a temporary file.
1197 * While copying, it will do some header processing
1198 * including the extraction of the envelope information.
1202 copy_message (int qd
, char *tmpfil
, int fold
)
1204 int i
, first
= 1, fd1
, fd2
;
1205 char buffer
[BUFSIZ
];
1209 tfile
= m_mktemp2(NULL
, invo_name
, &fd1
, NULL
);
1210 if (tfile
== NULL
) return -1;
1211 strncpy (tmpfil
, tfile
, BUFSIZ
);
1214 while ((i
= read (qd
, buffer
, sizeof(buffer
))) > 0)
1215 if (write (fd1
, buffer
, i
) != i
) {
1218 (void) m_unlink (tmpfil
);
1223 lseek(fd1
, 0, SEEK_SET
);
1227 /* dup the fd for incoming message */
1228 if ((fd2
= dup (qd
)) == -1) {
1233 /* now create a FILE pointer for it */
1234 if ((qfp
= fdopen (fd2
, "r")) == NULL
) {
1240 /* dup the fd for temporary file */
1241 if ((fd2
= dup (fd1
)) == -1) {
1247 /* now create a FILE pointer for it */
1248 if ((ffp
= fdopen (fd2
, "r+")) == NULL
) {
1256 * copy message into temporary file
1257 * and massage the headers. Save
1258 * a copy of the "From " line for later.
1260 while (fgets (buffer
, sizeof(buffer
), qfp
)) {
1263 if (has_prefix(buffer
, "From ")) {
1264 /* get copy of envelope information ("From " line) */
1265 envelope
= mh_xstrdup(buffer
);
1267 /* Put the delivery date in message */
1276 fputs (buffer
, ffp
);
1288 lseek(fd1
, 0, SEEK_SET
);
1300 * Trim strings for pretty printing of debugging output
1306 static char buffer
[BUFSIZ
* 4];
1312 /* copy string into temp buffer */
1313 strncpy (buffer
, cp
, sizeof(buffer
));
1316 /* skip over leading whitespace */
1317 while (isspace((unsigned char) *bp
))
1320 /* start at the end and zap trailing whitespace */
1321 for (sp
= bp
+ strlen(bp
) - 1; sp
>= bp
; sp
--) {
1322 if (isspace((unsigned char) *sp
))
1328 /* replace remaining whitespace with spaces */
1329 for (sp
= bp
; *sp
; sp
++)
1330 if (isspace((unsigned char) *sp
))
1337 * Function for printing `verbose' messages.
1341 verbose_printf (char *fmt
, ...)
1349 fflush (stdout
); /* now flush output */
1354 * Function for printing `verbose' delivery
1359 adorn (char *what
, char *fmt
, ...)
1365 eindex
= errno
; /* save the errno */
1366 fputs(", ", stdout
);
1374 printf(" %s: ", what
);
1375 if ((s
= strerror (eindex
)))
1378 printf("Error %d", eindex
);
1387 * Function for printing `debug' messages.
1391 debug_printf (char *fmt
, ...)
1396 vfprintf (stderr
, fmt
, ap
);
1402 * Check ndbm/db file(s) to see if the Message-Id of this
1403 * message matches the Message-Id of a previous message,
1404 * so we can discard it. If it doesn't match, we add the
1405 * Message-Id of this message to the ndbm/db file.
1408 suppress_duplicates (int fd
, char *file
)
1410 int fd1
, lockfd
, state
, result
;
1411 char *cp
, buf
[NMH_BUFSIZ
], name
[NAMESZ
];
1415 m_getfld_state_t gstate
;
1417 if ((fd1
= dup (fd
)) == -1)
1419 if (!(in
= fdopen (fd1
, "r"))) {
1425 gstate
= m_getfld_state_init(in
);
1427 int failed_to_lock
= 0;
1428 int bufsz
= sizeof buf
;
1429 state
= m_getfld2(&gstate
, name
, buf
, &bufsz
);
1433 /* Search for the message ID */
1434 if (strcasecmp (name
, "Message-ID")) {
1435 while (state
== FLDPLUS
) {
1437 state
= m_getfld2(&gstate
, name
, buf
, &bufsz
);
1442 cp
= mh_xstrdup(buf
);
1443 while (state
== FLDPLUS
) {
1445 state
= m_getfld2(&gstate
, name
, buf
, &bufsz
);
1448 key
.dptr
= trimcpy (cp
);
1449 key
.dsize
= strlen (key
.dptr
) + 1;
1453 if (!(db
= dbm_open (file
, O_RDWR
| O_CREAT
, 0600))) {
1454 advise (file
, "unable to perform dbm_open on");
1460 * Since it is difficult to portable lock a ndbm file,
1461 * we will open and lock the Maildelivery file instead.
1462 * This will fail if your Maildelivery file doesn't
1465 if ((lockfd
= lkopendata(file
, O_RDWR
, 0, &failed_to_lock
))
1467 advise (file
, "unable to perform file locking on");
1472 value
= dbm_fetch (db
, key
);
1475 verbose_printf ("Message-ID: %s\n already received on %s",
1479 value
.dptr
= ddate
+ sizeof("Delivery-Date:");
1480 value
.dsize
= strlen(value
.dptr
) + 1;
1481 if (dbm_store (db
, key
, value
, DBM_INSERT
))
1482 advise (file
, "possibly corrupt file");
1487 lkclosedata(lockfd
, file
);
1504 m_getfld_state_destroy (&gstate
);