scan(): Replaced explicit noisy parameter with implicit nfs check.
[mmh] / uip / inc.c
1 /*
2 ** inc.c -- incorporate messages from a maildrop into a folder
3 **
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.
7 */
8
9 #ifdef MAILGROUP
10 /*
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.
16 **
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.
19 **
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
23 **
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
26 */
27 #endif
28
29 #include <h/mh.h>
30 #include <h/utils.h>
31 #include <fcntl.h>
32
33 #include <h/fmt_scan.h>
34 #include <h/scansbr.h>
35 #include <h/signals.h>
36 #include <h/tws.h>
37 #include <errno.h>
38 #include <signal.h>
39
40 static struct swit switches[] = {
41 #define AUDSW  0
42         { "audit audit-file", 0 },
43 #define NAUDSW  1
44         { "noaudit", 0 },
45 #define CHGSW  2
46         { "changecur", 0 },
47 #define NCHGSW  3
48         { "nochangecur", 0 },
49 #define FILESW  4
50         { "file name", 0 },
51 #define FORMSW  5
52         { "form formatfile", 0 },
53 #define SILSW  6
54         { "silent", 0 },
55 #define NSILSW  7
56         { "nosilent", 0 },
57 #define TRNCSW  8
58         { "truncate", 0 },
59 #define NTRNCSW  9
60         { "notruncate", 0 },
61 #define WIDTHSW  10
62         { "width columns", 0 },
63 #define VERSIONSW  11
64         { "version", 0 },
65 #define HELPSW  12
66         { "help", 0 },
67 };
68
69 /*
70 ** This is an attempt to simplify things by putting all the
71 ** privilege ops into macros.
72 ** *GROUPPRIVS() is related to handling the setgid MAIL property,
73 ** and only applies if MAILGROUP is defined.
74 ** Basically, SAVEGROUPPRIVS() is called right at the top of main()
75 ** to initialise things, and then DROPGROUPPRIVS() and GETGROUPPRIVS()
76 ** do the obvious thing. TRYDROPGROUPPRIVS() has to be safe to call
77 ** before DROPUSERPRIVS() is called [this is needed because setgid()
78 ** sets both effective and real uids if euid is root.]
79 **
80 ** There's probably a better implementation if we're allowed to use
81 ** BSD-style setreuid() rather than using POSIX saved-ids.
82 ** Anyway, if you're euid root it's a bit pointless to drop the group
83 ** permissions...
84 **
85 ** I'm pretty happy that the security is good provided we aren't setuid root.
86 ** The only things we trust with group=mail privilege are lkfopen()
87 ** and lkfclose().
88 */
89
90 /*
91 ** For setting and returning to "mail" gid
92 */
93 #ifdef MAILGROUP
94 static int return_gid;
95 /*
96 ** easy case; we're not setuid root, so can drop group privs immediately.
97 */
98 #define TRYDROPGROUPPRIVS() DROPGROUPPRIVS()
99 #define DROPGROUPPRIVS() setgid(getgid())
100 #define GETGROUPPRIVS() setgid(return_gid)
101 #define SAVEGROUPPRIVS() return_gid = getegid()
102 #else
103 /* define *GROUPPRIVS() as null; this avoids having lots of "#ifdef MAILGROUP"s */
104 #define TRYDROPGROUPPRIVS()
105 #define DROPGROUPPRIVS()
106 #define GETGROUPPRIVS()
107 #define SAVEGROUPPRIVS()
108 #endif /* not MAILGROUP */
109
110 /*
111 ** these variables have to be globals so that done() can correctly clean
112 ** up the lockfile
113 */
114 static int locked = 0;
115 static char *newmail;
116 static FILE *in;
117
118 /*
119 ** prototypes
120 */
121 static void inc_done(int) NORETURN;
122
123
124 int
125 main(int argc, char **argv)
126 {
127         int chgflag = 1, trnflag = 1;
128         int noisy = 1, width = 0;
129         int hghnum = 0, msgnum = 0;
130         int incerr = 0;  /*
131                         ** <0 if inc hits an error which means it should
132                         ** not truncate mailspool
133                         */
134         char *cp, *maildir = NULL, *folder = NULL;
135         char *form = NULL;
136         char *audfile = NULL, *from = NULL;
137         char buf[BUFSIZ], **argp, *nfs, **arguments;
138         struct msgs *mp = NULL;
139         struct stat st, s1;
140         FILE *aud = NULL;
141         char b[MAXPATHLEN + 1];
142         /* copy of mail directory because the static gets overwritten */
143         char *maildir_copy = NULL;
144
145 #ifdef MHE
146         FILE *mhe = NULL;
147 #endif
148
149         done=inc_done;
150
151 /*
152 ** absolutely the first thing we do is save our privileges,
153 ** and drop them if we can.
154 */
155         SAVEGROUPPRIVS();
156         TRYDROPGROUPPRIVS();
157
158 #ifdef LOCALE
159         setlocale(LC_ALL, "");
160 #endif
161         invo_name = mhbasename(argv[0]);
162
163         /* read user profile/context */
164         context_read();
165
166         arguments = getarguments(invo_name, argc, argv, 1);
167         argp = arguments;
168
169         while ((cp = *argp++)) {
170                 if (*cp == '-') {
171                         switch (smatch(++cp, switches)) {
172                         case AMBIGSW:
173                                 ambigsw(cp, switches);
174                                 done(1);
175                         case UNKWNSW:
176                                 adios(NULL, "-%s unknown", cp);
177
178                         case HELPSW:
179                                 snprintf(buf, sizeof(buf), "%s [+folder] [switches]", invo_name);
180                                 print_help(buf, switches, 1);
181                                 done(1);
182                         case VERSIONSW:
183                                 print_version(invo_name);
184                                 done(1);
185
186                         case AUDSW:
187                                 if (!(cp = *argp++) || *cp == '-')
188                                         adios(NULL, "missing argument to %s", argp[-2]);
189                                 audfile = getcpy(expanddir(cp));
190                                 continue;
191                         case NAUDSW:
192                                 audfile = NULL;
193                                 continue;
194
195                         case CHGSW:
196                                 chgflag++;
197                                 continue;
198                         case NCHGSW:
199                                 chgflag = 0;
200                                 continue;
201
202                         /*
203                         ** The flag `trnflag' has the value:
204                         **
205                         ** 2 if -truncate is given
206                         ** 1 by default (truncating is default)
207                         ** 0 if -notruncate is given
208                         */
209                         case TRNCSW:
210                                 trnflag = 2;
211                                 continue;
212                         case NTRNCSW:
213                                 trnflag = 0;
214                                 continue;
215
216                         case FILESW:
217                                 if (!(cp = *argp++) || *cp == '-')
218                                         adios(NULL, "missing argument to %s",
219                                                         argp[-2]);
220                                 from = getcpy(expanddir(cp));
221
222                                 /*
223                                 ** If the truncate file is in default state,
224                                 ** change to not truncate.
225                                 */
226                                 if (trnflag == 1)
227                                         trnflag = 0;
228                                 continue;
229
230                         case SILSW:
231                                 noisy = 0;
232                                 continue;
233                         case NSILSW:
234                                 noisy++;
235                                 continue;
236
237                         case FORMSW:
238                                 if (!(form = *argp++) || *form == '-')
239                                         adios(NULL, "missing argument to %s",
240                                                         argp[-2]);
241                                 continue;
242
243                         case WIDTHSW:
244                                 if (!(cp = *argp++) || *cp == '-')
245                                         adios(NULL, "missing argument to %s",
246                                                         argp[-2]);
247                                 width = atoi(cp);
248                                 continue;
249                         }
250                 }
251                 if (*cp == '+' || *cp == '@') {
252                         if (folder)
253                                 adios(NULL, "only one folder at a time!");
254                         else
255                                 folder = getcpy(expandfol(cp));
256                 } else {
257                         adios(NULL, "usage: %s [+folder] [switches]",
258                                         invo_name);
259                 }
260         }
261
262         /*
263         ** NOTE: above this point you should use TRYDROPGROUPPRIVS(),
264         ** not DROPGROUPPRIVS().
265         */
266         /* guarantee dropping group priveleges; we might not have done so earlier */
267         DROPGROUPPRIVS();
268
269         /*
270         ** We will get the mail from a file
271         ** (typically the standard maildrop)
272         */
273         if (from)
274                 newmail = from;
275         else if ((newmail = getenv("MAILDROP")) && *newmail)
276                 newmail = toabsdir(newmail);
277         else if ((newmail = context_find("maildrop")) && *newmail)
278                 newmail = toabsdir(newmail);
279         else {
280                 newmail = concat(mailspool, "/", getusername(), NULL);
281         }
282         if (stat(newmail, &s1) == NOTOK || s1.st_size == 0)
283                 adios(NULL, "no mail to incorporate");
284
285         if ((cp = strdup(newmail)) == NULL)
286                 adios(NULL, "error allocating memory to copy newmail");
287
288         newmail = cp;
289
290         if (!folder)
291                 folder = getdeffol();
292         maildir = toabsdir(folder);
293
294         if ((maildir_copy = strdup(maildir)) == NULL)
295                 adios(maildir, "error allocating memory to copy maildir");
296
297         if (!folder_exists(maildir)) {
298                 /*
299                 ** If the folder doesn't exist, and we're given the -silent
300                 ** flag, just fail.
301                 */
302                 if (noisy)
303                         create_folder(maildir, 0, done);
304                 else
305                         done(1);
306         }
307
308         if (chdir(maildir) == NOTOK)
309                 adios(maildir, "unable to change directory to");
310
311         /* read folder and create message structure */
312         if (!(mp = folder_read(folder)))
313                 adios(NULL, "unable to read folder %s", folder);
314
315         if (access(newmail, W_OK) != NOTOK) {
316                 locked++;
317                 if (trnflag) {
318                         SIGNAL(SIGHUP, SIG_IGN);
319                         SIGNAL(SIGINT, SIG_IGN);
320                         SIGNAL(SIGQUIT, SIG_IGN);
321                         SIGNAL(SIGTERM, SIG_IGN);
322                 }
323
324                 GETGROUPPRIVS();  /* Reset gid to lock mail file */
325                 in = lkfopen(newmail, "r");
326                 DROPGROUPPRIVS();
327                 if (in == NULL)
328                         adios(NULL, "unable to lock and fopen %s", newmail);
329                 fstat(fileno(in), &s1);
330         } else {
331                 trnflag = 0;
332                 if ((in = fopen(newmail, "r")) == NULL)
333                         adios(newmail, "unable to read");
334         }
335
336         /* This shouldn't be necessary but it can't hurt. */
337         DROPGROUPPRIVS();
338
339         if (audfile) {
340                 int i;
341                 if ((i = stat(audfile, &st)) == NOTOK)
342                         advise(NULL, "Creating Receive-Audit: %s", audfile);
343                 if ((aud = fopen(audfile, "a")) == NULL)
344                         adios(audfile, "unable to append to");
345                 else if (i == NOTOK)
346                         chmod(audfile, m_gmprot());
347
348                 fprintf(aud, from ? "<<inc>> %s  -ms %s\n" : "<<inc>> %s\n",
349                          dtimenow(0), from);
350         }
351
352 #ifdef MHE
353         if (context_find("mhe")) {
354                 int i;
355                 cp = concat(maildir, "/++", NULL);
356                 i = stat(cp, &st);
357                 if ((mhe = fopen(cp, "a")) == NULL)
358                         admonish(cp, "unable to append to");
359                 else
360                         if (i == NOTOK)
361                                 chmod(cp, m_gmprot());
362                 free(cp);
363         }
364 #endif /* MHE */
365
366         /* Get new format string */
367         nfs = new_fs(form, FORMAT);
368
369         if (noisy) {
370                 printf("Incorporating new mail into %s...\n\n", folder);
371                 fflush(stdout);
372         }
373
374         /*
375         ** Get the mail from file (usually mail spool)
376         */
377         m_unknown(in);  /* the MAGIC invocation... */
378         hghnum = msgnum = mp->hghmsg;
379         for (;;) {
380                 /*
381                 ** Check if we need to allocate more space for message status.
382                 ** If so, then add space for an additional 100 messages.
383                 */
384                 if (msgnum >= mp->hghoff && !(mp = folder_realloc(mp, mp->lowoff, mp->hghoff + 100))) {
385                         advise(NULL, "unable to allocate folder storage");
386                         incerr = NOTOK;
387                         break;
388                 }
389
390                 /* create scanline for new message */
391                 switch (incerr = scan(in, msgnum + 1, msgnum + 1,
392                                 noisy ? nfs : NULL, width,
393                                 msgnum == hghnum && chgflag, 1)) {
394                 case SCNFAT:
395                 case SCNEOF:
396                         break;
397
398                 case SCNERR:
399                         if (aud)
400                                 fputs("inc aborted!\n", aud);
401                         /* doesn't clean up locks! */
402                         advise(NULL, "aborted!");
403                         break;
404
405                 case SCNNUM:
406                         advise(NULL, "BUG in %s, number out of range",
407                                         invo_name);
408                         break;
409
410                 default:
411                         advise(NULL, "BUG in %s, scan() botch (%d)",
412                                         invo_name, incerr);
413                         break;
414
415                 case SCNMSG:
416                         /*
417                         **  Run the external program hook on the message.
418                         */
419
420                         snprintf(b, sizeof (b), "%s/%d", maildir_copy,
421                                         msgnum + 1);
422                         ext_hook("add-hook", b, NULL);
423
424                         if (aud)
425                                 fputs(scanl, aud);
426 #ifdef MHE
427                         if (mhe)
428                                 fputs(scanl, mhe);
429 #endif /* MHE */
430                         if (noisy)
431                                 fflush(stdout);
432                         msgnum++;
433                         mp->hghmsg++;
434                         mp->nummsg++;
435                         if (mp->lowmsg == 0)
436                                 mp->lowmsg = 1;
437                         clear_msg_flags(mp, msgnum);
438                         set_exists(mp, msgnum);
439                         set_unseen(mp, msgnum);
440                         mp->msgflags |= SEQMOD;
441                         continue;
442                 }
443                 /*
444                 ** If we get here there was some sort of error from scan(),
445                 ** so stop processing anything more from the spool.
446                 */
447                 break;
448         }
449
450         if (incerr < 0) {  /* error */
451                 if (locked) {
452                         GETGROUPPRIVS();  /* Be sure we can unlock mail file */
453                         lkfclose(in, newmail); in = NULL;
454                         DROPGROUPPRIVS();  /*
455                                         ** And then return us to normal
456                                         ** privileges
457                                         */
458                 } else {
459                         fclose(in); in = NULL;
460                 }
461                 adios(NULL, "failed");
462         }
463
464         if (aud)
465                 fclose(aud);
466
467 #ifdef MHE
468         if (mhe)
469                 fclose(mhe);
470 #endif /* MHE */
471
472         if (noisy)
473                 fflush(stdout);
474
475         /*
476         ** truncate file we are incorporating from
477         */
478         if (trnflag) {
479                 if (stat(newmail, &st) != NOTOK && s1.st_mtime != st.st_mtime)
480                         advise(NULL, "new messages have arrived!\007");
481                 else {
482                         int newfd;
483                         if ((newfd = creat(newmail, 0600)) != NOTOK)
484                                 close(newfd);
485                         else
486                                 admonish(newmail, "error zero'ing");
487                 }
488         } else if (noisy) {
489                 printf("%s not zero'd\n", newmail);
490         }
491
492         if (msgnum == hghnum) {
493                 admonish(NULL, "no messages incorporated");
494         } else {
495                 context_replace(curfolder, folder); /* update current folder */
496                 if (chgflag)
497                         mp->curmsg = hghnum + 1;
498                 mp->hghmsg = msgnum;
499                 if (mp->lowmsg == 0)
500                         mp->lowmsg = 1;
501                 if (chgflag)  /* sigh... */
502                         seq_setcur(mp, mp->curmsg);
503         }
504
505         /*
506         ** unlock the mail spool
507         */
508         if (locked) {
509                 GETGROUPPRIVS();  /* Be sure we can unlock mail file */
510                 lkfclose(in, newmail); in = NULL;
511                 DROPGROUPPRIVS();  /* And then return us to normal privileges */
512         } else {
513                 fclose(in); in = NULL;
514         }
515
516         seq_setunseen(mp, 1);  /* add new msgs to unseen sequences */
517         seq_save(mp);  /* synchronize sequences   */
518         context_save();  /* save the context file   */
519         done(0);
520         return 1;
521 }
522
523 static void
524 inc_done(int status)
525 {
526         if (locked) {
527                 GETGROUPPRIVS();
528                 lkfclose(in, newmail);
529                 DROPGROUPPRIVS();
530         }
531         exit(status);
532 }