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