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