]> diplodocus.org Git - nmh/blob - uip/mhstoresbr.c
oauth.c: Alter permissions from 0755 to 0644.
[nmh] / uip / mhstoresbr.c
1 /* mhstoresbr.c -- routines to save/store the contents of MIME messages
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 #include "h/mh.h"
9 #include "sbr/read_switch_multiword.h"
10 #include "sbr/concat.h"
11 #include "sbr/smatch.h"
12 #include "sbr/r1bindex.h"
13 #include "sbr/uprf.h"
14 #include "sbr/getcpy.h"
15 #include "sbr/getfolder.h"
16 #include "sbr/folder_read.h"
17 #include "sbr/folder_free.h"
18 #include "sbr/folder_addmsg.h"
19 #include "sbr/context_find.h"
20 #include "sbr/path.h"
21 #include "sbr/error.h"
22 #include <fcntl.h>
23 #include "h/md5.h"
24 #include "h/mts.h"
25 #include "h/tws.h"
26 #include "h/fmt_scan.h"
27 #include "h/mime.h"
28 #include "h/mhparse.h"
29 #include "h/utils.h"
30 #include "mhmisc.h"
31 #include "mhshowsbr.h"
32 #include "sbr/m_maildir.h"
33 #include "sbr/m_mktemp.h"
34
35 enum clobber_policy_t {
36 NMH_CLOBBER_ALWAYS = 0,
37 NMH_CLOBBER_AUTO,
38 NMH_CLOBBER_SUFFIX,
39 NMH_CLOBBER_ASK,
40 NMH_CLOBBER_NEVER
41 };
42
43 static enum clobber_policy_t clobber_policy (const char *) PURE;
44
45 struct mhstoreinfo {
46 CT *cts; /* Top-level list of contents to store. */
47 char *cwd; /* cached current directory */
48 int autosw; /* -auto enabled */
49 int verbosw; /* -verbose enabled */
50 int files_not_clobbered; /* output flag indicating that store failed
51 in order to not clobber an existing file */
52
53 /* The following must never be touched by a caller: they are for
54 internal use by the mhstoresbr functions. */
55 char *dir; /* directory in which to store contents */
56 enum clobber_policy_t clobber_policy; /* -clobber selection */
57 };
58
59 static bool use_param_as_filename(const char *p);
60
61 mhstoreinfo_t
62 mhstoreinfo_create (CT *ct, char *pwd, const char *csw, int asw, int vsw)
63 {
64 mhstoreinfo_t info;
65
66 NEW(info);
67 info->cts = ct;
68 info->cwd = pwd;
69 info->autosw = asw;
70 info->verbosw = vsw;
71 info->files_not_clobbered = 0;
72 info->dir = NULL;
73 info->clobber_policy = clobber_policy (csw);
74
75 return info;
76 }
77
78 void
79 mhstoreinfo_free (mhstoreinfo_t info)
80 {
81 free (info->cwd);
82 free (info->dir);
83 free (info);
84 }
85
86 int
87 mhstoreinfo_files_not_clobbered (const mhstoreinfo_t info)
88 {
89 return info->files_not_clobbered;
90 }
91
92
93 /*
94 * Type for a compare function for qsort. This keeps
95 * the compiler happy.
96 */
97 typedef int (*qsort_comp) (const void *, const void *);
98
99
100 /*
101 * static prototypes
102 */
103 static void store_single_message (CT, mhstoreinfo_t);
104 static int store_switch (CT, mhstoreinfo_t);
105 static int store_generic (CT, mhstoreinfo_t);
106 static int store_application (CT, mhstoreinfo_t);
107 static int store_multi (CT, mhstoreinfo_t);
108 static int store_partial (CT, mhstoreinfo_t);
109 static int store_external (CT, mhstoreinfo_t);
110 static int ct_compar (CT *, CT *);
111 static int store_content (CT, CT, mhstoreinfo_t);
112 static int output_content_file (CT, int);
113 static int output_content_folder (char *, char *);
114 static int parse_format_string (CT, char *, char *, int, char *);
115 static void get_storeproc (CT);
116 static int copy_some_headers (FILE *, CT);
117 static char *clobber_check (char *, mhstoreinfo_t);
118
119 /*
120 * Main entry point to store content
121 * from a collection of messages.
122 */
123
124 void
125 store_all_messages (mhstoreinfo_t info)
126 {
127 CT ct, *ctp;
128 char *cp;
129
130 /*
131 * Check for the directory in which to
132 * store any contents.
133 */
134 if ((cp = context_find (nmhstorage)) && *cp)
135 info->dir = mh_xstrdup(cp);
136 else
137 info->dir = getcpy (info->cwd);
138
139 for (ctp = info->cts; *ctp; ctp++) {
140 ct = *ctp;
141 store_single_message (ct, info);
142 }
143
144 flush_errors ();
145 }
146
147
148 /*
149 * Entry point to store the content
150 * in a (single) message
151 */
152
153 static void
154 store_single_message (CT ct, mhstoreinfo_t info)
155 {
156 if (type_ok (ct, 1)) {
157 umask (ct->c_umask);
158 store_switch (ct, info);
159 if (ct->c_fp) {
160 fclose (ct->c_fp);
161 ct->c_fp = NULL;
162 }
163 if (ct->c_ceclosefnx)
164 (*ct->c_ceclosefnx) (ct);
165 }
166 }
167
168
169 /*
170 * Switching routine to store different content types
171 */
172
173 static int
174 store_switch (CT ct, mhstoreinfo_t info)
175 {
176 switch (ct->c_type) {
177 case CT_MULTIPART:
178 return store_multi (ct, info);
179
180 case CT_MESSAGE:
181 switch (ct->c_subtype) {
182 case MESSAGE_PARTIAL:
183 return store_partial (ct, info);
184
185 case MESSAGE_EXTERNAL:
186 return store_external (ct, info);
187
188 case MESSAGE_RFC822:
189 default:
190 return store_generic (ct, info);
191 }
192
193 case CT_APPLICATION:
194 default:
195 return store_application (ct, info);
196
197 case CT_TEXT:
198 case CT_AUDIO:
199 case CT_IMAGE:
200 case CT_VIDEO:
201 return store_generic (ct, info);
202 }
203
204 return OK; /* NOT REACHED */
205 }
206
207
208 /*
209 * Generic routine to store a MIME content.
210 * (audio, video, image, text, message/rfc822)
211 */
212
213 static int
214 store_generic (CT ct, mhstoreinfo_t info)
215 {
216 /*
217 * Check if the content specifies a filename.
218 * Don't bother with this for type "message"
219 * (only "message/rfc822" will use store_generic).
220 */
221 if (info->autosw && ct->c_type != CT_MESSAGE)
222 get_storeproc (ct);
223
224 return store_content (ct, NULL, info);
225 }
226
227
228 /*
229 * Store content of type "application"
230 */
231
232 static int
233 store_application (CT ct, mhstoreinfo_t info)
234 {
235 CI ci = &ct->c_ctinfo;
236
237 /* Check if the content specifies a filename */
238 if (info->autosw)
239 get_storeproc (ct);
240
241 /*
242 * If storeproc is not defined, and the content is type
243 * "application/octet-stream", we also check for various
244 * attribute/value pairs which specify if this a tar file.
245 */
246 if (!ct->c_storeproc && ct->c_subtype == APPLICATION_OCTETS) {
247 bool tarP = false;
248 bool zP = false;
249 bool gzP = false;
250 char *cp;
251
252 if ((cp = get_param(ci->ci_first_pm, "type", ' ', 1))) {
253 if (strcasecmp (cp, "tar") == 0)
254 tarP = true;
255 }
256
257 /* check for "conversions=compress" attribute */
258 if ((cp = get_param(ci->ci_first_pm, "conversions", ' ', 1)) ||
259 (cp = get_param(ci->ci_first_pm, "x-conversions", ' ', 1))) {
260 if (strcasecmp (cp, "compress") == 0 ||
261 strcasecmp (cp, "x-compress") == 0) {
262 zP = true;
263 }
264 if (strcasecmp (cp, "gzip") == 0 ||
265 strcasecmp (cp, "x-gzip") == 0) {
266 gzP = true;
267 }
268 }
269
270 if (tarP) {
271 ct->c_showproc = add (zP ? "%euncompress | tar tvf -"
272 : (gzP ? "%egzip -dc | tar tvf -"
273 : "%etar tvf -"), NULL);
274 if (!ct->c_storeproc) {
275 if (info->autosw) {
276 ct->c_storeproc = add (zP ? "| uncompress | tar xvpf -"
277 : (gzP ? "| gzip -dc | tar xvpf -"
278 : "| tar xvpf -"), NULL);
279 ct->c_umask = 0022;
280 } else {
281 ct->c_storeproc= add (zP ? "%m%P.tar.Z"
282 : (gzP ? "%m%P.tar.gz"
283 : "%m%P.tar"), NULL);
284 }
285 }
286 }
287 }
288
289 return store_content (ct, NULL, info);
290 }
291
292
293 /*
294 * Store the content of a multipart message
295 */
296
297 static int
298 store_multi (CT ct, mhstoreinfo_t info)
299 {
300 int result;
301 struct multipart *m = (struct multipart *) ct->c_ctparams;
302 struct part *part;
303
304 result = NOTOK;
305 for (part = m->mp_parts; part; part = part->mp_next) {
306 CT p = part->mp_part;
307
308 if (part_ok (p) && type_ok (p, 1)) {
309 if (ct->c_storage) {
310 /* Support mhstore -outfile. The MIME parser doesn't
311 load c_storage, so we know that p->c_storage is
312 NULL here. */
313 p->c_storage = mh_xstrdup(ct->c_storage);
314 }
315 result = store_switch (p, info);
316
317 if (result == OK && ct->c_subtype == MULTI_ALTERNATE)
318 break;
319 }
320 }
321
322 return result;
323 }
324
325
326 /*
327 * Reassemble and store the contents of a collection
328 * of messages of type "message/partial".
329 */
330
331 static int
332 store_partial (CT ct, mhstoreinfo_t info)
333 {
334 int cur, hi, i;
335 CT p, *ctp, *ctq;
336 CT *base;
337 struct partial *pm, *qm;
338
339 qm = (struct partial *) ct->c_ctparams;
340 if (qm->pm_stored)
341 return OK;
342
343 hi = i = 0;
344 for (ctp = info->cts; *ctp; ctp++) {
345 p = *ctp;
346 if (p->c_type == CT_MESSAGE && p->c_subtype == ct->c_subtype) {
347 pm = (struct partial *) p->c_ctparams;
348 if (!pm->pm_stored
349 && strcmp (qm->pm_partid, pm->pm_partid) == 0) {
350 pm->pm_marked = pm->pm_partno;
351 if (pm->pm_maxno)
352 hi = pm->pm_maxno;
353 pm->pm_stored = 1;
354 i++;
355 }
356 else
357 pm->pm_marked = 0;
358 }
359 }
360
361 if (hi == 0) {
362 inform("missing (at least) last part of multipart message");
363 return NOTOK;
364 }
365
366 base = mh_xcalloc(i + 1, sizeof *base);
367 ctq = base;
368 for (ctp = info->cts; *ctp; ctp++) {
369 p = *ctp;
370 if (p->c_type == CT_MESSAGE && p->c_subtype == ct->c_subtype) {
371 pm = (struct partial *) p->c_ctparams;
372 if (pm->pm_marked)
373 *ctq++ = p;
374 }
375 }
376 *ctq = NULL;
377
378 if (i > 1)
379 qsort(base, i, sizeof(*base), (qsort_comp) ct_compar);
380
381 cur = 1;
382 for (ctq = base; *ctq; ctq++) {
383 p = *ctq;
384 pm = (struct partial *) p->c_ctparams;
385 if (pm->pm_marked == cur) {
386 cur++;
387 continue;
388 }
389
390 if (pm->pm_marked == cur - 1) {
391 inform("duplicate part %d of %d part multipart message, continuing...",
392 pm->pm_marked, hi);
393 continue;
394 }
395
396 missing_part:
397 inform("missing %spart %d of %d part multipart message",
398 cur != hi ? "(at least) " : "", cur, hi);
399 goto losing;
400 }
401 if (hi != --cur) {
402 cur = hi;
403 goto missing_part;
404 }
405
406 /*
407 * Now cycle through the sorted list of messages of type
408 * "message/partial" and save/append them to a file.
409 */
410
411 ctq = base;
412 ct = *ctq++;
413 if (store_content (ct, NULL, info) == NOTOK) {
414 losing:
415 free(base);
416 return NOTOK;
417 }
418
419 for (; *ctq; ctq++) {
420 p = *ctq;
421 if (store_content (p, ct, info) == NOTOK)
422 goto losing;
423 }
424
425 free(base);
426 return OK;
427 }
428
429
430 /*
431 * Store content from a message of type "message/external".
432 */
433
434 static int
435 store_external (CT ct, mhstoreinfo_t info)
436 {
437 int result = NOTOK;
438 struct exbody *e = (struct exbody *) ct->c_ctparams;
439 CT p = e->eb_content;
440
441 if (!type_ok (p, 1))
442 return OK;
443
444 /*
445 * Check if the parameters for the external body
446 * specified a filename.
447 */
448 if (info->autosw) {
449 char *cp;
450
451 if ((cp = e->eb_name) && use_param_as_filename(cp)) {
452 if (!ct->c_storeproc)
453 ct->c_storeproc = mh_xstrdup(cp);
454 if (!p->c_storeproc)
455 p->c_storeproc = mh_xstrdup(cp);
456 }
457 }
458
459 /*
460 * Since we will let the Content structure for the
461 * external body substitute for the current content,
462 * we temporarily change its partno (number inside
463 * multipart), so everything looks right.
464 */
465 p->c_partno = ct->c_partno;
466
467 /* we probably need to check if content is really there */
468 if (ct->c_storage) {
469 /* Support mhstore -outfile. The MIME parser doesn't load
470 c_storage, so we know that p->c_storage is NULL here. */
471 p->c_storage = mh_xstrdup(ct->c_storage);
472 }
473 result = store_switch (p, info);
474
475 p->c_partno = NULL;
476 return result;
477 }
478
479
480 /*
481 * Compare the numbering from two different
482 * message/partials (needed for sorting).
483 */
484
485 static int
486 ct_compar (CT *a, CT *b)
487 {
488 struct partial *am = (struct partial *) ((*a)->c_ctparams);
489 struct partial *bm = (struct partial *) ((*b)->c_ctparams);
490
491 return am->pm_marked - bm->pm_marked;
492 }
493
494
495 /*
496 * Store contents of a message or message part to
497 * a folder, a file, the standard output, or pass
498 * the contents to a command.
499 *
500 * If the current content to be saved is a followup part
501 * to a collection of messages of type "message/partial",
502 * then field "p" is a pointer to the Content structure
503 * to the first message/partial in the group.
504 */
505
506 static int
507 store_content (CT ct, CT p, mhstoreinfo_t info)
508 {
509 bool appending = false;
510 int msgnum = 0;
511 bool is_partial = false;
512 bool first_partial = false;
513 bool last_partial = false;
514 char *cp, buffer[BUFSIZ];
515
516 /*
517 * Do special processing for messages of
518 * type "message/partial".
519 *
520 * We first check if this content is of type
521 * "message/partial". If it is, then we need to check
522 * whether it is the first and/or last in the group.
523 *
524 * Then if "p" is a valid pointer, it points to the Content
525 * structure of the first partial in the group. So we copy
526 * the file name and/or folder name from that message. In
527 * this case, we also note that we will be appending.
528 */
529 if (ct->c_type == CT_MESSAGE && ct->c_subtype == MESSAGE_PARTIAL) {
530 struct partial *pm = (struct partial *) ct->c_ctparams;
531
532 /* Yep, it's a message/partial */
533 is_partial = true;
534
535 /* But is it the first and/or last in the collection? */
536 if (pm->pm_partno == 1)
537 first_partial = true;
538 if (pm->pm_maxno && pm->pm_partno == pm->pm_maxno)
539 last_partial = true;
540
541 /*
542 * If "p" is a valid pointer, then it points to the
543 * Content structure for the first message in the group.
544 * So we just copy the filename or foldername information
545 * from the previous iteration of this function.
546 */
547 if (p) {
548 appending = true;
549 if (! ct->c_storage) {
550 ct->c_storage = mh_xstrdup(FENDNULL(p->c_storage));
551
552 /* record the folder name */
553 if (p->c_folder) {
554 ct->c_folder = mh_xstrdup(p->c_folder);
555 }
556 }
557 goto got_filename;
558 }
559 }
560
561 /*
562 * Get storage formatting string.
563 *
564 * 1) If we have storeproc defined, then use that
565 * 2) Else check for a mhn-store-<type>/<subtype> entry
566 * 3) Else check for a mhn-store-<type> entry
567 * 4) Else if content is "message", use "+" (current folder)
568 * 5) Else use string "%m%P.%s".
569 */
570 if ((cp = ct->c_storeproc) == NULL || *cp == '\0') {
571 CI ci = &ct->c_ctinfo;
572
573 cp = context_find_by_type ("store", ci->ci_type, ci->ci_subtype);
574 if (cp == NULL) {
575 cp = ct->c_type == CT_MESSAGE ? "+" : "%m%P.%s";
576 }
577 }
578
579 if (! ct->c_storage) {
580 /*
581 * Check the beginning of storage formatting string
582 * to see if we are saving content to a folder.
583 */
584 if (*cp == '+' || *cp == '@') {
585 char *tmpfilenam, *folder;
586
587 /* Store content in temporary file for now */
588 if ((tmpfilenam = m_mktemp(invo_name, NULL, NULL)) == NULL) {
589 die("unable to create temporary file in %s",
590 get_temp_dir());
591 }
592 ct->c_storage = mh_xstrdup(tmpfilenam);
593
594 /* Get the folder name */
595 if (cp[1])
596 folder = pluspath (cp);
597 else
598 folder = getfolder (1);
599
600 /* Check if folder exists */
601 create_folder(m_mailpath(folder), 0, exit);
602
603 /* Record the folder name */
604 ct->c_folder = mh_xstrdup(folder);
605
606 if (cp[1])
607 free (folder);
608
609 goto got_filename;
610 }
611
612 /*
613 * Parse and expand the storage formatting string
614 * in `cp' into `buffer'.
615 */
616 parse_format_string (ct, cp, buffer, sizeof(buffer), info->dir);
617
618 /*
619 * If formatting begins with '|' or '!', then pass
620 * content to standard input of a command and return.
621 */
622 if (buffer[0] == '|' || buffer[0] == '!')
623 return show_content_aux (ct, 0, buffer + 1, info->dir, NULL);
624
625 /* record the filename */
626 if ((ct->c_storage = clobber_check (mh_xstrdup(buffer), info)) ==
627 NULL) {
628 return NOTOK;
629 }
630 } else {
631 /* The output filename was explicitly specified, so use it. */
632 if ((ct->c_storage = clobber_check (ct->c_storage, info)) ==
633 NULL) {
634 return NOTOK;
635 }
636 }
637
638 got_filename:
639 /* flush the output stream */
640 fflush (stdout);
641
642 /* Now save or append the content to a file */
643 if (output_content_file (ct, appending) == NOTOK)
644 return NOTOK;
645
646 /*
647 * If necessary, link the file into a folder and remove
648 * the temporary file. If this message is a partial,
649 * then only do this if it is the last one in the group.
650 */
651 if (ct->c_folder && (!is_partial || last_partial)) {
652 msgnum = output_content_folder (ct->c_folder, ct->c_storage);
653 (void) m_unlink (ct->c_storage);
654 if (msgnum == NOTOK)
655 return NOTOK;
656 }
657
658 if (info->verbosw) {
659 /*
660 * Now print out the name/number of the message
661 * that we are storing.
662 */
663 if (is_partial) {
664 if (first_partial)
665 fprintf (stderr, "reassembling partials ");
666 if (last_partial)
667 fputs(ct->c_file, stderr);
668 else
669 fprintf (stderr, "%s,", ct->c_file);
670 } else {
671 fprintf (stderr, "storing message %s", ct->c_file);
672 if (ct->c_partno)
673 fprintf (stderr, " part %s", ct->c_partno);
674 }
675
676 /*
677 * Unless we are in the "middle" of group of message/partials,
678 * we now print the name of the file, folder, and/or message
679 * to which we are storing the content.
680 */
681 if (!is_partial || last_partial) {
682 if (ct->c_folder) {
683 fprintf (stderr, " to folder %s as message %d\n", ct->c_folder,
684 msgnum);
685 } else if (!strcmp(ct->c_storage, "-")) {
686 fprintf (stderr, " to stdout\n");
687 } else {
688 int cwdlen = strlen (info->cwd);
689
690 fprintf (stderr, " as file %s\n",
691 !has_prefix(ct->c_storage, info->cwd)
692 || ct->c_storage[cwdlen] != '/'
693 ? ct->c_storage : ct->c_storage + cwdlen + 1);
694 }
695 }
696 }
697
698 return OK;
699 }
700
701
702 /*
703 * Output content to a file
704 */
705
706 static int
707 output_content_file (CT ct, int appending)
708 {
709 int filterstate;
710 char *file, buffer[BUFSIZ];
711 long pos, last;
712 FILE *fp;
713
714 /*
715 * If the pathname is absolute, make sure
716 * all the relevant directories exist.
717 */
718 if (strchr(ct->c_storage, '/')
719 && make_intermediates (ct->c_storage) == NOTOK)
720 return NOTOK;
721
722 if (ct->c_encoding != CE_7BIT) {
723 int cc, fd;
724
725 if (!ct->c_ceopenfnx) {
726 inform("don't know how to decode part %s of message %s",
727 ct->c_partno, ct->c_file);
728 return NOTOK;
729 }
730
731 file = appending || !strcmp (ct->c_storage, "-") ? NULL
732 : ct->c_storage;
733 if ((fd = (*ct->c_ceopenfnx) (ct, &file)) == NOTOK)
734 return NOTOK;
735 if (!strcmp (file, ct->c_storage)) {
736 (*ct->c_ceclosefnx) (ct);
737 return OK;
738 }
739
740 /*
741 * Send to standard output
742 */
743 if (!strcmp (ct->c_storage, "-")) {
744 int gd;
745
746 if ((gd = dup (fileno (stdout))) == NOTOK) {
747 advise ("stdout", "unable to dup");
748 losing:
749 (*ct->c_ceclosefnx) (ct);
750 return NOTOK;
751 }
752 if ((fp = fdopen (gd, appending ? "a" : "w")) == NULL) {
753 advise ("stdout", "unable to fdopen (%d, \"%s\") from", gd,
754 appending ? "a" : "w");
755 close (gd);
756 goto losing;
757 }
758 } else {
759 /*
760 * Open output file
761 */
762 if ((fp = fopen (ct->c_storage, appending ? "a" : "w")) == NULL) {
763 advise (ct->c_storage, "unable to fopen for %s",
764 appending ? "appending" : "writing");
765 goto losing;
766 }
767 }
768
769 /*
770 * Filter the header fields of the initial enclosing
771 * message/partial into the file.
772 */
773 if (ct->c_type == CT_MESSAGE && ct->c_subtype == MESSAGE_PARTIAL) {
774 struct partial *pm = (struct partial *) ct->c_ctparams;
775
776 if (pm->pm_partno == 1)
777 copy_some_headers (fp, ct);
778 }
779
780 for (;;) {
781 switch (cc = read (fd, buffer, sizeof(buffer))) {
782 case NOTOK:
783 advise (file, "error reading content from");
784 break;
785
786 case OK:
787 break;
788
789 default:
790 if ((int) fwrite (buffer, sizeof(*buffer), cc, fp) < cc) {
791 advise ("output_content_file", "fwrite");
792 }
793 continue;
794 }
795 break;
796 }
797
798 (*ct->c_ceclosefnx) (ct);
799
800 if (cc != NOTOK && fflush (fp))
801 advise (ct->c_storage, "error writing to");
802
803 fclose (fp);
804
805 return cc == NOTOK ? NOTOK : OK;
806 }
807
808 if (!ct->c_fp && (ct->c_fp = fopen (ct->c_file, "r")) == NULL) {
809 advise (ct->c_file, "unable to open for reading");
810 return NOTOK;
811 }
812
813 pos = ct->c_begin;
814 last = ct->c_end;
815 fseek (ct->c_fp, pos, SEEK_SET);
816
817 if (!strcmp (ct->c_storage, "-")) {
818 int gd;
819
820 if ((gd = dup (fileno (stdout))) == NOTOK) {
821 advise ("stdout", "unable to dup");
822 return NOTOK;
823 }
824 if ((fp = fdopen (gd, appending ? "a" : "w")) == NULL) {
825 advise ("stdout", "unable to fdopen (%d, \"%s\") from", gd,
826 appending ? "a" : "w");
827 close (gd);
828 return NOTOK;
829 }
830 } else {
831 if ((fp = fopen (ct->c_storage, appending ? "a" : "w")) == NULL) {
832 advise (ct->c_storage, "unable to fopen for %s",
833 appending ? "appending" : "writing");
834 return NOTOK;
835 }
836 }
837
838 /*
839 * Copy a few of the header fields of the initial
840 * enclosing message/partial into the file.
841 */
842 filterstate = 0;
843 if (ct->c_type == CT_MESSAGE && ct->c_subtype == MESSAGE_PARTIAL) {
844 struct partial *pm = (struct partial *) ct->c_ctparams;
845
846 if (pm->pm_partno == 1) {
847 copy_some_headers (fp, ct);
848 filterstate = 1;
849 }
850 }
851
852 while (fgets (buffer, sizeof buffer, ct->c_fp)) {
853 if ((pos += strlen (buffer)) > last) {
854 int diff;
855
856 diff = strlen (buffer) - (pos - last);
857 if (diff >= 0)
858 buffer[diff] = '\0';
859 }
860 /*
861 * If this is the first content of a group of
862 * message/partial contents, then we only copy a few
863 * of the header fields of the enclosed message.
864 */
865 if (filterstate) {
866 switch (buffer[0]) {
867 case ' ':
868 case '\t':
869 if (filterstate < 0)
870 buffer[0] = 0;
871 break;
872
873 case '\n':
874 filterstate = 0;
875 break;
876
877 default:
878 if (!uprf (buffer, XXX_FIELD_PRF)
879 && !uprf (buffer, VRSN_FIELD)
880 && !uprf (buffer, "Subject:")
881 && !uprf (buffer, "Encrypted:")
882 && !uprf (buffer, "Message-ID:")) {
883 filterstate = -1;
884 buffer[0] = 0;
885 break;
886 }
887 filterstate = 1;
888 break;
889 }
890 }
891 fputs (buffer, fp);
892 if (pos >= last)
893 break;
894 }
895
896 if (fflush (fp))
897 advise (ct->c_storage, "error writing to");
898
899 fclose (fp);
900 fclose (ct->c_fp);
901 ct->c_fp = NULL;
902 return OK;
903 }
904
905
906 /*
907 * Add a file to a folder.
908 *
909 * Return the new message number of the file
910 * when added to the folder. Return -1, if
911 * there is an error.
912 */
913
914 static int
915 output_content_folder (char *folder, char *filename)
916 {
917 int msgnum;
918 struct msgs *mp;
919
920 /* Read the folder. */
921 if (!(mp = folder_read(folder, 0))) {
922 inform("unable to read folder %s", folder);
923 return NOTOK;
924 }
925 /* Link file into folder */
926 msgnum = folder_addmsg(&mp, filename, 0, 0, 0, 0, NULL);
927
928 /* free folder structure */
929 folder_free (mp);
930
931 /*
932 * Return msgnum. We are relying on the fact that
933 * msgnum will be -1, if folder_addmsg() had an error.
934 */
935 return msgnum;
936 }
937
938
939 /*
940 * Parse and expand the storage formatting string
941 * pointed to by "cp" into "buffer".
942 */
943
944 static int
945 parse_format_string (CT ct, char *cp, char *buffer, int buflen, char *dir)
946 {
947 int len;
948 char *bp;
949 CI ci = &ct->c_ctinfo;
950
951 /*
952 * If storage string is "-", just copy it, and
953 * return (send content to standard output).
954 */
955 if (cp[0] == '-' && cp[1] == '\0') {
956 strncpy (buffer, cp, buflen);
957 return 0;
958 }
959
960 bp = buffer;
961 bp[0] = '\0';
962
963 /*
964 * If formatting string is a pathname that doesn't
965 * begin with '/', then preface the path with the
966 * appropriate directory.
967 */
968 if (*cp != '/' && *cp != '|' && *cp != '!') {
969 if (!strcmp(dir, "/"))
970 dir = ""; /* Don't start with "//". */
971 snprintf (bp, buflen, "%s/", dir);
972 len = strlen (bp);
973 bp += len;
974 buflen -= len;
975 }
976
977 for (; *cp; cp++) {
978
979 /* We are processing a storage escape */
980 if (*cp == '%') {
981 switch (*++cp) {
982 case 'a':
983 /*
984 * Insert parameters from Content-Type.
985 * This is only valid for '|' commands.
986 */
987 if (buffer[0] != '|' && buffer[0] != '!') {
988 *bp++ = *--cp;
989 *bp = '\0';
990 buflen--;
991 continue;
992 }
993 {
994 PM pm;
995 char *s = "";
996
997 for (pm = ci->ci_first_pm; pm; pm = pm->pm_next) {
998 snprintf (bp, buflen, "%s%s=\"%s\"", s,
999 pm->pm_name, get_param_value(pm, '?'));
1000 len = strlen (bp);
1001 bp += len;
1002 buflen -= len;
1003 s = " ";
1004 }
1005 }
1006 break;
1007
1008 case 'm':
1009 /* insert message number */
1010 snprintf (bp, buflen, "%s", r1bindex (ct->c_file, '/'));
1011 break;
1012
1013 case 'P':
1014 /* insert part number with leading dot */
1015 if (ct->c_partno)
1016 snprintf (bp, buflen, ".%s", ct->c_partno);
1017 break;
1018
1019 case 'p':
1020 /* insert part number without leading dot */
1021 if (ct->c_partno)
1022 strncpy (bp, ct->c_partno, buflen);
1023 break;
1024
1025 case 't':
1026 /* insert content type */
1027 strncpy (bp, ci->ci_type, buflen);
1028 break;
1029
1030 case 's':
1031 /* insert content subtype */
1032 strncpy (bp, ci->ci_subtype, buflen);
1033 break;
1034
1035 case '%':
1036 /* insert the character % */
1037 goto raw;
1038
1039 default:
1040 *bp++ = *--cp;
1041 *bp = '\0';
1042 buflen--;
1043 continue;
1044 }
1045
1046 /* Advance bp and decrement buflen */
1047 len = strlen (bp);
1048 bp += len;
1049 buflen -= len;
1050
1051 } else {
1052 raw:
1053 *bp++ = *cp;
1054 *bp = '\0';
1055 buflen--;
1056 }
1057 }
1058
1059 return 0;
1060 }
1061
1062
1063 /*
1064 * Check if the content specifies a filename
1065 * in its MIME parameters.
1066 */
1067
1068 static void
1069 get_storeproc (CT ct)
1070 {
1071 char *cp;
1072 CI ci;
1073
1074 /*
1075 * If the storeproc has already been defined,
1076 * we just return (for instance, if this content
1077 * is part of a "message/external".
1078 */
1079 if (ct->c_storeproc)
1080 return;
1081
1082 /*
1083 * If there's a Content-Disposition header and it has a filename,
1084 * use that (RFC-2183).
1085 */
1086 if (ct->c_dispo) {
1087 if ((cp = get_param(ct->c_dispo_first, "filename", '_', 0)) &&
1088 use_param_as_filename(cp)) {
1089 ct->c_storeproc = mh_xstrdup(cp);
1090 free(cp);
1091 return;
1092 }
1093 free(cp);
1094 }
1095
1096 /*
1097 * Check the attribute/value pairs, for the attribute "name".
1098 * If found, do a few sanity checks and copy the value into
1099 * the storeproc.
1100 */
1101 ci = &ct->c_ctinfo;
1102 if ((cp = get_param(ci->ci_first_pm, "name", '_', 0)) &&
1103 use_param_as_filename(cp)) {
1104 ct->c_storeproc = mh_xstrdup(cp);
1105
1106 }
1107 free(cp);
1108 }
1109
1110
1111 /*
1112 * Copy some of the header fields of the initial message/partial
1113 * message into the header of the reassembled message.
1114 */
1115
1116 static int
1117 copy_some_headers (FILE *out, CT ct)
1118 {
1119 HF hp;
1120
1121 hp = ct->c_first_hf; /* start at first header field */
1122
1123 while (hp) {
1124 /*
1125 * A few of the header fields of the enclosing
1126 * messages are not copied.
1127 */
1128 if (!uprf (hp->name, XXX_FIELD_PRF)
1129 && strcasecmp (hp->name, VRSN_FIELD)
1130 && strcasecmp (hp->name, "Subject")
1131 && strcasecmp (hp->name, "Encrypted")
1132 && strcasecmp (hp->name, "Message-ID"))
1133 fprintf (out, "%s:%s", hp->name, hp->value);
1134 hp = hp->next; /* next header field */
1135 }
1136
1137 return OK;
1138 }
1139
1140 /******************************************************************************/
1141 /* -clobber support */
1142
1143 static enum clobber_policy_t
1144 clobber_policy (const char *value)
1145 {
1146 if (value == NULL || ! strcasecmp (value, "always")) {
1147 return NMH_CLOBBER_ALWAYS;
1148 }
1149 if (! strcasecmp (value, "auto")) {
1150 return NMH_CLOBBER_AUTO;
1151 }
1152 if (! strcasecmp (value, "suffix")) {
1153 return NMH_CLOBBER_SUFFIX;
1154 }
1155 if (! strcasecmp (value, "ask")) {
1156 return NMH_CLOBBER_ASK;
1157 }
1158 if (! strcasecmp (value, "never")) {
1159 return NMH_CLOBBER_NEVER;
1160 }
1161
1162 die("invalid argument, %s, to clobber", value);
1163 }
1164
1165
1166 static char *
1167 next_version (char *file, enum clobber_policy_t clobber_policy)
1168 {
1169 const size_t max_versions = 1000000;
1170 /* 8 = log max_versions + one for - or . + one for null terminator */
1171 const size_t buflen = strlen (file) + 8;
1172 char *buffer = mh_xmalloc (buflen);
1173 size_t version;
1174
1175 char *extension = NULL;
1176 if (clobber_policy == NMH_CLOBBER_AUTO &&
1177 ((extension = strrchr (file, '.')) != NULL)) {
1178 *extension++ = '\0';
1179 }
1180
1181 for (version = 1; version < max_versions; ++version) {
1182 int fd;
1183
1184 switch (clobber_policy) {
1185 case NMH_CLOBBER_AUTO: {
1186 snprintf (buffer, buflen, "%s-%ld%s%s", file, (long) version,
1187 extension == NULL ? "" : ".",
1188 extension == NULL ? "" : extension);
1189 break;
1190 }
1191
1192 case NMH_CLOBBER_SUFFIX:
1193 snprintf (buffer, buflen, "%s.%ld", file, (long) version);
1194 break;
1195
1196 default:
1197 /* Should never get here. */
1198 inform("will not overwrite %s, invalid clobber policy", buffer);
1199 free (buffer);
1200 return NULL;
1201 }
1202
1203 /* Actually (try to) create the file here to avoid a race
1204 condition on file naming + creation. This won't solve the
1205 problem with old NFS that doesn't support O_EXCL, though.
1206 Let the umask strip off permissions from 0666 as desired.
1207 That's what fopen () would do if it was creating the file. */
1208 if ((fd = open (buffer, O_CREAT | O_EXCL,
1209 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP |
1210 S_IROTH | S_IWOTH)) >= 0) {
1211 close (fd);
1212 break;
1213 }
1214 }
1215
1216 free (file);
1217
1218 if (version >= max_versions) {
1219 inform("will not overwrite %s, too many versions", buffer);
1220 free (buffer);
1221 buffer = NULL;
1222 }
1223
1224 return buffer;
1225 }
1226
1227
1228 static char *
1229 clobber_check (char *original_file, mhstoreinfo_t info)
1230 {
1231 /* clobber policy return value
1232 * -------------- ------------
1233 * -always original_file
1234 * -auto original_file-<digits>.extension
1235 * -suffix original_file.<digits>
1236 * -ask original_file, 0, or another filename/path
1237 * -never 0
1238 */
1239
1240 char *file;
1241 char *cwd = NULL;
1242 bool check_again;
1243
1244 if (! strcmp (original_file, "-")) {
1245 return original_file;
1246 }
1247
1248 if (info->clobber_policy == NMH_CLOBBER_ASK) {
1249 /* Save cwd for possible use in loop below. */
1250 char *slash;
1251
1252 cwd = mh_xstrdup(original_file);
1253 slash = strrchr (cwd, '/');
1254
1255 if (slash) {
1256 *slash = '\0';
1257 } else {
1258 /* original_file isn't a full path, which should only happen if
1259 it is -. */
1260 free (cwd);
1261 cwd = NULL;
1262 }
1263 }
1264
1265 do {
1266 struct stat st;
1267
1268 file = original_file;
1269 check_again = false;
1270
1271 switch (info->clobber_policy) {
1272 case NMH_CLOBBER_ALWAYS:
1273 break;
1274
1275 case NMH_CLOBBER_SUFFIX:
1276 case NMH_CLOBBER_AUTO:
1277 if (stat (file, &st) == OK) {
1278 if ((file = next_version (original_file, info->clobber_policy)) ==
1279 NULL) {
1280 ++info->files_not_clobbered;
1281 }
1282 }
1283 break;
1284
1285 case NMH_CLOBBER_ASK:
1286 if (stat (file, &st) == OK) {
1287 enum answers { NMH_YES, NMH_NO, NMH_RENAME };
1288 static struct swit answer[4] = {
1289 { "yes", 0, NMH_YES },
1290 { "no", 0, NMH_NO },
1291 { "rename", 0, NMH_RENAME },
1292 { NULL, 0, 0 } };
1293 char **ans;
1294
1295 if (isatty (fileno (stdin))) {
1296 char *prompt =
1297 concat ("Overwrite \"", file, "\" [y/n/rename]? ", NULL);
1298 ans = read_switch_multiword (prompt, answer);
1299 free (prompt);
1300 } else {
1301 /* Overwrite, that's what nmh used to do. And warn. */
1302 inform("-clobber ask but no tty, so overwrite %s", file);
1303 break;
1304 }
1305
1306 switch ((enum answers) smatch (*ans, answer)) {
1307 case NMH_YES:
1308 break;
1309 case NMH_NO:
1310 free (file);
1311 file = NULL;
1312 ++info->files_not_clobbered;
1313 break;
1314 case NMH_RENAME: {
1315 char buf[PATH_MAX];
1316 fputs("Enter filename or full path of the new file: ", stdout);
1317 if (fgets (buf, sizeof buf, stdin) == NULL ||
1318 buf[0] == '\0') {
1319 file = NULL;
1320 ++info->files_not_clobbered;
1321 } else {
1322 trim_suffix_c(buf, '\n');
1323 }
1324
1325 free (file);
1326
1327 if (buf[0] == '/') {
1328 /* Full path, use it. */
1329 file = mh_xstrdup(buf);
1330 } else {
1331 /* Relative path. */
1332 file = cwd ? concat (cwd, "/", buf, NULL) : mh_xstrdup(buf);
1333 }
1334
1335 check_again = true;
1336 break;
1337 }
1338 }
1339 }
1340 break;
1341
1342 case NMH_CLOBBER_NEVER:
1343 if (stat (file, &st) == OK) {
1344 /* Keep count of files that would have been clobbered,
1345 and return that as process exit status. */
1346 inform("will not overwrite %s with -clobber never", file);
1347 free (file);
1348 file = NULL;
1349 ++info->files_not_clobbered;
1350 }
1351 break;
1352 }
1353
1354 original_file = file;
1355 } while (check_again);
1356
1357 free (cwd);
1358
1359 return file;
1360 }
1361
1362 static bool
1363 use_param_as_filename(const char *p)
1364 {
1365 /* Preserve result of original test that considered an empty string
1366 * OK. */
1367 return !*p || (!strchr("/.|!", *p) && !strchr(p, '%'));
1368 }
1369
1370 /* -clobber support */
1371 /******************************************************************************/