]>
diplodocus.org Git - nmh/blob - uip/slocal.c
3 * slocal.c -- asynchronously filter and deliver new mail
5 * This code is Copyright (c) 2002, by the authors of nmh. See the
6 * COPYRIGHT file in the root directory of the nmh distribution for
7 * complete copyright information.
11 * Under sendmail, users should add the line
13 * "| /usr/local/nmh/lib/slocal"
15 * to their $HOME/.forward file.
19 /* Changed to use getutent() and friends. Assumes that when getutent() exists,
20 * a number of other things also exist. Please check.
21 * Ruud de Rooij <ruud@ruud.org> Sun, 28 May 2000 17:28:55 +0200
25 #include <h/dropsbr.h>
26 #include <h/rcvmail.h>
27 #include <h/signals.h>
34 #include <sys/ioctl.h>
37 /* Hopefully, grp.h declares initgroups(). If we run into a platform
38 where it doesn't, we could consider declaring it here as well. */
41 /* This define is needed for Berkeley db v2 and above to
42 * make the header file expose the 'historical' ndbm APIs.
43 * We define it unconditionally because this is simple and
46 #define DB_DBM_HSEARCH 1
48 #endif /* Use DB_DBM_HSEARCH to prevent warning from gcc -Wunused-macros. */
55 #endif /* HAVE_GETUTXENT */
57 #define SLOCAL_SWITCHES \
58 X("addr address", 0, ADDRSW) \
59 X("user name", 0, USERSW) \
60 X("file file", 0, FILESW) \
61 X("sender address", 0, SENDERSW) \
62 X("mailbox file", 0, MAILBOXSW) \
63 X("home directory", -4, HOMESW) \
64 X("info data", 0, INFOSW) \
65 X("maildelivery file", 0, MAILSW) \
66 X("verbose", 0, VERBSW) \
67 X("noverbose", 0, NVERBSW) \
68 X("suppressdup", 0, SUPPRESSDUP) \
69 X("nosuppressdup", 0, NSUPPRESSDUP) \
70 X("debug", 0, DEBUGSW) \
71 X("version", 0, VERSIONSW) \
72 X("help", 0, HELPSW) \
74 #define X(sw, minchars, id) id,
75 DEFINE_SWITCH_ENUM(SLOCAL
);
78 #define X(sw, minchars, id) { sw, minchars, id },
79 DEFINE_SWITCH_ARRAY(SLOCAL
, switches
);
82 static int globbed
= 0; /* have we built "vars" table yet? */
83 static int parsed
= 0; /* have we built header field table yet */
84 static int utmped
= 0; /* have we scanned umtp(x) file yet */
85 static int suppressdup
= 0; /* are we suppressing duplicate messages? */
87 static int verbose
= 0;
90 static char *addr
= NULL
;
91 static char *user
= NULL
;
92 static char *info
= NULL
;
93 static char *file
= NULL
;
94 static char *sender
= NULL
;
95 static char *envelope
= NULL
; /* envelope information ("From " line) */
96 static char *mbox
= NULL
;
97 static char *home
= NULL
;
99 static struct passwd
*pw
; /* passwd file entry */
101 static char ddate
[BUFSIZ
]; /* record the delivery date */
104 static jmp_buf myctx
;
106 /* flags for pair->p_flags */
108 #define P_ADR 0x01 /* field is address */
109 #define P_HID 0x02 /* special (fake) field */
121 * Lookup table for matching fields and patterns
122 * in messages. The rest of the table is added
123 * when the message is parsed.
125 static struct pair hdrs
[NVEC
+ 1] = {
126 { "source", NULL
, P_HID
},
127 { "addr", NULL
, P_HID
},
128 { "Return-Path", NULL
, P_ADR
},
129 { "Reply-To", NULL
, P_ADR
},
130 { "From", NULL
, P_ADR
},
131 { "Sender", NULL
, P_ADR
},
132 { "To", NULL
, P_ADR
},
133 { "cc", NULL
, P_ADR
},
134 { "Resent-Reply-To", NULL
, P_ADR
},
135 { "Resent-From", NULL
, P_ADR
},
136 { "Resent-Sender", NULL
, P_ADR
},
137 { "Resent-To", NULL
, P_ADR
},
138 { "Resent-cc", NULL
, P_ADR
},
143 * The list of builtin variables to expand in a string
144 * before it is executed by the "pipe" or "qpipe" action.
146 static struct pair vars
[] = {
147 { "sender", NULL
, P_NIL
},
148 { "address", NULL
, P_NIL
},
149 { "size", NULL
, P_NIL
},
150 { "reply-to", NULL
, P_CHK
},
151 { "info", NULL
, P_NIL
},
155 extern char **environ
;
160 static int localmail (int, char *);
161 static int usr_delivery (int, char *, int);
162 static int split (char *, char **);
163 static int parse (int);
164 static void expand (char *, char *, int);
165 static void glob (int);
166 static struct pair
*lookup (struct pair
*, char *);
167 static int logged_in (void);
168 static int timely (char *, char *);
169 static int usr_file (int, char *, int);
170 static int usr_pipe (int, char *, char *, char **, int);
171 static int usr_folder (int, char *);
172 static void alrmser (int);
173 static void get_sender (char *, char **);
174 static int copy_message (int, char *, int);
175 static void verbose_printf (char *fmt
, ...);
176 static void adorn (char *, char *, ...);
177 static void debug_printf (char *fmt
, ...);
178 static int suppress_duplicates (int, char *);
179 static char *trim (char *);
183 main (int argc
, char **argv
)
187 char *cp
, *mdlvr
= NULL
, buf
[BUFSIZ
];
188 char mailbox
[BUFSIZ
], tmpfil
[BUFSIZ
];
189 char **argp
, **arguments
;
191 if (nmh_init(argv
[0], 0 /* use context_foil() */)) { return 1; }
193 mts_init (invo_name
);
194 arguments
= getarguments (invo_name
, argc
, argv
, 0);
197 /* Parse arguments */
198 while ((cp
= *argp
++)) {
200 switch (smatch (++cp
, switches
)) {
202 ambigsw (cp
, switches
);
205 adios (NULL
, "-%s unknown", cp
);
208 snprintf (buf
, sizeof(buf
), "%s [switches]", invo_name
);
209 print_help (buf
, switches
, 0);
212 print_version(invo_name
);
216 if (!(addr
= *argp
++))/* allow -xyz arguments */
217 adios (NULL
, "missing argument to %s", argp
[-2]);
220 if (!(info
= *argp
++))/* allow -xyz arguments */
221 adios (NULL
, "missing argument to %s", argp
[-2]);
224 if (!(user
= *argp
++))/* allow -xyz arguments */
225 adios (NULL
, "missing argument to %s", argp
[-2]);
228 if (!(file
= *argp
++) || *file
== '-')
229 adios (NULL
, "missing argument to %s", argp
[-2]);
232 if (!(sender
= *argp
++))/* allow -xyz arguments */
233 adios (NULL
, "missing argument to %s", argp
[-2]);
236 if (!(mbox
= *argp
++) || *mbox
== '-')
237 adios (NULL
, "missing argument to %s", argp
[-2]);
240 if (!(home
= *argp
++) || *home
== '-')
241 adios (NULL
, "missing argument to %s", argp
[-2]);
245 if (!(cp
= *argp
++) || *cp
== '-')
246 adios (NULL
, "missing argument to %s", argp
[-2]);
248 adios (NULL
, "only one maildelivery file at a time!");
270 adios (NULL
, "only switch arguments are supported");
275 addr
= getusername ();
277 user
= (cp
= strchr(addr
, '.')) ? ++cp
: addr
;
278 if ((pw
= getpwnam (user
)) == NULL
)
279 adios (NULL
, "no such local user as %s", user
);
281 if (chdir (pw
->pw_dir
) == -1)
282 if (chdir ("/") < 0) {
283 advise ("/", "chdir");
287 if (geteuid() == 0) {
288 if (setgid (pw
->pw_gid
) != 0) {
289 adios ("setgid", "unable to set group to %ld", (long) pw
->pw_gid
);
291 initgroups (pw
->pw_name
, pw
->pw_gid
);
292 if (setuid (pw
->pw_uid
) != 0) {
293 adios ("setuid", "unable to set user to %ld", (long) pw
->pw_uid
);
300 setbuf (stdin
, NULL
);
302 /* Record the delivery time */
303 if ((now
= dlocaltimenow ()) == NULL
)
304 adios (NULL
, "unable to ascertain local time");
305 snprintf (ddate
, sizeof(ddate
), "Delivery-Date: %s\n", dtimenow (0));
308 * Copy the message to a temporary file
313 /* getting message from file */
314 if ((tempfd
= open (file
, O_RDONLY
)) == -1)
315 adios (file
, "unable to open");
317 debug_printf ("retrieving message from file \"%s\"\n", file
);
318 if ((fd
= copy_message (tempfd
, tmpfil
, 1)) == -1)
319 adios(NULL
, "unable to create temporary file in %s",
323 /* getting message from stdin */
325 debug_printf ("retrieving message from stdin\n");
326 if ((fd
= copy_message (fileno (stdin
), tmpfil
, 1)) == -1)
327 adios(NULL
, "unable to create temporary file in %s",
332 debug_printf ("temporary file=\"%s\"\n", tmpfil
);
334 /* Delete the temp file now or a copy of every single message passed through
335 slocal will be left in the /tmp directory until deleted manually! This
336 unlink() used to be under an 'else' of the 'if (debug)' above, but since
337 some people like to always run slocal with -debug and log the results,
338 the /tmp directory would get choked over time. Of course, now that we
339 always delete the temp file, the "temporary file=" message above is
340 somewhat pointless -- someone watching debug output wouldn't have a
341 chance to 'tail -f' or 'ln' the temp file before it's unlinked. The best
342 thing would be to delay this unlink() until later if debug == 1, but I'll
343 leave that for someone who cares about the temp-file-accessing
344 functionality (they'll have to watch out for cases where we adios()). */
345 (void) m_unlink (tmpfil
);
347 if (!(fp
= fdopen (fd
, "r+")))
348 adios (NULL
, "unable to access temporary file");
351 * If no sender given, extract it
352 * from envelope information. */
354 get_sender (envelope
, &sender
);
357 snprintf (mailbox
, sizeof(mailbox
), "%s/%s",
358 mmdfldir
[0] ? mmdfldir
: pw
->pw_dir
,
359 mmdflfil
[0] ? mmdflfil
: pw
->pw_name
);
366 debug_printf ("addr=\"%s\"\n", trim(addr
));
367 debug_printf ("user=\"%s\"\n", trim(user
));
368 debug_printf ("info=\"%s\"\n", trim(info
));
369 debug_printf ("sender=\"%s\"\n", trim(sender
));
370 debug_printf ("envelope=\"%s\"\n", envelope
? trim(envelope
) : "");
371 debug_printf ("mbox=\"%s\"\n", trim(mbox
));
372 debug_printf ("home=\"%s\"\n", trim(home
));
373 debug_printf ("ddate=\"%s\"\n", trim(ddate
));
374 debug_printf ("now=%02d:%02d\n\n", now
->tw_hour
, now
->tw_min
);
377 /* deliver the message */
378 status
= localmail (fd
, mdlvr
);
380 done (status
!= -1 ? RCV_MOK
: RCV_MBX
);
386 * Main routine for delivering message.
390 localmail (int fd
, char *mdlvr
)
392 /* check if this message is a duplicate */
394 suppress_duplicates(fd
, mdlvr
? mdlvr
: ".maildelivery") == DONE
)
397 /* delivery according to personal Maildelivery file */
398 if (usr_delivery (fd
, mdlvr
? mdlvr
: ".maildelivery", 0) != -1)
401 /* delivery according to global Maildelivery file */
402 if (usr_delivery (fd
, maildelivery
, 1) != -1)
406 verbose_printf ("(delivering to standard mail spool)\n");
408 /* last resort - deliver to standard mail spool */
409 return usr_file (fd
, mbox
, MBOX_FORMAT
);
413 #define matches(a,b) (stringdex (b, a) >= 0)
416 * Parse the delivery file, and process incoming message.
420 usr_delivery (int fd
, char *delivery
, int su
)
422 int i
, accept
, status
=1, won
, vecp
, next
;
423 char *field
, *pattern
, *action
, *result
, *string
;
424 char buffer
[BUFSIZ
], tmpbuf
[BUFSIZ
];
425 char *cp
, *vec
[NVEC
];
430 /* open the delivery file */
431 if ((fp
= fopen (delivery
, "r")) == NULL
)
434 /* check if delivery file has bad ownership or permissions */
435 if (fstat (fileno (fp
), &st
) == -1
436 || (st
.st_uid
!= 0 && (su
|| st
.st_uid
!= pw
->pw_uid
))
437 || st
.st_mode
& (S_IWGRP
|S_IWOTH
)) {
439 verbose_printf ("WARNING: %s has bad ownership/modes (su=%d,uid=%d,owner=%d,mode=0%o)\n",
440 delivery
, su
, (int) pw
->pw_uid
, (int) st
.st_uid
, (int) st
.st_mode
);
448 /* read and process delivery file */
449 while (fgets (buffer
, sizeof(buffer
), fp
)) {
450 /* skip comments and empty lines */
451 if (*buffer
== '#' || *buffer
== '\n')
454 /* zap trailing newline */
455 if ((cp
= strchr(buffer
, '\n')))
458 /* split buffer into fields */
459 vecp
= split (buffer
, vec
);
461 /* check for too few fields */
464 debug_printf ("WARNING: entry with only %d fields, skipping.\n", vecp
);
469 for (i
= 0; vec
[i
]; i
++)
470 debug_printf ("vec[%d]: \"%s\"\n", i
, trim(vec
[i
]));
479 /* find out how to perform the action */
484 * If previous condition failed, don't
485 * do this - else fall through
488 continue; /* else fall */
492 * If already delivered, skip this action. Else
493 * consider delivered if action is successful.
496 continue; /* else fall */
501 * Take action, and consider delivered if
502 * action is successful.
511 * Take action, but don't consider delivered, even
512 * if action is successful
519 if (!strcasecmp (vec
[5], "select")) {
520 if (logged_in () != -1)
522 if (vecp
> 7 && timely (vec
[6], vec
[7]) == -1)
527 /* check if the field matches */
535 * "default" matches only if the message hasn't
536 * been delivered yet.
538 if (!strcasecmp (field
, "default")) {
545 /* parse message and build lookup table */
546 if (!parsed
&& parse (fd
) == -1) {
551 * find header field in lookup table, and
552 * see if the pattern matches.
554 if ((p
= lookup (hdrs
, field
)) && (p
->p_value
!= NULL
)
555 && matches (p
->p_value
, pattern
)) {
564 /* find out the action to perform */
567 /* deliver to quoted pipe */
568 if (strcasecmp (action
, "qpipe"))
569 continue; /* else fall */
571 expand (tmpbuf
, string
, fd
);
572 if (split (tmpbuf
, vec
) < 1)
574 status
= usr_pipe (fd
, tmpbuf
, vec
[0], vec
, 0);
578 /* deliver to pipe */
579 if (strcasecmp (action
, "pipe"))
580 continue; /* else fall */
584 expand (tmpbuf
, string
, fd
);
587 status
= usr_pipe (fd
, tmpbuf
, "/bin/sh", vec
+ 2, 0);
592 if (!strcasecmp (action
, "file")) {
593 status
= usr_file (fd
, string
, MBOX_FORMAT
);
596 /* deliver to nmh folder */
597 else if (strcasecmp (action
, "folder"))
598 continue; /* else fall */
600 status
= usr_folder (fd
, string
);
605 if (!strcasecmp (action
, "mmdf")) {
606 status
= usr_file (fd
, string
, MMDF_FORMAT
);
610 else if (strcasecmp (action
, "mbox"))
611 continue; /* else fall */
615 status
= usr_file (fd
, string
, MBOX_FORMAT
);
620 if (strcasecmp (action
, "destroy"))
626 if (status
) next
= 0; /* action failed, mark for 'N' result */
628 if (accept
&& status
== 0)
633 return (won
? 0 : -1);
640 * Split buffer into fields (delimited by whitespace or
641 * comma's). Return the number of fields found.
645 split (char *cp
, char **vec
)
652 /* split into a maximum of NVEC fields */
653 for (i
= 0; i
<= NVEC
;) {
656 /* zap any whitespace and comma's */
657 while (isspace ((unsigned char) *s
) || *s
== ',')
660 /* end of buffer, time to leave */
664 /* get double quote text as a single field */
666 for (vec
[i
++] = ++s
; *s
&& *s
!= '"'; s
++) {
668 * Check for escaped double quote. We need
669 * to shift the string to remove slash.
677 if (*s
== '"') /* zap trailing double quote */
682 if (*s
== QUOTE
&& *++s
!= '"')
686 /* move forward to next field delimiter */
687 while (*s
&& !isspace ((unsigned char) *s
) && *s
!= ',')
697 * Parse the headers of a message, and build the
698 * lookup table for matching fields and patterns.
707 char name
[NAMESZ
], field
[BUFSIZ
];
710 m_getfld_state_t gstate
= 0;
715 /* get a new FILE pointer to message */
716 if ((fd1
= dup (fd
)) == -1)
718 if ((in
= fdopen (fd1
, "r")) == NULL
) {
724 /* add special entries to lookup table */
725 if ((p
= lookup (hdrs
, "source")))
726 p
->p_value
= getcpy (sender
);
727 if ((p
= lookup (hdrs
, "addr")))
728 p
->p_value
= getcpy (addr
);
731 * Scan the headers of the message and build
735 int fieldsz
= sizeof field
;
736 switch (state
= m_getfld (&gstate
, name
, field
, &fieldsz
, in
)) {
739 lp
= add (field
, NULL
);
740 while (state
== FLDPLUS
) {
741 fieldsz
= sizeof field
;
742 state
= m_getfld (&gstate
, name
, field
, &fieldsz
, in
);
743 lp
= add (field
, lp
);
745 for (p
= hdrs
; p
->p_name
; p
++) {
746 if (!strcasecmp (p
->p_name
, name
)) {
747 if (!(p
->p_flags
& P_HID
)) {
748 if ((cp
= p
->p_value
)) {
749 if (p
->p_flags
& P_ADR
) {
750 dp
= cp
+ strlen (cp
) - 1;
753 cp
= add (",\n\t", cp
);
758 p
->p_value
= add (lp
, cp
);
764 if (p
->p_name
== NULL
&& i
< NVEC
) {
765 p
->p_name
= getcpy (name
);
779 advise (NULL
, "format error in message");
783 advise (NULL
, "internal error in m_getfld");
789 m_getfld_state_destroy (&gstate
);
792 if ((p
= lookup (vars
, "reply-to"))) {
793 if ((q
= lookup (hdrs
, "reply-to")) == NULL
|| q
->p_value
== NULL
)
794 q
= lookup (hdrs
, "from");
795 p
->p_value
= getcpy (q
? q
->p_value
: "");
796 p
->p_flags
&= ~P_CHK
;
798 debug_printf ("vars[%d]: name=\"%s\" value=\"%s\"\n",
799 p
- vars
, p
->p_name
, trim(p
->p_value
));
802 for (p
= hdrs
; p
->p_name
; p
++)
803 debug_printf ("hdrs[%d]: name=\"%s\" value=\"%s\"\n",
804 p
- hdrs
, p
->p_name
, p
->p_value
? trim(p
->p_value
) : "");
815 * Expand any builtin variables such as $(sender),
816 * $(address), etc., in a string.
820 expand (char *s1
, char *s2
, int fd
)
828 while ((c
= *s2
++)) {
829 if (c
!= '$' || *s2
!= LPAREN
) {
832 for (cp
= ++s2
; *s2
&& *s2
!= RPAREN
; s2
++)
839 if ((p
= lookup (vars
, cp
))) {
840 if (!parsed
&& (p
->p_flags
& P_CHK
))
843 strcpy (s1
, p
->p_value
);
853 * Fill in the information missing from the "vars"
854 * table, which is necessary to expand any builtin
855 * variables in the string for a "pipe" or "qpipe"
869 if ((p
= lookup (vars
, "sender")))
870 p
->p_value
= getcpy (sender
);
871 if ((p
= lookup (vars
, "address")))
872 p
->p_value
= getcpy (addr
);
873 if ((p
= lookup (vars
, "size"))) {
874 snprintf (buffer
, sizeof(buffer
), "%d",
875 fstat (fd
, &st
) != -1 ? (int) st
.st_size
: 0);
876 p
->p_value
= getcpy (buffer
);
878 if ((p
= lookup (vars
, "info")))
879 p
->p_value
= getcpy (info
);
882 for (p
= vars
; p
->p_name
; p
++)
883 debug_printf ("vars[%d]: name=\"%s\" value=\"%s\"\n",
884 p
- vars
, p
->p_name
, trim(p
->p_value
));
890 * Find a matching name in a lookup table. If found,
891 * return the "pairs" entry, else return NULL.
895 lookup (struct pair
*pairs
, char *key
)
897 for (; pairs
->p_name
; pairs
++)
898 if (!strcasecmp (pairs
->p_name
, key
))
906 * Check utmp(x) file to see if user is currently
921 while ((utp
= getutxent()) != NULL
) {
922 if ( utp
->ut_type
== USER_PROCESS
&& utp
->ut_user
[0] != 0
923 && strncmp (user
, utp
->ut_user
, sizeof(utp
->ut_user
)) == 0) {
927 return (utmped
= DONE
);
932 #endif /* HAVE_GETUTXENT */
933 return (utmped
= NOTOK
);
936 #define check(t,a,b) if (t < a || t > b) return -1
937 #define cmpar(h1,m1,h2,m2) if (h1 < h2 || (h1 == h2 && m1 < m2)) return 0
940 timely (char *t1
, char *t2
)
942 int t1hours
, t1mins
, t2hours
, t2mins
;
944 if (sscanf (t1
, "%d:%d", &t1hours
, &t1mins
) != 2)
946 check (t1hours
, 0, 23);
947 check (t1mins
, 0, 59);
949 if (sscanf (t2
, "%d:%d", &t2hours
, &t2mins
) != 2)
951 check (t2hours
, 0, 23);
952 check (t2mins
, 0, 59);
954 cmpar (now
->tw_hour
, now
->tw_min
, t1hours
, t1mins
);
955 cmpar (t2hours
, t2mins
, now
->tw_hour
, now
->tw_min
);
962 * Deliver message by appending to a file.
966 usr_file (int fd
, char *mailbox
, int mbx_style
)
971 verbose_printf ("delivering to file \"%s\"", mailbox
);
973 if (mbx_style
== MBOX_FORMAT
) {
975 verbose_printf (" (mbox style)");
979 verbose_printf (" (mmdf style)");
983 /* open and lock the file */
984 if ((md
= mbx_open (mailbox
, mbx_style
, pw
->pw_uid
, pw
->pw_gid
, m_gmprot())) == -1) {
986 adorn ("", "unable to open:");
990 lseek (fd
, (off_t
) 0, SEEK_SET
);
992 /* append message to file */
993 if (mbx_copy (mailbox
, mbx_style
, md
, fd
, mapping
, NULL
, verbose
) == -1) {
995 adorn ("", "error writing to:");
999 /* close and unlock file */
1000 if (mbx_close (mailbox
, md
) == NOTOK
) {
1002 adorn ("", "error closing:");
1007 verbose_printf (", success.\n");
1013 * Deliver message to a nmh folder.
1017 usr_folder (int fd
, char *string
)
1020 char folder
[BUFSIZ
], *vec
[3];
1022 /* get folder name ready */
1024 strncpy(folder
, string
, sizeof(folder
));
1026 snprintf(folder
, sizeof(folder
), "+%s", string
);
1029 verbose_printf ("delivering to folder \"%s\"", folder
+ 1);
1031 vec
[0] = "rcvstore";
1035 /* use rcvstore to put message in folder */
1036 status
= usr_pipe (fd
, "rcvstore", rcvstoreproc
, vec
, 1);
1042 * Deliver message to a process.
1046 usr_pipe (int fd_arg
, char *cmd
, char *pgm
, char **vec
, int suppress
)
1048 volatile int fd
= fd_arg
;
1050 int i
, bytes
, seconds
, status
;
1053 if (verbose
&& !suppress
)
1054 verbose_printf ("delivering to pipe \"%s\"", cmd
);
1056 lseek (fd
, (off_t
) 0, SEEK_SET
);
1058 for (i
= 0; (child_id
= fork()) == -1 && i
< 5; i
++)
1065 adorn ("fork", "unable to");
1072 if (freopen ("/dev/null", "w", stdout
) == NULL
) {
1073 advise ("stdout", "freopen");
1075 if (freopen ("/dev/null", "w", stderr
) == NULL
) {
1076 advise ("stderr", "freopen");
1083 if ((fd
= open ("/dev/tty", O_RDWR
)) != -1) {
1084 ioctl (fd
, TIOCNOTTY
, NULL
);
1087 #endif /* TIOCNOTTY */
1089 setpgid ((pid_t
) 0, getpid ()); /* put in own process group */
1092 m_putenv ("USER", pw
->pw_name
);
1093 m_putenv ("HOME", pw
->pw_dir
);
1094 m_putenv ("SHELL", pw
->pw_shell
);
1100 /* parent process */
1101 if (! setjmp (myctx
)) {
1102 SIGNAL (SIGALRM
, alrmser
);
1103 bytes
= fstat (fd
, &st
) != -1 ? (int) st
.st_size
: 100;
1105 /* amount of time to wait depends on message size */
1107 /* give at least 5 minutes */
1109 } else if (bytes
>= 90000) {
1110 /* a half hour is long enough */
1113 seconds
= (bytes
/ 60) + 300;
1115 alarm ((unsigned int) seconds
);
1116 status
= pidwait (child_id
, 0);
1121 verbose_printf (", success.\n");
1123 if ((status
& 0xff00) == 0xff00)
1124 verbose_printf (", system error\n");
1126 pidstatus (status
, stdout
, ", failed");
1128 return (status
== 0 ? 0 : -1);
1131 * Ruthlessly kill the child and anything
1132 * else in its process group.
1134 killpg(child_id
, SIGKILL
);
1136 verbose_printf (", timed-out; terminated\n");
1148 longjmp (myctx
, DONE
);
1153 * Get the `sender' from the envelope
1154 * information ("From " line).
1158 get_sender (char *envelope
, char **sender
)
1162 char buffer
[BUFSIZ
];
1164 if (envelope
== NULL
) {
1165 *sender
= getcpy ("");
1169 i
= strlen ("From ");
1170 strncpy (buffer
, envelope
+ i
, sizeof(buffer
));
1171 if ((cp
= strchr(buffer
, '\n'))) {
1181 for (cp
= buffer
+ strlen (buffer
) - 1; cp
>= buffer
; cp
--)
1182 if (isspace ((unsigned char) *cp
))
1186 *sender
= getcpy (buffer
);
1191 * Copy message into a temporary file.
1192 * While copying, it will do some header processing
1193 * including the extraction of the envelope information.
1197 copy_message (int qd
, char *tmpfil
, int fold
)
1199 int i
, first
= 1, fd1
, fd2
;
1200 char buffer
[BUFSIZ
];
1204 tfile
= m_mktemp2(NULL
, invo_name
, &fd1
, NULL
);
1205 if (tfile
== NULL
) return -1;
1206 strncpy (tmpfil
, tfile
, BUFSIZ
);
1209 while ((i
= read (qd
, buffer
, sizeof(buffer
))) > 0)
1210 if (write (fd1
, buffer
, i
) != i
) {
1213 (void) m_unlink (tmpfil
);
1218 lseek (fd1
, (off_t
) 0, SEEK_SET
);
1222 /* dup the fd for incoming message */
1223 if ((fd2
= dup (qd
)) == -1) {
1228 /* now create a FILE pointer for it */
1229 if ((qfp
= fdopen (fd2
, "r")) == NULL
) {
1235 /* dup the fd for temporary file */
1236 if ((fd2
= dup (fd1
)) == -1) {
1242 /* now create a FILE pointer for it */
1243 if ((ffp
= fdopen (fd2
, "r+")) == NULL
) {
1251 * copy message into temporary file
1252 * and massage the headers. Save
1253 * a copy of the "From " line for later.
1255 i
= strlen ("From ");
1256 while (fgets (buffer
, sizeof(buffer
), qfp
)) {
1259 if (!strncmp (buffer
, "From ", i
)) {
1260 /* get copy of envelope information ("From " line) */
1261 envelope
= getcpy (buffer
);
1263 /* Put the delivery date in message */
1272 fputs (buffer
, ffp
);
1284 lseek (fd1
, (off_t
) 0, SEEK_SET
);
1296 * Trim strings for pretty printing of debugging output
1302 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
))
1329 /* now return a copy */
1334 * Function for printing `verbose' messages.
1338 verbose_printf (char *fmt
, ...)
1343 vfprintf (stdout
, fmt
, ap
);
1346 fflush (stdout
); /* now flush output */
1351 * Function for printing `verbose' delivery
1356 adorn (char *what
, char *fmt
, ...)
1362 eindex
= errno
; /* save the errno */
1363 fprintf (stdout
, ", ");
1366 vfprintf (stdout
, fmt
, ap
);
1371 fprintf (stdout
, " %s: ", what
);
1372 if ((s
= strerror (eindex
)))
1373 fprintf (stdout
, "%s", s
);
1375 fprintf (stdout
, "Error %d", eindex
);
1378 fputc ('\n', stdout
);
1384 * Function for printing `debug' messages.
1388 debug_printf (char *fmt
, ...)
1393 vfprintf (stderr
, fmt
, ap
);
1399 * Check ndbm/db file(s) to see if the Message-Id of this
1400 * message matches the Message-Id of a previous message,
1401 * so we can discard it. If it doesn't match, we add the
1402 * Message-Id of this message to the ndbm/db file.
1405 suppress_duplicates (int fd
, char *file
)
1407 int fd1
, lockfd
, state
, result
;
1408 char *cp
, buf
[BUFSIZ
], name
[NAMESZ
];
1412 m_getfld_state_t gstate
= 0;
1414 if ((fd1
= dup (fd
)) == -1)
1416 if (!(in
= fdopen (fd1
, "r"))) {
1423 int failed_to_lock
= 0;
1424 int bufsz
= sizeof buf
;
1425 state
= m_getfld (&gstate
, name
, buf
, &bufsz
, in
);
1429 /* Search for the message ID */
1430 if (strcasecmp (name
, "Message-ID")) {
1431 while (state
== FLDPLUS
) {
1433 state
= m_getfld (&gstate
, name
, buf
, &bufsz
, in
);
1438 cp
= add (buf
, NULL
);
1439 while (state
== FLDPLUS
) {
1441 state
= m_getfld (&gstate
, name
, buf
, &bufsz
, in
);
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
);