3 * inc.c -- incorporate messages from a maildrop into a folder
5 * This code is Copyright (c) 2002, by the authors of nmh. See the
6 * COPYRIGHT file in the root directory of the nmh distribution for
7 * 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 },
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;
97 /* easy case; we're not setuid root, so can drop group privs
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 */
112 /* these variables have to be globals so that done() can correctly clean up the lockfile */
113 static int locked = 0;
114 static char *newmail;
120 char *map_name(char *);
122 static void inc_done(int) NORETURN;
126 main (int argc, char **argv)
128 int chgflag = 1, trnflag = 1;
129 int noisy = 1, width = 0;
130 int hghnum = 0, msgnum = 0;
131 int incerr = 0; /* <0 if inc hits an error which means it should not truncate mailspool */
132 char *cp, *maildir = NULL, *folder = NULL;
133 char *format = NULL, *form = NULL;
134 char *audfile = NULL, *from = NULL;
135 char buf[BUFSIZ], **argp, *nfs, **arguments;
136 struct msgs *mp = NULL;
139 char b[MAXPATHLEN + 1];
140 char *maildir_copy = NULL; /* copy of mail directory because the static gets overwritten */
148 /* absolutely the first thing we do is save our privileges,
149 * and drop them if we can.
155 setlocale(LC_ALL, "");
157 invo_name = r1bindex (argv[0], '/');
159 /* read user profile/context */
162 mts_init (invo_name);
163 arguments = getarguments (invo_name, argc, argv, 1);
166 while ((cp = *argp++)) {
168 switch (smatch (++cp, switches)) {
170 ambigsw (cp, switches);
173 adios (NULL, "-%s unknown", cp);
176 snprintf (buf, sizeof(buf), "%s [+folder] [switches]", invo_name);
177 print_help (buf, switches, 1);
180 print_version(invo_name);
184 if (!(cp = *argp++) || *cp == '-')
185 adios (NULL, "missing argument to %s", argp[-2]);
186 audfile = getcpy (m_maildir (cp));
200 * The flag `trnflag' has the value:
202 * 2 if -truncate is given
203 * 1 by default (truncating is default)
204 * 0 if -notruncate is given
214 if (!(cp = *argp++) || *cp == '-')
215 adios (NULL, "missing argument to %s", argp[-2]);
216 from = path (cp, TFILE);
219 * If the truncate file is in default state,
220 * change to not truncate.
234 if (!(form = *argp++) || *form == '-')
235 adios (NULL, "missing argument to %s", argp[-2]);
239 if (!(format = *argp++) || *format == '-')
240 adios (NULL, "missing argument to %s", argp[-2]);
245 if (!(cp = *argp++) || *cp == '-')
246 adios (NULL, "missing argument to %s", argp[-2]);
251 if (*cp == '+' || *cp == '@') {
253 adios (NULL, "only one folder at a time!");
255 folder = pluspath (cp);
257 adios (NULL, "usage: %s [+folder] [switches]", invo_name);
261 /* NOTE: above this point you should use TRYDROPGROUPPRIVS(),
262 * not DROPGROUPPRIVS().
264 /* guarantee dropping group priveleges; we might not have done so earlier */
268 * We will get the mail from a file
269 * (typically the standard maildrop)
273 else if ((newmail = getenv ("MAILDROP")) && *newmail)
274 newmail = m_mailpath (newmail);
275 else if ((newmail = context_find ("maildrop")) && *newmail)
276 newmail = m_mailpath (newmail);
278 newmail = concat (MAILDIR, "/", MAILFIL, NULL);
280 if (stat (newmail, &s1) == NOTOK || s1.st_size == 0)
281 adios (NULL, "no mail to incorporate");
283 if ((cp = strdup(newmail)) == (char *)0)
284 adios (NULL, "error allocating memory to copy newmail");
288 if (!context_find ("path"))
289 free (path ("./", TFOLDER));
291 folder = getfolder (0);
292 maildir = m_maildir (folder);
294 if ((maildir_copy = strdup(maildir)) == (char *)0)
295 adios (maildir, "error allocating memory to copy maildir");
297 if (!folder_exists(maildir)) {
298 /* If the folder doesn't exist, and we're given the -silent flag,
302 create_folder(maildir, 0, done);
307 if (chdir (maildir) == NOTOK)
308 adios (maildir, "unable to change directory to");
310 /* read folder and create message structure */
311 if (!(mp = folder_read (folder)))
312 adios (NULL, "unable to read folder %s", folder);
314 if (access (newmail, W_OK) != NOTOK) {
317 SIGNAL (SIGHUP, SIG_IGN);
318 SIGNAL (SIGINT, SIG_IGN);
319 SIGNAL (SIGQUIT, SIG_IGN);
320 SIGNAL (SIGTERM, SIG_IGN);
323 GETGROUPPRIVS(); /* Reset gid to lock mail file */
324 in = lkfopen (newmail, "r");
327 adios (NULL, "unable to lock and fopen %s", newmail);
328 fstat (fileno(in), &s1);
331 if ((in = fopen (newmail, "r")) == NULL)
332 adios (newmail, "unable to read");
335 /* This shouldn't be necessary but it can't hurt. */
340 if ((i = stat (audfile, &st)) == NOTOK)
341 advise (NULL, "Creating Receive-Audit: %s", audfile);
342 if ((aud = fopen (audfile, "a")) == NULL)
343 adios (audfile, "unable to append to");
345 chmod (audfile, m_gmprot ());
347 fprintf (aud, from ? "<<inc>> %s -ms %s\n" : "<<inc>> %s\n",
352 if (context_find ("mhe")) {
354 cp = concat (maildir, "/++", NULL);
356 if ((mhe = fopen (cp, "a")) == NULL)
357 admonish (cp, "unable to append to");
360 chmod (cp, m_gmprot ());
365 /* Get new format string */
366 nfs = new_fs (form, format, FORMAT);
369 printf ("Incorporating new mail into %s...\n\n", folder);
374 * Get the mail from file (usually mail spool)
376 m_unknown (in); /* the MAGIC invocation... */
377 hghnum = msgnum = mp->hghmsg;
380 * Check if we need to allocate more space for message status.
381 * If so, then add space for an additional 100 messages.
383 if (msgnum >= mp->hghoff
384 && !(mp = folder_realloc (mp, mp->lowoff, mp->hghoff + 100))) {
385 advise (NULL, "unable to allocate folder storage");
390 /* create scanline for new message */
391 switch (incerr = scan (in, msgnum + 1, msgnum + 1, nfs, width,
392 msgnum == hghnum && chgflag, 1, NULL, 0L, noisy)) {
399 fputs ("inc aborted!\n", aud);
400 advise (NULL, "aborted!"); /* doesn't clean up locks! */
404 advise (NULL, "BUG in %s, number out of range", invo_name);
408 advise (NULL, "BUG in %s, scan() botch (%d)", invo_name, incerr);
414 * Run the external program hook on the message.
417 (void)snprintf(b, sizeof (b), "%s/%d", maildir_copy, msgnum + 1);
418 (void)ext_hook("add-hook", b, (char *)0);
433 clear_msg_flags (mp, msgnum);
434 set_exists (mp, msgnum);
435 set_unseen (mp, msgnum);
436 mp->msgflags |= SEQMOD;
439 /* If we get here there was some sort of error from scan(),
440 * so stop processing anything more from the spool.
445 if (incerr < 0) { /* error */
447 GETGROUPPRIVS(); /* Be sure we can unlock mail file */
448 (void) lkfclose (in, newmail); in = NULL;
449 DROPGROUPPRIVS(); /* And then return us to normal privileges */
451 fclose (in); in = NULL;
453 adios (NULL, "failed");
468 * truncate file we are incorporating from
471 if (stat (newmail, &st) != NOTOK && s1.st_mtime != st.st_mtime)
472 advise (NULL, "new messages have arrived!\007");
475 if ((newfd = creat (newmail, 0600)) != NOTOK)
478 admonish (newmail, "error zero'ing");
479 unlink(map_name(newmail));
483 printf ("%s not zero'd\n", newmail);
486 if (msgnum == hghnum) {
487 admonish (NULL, "no messages incorporated");
489 context_replace (pfolder, folder); /* update current folder */
491 mp->curmsg = hghnum + 1;
495 if (chgflag) /* sigh... */
496 seq_setcur (mp, mp->curmsg);
500 * unlock the mail spool
503 GETGROUPPRIVS(); /* Be sure we can unlock mail file */
504 (void) lkfclose (in, newmail); in = NULL;
505 DROPGROUPPRIVS(); /* And then return us to normal privileges */
507 fclose (in); in = NULL;
510 seq_setunseen (mp, 0); /* set the Unseen-Sequence */
511 seq_save (mp); /* synchronize sequences */
512 context_save (); /* save the context file */
518 inc_done (int status)
523 lkfclose(in, newmail);