]>
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
35 #include "sbr/print_version.h"
36 #include "sbr/print_help.h"
37 #include "sbr/error.h"
40 #include "h/dropsbr.h"
42 #include "h/fmt_scan.h"
43 #include "h/scansbr.h"
44 #include "h/signals.h"
48 #include "sbr/lock_file.h"
49 #include "sbr/m_maildir.h"
50 #include "sbr/m_mktemp.h"
53 # define TLSminc(a) (a)
58 #define INC_SWITCHES \
59 X("audit audit-file", 0, AUDSW) \
60 X("noaudit", 0, NAUDSW) \
61 X("changecur", 0, CHGSW) \
62 X("nochangecur", 0, NCHGSW) \
63 X("file name", 0, FILESW) \
64 X("form formatfile", 0, FORMSW) \
65 X("format string", 5, FMTSW) \
66 X("host hostname", 0, HOSTSW) \
67 X("user username", 0, USERSW) \
68 X("port name/number", 0, PORTSW) \
69 X("silent", 0, SILSW) \
70 X("nosilent", 0, NSILSW) \
71 X("truncate", 0, TRNCSW) \
72 X("notruncate", 0, NTRNCSW) \
73 X("width columns", 0, WIDTHSW) \
74 X("version", 0, VERSIONSW) \
75 X("help", 0, HELPSW) \
76 X("snoop", 0, SNOOPSW) \
77 X("sasl", 0, SASLSW) \
78 X("nosasl", 0, NOSASLSW) \
79 X("saslmech", 0, SASLMECHSW) \
80 X("initialtls", TLSminc(-10), INITTLSSW) \
81 X("notls", TLSminc(-5), NOTLSSW) \
82 X("certverify", TLSminc(-10), CERTVERSW) \
83 X("nocertverify", TLSminc(-12), NOCERTVERSW) \
84 X("authservice", 0, AUTHSERVICESW) \
85 X("proxy command", 0, PROXYSW) \
87 #define X(sw, minchars, id) id,
88 DEFINE_SWITCH_ENUM(INC
);
91 #define X(sw, minchars, id) { sw, minchars, id },
92 DEFINE_SWITCH_ARRAY(INC
, switches
);
96 * flags for the mail source
101 static struct Maildir_entry
{
105 static int num_maildir_entries
= 0;
113 extern char response
[];
115 /* This is an attempt to simplify things by putting all the
116 * privilege ops into macros.
117 * *GROUPPRIVS() is related to handling the setgid MAIL property,
118 * and only applies if MAILGROUP is defined.
119 * Basically, SAVEGROUPPRIVS() is called right at the top of main()
120 * to initialise things, and then DROPGROUPPRIVS() and GETGROUPPRIVS()
121 * do the obvious thing.
123 * There's probably a better implementation if we're allowed to use
124 * BSD-style setreuid() rather than using POSIX saved-ids.
125 * Anyway, if you're euid root it's a bit pointless to drop the group
128 * I'm pretty happy that the security is good provided we aren't setuid root.
129 * The only things we trust with group=mail privilege are lkfopen()
134 * For setting and returning to "mail" gid
137 static gid_t return_gid
;
138 #define TRYDROPGROUPPRIVS() DROPGROUPPRIVS()
139 #define DROPGROUPPRIVS() \
140 if (setegid(getgid()) != 0) { \
141 adios ("setegid", "unable to restore group to %ld", (long) getgid()); \
143 #define GETGROUPPRIVS() \
144 if (setegid(return_gid) != 0) { \
145 adios ("setegid", "unable to set group to %ld", (long) return_gid); \
147 #define SAVEGROUPPRIVS() return_gid = getegid()
149 /* define *GROUPPRIVS() as null; this avoids having lots of "#ifdef MAILGROUP"s */
150 #define TRYDROPGROUPPRIVS()
151 #define DROPGROUPPRIVS()
152 #define GETGROUPPRIVS()
153 #define SAVEGROUPPRIVS()
154 #endif /* not MAILGROUP */
156 /* these variables have to be globals so that done() can correctly clean up the lockfile */
158 static char *newmail
;
164 static int maildir_srt(const void *va
, const void *vb
) PURE
;
165 static void inc_done(int) NORETURN
;
166 static int pop_action(void *closure
, char *);
169 maildir_srt(const void *va
, const void *vb
)
171 const struct Maildir_entry
*a
= va
, *b
= vb
;
172 if (a
->mtime
> b
->mtime
)
174 if (a
->mtime
< b
->mtime
)
180 main (int argc
, char **argv
)
187 int hghnum
= 0, msgnum
= 0;
189 bool sasl
, tls
, noverify
;
190 int incerr
= 0; /* <0 if inc hits an error which means it should not truncate mailspool */
191 char *cp
, *maildir
= NULL
, *folder
= NULL
;
192 char *format
= NULL
, *form
= NULL
;
193 char *host
= NULL
, *port
= NULL
, *user
= NULL
, *proxy
= NULL
;
194 char *audfile
= NULL
, *from
= NULL
, *saslmech
= NULL
, *auth_svc
= NULL
;
195 char buf
[BUFSIZ
], **argp
, *nfs
, **arguments
;
196 struct msgs
*mp
= NULL
;
199 char b
[PATH_MAX
+ 1];
200 char *maildir_copy
= NULL
; /* copy of mail directory because the static gets overwritten */
203 char *MAILHOST_env_variable
;
206 /* absolutely the first thing we do is save our privileges,
207 * and drop them if we can.
212 if (nmh_init(argv
[0], true, true)) { return 1; }
215 arguments
= getarguments (invo_name
, argc
, argv
, 1);
220 * use MAILHOST environment variable if present,
222 * If that fails, use the default (if any)
223 * provided by mts.conf in mts_init()
225 if ((MAILHOST_env_variable
= getenv("MAILHOST")) != NULL
)
226 pophost
= MAILHOST_env_variable
;
228 * If there is a valid "pophost" entry in mts.conf,
229 * then use it as the default host.
231 if (pophost
&& *pophost
)
235 chgflag
= noisy
= noverify
= true;
236 while ((cp
= *argp
++)) {
238 switch (smatch (++cp
, switches
)) {
240 ambigsw (cp
, switches
);
243 die("-%s unknown", cp
);
246 snprintf (buf
, sizeof(buf
), "%s [+folder] [switches]", invo_name
);
247 print_help (buf
, switches
, 1);
250 print_version(invo_name
);
254 if (!(cp
= *argp
++) || *cp
== '-')
255 die("missing argument to %s", argp
[-2]);
256 audfile
= mh_xstrdup(m_maildir(cp
));
270 * The flag `trnflag' has the value:
272 * 2 if -truncate is given
273 * 1 by default (truncating is default)
274 * 0 if -notruncate is given
284 if (!(cp
= *argp
++) || *cp
== '-')
285 die("missing argument to %s", argp
[-2]);
286 from
= path (cp
, TFILE
);
289 * If the truncate file is in default state,
290 * change to not truncate.
304 if (!(form
= *argp
++) || *form
== '-')
305 die("missing argument to %s", argp
[-2]);
309 if (!(format
= *argp
++) || *format
== '-')
310 die("missing argument to %s", argp
[-2]);
315 if (!(cp
= *argp
++) || *cp
== '-')
316 die("missing argument to %s", argp
[-2]);
321 if (!(host
= *argp
++) || *host
== '-')
322 die("missing argument to %s", argp
[-2]);
326 if (!(port
= *argp
++) || *port
== '-')
327 die("missing argument to %s", argp
[-2]);
331 if (!(user
= *argp
++) || *user
== '-')
332 die("missing argument to %s", argp
[-2]);
347 if (!(saslmech
= *argp
++) || *saslmech
== '-')
348 die("missing argument to %s", argp
[-2]);
369 if (!(auth_svc
= *argp
++) || *auth_svc
== '-')
370 die("missing argument to %s", argp
[-2]);
372 die("not built with OAuth support");
377 if (!(proxy
= *argp
++) || *proxy
== '-')
378 die("missing argument to %s", argp
[-2]);
382 if (*cp
== '+' || *cp
== '@') {
384 die("only one folder at a time!");
385 folder
= pluspath (cp
);
387 die("usage: %s [+folder] [switches]", invo_name
);
391 /* NOTE: above this point you should use TRYDROPGROUPPRIVS(),
392 * not DROPGROUPPRIVS().
397 /* guarantee dropping group privileges; we might not have done so earlier */
400 /* Source of mail; -from overrides any -host. */
401 inc_type
= host
&& !from
? INC_POP
: INC_FILE
;
403 if (inc_type
== INC_POP
) {
404 /* Mail from a POP server. */
407 if (auth_svc
== NULL
) {
408 if (saslmech
&& ! strcasecmp(saslmech
, "xoauth2")) {
409 die("must specify -authservice with -saslmech xoauth2");
413 die("must specify -user with -saslmech xoauth2");
418 tlsflag
|= P_INITTLS
;
421 tlsflag
|= P_NOVERIFY
;
424 * initialize POP connection
426 if (pop_init (host
, port
, user
, proxy
, snoop
, sasl
, saslmech
,
427 tlsflag
, auth_svc
) == NOTOK
)
430 /* Check if there are any messages */
431 if (pop_stat (&nmsgs
, &nbytes
) == NOTOK
)
436 die("no mail to incorporate");
439 } else if (inc_type
== INC_FILE
) {
440 /* Mail from a spool file, or Maildir. */
443 else if ((newmail
= getenv ("MAILDROP")) && *newmail
)
444 newmail
= m_mailpath (newmail
);
445 else if ((newmail
= context_find ("maildrop")) && *newmail
)
446 newmail
= m_mailpath (newmail
);
448 newmail
= concat (MAILDIR
, "/", MAILFIL
, NULL
);
450 if (stat (newmail
, &s1
) == NOTOK
|| s1
.st_size
== 0)
451 die("no mail to incorporate");
452 if (s1
.st_mode
& S_IFDIR
) {
458 cp
= concat (newmail
, "/new", NULL
);
459 if ((md
= opendir(cp
)) == NULL
)
460 die("unable to open %s", cp
);
461 while ((de
= readdir (md
)) != NULL
) {
462 if (de
->d_name
[0] == '.')
464 if (i
>= num_maildir_entries
) {
465 if ((Maildir
= realloc(Maildir
, sizeof(*Maildir
) * (2*i
+16))) == NULL
)
466 die("not enough memory for %d messages", 2*i
+16);
467 num_maildir_entries
= 2*i
+16;
469 Maildir
[i
].filename
= concat (cp
, "/", de
->d_name
, NULL
);
470 if (stat(Maildir
[i
].filename
, &ms
) != 0)
471 adios (Maildir
[i
].filename
, "couldn't get delivery time");
472 Maildir
[i
].mtime
= ms
.st_mtime
;
477 cp
= concat (newmail
, "/cur", 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
;
497 die("no mail to incorporate");
498 num_maildir_entries
= i
;
499 qsort (Maildir
, num_maildir_entries
, sizeof(*Maildir
), maildir_srt
);
502 cp
= mh_xstrdup(newmail
);
506 if (!context_find ("path"))
507 free (path ("./", TFOLDER
));
509 folder
= getfolder (0);
510 maildir
= m_maildir (folder
);
511 maildir_copy
= mh_xstrdup(maildir
);
513 if (!folder_exists(maildir
)) {
514 /* If the folder doesn't exist, and we're given the -silent flag,
518 create_folder(maildir
, 0, done
);
523 if (chdir (maildir
) == NOTOK
)
524 adios (maildir
, "unable to change directory to");
526 /* read folder and create message structure */
527 if (!(mp
= folder_read (folder
, 0)))
528 die("unable to read folder %s", folder
);
530 if (inc_type
== INC_FILE
&& Maildir
== NULL
) {
531 /* Mail from a spool file. */
533 if (access (newmail
, W_OK
) != NOTOK
) {
536 SIGNAL (SIGHUP
, SIG_IGN
);
537 SIGNAL (SIGINT
, SIG_IGN
);
538 SIGNAL (SIGQUIT
, SIG_IGN
);
539 SIGNAL (SIGTERM
, SIG_IGN
);
542 GETGROUPPRIVS(); /* Reset gid to lock mail file */
543 in
= lkfopenspool (newmail
, "r");
546 die("unable to lock and fopen %s", newmail
);
547 fstat (fileno(in
), &s1
);
550 if ((in
= fopen (newmail
, "r")) == NULL
)
551 adios (newmail
, "unable to read");
555 /* This shouldn't be necessary but it can't hurt. */
560 if ((i
= stat (audfile
, &st
)) == NOTOK
)
561 inform("Creating Receive-Audit: %s", audfile
);
562 if ((aud
= fopen (audfile
, "a")) == NULL
)
563 adios (audfile
, "unable to append to");
565 chmod (audfile
, m_gmprot ());
568 fprintf (aud
, "<<inc>> %s -ms %s\n", dtimenow(0), from
);
571 fprintf (aud
, "<<inc>> %s -host %s -user %s\n", dtimenow(0),
574 fprintf (aud
, "<<inc>> %s\n", dtimenow (0));
578 /* Get new format string */
579 nfs
= new_fs (form
, format
, FORMAT
);
582 printf ("Incorporating new mail into %s...\n\n", folder
);
588 * Get the mail from a POP server
590 if (inc_type
== INC_POP
) {
591 /* Mail from a POP server. */
595 hghnum
= msgnum
= mp
->hghmsg
;
596 for (i
= 1; i
<= nmsgs
; i
++) {
597 charstring_t scanl
= NULL
;
600 cp
= mh_xstrdup(m_name (msgnum
));
601 if ((pf
= fopen (cp
, "w+")) == NULL
)
602 adios (cp
, "unable to write");
603 chmod (cp
, m_gmprot ());
607 if (pop_retr(i
, pop_action
, &pc
) == NOTOK
)
611 adios (cp
, "write error on");
612 fseek (pf
, 0L, SEEK_SET
);
613 switch (incerr
= scan (pf
, msgnum
, 0, nfs
, width
,
614 msgnum
== mp
->hghmsg
+ 1 && chgflag
,
615 1, NULL
, pc
.written
, noisy
, &scanl
)) {
617 printf ("%*d empty\n", DMAXFOLDER
, msgnum
);
623 /* advise (cp, "unable to read"); already advised */
634 fputs (charstring_buffer (scanl
), aud
);
639 charstring_free (scanl
);
641 if (ferror(pf
) || fclose (pf
)) {
643 (void) m_unlink (cp
);
646 adios (cp
, "write error on");
650 if (trnflag
&& pop_dele (i
) == NOTOK
)
656 if (pop_quit () == NOTOK
)
659 } else if (inc_type
== INC_FILE
&& Maildir
== NULL
) {
660 /* Mail from a spool file. */
662 scan_detect_mbox_style (in
); /* the MAGIC invocation... */
663 hghnum
= msgnum
= mp
->hghmsg
;
665 charstring_t scanl
= NULL
;
667 /* create scanline for new message */
668 switch (incerr
= scan (in
, msgnum
+ 1, msgnum
+ 1, nfs
, width
,
669 msgnum
== hghnum
&& chgflag
, 1, NULL
, 0L, noisy
,
677 fputs ("inc aborted!\n", aud
);
678 inform("aborted!"); /* doesn't clean up locks! */
682 inform("BUG in %s, number out of range", invo_name
);
686 inform("BUG in %s, scan() botch (%d)", invo_name
, incerr
);
692 * Run the external program hook on the message.
695 (void)snprintf(b
, sizeof (b
), "%s/%d", maildir_copy
, msgnum
+ 1);
696 (void)ext_hook("add-hook", b
, NULL
);
699 fputs (charstring_buffer (scanl
), aud
);
706 charstring_free (scanl
);
708 /* If we get here there was some sort of error from scan(),
709 * so stop processing anything more from the spool.
715 /* Mail from Maildir. */
720 hghnum
= msgnum
= mp
->hghmsg
;
721 for (i
= 0; i
< num_maildir_entries
; i
++) {
722 charstring_t scanl
= NULL
;
726 sp
= Maildir
[i
].filename
;
727 cp
= mh_xstrdup(m_name (msgnum
));
729 if (!trnflag
|| link(sp
, cp
) == -1) {
730 static char buf
[65536];
733 if ((sf
= fopen (sp
, "r")) == NULL
)
734 adios (sp
, "unable to read for copy");
735 if ((pf
= fopen (cp
, "w+")) == NULL
)
736 adios (cp
, "unable to write for copy");
737 while ((nrd
= fread(buf
, 1, sizeof(buf
), sf
)) > 0)
738 if (fwrite(buf
, 1, nrd
, pf
) != nrd
)
740 if (ferror(sf
) || fflush(pf
) || ferror(pf
)) {
742 fclose(pf
); fclose(sf
); (void) m_unlink(cp
);
744 adios(cp
, "copy error %s -> %s", sp
, cp
);
749 if (pf
== NULL
&& (pf
= fopen (cp
, "r")) == NULL
)
750 adios (cp
, "not available");
751 chmod (cp
, m_gmprot ());
753 fseek (pf
, 0L, SEEK_SET
);
754 switch (incerr
= scan (pf
, msgnum
, 0, nfs
, width
,
755 msgnum
== mp
->hghmsg
+ 1 && chgflag
,
756 1, NULL
, 0, noisy
, &scanl
)) {
758 printf ("%*d empty\n", DMAXFOLDER
, msgnum
);
764 /* advise (cp, "unable to read"); already advised */
775 * Run the external program hook on the message.
778 (void)snprintf(b
, sizeof (b
), "%s/%d", maildir_copy
, msgnum
+ 1);
779 (void)ext_hook("add-hook", b
, NULL
);
782 fputs (charstring_buffer (scanl
), aud
);
787 charstring_free (scanl
);
789 if (ferror(pf
) || fclose (pf
)) {
791 (void) m_unlink (cp
);
793 adios (cp
, "write error on");
798 if (trnflag
&& m_unlink (sp
) == NOTOK
)
799 adios (sp
, "couldn't unlink");
800 free (sp
); /* Free Maildir[i]->filename */
804 free (Maildir
); /* From now on Maildir is just a flag - don't dref! */
809 if (incerr
< 0) { /* error */
811 GETGROUPPRIVS(); /* Be sure we can unlock mail file */
812 (void) lkfclosespool (in
, newmail
); in
= NULL
;
813 DROPGROUPPRIVS(); /* And then return us to normal privileges */
815 fclose (in
); in
= NULL
;
826 if (inc_type
== INC_FILE
&& Maildir
== NULL
) {
827 /* Mail from a spool file; truncate it. */
830 if (stat (newmail
, &st
) != NOTOK
&& s1
.st_mtime
!= st
.st_mtime
)
831 inform("new messages have arrived!\007");
834 if ((newfd
= creat (newmail
, 0600)) != NOTOK
)
837 admonish (newmail
, "error zero'ing");
841 printf ("%s not zero'd\n", newmail
);
845 if (msgnum
== hghnum
) {
846 inform("no messages incorporated, continuing...");
849 * Lock the sequence file now, and loop to set the right flags
850 * in the folder structure
856 context_replace (pfolder
, folder
); /* update current folder */
858 if ((mp2
= folder_read(folder
, 1)) == NULL
) {
859 inform("Unable to reread folder %s, continuing...", folder
);
864 * Shouldn't happen, but just in case ...
867 if (msgnum
>= mp2
->hghoff
868 && !(mp2
= folder_realloc (mp2
, mp2
->lowoff
, msgnum
+ 1))) {
869 inform("unable to reallocate folder storage");
874 mp2
->curmsg
= hghnum
+ 1;
875 mp2
->hghmsg
= msgnum
;
877 if (mp2
->lowmsg
== 0)
879 if (chgflag
) /* sigh... */
880 seq_setcur (mp2
, mp2
->curmsg
);
882 for (i
= hghnum
+ 1; i
<= msgnum
; i
++) {
883 clear_msg_flags (mp2
, i
);
887 mp2
->msgflags
|= SEQMOD
;
888 seq_setunseen(mp2
, 0); /* Set the Unseen-Sequence */
889 seq_save(mp2
); /* Save the sequence file */
894 if (inc_type
== INC_FILE
&& Maildir
== NULL
) {
895 /* Mail from a spool file; unlock it. */
898 GETGROUPPRIVS(); /* Be sure we can unlock mail file */
899 (void) lkfclosespool (in
, newmail
); in
= NULL
;
900 DROPGROUPPRIVS(); /* And then return us to normal privileges */
902 fclose (in
); in
= NULL
;
906 context_save (); /* save the context file */
913 inc_done (int status
)
919 lkfclosespool(in
, newmail
);
926 pop_action(void *closure
, char *s
)
932 n
= fprintf(pc
->mailout
, "%s\n", s
);
935 pc
->written
+= n
; /* Count linefeed too. */