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