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