]>
diplodocus.org Git - nmh/blob - uip/inc.c
e40b730684e9ba38caca762945e400e3acababa3
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("tls", TLSminc(-3), TLSSW) \
100 X("initialtls", TLSminc(-10), INITTLSSW) \
101 X("notls", TLSminc(-5), NOTLSSW) \
102 X("certverify", TLSminc(-10), CERTVERSW) \
103 X("nocertverify", TLSminc(-12), NOCERTVERSW) \
104 X("authservice", 0, AUTHSERVICESW) \
105 X("proxy command", 0, PROXYSW) \
107 #define X(sw, minchars, id) id,
108 DEFINE_SWITCH_ENUM(INC
);
111 #define X(sw, minchars, id) { sw, minchars, id },
112 DEFINE_SWITCH_ARRAY(INC
, switches
);
116 * flags for the mail source
121 static struct Maildir_entry
{
125 static int num_maildir_entries
= 0;
133 extern char response
[];
135 /* This is an attempt to simplify things by putting all the
136 * privilege ops into macros.
137 * *GROUPPRIVS() is related to handling the setgid MAIL property,
138 * and only applies if MAILGROUP is defined.
139 * Basically, SAVEGROUPPRIVS() is called right at the top of main()
140 * to initialise things, and then DROPGROUPPRIVS() and GETGROUPPRIVS()
141 * do the obvious thing.
143 * There's probably a better implementation if we're allowed to use
144 * BSD-style setreuid() rather than using POSIX saved-ids.
145 * Anyway, if you're euid root it's a bit pointless to drop the group
148 * I'm pretty happy that the security is good provided we aren't setuid root.
149 * The only things we trust with group=mail privilege are lkfopen()
154 * For setting and returning to "mail" gid
157 static gid_t return_gid
;
158 #define TRYDROPGROUPPRIVS() DROPGROUPPRIVS()
159 #define DROPGROUPPRIVS() \
160 if (setegid(getgid()) != 0) { \
161 adios ("setegid", "unable to restore group to %ld", (long) getgid()); \
163 #define GETGROUPPRIVS() \
164 if (setegid(return_gid) != 0) { \
165 adios ("setegid", "unable to set group to %ld", (long) return_gid); \
167 #define SAVEGROUPPRIVS() return_gid = getegid()
169 /* define *GROUPPRIVS() as null; this avoids having lots of "#ifdef MAILGROUP"s */
170 #define TRYDROPGROUPPRIVS()
171 #define DROPGROUPPRIVS()
172 #define GETGROUPPRIVS()
173 #define SAVEGROUPPRIVS()
174 #endif /* not MAILGROUP */
176 /* these variables have to be globals so that done() can correctly clean up the lockfile */
178 static char *newmail
;
184 static int maildir_srt(const void *va
, const void *vb
) PURE
;
185 static void inc_done(int) NORETURN
;
186 static int pop_action(void *closure
, char *);
189 maildir_srt(const void *va
, const void *vb
)
191 const struct Maildir_entry
*a
= va
, *b
= vb
;
192 if (a
->mtime
> b
->mtime
)
194 if (a
->mtime
< b
->mtime
)
200 main (int argc
, char **argv
)
207 int hghnum
= 0, msgnum
= 0;
211 int incerr
= 0; /* <0 if inc hits an error which means it should not truncate mailspool */
212 char *cp
, *maildir
= NULL
, *folder
= NULL
;
213 char *format
= NULL
, *form
= NULL
;
214 char *host
= NULL
, *port
= NULL
, *user
= NULL
, *proxy
= NULL
;
215 char *audfile
= NULL
, *from
= NULL
, *saslmech
= NULL
, *auth_svc
= NULL
;
216 char buf
[BUFSIZ
], **argp
, *nfs
, **arguments
;
217 struct msgs
*mp
= NULL
;
220 char b
[PATH_MAX
+ 1];
221 char *maildir_copy
= NULL
; /* copy of mail directory because the static gets overwritten */
222 charstring_t scanl
= NULL
;
225 char *MAILHOST_env_variable
;
228 /* absolutely the first thing we do is save our privileges,
229 * and drop them if we can.
234 if (nmh_init(argv
[0], true, true)) { return 1; }
237 arguments
= getarguments (invo_name
, argc
, argv
, 1);
242 * use MAILHOST environment variable if present,
244 * If that fails, use the default (if any)
245 * provided by mts.conf in mts_init()
247 if ((MAILHOST_env_variable
= getenv("MAILHOST")) != NULL
)
248 pophost
= MAILHOST_env_variable
;
250 * If there is a valid "pophost" entry in mts.conf,
251 * then use it as the default host.
253 if (pophost
&& *pophost
)
257 chgflag
= noisy
= noverify
= true;
258 while ((cp
= *argp
++)) {
260 switch (smatch (++cp
, switches
)) {
262 ambigsw (cp
, switches
);
265 die("-%s unknown", cp
);
268 snprintf (buf
, sizeof(buf
), "%s [+folder] [switches]", invo_name
);
269 print_help (buf
, switches
, 1);
272 print_version(invo_name
);
276 if (!(cp
= *argp
++) || *cp
== '-')
277 die("missing argument to %s", argp
[-2]);
278 audfile
= mh_xstrdup(m_maildir(cp
));
292 * The flag `trnflag' has the value:
294 * 2 if -truncate is given
295 * 1 by default (truncating is default)
296 * 0 if -notruncate is given
306 if (!(cp
= *argp
++) || *cp
== '-')
307 die("missing argument to %s", argp
[-2]);
308 from
= path (cp
, TFILE
);
311 * If the truncate file is in default state,
312 * change to not truncate.
326 if (!(form
= *argp
++) || *form
== '-')
327 die("missing argument to %s", argp
[-2]);
331 if (!(format
= *argp
++) || *format
== '-')
332 die("missing argument to %s", argp
[-2]);
337 if (!(cp
= *argp
++) || *cp
== '-')
338 die("missing argument to %s", argp
[-2]);
343 if (!(host
= *argp
++) || *host
== '-')
344 die("missing argument to %s", argp
[-2]);
348 if (!(port
= *argp
++) || *port
== '-')
349 die("missing argument to %s", argp
[-2]);
353 if (!(user
= *argp
++) || *user
== '-')
354 die("missing argument to %s", argp
[-2]);
369 if (!(saslmech
= *argp
++) || *saslmech
== '-')
370 die("missing argument to %s", argp
[-2]);
395 if (!(auth_svc
= *argp
++) || *auth_svc
== '-')
396 die("missing argument to %s", argp
[-2]);
398 die("not built with OAuth support");
403 if (!(proxy
= *argp
++) || *proxy
== '-')
404 die("missing argument to %s", argp
[-2]);
408 if (*cp
== '+' || *cp
== '@') {
410 die("only one folder at a time!");
411 folder
= pluspath (cp
);
413 die("usage: %s [+folder] [switches]", invo_name
);
417 /* NOTE: above this point you should use TRYDROPGROUPPRIVS(),
418 * not DROPGROUPPRIVS().
423 /* guarantee dropping group privileges; we might not have done so earlier */
426 /* Source of mail; -from overrides any -host. */
427 inc_type
= host
&& !from
? INC_POP
: INC_FILE
;
429 if (inc_type
== INC_POP
) {
430 /* Mail from a POP server. */
433 if (auth_svc
== NULL
) {
434 if (saslmech
&& ! strcasecmp(saslmech
, "xoauth2")) {
435 die("must specify -authservice with -saslmech xoauth2");
439 die("must specify -user with -saslmech xoauth2");
444 tlsflag
= P_STARTTLS
;
449 tlsflag
|= P_NOVERIFY
;
452 * initialize POP connection
454 if (pop_init (host
, port
, user
, proxy
, snoop
, sasl
, saslmech
,
455 tlsflag
, auth_svc
) == NOTOK
)
458 /* Check if there are any messages */
459 if (pop_stat (&nmsgs
, &nbytes
) == NOTOK
)
464 die("no mail to incorporate");
467 } else if (inc_type
== INC_FILE
) {
468 /* Mail from a spool file, or Maildir. */
471 else if ((newmail
= getenv ("MAILDROP")) && *newmail
)
472 newmail
= m_mailpath (newmail
);
473 else if ((newmail
= context_find ("maildrop")) && *newmail
)
474 newmail
= m_mailpath (newmail
);
476 newmail
= concat (MAILDIR
, "/", MAILFIL
, NULL
);
478 if (stat (newmail
, &s1
) == NOTOK
|| s1
.st_size
== 0)
479 die("no mail to incorporate");
480 if (s1
.st_mode
& S_IFDIR
) {
486 cp
= concat (newmail
, "/new", NULL
);
487 if ((md
= opendir(cp
)) == NULL
)
488 die("unable to open %s", cp
);
489 while ((de
= readdir (md
)) != NULL
) {
490 if (de
->d_name
[0] == '.')
492 if (i
>= num_maildir_entries
) {
493 if ((Maildir
= realloc(Maildir
, sizeof(*Maildir
) * (2*i
+16))) == NULL
)
494 die("not enough memory for %d messages", 2*i
+16);
495 num_maildir_entries
= 2*i
+16;
497 Maildir
[i
].filename
= concat (cp
, "/", de
->d_name
, NULL
);
498 if (stat(Maildir
[i
].filename
, &ms
) != 0)
499 adios (Maildir
[i
].filename
, "couldn't get delivery time");
500 Maildir
[i
].mtime
= ms
.st_mtime
;
505 cp
= concat (newmail
, "/cur", NULL
);
506 if ((md
= opendir(cp
)) == NULL
)
507 die("unable to open %s", cp
);
508 while ((de
= readdir (md
)) != NULL
) {
509 if (de
->d_name
[0] == '.')
511 if (i
>= num_maildir_entries
) {
512 if ((Maildir
= realloc(Maildir
, sizeof(*Maildir
) * (2*i
+16))) == NULL
)
513 die("not enough memory for %d messages", 2*i
+16);
514 num_maildir_entries
= 2*i
+16;
516 Maildir
[i
].filename
= concat (cp
, "/", de
->d_name
, NULL
);
517 if (stat(Maildir
[i
].filename
, &ms
) != 0)
518 adios (Maildir
[i
].filename
, "couldn't get delivery time");
519 Maildir
[i
].mtime
= ms
.st_mtime
;
525 die("no mail to incorporate");
526 num_maildir_entries
= i
;
527 qsort (Maildir
, num_maildir_entries
, sizeof(*Maildir
), maildir_srt
);
530 cp
= mh_xstrdup(newmail
);
534 if (!context_find ("path"))
535 free (path ("./", TFOLDER
));
537 folder
= getfolder (0);
538 maildir
= m_maildir (folder
);
539 maildir_copy
= mh_xstrdup(maildir
);
541 if (!folder_exists(maildir
)) {
542 /* If the folder doesn't exist, and we're given the -silent flag,
546 create_folder(maildir
, 0, done
);
551 if (chdir (maildir
) == NOTOK
)
552 adios (maildir
, "unable to change directory to");
554 /* read folder and create message structure */
555 if (!(mp
= folder_read (folder
, 0)))
556 die("unable to read folder %s", folder
);
558 if (inc_type
== INC_FILE
&& Maildir
== NULL
) {
559 /* Mail from a spool file. */
561 if (access (newmail
, W_OK
) != NOTOK
) {
564 SIGNAL (SIGHUP
, SIG_IGN
);
565 SIGNAL (SIGINT
, SIG_IGN
);
566 SIGNAL (SIGQUIT
, SIG_IGN
);
567 SIGNAL (SIGTERM
, SIG_IGN
);
570 GETGROUPPRIVS(); /* Reset gid to lock mail file */
571 in
= lkfopenspool (newmail
, "r");
574 die("unable to lock and fopen %s", newmail
);
575 fstat (fileno(in
), &s1
);
578 if ((in
= fopen (newmail
, "r")) == NULL
)
579 adios (newmail
, "unable to read");
583 /* This shouldn't be necessary but it can't hurt. */
588 if ((i
= stat (audfile
, &st
)) == NOTOK
)
589 inform("Creating Receive-Audit: %s", audfile
);
590 if ((aud
= fopen (audfile
, "a")) == NULL
)
591 adios (audfile
, "unable to append to");
593 chmod (audfile
, m_gmprot ());
596 fprintf (aud
, "<<inc>> %s -ms %s\n", dtimenow(0), from
);
599 fprintf (aud
, "<<inc>> %s -host %s -user %s\n", dtimenow(0),
602 fprintf (aud
, "<<inc>> %s\n", dtimenow (0));
606 /* Get new format string */
607 nfs
= new_fs (form
, format
, FORMAT
);
610 printf ("Incorporating new mail into %s...\n\n", folder
);
616 * Get the mail from a POP server
618 if (inc_type
== INC_POP
) {
619 /* Mail from a POP server. */
623 hghnum
= msgnum
= mp
->hghmsg
;
624 for (i
= 1; i
<= nmsgs
; i
++) {
627 cp
= mh_xstrdup(m_name (msgnum
));
628 if ((pf
= fopen (cp
, "w+")) == NULL
)
629 adios (cp
, "unable to write");
630 chmod (cp
, m_gmprot ());
634 if (pop_retr(i
, pop_action
, &pc
) == NOTOK
)
638 adios (cp
, "write error on");
639 fseek (pf
, 0L, SEEK_SET
);
640 switch (incerr
= scan (pf
, msgnum
, 0, nfs
, width
,
641 msgnum
== mp
->hghmsg
+ 1 && chgflag
,
642 1, NULL
, pc
.written
, noisy
, &scanl
)) {
644 printf ("%*d empty\n", DMAXFOLDER
, msgnum
);
650 /* advise (cp, "unable to read"); already advised */
661 fputs (charstring_buffer (scanl
), aud
);
668 charstring_clear (scanl
);
670 if (ferror(pf
) || fclose (pf
)) {
672 (void) m_unlink (cp
);
675 adios (cp
, "write error on");
679 if (trnflag
&& pop_dele (i
) == NOTOK
)
685 charstring_free (scanl
);
688 if (pop_quit () == NOTOK
)
691 } else if (inc_type
== INC_FILE
&& Maildir
== NULL
) {
692 /* Mail from a spool file. */
694 scan_detect_mbox_style (in
); /* the MAGIC invocation... */
695 hghnum
= msgnum
= mp
->hghmsg
;
697 /* create scanline for new message */
698 switch (incerr
= scan (in
, msgnum
+ 1, msgnum
+ 1, nfs
, width
,
699 msgnum
== hghnum
&& chgflag
, 1, NULL
, 0L, noisy
,
707 fputs ("inc aborted!\n", aud
);
708 inform("aborted!"); /* doesn't clean up locks! */
712 inform("BUG in %s, number out of range", invo_name
);
716 inform("BUG in %s, scan() botch (%d)", invo_name
, incerr
);
722 * Run the external program hook on the message.
725 (void)snprintf(b
, sizeof (b
), "%s/%d", maildir_copy
, msgnum
+ 1);
726 (void)ext_hook("add-hook", b
, NULL
);
729 fputs (charstring_buffer (scanl
), aud
);
733 charstring_clear (scanl
);
738 /* If we get here there was some sort of error from scan(),
739 * so stop processing anything more from the spool.
743 charstring_free (scanl
);
747 /* Mail from Maildir. */
752 hghnum
= msgnum
= mp
->hghmsg
;
753 for (i
= 0; i
< num_maildir_entries
; i
++) {
756 sp
= Maildir
[i
].filename
;
757 cp
= mh_xstrdup(m_name (msgnum
));
759 if (!trnflag
|| link(sp
, cp
) == -1) {
760 static char buf
[65536];
763 if ((sf
= fopen (sp
, "r")) == NULL
)
764 adios (sp
, "unable to read for copy");
765 if ((pf
= fopen (cp
, "w+")) == NULL
)
766 adios (cp
, "unable to write for copy");
767 while ((nrd
= fread(buf
, 1, sizeof(buf
), sf
)) > 0)
768 if (fwrite(buf
, 1, nrd
, pf
) != nrd
)
770 if (ferror(sf
) || fflush(pf
) || ferror(pf
)) {
772 fclose(pf
); fclose(sf
); (void) m_unlink(cp
);
774 adios(cp
, "copy error %s -> %s", sp
, cp
);
779 if (pf
== NULL
&& (pf
= fopen (cp
, "r")) == NULL
)
780 adios (cp
, "not available");
781 chmod (cp
, m_gmprot ());
783 fseek (pf
, 0L, SEEK_SET
);
784 switch (incerr
= scan (pf
, msgnum
, 0, nfs
, width
,
785 msgnum
== mp
->hghmsg
+ 1 && chgflag
,
786 1, NULL
, 0, noisy
, &scanl
)) {
788 printf ("%*d empty\n", DMAXFOLDER
, msgnum
);
794 /* advise (cp, "unable to read"); already advised */
805 * Run the external program hook on the message.
808 (void)snprintf(b
, sizeof (b
), "%s/%d", maildir_copy
, msgnum
+ 1);
809 (void)ext_hook("add-hook", b
, NULL
);
812 fputs (charstring_buffer (scanl
), aud
);
817 charstring_clear (scanl
);
819 if (ferror(pf
) || fclose (pf
)) {
821 (void) m_unlink (cp
);
823 adios (cp
, "write error on");
828 if (trnflag
&& m_unlink (sp
) == NOTOK
)
829 adios (sp
, "couldn't unlink");
830 free (sp
); /* Free Maildir[i]->filename */
834 free (Maildir
); /* From now on Maildir is just a flag - don't dref! */
835 charstring_free (scanl
);
841 if (incerr
< 0) { /* error */
843 GETGROUPPRIVS(); /* Be sure we can unlock mail file */
844 (void) lkfclosespool (in
, newmail
); in
= NULL
;
845 DROPGROUPPRIVS(); /* And then return us to normal privileges */
847 fclose (in
); in
= NULL
;
858 if (inc_type
== INC_FILE
&& Maildir
== NULL
) {
859 /* Mail from a spool file; truncate it. */
862 if (stat (newmail
, &st
) != NOTOK
&& s1
.st_mtime
!= st
.st_mtime
)
863 inform("new messages have arrived!\007");
866 if ((newfd
= creat (newmail
, 0600)) != NOTOK
)
869 admonish (newmail
, "error zero'ing");
873 printf ("%s not zero'd\n", newmail
);
877 if (msgnum
== hghnum
) {
878 inform("no messages incorporated, continuing...");
881 * Lock the sequence file now, and loop to set the right flags
882 * in the folder structure
888 context_replace (pfolder
, folder
); /* update current folder */
890 if ((mp2
= folder_read(folder
, 1)) == NULL
) {
891 inform("Unable to reread folder %s, continuing...", folder
);
896 * Shouldn't happen, but just in case ...
899 if (msgnum
>= mp2
->hghoff
900 && !(mp2
= folder_realloc (mp2
, mp2
->lowoff
, msgnum
+ 1))) {
901 inform("unable to reallocate folder storage");
906 mp2
->curmsg
= hghnum
+ 1;
907 mp2
->hghmsg
= msgnum
;
909 if (mp2
->lowmsg
== 0)
911 if (chgflag
) /* sigh... */
912 seq_setcur (mp2
, mp2
->curmsg
);
914 for (i
= hghnum
+ 1; i
<= msgnum
; i
++) {
915 clear_msg_flags (mp2
, i
);
919 mp2
->msgflags
|= SEQMOD
;
920 seq_setunseen(mp2
, 0); /* Set the Unseen-Sequence */
921 seq_save(mp2
); /* Save the sequence file */
926 if (inc_type
== INC_FILE
&& Maildir
== NULL
) {
927 /* Mail from a spool file; unlock it. */
930 GETGROUPPRIVS(); /* Be sure we can unlock mail file */
931 (void) lkfclosespool (in
, newmail
); in
= NULL
;
932 DROPGROUPPRIVS(); /* And then return us to normal privileges */
934 fclose (in
); in
= NULL
;
938 context_save (); /* save the context file */
945 inc_done (int status
)
951 lkfclosespool(in
, newmail
);
958 pop_action(void *closure
, char *s
)
964 n
= fprintf(pc
->mailout
, "%s\n", s
);
967 pc
->written
+= n
; /* Count linefeed too. */