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