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/concat.h"
10 #include "sbr/r1bindex.h"
11 #include "sbr/ruserpass.h"
12 #include "sbr/fmt_rfc2047.h"
14 #include "sbr/check_charset.h"
15 #include "sbr/getcpy.h"
16 #include "sbr/context_find.h"
17 #include "sbr/pidstatus.h"
18 #include "sbr/arglist.h"
19 #include "sbr/error.h"
25 #include "h/mhparse.h"
28 #include "h/mhcachesbr.h"
29 #include "sbr/m_mktemp.h"
33 #endif /* HAVE_ICONV */
34 #include "sbr/base64.h"
39 int checksw
= 0; /* check Content-MD5 field */
42 * These are for mhfixmsg to:
43 * 1) Instruct parser not to detect invalid Content-Transfer-Encoding
45 * 2) Suppress the warning about bogus multipart content, and report it.
46 * 3) Suppress the warning about extraneous trailing ';' in header parameter
49 bool skip_mp_cte_check
;
50 bool suppress_bogus_mp_content_warning
;
51 bool bogus_mp_content
;
52 bool suppress_extraneous_trailing_semicolon_warning
;
55 * By default, suppress warning about multiple MIME-Version header fields.
57 bool suppress_multiple_mime_version_warning
= true;
59 /* list of preferred type/subtype pairs, for -prefer */
60 mime_type_subtype mime_preference
[NPREFS
];
65 * Structures for TEXT messages
67 struct k2v SubText
[] = {
68 { "plain", TEXT_PLAIN
},
69 { "richtext", TEXT_RICHTEXT
}, /* defined in RFC 1341 */
70 { "enriched", TEXT_ENRICHED
}, /* defined in RFC 1896 */
71 { NULL
, TEXT_UNKNOWN
} /* this one must be last! */
74 /* Charset[] removed -- yozo. Mon Oct 8 01:03:41 JST 2012 */
77 * Structures for MULTIPART messages
79 struct k2v SubMultiPart
[] = {
80 { "mixed", MULTI_MIXED
},
81 { "alternative", MULTI_ALTERNATE
},
82 { "digest", MULTI_DIGEST
},
83 { "parallel", MULTI_PARALLEL
},
84 { "related", MULTI_RELATED
},
85 { NULL
, MULTI_UNKNOWN
} /* this one must be last! */
89 * Structures for MESSAGE messages
91 struct k2v SubMessage
[] = {
92 { "rfc822", MESSAGE_RFC822
},
93 { "partial", MESSAGE_PARTIAL
},
94 { "external-body", MESSAGE_EXTERNAL
},
95 { NULL
, MESSAGE_UNKNOWN
} /* this one must be last! */
99 * Structure for APPLICATION messages
101 struct k2v SubApplication
[] = {
102 { "octet-stream", APPLICATION_OCTETS
},
103 { "postscript", APPLICATION_POSTSCRIPT
},
104 { NULL
, APPLICATION_UNKNOWN
} /* this one must be last! */
108 * Mapping of names of CTE types in mhbuild directives
110 static struct k2v EncodingType
[] = {
113 { "q-p", CE_QUOTED
},
114 { "quoted-printable", CE_QUOTED
},
115 { "b64", CE_BASE64
},
116 { "base64", CE_BASE64
},
124 static CT
get_content (FILE *, char *, int);
125 static int get_comment (const char *, const char *, char **, char **);
127 static int InitGeneric (CT
);
128 static int InitText (CT
);
129 static int InitMultiPart (CT
);
130 static void reverse_parts (CT
);
131 static void prefer_parts(CT ct
);
132 static int InitMessage (CT
);
133 static int InitApplication (CT
);
134 static int init_encoding (CT
, OpenCEFunc
);
135 static unsigned long size_encoding (CT
);
136 static int InitBase64 (CT
);
137 static int openBase64 (CT
, char **);
138 static int InitQuoted (CT
);
139 static int openQuoted (CT
, char **);
140 static int Init7Bit (CT
);
141 static int openExternal (CT
, CT
, CE
, char **, int *);
142 static int InitFile (CT
);
143 static int openFile (CT
, char **);
144 static int InitFTP (CT
);
145 static int openFTP (CT
, char **);
146 static int InitMail (CT
);
147 static int openMail (CT
, char **);
148 static int readDigest (CT
, char *);
149 static int get_leftover_mp_content (CT
, int);
150 static int InitURL (CT
);
151 static int openURL (CT
, char **);
152 static int parse_header_attrs (const char *, const char *, char **, PM
*,
154 static size_t param_len(PM
, int, size_t, int *, int *, size_t *);
155 static size_t normal_param(PM
, char *, size_t, size_t, size_t);
156 static int get_dispo (char *, CT
, int);
158 struct str2init str2cts
[] = {
159 { "application", CT_APPLICATION
, InitApplication
},
160 { "audio", CT_AUDIO
, InitGeneric
},
161 { "image", CT_IMAGE
, InitGeneric
},
162 { "message", CT_MESSAGE
, InitMessage
},
163 { "multipart", CT_MULTIPART
, InitMultiPart
},
164 { "text", CT_TEXT
, InitText
},
165 { "video", CT_VIDEO
, InitGeneric
},
166 { NULL
, CT_EXTENSION
, NULL
}, /* these two must be last! */
167 { NULL
, CT_UNKNOWN
, NULL
},
170 struct str2init str2ces
[] = {
171 { "base64", CE_BASE64
, InitBase64
},
172 { "quoted-printable", CE_QUOTED
, InitQuoted
},
173 { "8bit", CE_8BIT
, Init7Bit
},
174 { "7bit", CE_7BIT
, Init7Bit
},
175 { "binary", CE_BINARY
, Init7Bit
},
176 { NULL
, CE_EXTENSION
, NULL
}, /* these two must be last! */
177 { NULL
, CE_UNKNOWN
, NULL
},
181 * NOTE WELL: si_key MUST NOT have value of NOTOK
183 * si_val is 1 if access method is anonymous.
185 struct str2init str2methods
[] = {
186 { "afs", 1, InitFile
},
187 { "anon-ftp", 1, InitFTP
},
188 { "ftp", 0, InitFTP
},
189 { "local-file", 0, InitFile
},
190 { "mail-server", 0, InitMail
},
191 { "url", 0, InitURL
},
197 * Main entry point for parsing a MIME message or file.
198 * It returns the Content structure for the top level
199 * entity in the file.
203 parse_mime (char *file
)
212 bogus_mp_content
= false;
215 * Check if file is actually standard input
217 if ((is_stdin
= !(strcmp (file
, "-")))) {
218 char *tfile
= m_mktemp2(NULL
, invo_name
, NULL
, &fp
);
220 advise("mhparse", "unable to create temporary file in %s",
224 file
= mh_xstrdup(tfile
);
226 while ((n
= fread(buffer
, 1, sizeof(buffer
), stdin
)) > 0) {
227 if (fwrite(buffer
, 1, n
, fp
) != n
) {
228 (void) m_unlink (file
);
229 advise (file
, "error copying to temporary file");
235 if (ferror (stdin
)) {
236 (void) m_unlink (file
);
237 advise ("stdin", "error reading");
241 (void) m_unlink (file
);
242 advise (file
, "error writing");
245 fseek (fp
, 0L, SEEK_SET
);
246 } else if (stat (file
, &statbuf
) == NOTOK
) {
247 advise (file
, "unable to stat");
249 } else if (S_ISDIR(statbuf
.st_mode
)) {
250 /* Don't try to parse a directory. */
251 inform("%s is a directory", file
);
253 } else if ((fp
= fopen (file
, "r")) == NULL
) {
254 advise (file
, "unable to read");
258 if (!(ct
= get_content (fp
, file
, 1))) {
260 (void) m_unlink (file
);
261 inform("unable to decode %s", file
);
266 ct
->c_unlink
= 1; /* temp file to remove */
270 if (ct
->c_end
== 0L) {
271 fseek (fp
, 0L, SEEK_END
);
272 ct
->c_end
= ftell (fp
);
275 if (ct
->c_ctinitfnx
&& (*ct
->c_ctinitfnx
) (ct
) == NOTOK
) {
287 * Main routine for reading/parsing the headers
288 * of a message content.
290 * toplevel = 1 # we are at the top level of the message
291 * toplevel = 0 # we are inside message type or multipart type
292 * # other than multipart/digest
293 * toplevel = -1 # we are inside multipart/digest
294 * NB: on failure we will fclose(in)!
298 get_content (FILE *in
, char *file
, int toplevel
)
301 char buf
[NMH_BUFSIZ
], name
[NAMESZ
];
305 m_getfld_state_t gstate
;
307 /* allocate the content structure */
310 ct
->c_file
= mh_xstrdup(FENDNULL(file
));
311 ct
->c_begin
= ftell (ct
->c_fp
) + 1;
314 * Parse the header fields for this
315 * content into a linked list.
317 gstate
= m_getfld_state_init(in
);
318 m_getfld_track_filepos2(&gstate
);
319 for (compnum
= 1;;) {
320 int bufsz
= sizeof buf
;
321 switch (state
= m_getfld2(&gstate
, name
, buf
, &bufsz
)) {
326 /* get copies of the buffers */
327 np
= mh_xstrdup(name
);
328 vp
= mh_xstrdup(buf
);
330 /* if necessary, get rest of field */
331 while (state
== FLDPLUS
) {
333 state
= m_getfld2(&gstate
, name
, buf
, &bufsz
);
334 vp
= add (buf
, vp
); /* add to previous value */
337 /* Now add the header data to the list */
338 add_header (ct
, np
, vp
);
340 /* continue, to see if this isn't the last header field */
341 ct
->c_begin
= ftell (in
) + 1;
345 /* There are two cases. The unusual one is when there is no
346 * blank line between the headers and the body. This is
347 * indicated by the name of the header starting with `:'.
349 * For both cases, normal first, `1' is the desired c_begin
350 * file position for the start of the body, and `2' is the
351 * file position when buf is returned.
353 * f o o : b a r \n \n b o d y \n bufsz = 6
355 * f o o : b a r \n b o d y \n bufsz = 4
358 * For the normal case, bufsz includes the
359 * header-terminating `\n', even though it is not in buf,
360 * but bufsz isn't affected when it's missing in the unusual
362 if (name
[0] == ':') {
363 ct
->c_begin
= ftell(in
) - bufsz
;
365 ct
->c_begin
= ftell (in
) - (bufsz
- 1);
370 ct
->c_begin
= ftell (in
);
375 die("message format error in component #%d", compnum
);
378 die("getfld() returned %d", state
);
381 /* break out of the loop */
384 m_getfld_state_destroy (&gstate
);
387 * Read the content headers. We will parse the
388 * MIME related header fields into their various
389 * structures and set internal flags related to
390 * content type/subtype, etc.
393 hp
= ct
->c_first_hf
; /* start at first header field */
395 /* Get MIME-Version field */
396 if (!strcasecmp (hp
->name
, VRSN_FIELD
)) {
401 vrsn
= mh_xstrdup(FENDNULL(hp
->value
));
403 /* Now, cleanup this field */
406 while (isspace ((unsigned char) *cp
))
408 for (dp
= strchr(cp
, '\n'); dp
; dp
= strchr(dp
, '\n'))
410 for (dp
= cp
+ strlen (cp
) - 1; dp
>= cp
; dp
--)
411 if (!isspace ((unsigned char) *dp
))
415 fprintf (stderr
, "%s: %s\n", VRSN_FIELD
, cp
);
418 get_comment (ct
->c_file
, VRSN_FIELD
, &cp
, NULL
) == NOTOK
)
421 for (dp
= cp
; istoken (*dp
); dp
++)
425 ucmp
= !strcasecmp (cp
, VRSN_VALUE
);
428 inform("message %s has unknown value for %s: field (%s), continuing...",
429 ct
->c_file
, VRSN_FIELD
, cp
);
434 if (! suppress_multiple_mime_version_warning
)
435 inform("message %s has multiple %s: fields",
436 ct
->c_file
, VRSN_FIELD
);
440 else if (!strcasecmp (hp
->name
, TYPE_FIELD
)) {
441 /* Get Content-Type field */
442 struct str2init
*s2i
;
443 CI ci
= &ct
->c_ctinfo
;
445 /* Check if we've already seen a Content-Type header */
447 inform("message %s has multiple %s: fields",
448 ct
->c_file
, TYPE_FIELD
);
452 /* Parse the Content-Type field */
453 if (get_ctinfo (hp
->value
, ct
, 0) == NOTOK
)
457 * Set the Init function and the internal
458 * flag for this content type.
460 for (s2i
= str2cts
; s2i
->si_key
; s2i
++)
461 if (!strcasecmp (ci
->ci_type
, s2i
->si_key
))
463 if (!s2i
->si_key
&& !uprf (ci
->ci_type
, "X-"))
465 ct
->c_type
= s2i
->si_val
;
466 ct
->c_ctinitfnx
= s2i
->si_init
;
468 else if (!strcasecmp (hp
->name
, ENCODING_FIELD
)) {
469 /* Get Content-Transfer-Encoding field */
471 struct str2init
*s2i
;
474 * Check if we've already seen the
475 * Content-Transfer-Encoding field
478 inform("message %s has multiple %s: fields",
479 ct
->c_file
, ENCODING_FIELD
);
483 /* get copy of this field */
484 ct
->c_celine
= cp
= mh_xstrdup(FENDNULL(hp
->value
));
486 while (isspace ((unsigned char) *cp
))
488 for (dp
= cp
; istoken (*dp
); dp
++)
494 * Find the internal flag and Init function
495 * for this transfer encoding.
497 for (s2i
= str2ces
; s2i
->si_key
; s2i
++)
498 if (!strcasecmp (cp
, s2i
->si_key
))
500 if (!s2i
->si_key
&& !uprf (cp
, "X-"))
503 ct
->c_encoding
= s2i
->si_val
;
505 /* Call the Init function for this encoding */
506 if (s2i
->si_init
&& (*s2i
->si_init
) (ct
) == NOTOK
)
509 else if (!strcasecmp (hp
->name
, MD5_FIELD
)) {
510 /* Get Content-MD5 field */
516 if (ct
->c_digested
) {
517 inform("message %s has multiple %s: fields",
518 ct
->c_file
, MD5_FIELD
);
522 ep
= cp
= mh_xstrdup(FENDNULL(hp
->value
)); /* get a copy */
524 while (isspace ((unsigned char) *cp
))
526 for (dp
= strchr(cp
, '\n'); dp
; dp
= strchr(dp
, '\n'))
528 for (dp
= cp
+ strlen (cp
) - 1; dp
>= cp
; dp
--)
529 if (!isspace ((unsigned char) *dp
))
533 fprintf (stderr
, "%s: %s\n", MD5_FIELD
, cp
);
536 get_comment (ct
->c_file
, MD5_FIELD
, &cp
, NULL
) == NOTOK
) {
541 for (dp
= cp
; *dp
&& !isspace ((unsigned char) *dp
); dp
++)
549 else if (!strcasecmp (hp
->name
, ID_FIELD
)) {
550 /* Get Content-ID field */
551 ct
->c_id
= add (hp
->value
, ct
->c_id
);
553 else if (!strcasecmp (hp
->name
, DESCR_FIELD
)) {
554 /* Get Content-Description field */
555 ct
->c_descr
= add (hp
->value
, ct
->c_descr
);
557 else if (!strcasecmp (hp
->name
, DISPO_FIELD
)) {
558 /* Get Content-Disposition field */
559 if (get_dispo(hp
->value
, ct
, 0) == NOTOK
)
564 hp
= hp
->next
; /* next header field */
568 * Check if we saw a Content-Type field.
569 * If not, then assign a default value for
570 * it, and the Init function.
574 * If we are inside a multipart/digest message,
575 * so default type is message/rfc822
578 if (get_ctinfo ("message/rfc822", ct
, 0) == NOTOK
)
580 ct
->c_type
= CT_MESSAGE
;
581 ct
->c_ctinitfnx
= InitMessage
;
584 * Else default type is text/plain
586 if (get_ctinfo ("text/plain", ct
, 0) == NOTOK
)
588 ct
->c_type
= CT_TEXT
;
589 ct
->c_ctinitfnx
= InitText
;
593 /* Use default Transfer-Encoding, if necessary */
595 ct
->c_encoding
= CE_7BIT
;
608 * small routine to add header field to list
612 add_header (CT ct
, char *name
, char *value
)
616 /* allocate header field structure */
619 /* link data into header structure */
624 /* link header structure into the list */
625 if (ct
->c_first_hf
== NULL
) {
626 ct
->c_first_hf
= hp
; /* this is the first */
629 ct
->c_last_hf
->next
= hp
; /* add it to the end */
638 * Parse Content-Type line and (if `magic' is non-zero) mhbuild composition
639 * directives. Fills in the information of the CTinfo structure.
642 get_ctinfo (char *cp
, CT ct
, int magic
)
651 /* store copy of Content-Type line */
652 cp
= ct
->c_ctline
= mh_xstrdup(FENDNULL(cp
));
654 while (isspace ((unsigned char) *cp
)) /* trim leading spaces */
657 /* change newlines to spaces */
658 for (dp
= strchr(cp
, '\n'); dp
; dp
= strchr(dp
, '\n'))
661 /* trim trailing spaces */
662 for (dp
= cp
+ strlen (cp
) - 1; dp
>= cp
; dp
--)
663 if (!isspace ((unsigned char) *dp
))
668 fprintf (stderr
, "%s: %s\n", TYPE_FIELD
, cp
);
670 if (*cp
== '(' && get_comment (ct
->c_file
, TYPE_FIELD
, &cp
,
671 &ci
->ci_comment
) == NOTOK
)
674 for (dp
= cp
; istoken (*dp
); dp
++)
678 ci
->ci_type
= mh_xstrdup(cp
); /* store content type */
683 inform("invalid %s: field in message %s (empty type)",
684 TYPE_FIELD
, ct
->c_file
);
687 to_lower(ci
->ci_type
);
689 while (isspace ((unsigned char) *cp
))
692 if (*cp
== '(' && get_comment (ct
->c_file
, TYPE_FIELD
, &cp
,
693 &ci
->ci_comment
) == NOTOK
)
698 ci
->ci_subtype
= mh_xstrdup("");
703 while (isspace ((unsigned char) *cp
))
706 if (*cp
== '(' && get_comment (ct
->c_file
, TYPE_FIELD
, &cp
,
707 &ci
->ci_comment
) == NOTOK
)
710 for (dp
= cp
; istoken (*dp
); dp
++)
714 ci
->ci_subtype
= mh_xstrdup(cp
); /* store the content subtype */
718 if (!*ci
->ci_subtype
) {
719 inform("invalid %s: field in message %s (empty subtype for \"%s\")",
720 TYPE_FIELD
, ct
->c_file
, ci
->ci_type
);
723 to_lower(ci
->ci_subtype
);
726 while (isspace ((unsigned char) *cp
))
729 if (*cp
== '(' && get_comment (ct
->c_file
, TYPE_FIELD
, &cp
,
730 &ci
->ci_comment
) == NOTOK
)
733 if ((status
= parse_header_attrs (ct
->c_file
, TYPE_FIELD
, &cp
,
734 &ci
->ci_first_pm
, &ci
->ci_last_pm
,
735 &ci
->ci_comment
)) != OK
) {
736 return status
== NOTOK
? NOTOK
: OK
;
740 * Get any <Content-Id> given in buffer
742 if (magic
&& *cp
== '<') {
745 if (!(dp
= strchr(ct
->c_id
= ++cp
, '>'))) {
746 inform("invalid ID in message %s", ct
->c_file
);
752 ct
->c_id
= concat ("<", ct
->c_id
, ">\n", NULL
);
758 while (isspace ((unsigned char) *cp
))
763 * Get any [Content-Description] given in buffer.
765 if (magic
&& *cp
== '[') {
767 for (dp
= cp
+ strlen (cp
) - 1; dp
>= cp
; dp
--)
771 inform("invalid description in message %s", ct
->c_file
);
779 ct
->c_descr
= concat (ct
->c_descr
, "\n", NULL
);
785 while (isspace ((unsigned char) *cp
))
790 * Get any {Content-Disposition} given in buffer.
792 if (magic
&& *cp
== '{') {
794 for (dp
= cp
+ strlen (cp
) - 1; dp
>= cp
; dp
--)
798 inform("invalid disposition in message %s", ct
->c_file
);
806 if (get_dispo(cp
, ct
, 1) != OK
)
812 while (isspace ((unsigned char) *cp
))
817 * Get any extension directives (right now just the content transfer
818 * encoding, but maybe others) that we care about.
821 if (magic
&& *cp
== '*') {
823 * See if it's a CTE we match on
828 while (*cp
!= '\0' && ! isspace((unsigned char) *cp
))
832 inform("invalid null transfer encoding specification");
839 ct
->c_reqencoding
= CE_UNKNOWN
;
841 for (kv
= EncodingType
; kv
->kv_key
; kv
++) {
842 if (strcasecmp(kv
->kv_key
, dp
) == 0) {
843 ct
->c_reqencoding
= kv
->kv_value
;
848 if (ct
->c_reqencoding
== CE_UNKNOWN
) {
849 inform("invalid CTE specification: \"%s\"", dp
);
853 while (isspace ((unsigned char) *cp
))
858 * Check if anything is left over
862 ci
->ci_magic
= mh_xstrdup(cp
);
864 /* If there is a Content-Disposition header and it doesn't
865 have a *filename=, extract it from the magic contents.
866 The r1bindex call skips any leading directory
868 if (ct
->c_dispo_type
&&
869 !get_param(ct
->c_dispo_first
, "filename", '_', 1)) {
870 add_param(&ct
->c_dispo_first
, &ct
->c_dispo_last
, "filename",
871 r1bindex(ci
->ci_magic
, '/'), 0);
875 inform("extraneous information in message %s's %s: field\n"
876 " (%s)", ct
->c_file
, TYPE_FIELD
, cp
);
884 * Parse out a Content-Disposition header. A lot of this is cribbed from
888 get_dispo (char *cp
, CT ct
, int buildflag
)
890 char *dp
, *dispoheader
;
895 * Save the whole copy of the Content-Disposition header, unless we're
896 * processing a mhbuild directive. A NULL c_dispo will be a flag to
897 * mhbuild that the disposition header needs to be generated at that
901 dispoheader
= cp
= mh_xstrdup(FENDNULL(cp
));
903 while (isspace ((unsigned char) *cp
)) /* trim leading spaces */
906 /* change newlines to spaces */
907 for (dp
= strchr(cp
, '\n'); dp
; dp
= strchr(dp
, '\n'))
910 /* trim trailing spaces */
911 for (dp
= cp
+ strlen (cp
) - 1; dp
>= cp
; dp
--)
912 if (!isspace ((unsigned char) *dp
))
917 fprintf (stderr
, "%s: %s\n", DISPO_FIELD
, cp
);
919 if (*cp
== '(' && get_comment (ct
->c_file
, DISPO_FIELD
, &cp
, NULL
) ==
925 for (dp
= cp
; istoken (*dp
); dp
++)
929 ct
->c_dispo_type
= mh_xstrdup(cp
); /* store disposition type */
933 if (*cp
== '(' && get_comment (ct
->c_file
, DISPO_FIELD
, &cp
, NULL
) == NOTOK
)
936 if ((status
= parse_header_attrs (ct
->c_file
, DISPO_FIELD
, &cp
,
937 &ct
->c_dispo_first
, &ct
->c_dispo_last
,
939 if (status
== NOTOK
) {
944 inform("extraneous information in message %s's %s: field\n (%s)",
945 ct
->c_file
, DISPO_FIELD
, cp
);
951 ct
->c_dispo
= dispoheader
;
958 get_comment (const char *filename
, const char *fieldname
, char **ap
,
963 char c
, buffer
[BUFSIZ
], *dp
;
973 inform("invalid comment in message %s's %s: field",
974 filename
, fieldname
);
979 if ((c
= *cp
++) == '\0')
1002 if ((dp
= *commentp
)) {
1003 *commentp
= concat (dp
, " ", buffer
, NULL
);
1006 *commentp
= mh_xstrdup(buffer
);
1010 while (isspace ((unsigned char) *cp
))
1021 * Handles content types audio, image, and video.
1022 * There's not much to do right here.
1030 return OK
; /* not much to do here */
1041 char buffer
[BUFSIZ
];
1046 CI ci
= &ct
->c_ctinfo
;
1048 /* check for missing subtype */
1049 if (!*ci
->ci_subtype
)
1050 ci
->ci_subtype
= add ("plain", ci
->ci_subtype
);
1053 ct
->c_subtype
= ct_str_subtype (CT_TEXT
, ci
->ci_subtype
);
1055 /* allocate text character set structure */
1057 ct
->c_ctparams
= (void *) t
;
1059 /* scan for charset parameter */
1060 for (pm
= ci
->ci_first_pm
; pm
; pm
= pm
->pm_next
)
1061 if (!strcasecmp (pm
->pm_name
, "charset"))
1064 /* check if content specified a character set */
1066 chset
= pm
->pm_value
;
1067 t
->tx_charset
= CHARSET_SPECIFIED
;
1069 t
->tx_charset
= CHARSET_UNSPECIFIED
;
1073 * If we can not handle character set natively,
1074 * then check profile for string to modify the
1075 * terminal or display method.
1077 * termproc is for mhshow, though mhlist -debug prints it, too.
1079 if (chset
!= NULL
&& !check_charset (chset
, strlen (chset
))) {
1080 snprintf (buffer
, sizeof(buffer
), "%s-charset-%s", invo_name
, chset
);
1081 if ((cp
= context_find (buffer
)))
1082 ct
->c_termproc
= mh_xstrdup(cp
);
1094 InitMultiPart (CT ct
)
1104 struct multipart
*m
;
1105 struct part
*part
, **next
;
1106 CI ci
= &ct
->c_ctinfo
;
1111 * The encoding for multipart messages must be either
1112 * 7bit, 8bit, or binary (per RFC 2045).
1114 if (! skip_mp_cte_check
&& ct
->c_encoding
!= CE_7BIT
&&
1115 ct
->c_encoding
!= CE_8BIT
&& ct
->c_encoding
!= CE_BINARY
) {
1116 /* Copy the Content-Transfer-Encoding header field body so we can
1117 remove any trailing whitespace and leading blanks from it. */
1118 char *cte
= mh_xstrdup(ct
->c_celine
? ct
->c_celine
: "(null)");
1120 bp
= cte
+ strlen (cte
) - 1;
1121 while (bp
>= cte
&& isspace ((unsigned char) *bp
)) *bp
-- = '\0';
1122 for (bp
= cte
; isblank((unsigned char)*bp
); ++bp
) continue;
1124 inform("\"%s/%s\" type in message %s must be encoded in\n"
1125 "7bit, 8bit, or binary, per RFC 2045 (6.4). "
1126 "mhfixmsg -fixcte can fix it, or\n"
1127 "manually edit the file and change the \"%s\"\n"
1128 "Content-Transfer-Encoding to one of those. For now, continuing...",
1129 ci
->ci_type
, ci
->ci_subtype
, ct
->c_file
, bp
);
1136 ct
->c_subtype
= ct_str_subtype (CT_MULTIPART
, ci
->ci_subtype
);
1139 * Check for "boundary" parameter, which is
1140 * required for multipart messages.
1143 for (pm
= ci
->ci_first_pm
; pm
; pm
= pm
->pm_next
) {
1144 if (!strcasecmp (pm
->pm_name
, "boundary")) {
1150 /* complain if boundary parameter is missing */
1152 inform("a \"boundary\" parameter is mandatory for \"%s/%s\" type in message %s's %s: field",
1153 ci
->ci_type
, ci
->ci_subtype
, ct
->c_file
, TYPE_FIELD
);
1157 /* allocate primary structure for multipart info */
1159 ct
->c_ctparams
= (void *) m
;
1161 /* check if boundary parameter contains only whitespace characters */
1162 for (cp
= bp
; isspace ((unsigned char) *cp
); cp
++)
1165 inform("invalid \"boundary\" parameter for \"%s/%s\" type in message %s's %s: field",
1166 ci
->ci_type
, ci
->ci_subtype
, ct
->c_file
, TYPE_FIELD
);
1170 /* remove trailing whitespace from boundary parameter */
1171 for (cp
= bp
, dp
= cp
+ strlen (cp
) - 1; dp
> cp
; dp
--)
1172 if (!isspace ((unsigned char) *dp
))
1176 /* record boundary separators */
1177 m
->mp_start
= concat (bp
, "\n", NULL
);
1178 m
->mp_stop
= concat (bp
, "--\n", NULL
);
1180 if (!ct
->c_fp
&& (ct
->c_fp
= fopen (ct
->c_file
, "r")) == NULL
) {
1181 advise (ct
->c_file
, "unable to open for reading");
1185 fseek (fp
= ct
->c_fp
, pos
= ct
->c_begin
, SEEK_SET
);
1187 next
= &m
->mp_parts
;
1191 while ((gotlen
= getline(&bufp
, &buflen
, fp
)) != -1) {
1196 if (bufp
[0] != '-' || bufp
[1] != '-')
1199 if (strcmp (bufp
+ 2, m
->mp_start
))
1204 next
= &part
->mp_next
;
1206 if (!(p
= get_content (fp
, ct
->c_file
,
1207 ct
->c_subtype
== MULTI_DIGEST
? -1 : 0))) {
1215 fseek (fp
, pos
, SEEK_SET
);
1218 if (strcmp (bufp
+ 2, m
->mp_start
) == 0) {
1222 p
->c_end
= ftell(fp
) - (gotlen
+ 1);
1223 if (p
->c_end
< p
->c_begin
)
1224 p
->c_begin
= p
->c_end
;
1229 if (strcmp (bufp
+ 2, m
->mp_stop
) == 0)
1234 if (! suppress_bogus_mp_content_warning
) {
1235 inform("bogus multipart content in message %s", ct
->c_file
);
1237 bogus_mp_content
= true;
1239 if (!inout
&& part
) {
1241 p
->c_end
= ct
->c_end
;
1243 if (p
->c_begin
>= p
->c_end
) {
1244 for (next
= &m
->mp_parts
; *next
!= part
;
1245 next
= &((*next
)->mp_next
))
1254 /* reverse the order of the parts for multipart/alternative */
1255 if (ct
->c_subtype
== MULTI_ALTERNATE
) {
1261 * label all subparts with part number, and
1262 * then initialize the content of the subpart.
1267 char partnam
[BUFSIZ
];
1270 snprintf (partnam
, sizeof(partnam
), "%s.", ct
->c_partno
);
1271 pp
= partnam
+ strlen (partnam
);
1276 for (part
= m
->mp_parts
, partnum
= 1; part
;
1277 part
= part
->mp_next
, partnum
++) {
1280 sprintf (pp
, "%d", partnum
);
1281 p
->c_partno
= mh_xstrdup(partnam
);
1283 /* initialize the content of the subparts */
1284 if (p
->c_ctinitfnx
&& (*p
->c_ctinitfnx
) (p
) == NOTOK
) {
1293 get_leftover_mp_content (ct
, 1);
1294 get_leftover_mp_content (ct
, 0);
1304 * reverse the order of the parts of a multipart/alternative,
1305 * presumably to put the "most favored" alternative first, for
1306 * ease of choosing/displaying it later on. from a mail message on
1307 * nmh-workers, from kenh:
1308 * "Stock" MH 6.8.5 did not have a reverse_parts() function, but I
1309 * see code in mhn that did the same thing... According to the RCS
1310 * logs, that code was around from the initial checkin of mhn.c by
1311 * John Romine in 1992, which is as far back as we have."
1314 reverse_parts (CT ct
)
1316 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
1320 /* Reverse the order of its parts by walking the mp_parts list
1321 and pushing each node to the front. */
1322 for (part
= m
->mp_parts
, m
->mp_parts
= NULL
; part
; part
= next
) {
1323 next
= part
->mp_next
;
1324 part
->mp_next
= m
->mp_parts
;
1330 move_preferred_part(CT ct
, mime_type_subtype
*pref
)
1332 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
1333 struct part
*part
, *prev
, *head
, *nhead
, *ntail
;
1337 /* move the matching part(s) to the head of the list: walk the
1338 * list of parts, move matching parts to a new list (maintaining
1339 * their order), and finally, concatenate the old list onto the
1346 head
->mp_next
= m
->mp_parts
;
1347 nhead
->mp_next
= NULL
;
1351 part
= head
->mp_next
;
1352 while (part
!= NULL
) {
1353 ci
= &part
->mp_part
->c_ctinfo
;
1354 if (!strcasecmp(ci
->ci_type
, pref
->type
) &&
1356 !strcasecmp(ci
->ci_subtype
, pref
->subtype
))) {
1357 prev
->mp_next
= part
->mp_next
;
1358 part
->mp_next
= NULL
;
1359 ntail
->mp_next
= part
;
1361 part
= prev
->mp_next
;
1364 part
= prev
->mp_next
;
1367 ntail
->mp_next
= head
->mp_next
;
1368 m
->mp_parts
= nhead
->mp_next
;
1372 * move parts that match the user's preferences (-prefer) to the head
1373 * of the line. process preferences in reverse so first one given
1374 * ends up first in line
1380 for (i
= 0; i
< npreferred
; i
++)
1381 move_preferred_part(ct
, mime_preference
+ i
);
1386 /* parse_mime() arranges alternates in reverse (priority) order. This
1387 function can be used to reverse them back. This will put, for
1388 example, a text/plain part before a text/html part in a
1389 multipart/alternative part, for example, where it belongs. */
1391 reverse_alternative_parts (CT ct
)
1393 if (ct
->c_type
== CT_MULTIPART
) {
1394 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
1397 if (ct
->c_subtype
== MULTI_ALTERNATE
) {
1401 /* And call recursively on each part of a multipart. */
1402 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
1403 reverse_alternative_parts (part
->mp_part
);
1416 CI ci
= &ct
->c_ctinfo
;
1418 if ((ct
->c_encoding
!= CE_7BIT
) && (ct
->c_encoding
!= CE_8BIT
)) {
1419 inform("\"%s/%s\" type in message %s should be encoded in "
1420 "7bit or 8bit, continuing...", ci
->ci_type
, ci
->ci_subtype
,
1425 /* check for missing subtype */
1426 if (!*ci
->ci_subtype
)
1427 ci
->ci_subtype
= add ("rfc822", ci
->ci_subtype
);
1430 ct
->c_subtype
= ct_str_subtype (CT_MESSAGE
, ci
->ci_subtype
);
1432 switch (ct
->c_subtype
) {
1433 case MESSAGE_RFC822
:
1436 case MESSAGE_PARTIAL
:
1442 ct
->c_ctparams
= (void *) p
;
1444 /* scan for parameters "id", "number", and "total" */
1445 for (pm
= ci
->ci_first_pm
; pm
; pm
= pm
->pm_next
) {
1446 if (!strcasecmp (pm
->pm_name
, "id")) {
1447 p
->pm_partid
= mh_xstrdup(FENDNULL(pm
->pm_value
));
1450 if (!strcasecmp (pm
->pm_name
, "number")) {
1451 if (sscanf (pm
->pm_value
, "%d", &p
->pm_partno
) != 1
1452 || p
->pm_partno
< 1) {
1454 inform("invalid %s parameter for \"%s/%s\" type in message %s's %s field",
1455 pm
->pm_name
, ci
->ci_type
, ci
->ci_subtype
,
1456 ct
->c_file
, TYPE_FIELD
);
1461 if (!strcasecmp (pm
->pm_name
, "total")) {
1462 if (sscanf (pm
->pm_value
, "%d", &p
->pm_maxno
) != 1
1471 || (p
->pm_maxno
&& p
->pm_partno
> p
->pm_maxno
)) {
1472 inform("invalid parameters for \"%s/%s\" type in message %s's %s field",
1473 ci
->ci_type
, ci
->ci_subtype
, ct
->c_file
, TYPE_FIELD
);
1479 case MESSAGE_EXTERNAL
:
1487 ct
->c_ctparams
= (void *) e
;
1490 && (ct
->c_fp
= fopen (ct
->c_file
, "r")) == NULL
) {
1491 advise (ct
->c_file
, "unable to open for reading");
1495 fseek (fp
= ct
->c_fp
, ct
->c_begin
, SEEK_SET
);
1497 if (!(p
= get_content (fp
, ct
->c_file
, 0))) {
1505 p
->c_ceopenfnx
= NULL
;
1506 if ((exresult
= params_external (ct
, 0)) != NOTOK
1507 && p
->c_ceopenfnx
== openMail
) {
1511 if ((size
= ct
->c_end
- p
->c_begin
) <= 0) {
1513 content_error (NULL
, ct
,
1514 "empty body for access-type=mail-server");
1518 e
->eb_body
= bp
= mh_xmalloc ((unsigned) size
);
1519 fseek (p
->c_fp
, p
->c_begin
, SEEK_SET
);
1521 switch (cc
= fread (bp
, sizeof(*bp
), size
, p
->c_fp
)) {
1523 adios ("failed", "fread");
1526 die("unexpected EOF from fread");
1529 bp
+= cc
, size
-= cc
;
1536 p
->c_end
= p
->c_begin
;
1541 if (exresult
== NOTOK
)
1543 if (e
->eb_flags
== NOTOK
)
1546 switch (p
->c_type
) {
1551 if (p
->c_subtype
!= MESSAGE_RFC822
)
1555 e
->eb_partno
= ct
->c_partno
;
1557 (*p
->c_ctinitfnx
) (p
);
1572 params_external (CT ct
, int composing
)
1575 struct exbody
*e
= (struct exbody
*) ct
->c_ctparams
;
1576 CI ci
= &ct
->c_ctinfo
;
1578 ct
->c_ceopenfnx
= NULL
;
1579 for (pm
= ci
->ci_first_pm
; pm
; pm
= pm
->pm_next
) {
1580 if (!strcasecmp (pm
->pm_name
, "access-type")) {
1581 struct str2init
*s2i
;
1582 CT p
= e
->eb_content
;
1584 for (s2i
= str2methods
; s2i
->si_key
; s2i
++)
1585 if (!strcasecmp (pm
->pm_value
, s2i
->si_key
))
1588 e
->eb_access
= pm
->pm_value
;
1589 e
->eb_flags
= NOTOK
;
1590 p
->c_encoding
= CE_EXTERNAL
;
1593 e
->eb_access
= s2i
->si_key
;
1594 e
->eb_flags
= s2i
->si_val
;
1595 p
->c_encoding
= CE_EXTERNAL
;
1597 /* Call the Init function for this external type */
1598 if ((*s2i
->si_init
)(p
) == NOTOK
)
1602 if (!strcasecmp (pm
->pm_name
, "name")) {
1603 e
->eb_name
= pm
->pm_value
;
1606 if (!strcasecmp (pm
->pm_name
, "permission")) {
1607 e
->eb_permission
= pm
->pm_value
;
1610 if (!strcasecmp (pm
->pm_name
, "site")) {
1611 e
->eb_site
= pm
->pm_value
;
1614 if (!strcasecmp (pm
->pm_name
, "directory")) {
1615 e
->eb_dir
= pm
->pm_value
;
1618 if (!strcasecmp (pm
->pm_name
, "mode")) {
1619 e
->eb_mode
= pm
->pm_value
;
1622 if (!strcasecmp (pm
->pm_name
, "size")) {
1623 sscanf (pm
->pm_value
, "%lu", &e
->eb_size
);
1626 if (!strcasecmp (pm
->pm_name
, "server")) {
1627 e
->eb_server
= pm
->pm_value
;
1630 if (!strcasecmp (pm
->pm_name
, "subject")) {
1631 e
->eb_subject
= pm
->pm_value
;
1634 if (!strcasecmp (pm
->pm_name
, "url")) {
1636 * According to RFC 2017, we have to remove all whitespace from
1640 char *u
, *p
= pm
->pm_value
;
1641 e
->eb_url
= u
= mh_xmalloc(strlen(pm
->pm_value
) + 1);
1643 for (; *p
!= '\0'; p
++) {
1644 if (! isspace((unsigned char) *p
))
1651 if (composing
&& !strcasecmp (pm
->pm_name
, "body")) {
1652 e
->eb_body
= getcpy (pm
->pm_value
);
1657 if (!e
->eb_access
) {
1658 inform("invalid parameters for \"%s/%s\" type in message %s's %s field",
1659 ci
->ci_type
, ci
->ci_subtype
, ct
->c_file
, TYPE_FIELD
);
1672 InitApplication (CT ct
)
1674 CI ci
= &ct
->c_ctinfo
;
1677 ct
->c_subtype
= ct_str_subtype (CT_APPLICATION
, ci
->ci_subtype
);
1684 * TRANSFER ENCODINGS
1688 init_encoding (CT ct
, OpenCEFunc openfnx
)
1690 ct
->c_ceopenfnx
= openfnx
;
1691 ct
->c_ceclosefnx
= close_encoding
;
1692 ct
->c_cesizefnx
= size_encoding
;
1699 close_encoding (CT ct
)
1701 CE ce
= &ct
->c_cefile
;
1710 static unsigned long
1711 size_encoding (CT ct
)
1716 CE ce
= &ct
->c_cefile
;
1719 if (ce
->ce_fp
&& fstat (fileno (ce
->ce_fp
), &st
) != NOTOK
)
1720 return (long) st
.st_size
;
1723 if (stat (ce
->ce_file
, &st
) != NOTOK
)
1724 return (long) st
.st_size
;
1728 if (ct
->c_encoding
== CE_EXTERNAL
)
1729 return ct
->c_end
- ct
->c_begin
;
1732 if ((fd
= (*ct
->c_ceopenfnx
) (ct
, &file
)) == NOTOK
)
1733 return ct
->c_end
- ct
->c_begin
;
1735 if (fstat (fd
, &st
) != NOTOK
)
1736 size
= (long) st
.st_size
;
1740 (*ct
->c_ceclosefnx
) (ct
);
1752 return init_encoding (ct
, openBase64
);
1757 openBase64 (CT ct
, char **file
)
1761 bool own_ct_fp
= false;
1762 char *cp
, *buffer
= NULL
;
1763 /* sbeck -- handle suffixes */
1765 CE ce
= &ct
->c_cefile
;
1766 unsigned char *decoded
;
1768 unsigned char digest
[16];
1771 fseek (ce
->ce_fp
, 0L, SEEK_SET
);
1776 if ((ce
->ce_fp
= fopen (ce
->ce_file
, "r")) == NULL
) {
1777 content_error (ce
->ce_file
, ct
, "unable to fopen for reading");
1783 if (*file
== NULL
) {
1786 ce
->ce_file
= mh_xstrdup(*file
);
1790 /* sbeck@cise.ufl.edu -- handle suffixes */
1792 if ((cp
= context_find_by_type ("suffix", ci
->ci_type
, ci
->ci_subtype
))) {
1793 if (ce
->ce_unlink
) {
1794 /* Create temporary file with filename extension. */
1795 if ((ce
->ce_file
= m_mktemps(invo_name
, cp
, NULL
, NULL
)) == NULL
) {
1796 die("unable to create temporary file in %s",
1800 ce
->ce_file
= add (cp
, ce
->ce_file
);
1802 } else if (*file
== NULL
) {
1804 if ((tempfile
= m_mktemp2(NULL
, invo_name
, NULL
, NULL
)) == NULL
) {
1805 die("unable to create temporary file in %s",
1808 ce
->ce_file
= mh_xstrdup(tempfile
);
1811 if ((ce
->ce_fp
= fopen (ce
->ce_file
, "w+")) == NULL
) {
1812 content_error (ce
->ce_file
, ct
, "unable to fopen for reading/writing");
1816 if ((len
= ct
->c_end
- ct
->c_begin
) < 0)
1817 die("internal error(1)");
1819 buffer
= mh_xmalloc (len
+ 1);
1822 if ((ct
->c_fp
= fopen (ct
->c_file
, "r")) == NULL
) {
1823 content_error (ct
->c_file
, ct
, "unable to open for reading");
1829 lseek (fd
= fileno (ct
->c_fp
), (off_t
) ct
->c_begin
, SEEK_SET
);
1832 switch (cc
= read (fd
, cp
, len
)) {
1834 content_error (ct
->c_file
, ct
, "error reading from");
1838 content_error (NULL
, ct
, "premature eof");
1849 /* decodeBase64() requires null-terminated input. */
1852 if (decodeBase64 (buffer
, &decoded
, &decoded_len
, ct
->c_type
== CT_TEXT
,
1853 ct
->c_digested
? digest
: NULL
) != OK
)
1858 unsigned char *decoded_p
= decoded
;
1859 for (i
= 0; i
< decoded_len
; ++i
) {
1860 putc (*decoded_p
++, ce
->ce_fp
);
1863 if (ferror (ce
->ce_fp
)) {
1864 content_error (ce
->ce_file
, ct
, "error writing to");
1868 if (ct
->c_digested
) {
1869 if (memcmp(digest
, ct
->c_digest
,
1871 content_error (NULL
, ct
,
1872 "content integrity suspect (digest mismatch) -- continuing");
1875 fprintf (stderr
, "content integrity confirmed\n");
1881 fseek (ct
->c_fp
, 0L, SEEK_SET
);
1883 if (fflush (ce
->ce_fp
)) {
1884 content_error (ce
->ce_file
, ct
, "error writing to");
1888 fseek (ce
->ce_fp
, 0L, SEEK_SET
);
1891 *file
= ce
->ce_file
;
1897 return fileno (ce
->ce_fp
);
1904 free_encoding (ct
, 0);
1914 static char hex2nib
[0x80] = {
1915 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1916 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1917 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1918 0x00, 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, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
1922 0x08, 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1923 0x00, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x00,
1924 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1925 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1926 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1927 0x00, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x00,
1928 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1929 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1930 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
1937 return init_encoding (ct
, openQuoted
);
1942 openQuoted (CT ct
, char **file
)
1944 int cc
, digested
, len
, quoted
;
1945 bool own_ct_fp
= false;
1951 CE ce
= &ct
->c_cefile
;
1952 /* sbeck -- handle suffixes */
1957 fseek (ce
->ce_fp
, 0L, SEEK_SET
);
1962 if ((ce
->ce_fp
= fopen (ce
->ce_file
, "r")) == NULL
) {
1963 content_error (ce
->ce_file
, ct
, "unable to fopen for reading");
1969 if (*file
== NULL
) {
1972 ce
->ce_file
= mh_xstrdup(*file
);
1976 /* sbeck@cise.ufl.edu -- handle suffixes */
1978 if ((cp
= context_find_by_type ("suffix", ci
->ci_type
, ci
->ci_subtype
))) {
1979 if (ce
->ce_unlink
) {
1980 /* Create temporary file with filename extension. */
1981 if ((ce
->ce_file
= m_mktemps(invo_name
, cp
, NULL
, NULL
)) == NULL
) {
1982 die("unable to create temporary file in %s",
1986 ce
->ce_file
= add (cp
, ce
->ce_file
);
1988 } else if (*file
== NULL
) {
1990 if ((tempfile
= m_mktemp2(NULL
, invo_name
, NULL
, NULL
)) == NULL
) {
1991 die("unable to create temporary file in %s",
1994 ce
->ce_file
= mh_xstrdup(tempfile
);
1997 if ((ce
->ce_fp
= fopen (ce
->ce_file
, "w+")) == NULL
) {
1998 content_error (ce
->ce_file
, ct
, "unable to fopen for reading/writing");
2002 if ((len
= ct
->c_end
- ct
->c_begin
) < 0)
2003 die("internal error(2)");
2006 if ((ct
->c_fp
= fopen (ct
->c_file
, "r")) == NULL
) {
2007 content_error (ct
->c_file
, ct
, "unable to open for reading");
2013 if ((digested
= ct
->c_digested
))
2014 MD5Init (&mdContext
);
2018 fseek (ct
->c_fp
, ct
->c_begin
, SEEK_SET
);
2020 if ((gotlen
= getline(&bufp
, &buflen
, ct
->c_fp
)) == -1) {
2021 content_error (NULL
, ct
, "premature eof");
2025 if ((cc
= gotlen
) > len
)
2029 for (ep
= (cp
= bufp
) + cc
- 1; cp
<= ep
; ep
--)
2030 if (!isspace ((unsigned char) *ep
))
2035 for (; cp
< ep
; cp
++) {
2037 /* in an escape sequence */
2039 /* at byte 1 of an escape sequence */
2040 mask
= hex2nib
[((unsigned char) *cp
) & 0x7f];
2041 /* next is byte 2 */
2044 /* at byte 2 of an escape sequence */
2046 mask
|= hex2nib
[((unsigned char) *cp
) & 0x7f];
2047 putc (mask
, ce
->ce_fp
);
2049 MD5Update (&mdContext
, &mask
, 1);
2050 if (ferror (ce
->ce_fp
)) {
2051 content_error (ce
->ce_file
, ct
, "error writing to");
2054 /* finished escape sequence; next may be literal or a new
2055 * escape sequence */
2058 /* on to next byte */
2062 /* not in an escape sequence */
2064 /* starting an escape sequence, or invalid '='? */
2065 if (cp
+ 1 < ep
&& cp
[1] == '\n') {
2066 /* "=\n" soft line break, eat the \n */
2070 if (cp
+ 1 >= ep
|| cp
+ 2 >= ep
) {
2071 /* We don't have 2 bytes left, so this is an invalid
2072 * escape sequence; just show the raw bytes (below). */
2073 } else if (isxdigit ((unsigned char) cp
[1]) &&
2074 isxdigit ((unsigned char) cp
[2])) {
2075 /* Next 2 bytes are hex digits, making this a valid escape
2076 * sequence; let's decode it (above). */
2080 /* One or both of the next 2 is out of range, making this
2081 * an invalid escape sequence; just show the raw bytes
2085 /* Just show the raw byte. */
2086 putc (*cp
, ce
->ce_fp
);
2089 MD5Update (&mdContext
, (unsigned char *) "\r\n",2);
2091 MD5Update (&mdContext
, (unsigned char *) cp
, 1);
2094 if (ferror (ce
->ce_fp
)) {
2095 content_error (ce
->ce_file
, ct
, "error writing to");
2101 content_error (NULL
, ct
,
2102 "invalid QUOTED-PRINTABLE encoding -- end-of-content while still quoting");
2106 fseek (ct
->c_fp
, 0L, SEEK_SET
);
2108 if (fflush (ce
->ce_fp
)) {
2109 content_error (ce
->ce_file
, ct
, "error writing to");
2114 unsigned char digest
[16];
2116 MD5Final (digest
, &mdContext
);
2117 if (memcmp(digest
, ct
->c_digest
,
2119 content_error (NULL
, ct
,
2120 "content integrity suspect (digest mismatch) -- continuing");
2122 fprintf (stderr
, "content integrity confirmed\n");
2125 fseek (ce
->ce_fp
, 0L, SEEK_SET
);
2128 *file
= ce
->ce_file
;
2134 return fileno (ce
->ce_fp
);
2137 free_encoding (ct
, 0);
2154 if (init_encoding (ct
, open7Bit
) == NOTOK
)
2157 ct
->c_cesizefnx
= NULL
; /* no need to decode for real size */
2163 open7Bit (CT ct
, char **file
)
2166 bool own_ct_fp
= false;
2167 char buffer
[BUFSIZ
];
2168 /* sbeck -- handle suffixes */
2171 CE ce
= &ct
->c_cefile
;
2174 fseek (ce
->ce_fp
, 0L, SEEK_SET
);
2179 if ((ce
->ce_fp
= fopen (ce
->ce_file
, "r")) == NULL
) {
2180 content_error (ce
->ce_file
, ct
, "unable to fopen for reading");
2186 if (*file
== NULL
) {
2189 ce
->ce_file
= mh_xstrdup(*file
);
2193 /* sbeck@cise.ufl.edu -- handle suffixes */
2195 if ((cp
= context_find_by_type ("suffix", ci
->ci_type
, ci
->ci_subtype
))) {
2196 if (ce
->ce_unlink
) {
2197 /* Create temporary file with filename extension. */
2198 if ((ce
->ce_file
= m_mktemps(invo_name
, cp
, NULL
, NULL
)) == NULL
) {
2199 die("unable to create temporary file in %s",
2203 ce
->ce_file
= add (cp
, ce
->ce_file
);
2205 } else if (*file
== NULL
) {
2207 if ((tempfile
= m_mktemp2(NULL
, invo_name
, NULL
, NULL
)) == NULL
) {
2208 die("unable to create temporary file in %s",
2211 ce
->ce_file
= mh_xstrdup(tempfile
);
2214 if ((ce
->ce_fp
= fopen (ce
->ce_file
, "w+")) == NULL
) {
2215 content_error (ce
->ce_file
, ct
, "unable to fopen for reading/writing");
2219 if (ct
->c_type
== CT_MULTIPART
) {
2220 CI ci
= &ct
->c_ctinfo
;
2224 fprintf (ce
->ce_fp
, "%s: %s/%s", TYPE_FIELD
, ci
->ci_type
, ci
->ci_subtype
);
2225 len
+= LEN(TYPE_FIELD
) + 2 + strlen (ci
->ci_type
)
2226 + 1 + strlen (ci
->ci_subtype
);
2227 buffer
= output_params(len
, ci
->ci_first_pm
, &len
, 0);
2230 fputs (buffer
, ce
->ce_fp
);
2234 if (ci
->ci_comment
) {
2235 if (len
+ 1 + (cc
= 2 + strlen (ci
->ci_comment
)) >= CPERLIN
) {
2236 fputs ("\n\t", ce
->ce_fp
);
2240 putc (' ', ce
->ce_fp
);
2243 fprintf (ce
->ce_fp
, "(%s)", ci
->ci_comment
);
2246 fprintf (ce
->ce_fp
, "\n");
2248 fprintf (ce
->ce_fp
, "%s:%s", ID_FIELD
, ct
->c_id
);
2250 fprintf (ce
->ce_fp
, "%s:%s", DESCR_FIELD
, ct
->c_descr
);
2252 fprintf (ce
->ce_fp
, "%s:%s", DISPO_FIELD
, ct
->c_dispo
);
2253 fprintf (ce
->ce_fp
, "\n");
2256 if ((len
= ct
->c_end
- ct
->c_begin
) < 0)
2257 die("internal error(3)");
2260 if ((ct
->c_fp
= fopen (ct
->c_file
, "r")) == NULL
) {
2261 content_error (ct
->c_file
, ct
, "unable to open for reading");
2267 lseek (fd
= fileno (ct
->c_fp
), (off_t
) ct
->c_begin
, SEEK_SET
);
2269 switch (cc
= read (fd
, buffer
, sizeof(buffer
) - 1)) {
2271 content_error (ct
->c_file
, ct
, "error reading from");
2275 content_error (NULL
, ct
, "premature eof");
2283 if ((int) fwrite (buffer
, sizeof(*buffer
), cc
, ce
->ce_fp
) < cc
) {
2284 advise ("open7Bit", "fwrite");
2286 if (ferror (ce
->ce_fp
)) {
2287 content_error (ce
->ce_file
, ct
, "error writing to");
2292 fseek (ct
->c_fp
, 0L, SEEK_SET
);
2294 if (fflush (ce
->ce_fp
)) {
2295 content_error (ce
->ce_file
, ct
, "error writing to");
2299 fseek (ce
->ce_fp
, 0L, SEEK_SET
);
2302 *file
= ce
->ce_file
;
2307 return fileno (ce
->ce_fp
);
2310 free_encoding (ct
, 0);
2324 openExternal (CT ct
, CT cb
, CE ce
, char **file
, int *fd
)
2326 char cachefile
[BUFSIZ
];
2329 fseek (ce
->ce_fp
, 0L, SEEK_SET
);
2334 if ((ce
->ce_fp
= fopen (ce
->ce_file
, "r")) == NULL
) {
2335 content_error (ce
->ce_file
, ct
, "unable to fopen for reading");
2341 if (find_cache(ct
, rcachesw
, NULL
, cb
->c_id
,
2342 cachefile
, sizeof(cachefile
)) != NOTOK
) {
2343 if ((ce
->ce_fp
= fopen (cachefile
, "r"))) {
2344 ce
->ce_file
= mh_xstrdup(cachefile
);
2348 admonish (cachefile
, "unable to fopen for reading");
2351 *fd
= ce
->ce_fp
? fileno (ce
->ce_fp
) : -1;
2355 *file
= ce
->ce_file
;
2356 *fd
= fileno (ce
->ce_fp
);
2367 return init_encoding (ct
, openFile
);
2372 openFile (CT ct
, char **file
)
2375 char cachefile
[BUFSIZ
];
2376 struct exbody
*e
= ct
->c_ctexbody
;
2377 CE ce
= &ct
->c_cefile
;
2379 switch (openExternal (e
->eb_parent
, e
->eb_content
, ce
, file
, &fd
)) {
2391 content_error (NULL
, ct
, "missing name parameter");
2395 ce
->ce_file
= mh_xstrdup(e
->eb_name
);
2398 if ((ce
->ce_fp
= fopen (ce
->ce_file
, "r")) == NULL
) {
2399 content_error (ce
->ce_file
, ct
, "unable to fopen for reading");
2403 if ((!e
->eb_permission
|| strcasecmp (e
->eb_permission
, "read-write"))
2404 && find_cache (NULL
, wcachesw
, &cachetype
, e
->eb_content
->c_id
,
2405 cachefile
, sizeof(cachefile
)) != NOTOK
) {
2409 mask
= umask (cachetype
? ~m_gmprot () : 0222);
2410 if ((fp
= fopen (cachefile
, "w"))) {
2412 char buffer
[BUFSIZ
];
2413 FILE *gp
= ce
->ce_fp
;
2415 fseek (gp
, 0L, SEEK_SET
);
2417 while ((cc
= fread (buffer
, sizeof(*buffer
), sizeof(buffer
), gp
))
2419 if ((int) fwrite (buffer
, sizeof(*buffer
), cc
, fp
) < cc
) {
2420 advise ("openFile", "fwrite");
2425 admonish (ce
->ce_file
, "error reading");
2426 (void) m_unlink (cachefile
);
2427 } else if (ferror (fp
)) {
2428 admonish (cachefile
, "error writing");
2429 (void) m_unlink (cachefile
);
2436 fseek (ce
->ce_fp
, 0L, SEEK_SET
);
2437 *file
= ce
->ce_file
;
2438 return fileno (ce
->ce_fp
);
2448 return init_encoding (ct
, openFTP
);
2453 openFTP (CT ct
, char **file
)
2459 char *bp
, *ftp
, *user
, *pass
;
2460 char buffer
[BUFSIZ
], cachefile
[BUFSIZ
];
2462 CE ce
= &ct
->c_cefile
;
2463 static char *username
= NULL
;
2464 static char *password
= NULL
;
2468 if ((ftp
= context_find (nmhaccessftp
)) && !*ftp
)
2474 switch (openExternal (e
->eb_parent
, e
->eb_content
, ce
, file
, &fd
)) {
2485 if (!e
->eb_name
|| !e
->eb_site
) {
2486 content_error (NULL
, ct
, "missing %s parameter",
2487 e
->eb_name
? "site": "name");
2491 /* Get the buffer ready to go */
2493 buflen
= sizeof(buffer
);
2496 * Construct the query message for user
2498 snprintf (bp
, buflen
, "Retrieve %s", e
->eb_name
);
2504 snprintf (bp
, buflen
, " (content %s)", e
->eb_partno
);
2510 snprintf (bp
, buflen
, "\n using %sFTP from site %s",
2511 e
->eb_flags
? "anonymous " : "", e
->eb_site
);
2516 if (e
->eb_size
> 0) {
2517 snprintf (bp
, buflen
, " (%lu octets)", e
->eb_size
);
2522 snprintf (bp
, buflen
, "? ");
2525 * Now, check the answer
2527 if (!read_yes_or_no_if_tty (buffer
))
2532 snprintf (buffer
, sizeof(buffer
), "%s@%s", getusername (),
2536 ruserpass (e
->eb_site
, &username
, &password
, 0);
2541 ce
->ce_unlink
= (*file
== NULL
);
2543 cachefile
[0] = '\0';
2544 if ((!e
->eb_permission
|| strcasecmp (e
->eb_permission
, "read-write"))
2545 && find_cache (NULL
, wcachesw
, &cachetype
, e
->eb_content
->c_id
,
2546 cachefile
, sizeof(cachefile
)) != NOTOK
) {
2547 if (*file
== NULL
) {
2554 ce
->ce_file
= mh_xstrdup(*file
);
2556 ce
->ce_file
= mh_xstrdup(cachefile
);
2559 if ((tempfile
= m_mktemp2(NULL
, invo_name
, NULL
, NULL
)) == NULL
) {
2560 die("unable to create temporary file in %s",
2563 ce
->ce_file
= mh_xstrdup(tempfile
);
2566 if ((ce
->ce_fp
= fopen (ce
->ce_file
, "w+")) == NULL
) {
2567 content_error (ce
->ce_file
, ct
, "unable to fopen for reading/writing");
2576 vec
[vecp
++] = r1bindex (ftp
, '/');
2577 vec
[vecp
++] = e
->eb_site
;
2580 vec
[vecp
++] = e
->eb_dir
;
2581 vec
[vecp
++] = e
->eb_name
;
2582 vec
[vecp
++] = ce
->ce_file
,
2583 vec
[vecp
++] = e
->eb_mode
&& !strcasecmp (e
->eb_mode
, "ascii")
2584 ? "ascii" : "binary";
2592 adios ("fork", "unable to");
2596 close (fileno (ce
->ce_fp
));
2598 fprintf (stderr
, "unable to exec ");
2604 if (pidXwait (child_id
, NULL
)) {
2605 username
= password
= NULL
;
2615 chmod (cachefile
, cachetype
? m_gmprot () : 0444);
2620 mask
= umask (cachetype
? ~m_gmprot () : 0222);
2621 if ((fp
= fopen (cachefile
, "w"))) {
2623 FILE *gp
= ce
->ce_fp
;
2625 fseek (gp
, 0L, SEEK_SET
);
2627 while ((cc
= fread (buffer
, sizeof(*buffer
), sizeof(buffer
), gp
))
2629 if ((int) fwrite (buffer
, sizeof(*buffer
), cc
, fp
) < cc
) {
2630 advise ("openFTP", "fwrite");
2635 admonish (ce
->ce_file
, "error reading");
2636 (void) m_unlink (cachefile
);
2637 } else if (ferror (fp
)) {
2638 admonish (cachefile
, "error writing");
2639 (void) m_unlink (cachefile
);
2647 fseek (ce
->ce_fp
, 0L, SEEK_SET
);
2648 *file
= ce
->ce_file
;
2649 return fileno (ce
->ce_fp
);
2660 return init_encoding (ct
, openMail
);
2665 openMail (CT ct
, char **file
)
2667 int child_id
, fd
, vecp
;
2669 char *bp
, buffer
[BUFSIZ
], *vec
[7];
2670 struct exbody
*e
= ct
->c_ctexbody
;
2671 CE ce
= &ct
->c_cefile
;
2673 switch (openExternal (e
->eb_parent
, e
->eb_content
, ce
, file
, &fd
)) {
2684 if (!e
->eb_server
) {
2685 content_error (NULL
, ct
, "missing server parameter");
2689 /* Get buffer ready to go */
2691 buflen
= sizeof(buffer
);
2693 /* Now, construct query message */
2694 snprintf (bp
, buflen
, "Retrieve content");
2700 snprintf (bp
, buflen
, " %s", e
->eb_partno
);
2706 snprintf (bp
, buflen
, " by asking %s\n\n%s\n? ",
2708 e
->eb_subject
? e
->eb_subject
: e
->eb_body
);
2710 /* Now, check answer */
2711 if (!read_yes_or_no_if_tty (buffer
))
2715 vec
[vecp
++] = r1bindex (mailproc
, '/');
2716 vec
[vecp
++] = e
->eb_server
;
2717 vec
[vecp
++] = "-subject";
2718 vec
[vecp
++] = e
->eb_subject
? e
->eb_subject
: "mail-server request";
2719 vec
[vecp
++] = "-body";
2720 vec
[vecp
++] = e
->eb_body
;
2726 advise ("fork", "unable to");
2730 execvp (mailproc
, vec
);
2731 fprintf (stderr
, "unable to exec ");
2737 if (pidXwait (child_id
, NULL
) == OK
)
2738 inform("request sent");
2742 if (*file
== NULL
) {
2744 if ((tempfile
= m_mktemp2(NULL
, invo_name
, NULL
, NULL
)) == NULL
) {
2745 die("unable to create temporary file in %s",
2748 ce
->ce_file
= mh_xstrdup(tempfile
);
2751 ce
->ce_file
= mh_xstrdup(*file
);
2755 if ((ce
->ce_fp
= fopen (ce
->ce_file
, "w+")) == NULL
) {
2756 content_error (ce
->ce_file
, ct
, "unable to fopen for reading/writing");
2760 /* showproc is for mhshow and mhstore, though mhlist -debug
2761 * prints it, too. */
2762 free(ct
->c_showproc
);
2763 ct
->c_showproc
= mh_xstrdup("true");
2765 fseek (ce
->ce_fp
, 0L, SEEK_SET
);
2766 *file
= ce
->ce_file
;
2767 return fileno (ce
->ce_fp
);
2778 return init_encoding (ct
, openURL
);
2783 openURL (CT ct
, char **file
)
2785 struct exbody
*e
= ct
->c_ctexbody
;
2786 CE ce
= &ct
->c_cefile
;
2787 char *urlprog
, *program
;
2788 char buffer
[BUFSIZ
], cachefile
[BUFSIZ
];
2792 struct msgs_array args
= { 0, 0, NULL
};
2795 if ((urlprog
= context_find(nmhaccessurl
)) && *urlprog
== '\0')
2799 content_error(NULL
, ct
, "No entry for nmh-access-url in profile");
2803 switch (openExternal(e
->eb_parent
, e
->eb_content
, ce
, file
, &fd
)) {
2815 content_error(NULL
, ct
, "missing url parameter");
2819 ce
->ce_unlink
= (*file
== NULL
);
2821 cachefile
[0] = '\0';
2823 if (find_cache(NULL
, wcachesw
, &cachetype
, e
->eb_content
->c_id
,
2824 cachefile
, sizeof(cachefile
)) != NOTOK
) {
2825 if (*file
== NULL
) {
2832 ce
->ce_file
= mh_xstrdup(*file
);
2834 ce
->ce_file
= mh_xstrdup(cachefile
);
2837 if ((tempfile
= m_mktemp2(NULL
, invo_name
, NULL
, NULL
)) == NULL
) {
2838 die("unable to create temporary file in %s",
2841 ce
->ce_file
= mh_xstrdup(tempfile
);
2844 if ((ce
->ce_fp
= fopen(ce
->ce_file
, "w+")) == NULL
) {
2845 content_error(ce
->ce_file
, ct
, "unable to fopen for read/writing");
2849 switch (child_id
= fork()) {
2851 adios ("fork", "unable to");
2855 argsplit_msgarg(&args
, urlprog
, &program
);
2856 app_msgarg(&args
, e
->eb_url
);
2857 app_msgarg(&args
, NULL
);
2858 dup2(fileno(ce
->ce_fp
), 1);
2859 close(fileno(ce
->ce_fp
));
2860 execvp(program
, args
.msgs
);
2861 fprintf(stderr
, "Unable to exec ");
2867 if (pidXwait(child_id
, NULL
)) {
2875 chmod(cachefile
, cachetype
? m_gmprot() : 0444);
2880 mask
= umask (cachetype
? ~m_gmprot() : 0222);
2881 if ((fp
= fopen(cachefile
, "w"))) {
2883 FILE *gp
= ce
->ce_fp
;
2885 fseeko(gp
, 0, SEEK_SET
);
2887 while ((cc
= fread(buffer
, sizeof(*buffer
),
2888 sizeof(buffer
), gp
)) > 0)
2889 if ((int) fwrite(buffer
, sizeof(*buffer
), cc
, fp
) < cc
) {
2890 advise ("openURL", "fwrite");
2896 admonish(ce
->ce_file
, "error reading");
2897 (void) m_unlink (cachefile
);
2904 fseeko(ce
->ce_fp
, 0, SEEK_SET
);
2905 *file
= ce
->ce_file
;
2906 return fileno(ce
->ce_fp
);
2911 * Stores MD5 digest (in cp, from Content-MD5 header) in ct->c_digest. It
2912 * has to be base64 decoded.
2915 readDigest (CT ct
, char *cp
)
2917 unsigned char *digest
;
2920 if (decodeBase64 (cp
, &digest
, &len
, 0, NULL
) == OK
) {
2921 const size_t maxlen
= sizeof ct
->c_digest
;
2923 if (strlen ((char *) digest
) <= maxlen
) {
2924 memcpy (ct
->c_digest
, digest
, maxlen
);
2929 fprintf (stderr
, "MD5 digest=");
2930 for (i
= 0; i
< maxlen
; ++i
) {
2931 fprintf (stderr
, "%02x", ct
->c_digest
[i
] & 0xff);
2933 fprintf (stderr
, "\n");
2939 fprintf (stderr
, "invalid MD5 digest (got %d octets)\n",
2940 (int) strlen ((char *) digest
));
2950 /* Multipart parts might have content before the first subpart and/or
2951 after the last subpart that hasn't been stored anywhere else, so do
2954 get_leftover_mp_content (CT ct
, int before
/* or after */)
2956 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
2958 bool found_boundary
= false;
2964 char *content
= NULL
;
2966 if (! m
) return NOTOK
;
2969 if (! m
->mp_parts
|| ! m
->mp_parts
->mp_part
) return NOTOK
;
2971 /* Isolate the beginning of this part to the beginning of the
2972 first subpart and save any content between them. */
2973 fseeko (ct
->c_fp
, ct
->c_begin
, SEEK_SET
);
2974 max
= m
->mp_parts
->mp_part
->c_begin
- ct
->c_begin
;
2975 boundary
= concat ("--", m
->mp_start
, NULL
);
2977 struct part
*last_subpart
= NULL
;
2978 struct part
*subpart
;
2980 /* Go to the last subpart to get its end position. */
2981 for (subpart
= m
->mp_parts
; subpart
; subpart
= subpart
->mp_next
) {
2982 last_subpart
= subpart
;
2985 if (last_subpart
== NULL
) return NOTOK
;
2987 /* Isolate the end of the last subpart to the end of this part
2988 and save any content between them. */
2989 fseeko (ct
->c_fp
, last_subpart
->mp_part
->c_end
, SEEK_SET
);
2990 max
= ct
->c_end
- last_subpart
->mp_part
->c_end
;
2991 boundary
= concat ("--", m
->mp_stop
, NULL
);
2994 /* Back up by 1 to pick up the newline. */
2995 while ((gotlen
= getline(&bufp
, &buflen
, ct
->c_fp
)) != -1) {
2997 /* Don't look beyond beginning of first subpart (before) or
2998 next part (after). */
2999 if (read
> max
) bufp
[read
-max
] = '\0';
3002 if (! strcmp (bufp
, boundary
)) {
3003 found_boundary
= true;
3006 if (! found_boundary
&& ! strcmp (bufp
, boundary
)) {
3007 found_boundary
= true;
3012 if ((before
&& ! found_boundary
) || (! before
&& found_boundary
)) {
3014 char *old_content
= content
;
3015 content
= concat (content
, bufp
, NULL
);
3019 ? concat ("\n", bufp
, NULL
)
3020 : concat (bufp
, NULL
);
3025 if (found_boundary
|| read
> max
) break;
3027 if (read
> max
) break;
3031 /* Skip the newline if that's all there is. */
3035 /* Remove trailing newline, except at EOF. */
3036 if ((before
|| ! feof (ct
->c_fp
)) &&
3037 (cp
= content
+ strlen (content
)) > content
&&
3042 if (strlen (content
) > 1) {
3044 m
->mp_content_before
= content
;
3046 m
->mp_content_after
= content
;
3061 ct_type_str (int type
)
3064 case CT_APPLICATION
:
3065 return "application";
3081 return "unknown_type";
3087 ct_subtype_str (int type
, int subtype
)
3090 case CT_APPLICATION
:
3092 case APPLICATION_OCTETS
:
3094 case APPLICATION_POSTSCRIPT
:
3095 return "postscript";
3097 return "unknown_app_subtype";
3101 case MESSAGE_RFC822
:
3103 case MESSAGE_PARTIAL
:
3105 case MESSAGE_EXTERNAL
:
3108 return "unknown_msg_subtype";
3114 case MULTI_ALTERNATE
:
3115 return "alternative";
3118 case MULTI_PARALLEL
:
3123 return "unknown_multipart_subtype";
3134 return "unknown_text_subtype";
3137 return "unknown_type";
3143 ct_str_type (const char *type
)
3145 struct str2init
*s2i
;
3147 for (s2i
= str2cts
; s2i
->si_key
; ++s2i
) {
3148 if (! strcasecmp (type
, s2i
->si_key
)) {
3152 if (! s2i
->si_key
&& ! uprf (type
, "X-")) {
3161 ct_str_subtype (int type
, const char *subtype
)
3166 case CT_APPLICATION
:
3167 for (kv
= SubApplication
; kv
->kv_key
; ++kv
) {
3168 if (! strcasecmp (subtype
, kv
->kv_key
)) {
3172 return kv
->kv_value
;
3174 for (kv
= SubMessage
; kv
->kv_key
; ++kv
) {
3175 if (! strcasecmp (subtype
, kv
->kv_key
)) {
3179 return kv
->kv_value
;
3181 for (kv
= SubMultiPart
; kv
->kv_key
; ++kv
) {
3182 if (! strcasecmp (subtype
, kv
->kv_key
)) {
3186 return kv
->kv_value
;
3188 for (kv
= SubText
; kv
->kv_key
; ++kv
) {
3189 if (! strcasecmp (subtype
, kv
->kv_key
)) {
3193 return kv
->kv_value
;
3200 /* Find the content type and InitFunc for the CT. */
3201 const struct str2init
*
3202 get_ct_init (int type
)
3204 const struct str2init
*sp
;
3206 for (sp
= str2cts
; sp
->si_key
; ++sp
) {
3207 if (type
== sp
->si_val
) {
3216 ce_str (int encoding
)
3222 return "quoted-printable";
3238 /* Find the content type and InitFunc for the content encoding method. */
3239 const struct str2init
*
3240 get_ce_method (const char *method
)
3242 struct str2init
*sp
;
3244 for (sp
= str2ces
; sp
->si_key
; ++sp
) {
3245 if (! strcasecmp (method
, sp
->si_key
)) {
3254 * Parse a series of MIME attributes (or parameters) given a header as
3257 * Arguments include:
3259 * filename - Name of input file (for error messages)
3260 * fieldname - Name of field being processed
3261 * headerp - Pointer to pointer of the beginning of the MIME attributes.
3262 * Updated to point to end of attributes when finished.
3263 * param_head - Pointer to head of parameter list
3264 * param_tail - Pointer to tail of parameter list
3265 * commentp - Pointer to header comment pointer (may be NULL)
3267 * Returns OK if parsing was successful, NOTOK if parsing failed, and
3268 * DONE to indicate a benign error (minor parsing error, but the program
3273 parse_header_attrs (const char *filename
, const char *fieldname
,
3274 char **header_attrp
, PM
*param_head
, PM
*param_tail
,
3277 char *cp
= *header_attrp
;
3283 struct sectlist
*next
;
3289 struct sectlist
*sechead
;
3290 struct parmlist
*next
;
3291 } *pp
, *pp2
, *phead
= NULL
;
3293 while (*cp
== ';') {
3294 char *dp
, *vp
, *up
, *nameptr
, *valptr
, *charset
= NULL
, *lang
= NULL
;
3295 bool encoded
= false;
3296 bool partial
= false;
3297 int len
= 0, index
= 0;
3300 while (isspace ((unsigned char) *cp
))
3304 get_comment (filename
, fieldname
, &cp
, commentp
) == NOTOK
) {
3309 if (! suppress_extraneous_trailing_semicolon_warning
) {
3310 inform("extraneous trailing ';' in message %s's %s: "
3311 "parameter list", filename
, fieldname
);
3316 /* down case the attribute name */
3317 for (dp
= cp
; istoken ((unsigned char) *dp
); dp
++)
3318 *dp
= tolower ((unsigned char) *dp
);
3320 for (up
= dp
; isspace ((unsigned char) *dp
);)
3322 if (dp
== cp
|| *dp
!= '=') {
3323 inform("invalid parameter in message %s's %s: field\n"
3324 " parameter %s (error detected at offset %ld)",
3325 filename
, fieldname
, cp
, (long)(dp
- cp
));
3330 * To handle RFC 2231, we have to deal with the following extensions:
3332 * name*=encoded-value
3333 * name*<N>=part-N-of-a-parameter-value
3334 * name*<N>*=encoded-part-N-of-a-parameter-value
3337 * If there's a * right before the equal sign, it's encoded.
3338 * If there's a * and one or more digits, then it's section N.
3340 * Remember we can have one or the other, or both. cp points to
3341 * beginning of name, up points past the last character in the
3345 for (vp
= cp
; vp
< up
; vp
++) {
3346 if (*vp
== '*' && vp
< up
- 1) {
3350 if (*vp
== '*' && vp
== up
- 1) {
3352 } else if (partial
) {
3353 if (isdigit((unsigned char) *vp
))
3354 index
= *vp
- '0' + index
* 10;
3356 inform("invalid parameter index in message %s's %s: field"
3357 "\n (parameter %s)", filename
, fieldname
, cp
);
3366 * Break out the parameter name and value sections and allocate
3370 nameptr
= mh_xmalloc(len
+ 1);
3371 strncpy(nameptr
, cp
, len
);
3372 nameptr
[len
] = '\0';
3374 for (dp
++; isspace ((unsigned char) *dp
);)
3379 * Single quotes delimit the character set and language tag.
3380 * They are required on the first section (or a complete
3385 while (*vp
!= '\'' && !isspace((unsigned char) *vp
) &&
3391 charset
= mh_xmalloc(len
+ 1);
3392 strncpy(charset
, dp
, len
);
3393 charset
[len
] = '\0';
3399 inform("missing charset in message %s's %s: field\n"
3400 " (parameter %s)", filename
, fieldname
, nameptr
);
3406 while (*vp
!= '\'' && !isspace((unsigned char) *vp
) &&
3413 lang
= mh_xmalloc(len
+ 1);
3414 strncpy(lang
, dp
, len
);
3421 inform("missing language tag in message %s's %s: field\n"
3422 " (parameter %s)", filename
, fieldname
, nameptr
);
3432 * At this point vp should be pointing at the beginning
3433 * of the encoded value/section. Continue until we reach
3434 * the end or get whitespace. But first, calculate the
3435 * length so we can allocate the correct buffer size.
3438 for (vp
= dp
, len
= 0; istoken(*vp
); vp
++) {
3440 if (*(vp
+ 1) == '\0' ||
3441 !isxdigit((unsigned char) *(vp
+ 1)) ||
3442 *(vp
+ 2) == '\0' ||
3443 !isxdigit((unsigned char) *(vp
+ 2))) {
3444 inform("invalid encoded sequence in message %s's %s: field\n"
3445 " (parameter %s)", filename
, fieldname
, nameptr
);
3456 up
= valptr
= mh_xmalloc(len
+ 1);
3458 for (vp
= dp
; istoken(*vp
); vp
++) {
3460 *up
++ = decode_qp(*(vp
+ 1), *(vp
+ 2));
3471 * A "normal" string. If it's got a leading quote, then we
3472 * strip the quotes out. Otherwise go until we reach the end
3473 * or get whitespace. Note we scan it twice; once to get the
3474 * length, then the second time copies it into the destination
3481 for (cp
= dp
+ 1;;) {
3485 inform("invalid quoted-string in message %s's %s: field\n"
3486 " (parameter %s)", filename
, fieldname
, nameptr
);
3506 for (cp
= dp
; istoken (*cp
); cp
++) {
3511 valptr
= mh_xmalloc(len
+ 1);
3515 for (cp
= dp
+ 1, vp
= valptr
, i
= 0; i
< len
; i
++) {
3523 strncpy(valptr
, cp
= dp
, len
);
3531 * If 'partial' is set, we don't allocate a parameter now. We
3532 * put it on the parameter linked list to be reassembled later.
3534 * "phead" points to a list of all parameters we need to reassemble.
3535 * Each parameter has a list of sections. We insert the sections in
3540 for (pp
= phead
; pp
!= NULL
; pp
= pp
->next
) {
3541 if (strcasecmp(nameptr
, pp
->name
) == 0) {
3556 * Insert this into the section linked list
3564 if (pp
->sechead
== NULL
|| pp
->sechead
->index
> index
) {
3565 sp
->next
= pp
->sechead
;
3568 for (sp2
= pp
->sechead
; sp2
!= NULL
; sp2
= sp2
->next
) {
3569 if (sp2
->index
== sp
->index
) {
3570 inform("duplicate index (%d) in message %s's %s: field"
3571 "\n (parameter %s)", sp
->index
, filename
,
3572 fieldname
, nameptr
);
3575 if (sp2
->index
< sp
->index
&&
3576 (sp2
->next
== NULL
|| sp2
->next
->index
> sp
->index
)) {
3577 sp
->next
= sp2
->next
;
3584 inform("Internal error: cannot insert partial param "
3585 "in message %s's %s: field\n (parameter %s)",
3586 filename
, fieldname
, nameptr
);
3592 * Save our charset and lang tags.
3595 if (index
== 0 && encoded
) {
3597 pp
->charset
= charset
;
3602 pm
= add_param(param_head
, param_tail
, nameptr
, valptr
, 1);
3603 pm
->pm_charset
= charset
;
3607 while (isspace ((unsigned char) *cp
))
3611 get_comment (filename
, fieldname
, &cp
, commentp
) == NOTOK
) {
3617 * Now that we're done, reassemble all of the partial parameters.
3620 for (pp
= phead
; pp
!= NULL
; ) {
3624 for (sp
= pp
->sechead
; sp
!= NULL
; sp
= sp
->next
) {
3625 if (sp
->index
!= pindex
++) {
3626 inform("missing section %d for parameter in message "
3627 "%s's %s: field\n (parameter %s)", pindex
- 1,
3628 filename
, fieldname
, pp
->name
);
3634 p
= q
= mh_xmalloc(tlen
+ 1);
3635 for (sp
= pp
->sechead
; sp
!= NULL
; ) {
3636 memcpy(q
, sp
->value
, sp
->len
);
3646 pm
= add_param(param_head
, param_tail
, pp
->name
, p
, 1);
3647 pm
->pm_charset
= pp
->charset
;
3648 pm
->pm_lang
= pp
->lang
;
3659 * Return the charset for a particular content type.
3663 content_charset (CT ct
)
3665 char *ret_charset
= NULL
;
3667 ret_charset
= get_param(ct
->c_ctinfo
.ci_first_pm
, "charset", '?', 0);
3669 return ret_charset
? ret_charset
: mh_xstrdup("US-ASCII");
3674 * Create a string based on a list of output parameters. Assume that this
3675 * parameter string will be appended to an existing header, so start out
3676 * with the separator (;). Perform RFC 2231 encoding when necessary.
3680 output_params(size_t initialwidth
, PM params
, int *offsetout
, int external
)
3682 char *paramout
= NULL
;
3683 char line
[CPERLIN
* 2], *q
;
3684 int curlen
, index
, cont
, encode
, i
;
3685 size_t valoff
, numchars
;
3687 while (params
!= NULL
) {
3693 if (external
&& strcasecmp(params
->pm_name
, "body") == 0)
3696 if (strlen(params
->pm_name
) > CPERLIN
) {
3697 inform("Parameter name \"%s\" is too long", params
->pm_name
);
3702 curlen
= param_len(params
, index
, valoff
, &encode
, &cont
, &numchars
);
3705 * Loop until we get a parameter that fits within a line. We
3706 * assume new lines start with a tab, so check our overflow based
3716 * At this point we're definitely continuing the line, so
3717 * be sure to include the parameter name and section index.
3720 q
+= snprintf(q
, sizeof(line
) - (q
- line
), "%s*%d",
3721 params
->pm_name
, index
);
3724 * Both of these functions do a NUL termination
3728 i
= encode_param(params
, q
, sizeof(line
) - (q
- line
),
3729 numchars
, valoff
, index
);
3731 i
= normal_param(params
, q
, sizeof(line
) - (q
- line
),
3741 curlen
= param_len(params
, index
, valoff
, &encode
, &cont
,
3746 * "line" starts with a ;\n\t, so that doesn't count against
3747 * the length. But add 8 since it starts with a tab; that's
3748 * how we end up with 5.
3751 initialwidth
= strlen(line
) + 5;
3754 * At this point the line should be built, so add it to our
3755 * current output buffer.
3758 paramout
= add(line
, paramout
);
3762 * If this won't fit on the line, start a new one. Save room in
3763 * case we need a semicolon on the end
3766 if (initialwidth
+ curlen
> CPERLIN
- 1) {
3778 * At this point, we're either finishing a continued parameter, or
3779 * we're working on a new one.
3783 q
+= snprintf(q
, sizeof(line
) - (q
- line
), "%s*%d",
3784 params
->pm_name
, index
);
3786 strncpy(q
, params
->pm_name
, sizeof(line
) - (q
- line
));
3791 i
= encode_param(params
, q
, sizeof(line
) - (q
- line
),
3792 strlen(params
->pm_value
+ valoff
), valoff
, index
);
3794 i
= normal_param(params
, q
, sizeof(line
) - (q
- line
),
3795 strlen(params
->pm_value
+ valoff
), valoff
);
3802 paramout
= add(line
, paramout
);
3803 initialwidth
+= strlen(line
);
3805 params
= params
->pm_next
;
3809 *offsetout
= initialwidth
;
3815 * Calculate the size of a parameter.
3819 * pm - The parameter being output
3820 * index - If continuing the parameter, the index of the section
3822 * valueoff - The current offset into the parameter value that we're
3823 * working on (previous sections have consumed valueoff bytes).
3824 * encode - Set if we should perform encoding on this parameter section
3825 * (given that we're consuming bytesfit bytes).
3826 * cont - Set if the remaining data in value will not fit on a single
3827 * line and will need to be continued.
3828 * bytesfit - The number of bytes that we can consume from the parameter
3829 * value and still fit on a completely new line. The
3830 * calculation assumes the new line starts with a tab,
3831 * includes the parameter name and any encoding, and fits
3832 * within CPERLIN bytes. Will always be at least 1.
3836 param_len(PM pm
, int index
, size_t valueoff
, int *encode
, int *cont
,
3839 char *start
= pm
->pm_value
+ valueoff
, *p
, indexchar
[32];
3840 size_t len
= 0, fit
= 0;
3841 int fitlimit
= 0, eightbit
, maxfit
;
3846 * Add up the length. First, start with the parameter name.
3849 len
= strlen(pm
->pm_name
);
3852 * Scan the parameter value and see if we need to do encoding for this
3856 eightbit
= contains8bit(start
, NULL
);
3859 * Determine if we need to encode this section. Encoding is necessary if:
3861 * - There are any 8-bit characters at all and we're on the first
3863 * - There are 8-bit characters within N bytes of our section start.
3864 * N is calculated based on the number of bytes it would take to
3865 * reach CPERLIN. Specifically:
3866 * 8 (starting tab) +
3867 * strlen(param name) +
3868 * 4 ('* for section marker, '=', opening/closing '"')
3870 * is the number of bytes used by everything that isn't part of the
3871 * value. So that gets subtracted from CPERLIN.
3874 snprintf(indexchar
, sizeof(indexchar
), "%d", index
);
3875 maxfit
= CPERLIN
- (12 + len
+ strlen(indexchar
));
3876 if ((eightbit
&& index
== 0) || contains8bit(start
, start
+ maxfit
)) {
3880 len
++; /* Add in equal sign */
3884 * We're using maxfit as a marker for how many characters we can
3885 * fit into the line. Bump it by two because we're not using quotes
3892 * If we don't have a charset or language tag in this parameter,
3896 if (! pm
->pm_charset
) {
3897 pm
->pm_charset
= mh_xstrdup(write_charset_8bit());
3898 if (strcasecmp(pm
->pm_charset
, "US-ASCII") == 0)
3899 die("8-bit characters in parameter \"%s\", but "
3900 "local character set is US-ASCII", pm
->pm_name
);
3903 pm
->pm_lang
= mh_xstrdup(""); /* Default to a blank lang tag */
3905 len
++; /* For the encoding marker */
3908 int enclen
= strlen(pm
->pm_charset
) + strlen(pm
->pm_lang
) + 2;
3913 * We know we definitely need to include an index. maxfit already
3914 * includes the section marker.
3916 len
+= strlen(indexchar
);
3918 for (p
= start
; *p
!= '\0'; p
++) {
3919 if (isparamencode(*p
)) {
3927 * Just so there's no confusion: maxfit is counting OUTPUT
3928 * characters (post-encoding). fit is counting INPUT characters.
3930 if (! fitlimit
&& maxfit
>= 0)
3932 else if (! fitlimit
)
3937 * Calculate the string length, but add room for quoting \
3938 * and " if necessary. Also account for quotes at beginning
3941 for (p
= start
; *p
!= '\0'; p
++) {
3952 if (! fitlimit
&& maxfit
>= 0)
3954 else if (! fitlimit
)
3971 * Output an encoded parameter string.
3975 encode_param(PM pm
, char *output
, size_t len
, size_t valuelen
,
3976 size_t valueoff
, int index
)
3978 size_t outlen
= 0, n
;
3979 char *endptr
= output
+ len
, *p
;
3982 * First, output the marker for an encoded string.
3990 * If the index is 0, output the character set and language tag.
3991 * If theses were NULL, they should have already been filled in
3996 n
= snprintf(output
, len
- outlen
, "%s'%s'", pm
->pm_charset
,
4000 if (output
> endptr
) {
4001 inform("Internal error: parameter buffer overflow");
4007 * Copy over the value, encoding if necessary
4010 p
= pm
->pm_value
+ valueoff
;
4011 while (valuelen
-- > 0) {
4012 if (isparamencode(*p
)) {
4013 n
= snprintf(output
, len
- outlen
, "%%%02X", (unsigned char) *p
++);
4020 if (output
> endptr
) {
4021 inform("Internal error: parameter buffer overflow");
4032 * Output a "normal" parameter, without encoding. Be sure to escape
4033 * quotes and backslashes if necessary.
4037 normal_param(PM pm
, char *output
, size_t len
, size_t valuelen
,
4041 char *endptr
= output
+ len
, *p
;
4047 p
= pm
->pm_value
+ valueoff
;
4049 while (valuelen
-- > 0) {
4060 if (output
> endptr
) {
4061 inform("Internal error: parameter buffer overflow");
4066 if (output
- 2 > endptr
) {
4067 inform("Internal error: parameter buffer overflow");
4078 * Add a parameter to the parameter linked list
4082 add_param(PM
*first
, PM
*last
, char *name
, char *value
, int nocopy
)
4087 pm
->pm_name
= nocopy
? name
: getcpy(name
);
4088 pm
->pm_value
= nocopy
? value
: getcpy(value
);
4091 (*last
)->pm_next
= pm
;
4102 * Either replace a current parameter with a new value, or add the parameter
4103 * to the parameter linked list.
4107 replace_param(PM
*first
, PM
*last
, char *name
, char *value
, int nocopy
)
4111 for (pm
= *first
; pm
!= NULL
; pm
= pm
->pm_next
) {
4112 if (strcasecmp(name
, pm
->pm_name
) == 0) {
4114 * If nocopy is set, it's assumed that we own both name
4115 * and value. We don't need name, so we discard it now.
4120 pm
->pm_value
= nocopy
? value
: getcpy(value
);
4125 return add_param(first
, last
, name
, value
, nocopy
);
4129 * Retrieve a parameter value from a parameter linked list. If the parameter
4130 * value needs converted to the local character set, do that now.
4134 get_param(PM first
, const char *name
, char replace
, int fetchonly
)
4136 while (first
!= NULL
) {
4137 if (strcasecmp(name
, first
->pm_name
) == 0) {
4139 return first
->pm_value
;
4140 return getcpy(get_param_value(first
, replace
));
4142 first
= first
->pm_next
;
4149 * Return a parameter value, converting to the local character set if
4154 get_param_value(PM pm
, char replace
)
4156 static char buffer
[4096]; /* I hope no parameters are larger */
4157 size_t bufsize
= sizeof(buffer
);
4162 ICONV_CONST
char *p
;
4163 #else /* HAVE_ICONV */
4165 #endif /* HAVE_ICONV */
4170 * If we don't have a character set indicated, it's assumed to be
4171 * US-ASCII. If it matches our character set, we don't need to convert
4175 if (!pm
->pm_charset
|| check_charset(pm
->pm_charset
,
4176 strlen(pm
->pm_charset
))) {
4177 return pm
->pm_value
;
4181 * In this case, we need to convert. If we have iconv support, use
4182 * that. Otherwise, go through and simply replace every non-ASCII
4183 * character with the substitution character.
4188 bufsize
= sizeof(buffer
);
4189 utf8
= strcasecmp(pm
->pm_charset
, "UTF-8") == 0;
4191 cd
= iconv_open(get_charset(), pm
->pm_charset
);
4192 if (cd
== (iconv_t
) -1) {
4196 inbytes
= strlen(pm
->pm_value
);
4200 if (iconv(cd
, &p
, &inbytes
, &q
, &bufsize
) == (size_t)-1) {
4201 if (errno
!= EILSEQ
) {
4206 * Reset shift state, substitute our character,
4207 * try to restart conversion.
4210 iconv(cd
, NULL
, NULL
, &q
, &bufsize
);
4223 for (++p
, --inbytes
;
4224 inbytes
> 0 && (((unsigned char) *p
) & 0xc0) == 0x80;
4243 #endif /* HAVE_ICONV */
4246 * Take everything non-ASCII and substitute the replacement character
4250 bufsize
= sizeof(buffer
);
4251 for (p
= pm
->pm_value
; *p
!= '\0' && bufsize
> 1; p
++, q
++, bufsize
--) {
4252 if (isascii((unsigned char) *p
) && isprint((unsigned char) *p
))