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