]> diplodocus.org Git - nmh/blob - uip/inc.c
add rmf(1) and folder(1) to one another's SEE ALSO sections
[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 if (nmh_init(argv[0], 1)) { return 1; }
201
202 mts_init (invo_name);
203 arguments = getarguments (invo_name, argc, argv, 1);
204 argp = arguments;
205
206 /*
207 * Scheme is:
208 * use MAILHOST environment variable if present,
209 * else try Hesiod.
210 * If that fails, use the default (if any)
211 * provided by mts.conf in mts_init()
212 */
213 if ((MAILHOST_env_variable = getenv("MAILHOST")) != NULL)
214 pophost = MAILHOST_env_variable;
215 /*
216 * If there is a valid "pophost" entry in mts.conf,
217 * then use it as the default host.
218 */
219 if (pophost && *pophost)
220 host = pophost;
221
222 while ((cp = *argp++)) {
223 if (*cp == '-') {
224 switch (smatch (++cp, switches)) {
225 case AMBIGSW:
226 ambigsw (cp, switches);
227 done (1);
228 case UNKWNSW:
229 adios (NULL, "-%s unknown", cp);
230
231 case HELPSW:
232 snprintf (buf, sizeof(buf), "%s [+folder] [switches]", invo_name);
233 print_help (buf, switches, 1);
234 done (0);
235 case VERSIONSW:
236 print_version(invo_name);
237 done (0);
238
239 case AUDSW:
240 if (!(cp = *argp++) || *cp == '-')
241 adios (NULL, "missing argument to %s", argp[-2]);
242 audfile = getcpy (m_maildir (cp));
243 continue;
244 case NAUDSW:
245 audfile = NULL;
246 continue;
247
248 case CHGSW:
249 chgflag++;
250 continue;
251 case NCHGSW:
252 chgflag = 0;
253 continue;
254
255 /*
256 * The flag `trnflag' has the value:
257 *
258 * 2 if -truncate is given
259 * 1 by default (truncating is default)
260 * 0 if -notruncate is given
261 */
262 case TRNCSW:
263 trnflag = 2;
264 continue;
265 case NTRNCSW:
266 trnflag = 0;
267 continue;
268
269 case FILESW:
270 if (!(cp = *argp++) || *cp == '-')
271 adios (NULL, "missing argument to %s", argp[-2]);
272 from = path (cp, TFILE);
273
274 /*
275 * If the truncate file is in default state,
276 * change to not truncate.
277 */
278 if (trnflag == 1)
279 trnflag = 0;
280 continue;
281
282 case SILSW:
283 noisy = 0;
284 continue;
285 case NSILSW:
286 noisy++;
287 continue;
288
289 case FORMSW:
290 if (!(form = *argp++) || *form == '-')
291 adios (NULL, "missing argument to %s", argp[-2]);
292 format = NULL;
293 continue;
294 case FMTSW:
295 if (!(format = *argp++) || *format == '-')
296 adios (NULL, "missing argument to %s", argp[-2]);
297 form = NULL;
298 continue;
299
300 case WIDTHSW:
301 if (!(cp = *argp++) || *cp == '-')
302 adios (NULL, "missing argument to %s", argp[-2]);
303 width = atoi (cp);
304 continue;
305
306 case HOSTSW:
307 if (!(host = *argp++) || *host == '-')
308 adios (NULL, "missing argument to %s", argp[-2]);
309 continue;
310
311 case PORTSW:
312 if (!(port = *argp++) || *port == '-')
313 adios (NULL, "missing argument to %s", argp[-2]);
314 continue;
315
316 case USERSW:
317 if (!(user = *argp++) || *user == '-')
318 adios (NULL, "missing argument to %s", argp[-2]);
319 continue;
320
321 case PACKSW:
322 if (!(packfile = *argp++) || *packfile == '-')
323 adios (NULL, "missing argument to %s", argp[-2]);
324 continue;
325 case NPACKSW:
326 packfile = NULL;
327 continue;
328
329 case SNOOPSW:
330 snoop++;
331 continue;
332
333 case SASLSW:
334 sasl++;
335 continue;
336 case NOSASLSW:
337 sasl = 0;
338 continue;
339
340 case SASLMECHSW:
341 if (!(saslmech = *argp++) || *saslmech == '-')
342 adios (NULL, "missing argument to %s", argp[-2]);
343 continue;
344 case PROXYSW:
345 if (!(proxy = *argp++) || *proxy == '-')
346 adios (NULL, "missing argument to %s", argp[-2]);
347 continue;
348 }
349 }
350 if (*cp == '+' || *cp == '@') {
351 if (folder)
352 adios (NULL, "only one folder at a time!");
353 else
354 folder = pluspath (cp);
355 } else {
356 adios (NULL, "usage: %s [+folder] [switches]", invo_name);
357 }
358 }
359
360 /* NOTE: above this point you should use TRYDROPGROUPPRIVS(),
361 * not DROPGROUPPRIVS().
362 */
363 if (host && !*host)
364 host = NULL;
365
366 /* guarantee dropping group priveleges; we might not have done so earlier */
367 DROPGROUPPRIVS();
368
369 /*
370 * Where are we getting the new mail?
371 */
372 if (from)
373 inc_type = INC_FILE;
374 else if (host)
375 inc_type = INC_POP;
376 else
377 inc_type = INC_FILE;
378
379 /*
380 * Are we getting the mail from
381 * a POP server?
382 */
383 if (inc_type == INC_POP) {
384 struct nmh_creds creds = { 0, 0, 0 };
385
386 /*
387 * initialize POP connection
388 */
389 nmh_get_credentials (host, user, sasl, &creds);
390 if (pop_init (host, port, creds.user, creds.password, proxy, snoop,
391 sasl, saslmech) == NOTOK)
392 adios (NULL, "%s", response);
393
394 /* Check if there are any messages */
395 if (pop_stat (&nmsgs, &nbytes) == NOTOK)
396 adios (NULL, "%s", response);
397
398 if (nmsgs == 0) {
399 pop_quit();
400 adios (NULL, "no mail to incorporate");
401 }
402 }
403
404 /*
405 * We will get the mail from a file
406 * (typically the standard maildrop)
407 */
408
409 if (inc_type == INC_FILE) {
410 if (from)
411 newmail = from;
412 else if ((newmail = getenv ("MAILDROP")) && *newmail)
413 newmail = m_mailpath (newmail);
414 else if ((newmail = context_find ("maildrop")) && *newmail)
415 newmail = m_mailpath (newmail);
416 else {
417 newmail = concat (MAILDIR, "/", MAILFIL, NULL);
418 }
419 if (stat (newmail, &s1) == NOTOK || s1.st_size == 0)
420 adios (NULL, "no mail to incorporate");
421 if (s1.st_mode & S_IFDIR) {
422 DIR *md;
423 struct dirent *de;
424 struct stat ms;
425 int i;
426 i = 0;
427 cp = concat (newmail, "/new", NULL);
428 if ((md = opendir(cp)) == NULL)
429 adios (NULL, "unable to open %s", cp);
430 while ((de = readdir (md)) != NULL) {
431 if (de->d_name[0] == '.')
432 continue;
433 if (i >= num_maildir_entries) {
434 if ((Maildir = realloc(Maildir, sizeof(*Maildir) * (2*i+16))) == NULL)
435 adios(NULL, "not enough memory for %d messages", 2*i+16);
436 num_maildir_entries = 2*i+16;
437 }
438 Maildir[i].filename = concat (cp, "/", de->d_name, NULL);
439 if (stat(Maildir[i].filename, &ms) != 0)
440 adios (Maildir[i].filename, "couldn't get delivery time");
441 Maildir[i].mtime = ms.st_mtime;
442 i++;
443 }
444 free (cp);
445 closedir (md);
446 cp = concat (newmail, "/cur", NULL);
447 if ((md = opendir(cp)) == NULL)
448 adios (NULL, "unable to open %s", cp);
449 while ((de = readdir (md)) != NULL) {
450 if (de->d_name[0] == '.')
451 continue;
452 if (i >= num_maildir_entries) {
453 if ((Maildir = realloc(Maildir, sizeof(*Maildir) * (2*i+16))) == NULL)
454 adios(NULL, "not enough memory for %d messages", 2*i+16);
455 num_maildir_entries = 2*i+16;
456 }
457 Maildir[i].filename = concat (cp, "/", de->d_name, NULL);
458 if (stat(Maildir[i].filename, &ms) != 0)
459 adios (Maildir[i].filename, "couldn't get delivery time");
460 Maildir[i].mtime = ms.st_mtime;
461 i++;
462 }
463 free (cp);
464 closedir (md);
465 if (i == 0)
466 adios (NULL, "no mail to incorporate");
467 num_maildir_entries = i;
468 qsort (Maildir, num_maildir_entries, sizeof(*Maildir), maildir_srt);
469 }
470
471 if ((cp = strdup(newmail)) == (char *)0)
472 adios (NULL, "error allocating memory to copy newmail");
473
474 newmail = cp;
475 }
476
477 /* skip the folder setup */
478 if ((inc_type == INC_POP) && packfile)
479 goto go_to_it;
480
481 if (!context_find ("path"))
482 free (path ("./", TFOLDER));
483 if (!folder)
484 folder = getfolder (0);
485 maildir = m_maildir (folder);
486
487 if ((maildir_copy = strdup(maildir)) == (char *)0)
488 adios (maildir, "error allocating memory to copy maildir");
489
490 if (!folder_exists(maildir)) {
491 /* If the folder doesn't exist, and we're given the -silent flag,
492 * just fail.
493 */
494 if (noisy)
495 create_folder(maildir, 0, done);
496 else
497 done (1);
498 }
499
500 if (chdir (maildir) == NOTOK)
501 adios (maildir, "unable to change directory to");
502
503 /* read folder and create message structure */
504 if (!(mp = folder_read (folder, 0)))
505 adios (NULL, "unable to read folder %s", folder);
506
507 go_to_it:
508
509 if (inc_type == INC_FILE && Maildir == NULL) {
510 if (access (newmail, W_OK) != NOTOK) {
511 locked++;
512 if (trnflag) {
513 SIGNAL (SIGHUP, SIG_IGN);
514 SIGNAL (SIGINT, SIG_IGN);
515 SIGNAL (SIGQUIT, SIG_IGN);
516 SIGNAL (SIGTERM, SIG_IGN);
517 }
518
519 GETGROUPPRIVS(); /* Reset gid to lock mail file */
520 in = lkfopenspool (newmail, "r");
521 DROPGROUPPRIVS();
522 if (in == NULL)
523 adios (NULL, "unable to lock and fopen %s", newmail);
524 fstat (fileno(in), &s1);
525 } else {
526 trnflag = 0;
527 if ((in = fopen (newmail, "r")) == NULL)
528 adios (newmail, "unable to read");
529 }
530 }
531
532 /* This shouldn't be necessary but it can't hurt. */
533 DROPGROUPPRIVS();
534
535 if (audfile) {
536 int i;
537 if ((i = stat (audfile, &st)) == NOTOK)
538 advise (NULL, "Creating Receive-Audit: %s", audfile);
539 if ((aud = fopen (audfile, "a")) == NULL)
540 adios (audfile, "unable to append to");
541 else if (i == NOTOK)
542 chmod (audfile, m_gmprot ());
543
544 if (from)
545 fprintf (aud, "<<inc>> %s -ms %s\n", dtimenow(0), from);
546 else {
547 if (host)
548 fprintf (aud, "<<inc>> %s -host %s -user %s\n", dtimenow(0),
549 host, user);
550 else
551 fprintf (aud, "<<inc>> %s\n", dtimenow (0));
552 }
553 }
554
555 /* Get new format string */
556 nfs = new_fs (form, format, FORMAT);
557
558 if (noisy) {
559 printf ("Incorporating new mail into %s...\n\n", folder);
560 fflush (stdout);
561 }
562
563
564 /*
565 * Get the mail from a POP server
566 */
567 if (inc_type == INC_POP) {
568 int i;
569 if (packfile) {
570 packfile = path (packfile, TFILE);
571 if (stat (packfile, &st) == NOTOK) {
572 if (errno != ENOENT)
573 adios (packfile, "error on file");
574 cp = concat ("Create file \"", packfile, "\"? ", NULL);
575 if (noisy && !getanswer (cp))
576 done (1);
577 free (cp);
578 }
579 msgnum = map_count ();
580 if ((pd = mbx_open (packfile, mbx_style, getuid(), getgid(), m_gmprot()))
581 == NOTOK)
582 adios (packfile, "unable to open");
583 if ((pf = fdopen (pd, "w+")) == NULL)
584 adios (NULL, "unable to fdopen %s", packfile);
585 } else {
586 hghnum = msgnum = mp->hghmsg;
587 }
588
589 for (i = 1; i <= nmsgs; i++) {
590 msgnum++;
591 if (packfile) {
592 fseek (pf, 0L, SEEK_CUR);
593 pos = ftell (pf);
594 size = 0;
595 fwrite (mmdlm1, 1, strlen (mmdlm1), pf);
596 start = ftell (pf);
597
598 if (pop_retr (i, pop_pack) == NOTOK)
599 adios (NULL, "%s", response);
600
601 fseek (pf, 0L, SEEK_CUR);
602 stop = ftell (pf);
603 if (fflush (pf))
604 adios (packfile, "write error on");
605 fseek (pf, start, SEEK_SET);
606 } else {
607 cp = getcpy (m_name (msgnum));
608 if ((pf = fopen (cp, "w+")) == NULL)
609 adios (cp, "unable to write");
610 chmod (cp, m_gmprot ());
611 start = stop = 0L;
612
613 if (pop_retr (i, pop_action) == NOTOK)
614 adios (NULL, "%s", response);
615
616 if (fflush (pf))
617 adios (cp, "write error on");
618 fseek (pf, 0L, SEEK_SET);
619 }
620 switch (incerr = scan (pf, msgnum, 0, nfs, width,
621 packfile ? 0 : msgnum == mp->hghmsg + 1 && chgflag,
622 1, NULL, stop - start, noisy)) {
623 case SCNEOF:
624 printf ("%*d empty\n", DMAXFOLDER, msgnum);
625 break;
626
627 case SCNFAT:
628 trnflag = 0;
629 noisy++;
630 /* advise (cp, "unable to read"); already advised */
631 /* fall thru */
632
633 case SCNERR:
634 case SCNNUM:
635 break;
636
637 case SCNMSG:
638 case SCNENC:
639 default:
640 if (aud)
641 fputs (scanl, aud);
642 if (noisy)
643 fflush (stdout);
644 break;
645 }
646 if (packfile) {
647 fseek (pf, stop, SEEK_SET);
648 fwrite (mmdlm2, 1, strlen (mmdlm2), pf);
649 if (fflush (pf) || ferror (pf)) {
650 int e = errno;
651 pop_quit ();
652 errno = e;
653 adios (packfile, "write error on");
654 }
655 map_write (packfile, pd, 0, 0L, start, stop, pos, size, noisy);
656 } else {
657 if (ferror(pf) || fclose (pf)) {
658 int e = errno;
659 (void) m_unlink (cp);
660 pop_quit ();
661 errno = e;
662 adios (cp, "write error on");
663 }
664 free (cp);
665 }
666
667 if (trnflag && pop_dele (i) == NOTOK)
668 adios (NULL, "%s", response);
669 }
670
671 if (pop_quit () == NOTOK)
672 adios (NULL, "%s", response);
673 if (packfile) {
674 mbx_close (packfile, pd);
675 pd = NOTOK;
676 }
677 }
678
679 /*
680 * Get the mail from file (usually mail spool)
681 */
682 if (inc_type == INC_FILE && Maildir == NULL) {
683 scan_detect_mbox_style (in); /* the MAGIC invocation... */
684 hghnum = msgnum = mp->hghmsg;
685 for (;;) {
686 /* create scanline for new message */
687 switch (incerr = scan (in, msgnum + 1, msgnum + 1, nfs, width,
688 msgnum == hghnum && chgflag, 1, NULL, 0L, noisy)) {
689 case SCNFAT:
690 case SCNEOF:
691 break;
692
693 case SCNERR:
694 if (aud)
695 fputs ("inc aborted!\n", aud);
696 advise (NULL, "aborted!"); /* doesn't clean up locks! */
697 break;
698
699 case SCNNUM:
700 advise (NULL, "BUG in %s, number out of range", invo_name);
701 break;
702
703 default:
704 advise (NULL, "BUG in %s, scan() botch (%d)", invo_name, incerr);
705 break;
706
707 case SCNMSG:
708 case SCNENC:
709 /*
710 * Run the external program hook on the message.
711 */
712
713 (void)snprintf(b, sizeof (b), "%s/%d", maildir_copy, msgnum + 1);
714 (void)ext_hook("add-hook", b, (char *)0);
715
716 if (aud)
717 fputs (scanl, aud);
718 if (noisy)
719 fflush (stdout);
720
721 msgnum++;
722 continue;
723 }
724 /* If we get here there was some sort of error from scan(),
725 * so stop processing anything more from the spool.
726 */
727 break;
728 }
729 } else if (inc_type == INC_FILE) { /* Maildir inbox to process */
730 char *sp;
731 FILE *sf;
732 int i;
733
734 hghnum = msgnum = mp->hghmsg;
735 for (i = 0; i < num_maildir_entries; i++) {
736 msgnum++;
737
738 sp = Maildir[i].filename;
739 cp = getcpy (m_name (msgnum));
740 pf = NULL;
741 if (!trnflag || link(sp, cp) == -1) {
742 static char buf[65536];
743 size_t nrd;
744
745 if ((sf = fopen (sp, "r")) == NULL)
746 adios (sp, "unable to read for copy");
747 if ((pf = fopen (cp, "w+")) == NULL)
748 adios (cp, "unable to write for copy");
749 while ((nrd = fread(buf, 1, sizeof(buf), sf)) > 0)
750 if (fwrite(buf, 1, nrd, pf) != nrd)
751 break;
752 if (ferror(sf) || fflush(pf) || ferror(pf)) {
753 int e = errno;
754 fclose(pf); fclose(sf); (void) m_unlink(cp);
755 errno = e;
756 adios(cp, "copy error %s -> %s", sp, cp);
757 }
758 fclose (sf);
759 sf = NULL;
760 }
761 if (pf == NULL && (pf = fopen (cp, "r")) == NULL)
762 adios (cp, "not available");
763 chmod (cp, m_gmprot ());
764
765 fseek (pf, 0L, SEEK_SET);
766 switch (incerr = scan (pf, msgnum, 0, nfs, width,
767 msgnum == mp->hghmsg + 1 && chgflag,
768 1, NULL, stop - start, noisy)) {
769 case SCNEOF:
770 printf ("%*d empty\n", DMAXFOLDER, msgnum);
771 break;
772
773 case SCNFAT:
774 trnflag = 0;
775 noisy++;
776 /* advise (cp, "unable to read"); already advised */
777 /* fall thru */
778
779 case SCNERR:
780 case SCNNUM:
781 break;
782
783 case SCNMSG:
784 case SCNENC:
785 default:
786 /*
787 * Run the external program hook on the message.
788 */
789
790 (void)snprintf(b, sizeof (b), "%s/%d", maildir_copy, msgnum + 1);
791 (void)ext_hook("add-hook", b, (char *)0);
792
793 if (aud)
794 fputs (scanl, aud);
795 if (noisy)
796 fflush (stdout);
797 break;
798 }
799 if (ferror(pf) || fclose (pf)) {
800 int e = errno;
801 (void) m_unlink (cp);
802 errno = e;
803 adios (cp, "write error on");
804 }
805 pf = NULL;
806 free (cp);
807
808 if (trnflag && m_unlink (sp) == NOTOK)
809 adios (sp, "couldn't unlink");
810 free (sp); /* Free Maildir[i]->filename */
811 }
812 free (Maildir); /* From now on Maildir is just a flag - don't dref! */
813 }
814
815 scan_finished ();
816
817 if (incerr < 0) { /* error */
818 if (locked) {
819 GETGROUPPRIVS(); /* Be sure we can unlock mail file */
820 (void) lkfclosespool (in, newmail); in = NULL;
821 DROPGROUPPRIVS(); /* And then return us to normal privileges */
822 } else {
823 fclose (in); in = NULL;
824 }
825 adios (NULL, "failed");
826 }
827
828 if (aud)
829 fclose (aud);
830
831 if (noisy)
832 fflush (stdout);
833
834 if ((inc_type == INC_POP) && packfile)
835 done (0);
836
837 /*
838 * truncate file we are incorporating from
839 */
840 if (inc_type == INC_FILE && Maildir == NULL) {
841 if (trnflag) {
842 if (stat (newmail, &st) != NOTOK && s1.st_mtime != st.st_mtime)
843 advise (NULL, "new messages have arrived!\007");
844 else {
845 int newfd;
846 if ((newfd = creat (newmail, 0600)) != NOTOK)
847 close (newfd);
848 else
849 admonish (newmail, "error zero'ing");
850 (void) m_unlink(map_name(newmail));
851 }
852 } else {
853 if (noisy)
854 printf ("%s not zero'd\n", newmail);
855 }
856 }
857
858 if (msgnum == hghnum) {
859 admonish (NULL, "no messages incorporated");
860 } else {
861 /*
862 * Lock the sequence file now, and loop to set the right flags
863 * in the folder structure
864 */
865
866 struct msgs *mp2;
867 int i;
868
869 context_replace (pfolder, folder); /* update current folder */
870
871 if ((mp2 = folder_read(folder, 1)) == NULL) {
872 admonish(NULL, "Unable to reread folder %s", folder);
873 goto skip;
874 }
875
876 /*
877 * Shouldn't happen, but just in case ...
878 */
879
880 if (msgnum >= mp2->hghoff
881 && !(mp2 = folder_realloc (mp2, mp2->lowoff, msgnum + 1))) {
882 advise (NULL, "unable to reallocate folder storage");
883 goto skip;
884 }
885
886 if (chgflag)
887 mp2->curmsg = hghnum + 1;
888 mp2->hghmsg = msgnum;
889
890 if (mp2->lowmsg == 0)
891 mp2->lowmsg = 1;
892 if (chgflag) /* sigh... */
893 seq_setcur (mp2, mp2->curmsg);
894
895 for (i = hghnum + 1; i <= msgnum; i++) {
896 clear_msg_flags (mp2, i);
897 set_exists (mp2, i);
898 set_unseen (mp2, i);
899 }
900 mp2->msgflags |= SEQMOD;
901 seq_setunseen(mp2, 0); /* Set the Unseen-Sequence */
902 seq_save(mp2); /* Save the sequence file */
903 folder_free(mp2);
904 }
905 skip:
906
907 /*
908 * unlock the mail spool
909 */
910 if (inc_type == INC_FILE && Maildir == NULL) {
911 if (locked) {
912 GETGROUPPRIVS(); /* Be sure we can unlock mail file */
913 (void) lkfclosespool (in, newmail); in = NULL;
914 DROPGROUPPRIVS(); /* And then return us to normal privileges */
915 } else {
916 fclose (in); in = NULL;
917 }
918 }
919
920 context_save (); /* save the context file */
921 done (0);
922 return 1;
923 }
924
925
926 static void
927 inc_done (int status)
928 {
929 if (packfile && pd != NOTOK)
930 mbx_close (packfile, pd);
931 if (locked)
932 {
933 GETGROUPPRIVS();
934 lkfclosespool(in, newmail);
935 DROPGROUPPRIVS();
936 }
937 exit (status);
938 }
939
940 static int
941 pop_action (char *s)
942 {
943 fprintf (pf, "%s\n", s);
944 stop += strlen (s) + 1;
945 return 0; /* Is return value used? This was missing before 1999-07-15. */
946 }
947
948 static int
949 pop_pack (char *s)
950 {
951 int j;
952 char buffer[BUFSIZ];
953
954 snprintf (buffer, sizeof(buffer), "%s\n", s);
955 for (j = 0; (j = stringdex (mmdlm1, buffer)) >= 0; buffer[j]++)
956 continue;
957 for (j = 0; (j = stringdex (mmdlm2, buffer)) >= 0; buffer[j]++)
958 continue;
959 fputs (buffer, pf);
960 size += strlen (buffer) + 1;
961 return 0; /* Is return value used? This was missing before 1999-07-15. */
962 }
963
964 static int
965 map_count (void)
966 {
967 int md;
968 char *cp;
969 struct drop d;
970 struct stat st;
971
972 if (stat (packfile, &st) == NOTOK)
973 return 0;
974 if ((md = open (cp = map_name (packfile), O_RDONLY)) == NOTOK
975 || map_chk (cp, md, &d, (long) st.st_size, 1)) {
976 if (md != NOTOK)
977 close (md);
978 return 0;
979 }
980 close (md);
981 return (d.d_id);
982 }