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