2 ** inc.c -- incorporate messages from a maildrop into a folder
4 ** This code is Copyright (c) 2002, by the authors of nmh. See the
5 ** COPYRIGHT file in the root directory of the nmh distribution for
6 ** 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
33 #include <h/fmt_scan.h>
34 #include <h/scansbr.h>
35 #include <h/signals.h>
40 static struct swit switches[] = {
42 { "audit audit-file", 0 },
52 { "form formatfile", 0 },
62 { "width columns", 0 },
70 ** This is an attempt to simplify things by putting all the
71 ** privilege ops into macros.
72 ** *GROUPPRIVS() is related to handling the setgid MAIL property,
73 ** and only applies if MAILGROUP is defined.
74 ** Basically, SAVEGROUPPRIVS() is called right at the top of main()
75 ** to initialise things, and then DROPGROUPPRIVS() and GETGROUPPRIVS()
76 ** do the obvious thing. TRYDROPGROUPPRIVS() has to be safe to call
77 ** before DROPUSERPRIVS() is called [this is needed because setgid()
78 ** sets both effective and real uids if euid is root.]
80 ** There's probably a better implementation if we're allowed to use
81 ** BSD-style setreuid() rather than using POSIX saved-ids.
82 ** Anyway, if you're euid root it's a bit pointless to drop the group
85 ** I'm pretty happy that the security is good provided we aren't setuid root.
86 ** The only things we trust with group=mail privilege are lkfopen()
91 ** For setting and returning to "mail" gid
94 static int return_gid;
96 ** easy case; we're not setuid root, so can drop group privs immediately.
98 #define TRYDROPGROUPPRIVS() DROPGROUPPRIVS()
99 #define DROPGROUPPRIVS() setgid(getgid())
100 #define GETGROUPPRIVS() setgid(return_gid)
101 #define SAVEGROUPPRIVS() return_gid = getegid()
103 /* define *GROUPPRIVS() as null; this avoids having lots of "#ifdef MAILGROUP"s */
104 #define TRYDROPGROUPPRIVS()
105 #define DROPGROUPPRIVS()
106 #define GETGROUPPRIVS()
107 #define SAVEGROUPPRIVS()
108 #endif /* not MAILGROUP */
111 ** these variables have to be globals so that done() can correctly clean
114 static int locked = 0;
115 static char *newmail;
121 static void inc_done(int) NORETURN;
125 main(int argc, char **argv)
127 int chgflag = 1, trnflag = 1;
128 int noisy = 1, width = 0;
129 int hghnum = 0, msgnum = 0;
131 ** <0 if inc hits an error which means it should
132 ** not truncate mailspool
134 char *cp, *maildir = NULL, *folder = NULL;
136 char *audfile = NULL, *from = NULL;
137 char buf[BUFSIZ], **argp, *fmtstr, **arguments;
138 struct msgs *mp = NULL;
141 char b[MAXPATHLEN + 1];
142 /* copy of mail directory because the static gets overwritten */
143 char *maildir_copy = NULL;
152 ** absolutely the first thing we do is save our privileges,
153 ** and drop them if we can.
159 setlocale(LC_ALL, "");
161 invo_name = mhbasename(argv[0]);
163 /* read user profile/context */
166 arguments = getarguments(invo_name, argc, argv, 1);
169 while ((cp = *argp++)) {
171 switch (smatch(++cp, switches)) {
173 ambigsw(cp, switches);
176 adios(NULL, "-%s unknown", cp);
179 snprintf(buf, sizeof(buf), "%s [+folder] [switches]", invo_name);
180 print_help(buf, switches, 1);
183 print_version(invo_name);
187 if (!(cp = *argp++) || *cp == '-')
188 adios(NULL, "missing argument to %s", argp[-2]);
189 audfile = getcpy(expanddir(cp));
203 ** The flag `trnflag' has the value:
205 ** 2 if -truncate is given
206 ** 1 by default (truncating is default)
207 ** 0 if -notruncate is given
217 if (!(cp = *argp++) || *cp == '-')
218 adios(NULL, "missing argument to %s",
220 from = getcpy(expanddir(cp));
223 ** If the truncate file is in default state,
224 ** change to not truncate.
238 if (!(form = *argp++) || *form == '-')
239 adios(NULL, "missing argument to %s",
244 if (!(cp = *argp++) || *cp == '-')
245 adios(NULL, "missing argument to %s",
251 if (*cp == '+' || *cp == '@') {
253 adios(NULL, "only one folder at a time!");
255 folder = getcpy(expandfol(cp));
257 adios(NULL, "usage: %s [+folder] [switches]",
263 ** NOTE: above this point you should use TRYDROPGROUPPRIVS(),
264 ** not DROPGROUPPRIVS().
266 /* guarantee dropping group priveleges; we might not have done so earlier */
270 ** We will get the mail from a file
271 ** (typically the standard maildrop)
275 else if ((newmail = getenv("MAILDROP")) && *newmail)
276 newmail = toabsdir(newmail);
277 else if ((newmail = context_find("maildrop")) && *newmail)
278 newmail = toabsdir(newmail);
280 newmail = concat(mailspool, "/", getusername(), NULL);
282 if (stat(newmail, &s1) == NOTOK || s1.st_size == 0)
283 adios(NULL, "no mail to incorporate");
285 if ((cp = strdup(newmail)) == NULL)
286 adios(NULL, "error allocating memory to copy newmail");
291 folder = getdeffol();
292 maildir = toabsdir(folder);
294 if ((maildir_copy = strdup(maildir)) == NULL)
295 adios(maildir, "error allocating memory to copy maildir");
297 create_folder(maildir, noisy ? 0 : 1, done);
299 if (chdir(maildir) == NOTOK)
300 adios(maildir, "unable to change directory to");
302 /* read folder and create message structure */
303 if (!(mp = folder_read(folder)))
304 adios(NULL, "unable to read folder %s", folder);
306 if (access(newmail, W_OK) != NOTOK) {
309 SIGNAL(SIGHUP, SIG_IGN);
310 SIGNAL(SIGINT, SIG_IGN);
311 SIGNAL(SIGQUIT, SIG_IGN);
312 SIGNAL(SIGTERM, SIG_IGN);
315 GETGROUPPRIVS(); /* Reset gid to lock mail file */
316 in = lkfopen(newmail, "r");
319 adios(NULL, "unable to lock and fopen %s", newmail);
320 fstat(fileno(in), &s1);
323 if ((in = fopen(newmail, "r")) == NULL)
324 adios(newmail, "unable to read");
327 /* This shouldn't be necessary but it can't hurt. */
332 if ((i = stat(audfile, &st)) == NOTOK)
333 advise(NULL, "Creating Receive-Audit: %s", audfile);
334 if ((aud = fopen(audfile, "a")) == NULL)
335 adios(audfile, "unable to append to");
337 chmod(audfile, m_gmprot());
339 fprintf(aud, from ? "<<inc>> %s -ms %s\n" : "<<inc>> %s\n",
344 if (context_find("mhe")) {
346 cp = concat(maildir, "/++", NULL);
348 if ((mhe = fopen(cp, "a")) == NULL)
349 admonish(cp, "unable to append to");
352 chmod(cp, m_gmprot());
357 /* Get new format string */
358 fmtstr = new_fs(form, FORMAT);
361 printf("Incorporating new mail into %s...\n\n", folder);
366 ** Get the mail from file (usually mail spool)
369 hghnum = msgnum = mp->hghmsg;
372 ** Check if we need to allocate more space for message status.
373 ** If so, then add space for an additional 100 messages.
375 if (msgnum >= mp->hghoff && !(mp = folder_realloc(mp, mp->lowoff, mp->hghoff + 100))) {
376 advise(NULL, "unable to allocate folder storage");
381 /* create scanline for new message */
382 switch (incerr = scan(in, msgnum + 1, msgnum + 1,
383 noisy ? fmtstr : NULL, width,
384 msgnum == hghnum && chgflag, 1)) {
391 fputs("inc aborted!\n", aud);
392 /* doesn't clean up locks! */
393 advise(NULL, "aborted!");
397 advise(NULL, "BUG in %s, number out of range",
402 advise(NULL, "BUG in %s, scan() botch (%d)",
408 ** Run the external program hook on the message.
411 snprintf(b, sizeof (b), "%s/%d", maildir_copy,
413 ext_hook("add-hook", b, NULL);
428 clear_msg_flags(mp, msgnum);
429 set_exists(mp, msgnum);
430 set_unseen(mp, msgnum);
431 mp->msgflags |= SEQMOD;
435 ** If we get here there was some sort of error from scan(),
436 ** so stop processing anything more from the spool.
441 if (incerr < 0) { /* error */
443 GETGROUPPRIVS(); /* Be sure we can unlock mail file */
444 lkfclose(in, newmail); in = NULL;
446 ** And then return us to normal
450 fclose(in); in = NULL;
452 adios(NULL, "failed");
467 ** truncate file we are incorporating from
470 if (stat(newmail, &st) != NOTOK && s1.st_mtime != st.st_mtime)
471 advise(NULL, "new messages have arrived!\007");
474 if ((newfd = creat(newmail, 0600)) != NOTOK)
477 admonish(newmail, "error zero'ing");
480 printf("%s not zero'd\n", newmail);
483 if (msgnum == hghnum) {
484 admonish(NULL, "no messages incorporated");
486 context_replace(curfolder, folder); /* update current folder */
488 mp->curmsg = hghnum + 1;
492 if (chgflag) /* sigh... */
493 seq_setcur(mp, mp->curmsg);
497 ** unlock the mail spool
500 GETGROUPPRIVS(); /* Be sure we can unlock mail file */
501 lkfclose(in, newmail); in = NULL;
502 DROPGROUPPRIVS(); /* And then return us to normal privileges */
504 fclose(in); in = NULL;
507 seq_setunseen(mp, 1); /* add new msgs to unseen sequences */
508 seq_save(mp); /* synchronize sequences */
509 context_save(); /* save the context file */
519 lkfclose(in, newmail);