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