]>
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/smatch.h"
24 #include "sbr/strindex.h"
25 #include "sbr/closefds.h"
26 #include "sbr/trimcpy.h"
27 #include "sbr/getcpy.h"
28 #include "sbr/ambigsw.h"
29 #include "sbr/pidstatus.h"
30 #include "sbr/print_version.h"
31 #include "sbr/print_help.h"
32 #include "sbr/error.h"
33 #include "h/dropsbr.h"
34 #include "h/rcvmail.h"
35 #include "h/signals.h"
41 #include "sbr/lock_file.h"
42 #include "sbr/m_mktemp.h"
45 #include <sys/ioctl.h>
48 /* Hopefully, grp.h declares initgroups(). If we run into a platform
49 where it doesn't, we could consider declaring it here as well. */
52 /* This define is needed for Berkeley db v2 and above to
53 * make the header file expose the 'historical' ndbm APIs.
54 * We define it unconditionally because this is simple and
57 #define DB_DBM_HSEARCH 1
59 #endif /* Use DB_DBM_HSEARCH to prevent warning from gcc -Wunused-macros. */
66 #endif /* HAVE_GETUTXENT */
68 #define SLOCAL_SWITCHES \
69 X("addr address", 0, ADDRSW) \
70 X("user name", 0, USERSW) \
71 X("file file", 0, FILESW) \
72 X("sender address", 0, SENDERSW) \
73 X("mailbox file", 0, MAILBOXSW) \
74 X("home directory", -4, HOMESW) \
75 X("info data", 0, INFOSW) \
76 X("maildelivery file", 0, MAILSW) \
77 X("verbose", 0, VERBSW) \
78 X("noverbose", 0, NVERBSW) \
79 X("suppressdup", 0, SUPPRESSDUP) \
80 X("nosuppressdup", 0, NSUPPRESSDUP) \
81 X("debug", 0, DEBUGSW) \
82 X("version", 0, VERSIONSW) \
83 X("help", 0, HELPSW) \
85 #define X(sw, minchars, id) id,
86 DEFINE_SWITCH_ENUM(SLOCAL
);
89 #define X(sw, minchars, id) { sw, minchars, id },
90 DEFINE_SWITCH_ARRAY(SLOCAL
, switches
);
93 static int globbed
= 0; /* have we built "vars" table yet? */
94 static int parsed
= 0; /* have we built header field table yet */
95 static int utmped
= 0; /* have we scanned umtp(x) file yet */
96 static bool suppressdup
; /* are we suppressing duplicate messages? */
101 static char *addr
= NULL
;
102 static char *user
= NULL
;
103 static char *info
= NULL
;
104 static char *file
= NULL
;
105 static char *sender
= NULL
;
106 static char *envelope
= NULL
; /* envelope information ("From " line) */
107 static char *mbox
= NULL
;
108 static char *home
= NULL
;
110 static struct passwd
*pw
; /* passwd file entry */
112 static char ddate
[BUFSIZ
]; /* record the delivery date */
115 static jmp_buf myctx
;
117 /* flags for pair->p_flags */
119 #define P_ADR 0x01 /* field is address */
120 #define P_HID 0x02 /* special (fake) field */
132 * Lookup table for matching fields and patterns
133 * in messages. The rest of the table is added
134 * when the message is parsed.
136 static struct pair hdrs
[NVEC
+ 1] = {
137 { "source", NULL
, P_HID
},
138 { "addr", NULL
, P_HID
},
139 { "Return-Path", NULL
, P_ADR
},
140 { "Reply-To", NULL
, P_ADR
},
141 { "From", NULL
, P_ADR
},
142 { "Sender", NULL
, P_ADR
},
143 { "To", NULL
, P_ADR
},
144 { "cc", NULL
, P_ADR
},
145 { "Resent-Reply-To", NULL
, P_ADR
},
146 { "Resent-From", NULL
, P_ADR
},
147 { "Resent-Sender", NULL
, P_ADR
},
148 { "Resent-To", NULL
, P_ADR
},
149 { "Resent-cc", NULL
, P_ADR
},
154 * The list of builtin variables to expand in a string
155 * before it is executed by the "pipe" or "qpipe" action.
157 static struct pair vars
[] = {
158 { "sender", NULL
, P_NIL
},
159 { "address", NULL
, P_NIL
},
160 { "size", NULL
, P_NIL
},
161 { "reply-to", NULL
, P_CHK
},
162 { "info", NULL
, P_NIL
},
166 extern char **environ
;
171 static int localmail (int, char *);
172 static int usr_delivery (int, char *, int);
173 static int split (char *, char **);
174 static int parse (int);
175 static void expand (char *, char *, int);
176 static void glob (int);
177 static struct pair
*lookup (struct pair
*, char *) PURE
;
178 static int logged_in (void);
179 static int timely (char *, char *);
180 static int usr_file (int, char *, int);
181 static int usr_pipe (int, char *, char *, char **, int);
182 static int usr_folder (int, char *);
183 static void alrmser (int);
184 static void get_sender (char *, char **);
185 static int copy_message (int, char *, int);
186 static void verbose_printf (char *fmt
, ...) CHECK_PRINTF(1, 2);
187 static void adorn (char *, char *, ...) CHECK_PRINTF(2, 3);
188 static void debug_printf (char *fmt
, ...) CHECK_PRINTF(1, 2);
189 static int suppress_duplicates (int, char *);
190 static char *trim (char *);
194 main (int argc
, char **argv
)
198 char *cp
, *mdlvr
= NULL
, buf
[BUFSIZ
];
199 char mailbox
[BUFSIZ
], tmpfil
[BUFSIZ
];
200 char **argp
, **arguments
;
202 if (nmh_init(argv
[0], false, false)) { return 1; }
205 arguments
= getarguments (invo_name
, argc
, argv
, 0);
208 /* Parse arguments */
209 while ((cp
= *argp
++)) {
211 switch (smatch (++cp
, switches
)) {
213 ambigsw (cp
, switches
);
216 die("-%s unknown", cp
);
219 snprintf (buf
, sizeof(buf
), "%s [switches]", invo_name
);
220 print_help (buf
, switches
, 0);
223 print_version(invo_name
);
227 if (!(addr
= *argp
++))/* allow -xyz arguments */
228 die("missing argument to %s", argp
[-2]);
231 if (!(info
= *argp
++))/* allow -xyz arguments */
232 die("missing argument to %s", argp
[-2]);
235 if (!(user
= *argp
++))/* allow -xyz arguments */
236 die("missing argument to %s", argp
[-2]);
239 if (!(file
= *argp
++) || *file
== '-')
240 die("missing argument to %s", argp
[-2]);
243 if (!(sender
= *argp
++))/* allow -xyz arguments */
244 die("missing argument to %s", argp
[-2]);
247 if (!(mbox
= *argp
++) || *mbox
== '-')
248 die("missing argument to %s", argp
[-2]);
251 if (!(home
= *argp
++) || *home
== '-')
252 die("missing argument to %s", argp
[-2]);
256 if (!(cp
= *argp
++) || *cp
== '-')
257 die("missing argument to %s", argp
[-2]);
259 die("only one maildelivery file at a time!");
281 die("only switch arguments are supported");
286 addr
= getusername ();
288 user
= getusername ();
290 if ((pw
= getpwnam (user
)) == NULL
)
291 die("no such local user as %s", user
);
293 if (chdir (pw
->pw_dir
) == -1)
294 if (chdir ("/") < 0) {
295 advise ("/", "chdir");
299 if (geteuid() == 0) {
300 if (setgid (pw
->pw_gid
) != 0) {
301 adios ("setgid", "unable to set group to %ld", (long) pw
->pw_gid
);
303 initgroups (pw
->pw_name
, pw
->pw_gid
);
304 if (setuid (pw
->pw_uid
) != 0) {
305 adios ("setuid", "unable to set user to %ld", (long) pw
->pw_uid
);
312 setbuf (stdin
, NULL
);
314 /* Record the delivery time */
315 if ((now
= dlocaltimenow ()) == NULL
)
316 die("unable to ascertain local time");
317 snprintf (ddate
, sizeof(ddate
), "Delivery-Date: %s\n", dtimenow (0));
320 * Copy the message to a temporary file
325 /* getting message from file */
326 if ((tempfd
= open (file
, O_RDONLY
)) == -1)
327 adios (file
, "unable to open");
329 debug_printf ("retrieving message from file \"%s\"\n", file
);
330 if ((fd
= copy_message (tempfd
, tmpfil
, 1)) == -1)
331 die("unable to create temporary file in %s",
335 /* getting message from stdin */
337 debug_printf ("retrieving message from stdin\n");
338 if ((fd
= copy_message (fileno (stdin
), tmpfil
, 1)) == -1)
339 die("unable to create temporary file in %s",
344 debug_printf ("temporary file=\"%s\"\n", tmpfil
);
346 /* Delete the temp file now or a copy of every single message passed through
347 slocal will be left in the /tmp directory until deleted manually! This
348 unlink() used to be under an 'else' of the 'if (debug)' above, but since
349 some people like to always run slocal with -debug and log the results,
350 the /tmp directory would get choked over time. Of course, now that we
351 always delete the temp file, the "temporary file=" message above is
352 somewhat pointless -- someone watching debug output wouldn't have a
353 chance to 'tail -f' or 'ln' the temp file before it's unlinked. The best
354 thing would be to delay this unlink() until later if debug == 1, but I'll
355 leave that for someone who cares about the temp-file-accessing
356 functionality (they'll have to watch out for cases where we adios()). */
357 (void) m_unlink (tmpfil
);
359 if (!(fp
= fdopen (fd
, "r+")))
360 die("unable to access temporary file");
363 * If no sender given, extract it
364 * from envelope information. */
366 get_sender (envelope
, &sender
);
369 snprintf (mailbox
, sizeof(mailbox
), "%s/%s",
370 mmdfldir
[0] ? mmdfldir
: pw
->pw_dir
,
371 mmdflfil
[0] ? mmdflfil
: pw
->pw_name
);
378 debug_printf ("addr=\"%s\"\n", trim(addr
));
379 debug_printf ("user=\"%s\"\n", trim(user
));
380 debug_printf ("info=\"%s\"\n", trim(info
));
381 debug_printf ("sender=\"%s\"\n", trim(sender
));
382 debug_printf ("envelope=\"%s\"\n", envelope
? trim(envelope
) : "");
383 debug_printf ("mbox=\"%s\"\n", trim(mbox
));
384 debug_printf ("home=\"%s\"\n", trim(home
));
385 debug_printf ("ddate=\"%s\"\n", trim(ddate
));
386 debug_printf ("now=%02d:%02d\n\n", now
->tw_hour
, now
->tw_min
);
389 /* deliver the message */
390 status
= localmail (fd
, mdlvr
);
392 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 */
421 return usr_file (fd
, mbox
, MBOX_FORMAT
);
425 #define matches(a,b) (stringdex (b, a) >= 0)
428 * Parse the delivery file, and process incoming message.
432 usr_delivery (int fd
, char *delivery
, int su
)
440 char *field
, *pattern
, *action
, *result
, *string
;
441 char buffer
[BUFSIZ
], tmpbuf
[BUFSIZ
];
447 /* open the delivery file */
448 if ((fp
= fopen (delivery
, "r")) == NULL
)
451 /* check if delivery file has bad ownership or permissions */
452 if (fstat (fileno (fp
), &st
) == -1
453 || (st
.st_uid
!= 0 && (su
|| st
.st_uid
!= pw
->pw_uid
))
454 || st
.st_mode
& (S_IWGRP
|S_IWOTH
)) {
456 verbose_printf ("WARNING: %s has bad ownership/modes (su=%d,uid=%d,owner=%d,mode=0%o)\n",
457 delivery
, su
, (int) pw
->pw_uid
, (int) st
.st_uid
, (int) st
.st_mode
);
465 /* read and process delivery file */
466 while (fgets (buffer
, sizeof(buffer
), fp
)) {
467 /* skip comments and empty lines */
468 if (*buffer
== '#' || *buffer
== '\n')
471 trim_suffix_c(buffer
, '\n');
473 /* split buffer into fields */
474 vecp
= split (buffer
, vec
);
476 /* check for too few fields */
479 debug_printf ("WARNING: entry with only %d fields, skipping.\n", vecp
);
484 for (i
= 0; vec
[i
]; i
++)
485 debug_printf ("vec[%d]: \"%s\"\n", i
, trim(vec
[i
]));
494 /* find out how to perform the action */
499 * If previous condition failed, don't
500 * do this - else fall through
508 * If already delivered, skip this action. Else
509 * consider delivered if action is successful.
518 * Take action, and consider delivered if
519 * action is successful.
528 * Take action, but don't consider delivered, even
529 * if action is successful
536 if (!strcasecmp (vec
[5], "select")) {
537 if (logged_in () != -1)
539 if (vecp
> 7 && timely (vec
[6], vec
[7]) == -1)
544 /* check if the field matches */
552 * "default" matches only if the message hasn't
553 * been delivered yet.
555 if (!strcasecmp (field
, "default")) {
563 /* parse message and build lookup table */
564 if (!parsed
&& parse (fd
) == -1) {
569 * find header field in lookup table, and
570 * see if the pattern matches.
572 if ((p
= lookup (hdrs
, field
)) && (p
->p_value
!= NULL
)
573 && matches (p
->p_value
, pattern
)) {
582 /* find out the action to perform */
585 /* deliver to quoted pipe */
586 if (strcasecmp (action
, "qpipe"))
590 expand (tmpbuf
, string
, fd
);
591 if (split (tmpbuf
, vec
) < 1)
593 status
= usr_pipe (fd
, tmpbuf
, vec
[0], vec
, 0);
597 /* deliver to pipe */
598 if (strcasecmp (action
, "pipe"))
604 expand (tmpbuf
, string
, fd
);
607 status
= usr_pipe (fd
, tmpbuf
, "/bin/sh", vec
+ 2, 0);
612 if (!strcasecmp (action
, "file")) {
613 status
= usr_file (fd
, string
, MBOX_FORMAT
);
616 /* deliver to nmh folder */
617 else if (strcasecmp (action
, "folder"))
621 status
= usr_folder (fd
, string
);
626 if (!strcasecmp (action
, "mmdf")) {
627 status
= usr_file (fd
, string
, MMDF_FORMAT
);
631 else if (strcasecmp (action
, "mbox"))
637 status
= usr_file (fd
, string
, MBOX_FORMAT
);
642 if (strcasecmp (action
, "destroy"))
648 if (status
) next
= false; /* action failed, mark for 'N' result */
650 if (accept
&& status
== 0)
662 * Split buffer into fields (delimited by whitespace or
663 * comma's). Return the number of fields found.
667 split (char *cp
, char **vec
)
674 /* split into a maximum of NVEC fields */
675 for (i
= 0; i
<= NVEC
;) {
678 /* zap any whitespace and comma's */
679 while (isspace ((unsigned char) *s
) || *s
== ',')
682 /* end of buffer, time to leave */
686 /* get double quote text as a single field */
688 for (vec
[i
++] = ++s
; *s
&& *s
!= '"'; s
++) {
690 * Check for escaped double quote. We need
691 * to shift the string to remove slash.
699 if (*s
== '"') /* zap trailing double quote */
704 if (*s
== QUOTE
&& *++s
!= '"')
708 /* move forward to next field delimiter */
709 while (*s
&& !isspace ((unsigned char) *s
) && *s
!= ',')
719 * Parse the headers of a message, and build the
720 * lookup table for matching fields and patterns.
729 char name
[NAMESZ
], field
[NMH_BUFSIZ
];
732 m_getfld_state_t gstate
;
737 /* get a new FILE pointer to message */
738 if ((fd1
= dup (fd
)) == -1)
740 if ((in
= fdopen (fd1
, "r")) == NULL
) {
746 /* add special entries to lookup table */
747 if ((p
= lookup (hdrs
, "source")))
748 p
->p_value
= getcpy (sender
);
749 if ((p
= lookup (hdrs
, "addr")))
750 p
->p_value
= getcpy (addr
);
753 * Scan the headers of the message and build
756 gstate
= m_getfld_state_init(in
);
758 int fieldsz
= sizeof field
;
759 switch (state
= m_getfld2(&gstate
, name
, field
, &fieldsz
)) {
762 lp
= mh_xstrdup(field
);
763 while (state
== FLDPLUS
) {
764 fieldsz
= sizeof field
;
765 state
= m_getfld2(&gstate
, name
, field
, &fieldsz
);
766 lp
= add (field
, lp
);
768 for (p
= hdrs
; p
->p_name
; p
++) {
769 if (!strcasecmp (p
->p_name
, name
)) {
770 if (!(p
->p_flags
& P_HID
)) {
771 if ((cp
= p
->p_value
)) {
772 if (p
->p_flags
& P_ADR
) {
773 dp
= cp
+ strlen (cp
) - 1;
776 cp
= add (",\n\t", cp
);
781 p
->p_value
= add (lp
, cp
);
787 if (p
->p_name
== NULL
&& i
< NVEC
) {
788 p
->p_name
= mh_xstrdup(name
);
802 inform("format error in message");
806 inform("internal error in m_getfld2");
812 m_getfld_state_destroy (&gstate
);
815 if ((p
= lookup (vars
, "reply-to"))) {
816 if ((q
= lookup (hdrs
, "reply-to")) == NULL
|| q
->p_value
== NULL
)
817 q
= lookup (hdrs
, "from");
818 p
->p_value
= getcpy (q
? q
->p_value
: "");
819 p
->p_flags
&= ~P_CHK
;
821 debug_printf ("vars[%td]: name=\"%s\" value=\"%s\"\n",
822 p
- vars
, p
->p_name
, trim(p
->p_value
));
825 for (p
= hdrs
; p
->p_name
; p
++)
826 debug_printf ("hdrs[%td]: name=\"%s\" value=\"%s\"\n",
827 p
- hdrs
, p
->p_name
, p
->p_value
? trim(p
->p_value
) : "");
838 * Expand any builtin variables such as $(sender),
839 * $(address), etc., in a string.
843 expand (char *s1
, char *s2
, int fd
)
851 while ((c
= *s2
++)) {
852 if (c
!= '$' || *s2
!= LPAREN
) {
855 for (cp
= ++s2
; *s2
&& *s2
!= RPAREN
; s2
++)
862 if ((p
= lookup (vars
, cp
))) {
863 if (!parsed
&& (p
->p_flags
& P_CHK
))
866 strcpy (s1
, p
->p_value
);
876 * Fill in the information missing from the "vars"
877 * table, which is necessary to expand any builtin
878 * variables in the string for a "pipe" or "qpipe"
892 if ((p
= lookup (vars
, "sender")))
893 p
->p_value
= getcpy (sender
);
894 if ((p
= lookup (vars
, "address")))
895 p
->p_value
= getcpy (addr
);
896 if ((p
= lookup (vars
, "size"))) {
897 snprintf (buffer
, sizeof(buffer
), "%d",
898 fstat (fd
, &st
) != -1 ? (int) st
.st_size
: 0);
899 p
->p_value
= mh_xstrdup(buffer
);
901 if ((p
= lookup (vars
, "info")))
902 p
->p_value
= getcpy (info
);
905 for (p
= vars
; p
->p_name
; p
++)
906 debug_printf ("vars[%td]: name=\"%s\" value=\"%s\"\n",
907 p
- vars
, p
->p_name
, trim(p
->p_value
));
913 * Find a matching name in a lookup table. If found,
914 * return the "pairs" entry, else return NULL.
918 lookup (struct pair
*pairs
, char *key
)
920 for (; pairs
->p_name
; pairs
++)
921 if (!strcasecmp (pairs
->p_name
, key
))
929 * Check utmp(x) file to see if user is currently
944 while ((utp
= getutxent()) != NULL
) {
945 if ( utp
->ut_type
== USER_PROCESS
&& utp
->ut_user
[0] != 0
946 && strncmp (user
, utp
->ut_user
, sizeof(utp
->ut_user
)) == 0) {
950 return utmped
= DONE
;
955 #endif /* HAVE_GETUTXENT */
956 return utmped
= NOTOK
;
959 #define check(t,a,b) if (t < a || t > b) return -1
960 #define cmpar(h1,m1,h2,m2) if (h1 < h2 || (h1 == h2 && m1 < m2)) return 0
963 timely (char *t1
, char *t2
)
965 int t1hours
, t1mins
, t2hours
, t2mins
;
967 if (sscanf (t1
, "%d:%d", &t1hours
, &t1mins
) != 2)
969 check (t1hours
, 0, 23);
970 check (t1mins
, 0, 59);
972 if (sscanf (t2
, "%d:%d", &t2hours
, &t2mins
) != 2)
974 check (t2hours
, 0, 23);
975 check (t2mins
, 0, 59);
977 cmpar (now
->tw_hour
, now
->tw_min
, t1hours
, t1mins
);
978 cmpar (t2hours
, t2mins
, now
->tw_hour
, now
->tw_min
);
985 * Deliver message by appending to a file.
989 usr_file (int fd
, char *mailbox
, int mbx_style
)
994 verbose_printf("delivering to file \"%s\" (%s style)", mailbox
,
995 mbx_style
== MBOX_FORMAT
? "mbox" : "mmdf");
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
, 0, SEEK_SET
);
1007 /* append message to file */
1008 if (mbx_copy (mailbox
, mbx_style
, md
, fd
, NULL
) == -1) {
1010 adorn ("", "error writing to:");
1014 /* close and unlock file */
1015 if (mbx_close (mailbox
, md
) == NOTOK
) {
1017 adorn ("", "error closing:");
1022 verbose_printf (", success.\n");
1028 * Deliver message to a nmh folder.
1032 usr_folder (int fd
, char *string
)
1035 char folder
[BUFSIZ
], *vec
[3];
1037 /* get folder name ready */
1039 strncpy(folder
, string
, sizeof(folder
));
1041 snprintf(folder
, sizeof(folder
), "+%s", string
);
1044 verbose_printf ("delivering to folder \"%s\"", folder
+ 1);
1046 vec
[0] = "rcvstore";
1050 /* use rcvstore to put message in folder */
1051 status
= usr_pipe (fd
, "rcvstore", rcvstoreproc
, vec
, 1);
1057 * Deliver message to a process.
1061 usr_pipe (int fd_arg
, char *cmd
, char *pgm
, char **vec
, int suppress
)
1063 volatile int fd
= fd_arg
;
1065 int bytes
, seconds
, status
;
1068 if (verbose
&& !suppress
)
1069 verbose_printf ("delivering to pipe \"%s\"", cmd
);
1071 lseek(fd
, 0, SEEK_SET
);
1078 adorn ("fork", "unable to");
1085 if (freopen ("/dev/null", "w", stdout
) == NULL
) {
1086 advise ("stdout", "freopen");
1088 if (freopen ("/dev/null", "w", stderr
) == NULL
) {
1089 advise ("stderr", "freopen");
1096 if ((fd
= open ("/dev/tty", O_RDWR
)) != -1) {
1097 ioctl (fd
, TIOCNOTTY
, NULL
);
1100 #endif /* TIOCNOTTY */
1102 setpgid(0, getpid()); /* put in own process group */
1105 setenv("USER", pw
->pw_name
, 1);
1106 setenv("HOME", pw
->pw_dir
, 1);
1107 setenv("SHELL", pw
->pw_shell
, 1);
1113 /* parent process */
1114 if (! setjmp (myctx
)) {
1115 SIGNAL (SIGALRM
, alrmser
);
1116 bytes
= fstat (fd
, &st
) != -1 ? (int) st
.st_size
: 100;
1118 /* amount of time to wait depends on message size */
1120 /* give at least 5 minutes */
1122 } else if (bytes
>= 90000) {
1123 /* a half hour is long enough */
1126 seconds
= (bytes
/ 60) + 300;
1128 alarm ((unsigned int) seconds
);
1129 status
= pidwait (child_id
, 0);
1134 verbose_printf (", success.\n");
1135 else if ((status
& 0xff00) == 0xff00)
1136 verbose_printf (", system error\n");
1138 pidstatus (status
, stdout
, ", failed");
1140 return status
== 0 ? 0 : -1;
1143 * Ruthlessly kill the child and anything
1144 * else in its process group.
1146 killpg(child_id
, SIGKILL
);
1148 verbose_printf (", timed-out; terminated\n");
1159 longjmp (myctx
, DONE
);
1164 * Get the `sender' from the envelope
1165 * information ("From " line).
1169 get_sender (char *envelope
, char **sender
)
1173 char buffer
[BUFSIZ
];
1175 if (envelope
== NULL
) {
1176 *sender
= mh_xstrdup("");
1181 strncpy (buffer
, envelope
+ i
, sizeof(buffer
));
1182 if ((cp
= strchr(buffer
, '\n'))) {
1192 for (cp
= buffer
+ strlen (buffer
) - 1; cp
>= buffer
; cp
--)
1193 if (isspace ((unsigned char) *cp
))
1197 *sender
= mh_xstrdup(buffer
);
1202 * Copy message into a temporary file.
1203 * While copying, it will do some header processing
1204 * including the extraction of the envelope information.
1208 copy_message (int qd
, char *tmpfil
, int fold
)
1210 int i
, first
= 1, fd1
, fd2
;
1211 char buffer
[BUFSIZ
];
1215 tfile
= m_mktemp2(NULL
, invo_name
, &fd1
, NULL
);
1216 if (tfile
== NULL
) return -1;
1217 strncpy (tmpfil
, tfile
, BUFSIZ
);
1220 while ((i
= read (qd
, buffer
, sizeof(buffer
))) > 0)
1221 if (write (fd1
, buffer
, i
) != i
) {
1224 (void) m_unlink (tmpfil
);
1229 lseek(fd1
, 0, SEEK_SET
);
1233 /* dup the fd for incoming message */
1234 if ((fd2
= dup (qd
)) == -1) {
1239 /* now create a FILE pointer for it */
1240 if ((qfp
= fdopen (fd2
, "r")) == NULL
) {
1246 /* dup the fd for temporary file */
1247 if ((fd2
= dup (fd1
)) == -1) {
1253 /* now create a FILE pointer for it */
1254 if ((ffp
= fdopen (fd2
, "r+")) == NULL
) {
1262 * copy message into temporary file
1263 * and massage the headers. Save
1264 * a copy of the "From " line for later.
1266 while (fgets (buffer
, sizeof(buffer
), qfp
)) {
1269 if (has_prefix(buffer
, "From ")) {
1270 /* get copy of envelope information ("From " line) */
1271 envelope
= mh_xstrdup(buffer
);
1273 /* Put the delivery date in message */
1282 fputs (buffer
, ffp
);
1294 lseek(fd1
, 0, SEEK_SET
);
1306 * Trim strings for pretty printing of debugging output
1312 static char buffer
[BUFSIZ
* 4];
1318 /* copy string into temp buffer */
1319 strncpy (buffer
, cp
, sizeof(buffer
));
1322 /* skip over leading whitespace */
1323 while (isspace((unsigned char) *bp
))
1326 /* start at the end and zap trailing whitespace */
1327 for (sp
= bp
+ strlen(bp
) - 1; sp
>= bp
; sp
--) {
1328 if (isspace((unsigned char) *sp
))
1334 /* replace remaining whitespace with spaces */
1335 for (sp
= bp
; *sp
; sp
++)
1336 if (isspace((unsigned char) *sp
))
1343 * Function for printing `verbose' messages.
1347 verbose_printf (char *fmt
, ...)
1355 fflush (stdout
); /* now flush output */
1360 * Function for printing `verbose' delivery
1365 adorn (char *what
, char *fmt
, ...)
1371 eindex
= errno
; /* save the errno */
1372 fputs(", ", stdout
);
1380 printf(" %s: ", what
);
1381 if ((s
= strerror (eindex
)))
1384 printf("Error %d", eindex
);
1393 * Function for printing `debug' messages.
1397 debug_printf (char *fmt
, ...)
1402 vfprintf (stderr
, fmt
, ap
);
1408 * Check ndbm/db file(s) to see if the Message-Id of this
1409 * message matches the Message-Id of a previous message,
1410 * so we can discard it. If it doesn't match, we add the
1411 * Message-Id of this message to the ndbm/db file.
1414 suppress_duplicates (int fd
, char *file
)
1416 int fd1
, lockfd
, state
, result
;
1417 char *cp
, buf
[NMH_BUFSIZ
], name
[NAMESZ
];
1421 m_getfld_state_t gstate
;
1423 if ((fd1
= dup (fd
)) == -1)
1425 if (!(in
= fdopen (fd1
, "r"))) {
1431 gstate
= m_getfld_state_init(in
);
1433 int failed_to_lock
= 0;
1434 int bufsz
= sizeof buf
;
1435 state
= m_getfld2(&gstate
, name
, buf
, &bufsz
);
1439 /* Search for the message ID */
1440 if (strcasecmp (name
, "Message-ID")) {
1441 while (state
== FLDPLUS
) {
1443 state
= m_getfld2(&gstate
, name
, buf
, &bufsz
);
1448 cp
= mh_xstrdup(buf
);
1449 while (state
== FLDPLUS
) {
1451 state
= m_getfld2(&gstate
, name
, buf
, &bufsz
);
1454 key
.dptr
= trimcpy (cp
);
1455 key
.dsize
= strlen (key
.dptr
) + 1;
1459 if (!(db
= dbm_open (file
, O_RDWR
| O_CREAT
, 0600))) {
1460 advise (file
, "unable to perform dbm_open on");
1466 * Since it is difficult to portable lock a ndbm file,
1467 * we will open and lock the Maildelivery file instead.
1468 * This will fail if your Maildelivery file doesn't
1471 if ((lockfd
= lkopendata(file
, O_RDWR
, 0, &failed_to_lock
))
1473 advise (file
, "unable to perform file locking on");
1478 value
= dbm_fetch (db
, key
);
1481 verbose_printf ("Message-ID: %s\n already received on %s",
1485 value
.dptr
= ddate
+ sizeof("Delivery-Date:");
1486 value
.dsize
= strlen(value
.dptr
) + 1;
1487 if (dbm_store (db
, key
, value
, DBM_INSERT
))
1488 advise (file
, "possibly corrupt file");
1493 lkclosedata(lockfd
, file
);
1510 m_getfld_state_destroy (&gstate
);