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