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