]>
diplodocus.org Git - nmh/blob - uip/inc.c
1 /* inc.c -- incorporate messages from a maildrop into a folder
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.
10 * Thu Feb 12 21:00 CST 2015 Marcin Cieslak <saper@saper.info>
11 * Replaced setgid() calls with setegid() so that it works with dot
12 * locking on FreeBSD. setegid() should be supported on modern POSIX
15 * Revised: Sat Apr 14 17:08:17 PDT 1990 (marvit@hplabs)
16 * Added hpux hacks to set and reset gid to be "mail" as needed. The reset
17 * is necessary so inc'ed mail is the group of the inc'er, rather than
18 * "mail". We setgid to egid only when [un]locking the mail file. This
19 * is also a major security precaution which will not be explained here.
21 * Fri Feb 7 16:04:57 PST 1992 John Romine <bug-mh@ics.uci.edu>
22 * NB: I'm not 100% sure that this setgid stuff is secure even now.
24 * See the *GROUPPRIVS() macros later. I'm reasonably happy with the setgid
25 * attribute. Running setuid root is probably not a terribly good idea, though.
26 * -- Peter Maydell <pmaydell@chiark.greenend.org.uk>, 04/1998
28 * Peter Maydell's patch slightly modified for nmh 0.28-pre2.
29 * Ruud de Rooij <ruud@debian.org> Wed, 22 Jul 1998 13:24:22 +0200
36 #include <h/dropsbr.h>
38 #include <h/fmt_scan.h>
39 #include <h/scansbr.h>
40 #include <h/signals.h>
43 #include "sbr/lock_file.h"
44 #include "sbr/m_maildir.h"
45 #include "sbr/m_mktemp.h"
48 # define TLSminc(a) (a)
53 #define INC_SWITCHES \
54 X("audit audit-file", 0, AUDSW) \
55 X("noaudit", 0, NAUDSW) \
56 X("changecur", 0, CHGSW) \
57 X("nochangecur", 0, NCHGSW) \
58 X("file name", 0, FILESW) \
59 X("form formatfile", 0, FORMSW) \
60 X("format string", 5, FMTSW) \
61 X("host hostname", 0, HOSTSW) \
62 X("user username", 0, USERSW) \
63 X("port name/number", 0, PORTSW) \
64 X("silent", 0, SILSW) \
65 X("nosilent", 0, NSILSW) \
66 X("truncate", 0, TRNCSW) \
67 X("notruncate", 0, NTRNCSW) \
68 X("width columns", 0, WIDTHSW) \
69 X("version", 0, VERSIONSW) \
70 X("help", 0, HELPSW) \
71 X("snoop", 0, SNOOPSW) \
72 X("sasl", 0, SASLSW) \
73 X("nosasl", 0, NOSASLSW) \
74 X("saslmech", 0, SASLMECHSW) \
75 X("initialtls", TLSminc(-10), INITTLSSW) \
76 X("notls", TLSminc(-5), NOTLSSW) \
77 X("certverify", TLSminc(-10), CERTVERSW) \
78 X("nocertverify", TLSminc(-12), NOCERTVERSW) \
79 X("authservice", 0, AUTHSERVICESW) \
80 X("proxy command", 0, PROXYSW) \
82 #define X(sw, minchars, id) id,
83 DEFINE_SWITCH_ENUM(INC
);
86 #define X(sw, minchars, id) { sw, minchars, id },
87 DEFINE_SWITCH_ARRAY(INC
, switches
);
91 * flags for the mail source
96 static struct Maildir_entry
{
100 static int num_maildir_entries
= 0;
101 static int snoop
= 0;
108 extern char response
[];
110 /* This is an attempt to simplify things by putting all the
111 * privilege ops into macros.
112 * *GROUPPRIVS() is related to handling the setgid MAIL property,
113 * and only applies if MAILGROUP is defined.
114 * Basically, SAVEGROUPPRIVS() is called right at the top of main()
115 * to initialise things, and then DROPGROUPPRIVS() and GETGROUPPRIVS()
116 * do the obvious thing.
118 * There's probably a better implementation if we're allowed to use
119 * BSD-style setreuid() rather than using POSIX saved-ids.
120 * Anyway, if you're euid root it's a bit pointless to drop the group
123 * I'm pretty happy that the security is good provided we aren't setuid root.
124 * The only things we trust with group=mail privilege are lkfopen()
129 * For setting and returning to "mail" gid
132 static gid_t return_gid
;
133 #define TRYDROPGROUPPRIVS() DROPGROUPPRIVS()
134 #define DROPGROUPPRIVS() \
135 if (setegid(getgid()) != 0) { \
136 adios ("setegid", "unable to restore group to %ld", (long) getgid()); \
138 #define GETGROUPPRIVS() \
139 if (setegid(return_gid) != 0) { \
140 adios ("setegid", "unable to set group to %ld", (long) return_gid); \
142 #define SAVEGROUPPRIVS() return_gid = getegid()
144 /* define *GROUPPRIVS() as null; this avoids having lots of "#ifdef MAILGROUP"s */
145 #define TRYDROPGROUPPRIVS()
146 #define DROPGROUPPRIVS()
147 #define GETGROUPPRIVS()
148 #define SAVEGROUPPRIVS()
149 #endif /* not MAILGROUP */
151 /* these variables have to be globals so that done() can correctly clean up the lockfile */
153 static char *newmail
;
159 static int maildir_srt(const void *va
, const void *vb
) PURE
;
160 static void inc_done(int) NORETURN
;
161 static int pop_action(void *closure
, char *);
164 maildir_srt(const void *va
, const void *vb
)
166 const struct Maildir_entry
*a
= va
, *b
= vb
;
167 if (a
->mtime
> b
->mtime
)
169 if (a
->mtime
< b
->mtime
)
175 main (int argc
, char **argv
)
182 int hghnum
= 0, msgnum
= 0;
184 bool sasl
, tls
, noverify
;
185 int incerr
= 0; /* <0 if inc hits an error which means it should not truncate mailspool */
186 char *cp
, *maildir
= NULL
, *folder
= NULL
;
187 char *format
= NULL
, *form
= NULL
;
188 char *host
= NULL
, *port
= NULL
, *user
= NULL
, *proxy
= NULL
;
189 char *audfile
= NULL
, *from
= NULL
, *saslmech
= NULL
, *auth_svc
= NULL
;
190 char buf
[BUFSIZ
], **argp
, *nfs
, **arguments
;
191 struct msgs
*mp
= NULL
;
194 char b
[PATH_MAX
+ 1];
195 char *maildir_copy
= NULL
; /* copy of mail directory because the static gets overwritten */
198 char *MAILHOST_env_variable
;
201 /* absolutely the first thing we do is save our privileges,
202 * and drop them if we can.
207 if (nmh_init(argv
[0], 1)) { return 1; }
210 arguments
= getarguments (invo_name
, argc
, argv
, 1);
215 * use MAILHOST environment variable if present,
217 * If that fails, use the default (if any)
218 * provided by mts.conf in mts_init()
220 if ((MAILHOST_env_variable
= getenv("MAILHOST")) != NULL
)
221 pophost
= MAILHOST_env_variable
;
223 * If there is a valid "pophost" entry in mts.conf,
224 * then use it as the default host.
226 if (pophost
&& *pophost
)
230 chgflag
= noisy
= noverify
= true;
231 while ((cp
= *argp
++)) {
233 switch (smatch (++cp
, switches
)) {
235 ambigsw (cp
, switches
);
238 adios (NULL
, "-%s unknown", cp
);
241 snprintf (buf
, sizeof(buf
), "%s [+folder] [switches]", invo_name
);
242 print_help (buf
, switches
, 1);
245 print_version(invo_name
);
249 if (!(cp
= *argp
++) || *cp
== '-')
250 adios (NULL
, "missing argument to %s", argp
[-2]);
251 audfile
= getcpy (m_maildir (cp
));
265 * The flag `trnflag' has the value:
267 * 2 if -truncate is given
268 * 1 by default (truncating is default)
269 * 0 if -notruncate is given
279 if (!(cp
= *argp
++) || *cp
== '-')
280 adios (NULL
, "missing argument to %s", argp
[-2]);
281 from
= path (cp
, TFILE
);
284 * If the truncate file is in default state,
285 * change to not truncate.
299 if (!(form
= *argp
++) || *form
== '-')
300 adios (NULL
, "missing argument to %s", argp
[-2]);
304 if (!(format
= *argp
++) || *format
== '-')
305 adios (NULL
, "missing argument to %s", argp
[-2]);
310 if (!(cp
= *argp
++) || *cp
== '-')
311 adios (NULL
, "missing argument to %s", argp
[-2]);
316 if (!(host
= *argp
++) || *host
== '-')
317 adios (NULL
, "missing argument to %s", argp
[-2]);
321 if (!(port
= *argp
++) || *port
== '-')
322 adios (NULL
, "missing argument to %s", argp
[-2]);
326 if (!(user
= *argp
++) || *user
== '-')
327 adios (NULL
, "missing argument to %s", argp
[-2]);
342 if (!(saslmech
= *argp
++) || *saslmech
== '-')
343 adios (NULL
, "missing argument to %s", argp
[-2]);
364 if (!(auth_svc
= *argp
++) || *auth_svc
== '-')
365 adios (NULL
, "missing argument to %s", argp
[-2]);
367 adios (NULL
, "not built with OAuth support");
372 if (!(proxy
= *argp
++) || *proxy
== '-')
373 adios (NULL
, "missing argument to %s", argp
[-2]);
377 if (*cp
== '+' || *cp
== '@') {
379 adios (NULL
, "only one folder at a time!");
380 folder
= pluspath (cp
);
382 adios (NULL
, "usage: %s [+folder] [switches]", invo_name
);
386 /* NOTE: above this point you should use TRYDROPGROUPPRIVS(),
387 * not DROPGROUPPRIVS().
392 /* guarantee dropping group privileges; we might not have done so earlier */
395 /* Source of mail; -from overrides any -host. */
396 inc_type
= host
&& !from
? INC_POP
: INC_FILE
;
398 if (inc_type
== INC_POP
) {
399 /* Mail from a POP server. */
402 if (auth_svc
== NULL
) {
403 if (saslmech
&& ! strcasecmp(saslmech
, "xoauth2")) {
404 adios (NULL
, "must specify -authservice with -saslmech xoauth2");
408 adios (NULL
, "must specify -user with -saslmech xoauth2");
413 tlsflag
|= P_INITTLS
;
416 tlsflag
|= P_NOVERIFY
;
419 * initialize POP connection
421 if (pop_init (host
, port
, user
, proxy
, snoop
, sasl
, saslmech
,
422 tlsflag
, auth_svc
) == NOTOK
)
423 adios (NULL
, "%s", response
);
425 /* Check if there are any messages */
426 if (pop_stat (&nmsgs
, &nbytes
) == NOTOK
)
427 adios (NULL
, "%s", response
);
431 adios (NULL
, "no mail to incorporate");
434 } else if (inc_type
== INC_FILE
) {
435 /* Mail from a spool file, or Maildir. */
438 else if ((newmail
= getenv ("MAILDROP")) && *newmail
)
439 newmail
= m_mailpath (newmail
);
440 else if ((newmail
= context_find ("maildrop")) && *newmail
)
441 newmail
= m_mailpath (newmail
);
443 newmail
= concat (MAILDIR
, "/", MAILFIL
, NULL
);
445 if (stat (newmail
, &s1
) == NOTOK
|| s1
.st_size
== 0)
446 adios (NULL
, "no mail to incorporate");
447 if (s1
.st_mode
& S_IFDIR
) {
453 cp
= concat (newmail
, "/new", NULL
);
454 if ((md
= opendir(cp
)) == NULL
)
455 adios (NULL
, "unable to open %s", cp
);
456 while ((de
= readdir (md
)) != NULL
) {
457 if (de
->d_name
[0] == '.')
459 if (i
>= num_maildir_entries
) {
460 if ((Maildir
= realloc(Maildir
, sizeof(*Maildir
) * (2*i
+16))) == NULL
)
461 adios(NULL
, "not enough memory for %d messages", 2*i
+16);
462 num_maildir_entries
= 2*i
+16;
464 Maildir
[i
].filename
= concat (cp
, "/", de
->d_name
, NULL
);
465 if (stat(Maildir
[i
].filename
, &ms
) != 0)
466 adios (Maildir
[i
].filename
, "couldn't get delivery time");
467 Maildir
[i
].mtime
= ms
.st_mtime
;
472 cp
= concat (newmail
, "/cur", NULL
);
473 if ((md
= opendir(cp
)) == NULL
)
474 adios (NULL
, "unable to open %s", cp
);
475 while ((de
= readdir (md
)) != NULL
) {
476 if (de
->d_name
[0] == '.')
478 if (i
>= num_maildir_entries
) {
479 if ((Maildir
= realloc(Maildir
, sizeof(*Maildir
) * (2*i
+16))) == NULL
)
480 adios(NULL
, "not enough memory for %d messages", 2*i
+16);
481 num_maildir_entries
= 2*i
+16;
483 Maildir
[i
].filename
= concat (cp
, "/", de
->d_name
, NULL
);
484 if (stat(Maildir
[i
].filename
, &ms
) != 0)
485 adios (Maildir
[i
].filename
, "couldn't get delivery time");
486 Maildir
[i
].mtime
= ms
.st_mtime
;
492 adios (NULL
, "no mail to incorporate");
493 num_maildir_entries
= i
;
494 qsort (Maildir
, num_maildir_entries
, sizeof(*Maildir
), maildir_srt
);
497 cp
= mh_xstrdup(newmail
);
501 if (!context_find ("path"))
502 free (path ("./", TFOLDER
));
504 folder
= getfolder (0);
505 maildir
= m_maildir (folder
);
506 maildir_copy
= mh_xstrdup(maildir
);
508 if (!folder_exists(maildir
)) {
509 /* If the folder doesn't exist, and we're given the -silent flag,
513 create_folder(maildir
, 0, done
);
518 if (chdir (maildir
) == NOTOK
)
519 adios (maildir
, "unable to change directory to");
521 /* read folder and create message structure */
522 if (!(mp
= folder_read (folder
, 0)))
523 adios (NULL
, "unable to read folder %s", folder
);
525 if (inc_type
== INC_FILE
&& Maildir
== NULL
) {
526 /* Mail from a spool file. */
528 if (access (newmail
, W_OK
) != NOTOK
) {
531 SIGNAL (SIGHUP
, SIG_IGN
);
532 SIGNAL (SIGINT
, SIG_IGN
);
533 SIGNAL (SIGQUIT
, SIG_IGN
);
534 SIGNAL (SIGTERM
, SIG_IGN
);
537 GETGROUPPRIVS(); /* Reset gid to lock mail file */
538 in
= lkfopenspool (newmail
, "r");
541 adios (NULL
, "unable to lock and fopen %s", newmail
);
542 fstat (fileno(in
), &s1
);
545 if ((in
= fopen (newmail
, "r")) == NULL
)
546 adios (newmail
, "unable to read");
550 /* This shouldn't be necessary but it can't hurt. */
555 if ((i
= stat (audfile
, &st
)) == NOTOK
)
556 inform("Creating Receive-Audit: %s", audfile
);
557 if ((aud
= fopen (audfile
, "a")) == NULL
)
558 adios (audfile
, "unable to append to");
560 chmod (audfile
, m_gmprot ());
563 fprintf (aud
, "<<inc>> %s -ms %s\n", dtimenow(0), from
);
566 fprintf (aud
, "<<inc>> %s -host %s -user %s\n", dtimenow(0),
569 fprintf (aud
, "<<inc>> %s\n", dtimenow (0));
573 /* Get new format string */
574 nfs
= new_fs (form
, format
, FORMAT
);
577 printf ("Incorporating new mail into %s...\n\n", folder
);
583 * Get the mail from a POP server
585 if (inc_type
== INC_POP
) {
586 /* Mail from a POP server. */
590 hghnum
= msgnum
= mp
->hghmsg
;
591 for (i
= 1; i
<= nmsgs
; i
++) {
592 charstring_t scanl
= NULL
;
595 cp
= mh_xstrdup(m_name (msgnum
));
596 if ((pf
= fopen (cp
, "w+")) == NULL
)
597 adios (cp
, "unable to write");
598 chmod (cp
, m_gmprot ());
602 if (pop_retr(i
, pop_action
, &pc
) == NOTOK
)
603 adios (NULL
, "%s", response
);
606 adios (cp
, "write error on");
607 fseek (pf
, 0L, SEEK_SET
);
608 switch (incerr
= scan (pf
, msgnum
, 0, nfs
, width
,
609 msgnum
== mp
->hghmsg
+ 1 && chgflag
,
610 1, NULL
, pc
.written
, noisy
, &scanl
)) {
612 printf ("%*d empty\n", DMAXFOLDER
, msgnum
);
618 /* advise (cp, "unable to read"); already advised */
629 fputs (charstring_buffer (scanl
), aud
);
634 charstring_free (scanl
);
636 if (ferror(pf
) || fclose (pf
)) {
638 (void) m_unlink (cp
);
641 adios (cp
, "write error on");
645 if (trnflag
&& pop_dele (i
) == NOTOK
)
646 adios (NULL
, "%s", response
);
651 if (pop_quit () == NOTOK
)
652 adios (NULL
, "%s", response
);
654 } else if (inc_type
== INC_FILE
&& Maildir
== NULL
) {
655 /* Mail from a spool file. */
657 scan_detect_mbox_style (in
); /* the MAGIC invocation... */
658 hghnum
= msgnum
= mp
->hghmsg
;
660 charstring_t scanl
= NULL
;
662 /* create scanline for new message */
663 switch (incerr
= scan (in
, msgnum
+ 1, msgnum
+ 1, nfs
, width
,
664 msgnum
== hghnum
&& chgflag
, 1, NULL
, 0L, noisy
,
672 fputs ("inc aborted!\n", aud
);
673 inform("aborted!"); /* doesn't clean up locks! */
677 inform("BUG in %s, number out of range", invo_name
);
681 inform("BUG in %s, scan() botch (%d)", invo_name
, incerr
);
687 * Run the external program hook on the message.
690 (void)snprintf(b
, sizeof (b
), "%s/%d", maildir_copy
, msgnum
+ 1);
691 (void)ext_hook("add-hook", b
, NULL
);
694 fputs (charstring_buffer (scanl
), aud
);
701 charstring_free (scanl
);
703 /* If we get here there was some sort of error from scan(),
704 * so stop processing anything more from the spool.
710 /* Mail from Maildir. */
715 hghnum
= msgnum
= mp
->hghmsg
;
716 for (i
= 0; i
< num_maildir_entries
; i
++) {
717 charstring_t scanl
= NULL
;
721 sp
= Maildir
[i
].filename
;
722 cp
= mh_xstrdup(m_name (msgnum
));
724 if (!trnflag
|| link(sp
, cp
) == -1) {
725 static char buf
[65536];
728 if ((sf
= fopen (sp
, "r")) == NULL
)
729 adios (sp
, "unable to read for copy");
730 if ((pf
= fopen (cp
, "w+")) == NULL
)
731 adios (cp
, "unable to write for copy");
732 while ((nrd
= fread(buf
, 1, sizeof(buf
), sf
)) > 0)
733 if (fwrite(buf
, 1, nrd
, pf
) != nrd
)
735 if (ferror(sf
) || fflush(pf
) || ferror(pf
)) {
737 fclose(pf
); fclose(sf
); (void) m_unlink(cp
);
739 adios(cp
, "copy error %s -> %s", sp
, cp
);
744 if (pf
== NULL
&& (pf
= fopen (cp
, "r")) == NULL
)
745 adios (cp
, "not available");
746 chmod (cp
, m_gmprot ());
748 fseek (pf
, 0L, SEEK_SET
);
749 switch (incerr
= scan (pf
, msgnum
, 0, nfs
, width
,
750 msgnum
== mp
->hghmsg
+ 1 && chgflag
,
751 1, NULL
, 0, noisy
, &scanl
)) {
753 printf ("%*d empty\n", DMAXFOLDER
, msgnum
);
759 /* advise (cp, "unable to read"); already advised */
770 * Run the external program hook on the message.
773 (void)snprintf(b
, sizeof (b
), "%s/%d", maildir_copy
, msgnum
+ 1);
774 (void)ext_hook("add-hook", b
, NULL
);
777 fputs (charstring_buffer (scanl
), aud
);
782 charstring_free (scanl
);
784 if (ferror(pf
) || fclose (pf
)) {
786 (void) m_unlink (cp
);
788 adios (cp
, "write error on");
793 if (trnflag
&& m_unlink (sp
) == NOTOK
)
794 adios (sp
, "couldn't unlink");
795 free (sp
); /* Free Maildir[i]->filename */
799 free (Maildir
); /* From now on Maildir is just a flag - don't dref! */
804 if (incerr
< 0) { /* error */
806 GETGROUPPRIVS(); /* Be sure we can unlock mail file */
807 (void) lkfclosespool (in
, newmail
); in
= NULL
;
808 DROPGROUPPRIVS(); /* And then return us to normal privileges */
810 fclose (in
); in
= NULL
;
812 adios (NULL
, "failed");
821 if (inc_type
== INC_FILE
&& Maildir
== NULL
) {
822 /* Mail from a spool file; truncate it. */
825 if (stat (newmail
, &st
) != NOTOK
&& s1
.st_mtime
!= st
.st_mtime
)
826 inform("new messages have arrived!\007");
829 if ((newfd
= creat (newmail
, 0600)) != NOTOK
)
832 admonish (newmail
, "error zero'ing");
836 printf ("%s not zero'd\n", newmail
);
840 if (msgnum
== hghnum
) {
841 inform("no messages incorporated, continuing...");
844 * Lock the sequence file now, and loop to set the right flags
845 * in the folder structure
851 context_replace (pfolder
, folder
); /* update current folder */
853 if ((mp2
= folder_read(folder
, 1)) == NULL
) {
854 inform("Unable to reread folder %s, continuing...", folder
);
859 * Shouldn't happen, but just in case ...
862 if (msgnum
>= mp2
->hghoff
863 && !(mp2
= folder_realloc (mp2
, mp2
->lowoff
, msgnum
+ 1))) {
864 inform("unable to reallocate folder storage");
869 mp2
->curmsg
= hghnum
+ 1;
870 mp2
->hghmsg
= msgnum
;
872 if (mp2
->lowmsg
== 0)
874 if (chgflag
) /* sigh... */
875 seq_setcur (mp2
, mp2
->curmsg
);
877 for (i
= hghnum
+ 1; i
<= msgnum
; i
++) {
878 clear_msg_flags (mp2
, i
);
882 mp2
->msgflags
|= SEQMOD
;
883 seq_setunseen(mp2
, 0); /* Set the Unseen-Sequence */
884 seq_save(mp2
); /* Save the sequence file */
889 if (inc_type
== INC_FILE
&& Maildir
== NULL
) {
890 /* Mail from a spool file; unlock it. */
893 GETGROUPPRIVS(); /* Be sure we can unlock mail file */
894 (void) lkfclosespool (in
, newmail
); in
= NULL
;
895 DROPGROUPPRIVS(); /* And then return us to normal privileges */
897 fclose (in
); in
= NULL
;
901 context_save (); /* save the context file */
908 inc_done (int status
)
914 lkfclosespool(in
, newmail
);
921 pop_action(void *closure
, char *s
)
927 n
= fprintf(pc
->mailout
, "%s\n", s
);
930 pc
->written
+= n
; /* Count linefeed too. */