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>
23 extern pid_t xpid
; /* mhshowsbr.c */
26 extern int rcachesw
; /* mhcachesbr.c */
27 extern int wcachesw
; /* mhcachesbr.c */
29 int checksw
= 0; /* check Content-MD5 field */
32 * These are for mhfixmsg to:
33 * 1) Instruct parser not to detect invalid Content-Transfer-Encoding
35 * 2) Suppress the warning about bogus multipart content, and report it.
37 int skip_mp_cte_check
;
38 int suppress_bogus_mp_content_warning
;
42 * Structures for TEXT messages
44 struct k2v SubText
[] = {
45 { "plain", TEXT_PLAIN
},
46 { "richtext", TEXT_RICHTEXT
}, /* defined in RFC-1341 */
47 { "enriched", TEXT_ENRICHED
}, /* defined in RFC-1896 */
48 { NULL
, TEXT_UNKNOWN
} /* this one must be last! */
51 /* Charset[] removed -- yozo. Mon Oct 8 01:03:41 JST 2012 */
54 * Structures for MULTIPART messages
56 struct k2v SubMultiPart
[] = {
57 { "mixed", MULTI_MIXED
},
58 { "alternative", MULTI_ALTERNATE
},
59 { "digest", MULTI_DIGEST
},
60 { "parallel", MULTI_PARALLEL
},
61 { NULL
, MULTI_UNKNOWN
} /* this one must be last! */
65 * Structures for MESSAGE messages
67 struct k2v SubMessage
[] = {
68 { "rfc822", MESSAGE_RFC822
},
69 { "partial", MESSAGE_PARTIAL
},
70 { "external-body", MESSAGE_EXTERNAL
},
71 { NULL
, MESSAGE_UNKNOWN
} /* this one must be last! */
75 * Structure for APPLICATION messages
77 struct k2v SubApplication
[] = {
78 { "octet-stream", APPLICATION_OCTETS
},
79 { "postscript", APPLICATION_POSTSCRIPT
},
80 { NULL
, APPLICATION_UNKNOWN
} /* this one must be last! */
84 * Mapping of names of CTE types in mhbuild directives
86 static struct k2v EncodingType
[] = {
90 { "quoted-printable", CE_QUOTED
},
92 { "base64", CE_BASE64
},
98 int find_cache (CT
, int, int *, char *, char *, int);
101 int part_ok (CT
, int);
102 int type_ok (CT
, int);
103 void content_error (char *, CT
, char *, ...);
106 void free_encoding (CT
, int);
111 static CT
get_content (FILE *, char *, int);
112 static int get_comment (const char *, const char *, char **, char **);
114 static int InitGeneric (CT
);
115 static int InitText (CT
);
116 static int InitMultiPart (CT
);
117 void reverse_parts (CT
);
118 static int InitMessage (CT
);
119 static int InitApplication (CT
);
120 static int init_encoding (CT
, OpenCEFunc
);
121 static unsigned long size_encoding (CT
);
122 static int InitBase64 (CT
);
123 static int openBase64 (CT
, char **);
124 static int InitQuoted (CT
);
125 static int openQuoted (CT
, char **);
126 static int Init7Bit (CT
);
127 static int openExternal (CT
, CT
, CE
, char **, int *);
128 static int InitFile (CT
);
129 static int openFile (CT
, char **);
130 static int InitFTP (CT
);
131 static int openFTP (CT
, char **);
132 static int InitMail (CT
);
133 static int openMail (CT
, char **);
134 static int readDigest (CT
, char *);
135 static int get_leftover_mp_content (CT
, int);
136 static int InitURL (CT
);
137 static int openURL (CT
, char **);
138 static size_t param_len(PM
, int, size_t, int *);
139 static size_t encode_param(PM
, char *, size_t, size_t, size_t, int);
140 static size_t normal_param(PM
, char *, size_t, size_t, size_t);
141 static int get_dispo (char *, CT
);
143 struct str2init str2cts
[] = {
144 { "application", CT_APPLICATION
, InitApplication
},
145 { "audio", CT_AUDIO
, InitGeneric
},
146 { "image", CT_IMAGE
, InitGeneric
},
147 { "message", CT_MESSAGE
, InitMessage
},
148 { "multipart", CT_MULTIPART
, InitMultiPart
},
149 { "text", CT_TEXT
, InitText
},
150 { "video", CT_VIDEO
, InitGeneric
},
151 { NULL
, CT_EXTENSION
, NULL
}, /* these two must be last! */
152 { NULL
, CT_UNKNOWN
, NULL
},
155 struct str2init str2ces
[] = {
156 { "base64", CE_BASE64
, InitBase64
},
157 { "quoted-printable", CE_QUOTED
, InitQuoted
},
158 { "8bit", CE_8BIT
, Init7Bit
},
159 { "7bit", CE_7BIT
, Init7Bit
},
160 { "binary", CE_BINARY
, Init7Bit
},
161 { NULL
, CE_EXTENSION
, NULL
}, /* these two must be last! */
162 { NULL
, CE_UNKNOWN
, NULL
},
166 * NOTE WELL: si_key MUST NOT have value of NOTOK
168 * si_key is 1 if access method is anonymous.
170 struct str2init str2methods
[] = {
171 { "afs", 1, InitFile
},
172 { "anon-ftp", 1, InitFTP
},
173 { "ftp", 0, InitFTP
},
174 { "local-file", 0, InitFile
},
175 { "mail-server", 0, InitMail
},
176 { "url", 0, InitURL
},
182 pidcheck (int status
)
184 if ((status
& 0xff00) == 0xff00 || (status
& 0x007f) != SIGQUIT
)
195 * Main entry point for parsing a MIME message or file.
196 * It returns the Content structure for the top level
197 * entity in the file.
201 parse_mime (char *file
)
209 * Check if file is actually standard input
211 if ((is_stdin
= !(strcmp (file
, "-")))) {
212 char *tfile
= m_mktemp2(NULL
, invo_name
, NULL
, &fp
);
214 advise("mhparse", "unable to create temporary file in %s",
218 file
= add (tfile
, NULL
);
220 while (fgets (buffer
, sizeof(buffer
), stdin
))
224 if (ferror (stdin
)) {
225 (void) m_unlink (file
);
226 advise ("stdin", "error reading");
230 (void) m_unlink (file
);
231 advise (file
, "error writing");
234 fseek (fp
, 0L, SEEK_SET
);
235 } else if ((fp
= fopen (file
, "r")) == NULL
) {
236 advise (file
, "unable to read");
240 if (!(ct
= get_content (fp
, file
, 1))) {
242 (void) m_unlink (file
);
243 advise (NULL
, "unable to decode %s", file
);
248 ct
->c_unlink
= 1; /* temp file to remove */
252 if (ct
->c_end
== 0L) {
253 fseek (fp
, 0L, SEEK_END
);
254 ct
->c_end
= ftell (fp
);
257 if (ct
->c_ctinitfnx
&& (*ct
->c_ctinitfnx
) (ct
) == NOTOK
) {
269 * Main routine for reading/parsing the headers
270 * of a message content.
272 * toplevel = 1 # we are at the top level of the message
273 * toplevel = 0 # we are inside message type or multipart type
274 * # other than multipart/digest
275 * toplevel = -1 # we are inside multipart/digest
276 * NB: on failure we will fclose(in)!
280 get_content (FILE *in
, char *file
, int toplevel
)
283 char buf
[BUFSIZ
], name
[NAMESZ
];
287 m_getfld_state_t gstate
= 0;
289 /* allocate the content structure */
290 if (!(ct
= (CT
) calloc (1, sizeof(*ct
))))
291 adios (NULL
, "out of memory");
294 ct
->c_file
= add (file
, NULL
);
295 ct
->c_begin
= ftell (ct
->c_fp
) + 1;
298 * Parse the header fields for this
299 * content into a linked list.
301 m_getfld_track_filepos (&gstate
, in
);
302 for (compnum
= 1;;) {
303 int bufsz
= sizeof buf
;
304 switch (state
= m_getfld (&gstate
, name
, buf
, &bufsz
, in
)) {
309 /* get copies of the buffers */
310 np
= add (name
, NULL
);
311 vp
= add (buf
, NULL
);
313 /* if necessary, get rest of field */
314 while (state
== FLDPLUS
) {
316 state
= m_getfld (&gstate
, name
, buf
, &bufsz
, in
);
317 vp
= add (buf
, vp
); /* add to previous value */
320 /* Now add the header data to the list */
321 add_header (ct
, np
, vp
);
323 /* continue, to see if this isn't the last header field */
324 ct
->c_begin
= ftell (in
) + 1;
328 ct
->c_begin
= ftell (in
) - strlen (buf
);
332 ct
->c_begin
= ftell (in
);
337 adios (NULL
, "message format error in component #%d", compnum
);
340 adios (NULL
, "getfld() returned %d", state
);
343 /* break out of the loop */
346 m_getfld_state_destroy (&gstate
);
349 * Read the content headers. We will parse the
350 * MIME related header fields into their various
351 * structures and set internal flags related to
352 * content type/subtype, etc.
355 hp
= ct
->c_first_hf
; /* start at first header field */
357 /* Get MIME-Version field */
358 if (!strcasecmp (hp
->name
, VRSN_FIELD
)) {
363 advise (NULL
, "message %s has multiple %s: fields",
364 ct
->c_file
, VRSN_FIELD
);
367 ct
->c_vrsn
= add (hp
->value
, NULL
);
369 /* Now, cleanup this field */
372 while (isspace ((unsigned char) *cp
))
374 for (dp
= strchr(cp
, '\n'); dp
; dp
= strchr(dp
, '\n'))
376 for (dp
= cp
+ strlen (cp
) - 1; dp
>= cp
; dp
--)
377 if (!isspace ((unsigned char) *dp
))
381 fprintf (stderr
, "%s: %s\n", VRSN_FIELD
, cp
);
384 get_comment (ct
->c_file
, VRSN_FIELD
, &cp
, NULL
) == NOTOK
)
387 for (dp
= cp
; istoken (*dp
); dp
++)
391 ucmp
= !strcasecmp (cp
, VRSN_VALUE
);
394 admonish (NULL
, "message %s has unknown value for %s: field (%s)",
395 ct
->c_file
, VRSN_FIELD
, cp
);
398 else if (!strcasecmp (hp
->name
, TYPE_FIELD
)) {
399 /* Get Content-Type field */
400 struct str2init
*s2i
;
401 CI ci
= &ct
->c_ctinfo
;
403 /* Check if we've already seen a Content-Type header */
405 advise (NULL
, "message %s has multiple %s: fields",
406 ct
->c_file
, TYPE_FIELD
);
410 /* Parse the Content-Type field */
411 if (get_ctinfo (hp
->value
, ct
, 0) == NOTOK
)
415 * Set the Init function and the internal
416 * flag for this content type.
418 for (s2i
= str2cts
; s2i
->si_key
; s2i
++)
419 if (!strcasecmp (ci
->ci_type
, s2i
->si_key
))
421 if (!s2i
->si_key
&& !uprf (ci
->ci_type
, "X-"))
423 ct
->c_type
= s2i
->si_val
;
424 ct
->c_ctinitfnx
= s2i
->si_init
;
426 else if (!strcasecmp (hp
->name
, ENCODING_FIELD
)) {
427 /* Get Content-Transfer-Encoding field */
429 struct str2init
*s2i
;
432 * Check if we've already seen the
433 * Content-Transfer-Encoding field
436 advise (NULL
, "message %s has multiple %s: fields",
437 ct
->c_file
, ENCODING_FIELD
);
441 /* get copy of this field */
442 ct
->c_celine
= cp
= add (hp
->value
, NULL
);
444 while (isspace ((unsigned char) *cp
))
446 for (dp
= cp
; istoken (*dp
); dp
++)
452 * Find the internal flag and Init function
453 * for this transfer encoding.
455 for (s2i
= str2ces
; s2i
->si_key
; s2i
++)
456 if (!strcasecmp (cp
, s2i
->si_key
))
458 if (!s2i
->si_key
&& !uprf (cp
, "X-"))
461 ct
->c_encoding
= s2i
->si_val
;
463 /* Call the Init function for this encoding */
464 if (s2i
->si_init
&& (*s2i
->si_init
) (ct
) == NOTOK
)
467 else if (!strcasecmp (hp
->name
, MD5_FIELD
)) {
468 /* Get Content-MD5 field */
474 if (ct
->c_digested
) {
475 advise (NULL
, "message %s has multiple %s: fields",
476 ct
->c_file
, MD5_FIELD
);
480 ep
= cp
= add (hp
->value
, NULL
); /* get a copy */
482 while (isspace ((unsigned char) *cp
))
484 for (dp
= strchr(cp
, '\n'); dp
; dp
= strchr(dp
, '\n'))
486 for (dp
= cp
+ strlen (cp
) - 1; dp
>= cp
; dp
--)
487 if (!isspace ((unsigned char) *dp
))
491 fprintf (stderr
, "%s: %s\n", MD5_FIELD
, cp
);
494 get_comment (ct
->c_file
, MD5_FIELD
, &cp
, NULL
) == NOTOK
) {
499 for (dp
= cp
; *dp
&& !isspace ((unsigned char) *dp
); dp
++)
507 else if (!strcasecmp (hp
->name
, ID_FIELD
)) {
508 /* Get Content-ID field */
509 ct
->c_id
= add (hp
->value
, ct
->c_id
);
511 else if (!strcasecmp (hp
->name
, DESCR_FIELD
)) {
512 /* Get Content-Description field */
513 ct
->c_descr
= add (hp
->value
, ct
->c_descr
);
515 else if (!strcasecmp (hp
->name
, DISPO_FIELD
)) {
516 /* Get Content-Disposition field */
517 if (get_dispo(hp
->value
, ct
) == NOTOK
)
522 hp
= hp
->next
; /* next header field */
526 * Check if we saw a Content-Type field.
527 * If not, then assign a default value for
528 * it, and the Init function.
532 * If we are inside a multipart/digest message,
533 * so default type is message/rfc822
536 if (get_ctinfo ("message/rfc822", ct
, 0) == NOTOK
)
538 ct
->c_type
= CT_MESSAGE
;
539 ct
->c_ctinitfnx
= InitMessage
;
542 * Else default type is text/plain
544 if (get_ctinfo ("text/plain", ct
, 0) == NOTOK
)
546 ct
->c_type
= CT_TEXT
;
547 ct
->c_ctinitfnx
= InitText
;
551 /* Use default Transfer-Encoding, if necessary */
553 ct
->c_encoding
= CE_7BIT
;
566 * small routine to add header field to list
570 add_header (CT ct
, char *name
, char *value
)
574 /* allocate header field structure */
575 hp
= mh_xmalloc (sizeof(*hp
));
577 /* link data into header structure */
582 /* link header structure into the list */
583 if (ct
->c_first_hf
== NULL
) {
584 ct
->c_first_hf
= hp
; /* this is the first */
587 ct
->c_last_hf
->next
= hp
; /* add it to the end */
595 /* Make sure that buf contains at least one appearance of name,
596 followed by =. If not, insert both name and value, just after
597 first semicolon, if any. Note that name should not contain a
598 trailing =. And quotes will be added around the value. Typical
599 usage: make sure that a Content-Disposition header contains
600 filename="foo". If it doesn't and value does, use value from
603 incl_name_value (char *buf
, char *name
, char *value
) {
606 /* Assume that name is non-null. */
608 char *name_plus_equal
= concat (name
, "=", NULL
);
610 if (! strstr (buf
, name_plus_equal
)) {
612 char *cp
, *prefix
, *suffix
;
614 /* Trim trailing space, esp. newline. */
615 for (cp
= &buf
[strlen (buf
) - 1];
616 cp
>= buf
&& isspace ((unsigned char) *cp
);
621 insertion
= concat ("; ", name
, "=", "\"", value
, "\"", NULL
);
623 /* Insert at first semicolon, if any. If none, append to
625 prefix
= add (buf
, NULL
);
626 if ((cp
= strchr (prefix
, ';'))) {
627 suffix
= concat (cp
, NULL
);
629 newbuf
= concat (prefix
, insertion
, suffix
, "\n", NULL
);
633 newbuf
= concat (buf
, insertion
, "\n", NULL
);
641 free (name_plus_equal
);
647 /* Extract just name_suffix="foo", if any, from value. If there isn't
648 one, return the entire value. Note that, for example, a name_suffix
649 of name will match filename="foo", and return foo. */
651 extract_name_value (char *name_suffix
, char *value
) {
652 char *extracted_name_value
= value
;
653 char *name_suffix_plus_quote
= concat (name_suffix
, "=\"", NULL
);
654 char *name_suffix_equals
= strstr (value
, name_suffix_plus_quote
);
657 free (name_suffix_plus_quote
);
658 if (name_suffix_equals
) {
659 char *name_suffix_begin
;
662 for (cp
= name_suffix_equals
; *cp
!= '"'; ++cp
) /* empty */;
663 name_suffix_begin
= ++cp
;
664 /* Find second \". */
665 for (; *cp
!= '"'; ++cp
) /* empty */;
667 extracted_name_value
= mh_xmalloc (cp
- name_suffix_begin
+ 1);
668 memcpy (extracted_name_value
,
670 cp
- name_suffix_begin
);
671 extracted_name_value
[cp
- name_suffix_begin
] = '\0';
674 return extracted_name_value
;
678 * Parse Content-Type line and (if `magic' is non-zero) mhbuild composition
679 * directives. Fills in the information of the CTinfo structure.
682 get_ctinfo (char *cp
, CT ct
, int magic
)
691 /* store copy of Content-Type line */
692 cp
= ct
->c_ctline
= add (cp
, NULL
);
694 while (isspace ((unsigned char) *cp
)) /* trim leading spaces */
697 /* change newlines to spaces */
698 for (dp
= strchr(cp
, '\n'); dp
; dp
= strchr(dp
, '\n'))
701 /* trim trailing spaces */
702 for (dp
= cp
+ strlen (cp
) - 1; dp
>= cp
; dp
--)
703 if (!isspace ((unsigned char) *dp
))
708 fprintf (stderr
, "%s: %s\n", TYPE_FIELD
, cp
);
710 if (*cp
== '(' && get_comment (ct
->c_file
, TYPE_FIELD
, &cp
,
711 &ci
->ci_comment
) == NOTOK
)
714 for (dp
= cp
; istoken (*dp
); dp
++)
717 ci
->ci_type
= add (cp
, NULL
); /* store content type */
721 advise (NULL
, "invalid %s: field in message %s (empty type)",
722 TYPE_FIELD
, ct
->c_file
);
726 /* down case the content type string */
727 for (dp
= ci
->ci_type
; *dp
; dp
++)
728 if (isalpha((unsigned char) *dp
) && isupper ((unsigned char) *dp
))
729 *dp
= tolower ((unsigned char) *dp
);
731 while (isspace ((unsigned char) *cp
))
734 if (*cp
== '(' && get_comment (ct
->c_file
, TYPE_FIELD
, &cp
,
735 &ci
->ci_comment
) == NOTOK
)
740 ci
->ci_subtype
= add ("", NULL
);
745 while (isspace ((unsigned char) *cp
))
748 if (*cp
== '(' && get_comment (ct
->c_file
, TYPE_FIELD
, &cp
,
749 &ci
->ci_comment
) == NOTOK
)
752 for (dp
= cp
; istoken (*dp
); dp
++)
755 ci
->ci_subtype
= add (cp
, NULL
); /* store the content subtype */
758 if (!*ci
->ci_subtype
) {
760 "invalid %s: field in message %s (empty subtype for \"%s\")",
761 TYPE_FIELD
, ct
->c_file
, ci
->ci_type
);
765 /* down case the content subtype string */
766 for (dp
= ci
->ci_subtype
; *dp
; dp
++)
767 if (isalpha((unsigned char) *dp
) && isupper ((unsigned char) *dp
))
768 *dp
= tolower ((unsigned char) *dp
);
771 while (isspace ((unsigned char) *cp
))
774 if (*cp
== '(' && get_comment (ct
->c_file
, TYPE_FIELD
, &cp
,
775 &ci
->ci_comment
) == NOTOK
)
778 if ((status
= parse_header_attrs (ct
->c_file
, TYPE_FIELD
, &cp
,
779 &ci
->ci_first_pm
, &ci
->ci_last_pm
,
780 &ci
->ci_comment
)) != OK
) {
781 return status
== NOTOK
? NOTOK
: OK
;
785 * Get any <Content-Id> given in buffer
787 if (magic
&& *cp
== '<') {
792 if (!(dp
= strchr(ct
->c_id
= ++cp
, '>'))) {
793 advise (NULL
, "invalid ID in message %s", ct
->c_file
);
799 ct
->c_id
= concat ("<", ct
->c_id
, ">\n", NULL
);
805 while (isspace ((unsigned char) *cp
))
810 * Get any [Content-Description] given in buffer.
812 if (magic
&& *cp
== '[') {
814 for (dp
= cp
+ strlen (cp
) - 1; dp
>= cp
; dp
--)
818 advise (NULL
, "invalid description in message %s", ct
->c_file
);
826 ct
->c_descr
= concat (ct
->c_descr
, "\n", NULL
);
832 while (isspace ((unsigned char) *cp
))
837 * Get any {Content-Disposition} given in buffer.
839 if (magic
&& *cp
== '{') {
841 for (dp
= cp
+ strlen (cp
) - 1; dp
>= cp
; dp
--)
845 advise (NULL
, "invalid disposition in message %s", ct
->c_file
);
853 ct
->c_dispo
= concat (ct
->c_dispo
, "\n", NULL
);
859 while (isspace ((unsigned char) *cp
))
864 * Get any extension directives (right now just the content transfer
865 * encoding, but maybe others) that we care about.
868 if (magic
&& *cp
== '*') {
870 * See if it's a CTE we match on
875 while (*cp
!= '\0' && ! isspace((unsigned char) *cp
))
879 advise (NULL
, "invalid null transfer encoding specification");
886 ct
->c_reqencoding
= CE_UNKNOWN
;
888 for (kv
= EncodingType
; kv
->kv_key
; kv
++) {
889 if (strcasecmp(kv
->kv_key
, dp
) == 0) {
890 ct
->c_reqencoding
= kv
->kv_value
;
895 if (ct
->c_reqencoding
== CE_UNKNOWN
) {
896 advise (NULL
, "invalid CTE specification: \"%s\"", dp
);
900 while (isspace ((unsigned char) *cp
))
905 * Check if anything is left over
909 ci
->ci_magic
= add (cp
, NULL
);
911 /* If there is a Content-Disposition header and it doesn't
912 have a *filename=, extract it from the magic contents.
913 The r1bindex call skips any leading directory
917 incl_name_value (ct
->c_dispo
,
919 r1bindex (extract_name_value ("name",
926 "extraneous information in message %s's %s: field\n%*s(%s)",
927 ct
->c_file
, TYPE_FIELD
, strlen(invo_name
) + 2, "", cp
);
935 * Parse out a Content-Disposition header. A lot of this is cribbed from
939 get_dispo (char *cp
, CT ct
)
945 /* Save the whole copy of the Content-Disposition header */
947 cp
= ct
->c_dispo
= add(cp
, NULL
);
949 while (isspace ((unsigned char) *cp
)) /* trim leading spaces */
952 /* change newlines to spaces */
953 for (dp
= strchr(cp
, '\n'); dp
; dp
= strchr(dp
, '\n'))
956 /* trim trailing spaces */
957 for (dp
= cp
+ strlen (cp
) - 1; dp
>= cp
; dp
--)
958 if (!isspace ((unsigned char) *dp
))
963 fprintf (stderr
, "%s: %s\n", DISPO_FIELD
, cp
);
965 if (*cp
== '(' && get_comment (ct
->c_file
, DISPO_FIELD
, &cp
, NULL
) == NOTOK
)
968 for (dp
= cp
; istoken (*dp
); dp
++)
971 ct
->c_dispo_type
= add (cp
, NULL
); /* store disposition type */
974 if (*cp
== '(' && get_comment (ct
->c_file
, DISPO_FIELD
, &cp
, NULL
) == NOTOK
)
977 if ((status
= parse_header_attrs (ct
->c_file
, DISPO_FIELD
, &cp
,
978 &ct
->c_dispo_first
, &ct
->c_dispo_last
,
980 return status
== NOTOK
? NOTOK
: OK
;
985 "extraneous information in message %s's %s: field\n%*s(%s)",
986 ct
->c_file
, DISPO_FIELD
, strlen(invo_name
) + 2, "", cp
);
994 get_comment (const char *filename
, const char *fieldname
, char **ap
,
999 char c
, buffer
[BUFSIZ
], *dp
;
1006 switch (c
= *cp
++) {
1009 advise (NULL
, "invalid comment in message %s's %s: field",
1010 filename
, fieldname
);
1015 if ((c
= *cp
++) == '\0')
1038 if ((dp
= *commentp
)) {
1039 *commentp
= concat (dp
, " ", buffer
, NULL
);
1042 *commentp
= add (buffer
, NULL
);
1046 while (isspace ((unsigned char) *cp
))
1057 * Handles content types audio, image, and video.
1058 * There's not much to do right here.
1066 return OK
; /* not much to do here */
1077 char buffer
[BUFSIZ
];
1083 CI ci
= &ct
->c_ctinfo
;
1085 /* check for missing subtype */
1086 if (!*ci
->ci_subtype
)
1087 ci
->ci_subtype
= add ("plain", ci
->ci_subtype
);
1090 for (kv
= SubText
; kv
->kv_key
; kv
++)
1091 if (!strcasecmp (ci
->ci_subtype
, kv
->kv_key
))
1093 ct
->c_subtype
= kv
->kv_value
;
1095 /* allocate text character set structure */
1096 if ((t
= (struct text
*) calloc (1, sizeof(*t
))) == NULL
)
1097 adios (NULL
, "out of memory");
1098 ct
->c_ctparams
= (void *) t
;
1100 /* scan for charset parameter */
1101 for (pm
= ci
->ci_first_pm
; pm
; pm
= pm
->pm_next
)
1102 if (!strcasecmp (pm
->pm_name
, "charset"))
1105 /* check if content specified a character set */
1107 chset
= pm
->pm_value
;
1108 t
->tx_charset
= CHARSET_SPECIFIED
;
1110 t
->tx_charset
= CHARSET_UNSPECIFIED
;
1114 * If we can not handle character set natively,
1115 * then check profile for string to modify the
1116 * terminal or display method.
1118 * termproc is for mhshow, though mhlist -debug prints it, too.
1120 if (chset
!= NULL
&& !check_charset (chset
, strlen (chset
))) {
1121 snprintf (buffer
, sizeof(buffer
), "%s-charset-%s", invo_name
, chset
);
1122 if ((cp
= context_find (buffer
)))
1123 ct
->c_termproc
= getcpy (cp
);
1135 InitMultiPart (CT ct
)
1141 char *bp
, buffer
[BUFSIZ
];
1142 struct multipart
*m
;
1144 struct part
*part
, **next
;
1145 CI ci
= &ct
->c_ctinfo
;
1150 * The encoding for multipart messages must be either
1151 * 7bit, 8bit, or binary (per RFC2045).
1153 if (! skip_mp_cte_check
&& ct
->c_encoding
!= CE_7BIT
&&
1154 ct
->c_encoding
!= CE_8BIT
&& ct
->c_encoding
!= CE_BINARY
) {
1155 /* Copy the Content-Transfer-Encoding header field body so we can
1156 remove any trailing whitespace and leading blanks from it. */
1157 char *cte
= add (ct
->c_celine
? ct
->c_celine
: "(null)", NULL
);
1159 bp
= cte
+ strlen (cte
) - 1;
1160 while (bp
>= cte
&& isspace ((unsigned char) *bp
)) *bp
-- = '\0';
1161 for (bp
= cte
; *bp
&& isblank ((unsigned char) *bp
); ++bp
) continue;
1164 "\"%s/%s\" type in message %s must be encoded in\n"
1165 "7bit, 8bit, or binary, per RFC 2045 (6.4). One workaround "
1166 "is to\nmanually edit the file and change the \"%s\"\n"
1167 "Content-Transfer-Encoding to one of those. For now",
1168 ci
->ci_type
, ci
->ci_subtype
, ct
->c_file
, bp
);
1175 for (kv
= SubMultiPart
; kv
->kv_key
; kv
++)
1176 if (!strcasecmp (ci
->ci_subtype
, kv
->kv_key
))
1178 ct
->c_subtype
= kv
->kv_value
;
1181 * Check for "boundary" parameter, which is
1182 * required for multipart messages.
1185 for (pm
= ci
->ci_first_pm
; pm
; pm
= pm
->pm_next
) {
1186 if (!strcasecmp (pm
->pm_name
, "boundary")) {
1192 /* complain if boundary parameter is missing */
1195 "a \"boundary\" parameter is mandatory for \"%s/%s\" type in message %s's %s: field",
1196 ci
->ci_type
, ci
->ci_subtype
, ct
->c_file
, TYPE_FIELD
);
1200 /* allocate primary structure for multipart info */
1201 if ((m
= (struct multipart
*) calloc (1, sizeof(*m
))) == NULL
)
1202 adios (NULL
, "out of memory");
1203 ct
->c_ctparams
= (void *) m
;
1205 /* check if boundary parameter contains only whitespace characters */
1206 for (cp
= bp
; isspace ((unsigned char) *cp
); cp
++)
1209 advise (NULL
, "invalid \"boundary\" parameter for \"%s/%s\" type in message %s's %s: field",
1210 ci
->ci_type
, ci
->ci_subtype
, ct
->c_file
, TYPE_FIELD
);
1214 /* remove trailing whitespace from boundary parameter */
1215 for (cp
= bp
, dp
= cp
+ strlen (cp
) - 1; dp
> cp
; dp
--)
1216 if (!isspace ((unsigned char) *dp
))
1220 /* record boundary separators */
1221 m
->mp_start
= concat (bp
, "\n", NULL
);
1222 m
->mp_stop
= concat (bp
, "--\n", NULL
);
1224 if (!ct
->c_fp
&& (ct
->c_fp
= fopen (ct
->c_file
, "r")) == NULL
) {
1225 advise (ct
->c_file
, "unable to open for reading");
1229 fseek (fp
= ct
->c_fp
, pos
= ct
->c_begin
, SEEK_SET
);
1231 next
= &m
->mp_parts
;
1235 while (fgets (buffer
, sizeof(buffer
) - 1, fp
)) {
1239 pos
+= strlen (buffer
);
1240 if (buffer
[0] != '-' || buffer
[1] != '-')
1243 if (strcmp (buffer
+ 2, m
->mp_start
))
1246 if ((part
= (struct part
*) calloc (1, sizeof(*part
))) == NULL
)
1247 adios (NULL
, "out of memory");
1249 next
= &part
->mp_next
;
1251 if (!(p
= get_content (fp
, ct
->c_file
,
1252 ct
->c_subtype
== MULTI_DIGEST
? -1 : 0))) {
1259 fseek (fp
, pos
, SEEK_SET
);
1262 if (strcmp (buffer
+ 2, m
->mp_start
) == 0) {
1266 p
->c_end
= ftell(fp
) - (strlen(buffer
) + 1);
1267 if (p
->c_end
< p
->c_begin
)
1268 p
->c_begin
= p
->c_end
;
1273 if (strcmp (buffer
+ 2, m
->mp_stop
) == 0)
1279 if (! suppress_bogus_mp_content_warning
) {
1280 advise (NULL
, "bogus multipart content in message %s", ct
->c_file
);
1282 bogus_mp_content
= 1;
1284 if (!inout
&& part
) {
1286 p
->c_end
= ct
->c_end
;
1288 if (p
->c_begin
>= p
->c_end
) {
1289 for (next
= &m
->mp_parts
; *next
!= part
;
1290 next
= &((*next
)->mp_next
))
1294 free ((char *) part
);
1299 /* reverse the order of the parts for multipart/alternative */
1300 if (ct
->c_subtype
== MULTI_ALTERNATE
)
1304 * label all subparts with part number, and
1305 * then initialize the content of the subpart.
1310 char partnam
[BUFSIZ
];
1313 snprintf (partnam
, sizeof(partnam
), "%s.", ct
->c_partno
);
1314 pp
= partnam
+ strlen (partnam
);
1319 for (part
= m
->mp_parts
, partnum
= 1; part
;
1320 part
= part
->mp_next
, partnum
++) {
1323 sprintf (pp
, "%d", partnum
);
1324 p
->c_partno
= add (partnam
, NULL
);
1326 /* initialize the content of the subparts */
1327 if (p
->c_ctinitfnx
&& (*p
->c_ctinitfnx
) (p
) == NOTOK
) {
1335 get_leftover_mp_content (ct
, 1);
1336 get_leftover_mp_content (ct
, 0);
1345 * reverse the order of the parts of a multipart/alternative
1349 reverse_parts (CT ct
)
1351 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
1355 /* Reverse the order of its parts by walking the mp_parts list
1356 and pushing each node to the front. */
1357 for (part
= m
->mp_parts
, m
->mp_parts
= NULL
; part
; part
= next
) {
1358 next
= part
->mp_next
;
1359 part
->mp_next
= m
->mp_parts
;
1373 CI ci
= &ct
->c_ctinfo
;
1375 if ((ct
->c_encoding
!= CE_7BIT
) && (ct
->c_encoding
!= CE_8BIT
)) {
1377 "\"%s/%s\" type in message %s should be encoded in 7bit or 8bit",
1378 ci
->ci_type
, ci
->ci_subtype
, ct
->c_file
);
1382 /* check for missing subtype */
1383 if (!*ci
->ci_subtype
)
1384 ci
->ci_subtype
= add ("rfc822", ci
->ci_subtype
);
1387 for (kv
= SubMessage
; kv
->kv_key
; kv
++)
1388 if (!strcasecmp (ci
->ci_subtype
, kv
->kv_key
))
1390 ct
->c_subtype
= kv
->kv_value
;
1392 switch (ct
->c_subtype
) {
1393 case MESSAGE_RFC822
:
1396 case MESSAGE_PARTIAL
:
1401 if ((p
= (struct partial
*) calloc (1, sizeof(*p
))) == NULL
)
1402 adios (NULL
, "out of memory");
1403 ct
->c_ctparams
= (void *) p
;
1405 /* scan for parameters "id", "number", and "total" */
1406 for (pm
= ci
->ci_first_pm
; pm
; pm
= pm
->pm_next
) {
1407 if (!strcasecmp (pm
->pm_name
, "id")) {
1408 p
->pm_partid
= add (pm
->pm_value
, NULL
);
1411 if (!strcasecmp (pm
->pm_name
, "number")) {
1412 if (sscanf (pm
->pm_value
, "%d", &p
->pm_partno
) != 1
1413 || p
->pm_partno
< 1) {
1416 "invalid %s parameter for \"%s/%s\" type in message %s's %s field",
1417 pm
->pm_name
, ci
->ci_type
, ci
->ci_subtype
,
1418 ct
->c_file
, TYPE_FIELD
);
1423 if (!strcasecmp (pm
->pm_name
, "total")) {
1424 if (sscanf (pm
->pm_value
, "%d", &p
->pm_maxno
) != 1
1433 || (p
->pm_maxno
&& p
->pm_partno
> p
->pm_maxno
)) {
1435 "invalid parameters for \"%s/%s\" type in message %s's %s field",
1436 ci
->ci_type
, ci
->ci_subtype
,
1437 ct
->c_file
, TYPE_FIELD
);
1443 case MESSAGE_EXTERNAL
:
1450 if ((e
= (struct exbody
*) calloc (1, sizeof(*e
))) == NULL
)
1451 adios (NULL
, "out of memory");
1452 ct
->c_ctparams
= (void *) e
;
1455 && (ct
->c_fp
= fopen (ct
->c_file
, "r")) == NULL
) {
1456 advise (ct
->c_file
, "unable to open for reading");
1460 fseek (fp
= ct
->c_fp
, ct
->c_begin
, SEEK_SET
);
1462 if (!(p
= get_content (fp
, ct
->c_file
, 0))) {
1470 p
->c_ceopenfnx
= NULL
;
1471 if ((exresult
= params_external (ct
, 0)) != NOTOK
1472 && p
->c_ceopenfnx
== openMail
) {
1476 if ((size
= ct
->c_end
- p
->c_begin
) <= 0) {
1478 content_error (NULL
, ct
,
1479 "empty body for access-type=mail-server");
1483 e
->eb_body
= bp
= mh_xmalloc ((unsigned) size
);
1484 fseek (p
->c_fp
, p
->c_begin
, SEEK_SET
);
1486 switch (cc
= fread (bp
, sizeof(*bp
), size
, p
->c_fp
)) {
1488 adios ("failed", "fread");
1491 adios (NULL
, "unexpected EOF from fread");
1494 bp
+= cc
, size
-= cc
;
1501 p
->c_end
= p
->c_begin
;
1506 if (exresult
== NOTOK
)
1508 if (e
->eb_flags
== NOTOK
)
1511 switch (p
->c_type
) {
1516 if (p
->c_subtype
!= MESSAGE_RFC822
)
1520 e
->eb_partno
= ct
->c_partno
;
1522 (*p
->c_ctinitfnx
) (p
);
1537 params_external (CT ct
, int composing
)
1540 struct exbody
*e
= (struct exbody
*) ct
->c_ctparams
;
1541 CI ci
= &ct
->c_ctinfo
;
1543 ct
->c_ceopenfnx
= NULL
;
1544 for (pm
= ci
->ci_first_pm
; pm
; pm
= pm
->pm_next
) {
1545 if (!strcasecmp (pm
->pm_name
, "access-type")) {
1546 struct str2init
*s2i
;
1547 CT p
= e
->eb_content
;
1549 for (s2i
= str2methods
; s2i
->si_key
; s2i
++)
1550 if (!strcasecmp (pm
->pm_value
, s2i
->si_key
))
1553 e
->eb_access
= pm
->pm_value
;
1554 e
->eb_flags
= NOTOK
;
1555 p
->c_encoding
= CE_EXTERNAL
;
1558 e
->eb_access
= s2i
->si_key
;
1559 e
->eb_flags
= s2i
->si_val
;
1560 p
->c_encoding
= CE_EXTERNAL
;
1562 /* Call the Init function for this external type */
1563 if ((*s2i
->si_init
)(p
) == NOTOK
)
1567 if (!strcasecmp (pm
->pm_name
, "name")) {
1568 e
->eb_name
= pm
->pm_value
;
1571 if (!strcasecmp (pm
->pm_name
, "permission")) {
1572 e
->eb_permission
= pm
->pm_value
;
1575 if (!strcasecmp (pm
->pm_name
, "site")) {
1576 e
->eb_site
= pm
->pm_value
;
1579 if (!strcasecmp (pm
->pm_name
, "directory")) {
1580 e
->eb_dir
= pm
->pm_value
;
1583 if (!strcasecmp (pm
->pm_name
, "mode")) {
1584 e
->eb_mode
= pm
->pm_value
;
1587 if (!strcasecmp (pm
->pm_name
, "size")) {
1588 sscanf (pm
->pm_value
, "%lu", &e
->eb_size
);
1591 if (!strcasecmp (pm
->pm_name
, "server")) {
1592 e
->eb_server
= pm
->pm_value
;
1595 if (!strcasecmp (pm
->pm_name
, "subject")) {
1596 e
->eb_subject
= pm
->pm_value
;
1599 if (!strcasecmp (pm
->pm_name
, "url")) {
1601 * According to RFC 2017, we have to remove all whitespace from
1605 char *u
, *p
= pm
->pm_value
;
1606 e
->eb_url
= u
= mh_xmalloc(strlen(pm
->pm_value
) + 1);
1608 for (; *p
!= '\0'; p
++) {
1609 if (! isspace((unsigned char) *p
))
1616 if (composing
&& !strcasecmp (pm
->pm_name
, "body")) {
1617 e
->eb_body
= getcpy (pm
->pm_value
);
1622 if (!e
->eb_access
) {
1624 "invalid parameters for \"%s/%s\" type in message %s's %s field",
1625 ci
->ci_type
, ci
->ci_subtype
, ct
->c_file
, TYPE_FIELD
);
1638 InitApplication (CT ct
)
1641 CI ci
= &ct
->c_ctinfo
;
1644 for (kv
= SubApplication
; kv
->kv_key
; kv
++)
1645 if (!strcasecmp (ci
->ci_subtype
, kv
->kv_key
))
1647 ct
->c_subtype
= kv
->kv_value
;
1654 * TRANSFER ENCODINGS
1658 init_encoding (CT ct
, OpenCEFunc openfnx
)
1660 ct
->c_ceopenfnx
= openfnx
;
1661 ct
->c_ceclosefnx
= close_encoding
;
1662 ct
->c_cesizefnx
= size_encoding
;
1669 close_encoding (CT ct
)
1671 CE ce
= &ct
->c_cefile
;
1680 static unsigned long
1681 size_encoding (CT ct
)
1686 CE ce
= &ct
->c_cefile
;
1689 if (ce
->ce_fp
&& fstat (fileno (ce
->ce_fp
), &st
) != NOTOK
)
1690 return (long) st
.st_size
;
1693 if (stat (ce
->ce_file
, &st
) != NOTOK
)
1694 return (long) st
.st_size
;
1699 if (ct
->c_encoding
== CE_EXTERNAL
)
1700 return (ct
->c_end
- ct
->c_begin
);
1703 if ((fd
= (*ct
->c_ceopenfnx
) (ct
, &file
)) == NOTOK
)
1704 return (ct
->c_end
- ct
->c_begin
);
1706 if (fstat (fd
, &st
) != NOTOK
)
1707 size
= (long) st
.st_size
;
1711 (*ct
->c_ceclosefnx
) (ct
);
1720 static unsigned char b642nib
[0x80] = {
1721 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1722 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1723 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1724 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1725 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1726 0xff, 0xff, 0xff, 0x3e, 0xff, 0xff, 0xff, 0x3f,
1727 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b,
1728 0x3c, 0x3d, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1729 0xff, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
1730 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e,
1731 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16,
1732 0x17, 0x18, 0x19, 0xff, 0xff, 0xff, 0xff, 0xff,
1733 0xff, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20,
1734 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28,
1735 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30,
1736 0x31, 0x32, 0x33, 0xff, 0xff, 0xff, 0xff, 0xff
1743 return init_encoding (ct
, openBase64
);
1748 openBase64 (CT ct
, char **file
)
1750 int bitno
, cc
, digested
;
1751 int fd
, len
, skip
, own_ct_fp
= 0, text
= ct
->c_type
== CT_TEXT
;
1753 unsigned char value
, b
;
1754 char *cp
, *ep
, buffer
[BUFSIZ
];
1755 /* sbeck -- handle suffixes */
1757 CE ce
= &ct
->c_cefile
;
1761 fseek (ce
->ce_fp
, 0L, SEEK_SET
);
1766 if ((ce
->ce_fp
= fopen (ce
->ce_file
, "r")) == NULL
) {
1767 content_error (ce
->ce_file
, ct
, "unable to fopen for reading");
1773 if (*file
== NULL
) {
1776 ce
->ce_file
= add (*file
, NULL
);
1780 /* sbeck@cise.ufl.edu -- handle suffixes */
1782 snprintf (buffer
, sizeof(buffer
), "%s-suffix-%s/%s",
1783 invo_name
, ci
->ci_type
, ci
->ci_subtype
);
1784 cp
= context_find (buffer
);
1785 if (cp
== NULL
|| *cp
== '\0') {
1786 snprintf (buffer
, sizeof(buffer
), "%s-suffix-%s", invo_name
,
1788 cp
= context_find (buffer
);
1790 if (cp
!= NULL
&& *cp
!= '\0') {
1791 if (ce
->ce_unlink
) {
1792 /* Create temporary file with filename extension. */
1793 if ((ce
->ce_file
= m_mktemps(invo_name
, cp
, NULL
, NULL
)) == NULL
) {
1794 adios(NULL
, "unable to create temporary file in %s",
1798 ce
->ce_file
= add (cp
, ce
->ce_file
);
1800 } else if (*file
== NULL
) {
1802 if ((tempfile
= m_mktemp2(NULL
, invo_name
, NULL
, NULL
)) == NULL
) {
1803 adios(NULL
, "unable to create temporary file in %s",
1806 ce
->ce_file
= add (tempfile
, NULL
);
1809 if ((ce
->ce_fp
= fopen (ce
->ce_file
, "w+")) == NULL
) {
1810 content_error (ce
->ce_file
, ct
, "unable to fopen for reading/writing");
1814 if ((len
= ct
->c_end
- ct
->c_begin
) < 0)
1815 adios (NULL
, "internal error(1)");
1818 if ((ct
->c_fp
= fopen (ct
->c_file
, "r")) == NULL
) {
1819 content_error (ct
->c_file
, ct
, "unable to open for reading");
1825 if ((digested
= ct
->c_digested
))
1826 MD5Init (&mdContext
);
1832 lseek (fd
= fileno (ct
->c_fp
), (off_t
) ct
->c_begin
, SEEK_SET
);
1834 switch (cc
= read (fd
, buffer
, sizeof(buffer
) - 1)) {
1836 content_error (ct
->c_file
, ct
, "error reading from");
1840 content_error (NULL
, ct
, "premature eof");
1848 for (ep
= (cp
= buffer
) + cc
; cp
< ep
; cp
++) {
1851 if (isspace ((unsigned char) *cp
))
1853 if (skip
|| (((unsigned char) *cp
) & 0x80)
1854 || (value
= b642nib
[((unsigned char) *cp
) & 0x7f]) > 0x3f) {
1856 fprintf (stderr
, "*cp=0x%x pos=%ld skip=%d\n",
1857 (unsigned char) *cp
,
1858 (long) (lseek (fd
, (off_t
) 0, SEEK_CUR
) - (ep
- cp
)),
1861 content_error (NULL
, ct
,
1862 "invalid BASE64 encoding -- continuing");
1866 bits
|= value
<< bitno
;
1868 if ((bitno
-= 6) < 0) {
1869 b
= (bits
>> 16) & 0xff;
1870 if (!text
|| b
!= '\r')
1871 putc ((char) b
, ce
->ce_fp
);
1873 MD5Update (&mdContext
, &b
, 1);
1875 b
= (bits
>> 8) & 0xff;
1876 if (! text
|| b
!= '\r')
1877 putc ((char) b
, ce
->ce_fp
);
1879 MD5Update (&mdContext
, &b
, 1);
1882 if (! text
|| b
!= '\r')
1883 putc ((char) b
, ce
->ce_fp
);
1885 MD5Update (&mdContext
, &b
, 1);
1889 if (ferror (ce
->ce_fp
)) {
1890 content_error (ce
->ce_file
, ct
,
1891 "error writing to");
1894 bitno
= 18, bits
= 0L, skip
= 0;
1900 goto self_delimiting
;
1909 fprintf (stderr
, "premature ending (bitno %d)\n", bitno
);
1911 content_error (NULL
, ct
, "invalid BASE64 encoding");
1916 fseek (ct
->c_fp
, 0L, SEEK_SET
);
1918 if (fflush (ce
->ce_fp
)) {
1919 content_error (ce
->ce_file
, ct
, "error writing to");
1924 unsigned char digest
[16];
1926 MD5Final (digest
, &mdContext
);
1927 if (memcmp((char *) digest
, (char *) ct
->c_digest
,
1928 sizeof(digest
) / sizeof(digest
[0])))
1929 content_error (NULL
, ct
,
1930 "content integrity suspect (digest mismatch) -- continuing");
1933 fprintf (stderr
, "content integrity confirmed\n");
1936 fseek (ce
->ce_fp
, 0L, SEEK_SET
);
1939 *file
= ce
->ce_file
;
1944 return fileno (ce
->ce_fp
);
1951 free_encoding (ct
, 0);
1960 static char hex2nib
[0x80] = {
1961 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1962 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1963 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1964 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1965 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1966 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1967 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
1968 0x08, 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1969 0x00, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x00,
1970 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1971 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1972 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1973 0x00, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x00,
1974 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1975 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1976 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
1983 return init_encoding (ct
, openQuoted
);
1988 openQuoted (CT ct
, char **file
)
1990 int cc
, digested
, len
, quoted
, own_ct_fp
= 0;
1992 char buffer
[BUFSIZ
];
1994 CE ce
= &ct
->c_cefile
;
1995 /* sbeck -- handle suffixes */
2000 fseek (ce
->ce_fp
, 0L, SEEK_SET
);
2005 if ((ce
->ce_fp
= fopen (ce
->ce_file
, "r")) == NULL
) {
2006 content_error (ce
->ce_file
, ct
, "unable to fopen for reading");
2012 if (*file
== NULL
) {
2015 ce
->ce_file
= add (*file
, NULL
);
2019 /* sbeck@cise.ufl.edu -- handle suffixes */
2021 snprintf (buffer
, sizeof(buffer
), "%s-suffix-%s/%s",
2022 invo_name
, ci
->ci_type
, ci
->ci_subtype
);
2023 cp
= context_find (buffer
);
2024 if (cp
== NULL
|| *cp
== '\0') {
2025 snprintf (buffer
, sizeof(buffer
), "%s-suffix-%s", invo_name
,
2027 cp
= context_find (buffer
);
2029 if (cp
!= NULL
&& *cp
!= '\0') {
2030 if (ce
->ce_unlink
) {
2031 /* Create temporary file with filename extension. */
2032 if ((ce
->ce_file
= m_mktemps(invo_name
, cp
, NULL
, NULL
)) == NULL
) {
2033 adios(NULL
, "unable to create temporary file in %s",
2037 ce
->ce_file
= add (cp
, ce
->ce_file
);
2039 } else if (*file
== NULL
) {
2041 if ((tempfile
= m_mktemp2(NULL
, invo_name
, NULL
, NULL
)) == NULL
) {
2042 adios(NULL
, "unable to create temporary file in %s",
2045 ce
->ce_file
= add (tempfile
, NULL
);
2048 if ((ce
->ce_fp
= fopen (ce
->ce_file
, "w+")) == NULL
) {
2049 content_error (ce
->ce_file
, ct
, "unable to fopen for reading/writing");
2053 if ((len
= ct
->c_end
- ct
->c_begin
) < 0)
2054 adios (NULL
, "internal error(2)");
2057 if ((ct
->c_fp
= fopen (ct
->c_file
, "r")) == NULL
) {
2058 content_error (ct
->c_file
, ct
, "unable to open for reading");
2064 if ((digested
= ct
->c_digested
))
2065 MD5Init (&mdContext
);
2072 fseek (ct
->c_fp
, ct
->c_begin
, SEEK_SET
);
2074 if (fgets (buffer
, sizeof(buffer
) - 1, ct
->c_fp
) == NULL
) {
2075 content_error (NULL
, ct
, "premature eof");
2079 if ((cc
= strlen (buffer
)) > len
)
2083 for (ep
= (cp
= buffer
) + cc
- 1; cp
<= ep
; ep
--)
2084 if (!isspace ((unsigned char) *ep
))
2088 for (; cp
< ep
; cp
++) {
2090 /* in an escape sequence */
2092 /* at byte 1 of an escape sequence */
2093 mask
= hex2nib
[((unsigned char) *cp
) & 0x7f];
2094 /* next is byte 2 */
2097 /* at byte 2 of an escape sequence */
2099 mask
|= hex2nib
[((unsigned char) *cp
) & 0x7f];
2100 putc (mask
, ce
->ce_fp
);
2102 MD5Update (&mdContext
, &mask
, 1);
2103 if (ferror (ce
->ce_fp
)) {
2104 content_error (ce
->ce_file
, ct
, "error writing to");
2107 /* finished escape sequence; next may be literal or a new
2108 * escape sequence */
2111 /* on to next byte */
2115 /* not in an escape sequence */
2117 /* starting an escape sequence, or invalid '='? */
2118 if (cp
+ 1 < ep
&& cp
[1] == '\n') {
2119 /* "=\n" soft line break, eat the \n */
2123 if (cp
+ 1 >= ep
|| cp
+ 2 >= ep
) {
2124 /* We don't have 2 bytes left, so this is an invalid
2125 * escape sequence; just show the raw bytes (below). */
2126 } else if (isxdigit ((unsigned char) cp
[1]) &&
2127 isxdigit ((unsigned char) cp
[2])) {
2128 /* Next 2 bytes are hex digits, making this a valid escape
2129 * sequence; let's decode it (above). */
2133 /* One or both of the next 2 is out of range, making this
2134 * an invalid escape sequence; just show the raw bytes
2139 /* Just show the raw byte. */
2140 putc (*cp
, ce
->ce_fp
);
2143 MD5Update (&mdContext
, (unsigned char *) "\r\n",2);
2145 MD5Update (&mdContext
, (unsigned char *) cp
, 1);
2148 if (ferror (ce
->ce_fp
)) {
2149 content_error (ce
->ce_file
, ct
, "error writing to");
2155 content_error (NULL
, ct
,
2156 "invalid QUOTED-PRINTABLE encoding -- end-of-content while still quoting");
2160 fseek (ct
->c_fp
, 0L, SEEK_SET
);
2162 if (fflush (ce
->ce_fp
)) {
2163 content_error (ce
->ce_file
, ct
, "error writing to");
2168 unsigned char digest
[16];
2170 MD5Final (digest
, &mdContext
);
2171 if (memcmp((char *) digest
, (char *) ct
->c_digest
,
2172 sizeof(digest
) / sizeof(digest
[0])))
2173 content_error (NULL
, ct
,
2174 "content integrity suspect (digest mismatch) -- continuing");
2177 fprintf (stderr
, "content integrity confirmed\n");
2180 fseek (ce
->ce_fp
, 0L, SEEK_SET
);
2183 *file
= ce
->ce_file
;
2188 return fileno (ce
->ce_fp
);
2191 free_encoding (ct
, 0);
2207 if (init_encoding (ct
, open7Bit
) == NOTOK
)
2210 ct
->c_cesizefnx
= NULL
; /* no need to decode for real size */
2216 open7Bit (CT ct
, char **file
)
2218 int cc
, fd
, len
, own_ct_fp
= 0;
2219 char buffer
[BUFSIZ
];
2220 /* sbeck -- handle suffixes */
2223 CE ce
= &ct
->c_cefile
;
2226 fseek (ce
->ce_fp
, 0L, SEEK_SET
);
2231 if ((ce
->ce_fp
= fopen (ce
->ce_file
, "r")) == NULL
) {
2232 content_error (ce
->ce_file
, ct
, "unable to fopen for reading");
2238 if (*file
== NULL
) {
2241 ce
->ce_file
= add (*file
, NULL
);
2245 /* sbeck@cise.ufl.edu -- handle suffixes */
2247 snprintf (buffer
, sizeof(buffer
), "%s-suffix-%s/%s",
2248 invo_name
, ci
->ci_type
, ci
->ci_subtype
);
2249 cp
= context_find (buffer
);
2250 if (cp
== NULL
|| *cp
== '\0') {
2251 snprintf (buffer
, sizeof(buffer
), "%s-suffix-%s", invo_name
,
2253 cp
= context_find (buffer
);
2255 if (cp
!= NULL
&& *cp
!= '\0') {
2256 if (ce
->ce_unlink
) {
2257 /* Create temporary file with filename extension. */
2258 if ((ce
->ce_file
= m_mktemps(invo_name
, cp
, NULL
, NULL
)) == NULL
) {
2259 adios(NULL
, "unable to create temporary file in %s",
2263 ce
->ce_file
= add (cp
, ce
->ce_file
);
2265 } else if (*file
== NULL
) {
2267 if ((tempfile
= m_mktemp2(NULL
, invo_name
, NULL
, NULL
)) == NULL
) {
2268 adios(NULL
, "unable to create temporary file in %s",
2271 ce
->ce_file
= add (tempfile
, NULL
);
2274 if ((ce
->ce_fp
= fopen (ce
->ce_file
, "w+")) == NULL
) {
2275 content_error (ce
->ce_file
, ct
, "unable to fopen for reading/writing");
2279 if (ct
->c_type
== CT_MULTIPART
) {
2280 CI ci
= &ct
->c_ctinfo
;
2284 fprintf (ce
->ce_fp
, "%s: %s/%s", TYPE_FIELD
, ci
->ci_type
, ci
->ci_subtype
);
2285 len
+= strlen (TYPE_FIELD
) + 2 + strlen (ci
->ci_type
)
2286 + 1 + strlen (ci
->ci_subtype
);
2287 buffer
= output_params(len
, ci
->ci_first_pm
, &len
);
2290 fputs (buffer
, ce
->ce_fp
);
2294 if (ci
->ci_comment
) {
2295 if (len
+ 1 + (cc
= 2 + strlen (ci
->ci_comment
)) >= CPERLIN
) {
2296 fputs ("\n\t", ce
->ce_fp
);
2300 putc (' ', ce
->ce_fp
);
2303 fprintf (ce
->ce_fp
, "(%s)", ci
->ci_comment
);
2306 fprintf (ce
->ce_fp
, "\n");
2308 fprintf (ce
->ce_fp
, "%s:%s", ID_FIELD
, ct
->c_id
);
2310 fprintf (ce
->ce_fp
, "%s:%s", DESCR_FIELD
, ct
->c_descr
);
2312 fprintf (ce
->ce_fp
, "%s:%s", DISPO_FIELD
, ct
->c_dispo
);
2313 fprintf (ce
->ce_fp
, "\n");
2316 if ((len
= ct
->c_end
- ct
->c_begin
) < 0)
2317 adios (NULL
, "internal error(3)");
2320 if ((ct
->c_fp
= fopen (ct
->c_file
, "r")) == NULL
) {
2321 content_error (ct
->c_file
, ct
, "unable to open for reading");
2327 lseek (fd
= fileno (ct
->c_fp
), (off_t
) ct
->c_begin
, SEEK_SET
);
2329 switch (cc
= read (fd
, buffer
, sizeof(buffer
) - 1)) {
2331 content_error (ct
->c_file
, ct
, "error reading from");
2335 content_error (NULL
, ct
, "premature eof");
2343 fwrite (buffer
, sizeof(*buffer
), cc
, ce
->ce_fp
);
2344 if (ferror (ce
->ce_fp
)) {
2345 content_error (ce
->ce_file
, ct
, "error writing to");
2350 fseek (ct
->c_fp
, 0L, SEEK_SET
);
2352 if (fflush (ce
->ce_fp
)) {
2353 content_error (ce
->ce_file
, ct
, "error writing to");
2357 fseek (ce
->ce_fp
, 0L, SEEK_SET
);
2360 *file
= ce
->ce_file
;
2365 return fileno (ce
->ce_fp
);
2368 free_encoding (ct
, 0);
2382 openExternal (CT ct
, CT cb
, CE ce
, char **file
, int *fd
)
2384 char cachefile
[BUFSIZ
];
2387 fseek (ce
->ce_fp
, 0L, SEEK_SET
);
2392 if ((ce
->ce_fp
= fopen (ce
->ce_file
, "r")) == NULL
) {
2393 content_error (ce
->ce_file
, ct
, "unable to fopen for reading");
2399 if (find_cache (ct
, rcachesw
, (int *) 0, cb
->c_id
,
2400 cachefile
, sizeof(cachefile
)) != NOTOK
) {
2401 if ((ce
->ce_fp
= fopen (cachefile
, "r"))) {
2402 ce
->ce_file
= getcpy (cachefile
);
2406 admonish (cachefile
, "unable to fopen for reading");
2413 *file
= ce
->ce_file
;
2414 *fd
= fileno (ce
->ce_fp
);
2425 return init_encoding (ct
, openFile
);
2430 openFile (CT ct
, char **file
)
2433 char cachefile
[BUFSIZ
];
2434 struct exbody
*e
= ct
->c_ctexbody
;
2435 CE ce
= &ct
->c_cefile
;
2437 switch (openExternal (e
->eb_parent
, e
->eb_content
, ce
, file
, &fd
)) {
2449 content_error (NULL
, ct
, "missing name parameter");
2453 ce
->ce_file
= getcpy (e
->eb_name
);
2456 if ((ce
->ce_fp
= fopen (ce
->ce_file
, "r")) == NULL
) {
2457 content_error (ce
->ce_file
, ct
, "unable to fopen for reading");
2461 if ((!e
->eb_permission
|| strcasecmp (e
->eb_permission
, "read-write"))
2462 && find_cache (NULL
, wcachesw
, &cachetype
, e
->eb_content
->c_id
,
2463 cachefile
, sizeof(cachefile
)) != NOTOK
) {
2467 mask
= umask (cachetype
? ~m_gmprot () : 0222);
2468 if ((fp
= fopen (cachefile
, "w"))) {
2470 char buffer
[BUFSIZ
];
2471 FILE *gp
= ce
->ce_fp
;
2473 fseek (gp
, 0L, SEEK_SET
);
2475 while ((cc
= fread (buffer
, sizeof(*buffer
), sizeof(buffer
), gp
))
2477 fwrite (buffer
, sizeof(*buffer
), cc
, fp
);
2481 admonish (ce
->ce_file
, "error reading");
2482 (void) m_unlink (cachefile
);
2486 admonish (cachefile
, "error writing");
2487 (void) m_unlink (cachefile
);
2494 fseek (ce
->ce_fp
, 0L, SEEK_SET
);
2495 *file
= ce
->ce_file
;
2496 return fileno (ce
->ce_fp
);
2506 return init_encoding (ct
, openFTP
);
2511 openFTP (CT ct
, char **file
)
2513 int cachetype
, caching
, fd
;
2515 char *bp
, *ftp
, *user
, *pass
;
2516 char buffer
[BUFSIZ
], cachefile
[BUFSIZ
];
2518 CE ce
= &ct
->c_cefile
;
2519 static char *username
= NULL
;
2520 static char *password
= NULL
;
2524 if ((ftp
= context_find (nmhaccessftp
)) && !*ftp
)
2530 switch (openExternal (e
->eb_parent
, e
->eb_content
, ce
, file
, &fd
)) {
2541 if (!e
->eb_name
|| !e
->eb_site
) {
2542 content_error (NULL
, ct
, "missing %s parameter",
2543 e
->eb_name
? "site": "name");
2550 pidcheck (pidwait (xpid
, NOTOK
));
2554 /* Get the buffer ready to go */
2556 buflen
= sizeof(buffer
);
2559 * Construct the query message for user
2561 snprintf (bp
, buflen
, "Retrieve %s", e
->eb_name
);
2567 snprintf (bp
, buflen
, " (content %s)", e
->eb_partno
);
2573 snprintf (bp
, buflen
, "\n using %sFTP from site %s",
2574 e
->eb_flags
? "anonymous " : "", e
->eb_site
);
2579 if (e
->eb_size
> 0) {
2580 snprintf (bp
, buflen
, " (%lu octets)", e
->eb_size
);
2585 snprintf (bp
, buflen
, "? ");
2588 * Now, check the answer
2590 if (!getanswer (buffer
))
2595 snprintf (buffer
, sizeof(buffer
), "%s@%s", getusername (),
2599 ruserpass (e
->eb_site
, &username
, &password
);
2604 ce
->ce_unlink
= (*file
== NULL
);
2606 cachefile
[0] = '\0';
2607 if ((!e
->eb_permission
|| strcasecmp (e
->eb_permission
, "read-write"))
2608 && find_cache (NULL
, wcachesw
, &cachetype
, e
->eb_content
->c_id
,
2609 cachefile
, sizeof(cachefile
)) != NOTOK
) {
2610 if (*file
== NULL
) {
2617 ce
->ce_file
= add (*file
, NULL
);
2619 ce
->ce_file
= add (cachefile
, NULL
);
2622 if ((tempfile
= m_mktemp2(NULL
, invo_name
, NULL
, NULL
)) == NULL
) {
2623 adios(NULL
, "unable to create temporary file in %s",
2626 ce
->ce_file
= add (tempfile
, NULL
);
2629 if ((ce
->ce_fp
= fopen (ce
->ce_file
, "w+")) == NULL
) {
2630 content_error (ce
->ce_file
, ct
, "unable to fopen for reading/writing");
2635 int child_id
, i
, vecp
;
2639 vec
[vecp
++] = r1bindex (ftp
, '/');
2640 vec
[vecp
++] = e
->eb_site
;
2643 vec
[vecp
++] = e
->eb_dir
;
2644 vec
[vecp
++] = e
->eb_name
;
2645 vec
[vecp
++] = ce
->ce_file
,
2646 vec
[vecp
++] = e
->eb_mode
&& !strcasecmp (e
->eb_mode
, "ascii")
2647 ? "ascii" : "binary";
2652 for (i
= 0; (child_id
= fork()) == NOTOK
&& i
< 5; i
++)
2656 adios ("fork", "unable to");
2660 close (fileno (ce
->ce_fp
));
2662 fprintf (stderr
, "unable to exec ");
2668 if (pidXwait (child_id
, NULL
)) {
2669 username
= password
= NULL
;
2679 chmod (cachefile
, cachetype
? m_gmprot () : 0444);
2684 mask
= umask (cachetype
? ~m_gmprot () : 0222);
2685 if ((fp
= fopen (cachefile
, "w"))) {
2687 FILE *gp
= ce
->ce_fp
;
2689 fseek (gp
, 0L, SEEK_SET
);
2691 while ((cc
= fread (buffer
, sizeof(*buffer
), sizeof(buffer
), gp
))
2693 fwrite (buffer
, sizeof(*buffer
), cc
, fp
);
2697 admonish (ce
->ce_file
, "error reading");
2698 (void) m_unlink (cachefile
);
2702 admonish (cachefile
, "error writing");
2703 (void) m_unlink (cachefile
);
2711 fseek (ce
->ce_fp
, 0L, SEEK_SET
);
2712 *file
= ce
->ce_file
;
2713 return fileno (ce
->ce_fp
);
2724 return init_encoding (ct
, openMail
);
2729 openMail (CT ct
, char **file
)
2731 int child_id
, fd
, i
, vecp
;
2733 char *bp
, buffer
[BUFSIZ
], *vec
[7];
2734 struct exbody
*e
= ct
->c_ctexbody
;
2735 CE ce
= &ct
->c_cefile
;
2737 switch (openExternal (e
->eb_parent
, e
->eb_content
, ce
, file
, &fd
)) {
2748 if (!e
->eb_server
) {
2749 content_error (NULL
, ct
, "missing server parameter");
2756 pidcheck (pidwait (xpid
, NOTOK
));
2760 /* Get buffer ready to go */
2762 buflen
= sizeof(buffer
);
2764 /* Now, construct query message */
2765 snprintf (bp
, buflen
, "Retrieve content");
2771 snprintf (bp
, buflen
, " %s", e
->eb_partno
);
2777 snprintf (bp
, buflen
, " by asking %s\n\n%s\n? ",
2779 e
->eb_subject
? e
->eb_subject
: e
->eb_body
);
2781 /* Now, check answer */
2782 if (!getanswer (buffer
))
2786 vec
[vecp
++] = r1bindex (mailproc
, '/');
2787 vec
[vecp
++] = e
->eb_server
;
2788 vec
[vecp
++] = "-subject";
2789 vec
[vecp
++] = e
->eb_subject
? e
->eb_subject
: "mail-server request";
2790 vec
[vecp
++] = "-body";
2791 vec
[vecp
++] = e
->eb_body
;
2794 for (i
= 0; (child_id
= fork()) == NOTOK
&& i
< 5; i
++)
2798 advise ("fork", "unable to");
2802 execvp (mailproc
, vec
);
2803 fprintf (stderr
, "unable to exec ");
2809 if (pidXwait (child_id
, NULL
) == OK
)
2810 advise (NULL
, "request sent");
2814 if (*file
== NULL
) {
2816 if ((tempfile
= m_mktemp2(NULL
, invo_name
, NULL
, NULL
)) == NULL
) {
2817 adios(NULL
, "unable to create temporary file in %s",
2820 ce
->ce_file
= add (tempfile
, NULL
);
2823 ce
->ce_file
= add (*file
, NULL
);
2827 if ((ce
->ce_fp
= fopen (ce
->ce_file
, "w+")) == NULL
) {
2828 content_error (ce
->ce_file
, ct
, "unable to fopen for reading/writing");
2832 /* showproc is for mhshow and mhstore, though mhlist -debug
2833 * prints it, too. */
2835 free (ct
->c_showproc
);
2836 ct
->c_showproc
= add ("true", NULL
);
2838 fseek (ce
->ce_fp
, 0L, SEEK_SET
);
2839 *file
= ce
->ce_file
;
2840 return fileno (ce
->ce_fp
);
2851 return init_encoding (ct
, openURL
);
2856 openURL (CT ct
, char **file
)
2858 struct exbody
*e
= ct
->c_ctexbody
;
2859 CE ce
= &ct
->c_cefile
;
2860 char *urlprog
, *program
;
2861 char buffer
[BUFSIZ
], cachefile
[BUFSIZ
];
2862 int fd
, caching
, cachetype
;
2863 struct msgs_array args
= { 0, 0, NULL
};
2866 if ((urlprog
= context_find(nmhaccessurl
)) && *urlprog
== '\0')
2870 content_error(NULL
, ct
, "No entry for nmh-access-url in profile");
2874 switch (openExternal(e
->eb_parent
, e
->eb_content
, ce
, file
, &fd
)) {
2886 content_error(NULL
, ct
, "missing url parameter");
2893 pidcheck (pidwait (xpid
, NOTOK
));
2897 ce
->ce_unlink
= (*file
== NULL
);
2899 cachefile
[0] = '\0';
2901 if (find_cache(NULL
, wcachesw
, &cachetype
, e
->eb_content
->c_id
,
2902 cachefile
, sizeof(cachefile
)) != NOTOK
) {
2903 if (*file
== NULL
) {
2910 ce
->ce_file
= add(*file
, NULL
);
2912 ce
->ce_file
= add(cachefile
, NULL
);
2915 if ((tempfile
= m_mktemp2(NULL
, invo_name
, NULL
, NULL
)) == NULL
) {
2916 adios(NULL
, "unable to create temporary file in %s",
2919 ce
->ce_file
= add (tempfile
, NULL
);
2922 if ((ce
->ce_fp
= fopen(ce
->ce_file
, "w+")) == NULL
) {
2923 content_error(ce
->ce_file
, ct
, "unable to fopen for read/writing");
2927 switch (child_id
= fork()) {
2929 adios ("fork", "unable to");
2933 argsplit_msgarg(&args
, urlprog
, &program
);
2934 app_msgarg(&args
, e
->eb_url
);
2935 app_msgarg(&args
, NULL
);
2936 dup2(fileno(ce
->ce_fp
), 1);
2937 close(fileno(ce
->ce_fp
));
2938 execvp(program
, args
.msgs
);
2939 fprintf(stderr
, "Unable to exec ");
2945 if (pidXwait(child_id
, NULL
)) {
2953 chmod(cachefile
, cachetype
? m_gmprot() : 0444);
2958 mask
= umask (cachetype
? ~m_gmprot() : 0222);
2959 if ((fp
= fopen(cachefile
, "w"))) {
2961 FILE *gp
= ce
->ce_fp
;
2963 fseeko(gp
, 0, SEEK_SET
);
2965 while ((cc
= fread(buffer
, sizeof(*buffer
),
2966 sizeof(buffer
), gp
)) > 0)
2967 fwrite(buffer
, sizeof(*buffer
), cc
, fp
);
2972 admonish(ce
->ce_file
, "error reading");
2973 (void) m_unlink (cachefile
);
2980 fseeko(ce
->ce_fp
, 0, SEEK_SET
);
2981 *file
= ce
->ce_file
;
2986 readDigest (CT ct
, char *cp
)
2991 unsigned char *dp
, value
, *ep
;
2997 for (ep
= (dp
= ct
->c_digest
)
2998 + sizeof(ct
->c_digest
) / sizeof(ct
->c_digest
[0]); *cp
; cp
++)
3003 || (value
= b642nib
[*cp
& 0x7f]) > 0x3f) {
3005 fprintf (stderr
, "invalid BASE64 encoding\n");
3009 bits
|= value
<< bitno
;
3011 if ((bitno
-= 6) < 0) {
3012 if (dp
+ (3 - skip
) > ep
)
3013 goto invalid_digest
;
3014 *dp
++ = (bits
>> 16) & 0xff;
3016 *dp
++ = (bits
>> 8) & 0xff;
3018 *dp
++ = bits
& 0xff;
3028 goto self_delimiting
;
3033 fprintf (stderr
, "premature ending (bitno %d)\n", bitno
);
3043 fprintf (stderr
, "invalid MD5 digest (got %d octets)\n",
3051 fprintf (stderr
, "MD5 digest=");
3052 for (dp
= ct
->c_digest
; dp
< ep
; dp
++)
3053 fprintf (stderr
, "%02x", *dp
& 0xff);
3054 fprintf (stderr
, "\n");
3061 /* Multipart parts might have content before the first subpart and/or
3062 after the last subpart that hasn't been stored anywhere else, so do
3065 get_leftover_mp_content (CT ct
, int before
/* or after */) {
3066 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
3068 int found_boundary
= 0;
3069 char buffer
[BUFSIZ
];
3072 char *content
= NULL
;
3074 if (! m
) return NOTOK
;
3077 if (! m
->mp_parts
|| ! m
->mp_parts
->mp_part
) return NOTOK
;
3079 /* Isolate the beginning of this part to the beginning of the
3080 first subpart and save any content between them. */
3081 fseeko (ct
->c_fp
, ct
->c_begin
, SEEK_SET
);
3082 max
= m
->mp_parts
->mp_part
->c_begin
- ct
->c_begin
;
3083 boundary
= concat ("--", m
->mp_start
, NULL
);
3085 struct part
*last_subpart
= NULL
;
3086 struct part
*subpart
;
3088 /* Go to the last subpart to get its end position. */
3089 for (subpart
= m
->mp_parts
; subpart
; subpart
= subpart
->mp_next
) {
3090 last_subpart
= subpart
;
3093 if (last_subpart
== NULL
) return NOTOK
;
3095 /* Isolate the end of the last subpart to the end of this part
3096 and save any content between them. */
3097 fseeko (ct
->c_fp
, last_subpart
->mp_part
->c_end
, SEEK_SET
);
3098 max
= ct
->c_end
- last_subpart
->mp_part
->c_end
;
3099 boundary
= concat ("--", m
->mp_stop
, NULL
);
3102 /* Back up by 1 to pick up the newline. */
3103 while (fgets (buffer
, sizeof(buffer
) - 1, ct
->c_fp
)) {
3104 read
+= strlen (buffer
);
3105 /* Don't look beyond beginning of first subpart (before) or
3106 next part (after). */
3107 if (read
> max
) buffer
[read
-max
] = '\0';
3110 if (! strcmp (buffer
, boundary
)) {
3114 if (! found_boundary
&& ! strcmp (buffer
, boundary
)) {
3120 if ((before
&& ! found_boundary
) || (! before
&& found_boundary
)) {
3122 char *old_content
= content
;
3123 content
= concat (content
, buffer
, NULL
);
3127 ? concat ("\n", buffer
, NULL
)
3128 : concat (buffer
, NULL
);
3133 if (found_boundary
|| read
> max
) break;
3135 if (read
> max
) break;
3139 /* Skip the newline if that's all there is. */
3143 /* Remove trailing newline, except at EOF. */
3144 if ((before
|| ! feof (ct
->c_fp
)) &&
3145 (cp
= content
+ strlen (content
)) > content
&&
3150 if (strlen (content
) > 1) {
3152 m
->mp_content_before
= content
;
3154 m
->mp_content_after
= content
;
3168 ct_type_str (int type
) {
3170 case CT_APPLICATION
:
3171 return "application";
3187 return "unknown_type";
3193 ct_subtype_str (int type
, int subtype
) {
3195 case CT_APPLICATION
:
3197 case APPLICATION_OCTETS
:
3199 case APPLICATION_POSTSCRIPT
:
3200 return "postscript";
3202 return "unknown_app_subtype";
3206 case MESSAGE_RFC822
:
3208 case MESSAGE_PARTIAL
:
3210 case MESSAGE_EXTERNAL
:
3213 return "unknown_msg_subtype";
3219 case MULTI_ALTERNATE
:
3220 return "alternative";
3223 case MULTI_PARALLEL
:
3226 return "unknown_multipart_subtype";
3237 return "unknown_text_subtype";
3240 return "unknown_type";
3245 /* Find the content type and InitFunc for the CT. */
3246 const struct str2init
*
3247 get_ct_init (int type
) {
3248 const struct str2init
*sp
;
3250 for (sp
= str2cts
; sp
->si_key
; ++sp
) {
3251 if (type
== sp
->si_val
) {
3260 ce_str (int encoding
) {
3265 return "quoted-printable";
3281 /* Find the content type and InitFunc for the content encoding method. */
3282 const struct str2init
*
3283 get_ce_method (const char *method
) {
3284 struct str2init
*sp
;
3286 for (sp
= str2ces
; sp
->si_key
; ++sp
) {
3287 if (! strcasecmp (method
, sp
->si_key
)) {
3296 parse_header_attrs (const char *filename
, const char *fieldname
,
3297 char **header_attrp
, PM
*param_head
, PM
*param_tail
,
3300 char *cp
= *header_attrp
;
3303 while (*cp
== ';') {
3304 char *dp
, *vp
, *up
, c
;
3307 while (isspace ((unsigned char) *cp
))
3311 get_comment (filename
, fieldname
, &cp
, commentp
) == NOTOK
) {
3317 "extraneous trailing ';' in message %s's %s: "
3319 filename
, fieldname
);
3323 /* down case the attribute name */
3324 for (dp
= cp
; istoken ((unsigned char) *dp
); dp
++)
3325 if (isalpha((unsigned char) *dp
) && isupper ((unsigned char) *dp
))
3326 *dp
= tolower ((unsigned char) *dp
);
3328 for (up
= dp
; isspace ((unsigned char) *dp
);)
3330 if (dp
== cp
|| *dp
!= '=') {
3332 "invalid parameter in message %s's %s: "
3333 "field\n%*sparameter %s (error detected at offset %d)",
3334 filename
, fieldname
, strlen(invo_name
) + 2, "",cp
, dp
- cp
);
3338 pm
= mh_xmalloc(sizeof(*pm
));
3339 memset(pm
, 0, sizeof(*pm
));
3341 /* This is all mega-bozo and needs cleanup */
3342 vp
= (pm
->pm_name
= add (cp
, NULL
)) + (up
- cp
);
3344 for (dp
++; isspace ((unsigned char) *dp
);)
3347 /* Now store the attribute value. */
3349 vp
= pm
->pm_name
+ (dp
- cp
);
3352 for (cp
= ++dp
, dp
= vp
;;) {
3353 switch (c
= *cp
++) {
3357 "invalid quoted-string in message %s's %s: "
3358 "field\n%*s(parameter %s)",
3359 filename
, fieldname
, strlen(invo_name
) + 2, "",
3365 if ((c
= *cp
++) == '\0')
3380 for (cp
= dp
, dp
= vp
; istoken (*cp
); cp
++, dp
++)
3384 pm
->pm_value
= getcpy(vp
);
3387 "invalid parameter in message %s's %s: "
3388 "field\n%*s(parameter %s)",
3389 filename
, fieldname
, strlen(invo_name
) + 2, "",
3394 while (isspace ((unsigned char) *cp
))
3398 get_comment (filename
, fieldname
, &cp
, commentp
) == NOTOK
) {
3402 if (*param_head
== NULL
) {
3406 (*param_tail
)->pm_next
= pm
;
3416 * Create a string based on a list of output parameters. Assume that this
3417 * parameter string will be appended to an existing header, so start out
3418 * with the separator (;). Perform RFC 2231 encoding when necessary.
3422 output_params(size_t initialwidth
, PM params
, int *offsetout
)
3424 char *paramout
= NULL
;
3425 char line
[CPERLIN
* 2], *q
;
3426 int curlen
, index
, eightbit
, encode
, i
;
3429 while (params
!= NULL
) {
3435 if (strlen(params
->pm_name
) > CPERLIN
) {
3436 advise(NULL
, "Parameter name \"%s\" is too long", params
->pm_name
);
3442 curlen
= param_len(params
, index
, valoff
, &eightbit
);
3445 * Loop until we get a parameter that fits within a line. We
3446 * assume new lines start with a tab, so check our overflow based
3450 while (curlen
+ 8 > CPERLIN
- 1) {
3451 int curvallen
= strlen(params
->pm_value
+ valoff
) -
3452 (initialwidth
+ curlen
- (CPERLIN
- 1));
3459 * curvallen holds how many characters we take from this
3460 * current value. Make sure it's at least 1.
3467 * At this point we're definitely continuing the line, so
3468 * be sure to include the parameter name and section index.
3471 q
+= snprintf(q
, sizeof(line
) - (q
- line
), "%s*%d",
3472 params
->pm_name
, index
);
3475 * If eightbit was set and we're on index 0, we need to include
3476 * the character set and encode the first section. Otherwise
3477 * only encode if the section we're on contains an 8bit character.
3480 if (eightbit
&& index
== 0)
3482 else if (eightbit
&& contains8bit(params
->pm_value
+ valoff
,
3483 params
->pm_value
+ valoff
+ curvallen
))
3487 * Both of these functions do a NUL termination
3491 i
= encode_param(params
, q
, sizeof(line
) - (q
- line
),
3492 curvallen
, valoff
, index
);
3494 i
= normal_param(params
, q
, sizeof(line
) - (q
- line
),
3503 valoff
+= curvallen
;
3505 curlen
= param_len(params
, index
, valoff
, &eightbit
);
3509 * "line" starts with a ;\n\t, so that doesn't count against
3510 * the length. But add 8 since it starts with a tab; that's
3511 * how we end up with 5.
3514 initialwidth
= strlen(line
) + 5;
3517 * At this point the line should be built, so add it to our
3518 * current output buffer.
3521 paramout
= add(line
, paramout
);
3525 * If this won't fit on the line, start a new one. Save room in
3526 * case we need a semicolon on the end
3529 if (initialwidth
+ curlen
> CPERLIN
- 1) {
3541 * At this point, we're either finishing a contined parameter, or
3542 * we're working on a new one.
3546 q
+= snprintf(q
, sizeof(line
) - (q
- line
), "%s*%d",
3547 params
->pm_name
, index
);
3549 q
= strncpy(q
, params
->pm_name
, sizeof(line
) - (q
- line
));
3553 i
= encode_param(params
, q
, sizeof(line
) - (q
- line
),
3554 strlen(params
->pm_value
+ valoff
), valoff
, index
);
3556 i
= normal_param(params
, q
, sizeof(line
) - (q
- line
),
3557 strlen(params
->pm_value
+ valoff
), valoff
);
3565 paramout
= add(line
, paramout
);
3566 initialwidth
+= strlen(line
);
3568 params
= params
->pm_next
;
3572 *offsetout
= initialwidth
;
3578 * Calculate the size of a parameter. Include any necessary encoding.
3579 * Start the length computation from where "offset" is marked.
3583 param_len(PM pm
, int index
, size_t valueoff
, int *eightbit
)
3585 char *start
= pm
->pm_value
+ valueoff
, *p
;
3589 * Add up the length. First, start with the parameter name, and include
3593 len
+= strlen(pm
->pm_name
) + 1;
3596 * Scan the parameter value. If we find an 8-bit character, then
3597 * we need to compute the locale name for the length.
3600 *eightbit
= contains8bit(start
, NULL
);
3603 * If we've got 8-bit character, put the locale on the front (if we're
3604 * doing part 0. Also compute the length of the string based on the
3605 * encoding we need to do.
3610 * If we don't have a charset or language tag in this parameter,
3614 if (! pm
->pm_charset
)
3615 pm
->pm_charset
= getcpy(write_charset_8bit());
3617 pm
->pm_lang
= getcpy(NULL
); /* Default to a blank lang tag */
3619 len
++; /* For the encoding we need to do */
3621 len
+= strlen(pm
->pm_charset
) + strlen(pm
->pm_lang
) + 2;
3624 * We know we definitely need to include an index.
3625 * This will get the length wrong if we have more than 99
3626 * sections. I can live with that.
3628 len
+= 2; /* *<N> */
3632 for (p
= start
; *p
!= '\0'; p
++) {
3633 if (isparamencode(*p
))
3640 * Calculate the string length, but add room for quoting \
3641 * and " if necessary. Also account for quotes at beginning
3644 for (p
= start
; *p
!= '\0'; p
++) {
3662 * Output an encoded parameter string.
3666 encode_param(PM pm
, char *output
, size_t len
, size_t valuelen
,
3667 size_t valueoff
, int index
)
3669 size_t outlen
= 0, n
;
3670 char *endptr
= output
+ len
, *p
;
3673 * First, output the marker for an encoded string.
3681 * If the index is 0, output the character set and language tag.
3682 * If theses were NULL, they should have already been filled in
3687 n
= snprintf(output
, len
- outlen
, "%s'%s'", pm
->pm_charset
,
3691 if (output
> endptr
) {
3692 advise(NULL
, "Internal error: parameter buffer overflow");
3698 * Copy over the value, encoding if necessary
3701 p
= pm
->pm_value
+ valueoff
;
3702 while (valuelen
-- > 0) {
3703 if (isparamencode(*p
)) {
3704 n
= snprintf(output
, len
- outlen
, "%%%02X", (unsigned char) *p
++);
3711 if (output
> endptr
) {
3712 advise(NULL
, "Internal error: parameter buffer overflow");
3723 * Output a "normal" parameter, without encoding. Be sure to escape
3724 * quotes and backslashes if necessary.
3728 normal_param(PM pm
, char *output
, size_t len
, size_t valuelen
,
3732 char *endptr
= output
+ len
, *p
;
3738 p
= pm
->pm_value
+ valueoff
;
3740 while (valuelen
-- > 0) {
3750 if (output
> endptr
) {
3751 advise(NULL
, "Internal error: parameter buffer overflow");
3756 if (output
- 2 > endptr
) {
3757 advise(NULL
, "Internal error: parameter buffer overflow");
3768 * Add a parameter to the parameter linked list
3772 add_param(PM
*first
, PM
*last
, const char *name
, const char *value
)
3774 PM pm
= mh_xmalloc(sizeof(*pm
));
3776 memset(pm
, 0, sizeof(*pm
));
3778 pm
->pm_name
= getcpy(name
);
3779 pm
->pm_value
= getcpy(value
);
3782 (*last
)->pm_next
= pm
;