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