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>
41 static struct swit switches[] = {
43 { "audit audit-file", 0 },
53 { "form formatfile", 0 },
55 { "format string", 5 },
65 { "width columns", 0 },
73 ** This is an attempt to simplify things by putting all the
74 ** privilege ops into macros.
75 ** *GROUPPRIVS() is related to handling the setgid MAIL property,
76 ** and only applies if MAILGROUP is defined.
77 ** Basically, SAVEGROUPPRIVS() is called right at the top of main()
78 ** to initialise things, and then DROPGROUPPRIVS() and GETGROUPPRIVS()
79 ** do the obvious thing. TRYDROPGROUPPRIVS() has to be safe to call
80 ** before DROPUSERPRIVS() is called [this is needed because setgid()
81 ** sets both effective and real uids if euid is root.]
83 ** There's probably a better implementation if we're allowed to use
84 ** BSD-style setreuid() rather than using POSIX saved-ids.
85 ** Anyway, if you're euid root it's a bit pointless to drop the group
88 ** I'm pretty happy that the security is good provided we aren't setuid root.
89 ** The only things we trust with group=mail privilege are lkfopen()
94 ** For setting and returning to "mail" gid
97 static int return_gid;
99 ** easy case; we're not setuid root, so can drop group privs immediately.
101 #define TRYDROPGROUPPRIVS() DROPGROUPPRIVS()
102 #define DROPGROUPPRIVS() setgid(getgid())
103 #define GETGROUPPRIVS() setgid(return_gid)
104 #define SAVEGROUPPRIVS() return_gid = getegid()
106 /* define *GROUPPRIVS() as null; this avoids having lots of "#ifdef MAILGROUP"s */
107 #define TRYDROPGROUPPRIVS()
108 #define DROPGROUPPRIVS()
109 #define GETGROUPPRIVS()
110 #define SAVEGROUPPRIVS()
111 #endif /* not MAILGROUP */
114 ** these variables have to be globals so that done() can correctly clean
117 static int locked = 0;
118 static char *newmail;
124 static void inc_done(int) NORETURN;
128 main(int argc, char **argv)
130 int chgflag = 1, trnflag = 1;
131 int noisy = 1, width = 0;
132 int hghnum = 0, msgnum = 0;
134 ** <0 if inc hits an error which means it should
135 ** not truncate mailspool
137 char *cp, *maildir = NULL, *folder = NULL;
138 char *format = NULL, *form = NULL;
139 char *audfile = NULL, *from = NULL;
140 char buf[BUFSIZ], **argp, *nfs, **arguments;
141 struct msgs *mp = NULL;
144 char b[MAXPATHLEN + 1];
145 /* copy of mail directory because the static gets overwritten */
146 char *maildir_copy = NULL;
155 ** absolutely the first thing we do is save our privileges,
156 ** and drop them if we can.
162 setlocale(LC_ALL, "");
164 invo_name = mhbasename(argv[0]);
166 /* read user profile/context */
169 arguments = getarguments(invo_name, argc, argv, 1);
172 while ((cp = *argp++)) {
174 switch (smatch(++cp, switches)) {
176 ambigsw(cp, switches);
179 adios(NULL, "-%s unknown", cp);
182 snprintf(buf, sizeof(buf), "%s [+folder] [switches]", invo_name);
183 print_help(buf, switches, 1);
186 print_version(invo_name);
190 if (!(cp = *argp++) || *cp == '-')
191 adios(NULL, "missing argument to %s", argp[-2]);
192 audfile = getcpy(expanddir(cp));
206 ** The flag `trnflag' has the value:
208 ** 2 if -truncate is given
209 ** 1 by default (truncating is default)
210 ** 0 if -notruncate is given
220 if (!(cp = *argp++) || *cp == '-')
221 adios(NULL, "missing argument to %s",
223 from = getcpy(expanddir(cp));
226 ** If the truncate file is in default state,
227 ** change to not truncate.
241 if (!(form = *argp++) || *form == '-')
242 adios(NULL, "missing argument to %s",
247 if (!(format = *argp++) || *format == '-')
248 adios(NULL, "missing argument to %s",
254 if (!(cp = *argp++) || *cp == '-')
255 adios(NULL, "missing argument to %s",
261 if (*cp == '+' || *cp == '@') {
263 adios(NULL, "only one folder at a time!");
265 folder = getcpy(expandfol(cp));
267 adios(NULL, "usage: %s [+folder] [switches]",
273 ** NOTE: above this point you should use TRYDROPGROUPPRIVS(),
274 ** not DROPGROUPPRIVS().
276 /* guarantee dropping group priveleges; we might not have done so earlier */
280 ** We will get the mail from a file
281 ** (typically the standard maildrop)
285 else if ((newmail = getenv("MAILDROP")) && *newmail)
286 newmail = toabsdir(newmail);
287 else if ((newmail = context_find("maildrop")) && *newmail)
288 newmail = toabsdir(newmail);
290 newmail = concat(mailspool, "/", getusername(), NULL);
292 if (stat(newmail, &s1) == NOTOK || s1.st_size == 0)
293 adios(NULL, "no mail to incorporate");
295 if ((cp = strdup(newmail)) == NULL)
296 adios(NULL, "error allocating memory to copy newmail");
301 folder = getdeffol();
302 maildir = toabsdir(folder);
304 if ((maildir_copy = strdup(maildir)) == NULL)
305 adios(maildir, "error allocating memory to copy maildir");
307 if (!folder_exists(maildir)) {
309 ** If the folder doesn't exist, and we're given the -silent
313 create_folder(maildir, 0, done);
318 if (chdir(maildir) == NOTOK)
319 adios(maildir, "unable to change directory to");
321 /* read folder and create message structure */
322 if (!(mp = folder_read(folder)))
323 adios(NULL, "unable to read folder %s", folder);
325 if (access(newmail, W_OK) != NOTOK) {
328 SIGNAL(SIGHUP, SIG_IGN);
329 SIGNAL(SIGINT, SIG_IGN);
330 SIGNAL(SIGQUIT, SIG_IGN);
331 SIGNAL(SIGTERM, SIG_IGN);
334 GETGROUPPRIVS(); /* Reset gid to lock mail file */
335 in = lkfopen(newmail, "r");
338 adios(NULL, "unable to lock and fopen %s", newmail);
339 fstat(fileno(in), &s1);
342 if ((in = fopen(newmail, "r")) == NULL)
343 adios(newmail, "unable to read");
346 /* This shouldn't be necessary but it can't hurt. */
351 if ((i = stat(audfile, &st)) == NOTOK)
352 advise(NULL, "Creating Receive-Audit: %s", audfile);
353 if ((aud = fopen(audfile, "a")) == NULL)
354 adios(audfile, "unable to append to");
356 chmod(audfile, m_gmprot());
358 fprintf(aud, from ? "<<inc>> %s -ms %s\n" : "<<inc>> %s\n",
363 if (context_find("mhe")) {
365 cp = concat(maildir, "/++", NULL);
367 if ((mhe = fopen(cp, "a")) == NULL)
368 admonish(cp, "unable to append to");
371 chmod(cp, m_gmprot());
376 /* Get new format string */
377 nfs = new_fs(form, format, FORMAT);
380 printf("Incorporating new mail into %s...\n\n", folder);
385 ** Get the mail from file (usually mail spool)
387 m_unknown(in); /* the MAGIC invocation... */
388 hghnum = msgnum = mp->hghmsg;
391 ** Check if we need to allocate more space for message status.
392 ** If so, then add space for an additional 100 messages.
394 if (msgnum >= mp->hghoff && !(mp = folder_realloc(mp, mp->lowoff, mp->hghoff + 100))) {
395 advise(NULL, "unable to allocate folder storage");
400 /* create scanline for new message */
401 switch (incerr = scan(in, msgnum + 1, msgnum + 1, nfs, width,
402 msgnum == hghnum && chgflag, 1, NULL, 0L, noisy)) {
409 fputs("inc aborted!\n", aud);
410 /* doesn't clean up locks! */
411 advise(NULL, "aborted!");
415 advise(NULL, "BUG in %s, number out of range",
420 advise(NULL, "BUG in %s, scan() botch (%d)",
427 ** Run the external program hook on the message.
430 snprintf(b, sizeof (b), "%s/%d", maildir_copy,
432 ext_hook("add-hook", b, NULL);
447 clear_msg_flags(mp, msgnum);
448 set_exists(mp, msgnum);
449 set_unseen(mp, msgnum);
450 mp->msgflags |= SEQMOD;
454 ** If we get here there was some sort of error from scan(),
455 ** so stop processing anything more from the spool.
460 if (incerr < 0) { /* error */
462 GETGROUPPRIVS(); /* Be sure we can unlock mail file */
463 lkfclose(in, newmail); in = NULL;
465 ** And then return us to normal
469 fclose(in); in = NULL;
471 adios(NULL, "failed");
486 ** truncate file we are incorporating from
489 if (stat(newmail, &st) != NOTOK && s1.st_mtime != st.st_mtime)
490 advise(NULL, "new messages have arrived!\007");
493 if ((newfd = creat(newmail, 0600)) != NOTOK)
496 admonish(newmail, "error zero'ing");
499 printf("%s not zero'd\n", newmail);
502 if (msgnum == hghnum) {
503 admonish(NULL, "no messages incorporated");
505 context_replace(curfolder, folder); /* update current folder */
507 mp->curmsg = hghnum + 1;
511 if (chgflag) /* sigh... */
512 seq_setcur(mp, mp->curmsg);
516 ** unlock the mail spool
519 GETGROUPPRIVS(); /* Be sure we can unlock mail file */
520 lkfclose(in, newmail); in = NULL;
521 DROPGROUPPRIVS(); /* And then return us to normal privileges */
523 fclose(in); in = NULL;
526 seq_setunseen(mp, 1); /* add new msgs to unseen sequences */
527 seq_save(mp); /* synchronize sequences */
528 context_save(); /* save the context file */
538 lkfclose(in, newmail);