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