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