]>
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>
32 #include <sys/ioctl.h>
35 /* Hopefully, grp.h declares initgroups(). If we run into a platform
36 where it doesn't, we could consider declaring it here as well. */
39 /* This define is needed for Berkeley db v2 and above to
40 * make the header file expose the 'historical' ndbm APIs.
41 * We define it unconditionally because this is simple and
44 #define DB_DBM_HSEARCH 1
46 #endif /* Use DB_DBM_HSEARCH to prevent warning from gcc -Wunused-macros. */
53 #endif /* HAVE_GETUTXENT */
55 #define SLOCAL_SWITCHES \
56 X("addr address", 0, ADDRSW) \
57 X("user name", 0, USERSW) \
58 X("file file", 0, FILESW) \
59 X("sender address", 0, SENDERSW) \
60 X("mailbox file", 0, MAILBOXSW) \
61 X("home directory", -4, HOMESW) \
62 X("info data", 0, INFOSW) \
63 X("maildelivery file", 0, MAILSW) \
64 X("verbose", 0, VERBSW) \
65 X("noverbose", 0, NVERBSW) \
66 X("suppressdup", 0, SUPPRESSDUP) \
67 X("nosuppressdup", 0, NSUPPRESSDUP) \
68 X("debug", 0, DEBUGSW) \
69 X("version", 0, VERSIONSW) \
70 X("help", 0, HELPSW) \
72 #define X(sw, minchars, id) id,
73 DEFINE_SWITCH_ENUM(SLOCAL
);
76 #define X(sw, minchars, id) { sw, minchars, id },
77 DEFINE_SWITCH_ARRAY(SLOCAL
, switches
);
80 static int globbed
= 0; /* have we built "vars" table yet? */
81 static int parsed
= 0; /* have we built header field table yet */
82 static int utmped
= 0; /* have we scanned umtp(x) file yet */
83 static int suppressdup
= 0; /* are we suppressing duplicate messages? */
85 static int verbose
= 0;
88 static char *addr
= NULL
;
89 static char *user
= NULL
;
90 static char *info
= NULL
;
91 static char *file
= NULL
;
92 static char *sender
= NULL
;
93 static char *envelope
= NULL
; /* envelope information ("From " line) */
94 static char *mbox
= NULL
;
95 static char *home
= NULL
;
97 static struct passwd
*pw
; /* passwd file entry */
99 static char ddate
[BUFSIZ
]; /* record the delivery date */
102 static jmp_buf myctx
;
104 /* flags for pair->p_flags */
106 #define P_ADR 0x01 /* field is address */
107 #define P_HID 0x02 /* special (fake) field */
119 * Lookup table for matching fields and patterns
120 * in messages. The rest of the table is added
121 * when the message is parsed.
123 static struct pair hdrs
[NVEC
+ 1] = {
124 { "source", NULL
, P_HID
},
125 { "addr", NULL
, P_HID
},
126 { "Return-Path", NULL
, P_ADR
},
127 { "Reply-To", NULL
, P_ADR
},
128 { "From", NULL
, P_ADR
},
129 { "Sender", NULL
, P_ADR
},
130 { "To", NULL
, P_ADR
},
131 { "cc", NULL
, P_ADR
},
132 { "Resent-Reply-To", NULL
, P_ADR
},
133 { "Resent-From", NULL
, P_ADR
},
134 { "Resent-Sender", NULL
, P_ADR
},
135 { "Resent-To", NULL
, P_ADR
},
136 { "Resent-cc", NULL
, P_ADR
},
141 * The list of builtin variables to expand in a string
142 * before it is executed by the "pipe" or "qpipe" action.
144 static struct pair vars
[] = {
145 { "sender", NULL
, P_NIL
},
146 { "address", NULL
, P_NIL
},
147 { "size", NULL
, P_NIL
},
148 { "reply-to", NULL
, P_CHK
},
149 { "info", NULL
, P_NIL
},
153 extern char **environ
;
158 static int localmail (int, char *);
159 static int usr_delivery (int, char *, int);
160 static int split (char *, char **);
161 static int parse (int);
162 static void expand (char *, char *, int);
163 static void glob (int);
164 static struct pair
*lookup (struct pair
*, char *);
165 static int logged_in (void);
166 static int timely (char *, char *);
167 static int usr_file (int, char *, int);
168 static int usr_pipe (int, char *, char *, char **, int);
169 static int usr_folder (int, char *);
170 static void alrmser (int);
171 static void get_sender (char *, char **);
172 static int copy_message (int, char *, int);
173 static void verbose_printf (char *fmt
, ...);
174 static void adorn (char *, char *, ...);
175 static void debug_printf (char *fmt
, ...);
176 static int suppress_duplicates (int, char *);
177 static char *trim (char *);
181 main (int argc
, char **argv
)
185 char *cp
, *mdlvr
= NULL
, buf
[BUFSIZ
];
186 char mailbox
[BUFSIZ
], tmpfil
[BUFSIZ
];
187 char **argp
, **arguments
;
189 if (nmh_init(argv
[0], 0 /* use context_foil() */)) { return 1; }
192 arguments
= getarguments (invo_name
, argc
, argv
, 0);
195 /* Parse arguments */
196 while ((cp
= *argp
++)) {
198 switch (smatch (++cp
, switches
)) {
200 ambigsw (cp
, switches
);
203 adios (NULL
, "-%s unknown", cp
);
206 snprintf (buf
, sizeof(buf
), "%s [switches]", invo_name
);
207 print_help (buf
, switches
, 0);
210 print_version(invo_name
);
214 if (!(addr
= *argp
++))/* allow -xyz arguments */
215 adios (NULL
, "missing argument to %s", argp
[-2]);
218 if (!(info
= *argp
++))/* allow -xyz arguments */
219 adios (NULL
, "missing argument to %s", argp
[-2]);
222 if (!(user
= *argp
++))/* allow -xyz arguments */
223 adios (NULL
, "missing argument to %s", argp
[-2]);
226 if (!(file
= *argp
++) || *file
== '-')
227 adios (NULL
, "missing argument to %s", argp
[-2]);
230 if (!(sender
= *argp
++))/* allow -xyz arguments */
231 adios (NULL
, "missing argument to %s", argp
[-2]);
234 if (!(mbox
= *argp
++) || *mbox
== '-')
235 adios (NULL
, "missing argument to %s", argp
[-2]);
238 if (!(home
= *argp
++) || *home
== '-')
239 adios (NULL
, "missing argument to %s", argp
[-2]);
243 if (!(cp
= *argp
++) || *cp
== '-')
244 adios (NULL
, "missing argument to %s", argp
[-2]);
246 adios (NULL
, "only one maildelivery file at a time!");
268 adios (NULL
, "only switch arguments are supported");
273 addr
= getusername ();
275 user
= getusername ();
277 if ((pw
= getpwnam (user
)) == NULL
)
278 adios (NULL
, "no such local user as %s", user
);
280 if (chdir (pw
->pw_dir
) == -1)
281 if (chdir ("/") < 0) {
282 advise ("/", "chdir");
286 if (geteuid() == 0) {
287 if (setgid (pw
->pw_gid
) != 0) {
288 adios ("setgid", "unable to set group to %ld", (long) pw
->pw_gid
);
290 initgroups (pw
->pw_name
, pw
->pw_gid
);
291 if (setuid (pw
->pw_uid
) != 0) {
292 adios ("setuid", "unable to set user to %ld", (long) pw
->pw_uid
);
299 setbuf (stdin
, NULL
);
301 /* Record the delivery time */
302 if ((now
= dlocaltimenow ()) == NULL
)
303 adios (NULL
, "unable to ascertain local time");
304 snprintf (ddate
, sizeof(ddate
), "Delivery-Date: %s\n", dtimenow (0));
307 * Copy the message to a temporary file
312 /* getting message from file */
313 if ((tempfd
= open (file
, O_RDONLY
)) == -1)
314 adios (file
, "unable to open");
316 debug_printf ("retrieving message from file \"%s\"\n", file
);
317 if ((fd
= copy_message (tempfd
, tmpfil
, 1)) == -1)
318 adios(NULL
, "unable to create temporary file in %s",
322 /* getting message from stdin */
324 debug_printf ("retrieving message from stdin\n");
325 if ((fd
= copy_message (fileno (stdin
), tmpfil
, 1)) == -1)
326 adios(NULL
, "unable to create temporary file in %s",
331 debug_printf ("temporary file=\"%s\"\n", tmpfil
);
333 /* Delete the temp file now or a copy of every single message passed through
334 slocal will be left in the /tmp directory until deleted manually! This
335 unlink() used to be under an 'else' of the 'if (debug)' above, but since
336 some people like to always run slocal with -debug and log the results,
337 the /tmp directory would get choked over time. Of course, now that we
338 always delete the temp file, the "temporary file=" message above is
339 somewhat pointless -- someone watching debug output wouldn't have a
340 chance to 'tail -f' or 'ln' the temp file before it's unlinked. The best
341 thing would be to delay this unlink() until later if debug == 1, but I'll
342 leave that for someone who cares about the temp-file-accessing
343 functionality (they'll have to watch out for cases where we adios()). */
344 (void) m_unlink (tmpfil
);
346 if (!(fp
= fdopen (fd
, "r+")))
347 adios (NULL
, "unable to access temporary file");
350 * If no sender given, extract it
351 * from envelope information. */
353 get_sender (envelope
, &sender
);
356 snprintf (mailbox
, sizeof(mailbox
), "%s/%s",
357 mmdfldir
[0] ? mmdfldir
: pw
->pw_dir
,
358 mmdflfil
[0] ? mmdflfil
: pw
->pw_name
);
365 debug_printf ("addr=\"%s\"\n", trim(addr
));
366 debug_printf ("user=\"%s\"\n", trim(user
));
367 debug_printf ("info=\"%s\"\n", trim(info
));
368 debug_printf ("sender=\"%s\"\n", trim(sender
));
369 debug_printf ("envelope=\"%s\"\n", envelope
? trim(envelope
) : "");
370 debug_printf ("mbox=\"%s\"\n", trim(mbox
));
371 debug_printf ("home=\"%s\"\n", trim(home
));
372 debug_printf ("ddate=\"%s\"\n", trim(ddate
));
373 debug_printf ("now=%02d:%02d\n\n", now
->tw_hour
, now
->tw_min
);
376 /* deliver the message */
377 status
= localmail (fd
, mdlvr
);
379 done (status
!= -1 ? RCV_MOK
: RCV_MBX
);
385 * Main routine for delivering message.
389 localmail (int fd
, char *mdlvr
)
391 /* check if this message is a duplicate */
393 suppress_duplicates(fd
, mdlvr
? mdlvr
: ".maildelivery") == DONE
)
396 /* delivery according to personal Maildelivery file */
397 if (usr_delivery (fd
, mdlvr
? mdlvr
: ".maildelivery", 0) != -1)
400 /* delivery according to global Maildelivery file */
401 if (usr_delivery (fd
, maildelivery
, 1) != -1)
405 verbose_printf ("(delivering to standard mail spool)\n");
407 /* last resort - deliver to standard mail spool */
408 return usr_file (fd
, mbox
, MBOX_FORMAT
);
412 #define matches(a,b) (stringdex (b, a) >= 0)
415 * Parse the delivery file, and process incoming message.
419 usr_delivery (int fd
, char *delivery
, int su
)
421 int i
, accept
, status
=1, won
, vecp
, next
;
422 char *field
, *pattern
, *action
, *result
, *string
;
423 char buffer
[BUFSIZ
], tmpbuf
[BUFSIZ
];
429 /* open the delivery file */
430 if ((fp
= fopen (delivery
, "r")) == NULL
)
433 /* check if delivery file has bad ownership or permissions */
434 if (fstat (fileno (fp
), &st
) == -1
435 || (st
.st_uid
!= 0 && (su
|| st
.st_uid
!= pw
->pw_uid
))
436 || st
.st_mode
& (S_IWGRP
|S_IWOTH
)) {
438 verbose_printf ("WARNING: %s has bad ownership/modes (su=%d,uid=%d,owner=%d,mode=0%o)\n",
439 delivery
, su
, (int) pw
->pw_uid
, (int) st
.st_uid
, (int) st
.st_mode
);
447 /* read and process delivery file */
448 while (fgets (buffer
, sizeof(buffer
), fp
)) {
449 /* skip comments and empty lines */
450 if (*buffer
== '#' || *buffer
== '\n')
453 trim_suffix_c(buffer
, '\n');
455 /* split buffer into fields */
456 vecp
= split (buffer
, vec
);
458 /* check for too few fields */
461 debug_printf ("WARNING: entry with only %d fields, skipping.\n", vecp
);
466 for (i
= 0; vec
[i
]; i
++)
467 debug_printf ("vec[%d]: \"%s\"\n", i
, trim(vec
[i
]));
476 /* find out how to perform the action */
481 * If previous condition failed, don't
482 * do this - else fall through
490 * If already delivered, skip this action. Else
491 * consider delivered if action is successful.
500 * Take action, and consider delivered if
501 * action is successful.
510 * Take action, but don't consider delivered, even
511 * if action is successful
518 if (!strcasecmp (vec
[5], "select")) {
519 if (logged_in () != -1)
521 if (vecp
> 7 && timely (vec
[6], vec
[7]) == -1)
526 /* check if the field matches */
534 * "default" matches only if the message hasn't
535 * been delivered yet.
537 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"))
572 expand (tmpbuf
, string
, fd
);
573 if (split (tmpbuf
, vec
) < 1)
575 status
= usr_pipe (fd
, tmpbuf
, vec
[0], vec
, 0);
579 /* deliver to pipe */
580 if (strcasecmp (action
, "pipe"))
586 expand (tmpbuf
, string
, fd
);
589 status
= usr_pipe (fd
, tmpbuf
, "/bin/sh", vec
+ 2, 0);
594 if (!strcasecmp (action
, "file")) {
595 status
= usr_file (fd
, string
, MBOX_FORMAT
);
598 /* deliver to nmh folder */
599 else if (strcasecmp (action
, "folder"))
603 status
= usr_folder (fd
, string
);
608 if (!strcasecmp (action
, "mmdf")) {
609 status
= usr_file (fd
, string
, MMDF_FORMAT
);
613 else if (strcasecmp (action
, "mbox"))
619 status
= usr_file (fd
, string
, MBOX_FORMAT
);
624 if (strcasecmp (action
, "destroy"))
630 if (status
) next
= 0; /* action failed, mark for 'N' result */
632 if (accept
&& status
== 0)
637 return (won
? 0 : -1);
644 * Split buffer into fields (delimited by whitespace or
645 * comma's). Return the number of fields found.
649 split (char *cp
, char **vec
)
656 /* split into a maximum of NVEC fields */
657 for (i
= 0; i
<= NVEC
;) {
660 /* zap any whitespace and comma's */
661 while (isspace ((unsigned char) *s
) || *s
== ',')
664 /* end of buffer, time to leave */
668 /* get double quote text as a single field */
670 for (vec
[i
++] = ++s
; *s
&& *s
!= '"'; s
++) {
672 * Check for escaped double quote. We need
673 * to shift the string to remove slash.
681 if (*s
== '"') /* zap trailing double quote */
686 if (*s
== QUOTE
&& *++s
!= '"')
690 /* move forward to next field delimiter */
691 while (*s
&& !isspace ((unsigned char) *s
) && *s
!= ',')
701 * Parse the headers of a message, and build the
702 * lookup table for matching fields and patterns.
711 char name
[NAMESZ
], field
[BUFSIZ
];
714 m_getfld_state_t gstate
= 0;
719 /* get a new FILE pointer to message */
720 if ((fd1
= dup (fd
)) == -1)
722 if ((in
= fdopen (fd1
, "r")) == NULL
) {
728 /* add special entries to lookup table */
729 if ((p
= lookup (hdrs
, "source")))
730 p
->p_value
= getcpy (sender
);
731 if ((p
= lookup (hdrs
, "addr")))
732 p
->p_value
= getcpy (addr
);
735 * Scan the headers of the message and build
739 int fieldsz
= sizeof field
;
740 switch (state
= m_getfld (&gstate
, name
, field
, &fieldsz
, in
)) {
743 lp
= mh_xstrdup(field
);
744 while (state
== FLDPLUS
) {
745 fieldsz
= sizeof field
;
746 state
= m_getfld (&gstate
, name
, field
, &fieldsz
, in
);
747 lp
= add (field
, lp
);
749 for (p
= hdrs
; p
->p_name
; p
++) {
750 if (!strcasecmp (p
->p_name
, name
)) {
751 if (!(p
->p_flags
& P_HID
)) {
752 if ((cp
= p
->p_value
)) {
753 if (p
->p_flags
& P_ADR
) {
754 dp
= cp
+ strlen (cp
) - 1;
757 cp
= add (",\n\t", cp
);
762 p
->p_value
= add (lp
, cp
);
768 if (p
->p_name
== NULL
&& i
< NVEC
) {
769 p
->p_name
= mh_xstrdup(name
);
783 inform("format error in message");
787 inform("internal error in m_getfld");
793 m_getfld_state_destroy (&gstate
);
796 if ((p
= lookup (vars
, "reply-to"))) {
797 if ((q
= lookup (hdrs
, "reply-to")) == NULL
|| q
->p_value
== NULL
)
798 q
= lookup (hdrs
, "from");
799 p
->p_value
= getcpy (q
? q
->p_value
: "");
800 p
->p_flags
&= ~P_CHK
;
802 debug_printf ("vars[%d]: name=\"%s\" value=\"%s\"\n",
803 p
- vars
, p
->p_name
, trim(p
->p_value
));
806 for (p
= hdrs
; p
->p_name
; p
++)
807 debug_printf ("hdrs[%d]: name=\"%s\" value=\"%s\"\n",
808 p
- hdrs
, p
->p_name
, p
->p_value
? trim(p
->p_value
) : "");
819 * Expand any builtin variables such as $(sender),
820 * $(address), etc., in a string.
824 expand (char *s1
, char *s2
, int fd
)
832 while ((c
= *s2
++)) {
833 if (c
!= '$' || *s2
!= LPAREN
) {
836 for (cp
= ++s2
; *s2
&& *s2
!= RPAREN
; s2
++)
843 if ((p
= lookup (vars
, cp
))) {
844 if (!parsed
&& (p
->p_flags
& P_CHK
))
847 strcpy (s1
, p
->p_value
);
857 * Fill in the information missing from the "vars"
858 * table, which is necessary to expand any builtin
859 * variables in the string for a "pipe" or "qpipe"
873 if ((p
= lookup (vars
, "sender")))
874 p
->p_value
= getcpy (sender
);
875 if ((p
= lookup (vars
, "address")))
876 p
->p_value
= getcpy (addr
);
877 if ((p
= lookup (vars
, "size"))) {
878 snprintf (buffer
, sizeof(buffer
), "%d",
879 fstat (fd
, &st
) != -1 ? (int) st
.st_size
: 0);
880 p
->p_value
= mh_xstrdup(buffer
);
882 if ((p
= lookup (vars
, "info")))
883 p
->p_value
= getcpy (info
);
886 for (p
= vars
; p
->p_name
; p
++)
887 debug_printf ("vars[%d]: name=\"%s\" value=\"%s\"\n",
888 p
- vars
, p
->p_name
, trim(p
->p_value
));
894 * Find a matching name in a lookup table. If found,
895 * return the "pairs" entry, else return NULL.
899 lookup (struct pair
*pairs
, char *key
)
901 for (; pairs
->p_name
; pairs
++)
902 if (!strcasecmp (pairs
->p_name
, key
))
910 * Check utmp(x) file to see if user is currently
925 while ((utp
= getutxent()) != NULL
) {
926 if ( utp
->ut_type
== USER_PROCESS
&& utp
->ut_user
[0] != 0
927 && strncmp (user
, utp
->ut_user
, sizeof(utp
->ut_user
)) == 0) {
931 return (utmped
= DONE
);
936 #endif /* HAVE_GETUTXENT */
937 return (utmped
= NOTOK
);
940 #define check(t,a,b) if (t < a || t > b) return -1
941 #define cmpar(h1,m1,h2,m2) if (h1 < h2 || (h1 == h2 && m1 < m2)) return 0
944 timely (char *t1
, char *t2
)
946 int t1hours
, t1mins
, t2hours
, t2mins
;
948 if (sscanf (t1
, "%d:%d", &t1hours
, &t1mins
) != 2)
950 check (t1hours
, 0, 23);
951 check (t1mins
, 0, 59);
953 if (sscanf (t2
, "%d:%d", &t2hours
, &t2mins
) != 2)
955 check (t2hours
, 0, 23);
956 check (t2mins
, 0, 59);
958 cmpar (now
->tw_hour
, now
->tw_min
, t1hours
, t1mins
);
959 cmpar (t2hours
, t2mins
, now
->tw_hour
, now
->tw_min
);
966 * Deliver message by appending to a file.
970 usr_file (int fd
, char *mailbox
, int mbx_style
)
975 verbose_printf ("delivering to file \"%s\"", mailbox
);
977 if (mbx_style
== MBOX_FORMAT
) {
979 verbose_printf (" (mbox style)");
983 verbose_printf (" (mmdf style)");
987 /* open and lock the file */
988 if ((md
= mbx_open (mailbox
, mbx_style
, pw
->pw_uid
, pw
->pw_gid
, m_gmprot())) == -1) {
990 adorn ("", "unable to open:");
994 lseek (fd
, (off_t
) 0, SEEK_SET
);
996 /* append message to file */
997 if (mbx_copy (mailbox
, mbx_style
, md
, fd
, mapping
, NULL
, verbose
) == -1) {
999 adorn ("", "error writing to:");
1003 /* close and unlock file */
1004 if (mbx_close (mailbox
, md
) == NOTOK
) {
1006 adorn ("", "error closing:");
1011 verbose_printf (", success.\n");
1017 * Deliver message to a nmh folder.
1021 usr_folder (int fd
, char *string
)
1024 char folder
[BUFSIZ
], *vec
[3];
1026 /* get folder name ready */
1028 strncpy(folder
, string
, sizeof(folder
));
1030 snprintf(folder
, sizeof(folder
), "+%s", string
);
1033 verbose_printf ("delivering to folder \"%s\"", folder
+ 1);
1035 vec
[0] = "rcvstore";
1039 /* use rcvstore to put message in folder */
1040 status
= usr_pipe (fd
, "rcvstore", rcvstoreproc
, vec
, 1);
1046 * Deliver message to a process.
1050 usr_pipe (int fd_arg
, char *cmd
, char *pgm
, char **vec
, int suppress
)
1052 volatile int fd
= fd_arg
;
1054 int i
, bytes
, seconds
, status
;
1057 if (verbose
&& !suppress
)
1058 verbose_printf ("delivering to pipe \"%s\"", cmd
);
1060 lseek (fd
, (off_t
) 0, SEEK_SET
);
1062 for (i
= 0; (child_id
= fork()) == -1 && i
< 5; i
++)
1069 adorn ("fork", "unable to");
1076 if (freopen ("/dev/null", "w", stdout
) == NULL
) {
1077 advise ("stdout", "freopen");
1079 if (freopen ("/dev/null", "w", stderr
) == NULL
) {
1080 advise ("stderr", "freopen");
1087 if ((fd
= open ("/dev/tty", O_RDWR
)) != -1) {
1088 ioctl (fd
, TIOCNOTTY
, NULL
);
1091 #endif /* TIOCNOTTY */
1093 setpgid ((pid_t
) 0, getpid ()); /* put in own process group */
1096 m_putenv ("USER", pw
->pw_name
);
1097 m_putenv ("HOME", pw
->pw_dir
);
1098 m_putenv ("SHELL", pw
->pw_shell
);
1104 /* parent process */
1105 if (! setjmp (myctx
)) {
1106 SIGNAL (SIGALRM
, alrmser
);
1107 bytes
= fstat (fd
, &st
) != -1 ? (int) st
.st_size
: 100;
1109 /* amount of time to wait depends on message size */
1111 /* give at least 5 minutes */
1113 } else if (bytes
>= 90000) {
1114 /* a half hour is long enough */
1117 seconds
= (bytes
/ 60) + 300;
1119 alarm ((unsigned int) seconds
);
1120 status
= pidwait (child_id
, 0);
1125 verbose_printf (", success.\n");
1127 if ((status
& 0xff00) == 0xff00)
1128 verbose_printf (", system error\n");
1130 pidstatus (status
, stdout
, ", failed");
1132 return (status
== 0 ? 0 : -1);
1135 * Ruthlessly kill the child and anything
1136 * else in its process group.
1138 killpg(child_id
, SIGKILL
);
1140 verbose_printf (", timed-out; terminated\n");
1151 longjmp (myctx
, DONE
);
1156 * Get the `sender' from the envelope
1157 * information ("From " line).
1161 get_sender (char *envelope
, char **sender
)
1165 char buffer
[BUFSIZ
];
1167 if (envelope
== NULL
) {
1168 *sender
= mh_xstrdup("");
1173 strncpy (buffer
, envelope
+ i
, sizeof(buffer
));
1174 if ((cp
= strchr(buffer
, '\n'))) {
1184 for (cp
= buffer
+ strlen (buffer
) - 1; cp
>= buffer
; cp
--)
1185 if (isspace ((unsigned char) *cp
))
1189 *sender
= mh_xstrdup(buffer
);
1194 * Copy message into a temporary file.
1195 * While copying, it will do some header processing
1196 * including the extraction of the envelope information.
1200 copy_message (int qd
, char *tmpfil
, int fold
)
1202 int i
, first
= 1, fd1
, fd2
;
1203 char buffer
[BUFSIZ
];
1207 tfile
= m_mktemp2(NULL
, invo_name
, &fd1
, NULL
);
1208 if (tfile
== NULL
) return -1;
1209 strncpy (tmpfil
, tfile
, BUFSIZ
);
1212 while ((i
= read (qd
, buffer
, sizeof(buffer
))) > 0)
1213 if (write (fd1
, buffer
, i
) != i
) {
1216 (void) m_unlink (tmpfil
);
1221 lseek (fd1
, (off_t
) 0, SEEK_SET
);
1225 /* dup the fd for incoming message */
1226 if ((fd2
= dup (qd
)) == -1) {
1231 /* now create a FILE pointer for it */
1232 if ((qfp
= fdopen (fd2
, "r")) == NULL
) {
1238 /* dup the fd for temporary file */
1239 if ((fd2
= dup (fd1
)) == -1) {
1245 /* now create a FILE pointer for it */
1246 if ((ffp
= fdopen (fd2
, "r+")) == NULL
) {
1254 * copy message into temporary file
1255 * and massage the headers. Save
1256 * a copy of the "From " line for later.
1258 while (fgets (buffer
, sizeof(buffer
), qfp
)) {
1261 if (has_prefix(buffer
, "From ")) {
1262 /* get copy of envelope information ("From " line) */
1263 envelope
= mh_xstrdup(buffer
);
1265 /* Put the delivery date in message */
1274 fputs (buffer
, ffp
);
1286 lseek (fd1
, (off_t
) 0, SEEK_SET
);
1298 * Trim strings for pretty printing of debugging output
1304 char buffer
[BUFSIZ
*4];
1310 /* copy string into temp buffer */
1311 strncpy (buffer
, cp
, sizeof(buffer
));
1314 /* skip over leading whitespace */
1315 while (isspace((unsigned char) *bp
))
1318 /* start at the end and zap trailing whitespace */
1319 for (sp
= bp
+ strlen(bp
) - 1; sp
>= bp
; sp
--) {
1320 if (isspace((unsigned char) *sp
))
1326 /* replace remaining whitespace with spaces */
1327 for (sp
= bp
; *sp
; sp
++)
1328 if (isspace((unsigned char) *sp
))
1331 /* now return a copy */
1332 return mh_xstrdup(bp
);
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 */
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
[BUFSIZ
], name
[NAMESZ
];
1414 m_getfld_state_t gstate
= 0;
1416 if ((fd1
= dup (fd
)) == -1)
1418 if (!(in
= fdopen (fd1
, "r"))) {
1425 int failed_to_lock
= 0;
1426 int bufsz
= sizeof buf
;
1427 state
= m_getfld (&gstate
, name
, buf
, &bufsz
, in
);
1431 /* Search for the message ID */
1432 if (strcasecmp (name
, "Message-ID")) {
1433 while (state
== FLDPLUS
) {
1435 state
= m_getfld (&gstate
, name
, buf
, &bufsz
, in
);
1440 cp
= mh_xstrdup(buf
);
1441 while (state
== FLDPLUS
) {
1443 state
= m_getfld (&gstate
, name
, buf
, &bufsz
, in
);
1446 key
.dptr
= trimcpy (cp
);
1447 key
.dsize
= strlen (key
.dptr
) + 1;
1451 if (!(db
= dbm_open (file
, O_RDWR
| O_CREAT
, 0600))) {
1452 advise (file
, "unable to perform dbm_open on");
1458 * Since it is difficult to portable lock a ndbm file,
1459 * we will open and lock the Maildelivery file instead.
1460 * This will fail if your Maildelivery file doesn't
1463 if ((lockfd
= lkopendata(file
, O_RDWR
, 0, &failed_to_lock
))
1465 advise (file
, "unable to perform file locking on");
1470 value
= dbm_fetch (db
, key
);
1473 verbose_printf ("Message-ID: %s\n already received on %s",
1477 value
.dptr
= ddate
+ sizeof("Delivery-Date:");
1478 value
.dsize
= strlen(value
.dptr
) + 1;
1479 if (dbm_store (db
, key
, value
, DBM_INSERT
))
1480 advise (file
, "possibly corrupt file");
1485 lkclosedata(lockfd
, file
);
1502 m_getfld_state_destroy (&gstate
);