]> diplodocus.org Git - nmh/blob - uip/inc.c
Document argsplit changes in mh-profile man page.
[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)))
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 = lkfopen (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 * Check if we have enough message space for all the new
598 * messages. If not, then realloc the folder and add enough
599 * space for all new messages plus 10 additional slots.
600 */
601 if (mp->hghmsg + nmsgs >= mp->hghoff
602 && !(mp = folder_realloc (mp, mp->lowoff, mp->hghmsg + nmsgs + 10)))
603 adios (NULL, "unable to allocate folder storage");
604 }
605
606 for (i = 1; i <= nmsgs; i++) {
607 msgnum++;
608 if (packfile) {
609 fseek (pf, 0L, SEEK_CUR);
610 pos = ftell (pf);
611 size = 0;
612 fwrite (mmdlm1, 1, strlen (mmdlm1), pf);
613 start = ftell (pf);
614
615 if (pop_retr (i, pop_pack) == NOTOK)
616 adios (NULL, "%s", response);
617
618 fseek (pf, 0L, SEEK_CUR);
619 stop = ftell (pf);
620 if (fflush (pf))
621 adios (packfile, "write error on");
622 fseek (pf, start, SEEK_SET);
623 } else {
624 cp = getcpy (m_name (msgnum));
625 if ((pf = fopen (cp, "w+")) == NULL)
626 adios (cp, "unable to write");
627 chmod (cp, m_gmprot ());
628 start = stop = 0L;
629
630 if (pop_retr (i, pop_action) == NOTOK)
631 adios (NULL, "%s", response);
632
633 if (fflush (pf))
634 adios (cp, "write error on");
635 fseek (pf, 0L, SEEK_SET);
636 }
637 switch (incerr = scan (pf, msgnum, 0, nfs, width,
638 packfile ? 0 : msgnum == mp->hghmsg + 1 && chgflag,
639 1, NULL, stop - start, noisy)) {
640 case SCNEOF:
641 printf ("%*d empty\n", DMAXFOLDER, msgnum);
642 break;
643
644 case SCNFAT:
645 trnflag = 0;
646 noisy++;
647 /* advise (cp, "unable to read"); already advised */
648 /* fall thru */
649
650 case SCNERR:
651 case SCNNUM:
652 break;
653
654 case SCNMSG:
655 case SCNENC:
656 default:
657 if (aud)
658 fputs (scanl, aud);
659 if (noisy)
660 fflush (stdout);
661 if (!packfile) {
662 clear_msg_flags (mp, msgnum);
663 set_exists (mp, msgnum);
664 set_unseen (mp, msgnum);
665 mp->msgflags |= SEQMOD;
666 }
667 break;
668 }
669 if (packfile) {
670 fseek (pf, stop, SEEK_SET);
671 fwrite (mmdlm2, 1, strlen (mmdlm2), pf);
672 if (fflush (pf) || ferror (pf)) {
673 int e = errno;
674 pop_quit ();
675 errno = e;
676 adios (packfile, "write error on");
677 }
678 map_write (packfile, pd, 0, 0L, start, stop, pos, size, noisy);
679 } else {
680 if (ferror(pf) || fclose (pf)) {
681 int e = errno;
682 unlink (cp);
683 pop_quit ();
684 errno = e;
685 adios (cp, "write error on");
686 }
687 free (cp);
688 }
689
690 if (trnflag && pop_dele (i) == NOTOK)
691 adios (NULL, "%s", response);
692 }
693
694 if (pop_quit () == NOTOK)
695 adios (NULL, "%s", response);
696 if (packfile) {
697 mbx_close (packfile, pd);
698 pd = NOTOK;
699 }
700 }
701
702 /*
703 * Get the mail from file (usually mail spool)
704 */
705 if (inc_type == INC_FILE && Maildir == NULL) {
706 scan_detect_mbox_style (in); /* the MAGIC invocation... */
707 hghnum = msgnum = mp->hghmsg;
708 for (;;) {
709 /*
710 * Check if we need to allocate more space for message status.
711 * If so, then add space for an additional 100 messages.
712 */
713 if (msgnum >= mp->hghoff
714 && !(mp = folder_realloc (mp, mp->lowoff, mp->hghoff + 100))) {
715 advise (NULL, "unable to allocate folder storage");
716 incerr = NOTOK;
717 break;
718 }
719
720 /* create scanline for new message */
721 switch (incerr = scan (in, msgnum + 1, msgnum + 1, nfs, width,
722 msgnum == hghnum && chgflag, 1, NULL, 0L, noisy)) {
723 case SCNFAT:
724 case SCNEOF:
725 break;
726
727 case SCNERR:
728 if (aud)
729 fputs ("inc aborted!\n", aud);
730 advise (NULL, "aborted!"); /* doesn't clean up locks! */
731 break;
732
733 case SCNNUM:
734 advise (NULL, "BUG in %s, number out of range", invo_name);
735 break;
736
737 default:
738 advise (NULL, "BUG in %s, scan() botch (%d)", invo_name, incerr);
739 break;
740
741 case SCNMSG:
742 case SCNENC:
743 /*
744 * Run the external program hook on the message.
745 */
746
747 (void)snprintf(b, sizeof (b), "%s/%d", maildir_copy, msgnum + 1);
748 (void)ext_hook("add-hook", b, (char *)0);
749
750 if (aud)
751 fputs (scanl, aud);
752 if (noisy)
753 fflush (stdout);
754
755 msgnum++;
756 mp->hghmsg++;
757 mp->nummsg++;
758 if (mp->lowmsg == 0) mp->lowmsg = 1;
759
760 clear_msg_flags (mp, msgnum);
761 set_exists (mp, msgnum);
762 set_unseen (mp, msgnum);
763 mp->msgflags |= SEQMOD;
764 continue;
765 }
766 /* If we get here there was some sort of error from scan(),
767 * so stop processing anything more from the spool.
768 */
769 break;
770 }
771 } else if (inc_type == INC_FILE) { /* Maildir inbox to process */
772 char *sp;
773 FILE *sf;
774 int i;
775
776 hghnum = msgnum = mp->hghmsg;
777 for (i = 0; i < num_maildir_entries; i++) {
778 msgnum++;
779 /*
780 * Check if we need to allocate more space for message status.
781 * If so, then add space for an additional 100 messages.
782 */
783 if (msgnum >= mp->hghoff
784 && !(mp = folder_realloc (mp, mp->lowoff, mp->hghoff + 100))) {
785 advise (NULL, "unable to allocate folder storage");
786 incerr = NOTOK;
787 break;
788 }
789
790 sp = Maildir[i].filename;
791 cp = getcpy (m_name (msgnum));
792 pf = NULL;
793 if (!trnflag || link(sp, cp) == -1) {
794 static char buf[65536];
795 size_t nrd;
796
797 if ((sf = fopen (sp, "r")) == NULL)
798 adios (sp, "unable to read for copy");
799 if ((pf = fopen (cp, "w+")) == NULL)
800 adios (cp, "unable to write for copy");
801 while ((nrd = fread(buf, 1, sizeof(buf), sf)) > 0)
802 if (fwrite(buf, 1, nrd, pf) != nrd)
803 break;
804 if (ferror(sf) || fflush(pf) || ferror(pf)) {
805 int e = errno;
806 fclose(pf); fclose(sf); unlink(cp);
807 errno = e;
808 adios(cp, "copy error %s -> %s", sp, cp);
809 }
810 fclose (sf);
811 sf = NULL;
812 }
813 if (pf == NULL && (pf = fopen (cp, "r")) == NULL)
814 adios (cp, "not available");
815 chmod (cp, m_gmprot ());
816
817 fseek (pf, 0L, SEEK_SET);
818 switch (incerr = scan (pf, msgnum, 0, nfs, width,
819 msgnum == mp->hghmsg + 1 && chgflag,
820 1, NULL, stop - start, noisy)) {
821 case SCNEOF:
822 printf ("%*d empty\n", DMAXFOLDER, msgnum);
823 break;
824
825 case SCNFAT:
826 trnflag = 0;
827 noisy++;
828 /* advise (cp, "unable to read"); already advised */
829 /* fall thru */
830
831 case SCNERR:
832 case SCNNUM:
833 break;
834
835 case SCNMSG:
836 case SCNENC:
837 default:
838 /*
839 * Run the external program hook on the message.
840 */
841
842 (void)snprintf(b, sizeof (b), "%s/%d", maildir_copy, msgnum + 1);
843 (void)ext_hook("add-hook", b, (char *)0);
844
845 if (aud)
846 fputs (scanl, aud);
847 if (noisy)
848 fflush (stdout);
849 if (!packfile) {
850 clear_msg_flags (mp, msgnum);
851 set_exists (mp, msgnum);
852 set_unseen (mp, msgnum);
853 mp->msgflags |= SEQMOD;
854 }
855 break;
856 }
857 if (ferror(pf) || fclose (pf)) {
858 int e = errno;
859 unlink (cp);
860 errno = e;
861 adios (cp, "write error on");
862 }
863 pf = NULL;
864 free (cp);
865
866 if (trnflag && unlink (sp) == NOTOK)
867 adios (sp, "couldn't unlink");
868 free (sp); /* Free Maildir[i]->filename */
869 }
870 free (Maildir); /* From now on Maildir is just a flag - don't dref! */
871 }
872
873 scan_finished ();
874
875 if (incerr < 0) { /* error */
876 if (locked) {
877 GETGROUPPRIVS(); /* Be sure we can unlock mail file */
878 (void) lkfclose (in, newmail); in = NULL;
879 DROPGROUPPRIVS(); /* And then return us to normal privileges */
880 } else {
881 fclose (in); in = NULL;
882 }
883 adios (NULL, "failed");
884 }
885
886 if (aud)
887 fclose (aud);
888
889 if (noisy)
890 fflush (stdout);
891
892 if ((inc_type == INC_POP) && packfile)
893 done (0);
894
895 /*
896 * truncate file we are incorporating from
897 */
898 if (inc_type == INC_FILE && Maildir == NULL) {
899 if (trnflag) {
900 if (stat (newmail, &st) != NOTOK && s1.st_mtime != st.st_mtime)
901 advise (NULL, "new messages have arrived!\007");
902 else {
903 int newfd;
904 if ((newfd = creat (newmail, 0600)) != NOTOK)
905 close (newfd);
906 else
907 admonish (newmail, "error zero'ing");
908 unlink(map_name(newmail));
909 }
910 } else {
911 if (noisy)
912 printf ("%s not zero'd\n", newmail);
913 }
914 }
915
916 if (msgnum == hghnum) {
917 admonish (NULL, "no messages incorporated");
918 } else {
919 context_replace (pfolder, folder); /* update current folder */
920 if (chgflag)
921 mp->curmsg = hghnum + 1;
922 mp->hghmsg = msgnum;
923 if (mp->lowmsg == 0)
924 mp->lowmsg = 1;
925 if (chgflag) /* sigh... */
926 seq_setcur (mp, mp->curmsg);
927 }
928
929 /*
930 * unlock the mail spool
931 */
932 if (inc_type == INC_FILE && Maildir == NULL) {
933 if (locked) {
934 GETGROUPPRIVS(); /* Be sure we can unlock mail file */
935 (void) lkfclose (in, newmail); in = NULL;
936 DROPGROUPPRIVS(); /* And then return us to normal privileges */
937 } else {
938 fclose (in); in = NULL;
939 }
940 }
941
942 seq_setunseen (mp, 0); /* set the Unseen-Sequence */
943 seq_save (mp); /* synchronize sequences */
944 context_save (); /* save the context file */
945 done (0);
946 return 1;
947 }
948
949
950 static void
951 inc_done (int status)
952 {
953 if (packfile && pd != NOTOK)
954 mbx_close (packfile, pd);
955 if (locked)
956 {
957 GETGROUPPRIVS();
958 lkfclose(in, newmail);
959 DROPGROUPPRIVS();
960 }
961 exit (status);
962 }
963
964 static int
965 pop_action (char *s)
966 {
967 fprintf (pf, "%s\n", s);
968 stop += strlen (s) + 1;
969 return 0; /* Is return value used? This was missing before 1999-07-15. */
970 }
971
972 static int
973 pop_pack (char *s)
974 {
975 int j;
976 char buffer[BUFSIZ];
977
978 snprintf (buffer, sizeof(buffer), "%s\n", s);
979 for (j = 0; (j = stringdex (mmdlm1, buffer)) >= 0; buffer[j]++)
980 continue;
981 for (j = 0; (j = stringdex (mmdlm2, buffer)) >= 0; buffer[j]++)
982 continue;
983 fputs (buffer, pf);
984 size += strlen (buffer) + 1;
985 return 0; /* Is return value used? This was missing before 1999-07-15. */
986 }
987
988 static int
989 map_count (void)
990 {
991 int md;
992 char *cp;
993 struct drop d;
994 struct stat st;
995
996 if (stat (packfile, &st) == NOTOK)
997 return 0;
998 if ((md = open (cp = map_name (packfile), O_RDONLY)) == NOTOK
999 || map_chk (cp, md, &d, (long) st.st_size, 1)) {
1000 if (md != NOTOK)
1001 close (md);
1002 return 0;
1003 }
1004 close (md);
1005 return (d.d_id);
1006 }