]>
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/ambigsw.h"
24 #include "sbr/pidstatus.h"
25 #include "sbr/print_version.h"
26 #include "sbr/print_help.h"
27 #include "sbr/error.h"
28 #include "h/dropsbr.h"
29 #include "h/rcvmail.h"
30 #include "h/signals.h"
36 #include "sbr/lock_file.h"
37 #include "sbr/m_mktemp.h"
40 #include <sys/ioctl.h>
43 /* Hopefully, grp.h declares initgroups(). If we run into a platform
44 where it doesn't, we could consider declaring it here as well. */
47 /* This define is needed for Berkeley db v2 and above to
48 * make the header file expose the 'historical' ndbm APIs.
49 * We define it unconditionally because this is simple and
52 #define DB_DBM_HSEARCH 1
54 #endif /* Use DB_DBM_HSEARCH to prevent warning from gcc -Wunused-macros. */
61 #endif /* HAVE_GETUTXENT */
63 #define SLOCAL_SWITCHES \
64 X("addr address", 0, ADDRSW) \
65 X("user name", 0, USERSW) \
66 X("file file", 0, FILESW) \
67 X("sender address", 0, SENDERSW) \
68 X("mailbox file", 0, MAILBOXSW) \
69 X("home directory", -4, HOMESW) \
70 X("info data", 0, INFOSW) \
71 X("maildelivery file", 0, MAILSW) \
72 X("verbose", 0, VERBSW) \
73 X("noverbose", 0, NVERBSW) \
74 X("suppressdup", 0, SUPPRESSDUP) \
75 X("nosuppressdup", 0, NSUPPRESSDUP) \
76 X("debug", 0, DEBUGSW) \
77 X("version", 0, VERSIONSW) \
78 X("help", 0, HELPSW) \
80 #define X(sw, minchars, id) id,
81 DEFINE_SWITCH_ENUM(SLOCAL
);
84 #define X(sw, minchars, id) { sw, minchars, id },
85 DEFINE_SWITCH_ARRAY(SLOCAL
, switches
);
88 static int globbed
= 0; /* have we built "vars" table yet? */
89 static int parsed
= 0; /* have we built header field table yet */
90 static int utmped
= 0; /* have we scanned umtp(x) file yet */
91 static bool suppressdup
; /* are we suppressing duplicate messages? */
96 static char *addr
= NULL
;
97 static char *user
= NULL
;
98 static char *info
= NULL
;
99 static char *file
= NULL
;
100 static char *sender
= NULL
;
101 static char *envelope
= NULL
; /* envelope information ("From " line) */
102 static char *mbox
= NULL
;
103 static char *home
= NULL
;
105 static struct passwd
*pw
; /* passwd file entry */
107 static char ddate
[BUFSIZ
]; /* record the delivery date */
110 static jmp_buf myctx
;
112 /* flags for pair->p_flags */
114 #define P_ADR 0x01 /* field is address */
115 #define P_HID 0x02 /* special (fake) field */
127 * Lookup table for matching fields and patterns
128 * in messages. The rest of the table is added
129 * when the message is parsed.
131 static struct pair hdrs
[NVEC
+ 1] = {
132 { "source", NULL
, P_HID
},
133 { "addr", NULL
, P_HID
},
134 { "Return-Path", NULL
, P_ADR
},
135 { "Reply-To", NULL
, P_ADR
},
136 { "From", NULL
, P_ADR
},
137 { "Sender", NULL
, P_ADR
},
138 { "To", NULL
, P_ADR
},
139 { "cc", NULL
, P_ADR
},
140 { "Resent-Reply-To", NULL
, P_ADR
},
141 { "Resent-From", NULL
, P_ADR
},
142 { "Resent-Sender", NULL
, P_ADR
},
143 { "Resent-To", NULL
, P_ADR
},
144 { "Resent-cc", NULL
, P_ADR
},
149 * The list of builtin variables to expand in a string
150 * before it is executed by the "pipe" or "qpipe" action.
152 static struct pair vars
[] = {
153 { "sender", NULL
, P_NIL
},
154 { "address", NULL
, P_NIL
},
155 { "size", NULL
, P_NIL
},
156 { "reply-to", NULL
, P_CHK
},
157 { "info", NULL
, P_NIL
},
161 extern char **environ
;
166 static int localmail (int, char *);
167 static int usr_delivery (int, char *, int);
168 static int split (char *, char **);
169 static int parse (int);
170 static void expand (char *, char *, int);
171 static void glob (int);
172 static struct pair
*lookup (struct pair
*, char *) PURE
;
173 static int logged_in (void);
174 static int timely (char *, char *);
175 static int usr_file (int, char *, int);
176 static int usr_pipe (int, char *, char *, char **, int);
177 static int usr_folder (int, char *);
178 static void alrmser (int);
179 static void get_sender (char *, char **);
180 static int copy_message (int, char *, int);
181 static void verbose_printf (char *fmt
, ...) CHECK_PRINTF(1, 2);
182 static void adorn (char *, char *, ...) CHECK_PRINTF(2, 3);
183 static void debug_printf (char *fmt
, ...) CHECK_PRINTF(1, 2);
184 static int suppress_duplicates (int, char *);
185 static char *trim (char *);
189 main (int argc
, char **argv
)
193 char *cp
, *mdlvr
= NULL
, buf
[BUFSIZ
];
194 char mailbox
[BUFSIZ
], tmpfil
[BUFSIZ
];
195 char **argp
, **arguments
;
197 if (nmh_init(argv
[0], false, false)) { return 1; }
200 arguments
= getarguments (invo_name
, argc
, argv
, 0);
203 /* Parse arguments */
204 while ((cp
= *argp
++)) {
206 switch (smatch (++cp
, switches
)) {
208 ambigsw (cp
, switches
);
211 die("-%s unknown", cp
);
214 snprintf (buf
, sizeof(buf
), "%s [switches]", invo_name
);
215 print_help (buf
, switches
, 0);
218 print_version(invo_name
);
222 if (!(addr
= *argp
++))/* allow -xyz arguments */
223 die("missing argument to %s", argp
[-2]);
226 if (!(info
= *argp
++))/* allow -xyz arguments */
227 die("missing argument to %s", argp
[-2]);
230 if (!(user
= *argp
++))/* allow -xyz arguments */
231 die("missing argument to %s", argp
[-2]);
234 if (!(file
= *argp
++) || *file
== '-')
235 die("missing argument to %s", argp
[-2]);
238 if (!(sender
= *argp
++))/* allow -xyz arguments */
239 die("missing argument to %s", argp
[-2]);
242 if (!(mbox
= *argp
++) || *mbox
== '-')
243 die("missing argument to %s", argp
[-2]);
246 if (!(home
= *argp
++) || *home
== '-')
247 die("missing argument to %s", argp
[-2]);
251 if (!(cp
= *argp
++) || *cp
== '-')
252 die("missing argument to %s", argp
[-2]);
254 die("only one maildelivery file at a time!");
276 die("only switch arguments are supported");
281 addr
= getusername ();
283 user
= getusername ();
285 if ((pw
= getpwnam (user
)) == NULL
)
286 die("no such local user as %s", user
);
288 if (chdir (pw
->pw_dir
) == -1)
289 if (chdir ("/") < 0) {
290 advise ("/", "chdir");
294 if (geteuid() == 0) {
295 if (setgid (pw
->pw_gid
) != 0) {
296 adios ("setgid", "unable to set group to %ld", (long) pw
->pw_gid
);
298 initgroups (pw
->pw_name
, pw
->pw_gid
);
299 if (setuid (pw
->pw_uid
) != 0) {
300 adios ("setuid", "unable to set user to %ld", (long) pw
->pw_uid
);
307 setbuf (stdin
, NULL
);
309 /* Record the delivery time */
310 if ((now
= dlocaltimenow ()) == NULL
)
311 die("unable to ascertain local time");
312 snprintf (ddate
, sizeof(ddate
), "Delivery-Date: %s\n", dtimenow (0));
315 * Copy the message to a temporary file
320 /* getting message from file */
321 if ((tempfd
= open (file
, O_RDONLY
)) == -1)
322 adios (file
, "unable to open");
324 debug_printf ("retrieving message from file \"%s\"\n", file
);
325 if ((fd
= copy_message (tempfd
, tmpfil
, 1)) == -1)
326 die("unable to create temporary file in %s",
330 /* getting message from stdin */
332 debug_printf ("retrieving message from stdin\n");
333 if ((fd
= copy_message (fileno (stdin
), tmpfil
, 1)) == -1)
334 die("unable to create temporary file in %s",
339 debug_printf ("temporary file=\"%s\"\n", tmpfil
);
341 /* Delete the temp file now or a copy of every single message passed through
342 slocal will be left in the /tmp directory until deleted manually! This
343 unlink() used to be under an 'else' of the 'if (debug)' above, but since
344 some people like to always run slocal with -debug and log the results,
345 the /tmp directory would get choked over time. Of course, now that we
346 always delete the temp file, the "temporary file=" message above is
347 somewhat pointless -- someone watching debug output wouldn't have a
348 chance to 'tail -f' or 'ln' the temp file before it's unlinked. The best
349 thing would be to delay this unlink() until later if debug == 1, but I'll
350 leave that for someone who cares about the temp-file-accessing
351 functionality (they'll have to watch out for cases where we adios()). */
352 (void) m_unlink (tmpfil
);
354 if (!(fp
= fdopen (fd
, "r+")))
355 die("unable to access temporary file");
358 * If no sender given, extract it
359 * from envelope information. */
361 get_sender (envelope
, &sender
);
364 snprintf (mailbox
, sizeof(mailbox
), "%s/%s",
365 mmdfldir
[0] ? mmdfldir
: pw
->pw_dir
,
366 mmdflfil
[0] ? mmdflfil
: pw
->pw_name
);
373 debug_printf ("addr=\"%s\"\n", trim(addr
));
374 debug_printf ("user=\"%s\"\n", trim(user
));
375 debug_printf ("info=\"%s\"\n", trim(info
));
376 debug_printf ("sender=\"%s\"\n", trim(sender
));
377 debug_printf ("envelope=\"%s\"\n", envelope
? trim(envelope
) : "");
378 debug_printf ("mbox=\"%s\"\n", trim(mbox
));
379 debug_printf ("home=\"%s\"\n", trim(home
));
380 debug_printf ("ddate=\"%s\"\n", trim(ddate
));
381 debug_printf ("now=%02d:%02d\n\n", now
->tw_hour
, now
->tw_min
);
384 /* deliver the message */
385 status
= localmail (fd
, mdlvr
);
387 done (status
!= -1 ? RCV_MOK
: RCV_MBX
);
393 * Main routine for delivering message.
397 localmail (int fd
, char *mdlvr
)
399 /* check if this message is a duplicate */
401 suppress_duplicates(fd
, mdlvr
? mdlvr
: ".maildelivery") == DONE
)
404 /* delivery according to personal Maildelivery file */
405 if (usr_delivery (fd
, mdlvr
? mdlvr
: ".maildelivery", 0) != -1)
408 /* delivery according to global Maildelivery file */
409 if (usr_delivery (fd
, maildelivery
, 1) != -1)
413 verbose_printf ("(delivering to standard mail spool)\n");
415 /* last resort - deliver to standard mail spool */
416 return usr_file (fd
, mbox
, MBOX_FORMAT
);
420 #define matches(a,b) (stringdex (b, a) >= 0)
423 * Parse the delivery file, and process incoming message.
427 usr_delivery (int fd
, char *delivery
, int su
)
435 char *field
, *pattern
, *action
, *result
, *string
;
436 char buffer
[BUFSIZ
], tmpbuf
[BUFSIZ
];
442 /* open the delivery file */
443 if ((fp
= fopen (delivery
, "r")) == NULL
)
446 /* check if delivery file has bad ownership or permissions */
447 if (fstat (fileno (fp
), &st
) == -1
448 || (st
.st_uid
!= 0 && (su
|| st
.st_uid
!= pw
->pw_uid
))
449 || st
.st_mode
& (S_IWGRP
|S_IWOTH
)) {
451 verbose_printf ("WARNING: %s has bad ownership/modes (su=%d,uid=%d,owner=%d,mode=0%o)\n",
452 delivery
, su
, (int) pw
->pw_uid
, (int) st
.st_uid
, (int) st
.st_mode
);
460 /* read and process delivery file */
461 while (fgets (buffer
, sizeof(buffer
), fp
)) {
462 /* skip comments and empty lines */
463 if (*buffer
== '#' || *buffer
== '\n')
466 trim_suffix_c(buffer
, '\n');
468 /* split buffer into fields */
469 vecp
= split (buffer
, vec
);
471 /* check for too few fields */
474 debug_printf ("WARNING: entry with only %d fields, skipping.\n", vecp
);
479 for (i
= 0; vec
[i
]; i
++)
480 debug_printf ("vec[%d]: \"%s\"\n", i
, trim(vec
[i
]));
489 /* find out how to perform the action */
494 * If previous condition failed, don't
495 * do this - else fall through
503 * If already delivered, skip this action. Else
504 * consider delivered if action is successful.
513 * Take action, and consider delivered if
514 * action is successful.
523 * Take action, but don't consider delivered, even
524 * if action is successful
531 if (!strcasecmp (vec
[5], "select")) {
532 if (logged_in () != -1)
534 if (vecp
> 7 && timely (vec
[6], vec
[7]) == -1)
539 /* check if the field matches */
547 * "default" matches only if the message hasn't
548 * been delivered yet.
550 if (!strcasecmp (field
, "default")) {
558 /* parse message and build lookup table */
559 if (!parsed
&& parse (fd
) == -1) {
564 * find header field in lookup table, and
565 * see if the pattern matches.
567 if ((p
= lookup (hdrs
, field
)) && (p
->p_value
!= NULL
)
568 && matches (p
->p_value
, pattern
)) {
577 /* find out the action to perform */
580 /* deliver to quoted pipe */
581 if (strcasecmp (action
, "qpipe"))
585 expand (tmpbuf
, string
, fd
);
586 if (split (tmpbuf
, vec
) < 1)
588 status
= usr_pipe (fd
, tmpbuf
, vec
[0], vec
, 0);
592 /* deliver to pipe */
593 if (strcasecmp (action
, "pipe"))
599 expand (tmpbuf
, string
, fd
);
602 status
= usr_pipe (fd
, tmpbuf
, "/bin/sh", vec
+ 2, 0);
607 if (!strcasecmp (action
, "file")) {
608 status
= usr_file (fd
, string
, MBOX_FORMAT
);
611 /* deliver to nmh folder */
612 else if (strcasecmp (action
, "folder"))
616 status
= usr_folder (fd
, string
);
621 if (!strcasecmp (action
, "mmdf")) {
622 status
= usr_file (fd
, string
, MMDF_FORMAT
);
626 else if (strcasecmp (action
, "mbox"))
632 status
= usr_file (fd
, string
, MBOX_FORMAT
);
637 if (strcasecmp (action
, "destroy"))
643 if (status
) next
= false; /* action failed, mark for 'N' result */
645 if (accept
&& status
== 0)
657 * Split buffer into fields (delimited by whitespace or
658 * comma's). Return the number of fields found.
662 split (char *cp
, char **vec
)
669 /* split into a maximum of NVEC fields */
670 for (i
= 0; i
<= NVEC
;) {
673 /* zap any whitespace and comma's */
674 while (isspace ((unsigned char) *s
) || *s
== ',')
677 /* end of buffer, time to leave */
681 /* get double quote text as a single field */
683 for (vec
[i
++] = ++s
; *s
&& *s
!= '"'; s
++) {
685 * Check for escaped double quote. We need
686 * to shift the string to remove slash.
694 if (*s
== '"') /* zap trailing double quote */
699 if (*s
== QUOTE
&& *++s
!= '"')
703 /* move forward to next field delimiter */
704 while (*s
&& !isspace ((unsigned char) *s
) && *s
!= ',')
714 * Parse the headers of a message, and build the
715 * lookup table for matching fields and patterns.
724 char name
[NAMESZ
], field
[NMH_BUFSIZ
];
727 m_getfld_state_t gstate
;
732 /* get a new FILE pointer to message */
733 if ((fd1
= dup (fd
)) == -1)
735 if ((in
= fdopen (fd1
, "r")) == NULL
) {
741 /* add special entries to lookup table */
742 if ((p
= lookup (hdrs
, "source")))
743 p
->p_value
= getcpy (sender
);
744 if ((p
= lookup (hdrs
, "addr")))
745 p
->p_value
= getcpy (addr
);
748 * Scan the headers of the message and build
751 gstate
= m_getfld_state_init(in
);
753 int fieldsz
= sizeof field
;
754 switch (state
= m_getfld2(&gstate
, name
, field
, &fieldsz
)) {
757 lp
= mh_xstrdup(field
);
758 while (state
== FLDPLUS
) {
759 fieldsz
= sizeof field
;
760 state
= m_getfld2(&gstate
, name
, field
, &fieldsz
);
761 lp
= add (field
, lp
);
763 for (p
= hdrs
; p
->p_name
; p
++) {
764 if (!strcasecmp (p
->p_name
, name
)) {
765 if (!(p
->p_flags
& P_HID
)) {
766 if ((cp
= p
->p_value
)) {
767 if (p
->p_flags
& P_ADR
) {
768 dp
= cp
+ strlen (cp
) - 1;
771 cp
= add (",\n\t", cp
);
776 p
->p_value
= add (lp
, cp
);
782 if (p
->p_name
== NULL
&& i
< NVEC
) {
783 p
->p_name
= mh_xstrdup(name
);
797 inform("format error in message");
801 inform("internal error in m_getfld2");
807 m_getfld_state_destroy (&gstate
);
810 if ((p
= lookup (vars
, "reply-to"))) {
811 if ((q
= lookup (hdrs
, "reply-to")) == NULL
|| q
->p_value
== NULL
)
812 q
= lookup (hdrs
, "from");
813 p
->p_value
= getcpy (q
? q
->p_value
: "");
814 p
->p_flags
&= ~P_CHK
;
816 debug_printf ("vars[%td]: name=\"%s\" value=\"%s\"\n",
817 p
- vars
, p
->p_name
, trim(p
->p_value
));
820 for (p
= hdrs
; p
->p_name
; p
++)
821 debug_printf ("hdrs[%td]: name=\"%s\" value=\"%s\"\n",
822 p
- hdrs
, p
->p_name
, p
->p_value
? trim(p
->p_value
) : "");
833 * Expand any builtin variables such as $(sender),
834 * $(address), etc., in a string.
838 expand (char *s1
, char *s2
, int fd
)
846 while ((c
= *s2
++)) {
847 if (c
!= '$' || *s2
!= LPAREN
) {
850 for (cp
= ++s2
; *s2
&& *s2
!= RPAREN
; s2
++)
857 if ((p
= lookup (vars
, cp
))) {
858 if (!parsed
&& (p
->p_flags
& P_CHK
))
861 strcpy (s1
, p
->p_value
);
871 * Fill in the information missing from the "vars"
872 * table, which is necessary to expand any builtin
873 * variables in the string for a "pipe" or "qpipe"
887 if ((p
= lookup (vars
, "sender")))
888 p
->p_value
= getcpy (sender
);
889 if ((p
= lookup (vars
, "address")))
890 p
->p_value
= getcpy (addr
);
891 if ((p
= lookup (vars
, "size"))) {
892 snprintf (buffer
, sizeof(buffer
), "%d",
893 fstat (fd
, &st
) != -1 ? (int) st
.st_size
: 0);
894 p
->p_value
= mh_xstrdup(buffer
);
896 if ((p
= lookup (vars
, "info")))
897 p
->p_value
= getcpy (info
);
900 for (p
= vars
; p
->p_name
; p
++)
901 debug_printf ("vars[%td]: name=\"%s\" value=\"%s\"\n",
902 p
- vars
, p
->p_name
, trim(p
->p_value
));
908 * Find a matching name in a lookup table. If found,
909 * return the "pairs" entry, else return NULL.
913 lookup (struct pair
*pairs
, char *key
)
915 for (; pairs
->p_name
; pairs
++)
916 if (!strcasecmp (pairs
->p_name
, key
))
924 * Check utmp(x) file to see if user is currently
939 while ((utp
= getutxent()) != NULL
) {
940 if ( utp
->ut_type
== USER_PROCESS
&& utp
->ut_user
[0] != 0
941 && strncmp (user
, utp
->ut_user
, sizeof(utp
->ut_user
)) == 0) {
945 return utmped
= DONE
;
950 #endif /* HAVE_GETUTXENT */
951 return utmped
= NOTOK
;
954 #define check(t,a,b) if (t < a || t > b) return -1
955 #define cmpar(h1,m1,h2,m2) if (h1 < h2 || (h1 == h2 && m1 < m2)) return 0
958 timely (char *t1
, char *t2
)
960 int t1hours
, t1mins
, t2hours
, t2mins
;
962 if (sscanf (t1
, "%d:%d", &t1hours
, &t1mins
) != 2)
964 check (t1hours
, 0, 23);
965 check (t1mins
, 0, 59);
967 if (sscanf (t2
, "%d:%d", &t2hours
, &t2mins
) != 2)
969 check (t2hours
, 0, 23);
970 check (t2mins
, 0, 59);
972 cmpar (now
->tw_hour
, now
->tw_min
, t1hours
, t1mins
);
973 cmpar (t2hours
, t2mins
, now
->tw_hour
, now
->tw_min
);
980 * Deliver message by appending to a file.
984 usr_file (int fd
, char *mailbox
, int mbx_style
)
989 verbose_printf("delivering to file \"%s\" (%s style)", mailbox
,
990 mbx_style
== MBOX_FORMAT
? "mbox" : "mmdf");
993 /* open and lock the file */
994 if ((md
= mbx_open (mailbox
, mbx_style
, pw
->pw_uid
, pw
->pw_gid
, m_gmprot())) == -1) {
996 adorn ("", "unable to open:");
1000 lseek(fd
, 0, SEEK_SET
);
1002 /* append message to file */
1003 if (mbx_copy (mailbox
, mbx_style
, md
, fd
, NULL
) == -1) {
1005 adorn ("", "error writing to:");
1009 /* close and unlock file */
1010 if (mbx_close (mailbox
, md
) == NOTOK
) {
1012 adorn ("", "error closing:");
1017 verbose_printf (", success.\n");
1023 * Deliver message to a nmh folder.
1027 usr_folder (int fd
, char *string
)
1030 char folder
[BUFSIZ
], *vec
[3];
1032 /* get folder name ready */
1034 strncpy(folder
, string
, sizeof(folder
));
1036 snprintf(folder
, sizeof(folder
), "+%s", string
);
1039 verbose_printf ("delivering to folder \"%s\"", folder
+ 1);
1041 vec
[0] = "rcvstore";
1045 /* use rcvstore to put message in folder */
1046 status
= usr_pipe (fd
, "rcvstore", rcvstoreproc
, vec
, 1);
1052 * Deliver message to a process.
1056 usr_pipe (int fd_arg
, char *cmd
, char *pgm
, char **vec
, int suppress
)
1058 volatile int fd
= fd_arg
;
1060 int bytes
, seconds
, status
;
1063 if (verbose
&& !suppress
)
1064 verbose_printf ("delivering to pipe \"%s\"", cmd
);
1066 lseek(fd
, 0, SEEK_SET
);
1073 adorn ("fork", "unable to");
1080 if (freopen ("/dev/null", "w", stdout
) == NULL
) {
1081 advise ("stdout", "freopen");
1083 if (freopen ("/dev/null", "w", stderr
) == NULL
) {
1084 advise ("stderr", "freopen");
1091 if ((fd
= open ("/dev/tty", O_RDWR
)) != -1) {
1092 ioctl (fd
, TIOCNOTTY
, NULL
);
1095 #endif /* TIOCNOTTY */
1097 setpgid(0, getpid()); /* put in own process group */
1100 setenv("USER", pw
->pw_name
, 1);
1101 setenv("HOME", pw
->pw_dir
, 1);
1102 setenv("SHELL", pw
->pw_shell
, 1);
1108 /* parent process */
1109 if (! setjmp (myctx
)) {
1110 SIGNAL (SIGALRM
, alrmser
);
1111 bytes
= fstat (fd
, &st
) != -1 ? (int) st
.st_size
: 100;
1113 /* amount of time to wait depends on message size */
1115 /* give at least 5 minutes */
1117 } else if (bytes
>= 90000) {
1118 /* a half hour is long enough */
1121 seconds
= (bytes
/ 60) + 300;
1123 alarm ((unsigned int) seconds
);
1124 status
= pidwait (child_id
, 0);
1129 verbose_printf (", success.\n");
1130 else if ((status
& 0xff00) == 0xff00)
1131 verbose_printf (", system error\n");
1133 pidstatus (status
, stdout
, ", failed");
1135 return status
== 0 ? 0 : -1;
1138 * Ruthlessly kill the child and anything
1139 * else in its process group.
1141 killpg(child_id
, SIGKILL
);
1143 verbose_printf (", timed-out; terminated\n");
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
= mh_xstrdup("");
1176 strncpy (buffer
, envelope
+ i
, sizeof(buffer
));
1177 if ((cp
= strchr(buffer
, '\n'))) {
1187 for (cp
= buffer
+ strlen (buffer
) - 1; cp
>= buffer
; cp
--)
1188 if (isspace ((unsigned char) *cp
))
1192 *sender
= mh_xstrdup(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
];
1210 tfile
= m_mktemp2(NULL
, invo_name
, &fd1
, NULL
);
1211 if (tfile
== NULL
) return -1;
1212 strncpy (tmpfil
, tfile
, BUFSIZ
);
1215 while ((i
= read (qd
, buffer
, sizeof(buffer
))) > 0)
1216 if (write (fd1
, buffer
, i
) != i
) {
1219 (void) m_unlink (tmpfil
);
1224 lseek(fd1
, 0, SEEK_SET
);
1228 /* dup the fd for incoming message */
1229 if ((fd2
= dup (qd
)) == -1) {
1234 /* now create a FILE pointer for it */
1235 if ((qfp
= fdopen (fd2
, "r")) == NULL
) {
1241 /* dup the fd for temporary file */
1242 if ((fd2
= dup (fd1
)) == -1) {
1248 /* now create a FILE pointer for it */
1249 if ((ffp
= fdopen (fd2
, "r+")) == NULL
) {
1257 * copy message into temporary file
1258 * and massage the headers. Save
1259 * a copy of the "From " line for later.
1261 while (fgets (buffer
, sizeof(buffer
), qfp
)) {
1264 if (has_prefix(buffer
, "From ")) {
1265 /* get copy of envelope information ("From " line) */
1266 envelope
= mh_xstrdup(buffer
);
1268 /* Put the delivery date in message */
1277 fputs (buffer
, ffp
);
1289 lseek(fd1
, 0, SEEK_SET
);
1301 * Trim strings for pretty printing of debugging output
1307 static char buffer
[BUFSIZ
* 4];
1313 /* copy string into temp buffer */
1314 strncpy (buffer
, cp
, sizeof(buffer
));
1317 /* skip over leading whitespace */
1318 while (isspace((unsigned char) *bp
))
1321 /* start at the end and zap trailing whitespace */
1322 for (sp
= bp
+ strlen(bp
) - 1; sp
>= bp
; sp
--) {
1323 if (isspace((unsigned char) *sp
))
1329 /* replace remaining whitespace with spaces */
1330 for (sp
= bp
; *sp
; sp
++)
1331 if (isspace((unsigned char) *sp
))
1338 * Function for printing `verbose' messages.
1342 verbose_printf (char *fmt
, ...)
1350 fflush (stdout
); /* now flush output */
1355 * Function for printing `verbose' delivery
1360 adorn (char *what
, char *fmt
, ...)
1366 eindex
= errno
; /* save the errno */
1367 fputs(", ", stdout
);
1375 printf(" %s: ", what
);
1376 if ((s
= strerror (eindex
)))
1379 printf("Error %d", eindex
);
1388 * Function for printing `debug' messages.
1392 debug_printf (char *fmt
, ...)
1397 vfprintf (stderr
, fmt
, ap
);
1403 * Check ndbm/db file(s) to see if the Message-Id of this
1404 * message matches the Message-Id of a previous message,
1405 * so we can discard it. If it doesn't match, we add the
1406 * Message-Id of this message to the ndbm/db file.
1409 suppress_duplicates (int fd
, char *file
)
1411 int fd1
, lockfd
, state
, result
;
1412 char *cp
, buf
[NMH_BUFSIZ
], name
[NAMESZ
];
1416 m_getfld_state_t gstate
;
1418 if ((fd1
= dup (fd
)) == -1)
1420 if (!(in
= fdopen (fd1
, "r"))) {
1426 gstate
= m_getfld_state_init(in
);
1428 int failed_to_lock
= 0;
1429 int bufsz
= sizeof buf
;
1430 state
= m_getfld2(&gstate
, name
, buf
, &bufsz
);
1434 /* Search for the message ID */
1435 if (strcasecmp (name
, "Message-ID")) {
1436 while (state
== FLDPLUS
) {
1438 state
= m_getfld2(&gstate
, name
, buf
, &bufsz
);
1443 cp
= mh_xstrdup(buf
);
1444 while (state
== FLDPLUS
) {
1446 state
= m_getfld2(&gstate
, name
, buf
, &bufsz
);
1449 key
.dptr
= trimcpy (cp
);
1450 key
.dsize
= strlen (key
.dptr
) + 1;
1454 if (!(db
= dbm_open (file
, O_RDWR
| O_CREAT
, 0600))) {
1455 advise (file
, "unable to perform dbm_open on");
1461 * Since it is difficult to portable lock a ndbm file,
1462 * we will open and lock the Maildelivery file instead.
1463 * This will fail if your Maildelivery file doesn't
1466 if ((lockfd
= lkopendata(file
, O_RDWR
, 0, &failed_to_lock
))
1468 advise (file
, "unable to perform file locking on");
1473 value
= dbm_fetch (db
, key
);
1476 verbose_printf ("Message-ID: %s\n already received on %s",
1480 value
.dptr
= ddate
+ sizeof("Delivery-Date:");
1481 value
.dsize
= strlen(value
.dptr
) + 1;
1482 if (dbm_store (db
, key
, value
, DBM_INSERT
))
1483 advise (file
, "possibly corrupt file");
1488 lkclosedata(lockfd
, file
);
1505 m_getfld_state_destroy (&gstate
);