]>
diplodocus.org Git - nmh/blob - uip/inc.c
ae41caa031c5fc6c05e2c40b59f0d2b199ed3e78
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/context_save.h"
35 #include "sbr/context_replace.h"
36 #include "sbr/context_find.h"
37 #include "sbr/ambigsw.h"
39 #include "sbr/print_version.h"
40 #include "sbr/print_help.h"
41 #include "sbr/error.h"
44 #include "h/dropsbr.h"
46 #include "h/fmt_scan.h"
47 #include "h/scansbr.h"
48 #include "h/signals.h"
52 #include "sbr/lock_file.h"
53 #include "sbr/m_maildir.h"
54 #include "sbr/m_mktemp.h"
57 # define TLSminc(a) (a)
62 #define INC_SWITCHES \
63 X("audit audit-file", 0, AUDSW) \
64 X("noaudit", 0, NAUDSW) \
65 X("changecur", 0, CHGSW) \
66 X("nochangecur", 0, NCHGSW) \
67 X("file name", 0, FILESW) \
68 X("form formatfile", 0, FORMSW) \
69 X("format string", 5, FMTSW) \
70 X("host hostname", 0, HOSTSW) \
71 X("user username", 0, USERSW) \
72 X("port name/number", 0, PORTSW) \
73 X("silent", 0, SILSW) \
74 X("nosilent", 0, NSILSW) \
75 X("truncate", 0, TRNCSW) \
76 X("notruncate", 0, NTRNCSW) \
77 X("width columns", 0, WIDTHSW) \
78 X("version", 0, VERSIONSW) \
79 X("help", 0, HELPSW) \
80 X("snoop", 0, SNOOPSW) \
81 X("sasl", 0, SASLSW) \
82 X("nosasl", 0, NOSASLSW) \
83 X("saslmech", 0, SASLMECHSW) \
84 X("initialtls", TLSminc(-10), INITTLSSW) \
85 X("notls", TLSminc(-5), NOTLSSW) \
86 X("certverify", TLSminc(-10), CERTVERSW) \
87 X("nocertverify", TLSminc(-12), NOCERTVERSW) \
88 X("authservice", 0, AUTHSERVICESW) \
89 X("proxy command", 0, PROXYSW) \
91 #define X(sw, minchars, id) id,
92 DEFINE_SWITCH_ENUM(INC
);
95 #define X(sw, minchars, id) { sw, minchars, id },
96 DEFINE_SWITCH_ARRAY(INC
, switches
);
100 * flags for the mail source
105 static struct Maildir_entry
{
109 static int num_maildir_entries
= 0;
117 extern char response
[];
119 /* This is an attempt to simplify things by putting all the
120 * privilege ops into macros.
121 * *GROUPPRIVS() is related to handling the setgid MAIL property,
122 * and only applies if MAILGROUP is defined.
123 * Basically, SAVEGROUPPRIVS() is called right at the top of main()
124 * to initialise things, and then DROPGROUPPRIVS() and GETGROUPPRIVS()
125 * do the obvious thing.
127 * There's probably a better implementation if we're allowed to use
128 * BSD-style setreuid() rather than using POSIX saved-ids.
129 * Anyway, if you're euid root it's a bit pointless to drop the group
132 * I'm pretty happy that the security is good provided we aren't setuid root.
133 * The only things we trust with group=mail privilege are lkfopen()
138 * For setting and returning to "mail" gid
141 static gid_t return_gid
;
142 #define TRYDROPGROUPPRIVS() DROPGROUPPRIVS()
143 #define DROPGROUPPRIVS() \
144 if (setegid(getgid()) != 0) { \
145 adios ("setegid", "unable to restore group to %ld", (long) getgid()); \
147 #define GETGROUPPRIVS() \
148 if (setegid(return_gid) != 0) { \
149 adios ("setegid", "unable to set group to %ld", (long) return_gid); \
151 #define SAVEGROUPPRIVS() return_gid = getegid()
153 /* define *GROUPPRIVS() as null; this avoids having lots of "#ifdef MAILGROUP"s */
154 #define TRYDROPGROUPPRIVS()
155 #define DROPGROUPPRIVS()
156 #define GETGROUPPRIVS()
157 #define SAVEGROUPPRIVS()
158 #endif /* not MAILGROUP */
160 /* these variables have to be globals so that done() can correctly clean up the lockfile */
162 static char *newmail
;
168 static int maildir_srt(const void *va
, const void *vb
) PURE
;
169 static void inc_done(int) NORETURN
;
170 static int pop_action(void *closure
, char *);
173 maildir_srt(const void *va
, const void *vb
)
175 const struct Maildir_entry
*a
= va
, *b
= vb
;
176 if (a
->mtime
> b
->mtime
)
178 if (a
->mtime
< b
->mtime
)
184 main (int argc
, char **argv
)
191 int hghnum
= 0, msgnum
= 0;
193 bool sasl
, tls
, noverify
;
194 int incerr
= 0; /* <0 if inc hits an error which means it should not truncate mailspool */
195 char *cp
, *maildir
= NULL
, *folder
= NULL
;
196 char *format
= NULL
, *form
= NULL
;
197 char *host
= NULL
, *port
= NULL
, *user
= NULL
, *proxy
= NULL
;
198 char *audfile
= NULL
, *from
= NULL
, *saslmech
= NULL
, *auth_svc
= NULL
;
199 char buf
[BUFSIZ
], **argp
, *nfs
, **arguments
;
200 struct msgs
*mp
= NULL
;
203 char b
[PATH_MAX
+ 1];
204 char *maildir_copy
= NULL
; /* copy of mail directory because the static gets overwritten */
207 char *MAILHOST_env_variable
;
210 /* absolutely the first thing we do is save our privileges,
211 * and drop them if we can.
216 if (nmh_init(argv
[0], true, true)) { return 1; }
219 arguments
= getarguments (invo_name
, argc
, argv
, 1);
224 * use MAILHOST environment variable if present,
226 * If that fails, use the default (if any)
227 * provided by mts.conf in mts_init()
229 if ((MAILHOST_env_variable
= getenv("MAILHOST")) != NULL
)
230 pophost
= MAILHOST_env_variable
;
232 * If there is a valid "pophost" entry in mts.conf,
233 * then use it as the default host.
235 if (pophost
&& *pophost
)
239 chgflag
= noisy
= noverify
= true;
240 while ((cp
= *argp
++)) {
242 switch (smatch (++cp
, switches
)) {
244 ambigsw (cp
, switches
);
247 die("-%s unknown", cp
);
250 snprintf (buf
, sizeof(buf
), "%s [+folder] [switches]", invo_name
);
251 print_help (buf
, switches
, 1);
254 print_version(invo_name
);
258 if (!(cp
= *argp
++) || *cp
== '-')
259 die("missing argument to %s", argp
[-2]);
260 audfile
= mh_xstrdup(m_maildir(cp
));
274 * The flag `trnflag' has the value:
276 * 2 if -truncate is given
277 * 1 by default (truncating is default)
278 * 0 if -notruncate is given
288 if (!(cp
= *argp
++) || *cp
== '-')
289 die("missing argument to %s", argp
[-2]);
290 from
= path (cp
, TFILE
);
293 * If the truncate file is in default state,
294 * change to not truncate.
308 if (!(form
= *argp
++) || *form
== '-')
309 die("missing argument to %s", argp
[-2]);
313 if (!(format
= *argp
++) || *format
== '-')
314 die("missing argument to %s", argp
[-2]);
319 if (!(cp
= *argp
++) || *cp
== '-')
320 die("missing argument to %s", argp
[-2]);
325 if (!(host
= *argp
++) || *host
== '-')
326 die("missing argument to %s", argp
[-2]);
330 if (!(port
= *argp
++) || *port
== '-')
331 die("missing argument to %s", argp
[-2]);
335 if (!(user
= *argp
++) || *user
== '-')
336 die("missing argument to %s", argp
[-2]);
351 if (!(saslmech
= *argp
++) || *saslmech
== '-')
352 die("missing argument to %s", argp
[-2]);
373 if (!(auth_svc
= *argp
++) || *auth_svc
== '-')
374 die("missing argument to %s", argp
[-2]);
376 die("not built with OAuth support");
381 if (!(proxy
= *argp
++) || *proxy
== '-')
382 die("missing argument to %s", argp
[-2]);
386 if (*cp
== '+' || *cp
== '@') {
388 die("only one folder at a time!");
389 folder
= pluspath (cp
);
391 die("usage: %s [+folder] [switches]", invo_name
);
395 /* NOTE: above this point you should use TRYDROPGROUPPRIVS(),
396 * not DROPGROUPPRIVS().
401 /* guarantee dropping group privileges; we might not have done so earlier */
404 /* Source of mail; -from overrides any -host. */
405 inc_type
= host
&& !from
? INC_POP
: INC_FILE
;
407 if (inc_type
== INC_POP
) {
408 /* Mail from a POP server. */
411 if (auth_svc
== NULL
) {
412 if (saslmech
&& ! strcasecmp(saslmech
, "xoauth2")) {
413 die("must specify -authservice with -saslmech xoauth2");
417 die("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
)
434 /* Check if there are any messages */
435 if (pop_stat (&nmsgs
, &nbytes
) == NOTOK
)
440 die("no mail to incorporate");
443 } else if (inc_type
== INC_FILE
) {
444 /* Mail from a spool file, or Maildir. */
447 else if ((newmail
= getenv ("MAILDROP")) && *newmail
)
448 newmail
= m_mailpath (newmail
);
449 else if ((newmail
= context_find ("maildrop")) && *newmail
)
450 newmail
= m_mailpath (newmail
);
452 newmail
= concat (MAILDIR
, "/", MAILFIL
, NULL
);
454 if (stat (newmail
, &s1
) == NOTOK
|| s1
.st_size
== 0)
455 die("no mail to incorporate");
456 if (s1
.st_mode
& S_IFDIR
) {
462 cp
= concat (newmail
, "/new", NULL
);
463 if ((md
= opendir(cp
)) == NULL
)
464 die("unable to open %s", cp
);
465 while ((de
= readdir (md
)) != NULL
) {
466 if (de
->d_name
[0] == '.')
468 if (i
>= num_maildir_entries
) {
469 if ((Maildir
= realloc(Maildir
, sizeof(*Maildir
) * (2*i
+16))) == NULL
)
470 die("not enough memory for %d messages", 2*i
+16);
471 num_maildir_entries
= 2*i
+16;
473 Maildir
[i
].filename
= concat (cp
, "/", de
->d_name
, NULL
);
474 if (stat(Maildir
[i
].filename
, &ms
) != 0)
475 adios (Maildir
[i
].filename
, "couldn't get delivery time");
476 Maildir
[i
].mtime
= ms
.st_mtime
;
481 cp
= concat (newmail
, "/cur", NULL
);
482 if ((md
= opendir(cp
)) == NULL
)
483 die("unable to open %s", cp
);
484 while ((de
= readdir (md
)) != NULL
) {
485 if (de
->d_name
[0] == '.')
487 if (i
>= num_maildir_entries
) {
488 if ((Maildir
= realloc(Maildir
, sizeof(*Maildir
) * (2*i
+16))) == NULL
)
489 die("not enough memory for %d messages", 2*i
+16);
490 num_maildir_entries
= 2*i
+16;
492 Maildir
[i
].filename
= concat (cp
, "/", de
->d_name
, NULL
);
493 if (stat(Maildir
[i
].filename
, &ms
) != 0)
494 adios (Maildir
[i
].filename
, "couldn't get delivery time");
495 Maildir
[i
].mtime
= ms
.st_mtime
;
501 die("no mail to incorporate");
502 num_maildir_entries
= i
;
503 qsort (Maildir
, num_maildir_entries
, sizeof(*Maildir
), maildir_srt
);
506 cp
= mh_xstrdup(newmail
);
510 if (!context_find ("path"))
511 free (path ("./", TFOLDER
));
513 folder
= getfolder (0);
514 maildir
= m_maildir (folder
);
515 maildir_copy
= mh_xstrdup(maildir
);
517 if (!folder_exists(maildir
)) {
518 /* If the folder doesn't exist, and we're given the -silent flag,
522 create_folder(maildir
, 0, done
);
527 if (chdir (maildir
) == NOTOK
)
528 adios (maildir
, "unable to change directory to");
530 /* read folder and create message structure */
531 if (!(mp
= folder_read (folder
, 0)))
532 die("unable to read folder %s", folder
);
534 if (inc_type
== INC_FILE
&& Maildir
== NULL
) {
535 /* Mail from a spool file. */
537 if (access (newmail
, W_OK
) != NOTOK
) {
540 SIGNAL (SIGHUP
, SIG_IGN
);
541 SIGNAL (SIGINT
, SIG_IGN
);
542 SIGNAL (SIGQUIT
, SIG_IGN
);
543 SIGNAL (SIGTERM
, SIG_IGN
);
546 GETGROUPPRIVS(); /* Reset gid to lock mail file */
547 in
= lkfopenspool (newmail
, "r");
550 die("unable to lock and fopen %s", newmail
);
551 fstat (fileno(in
), &s1
);
554 if ((in
= fopen (newmail
, "r")) == NULL
)
555 adios (newmail
, "unable to read");
559 /* This shouldn't be necessary but it can't hurt. */
564 if ((i
= stat (audfile
, &st
)) == NOTOK
)
565 inform("Creating Receive-Audit: %s", audfile
);
566 if ((aud
= fopen (audfile
, "a")) == NULL
)
567 adios (audfile
, "unable to append to");
569 chmod (audfile
, m_gmprot ());
572 fprintf (aud
, "<<inc>> %s -ms %s\n", dtimenow(0), from
);
575 fprintf (aud
, "<<inc>> %s -host %s -user %s\n", dtimenow(0),
578 fprintf (aud
, "<<inc>> %s\n", dtimenow (0));
582 /* Get new format string */
583 nfs
= new_fs (form
, format
, FORMAT
);
586 printf ("Incorporating new mail into %s...\n\n", folder
);
592 * Get the mail from a POP server
594 if (inc_type
== INC_POP
) {
595 /* Mail from a POP server. */
599 hghnum
= msgnum
= mp
->hghmsg
;
600 for (i
= 1; i
<= nmsgs
; i
++) {
601 charstring_t scanl
= NULL
;
604 cp
= mh_xstrdup(m_name (msgnum
));
605 if ((pf
= fopen (cp
, "w+")) == NULL
)
606 adios (cp
, "unable to write");
607 chmod (cp
, m_gmprot ());
611 if (pop_retr(i
, pop_action
, &pc
) == NOTOK
)
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
, pc
.written
, 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
)
660 if (pop_quit () == NOTOK
)
663 } else if (inc_type
== INC_FILE
&& Maildir
== NULL
) {
664 /* Mail from a spool file. */
666 scan_detect_mbox_style (in
); /* the MAGIC invocation... */
667 hghnum
= msgnum
= mp
->hghmsg
;
669 charstring_t scanl
= NULL
;
671 /* create scanline for new message */
672 switch (incerr
= scan (in
, msgnum
+ 1, msgnum
+ 1, nfs
, width
,
673 msgnum
== hghnum
&& chgflag
, 1, NULL
, 0L, noisy
,
681 fputs ("inc aborted!\n", aud
);
682 inform("aborted!"); /* doesn't clean up locks! */
686 inform("BUG in %s, number out of range", invo_name
);
690 inform("BUG in %s, scan() botch (%d)", invo_name
, incerr
);
696 * Run the external program hook on the message.
699 (void)snprintf(b
, sizeof (b
), "%s/%d", maildir_copy
, msgnum
+ 1);
700 (void)ext_hook("add-hook", b
, NULL
);
703 fputs (charstring_buffer (scanl
), aud
);
710 charstring_free (scanl
);
712 /* If we get here there was some sort of error from scan(),
713 * so stop processing anything more from the spool.
719 /* Mail from Maildir. */
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
, 0, 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
;
830 if (inc_type
== INC_FILE
&& Maildir
== NULL
) {
831 /* Mail from a spool file; truncate it. */
834 if (stat (newmail
, &st
) != NOTOK
&& s1
.st_mtime
!= st
.st_mtime
)
835 inform("new messages have arrived!\007");
838 if ((newfd
= creat (newmail
, 0600)) != NOTOK
)
841 admonish (newmail
, "error zero'ing");
845 printf ("%s not zero'd\n", newmail
);
849 if (msgnum
== hghnum
) {
850 inform("no messages incorporated, continuing...");
853 * Lock the sequence file now, and loop to set the right flags
854 * in the folder structure
860 context_replace (pfolder
, folder
); /* update current folder */
862 if ((mp2
= folder_read(folder
, 1)) == NULL
) {
863 inform("Unable to reread folder %s, continuing...", folder
);
868 * Shouldn't happen, but just in case ...
871 if (msgnum
>= mp2
->hghoff
872 && !(mp2
= folder_realloc (mp2
, mp2
->lowoff
, msgnum
+ 1))) {
873 inform("unable to reallocate folder storage");
878 mp2
->curmsg
= hghnum
+ 1;
879 mp2
->hghmsg
= msgnum
;
881 if (mp2
->lowmsg
== 0)
883 if (chgflag
) /* sigh... */
884 seq_setcur (mp2
, mp2
->curmsg
);
886 for (i
= hghnum
+ 1; i
<= msgnum
; i
++) {
887 clear_msg_flags (mp2
, i
);
891 mp2
->msgflags
|= SEQMOD
;
892 seq_setunseen(mp2
, 0); /* Set the Unseen-Sequence */
893 seq_save(mp2
); /* Save the sequence file */
898 if (inc_type
== INC_FILE
&& Maildir
== NULL
) {
899 /* Mail from a spool file; unlock it. */
902 GETGROUPPRIVS(); /* Be sure we can unlock mail file */
903 (void) lkfclosespool (in
, newmail
); in
= NULL
;
904 DROPGROUPPRIVS(); /* And then return us to normal privileges */
906 fclose (in
); in
= NULL
;
910 context_save (); /* save the context file */
917 inc_done (int status
)
923 lkfclosespool(in
, newmail
);
930 pop_action(void *closure
, char *s
)
936 n
= fprintf(pc
->mailout
, "%s\n", s
);
939 pc
->written
+= n
; /* Count linefeed too. */