We have only numeric timezone support (e.g. +0200) from now on.
[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, *fmtstr, **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         create_folder(maildir, noisy ? 0 : 1, done);
298
299         if (chdir(maildir) == NOTOK)
300                 adios(maildir, "unable to change directory to");
301
302         /* read folder and create message structure */
303         if (!(mp = folder_read(folder)))
304                 adios(NULL, "unable to read folder %s", folder);
305
306         if (access(newmail, W_OK) != NOTOK) {
307                 locked++;
308                 if (trnflag) {
309                         SIGNAL(SIGHUP, SIG_IGN);
310                         SIGNAL(SIGINT, SIG_IGN);
311                         SIGNAL(SIGQUIT, SIG_IGN);
312                         SIGNAL(SIGTERM, SIG_IGN);
313                 }
314
315                 GETGROUPPRIVS();  /* Reset gid to lock mail file */
316                 in = lkfopen(newmail, "r");
317                 DROPGROUPPRIVS();
318                 if (in == NULL)
319                         adios(NULL, "unable to lock and fopen %s", newmail);
320                 fstat(fileno(in), &s1);
321         } else {
322                 trnflag = 0;
323                 if ((in = fopen(newmail, "r")) == NULL)
324                         adios(newmail, "unable to read");
325         }
326
327         /* This shouldn't be necessary but it can't hurt. */
328         DROPGROUPPRIVS();
329
330         if (audfile) {
331                 int i;
332                 if ((i = stat(audfile, &st)) == NOTOK)
333                         advise(NULL, "Creating Receive-Audit: %s", audfile);
334                 if ((aud = fopen(audfile, "a")) == NULL)
335                         adios(audfile, "unable to append to");
336                 else if (i == NOTOK)
337                         chmod(audfile, m_gmprot());
338
339                 fprintf(aud, from ? "<<inc>> %s  -ms %s\n" : "<<inc>> %s\n",
340                          dtimenow(), from);
341         }
342
343 #ifdef MHE
344         if (context_find("mhe")) {
345                 int i;
346                 cp = concat(maildir, "/++", NULL);
347                 i = stat(cp, &st);
348                 if ((mhe = fopen(cp, "a")) == NULL)
349                         admonish(cp, "unable to append to");
350                 else
351                         if (i == NOTOK)
352                                 chmod(cp, m_gmprot());
353                 free(cp);
354         }
355 #endif /* MHE */
356
357         /* Get new format string */
358         fmtstr = new_fs(form, FORMAT);
359
360         if (noisy) {
361                 printf("Incorporating new mail into %s...\n\n", folder);
362                 fflush(stdout);
363         }
364
365         /*
366         ** Get the mail from file (usually mail spool)
367         */
368         thisisanmbox(in);
369         hghnum = msgnum = mp->hghmsg;
370         for (;;) {
371                 /*
372                 ** Check if we need to allocate more space for message status.
373                 ** If so, then add space for an additional 100 messages.
374                 */
375                 if (msgnum >= mp->hghoff && !(mp = folder_realloc(mp, mp->lowoff, mp->hghoff + 100))) {
376                         advise(NULL, "unable to allocate folder storage");
377                         incerr = NOTOK;
378                         break;
379                 }
380
381                 /* create scanline for new message */
382                 switch (incerr = scan(in, msgnum + 1, msgnum + 1,
383                                 noisy ? fmtstr : NULL, width,
384                                 msgnum == hghnum && chgflag, 1)) {
385                 case SCNFAT:
386                 case SCNEOF:
387                         break;
388
389                 case SCNERR:
390                         if (aud)
391                                 fputs("inc aborted!\n", aud);
392                         /* doesn't clean up locks! */
393                         advise(NULL, "aborted!");
394                         break;
395
396                 case SCNNUM:
397                         advise(NULL, "BUG in %s, number out of range",
398                                         invo_name);
399                         break;
400
401                 default:
402                         advise(NULL, "BUG in %s, scan() botch (%d)",
403                                         invo_name, incerr);
404                         break;
405
406                 case SCNMSG:
407                         /*
408                         **  Run the external program hook on the message.
409                         */
410
411                         snprintf(b, sizeof (b), "%s/%d", maildir_copy,
412                                         msgnum + 1);
413                         ext_hook("add-hook", b, NULL);
414
415                         if (aud)
416                                 fputs(scanl, aud);
417 #ifdef MHE
418                         if (mhe)
419                                 fputs(scanl, mhe);
420 #endif /* MHE */
421                         if (noisy)
422                                 fflush(stdout);
423                         msgnum++;
424                         mp->hghmsg++;
425                         mp->nummsg++;
426                         if (mp->lowmsg == 0)
427                                 mp->lowmsg = 1;
428                         clear_msg_flags(mp, msgnum);
429                         set_exists(mp, msgnum);
430                         set_unseen(mp, msgnum);
431                         mp->msgflags |= SEQMOD;
432                         continue;
433                 }
434                 /*
435                 ** If we get here there was some sort of error from scan(),
436                 ** so stop processing anything more from the spool.
437                 */
438                 break;
439         }
440
441         if (incerr < 0) {  /* error */
442                 if (locked) {
443                         GETGROUPPRIVS();  /* Be sure we can unlock mail file */
444                         lkfclose(in, newmail); in = NULL;
445                         DROPGROUPPRIVS();  /*
446                                         ** And then return us to normal
447                                         ** privileges
448                                         */
449                 } else {
450                         fclose(in); in = NULL;
451                 }
452                 adios(NULL, "failed");
453         }
454
455         if (aud)
456                 fclose(aud);
457
458 #ifdef MHE
459         if (mhe)
460                 fclose(mhe);
461 #endif /* MHE */
462
463         if (noisy)
464                 fflush(stdout);
465
466         /*
467         ** truncate file we are incorporating from
468         */
469         if (trnflag) {
470                 if (stat(newmail, &st) != NOTOK && s1.st_mtime != st.st_mtime)
471                         advise(NULL, "new messages have arrived!\007");
472                 else {
473                         int newfd;
474                         if ((newfd = creat(newmail, 0600)) != NOTOK)
475                                 close(newfd);
476                         else
477                                 admonish(newmail, "error zero'ing");
478                 }
479         } else if (noisy) {
480                 printf("%s not zero'd\n", newmail);
481         }
482
483         if (msgnum == hghnum) {
484                 admonish(NULL, "no messages incorporated");
485         } else {
486                 context_replace(curfolder, folder); /* update current folder */
487                 if (chgflag)
488                         mp->curmsg = hghnum + 1;
489                 mp->hghmsg = msgnum;
490                 if (mp->lowmsg == 0)
491                         mp->lowmsg = 1;
492                 if (chgflag)  /* sigh... */
493                         seq_setcur(mp, mp->curmsg);
494         }
495
496         /*
497         ** unlock the mail spool
498         */
499         if (locked) {
500                 GETGROUPPRIVS();  /* Be sure we can unlock mail file */
501                 lkfclose(in, newmail); in = NULL;
502                 DROPGROUPPRIVS();  /* And then return us to normal privileges */
503         } else {
504                 fclose(in); in = NULL;
505         }
506
507         seq_setunseen(mp, 1);  /* add new msgs to unseen sequences */
508         seq_save(mp);  /* synchronize sequences   */
509         context_save();  /* save the context file   */
510         done(0);
511         return 1;
512 }
513
514 static void
515 inc_done(int status)
516 {
517         if (locked) {
518                 GETGROUPPRIVS();
519                 lkfclose(in, newmail);
520                 DROPGROUPPRIVS();
521         }
522         exit(status);
523 }