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