]>
diplodocus.org Git - nmh/blob - uip/slocal.c
e773b134ef04a909df2ecedc450f4b492b9938b6
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/print_version.h"
24 #include "sbr/print_help.h"
25 #include "sbr/error.h"
26 #include "h/dropsbr.h"
27 #include "h/rcvmail.h"
28 #include "h/signals.h"
34 #include "sbr/lock_file.h"
35 #include "sbr/m_mktemp.h"
38 #include <sys/ioctl.h>
41 /* Hopefully, grp.h declares initgroups(). If we run into a platform
42 where it doesn't, we could consider declaring it here as well. */
45 /* This define is needed for Berkeley db v2 and above to
46 * make the header file expose the 'historical' ndbm APIs.
47 * We define it unconditionally because this is simple and
50 #define DB_DBM_HSEARCH 1
52 #endif /* Use DB_DBM_HSEARCH to prevent warning from gcc -Wunused-macros. */
59 #endif /* HAVE_GETUTXENT */
61 #define SLOCAL_SWITCHES \
62 X("addr address", 0, ADDRSW) \
63 X("user name", 0, USERSW) \
64 X("file file", 0, FILESW) \
65 X("sender address", 0, SENDERSW) \
66 X("mailbox file", 0, MAILBOXSW) \
67 X("home directory", -4, HOMESW) \
68 X("info data", 0, INFOSW) \
69 X("maildelivery file", 0, MAILSW) \
70 X("verbose", 0, VERBSW) \
71 X("noverbose", 0, NVERBSW) \
72 X("suppressdup", 0, SUPPRESSDUP) \
73 X("nosuppressdup", 0, NSUPPRESSDUP) \
74 X("debug", 0, DEBUGSW) \
75 X("version", 0, VERSIONSW) \
76 X("help", 0, HELPSW) \
78 #define X(sw, minchars, id) id,
79 DEFINE_SWITCH_ENUM(SLOCAL
);
82 #define X(sw, minchars, id) { sw, minchars, id },
83 DEFINE_SWITCH_ARRAY(SLOCAL
, switches
);
86 static int globbed
= 0; /* have we built "vars" table yet? */
87 static int parsed
= 0; /* have we built header field table yet */
88 static int utmped
= 0; /* have we scanned umtp(x) file yet */
89 static bool suppressdup
; /* are we suppressing duplicate messages? */
94 static char *addr
= NULL
;
95 static char *user
= NULL
;
96 static char *info
= NULL
;
97 static char *file
= NULL
;
98 static char *sender
= NULL
;
99 static char *envelope
= NULL
; /* envelope information ("From " line) */
100 static char *mbox
= NULL
;
101 static char *home
= NULL
;
103 static struct passwd
*pw
; /* passwd file entry */
105 static char ddate
[BUFSIZ
]; /* record the delivery date */
108 static jmp_buf myctx
;
110 /* flags for pair->p_flags */
112 #define P_ADR 0x01 /* field is address */
113 #define P_HID 0x02 /* special (fake) field */
125 * Lookup table for matching fields and patterns
126 * in messages. The rest of the table is added
127 * when the message is parsed.
129 static struct pair hdrs
[NVEC
+ 1] = {
130 { "source", NULL
, P_HID
},
131 { "addr", NULL
, P_HID
},
132 { "Return-Path", NULL
, P_ADR
},
133 { "Reply-To", NULL
, P_ADR
},
134 { "From", NULL
, P_ADR
},
135 { "Sender", NULL
, P_ADR
},
136 { "To", NULL
, P_ADR
},
137 { "cc", NULL
, P_ADR
},
138 { "Resent-Reply-To", NULL
, P_ADR
},
139 { "Resent-From", NULL
, P_ADR
},
140 { "Resent-Sender", NULL
, P_ADR
},
141 { "Resent-To", NULL
, P_ADR
},
142 { "Resent-cc", NULL
, P_ADR
},
147 * The list of builtin variables to expand in a string
148 * before it is executed by the "pipe" or "qpipe" action.
150 static struct pair vars
[] = {
151 { "sender", NULL
, P_NIL
},
152 { "address", NULL
, P_NIL
},
153 { "size", NULL
, P_NIL
},
154 { "reply-to", NULL
, P_CHK
},
155 { "info", NULL
, P_NIL
},
159 extern char **environ
;
164 static int localmail (int, char *);
165 static int usr_delivery (int, char *, int);
166 static int split (char *, char **);
167 static int parse (int);
168 static void expand (char *, char *, int);
169 static void glob (int);
170 static struct pair
*lookup (struct pair
*, char *) PURE
;
171 static int logged_in (void);
172 static int timely (char *, char *);
173 static int usr_file (int, char *, int);
174 static int usr_pipe (int, char *, char *, char **, int);
175 static int usr_folder (int, char *);
176 static void alrmser (int);
177 static void get_sender (char *, char **);
178 static int copy_message (int, char *, int);
179 static void verbose_printf (char *fmt
, ...) CHECK_PRINTF(1, 2);
180 static void adorn (char *, char *, ...) CHECK_PRINTF(2, 3);
181 static void debug_printf (char *fmt
, ...) CHECK_PRINTF(1, 2);
182 static int suppress_duplicates (int, char *);
183 static char *trim (char *);
187 main (int argc
, char **argv
)
191 char *cp
, *mdlvr
= NULL
, buf
[BUFSIZ
];
192 char mailbox
[BUFSIZ
], tmpfil
[BUFSIZ
];
193 char **argp
, **arguments
;
195 if (nmh_init(argv
[0], false, false)) { return 1; }
198 arguments
= getarguments (invo_name
, argc
, argv
, 0);
201 /* Parse arguments */
202 while ((cp
= *argp
++)) {
204 switch (smatch (++cp
, switches
)) {
206 ambigsw (cp
, switches
);
209 die("-%s unknown", cp
);
212 snprintf (buf
, sizeof(buf
), "%s [switches]", invo_name
);
213 print_help (buf
, switches
, 0);
216 print_version(invo_name
);
220 if (!(addr
= *argp
++))/* allow -xyz arguments */
221 die("missing argument to %s", argp
[-2]);
224 if (!(info
= *argp
++))/* allow -xyz arguments */
225 die("missing argument to %s", argp
[-2]);
228 if (!(user
= *argp
++))/* allow -xyz arguments */
229 die("missing argument to %s", argp
[-2]);
232 if (!(file
= *argp
++) || *file
== '-')
233 die("missing argument to %s", argp
[-2]);
236 if (!(sender
= *argp
++))/* allow -xyz arguments */
237 die("missing argument to %s", argp
[-2]);
240 if (!(mbox
= *argp
++) || *mbox
== '-')
241 die("missing argument to %s", argp
[-2]);
244 if (!(home
= *argp
++) || *home
== '-')
245 die("missing argument to %s", argp
[-2]);
249 if (!(cp
= *argp
++) || *cp
== '-')
250 die("missing argument to %s", argp
[-2]);
252 die("only one maildelivery file at a time!");
274 die("only switch arguments are supported");
279 addr
= getusername ();
281 user
= getusername ();
283 if ((pw
= getpwnam (user
)) == NULL
)
284 die("no such local user as %s", user
);
286 if (chdir (pw
->pw_dir
) == -1)
287 if (chdir ("/") < 0) {
288 advise ("/", "chdir");
292 if (geteuid() == 0) {
293 if (setgid (pw
->pw_gid
) != 0) {
294 adios ("setgid", "unable to set group to %ld", (long) pw
->pw_gid
);
296 initgroups (pw
->pw_name
, pw
->pw_gid
);
297 if (setuid (pw
->pw_uid
) != 0) {
298 adios ("setuid", "unable to set user to %ld", (long) pw
->pw_uid
);
305 setbuf (stdin
, NULL
);
307 /* Record the delivery time */
308 if ((now
= dlocaltimenow ()) == NULL
)
309 die("unable to ascertain local time");
310 snprintf (ddate
, sizeof(ddate
), "Delivery-Date: %s\n", dtimenow (0));
313 * Copy the message to a temporary file
318 /* getting message from file */
319 if ((tempfd
= open (file
, O_RDONLY
)) == -1)
320 adios (file
, "unable to open");
322 debug_printf ("retrieving message from file \"%s\"\n", file
);
323 if ((fd
= copy_message (tempfd
, tmpfil
, 1)) == -1)
324 die("unable to create temporary file in %s",
328 /* getting message from stdin */
330 debug_printf ("retrieving message from stdin\n");
331 if ((fd
= copy_message (fileno (stdin
), tmpfil
, 1)) == -1)
332 die("unable to create temporary file in %s",
337 debug_printf ("temporary file=\"%s\"\n", tmpfil
);
339 /* Delete the temp file now or a copy of every single message passed through
340 slocal will be left in the /tmp directory until deleted manually! This
341 unlink() used to be under an 'else' of the 'if (debug)' above, but since
342 some people like to always run slocal with -debug and log the results,
343 the /tmp directory would get choked over time. Of course, now that we
344 always delete the temp file, the "temporary file=" message above is
345 somewhat pointless -- someone watching debug output wouldn't have a
346 chance to 'tail -f' or 'ln' the temp file before it's unlinked. The best
347 thing would be to delay this unlink() until later if debug == 1, but I'll
348 leave that for someone who cares about the temp-file-accessing
349 functionality (they'll have to watch out for cases where we adios()). */
350 (void) m_unlink (tmpfil
);
352 if (!(fp
= fdopen (fd
, "r+")))
353 die("unable to access temporary file");
356 * If no sender given, extract it
357 * from envelope information. */
359 get_sender (envelope
, &sender
);
362 snprintf (mailbox
, sizeof(mailbox
), "%s/%s",
363 mmdfldir
[0] ? mmdfldir
: pw
->pw_dir
,
364 mmdflfil
[0] ? mmdflfil
: pw
->pw_name
);
371 debug_printf ("addr=\"%s\"\n", trim(addr
));
372 debug_printf ("user=\"%s\"\n", trim(user
));
373 debug_printf ("info=\"%s\"\n", trim(info
));
374 debug_printf ("sender=\"%s\"\n", trim(sender
));
375 debug_printf ("envelope=\"%s\"\n", envelope
? trim(envelope
) : "");
376 debug_printf ("mbox=\"%s\"\n", trim(mbox
));
377 debug_printf ("home=\"%s\"\n", trim(home
));
378 debug_printf ("ddate=\"%s\"\n", trim(ddate
));
379 debug_printf ("now=%02d:%02d\n\n", now
->tw_hour
, now
->tw_min
);
382 /* deliver the message */
383 status
= localmail (fd
, mdlvr
);
385 done (status
!= -1 ? RCV_MOK
: RCV_MBX
);
391 * Main routine for delivering message.
395 localmail (int fd
, char *mdlvr
)
397 /* check if this message is a duplicate */
399 suppress_duplicates(fd
, mdlvr
? mdlvr
: ".maildelivery") == DONE
)
402 /* delivery according to personal Maildelivery file */
403 if (usr_delivery (fd
, mdlvr
? mdlvr
: ".maildelivery", 0) != -1)
406 /* delivery according to global Maildelivery file */
407 if (usr_delivery (fd
, maildelivery
, 1) != -1)
411 verbose_printf ("(delivering to standard mail spool)\n");
413 /* last resort - deliver to standard mail spool */
414 return usr_file (fd
, mbox
, MBOX_FORMAT
);
418 #define matches(a,b) (stringdex (b, a) >= 0)
421 * Parse the delivery file, and process incoming message.
425 usr_delivery (int fd
, char *delivery
, int su
)
433 char *field
, *pattern
, *action
, *result
, *string
;
434 char buffer
[BUFSIZ
], tmpbuf
[BUFSIZ
];
440 /* open the delivery file */
441 if ((fp
= fopen (delivery
, "r")) == NULL
)
444 /* check if delivery file has bad ownership or permissions */
445 if (fstat (fileno (fp
), &st
) == -1
446 || (st
.st_uid
!= 0 && (su
|| st
.st_uid
!= pw
->pw_uid
))
447 || st
.st_mode
& (S_IWGRP
|S_IWOTH
)) {
449 verbose_printf ("WARNING: %s has bad ownership/modes (su=%d,uid=%d,owner=%d,mode=0%o)\n",
450 delivery
, su
, (int) pw
->pw_uid
, (int) st
.st_uid
, (int) st
.st_mode
);
458 /* read and process delivery file */
459 while (fgets (buffer
, sizeof(buffer
), fp
)) {
460 /* skip comments and empty lines */
461 if (*buffer
== '#' || *buffer
== '\n')
464 trim_suffix_c(buffer
, '\n');
466 /* split buffer into fields */
467 vecp
= split (buffer
, vec
);
469 /* check for too few fields */
472 debug_printf ("WARNING: entry with only %d fields, skipping.\n", vecp
);
477 for (i
= 0; vec
[i
]; i
++)
478 debug_printf ("vec[%d]: \"%s\"\n", i
, trim(vec
[i
]));
487 /* find out how to perform the action */
492 * If previous condition failed, don't
493 * do this - else fall through
501 * If already delivered, skip this action. Else
502 * consider delivered if action is successful.
511 * Take action, and consider delivered if
512 * action is successful.
521 * Take action, but don't consider delivered, even
522 * if action is successful
529 if (!strcasecmp (vec
[5], "select")) {
530 if (logged_in () != -1)
532 if (vecp
> 7 && timely (vec
[6], vec
[7]) == -1)
537 /* check if the field matches */
545 * "default" matches only if the message hasn't
546 * been delivered yet.
548 if (!strcasecmp (field
, "default")) {
556 /* parse message and build lookup table */
557 if (!parsed
&& parse (fd
) == -1) {
562 * find header field in lookup table, and
563 * see if the pattern matches.
565 if ((p
= lookup (hdrs
, field
)) && (p
->p_value
!= NULL
)
566 && matches (p
->p_value
, pattern
)) {
575 /* find out the action to perform */
578 /* deliver to quoted pipe */
579 if (strcasecmp (action
, "qpipe"))
583 expand (tmpbuf
, string
, fd
);
584 if (split (tmpbuf
, vec
) < 1)
586 status
= usr_pipe (fd
, tmpbuf
, vec
[0], vec
, 0);
590 /* deliver to pipe */
591 if (strcasecmp (action
, "pipe"))
597 expand (tmpbuf
, string
, fd
);
600 status
= usr_pipe (fd
, tmpbuf
, "/bin/sh", vec
+ 2, 0);
605 if (!strcasecmp (action
, "file")) {
606 status
= usr_file (fd
, string
, MBOX_FORMAT
);
609 /* deliver to nmh folder */
610 else if (strcasecmp (action
, "folder"))
614 status
= usr_folder (fd
, string
);
619 if (!strcasecmp (action
, "mmdf")) {
620 status
= usr_file (fd
, string
, MMDF_FORMAT
);
624 else if (strcasecmp (action
, "mbox"))
630 status
= usr_file (fd
, string
, MBOX_FORMAT
);
635 if (strcasecmp (action
, "destroy"))
641 if (status
) next
= false; /* action failed, mark for 'N' result */
643 if (accept
&& status
== 0)
655 * Split buffer into fields (delimited by whitespace or
656 * comma's). Return the number of fields found.
660 split (char *cp
, char **vec
)
667 /* split into a maximum of NVEC fields */
668 for (i
= 0; i
<= NVEC
;) {
671 /* zap any whitespace and comma's */
672 while (isspace ((unsigned char) *s
) || *s
== ',')
675 /* end of buffer, time to leave */
679 /* get double quote text as a single field */
681 for (vec
[i
++] = ++s
; *s
&& *s
!= '"'; s
++) {
683 * Check for escaped double quote. We need
684 * to shift the string to remove slash.
692 if (*s
== '"') /* zap trailing double quote */
697 if (*s
== QUOTE
&& *++s
!= '"')
701 /* move forward to next field delimiter */
702 while (*s
&& !isspace ((unsigned char) *s
) && *s
!= ',')
712 * Parse the headers of a message, and build the
713 * lookup table for matching fields and patterns.
722 char name
[NAMESZ
], field
[NMH_BUFSIZ
];
725 m_getfld_state_t gstate
;
730 /* get a new FILE pointer to message */
731 if ((fd1
= dup (fd
)) == -1)
733 if ((in
= fdopen (fd1
, "r")) == NULL
) {
739 /* add special entries to lookup table */
740 if ((p
= lookup (hdrs
, "source")))
741 p
->p_value
= getcpy (sender
);
742 if ((p
= lookup (hdrs
, "addr")))
743 p
->p_value
= getcpy (addr
);
746 * Scan the headers of the message and build
749 gstate
= m_getfld_state_init(in
);
751 int fieldsz
= sizeof field
;
752 switch (state
= m_getfld2(&gstate
, name
, field
, &fieldsz
)) {
755 lp
= mh_xstrdup(field
);
756 while (state
== FLDPLUS
) {
757 fieldsz
= sizeof field
;
758 state
= m_getfld2(&gstate
, name
, field
, &fieldsz
);
759 lp
= add (field
, lp
);
761 for (p
= hdrs
; p
->p_name
; p
++) {
762 if (!strcasecmp (p
->p_name
, name
)) {
763 if (!(p
->p_flags
& P_HID
)) {
764 if ((cp
= p
->p_value
)) {
765 if (p
->p_flags
& P_ADR
) {
766 dp
= cp
+ strlen (cp
) - 1;
769 cp
= add (",\n\t", cp
);
774 p
->p_value
= add (lp
, cp
);
780 if (p
->p_name
== NULL
&& i
< NVEC
) {
781 p
->p_name
= mh_xstrdup(name
);
795 inform("format error in message");
799 inform("internal error in m_getfld2");
805 m_getfld_state_destroy (&gstate
);
808 if ((p
= lookup (vars
, "reply-to"))) {
809 if ((q
= lookup (hdrs
, "reply-to")) == NULL
|| q
->p_value
== NULL
)
810 q
= lookup (hdrs
, "from");
811 p
->p_value
= getcpy (q
? q
->p_value
: "");
812 p
->p_flags
&= ~P_CHK
;
814 debug_printf ("vars[%td]: name=\"%s\" value=\"%s\"\n",
815 p
- vars
, p
->p_name
, trim(p
->p_value
));
818 for (p
= hdrs
; p
->p_name
; p
++)
819 debug_printf ("hdrs[%td]: name=\"%s\" value=\"%s\"\n",
820 p
- hdrs
, p
->p_name
, p
->p_value
? trim(p
->p_value
) : "");
831 * Expand any builtin variables such as $(sender),
832 * $(address), etc., in a string.
836 expand (char *s1
, char *s2
, int fd
)
844 while ((c
= *s2
++)) {
845 if (c
!= '$' || *s2
!= LPAREN
) {
848 for (cp
= ++s2
; *s2
&& *s2
!= RPAREN
; s2
++)
855 if ((p
= lookup (vars
, cp
))) {
856 if (!parsed
&& (p
->p_flags
& P_CHK
))
859 strcpy (s1
, p
->p_value
);
869 * Fill in the information missing from the "vars"
870 * table, which is necessary to expand any builtin
871 * variables in the string for a "pipe" or "qpipe"
885 if ((p
= lookup (vars
, "sender")))
886 p
->p_value
= getcpy (sender
);
887 if ((p
= lookup (vars
, "address")))
888 p
->p_value
= getcpy (addr
);
889 if ((p
= lookup (vars
, "size"))) {
890 snprintf (buffer
, sizeof(buffer
), "%d",
891 fstat (fd
, &st
) != -1 ? (int) st
.st_size
: 0);
892 p
->p_value
= mh_xstrdup(buffer
);
894 if ((p
= lookup (vars
, "info")))
895 p
->p_value
= getcpy (info
);
898 for (p
= vars
; p
->p_name
; p
++)
899 debug_printf ("vars[%td]: name=\"%s\" value=\"%s\"\n",
900 p
- vars
, p
->p_name
, trim(p
->p_value
));
906 * Find a matching name in a lookup table. If found,
907 * return the "pairs" entry, else return NULL.
911 lookup (struct pair
*pairs
, char *key
)
913 for (; pairs
->p_name
; pairs
++)
914 if (!strcasecmp (pairs
->p_name
, key
))
922 * Check utmp(x) file to see if user is currently
937 while ((utp
= getutxent()) != NULL
) {
938 if ( utp
->ut_type
== USER_PROCESS
&& utp
->ut_user
[0] != 0
939 && strncmp (user
, utp
->ut_user
, sizeof(utp
->ut_user
)) == 0) {
943 return utmped
= DONE
;
948 #endif /* HAVE_GETUTXENT */
949 return utmped
= NOTOK
;
952 #define check(t,a,b) if (t < a || t > b) return -1
953 #define cmpar(h1,m1,h2,m2) if (h1 < h2 || (h1 == h2 && m1 < m2)) return 0
956 timely (char *t1
, char *t2
)
958 int t1hours
, t1mins
, t2hours
, t2mins
;
960 if (sscanf (t1
, "%d:%d", &t1hours
, &t1mins
) != 2)
962 check (t1hours
, 0, 23);
963 check (t1mins
, 0, 59);
965 if (sscanf (t2
, "%d:%d", &t2hours
, &t2mins
) != 2)
967 check (t2hours
, 0, 23);
968 check (t2mins
, 0, 59);
970 cmpar (now
->tw_hour
, now
->tw_min
, t1hours
, t1mins
);
971 cmpar (t2hours
, t2mins
, now
->tw_hour
, now
->tw_min
);
978 * Deliver message by appending to a file.
982 usr_file (int fd
, char *mailbox
, int mbx_style
)
987 verbose_printf("delivering to file \"%s\" (%s style)", mailbox
,
988 mbx_style
== MBOX_FORMAT
? "mbox" : "mmdf");
991 /* open and lock the file */
992 if ((md
= mbx_open (mailbox
, mbx_style
, pw
->pw_uid
, pw
->pw_gid
, m_gmprot())) == -1) {
994 adorn ("", "unable to open:");
998 lseek(fd
, 0, SEEK_SET
);
1000 /* append message to file */
1001 if (mbx_copy (mailbox
, mbx_style
, md
, fd
, NULL
) == -1) {
1003 adorn ("", "error writing to:");
1007 /* close and unlock file */
1008 if (mbx_close (mailbox
, md
) == NOTOK
) {
1010 adorn ("", "error closing:");
1015 verbose_printf (", success.\n");
1021 * Deliver message to a nmh folder.
1025 usr_folder (int fd
, char *string
)
1028 char folder
[BUFSIZ
], *vec
[3];
1030 /* get folder name ready */
1032 strncpy(folder
, string
, sizeof(folder
));
1034 snprintf(folder
, sizeof(folder
), "+%s", string
);
1037 verbose_printf ("delivering to folder \"%s\"", folder
+ 1);
1039 vec
[0] = "rcvstore";
1043 /* use rcvstore to put message in folder */
1044 status
= usr_pipe (fd
, "rcvstore", rcvstoreproc
, vec
, 1);
1050 * Deliver message to a process.
1054 usr_pipe (int fd_arg
, char *cmd
, char *pgm
, char **vec
, int suppress
)
1056 volatile int fd
= fd_arg
;
1058 int bytes
, seconds
, status
;
1061 if (verbose
&& !suppress
)
1062 verbose_printf ("delivering to pipe \"%s\"", cmd
);
1064 lseek(fd
, 0, SEEK_SET
);
1071 adorn ("fork", "unable to");
1078 if (freopen ("/dev/null", "w", stdout
) == NULL
) {
1079 advise ("stdout", "freopen");
1081 if (freopen ("/dev/null", "w", stderr
) == NULL
) {
1082 advise ("stderr", "freopen");
1089 if ((fd
= open ("/dev/tty", O_RDWR
)) != -1) {
1090 ioctl (fd
, TIOCNOTTY
, NULL
);
1093 #endif /* TIOCNOTTY */
1095 setpgid(0, getpid()); /* put in own process group */
1098 setenv("USER", pw
->pw_name
, 1);
1099 setenv("HOME", pw
->pw_dir
, 1);
1100 setenv("SHELL", pw
->pw_shell
, 1);
1106 /* parent process */
1107 if (! setjmp (myctx
)) {
1108 SIGNAL (SIGALRM
, alrmser
);
1109 bytes
= fstat (fd
, &st
) != -1 ? (int) st
.st_size
: 100;
1111 /* amount of time to wait depends on message size */
1113 /* give at least 5 minutes */
1115 } else if (bytes
>= 90000) {
1116 /* a half hour is long enough */
1119 seconds
= (bytes
/ 60) + 300;
1121 alarm ((unsigned int) seconds
);
1122 status
= pidwait (child_id
, 0);
1127 verbose_printf (", success.\n");
1128 else if ((status
& 0xff00) == 0xff00)
1129 verbose_printf (", system error\n");
1131 pidstatus (status
, stdout
, ", failed");
1133 return status
== 0 ? 0 : -1;
1136 * Ruthlessly kill the child and anything
1137 * else in its process group.
1139 killpg(child_id
, SIGKILL
);
1141 verbose_printf (", timed-out; terminated\n");
1152 longjmp (myctx
, DONE
);
1157 * Get the `sender' from the envelope
1158 * information ("From " line).
1162 get_sender (char *envelope
, char **sender
)
1166 char buffer
[BUFSIZ
];
1168 if (envelope
== NULL
) {
1169 *sender
= mh_xstrdup("");
1174 strncpy (buffer
, envelope
+ i
, sizeof(buffer
));
1175 if ((cp
= strchr(buffer
, '\n'))) {
1185 for (cp
= buffer
+ strlen (buffer
) - 1; cp
>= buffer
; cp
--)
1186 if (isspace ((unsigned char) *cp
))
1190 *sender
= mh_xstrdup(buffer
);
1195 * Copy message into a temporary file.
1196 * While copying, it will do some header processing
1197 * including the extraction of the envelope information.
1201 copy_message (int qd
, char *tmpfil
, int fold
)
1203 int i
, first
= 1, fd1
, fd2
;
1204 char buffer
[BUFSIZ
];
1208 tfile
= m_mktemp2(NULL
, invo_name
, &fd1
, NULL
);
1209 if (tfile
== NULL
) return -1;
1210 strncpy (tmpfil
, tfile
, BUFSIZ
);
1213 while ((i
= read (qd
, buffer
, sizeof(buffer
))) > 0)
1214 if (write (fd1
, buffer
, i
) != i
) {
1217 (void) m_unlink (tmpfil
);
1222 lseek(fd1
, 0, SEEK_SET
);
1226 /* dup the fd for incoming message */
1227 if ((fd2
= dup (qd
)) == -1) {
1232 /* now create a FILE pointer for it */
1233 if ((qfp
= fdopen (fd2
, "r")) == NULL
) {
1239 /* dup the fd for temporary file */
1240 if ((fd2
= dup (fd1
)) == -1) {
1246 /* now create a FILE pointer for it */
1247 if ((ffp
= fdopen (fd2
, "r+")) == NULL
) {
1255 * copy message into temporary file
1256 * and massage the headers. Save
1257 * a copy of the "From " line for later.
1259 while (fgets (buffer
, sizeof(buffer
), qfp
)) {
1262 if (has_prefix(buffer
, "From ")) {
1263 /* get copy of envelope information ("From " line) */
1264 envelope
= mh_xstrdup(buffer
);
1266 /* Put the delivery date in message */
1275 fputs (buffer
, ffp
);
1287 lseek(fd1
, 0, SEEK_SET
);
1299 * Trim strings for pretty printing of debugging output
1305 static char buffer
[BUFSIZ
* 4];
1311 /* copy string into temp buffer */
1312 strncpy (buffer
, cp
, sizeof(buffer
));
1315 /* skip over leading whitespace */
1316 while (isspace((unsigned char) *bp
))
1319 /* start at the end and zap trailing whitespace */
1320 for (sp
= bp
+ strlen(bp
) - 1; sp
>= bp
; sp
--) {
1321 if (isspace((unsigned char) *sp
))
1327 /* replace remaining whitespace with spaces */
1328 for (sp
= bp
; *sp
; sp
++)
1329 if (isspace((unsigned char) *sp
))
1336 * Function for printing `verbose' messages.
1340 verbose_printf (char *fmt
, ...)
1348 fflush (stdout
); /* now flush output */
1353 * Function for printing `verbose' delivery
1358 adorn (char *what
, char *fmt
, ...)
1364 eindex
= errno
; /* save the errno */
1365 fputs(", ", stdout
);
1373 printf(" %s: ", what
);
1374 if ((s
= strerror (eindex
)))
1377 printf("Error %d", eindex
);
1386 * Function for printing `debug' messages.
1390 debug_printf (char *fmt
, ...)
1395 vfprintf (stderr
, fmt
, ap
);
1401 * Check ndbm/db file(s) to see if the Message-Id of this
1402 * message matches the Message-Id of a previous message,
1403 * so we can discard it. If it doesn't match, we add the
1404 * Message-Id of this message to the ndbm/db file.
1407 suppress_duplicates (int fd
, char *file
)
1409 int fd1
, lockfd
, state
, result
;
1410 char *cp
, buf
[NMH_BUFSIZ
], name
[NAMESZ
];
1414 m_getfld_state_t gstate
;
1416 if ((fd1
= dup (fd
)) == -1)
1418 if (!(in
= fdopen (fd1
, "r"))) {
1424 gstate
= m_getfld_state_init(in
);
1426 int failed_to_lock
= 0;
1427 int bufsz
= sizeof buf
;
1428 state
= m_getfld2(&gstate
, name
, buf
, &bufsz
);
1432 /* Search for the message ID */
1433 if (strcasecmp (name
, "Message-ID")) {
1434 while (state
== FLDPLUS
) {
1436 state
= m_getfld2(&gstate
, name
, buf
, &bufsz
);
1441 cp
= mh_xstrdup(buf
);
1442 while (state
== FLDPLUS
) {
1444 state
= m_getfld2(&gstate
, name
, buf
, &bufsz
);
1447 key
.dptr
= trimcpy (cp
);
1448 key
.dsize
= strlen (key
.dptr
) + 1;
1452 if (!(db
= dbm_open (file
, O_RDWR
| O_CREAT
, 0600))) {
1453 advise (file
, "unable to perform dbm_open on");
1459 * Since it is difficult to portable lock a ndbm file,
1460 * we will open and lock the Maildelivery file instead.
1461 * This will fail if your Maildelivery file doesn't
1464 if ((lockfd
= lkopendata(file
, O_RDWR
, 0, &failed_to_lock
))
1466 advise (file
, "unable to perform file locking on");
1471 value
= dbm_fetch (db
, key
);
1474 verbose_printf ("Message-ID: %s\n already received on %s",
1478 value
.dptr
= ddate
+ sizeof("Delivery-Date:");
1479 value
.dsize
= strlen(value
.dptr
) + 1;
1480 if (dbm_store (db
, key
, value
, DBM_INSERT
))
1481 advise (file
, "possibly corrupt file");
1486 lkclosedata(lockfd
, file
);
1503 m_getfld_state_destroy (&gstate
);