]>
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 <h/dropsbr.h>
24 #include <h/rcvmail.h>
25 #include <h/signals.h>
31 #include "sbr/lock_file.h"
32 #include "sbr/m_mktemp.h"
35 #include <sys/ioctl.h>
38 /* Hopefully, grp.h declares initgroups(). If we run into a platform
39 where it doesn't, we could consider declaring it here as well. */
42 /* This define is needed for Berkeley db v2 and above to
43 * make the header file expose the 'historical' ndbm APIs.
44 * We define it unconditionally because this is simple and
47 #define DB_DBM_HSEARCH 1
49 #endif /* Use DB_DBM_HSEARCH to prevent warning from gcc -Wunused-macros. */
56 #endif /* HAVE_GETUTXENT */
58 #define SLOCAL_SWITCHES \
59 X("addr address", 0, ADDRSW) \
60 X("user name", 0, USERSW) \
61 X("file file", 0, FILESW) \
62 X("sender address", 0, SENDERSW) \
63 X("mailbox file", 0, MAILBOXSW) \
64 X("home directory", -4, HOMESW) \
65 X("info data", 0, INFOSW) \
66 X("maildelivery file", 0, MAILSW) \
67 X("verbose", 0, VERBSW) \
68 X("noverbose", 0, NVERBSW) \
69 X("suppressdup", 0, SUPPRESSDUP) \
70 X("nosuppressdup", 0, NSUPPRESSDUP) \
71 X("debug", 0, DEBUGSW) \
72 X("version", 0, VERSIONSW) \
73 X("help", 0, HELPSW) \
75 #define X(sw, minchars, id) id,
76 DEFINE_SWITCH_ENUM(SLOCAL
);
79 #define X(sw, minchars, id) { sw, minchars, id },
80 DEFINE_SWITCH_ARRAY(SLOCAL
, switches
);
83 static int globbed
= 0; /* have we built "vars" table yet? */
84 static int parsed
= 0; /* have we built header field table yet */
85 static int utmped
= 0; /* have we scanned umtp(x) file yet */
86 static bool suppressdup
; /* are we suppressing duplicate messages? */
91 static char *addr
= NULL
;
92 static char *user
= NULL
;
93 static char *info
= NULL
;
94 static char *file
= NULL
;
95 static char *sender
= NULL
;
96 static char *envelope
= NULL
; /* envelope information ("From " line) */
97 static char *mbox
= NULL
;
98 static char *home
= NULL
;
100 static struct passwd
*pw
; /* passwd file entry */
102 static char ddate
[BUFSIZ
]; /* record the delivery date */
105 static jmp_buf myctx
;
107 /* flags for pair->p_flags */
109 #define P_ADR 0x01 /* field is address */
110 #define P_HID 0x02 /* special (fake) field */
122 * Lookup table for matching fields and patterns
123 * in messages. The rest of the table is added
124 * when the message is parsed.
126 static struct pair hdrs
[NVEC
+ 1] = {
127 { "source", NULL
, P_HID
},
128 { "addr", NULL
, P_HID
},
129 { "Return-Path", NULL
, P_ADR
},
130 { "Reply-To", NULL
, P_ADR
},
131 { "From", NULL
, P_ADR
},
132 { "Sender", NULL
, P_ADR
},
133 { "To", NULL
, P_ADR
},
134 { "cc", NULL
, P_ADR
},
135 { "Resent-Reply-To", NULL
, P_ADR
},
136 { "Resent-From", NULL
, P_ADR
},
137 { "Resent-Sender", NULL
, P_ADR
},
138 { "Resent-To", NULL
, P_ADR
},
139 { "Resent-cc", NULL
, P_ADR
},
144 * The list of builtin variables to expand in a string
145 * before it is executed by the "pipe" or "qpipe" action.
147 static struct pair vars
[] = {
148 { "sender", NULL
, P_NIL
},
149 { "address", NULL
, P_NIL
},
150 { "size", NULL
, P_NIL
},
151 { "reply-to", NULL
, P_CHK
},
152 { "info", NULL
, P_NIL
},
156 extern char **environ
;
161 static int localmail (int, char *);
162 static int usr_delivery (int, char *, int);
163 static int split (char *, char **);
164 static int parse (int);
165 static void expand (char *, char *, int);
166 static void glob (int);
167 static struct pair
*lookup (struct pair
*, char *) PURE
;
168 static int logged_in (void);
169 static int timely (char *, char *);
170 static int usr_file (int, char *, int);
171 static int usr_pipe (int, char *, char *, char **, int);
172 static int usr_folder (int, char *);
173 static void alrmser (int);
174 static void get_sender (char *, char **);
175 static int copy_message (int, char *, int);
176 static void verbose_printf (char *fmt
, ...) CHECK_PRINTF(1, 2);
177 static void adorn (char *, char *, ...) CHECK_PRINTF(2, 3);
178 static void debug_printf (char *fmt
, ...) CHECK_PRINTF(1, 2);
179 static int suppress_duplicates (int, char *);
180 static char *trim (char *);
184 main (int argc
, char **argv
)
188 char *cp
, *mdlvr
= NULL
, buf
[BUFSIZ
];
189 char mailbox
[BUFSIZ
], tmpfil
[BUFSIZ
];
190 char **argp
, **arguments
;
192 if (nmh_init(argv
[0], false, false)) { return 1; }
195 arguments
= getarguments (invo_name
, argc
, argv
, 0);
198 /* Parse arguments */
199 while ((cp
= *argp
++)) {
201 switch (smatch (++cp
, switches
)) {
203 ambigsw (cp
, switches
);
206 die("-%s unknown", cp
);
209 snprintf (buf
, sizeof(buf
), "%s [switches]", invo_name
);
210 print_help (buf
, switches
, 0);
213 print_version(invo_name
);
217 if (!(addr
= *argp
++))/* allow -xyz arguments */
218 die("missing argument to %s", argp
[-2]);
221 if (!(info
= *argp
++))/* allow -xyz arguments */
222 die("missing argument to %s", argp
[-2]);
225 if (!(user
= *argp
++))/* allow -xyz arguments */
226 die("missing argument to %s", argp
[-2]);
229 if (!(file
= *argp
++) || *file
== '-')
230 die("missing argument to %s", argp
[-2]);
233 if (!(sender
= *argp
++))/* allow -xyz arguments */
234 die("missing argument to %s", argp
[-2]);
237 if (!(mbox
= *argp
++) || *mbox
== '-')
238 die("missing argument to %s", argp
[-2]);
241 if (!(home
= *argp
++) || *home
== '-')
242 die("missing argument to %s", argp
[-2]);
246 if (!(cp
= *argp
++) || *cp
== '-')
247 die("missing argument to %s", argp
[-2]);
249 die("only one maildelivery file at a time!");
271 die("only switch arguments are supported");
276 addr
= getusername ();
278 user
= getusername ();
280 if ((pw
= getpwnam (user
)) == NULL
)
281 die("no such local user as %s", user
);
283 if (chdir (pw
->pw_dir
) == -1)
284 if (chdir ("/") < 0) {
285 advise ("/", "chdir");
289 if (geteuid() == 0) {
290 if (setgid (pw
->pw_gid
) != 0) {
291 adios ("setgid", "unable to set group to %ld", (long) pw
->pw_gid
);
293 initgroups (pw
->pw_name
, pw
->pw_gid
);
294 if (setuid (pw
->pw_uid
) != 0) {
295 adios ("setuid", "unable to set user to %ld", (long) pw
->pw_uid
);
302 setbuf (stdin
, NULL
);
304 /* Record the delivery time */
305 if ((now
= dlocaltimenow ()) == NULL
)
306 die("unable to ascertain local time");
307 snprintf (ddate
, sizeof(ddate
), "Delivery-Date: %s\n", dtimenow (0));
310 * Copy the message to a temporary file
315 /* getting message from file */
316 if ((tempfd
= open (file
, O_RDONLY
)) == -1)
317 adios (file
, "unable to open");
319 debug_printf ("retrieving message from file \"%s\"\n", file
);
320 if ((fd
= copy_message (tempfd
, tmpfil
, 1)) == -1)
321 die("unable to create temporary file in %s",
325 /* getting message from stdin */
327 debug_printf ("retrieving message from stdin\n");
328 if ((fd
= copy_message (fileno (stdin
), tmpfil
, 1)) == -1)
329 die("unable to create temporary file in %s",
334 debug_printf ("temporary file=\"%s\"\n", tmpfil
);
336 /* Delete the temp file now or a copy of every single message passed through
337 slocal will be left in the /tmp directory until deleted manually! This
338 unlink() used to be under an 'else' of the 'if (debug)' above, but since
339 some people like to always run slocal with -debug and log the results,
340 the /tmp directory would get choked over time. Of course, now that we
341 always delete the temp file, the "temporary file=" message above is
342 somewhat pointless -- someone watching debug output wouldn't have a
343 chance to 'tail -f' or 'ln' the temp file before it's unlinked. The best
344 thing would be to delay this unlink() until later if debug == 1, but I'll
345 leave that for someone who cares about the temp-file-accessing
346 functionality (they'll have to watch out for cases where we adios()). */
347 (void) m_unlink (tmpfil
);
349 if (!(fp
= fdopen (fd
, "r+")))
350 die("unable to access temporary file");
353 * If no sender given, extract it
354 * from envelope information. */
356 get_sender (envelope
, &sender
);
359 snprintf (mailbox
, sizeof(mailbox
), "%s/%s",
360 mmdfldir
[0] ? mmdfldir
: pw
->pw_dir
,
361 mmdflfil
[0] ? mmdflfil
: pw
->pw_name
);
368 debug_printf ("addr=\"%s\"\n", trim(addr
));
369 debug_printf ("user=\"%s\"\n", trim(user
));
370 debug_printf ("info=\"%s\"\n", trim(info
));
371 debug_printf ("sender=\"%s\"\n", trim(sender
));
372 debug_printf ("envelope=\"%s\"\n", envelope
? trim(envelope
) : "");
373 debug_printf ("mbox=\"%s\"\n", trim(mbox
));
374 debug_printf ("home=\"%s\"\n", trim(home
));
375 debug_printf ("ddate=\"%s\"\n", trim(ddate
));
376 debug_printf ("now=%02d:%02d\n\n", now
->tw_hour
, now
->tw_min
);
379 /* deliver the message */
380 status
= localmail (fd
, mdlvr
);
382 done (status
!= -1 ? RCV_MOK
: RCV_MBX
);
388 * Main routine for delivering message.
392 localmail (int fd
, char *mdlvr
)
394 /* check if this message is a duplicate */
396 suppress_duplicates(fd
, mdlvr
? mdlvr
: ".maildelivery") == DONE
)
399 /* delivery according to personal Maildelivery file */
400 if (usr_delivery (fd
, mdlvr
? mdlvr
: ".maildelivery", 0) != -1)
403 /* delivery according to global Maildelivery file */
404 if (usr_delivery (fd
, maildelivery
, 1) != -1)
408 verbose_printf ("(delivering to standard mail spool)\n");
410 /* last resort - deliver to standard mail spool */
411 return usr_file (fd
, mbox
, MBOX_FORMAT
);
415 #define matches(a,b) (stringdex (b, a) >= 0)
418 * Parse the delivery file, and process incoming message.
422 usr_delivery (int fd
, char *delivery
, int su
)
430 char *field
, *pattern
, *action
, *result
, *string
;
431 char buffer
[BUFSIZ
], tmpbuf
[BUFSIZ
];
437 /* open the delivery file */
438 if ((fp
= fopen (delivery
, "r")) == NULL
)
441 /* check if delivery file has bad ownership or permissions */
442 if (fstat (fileno (fp
), &st
) == -1
443 || (st
.st_uid
!= 0 && (su
|| st
.st_uid
!= pw
->pw_uid
))
444 || st
.st_mode
& (S_IWGRP
|S_IWOTH
)) {
446 verbose_printf ("WARNING: %s has bad ownership/modes (su=%d,uid=%d,owner=%d,mode=0%o)\n",
447 delivery
, su
, (int) pw
->pw_uid
, (int) st
.st_uid
, (int) st
.st_mode
);
455 /* read and process delivery file */
456 while (fgets (buffer
, sizeof(buffer
), fp
)) {
457 /* skip comments and empty lines */
458 if (*buffer
== '#' || *buffer
== '\n')
461 trim_suffix_c(buffer
, '\n');
463 /* split buffer into fields */
464 vecp
= split (buffer
, vec
);
466 /* check for too few fields */
469 debug_printf ("WARNING: entry with only %d fields, skipping.\n", vecp
);
474 for (i
= 0; vec
[i
]; i
++)
475 debug_printf ("vec[%d]: \"%s\"\n", i
, trim(vec
[i
]));
484 /* find out how to perform the action */
489 * If previous condition failed, don't
490 * do this - else fall through
498 * If already delivered, skip this action. Else
499 * consider delivered if action is successful.
508 * Take action, and consider delivered if
509 * action is successful.
518 * Take action, but don't consider delivered, even
519 * if action is successful
526 if (!strcasecmp (vec
[5], "select")) {
527 if (logged_in () != -1)
529 if (vecp
> 7 && timely (vec
[6], vec
[7]) == -1)
534 /* check if the field matches */
542 * "default" matches only if the message hasn't
543 * been delivered yet.
545 if (!strcasecmp (field
, "default")) {
553 /* parse message and build lookup table */
554 if (!parsed
&& parse (fd
) == -1) {
559 * find header field in lookup table, and
560 * see if the pattern matches.
562 if ((p
= lookup (hdrs
, field
)) && (p
->p_value
!= NULL
)
563 && matches (p
->p_value
, pattern
)) {
572 /* find out the action to perform */
575 /* deliver to quoted pipe */
576 if (strcasecmp (action
, "qpipe"))
580 expand (tmpbuf
, string
, fd
);
581 if (split (tmpbuf
, vec
) < 1)
583 status
= usr_pipe (fd
, tmpbuf
, vec
[0], vec
, 0);
587 /* deliver to pipe */
588 if (strcasecmp (action
, "pipe"))
594 expand (tmpbuf
, string
, fd
);
597 status
= usr_pipe (fd
, tmpbuf
, "/bin/sh", vec
+ 2, 0);
602 if (!strcasecmp (action
, "file")) {
603 status
= usr_file (fd
, string
, MBOX_FORMAT
);
606 /* deliver to nmh folder */
607 else if (strcasecmp (action
, "folder"))
611 status
= usr_folder (fd
, string
);
616 if (!strcasecmp (action
, "mmdf")) {
617 status
= usr_file (fd
, string
, MMDF_FORMAT
);
621 else if (strcasecmp (action
, "mbox"))
627 status
= usr_file (fd
, string
, MBOX_FORMAT
);
632 if (strcasecmp (action
, "destroy"))
638 if (status
) next
= false; /* action failed, mark for 'N' result */
640 if (accept
&& status
== 0)
652 * Split buffer into fields (delimited by whitespace or
653 * comma's). Return the number of fields found.
657 split (char *cp
, char **vec
)
664 /* split into a maximum of NVEC fields */
665 for (i
= 0; i
<= NVEC
;) {
668 /* zap any whitespace and comma's */
669 while (isspace ((unsigned char) *s
) || *s
== ',')
672 /* end of buffer, time to leave */
676 /* get double quote text as a single field */
678 for (vec
[i
++] = ++s
; *s
&& *s
!= '"'; s
++) {
680 * Check for escaped double quote. We need
681 * to shift the string to remove slash.
689 if (*s
== '"') /* zap trailing double quote */
694 if (*s
== QUOTE
&& *++s
!= '"')
698 /* move forward to next field delimiter */
699 while (*s
&& !isspace ((unsigned char) *s
) && *s
!= ',')
709 * Parse the headers of a message, and build the
710 * lookup table for matching fields and patterns.
719 char name
[NAMESZ
], field
[NMH_BUFSIZ
];
722 m_getfld_state_t gstate
;
727 /* get a new FILE pointer to message */
728 if ((fd1
= dup (fd
)) == -1)
730 if ((in
= fdopen (fd1
, "r")) == NULL
) {
736 /* add special entries to lookup table */
737 if ((p
= lookup (hdrs
, "source")))
738 p
->p_value
= getcpy (sender
);
739 if ((p
= lookup (hdrs
, "addr")))
740 p
->p_value
= getcpy (addr
);
743 * Scan the headers of the message and build
746 gstate
= m_getfld_state_init(in
);
748 int fieldsz
= sizeof field
;
749 switch (state
= m_getfld2(&gstate
, name
, field
, &fieldsz
)) {
752 lp
= mh_xstrdup(field
);
753 while (state
== FLDPLUS
) {
754 fieldsz
= sizeof field
;
755 state
= m_getfld2(&gstate
, name
, field
, &fieldsz
);
756 lp
= add (field
, lp
);
758 for (p
= hdrs
; p
->p_name
; p
++) {
759 if (!strcasecmp (p
->p_name
, name
)) {
760 if (!(p
->p_flags
& P_HID
)) {
761 if ((cp
= p
->p_value
)) {
762 if (p
->p_flags
& P_ADR
) {
763 dp
= cp
+ strlen (cp
) - 1;
766 cp
= add (",\n\t", cp
);
771 p
->p_value
= add (lp
, cp
);
777 if (p
->p_name
== NULL
&& i
< NVEC
) {
778 p
->p_name
= mh_xstrdup(name
);
792 inform("format error in message");
796 inform("internal error in m_getfld2");
802 m_getfld_state_destroy (&gstate
);
805 if ((p
= lookup (vars
, "reply-to"))) {
806 if ((q
= lookup (hdrs
, "reply-to")) == NULL
|| q
->p_value
== NULL
)
807 q
= lookup (hdrs
, "from");
808 p
->p_value
= getcpy (q
? q
->p_value
: "");
809 p
->p_flags
&= ~P_CHK
;
811 debug_printf ("vars[%td]: name=\"%s\" value=\"%s\"\n",
812 p
- vars
, p
->p_name
, trim(p
->p_value
));
815 for (p
= hdrs
; p
->p_name
; p
++)
816 debug_printf ("hdrs[%td]: name=\"%s\" value=\"%s\"\n",
817 p
- hdrs
, p
->p_name
, p
->p_value
? trim(p
->p_value
) : "");
828 * Expand any builtin variables such as $(sender),
829 * $(address), etc., in a string.
833 expand (char *s1
, char *s2
, int fd
)
841 while ((c
= *s2
++)) {
842 if (c
!= '$' || *s2
!= LPAREN
) {
845 for (cp
= ++s2
; *s2
&& *s2
!= RPAREN
; s2
++)
852 if ((p
= lookup (vars
, cp
))) {
853 if (!parsed
&& (p
->p_flags
& P_CHK
))
856 strcpy (s1
, p
->p_value
);
866 * Fill in the information missing from the "vars"
867 * table, which is necessary to expand any builtin
868 * variables in the string for a "pipe" or "qpipe"
882 if ((p
= lookup (vars
, "sender")))
883 p
->p_value
= getcpy (sender
);
884 if ((p
= lookup (vars
, "address")))
885 p
->p_value
= getcpy (addr
);
886 if ((p
= lookup (vars
, "size"))) {
887 snprintf (buffer
, sizeof(buffer
), "%d",
888 fstat (fd
, &st
) != -1 ? (int) st
.st_size
: 0);
889 p
->p_value
= mh_xstrdup(buffer
);
891 if ((p
= lookup (vars
, "info")))
892 p
->p_value
= getcpy (info
);
895 for (p
= vars
; p
->p_name
; p
++)
896 debug_printf ("vars[%td]: name=\"%s\" value=\"%s\"\n",
897 p
- vars
, p
->p_name
, trim(p
->p_value
));
903 * Find a matching name in a lookup table. If found,
904 * return the "pairs" entry, else return NULL.
908 lookup (struct pair
*pairs
, char *key
)
910 for (; pairs
->p_name
; pairs
++)
911 if (!strcasecmp (pairs
->p_name
, key
))
919 * Check utmp(x) file to see if user is currently
934 while ((utp
= getutxent()) != NULL
) {
935 if ( utp
->ut_type
== USER_PROCESS
&& utp
->ut_user
[0] != 0
936 && strncmp (user
, utp
->ut_user
, sizeof(utp
->ut_user
)) == 0) {
940 return utmped
= DONE
;
945 #endif /* HAVE_GETUTXENT */
946 return utmped
= NOTOK
;
949 #define check(t,a,b) if (t < a || t > b) return -1
950 #define cmpar(h1,m1,h2,m2) if (h1 < h2 || (h1 == h2 && m1 < m2)) return 0
953 timely (char *t1
, char *t2
)
955 int t1hours
, t1mins
, t2hours
, t2mins
;
957 if (sscanf (t1
, "%d:%d", &t1hours
, &t1mins
) != 2)
959 check (t1hours
, 0, 23);
960 check (t1mins
, 0, 59);
962 if (sscanf (t2
, "%d:%d", &t2hours
, &t2mins
) != 2)
964 check (t2hours
, 0, 23);
965 check (t2mins
, 0, 59);
967 cmpar (now
->tw_hour
, now
->tw_min
, t1hours
, t1mins
);
968 cmpar (t2hours
, t2mins
, now
->tw_hour
, now
->tw_min
);
975 * Deliver message by appending to a file.
979 usr_file (int fd
, char *mailbox
, int mbx_style
)
984 verbose_printf("delivering to file \"%s\" (%s style)", mailbox
,
985 mbx_style
== MBOX_FORMAT
? "mbox" : "mmdf");
988 /* open and lock the file */
989 if ((md
= mbx_open (mailbox
, mbx_style
, pw
->pw_uid
, pw
->pw_gid
, m_gmprot())) == -1) {
991 adorn ("", "unable to open:");
995 lseek(fd
, 0, SEEK_SET
);
997 /* append message to file */
998 if (mbx_copy (mailbox
, mbx_style
, md
, fd
, NULL
) == -1) {
1000 adorn ("", "error writing to:");
1004 /* close and unlock file */
1005 if (mbx_close (mailbox
, md
) == NOTOK
) {
1007 adorn ("", "error closing:");
1012 verbose_printf (", success.\n");
1018 * Deliver message to a nmh folder.
1022 usr_folder (int fd
, char *string
)
1025 char folder
[BUFSIZ
], *vec
[3];
1027 /* get folder name ready */
1029 strncpy(folder
, string
, sizeof(folder
));
1031 snprintf(folder
, sizeof(folder
), "+%s", string
);
1034 verbose_printf ("delivering to folder \"%s\"", folder
+ 1);
1036 vec
[0] = "rcvstore";
1040 /* use rcvstore to put message in folder */
1041 status
= usr_pipe (fd
, "rcvstore", rcvstoreproc
, vec
, 1);
1047 * Deliver message to a process.
1051 usr_pipe (int fd_arg
, char *cmd
, char *pgm
, char **vec
, int suppress
)
1053 volatile int fd
= fd_arg
;
1055 int bytes
, seconds
, status
;
1058 if (verbose
&& !suppress
)
1059 verbose_printf ("delivering to pipe \"%s\"", cmd
);
1061 lseek(fd
, 0, SEEK_SET
);
1068 adorn ("fork", "unable to");
1075 if (freopen ("/dev/null", "w", stdout
) == NULL
) {
1076 advise ("stdout", "freopen");
1078 if (freopen ("/dev/null", "w", stderr
) == NULL
) {
1079 advise ("stderr", "freopen");
1086 if ((fd
= open ("/dev/tty", O_RDWR
)) != -1) {
1087 ioctl (fd
, TIOCNOTTY
, NULL
);
1090 #endif /* TIOCNOTTY */
1092 setpgid(0, getpid()); /* put in own process group */
1095 setenv("USER", pw
->pw_name
, 1);
1096 setenv("HOME", pw
->pw_dir
, 1);
1097 setenv("SHELL", pw
->pw_shell
, 1);
1103 /* parent process */
1104 if (! setjmp (myctx
)) {
1105 SIGNAL (SIGALRM
, alrmser
);
1106 bytes
= fstat (fd
, &st
) != -1 ? (int) st
.st_size
: 100;
1108 /* amount of time to wait depends on message size */
1110 /* give at least 5 minutes */
1112 } else if (bytes
>= 90000) {
1113 /* a half hour is long enough */
1116 seconds
= (bytes
/ 60) + 300;
1118 alarm ((unsigned int) seconds
);
1119 status
= pidwait (child_id
, 0);
1124 verbose_printf (", success.\n");
1125 else if ((status
& 0xff00) == 0xff00)
1126 verbose_printf (", system error\n");
1128 pidstatus (status
, stdout
, ", failed");
1130 return status
== 0 ? 0 : -1;
1133 * Ruthlessly kill the child and anything
1134 * else in its process group.
1136 killpg(child_id
, SIGKILL
);
1138 verbose_printf (", timed-out; terminated\n");
1149 longjmp (myctx
, DONE
);
1154 * Get the `sender' from the envelope
1155 * information ("From " line).
1159 get_sender (char *envelope
, char **sender
)
1163 char buffer
[BUFSIZ
];
1165 if (envelope
== NULL
) {
1166 *sender
= mh_xstrdup("");
1171 strncpy (buffer
, envelope
+ i
, sizeof(buffer
));
1172 if ((cp
= strchr(buffer
, '\n'))) {
1182 for (cp
= buffer
+ strlen (buffer
) - 1; cp
>= buffer
; cp
--)
1183 if (isspace ((unsigned char) *cp
))
1187 *sender
= mh_xstrdup(buffer
);
1192 * Copy message into a temporary file.
1193 * While copying, it will do some header processing
1194 * including the extraction of the envelope information.
1198 copy_message (int qd
, char *tmpfil
, int fold
)
1200 int i
, first
= 1, fd1
, fd2
;
1201 char buffer
[BUFSIZ
];
1205 tfile
= m_mktemp2(NULL
, invo_name
, &fd1
, NULL
);
1206 if (tfile
== NULL
) return -1;
1207 strncpy (tmpfil
, tfile
, BUFSIZ
);
1210 while ((i
= read (qd
, buffer
, sizeof(buffer
))) > 0)
1211 if (write (fd1
, buffer
, i
) != i
) {
1214 (void) m_unlink (tmpfil
);
1219 lseek(fd1
, 0, SEEK_SET
);
1223 /* dup the fd for incoming message */
1224 if ((fd2
= dup (qd
)) == -1) {
1229 /* now create a FILE pointer for it */
1230 if ((qfp
= fdopen (fd2
, "r")) == NULL
) {
1236 /* dup the fd for temporary file */
1237 if ((fd2
= dup (fd1
)) == -1) {
1243 /* now create a FILE pointer for it */
1244 if ((ffp
= fdopen (fd2
, "r+")) == NULL
) {
1252 * copy message into temporary file
1253 * and massage the headers. Save
1254 * a copy of the "From " line for later.
1256 while (fgets (buffer
, sizeof(buffer
), qfp
)) {
1259 if (has_prefix(buffer
, "From ")) {
1260 /* get copy of envelope information ("From " line) */
1261 envelope
= mh_xstrdup(buffer
);
1263 /* Put the delivery date in message */
1272 fputs (buffer
, ffp
);
1284 lseek(fd1
, 0, SEEK_SET
);
1296 * Trim strings for pretty printing of debugging output
1302 static char buffer
[BUFSIZ
* 4];
1308 /* copy string into temp buffer */
1309 strncpy (buffer
, cp
, sizeof(buffer
));
1312 /* skip over leading whitespace */
1313 while (isspace((unsigned char) *bp
))
1316 /* start at the end and zap trailing whitespace */
1317 for (sp
= bp
+ strlen(bp
) - 1; sp
>= bp
; sp
--) {
1318 if (isspace((unsigned char) *sp
))
1324 /* replace remaining whitespace with spaces */
1325 for (sp
= bp
; *sp
; sp
++)
1326 if (isspace((unsigned char) *sp
))
1333 * Function for printing `verbose' messages.
1337 verbose_printf (char *fmt
, ...)
1345 fflush (stdout
); /* now flush output */
1350 * Function for printing `verbose' delivery
1355 adorn (char *what
, char *fmt
, ...)
1361 eindex
= errno
; /* save the errno */
1362 fputs(", ", stdout
);
1370 printf(" %s: ", what
);
1371 if ((s
= strerror (eindex
)))
1374 printf("Error %d", eindex
);
1383 * Function for printing `debug' messages.
1387 debug_printf (char *fmt
, ...)
1392 vfprintf (stderr
, fmt
, ap
);
1398 * Check ndbm/db file(s) to see if the Message-Id of this
1399 * message matches the Message-Id of a previous message,
1400 * so we can discard it. If it doesn't match, we add the
1401 * Message-Id of this message to the ndbm/db file.
1404 suppress_duplicates (int fd
, char *file
)
1406 int fd1
, lockfd
, state
, result
;
1407 char *cp
, buf
[NMH_BUFSIZ
], name
[NAMESZ
];
1411 m_getfld_state_t gstate
;
1413 if ((fd1
= dup (fd
)) == -1)
1415 if (!(in
= fdopen (fd1
, "r"))) {
1421 gstate
= m_getfld_state_init(in
);
1423 int failed_to_lock
= 0;
1424 int bufsz
= sizeof buf
;
1425 state
= m_getfld2(&gstate
, name
, buf
, &bufsz
);
1429 /* Search for the message ID */
1430 if (strcasecmp (name
, "Message-ID")) {
1431 while (state
== FLDPLUS
) {
1433 state
= m_getfld2(&gstate
, name
, buf
, &bufsz
);
1438 cp
= mh_xstrdup(buf
);
1439 while (state
== FLDPLUS
) {
1441 state
= m_getfld2(&gstate
, name
, buf
, &bufsz
);
1444 key
.dptr
= trimcpy (cp
);
1445 key
.dsize
= strlen (key
.dptr
) + 1;
1449 if (!(db
= dbm_open (file
, O_RDWR
| O_CREAT
, 0600))) {
1450 advise (file
, "unable to perform dbm_open on");
1456 * Since it is difficult to portable lock a ndbm file,
1457 * we will open and lock the Maildelivery file instead.
1458 * This will fail if your Maildelivery file doesn't
1461 if ((lockfd
= lkopendata(file
, O_RDWR
, 0, &failed_to_lock
))
1463 advise (file
, "unable to perform file locking on");
1468 value
= dbm_fetch (db
, key
);
1471 verbose_printf ("Message-ID: %s\n already received on %s",
1475 value
.dptr
= ddate
+ sizeof("Delivery-Date:");
1476 value
.dsize
= strlen(value
.dptr
) + 1;
1477 if (dbm_store (db
, key
, value
, DBM_INSERT
))
1478 advise (file
, "possibly corrupt file");
1483 lkclosedata(lockfd
, file
);
1500 m_getfld_state_destroy (&gstate
);