]>
diplodocus.org Git - nmh/blob - uip/inc.c
d54ae791bdd7b6abee1ee0fd6cf6bb9d23904047
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/print_version.h"
35 #include "sbr/print_help.h"
36 #include "sbr/error.h"
39 #include "h/dropsbr.h"
41 #include "h/fmt_scan.h"
42 #include "h/scansbr.h"
43 #include "h/signals.h"
47 #include "sbr/lock_file.h"
48 #include "sbr/m_maildir.h"
49 #include "sbr/m_mktemp.h"
52 # define TLSminc(a) (a)
57 #define INC_SWITCHES \
58 X("audit audit-file", 0, AUDSW) \
59 X("noaudit", 0, NAUDSW) \
60 X("changecur", 0, CHGSW) \
61 X("nochangecur", 0, NCHGSW) \
62 X("file name", 0, FILESW) \
63 X("form formatfile", 0, FORMSW) \
64 X("format string", 5, FMTSW) \
65 X("host hostname", 0, HOSTSW) \
66 X("user username", 0, USERSW) \
67 X("port name/number", 0, PORTSW) \
68 X("silent", 0, SILSW) \
69 X("nosilent", 0, NSILSW) \
70 X("truncate", 0, TRNCSW) \
71 X("notruncate", 0, NTRNCSW) \
72 X("width columns", 0, WIDTHSW) \
73 X("version", 0, VERSIONSW) \
74 X("help", 0, HELPSW) \
75 X("snoop", 0, SNOOPSW) \
76 X("sasl", 0, SASLSW) \
77 X("nosasl", 0, NOSASLSW) \
78 X("saslmech", 0, SASLMECHSW) \
79 X("initialtls", TLSminc(-10), INITTLSSW) \
80 X("notls", TLSminc(-5), NOTLSSW) \
81 X("certverify", TLSminc(-10), CERTVERSW) \
82 X("nocertverify", TLSminc(-12), NOCERTVERSW) \
83 X("authservice", 0, AUTHSERVICESW) \
84 X("proxy command", 0, PROXYSW) \
86 #define X(sw, minchars, id) id,
87 DEFINE_SWITCH_ENUM(INC
);
90 #define X(sw, minchars, id) { sw, minchars, id },
91 DEFINE_SWITCH_ARRAY(INC
, switches
);
95 * flags for the mail source
100 static struct Maildir_entry
{
104 static int num_maildir_entries
= 0;
112 extern char response
[];
114 /* This is an attempt to simplify things by putting all the
115 * privilege ops into macros.
116 * *GROUPPRIVS() is related to handling the setgid MAIL property,
117 * and only applies if MAILGROUP is defined.
118 * Basically, SAVEGROUPPRIVS() is called right at the top of main()
119 * to initialise things, and then DROPGROUPPRIVS() and GETGROUPPRIVS()
120 * do the obvious thing.
122 * There's probably a better implementation if we're allowed to use
123 * BSD-style setreuid() rather than using POSIX saved-ids.
124 * Anyway, if you're euid root it's a bit pointless to drop the group
127 * I'm pretty happy that the security is good provided we aren't setuid root.
128 * The only things we trust with group=mail privilege are lkfopen()
133 * For setting and returning to "mail" gid
136 static gid_t return_gid
;
137 #define TRYDROPGROUPPRIVS() DROPGROUPPRIVS()
138 #define DROPGROUPPRIVS() \
139 if (setegid(getgid()) != 0) { \
140 adios ("setegid", "unable to restore group to %ld", (long) getgid()); \
142 #define GETGROUPPRIVS() \
143 if (setegid(return_gid) != 0) { \
144 adios ("setegid", "unable to set group to %ld", (long) return_gid); \
146 #define SAVEGROUPPRIVS() return_gid = getegid()
148 /* define *GROUPPRIVS() as null; this avoids having lots of "#ifdef MAILGROUP"s */
149 #define TRYDROPGROUPPRIVS()
150 #define DROPGROUPPRIVS()
151 #define GETGROUPPRIVS()
152 #define SAVEGROUPPRIVS()
153 #endif /* not MAILGROUP */
155 /* these variables have to be globals so that done() can correctly clean up the lockfile */
157 static char *newmail
;
163 static int maildir_srt(const void *va
, const void *vb
) PURE
;
164 static void inc_done(int) NORETURN
;
165 static int pop_action(void *closure
, char *);
168 maildir_srt(const void *va
, const void *vb
)
170 const struct Maildir_entry
*a
= va
, *b
= vb
;
171 if (a
->mtime
> b
->mtime
)
173 if (a
->mtime
< b
->mtime
)
179 main (int argc
, char **argv
)
186 int hghnum
= 0, msgnum
= 0;
188 bool sasl
, tls
, noverify
;
189 int incerr
= 0; /* <0 if inc hits an error which means it should not truncate mailspool */
190 char *cp
, *maildir
= NULL
, *folder
= NULL
;
191 char *format
= NULL
, *form
= NULL
;
192 char *host
= NULL
, *port
= NULL
, *user
= NULL
, *proxy
= NULL
;
193 char *audfile
= NULL
, *from
= NULL
, *saslmech
= NULL
, *auth_svc
= NULL
;
194 char buf
[BUFSIZ
], **argp
, *nfs
, **arguments
;
195 struct msgs
*mp
= NULL
;
198 char b
[PATH_MAX
+ 1];
199 char *maildir_copy
= NULL
; /* copy of mail directory because the static gets overwritten */
202 char *MAILHOST_env_variable
;
205 /* absolutely the first thing we do is save our privileges,
206 * and drop them if we can.
211 if (nmh_init(argv
[0], true, true)) { return 1; }
214 arguments
= getarguments (invo_name
, argc
, argv
, 1);
219 * use MAILHOST environment variable if present,
221 * If that fails, use the default (if any)
222 * provided by mts.conf in mts_init()
224 if ((MAILHOST_env_variable
= getenv("MAILHOST")) != NULL
)
225 pophost
= MAILHOST_env_variable
;
227 * If there is a valid "pophost" entry in mts.conf,
228 * then use it as the default host.
230 if (pophost
&& *pophost
)
234 chgflag
= noisy
= noverify
= true;
235 while ((cp
= *argp
++)) {
237 switch (smatch (++cp
, switches
)) {
239 ambigsw (cp
, switches
);
242 die("-%s unknown", cp
);
245 snprintf (buf
, sizeof(buf
), "%s [+folder] [switches]", invo_name
);
246 print_help (buf
, switches
, 1);
249 print_version(invo_name
);
253 if (!(cp
= *argp
++) || *cp
== '-')
254 die("missing argument to %s", argp
[-2]);
255 audfile
= mh_xstrdup(m_maildir(cp
));
269 * The flag `trnflag' has the value:
271 * 2 if -truncate is given
272 * 1 by default (truncating is default)
273 * 0 if -notruncate is given
283 if (!(cp
= *argp
++) || *cp
== '-')
284 die("missing argument to %s", argp
[-2]);
285 from
= path (cp
, TFILE
);
288 * If the truncate file is in default state,
289 * change to not truncate.
303 if (!(form
= *argp
++) || *form
== '-')
304 die("missing argument to %s", argp
[-2]);
308 if (!(format
= *argp
++) || *format
== '-')
309 die("missing argument to %s", argp
[-2]);
314 if (!(cp
= *argp
++) || *cp
== '-')
315 die("missing argument to %s", argp
[-2]);
320 if (!(host
= *argp
++) || *host
== '-')
321 die("missing argument to %s", argp
[-2]);
325 if (!(port
= *argp
++) || *port
== '-')
326 die("missing argument to %s", argp
[-2]);
330 if (!(user
= *argp
++) || *user
== '-')
331 die("missing argument to %s", argp
[-2]);
346 if (!(saslmech
= *argp
++) || *saslmech
== '-')
347 die("missing argument to %s", argp
[-2]);
368 if (!(auth_svc
= *argp
++) || *auth_svc
== '-')
369 die("missing argument to %s", argp
[-2]);
371 die("not built with OAuth support");
376 if (!(proxy
= *argp
++) || *proxy
== '-')
377 die("missing argument to %s", argp
[-2]);
381 if (*cp
== '+' || *cp
== '@') {
383 die("only one folder at a time!");
384 folder
= pluspath (cp
);
386 die("usage: %s [+folder] [switches]", invo_name
);
390 /* NOTE: above this point you should use TRYDROPGROUPPRIVS(),
391 * not DROPGROUPPRIVS().
396 /* guarantee dropping group privileges; we might not have done so earlier */
399 /* Source of mail; -from overrides any -host. */
400 inc_type
= host
&& !from
? INC_POP
: INC_FILE
;
402 if (inc_type
== INC_POP
) {
403 /* Mail from a POP server. */
406 if (auth_svc
== NULL
) {
407 if (saslmech
&& ! strcasecmp(saslmech
, "xoauth2")) {
408 die("must specify -authservice with -saslmech xoauth2");
412 die("must specify -user with -saslmech xoauth2");
417 tlsflag
|= P_INITTLS
;
420 tlsflag
|= P_NOVERIFY
;
423 * initialize POP connection
425 if (pop_init (host
, port
, user
, proxy
, snoop
, sasl
, saslmech
,
426 tlsflag
, auth_svc
) == NOTOK
)
429 /* Check if there are any messages */
430 if (pop_stat (&nmsgs
, &nbytes
) == NOTOK
)
435 die("no mail to incorporate");
438 } else if (inc_type
== INC_FILE
) {
439 /* Mail from a spool file, or Maildir. */
442 else if ((newmail
= getenv ("MAILDROP")) && *newmail
)
443 newmail
= m_mailpath (newmail
);
444 else if ((newmail
= context_find ("maildrop")) && *newmail
)
445 newmail
= m_mailpath (newmail
);
447 newmail
= concat (MAILDIR
, "/", MAILFIL
, NULL
);
449 if (stat (newmail
, &s1
) == NOTOK
|| s1
.st_size
== 0)
450 die("no mail to incorporate");
451 if (s1
.st_mode
& S_IFDIR
) {
457 cp
= concat (newmail
, "/new", NULL
);
458 if ((md
= opendir(cp
)) == NULL
)
459 die("unable to open %s", cp
);
460 while ((de
= readdir (md
)) != NULL
) {
461 if (de
->d_name
[0] == '.')
463 if (i
>= num_maildir_entries
) {
464 if ((Maildir
= realloc(Maildir
, sizeof(*Maildir
) * (2*i
+16))) == NULL
)
465 die("not enough memory for %d messages", 2*i
+16);
466 num_maildir_entries
= 2*i
+16;
468 Maildir
[i
].filename
= concat (cp
, "/", de
->d_name
, NULL
);
469 if (stat(Maildir
[i
].filename
, &ms
) != 0)
470 adios (Maildir
[i
].filename
, "couldn't get delivery time");
471 Maildir
[i
].mtime
= ms
.st_mtime
;
476 cp
= concat (newmail
, "/cur", NULL
);
477 if ((md
= opendir(cp
)) == NULL
)
478 die("unable to open %s", cp
);
479 while ((de
= readdir (md
)) != NULL
) {
480 if (de
->d_name
[0] == '.')
482 if (i
>= num_maildir_entries
) {
483 if ((Maildir
= realloc(Maildir
, sizeof(*Maildir
) * (2*i
+16))) == NULL
)
484 die("not enough memory for %d messages", 2*i
+16);
485 num_maildir_entries
= 2*i
+16;
487 Maildir
[i
].filename
= concat (cp
, "/", de
->d_name
, NULL
);
488 if (stat(Maildir
[i
].filename
, &ms
) != 0)
489 adios (Maildir
[i
].filename
, "couldn't get delivery time");
490 Maildir
[i
].mtime
= ms
.st_mtime
;
496 die("no mail to incorporate");
497 num_maildir_entries
= i
;
498 qsort (Maildir
, num_maildir_entries
, sizeof(*Maildir
), maildir_srt
);
501 cp
= mh_xstrdup(newmail
);
505 if (!context_find ("path"))
506 free (path ("./", TFOLDER
));
508 folder
= getfolder (0);
509 maildir
= m_maildir (folder
);
510 maildir_copy
= mh_xstrdup(maildir
);
512 if (!folder_exists(maildir
)) {
513 /* If the folder doesn't exist, and we're given the -silent flag,
517 create_folder(maildir
, 0, done
);
522 if (chdir (maildir
) == NOTOK
)
523 adios (maildir
, "unable to change directory to");
525 /* read folder and create message structure */
526 if (!(mp
= folder_read (folder
, 0)))
527 die("unable to read folder %s", folder
);
529 if (inc_type
== INC_FILE
&& Maildir
== NULL
) {
530 /* Mail from a spool file. */
532 if (access (newmail
, W_OK
) != NOTOK
) {
535 SIGNAL (SIGHUP
, SIG_IGN
);
536 SIGNAL (SIGINT
, SIG_IGN
);
537 SIGNAL (SIGQUIT
, SIG_IGN
);
538 SIGNAL (SIGTERM
, SIG_IGN
);
541 GETGROUPPRIVS(); /* Reset gid to lock mail file */
542 in
= lkfopenspool (newmail
, "r");
545 die("unable to lock and fopen %s", newmail
);
546 fstat (fileno(in
), &s1
);
549 if ((in
= fopen (newmail
, "r")) == NULL
)
550 adios (newmail
, "unable to read");
554 /* This shouldn't be necessary but it can't hurt. */
559 if ((i
= stat (audfile
, &st
)) == NOTOK
)
560 inform("Creating Receive-Audit: %s", audfile
);
561 if ((aud
= fopen (audfile
, "a")) == NULL
)
562 adios (audfile
, "unable to append to");
564 chmod (audfile
, m_gmprot ());
567 fprintf (aud
, "<<inc>> %s -ms %s\n", dtimenow(0), from
);
570 fprintf (aud
, "<<inc>> %s -host %s -user %s\n", dtimenow(0),
573 fprintf (aud
, "<<inc>> %s\n", dtimenow (0));
577 /* Get new format string */
578 nfs
= new_fs (form
, format
, FORMAT
);
581 printf ("Incorporating new mail into %s...\n\n", folder
);
587 * Get the mail from a POP server
589 if (inc_type
== INC_POP
) {
590 /* Mail from a POP server. */
594 hghnum
= msgnum
= mp
->hghmsg
;
595 for (i
= 1; i
<= nmsgs
; i
++) {
596 charstring_t scanl
= NULL
;
599 cp
= mh_xstrdup(m_name (msgnum
));
600 if ((pf
= fopen (cp
, "w+")) == NULL
)
601 adios (cp
, "unable to write");
602 chmod (cp
, m_gmprot ());
606 if (pop_retr(i
, pop_action
, &pc
) == NOTOK
)
610 adios (cp
, "write error on");
611 fseek (pf
, 0L, SEEK_SET
);
612 switch (incerr
= scan (pf
, msgnum
, 0, nfs
, width
,
613 msgnum
== mp
->hghmsg
+ 1 && chgflag
,
614 1, NULL
, pc
.written
, noisy
, &scanl
)) {
616 printf ("%*d empty\n", DMAXFOLDER
, msgnum
);
622 /* advise (cp, "unable to read"); already advised */
633 fputs (charstring_buffer (scanl
), aud
);
638 charstring_free (scanl
);
640 if (ferror(pf
) || fclose (pf
)) {
642 (void) m_unlink (cp
);
645 adios (cp
, "write error on");
649 if (trnflag
&& pop_dele (i
) == NOTOK
)
655 if (pop_quit () == NOTOK
)
658 } else if (inc_type
== INC_FILE
&& Maildir
== NULL
) {
659 /* Mail from a spool file. */
661 scan_detect_mbox_style (in
); /* the MAGIC invocation... */
662 hghnum
= msgnum
= mp
->hghmsg
;
664 charstring_t scanl
= NULL
;
666 /* create scanline for new message */
667 switch (incerr
= scan (in
, msgnum
+ 1, msgnum
+ 1, nfs
, width
,
668 msgnum
== hghnum
&& chgflag
, 1, NULL
, 0L, noisy
,
676 fputs ("inc aborted!\n", aud
);
677 inform("aborted!"); /* doesn't clean up locks! */
681 inform("BUG in %s, number out of range", invo_name
);
685 inform("BUG in %s, scan() botch (%d)", invo_name
, incerr
);
691 * Run the external program hook on the message.
694 (void)snprintf(b
, sizeof (b
), "%s/%d", maildir_copy
, msgnum
+ 1);
695 (void)ext_hook("add-hook", b
, NULL
);
698 fputs (charstring_buffer (scanl
), aud
);
705 charstring_free (scanl
);
707 /* If we get here there was some sort of error from scan(),
708 * so stop processing anything more from the spool.
714 /* Mail from Maildir. */
719 hghnum
= msgnum
= mp
->hghmsg
;
720 for (i
= 0; i
< num_maildir_entries
; i
++) {
721 charstring_t scanl
= NULL
;
725 sp
= Maildir
[i
].filename
;
726 cp
= mh_xstrdup(m_name (msgnum
));
728 if (!trnflag
|| link(sp
, cp
) == -1) {
729 static char buf
[65536];
732 if ((sf
= fopen (sp
, "r")) == NULL
)
733 adios (sp
, "unable to read for copy");
734 if ((pf
= fopen (cp
, "w+")) == NULL
)
735 adios (cp
, "unable to write for copy");
736 while ((nrd
= fread(buf
, 1, sizeof(buf
), sf
)) > 0)
737 if (fwrite(buf
, 1, nrd
, pf
) != nrd
)
739 if (ferror(sf
) || fflush(pf
) || ferror(pf
)) {
741 fclose(pf
); fclose(sf
); (void) m_unlink(cp
);
743 adios(cp
, "copy error %s -> %s", sp
, cp
);
748 if (pf
== NULL
&& (pf
= fopen (cp
, "r")) == NULL
)
749 adios (cp
, "not available");
750 chmod (cp
, m_gmprot ());
752 fseek (pf
, 0L, SEEK_SET
);
753 switch (incerr
= scan (pf
, msgnum
, 0, nfs
, width
,
754 msgnum
== mp
->hghmsg
+ 1 && chgflag
,
755 1, NULL
, 0, noisy
, &scanl
)) {
757 printf ("%*d empty\n", DMAXFOLDER
, msgnum
);
763 /* advise (cp, "unable to read"); already advised */
774 * Run the external program hook on the message.
777 (void)snprintf(b
, sizeof (b
), "%s/%d", maildir_copy
, msgnum
+ 1);
778 (void)ext_hook("add-hook", b
, NULL
);
781 fputs (charstring_buffer (scanl
), aud
);
786 charstring_free (scanl
);
788 if (ferror(pf
) || fclose (pf
)) {
790 (void) m_unlink (cp
);
792 adios (cp
, "write error on");
797 if (trnflag
&& m_unlink (sp
) == NOTOK
)
798 adios (sp
, "couldn't unlink");
799 free (sp
); /* Free Maildir[i]->filename */
803 free (Maildir
); /* From now on Maildir is just a flag - don't dref! */
808 if (incerr
< 0) { /* error */
810 GETGROUPPRIVS(); /* Be sure we can unlock mail file */
811 (void) lkfclosespool (in
, newmail
); in
= NULL
;
812 DROPGROUPPRIVS(); /* And then return us to normal privileges */
814 fclose (in
); in
= NULL
;
825 if (inc_type
== INC_FILE
&& Maildir
== NULL
) {
826 /* Mail from a spool file; truncate it. */
829 if (stat (newmail
, &st
) != NOTOK
&& s1
.st_mtime
!= st
.st_mtime
)
830 inform("new messages have arrived!\007");
833 if ((newfd
= creat (newmail
, 0600)) != NOTOK
)
836 admonish (newmail
, "error zero'ing");
840 printf ("%s not zero'd\n", newmail
);
844 if (msgnum
== hghnum
) {
845 inform("no messages incorporated, continuing...");
848 * Lock the sequence file now, and loop to set the right flags
849 * in the folder structure
855 context_replace (pfolder
, folder
); /* update current folder */
857 if ((mp2
= folder_read(folder
, 1)) == NULL
) {
858 inform("Unable to reread folder %s, continuing...", folder
);
863 * Shouldn't happen, but just in case ...
866 if (msgnum
>= mp2
->hghoff
867 && !(mp2
= folder_realloc (mp2
, mp2
->lowoff
, msgnum
+ 1))) {
868 inform("unable to reallocate folder storage");
873 mp2
->curmsg
= hghnum
+ 1;
874 mp2
->hghmsg
= msgnum
;
876 if (mp2
->lowmsg
== 0)
878 if (chgflag
) /* sigh... */
879 seq_setcur (mp2
, mp2
->curmsg
);
881 for (i
= hghnum
+ 1; i
<= msgnum
; i
++) {
882 clear_msg_flags (mp2
, i
);
886 mp2
->msgflags
|= SEQMOD
;
887 seq_setunseen(mp2
, 0); /* Set the Unseen-Sequence */
888 seq_save(mp2
); /* Save the sequence file */
893 if (inc_type
== INC_FILE
&& Maildir
== NULL
) {
894 /* Mail from a spool file; unlock it. */
897 GETGROUPPRIVS(); /* Be sure we can unlock mail file */
898 (void) lkfclosespool (in
, newmail
); in
= NULL
;
899 DROPGROUPPRIVS(); /* And then return us to normal privileges */
901 fclose (in
); in
= NULL
;
905 context_save (); /* save the context file */
912 inc_done (int status
)
918 lkfclosespool(in
, newmail
);
925 pop_action(void *closure
, char *s
)
931 n
= fprintf(pc
->mailout
, "%s\n", s
);
934 pc
->written
+= n
; /* Count linefeed too. */