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