]>
diplodocus.org Git - nmh/blob - uip/slocal.c
8cca5e355951bbe74a9309af6edd3a61fecd2b57
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/dtime.h"
24 #include "sbr/m_gmprot.h"
25 #include "sbr/m_getfld.h"
26 #include "sbr/getarguments.h"
27 #include "sbr/smatch.h"
28 #include "sbr/strindex.h"
29 #include "sbr/trimcpy.h"
30 #include "sbr/getcpy.h"
31 #include "sbr/ambigsw.h"
32 #include "sbr/pidstatus.h"
33 #include "sbr/print_version.h"
34 #include "sbr/print_help.h"
35 #include "sbr/error.h"
36 #include "h/dropsbr.h"
37 #include "h/signals.h"
43 #include "sbr/lock_file.h"
44 #include "sbr/m_mktemp.h"
47 #include <sys/ioctl.h>
50 /* Hopefully, grp.h declares initgroups(). If we run into a platform
51 where it doesn't, we could consider declaring it here as well. */
54 /* This define is needed for Berkeley db v2 and above to
55 * make the header file expose the 'historical' ndbm APIs.
56 * We define it unconditionally because this is simple and
59 #define DB_DBM_HSEARCH 1
61 #endif /* Use DB_DBM_HSEARCH to prevent warning from gcc -Wunused-macros. */
68 #endif /* HAVE_GETUTXENT */
70 #define SLOCAL_SWITCHES \
71 X("addr address", 0, ADDRSW) \
72 X("user name", 0, USERSW) \
73 X("file file", 0, FILESW) \
74 X("sender address", 0, SENDERSW) \
75 X("mailbox file", 0, MAILBOXSW) \
76 X("home directory", -4, HOMESW) \
77 X("info data", 0, INFOSW) \
78 X("maildelivery file", 0, MAILSW) \
79 X("verbose", 0, VERBSW) \
80 X("noverbose", 0, NVERBSW) \
81 X("suppressdup", 0, SUPPRESSDUP) \
82 X("nosuppressdup", 0, NSUPPRESSDUP) \
83 X("debug", 0, DEBUGSW) \
84 X("version", 0, VERSIONSW) \
85 X("help", 0, HELPSW) \
87 #define X(sw, minchars, id) id,
88 DEFINE_SWITCH_ENUM(SLOCAL
);
91 #define X(sw, minchars, id) { sw, minchars, id },
92 DEFINE_SWITCH_ARRAY(SLOCAL
, switches
);
95 static int globbed
= 0; /* have we built "vars" table yet? */
96 static int parsed
= 0; /* have we built header field table yet */
97 static int utmped
= 0; /* have we scanned umtp(x) file yet */
98 static bool suppressdup
; /* are we suppressing duplicate messages? */
103 static char *addr
= NULL
;
104 static char *user
= NULL
;
105 static char *info
= NULL
;
106 static char *file
= NULL
;
107 static char *sender
= NULL
;
108 static char *envelope
= NULL
; /* envelope information ("From " line) */
109 static char *mbox
= NULL
;
110 static char *home
= NULL
;
112 static struct passwd
*pw
; /* passwd file entry */
114 static char ddate
[BUFSIZ
]; /* record the delivery date */
117 static jmp_buf myctx
;
119 /* flags for pair->p_flags */
121 #define P_ADR 0x01 /* field is address */
122 #define P_HID 0x02 /* special (fake) field */
134 * Lookup table for matching fields and patterns
135 * in messages. The rest of the table is added
136 * when the message is parsed.
138 static struct pair hdrs
[NVEC
+ 1] = {
139 { "source", NULL
, P_HID
},
140 { "addr", NULL
, P_HID
},
141 { "Return-Path", NULL
, P_ADR
},
142 { "Reply-To", NULL
, P_ADR
},
143 { "From", NULL
, P_ADR
},
144 { "Sender", NULL
, P_ADR
},
145 { "To", NULL
, P_ADR
},
146 { "cc", NULL
, P_ADR
},
147 { "Resent-Reply-To", NULL
, P_ADR
},
148 { "Resent-From", NULL
, P_ADR
},
149 { "Resent-Sender", NULL
, P_ADR
},
150 { "Resent-To", NULL
, P_ADR
},
151 { "Resent-cc", NULL
, P_ADR
},
156 * The list of builtin variables to expand in a string
157 * before it is executed by the "pipe" or "qpipe" action.
159 static struct pair vars
[] = {
160 { "sender", NULL
, P_NIL
},
161 { "address", NULL
, P_NIL
},
162 { "size", NULL
, P_NIL
},
163 { "reply-to", NULL
, P_CHK
},
164 { "info", NULL
, P_NIL
},
168 extern char **environ
;
173 static int localmail (int, char *);
174 static int usr_delivery (int, char *, int);
175 static int split (char *, char **);
176 static int parse (int);
177 static void expand (char *, char *, int);
178 static void glob (int);
179 static struct pair
*lookup (struct pair
*, char *) PURE
;
180 static int logged_in (void);
181 static int timely (char *, char *);
182 static int usr_file (int, char *, int);
183 static int usr_pipe (int, char *, char *, char **, int);
184 static int usr_folder (int, char *);
185 static void alrmser (int);
186 static void get_sender (char *, char **);
187 static int copy_message (int, char *, int);
188 static void verbose_printf (char *fmt
, ...) CHECK_PRINTF(1, 2);
189 static void adorn (char *, char *, ...) CHECK_PRINTF(2, 3);
190 static void debug_printf (char *fmt
, ...) CHECK_PRINTF(1, 2);
191 static int suppress_duplicates (int, char *);
192 static char *trim (char *);
196 main (int argc
, char **argv
)
200 char *cp
, *mdlvr
= NULL
, buf
[BUFSIZ
];
201 char mailbox
[BUFSIZ
], tmpfil
[BUFSIZ
];
202 char **argp
, **arguments
;
204 if (nmh_init(argv
[0], false, false)) { return 1; }
207 arguments
= getarguments (invo_name
, argc
, argv
, 0);
210 /* Parse arguments */
211 while ((cp
= *argp
++)) {
213 switch (smatch (++cp
, switches
)) {
215 ambigsw (cp
, switches
);
218 die("-%s unknown", cp
);
221 snprintf (buf
, sizeof(buf
), "%s [switches]", invo_name
);
222 print_help (buf
, switches
, 0);
225 print_version(invo_name
);
229 if (!(addr
= *argp
++))/* allow -xyz arguments */
230 die("missing argument to %s", argp
[-2]);
233 if (!(info
= *argp
++))/* allow -xyz arguments */
234 die("missing argument to %s", argp
[-2]);
237 if (!(user
= *argp
++))/* allow -xyz arguments */
238 die("missing argument to %s", argp
[-2]);
241 if (!(file
= *argp
++) || *file
== '-')
242 die("missing argument to %s", argp
[-2]);
245 if (!(sender
= *argp
++))/* allow -xyz arguments */
246 die("missing argument to %s", argp
[-2]);
249 if (!(mbox
= *argp
++) || *mbox
== '-')
250 die("missing argument to %s", argp
[-2]);
253 if (!(home
= *argp
++) || *home
== '-')
254 die("missing argument to %s", argp
[-2]);
258 if (!(cp
= *argp
++) || *cp
== '-')
259 die("missing argument to %s", argp
[-2]);
261 die("only one maildelivery file at a time!");
283 die("only switch arguments are supported");
288 addr
= getusername ();
290 user
= getusername ();
292 if ((pw
= getpwnam (user
)) == NULL
)
293 die("no such local user as %s", user
);
295 if (chdir (pw
->pw_dir
) == -1)
296 if (chdir ("/") < 0) {
297 advise ("/", "chdir");
301 if (geteuid() == 0) {
302 if (setgid (pw
->pw_gid
) != 0) {
303 adios ("setgid", "unable to set group to %ld", (long) pw
->pw_gid
);
305 initgroups (pw
->pw_name
, pw
->pw_gid
);
306 if (setuid (pw
->pw_uid
) != 0) {
307 adios ("setuid", "unable to set user to %ld", (long) pw
->pw_uid
);
314 setbuf (stdin
, NULL
);
316 /* Record the delivery time */
317 if ((now
= dlocaltimenow ()) == NULL
)
318 die("unable to ascertain local time");
319 snprintf (ddate
, sizeof(ddate
), "Delivery-Date: %s\n", dtimenow (0));
322 * Copy the message to a temporary file
327 /* getting message from file */
328 if ((tempfd
= open (file
, O_RDONLY
)) == -1)
329 adios (file
, "unable to open");
331 debug_printf ("retrieving message from file \"%s\"\n", file
);
332 if ((fd
= copy_message (tempfd
, tmpfil
, 1)) == -1)
333 die("unable to create temporary file in %s",
337 /* getting message from stdin */
339 debug_printf ("retrieving message from stdin\n");
340 if ((fd
= copy_message (fileno (stdin
), tmpfil
, 1)) == -1)
341 die("unable to create temporary file in %s",
346 debug_printf ("temporary file=\"%s\"\n", tmpfil
);
348 /* Delete the temp file now or a copy of every single message passed through
349 slocal will be left in the /tmp directory until deleted manually! This
350 unlink() used to be under an 'else' of the 'if (debug)' above, but since
351 some people like to always run slocal with -debug and log the results,
352 the /tmp directory would get choked over time. Of course, now that we
353 always delete the temp file, the "temporary file=" message above is
354 somewhat pointless -- someone watching debug output wouldn't have a
355 chance to 'tail -f' or 'ln' the temp file before it's unlinked. The best
356 thing would be to delay this unlink() until later if debug == 1, but I'll
357 leave that for someone who cares about the temp-file-accessing
358 functionality (they'll have to watch out for cases where we adios()). */
359 (void) m_unlink (tmpfil
);
361 if (!(fp
= fdopen (fd
, "r+")))
362 die("unable to access temporary file");
365 * If no sender given, extract it
366 * from envelope information. */
368 get_sender (envelope
, &sender
);
371 snprintf (mailbox
, sizeof(mailbox
), "%s/%s",
372 mmdfldir
[0] ? mmdfldir
: pw
->pw_dir
,
373 mmdflfil
[0] ? mmdflfil
: pw
->pw_name
);
380 debug_printf ("addr=\"%s\"\n", trim(addr
));
381 debug_printf ("user=\"%s\"\n", trim(user
));
382 debug_printf ("info=\"%s\"\n", trim(info
));
383 debug_printf ("sender=\"%s\"\n", trim(sender
));
384 debug_printf ("envelope=\"%s\"\n", envelope
? trim(envelope
) : "");
385 debug_printf ("mbox=\"%s\"\n", trim(mbox
));
386 debug_printf ("home=\"%s\"\n", trim(home
));
387 debug_printf ("ddate=\"%s\"\n", trim(ddate
));
388 debug_printf ("now=%02d:%02d\n\n", now
->tw_hour
, now
->tw_min
);
391 /* deliver the message */
392 status
= localmail (fd
, mdlvr
);
394 done(status
!= -1 ? 0 : 1);
400 * Main routine for delivering message.
404 localmail (int fd
, char *mdlvr
)
406 /* check if this message is a duplicate */
408 suppress_duplicates(fd
, mdlvr
? mdlvr
: ".maildelivery") == DONE
)
411 /* delivery according to personal Maildelivery file */
412 if (usr_delivery (fd
, mdlvr
? mdlvr
: ".maildelivery", 0) != -1)
415 /* delivery according to global Maildelivery file */
416 if (usr_delivery (fd
, maildelivery
, 1) != -1)
420 verbose_printf ("(delivering to standard mail spool)\n");
422 /* last resort - deliver to standard mail spool */
423 return usr_file (fd
, mbox
, MBOX_FORMAT
);
427 #define matches(a,b) (stringdex (b, a) >= 0)
430 * Parse the delivery file, and process incoming message.
434 usr_delivery (int fd
, char *delivery
, int su
)
442 char *field
, *pattern
, *action
, *result
, *string
;
443 char buffer
[BUFSIZ
], tmpbuf
[BUFSIZ
];
449 /* open the delivery file */
450 if ((fp
= fopen (delivery
, "r")) == NULL
)
453 /* check if delivery file has bad ownership or permissions */
454 if (fstat (fileno (fp
), &st
) == -1
455 || (st
.st_uid
!= 0 && (su
|| st
.st_uid
!= pw
->pw_uid
))
456 || st
.st_mode
& (S_IWGRP
|S_IWOTH
)) {
458 verbose_printf ("WARNING: %s has bad ownership/modes (su=%d,uid=%d,owner=%d,mode=0%o)\n",
459 delivery
, su
, (int) pw
->pw_uid
, (int) st
.st_uid
, (int) st
.st_mode
);
467 /* read and process delivery file */
468 while (fgets (buffer
, sizeof(buffer
), fp
)) {
469 /* skip comments and empty lines */
470 if (*buffer
== '#' || *buffer
== '\n')
473 trim_suffix_c(buffer
, '\n');
475 /* split buffer into fields */
476 vecp
= split (buffer
, vec
);
478 /* check for too few fields */
481 debug_printf ("WARNING: entry with only %d fields, skipping.\n", vecp
);
486 for (i
= 0; vec
[i
]; i
++)
487 debug_printf ("vec[%d]: \"%s\"\n", i
, trim(vec
[i
]));
496 /* find out how to perform the action */
501 * If previous condition failed, don't
502 * do this - else fall through
510 * If already delivered, skip this action. Else
511 * consider delivered if action is successful.
520 * Take action, and consider delivered if
521 * action is successful.
530 * Take action, but don't consider delivered, even
531 * if action is successful
538 if (!strcasecmp (vec
[5], "select")) {
539 if (logged_in () != -1)
541 if (vecp
> 7 && timely (vec
[6], vec
[7]) == -1)
546 /* check if the field matches */
554 * "default" matches only if the message hasn't
555 * been delivered yet.
557 if (!strcasecmp (field
, "default")) {
565 /* parse message and build lookup table */
566 if (!parsed
&& parse (fd
) == -1) {
571 * find header field in lookup table, and
572 * see if the pattern matches.
574 if ((p
= lookup (hdrs
, field
)) && (p
->p_value
!= NULL
)
575 && matches (p
->p_value
, pattern
)) {
584 /* find out the action to perform */
587 /* deliver to quoted pipe */
588 if (strcasecmp (action
, "qpipe"))
592 expand (tmpbuf
, string
, fd
);
593 if (split (tmpbuf
, vec
) < 1)
595 status
= usr_pipe (fd
, tmpbuf
, vec
[0], vec
, 0);
599 /* deliver to pipe */
600 if (strcasecmp (action
, "pipe"))
606 expand (tmpbuf
, string
, fd
);
609 status
= usr_pipe (fd
, tmpbuf
, "/bin/sh", vec
+ 2, 0);
614 if (!strcasecmp (action
, "file")) {
615 status
= usr_file (fd
, string
, MBOX_FORMAT
);
618 /* deliver to nmh folder */
619 else if (strcasecmp (action
, "folder"))
623 status
= usr_folder (fd
, string
);
628 if (!strcasecmp (action
, "mmdf")) {
629 status
= usr_file (fd
, string
, MMDF_FORMAT
);
633 else if (strcasecmp (action
, "mbox"))
639 status
= usr_file (fd
, string
, MBOX_FORMAT
);
644 if (strcasecmp (action
, "destroy"))
650 if (status
) next
= false; /* action failed, mark for 'N' result */
652 if (accept
&& status
== 0)
664 * Split buffer into fields (delimited by whitespace or
665 * comma's). Return the number of fields found.
669 split (char *cp
, char **vec
)
676 /* split into a maximum of NVEC fields */
677 for (i
= 0; i
<= NVEC
;) {
680 /* zap any whitespace and comma's */
681 while (isspace ((unsigned char) *s
) || *s
== ',')
684 /* end of buffer, time to leave */
688 /* get double quote text as a single field */
690 for (vec
[i
++] = ++s
; *s
&& *s
!= '"'; s
++) {
692 * Check for escaped double quote. We need
693 * to shift the string to remove slash.
701 if (*s
== '"') /* zap trailing double quote */
706 if (*s
== QUOTE
&& *++s
!= '"')
710 /* move forward to next field delimiter */
711 while (*s
&& !isspace ((unsigned char) *s
) && *s
!= ',')
721 * Parse the headers of a message, and build the
722 * lookup table for matching fields and patterns.
731 char name
[NAMESZ
], field
[NMH_BUFSIZ
];
734 m_getfld_state_t gstate
;
739 /* get a new FILE pointer to message */
740 if ((fd1
= dup (fd
)) == -1)
742 if ((in
= fdopen (fd1
, "r")) == NULL
) {
748 /* add special entries to lookup table */
749 if ((p
= lookup (hdrs
, "source")))
750 p
->p_value
= getcpy (sender
);
751 if ((p
= lookup (hdrs
, "addr")))
752 p
->p_value
= getcpy (addr
);
755 * Scan the headers of the message and build
758 gstate
= m_getfld_state_init(in
);
760 int fieldsz
= sizeof field
;
761 switch (state
= m_getfld2(&gstate
, name
, field
, &fieldsz
)) {
764 lp
= mh_xstrdup(field
);
765 while (state
== FLDPLUS
) {
766 fieldsz
= sizeof field
;
767 state
= m_getfld2(&gstate
, name
, field
, &fieldsz
);
768 lp
= add (field
, lp
);
770 for (p
= hdrs
; p
->p_name
; p
++) {
771 if (!strcasecmp (p
->p_name
, name
)) {
772 if (!(p
->p_flags
& P_HID
)) {
773 if ((cp
= p
->p_value
)) {
774 if (p
->p_flags
& P_ADR
) {
775 dp
= cp
+ strlen (cp
) - 1;
778 cp
= add (",\n\t", cp
);
783 p
->p_value
= add (lp
, cp
);
789 if (p
->p_name
== NULL
&& i
< NVEC
) {
790 p
->p_name
= mh_xstrdup(name
);
804 inform("format error in message");
808 inform("internal error in m_getfld2");
814 m_getfld_state_destroy (&gstate
);
817 if ((p
= lookup (vars
, "reply-to"))) {
818 if ((q
= lookup (hdrs
, "reply-to")) == NULL
|| q
->p_value
== NULL
)
819 q
= lookup (hdrs
, "from");
820 p
->p_value
= getcpy (q
? q
->p_value
: "");
821 p
->p_flags
&= ~P_CHK
;
823 debug_printf ("vars[%td]: name=\"%s\" value=\"%s\"\n",
824 p
- vars
, p
->p_name
, trim(p
->p_value
));
827 for (p
= hdrs
; p
->p_name
; p
++)
828 debug_printf ("hdrs[%td]: name=\"%s\" value=\"%s\"\n",
829 p
- hdrs
, p
->p_name
, p
->p_value
? trim(p
->p_value
) : "");
840 * Expand any builtin variables such as $(sender),
841 * $(address), etc., in a string.
845 expand (char *s1
, char *s2
, int fd
)
853 while ((c
= *s2
++)) {
854 if (c
!= '$' || *s2
!= LPAREN
) {
857 for (cp
= ++s2
; *s2
&& *s2
!= RPAREN
; s2
++)
864 if ((p
= lookup (vars
, cp
))) {
865 if (!parsed
&& (p
->p_flags
& P_CHK
))
868 strcpy (s1
, p
->p_value
);
878 * Fill in the information missing from the "vars"
879 * table, which is necessary to expand any builtin
880 * variables in the string for a "pipe" or "qpipe"
894 if ((p
= lookup (vars
, "sender")))
895 p
->p_value
= getcpy (sender
);
896 if ((p
= lookup (vars
, "address")))
897 p
->p_value
= getcpy (addr
);
898 if ((p
= lookup (vars
, "size"))) {
899 snprintf (buffer
, sizeof(buffer
), "%d",
900 fstat (fd
, &st
) != -1 ? (int) st
.st_size
: 0);
901 p
->p_value
= mh_xstrdup(buffer
);
903 if ((p
= lookup (vars
, "info")))
904 p
->p_value
= getcpy (info
);
907 for (p
= vars
; p
->p_name
; p
++)
908 debug_printf ("vars[%td]: name=\"%s\" value=\"%s\"\n",
909 p
- vars
, p
->p_name
, trim(p
->p_value
));
915 * Find a matching name in a lookup table. If found,
916 * return the "pairs" entry, else return NULL.
920 lookup (struct pair
*pairs
, char *key
)
922 for (; pairs
->p_name
; pairs
++)
923 if (!strcasecmp (pairs
->p_name
, key
))
931 * Check utmp(x) file to see if user is currently
946 while ((utp
= getutxent()) != NULL
) {
947 if ( utp
->ut_type
== USER_PROCESS
&& utp
->ut_user
[0] != 0
948 && strncmp (user
, utp
->ut_user
, sizeof(utp
->ut_user
)) == 0) {
952 return utmped
= DONE
;
957 #endif /* HAVE_GETUTXENT */
958 return utmped
= NOTOK
;
961 #define check(t,a,b) if (t < a || t > b) return -1
962 #define cmpar(h1,m1,h2,m2) if (h1 < h2 || (h1 == h2 && m1 < m2)) return 0
965 timely (char *t1
, char *t2
)
967 int t1hours
, t1mins
, t2hours
, t2mins
;
969 if (sscanf (t1
, "%d:%d", &t1hours
, &t1mins
) != 2)
971 check (t1hours
, 0, 23);
972 check (t1mins
, 0, 59);
974 if (sscanf (t2
, "%d:%d", &t2hours
, &t2mins
) != 2)
976 check (t2hours
, 0, 23);
977 check (t2mins
, 0, 59);
979 cmpar (now
->tw_hour
, now
->tw_min
, t1hours
, t1mins
);
980 cmpar (t2hours
, t2mins
, now
->tw_hour
, now
->tw_min
);
987 * Deliver message by appending to a file.
991 usr_file (int fd
, char *mailbox
, int mbx_style
)
996 verbose_printf("delivering to file \"%s\" (%s style)", mailbox
,
997 mbx_style
== MBOX_FORMAT
? "mbox" : "mmdf");
1000 /* open and lock the file */
1001 if ((md
= mbx_open (mailbox
, mbx_style
, pw
->pw_uid
, pw
->pw_gid
, m_gmprot())) == -1) {
1003 adorn ("", "unable to open:");
1007 lseek(fd
, 0, SEEK_SET
);
1009 /* append message to file */
1010 if (mbx_copy (mailbox
, mbx_style
, md
, fd
, NULL
) == -1) {
1012 adorn ("", "error writing to:");
1016 /* close and unlock file */
1017 if (mbx_close (mailbox
, md
) == NOTOK
) {
1019 adorn ("", "error closing:");
1024 verbose_printf (", success.\n");
1030 * Deliver message to a nmh folder.
1034 usr_folder (int fd
, char *string
)
1037 char folder
[BUFSIZ
], *vec
[3];
1039 /* get folder name ready */
1041 strncpy(folder
, string
, sizeof(folder
));
1043 snprintf(folder
, sizeof(folder
), "+%s", string
);
1046 verbose_printf ("delivering to folder \"%s\"", folder
+ 1);
1048 vec
[0] = "rcvstore";
1052 /* use rcvstore to put message in folder */
1053 status
= usr_pipe (fd
, "rcvstore", rcvstoreproc
, vec
, 1);
1059 * Deliver message to a process.
1063 usr_pipe (int fd_arg
, char *cmd
, char *pgm
, char **vec
, int suppress
)
1065 volatile int fd
= fd_arg
;
1067 int bytes
, seconds
, status
;
1070 if (verbose
&& !suppress
)
1071 verbose_printf ("delivering to pipe \"%s\"", cmd
);
1073 lseek(fd
, 0, SEEK_SET
);
1080 adorn ("fork", "unable to");
1087 if (freopen ("/dev/null", "w", stdout
) == NULL
) {
1088 advise ("stdout", "freopen");
1090 if (freopen ("/dev/null", "w", stderr
) == NULL
) {
1091 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
);