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