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