3 * inc.c -- incorporate messages from a maildrop into a folder
5 * This code is Copyright (c) 2002, by the authors of nmh. See the
6 * COPYRIGHT file in the root directory of the nmh distribution for
7 * complete copyright information.
11 /* Revised: Sat Apr 14 17:08:17 PDT 1990 (marvit@hplabs)
12 * Added hpux hacks to set and reset gid to be "mail" as needed. The reset
13 * is necessary so inc'ed mail is the group of the inc'er, rather than
14 * "mail". We setgid to egid only when [un]locking the mail file. This
15 * is also a major security precaution which will not be explained here.
17 * Fri Feb 7 16:04:57 PST 1992 John Romine <bug-mh@ics.uci.edu>
18 * NB: I'm not 100% sure that this setgid stuff is secure even now.
20 * See the *GROUPPRIVS() macros later. I'm reasonably happy with the setgid
21 * attribute. Running setuid root is probably not a terribly good idea, though.
22 * -- Peter Maydell <pmaydell@chiark.greenend.org.uk>, 04/1998
24 * Peter Maydell's patch slightly modified for nmh 0.28-pre2.
25 * Ruud de Rooij <ruud@debian.org> Wed, 22 Jul 1998 13:24:22 +0200
34 # include <h/dropsbr.h>
35 # include <h/popsbr.h>
38 #include <h/fmt_scan.h>
39 #include <h/scansbr.h>
40 #include <h/signals.h>
47 # define POPminc(a) (a)
53 # define SASLminc(a) (a)
55 # define SASLminc(a) 0
58 static struct swit switches[] = {
60 { "audit audit-file", 0 },
70 { "form formatfile", 0 },
72 { "format string", 5 },
74 { "host hostname", POPminc (-4) },
76 { "user username", POPminc (-4) },
78 { "pack file", POPminc (-4) },
80 { "nopack", POPminc (-6) },
82 { "port name/number", POPminc (-4) },
92 { "width columns", 0 },
100 { "sasl", SASLminc(-4) },
101 #define SASLMECHSW 21
102 { "saslmech", SASLminc(-8) },
104 { "proxy command", POPminc(-5) },
109 * flags for the mail source
115 static struct Maildir_entry {
119 static int num_maildir_entries = 0;
120 static int snoop = 0;
123 extern char response[];
128 static int mbx_style = MMDF_FORMAT;
129 static int pd = NOTOK;
135 static char *packfile = NULL;
136 static FILE *pf = NULL;
138 /* This is an attempt to simplify things by putting all the
139 * privilege ops into macros.
140 * *GROUPPRIVS() is related to handling the setgid MAIL property,
141 * and only applies if MAILGROUP is defined.
142 * *USERPRIVS() is related to handling the setuid root property,
143 * and only applies if POP is defined [why does POP => setuid root?]
144 * Basically, SAVEGROUPPRIVS() is called right at the top of main()
145 * to initialise things, and then DROPGROUPPRIVS() and GETGROUPPRIVS()
146 * do the obvious thing. TRYDROPGROUPPRIVS() has to be safe to call
147 * before DROPUSERPRIVS() is called [this is needed because setgid()
148 * sets both effective and real uids if euid is root.]
150 * There's probably a better implementation if we're allowed to use
151 * BSD-style setreuid() rather than using POSIX saved-ids.
152 * Anyway, if you're euid root it's a bit pointless to drop the group
155 * I'm pretty happy that the security is good provided we aren't setuid root.
156 * The only things we trust with group=mail privilege are lkfopen()
161 * For setting and returning to "mail" gid
164 static int return_gid;
166 /* easy case; we're not setuid root, so can drop group privs
169 #define TRYDROPGROUPPRIVS() DROPGROUPPRIVS()
170 #else /* POP ie we are setuid root */
171 #define TRYDROPGROUPPRIVS() \
172 if (geteuid() != 0) DROPGROUPPRIVS()
174 #define DROPGROUPPRIVS() setgid(getgid())
175 #define GETGROUPPRIVS() setgid(return_gid)
176 #define SAVEGROUPPRIVS() return_gid = getegid()
178 /* define *GROUPPRIVS() as null; this avoids having lots of "#ifdef MAILGROUP"s */
179 #define TRYDROPGROUPPRIVS()
180 #define DROPGROUPPRIVS()
181 #define GETGROUPPRIVS()
182 #define SAVEGROUPPRIVS()
183 #endif /* not MAILGROUP */
185 /* these variables have to be globals so that done() can correctly clean up the lockfile */
186 static int locked = 0;
187 static char *newmail;
193 char *map_name(char *);
195 static void inc_done(int) NORETURN;
197 static int pop_action(char *);
198 static int pop_pack(char *);
199 static int map_count(void);
203 maildir_srt(const void *va, const void *vb)
205 const struct Maildir_entry *a = va, *b = vb;
206 if (a->mtime > b->mtime)
208 else if (a->mtime < b->mtime)
215 main (int argc, char **argv)
217 int chgflag = 1, trnflag = 1;
218 int noisy = 1, width = 0;
219 int hghnum = 0, msgnum = 0;
221 int incerr = 0; /* <0 if inc hits an error which means it should not truncate mailspool */
222 char *cp, *maildir = NULL, *folder = NULL;
223 char *format = NULL, *form = NULL;
224 char *host = NULL, *port = NULL, *user = NULL, *proxy = NULL;
225 char *audfile = NULL, *from = NULL, *saslmech = NULL;
226 char buf[BUFSIZ], **argp, *nfs, **arguments;
227 struct msgs *mp = NULL;
230 char b[MAXPATHLEN + 1];
231 char *maildir_copy = NULL; /* copy of mail directory because the static gets overwritten */
236 char *MAILHOST_env_variable;
245 /* absolutely the first thing we do is save our privileges,
246 * and drop them if we can.
252 setlocale(LC_ALL, "");
254 invo_name = r1bindex (argv[0], '/');
256 /* read user profile/context */
259 mts_init (invo_name);
260 arguments = getarguments (invo_name, argc, argv, 1);
266 * use MAILHOST environment variable if present,
268 * If that fails, use the default (if any)
269 * provided by mts.conf in mts_init()
271 if ((MAILHOST_env_variable = getenv("MAILHOST")) != NULL)
272 pophost = MAILHOST_env_variable;
274 * If there is a valid "pophost" entry in mts.conf,
275 * then use it as the default host.
277 if (pophost && *pophost)
280 if ((cp = getenv ("MHPOPDEBUG")) && *cp)
284 while ((cp = *argp++)) {
286 switch (smatch (++cp, switches)) {
288 ambigsw (cp, switches);
291 adios (NULL, "-%s unknown", cp);
294 snprintf (buf, sizeof(buf), "%s [+folder] [switches]", invo_name);
295 print_help (buf, switches, 1);
298 print_version(invo_name);
302 if (!(cp = *argp++) || *cp == '-')
303 adios (NULL, "missing argument to %s", argp[-2]);
304 audfile = getcpy (m_maildir (cp));
318 * The flag `trnflag' has the value:
320 * 2 if -truncate is given
321 * 1 by default (truncating is default)
322 * 0 if -notruncate is given
332 if (!(cp = *argp++) || *cp == '-')
333 adios (NULL, "missing argument to %s", argp[-2]);
334 from = path (cp, TFILE);
337 * If the truncate file is in default state,
338 * change to not truncate.
352 if (!(form = *argp++) || *form == '-')
353 adios (NULL, "missing argument to %s", argp[-2]);
357 if (!(format = *argp++) || *format == '-')
358 adios (NULL, "missing argument to %s", argp[-2]);
363 if (!(cp = *argp++) || *cp == '-')
364 adios (NULL, "missing argument to %s", argp[-2]);
369 if (!(host = *argp++) || *host == '-')
370 adios (NULL, "missing argument to %s", argp[-2]);
374 if (!(host = *argp++) || *port == '-')
375 adios (NULL, "missing argument to %s", argp[-2]);
379 if (!(user = *argp++) || *user == '-')
380 adios (NULL, "missing argument to %s", argp[-2]);
385 if (!(cp = *argp++) || *cp == '-')
386 adios (NULL, "missing argument to %s", argp[-2]);
388 if (!(packfile = *argp++) || *packfile == '-')
389 adios (NULL, "missing argument to %s", argp[-2]);
407 if (!(saslmech = *argp++) || *saslmech == '-')
408 adios (NULL, "missing argument to %s", argp[-2]);
411 if (!(proxy = *argp++) || *proxy == '-')
412 adios (NULL, "missing argument to %s", argp[-2]);
416 if (*cp == '+' || *cp == '@') {
418 adios (NULL, "only one folder at a time!");
420 folder = pluspath (cp);
422 adios (NULL, "usage: %s [+folder] [switches]", invo_name);
426 /* NOTE: above this point you should use TRYDROPGROUPPRIVS(),
427 * not DROPGROUPPRIVS().
434 /* guarantee dropping group priveleges; we might not have done so earlier */
438 * Where are we getting the new mail?
451 * Are we getting the mail from
454 if (inc_type == INC_POP) {
456 user = getusername ();
458 pass = getusername ();
460 ruserpass (host, &user, &pass);
463 * initialize POP connection
465 if (pop_init (host, port, user, pass, proxy, snoop, sasl,
467 adios (NULL, "%s", response);
469 /* Check if there are any messages */
470 if (pop_stat (&nmsgs, &nbytes) == NOTOK)
471 adios (NULL, "%s", response);
475 adios (NULL, "no mail to incorporate");
481 * We will get the mail from a file
482 * (typically the standard maildrop)
485 if (inc_type == INC_FILE) {
488 else if ((newmail = getenv ("MAILDROP")) && *newmail)
489 newmail = m_mailpath (newmail);
490 else if ((newmail = context_find ("maildrop")) && *newmail)
491 newmail = m_mailpath (newmail);
493 newmail = concat (MAILDIR, "/", MAILFIL, NULL);
495 if (stat (newmail, &s1) == NOTOK || s1.st_size == 0)
496 adios (NULL, "no mail to incorporate");
497 if (s1.st_mode & S_IFDIR) {
503 cp = concat (newmail, "/new", NULL);
504 if ((md = opendir(cp)) == NULL)
505 adios (NULL, "unable to open %s", cp);
506 while ((de = readdir (md)) != NULL) {
507 if (de->d_name[0] == '.')
509 if (i >= num_maildir_entries) {
510 if ((Maildir = realloc(Maildir, sizeof(*Maildir) * (2*i+16))) == NULL)
511 adios(NULL, "not enough memory for %d messages", 2*i+16);
512 num_maildir_entries = 2*i+16;
514 Maildir[i].filename = concat (cp, "/", de->d_name, NULL);
515 if (stat(Maildir[i].filename, &ms) != 0)
516 adios (Maildir[i].filename, "couldn't get delivery time");
517 Maildir[i].mtime = ms.st_mtime;
522 cp = concat (newmail, "/cur", NULL);
523 if ((md = opendir(cp)) == NULL)
524 adios (NULL, "unable to open %s", cp);
525 while ((de = readdir (md)) != NULL) {
526 if (de->d_name[0] == '.')
528 if (i >= num_maildir_entries) {
529 if ((Maildir = realloc(Maildir, sizeof(*Maildir) * (2*i+16))) == NULL)
530 adios(NULL, "not enough memory for %d messages", 2*i+16);
531 num_maildir_entries = 2*i+16;
533 Maildir[i].filename = concat (cp, "/", de->d_name, NULL);
534 if (stat(Maildir[i].filename, &ms) != 0)
535 adios (Maildir[i].filename, "couldn't get delivery time");
536 Maildir[i].mtime = ms.st_mtime;
542 adios (NULL, "no mail to incorporate");
543 num_maildir_entries = i;
544 qsort (Maildir, num_maildir_entries, sizeof(*Maildir), maildir_srt);
547 if ((cp = strdup(newmail)) == (char *)0)
548 adios (NULL, "error allocating memory to copy newmail");
554 /* skip the folder setup */
555 if ((inc_type == INC_POP) && packfile)
559 if (!context_find ("path"))
560 free (path ("./", TFOLDER));
562 folder = getfolder (0);
563 maildir = m_maildir (folder);
565 if ((maildir_copy = strdup(maildir)) == (char *)0)
566 adios (maildir, "error allocating memory to copy maildir");
568 if (!folder_exists(maildir)) {
569 /* If the folder doesn't exist, and we're given the -silent flag,
573 create_folder(maildir, 0, done);
578 if (chdir (maildir) == NOTOK)
579 adios (maildir, "unable to change directory to");
581 /* read folder and create message structure */
582 if (!(mp = folder_read (folder)))
583 adios (NULL, "unable to read folder %s", folder);
589 if (inc_type == INC_FILE && Maildir == NULL) {
590 if (access (newmail, W_OK) != NOTOK) {
593 SIGNAL (SIGHUP, SIG_IGN);
594 SIGNAL (SIGINT, SIG_IGN);
595 SIGNAL (SIGQUIT, SIG_IGN);
596 SIGNAL (SIGTERM, SIG_IGN);
599 GETGROUPPRIVS(); /* Reset gid to lock mail file */
600 in = lkfopen (newmail, "r");
603 adios (NULL, "unable to lock and fopen %s", newmail);
604 fstat (fileno(in), &s1);
607 if ((in = fopen (newmail, "r")) == NULL)
608 adios (newmail, "unable to read");
612 /* This shouldn't be necessary but it can't hurt. */
617 if ((i = stat (audfile, &st)) == NOTOK)
618 advise (NULL, "Creating Receive-Audit: %s", audfile);
619 if ((aud = fopen (audfile, "a")) == NULL)
620 adios (audfile, "unable to append to");
622 chmod (audfile, m_gmprot ());
625 fprintf (aud, from ? "<<inc>> %s -ms %s\n"
626 : host ? "<<inc>> %s -host %s -user %s\n"
628 dtimenow (0), from ? from : host, user);
630 fprintf (aud, from ? "<<inc>> %s -ms %s\n" : "<<inc>> %s\n",
636 if (context_find ("mhe")) {
638 cp = concat (maildir, "/++", NULL);
640 if ((mhe = fopen (cp, "a")) == NULL)
641 admonish (cp, "unable to append to");
644 chmod (cp, m_gmprot ());
649 /* Get new format string */
650 nfs = new_fs (form, format, FORMAT);
653 printf ("Incorporating new mail into %s...\n\n", folder);
659 * Get the mail from a POP server
661 if (inc_type == INC_POP) {
664 packfile = path (packfile, TFILE);
665 if (stat (packfile, &st) == NOTOK) {
667 adios (packfile, "error on file");
668 cp = concat ("Create file \"", packfile, "\"? ", NULL);
669 if (noisy && !getanswer (cp))
673 msgnum = map_count ();
674 if ((pd = mbx_open (packfile, mbx_style, getuid(), getgid(), m_gmprot()))
676 adios (packfile, "unable to open");
677 if ((pf = fdopen (pd, "w+")) == NULL)
678 adios (NULL, "unable to fdopen %s", packfile);
680 hghnum = msgnum = mp->hghmsg;
682 * Check if we have enough message space for all the new
683 * messages. If not, then realloc the folder and add enough
684 * space for all new messages plus 10 additional slots.
686 if (mp->hghmsg + nmsgs >= mp->hghoff
687 && !(mp = folder_realloc (mp, mp->lowoff, mp->hghmsg + nmsgs + 10)))
688 adios (NULL, "unable to allocate folder storage");
691 for (i = 1; i <= nmsgs; i++) {
694 fseek (pf, 0L, SEEK_CUR);
697 fwrite (mmdlm1, 1, strlen (mmdlm1), pf);
700 if (pop_retr (i, pop_pack) == NOTOK)
701 adios (NULL, "%s", response);
703 fseek (pf, 0L, SEEK_CUR);
706 adios (packfile, "write error on");
707 fseek (pf, start, SEEK_SET);
709 cp = getcpy (m_name (msgnum));
710 if ((pf = fopen (cp, "w+")) == NULL)
711 adios (cp, "unable to write");
712 chmod (cp, m_gmprot ());
715 if (pop_retr (i, pop_action) == NOTOK)
716 adios (NULL, "%s", response);
719 adios (cp, "write error on");
720 fseek (pf, 0L, SEEK_SET);
722 switch (incerr = scan (pf, msgnum, 0, nfs, width,
723 packfile ? 0 : msgnum == mp->hghmsg + 1 && chgflag,
724 1, NULL, stop - start, noisy)) {
726 printf ("%*d empty\n", DMAXFOLDER, msgnum);
732 /* advise (cp, "unable to read"); already advised */
751 clear_msg_flags (mp, msgnum);
752 set_exists (mp, msgnum);
753 set_unseen (mp, msgnum);
754 mp->msgflags |= SEQMOD;
759 fseek (pf, stop, SEEK_SET);
760 fwrite (mmdlm2, 1, strlen (mmdlm2), pf);
761 if (fflush (pf) || ferror (pf)) {
765 adios (packfile, "write error on");
767 map_write (packfile, pd, 0, 0L, start, stop, pos, size, noisy);
769 if (ferror(pf) || fclose (pf)) {
774 adios (cp, "write error on");
779 if (trnflag && pop_dele (i) == NOTOK)
780 adios (NULL, "%s", response);
783 if (pop_quit () == NOTOK)
784 adios (NULL, "%s", response);
786 mbx_close (packfile, pd);
793 * Get the mail from file (usually mail spool)
795 if (inc_type == INC_FILE && Maildir == NULL) {
796 m_unknown (in); /* the MAGIC invocation... */
797 hghnum = msgnum = mp->hghmsg;
800 * Check if we need to allocate more space for message status.
801 * If so, then add space for an additional 100 messages.
803 if (msgnum >= mp->hghoff
804 && !(mp = folder_realloc (mp, mp->lowoff, mp->hghoff + 100))) {
805 advise (NULL, "unable to allocate folder storage");
811 /* copy file from spool to tmp file */
812 tmpfilenam = m_scratch ("", invo_name);
813 if ((fd = creat (tmpfilenam, m_gmprot ())) == NOTOK)
814 adios (tmpfilenam, "unable to create");
815 chmod (tmpfilenam, m_gmprot ());
816 if (!(in2 = fdopen (fd, "r+")))
817 adios (tmpfilenam, "unable to access");
820 /* link message into folder */
821 newmsg = folder_addmsg(mp, tmpfilenam);
823 /* create scanline for new message */
824 switch (incerr = scan (in, msgnum + 1, msgnum + 1, nfs, width,
825 msgnum == hghnum && chgflag, 1, NULL, 0L, noisy)) {
832 fputs ("inc aborted!\n", aud);
833 advise (NULL, "aborted!"); /* doesn't clean up locks! */
837 advise (NULL, "BUG in %s, number out of range", invo_name);
841 advise (NULL, "BUG in %s, scan() botch (%d)", invo_name, incerr);
847 * Run the external program hook on the message.
850 (void)snprintf(b, sizeof (b), "%s/%d", maildir_copy, msgnum + 1);
851 (void)ext_hook("add-hook", b, (char *)0);
865 if (mp->lowmsg == 0) mp->lowmsg = 1;
867 clear_msg_flags (mp, msgnum);
868 set_exists (mp, msgnum);
869 set_unseen (mp, msgnum);
870 mp->msgflags |= SEQMOD;
873 /* If we get here there was some sort of error from scan(),
874 * so stop processing anything more from the spool.
878 } else if (inc_type == INC_FILE) { /* Maildir inbox to process */
883 hghnum = msgnum = mp->hghmsg;
884 for (i = 0; i < num_maildir_entries; i++) {
887 * Check if we need to allocate more space for message status.
888 * If so, then add space for an additional 100 messages.
890 if (msgnum >= mp->hghoff
891 && !(mp = folder_realloc (mp, mp->lowoff, mp->hghoff + 100))) {
892 advise (NULL, "unable to allocate folder storage");
897 sp = Maildir[i].filename;
898 cp = getcpy (m_name (msgnum));
900 if (!trnflag || link(sp, cp) == -1) {
901 static char buf[65536];
904 if ((sf = fopen (sp, "r")) == NULL)
905 adios (sp, "unable to read for copy");
906 if ((pf = fopen (cp, "w+")) == NULL)
907 adios (cp, "unable to write for copy");
908 while ((nrd = fread(buf, 1, sizeof(buf), sf)) > 0)
909 if (fwrite(buf, 1, nrd, pf) != nrd)
911 if (ferror(sf) || fflush(pf) || ferror(pf)) {
913 fclose(pf); fclose(sf); unlink(cp);
915 adios(cp, "copy error %s -> %s", sp, cp);
920 if (pf == NULL && (pf = fopen (cp, "r")) == NULL)
921 adios (cp, "not available");
922 chmod (cp, m_gmprot ());
924 fseek (pf, 0L, SEEK_SET);
925 switch (incerr = scan (pf, msgnum, 0, nfs, width,
926 msgnum == mp->hghmsg + 1 && chgflag,
927 1, NULL, stop - start, noisy)) {
929 printf ("%*d empty\n", DMAXFOLDER, msgnum);
935 /* advise (cp, "unable to read"); already advised */
946 * Run the external program hook on the message.
949 (void)snprintf(b, sizeof (b), "%s/%d", maildir_copy, msgnum + 1);
950 (void)ext_hook("add-hook", b, (char *)0);
961 clear_msg_flags (mp, msgnum);
962 set_exists (mp, msgnum);
963 set_unseen (mp, msgnum);
964 mp->msgflags |= SEQMOD;
968 if (ferror(pf) || fclose (pf)) {
972 adios (cp, "write error on");
977 if (trnflag && unlink (sp) == NOTOK)
978 adios (sp, "couldn't unlink");
979 free (sp); /* Free Maildir[i]->filename */
981 free (Maildir); /* From now on Maildir is just a flag - don't dref! */
984 if (incerr < 0) { /* error */
986 GETGROUPPRIVS(); /* Be sure we can unlock mail file */
987 (void) lkfclose (in, newmail); in = NULL;
988 DROPGROUPPRIVS(); /* And then return us to normal privileges */
990 fclose (in); in = NULL;
992 adios (NULL, "failed");
1007 if ((inc_type == INC_POP) && packfile)
1012 * truncate file we are incorporating from
1014 if (inc_type == INC_FILE && Maildir == NULL) {
1016 if (stat (newmail, &st) != NOTOK && s1.st_mtime != st.st_mtime)
1017 advise (NULL, "new messages have arrived!\007");
1020 if ((newfd = creat (newmail, 0600)) != NOTOK)
1023 admonish (newmail, "error zero'ing");
1024 unlink(map_name(newmail));
1028 printf ("%s not zero'd\n", newmail);
1032 if (msgnum == hghnum) {
1033 admonish (NULL, "no messages incorporated");
1035 context_replace (pfolder, folder); /* update current folder */
1037 mp->curmsg = hghnum + 1;
1038 mp->hghmsg = msgnum;
1039 if (mp->lowmsg == 0)
1041 if (chgflag) /* sigh... */
1042 seq_setcur (mp, mp->curmsg);
1046 * unlock the mail spool
1048 if (inc_type == INC_FILE && Maildir == NULL) {
1050 GETGROUPPRIVS(); /* Be sure we can unlock mail file */
1051 (void) lkfclose (in, newmail); in = NULL;
1052 DROPGROUPPRIVS(); /* And then return us to normal privileges */
1054 fclose (in); in = NULL;
1058 seq_setunseen (mp, 0); /* set the Unseen-Sequence */
1059 seq_save (mp); /* synchronize sequences */
1060 context_save (); /* save the context file */
1069 * Copy message message from spool into
1070 * temporary file. Massage the "From " line
1074 cpymsg (FILE *in, FILE *out)
1077 char *tmpbuf, name[NAMESZ];
1080 state = m_getfld (state, name, tmpbuf, rlwidth, in);
1100 inc_done (int status)
1103 if (packfile && pd != NOTOK)
1104 mbx_close (packfile, pd);
1109 lkfclose(in, newmail);
1117 pop_action (char *s)
1119 fprintf (pf, "%s\n", s);
1120 stop += strlen (s) + 1;
1121 return 0; /* Is return value used? This was missing before 1999-07-15. */
1128 char buffer[BUFSIZ];
1130 snprintf (buffer, sizeof(buffer), "%s\n", s);
1131 for (j = 0; (j = stringdex (mmdlm1, buffer)) >= 0; buffer[j]++)
1133 for (j = 0; (j = stringdex (mmdlm2, buffer)) >= 0; buffer[j]++)
1136 size += strlen (buffer) + 1;
1137 return 0; /* Is return value used? This was missing before 1999-07-15. */
1148 if (stat (packfile, &st) == NOTOK)
1150 if ((md = open (cp = map_name (packfile), O_RDONLY)) == NOTOK
1151 || map_chk (cp, md, &d, (long) st.st_size, 1)) {