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/read_yes_or_no_if_tty.h"
10 #include "sbr/concat.h"
11 #include "sbr/r1bindex.h"
12 #include "sbr/ruserpass.h"
13 #include "sbr/fmt_rfc2047.h"
15 #include "sbr/check_charset.h"
16 #include "sbr/getcpy.h"
17 #include "sbr/context_find.h"
18 #include "sbr/pidstatus.h"
19 #include "sbr/arglist.h"
20 #include "sbr/error.h"
26 #include "h/mhparse.h"
29 #include "h/mhcachesbr.h"
30 #include "sbr/m_mktemp.h"
34 #endif /* HAVE_ICONV */
35 #include "sbr/base64.h"
40 int checksw
= 0; /* check Content-MD5 field */
43 * These are for mhfixmsg to:
44 * 1) Instruct parser not to detect invalid Content-Transfer-Encoding
46 * 2) Suppress the warning about bogus multipart content, and report it.
47 * 3) Suppress the warning about extraneous trailing ';' in header parameter
50 bool skip_mp_cte_check
;
51 bool suppress_bogus_mp_content_warning
;
52 bool bogus_mp_content
;
53 bool suppress_extraneous_trailing_semicolon_warning
;
56 * By default, suppress warning about multiple MIME-Version header fields.
58 bool suppress_multiple_mime_version_warning
= true;
60 /* list of preferred type/subtype pairs, for -prefer */
61 mime_type_subtype mime_preference
[NPREFS
];
66 * Structures for TEXT messages
68 struct k2v SubText
[] = {
69 { "plain", TEXT_PLAIN
},
70 { "richtext", TEXT_RICHTEXT
}, /* defined in RFC 1341 */
71 { "enriched", TEXT_ENRICHED
}, /* defined in RFC 1896 */
72 { NULL
, TEXT_UNKNOWN
} /* this one must be last! */
75 /* Charset[] removed -- yozo. Mon Oct 8 01:03:41 JST 2012 */
78 * Structures for MULTIPART messages
80 struct k2v SubMultiPart
[] = {
81 { "mixed", MULTI_MIXED
},
82 { "alternative", MULTI_ALTERNATE
},
83 { "digest", MULTI_DIGEST
},
84 { "parallel", MULTI_PARALLEL
},
85 { "related", MULTI_RELATED
},
86 { NULL
, MULTI_UNKNOWN
} /* this one must be last! */
90 * Structures for MESSAGE messages
92 struct k2v SubMessage
[] = {
93 { "rfc822", MESSAGE_RFC822
},
94 { "partial", MESSAGE_PARTIAL
},
95 { "external-body", MESSAGE_EXTERNAL
},
96 { NULL
, MESSAGE_UNKNOWN
} /* this one must be last! */
100 * Structure for APPLICATION messages
102 struct k2v SubApplication
[] = {
103 { "octet-stream", APPLICATION_OCTETS
},
104 { "postscript", APPLICATION_POSTSCRIPT
},
105 { NULL
, APPLICATION_UNKNOWN
} /* this one must be last! */
109 * Mapping of names of CTE types in mhbuild directives
111 static struct k2v EncodingType
[] = {
114 { "q-p", CE_QUOTED
},
115 { "quoted-printable", CE_QUOTED
},
116 { "b64", CE_BASE64
},
117 { "base64", CE_BASE64
},
125 static CT
get_content (FILE *, char *, int);
126 static int get_comment (const char *, const char *, char **, char **);
128 static int InitGeneric (CT
);
129 static int InitText (CT
);
130 static int InitMultiPart (CT
);
131 static void reverse_parts (CT
);
132 static void prefer_parts(CT ct
);
133 static int InitMessage (CT
);
134 static int InitApplication (CT
);
135 static int init_encoding (CT
, OpenCEFunc
);
136 static unsigned long size_encoding (CT
);
137 static int InitBase64 (CT
);
138 static int openBase64 (CT
, char **);
139 static int InitQuoted (CT
);
140 static int openQuoted (CT
, char **);
141 static int Init7Bit (CT
);
142 static int openExternal (CT
, CT
, CE
, char **, int *);
143 static int InitFile (CT
);
144 static int openFile (CT
, char **);
145 static int InitFTP (CT
);
146 static int openFTP (CT
, char **);
147 static int InitMail (CT
);
148 static int openMail (CT
, char **);
149 static int readDigest (CT
, char *);
150 static int get_leftover_mp_content (CT
, int);
151 static int InitURL (CT
);
152 static int openURL (CT
, char **);
153 static int parse_header_attrs (const char *, const char *, char **, PM
*,
155 static size_t param_len(PM
, int, size_t, int *, int *, size_t *);
156 static size_t normal_param(PM
, char *, size_t, size_t, size_t);
157 static int get_dispo (char *, CT
, int);
159 struct str2init str2cts
[] = {
160 { "application", CT_APPLICATION
, InitApplication
},
161 { "audio", CT_AUDIO
, InitGeneric
},
162 { "image", CT_IMAGE
, InitGeneric
},
163 { "message", CT_MESSAGE
, InitMessage
},
164 { "multipart", CT_MULTIPART
, InitMultiPart
},
165 { "text", CT_TEXT
, InitText
},
166 { "video", CT_VIDEO
, InitGeneric
},
167 { NULL
, CT_EXTENSION
, NULL
}, /* these two must be last! */
168 { NULL
, CT_UNKNOWN
, NULL
},
171 struct str2init str2ces
[] = {
172 { "base64", CE_BASE64
, InitBase64
},
173 { "quoted-printable", CE_QUOTED
, InitQuoted
},
174 { "8bit", CE_8BIT
, Init7Bit
},
175 { "7bit", CE_7BIT
, Init7Bit
},
176 { "binary", CE_BINARY
, Init7Bit
},
177 { NULL
, CE_EXTENSION
, NULL
}, /* these two must be last! */
178 { NULL
, CE_UNKNOWN
, NULL
},
182 * NOTE WELL: si_key MUST NOT have value of NOTOK
184 * si_val is 1 if access method is anonymous.
186 struct str2init str2methods
[] = {
187 { "afs", 1, InitFile
},
188 { "anon-ftp", 1, InitFTP
},
189 { "ftp", 0, InitFTP
},
190 { "local-file", 0, InitFile
},
191 { "mail-server", 0, InitMail
},
192 { "url", 0, InitURL
},
198 * Main entry point for parsing a MIME message or file.
199 * It returns the Content structure for the top level
200 * entity in the file.
204 parse_mime (char *file
)
213 bogus_mp_content
= false;
216 * Check if file is actually standard input
218 if ((is_stdin
= !(strcmp (file
, "-")))) {
219 char *tfile
= m_mktemp2(NULL
, invo_name
, NULL
, &fp
);
221 advise("mhparse", "unable to create temporary file in %s",
225 file
= mh_xstrdup(tfile
);
227 while ((n
= fread(buffer
, 1, sizeof(buffer
), stdin
)) > 0) {
228 if (fwrite(buffer
, 1, n
, fp
) != n
) {
229 (void) m_unlink (file
);
230 advise (file
, "error copying to temporary file");
236 if (ferror (stdin
)) {
237 (void) m_unlink (file
);
238 advise ("stdin", "error reading");
242 (void) m_unlink (file
);
243 advise (file
, "error writing");
246 fseek (fp
, 0L, SEEK_SET
);
247 } else if (stat (file
, &statbuf
) == NOTOK
) {
248 advise (file
, "unable to stat");
250 } else if (S_ISDIR(statbuf
.st_mode
)) {
251 /* Don't try to parse a directory. */
252 inform("%s is a directory", file
);
254 } else if ((fp
= fopen (file
, "r")) == NULL
) {
255 advise (file
, "unable to read");
259 if (!(ct
= get_content (fp
, file
, 1))) {
261 (void) m_unlink (file
);
262 inform("unable to decode %s", file
);
267 ct
->c_unlink
= 1; /* temp file to remove */
271 if (ct
->c_end
== 0L) {
272 fseek (fp
, 0L, SEEK_END
);
273 ct
->c_end
= ftell (fp
);
276 if (ct
->c_ctinitfnx
&& (*ct
->c_ctinitfnx
) (ct
) == NOTOK
) {
288 * Main routine for reading/parsing the headers
289 * of a message content.
291 * toplevel = 1 # we are at the top level of the message
292 * toplevel = 0 # we are inside message type or multipart type
293 * # other than multipart/digest
294 * toplevel = -1 # we are inside multipart/digest
295 * NB: on failure we will fclose(in)!
299 get_content (FILE *in
, char *file
, int toplevel
)
302 char buf
[NMH_BUFSIZ
], name
[NAMESZ
];
306 m_getfld_state_t gstate
;
308 /* allocate the content structure */
311 ct
->c_file
= mh_xstrdup(FENDNULL(file
));
312 ct
->c_begin
= ftell (ct
->c_fp
) + 1;
315 * Parse the header fields for this
316 * content into a linked list.
318 gstate
= m_getfld_state_init(in
);
319 m_getfld_track_filepos2(&gstate
);
320 for (compnum
= 1;;) {
321 int bufsz
= sizeof buf
;
322 switch (state
= m_getfld2(&gstate
, name
, buf
, &bufsz
)) {
327 /* get copies of the buffers */
328 np
= mh_xstrdup(name
);
329 vp
= mh_xstrdup(buf
);
331 /* if necessary, get rest of field */
332 while (state
== FLDPLUS
) {
334 state
= m_getfld2(&gstate
, name
, buf
, &bufsz
);
335 vp
= add (buf
, vp
); /* add to previous value */
338 /* Now add the header data to the list */
339 add_header (ct
, np
, vp
);
341 /* continue, to see if this isn't the last header field */
342 ct
->c_begin
= ftell (in
) + 1;
346 /* There are two cases. The unusual one is when there is no
347 * blank line between the headers and the body. This is
348 * indicated by the name of the header starting with `:'.
350 * For both cases, normal first, `1' is the desired c_begin
351 * file position for the start of the body, and `2' is the
352 * file position when buf is returned.
354 * f o o : b a r \n \n b o d y \n bufsz = 6
356 * f o o : b a r \n b o d y \n bufsz = 4
359 * For the normal case, bufsz includes the
360 * header-terminating `\n', even though it is not in buf,
361 * but bufsz isn't affected when it's missing in the unusual
363 if (name
[0] == ':') {
364 ct
->c_begin
= ftell(in
) - bufsz
;
366 ct
->c_begin
= ftell (in
) - (bufsz
- 1);
371 ct
->c_begin
= ftell (in
);
376 die("message format error in component #%d", compnum
);
379 die("getfld() returned %d", state
);
382 /* break out of the loop */
385 m_getfld_state_destroy (&gstate
);
388 * Read the content headers. We will parse the
389 * MIME related header fields into their various
390 * structures and set internal flags related to
391 * content type/subtype, etc.
394 hp
= ct
->c_first_hf
; /* start at first header field */
396 /* Get MIME-Version field */
397 if (!strcasecmp (hp
->name
, VRSN_FIELD
)) {
402 vrsn
= mh_xstrdup(FENDNULL(hp
->value
));
404 /* Now, cleanup this field */
407 while (isspace ((unsigned char) *cp
))
409 for (dp
= strchr(cp
, '\n'); dp
; dp
= strchr(dp
, '\n'))
411 for (dp
= cp
+ strlen (cp
) - 1; dp
>= cp
; dp
--)
412 if (!isspace ((unsigned char) *dp
))
416 fprintf (stderr
, "%s: %s\n", VRSN_FIELD
, cp
);
419 get_comment (ct
->c_file
, VRSN_FIELD
, &cp
, NULL
) == NOTOK
)
422 for (dp
= cp
; istoken (*dp
); dp
++)
426 ucmp
= !strcasecmp (cp
, VRSN_VALUE
);
429 inform("message %s has unknown value for %s: field (%s), continuing...",
430 ct
->c_file
, VRSN_FIELD
, cp
);
435 if (! suppress_multiple_mime_version_warning
)
436 inform("message %s has multiple %s: fields",
437 ct
->c_file
, VRSN_FIELD
);
441 else if (!strcasecmp (hp
->name
, TYPE_FIELD
)) {
442 /* Get Content-Type field */
443 struct str2init
*s2i
;
444 CI ci
= &ct
->c_ctinfo
;
446 /* Check if we've already seen a Content-Type header */
448 inform("message %s has multiple %s: fields",
449 ct
->c_file
, TYPE_FIELD
);
453 /* Parse the Content-Type field */
454 if (get_ctinfo (hp
->value
, ct
, 0) == NOTOK
)
458 * Set the Init function and the internal
459 * flag for this content type.
461 for (s2i
= str2cts
; s2i
->si_key
; s2i
++)
462 if (!strcasecmp (ci
->ci_type
, s2i
->si_key
))
464 if (!s2i
->si_key
&& !uprf (ci
->ci_type
, "X-"))
466 ct
->c_type
= s2i
->si_val
;
467 ct
->c_ctinitfnx
= s2i
->si_init
;
469 else if (!strcasecmp (hp
->name
, ENCODING_FIELD
)) {
470 /* Get Content-Transfer-Encoding field */
472 struct str2init
*s2i
;
475 * Check if we've already seen the
476 * Content-Transfer-Encoding field
479 inform("message %s has multiple %s: fields",
480 ct
->c_file
, ENCODING_FIELD
);
484 /* get copy of this field */
485 ct
->c_celine
= cp
= mh_xstrdup(FENDNULL(hp
->value
));
487 while (isspace ((unsigned char) *cp
))
489 for (dp
= cp
; istoken (*dp
); dp
++)
495 * Find the internal flag and Init function
496 * for this transfer encoding.
498 for (s2i
= str2ces
; s2i
->si_key
; s2i
++)
499 if (!strcasecmp (cp
, s2i
->si_key
))
501 if (!s2i
->si_key
&& !uprf (cp
, "X-"))
504 ct
->c_encoding
= s2i
->si_val
;
506 /* Call the Init function for this encoding */
507 if (s2i
->si_init
&& (*s2i
->si_init
) (ct
) == NOTOK
)
510 else if (!strcasecmp (hp
->name
, MD5_FIELD
)) {
511 /* Get Content-MD5 field */
517 if (ct
->c_digested
) {
518 inform("message %s has multiple %s: fields",
519 ct
->c_file
, MD5_FIELD
);
523 ep
= cp
= mh_xstrdup(FENDNULL(hp
->value
)); /* get a copy */
525 while (isspace ((unsigned char) *cp
))
527 for (dp
= strchr(cp
, '\n'); dp
; dp
= strchr(dp
, '\n'))
529 for (dp
= cp
+ strlen (cp
) - 1; dp
>= cp
; dp
--)
530 if (!isspace ((unsigned char) *dp
))
534 fprintf (stderr
, "%s: %s\n", MD5_FIELD
, cp
);
537 get_comment (ct
->c_file
, MD5_FIELD
, &cp
, NULL
) == NOTOK
) {
542 for (dp
= cp
; *dp
&& !isspace ((unsigned char) *dp
); dp
++)
550 else if (!strcasecmp (hp
->name
, ID_FIELD
)) {
551 /* Get Content-ID field */
552 ct
->c_id
= add (hp
->value
, ct
->c_id
);
554 else if (!strcasecmp (hp
->name
, DESCR_FIELD
)) {
555 /* Get Content-Description field */
556 ct
->c_descr
= add (hp
->value
, ct
->c_descr
);
558 else if (!strcasecmp (hp
->name
, DISPO_FIELD
)) {
559 /* Get Content-Disposition field */
560 if (get_dispo(hp
->value
, ct
, 0) == NOTOK
)
565 hp
= hp
->next
; /* next header field */
569 * Check if we saw a Content-Type field.
570 * If not, then assign a default value for
571 * it, and the Init function.
575 * If we are inside a multipart/digest message,
576 * so default type is message/rfc822
579 if (get_ctinfo ("message/rfc822", ct
, 0) == NOTOK
)
581 ct
->c_type
= CT_MESSAGE
;
582 ct
->c_ctinitfnx
= InitMessage
;
585 * Else default type is text/plain
587 if (get_ctinfo ("text/plain", ct
, 0) == NOTOK
)
589 ct
->c_type
= CT_TEXT
;
590 ct
->c_ctinitfnx
= InitText
;
594 /* Use default Transfer-Encoding, if necessary */
596 ct
->c_encoding
= CE_7BIT
;
609 * small routine to add header field to list
613 add_header (CT ct
, char *name
, char *value
)
617 /* allocate header field structure */
620 /* link data into header structure */
625 /* link header structure into the list */
626 if (ct
->c_first_hf
== NULL
) {
627 ct
->c_first_hf
= hp
; /* this is the first */
630 ct
->c_last_hf
->next
= hp
; /* add it to the end */
639 * Parse Content-Type line and (if `magic' is non-zero) mhbuild composition
640 * directives. Fills in the information of the CTinfo structure.
643 get_ctinfo (char *cp
, CT ct
, int magic
)
652 /* store copy of Content-Type line */
653 cp
= ct
->c_ctline
= mh_xstrdup(FENDNULL(cp
));
655 while (isspace ((unsigned char) *cp
)) /* trim leading spaces */
658 /* change newlines to spaces */
659 for (dp
= strchr(cp
, '\n'); dp
; dp
= strchr(dp
, '\n'))
662 /* trim trailing spaces */
663 for (dp
= cp
+ strlen (cp
) - 1; dp
>= cp
; dp
--)
664 if (!isspace ((unsigned char) *dp
))
669 fprintf (stderr
, "%s: %s\n", TYPE_FIELD
, cp
);
671 if (*cp
== '(' && get_comment (ct
->c_file
, TYPE_FIELD
, &cp
,
672 &ci
->ci_comment
) == NOTOK
)
675 for (dp
= cp
; istoken (*dp
); dp
++)
679 ci
->ci_type
= mh_xstrdup(cp
); /* store content type */
684 inform("invalid %s: field in message %s (empty type)",
685 TYPE_FIELD
, ct
->c_file
);
688 to_lower(ci
->ci_type
);
690 while (isspace ((unsigned char) *cp
))
693 if (*cp
== '(' && get_comment (ct
->c_file
, TYPE_FIELD
, &cp
,
694 &ci
->ci_comment
) == NOTOK
)
699 ci
->ci_subtype
= mh_xstrdup("");
704 while (isspace ((unsigned char) *cp
))
707 if (*cp
== '(' && get_comment (ct
->c_file
, TYPE_FIELD
, &cp
,
708 &ci
->ci_comment
) == NOTOK
)
711 for (dp
= cp
; istoken (*dp
); dp
++)
715 ci
->ci_subtype
= mh_xstrdup(cp
); /* store the content subtype */
719 if (!*ci
->ci_subtype
) {
720 inform("invalid %s: field in message %s (empty subtype for \"%s\")",
721 TYPE_FIELD
, ct
->c_file
, ci
->ci_type
);
724 to_lower(ci
->ci_subtype
);
727 while (isspace ((unsigned char) *cp
))
730 if (*cp
== '(' && get_comment (ct
->c_file
, TYPE_FIELD
, &cp
,
731 &ci
->ci_comment
) == NOTOK
)
734 if ((status
= parse_header_attrs (ct
->c_file
, TYPE_FIELD
, &cp
,
735 &ci
->ci_first_pm
, &ci
->ci_last_pm
,
736 &ci
->ci_comment
)) != OK
) {
737 return status
== NOTOK
? NOTOK
: OK
;
741 * Get any <Content-Id> given in buffer
743 if (magic
&& *cp
== '<') {
746 if (!(dp
= strchr(ct
->c_id
= ++cp
, '>'))) {
747 inform("invalid ID in message %s", ct
->c_file
);
753 ct
->c_id
= concat ("<", ct
->c_id
, ">\n", NULL
);
759 while (isspace ((unsigned char) *cp
))
764 * Get any [Content-Description] given in buffer.
766 if (magic
&& *cp
== '[') {
768 for (dp
= cp
+ strlen (cp
) - 1; dp
>= cp
; dp
--)
772 inform("invalid description in message %s", ct
->c_file
);
780 ct
->c_descr
= concat (ct
->c_descr
, "\n", NULL
);
786 while (isspace ((unsigned char) *cp
))
791 * Get any {Content-Disposition} given in buffer.
793 if (magic
&& *cp
== '{') {
795 for (dp
= cp
+ strlen (cp
) - 1; dp
>= cp
; dp
--)
799 inform("invalid disposition in message %s", ct
->c_file
);
807 if (get_dispo(cp
, ct
, 1) != OK
)
813 while (isspace ((unsigned char) *cp
))
818 * Get any extension directives (right now just the content transfer
819 * encoding, but maybe others) that we care about.
822 if (magic
&& *cp
== '*') {
824 * See if it's a CTE we match on
829 while (*cp
!= '\0' && ! isspace((unsigned char) *cp
))
833 inform("invalid null transfer encoding specification");
840 ct
->c_reqencoding
= CE_UNKNOWN
;
842 for (kv
= EncodingType
; kv
->kv_key
; kv
++) {
843 if (strcasecmp(kv
->kv_key
, dp
) == 0) {
844 ct
->c_reqencoding
= kv
->kv_value
;
849 if (ct
->c_reqencoding
== CE_UNKNOWN
) {
850 inform("invalid CTE specification: \"%s\"", dp
);
854 while (isspace ((unsigned char) *cp
))
859 * Check if anything is left over
863 ci
->ci_magic
= mh_xstrdup(cp
);
865 /* If there is a Content-Disposition header and it doesn't
866 have a *filename=, extract it from the magic contents.
867 The r1bindex call skips any leading directory
869 if (ct
->c_dispo_type
&&
870 !get_param(ct
->c_dispo_first
, "filename", '_', 1)) {
871 add_param(&ct
->c_dispo_first
, &ct
->c_dispo_last
, "filename",
872 r1bindex(ci
->ci_magic
, '/'), 0);
876 inform("extraneous information in message %s's %s: field\n"
877 " (%s)", ct
->c_file
, TYPE_FIELD
, cp
);
885 * Parse out a Content-Disposition header. A lot of this is cribbed from
889 get_dispo (char *cp
, CT ct
, int buildflag
)
891 char *dp
, *dispoheader
;
896 * Save the whole copy of the Content-Disposition header, unless we're
897 * processing a mhbuild directive. A NULL c_dispo will be a flag to
898 * mhbuild that the disposition header needs to be generated at that
902 dispoheader
= cp
= mh_xstrdup(FENDNULL(cp
));
904 while (isspace ((unsigned char) *cp
)) /* trim leading spaces */
907 /* change newlines to spaces */
908 for (dp
= strchr(cp
, '\n'); dp
; dp
= strchr(dp
, '\n'))
911 /* trim trailing spaces */
912 for (dp
= cp
+ strlen (cp
) - 1; dp
>= cp
; dp
--)
913 if (!isspace ((unsigned char) *dp
))
918 fprintf (stderr
, "%s: %s\n", DISPO_FIELD
, cp
);
920 if (*cp
== '(' && get_comment (ct
->c_file
, DISPO_FIELD
, &cp
, NULL
) ==
926 for (dp
= cp
; istoken (*dp
); dp
++)
930 ct
->c_dispo_type
= mh_xstrdup(cp
); /* store disposition type */
934 if (*cp
== '(' && get_comment (ct
->c_file
, DISPO_FIELD
, &cp
, NULL
) == NOTOK
)
937 if ((status
= parse_header_attrs (ct
->c_file
, DISPO_FIELD
, &cp
,
938 &ct
->c_dispo_first
, &ct
->c_dispo_last
,
940 if (status
== NOTOK
) {
945 inform("extraneous information in message %s's %s: field\n (%s)",
946 ct
->c_file
, DISPO_FIELD
, cp
);
952 ct
->c_dispo
= dispoheader
;
959 get_comment (const char *filename
, const char *fieldname
, char **ap
,
964 char c
, buffer
[BUFSIZ
], *dp
;
974 inform("invalid comment in message %s's %s: field",
975 filename
, fieldname
);
980 if ((c
= *cp
++) == '\0')
1003 if ((dp
= *commentp
)) {
1004 *commentp
= concat (dp
, " ", buffer
, NULL
);
1007 *commentp
= mh_xstrdup(buffer
);
1011 while (isspace ((unsigned char) *cp
))
1022 * Handles content types audio, image, and video.
1023 * There's not much to do right here.
1031 return OK
; /* not much to do here */
1042 char buffer
[BUFSIZ
];
1047 CI ci
= &ct
->c_ctinfo
;
1049 /* check for missing subtype */
1050 if (!*ci
->ci_subtype
)
1051 ci
->ci_subtype
= add ("plain", ci
->ci_subtype
);
1054 ct
->c_subtype
= ct_str_subtype (CT_TEXT
, ci
->ci_subtype
);
1056 /* allocate text character set structure */
1058 ct
->c_ctparams
= (void *) t
;
1060 /* scan for charset parameter */
1061 for (pm
= ci
->ci_first_pm
; pm
; pm
= pm
->pm_next
)
1062 if (!strcasecmp (pm
->pm_name
, "charset"))
1065 /* check if content specified a character set */
1067 chset
= pm
->pm_value
;
1068 t
->tx_charset
= CHARSET_SPECIFIED
;
1070 t
->tx_charset
= CHARSET_UNSPECIFIED
;
1074 * If we can not handle character set natively,
1075 * then check profile for string to modify the
1076 * terminal or display method.
1078 * termproc is for mhshow, though mhlist -debug prints it, too.
1080 if (chset
!= NULL
&& !check_charset (chset
, strlen (chset
))) {
1081 snprintf (buffer
, sizeof(buffer
), "%s-charset-%s", invo_name
, chset
);
1082 if ((cp
= context_find (buffer
)))
1083 ct
->c_termproc
= mh_xstrdup(cp
);
1095 InitMultiPart (CT ct
)
1105 struct multipart
*m
;
1106 struct part
*part
, **next
;
1107 CI ci
= &ct
->c_ctinfo
;
1112 * The encoding for multipart messages must be either
1113 * 7bit, 8bit, or binary (per RFC 2045).
1115 if (! skip_mp_cte_check
&& ct
->c_encoding
!= CE_7BIT
&&
1116 ct
->c_encoding
!= CE_8BIT
&& ct
->c_encoding
!= CE_BINARY
) {
1117 /* Copy the Content-Transfer-Encoding header field body so we can
1118 remove any trailing whitespace and leading blanks from it. */
1119 char *cte
= mh_xstrdup(ct
->c_celine
? ct
->c_celine
: "(null)");
1121 bp
= cte
+ strlen (cte
) - 1;
1122 while (bp
>= cte
&& isspace ((unsigned char) *bp
)) *bp
-- = '\0';
1123 for (bp
= cte
; isblank((unsigned char)*bp
); ++bp
) continue;
1125 inform("\"%s/%s\" type in message %s must be encoded in\n"
1126 "7bit, 8bit, or binary, per RFC 2045 (6.4). "
1127 "mhfixmsg -fixcte can fix it, or\n"
1128 "manually edit the file and change the \"%s\"\n"
1129 "Content-Transfer-Encoding to one of those. For now, continuing...",
1130 ci
->ci_type
, ci
->ci_subtype
, ct
->c_file
, bp
);
1137 ct
->c_subtype
= ct_str_subtype (CT_MULTIPART
, ci
->ci_subtype
);
1140 * Check for "boundary" parameter, which is
1141 * required for multipart messages.
1144 for (pm
= ci
->ci_first_pm
; pm
; pm
= pm
->pm_next
) {
1145 if (!strcasecmp (pm
->pm_name
, "boundary")) {
1151 /* complain if boundary parameter is missing */
1153 inform("a \"boundary\" parameter is mandatory for \"%s/%s\" type in message %s's %s: field",
1154 ci
->ci_type
, ci
->ci_subtype
, ct
->c_file
, TYPE_FIELD
);
1158 /* allocate primary structure for multipart info */
1160 ct
->c_ctparams
= (void *) m
;
1162 /* check if boundary parameter contains only whitespace characters */
1163 for (cp
= bp
; isspace ((unsigned char) *cp
); cp
++)
1166 inform("invalid \"boundary\" parameter for \"%s/%s\" type in message %s's %s: field",
1167 ci
->ci_type
, ci
->ci_subtype
, ct
->c_file
, TYPE_FIELD
);
1171 /* remove trailing whitespace from boundary parameter */
1172 for (cp
= bp
, dp
= cp
+ strlen (cp
) - 1; dp
> cp
; dp
--)
1173 if (!isspace ((unsigned char) *dp
))
1177 /* record boundary separators */
1178 m
->mp_start
= concat (bp
, "\n", NULL
);
1179 m
->mp_stop
= concat (bp
, "--\n", NULL
);
1181 if (!ct
->c_fp
&& (ct
->c_fp
= fopen (ct
->c_file
, "r")) == NULL
) {
1182 advise (ct
->c_file
, "unable to open for reading");
1186 fseek (fp
= ct
->c_fp
, pos
= ct
->c_begin
, SEEK_SET
);
1188 next
= &m
->mp_parts
;
1192 while ((gotlen
= getline(&bufp
, &buflen
, fp
)) != -1) {
1197 if (bufp
[0] != '-' || bufp
[1] != '-')
1200 if (strcmp (bufp
+ 2, m
->mp_start
))
1205 next
= &part
->mp_next
;
1207 if (!(p
= get_content (fp
, ct
->c_file
,
1208 ct
->c_subtype
== MULTI_DIGEST
? -1 : 0))) {
1216 fseek (fp
, pos
, SEEK_SET
);
1219 if (strcmp (bufp
+ 2, m
->mp_start
) == 0) {
1223 p
->c_end
= ftell(fp
) - (gotlen
+ 1);
1224 if (p
->c_end
< p
->c_begin
)
1225 p
->c_begin
= p
->c_end
;
1230 if (strcmp (bufp
+ 2, m
->mp_stop
) == 0)
1235 if (! suppress_bogus_mp_content_warning
) {
1236 inform("bogus multipart content in message %s", ct
->c_file
);
1238 bogus_mp_content
= true;
1240 if (!inout
&& part
) {
1242 p
->c_end
= ct
->c_end
;
1244 if (p
->c_begin
>= p
->c_end
) {
1245 for (next
= &m
->mp_parts
; *next
!= part
;
1246 next
= &((*next
)->mp_next
))
1255 /* reverse the order of the parts for multipart/alternative */
1256 if (ct
->c_subtype
== MULTI_ALTERNATE
) {
1262 * label all subparts with part number, and
1263 * then initialize the content of the subpart.
1268 char partnam
[BUFSIZ
];
1271 snprintf (partnam
, sizeof(partnam
), "%s.", ct
->c_partno
);
1272 pp
= partnam
+ strlen (partnam
);
1277 for (part
= m
->mp_parts
, partnum
= 1; part
;
1278 part
= part
->mp_next
, partnum
++) {
1281 sprintf (pp
, "%d", partnum
);
1282 p
->c_partno
= mh_xstrdup(partnam
);
1284 /* initialize the content of the subparts */
1285 if (p
->c_ctinitfnx
&& (*p
->c_ctinitfnx
) (p
) == NOTOK
) {
1294 get_leftover_mp_content (ct
, 1);
1295 get_leftover_mp_content (ct
, 0);
1305 * reverse the order of the parts of a multipart/alternative,
1306 * presumably to put the "most favored" alternative first, for
1307 * ease of choosing/displaying it later on. from a mail message on
1308 * nmh-workers, from kenh:
1309 * "Stock" MH 6.8.5 did not have a reverse_parts() function, but I
1310 * see code in mhn that did the same thing... According to the RCS
1311 * logs, that code was around from the initial checkin of mhn.c by
1312 * John Romine in 1992, which is as far back as we have."
1315 reverse_parts (CT ct
)
1317 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
1321 /* Reverse the order of its parts by walking the mp_parts list
1322 and pushing each node to the front. */
1323 for (part
= m
->mp_parts
, m
->mp_parts
= NULL
; part
; part
= next
) {
1324 next
= part
->mp_next
;
1325 part
->mp_next
= m
->mp_parts
;
1331 move_preferred_part(CT ct
, mime_type_subtype
*pref
)
1333 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
1334 struct part
*part
, *prev
, *head
, *nhead
, *ntail
;
1338 /* move the matching part(s) to the head of the list: walk the
1339 * list of parts, move matching parts to a new list (maintaining
1340 * their order), and finally, concatenate the old list onto the
1347 head
->mp_next
= m
->mp_parts
;
1348 nhead
->mp_next
= NULL
;
1352 part
= head
->mp_next
;
1353 while (part
!= NULL
) {
1354 ci
= &part
->mp_part
->c_ctinfo
;
1355 if (!strcasecmp(ci
->ci_type
, pref
->type
) &&
1357 !strcasecmp(ci
->ci_subtype
, pref
->subtype
))) {
1358 prev
->mp_next
= part
->mp_next
;
1359 part
->mp_next
= NULL
;
1360 ntail
->mp_next
= part
;
1362 part
= prev
->mp_next
;
1365 part
= prev
->mp_next
;
1368 ntail
->mp_next
= head
->mp_next
;
1369 m
->mp_parts
= nhead
->mp_next
;
1373 * move parts that match the user's preferences (-prefer) to the head
1374 * of the line. process preferences in reverse so first one given
1375 * ends up first in line
1381 for (i
= 0; i
< npreferred
; i
++)
1382 move_preferred_part(ct
, mime_preference
+ i
);
1387 /* parse_mime() arranges alternates in reverse (priority) order. This
1388 function can be used to reverse them back. This will put, for
1389 example, a text/plain part before a text/html part in a
1390 multipart/alternative part, for example, where it belongs. */
1392 reverse_alternative_parts (CT ct
)
1394 if (ct
->c_type
== CT_MULTIPART
) {
1395 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
1398 if (ct
->c_subtype
== MULTI_ALTERNATE
) {
1402 /* And call recursively on each part of a multipart. */
1403 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
1404 reverse_alternative_parts (part
->mp_part
);
1417 CI ci
= &ct
->c_ctinfo
;
1419 if ((ct
->c_encoding
!= CE_7BIT
) && (ct
->c_encoding
!= CE_8BIT
)) {
1420 inform("\"%s/%s\" type in message %s should be encoded in "
1421 "7bit or 8bit, continuing...", ci
->ci_type
, ci
->ci_subtype
,
1426 /* check for missing subtype */
1427 if (!*ci
->ci_subtype
)
1428 ci
->ci_subtype
= add ("rfc822", ci
->ci_subtype
);
1431 ct
->c_subtype
= ct_str_subtype (CT_MESSAGE
, ci
->ci_subtype
);
1433 switch (ct
->c_subtype
) {
1434 case MESSAGE_RFC822
:
1437 case MESSAGE_PARTIAL
:
1443 ct
->c_ctparams
= (void *) p
;
1445 /* scan for parameters "id", "number", and "total" */
1446 for (pm
= ci
->ci_first_pm
; pm
; pm
= pm
->pm_next
) {
1447 if (!strcasecmp (pm
->pm_name
, "id")) {
1448 p
->pm_partid
= mh_xstrdup(FENDNULL(pm
->pm_value
));
1451 if (!strcasecmp (pm
->pm_name
, "number")) {
1452 if (sscanf (pm
->pm_value
, "%d", &p
->pm_partno
) != 1
1453 || p
->pm_partno
< 1) {
1455 inform("invalid %s parameter for \"%s/%s\" type in message %s's %s field",
1456 pm
->pm_name
, ci
->ci_type
, ci
->ci_subtype
,
1457 ct
->c_file
, TYPE_FIELD
);
1462 if (!strcasecmp (pm
->pm_name
, "total")) {
1463 if (sscanf (pm
->pm_value
, "%d", &p
->pm_maxno
) != 1
1472 || (p
->pm_maxno
&& p
->pm_partno
> p
->pm_maxno
)) {
1473 inform("invalid parameters for \"%s/%s\" type in message %s's %s field",
1474 ci
->ci_type
, ci
->ci_subtype
, ct
->c_file
, TYPE_FIELD
);
1480 case MESSAGE_EXTERNAL
:
1488 ct
->c_ctparams
= (void *) e
;
1491 && (ct
->c_fp
= fopen (ct
->c_file
, "r")) == NULL
) {
1492 advise (ct
->c_file
, "unable to open for reading");
1496 fseek (fp
= ct
->c_fp
, ct
->c_begin
, SEEK_SET
);
1498 if (!(p
= get_content (fp
, ct
->c_file
, 0))) {
1506 p
->c_ceopenfnx
= NULL
;
1507 if ((exresult
= params_external (ct
, 0)) != NOTOK
1508 && p
->c_ceopenfnx
== openMail
) {
1512 if ((size
= ct
->c_end
- p
->c_begin
) <= 0) {
1514 content_error (NULL
, ct
,
1515 "empty body for access-type=mail-server");
1519 e
->eb_body
= bp
= mh_xmalloc ((unsigned) size
);
1520 fseek (p
->c_fp
, p
->c_begin
, SEEK_SET
);
1522 switch (cc
= fread (bp
, sizeof(*bp
), size
, p
->c_fp
)) {
1524 adios ("failed", "fread");
1527 die("unexpected EOF from fread");
1530 bp
+= cc
, size
-= cc
;
1537 p
->c_end
= p
->c_begin
;
1542 if (exresult
== NOTOK
)
1544 if (e
->eb_flags
== NOTOK
)
1547 switch (p
->c_type
) {
1552 if (p
->c_subtype
!= MESSAGE_RFC822
)
1556 e
->eb_partno
= ct
->c_partno
;
1558 (*p
->c_ctinitfnx
) (p
);
1573 params_external (CT ct
, int composing
)
1576 struct exbody
*e
= (struct exbody
*) ct
->c_ctparams
;
1577 CI ci
= &ct
->c_ctinfo
;
1579 ct
->c_ceopenfnx
= NULL
;
1580 for (pm
= ci
->ci_first_pm
; pm
; pm
= pm
->pm_next
) {
1581 if (!strcasecmp (pm
->pm_name
, "access-type")) {
1582 struct str2init
*s2i
;
1583 CT p
= e
->eb_content
;
1585 for (s2i
= str2methods
; s2i
->si_key
; s2i
++)
1586 if (!strcasecmp (pm
->pm_value
, s2i
->si_key
))
1589 e
->eb_access
= pm
->pm_value
;
1590 e
->eb_flags
= NOTOK
;
1591 p
->c_encoding
= CE_EXTERNAL
;
1594 e
->eb_access
= s2i
->si_key
;
1595 e
->eb_flags
= s2i
->si_val
;
1596 p
->c_encoding
= CE_EXTERNAL
;
1598 /* Call the Init function for this external type */
1599 if ((*s2i
->si_init
)(p
) == NOTOK
)
1603 if (!strcasecmp (pm
->pm_name
, "name")) {
1604 e
->eb_name
= pm
->pm_value
;
1607 if (!strcasecmp (pm
->pm_name
, "permission")) {
1608 e
->eb_permission
= pm
->pm_value
;
1611 if (!strcasecmp (pm
->pm_name
, "site")) {
1612 e
->eb_site
= pm
->pm_value
;
1615 if (!strcasecmp (pm
->pm_name
, "directory")) {
1616 e
->eb_dir
= pm
->pm_value
;
1619 if (!strcasecmp (pm
->pm_name
, "mode")) {
1620 e
->eb_mode
= pm
->pm_value
;
1623 if (!strcasecmp (pm
->pm_name
, "size")) {
1624 sscanf (pm
->pm_value
, "%lu", &e
->eb_size
);
1627 if (!strcasecmp (pm
->pm_name
, "server")) {
1628 e
->eb_server
= pm
->pm_value
;
1631 if (!strcasecmp (pm
->pm_name
, "subject")) {
1632 e
->eb_subject
= pm
->pm_value
;
1635 if (!strcasecmp (pm
->pm_name
, "url")) {
1637 * According to RFC 2017, we have to remove all whitespace from
1641 char *u
, *p
= pm
->pm_value
;
1642 e
->eb_url
= u
= mh_xmalloc(strlen(pm
->pm_value
) + 1);
1644 for (; *p
!= '\0'; p
++) {
1645 if (! isspace((unsigned char) *p
))
1652 if (composing
&& !strcasecmp (pm
->pm_name
, "body")) {
1653 e
->eb_body
= getcpy (pm
->pm_value
);
1658 if (!e
->eb_access
) {
1659 inform("invalid parameters for \"%s/%s\" type in message %s's %s field",
1660 ci
->ci_type
, ci
->ci_subtype
, ct
->c_file
, TYPE_FIELD
);
1673 InitApplication (CT ct
)
1675 CI ci
= &ct
->c_ctinfo
;
1678 ct
->c_subtype
= ct_str_subtype (CT_APPLICATION
, ci
->ci_subtype
);
1685 * TRANSFER ENCODINGS
1689 init_encoding (CT ct
, OpenCEFunc openfnx
)
1691 ct
->c_ceopenfnx
= openfnx
;
1692 ct
->c_ceclosefnx
= close_encoding
;
1693 ct
->c_cesizefnx
= size_encoding
;
1700 close_encoding (CT ct
)
1702 CE ce
= &ct
->c_cefile
;
1711 static unsigned long
1712 size_encoding (CT ct
)
1717 CE ce
= &ct
->c_cefile
;
1720 if (ce
->ce_fp
&& fstat (fileno (ce
->ce_fp
), &st
) != NOTOK
)
1721 return (long) st
.st_size
;
1724 if (stat (ce
->ce_file
, &st
) != NOTOK
)
1725 return (long) st
.st_size
;
1729 if (ct
->c_encoding
== CE_EXTERNAL
)
1730 return ct
->c_end
- ct
->c_begin
;
1733 if ((fd
= (*ct
->c_ceopenfnx
) (ct
, &file
)) == NOTOK
)
1734 return ct
->c_end
- ct
->c_begin
;
1736 if (fstat (fd
, &st
) != NOTOK
)
1737 size
= (long) st
.st_size
;
1741 (*ct
->c_ceclosefnx
) (ct
);
1753 return init_encoding (ct
, openBase64
);
1758 openBase64 (CT ct
, char **file
)
1762 bool own_ct_fp
= false;
1763 char *cp
, *buffer
= NULL
;
1764 /* sbeck -- handle suffixes */
1766 CE ce
= &ct
->c_cefile
;
1767 unsigned char *decoded
;
1769 unsigned char digest
[16];
1772 fseek (ce
->ce_fp
, 0L, SEEK_SET
);
1777 if ((ce
->ce_fp
= fopen (ce
->ce_file
, "r")) == NULL
) {
1778 content_error (ce
->ce_file
, ct
, "unable to fopen for reading");
1784 if (*file
== NULL
) {
1787 ce
->ce_file
= mh_xstrdup(*file
);
1791 /* sbeck@cise.ufl.edu -- handle suffixes */
1793 if ((cp
= context_find_by_type ("suffix", ci
->ci_type
, ci
->ci_subtype
))) {
1794 if (ce
->ce_unlink
) {
1795 /* Create temporary file with filename extension. */
1796 if ((ce
->ce_file
= m_mktemps(invo_name
, cp
, NULL
, NULL
)) == NULL
) {
1797 die("unable to create temporary file in %s",
1801 ce
->ce_file
= add (cp
, ce
->ce_file
);
1803 } else if (*file
== NULL
) {
1805 if ((tempfile
= m_mktemp2(NULL
, invo_name
, NULL
, NULL
)) == NULL
) {
1806 die("unable to create temporary file in %s",
1809 ce
->ce_file
= mh_xstrdup(tempfile
);
1812 if ((ce
->ce_fp
= fopen (ce
->ce_file
, "w+")) == NULL
) {
1813 content_error (ce
->ce_file
, ct
, "unable to fopen for reading/writing");
1817 if ((len
= ct
->c_end
- ct
->c_begin
) < 0)
1818 die("internal error(1)");
1820 buffer
= mh_xmalloc (len
+ 1);
1823 if ((ct
->c_fp
= fopen (ct
->c_file
, "r")) == NULL
) {
1824 content_error (ct
->c_file
, ct
, "unable to open for reading");
1830 lseek (fd
= fileno (ct
->c_fp
), (off_t
) ct
->c_begin
, SEEK_SET
);
1833 switch (cc
= read (fd
, cp
, len
)) {
1835 content_error (ct
->c_file
, ct
, "error reading from");
1839 content_error (NULL
, ct
, "premature eof");
1850 /* decodeBase64() requires null-terminated input. */
1853 if (decodeBase64 (buffer
, &decoded
, &decoded_len
, ct
->c_type
== CT_TEXT
,
1854 ct
->c_digested
? digest
: NULL
) != OK
)
1859 unsigned char *decoded_p
= decoded
;
1860 for (i
= 0; i
< decoded_len
; ++i
) {
1861 putc (*decoded_p
++, ce
->ce_fp
);
1864 if (ferror (ce
->ce_fp
)) {
1865 content_error (ce
->ce_file
, ct
, "error writing to");
1869 if (ct
->c_digested
) {
1870 if (memcmp(digest
, ct
->c_digest
,
1872 content_error (NULL
, ct
,
1873 "content integrity suspect (digest mismatch) -- continuing");
1876 fprintf (stderr
, "content integrity confirmed\n");
1882 fseek (ct
->c_fp
, 0L, SEEK_SET
);
1884 if (fflush (ce
->ce_fp
)) {
1885 content_error (ce
->ce_file
, ct
, "error writing to");
1889 fseek (ce
->ce_fp
, 0L, SEEK_SET
);
1892 *file
= ce
->ce_file
;
1898 return fileno (ce
->ce_fp
);
1905 free_encoding (ct
, 0);
1915 static char hex2nib
[0x80] = {
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, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1922 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
1923 0x08, 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1924 0x00, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x00,
1925 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1926 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1927 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1928 0x00, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x00,
1929 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1930 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1931 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
1938 return init_encoding (ct
, openQuoted
);
1943 openQuoted (CT ct
, char **file
)
1945 int cc
, digested
, len
, quoted
;
1946 bool own_ct_fp
= false;
1952 CE ce
= &ct
->c_cefile
;
1953 /* sbeck -- handle suffixes */
1958 fseek (ce
->ce_fp
, 0L, SEEK_SET
);
1963 if ((ce
->ce_fp
= fopen (ce
->ce_file
, "r")) == NULL
) {
1964 content_error (ce
->ce_file
, ct
, "unable to fopen for reading");
1970 if (*file
== NULL
) {
1973 ce
->ce_file
= mh_xstrdup(*file
);
1977 /* sbeck@cise.ufl.edu -- handle suffixes */
1979 if ((cp
= context_find_by_type ("suffix", ci
->ci_type
, ci
->ci_subtype
))) {
1980 if (ce
->ce_unlink
) {
1981 /* Create temporary file with filename extension. */
1982 if ((ce
->ce_file
= m_mktemps(invo_name
, cp
, NULL
, NULL
)) == NULL
) {
1983 die("unable to create temporary file in %s",
1987 ce
->ce_file
= add (cp
, ce
->ce_file
);
1989 } else if (*file
== NULL
) {
1991 if ((tempfile
= m_mktemp2(NULL
, invo_name
, NULL
, NULL
)) == NULL
) {
1992 die("unable to create temporary file in %s",
1995 ce
->ce_file
= mh_xstrdup(tempfile
);
1998 if ((ce
->ce_fp
= fopen (ce
->ce_file
, "w+")) == NULL
) {
1999 content_error (ce
->ce_file
, ct
, "unable to fopen for reading/writing");
2003 if ((len
= ct
->c_end
- ct
->c_begin
) < 0)
2004 die("internal error(2)");
2007 if ((ct
->c_fp
= fopen (ct
->c_file
, "r")) == NULL
) {
2008 content_error (ct
->c_file
, ct
, "unable to open for reading");
2014 if ((digested
= ct
->c_digested
))
2015 MD5Init (&mdContext
);
2019 fseek (ct
->c_fp
, ct
->c_begin
, SEEK_SET
);
2021 if ((gotlen
= getline(&bufp
, &buflen
, ct
->c_fp
)) == -1) {
2022 content_error (NULL
, ct
, "premature eof");
2026 if ((cc
= gotlen
) > len
)
2030 for (ep
= (cp
= bufp
) + cc
- 1; cp
<= ep
; ep
--)
2031 if (!isspace ((unsigned char) *ep
))
2036 for (; cp
< ep
; cp
++) {
2038 /* in an escape sequence */
2040 /* at byte 1 of an escape sequence */
2041 mask
= hex2nib
[((unsigned char) *cp
) & 0x7f];
2042 /* next is byte 2 */
2045 /* at byte 2 of an escape sequence */
2047 mask
|= hex2nib
[((unsigned char) *cp
) & 0x7f];
2048 putc (mask
, ce
->ce_fp
);
2050 MD5Update (&mdContext
, &mask
, 1);
2051 if (ferror (ce
->ce_fp
)) {
2052 content_error (ce
->ce_file
, ct
, "error writing to");
2055 /* finished escape sequence; next may be literal or a new
2056 * escape sequence */
2059 /* on to next byte */
2063 /* not in an escape sequence */
2065 /* starting an escape sequence, or invalid '='? */
2066 if (cp
+ 1 < ep
&& cp
[1] == '\n') {
2067 /* "=\n" soft line break, eat the \n */
2071 if (cp
+ 1 >= ep
|| cp
+ 2 >= ep
) {
2072 /* We don't have 2 bytes left, so this is an invalid
2073 * escape sequence; just show the raw bytes (below). */
2074 } else if (isxdigit ((unsigned char) cp
[1]) &&
2075 isxdigit ((unsigned char) cp
[2])) {
2076 /* Next 2 bytes are hex digits, making this a valid escape
2077 * sequence; let's decode it (above). */
2081 /* One or both of the next 2 is out of range, making this
2082 * an invalid escape sequence; just show the raw bytes
2086 /* Just show the raw byte. */
2087 putc (*cp
, ce
->ce_fp
);
2090 MD5Update (&mdContext
, (unsigned char *) "\r\n",2);
2092 MD5Update (&mdContext
, (unsigned char *) cp
, 1);
2095 if (ferror (ce
->ce_fp
)) {
2096 content_error (ce
->ce_file
, ct
, "error writing to");
2102 content_error (NULL
, ct
,
2103 "invalid QUOTED-PRINTABLE encoding -- end-of-content while still quoting");
2107 fseek (ct
->c_fp
, 0L, SEEK_SET
);
2109 if (fflush (ce
->ce_fp
)) {
2110 content_error (ce
->ce_file
, ct
, "error writing to");
2115 unsigned char digest
[16];
2117 MD5Final (digest
, &mdContext
);
2118 if (memcmp(digest
, ct
->c_digest
,
2120 content_error (NULL
, ct
,
2121 "content integrity suspect (digest mismatch) -- continuing");
2123 fprintf (stderr
, "content integrity confirmed\n");
2126 fseek (ce
->ce_fp
, 0L, SEEK_SET
);
2129 *file
= ce
->ce_file
;
2135 return fileno (ce
->ce_fp
);
2138 free_encoding (ct
, 0);
2155 if (init_encoding (ct
, open7Bit
) == NOTOK
)
2158 ct
->c_cesizefnx
= NULL
; /* no need to decode for real size */
2164 open7Bit (CT ct
, char **file
)
2167 bool own_ct_fp
= false;
2168 char buffer
[BUFSIZ
];
2169 /* sbeck -- handle suffixes */
2172 CE ce
= &ct
->c_cefile
;
2175 fseek (ce
->ce_fp
, 0L, SEEK_SET
);
2180 if ((ce
->ce_fp
= fopen (ce
->ce_file
, "r")) == NULL
) {
2181 content_error (ce
->ce_file
, ct
, "unable to fopen for reading");
2187 if (*file
== NULL
) {
2190 ce
->ce_file
= mh_xstrdup(*file
);
2194 /* sbeck@cise.ufl.edu -- handle suffixes */
2196 if ((cp
= context_find_by_type ("suffix", ci
->ci_type
, ci
->ci_subtype
))) {
2197 if (ce
->ce_unlink
) {
2198 /* Create temporary file with filename extension. */
2199 if ((ce
->ce_file
= m_mktemps(invo_name
, cp
, NULL
, NULL
)) == NULL
) {
2200 die("unable to create temporary file in %s",
2204 ce
->ce_file
= add (cp
, ce
->ce_file
);
2206 } else if (*file
== NULL
) {
2208 if ((tempfile
= m_mktemp2(NULL
, invo_name
, NULL
, NULL
)) == NULL
) {
2209 die("unable to create temporary file in %s",
2212 ce
->ce_file
= mh_xstrdup(tempfile
);
2215 if ((ce
->ce_fp
= fopen (ce
->ce_file
, "w+")) == NULL
) {
2216 content_error (ce
->ce_file
, ct
, "unable to fopen for reading/writing");
2220 if (ct
->c_type
== CT_MULTIPART
) {
2221 CI ci
= &ct
->c_ctinfo
;
2225 fprintf (ce
->ce_fp
, "%s: %s/%s", TYPE_FIELD
, ci
->ci_type
, ci
->ci_subtype
);
2226 len
+= LEN(TYPE_FIELD
) + 2 + strlen (ci
->ci_type
)
2227 + 1 + strlen (ci
->ci_subtype
);
2228 buffer
= output_params(len
, ci
->ci_first_pm
, &len
, 0);
2231 fputs (buffer
, ce
->ce_fp
);
2235 if (ci
->ci_comment
) {
2236 if (len
+ 1 + (cc
= 2 + strlen (ci
->ci_comment
)) >= CPERLIN
) {
2237 fputs ("\n\t", ce
->ce_fp
);
2241 putc (' ', ce
->ce_fp
);
2244 fprintf (ce
->ce_fp
, "(%s)", ci
->ci_comment
);
2247 fprintf (ce
->ce_fp
, "\n");
2249 fprintf (ce
->ce_fp
, "%s:%s", ID_FIELD
, ct
->c_id
);
2251 fprintf (ce
->ce_fp
, "%s:%s", DESCR_FIELD
, ct
->c_descr
);
2253 fprintf (ce
->ce_fp
, "%s:%s", DISPO_FIELD
, ct
->c_dispo
);
2254 fprintf (ce
->ce_fp
, "\n");
2257 if ((len
= ct
->c_end
- ct
->c_begin
) < 0)
2258 die("internal error(3)");
2261 if ((ct
->c_fp
= fopen (ct
->c_file
, "r")) == NULL
) {
2262 content_error (ct
->c_file
, ct
, "unable to open for reading");
2268 lseek (fd
= fileno (ct
->c_fp
), (off_t
) ct
->c_begin
, SEEK_SET
);
2270 switch (cc
= read (fd
, buffer
, sizeof(buffer
) - 1)) {
2272 content_error (ct
->c_file
, ct
, "error reading from");
2276 content_error (NULL
, ct
, "premature eof");
2284 if ((int) fwrite (buffer
, sizeof(*buffer
), cc
, ce
->ce_fp
) < cc
) {
2285 advise ("open7Bit", "fwrite");
2287 if (ferror (ce
->ce_fp
)) {
2288 content_error (ce
->ce_file
, ct
, "error writing to");
2293 fseek (ct
->c_fp
, 0L, SEEK_SET
);
2295 if (fflush (ce
->ce_fp
)) {
2296 content_error (ce
->ce_file
, ct
, "error writing to");
2300 fseek (ce
->ce_fp
, 0L, SEEK_SET
);
2303 *file
= ce
->ce_file
;
2308 return fileno (ce
->ce_fp
);
2311 free_encoding (ct
, 0);
2325 openExternal (CT ct
, CT cb
, CE ce
, char **file
, int *fd
)
2327 char cachefile
[BUFSIZ
];
2330 fseek (ce
->ce_fp
, 0L, SEEK_SET
);
2335 if ((ce
->ce_fp
= fopen (ce
->ce_file
, "r")) == NULL
) {
2336 content_error (ce
->ce_file
, ct
, "unable to fopen for reading");
2342 if (find_cache(ct
, rcachesw
, NULL
, cb
->c_id
,
2343 cachefile
, sizeof(cachefile
)) != NOTOK
) {
2344 if ((ce
->ce_fp
= fopen (cachefile
, "r"))) {
2345 ce
->ce_file
= mh_xstrdup(cachefile
);
2349 admonish (cachefile
, "unable to fopen for reading");
2352 *fd
= ce
->ce_fp
? fileno (ce
->ce_fp
) : -1;
2356 *file
= ce
->ce_file
;
2357 *fd
= fileno (ce
->ce_fp
);
2368 return init_encoding (ct
, openFile
);
2373 openFile (CT ct
, char **file
)
2376 char cachefile
[BUFSIZ
];
2377 struct exbody
*e
= ct
->c_ctexbody
;
2378 CE ce
= &ct
->c_cefile
;
2380 switch (openExternal (e
->eb_parent
, e
->eb_content
, ce
, file
, &fd
)) {
2392 content_error (NULL
, ct
, "missing name parameter");
2396 ce
->ce_file
= mh_xstrdup(e
->eb_name
);
2399 if ((ce
->ce_fp
= fopen (ce
->ce_file
, "r")) == NULL
) {
2400 content_error (ce
->ce_file
, ct
, "unable to fopen for reading");
2404 if ((!e
->eb_permission
|| strcasecmp (e
->eb_permission
, "read-write"))
2405 && find_cache (NULL
, wcachesw
, &cachetype
, e
->eb_content
->c_id
,
2406 cachefile
, sizeof(cachefile
)) != NOTOK
) {
2410 mask
= umask (cachetype
? ~m_gmprot () : 0222);
2411 if ((fp
= fopen (cachefile
, "w"))) {
2413 char buffer
[BUFSIZ
];
2414 FILE *gp
= ce
->ce_fp
;
2416 fseek (gp
, 0L, SEEK_SET
);
2418 while ((cc
= fread (buffer
, sizeof(*buffer
), sizeof(buffer
), gp
))
2420 if ((int) fwrite (buffer
, sizeof(*buffer
), cc
, fp
) < cc
) {
2421 advise ("openFile", "fwrite");
2426 admonish (ce
->ce_file
, "error reading");
2427 (void) m_unlink (cachefile
);
2428 } else if (ferror (fp
)) {
2429 admonish (cachefile
, "error writing");
2430 (void) m_unlink (cachefile
);
2437 fseek (ce
->ce_fp
, 0L, SEEK_SET
);
2438 *file
= ce
->ce_file
;
2439 return fileno (ce
->ce_fp
);
2449 return init_encoding (ct
, openFTP
);
2454 openFTP (CT ct
, char **file
)
2460 char *bp
, *ftp
, *user
, *pass
;
2461 char buffer
[BUFSIZ
], cachefile
[BUFSIZ
];
2463 CE ce
= &ct
->c_cefile
;
2464 static char *username
= NULL
;
2465 static char *password
= NULL
;
2469 if ((ftp
= context_find (nmhaccessftp
)) && !*ftp
)
2475 switch (openExternal (e
->eb_parent
, e
->eb_content
, ce
, file
, &fd
)) {
2486 if (!e
->eb_name
|| !e
->eb_site
) {
2487 content_error (NULL
, ct
, "missing %s parameter",
2488 e
->eb_name
? "site": "name");
2492 /* Get the buffer ready to go */
2494 buflen
= sizeof(buffer
);
2497 * Construct the query message for user
2499 snprintf (bp
, buflen
, "Retrieve %s", e
->eb_name
);
2505 snprintf (bp
, buflen
, " (content %s)", e
->eb_partno
);
2511 snprintf (bp
, buflen
, "\n using %sFTP from site %s",
2512 e
->eb_flags
? "anonymous " : "", e
->eb_site
);
2517 if (e
->eb_size
> 0) {
2518 snprintf (bp
, buflen
, " (%lu octets)", e
->eb_size
);
2523 snprintf (bp
, buflen
, "? ");
2526 * Now, check the answer
2528 if (!read_yes_or_no_if_tty (buffer
))
2533 snprintf (buffer
, sizeof(buffer
), "%s@%s", getusername (),
2537 ruserpass (e
->eb_site
, &username
, &password
, 0);
2542 ce
->ce_unlink
= (*file
== NULL
);
2544 cachefile
[0] = '\0';
2545 if ((!e
->eb_permission
|| strcasecmp (e
->eb_permission
, "read-write"))
2546 && find_cache (NULL
, wcachesw
, &cachetype
, e
->eb_content
->c_id
,
2547 cachefile
, sizeof(cachefile
)) != NOTOK
) {
2548 if (*file
== NULL
) {
2555 ce
->ce_file
= mh_xstrdup(*file
);
2557 ce
->ce_file
= mh_xstrdup(cachefile
);
2560 if ((tempfile
= m_mktemp2(NULL
, invo_name
, NULL
, NULL
)) == NULL
) {
2561 die("unable to create temporary file in %s",
2564 ce
->ce_file
= mh_xstrdup(tempfile
);
2567 if ((ce
->ce_fp
= fopen (ce
->ce_file
, "w+")) == NULL
) {
2568 content_error (ce
->ce_file
, ct
, "unable to fopen for reading/writing");
2577 vec
[vecp
++] = r1bindex (ftp
, '/');
2578 vec
[vecp
++] = e
->eb_site
;
2581 vec
[vecp
++] = e
->eb_dir
;
2582 vec
[vecp
++] = e
->eb_name
;
2583 vec
[vecp
++] = ce
->ce_file
,
2584 vec
[vecp
++] = e
->eb_mode
&& !strcasecmp (e
->eb_mode
, "ascii")
2585 ? "ascii" : "binary";
2593 adios ("fork", "unable to");
2597 close (fileno (ce
->ce_fp
));
2599 fprintf (stderr
, "unable to exec ");
2605 if (pidXwait (child_id
, NULL
)) {
2606 username
= password
= NULL
;
2616 chmod (cachefile
, cachetype
? m_gmprot () : 0444);
2621 mask
= umask (cachetype
? ~m_gmprot () : 0222);
2622 if ((fp
= fopen (cachefile
, "w"))) {
2624 FILE *gp
= ce
->ce_fp
;
2626 fseek (gp
, 0L, SEEK_SET
);
2628 while ((cc
= fread (buffer
, sizeof(*buffer
), sizeof(buffer
), gp
))
2630 if ((int) fwrite (buffer
, sizeof(*buffer
), cc
, fp
) < cc
) {
2631 advise ("openFTP", "fwrite");
2636 admonish (ce
->ce_file
, "error reading");
2637 (void) m_unlink (cachefile
);
2638 } else if (ferror (fp
)) {
2639 admonish (cachefile
, "error writing");
2640 (void) m_unlink (cachefile
);
2648 fseek (ce
->ce_fp
, 0L, SEEK_SET
);
2649 *file
= ce
->ce_file
;
2650 return fileno (ce
->ce_fp
);
2661 return init_encoding (ct
, openMail
);
2666 openMail (CT ct
, char **file
)
2668 int child_id
, fd
, vecp
;
2670 char *bp
, buffer
[BUFSIZ
], *vec
[7];
2671 struct exbody
*e
= ct
->c_ctexbody
;
2672 CE ce
= &ct
->c_cefile
;
2674 switch (openExternal (e
->eb_parent
, e
->eb_content
, ce
, file
, &fd
)) {
2685 if (!e
->eb_server
) {
2686 content_error (NULL
, ct
, "missing server parameter");
2690 /* Get buffer ready to go */
2692 buflen
= sizeof(buffer
);
2694 /* Now, construct query message */
2695 snprintf (bp
, buflen
, "Retrieve content");
2701 snprintf (bp
, buflen
, " %s", e
->eb_partno
);
2707 snprintf (bp
, buflen
, " by asking %s\n\n%s\n? ",
2709 e
->eb_subject
? e
->eb_subject
: e
->eb_body
);
2711 /* Now, check answer */
2712 if (!read_yes_or_no_if_tty (buffer
))
2716 vec
[vecp
++] = r1bindex (mailproc
, '/');
2717 vec
[vecp
++] = e
->eb_server
;
2718 vec
[vecp
++] = "-subject";
2719 vec
[vecp
++] = e
->eb_subject
? e
->eb_subject
: "mail-server request";
2720 vec
[vecp
++] = "-body";
2721 vec
[vecp
++] = e
->eb_body
;
2727 advise ("fork", "unable to");
2731 execvp (mailproc
, vec
);
2732 fprintf (stderr
, "unable to exec ");
2738 if (pidXwait (child_id
, NULL
) == OK
)
2739 inform("request sent");
2743 if (*file
== NULL
) {
2745 if ((tempfile
= m_mktemp2(NULL
, invo_name
, NULL
, NULL
)) == NULL
) {
2746 die("unable to create temporary file in %s",
2749 ce
->ce_file
= mh_xstrdup(tempfile
);
2752 ce
->ce_file
= mh_xstrdup(*file
);
2756 if ((ce
->ce_fp
= fopen (ce
->ce_file
, "w+")) == NULL
) {
2757 content_error (ce
->ce_file
, ct
, "unable to fopen for reading/writing");
2761 /* showproc is for mhshow and mhstore, though mhlist -debug
2762 * prints it, too. */
2763 free(ct
->c_showproc
);
2764 ct
->c_showproc
= mh_xstrdup("true");
2766 fseek (ce
->ce_fp
, 0L, SEEK_SET
);
2767 *file
= ce
->ce_file
;
2768 return fileno (ce
->ce_fp
);
2779 return init_encoding (ct
, openURL
);
2784 openURL (CT ct
, char **file
)
2786 struct exbody
*e
= ct
->c_ctexbody
;
2787 CE ce
= &ct
->c_cefile
;
2788 char *urlprog
, *program
;
2789 char buffer
[BUFSIZ
], cachefile
[BUFSIZ
];
2793 struct msgs_array args
= { 0, 0, NULL
};
2796 if ((urlprog
= context_find(nmhaccessurl
)) && *urlprog
== '\0')
2800 content_error(NULL
, ct
, "No entry for nmh-access-url in profile");
2804 switch (openExternal(e
->eb_parent
, e
->eb_content
, ce
, file
, &fd
)) {
2816 content_error(NULL
, ct
, "missing url parameter");
2820 ce
->ce_unlink
= (*file
== NULL
);
2822 cachefile
[0] = '\0';
2824 if (find_cache(NULL
, wcachesw
, &cachetype
, e
->eb_content
->c_id
,
2825 cachefile
, sizeof(cachefile
)) != NOTOK
) {
2826 if (*file
== NULL
) {
2833 ce
->ce_file
= mh_xstrdup(*file
);
2835 ce
->ce_file
= mh_xstrdup(cachefile
);
2838 if ((tempfile
= m_mktemp2(NULL
, invo_name
, NULL
, NULL
)) == NULL
) {
2839 die("unable to create temporary file in %s",
2842 ce
->ce_file
= mh_xstrdup(tempfile
);
2845 if ((ce
->ce_fp
= fopen(ce
->ce_file
, "w+")) == NULL
) {
2846 content_error(ce
->ce_file
, ct
, "unable to fopen for read/writing");
2850 switch (child_id
= fork()) {
2852 adios ("fork", "unable to");
2856 argsplit_msgarg(&args
, urlprog
, &program
);
2857 app_msgarg(&args
, e
->eb_url
);
2858 app_msgarg(&args
, NULL
);
2859 dup2(fileno(ce
->ce_fp
), 1);
2860 close(fileno(ce
->ce_fp
));
2861 execvp(program
, args
.msgs
);
2862 fprintf(stderr
, "Unable to exec ");
2868 if (pidXwait(child_id
, NULL
)) {
2876 chmod(cachefile
, cachetype
? m_gmprot() : 0444);
2881 mask
= umask (cachetype
? ~m_gmprot() : 0222);
2882 if ((fp
= fopen(cachefile
, "w"))) {
2884 FILE *gp
= ce
->ce_fp
;
2886 fseeko(gp
, 0, SEEK_SET
);
2888 while ((cc
= fread(buffer
, sizeof(*buffer
),
2889 sizeof(buffer
), gp
)) > 0)
2890 if ((int) fwrite(buffer
, sizeof(*buffer
), cc
, fp
) < cc
) {
2891 advise ("openURL", "fwrite");
2897 admonish(ce
->ce_file
, "error reading");
2898 (void) m_unlink (cachefile
);
2905 fseeko(ce
->ce_fp
, 0, SEEK_SET
);
2906 *file
= ce
->ce_file
;
2907 return fileno(ce
->ce_fp
);
2912 * Stores MD5 digest (in cp, from Content-MD5 header) in ct->c_digest. It
2913 * has to be base64 decoded.
2916 readDigest (CT ct
, char *cp
)
2918 unsigned char *digest
;
2921 if (decodeBase64 (cp
, &digest
, &len
, 0, NULL
) == OK
) {
2922 const size_t maxlen
= sizeof ct
->c_digest
;
2924 if (strlen ((char *) digest
) <= maxlen
) {
2925 memcpy (ct
->c_digest
, digest
, maxlen
);
2930 fprintf (stderr
, "MD5 digest=");
2931 for (i
= 0; i
< maxlen
; ++i
) {
2932 fprintf (stderr
, "%02x", ct
->c_digest
[i
] & 0xff);
2934 fprintf (stderr
, "\n");
2940 fprintf (stderr
, "invalid MD5 digest (got %d octets)\n",
2941 (int) strlen ((char *) digest
));
2951 /* Multipart parts might have content before the first subpart and/or
2952 after the last subpart that hasn't been stored anywhere else, so do
2955 get_leftover_mp_content (CT ct
, int before
/* or after */)
2957 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
2959 bool found_boundary
= false;
2965 char *content
= NULL
;
2967 if (! m
) return NOTOK
;
2970 if (! m
->mp_parts
|| ! m
->mp_parts
->mp_part
) return NOTOK
;
2972 /* Isolate the beginning of this part to the beginning of the
2973 first subpart and save any content between them. */
2974 fseeko (ct
->c_fp
, ct
->c_begin
, SEEK_SET
);
2975 max
= m
->mp_parts
->mp_part
->c_begin
- ct
->c_begin
;
2976 boundary
= concat ("--", m
->mp_start
, NULL
);
2978 struct part
*last_subpart
= NULL
;
2979 struct part
*subpart
;
2981 /* Go to the last subpart to get its end position. */
2982 for (subpart
= m
->mp_parts
; subpart
; subpart
= subpart
->mp_next
) {
2983 last_subpart
= subpart
;
2986 if (last_subpart
== NULL
) return NOTOK
;
2988 /* Isolate the end of the last subpart to the end of this part
2989 and save any content between them. */
2990 fseeko (ct
->c_fp
, last_subpart
->mp_part
->c_end
, SEEK_SET
);
2991 max
= ct
->c_end
- last_subpart
->mp_part
->c_end
;
2992 boundary
= concat ("--", m
->mp_stop
, NULL
);
2995 /* Back up by 1 to pick up the newline. */
2996 while ((gotlen
= getline(&bufp
, &buflen
, ct
->c_fp
)) != -1) {
2998 /* Don't look beyond beginning of first subpart (before) or
2999 next part (after). */
3000 if (read
> max
) bufp
[read
-max
] = '\0';
3003 if (! strcmp (bufp
, boundary
)) {
3004 found_boundary
= true;
3007 if (! found_boundary
&& ! strcmp (bufp
, boundary
)) {
3008 found_boundary
= true;
3013 if ((before
&& ! found_boundary
) || (! before
&& found_boundary
)) {
3015 char *old_content
= content
;
3016 content
= concat (content
, bufp
, NULL
);
3020 ? concat ("\n", bufp
, NULL
)
3021 : concat (bufp
, NULL
);
3026 if (found_boundary
|| read
> max
) break;
3028 if (read
> max
) break;
3032 /* Skip the newline if that's all there is. */
3036 /* Remove trailing newline, except at EOF. */
3037 if ((before
|| ! feof (ct
->c_fp
)) &&
3038 (cp
= content
+ strlen (content
)) > content
&&
3043 if (strlen (content
) > 1) {
3045 m
->mp_content_before
= content
;
3047 m
->mp_content_after
= content
;
3062 ct_type_str (int type
)
3065 case CT_APPLICATION
:
3066 return "application";
3082 return "unknown_type";
3088 ct_subtype_str (int type
, int subtype
)
3091 case CT_APPLICATION
:
3093 case APPLICATION_OCTETS
:
3095 case APPLICATION_POSTSCRIPT
:
3096 return "postscript";
3098 return "unknown_app_subtype";
3102 case MESSAGE_RFC822
:
3104 case MESSAGE_PARTIAL
:
3106 case MESSAGE_EXTERNAL
:
3109 return "unknown_msg_subtype";
3115 case MULTI_ALTERNATE
:
3116 return "alternative";
3119 case MULTI_PARALLEL
:
3124 return "unknown_multipart_subtype";
3135 return "unknown_text_subtype";
3138 return "unknown_type";
3144 ct_str_type (const char *type
)
3146 struct str2init
*s2i
;
3148 for (s2i
= str2cts
; s2i
->si_key
; ++s2i
) {
3149 if (! strcasecmp (type
, s2i
->si_key
)) {
3153 if (! s2i
->si_key
&& ! uprf (type
, "X-")) {
3162 ct_str_subtype (int type
, const char *subtype
)
3167 case CT_APPLICATION
:
3168 for (kv
= SubApplication
; kv
->kv_key
; ++kv
) {
3169 if (! strcasecmp (subtype
, kv
->kv_key
)) {
3173 return kv
->kv_value
;
3175 for (kv
= SubMessage
; kv
->kv_key
; ++kv
) {
3176 if (! strcasecmp (subtype
, kv
->kv_key
)) {
3180 return kv
->kv_value
;
3182 for (kv
= SubMultiPart
; kv
->kv_key
; ++kv
) {
3183 if (! strcasecmp (subtype
, kv
->kv_key
)) {
3187 return kv
->kv_value
;
3189 for (kv
= SubText
; kv
->kv_key
; ++kv
) {
3190 if (! strcasecmp (subtype
, kv
->kv_key
)) {
3194 return kv
->kv_value
;
3201 /* Find the content type and InitFunc for the CT. */
3202 const struct str2init
*
3203 get_ct_init (int type
)
3205 const struct str2init
*sp
;
3207 for (sp
= str2cts
; sp
->si_key
; ++sp
) {
3208 if (type
== sp
->si_val
) {
3217 ce_str (int encoding
)
3223 return "quoted-printable";
3239 /* Find the content type and InitFunc for the content encoding method. */
3240 const struct str2init
*
3241 get_ce_method (const char *method
)
3243 struct str2init
*sp
;
3245 for (sp
= str2ces
; sp
->si_key
; ++sp
) {
3246 if (! strcasecmp (method
, sp
->si_key
)) {
3255 * Parse a series of MIME attributes (or parameters) given a header as
3258 * Arguments include:
3260 * filename - Name of input file (for error messages)
3261 * fieldname - Name of field being processed
3262 * headerp - Pointer to pointer of the beginning of the MIME attributes.
3263 * Updated to point to end of attributes when finished.
3264 * param_head - Pointer to head of parameter list
3265 * param_tail - Pointer to tail of parameter list
3266 * commentp - Pointer to header comment pointer (may be NULL)
3268 * Returns OK if parsing was successful, NOTOK if parsing failed, and
3269 * DONE to indicate a benign error (minor parsing error, but the program
3274 parse_header_attrs (const char *filename
, const char *fieldname
,
3275 char **header_attrp
, PM
*param_head
, PM
*param_tail
,
3278 char *cp
= *header_attrp
;
3284 struct sectlist
*next
;
3290 struct sectlist
*sechead
;
3291 struct parmlist
*next
;
3292 } *pp
, *pp2
, *phead
= NULL
;
3294 while (*cp
== ';') {
3295 char *dp
, *vp
, *up
, *nameptr
, *valptr
, *charset
= NULL
, *lang
= NULL
;
3296 bool encoded
= false;
3297 bool partial
= false;
3298 int len
= 0, index
= 0;
3301 while (isspace ((unsigned char) *cp
))
3305 get_comment (filename
, fieldname
, &cp
, commentp
) == NOTOK
) {
3310 if (! suppress_extraneous_trailing_semicolon_warning
) {
3311 inform("extraneous trailing ';' in message %s's %s: "
3312 "parameter list", filename
, fieldname
);
3317 /* down case the attribute name */
3318 for (dp
= cp
; istoken ((unsigned char) *dp
); dp
++)
3319 *dp
= tolower ((unsigned char) *dp
);
3321 for (up
= dp
; isspace ((unsigned char) *dp
);)
3323 if (dp
== cp
|| *dp
!= '=') {
3324 inform("invalid parameter in message %s's %s: field\n"
3325 " parameter %s (error detected at offset %ld)",
3326 filename
, fieldname
, cp
, (long)(dp
- cp
));
3331 * To handle RFC 2231, we have to deal with the following extensions:
3333 * name*=encoded-value
3334 * name*<N>=part-N-of-a-parameter-value
3335 * name*<N>*=encoded-part-N-of-a-parameter-value
3338 * If there's a * right before the equal sign, it's encoded.
3339 * If there's a * and one or more digits, then it's section N.
3341 * Remember we can have one or the other, or both. cp points to
3342 * beginning of name, up points past the last character in the
3346 for (vp
= cp
; vp
< up
; vp
++) {
3347 if (*vp
== '*' && vp
< up
- 1) {
3351 if (*vp
== '*' && vp
== up
- 1) {
3353 } else if (partial
) {
3354 if (isdigit((unsigned char) *vp
))
3355 index
= *vp
- '0' + index
* 10;
3357 inform("invalid parameter index in message %s's %s: field"
3358 "\n (parameter %s)", filename
, fieldname
, cp
);
3367 * Break out the parameter name and value sections and allocate
3371 nameptr
= mh_xmalloc(len
+ 1);
3372 strncpy(nameptr
, cp
, len
);
3373 nameptr
[len
] = '\0';
3375 for (dp
++; isspace ((unsigned char) *dp
);)
3380 * Single quotes delimit the character set and language tag.
3381 * They are required on the first section (or a complete
3386 while (*vp
!= '\'' && !isspace((unsigned char) *vp
) &&
3392 charset
= mh_xmalloc(len
+ 1);
3393 strncpy(charset
, dp
, len
);
3394 charset
[len
] = '\0';
3400 inform("missing charset in message %s's %s: field\n"
3401 " (parameter %s)", filename
, fieldname
, nameptr
);
3407 while (*vp
!= '\'' && !isspace((unsigned char) *vp
) &&
3414 lang
= mh_xmalloc(len
+ 1);
3415 strncpy(lang
, dp
, len
);
3422 inform("missing language tag in message %s's %s: field\n"
3423 " (parameter %s)", filename
, fieldname
, nameptr
);
3433 * At this point vp should be pointing at the beginning
3434 * of the encoded value/section. Continue until we reach
3435 * the end or get whitespace. But first, calculate the
3436 * length so we can allocate the correct buffer size.
3439 for (vp
= dp
, len
= 0; istoken(*vp
); vp
++) {
3441 if (*(vp
+ 1) == '\0' ||
3442 !isxdigit((unsigned char) *(vp
+ 1)) ||
3443 *(vp
+ 2) == '\0' ||
3444 !isxdigit((unsigned char) *(vp
+ 2))) {
3445 inform("invalid encoded sequence in message %s's %s: field\n"
3446 " (parameter %s)", filename
, fieldname
, nameptr
);
3457 up
= valptr
= mh_xmalloc(len
+ 1);
3459 for (vp
= dp
; istoken(*vp
); vp
++) {
3461 *up
++ = decode_qp(*(vp
+ 1), *(vp
+ 2));
3472 * A "normal" string. If it's got a leading quote, then we
3473 * strip the quotes out. Otherwise go until we reach the end
3474 * or get whitespace. Note we scan it twice; once to get the
3475 * length, then the second time copies it into the destination
3482 for (cp
= dp
+ 1;;) {
3486 inform("invalid quoted-string in message %s's %s: field\n"
3487 " (parameter %s)", filename
, fieldname
, nameptr
);
3507 for (cp
= dp
; istoken (*cp
); cp
++) {
3512 valptr
= mh_xmalloc(len
+ 1);
3516 for (cp
= dp
+ 1, vp
= valptr
, i
= 0; i
< len
; i
++) {
3524 strncpy(valptr
, cp
= dp
, len
);
3532 * If 'partial' is set, we don't allocate a parameter now. We
3533 * put it on the parameter linked list to be reassembled later.
3535 * "phead" points to a list of all parameters we need to reassemble.
3536 * Each parameter has a list of sections. We insert the sections in
3541 for (pp
= phead
; pp
!= NULL
; pp
= pp
->next
) {
3542 if (strcasecmp(nameptr
, pp
->name
) == 0) {
3557 * Insert this into the section linked list
3565 if (pp
->sechead
== NULL
|| pp
->sechead
->index
> index
) {
3566 sp
->next
= pp
->sechead
;
3569 for (sp2
= pp
->sechead
; sp2
!= NULL
; sp2
= sp2
->next
) {
3570 if (sp2
->index
== sp
->index
) {
3571 inform("duplicate index (%d) in message %s's %s: field"
3572 "\n (parameter %s)", sp
->index
, filename
,
3573 fieldname
, nameptr
);
3576 if (sp2
->index
< sp
->index
&&
3577 (sp2
->next
== NULL
|| sp2
->next
->index
> sp
->index
)) {
3578 sp
->next
= sp2
->next
;
3585 inform("Internal error: cannot insert partial param "
3586 "in message %s's %s: field\n (parameter %s)",
3587 filename
, fieldname
, nameptr
);
3593 * Save our charset and lang tags.
3596 if (index
== 0 && encoded
) {
3598 pp
->charset
= charset
;
3603 pm
= add_param(param_head
, param_tail
, nameptr
, valptr
, 1);
3604 pm
->pm_charset
= charset
;
3608 while (isspace ((unsigned char) *cp
))
3612 get_comment (filename
, fieldname
, &cp
, commentp
) == NOTOK
) {
3618 * Now that we're done, reassemble all of the partial parameters.
3621 for (pp
= phead
; pp
!= NULL
; ) {
3625 for (sp
= pp
->sechead
; sp
!= NULL
; sp
= sp
->next
) {
3626 if (sp
->index
!= pindex
++) {
3627 inform("missing section %d for parameter in message "
3628 "%s's %s: field\n (parameter %s)", pindex
- 1,
3629 filename
, fieldname
, pp
->name
);
3635 p
= q
= mh_xmalloc(tlen
+ 1);
3636 for (sp
= pp
->sechead
; sp
!= NULL
; ) {
3637 memcpy(q
, sp
->value
, sp
->len
);
3647 pm
= add_param(param_head
, param_tail
, pp
->name
, p
, 1);
3648 pm
->pm_charset
= pp
->charset
;
3649 pm
->pm_lang
= pp
->lang
;
3660 * Return the charset for a particular content type.
3664 content_charset (CT ct
)
3666 char *ret_charset
= NULL
;
3668 ret_charset
= get_param(ct
->c_ctinfo
.ci_first_pm
, "charset", '?', 0);
3670 return ret_charset
? ret_charset
: mh_xstrdup("US-ASCII");
3675 * Create a string based on a list of output parameters. Assume that this
3676 * parameter string will be appended to an existing header, so start out
3677 * with the separator (;). Perform RFC 2231 encoding when necessary.
3681 output_params(size_t initialwidth
, PM params
, int *offsetout
, int external
)
3683 char *paramout
= NULL
;
3684 char line
[CPERLIN
* 2], *q
;
3685 int curlen
, index
, cont
, encode
, i
;
3686 size_t valoff
, numchars
;
3688 while (params
!= NULL
) {
3694 if (external
&& strcasecmp(params
->pm_name
, "body") == 0)
3697 if (strlen(params
->pm_name
) > CPERLIN
) {
3698 inform("Parameter name \"%s\" is too long", params
->pm_name
);
3703 curlen
= param_len(params
, index
, valoff
, &encode
, &cont
, &numchars
);
3706 * Loop until we get a parameter that fits within a line. We
3707 * assume new lines start with a tab, so check our overflow based
3717 * At this point we're definitely continuing the line, so
3718 * be sure to include the parameter name and section index.
3721 q
+= snprintf(q
, sizeof(line
) - (q
- line
), "%s*%d",
3722 params
->pm_name
, index
);
3725 * Both of these functions do a NUL termination
3729 i
= encode_param(params
, q
, sizeof(line
) - (q
- line
),
3730 numchars
, valoff
, index
);
3732 i
= normal_param(params
, q
, sizeof(line
) - (q
- line
),
3742 curlen
= param_len(params
, index
, valoff
, &encode
, &cont
,
3747 * "line" starts with a ;\n\t, so that doesn't count against
3748 * the length. But add 8 since it starts with a tab; that's
3749 * how we end up with 5.
3752 initialwidth
= strlen(line
) + 5;
3755 * At this point the line should be built, so add it to our
3756 * current output buffer.
3759 paramout
= add(line
, paramout
);
3763 * If this won't fit on the line, start a new one. Save room in
3764 * case we need a semicolon on the end
3767 if (initialwidth
+ curlen
> CPERLIN
- 1) {
3779 * At this point, we're either finishing a continued parameter, or
3780 * we're working on a new one.
3784 q
+= snprintf(q
, sizeof(line
) - (q
- line
), "%s*%d",
3785 params
->pm_name
, index
);
3787 strncpy(q
, params
->pm_name
, sizeof(line
) - (q
- line
));
3792 i
= encode_param(params
, q
, sizeof(line
) - (q
- line
),
3793 strlen(params
->pm_value
+ valoff
), valoff
, index
);
3795 i
= normal_param(params
, q
, sizeof(line
) - (q
- line
),
3796 strlen(params
->pm_value
+ valoff
), valoff
);
3803 paramout
= add(line
, paramout
);
3804 initialwidth
+= strlen(line
);
3806 params
= params
->pm_next
;
3810 *offsetout
= initialwidth
;
3816 * Calculate the size of a parameter.
3820 * pm - The parameter being output
3821 * index - If continuing the parameter, the index of the section
3823 * valueoff - The current offset into the parameter value that we're
3824 * working on (previous sections have consumed valueoff bytes).
3825 * encode - Set if we should perform encoding on this parameter section
3826 * (given that we're consuming bytesfit bytes).
3827 * cont - Set if the remaining data in value will not fit on a single
3828 * line and will need to be continued.
3829 * bytesfit - The number of bytes that we can consume from the parameter
3830 * value and still fit on a completely new line. The
3831 * calculation assumes the new line starts with a tab,
3832 * includes the parameter name and any encoding, and fits
3833 * within CPERLIN bytes. Will always be at least 1.
3837 param_len(PM pm
, int index
, size_t valueoff
, int *encode
, int *cont
,
3840 char *start
= pm
->pm_value
+ valueoff
, *p
, indexchar
[32];
3841 size_t len
= 0, fit
= 0;
3842 int fitlimit
= 0, eightbit
, maxfit
;
3847 * Add up the length. First, start with the parameter name.
3850 len
= strlen(pm
->pm_name
);
3853 * Scan the parameter value and see if we need to do encoding for this
3857 eightbit
= contains8bit(start
, NULL
);
3860 * Determine if we need to encode this section. Encoding is necessary if:
3862 * - There are any 8-bit characters at all and we're on the first
3864 * - There are 8-bit characters within N bytes of our section start.
3865 * N is calculated based on the number of bytes it would take to
3866 * reach CPERLIN. Specifically:
3867 * 8 (starting tab) +
3868 * strlen(param name) +
3869 * 4 ('* for section marker, '=', opening/closing '"')
3871 * is the number of bytes used by everything that isn't part of the
3872 * value. So that gets subtracted from CPERLIN.
3875 snprintf(indexchar
, sizeof(indexchar
), "%d", index
);
3876 maxfit
= CPERLIN
- (12 + len
+ strlen(indexchar
));
3877 if ((eightbit
&& index
== 0) || contains8bit(start
, start
+ maxfit
)) {
3881 len
++; /* Add in equal sign */
3885 * We're using maxfit as a marker for how many characters we can
3886 * fit into the line. Bump it by two because we're not using quotes
3893 * If we don't have a charset or language tag in this parameter,
3897 if (! pm
->pm_charset
) {
3898 pm
->pm_charset
= mh_xstrdup(write_charset_8bit());
3899 if (strcasecmp(pm
->pm_charset
, "US-ASCII") == 0)
3900 die("8-bit characters in parameter \"%s\", but "
3901 "local character set is US-ASCII", pm
->pm_name
);
3904 pm
->pm_lang
= mh_xstrdup(""); /* Default to a blank lang tag */
3906 len
++; /* For the encoding marker */
3909 int enclen
= strlen(pm
->pm_charset
) + strlen(pm
->pm_lang
) + 2;
3914 * We know we definitely need to include an index. maxfit already
3915 * includes the section marker.
3917 len
+= strlen(indexchar
);
3919 for (p
= start
; *p
!= '\0'; p
++) {
3920 if (isparamencode(*p
)) {
3928 * Just so there's no confusion: maxfit is counting OUTPUT
3929 * characters (post-encoding). fit is counting INPUT characters.
3931 if (! fitlimit
&& maxfit
>= 0)
3933 else if (! fitlimit
)
3938 * Calculate the string length, but add room for quoting \
3939 * and " if necessary. Also account for quotes at beginning
3942 for (p
= start
; *p
!= '\0'; p
++) {
3953 if (! fitlimit
&& maxfit
>= 0)
3955 else if (! fitlimit
)
3972 * Output an encoded parameter string.
3976 encode_param(PM pm
, char *output
, size_t len
, size_t valuelen
,
3977 size_t valueoff
, int index
)
3979 size_t outlen
= 0, n
;
3980 char *endptr
= output
+ len
, *p
;
3983 * First, output the marker for an encoded string.
3991 * If the index is 0, output the character set and language tag.
3992 * If theses were NULL, they should have already been filled in
3997 n
= snprintf(output
, len
- outlen
, "%s'%s'", pm
->pm_charset
,
4001 if (output
> endptr
) {
4002 inform("Internal error: parameter buffer overflow");
4008 * Copy over the value, encoding if necessary
4011 p
= pm
->pm_value
+ valueoff
;
4012 while (valuelen
-- > 0) {
4013 if (isparamencode(*p
)) {
4014 n
= snprintf(output
, len
- outlen
, "%%%02X", (unsigned char) *p
++);
4021 if (output
> endptr
) {
4022 inform("Internal error: parameter buffer overflow");
4033 * Output a "normal" parameter, without encoding. Be sure to escape
4034 * quotes and backslashes if necessary.
4038 normal_param(PM pm
, char *output
, size_t len
, size_t valuelen
,
4042 char *endptr
= output
+ len
, *p
;
4048 p
= pm
->pm_value
+ valueoff
;
4050 while (valuelen
-- > 0) {
4061 if (output
> endptr
) {
4062 inform("Internal error: parameter buffer overflow");
4067 if (output
- 2 > endptr
) {
4068 inform("Internal error: parameter buffer overflow");
4079 * Add a parameter to the parameter linked list
4083 add_param(PM
*first
, PM
*last
, char *name
, char *value
, int nocopy
)
4088 pm
->pm_name
= nocopy
? name
: getcpy(name
);
4089 pm
->pm_value
= nocopy
? value
: getcpy(value
);
4092 (*last
)->pm_next
= pm
;
4103 * Either replace a current parameter with a new value, or add the parameter
4104 * to the parameter linked list.
4108 replace_param(PM
*first
, PM
*last
, char *name
, char *value
, int nocopy
)
4112 for (pm
= *first
; pm
!= NULL
; pm
= pm
->pm_next
) {
4113 if (strcasecmp(name
, pm
->pm_name
) == 0) {
4115 * If nocopy is set, it's assumed that we own both name
4116 * and value. We don't need name, so we discard it now.
4121 pm
->pm_value
= nocopy
? value
: getcpy(value
);
4126 return add_param(first
, last
, name
, value
, nocopy
);
4130 * Retrieve a parameter value from a parameter linked list. If the parameter
4131 * value needs converted to the local character set, do that now.
4135 get_param(PM first
, const char *name
, char replace
, int fetchonly
)
4137 while (first
!= NULL
) {
4138 if (strcasecmp(name
, first
->pm_name
) == 0) {
4140 return first
->pm_value
;
4141 return getcpy(get_param_value(first
, replace
));
4143 first
= first
->pm_next
;
4150 * Return a parameter value, converting to the local character set if
4155 get_param_value(PM pm
, char replace
)
4157 static char buffer
[4096]; /* I hope no parameters are larger */
4158 size_t bufsize
= sizeof(buffer
);
4163 ICONV_CONST
char *p
;
4164 #else /* HAVE_ICONV */
4166 #endif /* HAVE_ICONV */
4171 * If we don't have a character set indicated, it's assumed to be
4172 * US-ASCII. If it matches our character set, we don't need to convert
4176 if (!pm
->pm_charset
|| check_charset(pm
->pm_charset
,
4177 strlen(pm
->pm_charset
))) {
4178 return pm
->pm_value
;
4182 * In this case, we need to convert. If we have iconv support, use
4183 * that. Otherwise, go through and simply replace every non-ASCII
4184 * character with the substitution character.
4189 bufsize
= sizeof(buffer
);
4190 utf8
= strcasecmp(pm
->pm_charset
, "UTF-8") == 0;
4192 cd
= iconv_open(get_charset(), pm
->pm_charset
);
4193 if (cd
== (iconv_t
) -1) {
4197 inbytes
= strlen(pm
->pm_value
);
4201 if (iconv(cd
, &p
, &inbytes
, &q
, &bufsize
) == (size_t)-1) {
4202 if (errno
!= EILSEQ
) {
4207 * Reset shift state, substitute our character,
4208 * try to restart conversion.
4211 iconv(cd
, NULL
, NULL
, &q
, &bufsize
);
4224 for (++p
, --inbytes
;
4225 inbytes
> 0 && (((unsigned char) *p
) & 0xc0) == 0x80;
4244 #endif /* HAVE_ICONV */
4247 * Take everything non-ASCII and substitute the replacement character
4251 bufsize
= sizeof(buffer
);
4252 for (p
= pm
->pm_value
; *p
!= '\0' && bufsize
> 1; p
++, q
++, bufsize
--) {
4253 if (isascii((unsigned char) *p
) && isprint((unsigned char) *p
))