]>
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/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("initialtls", TLSminc(-10), INITTLSSW) \
100 X("notls", TLSminc(-5), NOTLSSW) \
101 X("certverify", TLSminc(-10), CERTVERSW) \
102 X("nocertverify", TLSminc(-12), NOCERTVERSW) \
103 X("authservice", 0, AUTHSERVICESW) \
104 X("proxy command", 0, PROXYSW) \
106 #define X(sw, minchars, id) id,
107 DEFINE_SWITCH_ENUM(INC
);
110 #define X(sw, minchars, id) { sw, minchars, id },
111 DEFINE_SWITCH_ARRAY(INC
, switches
);
115 * flags for the mail source
120 static struct Maildir_entry
{
124 static int num_maildir_entries
= 0;
132 extern char response
[];
134 /* This is an attempt to simplify things by putting all the
135 * privilege ops into macros.
136 * *GROUPPRIVS() is related to handling the setgid MAIL property,
137 * and only applies if MAILGROUP is defined.
138 * Basically, SAVEGROUPPRIVS() is called right at the top of main()
139 * to initialise things, and then DROPGROUPPRIVS() and GETGROUPPRIVS()
140 * do the obvious thing.
142 * There's probably a better implementation if we're allowed to use
143 * BSD-style setreuid() rather than using POSIX saved-ids.
144 * Anyway, if you're euid root it's a bit pointless to drop the group
147 * I'm pretty happy that the security is good provided we aren't setuid root.
148 * The only things we trust with group=mail privilege are lkfopen()
153 * For setting and returning to "mail" gid
156 static gid_t return_gid
;
157 #define TRYDROPGROUPPRIVS() DROPGROUPPRIVS()
158 #define DROPGROUPPRIVS() \
159 if (setegid(getgid()) != 0) { \
160 adios ("setegid", "unable to restore group to %ld", (long) getgid()); \
162 #define GETGROUPPRIVS() \
163 if (setegid(return_gid) != 0) { \
164 adios ("setegid", "unable to set group to %ld", (long) return_gid); \
166 #define SAVEGROUPPRIVS() return_gid = getegid()
168 /* define *GROUPPRIVS() as null; this avoids having lots of "#ifdef MAILGROUP"s */
169 #define TRYDROPGROUPPRIVS()
170 #define DROPGROUPPRIVS()
171 #define GETGROUPPRIVS()
172 #define SAVEGROUPPRIVS()
173 #endif /* not MAILGROUP */
175 /* these variables have to be globals so that done() can correctly clean up the lockfile */
177 static char *newmail
;
183 static int maildir_srt(const void *va
, const void *vb
) PURE
;
184 static void inc_done(int) NORETURN
;
185 static int pop_action(void *closure
, char *);
188 maildir_srt(const void *va
, const void *vb
)
190 const struct Maildir_entry
*a
= va
, *b
= vb
;
191 if (a
->mtime
> b
->mtime
)
193 if (a
->mtime
< b
->mtime
)
199 main (int argc
, char **argv
)
206 int hghnum
= 0, msgnum
= 0;
208 bool sasl
, tls
, noverify
;
209 int incerr
= 0; /* <0 if inc hits an error which means it should not truncate mailspool */
210 char *cp
, *maildir
= NULL
, *folder
= NULL
;
211 char *format
= NULL
, *form
= NULL
;
212 char *host
= NULL
, *port
= NULL
, *user
= NULL
, *proxy
= NULL
;
213 char *audfile
= NULL
, *from
= NULL
, *saslmech
= NULL
, *auth_svc
= NULL
;
214 char buf
[BUFSIZ
], **argp
, *nfs
, **arguments
;
215 struct msgs
*mp
= NULL
;
218 char b
[PATH_MAX
+ 1];
219 char *maildir_copy
= NULL
; /* copy of mail directory because the static gets overwritten */
222 char *MAILHOST_env_variable
;
225 /* absolutely the first thing we do is save our privileges,
226 * and drop them if we can.
231 if (nmh_init(argv
[0], true, true)) { return 1; }
234 arguments
= getarguments (invo_name
, argc
, argv
, 1);
239 * use MAILHOST environment variable if present,
241 * If that fails, use the default (if any)
242 * provided by mts.conf in mts_init()
244 if ((MAILHOST_env_variable
= getenv("MAILHOST")) != NULL
)
245 pophost
= MAILHOST_env_variable
;
247 * If there is a valid "pophost" entry in mts.conf,
248 * then use it as the default host.
250 if (pophost
&& *pophost
)
254 chgflag
= noisy
= noverify
= true;
255 while ((cp
= *argp
++)) {
257 switch (smatch (++cp
, switches
)) {
259 ambigsw (cp
, switches
);
262 die("-%s unknown", cp
);
265 snprintf (buf
, sizeof(buf
), "%s [+folder] [switches]", invo_name
);
266 print_help (buf
, switches
, 1);
269 print_version(invo_name
);
273 if (!(cp
= *argp
++) || *cp
== '-')
274 die("missing argument to %s", argp
[-2]);
275 audfile
= mh_xstrdup(m_maildir(cp
));
289 * The flag `trnflag' has the value:
291 * 2 if -truncate is given
292 * 1 by default (truncating is default)
293 * 0 if -notruncate is given
303 if (!(cp
= *argp
++) || *cp
== '-')
304 die("missing argument to %s", argp
[-2]);
305 from
= path (cp
, TFILE
);
308 * If the truncate file is in default state,
309 * change to not truncate.
323 if (!(form
= *argp
++) || *form
== '-')
324 die("missing argument to %s", argp
[-2]);
328 if (!(format
= *argp
++) || *format
== '-')
329 die("missing argument to %s", argp
[-2]);
334 if (!(cp
= *argp
++) || *cp
== '-')
335 die("missing argument to %s", argp
[-2]);
340 if (!(host
= *argp
++) || *host
== '-')
341 die("missing argument to %s", argp
[-2]);
345 if (!(port
= *argp
++) || *port
== '-')
346 die("missing argument to %s", argp
[-2]);
350 if (!(user
= *argp
++) || *user
== '-')
351 die("missing argument to %s", argp
[-2]);
366 if (!(saslmech
= *argp
++) || *saslmech
== '-')
367 die("missing argument to %s", argp
[-2]);
388 if (!(auth_svc
= *argp
++) || *auth_svc
== '-')
389 die("missing argument to %s", argp
[-2]);
391 die("not built with OAuth support");
396 if (!(proxy
= *argp
++) || *proxy
== '-')
397 die("missing argument to %s", argp
[-2]);
401 if (*cp
== '+' || *cp
== '@') {
403 die("only one folder at a time!");
404 folder
= pluspath (cp
);
406 die("usage: %s [+folder] [switches]", invo_name
);
410 /* NOTE: above this point you should use TRYDROPGROUPPRIVS(),
411 * not DROPGROUPPRIVS().
416 /* guarantee dropping group privileges; we might not have done so earlier */
419 /* Source of mail; -from overrides any -host. */
420 inc_type
= host
&& !from
? INC_POP
: INC_FILE
;
422 if (inc_type
== INC_POP
) {
423 /* Mail from a POP server. */
426 if (auth_svc
== NULL
) {
427 if (saslmech
&& ! strcasecmp(saslmech
, "xoauth2")) {
428 die("must specify -authservice with -saslmech xoauth2");
432 die("must specify -user with -saslmech xoauth2");
437 tlsflag
|= P_INITTLS
;
440 tlsflag
|= P_NOVERIFY
;
443 * initialize POP connection
445 if (pop_init (host
, port
, user
, proxy
, snoop
, sasl
, saslmech
,
446 tlsflag
, auth_svc
) == NOTOK
)
449 /* Check if there are any messages */
450 if (pop_stat (&nmsgs
, &nbytes
) == NOTOK
)
455 die("no mail to incorporate");
458 } else if (inc_type
== INC_FILE
) {
459 /* Mail from a spool file, or Maildir. */
462 else if ((newmail
= getenv ("MAILDROP")) && *newmail
)
463 newmail
= m_mailpath (newmail
);
464 else if ((newmail
= context_find ("maildrop")) && *newmail
)
465 newmail
= m_mailpath (newmail
);
467 newmail
= concat (MAILDIR
, "/", MAILFIL
, NULL
);
469 if (stat (newmail
, &s1
) == NOTOK
|| s1
.st_size
== 0)
470 die("no mail to incorporate");
471 if (s1
.st_mode
& S_IFDIR
) {
477 cp
= concat (newmail
, "/new", NULL
);
478 if ((md
= opendir(cp
)) == NULL
)
479 die("unable to open %s", cp
);
480 while ((de
= readdir (md
)) != NULL
) {
481 if (de
->d_name
[0] == '.')
483 if (i
>= num_maildir_entries
) {
484 if ((Maildir
= realloc(Maildir
, sizeof(*Maildir
) * (2*i
+16))) == NULL
)
485 die("not enough memory for %d messages", 2*i
+16);
486 num_maildir_entries
= 2*i
+16;
488 Maildir
[i
].filename
= concat (cp
, "/", de
->d_name
, NULL
);
489 if (stat(Maildir
[i
].filename
, &ms
) != 0)
490 adios (Maildir
[i
].filename
, "couldn't get delivery time");
491 Maildir
[i
].mtime
= ms
.st_mtime
;
496 cp
= concat (newmail
, "/cur", NULL
);
497 if ((md
= opendir(cp
)) == NULL
)
498 die("unable to open %s", cp
);
499 while ((de
= readdir (md
)) != NULL
) {
500 if (de
->d_name
[0] == '.')
502 if (i
>= num_maildir_entries
) {
503 if ((Maildir
= realloc(Maildir
, sizeof(*Maildir
) * (2*i
+16))) == NULL
)
504 die("not enough memory for %d messages", 2*i
+16);
505 num_maildir_entries
= 2*i
+16;
507 Maildir
[i
].filename
= concat (cp
, "/", de
->d_name
, NULL
);
508 if (stat(Maildir
[i
].filename
, &ms
) != 0)
509 adios (Maildir
[i
].filename
, "couldn't get delivery time");
510 Maildir
[i
].mtime
= ms
.st_mtime
;
516 die("no mail to incorporate");
517 num_maildir_entries
= i
;
518 qsort (Maildir
, num_maildir_entries
, sizeof(*Maildir
), maildir_srt
);
521 cp
= mh_xstrdup(newmail
);
525 if (!context_find ("path"))
526 free (path ("./", TFOLDER
));
528 folder
= getfolder (0);
529 maildir
= m_maildir (folder
);
530 maildir_copy
= mh_xstrdup(maildir
);
532 if (!folder_exists(maildir
)) {
533 /* If the folder doesn't exist, and we're given the -silent flag,
537 create_folder(maildir
, 0, done
);
542 if (chdir (maildir
) == NOTOK
)
543 adios (maildir
, "unable to change directory to");
545 /* read folder and create message structure */
546 if (!(mp
= folder_read (folder
, 0)))
547 die("unable to read folder %s", folder
);
549 if (inc_type
== INC_FILE
&& Maildir
== NULL
) {
550 /* Mail from a spool file. */
552 if (access (newmail
, W_OK
) != NOTOK
) {
555 SIGNAL (SIGHUP
, SIG_IGN
);
556 SIGNAL (SIGINT
, SIG_IGN
);
557 SIGNAL (SIGQUIT
, SIG_IGN
);
558 SIGNAL (SIGTERM
, SIG_IGN
);
561 GETGROUPPRIVS(); /* Reset gid to lock mail file */
562 in
= lkfopenspool (newmail
, "r");
565 die("unable to lock and fopen %s", newmail
);
566 fstat (fileno(in
), &s1
);
569 if ((in
= fopen (newmail
, "r")) == NULL
)
570 adios (newmail
, "unable to read");
574 /* This shouldn't be necessary but it can't hurt. */
579 if ((i
= stat (audfile
, &st
)) == NOTOK
)
580 inform("Creating Receive-Audit: %s", audfile
);
581 if ((aud
= fopen (audfile
, "a")) == NULL
)
582 adios (audfile
, "unable to append to");
584 chmod (audfile
, m_gmprot ());
587 fprintf (aud
, "<<inc>> %s -ms %s\n", dtimenow(0), from
);
590 fprintf (aud
, "<<inc>> %s -host %s -user %s\n", dtimenow(0),
593 fprintf (aud
, "<<inc>> %s\n", dtimenow (0));
597 /* Get new format string */
598 nfs
= new_fs (form
, format
, FORMAT
);
601 printf ("Incorporating new mail into %s...\n\n", folder
);
607 * Get the mail from a POP server
609 if (inc_type
== INC_POP
) {
610 /* Mail from a POP server. */
614 hghnum
= msgnum
= mp
->hghmsg
;
615 for (i
= 1; i
<= nmsgs
; i
++) {
616 charstring_t scanl
= NULL
;
619 cp
= mh_xstrdup(m_name (msgnum
));
620 if ((pf
= fopen (cp
, "w+")) == NULL
)
621 adios (cp
, "unable to write");
622 chmod (cp
, m_gmprot ());
626 if (pop_retr(i
, pop_action
, &pc
) == NOTOK
)
630 adios (cp
, "write error on");
631 fseek (pf
, 0L, SEEK_SET
);
632 switch (incerr
= scan (pf
, msgnum
, 0, nfs
, width
,
633 msgnum
== mp
->hghmsg
+ 1 && chgflag
,
634 1, NULL
, pc
.written
, noisy
, &scanl
)) {
636 printf ("%*d empty\n", DMAXFOLDER
, msgnum
);
642 /* advise (cp, "unable to read"); already advised */
653 fputs (charstring_buffer (scanl
), aud
);
658 charstring_free (scanl
);
660 if (ferror(pf
) || fclose (pf
)) {
662 (void) m_unlink (cp
);
665 adios (cp
, "write error on");
669 if (trnflag
&& pop_dele (i
) == NOTOK
)
675 if (pop_quit () == NOTOK
)
678 } else if (inc_type
== INC_FILE
&& Maildir
== NULL
) {
679 /* Mail from a spool file. */
681 scan_detect_mbox_style (in
); /* the MAGIC invocation... */
682 hghnum
= msgnum
= mp
->hghmsg
;
684 charstring_t scanl
= NULL
;
686 /* create scanline for new message */
687 switch (incerr
= scan (in
, msgnum
+ 1, msgnum
+ 1, nfs
, width
,
688 msgnum
== hghnum
&& chgflag
, 1, NULL
, 0L, noisy
,
696 fputs ("inc aborted!\n", aud
);
697 inform("aborted!"); /* doesn't clean up locks! */
701 inform("BUG in %s, number out of range", invo_name
);
705 inform("BUG in %s, scan() botch (%d)", invo_name
, incerr
);
711 * Run the external program hook on the message.
714 (void)snprintf(b
, sizeof (b
), "%s/%d", maildir_copy
, msgnum
+ 1);
715 (void)ext_hook("add-hook", b
, NULL
);
718 fputs (charstring_buffer (scanl
), aud
);
725 charstring_free (scanl
);
727 /* If we get here there was some sort of error from scan(),
728 * so stop processing anything more from the spool.
734 /* Mail from Maildir. */
739 hghnum
= msgnum
= mp
->hghmsg
;
740 for (i
= 0; i
< num_maildir_entries
; i
++) {
741 charstring_t scanl
= NULL
;
745 sp
= Maildir
[i
].filename
;
746 cp
= mh_xstrdup(m_name (msgnum
));
748 if (!trnflag
|| link(sp
, cp
) == -1) {
749 static char buf
[65536];
752 if ((sf
= fopen (sp
, "r")) == NULL
)
753 adios (sp
, "unable to read for copy");
754 if ((pf
= fopen (cp
, "w+")) == NULL
)
755 adios (cp
, "unable to write for copy");
756 while ((nrd
= fread(buf
, 1, sizeof(buf
), sf
)) > 0)
757 if (fwrite(buf
, 1, nrd
, pf
) != nrd
)
759 if (ferror(sf
) || fflush(pf
) || ferror(pf
)) {
761 fclose(pf
); fclose(sf
); (void) m_unlink(cp
);
763 adios(cp
, "copy error %s -> %s", sp
, cp
);
768 if (pf
== NULL
&& (pf
= fopen (cp
, "r")) == NULL
)
769 adios (cp
, "not available");
770 chmod (cp
, m_gmprot ());
772 fseek (pf
, 0L, SEEK_SET
);
773 switch (incerr
= scan (pf
, msgnum
, 0, nfs
, width
,
774 msgnum
== mp
->hghmsg
+ 1 && chgflag
,
775 1, NULL
, 0, noisy
, &scanl
)) {
777 printf ("%*d empty\n", DMAXFOLDER
, msgnum
);
783 /* advise (cp, "unable to read"); already advised */
794 * Run the external program hook on the message.
797 (void)snprintf(b
, sizeof (b
), "%s/%d", maildir_copy
, msgnum
+ 1);
798 (void)ext_hook("add-hook", b
, NULL
);
801 fputs (charstring_buffer (scanl
), aud
);
806 charstring_free (scanl
);
808 if (ferror(pf
) || fclose (pf
)) {
810 (void) m_unlink (cp
);
812 adios (cp
, "write error on");
817 if (trnflag
&& m_unlink (sp
) == NOTOK
)
818 adios (sp
, "couldn't unlink");
819 free (sp
); /* Free Maildir[i]->filename */
823 free (Maildir
); /* From now on Maildir is just a flag - don't dref! */
828 if (incerr
< 0) { /* error */
830 GETGROUPPRIVS(); /* Be sure we can unlock mail file */
831 (void) lkfclosespool (in
, newmail
); in
= NULL
;
832 DROPGROUPPRIVS(); /* And then return us to normal privileges */
834 fclose (in
); in
= NULL
;
845 if (inc_type
== INC_FILE
&& Maildir
== NULL
) {
846 /* Mail from a spool file; truncate it. */
849 if (stat (newmail
, &st
) != NOTOK
&& s1
.st_mtime
!= st
.st_mtime
)
850 inform("new messages have arrived!\007");
853 if ((newfd
= creat (newmail
, 0600)) != NOTOK
)
856 admonish (newmail
, "error zero'ing");
860 printf ("%s not zero'd\n", newmail
);
864 if (msgnum
== hghnum
) {
865 inform("no messages incorporated, continuing...");
868 * Lock the sequence file now, and loop to set the right flags
869 * in the folder structure
875 context_replace (pfolder
, folder
); /* update current folder */
877 if ((mp2
= folder_read(folder
, 1)) == NULL
) {
878 inform("Unable to reread folder %s, continuing...", folder
);
883 * Shouldn't happen, but just in case ...
886 if (msgnum
>= mp2
->hghoff
887 && !(mp2
= folder_realloc (mp2
, mp2
->lowoff
, msgnum
+ 1))) {
888 inform("unable to reallocate folder storage");
893 mp2
->curmsg
= hghnum
+ 1;
894 mp2
->hghmsg
= msgnum
;
896 if (mp2
->lowmsg
== 0)
898 if (chgflag
) /* sigh... */
899 seq_setcur (mp2
, mp2
->curmsg
);
901 for (i
= hghnum
+ 1; i
<= msgnum
; i
++) {
902 clear_msg_flags (mp2
, i
);
906 mp2
->msgflags
|= SEQMOD
;
907 seq_setunseen(mp2
, 0); /* Set the Unseen-Sequence */
908 seq_save(mp2
); /* Save the sequence file */
913 if (inc_type
== INC_FILE
&& Maildir
== NULL
) {
914 /* Mail from a spool file; unlock it. */
917 GETGROUPPRIVS(); /* Be sure we can unlock mail file */
918 (void) lkfclosespool (in
, newmail
); in
= NULL
;
919 DROPGROUPPRIVS(); /* And then return us to normal privileges */
921 fclose (in
); in
= NULL
;
925 context_save (); /* save the context file */
932 inc_done (int status
)
938 lkfclosespool(in
, newmail
);
945 pop_action(void *closure
, char *s
)
951 n
= fprintf(pc
->mailout
, "%s\n", s
);
954 pc
->written
+= n
; /* Count linefeed too. */