1 /* mhparse.c -- routines to parse the contents of MIME messages
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.
14 #include <h/mhparse.h>
19 #endif /* HAVE_ICONV */
25 extern int rcachesw
; /* mhcachesbr.c */
26 extern int wcachesw
; /* mhcachesbr.c */
28 int checksw
= 0; /* check Content-MD5 field */
31 * These are for mhfixmsg to:
32 * 1) Instruct parser not to detect invalid Content-Transfer-Encoding
34 * 2) Suppress the warning about bogus multipart content, and report it.
35 * 3) Suppress the warning about extraneous trailing ';' in header parameter
38 int skip_mp_cte_check
;
39 int suppress_bogus_mp_content_warning
;
41 int suppress_extraneous_trailing_semicolon_warning
;
44 * By default, suppress warning about multiple MIME-Version header fields.
46 int suppress_multiple_mime_version_warning
= 1;
48 /* list of preferred type/subtype pairs, for -prefer */
49 char *preferred_types
[NPREFS
],
50 *preferred_subtypes
[NPREFS
];
55 * Structures for TEXT messages
57 struct k2v SubText
[] = {
58 { "plain", TEXT_PLAIN
},
59 { "richtext", TEXT_RICHTEXT
}, /* defined in RFC 1341 */
60 { "enriched", TEXT_ENRICHED
}, /* defined in RFC 1896 */
61 { NULL
, TEXT_UNKNOWN
} /* this one must be last! */
64 /* Charset[] removed -- yozo. Mon Oct 8 01:03:41 JST 2012 */
67 * Structures for MULTIPART messages
69 struct k2v SubMultiPart
[] = {
70 { "mixed", MULTI_MIXED
},
71 { "alternative", MULTI_ALTERNATE
},
72 { "digest", MULTI_DIGEST
},
73 { "parallel", MULTI_PARALLEL
},
74 { "related", MULTI_RELATED
},
75 { NULL
, MULTI_UNKNOWN
} /* this one must be last! */
79 * Structures for MESSAGE messages
81 struct k2v SubMessage
[] = {
82 { "rfc822", MESSAGE_RFC822
},
83 { "partial", MESSAGE_PARTIAL
},
84 { "external-body", MESSAGE_EXTERNAL
},
85 { NULL
, MESSAGE_UNKNOWN
} /* this one must be last! */
89 * Structure for APPLICATION messages
91 struct k2v SubApplication
[] = {
92 { "octet-stream", APPLICATION_OCTETS
},
93 { "postscript", APPLICATION_POSTSCRIPT
},
94 { NULL
, APPLICATION_UNKNOWN
} /* this one must be last! */
98 * Mapping of names of CTE types in mhbuild directives
100 static struct k2v EncodingType
[] = {
103 { "q-p", CE_QUOTED
},
104 { "quoted-printable", CE_QUOTED
},
105 { "b64", CE_BASE64
},
106 { "base64", CE_BASE64
},
112 int find_cache (CT
, int, int *, char *, char *, int);
116 int type_ok (CT
, int);
117 void content_error (char *, CT
, char *, ...);
122 static CT
get_content (FILE *, char *, int);
123 static int get_comment (const char *, const char *, char **, char **);
125 static int InitGeneric (CT
);
126 static int InitText (CT
);
127 static int InitMultiPart (CT
);
128 static void reverse_parts (CT
);
129 static void prefer_parts(CT ct
);
130 static int InitMessage (CT
);
131 static int InitApplication (CT
);
132 static int init_encoding (CT
, OpenCEFunc
);
133 static unsigned long size_encoding (CT
);
134 static int InitBase64 (CT
);
135 static int openBase64 (CT
, char **);
136 static int InitQuoted (CT
);
137 static int openQuoted (CT
, char **);
138 static int Init7Bit (CT
);
139 static int openExternal (CT
, CT
, CE
, char **, int *);
140 static int InitFile (CT
);
141 static int openFile (CT
, char **);
142 static int InitFTP (CT
);
143 static int openFTP (CT
, char **);
144 static int InitMail (CT
);
145 static int openMail (CT
, char **);
146 static int readDigest (CT
, char *);
147 static int get_leftover_mp_content (CT
, int);
148 static int InitURL (CT
);
149 static int openURL (CT
, char **);
150 static int parse_header_attrs (const char *, const char *, char **, PM
*,
152 static size_t param_len(PM
, int, size_t, int *, int *, size_t *);
153 static size_t normal_param(PM
, char *, size_t, size_t, size_t);
154 static int get_dispo (char *, CT
, int);
156 struct str2init str2cts
[] = {
157 { "application", CT_APPLICATION
, InitApplication
},
158 { "audio", CT_AUDIO
, InitGeneric
},
159 { "image", CT_IMAGE
, InitGeneric
},
160 { "message", CT_MESSAGE
, InitMessage
},
161 { "multipart", CT_MULTIPART
, InitMultiPart
},
162 { "text", CT_TEXT
, InitText
},
163 { "video", CT_VIDEO
, InitGeneric
},
164 { NULL
, CT_EXTENSION
, NULL
}, /* these two must be last! */
165 { NULL
, CT_UNKNOWN
, NULL
},
168 struct str2init str2ces
[] = {
169 { "base64", CE_BASE64
, InitBase64
},
170 { "quoted-printable", CE_QUOTED
, InitQuoted
},
171 { "8bit", CE_8BIT
, Init7Bit
},
172 { "7bit", CE_7BIT
, Init7Bit
},
173 { "binary", CE_BINARY
, Init7Bit
},
174 { NULL
, CE_EXTENSION
, NULL
}, /* these two must be last! */
175 { NULL
, CE_UNKNOWN
, NULL
},
179 * NOTE WELL: si_key MUST NOT have value of NOTOK
181 * si_key is 1 if access method is anonymous.
183 struct str2init str2methods
[] = {
184 { "afs", 1, InitFile
},
185 { "anon-ftp", 1, InitFTP
},
186 { "ftp", 0, InitFTP
},
187 { "local-file", 0, InitFile
},
188 { "mail-server", 0, InitMail
},
189 { "url", 0, InitURL
},
195 * Main entry point for parsing a MIME message or file.
196 * It returns the Content structure for the top level
197 * entity in the file.
201 parse_mime (char *file
)
210 bogus_mp_content
= 0;
213 * Check if file is actually standard input
215 if ((is_stdin
= !(strcmp (file
, "-")))) {
216 char *tfile
= m_mktemp2(NULL
, invo_name
, NULL
, &fp
);
218 advise("mhparse", "unable to create temporary file in %s",
222 file
= mh_xstrdup(tfile
);
224 while ((n
= fread(buffer
, 1, sizeof(buffer
), stdin
)) > 0) {
225 if (fwrite(buffer
, 1, n
, fp
) != n
) {
226 (void) m_unlink (file
);
227 advise (file
, "error copying to temporary file");
233 if (ferror (stdin
)) {
234 (void) m_unlink (file
);
235 advise ("stdin", "error reading");
239 (void) m_unlink (file
);
240 advise (file
, "error writing");
243 fseek (fp
, 0L, SEEK_SET
);
244 } else if (stat (file
, &statbuf
) == NOTOK
) {
245 advise (file
, "unable to stat");
247 } else if (S_ISDIR(statbuf
.st_mode
)) {
248 /* Don't try to parse a directory. */
249 inform("%s is a directory", file
);
251 } else if ((fp
= fopen (file
, "r")) == NULL
) {
252 advise (file
, "unable to read");
256 if (!(ct
= get_content (fp
, file
, 1))) {
258 (void) m_unlink (file
);
259 inform("unable to decode %s", file
);
264 ct
->c_unlink
= 1; /* temp file to remove */
268 if (ct
->c_end
== 0L) {
269 fseek (fp
, 0L, SEEK_END
);
270 ct
->c_end
= ftell (fp
);
273 if (ct
->c_ctinitfnx
&& (*ct
->c_ctinitfnx
) (ct
) == NOTOK
) {
285 * Main routine for reading/parsing the headers
286 * of a message content.
288 * toplevel = 1 # we are at the top level of the message
289 * toplevel = 0 # we are inside message type or multipart type
290 * # other than multipart/digest
291 * toplevel = -1 # we are inside multipart/digest
292 * NB: on failure we will fclose(in)!
296 get_content (FILE *in
, char *file
, int toplevel
)
299 char buf
[NMH_BUFSIZ
], name
[NAMESZ
];
303 m_getfld_state_t gstate
= 0;
305 /* allocate the content structure */
308 ct
->c_file
= add (file
, NULL
);
309 ct
->c_begin
= ftell (ct
->c_fp
) + 1;
312 * Parse the header fields for this
313 * content into a linked list.
315 m_getfld_track_filepos (&gstate
, in
);
316 for (compnum
= 1;;) {
317 int bufsz
= sizeof buf
;
318 switch (state
= m_getfld (&gstate
, name
, buf
, &bufsz
, in
)) {
323 /* get copies of the buffers */
324 np
= mh_xstrdup(name
);
325 vp
= mh_xstrdup(buf
);
327 /* if necessary, get rest of field */
328 while (state
== FLDPLUS
) {
330 state
= m_getfld (&gstate
, name
, buf
, &bufsz
, in
);
331 vp
= add (buf
, vp
); /* add to previous value */
334 /* Now add the header data to the list */
335 add_header (ct
, np
, vp
);
337 /* continue, to see if this isn't the last header field */
338 ct
->c_begin
= ftell (in
) + 1;
342 if (name
[0] == ':') {
343 /* Special case: no blank line between header and body. The
344 file position indicator is on the newline at the end of the
345 line, but it needs to be one prior to the beginning of the
346 line. So subtract the length of the line, bufsz, plus 1. */
347 ct
->c_begin
= ftell (in
) - (bufsz
+ 1);
349 ct
->c_begin
= ftell (in
) - (bufsz
- 1);
354 ct
->c_begin
= ftell (in
);
359 adios (NULL
, "message format error in component #%d", compnum
);
362 adios (NULL
, "getfld() returned %d", state
);
365 /* break out of the loop */
368 m_getfld_state_destroy (&gstate
);
371 * Read the content headers. We will parse the
372 * MIME related header fields into their various
373 * structures and set internal flags related to
374 * content type/subtype, etc.
377 hp
= ct
->c_first_hf
; /* start at first header field */
379 /* Get MIME-Version field */
380 if (!strcasecmp (hp
->name
, VRSN_FIELD
)) {
385 vrsn
= add (hp
->value
, NULL
);
387 /* Now, cleanup this field */
390 while (isspace ((unsigned char) *cp
))
392 for (dp
= strchr(cp
, '\n'); dp
; dp
= strchr(dp
, '\n'))
394 for (dp
= cp
+ strlen (cp
) - 1; dp
>= cp
; dp
--)
395 if (!isspace ((unsigned char) *dp
))
399 fprintf (stderr
, "%s: %s\n", VRSN_FIELD
, cp
);
402 get_comment (ct
->c_file
, VRSN_FIELD
, &cp
, NULL
) == NOTOK
)
405 for (dp
= cp
; istoken (*dp
); dp
++)
409 ucmp
= !strcasecmp (cp
, VRSN_VALUE
);
412 inform("message %s has unknown value for %s: field (%s), continuing...",
413 ct
->c_file
, VRSN_FIELD
, cp
);
418 if (! suppress_multiple_mime_version_warning
)
419 inform("message %s has multiple %s: fields",
420 ct
->c_file
, VRSN_FIELD
);
424 else if (!strcasecmp (hp
->name
, TYPE_FIELD
)) {
425 /* Get Content-Type field */
426 struct str2init
*s2i
;
427 CI ci
= &ct
->c_ctinfo
;
429 /* Check if we've already seen a Content-Type header */
431 inform("message %s has multiple %s: fields",
432 ct
->c_file
, TYPE_FIELD
);
436 /* Parse the Content-Type field */
437 if (get_ctinfo (hp
->value
, ct
, 0) == NOTOK
)
441 * Set the Init function and the internal
442 * flag for this content type.
444 for (s2i
= str2cts
; s2i
->si_key
; s2i
++)
445 if (!strcasecmp (ci
->ci_type
, s2i
->si_key
))
447 if (!s2i
->si_key
&& !uprf (ci
->ci_type
, "X-"))
449 ct
->c_type
= s2i
->si_val
;
450 ct
->c_ctinitfnx
= s2i
->si_init
;
452 else if (!strcasecmp (hp
->name
, ENCODING_FIELD
)) {
453 /* Get Content-Transfer-Encoding field */
455 struct str2init
*s2i
;
458 * Check if we've already seen the
459 * Content-Transfer-Encoding field
462 inform("message %s has multiple %s: fields",
463 ct
->c_file
, ENCODING_FIELD
);
467 /* get copy of this field */
468 ct
->c_celine
= cp
= add (hp
->value
, NULL
);
470 while (isspace ((unsigned char) *cp
))
472 for (dp
= cp
; istoken (*dp
); dp
++)
478 * Find the internal flag and Init function
479 * for this transfer encoding.
481 for (s2i
= str2ces
; s2i
->si_key
; s2i
++)
482 if (!strcasecmp (cp
, s2i
->si_key
))
484 if (!s2i
->si_key
&& !uprf (cp
, "X-"))
487 ct
->c_encoding
= s2i
->si_val
;
489 /* Call the Init function for this encoding */
490 if (s2i
->si_init
&& (*s2i
->si_init
) (ct
) == NOTOK
)
493 else if (!strcasecmp (hp
->name
, MD5_FIELD
)) {
494 /* Get Content-MD5 field */
500 if (ct
->c_digested
) {
501 inform("message %s has multiple %s: fields",
502 ct
->c_file
, MD5_FIELD
);
506 ep
= cp
= add (hp
->value
, NULL
); /* get a copy */
508 while (isspace ((unsigned char) *cp
))
510 for (dp
= strchr(cp
, '\n'); dp
; dp
= strchr(dp
, '\n'))
512 for (dp
= cp
+ strlen (cp
) - 1; dp
>= cp
; dp
--)
513 if (!isspace ((unsigned char) *dp
))
517 fprintf (stderr
, "%s: %s\n", MD5_FIELD
, cp
);
520 get_comment (ct
->c_file
, MD5_FIELD
, &cp
, NULL
) == NOTOK
) {
525 for (dp
= cp
; *dp
&& !isspace ((unsigned char) *dp
); dp
++)
533 else if (!strcasecmp (hp
->name
, ID_FIELD
)) {
534 /* Get Content-ID field */
535 ct
->c_id
= add (hp
->value
, ct
->c_id
);
537 else if (!strcasecmp (hp
->name
, DESCR_FIELD
)) {
538 /* Get Content-Description field */
539 ct
->c_descr
= add (hp
->value
, ct
->c_descr
);
541 else if (!strcasecmp (hp
->name
, DISPO_FIELD
)) {
542 /* Get Content-Disposition field */
543 if (get_dispo(hp
->value
, ct
, 0) == NOTOK
)
548 hp
= hp
->next
; /* next header field */
552 * Check if we saw a Content-Type field.
553 * If not, then assign a default value for
554 * it, and the Init function.
558 * If we are inside a multipart/digest message,
559 * so default type is message/rfc822
562 if (get_ctinfo ("message/rfc822", ct
, 0) == NOTOK
)
564 ct
->c_type
= CT_MESSAGE
;
565 ct
->c_ctinitfnx
= InitMessage
;
568 * Else default type is text/plain
570 if (get_ctinfo ("text/plain", ct
, 0) == NOTOK
)
572 ct
->c_type
= CT_TEXT
;
573 ct
->c_ctinitfnx
= InitText
;
577 /* Use default Transfer-Encoding, if necessary */
579 ct
->c_encoding
= CE_7BIT
;
592 * small routine to add header field to list
596 add_header (CT ct
, char *name
, char *value
)
600 /* allocate header field structure */
603 /* link data into header structure */
608 /* link header structure into the list */
609 if (ct
->c_first_hf
== NULL
) {
610 ct
->c_first_hf
= hp
; /* this is the first */
613 ct
->c_last_hf
->next
= hp
; /* add it to the end */
622 * Parse Content-Type line and (if `magic' is non-zero) mhbuild composition
623 * directives. Fills in the information of the CTinfo structure.
626 get_ctinfo (char *cp
, CT ct
, int magic
)
635 /* store copy of Content-Type line */
636 cp
= ct
->c_ctline
= add (cp
, NULL
);
638 while (isspace ((unsigned char) *cp
)) /* trim leading spaces */
641 /* change newlines to spaces */
642 for (dp
= strchr(cp
, '\n'); dp
; dp
= strchr(dp
, '\n'))
645 /* trim trailing spaces */
646 for (dp
= cp
+ strlen (cp
) - 1; dp
>= cp
; dp
--)
647 if (!isspace ((unsigned char) *dp
))
652 fprintf (stderr
, "%s: %s\n", TYPE_FIELD
, cp
);
654 if (*cp
== '(' && get_comment (ct
->c_file
, TYPE_FIELD
, &cp
,
655 &ci
->ci_comment
) == NOTOK
)
658 for (dp
= cp
; istoken (*dp
); dp
++)
662 ci
->ci_type
= mh_xstrdup(cp
); /* store content type */
667 inform("invalid %s: field in message %s (empty type)",
668 TYPE_FIELD
, ct
->c_file
);
671 to_lower(ci
->ci_type
);
673 while (isspace ((unsigned char) *cp
))
676 if (*cp
== '(' && get_comment (ct
->c_file
, TYPE_FIELD
, &cp
,
677 &ci
->ci_comment
) == NOTOK
)
682 ci
->ci_subtype
= mh_xstrdup("");
687 while (isspace ((unsigned char) *cp
))
690 if (*cp
== '(' && get_comment (ct
->c_file
, TYPE_FIELD
, &cp
,
691 &ci
->ci_comment
) == NOTOK
)
694 for (dp
= cp
; istoken (*dp
); dp
++)
698 ci
->ci_subtype
= mh_xstrdup(cp
); /* store the content subtype */
702 if (!*ci
->ci_subtype
) {
703 inform("invalid %s: field in message %s (empty subtype for \"%s\")",
704 TYPE_FIELD
, ct
->c_file
, ci
->ci_type
);
707 to_lower(ci
->ci_subtype
);
710 while (isspace ((unsigned char) *cp
))
713 if (*cp
== '(' && get_comment (ct
->c_file
, TYPE_FIELD
, &cp
,
714 &ci
->ci_comment
) == NOTOK
)
717 if ((status
= parse_header_attrs (ct
->c_file
, TYPE_FIELD
, &cp
,
718 &ci
->ci_first_pm
, &ci
->ci_last_pm
,
719 &ci
->ci_comment
)) != OK
) {
720 return status
== NOTOK
? NOTOK
: OK
;
724 * Get any <Content-Id> given in buffer
726 if (magic
&& *cp
== '<') {
729 if (!(dp
= strchr(ct
->c_id
= ++cp
, '>'))) {
730 inform("invalid ID in message %s", ct
->c_file
);
736 ct
->c_id
= concat ("<", ct
->c_id
, ">\n", NULL
);
742 while (isspace ((unsigned char) *cp
))
747 * Get any [Content-Description] given in buffer.
749 if (magic
&& *cp
== '[') {
751 for (dp
= cp
+ strlen (cp
) - 1; dp
>= cp
; dp
--)
755 inform("invalid description in message %s", ct
->c_file
);
763 ct
->c_descr
= concat (ct
->c_descr
, "\n", NULL
);
769 while (isspace ((unsigned char) *cp
))
774 * Get any {Content-Disposition} given in buffer.
776 if (magic
&& *cp
== '{') {
778 for (dp
= cp
+ strlen (cp
) - 1; dp
>= cp
; dp
--)
782 inform("invalid disposition in message %s", ct
->c_file
);
790 if (get_dispo(cp
, ct
, 1) != OK
)
796 while (isspace ((unsigned char) *cp
))
801 * Get any extension directives (right now just the content transfer
802 * encoding, but maybe others) that we care about.
805 if (magic
&& *cp
== '*') {
807 * See if it's a CTE we match on
812 while (*cp
!= '\0' && ! isspace((unsigned char) *cp
))
816 inform("invalid null transfer encoding specification");
823 ct
->c_reqencoding
= CE_UNKNOWN
;
825 for (kv
= EncodingType
; kv
->kv_key
; kv
++) {
826 if (strcasecmp(kv
->kv_key
, dp
) == 0) {
827 ct
->c_reqencoding
= kv
->kv_value
;
832 if (ct
->c_reqencoding
== CE_UNKNOWN
) {
833 inform("invalid CTE specification: \"%s\"", dp
);
837 while (isspace ((unsigned char) *cp
))
842 * Check if anything is left over
846 ci
->ci_magic
= mh_xstrdup(cp
);
848 /* If there is a Content-Disposition header and it doesn't
849 have a *filename=, extract it from the magic contents.
850 The r1bindex call skips any leading directory
852 if (ct
->c_dispo_type
&&
853 !get_param(ct
->c_dispo_first
, "filename", '_', 1)) {
854 add_param(&ct
->c_dispo_first
, &ct
->c_dispo_last
, "filename",
855 r1bindex(ci
->ci_magic
, '/'), 0);
859 inform("extraneous information in message %s's %s: field\n%*s(%s)",
860 ct
->c_file
, TYPE_FIELD
, strlen(invo_name
) + 2, "", cp
);
868 * Parse out a Content-Disposition header. A lot of this is cribbed from
872 get_dispo (char *cp
, CT ct
, int buildflag
)
874 char *dp
, *dispoheader
;
879 * Save the whole copy of the Content-Disposition header, unless we're
880 * processing a mhbuild directive. A NULL c_dispo will be a flag to
881 * mhbuild that the disposition header needs to be generated at that
885 dispoheader
= cp
= add(cp
, NULL
);
887 while (isspace ((unsigned char) *cp
)) /* trim leading spaces */
890 /* change newlines to spaces */
891 for (dp
= strchr(cp
, '\n'); dp
; dp
= strchr(dp
, '\n'))
894 /* trim trailing spaces */
895 for (dp
= cp
+ strlen (cp
) - 1; dp
>= cp
; dp
--)
896 if (!isspace ((unsigned char) *dp
))
901 fprintf (stderr
, "%s: %s\n", DISPO_FIELD
, cp
);
903 if (*cp
== '(' && get_comment (ct
->c_file
, DISPO_FIELD
, &cp
, NULL
) ==
909 for (dp
= cp
; istoken (*dp
); dp
++)
913 ct
->c_dispo_type
= mh_xstrdup(cp
); /* store disposition type */
917 if (*cp
== '(' && get_comment (ct
->c_file
, DISPO_FIELD
, &cp
, NULL
) == NOTOK
)
920 if ((status
= parse_header_attrs (ct
->c_file
, DISPO_FIELD
, &cp
,
921 &ct
->c_dispo_first
, &ct
->c_dispo_last
,
923 if (status
== NOTOK
) {
928 inform("extraneous information in message %s's %s: field\n%*s(%s)",
929 ct
->c_file
, DISPO_FIELD
, strlen(invo_name
) + 2, "", cp
);
935 ct
->c_dispo
= dispoheader
;
942 get_comment (const char *filename
, const char *fieldname
, char **ap
,
947 char c
, buffer
[BUFSIZ
], *dp
;
957 inform("invalid comment in message %s's %s: field",
958 filename
, fieldname
);
963 if ((c
= *cp
++) == '\0')
986 if ((dp
= *commentp
)) {
987 *commentp
= concat (dp
, " ", buffer
, NULL
);
990 *commentp
= mh_xstrdup(buffer
);
994 while (isspace ((unsigned char) *cp
))
1005 * Handles content types audio, image, and video.
1006 * There's not much to do right here.
1014 return OK
; /* not much to do here */
1025 char buffer
[BUFSIZ
];
1030 CI ci
= &ct
->c_ctinfo
;
1032 /* check for missing subtype */
1033 if (!*ci
->ci_subtype
)
1034 ci
->ci_subtype
= add ("plain", ci
->ci_subtype
);
1037 ct
->c_subtype
= ct_str_subtype (CT_TEXT
, ci
->ci_subtype
);
1039 /* allocate text character set structure */
1041 ct
->c_ctparams
= (void *) t
;
1043 /* scan for charset parameter */
1044 for (pm
= ci
->ci_first_pm
; pm
; pm
= pm
->pm_next
)
1045 if (!strcasecmp (pm
->pm_name
, "charset"))
1048 /* check if content specified a character set */
1050 chset
= pm
->pm_value
;
1051 t
->tx_charset
= CHARSET_SPECIFIED
;
1053 t
->tx_charset
= CHARSET_UNSPECIFIED
;
1057 * If we can not handle character set natively,
1058 * then check profile for string to modify the
1059 * terminal or display method.
1061 * termproc is for mhshow, though mhlist -debug prints it, too.
1063 if (chset
!= NULL
&& !check_charset (chset
, strlen (chset
))) {
1064 snprintf (buffer
, sizeof(buffer
), "%s-charset-%s", invo_name
, chset
);
1065 if ((cp
= context_find (buffer
)))
1066 ct
->c_termproc
= mh_xstrdup(cp
);
1078 InitMultiPart (CT ct
)
1088 struct multipart
*m
;
1089 struct part
*part
, **next
;
1090 CI ci
= &ct
->c_ctinfo
;
1095 * The encoding for multipart messages must be either
1096 * 7bit, 8bit, or binary (per RFC 2045).
1098 if (! skip_mp_cte_check
&& ct
->c_encoding
!= CE_7BIT
&&
1099 ct
->c_encoding
!= CE_8BIT
&& ct
->c_encoding
!= CE_BINARY
) {
1100 /* Copy the Content-Transfer-Encoding header field body so we can
1101 remove any trailing whitespace and leading blanks from it. */
1102 char *cte
= mh_xstrdup(ct
->c_celine
? ct
->c_celine
: "(null)");
1104 bp
= cte
+ strlen (cte
) - 1;
1105 while (bp
>= cte
&& isspace ((unsigned char) *bp
)) *bp
-- = '\0';
1106 for (bp
= cte
; *bp
&& isblank ((unsigned char) *bp
); ++bp
) continue;
1108 inform("\"%s/%s\" type in message %s must be encoded in\n"
1109 "7bit, 8bit, or binary, per RFC 2045 (6.4). "
1110 "mhfixmsg -fixcte can fix it, or\n"
1111 "manually edit the file and change the \"%s\"\n"
1112 "Content-Transfer-Encoding to one of those. For now, continuing...",
1113 ci
->ci_type
, ci
->ci_subtype
, ct
->c_file
, bp
);
1120 ct
->c_subtype
= ct_str_subtype (CT_MULTIPART
, ci
->ci_subtype
);
1123 * Check for "boundary" parameter, which is
1124 * required for multipart messages.
1127 for (pm
= ci
->ci_first_pm
; pm
; pm
= pm
->pm_next
) {
1128 if (!strcasecmp (pm
->pm_name
, "boundary")) {
1134 /* complain if boundary parameter is missing */
1136 inform("a \"boundary\" parameter is mandatory for \"%s/%s\" type in message %s's %s: field",
1137 ci
->ci_type
, ci
->ci_subtype
, ct
->c_file
, TYPE_FIELD
);
1141 /* allocate primary structure for multipart info */
1143 ct
->c_ctparams
= (void *) m
;
1145 /* check if boundary parameter contains only whitespace characters */
1146 for (cp
= bp
; isspace ((unsigned char) *cp
); cp
++)
1149 inform("invalid \"boundary\" parameter for \"%s/%s\" type in message %s's %s: field",
1150 ci
->ci_type
, ci
->ci_subtype
, ct
->c_file
, TYPE_FIELD
);
1154 /* remove trailing whitespace from boundary parameter */
1155 for (cp
= bp
, dp
= cp
+ strlen (cp
) - 1; dp
> cp
; dp
--)
1156 if (!isspace ((unsigned char) *dp
))
1160 /* record boundary separators */
1161 m
->mp_start
= concat (bp
, "\n", NULL
);
1162 m
->mp_stop
= concat (bp
, "--\n", NULL
);
1164 if (!ct
->c_fp
&& (ct
->c_fp
= fopen (ct
->c_file
, "r")) == NULL
) {
1165 advise (ct
->c_file
, "unable to open for reading");
1169 fseek (fp
= ct
->c_fp
, pos
= ct
->c_begin
, SEEK_SET
);
1171 next
= &m
->mp_parts
;
1175 while ((gotlen
= getline(&bufp
, &buflen
, fp
)) != -1) {
1180 if (bufp
[0] != '-' || bufp
[1] != '-')
1183 if (strcmp (bufp
+ 2, m
->mp_start
))
1188 next
= &part
->mp_next
;
1190 if (!(p
= get_content (fp
, ct
->c_file
,
1191 ct
->c_subtype
== MULTI_DIGEST
? -1 : 0))) {
1199 fseek (fp
, pos
, SEEK_SET
);
1202 if (strcmp (bufp
+ 2, m
->mp_start
) == 0) {
1206 p
->c_end
= ftell(fp
) - (gotlen
+ 1);
1207 if (p
->c_end
< p
->c_begin
)
1208 p
->c_begin
= p
->c_end
;
1213 if (strcmp (bufp
+ 2, m
->mp_stop
) == 0)
1218 if (! suppress_bogus_mp_content_warning
) {
1219 inform("bogus multipart content in message %s", ct
->c_file
);
1221 bogus_mp_content
= 1;
1223 if (!inout
&& part
) {
1225 p
->c_end
= ct
->c_end
;
1227 if (p
->c_begin
>= p
->c_end
) {
1228 for (next
= &m
->mp_parts
; *next
!= part
;
1229 next
= &((*next
)->mp_next
))
1238 /* reverse the order of the parts for multipart/alternative */
1239 if (ct
->c_subtype
== MULTI_ALTERNATE
) {
1245 * label all subparts with part number, and
1246 * then initialize the content of the subpart.
1251 char partnam
[BUFSIZ
];
1254 snprintf (partnam
, sizeof(partnam
), "%s.", ct
->c_partno
);
1255 pp
= partnam
+ strlen (partnam
);
1260 for (part
= m
->mp_parts
, partnum
= 1; part
;
1261 part
= part
->mp_next
, partnum
++) {
1264 sprintf (pp
, "%d", partnum
);
1265 p
->c_partno
= mh_xstrdup(partnam
);
1267 /* initialize the content of the subparts */
1268 if (p
->c_ctinitfnx
&& (*p
->c_ctinitfnx
) (p
) == NOTOK
) {
1277 get_leftover_mp_content (ct
, 1);
1278 get_leftover_mp_content (ct
, 0);
1288 * reverse the order of the parts of a multipart/alternative,
1289 * presumably to put the "most favored" alternative first, for
1290 * ease of choosing/displaying it later on. from a mail message on
1291 * nmh-workers, from kenh:
1292 * "Stock" MH 6.8.5 did not have a reverse_parts() function, but I
1293 * see code in mhn that did the same thing... According to the RCS
1294 * logs, that code was around from the initial checkin of mhn.c by
1295 * John Romine in 1992, which is as far back as we have."
1298 reverse_parts (CT ct
)
1300 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
1304 /* Reverse the order of its parts by walking the mp_parts list
1305 and pushing each node to the front. */
1306 for (part
= m
->mp_parts
, m
->mp_parts
= NULL
; part
; part
= next
) {
1307 next
= part
->mp_next
;
1308 part
->mp_next
= m
->mp_parts
;
1314 move_preferred_part (CT ct
, char *type
, char *subtype
)
1316 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
1317 struct part
*part
, *prev
, *head
, *nhead
, *ntail
;
1321 /* move the matching part(s) to the head of the list: walk the
1322 * list of parts, move matching parts to a new list (maintaining
1323 * their order), and finally, concatenate the old list onto the
1330 head
->mp_next
= m
->mp_parts
;
1331 nhead
->mp_next
= NULL
;
1335 part
= head
->mp_next
;
1336 while (part
!= NULL
) {
1337 ci
= &part
->mp_part
->c_ctinfo
;
1338 if (!strcasecmp(ci
->ci_type
, type
) &&
1339 (!subtype
|| !strcasecmp(ci
->ci_subtype
, subtype
))) {
1340 prev
->mp_next
= part
->mp_next
;
1341 part
->mp_next
= NULL
;
1342 ntail
->mp_next
= part
;
1344 part
= prev
->mp_next
;
1347 part
= prev
->mp_next
;
1350 ntail
->mp_next
= head
->mp_next
;
1351 m
->mp_parts
= nhead
->mp_next
;
1356 * move parts that match the user's preferences (-prefer) to the head
1357 * of the line. process preferences in reverse so first one given
1358 * ends up first in line
1364 for (i
= npreferred
-1; i
>= 0; i
--)
1365 move_preferred_part(ct
, preferred_types
[i
], preferred_subtypes
[i
]);
1370 /* parse_mime() arranges alternates in reverse (priority) order. This
1371 function can be used to reverse them back. This will put, for
1372 example, a text/plain part before a text/html part in a
1373 multipart/alternative part, for example, where it belongs. */
1375 reverse_alternative_parts (CT ct
) {
1376 if (ct
->c_type
== CT_MULTIPART
) {
1377 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
1380 if (ct
->c_subtype
== MULTI_ALTERNATE
) {
1384 /* And call recursively on each part of a multipart. */
1385 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
1386 reverse_alternative_parts (part
->mp_part
);
1399 CI ci
= &ct
->c_ctinfo
;
1401 if ((ct
->c_encoding
!= CE_7BIT
) && (ct
->c_encoding
!= CE_8BIT
)) {
1402 inform("\"%s/%s\" type in message %s should be encoded in "
1403 "7bit or 8bit, continuing...", ci
->ci_type
, ci
->ci_subtype
,
1408 /* check for missing subtype */
1409 if (!*ci
->ci_subtype
)
1410 ci
->ci_subtype
= add ("rfc822", ci
->ci_subtype
);
1413 ct
->c_subtype
= ct_str_subtype (CT_MESSAGE
, ci
->ci_subtype
);
1415 switch (ct
->c_subtype
) {
1416 case MESSAGE_RFC822
:
1419 case MESSAGE_PARTIAL
:
1425 ct
->c_ctparams
= (void *) p
;
1427 /* scan for parameters "id", "number", and "total" */
1428 for (pm
= ci
->ci_first_pm
; pm
; pm
= pm
->pm_next
) {
1429 if (!strcasecmp (pm
->pm_name
, "id")) {
1430 p
->pm_partid
= add (pm
->pm_value
, NULL
);
1433 if (!strcasecmp (pm
->pm_name
, "number")) {
1434 if (sscanf (pm
->pm_value
, "%d", &p
->pm_partno
) != 1
1435 || p
->pm_partno
< 1) {
1437 inform("invalid %s parameter for \"%s/%s\" type in message %s's %s field",
1438 pm
->pm_name
, ci
->ci_type
, ci
->ci_subtype
,
1439 ct
->c_file
, TYPE_FIELD
);
1444 if (!strcasecmp (pm
->pm_name
, "total")) {
1445 if (sscanf (pm
->pm_value
, "%d", &p
->pm_maxno
) != 1
1454 || (p
->pm_maxno
&& p
->pm_partno
> p
->pm_maxno
)) {
1455 inform("invalid parameters for \"%s/%s\" type in message %s's %s field",
1456 ci
->ci_type
, ci
->ci_subtype
, ct
->c_file
, TYPE_FIELD
);
1462 case MESSAGE_EXTERNAL
:
1470 ct
->c_ctparams
= (void *) e
;
1473 && (ct
->c_fp
= fopen (ct
->c_file
, "r")) == NULL
) {
1474 advise (ct
->c_file
, "unable to open for reading");
1478 fseek (fp
= ct
->c_fp
, ct
->c_begin
, SEEK_SET
);
1480 if (!(p
= get_content (fp
, ct
->c_file
, 0))) {
1488 p
->c_ceopenfnx
= NULL
;
1489 if ((exresult
= params_external (ct
, 0)) != NOTOK
1490 && p
->c_ceopenfnx
== openMail
) {
1494 if ((size
= ct
->c_end
- p
->c_begin
) <= 0) {
1496 content_error (NULL
, ct
,
1497 "empty body for access-type=mail-server");
1501 e
->eb_body
= bp
= mh_xmalloc ((unsigned) size
);
1502 fseek (p
->c_fp
, p
->c_begin
, SEEK_SET
);
1504 switch (cc
= fread (bp
, sizeof(*bp
), size
, p
->c_fp
)) {
1506 adios ("failed", "fread");
1509 adios (NULL
, "unexpected EOF from fread");
1512 bp
+= cc
, size
-= cc
;
1519 p
->c_end
= p
->c_begin
;
1524 if (exresult
== NOTOK
)
1526 if (e
->eb_flags
== NOTOK
)
1529 switch (p
->c_type
) {
1534 if (p
->c_subtype
!= MESSAGE_RFC822
)
1538 e
->eb_partno
= ct
->c_partno
;
1540 (*p
->c_ctinitfnx
) (p
);
1555 params_external (CT ct
, int composing
)
1558 struct exbody
*e
= (struct exbody
*) ct
->c_ctparams
;
1559 CI ci
= &ct
->c_ctinfo
;
1561 ct
->c_ceopenfnx
= NULL
;
1562 for (pm
= ci
->ci_first_pm
; pm
; pm
= pm
->pm_next
) {
1563 if (!strcasecmp (pm
->pm_name
, "access-type")) {
1564 struct str2init
*s2i
;
1565 CT p
= e
->eb_content
;
1567 for (s2i
= str2methods
; s2i
->si_key
; s2i
++)
1568 if (!strcasecmp (pm
->pm_value
, s2i
->si_key
))
1571 e
->eb_access
= pm
->pm_value
;
1572 e
->eb_flags
= NOTOK
;
1573 p
->c_encoding
= CE_EXTERNAL
;
1576 e
->eb_access
= s2i
->si_key
;
1577 e
->eb_flags
= s2i
->si_val
;
1578 p
->c_encoding
= CE_EXTERNAL
;
1580 /* Call the Init function for this external type */
1581 if ((*s2i
->si_init
)(p
) == NOTOK
)
1585 if (!strcasecmp (pm
->pm_name
, "name")) {
1586 e
->eb_name
= pm
->pm_value
;
1589 if (!strcasecmp (pm
->pm_name
, "permission")) {
1590 e
->eb_permission
= pm
->pm_value
;
1593 if (!strcasecmp (pm
->pm_name
, "site")) {
1594 e
->eb_site
= pm
->pm_value
;
1597 if (!strcasecmp (pm
->pm_name
, "directory")) {
1598 e
->eb_dir
= pm
->pm_value
;
1601 if (!strcasecmp (pm
->pm_name
, "mode")) {
1602 e
->eb_mode
= pm
->pm_value
;
1605 if (!strcasecmp (pm
->pm_name
, "size")) {
1606 sscanf (pm
->pm_value
, "%lu", &e
->eb_size
);
1609 if (!strcasecmp (pm
->pm_name
, "server")) {
1610 e
->eb_server
= pm
->pm_value
;
1613 if (!strcasecmp (pm
->pm_name
, "subject")) {
1614 e
->eb_subject
= pm
->pm_value
;
1617 if (!strcasecmp (pm
->pm_name
, "url")) {
1619 * According to RFC 2017, we have to remove all whitespace from
1623 char *u
, *p
= pm
->pm_value
;
1624 e
->eb_url
= u
= mh_xmalloc(strlen(pm
->pm_value
) + 1);
1626 for (; *p
!= '\0'; p
++) {
1627 if (! isspace((unsigned char) *p
))
1634 if (composing
&& !strcasecmp (pm
->pm_name
, "body")) {
1635 e
->eb_body
= getcpy (pm
->pm_value
);
1640 if (!e
->eb_access
) {
1641 inform("invalid parameters for \"%s/%s\" type in message %s's %s field",
1642 ci
->ci_type
, ci
->ci_subtype
, ct
->c_file
, TYPE_FIELD
);
1655 InitApplication (CT ct
)
1657 CI ci
= &ct
->c_ctinfo
;
1660 ct
->c_subtype
= ct_str_subtype (CT_APPLICATION
, ci
->ci_subtype
);
1667 * TRANSFER ENCODINGS
1671 init_encoding (CT ct
, OpenCEFunc openfnx
)
1673 ct
->c_ceopenfnx
= openfnx
;
1674 ct
->c_ceclosefnx
= close_encoding
;
1675 ct
->c_cesizefnx
= size_encoding
;
1682 close_encoding (CT ct
)
1684 CE ce
= &ct
->c_cefile
;
1693 static unsigned long
1694 size_encoding (CT ct
)
1699 CE ce
= &ct
->c_cefile
;
1702 if (ce
->ce_fp
&& fstat (fileno (ce
->ce_fp
), &st
) != NOTOK
)
1703 return (long) st
.st_size
;
1706 if (stat (ce
->ce_file
, &st
) != NOTOK
)
1707 return (long) st
.st_size
;
1711 if (ct
->c_encoding
== CE_EXTERNAL
)
1712 return (ct
->c_end
- ct
->c_begin
);
1715 if ((fd
= (*ct
->c_ceopenfnx
) (ct
, &file
)) == NOTOK
)
1716 return (ct
->c_end
- ct
->c_begin
);
1718 if (fstat (fd
, &st
) != NOTOK
)
1719 size
= (long) st
.st_size
;
1723 (*ct
->c_ceclosefnx
) (ct
);
1735 return init_encoding (ct
, openBase64
);
1740 openBase64 (CT ct
, char **file
)
1743 int fd
, own_ct_fp
= 0;
1744 char *cp
, *buffer
= NULL
;
1745 /* sbeck -- handle suffixes */
1747 CE ce
= &ct
->c_cefile
;
1748 unsigned char *decoded
;
1750 unsigned char digest
[16];
1753 fseek (ce
->ce_fp
, 0L, SEEK_SET
);
1758 if ((ce
->ce_fp
= fopen (ce
->ce_file
, "r")) == NULL
) {
1759 content_error (ce
->ce_file
, ct
, "unable to fopen for reading");
1765 if (*file
== NULL
) {
1768 ce
->ce_file
= mh_xstrdup(*file
);
1772 /* sbeck@cise.ufl.edu -- handle suffixes */
1774 if ((cp
= context_find_by_type ("suffix", ci
->ci_type
, ci
->ci_subtype
))) {
1775 if (ce
->ce_unlink
) {
1776 /* Create temporary file with filename extension. */
1777 if ((ce
->ce_file
= m_mktemps(invo_name
, cp
, NULL
, NULL
)) == NULL
) {
1778 adios(NULL
, "unable to create temporary file in %s",
1782 ce
->ce_file
= add (cp
, ce
->ce_file
);
1784 } else if (*file
== NULL
) {
1786 if ((tempfile
= m_mktemp2(NULL
, invo_name
, NULL
, NULL
)) == NULL
) {
1787 adios(NULL
, "unable to create temporary file in %s",
1790 ce
->ce_file
= mh_xstrdup(tempfile
);
1793 if ((ce
->ce_fp
= fopen (ce
->ce_file
, "w+")) == NULL
) {
1794 content_error (ce
->ce_file
, ct
, "unable to fopen for reading/writing");
1798 if ((len
= ct
->c_end
- ct
->c_begin
) < 0)
1799 adios (NULL
, "internal error(1)");
1801 buffer
= mh_xmalloc (len
+ 1);
1804 if ((ct
->c_fp
= fopen (ct
->c_file
, "r")) == NULL
) {
1805 content_error (ct
->c_file
, ct
, "unable to open for reading");
1811 lseek (fd
= fileno (ct
->c_fp
), (off_t
) ct
->c_begin
, SEEK_SET
);
1814 switch (cc
= read (fd
, cp
, len
)) {
1816 content_error (ct
->c_file
, ct
, "error reading from");
1820 content_error (NULL
, ct
, "premature eof");
1831 /* decodeBase64() requires null-terminated input. */
1834 if (decodeBase64 (buffer
, &decoded
, &decoded_len
, ct
->c_type
== CT_TEXT
,
1835 ct
->c_digested
? digest
: NULL
) == OK
) {
1837 unsigned char *decoded_p
= decoded
;
1838 for (i
= 0; i
< decoded_len
; ++i
) {
1839 putc (*decoded_p
++, ce
->ce_fp
);
1842 if (ferror (ce
->ce_fp
)) {
1843 content_error (ce
->ce_file
, ct
, "error writing to");
1847 if (ct
->c_digested
) {
1848 if (memcmp(digest
, ct
->c_digest
,
1849 sizeof(digest
) / sizeof(digest
[0]))) {
1850 content_error (NULL
, ct
,
1851 "content integrity suspect (digest mismatch) -- continuing");
1854 fprintf (stderr
, "content integrity confirmed\n");
1862 fseek (ct
->c_fp
, 0L, SEEK_SET
);
1864 if (fflush (ce
->ce_fp
)) {
1865 content_error (ce
->ce_file
, ct
, "error writing to");
1869 fseek (ce
->ce_fp
, 0L, SEEK_SET
);
1872 *file
= ce
->ce_file
;
1878 return fileno (ce
->ce_fp
);
1885 free_encoding (ct
, 0);
1895 static char hex2nib
[0x80] = {
1896 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1897 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1898 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1899 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1900 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1901 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1902 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
1903 0x08, 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1904 0x00, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x00,
1905 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1906 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1907 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1908 0x00, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 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
1918 return init_encoding (ct
, openQuoted
);
1923 openQuoted (CT ct
, char **file
)
1925 int cc
, digested
, len
, quoted
, own_ct_fp
= 0;
1931 CE ce
= &ct
->c_cefile
;
1932 /* sbeck -- handle suffixes */
1937 fseek (ce
->ce_fp
, 0L, SEEK_SET
);
1942 if ((ce
->ce_fp
= fopen (ce
->ce_file
, "r")) == NULL
) {
1943 content_error (ce
->ce_file
, ct
, "unable to fopen for reading");
1949 if (*file
== NULL
) {
1952 ce
->ce_file
= mh_xstrdup(*file
);
1956 /* sbeck@cise.ufl.edu -- handle suffixes */
1958 if ((cp
= context_find_by_type ("suffix", ci
->ci_type
, ci
->ci_subtype
))) {
1959 if (ce
->ce_unlink
) {
1960 /* Create temporary file with filename extension. */
1961 if ((ce
->ce_file
= m_mktemps(invo_name
, cp
, NULL
, NULL
)) == NULL
) {
1962 adios(NULL
, "unable to create temporary file in %s",
1966 ce
->ce_file
= add (cp
, ce
->ce_file
);
1968 } else if (*file
== NULL
) {
1970 if ((tempfile
= m_mktemp2(NULL
, invo_name
, NULL
, NULL
)) == NULL
) {
1971 adios(NULL
, "unable to create temporary file in %s",
1974 ce
->ce_file
= mh_xstrdup(tempfile
);
1977 if ((ce
->ce_fp
= fopen (ce
->ce_file
, "w+")) == NULL
) {
1978 content_error (ce
->ce_file
, ct
, "unable to fopen for reading/writing");
1982 if ((len
= ct
->c_end
- ct
->c_begin
) < 0)
1983 adios (NULL
, "internal error(2)");
1986 if ((ct
->c_fp
= fopen (ct
->c_file
, "r")) == NULL
) {
1987 content_error (ct
->c_file
, ct
, "unable to open for reading");
1993 if ((digested
= ct
->c_digested
))
1994 MD5Init (&mdContext
);
2001 fseek (ct
->c_fp
, ct
->c_begin
, SEEK_SET
);
2003 if ((gotlen
= getline(&bufp
, &buflen
, ct
->c_fp
)) == -1) {
2004 content_error (NULL
, ct
, "premature eof");
2008 if ((cc
= gotlen
) > len
)
2012 for (ep
= (cp
= bufp
) + cc
- 1; cp
<= ep
; ep
--)
2013 if (!isspace ((unsigned char) *ep
))
2018 for (; cp
< ep
; cp
++) {
2020 /* in an escape sequence */
2022 /* at byte 1 of an escape sequence */
2023 mask
= hex2nib
[((unsigned char) *cp
) & 0x7f];
2024 /* next is byte 2 */
2027 /* at byte 2 of an escape sequence */
2029 mask
|= hex2nib
[((unsigned char) *cp
) & 0x7f];
2030 putc (mask
, ce
->ce_fp
);
2032 MD5Update (&mdContext
, &mask
, 1);
2033 if (ferror (ce
->ce_fp
)) {
2034 content_error (ce
->ce_file
, ct
, "error writing to");
2037 /* finished escape sequence; next may be literal or a new
2038 * escape sequence */
2041 /* on to next byte */
2045 /* not in an escape sequence */
2047 /* starting an escape sequence, or invalid '='? */
2048 if (cp
+ 1 < ep
&& cp
[1] == '\n') {
2049 /* "=\n" soft line break, eat the \n */
2053 if (cp
+ 1 >= ep
|| cp
+ 2 >= ep
) {
2054 /* We don't have 2 bytes left, so this is an invalid
2055 * escape sequence; just show the raw bytes (below). */
2056 } else if (isxdigit ((unsigned char) cp
[1]) &&
2057 isxdigit ((unsigned char) cp
[2])) {
2058 /* Next 2 bytes are hex digits, making this a valid escape
2059 * sequence; let's decode it (above). */
2063 /* One or both of the next 2 is out of range, making this
2064 * an invalid escape sequence; just show the raw bytes
2068 /* Just show the raw byte. */
2069 putc (*cp
, ce
->ce_fp
);
2072 MD5Update (&mdContext
, (unsigned char *) "\r\n",2);
2074 MD5Update (&mdContext
, (unsigned char *) cp
, 1);
2077 if (ferror (ce
->ce_fp
)) {
2078 content_error (ce
->ce_file
, ct
, "error writing to");
2084 content_error (NULL
, ct
,
2085 "invalid QUOTED-PRINTABLE encoding -- end-of-content while still quoting");
2089 fseek (ct
->c_fp
, 0L, SEEK_SET
);
2091 if (fflush (ce
->ce_fp
)) {
2092 content_error (ce
->ce_file
, ct
, "error writing to");
2097 unsigned char digest
[16];
2099 MD5Final (digest
, &mdContext
);
2100 if (memcmp((char *) digest
, (char *) ct
->c_digest
,
2101 sizeof(digest
) / sizeof(digest
[0])))
2102 content_error (NULL
, ct
,
2103 "content integrity suspect (digest mismatch) -- continuing");
2106 fprintf (stderr
, "content integrity confirmed\n");
2109 fseek (ce
->ce_fp
, 0L, SEEK_SET
);
2112 *file
= ce
->ce_file
;
2118 return fileno (ce
->ce_fp
);
2121 free_encoding (ct
, 0);
2138 if (init_encoding (ct
, open7Bit
) == NOTOK
)
2141 ct
->c_cesizefnx
= NULL
; /* no need to decode for real size */
2147 open7Bit (CT ct
, char **file
)
2149 int cc
, fd
, len
, own_ct_fp
= 0;
2150 char buffer
[BUFSIZ
];
2151 /* sbeck -- handle suffixes */
2154 CE ce
= &ct
->c_cefile
;
2157 fseek (ce
->ce_fp
, 0L, SEEK_SET
);
2162 if ((ce
->ce_fp
= fopen (ce
->ce_file
, "r")) == NULL
) {
2163 content_error (ce
->ce_file
, ct
, "unable to fopen for reading");
2169 if (*file
== NULL
) {
2172 ce
->ce_file
= mh_xstrdup(*file
);
2176 /* sbeck@cise.ufl.edu -- handle suffixes */
2178 if ((cp
= context_find_by_type ("suffix", ci
->ci_type
, ci
->ci_subtype
))) {
2179 if (ce
->ce_unlink
) {
2180 /* Create temporary file with filename extension. */
2181 if ((ce
->ce_file
= m_mktemps(invo_name
, cp
, NULL
, NULL
)) == NULL
) {
2182 adios(NULL
, "unable to create temporary file in %s",
2186 ce
->ce_file
= add (cp
, ce
->ce_file
);
2188 } else if (*file
== NULL
) {
2190 if ((tempfile
= m_mktemp2(NULL
, invo_name
, NULL
, NULL
)) == NULL
) {
2191 adios(NULL
, "unable to create temporary file in %s",
2194 ce
->ce_file
= mh_xstrdup(tempfile
);
2197 if ((ce
->ce_fp
= fopen (ce
->ce_file
, "w+")) == NULL
) {
2198 content_error (ce
->ce_file
, ct
, "unable to fopen for reading/writing");
2202 if (ct
->c_type
== CT_MULTIPART
) {
2203 CI ci
= &ct
->c_ctinfo
;
2207 fprintf (ce
->ce_fp
, "%s: %s/%s", TYPE_FIELD
, ci
->ci_type
, ci
->ci_subtype
);
2208 len
+= strlen (TYPE_FIELD
) + 2 + strlen (ci
->ci_type
)
2209 + 1 + strlen (ci
->ci_subtype
);
2210 buffer
= output_params(len
, ci
->ci_first_pm
, &len
, 0);
2213 fputs (buffer
, ce
->ce_fp
);
2217 if (ci
->ci_comment
) {
2218 if (len
+ 1 + (cc
= 2 + strlen (ci
->ci_comment
)) >= CPERLIN
) {
2219 fputs ("\n\t", ce
->ce_fp
);
2223 putc (' ', ce
->ce_fp
);
2226 fprintf (ce
->ce_fp
, "(%s)", ci
->ci_comment
);
2229 fprintf (ce
->ce_fp
, "\n");
2231 fprintf (ce
->ce_fp
, "%s:%s", ID_FIELD
, ct
->c_id
);
2233 fprintf (ce
->ce_fp
, "%s:%s", DESCR_FIELD
, ct
->c_descr
);
2235 fprintf (ce
->ce_fp
, "%s:%s", DISPO_FIELD
, ct
->c_dispo
);
2236 fprintf (ce
->ce_fp
, "\n");
2239 if ((len
= ct
->c_end
- ct
->c_begin
) < 0)
2240 adios (NULL
, "internal error(3)");
2243 if ((ct
->c_fp
= fopen (ct
->c_file
, "r")) == NULL
) {
2244 content_error (ct
->c_file
, ct
, "unable to open for reading");
2250 lseek (fd
= fileno (ct
->c_fp
), (off_t
) ct
->c_begin
, SEEK_SET
);
2252 switch (cc
= read (fd
, buffer
, sizeof(buffer
) - 1)) {
2254 content_error (ct
->c_file
, ct
, "error reading from");
2258 content_error (NULL
, ct
, "premature eof");
2266 if ((int) fwrite (buffer
, sizeof(*buffer
), cc
, ce
->ce_fp
) < cc
) {
2267 advise ("open7Bit", "fwrite");
2269 if (ferror (ce
->ce_fp
)) {
2270 content_error (ce
->ce_file
, ct
, "error writing to");
2275 fseek (ct
->c_fp
, 0L, SEEK_SET
);
2277 if (fflush (ce
->ce_fp
)) {
2278 content_error (ce
->ce_file
, ct
, "error writing to");
2282 fseek (ce
->ce_fp
, 0L, SEEK_SET
);
2285 *file
= ce
->ce_file
;
2290 return fileno (ce
->ce_fp
);
2293 free_encoding (ct
, 0);
2307 openExternal (CT ct
, CT cb
, CE ce
, char **file
, int *fd
)
2309 char cachefile
[BUFSIZ
];
2312 fseek (ce
->ce_fp
, 0L, SEEK_SET
);
2317 if ((ce
->ce_fp
= fopen (ce
->ce_file
, "r")) == NULL
) {
2318 content_error (ce
->ce_file
, ct
, "unable to fopen for reading");
2324 if (find_cache (ct
, rcachesw
, (int *) 0, cb
->c_id
,
2325 cachefile
, sizeof(cachefile
)) != NOTOK
) {
2326 if ((ce
->ce_fp
= fopen (cachefile
, "r"))) {
2327 ce
->ce_file
= mh_xstrdup(cachefile
);
2331 admonish (cachefile
, "unable to fopen for reading");
2334 *fd
= fileno (ce
->ce_fp
);
2338 *file
= ce
->ce_file
;
2339 *fd
= fileno (ce
->ce_fp
);
2350 return init_encoding (ct
, openFile
);
2355 openFile (CT ct
, char **file
)
2358 char cachefile
[BUFSIZ
];
2359 struct exbody
*e
= ct
->c_ctexbody
;
2360 CE ce
= &ct
->c_cefile
;
2362 switch (openExternal (e
->eb_parent
, e
->eb_content
, ce
, file
, &fd
)) {
2374 content_error (NULL
, ct
, "missing name parameter");
2378 ce
->ce_file
= mh_xstrdup(e
->eb_name
);
2381 if ((ce
->ce_fp
= fopen (ce
->ce_file
, "r")) == NULL
) {
2382 content_error (ce
->ce_file
, ct
, "unable to fopen for reading");
2386 if ((!e
->eb_permission
|| strcasecmp (e
->eb_permission
, "read-write"))
2387 && find_cache (NULL
, wcachesw
, &cachetype
, e
->eb_content
->c_id
,
2388 cachefile
, sizeof(cachefile
)) != NOTOK
) {
2392 mask
= umask (cachetype
? ~m_gmprot () : 0222);
2393 if ((fp
= fopen (cachefile
, "w"))) {
2395 char buffer
[BUFSIZ
];
2396 FILE *gp
= ce
->ce_fp
;
2398 fseek (gp
, 0L, SEEK_SET
);
2400 while ((cc
= fread (buffer
, sizeof(*buffer
), sizeof(buffer
), gp
))
2402 if ((int) fwrite (buffer
, sizeof(*buffer
), cc
, fp
) < cc
) {
2403 advise ("openFile", "fwrite");
2408 admonish (ce
->ce_file
, "error reading");
2409 (void) m_unlink (cachefile
);
2413 admonish (cachefile
, "error writing");
2414 (void) m_unlink (cachefile
);
2421 fseek (ce
->ce_fp
, 0L, SEEK_SET
);
2422 *file
= ce
->ce_file
;
2423 return fileno (ce
->ce_fp
);
2433 return init_encoding (ct
, openFTP
);
2438 openFTP (CT ct
, char **file
)
2440 int cachetype
, caching
, fd
;
2442 char *bp
, *ftp
, *user
, *pass
;
2443 char buffer
[BUFSIZ
], cachefile
[BUFSIZ
];
2445 CE ce
= &ct
->c_cefile
;
2446 static char *username
= NULL
;
2447 static char *password
= NULL
;
2451 if ((ftp
= context_find (nmhaccessftp
)) && !*ftp
)
2457 switch (openExternal (e
->eb_parent
, e
->eb_content
, ce
, file
, &fd
)) {
2468 if (!e
->eb_name
|| !e
->eb_site
) {
2469 content_error (NULL
, ct
, "missing %s parameter",
2470 e
->eb_name
? "site": "name");
2474 /* Get the buffer ready to go */
2476 buflen
= sizeof(buffer
);
2479 * Construct the query message for user
2481 snprintf (bp
, buflen
, "Retrieve %s", e
->eb_name
);
2487 snprintf (bp
, buflen
, " (content %s)", e
->eb_partno
);
2493 snprintf (bp
, buflen
, "\n using %sFTP from site %s",
2494 e
->eb_flags
? "anonymous " : "", e
->eb_site
);
2499 if (e
->eb_size
> 0) {
2500 snprintf (bp
, buflen
, " (%lu octets)", e
->eb_size
);
2505 snprintf (bp
, buflen
, "? ");
2508 * Now, check the answer
2510 if (!read_yes_or_no_if_tty (buffer
))
2515 snprintf (buffer
, sizeof(buffer
), "%s@%s", getusername (),
2519 ruserpass (e
->eb_site
, &username
, &password
, 0);
2524 ce
->ce_unlink
= (*file
== NULL
);
2526 cachefile
[0] = '\0';
2527 if ((!e
->eb_permission
|| strcasecmp (e
->eb_permission
, "read-write"))
2528 && find_cache (NULL
, wcachesw
, &cachetype
, e
->eb_content
->c_id
,
2529 cachefile
, sizeof(cachefile
)) != NOTOK
) {
2530 if (*file
== NULL
) {
2537 ce
->ce_file
= mh_xstrdup(*file
);
2539 ce
->ce_file
= mh_xstrdup(cachefile
);
2542 if ((tempfile
= m_mktemp2(NULL
, invo_name
, NULL
, NULL
)) == NULL
) {
2543 adios(NULL
, "unable to create temporary file in %s",
2546 ce
->ce_file
= mh_xstrdup(tempfile
);
2549 if ((ce
->ce_fp
= fopen (ce
->ce_file
, "w+")) == NULL
) {
2550 content_error (ce
->ce_file
, ct
, "unable to fopen for reading/writing");
2555 int child_id
, i
, vecp
;
2559 vec
[vecp
++] = r1bindex (ftp
, '/');
2560 vec
[vecp
++] = e
->eb_site
;
2563 vec
[vecp
++] = e
->eb_dir
;
2564 vec
[vecp
++] = e
->eb_name
;
2565 vec
[vecp
++] = ce
->ce_file
,
2566 vec
[vecp
++] = e
->eb_mode
&& !strcasecmp (e
->eb_mode
, "ascii")
2567 ? "ascii" : "binary";
2572 for (i
= 0; (child_id
= fork()) == NOTOK
&& i
< 5; i
++)
2576 adios ("fork", "unable to");
2580 close (fileno (ce
->ce_fp
));
2582 fprintf (stderr
, "unable to exec ");
2588 if (pidXwait (child_id
, NULL
)) {
2589 username
= password
= NULL
;
2599 chmod (cachefile
, cachetype
? m_gmprot () : 0444);
2604 mask
= umask (cachetype
? ~m_gmprot () : 0222);
2605 if ((fp
= fopen (cachefile
, "w"))) {
2607 FILE *gp
= ce
->ce_fp
;
2609 fseek (gp
, 0L, SEEK_SET
);
2611 while ((cc
= fread (buffer
, sizeof(*buffer
), sizeof(buffer
), gp
))
2613 if ((int) fwrite (buffer
, sizeof(*buffer
), cc
, fp
) < cc
) {
2614 advise ("openFTP", "fwrite");
2619 admonish (ce
->ce_file
, "error reading");
2620 (void) m_unlink (cachefile
);
2624 admonish (cachefile
, "error writing");
2625 (void) m_unlink (cachefile
);
2633 fseek (ce
->ce_fp
, 0L, SEEK_SET
);
2634 *file
= ce
->ce_file
;
2635 return fileno (ce
->ce_fp
);
2646 return init_encoding (ct
, openMail
);
2651 openMail (CT ct
, char **file
)
2653 int child_id
, fd
, i
, vecp
;
2655 char *bp
, buffer
[BUFSIZ
], *vec
[7];
2656 struct exbody
*e
= ct
->c_ctexbody
;
2657 CE ce
= &ct
->c_cefile
;
2659 switch (openExternal (e
->eb_parent
, e
->eb_content
, ce
, file
, &fd
)) {
2670 if (!e
->eb_server
) {
2671 content_error (NULL
, ct
, "missing server parameter");
2675 /* Get buffer ready to go */
2677 buflen
= sizeof(buffer
);
2679 /* Now, construct query message */
2680 snprintf (bp
, buflen
, "Retrieve content");
2686 snprintf (bp
, buflen
, " %s", e
->eb_partno
);
2692 snprintf (bp
, buflen
, " by asking %s\n\n%s\n? ",
2694 e
->eb_subject
? e
->eb_subject
: e
->eb_body
);
2696 /* Now, check answer */
2697 if (!read_yes_or_no_if_tty (buffer
))
2701 vec
[vecp
++] = r1bindex (mailproc
, '/');
2702 vec
[vecp
++] = e
->eb_server
;
2703 vec
[vecp
++] = "-subject";
2704 vec
[vecp
++] = e
->eb_subject
? e
->eb_subject
: "mail-server request";
2705 vec
[vecp
++] = "-body";
2706 vec
[vecp
++] = e
->eb_body
;
2709 for (i
= 0; (child_id
= fork()) == NOTOK
&& i
< 5; i
++)
2713 advise ("fork", "unable to");
2717 execvp (mailproc
, vec
);
2718 fprintf (stderr
, "unable to exec ");
2724 if (pidXwait (child_id
, NULL
) == OK
)
2725 inform("request sent");
2729 if (*file
== NULL
) {
2731 if ((tempfile
= m_mktemp2(NULL
, invo_name
, NULL
, NULL
)) == NULL
) {
2732 adios(NULL
, "unable to create temporary file in %s",
2735 ce
->ce_file
= mh_xstrdup(tempfile
);
2738 ce
->ce_file
= mh_xstrdup(*file
);
2742 if ((ce
->ce_fp
= fopen (ce
->ce_file
, "w+")) == NULL
) {
2743 content_error (ce
->ce_file
, ct
, "unable to fopen for reading/writing");
2747 /* showproc is for mhshow and mhstore, though mhlist -debug
2748 * prints it, too. */
2749 mh_xfree(ct
->c_showproc
);
2750 ct
->c_showproc
= mh_xstrdup("true");
2752 fseek (ce
->ce_fp
, 0L, SEEK_SET
);
2753 *file
= ce
->ce_file
;
2754 return fileno (ce
->ce_fp
);
2765 return init_encoding (ct
, openURL
);
2770 openURL (CT ct
, char **file
)
2772 struct exbody
*e
= ct
->c_ctexbody
;
2773 CE ce
= &ct
->c_cefile
;
2774 char *urlprog
, *program
;
2775 char buffer
[BUFSIZ
], cachefile
[BUFSIZ
];
2776 int fd
, caching
, cachetype
;
2777 struct msgs_array args
= { 0, 0, NULL
};
2780 if ((urlprog
= context_find(nmhaccessurl
)) && *urlprog
== '\0')
2784 content_error(NULL
, ct
, "No entry for nmh-access-url in profile");
2788 switch (openExternal(e
->eb_parent
, e
->eb_content
, ce
, file
, &fd
)) {
2800 content_error(NULL
, ct
, "missing url parameter");
2804 ce
->ce_unlink
= (*file
== NULL
);
2806 cachefile
[0] = '\0';
2808 if (find_cache(NULL
, wcachesw
, &cachetype
, e
->eb_content
->c_id
,
2809 cachefile
, sizeof(cachefile
)) != NOTOK
) {
2810 if (*file
== NULL
) {
2817 ce
->ce_file
= mh_xstrdup(*file
);
2819 ce
->ce_file
= mh_xstrdup(cachefile
);
2822 if ((tempfile
= m_mktemp2(NULL
, invo_name
, NULL
, NULL
)) == NULL
) {
2823 adios(NULL
, "unable to create temporary file in %s",
2826 ce
->ce_file
= mh_xstrdup(tempfile
);
2829 if ((ce
->ce_fp
= fopen(ce
->ce_file
, "w+")) == NULL
) {
2830 content_error(ce
->ce_file
, ct
, "unable to fopen for read/writing");
2834 switch (child_id
= fork()) {
2836 adios ("fork", "unable to");
2840 argsplit_msgarg(&args
, urlprog
, &program
);
2841 app_msgarg(&args
, e
->eb_url
);
2842 app_msgarg(&args
, NULL
);
2843 dup2(fileno(ce
->ce_fp
), 1);
2844 close(fileno(ce
->ce_fp
));
2845 execvp(program
, args
.msgs
);
2846 fprintf(stderr
, "Unable to exec ");
2852 if (pidXwait(child_id
, NULL
)) {
2860 chmod(cachefile
, cachetype
? m_gmprot() : 0444);
2865 mask
= umask (cachetype
? ~m_gmprot() : 0222);
2866 if ((fp
= fopen(cachefile
, "w"))) {
2868 FILE *gp
= ce
->ce_fp
;
2870 fseeko(gp
, 0, SEEK_SET
);
2872 while ((cc
= fread(buffer
, sizeof(*buffer
),
2873 sizeof(buffer
), gp
)) > 0)
2874 if ((int) fwrite(buffer
, sizeof(*buffer
), cc
, fp
) < cc
) {
2875 advise ("openURL", "fwrite");
2881 admonish(ce
->ce_file
, "error reading");
2882 (void) m_unlink (cachefile
);
2889 fseeko(ce
->ce_fp
, 0, SEEK_SET
);
2890 *file
= ce
->ce_file
;
2896 * Stores MD5 digest (in cp, from Content-MD5 header) in ct->c_digest. It
2897 * has to be base64 decoded.
2900 readDigest (CT ct
, char *cp
)
2902 unsigned char *digest
;
2905 if (decodeBase64 (cp
, &digest
, &len
, 0, NULL
) == OK
) {
2906 const size_t maxlen
= sizeof ct
->c_digest
/ sizeof ct
->c_digest
[0];
2908 if (strlen ((char *) digest
) <= maxlen
) {
2909 memcpy (ct
->c_digest
, digest
, maxlen
);
2914 fprintf (stderr
, "MD5 digest=");
2915 for (i
= 0; i
< maxlen
; ++i
) {
2916 fprintf (stderr
, "%02x", ct
->c_digest
[i
] & 0xff);
2918 fprintf (stderr
, "\n");
2924 fprintf (stderr
, "invalid MD5 digest (got %d octets)\n",
2925 (int) strlen ((char *) digest
));
2935 /* Multipart parts might have content before the first subpart and/or
2936 after the last subpart that hasn't been stored anywhere else, so do
2939 get_leftover_mp_content (CT ct
, int before
/* or after */)
2941 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
2943 int found_boundary
= 0;
2949 char *content
= NULL
;
2951 if (! m
) return NOTOK
;
2954 if (! m
->mp_parts
|| ! m
->mp_parts
->mp_part
) return NOTOK
;
2956 /* Isolate the beginning of this part to the beginning of the
2957 first subpart and save any content between them. */
2958 fseeko (ct
->c_fp
, ct
->c_begin
, SEEK_SET
);
2959 max
= m
->mp_parts
->mp_part
->c_begin
- ct
->c_begin
;
2960 boundary
= concat ("--", m
->mp_start
, NULL
);
2962 struct part
*last_subpart
= NULL
;
2963 struct part
*subpart
;
2965 /* Go to the last subpart to get its end position. */
2966 for (subpart
= m
->mp_parts
; subpart
; subpart
= subpart
->mp_next
) {
2967 last_subpart
= subpart
;
2970 if (last_subpart
== NULL
) return NOTOK
;
2972 /* Isolate the end of the last subpart to the end of this part
2973 and save any content between them. */
2974 fseeko (ct
->c_fp
, last_subpart
->mp_part
->c_end
, SEEK_SET
);
2975 max
= ct
->c_end
- last_subpart
->mp_part
->c_end
;
2976 boundary
= concat ("--", m
->mp_stop
, NULL
);
2979 /* Back up by 1 to pick up the newline. */
2980 while ((gotlen
= getline(&bufp
, &buflen
, ct
->c_fp
)) != -1) {
2982 /* Don't look beyond beginning of first subpart (before) or
2983 next part (after). */
2984 if (read
> max
) bufp
[read
-max
] = '\0';
2987 if (! strcmp (bufp
, boundary
)) {
2991 if (! found_boundary
&& ! strcmp (bufp
, boundary
)) {
2997 if ((before
&& ! found_boundary
) || (! before
&& found_boundary
)) {
2999 char *old_content
= content
;
3000 content
= concat (content
, bufp
, NULL
);
3004 ? concat ("\n", bufp
, NULL
)
3005 : concat (bufp
, NULL
);
3010 if (found_boundary
|| read
> max
) break;
3012 if (read
> max
) break;
3016 /* Skip the newline if that's all there is. */
3020 /* Remove trailing newline, except at EOF. */
3021 if ((before
|| ! feof (ct
->c_fp
)) &&
3022 (cp
= content
+ strlen (content
)) > content
&&
3027 if (strlen (content
) > 1) {
3029 m
->mp_content_before
= content
;
3031 m
->mp_content_after
= content
;
3046 ct_type_str (int type
) {
3048 case CT_APPLICATION
:
3049 return "application";
3065 return "unknown_type";
3071 ct_subtype_str (int type
, int subtype
) {
3073 case CT_APPLICATION
:
3075 case APPLICATION_OCTETS
:
3077 case APPLICATION_POSTSCRIPT
:
3078 return "postscript";
3080 return "unknown_app_subtype";
3084 case MESSAGE_RFC822
:
3086 case MESSAGE_PARTIAL
:
3088 case MESSAGE_EXTERNAL
:
3091 return "unknown_msg_subtype";
3097 case MULTI_ALTERNATE
:
3098 return "alternative";
3101 case MULTI_PARALLEL
:
3106 return "unknown_multipart_subtype";
3117 return "unknown_text_subtype";
3120 return "unknown_type";
3126 ct_str_type (const char *type
) {
3127 struct str2init
*s2i
;
3129 for (s2i
= str2cts
; s2i
->si_key
; ++s2i
) {
3130 if (! strcasecmp (type
, s2i
->si_key
)) {
3134 if (! s2i
->si_key
&& ! uprf (type
, "X-")) {
3143 ct_str_subtype (int type
, const char *subtype
) {
3147 case CT_APPLICATION
:
3148 for (kv
= SubApplication
; kv
->kv_key
; ++kv
) {
3149 if (! strcasecmp (subtype
, kv
->kv_key
)) {
3153 return kv
->kv_value
;
3155 for (kv
= SubMessage
; kv
->kv_key
; ++kv
) {
3156 if (! strcasecmp (subtype
, kv
->kv_key
)) {
3160 return kv
->kv_value
;
3162 for (kv
= SubMultiPart
; kv
->kv_key
; ++kv
) {
3163 if (! strcasecmp (subtype
, kv
->kv_key
)) {
3167 return kv
->kv_value
;
3169 for (kv
= SubText
; kv
->kv_key
; ++kv
) {
3170 if (! strcasecmp (subtype
, kv
->kv_key
)) {
3174 return kv
->kv_value
;
3181 /* Find the content type and InitFunc for the CT. */
3182 const struct str2init
*
3183 get_ct_init (int type
) {
3184 const struct str2init
*sp
;
3186 for (sp
= str2cts
; sp
->si_key
; ++sp
) {
3187 if (type
== sp
->si_val
) {
3196 ce_str (int encoding
) {
3201 return "quoted-printable";
3217 /* Find the content type and InitFunc for the content encoding method. */
3218 const struct str2init
*
3219 get_ce_method (const char *method
) {
3220 struct str2init
*sp
;
3222 for (sp
= str2ces
; sp
->si_key
; ++sp
) {
3223 if (! strcasecmp (method
, sp
->si_key
)) {
3232 * Parse a series of MIME attributes (or parameters) given a header as
3235 * Arguments include:
3237 * filename - Name of input file (for error messages)
3238 * fieldname - Name of field being processed
3239 * headerp - Pointer to pointer of the beginning of the MIME attributes.
3240 * Updated to point to end of attributes when finished.
3241 * param_head - Pointer to head of parameter list
3242 * param_tail - Pointer to tail of parameter list
3243 * commentp - Pointer to header comment pointer (may be NULL)
3245 * Returns OK if parsing was successful, NOTOK if parsing failed, and
3246 * DONE to indicate a benign error (minor parsing error, but the program
3251 parse_header_attrs (const char *filename
, const char *fieldname
,
3252 char **header_attrp
, PM
*param_head
, PM
*param_tail
,
3255 char *cp
= *header_attrp
;
3261 struct sectlist
*next
;
3267 struct sectlist
*sechead
;
3268 struct parmlist
*next
;
3269 } *pp
, *pp2
, *phead
= NULL
;
3271 while (*cp
== ';') {
3272 char *dp
, *vp
, *up
, *nameptr
, *valptr
, *charset
= NULL
, *lang
= NULL
;
3273 int encoded
= 0, partial
= 0, len
= 0, index
= 0;
3276 while (isspace ((unsigned char) *cp
))
3280 get_comment (filename
, fieldname
, &cp
, commentp
) == NOTOK
) {
3285 if (! suppress_extraneous_trailing_semicolon_warning
) {
3286 inform("extraneous trailing ';' in message %s's %s: "
3287 "parameter list", filename
, fieldname
);
3292 /* down case the attribute name */
3293 for (dp
= cp
; istoken ((unsigned char) *dp
); dp
++)
3294 *dp
= tolower ((unsigned char) *dp
);
3296 for (up
= dp
; isspace ((unsigned char) *dp
);)
3298 if (dp
== cp
|| *dp
!= '=') {
3299 inform("invalid parameter in message %s's %s: "
3300 "field\n%*sparameter %s (error detected at offset %d)",
3301 filename
, fieldname
, strlen(invo_name
) + 2, "",cp
, dp
- cp
);
3306 * To handle RFC 2231, we have to deal with the following extensions:
3308 * name*=encoded-value
3309 * name*<N>=part-N-of-a-parameter-value
3310 * name*<N>*=encoded-part-N-of-a-parameter-value
3313 * If there's a * right before the equal sign, it's encoded.
3314 * If there's a * and one or more digits, then it's section N.
3316 * Remember we can have one or the other, or both. cp points to
3317 * beginning of name, up points past the last character in the
3321 for (vp
= cp
; vp
< up
; vp
++) {
3322 if (*vp
== '*' && vp
< up
- 1) {
3326 if (*vp
== '*' && vp
== up
- 1) {
3328 } else if (partial
) {
3329 if (isdigit((unsigned char) *vp
))
3330 index
= *vp
- '0' + index
* 10;
3332 inform("invalid parameter index in message %s's "
3333 "%s: field\n%*s(parameter %s)", filename
,
3334 fieldname
, strlen(invo_name
) + 2, "", cp
);
3343 * Break out the parameter name and value sections and allocate
3347 nameptr
= mh_xmalloc(len
+ 1);
3348 strncpy(nameptr
, cp
, len
);
3349 nameptr
[len
] = '\0';
3351 for (dp
++; isspace ((unsigned char) *dp
);)
3356 * Single quotes delimit the character set and language tag.
3357 * They are required on the first section (or a complete
3362 while (*vp
!= '\'' && !isspace((unsigned char) *vp
) &&
3368 charset
= mh_xmalloc(len
+ 1);
3369 strncpy(charset
, dp
, len
);
3370 charset
[len
] = '\0';
3376 inform("missing charset in message %s's %s: "
3377 "field\n%*s(parameter %s)", filename
, fieldname
,
3378 strlen(invo_name
) + 2, "", nameptr
);
3384 while (*vp
!= '\'' && !isspace((unsigned char) *vp
) &&
3391 lang
= mh_xmalloc(len
+ 1);
3392 strncpy(lang
, dp
, len
);
3399 inform("missing language tag in message %s's %s: "
3400 "field\n%*s(parameter %s)", filename
, fieldname
,
3401 strlen(invo_name
) + 2, "", nameptr
);
3411 * At this point vp should be pointing at the beginning
3412 * of the encoded value/section. Continue until we reach
3413 * the end or get whitespace. But first, calculate the
3414 * length so we can allocate the correct buffer size.
3417 for (vp
= dp
, len
= 0; istoken(*vp
); vp
++) {
3419 if (*(vp
+ 1) == '\0' ||
3420 !isxdigit((unsigned char) *(vp
+ 1)) ||
3421 *(vp
+ 2) == '\0' ||
3422 !isxdigit((unsigned char) *(vp
+ 2))) {
3423 inform("invalid encoded sequence in message "
3424 "%s's %s: field\n%*s(parameter %s)",
3425 filename
, fieldname
, strlen(invo_name
) + 2,
3437 up
= valptr
= mh_xmalloc(len
+ 1);
3439 for (vp
= dp
; istoken(*vp
); vp
++) {
3441 *up
++ = decode_qp(*(vp
+ 1), *(vp
+ 2));
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
3462 for (cp
= dp
+ 1;;) {
3466 inform("invalid quoted-string in message %s's %s: "
3467 "field\n%*s(parameter %s)", filename
,
3468 fieldname
, strlen(invo_name
) + 2, "", nameptr
);
3488 for (cp
= dp
; istoken (*cp
); cp
++) {
3493 valptr
= mh_xmalloc(len
+ 1);
3497 for (cp
= dp
+ 1, vp
= valptr
, i
= 0; i
< len
; i
++) {
3505 strncpy(valptr
, cp
= dp
, len
);
3513 * If 'partial' is set, we don't allocate a parameter now. We
3514 * put it on the parameter linked list to be reassembled later.
3516 * "phead" points to a list of all parameters we need to reassemble.
3517 * Each parameter has a list of sections. We insert the sections in
3522 for (pp
= phead
; pp
!= NULL
; pp
= pp
->next
) {
3523 if (strcasecmp(nameptr
, pp
->name
) == 0) {
3538 * Insert this into the section linked list
3546 if (pp
->sechead
== NULL
|| pp
->sechead
->index
> index
) {
3547 sp
->next
= pp
->sechead
;
3550 for (sp2
= pp
->sechead
; sp2
!= NULL
; sp2
= sp2
->next
) {
3551 if (sp2
->index
== sp
->index
) {
3552 inform("duplicate index (%d) in message "
3553 "%s's %s: field\n%*s(parameter %s)", sp
->index
,
3554 filename
, fieldname
, strlen(invo_name
) + 2, "",
3558 if (sp2
->index
< sp
->index
&&
3559 (sp2
->next
== NULL
|| sp2
->next
->index
> sp
->index
)) {
3560 sp
->next
= sp2
->next
;
3567 inform("Internal error: cannot insert partial "
3568 "param in message %s's %s: field\n%*s(parameter %s)",
3569 filename
, fieldname
, strlen(invo_name
) + 2, "",
3576 * Save our charset and lang tags.
3579 if (index
== 0 && encoded
) {
3580 mh_xfree(pp
->charset
);
3581 pp
->charset
= charset
;
3586 pm
= add_param(param_head
, param_tail
, nameptr
, valptr
, 1);
3587 pm
->pm_charset
= charset
;
3591 while (isspace ((unsigned char) *cp
))
3595 get_comment (filename
, fieldname
, &cp
, commentp
) == NOTOK
) {
3601 * Now that we're done, reassemble all of the partial parameters.
3604 for (pp
= phead
; pp
!= NULL
; ) {
3608 for (sp
= pp
->sechead
; sp
!= NULL
; sp
= sp
->next
) {
3609 if (sp
->index
!= pindex
++) {
3610 inform("missing section %d for parameter in "
3611 "message %s's %s: field\n%*s(parameter %s)", pindex
- 1,
3612 filename
, fieldname
, strlen(invo_name
) + 2, "",
3619 p
= q
= mh_xmalloc(tlen
+ 1);
3620 for (sp
= pp
->sechead
; sp
!= NULL
; ) {
3621 memcpy(q
, sp
->value
, sp
->len
);
3631 pm
= add_param(param_head
, param_tail
, pp
->name
, p
, 1);
3632 pm
->pm_charset
= pp
->charset
;
3633 pm
->pm_lang
= pp
->lang
;
3644 * Return the charset for a particular content type.
3648 content_charset (CT ct
) {
3649 char *ret_charset
= NULL
;
3651 ret_charset
= get_param(ct
->c_ctinfo
.ci_first_pm
, "charset", '?', 0);
3653 return ret_charset
? ret_charset
: mh_xstrdup("US-ASCII");
3658 * Create a string based on a list of output parameters. Assume that this
3659 * parameter string will be appended to an existing header, so start out
3660 * with the separator (;). Perform RFC 2231 encoding when necessary.
3664 output_params(size_t initialwidth
, PM params
, int *offsetout
, int external
)
3666 char *paramout
= NULL
;
3667 char line
[CPERLIN
* 2], *q
;
3668 int curlen
, index
, cont
, encode
, i
;
3669 size_t valoff
, numchars
;
3671 while (params
!= NULL
) {
3677 if (external
&& strcasecmp(params
->pm_name
, "body") == 0)
3680 if (strlen(params
->pm_name
) > CPERLIN
) {
3681 inform("Parameter name \"%s\" is too long", params
->pm_name
);
3686 curlen
= param_len(params
, index
, valoff
, &encode
, &cont
, &numchars
);
3689 * Loop until we get a parameter that fits within a line. We
3690 * assume new lines start with a tab, so check our overflow based
3700 * At this point we're definitely continuing the line, so
3701 * be sure to include the parameter name and section index.
3704 q
+= snprintf(q
, sizeof(line
) - (q
- line
), "%s*%d",
3705 params
->pm_name
, index
);
3708 * Both of these functions do a NUL termination
3712 i
= encode_param(params
, q
, sizeof(line
) - (q
- line
),
3713 numchars
, valoff
, index
);
3715 i
= normal_param(params
, q
, sizeof(line
) - (q
- line
),
3725 curlen
= param_len(params
, index
, valoff
, &encode
, &cont
,
3730 * "line" starts with a ;\n\t, so that doesn't count against
3731 * the length. But add 8 since it starts with a tab; that's
3732 * how we end up with 5.
3735 initialwidth
= strlen(line
) + 5;
3738 * At this point the line should be built, so add it to our
3739 * current output buffer.
3742 paramout
= add(line
, paramout
);
3746 * If this won't fit on the line, start a new one. Save room in
3747 * case we need a semicolon on the end
3750 if (initialwidth
+ curlen
> CPERLIN
- 1) {
3762 * At this point, we're either finishing a continued parameter, or
3763 * we're working on a new one.
3767 q
+= snprintf(q
, sizeof(line
) - (q
- line
), "%s*%d",
3768 params
->pm_name
, index
);
3770 strncpy(q
, params
->pm_name
, sizeof(line
) - (q
- line
));
3775 i
= encode_param(params
, q
, sizeof(line
) - (q
- line
),
3776 strlen(params
->pm_value
+ valoff
), valoff
, index
);
3778 i
= normal_param(params
, q
, sizeof(line
) - (q
- line
),
3779 strlen(params
->pm_value
+ valoff
), valoff
);
3786 paramout
= add(line
, paramout
);
3787 initialwidth
+= strlen(line
);
3789 params
= params
->pm_next
;
3793 *offsetout
= initialwidth
;
3799 * Calculate the size of a parameter.
3803 * pm - The parameter being output
3804 * index - If continuing the parameter, the index of the section
3806 * valueoff - The current offset into the parameter value that we're
3807 * working on (previous sections have consumed valueoff bytes).
3808 * encode - Set if we should perform encoding on this parameter section
3809 * (given that we're consuming bytesfit bytes).
3810 * cont - Set if the remaining data in value will not fit on a single
3811 * line and will need to be continued.
3812 * bytesfit - The number of bytes that we can consume from the parameter
3813 * value and still fit on a completely new line. The
3814 * calculation assumes the new line starts with a tab,
3815 * includes the parameter name and any encoding, and fits
3816 * within CPERLIN bytes. Will always be at least 1.
3820 param_len(PM pm
, int index
, size_t valueoff
, int *encode
, int *cont
,
3823 char *start
= pm
->pm_value
+ valueoff
, *p
, indexchar
[32];
3824 size_t len
= 0, fit
= 0;
3825 int fitlimit
= 0, eightbit
, maxfit
;
3830 * Add up the length. First, start with the parameter name.
3833 len
= strlen(pm
->pm_name
);
3836 * Scan the parameter value and see if we need to do encoding for this
3840 eightbit
= contains8bit(start
, NULL
);
3843 * Determine if we need to encode this section. Encoding is necessary if:
3845 * - There are any 8-bit characters at all and we're on the first
3847 * - There are 8-bit characters within N bytes of our section start.
3848 * N is calculated based on the number of bytes it would take to
3849 * reach CPERLIN. Specifically:
3850 * 8 (starting tab) +
3851 * strlen(param name) +
3852 * 4 ('* for section marker, '=', opening/closing '"')
3854 * is the number of bytes used by everything that isn't part of the
3855 * value. So that gets subtracted from CPERLIN.
3858 snprintf(indexchar
, sizeof(indexchar
), "%d", index
);
3859 maxfit
= CPERLIN
- (12 + len
+ strlen(indexchar
));
3860 if ((eightbit
&& index
== 0) || contains8bit(start
, start
+ maxfit
)) {
3864 len
++; /* Add in equal sign */
3868 * We're using maxfit as a marker for how many characters we can
3869 * fit into the line. Bump it by two because we're not using quotes
3876 * If we don't have a charset or language tag in this parameter,
3880 if (! pm
->pm_charset
) {
3881 pm
->pm_charset
= mh_xstrdup(write_charset_8bit());
3882 if (strcasecmp(pm
->pm_charset
, "US-ASCII") == 0)
3883 adios(NULL
, "8-bit characters in parameter \"%s\", but "
3884 "local character set is US-ASCII", pm
->pm_name
);
3887 pm
->pm_lang
= mh_xstrdup(""); /* Default to a blank lang tag */
3889 len
++; /* For the encoding marker */
3892 int enclen
= strlen(pm
->pm_charset
) + strlen(pm
->pm_lang
) + 2;
3897 * We know we definitely need to include an index. maxfit already
3898 * includes the section marker.
3900 len
+= strlen(indexchar
);
3902 for (p
= start
; *p
!= '\0'; p
++) {
3903 if (isparamencode(*p
)) {
3911 * Just so there's no confusion: maxfit is counting OUTPUT
3912 * characters (post-encoding). fit is counting INPUT characters.
3914 if (! fitlimit
&& maxfit
>= 0)
3916 else if (! fitlimit
)
3921 * Calculate the string length, but add room for quoting \
3922 * and " if necessary. Also account for quotes at beginning
3925 for (p
= start
; *p
!= '\0'; p
++) {
3936 if (! fitlimit
&& maxfit
>= 0)
3938 else if (! fitlimit
)
3955 * Output an encoded parameter string.
3959 encode_param(PM pm
, char *output
, size_t len
, size_t valuelen
,
3960 size_t valueoff
, int index
)
3962 size_t outlen
= 0, n
;
3963 char *endptr
= output
+ len
, *p
;
3966 * First, output the marker for an encoded string.
3974 * If the index is 0, output the character set and language tag.
3975 * If theses were NULL, they should have already been filled in
3980 n
= snprintf(output
, len
- outlen
, "%s'%s'", pm
->pm_charset
,
3984 if (output
> endptr
) {
3985 inform("Internal error: parameter buffer overflow");
3991 * Copy over the value, encoding if necessary
3994 p
= pm
->pm_value
+ valueoff
;
3995 while (valuelen
-- > 0) {
3996 if (isparamencode(*p
)) {
3997 n
= snprintf(output
, len
- outlen
, "%%%02X", (unsigned char) *p
++);
4004 if (output
> endptr
) {
4005 inform("Internal error: parameter buffer overflow");
4016 * Output a "normal" parameter, without encoding. Be sure to escape
4017 * quotes and backslashes if necessary.
4021 normal_param(PM pm
, char *output
, size_t len
, size_t valuelen
,
4025 char *endptr
= output
+ len
, *p
;
4031 p
= pm
->pm_value
+ valueoff
;
4033 while (valuelen
-- > 0) {
4044 if (output
> endptr
) {
4045 inform("Internal error: parameter buffer overflow");
4050 if (output
- 2 > endptr
) {
4051 inform("Internal error: parameter buffer overflow");
4062 * Add a parameter to the parameter linked list
4066 add_param(PM
*first
, PM
*last
, char *name
, char *value
, int nocopy
)
4071 pm
->pm_name
= nocopy
? name
: getcpy(name
);
4072 pm
->pm_value
= nocopy
? value
: getcpy(value
);
4075 (*last
)->pm_next
= pm
;
4086 * Either replace a current parameter with a new value, or add the parameter
4087 * to the parameter linked list.
4091 replace_param(PM
*first
, PM
*last
, char *name
, char *value
, int nocopy
)
4095 for (pm
= *first
; pm
!= NULL
; pm
= pm
->pm_next
) {
4096 if (strcasecmp(name
, pm
->pm_name
) == 0) {
4098 * If nocopy is set, it's assumed that we own both name
4099 * and value. We don't need name, so we discard it now.
4104 pm
->pm_value
= nocopy
? value
: getcpy(value
);
4109 return add_param(first
, last
, name
, value
, nocopy
);
4113 * Retrieve a parameter value from a parameter linked list. If the parameter
4114 * value needs converted to the local character set, do that now.
4118 get_param(PM first
, const char *name
, char replace
, int fetchonly
)
4120 while (first
!= NULL
) {
4121 if (strcasecmp(name
, first
->pm_name
) == 0) {
4123 return first
->pm_value
;
4124 return getcpy(get_param_value(first
, replace
));
4126 first
= first
->pm_next
;
4133 * Return a parameter value, converting to the local character set if
4137 char *get_param_value(PM pm
, char replace
)
4139 static char buffer
[4096]; /* I hope no parameters are larger */
4140 size_t bufsize
= sizeof(buffer
);
4145 ICONV_CONST
char *p
;
4146 #else /* HAVE_ICONV */
4148 #endif /* HAVE_ICONV */
4153 * If we don't have a character set indicated, it's assumed to be
4154 * US-ASCII. If it matches our character set, we don't need to convert
4158 if (!pm
->pm_charset
|| check_charset(pm
->pm_charset
,
4159 strlen(pm
->pm_charset
))) {
4160 return pm
->pm_value
;
4164 * In this case, we need to convert. If we have iconv support, use
4165 * that. Otherwise, go through and simply replace every non-ASCII
4166 * character with the substitution character.
4171 bufsize
= sizeof(buffer
);
4172 utf8
= strcasecmp(pm
->pm_charset
, "UTF-8") == 0;
4174 cd
= iconv_open(get_charset(), pm
->pm_charset
);
4175 if (cd
== (iconv_t
) -1) {
4179 inbytes
= strlen(pm
->pm_value
);
4183 if (iconv(cd
, &p
, &inbytes
, &q
, &bufsize
) == (size_t)-1) {
4184 if (errno
!= EILSEQ
) {
4189 * Reset shift state, substitute our character,
4190 * try to restart conversion.
4193 iconv(cd
, NULL
, NULL
, &q
, &bufsize
);
4206 for (++p
, --inbytes
;
4207 inbytes
> 0 && (((unsigned char) *p
) & 0xc0) == 0x80;
4226 #endif /* HAVE_ICONV */
4229 * Take everything non-ASCII and substitute the replacement character
4233 bufsize
= sizeof(buffer
);
4234 for (p
= pm
->pm_value
; *p
!= '\0' && bufsize
> 1; p
++, q
++, bufsize
--) {
4235 if (isascii((unsigned char) *p
) && isprint((unsigned char) *p
))