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