]> diplodocus.org Git - nmh/blob - uip/inc.c
sbr/dtime.c: Remove struct-assigning twscopy().
[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 int chgflag = 1, trnflag = 1;
188 int noisy = 1, width = -1;
189 int hghnum = 0, msgnum = 0;
190 int sasl = 0, tls = 0, noverify = 1;
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 CERTVERSW:
367 noverify = 0;
368 continue;
369
370 case NOCERTVERSW:
371 noverify++;
372 continue;
373
374 case AUTHSERVICESW:
375 #ifdef OAUTH_SUPPORT
376 if (!(auth_svc = *argp++) || *auth_svc == '-')
377 adios (NULL, "missing argument to %s", argp[-2]);
378 #else
379 adios (NULL, "not built with OAuth support");
380 #endif
381 continue;
382
383 case PROXYSW:
384 if (!(proxy = *argp++) || *proxy == '-')
385 adios (NULL, "missing argument to %s", argp[-2]);
386 continue;
387 }
388 }
389 if (*cp == '+' || *cp == '@') {
390 if (folder)
391 adios (NULL, "only one folder at a time!");
392 else
393 folder = pluspath (cp);
394 } else {
395 adios (NULL, "usage: %s [+folder] [switches]", invo_name);
396 }
397 }
398
399 /* NOTE: above this point you should use TRYDROPGROUPPRIVS(),
400 * not DROPGROUPPRIVS().
401 */
402 if (host && !*host)
403 host = NULL;
404
405 /* guarantee dropping group priveleges; we might not have done so earlier */
406 DROPGROUPPRIVS();
407
408 /*
409 * Where are we getting the new mail?
410 */
411 if (from)
412 inc_type = INC_FILE;
413 else if (host)
414 inc_type = INC_POP;
415 else
416 inc_type = INC_FILE;
417
418 /*
419 * Are we getting the mail from
420 * a POP server?
421 */
422 if (inc_type == INC_POP) {
423 int tlsflag = 0;
424
425 if (auth_svc == NULL) {
426 if (saslmech && ! strcasecmp(saslmech, "xoauth2")) {
427 adios (NULL, "must specify -authservice with -saslmech xoauth2");
428 }
429 } else {
430 if (user == NULL) {
431 adios (NULL, "must specify -user with -saslmech xoauth2");
432 }
433 }
434
435 if (tls)
436 tlsflag |= P_INITTLS;
437
438 if (noverify)
439 tlsflag |= P_NOVERIFY;
440
441 /*
442 * initialize POP connection
443 */
444 if (pop_init (host, port, user, proxy, snoop, sasl, saslmech,
445 tlsflag, auth_svc) == NOTOK)
446 adios (NULL, "%s", response);
447
448 /* Check if there are any messages */
449 if (pop_stat (&nmsgs, &nbytes) == NOTOK)
450 adios (NULL, "%s", response);
451
452 if (nmsgs == 0) {
453 pop_quit();
454 adios (NULL, "no mail to incorporate");
455 }
456 }
457
458 /*
459 * We will get the mail from a file
460 * (typically the standard maildrop)
461 */
462
463 if (inc_type == INC_FILE) {
464 if (from)
465 newmail = from;
466 else if ((newmail = getenv ("MAILDROP")) && *newmail)
467 newmail = m_mailpath (newmail);
468 else if ((newmail = context_find ("maildrop")) && *newmail)
469 newmail = m_mailpath (newmail);
470 else {
471 newmail = concat (MAILDIR, "/", MAILFIL, NULL);
472 }
473 if (stat (newmail, &s1) == NOTOK || s1.st_size == 0)
474 adios (NULL, "no mail to incorporate");
475 if (s1.st_mode & S_IFDIR) {
476 DIR *md;
477 struct dirent *de;
478 struct stat ms;
479 int i;
480 i = 0;
481 cp = concat (newmail, "/new", NULL);
482 if ((md = opendir(cp)) == NULL)
483 adios (NULL, "unable to open %s", cp);
484 while ((de = readdir (md)) != NULL) {
485 if (de->d_name[0] == '.')
486 continue;
487 if (i >= num_maildir_entries) {
488 if ((Maildir = realloc(Maildir, sizeof(*Maildir) * (2*i+16))) == NULL)
489 adios(NULL, "not enough memory for %d messages", 2*i+16);
490 num_maildir_entries = 2*i+16;
491 }
492 Maildir[i].filename = concat (cp, "/", de->d_name, NULL);
493 if (stat(Maildir[i].filename, &ms) != 0)
494 adios (Maildir[i].filename, "couldn't get delivery time");
495 Maildir[i].mtime = ms.st_mtime;
496 i++;
497 }
498 free (cp);
499 closedir (md);
500 cp = concat (newmail, "/cur", NULL);
501 if ((md = opendir(cp)) == NULL)
502 adios (NULL, "unable to open %s", cp);
503 while ((de = readdir (md)) != NULL) {
504 if (de->d_name[0] == '.')
505 continue;
506 if (i >= num_maildir_entries) {
507 if ((Maildir = realloc(Maildir, sizeof(*Maildir) * (2*i+16))) == NULL)
508 adios(NULL, "not enough memory for %d messages", 2*i+16);
509 num_maildir_entries = 2*i+16;
510 }
511 Maildir[i].filename = concat (cp, "/", de->d_name, NULL);
512 if (stat(Maildir[i].filename, &ms) != 0)
513 adios (Maildir[i].filename, "couldn't get delivery time");
514 Maildir[i].mtime = ms.st_mtime;
515 i++;
516 }
517 free (cp);
518 closedir (md);
519 if (i == 0)
520 adios (NULL, "no mail to incorporate");
521 num_maildir_entries = i;
522 qsort (Maildir, num_maildir_entries, sizeof(*Maildir), maildir_srt);
523 }
524
525 cp = mh_xstrdup(newmail);
526 newmail = cp;
527 }
528
529 /* skip the folder setup */
530 if ((inc_type == INC_POP) && packfile)
531 goto go_to_it;
532
533 if (!context_find ("path"))
534 free (path ("./", TFOLDER));
535 if (!folder)
536 folder = getfolder (0);
537 maildir = m_maildir (folder);
538 maildir_copy = mh_xstrdup(maildir);
539
540 if (!folder_exists(maildir)) {
541 /* If the folder doesn't exist, and we're given the -silent flag,
542 * just fail.
543 */
544 if (noisy)
545 create_folder(maildir, 0, done);
546 else
547 done (1);
548 }
549
550 if (chdir (maildir) == NOTOK)
551 adios (maildir, "unable to change directory to");
552
553 /* read folder and create message structure */
554 if (!(mp = folder_read (folder, 0)))
555 adios (NULL, "unable to read folder %s", folder);
556
557 go_to_it:
558
559 if (inc_type == INC_FILE && Maildir == NULL) {
560 if (access (newmail, W_OK) != NOTOK) {
561 locked++;
562 if (trnflag) {
563 SIGNAL (SIGHUP, SIG_IGN);
564 SIGNAL (SIGINT, SIG_IGN);
565 SIGNAL (SIGQUIT, SIG_IGN);
566 SIGNAL (SIGTERM, SIG_IGN);
567 }
568
569 GETGROUPPRIVS(); /* Reset gid to lock mail file */
570 in = lkfopenspool (newmail, "r");
571 DROPGROUPPRIVS();
572 if (in == NULL)
573 adios (NULL, "unable to lock and fopen %s", newmail);
574 fstat (fileno(in), &s1);
575 } else {
576 trnflag = 0;
577 if ((in = fopen (newmail, "r")) == NULL)
578 adios (newmail, "unable to read");
579 }
580 }
581
582 /* This shouldn't be necessary but it can't hurt. */
583 DROPGROUPPRIVS();
584
585 if (audfile) {
586 int i;
587 if ((i = stat (audfile, &st)) == NOTOK)
588 inform("Creating Receive-Audit: %s", audfile);
589 if ((aud = fopen (audfile, "a")) == NULL)
590 adios (audfile, "unable to append to");
591 else if (i == NOTOK)
592 chmod (audfile, m_gmprot ());
593
594 if (from)
595 fprintf (aud, "<<inc>> %s -ms %s\n", dtimenow(0), from);
596 else {
597 if (host)
598 fprintf (aud, "<<inc>> %s -host %s -user %s\n", dtimenow(0),
599 host, user);
600 else
601 fprintf (aud, "<<inc>> %s\n", dtimenow (0));
602 }
603 }
604
605 /* Get new format string */
606 nfs = new_fs (form, format, FORMAT);
607
608 if (noisy) {
609 printf ("Incorporating new mail into %s...\n\n", folder);
610 fflush (stdout);
611 }
612
613
614 /*
615 * Get the mail from a POP server
616 */
617 if (inc_type == INC_POP) {
618 int i;
619 if (packfile) {
620 packfile = path (packfile, TFILE);
621 if (stat (packfile, &st) == NOTOK) {
622 if (errno != ENOENT)
623 adios (packfile, "error on file");
624 cp = concat ("Create file \"", packfile, "\"? ", NULL);
625 if (noisy && !read_yes_or_no_if_tty (cp))
626 done (1);
627 free (cp);
628 }
629 msgnum = map_count ();
630 if ((pd = mbx_open (packfile, mbx_style, getuid(), getgid(), m_gmprot()))
631 == NOTOK)
632 adios (packfile, "unable to open");
633 if ((pf = fdopen (pd, "w+")) == NULL)
634 adios (NULL, "unable to fdopen %s", packfile);
635 } else {
636 hghnum = msgnum = mp->hghmsg;
637 }
638
639 for (i = 1; i <= nmsgs; i++) {
640 charstring_t scanl = NULL;
641
642 msgnum++;
643 if (packfile) {
644 size_t len;
645
646 fseek (pf, 0L, SEEK_CUR);
647 pos = ftell (pf);
648 size = 0;
649 len = strlen(mmdlm1);
650 if (fwrite(mmdlm1, 1, len, pf) < len)
651 advise (mmdlm1, "fwrite");
652 start = ftell (pf);
653
654 if (pop_retr (i, pop_pack) == NOTOK)
655 adios (NULL, "%s", response);
656
657 fseek (pf, 0L, SEEK_CUR);
658 stop = ftell (pf);
659 if (fflush (pf))
660 adios (packfile, "write error on");
661 fseek (pf, start, SEEK_SET);
662 } else {
663 cp = mh_xstrdup(m_name (msgnum));
664 if ((pf = fopen (cp, "w+")) == NULL)
665 adios (cp, "unable to write");
666 chmod (cp, m_gmprot ());
667 start = stop = 0L;
668
669 if (pop_retr (i, pop_action) == NOTOK)
670 adios (NULL, "%s", response);
671
672 if (fflush (pf))
673 adios (cp, "write error on");
674 fseek (pf, 0L, SEEK_SET);
675 }
676 switch (incerr = scan (pf, msgnum, 0, nfs, width,
677 packfile ? 0 : msgnum == mp->hghmsg + 1 && chgflag,
678 1, NULL, stop - start, noisy, &scanl)) {
679 case SCNEOF:
680 printf ("%*d empty\n", DMAXFOLDER, msgnum);
681 break;
682
683 case SCNFAT:
684 trnflag = 0;
685 noisy++;
686 /* advise (cp, "unable to read"); already advised */
687 /* FALLTHRU */
688
689 case SCNERR:
690 case SCNNUM:
691 break;
692
693 case SCNMSG:
694 case SCNENC:
695 default:
696 if (aud)
697 fputs (charstring_buffer (scanl), aud);
698 if (noisy)
699 fflush (stdout);
700 break;
701 }
702 charstring_free (scanl);
703
704 if (packfile) {
705 size_t len;
706
707 fseek (pf, stop, SEEK_SET);
708 len = strlen(mmdlm2);
709 if (fwrite(mmdlm2, 1, len, pf) < len)
710 advise (mmdlm2, "fwrite");
711 if (fflush (pf) || ferror (pf)) {
712 int e = errno;
713 pop_quit ();
714 errno = e;
715 adios (packfile, "write error on");
716 }
717 map_write (packfile, pd, 0, 0L, start, stop, pos, size, noisy);
718 } else {
719 if (ferror(pf) || fclose (pf)) {
720 int e = errno;
721 (void) m_unlink (cp);
722 pop_quit ();
723 errno = e;
724 adios (cp, "write error on");
725 }
726 free (cp);
727 }
728
729 if (trnflag && pop_dele (i) == NOTOK)
730 adios (NULL, "%s", response);
731
732 scan_finished();
733 }
734
735 if (pop_quit () == NOTOK)
736 adios (NULL, "%s", response);
737 if (packfile) {
738 mbx_close (packfile, pd);
739 pd = NOTOK;
740 }
741 }
742
743 /*
744 * Get the mail from file (usually mail spool)
745 */
746 if (inc_type == INC_FILE && Maildir == NULL) {
747 scan_detect_mbox_style (in); /* the MAGIC invocation... */
748 hghnum = msgnum = mp->hghmsg;
749 for (;;) {
750 charstring_t scanl = NULL;
751
752 /* create scanline for new message */
753 switch (incerr = scan (in, msgnum + 1, msgnum + 1, nfs, width,
754 msgnum == hghnum && chgflag, 1, NULL, 0L, noisy,
755 &scanl)) {
756 case SCNFAT:
757 case SCNEOF:
758 break;
759
760 case SCNERR:
761 if (aud)
762 fputs ("inc aborted!\n", aud);
763 inform("aborted!"); /* doesn't clean up locks! */
764 break;
765
766 case SCNNUM:
767 inform("BUG in %s, number out of range", invo_name);
768 break;
769
770 default:
771 inform("BUG in %s, scan() botch (%d)", invo_name, incerr);
772 break;
773
774 case SCNMSG:
775 case SCNENC:
776 /*
777 * Run the external program hook on the message.
778 */
779
780 (void)snprintf(b, sizeof (b), "%s/%d", maildir_copy, msgnum + 1);
781 (void)ext_hook("add-hook", b, NULL);
782
783 if (aud)
784 fputs (charstring_buffer (scanl), aud);
785 if (noisy)
786 fflush (stdout);
787
788 msgnum++;
789 continue;
790 }
791 charstring_free (scanl);
792
793 /* If we get here there was some sort of error from scan(),
794 * so stop processing anything more from the spool.
795 */
796 break;
797 }
798 } else if (inc_type == INC_FILE) { /* Maildir inbox to process */
799 char *sp;
800 FILE *sf;
801 int i;
802
803 hghnum = msgnum = mp->hghmsg;
804 for (i = 0; i < num_maildir_entries; i++) {
805 charstring_t scanl = NULL;
806
807 msgnum++;
808
809 sp = Maildir[i].filename;
810 cp = mh_xstrdup(m_name (msgnum));
811 pf = NULL;
812 if (!trnflag || link(sp, cp) == -1) {
813 static char buf[65536];
814 size_t nrd;
815
816 if ((sf = fopen (sp, "r")) == NULL)
817 adios (sp, "unable to read for copy");
818 if ((pf = fopen (cp, "w+")) == NULL)
819 adios (cp, "unable to write for copy");
820 while ((nrd = fread(buf, 1, sizeof(buf), sf)) > 0)
821 if (fwrite(buf, 1, nrd, pf) != nrd)
822 break;
823 if (ferror(sf) || fflush(pf) || ferror(pf)) {
824 int e = errno;
825 fclose(pf); fclose(sf); (void) m_unlink(cp);
826 errno = e;
827 adios(cp, "copy error %s -> %s", sp, cp);
828 }
829 fclose (sf);
830 sf = NULL;
831 }
832 if (pf == NULL && (pf = fopen (cp, "r")) == NULL)
833 adios (cp, "not available");
834 chmod (cp, m_gmprot ());
835
836 fseek (pf, 0L, SEEK_SET);
837 switch (incerr = scan (pf, msgnum, 0, nfs, width,
838 msgnum == mp->hghmsg + 1 && chgflag,
839 1, NULL, stop - start, noisy, &scanl)) {
840 case SCNEOF:
841 printf ("%*d empty\n", DMAXFOLDER, msgnum);
842 break;
843
844 case SCNFAT:
845 trnflag = 0;
846 noisy++;
847 /* advise (cp, "unable to read"); already advised */
848 /* FALLTHRU */
849
850 case SCNERR:
851 case SCNNUM:
852 break;
853
854 case SCNMSG:
855 case SCNENC:
856 default:
857 /*
858 * Run the external program hook on the message.
859 */
860
861 (void)snprintf(b, sizeof (b), "%s/%d", maildir_copy, msgnum + 1);
862 (void)ext_hook("add-hook", b, NULL);
863
864 if (aud)
865 fputs (charstring_buffer (scanl), aud);
866 if (noisy)
867 fflush (stdout);
868 break;
869 }
870 charstring_free (scanl);
871
872 if (ferror(pf) || fclose (pf)) {
873 int e = errno;
874 (void) m_unlink (cp);
875 errno = e;
876 adios (cp, "write error on");
877 }
878 pf = NULL;
879 free (cp);
880
881 if (trnflag && m_unlink (sp) == NOTOK)
882 adios (sp, "couldn't unlink");
883 free (sp); /* Free Maildir[i]->filename */
884
885 scan_finished();
886 }
887 free (Maildir); /* From now on Maildir is just a flag - don't dref! */
888 }
889
890 scan_finished ();
891
892 if (incerr < 0) { /* error */
893 if (locked) {
894 GETGROUPPRIVS(); /* Be sure we can unlock mail file */
895 (void) lkfclosespool (in, newmail); in = NULL;
896 DROPGROUPPRIVS(); /* And then return us to normal privileges */
897 } else {
898 fclose (in); in = NULL;
899 }
900 adios (NULL, "failed");
901 }
902
903 if (aud)
904 fclose (aud);
905
906 if (noisy)
907 fflush (stdout);
908
909 if ((inc_type == INC_POP) && packfile)
910 done (0);
911
912 /*
913 * truncate file we are incorporating from
914 */
915 if (inc_type == INC_FILE && Maildir == NULL) {
916 if (trnflag) {
917 if (stat (newmail, &st) != NOTOK && s1.st_mtime != st.st_mtime)
918 inform("new messages have arrived!\007");
919 else {
920 int newfd;
921 if ((newfd = creat (newmail, 0600)) != NOTOK)
922 close (newfd);
923 else
924 admonish (newmail, "error zero'ing");
925 (void) m_unlink(map_name(newmail));
926 }
927 } else {
928 if (noisy)
929 printf ("%s not zero'd\n", newmail);
930 }
931 }
932
933 if (msgnum == hghnum) {
934 inform("no messages incorporated, continuing...");
935 } else {
936 /*
937 * Lock the sequence file now, and loop to set the right flags
938 * in the folder structure
939 */
940
941 struct msgs *mp2;
942 int i;
943
944 context_replace (pfolder, folder); /* update current folder */
945
946 if ((mp2 = folder_read(folder, 1)) == NULL) {
947 inform("Unable to reread folder %s, continuing...", folder);
948 goto skip;
949 }
950
951 /*
952 * Shouldn't happen, but just in case ...
953 */
954
955 if (msgnum >= mp2->hghoff
956 && !(mp2 = folder_realloc (mp2, mp2->lowoff, msgnum + 1))) {
957 inform("unable to reallocate folder storage");
958 goto skip;
959 }
960
961 if (chgflag)
962 mp2->curmsg = hghnum + 1;
963 mp2->hghmsg = msgnum;
964
965 if (mp2->lowmsg == 0)
966 mp2->lowmsg = 1;
967 if (chgflag) /* sigh... */
968 seq_setcur (mp2, mp2->curmsg);
969
970 for (i = hghnum + 1; i <= msgnum; i++) {
971 clear_msg_flags (mp2, i);
972 set_exists (mp2, i);
973 set_unseen (mp2, i);
974 }
975 mp2->msgflags |= SEQMOD;
976 seq_setunseen(mp2, 0); /* Set the Unseen-Sequence */
977 seq_save(mp2); /* Save the sequence file */
978 folder_free(mp2);
979 }
980 skip:
981
982 /*
983 * unlock the mail spool
984 */
985 if (inc_type == INC_FILE && Maildir == NULL) {
986 if (locked) {
987 GETGROUPPRIVS(); /* Be sure we can unlock mail file */
988 (void) lkfclosespool (in, newmail); in = NULL;
989 DROPGROUPPRIVS(); /* And then return us to normal privileges */
990 } else {
991 fclose (in); in = NULL;
992 }
993 }
994
995 context_save (); /* save the context file */
996 done (0);
997 return 1;
998 }
999
1000
1001 static void
1002 inc_done (int status)
1003 {
1004 done = exit;
1005 if (packfile && pd != NOTOK)
1006 mbx_close (packfile, pd);
1007 if (locked)
1008 {
1009 GETGROUPPRIVS();
1010 lkfclosespool(in, newmail);
1011 DROPGROUPPRIVS();
1012 }
1013 exit (status);
1014 }
1015
1016 static int
1017 pop_action (char *s)
1018 {
1019 fprintf (pf, "%s\n", s);
1020 stop += strlen (s) + 1;
1021 return 0; /* Is return value used? This was missing before 1999-07-15. */
1022 }
1023
1024 static int
1025 pop_pack (char *s)
1026 {
1027 int j;
1028 char buffer[BUFSIZ];
1029
1030 snprintf (buffer, sizeof(buffer), "%s\n", s);
1031 for ( ; (j = stringdex (mmdlm1, buffer)) >= 0; buffer[j]++)
1032 continue;
1033 for ( ; (j = stringdex (mmdlm2, buffer)) >= 0; buffer[j]++)
1034 continue;
1035 fputs (buffer, pf);
1036 size += strlen (buffer) + 1;
1037 return 0; /* Is return value used? This was missing before 1999-07-15. */
1038 }
1039
1040 static int
1041 map_count (void)
1042 {
1043 int md;
1044 char *cp;
1045 struct drop d;
1046 struct stat st;
1047
1048 if (stat (packfile, &st) == NOTOK)
1049 return 0;
1050 if ((md = open (cp = map_name (packfile), O_RDONLY)) == NOTOK
1051 || map_chk (cp, md, &d, (long) st.st_size, 1)) {
1052 if (md != NOTOK)
1053 close (md);
1054 return 0;
1055 }
1056 close (md);
1057 return (d.d_id);
1058 }