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