]> diplodocus.org Git - nmh/blob - uip/mhfixmsg.c
Added cast of MB_CUR_MAX to size_t to silence compiler warning on
[nmh] / uip / mhfixmsg.c
1 /*
2 * mhfixmsg.c -- rewrite a message with various tranformations
3 *
4 * This code is Copyright (c) 2002 and 2013, by the authors of nmh.
5 * See the COPYRIGHT file in the root directory of the nmh
6 * distribution for complete copyright information.
7 */
8
9 #include <h/mh.h>
10 #include <h/mime.h>
11 #include <h/mhparse.h>
12 #include <h/utils.h>
13 #include <h/signals.h>
14 #include <fcntl.h>
15
16 #define MHFIXMSG_SWITCHES \
17 X("decodetext 8bit|7bit", 0, DECODETEXTSW) \
18 X("nodecodetext", 0, NDECODETEXTSW) \
19 X("textcharset", 0, TEXTCHARSETSW) \
20 X("notextcharset", 0, NTEXTCHARSETSW) \
21 X("reformat", 0, REFORMATSW) \
22 X("noreformat", 0, NREFORMATSW) \
23 X("replacetextplain", 0, REPLACETEXTPLAINSW) \
24 X("noreplacetextplain", 0, NREPLACETEXTPLAINSW) \
25 X("fixboundary", 0, FIXBOUNDARYSW) \
26 X("nofixboundary", 0, NFIXBOUNDARYSW) \
27 X("fixcte", 0, FIXCTESW) \
28 X("nofixcte", 0, NFIXCTESW) \
29 X("file file", 0, FILESW) \
30 X("outfile file", 0, OUTFILESW) \
31 X("rmmproc program", 0, RPROCSW) \
32 X("normmproc", 0, NRPRCSW) \
33 X("verbose", 0, VERBSW) \
34 X("noverbose", 0, NVERBSW) \
35 X("version", 0, VERSIONSW) \
36 X("help", 0, HELPSW) \
37
38 #define X(sw, minchars, id) id,
39 DEFINE_SWITCH_ENUM(MHFIXMSG);
40 #undef X
41
42 #define X(sw, minchars, id) { sw, minchars, id },
43 DEFINE_SWITCH_ARRAY(MHFIXMSG, switches);
44 #undef X
45
46
47 int verbosw;
48 int debugsw; /* Needed by mhparse.c. */
49
50 #define quitser pipeser
51
52 /* mhparse.c */
53 extern int skip_mp_cte_check; /* flag to InitMultiPart */
54 extern int suppress_bogus_mp_content_warning; /* flag to InitMultiPart */
55 extern int bogus_mp_content; /* flag from InitMultiPart */
56 void reverse_parts (CT);
57
58 /* mhoutsbr.c */
59 int output_message (CT, char *);
60
61 /* mhmisc.c */
62 void flush_errors (void);
63
64 /* mhfree.c */
65 extern CT *cts;
66 void freects_done (int) NORETURN;
67
68 /*
69 * static prototypes
70 */
71 typedef struct fix_transformations {
72 int fixboundary;
73 int fixcte;
74 int reformat;
75 int replacetextplain;
76 int decodetext;
77 char *textcharset;
78 } fix_transformations;
79
80 int mhfixmsgsbr (CT *, const fix_transformations *, char *);
81 static void reverse_alternative_parts (CT);
82 static int fix_boundary (CT *, int *);
83 static int get_multipart_boundary (CT, char **);
84 static int replace_boundary (CT, char *, char *);
85 static int fix_multipart_cte (CT, int *);
86 static int set_ce (CT, int);
87 static int ensure_text_plain (CT *, CT, int *, int);
88 static CT build_text_plain_part (CT);
89 static CT divide_part (CT);
90 static void copy_ctinfo (CI, CI);
91 static int decode_part (CT);
92 static int reformat_part (CT, char *, char *, char *, int);
93 static int charset_encoding (CT);
94 static CT build_multipart_alt (CT, CT, int, int);
95 static int boundary_in_content (FILE **, char *, const char *);
96 static void transfer_noncontent_headers (CT, CT);
97 static int set_ct_type (CT, int type, int subtype, int encoding);
98 static int decode_text_parts (CT, int, int *);
99 static int content_encoding (CT, const char **);
100 static int strip_crs (CT, int *);
101 static int convert_charsets (CT, char *, int *);
102 static int write_content (CT, char *, char *, int, int);
103 static int remove_file (char *);
104 static void report (char *, char *, char *, char *, ...);
105 static void pipeser (int);
106
107
108 int
109 main (int argc, char **argv) {
110 int msgnum;
111 char *cp, *file = NULL, *folder = NULL;
112 char *maildir, buf[100], *outfile = NULL;
113 char **argp, **arguments;
114 struct msgs_array msgs = { 0, 0, NULL };
115 struct msgs *mp = NULL;
116 CT *ctp;
117 FILE *fp;
118 int using_stdin = 0;
119 int status = OK;
120 fix_transformations fx;
121 fx.reformat = fx.fixcte = fx.fixboundary = 1;
122 fx.replacetextplain = 0;
123 fx.decodetext = CE_8BIT;
124 fx.textcharset = NULL;
125
126 if (nmh_init(argv[0], 1)) { return 1; }
127
128 done = freects_done;
129
130 arguments = getarguments (invo_name, argc, argv, 1);
131 argp = arguments;
132
133 /*
134 * Parse arguments
135 */
136 while ((cp = *argp++)) {
137 if (*cp == '-') {
138 switch (smatch (++cp, switches)) {
139 case AMBIGSW:
140 ambigsw (cp, switches);
141 done (1);
142 case UNKWNSW:
143 adios (NULL, "-%s unknown", cp);
144
145 case HELPSW:
146 snprintf (buf, sizeof buf, "%s [+folder] [msgs] [switches]",
147 invo_name);
148 print_help (buf, switches, 1);
149 done (0);
150 case VERSIONSW:
151 print_version(invo_name);
152 done (0);
153
154 case DECODETEXTSW:
155 if (! (cp = *argp++) || *cp == '-')
156 adios (NULL, "missing argument to %s", argp[-2]);
157 if (! strcasecmp (cp, "8bit")) {
158 fx.decodetext = CE_8BIT;
159 } else if (! strcasecmp (cp, "7bit")) {
160 fx.decodetext = CE_7BIT;
161 } else {
162 adios (NULL, "invalid argument to %s", argp[-2]);
163 }
164 continue;
165 case NDECODETEXTSW:
166 fx.decodetext = 0;
167 continue;
168 case TEXTCHARSETSW:
169 if (! (cp = *argp++) || (*cp == '-' && cp[1]))
170 adios (NULL, "missing argument to %s", argp[-2]);
171 fx.textcharset = cp;
172 continue;
173 case NTEXTCHARSETSW:
174 fx.textcharset = 0;
175 continue;
176 case FIXBOUNDARYSW:
177 fx.fixboundary = 1;
178 continue;
179 case NFIXBOUNDARYSW:
180 fx.fixboundary = 0;
181 continue;
182 case FIXCTESW:
183 fx.fixcte = 1;
184 continue;
185 case NFIXCTESW:
186 fx.fixcte = 0;
187 continue;
188 case REFORMATSW:
189 fx.reformat = 1;
190 continue;
191 case NREFORMATSW:
192 fx.reformat = 0;
193 continue;
194 case REPLACETEXTPLAINSW:
195 fx.replacetextplain = 1;
196 continue;
197 case NREPLACETEXTPLAINSW:
198 fx.replacetextplain = 0;
199 continue;
200 case FILESW:
201 if (! (cp = *argp++) || (*cp == '-' && cp[1]))
202 adios (NULL, "missing argument to %s", argp[-2]);
203 file = *cp == '-' ? add (cp, NULL) : path (cp, TFILE);
204 continue;
205 case OUTFILESW:
206 if (! (cp = *argp++) || (*cp == '-' && cp[1]))
207 adios (NULL, "missing argument to %s", argp[-2]);
208 outfile = *cp == '-' ? add (cp, NULL) : path (cp, TFILE);
209 continue;
210 case RPROCSW:
211 if (!(rmmproc = *argp++) || *rmmproc == '-')
212 adios (NULL, "missing argument to %s", argp[-2]);
213 continue;
214 case NRPRCSW:
215 rmmproc = NULL;
216 continue;
217 case VERBSW:
218 verbosw = 1;
219 continue;
220 case NVERBSW:
221 verbosw = 0;
222 continue;
223 }
224 }
225 if (*cp == '+' || *cp == '@') {
226 if (folder)
227 adios (NULL, "only one folder at a time!");
228 else
229 folder = pluspath (cp);
230 } else {
231 if (*cp == '/') {
232 /* Interpret a full path as a filename, not a message. */
233 file = add (cp, NULL);
234 } else {
235 app_msgarg (&msgs, cp);
236 }
237 }
238 }
239
240 SIGNAL (SIGQUIT, quitser);
241 SIGNAL (SIGPIPE, pipeser);
242
243 /*
244 * Read the standard profile setup
245 */
246 if ((fp = fopen (cp = etcpath ("mhn.defaults"), "r"))) {
247 readconfig ((struct node **) 0, fp, cp, 0);
248 fclose (fp);
249 }
250
251 suppress_bogus_mp_content_warning = skip_mp_cte_check = 1;
252
253 if (! context_find ("path"))
254 free (path ("./", TFOLDER));
255
256 if (file && msgs.size)
257 adios (NULL, "cannot specify msg and file at same time!");
258
259 /*
260 * check if message is coming from file
261 */
262 if (file) {
263 /* If file is stdin, create a tmp file name before parse_mime()
264 has a chance, because it might put in on a different
265 filesystem than the output file. Instead, put it in the
266 user's preferred tmp directory. */
267 CT ct;
268
269 if (! strcmp ("-", file)) {
270 int fd;
271 char *cp;
272
273 using_stdin = 1;
274
275 if ((cp = m_mktemp2 (NULL, invo_name, &fd, NULL)) == NULL) {
276 adios (NULL, "unable to create temporary file in %s",
277 get_temp_dir());
278 } else {
279 free (file);
280 file = add (cp, NULL);
281 cpydata (STDIN_FILENO, fd, "-", file);
282 }
283
284 if (close (fd)) {
285 (void) m_unlink (file);
286 adios (NULL, "failed to write temporary file");
287 }
288 }
289
290 if (! (cts = (CT *) calloc ((size_t) 2, sizeof *cts))) {
291 adios (NULL, "out of memory");
292 }
293 ctp = cts;
294
295 if ((ct = parse_mime (file))) { *ctp++ = ct; }
296 } else {
297 /*
298 * message(s) are coming from a folder
299 */
300 CT ct;
301
302 if (! msgs.size)
303 app_msgarg(&msgs, "cur");
304 if (! folder)
305 folder = getfolder (1);
306 maildir = m_maildir (folder);
307
308 if (chdir (maildir) == NOTOK)
309 adios (maildir, "unable to change directory to");
310
311 /* read folder and create message structure */
312 if (! (mp = folder_read (folder, 1)))
313 adios (NULL, "unable to read folder %s", folder);
314
315 /* check for empty folder */
316 if (mp->nummsg == 0)
317 adios (NULL, "no messages in %s", folder);
318
319 /* parse all the message ranges/sequences and set SELECTED */
320 for (msgnum = 0; msgnum < msgs.size; msgnum++)
321 if (! m_convert (mp, msgs.msgs[msgnum]))
322 done (1);
323 seq_setprev (mp); /* set the previous-sequence */
324
325 if (! (cts = (CT *) calloc ((size_t) (mp->numsel + 1), sizeof *cts))) {
326 adios (NULL, "out of memory");
327 }
328 ctp = cts;
329
330 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
331 if (is_selected(mp, msgnum)) {
332 char *msgnam;
333
334 msgnam = m_name (msgnum);
335 if ((ct = parse_mime (msgnam))) { *ctp++ = ct; }
336 }
337 }
338
339 seq_setcur (mp, mp->hghsel); /* update current message */
340 seq_save (mp); /* synchronize sequences */
341 context_replace (pfolder, folder);/* update current folder */
342 context_save (); /* save the context file */
343 }
344
345 if (*cts) {
346 for (ctp = cts; *ctp; ++ctp) {
347 status += mhfixmsgsbr (ctp, &fx, outfile);
348
349 if (using_stdin) {
350 (void) m_unlink (file);
351
352 if (! outfile) {
353 /* Just calling m_backup() unlinks the backup file. */
354 (void) m_backup (file);
355 }
356 }
357 }
358 } else {
359 status = 1;
360 }
361
362 free (outfile);
363 free (file);
364
365 /* done is freects_done, which will clean up all of cts. */
366 done (status);
367 return NOTOK;
368 }
369
370
371 int
372 mhfixmsgsbr (CT *ctp, const fix_transformations *fx, char *outfile) {
373 /* Store input filename in case one of the transformations, i.e.,
374 fix_boundary(), rewrites to a tmp file. */
375 char *input_filename = add ((*ctp)->c_file, NULL);
376 int modify_inplace = 0;
377 int message_mods = 0;
378 int status = OK;
379
380 if (outfile == NULL) {
381 modify_inplace = 1;
382
383 if ((*ctp)->c_file) {
384 char *tempfile;
385 if ((tempfile = m_mktemp2 (NULL, invo_name, NULL, NULL)) == NULL) {
386 adios (NULL, "unable to create temporary file in %s",
387 get_temp_dir());
388 }
389 outfile = add (tempfile, NULL);
390 } else {
391 adios (NULL, "missing both input and output filenames\n");
392 }
393 }
394
395 reverse_alternative_parts (*ctp);
396 if (status == OK && fx->fixboundary) {
397 status = fix_boundary (ctp, &message_mods);
398 }
399 if (status == OK && fx->fixcte) {
400 status = fix_multipart_cte (*ctp, &message_mods);
401 }
402 if (status == OK && fx->reformat) {
403 status =
404 ensure_text_plain (ctp, NULL, &message_mods, fx->replacetextplain);
405 }
406 if (status == OK && fx->decodetext) {
407 status = decode_text_parts (*ctp, fx->decodetext, &message_mods);
408 }
409 if (status == OK && fx->textcharset != NULL) {
410 status = convert_charsets (*ctp, fx->textcharset, &message_mods);
411 }
412
413 if (! (*ctp)->c_umask) {
414 /* Set the umask for the contents file. This currently
415 isn't used but just in case it is in the future. */
416 struct stat st;
417
418 if (stat ((*ctp)->c_file, &st) != NOTOK) {
419 (*ctp)->c_umask = ~(st.st_mode & 0777);
420 } else {
421 (*ctp)->c_umask = ~m_gmprot();
422 }
423 }
424
425 /*
426 * Write the content to a file
427 */
428 if (status == OK) {
429 status = write_content (*ctp, input_filename, outfile, modify_inplace,
430 message_mods);
431 } else if (! modify_inplace) {
432 /* Something went wrong. Output might be expected, such
433 as if this were run as a filter. Just copy the input
434 to the output. */
435 int in = open (input_filename, O_RDONLY);
436 int out = strcmp (outfile, "-")
437 ? open (outfile, O_WRONLY | O_CREAT, m_gmprot ())
438 : STDOUT_FILENO;
439
440 if (in != -1 && out != -1) {
441 cpydata (in, out, input_filename, outfile);
442 } else {
443 status = NOTOK;
444 }
445
446 close (out);
447 close (in);
448 }
449
450 if (modify_inplace) {
451 if (status != OK) { (void) m_unlink (outfile); }
452 free (outfile);
453 outfile = NULL;
454 }
455
456 free (input_filename);
457
458 return status;
459 }
460
461
462 /* parse_mime() arranges alternates in reverse (priority) order, so
463 reverse them back. This will put a text/plain part at the front of
464 a multipart/alternative part, for example, where it belongs. */
465 static void
466 reverse_alternative_parts (CT ct) {
467 if (ct->c_type == CT_MULTIPART) {
468 struct multipart *m = (struct multipart *) ct->c_ctparams;
469 struct part *part;
470
471 if (ct->c_subtype == MULTI_ALTERNATE) {
472 reverse_parts (ct);
473 }
474
475 /* And call recursively on each part of a multipart. */
476 for (part = m->mp_parts; part; part = part->mp_next) {
477 reverse_alternative_parts (part->mp_part);
478 }
479 }
480 }
481
482
483 static int
484 fix_boundary (CT *ct, int *message_mods) {
485 struct multipart *mp;
486 int status = OK;
487
488 if (bogus_mp_content) {
489 mp = (struct multipart *) (*ct)->c_ctparams;
490
491 /*
492 * 1) Get boundary at end of part.
493 * 2) Get boundary at beginning of part and compare to the end-of-part
494 * boundary.
495 * 3) Write out contents of ct to tmp file, replacing boundary in
496 * header with boundary from part. Set c_unlink to 1.
497 * 4) Free ct.
498 * 5) Call parse_mime() on the tmp file, replacing ct.
499 */
500
501 if (mp && mp->mp_start) {
502 char *part_boundary;
503
504 if (get_multipart_boundary (*ct, &part_boundary) == OK) {
505 char *fixed;
506
507 if ((fixed = m_mktemp2 (NULL, invo_name, NULL, &(*ct)->c_fp))) {
508 if (replace_boundary (*ct, fixed, part_boundary) == OK) {
509 char *filename = add ((*ct)->c_file, NULL);
510
511 free_content (*ct);
512 if ((*ct = parse_mime (fixed))) {
513 (*ct)->c_unlink = 1;
514
515 ++*message_mods;
516 if (verbosw) {
517 report (NULL, NULL, filename,
518 "fix multipart boundary");
519 }
520 }
521 free (filename);
522 } else {
523 advise (NULL, "unable to replace broken boundary");
524 status = NOTOK;
525 }
526 } else {
527 advise (NULL, "unable to create temporary file in %s",
528 get_temp_dir());
529 status = NOTOK;
530 }
531
532 free (part_boundary);
533 }
534 }
535 }
536
537 return status;
538 }
539
540
541 static int
542 get_multipart_boundary (CT ct, char **part_boundary) {
543 char buffer[BUFSIZ];
544 char *end_boundary = NULL;
545 off_t begin = (off_t) ct->c_end > (off_t) (ct->c_begin + sizeof buffer)
546 ? (off_t) (ct->c_end - sizeof buffer)
547 : (off_t) ct->c_begin;
548 size_t bytes_read;
549 int status = OK;
550
551 /* This will fail if the boundary spans fread() calls. BUFSIZ should
552 be big enough, even if it's just 1024, to make that unlikely. */
553
554 /* free_content() will close ct->c_fp. */
555 if (! ct->c_fp && (ct->c_fp = fopen (ct->c_file, "r")) == NULL) {
556 advise (ct->c_file, "unable to open for reading");
557 return NOTOK;
558 }
559
560 /* Get boundary at end of multipart. */
561 while (begin >= (off_t) ct->c_begin) {
562 fseeko (ct->c_fp, begin, SEEK_SET);
563 while ((bytes_read = fread (buffer, 1, sizeof buffer, ct->c_fp)) > 0) {
564 char *cp = rfind_str (buffer, bytes_read, "--");
565
566 if (cp) {
567 char *end;
568
569 /* Trim off trailing "--" and anything beyond. */
570 *cp-- = '\0';
571 if ((end = rfind_str (buffer, cp - buffer, "\n"))) {
572 if (strlen (end) > 3 && *end++ == '\n' &&
573 *end++ == '-' && *end++ == '-') {
574 end_boundary = add (end, NULL);
575 break;
576 }
577 }
578 }
579 }
580
581 if (! end_boundary && begin > (off_t) (ct->c_begin + sizeof buffer)) {
582 begin -= sizeof buffer;
583 } else {
584 break;
585 }
586 }
587
588 /* Get boundary at beginning of multipart. */
589 if (end_boundary) {
590 fseeko (ct->c_fp, ct->c_begin, SEEK_SET);
591 while ((bytes_read = fread (buffer, 1, sizeof buffer, ct->c_fp)) > 0) {
592 if (bytes_read >= strlen (end_boundary)) {
593 char *cp = find_str (buffer, bytes_read, end_boundary);
594
595 if (cp && cp - buffer >= 2 && *--cp == '-' &&
596 *--cp == '-' && (cp > buffer && *--cp == '\n')) {
597 status = OK;
598 break;
599 }
600 } else {
601 /* The start and end boundaries didn't match, or the
602 start boundary doesn't begin with "\n--" (or "--"
603 if at the beginning of buffer). Keep trying. */
604 status = NOTOK;
605 }
606 }
607 } else {
608 status = NOTOK;
609 }
610
611 if (status == OK) {
612 *part_boundary = end_boundary;
613 } else {
614 *part_boundary = NULL;
615 free (end_boundary);
616 }
617
618 return status;
619 }
620
621
622 /* Open and copy ct->c_file to file, replacing the multipart boundary. */
623 static int
624 replace_boundary (CT ct, char *file, char *boundary) {
625 FILE *fpin, *fpout;
626 int compnum, state;
627 char buf[BUFSIZ], name[NAMESZ];
628 char *np, *vp;
629 m_getfld_state_t gstate = 0;
630 int status = OK;
631
632 if (ct->c_file == NULL) {
633 advise (NULL, "missing input filename");
634 return NOTOK;
635 }
636
637 if ((fpin = fopen (ct->c_file, "r")) == NULL) {
638 advise (ct->c_file, "unable to open for reading");
639 return NOTOK;
640 }
641
642 if ((fpout = fopen (file, "w")) == NULL) {
643 fclose (fpin);
644 advise (file, "unable to open for writing");
645 return NOTOK;
646 }
647
648 for (compnum = 1;;) {
649 int bufsz = (int) sizeof buf;
650
651 switch (state = m_getfld (&gstate, name, buf, &bufsz, fpin)) {
652 case FLD:
653 case FLDPLUS:
654 compnum++;
655
656 /* get copies of the buffers */
657 np = add (name, NULL);
658 vp = add (buf, NULL);
659
660 /* if necessary, get rest of field */
661 while (state == FLDPLUS) {
662 bufsz = sizeof buf;
663 state = m_getfld (&gstate, name, buf, &bufsz, fpin);
664 vp = add (buf, vp); /* add to previous value */
665 }
666
667 if (strcasecmp (TYPE_FIELD, np)) {
668 fprintf (fpout, "%s:%s", np, vp);
669 } else {
670 char *new_ctline, *new_params;
671
672 replace_param(&ct->c_ctinfo.ci_first_pm,
673 &ct->c_ctinfo.ci_last_pm, "boundary",
674 boundary, 0);
675
676 new_ctline = concat(" ", ct->c_ctinfo.ci_type, "/",
677 ct->c_ctinfo.ci_subtype, NULL);
678 new_params = output_params(strlen(TYPE_FIELD) +
679 strlen(new_ctline) + 1,
680 ct->c_ctinfo.ci_first_pm, NULL, 0);
681 fprintf (fpout, "%s:%s%s\n", np, new_ctline,
682 new_params ? new_params : "");
683 free(new_ctline);
684 if (new_params)
685 free(new_params);
686 }
687
688 free (vp);
689 free (np);
690
691 continue;
692
693 case BODY:
694 fputs ("\n", fpout);
695 /* buf will have a terminating NULL, skip it. */
696 fwrite (buf, 1, bufsz-1, fpout);
697 continue;
698
699 case FILEEOF:
700 break;
701
702 case LENERR:
703 case FMTERR:
704 advise (NULL, "message format error in component #%d", compnum);
705 status = NOTOK;
706 break;
707
708 default:
709 advise (NULL, "getfld() returned %d", state);
710 status = NOTOK;
711 break;
712 }
713
714 break;
715 }
716
717 m_getfld_state_destroy (&gstate);
718 fclose (fpout);
719 fclose (fpin);
720
721 return status;
722 }
723
724
725 static int
726 fix_multipart_cte (CT ct, int *message_mods) {
727 int status = OK;
728
729 if (ct->c_type == CT_MULTIPART) {
730 struct multipart *m;
731 struct part *part;
732
733 if (ct->c_encoding != CE_7BIT && ct->c_encoding != CE_8BIT &&
734 ct->c_encoding != CE_BINARY) {
735 HF hf;
736
737 for (hf = ct->c_first_hf; hf; hf = hf->next) {
738 char *name = hf->name;
739 for (; *name && isspace ((unsigned char) *name); ++name) {
740 continue;
741 }
742
743 if (! strncasecmp (name, ENCODING_FIELD,
744 strlen (ENCODING_FIELD))) {
745 char *prefix = "Nmh-REPLACED-INVALID-";
746 HF h = mh_xmalloc (sizeof *h);
747
748 h->name = add (hf->name, NULL);
749 h->hf_encoding = hf->hf_encoding;
750 h->next = hf->next;
751 hf->next = h;
752
753 /* Retain old header but prefix its name. */
754 free (hf->name);
755 hf->name = concat (prefix, h->name, NULL);
756
757 ++*message_mods;
758 if (verbosw) {
759 char *encoding = cpytrim (hf->value);
760 report (NULL, ct->c_partno, ct->c_file,
761 "replace Content-Transfer-Encoding of %s "
762 "with 8 bit", encoding);
763 free (encoding);
764 }
765
766 h->value = add (" 8bit\n", NULL);
767
768 /* Don't need to warn for multiple C-T-E header
769 fields, parse_mime() already does that. But
770 if there are any, fix them all as necessary. */
771 hf = h;
772 }
773 }
774
775 set_ce (ct, CE_8BIT);
776 }
777
778 m = (struct multipart *) ct->c_ctparams;
779 for (part = m->mp_parts; part; part = part->mp_next) {
780 if (fix_multipart_cte (part->mp_part, message_mods) != OK) {
781 status = NOTOK;
782 break;
783 }
784 }
785 }
786
787 return status;
788 }
789
790
791 static int
792 set_ce (CT ct, int encoding) {
793 const char *ce = ce_str (encoding);
794 const struct str2init *ctinit = get_ce_method (ce);
795
796 if (ctinit) {
797 char *cte = concat (" ", ce, "\n", NULL);
798 int found_cte = 0;
799 HF hf;
800 /* Decoded contents might be in ct->c_cefile.ce_file, if the
801 caller is decode_text_parts (). Save because we'll
802 overwrite below. */
803 struct cefile decoded_content_info = ct->c_cefile;
804
805 ct->c_encoding = encoding;
806
807 ct->c_ctinitfnx = ctinit->si_init;
808 /* This will assign ct->c_cefile with an all-0 struct, which
809 is what we want. */
810 (*ctinit->si_init) (ct);
811 /* After returning, the caller should set
812 ct->c_cefile.ce_file to the name of the file containing
813 the contents. */
814
815 /* Restore the cefile. */
816 ct->c_cefile = decoded_content_info;
817
818 /* Update/add Content-Transfer-Encoding header field. */
819 for (hf = ct->c_first_hf; hf; hf = hf->next) {
820 if (! strcasecmp (ENCODING_FIELD, hf->name)) {
821 found_cte = 1;
822 free (hf->value);
823 hf->value = cte;
824 }
825 }
826 if (! found_cte) {
827 add_header (ct, add (ENCODING_FIELD, NULL), cte);
828 }
829
830 /* Update c_celine. It's used only by mhlist -debug. */
831 free (ct->c_celine);
832 ct->c_celine = add (cte, NULL);
833
834 return OK;
835 } else {
836 return NOTOK;
837 }
838 }
839
840
841 /* Make sure each text part has a corresponding text/plain part. */
842 static int
843 ensure_text_plain (CT *ct, CT parent, int *message_mods, int replacetextplain) {
844 int status = OK;
845
846 switch ((*ct)->c_type) {
847 case CT_TEXT: {
848 int has_text_plain = 0;
849
850 /* Nothing to do for text/plain. */
851 if ((*ct)->c_subtype == TEXT_PLAIN) { return OK; }
852
853 if (parent && parent->c_type == CT_MULTIPART &&
854 parent->c_subtype == MULTI_ALTERNATE) {
855 struct multipart *mp = (struct multipart *) parent->c_ctparams;
856 struct part *part, *prev;
857 int new_subpart_number = 1;
858
859 /* See if there is a sibling text/plain. */
860 for (prev = part = mp->mp_parts; part; part = part->mp_next) {
861 ++new_subpart_number;
862 if (part->mp_part->c_type == CT_TEXT &&
863 part->mp_part->c_subtype == TEXT_PLAIN) {
864 if (replacetextplain) {
865 struct part *old_part;
866 if (part == mp->mp_parts) {
867 old_part = mp->mp_parts;
868 mp->mp_parts = part->mp_next;
869 } else {
870 old_part = prev->mp_next;
871 prev->mp_next = part->mp_next;
872 }
873 if (verbosw) {
874 report (NULL, parent->c_partno, parent->c_file,
875 "remove text/plain part %s",
876 old_part->mp_part->c_partno);
877 }
878 free_content (old_part->mp_part);
879 free (old_part);
880 } else {
881 has_text_plain = 1;
882 }
883 break;
884 }
885 prev = part;
886 }
887
888 if (! has_text_plain) {
889 /* Parent is a multipart/alternative. Insert a new
890 text/plain subpart. */
891 struct part *new_part = mh_xmalloc (sizeof *new_part);
892
893 if ((new_part->mp_part = build_text_plain_part (*ct))) {
894 char buffer[16];
895 snprintf (buffer, sizeof buffer, "%d", new_subpart_number);
896
897 new_part->mp_next = mp->mp_parts;
898 mp->mp_parts = new_part;
899 new_part->mp_part->c_partno =
900 concat (parent->c_partno ? parent->c_partno : "1", ".",
901 buffer, NULL);
902
903 ++*message_mods;
904 if (verbosw) {
905 report (NULL, parent->c_partno, parent->c_file,
906 "insert text/plain part");
907 }
908 } else {
909 free_content (new_part->mp_part);
910 free (new_part);
911 status = NOTOK;
912 }
913 }
914 } else {
915 /* Slip new text/plain part into a new multipart/alternative. */
916 CT tp_part = build_text_plain_part (*ct);
917
918 if (tp_part) {
919 CT mp_alt = build_multipart_alt (*ct, tp_part, CT_MULTIPART,
920 MULTI_ALTERNATE);
921 if (mp_alt) {
922 struct multipart *mp =
923 (struct multipart *) mp_alt->c_ctparams;
924
925 if (mp && mp->mp_parts) {
926 mp->mp_parts->mp_part = tp_part;
927 /* Make the new multipart/alternative the parent. */
928 *ct = mp_alt;
929
930 ++*message_mods;
931 if (verbosw) {
932 report (NULL, (*ct)->c_partno, (*ct)->c_file,
933 "insert text/plain part");
934 }
935 } else {
936 free_content (tp_part);
937 free_content (mp_alt);
938 status = NOTOK;
939 }
940 } else {
941 status = NOTOK;
942 }
943 } else {
944 status = NOTOK;
945 }
946 }
947 break;
948 }
949
950 case CT_MULTIPART: {
951 struct multipart *mp = (struct multipart *) (*ct)->c_ctparams;
952 struct part *part;
953
954 for (part = mp->mp_parts; status == OK && part; part = part->mp_next) {
955 if ((*ct)->c_type == CT_MULTIPART) {
956 status = ensure_text_plain (&part->mp_part, *ct, message_mods,
957 replacetextplain);
958 }
959 }
960 break;
961 }
962
963 case CT_MESSAGE:
964 if ((*ct)->c_subtype == MESSAGE_EXTERNAL) {
965 struct exbody *e;
966
967 e = (struct exbody *) (*ct)->c_ctparams;
968 status = ensure_text_plain (&e->eb_content, *ct, message_mods,
969 replacetextplain);
970 }
971 break;
972 }
973
974 return status;
975 }
976
977
978 static CT
979 build_text_plain_part (CT encoded_part) {
980 CT tp_part = divide_part (encoded_part);
981 char *tmp_plain_file = NULL;
982
983 if (decode_part (tp_part) == OK) {
984 /* Now, tp_part->c_cefile.ce_file is the name of the tmp file that
985 contains the decoded contents. And the decoding function, such
986 as openQuoted, will have set ...->ce_unlink to 1 so that it will
987 be unlinked by free_content (). */
988 char *tempfile;
989
990 if ((tempfile = m_mktemp2 (NULL, invo_name, NULL, NULL)) == NULL) {
991 advise (NULL, "unable to create temporary file in %s",
992 get_temp_dir());
993 }
994 tmp_plain_file = add (tempfile, NULL);
995 if (reformat_part (tp_part, tmp_plain_file,
996 tp_part->c_ctinfo.ci_type,
997 tp_part->c_ctinfo.ci_subtype,
998 tp_part->c_type) == OK) {
999 return tp_part;
1000 }
1001 }
1002
1003 free_content (tp_part);
1004 (void) m_unlink (tmp_plain_file);
1005 free (tmp_plain_file);
1006
1007 return NULL;
1008 }
1009
1010
1011 static CT
1012 divide_part (CT ct) {
1013 CT new_part;
1014
1015 if ((new_part = (CT) calloc (1, sizeof *new_part)) == NULL)
1016 adios (NULL, "out of memory");
1017
1018 /* Just copy over what is needed for decoding. c_vrsn and
1019 c_celine aren't necessary. */
1020 new_part->c_file = add (ct->c_file, NULL);
1021 new_part->c_begin = ct->c_begin;
1022 new_part->c_end = ct->c_end;
1023 copy_ctinfo (&new_part->c_ctinfo, &ct->c_ctinfo);
1024 new_part->c_type = ct->c_type;
1025 new_part->c_cefile = ct->c_cefile;
1026 new_part->c_encoding = ct->c_encoding;
1027 new_part->c_ctinitfnx = ct->c_ctinitfnx;
1028 new_part->c_ceopenfnx = ct->c_ceopenfnx;
1029 new_part->c_ceclosefnx = ct->c_ceclosefnx;
1030 new_part->c_cesizefnx = ct->c_cesizefnx;
1031
1032 /* c_ctline is used by reformat__part(), so it can preserve
1033 anything after the type/subtype. */
1034 new_part->c_ctline = add (ct->c_ctline, NULL);
1035
1036 return new_part;
1037 }
1038
1039
1040 static void
1041 copy_ctinfo (CI dest, CI src) {
1042 PM s_pm, d_pm;
1043
1044 dest->ci_type = src->ci_type ? add (src->ci_type, NULL) : NULL;
1045 dest->ci_subtype = src->ci_subtype ? add (src->ci_subtype, NULL) : NULL;
1046
1047 for (s_pm = src->ci_first_pm; s_pm; s_pm = s_pm->pm_next) {
1048 d_pm = add_param(&dest->ci_first_pm, &dest->ci_last_pm, s_pm->pm_name,
1049 s_pm->pm_value, 0);
1050 if (s_pm->pm_charset)
1051 d_pm->pm_charset = getcpy(s_pm->pm_charset);
1052 if (s_pm->pm_lang)
1053 d_pm->pm_lang = getcpy(s_pm->pm_lang);
1054 }
1055
1056 dest->ci_comment = src->ci_comment ? add (src->ci_comment, NULL) : NULL;
1057 dest->ci_magic = src->ci_magic ? add (src->ci_magic, NULL) : NULL;
1058 }
1059
1060
1061 static int
1062 decode_part (CT ct) {
1063 char *tmp_decoded;
1064 int status;
1065 char *tempfile;
1066
1067 if ((tempfile = m_mktemp2 (NULL, invo_name, NULL, NULL)) == NULL) {
1068 adios (NULL, "unable to create temporary file in %s", get_temp_dir());
1069 }
1070 tmp_decoded = add (tempfile, NULL);
1071 /* The following call will load ct->c_cefile.ce_file with the tmp
1072 filename of the decoded content. tmp_decoded will contain the
1073 encoded output, get rid of that. */
1074 status = output_message (ct, tmp_decoded);
1075 (void) m_unlink (tmp_decoded);
1076 free (tmp_decoded);
1077
1078 return status;
1079 }
1080
1081
1082 /* Some of the arguments aren't really needed now, but maybe will
1083 be in the future for other than text types. */
1084 static int
1085 reformat_part (CT ct, char *file, char *type, char *subtype, int c_type) {
1086 int output_subtype, output_encoding;
1087 char *cp, *cf;
1088 int status;
1089
1090 /* Hacky: this redirects the output from whatever command is used
1091 to show the part to a file. So, the user can't have any output
1092 redirection in that command.
1093 Could show_multi() in mhshowsbr.c avoid this? */
1094
1095 /* Check for invo_name-format-type/subtype. */
1096 if ((cf = context_find_by_type ("format", type, subtype)) == NULL) {
1097 if (verbosw) {
1098 advise (NULL, "Don't know how to convert %s, there is no "
1099 "%s-format-%s/%s profile entry",
1100 ct->c_file, invo_name, type, subtype);
1101 }
1102 return NOTOK;
1103 } else {
1104 if (strchr (cf, '>')) {
1105 advise (NULL, "'>' prohibited in \"%s\",\nplease fix your "
1106 "%s-format-%s/%s profile entry", cf, invo_name, type,
1107 subtype ? subtype : "");
1108
1109 return NOTOK;
1110 }
1111 }
1112
1113 cp = concat (cf, " >", file, NULL);
1114 status = show_content_aux (ct, 0, cp, NULL, NULL);
1115 free (cp);
1116
1117 /* Unlink decoded content tmp file and free its filename to avoid
1118 leaks. The file stream should already have been closed. */
1119 if (ct->c_cefile.ce_unlink) {
1120 (void) m_unlink (ct->c_cefile.ce_file);
1121 free (ct->c_cefile.ce_file);
1122 ct->c_cefile.ce_file = NULL;
1123 ct->c_cefile.ce_unlink = 0;
1124 }
1125
1126 if (c_type == CT_TEXT) {
1127 output_subtype = TEXT_PLAIN;
1128 } else {
1129 /* Set subtype to 0, which is always an UNKNOWN subtype. */
1130 output_subtype = 0;
1131 }
1132 output_encoding = charset_encoding (ct);
1133
1134 if (set_ct_type (ct, c_type, output_subtype, output_encoding) == OK) {
1135 ct->c_cefile.ce_file = file;
1136 ct->c_cefile.ce_unlink = 1;
1137 } else {
1138 ct->c_cefile.ce_unlink = 0;
1139 status = NOTOK;
1140 }
1141
1142 return status;
1143 }
1144
1145
1146 /* Identifies 7bit or 8bit content based on charset. */
1147 static int
1148 charset_encoding (CT ct) {
1149 int encoding =
1150 strcasecmp (content_charset (ct), "US-ASCII") ? CE_8BIT : CE_7BIT;
1151
1152 return encoding;
1153 }
1154
1155
1156 static CT
1157 build_multipart_alt (CT first_alt, CT new_part, int type, int subtype) {
1158 char *boundary_prefix = "----=_nmh-multipart";
1159 char *boundary = concat (boundary_prefix, first_alt->c_partno, NULL);
1160 char *boundary_indicator = "; boundary=";
1161 char *typename, *subtypename, *name;
1162 CT ct;
1163 struct part *p;
1164 struct multipart *m;
1165 const struct str2init *ctinit;
1166
1167 if ((ct = (CT) calloc (1, sizeof *ct)) == NULL)
1168 adios (NULL, "out of memory");
1169
1170 /* Set up the multipart/alternative part. These fields of *ct were
1171 initialized to 0 by calloc():
1172 c_fp, c_unlink, c_begin, c_end,
1173 c_vrsn, c_ctline, c_celine,
1174 c_id, c_descr, c_dispo, c_partno,
1175 c_ctinfo.ci_comment, c_ctinfo.ci_magic,
1176 c_cefile, c_encoding,
1177 c_digested, c_digest[16], c_ctexbody,
1178 c_ctinitfnx, c_ceopenfnx, c_ceclosefnx, c_cesizefnx,
1179 c_umask, c_rfc934,
1180 c_showproc, c_termproc, c_storeproc, c_storage, c_folder
1181 */
1182
1183 ct->c_file = add (first_alt->c_file, NULL);
1184 ct->c_type = type;
1185 ct->c_subtype = subtype;
1186
1187 ctinit = get_ct_init (ct->c_type);
1188
1189 typename = ct_type_str (type);
1190 subtypename = ct_subtype_str (type, subtype);
1191
1192 {
1193 int serial = 0;
1194 int found_boundary = 1;
1195
1196 while (found_boundary && serial < 1000000) {
1197 found_boundary = 0;
1198
1199 /* Ensure that the boundary doesn't appear in the decoded
1200 content. */
1201 if (new_part->c_cefile.ce_file) {
1202 if ((found_boundary =
1203 boundary_in_content (&new_part->c_cefile.ce_fp,
1204 new_part->c_cefile.ce_file,
1205 boundary)) == -1) {
1206 free (ct);
1207 return NULL;
1208 }
1209 }
1210
1211 /* Ensure that the boundary doesn't appear in the encoded
1212 content. */
1213 if (! found_boundary && new_part->c_file) {
1214 if ((found_boundary = boundary_in_content (&new_part->c_fp,
1215 new_part->c_file,
1216 boundary)) == -1) {
1217 free (ct);
1218 return NULL;
1219 }
1220 }
1221
1222 if (found_boundary) {
1223 /* Try a slightly different boundary. */
1224 char buffer2[16];
1225
1226 free (boundary);
1227 ++serial;
1228 snprintf (buffer2, sizeof buffer2, "%d", serial);
1229 boundary =
1230 concat (boundary_prefix,
1231 first_alt->c_partno ? first_alt->c_partno : "",
1232 "-", buffer2, NULL);
1233 }
1234 }
1235
1236 if (found_boundary) {
1237 advise (NULL, "giving up trying to find a unique boundary");
1238 free (ct);
1239 return NULL;
1240 }
1241 }
1242
1243 name = concat (" ", typename, "/", subtypename, boundary_indicator, "\"",
1244 boundary, "\"", NULL);
1245
1246 /* Load c_first_hf and c_last_hf. */
1247 transfer_noncontent_headers (first_alt, ct);
1248 add_header (ct, add (TYPE_FIELD, NULL), concat (name, "\n", NULL));
1249 free (name);
1250
1251 /* Load c_partno. */
1252 if (first_alt->c_partno) {
1253 ct->c_partno = add (first_alt->c_partno, NULL);
1254 free (first_alt->c_partno);
1255 first_alt->c_partno = concat (ct->c_partno, ".1", NULL);
1256 new_part->c_partno = concat (ct->c_partno, ".2", NULL);
1257 } else {
1258 first_alt->c_partno = add ("1", NULL);
1259 new_part->c_partno = add ("2", NULL);
1260 }
1261
1262 if (ctinit) {
1263 ct->c_ctinfo.ci_type = add (typename, NULL);
1264 ct->c_ctinfo.ci_subtype = add (subtypename, NULL);
1265 }
1266
1267 add_param(&ct->c_ctinfo.ci_first_pm, &ct->c_ctinfo.ci_last_pm,
1268 "boundary", boundary, 0);
1269
1270 p = (struct part *) mh_xmalloc (sizeof *p);
1271 p->mp_next = (struct part *) mh_xmalloc (sizeof *p->mp_next);
1272 p->mp_next->mp_next = NULL;
1273 p->mp_next->mp_part = first_alt;
1274
1275 if ((m = (struct multipart *) calloc (1, sizeof (struct multipart))) ==
1276 NULL)
1277 adios (NULL, "out of memory");
1278 m->mp_start = concat (boundary, "\n", NULL);
1279 m->mp_stop = concat (boundary, "--\n", NULL);
1280 m->mp_parts = p;
1281 ct->c_ctparams = (void *) m;
1282
1283 free (boundary);
1284
1285 return ct;
1286 }
1287
1288
1289 /* Check that the boundary does not appear in the content. */
1290 static int
1291 boundary_in_content (FILE **fp, char *file, const char *boundary) {
1292 char buffer[BUFSIZ];
1293 size_t bytes_read;
1294 int found_boundary = 0;
1295
1296 /* free_content() will close *fp if we fopen it here. */
1297 if (! *fp && (*fp = fopen (file, "r")) == NULL) {
1298 advise (file, "unable to open %s for reading", file);
1299 return NOTOK;
1300 }
1301
1302 fseeko (*fp, 0L, SEEK_SET);
1303 while ((bytes_read = fread (buffer, 1, sizeof buffer, *fp)) > 0) {
1304 if (find_str (buffer, bytes_read, boundary)) {
1305 found_boundary = 1;
1306 break;
1307 }
1308 }
1309
1310 return found_boundary;
1311 }
1312
1313
1314 /* Remove all non-Content headers. */
1315 static void
1316 transfer_noncontent_headers (CT old, CT new) {
1317 HF hp, hp_prev;
1318
1319 hp_prev = hp = old->c_first_hf;
1320 while (hp) {
1321 HF next = hp->next;
1322
1323 if (strncasecmp (XXX_FIELD_PRF, hp->name, strlen (XXX_FIELD_PRF))) {
1324 if (hp == old->c_last_hf) {
1325 if (hp == old->c_first_hf) {
1326 old->c_last_hf = old->c_first_hf = NULL;
1327 } else {
1328 hp_prev->next = NULL;
1329 old->c_last_hf = hp_prev;
1330 }
1331 } else {
1332 if (hp == old->c_first_hf) {
1333 old->c_first_hf = next;
1334 } else {
1335 hp_prev->next = next;
1336 }
1337 }
1338
1339 /* Put node hp in the new CT. */
1340 if (new->c_first_hf == NULL) {
1341 new->c_first_hf = hp;
1342 } else {
1343 new->c_last_hf->next = hp;
1344 }
1345 new->c_last_hf = hp;
1346 } else {
1347 /* A Content- header, leave in old. */
1348 hp_prev = hp;
1349 }
1350
1351 hp = next;
1352 }
1353 }
1354
1355
1356 static int
1357 set_ct_type (CT ct, int type, int subtype, int encoding) {
1358 char *typename = ct_type_str (type);
1359 char *subtypename = ct_subtype_str (type, subtype);
1360 /* E.g, " text/plain" */
1361 char *type_subtypename = concat (" ", typename, "/", subtypename, NULL);
1362 /* E.g, " text/plain\n" */
1363 char *name_plus_nl = concat (type_subtypename, "\n", NULL);
1364 int found_content_type = 0;
1365 HF hf;
1366 const char *cp = NULL;
1367 char *ctline;
1368 int status;
1369
1370 /* Update/add Content-Type header field. */
1371 for (hf = ct->c_first_hf; hf; hf = hf->next) {
1372 if (! strcasecmp (TYPE_FIELD, hf->name)) {
1373 found_content_type = 1;
1374 free (hf->value);
1375 hf->value = (cp = strchr (ct->c_ctline, ';'))
1376 ? concat (type_subtypename, cp, "\n", NULL)
1377 : add (name_plus_nl, NULL);
1378 }
1379 }
1380 if (! found_content_type) {
1381 add_header (ct, add (TYPE_FIELD, NULL),
1382 (cp = strchr (ct->c_ctline, ';'))
1383 ? concat (type_subtypename, cp, "\n", NULL)
1384 : add (name_plus_nl, NULL));
1385 }
1386
1387 /* Some of these might not be used, but set them anyway. */
1388 ctline = cp
1389 ? concat (type_subtypename, cp, NULL)
1390 : concat (type_subtypename, NULL);
1391 free (ct->c_ctline);
1392 ct->c_ctline = ctline;
1393 /* Leave other ctinfo members as they were. */
1394 free (ct->c_ctinfo.ci_type);
1395 ct->c_ctinfo.ci_type = add (typename, NULL);
1396 free (ct->c_ctinfo.ci_subtype);
1397 ct->c_ctinfo.ci_subtype = add (subtypename, NULL);
1398 ct->c_type = type;
1399 ct->c_subtype = subtype;
1400
1401 free (name_plus_nl);
1402 free (type_subtypename);
1403
1404 status = set_ce (ct, encoding);
1405
1406 return status;
1407 }
1408
1409
1410 static int
1411 decode_text_parts (CT ct, int encoding, int *message_mods) {
1412 int status = OK;
1413
1414 switch (ct->c_type) {
1415 case CT_TEXT:
1416 switch (ct->c_encoding) {
1417 case CE_BASE64:
1418 case CE_QUOTED: {
1419 int ct_encoding;
1420
1421 if (decode_part (ct) == OK && ct->c_cefile.ce_file) {
1422 const char *reason = NULL;
1423
1424 if ((ct_encoding = content_encoding (ct, &reason)) == CE_BINARY
1425 && encoding != CE_BINARY) {
1426 /* The decoding isn't acceptable so discard it.
1427 Leave status as OK to allow other transformations. */
1428 if (verbosw) {
1429 report (NULL, ct->c_partno, ct->c_file,
1430 "will not decode%s because it is binary (%s)",
1431 ct->c_partno ? ""
1432 : ct->c_ctline ? ct->c_ctline
1433 : "",
1434 reason);
1435 }
1436 (void) m_unlink (ct->c_cefile.ce_file);
1437 free (ct->c_cefile.ce_file);
1438 ct->c_cefile.ce_file = NULL;
1439 } else if (ct->c_encoding == CE_QUOTED &&
1440 ct_encoding == CE_8BIT && encoding == CE_7BIT) {
1441 /* The decoding isn't acceptable so discard it.
1442 Leave status as OK to allow other transformations. */
1443 if (verbosw) {
1444 report (NULL, ct->c_partno, ct->c_file,
1445 "will not decode%s because it is 8bit",
1446 ct->c_partno ? ""
1447 : ct->c_ctline ? ct->c_ctline
1448 : "");
1449 }
1450 (void) m_unlink (ct->c_cefile.ce_file);
1451 free (ct->c_cefile.ce_file);
1452 ct->c_cefile.ce_file = NULL;
1453 } else {
1454 int enc;
1455 if (ct_encoding == CE_BINARY)
1456 enc = CE_BINARY;
1457 else if (ct_encoding == CE_8BIT && encoding == CE_7BIT)
1458 enc = CE_QUOTED;
1459 else
1460 enc = charset_encoding (ct);
1461 if (set_ce (ct, enc) == OK) {
1462 ++*message_mods;
1463 if (verbosw) {
1464 report (NULL, ct->c_partno, ct->c_file, "decode%s",
1465 ct->c_ctline ? ct->c_ctline : "");
1466 }
1467 strip_crs (ct, message_mods);
1468 } else {
1469 status = NOTOK;
1470 }
1471 }
1472 } else {
1473 status = NOTOK;
1474 }
1475 break;
1476 }
1477 case CE_8BIT:
1478 case CE_7BIT:
1479 strip_crs (ct, message_mods);
1480 break;
1481 default:
1482 break;
1483 }
1484
1485 break;
1486
1487 case CT_MULTIPART: {
1488 struct multipart *m = (struct multipart *) ct->c_ctparams;
1489 struct part *part;
1490
1491 /* Should check to see if the body for this part is encoded?
1492 For now, it gets passed along as-is by InitMultiPart(). */
1493 for (part = m->mp_parts; status == OK && part; part = part->mp_next) {
1494 status = decode_text_parts (part->mp_part, encoding, message_mods);
1495 }
1496 break;
1497 }
1498
1499 case CT_MESSAGE:
1500 if (ct->c_subtype == MESSAGE_EXTERNAL) {
1501 struct exbody *e;
1502
1503 e = (struct exbody *) ct->c_ctparams;
1504 status = decode_text_parts (e->eb_content, encoding, message_mods);
1505 }
1506 break;
1507
1508 default:
1509 break;
1510 }
1511
1512 return status;
1513 }
1514
1515
1516 /* See if the decoded content is 7bit, 8bit, or binary. It's binary
1517 if it has any NUL characters, a CR not followed by a LF, or lines
1518 greater than 998 characters in length. If binary, reason is set
1519 to a string explaining why. */
1520 static int
1521 content_encoding (CT ct, const char **reason) {
1522 CE ce = &ct->c_cefile;
1523 int encoding = CE_7BIT;
1524
1525 if (ce->ce_file) {
1526 size_t line_len = 0;
1527 char buffer[BUFSIZ];
1528 size_t inbytes;
1529
1530 if (! ce->ce_fp && (ce->ce_fp = fopen (ce->ce_file, "r")) == NULL) {
1531 advise (ce->ce_file, "unable to open for reading");
1532 return CE_UNKNOWN;
1533 }
1534
1535 fseeko (ce->ce_fp, 0L, SEEK_SET);
1536 while (encoding != CE_BINARY &&
1537 (inbytes = fread (buffer, 1, sizeof buffer, ce->ce_fp)) > 0) {
1538 char *cp;
1539 size_t i;
1540 int last_char_was_cr = 0;
1541
1542 for (i = 0, cp = buffer; i < inbytes; ++i, ++cp) {
1543 if (*cp == '\0' || ++line_len > 998 ||
1544 (*cp != '\n' && last_char_was_cr)) {
1545 encoding = CE_BINARY;
1546 if (*cp == '\0') {
1547 *reason = "null character";
1548 } else if (line_len > 998) {
1549 *reason = "line length > 998";
1550 } else if (*cp != '\n' && last_char_was_cr) {
1551 *reason = "CR not followed by LF";
1552 } else {
1553 /* Should not reach this. */
1554 *reason = "";
1555 }
1556 break;
1557 } else if (*cp == '\n') {
1558 line_len = 0;
1559 } else if (! isascii ((unsigned char) *cp)) {
1560 encoding = CE_8BIT;
1561 }
1562
1563 last_char_was_cr = *cp == '\r' ? 1 : 0;
1564 }
1565 }
1566
1567 fclose (ce->ce_fp);
1568 ce->ce_fp = NULL;
1569 } /* else should never happen */
1570
1571 return encoding;
1572 }
1573
1574
1575 static int
1576 strip_crs (CT ct, int *message_mods) {
1577 char *charset = content_charset (ct);
1578 int status = OK;
1579
1580 /* Only strip carriage returns if content is ASCII or another
1581 charset that has the same readily recognizable CR followed by a
1582 LF. We can include UTF-8 here because if the high-order bit of
1583 a UTF-8 byte is 0, then it must be a single-byte ASCII
1584 character. */
1585 if (! strcasecmp (charset, "US-ASCII") ||
1586 ! strcasecmp (charset, "UTF-8") ||
1587 ! strncasecmp (charset, "ISO-8859-", 9) ||
1588 ! strncasecmp (charset, "WINDOWS-12", 10)) {
1589 char **file = NULL;
1590 FILE **fp = NULL;
1591 size_t begin;
1592 size_t end;
1593 int has_crs = 0;
1594 int opened_input_file = 0;
1595
1596 if (ct->c_cefile.ce_file) {
1597 file = &ct->c_cefile.ce_file;
1598 fp = &ct->c_cefile.ce_fp;
1599 begin = end = 0;
1600 } else if (ct->c_file) {
1601 file = &ct->c_file;
1602 fp = &ct->c_fp;
1603 begin = (size_t) ct->c_begin;
1604 end = (size_t) ct->c_end;
1605 } /* else don't know where the content is */
1606
1607 if (file && *file && fp) {
1608 if (! *fp) {
1609 if ((*fp = fopen (*file, "r")) == NULL) {
1610 advise (*file, "unable to open for reading");
1611 status = NOTOK;
1612 } else {
1613 opened_input_file = 1;
1614 }
1615 }
1616 }
1617
1618 if (fp && *fp) {
1619 char buffer[BUFSIZ];
1620 size_t bytes_read;
1621 size_t bytes_to_read =
1622 end > 0 && end > begin ? end - begin : sizeof buffer;
1623
1624 fseeko (*fp, begin, SEEK_SET);
1625 while ((bytes_read = fread (buffer, 1,
1626 min (bytes_to_read, sizeof buffer),
1627 *fp)) > 0) {
1628 /* Look for CR followed by a LF. This is supposed to
1629 be text so there should be LF's. If not, don't
1630 modify the content. */
1631 char *cp;
1632 size_t i;
1633 int last_char_was_cr = 0;
1634
1635 if (end > 0) { bytes_to_read -= bytes_read; }
1636
1637 for (i = 0, cp = buffer; i < bytes_read; ++i, ++cp) {
1638 if (*cp == '\n' && last_char_was_cr) {
1639 has_crs = 1;
1640 break;
1641 }
1642
1643 last_char_was_cr = *cp == '\r' ? 1 : 0;
1644 }
1645 }
1646
1647 if (has_crs) {
1648 int fd;
1649 char *stripped_content_file;
1650 char *tempfile = m_mktemp2 (NULL, invo_name, &fd, NULL);
1651
1652 if (tempfile == NULL) {
1653 adios (NULL, "unable to create temporary file in %s",
1654 get_temp_dir());
1655 }
1656 stripped_content_file = add (tempfile, NULL);
1657
1658 /* Strip each CR before a LF from the content. */
1659 fseeko (*fp, begin, SEEK_SET);
1660 while ((bytes_read = fread (buffer, 1, sizeof buffer, *fp)) >
1661 0) {
1662 char *cp;
1663 size_t i;
1664 int last_char_was_cr = 0;
1665
1666 for (i = 0, cp = buffer; i < bytes_read; ++i, ++cp) {
1667 if (*cp == '\r') {
1668 last_char_was_cr = 1;
1669 } else if (last_char_was_cr) {
1670 if (*cp != '\n') {
1671 if (write (fd, "\r", 1) < 0) {
1672 advise (tempfile, "write of CR failed");
1673 }
1674 }
1675 if (write (fd, cp, 1) < 0) {
1676 advise (tempfile, "write failed");
1677 }
1678 last_char_was_cr = 0;
1679 } else {
1680 if (write (fd, cp, 1) < 0) {
1681 advise (tempfile, "write failed");
1682 }
1683 last_char_was_cr = 0;
1684 }
1685 }
1686 }
1687
1688 if (close (fd)) {
1689 admonish (NULL, "unable to write temporary file %s",
1690 stripped_content_file);
1691 (void) m_unlink (stripped_content_file);
1692 status = NOTOK;
1693 } else {
1694 /* Replace the decoded file with the converted one. */
1695 if (ct->c_cefile.ce_file) {
1696 if (ct->c_cefile.ce_unlink) {
1697 (void) m_unlink (ct->c_cefile.ce_file);
1698 }
1699 free (ct->c_cefile.ce_file);
1700 }
1701 ct->c_cefile.ce_file = stripped_content_file;
1702 ct->c_cefile.ce_unlink = 1;
1703
1704 ++*message_mods;
1705 if (verbosw) {
1706 report (NULL, ct->c_partno,
1707 begin == 0 && end == 0 ? "" : *file,
1708 "stripped CRs");
1709 }
1710 }
1711 }
1712
1713 if (opened_input_file) {
1714 fclose (*fp);
1715 *fp = NULL;
1716 }
1717 }
1718 }
1719
1720 return status;
1721 }
1722
1723
1724 static int
1725 convert_charsets (CT ct, char *dest_charset, int *message_mods) {
1726 int status = OK;
1727
1728 switch (ct->c_type) {
1729 case CT_TEXT:
1730 if (ct->c_subtype == TEXT_PLAIN) {
1731 status = convert_charset (ct, dest_charset, message_mods);
1732 if (status == OK) {
1733 if (verbosw) {
1734 report (NULL, ct->c_partno, ct->c_file,
1735 "convert %s to %s",
1736 content_charset(ct), dest_charset);
1737 }
1738 } else {
1739 report ("iconv", ct->c_partno, ct->c_file,
1740 "failed to convert %s to %s",
1741 content_charset(ct), dest_charset);
1742 }
1743 }
1744 break;
1745
1746 case CT_MULTIPART: {
1747 struct multipart *m = (struct multipart *) ct->c_ctparams;
1748 struct part *part;
1749
1750 /* Should check to see if the body for this part is encoded?
1751 For now, it gets passed along as-is by InitMultiPart(). */
1752 for (part = m->mp_parts; status == OK && part; part = part->mp_next) {
1753 status =
1754 convert_charsets (part->mp_part, dest_charset, message_mods);
1755 }
1756 break;
1757 }
1758
1759 case CT_MESSAGE:
1760 if (ct->c_subtype == MESSAGE_EXTERNAL) {
1761 struct exbody *e;
1762
1763 e = (struct exbody *) ct->c_ctparams;
1764 status =
1765 convert_charsets (e->eb_content, dest_charset, message_mods);
1766 }
1767 break;
1768
1769 default:
1770 break;
1771 }
1772
1773 return status;
1774 }
1775
1776
1777 static int
1778 write_content (CT ct, char *input_filename, char *outfile, int modify_inplace,
1779 int message_mods) {
1780 int status = OK;
1781
1782 if (modify_inplace) {
1783 if (message_mods > 0) {
1784 if ((status = output_message (ct, outfile)) == OK) {
1785 char *infile = input_filename
1786 ? add (input_filename, NULL)
1787 : add (ct->c_file ? ct->c_file : "-", NULL);
1788
1789 if (remove_file (infile) == OK) {
1790 if (rename (outfile, infile)) {
1791 /* Rename didn't work, possibly because of an
1792 attempt to rename across filesystems. Try
1793 brute force copy. */
1794 int old = open (outfile, O_RDONLY);
1795 int new =
1796 open (infile, O_WRONLY | O_CREAT, m_gmprot ());
1797 int i = -1;
1798
1799 if (old != -1 && new != -1) {
1800 char buffer[BUFSIZ];
1801
1802 while ((i = read (old, buffer, sizeof buffer)) >
1803 0) {
1804 if (write (new, buffer, i) != i) {
1805 i = -1;
1806 break;
1807 }
1808 }
1809 }
1810 if (new != -1) { close (new); }
1811 if (old != -1) { close (old); }
1812 (void) m_unlink (outfile);
1813
1814 if (i < 0) {
1815 /* The -file argument processing used path() to
1816 expand filename to absolute path. */
1817 int file = ct->c_file && ct->c_file[0] == '/';
1818
1819 admonish (NULL, "unable to rename %s %s to %s",
1820 file ? "file" : "message", outfile,
1821 infile);
1822 status = NOTOK;
1823 }
1824 }
1825 } else {
1826 admonish (NULL, "unable to remove input file %s, "
1827 "not modifying it", infile);
1828 (void) m_unlink (outfile);
1829 status = NOTOK;
1830 }
1831
1832 free (infile);
1833 } else {
1834 status = NOTOK;
1835 }
1836 } else {
1837 /* No modifications and didn't need the tmp outfile. */
1838 (void) m_unlink (outfile);
1839 }
1840 } else {
1841 /* Output is going to some file. Produce it whether or not
1842 there were modifications. */
1843 status = output_message (ct, outfile);
1844 }
1845
1846 flush_errors ();
1847 return status;
1848 }
1849
1850
1851 /*
1852 * If "rmmproc" is defined, call that to remove the file. Otherwise,
1853 * use the standard MH backup file.
1854 */
1855 static int
1856 remove_file (char *file) {
1857 if (rmmproc) {
1858 char *rmm_command = concat (rmmproc, " ", file, NULL);
1859 int status = system (rmm_command);
1860
1861 free (rmm_command);
1862 return WIFEXITED (status) ? WEXITSTATUS (status) : NOTOK;
1863 } else {
1864 /* This is OK for a non-message file, it still uses the
1865 BACKUP_PREFIX form. The backup file will be in the same
1866 directory as file. */
1867 return rename (file, m_backup (file));
1868 }
1869 }
1870
1871
1872 static void
1873 report (char *what, char *partno, char *filename, char *message, ...) {
1874 va_list args;
1875 char *fmt;
1876
1877 if (verbosw) {
1878 va_start (args, message);
1879 fmt = concat (filename, partno ? " part " : ", ",
1880 partno ? partno : "", partno ? ", " : "", message, NULL);
1881
1882 advertise (what, NULL, fmt, args);
1883
1884 free (fmt);
1885 va_end (args);
1886 }
1887 }
1888
1889
1890 static void
1891 pipeser (int i)
1892 {
1893 if (i == SIGQUIT) {
1894 fflush (stdout);
1895 fprintf (stderr, "\n");
1896 fflush (stderr);
1897 }
1898
1899 done (1);
1900 /* NOTREACHED */
1901 }