]>
diplodocus.org Git - nmh/blob - uip/mhparse.c
d4c6f10a037deb4f1b86e23a382674c2bc8a708f
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
];
265 m_getfld_state_t gstate
= 0;
267 /* allocate the content structure */
268 if (!(ct
= (CT
) calloc (1, sizeof(*ct
))))
269 adios (NULL
, "out of memory");
272 ct
->c_file
= add (file
, NULL
);
273 ct
->c_begin
= ftell (ct
->c_fp
) + 1;
276 * Parse the header fields for this
277 * content into a linked list.
279 for (compnum
= 1;;) {
280 int bufsz
= sizeof buf
;
281 switch (state
= m_getfld (&gstate
, name
, buf
, &bufsz
, in
)) {
286 /* get copies of the buffers */
287 np
= add (name
, NULL
);
288 vp
= add (buf
, NULL
);
290 /* if necessary, get rest of field */
291 while (state
== FLDPLUS
) {
293 state
= m_getfld (&gstate
, name
, buf
, &bufsz
, in
);
294 vp
= add (buf
, vp
); /* add to previous value */
297 /* Now add the header data to the list */
298 add_header (ct
, np
, vp
);
300 /* continue, to see if this isn't the last header field */
301 ct
->c_begin
= ftell (in
) + 1;
305 ct
->c_begin
= ftell (in
) - strlen (buf
);
309 ct
->c_begin
= ftell (in
);
314 adios (NULL
, "message format error in component #%d", compnum
);
317 adios (NULL
, "getfld() returned %d", state
);
320 /* break out of the loop */
323 m_getfld_state_destroy (&gstate
);
326 * Read the content headers. We will parse the
327 * MIME related header fields into their various
328 * structures and set internal flags related to
329 * content type/subtype, etc.
332 hp
= ct
->c_first_hf
; /* start at first header field */
334 /* Get MIME-Version field */
335 if (!mh_strcasecmp (hp
->name
, VRSN_FIELD
)) {
338 unsigned char *cp
, *dp
;
341 advise (NULL
, "message %s has multiple %s: fields",
342 ct
->c_file
, VRSN_FIELD
);
345 ct
->c_vrsn
= add (hp
->value
, NULL
);
347 /* Now, cleanup this field */
350 while (isspace (*cp
))
352 for (dp
= strchr(cp
, '\n'); dp
; dp
= strchr(dp
, '\n'))
354 for (dp
= cp
+ strlen (cp
) - 1; dp
>= cp
; dp
--)
359 fprintf (stderr
, "%s: %s\n", VRSN_FIELD
, cp
);
361 if (*cp
== '(' && get_comment (ct
, &cp
, 0) == NOTOK
)
364 for (dp
= cp
; istoken (*dp
); dp
++)
368 ucmp
= !mh_strcasecmp (cp
, VRSN_VALUE
);
371 admonish (NULL
, "message %s has unknown value for %s: field (%s)",
372 ct
->c_file
, VRSN_FIELD
, cp
);
375 else if (!mh_strcasecmp (hp
->name
, TYPE_FIELD
)) {
376 /* Get Content-Type field */
377 struct str2init
*s2i
;
378 CI ci
= &ct
->c_ctinfo
;
380 /* Check if we've already seen a Content-Type header */
382 advise (NULL
, "message %s has multiple %s: fields",
383 ct
->c_file
, TYPE_FIELD
);
387 /* Parse the Content-Type field */
388 if (get_ctinfo (hp
->value
, ct
, 0) == NOTOK
)
392 * Set the Init function and the internal
393 * flag for this content type.
395 for (s2i
= str2cts
; s2i
->si_key
; s2i
++)
396 if (!mh_strcasecmp (ci
->ci_type
, s2i
->si_key
))
398 if (!s2i
->si_key
&& !uprf (ci
->ci_type
, "X-"))
400 ct
->c_type
= s2i
->si_val
;
401 ct
->c_ctinitfnx
= s2i
->si_init
;
403 else if (!mh_strcasecmp (hp
->name
, ENCODING_FIELD
)) {
404 /* Get Content-Transfer-Encoding field */
406 unsigned char *cp
, *dp
;
407 struct str2init
*s2i
;
410 * Check if we've already seen the
411 * Content-Transfer-Encoding field
414 advise (NULL
, "message %s has multiple %s: fields",
415 ct
->c_file
, ENCODING_FIELD
);
419 /* get copy of this field */
420 ct
->c_celine
= cp
= add (hp
->value
, NULL
);
422 while (isspace (*cp
))
424 for (dp
= cp
; istoken (*dp
); dp
++)
430 * Find the internal flag and Init function
431 * for this transfer encoding.
433 for (s2i
= str2ces
; s2i
->si_key
; s2i
++)
434 if (!mh_strcasecmp (cp
, s2i
->si_key
))
436 if (!s2i
->si_key
&& !uprf (cp
, "X-"))
439 ct
->c_encoding
= s2i
->si_val
;
441 /* Call the Init function for this encoding */
442 if (s2i
->si_init
&& (*s2i
->si_init
) (ct
) == NOTOK
)
445 else if (!mh_strcasecmp (hp
->name
, MD5_FIELD
)) {
446 /* Get Content-MD5 field */
447 unsigned char *cp
, *dp
;
453 if (ct
->c_digested
) {
454 advise (NULL
, "message %s has multiple %s: fields",
455 ct
->c_file
, MD5_FIELD
);
459 ep
= cp
= add (hp
->value
, NULL
); /* get a copy */
461 while (isspace (*cp
))
463 for (dp
= strchr(cp
, '\n'); dp
; dp
= strchr(dp
, '\n'))
465 for (dp
= cp
+ strlen (cp
) - 1; dp
>= cp
; dp
--)
470 fprintf (stderr
, "%s: %s\n", MD5_FIELD
, cp
);
472 if (*cp
== '(' && get_comment (ct
, &cp
, 0) == NOTOK
) {
477 for (dp
= cp
; *dp
&& !isspace (*dp
); dp
++)
485 else if (!mh_strcasecmp (hp
->name
, ID_FIELD
)) {
486 /* Get Content-ID field */
487 ct
->c_id
= add (hp
->value
, ct
->c_id
);
489 else if (!mh_strcasecmp (hp
->name
, DESCR_FIELD
)) {
490 /* Get Content-Description field */
491 ct
->c_descr
= add (hp
->value
, ct
->c_descr
);
493 else if (!mh_strcasecmp (hp
->name
, DISPO_FIELD
)) {
494 /* Get Content-Disposition field */
495 ct
->c_dispo
= add (hp
->value
, ct
->c_dispo
);
499 hp
= hp
->next
; /* next header field */
503 * Check if we saw a Content-Type field.
504 * If not, then assign a default value for
505 * it, and the Init function.
509 * If we are inside a multipart/digest message,
510 * so default type is message/rfc822
513 if (get_ctinfo ("message/rfc822", ct
, 0) == NOTOK
)
515 ct
->c_type
= CT_MESSAGE
;
516 ct
->c_ctinitfnx
= InitMessage
;
519 * Else default type is text/plain
521 if (get_ctinfo ("text/plain", ct
, 0) == NOTOK
)
523 ct
->c_type
= CT_TEXT
;
524 ct
->c_ctinitfnx
= InitText
;
528 /* Use default Transfer-Encoding, if necessary */
530 ct
->c_encoding
= CE_7BIT
;
543 * small routine to add header field to list
547 add_header (CT ct
, char *name
, char *value
)
551 /* allocate header field structure */
552 hp
= mh_xmalloc (sizeof(*hp
));
554 /* link data into header structure */
559 /* link header structure into the list */
560 if (ct
->c_first_hf
== NULL
) {
561 ct
->c_first_hf
= hp
; /* this is the first */
564 ct
->c_last_hf
->next
= hp
; /* add it to the end */
572 /* Make sure that buf contains at least one appearance of name,
573 followed by =. If not, insert both name and value, just after
574 first semicolon, if any. Note that name should not contain a
575 trailing =. And quotes will be added around the value. Typical
576 usage: make sure that a Content-Disposition header contains
577 filename="foo". If it doesn't and value does, use value from
580 incl_name_value (unsigned char *buf
, char *name
, char *value
) {
583 /* Assume that name is non-null. */
585 char *name_plus_equal
= concat (name
, "=", NULL
);
587 if (! strstr (buf
, name_plus_equal
)) {
590 char *prefix
, *suffix
;
592 /* Trim trailing space, esp. newline. */
593 for (cp
= &buf
[strlen (buf
) - 1];
594 cp
>= buf
&& isspace (*cp
);
599 insertion
= concat ("; ", name
, "=", "\"", value
, "\"", NULL
);
601 /* Insert at first semicolon, if any. If none, append to
603 prefix
= add (buf
, NULL
);
604 if ((cp
= strchr (prefix
, ';'))) {
605 suffix
= concat (cp
, NULL
);
607 newbuf
= concat (prefix
, insertion
, suffix
, "\n", NULL
);
611 newbuf
= concat (buf
, insertion
, "\n", NULL
);
619 free (name_plus_equal
);
625 /* Extract just name_suffix="foo", if any, from value. If there isn't
626 one, return the entire value. Note that, for example, a name_suffix
627 of name will match filename="foo", and return foo. */
629 extract_name_value (char *name_suffix
, char *value
) {
630 char *extracted_name_value
= value
;
631 char *name_suffix_plus_quote
= concat (name_suffix
, "=\"", NULL
);
632 char *name_suffix_equals
= strstr (value
, name_suffix_plus_quote
);
635 free (name_suffix_plus_quote
);
636 if (name_suffix_equals
) {
637 char *name_suffix_begin
;
640 for (cp
= name_suffix_equals
; *cp
!= '"'; ++cp
) /* empty */;
641 name_suffix_begin
= ++cp
;
642 /* Find second \". */
643 for (; *cp
!= '"'; ++cp
) /* empty */;
645 extracted_name_value
= mh_xmalloc (cp
- name_suffix_begin
+ 1);
646 memcpy (extracted_name_value
,
648 cp
- name_suffix_begin
);
649 extracted_name_value
[cp
- name_suffix_begin
] = '\0';
652 return extracted_name_value
;
656 * Parse Content-Type line and (if `magic' is non-zero) mhbuild composition
657 * directives. Fills in the information of the CTinfo structure.
660 get_ctinfo (unsigned char *cp
, CT ct
, int magic
)
669 i
= strlen (invo_name
) + 2;
671 /* store copy of Content-Type line */
672 cp
= ct
->c_ctline
= add (cp
, NULL
);
674 while (isspace (*cp
)) /* trim leading spaces */
677 /* change newlines to spaces */
678 for (dp
= strchr(cp
, '\n'); dp
; dp
= strchr(dp
, '\n'))
681 /* trim trailing spaces */
682 for (dp
= cp
+ strlen (cp
) - 1; dp
>= cp
; dp
--)
688 fprintf (stderr
, "%s: %s\n", TYPE_FIELD
, cp
);
690 if (*cp
== '(' && get_comment (ct
, &cp
, 1) == NOTOK
)
693 for (dp
= cp
; istoken (*dp
); dp
++)
696 ci
->ci_type
= add (cp
, NULL
); /* store content type */
700 advise (NULL
, "invalid %s: field in message %s (empty type)",
701 TYPE_FIELD
, ct
->c_file
);
705 /* down case the content type string */
706 for (dp
= ci
->ci_type
; *dp
; dp
++)
707 if (isalpha(*dp
) && isupper (*dp
))
710 while (isspace (*cp
))
713 if (*cp
== '(' && get_comment (ct
, &cp
, 1) == NOTOK
)
718 ci
->ci_subtype
= add ("", NULL
);
723 while (isspace (*cp
))
726 if (*cp
== '(' && get_comment (ct
, &cp
, 1) == NOTOK
)
729 for (dp
= cp
; istoken (*dp
); dp
++)
732 ci
->ci_subtype
= add (cp
, NULL
); /* store the content subtype */
735 if (!*ci
->ci_subtype
) {
737 "invalid %s: field in message %s (empty subtype for \"%s\")",
738 TYPE_FIELD
, ct
->c_file
, ci
->ci_type
);
742 /* down case the content subtype string */
743 for (dp
= ci
->ci_subtype
; *dp
; dp
++)
744 if (isalpha(*dp
) && isupper (*dp
))
748 while (isspace (*cp
))
751 if (*cp
== '(' && get_comment (ct
, &cp
, 1) == NOTOK
)
755 * Parse attribute/value pairs given with Content-Type
757 ep
= (ap
= ci
->ci_attrs
) + NPARMS
;
764 "too many parameters in message %s's %s: field (%d max)",
765 ct
->c_file
, TYPE_FIELD
, NPARMS
);
770 while (isspace (*cp
))
773 if (*cp
== '(' && get_comment (ct
, &cp
, 1) == NOTOK
)
778 "extraneous trailing ';' in message %s's %s: parameter list",
779 ct
->c_file
, TYPE_FIELD
);
783 /* down case the attribute name */
784 for (dp
= cp
; istoken (*dp
); dp
++)
785 if (isalpha(*dp
) && isupper (*dp
))
788 for (up
= dp
; isspace (*dp
);)
790 if (dp
== cp
|| *dp
!= '=') {
792 "invalid parameter in message %s's %s: field\n%*.*sparameter %s (error detected at offset %d)",
793 ct
->c_file
, TYPE_FIELD
, i
, i
, "", cp
, dp
- cp
);
797 vp
= (*ap
= add (cp
, NULL
)) + (up
- cp
);
799 for (dp
++; isspace (*dp
);)
802 /* now add the attribute value */
803 ci
->ci_values
[ap
- ci
->ci_attrs
] = vp
= *ap
+ (dp
- cp
);
806 for (cp
= ++dp
, dp
= vp
;;) {
811 "invalid quoted-string in message %s's %s: field\n%*.*s(parameter %s)",
812 ct
->c_file
, TYPE_FIELD
, i
, i
, "", *ap
);
817 if ((c
= *cp
++) == '\0')
832 for (cp
= dp
, dp
= vp
; istoken (*cp
); cp
++, dp
++)
838 "invalid parameter in message %s's %s: field\n%*.*s(parameter %s)",
839 ct
->c_file
, TYPE_FIELD
, i
, i
, "", *ap
);
844 while (isspace (*cp
))
847 if (*cp
== '(' && get_comment (ct
, &cp
, 1) == NOTOK
)
852 * Get any <Content-Id> given in buffer
854 if (magic
&& *cp
== '<') {
859 if (!(dp
= strchr(ct
->c_id
= ++cp
, '>'))) {
860 advise (NULL
, "invalid ID in message %s", ct
->c_file
);
866 ct
->c_id
= concat ("<", ct
->c_id
, ">\n", NULL
);
872 while (isspace (*cp
))
877 * Get any [Content-Description] given in buffer.
879 if (magic
&& *cp
== '[') {
881 for (dp
= cp
+ strlen (cp
) - 1; dp
>= cp
; dp
--)
885 advise (NULL
, "invalid description in message %s", ct
->c_file
);
893 ct
->c_descr
= concat (ct
->c_descr
, "\n", NULL
);
899 while (isspace (*cp
))
904 * Get any {Content-Disposition} given in buffer.
906 if (magic
&& *cp
== '{') {
908 for (dp
= cp
+ strlen (cp
) - 1; dp
>= cp
; dp
--)
912 advise (NULL
, "invalid disposition in message %s", ct
->c_file
);
920 ct
->c_dispo
= concat (ct
->c_dispo
, "\n", NULL
);
926 while (isspace (*cp
))
931 * Check if anything is left over
935 ci
->ci_magic
= add (cp
, NULL
);
937 /* If there is a Content-Disposition header and it doesn't
938 have a *filename=, extract it from the magic contents.
939 The r1bindex call skips any leading directory
943 incl_name_value (ct
->c_dispo
,
945 r1bindex (extract_name_value ("name",
952 "extraneous information in message %s's %s: field\n%*.*s(%s)",
953 ct
->c_file
, TYPE_FIELD
, i
, i
, "", cp
);
961 get_comment (CT ct
, unsigned char **ap
, int istype
)
966 char c
, buffer
[BUFSIZ
], *dp
;
978 advise (NULL
, "invalid comment in message %s's %s: field",
979 ct
->c_file
, istype
? TYPE_FIELD
: VRSN_FIELD
);
984 if ((c
= *cp
++) == '\0')
1007 if ((dp
= ci
->ci_comment
)) {
1008 ci
->ci_comment
= concat (dp
, " ", buffer
, NULL
);
1011 ci
->ci_comment
= add (buffer
, NULL
);
1015 while (isspace (*cp
))
1026 * Handles content types audio, image, and video.
1027 * There's not much to do right here.
1035 return OK
; /* not much to do here */
1046 char buffer
[BUFSIZ
];
1048 char **ap
, **ep
, *cp
;
1051 CI ci
= &ct
->c_ctinfo
;
1053 /* check for missing subtype */
1054 if (!*ci
->ci_subtype
)
1055 ci
->ci_subtype
= add ("plain", ci
->ci_subtype
);
1058 for (kv
= SubText
; kv
->kv_key
; kv
++)
1059 if (!mh_strcasecmp (ci
->ci_subtype
, kv
->kv_key
))
1061 ct
->c_subtype
= kv
->kv_value
;
1063 /* allocate text character set structure */
1064 if ((t
= (struct text
*) calloc (1, sizeof(*t
))) == NULL
)
1065 adios (NULL
, "out of memory");
1066 ct
->c_ctparams
= (void *) t
;
1068 /* scan for charset parameter */
1069 for (ap
= ci
->ci_attrs
, ep
= ci
->ci_values
; *ap
; ap
++, ep
++)
1070 if (!mh_strcasecmp (*ap
, "charset"))
1073 /* check if content specified a character set */
1076 t
->tx_charset
= CHARSET_SPECIFIED
;
1078 t
->tx_charset
= CHARSET_UNSPECIFIED
;
1082 * If we can not handle character set natively,
1083 * then check profile for string to modify the
1084 * terminal or display method.
1086 * termproc is for mhshow, though mhlist -debug prints it, too.
1088 if (chset
!= NULL
&& !check_charset (chset
, strlen (chset
))) {
1089 snprintf (buffer
, sizeof(buffer
), "%s-charset-%s", invo_name
, chset
);
1090 if ((cp
= context_find (buffer
)))
1091 ct
->c_termproc
= getcpy (cp
);
1103 InitMultiPart (CT ct
)
1107 unsigned char *cp
, *dp
;
1109 char *bp
, buffer
[BUFSIZ
];
1110 struct multipart
*m
;
1112 struct part
*part
, **next
;
1113 CI ci
= &ct
->c_ctinfo
;
1118 * The encoding for multipart messages must be either
1119 * 7bit, 8bit, or binary (per RFC2045).
1121 if (ct
->c_encoding
!= CE_7BIT
&& ct
->c_encoding
!= CE_8BIT
1122 && ct
->c_encoding
!= CE_BINARY
) {
1123 /* Copy the Content-Transfer-Encoding header field body so we can
1124 remove any trailing whitespace and leading blanks from it. */
1125 char *cte
= add (ct
->c_celine
? ct
->c_celine
: "(null)", NULL
);
1127 bp
= cte
+ strlen (cte
) - 1;
1128 while (bp
>= cte
&& isspace (*bp
)) *bp
-- = '\0';
1129 for (bp
= cte
; *bp
&& isblank (*bp
); ++bp
) continue;
1132 "\"%s/%s\" type in message %s must be encoded in\n"
1133 "7bit, 8bit, or binary, per RFC 2045 (6.4). One workaround "
1134 "is to\nmanually edit the file and change the \"%s\"\n"
1135 "Content-Transfer-Encoding to one of those. For now",
1136 ci
->ci_type
, ci
->ci_subtype
, ct
->c_file
, bp
);
1143 for (kv
= SubMultiPart
; kv
->kv_key
; kv
++)
1144 if (!mh_strcasecmp (ci
->ci_subtype
, kv
->kv_key
))
1146 ct
->c_subtype
= kv
->kv_value
;
1149 * Check for "boundary" parameter, which is
1150 * required for multipart messages.
1153 for (ap
= ci
->ci_attrs
, ep
= ci
->ci_values
; *ap
; ap
++, ep
++) {
1154 if (!mh_strcasecmp (*ap
, "boundary")) {
1160 /* complain if boundary parameter is missing */
1163 "a \"boundary\" parameter is mandatory for \"%s/%s\" type in message %s's %s: field",
1164 ci
->ci_type
, ci
->ci_subtype
, ct
->c_file
, TYPE_FIELD
);
1168 /* allocate primary structure for multipart info */
1169 if ((m
= (struct multipart
*) calloc (1, sizeof(*m
))) == NULL
)
1170 adios (NULL
, "out of memory");
1171 ct
->c_ctparams
= (void *) m
;
1173 /* check if boundary parameter contains only whitespace characters */
1174 for (cp
= bp
; isspace (*cp
); cp
++)
1177 advise (NULL
, "invalid \"boundary\" parameter for \"%s/%s\" type in message %s's %s: field",
1178 ci
->ci_type
, ci
->ci_subtype
, ct
->c_file
, TYPE_FIELD
);
1182 /* remove trailing whitespace from boundary parameter */
1183 for (cp
= bp
, dp
= cp
+ strlen (cp
) - 1; dp
> cp
; dp
--)
1188 /* record boundary separators */
1189 m
->mp_start
= concat (bp
, "\n", NULL
);
1190 m
->mp_stop
= concat (bp
, "--\n", NULL
);
1192 if (!ct
->c_fp
&& (ct
->c_fp
= fopen (ct
->c_file
, "r")) == NULL
) {
1193 advise (ct
->c_file
, "unable to open for reading");
1197 fseek (fp
= ct
->c_fp
, pos
= ct
->c_begin
, SEEK_SET
);
1199 next
= &m
->mp_parts
;
1203 while (fgets (buffer
, sizeof(buffer
) - 1, fp
)) {
1207 pos
+= strlen (buffer
);
1208 if (buffer
[0] != '-' || buffer
[1] != '-')
1211 if (strcmp (buffer
+ 2, m
->mp_start
))
1214 if ((part
= (struct part
*) calloc (1, sizeof(*part
))) == NULL
)
1215 adios (NULL
, "out of memory");
1217 next
= &part
->mp_next
;
1219 if (!(p
= get_content (fp
, ct
->c_file
,
1220 ct
->c_subtype
== MULTI_DIGEST
? -1 : 0))) {
1227 fseek (fp
, pos
, SEEK_SET
);
1230 if (strcmp (buffer
+ 2, m
->mp_start
) == 0) {
1234 p
->c_end
= ftell(fp
) - (strlen(buffer
) + 1);
1235 if (p
->c_end
< p
->c_begin
)
1236 p
->c_begin
= p
->c_end
;
1241 if (strcmp (buffer
+ 2, m
->mp_stop
) == 0)
1247 advise (NULL
, "bogus multipart content in message %s", ct
->c_file
);
1248 if (!inout
&& part
) {
1250 p
->c_end
= ct
->c_end
;
1252 if (p
->c_begin
>= p
->c_end
) {
1253 for (next
= &m
->mp_parts
; *next
!= part
;
1254 next
= &((*next
)->mp_next
))
1258 free ((char *) part
);
1263 /* reverse the order of the parts for multipart/alternative */
1264 if (ct
->c_subtype
== MULTI_ALTERNATE
)
1268 * label all subparts with part number, and
1269 * then initialize the content of the subpart.
1274 char partnam
[BUFSIZ
];
1277 snprintf (partnam
, sizeof(partnam
), "%s.", ct
->c_partno
);
1278 pp
= partnam
+ strlen (partnam
);
1283 for (part
= m
->mp_parts
, partnum
= 1; part
;
1284 part
= part
->mp_next
, partnum
++) {
1287 sprintf (pp
, "%d", partnum
);
1288 p
->c_partno
= add (partnam
, NULL
);
1290 /* initialize the content of the subparts */
1291 if (p
->c_ctinitfnx
&& (*p
->c_ctinitfnx
) (p
) == NOTOK
) {
1306 * reverse the order of the parts of a multipart
1310 reverse_parts (CT ct
)
1313 struct multipart
*m
;
1314 struct part
**base
, **bmp
, **next
, *part
;
1316 m
= (struct multipart
*) ct
->c_ctparams
;
1318 /* if only one part, just return */
1319 if (!m
->mp_parts
|| !m
->mp_parts
->mp_next
)
1322 /* count number of parts */
1324 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
)
1327 /* allocate array of pointers to the parts */
1328 if (!(base
= (struct part
**) calloc ((size_t) (i
+ 1), sizeof(*base
))))
1329 adios (NULL
, "out of memory");
1332 /* point at all the parts */
1333 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
)
1337 /* reverse the order of the parts */
1338 next
= &m
->mp_parts
;
1339 for (bmp
--; bmp
>= base
; bmp
--) {
1342 next
= &part
->mp_next
;
1346 /* free array of pointers */
1347 free ((char *) base
);
1359 CI ci
= &ct
->c_ctinfo
;
1361 if ((ct
->c_encoding
!= CE_7BIT
) && (ct
->c_encoding
!= CE_8BIT
)) {
1363 "\"%s/%s\" type in message %s should be encoded in 7bit or 8bit",
1364 ci
->ci_type
, ci
->ci_subtype
, ct
->c_file
);
1368 /* check for missing subtype */
1369 if (!*ci
->ci_subtype
)
1370 ci
->ci_subtype
= add ("rfc822", ci
->ci_subtype
);
1373 for (kv
= SubMessage
; kv
->kv_key
; kv
++)
1374 if (!mh_strcasecmp (ci
->ci_subtype
, kv
->kv_key
))
1376 ct
->c_subtype
= kv
->kv_value
;
1378 switch (ct
->c_subtype
) {
1379 case MESSAGE_RFC822
:
1382 case MESSAGE_PARTIAL
:
1387 if ((p
= (struct partial
*) calloc (1, sizeof(*p
))) == NULL
)
1388 adios (NULL
, "out of memory");
1389 ct
->c_ctparams
= (void *) p
;
1391 /* scan for parameters "id", "number", and "total" */
1392 for (ap
= ci
->ci_attrs
, ep
= ci
->ci_values
; *ap
; ap
++, ep
++) {
1393 if (!mh_strcasecmp (*ap
, "id")) {
1394 p
->pm_partid
= add (*ep
, NULL
);
1397 if (!mh_strcasecmp (*ap
, "number")) {
1398 if (sscanf (*ep
, "%d", &p
->pm_partno
) != 1
1399 || p
->pm_partno
< 1) {
1402 "invalid %s parameter for \"%s/%s\" type in message %s's %s field",
1403 *ap
, ci
->ci_type
, ci
->ci_subtype
,
1404 ct
->c_file
, TYPE_FIELD
);
1409 if (!mh_strcasecmp (*ap
, "total")) {
1410 if (sscanf (*ep
, "%d", &p
->pm_maxno
) != 1
1419 || (p
->pm_maxno
&& p
->pm_partno
> p
->pm_maxno
)) {
1421 "invalid parameters for \"%s/%s\" type in message %s's %s field",
1422 ci
->ci_type
, ci
->ci_subtype
,
1423 ct
->c_file
, TYPE_FIELD
);
1429 case MESSAGE_EXTERNAL
:
1436 if ((e
= (struct exbody
*) calloc (1, sizeof(*e
))) == NULL
)
1437 adios (NULL
, "out of memory");
1438 ct
->c_ctparams
= (void *) e
;
1441 && (ct
->c_fp
= fopen (ct
->c_file
, "r")) == NULL
) {
1442 advise (ct
->c_file
, "unable to open for reading");
1446 fseek (fp
= ct
->c_fp
, ct
->c_begin
, SEEK_SET
);
1448 if (!(p
= get_content (fp
, ct
->c_file
, 0))) {
1456 if ((exresult
= params_external (ct
, 0)) != NOTOK
1457 && p
->c_ceopenfnx
== openMail
) {
1461 if ((size
= ct
->c_end
- p
->c_begin
) <= 0) {
1463 content_error (NULL
, ct
,
1464 "empty body for access-type=mail-server");
1468 e
->eb_body
= bp
= mh_xmalloc ((unsigned) size
);
1469 fseek (p
->c_fp
, p
->c_begin
, SEEK_SET
);
1471 switch (cc
= fread (bp
, sizeof(*bp
), size
, p
->c_fp
)) {
1473 adios ("failed", "fread");
1476 adios (NULL
, "unexpected EOF from fread");
1479 bp
+= cc
, size
-= cc
;
1486 p
->c_end
= p
->c_begin
;
1491 if (exresult
== NOTOK
)
1493 if (e
->eb_flags
== NOTOK
)
1496 switch (p
->c_type
) {
1501 if (p
->c_subtype
!= MESSAGE_RFC822
)
1505 e
->eb_partno
= ct
->c_partno
;
1507 (*p
->c_ctinitfnx
) (p
);
1522 params_external (CT ct
, int composing
)
1525 struct exbody
*e
= (struct exbody
*) ct
->c_ctparams
;
1526 CI ci
= &ct
->c_ctinfo
;
1528 for (ap
= ci
->ci_attrs
, ep
= ci
->ci_values
; *ap
; ap
++, ep
++) {
1529 if (!mh_strcasecmp (*ap
, "access-type")) {
1530 struct str2init
*s2i
;
1531 CT p
= e
->eb_content
;
1533 for (s2i
= str2methods
; s2i
->si_key
; s2i
++)
1534 if (!mh_strcasecmp (*ep
, s2i
->si_key
))
1538 e
->eb_flags
= NOTOK
;
1539 p
->c_encoding
= CE_EXTERNAL
;
1542 e
->eb_access
= s2i
->si_key
;
1543 e
->eb_flags
= s2i
->si_val
;
1544 p
->c_encoding
= CE_EXTERNAL
;
1546 /* Call the Init function for this external type */
1547 if ((*s2i
->si_init
)(p
) == NOTOK
)
1551 if (!mh_strcasecmp (*ap
, "name")) {
1555 if (!mh_strcasecmp (*ap
, "permission")) {
1556 e
->eb_permission
= *ep
;
1559 if (!mh_strcasecmp (*ap
, "site")) {
1563 if (!mh_strcasecmp (*ap
, "directory")) {
1567 if (!mh_strcasecmp (*ap
, "mode")) {
1571 if (!mh_strcasecmp (*ap
, "size")) {
1572 sscanf (*ep
, "%lu", &e
->eb_size
);
1575 if (!mh_strcasecmp (*ap
, "server")) {
1579 if (!mh_strcasecmp (*ap
, "subject")) {
1580 e
->eb_subject
= *ep
;
1583 if (composing
&& !mh_strcasecmp (*ap
, "body")) {
1584 e
->eb_body
= getcpy (*ep
);
1589 if (!e
->eb_access
) {
1591 "invalid parameters for \"%s/%s\" type in message %s's %s field",
1592 ci
->ci_type
, ci
->ci_subtype
, ct
->c_file
, TYPE_FIELD
);
1605 InitApplication (CT ct
)
1608 CI ci
= &ct
->c_ctinfo
;
1611 for (kv
= SubApplication
; kv
->kv_key
; kv
++)
1612 if (!mh_strcasecmp (ci
->ci_subtype
, kv
->kv_key
))
1614 ct
->c_subtype
= kv
->kv_value
;
1621 * TRANSFER ENCODINGS
1625 init_encoding (CT ct
, OpenCEFunc openfnx
)
1629 if ((ce
= (CE
) calloc (1, sizeof(*ce
))) == NULL
)
1630 adios (NULL
, "out of memory");
1633 ct
->c_ceopenfnx
= openfnx
;
1634 ct
->c_ceclosefnx
= close_encoding
;
1635 ct
->c_cesizefnx
= size_encoding
;
1642 close_encoding (CT ct
)
1646 if (!(ce
= ct
->c_cefile
))
1656 static unsigned long
1657 size_encoding (CT ct
)
1665 if (!(ce
= ct
->c_cefile
))
1666 return (ct
->c_end
- ct
->c_begin
);
1668 if (ce
->ce_fp
&& fstat (fileno (ce
->ce_fp
), &st
) != NOTOK
)
1669 return (long) st
.st_size
;
1672 if (stat (ce
->ce_file
, &st
) != NOTOK
)
1673 return (long) st
.st_size
;
1678 if (ct
->c_encoding
== CE_EXTERNAL
)
1679 return (ct
->c_end
- ct
->c_begin
);
1682 if ((fd
= (*ct
->c_ceopenfnx
) (ct
, &file
)) == NOTOK
)
1683 return (ct
->c_end
- ct
->c_begin
);
1685 if (fstat (fd
, &st
) != NOTOK
)
1686 size
= (long) st
.st_size
;
1690 (*ct
->c_ceclosefnx
) (ct
);
1699 static unsigned char b642nib
[0x80] = {
1700 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1701 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1702 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1703 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1704 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1705 0xff, 0xff, 0xff, 0x3e, 0xff, 0xff, 0xff, 0x3f,
1706 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b,
1707 0x3c, 0x3d, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1708 0xff, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
1709 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e,
1710 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16,
1711 0x17, 0x18, 0x19, 0xff, 0xff, 0xff, 0xff, 0xff,
1712 0xff, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20,
1713 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28,
1714 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30,
1715 0x31, 0x32, 0x33, 0xff, 0xff, 0xff, 0xff, 0xff
1722 return init_encoding (ct
, openBase64
);
1727 openBase64 (CT ct
, char **file
)
1729 int bitno
, cc
, digested
;
1730 int fd
, len
, skip
, own_ct_fp
= 0;
1732 unsigned char value
, b
;
1733 unsigned char *cp
, *ep
;
1734 char buffer
[BUFSIZ
];
1735 /* sbeck -- handle suffixes */
1742 fseek (ce
->ce_fp
, 0L, SEEK_SET
);
1747 if ((ce
->ce_fp
= fopen (ce
->ce_file
, "r")) == NULL
) {
1748 content_error (ce
->ce_file
, ct
, "unable to fopen for reading");
1754 if (*file
== NULL
) {
1755 ce
->ce_file
= add (m_mktemp(tmp
, NULL
, NULL
), NULL
);
1758 ce
->ce_file
= add (*file
, NULL
);
1762 /* sbeck@cise.ufl.edu -- handle suffixes */
1764 snprintf (buffer
, sizeof(buffer
), "%s-suffix-%s/%s",
1765 invo_name
, ci
->ci_type
, ci
->ci_subtype
);
1766 cp
= context_find (buffer
);
1767 if (cp
== NULL
|| *cp
== '\0') {
1768 snprintf (buffer
, sizeof(buffer
), "%s-suffix-%s", invo_name
,
1770 cp
= context_find (buffer
);
1772 if (cp
!= NULL
&& *cp
!= '\0') {
1773 if (ce
->ce_unlink
) {
1774 /* Temporary file already exists, so we rename to
1775 version with extension. */
1776 char *file_org
= strdup(ce
->ce_file
);
1777 ce
->ce_file
= add (cp
, ce
->ce_file
);
1778 if (rename(file_org
, ce
->ce_file
)) {
1779 adios (ce
->ce_file
, "unable to rename %s to ", file_org
);
1784 ce
->ce_file
= add (cp
, ce
->ce_file
);
1788 if ((ce
->ce_fp
= fopen (ce
->ce_file
, "w+")) == NULL
) {
1789 content_error (ce
->ce_file
, ct
, "unable to fopen for reading/writing");
1793 if ((len
= ct
->c_end
- ct
->c_begin
) < 0)
1794 adios (NULL
, "internal error(1)");
1797 if ((ct
->c_fp
= fopen (ct
->c_file
, "r")) == NULL
) {
1798 content_error (ct
->c_file
, ct
, "unable to open for reading");
1804 if ((digested
= ct
->c_digested
))
1805 MD5Init (&mdContext
);
1811 lseek (fd
= fileno (ct
->c_fp
), (off_t
) ct
->c_begin
, SEEK_SET
);
1813 switch (cc
= read (fd
, buffer
, sizeof(buffer
) - 1)) {
1815 content_error (ct
->c_file
, ct
, "error reading from");
1819 content_error (NULL
, ct
, "premature eof");
1827 for (ep
= (cp
= buffer
) + cc
; cp
< ep
; cp
++) {
1832 if (skip
|| (*cp
& 0x80)
1833 || (value
= b642nib
[*cp
& 0x7f]) > 0x3f) {
1835 fprintf (stderr
, "*cp=0x%x pos=%ld skip=%d\n",
1837 (long) (lseek (fd
, (off_t
) 0, SEEK_CUR
) - (ep
- cp
)),
1840 content_error (NULL
, ct
,
1841 "invalid BASE64 encoding -- continuing");
1845 bits
|= value
<< bitno
;
1847 if ((bitno
-= 6) < 0) {
1848 b
= (bits
>> 16) & 0xff;
1849 putc ((char) b
, ce
->ce_fp
);
1851 MD5Update (&mdContext
, &b
, 1);
1853 b
= (bits
>> 8) & 0xff;
1854 putc ((char) b
, ce
->ce_fp
);
1856 MD5Update (&mdContext
, &b
, 1);
1859 putc ((char) b
, ce
->ce_fp
);
1861 MD5Update (&mdContext
, &b
, 1);
1865 if (ferror (ce
->ce_fp
)) {
1866 content_error (ce
->ce_file
, ct
,
1867 "error writing to");
1870 bitno
= 18, bits
= 0L, skip
= 0;
1876 goto self_delimiting
;
1885 fprintf (stderr
, "premature ending (bitno %d)\n", bitno
);
1887 content_error (NULL
, ct
, "invalid BASE64 encoding");
1892 fseek (ct
->c_fp
, 0L, SEEK_SET
);
1894 if (fflush (ce
->ce_fp
)) {
1895 content_error (ce
->ce_file
, ct
, "error writing to");
1900 unsigned char digest
[16];
1902 MD5Final (digest
, &mdContext
);
1903 if (memcmp((char *) digest
, (char *) ct
->c_digest
,
1904 sizeof(digest
) / sizeof(digest
[0])))
1905 content_error (NULL
, ct
,
1906 "content integrity suspect (digest mismatch) -- continuing");
1909 fprintf (stderr
, "content integrity confirmed\n");
1912 fseek (ce
->ce_fp
, 0L, SEEK_SET
);
1915 *file
= ce
->ce_file
;
1920 return fileno (ce
->ce_fp
);
1927 free_encoding (ct
, 0);
1936 static char hex2nib
[0x80] = {
1937 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1938 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1939 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1940 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1941 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1942 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1943 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
1944 0x08, 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1945 0x00, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x00,
1946 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1947 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1948 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1949 0x00, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x00,
1950 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1951 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1952 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
1959 return init_encoding (ct
, openQuoted
);
1964 openQuoted (CT ct
, char **file
)
1966 int cc
, digested
, len
, quoted
, own_ct_fp
= 0;
1967 unsigned char *cp
, *ep
;
1968 char buffer
[BUFSIZ
];
1971 /* sbeck -- handle suffixes */
1977 fseek (ce
->ce_fp
, 0L, SEEK_SET
);
1982 if ((ce
->ce_fp
= fopen (ce
->ce_file
, "r")) == NULL
) {
1983 content_error (ce
->ce_file
, ct
, "unable to fopen for reading");
1989 if (*file
== NULL
) {
1990 ce
->ce_file
= add (m_mktemp(tmp
, NULL
, NULL
), NULL
);
1993 ce
->ce_file
= add (*file
, NULL
);
1997 /* sbeck@cise.ufl.edu -- handle suffixes */
1999 snprintf (buffer
, sizeof(buffer
), "%s-suffix-%s/%s",
2000 invo_name
, ci
->ci_type
, ci
->ci_subtype
);
2001 cp
= context_find (buffer
);
2002 if (cp
== NULL
|| *cp
== '\0') {
2003 snprintf (buffer
, sizeof(buffer
), "%s-suffix-%s", invo_name
,
2005 cp
= context_find (buffer
);
2007 if (cp
!= NULL
&& *cp
!= '\0') {
2008 if (ce
->ce_unlink
) {
2009 /* Temporary file already exists, so we rename to
2010 version with extension. */
2011 char *file_org
= strdup(ce
->ce_file
);
2012 ce
->ce_file
= add (cp
, ce
->ce_file
);
2013 if (rename(file_org
, ce
->ce_file
)) {
2014 adios (ce
->ce_file
, "unable to rename %s to ", file_org
);
2019 ce
->ce_file
= add (cp
, ce
->ce_file
);
2023 if ((ce
->ce_fp
= fopen (ce
->ce_file
, "w+")) == NULL
) {
2024 content_error (ce
->ce_file
, ct
, "unable to fopen for reading/writing");
2028 if ((len
= ct
->c_end
- ct
->c_begin
) < 0)
2029 adios (NULL
, "internal error(2)");
2032 if ((ct
->c_fp
= fopen (ct
->c_file
, "r")) == NULL
) {
2033 content_error (ct
->c_file
, ct
, "unable to open for reading");
2039 if ((digested
= ct
->c_digested
))
2040 MD5Init (&mdContext
);
2047 fseek (ct
->c_fp
, ct
->c_begin
, SEEK_SET
);
2049 if (fgets (buffer
, sizeof(buffer
) - 1, ct
->c_fp
) == NULL
) {
2050 content_error (NULL
, ct
, "premature eof");
2054 if ((cc
= strlen (buffer
)) > len
)
2058 for (ep
= (cp
= buffer
) + cc
- 1; cp
<= ep
; ep
--)
2063 for (; cp
< ep
; cp
++) {
2065 /* in an escape sequence */
2067 /* at byte 1 of an escape sequence */
2068 mask
= hex2nib
[*cp
& 0x7f];
2069 /* next is byte 2 */
2072 /* at byte 2 of an escape sequence */
2074 mask
|= hex2nib
[*cp
& 0x7f];
2075 putc (mask
, ce
->ce_fp
);
2077 MD5Update (&mdContext
, &mask
, 1);
2078 if (ferror (ce
->ce_fp
)) {
2079 content_error (ce
->ce_file
, ct
, "error writing to");
2082 /* finished escape sequence; next may be literal or a new
2083 * escape sequence */
2086 /* on to next byte */
2090 /* not in an escape sequence */
2092 /* starting an escape sequence, or invalid '='? */
2093 if (cp
+ 1 < ep
&& cp
[1] == '\n') {
2094 /* "=\n" soft line break, eat the \n */
2098 if (cp
+ 1 >= ep
|| cp
+ 2 >= ep
) {
2099 /* We don't have 2 bytes left, so this is an invalid
2100 * escape sequence; just show the raw bytes (below). */
2101 } else if (isxdigit (cp
[1]) && isxdigit (cp
[2])) {
2102 /* Next 2 bytes are hex digits, making this a valid escape
2103 * sequence; let's decode it (above). */
2107 /* One or both of the next 2 is out of range, making this
2108 * an invalid escape sequence; just show the raw bytes
2113 /* Just show the raw byte. */
2114 putc (*cp
, ce
->ce_fp
);
2117 MD5Update (&mdContext
, (unsigned char *) "\r\n",2);
2119 MD5Update (&mdContext
, (unsigned char *) cp
, 1);
2122 if (ferror (ce
->ce_fp
)) {
2123 content_error (ce
->ce_file
, ct
, "error writing to");
2129 content_error (NULL
, ct
,
2130 "invalid QUOTED-PRINTABLE encoding -- end-of-content while still quoting");
2134 fseek (ct
->c_fp
, 0L, SEEK_SET
);
2136 if (fflush (ce
->ce_fp
)) {
2137 content_error (ce
->ce_file
, ct
, "error writing to");
2142 unsigned char digest
[16];
2144 MD5Final (digest
, &mdContext
);
2145 if (memcmp((char *) digest
, (char *) ct
->c_digest
,
2146 sizeof(digest
) / sizeof(digest
[0])))
2147 content_error (NULL
, ct
,
2148 "content integrity suspect (digest mismatch) -- continuing");
2151 fprintf (stderr
, "content integrity confirmed\n");
2154 fseek (ce
->ce_fp
, 0L, SEEK_SET
);
2157 *file
= ce
->ce_file
;
2162 return fileno (ce
->ce_fp
);
2165 free_encoding (ct
, 0);
2181 if (init_encoding (ct
, open7Bit
) == NOTOK
)
2184 ct
->c_cesizefnx
= NULL
; /* no need to decode for real size */
2190 open7Bit (CT ct
, char **file
)
2192 int cc
, fd
, len
, own_ct_fp
= 0;
2193 char buffer
[BUFSIZ
];
2194 /* sbeck -- handle suffixes */
2201 fseek (ce
->ce_fp
, 0L, SEEK_SET
);
2206 if ((ce
->ce_fp
= fopen (ce
->ce_file
, "r")) == NULL
) {
2207 content_error (ce
->ce_file
, ct
, "unable to fopen for reading");
2213 if (*file
== NULL
) {
2214 ce
->ce_file
= add (m_mktemp(tmp
, NULL
, NULL
), NULL
);
2217 ce
->ce_file
= add (*file
, NULL
);
2221 /* sbeck@cise.ufl.edu -- handle suffixes */
2223 snprintf (buffer
, sizeof(buffer
), "%s-suffix-%s/%s",
2224 invo_name
, ci
->ci_type
, ci
->ci_subtype
);
2225 cp
= context_find (buffer
);
2226 if (cp
== NULL
|| *cp
== '\0') {
2227 snprintf (buffer
, sizeof(buffer
), "%s-suffix-%s", invo_name
,
2229 cp
= context_find (buffer
);
2231 if (cp
!= NULL
&& *cp
!= '\0') {
2232 if (ce
->ce_unlink
) {
2233 /* Temporary file already exists, so we rename to
2234 version with extension. */
2235 char *file_org
= strdup(ce
->ce_file
);
2236 ce
->ce_file
= add (cp
, ce
->ce_file
);
2237 if (rename(file_org
, ce
->ce_file
)) {
2238 adios (ce
->ce_file
, "unable to rename %s to ", file_org
);
2243 ce
->ce_file
= add (cp
, ce
->ce_file
);
2247 if ((ce
->ce_fp
= fopen (ce
->ce_file
, "w+")) == NULL
) {
2248 content_error (ce
->ce_file
, ct
, "unable to fopen for reading/writing");
2252 if (ct
->c_type
== CT_MULTIPART
) {
2254 CI ci
= &ct
->c_ctinfo
;
2257 fprintf (ce
->ce_fp
, "%s: %s/%s", TYPE_FIELD
, ci
->ci_type
, ci
->ci_subtype
);
2258 len
+= strlen (TYPE_FIELD
) + 2 + strlen (ci
->ci_type
)
2259 + 1 + strlen (ci
->ci_subtype
);
2260 for (ap
= ci
->ci_attrs
, ep
= ci
->ci_values
; *ap
; ap
++, ep
++) {
2261 putc (';', ce
->ce_fp
);
2264 snprintf (buffer
, sizeof(buffer
), "%s=\"%s\"", *ap
, *ep
);
2266 if (len
+ 1 + (cc
= strlen (buffer
)) >= CPERLIN
) {
2267 fputs ("\n\t", ce
->ce_fp
);
2270 putc (' ', ce
->ce_fp
);
2273 fprintf (ce
->ce_fp
, "%s", buffer
);
2277 if (ci
->ci_comment
) {
2278 if (len
+ 1 + (cc
= 2 + strlen (ci
->ci_comment
)) >= CPERLIN
) {
2279 fputs ("\n\t", ce
->ce_fp
);
2283 putc (' ', ce
->ce_fp
);
2286 fprintf (ce
->ce_fp
, "(%s)", ci
->ci_comment
);
2289 fprintf (ce
->ce_fp
, "\n");
2291 fprintf (ce
->ce_fp
, "%s:%s", ID_FIELD
, ct
->c_id
);
2293 fprintf (ce
->ce_fp
, "%s:%s", DESCR_FIELD
, ct
->c_descr
);
2295 fprintf (ce
->ce_fp
, "%s:%s", DISPO_FIELD
, ct
->c_dispo
);
2296 fprintf (ce
->ce_fp
, "\n");
2299 if ((len
= ct
->c_end
- ct
->c_begin
) < 0)
2300 adios (NULL
, "internal error(3)");
2303 if ((ct
->c_fp
= fopen (ct
->c_file
, "r")) == NULL
) {
2304 content_error (ct
->c_file
, ct
, "unable to open for reading");
2310 lseek (fd
= fileno (ct
->c_fp
), (off_t
) ct
->c_begin
, SEEK_SET
);
2312 switch (cc
= read (fd
, buffer
, sizeof(buffer
) - 1)) {
2314 content_error (ct
->c_file
, ct
, "error reading from");
2318 content_error (NULL
, ct
, "premature eof");
2326 fwrite (buffer
, sizeof(*buffer
), cc
, ce
->ce_fp
);
2327 if (ferror (ce
->ce_fp
)) {
2328 content_error (ce
->ce_file
, ct
, "error writing to");
2333 fseek (ct
->c_fp
, 0L, SEEK_SET
);
2335 if (fflush (ce
->ce_fp
)) {
2336 content_error (ce
->ce_file
, ct
, "error writing to");
2340 fseek (ce
->ce_fp
, 0L, SEEK_SET
);
2343 *file
= ce
->ce_file
;
2348 return fileno (ce
->ce_fp
);
2351 free_encoding (ct
, 0);
2365 openExternal (CT ct
, CT cb
, CE ce
, char **file
, int *fd
)
2367 char cachefile
[BUFSIZ
];
2370 fseek (ce
->ce_fp
, 0L, SEEK_SET
);
2375 if ((ce
->ce_fp
= fopen (ce
->ce_file
, "r")) == NULL
) {
2376 content_error (ce
->ce_file
, ct
, "unable to fopen for reading");
2382 if (find_cache (ct
, rcachesw
, (int *) 0, cb
->c_id
,
2383 cachefile
, sizeof(cachefile
)) != NOTOK
) {
2384 if ((ce
->ce_fp
= fopen (cachefile
, "r"))) {
2385 ce
->ce_file
= getcpy (cachefile
);
2389 admonish (cachefile
, "unable to fopen for reading");
2396 *file
= ce
->ce_file
;
2397 *fd
= fileno (ce
->ce_fp
);
2408 return init_encoding (ct
, openFile
);
2413 openFile (CT ct
, char **file
)
2416 char cachefile
[BUFSIZ
];
2417 struct exbody
*e
= ct
->c_ctexbody
;
2418 CE ce
= ct
->c_cefile
;
2420 switch (openExternal (e
->eb_parent
, e
->eb_content
, ce
, file
, &fd
)) {
2432 content_error (NULL
, ct
, "missing name parameter");
2436 ce
->ce_file
= getcpy (e
->eb_name
);
2439 if ((ce
->ce_fp
= fopen (ce
->ce_file
, "r")) == NULL
) {
2440 content_error (ce
->ce_file
, ct
, "unable to fopen for reading");
2444 if ((!e
->eb_permission
|| mh_strcasecmp (e
->eb_permission
, "read-write"))
2445 && find_cache (NULL
, wcachesw
, &cachetype
, e
->eb_content
->c_id
,
2446 cachefile
, sizeof(cachefile
)) != NOTOK
) {
2450 mask
= umask (cachetype
? ~m_gmprot () : 0222);
2451 if ((fp
= fopen (cachefile
, "w"))) {
2453 char buffer
[BUFSIZ
];
2454 FILE *gp
= ce
->ce_fp
;
2456 fseek (gp
, 0L, SEEK_SET
);
2458 while ((cc
= fread (buffer
, sizeof(*buffer
), sizeof(buffer
), gp
))
2460 fwrite (buffer
, sizeof(*buffer
), cc
, fp
);
2464 admonish (ce
->ce_file
, "error reading");
2469 admonish (cachefile
, "error writing");
2477 fseek (ce
->ce_fp
, 0L, SEEK_SET
);
2478 *file
= ce
->ce_file
;
2479 return fileno (ce
->ce_fp
);
2489 return init_encoding (ct
, openFTP
);
2494 openFTP (CT ct
, char **file
)
2496 int cachetype
, caching
, fd
;
2498 char *bp
, *ftp
, *user
, *pass
;
2499 char buffer
[BUFSIZ
], cachefile
[BUFSIZ
];
2502 static char *username
= NULL
;
2503 static char *password
= NULL
;
2508 if ((ftp
= context_find (nmhaccessftp
)) && !*ftp
)
2514 switch (openExternal (e
->eb_parent
, e
->eb_content
, ce
, file
, &fd
)) {
2525 if (!e
->eb_name
|| !e
->eb_site
) {
2526 content_error (NULL
, ct
, "missing %s parameter",
2527 e
->eb_name
? "site": "name");
2534 pidcheck (pidwait (xpid
, NOTOK
));
2538 /* Get the buffer ready to go */
2540 buflen
= sizeof(buffer
);
2543 * Construct the query message for user
2545 snprintf (bp
, buflen
, "Retrieve %s", e
->eb_name
);
2551 snprintf (bp
, buflen
, " (content %s)", e
->eb_partno
);
2557 snprintf (bp
, buflen
, "\n using %sFTP from site %s",
2558 e
->eb_flags
? "anonymous " : "", e
->eb_site
);
2563 if (e
->eb_size
> 0) {
2564 snprintf (bp
, buflen
, " (%lu octets)", e
->eb_size
);
2569 snprintf (bp
, buflen
, "? ");
2572 * Now, check the answer
2574 if (!getanswer (buffer
))
2579 snprintf (buffer
, sizeof(buffer
), "%s@%s", getusername (),
2583 ruserpass (e
->eb_site
, &username
, &password
);
2588 ce
->ce_unlink
= (*file
== NULL
);
2590 cachefile
[0] = '\0';
2591 if ((!e
->eb_permission
|| mh_strcasecmp (e
->eb_permission
, "read-write"))
2592 && find_cache (NULL
, wcachesw
, &cachetype
, e
->eb_content
->c_id
,
2593 cachefile
, sizeof(cachefile
)) != NOTOK
) {
2594 if (*file
== NULL
) {
2601 ce
->ce_file
= add (*file
, NULL
);
2603 ce
->ce_file
= add (cachefile
, NULL
);
2605 ce
->ce_file
= add (m_mktemp(tmp
, NULL
, NULL
), NULL
);
2607 if ((ce
->ce_fp
= fopen (ce
->ce_file
, "w+")) == NULL
) {
2608 content_error (ce
->ce_file
, ct
, "unable to fopen for reading/writing");
2613 int child_id
, i
, vecp
;
2617 vec
[vecp
++] = r1bindex (ftp
, '/');
2618 vec
[vecp
++] = e
->eb_site
;
2621 vec
[vecp
++] = e
->eb_dir
;
2622 vec
[vecp
++] = e
->eb_name
;
2623 vec
[vecp
++] = ce
->ce_file
,
2624 vec
[vecp
++] = e
->eb_mode
&& !mh_strcasecmp (e
->eb_mode
, "ascii")
2625 ? "ascii" : "binary";
2630 for (i
= 0; (child_id
= vfork()) == NOTOK
&& i
< 5; i
++)
2634 adios ("fork", "unable to");
2638 close (fileno (ce
->ce_fp
));
2640 fprintf (stderr
, "unable to exec ");
2646 if (pidXwait (child_id
, NULL
)) {
2647 username
= password
= NULL
;
2657 chmod (cachefile
, cachetype
? m_gmprot () : 0444);
2662 mask
= umask (cachetype
? ~m_gmprot () : 0222);
2663 if ((fp
= fopen (cachefile
, "w"))) {
2665 FILE *gp
= ce
->ce_fp
;
2667 fseek (gp
, 0L, SEEK_SET
);
2669 while ((cc
= fread (buffer
, sizeof(*buffer
), sizeof(buffer
), gp
))
2671 fwrite (buffer
, sizeof(*buffer
), cc
, fp
);
2675 admonish (ce
->ce_file
, "error reading");
2680 admonish (cachefile
, "error writing");
2689 fseek (ce
->ce_fp
, 0L, SEEK_SET
);
2690 *file
= ce
->ce_file
;
2691 return fileno (ce
->ce_fp
);
2702 return init_encoding (ct
, openMail
);
2707 openMail (CT ct
, char **file
)
2709 int child_id
, fd
, i
, vecp
;
2711 char *bp
, buffer
[BUFSIZ
], *vec
[7];
2712 struct exbody
*e
= ct
->c_ctexbody
;
2713 CE ce
= ct
->c_cefile
;
2715 switch (openExternal (e
->eb_parent
, e
->eb_content
, ce
, file
, &fd
)) {
2726 if (!e
->eb_server
) {
2727 content_error (NULL
, ct
, "missing server parameter");
2734 pidcheck (pidwait (xpid
, NOTOK
));
2738 /* Get buffer ready to go */
2740 buflen
= sizeof(buffer
);
2742 /* Now, construct query message */
2743 snprintf (bp
, buflen
, "Retrieve content");
2749 snprintf (bp
, buflen
, " %s", e
->eb_partno
);
2755 snprintf (bp
, buflen
, " by asking %s\n\n%s\n? ",
2757 e
->eb_subject
? e
->eb_subject
: e
->eb_body
);
2759 /* Now, check answer */
2760 if (!getanswer (buffer
))
2764 vec
[vecp
++] = r1bindex (mailproc
, '/');
2765 vec
[vecp
++] = e
->eb_server
;
2766 vec
[vecp
++] = "-subject";
2767 vec
[vecp
++] = e
->eb_subject
? e
->eb_subject
: "mail-server request";
2768 vec
[vecp
++] = "-body";
2769 vec
[vecp
++] = e
->eb_body
;
2772 for (i
= 0; (child_id
= vfork()) == NOTOK
&& i
< 5; i
++)
2776 advise ("fork", "unable to");
2780 execvp (mailproc
, vec
);
2781 fprintf (stderr
, "unable to exec ");
2787 if (pidXwait (child_id
, NULL
) == OK
)
2788 advise (NULL
, "request sent");
2792 if (*file
== NULL
) {
2793 ce
->ce_file
= add (m_mktemp(tmp
, NULL
, NULL
), NULL
);
2796 ce
->ce_file
= add (*file
, NULL
);
2800 if ((ce
->ce_fp
= fopen (ce
->ce_file
, "w+")) == NULL
) {
2801 content_error (ce
->ce_file
, ct
, "unable to fopen for reading/writing");
2805 /* showproc is for mhshow and mhstore, though mhlist -debug
2806 * prints it, too. */
2808 free (ct
->c_showproc
);
2809 ct
->c_showproc
= add ("true", NULL
);
2811 fseek (ce
->ce_fp
, 0L, SEEK_SET
);
2812 *file
= ce
->ce_file
;
2813 return fileno (ce
->ce_fp
);
2818 readDigest (CT ct
, char *cp
)
2823 unsigned char *dp
, value
, *ep
;
2829 for (ep
= (dp
= ct
->c_digest
)
2830 + sizeof(ct
->c_digest
) / sizeof(ct
->c_digest
[0]); *cp
; cp
++)
2835 || (value
= b642nib
[*cp
& 0x7f]) > 0x3f) {
2837 fprintf (stderr
, "invalid BASE64 encoding\n");
2841 bits
|= value
<< bitno
;
2843 if ((bitno
-= 6) < 0) {
2844 if (dp
+ (3 - skip
) > ep
)
2845 goto invalid_digest
;
2846 *dp
++ = (bits
>> 16) & 0xff;
2848 *dp
++ = (bits
>> 8) & 0xff;
2850 *dp
++ = bits
& 0xff;
2860 goto self_delimiting
;
2865 fprintf (stderr
, "premature ending (bitno %d)\n", bitno
);
2875 fprintf (stderr
, "invalid MD5 digest (got %d octets)\n",
2883 fprintf (stderr
, "MD5 digest=");
2884 for (dp
= ct
->c_digest
; dp
< ep
; dp
++)
2885 fprintf (stderr
, "%02x", *dp
& 0xff);
2886 fprintf (stderr
, "\n");