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
32 #include <h/fmt_scan.h>
33 #include <h/scansbr.h>
34 #include <h/signals.h>
43 #ifdef HAVE_SYS_PARAM_H
44 # include <sys/param.h>
47 static struct swit switches[] = {
49 { "audit audit-file", 0 },
59 { "form formatfile", 0 },
69 { "width columns", 0 },
78 ** This is an attempt to simplify things by putting all the
79 ** privilege ops into macros.
80 ** *GROUPPRIVS() is related to handling the setgid MAIL property,
81 ** and only applies if MAILGROUP is defined.
82 ** Basically, SAVEGROUPPRIVS() is called right at the top of main()
83 ** to initialise things, and then DROPGROUPPRIVS() and GETGROUPPRIVS()
84 ** do the obvious thing. TRYDROPGROUPPRIVS() has to be safe to call
85 ** before DROPUSERPRIVS() is called [this is needed because setgid()
86 ** sets both effective and real uids if euid is root.]
88 ** There's probably a better implementation if we're allowed to use
89 ** BSD-style setreuid() rather than using POSIX saved-ids.
90 ** Anyway, if you're euid root it's a bit pointless to drop the group
93 ** I'm pretty happy that the security is good provided we aren't setuid root.
94 ** The only things we trust with group=mail privilege are lkfopen()
99 ** For setting and returning to "mail" gid
102 static int return_gid;
104 ** easy case; we're not setuid root, so can drop group privs immediately.
106 #define TRYDROPGROUPPRIVS() DROPGROUPPRIVS()
107 #define DROPGROUPPRIVS() \
108 if (setegid(getgid()) != 0) { \
109 advise ("setegid", "unable to set group to %ld", (long) getgid()); \
112 #define GETGROUPPRIVS() \
113 if (setegid(return_gid) != 0) { \
114 advise ("setegid", "unable to set group to %ld", (long) return_gid); \
117 #define SAVEGROUPPRIVS() return_gid = getegid()
119 /* define *GROUPPRIVS() as null; this avoids having lots of "#ifdef MAILGROUP"s */
120 #define TRYDROPGROUPPRIVS()
121 #define DROPGROUPPRIVS()
122 #define GETGROUPPRIVS()
123 #define SAVEGROUPPRIVS()
124 #endif /* not MAILGROUP */
127 ** these variables have to be globals so that done() can correctly clean
130 static int locked = 0;
131 static char *newmail;
141 main(int argc, char **argv)
143 int chgflag = 1, trnflag = 1;
144 int noisy = 1, width = 0;
145 int hghnum = 0, msgnum = 0;
147 ** <0 if inc hits an error which means it should
148 ** not truncate mailspool
150 char *cp, *maildir = NULL, *folder = NULL;
152 char *audfile = NULL, *from = NULL;
153 char buf[BUFSIZ], **argp, *fmtstr, **arguments;
154 struct msgs *mp = NULL;
157 char b[MAXPATHLEN + 1];
158 /* copy of mail directory because the static gets overwritten */
159 char *maildir_copy = NULL;
161 if (atexit(inc_done) != 0) {
162 adios(EX_OSERR, NULL, "atexit failed");
166 ** absolutely the first thing we do is save our privileges,
167 ** and drop them if we can.
172 setlocale(LC_ALL, "");
173 invo_name = mhbasename(argv[0]);
175 /* read user profile/context */
178 arguments = getarguments(invo_name, argc, argv, 1);
181 while ((cp = *argp++)) {
183 switch (smatch(++cp, switches)) {
185 ambigsw(cp, switches);
188 adios(EX_USAGE, NULL, "-%s unknown", cp);
191 snprintf(buf, sizeof(buf), "%s [+folder] [switches]", invo_name);
192 print_help(buf, switches, 1);
193 exit(argc == 2 ? EX_OK : EX_USAGE);
195 print_version(invo_name);
196 exit(argc == 2 ? EX_OK : EX_USAGE);
199 if (!(cp = *argp++) || *cp == '-')
200 adios(EX_USAGE, NULL, "missing argument to %s", argp[-2]);
201 audfile = getcpy(expanddir(cp));
215 ** The flag `trnflag' has the value:
217 ** 2 if -truncate is given
218 ** 1 by default (truncating is default)
219 ** 0 if -notruncate is given
229 if (!(cp = *argp++) || *cp == '-')
230 adios(EX_USAGE, NULL, "missing argument to %s",
232 from = getcpy(expanddir(cp));
235 ** If the truncate file is in default state,
236 ** change to not truncate.
250 if (!(form = *argp++) || *form == '-')
251 adios(EX_USAGE, NULL, "missing argument to %s",
256 if (!(cp = *argp++) || *cp == '-')
257 adios(EX_USAGE, NULL, "missing argument to %s",
263 if (*cp == '+' || *cp == '@') {
265 adios(EX_USAGE, NULL, "only one folder at a time!");
267 folder = getcpy(expandfol(cp));
269 adios(EX_USAGE, NULL, "usage: %s [+folder] [switches]",
275 ** NOTE: above this point you should use TRYDROPGROUPPRIVS(),
276 ** not DROPGROUPPRIVS().
278 /* guarantee dropping group priveleges; we might not have done so earlier */
282 ** We will get the mail from a file
283 ** (typically the standard maildrop)
287 else if ((newmail = getenv("MAILDROP")) && *newmail)
288 newmail = toabsdir(newmail);
289 else if ((newmail = context_find("maildrop")) && *newmail)
290 newmail = toabsdir(newmail);
292 newmail = concat(mailspool, "/", getusername(), NULL);
294 if (stat(newmail, &s1) == NOTOK || s1.st_size == 0)
295 adios(EX_DATAERR, NULL, "no mail to incorporate");
297 if ((cp = strdup(newmail)) == NULL)
298 adios(EX_OSERR, NULL, "error allocating memory to copy newmail");
303 folder = getdeffol();
304 maildir = toabsdir(folder);
306 if ((maildir_copy = strdup(maildir)) == NULL)
307 adios(EX_OSERR, maildir, "error allocating memory to copy maildir");
309 create_folder(maildir, noisy ? 0 : 1, exit);
311 if (chdir(maildir) == NOTOK)
312 adios(EX_OSERR, maildir, "unable to change directory to");
314 /* read folder and create message structure */
315 if (!(mp = folder_read(folder)))
316 adios(EX_IOERR, NULL, "unable to read folder %s", folder);
318 if (access(newmail, W_OK) != NOTOK) {
321 SIGNAL(SIGHUP, SIG_IGN);
322 SIGNAL(SIGINT, SIG_IGN);
323 SIGNAL(SIGQUIT, SIG_IGN);
324 SIGNAL(SIGTERM, SIG_IGN);
327 GETGROUPPRIVS(); /* Reset gid to lock mail file */
328 in = lkfopen(newmail, "r");
331 adios(EX_IOERR, NULL, "unable to lock and fopen %s", newmail);
332 fstat(fileno(in), &s1);
335 if ((in = fopen(newmail, "r")) == NULL)
336 adios(EX_IOERR, newmail, "unable to read");
339 /* This shouldn't be necessary but it can't hurt. */
344 if ((i = stat(audfile, &st)) == NOTOK)
345 advise(NULL, "Creating Receive-Audit: %s", audfile);
346 if ((aud = fopen(audfile, "a")) == NULL)
347 adios(EX_IOERR, audfile, "unable to append to");
349 chmod(audfile, m_gmprot());
351 fprintf(aud, from ? "<<inc>> %s -ms %s\n" : "<<inc>> %s\n",
355 /* Get new format string */
356 fmtstr = new_fs(form, FORMAT);
359 printf("Incorporating new mail into %s...\n\n", folder);
364 ** Get the mail from file (usually mail spool)
367 hghnum = msgnum = mp->hghmsg;
370 ** Check if we need to allocate more space for message status.
371 ** If so, then add space for an additional 100 messages.
373 if (msgnum >= mp->hghoff && !(mp = folder_realloc(mp, mp->lowoff, mp->hghoff + 100))) {
374 advise(NULL, "unable to allocate folder storage");
379 /* create scanline for new message */
380 switch (incerr = scan(in, msgnum + 1, msgnum + 1,
381 noisy ? fmtstr : NULL, width,
382 msgnum == hghnum && chgflag, 1)) {
389 fputs("inc aborted!\n", aud);
390 /* doesn't clean up locks! */
391 advise(NULL, "aborted!");
395 advise(NULL, "BUG in %s, number out of range",
400 advise(NULL, "BUG in %s, scan() botch (%d)",
406 ** Run the external program hook on the message.
409 snprintf(b, sizeof (b), "%s/%d", maildir_copy,
411 ext_hook("add-hook", b, NULL);
422 clear_msg_flags(mp, msgnum);
423 set_exists(mp, msgnum);
424 set_unseen(mp, msgnum);
425 mp->msgflags |= SEQMOD;
429 ** If we get here there was some sort of error from scan(),
430 ** so stop processing anything more from the spool.
436 if (incerr < 0) { /* error */
438 GETGROUPPRIVS(); /* Be sure we can unlock mail file */
439 lkfclose(in, newmail); in = NULL;
441 ** And then return us to normal
445 fclose(in); in = NULL;
447 adios(EX_SOFTWARE, NULL, "failed");
457 ** truncate file we are incorporating from
460 if (stat(newmail, &st) != NOTOK && s1.st_mtime != st.st_mtime)
461 advise(NULL, "new messages have arrived!\007");
464 if ((newfd = creat(newmail, 0600)) != NOTOK)
467 admonish(newmail, "error zero'ing");
470 printf("%s not zero'd\n", newmail);
473 if (msgnum == hghnum) {
474 admonish(NULL, "no messages incorporated");
476 context_replace(curfolder, folder); /* update current folder */
478 mp->curmsg = hghnum + 1;
482 if (chgflag) /* sigh... */
483 seq_setcur(mp, mp->curmsg);
487 ** unlock the mail spool
490 GETGROUPPRIVS(); /* Be sure we can unlock mail file */
491 lkfclose(in, newmail); in = NULL;
492 DROPGROUPPRIVS(); /* And then return us to normal privileges */
494 fclose(in); in = NULL;
497 seq_setunseen(mp, 1); /* add new msgs to unseen sequences */
498 seq_save(mp); /* synchronize sequences */
499 context_save(); /* save the context file */
508 lkfclose(in, newmail);