]>
diplodocus.org Git - nmh/blob - uip/mhparse.c
ad499d019bfef80b7f919fdb60412a39b2cd0069
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>
19 #include <h/mhparse.h>
25 extern pid_t xpid
; /* mhshowsbr.c */
28 extern int rcachesw
; /* mhcachesbr.c */
29 extern int wcachesw
; /* mhcachesbr.c */
31 int checksw
= 0; /* check Content-MD5 field */
34 * Directory to place temp files. This must
35 * be set before these routines are called.
40 * Structures for TEXT messages
42 struct k2v SubText
[] = {
43 { "plain", TEXT_PLAIN
},
44 { "richtext", TEXT_RICHTEXT
}, /* defined in RFC-1341 */
45 { "enriched", TEXT_ENRICHED
}, /* defined in RFC-1896 */
46 { NULL
, TEXT_UNKNOWN
} /* this one must be last! */
49 /* Charset[] removed -- yozo. Mon Oct 8 01:03:41 JST 2012 */
52 * Structures for MULTIPART messages
54 struct k2v SubMultiPart
[] = {
55 { "mixed", MULTI_MIXED
},
56 { "alternative", MULTI_ALTERNATE
},
57 { "digest", MULTI_DIGEST
},
58 { "parallel", MULTI_PARALLEL
},
59 { NULL
, MULTI_UNKNOWN
} /* this one must be last! */
63 * Structures for MESSAGE messages
65 struct k2v SubMessage
[] = {
66 { "rfc822", MESSAGE_RFC822
},
67 { "partial", MESSAGE_PARTIAL
},
68 { "external-body", MESSAGE_EXTERNAL
},
69 { NULL
, MESSAGE_UNKNOWN
} /* this one must be last! */
73 * Structure for APPLICATION messages
75 struct k2v SubApplication
[] = {
76 { "octet-stream", APPLICATION_OCTETS
},
77 { "postscript", APPLICATION_POSTSCRIPT
},
78 { NULL
, APPLICATION_UNKNOWN
} /* this one must be last! */
83 int find_cache (CT
, int, int *, char *, char *, int);
86 int part_ok (CT
, int);
87 int type_ok (CT
, int);
88 void content_error (char *, CT
, char *, ...);
91 void free_content (CT
);
92 void free_encoding (CT
, int);
97 static CT
get_content (FILE *, char *, int);
98 static int get_comment (CT
, unsigned char **, int);
100 static int InitGeneric (CT
);
101 static int InitText (CT
);
102 static int InitMultiPart (CT
);
103 static void reverse_parts (CT
);
104 static int InitMessage (CT
);
105 static int InitApplication (CT
);
106 static int init_encoding (CT
, OpenCEFunc
);
107 static unsigned long size_encoding (CT
);
108 static int InitBase64 (CT
);
109 static int openBase64 (CT
, char **);
110 static int InitQuoted (CT
);
111 static int openQuoted (CT
, char **);
112 static int Init7Bit (CT
);
113 static int openExternal (CT
, CT
, CE
, char **, int *);
114 static int InitFile (CT
);
115 static int openFile (CT
, char **);
116 static int InitFTP (CT
);
117 static int openFTP (CT
, char **);
118 static int InitMail (CT
);
119 static int openMail (CT
, char **);
120 static int readDigest (CT
, char *);
122 struct str2init str2cts
[] = {
123 { "application", CT_APPLICATION
, InitApplication
},
124 { "audio", CT_AUDIO
, InitGeneric
},
125 { "image", CT_IMAGE
, InitGeneric
},
126 { "message", CT_MESSAGE
, InitMessage
},
127 { "multipart", CT_MULTIPART
, InitMultiPart
},
128 { "text", CT_TEXT
, InitText
},
129 { "video", CT_VIDEO
, InitGeneric
},
130 { NULL
, CT_EXTENSION
, NULL
}, /* these two must be last! */
131 { NULL
, CT_UNKNOWN
, NULL
},
134 struct str2init str2ces
[] = {
135 { "base64", CE_BASE64
, InitBase64
},
136 { "quoted-printable", CE_QUOTED
, InitQuoted
},
137 { "8bit", CE_8BIT
, Init7Bit
},
138 { "7bit", CE_7BIT
, Init7Bit
},
139 { "binary", CE_BINARY
, Init7Bit
},
140 { NULL
, CE_EXTENSION
, NULL
}, /* these two must be last! */
141 { NULL
, CE_UNKNOWN
, NULL
},
145 * NOTE WELL: si_key MUST NOT have value of NOTOK
147 * si_key is 1 if access method is anonymous.
149 struct str2init str2methods
[] = {
150 { "afs", 1, InitFile
},
151 { "anon-ftp", 1, InitFTP
},
152 { "ftp", 0, InitFTP
},
153 { "local-file", 0, InitFile
},
154 { "mail-server", 0, InitMail
},
160 pidcheck (int status
)
162 if ((status
& 0xff00) == 0xff00 || (status
& 0x007f) != SIGQUIT
)
173 * Main entry point for parsing a MIME message or file.
174 * It returns the Content structure for the top level
175 * entity in the file.
179 parse_mime (char *file
)
187 * Check if file is actually standard input
189 if ((is_stdin
= !(strcmp (file
, "-")))) {
190 char *tfile
= m_mktemp2(NULL
, invo_name
, NULL
, &fp
);
192 advise("mhparse", "unable to create temporary file");
195 file
= add (tfile
, NULL
);
198 while (fgets (buffer
, sizeof(buffer
), stdin
))
202 if (ferror (stdin
)) {
204 advise ("stdin", "error reading");
209 advise (file
, "error writing");
212 fseek (fp
, 0L, SEEK_SET
);
213 } else if ((fp
= fopen (file
, "r")) == NULL
) {
214 advise (file
, "unable to read");
218 if (!(ct
= get_content (fp
, file
, 1))) {
221 advise (NULL
, "unable to decode %s", file
);
226 ct
->c_unlink
= 1; /* temp file to remove */
230 if (ct
->c_end
== 0L) {
231 fseek (fp
, 0L, SEEK_END
);
232 ct
->c_end
= ftell (fp
);
235 if (ct
->c_ctinitfnx
&& (*ct
->c_ctinitfnx
) (ct
) == NOTOK
) {
247 * Main routine for reading/parsing the headers
248 * of a message content.
250 * toplevel = 1 # we are at the top level of the message
251 * toplevel = 0 # we are inside message type or multipart type
252 * # other than multipart/digest
253 * toplevel = -1 # we are inside multipart/digest
254 * NB: on failure we will fclose(in)!
258 get_content (FILE *in
, char *file
, int toplevel
)
261 char buf
[BUFSIZ
], name
[NAMESZ
];
266 /* allocate the content structure */
267 if (!(ct
= (CT
) calloc (1, sizeof(*ct
))))
268 adios (NULL
, "out of memory");
271 ct
->c_file
= add (file
, NULL
);
272 ct
->c_begin
= ftell (ct
->c_fp
) + 1;
275 * Parse the header fields for this
276 * content into a linked list.
278 for (compnum
= 1, state
= FLD
;;) {
279 int bufsz
= sizeof buf
;
280 switch (state
= m_getfld (state
, name
, buf
, &bufsz
, in
)) {
285 /* get copies of the buffers */
286 np
= add (name
, NULL
);
287 vp
= add (buf
, NULL
);
289 /* if necessary, get rest of field */
290 while (state
== FLDPLUS
) {
292 state
= m_getfld (state
, name
, buf
, &bufsz
, in
);
293 vp
= add (buf
, vp
); /* add to previous value */
296 /* Now add the header data to the list */
297 add_header (ct
, np
, vp
);
299 /* continue, to see if this isn't the last header field */
300 ct
->c_begin
= ftell (in
) + 1;
304 ct
->c_begin
= ftell (in
) - strlen (buf
);
308 ct
->c_begin
= ftell (in
);
313 adios (NULL
, "message format error in component #%d", compnum
);
316 adios (NULL
, "getfld() returned %d", state
);
319 /* break out of the loop */
324 * Read the content headers. We will parse the
325 * MIME related header fields into their various
326 * structures and set internal flags related to
327 * content type/subtype, etc.
330 hp
= ct
->c_first_hf
; /* start at first header field */
332 /* Get MIME-Version field */
333 if (!mh_strcasecmp (hp
->name
, VRSN_FIELD
)) {
336 unsigned char *cp
, *dp
;
339 advise (NULL
, "message %s has multiple %s: fields",
340 ct
->c_file
, VRSN_FIELD
);
343 ct
->c_vrsn
= add (hp
->value
, NULL
);
345 /* Now, cleanup this field */
348 while (isspace (*cp
))
350 for (dp
= strchr(cp
, '\n'); dp
; dp
= strchr(dp
, '\n'))
352 for (dp
= cp
+ strlen (cp
) - 1; dp
>= cp
; dp
--)
357 fprintf (stderr
, "%s: %s\n", VRSN_FIELD
, cp
);
359 if (*cp
== '(' && get_comment (ct
, &cp
, 0) == NOTOK
)
362 for (dp
= cp
; istoken (*dp
); dp
++)
366 ucmp
= !mh_strcasecmp (cp
, VRSN_VALUE
);
369 admonish (NULL
, "message %s has unknown value for %s: field (%s)",
370 ct
->c_file
, VRSN_FIELD
, cp
);
373 else if (!mh_strcasecmp (hp
->name
, TYPE_FIELD
)) {
374 /* Get Content-Type field */
375 struct str2init
*s2i
;
376 CI ci
= &ct
->c_ctinfo
;
378 /* Check if we've already seen a Content-Type header */
380 advise (NULL
, "message %s has multiple %s: fields",
381 ct
->c_file
, TYPE_FIELD
);
385 /* Parse the Content-Type field */
386 if (get_ctinfo (hp
->value
, ct
, 0) == NOTOK
)
390 * Set the Init function and the internal
391 * flag for this content type.
393 for (s2i
= str2cts
; s2i
->si_key
; s2i
++)
394 if (!mh_strcasecmp (ci
->ci_type
, s2i
->si_key
))
396 if (!s2i
->si_key
&& !uprf (ci
->ci_type
, "X-"))
398 ct
->c_type
= s2i
->si_val
;
399 ct
->c_ctinitfnx
= s2i
->si_init
;
401 else if (!mh_strcasecmp (hp
->name
, ENCODING_FIELD
)) {
402 /* Get Content-Transfer-Encoding field */
404 unsigned char *cp
, *dp
;
405 struct str2init
*s2i
;
408 * Check if we've already seen the
409 * Content-Transfer-Encoding field
412 advise (NULL
, "message %s has multiple %s: fields",
413 ct
->c_file
, ENCODING_FIELD
);
417 /* get copy of this field */
418 ct
->c_celine
= cp
= add (hp
->value
, NULL
);
420 while (isspace (*cp
))
422 for (dp
= cp
; istoken (*dp
); dp
++)
428 * Find the internal flag and Init function
429 * for this transfer encoding.
431 for (s2i
= str2ces
; s2i
->si_key
; s2i
++)
432 if (!mh_strcasecmp (cp
, s2i
->si_key
))
434 if (!s2i
->si_key
&& !uprf (cp
, "X-"))
437 ct
->c_encoding
= s2i
->si_val
;
439 /* Call the Init function for this encoding */
440 if (s2i
->si_init
&& (*s2i
->si_init
) (ct
) == NOTOK
)
443 else if (!mh_strcasecmp (hp
->name
, MD5_FIELD
)) {
444 /* Get Content-MD5 field */
445 unsigned char *cp
, *dp
;
451 if (ct
->c_digested
) {
452 advise (NULL
, "message %s has multiple %s: fields",
453 ct
->c_file
, MD5_FIELD
);
457 ep
= cp
= add (hp
->value
, NULL
); /* get a copy */
459 while (isspace (*cp
))
461 for (dp
= strchr(cp
, '\n'); dp
; dp
= strchr(dp
, '\n'))
463 for (dp
= cp
+ strlen (cp
) - 1; dp
>= cp
; dp
--)
468 fprintf (stderr
, "%s: %s\n", MD5_FIELD
, cp
);
470 if (*cp
== '(' && get_comment (ct
, &cp
, 0) == NOTOK
) {
475 for (dp
= cp
; *dp
&& !isspace (*dp
); dp
++)
483 else if (!mh_strcasecmp (hp
->name
, ID_FIELD
)) {
484 /* Get Content-ID field */
485 ct
->c_id
= add (hp
->value
, ct
->c_id
);
487 else if (!mh_strcasecmp (hp
->name
, DESCR_FIELD
)) {
488 /* Get Content-Description field */
489 ct
->c_descr
= add (hp
->value
, ct
->c_descr
);
491 else if (!mh_strcasecmp (hp
->name
, DISPO_FIELD
)) {
492 /* Get Content-Disposition field */
493 ct
->c_dispo
= add (hp
->value
, ct
->c_dispo
);
497 hp
= hp
->next
; /* next header field */
501 * Check if we saw a Content-Type field.
502 * If not, then assign a default value for
503 * it, and the Init function.
507 * If we are inside a multipart/digest message,
508 * so default type is message/rfc822
511 if (get_ctinfo ("message/rfc822", ct
, 0) == NOTOK
)
513 ct
->c_type
= CT_MESSAGE
;
514 ct
->c_ctinitfnx
= InitMessage
;
517 * Else default type is text/plain
519 if (get_ctinfo ("text/plain", ct
, 0) == NOTOK
)
521 ct
->c_type
= CT_TEXT
;
522 ct
->c_ctinitfnx
= InitText
;
526 /* Use default Transfer-Encoding, if necessary */
528 ct
->c_encoding
= CE_7BIT
;
541 * small routine to add header field to list
545 add_header (CT ct
, char *name
, char *value
)
549 /* allocate header field structure */
550 hp
= mh_xmalloc (sizeof(*hp
));
552 /* link data into header structure */
557 /* link header structure into the list */
558 if (ct
->c_first_hf
== NULL
) {
559 ct
->c_first_hf
= hp
; /* this is the first */
562 ct
->c_last_hf
->next
= hp
; /* add it to the end */
570 /* Make sure that buf contains at least one appearance of name,
571 followed by =. If not, insert both name and value, just after
572 first semicolon, if any. Note that name should not contain a
573 trailing =. And quotes will be added around the value. Typical
574 usage: make sure that a Content-Disposition header contains
575 filename="foo". If it doesn't and value does, use value from
578 incl_name_value (unsigned char *buf
, char *name
, char *value
) {
581 /* Assume that name is non-null. */
583 char *name_plus_equal
= concat (name
, "=", NULL
);
585 if (! strstr (buf
, name_plus_equal
)) {
588 char *prefix
, *suffix
;
590 /* Trim trailing space, esp. newline. */
591 for (cp
= &buf
[strlen (buf
) - 1];
592 cp
>= buf
&& isspace (*cp
);
597 insertion
= concat ("; ", name
, "=", "\"", value
, "\"", NULL
);
599 /* Insert at first semicolon, if any. If none, append to
601 prefix
= add (buf
, NULL
);
602 if ((cp
= strchr (prefix
, ';'))) {
603 suffix
= concat (cp
, NULL
);
605 newbuf
= concat (prefix
, insertion
, suffix
, "\n", NULL
);
609 newbuf
= concat (buf
, insertion
, "\n", NULL
);
617 free (name_plus_equal
);
623 /* Extract just name_suffix="foo", if any, from value. If there isn't
624 one, return the entire value. Note that, for example, a name_suffix
625 of name will match filename="foo", and return foo. */
627 extract_name_value (char *name_suffix
, char *value
) {
628 char *extracted_name_value
= value
;
629 char *name_suffix_plus_quote
= concat (name_suffix
, "=\"", NULL
);
630 char *name_suffix_equals
= strstr (value
, name_suffix_plus_quote
);
633 free (name_suffix_plus_quote
);
634 if (name_suffix_equals
) {
635 char *name_suffix_begin
;
638 for (cp
= name_suffix_equals
; *cp
!= '"'; ++cp
) /* empty */;
639 name_suffix_begin
= ++cp
;
640 /* Find second \". */
641 for (; *cp
!= '"'; ++cp
) /* empty */;
643 extracted_name_value
= mh_xmalloc (cp
- name_suffix_begin
+ 1);
644 memcpy (extracted_name_value
,
646 cp
- name_suffix_begin
);
647 extracted_name_value
[cp
- name_suffix_begin
] = '\0';
650 return extracted_name_value
;
654 * Parse Content-Type line and (if `magic' is non-zero) mhbuild composition
655 * directives. Fills in the information of the CTinfo structure.
658 get_ctinfo (unsigned char *cp
, CT ct
, int magic
)
667 i
= strlen (invo_name
) + 2;
669 /* store copy of Content-Type line */
670 cp
= ct
->c_ctline
= add (cp
, NULL
);
672 while (isspace (*cp
)) /* trim leading spaces */
675 /* change newlines to spaces */
676 for (dp
= strchr(cp
, '\n'); dp
; dp
= strchr(dp
, '\n'))
679 /* trim trailing spaces */
680 for (dp
= cp
+ strlen (cp
) - 1; dp
>= cp
; dp
--)
686 fprintf (stderr
, "%s: %s\n", TYPE_FIELD
, cp
);
688 if (*cp
== '(' && get_comment (ct
, &cp
, 1) == NOTOK
)
691 for (dp
= cp
; istoken (*dp
); dp
++)
694 ci
->ci_type
= add (cp
, NULL
); /* store content type */
698 advise (NULL
, "invalid %s: field in message %s (empty type)",
699 TYPE_FIELD
, ct
->c_file
);
703 /* down case the content type string */
704 for (dp
= ci
->ci_type
; *dp
; dp
++)
705 if (isalpha(*dp
) && isupper (*dp
))
708 while (isspace (*cp
))
711 if (*cp
== '(' && get_comment (ct
, &cp
, 1) == NOTOK
)
716 ci
->ci_subtype
= add ("", NULL
);
721 while (isspace (*cp
))
724 if (*cp
== '(' && get_comment (ct
, &cp
, 1) == NOTOK
)
727 for (dp
= cp
; istoken (*dp
); dp
++)
730 ci
->ci_subtype
= add (cp
, NULL
); /* store the content subtype */
733 if (!*ci
->ci_subtype
) {
735 "invalid %s: field in message %s (empty subtype for \"%s\")",
736 TYPE_FIELD
, ct
->c_file
, ci
->ci_type
);
740 /* down case the content subtype string */
741 for (dp
= ci
->ci_subtype
; *dp
; dp
++)
742 if (isalpha(*dp
) && isupper (*dp
))
746 while (isspace (*cp
))
749 if (*cp
== '(' && get_comment (ct
, &cp
, 1) == NOTOK
)
753 * Parse attribute/value pairs given with Content-Type
755 ep
= (ap
= ci
->ci_attrs
) + NPARMS
;
762 "too many parameters in message %s's %s: field (%d max)",
763 ct
->c_file
, TYPE_FIELD
, NPARMS
);
768 while (isspace (*cp
))
771 if (*cp
== '(' && get_comment (ct
, &cp
, 1) == NOTOK
)
776 "extraneous trailing ';' in message %s's %s: parameter list",
777 ct
->c_file
, TYPE_FIELD
);
781 /* down case the attribute name */
782 for (dp
= cp
; istoken (*dp
); dp
++)
783 if (isalpha(*dp
) && isupper (*dp
))
786 for (up
= dp
; isspace (*dp
);)
788 if (dp
== cp
|| *dp
!= '=') {
790 "invalid parameter in message %s's %s: field\n%*.*sparameter %s (error detected at offset %d)",
791 ct
->c_file
, TYPE_FIELD
, i
, i
, "", cp
, dp
- cp
);
795 vp
= (*ap
= add (cp
, NULL
)) + (up
- cp
);
797 for (dp
++; isspace (*dp
);)
800 /* now add the attribute value */
801 ci
->ci_values
[ap
- ci
->ci_attrs
] = vp
= *ap
+ (dp
- cp
);
804 for (cp
= ++dp
, dp
= vp
;;) {
809 "invalid quoted-string in message %s's %s: field\n%*.*s(parameter %s)",
810 ct
->c_file
, TYPE_FIELD
, i
, i
, "", *ap
);
815 if ((c
= *cp
++) == '\0')
830 for (cp
= dp
, dp
= vp
; istoken (*cp
); cp
++, dp
++)
836 "invalid parameter in message %s's %s: field\n%*.*s(parameter %s)",
837 ct
->c_file
, TYPE_FIELD
, i
, i
, "", *ap
);
842 while (isspace (*cp
))
845 if (*cp
== '(' && get_comment (ct
, &cp
, 1) == NOTOK
)
850 * Get any <Content-Id> given in buffer
852 if (magic
&& *cp
== '<') {
857 if (!(dp
= strchr(ct
->c_id
= ++cp
, '>'))) {
858 advise (NULL
, "invalid ID in message %s", ct
->c_file
);
864 ct
->c_id
= concat ("<", ct
->c_id
, ">\n", NULL
);
870 while (isspace (*cp
))
875 * Get any [Content-Description] given in buffer.
877 if (magic
&& *cp
== '[') {
879 for (dp
= cp
+ strlen (cp
) - 1; dp
>= cp
; dp
--)
883 advise (NULL
, "invalid description in message %s", ct
->c_file
);
891 ct
->c_descr
= concat (ct
->c_descr
, "\n", NULL
);
897 while (isspace (*cp
))
902 * Get any {Content-Disposition} given in buffer.
904 if (magic
&& *cp
== '{') {
906 for (dp
= cp
+ strlen (cp
) - 1; dp
>= cp
; dp
--)
910 advise (NULL
, "invalid disposition in message %s", ct
->c_file
);
918 ct
->c_dispo
= concat (ct
->c_dispo
, "\n", NULL
);
924 while (isspace (*cp
))
929 * Check if anything is left over
933 ci
->ci_magic
= add (cp
, NULL
);
935 /* If there is a Content-Disposition header and it doesn't
936 have a *filename=, extract it from the magic contents.
937 The r1bindex call skips any leading directory
941 incl_name_value (ct
->c_dispo
,
943 r1bindex (extract_name_value ("name",
950 "extraneous information in message %s's %s: field\n%*.*s(%s)",
951 ct
->c_file
, TYPE_FIELD
, i
, i
, "", cp
);
959 get_comment (CT ct
, unsigned char **ap
, int istype
)
964 char c
, buffer
[BUFSIZ
], *dp
;
976 advise (NULL
, "invalid comment in message %s's %s: field",
977 ct
->c_file
, istype
? TYPE_FIELD
: VRSN_FIELD
);
982 if ((c
= *cp
++) == '\0')
1005 if ((dp
= ci
->ci_comment
)) {
1006 ci
->ci_comment
= concat (dp
, " ", buffer
, NULL
);
1009 ci
->ci_comment
= add (buffer
, NULL
);
1013 while (isspace (*cp
))
1024 * Handles content types audio, image, and video.
1025 * There's not much to do right here.
1033 return OK
; /* not much to do here */
1044 char buffer
[BUFSIZ
];
1046 char **ap
, **ep
, *cp
;
1049 CI ci
= &ct
->c_ctinfo
;
1051 /* check for missing subtype */
1052 if (!*ci
->ci_subtype
)
1053 ci
->ci_subtype
= add ("plain", ci
->ci_subtype
);
1056 for (kv
= SubText
; kv
->kv_key
; kv
++)
1057 if (!mh_strcasecmp (ci
->ci_subtype
, kv
->kv_key
))
1059 ct
->c_subtype
= kv
->kv_value
;
1061 /* allocate text character set structure */
1062 if ((t
= (struct text
*) calloc (1, sizeof(*t
))) == NULL
)
1063 adios (NULL
, "out of memory");
1064 ct
->c_ctparams
= (void *) t
;
1066 /* scan for charset parameter */
1067 for (ap
= ci
->ci_attrs
, ep
= ci
->ci_values
; *ap
; ap
++, ep
++)
1068 if (!mh_strcasecmp (*ap
, "charset"))
1071 /* check if content specified a character set */
1074 t
->tx_charset
= CHARSET_SPECIFIED
;
1076 t
->tx_charset
= CHARSET_UNSPECIFIED
;
1080 * If we can not handle character set natively,
1081 * then check profile for string to modify the
1082 * terminal or display method.
1084 * termproc is for mhshow, though mhlist -debug prints it, too.
1086 if (chset
!= NULL
&& !check_charset (chset
, strlen (chset
))) {
1087 snprintf (buffer
, sizeof(buffer
), "%s-charset-%s", invo_name
, chset
);
1088 if ((cp
= context_find (buffer
)))
1089 ct
->c_termproc
= getcpy (cp
);
1101 InitMultiPart (CT ct
)
1105 unsigned char *cp
, *dp
;
1107 char *bp
, buffer
[BUFSIZ
];
1108 struct multipart
*m
;
1110 struct part
*part
, **next
;
1111 CI ci
= &ct
->c_ctinfo
;
1116 * The encoding for multipart messages must be either
1117 * 7bit, 8bit, or binary (per RFC2045).
1119 if (ct
->c_encoding
!= CE_7BIT
&& ct
->c_encoding
!= CE_8BIT
1120 && ct
->c_encoding
!= CE_BINARY
) {
1122 "\"%s/%s\" type in message %s must be encoded in 7bit, 8bit, or binary",
1123 ci
->ci_type
, ci
->ci_subtype
, ct
->c_file
);
1128 for (kv
= SubMultiPart
; kv
->kv_key
; kv
++)
1129 if (!mh_strcasecmp (ci
->ci_subtype
, kv
->kv_key
))
1131 ct
->c_subtype
= kv
->kv_value
;
1134 * Check for "boundary" parameter, which is
1135 * required for multipart messages.
1138 for (ap
= ci
->ci_attrs
, ep
= ci
->ci_values
; *ap
; ap
++, ep
++) {
1139 if (!mh_strcasecmp (*ap
, "boundary")) {
1145 /* complain if boundary parameter is missing */
1148 "a \"boundary\" parameter is mandatory for \"%s/%s\" type in message %s's %s: field",
1149 ci
->ci_type
, ci
->ci_subtype
, ct
->c_file
, TYPE_FIELD
);
1153 /* allocate primary structure for multipart info */
1154 if ((m
= (struct multipart
*) calloc (1, sizeof(*m
))) == NULL
)
1155 adios (NULL
, "out of memory");
1156 ct
->c_ctparams
= (void *) m
;
1158 /* check if boundary parameter contains only whitespace characters */
1159 for (cp
= bp
; isspace (*cp
); cp
++)
1162 advise (NULL
, "invalid \"boundary\" parameter for \"%s/%s\" type in message %s's %s: field",
1163 ci
->ci_type
, ci
->ci_subtype
, ct
->c_file
, TYPE_FIELD
);
1167 /* remove trailing whitespace from boundary parameter */
1168 for (cp
= bp
, dp
= cp
+ strlen (cp
) - 1; dp
> cp
; dp
--)
1173 /* record boundary separators */
1174 m
->mp_start
= concat (bp
, "\n", NULL
);
1175 m
->mp_stop
= concat (bp
, "--\n", NULL
);
1177 if (!ct
->c_fp
&& (ct
->c_fp
= fopen (ct
->c_file
, "r")) == NULL
) {
1178 advise (ct
->c_file
, "unable to open for reading");
1182 fseek (fp
= ct
->c_fp
, pos
= ct
->c_begin
, SEEK_SET
);
1184 next
= &m
->mp_parts
;
1188 while (fgets (buffer
, sizeof(buffer
) - 1, fp
)) {
1192 pos
+= strlen (buffer
);
1193 if (buffer
[0] != '-' || buffer
[1] != '-')
1196 if (strcmp (buffer
+ 2, m
->mp_start
))
1199 if ((part
= (struct part
*) calloc (1, sizeof(*part
))) == NULL
)
1200 adios (NULL
, "out of memory");
1202 next
= &part
->mp_next
;
1204 if (!(p
= get_content (fp
, ct
->c_file
,
1205 ct
->c_subtype
== MULTI_DIGEST
? -1 : 0))) {
1212 fseek (fp
, pos
, SEEK_SET
);
1215 if (strcmp (buffer
+ 2, m
->mp_start
) == 0) {
1219 p
->c_end
= ftell(fp
) - (strlen(buffer
) + 1);
1220 if (p
->c_end
< p
->c_begin
)
1221 p
->c_begin
= p
->c_end
;
1226 if (strcmp (buffer
+ 2, m
->mp_stop
) == 0)
1232 advise (NULL
, "bogus multipart content in message %s", ct
->c_file
);
1233 if (!inout
&& part
) {
1235 p
->c_end
= ct
->c_end
;
1237 if (p
->c_begin
>= p
->c_end
) {
1238 for (next
= &m
->mp_parts
; *next
!= part
;
1239 next
= &((*next
)->mp_next
))
1243 free ((char *) part
);
1248 /* reverse the order of the parts for multipart/alternative */
1249 if (ct
->c_subtype
== MULTI_ALTERNATE
)
1253 * label all subparts with part number, and
1254 * then initialize the content of the subpart.
1259 char partnam
[BUFSIZ
];
1262 snprintf (partnam
, sizeof(partnam
), "%s.", ct
->c_partno
);
1263 pp
= partnam
+ strlen (partnam
);
1268 for (part
= m
->mp_parts
, partnum
= 1; part
;
1269 part
= part
->mp_next
, partnum
++) {
1272 sprintf (pp
, "%d", partnum
);
1273 p
->c_partno
= add (partnam
, NULL
);
1275 /* initialize the content of the subparts */
1276 if (p
->c_ctinitfnx
&& (*p
->c_ctinitfnx
) (p
) == NOTOK
) {
1291 * reverse the order of the parts of a multipart
1295 reverse_parts (CT ct
)
1298 struct multipart
*m
;
1299 struct part
**base
, **bmp
, **next
, *part
;
1301 m
= (struct multipart
*) ct
->c_ctparams
;
1303 /* if only one part, just return */
1304 if (!m
->mp_parts
|| !m
->mp_parts
->mp_next
)
1307 /* count number of parts */
1309 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
)
1312 /* allocate array of pointers to the parts */
1313 if (!(base
= (struct part
**) calloc ((size_t) (i
+ 1), sizeof(*base
))))
1314 adios (NULL
, "out of memory");
1317 /* point at all the parts */
1318 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
)
1322 /* reverse the order of the parts */
1323 next
= &m
->mp_parts
;
1324 for (bmp
--; bmp
>= base
; bmp
--) {
1327 next
= &part
->mp_next
;
1331 /* free array of pointers */
1332 free ((char *) base
);
1344 CI ci
= &ct
->c_ctinfo
;
1346 if ((ct
->c_encoding
!= CE_7BIT
) && (ct
->c_encoding
!= CE_8BIT
)) {
1348 "\"%s/%s\" type in message %s should be encoded in 7bit or 8bit",
1349 ci
->ci_type
, ci
->ci_subtype
, ct
->c_file
);
1353 /* check for missing subtype */
1354 if (!*ci
->ci_subtype
)
1355 ci
->ci_subtype
= add ("rfc822", ci
->ci_subtype
);
1358 for (kv
= SubMessage
; kv
->kv_key
; kv
++)
1359 if (!mh_strcasecmp (ci
->ci_subtype
, kv
->kv_key
))
1361 ct
->c_subtype
= kv
->kv_value
;
1363 switch (ct
->c_subtype
) {
1364 case MESSAGE_RFC822
:
1367 case MESSAGE_PARTIAL
:
1372 if ((p
= (struct partial
*) calloc (1, sizeof(*p
))) == NULL
)
1373 adios (NULL
, "out of memory");
1374 ct
->c_ctparams
= (void *) p
;
1376 /* scan for parameters "id", "number", and "total" */
1377 for (ap
= ci
->ci_attrs
, ep
= ci
->ci_values
; *ap
; ap
++, ep
++) {
1378 if (!mh_strcasecmp (*ap
, "id")) {
1379 p
->pm_partid
= add (*ep
, NULL
);
1382 if (!mh_strcasecmp (*ap
, "number")) {
1383 if (sscanf (*ep
, "%d", &p
->pm_partno
) != 1
1384 || p
->pm_partno
< 1) {
1387 "invalid %s parameter for \"%s/%s\" type in message %s's %s field",
1388 *ap
, ci
->ci_type
, ci
->ci_subtype
,
1389 ct
->c_file
, TYPE_FIELD
);
1394 if (!mh_strcasecmp (*ap
, "total")) {
1395 if (sscanf (*ep
, "%d", &p
->pm_maxno
) != 1
1404 || (p
->pm_maxno
&& p
->pm_partno
> p
->pm_maxno
)) {
1406 "invalid parameters for \"%s/%s\" type in message %s's %s field",
1407 ci
->ci_type
, ci
->ci_subtype
,
1408 ct
->c_file
, TYPE_FIELD
);
1414 case MESSAGE_EXTERNAL
:
1421 if ((e
= (struct exbody
*) calloc (1, sizeof(*e
))) == NULL
)
1422 adios (NULL
, "out of memory");
1423 ct
->c_ctparams
= (void *) e
;
1426 && (ct
->c_fp
= fopen (ct
->c_file
, "r")) == NULL
) {
1427 advise (ct
->c_file
, "unable to open for reading");
1431 fseek (fp
= ct
->c_fp
, ct
->c_begin
, SEEK_SET
);
1433 if (!(p
= get_content (fp
, ct
->c_file
, 0))) {
1441 if ((exresult
= params_external (ct
, 0)) != NOTOK
1442 && p
->c_ceopenfnx
== openMail
) {
1446 if ((size
= ct
->c_end
- p
->c_begin
) <= 0) {
1448 content_error (NULL
, ct
,
1449 "empty body for access-type=mail-server");
1453 e
->eb_body
= bp
= mh_xmalloc ((unsigned) size
);
1454 fseek (p
->c_fp
, p
->c_begin
, SEEK_SET
);
1456 switch (cc
= fread (bp
, sizeof(*bp
), size
, p
->c_fp
)) {
1458 adios ("failed", "fread");
1461 adios (NULL
, "unexpected EOF from fread");
1464 bp
+= cc
, size
-= cc
;
1471 p
->c_end
= p
->c_begin
;
1476 if (exresult
== NOTOK
)
1478 if (e
->eb_flags
== NOTOK
)
1481 switch (p
->c_type
) {
1486 if (p
->c_subtype
!= MESSAGE_RFC822
)
1490 e
->eb_partno
= ct
->c_partno
;
1492 (*p
->c_ctinitfnx
) (p
);
1507 params_external (CT ct
, int composing
)
1510 struct exbody
*e
= (struct exbody
*) ct
->c_ctparams
;
1511 CI ci
= &ct
->c_ctinfo
;
1513 for (ap
= ci
->ci_attrs
, ep
= ci
->ci_values
; *ap
; ap
++, ep
++) {
1514 if (!mh_strcasecmp (*ap
, "access-type")) {
1515 struct str2init
*s2i
;
1516 CT p
= e
->eb_content
;
1518 for (s2i
= str2methods
; s2i
->si_key
; s2i
++)
1519 if (!mh_strcasecmp (*ep
, s2i
->si_key
))
1523 e
->eb_flags
= NOTOK
;
1524 p
->c_encoding
= CE_EXTERNAL
;
1527 e
->eb_access
= s2i
->si_key
;
1528 e
->eb_flags
= s2i
->si_val
;
1529 p
->c_encoding
= CE_EXTERNAL
;
1531 /* Call the Init function for this external type */
1532 if ((*s2i
->si_init
)(p
) == NOTOK
)
1536 if (!mh_strcasecmp (*ap
, "name")) {
1540 if (!mh_strcasecmp (*ap
, "permission")) {
1541 e
->eb_permission
= *ep
;
1544 if (!mh_strcasecmp (*ap
, "site")) {
1548 if (!mh_strcasecmp (*ap
, "directory")) {
1552 if (!mh_strcasecmp (*ap
, "mode")) {
1556 if (!mh_strcasecmp (*ap
, "size")) {
1557 sscanf (*ep
, "%lu", &e
->eb_size
);
1560 if (!mh_strcasecmp (*ap
, "server")) {
1564 if (!mh_strcasecmp (*ap
, "subject")) {
1565 e
->eb_subject
= *ep
;
1568 if (composing
&& !mh_strcasecmp (*ap
, "body")) {
1569 e
->eb_body
= getcpy (*ep
);
1574 if (!e
->eb_access
) {
1576 "invalid parameters for \"%s/%s\" type in message %s's %s field",
1577 ci
->ci_type
, ci
->ci_subtype
, ct
->c_file
, TYPE_FIELD
);
1590 InitApplication (CT ct
)
1593 CI ci
= &ct
->c_ctinfo
;
1596 for (kv
= SubApplication
; kv
->kv_key
; kv
++)
1597 if (!mh_strcasecmp (ci
->ci_subtype
, kv
->kv_key
))
1599 ct
->c_subtype
= kv
->kv_value
;
1606 * TRANSFER ENCODINGS
1610 init_encoding (CT ct
, OpenCEFunc openfnx
)
1614 if ((ce
= (CE
) calloc (1, sizeof(*ce
))) == NULL
)
1615 adios (NULL
, "out of memory");
1618 ct
->c_ceopenfnx
= openfnx
;
1619 ct
->c_ceclosefnx
= close_encoding
;
1620 ct
->c_cesizefnx
= size_encoding
;
1627 close_encoding (CT ct
)
1631 if (!(ce
= ct
->c_cefile
))
1641 static unsigned long
1642 size_encoding (CT ct
)
1650 if (!(ce
= ct
->c_cefile
))
1651 return (ct
->c_end
- ct
->c_begin
);
1653 if (ce
->ce_fp
&& fstat (fileno (ce
->ce_fp
), &st
) != NOTOK
)
1654 return (long) st
.st_size
;
1657 if (stat (ce
->ce_file
, &st
) != NOTOK
)
1658 return (long) st
.st_size
;
1663 if (ct
->c_encoding
== CE_EXTERNAL
)
1664 return (ct
->c_end
- ct
->c_begin
);
1667 if ((fd
= (*ct
->c_ceopenfnx
) (ct
, &file
)) == NOTOK
)
1668 return (ct
->c_end
- ct
->c_begin
);
1670 if (fstat (fd
, &st
) != NOTOK
)
1671 size
= (long) st
.st_size
;
1675 (*ct
->c_ceclosefnx
) (ct
);
1684 static unsigned char b642nib
[0x80] = {
1685 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1686 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1687 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1688 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1689 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1690 0xff, 0xff, 0xff, 0x3e, 0xff, 0xff, 0xff, 0x3f,
1691 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b,
1692 0x3c, 0x3d, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1693 0xff, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
1694 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e,
1695 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16,
1696 0x17, 0x18, 0x19, 0xff, 0xff, 0xff, 0xff, 0xff,
1697 0xff, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20,
1698 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28,
1699 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30,
1700 0x31, 0x32, 0x33, 0xff, 0xff, 0xff, 0xff, 0xff
1707 return init_encoding (ct
, openBase64
);
1712 openBase64 (CT ct
, char **file
)
1714 int bitno
, cc
, digested
;
1715 int fd
, len
, skip
, own_ct_fp
= 0;
1717 unsigned char value
, b
;
1718 unsigned char *cp
, *ep
;
1719 char buffer
[BUFSIZ
];
1720 /* sbeck -- handle suffixes */
1727 fseek (ce
->ce_fp
, 0L, SEEK_SET
);
1732 if ((ce
->ce_fp
= fopen (ce
->ce_file
, "r")) == NULL
) {
1733 content_error (ce
->ce_file
, ct
, "unable to fopen for reading");
1739 if (*file
== NULL
) {
1740 ce
->ce_file
= add (m_mktemp(tmp
, NULL
, NULL
), NULL
);
1743 ce
->ce_file
= add (*file
, NULL
);
1747 /* sbeck@cise.ufl.edu -- handle suffixes */
1749 snprintf (buffer
, sizeof(buffer
), "%s-suffix-%s/%s",
1750 invo_name
, ci
->ci_type
, ci
->ci_subtype
);
1751 cp
= context_find (buffer
);
1752 if (cp
== NULL
|| *cp
== '\0') {
1753 snprintf (buffer
, sizeof(buffer
), "%s-suffix-%s", invo_name
,
1755 cp
= context_find (buffer
);
1757 if (cp
!= NULL
&& *cp
!= '\0') {
1758 if (ce
->ce_unlink
) {
1759 /* Temporary file already exists, so we rename to
1760 version with extension. */
1761 char *file_org
= strdup(ce
->ce_file
);
1762 ce
->ce_file
= add (cp
, ce
->ce_file
);
1763 if (rename(file_org
, ce
->ce_file
)) {
1764 adios (ce
->ce_file
, "unable to rename %s to ", file_org
);
1769 ce
->ce_file
= add (cp
, ce
->ce_file
);
1773 if ((ce
->ce_fp
= fopen (ce
->ce_file
, "w+")) == NULL
) {
1774 content_error (ce
->ce_file
, ct
, "unable to fopen for reading/writing");
1778 if ((len
= ct
->c_end
- ct
->c_begin
) < 0)
1779 adios (NULL
, "internal error(1)");
1782 if ((ct
->c_fp
= fopen (ct
->c_file
, "r")) == NULL
) {
1783 content_error (ct
->c_file
, ct
, "unable to open for reading");
1789 if ((digested
= ct
->c_digested
))
1790 MD5Init (&mdContext
);
1796 lseek (fd
= fileno (ct
->c_fp
), (off_t
) ct
->c_begin
, SEEK_SET
);
1798 switch (cc
= read (fd
, buffer
, sizeof(buffer
) - 1)) {
1800 content_error (ct
->c_file
, ct
, "error reading from");
1804 content_error (NULL
, ct
, "premature eof");
1812 for (ep
= (cp
= buffer
) + cc
; cp
< ep
; cp
++) {
1817 if (skip
|| (*cp
& 0x80)
1818 || (value
= b642nib
[*cp
& 0x7f]) > 0x3f) {
1820 fprintf (stderr
, "*cp=0x%x pos=%ld skip=%d\n",
1822 (long) (lseek (fd
, (off_t
) 0, SEEK_CUR
) - (ep
- cp
)),
1825 content_error (NULL
, ct
,
1826 "invalid BASE64 encoding -- continuing");
1830 bits
|= value
<< bitno
;
1832 if ((bitno
-= 6) < 0) {
1833 b
= (bits
>> 16) & 0xff;
1834 putc ((char) b
, ce
->ce_fp
);
1836 MD5Update (&mdContext
, &b
, 1);
1838 b
= (bits
>> 8) & 0xff;
1839 putc ((char) b
, ce
->ce_fp
);
1841 MD5Update (&mdContext
, &b
, 1);
1844 putc ((char) b
, ce
->ce_fp
);
1846 MD5Update (&mdContext
, &b
, 1);
1850 if (ferror (ce
->ce_fp
)) {
1851 content_error (ce
->ce_file
, ct
,
1852 "error writing to");
1855 bitno
= 18, bits
= 0L, skip
= 0;
1861 goto self_delimiting
;
1870 fprintf (stderr
, "premature ending (bitno %d)\n", bitno
);
1872 content_error (NULL
, ct
, "invalid BASE64 encoding");
1877 fseek (ct
->c_fp
, 0L, SEEK_SET
);
1879 if (fflush (ce
->ce_fp
)) {
1880 content_error (ce
->ce_file
, ct
, "error writing to");
1885 unsigned char digest
[16];
1887 MD5Final (digest
, &mdContext
);
1888 if (memcmp((char *) digest
, (char *) ct
->c_digest
,
1889 sizeof(digest
) / sizeof(digest
[0])))
1890 content_error (NULL
, ct
,
1891 "content integrity suspect (digest mismatch) -- continuing");
1894 fprintf (stderr
, "content integrity confirmed\n");
1897 fseek (ce
->ce_fp
, 0L, SEEK_SET
);
1900 *file
= ce
->ce_file
;
1905 return fileno (ce
->ce_fp
);
1912 free_encoding (ct
, 0);
1921 static char hex2nib
[0x80] = {
1922 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1923 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1924 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1925 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1926 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1927 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1928 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
1929 0x08, 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1930 0x00, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x00,
1931 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1932 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1933 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1934 0x00, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x00,
1935 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1936 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1937 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
1944 return init_encoding (ct
, openQuoted
);
1949 openQuoted (CT ct
, char **file
)
1951 int cc
, digested
, len
, quoted
, own_ct_fp
= 0;
1952 unsigned char *cp
, *ep
;
1953 char buffer
[BUFSIZ
];
1956 /* sbeck -- handle suffixes */
1962 fseek (ce
->ce_fp
, 0L, SEEK_SET
);
1967 if ((ce
->ce_fp
= fopen (ce
->ce_file
, "r")) == NULL
) {
1968 content_error (ce
->ce_file
, ct
, "unable to fopen for reading");
1974 if (*file
== NULL
) {
1975 ce
->ce_file
= add (m_mktemp(tmp
, NULL
, NULL
), NULL
);
1978 ce
->ce_file
= add (*file
, NULL
);
1982 /* sbeck@cise.ufl.edu -- handle suffixes */
1984 snprintf (buffer
, sizeof(buffer
), "%s-suffix-%s/%s",
1985 invo_name
, ci
->ci_type
, ci
->ci_subtype
);
1986 cp
= context_find (buffer
);
1987 if (cp
== NULL
|| *cp
== '\0') {
1988 snprintf (buffer
, sizeof(buffer
), "%s-suffix-%s", invo_name
,
1990 cp
= context_find (buffer
);
1992 if (cp
!= NULL
&& *cp
!= '\0') {
1993 if (ce
->ce_unlink
) {
1994 /* Temporary file already exists, so we rename to
1995 version with extension. */
1996 char *file_org
= strdup(ce
->ce_file
);
1997 ce
->ce_file
= add (cp
, ce
->ce_file
);
1998 if (rename(file_org
, ce
->ce_file
)) {
1999 adios (ce
->ce_file
, "unable to rename %s to ", file_org
);
2004 ce
->ce_file
= add (cp
, ce
->ce_file
);
2008 if ((ce
->ce_fp
= fopen (ce
->ce_file
, "w+")) == NULL
) {
2009 content_error (ce
->ce_file
, ct
, "unable to fopen for reading/writing");
2013 if ((len
= ct
->c_end
- ct
->c_begin
) < 0)
2014 adios (NULL
, "internal error(2)");
2017 if ((ct
->c_fp
= fopen (ct
->c_file
, "r")) == NULL
) {
2018 content_error (ct
->c_file
, ct
, "unable to open for reading");
2024 if ((digested
= ct
->c_digested
))
2025 MD5Init (&mdContext
);
2032 fseek (ct
->c_fp
, ct
->c_begin
, SEEK_SET
);
2034 if (fgets (buffer
, sizeof(buffer
) - 1, ct
->c_fp
) == NULL
) {
2035 content_error (NULL
, ct
, "premature eof");
2039 if ((cc
= strlen (buffer
)) > len
)
2043 for (ep
= (cp
= buffer
) + cc
- 1; cp
<= ep
; ep
--)
2048 for (; cp
< ep
; cp
++) {
2050 /* in an escape sequence */
2052 /* at byte 1 of an escape sequence */
2053 mask
= hex2nib
[*cp
& 0x7f];
2054 /* next is byte 2 */
2057 /* at byte 2 of an escape sequence */
2059 mask
|= hex2nib
[*cp
& 0x7f];
2060 putc (mask
, ce
->ce_fp
);
2062 MD5Update (&mdContext
, &mask
, 1);
2063 if (ferror (ce
->ce_fp
)) {
2064 content_error (ce
->ce_file
, ct
, "error writing to");
2067 /* finished escape sequence; next may be literal or a new
2068 * escape sequence */
2071 /* on to next byte */
2075 /* not in an escape sequence */
2077 /* starting an escape sequence, or invalid '='? */
2078 if (cp
+ 1 < ep
&& cp
[1] == '\n') {
2079 /* "=\n" soft line break, eat the \n */
2083 if (cp
+ 1 >= ep
|| cp
+ 2 >= ep
) {
2084 /* We don't have 2 bytes left, so this is an invalid
2085 * escape sequence; just show the raw bytes (below). */
2086 } else if (isxdigit (cp
[1]) && isxdigit (cp
[2])) {
2087 /* Next 2 bytes are hex digits, making this a valid escape
2088 * sequence; let's decode it (above). */
2092 /* One or both of the next 2 is out of range, making this
2093 * an invalid escape sequence; just show the raw bytes
2098 /* Just show the raw byte. */
2099 putc (*cp
, ce
->ce_fp
);
2102 MD5Update (&mdContext
, (unsigned char *) "\r\n",2);
2104 MD5Update (&mdContext
, (unsigned char *) cp
, 1);
2107 if (ferror (ce
->ce_fp
)) {
2108 content_error (ce
->ce_file
, ct
, "error writing to");
2114 content_error (NULL
, ct
,
2115 "invalid QUOTED-PRINTABLE encoding -- end-of-content while still quoting");
2119 fseek (ct
->c_fp
, 0L, SEEK_SET
);
2121 if (fflush (ce
->ce_fp
)) {
2122 content_error (ce
->ce_file
, ct
, "error writing to");
2127 unsigned char digest
[16];
2129 MD5Final (digest
, &mdContext
);
2130 if (memcmp((char *) digest
, (char *) ct
->c_digest
,
2131 sizeof(digest
) / sizeof(digest
[0])))
2132 content_error (NULL
, ct
,
2133 "content integrity suspect (digest mismatch) -- continuing");
2136 fprintf (stderr
, "content integrity confirmed\n");
2139 fseek (ce
->ce_fp
, 0L, SEEK_SET
);
2142 *file
= ce
->ce_file
;
2147 return fileno (ce
->ce_fp
);
2150 free_encoding (ct
, 0);
2166 if (init_encoding (ct
, open7Bit
) == NOTOK
)
2169 ct
->c_cesizefnx
= NULL
; /* no need to decode for real size */
2175 open7Bit (CT ct
, char **file
)
2177 int cc
, fd
, len
, own_ct_fp
= 0;
2178 char buffer
[BUFSIZ
];
2179 /* sbeck -- handle suffixes */
2186 fseek (ce
->ce_fp
, 0L, SEEK_SET
);
2191 if ((ce
->ce_fp
= fopen (ce
->ce_file
, "r")) == NULL
) {
2192 content_error (ce
->ce_file
, ct
, "unable to fopen for reading");
2198 if (*file
== NULL
) {
2199 ce
->ce_file
= add (m_mktemp(tmp
, NULL
, NULL
), NULL
);
2202 ce
->ce_file
= add (*file
, NULL
);
2206 /* sbeck@cise.ufl.edu -- handle suffixes */
2208 snprintf (buffer
, sizeof(buffer
), "%s-suffix-%s/%s",
2209 invo_name
, ci
->ci_type
, ci
->ci_subtype
);
2210 cp
= context_find (buffer
);
2211 if (cp
== NULL
|| *cp
== '\0') {
2212 snprintf (buffer
, sizeof(buffer
), "%s-suffix-%s", invo_name
,
2214 cp
= context_find (buffer
);
2216 if (cp
!= NULL
&& *cp
!= '\0') {
2217 if (ce
->ce_unlink
) {
2218 /* Temporary file already exists, so we rename to
2219 version with extension. */
2220 char *file_org
= strdup(ce
->ce_file
);
2221 ce
->ce_file
= add (cp
, ce
->ce_file
);
2222 if (rename(file_org
, ce
->ce_file
)) {
2223 adios (ce
->ce_file
, "unable to rename %s to ", file_org
);
2228 ce
->ce_file
= add (cp
, ce
->ce_file
);
2232 if ((ce
->ce_fp
= fopen (ce
->ce_file
, "w+")) == NULL
) {
2233 content_error (ce
->ce_file
, ct
, "unable to fopen for reading/writing");
2237 if (ct
->c_type
== CT_MULTIPART
) {
2239 CI ci
= &ct
->c_ctinfo
;
2242 fprintf (ce
->ce_fp
, "%s: %s/%s", TYPE_FIELD
, ci
->ci_type
, ci
->ci_subtype
);
2243 len
+= strlen (TYPE_FIELD
) + 2 + strlen (ci
->ci_type
)
2244 + 1 + strlen (ci
->ci_subtype
);
2245 for (ap
= ci
->ci_attrs
, ep
= ci
->ci_values
; *ap
; ap
++, ep
++) {
2246 putc (';', ce
->ce_fp
);
2249 snprintf (buffer
, sizeof(buffer
), "%s=\"%s\"", *ap
, *ep
);
2251 if (len
+ 1 + (cc
= strlen (buffer
)) >= CPERLIN
) {
2252 fputs ("\n\t", ce
->ce_fp
);
2255 putc (' ', ce
->ce_fp
);
2258 fprintf (ce
->ce_fp
, "%s", buffer
);
2262 if (ci
->ci_comment
) {
2263 if (len
+ 1 + (cc
= 2 + strlen (ci
->ci_comment
)) >= CPERLIN
) {
2264 fputs ("\n\t", ce
->ce_fp
);
2268 putc (' ', ce
->ce_fp
);
2271 fprintf (ce
->ce_fp
, "(%s)", ci
->ci_comment
);
2274 fprintf (ce
->ce_fp
, "\n");
2276 fprintf (ce
->ce_fp
, "%s:%s", ID_FIELD
, ct
->c_id
);
2278 fprintf (ce
->ce_fp
, "%s:%s", DESCR_FIELD
, ct
->c_descr
);
2280 fprintf (ce
->ce_fp
, "%s:%s", DISPO_FIELD
, ct
->c_dispo
);
2281 fprintf (ce
->ce_fp
, "\n");
2284 if ((len
= ct
->c_end
- ct
->c_begin
) < 0)
2285 adios (NULL
, "internal error(3)");
2288 if ((ct
->c_fp
= fopen (ct
->c_file
, "r")) == NULL
) {
2289 content_error (ct
->c_file
, ct
, "unable to open for reading");
2295 lseek (fd
= fileno (ct
->c_fp
), (off_t
) ct
->c_begin
, SEEK_SET
);
2297 switch (cc
= read (fd
, buffer
, sizeof(buffer
) - 1)) {
2299 content_error (ct
->c_file
, ct
, "error reading from");
2303 content_error (NULL
, ct
, "premature eof");
2311 fwrite (buffer
, sizeof(*buffer
), cc
, ce
->ce_fp
);
2312 if (ferror (ce
->ce_fp
)) {
2313 content_error (ce
->ce_file
, ct
, "error writing to");
2318 fseek (ct
->c_fp
, 0L, SEEK_SET
);
2320 if (fflush (ce
->ce_fp
)) {
2321 content_error (ce
->ce_file
, ct
, "error writing to");
2325 fseek (ce
->ce_fp
, 0L, SEEK_SET
);
2328 *file
= ce
->ce_file
;
2333 return fileno (ce
->ce_fp
);
2336 free_encoding (ct
, 0);
2350 openExternal (CT ct
, CT cb
, CE ce
, char **file
, int *fd
)
2352 char cachefile
[BUFSIZ
];
2355 fseek (ce
->ce_fp
, 0L, SEEK_SET
);
2360 if ((ce
->ce_fp
= fopen (ce
->ce_file
, "r")) == NULL
) {
2361 content_error (ce
->ce_file
, ct
, "unable to fopen for reading");
2367 if (find_cache (ct
, rcachesw
, (int *) 0, cb
->c_id
,
2368 cachefile
, sizeof(cachefile
)) != NOTOK
) {
2369 if ((ce
->ce_fp
= fopen (cachefile
, "r"))) {
2370 ce
->ce_file
= getcpy (cachefile
);
2374 admonish (cachefile
, "unable to fopen for reading");
2381 *file
= ce
->ce_file
;
2382 *fd
= fileno (ce
->ce_fp
);
2393 return init_encoding (ct
, openFile
);
2398 openFile (CT ct
, char **file
)
2401 char cachefile
[BUFSIZ
];
2402 struct exbody
*e
= ct
->c_ctexbody
;
2403 CE ce
= ct
->c_cefile
;
2405 switch (openExternal (e
->eb_parent
, e
->eb_content
, ce
, file
, &fd
)) {
2417 content_error (NULL
, ct
, "missing name parameter");
2421 ce
->ce_file
= getcpy (e
->eb_name
);
2424 if ((ce
->ce_fp
= fopen (ce
->ce_file
, "r")) == NULL
) {
2425 content_error (ce
->ce_file
, ct
, "unable to fopen for reading");
2429 if ((!e
->eb_permission
|| mh_strcasecmp (e
->eb_permission
, "read-write"))
2430 && find_cache (NULL
, wcachesw
, &cachetype
, e
->eb_content
->c_id
,
2431 cachefile
, sizeof(cachefile
)) != NOTOK
) {
2435 mask
= umask (cachetype
? ~m_gmprot () : 0222);
2436 if ((fp
= fopen (cachefile
, "w"))) {
2438 char buffer
[BUFSIZ
];
2439 FILE *gp
= ce
->ce_fp
;
2441 fseek (gp
, 0L, SEEK_SET
);
2443 while ((cc
= fread (buffer
, sizeof(*buffer
), sizeof(buffer
), gp
))
2445 fwrite (buffer
, sizeof(*buffer
), cc
, fp
);
2449 admonish (ce
->ce_file
, "error reading");
2454 admonish (cachefile
, "error writing");
2462 fseek (ce
->ce_fp
, 0L, SEEK_SET
);
2463 *file
= ce
->ce_file
;
2464 return fileno (ce
->ce_fp
);
2474 return init_encoding (ct
, openFTP
);
2479 openFTP (CT ct
, char **file
)
2481 int cachetype
, caching
, fd
;
2483 char *bp
, *ftp
, *user
, *pass
;
2484 char buffer
[BUFSIZ
], cachefile
[BUFSIZ
];
2487 static char *username
= NULL
;
2488 static char *password
= NULL
;
2493 if ((ftp
= context_find (nmhaccessftp
)) && !*ftp
)
2499 switch (openExternal (e
->eb_parent
, e
->eb_content
, ce
, file
, &fd
)) {
2510 if (!e
->eb_name
|| !e
->eb_site
) {
2511 content_error (NULL
, ct
, "missing %s parameter",
2512 e
->eb_name
? "site": "name");
2519 pidcheck (pidwait (xpid
, NOTOK
));
2523 /* Get the buffer ready to go */
2525 buflen
= sizeof(buffer
);
2528 * Construct the query message for user
2530 snprintf (bp
, buflen
, "Retrieve %s", e
->eb_name
);
2536 snprintf (bp
, buflen
, " (content %s)", e
->eb_partno
);
2542 snprintf (bp
, buflen
, "\n using %sFTP from site %s",
2543 e
->eb_flags
? "anonymous " : "", e
->eb_site
);
2548 if (e
->eb_size
> 0) {
2549 snprintf (bp
, buflen
, " (%lu octets)", e
->eb_size
);
2554 snprintf (bp
, buflen
, "? ");
2557 * Now, check the answer
2559 if (!getanswer (buffer
))
2564 snprintf (buffer
, sizeof(buffer
), "%s@%s", getusername (),
2568 ruserpass (e
->eb_site
, &username
, &password
);
2573 ce
->ce_unlink
= (*file
== NULL
);
2575 cachefile
[0] = '\0';
2576 if ((!e
->eb_permission
|| mh_strcasecmp (e
->eb_permission
, "read-write"))
2577 && find_cache (NULL
, wcachesw
, &cachetype
, e
->eb_content
->c_id
,
2578 cachefile
, sizeof(cachefile
)) != NOTOK
) {
2579 if (*file
== NULL
) {
2586 ce
->ce_file
= add (*file
, NULL
);
2588 ce
->ce_file
= add (cachefile
, NULL
);
2590 ce
->ce_file
= add (m_mktemp(tmp
, NULL
, NULL
), NULL
);
2592 if ((ce
->ce_fp
= fopen (ce
->ce_file
, "w+")) == NULL
) {
2593 content_error (ce
->ce_file
, ct
, "unable to fopen for reading/writing");
2598 int child_id
, i
, vecp
;
2602 vec
[vecp
++] = r1bindex (ftp
, '/');
2603 vec
[vecp
++] = e
->eb_site
;
2606 vec
[vecp
++] = e
->eb_dir
;
2607 vec
[vecp
++] = e
->eb_name
;
2608 vec
[vecp
++] = ce
->ce_file
,
2609 vec
[vecp
++] = e
->eb_mode
&& !mh_strcasecmp (e
->eb_mode
, "ascii")
2610 ? "ascii" : "binary";
2615 for (i
= 0; (child_id
= vfork()) == NOTOK
&& i
< 5; i
++)
2619 adios ("fork", "unable to");
2623 close (fileno (ce
->ce_fp
));
2625 fprintf (stderr
, "unable to exec ");
2631 if (pidXwait (child_id
, NULL
)) {
2632 username
= password
= NULL
;
2642 chmod (cachefile
, cachetype
? m_gmprot () : 0444);
2647 mask
= umask (cachetype
? ~m_gmprot () : 0222);
2648 if ((fp
= fopen (cachefile
, "w"))) {
2650 FILE *gp
= ce
->ce_fp
;
2652 fseek (gp
, 0L, SEEK_SET
);
2654 while ((cc
= fread (buffer
, sizeof(*buffer
), sizeof(buffer
), gp
))
2656 fwrite (buffer
, sizeof(*buffer
), cc
, fp
);
2660 admonish (ce
->ce_file
, "error reading");
2665 admonish (cachefile
, "error writing");
2674 fseek (ce
->ce_fp
, 0L, SEEK_SET
);
2675 *file
= ce
->ce_file
;
2676 return fileno (ce
->ce_fp
);
2687 return init_encoding (ct
, openMail
);
2692 openMail (CT ct
, char **file
)
2694 int child_id
, fd
, i
, vecp
;
2696 char *bp
, buffer
[BUFSIZ
], *vec
[7];
2697 struct exbody
*e
= ct
->c_ctexbody
;
2698 CE ce
= ct
->c_cefile
;
2700 switch (openExternal (e
->eb_parent
, e
->eb_content
, ce
, file
, &fd
)) {
2711 if (!e
->eb_server
) {
2712 content_error (NULL
, ct
, "missing server parameter");
2719 pidcheck (pidwait (xpid
, NOTOK
));
2723 /* Get buffer ready to go */
2725 buflen
= sizeof(buffer
);
2727 /* Now, construct query message */
2728 snprintf (bp
, buflen
, "Retrieve content");
2734 snprintf (bp
, buflen
, " %s", e
->eb_partno
);
2740 snprintf (bp
, buflen
, " by asking %s\n\n%s\n? ",
2742 e
->eb_subject
? e
->eb_subject
: e
->eb_body
);
2744 /* Now, check answer */
2745 if (!getanswer (buffer
))
2749 vec
[vecp
++] = r1bindex (mailproc
, '/');
2750 vec
[vecp
++] = e
->eb_server
;
2751 vec
[vecp
++] = "-subject";
2752 vec
[vecp
++] = e
->eb_subject
? e
->eb_subject
: "mail-server request";
2753 vec
[vecp
++] = "-body";
2754 vec
[vecp
++] = e
->eb_body
;
2757 for (i
= 0; (child_id
= vfork()) == NOTOK
&& i
< 5; i
++)
2761 advise ("fork", "unable to");
2765 execvp (mailproc
, vec
);
2766 fprintf (stderr
, "unable to exec ");
2772 if (pidXwait (child_id
, NULL
) == OK
)
2773 advise (NULL
, "request sent");
2777 if (*file
== NULL
) {
2778 ce
->ce_file
= add (m_mktemp(tmp
, NULL
, NULL
), NULL
);
2781 ce
->ce_file
= add (*file
, NULL
);
2785 if ((ce
->ce_fp
= fopen (ce
->ce_file
, "w+")) == NULL
) {
2786 content_error (ce
->ce_file
, ct
, "unable to fopen for reading/writing");
2790 /* showproc is for mhshow and mhstore, though mhlist -debug
2791 * prints it, too. */
2793 free (ct
->c_showproc
);
2794 ct
->c_showproc
= add ("true", NULL
);
2796 fseek (ce
->ce_fp
, 0L, SEEK_SET
);
2797 *file
= ce
->ce_file
;
2798 return fileno (ce
->ce_fp
);
2803 readDigest (CT ct
, char *cp
)
2808 unsigned char *dp
, value
, *ep
;
2814 for (ep
= (dp
= ct
->c_digest
)
2815 + sizeof(ct
->c_digest
) / sizeof(ct
->c_digest
[0]); *cp
; cp
++)
2820 || (value
= b642nib
[*cp
& 0x7f]) > 0x3f) {
2822 fprintf (stderr
, "invalid BASE64 encoding\n");
2826 bits
|= value
<< bitno
;
2828 if ((bitno
-= 6) < 0) {
2829 if (dp
+ (3 - skip
) > ep
)
2830 goto invalid_digest
;
2831 *dp
++ = (bits
>> 16) & 0xff;
2833 *dp
++ = (bits
>> 8) & 0xff;
2835 *dp
++ = bits
& 0xff;
2845 goto self_delimiting
;
2850 fprintf (stderr
, "premature ending (bitno %d)\n", bitno
);
2860 fprintf (stderr
, "invalid MD5 digest (got %d octets)\n",
2868 fprintf (stderr
, "MD5 digest=");
2869 for (dp
= ct
->c_digest
; dp
< ep
; dp
++)
2870 fprintf (stderr
, "%02x", *dp
& 0xff);
2871 fprintf (stderr
, "\n");