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