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