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