]>
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/dtime.h"
36 #include "sbr/m_name.h"
37 #include "sbr/m_gmprot.h"
38 #include "sbr/getarguments.h"
39 #include "sbr/concat.h"
40 #include "sbr/seq_setunseen.h"
41 #include "sbr/seq_setcur.h"
42 #include "sbr/seq_save.h"
43 #include "sbr/smatch.h"
44 #include "sbr/getfolder.h"
45 #include "sbr/ext_hook.h"
46 #include "sbr/folder_read.h"
47 #include "sbr/folder_realloc.h"
48 #include "sbr/folder_free.h"
49 #include "sbr/context_save.h"
50 #include "sbr/context_replace.h"
51 #include "sbr/context_find.h"
52 #include "sbr/ambigsw.h"
54 #include "sbr/print_version.h"
55 #include "sbr/print_help.h"
56 #include "sbr/error.h"
59 #include "h/dropsbr.h"
61 #include "h/fmt_scan.h"
62 #include "h/signals.h"
66 #include "sbr/lock_file.h"
67 #include "sbr/m_maildir.h"
68 #include "sbr/m_mktemp.h"
71 # define TLSminc(a) (a)
76 #define INC_SWITCHES \
77 X("audit audit-file", 0, AUDSW) \
78 X("noaudit", 0, NAUDSW) \
79 X("changecur", 0, CHGSW) \
80 X("nochangecur", 0, NCHGSW) \
81 X("file name", 0, FILESW) \
82 X("form formatfile", 0, FORMSW) \
83 X("format string", 5, FMTSW) \
84 X("host hostname", 0, HOSTSW) \
85 X("user username", 0, USERSW) \
86 X("port name/number", 0, PORTSW) \
87 X("silent", 0, SILSW) \
88 X("nosilent", 0, NSILSW) \
89 X("truncate", 0, TRNCSW) \
90 X("notruncate", 0, NTRNCSW) \
91 X("width columns", 0, WIDTHSW) \
92 X("version", 0, VERSIONSW) \
93 X("help", 0, HELPSW) \
94 X("snoop", 0, SNOOPSW) \
95 X("sasl", 0, SASLSW) \
96 X("nosasl", 0, NOSASLSW) \
97 X("saslmech", 0, SASLMECHSW) \
98 X("initialtls", TLSminc(-10), INITTLSSW) \
99 X("notls", TLSminc(-5), NOTLSSW) \
100 X("certverify", TLSminc(-10), CERTVERSW) \
101 X("nocertverify", TLSminc(-12), NOCERTVERSW) \
102 X("authservice", 0, AUTHSERVICESW) \
103 X("proxy command", 0, PROXYSW) \
105 #define X(sw, minchars, id) id,
106 DEFINE_SWITCH_ENUM(INC
);
109 #define X(sw, minchars, id) { sw, minchars, id },
110 DEFINE_SWITCH_ARRAY(INC
, switches
);
114 * flags for the mail source
119 static struct Maildir_entry
{
123 static int num_maildir_entries
= 0;
131 extern char response
[];
133 /* This is an attempt to simplify things by putting all the
134 * privilege ops into macros.
135 * *GROUPPRIVS() is related to handling the setgid MAIL property,
136 * and only applies if MAILGROUP is defined.
137 * Basically, SAVEGROUPPRIVS() is called right at the top of main()
138 * to initialise things, and then DROPGROUPPRIVS() and GETGROUPPRIVS()
139 * do the obvious thing.
141 * There's probably a better implementation if we're allowed to use
142 * BSD-style setreuid() rather than using POSIX saved-ids.
143 * Anyway, if you're euid root it's a bit pointless to drop the group
146 * I'm pretty happy that the security is good provided we aren't setuid root.
147 * The only things we trust with group=mail privilege are lkfopen()
152 * For setting and returning to "mail" gid
155 static gid_t return_gid
;
156 #define TRYDROPGROUPPRIVS() DROPGROUPPRIVS()
157 #define DROPGROUPPRIVS() \
158 if (setegid(getgid()) != 0) { \
159 adios ("setegid", "unable to restore group to %ld", (long) getgid()); \
161 #define GETGROUPPRIVS() \
162 if (setegid(return_gid) != 0) { \
163 adios ("setegid", "unable to set group to %ld", (long) return_gid); \
165 #define SAVEGROUPPRIVS() return_gid = getegid()
167 /* define *GROUPPRIVS() as null; this avoids having lots of "#ifdef MAILGROUP"s */
168 #define TRYDROPGROUPPRIVS()
169 #define DROPGROUPPRIVS()
170 #define GETGROUPPRIVS()
171 #define SAVEGROUPPRIVS()
172 #endif /* not MAILGROUP */
174 /* these variables have to be globals so that done() can correctly clean up the lockfile */
176 static char *newmail
;
182 static int maildir_srt(const void *va
, const void *vb
) PURE
;
183 static void inc_done(int) NORETURN
;
184 static int pop_action(void *closure
, char *);
187 maildir_srt(const void *va
, const void *vb
)
189 const struct Maildir_entry
*a
= va
, *b
= vb
;
190 if (a
->mtime
> b
->mtime
)
192 if (a
->mtime
< b
->mtime
)
198 main (int argc
, char **argv
)
205 int hghnum
= 0, msgnum
= 0;
207 bool sasl
, tls
, noverify
;
208 int incerr
= 0; /* <0 if inc hits an error which means it should not truncate mailspool */
209 char *cp
, *maildir
= NULL
, *folder
= NULL
;
210 char *format
= NULL
, *form
= NULL
;
211 char *host
= NULL
, *port
= NULL
, *user
= NULL
, *proxy
= NULL
;
212 char *audfile
= NULL
, *from
= NULL
, *saslmech
= NULL
, *auth_svc
= NULL
;
213 char buf
[BUFSIZ
], **argp
, *nfs
, **arguments
;
214 struct msgs
*mp
= NULL
;
217 char b
[PATH_MAX
+ 1];
218 char *maildir_copy
= NULL
; /* copy of mail directory because the static gets overwritten */
221 char *MAILHOST_env_variable
;
224 /* absolutely the first thing we do is save our privileges,
225 * and drop them if we can.
230 if (nmh_init(argv
[0], true, true)) { return 1; }
233 arguments
= getarguments (invo_name
, argc
, argv
, 1);
238 * use MAILHOST environment variable if present,
240 * If that fails, use the default (if any)
241 * provided by mts.conf in mts_init()
243 if ((MAILHOST_env_variable
= getenv("MAILHOST")) != NULL
)
244 pophost
= MAILHOST_env_variable
;
246 * If there is a valid "pophost" entry in mts.conf,
247 * then use it as the default host.
249 if (pophost
&& *pophost
)
253 chgflag
= noisy
= noverify
= true;
254 while ((cp
= *argp
++)) {
256 switch (smatch (++cp
, switches
)) {
258 ambigsw (cp
, switches
);
261 die("-%s unknown", cp
);
264 snprintf (buf
, sizeof(buf
), "%s [+folder] [switches]", invo_name
);
265 print_help (buf
, switches
, 1);
268 print_version(invo_name
);
272 if (!(cp
= *argp
++) || *cp
== '-')
273 die("missing argument to %s", argp
[-2]);
274 audfile
= mh_xstrdup(m_maildir(cp
));
288 * The flag `trnflag' has the value:
290 * 2 if -truncate is given
291 * 1 by default (truncating is default)
292 * 0 if -notruncate is given
302 if (!(cp
= *argp
++) || *cp
== '-')
303 die("missing argument to %s", argp
[-2]);
304 from
= path (cp
, TFILE
);
307 * If the truncate file is in default state,
308 * change to not truncate.
322 if (!(form
= *argp
++) || *form
== '-')
323 die("missing argument to %s", argp
[-2]);
327 if (!(format
= *argp
++) || *format
== '-')
328 die("missing argument to %s", argp
[-2]);
333 if (!(cp
= *argp
++) || *cp
== '-')
334 die("missing argument to %s", argp
[-2]);
339 if (!(host
= *argp
++) || *host
== '-')
340 die("missing argument to %s", argp
[-2]);
344 if (!(port
= *argp
++) || *port
== '-')
345 die("missing argument to %s", argp
[-2]);
349 if (!(user
= *argp
++) || *user
== '-')
350 die("missing argument to %s", argp
[-2]);
365 if (!(saslmech
= *argp
++) || *saslmech
== '-')
366 die("missing argument to %s", argp
[-2]);
387 if (!(auth_svc
= *argp
++) || *auth_svc
== '-')
388 die("missing argument to %s", argp
[-2]);
390 die("not built with OAuth support");
395 if (!(proxy
= *argp
++) || *proxy
== '-')
396 die("missing argument to %s", argp
[-2]);
400 if (*cp
== '+' || *cp
== '@') {
402 die("only one folder at a time!");
403 folder
= pluspath (cp
);
405 die("usage: %s [+folder] [switches]", invo_name
);
409 /* NOTE: above this point you should use TRYDROPGROUPPRIVS(),
410 * not DROPGROUPPRIVS().
415 /* guarantee dropping group privileges; we might not have done so earlier */
418 /* Source of mail; -from overrides any -host. */
419 inc_type
= host
&& !from
? INC_POP
: INC_FILE
;
421 if (inc_type
== INC_POP
) {
422 /* Mail from a POP server. */
425 if (auth_svc
== NULL
) {
426 if (saslmech
&& ! strcasecmp(saslmech
, "xoauth2")) {
427 die("must specify -authservice with -saslmech xoauth2");
431 die("must specify -user with -saslmech xoauth2");
436 tlsflag
|= P_INITTLS
;
439 tlsflag
|= P_NOVERIFY
;
442 * initialize POP connection
444 if (pop_init (host
, port
, user
, proxy
, snoop
, sasl
, saslmech
,
445 tlsflag
, auth_svc
) == NOTOK
)
448 /* Check if there are any messages */
449 if (pop_stat (&nmsgs
, &nbytes
) == NOTOK
)
454 die("no mail to incorporate");
457 } else if (inc_type
== INC_FILE
) {
458 /* Mail from a spool file, or Maildir. */
461 else if ((newmail
= getenv ("MAILDROP")) && *newmail
)
462 newmail
= m_mailpath (newmail
);
463 else if ((newmail
= context_find ("maildrop")) && *newmail
)
464 newmail
= m_mailpath (newmail
);
466 newmail
= concat (MAILDIR
, "/", MAILFIL
, NULL
);
468 if (stat (newmail
, &s1
) == NOTOK
|| s1
.st_size
== 0)
469 die("no mail to incorporate");
470 if (s1
.st_mode
& S_IFDIR
) {
476 cp
= concat (newmail
, "/new", NULL
);
477 if ((md
= opendir(cp
)) == NULL
)
478 die("unable to open %s", cp
);
479 while ((de
= readdir (md
)) != NULL
) {
480 if (de
->d_name
[0] == '.')
482 if (i
>= num_maildir_entries
) {
483 if ((Maildir
= realloc(Maildir
, sizeof(*Maildir
) * (2*i
+16))) == NULL
)
484 die("not enough memory for %d messages", 2*i
+16);
485 num_maildir_entries
= 2*i
+16;
487 Maildir
[i
].filename
= concat (cp
, "/", de
->d_name
, NULL
);
488 if (stat(Maildir
[i
].filename
, &ms
) != 0)
489 adios (Maildir
[i
].filename
, "couldn't get delivery time");
490 Maildir
[i
].mtime
= ms
.st_mtime
;
495 cp
= concat (newmail
, "/cur", NULL
);
496 if ((md
= opendir(cp
)) == NULL
)
497 die("unable to open %s", cp
);
498 while ((de
= readdir (md
)) != NULL
) {
499 if (de
->d_name
[0] == '.')
501 if (i
>= num_maildir_entries
) {
502 if ((Maildir
= realloc(Maildir
, sizeof(*Maildir
) * (2*i
+16))) == NULL
)
503 die("not enough memory for %d messages", 2*i
+16);
504 num_maildir_entries
= 2*i
+16;
506 Maildir
[i
].filename
= concat (cp
, "/", de
->d_name
, NULL
);
507 if (stat(Maildir
[i
].filename
, &ms
) != 0)
508 adios (Maildir
[i
].filename
, "couldn't get delivery time");
509 Maildir
[i
].mtime
= ms
.st_mtime
;
515 die("no mail to incorporate");
516 num_maildir_entries
= i
;
517 qsort (Maildir
, num_maildir_entries
, sizeof(*Maildir
), maildir_srt
);
520 cp
= mh_xstrdup(newmail
);
524 if (!context_find ("path"))
525 free (path ("./", TFOLDER
));
527 folder
= getfolder (0);
528 maildir
= m_maildir (folder
);
529 maildir_copy
= mh_xstrdup(maildir
);
531 if (!folder_exists(maildir
)) {
532 /* If the folder doesn't exist, and we're given the -silent flag,
536 create_folder(maildir
, 0, done
);
541 if (chdir (maildir
) == NOTOK
)
542 adios (maildir
, "unable to change directory to");
544 /* read folder and create message structure */
545 if (!(mp
= folder_read (folder
, 0)))
546 die("unable to read folder %s", folder
);
548 if (inc_type
== INC_FILE
&& Maildir
== NULL
) {
549 /* Mail from a spool file. */
551 if (access (newmail
, W_OK
) != NOTOK
) {
554 SIGNAL (SIGHUP
, SIG_IGN
);
555 SIGNAL (SIGINT
, SIG_IGN
);
556 SIGNAL (SIGQUIT
, SIG_IGN
);
557 SIGNAL (SIGTERM
, SIG_IGN
);
560 GETGROUPPRIVS(); /* Reset gid to lock mail file */
561 in
= lkfopenspool (newmail
, "r");
564 die("unable to lock and fopen %s", newmail
);
565 fstat (fileno(in
), &s1
);
568 if ((in
= fopen (newmail
, "r")) == NULL
)
569 adios (newmail
, "unable to read");
573 /* This shouldn't be necessary but it can't hurt. */
578 if ((i
= stat (audfile
, &st
)) == NOTOK
)
579 inform("Creating Receive-Audit: %s", audfile
);
580 if ((aud
= fopen (audfile
, "a")) == NULL
)
581 adios (audfile
, "unable to append to");
583 chmod (audfile
, m_gmprot ());
586 fprintf (aud
, "<<inc>> %s -ms %s\n", dtimenow(0), from
);
589 fprintf (aud
, "<<inc>> %s -host %s -user %s\n", dtimenow(0),
592 fprintf (aud
, "<<inc>> %s\n", dtimenow (0));
596 /* Get new format string */
597 nfs
= new_fs (form
, format
, FORMAT
);
600 printf ("Incorporating new mail into %s...\n\n", folder
);
606 * Get the mail from a POP server
608 if (inc_type
== INC_POP
) {
609 /* Mail from a POP server. */
613 hghnum
= msgnum
= mp
->hghmsg
;
614 for (i
= 1; i
<= nmsgs
; i
++) {
615 charstring_t scanl
= NULL
;
618 cp
= mh_xstrdup(m_name (msgnum
));
619 if ((pf
= fopen (cp
, "w+")) == NULL
)
620 adios (cp
, "unable to write");
621 chmod (cp
, m_gmprot ());
625 if (pop_retr(i
, pop_action
, &pc
) == NOTOK
)
629 adios (cp
, "write error on");
630 fseek (pf
, 0L, SEEK_SET
);
631 switch (incerr
= scan (pf
, msgnum
, 0, nfs
, width
,
632 msgnum
== mp
->hghmsg
+ 1 && chgflag
,
633 1, NULL
, pc
.written
, noisy
, &scanl
)) {
635 printf ("%*d empty\n", DMAXFOLDER
, msgnum
);
641 /* advise (cp, "unable to read"); already advised */
652 fputs (charstring_buffer (scanl
), aud
);
657 charstring_free (scanl
);
659 if (ferror(pf
) || fclose (pf
)) {
661 (void) m_unlink (cp
);
664 adios (cp
, "write error on");
668 if (trnflag
&& pop_dele (i
) == NOTOK
)
674 if (pop_quit () == NOTOK
)
677 } else if (inc_type
== INC_FILE
&& Maildir
== NULL
) {
678 /* Mail from a spool file. */
680 scan_detect_mbox_style (in
); /* the MAGIC invocation... */
681 hghnum
= msgnum
= mp
->hghmsg
;
683 charstring_t scanl
= NULL
;
685 /* create scanline for new message */
686 switch (incerr
= scan (in
, msgnum
+ 1, msgnum
+ 1, nfs
, width
,
687 msgnum
== hghnum
&& chgflag
, 1, NULL
, 0L, noisy
,
695 fputs ("inc aborted!\n", aud
);
696 inform("aborted!"); /* doesn't clean up locks! */
700 inform("BUG in %s, number out of range", invo_name
);
704 inform("BUG in %s, scan() botch (%d)", invo_name
, incerr
);
710 * Run the external program hook on the message.
713 (void)snprintf(b
, sizeof (b
), "%s/%d", maildir_copy
, msgnum
+ 1);
714 (void)ext_hook("add-hook", b
, NULL
);
717 fputs (charstring_buffer (scanl
), aud
);
724 charstring_free (scanl
);
726 /* If we get here there was some sort of error from scan(),
727 * so stop processing anything more from the spool.
733 /* Mail from Maildir. */
738 hghnum
= msgnum
= mp
->hghmsg
;
739 for (i
= 0; i
< num_maildir_entries
; i
++) {
740 charstring_t scanl
= NULL
;
744 sp
= Maildir
[i
].filename
;
745 cp
= mh_xstrdup(m_name (msgnum
));
747 if (!trnflag
|| link(sp
, cp
) == -1) {
748 static char buf
[65536];
751 if ((sf
= fopen (sp
, "r")) == NULL
)
752 adios (sp
, "unable to read for copy");
753 if ((pf
= fopen (cp
, "w+")) == NULL
)
754 adios (cp
, "unable to write for copy");
755 while ((nrd
= fread(buf
, 1, sizeof(buf
), sf
)) > 0)
756 if (fwrite(buf
, 1, nrd
, pf
) != nrd
)
758 if (ferror(sf
) || fflush(pf
) || ferror(pf
)) {
760 fclose(pf
); fclose(sf
); (void) m_unlink(cp
);
762 adios(cp
, "copy error %s -> %s", sp
, cp
);
767 if (pf
== NULL
&& (pf
= fopen (cp
, "r")) == NULL
)
768 adios (cp
, "not available");
769 chmod (cp
, m_gmprot ());
771 fseek (pf
, 0L, SEEK_SET
);
772 switch (incerr
= scan (pf
, msgnum
, 0, nfs
, width
,
773 msgnum
== mp
->hghmsg
+ 1 && chgflag
,
774 1, NULL
, 0, noisy
, &scanl
)) {
776 printf ("%*d empty\n", DMAXFOLDER
, msgnum
);
782 /* advise (cp, "unable to read"); already advised */
793 * Run the external program hook on the message.
796 (void)snprintf(b
, sizeof (b
), "%s/%d", maildir_copy
, msgnum
+ 1);
797 (void)ext_hook("add-hook", b
, NULL
);
800 fputs (charstring_buffer (scanl
), aud
);
805 charstring_free (scanl
);
807 if (ferror(pf
) || fclose (pf
)) {
809 (void) m_unlink (cp
);
811 adios (cp
, "write error on");
816 if (trnflag
&& m_unlink (sp
) == NOTOK
)
817 adios (sp
, "couldn't unlink");
818 free (sp
); /* Free Maildir[i]->filename */
822 free (Maildir
); /* From now on Maildir is just a flag - don't dref! */
827 if (incerr
< 0) { /* error */
829 GETGROUPPRIVS(); /* Be sure we can unlock mail file */
830 (void) lkfclosespool (in
, newmail
); in
= NULL
;
831 DROPGROUPPRIVS(); /* And then return us to normal privileges */
833 fclose (in
); in
= NULL
;
844 if (inc_type
== INC_FILE
&& Maildir
== NULL
) {
845 /* Mail from a spool file; truncate it. */
848 if (stat (newmail
, &st
) != NOTOK
&& s1
.st_mtime
!= st
.st_mtime
)
849 inform("new messages have arrived!\007");
852 if ((newfd
= creat (newmail
, 0600)) != NOTOK
)
855 admonish (newmail
, "error zero'ing");
859 printf ("%s not zero'd\n", newmail
);
863 if (msgnum
== hghnum
) {
864 inform("no messages incorporated, continuing...");
867 * Lock the sequence file now, and loop to set the right flags
868 * in the folder structure
874 context_replace (pfolder
, folder
); /* update current folder */
876 if ((mp2
= folder_read(folder
, 1)) == NULL
) {
877 inform("Unable to reread folder %s, continuing...", folder
);
882 * Shouldn't happen, but just in case ...
885 if (msgnum
>= mp2
->hghoff
886 && !(mp2
= folder_realloc (mp2
, mp2
->lowoff
, msgnum
+ 1))) {
887 inform("unable to reallocate folder storage");
892 mp2
->curmsg
= hghnum
+ 1;
893 mp2
->hghmsg
= msgnum
;
895 if (mp2
->lowmsg
== 0)
897 if (chgflag
) /* sigh... */
898 seq_setcur (mp2
, mp2
->curmsg
);
900 for (i
= hghnum
+ 1; i
<= msgnum
; i
++) {
901 clear_msg_flags (mp2
, i
);
905 mp2
->msgflags
|= SEQMOD
;
906 seq_setunseen(mp2
, 0); /* Set the Unseen-Sequence */
907 seq_save(mp2
); /* Save the sequence file */
912 if (inc_type
== INC_FILE
&& Maildir
== NULL
) {
913 /* Mail from a spool file; unlock it. */
916 GETGROUPPRIVS(); /* Be sure we can unlock mail file */
917 (void) lkfclosespool (in
, newmail
); in
= NULL
;
918 DROPGROUPPRIVS(); /* And then return us to normal privileges */
920 fclose (in
); in
= NULL
;
924 context_save (); /* save the context file */
931 inc_done (int status
)
937 lkfclosespool(in
, newmail
);
944 pop_action(void *closure
, char *s
)
950 n
= fprintf(pc
->mailout
, "%s\n", s
);
953 pc
->written
+= n
; /* Count linefeed too. */