]>
diplodocus.org Git - nmh/blob - uip/inc.c
e07f4caac17ba8085ac03c057c23d51c5f9f6e92
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/seq_save.h"
35 #include "sbr/smatch.h"
36 #include "sbr/getfolder.h"
37 #include "sbr/ext_hook.h"
38 #include "sbr/folder_read.h"
39 #include "sbr/folder_realloc.h"
40 #include "sbr/folder_free.h"
41 #include "sbr/context_save.h"
42 #include "sbr/context_replace.h"
43 #include "sbr/context_find.h"
44 #include "sbr/ambigsw.h"
46 #include "sbr/print_version.h"
47 #include "sbr/print_help.h"
48 #include "sbr/error.h"
51 #include "h/dropsbr.h"
53 #include "h/fmt_scan.h"
54 #include "h/scansbr.h"
55 #include "h/signals.h"
59 #include "sbr/lock_file.h"
60 #include "sbr/m_maildir.h"
61 #include "sbr/m_mktemp.h"
64 # define TLSminc(a) (a)
69 #define INC_SWITCHES \
70 X("audit audit-file", 0, AUDSW) \
71 X("noaudit", 0, NAUDSW) \
72 X("changecur", 0, CHGSW) \
73 X("nochangecur", 0, NCHGSW) \
74 X("file name", 0, FILESW) \
75 X("form formatfile", 0, FORMSW) \
76 X("format string", 5, FMTSW) \
77 X("host hostname", 0, HOSTSW) \
78 X("user username", 0, USERSW) \
79 X("port name/number", 0, PORTSW) \
80 X("silent", 0, SILSW) \
81 X("nosilent", 0, NSILSW) \
82 X("truncate", 0, TRNCSW) \
83 X("notruncate", 0, NTRNCSW) \
84 X("width columns", 0, WIDTHSW) \
85 X("version", 0, VERSIONSW) \
86 X("help", 0, HELPSW) \
87 X("snoop", 0, SNOOPSW) \
88 X("sasl", 0, SASLSW) \
89 X("nosasl", 0, NOSASLSW) \
90 X("saslmech", 0, SASLMECHSW) \
91 X("initialtls", TLSminc(-10), INITTLSSW) \
92 X("notls", TLSminc(-5), NOTLSSW) \
93 X("certverify", TLSminc(-10), CERTVERSW) \
94 X("nocertverify", TLSminc(-12), NOCERTVERSW) \
95 X("authservice", 0, AUTHSERVICESW) \
96 X("proxy command", 0, PROXYSW) \
98 #define X(sw, minchars, id) id,
99 DEFINE_SWITCH_ENUM(INC
);
102 #define X(sw, minchars, id) { sw, minchars, id },
103 DEFINE_SWITCH_ARRAY(INC
, switches
);
107 * flags for the mail source
112 static struct Maildir_entry
{
116 static int num_maildir_entries
= 0;
124 extern char response
[];
126 /* This is an attempt to simplify things by putting all the
127 * privilege ops into macros.
128 * *GROUPPRIVS() is related to handling the setgid MAIL property,
129 * and only applies if MAILGROUP is defined.
130 * Basically, SAVEGROUPPRIVS() is called right at the top of main()
131 * to initialise things, and then DROPGROUPPRIVS() and GETGROUPPRIVS()
132 * do the obvious thing.
134 * There's probably a better implementation if we're allowed to use
135 * BSD-style setreuid() rather than using POSIX saved-ids.
136 * Anyway, if you're euid root it's a bit pointless to drop the group
139 * I'm pretty happy that the security is good provided we aren't setuid root.
140 * The only things we trust with group=mail privilege are lkfopen()
145 * For setting and returning to "mail" gid
148 static gid_t return_gid
;
149 #define TRYDROPGROUPPRIVS() DROPGROUPPRIVS()
150 #define DROPGROUPPRIVS() \
151 if (setegid(getgid()) != 0) { \
152 adios ("setegid", "unable to restore group to %ld", (long) getgid()); \
154 #define GETGROUPPRIVS() \
155 if (setegid(return_gid) != 0) { \
156 adios ("setegid", "unable to set group to %ld", (long) return_gid); \
158 #define SAVEGROUPPRIVS() return_gid = getegid()
160 /* define *GROUPPRIVS() as null; this avoids having lots of "#ifdef MAILGROUP"s */
161 #define TRYDROPGROUPPRIVS()
162 #define DROPGROUPPRIVS()
163 #define GETGROUPPRIVS()
164 #define SAVEGROUPPRIVS()
165 #endif /* not MAILGROUP */
167 /* these variables have to be globals so that done() can correctly clean up the lockfile */
169 static char *newmail
;
175 static int maildir_srt(const void *va
, const void *vb
) PURE
;
176 static void inc_done(int) NORETURN
;
177 static int pop_action(void *closure
, char *);
180 maildir_srt(const void *va
, const void *vb
)
182 const struct Maildir_entry
*a
= va
, *b
= vb
;
183 if (a
->mtime
> b
->mtime
)
185 if (a
->mtime
< b
->mtime
)
191 main (int argc
, char **argv
)
198 int hghnum
= 0, msgnum
= 0;
200 bool sasl
, tls
, noverify
;
201 int incerr
= 0; /* <0 if inc hits an error which means it should not truncate mailspool */
202 char *cp
, *maildir
= NULL
, *folder
= NULL
;
203 char *format
= NULL
, *form
= NULL
;
204 char *host
= NULL
, *port
= NULL
, *user
= NULL
, *proxy
= NULL
;
205 char *audfile
= NULL
, *from
= NULL
, *saslmech
= NULL
, *auth_svc
= NULL
;
206 char buf
[BUFSIZ
], **argp
, *nfs
, **arguments
;
207 struct msgs
*mp
= NULL
;
210 char b
[PATH_MAX
+ 1];
211 char *maildir_copy
= NULL
; /* copy of mail directory because the static gets overwritten */
214 char *MAILHOST_env_variable
;
217 /* absolutely the first thing we do is save our privileges,
218 * and drop them if we can.
223 if (nmh_init(argv
[0], true, true)) { return 1; }
226 arguments
= getarguments (invo_name
, argc
, argv
, 1);
231 * use MAILHOST environment variable if present,
233 * If that fails, use the default (if any)
234 * provided by mts.conf in mts_init()
236 if ((MAILHOST_env_variable
= getenv("MAILHOST")) != NULL
)
237 pophost
= MAILHOST_env_variable
;
239 * If there is a valid "pophost" entry in mts.conf,
240 * then use it as the default host.
242 if (pophost
&& *pophost
)
246 chgflag
= noisy
= noverify
= true;
247 while ((cp
= *argp
++)) {
249 switch (smatch (++cp
, switches
)) {
251 ambigsw (cp
, switches
);
254 die("-%s unknown", cp
);
257 snprintf (buf
, sizeof(buf
), "%s [+folder] [switches]", invo_name
);
258 print_help (buf
, switches
, 1);
261 print_version(invo_name
);
265 if (!(cp
= *argp
++) || *cp
== '-')
266 die("missing argument to %s", argp
[-2]);
267 audfile
= mh_xstrdup(m_maildir(cp
));
281 * The flag `trnflag' has the value:
283 * 2 if -truncate is given
284 * 1 by default (truncating is default)
285 * 0 if -notruncate is given
295 if (!(cp
= *argp
++) || *cp
== '-')
296 die("missing argument to %s", argp
[-2]);
297 from
= path (cp
, TFILE
);
300 * If the truncate file is in default state,
301 * change to not truncate.
315 if (!(form
= *argp
++) || *form
== '-')
316 die("missing argument to %s", argp
[-2]);
320 if (!(format
= *argp
++) || *format
== '-')
321 die("missing argument to %s", argp
[-2]);
326 if (!(cp
= *argp
++) || *cp
== '-')
327 die("missing argument to %s", argp
[-2]);
332 if (!(host
= *argp
++) || *host
== '-')
333 die("missing argument to %s", argp
[-2]);
337 if (!(port
= *argp
++) || *port
== '-')
338 die("missing argument to %s", argp
[-2]);
342 if (!(user
= *argp
++) || *user
== '-')
343 die("missing argument to %s", argp
[-2]);
358 if (!(saslmech
= *argp
++) || *saslmech
== '-')
359 die("missing argument to %s", argp
[-2]);
380 if (!(auth_svc
= *argp
++) || *auth_svc
== '-')
381 die("missing argument to %s", argp
[-2]);
383 die("not built with OAuth support");
388 if (!(proxy
= *argp
++) || *proxy
== '-')
389 die("missing argument to %s", argp
[-2]);
393 if (*cp
== '+' || *cp
== '@') {
395 die("only one folder at a time!");
396 folder
= pluspath (cp
);
398 die("usage: %s [+folder] [switches]", invo_name
);
402 /* NOTE: above this point you should use TRYDROPGROUPPRIVS(),
403 * not DROPGROUPPRIVS().
408 /* guarantee dropping group privileges; we might not have done so earlier */
411 /* Source of mail; -from overrides any -host. */
412 inc_type
= host
&& !from
? INC_POP
: INC_FILE
;
414 if (inc_type
== INC_POP
) {
415 /* Mail from a POP server. */
418 if (auth_svc
== NULL
) {
419 if (saslmech
&& ! strcasecmp(saslmech
, "xoauth2")) {
420 die("must specify -authservice with -saslmech xoauth2");
424 die("must specify -user with -saslmech xoauth2");
429 tlsflag
|= P_INITTLS
;
432 tlsflag
|= P_NOVERIFY
;
435 * initialize POP connection
437 if (pop_init (host
, port
, user
, proxy
, snoop
, sasl
, saslmech
,
438 tlsflag
, auth_svc
) == NOTOK
)
441 /* Check if there are any messages */
442 if (pop_stat (&nmsgs
, &nbytes
) == NOTOK
)
447 die("no mail to incorporate");
450 } else if (inc_type
== INC_FILE
) {
451 /* Mail from a spool file, or Maildir. */
454 else if ((newmail
= getenv ("MAILDROP")) && *newmail
)
455 newmail
= m_mailpath (newmail
);
456 else if ((newmail
= context_find ("maildrop")) && *newmail
)
457 newmail
= m_mailpath (newmail
);
459 newmail
= concat (MAILDIR
, "/", MAILFIL
, NULL
);
461 if (stat (newmail
, &s1
) == NOTOK
|| s1
.st_size
== 0)
462 die("no mail to incorporate");
463 if (s1
.st_mode
& S_IFDIR
) {
469 cp
= concat (newmail
, "/new", NULL
);
470 if ((md
= opendir(cp
)) == NULL
)
471 die("unable to open %s", cp
);
472 while ((de
= readdir (md
)) != NULL
) {
473 if (de
->d_name
[0] == '.')
475 if (i
>= num_maildir_entries
) {
476 if ((Maildir
= realloc(Maildir
, sizeof(*Maildir
) * (2*i
+16))) == NULL
)
477 die("not enough memory for %d messages", 2*i
+16);
478 num_maildir_entries
= 2*i
+16;
480 Maildir
[i
].filename
= concat (cp
, "/", de
->d_name
, NULL
);
481 if (stat(Maildir
[i
].filename
, &ms
) != 0)
482 adios (Maildir
[i
].filename
, "couldn't get delivery time");
483 Maildir
[i
].mtime
= ms
.st_mtime
;
488 cp
= concat (newmail
, "/cur", NULL
);
489 if ((md
= opendir(cp
)) == NULL
)
490 die("unable to open %s", cp
);
491 while ((de
= readdir (md
)) != NULL
) {
492 if (de
->d_name
[0] == '.')
494 if (i
>= num_maildir_entries
) {
495 if ((Maildir
= realloc(Maildir
, sizeof(*Maildir
) * (2*i
+16))) == NULL
)
496 die("not enough memory for %d messages", 2*i
+16);
497 num_maildir_entries
= 2*i
+16;
499 Maildir
[i
].filename
= concat (cp
, "/", de
->d_name
, NULL
);
500 if (stat(Maildir
[i
].filename
, &ms
) != 0)
501 adios (Maildir
[i
].filename
, "couldn't get delivery time");
502 Maildir
[i
].mtime
= ms
.st_mtime
;
508 die("no mail to incorporate");
509 num_maildir_entries
= i
;
510 qsort (Maildir
, num_maildir_entries
, sizeof(*Maildir
), maildir_srt
);
513 cp
= mh_xstrdup(newmail
);
517 if (!context_find ("path"))
518 free (path ("./", TFOLDER
));
520 folder
= getfolder (0);
521 maildir
= m_maildir (folder
);
522 maildir_copy
= mh_xstrdup(maildir
);
524 if (!folder_exists(maildir
)) {
525 /* If the folder doesn't exist, and we're given the -silent flag,
529 create_folder(maildir
, 0, done
);
534 if (chdir (maildir
) == NOTOK
)
535 adios (maildir
, "unable to change directory to");
537 /* read folder and create message structure */
538 if (!(mp
= folder_read (folder
, 0)))
539 die("unable to read folder %s", folder
);
541 if (inc_type
== INC_FILE
&& Maildir
== NULL
) {
542 /* Mail from a spool file. */
544 if (access (newmail
, W_OK
) != NOTOK
) {
547 SIGNAL (SIGHUP
, SIG_IGN
);
548 SIGNAL (SIGINT
, SIG_IGN
);
549 SIGNAL (SIGQUIT
, SIG_IGN
);
550 SIGNAL (SIGTERM
, SIG_IGN
);
553 GETGROUPPRIVS(); /* Reset gid to lock mail file */
554 in
= lkfopenspool (newmail
, "r");
557 die("unable to lock and fopen %s", newmail
);
558 fstat (fileno(in
), &s1
);
561 if ((in
= fopen (newmail
, "r")) == NULL
)
562 adios (newmail
, "unable to read");
566 /* This shouldn't be necessary but it can't hurt. */
571 if ((i
= stat (audfile
, &st
)) == NOTOK
)
572 inform("Creating Receive-Audit: %s", audfile
);
573 if ((aud
= fopen (audfile
, "a")) == NULL
)
574 adios (audfile
, "unable to append to");
576 chmod (audfile
, m_gmprot ());
579 fprintf (aud
, "<<inc>> %s -ms %s\n", dtimenow(0), from
);
582 fprintf (aud
, "<<inc>> %s -host %s -user %s\n", dtimenow(0),
585 fprintf (aud
, "<<inc>> %s\n", dtimenow (0));
589 /* Get new format string */
590 nfs
= new_fs (form
, format
, FORMAT
);
593 printf ("Incorporating new mail into %s...\n\n", folder
);
599 * Get the mail from a POP server
601 if (inc_type
== INC_POP
) {
602 /* Mail from a POP server. */
606 hghnum
= msgnum
= mp
->hghmsg
;
607 for (i
= 1; i
<= nmsgs
; i
++) {
608 charstring_t scanl
= NULL
;
611 cp
= mh_xstrdup(m_name (msgnum
));
612 if ((pf
= fopen (cp
, "w+")) == NULL
)
613 adios (cp
, "unable to write");
614 chmod (cp
, m_gmprot ());
618 if (pop_retr(i
, pop_action
, &pc
) == NOTOK
)
622 adios (cp
, "write error on");
623 fseek (pf
, 0L, SEEK_SET
);
624 switch (incerr
= scan (pf
, msgnum
, 0, nfs
, width
,
625 msgnum
== mp
->hghmsg
+ 1 && chgflag
,
626 1, NULL
, pc
.written
, noisy
, &scanl
)) {
628 printf ("%*d empty\n", DMAXFOLDER
, msgnum
);
634 /* advise (cp, "unable to read"); already advised */
645 fputs (charstring_buffer (scanl
), aud
);
650 charstring_free (scanl
);
652 if (ferror(pf
) || fclose (pf
)) {
654 (void) m_unlink (cp
);
657 adios (cp
, "write error on");
661 if (trnflag
&& pop_dele (i
) == NOTOK
)
667 if (pop_quit () == NOTOK
)
670 } else if (inc_type
== INC_FILE
&& Maildir
== NULL
) {
671 /* Mail from a spool file. */
673 scan_detect_mbox_style (in
); /* the MAGIC invocation... */
674 hghnum
= msgnum
= mp
->hghmsg
;
676 charstring_t scanl
= NULL
;
678 /* create scanline for new message */
679 switch (incerr
= scan (in
, msgnum
+ 1, msgnum
+ 1, nfs
, width
,
680 msgnum
== hghnum
&& chgflag
, 1, NULL
, 0L, noisy
,
688 fputs ("inc aborted!\n", aud
);
689 inform("aborted!"); /* doesn't clean up locks! */
693 inform("BUG in %s, number out of range", invo_name
);
697 inform("BUG in %s, scan() botch (%d)", invo_name
, incerr
);
703 * Run the external program hook on the message.
706 (void)snprintf(b
, sizeof (b
), "%s/%d", maildir_copy
, msgnum
+ 1);
707 (void)ext_hook("add-hook", b
, NULL
);
710 fputs (charstring_buffer (scanl
), aud
);
717 charstring_free (scanl
);
719 /* If we get here there was some sort of error from scan(),
720 * so stop processing anything more from the spool.
726 /* Mail from Maildir. */
731 hghnum
= msgnum
= mp
->hghmsg
;
732 for (i
= 0; i
< num_maildir_entries
; i
++) {
733 charstring_t scanl
= NULL
;
737 sp
= Maildir
[i
].filename
;
738 cp
= mh_xstrdup(m_name (msgnum
));
740 if (!trnflag
|| link(sp
, cp
) == -1) {
741 static char buf
[65536];
744 if ((sf
= fopen (sp
, "r")) == NULL
)
745 adios (sp
, "unable to read for copy");
746 if ((pf
= fopen (cp
, "w+")) == NULL
)
747 adios (cp
, "unable to write for copy");
748 while ((nrd
= fread(buf
, 1, sizeof(buf
), sf
)) > 0)
749 if (fwrite(buf
, 1, nrd
, pf
) != nrd
)
751 if (ferror(sf
) || fflush(pf
) || ferror(pf
)) {
753 fclose(pf
); fclose(sf
); (void) m_unlink(cp
);
755 adios(cp
, "copy error %s -> %s", sp
, cp
);
760 if (pf
== NULL
&& (pf
= fopen (cp
, "r")) == NULL
)
761 adios (cp
, "not available");
762 chmod (cp
, m_gmprot ());
764 fseek (pf
, 0L, SEEK_SET
);
765 switch (incerr
= scan (pf
, msgnum
, 0, nfs
, width
,
766 msgnum
== mp
->hghmsg
+ 1 && chgflag
,
767 1, NULL
, 0, noisy
, &scanl
)) {
769 printf ("%*d empty\n", DMAXFOLDER
, msgnum
);
775 /* advise (cp, "unable to read"); already advised */
786 * Run the external program hook on the message.
789 (void)snprintf(b
, sizeof (b
), "%s/%d", maildir_copy
, msgnum
+ 1);
790 (void)ext_hook("add-hook", b
, NULL
);
793 fputs (charstring_buffer (scanl
), aud
);
798 charstring_free (scanl
);
800 if (ferror(pf
) || fclose (pf
)) {
802 (void) m_unlink (cp
);
804 adios (cp
, "write error on");
809 if (trnflag
&& m_unlink (sp
) == NOTOK
)
810 adios (sp
, "couldn't unlink");
811 free (sp
); /* Free Maildir[i]->filename */
815 free (Maildir
); /* From now on Maildir is just a flag - don't dref! */
820 if (incerr
< 0) { /* error */
822 GETGROUPPRIVS(); /* Be sure we can unlock mail file */
823 (void) lkfclosespool (in
, newmail
); in
= NULL
;
824 DROPGROUPPRIVS(); /* And then return us to normal privileges */
826 fclose (in
); in
= NULL
;
837 if (inc_type
== INC_FILE
&& Maildir
== NULL
) {
838 /* Mail from a spool file; truncate it. */
841 if (stat (newmail
, &st
) != NOTOK
&& s1
.st_mtime
!= st
.st_mtime
)
842 inform("new messages have arrived!\007");
845 if ((newfd
= creat (newmail
, 0600)) != NOTOK
)
848 admonish (newmail
, "error zero'ing");
852 printf ("%s not zero'd\n", newmail
);
856 if (msgnum
== hghnum
) {
857 inform("no messages incorporated, continuing...");
860 * Lock the sequence file now, and loop to set the right flags
861 * in the folder structure
867 context_replace (pfolder
, folder
); /* update current folder */
869 if ((mp2
= folder_read(folder
, 1)) == NULL
) {
870 inform("Unable to reread folder %s, continuing...", folder
);
875 * Shouldn't happen, but just in case ...
878 if (msgnum
>= mp2
->hghoff
879 && !(mp2
= folder_realloc (mp2
, mp2
->lowoff
, msgnum
+ 1))) {
880 inform("unable to reallocate folder storage");
885 mp2
->curmsg
= hghnum
+ 1;
886 mp2
->hghmsg
= msgnum
;
888 if (mp2
->lowmsg
== 0)
890 if (chgflag
) /* sigh... */
891 seq_setcur (mp2
, mp2
->curmsg
);
893 for (i
= hghnum
+ 1; i
<= msgnum
; i
++) {
894 clear_msg_flags (mp2
, i
);
898 mp2
->msgflags
|= SEQMOD
;
899 seq_setunseen(mp2
, 0); /* Set the Unseen-Sequence */
900 seq_save(mp2
); /* Save the sequence file */
905 if (inc_type
== INC_FILE
&& Maildir
== NULL
) {
906 /* Mail from a spool file; unlock it. */
909 GETGROUPPRIVS(); /* Be sure we can unlock mail file */
910 (void) lkfclosespool (in
, newmail
); in
= NULL
;
911 DROPGROUPPRIVS(); /* And then return us to normal privileges */
913 fclose (in
); in
= NULL
;
917 context_save (); /* save the context file */
924 inc_done (int status
)
930 lkfclosespool(in
, newmail
);
937 pop_action(void *closure
, char *s
)
943 n
= fprintf(pc
->mailout
, "%s\n", s
);
946 pc
->written
+= n
; /* Count linefeed too. */