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 },
71 ** This is an attempt to simplify things by putting all the
72 ** privilege ops into macros.
73 ** *GROUPPRIVS() is related to handling the setgid MAIL property,
74 ** and only applies if MAILGROUP is defined.
75 ** Basically, SAVEGROUPPRIVS() is called right at the top of main()
76 ** to initialise things, and then DROPGROUPPRIVS() and GETGROUPPRIVS()
77 ** do the obvious thing. TRYDROPGROUPPRIVS() has to be safe to call
78 ** before DROPUSERPRIVS() is called [this is needed because setgid()
79 ** sets both effective and real uids if euid is root.]
81 ** There's probably a better implementation if we're allowed to use
82 ** BSD-style setreuid() rather than using POSIX saved-ids.
83 ** Anyway, if you're euid root it's a bit pointless to drop the group
86 ** I'm pretty happy that the security is good provided we aren't setuid root.
87 ** The only things we trust with group=mail privilege are lkfopen()
92 ** For setting and returning to "mail" gid
95 static int return_gid;
97 ** easy case; we're not setuid root, so can drop group privs immediately.
99 #define TRYDROPGROUPPRIVS() DROPGROUPPRIVS()
100 #define DROPGROUPPRIVS() \
101 if (setegid(getgid()) != 0) { \
102 advise ("setegid", "unable to set group to %ld", (long) getgid()); \
105 #define GETGROUPPRIVS() \
106 if (setegid(return_gid) != 0) { \
107 advise ("setegid", "unable to set group to %ld", (long) return_gid); \
110 #define SAVEGROUPPRIVS() return_gid = getegid()
112 /* define *GROUPPRIVS() as null; this avoids having lots of "#ifdef MAILGROUP"s */
113 #define TRYDROPGROUPPRIVS()
114 #define DROPGROUPPRIVS()
115 #define GETGROUPPRIVS()
116 #define SAVEGROUPPRIVS()
117 #endif /* not MAILGROUP */
120 ** these variables have to be globals so that done() can correctly clean
123 static int locked = 0;
124 static char *newmail;
134 main(int argc, char **argv)
136 int chgflag = 1, trnflag = 1;
137 int noisy = 1, width = 0;
138 int hghnum = 0, msgnum = 0;
140 ** <0 if inc hits an error which means it should
141 ** not truncate mailspool
143 char *cp, *maildir = NULL, *folder = NULL;
145 char *audfile = NULL, *from = NULL;
146 char buf[BUFSIZ], **argp, *fmtstr, **arguments;
147 struct msgs *mp = NULL;
150 char b[MAXPATHLEN + 1];
151 /* copy of mail directory because the static gets overwritten */
152 char *maildir_copy = NULL;
154 if (atexit(inc_done) != 0) {
155 adios(NULL, "atexit failed");
159 ** absolutely the first thing we do is save our privileges,
160 ** and drop them if we can.
165 setlocale(LC_ALL, "");
166 invo_name = mhbasename(argv[0]);
168 /* read user profile/context */
171 arguments = getarguments(invo_name, argc, argv, 1);
174 while ((cp = *argp++)) {
176 switch (smatch(++cp, switches)) {
178 ambigsw(cp, switches);
179 /* sysexits.h EX_USAGE */
182 adios(NULL, "-%s unknown", cp);
185 snprintf(buf, sizeof(buf), "%s [+folder] [switches]", invo_name);
186 print_help(buf, switches, 1);
189 print_version(invo_name);
193 if (!(cp = *argp++) || *cp == '-')
194 adios(NULL, "missing argument to %s", argp[-2]);
195 audfile = getcpy(expanddir(cp));
209 ** The flag `trnflag' has the value:
211 ** 2 if -truncate is given
212 ** 1 by default (truncating is default)
213 ** 0 if -notruncate is given
223 if (!(cp = *argp++) || *cp == '-')
224 adios(NULL, "missing argument to %s",
226 from = getcpy(expanddir(cp));
229 ** If the truncate file is in default state,
230 ** change to not truncate.
244 if (!(form = *argp++) || *form == '-')
245 adios(NULL, "missing argument to %s",
250 if (!(cp = *argp++) || *cp == '-')
251 adios(NULL, "missing argument to %s",
257 if (*cp == '+' || *cp == '@') {
259 adios(NULL, "only one folder at a time!");
261 folder = getcpy(expandfol(cp));
263 adios(NULL, "usage: %s [+folder] [switches]",
269 ** NOTE: above this point you should use TRYDROPGROUPPRIVS(),
270 ** not DROPGROUPPRIVS().
272 /* guarantee dropping group priveleges; we might not have done so earlier */
276 ** We will get the mail from a file
277 ** (typically the standard maildrop)
281 else if ((newmail = getenv("MAILDROP")) && *newmail)
282 newmail = toabsdir(newmail);
283 else if ((newmail = context_find("maildrop")) && *newmail)
284 newmail = toabsdir(newmail);
286 newmail = concat(mailspool, "/", getusername(), NULL);
288 if (stat(newmail, &s1) == NOTOK || s1.st_size == 0)
289 adios(NULL, "no mail to incorporate");
291 if ((cp = strdup(newmail)) == NULL)
292 adios(NULL, "error allocating memory to copy newmail");
297 folder = getdeffol();
298 maildir = toabsdir(folder);
300 if ((maildir_copy = strdup(maildir)) == NULL)
301 adios(maildir, "error allocating memory to copy maildir");
303 create_folder(maildir, noisy ? 0 : 1, exit);
305 if (chdir(maildir) == NOTOK)
306 adios(maildir, "unable to change directory to");
308 /* read folder and create message structure */
309 if (!(mp = folder_read(folder)))
310 adios(NULL, "unable to read folder %s", folder);
312 if (access(newmail, W_OK) != NOTOK) {
315 SIGNAL(SIGHUP, SIG_IGN);
316 SIGNAL(SIGINT, SIG_IGN);
317 SIGNAL(SIGQUIT, SIG_IGN);
318 SIGNAL(SIGTERM, SIG_IGN);
321 GETGROUPPRIVS(); /* Reset gid to lock mail file */
322 in = lkfopen(newmail, "r");
325 adios(NULL, "unable to lock and fopen %s", newmail);
326 fstat(fileno(in), &s1);
329 if ((in = fopen(newmail, "r")) == NULL)
330 adios(newmail, "unable to read");
333 /* This shouldn't be necessary but it can't hurt. */
338 if ((i = stat(audfile, &st)) == NOTOK)
339 advise(NULL, "Creating Receive-Audit: %s", audfile);
340 if ((aud = fopen(audfile, "a")) == NULL)
341 adios(audfile, "unable to append to");
343 chmod(audfile, m_gmprot());
345 fprintf(aud, from ? "<<inc>> %s -ms %s\n" : "<<inc>> %s\n",
349 /* Get new format string */
350 fmtstr = new_fs(form, FORMAT);
353 printf("Incorporating new mail into %s...\n\n", folder);
358 ** Get the mail from file (usually mail spool)
361 hghnum = msgnum = mp->hghmsg;
364 ** Check if we need to allocate more space for message status.
365 ** If so, then add space for an additional 100 messages.
367 if (msgnum >= mp->hghoff && !(mp = folder_realloc(mp, mp->lowoff, mp->hghoff + 100))) {
368 advise(NULL, "unable to allocate folder storage");
373 /* create scanline for new message */
374 switch (incerr = scan(in, msgnum + 1, msgnum + 1,
375 noisy ? fmtstr : NULL, width,
376 msgnum == hghnum && chgflag, 1)) {
383 fputs("inc aborted!\n", aud);
384 /* doesn't clean up locks! */
385 advise(NULL, "aborted!");
389 advise(NULL, "BUG in %s, number out of range",
394 advise(NULL, "BUG in %s, scan() botch (%d)",
400 ** Run the external program hook on the message.
403 snprintf(b, sizeof (b), "%s/%d", maildir_copy,
405 ext_hook("add-hook", b, NULL);
416 clear_msg_flags(mp, msgnum);
417 set_exists(mp, msgnum);
418 set_unseen(mp, msgnum);
419 mp->msgflags |= SEQMOD;
423 ** If we get here there was some sort of error from scan(),
424 ** so stop processing anything more from the spool.
430 if (incerr < 0) { /* error */
432 GETGROUPPRIVS(); /* Be sure we can unlock mail file */
433 lkfclose(in, newmail); in = NULL;
435 ** And then return us to normal
439 fclose(in); in = NULL;
441 adios(NULL, "failed");
451 ** truncate file we are incorporating from
454 if (stat(newmail, &st) != NOTOK && s1.st_mtime != st.st_mtime)
455 advise(NULL, "new messages have arrived!\007");
458 if ((newfd = creat(newmail, 0600)) != NOTOK)
461 admonish(newmail, "error zero'ing");
464 printf("%s not zero'd\n", newmail);
467 if (msgnum == hghnum) {
468 admonish(NULL, "no messages incorporated");
470 context_replace(curfolder, folder); /* update current folder */
472 mp->curmsg = hghnum + 1;
476 if (chgflag) /* sigh... */
477 seq_setcur(mp, mp->curmsg);
481 ** unlock the mail spool
484 GETGROUPPRIVS(); /* Be sure we can unlock mail file */
485 lkfclose(in, newmail); in = NULL;
486 DROPGROUPPRIVS(); /* And then return us to normal privileges */
488 fclose(in); in = NULL;
491 seq_setunseen(mp, 1); /* add new msgs to unseen sequences */
492 seq_save(mp); /* synchronize sequences */
493 context_save(); /* save the context file */
502 lkfclose(in, newmail);