]> diplodocus.org Git - nmh/blob - uip/mhfixmsg.c
picksbr.c: fprintf(3) function pointer using `%p' and union.
[nmh] / uip / mhfixmsg.c
1 /* mhfixmsg.c -- rewrite a message with various transformations
2 *
3 * This code is Copyright (c) 2002 and 2013, by the authors of nmh.
4 * See the COPYRIGHT file in the root directory of the nmh
5 * distribution for complete copyright information.
6 */
7
8 #include <h/mh.h>
9 #include <h/fmt_scan.h>
10 #include <h/mime.h>
11 #include <h/mhparse.h>
12 #include "h/done.h"
13 #include <h/utils.h>
14 #include <h/signals.h>
15 #include "sbr/m_maildir.h"
16 #include "sbr/m_mktemp.h"
17 #include "sbr/mime_type.h"
18 #include "mhmisc.h"
19 #include "mhfree.h"
20 #include "mhoutsbr.h"
21 #include "mhshowsbr.h"
22 #include <fcntl.h>
23
24 #define MHFIXMSG_SWITCHES \
25 X("decodetext 8bit|7bit|binary", 0, DECODETEXTSW) \
26 X("nodecodetext", 0, NDECODETEXTSW) \
27 X("decodetypes", 0, DECODETYPESW) \
28 X("crlflinebreaks", 0, CRLFLINEBREAKSSW) \
29 X("nocrlflinebreaks", 0, NCRLFLINEBREAKSSW) \
30 X("textcharset", 0, TEXTCHARSETSW) \
31 X("notextcharset", 0, NTEXTCHARSETSW) \
32 X("reformat", 0, REFORMATSW) \
33 X("noreformat", 0, NREFORMATSW) \
34 X("replacetextplain", 0, REPLACETEXTPLAINSW) \
35 X("noreplacetextplain", 0, NREPLACETEXTPLAINSW) \
36 X("fixboundary", 0, FIXBOUNDARYSW) \
37 X("nofixboundary", 0, NFIXBOUNDARYSW) \
38 X("fixcte", 0, FIXCOMPOSITECTESW) \
39 X("nofixcte", 0, NFIXCOMPOSITECTESW) \
40 X("fixtype mimetype", 0, FIXTYPESW) \
41 X("file file", 0, FILESW) \
42 X("outfile file", 0, OUTFILESW) \
43 X("rmmproc program", 0, RPROCSW) \
44 X("normmproc", 0, NRPRCSW) \
45 X("changecur", 0, CHGSW) \
46 X("nochangecur", 0, NCHGSW) \
47 X("verbose", 0, VERBSW) \
48 X("noverbose", 0, NVERBSW) \
49 X("version", 0, VERSIONSW) \
50 X("help", 0, HELPSW) \
51
52 #define X(sw, minchars, id) id,
53 DEFINE_SWITCH_ENUM(MHFIXMSG);
54 #undef X
55
56 #define X(sw, minchars, id) { sw, minchars, id },
57 DEFINE_SWITCH_ARRAY(MHFIXMSG, switches);
58 #undef X
59
60
61 int verbosw;
62 int debugsw; /* Needed by mhparse.c. */
63
64 #define quitser pipeser
65
66 /*
67 * static prototypes
68 */
69 typedef struct fix_transformations {
70 int fixboundary;
71 int fixcompositecte;
72 svector_t fixtypes;
73 int reformat;
74 int replacetextplain;
75 int decodetext;
76 char *decodetypes;
77 /* Whether to use CRLF linebreaks, per RFC 2046 Sec. 4.1.1, par.1. */
78 int lf_line_endings;
79 char *textcharset;
80 } fix_transformations;
81
82 static int mhfixmsgsbr (CT *, char *, const fix_transformations *,
83 FILE **, char *, FILE **);
84 static int fix_boundary (CT *, int *);
85 static int copy_input_to_output (const char *, FILE *, const char *, FILE *);
86 static int get_multipart_boundary (CT, char **);
87 static int replace_boundary (CT, char *, char *);
88 static int fix_types (CT, svector_t, int *);
89 static char *replace_substring (char **, const char *, const char *);
90 static char *remove_parameter (char *, const char *);
91 static int fix_composite_cte (CT, int *);
92 static int set_ce (CT, int);
93 static int ensure_text_plain (CT *, CT, int *, int);
94 static int find_textplain_sibling (CT, int, int *);
95 static int insert_new_text_plain_part (CT, int, CT);
96 static CT build_text_plain_part (CT);
97 static int insert_into_new_mp_alt (CT *, int *);
98 static CT divide_part (CT);
99 static void copy_ctinfo (CI, CI);
100 static int decode_part (CT);
101 static int reformat_part (CT, char *, char *, char *, int);
102 static CT build_multipart_alt (CT, CT, int, int);
103 static int boundary_in_content (FILE **, char *, const char *);
104 static void transfer_noncontent_headers (CT, CT);
105 static int set_ct_type (CT, int type, int subtype, int encoding);
106 static int decode_text_parts (CT, int, const char *, int *);
107 static int should_decode(const char *, const char *, const char *);
108 static int content_encoding (CT, const char **);
109 static int strip_crs (CT, int *);
110 static void update_cte (CT);
111 static int least_restrictive_encoding (CT) PURE;
112 static int less_restrictive (int, int);
113 static int convert_charsets (CT, char *, int *);
114 static int fix_always (CT, int *);
115 static int fix_filename_param (char *, char *, PM *, PM *);
116 static int fix_filename_encoding (CT);
117 static int write_content (CT, const char *, char *, FILE *, int, int);
118 static void set_text_ctparams(CT, char *, int);
119 static int remove_file (const char *);
120 static void report (char *, char *, char *, char *, ...)
121 CHECK_PRINTF(4, 5);
122 static void pipeser (int);
123
124
125 int
126 main (int argc, char **argv) {
127 int msgnum;
128 char *cp, *file = NULL, *folder = NULL;
129 char *maildir = NULL, buf[100], *outfile = NULL;
130 char **argp, **arguments;
131 struct msgs_array msgs = { 0, 0, NULL };
132 struct msgs *mp = NULL;
133 CT *ctp;
134 FILE *fp, *infp = NULL, *outfp = NULL;
135 int using_stdin = 0;
136 int chgflag = 1;
137 int status = OK;
138 fix_transformations fx;
139 fx.reformat = fx.fixcompositecte = fx.fixboundary = 1;
140 fx.fixtypes = NULL;
141 fx.replacetextplain = 0;
142 fx.decodetext = CE_8BIT;
143 fx.decodetypes = "text,application/ics"; /* Default, per man page. */
144 fx.lf_line_endings = 0;
145 fx.textcharset = NULL;
146
147 if (nmh_init(argv[0], 2)) { return 1; }
148
149 arguments = getarguments (invo_name, argc, argv, 1);
150 argp = arguments;
151
152 /*
153 * Parse arguments
154 */
155 while ((cp = *argp++)) {
156 if (*cp == '-') {
157 switch (smatch (++cp, switches)) {
158 case AMBIGSW:
159 ambigsw (cp, switches);
160 done (1);
161 case UNKWNSW:
162 adios (NULL, "-%s unknown", cp);
163
164 case HELPSW:
165 snprintf (buf, sizeof buf, "%s [+folder] [msgs] [switches]",
166 invo_name);
167 print_help (buf, switches, 1);
168 done (0);
169 case VERSIONSW:
170 print_version(invo_name);
171 done (0);
172
173 case DECODETEXTSW:
174 if (! (cp = *argp++) || *cp == '-') {
175 adios (NULL, "missing argument to %s", argp[-2]);
176 }
177 if (! strcasecmp (cp, "8bit")) {
178 fx.decodetext = CE_8BIT;
179 } else if (! strcasecmp (cp, "7bit")) {
180 fx.decodetext = CE_7BIT;
181 } else if (! strcasecmp (cp, "binary")) {
182 fx.decodetext = CE_BINARY;
183 } else {
184 adios (NULL, "invalid argument to %s", argp[-2]);
185 }
186 continue;
187 case NDECODETEXTSW:
188 fx.decodetext = 0;
189 continue;
190 case DECODETYPESW:
191 if (! (cp = *argp++) || *cp == '-') {
192 adios (NULL, "missing argument to %s", argp[-2]);
193 }
194 fx.decodetypes = cp;
195 continue;
196 case CRLFLINEBREAKSSW:
197 fx.lf_line_endings = 0;
198 continue;
199 case NCRLFLINEBREAKSSW:
200 fx.lf_line_endings = 1;
201 continue;
202 case TEXTCHARSETSW:
203 if (! (cp = *argp++) || (*cp == '-' && cp[1])) {
204 adios (NULL, "missing argument to %s", argp[-2]);
205 }
206 fx.textcharset = cp;
207 continue;
208 case NTEXTCHARSETSW:
209 fx.textcharset = 0;
210 continue;
211 case FIXBOUNDARYSW:
212 fx.fixboundary = 1;
213 continue;
214 case NFIXBOUNDARYSW:
215 fx.fixboundary = 0;
216 continue;
217 case FIXCOMPOSITECTESW:
218 fx.fixcompositecte = 1;
219 continue;
220 case NFIXCOMPOSITECTESW:
221 fx.fixcompositecte = 0;
222 continue;
223 case FIXTYPESW:
224 if (! (cp = *argp++) || (*cp == '-' && cp[1])) {
225 adios (NULL, "missing argument to %s", argp[-2]);
226 }
227 if (! strncasecmp (cp, "multipart/", 10) ||
228 ! strncasecmp (cp, "message/", 8))
229 adios (NULL, "-fixtype %s not allowed", cp);
230 if (! strchr (cp, '/'))
231 adios (NULL, "-fixtype requires type/subtype");
232 if (fx.fixtypes == NULL) { fx.fixtypes = svector_create (10); }
233 svector_push_back (fx.fixtypes, cp);
234 continue;
235 case REFORMATSW:
236 fx.reformat = 1;
237 continue;
238 case NREFORMATSW:
239 fx.reformat = 0;
240 continue;
241 case REPLACETEXTPLAINSW:
242 fx.replacetextplain = 1;
243 continue;
244 case NREPLACETEXTPLAINSW:
245 fx.replacetextplain = 0;
246 continue;
247 case FILESW:
248 if (! (cp = *argp++) || (*cp == '-' && cp[1])) {
249 adios (NULL, "missing argument to %s", argp[-2]);
250 }
251 file = *cp == '-' ? mh_xstrdup (cp) : path (cp, TFILE);
252 continue;
253 case OUTFILESW:
254 if (! (cp = *argp++) || (*cp == '-' && cp[1])) {
255 adios (NULL, "missing argument to %s", argp[-2]);
256 }
257 outfile = *cp == '-' ? mh_xstrdup (cp) : path (cp, TFILE);
258 continue;
259 case RPROCSW:
260 if (!(rmmproc = *argp++) || *rmmproc == '-') {
261 adios (NULL, "missing argument to %s", argp[-2]);
262 }
263 continue;
264 case NRPRCSW:
265 rmmproc = NULL;
266 continue;
267 case CHGSW:
268 chgflag = 1;
269 continue;
270 case NCHGSW:
271 chgflag = 0;
272 continue;
273 case VERBSW:
274 verbosw = 1;
275 continue;
276 case NVERBSW:
277 verbosw = 0;
278 continue;
279 }
280 }
281 if (*cp == '+' || *cp == '@') {
282 if (folder)
283 adios (NULL, "only one folder at a time!");
284 folder = pluspath (cp);
285 } else {
286 if (*cp == '/') {
287 /* Interpret a full path as a filename, not a message. */
288 file = mh_xstrdup (cp);
289 } else {
290 app_msgarg (&msgs, cp);
291 }
292 }
293 }
294
295 SIGNAL (SIGQUIT, quitser);
296 SIGNAL (SIGPIPE, pipeser);
297
298 /*
299 * Read the standard profile setup
300 */
301 if ((fp = fopen (cp = etcpath ("mhn.defaults"), "r"))) {
302 readconfig(NULL, fp, cp, 0);
303 fclose (fp);
304 }
305
306 suppress_bogus_mp_content_warning = skip_mp_cte_check = true;
307 suppress_extraneous_trailing_semicolon_warning = true;
308
309 if (! context_find ("path")) {
310 free (path ("./", TFOLDER));
311 }
312
313 if (file && msgs.size) {
314 adios (NULL, "cannot specify msg and file at same time!");
315 }
316
317 if (outfile) {
318 /* Open the outfile now, so we don't have to risk opening it
319 after running out of fds. */
320 if (strcmp (outfile, "-") == 0) {
321 outfp = stdout;
322 } else if ((outfp = fopen (outfile, "w")) == NULL) {
323 adios (outfile, "unable to open for writing");
324 }
325 }
326
327 /*
328 * check if message is coming from file
329 */
330 if (file) {
331 /* If file is stdin, create a tmp file name before parse_mime()
332 has a chance, because it might put in on a different
333 filesystem than the output file. Instead, put it in the
334 user's preferred tmp directory. */
335 CT ct;
336
337 if (! strcmp ("-", file)) {
338 int fd;
339 char *cp;
340
341 using_stdin = 1;
342
343 if ((cp = m_mktemp2 (NULL, invo_name, &fd, NULL)) == NULL) {
344 adios (NULL, "unable to create temporary file in %s",
345 get_temp_dir());
346 } else {
347 free (file);
348 file = mh_xstrdup (cp);
349 cpydata (STDIN_FILENO, fd, "-", file);
350 }
351
352 if (close (fd)) {
353 (void) m_unlink (file);
354 adios (NULL, "failed to write temporary file");
355 }
356 }
357
358 cts = mh_xcalloc(2, sizeof *cts);
359 ctp = cts;
360
361 if ((ct = parse_mime (file))) {
362 set_text_ctparams(ct, fx.decodetypes, fx.lf_line_endings);
363 *ctp++ = ct;
364 } else {
365 inform("unable to parse message from file %s", file);
366 status = NOTOK;
367
368 /* If there's an outfile, pass the input message unchanged, so the
369 message won't get dropped from a pipeline. */
370 if (outfile) {
371 /* Something went wrong. Output might be expected, such as if
372 this were run as a filter. Just copy the input to the
373 output. */
374 if ((infp = fopen (file, "r")) == NULL) {
375 adios (file, "unable to open for reading");
376 }
377
378 if (copy_input_to_output (file, infp, outfile, outfp) != OK) {
379 inform("unable to copy message to %s, "
380 "it might be lost\n", outfile);
381 }
382
383 fclose (infp);
384 infp = NULL;
385 }
386 }
387 } else {
388 /*
389 * message(s) are coming from a folder
390 */
391 CT ct;
392
393 if (! msgs.size) {
394 app_msgarg(&msgs, "cur");
395 }
396 if (! folder) {
397 folder = getfolder (1);
398 }
399 maildir = mh_xstrdup(m_maildir (folder));
400
401 /* chdir so that error messages, esp. from MIME parser, just
402 refer to the message and not its path. */
403 if (chdir (maildir) == NOTOK) {
404 adios (maildir, "unable to change directory to");
405 }
406
407 /* read folder and create message structure */
408 if (! (mp = folder_read (folder, 1))) {
409 adios (NULL, "unable to read folder %s", folder);
410 }
411
412 /* check for empty folder */
413 if (mp->nummsg == 0) {
414 adios (NULL, "no messages in %s", folder);
415 }
416
417 /* parse all the message ranges/sequences and set SELECTED */
418 for (msgnum = 0; msgnum < msgs.size; msgnum++)
419 if (! m_convert (mp, msgs.msgs[msgnum])) {
420 done (1);
421 }
422 seq_setprev (mp); /* set the previous-sequence */
423
424 cts = mh_xcalloc(mp->numsel + 1, sizeof *cts);
425 ctp = cts;
426
427 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
428 if (is_selected(mp, msgnum)) {
429 char *msgnam = m_name (msgnum);
430
431 if ((ct = parse_mime (msgnam))) {
432 set_text_ctparams(ct, fx.decodetypes, fx.lf_line_endings);
433 *ctp++ = ct;
434 } else {
435 inform("unable to parse message %s", msgnam);
436 status = NOTOK;
437
438 /* If there's an outfile, pass the input message
439 unchanged, so the message won't get dropped from a
440 pipeline. */
441 if (outfile) {
442 /* Something went wrong. Output might be expected,
443 such as if this were run as a filter. Just copy
444 the input to the output. */
445 /* Can't use path() here because 1) it might have been
446 called before and it caches the pwd, and 2) we call
447 chdir() after that. */
448 char *input_filename =
449 concat (maildir, "/", msgnam, NULL);
450
451 if ((infp = fopen (input_filename, "r")) == NULL) {
452 adios (input_filename,
453 "unable to open for reading");
454 }
455
456 if (copy_input_to_output (input_filename, infp,
457 outfile, outfp) != OK) {
458 inform("unable to copy message to %s, "
459 "it might be lost\n", outfile);
460 }
461
462 fclose (infp);
463 infp = NULL;
464 free (input_filename);
465 }
466 }
467 }
468 }
469
470 if (chgflag) {
471 seq_setcur (mp, mp->hghsel); /* update current message */
472 }
473 seq_save (mp); /* synchronize sequences */
474 context_replace (pfolder, folder);/* update current folder */
475 context_save (); /* save the context file */
476 }
477
478 if (*cts) {
479 for (ctp = cts; *ctp; ++ctp) {
480 status =
481 mhfixmsgsbr (ctp, maildir, &fx, &infp, outfile, &outfp) == OK
482 ? 0
483 : 1;
484 free_content (*ctp);
485
486 if (using_stdin) {
487 (void) m_unlink (file);
488
489 if (! outfile) {
490 /* Just calling m_backup() unlinks the backup file. */
491 (void) m_backup (file);
492 }
493 }
494 }
495 } else {
496 status = 1;
497 }
498
499 free(maildir);
500 free (cts);
501
502 if (fx.fixtypes != NULL) { svector_free (fx.fixtypes); }
503 if (infp) { fclose (infp); } /* even if stdin */
504 if (outfp) { fclose (outfp); } /* even if stdout */
505 free (outfile);
506 free (file);
507 free (folder);
508 free (arguments);
509
510 done (status == OK ? 0 : 1);
511 return NOTOK;
512 }
513
514
515 /*
516 * Apply transformations to one message.
517 */
518 static int
519 mhfixmsgsbr (CT *ctp, char *maildir, const fix_transformations *fx,
520 FILE **infp, char *outfile, FILE **outfp) {
521 /* Store input filename in case one of the transformations, i.e.,
522 fix_boundary(), rewrites to a tmp file. */
523 char *input_filename = maildir
524 ? concat (maildir, "/", (*ctp)->c_file, NULL)
525 : mh_xstrdup ((*ctp)->c_file);
526 int modify_inplace = 0;
527 int message_mods = 0;
528 int status = OK;
529
530 /* Though the input file won't need to be opened if everything goes
531 well, do it here just in case there's a failure, and that failure is
532 running out of file descriptors. */
533 if ((*infp = fopen (input_filename, "r")) == NULL) {
534 adios (input_filename, "unable to open for reading");
535 }
536
537 if (outfile == NULL) {
538 modify_inplace = 1;
539
540 if ((*ctp)->c_file) {
541 char *tempfile;
542 /* outfp will be closed by the caller */
543 if ((tempfile = m_mktemp2 (NULL, invo_name, NULL, outfp)) ==
544 NULL) {
545 adios (NULL, "unable to create temporary file in %s",
546 get_temp_dir());
547 }
548 outfile = mh_xstrdup (tempfile);
549 } else {
550 adios (NULL, "missing both input and output filenames\n");
551 }
552 } /* else *outfp was defined by caller */
553
554 reverse_alternative_parts (*ctp);
555 status = fix_always (*ctp, &message_mods);
556 if (status == OK && fx->fixboundary) {
557 status = fix_boundary (ctp, &message_mods);
558 }
559 if (status == OK && fx->fixtypes != NULL) {
560 status = fix_types (*ctp, fx->fixtypes, &message_mods);
561 }
562 if (status == OK && fx->fixcompositecte) {
563 status = fix_composite_cte (*ctp, &message_mods);
564 }
565 if (status == OK && fx->reformat) {
566 status =
567 ensure_text_plain (ctp, NULL, &message_mods, fx->replacetextplain);
568 }
569 if (status == OK && fx->decodetext) {
570 status = decode_text_parts (*ctp, fx->decodetext, fx->decodetypes,
571 &message_mods);
572 update_cte (*ctp);
573 }
574 if (status == OK && fx->textcharset != NULL) {
575 status = convert_charsets (*ctp, fx->textcharset, &message_mods);
576 }
577
578 if (status == OK && ! (*ctp)->c_umask) {
579 /* Set the umask for the contents file. This currently
580 isn't used but just in case it is in the future. */
581 struct stat st;
582
583 if (stat ((*ctp)->c_file, &st) != NOTOK) {
584 (*ctp)->c_umask = ~(st.st_mode & 0777);
585 } else {
586 (*ctp)->c_umask = ~m_gmprot();
587 }
588 }
589
590 /*
591 * Write the content to a file
592 */
593 if (status == OK) {
594 status = write_content (*ctp, input_filename, outfile, *outfp,
595 modify_inplace, message_mods);
596 } else if (! modify_inplace) {
597 /* Something went wrong. Output might be expected, such
598 as if this were run as a filter. Just copy the input
599 to the output. */
600 if (copy_input_to_output (input_filename, *infp, outfile,
601 *outfp) != OK) {
602 inform("unable to copy message to %s, it might be lost\n",
603 outfile);
604 }
605 }
606
607 if (modify_inplace) {
608 if (status != OK) { (void) m_unlink (outfile); }
609 free (outfile);
610 outfile = NULL;
611 }
612
613 fclose (*infp);
614 *infp = NULL;
615 free (input_filename);
616
617 return status;
618 }
619
620
621 /*
622 * Copy input message to output. Assumes not modifying in place, so this
623 * might be running as part of a pipeline.
624 */
625 static int
626 copy_input_to_output (const char *input_filename, FILE *infp,
627 const char *output_filename, FILE *outfp) {
628 int in = fileno (infp);
629 int out = fileno (outfp);
630 int status = OK;
631
632 if (in != -1 && out != -1) {
633 cpydata (in, out, input_filename, output_filename);
634 } else {
635 status = NOTOK;
636 }
637
638 return status;
639 }
640
641
642 /*
643 * Fix mismatched outer level boundary.
644 */
645 static int
646 fix_boundary (CT *ct, int *message_mods) {
647 struct multipart *mp;
648 int status = OK;
649
650 if (ct && (*ct)->c_type == CT_MULTIPART && bogus_mp_content) {
651 mp = (struct multipart *) (*ct)->c_ctparams;
652
653 /*
654 * 1) Get boundary at end of part.
655 * 2) Get boundary at beginning of part and compare to the end-of-part
656 * boundary.
657 * 3) Write out contents of ct to tmp file, replacing boundary in
658 * header with boundary from part. Set c_unlink to 1.
659 * 4) Free ct.
660 * 5) Call parse_mime() on the tmp file, replacing ct.
661 */
662
663 if (mp && mp->mp_start) {
664 char *part_boundary;
665
666 if (get_multipart_boundary (*ct, &part_boundary) == OK) {
667 char *fixed;
668
669 if ((fixed = m_mktemp2 (NULL, invo_name, NULL, &(*ct)->c_fp))) {
670 if (replace_boundary (*ct, fixed, part_boundary) == OK) {
671 char *filename = mh_xstrdup ((*ct)->c_file);
672 CT fixed_ct;
673
674 free_content (*ct);
675 if ((fixed_ct = parse_mime (fixed))) {
676 *ct = fixed_ct;
677 (*ct)->c_unlink = 1;
678
679 ++*message_mods;
680 if (verbosw) {
681 report (NULL, NULL, filename,
682 "fix multipart boundary");
683 }
684 } else {
685 *ct = NULL;
686 inform("unable to parse fixed part");
687 status = NOTOK;
688 }
689 free (filename);
690 } else {
691 inform("unable to replace broken boundary");
692 status = NOTOK;
693 }
694 } else {
695 inform("unable to create temporary file in %s",
696 get_temp_dir());
697 status = NOTOK;
698 }
699
700 free (part_boundary);
701 } else {
702 /* Couldn't fix the boundary. Report failure so that mhfixmsg
703 doesn't modify the message. */
704 status = NOTOK;
705 }
706 } else {
707 /* No multipart struct, even though the content type is
708 CT_MULTIPART. Report failure so that mhfixmsg doesn't modify
709 the message. */
710 status = NOTOK;
711 }
712 }
713
714 return status;
715 }
716
717
718 /*
719 * Find boundary at end of multipart.
720 */
721 static int
722 get_multipart_boundary (CT ct, char **part_boundary) {
723 char buffer[NMH_BUFSIZ];
724 char *end_boundary = NULL;
725 off_t begin = (off_t) ct->c_end > (off_t) (ct->c_begin + sizeof buffer)
726 ? (off_t) (ct->c_end - sizeof buffer)
727 : (off_t) ct->c_begin;
728 size_t bytes_read;
729 int status = OK;
730
731 /* This will fail if the boundary spans fread() calls. NMH_BUFSIZ should
732 be big enough, even if it's just 1024, to make that unlikely. */
733
734 /* free_content() will close ct->c_fp if bogus MP boundary is fixed. */
735 if (! ct->c_fp && (ct->c_fp = fopen (ct->c_file, "r")) == NULL) {
736 advise (ct->c_file, "unable to open for reading");
737 return NOTOK;
738 }
739
740 /* Get boundary at end of multipart. */
741 while (begin >= (off_t) ct->c_begin) {
742 fseeko (ct->c_fp, begin, SEEK_SET);
743 while ((bytes_read = fread (buffer, 1, sizeof buffer, ct->c_fp)) > 0) {
744 char *cp = rfind_str (buffer, bytes_read, "--");
745
746 if (cp) {
747 char *end;
748
749 /* Trim off trailing "--" and anything beyond. */
750 *cp-- = '\0';
751 if ((end = rfind_str (buffer, cp - buffer, "\n"))) {
752 if (strlen (end) > 3 && *end++ == '\n' &&
753 *end++ == '-' && *end++ == '-') {
754 end_boundary = mh_xstrdup (end);
755 break;
756 }
757 }
758 }
759 }
760
761 if (end_boundary || begin <= (off_t) (ct->c_begin + sizeof buffer))
762 break;
763 begin -= sizeof buffer;
764 }
765
766 /* Get boundary at beginning of multipart. */
767 if (end_boundary) {
768 fseeko (ct->c_fp, ct->c_begin, SEEK_SET);
769 while ((bytes_read = fread (buffer, 1, sizeof buffer, ct->c_fp)) > 0) {
770 if (bytes_read >= strlen (end_boundary)) {
771 char *cp = find_str (buffer, bytes_read, end_boundary);
772
773 if (cp && cp - buffer >= 2 && *--cp == '-' &&
774 *--cp == '-' && (cp > buffer && *--cp == '\n')) {
775 status = OK;
776 break;
777 }
778 } else {
779 /* The start and end boundaries didn't match, or the
780 start boundary doesn't begin with "\n--" (or "--"
781 if at the beginning of buffer). Keep trying. */
782 status = NOTOK;
783 }
784 }
785 } else {
786 status = NOTOK;
787 }
788
789 if (ct->c_fp) {
790 fclose (ct->c_fp);
791 ct->c_fp = NULL;
792 }
793
794 if (status == OK) {
795 *part_boundary = end_boundary;
796 } else {
797 *part_boundary = NULL;
798 free (end_boundary);
799 }
800
801 return status;
802 }
803
804
805 /*
806 * Open and copy ct->c_file to file, replacing the multipart boundary.
807 */
808 static int
809 replace_boundary (CT ct, char *file, char *boundary) {
810 FILE *fpin, *fpout;
811 int compnum, state;
812 char buf[NMH_BUFSIZ], name[NAMESZ];
813 char *np, *vp;
814 m_getfld_state_t gstate;
815 int status = OK;
816
817 if (ct->c_file == NULL) {
818 inform("missing input filename");
819 return NOTOK;
820 }
821
822 if ((fpin = fopen (ct->c_file, "r")) == NULL) {
823 advise (ct->c_file, "unable to open for reading");
824 return NOTOK;
825 }
826
827 if ((fpout = fopen (file, "w")) == NULL) {
828 fclose (fpin);
829 advise (file, "unable to open for writing");
830 return NOTOK;
831 }
832
833 gstate = m_getfld_state_init(fpin);
834 for (compnum = 1;;) {
835 int bufsz = (int) sizeof buf;
836
837 switch (state = m_getfld2(&gstate, name, buf, &bufsz)) {
838 case FLD:
839 case FLDPLUS:
840 compnum++;
841
842 /* get copies of the buffers */
843 np = mh_xstrdup (name);
844 vp = mh_xstrdup (buf);
845
846 /* if necessary, get rest of field */
847 while (state == FLDPLUS) {
848 bufsz = sizeof buf;
849 state = m_getfld2(&gstate, name, buf, &bufsz);
850 vp = add (buf, vp); /* add to previous value */
851 }
852
853 if (strcasecmp (TYPE_FIELD, np)) {
854 fprintf (fpout, "%s:%s", np, vp);
855 } else {
856 char *new_ctline, *new_params;
857
858 replace_param(&ct->c_ctinfo.ci_first_pm,
859 &ct->c_ctinfo.ci_last_pm, "boundary",
860 boundary, 0);
861
862 new_ctline = concat(" ", ct->c_ctinfo.ci_type, "/",
863 ct->c_ctinfo.ci_subtype, NULL);
864 new_params = output_params(LEN(TYPE_FIELD) +
865 strlen(new_ctline) + 1,
866 ct->c_ctinfo.ci_first_pm, NULL, 0);
867 fprintf (fpout, "%s:%s%s\n", np, new_ctline,
868 FENDNULL(new_params));
869 free(new_ctline);
870 free(new_params);
871 }
872
873 free (vp);
874 free (np);
875
876 continue;
877
878 case BODY:
879 putc('\n', fpout);
880 /* buf will have a terminating NULL, skip it. */
881 if ((int) fwrite (buf, 1, bufsz-1, fpout) < bufsz-1) {
882 advise (file, "fwrite");
883 }
884 continue;
885
886 case FILEEOF:
887 break;
888
889 case LENERR:
890 case FMTERR:
891 inform("message format error in component #%d", compnum);
892 status = NOTOK;
893 break;
894
895 default:
896 inform("getfld() returned %d", state);
897 status = NOTOK;
898 break;
899 }
900
901 break;
902 }
903
904 m_getfld_state_destroy (&gstate);
905 fclose (fpout);
906 fclose (fpin);
907
908 return status;
909 }
910
911
912 /*
913 * Fix Content-Type header to reflect the content of its part.
914 */
915 static int
916 fix_types (CT ct, svector_t fixtypes, int *message_mods) {
917 int status = OK;
918
919 switch (ct->c_type) {
920 case CT_MULTIPART: {
921 struct multipart *m = (struct multipart *) ct->c_ctparams;
922 struct part *part;
923
924 for (part = m->mp_parts; status == OK && part; part = part->mp_next) {
925 status = fix_types (part->mp_part, fixtypes, message_mods);
926 }
927 break;
928 }
929
930 case CT_MESSAGE:
931 if (ct->c_subtype == MESSAGE_EXTERNAL) {
932 struct exbody *e = (struct exbody *) ct->c_ctparams;
933
934 status = fix_types (e->eb_content, fixtypes, message_mods);
935 }
936 break;
937
938 default: {
939 char **typep, *type;
940
941 if (ct->c_ctinfo.ci_type && ct->c_ctinfo.ci_subtype) {
942 for (typep = svector_strs (fixtypes);
943 typep && (type = *typep);
944 ++typep) {
945 char *type_subtype =
946 concat (ct->c_ctinfo.ci_type, "/", ct->c_ctinfo.ci_subtype,
947 NULL);
948
949 if (! strcasecmp (type, type_subtype) &&
950 decode_part (ct) == OK &&
951 ct->c_cefile.ce_file != NULL) {
952 char *ct_type_subtype = mime_type (ct->c_cefile.ce_file);
953 char *cp;
954
955 if ((cp = strchr (ct_type_subtype, ';'))) {
956 /* Truncate to remove any parameter list from
957 mime_type () result. */
958 *cp = '\0';
959 }
960
961 if (strcasecmp (type, ct_type_subtype)) {
962 char *ct_type, *ct_subtype;
963 HF hf;
964
965 /* The Content-Type header does not match the
966 content, so update these struct Content
967 fields to match:
968 * c_type, c_subtype
969 * c_ctinfo.ci_type, c_ctinfo.ci_subtype
970 * c_ctline
971 */
972 /* Extract type and subtype from type/subtype. */
973 ct_type = mh_xstrdup(ct_type_subtype);
974 if ((cp = strchr (ct_type, '/'))) {
975 *cp = '\0';
976 ct_subtype = mh_xstrdup(++cp);
977 } else {
978 inform("missing / in MIME type of %s %s",
979 ct->c_file, ct->c_partno);
980 free (ct_type);
981 return NOTOK;
982 }
983
984 ct->c_type = ct_str_type (ct_type);
985 ct->c_subtype = ct_str_subtype (ct->c_type, ct_subtype);
986
987 free (ct->c_ctinfo.ci_type);
988 ct->c_ctinfo.ci_type = ct_type;
989 free (ct->c_ctinfo.ci_subtype);
990 ct->c_ctinfo.ci_subtype = ct_subtype;
991 if (! replace_substring (&ct->c_ctline, type,
992 ct_type_subtype)) {
993 inform("did not find %s in %s",
994 type, ct->c_ctline);
995 }
996
997 /* Update Content-Type header field. */
998 for (hf = ct->c_first_hf; hf; hf = hf->next) {
999 if (! strcasecmp (TYPE_FIELD, hf->name)) {
1000 if (replace_substring (&hf->value, type,
1001 ct_type_subtype)) {
1002 ++*message_mods;
1003 if (verbosw) {
1004 report (NULL, ct->c_partno, ct->c_file,
1005 "change Content-Type in header "
1006 "from %s to %s",
1007 type, ct_type_subtype);
1008 }
1009 break;
1010 }
1011 inform("did not find %s in %s", type, hf->value);
1012 }
1013 }
1014 }
1015 free (ct_type_subtype);
1016 }
1017 free (type_subtype);
1018 }
1019 }
1020 }}
1021
1022 return status;
1023 }
1024
1025
1026 /*
1027 * Replace a substring, allocating space to hold the new one.
1028 */
1029 char *
1030 replace_substring (char **str, const char *old, const char *new) {
1031 char *cp;
1032
1033 if ((cp = strstr (*str, old))) {
1034 char *remainder = cp + strlen (old);
1035 char *prefix, *new_str;
1036
1037 if (cp - *str) {
1038 prefix = mh_xstrdup(*str);
1039 *(prefix + (cp - *str)) = '\0';
1040 new_str = concat (prefix, new, remainder, NULL);
1041 free (prefix);
1042 } else {
1043 new_str = concat (new, remainder, NULL);
1044 }
1045
1046 free (*str);
1047
1048 return *str = new_str;
1049 }
1050
1051 return NULL;
1052 }
1053
1054
1055 /*
1056 * Remove a name=value parameter, given just its name, from a header value.
1057 */
1058 char *
1059 remove_parameter (char *str, const char *name) {
1060 /* It looks to me, based on the BNF in RFC 2045, than there can't
1061 be whitespace between the parameter name and the "=", or
1062 between the "=" and the parameter value. */
1063 char *param_name = concat (name, "=", NULL);
1064 char *cp;
1065
1066 if ((cp = strstr (str, param_name))) {
1067 char *start, *end;
1068 size_t count = 1;
1069
1070 /* Remove any leading spaces, before the parameter name. */
1071 for (start = cp;
1072 start > str && isspace ((unsigned char) *(start-1));
1073 --start) {
1074 continue;
1075 }
1076 /* Remove a leading semicolon. */
1077 if (start > str && *(start-1) == ';') { --start; }
1078
1079 end = cp + strlen (name) + 1;
1080 if (*end == '"') {
1081 /* Skip past the quoted value, and then the final quote. */
1082 for (++end ; *end && *end != '"'; ++end) { continue; }
1083 ++end;
1084 } else {
1085 /* Skip past the value. */
1086 for (++end ; *end && ! isspace ((unsigned char) *end); ++end) {}
1087 }
1088
1089 /* Count how many characters need to be moved. Include
1090 trailing null, which is accounted for by the
1091 initialization of count to 1. */
1092 for (cp = end; *cp; ++cp) { ++count; }
1093 (void) memmove (start, end, count);
1094 }
1095
1096 free (param_name);
1097
1098 return str;
1099 }
1100
1101
1102 /*
1103 * Fix Content-Transfer-Encoding of composite,, e.g., message or multipart, part.
1104 * According to RFC 2045 Sec. 6.4, it must be 7bit, 8bit, or binary. Set it to
1105 * 8 bit.
1106 */
1107 static int
1108 fix_composite_cte (CT ct, int *message_mods) {
1109 int status = OK;
1110
1111 if (ct->c_type == CT_MESSAGE || ct->c_type == CT_MULTIPART) {
1112 if (ct->c_encoding != CE_7BIT && ct->c_encoding != CE_8BIT &&
1113 ct->c_encoding != CE_BINARY) {
1114 HF hf;
1115
1116 for (hf = ct->c_first_hf; hf; hf = hf->next) {
1117 char *name = hf->name;
1118 for (; isspace((unsigned char)*name); ++name) {
1119 continue;
1120 }
1121
1122 if (! strncasecmp (name, ENCODING_FIELD,
1123 LEN(ENCODING_FIELD))) {
1124 char *prefix = "Nmh-REPLACED-INVALID-";
1125 HF h;
1126
1127 NEW(h);
1128 h->name = mh_xstrdup (hf->name);
1129 h->hf_encoding = hf->hf_encoding;
1130 h->next = hf->next;
1131 hf->next = h;
1132
1133 /* Retain old header but prefix its name. */
1134 free (hf->name);
1135 hf->name = concat (prefix, h->name, NULL);
1136
1137 ++*message_mods;
1138 if (verbosw) {
1139 char *encoding = cpytrim (hf->value);
1140 report (NULL, ct->c_partno, ct->c_file,
1141 "replace Content-Transfer-Encoding of %s "
1142 "with 8 bit", encoding);
1143 free (encoding);
1144 }
1145
1146 h->value = mh_xstrdup (" 8bit\n");
1147
1148 /* Don't need to warn for multiple C-T-E header
1149 fields, parse_mime() already does that. But
1150 if there are any, fix them all as necessary. */
1151 hf = h;
1152 }
1153 }
1154
1155 set_ce (ct, CE_8BIT);
1156 }
1157
1158 if (ct->c_type == CT_MULTIPART) {
1159 struct multipart *m;
1160 struct part *part;
1161
1162 m = (struct multipart *) ct->c_ctparams;
1163 for (part = m->mp_parts; part; part = part->mp_next) {
1164 if (fix_composite_cte (part->mp_part, message_mods) != OK) {
1165 status = NOTOK;
1166 break;
1167 }
1168 }
1169 }
1170 }
1171
1172 return status;
1173 }
1174
1175
1176 /*
1177 * Set content encoding.
1178 */
1179 static int
1180 set_ce (CT ct, int encoding) {
1181 const char *ce = ce_str (encoding);
1182 const struct str2init *ctinit = get_ce_method (ce);
1183
1184 if (ctinit) {
1185 char *cte = concat (" ", ce, "\n", NULL);
1186 int found_cte = 0;
1187 HF hf;
1188 /* Decoded contents might be in ct->c_cefile.ce_file, if the
1189 caller is decode_text_parts (). Save because we'll
1190 overwrite below. */
1191 struct cefile decoded_content_info = ct->c_cefile;
1192
1193 ct->c_encoding = encoding;
1194
1195 ct->c_ctinitfnx = ctinit->si_init;
1196 /* This will assign ct->c_cefile with an all-0 struct, which
1197 is what we want. */
1198 (*ctinit->si_init) (ct);
1199 /* After returning, the caller should set
1200 ct->c_cefile.ce_file to the name of the file containing
1201 the contents. */
1202
1203 if (ct->c_ceclosefnx) {
1204 (*ct->c_ceclosefnx) (ct);
1205 }
1206
1207 /* Restore the cefile. */
1208 ct->c_cefile = decoded_content_info;
1209
1210 /* Update/add Content-Transfer-Encoding header field. */
1211 for (hf = ct->c_first_hf; hf; hf = hf->next) {
1212 if (! strcasecmp (ENCODING_FIELD, hf->name)) {
1213 found_cte = 1;
1214 free (hf->value);
1215 hf->value = cte;
1216 }
1217 }
1218 if (! found_cte) {
1219 add_header (ct, mh_xstrdup (ENCODING_FIELD), cte);
1220 }
1221
1222 /* Update c_celine. It's used only by mhlist -debug. */
1223 free (ct->c_celine);
1224 ct->c_celine = mh_xstrdup (cte);
1225
1226 return OK;
1227 }
1228
1229 return NOTOK;
1230 }
1231
1232
1233 /*
1234 * Make sure each text part has a corresponding text/plain part.
1235 */
1236 static int
1237 ensure_text_plain (CT *ct, CT parent, int *message_mods, int replacetextplain) {
1238 int status = OK;
1239
1240 switch ((*ct)->c_type) {
1241 case CT_TEXT: {
1242 /* Nothing to do for text/plain. */
1243 if ((*ct)->c_subtype == TEXT_PLAIN) { return OK; }
1244
1245 if (parent && parent->c_type == CT_MULTIPART &&
1246 parent->c_subtype == MULTI_ALTERNATE) {
1247 int new_subpart_number = 1;
1248 int has_text_plain =
1249 find_textplain_sibling (parent, replacetextplain,
1250 &new_subpart_number);
1251
1252 if (! has_text_plain) {
1253 /* Parent is a multipart/alternative. Insert a new
1254 text/plain subpart. */
1255 const int inserted =
1256 insert_new_text_plain_part (*ct, new_subpart_number,
1257 parent);
1258 if (inserted) {
1259 ++*message_mods;
1260 if (verbosw) {
1261 report (NULL, parent->c_partno, parent->c_file,
1262 "insert text/plain part");
1263 }
1264 } else {
1265 status = NOTOK;
1266 }
1267 }
1268 } else if (parent && parent->c_type == CT_MULTIPART &&
1269 parent->c_subtype == MULTI_RELATED) {
1270 char *type_subtype =
1271 concat ((*ct)->c_ctinfo.ci_type, "/",
1272 (*ct)->c_ctinfo.ci_subtype, NULL);
1273 const char *parent_type =
1274 get_param (parent->c_ctinfo.ci_first_pm, "type", '?', 1);
1275 int new_subpart_number = 1;
1276 int has_text_plain = 0;
1277
1278 /* Have to do string comparison on the subtype because we
1279 don't enumerate all of them in c_subtype values.
1280 parent_type will be NULL if the multipart/related part
1281 doesn't have a type parameter. The type parameter must
1282 be specified according to RFC 2387 Sec. 3.1 but not all
1283 messages comply. */
1284 if (parent_type && strcasecmp (type_subtype, parent_type) == 0) {
1285 /* The type of this part matches the root type of the
1286 parent multipart/related. Look to see if there's
1287 text/plain sibling. */
1288 has_text_plain =
1289 find_textplain_sibling (parent, replacetextplain,
1290 &new_subpart_number);
1291 }
1292
1293 free (type_subtype);
1294
1295 if (! has_text_plain) {
1296 struct multipart *mp = (struct multipart *) parent->c_ctparams;
1297 struct part *part;
1298 int siblings = 0;
1299
1300 for (part = mp->mp_parts; part; part = part->mp_next) {
1301 if (*ct != part->mp_part) {
1302 ++siblings;
1303 }
1304 }
1305
1306 if (siblings) {
1307 /* Parent is a multipart/related. Insert a new
1308 text/plain subpart in a new multipart/alternative. */
1309 if (insert_into_new_mp_alt (ct, message_mods)) {
1310 /* Not an error if text/plain couldn't be added. */
1311 }
1312 } else {
1313 /* There are no siblings, so insert a new text/plain
1314 subpart, and change the parent type from
1315 multipart/related to multipart/alternative. */
1316 const int inserted =
1317 insert_new_text_plain_part (*ct, new_subpart_number,
1318 parent);
1319
1320 if (inserted) {
1321 HF hf;
1322
1323 parent->c_subtype = MULTI_ALTERNATE;
1324 free (parent->c_ctinfo.ci_subtype);
1325 parent->c_ctinfo.ci_subtype = mh_xstrdup("alternative");
1326 if (! replace_substring (&parent->c_ctline, "/related",
1327 "/alternative")) {
1328 inform("did not find multipart/related in %s",
1329 parent->c_ctline);
1330 }
1331
1332 /* Update Content-Type header field. */
1333 for (hf = parent->c_first_hf; hf; hf = hf->next) {
1334 if (! strcasecmp (TYPE_FIELD, hf->name)) {
1335 if (replace_substring (&hf->value, "/related",
1336 "/alternative")) {
1337 ++*message_mods;
1338 if (verbosw) {
1339 report (NULL, parent->c_partno,
1340 parent->c_file,
1341 "insert text/plain part");
1342 }
1343
1344 /* Remove, e.g., type="text/html" from
1345 multipart/alternative. */
1346 remove_parameter (hf->value, "type");
1347 break;
1348 }
1349 inform("did not find multipart/"
1350 "related in header %s", hf->value);
1351 }
1352 }
1353 } else {
1354 /* Not an error if text/plain couldn't be inserted. */
1355 }
1356 }
1357 }
1358 } else {
1359 if (insert_into_new_mp_alt (ct, message_mods)) {
1360 status = NOTOK;
1361 }
1362 }
1363 break;
1364 }
1365
1366 case CT_MULTIPART: {
1367 struct multipart *mp = (struct multipart *) (*ct)->c_ctparams;
1368 struct part *part;
1369
1370 for (part = mp->mp_parts; status == OK && part; part = part->mp_next) {
1371 if ((*ct)->c_type == CT_MULTIPART) {
1372 status = ensure_text_plain (&part->mp_part, *ct, message_mods,
1373 replacetextplain);
1374 }
1375 }
1376 break;
1377 }
1378
1379 case CT_MESSAGE:
1380 if ((*ct)->c_subtype == MESSAGE_EXTERNAL) {
1381 struct exbody *e = (struct exbody *) (*ct)->c_ctparams;
1382
1383 status = ensure_text_plain (&e->eb_content, *ct, message_mods,
1384 replacetextplain);
1385 }
1386 break;
1387 }
1388
1389 return status;
1390 }
1391
1392
1393 /*
1394 * See if there is a sibling text/plain, and return its subpart number.
1395 */
1396 static int
1397 find_textplain_sibling (CT parent, int replacetextplain,
1398 int *new_subpart_number) {
1399 struct multipart *mp = (struct multipart *) parent->c_ctparams;
1400 struct part *part, *prev;
1401 int has_text_plain = 0;
1402
1403 for (prev = part = mp->mp_parts; part; part = part->mp_next) {
1404 ++*new_subpart_number;
1405 if (part->mp_part->c_type == CT_TEXT &&
1406 part->mp_part->c_subtype == TEXT_PLAIN) {
1407 if (replacetextplain) {
1408 struct part *old_part;
1409 if (part == mp->mp_parts) {
1410 old_part = mp->mp_parts;
1411 mp->mp_parts = part->mp_next;
1412 } else {
1413 old_part = prev->mp_next;
1414 prev->mp_next = part->mp_next;
1415 }
1416 if (verbosw) {
1417 report (NULL, parent->c_partno, parent->c_file,
1418 "remove text/plain part %s",
1419 old_part->mp_part->c_partno);
1420 }
1421 free_content (old_part->mp_part);
1422 free (old_part);
1423 } else {
1424 has_text_plain = 1;
1425 }
1426 break;
1427 }
1428 prev = part;
1429 }
1430
1431 return has_text_plain;
1432 }
1433
1434
1435 /*
1436 * Insert a new text/plain part.
1437 */
1438 static int
1439 insert_new_text_plain_part (CT ct, int new_subpart_number, CT parent) {
1440 struct multipart *mp = (struct multipart *) parent->c_ctparams;
1441 struct part *new_part;
1442
1443 NEW(new_part);
1444 if ((new_part->mp_part = build_text_plain_part (ct))) {
1445 char buffer[16];
1446 snprintf (buffer, sizeof buffer, "%d", new_subpart_number);
1447
1448 new_part->mp_next = mp->mp_parts;
1449 mp->mp_parts = new_part;
1450 new_part->mp_part->c_partno =
1451 concat (parent->c_partno ? parent->c_partno : "1", ".",
1452 buffer, NULL);
1453
1454 return 1;
1455 }
1456
1457 free_content (new_part->mp_part);
1458 free (new_part);
1459
1460 return 0;
1461 }
1462
1463
1464 /*
1465 * Create a text/plain part to go along with non-plain sibling part.
1466 */
1467 static CT
1468 build_text_plain_part (CT encoded_part) {
1469 CT tp_part = divide_part (encoded_part);
1470 char *tmp_plain_file = NULL;
1471
1472 if (decode_part (tp_part) == OK) {
1473 /* Now, tp_part->c_cefile.ce_file is the name of the tmp file that
1474 contains the decoded contents. And the decoding function, such
1475 as openQuoted, will have set ...->ce_unlink to 1 so that it will
1476 be unlinked by free_content (). */
1477 char *tempfile;
1478
1479 /* This m_mktemp2() call closes the temp file. */
1480 if ((tempfile = m_mktemp2 (NULL, invo_name, NULL, NULL)) == NULL) {
1481 inform("unable to create temporary file in %s",
1482 get_temp_dir());
1483 } else {
1484 tmp_plain_file = mh_xstrdup (tempfile);
1485 if (reformat_part (tp_part, tmp_plain_file,
1486 tp_part->c_ctinfo.ci_type,
1487 tp_part->c_ctinfo.ci_subtype,
1488 tp_part->c_type) == OK) {
1489 return tp_part;
1490 }
1491 }
1492 }
1493
1494 free_content (tp_part);
1495 if (tmp_plain_file) { (void) m_unlink (tmp_plain_file); }
1496 free (tmp_plain_file);
1497
1498 return NULL;
1499 }
1500
1501
1502 /*
1503 * Slip new text/plain part into a new multipart/alternative.
1504 */
1505 static int
1506 insert_into_new_mp_alt (CT *ct, int *message_mods) {
1507 CT tp_part = build_text_plain_part (*ct);
1508 int status = OK;
1509
1510 if (tp_part) {
1511 CT mp_alt = build_multipart_alt (*ct, tp_part, CT_MULTIPART,
1512 MULTI_ALTERNATE);
1513 if (mp_alt) {
1514 struct multipart *mp = (struct multipart *) mp_alt->c_ctparams;
1515
1516 if (mp && mp->mp_parts) {
1517 mp->mp_parts->mp_part = tp_part;
1518 /* Make the new multipart/alternative the parent. */
1519 *ct = mp_alt;
1520
1521 ++*message_mods;
1522 if (verbosw) {
1523 report (NULL, (*ct)->c_partno, (*ct)->c_file,
1524 "insert text/plain part");
1525 }
1526 } else {
1527 free_content (tp_part);
1528 free_content (mp_alt);
1529 status = NOTOK;
1530 }
1531 } else {
1532 status = NOTOK;
1533 }
1534 } else {
1535 /* Not an error if text/plain couldn't be built. */
1536 }
1537
1538 return status;
1539 }
1540
1541
1542 /*
1543 * Clone a MIME part.
1544 */
1545 static CT
1546 divide_part (CT ct) {
1547 CT new_part;
1548
1549 NEW0(new_part);
1550 /* Just copy over what is needed for decoding. c_vrsn and
1551 c_celine aren't necessary. */
1552 new_part->c_file = mh_xstrdup (ct->c_file);
1553 new_part->c_begin = ct->c_begin;
1554 new_part->c_end = ct->c_end;
1555 copy_ctinfo (&new_part->c_ctinfo, &ct->c_ctinfo);
1556 new_part->c_type = ct->c_type;
1557 new_part->c_cefile = ct->c_cefile;
1558 new_part->c_encoding = ct->c_encoding;
1559 new_part->c_ctinitfnx = ct->c_ctinitfnx;
1560 new_part->c_ceopenfnx = ct->c_ceopenfnx;
1561 new_part->c_ceclosefnx = ct->c_ceclosefnx;
1562 new_part->c_cesizefnx = ct->c_cesizefnx;
1563
1564 /* c_ctline is used by reformat__part(), so it can preserve
1565 anything after the type/subtype. */
1566 new_part->c_ctline = mh_xstrdup (ct->c_ctline);
1567
1568 return new_part;
1569 }
1570
1571
1572 /*
1573 * Copy the content info from one part to another.
1574 */
1575 static void
1576 copy_ctinfo (CI dest, CI src) {
1577 PM s_pm, d_pm;
1578
1579 dest->ci_type = src->ci_type ? mh_xstrdup (src->ci_type) : NULL;
1580 dest->ci_subtype = src->ci_subtype ? mh_xstrdup (src->ci_subtype) : NULL;
1581
1582 for (s_pm = src->ci_first_pm; s_pm; s_pm = s_pm->pm_next) {
1583 d_pm = add_param(&dest->ci_first_pm, &dest->ci_last_pm, s_pm->pm_name,
1584 s_pm->pm_value, 0);
1585 if (s_pm->pm_charset) {
1586 d_pm->pm_charset = mh_xstrdup(s_pm->pm_charset);
1587 }
1588 if (s_pm->pm_lang) {
1589 d_pm->pm_lang = mh_xstrdup(s_pm->pm_lang);
1590 }
1591 }
1592
1593 dest->ci_comment = src->ci_comment ? mh_xstrdup (src->ci_comment) : NULL;
1594 dest->ci_magic = src->ci_magic ? mh_xstrdup (src->ci_magic) : NULL;
1595 }
1596
1597
1598 /*
1599 * Decode content.
1600 */
1601 static int
1602 decode_part (CT ct) {
1603 char *tmp_decoded;
1604 int status;
1605 FILE *file;
1606 char *tempfile;
1607
1608 if ((tempfile = m_mktemp2 (NULL, invo_name, NULL, &file)) == NULL) {
1609 adios (NULL, "unable to create temporary file in %s", get_temp_dir());
1610 }
1611 tmp_decoded = mh_xstrdup (tempfile);
1612 /* The following call will load ct->c_cefile.ce_file with the tmp
1613 filename of the decoded content. tmp_decoded will contain the
1614 encoded output, get rid of that. */
1615 status = output_message_fp (ct, file, tmp_decoded);
1616 (void) m_unlink (tmp_decoded);
1617 free (tmp_decoded);
1618 if (fclose (file)) {
1619 inform("unable to close temporary file %s, continuing...", tempfile);
1620 }
1621
1622 return status;
1623 }
1624
1625
1626 /*
1627 * Reformat content as plain text.
1628 * Some of the arguments aren't really needed now, but maybe will
1629 * be in the future for other than text types.
1630 */
1631 static int
1632 reformat_part (CT ct, char *file, char *type, char *subtype, int c_type) {
1633 int output_subtype, output_encoding;
1634 const char *reason = NULL;
1635 char *cp, *cf;
1636 int status;
1637
1638 /* Hacky: this redirects the output from whatever command is used
1639 to show the part to a file. So, the user can't have any output
1640 redirection in that command.
1641 Could show_multi() in mhshowsbr.c avoid this? */
1642
1643 /* Check for invo_name-format-type/subtype. */
1644 if ((cf = context_find_by_type ("format", type, subtype)) == NULL) {
1645 if (verbosw) {
1646 inform("Don't know how to convert %s, there is no "
1647 "%s-format-%s/%s profile entry",
1648 ct->c_file, invo_name, type, subtype);
1649 }
1650 return NOTOK;
1651 }
1652 if (strchr (cf, '>')) {
1653 inform("'>' prohibited in \"%s\",\nplease fix your "
1654 "%s-format-%s/%s profile entry", cf, invo_name, type,
1655 FENDNULL(subtype));
1656
1657 return NOTOK;
1658 }
1659
1660 cp = concat (cf, " >", file, NULL);
1661 status = show_content_aux (ct, 0, cp, NULL, NULL);
1662 free (cp);
1663
1664 /* Unlink decoded content tmp file and free its filename to avoid
1665 leaks. The file stream should already have been closed. */
1666 if (ct->c_cefile.ce_unlink) {
1667 (void) m_unlink (ct->c_cefile.ce_file);
1668 free (ct->c_cefile.ce_file);
1669 ct->c_cefile.ce_file = NULL;
1670 ct->c_cefile.ce_unlink = 0;
1671 }
1672
1673 if (c_type == CT_TEXT) {
1674 output_subtype = TEXT_PLAIN;
1675 } else {
1676 /* Set subtype to 0, which is always an UNKNOWN subtype. */
1677 output_subtype = 0;
1678 }
1679
1680 output_encoding = content_encoding (ct, &reason);
1681 if (status == OK &&
1682 set_ct_type (ct, c_type, output_subtype, output_encoding) == OK) {
1683 ct->c_cefile.ce_file = file;
1684 ct->c_cefile.ce_unlink = 1;
1685 } else {
1686 ct->c_cefile.ce_unlink = 0;
1687 status = NOTOK;
1688 }
1689
1690 return status;
1691 }
1692
1693
1694 /*
1695 * Fill in a multipart/alternative part.
1696 */
1697 static CT
1698 build_multipart_alt (CT first_alt, CT new_part, int type, int subtype) {
1699 char *boundary_prefix = "----=_nmh-multipart";
1700 char *boundary = concat (boundary_prefix, first_alt->c_partno, NULL);
1701 char *boundary_indicator = "; boundary=";
1702 char *typename, *subtypename, *name;
1703 CT ct;
1704 struct part *p;
1705 struct multipart *m;
1706 const struct str2init *ctinit;
1707
1708 NEW0(ct);
1709
1710 /* Set up the multipart/alternative part. These fields of *ct were
1711 initialized to 0 by mh_xcalloc():
1712 c_fp, c_unlink, c_begin, c_end,
1713 c_vrsn, c_ctline, c_celine,
1714 c_id, c_descr, c_dispo, c_partno,
1715 c_ctinfo.ci_comment, c_ctinfo.ci_magic,
1716 c_cefile, c_encoding,
1717 c_digested, c_digest[16], c_ctexbody,
1718 c_ctinitfnx, c_ceopenfnx, c_ceclosefnx, c_cesizefnx,
1719 c_umask, c_rfc934,
1720 c_showproc, c_termproc, c_storeproc, c_storage, c_folder
1721 */
1722
1723 ct->c_file = mh_xstrdup (first_alt->c_file);
1724 ct->c_type = type;
1725 ct->c_subtype = subtype;
1726
1727 ctinit = get_ct_init (ct->c_type);
1728
1729 typename = ct_type_str (type);
1730 subtypename = ct_subtype_str (type, subtype);
1731
1732 {
1733 int serial = 0;
1734 int found_boundary = 1;
1735
1736 while (found_boundary && serial < 1000000) {
1737 found_boundary = 0;
1738
1739 /* Ensure that the boundary doesn't appear in the decoded
1740 content. */
1741 if (new_part->c_cefile.ce_file) {
1742 if ((found_boundary =
1743 boundary_in_content (&new_part->c_cefile.ce_fp,
1744 new_part->c_cefile.ce_file,
1745 boundary)) == NOTOK) {
1746 goto return_null;
1747 }
1748 }
1749
1750 /* Ensure that the boundary doesn't appear in the encoded
1751 content. */
1752 if (! found_boundary && new_part->c_file) {
1753 if ((found_boundary =
1754 boundary_in_content (&new_part->c_fp,
1755 new_part->c_file,
1756 boundary)) == NOTOK) {
1757 goto return_null;
1758 }
1759 }
1760
1761 if (found_boundary) {
1762 /* Try a slightly different boundary. */
1763 char buffer2[16];
1764
1765 free (boundary);
1766 ++serial;
1767 snprintf (buffer2, sizeof buffer2, "%d", serial);
1768 boundary =
1769 concat (boundary_prefix,
1770 FENDNULL(first_alt->c_partno),
1771 "-", buffer2, NULL);
1772 }
1773 }
1774
1775 if (found_boundary) {
1776 inform("giving up trying to find a unique boundary");
1777 goto return_null;
1778 }
1779 }
1780
1781 name = concat (" ", typename, "/", subtypename, boundary_indicator, "\"",
1782 boundary, "\"", NULL);
1783
1784 /* Load c_first_hf and c_last_hf. */
1785 transfer_noncontent_headers (first_alt, ct);
1786 add_header (ct, mh_xstrdup (TYPE_FIELD), concat (name, "\n", NULL));
1787 free (name);
1788
1789 /* Load c_partno. */
1790 if (first_alt->c_partno) {
1791 ct->c_partno = mh_xstrdup (first_alt->c_partno);
1792 free (first_alt->c_partno);
1793 first_alt->c_partno = concat (ct->c_partno, ".1", NULL);
1794 new_part->c_partno = concat (ct->c_partno, ".2", NULL);
1795 } else {
1796 first_alt->c_partno = mh_xstrdup ("1");
1797 new_part->c_partno = mh_xstrdup ("2");
1798 }
1799
1800 if (ctinit) {
1801 ct->c_ctinfo.ci_type = mh_xstrdup (typename);
1802 ct->c_ctinfo.ci_subtype = mh_xstrdup (subtypename);
1803 }
1804
1805 add_param(&ct->c_ctinfo.ci_first_pm, &ct->c_ctinfo.ci_last_pm,
1806 "boundary", boundary, 0);
1807
1808 NEW(p);
1809 NEW(p->mp_next);
1810 p->mp_next->mp_next = NULL;
1811 p->mp_next->mp_part = first_alt;
1812
1813 NEW0(m);
1814 m->mp_start = concat (boundary, "\n", NULL);
1815 m->mp_stop = concat (boundary, "--\n", NULL);
1816 m->mp_parts = p;
1817 ct->c_ctparams = m;
1818
1819 free (boundary);
1820
1821 return ct;
1822
1823 return_null:
1824 free_content(ct);
1825 free(boundary);
1826 return NULL;
1827 }
1828
1829
1830 /*
1831 * Check that the boundary does not appear in the content.
1832 */
1833 static int
1834 boundary_in_content (FILE **fp, char *file, const char *boundary) {
1835 char buffer[NMH_BUFSIZ];
1836 size_t bytes_read;
1837 int found_boundary = 0;
1838
1839 /* free_content() will close *fp if we fopen it here. */
1840 if (! *fp && (*fp = fopen (file, "r")) == NULL) {
1841 advise (file, "unable to open %s for reading", file);
1842 return NOTOK;
1843 }
1844
1845 fseeko (*fp, 0L, SEEK_SET);
1846 while ((bytes_read = fread (buffer, 1, sizeof buffer, *fp)) > 0) {
1847 if (find_str (buffer, bytes_read, boundary)) {
1848 found_boundary = 1;
1849 break;
1850 }
1851 }
1852
1853 return found_boundary;
1854 }
1855
1856
1857 /*
1858 * Remove all non-Content headers.
1859 */
1860 static void
1861 transfer_noncontent_headers (CT old, CT new) {
1862 HF hp, hp_prev;
1863
1864 hp_prev = hp = old->c_first_hf;
1865 while (hp) {
1866 HF next = hp->next;
1867
1868 if (strncasecmp (XXX_FIELD_PRF, hp->name, LEN(XXX_FIELD_PRF))) {
1869 if (hp == old->c_last_hf) {
1870 if (hp == old->c_first_hf) {
1871 old->c_last_hf = old->c_first_hf = NULL;
1872 } else {
1873 hp_prev->next = NULL;
1874 old->c_last_hf = hp_prev;
1875 }
1876 } else {
1877 if (hp == old->c_first_hf) {
1878 old->c_first_hf = next;
1879 } else {
1880 hp_prev->next = next;
1881 }
1882 }
1883
1884 /* Put node hp in the new CT. */
1885 if (new->c_first_hf == NULL) {
1886 new->c_first_hf = hp;
1887 } else {
1888 new->c_last_hf->next = hp;
1889 }
1890 new->c_last_hf = hp;
1891 } else {
1892 /* A Content- header, leave in old. */
1893 hp_prev = hp;
1894 }
1895
1896 hp = next;
1897 }
1898 }
1899
1900
1901 /*
1902 * Set content type.
1903 */
1904 static int
1905 set_ct_type (CT ct, int type, int subtype, int encoding) {
1906 char *typename = ct_type_str (type);
1907 char *subtypename = ct_subtype_str (type, subtype);
1908 /* E.g, " text/plain" */
1909 char *type_subtypename = concat (" ", typename, "/", subtypename, NULL);
1910 /* E.g, " text/plain\n" */
1911 char *name_plus_nl = concat (type_subtypename, "\n", NULL);
1912 int found_content_type = 0;
1913 HF hf;
1914 const char *cp = NULL;
1915 char *ctline;
1916 int status;
1917
1918 /* Update/add Content-Type header field. */
1919 for (hf = ct->c_first_hf; hf; hf = hf->next) {
1920 if (! strcasecmp (TYPE_FIELD, hf->name)) {
1921 found_content_type = 1;
1922 free (hf->value);
1923 hf->value = (cp = strchr (ct->c_ctline, ';'))
1924 ? concat (type_subtypename, cp, "\n", NULL)
1925 : mh_xstrdup (name_plus_nl);
1926 }
1927 }
1928 if (! found_content_type) {
1929 add_header (ct, mh_xstrdup (TYPE_FIELD),
1930 (cp = strchr (ct->c_ctline, ';'))
1931 ? concat (type_subtypename, cp, "\n", NULL)
1932 : mh_xstrdup (name_plus_nl));
1933 }
1934
1935 /* Some of these might not be used, but set them anyway. */
1936 ctline = cp
1937 ? concat (type_subtypename, cp, NULL)
1938 : concat (type_subtypename, NULL);
1939 free (ct->c_ctline);
1940 ct->c_ctline = ctline;
1941 /* Leave other ctinfo members as they were. */
1942 free (ct->c_ctinfo.ci_type);
1943 ct->c_ctinfo.ci_type = mh_xstrdup (typename);
1944 free (ct->c_ctinfo.ci_subtype);
1945 ct->c_ctinfo.ci_subtype = mh_xstrdup (subtypename);
1946 ct->c_type = type;
1947 ct->c_subtype = subtype;
1948
1949 free (name_plus_nl);
1950 free (type_subtypename);
1951
1952 status = set_ce (ct, encoding);
1953
1954 return status;
1955 }
1956
1957
1958 /*
1959 * It's not necessary to update the charset parameter of a Content-Type
1960 * header for a text part. According to RFC 2045 Sec. 6.4, the body
1961 * (content) was originally in the specified charset, "and will be in
1962 * that character set again after decoding."
1963 */
1964 static int
1965 decode_text_parts (CT ct, int encoding, const char *decodetypes,
1966 int *message_mods) {
1967 int status = OK;
1968 int lf_line_endings = 0;
1969
1970 switch (ct->c_type) {
1971 case CT_MULTIPART: {
1972 struct multipart *m = (struct multipart *) ct->c_ctparams;
1973 struct part *part;
1974
1975 /* Should check to see if the body for this part is encoded?
1976 For now, it gets passed along as-is by InitMultiPart(). */
1977 for (part = m->mp_parts; status == OK && part; part = part->mp_next) {
1978 status = decode_text_parts (part->mp_part, encoding, decodetypes,
1979 message_mods);
1980 }
1981 break;
1982 }
1983
1984 case CT_MESSAGE:
1985 if (ct->c_subtype == MESSAGE_EXTERNAL) {
1986 struct exbody *e = (struct exbody *) ct->c_ctparams;
1987
1988 status = decode_text_parts (e->eb_content, encoding, decodetypes,
1989 message_mods);
1990 }
1991 break;
1992
1993 default:
1994 if (! should_decode(decodetypes, ct->c_ctinfo.ci_type, ct->c_ctinfo.ci_subtype)) {
1995 break;
1996 }
1997
1998 lf_line_endings =
1999 ct->c_ctparams && ((struct text *) ct->c_ctparams)->lf_line_endings;
2000
2001 switch (ct->c_encoding) {
2002 case CE_BASE64:
2003 case CE_QUOTED: {
2004 int ct_encoding;
2005
2006 if (decode_part (ct) == OK && ct->c_cefile.ce_file) {
2007 const char *reason = NULL;
2008
2009 if ((ct_encoding = content_encoding (ct, &reason)) == CE_BINARY
2010 && encoding != CE_BINARY) {
2011 /* The decoding isn't acceptable so discard it.
2012 Leave status as OK to allow other transformations. */
2013 if (verbosw) {
2014 report (NULL, ct->c_partno, ct->c_file,
2015 "will not decode%s because it is binary (%s)",
2016 ct->c_partno ? ""
2017 : (FENDNULL(ct->c_ctline)),
2018 reason);
2019 }
2020 (void) m_unlink (ct->c_cefile.ce_file);
2021 free (ct->c_cefile.ce_file);
2022 ct->c_cefile.ce_file = NULL;
2023 } else if (ct->c_encoding == CE_QUOTED &&
2024 ct_encoding == CE_8BIT && encoding == CE_7BIT) {
2025 /* The decoding isn't acceptable so discard it.
2026 Leave status as OK to allow other transformations. */
2027 if (verbosw) {
2028 report (NULL, ct->c_partno, ct->c_file,
2029 "will not decode%s because it is 8bit",
2030 ct->c_partno ? ""
2031 : (FENDNULL(ct->c_ctline)));
2032 }
2033 (void) m_unlink (ct->c_cefile.ce_file);
2034 free (ct->c_cefile.ce_file);
2035 ct->c_cefile.ce_file = NULL;
2036 } else {
2037 int enc;
2038
2039 if (ct_encoding == CE_BINARY) {
2040 enc = CE_BINARY;
2041 } else if (ct_encoding == CE_8BIT && encoding == CE_7BIT) {
2042 enc = CE_QUOTED;
2043 } else {
2044 enc = ct_encoding;
2045 }
2046 if (set_ce (ct, enc) == OK) {
2047 ++*message_mods;
2048 if (verbosw) {
2049 report (NULL, ct->c_partno, ct->c_file, "decode%s",
2050 FENDNULL(ct->c_ctline));
2051 }
2052 if (lf_line_endings) {
2053 strip_crs (ct, message_mods);
2054 }
2055 } else {
2056 status = NOTOK;
2057 }
2058 }
2059 } else {
2060 status = NOTOK;
2061 }
2062 break;
2063 }
2064 case CE_8BIT:
2065 case CE_7BIT:
2066 if (lf_line_endings) {
2067 strip_crs (ct, message_mods);
2068 }
2069 break;
2070 default:
2071 break;
2072 }
2073
2074 break;
2075 }
2076
2077 return status;
2078 }
2079
2080
2081 /*
2082 * Determine if the part with type[/subtype] should be decoded, according to
2083 * decodetypes (which came from the -decodetypes switch).
2084 */
2085 static int
2086 should_decode(const char *decodetypes, const char *type, const char *subtype) {
2087 /* Quick search for matching type[/subtype] in decodetypes: bracket
2088 decodetypes with commas, then search for ,type, and ,type/subtype, in
2089 it. */
2090
2091 int found_match = 0;
2092 char *delimited_decodetypes = concat(",", decodetypes, ",", NULL);
2093 char *delimited_type = concat(",", type, ",", NULL);
2094
2095 if (nmh_strcasestr(delimited_decodetypes, delimited_type)) {
2096 found_match = 1;
2097 } else if (subtype != NULL) {
2098 char *delimited_type_subtype =
2099 concat(",", type, "/", subtype, ",", NULL);
2100
2101 if (nmh_strcasestr(delimited_decodetypes, delimited_type_subtype)) {
2102 found_match = 1;
2103 }
2104 free(delimited_type_subtype);
2105 }
2106
2107 free(delimited_type);
2108 free(delimited_decodetypes);
2109
2110 return found_match;
2111 }
2112
2113
2114 /*
2115 * See if the decoded content is 7bit, 8bit, or binary. It's binary
2116 * if it has any NUL characters, a CR not followed by a LF, or lines
2117 * greater than 998 characters in length. If binary, reason is set
2118 * to a string explaining why.
2119 */
2120 static int
2121 content_encoding (CT ct, const char **reason) {
2122 CE ce = &ct->c_cefile;
2123 int encoding = CE_7BIT;
2124
2125 if (ce->ce_file) {
2126 size_t line_len = 0;
2127 char buffer[NMH_BUFSIZ];
2128 size_t inbytes;
2129
2130 if (! ce->ce_fp && (ce->ce_fp = fopen (ce->ce_file, "r")) == NULL) {
2131 advise (ce->ce_file, "unable to open for reading");
2132 return CE_UNKNOWN;
2133 }
2134
2135 fseeko (ce->ce_fp, 0L, SEEK_SET);
2136 while (encoding != CE_BINARY &&
2137 (inbytes = fread (buffer, 1, sizeof buffer, ce->ce_fp)) > 0) {
2138 char *cp;
2139 size_t i;
2140 int last_char_was_cr = 0;
2141
2142 for (i = 0, cp = buffer; i < inbytes; ++i, ++cp) {
2143 if (*cp == '\0' || ++line_len > 998 ||
2144 (*cp != '\n' && last_char_was_cr)) {
2145 encoding = CE_BINARY;
2146 if (*cp == '\0') {
2147 *reason = "null character";
2148 } else if (line_len > 998) {
2149 *reason = "line length > 998";
2150 } else if (*cp != '\n' && last_char_was_cr) {
2151 *reason = "CR not followed by LF";
2152 } else {
2153 /* Should not reach this. */
2154 *reason = "";
2155 }
2156 break;
2157 }
2158 if (*cp == '\n') {
2159 line_len = 0;
2160 } else if (! isascii ((unsigned char) *cp)) {
2161 encoding = CE_8BIT;
2162 }
2163
2164 last_char_was_cr = *cp == '\r';
2165 }
2166 }
2167
2168 fclose (ce->ce_fp);
2169 ce->ce_fp = NULL;
2170 } /* else should never happen */
2171
2172 return encoding;
2173 }
2174
2175
2176 /*
2177 * Strip carriage returns from content.
2178 */
2179 static int
2180 strip_crs (CT ct, int *message_mods) {
2181 char *charset = content_charset (ct);
2182 int status = OK;
2183
2184 /* Only strip carriage returns if content is ASCII or another
2185 charset that has the same readily recognizable CR followed by a
2186 LF. We can include UTF-8 here because if the high-order bit of
2187 a UTF-8 byte is 0, then it must be a single-byte ASCII
2188 character. */
2189 if (! strcasecmp (charset, "US-ASCII") ||
2190 ! strcasecmp (charset, "UTF-8") ||
2191 ! strncasecmp (charset, "ISO-8859-", 9) ||
2192 ! strncasecmp (charset, "WINDOWS-12", 10)) {
2193 char **file = NULL;
2194 FILE **fp = NULL;
2195 size_t begin;
2196 size_t end;
2197 int has_crs = 0;
2198 int opened_input_file = 0;
2199
2200 if (ct->c_cefile.ce_file) {
2201 file = &ct->c_cefile.ce_file;
2202 fp = &ct->c_cefile.ce_fp;
2203 begin = end = 0;
2204 } else if (ct->c_file) {
2205 file = &ct->c_file;
2206 fp = &ct->c_fp;
2207 begin = (size_t) ct->c_begin;
2208 end = (size_t) ct->c_end;
2209 } /* else don't know where the content is */
2210
2211 if (file && *file && fp) {
2212 if (! *fp) {
2213 if ((*fp = fopen (*file, "r")) == NULL) {
2214 advise (*file, "unable to open for reading");
2215 status = NOTOK;
2216 } else {
2217 opened_input_file = 1;
2218 }
2219 }
2220 }
2221
2222 if (fp && *fp) {
2223 char buffer[NMH_BUFSIZ];
2224 size_t bytes_read;
2225 size_t bytes_to_read =
2226 end > 0 && end > begin ? end - begin : sizeof buffer;
2227
2228 fseeko (*fp, begin, SEEK_SET);
2229 while ((bytes_read = fread (buffer, 1,
2230 min (bytes_to_read, sizeof buffer),
2231 *fp)) > 0) {
2232 /* Look for CR followed by a LF. This is supposed to
2233 be text so there should be LF's. If not, don't
2234 modify the content. */
2235 char *cp;
2236 size_t i;
2237 int last_char_was_cr = 0;
2238
2239 if (end > 0) { bytes_to_read -= bytes_read; }
2240
2241 for (i = 0, cp = buffer; i < bytes_read; ++i, ++cp) {
2242 if (*cp == '\n' && last_char_was_cr) {
2243 has_crs = 1;
2244 break;
2245 }
2246
2247 last_char_was_cr = *cp == '\r';
2248 }
2249 }
2250
2251 if (has_crs) {
2252 int fd;
2253 char *stripped_content_file;
2254 char *tempfile = m_mktemp2 (NULL, invo_name, &fd, NULL);
2255
2256 if (tempfile == NULL) {
2257 adios (NULL, "unable to create temporary file in %s",
2258 get_temp_dir());
2259 }
2260 stripped_content_file = mh_xstrdup (tempfile);
2261
2262 /* Strip each CR before a LF from the content. */
2263 fseeko (*fp, begin, SEEK_SET);
2264 while ((bytes_read = fread (buffer, 1, sizeof buffer, *fp)) >
2265 0) {
2266 char *cp;
2267 size_t i;
2268 int last_char_was_cr = 0;
2269
2270 for (i = 0, cp = buffer; i < bytes_read; ++i, ++cp) {
2271 if (*cp == '\r') {
2272 last_char_was_cr = 1;
2273 } else if (last_char_was_cr) {
2274 if (*cp != '\n') {
2275 if (write (fd, "\r", 1) < 0) {
2276 advise (tempfile, "CR write");
2277 }
2278 }
2279 if (write (fd, cp, 1) < 0) {
2280 advise (tempfile, "write");
2281 }
2282 last_char_was_cr = 0;
2283 } else {
2284 if (write (fd, cp, 1) < 0) {
2285 advise (tempfile, "write");
2286 }
2287 last_char_was_cr = 0;
2288 }
2289 }
2290 }
2291
2292 if (close (fd)) {
2293 inform("unable to write temporary file %s, continuing...",
2294 stripped_content_file);
2295 (void) m_unlink (stripped_content_file);
2296 free(stripped_content_file);
2297 status = NOTOK;
2298 } else {
2299 /* Replace the decoded file with the converted one. */
2300 if (ct->c_cefile.ce_file && ct->c_cefile.ce_unlink)
2301 (void) m_unlink (ct->c_cefile.ce_file);
2302
2303 free(ct->c_cefile.ce_file);
2304 ct->c_cefile.ce_file = stripped_content_file;
2305 ct->c_cefile.ce_unlink = 1;
2306
2307 ++*message_mods;
2308 if (verbosw) {
2309 report (NULL, ct->c_partno,
2310 begin == 0 && end == 0 ? "" : *file,
2311 "stripped CRs");
2312 }
2313 }
2314 }
2315
2316 if (opened_input_file) {
2317 fclose (*fp);
2318 *fp = NULL;
2319 }
2320 }
2321 }
2322
2323 free (charset);
2324
2325 return status;
2326 }
2327
2328
2329 /*
2330 * Add/update, if necessary, the message C-T-E, based on the least restrictive
2331 * of the part C-T-E's.
2332 */
2333 static void
2334 update_cte (CT ct) {
2335 const int least_restrictive_enc = least_restrictive_encoding (ct);
2336
2337 if (least_restrictive_enc != CE_UNKNOWN &&
2338 least_restrictive_enc != CE_7BIT) {
2339 char *cte = concat (" ", ce_str (least_restrictive_enc), "\n", NULL);
2340 HF hf;
2341 int found_cte = 0;
2342
2343 /* Update/add Content-Transfer-Encoding header field. */
2344 for (hf = ct->c_first_hf; hf; hf = hf->next) {
2345 if (! strcasecmp (ENCODING_FIELD, hf->name)) {
2346 found_cte = 1;
2347 free (hf->value);
2348 hf->value = cte;
2349 }
2350 }
2351 if (! found_cte) {
2352 add_header (ct, mh_xstrdup (ENCODING_FIELD), cte);
2353 }
2354 }
2355 }
2356
2357
2358 /*
2359 * Find the least restrictive encoding (7bit, 8bit, binary) of the parts
2360 * within a message.
2361 */
2362 static int
2363 least_restrictive_encoding (CT ct) {
2364 int encoding = CE_UNKNOWN;
2365
2366 switch (ct->c_type) {
2367 case CT_MULTIPART: {
2368 struct multipart *m = (struct multipart *) ct->c_ctparams;
2369 struct part *part;
2370
2371 for (part = m->mp_parts; part; part = part->mp_next) {
2372 const int part_encoding =
2373 least_restrictive_encoding (part->mp_part);
2374
2375 if (less_restrictive (encoding, part_encoding)) {
2376 encoding = part_encoding;
2377 }
2378 }
2379 break;
2380 }
2381
2382 case CT_MESSAGE:
2383 if (ct->c_subtype == MESSAGE_EXTERNAL) {
2384 struct exbody *e = (struct exbody *) ct->c_ctparams;
2385 const int part_encoding =
2386 least_restrictive_encoding (e->eb_content);
2387
2388 if (less_restrictive (encoding, part_encoding)) {
2389 encoding = part_encoding;
2390 }
2391 }
2392 break;
2393
2394 default: {
2395 if (less_restrictive (encoding, ct->c_encoding)) {
2396 encoding = ct->c_encoding;
2397 }
2398 }}
2399
2400 return encoding;
2401 }
2402
2403
2404 /*
2405 * Return whether the second encoding is less restrictive than the first, where
2406 * "less restrictive" is in the sense used by RFC 2045 Secs. 6.1 and 6.4. So,
2407 * CE_BINARY is less restrictive than CE_8BIT and
2408 * CE_8BIT is less restrictive than CE_7BIT.
2409 */
2410 static int
2411 less_restrictive (int encoding, int second_encoding) {
2412 switch (second_encoding) {
2413 case CE_BINARY:
2414 return encoding != CE_BINARY;
2415 case CE_8BIT:
2416 return encoding != CE_BINARY && encoding != CE_8BIT;
2417 case CE_7BIT:
2418 return encoding != CE_BINARY && encoding != CE_8BIT &&
2419 encoding != CE_7BIT;
2420 default :
2421 return 0;
2422 }
2423 }
2424
2425
2426 /*
2427 * Convert character set of each part.
2428 */
2429 static int
2430 convert_charsets (CT ct, char *dest_charset, int *message_mods) {
2431 int status = OK;
2432
2433 switch (ct->c_type) {
2434 case CT_TEXT:
2435 if (ct->c_subtype == TEXT_PLAIN) {
2436 status = convert_charset (ct, dest_charset, message_mods);
2437 if (status == OK) {
2438 if (verbosw) {
2439 char *ct_charset = content_charset (ct);
2440
2441 report (NULL, ct->c_partno, ct->c_file,
2442 "convert %s to %s", ct_charset, dest_charset);
2443 free (ct_charset);
2444 }
2445 } else {
2446 char *ct_charset = content_charset (ct);
2447
2448 report ("iconv", ct->c_partno, ct->c_file,
2449 "failed to convert %s to %s", ct_charset, dest_charset);
2450 free (ct_charset);
2451 }
2452 }
2453 break;
2454
2455 case CT_MULTIPART: {
2456 struct multipart *m = (struct multipart *) ct->c_ctparams;
2457 struct part *part;
2458
2459 /* Should check to see if the body for this part is encoded?
2460 For now, it gets passed along as-is by InitMultiPart(). */
2461 for (part = m->mp_parts; status == OK && part; part = part->mp_next) {
2462 status =
2463 convert_charsets (part->mp_part, dest_charset, message_mods);
2464 }
2465 break;
2466 }
2467
2468 case CT_MESSAGE:
2469 if (ct->c_subtype == MESSAGE_EXTERNAL) {
2470 struct exbody *e = (struct exbody *) ct->c_ctparams;
2471
2472 status =
2473 convert_charsets (e->eb_content, dest_charset, message_mods);
2474 }
2475 break;
2476
2477 default:
2478 break;
2479 }
2480
2481 return status;
2482 }
2483
2484
2485 /*
2486 * Fix various problems that aren't handled elsewhere. These
2487 * are fixed unconditionally: there are no switches to disable
2488 * them. Currently, "problems" are these:
2489 * 1) remove extraneous semicolon at the end of a header parameter list
2490 * 2) replace RFC 2047 encoding with RFC 2231 encoding of name and
2491 * filename parameters in Content-Type and Content-Disposition
2492 * headers, respectively.
2493 */
2494 static int
2495 fix_always (CT ct, int *message_mods) {
2496 int status = OK;
2497
2498 switch (ct->c_type) {
2499 case CT_MULTIPART: {
2500 struct multipart *m = (struct multipart *) ct->c_ctparams;
2501 struct part *part;
2502
2503 for (part = m->mp_parts; status == OK && part; part = part->mp_next) {
2504 status = fix_always (part->mp_part, message_mods);
2505 }
2506 break;
2507 }
2508
2509 case CT_MESSAGE:
2510 if (ct->c_subtype == MESSAGE_EXTERNAL) {
2511 struct exbody *e = (struct exbody *) ct->c_ctparams;
2512
2513 status = fix_always (e->eb_content, message_mods);
2514 }
2515 break;
2516
2517 default: {
2518 HF hf;
2519
2520 if (ct->c_first_hf) {
2521 fix_filename_encoding (ct);
2522 }
2523
2524 for (hf = ct->c_first_hf; hf; hf = hf->next) {
2525 size_t len = strlen (hf->value);
2526
2527 if (strcasecmp (hf->name, TYPE_FIELD) != 0 &&
2528 strcasecmp (hf->name, DISPO_FIELD) != 0) {
2529 /* Only do this for Content-Type and
2530 Content-Disposition fields because those are the
2531 only headers that parse_mime() warns about. */
2532 continue;
2533 }
2534
2535 /* whitespace following a trailing ';' will be nuked as well */
2536 if (hf->value[len - 1] == '\n') {
2537 while (isspace((unsigned char)(hf->value[len - 2]))) {
2538 if (len-- == 0) { break; }
2539 }
2540 }
2541
2542 if (hf->value[len - 2] == ';') {
2543 /* Remove trailing ';' from parameter value. */
2544 hf->value[len - 2] = '\n';
2545 hf->value[len - 1] = '\0';
2546
2547 /* Also, if Content-Type parameter, remove trailing ';'
2548 from ct->c_ctline. This probably isn't necessary
2549 but can't hurt. */
2550 if (strcasecmp(hf->name, TYPE_FIELD) == 0 && ct->c_ctline) {
2551 size_t l = strlen(ct->c_ctline) - 1;
2552 while (isspace((unsigned char)(ct->c_ctline[l])) ||
2553 ct->c_ctline[l] == ';') {
2554 ct->c_ctline[l--] = '\0';
2555 if (l == 0) { break; }
2556 }
2557 }
2558
2559 ++*message_mods;
2560 if (verbosw) {
2561 report (NULL, ct->c_partno, ct->c_file,
2562 "remove trailing ; from %s parameter value",
2563 hf->name);
2564 }
2565 }
2566 }
2567 }}
2568
2569 return status;
2570 }
2571
2572
2573 /*
2574 * Factor out common code for loops in fix_filename_encoding().
2575 */
2576 static int
2577 fix_filename_param (char *name, char *value, PM *first_pm, PM *last_pm) {
2578 int fixed = 0;
2579
2580 if (has_prefix(value, "=?") && has_suffix(value, "?=")) {
2581 /* Looks like an RFC 2047 encoded parameter. */
2582 char decoded[PATH_MAX + 1];
2583
2584 if (decode_rfc2047 (value, decoded, sizeof decoded)) {
2585 /* Encode using RFC 2231. */
2586 replace_param (first_pm, last_pm, name, decoded, 0);
2587 fixed = 1;
2588 } else {
2589 inform("failed to decode %s parameter %s", name, value);
2590 }
2591 }
2592
2593 return fixed;
2594 }
2595
2596
2597 /*
2598 * Replace RFC 2047 encoding with RFC 2231 encoding of name and
2599 * filename parameters in Content-Type and Content-Disposition
2600 * headers, respectively.
2601 */
2602 static int
2603 fix_filename_encoding (CT ct) {
2604 PM pm;
2605 HF hf;
2606 int fixed = 0;
2607
2608 for (pm = ct->c_ctinfo.ci_first_pm; pm; pm = pm->pm_next) {
2609 if (pm->pm_name && pm->pm_value &&
2610 strcasecmp (pm->pm_name, "name") == 0) {
2611 fixed = fix_filename_param (pm->pm_name, pm->pm_value,
2612 &ct->c_ctinfo.ci_first_pm,
2613 &ct->c_ctinfo.ci_last_pm);
2614 }
2615 }
2616
2617 for (pm = ct->c_dispo_first; pm; pm = pm->pm_next) {
2618 if (pm->pm_name && pm->pm_value &&
2619 strcasecmp (pm->pm_name, "filename") == 0) {
2620 fixed = fix_filename_param (pm->pm_name, pm->pm_value,
2621 &ct->c_dispo_first,
2622 &ct->c_dispo_last);
2623 }
2624 }
2625
2626 /* Fix hf values to correspond. */
2627 for (hf = ct->c_first_hf; fixed && hf; hf = hf->next) {
2628 enum { OTHER, TYPE_HEADER, DISPO_HEADER } field = OTHER;
2629
2630 if (strcasecmp (hf->name, TYPE_FIELD) == 0) {
2631 field = TYPE_HEADER;
2632 } else if (strcasecmp (hf->name, DISPO_FIELD) == 0) {
2633 field = DISPO_HEADER;
2634 }
2635
2636 if (field != OTHER) {
2637 const char *const semicolon_loc = strchr (hf->value, ';');
2638
2639 if (semicolon_loc) {
2640 const size_t len =
2641 strlen (hf->name) + 1 + semicolon_loc - hf->value;
2642 const char *const params =
2643 output_params (len,
2644 field == TYPE_HEADER
2645 ? ct->c_ctinfo.ci_first_pm
2646 : ct->c_dispo_first,
2647 NULL, 0);
2648 const char *const new_params = concat (params, "\n", NULL);
2649
2650 replace_substring (&hf->value, semicolon_loc, new_params);
2651 free((void *)new_params); /* Cast away const. Sigh. */
2652 free((void *)params);
2653 } else {
2654 inform("did not find semicolon in %s:%s\n",
2655 hf->name, hf->value);
2656 }
2657 }
2658 }
2659
2660 return OK;
2661 }
2662
2663
2664 /*
2665 * Output content in input file to output file.
2666 */
2667 static int
2668 write_content (CT ct, const char *input_filename, char *outfile, FILE *outfp,
2669 int modify_inplace, int message_mods) {
2670 int status = OK;
2671
2672 if (modify_inplace) {
2673 if (message_mods > 0) {
2674 if ((status = output_message_fp (ct, outfp, outfile)) == OK) {
2675 char *infile = input_filename
2676 ? mh_xstrdup (input_filename)
2677 : mh_xstrdup (ct->c_file ? ct->c_file : "-");
2678
2679 if (remove_file (infile) == OK) {
2680 if (rename (outfile, infile)) {
2681 /* Rename didn't work, possibly because of an
2682 attempt to rename across filesystems. Try
2683 brute force copy. */
2684 int old = open (outfile, O_RDONLY);
2685 int new =
2686 open (infile, O_WRONLY | O_CREAT, m_gmprot ());
2687 int i = -1;
2688
2689 if (old != -1 && new != -1) {
2690 char buffer[NMH_BUFSIZ];
2691
2692 while ((i = read (old, buffer, sizeof buffer)) >
2693 0) {
2694 if (write (new, buffer, i) != i) {
2695 i = -1;
2696 break;
2697 }
2698 }
2699 }
2700 if (new != -1) { close (new); }
2701 if (old != -1) { close (old); }
2702 (void) m_unlink (outfile);
2703
2704 if (i < 0) {
2705 /* The -file argument processing used path() to
2706 expand filename to absolute path. */
2707 int file = ct->c_file && ct->c_file[0] == '/';
2708
2709 inform("unable to rename %s %s to %s, continuing...",
2710 file ? "file" : "message", outfile,
2711 infile);
2712 status = NOTOK;
2713 }
2714 }
2715 } else {
2716 inform("unable to remove input file %s, "
2717 "not modifying it, continuing...", infile);
2718 (void) m_unlink (outfile);
2719 status = NOTOK;
2720 }
2721
2722 free (infile);
2723 } else {
2724 status = NOTOK;
2725 }
2726 } else {
2727 /* No modifications and didn't need the tmp outfile. */
2728 (void) m_unlink (outfile);
2729 }
2730 } else {
2731 /* Output is going to some file. Produce it whether or not
2732 there were modifications. */
2733 status = output_message_fp (ct, outfp, outfile);
2734 }
2735
2736 flush_errors ();
2737 return status;
2738 }
2739
2740
2741 /*
2742 * parse_mime() does not set lf_line_endings in struct text, so use this
2743 * function to do it. It touches the parts the decodetypes identifies.
2744 */
2745 static void
2746 set_text_ctparams(CT ct, char *decodetypes, int lf_line_endings) {
2747 switch (ct->c_type) {
2748 case CT_MULTIPART: {
2749 struct multipart *m = (struct multipart *) ct->c_ctparams;
2750 struct part *part;
2751
2752 for (part = m->mp_parts; part; part = part->mp_next) {
2753 set_text_ctparams(part->mp_part, decodetypes, lf_line_endings);
2754 }
2755 break;
2756 }
2757
2758 case CT_MESSAGE:
2759 if (ct->c_subtype == MESSAGE_EXTERNAL) {
2760 struct exbody *e = (struct exbody *) ct->c_ctparams;
2761
2762 set_text_ctparams(e->eb_content, decodetypes, lf_line_endings);
2763 }
2764 break;
2765
2766 default:
2767 if (should_decode(decodetypes, ct->c_ctinfo.ci_type, ct->c_ctinfo.ci_subtype)) {
2768 if (ct->c_ctparams == NULL) {
2769 ct->c_ctparams = mh_xcalloc(1, sizeof (struct text));
2770 }
2771 ((struct text *) ct->c_ctparams)->lf_line_endings = lf_line_endings;
2772 }
2773 }
2774 }
2775
2776
2777 /*
2778 * If "rmmproc" is defined, call that to remove the file. Otherwise,
2779 * use the standard MH backup file.
2780 */
2781 static int
2782 remove_file (const char *file) {
2783 if (rmmproc) {
2784 char *rmm_command = concat (rmmproc, " ", file, NULL);
2785 int status = system (rmm_command);
2786
2787 free (rmm_command);
2788 return WIFEXITED (status) ? WEXITSTATUS (status) : NOTOK;
2789 }
2790 /* This is OK for a non-message file, it still uses the
2791 BACKUP_PREFIX form. The backup file will be in the same
2792 directory as file. */
2793 return rename (file, m_backup (file));
2794 }
2795
2796
2797 /*
2798 * Output formatted message to user.
2799 */
2800 static void
2801 report (char *what, char *partno, char *filename, char *message, ...) {
2802 va_list args;
2803 char *fmt;
2804
2805 if (verbosw) {
2806 va_start (args, message);
2807 fmt = concat (filename, partno ? " part " : ", ",
2808 FENDNULL(partno), partno ? ", " : "", message, NULL);
2809
2810 advertise (what, NULL, fmt, args);
2811
2812 free (fmt);
2813 va_end (args);
2814 }
2815 }
2816
2817
2818 static void
2819 pipeser (int i)
2820 {
2821 if (i == SIGQUIT) {
2822 fflush (stdout);
2823 fprintf (stderr, "\n");
2824 fflush (stderr);
2825 }
2826
2827 done (1);
2828 /* NOTREACHED */
2829 }