]>
diplodocus.org Git - nmh/blob - uip/slocal.c
6cace00f2ac529d4ef0c192b88476b543235e109
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/getcpy.h"
24 #include "sbr/ambigsw.h"
25 #include "sbr/pidstatus.h"
26 #include "sbr/print_version.h"
27 #include "sbr/print_help.h"
28 #include "sbr/error.h"
29 #include "h/dropsbr.h"
30 #include "h/rcvmail.h"
31 #include "h/signals.h"
37 #include "sbr/lock_file.h"
38 #include "sbr/m_mktemp.h"
41 #include <sys/ioctl.h>
44 /* Hopefully, grp.h declares initgroups(). If we run into a platform
45 where it doesn't, we could consider declaring it here as well. */
48 /* This define is needed for Berkeley db v2 and above to
49 * make the header file expose the 'historical' ndbm APIs.
50 * We define it unconditionally because this is simple and
53 #define DB_DBM_HSEARCH 1
55 #endif /* Use DB_DBM_HSEARCH to prevent warning from gcc -Wunused-macros. */
62 #endif /* HAVE_GETUTXENT */
64 #define SLOCAL_SWITCHES \
65 X("addr address", 0, ADDRSW) \
66 X("user name", 0, USERSW) \
67 X("file file", 0, FILESW) \
68 X("sender address", 0, SENDERSW) \
69 X("mailbox file", 0, MAILBOXSW) \
70 X("home directory", -4, HOMESW) \
71 X("info data", 0, INFOSW) \
72 X("maildelivery file", 0, MAILSW) \
73 X("verbose", 0, VERBSW) \
74 X("noverbose", 0, NVERBSW) \
75 X("suppressdup", 0, SUPPRESSDUP) \
76 X("nosuppressdup", 0, NSUPPRESSDUP) \
77 X("debug", 0, DEBUGSW) \
78 X("version", 0, VERSIONSW) \
79 X("help", 0, HELPSW) \
81 #define X(sw, minchars, id) id,
82 DEFINE_SWITCH_ENUM(SLOCAL
);
85 #define X(sw, minchars, id) { sw, minchars, id },
86 DEFINE_SWITCH_ARRAY(SLOCAL
, switches
);
89 static int globbed
= 0; /* have we built "vars" table yet? */
90 static int parsed
= 0; /* have we built header field table yet */
91 static int utmped
= 0; /* have we scanned umtp(x) file yet */
92 static bool suppressdup
; /* are we suppressing duplicate messages? */
97 static char *addr
= NULL
;
98 static char *user
= NULL
;
99 static char *info
= NULL
;
100 static char *file
= NULL
;
101 static char *sender
= NULL
;
102 static char *envelope
= NULL
; /* envelope information ("From " line) */
103 static char *mbox
= NULL
;
104 static char *home
= NULL
;
106 static struct passwd
*pw
; /* passwd file entry */
108 static char ddate
[BUFSIZ
]; /* record the delivery date */
111 static jmp_buf myctx
;
113 /* flags for pair->p_flags */
115 #define P_ADR 0x01 /* field is address */
116 #define P_HID 0x02 /* special (fake) field */
128 * Lookup table for matching fields and patterns
129 * in messages. The rest of the table is added
130 * when the message is parsed.
132 static struct pair hdrs
[NVEC
+ 1] = {
133 { "source", NULL
, P_HID
},
134 { "addr", NULL
, P_HID
},
135 { "Return-Path", NULL
, P_ADR
},
136 { "Reply-To", NULL
, P_ADR
},
137 { "From", NULL
, P_ADR
},
138 { "Sender", NULL
, P_ADR
},
139 { "To", NULL
, P_ADR
},
140 { "cc", NULL
, P_ADR
},
141 { "Resent-Reply-To", NULL
, P_ADR
},
142 { "Resent-From", NULL
, P_ADR
},
143 { "Resent-Sender", NULL
, P_ADR
},
144 { "Resent-To", NULL
, P_ADR
},
145 { "Resent-cc", NULL
, P_ADR
},
150 * The list of builtin variables to expand in a string
151 * before it is executed by the "pipe" or "qpipe" action.
153 static struct pair vars
[] = {
154 { "sender", NULL
, P_NIL
},
155 { "address", NULL
, P_NIL
},
156 { "size", NULL
, P_NIL
},
157 { "reply-to", NULL
, P_CHK
},
158 { "info", NULL
, P_NIL
},
162 extern char **environ
;
167 static int localmail (int, char *);
168 static int usr_delivery (int, char *, int);
169 static int split (char *, char **);
170 static int parse (int);
171 static void expand (char *, char *, int);
172 static void glob (int);
173 static struct pair
*lookup (struct pair
*, char *) PURE
;
174 static int logged_in (void);
175 static int timely (char *, char *);
176 static int usr_file (int, char *, int);
177 static int usr_pipe (int, char *, char *, char **, int);
178 static int usr_folder (int, char *);
179 static void alrmser (int);
180 static void get_sender (char *, char **);
181 static int copy_message (int, char *, int);
182 static void verbose_printf (char *fmt
, ...) CHECK_PRINTF(1, 2);
183 static void adorn (char *, char *, ...) CHECK_PRINTF(2, 3);
184 static void debug_printf (char *fmt
, ...) CHECK_PRINTF(1, 2);
185 static int suppress_duplicates (int, char *);
186 static char *trim (char *);
190 main (int argc
, char **argv
)
194 char *cp
, *mdlvr
= NULL
, buf
[BUFSIZ
];
195 char mailbox
[BUFSIZ
], tmpfil
[BUFSIZ
];
196 char **argp
, **arguments
;
198 if (nmh_init(argv
[0], false, false)) { return 1; }
201 arguments
= getarguments (invo_name
, argc
, argv
, 0);
204 /* Parse arguments */
205 while ((cp
= *argp
++)) {
207 switch (smatch (++cp
, switches
)) {
209 ambigsw (cp
, switches
);
212 die("-%s unknown", cp
);
215 snprintf (buf
, sizeof(buf
), "%s [switches]", invo_name
);
216 print_help (buf
, switches
, 0);
219 print_version(invo_name
);
223 if (!(addr
= *argp
++))/* allow -xyz arguments */
224 die("missing argument to %s", argp
[-2]);
227 if (!(info
= *argp
++))/* allow -xyz arguments */
228 die("missing argument to %s", argp
[-2]);
231 if (!(user
= *argp
++))/* allow -xyz arguments */
232 die("missing argument to %s", argp
[-2]);
235 if (!(file
= *argp
++) || *file
== '-')
236 die("missing argument to %s", argp
[-2]);
239 if (!(sender
= *argp
++))/* allow -xyz arguments */
240 die("missing argument to %s", argp
[-2]);
243 if (!(mbox
= *argp
++) || *mbox
== '-')
244 die("missing argument to %s", argp
[-2]);
247 if (!(home
= *argp
++) || *home
== '-')
248 die("missing argument to %s", argp
[-2]);
252 if (!(cp
= *argp
++) || *cp
== '-')
253 die("missing argument to %s", argp
[-2]);
255 die("only one maildelivery file at a time!");
277 die("only switch arguments are supported");
282 addr
= getusername ();
284 user
= getusername ();
286 if ((pw
= getpwnam (user
)) == NULL
)
287 die("no such local user as %s", user
);
289 if (chdir (pw
->pw_dir
) == -1)
290 if (chdir ("/") < 0) {
291 advise ("/", "chdir");
295 if (geteuid() == 0) {
296 if (setgid (pw
->pw_gid
) != 0) {
297 adios ("setgid", "unable to set group to %ld", (long) pw
->pw_gid
);
299 initgroups (pw
->pw_name
, pw
->pw_gid
);
300 if (setuid (pw
->pw_uid
) != 0) {
301 adios ("setuid", "unable to set user to %ld", (long) pw
->pw_uid
);
308 setbuf (stdin
, NULL
);
310 /* Record the delivery time */
311 if ((now
= dlocaltimenow ()) == NULL
)
312 die("unable to ascertain local time");
313 snprintf (ddate
, sizeof(ddate
), "Delivery-Date: %s\n", dtimenow (0));
316 * Copy the message to a temporary file
321 /* getting message from file */
322 if ((tempfd
= open (file
, O_RDONLY
)) == -1)
323 adios (file
, "unable to open");
325 debug_printf ("retrieving message from file \"%s\"\n", file
);
326 if ((fd
= copy_message (tempfd
, tmpfil
, 1)) == -1)
327 die("unable to create temporary file in %s",
331 /* getting message from stdin */
333 debug_printf ("retrieving message from stdin\n");
334 if ((fd
= copy_message (fileno (stdin
), tmpfil
, 1)) == -1)
335 die("unable to create temporary file in %s",
340 debug_printf ("temporary file=\"%s\"\n", tmpfil
);
342 /* Delete the temp file now or a copy of every single message passed through
343 slocal will be left in the /tmp directory until deleted manually! This
344 unlink() used to be under an 'else' of the 'if (debug)' above, but since
345 some people like to always run slocal with -debug and log the results,
346 the /tmp directory would get choked over time. Of course, now that we
347 always delete the temp file, the "temporary file=" message above is
348 somewhat pointless -- someone watching debug output wouldn't have a
349 chance to 'tail -f' or 'ln' the temp file before it's unlinked. The best
350 thing would be to delay this unlink() until later if debug == 1, but I'll
351 leave that for someone who cares about the temp-file-accessing
352 functionality (they'll have to watch out for cases where we adios()). */
353 (void) m_unlink (tmpfil
);
355 if (!(fp
= fdopen (fd
, "r+")))
356 die("unable to access temporary file");
359 * If no sender given, extract it
360 * from envelope information. */
362 get_sender (envelope
, &sender
);
365 snprintf (mailbox
, sizeof(mailbox
), "%s/%s",
366 mmdfldir
[0] ? mmdfldir
: pw
->pw_dir
,
367 mmdflfil
[0] ? mmdflfil
: pw
->pw_name
);
374 debug_printf ("addr=\"%s\"\n", trim(addr
));
375 debug_printf ("user=\"%s\"\n", trim(user
));
376 debug_printf ("info=\"%s\"\n", trim(info
));
377 debug_printf ("sender=\"%s\"\n", trim(sender
));
378 debug_printf ("envelope=\"%s\"\n", envelope
? trim(envelope
) : "");
379 debug_printf ("mbox=\"%s\"\n", trim(mbox
));
380 debug_printf ("home=\"%s\"\n", trim(home
));
381 debug_printf ("ddate=\"%s\"\n", trim(ddate
));
382 debug_printf ("now=%02d:%02d\n\n", now
->tw_hour
, now
->tw_min
);
385 /* deliver the message */
386 status
= localmail (fd
, mdlvr
);
388 done (status
!= -1 ? RCV_MOK
: RCV_MBX
);
394 * Main routine for delivering message.
398 localmail (int fd
, char *mdlvr
)
400 /* check if this message is a duplicate */
402 suppress_duplicates(fd
, mdlvr
? mdlvr
: ".maildelivery") == DONE
)
405 /* delivery according to personal Maildelivery file */
406 if (usr_delivery (fd
, mdlvr
? mdlvr
: ".maildelivery", 0) != -1)
409 /* delivery according to global Maildelivery file */
410 if (usr_delivery (fd
, maildelivery
, 1) != -1)
414 verbose_printf ("(delivering to standard mail spool)\n");
416 /* last resort - deliver to standard mail spool */
417 return usr_file (fd
, mbox
, MBOX_FORMAT
);
421 #define matches(a,b) (stringdex (b, a) >= 0)
424 * Parse the delivery file, and process incoming message.
428 usr_delivery (int fd
, char *delivery
, int su
)
436 char *field
, *pattern
, *action
, *result
, *string
;
437 char buffer
[BUFSIZ
], tmpbuf
[BUFSIZ
];
443 /* open the delivery file */
444 if ((fp
= fopen (delivery
, "r")) == NULL
)
447 /* check if delivery file has bad ownership or permissions */
448 if (fstat (fileno (fp
), &st
) == -1
449 || (st
.st_uid
!= 0 && (su
|| st
.st_uid
!= pw
->pw_uid
))
450 || st
.st_mode
& (S_IWGRP
|S_IWOTH
)) {
452 verbose_printf ("WARNING: %s has bad ownership/modes (su=%d,uid=%d,owner=%d,mode=0%o)\n",
453 delivery
, su
, (int) pw
->pw_uid
, (int) st
.st_uid
, (int) st
.st_mode
);
461 /* read and process delivery file */
462 while (fgets (buffer
, sizeof(buffer
), fp
)) {
463 /* skip comments and empty lines */
464 if (*buffer
== '#' || *buffer
== '\n')
467 trim_suffix_c(buffer
, '\n');
469 /* split buffer into fields */
470 vecp
= split (buffer
, vec
);
472 /* check for too few fields */
475 debug_printf ("WARNING: entry with only %d fields, skipping.\n", vecp
);
480 for (i
= 0; vec
[i
]; i
++)
481 debug_printf ("vec[%d]: \"%s\"\n", i
, trim(vec
[i
]));
490 /* find out how to perform the action */
495 * If previous condition failed, don't
496 * do this - else fall through
504 * If already delivered, skip this action. Else
505 * consider delivered if action is successful.
514 * Take action, and consider delivered if
515 * action is successful.
524 * Take action, but don't consider delivered, even
525 * if action is successful
532 if (!strcasecmp (vec
[5], "select")) {
533 if (logged_in () != -1)
535 if (vecp
> 7 && timely (vec
[6], vec
[7]) == -1)
540 /* check if the field matches */
548 * "default" matches only if the message hasn't
549 * been delivered yet.
551 if (!strcasecmp (field
, "default")) {
559 /* parse message and build lookup table */
560 if (!parsed
&& parse (fd
) == -1) {
565 * find header field in lookup table, and
566 * see if the pattern matches.
568 if ((p
= lookup (hdrs
, field
)) && (p
->p_value
!= NULL
)
569 && matches (p
->p_value
, pattern
)) {
578 /* find out the action to perform */
581 /* deliver to quoted pipe */
582 if (strcasecmp (action
, "qpipe"))
586 expand (tmpbuf
, string
, fd
);
587 if (split (tmpbuf
, vec
) < 1)
589 status
= usr_pipe (fd
, tmpbuf
, vec
[0], vec
, 0);
593 /* deliver to pipe */
594 if (strcasecmp (action
, "pipe"))
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"))
617 status
= usr_folder (fd
, string
);
622 if (!strcasecmp (action
, "mmdf")) {
623 status
= usr_file (fd
, string
, MMDF_FORMAT
);
627 else if (strcasecmp (action
, "mbox"))
633 status
= usr_file (fd
, string
, MBOX_FORMAT
);
638 if (strcasecmp (action
, "destroy"))
644 if (status
) next
= false; /* action failed, mark for 'N' result */
646 if (accept
&& status
== 0)
658 * Split buffer into fields (delimited by whitespace or
659 * comma's). Return the number of fields found.
663 split (char *cp
, char **vec
)
670 /* split into a maximum of NVEC fields */
671 for (i
= 0; i
<= NVEC
;) {
674 /* zap any whitespace and comma's */
675 while (isspace ((unsigned char) *s
) || *s
== ',')
678 /* end of buffer, time to leave */
682 /* get double quote text as a single field */
684 for (vec
[i
++] = ++s
; *s
&& *s
!= '"'; s
++) {
686 * Check for escaped double quote. We need
687 * to shift the string to remove slash.
695 if (*s
== '"') /* zap trailing double quote */
700 if (*s
== QUOTE
&& *++s
!= '"')
704 /* move forward to next field delimiter */
705 while (*s
&& !isspace ((unsigned char) *s
) && *s
!= ',')
715 * Parse the headers of a message, and build the
716 * lookup table for matching fields and patterns.
725 char name
[NAMESZ
], field
[NMH_BUFSIZ
];
728 m_getfld_state_t gstate
;
733 /* get a new FILE pointer to message */
734 if ((fd1
= dup (fd
)) == -1)
736 if ((in
= fdopen (fd1
, "r")) == NULL
) {
742 /* add special entries to lookup table */
743 if ((p
= lookup (hdrs
, "source")))
744 p
->p_value
= getcpy (sender
);
745 if ((p
= lookup (hdrs
, "addr")))
746 p
->p_value
= getcpy (addr
);
749 * Scan the headers of the message and build
752 gstate
= m_getfld_state_init(in
);
754 int fieldsz
= sizeof field
;
755 switch (state
= m_getfld2(&gstate
, name
, field
, &fieldsz
)) {
758 lp
= mh_xstrdup(field
);
759 while (state
== FLDPLUS
) {
760 fieldsz
= sizeof field
;
761 state
= m_getfld2(&gstate
, name
, field
, &fieldsz
);
762 lp
= add (field
, lp
);
764 for (p
= hdrs
; p
->p_name
; p
++) {
765 if (!strcasecmp (p
->p_name
, name
)) {
766 if (!(p
->p_flags
& P_HID
)) {
767 if ((cp
= p
->p_value
)) {
768 if (p
->p_flags
& P_ADR
) {
769 dp
= cp
+ strlen (cp
) - 1;
772 cp
= add (",\n\t", cp
);
777 p
->p_value
= add (lp
, cp
);
783 if (p
->p_name
== NULL
&& i
< NVEC
) {
784 p
->p_name
= mh_xstrdup(name
);
798 inform("format error in message");
802 inform("internal error in m_getfld2");
808 m_getfld_state_destroy (&gstate
);
811 if ((p
= lookup (vars
, "reply-to"))) {
812 if ((q
= lookup (hdrs
, "reply-to")) == NULL
|| q
->p_value
== NULL
)
813 q
= lookup (hdrs
, "from");
814 p
->p_value
= getcpy (q
? q
->p_value
: "");
815 p
->p_flags
&= ~P_CHK
;
817 debug_printf ("vars[%td]: name=\"%s\" value=\"%s\"\n",
818 p
- vars
, p
->p_name
, trim(p
->p_value
));
821 for (p
= hdrs
; p
->p_name
; p
++)
822 debug_printf ("hdrs[%td]: name=\"%s\" value=\"%s\"\n",
823 p
- hdrs
, p
->p_name
, p
->p_value
? trim(p
->p_value
) : "");
834 * Expand any builtin variables such as $(sender),
835 * $(address), etc., in a string.
839 expand (char *s1
, char *s2
, int fd
)
847 while ((c
= *s2
++)) {
848 if (c
!= '$' || *s2
!= LPAREN
) {
851 for (cp
= ++s2
; *s2
&& *s2
!= RPAREN
; s2
++)
858 if ((p
= lookup (vars
, cp
))) {
859 if (!parsed
&& (p
->p_flags
& P_CHK
))
862 strcpy (s1
, p
->p_value
);
872 * Fill in the information missing from the "vars"
873 * table, which is necessary to expand any builtin
874 * variables in the string for a "pipe" or "qpipe"
888 if ((p
= lookup (vars
, "sender")))
889 p
->p_value
= getcpy (sender
);
890 if ((p
= lookup (vars
, "address")))
891 p
->p_value
= getcpy (addr
);
892 if ((p
= lookup (vars
, "size"))) {
893 snprintf (buffer
, sizeof(buffer
), "%d",
894 fstat (fd
, &st
) != -1 ? (int) st
.st_size
: 0);
895 p
->p_value
= mh_xstrdup(buffer
);
897 if ((p
= lookup (vars
, "info")))
898 p
->p_value
= getcpy (info
);
901 for (p
= vars
; p
->p_name
; p
++)
902 debug_printf ("vars[%td]: name=\"%s\" value=\"%s\"\n",
903 p
- vars
, p
->p_name
, trim(p
->p_value
));
909 * Find a matching name in a lookup table. If found,
910 * return the "pairs" entry, else return NULL.
914 lookup (struct pair
*pairs
, char *key
)
916 for (; pairs
->p_name
; pairs
++)
917 if (!strcasecmp (pairs
->p_name
, key
))
925 * Check utmp(x) file to see if user is currently
940 while ((utp
= getutxent()) != NULL
) {
941 if ( utp
->ut_type
== USER_PROCESS
&& utp
->ut_user
[0] != 0
942 && strncmp (user
, utp
->ut_user
, sizeof(utp
->ut_user
)) == 0) {
946 return utmped
= DONE
;
951 #endif /* HAVE_GETUTXENT */
952 return utmped
= NOTOK
;
955 #define check(t,a,b) if (t < a || t > b) return -1
956 #define cmpar(h1,m1,h2,m2) if (h1 < h2 || (h1 == h2 && m1 < m2)) return 0
959 timely (char *t1
, char *t2
)
961 int t1hours
, t1mins
, t2hours
, t2mins
;
963 if (sscanf (t1
, "%d:%d", &t1hours
, &t1mins
) != 2)
965 check (t1hours
, 0, 23);
966 check (t1mins
, 0, 59);
968 if (sscanf (t2
, "%d:%d", &t2hours
, &t2mins
) != 2)
970 check (t2hours
, 0, 23);
971 check (t2mins
, 0, 59);
973 cmpar (now
->tw_hour
, now
->tw_min
, t1hours
, t1mins
);
974 cmpar (t2hours
, t2mins
, now
->tw_hour
, now
->tw_min
);
981 * Deliver message by appending to a file.
985 usr_file (int fd
, char *mailbox
, int mbx_style
)
990 verbose_printf("delivering to file \"%s\" (%s style)", mailbox
,
991 mbx_style
== MBOX_FORMAT
? "mbox" : "mmdf");
994 /* open and lock the file */
995 if ((md
= mbx_open (mailbox
, mbx_style
, pw
->pw_uid
, pw
->pw_gid
, m_gmprot())) == -1) {
997 adorn ("", "unable to open:");
1001 lseek(fd
, 0, SEEK_SET
);
1003 /* append message to file */
1004 if (mbx_copy (mailbox
, mbx_style
, md
, fd
, NULL
) == -1) {
1006 adorn ("", "error writing to:");
1010 /* close and unlock file */
1011 if (mbx_close (mailbox
, md
) == NOTOK
) {
1013 adorn ("", "error closing:");
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);
1053 * Deliver message to a process.
1057 usr_pipe (int fd_arg
, char *cmd
, char *pgm
, char **vec
, int suppress
)
1059 volatile int fd
= fd_arg
;
1061 int bytes
, seconds
, status
;
1064 if (verbose
&& !suppress
)
1065 verbose_printf ("delivering to pipe \"%s\"", cmd
);
1067 lseek(fd
, 0, SEEK_SET
);
1074 adorn ("fork", "unable to");
1081 if (freopen ("/dev/null", "w", stdout
) == NULL
) {
1082 advise ("stdout", "freopen");
1084 if (freopen ("/dev/null", "w", stderr
) == NULL
) {
1085 advise ("stderr", "freopen");
1092 if ((fd
= open ("/dev/tty", O_RDWR
)) != -1) {
1093 ioctl (fd
, TIOCNOTTY
, NULL
);
1096 #endif /* TIOCNOTTY */
1098 setpgid(0, getpid()); /* put in own process group */
1101 setenv("USER", pw
->pw_name
, 1);
1102 setenv("HOME", pw
->pw_dir
, 1);
1103 setenv("SHELL", pw
->pw_shell
, 1);
1109 /* parent process */
1110 if (! setjmp (myctx
)) {
1111 SIGNAL (SIGALRM
, alrmser
);
1112 bytes
= fstat (fd
, &st
) != -1 ? (int) st
.st_size
: 100;
1114 /* amount of time to wait depends on message size */
1116 /* give at least 5 minutes */
1118 } else if (bytes
>= 90000) {
1119 /* a half hour is long enough */
1122 seconds
= (bytes
/ 60) + 300;
1124 alarm ((unsigned int) seconds
);
1125 status
= pidwait (child_id
, 0);
1130 verbose_printf (", success.\n");
1131 else if ((status
& 0xff00) == 0xff00)
1132 verbose_printf (", system error\n");
1134 pidstatus (status
, stdout
, ", failed");
1136 return status
== 0 ? 0 : -1;
1139 * Ruthlessly kill the child and anything
1140 * else in its process group.
1142 killpg(child_id
, SIGKILL
);
1144 verbose_printf (", timed-out; terminated\n");
1155 longjmp (myctx
, DONE
);
1160 * Get the `sender' from the envelope
1161 * information ("From " line).
1165 get_sender (char *envelope
, char **sender
)
1169 char buffer
[BUFSIZ
];
1171 if (envelope
== NULL
) {
1172 *sender
= mh_xstrdup("");
1177 strncpy (buffer
, envelope
+ i
, sizeof(buffer
));
1178 if ((cp
= strchr(buffer
, '\n'))) {
1188 for (cp
= buffer
+ strlen (buffer
) - 1; cp
>= buffer
; cp
--)
1189 if (isspace ((unsigned char) *cp
))
1193 *sender
= mh_xstrdup(buffer
);
1198 * Copy message into a temporary file.
1199 * While copying, it will do some header processing
1200 * including the extraction of the envelope information.
1204 copy_message (int qd
, char *tmpfil
, int fold
)
1206 int i
, first
= 1, fd1
, fd2
;
1207 char buffer
[BUFSIZ
];
1211 tfile
= m_mktemp2(NULL
, invo_name
, &fd1
, NULL
);
1212 if (tfile
== NULL
) return -1;
1213 strncpy (tmpfil
, tfile
, BUFSIZ
);
1216 while ((i
= read (qd
, buffer
, sizeof(buffer
))) > 0)
1217 if (write (fd1
, buffer
, i
) != i
) {
1220 (void) m_unlink (tmpfil
);
1225 lseek(fd1
, 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 while (fgets (buffer
, sizeof(buffer
), qfp
)) {
1265 if (has_prefix(buffer
, "From ")) {
1266 /* get copy of envelope information ("From " line) */
1267 envelope
= mh_xstrdup(buffer
);
1269 /* Put the delivery date in message */
1278 fputs (buffer
, ffp
);
1290 lseek(fd1
, 0, SEEK_SET
);
1302 * Trim strings for pretty printing of debugging output
1308 static char buffer
[BUFSIZ
* 4];
1314 /* copy string into temp buffer */
1315 strncpy (buffer
, cp
, sizeof(buffer
));
1318 /* skip over leading whitespace */
1319 while (isspace((unsigned char) *bp
))
1322 /* start at the end and zap trailing whitespace */
1323 for (sp
= bp
+ strlen(bp
) - 1; sp
>= bp
; sp
--) {
1324 if (isspace((unsigned char) *sp
))
1330 /* replace remaining whitespace with spaces */
1331 for (sp
= bp
; *sp
; sp
++)
1332 if (isspace((unsigned char) *sp
))
1339 * Function for printing `verbose' messages.
1343 verbose_printf (char *fmt
, ...)
1351 fflush (stdout
); /* now flush output */
1356 * Function for printing `verbose' delivery
1361 adorn (char *what
, char *fmt
, ...)
1367 eindex
= errno
; /* save the errno */
1368 fputs(", ", stdout
);
1376 printf(" %s: ", what
);
1377 if ((s
= strerror (eindex
)))
1380 printf("Error %d", eindex
);
1389 * Function for printing `debug' messages.
1393 debug_printf (char *fmt
, ...)
1398 vfprintf (stderr
, fmt
, ap
);
1404 * Check ndbm/db file(s) to see if the Message-Id of this
1405 * message matches the Message-Id of a previous message,
1406 * so we can discard it. If it doesn't match, we add the
1407 * Message-Id of this message to the ndbm/db file.
1410 suppress_duplicates (int fd
, char *file
)
1412 int fd1
, lockfd
, state
, result
;
1413 char *cp
, buf
[NMH_BUFSIZ
], name
[NAMESZ
];
1417 m_getfld_state_t gstate
;
1419 if ((fd1
= dup (fd
)) == -1)
1421 if (!(in
= fdopen (fd1
, "r"))) {
1427 gstate
= m_getfld_state_init(in
);
1429 int failed_to_lock
= 0;
1430 int bufsz
= sizeof buf
;
1431 state
= m_getfld2(&gstate
, name
, buf
, &bufsz
);
1435 /* Search for the message ID */
1436 if (strcasecmp (name
, "Message-ID")) {
1437 while (state
== FLDPLUS
) {
1439 state
= m_getfld2(&gstate
, name
, buf
, &bufsz
);
1444 cp
= mh_xstrdup(buf
);
1445 while (state
== FLDPLUS
) {
1447 state
= m_getfld2(&gstate
, name
, buf
, &bufsz
);
1450 key
.dptr
= trimcpy (cp
);
1451 key
.dsize
= strlen (key
.dptr
) + 1;
1455 if (!(db
= dbm_open (file
, O_RDWR
| O_CREAT
, 0600))) {
1456 advise (file
, "unable to perform dbm_open on");
1462 * Since it is difficult to portable lock a ndbm file,
1463 * we will open and lock the Maildelivery file instead.
1464 * This will fail if your Maildelivery file doesn't
1467 if ((lockfd
= lkopendata(file
, O_RDWR
, 0, &failed_to_lock
))
1469 advise (file
, "unable to perform file locking on");
1474 value
= dbm_fetch (db
, key
);
1477 verbose_printf ("Message-ID: %s\n already received on %s",
1481 value
.dptr
= ddate
+ sizeof("Delivery-Date:");
1482 value
.dsize
= strlen(value
.dptr
) + 1;
1483 if (dbm_store (db
, key
, value
, DBM_INSERT
))
1484 advise (file
, "possibly corrupt file");
1489 lkclosedata(lockfd
, file
);
1506 m_getfld_state_destroy (&gstate
);