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.
9 #include "sbr/m_getfld.h"
10 #include "sbr/read_yes_or_no_if_tty.h"
11 #include "sbr/concat.h"
12 #include "sbr/r1bindex.h"
13 #include "sbr/ruserpass.h"
14 #include "sbr/fmt_rfc2047.h"
16 #include "sbr/check_charset.h"
17 #include "sbr/getcpy.h"
18 #include "sbr/context_find.h"
19 #include "sbr/pidstatus.h"
20 #include "sbr/arglist.h"
21 #include "sbr/error.h"
27 #include "h/mhparse.h"
30 #include "h/mhcachesbr.h"
31 #include "sbr/m_mktemp.h"
35 #endif /* HAVE_ICONV */
36 #include "sbr/base64.h"
41 int checksw
= 0; /* check Content-MD5 field */
44 * These are for mhfixmsg to:
45 * 1) Instruct parser not to detect invalid Content-Transfer-Encoding
47 * 2) Suppress the warning about bogus multipart content, and report it.
48 * 3) Suppress the warning about extraneous trailing ';' in header parameter
51 bool skip_mp_cte_check
;
52 bool suppress_bogus_mp_content_warning
;
53 bool bogus_mp_content
;
54 bool suppress_extraneous_trailing_semicolon_warning
;
57 * By default, suppress warning about multiple MIME-Version header fields.
59 bool suppress_multiple_mime_version_warning
= true;
61 /* list of preferred type/subtype pairs, for -prefer */
62 mime_type_subtype mime_preference
[NPREFS
];
67 * Structures for TEXT messages
69 struct k2v SubText
[] = {
70 { "plain", TEXT_PLAIN
},
71 { "richtext", TEXT_RICHTEXT
}, /* defined in RFC 1341 */
72 { "enriched", TEXT_ENRICHED
}, /* defined in RFC 1896 */
73 { NULL
, TEXT_UNKNOWN
} /* this one must be last! */
76 /* Charset[] removed -- yozo. Mon Oct 8 01:03:41 JST 2012 */
79 * Structures for MULTIPART messages
81 struct k2v SubMultiPart
[] = {
82 { "mixed", MULTI_MIXED
},
83 { "alternative", MULTI_ALTERNATE
},
84 { "digest", MULTI_DIGEST
},
85 { "parallel", MULTI_PARALLEL
},
86 { "related", MULTI_RELATED
},
87 { NULL
, MULTI_UNKNOWN
} /* this one must be last! */
91 * Structures for MESSAGE messages
93 struct k2v SubMessage
[] = {
94 { "rfc822", MESSAGE_RFC822
},
95 { "partial", MESSAGE_PARTIAL
},
96 { "external-body", MESSAGE_EXTERNAL
},
97 { NULL
, MESSAGE_UNKNOWN
} /* this one must be last! */
101 * Structure for APPLICATION messages
103 struct k2v SubApplication
[] = {
104 { "octet-stream", APPLICATION_OCTETS
},
105 { "postscript", APPLICATION_POSTSCRIPT
},
106 { NULL
, APPLICATION_UNKNOWN
} /* this one must be last! */
110 * Mapping of names of CTE types in mhbuild directives
112 static struct k2v EncodingType
[] = {
115 { "q-p", CE_QUOTED
},
116 { "quoted-printable", CE_QUOTED
},
117 { "b64", CE_BASE64
},
118 { "base64", CE_BASE64
},
126 static CT
get_content (FILE *, char *, int);
127 static int get_comment (const char *, const char *, char **, char **);
129 static int InitGeneric (CT
);
130 static int InitText (CT
);
131 static int InitMultiPart (CT
);
132 static void reverse_parts (CT
);
133 static void prefer_parts(CT ct
);
134 static int InitMessage (CT
);
135 static int InitApplication (CT
);
136 static int init_encoding (CT
, OpenCEFunc
);
137 static unsigned long size_encoding (CT
);
138 static int InitBase64 (CT
);
139 static int openBase64 (CT
, char **);
140 static int InitQuoted (CT
);
141 static int openQuoted (CT
, char **);
142 static int Init7Bit (CT
);
143 static int openExternal (CT
, CT
, CE
, char **, int *);
144 static int InitFile (CT
);
145 static int openFile (CT
, char **);
146 static int InitFTP (CT
);
147 static int openFTP (CT
, char **);
148 static int InitMail (CT
);
149 static int openMail (CT
, char **);
150 static int readDigest (CT
, char *);
151 static int get_leftover_mp_content (CT
, int);
152 static int InitURL (CT
);
153 static int openURL (CT
, char **);
154 static int parse_header_attrs (const char *, const char *, char **, PM
*,
156 static size_t param_len(PM
, int, size_t, int *, int *, size_t *);
157 static size_t normal_param(PM
, char *, size_t, size_t, size_t);
158 static int get_dispo (char *, CT
, int);
160 struct str2init str2cts
[] = {
161 { "application", CT_APPLICATION
, InitApplication
},
162 { "audio", CT_AUDIO
, InitGeneric
},
163 { "image", CT_IMAGE
, InitGeneric
},
164 { "message", CT_MESSAGE
, InitMessage
},
165 { "multipart", CT_MULTIPART
, InitMultiPart
},
166 { "text", CT_TEXT
, InitText
},
167 { "video", CT_VIDEO
, InitGeneric
},
168 { NULL
, CT_EXTENSION
, NULL
}, /* these two must be last! */
169 { NULL
, CT_UNKNOWN
, NULL
},
172 struct str2init str2ces
[] = {
173 { "base64", CE_BASE64
, InitBase64
},
174 { "quoted-printable", CE_QUOTED
, InitQuoted
},
175 { "8bit", CE_8BIT
, Init7Bit
},
176 { "7bit", CE_7BIT
, Init7Bit
},
177 { "binary", CE_BINARY
, Init7Bit
},
178 { NULL
, CE_EXTENSION
, NULL
}, /* these two must be last! */
179 { NULL
, CE_UNKNOWN
, NULL
},
183 * NOTE WELL: si_key MUST NOT have value of NOTOK
185 * si_val is 1 if access method is anonymous.
187 struct str2init str2methods
[] = {
188 { "afs", 1, InitFile
},
189 { "anon-ftp", 1, InitFTP
},
190 { "ftp", 0, InitFTP
},
191 { "local-file", 0, InitFile
},
192 { "mail-server", 0, InitMail
},
193 { "url", 0, InitURL
},
199 * Main entry point for parsing a MIME message or file.
200 * It returns the Content structure for the top level
201 * entity in the file.
205 parse_mime (char *file
)
214 bogus_mp_content
= false;
217 * Check if file is actually standard input
219 if ((is_stdin
= !(strcmp (file
, "-")))) {
220 char *tfile
= m_mktemp2(NULL
, invo_name
, NULL
, &fp
);
222 advise("mhparse", "unable to create temporary file in %s",
226 file
= mh_xstrdup(tfile
);
228 while ((n
= fread(buffer
, 1, sizeof(buffer
), stdin
)) > 0) {
229 if (fwrite(buffer
, 1, n
, fp
) != n
) {
230 (void) m_unlink (file
);
231 advise (file
, "error copying to temporary file");
237 if (ferror (stdin
)) {
238 (void) m_unlink (file
);
239 advise ("stdin", "error reading");
243 (void) m_unlink (file
);
244 advise (file
, "error writing");
247 fseek (fp
, 0L, SEEK_SET
);
248 } else if (stat (file
, &statbuf
) == NOTOK
) {
249 advise (file
, "unable to stat");
251 } else if (S_ISDIR(statbuf
.st_mode
)) {
252 /* Don't try to parse a directory. */
253 inform("%s is a directory", file
);
255 } else if ((fp
= fopen (file
, "r")) == NULL
) {
256 advise (file
, "unable to read");
260 if (!(ct
= get_content (fp
, file
, 1))) {
262 (void) m_unlink (file
);
263 inform("unable to decode %s", file
);
268 ct
->c_unlink
= 1; /* temp file to remove */
272 if (ct
->c_end
== 0L) {
273 fseek (fp
, 0L, SEEK_END
);
274 ct
->c_end
= ftell (fp
);
277 if (ct
->c_ctinitfnx
&& (*ct
->c_ctinitfnx
) (ct
) == NOTOK
) {
289 * Main routine for reading/parsing the headers
290 * of a message content.
292 * toplevel = 1 # we are at the top level of the message
293 * toplevel = 0 # we are inside message type or multipart type
294 * # other than multipart/digest
295 * toplevel = -1 # we are inside multipart/digest
296 * NB: on failure we will fclose(in)!
300 get_content (FILE *in
, char *file
, int toplevel
)
303 char buf
[NMH_BUFSIZ
], name
[NAMESZ
];
307 m_getfld_state_t gstate
;
309 /* allocate the content structure */
312 ct
->c_file
= mh_xstrdup(FENDNULL(file
));
313 ct
->c_begin
= ftell (ct
->c_fp
) + 1;
316 * Parse the header fields for this
317 * content into a linked list.
319 gstate
= m_getfld_state_init(in
);
320 m_getfld_track_filepos2(&gstate
);
321 for (compnum
= 1;;) {
322 int bufsz
= sizeof buf
;
323 switch (state
= m_getfld2(&gstate
, name
, buf
, &bufsz
)) {
328 /* get copies of the buffers */
329 np
= mh_xstrdup(name
);
330 vp
= mh_xstrdup(buf
);
332 /* if necessary, get rest of field */
333 while (state
== FLDPLUS
) {
335 state
= m_getfld2(&gstate
, name
, buf
, &bufsz
);
336 vp
= add (buf
, vp
); /* add to previous value */
339 /* Now add the header data to the list */
340 add_header (ct
, np
, vp
);
342 /* continue, to see if this isn't the last header field */
343 ct
->c_begin
= ftell (in
) + 1;
347 /* There are two cases. The unusual one is when there is no
348 * blank line between the headers and the body. This is
349 * indicated by the name of the header starting with `:'.
351 * For both cases, normal first, `1' is the desired c_begin
352 * file position for the start of the body, and `2' is the
353 * file position when buf is returned.
355 * f o o : b a r \n \n b o d y \n bufsz = 6
357 * f o o : b a r \n b o d y \n bufsz = 4
360 * For the normal case, bufsz includes the
361 * header-terminating `\n', even though it is not in buf,
362 * but bufsz isn't affected when it's missing in the unusual
364 if (name
[0] == ':') {
365 ct
->c_begin
= ftell(in
) - bufsz
;
367 ct
->c_begin
= ftell (in
) - (bufsz
- 1);
372 ct
->c_begin
= ftell (in
);
377 die("message format error in component #%d", compnum
);
380 die("getfld() returned %d", state
);
383 /* break out of the loop */
386 m_getfld_state_destroy (&gstate
);
389 * Read the content headers. We will parse the
390 * MIME related header fields into their various
391 * structures and set internal flags related to
392 * content type/subtype, etc.
395 hp
= ct
->c_first_hf
; /* start at first header field */
397 /* Get MIME-Version field */
398 if (!strcasecmp (hp
->name
, VRSN_FIELD
)) {
403 vrsn
= mh_xstrdup(FENDNULL(hp
->value
));
405 /* Now, cleanup this field */
408 while (isspace ((unsigned char) *cp
))
410 for (dp
= strchr(cp
, '\n'); dp
; dp
= strchr(dp
, '\n'))
412 for (dp
= cp
+ strlen (cp
) - 1; dp
>= cp
; dp
--)
413 if (!isspace ((unsigned char) *dp
))
417 fprintf (stderr
, "%s: %s\n", VRSN_FIELD
, cp
);
420 get_comment (ct
->c_file
, VRSN_FIELD
, &cp
, NULL
) == NOTOK
)
423 for (dp
= cp
; istoken (*dp
); dp
++)
427 ucmp
= !strcasecmp (cp
, VRSN_VALUE
);
430 inform("message %s has unknown value for %s: field (%s), continuing...",
431 ct
->c_file
, VRSN_FIELD
, cp
);
436 if (! suppress_multiple_mime_version_warning
)
437 inform("message %s has multiple %s: fields",
438 ct
->c_file
, VRSN_FIELD
);
442 else if (!strcasecmp (hp
->name
, TYPE_FIELD
)) {
443 /* Get Content-Type field */
444 struct str2init
*s2i
;
445 CI ci
= &ct
->c_ctinfo
;
447 /* Check if we've already seen a Content-Type header */
449 inform("message %s has multiple %s: fields",
450 ct
->c_file
, TYPE_FIELD
);
454 /* Parse the Content-Type field */
455 if (get_ctinfo (hp
->value
, ct
, 0) == NOTOK
)
459 * Set the Init function and the internal
460 * flag for this content type.
462 for (s2i
= str2cts
; s2i
->si_key
; s2i
++)
463 if (!strcasecmp (ci
->ci_type
, s2i
->si_key
))
465 if (!s2i
->si_key
&& !uprf (ci
->ci_type
, "X-"))
467 ct
->c_type
= s2i
->si_val
;
468 ct
->c_ctinitfnx
= s2i
->si_init
;
470 else if (!strcasecmp (hp
->name
, ENCODING_FIELD
)) {
471 /* Get Content-Transfer-Encoding field */
473 struct str2init
*s2i
;
476 * Check if we've already seen the
477 * Content-Transfer-Encoding field
480 inform("message %s has multiple %s: fields",
481 ct
->c_file
, ENCODING_FIELD
);
485 /* get copy of this field */
486 ct
->c_celine
= cp
= mh_xstrdup(FENDNULL(hp
->value
));
488 while (isspace ((unsigned char) *cp
))
490 for (dp
= cp
; istoken (*dp
); dp
++)
496 * Find the internal flag and Init function
497 * for this transfer encoding.
499 for (s2i
= str2ces
; s2i
->si_key
; s2i
++)
500 if (!strcasecmp (cp
, s2i
->si_key
))
502 if (!s2i
->si_key
&& !uprf (cp
, "X-"))
505 ct
->c_encoding
= s2i
->si_val
;
507 /* Call the Init function for this encoding */
508 if (s2i
->si_init
&& (*s2i
->si_init
) (ct
) == NOTOK
)
511 else if (!strcasecmp (hp
->name
, MD5_FIELD
)) {
512 /* Get Content-MD5 field */
518 if (ct
->c_digested
) {
519 inform("message %s has multiple %s: fields",
520 ct
->c_file
, MD5_FIELD
);
524 ep
= cp
= mh_xstrdup(FENDNULL(hp
->value
)); /* get a copy */
526 while (isspace ((unsigned char) *cp
))
528 for (dp
= strchr(cp
, '\n'); dp
; dp
= strchr(dp
, '\n'))
530 for (dp
= cp
+ strlen (cp
) - 1; dp
>= cp
; dp
--)
531 if (!isspace ((unsigned char) *dp
))
535 fprintf (stderr
, "%s: %s\n", MD5_FIELD
, cp
);
538 get_comment (ct
->c_file
, MD5_FIELD
, &cp
, NULL
) == NOTOK
) {
543 for (dp
= cp
; *dp
&& !isspace ((unsigned char) *dp
); dp
++)
551 else if (!strcasecmp (hp
->name
, ID_FIELD
)) {
552 /* Get Content-ID field */
553 ct
->c_id
= add (hp
->value
, ct
->c_id
);
555 else if (!strcasecmp (hp
->name
, DESCR_FIELD
)) {
556 /* Get Content-Description field */
557 ct
->c_descr
= add (hp
->value
, ct
->c_descr
);
559 else if (!strcasecmp (hp
->name
, DISPO_FIELD
)) {
560 /* Get Content-Disposition field */
561 if (get_dispo(hp
->value
, ct
, 0) == NOTOK
)
566 hp
= hp
->next
; /* next header field */
570 * Check if we saw a Content-Type field.
571 * If not, then assign a default value for
572 * it, and the Init function.
576 * If we are inside a multipart/digest message,
577 * so default type is message/rfc822
580 if (get_ctinfo ("message/rfc822", ct
, 0) == NOTOK
)
582 ct
->c_type
= CT_MESSAGE
;
583 ct
->c_ctinitfnx
= InitMessage
;
586 * Else default type is text/plain
588 if (get_ctinfo ("text/plain", ct
, 0) == NOTOK
)
590 ct
->c_type
= CT_TEXT
;
591 ct
->c_ctinitfnx
= InitText
;
595 /* Use default Transfer-Encoding, if necessary */
597 ct
->c_encoding
= CE_7BIT
;
610 * small routine to add header field to list
614 add_header (CT ct
, char *name
, char *value
)
618 /* allocate header field structure */
621 /* link data into header structure */
626 /* link header structure into the list */
627 if (ct
->c_first_hf
== NULL
) {
628 ct
->c_first_hf
= hp
; /* this is the first */
631 ct
->c_last_hf
->next
= hp
; /* add it to the end */
640 * Parse Content-Type line and (if `magic' is non-zero) mhbuild composition
641 * directives. Fills in the information of the CTinfo structure.
644 get_ctinfo (char *cp
, CT ct
, int magic
)
653 /* store copy of Content-Type line */
654 cp
= ct
->c_ctline
= mh_xstrdup(FENDNULL(cp
));
656 while (isspace ((unsigned char) *cp
)) /* trim leading spaces */
659 /* change newlines to spaces */
660 for (dp
= strchr(cp
, '\n'); dp
; dp
= strchr(dp
, '\n'))
663 /* trim trailing spaces */
664 for (dp
= cp
+ strlen (cp
) - 1; dp
>= cp
; dp
--)
665 if (!isspace ((unsigned char) *dp
))
670 fprintf (stderr
, "%s: %s\n", TYPE_FIELD
, cp
);
672 if (*cp
== '(' && get_comment (ct
->c_file
, TYPE_FIELD
, &cp
,
673 &ci
->ci_comment
) == NOTOK
)
676 for (dp
= cp
; istoken (*dp
); dp
++)
680 ci
->ci_type
= mh_xstrdup(cp
); /* store content type */
685 inform("invalid %s: field in message %s (empty type)",
686 TYPE_FIELD
, ct
->c_file
);
689 to_lower(ci
->ci_type
);
691 while (isspace ((unsigned char) *cp
))
694 if (*cp
== '(' && get_comment (ct
->c_file
, TYPE_FIELD
, &cp
,
695 &ci
->ci_comment
) == NOTOK
)
700 ci
->ci_subtype
= mh_xstrdup("");
705 while (isspace ((unsigned char) *cp
))
708 if (*cp
== '(' && get_comment (ct
->c_file
, TYPE_FIELD
, &cp
,
709 &ci
->ci_comment
) == NOTOK
)
712 for (dp
= cp
; istoken (*dp
); dp
++)
716 ci
->ci_subtype
= mh_xstrdup(cp
); /* store the content subtype */
720 if (!*ci
->ci_subtype
) {
721 inform("invalid %s: field in message %s (empty subtype for \"%s\")",
722 TYPE_FIELD
, ct
->c_file
, ci
->ci_type
);
725 to_lower(ci
->ci_subtype
);
728 while (isspace ((unsigned char) *cp
))
731 if (*cp
== '(' && get_comment (ct
->c_file
, TYPE_FIELD
, &cp
,
732 &ci
->ci_comment
) == NOTOK
)
735 if ((status
= parse_header_attrs (ct
->c_file
, TYPE_FIELD
, &cp
,
736 &ci
->ci_first_pm
, &ci
->ci_last_pm
,
737 &ci
->ci_comment
)) != OK
) {
738 return status
== NOTOK
? NOTOK
: OK
;
742 * Get any <Content-Id> given in buffer
744 if (magic
&& *cp
== '<') {
747 if (!(dp
= strchr(ct
->c_id
= ++cp
, '>'))) {
748 inform("invalid ID in message %s", ct
->c_file
);
754 ct
->c_id
= concat ("<", ct
->c_id
, ">\n", NULL
);
760 while (isspace ((unsigned char) *cp
))
765 * Get any [Content-Description] given in buffer.
767 if (magic
&& *cp
== '[') {
769 for (dp
= cp
+ strlen (cp
) - 1; dp
>= cp
; dp
--)
773 inform("invalid description in message %s", ct
->c_file
);
781 ct
->c_descr
= concat (ct
->c_descr
, "\n", NULL
);
787 while (isspace ((unsigned char) *cp
))
792 * Get any {Content-Disposition} given in buffer.
794 if (magic
&& *cp
== '{') {
796 for (dp
= cp
+ strlen (cp
) - 1; dp
>= cp
; dp
--)
800 inform("invalid disposition in message %s", ct
->c_file
);
808 if (get_dispo(cp
, ct
, 1) != OK
)
814 while (isspace ((unsigned char) *cp
))
819 * Get any extension directives (right now just the content transfer
820 * encoding, but maybe others) that we care about.
823 if (magic
&& *cp
== '*') {
825 * See if it's a CTE we match on
830 while (*cp
!= '\0' && ! isspace((unsigned char) *cp
))
834 inform("invalid null transfer encoding specification");
841 ct
->c_reqencoding
= CE_UNKNOWN
;
843 for (kv
= EncodingType
; kv
->kv_key
; kv
++) {
844 if (strcasecmp(kv
->kv_key
, dp
) == 0) {
845 ct
->c_reqencoding
= kv
->kv_value
;
850 if (ct
->c_reqencoding
== CE_UNKNOWN
) {
851 inform("invalid CTE specification: \"%s\"", dp
);
855 while (isspace ((unsigned char) *cp
))
860 * Check if anything is left over
864 ci
->ci_magic
= mh_xstrdup(cp
);
866 /* If there is a Content-Disposition header and it doesn't
867 have a *filename=, extract it from the magic contents.
868 The r1bindex call skips any leading directory
870 if (ct
->c_dispo_type
&&
871 !get_param(ct
->c_dispo_first
, "filename", '_', 1)) {
872 add_param(&ct
->c_dispo_first
, &ct
->c_dispo_last
, "filename",
873 r1bindex(ci
->ci_magic
, '/'), 0);
877 inform("extraneous information in message %s's %s: field\n"
878 " (%s)", ct
->c_file
, TYPE_FIELD
, cp
);
886 * Parse out a Content-Disposition header. A lot of this is cribbed from
890 get_dispo (char *cp
, CT ct
, int buildflag
)
892 char *dp
, *dispoheader
;
897 * Save the whole copy of the Content-Disposition header, unless we're
898 * processing a mhbuild directive. A NULL c_dispo will be a flag to
899 * mhbuild that the disposition header needs to be generated at that
903 dispoheader
= cp
= mh_xstrdup(FENDNULL(cp
));
905 while (isspace ((unsigned char) *cp
)) /* trim leading spaces */
908 /* change newlines to spaces */
909 for (dp
= strchr(cp
, '\n'); dp
; dp
= strchr(dp
, '\n'))
912 /* trim trailing spaces */
913 for (dp
= cp
+ strlen (cp
) - 1; dp
>= cp
; dp
--)
914 if (!isspace ((unsigned char) *dp
))
919 fprintf (stderr
, "%s: %s\n", DISPO_FIELD
, cp
);
921 if (*cp
== '(' && get_comment (ct
->c_file
, DISPO_FIELD
, &cp
, NULL
) ==
927 for (dp
= cp
; istoken (*dp
); dp
++)
931 ct
->c_dispo_type
= mh_xstrdup(cp
); /* store disposition type */
935 if (*cp
== '(' && get_comment (ct
->c_file
, DISPO_FIELD
, &cp
, NULL
) == NOTOK
)
938 if ((status
= parse_header_attrs (ct
->c_file
, DISPO_FIELD
, &cp
,
939 &ct
->c_dispo_first
, &ct
->c_dispo_last
,
941 if (status
== NOTOK
) {
946 inform("extraneous information in message %s's %s: field\n (%s)",
947 ct
->c_file
, DISPO_FIELD
, cp
);
953 ct
->c_dispo
= dispoheader
;
960 get_comment (const char *filename
, const char *fieldname
, char **ap
,
965 char c
, buffer
[BUFSIZ
], *dp
;
975 inform("invalid comment in message %s's %s: field",
976 filename
, fieldname
);
981 if ((c
= *cp
++) == '\0')
1004 if ((dp
= *commentp
)) {
1005 *commentp
= concat (dp
, " ", buffer
, NULL
);
1008 *commentp
= mh_xstrdup(buffer
);
1012 while (isspace ((unsigned char) *cp
))
1023 * Handles content types audio, image, and video.
1024 * There's not much to do right here.
1032 return OK
; /* not much to do here */
1043 char buffer
[BUFSIZ
];
1048 CI ci
= &ct
->c_ctinfo
;
1050 /* check for missing subtype */
1051 if (!*ci
->ci_subtype
)
1052 ci
->ci_subtype
= add ("plain", ci
->ci_subtype
);
1055 ct
->c_subtype
= ct_str_subtype (CT_TEXT
, ci
->ci_subtype
);
1057 /* allocate text character set structure */
1059 ct
->c_ctparams
= (void *) t
;
1061 /* scan for charset parameter */
1062 for (pm
= ci
->ci_first_pm
; pm
; pm
= pm
->pm_next
)
1063 if (!strcasecmp (pm
->pm_name
, "charset"))
1066 /* check if content specified a character set */
1068 chset
= pm
->pm_value
;
1069 t
->tx_charset
= CHARSET_SPECIFIED
;
1071 t
->tx_charset
= CHARSET_UNSPECIFIED
;
1075 * If we can not handle character set natively,
1076 * then check profile for string to modify the
1077 * terminal or display method.
1079 * termproc is for mhshow, though mhlist -debug prints it, too.
1081 if (chset
!= NULL
&& !check_charset (chset
, strlen (chset
))) {
1082 snprintf (buffer
, sizeof(buffer
), "%s-charset-%s", invo_name
, chset
);
1083 if ((cp
= context_find (buffer
)))
1084 ct
->c_termproc
= mh_xstrdup(cp
);
1096 InitMultiPart (CT ct
)
1106 struct multipart
*m
;
1107 struct part
*part
, **next
;
1108 CI ci
= &ct
->c_ctinfo
;
1113 * The encoding for multipart messages must be either
1114 * 7bit, 8bit, or binary (per RFC 2045).
1116 if (! skip_mp_cte_check
&& ct
->c_encoding
!= CE_7BIT
&&
1117 ct
->c_encoding
!= CE_8BIT
&& ct
->c_encoding
!= CE_BINARY
) {
1118 /* Copy the Content-Transfer-Encoding header field body so we can
1119 remove any trailing whitespace and leading blanks from it. */
1120 char *cte
= mh_xstrdup(ct
->c_celine
? ct
->c_celine
: "(null)");
1122 bp
= cte
+ strlen (cte
) - 1;
1123 while (bp
>= cte
&& isspace ((unsigned char) *bp
)) *bp
-- = '\0';
1124 for (bp
= cte
; isblank((unsigned char)*bp
); ++bp
) continue;
1126 inform("\"%s/%s\" type in message %s must be encoded in\n"
1127 "7bit, 8bit, or binary, per RFC 2045 (6.4). "
1128 "mhfixmsg -fixcte can fix it, or\n"
1129 "manually edit the file and change the \"%s\"\n"
1130 "Content-Transfer-Encoding to one of those. For now, continuing...",
1131 ci
->ci_type
, ci
->ci_subtype
, ct
->c_file
, bp
);
1138 ct
->c_subtype
= ct_str_subtype (CT_MULTIPART
, ci
->ci_subtype
);
1141 * Check for "boundary" parameter, which is
1142 * required for multipart messages.
1145 for (pm
= ci
->ci_first_pm
; pm
; pm
= pm
->pm_next
) {
1146 if (!strcasecmp (pm
->pm_name
, "boundary")) {
1152 /* complain if boundary parameter is missing */
1154 inform("a \"boundary\" parameter is mandatory for \"%s/%s\" type in message %s's %s: field",
1155 ci
->ci_type
, ci
->ci_subtype
, ct
->c_file
, TYPE_FIELD
);
1159 /* allocate primary structure for multipart info */
1161 ct
->c_ctparams
= (void *) m
;
1163 /* check if boundary parameter contains only whitespace characters */
1164 for (cp
= bp
; isspace ((unsigned char) *cp
); cp
++)
1167 inform("invalid \"boundary\" parameter for \"%s/%s\" type in message %s's %s: field",
1168 ci
->ci_type
, ci
->ci_subtype
, ct
->c_file
, TYPE_FIELD
);
1172 /* remove trailing whitespace from boundary parameter */
1173 for (cp
= bp
, dp
= cp
+ strlen (cp
) - 1; dp
> cp
; dp
--)
1174 if (!isspace ((unsigned char) *dp
))
1178 /* record boundary separators */
1179 m
->mp_start
= concat (bp
, "\n", NULL
);
1180 m
->mp_stop
= concat (bp
, "--\n", NULL
);
1182 if (!ct
->c_fp
&& (ct
->c_fp
= fopen (ct
->c_file
, "r")) == NULL
) {
1183 advise (ct
->c_file
, "unable to open for reading");
1187 fseek (fp
= ct
->c_fp
, pos
= ct
->c_begin
, SEEK_SET
);
1189 next
= &m
->mp_parts
;
1193 while ((gotlen
= getline(&bufp
, &buflen
, fp
)) != -1) {
1198 if (bufp
[0] != '-' || bufp
[1] != '-')
1201 if (strcmp (bufp
+ 2, m
->mp_start
))
1206 next
= &part
->mp_next
;
1208 if (!(p
= get_content (fp
, ct
->c_file
,
1209 ct
->c_subtype
== MULTI_DIGEST
? -1 : 0))) {
1217 fseek (fp
, pos
, SEEK_SET
);
1220 if (strcmp (bufp
+ 2, m
->mp_start
) == 0) {
1224 p
->c_end
= ftell(fp
) - (gotlen
+ 1);
1225 if (p
->c_end
< p
->c_begin
)
1226 p
->c_begin
= p
->c_end
;
1231 if (strcmp (bufp
+ 2, m
->mp_stop
) == 0)
1236 if (! suppress_bogus_mp_content_warning
) {
1237 inform("bogus multipart content in message %s", ct
->c_file
);
1239 bogus_mp_content
= true;
1241 if (!inout
&& part
) {
1243 p
->c_end
= ct
->c_end
;
1245 if (p
->c_begin
>= p
->c_end
) {
1246 for (next
= &m
->mp_parts
; *next
!= part
;
1247 next
= &((*next
)->mp_next
))
1256 /* reverse the order of the parts for multipart/alternative */
1257 if (ct
->c_subtype
== MULTI_ALTERNATE
) {
1263 * label all subparts with part number, and
1264 * then initialize the content of the subpart.
1269 char partnam
[BUFSIZ
];
1272 snprintf (partnam
, sizeof(partnam
), "%s.", ct
->c_partno
);
1273 pp
= partnam
+ strlen (partnam
);
1278 for (part
= m
->mp_parts
, partnum
= 1; part
;
1279 part
= part
->mp_next
, partnum
++) {
1282 sprintf (pp
, "%d", partnum
);
1283 p
->c_partno
= mh_xstrdup(partnam
);
1285 /* initialize the content of the subparts */
1286 if (p
->c_ctinitfnx
&& (*p
->c_ctinitfnx
) (p
) == NOTOK
) {
1295 get_leftover_mp_content (ct
, 1);
1296 get_leftover_mp_content (ct
, 0);
1306 * reverse the order of the parts of a multipart/alternative,
1307 * presumably to put the "most favored" alternative first, for
1308 * ease of choosing/displaying it later on. from a mail message on
1309 * nmh-workers, from kenh:
1310 * "Stock" MH 6.8.5 did not have a reverse_parts() function, but I
1311 * see code in mhn that did the same thing... According to the RCS
1312 * logs, that code was around from the initial checkin of mhn.c by
1313 * John Romine in 1992, which is as far back as we have."
1316 reverse_parts (CT ct
)
1318 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
1322 /* Reverse the order of its parts by walking the mp_parts list
1323 and pushing each node to the front. */
1324 for (part
= m
->mp_parts
, m
->mp_parts
= NULL
; part
; part
= next
) {
1325 next
= part
->mp_next
;
1326 part
->mp_next
= m
->mp_parts
;
1332 move_preferred_part(CT ct
, mime_type_subtype
*pref
)
1334 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
1335 struct part
*part
, *prev
, *head
, *nhead
, *ntail
;
1339 /* move the matching part(s) to the head of the list: walk the
1340 * list of parts, move matching parts to a new list (maintaining
1341 * their order), and finally, concatenate the old list onto the
1348 head
->mp_next
= m
->mp_parts
;
1349 nhead
->mp_next
= NULL
;
1353 part
= head
->mp_next
;
1354 while (part
!= NULL
) {
1355 ci
= &part
->mp_part
->c_ctinfo
;
1356 if (!strcasecmp(ci
->ci_type
, pref
->type
) &&
1358 !strcasecmp(ci
->ci_subtype
, pref
->subtype
))) {
1359 prev
->mp_next
= part
->mp_next
;
1360 part
->mp_next
= NULL
;
1361 ntail
->mp_next
= part
;
1363 part
= prev
->mp_next
;
1366 part
= prev
->mp_next
;
1369 ntail
->mp_next
= head
->mp_next
;
1370 m
->mp_parts
= nhead
->mp_next
;
1374 * move parts that match the user's preferences (-prefer) to the head
1375 * of the line. process preferences in reverse so first one given
1376 * ends up first in line
1382 for (i
= 0; i
< npreferred
; i
++)
1383 move_preferred_part(ct
, mime_preference
+ i
);
1388 /* parse_mime() arranges alternates in reverse (priority) order. This
1389 function can be used to reverse them back. This will put, for
1390 example, a text/plain part before a text/html part in a
1391 multipart/alternative part, for example, where it belongs. */
1393 reverse_alternative_parts (CT ct
)
1395 if (ct
->c_type
== CT_MULTIPART
) {
1396 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
1399 if (ct
->c_subtype
== MULTI_ALTERNATE
) {
1403 /* And call recursively on each part of a multipart. */
1404 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
1405 reverse_alternative_parts (part
->mp_part
);
1418 CI ci
= &ct
->c_ctinfo
;
1420 if ((ct
->c_encoding
!= CE_7BIT
) && (ct
->c_encoding
!= CE_8BIT
)) {
1421 inform("\"%s/%s\" type in message %s should be encoded in "
1422 "7bit or 8bit, continuing...", ci
->ci_type
, ci
->ci_subtype
,
1427 /* check for missing subtype */
1428 if (!*ci
->ci_subtype
)
1429 ci
->ci_subtype
= add ("rfc822", ci
->ci_subtype
);
1432 ct
->c_subtype
= ct_str_subtype (CT_MESSAGE
, ci
->ci_subtype
);
1434 switch (ct
->c_subtype
) {
1435 case MESSAGE_RFC822
:
1438 case MESSAGE_PARTIAL
:
1444 ct
->c_ctparams
= (void *) p
;
1446 /* scan for parameters "id", "number", and "total" */
1447 for (pm
= ci
->ci_first_pm
; pm
; pm
= pm
->pm_next
) {
1448 if (!strcasecmp (pm
->pm_name
, "id")) {
1449 p
->pm_partid
= mh_xstrdup(FENDNULL(pm
->pm_value
));
1452 if (!strcasecmp (pm
->pm_name
, "number")) {
1453 if (sscanf (pm
->pm_value
, "%d", &p
->pm_partno
) != 1
1454 || p
->pm_partno
< 1) {
1456 inform("invalid %s parameter for \"%s/%s\" type in message %s's %s field",
1457 pm
->pm_name
, ci
->ci_type
, ci
->ci_subtype
,
1458 ct
->c_file
, TYPE_FIELD
);
1463 if (!strcasecmp (pm
->pm_name
, "total")) {
1464 if (sscanf (pm
->pm_value
, "%d", &p
->pm_maxno
) != 1
1473 || (p
->pm_maxno
&& p
->pm_partno
> p
->pm_maxno
)) {
1474 inform("invalid parameters for \"%s/%s\" type in message %s's %s field",
1475 ci
->ci_type
, ci
->ci_subtype
, ct
->c_file
, TYPE_FIELD
);
1481 case MESSAGE_EXTERNAL
:
1489 ct
->c_ctparams
= (void *) e
;
1492 && (ct
->c_fp
= fopen (ct
->c_file
, "r")) == NULL
) {
1493 advise (ct
->c_file
, "unable to open for reading");
1497 fseek (fp
= ct
->c_fp
, ct
->c_begin
, SEEK_SET
);
1499 if (!(p
= get_content (fp
, ct
->c_file
, 0))) {
1507 p
->c_ceopenfnx
= NULL
;
1508 if ((exresult
= params_external (ct
, 0)) != NOTOK
1509 && p
->c_ceopenfnx
== openMail
) {
1513 if ((size
= ct
->c_end
- p
->c_begin
) <= 0) {
1515 content_error (NULL
, ct
,
1516 "empty body for access-type=mail-server");
1520 e
->eb_body
= bp
= mh_xmalloc ((unsigned) size
);
1521 fseek (p
->c_fp
, p
->c_begin
, SEEK_SET
);
1523 switch (cc
= fread (bp
, sizeof(*bp
), size
, p
->c_fp
)) {
1525 adios ("failed", "fread");
1528 die("unexpected EOF from fread");
1531 bp
+= cc
, size
-= cc
;
1538 p
->c_end
= p
->c_begin
;
1543 if (exresult
== NOTOK
)
1545 if (e
->eb_flags
== NOTOK
)
1548 switch (p
->c_type
) {
1553 if (p
->c_subtype
!= MESSAGE_RFC822
)
1557 e
->eb_partno
= ct
->c_partno
;
1559 (*p
->c_ctinitfnx
) (p
);
1574 params_external (CT ct
, int composing
)
1577 struct exbody
*e
= (struct exbody
*) ct
->c_ctparams
;
1578 CI ci
= &ct
->c_ctinfo
;
1580 ct
->c_ceopenfnx
= NULL
;
1581 for (pm
= ci
->ci_first_pm
; pm
; pm
= pm
->pm_next
) {
1582 if (!strcasecmp (pm
->pm_name
, "access-type")) {
1583 struct str2init
*s2i
;
1584 CT p
= e
->eb_content
;
1586 for (s2i
= str2methods
; s2i
->si_key
; s2i
++)
1587 if (!strcasecmp (pm
->pm_value
, s2i
->si_key
))
1590 e
->eb_access
= pm
->pm_value
;
1591 e
->eb_flags
= NOTOK
;
1592 p
->c_encoding
= CE_EXTERNAL
;
1595 e
->eb_access
= s2i
->si_key
;
1596 e
->eb_flags
= s2i
->si_val
;
1597 p
->c_encoding
= CE_EXTERNAL
;
1599 /* Call the Init function for this external type */
1600 if ((*s2i
->si_init
)(p
) == NOTOK
)
1604 if (!strcasecmp (pm
->pm_name
, "name")) {
1605 e
->eb_name
= pm
->pm_value
;
1608 if (!strcasecmp (pm
->pm_name
, "permission")) {
1609 e
->eb_permission
= pm
->pm_value
;
1612 if (!strcasecmp (pm
->pm_name
, "site")) {
1613 e
->eb_site
= pm
->pm_value
;
1616 if (!strcasecmp (pm
->pm_name
, "directory")) {
1617 e
->eb_dir
= pm
->pm_value
;
1620 if (!strcasecmp (pm
->pm_name
, "mode")) {
1621 e
->eb_mode
= pm
->pm_value
;
1624 if (!strcasecmp (pm
->pm_name
, "size")) {
1625 sscanf (pm
->pm_value
, "%lu", &e
->eb_size
);
1628 if (!strcasecmp (pm
->pm_name
, "server")) {
1629 e
->eb_server
= pm
->pm_value
;
1632 if (!strcasecmp (pm
->pm_name
, "subject")) {
1633 e
->eb_subject
= pm
->pm_value
;
1636 if (!strcasecmp (pm
->pm_name
, "url")) {
1638 * According to RFC 2017, we have to remove all whitespace from
1642 char *u
, *p
= pm
->pm_value
;
1643 e
->eb_url
= u
= mh_xmalloc(strlen(pm
->pm_value
) + 1);
1645 for (; *p
!= '\0'; p
++) {
1646 if (! isspace((unsigned char) *p
))
1653 if (composing
&& !strcasecmp (pm
->pm_name
, "body")) {
1654 e
->eb_body
= getcpy (pm
->pm_value
);
1659 if (!e
->eb_access
) {
1660 inform("invalid parameters for \"%s/%s\" type in message %s's %s field",
1661 ci
->ci_type
, ci
->ci_subtype
, ct
->c_file
, TYPE_FIELD
);
1674 InitApplication (CT ct
)
1676 CI ci
= &ct
->c_ctinfo
;
1679 ct
->c_subtype
= ct_str_subtype (CT_APPLICATION
, ci
->ci_subtype
);
1686 * TRANSFER ENCODINGS
1690 init_encoding (CT ct
, OpenCEFunc openfnx
)
1692 ct
->c_ceopenfnx
= openfnx
;
1693 ct
->c_ceclosefnx
= close_encoding
;
1694 ct
->c_cesizefnx
= size_encoding
;
1701 close_encoding (CT ct
)
1703 CE ce
= &ct
->c_cefile
;
1712 static unsigned long
1713 size_encoding (CT ct
)
1718 CE ce
= &ct
->c_cefile
;
1721 if (ce
->ce_fp
&& fstat (fileno (ce
->ce_fp
), &st
) != NOTOK
)
1722 return (long) st
.st_size
;
1725 if (stat (ce
->ce_file
, &st
) != NOTOK
)
1726 return (long) st
.st_size
;
1730 if (ct
->c_encoding
== CE_EXTERNAL
)
1731 return ct
->c_end
- ct
->c_begin
;
1734 if ((fd
= (*ct
->c_ceopenfnx
) (ct
, &file
)) == NOTOK
)
1735 return ct
->c_end
- ct
->c_begin
;
1737 if (fstat (fd
, &st
) != NOTOK
)
1738 size
= (long) st
.st_size
;
1742 (*ct
->c_ceclosefnx
) (ct
);
1754 return init_encoding (ct
, openBase64
);
1759 openBase64 (CT ct
, char **file
)
1763 bool own_ct_fp
= false;
1764 char *cp
, *buffer
= NULL
;
1765 /* sbeck -- handle suffixes */
1767 CE ce
= &ct
->c_cefile
;
1768 unsigned char *decoded
;
1770 unsigned char digest
[16];
1773 fseek (ce
->ce_fp
, 0L, SEEK_SET
);
1778 if ((ce
->ce_fp
= fopen (ce
->ce_file
, "r")) == NULL
) {
1779 content_error (ce
->ce_file
, ct
, "unable to fopen for reading");
1785 if (*file
== NULL
) {
1788 ce
->ce_file
= mh_xstrdup(*file
);
1792 /* sbeck@cise.ufl.edu -- handle suffixes */
1794 if ((cp
= context_find_by_type ("suffix", ci
->ci_type
, ci
->ci_subtype
))) {
1795 if (ce
->ce_unlink
) {
1796 /* Create temporary file with filename extension. */
1797 if ((ce
->ce_file
= m_mktemps(invo_name
, cp
, NULL
, NULL
)) == NULL
) {
1798 die("unable to create temporary file in %s",
1802 ce
->ce_file
= add (cp
, ce
->ce_file
);
1804 } else if (*file
== NULL
) {
1806 if ((tempfile
= m_mktemp2(NULL
, invo_name
, NULL
, NULL
)) == NULL
) {
1807 die("unable to create temporary file in %s",
1810 ce
->ce_file
= mh_xstrdup(tempfile
);
1813 if ((ce
->ce_fp
= fopen (ce
->ce_file
, "w+")) == NULL
) {
1814 content_error (ce
->ce_file
, ct
, "unable to fopen for reading/writing");
1818 if ((len
= ct
->c_end
- ct
->c_begin
) < 0)
1819 die("internal error(1)");
1821 buffer
= mh_xmalloc (len
+ 1);
1824 if ((ct
->c_fp
= fopen (ct
->c_file
, "r")) == NULL
) {
1825 content_error (ct
->c_file
, ct
, "unable to open for reading");
1831 lseek (fd
= fileno (ct
->c_fp
), (off_t
) ct
->c_begin
, SEEK_SET
);
1834 switch (cc
= read (fd
, cp
, len
)) {
1836 content_error (ct
->c_file
, ct
, "error reading from");
1840 content_error (NULL
, ct
, "premature eof");
1851 /* decodeBase64() requires null-terminated input. */
1854 if (decodeBase64 (buffer
, &decoded
, &decoded_len
, ct
->c_type
== CT_TEXT
,
1855 ct
->c_digested
? digest
: NULL
) != OK
)
1860 unsigned char *decoded_p
= decoded
;
1861 for (i
= 0; i
< decoded_len
; ++i
) {
1862 putc (*decoded_p
++, ce
->ce_fp
);
1865 if (ferror (ce
->ce_fp
)) {
1866 content_error (ce
->ce_file
, ct
, "error writing to");
1870 if (ct
->c_digested
) {
1871 if (memcmp(digest
, ct
->c_digest
,
1873 content_error (NULL
, ct
,
1874 "content integrity suspect (digest mismatch) -- continuing");
1877 fprintf (stderr
, "content integrity confirmed\n");
1883 fseek (ct
->c_fp
, 0L, SEEK_SET
);
1885 if (fflush (ce
->ce_fp
)) {
1886 content_error (ce
->ce_file
, ct
, "error writing to");
1890 fseek (ce
->ce_fp
, 0L, SEEK_SET
);
1893 *file
= ce
->ce_file
;
1899 return fileno (ce
->ce_fp
);
1906 free_encoding (ct
, 0);
1916 static char hex2nib
[0x80] = {
1917 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 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,
1921 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1922 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1923 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
1924 0x08, 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1925 0x00, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x00,
1926 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1927 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1928 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1929 0x00, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x00,
1930 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1931 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1932 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
1939 return init_encoding (ct
, openQuoted
);
1944 openQuoted (CT ct
, char **file
)
1946 int cc
, digested
, len
, quoted
;
1947 bool own_ct_fp
= false;
1953 CE ce
= &ct
->c_cefile
;
1954 /* sbeck -- handle suffixes */
1959 fseek (ce
->ce_fp
, 0L, SEEK_SET
);
1964 if ((ce
->ce_fp
= fopen (ce
->ce_file
, "r")) == NULL
) {
1965 content_error (ce
->ce_file
, ct
, "unable to fopen for reading");
1971 if (*file
== NULL
) {
1974 ce
->ce_file
= mh_xstrdup(*file
);
1978 /* sbeck@cise.ufl.edu -- handle suffixes */
1980 if ((cp
= context_find_by_type ("suffix", ci
->ci_type
, ci
->ci_subtype
))) {
1981 if (ce
->ce_unlink
) {
1982 /* Create temporary file with filename extension. */
1983 if ((ce
->ce_file
= m_mktemps(invo_name
, cp
, NULL
, NULL
)) == NULL
) {
1984 die("unable to create temporary file in %s",
1988 ce
->ce_file
= add (cp
, ce
->ce_file
);
1990 } else if (*file
== NULL
) {
1992 if ((tempfile
= m_mktemp2(NULL
, invo_name
, NULL
, NULL
)) == NULL
) {
1993 die("unable to create temporary file in %s",
1996 ce
->ce_file
= mh_xstrdup(tempfile
);
1999 if ((ce
->ce_fp
= fopen (ce
->ce_file
, "w+")) == NULL
) {
2000 content_error (ce
->ce_file
, ct
, "unable to fopen for reading/writing");
2004 if ((len
= ct
->c_end
- ct
->c_begin
) < 0)
2005 die("internal error(2)");
2008 if ((ct
->c_fp
= fopen (ct
->c_file
, "r")) == NULL
) {
2009 content_error (ct
->c_file
, ct
, "unable to open for reading");
2015 if ((digested
= ct
->c_digested
))
2016 MD5Init (&mdContext
);
2020 fseek (ct
->c_fp
, ct
->c_begin
, SEEK_SET
);
2022 if ((gotlen
= getline(&bufp
, &buflen
, ct
->c_fp
)) == -1) {
2023 content_error (NULL
, ct
, "premature eof");
2027 if ((cc
= gotlen
) > len
)
2031 for (ep
= (cp
= bufp
) + cc
- 1; cp
<= ep
; ep
--)
2032 if (!isspace ((unsigned char) *ep
))
2037 for (; cp
< ep
; cp
++) {
2039 /* in an escape sequence */
2041 /* at byte 1 of an escape sequence */
2042 mask
= hex2nib
[((unsigned char) *cp
) & 0x7f];
2043 /* next is byte 2 */
2046 /* at byte 2 of an escape sequence */
2048 mask
|= hex2nib
[((unsigned char) *cp
) & 0x7f];
2049 putc (mask
, ce
->ce_fp
);
2051 MD5Update (&mdContext
, &mask
, 1);
2052 if (ferror (ce
->ce_fp
)) {
2053 content_error (ce
->ce_file
, ct
, "error writing to");
2056 /* finished escape sequence; next may be literal or a new
2057 * escape sequence */
2060 /* on to next byte */
2064 /* not in an escape sequence */
2066 /* starting an escape sequence, or invalid '='? */
2067 if (cp
+ 1 < ep
&& cp
[1] == '\n') {
2068 /* "=\n" soft line break, eat the \n */
2072 if (cp
+ 1 >= ep
|| cp
+ 2 >= ep
) {
2073 /* We don't have 2 bytes left, so this is an invalid
2074 * escape sequence; just show the raw bytes (below). */
2075 } else if (isxdigit ((unsigned char) cp
[1]) &&
2076 isxdigit ((unsigned char) cp
[2])) {
2077 /* Next 2 bytes are hex digits, making this a valid escape
2078 * sequence; let's decode it (above). */
2082 /* One or both of the next 2 is out of range, making this
2083 * an invalid escape sequence; just show the raw bytes
2087 /* Just show the raw byte. */
2088 putc (*cp
, ce
->ce_fp
);
2091 MD5Update (&mdContext
, (unsigned char *) "\r\n",2);
2093 MD5Update (&mdContext
, (unsigned char *) cp
, 1);
2096 if (ferror (ce
->ce_fp
)) {
2097 content_error (ce
->ce_file
, ct
, "error writing to");
2103 content_error (NULL
, ct
,
2104 "invalid QUOTED-PRINTABLE encoding -- end-of-content while still quoting");
2108 fseek (ct
->c_fp
, 0L, SEEK_SET
);
2110 if (fflush (ce
->ce_fp
)) {
2111 content_error (ce
->ce_file
, ct
, "error writing to");
2116 unsigned char digest
[16];
2118 MD5Final (digest
, &mdContext
);
2119 if (memcmp(digest
, ct
->c_digest
,
2121 content_error (NULL
, ct
,
2122 "content integrity suspect (digest mismatch) -- continuing");
2124 fprintf (stderr
, "content integrity confirmed\n");
2127 fseek (ce
->ce_fp
, 0L, SEEK_SET
);
2130 *file
= ce
->ce_file
;
2136 return fileno (ce
->ce_fp
);
2139 free_encoding (ct
, 0);
2156 if (init_encoding (ct
, open7Bit
) == NOTOK
)
2159 ct
->c_cesizefnx
= NULL
; /* no need to decode for real size */
2165 open7Bit (CT ct
, char **file
)
2168 bool own_ct_fp
= false;
2169 char buffer
[BUFSIZ
];
2170 /* sbeck -- handle suffixes */
2173 CE ce
= &ct
->c_cefile
;
2176 fseek (ce
->ce_fp
, 0L, SEEK_SET
);
2181 if ((ce
->ce_fp
= fopen (ce
->ce_file
, "r")) == NULL
) {
2182 content_error (ce
->ce_file
, ct
, "unable to fopen for reading");
2188 if (*file
== NULL
) {
2191 ce
->ce_file
= mh_xstrdup(*file
);
2195 /* sbeck@cise.ufl.edu -- handle suffixes */
2197 if ((cp
= context_find_by_type ("suffix", ci
->ci_type
, ci
->ci_subtype
))) {
2198 if (ce
->ce_unlink
) {
2199 /* Create temporary file with filename extension. */
2200 if ((ce
->ce_file
= m_mktemps(invo_name
, cp
, NULL
, NULL
)) == NULL
) {
2201 die("unable to create temporary file in %s",
2205 ce
->ce_file
= add (cp
, ce
->ce_file
);
2207 } else if (*file
== NULL
) {
2209 if ((tempfile
= m_mktemp2(NULL
, invo_name
, NULL
, NULL
)) == NULL
) {
2210 die("unable to create temporary file in %s",
2213 ce
->ce_file
= mh_xstrdup(tempfile
);
2216 if ((ce
->ce_fp
= fopen (ce
->ce_file
, "w+")) == NULL
) {
2217 content_error (ce
->ce_file
, ct
, "unable to fopen for reading/writing");
2221 if (ct
->c_type
== CT_MULTIPART
) {
2222 CI ci
= &ct
->c_ctinfo
;
2226 fprintf (ce
->ce_fp
, "%s: %s/%s", TYPE_FIELD
, ci
->ci_type
, ci
->ci_subtype
);
2227 len
+= LEN(TYPE_FIELD
) + 2 + strlen (ci
->ci_type
)
2228 + 1 + strlen (ci
->ci_subtype
);
2229 buffer
= output_params(len
, ci
->ci_first_pm
, &len
, 0);
2232 fputs (buffer
, ce
->ce_fp
);
2236 if (ci
->ci_comment
) {
2237 if (len
+ 1 + (cc
= 2 + strlen (ci
->ci_comment
)) >= CPERLIN
) {
2238 fputs ("\n\t", ce
->ce_fp
);
2242 putc (' ', ce
->ce_fp
);
2245 fprintf (ce
->ce_fp
, "(%s)", ci
->ci_comment
);
2248 fprintf (ce
->ce_fp
, "\n");
2250 fprintf (ce
->ce_fp
, "%s:%s", ID_FIELD
, ct
->c_id
);
2252 fprintf (ce
->ce_fp
, "%s:%s", DESCR_FIELD
, ct
->c_descr
);
2254 fprintf (ce
->ce_fp
, "%s:%s", DISPO_FIELD
, ct
->c_dispo
);
2255 fprintf (ce
->ce_fp
, "\n");
2258 if ((len
= ct
->c_end
- ct
->c_begin
) < 0)
2259 die("internal error(3)");
2262 if ((ct
->c_fp
= fopen (ct
->c_file
, "r")) == NULL
) {
2263 content_error (ct
->c_file
, ct
, "unable to open for reading");
2269 lseek (fd
= fileno (ct
->c_fp
), (off_t
) ct
->c_begin
, SEEK_SET
);
2271 switch (cc
= read (fd
, buffer
, sizeof(buffer
) - 1)) {
2273 content_error (ct
->c_file
, ct
, "error reading from");
2277 content_error (NULL
, ct
, "premature eof");
2285 if ((int) fwrite (buffer
, sizeof(*buffer
), cc
, ce
->ce_fp
) < cc
) {
2286 advise ("open7Bit", "fwrite");
2288 if (ferror (ce
->ce_fp
)) {
2289 content_error (ce
->ce_file
, ct
, "error writing to");
2294 fseek (ct
->c_fp
, 0L, SEEK_SET
);
2296 if (fflush (ce
->ce_fp
)) {
2297 content_error (ce
->ce_file
, ct
, "error writing to");
2301 fseek (ce
->ce_fp
, 0L, SEEK_SET
);
2304 *file
= ce
->ce_file
;
2309 return fileno (ce
->ce_fp
);
2312 free_encoding (ct
, 0);
2326 openExternal (CT ct
, CT cb
, CE ce
, char **file
, int *fd
)
2328 char cachefile
[BUFSIZ
];
2331 fseek (ce
->ce_fp
, 0L, SEEK_SET
);
2336 if ((ce
->ce_fp
= fopen (ce
->ce_file
, "r")) == NULL
) {
2337 content_error (ce
->ce_file
, ct
, "unable to fopen for reading");
2343 if (find_cache(ct
, rcachesw
, NULL
, cb
->c_id
,
2344 cachefile
, sizeof(cachefile
)) != NOTOK
) {
2345 if ((ce
->ce_fp
= fopen (cachefile
, "r"))) {
2346 ce
->ce_file
= mh_xstrdup(cachefile
);
2350 admonish (cachefile
, "unable to fopen for reading");
2353 *fd
= ce
->ce_fp
? fileno (ce
->ce_fp
) : -1;
2357 *file
= ce
->ce_file
;
2358 *fd
= fileno (ce
->ce_fp
);
2369 return init_encoding (ct
, openFile
);
2374 openFile (CT ct
, char **file
)
2377 char cachefile
[BUFSIZ
];
2378 struct exbody
*e
= ct
->c_ctexbody
;
2379 CE ce
= &ct
->c_cefile
;
2381 switch (openExternal (e
->eb_parent
, e
->eb_content
, ce
, file
, &fd
)) {
2393 content_error (NULL
, ct
, "missing name parameter");
2397 ce
->ce_file
= mh_xstrdup(e
->eb_name
);
2400 if ((ce
->ce_fp
= fopen (ce
->ce_file
, "r")) == NULL
) {
2401 content_error (ce
->ce_file
, ct
, "unable to fopen for reading");
2405 if ((!e
->eb_permission
|| strcasecmp (e
->eb_permission
, "read-write"))
2406 && find_cache (NULL
, wcachesw
, &cachetype
, e
->eb_content
->c_id
,
2407 cachefile
, sizeof(cachefile
)) != NOTOK
) {
2411 mask
= umask (cachetype
? ~m_gmprot () : 0222);
2412 if ((fp
= fopen (cachefile
, "w"))) {
2414 char buffer
[BUFSIZ
];
2415 FILE *gp
= ce
->ce_fp
;
2417 fseek (gp
, 0L, SEEK_SET
);
2419 while ((cc
= fread (buffer
, sizeof(*buffer
), sizeof(buffer
), gp
))
2421 if ((int) fwrite (buffer
, sizeof(*buffer
), cc
, fp
) < cc
) {
2422 advise ("openFile", "fwrite");
2427 admonish (ce
->ce_file
, "error reading");
2428 (void) m_unlink (cachefile
);
2429 } else if (ferror (fp
)) {
2430 admonish (cachefile
, "error writing");
2431 (void) m_unlink (cachefile
);
2438 fseek (ce
->ce_fp
, 0L, SEEK_SET
);
2439 *file
= ce
->ce_file
;
2440 return fileno (ce
->ce_fp
);
2450 return init_encoding (ct
, openFTP
);
2455 openFTP (CT ct
, char **file
)
2461 char *bp
, *ftp
, *user
, *pass
;
2462 char buffer
[BUFSIZ
], cachefile
[BUFSIZ
];
2464 CE ce
= &ct
->c_cefile
;
2465 static char *username
= NULL
;
2466 static char *password
= NULL
;
2470 if ((ftp
= context_find (nmhaccessftp
)) && !*ftp
)
2476 switch (openExternal (e
->eb_parent
, e
->eb_content
, ce
, file
, &fd
)) {
2487 if (!e
->eb_name
|| !e
->eb_site
) {
2488 content_error (NULL
, ct
, "missing %s parameter",
2489 e
->eb_name
? "site": "name");
2493 /* Get the buffer ready to go */
2495 buflen
= sizeof(buffer
);
2498 * Construct the query message for user
2500 snprintf (bp
, buflen
, "Retrieve %s", e
->eb_name
);
2506 snprintf (bp
, buflen
, " (content %s)", e
->eb_partno
);
2512 snprintf (bp
, buflen
, "\n using %sFTP from site %s",
2513 e
->eb_flags
? "anonymous " : "", e
->eb_site
);
2518 if (e
->eb_size
> 0) {
2519 snprintf (bp
, buflen
, " (%lu octets)", e
->eb_size
);
2524 snprintf (bp
, buflen
, "? ");
2527 * Now, check the answer
2529 if (!read_yes_or_no_if_tty (buffer
))
2534 snprintf (buffer
, sizeof(buffer
), "%s@%s", getusername (),
2538 ruserpass (e
->eb_site
, &username
, &password
, 0);
2543 ce
->ce_unlink
= (*file
== NULL
);
2545 cachefile
[0] = '\0';
2546 if ((!e
->eb_permission
|| strcasecmp (e
->eb_permission
, "read-write"))
2547 && find_cache (NULL
, wcachesw
, &cachetype
, e
->eb_content
->c_id
,
2548 cachefile
, sizeof(cachefile
)) != NOTOK
) {
2549 if (*file
== NULL
) {
2556 ce
->ce_file
= mh_xstrdup(*file
);
2558 ce
->ce_file
= mh_xstrdup(cachefile
);
2561 if ((tempfile
= m_mktemp2(NULL
, invo_name
, NULL
, NULL
)) == NULL
) {
2562 die("unable to create temporary file in %s",
2565 ce
->ce_file
= mh_xstrdup(tempfile
);
2568 if ((ce
->ce_fp
= fopen (ce
->ce_file
, "w+")) == NULL
) {
2569 content_error (ce
->ce_file
, ct
, "unable to fopen for reading/writing");
2578 vec
[vecp
++] = r1bindex (ftp
, '/');
2579 vec
[vecp
++] = e
->eb_site
;
2582 vec
[vecp
++] = e
->eb_dir
;
2583 vec
[vecp
++] = e
->eb_name
;
2584 vec
[vecp
++] = ce
->ce_file
,
2585 vec
[vecp
++] = e
->eb_mode
&& !strcasecmp (e
->eb_mode
, "ascii")
2586 ? "ascii" : "binary";
2594 adios ("fork", "unable to");
2598 close (fileno (ce
->ce_fp
));
2600 fprintf (stderr
, "unable to exec ");
2606 if (pidXwait (child_id
, NULL
)) {
2607 username
= password
= NULL
;
2617 chmod (cachefile
, cachetype
? m_gmprot () : 0444);
2622 mask
= umask (cachetype
? ~m_gmprot () : 0222);
2623 if ((fp
= fopen (cachefile
, "w"))) {
2625 FILE *gp
= ce
->ce_fp
;
2627 fseek (gp
, 0L, SEEK_SET
);
2629 while ((cc
= fread (buffer
, sizeof(*buffer
), sizeof(buffer
), gp
))
2631 if ((int) fwrite (buffer
, sizeof(*buffer
), cc
, fp
) < cc
) {
2632 advise ("openFTP", "fwrite");
2637 admonish (ce
->ce_file
, "error reading");
2638 (void) m_unlink (cachefile
);
2639 } else if (ferror (fp
)) {
2640 admonish (cachefile
, "error writing");
2641 (void) m_unlink (cachefile
);
2649 fseek (ce
->ce_fp
, 0L, SEEK_SET
);
2650 *file
= ce
->ce_file
;
2651 return fileno (ce
->ce_fp
);
2662 return init_encoding (ct
, openMail
);
2667 openMail (CT ct
, char **file
)
2669 int child_id
, fd
, vecp
;
2671 char *bp
, buffer
[BUFSIZ
], *vec
[7];
2672 struct exbody
*e
= ct
->c_ctexbody
;
2673 CE ce
= &ct
->c_cefile
;
2675 switch (openExternal (e
->eb_parent
, e
->eb_content
, ce
, file
, &fd
)) {
2686 if (!e
->eb_server
) {
2687 content_error (NULL
, ct
, "missing server parameter");
2691 /* Get buffer ready to go */
2693 buflen
= sizeof(buffer
);
2695 /* Now, construct query message */
2696 snprintf (bp
, buflen
, "Retrieve content");
2702 snprintf (bp
, buflen
, " %s", e
->eb_partno
);
2708 snprintf (bp
, buflen
, " by asking %s\n\n%s\n? ",
2710 e
->eb_subject
? e
->eb_subject
: e
->eb_body
);
2712 /* Now, check answer */
2713 if (!read_yes_or_no_if_tty (buffer
))
2717 vec
[vecp
++] = r1bindex (mailproc
, '/');
2718 vec
[vecp
++] = e
->eb_server
;
2719 vec
[vecp
++] = "-subject";
2720 vec
[vecp
++] = e
->eb_subject
? e
->eb_subject
: "mail-server request";
2721 vec
[vecp
++] = "-body";
2722 vec
[vecp
++] = e
->eb_body
;
2728 advise ("fork", "unable to");
2732 execvp (mailproc
, vec
);
2733 fprintf (stderr
, "unable to exec ");
2739 if (pidXwait (child_id
, NULL
) == OK
)
2740 inform("request sent");
2744 if (*file
== NULL
) {
2746 if ((tempfile
= m_mktemp2(NULL
, invo_name
, NULL
, NULL
)) == NULL
) {
2747 die("unable to create temporary file in %s",
2750 ce
->ce_file
= mh_xstrdup(tempfile
);
2753 ce
->ce_file
= mh_xstrdup(*file
);
2757 if ((ce
->ce_fp
= fopen (ce
->ce_file
, "w+")) == NULL
) {
2758 content_error (ce
->ce_file
, ct
, "unable to fopen for reading/writing");
2762 /* showproc is for mhshow and mhstore, though mhlist -debug
2763 * prints it, too. */
2764 free(ct
->c_showproc
);
2765 ct
->c_showproc
= mh_xstrdup("true");
2767 fseek (ce
->ce_fp
, 0L, SEEK_SET
);
2768 *file
= ce
->ce_file
;
2769 return fileno (ce
->ce_fp
);
2780 return init_encoding (ct
, openURL
);
2785 openURL (CT ct
, char **file
)
2787 struct exbody
*e
= ct
->c_ctexbody
;
2788 CE ce
= &ct
->c_cefile
;
2789 char *urlprog
, *program
;
2790 char buffer
[BUFSIZ
], cachefile
[BUFSIZ
];
2794 struct msgs_array args
= { 0, 0, NULL
};
2797 if ((urlprog
= context_find(nmhaccessurl
)) && *urlprog
== '\0')
2801 content_error(NULL
, ct
, "No entry for nmh-access-url in profile");
2805 switch (openExternal(e
->eb_parent
, e
->eb_content
, ce
, file
, &fd
)) {
2817 content_error(NULL
, ct
, "missing url parameter");
2821 ce
->ce_unlink
= (*file
== NULL
);
2823 cachefile
[0] = '\0';
2825 if (find_cache(NULL
, wcachesw
, &cachetype
, e
->eb_content
->c_id
,
2826 cachefile
, sizeof(cachefile
)) != NOTOK
) {
2827 if (*file
== NULL
) {
2834 ce
->ce_file
= mh_xstrdup(*file
);
2836 ce
->ce_file
= mh_xstrdup(cachefile
);
2839 if ((tempfile
= m_mktemp2(NULL
, invo_name
, NULL
, NULL
)) == NULL
) {
2840 die("unable to create temporary file in %s",
2843 ce
->ce_file
= mh_xstrdup(tempfile
);
2846 if ((ce
->ce_fp
= fopen(ce
->ce_file
, "w+")) == NULL
) {
2847 content_error(ce
->ce_file
, ct
, "unable to fopen for read/writing");
2851 switch (child_id
= fork()) {
2853 adios ("fork", "unable to");
2857 argsplit_msgarg(&args
, urlprog
, &program
);
2858 app_msgarg(&args
, e
->eb_url
);
2859 app_msgarg(&args
, NULL
);
2860 dup2(fileno(ce
->ce_fp
), 1);
2861 close(fileno(ce
->ce_fp
));
2862 execvp(program
, args
.msgs
);
2863 fprintf(stderr
, "Unable to exec ");
2869 if (pidXwait(child_id
, NULL
)) {
2877 chmod(cachefile
, cachetype
? m_gmprot() : 0444);
2882 mask
= umask (cachetype
? ~m_gmprot() : 0222);
2883 if ((fp
= fopen(cachefile
, "w"))) {
2885 FILE *gp
= ce
->ce_fp
;
2887 fseeko(gp
, 0, SEEK_SET
);
2889 while ((cc
= fread(buffer
, sizeof(*buffer
),
2890 sizeof(buffer
), gp
)) > 0)
2891 if ((int) fwrite(buffer
, sizeof(*buffer
), cc
, fp
) < cc
) {
2892 advise ("openURL", "fwrite");
2898 admonish(ce
->ce_file
, "error reading");
2899 (void) m_unlink (cachefile
);
2906 fseeko(ce
->ce_fp
, 0, SEEK_SET
);
2907 *file
= ce
->ce_file
;
2908 return fileno(ce
->ce_fp
);
2913 * Stores MD5 digest (in cp, from Content-MD5 header) in ct->c_digest. It
2914 * has to be base64 decoded.
2917 readDigest (CT ct
, char *cp
)
2919 unsigned char *digest
;
2922 if (decodeBase64 (cp
, &digest
, &len
, 0, NULL
) == OK
) {
2923 const size_t maxlen
= sizeof ct
->c_digest
;
2925 if (strlen ((char *) digest
) <= maxlen
) {
2926 memcpy (ct
->c_digest
, digest
, maxlen
);
2931 fprintf (stderr
, "MD5 digest=");
2932 for (i
= 0; i
< maxlen
; ++i
) {
2933 fprintf (stderr
, "%02x", ct
->c_digest
[i
] & 0xff);
2935 fprintf (stderr
, "\n");
2941 fprintf (stderr
, "invalid MD5 digest (got %d octets)\n",
2942 (int) strlen ((char *) digest
));
2952 /* Multipart parts might have content before the first subpart and/or
2953 after the last subpart that hasn't been stored anywhere else, so do
2956 get_leftover_mp_content (CT ct
, int before
/* or after */)
2958 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
2960 bool found_boundary
= false;
2966 char *content
= NULL
;
2968 if (! m
) return NOTOK
;
2971 if (! m
->mp_parts
|| ! m
->mp_parts
->mp_part
) return NOTOK
;
2973 /* Isolate the beginning of this part to the beginning of the
2974 first subpart and save any content between them. */
2975 fseeko (ct
->c_fp
, ct
->c_begin
, SEEK_SET
);
2976 max
= m
->mp_parts
->mp_part
->c_begin
- ct
->c_begin
;
2977 boundary
= concat ("--", m
->mp_start
, NULL
);
2979 struct part
*last_subpart
= NULL
;
2980 struct part
*subpart
;
2982 /* Go to the last subpart to get its end position. */
2983 for (subpart
= m
->mp_parts
; subpart
; subpart
= subpart
->mp_next
) {
2984 last_subpart
= subpart
;
2987 if (last_subpart
== NULL
) return NOTOK
;
2989 /* Isolate the end of the last subpart to the end of this part
2990 and save any content between them. */
2991 fseeko (ct
->c_fp
, last_subpart
->mp_part
->c_end
, SEEK_SET
);
2992 max
= ct
->c_end
- last_subpart
->mp_part
->c_end
;
2993 boundary
= concat ("--", m
->mp_stop
, NULL
);
2996 /* Back up by 1 to pick up the newline. */
2997 while ((gotlen
= getline(&bufp
, &buflen
, ct
->c_fp
)) != -1) {
2999 /* Don't look beyond beginning of first subpart (before) or
3000 next part (after). */
3001 if (read
> max
) bufp
[read
-max
] = '\0';
3004 if (! strcmp (bufp
, boundary
)) {
3005 found_boundary
= true;
3008 if (! found_boundary
&& ! strcmp (bufp
, boundary
)) {
3009 found_boundary
= true;
3014 if ((before
&& ! found_boundary
) || (! before
&& found_boundary
)) {
3016 char *old_content
= content
;
3017 content
= concat (content
, bufp
, NULL
);
3021 ? concat ("\n", bufp
, NULL
)
3022 : concat (bufp
, NULL
);
3027 if (found_boundary
|| read
> max
) break;
3029 if (read
> max
) break;
3033 /* Skip the newline if that's all there is. */
3037 /* Remove trailing newline, except at EOF. */
3038 if ((before
|| ! feof (ct
->c_fp
)) &&
3039 (cp
= content
+ strlen (content
)) > content
&&
3044 if (strlen (content
) > 1) {
3046 m
->mp_content_before
= content
;
3048 m
->mp_content_after
= content
;
3063 ct_type_str (int type
)
3066 case CT_APPLICATION
:
3067 return "application";
3083 return "unknown_type";
3089 ct_subtype_str (int type
, int subtype
)
3092 case CT_APPLICATION
:
3094 case APPLICATION_OCTETS
:
3096 case APPLICATION_POSTSCRIPT
:
3097 return "postscript";
3099 return "unknown_app_subtype";
3103 case MESSAGE_RFC822
:
3105 case MESSAGE_PARTIAL
:
3107 case MESSAGE_EXTERNAL
:
3110 return "unknown_msg_subtype";
3116 case MULTI_ALTERNATE
:
3117 return "alternative";
3120 case MULTI_PARALLEL
:
3125 return "unknown_multipart_subtype";
3136 return "unknown_text_subtype";
3139 return "unknown_type";
3145 ct_str_type (const char *type
)
3147 struct str2init
*s2i
;
3149 for (s2i
= str2cts
; s2i
->si_key
; ++s2i
) {
3150 if (! strcasecmp (type
, s2i
->si_key
)) {
3154 if (! s2i
->si_key
&& ! uprf (type
, "X-")) {
3163 ct_str_subtype (int type
, const char *subtype
)
3168 case CT_APPLICATION
:
3169 for (kv
= SubApplication
; kv
->kv_key
; ++kv
) {
3170 if (! strcasecmp (subtype
, kv
->kv_key
)) {
3174 return kv
->kv_value
;
3176 for (kv
= SubMessage
; kv
->kv_key
; ++kv
) {
3177 if (! strcasecmp (subtype
, kv
->kv_key
)) {
3181 return kv
->kv_value
;
3183 for (kv
= SubMultiPart
; kv
->kv_key
; ++kv
) {
3184 if (! strcasecmp (subtype
, kv
->kv_key
)) {
3188 return kv
->kv_value
;
3190 for (kv
= SubText
; kv
->kv_key
; ++kv
) {
3191 if (! strcasecmp (subtype
, kv
->kv_key
)) {
3195 return kv
->kv_value
;
3202 /* Find the content type and InitFunc for the CT. */
3203 const struct str2init
*
3204 get_ct_init (int type
)
3206 const struct str2init
*sp
;
3208 for (sp
= str2cts
; sp
->si_key
; ++sp
) {
3209 if (type
== sp
->si_val
) {
3218 ce_str (int encoding
)
3224 return "quoted-printable";
3240 /* Find the content type and InitFunc for the content encoding method. */
3241 const struct str2init
*
3242 get_ce_method (const char *method
)
3244 struct str2init
*sp
;
3246 for (sp
= str2ces
; sp
->si_key
; ++sp
) {
3247 if (! strcasecmp (method
, sp
->si_key
)) {
3256 * Parse a series of MIME attributes (or parameters) given a header as
3259 * Arguments include:
3261 * filename - Name of input file (for error messages)
3262 * fieldname - Name of field being processed
3263 * headerp - Pointer to pointer of the beginning of the MIME attributes.
3264 * Updated to point to end of attributes when finished.
3265 * param_head - Pointer to head of parameter list
3266 * param_tail - Pointer to tail of parameter list
3267 * commentp - Pointer to header comment pointer (may be NULL)
3269 * Returns OK if parsing was successful, NOTOK if parsing failed, and
3270 * DONE to indicate a benign error (minor parsing error, but the program
3275 parse_header_attrs (const char *filename
, const char *fieldname
,
3276 char **header_attrp
, PM
*param_head
, PM
*param_tail
,
3279 char *cp
= *header_attrp
;
3285 struct sectlist
*next
;
3291 struct sectlist
*sechead
;
3292 struct parmlist
*next
;
3293 } *pp
, *pp2
, *phead
= NULL
;
3295 while (*cp
== ';') {
3296 char *dp
, *vp
, *up
, *nameptr
, *valptr
, *charset
= NULL
, *lang
= NULL
;
3297 bool encoded
= false;
3298 bool partial
= false;
3299 int len
= 0, index
= 0;
3302 while (isspace ((unsigned char) *cp
))
3306 get_comment (filename
, fieldname
, &cp
, commentp
) == NOTOK
) {
3311 if (! suppress_extraneous_trailing_semicolon_warning
) {
3312 inform("extraneous trailing ';' in message %s's %s: "
3313 "parameter list", filename
, fieldname
);
3318 /* down case the attribute name */
3319 for (dp
= cp
; istoken ((unsigned char) *dp
); dp
++)
3320 *dp
= tolower ((unsigned char) *dp
);
3322 for (up
= dp
; isspace ((unsigned char) *dp
);)
3324 if (dp
== cp
|| *dp
!= '=') {
3325 inform("invalid parameter in message %s's %s: field\n"
3326 " parameter %s (error detected at offset %ld)",
3327 filename
, fieldname
, cp
, (long)(dp
- cp
));
3332 * To handle RFC 2231, we have to deal with the following extensions:
3334 * name*=encoded-value
3335 * name*<N>=part-N-of-a-parameter-value
3336 * name*<N>*=encoded-part-N-of-a-parameter-value
3339 * If there's a * right before the equal sign, it's encoded.
3340 * If there's a * and one or more digits, then it's section N.
3342 * Remember we can have one or the other, or both. cp points to
3343 * beginning of name, up points past the last character in the
3347 for (vp
= cp
; vp
< up
; vp
++) {
3348 if (*vp
== '*' && vp
< up
- 1) {
3352 if (*vp
== '*' && vp
== up
- 1) {
3354 } else if (partial
) {
3355 if (isdigit((unsigned char) *vp
))
3356 index
= *vp
- '0' + index
* 10;
3358 inform("invalid parameter index in message %s's %s: field"
3359 "\n (parameter %s)", filename
, fieldname
, cp
);
3368 * Break out the parameter name and value sections and allocate
3372 nameptr
= mh_xmalloc(len
+ 1);
3373 strncpy(nameptr
, cp
, len
);
3374 nameptr
[len
] = '\0';
3376 for (dp
++; isspace ((unsigned char) *dp
);)
3381 * Single quotes delimit the character set and language tag.
3382 * They are required on the first section (or a complete
3387 while (*vp
!= '\'' && !isspace((unsigned char) *vp
) &&
3393 charset
= mh_xmalloc(len
+ 1);
3394 strncpy(charset
, dp
, len
);
3395 charset
[len
] = '\0';
3401 inform("missing charset in message %s's %s: field\n"
3402 " (parameter %s)", filename
, fieldname
, nameptr
);
3408 while (*vp
!= '\'' && !isspace((unsigned char) *vp
) &&
3415 lang
= mh_xmalloc(len
+ 1);
3416 strncpy(lang
, dp
, len
);
3423 inform("missing language tag in message %s's %s: field\n"
3424 " (parameter %s)", filename
, fieldname
, nameptr
);
3434 * At this point vp should be pointing at the beginning
3435 * of the encoded value/section. Continue until we reach
3436 * the end or get whitespace. But first, calculate the
3437 * length so we can allocate the correct buffer size.
3440 for (vp
= dp
, len
= 0; istoken(*vp
); vp
++) {
3442 if (*(vp
+ 1) == '\0' ||
3443 !isxdigit((unsigned char) *(vp
+ 1)) ||
3444 *(vp
+ 2) == '\0' ||
3445 !isxdigit((unsigned char) *(vp
+ 2))) {
3446 inform("invalid encoded sequence in message %s's %s: field\n"
3447 " (parameter %s)", filename
, fieldname
, nameptr
);
3458 up
= valptr
= mh_xmalloc(len
+ 1);
3460 for (vp
= dp
; istoken(*vp
); vp
++) {
3462 *up
++ = decode_qp(*(vp
+ 1), *(vp
+ 2));
3473 * A "normal" string. If it's got a leading quote, then we
3474 * strip the quotes out. Otherwise go until we reach the end
3475 * or get whitespace. Note we scan it twice; once to get the
3476 * length, then the second time copies it into the destination
3483 for (cp
= dp
+ 1;;) {
3487 inform("invalid quoted-string in message %s's %s: field\n"
3488 " (parameter %s)", filename
, fieldname
, nameptr
);
3508 for (cp
= dp
; istoken (*cp
); cp
++) {
3513 valptr
= mh_xmalloc(len
+ 1);
3517 for (cp
= dp
+ 1, vp
= valptr
, i
= 0; i
< len
; i
++) {
3525 strncpy(valptr
, cp
= dp
, len
);
3533 * If 'partial' is set, we don't allocate a parameter now. We
3534 * put it on the parameter linked list to be reassembled later.
3536 * "phead" points to a list of all parameters we need to reassemble.
3537 * Each parameter has a list of sections. We insert the sections in
3542 for (pp
= phead
; pp
!= NULL
; pp
= pp
->next
) {
3543 if (strcasecmp(nameptr
, pp
->name
) == 0) {
3558 * Insert this into the section linked list
3566 if (pp
->sechead
== NULL
|| pp
->sechead
->index
> index
) {
3567 sp
->next
= pp
->sechead
;
3570 for (sp2
= pp
->sechead
; sp2
!= NULL
; sp2
= sp2
->next
) {
3571 if (sp2
->index
== sp
->index
) {
3572 inform("duplicate index (%d) in message %s's %s: field"
3573 "\n (parameter %s)", sp
->index
, filename
,
3574 fieldname
, nameptr
);
3577 if (sp2
->index
< sp
->index
&&
3578 (sp2
->next
== NULL
|| sp2
->next
->index
> sp
->index
)) {
3579 sp
->next
= sp2
->next
;
3586 inform("Internal error: cannot insert partial param "
3587 "in message %s's %s: field\n (parameter %s)",
3588 filename
, fieldname
, nameptr
);
3594 * Save our charset and lang tags.
3597 if (index
== 0 && encoded
) {
3599 pp
->charset
= charset
;
3604 pm
= add_param(param_head
, param_tail
, nameptr
, valptr
, 1);
3605 pm
->pm_charset
= charset
;
3609 while (isspace ((unsigned char) *cp
))
3613 get_comment (filename
, fieldname
, &cp
, commentp
) == NOTOK
) {
3619 * Now that we're done, reassemble all of the partial parameters.
3622 for (pp
= phead
; pp
!= NULL
; ) {
3626 for (sp
= pp
->sechead
; sp
!= NULL
; sp
= sp
->next
) {
3627 if (sp
->index
!= pindex
++) {
3628 inform("missing section %d for parameter in message "
3629 "%s's %s: field\n (parameter %s)", pindex
- 1,
3630 filename
, fieldname
, pp
->name
);
3636 p
= q
= mh_xmalloc(tlen
+ 1);
3637 for (sp
= pp
->sechead
; sp
!= NULL
; ) {
3638 memcpy(q
, sp
->value
, sp
->len
);
3648 pm
= add_param(param_head
, param_tail
, pp
->name
, p
, 1);
3649 pm
->pm_charset
= pp
->charset
;
3650 pm
->pm_lang
= pp
->lang
;
3661 * Return the charset for a particular content type.
3665 content_charset (CT ct
)
3667 char *ret_charset
= NULL
;
3669 ret_charset
= get_param(ct
->c_ctinfo
.ci_first_pm
, "charset", '?', 0);
3671 return ret_charset
? ret_charset
: mh_xstrdup("US-ASCII");
3676 * Create a string based on a list of output parameters. Assume that this
3677 * parameter string will be appended to an existing header, so start out
3678 * with the separator (;). Perform RFC 2231 encoding when necessary.
3682 output_params(size_t initialwidth
, PM params
, int *offsetout
, int external
)
3684 char *paramout
= NULL
;
3685 char line
[CPERLIN
* 2], *q
;
3686 int curlen
, index
, cont
, encode
, i
;
3687 size_t valoff
, numchars
;
3689 while (params
!= NULL
) {
3695 if (external
&& strcasecmp(params
->pm_name
, "body") == 0)
3698 if (strlen(params
->pm_name
) > CPERLIN
) {
3699 inform("Parameter name \"%s\" is too long", params
->pm_name
);
3704 curlen
= param_len(params
, index
, valoff
, &encode
, &cont
, &numchars
);
3707 * Loop until we get a parameter that fits within a line. We
3708 * assume new lines start with a tab, so check our overflow based
3718 * At this point we're definitely continuing the line, so
3719 * be sure to include the parameter name and section index.
3722 q
+= snprintf(q
, sizeof(line
) - (q
- line
), "%s*%d",
3723 params
->pm_name
, index
);
3726 * Both of these functions do a NUL termination
3730 i
= encode_param(params
, q
, sizeof(line
) - (q
- line
),
3731 numchars
, valoff
, index
);
3733 i
= normal_param(params
, q
, sizeof(line
) - (q
- line
),
3743 curlen
= param_len(params
, index
, valoff
, &encode
, &cont
,
3748 * "line" starts with a ;\n\t, so that doesn't count against
3749 * the length. But add 8 since it starts with a tab; that's
3750 * how we end up with 5.
3753 initialwidth
= strlen(line
) + 5;
3756 * At this point the line should be built, so add it to our
3757 * current output buffer.
3760 paramout
= add(line
, paramout
);
3764 * If this won't fit on the line, start a new one. Save room in
3765 * case we need a semicolon on the end
3768 if (initialwidth
+ curlen
> CPERLIN
- 1) {
3780 * At this point, we're either finishing a continued parameter, or
3781 * we're working on a new one.
3785 q
+= snprintf(q
, sizeof(line
) - (q
- line
), "%s*%d",
3786 params
->pm_name
, index
);
3788 strncpy(q
, params
->pm_name
, sizeof(line
) - (q
- line
));
3793 i
= encode_param(params
, q
, sizeof(line
) - (q
- line
),
3794 strlen(params
->pm_value
+ valoff
), valoff
, index
);
3796 i
= normal_param(params
, q
, sizeof(line
) - (q
- line
),
3797 strlen(params
->pm_value
+ valoff
), valoff
);
3804 paramout
= add(line
, paramout
);
3805 initialwidth
+= strlen(line
);
3807 params
= params
->pm_next
;
3811 *offsetout
= initialwidth
;
3817 * Calculate the size of a parameter.
3821 * pm - The parameter being output
3822 * index - If continuing the parameter, the index of the section
3824 * valueoff - The current offset into the parameter value that we're
3825 * working on (previous sections have consumed valueoff bytes).
3826 * encode - Set if we should perform encoding on this parameter section
3827 * (given that we're consuming bytesfit bytes).
3828 * cont - Set if the remaining data in value will not fit on a single
3829 * line and will need to be continued.
3830 * bytesfit - The number of bytes that we can consume from the parameter
3831 * value and still fit on a completely new line. The
3832 * calculation assumes the new line starts with a tab,
3833 * includes the parameter name and any encoding, and fits
3834 * within CPERLIN bytes. Will always be at least 1.
3838 param_len(PM pm
, int index
, size_t valueoff
, int *encode
, int *cont
,
3841 char *start
= pm
->pm_value
+ valueoff
, *p
, indexchar
[32];
3842 size_t len
= 0, fit
= 0;
3843 int fitlimit
= 0, eightbit
, maxfit
;
3848 * Add up the length. First, start with the parameter name.
3851 len
= strlen(pm
->pm_name
);
3854 * Scan the parameter value and see if we need to do encoding for this
3858 eightbit
= contains8bit(start
, NULL
);
3861 * Determine if we need to encode this section. Encoding is necessary if:
3863 * - There are any 8-bit characters at all and we're on the first
3865 * - There are 8-bit characters within N bytes of our section start.
3866 * N is calculated based on the number of bytes it would take to
3867 * reach CPERLIN. Specifically:
3868 * 8 (starting tab) +
3869 * strlen(param name) +
3870 * 4 ('* for section marker, '=', opening/closing '"')
3872 * is the number of bytes used by everything that isn't part of the
3873 * value. So that gets subtracted from CPERLIN.
3876 snprintf(indexchar
, sizeof(indexchar
), "%d", index
);
3877 maxfit
= CPERLIN
- (12 + len
+ strlen(indexchar
));
3878 if ((eightbit
&& index
== 0) || contains8bit(start
, start
+ maxfit
)) {
3882 len
++; /* Add in equal sign */
3886 * We're using maxfit as a marker for how many characters we can
3887 * fit into the line. Bump it by two because we're not using quotes
3894 * If we don't have a charset or language tag in this parameter,
3898 if (! pm
->pm_charset
) {
3899 pm
->pm_charset
= mh_xstrdup(write_charset_8bit());
3900 if (strcasecmp(pm
->pm_charset
, "US-ASCII") == 0)
3901 die("8-bit characters in parameter \"%s\", but "
3902 "local character set is US-ASCII", pm
->pm_name
);
3905 pm
->pm_lang
= mh_xstrdup(""); /* Default to a blank lang tag */
3907 len
++; /* For the encoding marker */
3910 int enclen
= strlen(pm
->pm_charset
) + strlen(pm
->pm_lang
) + 2;
3915 * We know we definitely need to include an index. maxfit already
3916 * includes the section marker.
3918 len
+= strlen(indexchar
);
3920 for (p
= start
; *p
!= '\0'; p
++) {
3921 if (isparamencode(*p
)) {
3929 * Just so there's no confusion: maxfit is counting OUTPUT
3930 * characters (post-encoding). fit is counting INPUT characters.
3932 if (! fitlimit
&& maxfit
>= 0)
3934 else if (! fitlimit
)
3939 * Calculate the string length, but add room for quoting \
3940 * and " if necessary. Also account for quotes at beginning
3943 for (p
= start
; *p
!= '\0'; p
++) {
3954 if (! fitlimit
&& maxfit
>= 0)
3956 else if (! fitlimit
)
3973 * Output an encoded parameter string.
3977 encode_param(PM pm
, char *output
, size_t len
, size_t valuelen
,
3978 size_t valueoff
, int index
)
3980 size_t outlen
= 0, n
;
3981 char *endptr
= output
+ len
, *p
;
3984 * First, output the marker for an encoded string.
3992 * If the index is 0, output the character set and language tag.
3993 * If theses were NULL, they should have already been filled in
3998 n
= snprintf(output
, len
- outlen
, "%s'%s'", pm
->pm_charset
,
4002 if (output
> endptr
) {
4003 inform("Internal error: parameter buffer overflow");
4009 * Copy over the value, encoding if necessary
4012 p
= pm
->pm_value
+ valueoff
;
4013 while (valuelen
-- > 0) {
4014 if (isparamencode(*p
)) {
4015 n
= snprintf(output
, len
- outlen
, "%%%02X", (unsigned char) *p
++);
4022 if (output
> endptr
) {
4023 inform("Internal error: parameter buffer overflow");
4034 * Output a "normal" parameter, without encoding. Be sure to escape
4035 * quotes and backslashes if necessary.
4039 normal_param(PM pm
, char *output
, size_t len
, size_t valuelen
,
4043 char *endptr
= output
+ len
, *p
;
4049 p
= pm
->pm_value
+ valueoff
;
4051 while (valuelen
-- > 0) {
4062 if (output
> endptr
) {
4063 inform("Internal error: parameter buffer overflow");
4068 if (output
- 2 > endptr
) {
4069 inform("Internal error: parameter buffer overflow");
4080 * Add a parameter to the parameter linked list
4084 add_param(PM
*first
, PM
*last
, char *name
, char *value
, int nocopy
)
4089 pm
->pm_name
= nocopy
? name
: getcpy(name
);
4090 pm
->pm_value
= nocopy
? value
: getcpy(value
);
4093 (*last
)->pm_next
= pm
;
4104 * Either replace a current parameter with a new value, or add the parameter
4105 * to the parameter linked list.
4109 replace_param(PM
*first
, PM
*last
, char *name
, char *value
, int nocopy
)
4113 for (pm
= *first
; pm
!= NULL
; pm
= pm
->pm_next
) {
4114 if (strcasecmp(name
, pm
->pm_name
) == 0) {
4116 * If nocopy is set, it's assumed that we own both name
4117 * and value. We don't need name, so we discard it now.
4122 pm
->pm_value
= nocopy
? value
: getcpy(value
);
4127 return add_param(first
, last
, name
, value
, nocopy
);
4131 * Retrieve a parameter value from a parameter linked list. If the parameter
4132 * value needs converted to the local character set, do that now.
4136 get_param(PM first
, const char *name
, char replace
, int fetchonly
)
4138 while (first
!= NULL
) {
4139 if (strcasecmp(name
, first
->pm_name
) == 0) {
4141 return first
->pm_value
;
4142 return getcpy(get_param_value(first
, replace
));
4144 first
= first
->pm_next
;
4151 * Return a parameter value, converting to the local character set if
4156 get_param_value(PM pm
, char replace
)
4158 static char buffer
[4096]; /* I hope no parameters are larger */
4159 size_t bufsize
= sizeof(buffer
);
4164 ICONV_CONST
char *p
;
4165 #else /* HAVE_ICONV */
4167 #endif /* HAVE_ICONV */
4172 * If we don't have a character set indicated, it's assumed to be
4173 * US-ASCII. If it matches our character set, we don't need to convert
4177 if (!pm
->pm_charset
|| check_charset(pm
->pm_charset
,
4178 strlen(pm
->pm_charset
))) {
4179 return pm
->pm_value
;
4183 * In this case, we need to convert. If we have iconv support, use
4184 * that. Otherwise, go through and simply replace every non-ASCII
4185 * character with the substitution character.
4190 bufsize
= sizeof(buffer
);
4191 utf8
= strcasecmp(pm
->pm_charset
, "UTF-8") == 0;
4193 cd
= iconv_open(get_charset(), pm
->pm_charset
);
4194 if (cd
== (iconv_t
) -1) {
4198 inbytes
= strlen(pm
->pm_value
);
4202 if (iconv(cd
, &p
, &inbytes
, &q
, &bufsize
) == (size_t)-1) {
4203 if (errno
!= EILSEQ
) {
4208 * Reset shift state, substitute our character,
4209 * try to restart conversion.
4212 iconv(cd
, NULL
, NULL
, &q
, &bufsize
);
4225 for (++p
, --inbytes
;
4226 inbytes
> 0 && (((unsigned char) *p
) & 0xc0) == 0x80;
4245 #endif /* HAVE_ICONV */
4248 * Take everything non-ASCII and substitute the replacement character
4252 bufsize
= sizeof(buffer
);
4253 for (p
= pm
->pm_value
; *p
!= '\0' && bufsize
> 1; p
++, q
++, bufsize
--) {
4254 if (isascii((unsigned char) *p
) && isprint((unsigned char) *p
))