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