]>
diplodocus.org Git - nmh/blob - uip/inc.c
1a3b28b11cea6fd8ce5791a5e952f82e72c94222
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/m_gmprot.h"
35 #include "sbr/getarguments.h"
36 #include "sbr/concat.h"
37 #include "sbr/seq_setunseen.h"
38 #include "sbr/seq_setcur.h"
39 #include "sbr/seq_save.h"
40 #include "sbr/smatch.h"
41 #include "sbr/getfolder.h"
42 #include "sbr/ext_hook.h"
43 #include "sbr/folder_read.h"
44 #include "sbr/folder_realloc.h"
45 #include "sbr/folder_free.h"
46 #include "sbr/context_save.h"
47 #include "sbr/context_replace.h"
48 #include "sbr/context_find.h"
49 #include "sbr/ambigsw.h"
51 #include "sbr/print_version.h"
52 #include "sbr/print_help.h"
53 #include "sbr/error.h"
56 #include "h/dropsbr.h"
58 #include "h/fmt_scan.h"
59 #include "h/scansbr.h"
60 #include "h/signals.h"
64 #include "sbr/lock_file.h"
65 #include "sbr/m_maildir.h"
66 #include "sbr/m_mktemp.h"
69 # define TLSminc(a) (a)
74 #define INC_SWITCHES \
75 X("audit audit-file", 0, AUDSW) \
76 X("noaudit", 0, NAUDSW) \
77 X("changecur", 0, CHGSW) \
78 X("nochangecur", 0, NCHGSW) \
79 X("file name", 0, FILESW) \
80 X("form formatfile", 0, FORMSW) \
81 X("format string", 5, FMTSW) \
82 X("host hostname", 0, HOSTSW) \
83 X("user username", 0, USERSW) \
84 X("port name/number", 0, PORTSW) \
85 X("silent", 0, SILSW) \
86 X("nosilent", 0, NSILSW) \
87 X("truncate", 0, TRNCSW) \
88 X("notruncate", 0, NTRNCSW) \
89 X("width columns", 0, WIDTHSW) \
90 X("version", 0, VERSIONSW) \
91 X("help", 0, HELPSW) \
92 X("snoop", 0, SNOOPSW) \
93 X("sasl", 0, SASLSW) \
94 X("nosasl", 0, NOSASLSW) \
95 X("saslmech", 0, SASLMECHSW) \
96 X("initialtls", TLSminc(-10), INITTLSSW) \
97 X("notls", TLSminc(-5), NOTLSSW) \
98 X("certverify", TLSminc(-10), CERTVERSW) \
99 X("nocertverify", TLSminc(-12), NOCERTVERSW) \
100 X("authservice", 0, AUTHSERVICESW) \
101 X("proxy command", 0, PROXYSW) \
103 #define X(sw, minchars, id) id,
104 DEFINE_SWITCH_ENUM(INC
);
107 #define X(sw, minchars, id) { sw, minchars, id },
108 DEFINE_SWITCH_ARRAY(INC
, switches
);
112 * flags for the mail source
117 static struct Maildir_entry
{
121 static int num_maildir_entries
= 0;
129 extern char response
[];
131 /* This is an attempt to simplify things by putting all the
132 * privilege ops into macros.
133 * *GROUPPRIVS() is related to handling the setgid MAIL property,
134 * and only applies if MAILGROUP is defined.
135 * Basically, SAVEGROUPPRIVS() is called right at the top of main()
136 * to initialise things, and then DROPGROUPPRIVS() and GETGROUPPRIVS()
137 * do the obvious thing.
139 * There's probably a better implementation if we're allowed to use
140 * BSD-style setreuid() rather than using POSIX saved-ids.
141 * Anyway, if you're euid root it's a bit pointless to drop the group
144 * I'm pretty happy that the security is good provided we aren't setuid root.
145 * The only things we trust with group=mail privilege are lkfopen()
150 * For setting and returning to "mail" gid
153 static gid_t return_gid
;
154 #define TRYDROPGROUPPRIVS() DROPGROUPPRIVS()
155 #define DROPGROUPPRIVS() \
156 if (setegid(getgid()) != 0) { \
157 adios ("setegid", "unable to restore group to %ld", (long) getgid()); \
159 #define GETGROUPPRIVS() \
160 if (setegid(return_gid) != 0) { \
161 adios ("setegid", "unable to set group to %ld", (long) return_gid); \
163 #define SAVEGROUPPRIVS() return_gid = getegid()
165 /* define *GROUPPRIVS() as null; this avoids having lots of "#ifdef MAILGROUP"s */
166 #define TRYDROPGROUPPRIVS()
167 #define DROPGROUPPRIVS()
168 #define GETGROUPPRIVS()
169 #define SAVEGROUPPRIVS()
170 #endif /* not MAILGROUP */
172 /* these variables have to be globals so that done() can correctly clean up the lockfile */
174 static char *newmail
;
180 static int maildir_srt(const void *va
, const void *vb
) PURE
;
181 static void inc_done(int) NORETURN
;
182 static int pop_action(void *closure
, char *);
185 maildir_srt(const void *va
, const void *vb
)
187 const struct Maildir_entry
*a
= va
, *b
= vb
;
188 if (a
->mtime
> b
->mtime
)
190 if (a
->mtime
< b
->mtime
)
196 main (int argc
, char **argv
)
203 int hghnum
= 0, msgnum
= 0;
205 bool sasl
, tls
, noverify
;
206 int incerr
= 0; /* <0 if inc hits an error which means it should not truncate mailspool */
207 char *cp
, *maildir
= NULL
, *folder
= NULL
;
208 char *format
= NULL
, *form
= NULL
;
209 char *host
= NULL
, *port
= NULL
, *user
= NULL
, *proxy
= NULL
;
210 char *audfile
= NULL
, *from
= NULL
, *saslmech
= NULL
, *auth_svc
= NULL
;
211 char buf
[BUFSIZ
], **argp
, *nfs
, **arguments
;
212 struct msgs
*mp
= NULL
;
215 char b
[PATH_MAX
+ 1];
216 char *maildir_copy
= NULL
; /* copy of mail directory because the static gets overwritten */
219 char *MAILHOST_env_variable
;
222 /* absolutely the first thing we do is save our privileges,
223 * and drop them if we can.
228 if (nmh_init(argv
[0], true, true)) { return 1; }
231 arguments
= getarguments (invo_name
, argc
, argv
, 1);
236 * use MAILHOST environment variable if present,
238 * If that fails, use the default (if any)
239 * provided by mts.conf in mts_init()
241 if ((MAILHOST_env_variable
= getenv("MAILHOST")) != NULL
)
242 pophost
= MAILHOST_env_variable
;
244 * If there is a valid "pophost" entry in mts.conf,
245 * then use it as the default host.
247 if (pophost
&& *pophost
)
251 chgflag
= noisy
= noverify
= true;
252 while ((cp
= *argp
++)) {
254 switch (smatch (++cp
, switches
)) {
256 ambigsw (cp
, switches
);
259 die("-%s unknown", cp
);
262 snprintf (buf
, sizeof(buf
), "%s [+folder] [switches]", invo_name
);
263 print_help (buf
, switches
, 1);
266 print_version(invo_name
);
270 if (!(cp
= *argp
++) || *cp
== '-')
271 die("missing argument to %s", argp
[-2]);
272 audfile
= mh_xstrdup(m_maildir(cp
));
286 * The flag `trnflag' has the value:
288 * 2 if -truncate is given
289 * 1 by default (truncating is default)
290 * 0 if -notruncate is given
300 if (!(cp
= *argp
++) || *cp
== '-')
301 die("missing argument to %s", argp
[-2]);
302 from
= path (cp
, TFILE
);
305 * If the truncate file is in default state,
306 * change to not truncate.
320 if (!(form
= *argp
++) || *form
== '-')
321 die("missing argument to %s", argp
[-2]);
325 if (!(format
= *argp
++) || *format
== '-')
326 die("missing argument to %s", argp
[-2]);
331 if (!(cp
= *argp
++) || *cp
== '-')
332 die("missing argument to %s", argp
[-2]);
337 if (!(host
= *argp
++) || *host
== '-')
338 die("missing argument to %s", argp
[-2]);
342 if (!(port
= *argp
++) || *port
== '-')
343 die("missing argument to %s", argp
[-2]);
347 if (!(user
= *argp
++) || *user
== '-')
348 die("missing argument to %s", argp
[-2]);
363 if (!(saslmech
= *argp
++) || *saslmech
== '-')
364 die("missing argument to %s", argp
[-2]);
385 if (!(auth_svc
= *argp
++) || *auth_svc
== '-')
386 die("missing argument to %s", argp
[-2]);
388 die("not built with OAuth support");
393 if (!(proxy
= *argp
++) || *proxy
== '-')
394 die("missing argument to %s", argp
[-2]);
398 if (*cp
== '+' || *cp
== '@') {
400 die("only one folder at a time!");
401 folder
= pluspath (cp
);
403 die("usage: %s [+folder] [switches]", invo_name
);
407 /* NOTE: above this point you should use TRYDROPGROUPPRIVS(),
408 * not DROPGROUPPRIVS().
413 /* guarantee dropping group privileges; we might not have done so earlier */
416 /* Source of mail; -from overrides any -host. */
417 inc_type
= host
&& !from
? INC_POP
: INC_FILE
;
419 if (inc_type
== INC_POP
) {
420 /* Mail from a POP server. */
423 if (auth_svc
== NULL
) {
424 if (saslmech
&& ! strcasecmp(saslmech
, "xoauth2")) {
425 die("must specify -authservice with -saslmech xoauth2");
429 die("must specify -user with -saslmech xoauth2");
434 tlsflag
|= P_INITTLS
;
437 tlsflag
|= P_NOVERIFY
;
440 * initialize POP connection
442 if (pop_init (host
, port
, user
, proxy
, snoop
, sasl
, saslmech
,
443 tlsflag
, auth_svc
) == NOTOK
)
446 /* Check if there are any messages */
447 if (pop_stat (&nmsgs
, &nbytes
) == NOTOK
)
452 die("no mail to incorporate");
455 } else if (inc_type
== INC_FILE
) {
456 /* Mail from a spool file, or Maildir. */
459 else if ((newmail
= getenv ("MAILDROP")) && *newmail
)
460 newmail
= m_mailpath (newmail
);
461 else if ((newmail
= context_find ("maildrop")) && *newmail
)
462 newmail
= m_mailpath (newmail
);
464 newmail
= concat (MAILDIR
, "/", MAILFIL
, NULL
);
466 if (stat (newmail
, &s1
) == NOTOK
|| s1
.st_size
== 0)
467 die("no mail to incorporate");
468 if (s1
.st_mode
& S_IFDIR
) {
474 cp
= concat (newmail
, "/new", NULL
);
475 if ((md
= opendir(cp
)) == NULL
)
476 die("unable to open %s", cp
);
477 while ((de
= readdir (md
)) != NULL
) {
478 if (de
->d_name
[0] == '.')
480 if (i
>= num_maildir_entries
) {
481 if ((Maildir
= realloc(Maildir
, sizeof(*Maildir
) * (2*i
+16))) == NULL
)
482 die("not enough memory for %d messages", 2*i
+16);
483 num_maildir_entries
= 2*i
+16;
485 Maildir
[i
].filename
= concat (cp
, "/", de
->d_name
, NULL
);
486 if (stat(Maildir
[i
].filename
, &ms
) != 0)
487 adios (Maildir
[i
].filename
, "couldn't get delivery time");
488 Maildir
[i
].mtime
= ms
.st_mtime
;
493 cp
= concat (newmail
, "/cur", NULL
);
494 if ((md
= opendir(cp
)) == NULL
)
495 die("unable to open %s", cp
);
496 while ((de
= readdir (md
)) != NULL
) {
497 if (de
->d_name
[0] == '.')
499 if (i
>= num_maildir_entries
) {
500 if ((Maildir
= realloc(Maildir
, sizeof(*Maildir
) * (2*i
+16))) == NULL
)
501 die("not enough memory for %d messages", 2*i
+16);
502 num_maildir_entries
= 2*i
+16;
504 Maildir
[i
].filename
= concat (cp
, "/", de
->d_name
, NULL
);
505 if (stat(Maildir
[i
].filename
, &ms
) != 0)
506 adios (Maildir
[i
].filename
, "couldn't get delivery time");
507 Maildir
[i
].mtime
= ms
.st_mtime
;
513 die("no mail to incorporate");
514 num_maildir_entries
= i
;
515 qsort (Maildir
, num_maildir_entries
, sizeof(*Maildir
), maildir_srt
);
518 cp
= mh_xstrdup(newmail
);
522 if (!context_find ("path"))
523 free (path ("./", TFOLDER
));
525 folder
= getfolder (0);
526 maildir
= m_maildir (folder
);
527 maildir_copy
= mh_xstrdup(maildir
);
529 if (!folder_exists(maildir
)) {
530 /* If the folder doesn't exist, and we're given the -silent flag,
534 create_folder(maildir
, 0, done
);
539 if (chdir (maildir
) == NOTOK
)
540 adios (maildir
, "unable to change directory to");
542 /* read folder and create message structure */
543 if (!(mp
= folder_read (folder
, 0)))
544 die("unable to read folder %s", folder
);
546 if (inc_type
== INC_FILE
&& Maildir
== NULL
) {
547 /* Mail from a spool file. */
549 if (access (newmail
, W_OK
) != NOTOK
) {
552 SIGNAL (SIGHUP
, SIG_IGN
);
553 SIGNAL (SIGINT
, SIG_IGN
);
554 SIGNAL (SIGQUIT
, SIG_IGN
);
555 SIGNAL (SIGTERM
, SIG_IGN
);
558 GETGROUPPRIVS(); /* Reset gid to lock mail file */
559 in
= lkfopenspool (newmail
, "r");
562 die("unable to lock and fopen %s", newmail
);
563 fstat (fileno(in
), &s1
);
566 if ((in
= fopen (newmail
, "r")) == NULL
)
567 adios (newmail
, "unable to read");
571 /* This shouldn't be necessary but it can't hurt. */
576 if ((i
= stat (audfile
, &st
)) == NOTOK
)
577 inform("Creating Receive-Audit: %s", audfile
);
578 if ((aud
= fopen (audfile
, "a")) == NULL
)
579 adios (audfile
, "unable to append to");
581 chmod (audfile
, m_gmprot ());
584 fprintf (aud
, "<<inc>> %s -ms %s\n", dtimenow(0), from
);
587 fprintf (aud
, "<<inc>> %s -host %s -user %s\n", dtimenow(0),
590 fprintf (aud
, "<<inc>> %s\n", dtimenow (0));
594 /* Get new format string */
595 nfs
= new_fs (form
, format
, FORMAT
);
598 printf ("Incorporating new mail into %s...\n\n", folder
);
604 * Get the mail from a POP server
606 if (inc_type
== INC_POP
) {
607 /* Mail from a POP server. */
611 hghnum
= msgnum
= mp
->hghmsg
;
612 for (i
= 1; i
<= nmsgs
; i
++) {
613 charstring_t scanl
= NULL
;
616 cp
= mh_xstrdup(m_name (msgnum
));
617 if ((pf
= fopen (cp
, "w+")) == NULL
)
618 adios (cp
, "unable to write");
619 chmod (cp
, m_gmprot ());
623 if (pop_retr(i
, pop_action
, &pc
) == NOTOK
)
627 adios (cp
, "write error on");
628 fseek (pf
, 0L, SEEK_SET
);
629 switch (incerr
= scan (pf
, msgnum
, 0, nfs
, width
,
630 msgnum
== mp
->hghmsg
+ 1 && chgflag
,
631 1, NULL
, pc
.written
, noisy
, &scanl
)) {
633 printf ("%*d empty\n", DMAXFOLDER
, msgnum
);
639 /* advise (cp, "unable to read"); already advised */
650 fputs (charstring_buffer (scanl
), aud
);
655 charstring_free (scanl
);
657 if (ferror(pf
) || fclose (pf
)) {
659 (void) m_unlink (cp
);
662 adios (cp
, "write error on");
666 if (trnflag
&& pop_dele (i
) == NOTOK
)
672 if (pop_quit () == NOTOK
)
675 } else if (inc_type
== INC_FILE
&& Maildir
== NULL
) {
676 /* Mail from a spool file. */
678 scan_detect_mbox_style (in
); /* the MAGIC invocation... */
679 hghnum
= msgnum
= mp
->hghmsg
;
681 charstring_t scanl
= NULL
;
683 /* create scanline for new message */
684 switch (incerr
= scan (in
, msgnum
+ 1, msgnum
+ 1, nfs
, width
,
685 msgnum
== hghnum
&& chgflag
, 1, NULL
, 0L, noisy
,
693 fputs ("inc aborted!\n", aud
);
694 inform("aborted!"); /* doesn't clean up locks! */
698 inform("BUG in %s, number out of range", invo_name
);
702 inform("BUG in %s, scan() botch (%d)", invo_name
, incerr
);
708 * Run the external program hook on the message.
711 (void)snprintf(b
, sizeof (b
), "%s/%d", maildir_copy
, msgnum
+ 1);
712 (void)ext_hook("add-hook", b
, NULL
);
715 fputs (charstring_buffer (scanl
), aud
);
722 charstring_free (scanl
);
724 /* If we get here there was some sort of error from scan(),
725 * so stop processing anything more from the spool.
731 /* Mail from Maildir. */
736 hghnum
= msgnum
= mp
->hghmsg
;
737 for (i
= 0; i
< num_maildir_entries
; i
++) {
738 charstring_t scanl
= NULL
;
742 sp
= Maildir
[i
].filename
;
743 cp
= mh_xstrdup(m_name (msgnum
));
745 if (!trnflag
|| link(sp
, cp
) == -1) {
746 static char buf
[65536];
749 if ((sf
= fopen (sp
, "r")) == NULL
)
750 adios (sp
, "unable to read for copy");
751 if ((pf
= fopen (cp
, "w+")) == NULL
)
752 adios (cp
, "unable to write for copy");
753 while ((nrd
= fread(buf
, 1, sizeof(buf
), sf
)) > 0)
754 if (fwrite(buf
, 1, nrd
, pf
) != nrd
)
756 if (ferror(sf
) || fflush(pf
) || ferror(pf
)) {
758 fclose(pf
); fclose(sf
); (void) m_unlink(cp
);
760 adios(cp
, "copy error %s -> %s", sp
, cp
);
765 if (pf
== NULL
&& (pf
= fopen (cp
, "r")) == NULL
)
766 adios (cp
, "not available");
767 chmod (cp
, m_gmprot ());
769 fseek (pf
, 0L, SEEK_SET
);
770 switch (incerr
= scan (pf
, msgnum
, 0, nfs
, width
,
771 msgnum
== mp
->hghmsg
+ 1 && chgflag
,
772 1, NULL
, 0, noisy
, &scanl
)) {
774 printf ("%*d empty\n", DMAXFOLDER
, msgnum
);
780 /* advise (cp, "unable to read"); already advised */
791 * Run the external program hook on the message.
794 (void)snprintf(b
, sizeof (b
), "%s/%d", maildir_copy
, msgnum
+ 1);
795 (void)ext_hook("add-hook", b
, NULL
);
798 fputs (charstring_buffer (scanl
), aud
);
803 charstring_free (scanl
);
805 if (ferror(pf
) || fclose (pf
)) {
807 (void) m_unlink (cp
);
809 adios (cp
, "write error on");
814 if (trnflag
&& m_unlink (sp
) == NOTOK
)
815 adios (sp
, "couldn't unlink");
816 free (sp
); /* Free Maildir[i]->filename */
820 free (Maildir
); /* From now on Maildir is just a flag - don't dref! */
825 if (incerr
< 0) { /* error */
827 GETGROUPPRIVS(); /* Be sure we can unlock mail file */
828 (void) lkfclosespool (in
, newmail
); in
= NULL
;
829 DROPGROUPPRIVS(); /* And then return us to normal privileges */
831 fclose (in
); in
= NULL
;
842 if (inc_type
== INC_FILE
&& Maildir
== NULL
) {
843 /* Mail from a spool file; truncate it. */
846 if (stat (newmail
, &st
) != NOTOK
&& s1
.st_mtime
!= st
.st_mtime
)
847 inform("new messages have arrived!\007");
850 if ((newfd
= creat (newmail
, 0600)) != NOTOK
)
853 admonish (newmail
, "error zero'ing");
857 printf ("%s not zero'd\n", newmail
);
861 if (msgnum
== hghnum
) {
862 inform("no messages incorporated, continuing...");
865 * Lock the sequence file now, and loop to set the right flags
866 * in the folder structure
872 context_replace (pfolder
, folder
); /* update current folder */
874 if ((mp2
= folder_read(folder
, 1)) == NULL
) {
875 inform("Unable to reread folder %s, continuing...", folder
);
880 * Shouldn't happen, but just in case ...
883 if (msgnum
>= mp2
->hghoff
884 && !(mp2
= folder_realloc (mp2
, mp2
->lowoff
, msgnum
+ 1))) {
885 inform("unable to reallocate folder storage");
890 mp2
->curmsg
= hghnum
+ 1;
891 mp2
->hghmsg
= msgnum
;
893 if (mp2
->lowmsg
== 0)
895 if (chgflag
) /* sigh... */
896 seq_setcur (mp2
, mp2
->curmsg
);
898 for (i
= hghnum
+ 1; i
<= msgnum
; i
++) {
899 clear_msg_flags (mp2
, i
);
903 mp2
->msgflags
|= SEQMOD
;
904 seq_setunseen(mp2
, 0); /* Set the Unseen-Sequence */
905 seq_save(mp2
); /* Save the sequence file */
910 if (inc_type
== INC_FILE
&& Maildir
== NULL
) {
911 /* Mail from a spool file; unlock it. */
914 GETGROUPPRIVS(); /* Be sure we can unlock mail file */
915 (void) lkfclosespool (in
, newmail
); in
= NULL
;
916 DROPGROUPPRIVS(); /* And then return us to normal privileges */
918 fclose (in
); in
= NULL
;
922 context_save (); /* save the context file */
929 inc_done (int status
)
935 lkfclosespool(in
, newmail
);
942 pop_action(void *closure
, char *s
)
948 n
= fprintf(pc
->mailout
, "%s\n", s
);
951 pc
->written
+= n
; /* Count linefeed too. */