]>
diplodocus.org Git - nmh/blob - uip/inc.c
ea1b43120dd0867013ac62d7a2dcc4a8fe408549
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/folder_read.h"
35 #include "sbr/folder_realloc.h"
36 #include "sbr/folder_free.h"
37 #include "sbr/context_save.h"
38 #include "sbr/context_replace.h"
39 #include "sbr/context_find.h"
40 #include "sbr/ambigsw.h"
42 #include "sbr/print_version.h"
43 #include "sbr/print_help.h"
44 #include "sbr/error.h"
47 #include "h/dropsbr.h"
49 #include "h/fmt_scan.h"
50 #include "h/scansbr.h"
51 #include "h/signals.h"
55 #include "sbr/lock_file.h"
56 #include "sbr/m_maildir.h"
57 #include "sbr/m_mktemp.h"
60 # define TLSminc(a) (a)
65 #define INC_SWITCHES \
66 X("audit audit-file", 0, AUDSW) \
67 X("noaudit", 0, NAUDSW) \
68 X("changecur", 0, CHGSW) \
69 X("nochangecur", 0, NCHGSW) \
70 X("file name", 0, FILESW) \
71 X("form formatfile", 0, FORMSW) \
72 X("format string", 5, FMTSW) \
73 X("host hostname", 0, HOSTSW) \
74 X("user username", 0, USERSW) \
75 X("port name/number", 0, PORTSW) \
76 X("silent", 0, SILSW) \
77 X("nosilent", 0, NSILSW) \
78 X("truncate", 0, TRNCSW) \
79 X("notruncate", 0, NTRNCSW) \
80 X("width columns", 0, WIDTHSW) \
81 X("version", 0, VERSIONSW) \
82 X("help", 0, HELPSW) \
83 X("snoop", 0, SNOOPSW) \
84 X("sasl", 0, SASLSW) \
85 X("nosasl", 0, NOSASLSW) \
86 X("saslmech", 0, SASLMECHSW) \
87 X("initialtls", TLSminc(-10), INITTLSSW) \
88 X("notls", TLSminc(-5), NOTLSSW) \
89 X("certverify", TLSminc(-10), CERTVERSW) \
90 X("nocertverify", TLSminc(-12), NOCERTVERSW) \
91 X("authservice", 0, AUTHSERVICESW) \
92 X("proxy command", 0, PROXYSW) \
94 #define X(sw, minchars, id) id,
95 DEFINE_SWITCH_ENUM(INC
);
98 #define X(sw, minchars, id) { sw, minchars, id },
99 DEFINE_SWITCH_ARRAY(INC
, switches
);
103 * flags for the mail source
108 static struct Maildir_entry
{
112 static int num_maildir_entries
= 0;
120 extern char response
[];
122 /* This is an attempt to simplify things by putting all the
123 * privilege ops into macros.
124 * *GROUPPRIVS() is related to handling the setgid MAIL property,
125 * and only applies if MAILGROUP is defined.
126 * Basically, SAVEGROUPPRIVS() is called right at the top of main()
127 * to initialise things, and then DROPGROUPPRIVS() and GETGROUPPRIVS()
128 * do the obvious thing.
130 * There's probably a better implementation if we're allowed to use
131 * BSD-style setreuid() rather than using POSIX saved-ids.
132 * Anyway, if you're euid root it's a bit pointless to drop the group
135 * I'm pretty happy that the security is good provided we aren't setuid root.
136 * The only things we trust with group=mail privilege are lkfopen()
141 * For setting and returning to "mail" gid
144 static gid_t return_gid
;
145 #define TRYDROPGROUPPRIVS() DROPGROUPPRIVS()
146 #define DROPGROUPPRIVS() \
147 if (setegid(getgid()) != 0) { \
148 adios ("setegid", "unable to restore group to %ld", (long) getgid()); \
150 #define GETGROUPPRIVS() \
151 if (setegid(return_gid) != 0) { \
152 adios ("setegid", "unable to set group to %ld", (long) return_gid); \
154 #define SAVEGROUPPRIVS() return_gid = getegid()
156 /* define *GROUPPRIVS() as null; this avoids having lots of "#ifdef MAILGROUP"s */
157 #define TRYDROPGROUPPRIVS()
158 #define DROPGROUPPRIVS()
159 #define GETGROUPPRIVS()
160 #define SAVEGROUPPRIVS()
161 #endif /* not MAILGROUP */
163 /* these variables have to be globals so that done() can correctly clean up the lockfile */
165 static char *newmail
;
171 static int maildir_srt(const void *va
, const void *vb
) PURE
;
172 static void inc_done(int) NORETURN
;
173 static int pop_action(void *closure
, char *);
176 maildir_srt(const void *va
, const void *vb
)
178 const struct Maildir_entry
*a
= va
, *b
= vb
;
179 if (a
->mtime
> b
->mtime
)
181 if (a
->mtime
< b
->mtime
)
187 main (int argc
, char **argv
)
194 int hghnum
= 0, msgnum
= 0;
196 bool sasl
, tls
, noverify
;
197 int incerr
= 0; /* <0 if inc hits an error which means it should not truncate mailspool */
198 char *cp
, *maildir
= NULL
, *folder
= NULL
;
199 char *format
= NULL
, *form
= NULL
;
200 char *host
= NULL
, *port
= NULL
, *user
= NULL
, *proxy
= NULL
;
201 char *audfile
= NULL
, *from
= NULL
, *saslmech
= NULL
, *auth_svc
= NULL
;
202 char buf
[BUFSIZ
], **argp
, *nfs
, **arguments
;
203 struct msgs
*mp
= NULL
;
206 char b
[PATH_MAX
+ 1];
207 char *maildir_copy
= NULL
; /* copy of mail directory because the static gets overwritten */
210 char *MAILHOST_env_variable
;
213 /* absolutely the first thing we do is save our privileges,
214 * and drop them if we can.
219 if (nmh_init(argv
[0], true, true)) { return 1; }
222 arguments
= getarguments (invo_name
, argc
, argv
, 1);
227 * use MAILHOST environment variable if present,
229 * If that fails, use the default (if any)
230 * provided by mts.conf in mts_init()
232 if ((MAILHOST_env_variable
= getenv("MAILHOST")) != NULL
)
233 pophost
= MAILHOST_env_variable
;
235 * If there is a valid "pophost" entry in mts.conf,
236 * then use it as the default host.
238 if (pophost
&& *pophost
)
242 chgflag
= noisy
= noverify
= true;
243 while ((cp
= *argp
++)) {
245 switch (smatch (++cp
, switches
)) {
247 ambigsw (cp
, switches
);
250 die("-%s unknown", cp
);
253 snprintf (buf
, sizeof(buf
), "%s [+folder] [switches]", invo_name
);
254 print_help (buf
, switches
, 1);
257 print_version(invo_name
);
261 if (!(cp
= *argp
++) || *cp
== '-')
262 die("missing argument to %s", argp
[-2]);
263 audfile
= mh_xstrdup(m_maildir(cp
));
277 * The flag `trnflag' has the value:
279 * 2 if -truncate is given
280 * 1 by default (truncating is default)
281 * 0 if -notruncate is given
291 if (!(cp
= *argp
++) || *cp
== '-')
292 die("missing argument to %s", argp
[-2]);
293 from
= path (cp
, TFILE
);
296 * If the truncate file is in default state,
297 * change to not truncate.
311 if (!(form
= *argp
++) || *form
== '-')
312 die("missing argument to %s", argp
[-2]);
316 if (!(format
= *argp
++) || *format
== '-')
317 die("missing argument to %s", argp
[-2]);
322 if (!(cp
= *argp
++) || *cp
== '-')
323 die("missing argument to %s", argp
[-2]);
328 if (!(host
= *argp
++) || *host
== '-')
329 die("missing argument to %s", argp
[-2]);
333 if (!(port
= *argp
++) || *port
== '-')
334 die("missing argument to %s", argp
[-2]);
338 if (!(user
= *argp
++) || *user
== '-')
339 die("missing argument to %s", argp
[-2]);
354 if (!(saslmech
= *argp
++) || *saslmech
== '-')
355 die("missing argument to %s", argp
[-2]);
376 if (!(auth_svc
= *argp
++) || *auth_svc
== '-')
377 die("missing argument to %s", argp
[-2]);
379 die("not built with OAuth support");
384 if (!(proxy
= *argp
++) || *proxy
== '-')
385 die("missing argument to %s", argp
[-2]);
389 if (*cp
== '+' || *cp
== '@') {
391 die("only one folder at a time!");
392 folder
= pluspath (cp
);
394 die("usage: %s [+folder] [switches]", invo_name
);
398 /* NOTE: above this point you should use TRYDROPGROUPPRIVS(),
399 * not DROPGROUPPRIVS().
404 /* guarantee dropping group privileges; we might not have done so earlier */
407 /* Source of mail; -from overrides any -host. */
408 inc_type
= host
&& !from
? INC_POP
: INC_FILE
;
410 if (inc_type
== INC_POP
) {
411 /* Mail from a POP server. */
414 if (auth_svc
== NULL
) {
415 if (saslmech
&& ! strcasecmp(saslmech
, "xoauth2")) {
416 die("must specify -authservice with -saslmech xoauth2");
420 die("must specify -user with -saslmech xoauth2");
425 tlsflag
|= P_INITTLS
;
428 tlsflag
|= P_NOVERIFY
;
431 * initialize POP connection
433 if (pop_init (host
, port
, user
, proxy
, snoop
, sasl
, saslmech
,
434 tlsflag
, auth_svc
) == NOTOK
)
437 /* Check if there are any messages */
438 if (pop_stat (&nmsgs
, &nbytes
) == NOTOK
)
443 die("no mail to incorporate");
446 } else if (inc_type
== INC_FILE
) {
447 /* Mail from a spool file, or Maildir. */
450 else if ((newmail
= getenv ("MAILDROP")) && *newmail
)
451 newmail
= m_mailpath (newmail
);
452 else if ((newmail
= context_find ("maildrop")) && *newmail
)
453 newmail
= m_mailpath (newmail
);
455 newmail
= concat (MAILDIR
, "/", MAILFIL
, NULL
);
457 if (stat (newmail
, &s1
) == NOTOK
|| s1
.st_size
== 0)
458 die("no mail to incorporate");
459 if (s1
.st_mode
& S_IFDIR
) {
465 cp
= concat (newmail
, "/new", NULL
);
466 if ((md
= opendir(cp
)) == NULL
)
467 die("unable to open %s", cp
);
468 while ((de
= readdir (md
)) != NULL
) {
469 if (de
->d_name
[0] == '.')
471 if (i
>= num_maildir_entries
) {
472 if ((Maildir
= realloc(Maildir
, sizeof(*Maildir
) * (2*i
+16))) == NULL
)
473 die("not enough memory for %d messages", 2*i
+16);
474 num_maildir_entries
= 2*i
+16;
476 Maildir
[i
].filename
= concat (cp
, "/", de
->d_name
, NULL
);
477 if (stat(Maildir
[i
].filename
, &ms
) != 0)
478 adios (Maildir
[i
].filename
, "couldn't get delivery time");
479 Maildir
[i
].mtime
= ms
.st_mtime
;
484 cp
= concat (newmail
, "/cur", NULL
);
485 if ((md
= opendir(cp
)) == NULL
)
486 die("unable to open %s", cp
);
487 while ((de
= readdir (md
)) != NULL
) {
488 if (de
->d_name
[0] == '.')
490 if (i
>= num_maildir_entries
) {
491 if ((Maildir
= realloc(Maildir
, sizeof(*Maildir
) * (2*i
+16))) == NULL
)
492 die("not enough memory for %d messages", 2*i
+16);
493 num_maildir_entries
= 2*i
+16;
495 Maildir
[i
].filename
= concat (cp
, "/", de
->d_name
, NULL
);
496 if (stat(Maildir
[i
].filename
, &ms
) != 0)
497 adios (Maildir
[i
].filename
, "couldn't get delivery time");
498 Maildir
[i
].mtime
= ms
.st_mtime
;
504 die("no mail to incorporate");
505 num_maildir_entries
= i
;
506 qsort (Maildir
, num_maildir_entries
, sizeof(*Maildir
), maildir_srt
);
509 cp
= mh_xstrdup(newmail
);
513 if (!context_find ("path"))
514 free (path ("./", TFOLDER
));
516 folder
= getfolder (0);
517 maildir
= m_maildir (folder
);
518 maildir_copy
= mh_xstrdup(maildir
);
520 if (!folder_exists(maildir
)) {
521 /* If the folder doesn't exist, and we're given the -silent flag,
525 create_folder(maildir
, 0, done
);
530 if (chdir (maildir
) == NOTOK
)
531 adios (maildir
, "unable to change directory to");
533 /* read folder and create message structure */
534 if (!(mp
= folder_read (folder
, 0)))
535 die("unable to read folder %s", folder
);
537 if (inc_type
== INC_FILE
&& Maildir
== NULL
) {
538 /* Mail from a spool file. */
540 if (access (newmail
, W_OK
) != NOTOK
) {
543 SIGNAL (SIGHUP
, SIG_IGN
);
544 SIGNAL (SIGINT
, SIG_IGN
);
545 SIGNAL (SIGQUIT
, SIG_IGN
);
546 SIGNAL (SIGTERM
, SIG_IGN
);
549 GETGROUPPRIVS(); /* Reset gid to lock mail file */
550 in
= lkfopenspool (newmail
, "r");
553 die("unable to lock and fopen %s", newmail
);
554 fstat (fileno(in
), &s1
);
557 if ((in
= fopen (newmail
, "r")) == NULL
)
558 adios (newmail
, "unable to read");
562 /* This shouldn't be necessary but it can't hurt. */
567 if ((i
= stat (audfile
, &st
)) == NOTOK
)
568 inform("Creating Receive-Audit: %s", audfile
);
569 if ((aud
= fopen (audfile
, "a")) == NULL
)
570 adios (audfile
, "unable to append to");
572 chmod (audfile
, m_gmprot ());
575 fprintf (aud
, "<<inc>> %s -ms %s\n", dtimenow(0), from
);
578 fprintf (aud
, "<<inc>> %s -host %s -user %s\n", dtimenow(0),
581 fprintf (aud
, "<<inc>> %s\n", dtimenow (0));
585 /* Get new format string */
586 nfs
= new_fs (form
, format
, FORMAT
);
589 printf ("Incorporating new mail into %s...\n\n", folder
);
595 * Get the mail from a POP server
597 if (inc_type
== INC_POP
) {
598 /* Mail from a POP server. */
602 hghnum
= msgnum
= mp
->hghmsg
;
603 for (i
= 1; i
<= nmsgs
; i
++) {
604 charstring_t scanl
= NULL
;
607 cp
= mh_xstrdup(m_name (msgnum
));
608 if ((pf
= fopen (cp
, "w+")) == NULL
)
609 adios (cp
, "unable to write");
610 chmod (cp
, m_gmprot ());
614 if (pop_retr(i
, pop_action
, &pc
) == NOTOK
)
618 adios (cp
, "write error on");
619 fseek (pf
, 0L, SEEK_SET
);
620 switch (incerr
= scan (pf
, msgnum
, 0, nfs
, width
,
621 msgnum
== mp
->hghmsg
+ 1 && chgflag
,
622 1, NULL
, pc
.written
, noisy
, &scanl
)) {
624 printf ("%*d empty\n", DMAXFOLDER
, msgnum
);
630 /* advise (cp, "unable to read"); already advised */
641 fputs (charstring_buffer (scanl
), aud
);
646 charstring_free (scanl
);
648 if (ferror(pf
) || fclose (pf
)) {
650 (void) m_unlink (cp
);
653 adios (cp
, "write error on");
657 if (trnflag
&& pop_dele (i
) == NOTOK
)
663 if (pop_quit () == NOTOK
)
666 } else if (inc_type
== INC_FILE
&& Maildir
== NULL
) {
667 /* Mail from a spool file. */
669 scan_detect_mbox_style (in
); /* the MAGIC invocation... */
670 hghnum
= msgnum
= mp
->hghmsg
;
672 charstring_t scanl
= NULL
;
674 /* create scanline for new message */
675 switch (incerr
= scan (in
, msgnum
+ 1, msgnum
+ 1, nfs
, width
,
676 msgnum
== hghnum
&& chgflag
, 1, NULL
, 0L, noisy
,
684 fputs ("inc aborted!\n", aud
);
685 inform("aborted!"); /* doesn't clean up locks! */
689 inform("BUG in %s, number out of range", invo_name
);
693 inform("BUG in %s, scan() botch (%d)", invo_name
, incerr
);
699 * Run the external program hook on the message.
702 (void)snprintf(b
, sizeof (b
), "%s/%d", maildir_copy
, msgnum
+ 1);
703 (void)ext_hook("add-hook", b
, NULL
);
706 fputs (charstring_buffer (scanl
), aud
);
713 charstring_free (scanl
);
715 /* If we get here there was some sort of error from scan(),
716 * so stop processing anything more from the spool.
722 /* Mail from Maildir. */
727 hghnum
= msgnum
= mp
->hghmsg
;
728 for (i
= 0; i
< num_maildir_entries
; i
++) {
729 charstring_t scanl
= NULL
;
733 sp
= Maildir
[i
].filename
;
734 cp
= mh_xstrdup(m_name (msgnum
));
736 if (!trnflag
|| link(sp
, cp
) == -1) {
737 static char buf
[65536];
740 if ((sf
= fopen (sp
, "r")) == NULL
)
741 adios (sp
, "unable to read for copy");
742 if ((pf
= fopen (cp
, "w+")) == NULL
)
743 adios (cp
, "unable to write for copy");
744 while ((nrd
= fread(buf
, 1, sizeof(buf
), sf
)) > 0)
745 if (fwrite(buf
, 1, nrd
, pf
) != nrd
)
747 if (ferror(sf
) || fflush(pf
) || ferror(pf
)) {
749 fclose(pf
); fclose(sf
); (void) m_unlink(cp
);
751 adios(cp
, "copy error %s -> %s", sp
, cp
);
756 if (pf
== NULL
&& (pf
= fopen (cp
, "r")) == NULL
)
757 adios (cp
, "not available");
758 chmod (cp
, m_gmprot ());
760 fseek (pf
, 0L, SEEK_SET
);
761 switch (incerr
= scan (pf
, msgnum
, 0, nfs
, width
,
762 msgnum
== mp
->hghmsg
+ 1 && chgflag
,
763 1, NULL
, 0, noisy
, &scanl
)) {
765 printf ("%*d empty\n", DMAXFOLDER
, msgnum
);
771 /* advise (cp, "unable to read"); already advised */
782 * Run the external program hook on the message.
785 (void)snprintf(b
, sizeof (b
), "%s/%d", maildir_copy
, msgnum
+ 1);
786 (void)ext_hook("add-hook", b
, NULL
);
789 fputs (charstring_buffer (scanl
), aud
);
794 charstring_free (scanl
);
796 if (ferror(pf
) || fclose (pf
)) {
798 (void) m_unlink (cp
);
800 adios (cp
, "write error on");
805 if (trnflag
&& m_unlink (sp
) == NOTOK
)
806 adios (sp
, "couldn't unlink");
807 free (sp
); /* Free Maildir[i]->filename */
811 free (Maildir
); /* From now on Maildir is just a flag - don't dref! */
816 if (incerr
< 0) { /* error */
818 GETGROUPPRIVS(); /* Be sure we can unlock mail file */
819 (void) lkfclosespool (in
, newmail
); in
= NULL
;
820 DROPGROUPPRIVS(); /* And then return us to normal privileges */
822 fclose (in
); in
= NULL
;
833 if (inc_type
== INC_FILE
&& Maildir
== NULL
) {
834 /* Mail from a spool file; truncate it. */
837 if (stat (newmail
, &st
) != NOTOK
&& s1
.st_mtime
!= st
.st_mtime
)
838 inform("new messages have arrived!\007");
841 if ((newfd
= creat (newmail
, 0600)) != NOTOK
)
844 admonish (newmail
, "error zero'ing");
848 printf ("%s not zero'd\n", newmail
);
852 if (msgnum
== hghnum
) {
853 inform("no messages incorporated, continuing...");
856 * Lock the sequence file now, and loop to set the right flags
857 * in the folder structure
863 context_replace (pfolder
, folder
); /* update current folder */
865 if ((mp2
= folder_read(folder
, 1)) == NULL
) {
866 inform("Unable to reread folder %s, continuing...", folder
);
871 * Shouldn't happen, but just in case ...
874 if (msgnum
>= mp2
->hghoff
875 && !(mp2
= folder_realloc (mp2
, mp2
->lowoff
, msgnum
+ 1))) {
876 inform("unable to reallocate folder storage");
881 mp2
->curmsg
= hghnum
+ 1;
882 mp2
->hghmsg
= msgnum
;
884 if (mp2
->lowmsg
== 0)
886 if (chgflag
) /* sigh... */
887 seq_setcur (mp2
, mp2
->curmsg
);
889 for (i
= hghnum
+ 1; i
<= msgnum
; i
++) {
890 clear_msg_flags (mp2
, i
);
894 mp2
->msgflags
|= SEQMOD
;
895 seq_setunseen(mp2
, 0); /* Set the Unseen-Sequence */
896 seq_save(mp2
); /* Save the sequence file */
901 if (inc_type
== INC_FILE
&& Maildir
== NULL
) {
902 /* Mail from a spool file; unlock it. */
905 GETGROUPPRIVS(); /* Be sure we can unlock mail file */
906 (void) lkfclosespool (in
, newmail
); in
= NULL
;
907 DROPGROUPPRIVS(); /* And then return us to normal privileges */
909 fclose (in
); in
= NULL
;
913 context_save (); /* save the context file */
920 inc_done (int status
)
926 lkfclosespool(in
, newmail
);
933 pop_action(void *closure
, char *s
)
939 n
= fprintf(pc
->mailout
, "%s\n", s
);
942 pc
->written
+= n
; /* Count linefeed too. */