]>
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
97 static struct Maildir_entry
{
101 static int num_maildir_entries
= 0;
102 static int snoop
= 0;
104 extern char response
[];
109 static FILE *pf
= NULL
;
111 /* This is an attempt to simplify things by putting all the
112 * privilege ops into macros.
113 * *GROUPPRIVS() is related to handling the setgid MAIL property,
114 * and only applies if MAILGROUP is defined.
115 * Basically, SAVEGROUPPRIVS() is called right at the top of main()
116 * to initialise things, and then DROPGROUPPRIVS() and GETGROUPPRIVS()
117 * do the obvious thing.
119 * There's probably a better implementation if we're allowed to use
120 * BSD-style setreuid() rather than using POSIX saved-ids.
121 * Anyway, if you're euid root it's a bit pointless to drop the group
124 * I'm pretty happy that the security is good provided we aren't setuid root.
125 * The only things we trust with group=mail privilege are lkfopen()
130 * For setting and returning to "mail" gid
133 static gid_t return_gid
;
134 #define TRYDROPGROUPPRIVS() DROPGROUPPRIVS()
135 #define DROPGROUPPRIVS() \
136 if (setegid(getgid()) != 0) { \
137 adios ("setegid", "unable to restore group to %ld", (long) getgid()); \
139 #define GETGROUPPRIVS() \
140 if (setegid(return_gid) != 0) { \
141 adios ("setegid", "unable to set group to %ld", (long) return_gid); \
143 #define SAVEGROUPPRIVS() return_gid = getegid()
145 /* define *GROUPPRIVS() as null; this avoids having lots of "#ifdef MAILGROUP"s */
146 #define TRYDROPGROUPPRIVS()
147 #define DROPGROUPPRIVS()
148 #define GETGROUPPRIVS()
149 #define SAVEGROUPPRIVS()
150 #endif /* not MAILGROUP */
152 /* these variables have to be globals so that done() can correctly clean up the lockfile */
153 static int locked
= 0;
154 static char *newmail
;
160 static void inc_done(int) NORETURN
;
161 static int pop_action(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
)
181 int hghnum
= 0, msgnum
= 0;
182 bool sasl
, tls
, noverify
;
183 int incerr
= 0; /* <0 if inc hits an error which means it should not truncate mailspool */
184 char *cp
, *maildir
= NULL
, *folder
= NULL
;
185 char *format
= NULL
, *form
= NULL
;
186 char *host
= NULL
, *port
= NULL
, *user
= NULL
, *proxy
= NULL
;
187 char *audfile
= NULL
, *from
= NULL
, *saslmech
= NULL
, *auth_svc
= NULL
;
188 char buf
[BUFSIZ
], **argp
, *nfs
, **arguments
;
189 struct msgs
*mp
= NULL
;
192 char b
[PATH_MAX
+ 1];
193 char *maildir_copy
= NULL
; /* copy of mail directory because the static gets overwritten */
196 char *MAILHOST_env_variable
;
199 /* absolutely the first thing we do is save our privileges,
200 * and drop them if we can.
205 if (nmh_init(argv
[0], 1)) { return 1; }
208 arguments
= getarguments (invo_name
, argc
, argv
, 1);
213 * use MAILHOST environment variable if present,
215 * If that fails, use the default (if any)
216 * provided by mts.conf in mts_init()
218 if ((MAILHOST_env_variable
= getenv("MAILHOST")) != NULL
)
219 pophost
= MAILHOST_env_variable
;
221 * If there is a valid "pophost" entry in mts.conf,
222 * then use it as the default host.
224 if (pophost
&& *pophost
)
228 chgflag
= noisy
= noverify
= true;
229 while ((cp
= *argp
++)) {
231 switch (smatch (++cp
, switches
)) {
233 ambigsw (cp
, switches
);
236 adios (NULL
, "-%s unknown", cp
);
239 snprintf (buf
, sizeof(buf
), "%s [+folder] [switches]", invo_name
);
240 print_help (buf
, switches
, 1);
243 print_version(invo_name
);
247 if (!(cp
= *argp
++) || *cp
== '-')
248 adios (NULL
, "missing argument to %s", argp
[-2]);
249 audfile
= getcpy (m_maildir (cp
));
263 * The flag `trnflag' has the value:
265 * 2 if -truncate is given
266 * 1 by default (truncating is default)
267 * 0 if -notruncate is given
277 if (!(cp
= *argp
++) || *cp
== '-')
278 adios (NULL
, "missing argument to %s", argp
[-2]);
279 from
= path (cp
, TFILE
);
282 * If the truncate file is in default state,
283 * change to not truncate.
297 if (!(form
= *argp
++) || *form
== '-')
298 adios (NULL
, "missing argument to %s", argp
[-2]);
302 if (!(format
= *argp
++) || *format
== '-')
303 adios (NULL
, "missing argument to %s", argp
[-2]);
308 if (!(cp
= *argp
++) || *cp
== '-')
309 adios (NULL
, "missing argument to %s", argp
[-2]);
314 if (!(host
= *argp
++) || *host
== '-')
315 adios (NULL
, "missing argument to %s", argp
[-2]);
319 if (!(port
= *argp
++) || *port
== '-')
320 adios (NULL
, "missing argument to %s", argp
[-2]);
324 if (!(user
= *argp
++) || *user
== '-')
325 adios (NULL
, "missing argument to %s", argp
[-2]);
340 if (!(saslmech
= *argp
++) || *saslmech
== '-')
341 adios (NULL
, "missing argument to %s", argp
[-2]);
362 if (!(auth_svc
= *argp
++) || *auth_svc
== '-')
363 adios (NULL
, "missing argument to %s", argp
[-2]);
365 adios (NULL
, "not built with OAuth support");
370 if (!(proxy
= *argp
++) || *proxy
== '-')
371 adios (NULL
, "missing argument to %s", argp
[-2]);
375 if (*cp
== '+' || *cp
== '@') {
377 adios (NULL
, "only one folder at a time!");
379 folder
= pluspath (cp
);
381 adios (NULL
, "usage: %s [+folder] [switches]", invo_name
);
385 /* NOTE: above this point you should use TRYDROPGROUPPRIVS(),
386 * not DROPGROUPPRIVS().
391 /* guarantee dropping group privileges; we might not have done so earlier */
395 * Where are we getting the new mail?
405 * Are we getting the mail from
408 if (inc_type
== INC_POP
) {
411 if (auth_svc
== NULL
) {
412 if (saslmech
&& ! strcasecmp(saslmech
, "xoauth2")) {
413 adios (NULL
, "must specify -authservice with -saslmech xoauth2");
417 adios (NULL
, "must specify -user with -saslmech xoauth2");
422 tlsflag
|= P_INITTLS
;
425 tlsflag
|= P_NOVERIFY
;
428 * initialize POP connection
430 if (pop_init (host
, port
, user
, proxy
, snoop
, sasl
, saslmech
,
431 tlsflag
, auth_svc
) == NOTOK
)
432 adios (NULL
, "%s", response
);
434 /* Check if there are any messages */
435 if (pop_stat (&nmsgs
, &nbytes
) == NOTOK
)
436 adios (NULL
, "%s", response
);
440 adios (NULL
, "no mail to incorporate");
445 * We will get the mail from a file
446 * (typically the standard maildrop)
449 if (inc_type
== INC_FILE
) {
452 else if ((newmail
= getenv ("MAILDROP")) && *newmail
)
453 newmail
= m_mailpath (newmail
);
454 else if ((newmail
= context_find ("maildrop")) && *newmail
)
455 newmail
= m_mailpath (newmail
);
457 newmail
= concat (MAILDIR
, "/", MAILFIL
, NULL
);
459 if (stat (newmail
, &s1
) == NOTOK
|| s1
.st_size
== 0)
460 adios (NULL
, "no mail to incorporate");
461 if (s1
.st_mode
& S_IFDIR
) {
467 cp
= concat (newmail
, "/new", NULL
);
468 if ((md
= opendir(cp
)) == NULL
)
469 adios (NULL
, "unable to open %s", cp
);
470 while ((de
= readdir (md
)) != NULL
) {
471 if (de
->d_name
[0] == '.')
473 if (i
>= num_maildir_entries
) {
474 if ((Maildir
= realloc(Maildir
, sizeof(*Maildir
) * (2*i
+16))) == NULL
)
475 adios(NULL
, "not enough memory for %d messages", 2*i
+16);
476 num_maildir_entries
= 2*i
+16;
478 Maildir
[i
].filename
= concat (cp
, "/", de
->d_name
, NULL
);
479 if (stat(Maildir
[i
].filename
, &ms
) != 0)
480 adios (Maildir
[i
].filename
, "couldn't get delivery time");
481 Maildir
[i
].mtime
= ms
.st_mtime
;
486 cp
= concat (newmail
, "/cur", NULL
);
487 if ((md
= opendir(cp
)) == NULL
)
488 adios (NULL
, "unable to open %s", cp
);
489 while ((de
= readdir (md
)) != NULL
) {
490 if (de
->d_name
[0] == '.')
492 if (i
>= num_maildir_entries
) {
493 if ((Maildir
= realloc(Maildir
, sizeof(*Maildir
) * (2*i
+16))) == NULL
)
494 adios(NULL
, "not enough memory for %d messages", 2*i
+16);
495 num_maildir_entries
= 2*i
+16;
497 Maildir
[i
].filename
= concat (cp
, "/", de
->d_name
, NULL
);
498 if (stat(Maildir
[i
].filename
, &ms
) != 0)
499 adios (Maildir
[i
].filename
, "couldn't get delivery time");
500 Maildir
[i
].mtime
= ms
.st_mtime
;
506 adios (NULL
, "no mail to incorporate");
507 num_maildir_entries
= i
;
508 qsort (Maildir
, num_maildir_entries
, sizeof(*Maildir
), maildir_srt
);
511 cp
= mh_xstrdup(newmail
);
515 if (!context_find ("path"))
516 free (path ("./", TFOLDER
));
518 folder
= getfolder (0);
519 maildir
= m_maildir (folder
);
520 maildir_copy
= mh_xstrdup(maildir
);
522 if (!folder_exists(maildir
)) {
523 /* If the folder doesn't exist, and we're given the -silent flag,
527 create_folder(maildir
, 0, done
);
532 if (chdir (maildir
) == NOTOK
)
533 adios (maildir
, "unable to change directory to");
535 /* read folder and create message structure */
536 if (!(mp
= folder_read (folder
, 0)))
537 adios (NULL
, "unable to read folder %s", folder
);
539 if (inc_type
== INC_FILE
&& Maildir
== NULL
) {
540 if (access (newmail
, W_OK
) != NOTOK
) {
543 SIGNAL (SIGHUP
, SIG_IGN
);
544 SIGNAL (SIGINT
, SIG_IGN
);
545 SIGNAL (SIGQUIT
, SIG_IGN
);
546 SIGNAL (SIGTERM
, SIG_IGN
);
549 GETGROUPPRIVS(); /* Reset gid to lock mail file */
550 in
= lkfopenspool (newmail
, "r");
553 adios (NULL
, "unable to lock and fopen %s", newmail
);
554 fstat (fileno(in
), &s1
);
557 if ((in
= fopen (newmail
, "r")) == NULL
)
558 adios (newmail
, "unable to read");
562 /* This shouldn't be necessary but it can't hurt. */
567 if ((i
= stat (audfile
, &st
)) == NOTOK
)
568 inform("Creating Receive-Audit: %s", audfile
);
569 if ((aud
= fopen (audfile
, "a")) == NULL
)
570 adios (audfile
, "unable to append to");
572 chmod (audfile
, m_gmprot ());
575 fprintf (aud
, "<<inc>> %s -ms %s\n", dtimenow(0), from
);
578 fprintf (aud
, "<<inc>> %s -host %s -user %s\n", dtimenow(0),
581 fprintf (aud
, "<<inc>> %s\n", dtimenow (0));
585 /* Get new format string */
586 nfs
= new_fs (form
, format
, FORMAT
);
589 printf ("Incorporating new mail into %s...\n\n", folder
);
595 * Get the mail from a POP server
597 if (inc_type
== INC_POP
) {
600 hghnum
= msgnum
= mp
->hghmsg
;
601 for (i
= 1; i
<= nmsgs
; i
++) {
602 charstring_t scanl
= NULL
;
605 cp
= mh_xstrdup(m_name (msgnum
));
606 if ((pf
= fopen (cp
, "w+")) == NULL
)
607 adios (cp
, "unable to write");
608 chmod (cp
, m_gmprot ());
611 if (pop_retr (i
, pop_action
) == NOTOK
)
612 adios (NULL
, "%s", response
);
615 adios (cp
, "write error on");
616 fseek (pf
, 0L, SEEK_SET
);
617 switch (incerr
= scan (pf
, msgnum
, 0, nfs
, width
,
618 msgnum
== mp
->hghmsg
+ 1 && chgflag
,
619 1, NULL
, stop
- start
, noisy
, &scanl
)) {
621 printf ("%*d empty\n", DMAXFOLDER
, msgnum
);
627 /* advise (cp, "unable to read"); already advised */
638 fputs (charstring_buffer (scanl
), aud
);
643 charstring_free (scanl
);
645 if (ferror(pf
) || fclose (pf
)) {
647 (void) m_unlink (cp
);
650 adios (cp
, "write error on");
654 if (trnflag
&& pop_dele (i
) == NOTOK
)
655 adios (NULL
, "%s", response
);
660 if (pop_quit () == NOTOK
)
661 adios (NULL
, "%s", response
);
665 * Get the mail from file (usually mail spool)
667 if (inc_type
== INC_FILE
&& Maildir
== NULL
) {
668 scan_detect_mbox_style (in
); /* the MAGIC invocation... */
669 hghnum
= msgnum
= mp
->hghmsg
;
671 charstring_t scanl
= NULL
;
673 /* create scanline for new message */
674 switch (incerr
= scan (in
, msgnum
+ 1, msgnum
+ 1, nfs
, width
,
675 msgnum
== hghnum
&& chgflag
, 1, NULL
, 0L, noisy
,
683 fputs ("inc aborted!\n", aud
);
684 inform("aborted!"); /* doesn't clean up locks! */
688 inform("BUG in %s, number out of range", invo_name
);
692 inform("BUG in %s, scan() botch (%d)", invo_name
, incerr
);
698 * Run the external program hook on the message.
701 (void)snprintf(b
, sizeof (b
), "%s/%d", maildir_copy
, msgnum
+ 1);
702 (void)ext_hook("add-hook", b
, NULL
);
705 fputs (charstring_buffer (scanl
), aud
);
712 charstring_free (scanl
);
714 /* If we get here there was some sort of error from scan(),
715 * so stop processing anything more from the spool.
719 } else if (inc_type
== INC_FILE
) { /* Maildir inbox to process */
724 hghnum
= msgnum
= mp
->hghmsg
;
725 for (i
= 0; i
< num_maildir_entries
; i
++) {
726 charstring_t scanl
= NULL
;
730 sp
= Maildir
[i
].filename
;
731 cp
= mh_xstrdup(m_name (msgnum
));
733 if (!trnflag
|| link(sp
, cp
) == -1) {
734 static char buf
[65536];
737 if ((sf
= fopen (sp
, "r")) == NULL
)
738 adios (sp
, "unable to read for copy");
739 if ((pf
= fopen (cp
, "w+")) == NULL
)
740 adios (cp
, "unable to write for copy");
741 while ((nrd
= fread(buf
, 1, sizeof(buf
), sf
)) > 0)
742 if (fwrite(buf
, 1, nrd
, pf
) != nrd
)
744 if (ferror(sf
) || fflush(pf
) || ferror(pf
)) {
746 fclose(pf
); fclose(sf
); (void) m_unlink(cp
);
748 adios(cp
, "copy error %s -> %s", sp
, cp
);
753 if (pf
== NULL
&& (pf
= fopen (cp
, "r")) == NULL
)
754 adios (cp
, "not available");
755 chmod (cp
, m_gmprot ());
757 fseek (pf
, 0L, SEEK_SET
);
758 switch (incerr
= scan (pf
, msgnum
, 0, nfs
, width
,
759 msgnum
== mp
->hghmsg
+ 1 && chgflag
,
760 1, NULL
, stop
- start
, noisy
, &scanl
)) {
762 printf ("%*d empty\n", DMAXFOLDER
, msgnum
);
768 /* advise (cp, "unable to read"); already advised */
779 * Run the external program hook on the message.
782 (void)snprintf(b
, sizeof (b
), "%s/%d", maildir_copy
, msgnum
+ 1);
783 (void)ext_hook("add-hook", b
, NULL
);
786 fputs (charstring_buffer (scanl
), aud
);
791 charstring_free (scanl
);
793 if (ferror(pf
) || fclose (pf
)) {
795 (void) m_unlink (cp
);
797 adios (cp
, "write error on");
802 if (trnflag
&& m_unlink (sp
) == NOTOK
)
803 adios (sp
, "couldn't unlink");
804 free (sp
); /* Free Maildir[i]->filename */
808 free (Maildir
); /* From now on Maildir is just a flag - don't dref! */
813 if (incerr
< 0) { /* error */
815 GETGROUPPRIVS(); /* Be sure we can unlock mail file */
816 (void) lkfclosespool (in
, newmail
); in
= NULL
;
817 DROPGROUPPRIVS(); /* And then return us to normal privileges */
819 fclose (in
); in
= NULL
;
821 adios (NULL
, "failed");
831 * truncate file we are incorporating from
833 if (inc_type
== INC_FILE
&& Maildir
== NULL
) {
835 if (stat (newmail
, &st
) != NOTOK
&& s1
.st_mtime
!= st
.st_mtime
)
836 inform("new messages have arrived!\007");
839 if ((newfd
= creat (newmail
, 0600)) != NOTOK
)
842 admonish (newmail
, "error zero'ing");
846 printf ("%s not zero'd\n", newmail
);
850 if (msgnum
== hghnum
) {
851 inform("no messages incorporated, continuing...");
854 * Lock the sequence file now, and loop to set the right flags
855 * in the folder structure
861 context_replace (pfolder
, folder
); /* update current folder */
863 if ((mp2
= folder_read(folder
, 1)) == NULL
) {
864 inform("Unable to reread folder %s, continuing...", folder
);
869 * Shouldn't happen, but just in case ...
872 if (msgnum
>= mp2
->hghoff
873 && !(mp2
= folder_realloc (mp2
, mp2
->lowoff
, msgnum
+ 1))) {
874 inform("unable to reallocate folder storage");
879 mp2
->curmsg
= hghnum
+ 1;
880 mp2
->hghmsg
= msgnum
;
882 if (mp2
->lowmsg
== 0)
884 if (chgflag
) /* sigh... */
885 seq_setcur (mp2
, mp2
->curmsg
);
887 for (i
= hghnum
+ 1; i
<= msgnum
; i
++) {
888 clear_msg_flags (mp2
, i
);
892 mp2
->msgflags
|= SEQMOD
;
893 seq_setunseen(mp2
, 0); /* Set the Unseen-Sequence */
894 seq_save(mp2
); /* Save the sequence file */
900 * unlock the mail spool
902 if (inc_type
== INC_FILE
&& Maildir
== NULL
) {
904 GETGROUPPRIVS(); /* Be sure we can unlock mail file */
905 (void) lkfclosespool (in
, newmail
); in
= NULL
;
906 DROPGROUPPRIVS(); /* And then return us to normal privileges */
908 fclose (in
); in
= NULL
;
912 context_save (); /* save the context file */
919 inc_done (int status
)
925 lkfclosespool(in
, newmail
);
934 fprintf (pf
, "%s\n", s
);
935 stop
+= strlen (s
) + 1;
936 return 0; /* Is return value used? This was missing before 1999-07-15. */