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