]>
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
34 #include "sbr/fmt_new.h"
35 #include "sbr/dtime.h"
37 #include "sbr/m_name.h"
38 #include "sbr/m_gmprot.h"
39 #include "sbr/getarguments.h"
40 #include "sbr/concat.h"
41 #include "sbr/seq_setunseen.h"
42 #include "sbr/seq_setcur.h"
43 #include "sbr/seq_save.h"
44 #include "sbr/smatch.h"
45 #include "sbr/getfolder.h"
46 #include "sbr/ext_hook.h"
47 #include "sbr/folder_read.h"
48 #include "sbr/folder_realloc.h"
49 #include "sbr/folder_free.h"
50 #include "sbr/context_save.h"
51 #include "sbr/context_replace.h"
52 #include "sbr/context_find.h"
53 #include "sbr/ambigsw.h"
55 #include "sbr/print_version.h"
56 #include "sbr/print_help.h"
57 #include "sbr/error.h"
60 #include "h/dropsbr.h"
62 #include "h/fmt_scan.h"
63 #include "h/signals.h"
67 #include "sbr/lock_file.h"
68 #include "sbr/m_maildir.h"
69 #include "sbr/m_mktemp.h"
72 # define TLSminc(a) (a)
77 #define INC_SWITCHES \
78 X("audit audit-file", 0, AUDSW) \
79 X("noaudit", 0, NAUDSW) \
80 X("changecur", 0, CHGSW) \
81 X("nochangecur", 0, NCHGSW) \
82 X("file name", 0, FILESW) \
83 X("form formatfile", 0, FORMSW) \
84 X("format string", 5, FMTSW) \
85 X("host hostname", 0, HOSTSW) \
86 X("user username", 0, USERSW) \
87 X("port name/number", 0, PORTSW) \
88 X("silent", 0, SILSW) \
89 X("nosilent", 0, NSILSW) \
90 X("truncate", 0, TRNCSW) \
91 X("notruncate", 0, NTRNCSW) \
92 X("width columns", 0, WIDTHSW) \
93 X("version", 0, VERSIONSW) \
94 X("help", 0, HELPSW) \
95 X("snoop", 0, SNOOPSW) \
96 X("sasl", 0, SASLSW) \
97 X("nosasl", 0, NOSASLSW) \
98 X("saslmech", 0, SASLMECHSW) \
99 X("initialtls", TLSminc(-10), INITTLSSW) \
100 X("notls", TLSminc(-5), NOTLSSW) \
101 X("certverify", TLSminc(-10), CERTVERSW) \
102 X("nocertverify", TLSminc(-12), NOCERTVERSW) \
103 X("authservice", 0, AUTHSERVICESW) \
104 X("proxy command", 0, PROXYSW) \
106 #define X(sw, minchars, id) id,
107 DEFINE_SWITCH_ENUM(INC
);
110 #define X(sw, minchars, id) { sw, minchars, id },
111 DEFINE_SWITCH_ARRAY(INC
, switches
);
115 * flags for the mail source
120 static struct Maildir_entry
{
124 static int num_maildir_entries
= 0;
132 extern char response
[];
134 /* This is an attempt to simplify things by putting all the
135 * privilege ops into macros.
136 * *GROUPPRIVS() is related to handling the setgid MAIL property,
137 * and only applies if MAILGROUP is defined.
138 * Basically, SAVEGROUPPRIVS() is called right at the top of main()
139 * to initialise things, and then DROPGROUPPRIVS() and GETGROUPPRIVS()
140 * do the obvious thing.
142 * There's probably a better implementation if we're allowed to use
143 * BSD-style setreuid() rather than using POSIX saved-ids.
144 * Anyway, if you're euid root it's a bit pointless to drop the group
147 * I'm pretty happy that the security is good provided we aren't setuid root.
148 * The only things we trust with group=mail privilege are lkfopen()
153 * For setting and returning to "mail" gid
156 static gid_t return_gid
;
157 #define TRYDROPGROUPPRIVS() DROPGROUPPRIVS()
158 #define DROPGROUPPRIVS() \
159 if (setegid(getgid()) != 0) { \
160 adios ("setegid", "unable to restore group to %ld", (long) getgid()); \
162 #define GETGROUPPRIVS() \
163 if (setegid(return_gid) != 0) { \
164 adios ("setegid", "unable to set group to %ld", (long) return_gid); \
166 #define SAVEGROUPPRIVS() return_gid = getegid()
168 /* define *GROUPPRIVS() as null; this avoids having lots of "#ifdef MAILGROUP"s */
169 #define TRYDROPGROUPPRIVS()
170 #define DROPGROUPPRIVS()
171 #define GETGROUPPRIVS()
172 #define SAVEGROUPPRIVS()
173 #endif /* not MAILGROUP */
175 /* these variables have to be globals so that done() can correctly clean up the lockfile */
177 static char *newmail
;
183 static int maildir_srt(const void *va
, const void *vb
) PURE
;
184 static void inc_done(int) NORETURN
;
185 static int pop_action(void *closure
, char *);
188 maildir_srt(const void *va
, const void *vb
)
190 const struct Maildir_entry
*a
= va
, *b
= vb
;
191 if (a
->mtime
> b
->mtime
)
193 if (a
->mtime
< b
->mtime
)
199 main (int argc
, char **argv
)
206 int hghnum
= 0, msgnum
= 0;
208 bool sasl
, tls
, noverify
;
209 int incerr
= 0; /* <0 if inc hits an error which means it should not truncate mailspool */
210 char *cp
, *maildir
= NULL
, *folder
= NULL
;
211 char *format
= NULL
, *form
= NULL
;
212 char *host
= NULL
, *port
= NULL
, *user
= NULL
, *proxy
= NULL
;
213 char *audfile
= NULL
, *from
= NULL
, *saslmech
= NULL
, *auth_svc
= NULL
;
214 char buf
[BUFSIZ
], **argp
, *nfs
, **arguments
;
215 struct msgs
*mp
= NULL
;
218 char b
[PATH_MAX
+ 1];
219 char *maildir_copy
= NULL
; /* copy of mail directory because the static gets overwritten */
220 charstring_t scanl
= NULL
;
223 char *MAILHOST_env_variable
;
226 /* absolutely the first thing we do is save our privileges,
227 * and drop them if we can.
232 if (nmh_init(argv
[0], true, true)) { return 1; }
235 arguments
= getarguments (invo_name
, argc
, argv
, 1);
240 * use MAILHOST environment variable if present,
242 * If that fails, use the default (if any)
243 * provided by mts.conf in mts_init()
245 if ((MAILHOST_env_variable
= getenv("MAILHOST")) != NULL
)
246 pophost
= MAILHOST_env_variable
;
248 * If there is a valid "pophost" entry in mts.conf,
249 * then use it as the default host.
251 if (pophost
&& *pophost
)
255 chgflag
= noisy
= noverify
= true;
256 while ((cp
= *argp
++)) {
258 switch (smatch (++cp
, switches
)) {
260 ambigsw (cp
, switches
);
263 die("-%s unknown", cp
);
266 snprintf (buf
, sizeof(buf
), "%s [+folder] [switches]", invo_name
);
267 print_help (buf
, switches
, 1);
270 print_version(invo_name
);
274 if (!(cp
= *argp
++) || *cp
== '-')
275 die("missing argument to %s", argp
[-2]);
276 audfile
= mh_xstrdup(m_maildir(cp
));
290 * The flag `trnflag' has the value:
292 * 2 if -truncate is given
293 * 1 by default (truncating is default)
294 * 0 if -notruncate is given
304 if (!(cp
= *argp
++) || *cp
== '-')
305 die("missing argument to %s", argp
[-2]);
306 from
= path (cp
, TFILE
);
309 * If the truncate file is in default state,
310 * change to not truncate.
324 if (!(form
= *argp
++) || *form
== '-')
325 die("missing argument to %s", argp
[-2]);
329 if (!(format
= *argp
++) || *format
== '-')
330 die("missing argument to %s", argp
[-2]);
335 if (!(cp
= *argp
++) || *cp
== '-')
336 die("missing argument to %s", argp
[-2]);
341 if (!(host
= *argp
++) || *host
== '-')
342 die("missing argument to %s", argp
[-2]);
346 if (!(port
= *argp
++) || *port
== '-')
347 die("missing argument to %s", argp
[-2]);
351 if (!(user
= *argp
++) || *user
== '-')
352 die("missing argument to %s", argp
[-2]);
367 if (!(saslmech
= *argp
++) || *saslmech
== '-')
368 die("missing argument to %s", argp
[-2]);
389 if (!(auth_svc
= *argp
++) || *auth_svc
== '-')
390 die("missing argument to %s", argp
[-2]);
392 die("not built with OAuth support");
397 if (!(proxy
= *argp
++) || *proxy
== '-')
398 die("missing argument to %s", argp
[-2]);
402 if (*cp
== '+' || *cp
== '@') {
404 die("only one folder at a time!");
405 folder
= pluspath (cp
);
407 die("usage: %s [+folder] [switches]", invo_name
);
411 /* NOTE: above this point you should use TRYDROPGROUPPRIVS(),
412 * not DROPGROUPPRIVS().
417 /* guarantee dropping group privileges; we might not have done so earlier */
420 /* Source of mail; -from overrides any -host. */
421 inc_type
= host
&& !from
? INC_POP
: INC_FILE
;
423 if (inc_type
== INC_POP
) {
424 /* Mail from a POP server. */
427 if (auth_svc
== NULL
) {
428 if (saslmech
&& ! strcasecmp(saslmech
, "xoauth2")) {
429 die("must specify -authservice with -saslmech xoauth2");
433 die("must specify -user with -saslmech xoauth2");
438 tlsflag
|= P_INITTLS
;
441 tlsflag
|= P_NOVERIFY
;
444 * initialize POP connection
446 if (pop_init (host
, port
, user
, proxy
, snoop
, sasl
, saslmech
,
447 tlsflag
, auth_svc
) == NOTOK
)
450 /* Check if there are any messages */
451 if (pop_stat (&nmsgs
, &nbytes
) == NOTOK
)
456 die("no mail to incorporate");
459 } else if (inc_type
== INC_FILE
) {
460 /* Mail from a spool file, or Maildir. */
463 else if ((newmail
= getenv ("MAILDROP")) && *newmail
)
464 newmail
= m_mailpath (newmail
);
465 else if ((newmail
= context_find ("maildrop")) && *newmail
)
466 newmail
= m_mailpath (newmail
);
468 newmail
= concat (MAILDIR
, "/", MAILFIL
, NULL
);
470 if (stat (newmail
, &s1
) == NOTOK
|| s1
.st_size
== 0)
471 die("no mail to incorporate");
472 if (s1
.st_mode
& S_IFDIR
) {
478 cp
= concat (newmail
, "/new", NULL
);
479 if ((md
= opendir(cp
)) == NULL
)
480 die("unable to open %s", cp
);
481 while ((de
= readdir (md
)) != NULL
) {
482 if (de
->d_name
[0] == '.')
484 if (i
>= num_maildir_entries
) {
485 if ((Maildir
= realloc(Maildir
, sizeof(*Maildir
) * (2*i
+16))) == NULL
)
486 die("not enough memory for %d messages", 2*i
+16);
487 num_maildir_entries
= 2*i
+16;
489 Maildir
[i
].filename
= concat (cp
, "/", de
->d_name
, NULL
);
490 if (stat(Maildir
[i
].filename
, &ms
) != 0)
491 adios (Maildir
[i
].filename
, "couldn't get delivery time");
492 Maildir
[i
].mtime
= ms
.st_mtime
;
497 cp
= concat (newmail
, "/cur", NULL
);
498 if ((md
= opendir(cp
)) == NULL
)
499 die("unable to open %s", cp
);
500 while ((de
= readdir (md
)) != NULL
) {
501 if (de
->d_name
[0] == '.')
503 if (i
>= num_maildir_entries
) {
504 if ((Maildir
= realloc(Maildir
, sizeof(*Maildir
) * (2*i
+16))) == NULL
)
505 die("not enough memory for %d messages", 2*i
+16);
506 num_maildir_entries
= 2*i
+16;
508 Maildir
[i
].filename
= concat (cp
, "/", de
->d_name
, NULL
);
509 if (stat(Maildir
[i
].filename
, &ms
) != 0)
510 adios (Maildir
[i
].filename
, "couldn't get delivery time");
511 Maildir
[i
].mtime
= ms
.st_mtime
;
517 die("no mail to incorporate");
518 num_maildir_entries
= i
;
519 qsort (Maildir
, num_maildir_entries
, sizeof(*Maildir
), maildir_srt
);
522 cp
= mh_xstrdup(newmail
);
526 if (!context_find ("path"))
527 free (path ("./", TFOLDER
));
529 folder
= getfolder (0);
530 maildir
= m_maildir (folder
);
531 maildir_copy
= mh_xstrdup(maildir
);
533 if (!folder_exists(maildir
)) {
534 /* If the folder doesn't exist, and we're given the -silent flag,
538 create_folder(maildir
, 0, done
);
543 if (chdir (maildir
) == NOTOK
)
544 adios (maildir
, "unable to change directory to");
546 /* read folder and create message structure */
547 if (!(mp
= folder_read (folder
, 0)))
548 die("unable to read folder %s", folder
);
550 if (inc_type
== INC_FILE
&& Maildir
== NULL
) {
551 /* Mail from a spool file. */
553 if (access (newmail
, W_OK
) != NOTOK
) {
556 SIGNAL (SIGHUP
, SIG_IGN
);
557 SIGNAL (SIGINT
, SIG_IGN
);
558 SIGNAL (SIGQUIT
, SIG_IGN
);
559 SIGNAL (SIGTERM
, SIG_IGN
);
562 GETGROUPPRIVS(); /* Reset gid to lock mail file */
563 in
= lkfopenspool (newmail
, "r");
566 die("unable to lock and fopen %s", newmail
);
567 fstat (fileno(in
), &s1
);
570 if ((in
= fopen (newmail
, "r")) == NULL
)
571 adios (newmail
, "unable to read");
575 /* This shouldn't be necessary but it can't hurt. */
580 if ((i
= stat (audfile
, &st
)) == NOTOK
)
581 inform("Creating Receive-Audit: %s", audfile
);
582 if ((aud
= fopen (audfile
, "a")) == NULL
)
583 adios (audfile
, "unable to append to");
585 chmod (audfile
, m_gmprot ());
588 fprintf (aud
, "<<inc>> %s -ms %s\n", dtimenow(0), from
);
591 fprintf (aud
, "<<inc>> %s -host %s -user %s\n", dtimenow(0),
594 fprintf (aud
, "<<inc>> %s\n", dtimenow (0));
598 /* Get new format string */
599 nfs
= new_fs (form
, format
, FORMAT
);
602 printf ("Incorporating new mail into %s...\n\n", folder
);
608 * Get the mail from a POP server
610 if (inc_type
== INC_POP
) {
611 /* Mail from a POP server. */
615 hghnum
= msgnum
= mp
->hghmsg
;
616 for (i
= 1; i
<= nmsgs
; i
++) {
619 cp
= mh_xstrdup(m_name (msgnum
));
620 if ((pf
= fopen (cp
, "w+")) == NULL
)
621 adios (cp
, "unable to write");
622 chmod (cp
, m_gmprot ());
626 if (pop_retr(i
, pop_action
, &pc
) == NOTOK
)
630 adios (cp
, "write error on");
631 fseek (pf
, 0L, SEEK_SET
);
632 switch (incerr
= scan (pf
, msgnum
, 0, nfs
, width
,
633 msgnum
== mp
->hghmsg
+ 1 && chgflag
,
634 1, NULL
, pc
.written
, noisy
, &scanl
)) {
636 printf ("%*d empty\n", DMAXFOLDER
, msgnum
);
642 /* advise (cp, "unable to read"); already advised */
653 fputs (charstring_buffer (scanl
), aud
);
660 charstring_clear (scanl
);
662 if (ferror(pf
) || fclose (pf
)) {
664 (void) m_unlink (cp
);
667 adios (cp
, "write error on");
671 if (trnflag
&& pop_dele (i
) == NOTOK
)
677 charstring_free (scanl
);
680 if (pop_quit () == NOTOK
)
683 } else if (inc_type
== INC_FILE
&& Maildir
== NULL
) {
684 /* Mail from a spool file. */
686 scan_detect_mbox_style (in
); /* the MAGIC invocation... */
687 hghnum
= msgnum
= mp
->hghmsg
;
689 /* create scanline for new message */
690 switch (incerr
= scan (in
, msgnum
+ 1, msgnum
+ 1, nfs
, width
,
691 msgnum
== hghnum
&& chgflag
, 1, NULL
, 0L, noisy
,
699 fputs ("inc aborted!\n", aud
);
700 inform("aborted!"); /* doesn't clean up locks! */
704 inform("BUG in %s, number out of range", invo_name
);
708 inform("BUG in %s, scan() botch (%d)", invo_name
, incerr
);
714 * Run the external program hook on the message.
717 (void)snprintf(b
, sizeof (b
), "%s/%d", maildir_copy
, msgnum
+ 1);
718 (void)ext_hook("add-hook", b
, NULL
);
721 fputs (charstring_buffer (scanl
), aud
);
725 charstring_clear (scanl
);
730 /* If we get here there was some sort of error from scan(),
731 * so stop processing anything more from the spool.
735 charstring_free (scanl
);
739 /* Mail from Maildir. */
744 hghnum
= msgnum
= mp
->hghmsg
;
745 for (i
= 0; i
< num_maildir_entries
; i
++) {
748 sp
= Maildir
[i
].filename
;
749 cp
= mh_xstrdup(m_name (msgnum
));
751 if (!trnflag
|| link(sp
, cp
) == -1) {
752 static char buf
[65536];
755 if ((sf
= fopen (sp
, "r")) == NULL
)
756 adios (sp
, "unable to read for copy");
757 if ((pf
= fopen (cp
, "w+")) == NULL
)
758 adios (cp
, "unable to write for copy");
759 while ((nrd
= fread(buf
, 1, sizeof(buf
), sf
)) > 0)
760 if (fwrite(buf
, 1, nrd
, pf
) != nrd
)
762 if (ferror(sf
) || fflush(pf
) || ferror(pf
)) {
764 fclose(pf
); fclose(sf
); (void) m_unlink(cp
);
766 adios(cp
, "copy error %s -> %s", sp
, cp
);
771 if (pf
== NULL
&& (pf
= fopen (cp
, "r")) == NULL
)
772 adios (cp
, "not available");
773 chmod (cp
, m_gmprot ());
775 fseek (pf
, 0L, SEEK_SET
);
776 switch (incerr
= scan (pf
, msgnum
, 0, nfs
, width
,
777 msgnum
== mp
->hghmsg
+ 1 && chgflag
,
778 1, NULL
, 0, noisy
, &scanl
)) {
780 printf ("%*d empty\n", DMAXFOLDER
, msgnum
);
786 /* advise (cp, "unable to read"); already advised */
797 * Run the external program hook on the message.
800 (void)snprintf(b
, sizeof (b
), "%s/%d", maildir_copy
, msgnum
+ 1);
801 (void)ext_hook("add-hook", b
, NULL
);
804 fputs (charstring_buffer (scanl
), aud
);
809 charstring_clear (scanl
);
811 if (ferror(pf
) || fclose (pf
)) {
813 (void) m_unlink (cp
);
815 adios (cp
, "write error on");
820 if (trnflag
&& m_unlink (sp
) == NOTOK
)
821 adios (sp
, "couldn't unlink");
822 free (sp
); /* Free Maildir[i]->filename */
826 free (Maildir
); /* From now on Maildir is just a flag - don't dref! */
827 charstring_free (scanl
);
833 if (incerr
< 0) { /* error */
835 GETGROUPPRIVS(); /* Be sure we can unlock mail file */
836 (void) lkfclosespool (in
, newmail
); in
= NULL
;
837 DROPGROUPPRIVS(); /* And then return us to normal privileges */
839 fclose (in
); in
= NULL
;
850 if (inc_type
== INC_FILE
&& Maildir
== NULL
) {
851 /* Mail from a spool file; truncate it. */
854 if (stat (newmail
, &st
) != NOTOK
&& s1
.st_mtime
!= st
.st_mtime
)
855 inform("new messages have arrived!\007");
858 if ((newfd
= creat (newmail
, 0600)) != NOTOK
)
861 admonish (newmail
, "error zero'ing");
865 printf ("%s not zero'd\n", newmail
);
869 if (msgnum
== hghnum
) {
870 inform("no messages incorporated, continuing...");
873 * Lock the sequence file now, and loop to set the right flags
874 * in the folder structure
880 context_replace (pfolder
, folder
); /* update current folder */
882 if ((mp2
= folder_read(folder
, 1)) == NULL
) {
883 inform("Unable to reread folder %s, continuing...", folder
);
888 * Shouldn't happen, but just in case ...
891 if (msgnum
>= mp2
->hghoff
892 && !(mp2
= folder_realloc (mp2
, mp2
->lowoff
, msgnum
+ 1))) {
893 inform("unable to reallocate folder storage");
898 mp2
->curmsg
= hghnum
+ 1;
899 mp2
->hghmsg
= msgnum
;
901 if (mp2
->lowmsg
== 0)
903 if (chgflag
) /* sigh... */
904 seq_setcur (mp2
, mp2
->curmsg
);
906 for (i
= hghnum
+ 1; i
<= msgnum
; i
++) {
907 clear_msg_flags (mp2
, i
);
911 mp2
->msgflags
|= SEQMOD
;
912 seq_setunseen(mp2
, 0); /* Set the Unseen-Sequence */
913 seq_save(mp2
); /* Save the sequence file */
918 if (inc_type
== INC_FILE
&& Maildir
== NULL
) {
919 /* Mail from a spool file; unlock it. */
922 GETGROUPPRIVS(); /* Be sure we can unlock mail file */
923 (void) lkfclosespool (in
, newmail
); in
= NULL
;
924 DROPGROUPPRIVS(); /* And then return us to normal privileges */
926 fclose (in
); in
= NULL
;
930 context_save (); /* save the context file */
937 inc_done (int status
)
943 lkfclosespool(in
, newmail
);
950 pop_action(void *closure
, char *s
)
956 n
= fprintf(pc
->mailout
, "%s\n", s
);
959 pc
->written
+= n
; /* Count linefeed too. */