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