]> diplodocus.org Git - nmh/blob - uip/inc.c
90a9352896f7f69dba5661887150de68c560e3f4
[nmh] / uip / inc.c
1 /* inc.c -- incorporate messages from a maildrop into a folder
2 *
3 * This code is Copyright (c) 2002, by the authors of nmh. See the
4 * COPYRIGHT file in the root directory of the nmh distribution for
5 * complete copyright information.
6 */
7
8 #ifdef MAILGROUP
9 /*
10 * Thu Feb 12 21:00 CST 2015 Marcin Cieslak <saper@saper.info>
11 * Replaced setgid() calls with setegid() so that it works with dot
12 * locking on FreeBSD. setegid() should be supported on modern POSIX
13 * systems.
14 *
15 * Revised: Sat Apr 14 17:08:17 PDT 1990 (marvit@hplabs)
16 * Added hpux hacks to set and reset gid to be "mail" as needed. The reset
17 * is necessary so inc'ed mail is the group of the inc'er, rather than
18 * "mail". We setgid to egid only when [un]locking the mail file. This
19 * is also a major security precaution which will not be explained here.
20 *
21 * Fri Feb 7 16:04:57 PST 1992 John Romine <bug-mh@ics.uci.edu>
22 * NB: I'm not 100% sure that this setgid stuff is secure even now.
23 *
24 * See the *GROUPPRIVS() macros later. I'm reasonably happy with the setgid
25 * attribute. Running setuid root is probably not a terribly good idea, though.
26 * -- Peter Maydell <pmaydell@chiark.greenend.org.uk>, 04/1998
27 *
28 * Peter Maydell's patch slightly modified for nmh 0.28-pre2.
29 * Ruud de Rooij <ruud@debian.org> Wed, 22 Jul 1998 13:24:22 +0200
30 */
31 #endif
32
33 #include "h/mh.h"
34 #include "sbr/error.h"
35 #include "h/utils.h"
36 #include <fcntl.h>
37 #include "h/dropsbr.h"
38 #include "h/popsbr.h"
39 #include "h/fmt_scan.h"
40 #include "h/scansbr.h"
41 #include "h/signals.h"
42 #include "h/tws.h"
43 #include "h/mts.h"
44 #include "h/done.h"
45 #include "sbr/lock_file.h"
46 #include "sbr/m_maildir.h"
47 #include "sbr/m_mktemp.h"
48
49 #ifndef TLS_SUPPORT
50 # define TLSminc(a) (a)
51 #else
52 # define TLSminc(a) 0
53 #endif
54
55 #define INC_SWITCHES \
56 X("audit audit-file", 0, AUDSW) \
57 X("noaudit", 0, NAUDSW) \
58 X("changecur", 0, CHGSW) \
59 X("nochangecur", 0, NCHGSW) \
60 X("file name", 0, FILESW) \
61 X("form formatfile", 0, FORMSW) \
62 X("format string", 5, FMTSW) \
63 X("host hostname", 0, HOSTSW) \
64 X("user username", 0, USERSW) \
65 X("port name/number", 0, PORTSW) \
66 X("silent", 0, SILSW) \
67 X("nosilent", 0, NSILSW) \
68 X("truncate", 0, TRNCSW) \
69 X("notruncate", 0, NTRNCSW) \
70 X("width columns", 0, WIDTHSW) \
71 X("version", 0, VERSIONSW) \
72 X("help", 0, HELPSW) \
73 X("snoop", 0, SNOOPSW) \
74 X("sasl", 0, SASLSW) \
75 X("nosasl", 0, NOSASLSW) \
76 X("saslmech", 0, SASLMECHSW) \
77 X("initialtls", TLSminc(-10), INITTLSSW) \
78 X("notls", TLSminc(-5), NOTLSSW) \
79 X("certverify", TLSminc(-10), CERTVERSW) \
80 X("nocertverify", TLSminc(-12), NOCERTVERSW) \
81 X("authservice", 0, AUTHSERVICESW) \
82 X("proxy command", 0, PROXYSW) \
83
84 #define X(sw, minchars, id) id,
85 DEFINE_SWITCH_ENUM(INC);
86 #undef X
87
88 #define X(sw, minchars, id) { sw, minchars, id },
89 DEFINE_SWITCH_ARRAY(INC, switches);
90 #undef X
91
92 /*
93 * flags for the mail source
94 */
95 #define INC_FILE 0
96 #define INC_POP 1
97
98 static struct Maildir_entry {
99 char *filename;
100 time_t mtime;
101 } *Maildir = NULL;
102 static int num_maildir_entries = 0;
103 static bool snoop;
104
105 typedef struct {
106 FILE *mailout;
107 long written;
108 } pop_closure;
109
110 extern char response[];
111
112 /* This is an attempt to simplify things by putting all the
113 * privilege ops into macros.
114 * *GROUPPRIVS() is related to handling the setgid MAIL property,
115 * and only applies if MAILGROUP is defined.
116 * Basically, SAVEGROUPPRIVS() is called right at the top of main()
117 * to initialise things, and then DROPGROUPPRIVS() and GETGROUPPRIVS()
118 * do the obvious thing.
119 *
120 * There's probably a better implementation if we're allowed to use
121 * BSD-style setreuid() rather than using POSIX saved-ids.
122 * Anyway, if you're euid root it's a bit pointless to drop the group
123 * permissions...
124 *
125 * I'm pretty happy that the security is good provided we aren't setuid root.
126 * The only things we trust with group=mail privilege are lkfopen()
127 * and lkfclose().
128 */
129
130 /*
131 * For setting and returning to "mail" gid
132 */
133 #ifdef MAILGROUP
134 static gid_t return_gid;
135 #define TRYDROPGROUPPRIVS() DROPGROUPPRIVS()
136 #define DROPGROUPPRIVS() \
137 if (setegid(getgid()) != 0) { \
138 adios ("setegid", "unable to restore group to %ld", (long) getgid()); \
139 }
140 #define GETGROUPPRIVS() \
141 if (setegid(return_gid) != 0) { \
142 adios ("setegid", "unable to set group to %ld", (long) return_gid); \
143 }
144 #define SAVEGROUPPRIVS() return_gid = getegid()
145 #else
146 /* define *GROUPPRIVS() as null; this avoids having lots of "#ifdef MAILGROUP"s */
147 #define TRYDROPGROUPPRIVS()
148 #define DROPGROUPPRIVS()
149 #define GETGROUPPRIVS()
150 #define SAVEGROUPPRIVS()
151 #endif /* not MAILGROUP */
152
153 /* these variables have to be globals so that done() can correctly clean up the lockfile */
154 static bool locked;
155 static char *newmail;
156 static FILE *in;
157
158 /*
159 * prototypes
160 */
161 static int maildir_srt(const void *va, const void *vb) PURE;
162 static void inc_done(int) NORETURN;
163 static int pop_action(void *closure, char *);
164
165 static int
166 maildir_srt(const void *va, const void *vb)
167 {
168 const struct Maildir_entry *a = va, *b = vb;
169 if (a->mtime > b->mtime)
170 return 1;
171 if (a->mtime < b->mtime)
172 return -1;
173 return 0;
174 }
175
176 int
177 main (int argc, char **argv)
178 {
179 static int inc_type;
180 bool chgflag;
181 int trnflag = 1;
182 bool noisy;
183 int width = -1;
184 int hghnum = 0, msgnum = 0;
185 FILE *pf = NULL;
186 bool sasl, tls, noverify;
187 int incerr = 0; /* <0 if inc hits an error which means it should not truncate mailspool */
188 char *cp, *maildir = NULL, *folder = NULL;
189 char *format = NULL, *form = NULL;
190 char *host = NULL, *port = NULL, *user = NULL, *proxy = NULL;
191 char *audfile = NULL, *from = NULL, *saslmech = NULL, *auth_svc = NULL;
192 char buf[BUFSIZ], **argp, *nfs, **arguments;
193 struct msgs *mp = NULL;
194 struct stat st, s1;
195 FILE *aud = NULL;
196 char b[PATH_MAX + 1];
197 char *maildir_copy = NULL; /* copy of mail directory because the static gets overwritten */
198
199 int nmsgs, nbytes;
200 char *MAILHOST_env_variable;
201 set_done(inc_done);
202
203 /* absolutely the first thing we do is save our privileges,
204 * and drop them if we can.
205 */
206 SAVEGROUPPRIVS();
207 TRYDROPGROUPPRIVS();
208
209 if (nmh_init(argv[0], true, true)) { return 1; }
210
211 mts_init ();
212 arguments = getarguments (invo_name, argc, argv, 1);
213 argp = arguments;
214
215 /*
216 * Scheme is:
217 * use MAILHOST environment variable if present,
218 * else try Hesiod.
219 * If that fails, use the default (if any)
220 * provided by mts.conf in mts_init()
221 */
222 if ((MAILHOST_env_variable = getenv("MAILHOST")) != NULL)
223 pophost = MAILHOST_env_variable;
224 /*
225 * If there is a valid "pophost" entry in mts.conf,
226 * then use it as the default host.
227 */
228 if (pophost && *pophost)
229 host = pophost;
230
231 sasl = tls = false;
232 chgflag = noisy = noverify = true;
233 while ((cp = *argp++)) {
234 if (*cp == '-') {
235 switch (smatch (++cp, switches)) {
236 case AMBIGSW:
237 ambigsw (cp, switches);
238 done (1);
239 case UNKWNSW:
240 die("-%s unknown", cp);
241
242 case HELPSW:
243 snprintf (buf, sizeof(buf), "%s [+folder] [switches]", invo_name);
244 print_help (buf, switches, 1);
245 done (0);
246 case VERSIONSW:
247 print_version(invo_name);
248 done (0);
249
250 case AUDSW:
251 if (!(cp = *argp++) || *cp == '-')
252 die("missing argument to %s", argp[-2]);
253 audfile = mh_xstrdup(m_maildir(cp));
254 continue;
255 case NAUDSW:
256 audfile = NULL;
257 continue;
258
259 case CHGSW:
260 chgflag = true;
261 continue;
262 case NCHGSW:
263 chgflag = false;
264 continue;
265
266 /*
267 * The flag `trnflag' has the value:
268 *
269 * 2 if -truncate is given
270 * 1 by default (truncating is default)
271 * 0 if -notruncate is given
272 */
273 case TRNCSW:
274 trnflag = 2;
275 continue;
276 case NTRNCSW:
277 trnflag = 0;
278 continue;
279
280 case FILESW:
281 if (!(cp = *argp++) || *cp == '-')
282 die("missing argument to %s", argp[-2]);
283 from = path (cp, TFILE);
284
285 /*
286 * If the truncate file is in default state,
287 * change to not truncate.
288 */
289 if (trnflag == 1)
290 trnflag = 0;
291 continue;
292
293 case SILSW:
294 noisy = false;
295 continue;
296 case NSILSW:
297 noisy = true;
298 continue;
299
300 case FORMSW:
301 if (!(form = *argp++) || *form == '-')
302 die("missing argument to %s", argp[-2]);
303 format = NULL;
304 continue;
305 case FMTSW:
306 if (!(format = *argp++) || *format == '-')
307 die("missing argument to %s", argp[-2]);
308 form = NULL;
309 continue;
310
311 case WIDTHSW:
312 if (!(cp = *argp++) || *cp == '-')
313 die("missing argument to %s", argp[-2]);
314 width = atoi (cp);
315 continue;
316
317 case HOSTSW:
318 if (!(host = *argp++) || *host == '-')
319 die("missing argument to %s", argp[-2]);
320 continue;
321
322 case PORTSW:
323 if (!(port = *argp++) || *port == '-')
324 die("missing argument to %s", argp[-2]);
325 continue;
326
327 case USERSW:
328 if (!(user = *argp++) || *user == '-')
329 die("missing argument to %s", argp[-2]);
330 continue;
331
332 case SNOOPSW:
333 snoop = true;
334 continue;
335
336 case SASLSW:
337 sasl = true;
338 continue;
339 case NOSASLSW:
340 sasl = false;
341 continue;
342
343 case SASLMECHSW:
344 if (!(saslmech = *argp++) || *saslmech == '-')
345 die("missing argument to %s", argp[-2]);
346 continue;
347
348 case INITTLSSW:
349 tls = true;
350 continue;
351
352 case NOTLSSW:
353 tls = false;
354 continue;
355
356 case CERTVERSW:
357 noverify = false;
358 continue;
359
360 case NOCERTVERSW:
361 noverify = true;
362 continue;
363
364 case AUTHSERVICESW:
365 #ifdef OAUTH_SUPPORT
366 if (!(auth_svc = *argp++) || *auth_svc == '-')
367 die("missing argument to %s", argp[-2]);
368 #else
369 die("not built with OAuth support");
370 #endif
371 continue;
372
373 case PROXYSW:
374 if (!(proxy = *argp++) || *proxy == '-')
375 die("missing argument to %s", argp[-2]);
376 continue;
377 }
378 }
379 if (*cp == '+' || *cp == '@') {
380 if (folder)
381 die("only one folder at a time!");
382 folder = pluspath (cp);
383 } else {
384 die("usage: %s [+folder] [switches]", invo_name);
385 }
386 }
387
388 /* NOTE: above this point you should use TRYDROPGROUPPRIVS(),
389 * not DROPGROUPPRIVS().
390 */
391 if (host && !*host)
392 host = NULL;
393
394 /* guarantee dropping group privileges; we might not have done so earlier */
395 DROPGROUPPRIVS();
396
397 /* Source of mail; -from overrides any -host. */
398 inc_type = host && !from ? INC_POP : INC_FILE;
399
400 if (inc_type == INC_POP) {
401 /* Mail from a POP server. */
402 int tlsflag = 0;
403
404 if (auth_svc == NULL) {
405 if (saslmech && ! strcasecmp(saslmech, "xoauth2")) {
406 die("must specify -authservice with -saslmech xoauth2");
407 }
408 } else {
409 if (user == NULL) {
410 die("must specify -user with -saslmech xoauth2");
411 }
412 }
413
414 if (tls)
415 tlsflag |= P_INITTLS;
416
417 if (noverify)
418 tlsflag |= P_NOVERIFY;
419
420 /*
421 * initialize POP connection
422 */
423 if (pop_init (host, port, user, proxy, snoop, sasl, saslmech,
424 tlsflag, auth_svc) == NOTOK)
425 die("%s", response);
426
427 /* Check if there are any messages */
428 if (pop_stat (&nmsgs, &nbytes) == NOTOK)
429 die("%s", response);
430
431 if (nmsgs == 0) {
432 pop_quit();
433 die("no mail to incorporate");
434 }
435
436 } else if (inc_type == INC_FILE) {
437 /* Mail from a spool file, or Maildir. */
438 if (from)
439 newmail = from;
440 else if ((newmail = getenv ("MAILDROP")) && *newmail)
441 newmail = m_mailpath (newmail);
442 else if ((newmail = context_find ("maildrop")) && *newmail)
443 newmail = m_mailpath (newmail);
444 else {
445 newmail = concat (MAILDIR, "/", MAILFIL, NULL);
446 }
447 if (stat (newmail, &s1) == NOTOK || s1.st_size == 0)
448 die("no mail to incorporate");
449 if (s1.st_mode & S_IFDIR) {
450 DIR *md;
451 struct dirent *de;
452 struct stat ms;
453 int i;
454 i = 0;
455 cp = concat (newmail, "/new", NULL);
456 if ((md = opendir(cp)) == NULL)
457 die("unable to open %s", cp);
458 while ((de = readdir (md)) != NULL) {
459 if (de->d_name[0] == '.')
460 continue;
461 if (i >= num_maildir_entries) {
462 if ((Maildir = realloc(Maildir, sizeof(*Maildir) * (2*i+16))) == NULL)
463 die("not enough memory for %d messages", 2*i+16);
464 num_maildir_entries = 2*i+16;
465 }
466 Maildir[i].filename = concat (cp, "/", de->d_name, NULL);
467 if (stat(Maildir[i].filename, &ms) != 0)
468 adios (Maildir[i].filename, "couldn't get delivery time");
469 Maildir[i].mtime = ms.st_mtime;
470 i++;
471 }
472 free (cp);
473 closedir (md);
474 cp = concat (newmail, "/cur", NULL);
475 if ((md = opendir(cp)) == NULL)
476 die("unable to open %s", cp);
477 while ((de = readdir (md)) != NULL) {
478 if (de->d_name[0] == '.')
479 continue;
480 if (i >= num_maildir_entries) {
481 if ((Maildir = realloc(Maildir, sizeof(*Maildir) * (2*i+16))) == NULL)
482 die("not enough memory for %d messages", 2*i+16);
483 num_maildir_entries = 2*i+16;
484 }
485 Maildir[i].filename = concat (cp, "/", de->d_name, NULL);
486 if (stat(Maildir[i].filename, &ms) != 0)
487 adios (Maildir[i].filename, "couldn't get delivery time");
488 Maildir[i].mtime = ms.st_mtime;
489 i++;
490 }
491 free (cp);
492 closedir (md);
493 if (i == 0)
494 die("no mail to incorporate");
495 num_maildir_entries = i;
496 qsort (Maildir, num_maildir_entries, sizeof(*Maildir), maildir_srt);
497 }
498
499 cp = mh_xstrdup(newmail);
500 newmail = cp;
501 }
502
503 if (!context_find ("path"))
504 free (path ("./", TFOLDER));
505 if (!folder)
506 folder = getfolder (0);
507 maildir = m_maildir (folder);
508 maildir_copy = mh_xstrdup(maildir);
509
510 if (!folder_exists(maildir)) {
511 /* If the folder doesn't exist, and we're given the -silent flag,
512 * just fail.
513 */
514 if (noisy)
515 create_folder(maildir, 0, done);
516 else
517 done (1);
518 }
519
520 if (chdir (maildir) == NOTOK)
521 adios (maildir, "unable to change directory to");
522
523 /* read folder and create message structure */
524 if (!(mp = folder_read (folder, 0)))
525 die("unable to read folder %s", folder);
526
527 if (inc_type == INC_FILE && Maildir == NULL) {
528 /* Mail from a spool file. */
529
530 if (access (newmail, W_OK) != NOTOK) {
531 locked = true;
532 if (trnflag) {
533 SIGNAL (SIGHUP, SIG_IGN);
534 SIGNAL (SIGINT, SIG_IGN);
535 SIGNAL (SIGQUIT, SIG_IGN);
536 SIGNAL (SIGTERM, SIG_IGN);
537 }
538
539 GETGROUPPRIVS(); /* Reset gid to lock mail file */
540 in = lkfopenspool (newmail, "r");
541 DROPGROUPPRIVS();
542 if (in == NULL)
543 die("unable to lock and fopen %s", newmail);
544 fstat (fileno(in), &s1);
545 } else {
546 trnflag = 0;
547 if ((in = fopen (newmail, "r")) == NULL)
548 adios (newmail, "unable to read");
549 }
550 }
551
552 /* This shouldn't be necessary but it can't hurt. */
553 DROPGROUPPRIVS();
554
555 if (audfile) {
556 int i;
557 if ((i = stat (audfile, &st)) == NOTOK)
558 inform("Creating Receive-Audit: %s", audfile);
559 if ((aud = fopen (audfile, "a")) == NULL)
560 adios (audfile, "unable to append to");
561 if (i == NOTOK)
562 chmod (audfile, m_gmprot ());
563
564 if (from)
565 fprintf (aud, "<<inc>> %s -ms %s\n", dtimenow(0), from);
566 else {
567 if (host)
568 fprintf (aud, "<<inc>> %s -host %s -user %s\n", dtimenow(0),
569 host, user);
570 else
571 fprintf (aud, "<<inc>> %s\n", dtimenow (0));
572 }
573 }
574
575 /* Get new format string */
576 nfs = new_fs (form, format, FORMAT);
577
578 if (noisy) {
579 printf ("Incorporating new mail into %s...\n\n", folder);
580 fflush (stdout);
581 }
582
583
584 /*
585 * Get the mail from a POP server
586 */
587 if (inc_type == INC_POP) {
588 /* Mail from a POP server. */
589 int i;
590 pop_closure pc;
591
592 hghnum = msgnum = mp->hghmsg;
593 for (i = 1; i <= nmsgs; i++) {
594 charstring_t scanl = NULL;
595
596 msgnum++;
597 cp = mh_xstrdup(m_name (msgnum));
598 if ((pf = fopen (cp, "w+")) == NULL)
599 adios (cp, "unable to write");
600 chmod (cp, m_gmprot ());
601
602 pc.written = 0;
603 pc.mailout = pf;
604 if (pop_retr(i, pop_action, &pc) == NOTOK)
605 die("%s", response);
606
607 if (fflush (pf))
608 adios (cp, "write error on");
609 fseek (pf, 0L, SEEK_SET);
610 switch (incerr = scan (pf, msgnum, 0, nfs, width,
611 msgnum == mp->hghmsg + 1 && chgflag,
612 1, NULL, pc.written, noisy, &scanl)) {
613 case SCNEOF:
614 printf ("%*d empty\n", DMAXFOLDER, msgnum);
615 break;
616
617 case SCNFAT:
618 trnflag = 0;
619 noisy = true;
620 /* advise (cp, "unable to read"); already advised */
621 break;
622
623 case SCNERR:
624 case SCNNUM:
625 break;
626
627 case SCNMSG:
628 case SCNENC:
629 default:
630 if (aud)
631 fputs (charstring_buffer (scanl), aud);
632 if (noisy)
633 fflush (stdout);
634 break;
635 }
636 charstring_free (scanl);
637
638 if (ferror(pf) || fclose (pf)) {
639 int e = errno;
640 (void) m_unlink (cp);
641 pop_quit ();
642 errno = e;
643 adios (cp, "write error on");
644 }
645 free (cp);
646
647 if (trnflag && pop_dele (i) == NOTOK)
648 die("%s", response);
649
650 scan_finished();
651 }
652
653 if (pop_quit () == NOTOK)
654 die("%s", response);
655
656 } else if (inc_type == INC_FILE && Maildir == NULL) {
657 /* Mail from a spool file. */
658
659 scan_detect_mbox_style (in); /* the MAGIC invocation... */
660 hghnum = msgnum = mp->hghmsg;
661 for (;;) {
662 charstring_t scanl = NULL;
663
664 /* create scanline for new message */
665 switch (incerr = scan (in, msgnum + 1, msgnum + 1, nfs, width,
666 msgnum == hghnum && chgflag, 1, NULL, 0L, noisy,
667 &scanl)) {
668 case SCNFAT:
669 case SCNEOF:
670 break;
671
672 case SCNERR:
673 if (aud)
674 fputs ("inc aborted!\n", aud);
675 inform("aborted!"); /* doesn't clean up locks! */
676 break;
677
678 case SCNNUM:
679 inform("BUG in %s, number out of range", invo_name);
680 break;
681
682 default:
683 inform("BUG in %s, scan() botch (%d)", invo_name, incerr);
684 break;
685
686 case SCNMSG:
687 case SCNENC:
688 /*
689 * Run the external program hook on the message.
690 */
691
692 (void)snprintf(b, sizeof (b), "%s/%d", maildir_copy, msgnum + 1);
693 (void)ext_hook("add-hook", b, NULL);
694
695 if (aud)
696 fputs (charstring_buffer (scanl), aud);
697 if (noisy)
698 fflush (stdout);
699
700 msgnum++;
701 continue;
702 }
703 charstring_free (scanl);
704
705 /* If we get here there was some sort of error from scan(),
706 * so stop processing anything more from the spool.
707 */
708 break;
709 }
710
711 } else {
712 /* Mail from Maildir. */
713 char *sp;
714 FILE *sf;
715 int i;
716
717 hghnum = msgnum = mp->hghmsg;
718 for (i = 0; i < num_maildir_entries; i++) {
719 charstring_t scanl = NULL;
720
721 msgnum++;
722
723 sp = Maildir[i].filename;
724 cp = mh_xstrdup(m_name (msgnum));
725 pf = NULL;
726 if (!trnflag || link(sp, cp) == -1) {
727 static char buf[65536];
728 size_t nrd;
729
730 if ((sf = fopen (sp, "r")) == NULL)
731 adios (sp, "unable to read for copy");
732 if ((pf = fopen (cp, "w+")) == NULL)
733 adios (cp, "unable to write for copy");
734 while ((nrd = fread(buf, 1, sizeof(buf), sf)) > 0)
735 if (fwrite(buf, 1, nrd, pf) != nrd)
736 break;
737 if (ferror(sf) || fflush(pf) || ferror(pf)) {
738 int e = errno;
739 fclose(pf); fclose(sf); (void) m_unlink(cp);
740 errno = e;
741 adios(cp, "copy error %s -> %s", sp, cp);
742 }
743 fclose (sf);
744 sf = NULL;
745 }
746 if (pf == NULL && (pf = fopen (cp, "r")) == NULL)
747 adios (cp, "not available");
748 chmod (cp, m_gmprot ());
749
750 fseek (pf, 0L, SEEK_SET);
751 switch (incerr = scan (pf, msgnum, 0, nfs, width,
752 msgnum == mp->hghmsg + 1 && chgflag,
753 1, NULL, 0, noisy, &scanl)) {
754 case SCNEOF:
755 printf ("%*d empty\n", DMAXFOLDER, msgnum);
756 break;
757
758 case SCNFAT:
759 trnflag = 0;
760 noisy = true;
761 /* advise (cp, "unable to read"); already advised */
762 break;
763
764 case SCNERR:
765 case SCNNUM:
766 break;
767
768 case SCNMSG:
769 case SCNENC:
770 default:
771 /*
772 * Run the external program hook on the message.
773 */
774
775 (void)snprintf(b, sizeof (b), "%s/%d", maildir_copy, msgnum + 1);
776 (void)ext_hook("add-hook", b, NULL);
777
778 if (aud)
779 fputs (charstring_buffer (scanl), aud);
780 if (noisy)
781 fflush (stdout);
782 break;
783 }
784 charstring_free (scanl);
785
786 if (ferror(pf) || fclose (pf)) {
787 int e = errno;
788 (void) m_unlink (cp);
789 errno = e;
790 adios (cp, "write error on");
791 }
792 pf = NULL;
793 free (cp);
794
795 if (trnflag && m_unlink (sp) == NOTOK)
796 adios (sp, "couldn't unlink");
797 free (sp); /* Free Maildir[i]->filename */
798
799 scan_finished();
800 }
801 free (Maildir); /* From now on Maildir is just a flag - don't dref! */
802 }
803
804 scan_finished ();
805
806 if (incerr < 0) { /* error */
807 if (locked) {
808 GETGROUPPRIVS(); /* Be sure we can unlock mail file */
809 (void) lkfclosespool (in, newmail); in = NULL;
810 DROPGROUPPRIVS(); /* And then return us to normal privileges */
811 } else {
812 fclose (in); in = NULL;
813 }
814 die("failed");
815 }
816
817 if (aud)
818 fclose (aud);
819
820 if (noisy)
821 fflush (stdout);
822
823 if (inc_type == INC_FILE && Maildir == NULL) {
824 /* Mail from a spool file; truncate it. */
825
826 if (trnflag) {
827 if (stat (newmail, &st) != NOTOK && s1.st_mtime != st.st_mtime)
828 inform("new messages have arrived!\007");
829 else {
830 int newfd;
831 if ((newfd = creat (newmail, 0600)) != NOTOK)
832 close (newfd);
833 else
834 admonish (newmail, "error zero'ing");
835 }
836 } else {
837 if (noisy)
838 printf ("%s not zero'd\n", newmail);
839 }
840 }
841
842 if (msgnum == hghnum) {
843 inform("no messages incorporated, continuing...");
844 } else {
845 /*
846 * Lock the sequence file now, and loop to set the right flags
847 * in the folder structure
848 */
849
850 struct msgs *mp2;
851 int i;
852
853 context_replace (pfolder, folder); /* update current folder */
854
855 if ((mp2 = folder_read(folder, 1)) == NULL) {
856 inform("Unable to reread folder %s, continuing...", folder);
857 goto skip;
858 }
859
860 /*
861 * Shouldn't happen, but just in case ...
862 */
863
864 if (msgnum >= mp2->hghoff
865 && !(mp2 = folder_realloc (mp2, mp2->lowoff, msgnum + 1))) {
866 inform("unable to reallocate folder storage");
867 goto skip;
868 }
869
870 if (chgflag)
871 mp2->curmsg = hghnum + 1;
872 mp2->hghmsg = msgnum;
873
874 if (mp2->lowmsg == 0)
875 mp2->lowmsg = 1;
876 if (chgflag) /* sigh... */
877 seq_setcur (mp2, mp2->curmsg);
878
879 for (i = hghnum + 1; i <= msgnum; i++) {
880 clear_msg_flags (mp2, i);
881 set_exists (mp2, i);
882 set_unseen (mp2, i);
883 }
884 mp2->msgflags |= SEQMOD;
885 seq_setunseen(mp2, 0); /* Set the Unseen-Sequence */
886 seq_save(mp2); /* Save the sequence file */
887 folder_free(mp2);
888 }
889
890 skip:
891 if (inc_type == INC_FILE && Maildir == NULL) {
892 /* Mail from a spool file; unlock it. */
893
894 if (locked) {
895 GETGROUPPRIVS(); /* Be sure we can unlock mail file */
896 (void) lkfclosespool (in, newmail); in = NULL;
897 DROPGROUPPRIVS(); /* And then return us to normal privileges */
898 } else {
899 fclose (in); in = NULL;
900 }
901 }
902
903 context_save (); /* save the context file */
904 done (0);
905 return 1;
906 }
907
908
909 static void NORETURN
910 inc_done (int status)
911 {
912 set_done(exit);
913 if (locked)
914 {
915 GETGROUPPRIVS();
916 lkfclosespool(in, newmail);
917 DROPGROUPPRIVS();
918 }
919 exit (status);
920 }
921
922 static int
923 pop_action(void *closure, char *s)
924 {
925 pop_closure *pc;
926 int n;
927
928 pc = closure;
929 n = fprintf(pc->mailout, "%s\n", s);
930 if (n < 0)
931 return NOTOK;
932 pc->written += n; /* Count linefeed too. */
933
934 return OK;
935 }