3 * mhparse.c -- routines to parse the contents of MIME messages
5 * This code is Copyright (c) 2002, by the authors of nmh. See the
6 * COPYRIGHT file in the root directory of the nmh distribution for
7 * complete copyright information.
12 #include <h/signals.h>
17 #include <h/mhparse.h>
21 #endif /* HAVE_ICONV */
26 extern pid_t xpid
; /* mhshowsbr.c */
29 extern int rcachesw
; /* mhcachesbr.c */
30 extern int wcachesw
; /* mhcachesbr.c */
32 int checksw
= 0; /* check Content-MD5 field */
35 * These are for mhfixmsg to:
36 * 1) Instruct parser not to detect invalid Content-Transfer-Encoding
38 * 2) Suppress the warning about bogus multipart content, and report it.
40 int skip_mp_cte_check
;
41 int suppress_bogus_mp_content_warning
;
45 * Structures for TEXT messages
47 struct k2v SubText
[] = {
48 { "plain", TEXT_PLAIN
},
49 { "richtext", TEXT_RICHTEXT
}, /* defined in RFC-1341 */
50 { "enriched", TEXT_ENRICHED
}, /* defined in RFC-1896 */
51 { NULL
, TEXT_UNKNOWN
} /* this one must be last! */
54 /* Charset[] removed -- yozo. Mon Oct 8 01:03:41 JST 2012 */
57 * Structures for MULTIPART messages
59 struct k2v SubMultiPart
[] = {
60 { "mixed", MULTI_MIXED
},
61 { "alternative", MULTI_ALTERNATE
},
62 { "digest", MULTI_DIGEST
},
63 { "parallel", MULTI_PARALLEL
},
64 { NULL
, MULTI_UNKNOWN
} /* this one must be last! */
68 * Structures for MESSAGE messages
70 struct k2v SubMessage
[] = {
71 { "rfc822", MESSAGE_RFC822
},
72 { "partial", MESSAGE_PARTIAL
},
73 { "external-body", MESSAGE_EXTERNAL
},
74 { NULL
, MESSAGE_UNKNOWN
} /* this one must be last! */
78 * Structure for APPLICATION messages
80 struct k2v SubApplication
[] = {
81 { "octet-stream", APPLICATION_OCTETS
},
82 { "postscript", APPLICATION_POSTSCRIPT
},
83 { NULL
, APPLICATION_UNKNOWN
} /* this one must be last! */
87 * Mapping of names of CTE types in mhbuild directives
89 static struct k2v EncodingType
[] = {
93 { "quoted-printable", CE_QUOTED
},
95 { "base64", CE_BASE64
},
101 int find_cache (CT
, int, int *, char *, char *, int);
104 int part_ok (CT
, int);
105 int type_ok (CT
, int);
106 void content_error (char *, CT
, char *, ...);
109 void free_encoding (CT
, int);
114 static CT
get_content (FILE *, char *, int);
115 static int get_comment (const char *, const char *, char **, char **);
117 static int InitGeneric (CT
);
118 static int InitText (CT
);
119 static int InitMultiPart (CT
);
120 void reverse_parts (CT
);
121 static int InitMessage (CT
);
122 static int InitApplication (CT
);
123 static int init_encoding (CT
, OpenCEFunc
);
124 static unsigned long size_encoding (CT
);
125 static int InitBase64 (CT
);
126 static int openBase64 (CT
, char **);
127 static int InitQuoted (CT
);
128 static int openQuoted (CT
, char **);
129 static int Init7Bit (CT
);
130 static int openExternal (CT
, CT
, CE
, char **, int *);
131 static int InitFile (CT
);
132 static int openFile (CT
, char **);
133 static int InitFTP (CT
);
134 static int openFTP (CT
, char **);
135 static int InitMail (CT
);
136 static int openMail (CT
, char **);
137 static int readDigest (CT
, char *);
138 static int get_leftover_mp_content (CT
, int);
139 static int InitURL (CT
);
140 static int openURL (CT
, char **);
141 static size_t param_len(PM
, int, size_t, int *, int *, size_t *);
142 static size_t encode_param(PM
, char *, size_t, size_t, size_t, int);
143 static size_t normal_param(PM
, char *, size_t, size_t, size_t);
144 static int get_dispo (char *, CT
, int);
146 struct str2init str2cts
[] = {
147 { "application", CT_APPLICATION
, InitApplication
},
148 { "audio", CT_AUDIO
, InitGeneric
},
149 { "image", CT_IMAGE
, InitGeneric
},
150 { "message", CT_MESSAGE
, InitMessage
},
151 { "multipart", CT_MULTIPART
, InitMultiPart
},
152 { "text", CT_TEXT
, InitText
},
153 { "video", CT_VIDEO
, InitGeneric
},
154 { NULL
, CT_EXTENSION
, NULL
}, /* these two must be last! */
155 { NULL
, CT_UNKNOWN
, NULL
},
158 struct str2init str2ces
[] = {
159 { "base64", CE_BASE64
, InitBase64
},
160 { "quoted-printable", CE_QUOTED
, InitQuoted
},
161 { "8bit", CE_8BIT
, Init7Bit
},
162 { "7bit", CE_7BIT
, Init7Bit
},
163 { "binary", CE_BINARY
, Init7Bit
},
164 { NULL
, CE_EXTENSION
, NULL
}, /* these two must be last! */
165 { NULL
, CE_UNKNOWN
, NULL
},
169 * NOTE WELL: si_key MUST NOT have value of NOTOK
171 * si_key is 1 if access method is anonymous.
173 struct str2init str2methods
[] = {
174 { "afs", 1, InitFile
},
175 { "anon-ftp", 1, InitFTP
},
176 { "ftp", 0, InitFTP
},
177 { "local-file", 0, InitFile
},
178 { "mail-server", 0, InitMail
},
179 { "url", 0, InitURL
},
185 pidcheck (int status
)
187 if ((status
& 0xff00) == 0xff00 || (status
& 0x007f) != SIGQUIT
)
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
)
212 * Check if file is actually standard input
214 if ((is_stdin
= !(strcmp (file
, "-")))) {
215 char *tfile
= m_mktemp2(NULL
, invo_name
, NULL
, &fp
);
217 advise("mhparse", "unable to create temporary file in %s",
221 file
= add (tfile
, NULL
);
223 while (fgets (buffer
, sizeof(buffer
), stdin
))
227 if (ferror (stdin
)) {
228 (void) m_unlink (file
);
229 advise ("stdin", "error reading");
233 (void) m_unlink (file
);
234 advise (file
, "error writing");
237 fseek (fp
, 0L, SEEK_SET
);
238 } else if ((fp
= fopen (file
, "r")) == NULL
) {
239 advise (file
, "unable to read");
243 if (!(ct
= get_content (fp
, file
, 1))) {
245 (void) m_unlink (file
);
246 advise (NULL
, "unable to decode %s", file
);
251 ct
->c_unlink
= 1; /* temp file to remove */
255 if (ct
->c_end
== 0L) {
256 fseek (fp
, 0L, SEEK_END
);
257 ct
->c_end
= ftell (fp
);
260 if (ct
->c_ctinitfnx
&& (*ct
->c_ctinitfnx
) (ct
) == NOTOK
) {
272 * Main routine for reading/parsing the headers
273 * of a message content.
275 * toplevel = 1 # we are at the top level of the message
276 * toplevel = 0 # we are inside message type or multipart type
277 * # other than multipart/digest
278 * toplevel = -1 # we are inside multipart/digest
279 * NB: on failure we will fclose(in)!
283 get_content (FILE *in
, char *file
, int toplevel
)
286 char buf
[BUFSIZ
], name
[NAMESZ
];
290 m_getfld_state_t gstate
= 0;
292 /* allocate the content structure */
293 if (!(ct
= (CT
) calloc (1, sizeof(*ct
))))
294 adios (NULL
, "out of memory");
297 ct
->c_file
= add (file
, NULL
);
298 ct
->c_begin
= ftell (ct
->c_fp
) + 1;
301 * Parse the header fields for this
302 * content into a linked list.
304 m_getfld_track_filepos (&gstate
, in
);
305 for (compnum
= 1;;) {
306 int bufsz
= sizeof buf
;
307 switch (state
= m_getfld (&gstate
, name
, buf
, &bufsz
, in
)) {
312 /* get copies of the buffers */
313 np
= add (name
, NULL
);
314 vp
= add (buf
, NULL
);
316 /* if necessary, get rest of field */
317 while (state
== FLDPLUS
) {
319 state
= m_getfld (&gstate
, name
, buf
, &bufsz
, in
);
320 vp
= add (buf
, vp
); /* add to previous value */
323 /* Now add the header data to the list */
324 add_header (ct
, np
, vp
);
326 /* continue, to see if this isn't the last header field */
327 ct
->c_begin
= ftell (in
) + 1;
331 ct
->c_begin
= ftell (in
) - strlen (buf
);
335 ct
->c_begin
= ftell (in
);
340 adios (NULL
, "message format error in component #%d", compnum
);
343 adios (NULL
, "getfld() returned %d", state
);
346 /* break out of the loop */
349 m_getfld_state_destroy (&gstate
);
352 * Read the content headers. We will parse the
353 * MIME related header fields into their various
354 * structures and set internal flags related to
355 * content type/subtype, etc.
358 hp
= ct
->c_first_hf
; /* start at first header field */
360 /* Get MIME-Version field */
361 if (!strcasecmp (hp
->name
, VRSN_FIELD
)) {
366 advise (NULL
, "message %s has multiple %s: fields",
367 ct
->c_file
, VRSN_FIELD
);
370 ct
->c_vrsn
= add (hp
->value
, NULL
);
372 /* Now, cleanup this field */
375 while (isspace ((unsigned char) *cp
))
377 for (dp
= strchr(cp
, '\n'); dp
; dp
= strchr(dp
, '\n'))
379 for (dp
= cp
+ strlen (cp
) - 1; dp
>= cp
; dp
--)
380 if (!isspace ((unsigned char) *dp
))
384 fprintf (stderr
, "%s: %s\n", VRSN_FIELD
, cp
);
387 get_comment (ct
->c_file
, VRSN_FIELD
, &cp
, NULL
) == NOTOK
)
390 for (dp
= cp
; istoken (*dp
); dp
++)
394 ucmp
= !strcasecmp (cp
, VRSN_VALUE
);
397 admonish (NULL
, "message %s has unknown value for %s: field (%s)",
398 ct
->c_file
, VRSN_FIELD
, cp
);
401 else if (!strcasecmp (hp
->name
, TYPE_FIELD
)) {
402 /* Get Content-Type field */
403 struct str2init
*s2i
;
404 CI ci
= &ct
->c_ctinfo
;
406 /* Check if we've already seen a Content-Type header */
408 advise (NULL
, "message %s has multiple %s: fields",
409 ct
->c_file
, TYPE_FIELD
);
413 /* Parse the Content-Type field */
414 if (get_ctinfo (hp
->value
, ct
, 0) == NOTOK
)
418 * Set the Init function and the internal
419 * flag for this content type.
421 for (s2i
= str2cts
; s2i
->si_key
; s2i
++)
422 if (!strcasecmp (ci
->ci_type
, s2i
->si_key
))
424 if (!s2i
->si_key
&& !uprf (ci
->ci_type
, "X-"))
426 ct
->c_type
= s2i
->si_val
;
427 ct
->c_ctinitfnx
= s2i
->si_init
;
429 else if (!strcasecmp (hp
->name
, ENCODING_FIELD
)) {
430 /* Get Content-Transfer-Encoding field */
432 struct str2init
*s2i
;
435 * Check if we've already seen the
436 * Content-Transfer-Encoding field
439 advise (NULL
, "message %s has multiple %s: fields",
440 ct
->c_file
, ENCODING_FIELD
);
444 /* get copy of this field */
445 ct
->c_celine
= cp
= add (hp
->value
, NULL
);
447 while (isspace ((unsigned char) *cp
))
449 for (dp
= cp
; istoken (*dp
); dp
++)
455 * Find the internal flag and Init function
456 * for this transfer encoding.
458 for (s2i
= str2ces
; s2i
->si_key
; s2i
++)
459 if (!strcasecmp (cp
, s2i
->si_key
))
461 if (!s2i
->si_key
&& !uprf (cp
, "X-"))
464 ct
->c_encoding
= s2i
->si_val
;
466 /* Call the Init function for this encoding */
467 if (s2i
->si_init
&& (*s2i
->si_init
) (ct
) == NOTOK
)
470 else if (!strcasecmp (hp
->name
, MD5_FIELD
)) {
471 /* Get Content-MD5 field */
477 if (ct
->c_digested
) {
478 advise (NULL
, "message %s has multiple %s: fields",
479 ct
->c_file
, MD5_FIELD
);
483 ep
= cp
= add (hp
->value
, NULL
); /* get a copy */
485 while (isspace ((unsigned char) *cp
))
487 for (dp
= strchr(cp
, '\n'); dp
; dp
= strchr(dp
, '\n'))
489 for (dp
= cp
+ strlen (cp
) - 1; dp
>= cp
; dp
--)
490 if (!isspace ((unsigned char) *dp
))
494 fprintf (stderr
, "%s: %s\n", MD5_FIELD
, cp
);
497 get_comment (ct
->c_file
, MD5_FIELD
, &cp
, NULL
) == NOTOK
) {
502 for (dp
= cp
; *dp
&& !isspace ((unsigned char) *dp
); dp
++)
510 else if (!strcasecmp (hp
->name
, ID_FIELD
)) {
511 /* Get Content-ID field */
512 ct
->c_id
= add (hp
->value
, ct
->c_id
);
514 else if (!strcasecmp (hp
->name
, DESCR_FIELD
)) {
515 /* Get Content-Description field */
516 ct
->c_descr
= add (hp
->value
, ct
->c_descr
);
518 else if (!strcasecmp (hp
->name
, DISPO_FIELD
)) {
519 /* Get Content-Disposition field */
520 if (get_dispo(hp
->value
, ct
, 0) == NOTOK
)
525 hp
= hp
->next
; /* next header field */
529 * Check if we saw a Content-Type field.
530 * If not, then assign a default value for
531 * it, and the Init function.
535 * If we are inside a multipart/digest message,
536 * so default type is message/rfc822
539 if (get_ctinfo ("message/rfc822", ct
, 0) == NOTOK
)
541 ct
->c_type
= CT_MESSAGE
;
542 ct
->c_ctinitfnx
= InitMessage
;
545 * Else default type is text/plain
547 if (get_ctinfo ("text/plain", ct
, 0) == NOTOK
)
549 ct
->c_type
= CT_TEXT
;
550 ct
->c_ctinitfnx
= InitText
;
554 /* Use default Transfer-Encoding, if necessary */
556 ct
->c_encoding
= CE_7BIT
;
569 * small routine to add header field to list
573 add_header (CT ct
, char *name
, char *value
)
577 /* allocate header field structure */
578 hp
= mh_xmalloc (sizeof(*hp
));
580 /* link data into header structure */
585 /* link header structure into the list */
586 if (ct
->c_first_hf
== NULL
) {
587 ct
->c_first_hf
= hp
; /* this is the first */
590 ct
->c_last_hf
->next
= hp
; /* add it to the end */
598 /* Make sure that buf contains at least one appearance of name,
599 followed by =. If not, insert both name and value, just after
600 first semicolon, if any. Note that name should not contain a
601 trailing =. And quotes will be added around the value. Typical
602 usage: make sure that a Content-Disposition header contains
603 filename="foo". If it doesn't and value does, use value from
606 incl_name_value (char *buf
, char *name
, char *value
) {
609 /* Assume that name is non-null. */
611 char *name_plus_equal
= concat (name
, "=", NULL
);
613 if (! strstr (buf
, name_plus_equal
)) {
615 char *cp
, *prefix
, *suffix
;
617 /* Trim trailing space, esp. newline. */
618 for (cp
= &buf
[strlen (buf
) - 1];
619 cp
>= buf
&& isspace ((unsigned char) *cp
);
624 insertion
= concat ("; ", name
, "=", "\"", value
, "\"", NULL
);
626 /* Insert at first semicolon, if any. If none, append to
628 prefix
= add (buf
, NULL
);
629 if ((cp
= strchr (prefix
, ';'))) {
630 suffix
= concat (cp
, NULL
);
632 newbuf
= concat (prefix
, insertion
, suffix
, "\n", NULL
);
636 newbuf
= concat (buf
, insertion
, "\n", NULL
);
644 free (name_plus_equal
);
650 /* Extract just name_suffix="foo", if any, from value. If there isn't
651 one, return the entire value. Note that, for example, a name_suffix
652 of name will match filename="foo", and return foo. */
654 extract_name_value (char *name_suffix
, char *value
) {
655 char *extracted_name_value
= value
;
656 char *name_suffix_plus_quote
= concat (name_suffix
, "=\"", NULL
);
657 char *name_suffix_equals
= strstr (value
, name_suffix_plus_quote
);
660 free (name_suffix_plus_quote
);
661 if (name_suffix_equals
) {
662 char *name_suffix_begin
;
665 for (cp
= name_suffix_equals
; *cp
!= '"'; ++cp
) /* empty */;
666 name_suffix_begin
= ++cp
;
667 /* Find second \". */
668 for (; *cp
!= '"'; ++cp
) /* empty */;
670 extracted_name_value
= mh_xmalloc (cp
- name_suffix_begin
+ 1);
671 memcpy (extracted_name_value
,
673 cp
- name_suffix_begin
);
674 extracted_name_value
[cp
- name_suffix_begin
] = '\0';
677 return extracted_name_value
;
681 * Parse Content-Type line and (if `magic' is non-zero) mhbuild composition
682 * directives. Fills in the information of the CTinfo structure.
685 get_ctinfo (char *cp
, CT ct
, int magic
)
694 /* store copy of Content-Type line */
695 cp
= ct
->c_ctline
= add (cp
, NULL
);
697 while (isspace ((unsigned char) *cp
)) /* trim leading spaces */
700 /* change newlines to spaces */
701 for (dp
= strchr(cp
, '\n'); dp
; dp
= strchr(dp
, '\n'))
704 /* trim trailing spaces */
705 for (dp
= cp
+ strlen (cp
) - 1; dp
>= cp
; dp
--)
706 if (!isspace ((unsigned char) *dp
))
711 fprintf (stderr
, "%s: %s\n", TYPE_FIELD
, cp
);
713 if (*cp
== '(' && get_comment (ct
->c_file
, TYPE_FIELD
, &cp
,
714 &ci
->ci_comment
) == NOTOK
)
717 for (dp
= cp
; istoken (*dp
); dp
++)
720 ci
->ci_type
= add (cp
, NULL
); /* store content type */
724 advise (NULL
, "invalid %s: field in message %s (empty type)",
725 TYPE_FIELD
, ct
->c_file
);
729 /* down case the content type string */
730 for (dp
= ci
->ci_type
; *dp
; dp
++)
731 if (isalpha((unsigned char) *dp
) && isupper ((unsigned char) *dp
))
732 *dp
= tolower ((unsigned char) *dp
);
734 while (isspace ((unsigned char) *cp
))
737 if (*cp
== '(' && get_comment (ct
->c_file
, TYPE_FIELD
, &cp
,
738 &ci
->ci_comment
) == NOTOK
)
743 ci
->ci_subtype
= add ("", NULL
);
748 while (isspace ((unsigned char) *cp
))
751 if (*cp
== '(' && get_comment (ct
->c_file
, TYPE_FIELD
, &cp
,
752 &ci
->ci_comment
) == NOTOK
)
755 for (dp
= cp
; istoken (*dp
); dp
++)
758 ci
->ci_subtype
= add (cp
, NULL
); /* store the content subtype */
761 if (!*ci
->ci_subtype
) {
763 "invalid %s: field in message %s (empty subtype for \"%s\")",
764 TYPE_FIELD
, ct
->c_file
, ci
->ci_type
);
768 /* down case the content subtype string */
769 for (dp
= ci
->ci_subtype
; *dp
; dp
++)
770 if (isalpha((unsigned char) *dp
) && isupper ((unsigned char) *dp
))
771 *dp
= tolower ((unsigned char) *dp
);
774 while (isspace ((unsigned char) *cp
))
777 if (*cp
== '(' && get_comment (ct
->c_file
, TYPE_FIELD
, &cp
,
778 &ci
->ci_comment
) == NOTOK
)
781 if ((status
= parse_header_attrs (ct
->c_file
, TYPE_FIELD
, &cp
,
782 &ci
->ci_first_pm
, &ci
->ci_last_pm
,
783 &ci
->ci_comment
)) != OK
) {
784 return status
== NOTOK
? NOTOK
: OK
;
788 * Get any <Content-Id> given in buffer
790 if (magic
&& *cp
== '<') {
795 if (!(dp
= strchr(ct
->c_id
= ++cp
, '>'))) {
796 advise (NULL
, "invalid ID in message %s", ct
->c_file
);
802 ct
->c_id
= concat ("<", ct
->c_id
, ">\n", NULL
);
808 while (isspace ((unsigned char) *cp
))
813 * Get any [Content-Description] given in buffer.
815 if (magic
&& *cp
== '[') {
817 for (dp
= cp
+ strlen (cp
) - 1; dp
>= cp
; dp
--)
821 advise (NULL
, "invalid description in message %s", ct
->c_file
);
829 ct
->c_descr
= concat (ct
->c_descr
, "\n", NULL
);
835 while (isspace ((unsigned char) *cp
))
840 * Get any {Content-Disposition} given in buffer.
842 if (magic
&& *cp
== '{') {
844 for (dp
= cp
+ strlen (cp
) - 1; dp
>= cp
; dp
--)
848 advise (NULL
, "invalid disposition in message %s", ct
->c_file
);
856 if (get_dispo(cp
, ct
, 1) != OK
)
862 while (isspace ((unsigned char) *cp
))
867 * Get any extension directives (right now just the content transfer
868 * encoding, but maybe others) that we care about.
871 if (magic
&& *cp
== '*') {
873 * See if it's a CTE we match on
878 while (*cp
!= '\0' && ! isspace((unsigned char) *cp
))
882 advise (NULL
, "invalid null transfer encoding specification");
889 ct
->c_reqencoding
= CE_UNKNOWN
;
891 for (kv
= EncodingType
; kv
->kv_key
; kv
++) {
892 if (strcasecmp(kv
->kv_key
, dp
) == 0) {
893 ct
->c_reqencoding
= kv
->kv_value
;
898 if (ct
->c_reqencoding
== CE_UNKNOWN
) {
899 advise (NULL
, "invalid CTE specification: \"%s\"", dp
);
903 while (isspace ((unsigned char) *cp
))
908 * Check if anything is left over
912 ci
->ci_magic
= add (cp
, NULL
);
914 /* If there is a Content-Disposition header and it doesn't
915 have a *filename=, extract it from the magic contents.
916 The r1bindex call skips any leading directory
920 incl_name_value (ct
->c_dispo
,
922 r1bindex (extract_name_value ("name",
929 "extraneous information in message %s's %s: field\n%*s(%s)",
930 ct
->c_file
, TYPE_FIELD
, strlen(invo_name
) + 2, "", cp
);
938 * Parse out a Content-Disposition header. A lot of this is cribbed from
942 get_dispo (char *cp
, CT ct
, int buildflag
)
944 char *dp
, *dispoheader
;
949 * Save the whole copy of the Content-Disposition header, unless we're
950 * processing a mhbuild directive. A NULL c_dispo will be a flag to
951 * mhbuild that the disposition header needs to be generated at that
955 dispoheader
= cp
= add(cp
, NULL
);
957 while (isspace ((unsigned char) *cp
)) /* trim leading spaces */
960 /* change newlines to spaces */
961 for (dp
= strchr(cp
, '\n'); dp
; dp
= strchr(dp
, '\n'))
964 /* trim trailing spaces */
965 for (dp
= cp
+ strlen (cp
) - 1; dp
>= cp
; dp
--)
966 if (!isspace ((unsigned char) *dp
))
971 fprintf (stderr
, "%s: %s\n", DISPO_FIELD
, cp
);
973 if (*cp
== '(' && get_comment (ct
->c_file
, DISPO_FIELD
, &cp
, NULL
) ==
979 for (dp
= cp
; istoken (*dp
); dp
++)
982 ct
->c_dispo_type
= add (cp
, NULL
); /* store disposition type */
985 if (*cp
== '(' && get_comment (ct
->c_file
, DISPO_FIELD
, &cp
, NULL
) == NOTOK
)
988 if ((status
= parse_header_attrs (ct
->c_file
, DISPO_FIELD
, &cp
,
989 &ct
->c_dispo_first
, &ct
->c_dispo_last
,
991 if (status
== NOTOK
) {
997 "extraneous information in message %s's %s: field\n%*s(%s)",
998 ct
->c_file
, DISPO_FIELD
, strlen(invo_name
) + 2, "", cp
);
1004 ct
->c_dispo
= dispoheader
;
1011 get_comment (const char *filename
, const char *fieldname
, char **ap
,
1016 char c
, buffer
[BUFSIZ
], *dp
;
1023 switch (c
= *cp
++) {
1026 advise (NULL
, "invalid comment in message %s's %s: field",
1027 filename
, fieldname
);
1032 if ((c
= *cp
++) == '\0')
1055 if ((dp
= *commentp
)) {
1056 *commentp
= concat (dp
, " ", buffer
, NULL
);
1059 *commentp
= add (buffer
, NULL
);
1063 while (isspace ((unsigned char) *cp
))
1074 * Handles content types audio, image, and video.
1075 * There's not much to do right here.
1083 return OK
; /* not much to do here */
1094 char buffer
[BUFSIZ
];
1100 CI ci
= &ct
->c_ctinfo
;
1102 /* check for missing subtype */
1103 if (!*ci
->ci_subtype
)
1104 ci
->ci_subtype
= add ("plain", ci
->ci_subtype
);
1107 for (kv
= SubText
; kv
->kv_key
; kv
++)
1108 if (!strcasecmp (ci
->ci_subtype
, kv
->kv_key
))
1110 ct
->c_subtype
= kv
->kv_value
;
1112 /* allocate text character set structure */
1113 if ((t
= (struct text
*) calloc (1, sizeof(*t
))) == NULL
)
1114 adios (NULL
, "out of memory");
1115 ct
->c_ctparams
= (void *) t
;
1117 /* scan for charset parameter */
1118 for (pm
= ci
->ci_first_pm
; pm
; pm
= pm
->pm_next
)
1119 if (!strcasecmp (pm
->pm_name
, "charset"))
1122 /* check if content specified a character set */
1124 chset
= pm
->pm_value
;
1125 t
->tx_charset
= CHARSET_SPECIFIED
;
1127 t
->tx_charset
= CHARSET_UNSPECIFIED
;
1131 * If we can not handle character set natively,
1132 * then check profile for string to modify the
1133 * terminal or display method.
1135 * termproc is for mhshow, though mhlist -debug prints it, too.
1137 if (chset
!= NULL
&& !check_charset (chset
, strlen (chset
))) {
1138 snprintf (buffer
, sizeof(buffer
), "%s-charset-%s", invo_name
, chset
);
1139 if ((cp
= context_find (buffer
)))
1140 ct
->c_termproc
= getcpy (cp
);
1152 InitMultiPart (CT ct
)
1158 char *bp
, buffer
[BUFSIZ
];
1159 struct multipart
*m
;
1161 struct part
*part
, **next
;
1162 CI ci
= &ct
->c_ctinfo
;
1167 * The encoding for multipart messages must be either
1168 * 7bit, 8bit, or binary (per RFC2045).
1170 if (! skip_mp_cte_check
&& ct
->c_encoding
!= CE_7BIT
&&
1171 ct
->c_encoding
!= CE_8BIT
&& ct
->c_encoding
!= CE_BINARY
) {
1172 /* Copy the Content-Transfer-Encoding header field body so we can
1173 remove any trailing whitespace and leading blanks from it. */
1174 char *cte
= add (ct
->c_celine
? ct
->c_celine
: "(null)", NULL
);
1176 bp
= cte
+ strlen (cte
) - 1;
1177 while (bp
>= cte
&& isspace ((unsigned char) *bp
)) *bp
-- = '\0';
1178 for (bp
= cte
; *bp
&& isblank ((unsigned char) *bp
); ++bp
) continue;
1181 "\"%s/%s\" type in message %s must be encoded in\n"
1182 "7bit, 8bit, or binary, per RFC 2045 (6.4). One workaround "
1183 "is to\nmanually edit the file and change the \"%s\"\n"
1184 "Content-Transfer-Encoding to one of those. For now",
1185 ci
->ci_type
, ci
->ci_subtype
, ct
->c_file
, bp
);
1192 for (kv
= SubMultiPart
; kv
->kv_key
; kv
++)
1193 if (!strcasecmp (ci
->ci_subtype
, kv
->kv_key
))
1195 ct
->c_subtype
= kv
->kv_value
;
1198 * Check for "boundary" parameter, which is
1199 * required for multipart messages.
1202 for (pm
= ci
->ci_first_pm
; pm
; pm
= pm
->pm_next
) {
1203 if (!strcasecmp (pm
->pm_name
, "boundary")) {
1209 /* complain if boundary parameter is missing */
1212 "a \"boundary\" parameter is mandatory for \"%s/%s\" type in message %s's %s: field",
1213 ci
->ci_type
, ci
->ci_subtype
, ct
->c_file
, TYPE_FIELD
);
1217 /* allocate primary structure for multipart info */
1218 if ((m
= (struct multipart
*) calloc (1, sizeof(*m
))) == NULL
)
1219 adios (NULL
, "out of memory");
1220 ct
->c_ctparams
= (void *) m
;
1222 /* check if boundary parameter contains only whitespace characters */
1223 for (cp
= bp
; isspace ((unsigned char) *cp
); cp
++)
1226 advise (NULL
, "invalid \"boundary\" parameter for \"%s/%s\" type in message %s's %s: field",
1227 ci
->ci_type
, ci
->ci_subtype
, ct
->c_file
, TYPE_FIELD
);
1231 /* remove trailing whitespace from boundary parameter */
1232 for (cp
= bp
, dp
= cp
+ strlen (cp
) - 1; dp
> cp
; dp
--)
1233 if (!isspace ((unsigned char) *dp
))
1237 /* record boundary separators */
1238 m
->mp_start
= concat (bp
, "\n", NULL
);
1239 m
->mp_stop
= concat (bp
, "--\n", NULL
);
1241 if (!ct
->c_fp
&& (ct
->c_fp
= fopen (ct
->c_file
, "r")) == NULL
) {
1242 advise (ct
->c_file
, "unable to open for reading");
1246 fseek (fp
= ct
->c_fp
, pos
= ct
->c_begin
, SEEK_SET
);
1248 next
= &m
->mp_parts
;
1252 while (fgets (buffer
, sizeof(buffer
) - 1, fp
)) {
1256 pos
+= strlen (buffer
);
1257 if (buffer
[0] != '-' || buffer
[1] != '-')
1260 if (strcmp (buffer
+ 2, m
->mp_start
))
1263 if ((part
= (struct part
*) calloc (1, sizeof(*part
))) == NULL
)
1264 adios (NULL
, "out of memory");
1266 next
= &part
->mp_next
;
1268 if (!(p
= get_content (fp
, ct
->c_file
,
1269 ct
->c_subtype
== MULTI_DIGEST
? -1 : 0))) {
1276 fseek (fp
, pos
, SEEK_SET
);
1279 if (strcmp (buffer
+ 2, m
->mp_start
) == 0) {
1283 p
->c_end
= ftell(fp
) - (strlen(buffer
) + 1);
1284 if (p
->c_end
< p
->c_begin
)
1285 p
->c_begin
= p
->c_end
;
1290 if (strcmp (buffer
+ 2, m
->mp_stop
) == 0)
1296 if (! suppress_bogus_mp_content_warning
) {
1297 advise (NULL
, "bogus multipart content in message %s", ct
->c_file
);
1299 bogus_mp_content
= 1;
1301 if (!inout
&& part
) {
1303 p
->c_end
= ct
->c_end
;
1305 if (p
->c_begin
>= p
->c_end
) {
1306 for (next
= &m
->mp_parts
; *next
!= part
;
1307 next
= &((*next
)->mp_next
))
1311 free ((char *) part
);
1316 /* reverse the order of the parts for multipart/alternative */
1317 if (ct
->c_subtype
== MULTI_ALTERNATE
)
1321 * label all subparts with part number, and
1322 * then initialize the content of the subpart.
1327 char partnam
[BUFSIZ
];
1330 snprintf (partnam
, sizeof(partnam
), "%s.", ct
->c_partno
);
1331 pp
= partnam
+ strlen (partnam
);
1336 for (part
= m
->mp_parts
, partnum
= 1; part
;
1337 part
= part
->mp_next
, partnum
++) {
1340 sprintf (pp
, "%d", partnum
);
1341 p
->c_partno
= add (partnam
, NULL
);
1343 /* initialize the content of the subparts */
1344 if (p
->c_ctinitfnx
&& (*p
->c_ctinitfnx
) (p
) == NOTOK
) {
1352 get_leftover_mp_content (ct
, 1);
1353 get_leftover_mp_content (ct
, 0);
1362 * reverse the order of the parts of a multipart/alternative
1366 reverse_parts (CT ct
)
1368 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
1372 /* Reverse the order of its parts by walking the mp_parts list
1373 and pushing each node to the front. */
1374 for (part
= m
->mp_parts
, m
->mp_parts
= NULL
; part
; part
= next
) {
1375 next
= part
->mp_next
;
1376 part
->mp_next
= m
->mp_parts
;
1390 CI ci
= &ct
->c_ctinfo
;
1392 if ((ct
->c_encoding
!= CE_7BIT
) && (ct
->c_encoding
!= CE_8BIT
)) {
1394 "\"%s/%s\" type in message %s should be encoded in 7bit or 8bit",
1395 ci
->ci_type
, ci
->ci_subtype
, ct
->c_file
);
1399 /* check for missing subtype */
1400 if (!*ci
->ci_subtype
)
1401 ci
->ci_subtype
= add ("rfc822", ci
->ci_subtype
);
1404 for (kv
= SubMessage
; kv
->kv_key
; kv
++)
1405 if (!strcasecmp (ci
->ci_subtype
, kv
->kv_key
))
1407 ct
->c_subtype
= kv
->kv_value
;
1409 switch (ct
->c_subtype
) {
1410 case MESSAGE_RFC822
:
1413 case MESSAGE_PARTIAL
:
1418 if ((p
= (struct partial
*) calloc (1, sizeof(*p
))) == NULL
)
1419 adios (NULL
, "out of memory");
1420 ct
->c_ctparams
= (void *) p
;
1422 /* scan for parameters "id", "number", and "total" */
1423 for (pm
= ci
->ci_first_pm
; pm
; pm
= pm
->pm_next
) {
1424 if (!strcasecmp (pm
->pm_name
, "id")) {
1425 p
->pm_partid
= add (pm
->pm_value
, NULL
);
1428 if (!strcasecmp (pm
->pm_name
, "number")) {
1429 if (sscanf (pm
->pm_value
, "%d", &p
->pm_partno
) != 1
1430 || p
->pm_partno
< 1) {
1433 "invalid %s parameter for \"%s/%s\" type in message %s's %s field",
1434 pm
->pm_name
, ci
->ci_type
, ci
->ci_subtype
,
1435 ct
->c_file
, TYPE_FIELD
);
1440 if (!strcasecmp (pm
->pm_name
, "total")) {
1441 if (sscanf (pm
->pm_value
, "%d", &p
->pm_maxno
) != 1
1450 || (p
->pm_maxno
&& p
->pm_partno
> p
->pm_maxno
)) {
1452 "invalid parameters for \"%s/%s\" type in message %s's %s field",
1453 ci
->ci_type
, ci
->ci_subtype
,
1454 ct
->c_file
, TYPE_FIELD
);
1460 case MESSAGE_EXTERNAL
:
1467 if ((e
= (struct exbody
*) calloc (1, sizeof(*e
))) == NULL
)
1468 adios (NULL
, "out of memory");
1469 ct
->c_ctparams
= (void *) e
;
1472 && (ct
->c_fp
= fopen (ct
->c_file
, "r")) == NULL
) {
1473 advise (ct
->c_file
, "unable to open for reading");
1477 fseek (fp
= ct
->c_fp
, ct
->c_begin
, SEEK_SET
);
1479 if (!(p
= get_content (fp
, ct
->c_file
, 0))) {
1487 p
->c_ceopenfnx
= NULL
;
1488 if ((exresult
= params_external (ct
, 0)) != NOTOK
1489 && p
->c_ceopenfnx
== openMail
) {
1493 if ((size
= ct
->c_end
- p
->c_begin
) <= 0) {
1495 content_error (NULL
, ct
,
1496 "empty body for access-type=mail-server");
1500 e
->eb_body
= bp
= mh_xmalloc ((unsigned) size
);
1501 fseek (p
->c_fp
, p
->c_begin
, SEEK_SET
);
1503 switch (cc
= fread (bp
, sizeof(*bp
), size
, p
->c_fp
)) {
1505 adios ("failed", "fread");
1508 adios (NULL
, "unexpected EOF from fread");
1511 bp
+= cc
, size
-= cc
;
1518 p
->c_end
= p
->c_begin
;
1523 if (exresult
== NOTOK
)
1525 if (e
->eb_flags
== NOTOK
)
1528 switch (p
->c_type
) {
1533 if (p
->c_subtype
!= MESSAGE_RFC822
)
1537 e
->eb_partno
= ct
->c_partno
;
1539 (*p
->c_ctinitfnx
) (p
);
1554 params_external (CT ct
, int composing
)
1557 struct exbody
*e
= (struct exbody
*) ct
->c_ctparams
;
1558 CI ci
= &ct
->c_ctinfo
;
1560 ct
->c_ceopenfnx
= NULL
;
1561 for (pm
= ci
->ci_first_pm
; pm
; pm
= pm
->pm_next
) {
1562 if (!strcasecmp (pm
->pm_name
, "access-type")) {
1563 struct str2init
*s2i
;
1564 CT p
= e
->eb_content
;
1566 for (s2i
= str2methods
; s2i
->si_key
; s2i
++)
1567 if (!strcasecmp (pm
->pm_value
, s2i
->si_key
))
1570 e
->eb_access
= pm
->pm_value
;
1571 e
->eb_flags
= NOTOK
;
1572 p
->c_encoding
= CE_EXTERNAL
;
1575 e
->eb_access
= s2i
->si_key
;
1576 e
->eb_flags
= s2i
->si_val
;
1577 p
->c_encoding
= CE_EXTERNAL
;
1579 /* Call the Init function for this external type */
1580 if ((*s2i
->si_init
)(p
) == NOTOK
)
1584 if (!strcasecmp (pm
->pm_name
, "name")) {
1585 e
->eb_name
= pm
->pm_value
;
1588 if (!strcasecmp (pm
->pm_name
, "permission")) {
1589 e
->eb_permission
= pm
->pm_value
;
1592 if (!strcasecmp (pm
->pm_name
, "site")) {
1593 e
->eb_site
= pm
->pm_value
;
1596 if (!strcasecmp (pm
->pm_name
, "directory")) {
1597 e
->eb_dir
= pm
->pm_value
;
1600 if (!strcasecmp (pm
->pm_name
, "mode")) {
1601 e
->eb_mode
= pm
->pm_value
;
1604 if (!strcasecmp (pm
->pm_name
, "size")) {
1605 sscanf (pm
->pm_value
, "%lu", &e
->eb_size
);
1608 if (!strcasecmp (pm
->pm_name
, "server")) {
1609 e
->eb_server
= pm
->pm_value
;
1612 if (!strcasecmp (pm
->pm_name
, "subject")) {
1613 e
->eb_subject
= pm
->pm_value
;
1616 if (!strcasecmp (pm
->pm_name
, "url")) {
1618 * According to RFC 2017, we have to remove all whitespace from
1622 char *u
, *p
= pm
->pm_value
;
1623 e
->eb_url
= u
= mh_xmalloc(strlen(pm
->pm_value
) + 1);
1625 for (; *p
!= '\0'; p
++) {
1626 if (! isspace((unsigned char) *p
))
1633 if (composing
&& !strcasecmp (pm
->pm_name
, "body")) {
1634 e
->eb_body
= getcpy (pm
->pm_value
);
1639 if (!e
->eb_access
) {
1641 "invalid parameters for \"%s/%s\" type in message %s's %s field",
1642 ci
->ci_type
, ci
->ci_subtype
, ct
->c_file
, TYPE_FIELD
);
1655 InitApplication (CT ct
)
1658 CI ci
= &ct
->c_ctinfo
;
1661 for (kv
= SubApplication
; kv
->kv_key
; kv
++)
1662 if (!strcasecmp (ci
->ci_subtype
, kv
->kv_key
))
1664 ct
->c_subtype
= kv
->kv_value
;
1671 * TRANSFER ENCODINGS
1675 init_encoding (CT ct
, OpenCEFunc openfnx
)
1677 ct
->c_ceopenfnx
= openfnx
;
1678 ct
->c_ceclosefnx
= close_encoding
;
1679 ct
->c_cesizefnx
= size_encoding
;
1686 close_encoding (CT ct
)
1688 CE ce
= &ct
->c_cefile
;
1697 static unsigned long
1698 size_encoding (CT ct
)
1703 CE ce
= &ct
->c_cefile
;
1706 if (ce
->ce_fp
&& fstat (fileno (ce
->ce_fp
), &st
) != NOTOK
)
1707 return (long) st
.st_size
;
1710 if (stat (ce
->ce_file
, &st
) != NOTOK
)
1711 return (long) st
.st_size
;
1716 if (ct
->c_encoding
== CE_EXTERNAL
)
1717 return (ct
->c_end
- ct
->c_begin
);
1720 if ((fd
= (*ct
->c_ceopenfnx
) (ct
, &file
)) == NOTOK
)
1721 return (ct
->c_end
- ct
->c_begin
);
1723 if (fstat (fd
, &st
) != NOTOK
)
1724 size
= (long) st
.st_size
;
1728 (*ct
->c_ceclosefnx
) (ct
);
1737 static unsigned char b642nib
[0x80] = {
1738 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1739 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1740 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1741 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1742 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1743 0xff, 0xff, 0xff, 0x3e, 0xff, 0xff, 0xff, 0x3f,
1744 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b,
1745 0x3c, 0x3d, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1746 0xff, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
1747 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e,
1748 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16,
1749 0x17, 0x18, 0x19, 0xff, 0xff, 0xff, 0xff, 0xff,
1750 0xff, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20,
1751 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28,
1752 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30,
1753 0x31, 0x32, 0x33, 0xff, 0xff, 0xff, 0xff, 0xff
1760 return init_encoding (ct
, openBase64
);
1765 openBase64 (CT ct
, char **file
)
1767 int bitno
, cc
, digested
;
1768 int fd
, len
, skip
, own_ct_fp
= 0, text
= ct
->c_type
== CT_TEXT
;
1770 unsigned char value
, b
;
1771 char *cp
, *ep
, buffer
[BUFSIZ
];
1772 /* sbeck -- handle suffixes */
1774 CE ce
= &ct
->c_cefile
;
1778 fseek (ce
->ce_fp
, 0L, SEEK_SET
);
1783 if ((ce
->ce_fp
= fopen (ce
->ce_file
, "r")) == NULL
) {
1784 content_error (ce
->ce_file
, ct
, "unable to fopen for reading");
1790 if (*file
== NULL
) {
1793 ce
->ce_file
= add (*file
, NULL
);
1797 /* sbeck@cise.ufl.edu -- handle suffixes */
1799 snprintf (buffer
, sizeof(buffer
), "%s-suffix-%s/%s",
1800 invo_name
, ci
->ci_type
, ci
->ci_subtype
);
1801 cp
= context_find (buffer
);
1802 if (cp
== NULL
|| *cp
== '\0') {
1803 snprintf (buffer
, sizeof(buffer
), "%s-suffix-%s", invo_name
,
1805 cp
= context_find (buffer
);
1807 if (cp
!= NULL
&& *cp
!= '\0') {
1808 if (ce
->ce_unlink
) {
1809 /* Create temporary file with filename extension. */
1810 if ((ce
->ce_file
= m_mktemps(invo_name
, cp
, NULL
, NULL
)) == NULL
) {
1811 adios(NULL
, "unable to create temporary file in %s",
1815 ce
->ce_file
= add (cp
, ce
->ce_file
);
1817 } else if (*file
== NULL
) {
1819 if ((tempfile
= m_mktemp2(NULL
, invo_name
, NULL
, NULL
)) == NULL
) {
1820 adios(NULL
, "unable to create temporary file in %s",
1823 ce
->ce_file
= add (tempfile
, NULL
);
1826 if ((ce
->ce_fp
= fopen (ce
->ce_file
, "w+")) == NULL
) {
1827 content_error (ce
->ce_file
, ct
, "unable to fopen for reading/writing");
1831 if ((len
= ct
->c_end
- ct
->c_begin
) < 0)
1832 adios (NULL
, "internal error(1)");
1835 if ((ct
->c_fp
= fopen (ct
->c_file
, "r")) == NULL
) {
1836 content_error (ct
->c_file
, ct
, "unable to open for reading");
1842 if ((digested
= ct
->c_digested
))
1843 MD5Init (&mdContext
);
1849 lseek (fd
= fileno (ct
->c_fp
), (off_t
) ct
->c_begin
, SEEK_SET
);
1851 switch (cc
= read (fd
, buffer
, sizeof(buffer
) - 1)) {
1853 content_error (ct
->c_file
, ct
, "error reading from");
1857 content_error (NULL
, ct
, "premature eof");
1865 for (ep
= (cp
= buffer
) + cc
; cp
< ep
; cp
++) {
1868 if (isspace ((unsigned char) *cp
))
1870 if (skip
|| (((unsigned char) *cp
) & 0x80)
1871 || (value
= b642nib
[((unsigned char) *cp
) & 0x7f]) > 0x3f) {
1873 fprintf (stderr
, "*cp=0x%x pos=%ld skip=%d\n",
1874 (unsigned char) *cp
,
1875 (long) (lseek (fd
, (off_t
) 0, SEEK_CUR
) - (ep
- cp
)),
1878 content_error (NULL
, ct
,
1879 "invalid BASE64 encoding -- continuing");
1883 bits
|= value
<< bitno
;
1885 if ((bitno
-= 6) < 0) {
1886 b
= (bits
>> 16) & 0xff;
1887 if (!text
|| b
!= '\r')
1888 putc ((char) b
, ce
->ce_fp
);
1890 MD5Update (&mdContext
, &b
, 1);
1892 b
= (bits
>> 8) & 0xff;
1893 if (! text
|| b
!= '\r')
1894 putc ((char) b
, ce
->ce_fp
);
1896 MD5Update (&mdContext
, &b
, 1);
1899 if (! text
|| b
!= '\r')
1900 putc ((char) b
, ce
->ce_fp
);
1902 MD5Update (&mdContext
, &b
, 1);
1906 if (ferror (ce
->ce_fp
)) {
1907 content_error (ce
->ce_file
, ct
,
1908 "error writing to");
1911 bitno
= 18, bits
= 0L, skip
= 0;
1917 goto self_delimiting
;
1926 fprintf (stderr
, "premature ending (bitno %d)\n", bitno
);
1928 content_error (NULL
, ct
, "invalid BASE64 encoding");
1933 fseek (ct
->c_fp
, 0L, SEEK_SET
);
1935 if (fflush (ce
->ce_fp
)) {
1936 content_error (ce
->ce_file
, ct
, "error writing to");
1941 unsigned char digest
[16];
1943 MD5Final (digest
, &mdContext
);
1944 if (memcmp((char *) digest
, (char *) ct
->c_digest
,
1945 sizeof(digest
) / sizeof(digest
[0])))
1946 content_error (NULL
, ct
,
1947 "content integrity suspect (digest mismatch) -- continuing");
1950 fprintf (stderr
, "content integrity confirmed\n");
1953 fseek (ce
->ce_fp
, 0L, SEEK_SET
);
1956 *file
= ce
->ce_file
;
1961 return fileno (ce
->ce_fp
);
1968 free_encoding (ct
, 0);
1977 static char hex2nib
[0x80] = {
1978 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1979 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1980 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1981 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1982 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1983 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1984 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
1985 0x08, 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1986 0x00, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x00,
1987 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1988 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1989 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1990 0x00, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x00,
1991 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1992 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1993 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
2000 return init_encoding (ct
, openQuoted
);
2005 openQuoted (CT ct
, char **file
)
2007 int cc
, digested
, len
, quoted
, own_ct_fp
= 0;
2009 char buffer
[BUFSIZ
];
2011 CE ce
= &ct
->c_cefile
;
2012 /* sbeck -- handle suffixes */
2017 fseek (ce
->ce_fp
, 0L, SEEK_SET
);
2022 if ((ce
->ce_fp
= fopen (ce
->ce_file
, "r")) == NULL
) {
2023 content_error (ce
->ce_file
, ct
, "unable to fopen for reading");
2029 if (*file
== NULL
) {
2032 ce
->ce_file
= add (*file
, NULL
);
2036 /* sbeck@cise.ufl.edu -- handle suffixes */
2038 snprintf (buffer
, sizeof(buffer
), "%s-suffix-%s/%s",
2039 invo_name
, ci
->ci_type
, ci
->ci_subtype
);
2040 cp
= context_find (buffer
);
2041 if (cp
== NULL
|| *cp
== '\0') {
2042 snprintf (buffer
, sizeof(buffer
), "%s-suffix-%s", invo_name
,
2044 cp
= context_find (buffer
);
2046 if (cp
!= NULL
&& *cp
!= '\0') {
2047 if (ce
->ce_unlink
) {
2048 /* Create temporary file with filename extension. */
2049 if ((ce
->ce_file
= m_mktemps(invo_name
, cp
, NULL
, NULL
)) == NULL
) {
2050 adios(NULL
, "unable to create temporary file in %s",
2054 ce
->ce_file
= add (cp
, ce
->ce_file
);
2056 } else if (*file
== NULL
) {
2058 if ((tempfile
= m_mktemp2(NULL
, invo_name
, NULL
, NULL
)) == NULL
) {
2059 adios(NULL
, "unable to create temporary file in %s",
2062 ce
->ce_file
= add (tempfile
, NULL
);
2065 if ((ce
->ce_fp
= fopen (ce
->ce_file
, "w+")) == NULL
) {
2066 content_error (ce
->ce_file
, ct
, "unable to fopen for reading/writing");
2070 if ((len
= ct
->c_end
- ct
->c_begin
) < 0)
2071 adios (NULL
, "internal error(2)");
2074 if ((ct
->c_fp
= fopen (ct
->c_file
, "r")) == NULL
) {
2075 content_error (ct
->c_file
, ct
, "unable to open for reading");
2081 if ((digested
= ct
->c_digested
))
2082 MD5Init (&mdContext
);
2089 fseek (ct
->c_fp
, ct
->c_begin
, SEEK_SET
);
2091 if (fgets (buffer
, sizeof(buffer
) - 1, ct
->c_fp
) == NULL
) {
2092 content_error (NULL
, ct
, "premature eof");
2096 if ((cc
= strlen (buffer
)) > len
)
2100 for (ep
= (cp
= buffer
) + cc
- 1; cp
<= ep
; ep
--)
2101 if (!isspace ((unsigned char) *ep
))
2105 for (; cp
< ep
; cp
++) {
2107 /* in an escape sequence */
2109 /* at byte 1 of an escape sequence */
2110 mask
= hex2nib
[((unsigned char) *cp
) & 0x7f];
2111 /* next is byte 2 */
2114 /* at byte 2 of an escape sequence */
2116 mask
|= hex2nib
[((unsigned char) *cp
) & 0x7f];
2117 putc (mask
, ce
->ce_fp
);
2119 MD5Update (&mdContext
, &mask
, 1);
2120 if (ferror (ce
->ce_fp
)) {
2121 content_error (ce
->ce_file
, ct
, "error writing to");
2124 /* finished escape sequence; next may be literal or a new
2125 * escape sequence */
2128 /* on to next byte */
2132 /* not in an escape sequence */
2134 /* starting an escape sequence, or invalid '='? */
2135 if (cp
+ 1 < ep
&& cp
[1] == '\n') {
2136 /* "=\n" soft line break, eat the \n */
2140 if (cp
+ 1 >= ep
|| cp
+ 2 >= ep
) {
2141 /* We don't have 2 bytes left, so this is an invalid
2142 * escape sequence; just show the raw bytes (below). */
2143 } else if (isxdigit ((unsigned char) cp
[1]) &&
2144 isxdigit ((unsigned char) cp
[2])) {
2145 /* Next 2 bytes are hex digits, making this a valid escape
2146 * sequence; let's decode it (above). */
2150 /* One or both of the next 2 is out of range, making this
2151 * an invalid escape sequence; just show the raw bytes
2156 /* Just show the raw byte. */
2157 putc (*cp
, ce
->ce_fp
);
2160 MD5Update (&mdContext
, (unsigned char *) "\r\n",2);
2162 MD5Update (&mdContext
, (unsigned char *) cp
, 1);
2165 if (ferror (ce
->ce_fp
)) {
2166 content_error (ce
->ce_file
, ct
, "error writing to");
2172 content_error (NULL
, ct
,
2173 "invalid QUOTED-PRINTABLE encoding -- end-of-content while still quoting");
2177 fseek (ct
->c_fp
, 0L, SEEK_SET
);
2179 if (fflush (ce
->ce_fp
)) {
2180 content_error (ce
->ce_file
, ct
, "error writing to");
2185 unsigned char digest
[16];
2187 MD5Final (digest
, &mdContext
);
2188 if (memcmp((char *) digest
, (char *) ct
->c_digest
,
2189 sizeof(digest
) / sizeof(digest
[0])))
2190 content_error (NULL
, ct
,
2191 "content integrity suspect (digest mismatch) -- continuing");
2194 fprintf (stderr
, "content integrity confirmed\n");
2197 fseek (ce
->ce_fp
, 0L, SEEK_SET
);
2200 *file
= ce
->ce_file
;
2205 return fileno (ce
->ce_fp
);
2208 free_encoding (ct
, 0);
2224 if (init_encoding (ct
, open7Bit
) == NOTOK
)
2227 ct
->c_cesizefnx
= NULL
; /* no need to decode for real size */
2233 open7Bit (CT ct
, char **file
)
2235 int cc
, fd
, len
, own_ct_fp
= 0;
2236 char buffer
[BUFSIZ
];
2237 /* sbeck -- handle suffixes */
2240 CE ce
= &ct
->c_cefile
;
2243 fseek (ce
->ce_fp
, 0L, SEEK_SET
);
2248 if ((ce
->ce_fp
= fopen (ce
->ce_file
, "r")) == NULL
) {
2249 content_error (ce
->ce_file
, ct
, "unable to fopen for reading");
2255 if (*file
== NULL
) {
2258 ce
->ce_file
= add (*file
, NULL
);
2262 /* sbeck@cise.ufl.edu -- handle suffixes */
2264 snprintf (buffer
, sizeof(buffer
), "%s-suffix-%s/%s",
2265 invo_name
, ci
->ci_type
, ci
->ci_subtype
);
2266 cp
= context_find (buffer
);
2267 if (cp
== NULL
|| *cp
== '\0') {
2268 snprintf (buffer
, sizeof(buffer
), "%s-suffix-%s", invo_name
,
2270 cp
= context_find (buffer
);
2272 if (cp
!= NULL
&& *cp
!= '\0') {
2273 if (ce
->ce_unlink
) {
2274 /* Create temporary file with filename extension. */
2275 if ((ce
->ce_file
= m_mktemps(invo_name
, cp
, NULL
, NULL
)) == NULL
) {
2276 adios(NULL
, "unable to create temporary file in %s",
2280 ce
->ce_file
= add (cp
, ce
->ce_file
);
2282 } else if (*file
== NULL
) {
2284 if ((tempfile
= m_mktemp2(NULL
, invo_name
, NULL
, NULL
)) == NULL
) {
2285 adios(NULL
, "unable to create temporary file in %s",
2288 ce
->ce_file
= add (tempfile
, NULL
);
2291 if ((ce
->ce_fp
= fopen (ce
->ce_file
, "w+")) == NULL
) {
2292 content_error (ce
->ce_file
, ct
, "unable to fopen for reading/writing");
2296 if (ct
->c_type
== CT_MULTIPART
) {
2297 CI ci
= &ct
->c_ctinfo
;
2301 fprintf (ce
->ce_fp
, "%s: %s/%s", TYPE_FIELD
, ci
->ci_type
, ci
->ci_subtype
);
2302 len
+= strlen (TYPE_FIELD
) + 2 + strlen (ci
->ci_type
)
2303 + 1 + strlen (ci
->ci_subtype
);
2304 buffer
= output_params(len
, ci
->ci_first_pm
, &len
, 0);
2307 fputs (buffer
, ce
->ce_fp
);
2311 if (ci
->ci_comment
) {
2312 if (len
+ 1 + (cc
= 2 + strlen (ci
->ci_comment
)) >= CPERLIN
) {
2313 fputs ("\n\t", ce
->ce_fp
);
2317 putc (' ', ce
->ce_fp
);
2320 fprintf (ce
->ce_fp
, "(%s)", ci
->ci_comment
);
2323 fprintf (ce
->ce_fp
, "\n");
2325 fprintf (ce
->ce_fp
, "%s:%s", ID_FIELD
, ct
->c_id
);
2327 fprintf (ce
->ce_fp
, "%s:%s", DESCR_FIELD
, ct
->c_descr
);
2329 fprintf (ce
->ce_fp
, "%s:%s", DISPO_FIELD
, ct
->c_dispo
);
2330 fprintf (ce
->ce_fp
, "\n");
2333 if ((len
= ct
->c_end
- ct
->c_begin
) < 0)
2334 adios (NULL
, "internal error(3)");
2337 if ((ct
->c_fp
= fopen (ct
->c_file
, "r")) == NULL
) {
2338 content_error (ct
->c_file
, ct
, "unable to open for reading");
2344 lseek (fd
= fileno (ct
->c_fp
), (off_t
) ct
->c_begin
, SEEK_SET
);
2346 switch (cc
= read (fd
, buffer
, sizeof(buffer
) - 1)) {
2348 content_error (ct
->c_file
, ct
, "error reading from");
2352 content_error (NULL
, ct
, "premature eof");
2360 fwrite (buffer
, sizeof(*buffer
), cc
, ce
->ce_fp
);
2361 if (ferror (ce
->ce_fp
)) {
2362 content_error (ce
->ce_file
, ct
, "error writing to");
2367 fseek (ct
->c_fp
, 0L, SEEK_SET
);
2369 if (fflush (ce
->ce_fp
)) {
2370 content_error (ce
->ce_file
, ct
, "error writing to");
2374 fseek (ce
->ce_fp
, 0L, SEEK_SET
);
2377 *file
= ce
->ce_file
;
2382 return fileno (ce
->ce_fp
);
2385 free_encoding (ct
, 0);
2399 openExternal (CT ct
, CT cb
, CE ce
, char **file
, int *fd
)
2401 char cachefile
[BUFSIZ
];
2404 fseek (ce
->ce_fp
, 0L, SEEK_SET
);
2409 if ((ce
->ce_fp
= fopen (ce
->ce_file
, "r")) == NULL
) {
2410 content_error (ce
->ce_file
, ct
, "unable to fopen for reading");
2416 if (find_cache (ct
, rcachesw
, (int *) 0, cb
->c_id
,
2417 cachefile
, sizeof(cachefile
)) != NOTOK
) {
2418 if ((ce
->ce_fp
= fopen (cachefile
, "r"))) {
2419 ce
->ce_file
= getcpy (cachefile
);
2423 admonish (cachefile
, "unable to fopen for reading");
2430 *file
= ce
->ce_file
;
2431 *fd
= fileno (ce
->ce_fp
);
2442 return init_encoding (ct
, openFile
);
2447 openFile (CT ct
, char **file
)
2450 char cachefile
[BUFSIZ
];
2451 struct exbody
*e
= ct
->c_ctexbody
;
2452 CE ce
= &ct
->c_cefile
;
2454 switch (openExternal (e
->eb_parent
, e
->eb_content
, ce
, file
, &fd
)) {
2466 content_error (NULL
, ct
, "missing name parameter");
2470 ce
->ce_file
= getcpy (e
->eb_name
);
2473 if ((ce
->ce_fp
= fopen (ce
->ce_file
, "r")) == NULL
) {
2474 content_error (ce
->ce_file
, ct
, "unable to fopen for reading");
2478 if ((!e
->eb_permission
|| strcasecmp (e
->eb_permission
, "read-write"))
2479 && find_cache (NULL
, wcachesw
, &cachetype
, e
->eb_content
->c_id
,
2480 cachefile
, sizeof(cachefile
)) != NOTOK
) {
2484 mask
= umask (cachetype
? ~m_gmprot () : 0222);
2485 if ((fp
= fopen (cachefile
, "w"))) {
2487 char buffer
[BUFSIZ
];
2488 FILE *gp
= ce
->ce_fp
;
2490 fseek (gp
, 0L, SEEK_SET
);
2492 while ((cc
= fread (buffer
, sizeof(*buffer
), sizeof(buffer
), gp
))
2494 fwrite (buffer
, sizeof(*buffer
), cc
, fp
);
2498 admonish (ce
->ce_file
, "error reading");
2499 (void) m_unlink (cachefile
);
2503 admonish (cachefile
, "error writing");
2504 (void) m_unlink (cachefile
);
2511 fseek (ce
->ce_fp
, 0L, SEEK_SET
);
2512 *file
= ce
->ce_file
;
2513 return fileno (ce
->ce_fp
);
2523 return init_encoding (ct
, openFTP
);
2528 openFTP (CT ct
, char **file
)
2530 int cachetype
, caching
, fd
;
2532 char *bp
, *ftp
, *user
, *pass
;
2533 char buffer
[BUFSIZ
], cachefile
[BUFSIZ
];
2535 CE ce
= &ct
->c_cefile
;
2536 static char *username
= NULL
;
2537 static char *password
= NULL
;
2541 if ((ftp
= context_find (nmhaccessftp
)) && !*ftp
)
2547 switch (openExternal (e
->eb_parent
, e
->eb_content
, ce
, file
, &fd
)) {
2558 if (!e
->eb_name
|| !e
->eb_site
) {
2559 content_error (NULL
, ct
, "missing %s parameter",
2560 e
->eb_name
? "site": "name");
2567 pidcheck (pidwait (xpid
, NOTOK
));
2571 /* Get the buffer ready to go */
2573 buflen
= sizeof(buffer
);
2576 * Construct the query message for user
2578 snprintf (bp
, buflen
, "Retrieve %s", e
->eb_name
);
2584 snprintf (bp
, buflen
, " (content %s)", e
->eb_partno
);
2590 snprintf (bp
, buflen
, "\n using %sFTP from site %s",
2591 e
->eb_flags
? "anonymous " : "", e
->eb_site
);
2596 if (e
->eb_size
> 0) {
2597 snprintf (bp
, buflen
, " (%lu octets)", e
->eb_size
);
2602 snprintf (bp
, buflen
, "? ");
2605 * Now, check the answer
2607 if (!getanswer (buffer
))
2612 snprintf (buffer
, sizeof(buffer
), "%s@%s", getusername (),
2616 ruserpass (e
->eb_site
, &username
, &password
);
2621 ce
->ce_unlink
= (*file
== NULL
);
2623 cachefile
[0] = '\0';
2624 if ((!e
->eb_permission
|| strcasecmp (e
->eb_permission
, "read-write"))
2625 && find_cache (NULL
, wcachesw
, &cachetype
, e
->eb_content
->c_id
,
2626 cachefile
, sizeof(cachefile
)) != NOTOK
) {
2627 if (*file
== NULL
) {
2634 ce
->ce_file
= add (*file
, NULL
);
2636 ce
->ce_file
= add (cachefile
, NULL
);
2639 if ((tempfile
= m_mktemp2(NULL
, invo_name
, NULL
, NULL
)) == NULL
) {
2640 adios(NULL
, "unable to create temporary file in %s",
2643 ce
->ce_file
= add (tempfile
, NULL
);
2646 if ((ce
->ce_fp
= fopen (ce
->ce_file
, "w+")) == NULL
) {
2647 content_error (ce
->ce_file
, ct
, "unable to fopen for reading/writing");
2652 int child_id
, i
, vecp
;
2656 vec
[vecp
++] = r1bindex (ftp
, '/');
2657 vec
[vecp
++] = e
->eb_site
;
2660 vec
[vecp
++] = e
->eb_dir
;
2661 vec
[vecp
++] = e
->eb_name
;
2662 vec
[vecp
++] = ce
->ce_file
,
2663 vec
[vecp
++] = e
->eb_mode
&& !strcasecmp (e
->eb_mode
, "ascii")
2664 ? "ascii" : "binary";
2669 for (i
= 0; (child_id
= fork()) == NOTOK
&& i
< 5; i
++)
2673 adios ("fork", "unable to");
2677 close (fileno (ce
->ce_fp
));
2679 fprintf (stderr
, "unable to exec ");
2685 if (pidXwait (child_id
, NULL
)) {
2686 username
= password
= NULL
;
2696 chmod (cachefile
, cachetype
? m_gmprot () : 0444);
2701 mask
= umask (cachetype
? ~m_gmprot () : 0222);
2702 if ((fp
= fopen (cachefile
, "w"))) {
2704 FILE *gp
= ce
->ce_fp
;
2706 fseek (gp
, 0L, SEEK_SET
);
2708 while ((cc
= fread (buffer
, sizeof(*buffer
), sizeof(buffer
), gp
))
2710 fwrite (buffer
, sizeof(*buffer
), cc
, fp
);
2714 admonish (ce
->ce_file
, "error reading");
2715 (void) m_unlink (cachefile
);
2719 admonish (cachefile
, "error writing");
2720 (void) m_unlink (cachefile
);
2728 fseek (ce
->ce_fp
, 0L, SEEK_SET
);
2729 *file
= ce
->ce_file
;
2730 return fileno (ce
->ce_fp
);
2741 return init_encoding (ct
, openMail
);
2746 openMail (CT ct
, char **file
)
2748 int child_id
, fd
, i
, vecp
;
2750 char *bp
, buffer
[BUFSIZ
], *vec
[7];
2751 struct exbody
*e
= ct
->c_ctexbody
;
2752 CE ce
= &ct
->c_cefile
;
2754 switch (openExternal (e
->eb_parent
, e
->eb_content
, ce
, file
, &fd
)) {
2765 if (!e
->eb_server
) {
2766 content_error (NULL
, ct
, "missing server parameter");
2773 pidcheck (pidwait (xpid
, NOTOK
));
2777 /* Get buffer ready to go */
2779 buflen
= sizeof(buffer
);
2781 /* Now, construct query message */
2782 snprintf (bp
, buflen
, "Retrieve content");
2788 snprintf (bp
, buflen
, " %s", e
->eb_partno
);
2794 snprintf (bp
, buflen
, " by asking %s\n\n%s\n? ",
2796 e
->eb_subject
? e
->eb_subject
: e
->eb_body
);
2798 /* Now, check answer */
2799 if (!getanswer (buffer
))
2803 vec
[vecp
++] = r1bindex (mailproc
, '/');
2804 vec
[vecp
++] = e
->eb_server
;
2805 vec
[vecp
++] = "-subject";
2806 vec
[vecp
++] = e
->eb_subject
? e
->eb_subject
: "mail-server request";
2807 vec
[vecp
++] = "-body";
2808 vec
[vecp
++] = e
->eb_body
;
2811 for (i
= 0; (child_id
= fork()) == NOTOK
&& i
< 5; i
++)
2815 advise ("fork", "unable to");
2819 execvp (mailproc
, vec
);
2820 fprintf (stderr
, "unable to exec ");
2826 if (pidXwait (child_id
, NULL
) == OK
)
2827 advise (NULL
, "request sent");
2831 if (*file
== NULL
) {
2833 if ((tempfile
= m_mktemp2(NULL
, invo_name
, NULL
, NULL
)) == NULL
) {
2834 adios(NULL
, "unable to create temporary file in %s",
2837 ce
->ce_file
= add (tempfile
, NULL
);
2840 ce
->ce_file
= add (*file
, NULL
);
2844 if ((ce
->ce_fp
= fopen (ce
->ce_file
, "w+")) == NULL
) {
2845 content_error (ce
->ce_file
, ct
, "unable to fopen for reading/writing");
2849 /* showproc is for mhshow and mhstore, though mhlist -debug
2850 * prints it, too. */
2852 free (ct
->c_showproc
);
2853 ct
->c_showproc
= add ("true", NULL
);
2855 fseek (ce
->ce_fp
, 0L, SEEK_SET
);
2856 *file
= ce
->ce_file
;
2857 return fileno (ce
->ce_fp
);
2868 return init_encoding (ct
, openURL
);
2873 openURL (CT ct
, char **file
)
2875 struct exbody
*e
= ct
->c_ctexbody
;
2876 CE ce
= &ct
->c_cefile
;
2877 char *urlprog
, *program
;
2878 char buffer
[BUFSIZ
], cachefile
[BUFSIZ
];
2879 int fd
, caching
, cachetype
;
2880 struct msgs_array args
= { 0, 0, NULL
};
2883 if ((urlprog
= context_find(nmhaccessurl
)) && *urlprog
== '\0')
2887 content_error(NULL
, ct
, "No entry for nmh-access-url in profile");
2891 switch (openExternal(e
->eb_parent
, e
->eb_content
, ce
, file
, &fd
)) {
2903 content_error(NULL
, ct
, "missing url parameter");
2910 pidcheck (pidwait (xpid
, NOTOK
));
2914 ce
->ce_unlink
= (*file
== NULL
);
2916 cachefile
[0] = '\0';
2918 if (find_cache(NULL
, wcachesw
, &cachetype
, e
->eb_content
->c_id
,
2919 cachefile
, sizeof(cachefile
)) != NOTOK
) {
2920 if (*file
== NULL
) {
2927 ce
->ce_file
= add(*file
, NULL
);
2929 ce
->ce_file
= add(cachefile
, NULL
);
2932 if ((tempfile
= m_mktemp2(NULL
, invo_name
, NULL
, NULL
)) == NULL
) {
2933 adios(NULL
, "unable to create temporary file in %s",
2936 ce
->ce_file
= add (tempfile
, NULL
);
2939 if ((ce
->ce_fp
= fopen(ce
->ce_file
, "w+")) == NULL
) {
2940 content_error(ce
->ce_file
, ct
, "unable to fopen for read/writing");
2944 switch (child_id
= fork()) {
2946 adios ("fork", "unable to");
2950 argsplit_msgarg(&args
, urlprog
, &program
);
2951 app_msgarg(&args
, e
->eb_url
);
2952 app_msgarg(&args
, NULL
);
2953 dup2(fileno(ce
->ce_fp
), 1);
2954 close(fileno(ce
->ce_fp
));
2955 execvp(program
, args
.msgs
);
2956 fprintf(stderr
, "Unable to exec ");
2962 if (pidXwait(child_id
, NULL
)) {
2970 chmod(cachefile
, cachetype
? m_gmprot() : 0444);
2975 mask
= umask (cachetype
? ~m_gmprot() : 0222);
2976 if ((fp
= fopen(cachefile
, "w"))) {
2978 FILE *gp
= ce
->ce_fp
;
2980 fseeko(gp
, 0, SEEK_SET
);
2982 while ((cc
= fread(buffer
, sizeof(*buffer
),
2983 sizeof(buffer
), gp
)) > 0)
2984 fwrite(buffer
, sizeof(*buffer
), cc
, fp
);
2989 admonish(ce
->ce_file
, "error reading");
2990 (void) m_unlink (cachefile
);
2997 fseeko(ce
->ce_fp
, 0, SEEK_SET
);
2998 *file
= ce
->ce_file
;
3003 readDigest (CT ct
, char *cp
)
3008 unsigned char *dp
, value
, *ep
;
3014 for (ep
= (dp
= ct
->c_digest
)
3015 + sizeof(ct
->c_digest
) / sizeof(ct
->c_digest
[0]); *cp
; cp
++)
3020 || (value
= b642nib
[*cp
& 0x7f]) > 0x3f) {
3022 fprintf (stderr
, "invalid BASE64 encoding\n");
3026 bits
|= value
<< bitno
;
3028 if ((bitno
-= 6) < 0) {
3029 if (dp
+ (3 - skip
) > ep
)
3030 goto invalid_digest
;
3031 *dp
++ = (bits
>> 16) & 0xff;
3033 *dp
++ = (bits
>> 8) & 0xff;
3035 *dp
++ = bits
& 0xff;
3045 goto self_delimiting
;
3050 fprintf (stderr
, "premature ending (bitno %d)\n", bitno
);
3060 fprintf (stderr
, "invalid MD5 digest (got %d octets)\n",
3068 fprintf (stderr
, "MD5 digest=");
3069 for (dp
= ct
->c_digest
; dp
< ep
; dp
++)
3070 fprintf (stderr
, "%02x", *dp
& 0xff);
3071 fprintf (stderr
, "\n");
3078 /* Multipart parts might have content before the first subpart and/or
3079 after the last subpart that hasn't been stored anywhere else, so do
3082 get_leftover_mp_content (CT ct
, int before
/* or after */) {
3083 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
3085 int found_boundary
= 0;
3086 char buffer
[BUFSIZ
];
3089 char *content
= NULL
;
3091 if (! m
) return NOTOK
;
3094 if (! m
->mp_parts
|| ! m
->mp_parts
->mp_part
) return NOTOK
;
3096 /* Isolate the beginning of this part to the beginning of the
3097 first subpart and save any content between them. */
3098 fseeko (ct
->c_fp
, ct
->c_begin
, SEEK_SET
);
3099 max
= m
->mp_parts
->mp_part
->c_begin
- ct
->c_begin
;
3100 boundary
= concat ("--", m
->mp_start
, NULL
);
3102 struct part
*last_subpart
= NULL
;
3103 struct part
*subpart
;
3105 /* Go to the last subpart to get its end position. */
3106 for (subpart
= m
->mp_parts
; subpart
; subpart
= subpart
->mp_next
) {
3107 last_subpart
= subpart
;
3110 if (last_subpart
== NULL
) return NOTOK
;
3112 /* Isolate the end of the last subpart to the end of this part
3113 and save any content between them. */
3114 fseeko (ct
->c_fp
, last_subpart
->mp_part
->c_end
, SEEK_SET
);
3115 max
= ct
->c_end
- last_subpart
->mp_part
->c_end
;
3116 boundary
= concat ("--", m
->mp_stop
, NULL
);
3119 /* Back up by 1 to pick up the newline. */
3120 while (fgets (buffer
, sizeof(buffer
) - 1, ct
->c_fp
)) {
3121 read
+= strlen (buffer
);
3122 /* Don't look beyond beginning of first subpart (before) or
3123 next part (after). */
3124 if (read
> max
) buffer
[read
-max
] = '\0';
3127 if (! strcmp (buffer
, boundary
)) {
3131 if (! found_boundary
&& ! strcmp (buffer
, boundary
)) {
3137 if ((before
&& ! found_boundary
) || (! before
&& found_boundary
)) {
3139 char *old_content
= content
;
3140 content
= concat (content
, buffer
, NULL
);
3144 ? concat ("\n", buffer
, NULL
)
3145 : concat (buffer
, NULL
);
3150 if (found_boundary
|| read
> max
) break;
3152 if (read
> max
) break;
3156 /* Skip the newline if that's all there is. */
3160 /* Remove trailing newline, except at EOF. */
3161 if ((before
|| ! feof (ct
->c_fp
)) &&
3162 (cp
= content
+ strlen (content
)) > content
&&
3167 if (strlen (content
) > 1) {
3169 m
->mp_content_before
= content
;
3171 m
->mp_content_after
= content
;
3185 ct_type_str (int type
) {
3187 case CT_APPLICATION
:
3188 return "application";
3204 return "unknown_type";
3210 ct_subtype_str (int type
, int subtype
) {
3212 case CT_APPLICATION
:
3214 case APPLICATION_OCTETS
:
3216 case APPLICATION_POSTSCRIPT
:
3217 return "postscript";
3219 return "unknown_app_subtype";
3223 case MESSAGE_RFC822
:
3225 case MESSAGE_PARTIAL
:
3227 case MESSAGE_EXTERNAL
:
3230 return "unknown_msg_subtype";
3236 case MULTI_ALTERNATE
:
3237 return "alternative";
3240 case MULTI_PARALLEL
:
3243 return "unknown_multipart_subtype";
3254 return "unknown_text_subtype";
3257 return "unknown_type";
3262 /* Find the content type and InitFunc for the CT. */
3263 const struct str2init
*
3264 get_ct_init (int type
) {
3265 const struct str2init
*sp
;
3267 for (sp
= str2cts
; sp
->si_key
; ++sp
) {
3268 if (type
== sp
->si_val
) {
3277 ce_str (int encoding
) {
3282 return "quoted-printable";
3298 /* Find the content type and InitFunc for the content encoding method. */
3299 const struct str2init
*
3300 get_ce_method (const char *method
) {
3301 struct str2init
*sp
;
3303 for (sp
= str2ces
; sp
->si_key
; ++sp
) {
3304 if (! strcasecmp (method
, sp
->si_key
)) {
3313 parse_header_attrs (const char *filename
, const char *fieldname
,
3314 char **header_attrp
, PM
*param_head
, PM
*param_tail
,
3317 char *cp
= *header_attrp
;
3320 while (*cp
== ';') {
3321 char *dp
, *vp
, *up
, c
;
3324 while (isspace ((unsigned char) *cp
))
3328 get_comment (filename
, fieldname
, &cp
, commentp
) == NOTOK
) {
3334 "extraneous trailing ';' in message %s's %s: "
3336 filename
, fieldname
);
3340 /* down case the attribute name */
3341 for (dp
= cp
; istoken ((unsigned char) *dp
); dp
++)
3342 if (isalpha((unsigned char) *dp
) && isupper ((unsigned char) *dp
))
3343 *dp
= tolower ((unsigned char) *dp
);
3345 for (up
= dp
; isspace ((unsigned char) *dp
);)
3347 if (dp
== cp
|| *dp
!= '=') {
3349 "invalid parameter in message %s's %s: "
3350 "field\n%*sparameter %s (error detected at offset %d)",
3351 filename
, fieldname
, strlen(invo_name
) + 2, "",cp
, dp
- cp
);
3355 pm
= mh_xmalloc(sizeof(*pm
));
3356 memset(pm
, 0, sizeof(*pm
));
3358 /* This is all mega-bozo and needs cleanup */
3359 vp
= (pm
->pm_name
= add (cp
, NULL
)) + (up
- cp
);
3361 for (dp
++; isspace ((unsigned char) *dp
);)
3364 /* Now store the attribute value. */
3366 vp
= pm
->pm_name
+ (dp
- cp
);
3369 for (cp
= ++dp
, dp
= vp
;;) {
3370 switch (c
= *cp
++) {
3374 "invalid quoted-string in message %s's %s: "
3375 "field\n%*s(parameter %s)",
3376 filename
, fieldname
, strlen(invo_name
) + 2, "",
3382 if ((c
= *cp
++) == '\0')
3397 for (cp
= dp
, dp
= vp
; istoken (*cp
); cp
++, dp
++)
3401 pm
->pm_value
= getcpy(vp
);
3404 "invalid parameter in message %s's %s: "
3405 "field\n%*s(parameter %s)",
3406 filename
, fieldname
, strlen(invo_name
) + 2, "",
3411 while (isspace ((unsigned char) *cp
))
3415 get_comment (filename
, fieldname
, &cp
, commentp
) == NOTOK
) {
3419 if (*param_head
== NULL
) {
3423 (*param_tail
)->pm_next
= pm
;
3433 * Create a string based on a list of output parameters. Assume that this
3434 * parameter string will be appended to an existing header, so start out
3435 * with the separator (;). Perform RFC 2231 encoding when necessary.
3439 output_params(size_t initialwidth
, PM params
, int *offsetout
, int external
)
3441 char *paramout
= NULL
;
3442 char line
[CPERLIN
* 2], *q
;
3443 int curlen
, index
, cont
, encode
, i
;
3444 size_t valoff
, numchars
;
3446 while (params
!= NULL
) {
3452 if (external
&& strcasecmp(params
->pm_name
, "body") == 0)
3455 if (strlen(params
->pm_name
) > CPERLIN
) {
3456 advise(NULL
, "Parameter name \"%s\" is too long", params
->pm_name
);
3462 curlen
= param_len(params
, index
, valoff
, &encode
, &cont
, &numchars
);
3465 * Loop until we get a parameter that fits within a line. We
3466 * assume new lines start with a tab, so check our overflow based
3476 * At this point we're definitely continuing the line, so
3477 * be sure to include the parameter name and section index.
3480 q
+= snprintf(q
, sizeof(line
) - (q
- line
), "%s*%d",
3481 params
->pm_name
, index
);
3484 * Both of these functions do a NUL termination
3488 i
= encode_param(params
, q
, sizeof(line
) - (q
- line
),
3489 numchars
, valoff
, index
);
3491 i
= normal_param(params
, q
, sizeof(line
) - (q
- line
),
3502 curlen
= param_len(params
, index
, valoff
, &encode
, &cont
,
3507 * "line" starts with a ;\n\t, so that doesn't count against
3508 * the length. But add 8 since it starts with a tab; that's
3509 * how we end up with 5.
3512 initialwidth
= strlen(line
) + 5;
3515 * At this point the line should be built, so add it to our
3516 * current output buffer.
3519 paramout
= add(line
, paramout
);
3523 * If this won't fit on the line, start a new one. Save room in
3524 * case we need a semicolon on the end
3527 if (initialwidth
+ curlen
> CPERLIN
- 1) {
3539 * At this point, we're either finishing a contined parameter, or
3540 * we're working on a new one.
3544 q
+= snprintf(q
, sizeof(line
) - (q
- line
), "%s*%d",
3545 params
->pm_name
, index
);
3547 strncpy(q
, params
->pm_name
, sizeof(line
) - (q
- line
));
3552 i
= encode_param(params
, q
, sizeof(line
) - (q
- line
),
3553 strlen(params
->pm_value
+ valoff
), valoff
, index
);
3555 i
= normal_param(params
, q
, sizeof(line
) - (q
- line
),
3556 strlen(params
->pm_value
+ valoff
), valoff
);
3564 paramout
= add(line
, paramout
);
3565 initialwidth
+= strlen(line
);
3567 params
= params
->pm_next
;
3571 *offsetout
= initialwidth
;
3577 * Calculate the size of a parameter.
3581 * pm - The parameter being output
3582 * index - If continuing the parameter, the index of the section
3584 * valueoff - The current offset into the parameter value that we're
3585 * working on (previous sections have consumed valueoff bytes).
3586 * encode - Set if we should perform encoding on this parameter section
3587 * (given that we're consuming bytesfit bytes).
3588 * cont - Set if the remaining data in value will not fit on a single
3589 * line and will need to be continued.
3590 * bytesfit - The number of bytes that we can consume from the parameter
3591 * value and still fit on a completely new line. The
3592 * calculation assumes the new line starts with a tab,
3593 * includes the parameter name and any encoding, and fits
3594 * within CPERLIN bytes. Will always be at least 1.
3598 param_len(PM pm
, int index
, size_t valueoff
, int *encode
, int *cont
,
3601 char *start
= pm
->pm_value
+ valueoff
, *p
, indexchar
[32];
3602 size_t len
= 0, fit
= 0;
3603 int fitlimit
= 0, eightbit
, maxfit
;
3608 * Add up the length. First, start with the parameter name.
3611 len
= strlen(pm
->pm_name
);
3614 * Scan the parameter value and see if we need to do encoding for this
3618 eightbit
= contains8bit(start
, NULL
);
3621 * Determine if we need to encode this section. Encoding is necessary if:
3623 * - There are any 8-bit characters at all and we're on the first
3625 * - There are 8-bit characters within N bytes of our section start.
3626 * N is calculated based on the number of bytes it would take to
3627 * reach CPERLIN. Specifically:
3628 * 8 (starting tab) +
3629 * strlen(param name) +
3630 * 4 ('* for section marker, '=', opening/closing '"')
3632 * is the number of bytes used by everything that isn't part of the
3633 * value. So that gets subtracted from CPERLIN.
3636 snprintf(indexchar
, sizeof(indexchar
), "%d", index
);
3637 maxfit
= CPERLIN
- (12 + len
+ strlen(indexchar
));
3638 if ((eightbit
&& index
== 0) || contains8bit(start
, start
+ maxfit
)) {
3642 len
++; /* Add in equal sign */
3646 * We're using maxfit as a marker for how many characters we can
3647 * fit into the line. Bump it by two because we're not using quotes
3654 * If we don't have a charset or language tag in this parameter,
3658 if (! pm
->pm_charset
)
3659 pm
->pm_charset
= getcpy(write_charset_8bit());
3661 pm
->pm_lang
= getcpy(NULL
); /* Default to a blank lang tag */
3663 len
++; /* For the encoding marker */
3666 int enclen
= strlen(pm
->pm_charset
) + strlen(pm
->pm_lang
) + 2;
3671 * We know we definitely need to include an index. maxfit already
3672 * includes the section marker.
3674 len
+= strlen(indexchar
);
3676 for (p
= start
; *p
!= '\0'; p
++) {
3677 if (isparamencode(*p
)) {
3685 * Just so there's no confusion: maxfit is counting OUTPUT
3686 * characters (post-encoding). fit is counting INPUT characters.
3688 if (! fitlimit
&& maxfit
>= 0)
3690 else if (! fitlimit
)
3695 * Calculate the string length, but add room for quoting \
3696 * and " if necessary. Also account for quotes at beginning
3699 for (p
= start
; *p
!= '\0'; p
++) {
3710 if (! fitlimit
&& maxfit
>= 0)
3712 else if (! fitlimit
)
3729 * Output an encoded parameter string.
3733 encode_param(PM pm
, char *output
, size_t len
, size_t valuelen
,
3734 size_t valueoff
, int index
)
3736 size_t outlen
= 0, n
;
3737 char *endptr
= output
+ len
, *p
;
3740 * First, output the marker for an encoded string.
3748 * If the index is 0, output the character set and language tag.
3749 * If theses were NULL, they should have already been filled in
3754 n
= snprintf(output
, len
- outlen
, "%s'%s'", pm
->pm_charset
,
3758 if (output
> endptr
) {
3759 advise(NULL
, "Internal error: parameter buffer overflow");
3765 * Copy over the value, encoding if necessary
3768 p
= pm
->pm_value
+ valueoff
;
3769 while (valuelen
-- > 0) {
3770 if (isparamencode(*p
)) {
3771 n
= snprintf(output
, len
- outlen
, "%%%02X", (unsigned char) *p
++);
3778 if (output
> endptr
) {
3779 advise(NULL
, "Internal error: parameter buffer overflow");
3790 * Output a "normal" parameter, without encoding. Be sure to escape
3791 * quotes and backslashes if necessary.
3795 normal_param(PM pm
, char *output
, size_t len
, size_t valuelen
,
3799 char *endptr
= output
+ len
, *p
;
3805 p
= pm
->pm_value
+ valueoff
;
3807 while (valuelen
-- > 0) {
3817 if (output
> endptr
) {
3818 advise(NULL
, "Internal error: parameter buffer overflow");
3823 if (output
- 2 > endptr
) {
3824 advise(NULL
, "Internal error: parameter buffer overflow");
3835 * Add a parameter to the parameter linked list
3839 add_param(PM
*first
, PM
*last
, const char *name
, const char *value
)
3841 PM pm
= mh_xmalloc(sizeof(*pm
));
3843 memset(pm
, 0, sizeof(*pm
));
3845 pm
->pm_name
= getcpy(name
);
3846 pm
->pm_value
= getcpy(value
);
3849 (*last
)->pm_next
= pm
;
3860 * Retrieve a parameter value from a parameter linked list. If the parameter
3861 * value needs converted to the local character set, do that now.
3865 get_param(PM first
, const char *name
, char replace
, int fetchonly
)
3867 while (first
!= NULL
) {
3868 if (strcasecmp(name
, first
->pm_name
) == 0) {
3870 return first
->pm_value
;
3872 char convbuf
[BUFSIZ
];
3873 size_t inbytes
, outbytes
= sizeof(convbuf
);
3877 ICONV_CONST
char *p
;
3879 #endif /* HAVE_ICONV */
3880 if (!first
->pm_charset
||
3881 check_charset(first
->pm_charset
,
3882 strlen(first
->pm_charset
))) {
3884 * No conversion necessary
3886 return getcpy(first
->pm_value
);
3889 utf8
= strcasecmp(first
->pm_charset
, "UTF-8") == 0;
3891 cd
= iconv_open(get_charset(), first
->pm_charset
);
3892 if (cd
== (iconv_t
) -1) {
3896 inbytes
= strlen(first
->pm_value
);
3897 outbytes
= sizeof(convbuf
);
3898 p
= first
->pm_value
;
3902 if (iconv(cd
, &p
, &inbytes
, &q
, &outbytes
) == -1) {
3903 if (errno
!= EILSEQ
) {
3908 * Reset shift state, substitute our character,
3909 * try to restart conversion.
3911 iconv(cd
, NULL
, NULL
, &q
, &outbytes
);
3912 if (outbytes
== 0) {
3918 if (outbytes
== 0) {
3923 for (++p
, --inbytes
; inbytes
> 0 &&
3924 (((unsigned char) *q
) & 0xc0) == 0x80;
3940 return getcpy(convbuf
);
3941 #endif /* HAVE_ICONV */
3943 for (p
= first
->pm_value
, q
= convbuf
; *p
&& outbytes
> 1;
3944 p
++, q
++, outbytes
--) {
3945 if (isascii((unsigned char) p
) &&
3946 isprint((unsigned char) p
))
3954 return getcpy(convbuf
);
3958 first
= first
->pm_next
;