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