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