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.
15 #include "h/mhparse.h"
18 #include "h/mhcachesbr.h"
19 #include "sbr/m_mktemp.h"
23 #endif /* HAVE_ICONV */
24 #include "sbr/base64.h"
29 int checksw
= 0; /* check Content-MD5 field */
32 * These are for mhfixmsg to:
33 * 1) Instruct parser not to detect invalid Content-Transfer-Encoding
35 * 2) Suppress the warning about bogus multipart content, and report it.
36 * 3) Suppress the warning about extraneous trailing ';' in header parameter
39 bool skip_mp_cte_check
;
40 bool suppress_bogus_mp_content_warning
;
41 bool bogus_mp_content
;
42 bool suppress_extraneous_trailing_semicolon_warning
;
45 * By default, suppress warning about multiple MIME-Version header fields.
47 bool suppress_multiple_mime_version_warning
= true;
49 /* list of preferred type/subtype pairs, for -prefer */
50 mime_type_subtype mime_preference
[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
},
114 static CT
get_content (FILE *, char *, int);
115 static int get_comment (const char *, const char *, char **, char **);
117 static int InitGeneric (CT
);
118 static int InitText (CT
);
119 static int InitMultiPart (CT
);
120 static void reverse_parts (CT
);
121 static void prefer_parts(CT ct
);
122 static int InitMessage (CT
);
123 static int InitApplication (CT
);
124 static int init_encoding (CT
, OpenCEFunc
);
125 static unsigned long size_encoding (CT
);
126 static int InitBase64 (CT
);
127 static int openBase64 (CT
, char **);
128 static int InitQuoted (CT
);
129 static int openQuoted (CT
, char **);
130 static int Init7Bit (CT
);
131 static int openExternal (CT
, CT
, CE
, char **, int *);
132 static int InitFile (CT
);
133 static int openFile (CT
, char **);
134 static int InitFTP (CT
);
135 static int openFTP (CT
, char **);
136 static int InitMail (CT
);
137 static int openMail (CT
, char **);
138 static int readDigest (CT
, char *);
139 static int get_leftover_mp_content (CT
, int);
140 static int InitURL (CT
);
141 static int openURL (CT
, char **);
142 static int parse_header_attrs (const char *, const char *, char **, PM
*,
144 static size_t param_len(PM
, int, size_t, int *, int *, size_t *);
145 static size_t normal_param(PM
, char *, size_t, size_t, size_t);
146 static int get_dispo (char *, CT
, int);
148 struct str2init str2cts
[] = {
149 { "application", CT_APPLICATION
, InitApplication
},
150 { "audio", CT_AUDIO
, InitGeneric
},
151 { "image", CT_IMAGE
, InitGeneric
},
152 { "message", CT_MESSAGE
, InitMessage
},
153 { "multipart", CT_MULTIPART
, InitMultiPart
},
154 { "text", CT_TEXT
, InitText
},
155 { "video", CT_VIDEO
, InitGeneric
},
156 { NULL
, CT_EXTENSION
, NULL
}, /* these two must be last! */
157 { NULL
, CT_UNKNOWN
, NULL
},
160 struct str2init str2ces
[] = {
161 { "base64", CE_BASE64
, InitBase64
},
162 { "quoted-printable", CE_QUOTED
, InitQuoted
},
163 { "8bit", CE_8BIT
, Init7Bit
},
164 { "7bit", CE_7BIT
, Init7Bit
},
165 { "binary", CE_BINARY
, Init7Bit
},
166 { NULL
, CE_EXTENSION
, NULL
}, /* these two must be last! */
167 { NULL
, CE_UNKNOWN
, NULL
},
171 * NOTE WELL: si_key MUST NOT have value of NOTOK
173 * si_val is 1 if access method is anonymous.
175 struct str2init str2methods
[] = {
176 { "afs", 1, InitFile
},
177 { "anon-ftp", 1, InitFTP
},
178 { "ftp", 0, InitFTP
},
179 { "local-file", 0, InitFile
},
180 { "mail-server", 0, InitMail
},
181 { "url", 0, InitURL
},
187 * Main entry point for parsing a MIME message or file.
188 * It returns the Content structure for the top level
189 * entity in the file.
193 parse_mime (char *file
)
202 bogus_mp_content
= false;
205 * Check if file is actually standard input
207 if ((is_stdin
= !(strcmp (file
, "-")))) {
208 char *tfile
= m_mktemp2(NULL
, invo_name
, NULL
, &fp
);
210 advise("mhparse", "unable to create temporary file in %s",
214 file
= mh_xstrdup(tfile
);
216 while ((n
= fread(buffer
, 1, sizeof(buffer
), stdin
)) > 0) {
217 if (fwrite(buffer
, 1, n
, fp
) != n
) {
218 (void) m_unlink (file
);
219 advise (file
, "error copying to temporary file");
225 if (ferror (stdin
)) {
226 (void) m_unlink (file
);
227 advise ("stdin", "error reading");
231 (void) m_unlink (file
);
232 advise (file
, "error writing");
235 fseek (fp
, 0L, SEEK_SET
);
236 } else if (stat (file
, &statbuf
) == NOTOK
) {
237 advise (file
, "unable to stat");
239 } else if (S_ISDIR(statbuf
.st_mode
)) {
240 /* Don't try to parse a directory. */
241 inform("%s is a directory", file
);
243 } else if ((fp
= fopen (file
, "r")) == NULL
) {
244 advise (file
, "unable to read");
248 if (!(ct
= get_content (fp
, file
, 1))) {
250 (void) m_unlink (file
);
251 inform("unable to decode %s", file
);
256 ct
->c_unlink
= 1; /* temp file to remove */
260 if (ct
->c_end
== 0L) {
261 fseek (fp
, 0L, SEEK_END
);
262 ct
->c_end
= ftell (fp
);
265 if (ct
->c_ctinitfnx
&& (*ct
->c_ctinitfnx
) (ct
) == NOTOK
) {
277 * Main routine for reading/parsing the headers
278 * of a message content.
280 * toplevel = 1 # we are at the top level of the message
281 * toplevel = 0 # we are inside message type or multipart type
282 * # other than multipart/digest
283 * toplevel = -1 # we are inside multipart/digest
284 * NB: on failure we will fclose(in)!
288 get_content (FILE *in
, char *file
, int toplevel
)
291 char buf
[NMH_BUFSIZ
], name
[NAMESZ
];
295 m_getfld_state_t gstate
;
297 /* allocate the content structure */
300 ct
->c_file
= mh_xstrdup(FENDNULL(file
));
301 ct
->c_begin
= ftell (ct
->c_fp
) + 1;
304 * Parse the header fields for this
305 * content into a linked list.
307 gstate
= m_getfld_state_init(in
);
308 m_getfld_track_filepos2(&gstate
);
309 for (compnum
= 1;;) {
310 int bufsz
= sizeof buf
;
311 switch (state
= m_getfld2(&gstate
, name
, buf
, &bufsz
)) {
316 /* get copies of the buffers */
317 np
= mh_xstrdup(name
);
318 vp
= mh_xstrdup(buf
);
320 /* if necessary, get rest of field */
321 while (state
== FLDPLUS
) {
323 state
= m_getfld2(&gstate
, name
, buf
, &bufsz
);
324 vp
= add (buf
, vp
); /* add to previous value */
327 /* Now add the header data to the list */
328 add_header (ct
, np
, vp
);
330 /* continue, to see if this isn't the last header field */
331 ct
->c_begin
= ftell (in
) + 1;
335 /* There are two cases. The unusual one is when there is no
336 * blank line between the headers and the body. This is
337 * indicated by the name of the header starting with `:'.
339 * For both cases, normal first, `1' is the desired c_begin
340 * file position for the start of the body, and `2' is the
341 * file position when buf is returned.
343 * f o o : b a r \n \n b o d y \n bufsz = 6
345 * f o o : b a r \n b o d y \n bufsz = 4
348 * For the normal case, bufsz includes the
349 * header-terminating `\n', even though it is not in buf,
350 * but bufsz isn't affected when it's missing in the unusual
352 if (name
[0] == ':') {
353 ct
->c_begin
= ftell(in
) - bufsz
;
355 ct
->c_begin
= ftell (in
) - (bufsz
- 1);
360 ct
->c_begin
= ftell (in
);
365 die("message format error in component #%d", compnum
);
368 die("getfld() returned %d", state
);
371 /* break out of the loop */
374 m_getfld_state_destroy (&gstate
);
377 * Read the content headers. We will parse the
378 * MIME related header fields into their various
379 * structures and set internal flags related to
380 * content type/subtype, etc.
383 hp
= ct
->c_first_hf
; /* start at first header field */
385 /* Get MIME-Version field */
386 if (!strcasecmp (hp
->name
, VRSN_FIELD
)) {
391 vrsn
= mh_xstrdup(FENDNULL(hp
->value
));
393 /* Now, cleanup this field */
396 while (isspace ((unsigned char) *cp
))
398 for (dp
= strchr(cp
, '\n'); dp
; dp
= strchr(dp
, '\n'))
400 for (dp
= cp
+ strlen (cp
) - 1; dp
>= cp
; dp
--)
401 if (!isspace ((unsigned char) *dp
))
405 fprintf (stderr
, "%s: %s\n", VRSN_FIELD
, cp
);
408 get_comment (ct
->c_file
, VRSN_FIELD
, &cp
, NULL
) == NOTOK
)
411 for (dp
= cp
; istoken (*dp
); dp
++)
415 ucmp
= !strcasecmp (cp
, VRSN_VALUE
);
418 inform("message %s has unknown value for %s: field (%s), continuing...",
419 ct
->c_file
, VRSN_FIELD
, cp
);
424 if (! suppress_multiple_mime_version_warning
)
425 inform("message %s has multiple %s: fields",
426 ct
->c_file
, VRSN_FIELD
);
430 else if (!strcasecmp (hp
->name
, TYPE_FIELD
)) {
431 /* Get Content-Type field */
432 struct str2init
*s2i
;
433 CI ci
= &ct
->c_ctinfo
;
435 /* Check if we've already seen a Content-Type header */
437 inform("message %s has multiple %s: fields",
438 ct
->c_file
, TYPE_FIELD
);
442 /* Parse the Content-Type field */
443 if (get_ctinfo (hp
->value
, ct
, 0) == NOTOK
)
447 * Set the Init function and the internal
448 * flag for this content type.
450 for (s2i
= str2cts
; s2i
->si_key
; s2i
++)
451 if (!strcasecmp (ci
->ci_type
, s2i
->si_key
))
453 if (!s2i
->si_key
&& !uprf (ci
->ci_type
, "X-"))
455 ct
->c_type
= s2i
->si_val
;
456 ct
->c_ctinitfnx
= s2i
->si_init
;
458 else if (!strcasecmp (hp
->name
, ENCODING_FIELD
)) {
459 /* Get Content-Transfer-Encoding field */
461 struct str2init
*s2i
;
464 * Check if we've already seen the
465 * Content-Transfer-Encoding field
468 inform("message %s has multiple %s: fields",
469 ct
->c_file
, ENCODING_FIELD
);
473 /* get copy of this field */
474 ct
->c_celine
= cp
= mh_xstrdup(FENDNULL(hp
->value
));
476 while (isspace ((unsigned char) *cp
))
478 for (dp
= cp
; istoken (*dp
); dp
++)
484 * Find the internal flag and Init function
485 * for this transfer encoding.
487 for (s2i
= str2ces
; s2i
->si_key
; s2i
++)
488 if (!strcasecmp (cp
, s2i
->si_key
))
490 if (!s2i
->si_key
&& !uprf (cp
, "X-"))
493 ct
->c_encoding
= s2i
->si_val
;
495 /* Call the Init function for this encoding */
496 if (s2i
->si_init
&& (*s2i
->si_init
) (ct
) == NOTOK
)
499 else if (!strcasecmp (hp
->name
, MD5_FIELD
)) {
500 /* Get Content-MD5 field */
506 if (ct
->c_digested
) {
507 inform("message %s has multiple %s: fields",
508 ct
->c_file
, MD5_FIELD
);
512 ep
= cp
= mh_xstrdup(FENDNULL(hp
->value
)); /* get a copy */
514 while (isspace ((unsigned char) *cp
))
516 for (dp
= strchr(cp
, '\n'); dp
; dp
= strchr(dp
, '\n'))
518 for (dp
= cp
+ strlen (cp
) - 1; dp
>= cp
; dp
--)
519 if (!isspace ((unsigned char) *dp
))
523 fprintf (stderr
, "%s: %s\n", MD5_FIELD
, cp
);
526 get_comment (ct
->c_file
, MD5_FIELD
, &cp
, NULL
) == NOTOK
) {
531 for (dp
= cp
; *dp
&& !isspace ((unsigned char) *dp
); dp
++)
539 else if (!strcasecmp (hp
->name
, ID_FIELD
)) {
540 /* Get Content-ID field */
541 ct
->c_id
= add (hp
->value
, ct
->c_id
);
543 else if (!strcasecmp (hp
->name
, DESCR_FIELD
)) {
544 /* Get Content-Description field */
545 ct
->c_descr
= add (hp
->value
, ct
->c_descr
);
547 else if (!strcasecmp (hp
->name
, DISPO_FIELD
)) {
548 /* Get Content-Disposition field */
549 if (get_dispo(hp
->value
, ct
, 0) == NOTOK
)
554 hp
= hp
->next
; /* next header field */
558 * Check if we saw a Content-Type field.
559 * If not, then assign a default value for
560 * it, and the Init function.
564 * If we are inside a multipart/digest message,
565 * so default type is message/rfc822
568 if (get_ctinfo ("message/rfc822", ct
, 0) == NOTOK
)
570 ct
->c_type
= CT_MESSAGE
;
571 ct
->c_ctinitfnx
= InitMessage
;
574 * Else default type is text/plain
576 if (get_ctinfo ("text/plain", ct
, 0) == NOTOK
)
578 ct
->c_type
= CT_TEXT
;
579 ct
->c_ctinitfnx
= InitText
;
583 /* Use default Transfer-Encoding, if necessary */
585 ct
->c_encoding
= CE_7BIT
;
598 * small routine to add header field to list
602 add_header (CT ct
, char *name
, char *value
)
606 /* allocate header field structure */
609 /* link data into header structure */
614 /* link header structure into the list */
615 if (ct
->c_first_hf
== NULL
) {
616 ct
->c_first_hf
= hp
; /* this is the first */
619 ct
->c_last_hf
->next
= hp
; /* add it to the end */
628 * Parse Content-Type line and (if `magic' is non-zero) mhbuild composition
629 * directives. Fills in the information of the CTinfo structure.
632 get_ctinfo (char *cp
, CT ct
, int magic
)
641 /* store copy of Content-Type line */
642 cp
= ct
->c_ctline
= mh_xstrdup(FENDNULL(cp
));
644 while (isspace ((unsigned char) *cp
)) /* trim leading spaces */
647 /* change newlines to spaces */
648 for (dp
= strchr(cp
, '\n'); dp
; dp
= strchr(dp
, '\n'))
651 /* trim trailing spaces */
652 for (dp
= cp
+ strlen (cp
) - 1; dp
>= cp
; dp
--)
653 if (!isspace ((unsigned char) *dp
))
658 fprintf (stderr
, "%s: %s\n", TYPE_FIELD
, cp
);
660 if (*cp
== '(' && get_comment (ct
->c_file
, TYPE_FIELD
, &cp
,
661 &ci
->ci_comment
) == NOTOK
)
664 for (dp
= cp
; istoken (*dp
); dp
++)
668 ci
->ci_type
= mh_xstrdup(cp
); /* store content type */
673 inform("invalid %s: field in message %s (empty type)",
674 TYPE_FIELD
, ct
->c_file
);
677 to_lower(ci
->ci_type
);
679 while (isspace ((unsigned char) *cp
))
682 if (*cp
== '(' && get_comment (ct
->c_file
, TYPE_FIELD
, &cp
,
683 &ci
->ci_comment
) == NOTOK
)
688 ci
->ci_subtype
= mh_xstrdup("");
693 while (isspace ((unsigned char) *cp
))
696 if (*cp
== '(' && get_comment (ct
->c_file
, TYPE_FIELD
, &cp
,
697 &ci
->ci_comment
) == NOTOK
)
700 for (dp
= cp
; istoken (*dp
); dp
++)
704 ci
->ci_subtype
= mh_xstrdup(cp
); /* store the content subtype */
708 if (!*ci
->ci_subtype
) {
709 inform("invalid %s: field in message %s (empty subtype for \"%s\")",
710 TYPE_FIELD
, ct
->c_file
, ci
->ci_type
);
713 to_lower(ci
->ci_subtype
);
716 while (isspace ((unsigned char) *cp
))
719 if (*cp
== '(' && get_comment (ct
->c_file
, TYPE_FIELD
, &cp
,
720 &ci
->ci_comment
) == NOTOK
)
723 if ((status
= parse_header_attrs (ct
->c_file
, TYPE_FIELD
, &cp
,
724 &ci
->ci_first_pm
, &ci
->ci_last_pm
,
725 &ci
->ci_comment
)) != OK
) {
726 return status
== NOTOK
? NOTOK
: OK
;
730 * Get any <Content-Id> given in buffer
732 if (magic
&& *cp
== '<') {
735 if (!(dp
= strchr(ct
->c_id
= ++cp
, '>'))) {
736 inform("invalid ID in message %s", ct
->c_file
);
742 ct
->c_id
= concat ("<", ct
->c_id
, ">\n", NULL
);
748 while (isspace ((unsigned char) *cp
))
753 * Get any [Content-Description] given in buffer.
755 if (magic
&& *cp
== '[') {
757 for (dp
= cp
+ strlen (cp
) - 1; dp
>= cp
; dp
--)
761 inform("invalid description in message %s", ct
->c_file
);
769 ct
->c_descr
= concat (ct
->c_descr
, "\n", NULL
);
775 while (isspace ((unsigned char) *cp
))
780 * Get any {Content-Disposition} given in buffer.
782 if (magic
&& *cp
== '{') {
784 for (dp
= cp
+ strlen (cp
) - 1; dp
>= cp
; dp
--)
788 inform("invalid disposition in message %s", ct
->c_file
);
796 if (get_dispo(cp
, ct
, 1) != OK
)
802 while (isspace ((unsigned char) *cp
))
807 * Get any extension directives (right now just the content transfer
808 * encoding, but maybe others) that we care about.
811 if (magic
&& *cp
== '*') {
813 * See if it's a CTE we match on
818 while (*cp
!= '\0' && ! isspace((unsigned char) *cp
))
822 inform("invalid null transfer encoding specification");
829 ct
->c_reqencoding
= CE_UNKNOWN
;
831 for (kv
= EncodingType
; kv
->kv_key
; kv
++) {
832 if (strcasecmp(kv
->kv_key
, dp
) == 0) {
833 ct
->c_reqencoding
= kv
->kv_value
;
838 if (ct
->c_reqencoding
== CE_UNKNOWN
) {
839 inform("invalid CTE specification: \"%s\"", dp
);
843 while (isspace ((unsigned char) *cp
))
848 * Check if anything is left over
852 ci
->ci_magic
= mh_xstrdup(cp
);
854 /* If there is a Content-Disposition header and it doesn't
855 have a *filename=, extract it from the magic contents.
856 The r1bindex call skips any leading directory
858 if (ct
->c_dispo_type
&&
859 !get_param(ct
->c_dispo_first
, "filename", '_', 1)) {
860 add_param(&ct
->c_dispo_first
, &ct
->c_dispo_last
, "filename",
861 r1bindex(ci
->ci_magic
, '/'), 0);
865 inform("extraneous information in message %s's %s: field\n"
866 " (%s)", ct
->c_file
, TYPE_FIELD
, cp
);
874 * Parse out a Content-Disposition header. A lot of this is cribbed from
878 get_dispo (char *cp
, CT ct
, int buildflag
)
880 char *dp
, *dispoheader
;
885 * Save the whole copy of the Content-Disposition header, unless we're
886 * processing a mhbuild directive. A NULL c_dispo will be a flag to
887 * mhbuild that the disposition header needs to be generated at that
891 dispoheader
= cp
= mh_xstrdup(FENDNULL(cp
));
893 while (isspace ((unsigned char) *cp
)) /* trim leading spaces */
896 /* change newlines to spaces */
897 for (dp
= strchr(cp
, '\n'); dp
; dp
= strchr(dp
, '\n'))
900 /* trim trailing spaces */
901 for (dp
= cp
+ strlen (cp
) - 1; dp
>= cp
; dp
--)
902 if (!isspace ((unsigned char) *dp
))
907 fprintf (stderr
, "%s: %s\n", DISPO_FIELD
, cp
);
909 if (*cp
== '(' && get_comment (ct
->c_file
, DISPO_FIELD
, &cp
, NULL
) ==
915 for (dp
= cp
; istoken (*dp
); dp
++)
919 ct
->c_dispo_type
= mh_xstrdup(cp
); /* store disposition type */
923 if (*cp
== '(' && get_comment (ct
->c_file
, DISPO_FIELD
, &cp
, NULL
) == NOTOK
)
926 if ((status
= parse_header_attrs (ct
->c_file
, DISPO_FIELD
, &cp
,
927 &ct
->c_dispo_first
, &ct
->c_dispo_last
,
929 if (status
== NOTOK
) {
934 inform("extraneous information in message %s's %s: field\n (%s)",
935 ct
->c_file
, DISPO_FIELD
, cp
);
941 ct
->c_dispo
= dispoheader
;
948 get_comment (const char *filename
, const char *fieldname
, char **ap
,
953 char c
, buffer
[BUFSIZ
], *dp
;
963 inform("invalid comment in message %s's %s: field",
964 filename
, fieldname
);
969 if ((c
= *cp
++) == '\0')
992 if ((dp
= *commentp
)) {
993 *commentp
= concat (dp
, " ", buffer
, NULL
);
996 *commentp
= mh_xstrdup(buffer
);
1000 while (isspace ((unsigned char) *cp
))
1011 * Handles content types audio, image, and video.
1012 * There's not much to do right here.
1020 return OK
; /* not much to do here */
1031 char buffer
[BUFSIZ
];
1036 CI ci
= &ct
->c_ctinfo
;
1038 /* check for missing subtype */
1039 if (!*ci
->ci_subtype
)
1040 ci
->ci_subtype
= add ("plain", ci
->ci_subtype
);
1043 ct
->c_subtype
= ct_str_subtype (CT_TEXT
, ci
->ci_subtype
);
1045 /* allocate text character set structure */
1047 ct
->c_ctparams
= (void *) t
;
1049 /* scan for charset parameter */
1050 for (pm
= ci
->ci_first_pm
; pm
; pm
= pm
->pm_next
)
1051 if (!strcasecmp (pm
->pm_name
, "charset"))
1054 /* check if content specified a character set */
1056 chset
= pm
->pm_value
;
1057 t
->tx_charset
= CHARSET_SPECIFIED
;
1059 t
->tx_charset
= CHARSET_UNSPECIFIED
;
1063 * If we can not handle character set natively,
1064 * then check profile for string to modify the
1065 * terminal or display method.
1067 * termproc is for mhshow, though mhlist -debug prints it, too.
1069 if (chset
!= NULL
&& !check_charset (chset
, strlen (chset
))) {
1070 snprintf (buffer
, sizeof(buffer
), "%s-charset-%s", invo_name
, chset
);
1071 if ((cp
= context_find (buffer
)))
1072 ct
->c_termproc
= mh_xstrdup(cp
);
1084 InitMultiPart (CT ct
)
1094 struct multipart
*m
;
1095 struct part
*part
, **next
;
1096 CI ci
= &ct
->c_ctinfo
;
1101 * The encoding for multipart messages must be either
1102 * 7bit, 8bit, or binary (per RFC 2045).
1104 if (! skip_mp_cte_check
&& ct
->c_encoding
!= CE_7BIT
&&
1105 ct
->c_encoding
!= CE_8BIT
&& ct
->c_encoding
!= CE_BINARY
) {
1106 /* Copy the Content-Transfer-Encoding header field body so we can
1107 remove any trailing whitespace and leading blanks from it. */
1108 char *cte
= mh_xstrdup(ct
->c_celine
? ct
->c_celine
: "(null)");
1110 bp
= cte
+ strlen (cte
) - 1;
1111 while (bp
>= cte
&& isspace ((unsigned char) *bp
)) *bp
-- = '\0';
1112 for (bp
= cte
; isblank((unsigned char)*bp
); ++bp
) continue;
1114 inform("\"%s/%s\" type in message %s must be encoded in\n"
1115 "7bit, 8bit, or binary, per RFC 2045 (6.4). "
1116 "mhfixmsg -fixcte can fix it, or\n"
1117 "manually edit the file and change the \"%s\"\n"
1118 "Content-Transfer-Encoding to one of those. For now, continuing...",
1119 ci
->ci_type
, ci
->ci_subtype
, ct
->c_file
, bp
);
1126 ct
->c_subtype
= ct_str_subtype (CT_MULTIPART
, ci
->ci_subtype
);
1129 * Check for "boundary" parameter, which is
1130 * required for multipart messages.
1133 for (pm
= ci
->ci_first_pm
; pm
; pm
= pm
->pm_next
) {
1134 if (!strcasecmp (pm
->pm_name
, "boundary")) {
1140 /* complain if boundary parameter is missing */
1142 inform("a \"boundary\" parameter is mandatory for \"%s/%s\" type in message %s's %s: field",
1143 ci
->ci_type
, ci
->ci_subtype
, ct
->c_file
, TYPE_FIELD
);
1147 /* allocate primary structure for multipart info */
1149 ct
->c_ctparams
= (void *) m
;
1151 /* check if boundary parameter contains only whitespace characters */
1152 for (cp
= bp
; isspace ((unsigned char) *cp
); cp
++)
1155 inform("invalid \"boundary\" parameter for \"%s/%s\" type in message %s's %s: field",
1156 ci
->ci_type
, ci
->ci_subtype
, ct
->c_file
, TYPE_FIELD
);
1160 /* remove trailing whitespace from boundary parameter */
1161 for (cp
= bp
, dp
= cp
+ strlen (cp
) - 1; dp
> cp
; dp
--)
1162 if (!isspace ((unsigned char) *dp
))
1166 /* record boundary separators */
1167 m
->mp_start
= concat (bp
, "\n", NULL
);
1168 m
->mp_stop
= concat (bp
, "--\n", NULL
);
1170 if (!ct
->c_fp
&& (ct
->c_fp
= fopen (ct
->c_file
, "r")) == NULL
) {
1171 advise (ct
->c_file
, "unable to open for reading");
1175 fseek (fp
= ct
->c_fp
, pos
= ct
->c_begin
, SEEK_SET
);
1177 next
= &m
->mp_parts
;
1181 while ((gotlen
= getline(&bufp
, &buflen
, fp
)) != -1) {
1186 if (bufp
[0] != '-' || bufp
[1] != '-')
1189 if (strcmp (bufp
+ 2, m
->mp_start
))
1194 next
= &part
->mp_next
;
1196 if (!(p
= get_content (fp
, ct
->c_file
,
1197 ct
->c_subtype
== MULTI_DIGEST
? -1 : 0))) {
1205 fseek (fp
, pos
, SEEK_SET
);
1208 if (strcmp (bufp
+ 2, m
->mp_start
) == 0) {
1212 p
->c_end
= ftell(fp
) - (gotlen
+ 1);
1213 if (p
->c_end
< p
->c_begin
)
1214 p
->c_begin
= p
->c_end
;
1219 if (strcmp (bufp
+ 2, m
->mp_stop
) == 0)
1224 if (! suppress_bogus_mp_content_warning
) {
1225 inform("bogus multipart content in message %s", ct
->c_file
);
1227 bogus_mp_content
= true;
1229 if (!inout
&& part
) {
1231 p
->c_end
= ct
->c_end
;
1233 if (p
->c_begin
>= p
->c_end
) {
1234 for (next
= &m
->mp_parts
; *next
!= part
;
1235 next
= &((*next
)->mp_next
))
1244 /* reverse the order of the parts for multipart/alternative */
1245 if (ct
->c_subtype
== MULTI_ALTERNATE
) {
1251 * label all subparts with part number, and
1252 * then initialize the content of the subpart.
1257 char partnam
[BUFSIZ
];
1260 snprintf (partnam
, sizeof(partnam
), "%s.", ct
->c_partno
);
1261 pp
= partnam
+ strlen (partnam
);
1266 for (part
= m
->mp_parts
, partnum
= 1; part
;
1267 part
= part
->mp_next
, partnum
++) {
1270 sprintf (pp
, "%d", partnum
);
1271 p
->c_partno
= mh_xstrdup(partnam
);
1273 /* initialize the content of the subparts */
1274 if (p
->c_ctinitfnx
&& (*p
->c_ctinitfnx
) (p
) == NOTOK
) {
1283 get_leftover_mp_content (ct
, 1);
1284 get_leftover_mp_content (ct
, 0);
1294 * reverse the order of the parts of a multipart/alternative,
1295 * presumably to put the "most favored" alternative first, for
1296 * ease of choosing/displaying it later on. from a mail message on
1297 * nmh-workers, from kenh:
1298 * "Stock" MH 6.8.5 did not have a reverse_parts() function, but I
1299 * see code in mhn that did the same thing... According to the RCS
1300 * logs, that code was around from the initial checkin of mhn.c by
1301 * John Romine in 1992, which is as far back as we have."
1304 reverse_parts (CT ct
)
1306 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
1310 /* Reverse the order of its parts by walking the mp_parts list
1311 and pushing each node to the front. */
1312 for (part
= m
->mp_parts
, m
->mp_parts
= NULL
; part
; part
= next
) {
1313 next
= part
->mp_next
;
1314 part
->mp_next
= m
->mp_parts
;
1320 move_preferred_part(CT ct
, mime_type_subtype
*pref
)
1322 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
1323 struct part
*part
, *prev
, *head
, *nhead
, *ntail
;
1327 /* move the matching part(s) to the head of the list: walk the
1328 * list of parts, move matching parts to a new list (maintaining
1329 * their order), and finally, concatenate the old list onto the
1336 head
->mp_next
= m
->mp_parts
;
1337 nhead
->mp_next
= NULL
;
1341 part
= head
->mp_next
;
1342 while (part
!= NULL
) {
1343 ci
= &part
->mp_part
->c_ctinfo
;
1344 if (!strcasecmp(ci
->ci_type
, pref
->type
) &&
1346 !strcasecmp(ci
->ci_subtype
, pref
->subtype
))) {
1347 prev
->mp_next
= part
->mp_next
;
1348 part
->mp_next
= NULL
;
1349 ntail
->mp_next
= part
;
1351 part
= prev
->mp_next
;
1354 part
= prev
->mp_next
;
1357 ntail
->mp_next
= head
->mp_next
;
1358 m
->mp_parts
= nhead
->mp_next
;
1362 * move parts that match the user's preferences (-prefer) to the head
1363 * of the line. process preferences in reverse so first one given
1364 * ends up first in line
1370 for (i
= 0; i
< npreferred
; i
++)
1371 move_preferred_part(ct
, mime_preference
+ i
);
1376 /* parse_mime() arranges alternates in reverse (priority) order. This
1377 function can be used to reverse them back. This will put, for
1378 example, a text/plain part before a text/html part in a
1379 multipart/alternative part, for example, where it belongs. */
1381 reverse_alternative_parts (CT ct
)
1383 if (ct
->c_type
== CT_MULTIPART
) {
1384 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
1387 if (ct
->c_subtype
== MULTI_ALTERNATE
) {
1391 /* And call recursively on each part of a multipart. */
1392 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
1393 reverse_alternative_parts (part
->mp_part
);
1406 CI ci
= &ct
->c_ctinfo
;
1408 if ((ct
->c_encoding
!= CE_7BIT
) && (ct
->c_encoding
!= CE_8BIT
)) {
1409 inform("\"%s/%s\" type in message %s should be encoded in "
1410 "7bit or 8bit, continuing...", ci
->ci_type
, ci
->ci_subtype
,
1415 /* check for missing subtype */
1416 if (!*ci
->ci_subtype
)
1417 ci
->ci_subtype
= add ("rfc822", ci
->ci_subtype
);
1420 ct
->c_subtype
= ct_str_subtype (CT_MESSAGE
, ci
->ci_subtype
);
1422 switch (ct
->c_subtype
) {
1423 case MESSAGE_RFC822
:
1426 case MESSAGE_PARTIAL
:
1432 ct
->c_ctparams
= (void *) p
;
1434 /* scan for parameters "id", "number", and "total" */
1435 for (pm
= ci
->ci_first_pm
; pm
; pm
= pm
->pm_next
) {
1436 if (!strcasecmp (pm
->pm_name
, "id")) {
1437 p
->pm_partid
= mh_xstrdup(FENDNULL(pm
->pm_value
));
1440 if (!strcasecmp (pm
->pm_name
, "number")) {
1441 if (sscanf (pm
->pm_value
, "%d", &p
->pm_partno
) != 1
1442 || p
->pm_partno
< 1) {
1444 inform("invalid %s parameter for \"%s/%s\" type in message %s's %s field",
1445 pm
->pm_name
, ci
->ci_type
, ci
->ci_subtype
,
1446 ct
->c_file
, TYPE_FIELD
);
1451 if (!strcasecmp (pm
->pm_name
, "total")) {
1452 if (sscanf (pm
->pm_value
, "%d", &p
->pm_maxno
) != 1
1461 || (p
->pm_maxno
&& p
->pm_partno
> p
->pm_maxno
)) {
1462 inform("invalid parameters for \"%s/%s\" type in message %s's %s field",
1463 ci
->ci_type
, ci
->ci_subtype
, ct
->c_file
, TYPE_FIELD
);
1469 case MESSAGE_EXTERNAL
:
1477 ct
->c_ctparams
= (void *) e
;
1480 && (ct
->c_fp
= fopen (ct
->c_file
, "r")) == NULL
) {
1481 advise (ct
->c_file
, "unable to open for reading");
1485 fseek (fp
= ct
->c_fp
, ct
->c_begin
, SEEK_SET
);
1487 if (!(p
= get_content (fp
, ct
->c_file
, 0))) {
1495 p
->c_ceopenfnx
= NULL
;
1496 if ((exresult
= params_external (ct
, 0)) != NOTOK
1497 && p
->c_ceopenfnx
== openMail
) {
1501 if ((size
= ct
->c_end
- p
->c_begin
) <= 0) {
1503 content_error (NULL
, ct
,
1504 "empty body for access-type=mail-server");
1508 e
->eb_body
= bp
= mh_xmalloc ((unsigned) size
);
1509 fseek (p
->c_fp
, p
->c_begin
, SEEK_SET
);
1511 switch (cc
= fread (bp
, sizeof(*bp
), size
, p
->c_fp
)) {
1513 adios ("failed", "fread");
1516 die("unexpected EOF from fread");
1519 bp
+= cc
, size
-= cc
;
1526 p
->c_end
= p
->c_begin
;
1531 if (exresult
== NOTOK
)
1533 if (e
->eb_flags
== NOTOK
)
1536 switch (p
->c_type
) {
1541 if (p
->c_subtype
!= MESSAGE_RFC822
)
1545 e
->eb_partno
= ct
->c_partno
;
1547 (*p
->c_ctinitfnx
) (p
);
1562 params_external (CT ct
, int composing
)
1565 struct exbody
*e
= (struct exbody
*) ct
->c_ctparams
;
1566 CI ci
= &ct
->c_ctinfo
;
1568 ct
->c_ceopenfnx
= NULL
;
1569 for (pm
= ci
->ci_first_pm
; pm
; pm
= pm
->pm_next
) {
1570 if (!strcasecmp (pm
->pm_name
, "access-type")) {
1571 struct str2init
*s2i
;
1572 CT p
= e
->eb_content
;
1574 for (s2i
= str2methods
; s2i
->si_key
; s2i
++)
1575 if (!strcasecmp (pm
->pm_value
, s2i
->si_key
))
1578 e
->eb_access
= pm
->pm_value
;
1579 e
->eb_flags
= NOTOK
;
1580 p
->c_encoding
= CE_EXTERNAL
;
1583 e
->eb_access
= s2i
->si_key
;
1584 e
->eb_flags
= s2i
->si_val
;
1585 p
->c_encoding
= CE_EXTERNAL
;
1587 /* Call the Init function for this external type */
1588 if ((*s2i
->si_init
)(p
) == NOTOK
)
1592 if (!strcasecmp (pm
->pm_name
, "name")) {
1593 e
->eb_name
= pm
->pm_value
;
1596 if (!strcasecmp (pm
->pm_name
, "permission")) {
1597 e
->eb_permission
= pm
->pm_value
;
1600 if (!strcasecmp (pm
->pm_name
, "site")) {
1601 e
->eb_site
= pm
->pm_value
;
1604 if (!strcasecmp (pm
->pm_name
, "directory")) {
1605 e
->eb_dir
= pm
->pm_value
;
1608 if (!strcasecmp (pm
->pm_name
, "mode")) {
1609 e
->eb_mode
= pm
->pm_value
;
1612 if (!strcasecmp (pm
->pm_name
, "size")) {
1613 sscanf (pm
->pm_value
, "%lu", &e
->eb_size
);
1616 if (!strcasecmp (pm
->pm_name
, "server")) {
1617 e
->eb_server
= pm
->pm_value
;
1620 if (!strcasecmp (pm
->pm_name
, "subject")) {
1621 e
->eb_subject
= pm
->pm_value
;
1624 if (!strcasecmp (pm
->pm_name
, "url")) {
1626 * According to RFC 2017, we have to remove all whitespace from
1630 char *u
, *p
= pm
->pm_value
;
1631 e
->eb_url
= u
= mh_xmalloc(strlen(pm
->pm_value
) + 1);
1633 for (; *p
!= '\0'; p
++) {
1634 if (! isspace((unsigned char) *p
))
1641 if (composing
&& !strcasecmp (pm
->pm_name
, "body")) {
1642 e
->eb_body
= getcpy (pm
->pm_value
);
1647 if (!e
->eb_access
) {
1648 inform("invalid parameters for \"%s/%s\" type in message %s's %s field",
1649 ci
->ci_type
, ci
->ci_subtype
, ct
->c_file
, TYPE_FIELD
);
1662 InitApplication (CT ct
)
1664 CI ci
= &ct
->c_ctinfo
;
1667 ct
->c_subtype
= ct_str_subtype (CT_APPLICATION
, ci
->ci_subtype
);
1674 * TRANSFER ENCODINGS
1678 init_encoding (CT ct
, OpenCEFunc openfnx
)
1680 ct
->c_ceopenfnx
= openfnx
;
1681 ct
->c_ceclosefnx
= close_encoding
;
1682 ct
->c_cesizefnx
= size_encoding
;
1689 close_encoding (CT ct
)
1691 CE ce
= &ct
->c_cefile
;
1700 static unsigned long
1701 size_encoding (CT ct
)
1706 CE ce
= &ct
->c_cefile
;
1709 if (ce
->ce_fp
&& fstat (fileno (ce
->ce_fp
), &st
) != NOTOK
)
1710 return (long) st
.st_size
;
1713 if (stat (ce
->ce_file
, &st
) != NOTOK
)
1714 return (long) st
.st_size
;
1718 if (ct
->c_encoding
== CE_EXTERNAL
)
1719 return ct
->c_end
- ct
->c_begin
;
1722 if ((fd
= (*ct
->c_ceopenfnx
) (ct
, &file
)) == NOTOK
)
1723 return ct
->c_end
- ct
->c_begin
;
1725 if (fstat (fd
, &st
) != NOTOK
)
1726 size
= (long) st
.st_size
;
1730 (*ct
->c_ceclosefnx
) (ct
);
1742 return init_encoding (ct
, openBase64
);
1747 openBase64 (CT ct
, char **file
)
1751 bool own_ct_fp
= false;
1752 char *cp
, *buffer
= NULL
;
1753 /* sbeck -- handle suffixes */
1755 CE ce
= &ct
->c_cefile
;
1756 unsigned char *decoded
;
1758 unsigned char digest
[16];
1761 fseek (ce
->ce_fp
, 0L, SEEK_SET
);
1766 if ((ce
->ce_fp
= fopen (ce
->ce_file
, "r")) == NULL
) {
1767 content_error (ce
->ce_file
, ct
, "unable to fopen for reading");
1773 if (*file
== NULL
) {
1776 ce
->ce_file
= mh_xstrdup(*file
);
1780 /* sbeck@cise.ufl.edu -- handle suffixes */
1782 if ((cp
= context_find_by_type ("suffix", ci
->ci_type
, ci
->ci_subtype
))) {
1783 if (ce
->ce_unlink
) {
1784 /* Create temporary file with filename extension. */
1785 if ((ce
->ce_file
= m_mktemps(invo_name
, cp
, NULL
, NULL
)) == NULL
) {
1786 die("unable to create temporary file in %s",
1790 ce
->ce_file
= add (cp
, ce
->ce_file
);
1792 } else if (*file
== NULL
) {
1794 if ((tempfile
= m_mktemp2(NULL
, invo_name
, NULL
, NULL
)) == NULL
) {
1795 die("unable to create temporary file in %s",
1798 ce
->ce_file
= mh_xstrdup(tempfile
);
1801 if ((ce
->ce_fp
= fopen (ce
->ce_file
, "w+")) == NULL
) {
1802 content_error (ce
->ce_file
, ct
, "unable to fopen for reading/writing");
1806 if ((len
= ct
->c_end
- ct
->c_begin
) < 0)
1807 die("internal error(1)");
1809 buffer
= mh_xmalloc (len
+ 1);
1812 if ((ct
->c_fp
= fopen (ct
->c_file
, "r")) == NULL
) {
1813 content_error (ct
->c_file
, ct
, "unable to open for reading");
1819 lseek (fd
= fileno (ct
->c_fp
), (off_t
) ct
->c_begin
, SEEK_SET
);
1822 switch (cc
= read (fd
, cp
, len
)) {
1824 content_error (ct
->c_file
, ct
, "error reading from");
1828 content_error (NULL
, ct
, "premature eof");
1839 /* decodeBase64() requires null-terminated input. */
1842 if (decodeBase64 (buffer
, &decoded
, &decoded_len
, ct
->c_type
== CT_TEXT
,
1843 ct
->c_digested
? digest
: NULL
) != OK
)
1848 unsigned char *decoded_p
= decoded
;
1849 for (i
= 0; i
< decoded_len
; ++i
) {
1850 putc (*decoded_p
++, ce
->ce_fp
);
1853 if (ferror (ce
->ce_fp
)) {
1854 content_error (ce
->ce_file
, ct
, "error writing to");
1858 if (ct
->c_digested
) {
1859 if (memcmp(digest
, ct
->c_digest
,
1861 content_error (NULL
, ct
,
1862 "content integrity suspect (digest mismatch) -- continuing");
1865 fprintf (stderr
, "content integrity confirmed\n");
1871 fseek (ct
->c_fp
, 0L, SEEK_SET
);
1873 if (fflush (ce
->ce_fp
)) {
1874 content_error (ce
->ce_file
, ct
, "error writing to");
1878 fseek (ce
->ce_fp
, 0L, SEEK_SET
);
1881 *file
= ce
->ce_file
;
1887 return fileno (ce
->ce_fp
);
1894 free_encoding (ct
, 0);
1904 static char hex2nib
[0x80] = {
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, 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, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
1912 0x08, 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1913 0x00, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x00,
1914 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1915 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1916 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1917 0x00, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x00,
1918 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1919 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1920 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
1927 return init_encoding (ct
, openQuoted
);
1932 openQuoted (CT ct
, char **file
)
1934 int cc
, digested
, len
, quoted
;
1935 bool own_ct_fp
= false;
1941 CE ce
= &ct
->c_cefile
;
1942 /* sbeck -- handle suffixes */
1947 fseek (ce
->ce_fp
, 0L, SEEK_SET
);
1952 if ((ce
->ce_fp
= fopen (ce
->ce_file
, "r")) == NULL
) {
1953 content_error (ce
->ce_file
, ct
, "unable to fopen for reading");
1959 if (*file
== NULL
) {
1962 ce
->ce_file
= mh_xstrdup(*file
);
1966 /* sbeck@cise.ufl.edu -- handle suffixes */
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 die("unable to create temporary file in %s",
1976 ce
->ce_file
= add (cp
, ce
->ce_file
);
1978 } else if (*file
== NULL
) {
1980 if ((tempfile
= m_mktemp2(NULL
, invo_name
, NULL
, NULL
)) == NULL
) {
1981 die("unable to create temporary file in %s",
1984 ce
->ce_file
= mh_xstrdup(tempfile
);
1987 if ((ce
->ce_fp
= fopen (ce
->ce_file
, "w+")) == NULL
) {
1988 content_error (ce
->ce_file
, ct
, "unable to fopen for reading/writing");
1992 if ((len
= ct
->c_end
- ct
->c_begin
) < 0)
1993 die("internal error(2)");
1996 if ((ct
->c_fp
= fopen (ct
->c_file
, "r")) == NULL
) {
1997 content_error (ct
->c_file
, ct
, "unable to open for reading");
2003 if ((digested
= ct
->c_digested
))
2004 MD5Init (&mdContext
);
2008 fseek (ct
->c_fp
, ct
->c_begin
, SEEK_SET
);
2010 if ((gotlen
= getline(&bufp
, &buflen
, ct
->c_fp
)) == -1) {
2011 content_error (NULL
, ct
, "premature eof");
2015 if ((cc
= gotlen
) > len
)
2019 for (ep
= (cp
= bufp
) + cc
- 1; cp
<= ep
; ep
--)
2020 if (!isspace ((unsigned char) *ep
))
2025 for (; cp
< ep
; cp
++) {
2027 /* in an escape sequence */
2029 /* at byte 1 of an escape sequence */
2030 mask
= hex2nib
[((unsigned char) *cp
) & 0x7f];
2031 /* next is byte 2 */
2034 /* at byte 2 of an escape sequence */
2036 mask
|= hex2nib
[((unsigned char) *cp
) & 0x7f];
2037 putc (mask
, ce
->ce_fp
);
2039 MD5Update (&mdContext
, &mask
, 1);
2040 if (ferror (ce
->ce_fp
)) {
2041 content_error (ce
->ce_file
, ct
, "error writing to");
2044 /* finished escape sequence; next may be literal or a new
2045 * escape sequence */
2048 /* on to next byte */
2052 /* not in an escape sequence */
2054 /* starting an escape sequence, or invalid '='? */
2055 if (cp
+ 1 < ep
&& cp
[1] == '\n') {
2056 /* "=\n" soft line break, eat the \n */
2060 if (cp
+ 1 >= ep
|| cp
+ 2 >= ep
) {
2061 /* We don't have 2 bytes left, so this is an invalid
2062 * escape sequence; just show the raw bytes (below). */
2063 } else if (isxdigit ((unsigned char) cp
[1]) &&
2064 isxdigit ((unsigned char) cp
[2])) {
2065 /* Next 2 bytes are hex digits, making this a valid escape
2066 * sequence; let's decode it (above). */
2070 /* One or both of the next 2 is out of range, making this
2071 * an invalid escape sequence; just show the raw bytes
2075 /* Just show the raw byte. */
2076 putc (*cp
, ce
->ce_fp
);
2079 MD5Update (&mdContext
, (unsigned char *) "\r\n",2);
2081 MD5Update (&mdContext
, (unsigned char *) cp
, 1);
2084 if (ferror (ce
->ce_fp
)) {
2085 content_error (ce
->ce_file
, ct
, "error writing to");
2091 content_error (NULL
, ct
,
2092 "invalid QUOTED-PRINTABLE encoding -- end-of-content while still quoting");
2096 fseek (ct
->c_fp
, 0L, SEEK_SET
);
2098 if (fflush (ce
->ce_fp
)) {
2099 content_error (ce
->ce_file
, ct
, "error writing to");
2104 unsigned char digest
[16];
2106 MD5Final (digest
, &mdContext
);
2107 if (memcmp(digest
, ct
->c_digest
,
2109 content_error (NULL
, ct
,
2110 "content integrity suspect (digest mismatch) -- continuing");
2112 fprintf (stderr
, "content integrity confirmed\n");
2115 fseek (ce
->ce_fp
, 0L, SEEK_SET
);
2118 *file
= ce
->ce_file
;
2124 return fileno (ce
->ce_fp
);
2127 free_encoding (ct
, 0);
2144 if (init_encoding (ct
, open7Bit
) == NOTOK
)
2147 ct
->c_cesizefnx
= NULL
; /* no need to decode for real size */
2153 open7Bit (CT ct
, char **file
)
2156 bool own_ct_fp
= false;
2157 char buffer
[BUFSIZ
];
2158 /* sbeck -- handle suffixes */
2161 CE ce
= &ct
->c_cefile
;
2164 fseek (ce
->ce_fp
, 0L, SEEK_SET
);
2169 if ((ce
->ce_fp
= fopen (ce
->ce_file
, "r")) == NULL
) {
2170 content_error (ce
->ce_file
, ct
, "unable to fopen for reading");
2176 if (*file
== NULL
) {
2179 ce
->ce_file
= mh_xstrdup(*file
);
2183 /* sbeck@cise.ufl.edu -- handle suffixes */
2185 if ((cp
= context_find_by_type ("suffix", ci
->ci_type
, ci
->ci_subtype
))) {
2186 if (ce
->ce_unlink
) {
2187 /* Create temporary file with filename extension. */
2188 if ((ce
->ce_file
= m_mktemps(invo_name
, cp
, NULL
, NULL
)) == NULL
) {
2189 die("unable to create temporary file in %s",
2193 ce
->ce_file
= add (cp
, ce
->ce_file
);
2195 } else if (*file
== NULL
) {
2197 if ((tempfile
= m_mktemp2(NULL
, invo_name
, NULL
, NULL
)) == NULL
) {
2198 die("unable to create temporary file in %s",
2201 ce
->ce_file
= mh_xstrdup(tempfile
);
2204 if ((ce
->ce_fp
= fopen (ce
->ce_file
, "w+")) == NULL
) {
2205 content_error (ce
->ce_file
, ct
, "unable to fopen for reading/writing");
2209 if (ct
->c_type
== CT_MULTIPART
) {
2210 CI ci
= &ct
->c_ctinfo
;
2214 fprintf (ce
->ce_fp
, "%s: %s/%s", TYPE_FIELD
, ci
->ci_type
, ci
->ci_subtype
);
2215 len
+= LEN(TYPE_FIELD
) + 2 + strlen (ci
->ci_type
)
2216 + 1 + strlen (ci
->ci_subtype
);
2217 buffer
= output_params(len
, ci
->ci_first_pm
, &len
, 0);
2220 fputs (buffer
, ce
->ce_fp
);
2224 if (ci
->ci_comment
) {
2225 if (len
+ 1 + (cc
= 2 + strlen (ci
->ci_comment
)) >= CPERLIN
) {
2226 fputs ("\n\t", ce
->ce_fp
);
2230 putc (' ', ce
->ce_fp
);
2233 fprintf (ce
->ce_fp
, "(%s)", ci
->ci_comment
);
2236 fprintf (ce
->ce_fp
, "\n");
2238 fprintf (ce
->ce_fp
, "%s:%s", ID_FIELD
, ct
->c_id
);
2240 fprintf (ce
->ce_fp
, "%s:%s", DESCR_FIELD
, ct
->c_descr
);
2242 fprintf (ce
->ce_fp
, "%s:%s", DISPO_FIELD
, ct
->c_dispo
);
2243 fprintf (ce
->ce_fp
, "\n");
2246 if ((len
= ct
->c_end
- ct
->c_begin
) < 0)
2247 die("internal error(3)");
2250 if ((ct
->c_fp
= fopen (ct
->c_file
, "r")) == NULL
) {
2251 content_error (ct
->c_file
, ct
, "unable to open for reading");
2257 lseek (fd
= fileno (ct
->c_fp
), (off_t
) ct
->c_begin
, SEEK_SET
);
2259 switch (cc
= read (fd
, buffer
, sizeof(buffer
) - 1)) {
2261 content_error (ct
->c_file
, ct
, "error reading from");
2265 content_error (NULL
, ct
, "premature eof");
2273 if ((int) fwrite (buffer
, sizeof(*buffer
), cc
, ce
->ce_fp
) < cc
) {
2274 advise ("open7Bit", "fwrite");
2276 if (ferror (ce
->ce_fp
)) {
2277 content_error (ce
->ce_file
, ct
, "error writing to");
2282 fseek (ct
->c_fp
, 0L, SEEK_SET
);
2284 if (fflush (ce
->ce_fp
)) {
2285 content_error (ce
->ce_file
, ct
, "error writing to");
2289 fseek (ce
->ce_fp
, 0L, SEEK_SET
);
2292 *file
= ce
->ce_file
;
2297 return fileno (ce
->ce_fp
);
2300 free_encoding (ct
, 0);
2314 openExternal (CT ct
, CT cb
, CE ce
, char **file
, int *fd
)
2316 char cachefile
[BUFSIZ
];
2319 fseek (ce
->ce_fp
, 0L, SEEK_SET
);
2324 if ((ce
->ce_fp
= fopen (ce
->ce_file
, "r")) == NULL
) {
2325 content_error (ce
->ce_file
, ct
, "unable to fopen for reading");
2331 if (find_cache(ct
, rcachesw
, NULL
, cb
->c_id
,
2332 cachefile
, sizeof(cachefile
)) != NOTOK
) {
2333 if ((ce
->ce_fp
= fopen (cachefile
, "r"))) {
2334 ce
->ce_file
= mh_xstrdup(cachefile
);
2338 admonish (cachefile
, "unable to fopen for reading");
2341 *fd
= ce
->ce_fp
? fileno (ce
->ce_fp
) : -1;
2345 *file
= ce
->ce_file
;
2346 *fd
= fileno (ce
->ce_fp
);
2357 return init_encoding (ct
, openFile
);
2362 openFile (CT ct
, char **file
)
2365 char cachefile
[BUFSIZ
];
2366 struct exbody
*e
= ct
->c_ctexbody
;
2367 CE ce
= &ct
->c_cefile
;
2369 switch (openExternal (e
->eb_parent
, e
->eb_content
, ce
, file
, &fd
)) {
2381 content_error (NULL
, ct
, "missing name parameter");
2385 ce
->ce_file
= mh_xstrdup(e
->eb_name
);
2388 if ((ce
->ce_fp
= fopen (ce
->ce_file
, "r")) == NULL
) {
2389 content_error (ce
->ce_file
, ct
, "unable to fopen for reading");
2393 if ((!e
->eb_permission
|| strcasecmp (e
->eb_permission
, "read-write"))
2394 && find_cache (NULL
, wcachesw
, &cachetype
, e
->eb_content
->c_id
,
2395 cachefile
, sizeof(cachefile
)) != NOTOK
) {
2399 mask
= umask (cachetype
? ~m_gmprot () : 0222);
2400 if ((fp
= fopen (cachefile
, "w"))) {
2402 char buffer
[BUFSIZ
];
2403 FILE *gp
= ce
->ce_fp
;
2405 fseek (gp
, 0L, SEEK_SET
);
2407 while ((cc
= fread (buffer
, sizeof(*buffer
), sizeof(buffer
), gp
))
2409 if ((int) fwrite (buffer
, sizeof(*buffer
), cc
, fp
) < cc
) {
2410 advise ("openFile", "fwrite");
2415 admonish (ce
->ce_file
, "error reading");
2416 (void) m_unlink (cachefile
);
2417 } else if (ferror (fp
)) {
2418 admonish (cachefile
, "error writing");
2419 (void) m_unlink (cachefile
);
2426 fseek (ce
->ce_fp
, 0L, SEEK_SET
);
2427 *file
= ce
->ce_file
;
2428 return fileno (ce
->ce_fp
);
2438 return init_encoding (ct
, openFTP
);
2443 openFTP (CT ct
, char **file
)
2449 char *bp
, *ftp
, *user
, *pass
;
2450 char buffer
[BUFSIZ
], cachefile
[BUFSIZ
];
2452 CE ce
= &ct
->c_cefile
;
2453 static char *username
= NULL
;
2454 static char *password
= NULL
;
2458 if ((ftp
= context_find (nmhaccessftp
)) && !*ftp
)
2464 switch (openExternal (e
->eb_parent
, e
->eb_content
, ce
, file
, &fd
)) {
2475 if (!e
->eb_name
|| !e
->eb_site
) {
2476 content_error (NULL
, ct
, "missing %s parameter",
2477 e
->eb_name
? "site": "name");
2481 /* Get the buffer ready to go */
2483 buflen
= sizeof(buffer
);
2486 * Construct the query message for user
2488 snprintf (bp
, buflen
, "Retrieve %s", e
->eb_name
);
2494 snprintf (bp
, buflen
, " (content %s)", e
->eb_partno
);
2500 snprintf (bp
, buflen
, "\n using %sFTP from site %s",
2501 e
->eb_flags
? "anonymous " : "", e
->eb_site
);
2506 if (e
->eb_size
> 0) {
2507 snprintf (bp
, buflen
, " (%lu octets)", e
->eb_size
);
2512 snprintf (bp
, buflen
, "? ");
2515 * Now, check the answer
2517 if (!read_yes_or_no_if_tty (buffer
))
2522 snprintf (buffer
, sizeof(buffer
), "%s@%s", getusername (),
2526 ruserpass (e
->eb_site
, &username
, &password
, 0);
2531 ce
->ce_unlink
= (*file
== NULL
);
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
) {
2544 ce
->ce_file
= mh_xstrdup(*file
);
2546 ce
->ce_file
= mh_xstrdup(cachefile
);
2549 if ((tempfile
= m_mktemp2(NULL
, invo_name
, NULL
, NULL
)) == NULL
) {
2550 die("unable to create temporary file in %s",
2553 ce
->ce_file
= mh_xstrdup(tempfile
);
2556 if ((ce
->ce_fp
= fopen (ce
->ce_file
, "w+")) == NULL
) {
2557 content_error (ce
->ce_file
, ct
, "unable to fopen for reading/writing");
2566 vec
[vecp
++] = r1bindex (ftp
, '/');
2567 vec
[vecp
++] = e
->eb_site
;
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";
2582 adios ("fork", "unable to");
2586 close (fileno (ce
->ce_fp
));
2588 fprintf (stderr
, "unable to exec ");
2594 if (pidXwait (child_id
, NULL
)) {
2595 username
= password
= NULL
;
2605 chmod (cachefile
, cachetype
? m_gmprot () : 0444);
2610 mask
= umask (cachetype
? ~m_gmprot () : 0222);
2611 if ((fp
= fopen (cachefile
, "w"))) {
2613 FILE *gp
= ce
->ce_fp
;
2615 fseek (gp
, 0L, SEEK_SET
);
2617 while ((cc
= fread (buffer
, sizeof(*buffer
), sizeof(buffer
), gp
))
2619 if ((int) fwrite (buffer
, sizeof(*buffer
), cc
, fp
) < cc
) {
2620 advise ("openFTP", "fwrite");
2625 admonish (ce
->ce_file
, "error reading");
2626 (void) m_unlink (cachefile
);
2627 } else if (ferror (fp
)) {
2628 admonish (cachefile
, "error writing");
2629 (void) m_unlink (cachefile
);
2637 fseek (ce
->ce_fp
, 0L, SEEK_SET
);
2638 *file
= ce
->ce_file
;
2639 return fileno (ce
->ce_fp
);
2650 return init_encoding (ct
, openMail
);
2655 openMail (CT ct
, char **file
)
2657 int child_id
, fd
, vecp
;
2659 char *bp
, buffer
[BUFSIZ
], *vec
[7];
2660 struct exbody
*e
= ct
->c_ctexbody
;
2661 CE ce
= &ct
->c_cefile
;
2663 switch (openExternal (e
->eb_parent
, e
->eb_content
, ce
, file
, &fd
)) {
2674 if (!e
->eb_server
) {
2675 content_error (NULL
, ct
, "missing server parameter");
2679 /* Get buffer ready to go */
2681 buflen
= sizeof(buffer
);
2683 /* Now, construct query message */
2684 snprintf (bp
, buflen
, "Retrieve content");
2690 snprintf (bp
, buflen
, " %s", e
->eb_partno
);
2696 snprintf (bp
, buflen
, " by asking %s\n\n%s\n? ",
2698 e
->eb_subject
? e
->eb_subject
: e
->eb_body
);
2700 /* Now, check answer */
2701 if (!read_yes_or_no_if_tty (buffer
))
2705 vec
[vecp
++] = r1bindex (mailproc
, '/');
2706 vec
[vecp
++] = e
->eb_server
;
2707 vec
[vecp
++] = "-subject";
2708 vec
[vecp
++] = e
->eb_subject
? e
->eb_subject
: "mail-server request";
2709 vec
[vecp
++] = "-body";
2710 vec
[vecp
++] = e
->eb_body
;
2716 advise ("fork", "unable to");
2720 execvp (mailproc
, vec
);
2721 fprintf (stderr
, "unable to exec ");
2727 if (pidXwait (child_id
, NULL
) == OK
)
2728 inform("request sent");
2732 if (*file
== NULL
) {
2734 if ((tempfile
= m_mktemp2(NULL
, invo_name
, NULL
, NULL
)) == NULL
) {
2735 die("unable to create temporary file in %s",
2738 ce
->ce_file
= mh_xstrdup(tempfile
);
2741 ce
->ce_file
= mh_xstrdup(*file
);
2745 if ((ce
->ce_fp
= fopen (ce
->ce_file
, "w+")) == NULL
) {
2746 content_error (ce
->ce_file
, ct
, "unable to fopen for reading/writing");
2750 /* showproc is for mhshow and mhstore, though mhlist -debug
2751 * prints it, too. */
2752 free(ct
->c_showproc
);
2753 ct
->c_showproc
= mh_xstrdup("true");
2755 fseek (ce
->ce_fp
, 0L, SEEK_SET
);
2756 *file
= ce
->ce_file
;
2757 return fileno (ce
->ce_fp
);
2768 return init_encoding (ct
, openURL
);
2773 openURL (CT ct
, char **file
)
2775 struct exbody
*e
= ct
->c_ctexbody
;
2776 CE ce
= &ct
->c_cefile
;
2777 char *urlprog
, *program
;
2778 char buffer
[BUFSIZ
], cachefile
[BUFSIZ
];
2782 struct msgs_array args
= { 0, 0, NULL
};
2785 if ((urlprog
= context_find(nmhaccessurl
)) && *urlprog
== '\0')
2789 content_error(NULL
, ct
, "No entry for nmh-access-url in profile");
2793 switch (openExternal(e
->eb_parent
, e
->eb_content
, ce
, file
, &fd
)) {
2805 content_error(NULL
, ct
, "missing url parameter");
2809 ce
->ce_unlink
= (*file
== NULL
);
2811 cachefile
[0] = '\0';
2813 if (find_cache(NULL
, wcachesw
, &cachetype
, e
->eb_content
->c_id
,
2814 cachefile
, sizeof(cachefile
)) != NOTOK
) {
2815 if (*file
== NULL
) {
2822 ce
->ce_file
= mh_xstrdup(*file
);
2824 ce
->ce_file
= mh_xstrdup(cachefile
);
2827 if ((tempfile
= m_mktemp2(NULL
, invo_name
, NULL
, NULL
)) == NULL
) {
2828 die("unable to create temporary file in %s",
2831 ce
->ce_file
= mh_xstrdup(tempfile
);
2834 if ((ce
->ce_fp
= fopen(ce
->ce_file
, "w+")) == NULL
) {
2835 content_error(ce
->ce_file
, ct
, "unable to fopen for read/writing");
2839 switch (child_id
= fork()) {
2841 adios ("fork", "unable to");
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 ");
2857 if (pidXwait(child_id
, NULL
)) {
2865 chmod(cachefile
, cachetype
? m_gmprot() : 0444);
2870 mask
= umask (cachetype
? ~m_gmprot() : 0222);
2871 if ((fp
= fopen(cachefile
, "w"))) {
2873 FILE *gp
= ce
->ce_fp
;
2875 fseeko(gp
, 0, SEEK_SET
);
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");
2886 admonish(ce
->ce_file
, "error reading");
2887 (void) m_unlink (cachefile
);
2894 fseeko(ce
->ce_fp
, 0, SEEK_SET
);
2895 *file
= ce
->ce_file
;
2896 return fileno(ce
->ce_fp
);
2901 * Stores MD5 digest (in cp, from Content-MD5 header) in ct->c_digest. It
2902 * has to be base64 decoded.
2905 readDigest (CT ct
, char *cp
)
2907 unsigned char *digest
;
2910 if (decodeBase64 (cp
, &digest
, &len
, 0, NULL
) == OK
) {
2911 const size_t maxlen
= sizeof ct
->c_digest
;
2913 if (strlen ((char *) digest
) <= maxlen
) {
2914 memcpy (ct
->c_digest
, digest
, maxlen
);
2919 fprintf (stderr
, "MD5 digest=");
2920 for (i
= 0; i
< maxlen
; ++i
) {
2921 fprintf (stderr
, "%02x", ct
->c_digest
[i
] & 0xff);
2923 fprintf (stderr
, "\n");
2929 fprintf (stderr
, "invalid MD5 digest (got %d octets)\n",
2930 (int) strlen ((char *) digest
));
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
2944 get_leftover_mp_content (CT ct
, int before
/* or after */)
2946 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
2948 bool found_boundary
= false;
2954 char *content
= NULL
;
2956 if (! m
) return NOTOK
;
2959 if (! m
->mp_parts
|| ! m
->mp_parts
->mp_part
) return NOTOK
;
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
);
2967 struct part
*last_subpart
= NULL
;
2968 struct part
*subpart
;
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
;
2975 if (last_subpart
== NULL
) return NOTOK
;
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
);
2984 /* Back up by 1 to pick up the newline. */
2985 while ((gotlen
= getline(&bufp
, &buflen
, ct
->c_fp
)) != -1) {
2987 /* Don't look beyond beginning of first subpart (before) or
2988 next part (after). */
2989 if (read
> max
) bufp
[read
-max
] = '\0';
2992 if (! strcmp (bufp
, boundary
)) {
2993 found_boundary
= true;
2996 if (! found_boundary
&& ! strcmp (bufp
, boundary
)) {
2997 found_boundary
= true;
3002 if ((before
&& ! found_boundary
) || (! before
&& found_boundary
)) {
3004 char *old_content
= content
;
3005 content
= concat (content
, bufp
, NULL
);
3009 ? concat ("\n", bufp
, NULL
)
3010 : concat (bufp
, NULL
);
3015 if (found_boundary
|| read
> max
) break;
3017 if (read
> max
) break;
3021 /* Skip the newline if that's all there is. */
3025 /* Remove trailing newline, except at EOF. */
3026 if ((before
|| ! feof (ct
->c_fp
)) &&
3027 (cp
= content
+ strlen (content
)) > content
&&
3032 if (strlen (content
) > 1) {
3034 m
->mp_content_before
= content
;
3036 m
->mp_content_after
= content
;
3051 ct_type_str (int type
)
3054 case CT_APPLICATION
:
3055 return "application";
3071 return "unknown_type";
3077 ct_subtype_str (int type
, int subtype
)
3080 case CT_APPLICATION
:
3082 case APPLICATION_OCTETS
:
3084 case APPLICATION_POSTSCRIPT
:
3085 return "postscript";
3087 return "unknown_app_subtype";
3091 case MESSAGE_RFC822
:
3093 case MESSAGE_PARTIAL
:
3095 case MESSAGE_EXTERNAL
:
3098 return "unknown_msg_subtype";
3104 case MULTI_ALTERNATE
:
3105 return "alternative";
3108 case MULTI_PARALLEL
:
3113 return "unknown_multipart_subtype";
3124 return "unknown_text_subtype";
3127 return "unknown_type";
3133 ct_str_type (const char *type
)
3135 struct str2init
*s2i
;
3137 for (s2i
= str2cts
; s2i
->si_key
; ++s2i
) {
3138 if (! strcasecmp (type
, s2i
->si_key
)) {
3142 if (! s2i
->si_key
&& ! uprf (type
, "X-")) {
3151 ct_str_subtype (int type
, const char *subtype
)
3156 case CT_APPLICATION
:
3157 for (kv
= SubApplication
; kv
->kv_key
; ++kv
) {
3158 if (! strcasecmp (subtype
, kv
->kv_key
)) {
3162 return kv
->kv_value
;
3164 for (kv
= SubMessage
; kv
->kv_key
; ++kv
) {
3165 if (! strcasecmp (subtype
, kv
->kv_key
)) {
3169 return kv
->kv_value
;
3171 for (kv
= SubMultiPart
; kv
->kv_key
; ++kv
) {
3172 if (! strcasecmp (subtype
, kv
->kv_key
)) {
3176 return kv
->kv_value
;
3178 for (kv
= SubText
; kv
->kv_key
; ++kv
) {
3179 if (! strcasecmp (subtype
, kv
->kv_key
)) {
3183 return kv
->kv_value
;
3190 /* Find the content type and InitFunc for the CT. */
3191 const struct str2init
*
3192 get_ct_init (int type
)
3194 const struct str2init
*sp
;
3196 for (sp
= str2cts
; sp
->si_key
; ++sp
) {
3197 if (type
== sp
->si_val
) {
3206 ce_str (int encoding
)
3212 return "quoted-printable";
3228 /* Find the content type and InitFunc for the content encoding method. */
3229 const struct str2init
*
3230 get_ce_method (const char *method
)
3232 struct str2init
*sp
;
3234 for (sp
= str2ces
; sp
->si_key
; ++sp
) {
3235 if (! strcasecmp (method
, sp
->si_key
)) {
3244 * Parse a series of MIME attributes (or parameters) given a header as
3247 * Arguments include:
3249 * filename - Name of input file (for error messages)
3250 * fieldname - Name of field being processed
3251 * headerp - Pointer to pointer of the beginning of the MIME attributes.
3252 * Updated to point to end of attributes when finished.
3253 * param_head - Pointer to head of parameter list
3254 * param_tail - Pointer to tail of parameter list
3255 * commentp - Pointer to header comment pointer (may be NULL)
3257 * Returns OK if parsing was successful, NOTOK if parsing failed, and
3258 * DONE to indicate a benign error (minor parsing error, but the program
3263 parse_header_attrs (const char *filename
, const char *fieldname
,
3264 char **header_attrp
, PM
*param_head
, PM
*param_tail
,
3267 char *cp
= *header_attrp
;
3273 struct sectlist
*next
;
3279 struct sectlist
*sechead
;
3280 struct parmlist
*next
;
3281 } *pp
, *pp2
, *phead
= NULL
;
3283 while (*cp
== ';') {
3284 char *dp
, *vp
, *up
, *nameptr
, *valptr
, *charset
= NULL
, *lang
= NULL
;
3285 bool encoded
= false;
3286 bool partial
= false;
3287 int len
= 0, index
= 0;
3290 while (isspace ((unsigned char) *cp
))
3294 get_comment (filename
, fieldname
, &cp
, commentp
) == NOTOK
) {
3299 if (! suppress_extraneous_trailing_semicolon_warning
) {
3300 inform("extraneous trailing ';' in message %s's %s: "
3301 "parameter list", filename
, fieldname
);
3306 /* down case the attribute name */
3307 for (dp
= cp
; istoken ((unsigned char) *dp
); dp
++)
3308 *dp
= tolower ((unsigned char) *dp
);
3310 for (up
= dp
; isspace ((unsigned char) *dp
);)
3312 if (dp
== cp
|| *dp
!= '=') {
3313 inform("invalid parameter in message %s's %s: field\n"
3314 " parameter %s (error detected at offset %ld)",
3315 filename
, fieldname
, cp
, (long)(dp
- cp
));
3320 * To handle RFC 2231, we have to deal with the following extensions:
3322 * name*=encoded-value
3323 * name*<N>=part-N-of-a-parameter-value
3324 * name*<N>*=encoded-part-N-of-a-parameter-value
3327 * If there's a * right before the equal sign, it's encoded.
3328 * If there's a * and one or more digits, then it's section N.
3330 * Remember we can have one or the other, or both. cp points to
3331 * beginning of name, up points past the last character in the
3335 for (vp
= cp
; vp
< up
; vp
++) {
3336 if (*vp
== '*' && vp
< up
- 1) {
3340 if (*vp
== '*' && vp
== up
- 1) {
3342 } else if (partial
) {
3343 if (isdigit((unsigned char) *vp
))
3344 index
= *vp
- '0' + index
* 10;
3346 inform("invalid parameter index in message %s's %s: field"
3347 "\n (parameter %s)", filename
, fieldname
, cp
);
3356 * Break out the parameter name and value sections and allocate
3360 nameptr
= mh_xmalloc(len
+ 1);
3361 strncpy(nameptr
, cp
, len
);
3362 nameptr
[len
] = '\0';
3364 for (dp
++; isspace ((unsigned char) *dp
);)
3369 * Single quotes delimit the character set and language tag.
3370 * They are required on the first section (or a complete
3375 while (*vp
!= '\'' && !isspace((unsigned char) *vp
) &&
3381 charset
= mh_xmalloc(len
+ 1);
3382 strncpy(charset
, dp
, len
);
3383 charset
[len
] = '\0';
3389 inform("missing charset in message %s's %s: field\n"
3390 " (parameter %s)", filename
, fieldname
, nameptr
);
3396 while (*vp
!= '\'' && !isspace((unsigned char) *vp
) &&
3403 lang
= mh_xmalloc(len
+ 1);
3404 strncpy(lang
, dp
, len
);
3411 inform("missing language tag in message %s's %s: field\n"
3412 " (parameter %s)", filename
, fieldname
, nameptr
);
3422 * At this point vp should be pointing at the beginning
3423 * of the encoded value/section. Continue until we reach
3424 * the end or get whitespace. But first, calculate the
3425 * length so we can allocate the correct buffer size.
3428 for (vp
= dp
, len
= 0; istoken(*vp
); vp
++) {
3430 if (*(vp
+ 1) == '\0' ||
3431 !isxdigit((unsigned char) *(vp
+ 1)) ||
3432 *(vp
+ 2) == '\0' ||
3433 !isxdigit((unsigned char) *(vp
+ 2))) {
3434 inform("invalid encoded sequence in message %s's %s: field\n"
3435 " (parameter %s)", filename
, fieldname
, nameptr
);
3446 up
= valptr
= mh_xmalloc(len
+ 1);
3448 for (vp
= dp
; istoken(*vp
); vp
++) {
3450 *up
++ = decode_qp(*(vp
+ 1), *(vp
+ 2));
3461 * A "normal" string. If it's got a leading quote, then we
3462 * strip the quotes out. Otherwise go until we reach the end
3463 * or get whitespace. Note we scan it twice; once to get the
3464 * length, then the second time copies it into the destination
3471 for (cp
= dp
+ 1;;) {
3475 inform("invalid quoted-string in message %s's %s: field\n"
3476 " (parameter %s)", filename
, fieldname
, nameptr
);
3496 for (cp
= dp
; istoken (*cp
); cp
++) {
3501 valptr
= mh_xmalloc(len
+ 1);
3505 for (cp
= dp
+ 1, vp
= valptr
, i
= 0; i
< len
; i
++) {
3513 strncpy(valptr
, cp
= dp
, len
);
3521 * If 'partial' is set, we don't allocate a parameter now. We
3522 * put it on the parameter linked list to be reassembled later.
3524 * "phead" points to a list of all parameters we need to reassemble.
3525 * Each parameter has a list of sections. We insert the sections in
3530 for (pp
= phead
; pp
!= NULL
; pp
= pp
->next
) {
3531 if (strcasecmp(nameptr
, pp
->name
) == 0) {
3546 * Insert this into the section linked list
3554 if (pp
->sechead
== NULL
|| pp
->sechead
->index
> index
) {
3555 sp
->next
= pp
->sechead
;
3558 for (sp2
= pp
->sechead
; sp2
!= NULL
; sp2
= sp2
->next
) {
3559 if (sp2
->index
== sp
->index
) {
3560 inform("duplicate index (%d) in message %s's %s: field"
3561 "\n (parameter %s)", sp
->index
, filename
,
3562 fieldname
, nameptr
);
3565 if (sp2
->index
< sp
->index
&&
3566 (sp2
->next
== NULL
|| sp2
->next
->index
> sp
->index
)) {
3567 sp
->next
= sp2
->next
;
3574 inform("Internal error: cannot insert partial param "
3575 "in message %s's %s: field\n (parameter %s)",
3576 filename
, fieldname
, nameptr
);
3582 * Save our charset and lang tags.
3585 if (index
== 0 && encoded
) {
3587 pp
->charset
= charset
;
3592 pm
= add_param(param_head
, param_tail
, nameptr
, valptr
, 1);
3593 pm
->pm_charset
= charset
;
3597 while (isspace ((unsigned char) *cp
))
3601 get_comment (filename
, fieldname
, &cp
, commentp
) == NOTOK
) {
3607 * Now that we're done, reassemble all of the partial parameters.
3610 for (pp
= phead
; pp
!= NULL
; ) {
3614 for (sp
= pp
->sechead
; sp
!= NULL
; sp
= sp
->next
) {
3615 if (sp
->index
!= pindex
++) {
3616 inform("missing section %d for parameter in message "
3617 "%s's %s: field\n (parameter %s)", pindex
- 1,
3618 filename
, fieldname
, pp
->name
);
3624 p
= q
= mh_xmalloc(tlen
+ 1);
3625 for (sp
= pp
->sechead
; sp
!= NULL
; ) {
3626 memcpy(q
, sp
->value
, sp
->len
);
3636 pm
= add_param(param_head
, param_tail
, pp
->name
, p
, 1);
3637 pm
->pm_charset
= pp
->charset
;
3638 pm
->pm_lang
= pp
->lang
;
3649 * Return the charset for a particular content type.
3653 content_charset (CT ct
)
3655 char *ret_charset
= NULL
;
3657 ret_charset
= get_param(ct
->c_ctinfo
.ci_first_pm
, "charset", '?', 0);
3659 return ret_charset
? ret_charset
: mh_xstrdup("US-ASCII");
3664 * Create a string based on a list of output parameters. Assume that this
3665 * parameter string will be appended to an existing header, so start out
3666 * with the separator (;). Perform RFC 2231 encoding when necessary.
3670 output_params(size_t initialwidth
, PM params
, int *offsetout
, int external
)
3672 char *paramout
= NULL
;
3673 char line
[CPERLIN
* 2], *q
;
3674 int curlen
, index
, cont
, encode
, i
;
3675 size_t valoff
, numchars
;
3677 while (params
!= NULL
) {
3683 if (external
&& strcasecmp(params
->pm_name
, "body") == 0)
3686 if (strlen(params
->pm_name
) > CPERLIN
) {
3687 inform("Parameter name \"%s\" is too long", params
->pm_name
);
3692 curlen
= param_len(params
, index
, valoff
, &encode
, &cont
, &numchars
);
3695 * Loop until we get a parameter that fits within a line. We
3696 * assume new lines start with a tab, so check our overflow based
3706 * At this point we're definitely continuing the line, so
3707 * be sure to include the parameter name and section index.
3710 q
+= snprintf(q
, sizeof(line
) - (q
- line
), "%s*%d",
3711 params
->pm_name
, index
);
3714 * Both of these functions do a NUL termination
3718 i
= encode_param(params
, q
, sizeof(line
) - (q
- line
),
3719 numchars
, valoff
, index
);
3721 i
= normal_param(params
, q
, sizeof(line
) - (q
- line
),
3731 curlen
= param_len(params
, index
, valoff
, &encode
, &cont
,
3736 * "line" starts with a ;\n\t, so that doesn't count against
3737 * the length. But add 8 since it starts with a tab; that's
3738 * how we end up with 5.
3741 initialwidth
= strlen(line
) + 5;
3744 * At this point the line should be built, so add it to our
3745 * current output buffer.
3748 paramout
= add(line
, paramout
);
3752 * If this won't fit on the line, start a new one. Save room in
3753 * case we need a semicolon on the end
3756 if (initialwidth
+ curlen
> CPERLIN
- 1) {
3768 * At this point, we're either finishing a continued parameter, or
3769 * we're working on a new one.
3773 q
+= snprintf(q
, sizeof(line
) - (q
- line
), "%s*%d",
3774 params
->pm_name
, index
);
3776 strncpy(q
, params
->pm_name
, sizeof(line
) - (q
- line
));
3781 i
= encode_param(params
, q
, sizeof(line
) - (q
- line
),
3782 strlen(params
->pm_value
+ valoff
), valoff
, index
);
3784 i
= normal_param(params
, q
, sizeof(line
) - (q
- line
),
3785 strlen(params
->pm_value
+ valoff
), valoff
);
3792 paramout
= add(line
, paramout
);
3793 initialwidth
+= strlen(line
);
3795 params
= params
->pm_next
;
3799 *offsetout
= initialwidth
;
3805 * Calculate the size of a parameter.
3809 * pm - The parameter being output
3810 * index - If continuing the parameter, the index of the section
3812 * valueoff - The current offset into the parameter value that we're
3813 * working on (previous sections have consumed valueoff bytes).
3814 * encode - Set if we should perform encoding on this parameter section
3815 * (given that we're consuming bytesfit bytes).
3816 * cont - Set if the remaining data in value will not fit on a single
3817 * line and will need to be continued.
3818 * bytesfit - The number of bytes that we can consume from the parameter
3819 * value and still fit on a completely new line. The
3820 * calculation assumes the new line starts with a tab,
3821 * includes the parameter name and any encoding, and fits
3822 * within CPERLIN bytes. Will always be at least 1.
3826 param_len(PM pm
, int index
, size_t valueoff
, int *encode
, int *cont
,
3829 char *start
= pm
->pm_value
+ valueoff
, *p
, indexchar
[32];
3830 size_t len
= 0, fit
= 0;
3831 int fitlimit
= 0, eightbit
, maxfit
;
3836 * Add up the length. First, start with the parameter name.
3839 len
= strlen(pm
->pm_name
);
3842 * Scan the parameter value and see if we need to do encoding for this
3846 eightbit
= contains8bit(start
, NULL
);
3849 * Determine if we need to encode this section. Encoding is necessary if:
3851 * - There are any 8-bit characters at all and we're on the first
3853 * - There are 8-bit characters within N bytes of our section start.
3854 * N is calculated based on the number of bytes it would take to
3855 * reach CPERLIN. Specifically:
3856 * 8 (starting tab) +
3857 * strlen(param name) +
3858 * 4 ('* for section marker, '=', opening/closing '"')
3860 * is the number of bytes used by everything that isn't part of the
3861 * value. So that gets subtracted from CPERLIN.
3864 snprintf(indexchar
, sizeof(indexchar
), "%d", index
);
3865 maxfit
= CPERLIN
- (12 + len
+ strlen(indexchar
));
3866 if ((eightbit
&& index
== 0) || contains8bit(start
, start
+ maxfit
)) {
3870 len
++; /* Add in equal sign */
3874 * We're using maxfit as a marker for how many characters we can
3875 * fit into the line. Bump it by two because we're not using quotes
3882 * If we don't have a charset or language tag in this parameter,
3886 if (! pm
->pm_charset
) {
3887 pm
->pm_charset
= mh_xstrdup(write_charset_8bit());
3888 if (strcasecmp(pm
->pm_charset
, "US-ASCII") == 0)
3889 die("8-bit characters in parameter \"%s\", but "
3890 "local character set is US-ASCII", pm
->pm_name
);
3893 pm
->pm_lang
= mh_xstrdup(""); /* Default to a blank lang tag */
3895 len
++; /* For the encoding marker */
3898 int enclen
= strlen(pm
->pm_charset
) + strlen(pm
->pm_lang
) + 2;
3903 * We know we definitely need to include an index. maxfit already
3904 * includes the section marker.
3906 len
+= strlen(indexchar
);
3908 for (p
= start
; *p
!= '\0'; p
++) {
3909 if (isparamencode(*p
)) {
3917 * Just so there's no confusion: maxfit is counting OUTPUT
3918 * characters (post-encoding). fit is counting INPUT characters.
3920 if (! fitlimit
&& maxfit
>= 0)
3922 else if (! fitlimit
)
3927 * Calculate the string length, but add room for quoting \
3928 * and " if necessary. Also account for quotes at beginning
3931 for (p
= start
; *p
!= '\0'; p
++) {
3942 if (! fitlimit
&& maxfit
>= 0)
3944 else if (! fitlimit
)
3961 * Output an encoded parameter string.
3965 encode_param(PM pm
, char *output
, size_t len
, size_t valuelen
,
3966 size_t valueoff
, int index
)
3968 size_t outlen
= 0, n
;
3969 char *endptr
= output
+ len
, *p
;
3972 * First, output the marker for an encoded string.
3980 * If the index is 0, output the character set and language tag.
3981 * If theses were NULL, they should have already been filled in
3986 n
= snprintf(output
, len
- outlen
, "%s'%s'", pm
->pm_charset
,
3990 if (output
> endptr
) {
3991 inform("Internal error: parameter buffer overflow");
3997 * Copy over the value, encoding if necessary
4000 p
= pm
->pm_value
+ valueoff
;
4001 while (valuelen
-- > 0) {
4002 if (isparamencode(*p
)) {
4003 n
= snprintf(output
, len
- outlen
, "%%%02X", (unsigned char) *p
++);
4010 if (output
> endptr
) {
4011 inform("Internal error: parameter buffer overflow");
4022 * Output a "normal" parameter, without encoding. Be sure to escape
4023 * quotes and backslashes if necessary.
4027 normal_param(PM pm
, char *output
, size_t len
, size_t valuelen
,
4031 char *endptr
= output
+ len
, *p
;
4037 p
= pm
->pm_value
+ valueoff
;
4039 while (valuelen
-- > 0) {
4050 if (output
> endptr
) {
4051 inform("Internal error: parameter buffer overflow");
4056 if (output
- 2 > endptr
) {
4057 inform("Internal error: parameter buffer overflow");
4068 * Add a parameter to the parameter linked list
4072 add_param(PM
*first
, PM
*last
, char *name
, char *value
, int nocopy
)
4077 pm
->pm_name
= nocopy
? name
: getcpy(name
);
4078 pm
->pm_value
= nocopy
? value
: getcpy(value
);
4081 (*last
)->pm_next
= pm
;
4092 * Either replace a current parameter with a new value, or add the parameter
4093 * to the parameter linked list.
4097 replace_param(PM
*first
, PM
*last
, char *name
, char *value
, int nocopy
)
4101 for (pm
= *first
; pm
!= NULL
; pm
= pm
->pm_next
) {
4102 if (strcasecmp(name
, pm
->pm_name
) == 0) {
4104 * If nocopy is set, it's assumed that we own both name
4105 * and value. We don't need name, so we discard it now.
4110 pm
->pm_value
= nocopy
? value
: getcpy(value
);
4115 return add_param(first
, last
, name
, value
, nocopy
);
4119 * Retrieve a parameter value from a parameter linked list. If the parameter
4120 * value needs converted to the local character set, do that now.
4124 get_param(PM first
, const char *name
, char replace
, int fetchonly
)
4126 while (first
!= NULL
) {
4127 if (strcasecmp(name
, first
->pm_name
) == 0) {
4129 return first
->pm_value
;
4130 return getcpy(get_param_value(first
, replace
));
4132 first
= first
->pm_next
;
4139 * Return a parameter value, converting to the local character set if
4144 get_param_value(PM pm
, char replace
)
4146 static char buffer
[4096]; /* I hope no parameters are larger */
4147 size_t bufsize
= sizeof(buffer
);
4152 ICONV_CONST
char *p
;
4153 #else /* HAVE_ICONV */
4155 #endif /* HAVE_ICONV */
4160 * If we don't have a character set indicated, it's assumed to be
4161 * US-ASCII. If it matches our character set, we don't need to convert
4165 if (!pm
->pm_charset
|| check_charset(pm
->pm_charset
,
4166 strlen(pm
->pm_charset
))) {
4167 return pm
->pm_value
;
4171 * In this case, we need to convert. If we have iconv support, use
4172 * that. Otherwise, go through and simply replace every non-ASCII
4173 * character with the substitution character.
4178 bufsize
= sizeof(buffer
);
4179 utf8
= strcasecmp(pm
->pm_charset
, "UTF-8") == 0;
4181 cd
= iconv_open(get_charset(), pm
->pm_charset
);
4182 if (cd
== (iconv_t
) -1) {
4186 inbytes
= strlen(pm
->pm_value
);
4190 if (iconv(cd
, &p
, &inbytes
, &q
, &bufsize
) == (size_t)-1) {
4191 if (errno
!= EILSEQ
) {
4196 * Reset shift state, substitute our character,
4197 * try to restart conversion.
4200 iconv(cd
, NULL
, NULL
, &q
, &bufsize
);
4213 for (++p
, --inbytes
;
4214 inbytes
> 0 && (((unsigned char) *p
) & 0xc0) == 0x80;
4233 #endif /* HAVE_ICONV */
4236 * Take everything non-ASCII and substitute the replacement character
4240 bufsize
= sizeof(buffer
);
4241 for (p
= pm
->pm_value
; *p
!= '\0' && bufsize
> 1; p
++, q
++, bufsize
--) {
4242 if (isascii((unsigned char) *p
) && isprint((unsigned char) *p
))