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]);
177 arguments = getarguments(invo_name, argc, argv, 1);
180 while ((cp = *argp++)) {
182 switch (smatch(++cp, switches)) {
184 ambigsw(cp, switches);
187 adios(EX_USAGE, NULL, "-%s unknown", cp);
190 snprintf(buf, sizeof(buf), "%s [+folder] [switches]", invo_name);
191 print_help(buf, switches, 1);
192 exit(argc == 2 ? EX_OK : EX_USAGE);
194 print_version(invo_name);
195 exit(argc == 2 ? EX_OK : EX_USAGE);
198 if (!(cp = *argp++) || *cp == '-')
199 adios(EX_USAGE, NULL, "missing argument to %s", argp[-2]);
200 audfile = mh_xstrdup(expanddir(cp));
214 ** The flag `trnflag' has the value:
216 ** 2 if -truncate is given
217 ** 1 by default (truncating is default)
218 ** 0 if -notruncate is given
229 adios(EX_USAGE, NULL, "missing argument to %s",
231 from = (strcmp(cp, "-")==0) ? "-" : mh_xstrdup(expanddir(cp));
234 ** If the truncate file is in default state,
235 ** change to not truncate.
249 if (!(form = *argp++) || *form == '-')
250 adios(EX_USAGE, NULL, "missing argument to %s",
255 if (!(cp = *argp++) || *cp == '-')
256 adios(EX_USAGE, NULL, "missing argument to %s",
262 if (*cp == '+' || *cp == '@') {
264 adios(EX_USAGE, NULL, "only one folder at a time!");
266 folder = mh_xstrdup(expandfol(cp));
268 adios(EX_USAGE, NULL, "usage: %s [+folder] [switches]",
274 ** NOTE: above this point you should use TRYDROPGROUPPRIVS(),
275 ** not DROPGROUPPRIVS().
277 /* guarantee dropping group priveleges; we might not have done so earlier */
280 if (from && strcmp(from, "-")==0) {
281 /* We'll read mail from stdin. */
284 /* We'll read mail from a file. */
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");
304 folder = getdeffol();
305 maildir = toabsdir(folder);
307 if ((maildir_copy = strdup(maildir)) == NULL)
308 adios(EX_OSERR, maildir, "error allocating memory to copy maildir");
310 create_folder(maildir, noisy ? 0 : 1, exit);
312 if (chdir(maildir) == NOTOK)
313 adios(EX_OSERR, maildir, "unable to change directory to");
315 if (!(mp = folder_read(folder)))
316 adios(EX_IOERR, NULL, "unable to read folder %s", folder);
321 } else if (access(newmail, W_OK) != NOTOK) {
324 SIGNAL(SIGHUP, SIG_IGN);
325 SIGNAL(SIGINT, SIG_IGN);
326 SIGNAL(SIGQUIT, SIG_IGN);
327 SIGNAL(SIGTERM, SIG_IGN);
330 GETGROUPPRIVS(); /* Reset gid to lock mail file */
331 in = lkfopen(newmail, "r");
334 adios(EX_IOERR, NULL, "unable to lock and fopen %s", newmail);
335 fstat(fileno(in), &s1);
338 if ((in = fopen(newmail, "r")) == NULL)
339 adios(EX_IOERR, newmail, "unable to read");
342 /* This shouldn't be necessary but it can't hurt. */
347 if ((i = stat(audfile, &st)) == NOTOK)
348 advise(NULL, "Creating Receive-Audit: %s", audfile);
349 if ((aud = fopen(audfile, "a")) == NULL)
350 adios(EX_IOERR, audfile, "unable to append to");
352 chmod(audfile, m_gmprot());
354 fprintf(aud, from ? "<<inc>> %s -ms %s\n" : "<<inc>> %s\n",
358 /* Set format string */
359 fmtstr = new_fs(form, scanformat);
362 printf("Incorporating new mail into %s...\n\n", folder);
366 /* check if readable and nonempty */
367 if (!fgets(buf, sizeof(buf), in)) {
369 advise("read", "unable to");
376 if (strncmp("From ", buf, 5)!=0) {
377 advise(NULL, "not in mbox format");
383 ** Get the mail from file (usually mail spool)
385 hghnum = msgnum = mp->hghmsg;
388 ** Check if we need to allocate more space for message status.
389 ** If so, then add space for an additional 100 messages.
391 if (msgnum >= mp->hghoff && !(mp = folder_realloc(mp, mp->lowoff, mp->hghoff + 100))) {
392 advise(NULL, "unable to allocate folder storage");
397 /* create scanline for new message */
398 switch (incerr = scan(in, msgnum + 1, msgnum + 1,
399 noisy ? fmtstr : NULL, width,
400 msgnum == hghnum && chgflag, 1)) {
407 fputs("inc aborted!\n", aud);
408 /* doesn't clean up locks! */
409 advise(NULL, "aborted!");
413 advise(NULL, "BUG in %s, number out of range",
418 advise(NULL, "BUG in %s, scan() botch (%d)",
424 ** Run the external program hook on the message.
427 snprintf(b, sizeof (b), "%s/%d", maildir_copy,
429 ext_hook("add-hook", b, NULL);
440 clear_msg_flags(mp, msgnum);
441 set_exists(mp, msgnum);
442 set_unseen(mp, msgnum);
443 mp->msgflags |= SEQMOD;
447 ** If we get here there was some sort of error from scan(),
448 ** so stop processing anything more from the spool.
453 mh_free0(&maildir_copy);
455 if (incerr < 0) { /* error */
457 GETGROUPPRIVS(); /* Be sure we can unlock mail file */
458 lkfclose(in, newmail); in = NULL;
460 ** And then return us to normal
464 fclose(in); in = NULL;
466 adios(EX_SOFTWARE, NULL, "failed");
476 ** truncate file we are incorporating from
479 if (stat(newmail, &st) != NOTOK && s1.st_mtime != st.st_mtime)
480 advise(NULL, "new messages have arrived!\007");
483 if ((newfd = creat(newmail, 0600)) != NOTOK)
486 admonish(newmail, "error zero'ing");
488 } else if (noisy && newmail) {
489 printf("%s not zero'd\n", newmail);
492 if (msgnum == hghnum) {
493 admonish(NULL, "no messages incorporated");
495 context_replace(curfolder, folder); /* update current folder */
497 mp->curmsg = hghnum + 1;
501 if (chgflag) /* sigh... */
502 seq_setcur(mp, mp->curmsg);
506 ** unlock the mail spool
509 GETGROUPPRIVS(); /* Be sure we can unlock mail file */
510 lkfclose(in, newmail); in = NULL;
511 DROPGROUPPRIVS(); /* And then return us to normal privileges */
513 fclose(in); in = NULL;
516 seq_setunseen(mp, 1);
527 lkfclose(in, newmail);