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