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