]>
diplodocus.org Git - nmh/blob - uip/inc.c
de0c7993013f3f9e28cbfaf0517eb0cb276d1947
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_realloc.h"
35 #include "sbr/folder_free.h"
36 #include "sbr/context_save.h"
37 #include "sbr/context_replace.h"
38 #include "sbr/context_find.h"
39 #include "sbr/ambigsw.h"
41 #include "sbr/print_version.h"
42 #include "sbr/print_help.h"
43 #include "sbr/error.h"
46 #include "h/dropsbr.h"
48 #include "h/fmt_scan.h"
49 #include "h/scansbr.h"
50 #include "h/signals.h"
54 #include "sbr/lock_file.h"
55 #include "sbr/m_maildir.h"
56 #include "sbr/m_mktemp.h"
59 # define TLSminc(a) (a)
64 #define INC_SWITCHES \
65 X("audit audit-file", 0, AUDSW) \
66 X("noaudit", 0, NAUDSW) \
67 X("changecur", 0, CHGSW) \
68 X("nochangecur", 0, NCHGSW) \
69 X("file name", 0, FILESW) \
70 X("form formatfile", 0, FORMSW) \
71 X("format string", 5, FMTSW) \
72 X("host hostname", 0, HOSTSW) \
73 X("user username", 0, USERSW) \
74 X("port name/number", 0, PORTSW) \
75 X("silent", 0, SILSW) \
76 X("nosilent", 0, NSILSW) \
77 X("truncate", 0, TRNCSW) \
78 X("notruncate", 0, NTRNCSW) \
79 X("width columns", 0, WIDTHSW) \
80 X("version", 0, VERSIONSW) \
81 X("help", 0, HELPSW) \
82 X("snoop", 0, SNOOPSW) \
83 X("sasl", 0, SASLSW) \
84 X("nosasl", 0, NOSASLSW) \
85 X("saslmech", 0, SASLMECHSW) \
86 X("initialtls", TLSminc(-10), INITTLSSW) \
87 X("notls", TLSminc(-5), NOTLSSW) \
88 X("certverify", TLSminc(-10), CERTVERSW) \
89 X("nocertverify", TLSminc(-12), NOCERTVERSW) \
90 X("authservice", 0, AUTHSERVICESW) \
91 X("proxy command", 0, PROXYSW) \
93 #define X(sw, minchars, id) id,
94 DEFINE_SWITCH_ENUM(INC
);
97 #define X(sw, minchars, id) { sw, minchars, id },
98 DEFINE_SWITCH_ARRAY(INC
, switches
);
102 * flags for the mail source
107 static struct Maildir_entry
{
111 static int num_maildir_entries
= 0;
119 extern char response
[];
121 /* This is an attempt to simplify things by putting all the
122 * privilege ops into macros.
123 * *GROUPPRIVS() is related to handling the setgid MAIL property,
124 * and only applies if MAILGROUP is defined.
125 * Basically, SAVEGROUPPRIVS() is called right at the top of main()
126 * to initialise things, and then DROPGROUPPRIVS() and GETGROUPPRIVS()
127 * do the obvious thing.
129 * There's probably a better implementation if we're allowed to use
130 * BSD-style setreuid() rather than using POSIX saved-ids.
131 * Anyway, if you're euid root it's a bit pointless to drop the group
134 * I'm pretty happy that the security is good provided we aren't setuid root.
135 * The only things we trust with group=mail privilege are lkfopen()
140 * For setting and returning to "mail" gid
143 static gid_t return_gid
;
144 #define TRYDROPGROUPPRIVS() DROPGROUPPRIVS()
145 #define DROPGROUPPRIVS() \
146 if (setegid(getgid()) != 0) { \
147 adios ("setegid", "unable to restore group to %ld", (long) getgid()); \
149 #define GETGROUPPRIVS() \
150 if (setegid(return_gid) != 0) { \
151 adios ("setegid", "unable to set group to %ld", (long) return_gid); \
153 #define SAVEGROUPPRIVS() return_gid = getegid()
155 /* define *GROUPPRIVS() as null; this avoids having lots of "#ifdef MAILGROUP"s */
156 #define TRYDROPGROUPPRIVS()
157 #define DROPGROUPPRIVS()
158 #define GETGROUPPRIVS()
159 #define SAVEGROUPPRIVS()
160 #endif /* not MAILGROUP */
162 /* these variables have to be globals so that done() can correctly clean up the lockfile */
164 static char *newmail
;
170 static int maildir_srt(const void *va
, const void *vb
) PURE
;
171 static void inc_done(int) NORETURN
;
172 static int pop_action(void *closure
, char *);
175 maildir_srt(const void *va
, const void *vb
)
177 const struct Maildir_entry
*a
= va
, *b
= vb
;
178 if (a
->mtime
> b
->mtime
)
180 if (a
->mtime
< b
->mtime
)
186 main (int argc
, char **argv
)
193 int hghnum
= 0, msgnum
= 0;
195 bool sasl
, tls
, noverify
;
196 int incerr
= 0; /* <0 if inc hits an error which means it should not truncate mailspool */
197 char *cp
, *maildir
= NULL
, *folder
= NULL
;
198 char *format
= NULL
, *form
= NULL
;
199 char *host
= NULL
, *port
= NULL
, *user
= NULL
, *proxy
= NULL
;
200 char *audfile
= NULL
, *from
= NULL
, *saslmech
= NULL
, *auth_svc
= NULL
;
201 char buf
[BUFSIZ
], **argp
, *nfs
, **arguments
;
202 struct msgs
*mp
= NULL
;
205 char b
[PATH_MAX
+ 1];
206 char *maildir_copy
= NULL
; /* copy of mail directory because the static gets overwritten */
209 char *MAILHOST_env_variable
;
212 /* absolutely the first thing we do is save our privileges,
213 * and drop them if we can.
218 if (nmh_init(argv
[0], true, true)) { return 1; }
221 arguments
= getarguments (invo_name
, argc
, argv
, 1);
226 * use MAILHOST environment variable if present,
228 * If that fails, use the default (if any)
229 * provided by mts.conf in mts_init()
231 if ((MAILHOST_env_variable
= getenv("MAILHOST")) != NULL
)
232 pophost
= MAILHOST_env_variable
;
234 * If there is a valid "pophost" entry in mts.conf,
235 * then use it as the default host.
237 if (pophost
&& *pophost
)
241 chgflag
= noisy
= noverify
= true;
242 while ((cp
= *argp
++)) {
244 switch (smatch (++cp
, switches
)) {
246 ambigsw (cp
, switches
);
249 die("-%s unknown", cp
);
252 snprintf (buf
, sizeof(buf
), "%s [+folder] [switches]", invo_name
);
253 print_help (buf
, switches
, 1);
256 print_version(invo_name
);
260 if (!(cp
= *argp
++) || *cp
== '-')
261 die("missing argument to %s", argp
[-2]);
262 audfile
= mh_xstrdup(m_maildir(cp
));
276 * The flag `trnflag' has the value:
278 * 2 if -truncate is given
279 * 1 by default (truncating is default)
280 * 0 if -notruncate is given
290 if (!(cp
= *argp
++) || *cp
== '-')
291 die("missing argument to %s", argp
[-2]);
292 from
= path (cp
, TFILE
);
295 * If the truncate file is in default state,
296 * change to not truncate.
310 if (!(form
= *argp
++) || *form
== '-')
311 die("missing argument to %s", argp
[-2]);
315 if (!(format
= *argp
++) || *format
== '-')
316 die("missing argument to %s", argp
[-2]);
321 if (!(cp
= *argp
++) || *cp
== '-')
322 die("missing argument to %s", argp
[-2]);
327 if (!(host
= *argp
++) || *host
== '-')
328 die("missing argument to %s", argp
[-2]);
332 if (!(port
= *argp
++) || *port
== '-')
333 die("missing argument to %s", argp
[-2]);
337 if (!(user
= *argp
++) || *user
== '-')
338 die("missing argument to %s", argp
[-2]);
353 if (!(saslmech
= *argp
++) || *saslmech
== '-')
354 die("missing argument to %s", argp
[-2]);
375 if (!(auth_svc
= *argp
++) || *auth_svc
== '-')
376 die("missing argument to %s", argp
[-2]);
378 die("not built with OAuth support");
383 if (!(proxy
= *argp
++) || *proxy
== '-')
384 die("missing argument to %s", argp
[-2]);
388 if (*cp
== '+' || *cp
== '@') {
390 die("only one folder at a time!");
391 folder
= pluspath (cp
);
393 die("usage: %s [+folder] [switches]", invo_name
);
397 /* NOTE: above this point you should use TRYDROPGROUPPRIVS(),
398 * not DROPGROUPPRIVS().
403 /* guarantee dropping group privileges; we might not have done so earlier */
406 /* Source of mail; -from overrides any -host. */
407 inc_type
= host
&& !from
? INC_POP
: INC_FILE
;
409 if (inc_type
== INC_POP
) {
410 /* Mail from a POP server. */
413 if (auth_svc
== NULL
) {
414 if (saslmech
&& ! strcasecmp(saslmech
, "xoauth2")) {
415 die("must specify -authservice with -saslmech xoauth2");
419 die("must specify -user with -saslmech xoauth2");
424 tlsflag
|= P_INITTLS
;
427 tlsflag
|= P_NOVERIFY
;
430 * initialize POP connection
432 if (pop_init (host
, port
, user
, proxy
, snoop
, sasl
, saslmech
,
433 tlsflag
, auth_svc
) == NOTOK
)
436 /* Check if there are any messages */
437 if (pop_stat (&nmsgs
, &nbytes
) == NOTOK
)
442 die("no mail to incorporate");
445 } else if (inc_type
== INC_FILE
) {
446 /* Mail from a spool file, or Maildir. */
449 else if ((newmail
= getenv ("MAILDROP")) && *newmail
)
450 newmail
= m_mailpath (newmail
);
451 else if ((newmail
= context_find ("maildrop")) && *newmail
)
452 newmail
= m_mailpath (newmail
);
454 newmail
= concat (MAILDIR
, "/", MAILFIL
, NULL
);
456 if (stat (newmail
, &s1
) == NOTOK
|| s1
.st_size
== 0)
457 die("no mail to incorporate");
458 if (s1
.st_mode
& S_IFDIR
) {
464 cp
= concat (newmail
, "/new", NULL
);
465 if ((md
= opendir(cp
)) == NULL
)
466 die("unable to open %s", cp
);
467 while ((de
= readdir (md
)) != NULL
) {
468 if (de
->d_name
[0] == '.')
470 if (i
>= num_maildir_entries
) {
471 if ((Maildir
= realloc(Maildir
, sizeof(*Maildir
) * (2*i
+16))) == NULL
)
472 die("not enough memory for %d messages", 2*i
+16);
473 num_maildir_entries
= 2*i
+16;
475 Maildir
[i
].filename
= concat (cp
, "/", de
->d_name
, NULL
);
476 if (stat(Maildir
[i
].filename
, &ms
) != 0)
477 adios (Maildir
[i
].filename
, "couldn't get delivery time");
478 Maildir
[i
].mtime
= ms
.st_mtime
;
483 cp
= concat (newmail
, "/cur", NULL
);
484 if ((md
= opendir(cp
)) == NULL
)
485 die("unable to open %s", cp
);
486 while ((de
= readdir (md
)) != NULL
) {
487 if (de
->d_name
[0] == '.')
489 if (i
>= num_maildir_entries
) {
490 if ((Maildir
= realloc(Maildir
, sizeof(*Maildir
) * (2*i
+16))) == NULL
)
491 die("not enough memory for %d messages", 2*i
+16);
492 num_maildir_entries
= 2*i
+16;
494 Maildir
[i
].filename
= concat (cp
, "/", de
->d_name
, NULL
);
495 if (stat(Maildir
[i
].filename
, &ms
) != 0)
496 adios (Maildir
[i
].filename
, "couldn't get delivery time");
497 Maildir
[i
].mtime
= ms
.st_mtime
;
503 die("no mail to incorporate");
504 num_maildir_entries
= i
;
505 qsort (Maildir
, num_maildir_entries
, sizeof(*Maildir
), maildir_srt
);
508 cp
= mh_xstrdup(newmail
);
512 if (!context_find ("path"))
513 free (path ("./", TFOLDER
));
515 folder
= getfolder (0);
516 maildir
= m_maildir (folder
);
517 maildir_copy
= mh_xstrdup(maildir
);
519 if (!folder_exists(maildir
)) {
520 /* If the folder doesn't exist, and we're given the -silent flag,
524 create_folder(maildir
, 0, done
);
529 if (chdir (maildir
) == NOTOK
)
530 adios (maildir
, "unable to change directory to");
532 /* read folder and create message structure */
533 if (!(mp
= folder_read (folder
, 0)))
534 die("unable to read folder %s", folder
);
536 if (inc_type
== INC_FILE
&& Maildir
== NULL
) {
537 /* Mail from a spool file. */
539 if (access (newmail
, W_OK
) != NOTOK
) {
542 SIGNAL (SIGHUP
, SIG_IGN
);
543 SIGNAL (SIGINT
, SIG_IGN
);
544 SIGNAL (SIGQUIT
, SIG_IGN
);
545 SIGNAL (SIGTERM
, SIG_IGN
);
548 GETGROUPPRIVS(); /* Reset gid to lock mail file */
549 in
= lkfopenspool (newmail
, "r");
552 die("unable to lock and fopen %s", newmail
);
553 fstat (fileno(in
), &s1
);
556 if ((in
= fopen (newmail
, "r")) == NULL
)
557 adios (newmail
, "unable to read");
561 /* This shouldn't be necessary but it can't hurt. */
566 if ((i
= stat (audfile
, &st
)) == NOTOK
)
567 inform("Creating Receive-Audit: %s", audfile
);
568 if ((aud
= fopen (audfile
, "a")) == NULL
)
569 adios (audfile
, "unable to append to");
571 chmod (audfile
, m_gmprot ());
574 fprintf (aud
, "<<inc>> %s -ms %s\n", dtimenow(0), from
);
577 fprintf (aud
, "<<inc>> %s -host %s -user %s\n", dtimenow(0),
580 fprintf (aud
, "<<inc>> %s\n", dtimenow (0));
584 /* Get new format string */
585 nfs
= new_fs (form
, format
, FORMAT
);
588 printf ("Incorporating new mail into %s...\n\n", folder
);
594 * Get the mail from a POP server
596 if (inc_type
== INC_POP
) {
597 /* Mail from a POP server. */
601 hghnum
= msgnum
= mp
->hghmsg
;
602 for (i
= 1; i
<= nmsgs
; i
++) {
603 charstring_t scanl
= NULL
;
606 cp
= mh_xstrdup(m_name (msgnum
));
607 if ((pf
= fopen (cp
, "w+")) == NULL
)
608 adios (cp
, "unable to write");
609 chmod (cp
, m_gmprot ());
613 if (pop_retr(i
, pop_action
, &pc
) == NOTOK
)
617 adios (cp
, "write error on");
618 fseek (pf
, 0L, SEEK_SET
);
619 switch (incerr
= scan (pf
, msgnum
, 0, nfs
, width
,
620 msgnum
== mp
->hghmsg
+ 1 && chgflag
,
621 1, NULL
, pc
.written
, noisy
, &scanl
)) {
623 printf ("%*d empty\n", DMAXFOLDER
, msgnum
);
629 /* advise (cp, "unable to read"); already advised */
640 fputs (charstring_buffer (scanl
), aud
);
645 charstring_free (scanl
);
647 if (ferror(pf
) || fclose (pf
)) {
649 (void) m_unlink (cp
);
652 adios (cp
, "write error on");
656 if (trnflag
&& pop_dele (i
) == NOTOK
)
662 if (pop_quit () == NOTOK
)
665 } else if (inc_type
== INC_FILE
&& Maildir
== NULL
) {
666 /* Mail from a spool file. */
668 scan_detect_mbox_style (in
); /* the MAGIC invocation... */
669 hghnum
= msgnum
= mp
->hghmsg
;
671 charstring_t scanl
= NULL
;
673 /* create scanline for new message */
674 switch (incerr
= scan (in
, msgnum
+ 1, msgnum
+ 1, nfs
, width
,
675 msgnum
== hghnum
&& chgflag
, 1, NULL
, 0L, noisy
,
683 fputs ("inc aborted!\n", aud
);
684 inform("aborted!"); /* doesn't clean up locks! */
688 inform("BUG in %s, number out of range", invo_name
);
692 inform("BUG in %s, scan() botch (%d)", invo_name
, incerr
);
698 * Run the external program hook on the message.
701 (void)snprintf(b
, sizeof (b
), "%s/%d", maildir_copy
, msgnum
+ 1);
702 (void)ext_hook("add-hook", b
, NULL
);
705 fputs (charstring_buffer (scanl
), aud
);
712 charstring_free (scanl
);
714 /* If we get here there was some sort of error from scan(),
715 * so stop processing anything more from the spool.
721 /* Mail from Maildir. */
726 hghnum
= msgnum
= mp
->hghmsg
;
727 for (i
= 0; i
< num_maildir_entries
; i
++) {
728 charstring_t scanl
= NULL
;
732 sp
= Maildir
[i
].filename
;
733 cp
= mh_xstrdup(m_name (msgnum
));
735 if (!trnflag
|| link(sp
, cp
) == -1) {
736 static char buf
[65536];
739 if ((sf
= fopen (sp
, "r")) == NULL
)
740 adios (sp
, "unable to read for copy");
741 if ((pf
= fopen (cp
, "w+")) == NULL
)
742 adios (cp
, "unable to write for copy");
743 while ((nrd
= fread(buf
, 1, sizeof(buf
), sf
)) > 0)
744 if (fwrite(buf
, 1, nrd
, pf
) != nrd
)
746 if (ferror(sf
) || fflush(pf
) || ferror(pf
)) {
748 fclose(pf
); fclose(sf
); (void) m_unlink(cp
);
750 adios(cp
, "copy error %s -> %s", sp
, cp
);
755 if (pf
== NULL
&& (pf
= fopen (cp
, "r")) == NULL
)
756 adios (cp
, "not available");
757 chmod (cp
, m_gmprot ());
759 fseek (pf
, 0L, SEEK_SET
);
760 switch (incerr
= scan (pf
, msgnum
, 0, nfs
, width
,
761 msgnum
== mp
->hghmsg
+ 1 && chgflag
,
762 1, NULL
, 0, noisy
, &scanl
)) {
764 printf ("%*d empty\n", DMAXFOLDER
, msgnum
);
770 /* advise (cp, "unable to read"); already advised */
781 * Run the external program hook on the message.
784 (void)snprintf(b
, sizeof (b
), "%s/%d", maildir_copy
, msgnum
+ 1);
785 (void)ext_hook("add-hook", b
, NULL
);
788 fputs (charstring_buffer (scanl
), aud
);
793 charstring_free (scanl
);
795 if (ferror(pf
) || fclose (pf
)) {
797 (void) m_unlink (cp
);
799 adios (cp
, "write error on");
804 if (trnflag
&& m_unlink (sp
) == NOTOK
)
805 adios (sp
, "couldn't unlink");
806 free (sp
); /* Free Maildir[i]->filename */
810 free (Maildir
); /* From now on Maildir is just a flag - don't dref! */
815 if (incerr
< 0) { /* error */
817 GETGROUPPRIVS(); /* Be sure we can unlock mail file */
818 (void) lkfclosespool (in
, newmail
); in
= NULL
;
819 DROPGROUPPRIVS(); /* And then return us to normal privileges */
821 fclose (in
); in
= NULL
;
832 if (inc_type
== INC_FILE
&& Maildir
== NULL
) {
833 /* Mail from a spool file; truncate it. */
836 if (stat (newmail
, &st
) != NOTOK
&& s1
.st_mtime
!= st
.st_mtime
)
837 inform("new messages have arrived!\007");
840 if ((newfd
= creat (newmail
, 0600)) != NOTOK
)
843 admonish (newmail
, "error zero'ing");
847 printf ("%s not zero'd\n", newmail
);
851 if (msgnum
== hghnum
) {
852 inform("no messages incorporated, continuing...");
855 * Lock the sequence file now, and loop to set the right flags
856 * in the folder structure
862 context_replace (pfolder
, folder
); /* update current folder */
864 if ((mp2
= folder_read(folder
, 1)) == NULL
) {
865 inform("Unable to reread folder %s, continuing...", folder
);
870 * Shouldn't happen, but just in case ...
873 if (msgnum
>= mp2
->hghoff
874 && !(mp2
= folder_realloc (mp2
, mp2
->lowoff
, msgnum
+ 1))) {
875 inform("unable to reallocate folder storage");
880 mp2
->curmsg
= hghnum
+ 1;
881 mp2
->hghmsg
= msgnum
;
883 if (mp2
->lowmsg
== 0)
885 if (chgflag
) /* sigh... */
886 seq_setcur (mp2
, mp2
->curmsg
);
888 for (i
= hghnum
+ 1; i
<= msgnum
; i
++) {
889 clear_msg_flags (mp2
, i
);
893 mp2
->msgflags
|= SEQMOD
;
894 seq_setunseen(mp2
, 0); /* Set the Unseen-Sequence */
895 seq_save(mp2
); /* Save the sequence file */
900 if (inc_type
== INC_FILE
&& Maildir
== NULL
) {
901 /* Mail from a spool file; unlock it. */
904 GETGROUPPRIVS(); /* Be sure we can unlock mail file */
905 (void) lkfclosespool (in
, newmail
); in
= NULL
;
906 DROPGROUPPRIVS(); /* And then return us to normal privileges */
908 fclose (in
); in
= NULL
;
912 context_save (); /* save the context file */
919 inc_done (int status
)
925 lkfclosespool(in
, newmail
);
932 pop_action(void *closure
, char *s
)
938 n
= fprintf(pc
->mailout
, "%s\n", s
);
941 pc
->written
+= n
; /* Count linefeed too. */