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 },
54 { "format string", 5 },
64 { "width columns", 0 },
72 ** This is an attempt to simplify things by putting all the
73 ** privilege ops into macros.
74 ** *GROUPPRIVS() is related to handling the setgid MAIL property,
75 ** and only applies if MAILGROUP is defined.
76 ** Basically, SAVEGROUPPRIVS() is called right at the top of main()
77 ** to initialise things, and then DROPGROUPPRIVS() and GETGROUPPRIVS()
78 ** do the obvious thing. TRYDROPGROUPPRIVS() has to be safe to call
79 ** before DROPUSERPRIVS() is called [this is needed because setgid()
80 ** sets both effective and real uids if euid is root.]
82 ** There's probably a better implementation if we're allowed to use
83 ** BSD-style setreuid() rather than using POSIX saved-ids.
84 ** Anyway, if you're euid root it's a bit pointless to drop the group
87 ** I'm pretty happy that the security is good provided we aren't setuid root.
88 ** The only things we trust with group=mail privilege are lkfopen()
93 ** For setting and returning to "mail" gid
96 static int return_gid;
98 ** easy case; we're not setuid root, so can drop group privs immediately.
100 #define TRYDROPGROUPPRIVS() DROPGROUPPRIVS()
101 #define DROPGROUPPRIVS() setgid(getgid())
102 #define GETGROUPPRIVS() setgid(return_gid)
103 #define SAVEGROUPPRIVS() return_gid = getegid()
105 /* define *GROUPPRIVS() as null; this avoids having lots of "#ifdef MAILGROUP"s */
106 #define TRYDROPGROUPPRIVS()
107 #define DROPGROUPPRIVS()
108 #define GETGROUPPRIVS()
109 #define SAVEGROUPPRIVS()
110 #endif /* not MAILGROUP */
113 ** these variables have to be globals so that done() can correctly clean
116 static int locked = 0;
117 static char *newmail;
123 static void inc_done(int) NORETURN;
127 main(int argc, char **argv)
129 int chgflag = 1, trnflag = 1;
130 int noisy = 1, width = 0;
131 int hghnum = 0, msgnum = 0;
133 ** <0 if inc hits an error which means it should
134 ** not truncate mailspool
136 char *cp, *maildir = NULL, *folder = NULL;
137 char *format = NULL, *form = NULL;
138 char *audfile = NULL, *from = NULL;
139 char buf[BUFSIZ], **argp, *nfs, **arguments;
140 struct msgs *mp = NULL;
143 char b[MAXPATHLEN + 1];
144 /* copy of mail directory because the static gets overwritten */
145 char *maildir_copy = NULL;
154 ** absolutely the first thing we do is save our privileges,
155 ** and drop them if we can.
161 setlocale(LC_ALL, "");
163 invo_name = mhbasename(argv[0]);
165 /* read user profile/context */
168 arguments = getarguments(invo_name, argc, argv, 1);
171 while ((cp = *argp++)) {
173 switch (smatch(++cp, switches)) {
175 ambigsw(cp, switches);
178 adios(NULL, "-%s unknown", cp);
181 snprintf(buf, sizeof(buf), "%s [+folder] [switches]", invo_name);
182 print_help(buf, switches, 1);
185 print_version(invo_name);
189 if (!(cp = *argp++) || *cp == '-')
190 adios(NULL, "missing argument to %s", argp[-2]);
191 audfile = getcpy(expanddir(cp));
205 ** The flag `trnflag' has the value:
207 ** 2 if -truncate is given
208 ** 1 by default (truncating is default)
209 ** 0 if -notruncate is given
219 if (!(cp = *argp++) || *cp == '-')
220 adios(NULL, "missing argument to %s",
222 from = getcpy(expanddir(cp));
225 ** If the truncate file is in default state,
226 ** change to not truncate.
240 if (!(form = *argp++) || *form == '-')
241 adios(NULL, "missing argument to %s",
246 if (!(format = *argp++) || *format == '-')
247 adios(NULL, "missing argument to %s",
253 if (!(cp = *argp++) || *cp == '-')
254 adios(NULL, "missing argument to %s",
260 if (*cp == '+' || *cp == '@') {
262 adios(NULL, "only one folder at a time!");
264 folder = getcpy(expandfol(cp));
266 adios(NULL, "usage: %s [+folder] [switches]",
272 ** NOTE: above this point you should use TRYDROPGROUPPRIVS(),
273 ** not DROPGROUPPRIVS().
275 /* guarantee dropping group priveleges; we might not have done so earlier */
279 ** We will get the mail from a file
280 ** (typically the standard maildrop)
284 else if ((newmail = getenv("MAILDROP")) && *newmail)
285 newmail = toabsdir(newmail);
286 else if ((newmail = context_find("maildrop")) && *newmail)
287 newmail = toabsdir(newmail);
289 newmail = concat(mailspool, "/", getusername(), NULL);
291 if (stat(newmail, &s1) == NOTOK || s1.st_size == 0)
292 adios(NULL, "no mail to incorporate");
294 if ((cp = strdup(newmail)) == NULL)
295 adios(NULL, "error allocating memory to copy newmail");
300 folder = getdeffol();
301 maildir = toabsdir(folder);
303 if ((maildir_copy = strdup(maildir)) == NULL)
304 adios(maildir, "error allocating memory to copy maildir");
306 if (!folder_exists(maildir)) {
308 ** If the folder doesn't exist, and we're given the -silent
312 create_folder(maildir, 0, done);
317 if (chdir(maildir) == NOTOK)
318 adios(maildir, "unable to change directory to");
320 /* read folder and create message structure */
321 if (!(mp = folder_read(folder)))
322 adios(NULL, "unable to read folder %s", folder);
324 if (access(newmail, W_OK) != NOTOK) {
327 SIGNAL(SIGHUP, SIG_IGN);
328 SIGNAL(SIGINT, SIG_IGN);
329 SIGNAL(SIGQUIT, SIG_IGN);
330 SIGNAL(SIGTERM, SIG_IGN);
333 GETGROUPPRIVS(); /* Reset gid to lock mail file */
334 in = lkfopen(newmail, "r");
337 adios(NULL, "unable to lock and fopen %s", newmail);
338 fstat(fileno(in), &s1);
341 if ((in = fopen(newmail, "r")) == NULL)
342 adios(newmail, "unable to read");
345 /* This shouldn't be necessary but it can't hurt. */
350 if ((i = stat(audfile, &st)) == NOTOK)
351 advise(NULL, "Creating Receive-Audit: %s", audfile);
352 if ((aud = fopen(audfile, "a")) == NULL)
353 adios(audfile, "unable to append to");
355 chmod(audfile, m_gmprot());
357 fprintf(aud, from ? "<<inc>> %s -ms %s\n" : "<<inc>> %s\n",
362 if (context_find("mhe")) {
364 cp = concat(maildir, "/++", NULL);
366 if ((mhe = fopen(cp, "a")) == NULL)
367 admonish(cp, "unable to append to");
370 chmod(cp, m_gmprot());
375 /* Get new format string */
376 nfs = new_fs(form, format, FORMAT);
379 printf("Incorporating new mail into %s...\n\n", folder);
384 ** Get the mail from file (usually mail spool)
386 m_unknown(in); /* the MAGIC invocation... */
387 hghnum = msgnum = mp->hghmsg;
390 ** Check if we need to allocate more space for message status.
391 ** If so, then add space for an additional 100 messages.
393 if (msgnum >= mp->hghoff && !(mp = folder_realloc(mp, mp->lowoff, mp->hghoff + 100))) {
394 advise(NULL, "unable to allocate folder storage");
399 /* create scanline for new message */
400 switch (incerr = scan(in, msgnum + 1, msgnum + 1, nfs, width,
401 msgnum == hghnum && chgflag, 1, NULL, 0L, noisy)) {
408 fputs("inc aborted!\n", aud);
409 /* doesn't clean up locks! */
410 advise(NULL, "aborted!");
414 advise(NULL, "BUG in %s, number out of range",
419 advise(NULL, "BUG in %s, scan() botch (%d)",
426 ** Run the external program hook on the message.
429 snprintf(b, sizeof (b), "%s/%d", maildir_copy,
431 ext_hook("add-hook", b, NULL);
446 clear_msg_flags(mp, msgnum);
447 set_exists(mp, msgnum);
448 set_unseen(mp, msgnum);
449 mp->msgflags |= SEQMOD;
453 ** If we get here there was some sort of error from scan(),
454 ** so stop processing anything more from the spool.
459 if (incerr < 0) { /* error */
461 GETGROUPPRIVS(); /* Be sure we can unlock mail file */
462 lkfclose(in, newmail); in = NULL;
464 ** And then return us to normal
468 fclose(in); in = NULL;
470 adios(NULL, "failed");
485 ** truncate file we are incorporating from
488 if (stat(newmail, &st) != NOTOK && s1.st_mtime != st.st_mtime)
489 advise(NULL, "new messages have arrived!\007");
492 if ((newfd = creat(newmail, 0600)) != NOTOK)
495 admonish(newmail, "error zero'ing");
498 printf("%s not zero'd\n", newmail);
501 if (msgnum == hghnum) {
502 admonish(NULL, "no messages incorporated");
504 context_replace(curfolder, folder); /* update current folder */
506 mp->curmsg = hghnum + 1;
510 if (chgflag) /* sigh... */
511 seq_setcur(mp, mp->curmsg);
515 ** unlock the mail spool
518 GETGROUPPRIVS(); /* Be sure we can unlock mail file */
519 lkfclose(in, newmail); in = NULL;
520 DROPGROUPPRIVS(); /* And then return us to normal privileges */
522 fclose(in); in = NULL;
525 seq_setunseen(mp, 1); /* add new msgs to unseen sequences */
526 seq_save(mp); /* synchronize sequences */
527 context_save(); /* save the context file */
537 lkfclose(in, newmail);