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