]>
diplodocus.org Git - nmh/blob - uip/inc.c
7ebb0581c1f206befb1764d3794af17f8313b395
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/fmt_new.h"
35 #include "sbr/dtime.h"
37 #include "sbr/m_name.h"
38 #include "sbr/m_gmprot.h"
39 #include "sbr/getarguments.h"
40 #include "sbr/concat.h"
41 #include "sbr/seq_setunseen.h"
42 #include "sbr/seq_setcur.h"
43 #include "sbr/seq_save.h"
44 #include "sbr/smatch.h"
45 #include "sbr/getfolder.h"
46 #include "sbr/ext_hook.h"
47 #include "sbr/folder_read.h"
48 #include "sbr/folder_realloc.h"
49 #include "sbr/folder_free.h"
50 #include "sbr/context_save.h"
51 #include "sbr/context_replace.h"
52 #include "sbr/context_find.h"
53 #include "sbr/ambigsw.h"
55 #include "sbr/print_version.h"
56 #include "sbr/print_help.h"
57 #include "sbr/error.h"
60 #include "h/dropsbr.h"
62 #include "h/fmt_scan.h"
63 #include "h/signals.h"
67 #include "sbr/lock_file.h"
68 #include "sbr/m_maildir.h"
69 #include "sbr/m_mktemp.h"
70 #include "sbr/maildir_read_and_sort.h"
73 # define TLSminc(a) (a)
78 #define INC_SWITCHES \
79 X("audit audit-file", 0, AUDSW) \
80 X("noaudit", 0, NAUDSW) \
81 X("changecur", 0, CHGSW) \
82 X("nochangecur", 0, NCHGSW) \
83 X("file name", 0, FILESW) \
84 X("form formatfile", 0, FORMSW) \
85 X("format string", 5, FMTSW) \
86 X("host hostname", 0, HOSTSW) \
87 X("user username", 0, USERSW) \
88 X("port name/number", 0, PORTSW) \
89 X("silent", 0, SILSW) \
90 X("nosilent", 0, NSILSW) \
91 X("truncate", 0, TRNCSW) \
92 X("notruncate", 0, NTRNCSW) \
93 X("width columns", 0, WIDTHSW) \
94 X("version", 0, VERSIONSW) \
95 X("help", 0, HELPSW) \
96 X("snoop", 0, SNOOPSW) \
97 X("sasl", 0, SASLSW) \
98 X("nosasl", 0, NOSASLSW) \
99 X("saslmech", 0, SASLMECHSW) \
100 X("tls", TLSminc(-3), TLSSW) \
101 X("initialtls", TLSminc(-10), INITTLSSW) \
102 X("notls", TLSminc(-5), NOTLSSW) \
103 X("certverify", TLSminc(-10), CERTVERSW) \
104 X("nocertverify", TLSminc(-12), NOCERTVERSW) \
105 X("authservice", 0, AUTHSERVICESW) \
106 X("proxy command", 0, PROXYSW) \
108 #define X(sw, minchars, id) id,
109 DEFINE_SWITCH_ENUM(INC
);
112 #define X(sw, minchars, id) { sw, minchars, id },
113 DEFINE_SWITCH_ARRAY(INC
, switches
);
117 * flags for the mail source
122 static struct Maildir_entry
*Maildir
;
123 static int num_maildir_entries
;
131 extern char response
[];
133 /* This is an attempt to simplify things by putting all the
134 * privilege ops into macros.
135 * *GROUPPRIVS() is related to handling the setgid MAIL property,
136 * and only applies if MAILGROUP is defined.
137 * Basically, SAVEGROUPPRIVS() is called right at the top of main()
138 * to initialise things, and then DROPGROUPPRIVS() and GETGROUPPRIVS()
139 * do the obvious thing.
141 * There's probably a better implementation if we're allowed to use
142 * BSD-style setreuid() rather than using POSIX saved-ids.
143 * Anyway, if you're euid root it's a bit pointless to drop the group
146 * I'm pretty happy that the security is good provided we aren't setuid root.
147 * The only things we trust with group=mail privilege are lkfopen()
152 * For setting and returning to "mail" gid
155 static gid_t return_gid
;
156 #define TRYDROPGROUPPRIVS() DROPGROUPPRIVS()
157 #define DROPGROUPPRIVS() \
158 if (setegid(getgid()) != 0) { \
159 adios ("setegid", "unable to restore group to %ld", (long) getgid()); \
161 #define GETGROUPPRIVS() \
162 if (setegid(return_gid) != 0) { \
163 adios ("setegid", "unable to set group to %ld", (long) return_gid); \
165 #define SAVEGROUPPRIVS() return_gid = getegid()
167 /* define *GROUPPRIVS() as null; this avoids having lots of "#ifdef MAILGROUP"s */
168 #define TRYDROPGROUPPRIVS()
169 #define DROPGROUPPRIVS()
170 #define GETGROUPPRIVS()
171 #define SAVEGROUPPRIVS()
172 #endif /* not MAILGROUP */
174 /* these variables have to be globals so that done() can correctly clean up the lockfile */
176 static char *newmail
;
182 static void inc_done(int) NORETURN
;
183 static int pop_action(void *closure
, char *);
186 main (int argc
, char **argv
)
193 int hghnum
= 0, msgnum
= 0;
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 */
208 charstring_t scanl
= NULL
;
211 char *MAILHOST_env_variable
;
214 /* absolutely the first thing we do is save our privileges,
215 * and drop them if we can.
220 if (nmh_init(argv
[0], true, true)) { return 1; }
223 arguments
= getarguments (invo_name
, argc
, argv
, 1);
228 * use MAILHOST environment variable if present,
230 * If that fails, use the default (if any)
231 * provided by mts.conf in mts_init()
233 if ((MAILHOST_env_variable
= getenv("MAILHOST")) != NULL
)
234 pophost
= MAILHOST_env_variable
;
236 * If there is a valid "pophost" entry in mts.conf,
237 * then use it as the default host.
239 if (pophost
&& *pophost
)
242 sasl
= noverify
= false;
243 chgflag
= noisy
= true;
244 while ((cp
= *argp
++)) {
246 switch (smatch (++cp
, switches
)) {
248 ambigsw (cp
, switches
);
251 die("-%s unknown", cp
);
254 snprintf (buf
, sizeof(buf
), "%s [+folder] [switches]", invo_name
);
255 print_help (buf
, switches
, 1);
258 print_version(invo_name
);
262 if (!(cp
= *argp
++) || *cp
== '-')
263 die("missing argument to %s", argp
[-2]);
264 audfile
= mh_xstrdup(m_maildir(cp
));
278 * The flag `trnflag' has the value:
280 * 2 if -truncate is given
281 * 1 by default (truncating is default)
282 * 0 if -notruncate is given
292 if (!(cp
= *argp
++) || *cp
== '-')
293 die("missing argument to %s", argp
[-2]);
294 from
= path (cp
, TFILE
);
297 * If the truncate file is in default state,
298 * change to not truncate.
312 if (!(form
= *argp
++) || *form
== '-')
313 die("missing argument to %s", argp
[-2]);
317 if (!(format
= *argp
++) || *format
== '-')
318 die("missing argument to %s", argp
[-2]);
323 if (!(cp
= *argp
++) || *cp
== '-')
324 die("missing argument to %s", argp
[-2]);
329 if (!(host
= *argp
++) || *host
== '-')
330 die("missing argument to %s", argp
[-2]);
334 if (!(port
= *argp
++) || *port
== '-')
335 die("missing argument to %s", argp
[-2]);
339 if (!(user
= *argp
++) || *user
== '-')
340 die("missing argument to %s", argp
[-2]);
355 if (!(saslmech
= *argp
++) || *saslmech
== '-')
356 die("missing argument to %s", argp
[-2]);
381 if (!(auth_svc
= *argp
++) || *auth_svc
== '-')
382 die("missing argument to %s", argp
[-2]);
384 die("not built with OAuth support");
389 if (!(proxy
= *argp
++) || *proxy
== '-')
390 die("missing argument to %s", argp
[-2]);
394 if (*cp
== '+' || *cp
== '@') {
396 die("only one folder at a time!");
397 folder
= pluspath (cp
);
399 die("usage: %s [+folder] [switches]", invo_name
);
403 /* NOTE: above this point you should use TRYDROPGROUPPRIVS(),
404 * not DROPGROUPPRIVS().
409 /* guarantee dropping group privileges; we might not have done so earlier */
412 /* Source of mail; -from overrides any -host. */
413 inc_type
= host
&& !from
? INC_POP
: INC_FILE
;
415 if (inc_type
== INC_POP
) {
416 /* Mail from a POP server. */
419 if (auth_svc
== NULL
) {
420 if (saslmech
&& ! strcasecmp(saslmech
, "xoauth2")) {
421 die("must specify -authservice with -saslmech xoauth2");
425 die("must specify -user with -saslmech xoauth2");
430 tlsflag
= P_STARTTLS
;
435 tlsflag
|= P_NOVERIFY
;
438 * initialize POP connection
440 if (pop_init (host
, port
, user
, proxy
, snoop
, sasl
, saslmech
,
441 tlsflag
, auth_svc
) == NOTOK
)
444 /* Check if there are any messages */
445 if (pop_stat (&nmsgs
, &nbytes
) == NOTOK
)
450 die("no mail to incorporate");
453 } else if (inc_type
== INC_FILE
) {
454 /* Mail from a spool file, or Maildir. */
457 else if ((newmail
= getenv ("MAILDROP")) && *newmail
)
458 newmail
= m_mailpath (newmail
);
459 else if ((newmail
= context_find ("maildrop")) && *newmail
)
460 newmail
= m_mailpath (newmail
);
462 newmail
= concat (MAILDIR
, "/", MAILFIL
, NULL
);
464 if (stat (newmail
, &s1
) == NOTOK
|| s1
.st_size
== 0)
465 die("no mail to incorporate");
466 if (s1
.st_mode
& S_IFDIR
) {
467 maildir_read_and_sort(newmail
, &Maildir
, &num_maildir_entries
);
470 cp
= mh_xstrdup(newmail
);
474 if (!context_find ("path"))
475 free (path ("./", TFOLDER
));
477 folder
= getfolder (0);
478 maildir
= m_maildir (folder
);
479 maildir_copy
= mh_xstrdup(maildir
);
481 if (!folder_exists(maildir
)) {
482 /* If the folder doesn't exist, and we're given the -silent flag,
486 create_folder(maildir
, 0, done
);
491 if (chdir (maildir
) == NOTOK
)
492 adios (maildir
, "unable to change directory to");
494 /* read folder and create message structure */
495 if (!(mp
= folder_read (folder
, 0)))
496 die("unable to read folder %s", folder
);
498 if (inc_type
== INC_FILE
&& Maildir
== NULL
) {
499 /* Mail from a spool file. */
501 if (access (newmail
, W_OK
) != NOTOK
) {
504 SIGNAL (SIGHUP
, SIG_IGN
);
505 SIGNAL (SIGINT
, SIG_IGN
);
506 SIGNAL (SIGQUIT
, SIG_IGN
);
507 SIGNAL (SIGTERM
, SIG_IGN
);
510 GETGROUPPRIVS(); /* Reset gid to lock mail file */
511 in
= lkfopenspool (newmail
, "r");
514 die("unable to lock and fopen %s", newmail
);
515 fstat (fileno(in
), &s1
);
518 if ((in
= fopen (newmail
, "r")) == NULL
)
519 adios (newmail
, "unable to read");
523 /* This shouldn't be necessary but it can't hurt. */
528 if ((i
= stat (audfile
, &st
)) == NOTOK
)
529 inform("Creating Receive-Audit: %s", audfile
);
530 if ((aud
= fopen (audfile
, "a")) == NULL
)
531 adios (audfile
, "unable to append to");
533 chmod (audfile
, m_gmprot ());
536 fprintf (aud
, "<<inc>> %s -ms %s\n", dtimenow(0), from
);
539 fprintf (aud
, "<<inc>> %s -host %s -user %s\n", dtimenow(0),
542 fprintf (aud
, "<<inc>> %s\n", dtimenow (0));
546 /* Get new format string */
547 nfs
= new_fs (form
, format
, FORMAT
);
550 printf ("Incorporating new mail into %s...\n\n", folder
);
556 * Get the mail from a POP server
558 if (inc_type
== INC_POP
) {
559 /* Mail from a POP server. */
563 hghnum
= msgnum
= mp
->hghmsg
;
564 for (i
= 1; i
<= nmsgs
; i
++) {
567 cp
= mh_xstrdup(m_name (msgnum
));
568 if ((pf
= fopen (cp
, "w+")) == NULL
)
569 adios (cp
, "unable to write");
570 chmod (cp
, m_gmprot ());
574 if (pop_retr(i
, pop_action
, &pc
) == NOTOK
)
578 adios (cp
, "write error on");
579 fseek (pf
, 0L, SEEK_SET
);
580 switch (incerr
= scan (pf
, msgnum
, 0, nfs
, width
,
581 msgnum
== mp
->hghmsg
+ 1 && chgflag
,
582 1, NULL
, pc
.written
, noisy
, &scanl
)) {
584 printf ("%*d empty\n", DMAXFOLDER
, msgnum
);
590 /* advise (cp, "unable to read"); already advised */
600 fputs (charstring_buffer (scanl
), aud
);
607 charstring_clear (scanl
);
609 if (ferror(pf
) || fclose (pf
)) {
611 (void) m_unlink (cp
);
614 adios (cp
, "write error on");
618 if (trnflag
&& pop_dele (i
) == NOTOK
)
624 charstring_free (scanl
);
627 if (pop_quit () == NOTOK
)
630 } else if (inc_type
== INC_FILE
&& Maildir
== NULL
) {
631 /* Mail from a spool file. */
633 scan_detect_mbox_style (in
); /* the MAGIC invocation... */
634 hghnum
= msgnum
= mp
->hghmsg
;
636 /* create scanline for new message */
637 switch (incerr
= scan (in
, msgnum
+ 1, msgnum
+ 1, nfs
, width
,
638 msgnum
== hghnum
&& chgflag
, 1, NULL
, 0L, noisy
,
646 fputs ("inc aborted!\n", aud
);
647 inform("aborted!"); /* doesn't clean up locks! */
651 inform("BUG in %s, number out of range", invo_name
);
655 inform("BUG in %s, scan() botch (%d)", invo_name
, incerr
);
660 * Run the external program hook on the message.
663 (void)snprintf(b
, sizeof (b
), "%s/%d", maildir_copy
, msgnum
+ 1);
664 (void)ext_hook("add-hook", b
, NULL
);
667 fputs (charstring_buffer (scanl
), aud
);
671 charstring_clear (scanl
);
676 /* If we get here there was some sort of error from scan(),
677 * so stop processing anything more from the spool.
681 charstring_free (scanl
);
685 /* Mail from Maildir. */
690 hghnum
= msgnum
= mp
->hghmsg
;
691 for (i
= 0; i
< num_maildir_entries
; i
++) {
694 sp
= Maildir
[i
].filename
;
695 cp
= mh_xstrdup(m_name (msgnum
));
697 if (!trnflag
|| link(sp
, cp
) == -1) {
698 static char buf
[65536];
701 if ((sf
= fopen (sp
, "r")) == NULL
)
702 adios (sp
, "unable to read for copy");
703 if ((pf
= fopen (cp
, "w+")) == NULL
)
704 adios (cp
, "unable to write for copy");
705 while ((nrd
= fread(buf
, 1, sizeof(buf
), sf
)) > 0)
706 if (fwrite(buf
, 1, nrd
, pf
) != nrd
)
708 if (ferror(sf
) || fflush(pf
) || ferror(pf
)) {
710 fclose(pf
); fclose(sf
); (void) m_unlink(cp
);
712 adios(cp
, "copy error %s -> %s", sp
, cp
);
717 if (pf
== NULL
&& (pf
= fopen (cp
, "r")) == NULL
)
718 adios (cp
, "not available");
719 chmod (cp
, m_gmprot ());
721 fseek (pf
, 0L, SEEK_SET
);
722 switch (incerr
= scan (pf
, msgnum
, 0, nfs
, width
,
723 msgnum
== mp
->hghmsg
+ 1 && chgflag
,
724 1, NULL
, 0, noisy
, &scanl
)) {
726 printf ("%*d empty\n", DMAXFOLDER
, msgnum
);
732 /* advise (cp, "unable to read"); already advised */
742 * Run the external program hook on the message.
745 (void)snprintf(b
, sizeof (b
), "%s/%d", maildir_copy
, msgnum
+ 1);
746 (void)ext_hook("add-hook", b
, NULL
);
749 fputs (charstring_buffer (scanl
), aud
);
754 charstring_clear (scanl
);
756 if (ferror(pf
) || fclose (pf
)) {
758 (void) m_unlink (cp
);
760 adios (cp
, "write error on");
765 if (trnflag
&& m_unlink (sp
) == NOTOK
)
766 adios (sp
, "couldn't unlink");
767 free (sp
); /* Free Maildir[i]->filename */
771 free (Maildir
); /* From now on Maildir is just a flag - don't dref! */
772 charstring_free (scanl
);
778 if (incerr
< 0) { /* error */
780 GETGROUPPRIVS(); /* Be sure we can unlock mail file */
781 (void) lkfclosespool (in
, newmail
); in
= NULL
;
782 DROPGROUPPRIVS(); /* And then return us to normal privileges */
784 fclose (in
); in
= NULL
;
795 if (inc_type
== INC_FILE
&& Maildir
== NULL
) {
796 /* Mail from a spool file; truncate it. */
799 if (stat (newmail
, &st
) != NOTOK
&& s1
.st_mtime
!= st
.st_mtime
)
800 inform("new messages have arrived!\007");
803 if ((newfd
= creat (newmail
, 0600)) != NOTOK
)
806 admonish (newmail
, "error zero'ing");
810 printf ("%s not zero'd\n", newmail
);
814 if (msgnum
== hghnum
) {
815 inform("no messages incorporated, continuing...");
818 * Lock the sequence file now, and loop to set the right flags
819 * in the folder structure
825 context_replace (pfolder
, folder
); /* update current folder */
827 if ((mp2
= folder_read(folder
, 1)) == NULL
) {
828 inform("Unable to reread folder %s, continuing...", folder
);
833 * Shouldn't happen, but just in case ...
836 if (msgnum
>= mp2
->hghoff
837 && !(mp2
= folder_realloc (mp2
, mp2
->lowoff
, msgnum
+ 1))) {
838 inform("unable to reallocate folder storage");
843 mp2
->curmsg
= hghnum
+ 1;
844 mp2
->hghmsg
= msgnum
;
846 if (mp2
->lowmsg
== 0)
848 if (chgflag
) /* sigh... */
849 seq_setcur (mp2
, mp2
->curmsg
);
851 for (i
= hghnum
+ 1; i
<= msgnum
; i
++) {
852 clear_msg_flags (mp2
, i
);
856 mp2
->msgflags
|= SEQMOD
;
857 seq_setunseen(mp2
, 0); /* Set the Unseen-Sequence */
858 seq_save(mp2
); /* Save the sequence file */
863 if (inc_type
== INC_FILE
&& Maildir
== NULL
) {
864 /* Mail from a spool file; unlock it. */
867 GETGROUPPRIVS(); /* Be sure we can unlock mail file */
868 (void) lkfclosespool (in
, newmail
); in
= NULL
;
869 DROPGROUPPRIVS(); /* And then return us to normal privileges */
871 fclose (in
); in
= NULL
;
875 context_save (); /* save the context file */
882 inc_done (int status
)
888 lkfclosespool(in
, newmail
);
895 pop_action(void *closure
, char *s
)
901 n
= fprintf(pc
->mailout
, "%s\n", s
);
904 pc
->written
+= n
; /* Count linefeed too. */