]>
diplodocus.org Git - nmh/blob - uip/slocal.c
cc1aa7bc321630d1173f72a4eb079b0b746d8c4e
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/getarguments.h"
24 #include "sbr/smatch.h"
25 #include "sbr/strindex.h"
26 #include "sbr/closefds.h"
27 #include "sbr/trimcpy.h"
28 #include "sbr/getcpy.h"
29 #include "sbr/ambigsw.h"
30 #include "sbr/pidstatus.h"
31 #include "sbr/print_version.h"
32 #include "sbr/print_help.h"
33 #include "sbr/error.h"
34 #include "h/dropsbr.h"
35 #include "h/rcvmail.h"
36 #include "h/signals.h"
42 #include "sbr/lock_file.h"
43 #include "sbr/m_mktemp.h"
46 #include <sys/ioctl.h>
49 /* Hopefully, grp.h declares initgroups(). If we run into a platform
50 where it doesn't, we could consider declaring it here as well. */
53 /* This define is needed for Berkeley db v2 and above to
54 * make the header file expose the 'historical' ndbm APIs.
55 * We define it unconditionally because this is simple and
58 #define DB_DBM_HSEARCH 1
60 #endif /* Use DB_DBM_HSEARCH to prevent warning from gcc -Wunused-macros. */
67 #endif /* HAVE_GETUTXENT */
69 #define SLOCAL_SWITCHES \
70 X("addr address", 0, ADDRSW) \
71 X("user name", 0, USERSW) \
72 X("file file", 0, FILESW) \
73 X("sender address", 0, SENDERSW) \
74 X("mailbox file", 0, MAILBOXSW) \
75 X("home directory", -4, HOMESW) \
76 X("info data", 0, INFOSW) \
77 X("maildelivery file", 0, MAILSW) \
78 X("verbose", 0, VERBSW) \
79 X("noverbose", 0, NVERBSW) \
80 X("suppressdup", 0, SUPPRESSDUP) \
81 X("nosuppressdup", 0, NSUPPRESSDUP) \
82 X("debug", 0, DEBUGSW) \
83 X("version", 0, VERSIONSW) \
84 X("help", 0, HELPSW) \
86 #define X(sw, minchars, id) id,
87 DEFINE_SWITCH_ENUM(SLOCAL
);
90 #define X(sw, minchars, id) { sw, minchars, id },
91 DEFINE_SWITCH_ARRAY(SLOCAL
, switches
);
94 static int globbed
= 0; /* have we built "vars" table yet? */
95 static int parsed
= 0; /* have we built header field table yet */
96 static int utmped
= 0; /* have we scanned umtp(x) file yet */
97 static bool suppressdup
; /* are we suppressing duplicate messages? */
102 static char *addr
= NULL
;
103 static char *user
= NULL
;
104 static char *info
= NULL
;
105 static char *file
= NULL
;
106 static char *sender
= NULL
;
107 static char *envelope
= NULL
; /* envelope information ("From " line) */
108 static char *mbox
= NULL
;
109 static char *home
= NULL
;
111 static struct passwd
*pw
; /* passwd file entry */
113 static char ddate
[BUFSIZ
]; /* record the delivery date */
116 static jmp_buf myctx
;
118 /* flags for pair->p_flags */
120 #define P_ADR 0x01 /* field is address */
121 #define P_HID 0x02 /* special (fake) field */
133 * Lookup table for matching fields and patterns
134 * in messages. The rest of the table is added
135 * when the message is parsed.
137 static struct pair hdrs
[NVEC
+ 1] = {
138 { "source", NULL
, P_HID
},
139 { "addr", NULL
, P_HID
},
140 { "Return-Path", NULL
, P_ADR
},
141 { "Reply-To", NULL
, P_ADR
},
142 { "From", NULL
, P_ADR
},
143 { "Sender", NULL
, P_ADR
},
144 { "To", NULL
, P_ADR
},
145 { "cc", NULL
, P_ADR
},
146 { "Resent-Reply-To", NULL
, P_ADR
},
147 { "Resent-From", NULL
, P_ADR
},
148 { "Resent-Sender", NULL
, P_ADR
},
149 { "Resent-To", NULL
, P_ADR
},
150 { "Resent-cc", NULL
, P_ADR
},
155 * The list of builtin variables to expand in a string
156 * before it is executed by the "pipe" or "qpipe" action.
158 static struct pair vars
[] = {
159 { "sender", NULL
, P_NIL
},
160 { "address", NULL
, P_NIL
},
161 { "size", NULL
, P_NIL
},
162 { "reply-to", NULL
, P_CHK
},
163 { "info", NULL
, P_NIL
},
167 extern char **environ
;
172 static int localmail (int, char *);
173 static int usr_delivery (int, char *, int);
174 static int split (char *, char **);
175 static int parse (int);
176 static void expand (char *, char *, int);
177 static void glob (int);
178 static struct pair
*lookup (struct pair
*, char *) PURE
;
179 static int logged_in (void);
180 static int timely (char *, char *);
181 static int usr_file (int, char *, int);
182 static int usr_pipe (int, char *, char *, char **, int);
183 static int usr_folder (int, char *);
184 static void alrmser (int);
185 static void get_sender (char *, char **);
186 static int copy_message (int, char *, int);
187 static void verbose_printf (char *fmt
, ...) CHECK_PRINTF(1, 2);
188 static void adorn (char *, char *, ...) CHECK_PRINTF(2, 3);
189 static void debug_printf (char *fmt
, ...) CHECK_PRINTF(1, 2);
190 static int suppress_duplicates (int, char *);
191 static char *trim (char *);
195 main (int argc
, char **argv
)
199 char *cp
, *mdlvr
= NULL
, buf
[BUFSIZ
];
200 char mailbox
[BUFSIZ
], tmpfil
[BUFSIZ
];
201 char **argp
, **arguments
;
203 if (nmh_init(argv
[0], false, false)) { return 1; }
206 arguments
= getarguments (invo_name
, argc
, argv
, 0);
209 /* Parse arguments */
210 while ((cp
= *argp
++)) {
212 switch (smatch (++cp
, switches
)) {
214 ambigsw (cp
, switches
);
217 die("-%s unknown", cp
);
220 snprintf (buf
, sizeof(buf
), "%s [switches]", invo_name
);
221 print_help (buf
, switches
, 0);
224 print_version(invo_name
);
228 if (!(addr
= *argp
++))/* allow -xyz arguments */
229 die("missing argument to %s", argp
[-2]);
232 if (!(info
= *argp
++))/* allow -xyz arguments */
233 die("missing argument to %s", argp
[-2]);
236 if (!(user
= *argp
++))/* allow -xyz arguments */
237 die("missing argument to %s", argp
[-2]);
240 if (!(file
= *argp
++) || *file
== '-')
241 die("missing argument to %s", argp
[-2]);
244 if (!(sender
= *argp
++))/* allow -xyz arguments */
245 die("missing argument to %s", argp
[-2]);
248 if (!(mbox
= *argp
++) || *mbox
== '-')
249 die("missing argument to %s", argp
[-2]);
252 if (!(home
= *argp
++) || *home
== '-')
253 die("missing argument to %s", argp
[-2]);
257 if (!(cp
= *argp
++) || *cp
== '-')
258 die("missing argument to %s", argp
[-2]);
260 die("only one maildelivery file at a time!");
282 die("only switch arguments are supported");
287 addr
= getusername ();
289 user
= getusername ();
291 if ((pw
= getpwnam (user
)) == NULL
)
292 die("no such local user as %s", user
);
294 if (chdir (pw
->pw_dir
) == -1)
295 if (chdir ("/") < 0) {
296 advise ("/", "chdir");
300 if (geteuid() == 0) {
301 if (setgid (pw
->pw_gid
) != 0) {
302 adios ("setgid", "unable to set group to %ld", (long) pw
->pw_gid
);
304 initgroups (pw
->pw_name
, pw
->pw_gid
);
305 if (setuid (pw
->pw_uid
) != 0) {
306 adios ("setuid", "unable to set user to %ld", (long) pw
->pw_uid
);
313 setbuf (stdin
, NULL
);
315 /* Record the delivery time */
316 if ((now
= dlocaltimenow ()) == NULL
)
317 die("unable to ascertain local time");
318 snprintf (ddate
, sizeof(ddate
), "Delivery-Date: %s\n", dtimenow (0));
321 * Copy the message to a temporary file
326 /* getting message from file */
327 if ((tempfd
= open (file
, O_RDONLY
)) == -1)
328 adios (file
, "unable to open");
330 debug_printf ("retrieving message from file \"%s\"\n", file
);
331 if ((fd
= copy_message (tempfd
, tmpfil
, 1)) == -1)
332 die("unable to create temporary file in %s",
336 /* getting message from stdin */
338 debug_printf ("retrieving message from stdin\n");
339 if ((fd
= copy_message (fileno (stdin
), tmpfil
, 1)) == -1)
340 die("unable to create temporary file in %s",
345 debug_printf ("temporary file=\"%s\"\n", tmpfil
);
347 /* Delete the temp file now or a copy of every single message passed through
348 slocal will be left in the /tmp directory until deleted manually! This
349 unlink() used to be under an 'else' of the 'if (debug)' above, but since
350 some people like to always run slocal with -debug and log the results,
351 the /tmp directory would get choked over time. Of course, now that we
352 always delete the temp file, the "temporary file=" message above is
353 somewhat pointless -- someone watching debug output wouldn't have a
354 chance to 'tail -f' or 'ln' the temp file before it's unlinked. The best
355 thing would be to delay this unlink() until later if debug == 1, but I'll
356 leave that for someone who cares about the temp-file-accessing
357 functionality (they'll have to watch out for cases where we adios()). */
358 (void) m_unlink (tmpfil
);
360 if (!(fp
= fdopen (fd
, "r+")))
361 die("unable to access temporary file");
364 * If no sender given, extract it
365 * from envelope information. */
367 get_sender (envelope
, &sender
);
370 snprintf (mailbox
, sizeof(mailbox
), "%s/%s",
371 mmdfldir
[0] ? mmdfldir
: pw
->pw_dir
,
372 mmdflfil
[0] ? mmdflfil
: pw
->pw_name
);
379 debug_printf ("addr=\"%s\"\n", trim(addr
));
380 debug_printf ("user=\"%s\"\n", trim(user
));
381 debug_printf ("info=\"%s\"\n", trim(info
));
382 debug_printf ("sender=\"%s\"\n", trim(sender
));
383 debug_printf ("envelope=\"%s\"\n", envelope
? trim(envelope
) : "");
384 debug_printf ("mbox=\"%s\"\n", trim(mbox
));
385 debug_printf ("home=\"%s\"\n", trim(home
));
386 debug_printf ("ddate=\"%s\"\n", trim(ddate
));
387 debug_printf ("now=%02d:%02d\n\n", now
->tw_hour
, now
->tw_min
);
390 /* deliver the message */
391 status
= localmail (fd
, mdlvr
);
393 done (status
!= -1 ? RCV_MOK
: RCV_MBX
);
399 * Main routine for delivering message.
403 localmail (int fd
, char *mdlvr
)
405 /* check if this message is a duplicate */
407 suppress_duplicates(fd
, mdlvr
? mdlvr
: ".maildelivery") == DONE
)
410 /* delivery according to personal Maildelivery file */
411 if (usr_delivery (fd
, mdlvr
? mdlvr
: ".maildelivery", 0) != -1)
414 /* delivery according to global Maildelivery file */
415 if (usr_delivery (fd
, maildelivery
, 1) != -1)
419 verbose_printf ("(delivering to standard mail spool)\n");
421 /* last resort - deliver to standard mail spool */
422 return usr_file (fd
, mbox
, MBOX_FORMAT
);
426 #define matches(a,b) (stringdex (b, a) >= 0)
429 * Parse the delivery file, and process incoming message.
433 usr_delivery (int fd
, char *delivery
, int su
)
441 char *field
, *pattern
, *action
, *result
, *string
;
442 char buffer
[BUFSIZ
], tmpbuf
[BUFSIZ
];
448 /* open the delivery file */
449 if ((fp
= fopen (delivery
, "r")) == NULL
)
452 /* check if delivery file has bad ownership or permissions */
453 if (fstat (fileno (fp
), &st
) == -1
454 || (st
.st_uid
!= 0 && (su
|| st
.st_uid
!= pw
->pw_uid
))
455 || st
.st_mode
& (S_IWGRP
|S_IWOTH
)) {
457 verbose_printf ("WARNING: %s has bad ownership/modes (su=%d,uid=%d,owner=%d,mode=0%o)\n",
458 delivery
, su
, (int) pw
->pw_uid
, (int) st
.st_uid
, (int) st
.st_mode
);
466 /* read and process delivery file */
467 while (fgets (buffer
, sizeof(buffer
), fp
)) {
468 /* skip comments and empty lines */
469 if (*buffer
== '#' || *buffer
== '\n')
472 trim_suffix_c(buffer
, '\n');
474 /* split buffer into fields */
475 vecp
= split (buffer
, vec
);
477 /* check for too few fields */
480 debug_printf ("WARNING: entry with only %d fields, skipping.\n", vecp
);
485 for (i
= 0; vec
[i
]; i
++)
486 debug_printf ("vec[%d]: \"%s\"\n", i
, trim(vec
[i
]));
495 /* find out how to perform the action */
500 * If previous condition failed, don't
501 * do this - else fall through
509 * If already delivered, skip this action. Else
510 * consider delivered if action is successful.
519 * Take action, and consider delivered if
520 * action is successful.
529 * Take action, but don't consider delivered, even
530 * if action is successful
537 if (!strcasecmp (vec
[5], "select")) {
538 if (logged_in () != -1)
540 if (vecp
> 7 && timely (vec
[6], vec
[7]) == -1)
545 /* check if the field matches */
553 * "default" matches only if the message hasn't
554 * been delivered yet.
556 if (!strcasecmp (field
, "default")) {
564 /* parse message and build lookup table */
565 if (!parsed
&& parse (fd
) == -1) {
570 * find header field in lookup table, and
571 * see if the pattern matches.
573 if ((p
= lookup (hdrs
, field
)) && (p
->p_value
!= NULL
)
574 && matches (p
->p_value
, pattern
)) {
583 /* find out the action to perform */
586 /* deliver to quoted pipe */
587 if (strcasecmp (action
, "qpipe"))
591 expand (tmpbuf
, string
, fd
);
592 if (split (tmpbuf
, vec
) < 1)
594 status
= usr_pipe (fd
, tmpbuf
, vec
[0], vec
, 0);
598 /* deliver to pipe */
599 if (strcasecmp (action
, "pipe"))
605 expand (tmpbuf
, string
, fd
);
608 status
= usr_pipe (fd
, tmpbuf
, "/bin/sh", vec
+ 2, 0);
613 if (!strcasecmp (action
, "file")) {
614 status
= usr_file (fd
, string
, MBOX_FORMAT
);
617 /* deliver to nmh folder */
618 else if (strcasecmp (action
, "folder"))
622 status
= usr_folder (fd
, string
);
627 if (!strcasecmp (action
, "mmdf")) {
628 status
= usr_file (fd
, string
, MMDF_FORMAT
);
632 else if (strcasecmp (action
, "mbox"))
638 status
= usr_file (fd
, string
, MBOX_FORMAT
);
643 if (strcasecmp (action
, "destroy"))
649 if (status
) next
= false; /* action failed, mark for 'N' result */
651 if (accept
&& status
== 0)
663 * Split buffer into fields (delimited by whitespace or
664 * comma's). Return the number of fields found.
668 split (char *cp
, char **vec
)
675 /* split into a maximum of NVEC fields */
676 for (i
= 0; i
<= NVEC
;) {
679 /* zap any whitespace and comma's */
680 while (isspace ((unsigned char) *s
) || *s
== ',')
683 /* end of buffer, time to leave */
687 /* get double quote text as a single field */
689 for (vec
[i
++] = ++s
; *s
&& *s
!= '"'; s
++) {
691 * Check for escaped double quote. We need
692 * to shift the string to remove slash.
700 if (*s
== '"') /* zap trailing double quote */
705 if (*s
== QUOTE
&& *++s
!= '"')
709 /* move forward to next field delimiter */
710 while (*s
&& !isspace ((unsigned char) *s
) && *s
!= ',')
720 * Parse the headers of a message, and build the
721 * lookup table for matching fields and patterns.
730 char name
[NAMESZ
], field
[NMH_BUFSIZ
];
733 m_getfld_state_t gstate
;
738 /* get a new FILE pointer to message */
739 if ((fd1
= dup (fd
)) == -1)
741 if ((in
= fdopen (fd1
, "r")) == NULL
) {
747 /* add special entries to lookup table */
748 if ((p
= lookup (hdrs
, "source")))
749 p
->p_value
= getcpy (sender
);
750 if ((p
= lookup (hdrs
, "addr")))
751 p
->p_value
= getcpy (addr
);
754 * Scan the headers of the message and build
757 gstate
= m_getfld_state_init(in
);
759 int fieldsz
= sizeof field
;
760 switch (state
= m_getfld2(&gstate
, name
, field
, &fieldsz
)) {
763 lp
= mh_xstrdup(field
);
764 while (state
== FLDPLUS
) {
765 fieldsz
= sizeof field
;
766 state
= m_getfld2(&gstate
, name
, field
, &fieldsz
);
767 lp
= add (field
, lp
);
769 for (p
= hdrs
; p
->p_name
; p
++) {
770 if (!strcasecmp (p
->p_name
, name
)) {
771 if (!(p
->p_flags
& P_HID
)) {
772 if ((cp
= p
->p_value
)) {
773 if (p
->p_flags
& P_ADR
) {
774 dp
= cp
+ strlen (cp
) - 1;
777 cp
= add (",\n\t", cp
);
782 p
->p_value
= add (lp
, cp
);
788 if (p
->p_name
== NULL
&& i
< NVEC
) {
789 p
->p_name
= mh_xstrdup(name
);
803 inform("format error in message");
807 inform("internal error in m_getfld2");
813 m_getfld_state_destroy (&gstate
);
816 if ((p
= lookup (vars
, "reply-to"))) {
817 if ((q
= lookup (hdrs
, "reply-to")) == NULL
|| q
->p_value
== NULL
)
818 q
= lookup (hdrs
, "from");
819 p
->p_value
= getcpy (q
? q
->p_value
: "");
820 p
->p_flags
&= ~P_CHK
;
822 debug_printf ("vars[%td]: name=\"%s\" value=\"%s\"\n",
823 p
- vars
, p
->p_name
, trim(p
->p_value
));
826 for (p
= hdrs
; p
->p_name
; p
++)
827 debug_printf ("hdrs[%td]: name=\"%s\" value=\"%s\"\n",
828 p
- hdrs
, p
->p_name
, p
->p_value
? trim(p
->p_value
) : "");
839 * Expand any builtin variables such as $(sender),
840 * $(address), etc., in a string.
844 expand (char *s1
, char *s2
, int fd
)
852 while ((c
= *s2
++)) {
853 if (c
!= '$' || *s2
!= LPAREN
) {
856 for (cp
= ++s2
; *s2
&& *s2
!= RPAREN
; s2
++)
863 if ((p
= lookup (vars
, cp
))) {
864 if (!parsed
&& (p
->p_flags
& P_CHK
))
867 strcpy (s1
, p
->p_value
);
877 * Fill in the information missing from the "vars"
878 * table, which is necessary to expand any builtin
879 * variables in the string for a "pipe" or "qpipe"
893 if ((p
= lookup (vars
, "sender")))
894 p
->p_value
= getcpy (sender
);
895 if ((p
= lookup (vars
, "address")))
896 p
->p_value
= getcpy (addr
);
897 if ((p
= lookup (vars
, "size"))) {
898 snprintf (buffer
, sizeof(buffer
), "%d",
899 fstat (fd
, &st
) != -1 ? (int) st
.st_size
: 0);
900 p
->p_value
= mh_xstrdup(buffer
);
902 if ((p
= lookup (vars
, "info")))
903 p
->p_value
= getcpy (info
);
906 for (p
= vars
; p
->p_name
; p
++)
907 debug_printf ("vars[%td]: name=\"%s\" value=\"%s\"\n",
908 p
- vars
, p
->p_name
, trim(p
->p_value
));
914 * Find a matching name in a lookup table. If found,
915 * return the "pairs" entry, else return NULL.
919 lookup (struct pair
*pairs
, char *key
)
921 for (; pairs
->p_name
; pairs
++)
922 if (!strcasecmp (pairs
->p_name
, key
))
930 * Check utmp(x) file to see if user is currently
945 while ((utp
= getutxent()) != NULL
) {
946 if ( utp
->ut_type
== USER_PROCESS
&& utp
->ut_user
[0] != 0
947 && strncmp (user
, utp
->ut_user
, sizeof(utp
->ut_user
)) == 0) {
951 return utmped
= DONE
;
956 #endif /* HAVE_GETUTXENT */
957 return utmped
= NOTOK
;
960 #define check(t,a,b) if (t < a || t > b) return -1
961 #define cmpar(h1,m1,h2,m2) if (h1 < h2 || (h1 == h2 && m1 < m2)) return 0
964 timely (char *t1
, char *t2
)
966 int t1hours
, t1mins
, t2hours
, t2mins
;
968 if (sscanf (t1
, "%d:%d", &t1hours
, &t1mins
) != 2)
970 check (t1hours
, 0, 23);
971 check (t1mins
, 0, 59);
973 if (sscanf (t2
, "%d:%d", &t2hours
, &t2mins
) != 2)
975 check (t2hours
, 0, 23);
976 check (t2mins
, 0, 59);
978 cmpar (now
->tw_hour
, now
->tw_min
, t1hours
, t1mins
);
979 cmpar (t2hours
, t2mins
, now
->tw_hour
, now
->tw_min
);
986 * Deliver message by appending to a file.
990 usr_file (int fd
, char *mailbox
, int mbx_style
)
995 verbose_printf("delivering to file \"%s\" (%s style)", mailbox
,
996 mbx_style
== MBOX_FORMAT
? "mbox" : "mmdf");
999 /* open and lock the file */
1000 if ((md
= mbx_open (mailbox
, mbx_style
, pw
->pw_uid
, pw
->pw_gid
, m_gmprot())) == -1) {
1002 adorn ("", "unable to open:");
1006 lseek(fd
, 0, SEEK_SET
);
1008 /* append message to file */
1009 if (mbx_copy (mailbox
, mbx_style
, md
, fd
, NULL
) == -1) {
1011 adorn ("", "error writing to:");
1015 /* close and unlock file */
1016 if (mbx_close (mailbox
, md
) == NOTOK
) {
1018 adorn ("", "error closing:");
1023 verbose_printf (", success.\n");
1029 * Deliver message to a nmh folder.
1033 usr_folder (int fd
, char *string
)
1036 char folder
[BUFSIZ
], *vec
[3];
1038 /* get folder name ready */
1040 strncpy(folder
, string
, sizeof(folder
));
1042 snprintf(folder
, sizeof(folder
), "+%s", string
);
1045 verbose_printf ("delivering to folder \"%s\"", folder
+ 1);
1047 vec
[0] = "rcvstore";
1051 /* use rcvstore to put message in folder */
1052 status
= usr_pipe (fd
, "rcvstore", rcvstoreproc
, vec
, 1);
1058 * Deliver message to a process.
1062 usr_pipe (int fd_arg
, char *cmd
, char *pgm
, char **vec
, int suppress
)
1064 volatile int fd
= fd_arg
;
1066 int bytes
, seconds
, status
;
1069 if (verbose
&& !suppress
)
1070 verbose_printf ("delivering to pipe \"%s\"", cmd
);
1072 lseek(fd
, 0, SEEK_SET
);
1079 adorn ("fork", "unable to");
1086 if (freopen ("/dev/null", "w", stdout
) == NULL
) {
1087 advise ("stdout", "freopen");
1089 if (freopen ("/dev/null", "w", stderr
) == NULL
) {
1090 advise ("stderr", "freopen");
1097 if ((fd
= open ("/dev/tty", O_RDWR
)) != -1) {
1098 ioctl (fd
, TIOCNOTTY
, NULL
);
1101 #endif /* TIOCNOTTY */
1103 setpgid(0, getpid()); /* put in own process group */
1106 setenv("USER", pw
->pw_name
, 1);
1107 setenv("HOME", pw
->pw_dir
, 1);
1108 setenv("SHELL", pw
->pw_shell
, 1);
1114 /* parent process */
1115 if (! setjmp (myctx
)) {
1116 SIGNAL (SIGALRM
, alrmser
);
1117 bytes
= fstat (fd
, &st
) != -1 ? (int) st
.st_size
: 100;
1119 /* amount of time to wait depends on message size */
1121 /* give at least 5 minutes */
1123 } else if (bytes
>= 90000) {
1124 /* a half hour is long enough */
1127 seconds
= (bytes
/ 60) + 300;
1129 alarm ((unsigned int) seconds
);
1130 status
= pidwait (child_id
, 0);
1135 verbose_printf (", success.\n");
1136 else if ((status
& 0xff00) == 0xff00)
1137 verbose_printf (", system error\n");
1139 pidstatus (status
, stdout
, ", failed");
1141 return status
== 0 ? 0 : -1;
1144 * Ruthlessly kill the child and anything
1145 * else in its process group.
1147 killpg(child_id
, SIGKILL
);
1149 verbose_printf (", timed-out; terminated\n");
1160 longjmp (myctx
, DONE
);
1165 * Get the `sender' from the envelope
1166 * information ("From " line).
1170 get_sender (char *envelope
, char **sender
)
1174 char buffer
[BUFSIZ
];
1176 if (envelope
== NULL
) {
1177 *sender
= mh_xstrdup("");
1182 strncpy (buffer
, envelope
+ i
, sizeof(buffer
));
1183 if ((cp
= strchr(buffer
, '\n'))) {
1193 for (cp
= buffer
+ strlen (buffer
) - 1; cp
>= buffer
; cp
--)
1194 if (isspace ((unsigned char) *cp
))
1198 *sender
= mh_xstrdup(buffer
);
1203 * Copy message into a temporary file.
1204 * While copying, it will do some header processing
1205 * including the extraction of the envelope information.
1209 copy_message (int qd
, char *tmpfil
, int fold
)
1211 int i
, first
= 1, fd1
, fd2
;
1212 char buffer
[BUFSIZ
];
1216 tfile
= m_mktemp2(NULL
, invo_name
, &fd1
, NULL
);
1217 if (tfile
== NULL
) return -1;
1218 strncpy (tmpfil
, tfile
, BUFSIZ
);
1221 while ((i
= read (qd
, buffer
, sizeof(buffer
))) > 0)
1222 if (write (fd1
, buffer
, i
) != i
) {
1225 (void) m_unlink (tmpfil
);
1230 lseek(fd1
, 0, SEEK_SET
);
1234 /* dup the fd for incoming message */
1235 if ((fd2
= dup (qd
)) == -1) {
1240 /* now create a FILE pointer for it */
1241 if ((qfp
= fdopen (fd2
, "r")) == NULL
) {
1247 /* dup the fd for temporary file */
1248 if ((fd2
= dup (fd1
)) == -1) {
1254 /* now create a FILE pointer for it */
1255 if ((ffp
= fdopen (fd2
, "r+")) == NULL
) {
1263 * copy message into temporary file
1264 * and massage the headers. Save
1265 * a copy of the "From " line for later.
1267 while (fgets (buffer
, sizeof(buffer
), qfp
)) {
1270 if (has_prefix(buffer
, "From ")) {
1271 /* get copy of envelope information ("From " line) */
1272 envelope
= mh_xstrdup(buffer
);
1274 /* Put the delivery date in message */
1283 fputs (buffer
, ffp
);
1295 lseek(fd1
, 0, SEEK_SET
);
1307 * Trim strings for pretty printing of debugging output
1313 static char buffer
[BUFSIZ
* 4];
1319 /* copy string into temp buffer */
1320 strncpy (buffer
, cp
, sizeof(buffer
));
1323 /* skip over leading whitespace */
1324 while (isspace((unsigned char) *bp
))
1327 /* start at the end and zap trailing whitespace */
1328 for (sp
= bp
+ strlen(bp
) - 1; sp
>= bp
; sp
--) {
1329 if (isspace((unsigned char) *sp
))
1335 /* replace remaining whitespace with spaces */
1336 for (sp
= bp
; *sp
; sp
++)
1337 if (isspace((unsigned char) *sp
))
1344 * Function for printing `verbose' messages.
1348 verbose_printf (char *fmt
, ...)
1356 fflush (stdout
); /* now flush output */
1361 * Function for printing `verbose' delivery
1366 adorn (char *what
, char *fmt
, ...)
1372 eindex
= errno
; /* save the errno */
1373 fputs(", ", stdout
);
1381 printf(" %s: ", what
);
1382 if ((s
= strerror (eindex
)))
1385 printf("Error %d", eindex
);
1394 * Function for printing `debug' messages.
1398 debug_printf (char *fmt
, ...)
1403 vfprintf (stderr
, fmt
, ap
);
1409 * Check ndbm/db file(s) to see if the Message-Id of this
1410 * message matches the Message-Id of a previous message,
1411 * so we can discard it. If it doesn't match, we add the
1412 * Message-Id of this message to the ndbm/db file.
1415 suppress_duplicates (int fd
, char *file
)
1417 int fd1
, lockfd
, state
, result
;
1418 char *cp
, buf
[NMH_BUFSIZ
], name
[NAMESZ
];
1422 m_getfld_state_t gstate
;
1424 if ((fd1
= dup (fd
)) == -1)
1426 if (!(in
= fdopen (fd1
, "r"))) {
1432 gstate
= m_getfld_state_init(in
);
1434 int failed_to_lock
= 0;
1435 int bufsz
= sizeof buf
;
1436 state
= m_getfld2(&gstate
, name
, buf
, &bufsz
);
1440 /* Search for the message ID */
1441 if (strcasecmp (name
, "Message-ID")) {
1442 while (state
== FLDPLUS
) {
1444 state
= m_getfld2(&gstate
, name
, buf
, &bufsz
);
1449 cp
= mh_xstrdup(buf
);
1450 while (state
== FLDPLUS
) {
1452 state
= m_getfld2(&gstate
, name
, buf
, &bufsz
);
1455 key
.dptr
= trimcpy (cp
);
1456 key
.dsize
= strlen (key
.dptr
) + 1;
1460 if (!(db
= dbm_open (file
, O_RDWR
| O_CREAT
, 0600))) {
1461 advise (file
, "unable to perform dbm_open on");
1467 * Since it is difficult to portable lock a ndbm file,
1468 * we will open and lock the Maildelivery file instead.
1469 * This will fail if your Maildelivery file doesn't
1472 if ((lockfd
= lkopendata(file
, O_RDWR
, 0, &failed_to_lock
))
1474 advise (file
, "unable to perform file locking on");
1479 value
= dbm_fetch (db
, key
);
1482 verbose_printf ("Message-ID: %s\n already received on %s",
1486 value
.dptr
= ddate
+ sizeof("Delivery-Date:");
1487 value
.dsize
= strlen(value
.dptr
) + 1;
1488 if (dbm_store (db
, key
, value
, DBM_INSERT
))
1489 advise (file
, "possibly corrupt file");
1494 lkclosedata(lockfd
, file
);
1511 m_getfld_state_destroy (&gstate
);