]> diplodocus.org Git - nmh/blob - uip/inc.c
restored use of PUTDF() macro; was broken in versions 1.10-1.12;
[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
248 #ifdef POP
249 int nmsgs, nbytes, p = 0;
250 char *pass = NULL;
251 char *MAILHOST_env_variable;
252 #endif
253
254 #ifdef MHE
255 FILE *mhe = NULL;
256 #endif
257
258 #ifdef HESIOD
259 struct hes_postoffice *po;
260 #endif
261
262 /* absolutely the first thing we do is save our privileges,
263 * and drop them if we can.
264 */
265 SAVEGROUPPRIVS();
266 TRYDROPGROUPPRIVS();
267
268 #ifdef LOCALE
269 setlocale(LC_ALL, "");
270 #endif
271 invo_name = r1bindex (argv[0], '/');
272
273 /* read user profile/context */
274 context_read();
275
276 mts_init (invo_name);
277 arguments = getarguments (invo_name, argc, argv, 1);
278 argp = arguments;
279
280 #ifdef POP
281 /*
282 * Scheme is:
283 * use MAILHOST environment variable if present,
284 * else try Hesiod.
285 * If that fails, use the default (if any)
286 * provided by mts.conf in mts_init()
287 */
288 if ((MAILHOST_env_variable = getenv("MAILHOST")) != NULL)
289 pophost = MAILHOST_env_variable;
290 # ifdef HESIOD
291 else if ((po = hes_getmailhost(getusername())) != NULL &&
292 strcmp(po->po_type, "POP") == 0)
293 pophost = po->po_host;
294 # endif /* HESIOD */
295 /*
296 * If there is a valid "pophost" entry in mts.conf,
297 * then use it as the default host.
298 */
299 if (pophost && *pophost)
300 host = pophost;
301
302 if ((cp = getenv ("MHPOPDEBUG")) && *cp)
303 snoop++;
304 #endif /* POP */
305
306 rpop = 0;
307
308 while ((cp = *argp++)) {
309 if (*cp == '-') {
310 switch (smatch (++cp, switches)) {
311 case AMBIGSW:
312 ambigsw (cp, switches);
313 done (1);
314 case UNKWNSW:
315 adios (NULL, "-%s unknown", cp);
316
317 case HELPSW:
318 snprintf (buf, sizeof(buf), "%s [+folder] [switches]", invo_name);
319 print_help (buf, switches, 1);
320 done (1);
321 case VERSIONSW:
322 print_version(invo_name);
323 done (1);
324
325 case AUDSW:
326 if (!(cp = *argp++) || *cp == '-')
327 adios (NULL, "missing argument to %s", argp[-2]);
328 audfile = getcpy (m_maildir (cp));
329 continue;
330 case NAUDSW:
331 audfile = NULL;
332 continue;
333
334 case CHGSW:
335 chgflag++;
336 continue;
337 case NCHGSW:
338 chgflag = 0;
339 continue;
340
341 /*
342 * The flag `trnflag' has the value:
343 *
344 * 2 if -truncate is given
345 * 1 by default (truncating is default)
346 * 0 if -notruncate is given
347 */
348 case TRNCSW:
349 trnflag = 2;
350 continue;
351 case NTRNCSW:
352 trnflag = 0;
353 continue;
354
355 case FILESW:
356 if (!(cp = *argp++) || *cp == '-')
357 adios (NULL, "missing argument to %s", argp[-2]);
358 from = path (cp, TFILE);
359
360 /*
361 * If the truncate file is in default state,
362 * change to not truncate.
363 */
364 if (trnflag == 1)
365 trnflag = 0;
366 continue;
367
368 case SILSW:
369 noisy = 0;
370 continue;
371 case NSILSW:
372 noisy++;
373 continue;
374
375 case FORMSW:
376 if (!(form = *argp++) || *form == '-')
377 adios (NULL, "missing argument to %s", argp[-2]);
378 format = NULL;
379 continue;
380 case FMTSW:
381 if (!(format = *argp++) || *format == '-')
382 adios (NULL, "missing argument to %s", argp[-2]);
383 form = NULL;
384 continue;
385
386 case WIDTHSW:
387 if (!(cp = *argp++) || *cp == '-')
388 adios (NULL, "missing argument to %s", argp[-2]);
389 width = atoi (cp);
390 continue;
391
392 case HOSTSW:
393 if (!(host = *argp++) || *host == '-')
394 adios (NULL, "missing argument to %s", argp[-2]);
395 continue;
396 case USERSW:
397 if (!(user = *argp++) || *user == '-')
398 adios (NULL, "missing argument to %s", argp[-2]);
399 continue;
400
401 case PACKSW:
402 #ifndef POP
403 if (!(cp = *argp++) || *cp == '-')
404 adios (NULL, "missing argument to %s", argp[-2]);
405 #else /* POP */
406 if (!(packfile = *argp++) || *packfile == '-')
407 adios (NULL, "missing argument to %s", argp[-2]);
408 #endif /* POP */
409 continue;
410 case NPACKSW:
411 #ifdef POP
412 packfile = NULL;
413 #endif /* POP */
414 continue;
415
416 case APOPSW:
417 rpop = -1;
418 continue;
419 case NAPOPSW:
420 rpop = 0;
421 continue;
422
423 case RPOPSW:
424 rpop = 1;
425 continue;
426 case NRPOPSW:
427 rpop = 0;
428 continue;
429
430 case KPOPSW:
431 kpop = 1;
432 continue;
433
434 case SNOOPSW:
435 snoop++;
436 continue;
437
438 case SASLSW:
439 sasl++;
440 continue;
441
442 case SASLMECHSW:
443 if (!(saslmech = *argp++) || *saslmech == '-')
444 adios (NULL, "missing argument to %s", argp[-2]);
445 continue;
446 }
447 }
448 if (*cp == '+' || *cp == '@') {
449 if (folder)
450 adios (NULL, "only one folder at a time!");
451 else
452 folder = path (cp + 1, *cp == '+' ? TFOLDER : TSUBCWF);
453 } else {
454 adios (NULL, "usage: %s [+folder] [switches]", invo_name);
455 }
456 }
457
458 /* NOTE: above this point you should use TRYDROPGROUPPRIVS(),
459 * not DROPGROUPPRIVS().
460 */
461 #ifdef POP
462 if (host && !*host)
463 host = NULL;
464 if (from || !host || rpop <= 0)
465 DROPUSERPRIVS();
466 #endif /* POP */
467
468 /* guarantee dropping group priveleges; we might not have done so earlier */
469 DROPGROUPPRIVS();
470
471 /*
472 * Where are we getting the new mail?
473 */
474 if (from)
475 inc_type = INC_FILE;
476 #ifdef POP
477 else if (host)
478 inc_type = INC_POP;
479 #endif
480 else
481 inc_type = INC_FILE;
482
483 #ifdef POP
484 /*
485 * Are we getting the mail from
486 * a POP server?
487 */
488 if (inc_type == INC_POP) {
489 if (user == NULL)
490 user = getusername ();
491 if ( strcmp( POPSERVICE, "kpop" ) == 0 ) {
492 kpop = 1;
493 }
494 if (kpop || sasl || ( rpop > 0))
495 pass = getusername ();
496 else
497 ruserpass (host, &user, &pass);
498
499 /*
500 * initialize POP connection
501 */
502 if (pop_init (host, user, pass, snoop, kpop ? 1 : rpop, kpop,
503 sasl, saslmech) == NOTOK)
504 adios (NULL, "%s", response);
505
506 /* Check if there are any messages */
507 if (pop_stat (&nmsgs, &nbytes) == NOTOK)
508 adios (NULL, "%s", response);
509
510 if (rpop > 0)
511 DROPUSERPRIVS();
512 if (nmsgs == 0) {
513 pop_quit();
514 adios (NULL, "no mail to incorporate");
515 }
516 }
517 #endif /* POP */
518
519 /*
520 * We will get the mail from a file
521 * (typically the standard maildrop)
522 */
523
524 if (inc_type == INC_FILE) {
525 if (from)
526 newmail = from;
527 else if ((newmail = getenv ("MAILDROP")) && *newmail)
528 newmail = m_mailpath (newmail);
529 else if ((newmail = context_find ("maildrop")) && *newmail)
530 newmail = m_mailpath (newmail);
531 else {
532 newmail = concat (MAILDIR, "/", MAILFIL, NULL);
533 }
534 if (stat (newmail, &s1) == NOTOK || s1.st_size == 0)
535 adios (NULL, "no mail to incorporate");
536 }
537
538 #ifdef POP
539 /* skip the folder setup */
540 if ((inc_type == INC_POP) && packfile)
541 goto go_to_it;
542 #endif /* POP */
543
544 if (!context_find ("path"))
545 free (path ("./", TFOLDER));
546 if (!folder)
547 folder = getfolder (0);
548 maildir = m_maildir (folder);
549
550 if (stat (maildir, &st) == NOTOK) {
551 if (errno != ENOENT)
552 adios (maildir, "error on folder");
553 cp = concat ("Create folder \"", maildir, "\"? ", NULL);
554 if (noisy && !getanswer (cp))
555 done (1);
556 free (cp);
557 if (!makedir (maildir))
558 adios (NULL, "unable to create folder %s", maildir);
559 }
560
561 if (chdir (maildir) == NOTOK)
562 adios (maildir, "unable to change directory to");
563
564 /* read folder and create message structure */
565 if (!(mp = folder_read (folder)))
566 adios (NULL, "unable to read folder %s", folder);
567
568 #ifdef POP
569 go_to_it:
570 #endif /* POP */
571
572 if (inc_type == INC_FILE) {
573 if (access (newmail, W_OK) != NOTOK) {
574 locked++;
575 if (trnflag) {
576 SIGNAL (SIGHUP, SIG_IGN);
577 SIGNAL (SIGINT, SIG_IGN);
578 SIGNAL (SIGQUIT, SIG_IGN);
579 SIGNAL (SIGTERM, SIG_IGN);
580 }
581
582 GETGROUPPRIVS(); /* Reset gid to lock mail file */
583 in = lkfopen (newmail, "r");
584 DROPGROUPPRIVS();
585 if (in == NULL)
586 adios (NULL, "unable to lock and fopen %s", newmail);
587 fstat (fileno(in), &s1);
588 } else {
589 trnflag = 0;
590 if ((in = fopen (newmail, "r")) == NULL)
591 adios (newmail, "unable to read");
592 }
593 }
594
595 /* This shouldn't be necessary but it can't hurt. */
596 DROPGROUPPRIVS();
597
598 if (audfile) {
599 if ((i = stat (audfile, &st)) == NOTOK)
600 advise (NULL, "Creating Receive-Audit: %s", audfile);
601 if ((aud = fopen (audfile, "a")) == NULL)
602 adios (audfile, "unable to append to");
603 else if (i == NOTOK)
604 chmod (audfile, m_gmprot ());
605
606 #ifdef POP
607 fprintf (aud, from ? "<<inc>> %s -ms %s\n"
608 : host ? "<<inc>> %s -host %s -user %s%s\n"
609 : "<<inc>> %s\n",
610 dtimenow (0), from ? from : host, user,
611 rpop < 0 ? " -apop" : rpop > 0 ? " -rpop" : "");
612 #else /* POP */
613 fprintf (aud, from ? "<<inc>> %s -ms %s\n" : "<<inc>> %s\n",
614 dtimenow (0), from);
615 #endif /* POP */
616 }
617
618 #ifdef MHE
619 if (context_find ("mhe")) {
620 cp = concat (maildir, "/++", NULL);
621 i = stat (cp, &st);
622 if ((mhe = fopen (cp, "a")) == NULL)
623 admonish (cp, "unable to append to");
624 else
625 if (i == NOTOK)
626 chmod (cp, m_gmprot ());
627 free (cp);
628 }
629 #endif /* MHE */
630
631 /* Get new format string */
632 nfs = new_fs (form, format, FORMAT);
633
634 if (noisy) {
635 printf ("Incorporating new mail into %s...\n\n", folder);
636 fflush (stdout);
637 }
638
639 #ifdef POP
640 /*
641 * Get the mail from a POP server
642 */
643 if (inc_type == INC_POP) {
644 if (packfile) {
645 packfile = path (packfile, TFILE);
646 if (stat (packfile, &st) == NOTOK) {
647 if (errno != ENOENT)
648 adios (packfile, "error on file");
649 cp = concat ("Create file \"", packfile, "\"? ", NULL);
650 if (noisy && !getanswer (cp))
651 done (1);
652 free (cp);
653 }
654 msgnum = map_count ();
655 if ((pd = mbx_open (packfile, mbx_style, getuid(), getgid(), m_gmprot()))
656 == NOTOK)
657 adios (packfile, "unable to open");
658 if ((pf = fdopen (pd, "w+")) == NULL)
659 adios (NULL, "unable to fdopen %s", packfile);
660 } else {
661 hghnum = msgnum = mp->hghmsg;
662 /*
663 * Check if we have enough message space for all the new
664 * messages. If not, then realloc the folder and add enough
665 * space for all new messages plus 10 additional slots.
666 */
667 if (mp->hghmsg + nmsgs >= mp->hghoff
668 && !(mp = folder_realloc (mp, mp->lowoff, mp->hghmsg + nmsgs + 10)))
669 adios (NULL, "unable to allocate folder storage");
670 }
671
672 for (i = 1; i <= nmsgs; i++) {
673 msgnum++;
674 if (packfile) {
675 fseek (pf, 0L, SEEK_CUR);
676 pos = ftell (pf);
677 size = 0;
678 fwrite (mmdlm1, 1, strlen (mmdlm1), pf);
679 start = ftell (pf);
680
681 if (pop_retr (i, pop_pack) == NOTOK)
682 adios (NULL, "%s", response);
683
684 fseek (pf, 0L, SEEK_CUR);
685 stop = ftell (pf);
686 if (fflush (pf))
687 adios (packfile, "write error on");
688 fseek (pf, start, SEEK_SET);
689 } else {
690 cp = getcpy (m_name (msgnum));
691 if ((pf = fopen (cp, "w+")) == NULL)
692 adios (cp, "unable to write");
693 chmod (cp, m_gmprot ());
694 start = stop = 0L;
695
696 if (pop_retr (i, pop_action) == NOTOK)
697 adios (NULL, "%s", response);
698
699 if (fflush (pf))
700 adios (cp, "write error on");
701 fseek (pf, 0L, SEEK_SET);
702 }
703 switch (p = scan (pf, msgnum, 0, nfs, width,
704 packfile ? 0 : msgnum == mp->hghmsg + 1 && chgflag,
705 1, NULL, stop - start, noisy)) {
706 case SCNEOF:
707 printf ("%*d empty\n", DMAXFOLDER, msgnum);
708 break;
709
710 case SCNFAT:
711 trnflag = 0;
712 noisy++;
713 /* advise (cp, "unable to read"); already advised */
714 /* fall thru */
715
716 case SCNERR:
717 case SCNNUM:
718 break;
719
720 case SCNMSG:
721 case SCNENC:
722 default:
723 if (aud)
724 fputs (scanl, aud);
725 # ifdef MHE
726 if (mhe)
727 fputs (scanl, mhe);
728 # endif /* MHE */
729 if (noisy)
730 fflush (stdout);
731 if (!packfile) {
732 clear_msg_flags (mp, msgnum);
733 set_exists (mp, msgnum);
734 set_unseen (mp, msgnum);
735 mp->msgflags |= SEQMOD;
736 }
737 break;
738 }
739 if (packfile) {
740 fseek (pf, stop, SEEK_SET);
741 fwrite (mmdlm2, 1, strlen (mmdlm2), pf);
742 if (fflush (pf) || ferror (pf)) {
743 int e = errno;
744 pop_quit ();
745 errno = e;
746 adios (packfile, "write error on");
747 }
748 map_write (packfile, pd, 0, 0L, start, stop, pos, size, noisy);
749 } else {
750 if (ferror(pf) || fclose (pf)) {
751 int e = errno;
752 unlink (cp);
753 pop_quit ();
754 errno = e;
755 adios (cp, "write error on");
756 }
757 free (cp);
758 }
759
760 if (trnflag && pop_dele (i) == NOTOK)
761 adios (NULL, "%s", response);
762 }
763
764 if (pop_quit () == NOTOK)
765 adios (NULL, "%s", response);
766 if (packfile) {
767 mbx_close (packfile, pd);
768 pd = NOTOK;
769 }
770 }
771 #endif /* POP */
772
773 /*
774 * Get the mail from file (usually mail spool)
775 */
776 if (inc_type == INC_FILE) {
777 m_unknown (in); /* the MAGIC invocation... */
778 hghnum = msgnum = mp->hghmsg;
779 for (i = 0;;) {
780 /*
781 * Check if we need to allocate more space for message status.
782 * If so, then add space for an additional 100 messages.
783 */
784 if (msgnum >= mp->hghoff
785 && !(mp = folder_realloc (mp, mp->lowoff, mp->hghoff + 100))) {
786 advise (NULL, "unable to allocate folder storage");
787 i = NOTOK;
788 break;
789 }
790
791 #if 0
792 /* copy file from spool to tmp file */
793 tmpfilenam = m_scratch ("", invo_name);
794 if ((fd = creat (tmpfilenam, m_gmprot ())) == NOTOK)
795 adios (tmpfilenam, "unable to create");
796 chmod (tmpfilenam, m_gmprot ());
797 if (!(in2 = fdopen (fd, "r+")))
798 adios (tmpfilenam, "unable to access");
799 cpymsg (in, in2);
800
801 /* link message into folder */
802 newmsg = folder_addmsg(mp, tmpfilenam);
803 #endif
804 /* create scanline for new message */
805 switch (i = scan (in, msgnum + 1, msgnum + 1, nfs, width,
806 msgnum == hghnum && chgflag, 1, NULL, 0L, noisy)) {
807 case SCNFAT:
808 case SCNEOF:
809 break;
810
811 case SCNERR:
812 if (aud)
813 fputs ("inc aborted!\n", aud);
814 advise (NULL, "aborted!"); /* doesn't clean up locks! */
815 break;
816
817 case SCNNUM:
818 advise (NULL, "BUG in %s, number out of range", invo_name);
819 break;
820
821 default:
822 advise (NULL, "BUG in %s, scan() botch (%d)", invo_name, i);
823 break;
824
825 case SCNMSG:
826 case SCNENC:
827 /*
828 * Run the external program hook on the message.
829 */
830
831 (void)snprintf(b, sizeof (b), "%s/%d", maildir, msgnum + 1);
832 (void)ext_hook("add-hook", b, (char *)0);
833
834 if (aud)
835 fputs (scanl, aud);
836 #ifdef MHE
837 if (mhe)
838 fputs (scanl, mhe);
839 #endif /* MHE */
840 if (noisy)
841 fflush (stdout);
842
843 msgnum++;
844 mp->hghmsg++;
845 mp->nummsg++;
846 if (mp->lowmsg == 0) mp->lowmsg = 1;
847
848 clear_msg_flags (mp, msgnum);
849 set_exists (mp, msgnum);
850 set_unseen (mp, msgnum);
851 mp->msgflags |= SEQMOD;
852 continue;
853 }
854 break;
855 }
856 }
857
858 #ifdef POP
859 if (p < 0) { /* error */
860 #else
861 if (i < 0) { /* error */
862 #endif
863 if (locked) {
864 GETGROUPPRIVS(); /* Be sure we can unlock mail file */
865 (void) lkfclose (in, newmail); in = NULL;
866 DROPGROUPPRIVS(); /* And then return us to normal privileges */
867 } else {
868 fclose (in); in = NULL;
869 }
870 adios (NULL, "failed");
871 }
872
873 if (aud)
874 fclose (aud);
875
876 #ifdef MHE
877 if (mhe)
878 fclose (mhe);
879 #endif /* MHE */
880
881 if (noisy)
882 fflush (stdout);
883
884 #ifdef POP
885 if ((inc_type == INC_POP) && packfile)
886 done (0);
887 #endif /* POP */
888
889 /*
890 * truncate file we are incorporating from
891 */
892 if (inc_type == INC_FILE) {
893 if (trnflag) {
894 if (stat (newmail, &st) != NOTOK && s1.st_mtime != st.st_mtime)
895 advise (NULL, "new messages have arrived!\007");
896 else {
897 if ((i = creat (newmail, 0600)) != NOTOK)
898 close (i);
899 else
900 admonish (newmail, "error zero'ing");
901 unlink(map_name(newmail));
902 }
903 } else {
904 if (noisy)
905 printf ("%s not zero'd\n", newmail);
906 }
907 }
908
909 if (msgnum == hghnum) {
910 admonish (NULL, "no messages incorporated");
911 } else {
912 context_replace (pfolder, folder); /* update current folder */
913 if (chgflag)
914 mp->curmsg = hghnum + 1;
915 mp->hghmsg = msgnum;
916 if (mp->lowmsg == 0)
917 mp->lowmsg = 1;
918 if (chgflag) /* sigh... */
919 seq_setcur (mp, mp->curmsg);
920 }
921
922 /*
923 * unlock the mail spool
924 */
925 if (inc_type == INC_FILE) {
926 if (locked) {
927 GETGROUPPRIVS(); /* Be sure we can unlock mail file */
928 (void) lkfclose (in, newmail); in = NULL;
929 DROPGROUPPRIVS(); /* And then return us to normal privileges */
930 } else {
931 fclose (in); in = NULL;
932 }
933 }
934
935 seq_setunseen (mp, 0); /* set the Unseen-Sequence */
936 seq_save (mp); /* synchronize sequences */
937 context_save (); /* save the context file */
938 return done (0);
939 }
940
941
942 #if 0
943
944 /*
945 * Copy message message from spool into
946 * temporary file. Massage the "From " line
947 * while copying.
948 */
949
950 cpymsg (FILE *in, FILE *out)
951 {
952 int state;
953 char *tmpbuf, name[NAMESZ];
954
955 for (;;) {
956 state = m_getfld (state, name, tmpbuf, rlwidth, in);
957 switch (state) {
958 case FLD:
959 case FLDPLUS:
960 break;
961 case BODY:
962 break;
963 case LENERR:
964 case FMTERR:
965 break;
966 case FILEEOF:
967 break;
968 default:
969 }
970 }
971 }
972 #endif /* if 0 */
973
974
975 int
976 done (int status)
977 {
978 #ifdef POP
979 if (packfile && pd != NOTOK)
980 mbx_close (packfile, pd);
981 #endif /* POP */
982 if (locked)
983 {
984 GETGROUPPRIVS();
985 lkfclose(in, newmail);
986 DROPGROUPPRIVS();
987 }
988 exit (status);
989 return 1; /* dead code to satisfy the compiler */
990 }
991
992 #ifdef POP
993 static int
994 pop_action (char *s)
995 {
996 fprintf (pf, "%s\n", s);
997 stop += strlen (s) + 1;
998 return 0; /* Is return value used? This was missing before 1999-07-15. */
999 }
1000
1001 static int
1002 pop_pack (char *s)
1003 {
1004 int j;
1005 char buffer[BUFSIZ];
1006
1007 snprintf (buffer, sizeof(buffer), "%s\n", s);
1008 for (j = 0; (j = stringdex (mmdlm1, buffer)) >= 0; buffer[j]++)
1009 continue;
1010 for (j = 0; (j = stringdex (mmdlm2, buffer)) >= 0; buffer[j]++)
1011 continue;
1012 fputs (buffer, pf);
1013 size += strlen (buffer) + 1;
1014 return 0; /* Is return value used? This was missing before 1999-07-15. */
1015 }
1016
1017 static int
1018 map_count (void)
1019 {
1020 int md;
1021 char *cp;
1022 struct drop d;
1023 struct stat st;
1024
1025 if (stat (packfile, &st) == NOTOK)
1026 return 0;
1027 if ((md = open (cp = map_name (packfile), O_RDONLY)) == NOTOK
1028 || map_chk (cp, md, &d, (long) st.st_size, 1)) {
1029 if (md != NOTOK)
1030 close (md);
1031 return 0;
1032 }
1033 close (md);
1034 return (d.d_id);
1035 }
1036 #endif /* POP */