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