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