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