]> diplodocus.org Git - nmh/blob - uip/mhparse.c
picksbr.c: Specify parameters of nexus's n_action function pointer.
[nmh] / uip / mhparse.c
1 /* mhparse.c -- routines to parse the contents of MIME messages
2 *
3 * This code is Copyright (c) 2002, by the authors of nmh. See the
4 * COPYRIGHT file in the root directory of the nmh distribution for
5 * complete copyright information.
6 */
7
8 #include <h/mh.h>
9 #include <fcntl.h>
10 #include <h/md5.h>
11 #include <h/mts.h>
12 #include <h/tws.h>
13 #include <h/mime.h>
14 #include <h/mhparse.h>
15 #include <h/utils.h>
16 #include <h/mhcachesbr.h>
17 #include "../sbr/m_mktemp.h"
18 #include "mhfree.h"
19 #ifdef HAVE_ICONV
20 # include <iconv.h>
21 #endif /* HAVE_ICONV */
22
23
24 extern int debugsw;
25
26 int checksw = 0; /* check Content-MD5 field */
27
28 /*
29 * These are for mhfixmsg to:
30 * 1) Instruct parser not to detect invalid Content-Transfer-Encoding
31 * in a multipart.
32 * 2) Suppress the warning about bogus multipart content, and report it.
33 * 3) Suppress the warning about extraneous trailing ';' in header parameter
34 * lists.
35 */
36 int skip_mp_cte_check;
37 int suppress_bogus_mp_content_warning;
38 int bogus_mp_content;
39 int suppress_extraneous_trailing_semicolon_warning;
40
41 /*
42 * By default, suppress warning about multiple MIME-Version header fields.
43 */
44 int suppress_multiple_mime_version_warning = 1;
45
46 /* list of preferred type/subtype pairs, for -prefer */
47 char *preferred_types[NPREFS],
48 *preferred_subtypes[NPREFS];
49 int npreferred;
50
51
52 /*
53 * Structures for TEXT messages
54 */
55 struct k2v SubText[] = {
56 { "plain", TEXT_PLAIN },
57 { "richtext", TEXT_RICHTEXT }, /* defined in RFC 1341 */
58 { "enriched", TEXT_ENRICHED }, /* defined in RFC 1896 */
59 { NULL, TEXT_UNKNOWN } /* this one must be last! */
60 };
61
62 /* Charset[] removed -- yozo. Mon Oct 8 01:03:41 JST 2012 */
63
64 /*
65 * Structures for MULTIPART messages
66 */
67 struct k2v SubMultiPart[] = {
68 { "mixed", MULTI_MIXED },
69 { "alternative", MULTI_ALTERNATE },
70 { "digest", MULTI_DIGEST },
71 { "parallel", MULTI_PARALLEL },
72 { "related", MULTI_RELATED },
73 { NULL, MULTI_UNKNOWN } /* this one must be last! */
74 };
75
76 /*
77 * Structures for MESSAGE messages
78 */
79 struct k2v SubMessage[] = {
80 { "rfc822", MESSAGE_RFC822 },
81 { "partial", MESSAGE_PARTIAL },
82 { "external-body", MESSAGE_EXTERNAL },
83 { NULL, MESSAGE_UNKNOWN } /* this one must be last! */
84 };
85
86 /*
87 * Structure for APPLICATION messages
88 */
89 struct k2v SubApplication[] = {
90 { "octet-stream", APPLICATION_OCTETS },
91 { "postscript", APPLICATION_POSTSCRIPT },
92 { NULL, APPLICATION_UNKNOWN } /* this one must be last! */
93 };
94
95 /*
96 * Mapping of names of CTE types in mhbuild directives
97 */
98 static struct k2v EncodingType[] = {
99 { "8bit", CE_8BIT },
100 { "qp", CE_QUOTED },
101 { "q-p", CE_QUOTED },
102 { "quoted-printable", CE_QUOTED },
103 { "b64", CE_BASE64 },
104 { "base64", CE_BASE64 },
105 { NULL, 0 },
106 };
107
108
109 /* mhmisc.c */
110 int part_ok (CT);
111 int type_ok (CT, int);
112 void content_error (char *, CT, char *, ...);
113
114 /*
115 * static prototypes
116 */
117 static CT get_content (FILE *, char *, int);
118 static int get_comment (const char *, const char *, char **, char **);
119
120 static int InitGeneric (CT);
121 static int InitText (CT);
122 static int InitMultiPart (CT);
123 static void reverse_parts (CT);
124 static void prefer_parts(CT ct);
125 static int InitMessage (CT);
126 static int InitApplication (CT);
127 static int init_encoding (CT, OpenCEFunc);
128 static unsigned long size_encoding (CT);
129 static int InitBase64 (CT);
130 static int openBase64 (CT, char **);
131 static int InitQuoted (CT);
132 static int openQuoted (CT, char **);
133 static int Init7Bit (CT);
134 static int openExternal (CT, CT, CE, char **, int *);
135 static int InitFile (CT);
136 static int openFile (CT, char **);
137 static int InitFTP (CT);
138 static int openFTP (CT, char **);
139 static int InitMail (CT);
140 static int openMail (CT, char **);
141 static int readDigest (CT, char *);
142 static int get_leftover_mp_content (CT, int);
143 static int InitURL (CT);
144 static int openURL (CT, char **);
145 static int parse_header_attrs (const char *, const char *, char **, PM *,
146 PM *, char **);
147 static size_t param_len(PM, int, size_t, int *, int *, size_t *);
148 static size_t normal_param(PM, char *, size_t, size_t, size_t);
149 static int get_dispo (char *, CT, int);
150
151 struct str2init str2cts[] = {
152 { "application", CT_APPLICATION, InitApplication },
153 { "audio", CT_AUDIO, InitGeneric },
154 { "image", CT_IMAGE, InitGeneric },
155 { "message", CT_MESSAGE, InitMessage },
156 { "multipart", CT_MULTIPART, InitMultiPart },
157 { "text", CT_TEXT, InitText },
158 { "video", CT_VIDEO, InitGeneric },
159 { NULL, CT_EXTENSION, NULL }, /* these two must be last! */
160 { NULL, CT_UNKNOWN, NULL },
161 };
162
163 struct str2init str2ces[] = {
164 { "base64", CE_BASE64, InitBase64 },
165 { "quoted-printable", CE_QUOTED, InitQuoted },
166 { "8bit", CE_8BIT, Init7Bit },
167 { "7bit", CE_7BIT, Init7Bit },
168 { "binary", CE_BINARY, Init7Bit },
169 { NULL, CE_EXTENSION, NULL }, /* these two must be last! */
170 { NULL, CE_UNKNOWN, NULL },
171 };
172
173 /*
174 * NOTE WELL: si_key MUST NOT have value of NOTOK
175 *
176 * si_val is 1 if access method is anonymous.
177 */
178 struct str2init str2methods[] = {
179 { "afs", 1, InitFile },
180 { "anon-ftp", 1, InitFTP },
181 { "ftp", 0, InitFTP },
182 { "local-file", 0, InitFile },
183 { "mail-server", 0, InitMail },
184 { "url", 0, InitURL },
185 { NULL, 0, NULL }
186 };
187
188
189 /*
190 * Main entry point for parsing a MIME message or file.
191 * It returns the Content structure for the top level
192 * entity in the file.
193 */
194
195 CT
196 parse_mime (char *file)
197 {
198 int is_stdin;
199 char buffer[BUFSIZ];
200 FILE *fp;
201 CT ct;
202 size_t n;
203 struct stat statbuf;
204
205 bogus_mp_content = 0;
206
207 /*
208 * Check if file is actually standard input
209 */
210 if ((is_stdin = !(strcmp (file, "-")))) {
211 char *tfile = m_mktemp2(NULL, invo_name, NULL, &fp);
212 if (tfile == NULL) {
213 advise("mhparse", "unable to create temporary file in %s",
214 get_temp_dir());
215 return NULL;
216 }
217 file = mh_xstrdup(tfile);
218
219 while ((n = fread(buffer, 1, sizeof(buffer), stdin)) > 0) {
220 if (fwrite(buffer, 1, n, fp) != n) {
221 (void) m_unlink (file);
222 advise (file, "error copying to temporary file");
223 return NULL;
224 }
225 }
226 fflush (fp);
227
228 if (ferror (stdin)) {
229 (void) m_unlink (file);
230 advise ("stdin", "error reading");
231 return NULL;
232 }
233 if (ferror (fp)) {
234 (void) m_unlink (file);
235 advise (file, "error writing");
236 return NULL;
237 }
238 fseek (fp, 0L, SEEK_SET);
239 } else if (stat (file, &statbuf) == NOTOK) {
240 advise (file, "unable to stat");
241 return NULL;
242 } else if (S_ISDIR(statbuf.st_mode)) {
243 /* Don't try to parse a directory. */
244 inform("%s is a directory", file);
245 return NULL;
246 } else if ((fp = fopen (file, "r")) == NULL) {
247 advise (file, "unable to read");
248 return NULL;
249 }
250
251 if (!(ct = get_content (fp, file, 1))) {
252 if (is_stdin)
253 (void) m_unlink (file);
254 inform("unable to decode %s", file);
255 return NULL;
256 }
257
258 if (is_stdin)
259 ct->c_unlink = 1; /* temp file to remove */
260
261 ct->c_fp = NULL;
262
263 if (ct->c_end == 0L) {
264 fseek (fp, 0L, SEEK_END);
265 ct->c_end = ftell (fp);
266 }
267
268 if (ct->c_ctinitfnx && (*ct->c_ctinitfnx) (ct) == NOTOK) {
269 fclose (fp);
270 free_content (ct);
271 return NULL;
272 }
273
274 fclose (fp);
275 return ct;
276 }
277
278
279 /*
280 * Main routine for reading/parsing the headers
281 * of a message content.
282 *
283 * toplevel = 1 # we are at the top level of the message
284 * toplevel = 0 # we are inside message type or multipart type
285 * # other than multipart/digest
286 * toplevel = -1 # we are inside multipart/digest
287 * NB: on failure we will fclose(in)!
288 */
289
290 static CT
291 get_content (FILE *in, char *file, int toplevel)
292 {
293 int compnum, state;
294 char buf[NMH_BUFSIZ], name[NAMESZ];
295 char *np, *vp;
296 CT ct;
297 HF hp;
298 m_getfld_state_t gstate;
299
300 /* allocate the content structure */
301 NEW0(ct);
302 ct->c_fp = in;
303 ct->c_file = add (file, NULL);
304 ct->c_begin = ftell (ct->c_fp) + 1;
305
306 /*
307 * Parse the header fields for this
308 * content into a linked list.
309 */
310 gstate = m_getfld_state_init(in);
311 m_getfld_track_filepos2(&gstate);
312 for (compnum = 1;;) {
313 int bufsz = sizeof buf;
314 switch (state = m_getfld2(&gstate, name, buf, &bufsz)) {
315 case FLD:
316 case FLDPLUS:
317 compnum++;
318
319 /* get copies of the buffers */
320 np = mh_xstrdup(name);
321 vp = mh_xstrdup(buf);
322
323 /* if necessary, get rest of field */
324 while (state == FLDPLUS) {
325 bufsz = sizeof buf;
326 state = m_getfld2(&gstate, name, buf, &bufsz);
327 vp = add (buf, vp); /* add to previous value */
328 }
329
330 /* Now add the header data to the list */
331 add_header (ct, np, vp);
332
333 /* continue, to see if this isn't the last header field */
334 ct->c_begin = ftell (in) + 1;
335 continue;
336
337 case BODY:
338 /* There are two cases. The unusual one is when there is no
339 * blank line between the headers and the body. This is
340 * indicated by the name of the header starting with `:'.
341 *
342 * For both cases, normal first, `1' is the desired c_begin
343 * file position for the start of the body, and `2' is the
344 * file position when buf is returned.
345 *
346 * f o o : b a r \n \n b o d y \n bufsz = 6
347 * 1 2 move -5
348 * f o o : b a r \n b o d y \n bufsz = 4
349 * 1 2 move -4
350 *
351 * For the normal case, bufsz includes the
352 * header-terminating `\n', even though it is not in buf,
353 * but bufsz isn't affected when it's missing in the unusual
354 * case. */
355 if (name[0] == ':') {
356 ct->c_begin = ftell(in) - bufsz;
357 } else {
358 ct->c_begin = ftell (in) - (bufsz - 1);
359 }
360 break;
361
362 case FILEEOF:
363 ct->c_begin = ftell (in);
364 break;
365
366 case LENERR:
367 case FMTERR:
368 adios (NULL, "message format error in component #%d", compnum);
369
370 default:
371 adios (NULL, "getfld() returned %d", state);
372 }
373
374 /* break out of the loop */
375 break;
376 }
377 m_getfld_state_destroy (&gstate);
378
379 /*
380 * Read the content headers. We will parse the
381 * MIME related header fields into their various
382 * structures and set internal flags related to
383 * content type/subtype, etc.
384 */
385
386 hp = ct->c_first_hf; /* start at first header field */
387 while (hp) {
388 /* Get MIME-Version field */
389 if (!strcasecmp (hp->name, VRSN_FIELD)) {
390 int ucmp;
391 char c, *cp, *dp;
392 char *vrsn;
393
394 vrsn = add (hp->value, NULL);
395
396 /* Now, cleanup this field */
397 cp = vrsn;
398
399 while (isspace ((unsigned char) *cp))
400 cp++;
401 for (dp = strchr(cp, '\n'); dp; dp = strchr(dp, '\n'))
402 *dp++ = ' ';
403 for (dp = cp + strlen (cp) - 1; dp >= cp; dp--)
404 if (!isspace ((unsigned char) *dp))
405 break;
406 *++dp = '\0';
407 if (debugsw)
408 fprintf (stderr, "%s: %s\n", VRSN_FIELD, cp);
409
410 if (*cp == '(' &&
411 get_comment (ct->c_file, VRSN_FIELD, &cp, NULL) == NOTOK)
412 goto out;
413
414 for (dp = cp; istoken (*dp); dp++)
415 continue;
416 c = *dp;
417 *dp = '\0';
418 ucmp = !strcasecmp (cp, VRSN_VALUE);
419 *dp = c;
420 if (!ucmp) {
421 inform("message %s has unknown value for %s: field (%s), continuing...",
422 ct->c_file, VRSN_FIELD, cp);
423 }
424 if (!ct->c_vrsn) {
425 ct->c_vrsn = vrsn;
426 } else {
427 if (! suppress_multiple_mime_version_warning)
428 inform("message %s has multiple %s: fields",
429 ct->c_file, VRSN_FIELD);
430 free(vrsn);
431 }
432 }
433 else if (!strcasecmp (hp->name, TYPE_FIELD)) {
434 /* Get Content-Type field */
435 struct str2init *s2i;
436 CI ci = &ct->c_ctinfo;
437
438 /* Check if we've already seen a Content-Type header */
439 if (ct->c_ctline) {
440 inform("message %s has multiple %s: fields",
441 ct->c_file, TYPE_FIELD);
442 goto next_header;
443 }
444
445 /* Parse the Content-Type field */
446 if (get_ctinfo (hp->value, ct, 0) == NOTOK)
447 goto out;
448
449 /*
450 * Set the Init function and the internal
451 * flag for this content type.
452 */
453 for (s2i = str2cts; s2i->si_key; s2i++)
454 if (!strcasecmp (ci->ci_type, s2i->si_key))
455 break;
456 if (!s2i->si_key && !uprf (ci->ci_type, "X-"))
457 s2i++;
458 ct->c_type = s2i->si_val;
459 ct->c_ctinitfnx = s2i->si_init;
460 }
461 else if (!strcasecmp (hp->name, ENCODING_FIELD)) {
462 /* Get Content-Transfer-Encoding field */
463 char c, *cp, *dp;
464 struct str2init *s2i;
465
466 /*
467 * Check if we've already seen the
468 * Content-Transfer-Encoding field
469 */
470 if (ct->c_celine) {
471 inform("message %s has multiple %s: fields",
472 ct->c_file, ENCODING_FIELD);
473 goto next_header;
474 }
475
476 /* get copy of this field */
477 ct->c_celine = cp = add (hp->value, NULL);
478
479 while (isspace ((unsigned char) *cp))
480 cp++;
481 for (dp = cp; istoken (*dp); dp++)
482 continue;
483 c = *dp;
484 *dp = '\0';
485
486 /*
487 * Find the internal flag and Init function
488 * for this transfer encoding.
489 */
490 for (s2i = str2ces; s2i->si_key; s2i++)
491 if (!strcasecmp (cp, s2i->si_key))
492 break;
493 if (!s2i->si_key && !uprf (cp, "X-"))
494 s2i++;
495 *dp = c;
496 ct->c_encoding = s2i->si_val;
497
498 /* Call the Init function for this encoding */
499 if (s2i->si_init && (*s2i->si_init) (ct) == NOTOK)
500 goto out;
501 }
502 else if (!strcasecmp (hp->name, MD5_FIELD)) {
503 /* Get Content-MD5 field */
504 char *cp, *dp, *ep;
505
506 if (!checksw)
507 goto next_header;
508
509 if (ct->c_digested) {
510 inform("message %s has multiple %s: fields",
511 ct->c_file, MD5_FIELD);
512 goto next_header;
513 }
514
515 ep = cp = add (hp->value, NULL); /* get a copy */
516
517 while (isspace ((unsigned char) *cp))
518 cp++;
519 for (dp = strchr(cp, '\n'); dp; dp = strchr(dp, '\n'))
520 *dp++ = ' ';
521 for (dp = cp + strlen (cp) - 1; dp >= cp; dp--)
522 if (!isspace ((unsigned char) *dp))
523 break;
524 *++dp = '\0';
525 if (debugsw)
526 fprintf (stderr, "%s: %s\n", MD5_FIELD, cp);
527
528 if (*cp == '(' &&
529 get_comment (ct->c_file, MD5_FIELD, &cp, NULL) == NOTOK) {
530 free (ep);
531 goto out;
532 }
533
534 for (dp = cp; *dp && !isspace ((unsigned char) *dp); dp++)
535 continue;
536 *dp = '\0';
537
538 readDigest (ct, cp);
539 free (ep);
540 ct->c_digested++;
541 }
542 else if (!strcasecmp (hp->name, ID_FIELD)) {
543 /* Get Content-ID field */
544 ct->c_id = add (hp->value, ct->c_id);
545 }
546 else if (!strcasecmp (hp->name, DESCR_FIELD)) {
547 /* Get Content-Description field */
548 ct->c_descr = add (hp->value, ct->c_descr);
549 }
550 else if (!strcasecmp (hp->name, DISPO_FIELD)) {
551 /* Get Content-Disposition field */
552 if (get_dispo(hp->value, ct, 0) == NOTOK)
553 goto out;
554 }
555
556 next_header:
557 hp = hp->next; /* next header field */
558 }
559
560 /*
561 * Check if we saw a Content-Type field.
562 * If not, then assign a default value for
563 * it, and the Init function.
564 */
565 if (!ct->c_ctline) {
566 /*
567 * If we are inside a multipart/digest message,
568 * so default type is message/rfc822
569 */
570 if (toplevel < 0) {
571 if (get_ctinfo ("message/rfc822", ct, 0) == NOTOK)
572 goto out;
573 ct->c_type = CT_MESSAGE;
574 ct->c_ctinitfnx = InitMessage;
575 } else {
576 /*
577 * Else default type is text/plain
578 */
579 if (get_ctinfo ("text/plain", ct, 0) == NOTOK)
580 goto out;
581 ct->c_type = CT_TEXT;
582 ct->c_ctinitfnx = InitText;
583 }
584 }
585
586 /* Use default Transfer-Encoding, if necessary */
587 if (!ct->c_celine) {
588 ct->c_encoding = CE_7BIT;
589 Init7Bit (ct);
590 }
591
592 return ct;
593
594 out:
595 free_content (ct);
596 return NULL;
597 }
598
599
600 /*
601 * small routine to add header field to list
602 */
603
604 int
605 add_header (CT ct, char *name, char *value)
606 {
607 HF hp;
608
609 /* allocate header field structure */
610 NEW(hp);
611
612 /* link data into header structure */
613 hp->name = name;
614 hp->value = value;
615 hp->next = NULL;
616
617 /* link header structure into the list */
618 if (ct->c_first_hf == NULL) {
619 ct->c_first_hf = hp; /* this is the first */
620 ct->c_last_hf = hp;
621 } else {
622 ct->c_last_hf->next = hp; /* add it to the end */
623 ct->c_last_hf = hp;
624 }
625
626 return 0;
627 }
628
629
630 /*
631 * Parse Content-Type line and (if `magic' is non-zero) mhbuild composition
632 * directives. Fills in the information of the CTinfo structure.
633 */
634 int
635 get_ctinfo (char *cp, CT ct, int magic)
636 {
637 char *dp;
638 char c;
639 CI ci;
640 int status;
641
642 ci = &ct->c_ctinfo;
643
644 /* store copy of Content-Type line */
645 cp = ct->c_ctline = add (cp, NULL);
646
647 while (isspace ((unsigned char) *cp)) /* trim leading spaces */
648 cp++;
649
650 /* change newlines to spaces */
651 for (dp = strchr(cp, '\n'); dp; dp = strchr(dp, '\n'))
652 *dp++ = ' ';
653
654 /* trim trailing spaces */
655 for (dp = cp + strlen (cp) - 1; dp >= cp; dp--)
656 if (!isspace ((unsigned char) *dp))
657 break;
658 *++dp = '\0';
659
660 if (debugsw)
661 fprintf (stderr, "%s: %s\n", TYPE_FIELD, cp);
662
663 if (*cp == '(' && get_comment (ct->c_file, TYPE_FIELD, &cp,
664 &ci->ci_comment) == NOTOK)
665 return NOTOK;
666
667 for (dp = cp; istoken (*dp); dp++)
668 continue;
669 c = *dp;
670 *dp = '\0';
671 ci->ci_type = mh_xstrdup(cp); /* store content type */
672 *dp = c;
673 cp = dp;
674
675 if (!*ci->ci_type) {
676 inform("invalid %s: field in message %s (empty type)",
677 TYPE_FIELD, ct->c_file);
678 return NOTOK;
679 }
680 to_lower(ci->ci_type);
681
682 while (isspace ((unsigned char) *cp))
683 cp++;
684
685 if (*cp == '(' && get_comment (ct->c_file, TYPE_FIELD, &cp,
686 &ci->ci_comment) == NOTOK)
687 return NOTOK;
688
689 if (*cp != '/') {
690 if (!magic)
691 ci->ci_subtype = mh_xstrdup("");
692 goto magic_skip;
693 }
694
695 cp++;
696 while (isspace ((unsigned char) *cp))
697 cp++;
698
699 if (*cp == '(' && get_comment (ct->c_file, TYPE_FIELD, &cp,
700 &ci->ci_comment) == NOTOK)
701 return NOTOK;
702
703 for (dp = cp; istoken (*dp); dp++)
704 continue;
705 c = *dp;
706 *dp = '\0';
707 ci->ci_subtype = mh_xstrdup(cp); /* store the content subtype */
708 *dp = c;
709 cp = dp;
710
711 if (!*ci->ci_subtype) {
712 inform("invalid %s: field in message %s (empty subtype for \"%s\")",
713 TYPE_FIELD, ct->c_file, ci->ci_type);
714 return NOTOK;
715 }
716 to_lower(ci->ci_subtype);
717
718 magic_skip:
719 while (isspace ((unsigned char) *cp))
720 cp++;
721
722 if (*cp == '(' && get_comment (ct->c_file, TYPE_FIELD, &cp,
723 &ci->ci_comment) == NOTOK)
724 return NOTOK;
725
726 if ((status = parse_header_attrs (ct->c_file, TYPE_FIELD, &cp,
727 &ci->ci_first_pm, &ci->ci_last_pm,
728 &ci->ci_comment)) != OK) {
729 return status == NOTOK ? NOTOK : OK;
730 }
731
732 /*
733 * Get any <Content-Id> given in buffer
734 */
735 if (magic && *cp == '<') {
736 mh_xfree(ct->c_id);
737 ct->c_id = NULL;
738 if (!(dp = strchr(ct->c_id = ++cp, '>'))) {
739 inform("invalid ID in message %s", ct->c_file);
740 return NOTOK;
741 }
742 c = *dp;
743 *dp = '\0';
744 if (*ct->c_id)
745 ct->c_id = concat ("<", ct->c_id, ">\n", NULL);
746 else
747 ct->c_id = NULL;
748 *dp++ = c;
749 cp = dp;
750
751 while (isspace ((unsigned char) *cp))
752 cp++;
753 }
754
755 /*
756 * Get any [Content-Description] given in buffer.
757 */
758 if (magic && *cp == '[') {
759 ct->c_descr = ++cp;
760 for (dp = cp + strlen (cp) - 1; dp >= cp; dp--)
761 if (*dp == ']')
762 break;
763 if (dp < cp) {
764 inform("invalid description in message %s", ct->c_file);
765 ct->c_descr = NULL;
766 return NOTOK;
767 }
768
769 c = *dp;
770 *dp = '\0';
771 if (*ct->c_descr)
772 ct->c_descr = concat (ct->c_descr, "\n", NULL);
773 else
774 ct->c_descr = NULL;
775 *dp++ = c;
776 cp = dp;
777
778 while (isspace ((unsigned char) *cp))
779 cp++;
780 }
781
782 /*
783 * Get any {Content-Disposition} given in buffer.
784 */
785 if (magic && *cp == '{') {
786 ++cp;
787 for (dp = cp + strlen (cp) - 1; dp >= cp; dp--)
788 if (*dp == '}')
789 break;
790 if (dp < cp) {
791 inform("invalid disposition in message %s", ct->c_file);
792 ct->c_dispo = NULL;
793 return NOTOK;
794 }
795
796 c = *dp;
797 *dp = '\0';
798
799 if (get_dispo(cp, ct, 1) != OK)
800 return NOTOK;
801
802 *dp++ = c;
803 cp = dp;
804
805 while (isspace ((unsigned char) *cp))
806 cp++;
807 }
808
809 /*
810 * Get any extension directives (right now just the content transfer
811 * encoding, but maybe others) that we care about.
812 */
813
814 if (magic && *cp == '*') {
815 /*
816 * See if it's a CTE we match on
817 */
818 struct k2v *kv;
819
820 dp = ++cp;
821 while (*cp != '\0' && ! isspace((unsigned char) *cp))
822 cp++;
823
824 if (dp == cp) {
825 inform("invalid null transfer encoding specification");
826 return NOTOK;
827 }
828
829 if (*cp != '\0')
830 *cp++ = '\0';
831
832 ct->c_reqencoding = CE_UNKNOWN;
833
834 for (kv = EncodingType; kv->kv_key; kv++) {
835 if (strcasecmp(kv->kv_key, dp) == 0) {
836 ct->c_reqencoding = kv->kv_value;
837 break;
838 }
839 }
840
841 if (ct->c_reqencoding == CE_UNKNOWN) {
842 inform("invalid CTE specification: \"%s\"", dp);
843 return NOTOK;
844 }
845
846 while (isspace ((unsigned char) *cp))
847 cp++;
848 }
849
850 /*
851 * Check if anything is left over
852 */
853 if (*cp) {
854 if (magic) {
855 ci->ci_magic = mh_xstrdup(cp);
856
857 /* If there is a Content-Disposition header and it doesn't
858 have a *filename=, extract it from the magic contents.
859 The r1bindex call skips any leading directory
860 components. */
861 if (ct->c_dispo_type &&
862 !get_param(ct->c_dispo_first, "filename", '_', 1)) {
863 add_param(&ct->c_dispo_first, &ct->c_dispo_last, "filename",
864 r1bindex(ci->ci_magic, '/'), 0);
865 }
866 }
867 else
868 inform("extraneous information in message %s's %s: field\n"
869 " (%s)", ct->c_file, TYPE_FIELD, cp);
870 }
871
872 return OK;
873 }
874
875
876 /*
877 * Parse out a Content-Disposition header. A lot of this is cribbed from
878 * get_ctinfo().
879 */
880 static int
881 get_dispo (char *cp, CT ct, int buildflag)
882 {
883 char *dp, *dispoheader;
884 char c;
885 int status;
886
887 /*
888 * Save the whole copy of the Content-Disposition header, unless we're
889 * processing a mhbuild directive. A NULL c_dispo will be a flag to
890 * mhbuild that the disposition header needs to be generated at that
891 * time.
892 */
893
894 dispoheader = cp = add(cp, NULL);
895
896 while (isspace ((unsigned char) *cp)) /* trim leading spaces */
897 cp++;
898
899 /* change newlines to spaces */
900 for (dp = strchr(cp, '\n'); dp; dp = strchr(dp, '\n'))
901 *dp++ = ' ';
902
903 /* trim trailing spaces */
904 for (dp = cp + strlen (cp) - 1; dp >= cp; dp--)
905 if (!isspace ((unsigned char) *dp))
906 break;
907 *++dp = '\0';
908
909 if (debugsw)
910 fprintf (stderr, "%s: %s\n", DISPO_FIELD, cp);
911
912 if (*cp == '(' && get_comment (ct->c_file, DISPO_FIELD, &cp, NULL) ==
913 NOTOK) {
914 free(dispoheader);
915 return NOTOK;
916 }
917
918 for (dp = cp; istoken (*dp); dp++)
919 continue;
920 c = *dp;
921 *dp = '\0';
922 ct->c_dispo_type = mh_xstrdup(cp); /* store disposition type */
923 *dp = c;
924 cp = dp;
925
926 if (*cp == '(' && get_comment (ct->c_file, DISPO_FIELD, &cp, NULL) == NOTOK)
927 return NOTOK;
928
929 if ((status = parse_header_attrs (ct->c_file, DISPO_FIELD, &cp,
930 &ct->c_dispo_first, &ct->c_dispo_last,
931 NULL)) != OK) {
932 if (status == NOTOK) {
933 free(dispoheader);
934 return NOTOK;
935 }
936 } else if (*cp) {
937 inform("extraneous information in message %s's %s: field\n (%s)",
938 ct->c_file, DISPO_FIELD, cp);
939 }
940
941 if (buildflag)
942 free(dispoheader);
943 else
944 ct->c_dispo = dispoheader;
945
946 return OK;
947 }
948
949
950 static int
951 get_comment (const char *filename, const char *fieldname, char **ap,
952 char **commentp)
953 {
954 int i;
955 char *bp, *cp;
956 char c, buffer[BUFSIZ], *dp;
957
958 cp = *ap;
959 bp = buffer;
960 cp++;
961
962 for (i = 0;;) {
963 switch (c = *cp++) {
964 case '\0':
965 invalid:
966 inform("invalid comment in message %s's %s: field",
967 filename, fieldname);
968 return NOTOK;
969
970 case '\\':
971 *bp++ = c;
972 if ((c = *cp++) == '\0')
973 goto invalid;
974 *bp++ = c;
975 continue;
976
977 case '(':
978 i++;
979 /* FALLTHRU */
980 default:
981 *bp++ = c;
982 continue;
983
984 case ')':
985 if (--i < 0)
986 break;
987 *bp++ = c;
988 continue;
989 }
990 break;
991 }
992 *bp = '\0';
993
994 if (commentp) {
995 if ((dp = *commentp)) {
996 *commentp = concat (dp, " ", buffer, NULL);
997 free (dp);
998 } else {
999 *commentp = mh_xstrdup(buffer);
1000 }
1001 }
1002
1003 while (isspace ((unsigned char) *cp))
1004 cp++;
1005
1006 *ap = cp;
1007 return OK;
1008 }
1009
1010
1011 /*
1012 * CONTENTS
1013 *
1014 * Handles content types audio, image, and video.
1015 * There's not much to do right here.
1016 */
1017
1018 static int
1019 InitGeneric (CT ct)
1020 {
1021 NMH_UNUSED (ct);
1022
1023 return OK; /* not much to do here */
1024 }
1025
1026
1027 /*
1028 * TEXT
1029 */
1030
1031 static int
1032 InitText (CT ct)
1033 {
1034 char buffer[BUFSIZ];
1035 char *chset = NULL;
1036 char *cp;
1037 PM pm;
1038 struct text *t;
1039 CI ci = &ct->c_ctinfo;
1040
1041 /* check for missing subtype */
1042 if (!*ci->ci_subtype)
1043 ci->ci_subtype = add ("plain", ci->ci_subtype);
1044
1045 /* match subtype */
1046 ct->c_subtype = ct_str_subtype (CT_TEXT, ci->ci_subtype);
1047
1048 /* allocate text character set structure */
1049 NEW0(t);
1050 ct->c_ctparams = (void *) t;
1051
1052 /* scan for charset parameter */
1053 for (pm = ci->ci_first_pm; pm; pm = pm->pm_next)
1054 if (!strcasecmp (pm->pm_name, "charset"))
1055 break;
1056
1057 /* check if content specified a character set */
1058 if (pm) {
1059 chset = pm->pm_value;
1060 t->tx_charset = CHARSET_SPECIFIED;
1061 } else {
1062 t->tx_charset = CHARSET_UNSPECIFIED;
1063 }
1064
1065 /*
1066 * If we can not handle character set natively,
1067 * then check profile for string to modify the
1068 * terminal or display method.
1069 *
1070 * termproc is for mhshow, though mhlist -debug prints it, too.
1071 */
1072 if (chset != NULL && !check_charset (chset, strlen (chset))) {
1073 snprintf (buffer, sizeof(buffer), "%s-charset-%s", invo_name, chset);
1074 if ((cp = context_find (buffer)))
1075 ct->c_termproc = mh_xstrdup(cp);
1076 }
1077
1078 return OK;
1079 }
1080
1081
1082 /*
1083 * MULTIPART
1084 */
1085
1086 static int
1087 InitMultiPart (CT ct)
1088 {
1089 int inout;
1090 long last, pos;
1091 char *cp, *dp;
1092 PM pm;
1093 char *bp;
1094 char *bufp = NULL;
1095 size_t buflen;
1096 ssize_t gotlen;
1097 struct multipart *m;
1098 struct part *part, **next;
1099 CI ci = &ct->c_ctinfo;
1100 CT p;
1101 FILE *fp;
1102
1103 /*
1104 * The encoding for multipart messages must be either
1105 * 7bit, 8bit, or binary (per RFC 2045).
1106 */
1107 if (! skip_mp_cte_check && ct->c_encoding != CE_7BIT &&
1108 ct->c_encoding != CE_8BIT && ct->c_encoding != CE_BINARY) {
1109 /* Copy the Content-Transfer-Encoding header field body so we can
1110 remove any trailing whitespace and leading blanks from it. */
1111 char *cte = mh_xstrdup(ct->c_celine ? ct->c_celine : "(null)");
1112
1113 bp = cte + strlen (cte) - 1;
1114 while (bp >= cte && isspace ((unsigned char) *bp)) *bp-- = '\0';
1115 for (bp = cte; *bp && isblank ((unsigned char) *bp); ++bp) continue;
1116
1117 inform("\"%s/%s\" type in message %s must be encoded in\n"
1118 "7bit, 8bit, or binary, per RFC 2045 (6.4). "
1119 "mhfixmsg -fixcte can fix it, or\n"
1120 "manually edit the file and change the \"%s\"\n"
1121 "Content-Transfer-Encoding to one of those. For now, continuing...",
1122 ci->ci_type, ci->ci_subtype, ct->c_file, bp);
1123 free (cte);
1124
1125 return NOTOK;
1126 }
1127
1128 /* match subtype */
1129 ct->c_subtype = ct_str_subtype (CT_MULTIPART, ci->ci_subtype);
1130
1131 /*
1132 * Check for "boundary" parameter, which is
1133 * required for multipart messages.
1134 */
1135 bp = 0;
1136 for (pm = ci->ci_first_pm; pm; pm = pm->pm_next) {
1137 if (!strcasecmp (pm->pm_name, "boundary")) {
1138 bp = pm->pm_value;
1139 break;
1140 }
1141 }
1142
1143 /* complain if boundary parameter is missing */
1144 if (!pm) {
1145 inform("a \"boundary\" parameter is mandatory for \"%s/%s\" type in message %s's %s: field",
1146 ci->ci_type, ci->ci_subtype, ct->c_file, TYPE_FIELD);
1147 return NOTOK;
1148 }
1149
1150 /* allocate primary structure for multipart info */
1151 NEW0(m);
1152 ct->c_ctparams = (void *) m;
1153
1154 /* check if boundary parameter contains only whitespace characters */
1155 for (cp = bp; isspace ((unsigned char) *cp); cp++)
1156 continue;
1157 if (!*cp) {
1158 inform("invalid \"boundary\" parameter for \"%s/%s\" type in message %s's %s: field",
1159 ci->ci_type, ci->ci_subtype, ct->c_file, TYPE_FIELD);
1160 return NOTOK;
1161 }
1162
1163 /* remove trailing whitespace from boundary parameter */
1164 for (cp = bp, dp = cp + strlen (cp) - 1; dp > cp; dp--)
1165 if (!isspace ((unsigned char) *dp))
1166 break;
1167 *++dp = '\0';
1168
1169 /* record boundary separators */
1170 m->mp_start = concat (bp, "\n", NULL);
1171 m->mp_stop = concat (bp, "--\n", NULL);
1172
1173 if (!ct->c_fp && (ct->c_fp = fopen (ct->c_file, "r")) == NULL) {
1174 advise (ct->c_file, "unable to open for reading");
1175 return NOTOK;
1176 }
1177
1178 fseek (fp = ct->c_fp, pos = ct->c_begin, SEEK_SET);
1179 last = ct->c_end;
1180 next = &m->mp_parts;
1181 part = NULL;
1182 inout = 1;
1183
1184 while ((gotlen = getline(&bufp, &buflen, fp)) != -1) {
1185 if (pos > last)
1186 break;
1187
1188 pos += gotlen;
1189 if (bufp[0] != '-' || bufp[1] != '-')
1190 continue;
1191 if (inout) {
1192 if (strcmp (bufp + 2, m->mp_start))
1193 continue;
1194 next_part:
1195 NEW0(part);
1196 *next = part;
1197 next = &part->mp_next;
1198
1199 if (!(p = get_content (fp, ct->c_file,
1200 ct->c_subtype == MULTI_DIGEST ? -1 : 0))) {
1201 free(bufp);
1202 ct->c_fp = NULL;
1203 return NOTOK;
1204 }
1205 p->c_fp = NULL;
1206 part->mp_part = p;
1207 pos = p->c_begin;
1208 fseek (fp, pos, SEEK_SET);
1209 inout = 0;
1210 } else {
1211 if (strcmp (bufp + 2, m->mp_start) == 0) {
1212 inout = 1;
1213 end_part:
1214 p = part->mp_part;
1215 p->c_end = ftell(fp) - (gotlen + 1);
1216 if (p->c_end < p->c_begin)
1217 p->c_begin = p->c_end;
1218 if (inout)
1219 goto next_part;
1220 goto last_part;
1221 }
1222 if (strcmp (bufp + 2, m->mp_stop) == 0)
1223 goto end_part;
1224 }
1225 }
1226
1227 if (! suppress_bogus_mp_content_warning) {
1228 inform("bogus multipart content in message %s", ct->c_file);
1229 }
1230 bogus_mp_content = 1;
1231
1232 if (!inout && part) {
1233 p = part->mp_part;
1234 p->c_end = ct->c_end;
1235
1236 if (p->c_begin >= p->c_end) {
1237 for (next = &m->mp_parts; *next != part;
1238 next = &((*next)->mp_next))
1239 continue;
1240 *next = NULL;
1241 free_content (p);
1242 free(part);
1243 }
1244 }
1245
1246 last_part:
1247 /* reverse the order of the parts for multipart/alternative */
1248 if (ct->c_subtype == MULTI_ALTERNATE) {
1249 reverse_parts (ct);
1250 prefer_parts (ct);
1251 }
1252
1253 /*
1254 * label all subparts with part number, and
1255 * then initialize the content of the subpart.
1256 */
1257 {
1258 int partnum;
1259 char *pp;
1260 char partnam[BUFSIZ];
1261
1262 if (ct->c_partno) {
1263 snprintf (partnam, sizeof(partnam), "%s.", ct->c_partno);
1264 pp = partnam + strlen (partnam);
1265 } else {
1266 pp = partnam;
1267 }
1268
1269 for (part = m->mp_parts, partnum = 1; part;
1270 part = part->mp_next, partnum++) {
1271 p = part->mp_part;
1272
1273 sprintf (pp, "%d", partnum);
1274 p->c_partno = mh_xstrdup(partnam);
1275
1276 /* initialize the content of the subparts */
1277 if (p->c_ctinitfnx && (*p->c_ctinitfnx) (p) == NOTOK) {
1278 free(bufp);
1279 fclose (ct->c_fp);
1280 ct->c_fp = NULL;
1281 return NOTOK;
1282 }
1283 }
1284 }
1285
1286 get_leftover_mp_content (ct, 1);
1287 get_leftover_mp_content (ct, 0);
1288
1289 free(bufp);
1290 fclose (ct->c_fp);
1291 ct->c_fp = NULL;
1292 return OK;
1293 }
1294
1295
1296 /*
1297 * reverse the order of the parts of a multipart/alternative,
1298 * presumably to put the "most favored" alternative first, for
1299 * ease of choosing/displaying it later on. from a mail message on
1300 * nmh-workers, from kenh:
1301 * "Stock" MH 6.8.5 did not have a reverse_parts() function, but I
1302 * see code in mhn that did the same thing... According to the RCS
1303 * logs, that code was around from the initial checkin of mhn.c by
1304 * John Romine in 1992, which is as far back as we have."
1305 */
1306 static void
1307 reverse_parts (CT ct)
1308 {
1309 struct multipart *m = (struct multipart *) ct->c_ctparams;
1310 struct part *part;
1311 struct part *next;
1312
1313 /* Reverse the order of its parts by walking the mp_parts list
1314 and pushing each node to the front. */
1315 for (part = m->mp_parts, m->mp_parts = NULL; part; part = next) {
1316 next = part->mp_next;
1317 part->mp_next = m->mp_parts;
1318 m->mp_parts = part;
1319 }
1320 }
1321
1322 static void
1323 move_preferred_part (CT ct, char *type, char *subtype)
1324 {
1325 struct multipart *m = (struct multipart *) ct->c_ctparams;
1326 struct part *part, *prev, *head, *nhead, *ntail;
1327 struct part h, n;
1328 CI ci;
1329
1330 /* move the matching part(s) to the head of the list: walk the
1331 * list of parts, move matching parts to a new list (maintaining
1332 * their order), and finally, concatenate the old list onto the
1333 * new.
1334 */
1335
1336 head = &h;
1337 nhead = &n;
1338
1339 head->mp_next = m->mp_parts;
1340 nhead->mp_next = NULL;
1341 ntail = nhead;
1342
1343 prev = head;
1344 part = head->mp_next;
1345 while (part != NULL) {
1346 ci = &part->mp_part->c_ctinfo;
1347 if (!strcasecmp(ci->ci_type, type) &&
1348 (!subtype || !strcasecmp(ci->ci_subtype, subtype))) {
1349 prev->mp_next = part->mp_next;
1350 part->mp_next = NULL;
1351 ntail->mp_next = part;
1352 ntail = part;
1353 part = prev->mp_next;
1354 } else {
1355 prev = part;
1356 part = prev->mp_next;
1357 }
1358 }
1359 ntail->mp_next = head->mp_next;
1360 m->mp_parts = nhead->mp_next;
1361
1362 }
1363
1364 /*
1365 * move parts that match the user's preferences (-prefer) to the head
1366 * of the line. process preferences in reverse so first one given
1367 * ends up first in line
1368 */
1369 static void
1370 prefer_parts(CT ct)
1371 {
1372 int i;
1373 for (i = npreferred-1; i >= 0; i--)
1374 move_preferred_part(ct, preferred_types[i], preferred_subtypes[i]);
1375 }
1376
1377
1378
1379 /* parse_mime() arranges alternates in reverse (priority) order. This
1380 function can be used to reverse them back. This will put, for
1381 example, a text/plain part before a text/html part in a
1382 multipart/alternative part, for example, where it belongs. */
1383 void
1384 reverse_alternative_parts (CT ct) {
1385 if (ct->c_type == CT_MULTIPART) {
1386 struct multipart *m = (struct multipart *) ct->c_ctparams;
1387 struct part *part;
1388
1389 if (ct->c_subtype == MULTI_ALTERNATE) {
1390 reverse_parts (ct);
1391 }
1392
1393 /* And call recursively on each part of a multipart. */
1394 for (part = m->mp_parts; part; part = part->mp_next) {
1395 reverse_alternative_parts (part->mp_part);
1396 }
1397 }
1398 }
1399
1400
1401 /*
1402 * MESSAGE
1403 */
1404
1405 static int
1406 InitMessage (CT ct)
1407 {
1408 CI ci = &ct->c_ctinfo;
1409
1410 if ((ct->c_encoding != CE_7BIT) && (ct->c_encoding != CE_8BIT)) {
1411 inform("\"%s/%s\" type in message %s should be encoded in "
1412 "7bit or 8bit, continuing...", ci->ci_type, ci->ci_subtype,
1413 ct->c_file);
1414 return NOTOK;
1415 }
1416
1417 /* check for missing subtype */
1418 if (!*ci->ci_subtype)
1419 ci->ci_subtype = add ("rfc822", ci->ci_subtype);
1420
1421 /* match subtype */
1422 ct->c_subtype = ct_str_subtype (CT_MESSAGE, ci->ci_subtype);
1423
1424 switch (ct->c_subtype) {
1425 case MESSAGE_RFC822:
1426 break;
1427
1428 case MESSAGE_PARTIAL:
1429 {
1430 PM pm;
1431 struct partial *p;
1432
1433 NEW0(p);
1434 ct->c_ctparams = (void *) p;
1435
1436 /* scan for parameters "id", "number", and "total" */
1437 for (pm = ci->ci_first_pm; pm; pm = pm->pm_next) {
1438 if (!strcasecmp (pm->pm_name, "id")) {
1439 p->pm_partid = add (pm->pm_value, NULL);
1440 continue;
1441 }
1442 if (!strcasecmp (pm->pm_name, "number")) {
1443 if (sscanf (pm->pm_value, "%d", &p->pm_partno) != 1
1444 || p->pm_partno < 1) {
1445 invalid_param:
1446 inform("invalid %s parameter for \"%s/%s\" type in message %s's %s field",
1447 pm->pm_name, ci->ci_type, ci->ci_subtype,
1448 ct->c_file, TYPE_FIELD);
1449 return NOTOK;
1450 }
1451 continue;
1452 }
1453 if (!strcasecmp (pm->pm_name, "total")) {
1454 if (sscanf (pm->pm_value, "%d", &p->pm_maxno) != 1
1455 || p->pm_maxno < 1)
1456 goto invalid_param;
1457 continue;
1458 }
1459 }
1460
1461 if (!p->pm_partid
1462 || !p->pm_partno
1463 || (p->pm_maxno && p->pm_partno > p->pm_maxno)) {
1464 inform("invalid parameters for \"%s/%s\" type in message %s's %s field",
1465 ci->ci_type, ci->ci_subtype, ct->c_file, TYPE_FIELD);
1466 return NOTOK;
1467 }
1468 }
1469 break;
1470
1471 case MESSAGE_EXTERNAL:
1472 {
1473 int exresult;
1474 struct exbody *e;
1475 CT p;
1476 FILE *fp;
1477
1478 NEW0(e);
1479 ct->c_ctparams = (void *) e;
1480
1481 if (!ct->c_fp
1482 && (ct->c_fp = fopen (ct->c_file, "r")) == NULL) {
1483 advise (ct->c_file, "unable to open for reading");
1484 return NOTOK;
1485 }
1486
1487 fseek (fp = ct->c_fp, ct->c_begin, SEEK_SET);
1488
1489 if (!(p = get_content (fp, ct->c_file, 0))) {
1490 ct->c_fp = NULL;
1491 return NOTOK;
1492 }
1493
1494 e->eb_parent = ct;
1495 e->eb_content = p;
1496 p->c_ctexbody = e;
1497 p->c_ceopenfnx = NULL;
1498 if ((exresult = params_external (ct, 0)) != NOTOK
1499 && p->c_ceopenfnx == openMail) {
1500 int cc, size;
1501 char *bp;
1502
1503 if ((size = ct->c_end - p->c_begin) <= 0) {
1504 if (!e->eb_subject)
1505 content_error (NULL, ct,
1506 "empty body for access-type=mail-server");
1507 goto no_body;
1508 }
1509
1510 e->eb_body = bp = mh_xmalloc ((unsigned) size);
1511 fseek (p->c_fp, p->c_begin, SEEK_SET);
1512 while (size > 0)
1513 switch (cc = fread (bp, sizeof(*bp), size, p->c_fp)) {
1514 case NOTOK:
1515 adios ("failed", "fread");
1516
1517 case OK:
1518 adios (NULL, "unexpected EOF from fread");
1519
1520 default:
1521 bp += cc, size -= cc;
1522 break;
1523 }
1524 *bp = 0;
1525 }
1526 no_body:
1527 p->c_fp = NULL;
1528 p->c_end = p->c_begin;
1529
1530 fclose (ct->c_fp);
1531 ct->c_fp = NULL;
1532
1533 if (exresult == NOTOK)
1534 return NOTOK;
1535 if (e->eb_flags == NOTOK)
1536 return OK;
1537
1538 switch (p->c_type) {
1539 case CT_MULTIPART:
1540 break;
1541
1542 case CT_MESSAGE:
1543 if (p->c_subtype != MESSAGE_RFC822)
1544 break;
1545 /* FALLTHRU */
1546 default:
1547 e->eb_partno = ct->c_partno;
1548 if (p->c_ctinitfnx)
1549 (*p->c_ctinitfnx) (p);
1550 break;
1551 }
1552 }
1553 break;
1554
1555 default:
1556 break;
1557 }
1558
1559 return OK;
1560 }
1561
1562
1563 int
1564 params_external (CT ct, int composing)
1565 {
1566 PM pm;
1567 struct exbody *e = (struct exbody *) ct->c_ctparams;
1568 CI ci = &ct->c_ctinfo;
1569
1570 ct->c_ceopenfnx = NULL;
1571 for (pm = ci->ci_first_pm; pm; pm = pm->pm_next) {
1572 if (!strcasecmp (pm->pm_name, "access-type")) {
1573 struct str2init *s2i;
1574 CT p = e->eb_content;
1575
1576 for (s2i = str2methods; s2i->si_key; s2i++)
1577 if (!strcasecmp (pm->pm_value, s2i->si_key))
1578 break;
1579 if (!s2i->si_key) {
1580 e->eb_access = pm->pm_value;
1581 e->eb_flags = NOTOK;
1582 p->c_encoding = CE_EXTERNAL;
1583 continue;
1584 }
1585 e->eb_access = s2i->si_key;
1586 e->eb_flags = s2i->si_val;
1587 p->c_encoding = CE_EXTERNAL;
1588
1589 /* Call the Init function for this external type */
1590 if ((*s2i->si_init)(p) == NOTOK)
1591 return NOTOK;
1592 continue;
1593 }
1594 if (!strcasecmp (pm->pm_name, "name")) {
1595 e->eb_name = pm->pm_value;
1596 continue;
1597 }
1598 if (!strcasecmp (pm->pm_name, "permission")) {
1599 e->eb_permission = pm->pm_value;
1600 continue;
1601 }
1602 if (!strcasecmp (pm->pm_name, "site")) {
1603 e->eb_site = pm->pm_value;
1604 continue;
1605 }
1606 if (!strcasecmp (pm->pm_name, "directory")) {
1607 e->eb_dir = pm->pm_value;
1608 continue;
1609 }
1610 if (!strcasecmp (pm->pm_name, "mode")) {
1611 e->eb_mode = pm->pm_value;
1612 continue;
1613 }
1614 if (!strcasecmp (pm->pm_name, "size")) {
1615 sscanf (pm->pm_value, "%lu", &e->eb_size);
1616 continue;
1617 }
1618 if (!strcasecmp (pm->pm_name, "server")) {
1619 e->eb_server = pm->pm_value;
1620 continue;
1621 }
1622 if (!strcasecmp (pm->pm_name, "subject")) {
1623 e->eb_subject = pm->pm_value;
1624 continue;
1625 }
1626 if (!strcasecmp (pm->pm_name, "url")) {
1627 /*
1628 * According to RFC 2017, we have to remove all whitespace from
1629 * the URL
1630 */
1631
1632 char *u, *p = pm->pm_value;
1633 e->eb_url = u = mh_xmalloc(strlen(pm->pm_value) + 1);
1634
1635 for (; *p != '\0'; p++) {
1636 if (! isspace((unsigned char) *p))
1637 *u++ = *p;
1638 }
1639
1640 *u = '\0';
1641 continue;
1642 }
1643 if (composing && !strcasecmp (pm->pm_name, "body")) {
1644 e->eb_body = getcpy (pm->pm_value);
1645 continue;
1646 }
1647 }
1648
1649 if (!e->eb_access) {
1650 inform("invalid parameters for \"%s/%s\" type in message %s's %s field",
1651 ci->ci_type, ci->ci_subtype, ct->c_file, TYPE_FIELD);
1652 return NOTOK;
1653 }
1654
1655 return OK;
1656 }
1657
1658
1659 /*
1660 * APPLICATION
1661 */
1662
1663 static int
1664 InitApplication (CT ct)
1665 {
1666 CI ci = &ct->c_ctinfo;
1667
1668 /* match subtype */
1669 ct->c_subtype = ct_str_subtype (CT_APPLICATION, ci->ci_subtype);
1670
1671 return OK;
1672 }
1673
1674
1675 /*
1676 * TRANSFER ENCODINGS
1677 */
1678
1679 static int
1680 init_encoding (CT ct, OpenCEFunc openfnx)
1681 {
1682 ct->c_ceopenfnx = openfnx;
1683 ct->c_ceclosefnx = close_encoding;
1684 ct->c_cesizefnx = size_encoding;
1685
1686 return OK;
1687 }
1688
1689
1690 void
1691 close_encoding (CT ct)
1692 {
1693 CE ce = &ct->c_cefile;
1694
1695 if (ce->ce_fp) {
1696 fclose (ce->ce_fp);
1697 ce->ce_fp = NULL;
1698 }
1699 }
1700
1701
1702 static unsigned long
1703 size_encoding (CT ct)
1704 {
1705 int fd;
1706 unsigned long size;
1707 char *file;
1708 CE ce = &ct->c_cefile;
1709 struct stat st;
1710
1711 if (ce->ce_fp && fstat (fileno (ce->ce_fp), &st) != NOTOK)
1712 return (long) st.st_size;
1713
1714 if (ce->ce_file) {
1715 if (stat (ce->ce_file, &st) != NOTOK)
1716 return (long) st.st_size;
1717 return 0L;
1718 }
1719
1720 if (ct->c_encoding == CE_EXTERNAL)
1721 return (ct->c_end - ct->c_begin);
1722
1723 file = NULL;
1724 if ((fd = (*ct->c_ceopenfnx) (ct, &file)) == NOTOK)
1725 return (ct->c_end - ct->c_begin);
1726
1727 if (fstat (fd, &st) != NOTOK)
1728 size = (long) st.st_size;
1729 else
1730 size = 0L;
1731
1732 (*ct->c_ceclosefnx) (ct);
1733 return size;
1734 }
1735
1736
1737 /*
1738 * BASE64
1739 */
1740
1741 static int
1742 InitBase64 (CT ct)
1743 {
1744 return init_encoding (ct, openBase64);
1745 }
1746
1747
1748 static int
1749 openBase64 (CT ct, char **file)
1750 {
1751 ssize_t cc, len;
1752 int fd, own_ct_fp = 0;
1753 char *cp, *buffer = NULL;
1754 /* sbeck -- handle suffixes */
1755 CI ci;
1756 CE ce = &ct->c_cefile;
1757 unsigned char *decoded;
1758 size_t decoded_len;
1759 unsigned char digest[16];
1760
1761 if (ce->ce_fp) {
1762 fseek (ce->ce_fp, 0L, SEEK_SET);
1763 goto ready_to_go;
1764 }
1765
1766 if (ce->ce_file) {
1767 if ((ce->ce_fp = fopen (ce->ce_file, "r")) == NULL) {
1768 content_error (ce->ce_file, ct, "unable to fopen for reading");
1769 return NOTOK;
1770 }
1771 goto ready_to_go;
1772 }
1773
1774 if (*file == NULL) {
1775 ce->ce_unlink = 1;
1776 } else {
1777 ce->ce_file = mh_xstrdup(*file);
1778 ce->ce_unlink = 0;
1779 }
1780
1781 /* sbeck@cise.ufl.edu -- handle suffixes */
1782 ci = &ct->c_ctinfo;
1783 if ((cp = context_find_by_type ("suffix", ci->ci_type, ci->ci_subtype))) {
1784 if (ce->ce_unlink) {
1785 /* Create temporary file with filename extension. */
1786 if ((ce->ce_file = m_mktemps(invo_name, cp, NULL, NULL)) == NULL) {
1787 adios(NULL, "unable to create temporary file in %s",
1788 get_temp_dir());
1789 }
1790 } else {
1791 ce->ce_file = add (cp, ce->ce_file);
1792 }
1793 } else if (*file == NULL) {
1794 char *tempfile;
1795 if ((tempfile = m_mktemp2(NULL, invo_name, NULL, NULL)) == NULL) {
1796 adios(NULL, "unable to create temporary file in %s",
1797 get_temp_dir());
1798 }
1799 ce->ce_file = mh_xstrdup(tempfile);
1800 }
1801
1802 if ((ce->ce_fp = fopen (ce->ce_file, "w+")) == NULL) {
1803 content_error (ce->ce_file, ct, "unable to fopen for reading/writing");
1804 return NOTOK;
1805 }
1806
1807 if ((len = ct->c_end - ct->c_begin) < 0)
1808 adios (NULL, "internal error(1)");
1809
1810 buffer = mh_xmalloc (len + 1);
1811
1812 if (! ct->c_fp) {
1813 if ((ct->c_fp = fopen (ct->c_file, "r")) == NULL) {
1814 content_error (ct->c_file, ct, "unable to open for reading");
1815 return NOTOK;
1816 }
1817 own_ct_fp = 1;
1818 }
1819
1820 lseek (fd = fileno (ct->c_fp), (off_t) ct->c_begin, SEEK_SET);
1821 cp = buffer;
1822 while (len > 0) {
1823 switch (cc = read (fd, cp, len)) {
1824 case NOTOK:
1825 content_error (ct->c_file, ct, "error reading from");
1826 goto clean_up;
1827
1828 case OK:
1829 content_error (NULL, ct, "premature eof");
1830 goto clean_up;
1831
1832 default:
1833 if (cc > len)
1834 cc = len;
1835 len -= cc;
1836 cp += cc;
1837 }
1838 }
1839
1840 /* decodeBase64() requires null-terminated input. */
1841 *cp = '\0';
1842
1843 if (decodeBase64 (buffer, &decoded, &decoded_len, ct->c_type == CT_TEXT,
1844 ct->c_digested ? digest : NULL) != OK)
1845 goto clean_up;
1846
1847 {
1848 size_t i;
1849 unsigned char *decoded_p = decoded;
1850 for (i = 0; i < decoded_len; ++i) {
1851 putc (*decoded_p++, ce->ce_fp);
1852 }
1853 free(decoded);
1854 if (ferror (ce->ce_fp)) {
1855 content_error (ce->ce_file, ct, "error writing to");
1856 goto clean_up;
1857 }
1858
1859 if (ct->c_digested) {
1860 if (memcmp(digest, ct->c_digest,
1861 sizeof digest)) {
1862 content_error (NULL, ct,
1863 "content integrity suspect (digest mismatch) -- continuing");
1864 } else {
1865 if (debugsw) {
1866 fprintf (stderr, "content integrity confirmed\n");
1867 }
1868 }
1869 }
1870 }
1871
1872 fseek (ct->c_fp, 0L, SEEK_SET);
1873
1874 if (fflush (ce->ce_fp)) {
1875 content_error (ce->ce_file, ct, "error writing to");
1876 goto clean_up;
1877 }
1878
1879 fseek (ce->ce_fp, 0L, SEEK_SET);
1880
1881 ready_to_go:
1882 *file = ce->ce_file;
1883 if (own_ct_fp) {
1884 fclose (ct->c_fp);
1885 ct->c_fp = NULL;
1886 }
1887 free (buffer);
1888 return fileno (ce->ce_fp);
1889
1890 clean_up:
1891 if (own_ct_fp) {
1892 fclose (ct->c_fp);
1893 ct->c_fp = NULL;
1894 }
1895 free_encoding (ct, 0);
1896 free (buffer);
1897 return NOTOK;
1898 }
1899
1900
1901 /*
1902 * QUOTED PRINTABLE
1903 */
1904
1905 static char hex2nib[0x80] = {
1906 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1907 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1908 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1909 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1910 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1911 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1912 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
1913 0x08, 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1914 0x00, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x00,
1915 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1916 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1917 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1918 0x00, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x00,
1919 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1920 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1921 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
1922 };
1923
1924
1925 static int
1926 InitQuoted (CT ct)
1927 {
1928 return init_encoding (ct, openQuoted);
1929 }
1930
1931
1932 static int
1933 openQuoted (CT ct, char **file)
1934 {
1935 int cc, digested, len, quoted, own_ct_fp = 0;
1936 char *cp, *ep;
1937 char *bufp = NULL;
1938 size_t buflen;
1939 ssize_t gotlen;
1940 unsigned char mask;
1941 CE ce = &ct->c_cefile;
1942 /* sbeck -- handle suffixes */
1943 CI ci;
1944 MD5_CTX mdContext;
1945
1946 if (ce->ce_fp) {
1947 fseek (ce->ce_fp, 0L, SEEK_SET);
1948 goto ready_to_go;
1949 }
1950
1951 if (ce->ce_file) {
1952 if ((ce->ce_fp = fopen (ce->ce_file, "r")) == NULL) {
1953 content_error (ce->ce_file, ct, "unable to fopen for reading");
1954 return NOTOK;
1955 }
1956 goto ready_to_go;
1957 }
1958
1959 if (*file == NULL) {
1960 ce->ce_unlink = 1;
1961 } else {
1962 ce->ce_file = mh_xstrdup(*file);
1963 ce->ce_unlink = 0;
1964 }
1965
1966 /* sbeck@cise.ufl.edu -- handle suffixes */
1967 ci = &ct->c_ctinfo;
1968 if ((cp = context_find_by_type ("suffix", ci->ci_type, ci->ci_subtype))) {
1969 if (ce->ce_unlink) {
1970 /* Create temporary file with filename extension. */
1971 if ((ce->ce_file = m_mktemps(invo_name, cp, NULL, NULL)) == NULL) {
1972 adios(NULL, "unable to create temporary file in %s",
1973 get_temp_dir());
1974 }
1975 } else {
1976 ce->ce_file = add (cp, ce->ce_file);
1977 }
1978 } else if (*file == NULL) {
1979 char *tempfile;
1980 if ((tempfile = m_mktemp2(NULL, invo_name, NULL, NULL)) == NULL) {
1981 adios(NULL, "unable to create temporary file in %s",
1982 get_temp_dir());
1983 }
1984 ce->ce_file = mh_xstrdup(tempfile);
1985 }
1986
1987 if ((ce->ce_fp = fopen (ce->ce_file, "w+")) == NULL) {
1988 content_error (ce->ce_file, ct, "unable to fopen for reading/writing");
1989 return NOTOK;
1990 }
1991
1992 if ((len = ct->c_end - ct->c_begin) < 0)
1993 adios (NULL, "internal error(2)");
1994
1995 if (! ct->c_fp) {
1996 if ((ct->c_fp = fopen (ct->c_file, "r")) == NULL) {
1997 content_error (ct->c_file, ct, "unable to open for reading");
1998 return NOTOK;
1999 }
2000 own_ct_fp = 1;
2001 }
2002
2003 if ((digested = ct->c_digested))
2004 MD5Init (&mdContext);
2005
2006 quoted = 0;
2007 #ifdef lint
2008 mask = 0;
2009 #endif
2010
2011 fseek (ct->c_fp, ct->c_begin, SEEK_SET);
2012 while (len > 0) {
2013 if ((gotlen = getline(&bufp, &buflen, ct->c_fp)) == -1) {
2014 content_error (NULL, ct, "premature eof");
2015 goto clean_up;
2016 }
2017
2018 if ((cc = gotlen) > len)
2019 cc = len;
2020 len -= cc;
2021
2022 for (ep = (cp = bufp) + cc - 1; cp <= ep; ep--)
2023 if (!isspace ((unsigned char) *ep))
2024 break;
2025 *++ep = '\n';
2026 ep++;
2027
2028 for (; cp < ep; cp++) {
2029 if (quoted > 0) {
2030 /* in an escape sequence */
2031 if (quoted == 1) {
2032 /* at byte 1 of an escape sequence */
2033 mask = hex2nib[((unsigned char) *cp) & 0x7f];
2034 /* next is byte 2 */
2035 quoted = 2;
2036 } else {
2037 /* at byte 2 of an escape sequence */
2038 mask <<= 4;
2039 mask |= hex2nib[((unsigned char) *cp) & 0x7f];
2040 putc (mask, ce->ce_fp);
2041 if (digested)
2042 MD5Update (&mdContext, &mask, 1);
2043 if (ferror (ce->ce_fp)) {
2044 content_error (ce->ce_file, ct, "error writing to");
2045 goto clean_up;
2046 }
2047 /* finished escape sequence; next may be literal or a new
2048 * escape sequence */
2049 quoted = 0;
2050 }
2051 /* on to next byte */
2052 continue;
2053 }
2054
2055 /* not in an escape sequence */
2056 if (*cp == '=') {
2057 /* starting an escape sequence, or invalid '='? */
2058 if (cp + 1 < ep && cp[1] == '\n') {
2059 /* "=\n" soft line break, eat the \n */
2060 cp++;
2061 continue;
2062 }
2063 if (cp + 1 >= ep || cp + 2 >= ep) {
2064 /* We don't have 2 bytes left, so this is an invalid
2065 * escape sequence; just show the raw bytes (below). */
2066 } else if (isxdigit ((unsigned char) cp[1]) &&
2067 isxdigit ((unsigned char) cp[2])) {
2068 /* Next 2 bytes are hex digits, making this a valid escape
2069 * sequence; let's decode it (above). */
2070 quoted = 1;
2071 continue;
2072 }
2073 /* One or both of the next 2 is out of range, making this
2074 * an invalid escape sequence; just show the raw bytes
2075 * (below). */
2076 }
2077
2078 /* Just show the raw byte. */
2079 putc (*cp, ce->ce_fp);
2080 if (digested) {
2081 if (*cp == '\n') {
2082 MD5Update (&mdContext, (unsigned char *) "\r\n",2);
2083 } else {
2084 MD5Update (&mdContext, (unsigned char *) cp, 1);
2085 }
2086 }
2087 if (ferror (ce->ce_fp)) {
2088 content_error (ce->ce_file, ct, "error writing to");
2089 goto clean_up;
2090 }
2091 }
2092 }
2093 if (quoted) {
2094 content_error (NULL, ct,
2095 "invalid QUOTED-PRINTABLE encoding -- end-of-content while still quoting");
2096 goto clean_up;
2097 }
2098
2099 fseek (ct->c_fp, 0L, SEEK_SET);
2100
2101 if (fflush (ce->ce_fp)) {
2102 content_error (ce->ce_file, ct, "error writing to");
2103 goto clean_up;
2104 }
2105
2106 if (digested) {
2107 unsigned char digest[16];
2108
2109 MD5Final (digest, &mdContext);
2110 if (memcmp((char *) digest, (char *) ct->c_digest,
2111 sizeof digest))
2112 content_error (NULL, ct,
2113 "content integrity suspect (digest mismatch) -- continuing");
2114 else if (debugsw)
2115 fprintf (stderr, "content integrity confirmed\n");
2116 }
2117
2118 fseek (ce->ce_fp, 0L, SEEK_SET);
2119
2120 ready_to_go:
2121 *file = ce->ce_file;
2122 if (own_ct_fp) {
2123 fclose (ct->c_fp);
2124 ct->c_fp = NULL;
2125 }
2126 free (bufp);
2127 return fileno (ce->ce_fp);
2128
2129 clean_up:
2130 free_encoding (ct, 0);
2131 if (own_ct_fp) {
2132 fclose (ct->c_fp);
2133 ct->c_fp = NULL;
2134 }
2135 free (bufp);
2136 return NOTOK;
2137 }
2138
2139
2140 /*
2141 * 7BIT
2142 */
2143
2144 static int
2145 Init7Bit (CT ct)
2146 {
2147 if (init_encoding (ct, open7Bit) == NOTOK)
2148 return NOTOK;
2149
2150 ct->c_cesizefnx = NULL; /* no need to decode for real size */
2151 return OK;
2152 }
2153
2154
2155 int
2156 open7Bit (CT ct, char **file)
2157 {
2158 int cc, fd, len, own_ct_fp = 0;
2159 char buffer[BUFSIZ];
2160 /* sbeck -- handle suffixes */
2161 char *cp;
2162 CI ci;
2163 CE ce = &ct->c_cefile;
2164
2165 if (ce->ce_fp) {
2166 fseek (ce->ce_fp, 0L, SEEK_SET);
2167 goto ready_to_go;
2168 }
2169
2170 if (ce->ce_file) {
2171 if ((ce->ce_fp = fopen (ce->ce_file, "r")) == NULL) {
2172 content_error (ce->ce_file, ct, "unable to fopen for reading");
2173 return NOTOK;
2174 }
2175 goto ready_to_go;
2176 }
2177
2178 if (*file == NULL) {
2179 ce->ce_unlink = 1;
2180 } else {
2181 ce->ce_file = mh_xstrdup(*file);
2182 ce->ce_unlink = 0;
2183 }
2184
2185 /* sbeck@cise.ufl.edu -- handle suffixes */
2186 ci = &ct->c_ctinfo;
2187 if ((cp = context_find_by_type ("suffix", ci->ci_type, ci->ci_subtype))) {
2188 if (ce->ce_unlink) {
2189 /* Create temporary file with filename extension. */
2190 if ((ce->ce_file = m_mktemps(invo_name, cp, NULL, NULL)) == NULL) {
2191 adios(NULL, "unable to create temporary file in %s",
2192 get_temp_dir());
2193 }
2194 } else {
2195 ce->ce_file = add (cp, ce->ce_file);
2196 }
2197 } else if (*file == NULL) {
2198 char *tempfile;
2199 if ((tempfile = m_mktemp2(NULL, invo_name, NULL, NULL)) == NULL) {
2200 adios(NULL, "unable to create temporary file in %s",
2201 get_temp_dir());
2202 }
2203 ce->ce_file = mh_xstrdup(tempfile);
2204 }
2205
2206 if ((ce->ce_fp = fopen (ce->ce_file, "w+")) == NULL) {
2207 content_error (ce->ce_file, ct, "unable to fopen for reading/writing");
2208 return NOTOK;
2209 }
2210
2211 if (ct->c_type == CT_MULTIPART) {
2212 CI ci = &ct->c_ctinfo;
2213 char *buffer;
2214
2215 len = 0;
2216 fprintf (ce->ce_fp, "%s: %s/%s", TYPE_FIELD, ci->ci_type, ci->ci_subtype);
2217 len += strlen (TYPE_FIELD) + 2 + strlen (ci->ci_type)
2218 + 1 + strlen (ci->ci_subtype);
2219 buffer = output_params(len, ci->ci_first_pm, &len, 0);
2220
2221 if (buffer) {
2222 fputs (buffer, ce->ce_fp);
2223 free(buffer);
2224 }
2225
2226 if (ci->ci_comment) {
2227 if (len + 1 + (cc = 2 + strlen (ci->ci_comment)) >= CPERLIN) {
2228 fputs ("\n\t", ce->ce_fp);
2229 len = 8;
2230 }
2231 else {
2232 putc (' ', ce->ce_fp);
2233 len++;
2234 }
2235 fprintf (ce->ce_fp, "(%s)", ci->ci_comment);
2236 len += cc;
2237 }
2238 fprintf (ce->ce_fp, "\n");
2239 if (ct->c_id)
2240 fprintf (ce->ce_fp, "%s:%s", ID_FIELD, ct->c_id);
2241 if (ct->c_descr)
2242 fprintf (ce->ce_fp, "%s:%s", DESCR_FIELD, ct->c_descr);
2243 if (ct->c_dispo)
2244 fprintf (ce->ce_fp, "%s:%s", DISPO_FIELD, ct->c_dispo);
2245 fprintf (ce->ce_fp, "\n");
2246 }
2247
2248 if ((len = ct->c_end - ct->c_begin) < 0)
2249 adios (NULL, "internal error(3)");
2250
2251 if (! ct->c_fp) {
2252 if ((ct->c_fp = fopen (ct->c_file, "r")) == NULL) {
2253 content_error (ct->c_file, ct, "unable to open for reading");
2254 return NOTOK;
2255 }
2256 own_ct_fp = 1;
2257 }
2258
2259 lseek (fd = fileno (ct->c_fp), (off_t) ct->c_begin, SEEK_SET);
2260 while (len > 0)
2261 switch (cc = read (fd, buffer, sizeof(buffer) - 1)) {
2262 case NOTOK:
2263 content_error (ct->c_file, ct, "error reading from");
2264 goto clean_up;
2265
2266 case OK:
2267 content_error (NULL, ct, "premature eof");
2268 goto clean_up;
2269
2270 default:
2271 if (cc > len)
2272 cc = len;
2273 len -= cc;
2274
2275 if ((int) fwrite (buffer, sizeof(*buffer), cc, ce->ce_fp) < cc) {
2276 advise ("open7Bit", "fwrite");
2277 }
2278 if (ferror (ce->ce_fp)) {
2279 content_error (ce->ce_file, ct, "error writing to");
2280 goto clean_up;
2281 }
2282 }
2283
2284 fseek (ct->c_fp, 0L, SEEK_SET);
2285
2286 if (fflush (ce->ce_fp)) {
2287 content_error (ce->ce_file, ct, "error writing to");
2288 goto clean_up;
2289 }
2290
2291 fseek (ce->ce_fp, 0L, SEEK_SET);
2292
2293 ready_to_go:
2294 *file = ce->ce_file;
2295 if (own_ct_fp) {
2296 fclose (ct->c_fp);
2297 ct->c_fp = NULL;
2298 }
2299 return fileno (ce->ce_fp);
2300
2301 clean_up:
2302 free_encoding (ct, 0);
2303 if (own_ct_fp) {
2304 fclose (ct->c_fp);
2305 ct->c_fp = NULL;
2306 }
2307 return NOTOK;
2308 }
2309
2310
2311 /*
2312 * External
2313 */
2314
2315 static int
2316 openExternal (CT ct, CT cb, CE ce, char **file, int *fd)
2317 {
2318 char cachefile[BUFSIZ];
2319
2320 if (ce->ce_fp) {
2321 fseek (ce->ce_fp, 0L, SEEK_SET);
2322 goto ready_already;
2323 }
2324
2325 if (ce->ce_file) {
2326 if ((ce->ce_fp = fopen (ce->ce_file, "r")) == NULL) {
2327 content_error (ce->ce_file, ct, "unable to fopen for reading");
2328 return NOTOK;
2329 }
2330 goto ready_already;
2331 }
2332
2333 if (find_cache (ct, rcachesw, (int *) 0, cb->c_id,
2334 cachefile, sizeof(cachefile)) != NOTOK) {
2335 if ((ce->ce_fp = fopen (cachefile, "r"))) {
2336 ce->ce_file = mh_xstrdup(cachefile);
2337 ce->ce_unlink = 0;
2338 goto ready_already;
2339 }
2340 admonish (cachefile, "unable to fopen for reading");
2341 }
2342
2343 *fd = ce->ce_fp ? fileno (ce->ce_fp) : -1;
2344 return OK;
2345
2346 ready_already:
2347 *file = ce->ce_file;
2348 *fd = fileno (ce->ce_fp);
2349 return DONE;
2350 }
2351
2352 /*
2353 * File
2354 */
2355
2356 static int
2357 InitFile (CT ct)
2358 {
2359 return init_encoding (ct, openFile);
2360 }
2361
2362
2363 static int
2364 openFile (CT ct, char **file)
2365 {
2366 int fd, cachetype;
2367 char cachefile[BUFSIZ];
2368 struct exbody *e = ct->c_ctexbody;
2369 CE ce = &ct->c_cefile;
2370
2371 switch (openExternal (e->eb_parent, e->eb_content, ce, file, &fd)) {
2372 case NOTOK:
2373 return NOTOK;
2374
2375 case OK:
2376 break;
2377
2378 case DONE:
2379 return fd;
2380 }
2381
2382 if (!e->eb_name) {
2383 content_error (NULL, ct, "missing name parameter");
2384 return NOTOK;
2385 }
2386
2387 ce->ce_file = mh_xstrdup(e->eb_name);
2388 ce->ce_unlink = 0;
2389
2390 if ((ce->ce_fp = fopen (ce->ce_file, "r")) == NULL) {
2391 content_error (ce->ce_file, ct, "unable to fopen for reading");
2392 return NOTOK;
2393 }
2394
2395 if ((!e->eb_permission || strcasecmp (e->eb_permission, "read-write"))
2396 && find_cache (NULL, wcachesw, &cachetype, e->eb_content->c_id,
2397 cachefile, sizeof(cachefile)) != NOTOK) {
2398 int mask;
2399 FILE *fp;
2400
2401 mask = umask (cachetype ? ~m_gmprot () : 0222);
2402 if ((fp = fopen (cachefile, "w"))) {
2403 int cc;
2404 char buffer[BUFSIZ];
2405 FILE *gp = ce->ce_fp;
2406
2407 fseek (gp, 0L, SEEK_SET);
2408
2409 while ((cc = fread (buffer, sizeof(*buffer), sizeof(buffer), gp))
2410 > 0)
2411 if ((int) fwrite (buffer, sizeof(*buffer), cc, fp) < cc) {
2412 advise ("openFile", "fwrite");
2413 }
2414 fflush (fp);
2415
2416 if (ferror (gp)) {
2417 admonish (ce->ce_file, "error reading");
2418 (void) m_unlink (cachefile);
2419 } else if (ferror (fp)) {
2420 admonish (cachefile, "error writing");
2421 (void) m_unlink (cachefile);
2422 }
2423 fclose (fp);
2424 }
2425 umask (mask);
2426 }
2427
2428 fseek (ce->ce_fp, 0L, SEEK_SET);
2429 *file = ce->ce_file;
2430 return fileno (ce->ce_fp);
2431 }
2432
2433 /*
2434 * FTP
2435 */
2436
2437 static int
2438 InitFTP (CT ct)
2439 {
2440 return init_encoding (ct, openFTP);
2441 }
2442
2443
2444 static int
2445 openFTP (CT ct, char **file)
2446 {
2447 int cachetype, caching, fd;
2448 int len, buflen;
2449 char *bp, *ftp, *user, *pass;
2450 char buffer[BUFSIZ], cachefile[BUFSIZ];
2451 struct exbody *e;
2452 CE ce = &ct->c_cefile;
2453 static char *username = NULL;
2454 static char *password = NULL;
2455
2456 e = ct->c_ctexbody;
2457
2458 if ((ftp = context_find (nmhaccessftp)) && !*ftp)
2459 ftp = NULL;
2460
2461 if (!ftp)
2462 return NOTOK;
2463
2464 switch (openExternal (e->eb_parent, e->eb_content, ce, file, &fd)) {
2465 case NOTOK:
2466 return NOTOK;
2467
2468 case OK:
2469 break;
2470
2471 case DONE:
2472 return fd;
2473 }
2474
2475 if (!e->eb_name || !e->eb_site) {
2476 content_error (NULL, ct, "missing %s parameter",
2477 e->eb_name ? "site": "name");
2478 return NOTOK;
2479 }
2480
2481 /* Get the buffer ready to go */
2482 bp = buffer;
2483 buflen = sizeof(buffer);
2484
2485 /*
2486 * Construct the query message for user
2487 */
2488 snprintf (bp, buflen, "Retrieve %s", e->eb_name);
2489 len = strlen (bp);
2490 bp += len;
2491 buflen -= len;
2492
2493 if (e->eb_partno) {
2494 snprintf (bp, buflen, " (content %s)", e->eb_partno);
2495 len = strlen (bp);
2496 bp += len;
2497 buflen -= len;
2498 }
2499
2500 snprintf (bp, buflen, "\n using %sFTP from site %s",
2501 e->eb_flags ? "anonymous " : "", e->eb_site);
2502 len = strlen (bp);
2503 bp += len;
2504 buflen -= len;
2505
2506 if (e->eb_size > 0) {
2507 snprintf (bp, buflen, " (%lu octets)", e->eb_size);
2508 len = strlen (bp);
2509 bp += len;
2510 buflen -= len;
2511 }
2512 snprintf (bp, buflen, "? ");
2513
2514 /*
2515 * Now, check the answer
2516 */
2517 if (!read_yes_or_no_if_tty (buffer))
2518 return NOTOK;
2519
2520 if (e->eb_flags) {
2521 user = "anonymous";
2522 snprintf (buffer, sizeof(buffer), "%s@%s", getusername (),
2523 LocalName (1));
2524 pass = buffer;
2525 } else {
2526 ruserpass (e->eb_site, &username, &password, 0);
2527 user = username;
2528 pass = password;
2529 }
2530
2531 ce->ce_unlink = (*file == NULL);
2532 caching = 0;
2533 cachefile[0] = '\0';
2534 if ((!e->eb_permission || strcasecmp (e->eb_permission, "read-write"))
2535 && find_cache (NULL, wcachesw, &cachetype, e->eb_content->c_id,
2536 cachefile, sizeof(cachefile)) != NOTOK) {
2537 if (*file == NULL) {
2538 ce->ce_unlink = 0;
2539 caching = 1;
2540 }
2541 }
2542
2543 if (*file)
2544 ce->ce_file = mh_xstrdup(*file);
2545 else if (caching)
2546 ce->ce_file = mh_xstrdup(cachefile);
2547 else {
2548 char *tempfile;
2549 if ((tempfile = m_mktemp2(NULL, invo_name, NULL, NULL)) == NULL) {
2550 adios(NULL, "unable to create temporary file in %s",
2551 get_temp_dir());
2552 }
2553 ce->ce_file = mh_xstrdup(tempfile);
2554 }
2555
2556 if ((ce->ce_fp = fopen (ce->ce_file, "w+")) == NULL) {
2557 content_error (ce->ce_file, ct, "unable to fopen for reading/writing");
2558 return NOTOK;
2559 }
2560
2561 {
2562 int child_id, i, vecp;
2563 char *vec[9];
2564
2565 vecp = 0;
2566 vec[vecp++] = r1bindex (ftp, '/');
2567 vec[vecp++] = e->eb_site;
2568 vec[vecp++] = user;
2569 vec[vecp++] = pass;
2570 vec[vecp++] = e->eb_dir;
2571 vec[vecp++] = e->eb_name;
2572 vec[vecp++] = ce->ce_file,
2573 vec[vecp++] = e->eb_mode && !strcasecmp (e->eb_mode, "ascii")
2574 ? "ascii" : "binary";
2575 vec[vecp] = NULL;
2576
2577 fflush (stdout);
2578
2579 for (i = 0; (child_id = fork()) == NOTOK && i < 5; i++)
2580 sleep (5);
2581 switch (child_id) {
2582 case NOTOK:
2583 adios ("fork", "unable to");
2584 /* NOTREACHED */
2585
2586 case OK:
2587 close (fileno (ce->ce_fp));
2588 execvp (ftp, vec);
2589 fprintf (stderr, "unable to exec ");
2590 perror (ftp);
2591 _exit (-1);
2592 /* NOTREACHED */
2593
2594 default:
2595 if (pidXwait (child_id, NULL)) {
2596 username = password = NULL;
2597 ce->ce_unlink = 1;
2598 return NOTOK;
2599 }
2600 break;
2601 }
2602 }
2603
2604 if (cachefile[0]) {
2605 if (caching)
2606 chmod (cachefile, cachetype ? m_gmprot () : 0444);
2607 else {
2608 int mask;
2609 FILE *fp;
2610
2611 mask = umask (cachetype ? ~m_gmprot () : 0222);
2612 if ((fp = fopen (cachefile, "w"))) {
2613 int cc;
2614 FILE *gp = ce->ce_fp;
2615
2616 fseek (gp, 0L, SEEK_SET);
2617
2618 while ((cc= fread (buffer, sizeof(*buffer), sizeof(buffer), gp))
2619 > 0)
2620 if ((int) fwrite (buffer, sizeof(*buffer), cc, fp) < cc) {
2621 advise ("openFTP", "fwrite");
2622 }
2623 fflush (fp);
2624
2625 if (ferror (gp)) {
2626 admonish (ce->ce_file, "error reading");
2627 (void) m_unlink (cachefile);
2628 } else if (ferror (fp)) {
2629 admonish (cachefile, "error writing");
2630 (void) m_unlink (cachefile);
2631 }
2632 fclose (fp);
2633 }
2634 umask (mask);
2635 }
2636 }
2637
2638 fseek (ce->ce_fp, 0L, SEEK_SET);
2639 *file = ce->ce_file;
2640 return fileno (ce->ce_fp);
2641 }
2642
2643
2644 /*
2645 * Mail
2646 */
2647
2648 static int
2649 InitMail (CT ct)
2650 {
2651 return init_encoding (ct, openMail);
2652 }
2653
2654
2655 static int
2656 openMail (CT ct, char **file)
2657 {
2658 int child_id, fd, i, vecp;
2659 int len, buflen;
2660 char *bp, buffer[BUFSIZ], *vec[7];
2661 struct exbody *e = ct->c_ctexbody;
2662 CE ce = &ct->c_cefile;
2663
2664 switch (openExternal (e->eb_parent, e->eb_content, ce, file, &fd)) {
2665 case NOTOK:
2666 return NOTOK;
2667
2668 case OK:
2669 break;
2670
2671 case DONE:
2672 return fd;
2673 }
2674
2675 if (!e->eb_server) {
2676 content_error (NULL, ct, "missing server parameter");
2677 return NOTOK;
2678 }
2679
2680 /* Get buffer ready to go */
2681 bp = buffer;
2682 buflen = sizeof(buffer);
2683
2684 /* Now, construct query message */
2685 snprintf (bp, buflen, "Retrieve content");
2686 len = strlen (bp);
2687 bp += len;
2688 buflen -= len;
2689
2690 if (e->eb_partno) {
2691 snprintf (bp, buflen, " %s", e->eb_partno);
2692 len = strlen (bp);
2693 bp += len;
2694 buflen -= len;
2695 }
2696
2697 snprintf (bp, buflen, " by asking %s\n\n%s\n? ",
2698 e->eb_server,
2699 e->eb_subject ? e->eb_subject : e->eb_body);
2700
2701 /* Now, check answer */
2702 if (!read_yes_or_no_if_tty (buffer))
2703 return NOTOK;
2704
2705 vecp = 0;
2706 vec[vecp++] = r1bindex (mailproc, '/');
2707 vec[vecp++] = e->eb_server;
2708 vec[vecp++] = "-subject";
2709 vec[vecp++] = e->eb_subject ? e->eb_subject : "mail-server request";
2710 vec[vecp++] = "-body";
2711 vec[vecp++] = e->eb_body;
2712 vec[vecp] = NULL;
2713
2714 for (i = 0; (child_id = fork()) == NOTOK && i < 5; i++)
2715 sleep (5);
2716 switch (child_id) {
2717 case NOTOK:
2718 advise ("fork", "unable to");
2719 return NOTOK;
2720
2721 case OK:
2722 execvp (mailproc, vec);
2723 fprintf (stderr, "unable to exec ");
2724 perror (mailproc);
2725 _exit (-1);
2726 /* NOTREACHED */
2727
2728 default:
2729 if (pidXwait (child_id, NULL) == OK)
2730 inform("request sent");
2731 break;
2732 }
2733
2734 if (*file == NULL) {
2735 char *tempfile;
2736 if ((tempfile = m_mktemp2(NULL, invo_name, NULL, NULL)) == NULL) {
2737 adios(NULL, "unable to create temporary file in %s",
2738 get_temp_dir());
2739 }
2740 ce->ce_file = mh_xstrdup(tempfile);
2741 ce->ce_unlink = 1;
2742 } else {
2743 ce->ce_file = mh_xstrdup(*file);
2744 ce->ce_unlink = 0;
2745 }
2746
2747 if ((ce->ce_fp = fopen (ce->ce_file, "w+")) == NULL) {
2748 content_error (ce->ce_file, ct, "unable to fopen for reading/writing");
2749 return NOTOK;
2750 }
2751
2752 /* showproc is for mhshow and mhstore, though mhlist -debug
2753 * prints it, too. */
2754 mh_xfree(ct->c_showproc);
2755 ct->c_showproc = mh_xstrdup("true");
2756
2757 fseek (ce->ce_fp, 0L, SEEK_SET);
2758 *file = ce->ce_file;
2759 return fileno (ce->ce_fp);
2760 }
2761
2762
2763 /*
2764 * URL
2765 */
2766
2767 static int
2768 InitURL (CT ct)
2769 {
2770 return init_encoding (ct, openURL);
2771 }
2772
2773
2774 static int
2775 openURL (CT ct, char **file)
2776 {
2777 struct exbody *e = ct->c_ctexbody;
2778 CE ce = &ct->c_cefile;
2779 char *urlprog, *program;
2780 char buffer[BUFSIZ], cachefile[BUFSIZ];
2781 int fd, caching, cachetype;
2782 struct msgs_array args = { 0, 0, NULL};
2783 pid_t child_id;
2784
2785 if ((urlprog = context_find(nmhaccessurl)) && *urlprog == '\0')
2786 urlprog = NULL;
2787
2788 if (! urlprog) {
2789 content_error(NULL, ct, "No entry for nmh-access-url in profile");
2790 return NOTOK;
2791 }
2792
2793 switch (openExternal(e->eb_parent, e->eb_content, ce, file, &fd)) {
2794 case NOTOK:
2795 return NOTOK;
2796
2797 case OK:
2798 break;
2799
2800 case DONE:
2801 return fd;
2802 }
2803
2804 if (!e->eb_url) {
2805 content_error(NULL, ct, "missing url parameter");
2806 return NOTOK;
2807 }
2808
2809 ce->ce_unlink = (*file == NULL);
2810 caching = 0;
2811 cachefile[0] = '\0';
2812
2813 if (find_cache(NULL, wcachesw, &cachetype, e->eb_content->c_id,
2814 cachefile, sizeof(cachefile)) != NOTOK) {
2815 if (*file == NULL) {
2816 ce->ce_unlink = 0;
2817 caching = 1;
2818 }
2819 }
2820
2821 if (*file)
2822 ce->ce_file = mh_xstrdup(*file);
2823 else if (caching)
2824 ce->ce_file = mh_xstrdup(cachefile);
2825 else {
2826 char *tempfile;
2827 if ((tempfile = m_mktemp2(NULL, invo_name, NULL, NULL)) == NULL) {
2828 adios(NULL, "unable to create temporary file in %s",
2829 get_temp_dir());
2830 }
2831 ce->ce_file = mh_xstrdup(tempfile);
2832 }
2833
2834 if ((ce->ce_fp = fopen(ce->ce_file, "w+")) == NULL) {
2835 content_error(ce->ce_file, ct, "unable to fopen for read/writing");
2836 return NOTOK;
2837 }
2838
2839 switch (child_id = fork()) {
2840 case NOTOK:
2841 adios ("fork", "unable to");
2842 /* NOTREACHED */
2843
2844 case OK:
2845 argsplit_msgarg(&args, urlprog, &program);
2846 app_msgarg(&args, e->eb_url);
2847 app_msgarg(&args, NULL);
2848 dup2(fileno(ce->ce_fp), 1);
2849 close(fileno(ce->ce_fp));
2850 execvp(program, args.msgs);
2851 fprintf(stderr, "Unable to exec ");
2852 perror(program);
2853 _exit(-1);
2854 /* NOTREACHED */
2855
2856 default:
2857 if (pidXwait(child_id, NULL)) {
2858 ce->ce_unlink = 1;
2859 return NOTOK;
2860 }
2861 }
2862
2863 if (cachefile[0]) {
2864 if (caching)
2865 chmod(cachefile, cachetype ? m_gmprot() : 0444);
2866 else {
2867 int mask;
2868 FILE *fp;
2869
2870 mask = umask (cachetype ? ~m_gmprot() : 0222);
2871 if ((fp = fopen(cachefile, "w"))) {
2872 int cc;
2873 FILE *gp = ce->ce_fp;
2874
2875 fseeko(gp, 0, SEEK_SET);
2876
2877 while ((cc = fread(buffer, sizeof(*buffer),
2878 sizeof(buffer), gp)) > 0)
2879 if ((int) fwrite(buffer, sizeof(*buffer), cc, fp) < cc) {
2880 advise ("openURL", "fwrite");
2881 }
2882
2883 fflush(fp);
2884
2885 if (ferror(gp)) {
2886 admonish(ce->ce_file, "error reading");
2887 (void) m_unlink (cachefile);
2888 }
2889 }
2890 umask(mask);
2891 }
2892 }
2893
2894 fseeko(ce->ce_fp, 0, SEEK_SET);
2895 *file = ce->ce_file;
2896 return fileno(ce->ce_fp);
2897 }
2898
2899
2900 /*
2901 * Stores MD5 digest (in cp, from Content-MD5 header) in ct->c_digest. It
2902 * has to be base64 decoded.
2903 */
2904 static int
2905 readDigest (CT ct, char *cp)
2906 {
2907 unsigned char *digest;
2908
2909 size_t len;
2910 if (decodeBase64 (cp, &digest, &len, 0, NULL) == OK) {
2911 const size_t maxlen = sizeof ct->c_digest;
2912
2913 if (strlen ((char *) digest) <= maxlen) {
2914 memcpy (ct->c_digest, digest, maxlen);
2915
2916 if (debugsw) {
2917 size_t i;
2918
2919 fprintf (stderr, "MD5 digest=");
2920 for (i = 0; i < maxlen; ++i) {
2921 fprintf (stderr, "%02x", ct->c_digest[i] & 0xff);
2922 }
2923 fprintf (stderr, "\n");
2924 }
2925
2926 return OK;
2927 }
2928 if (debugsw) {
2929 fprintf (stderr, "invalid MD5 digest (got %d octets)\n",
2930 (int) strlen ((char *) digest));
2931 }
2932
2933 return NOTOK;
2934 }
2935
2936 return NOTOK;
2937 }
2938
2939
2940 /* Multipart parts might have content before the first subpart and/or
2941 after the last subpart that hasn't been stored anywhere else, so do
2942 that. */
2943 int
2944 get_leftover_mp_content (CT ct, int before /* or after */)
2945 {
2946 struct multipart *m = (struct multipart *) ct->c_ctparams;
2947 char *boundary;
2948 int found_boundary = 0;
2949 int max = BUFSIZ;
2950 char *bufp = NULL;
2951 size_t buflen;
2952 ssize_t gotlen;
2953 int read = 0;
2954 char *content = NULL;
2955
2956 if (! m) return NOTOK;
2957
2958 if (before) {
2959 if (! m->mp_parts || ! m->mp_parts->mp_part) return NOTOK;
2960
2961 /* Isolate the beginning of this part to the beginning of the
2962 first subpart and save any content between them. */
2963 fseeko (ct->c_fp, ct->c_begin, SEEK_SET);
2964 max = m->mp_parts->mp_part->c_begin - ct->c_begin;
2965 boundary = concat ("--", m->mp_start, NULL);
2966 } else {
2967 struct part *last_subpart = NULL;
2968 struct part *subpart;
2969
2970 /* Go to the last subpart to get its end position. */
2971 for (subpart = m->mp_parts; subpart; subpart = subpart->mp_next) {
2972 last_subpart = subpart;
2973 }
2974
2975 if (last_subpart == NULL) return NOTOK;
2976
2977 /* Isolate the end of the last subpart to the end of this part
2978 and save any content between them. */
2979 fseeko (ct->c_fp, last_subpart->mp_part->c_end, SEEK_SET);
2980 max = ct->c_end - last_subpart->mp_part->c_end;
2981 boundary = concat ("--", m->mp_stop, NULL);
2982 }
2983
2984 /* Back up by 1 to pick up the newline. */
2985 while ((gotlen = getline(&bufp, &buflen, ct->c_fp)) != -1) {
2986 read += gotlen;
2987 /* Don't look beyond beginning of first subpart (before) or
2988 next part (after). */
2989 if (read > max) bufp[read-max] = '\0';
2990
2991 if (before) {
2992 if (! strcmp (bufp, boundary)) {
2993 found_boundary = 1;
2994 }
2995 } else {
2996 if (! found_boundary && ! strcmp (bufp, boundary)) {
2997 found_boundary = 1;
2998 continue;
2999 }
3000 }
3001
3002 if ((before && ! found_boundary) || (! before && found_boundary)) {
3003 if (content) {
3004 char *old_content = content;
3005 content = concat (content, bufp, NULL);
3006 free (old_content);
3007 } else {
3008 content = before
3009 ? concat ("\n", bufp, NULL)
3010 : concat (bufp, NULL);
3011 }
3012 }
3013
3014 if (before) {
3015 if (found_boundary || read > max) break;
3016 } else {
3017 if (read > max) break;
3018 }
3019 }
3020
3021 /* Skip the newline if that's all there is. */
3022 if (content) {
3023 char *cp;
3024
3025 /* Remove trailing newline, except at EOF. */
3026 if ((before || ! feof (ct->c_fp)) &&
3027 (cp = content + strlen (content)) > content &&
3028 *--cp == '\n') {
3029 *cp = '\0';
3030 }
3031
3032 if (strlen (content) > 1) {
3033 if (before) {
3034 m->mp_content_before = content;
3035 } else {
3036 m->mp_content_after = content;
3037 }
3038 } else {
3039 free (content);
3040 }
3041 }
3042
3043 free (boundary);
3044 free (bufp);
3045
3046 return OK;
3047 }
3048
3049
3050 char *
3051 ct_type_str (int type) {
3052 switch (type) {
3053 case CT_APPLICATION:
3054 return "application";
3055 case CT_AUDIO:
3056 return "audio";
3057 case CT_IMAGE:
3058 return "image";
3059 case CT_MESSAGE:
3060 return "message";
3061 case CT_MULTIPART:
3062 return "multipart";
3063 case CT_TEXT:
3064 return "text";
3065 case CT_VIDEO:
3066 return "video";
3067 case CT_EXTENSION:
3068 return "extension";
3069 default:
3070 return "unknown_type";
3071 }
3072 }
3073
3074
3075 char *
3076 ct_subtype_str (int type, int subtype) {
3077 switch (type) {
3078 case CT_APPLICATION:
3079 switch (subtype) {
3080 case APPLICATION_OCTETS:
3081 return "octets";
3082 case APPLICATION_POSTSCRIPT:
3083 return "postscript";
3084 default:
3085 return "unknown_app_subtype";
3086 }
3087 case CT_MESSAGE:
3088 switch (subtype) {
3089 case MESSAGE_RFC822:
3090 return "rfc822";
3091 case MESSAGE_PARTIAL:
3092 return "partial";
3093 case MESSAGE_EXTERNAL:
3094 return "external";
3095 default:
3096 return "unknown_msg_subtype";
3097 }
3098 case CT_MULTIPART:
3099 switch (subtype) {
3100 case MULTI_MIXED:
3101 return "mixed";
3102 case MULTI_ALTERNATE:
3103 return "alternative";
3104 case MULTI_DIGEST:
3105 return "digest";
3106 case MULTI_PARALLEL:
3107 return "parallel";
3108 case MULTI_RELATED:
3109 return "related";
3110 default:
3111 return "unknown_multipart_subtype";
3112 }
3113 case CT_TEXT:
3114 switch (subtype) {
3115 case TEXT_PLAIN:
3116 return "plain";
3117 case TEXT_RICHTEXT:
3118 return "richtext";
3119 case TEXT_ENRICHED:
3120 return "enriched";
3121 default:
3122 return "unknown_text_subtype";
3123 }
3124 default:
3125 return "unknown_type";
3126 }
3127 }
3128
3129
3130 int
3131 ct_str_type (const char *type) {
3132 struct str2init *s2i;
3133
3134 for (s2i = str2cts; s2i->si_key; ++s2i) {
3135 if (! strcasecmp (type, s2i->si_key)) {
3136 break;
3137 }
3138 }
3139 if (! s2i->si_key && ! uprf (type, "X-")) {
3140 ++s2i;
3141 }
3142
3143 return s2i->si_val;
3144 }
3145
3146
3147 int
3148 ct_str_subtype (int type, const char *subtype) {
3149 struct k2v *kv;
3150
3151 switch (type) {
3152 case CT_APPLICATION:
3153 for (kv = SubApplication; kv->kv_key; ++kv) {
3154 if (! strcasecmp (subtype, kv->kv_key)) {
3155 break;
3156 }
3157 }
3158 return kv->kv_value;
3159 case CT_MESSAGE:
3160 for (kv = SubMessage; kv->kv_key; ++kv) {
3161 if (! strcasecmp (subtype, kv->kv_key)) {
3162 break;
3163 }
3164 }
3165 return kv->kv_value;
3166 case CT_MULTIPART:
3167 for (kv = SubMultiPart; kv->kv_key; ++kv) {
3168 if (! strcasecmp (subtype, kv->kv_key)) {
3169 break;
3170 }
3171 }
3172 return kv->kv_value;
3173 case CT_TEXT:
3174 for (kv = SubText; kv->kv_key; ++kv) {
3175 if (! strcasecmp (subtype, kv->kv_key)) {
3176 break;
3177 }
3178 }
3179 return kv->kv_value;
3180 default:
3181 return 0;
3182 }
3183 }
3184
3185
3186 /* Find the content type and InitFunc for the CT. */
3187 const struct str2init *
3188 get_ct_init (int type) {
3189 const struct str2init *sp;
3190
3191 for (sp = str2cts; sp->si_key; ++sp) {
3192 if (type == sp->si_val) {
3193 return sp;
3194 }
3195 }
3196
3197 return NULL;
3198 }
3199
3200 const char *
3201 ce_str (int encoding) {
3202 switch (encoding) {
3203 case CE_BASE64:
3204 return "base64";
3205 case CE_QUOTED:
3206 return "quoted-printable";
3207 case CE_8BIT:
3208 return "8bit";
3209 case CE_7BIT:
3210 return "7bit";
3211 case CE_BINARY:
3212 return "binary";
3213 case CE_EXTENSION:
3214 return "extension";
3215 case CE_EXTERNAL:
3216 return "external";
3217 default:
3218 return "unknown";
3219 }
3220 }
3221
3222 /* Find the content type and InitFunc for the content encoding method. */
3223 const struct str2init *
3224 get_ce_method (const char *method) {
3225 struct str2init *sp;
3226
3227 for (sp = str2ces; sp->si_key; ++sp) {
3228 if (! strcasecmp (method, sp->si_key)) {
3229 return sp;
3230 }
3231 }
3232
3233 return NULL;
3234 }
3235
3236 /*
3237 * Parse a series of MIME attributes (or parameters) given a header as
3238 * input.
3239 *
3240 * Arguments include:
3241 *
3242 * filename - Name of input file (for error messages)
3243 * fieldname - Name of field being processed
3244 * headerp - Pointer to pointer of the beginning of the MIME attributes.
3245 * Updated to point to end of attributes when finished.
3246 * param_head - Pointer to head of parameter list
3247 * param_tail - Pointer to tail of parameter list
3248 * commentp - Pointer to header comment pointer (may be NULL)
3249 *
3250 * Returns OK if parsing was successful, NOTOK if parsing failed, and
3251 * DONE to indicate a benign error (minor parsing error, but the program
3252 * should continue).
3253 */
3254
3255 static int
3256 parse_header_attrs (const char *filename, const char *fieldname,
3257 char **header_attrp, PM *param_head, PM *param_tail,
3258 char **commentp)
3259 {
3260 char *cp = *header_attrp;
3261 PM pm;
3262 struct sectlist {
3263 char *value;
3264 int index;
3265 int len;
3266 struct sectlist *next;
3267 } *sp, *sp2;
3268 struct parmlist {
3269 char *name;
3270 char *charset;
3271 char *lang;
3272 struct sectlist *sechead;
3273 struct parmlist *next;
3274 } *pp, *pp2, *phead = NULL;
3275
3276 while (*cp == ';') {
3277 char *dp, *vp, *up, *nameptr, *valptr, *charset = NULL, *lang = NULL;
3278 int encoded = 0, partial = 0, len = 0, index = 0;
3279
3280 cp++;
3281 while (isspace ((unsigned char) *cp))
3282 cp++;
3283
3284 if (*cp == '(' &&
3285 get_comment (filename, fieldname, &cp, commentp) == NOTOK) {
3286 return NOTOK;
3287 }
3288
3289 if (*cp == 0) {
3290 if (! suppress_extraneous_trailing_semicolon_warning) {
3291 inform("extraneous trailing ';' in message %s's %s: "
3292 "parameter list", filename, fieldname);
3293 }
3294 return DONE;
3295 }
3296
3297 /* down case the attribute name */
3298 for (dp = cp; istoken ((unsigned char) *dp); dp++)
3299 *dp = tolower ((unsigned char) *dp);
3300
3301 for (up = dp; isspace ((unsigned char) *dp);)
3302 dp++;
3303 if (dp == cp || *dp != '=') {
3304 inform("invalid parameter in message %s's %s: field\n"
3305 " parameter %s (error detected at offset %ld)",
3306 filename, fieldname, cp, (long)(dp - cp));
3307 return NOTOK;
3308 }
3309
3310 /*
3311 * To handle RFC 2231, we have to deal with the following extensions:
3312 *
3313 * name*=encoded-value
3314 * name*<N>=part-N-of-a-parameter-value
3315 * name*<N>*=encoded-part-N-of-a-parameter-value
3316 *
3317 * So the rule is:
3318 * If there's a * right before the equal sign, it's encoded.
3319 * If there's a * and one or more digits, then it's section N.
3320 *
3321 * Remember we can have one or the other, or both. cp points to
3322 * beginning of name, up points past the last character in the
3323 * parameter name.
3324 */
3325
3326 for (vp = cp; vp < up; vp++) {
3327 if (*vp == '*' && vp < up - 1) {
3328 partial = 1;
3329 continue;
3330 }
3331 if (*vp == '*' && vp == up - 1) {
3332 encoded = 1;
3333 } else if (partial) {
3334 if (isdigit((unsigned char) *vp))
3335 index = *vp - '0' + index * 10;
3336 else {
3337 inform("invalid parameter index in message %s's %s: field"
3338 "\n (parameter %s)", filename, fieldname, cp);
3339 return NOTOK;
3340 }
3341 } else {
3342 len++;
3343 }
3344 }
3345
3346 /*
3347 * Break out the parameter name and value sections and allocate
3348 * memory for each.
3349 */
3350
3351 nameptr = mh_xmalloc(len + 1);
3352 strncpy(nameptr, cp, len);
3353 nameptr[len] = '\0';
3354
3355 for (dp++; isspace ((unsigned char) *dp);)
3356 dp++;
3357
3358 if (encoded) {
3359 /*
3360 * Single quotes delimit the character set and language tag.
3361 * They are required on the first section (or a complete
3362 * parameter).
3363 */
3364 if (index == 0) {
3365 vp = dp;
3366 while (*vp != '\'' && !isspace((unsigned char) *vp) &&
3367 *vp != '\0')
3368 vp++;
3369 if (*vp == '\'') {
3370 if (vp != dp) {
3371 len = vp - dp;
3372 charset = mh_xmalloc(len + 1);
3373 strncpy(charset, dp, len);
3374 charset[len] = '\0';
3375 } else {
3376 charset = NULL;
3377 }
3378 vp++;
3379 } else {
3380 inform("missing charset in message %s's %s: field\n"
3381 " (parameter %s)", filename, fieldname, nameptr);
3382 free(nameptr);
3383 return NOTOK;
3384 }
3385 dp = vp;
3386
3387 while (*vp != '\'' && !isspace((unsigned char) *vp) &&
3388 *vp != '\0')
3389 vp++;
3390
3391 if (*vp == '\'') {
3392 if (vp != dp) {
3393 len = vp - dp;
3394 lang = mh_xmalloc(len + 1);
3395 strncpy(lang, dp, len);
3396 lang[len] = '\0';
3397 } else {
3398 lang = NULL;
3399 }
3400 vp++;
3401 } else {
3402 inform("missing language tag in message %s's %s: field\n"
3403 " (parameter %s)", filename, fieldname, nameptr);
3404 free(nameptr);
3405 mh_xfree(charset);
3406 return NOTOK;
3407 }
3408
3409 dp = vp;
3410 }
3411
3412 /*
3413 * At this point vp should be pointing at the beginning
3414 * of the encoded value/section. Continue until we reach
3415 * the end or get whitespace. But first, calculate the
3416 * length so we can allocate the correct buffer size.
3417 */
3418
3419 for (vp = dp, len = 0; istoken(*vp); vp++) {
3420 if (*vp == '%') {
3421 if (*(vp + 1) == '\0' ||
3422 !isxdigit((unsigned char) *(vp + 1)) ||
3423 *(vp + 2) == '\0' ||
3424 !isxdigit((unsigned char) *(vp + 2))) {
3425 inform("invalid encoded sequence in message %s's %s: field\n"
3426 " (parameter %s)", filename, fieldname, nameptr);
3427 free(nameptr);
3428 mh_xfree(charset);
3429 mh_xfree(lang);
3430 return NOTOK;
3431 }
3432 vp += 2;
3433 }
3434 len++;
3435 }
3436
3437 up = valptr = mh_xmalloc(len + 1);
3438
3439 for (vp = dp; istoken(*vp); vp++) {
3440 if (*vp == '%') {
3441 *up++ = decode_qp(*(vp + 1), *(vp + 2));
3442 vp += 2;
3443 } else {
3444 *up++ = *vp;
3445 }
3446 }
3447
3448 *up = '\0';
3449 cp = vp;
3450 } else {
3451 /*
3452 * A "normal" string. If it's got a leading quote, then we
3453 * strip the quotes out. Otherwise go until we reach the end
3454 * or get whitespace. Note we scan it twice; once to get the
3455 * length, then the second time copies it into the destination
3456 * buffer.
3457 */
3458
3459 len = 0;
3460
3461 if (*dp == '"') {
3462 for (cp = dp + 1;;) {
3463 switch (*cp++) {
3464 case '\0':
3465 bad_quote:
3466 inform("invalid quoted-string in message %s's %s: field\n"
3467 " (parameter %s)", filename, fieldname, nameptr);
3468 free(nameptr);
3469 mh_xfree(charset);
3470 mh_xfree(lang);
3471 return NOTOK;
3472 case '"':
3473 break;
3474
3475 case '\\':
3476 if (*++cp == '\0')
3477 goto bad_quote;
3478 /* FALLTHRU */
3479 default:
3480 len++;
3481 continue;
3482 }
3483 break;
3484 }
3485
3486 } else {
3487 for (cp = dp; istoken (*cp); cp++) {
3488 len++;
3489 }
3490 }
3491
3492 valptr = mh_xmalloc(len + 1);
3493
3494 if (*dp == '"') {
3495 int i;
3496 for (cp = dp + 1, vp = valptr, i = 0; i < len; i++) {
3497 if (*cp == '\\') {
3498 cp++;
3499 }
3500 *vp++ = *cp++;
3501 }
3502 cp++;
3503 } else {
3504 strncpy(valptr, cp = dp, len);
3505 cp += len;
3506 }
3507
3508 valptr[len] = '\0';
3509 }
3510
3511 /*
3512 * If 'partial' is set, we don't allocate a parameter now. We
3513 * put it on the parameter linked list to be reassembled later.
3514 *
3515 * "phead" points to a list of all parameters we need to reassemble.
3516 * Each parameter has a list of sections. We insert the sections in
3517 * order.
3518 */
3519
3520 if (partial) {
3521 for (pp = phead; pp != NULL; pp = pp->next) {
3522 if (strcasecmp(nameptr, pp->name) == 0) {
3523 free (nameptr);
3524 nameptr = pp->name;
3525 break;
3526 }
3527 }
3528
3529 if (pp == NULL) {
3530 NEW0(pp);
3531 pp->name = nameptr;
3532 pp->next = phead;
3533 phead = pp;
3534 }
3535
3536 /*
3537 * Insert this into the section linked list
3538 */
3539
3540 NEW0(sp);
3541 sp->value = valptr;
3542 sp->index = index;
3543 sp->len = len;
3544
3545 if (pp->sechead == NULL || pp->sechead->index > index) {
3546 sp->next = pp->sechead;
3547 pp->sechead = sp;
3548 } else {
3549 for (sp2 = pp->sechead; sp2 != NULL; sp2 = sp2->next) {
3550 if (sp2->index == sp->index) {
3551 inform("duplicate index (%d) in message %s's %s: field"
3552 "\n (parameter %s)", sp->index, filename,
3553 fieldname, nameptr);
3554 return NOTOK;
3555 }
3556 if (sp2->index < sp->index &&
3557 (sp2->next == NULL || sp2->next->index > sp->index)) {
3558 sp->next = sp2->next;
3559 sp2->next = sp;
3560 break;
3561 }
3562 }
3563
3564 if (sp2 == NULL) {
3565 inform("Internal error: cannot insert partial param "
3566 "in message %s's %s: field\n (parameter %s)",
3567 filename, fieldname, nameptr);
3568 return NOTOK;
3569 }
3570 }
3571
3572 /*
3573 * Save our charset and lang tags.
3574 */
3575
3576 if (index == 0 && encoded) {
3577 mh_xfree(pp->charset);
3578 pp->charset = charset;
3579 mh_xfree(pp->lang);
3580 pp->lang = lang;
3581 }
3582 } else {
3583 pm = add_param(param_head, param_tail, nameptr, valptr, 1);
3584 pm->pm_charset = charset;
3585 pm->pm_lang = lang;
3586 }
3587
3588 while (isspace ((unsigned char) *cp))
3589 cp++;
3590
3591 if (*cp == '(' &&
3592 get_comment (filename, fieldname, &cp, commentp) == NOTOK) {
3593 return NOTOK;
3594 }
3595 }
3596
3597 /*
3598 * Now that we're done, reassemble all of the partial parameters.
3599 */
3600
3601 for (pp = phead; pp != NULL; ) {
3602 char *p, *q;
3603 size_t tlen = 0;
3604 int pindex = 0;
3605 for (sp = pp->sechead; sp != NULL; sp = sp->next) {
3606 if (sp->index != pindex++) {
3607 inform("missing section %d for parameter in message "
3608 "%s's %s: field\n (parameter %s)", pindex - 1,
3609 filename, fieldname, pp->name);
3610 return NOTOK;
3611 }
3612 tlen += sp->len;
3613 }
3614
3615 p = q = mh_xmalloc(tlen + 1);
3616 for (sp = pp->sechead; sp != NULL; ) {
3617 memcpy(q, sp->value, sp->len);
3618 q += sp->len;
3619 free(sp->value);
3620 sp2 = sp->next;
3621 free(sp);
3622 sp = sp2;
3623 }
3624
3625 p[tlen] = '\0';
3626
3627 pm = add_param(param_head, param_tail, pp->name, p, 1);
3628 pm->pm_charset = pp->charset;
3629 pm->pm_lang = pp->lang;
3630 pp2 = pp->next;
3631 free(pp);
3632 pp = pp2;
3633 }
3634
3635 *header_attrp = cp;
3636 return OK;
3637 }
3638
3639 /*
3640 * Return the charset for a particular content type.
3641 */
3642
3643 char *
3644 content_charset (CT ct) {
3645 char *ret_charset = NULL;
3646
3647 ret_charset = get_param(ct->c_ctinfo.ci_first_pm, "charset", '?', 0);
3648
3649 return ret_charset ? ret_charset : mh_xstrdup("US-ASCII");
3650 }
3651
3652
3653 /*
3654 * Create a string based on a list of output parameters. Assume that this
3655 * parameter string will be appended to an existing header, so start out
3656 * with the separator (;). Perform RFC 2231 encoding when necessary.
3657 */
3658
3659 char *
3660 output_params(size_t initialwidth, PM params, int *offsetout, int external)
3661 {
3662 char *paramout = NULL;
3663 char line[CPERLIN * 2], *q;
3664 int curlen, index, cont, encode, i;
3665 size_t valoff, numchars;
3666
3667 while (params != NULL) {
3668 encode = 0;
3669 index = 0;
3670 valoff = 0;
3671 q = line;
3672
3673 if (external && strcasecmp(params->pm_name, "body") == 0)
3674 continue;
3675
3676 if (strlen(params->pm_name) > CPERLIN) {
3677 inform("Parameter name \"%s\" is too long", params->pm_name);
3678 mh_xfree(paramout);
3679 return NULL;
3680 }
3681
3682 curlen = param_len(params, index, valoff, &encode, &cont, &numchars);
3683
3684 /*
3685 * Loop until we get a parameter that fits within a line. We
3686 * assume new lines start with a tab, so check our overflow based
3687 * on that.
3688 */
3689
3690 while (cont) {
3691 *q++ = ';';
3692 *q++ = '\n';
3693 *q++ = '\t';
3694
3695 /*
3696 * At this point we're definitely continuing the line, so
3697 * be sure to include the parameter name and section index.
3698 */
3699
3700 q += snprintf(q, sizeof(line) - (q - line), "%s*%d",
3701 params->pm_name, index);
3702
3703 /*
3704 * Both of these functions do a NUL termination
3705 */
3706
3707 if (encode)
3708 i = encode_param(params, q, sizeof(line) - (q - line),
3709 numchars, valoff, index);
3710 else
3711 i = normal_param(params, q, sizeof(line) - (q - line),
3712 numchars, valoff);
3713
3714 if (i == 0) {
3715 mh_xfree(paramout);
3716 return NULL;
3717 }
3718
3719 valoff += numchars;
3720 index++;
3721 curlen = param_len(params, index, valoff, &encode, &cont,
3722 &numchars);
3723 q = line;
3724
3725 /*
3726 * "line" starts with a ;\n\t, so that doesn't count against
3727 * the length. But add 8 since it starts with a tab; that's
3728 * how we end up with 5.
3729 */
3730
3731 initialwidth = strlen(line) + 5;
3732
3733 /*
3734 * At this point the line should be built, so add it to our
3735 * current output buffer.
3736 */
3737
3738 paramout = add(line, paramout);
3739 }
3740
3741 /*
3742 * If this won't fit on the line, start a new one. Save room in
3743 * case we need a semicolon on the end
3744 */
3745
3746 if (initialwidth + curlen > CPERLIN - 1) {
3747 *q++ = ';';
3748 *q++ = '\n';
3749 *q++ = '\t';
3750 initialwidth = 8;
3751 } else {
3752 *q++ = ';';
3753 *q++ = ' ';
3754 initialwidth += 2;
3755 }
3756
3757 /*
3758 * At this point, we're either finishing a continued parameter, or
3759 * we're working on a new one.
3760 */
3761
3762 if (index > 0) {
3763 q += snprintf(q, sizeof(line) - (q - line), "%s*%d",
3764 params->pm_name, index);
3765 } else {
3766 strncpy(q, params->pm_name, sizeof(line) - (q - line));
3767 q += strlen(q);
3768 }
3769
3770 if (encode)
3771 i = encode_param(params, q, sizeof(line) - (q - line),
3772 strlen(params->pm_value + valoff), valoff, index);
3773 else
3774 i = normal_param(params, q, sizeof(line) - (q - line),
3775 strlen(params->pm_value + valoff), valoff);
3776
3777 if (i == 0) {
3778 mh_xfree(paramout);
3779 return NULL;
3780 }
3781
3782 paramout = add(line, paramout);
3783 initialwidth += strlen(line);
3784
3785 params = params->pm_next;
3786 }
3787
3788 if (offsetout)
3789 *offsetout = initialwidth;
3790
3791 return paramout;
3792 }
3793
3794 /*
3795 * Calculate the size of a parameter.
3796 *
3797 * Arguments include
3798 *
3799 * pm - The parameter being output
3800 * index - If continuing the parameter, the index of the section
3801 * we're on.
3802 * valueoff - The current offset into the parameter value that we're
3803 * working on (previous sections have consumed valueoff bytes).
3804 * encode - Set if we should perform encoding on this parameter section
3805 * (given that we're consuming bytesfit bytes).
3806 * cont - Set if the remaining data in value will not fit on a single
3807 * line and will need to be continued.
3808 * bytesfit - The number of bytes that we can consume from the parameter
3809 * value and still fit on a completely new line. The
3810 * calculation assumes the new line starts with a tab,
3811 * includes the parameter name and any encoding, and fits
3812 * within CPERLIN bytes. Will always be at least 1.
3813 */
3814
3815 static size_t
3816 param_len(PM pm, int index, size_t valueoff, int *encode, int *cont,
3817 size_t *bytesfit)
3818 {
3819 char *start = pm->pm_value + valueoff, *p, indexchar[32];
3820 size_t len = 0, fit = 0;
3821 int fitlimit = 0, eightbit, maxfit;
3822
3823 *encode = 0;
3824
3825 /*
3826 * Add up the length. First, start with the parameter name.
3827 */
3828
3829 len = strlen(pm->pm_name);
3830
3831 /*
3832 * Scan the parameter value and see if we need to do encoding for this
3833 * section.
3834 */
3835
3836 eightbit = contains8bit(start, NULL);
3837
3838 /*
3839 * Determine if we need to encode this section. Encoding is necessary if:
3840 *
3841 * - There are any 8-bit characters at all and we're on the first
3842 * section.
3843 * - There are 8-bit characters within N bytes of our section start.
3844 * N is calculated based on the number of bytes it would take to
3845 * reach CPERLIN. Specifically:
3846 * 8 (starting tab) +
3847 * strlen(param name) +
3848 * 4 ('* for section marker, '=', opening/closing '"')
3849 * strlen (index)
3850 * is the number of bytes used by everything that isn't part of the
3851 * value. So that gets subtracted from CPERLIN.
3852 */
3853
3854 snprintf(indexchar, sizeof(indexchar), "%d", index);
3855 maxfit = CPERLIN - (12 + len + strlen(indexchar));
3856 if ((eightbit && index == 0) || contains8bit(start, start + maxfit)) {
3857 *encode = 1;
3858 }
3859
3860 len++; /* Add in equal sign */
3861
3862 if (*encode) {
3863 /*
3864 * We're using maxfit as a marker for how many characters we can
3865 * fit into the line. Bump it by two because we're not using quotes
3866 * when encoding.
3867 */
3868
3869 maxfit += 2;
3870
3871 /*
3872 * If we don't have a charset or language tag in this parameter,
3873 * add them now.
3874 */
3875
3876 if (! pm->pm_charset) {
3877 pm->pm_charset = mh_xstrdup(write_charset_8bit());
3878 if (strcasecmp(pm->pm_charset, "US-ASCII") == 0)
3879 adios(NULL, "8-bit characters in parameter \"%s\", but "
3880 "local character set is US-ASCII", pm->pm_name);
3881 }
3882 if (! pm->pm_lang)
3883 pm->pm_lang = mh_xstrdup(""); /* Default to a blank lang tag */
3884
3885 len++; /* For the encoding marker */
3886 maxfit--;
3887 if (index == 0) {
3888 int enclen = strlen(pm->pm_charset) + strlen(pm->pm_lang) + 2;
3889 len += enclen;
3890 maxfit-= enclen;
3891 } else {
3892 /*
3893 * We know we definitely need to include an index. maxfit already
3894 * includes the section marker.
3895 */
3896 len += strlen(indexchar);
3897 }
3898 for (p = start; *p != '\0'; p++) {
3899 if (isparamencode(*p)) {
3900 len += 3;
3901 maxfit -= 3;
3902 } else {
3903 len++;
3904 maxfit--;
3905 }
3906 /*
3907 * Just so there's no confusion: maxfit is counting OUTPUT
3908 * characters (post-encoding). fit is counting INPUT characters.
3909 */
3910 if (! fitlimit && maxfit >= 0)
3911 fit++;
3912 else if (! fitlimit)
3913 fitlimit++;
3914 }
3915 } else {
3916 /*
3917 * Calculate the string length, but add room for quoting \
3918 * and " if necessary. Also account for quotes at beginning
3919 * and end.
3920 */
3921 for (p = start; *p != '\0'; p++) {
3922 switch (*p) {
3923 case '"':
3924 case '\\':
3925 len++;
3926 maxfit--;
3927 /* FALLTHRU */
3928 default:
3929 len++;
3930 maxfit--;
3931 }
3932 if (! fitlimit && maxfit >= 0)
3933 fit++;
3934 else if (! fitlimit)
3935 fitlimit++;
3936 }
3937
3938 len += 2;
3939 }
3940
3941 if (fit < 1)
3942 fit = 1;
3943
3944 *cont = fitlimit;
3945 *bytesfit = fit;
3946
3947 return len;
3948 }
3949
3950 /*
3951 * Output an encoded parameter string.
3952 */
3953
3954 size_t
3955 encode_param(PM pm, char *output, size_t len, size_t valuelen,
3956 size_t valueoff, int index)
3957 {
3958 size_t outlen = 0, n;
3959 char *endptr = output + len, *p;
3960
3961 /*
3962 * First, output the marker for an encoded string.
3963 */
3964
3965 *output++ = '*';
3966 *output++ = '=';
3967 outlen += 2;
3968
3969 /*
3970 * If the index is 0, output the character set and language tag.
3971 * If theses were NULL, they should have already been filled in
3972 * by param_len().
3973 */
3974
3975 if (index == 0) {
3976 n = snprintf(output, len - outlen, "%s'%s'", pm->pm_charset,
3977 pm->pm_lang);
3978 output += n;
3979 outlen += n;
3980 if (output > endptr) {
3981 inform("Internal error: parameter buffer overflow");
3982 return 0;
3983 }
3984 }
3985
3986 /*
3987 * Copy over the value, encoding if necessary
3988 */
3989
3990 p = pm->pm_value + valueoff;
3991 while (valuelen-- > 0) {
3992 if (isparamencode(*p)) {
3993 n = snprintf(output, len - outlen, "%%%02X", (unsigned char) *p++);
3994 output += n;
3995 outlen += n;
3996 } else {
3997 *output++ = *p++;
3998 outlen++;
3999 }
4000 if (output > endptr) {
4001 inform("Internal error: parameter buffer overflow");
4002 return 0;
4003 }
4004 }
4005
4006 *output = '\0';
4007
4008 return outlen;
4009 }
4010
4011 /*
4012 * Output a "normal" parameter, without encoding. Be sure to escape
4013 * quotes and backslashes if necessary.
4014 */
4015
4016 static size_t
4017 normal_param(PM pm, char *output, size_t len, size_t valuelen,
4018 size_t valueoff)
4019 {
4020 size_t outlen = 0;
4021 char *endptr = output + len, *p;
4022
4023 *output++ = '=';
4024 *output++ = '"';
4025 outlen += 2;
4026
4027 p = pm->pm_value + valueoff;
4028
4029 while (valuelen-- > 0) {
4030 switch (*p) {
4031 case '\\':
4032 case '"':
4033 *output++ = '\\';
4034 outlen++;
4035 /* FALLTHRU */
4036 default:
4037 *output++ = *p++;
4038 outlen++;
4039 }
4040 if (output > endptr) {
4041 inform("Internal error: parameter buffer overflow");
4042 return 0;
4043 }
4044 }
4045
4046 if (output - 2 > endptr) {
4047 inform("Internal error: parameter buffer overflow");
4048 return 0;
4049 }
4050
4051 *output++ = '"';
4052 *output++ = '\0';
4053
4054 return outlen + 1;
4055 }
4056
4057 /*
4058 * Add a parameter to the parameter linked list
4059 */
4060
4061 PM
4062 add_param(PM *first, PM *last, char *name, char *value, int nocopy)
4063 {
4064 PM pm;
4065
4066 NEW0(pm);
4067 pm->pm_name = nocopy ? name : getcpy(name);
4068 pm->pm_value = nocopy ? value : getcpy(value);
4069
4070 if (*first) {
4071 (*last)->pm_next = pm;
4072 *last = pm;
4073 } else {
4074 *first = pm;
4075 *last = pm;
4076 }
4077
4078 return pm;
4079 }
4080
4081 /*
4082 * Either replace a current parameter with a new value, or add the parameter
4083 * to the parameter linked list.
4084 */
4085
4086 PM
4087 replace_param(PM *first, PM *last, char *name, char *value, int nocopy)
4088 {
4089 PM pm;
4090
4091 for (pm = *first; pm != NULL; pm = pm->pm_next) {
4092 if (strcasecmp(name, pm->pm_name) == 0) {
4093 /*
4094 * If nocopy is set, it's assumed that we own both name
4095 * and value. We don't need name, so we discard it now.
4096 */
4097 if (nocopy)
4098 free(name);
4099 free(pm->pm_value);
4100 pm->pm_value = nocopy ? value : getcpy(value);
4101 return pm;
4102 }
4103 }
4104
4105 return add_param(first, last, name, value, nocopy);
4106 }
4107
4108 /*
4109 * Retrieve a parameter value from a parameter linked list. If the parameter
4110 * value needs converted to the local character set, do that now.
4111 */
4112
4113 char *
4114 get_param(PM first, const char *name, char replace, int fetchonly)
4115 {
4116 while (first != NULL) {
4117 if (strcasecmp(name, first->pm_name) == 0) {
4118 if (fetchonly)
4119 return first->pm_value;
4120 return getcpy(get_param_value(first, replace));
4121 }
4122 first = first->pm_next;
4123 }
4124
4125 return NULL;
4126 }
4127
4128 /*
4129 * Return a parameter value, converting to the local character set if
4130 * necessary
4131 */
4132
4133 char *get_param_value(PM pm, char replace)
4134 {
4135 static char buffer[4096]; /* I hope no parameters are larger */
4136 size_t bufsize = sizeof(buffer);
4137 #ifdef HAVE_ICONV
4138 size_t inbytes;
4139 int utf8;
4140 iconv_t cd;
4141 ICONV_CONST char *p;
4142 #else /* HAVE_ICONV */
4143 char *p;
4144 #endif /* HAVE_ICONV */
4145
4146 char *q;
4147
4148 /*
4149 * If we don't have a character set indicated, it's assumed to be
4150 * US-ASCII. If it matches our character set, we don't need to convert
4151 * anything.
4152 */
4153
4154 if (!pm->pm_charset || check_charset(pm->pm_charset,
4155 strlen(pm->pm_charset))) {
4156 return pm->pm_value;
4157 }
4158
4159 /*
4160 * In this case, we need to convert. If we have iconv support, use
4161 * that. Otherwise, go through and simply replace every non-ASCII
4162 * character with the substitution character.
4163 */
4164
4165 #ifdef HAVE_ICONV
4166 q = buffer;
4167 bufsize = sizeof(buffer);
4168 utf8 = strcasecmp(pm->pm_charset, "UTF-8") == 0;
4169
4170 cd = iconv_open(get_charset(), pm->pm_charset);
4171 if (cd == (iconv_t) -1) {
4172 goto noiconv;
4173 }
4174
4175 inbytes = strlen(pm->pm_value);
4176 p = pm->pm_value;
4177
4178 while (inbytes) {
4179 if (iconv(cd, &p, &inbytes, &q, &bufsize) == (size_t)-1) {
4180 if (errno != EILSEQ) {
4181 iconv_close(cd);
4182 goto noiconv;
4183 }
4184 /*
4185 * Reset shift state, substitute our character,
4186 * try to restart conversion.
4187 */
4188
4189 iconv(cd, NULL, NULL, &q, &bufsize);
4190
4191 if (bufsize == 0) {
4192 iconv_close(cd);
4193 goto noiconv;
4194 }
4195 *q++ = replace;
4196 bufsize--;
4197 if (bufsize == 0) {
4198 iconv_close(cd);
4199 goto noiconv;
4200 }
4201 if (utf8) {
4202 for (++p, --inbytes;
4203 inbytes > 0 && (((unsigned char) *p) & 0xc0) == 0x80;
4204 ++p, --inbytes)
4205 continue;
4206 } else {
4207 p++;
4208 inbytes--;
4209 }
4210 }
4211 }
4212
4213 iconv_close(cd);
4214
4215 if (bufsize == 0)
4216 q--;
4217 *q = '\0';
4218
4219 return buffer;
4220
4221 noiconv:
4222 #endif /* HAVE_ICONV */
4223
4224 /*
4225 * Take everything non-ASCII and substitute the replacement character
4226 */
4227
4228 q = buffer;
4229 bufsize = sizeof(buffer);
4230 for (p = pm->pm_value; *p != '\0' && bufsize > 1; p++, q++, bufsize--) {
4231 if (isascii((unsigned char) *p) && isprint((unsigned char) *p))
4232 *q = *p;
4233 else
4234 *q = replace;
4235 }
4236
4237 *q = '\0';
4238
4239 return buffer;
4240 }