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