]>
diplodocus.org Git - nmh/blob - uip/inc.c
5fde59a815483875555164fe6f6790f114e6731a
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/ambigsw.h"
36 #include "sbr/print_version.h"
37 #include "sbr/print_help.h"
38 #include "sbr/error.h"
41 #include "h/dropsbr.h"
43 #include "h/fmt_scan.h"
44 #include "h/scansbr.h"
45 #include "h/signals.h"
49 #include "sbr/lock_file.h"
50 #include "sbr/m_maildir.h"
51 #include "sbr/m_mktemp.h"
54 # define TLSminc(a) (a)
59 #define INC_SWITCHES \
60 X("audit audit-file", 0, AUDSW) \
61 X("noaudit", 0, NAUDSW) \
62 X("changecur", 0, CHGSW) \
63 X("nochangecur", 0, NCHGSW) \
64 X("file name", 0, FILESW) \
65 X("form formatfile", 0, FORMSW) \
66 X("format string", 5, FMTSW) \
67 X("host hostname", 0, HOSTSW) \
68 X("user username", 0, USERSW) \
69 X("port name/number", 0, PORTSW) \
70 X("silent", 0, SILSW) \
71 X("nosilent", 0, NSILSW) \
72 X("truncate", 0, TRNCSW) \
73 X("notruncate", 0, NTRNCSW) \
74 X("width columns", 0, WIDTHSW) \
75 X("version", 0, VERSIONSW) \
76 X("help", 0, HELPSW) \
77 X("snoop", 0, SNOOPSW) \
78 X("sasl", 0, SASLSW) \
79 X("nosasl", 0, NOSASLSW) \
80 X("saslmech", 0, SASLMECHSW) \
81 X("initialtls", TLSminc(-10), INITTLSSW) \
82 X("notls", TLSminc(-5), NOTLSSW) \
83 X("certverify", TLSminc(-10), CERTVERSW) \
84 X("nocertverify", TLSminc(-12), NOCERTVERSW) \
85 X("authservice", 0, AUTHSERVICESW) \
86 X("proxy command", 0, PROXYSW) \
88 #define X(sw, minchars, id) id,
89 DEFINE_SWITCH_ENUM(INC
);
92 #define X(sw, minchars, id) { sw, minchars, id },
93 DEFINE_SWITCH_ARRAY(INC
, switches
);
97 * flags for the mail source
102 static struct Maildir_entry
{
106 static int num_maildir_entries
= 0;
114 extern char response
[];
116 /* This is an attempt to simplify things by putting all the
117 * privilege ops into macros.
118 * *GROUPPRIVS() is related to handling the setgid MAIL property,
119 * and only applies if MAILGROUP is defined.
120 * Basically, SAVEGROUPPRIVS() is called right at the top of main()
121 * to initialise things, and then DROPGROUPPRIVS() and GETGROUPPRIVS()
122 * do the obvious thing.
124 * There's probably a better implementation if we're allowed to use
125 * BSD-style setreuid() rather than using POSIX saved-ids.
126 * Anyway, if you're euid root it's a bit pointless to drop the group
129 * I'm pretty happy that the security is good provided we aren't setuid root.
130 * The only things we trust with group=mail privilege are lkfopen()
135 * For setting and returning to "mail" gid
138 static gid_t return_gid
;
139 #define TRYDROPGROUPPRIVS() DROPGROUPPRIVS()
140 #define DROPGROUPPRIVS() \
141 if (setegid(getgid()) != 0) { \
142 adios ("setegid", "unable to restore group to %ld", (long) getgid()); \
144 #define GETGROUPPRIVS() \
145 if (setegid(return_gid) != 0) { \
146 adios ("setegid", "unable to set group to %ld", (long) return_gid); \
148 #define SAVEGROUPPRIVS() return_gid = getegid()
150 /* define *GROUPPRIVS() as null; this avoids having lots of "#ifdef MAILGROUP"s */
151 #define TRYDROPGROUPPRIVS()
152 #define DROPGROUPPRIVS()
153 #define GETGROUPPRIVS()
154 #define SAVEGROUPPRIVS()
155 #endif /* not MAILGROUP */
157 /* these variables have to be globals so that done() can correctly clean up the lockfile */
159 static char *newmail
;
165 static int maildir_srt(const void *va
, const void *vb
) PURE
;
166 static void inc_done(int) NORETURN
;
167 static int pop_action(void *closure
, char *);
170 maildir_srt(const void *va
, const void *vb
)
172 const struct Maildir_entry
*a
= va
, *b
= vb
;
173 if (a
->mtime
> b
->mtime
)
175 if (a
->mtime
< b
->mtime
)
181 main (int argc
, char **argv
)
188 int hghnum
= 0, msgnum
= 0;
190 bool sasl
, tls
, noverify
;
191 int incerr
= 0; /* <0 if inc hits an error which means it should not truncate mailspool */
192 char *cp
, *maildir
= NULL
, *folder
= NULL
;
193 char *format
= NULL
, *form
= NULL
;
194 char *host
= NULL
, *port
= NULL
, *user
= NULL
, *proxy
= NULL
;
195 char *audfile
= NULL
, *from
= NULL
, *saslmech
= NULL
, *auth_svc
= NULL
;
196 char buf
[BUFSIZ
], **argp
, *nfs
, **arguments
;
197 struct msgs
*mp
= NULL
;
200 char b
[PATH_MAX
+ 1];
201 char *maildir_copy
= NULL
; /* copy of mail directory because the static gets overwritten */
204 char *MAILHOST_env_variable
;
207 /* absolutely the first thing we do is save our privileges,
208 * and drop them if we can.
213 if (nmh_init(argv
[0], true, true)) { return 1; }
216 arguments
= getarguments (invo_name
, argc
, argv
, 1);
221 * use MAILHOST environment variable if present,
223 * If that fails, use the default (if any)
224 * provided by mts.conf in mts_init()
226 if ((MAILHOST_env_variable
= getenv("MAILHOST")) != NULL
)
227 pophost
= MAILHOST_env_variable
;
229 * If there is a valid "pophost" entry in mts.conf,
230 * then use it as the default host.
232 if (pophost
&& *pophost
)
236 chgflag
= noisy
= noverify
= true;
237 while ((cp
= *argp
++)) {
239 switch (smatch (++cp
, switches
)) {
241 ambigsw (cp
, switches
);
244 die("-%s unknown", cp
);
247 snprintf (buf
, sizeof(buf
), "%s [+folder] [switches]", invo_name
);
248 print_help (buf
, switches
, 1);
251 print_version(invo_name
);
255 if (!(cp
= *argp
++) || *cp
== '-')
256 die("missing argument to %s", argp
[-2]);
257 audfile
= mh_xstrdup(m_maildir(cp
));
271 * The flag `trnflag' has the value:
273 * 2 if -truncate is given
274 * 1 by default (truncating is default)
275 * 0 if -notruncate is given
285 if (!(cp
= *argp
++) || *cp
== '-')
286 die("missing argument to %s", argp
[-2]);
287 from
= path (cp
, TFILE
);
290 * If the truncate file is in default state,
291 * change to not truncate.
305 if (!(form
= *argp
++) || *form
== '-')
306 die("missing argument to %s", argp
[-2]);
310 if (!(format
= *argp
++) || *format
== '-')
311 die("missing argument to %s", argp
[-2]);
316 if (!(cp
= *argp
++) || *cp
== '-')
317 die("missing argument to %s", argp
[-2]);
322 if (!(host
= *argp
++) || *host
== '-')
323 die("missing argument to %s", argp
[-2]);
327 if (!(port
= *argp
++) || *port
== '-')
328 die("missing argument to %s", argp
[-2]);
332 if (!(user
= *argp
++) || *user
== '-')
333 die("missing argument to %s", argp
[-2]);
348 if (!(saslmech
= *argp
++) || *saslmech
== '-')
349 die("missing argument to %s", argp
[-2]);
370 if (!(auth_svc
= *argp
++) || *auth_svc
== '-')
371 die("missing argument to %s", argp
[-2]);
373 die("not built with OAuth support");
378 if (!(proxy
= *argp
++) || *proxy
== '-')
379 die("missing argument to %s", argp
[-2]);
383 if (*cp
== '+' || *cp
== '@') {
385 die("only one folder at a time!");
386 folder
= pluspath (cp
);
388 die("usage: %s [+folder] [switches]", invo_name
);
392 /* NOTE: above this point you should use TRYDROPGROUPPRIVS(),
393 * not DROPGROUPPRIVS().
398 /* guarantee dropping group privileges; we might not have done so earlier */
401 /* Source of mail; -from overrides any -host. */
402 inc_type
= host
&& !from
? INC_POP
: INC_FILE
;
404 if (inc_type
== INC_POP
) {
405 /* Mail from a POP server. */
408 if (auth_svc
== NULL
) {
409 if (saslmech
&& ! strcasecmp(saslmech
, "xoauth2")) {
410 die("must specify -authservice with -saslmech xoauth2");
414 die("must specify -user with -saslmech xoauth2");
419 tlsflag
|= P_INITTLS
;
422 tlsflag
|= P_NOVERIFY
;
425 * initialize POP connection
427 if (pop_init (host
, port
, user
, proxy
, snoop
, sasl
, saslmech
,
428 tlsflag
, auth_svc
) == NOTOK
)
431 /* Check if there are any messages */
432 if (pop_stat (&nmsgs
, &nbytes
) == NOTOK
)
437 die("no mail to incorporate");
440 } else if (inc_type
== INC_FILE
) {
441 /* Mail from a spool file, or Maildir. */
444 else if ((newmail
= getenv ("MAILDROP")) && *newmail
)
445 newmail
= m_mailpath (newmail
);
446 else if ((newmail
= context_find ("maildrop")) && *newmail
)
447 newmail
= m_mailpath (newmail
);
449 newmail
= concat (MAILDIR
, "/", MAILFIL
, NULL
);
451 if (stat (newmail
, &s1
) == NOTOK
|| s1
.st_size
== 0)
452 die("no mail to incorporate");
453 if (s1
.st_mode
& S_IFDIR
) {
459 cp
= concat (newmail
, "/new", NULL
);
460 if ((md
= opendir(cp
)) == NULL
)
461 die("unable to open %s", cp
);
462 while ((de
= readdir (md
)) != NULL
) {
463 if (de
->d_name
[0] == '.')
465 if (i
>= num_maildir_entries
) {
466 if ((Maildir
= realloc(Maildir
, sizeof(*Maildir
) * (2*i
+16))) == NULL
)
467 die("not enough memory for %d messages", 2*i
+16);
468 num_maildir_entries
= 2*i
+16;
470 Maildir
[i
].filename
= concat (cp
, "/", de
->d_name
, NULL
);
471 if (stat(Maildir
[i
].filename
, &ms
) != 0)
472 adios (Maildir
[i
].filename
, "couldn't get delivery time");
473 Maildir
[i
].mtime
= ms
.st_mtime
;
478 cp
= concat (newmail
, "/cur", NULL
);
479 if ((md
= opendir(cp
)) == NULL
)
480 die("unable to open %s", cp
);
481 while ((de
= readdir (md
)) != NULL
) {
482 if (de
->d_name
[0] == '.')
484 if (i
>= num_maildir_entries
) {
485 if ((Maildir
= realloc(Maildir
, sizeof(*Maildir
) * (2*i
+16))) == NULL
)
486 die("not enough memory for %d messages", 2*i
+16);
487 num_maildir_entries
= 2*i
+16;
489 Maildir
[i
].filename
= concat (cp
, "/", de
->d_name
, NULL
);
490 if (stat(Maildir
[i
].filename
, &ms
) != 0)
491 adios (Maildir
[i
].filename
, "couldn't get delivery time");
492 Maildir
[i
].mtime
= ms
.st_mtime
;
498 die("no mail to incorporate");
499 num_maildir_entries
= i
;
500 qsort (Maildir
, num_maildir_entries
, sizeof(*Maildir
), maildir_srt
);
503 cp
= mh_xstrdup(newmail
);
507 if (!context_find ("path"))
508 free (path ("./", TFOLDER
));
510 folder
= getfolder (0);
511 maildir
= m_maildir (folder
);
512 maildir_copy
= mh_xstrdup(maildir
);
514 if (!folder_exists(maildir
)) {
515 /* If the folder doesn't exist, and we're given the -silent flag,
519 create_folder(maildir
, 0, done
);
524 if (chdir (maildir
) == NOTOK
)
525 adios (maildir
, "unable to change directory to");
527 /* read folder and create message structure */
528 if (!(mp
= folder_read (folder
, 0)))
529 die("unable to read folder %s", folder
);
531 if (inc_type
== INC_FILE
&& Maildir
== NULL
) {
532 /* Mail from a spool file. */
534 if (access (newmail
, W_OK
) != NOTOK
) {
537 SIGNAL (SIGHUP
, SIG_IGN
);
538 SIGNAL (SIGINT
, SIG_IGN
);
539 SIGNAL (SIGQUIT
, SIG_IGN
);
540 SIGNAL (SIGTERM
, SIG_IGN
);
543 GETGROUPPRIVS(); /* Reset gid to lock mail file */
544 in
= lkfopenspool (newmail
, "r");
547 die("unable to lock and fopen %s", newmail
);
548 fstat (fileno(in
), &s1
);
551 if ((in
= fopen (newmail
, "r")) == NULL
)
552 adios (newmail
, "unable to read");
556 /* This shouldn't be necessary but it can't hurt. */
561 if ((i
= stat (audfile
, &st
)) == NOTOK
)
562 inform("Creating Receive-Audit: %s", audfile
);
563 if ((aud
= fopen (audfile
, "a")) == NULL
)
564 adios (audfile
, "unable to append to");
566 chmod (audfile
, m_gmprot ());
569 fprintf (aud
, "<<inc>> %s -ms %s\n", dtimenow(0), from
);
572 fprintf (aud
, "<<inc>> %s -host %s -user %s\n", dtimenow(0),
575 fprintf (aud
, "<<inc>> %s\n", dtimenow (0));
579 /* Get new format string */
580 nfs
= new_fs (form
, format
, FORMAT
);
583 printf ("Incorporating new mail into %s...\n\n", folder
);
589 * Get the mail from a POP server
591 if (inc_type
== INC_POP
) {
592 /* Mail from a POP server. */
596 hghnum
= msgnum
= mp
->hghmsg
;
597 for (i
= 1; i
<= nmsgs
; i
++) {
598 charstring_t scanl
= NULL
;
601 cp
= mh_xstrdup(m_name (msgnum
));
602 if ((pf
= fopen (cp
, "w+")) == NULL
)
603 adios (cp
, "unable to write");
604 chmod (cp
, m_gmprot ());
608 if (pop_retr(i
, pop_action
, &pc
) == NOTOK
)
612 adios (cp
, "write error on");
613 fseek (pf
, 0L, SEEK_SET
);
614 switch (incerr
= scan (pf
, msgnum
, 0, nfs
, width
,
615 msgnum
== mp
->hghmsg
+ 1 && chgflag
,
616 1, NULL
, pc
.written
, noisy
, &scanl
)) {
618 printf ("%*d empty\n", DMAXFOLDER
, msgnum
);
624 /* advise (cp, "unable to read"); already advised */
635 fputs (charstring_buffer (scanl
), aud
);
640 charstring_free (scanl
);
642 if (ferror(pf
) || fclose (pf
)) {
644 (void) m_unlink (cp
);
647 adios (cp
, "write error on");
651 if (trnflag
&& pop_dele (i
) == NOTOK
)
657 if (pop_quit () == NOTOK
)
660 } else if (inc_type
== INC_FILE
&& Maildir
== NULL
) {
661 /* Mail from a spool file. */
663 scan_detect_mbox_style (in
); /* the MAGIC invocation... */
664 hghnum
= msgnum
= mp
->hghmsg
;
666 charstring_t scanl
= NULL
;
668 /* create scanline for new message */
669 switch (incerr
= scan (in
, msgnum
+ 1, msgnum
+ 1, nfs
, width
,
670 msgnum
== hghnum
&& chgflag
, 1, NULL
, 0L, noisy
,
678 fputs ("inc aborted!\n", aud
);
679 inform("aborted!"); /* doesn't clean up locks! */
683 inform("BUG in %s, number out of range", invo_name
);
687 inform("BUG in %s, scan() botch (%d)", invo_name
, incerr
);
693 * Run the external program hook on the message.
696 (void)snprintf(b
, sizeof (b
), "%s/%d", maildir_copy
, msgnum
+ 1);
697 (void)ext_hook("add-hook", b
, NULL
);
700 fputs (charstring_buffer (scanl
), aud
);
707 charstring_free (scanl
);
709 /* If we get here there was some sort of error from scan(),
710 * so stop processing anything more from the spool.
716 /* Mail from Maildir. */
721 hghnum
= msgnum
= mp
->hghmsg
;
722 for (i
= 0; i
< num_maildir_entries
; i
++) {
723 charstring_t scanl
= NULL
;
727 sp
= Maildir
[i
].filename
;
728 cp
= mh_xstrdup(m_name (msgnum
));
730 if (!trnflag
|| link(sp
, cp
) == -1) {
731 static char buf
[65536];
734 if ((sf
= fopen (sp
, "r")) == NULL
)
735 adios (sp
, "unable to read for copy");
736 if ((pf
= fopen (cp
, "w+")) == NULL
)
737 adios (cp
, "unable to write for copy");
738 while ((nrd
= fread(buf
, 1, sizeof(buf
), sf
)) > 0)
739 if (fwrite(buf
, 1, nrd
, pf
) != nrd
)
741 if (ferror(sf
) || fflush(pf
) || ferror(pf
)) {
743 fclose(pf
); fclose(sf
); (void) m_unlink(cp
);
745 adios(cp
, "copy error %s -> %s", sp
, cp
);
750 if (pf
== NULL
&& (pf
= fopen (cp
, "r")) == NULL
)
751 adios (cp
, "not available");
752 chmod (cp
, m_gmprot ());
754 fseek (pf
, 0L, SEEK_SET
);
755 switch (incerr
= scan (pf
, msgnum
, 0, nfs
, width
,
756 msgnum
== mp
->hghmsg
+ 1 && chgflag
,
757 1, NULL
, 0, noisy
, &scanl
)) {
759 printf ("%*d empty\n", DMAXFOLDER
, msgnum
);
765 /* advise (cp, "unable to read"); already advised */
776 * Run the external program hook on the message.
779 (void)snprintf(b
, sizeof (b
), "%s/%d", maildir_copy
, msgnum
+ 1);
780 (void)ext_hook("add-hook", b
, NULL
);
783 fputs (charstring_buffer (scanl
), aud
);
788 charstring_free (scanl
);
790 if (ferror(pf
) || fclose (pf
)) {
792 (void) m_unlink (cp
);
794 adios (cp
, "write error on");
799 if (trnflag
&& m_unlink (sp
) == NOTOK
)
800 adios (sp
, "couldn't unlink");
801 free (sp
); /* Free Maildir[i]->filename */
805 free (Maildir
); /* From now on Maildir is just a flag - don't dref! */
810 if (incerr
< 0) { /* error */
812 GETGROUPPRIVS(); /* Be sure we can unlock mail file */
813 (void) lkfclosespool (in
, newmail
); in
= NULL
;
814 DROPGROUPPRIVS(); /* And then return us to normal privileges */
816 fclose (in
); in
= NULL
;
827 if (inc_type
== INC_FILE
&& Maildir
== NULL
) {
828 /* Mail from a spool file; truncate it. */
831 if (stat (newmail
, &st
) != NOTOK
&& s1
.st_mtime
!= st
.st_mtime
)
832 inform("new messages have arrived!\007");
835 if ((newfd
= creat (newmail
, 0600)) != NOTOK
)
838 admonish (newmail
, "error zero'ing");
842 printf ("%s not zero'd\n", newmail
);
846 if (msgnum
== hghnum
) {
847 inform("no messages incorporated, continuing...");
850 * Lock the sequence file now, and loop to set the right flags
851 * in the folder structure
857 context_replace (pfolder
, folder
); /* update current folder */
859 if ((mp2
= folder_read(folder
, 1)) == NULL
) {
860 inform("Unable to reread folder %s, continuing...", folder
);
865 * Shouldn't happen, but just in case ...
868 if (msgnum
>= mp2
->hghoff
869 && !(mp2
= folder_realloc (mp2
, mp2
->lowoff
, msgnum
+ 1))) {
870 inform("unable to reallocate folder storage");
875 mp2
->curmsg
= hghnum
+ 1;
876 mp2
->hghmsg
= msgnum
;
878 if (mp2
->lowmsg
== 0)
880 if (chgflag
) /* sigh... */
881 seq_setcur (mp2
, mp2
->curmsg
);
883 for (i
= hghnum
+ 1; i
<= msgnum
; i
++) {
884 clear_msg_flags (mp2
, i
);
888 mp2
->msgflags
|= SEQMOD
;
889 seq_setunseen(mp2
, 0); /* Set the Unseen-Sequence */
890 seq_save(mp2
); /* Save the sequence file */
895 if (inc_type
== INC_FILE
&& Maildir
== NULL
) {
896 /* Mail from a spool file; unlock it. */
899 GETGROUPPRIVS(); /* Be sure we can unlock mail file */
900 (void) lkfclosespool (in
, newmail
); in
= NULL
;
901 DROPGROUPPRIVS(); /* And then return us to normal privileges */
903 fclose (in
); in
= NULL
;
907 context_save (); /* save the context file */
914 inc_done (int status
)
920 lkfclosespool(in
, newmail
);
927 pop_action(void *closure
, char *s
)
933 n
= fprintf(pc
->mailout
, "%s\n", s
);
936 pc
->written
+= n
; /* Count linefeed too. */