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