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, *nfs, **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 if (!folder_exists(maildir)) {
299 ** If the folder doesn't exist, and we're given the -silent
303 create_folder(maildir, 0, done);
308 if (chdir(maildir) == NOTOK)
309 adios(maildir, "unable to change directory to");
311 /* read folder and create message structure */
312 if (!(mp = folder_read(folder)))
313 adios(NULL, "unable to read folder %s", folder);
315 if (access(newmail, W_OK) != NOTOK) {
318 SIGNAL(SIGHUP, SIG_IGN);
319 SIGNAL(SIGINT, SIG_IGN);
320 SIGNAL(SIGQUIT, SIG_IGN);
321 SIGNAL(SIGTERM, SIG_IGN);
324 GETGROUPPRIVS(); /* Reset gid to lock mail file */
325 in = lkfopen(newmail, "r");
328 adios(NULL, "unable to lock and fopen %s", newmail);
329 fstat(fileno(in), &s1);
332 if ((in = fopen(newmail, "r")) == NULL)
333 adios(newmail, "unable to read");
336 /* This shouldn't be necessary but it can't hurt. */
341 if ((i = stat(audfile, &st)) == NOTOK)
342 advise(NULL, "Creating Receive-Audit: %s", audfile);
343 if ((aud = fopen(audfile, "a")) == NULL)
344 adios(audfile, "unable to append to");
346 chmod(audfile, m_gmprot());
348 fprintf(aud, from ? "<<inc>> %s -ms %s\n" : "<<inc>> %s\n",
353 if (context_find("mhe")) {
355 cp = concat(maildir, "/++", NULL);
357 if ((mhe = fopen(cp, "a")) == NULL)
358 admonish(cp, "unable to append to");
361 chmod(cp, m_gmprot());
366 /* Get new format string */
367 nfs = new_fs(form, FORMAT);
370 printf("Incorporating new mail into %s...\n\n", folder);
375 ** Get the mail from file (usually mail spool)
377 m_unknown(in); /* the MAGIC invocation... */
378 hghnum = msgnum = mp->hghmsg;
381 ** Check if we need to allocate more space for message status.
382 ** If so, then add space for an additional 100 messages.
384 if (msgnum >= mp->hghoff && !(mp = folder_realloc(mp, mp->lowoff, mp->hghoff + 100))) {
385 advise(NULL, "unable to allocate folder storage");
390 /* create scanline for new message */
391 switch (incerr = scan(in, msgnum + 1, msgnum + 1, nfs, width,
392 msgnum == hghnum && chgflag, 1, NULL, 0L, noisy)) {
399 fputs("inc aborted!\n", aud);
400 /* doesn't clean up locks! */
401 advise(NULL, "aborted!");
405 advise(NULL, "BUG in %s, number out of range",
410 advise(NULL, "BUG in %s, scan() botch (%d)",
416 ** Run the external program hook on the message.
419 snprintf(b, sizeof (b), "%s/%d", maildir_copy,
421 ext_hook("add-hook", b, NULL);
436 clear_msg_flags(mp, msgnum);
437 set_exists(mp, msgnum);
438 set_unseen(mp, msgnum);
439 mp->msgflags |= SEQMOD;
443 ** If we get here there was some sort of error from scan(),
444 ** so stop processing anything more from the spool.
449 if (incerr < 0) { /* error */
451 GETGROUPPRIVS(); /* Be sure we can unlock mail file */
452 lkfclose(in, newmail); in = NULL;
454 ** And then return us to normal
458 fclose(in); in = NULL;
460 adios(NULL, "failed");
475 ** truncate file we are incorporating from
478 if (stat(newmail, &st) != NOTOK && s1.st_mtime != st.st_mtime)
479 advise(NULL, "new messages have arrived!\007");
482 if ((newfd = creat(newmail, 0600)) != NOTOK)
485 admonish(newmail, "error zero'ing");
488 printf("%s not zero'd\n", newmail);
491 if (msgnum == hghnum) {
492 admonish(NULL, "no messages incorporated");
494 context_replace(curfolder, folder); /* update current folder */
496 mp->curmsg = hghnum + 1;
500 if (chgflag) /* sigh... */
501 seq_setcur(mp, mp->curmsg);
505 ** unlock the mail spool
508 GETGROUPPRIVS(); /* Be sure we can unlock mail file */
509 lkfclose(in, newmail); in = NULL;
510 DROPGROUPPRIVS(); /* And then return us to normal privileges */
512 fclose(in); in = NULL;
515 seq_setunseen(mp, 1); /* add new msgs to unseen sequences */
516 seq_save(mp); /* synchronize sequences */
517 context_save(); /* save the context file */
527 lkfclose(in, newmail);