]> diplodocus.org Git - nmh/blob - uip/mhparse.c
58f3a81c7b5d0cefa435ba0ca4178e59ebde288b
[nmh] / uip / mhparse.c
1
2 /*
3 * mhparse.c -- routines to parse the contents of MIME messages
4 *
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.
8 */
9
10 #include <h/mh.h>
11 #include <fcntl.h>
12 #include <h/signals.h>
13 #include <h/md5.h>
14 #include <h/mts.h>
15 #include <h/tws.h>
16 #include <h/mime.h>
17 #include <h/mhparse.h>
18 #include <h/utils.h>
19
20
21 extern int debugsw;
22
23 extern pid_t xpid; /* mhshowsbr.c */
24
25 /* cache policies */
26 extern int rcachesw; /* mhcachesbr.c */
27 extern int wcachesw; /* mhcachesbr.c */
28
29 int checksw = 0; /* check Content-MD5 field */
30
31 /*
32 * These are for mhfixmsg to:
33 * 1) Instruct parser not to detect invalid Content-Transfer-Encoding
34 * in a multipart.
35 * 2) Suppress the warning about bogus multipart content, and report it.
36 */
37 int skip_mp_cte_check;
38 int suppress_bogus_mp_content_warning;
39 int bogus_mp_content;
40
41 /*
42 * Structures for TEXT messages
43 */
44 struct k2v SubText[] = {
45 { "plain", TEXT_PLAIN },
46 { "richtext", TEXT_RICHTEXT }, /* defined in RFC-1341 */
47 { "enriched", TEXT_ENRICHED }, /* defined in RFC-1896 */
48 { NULL, TEXT_UNKNOWN } /* this one must be last! */
49 };
50
51 /* Charset[] removed -- yozo. Mon Oct 8 01:03:41 JST 2012 */
52
53 /*
54 * Structures for MULTIPART messages
55 */
56 struct k2v SubMultiPart[] = {
57 { "mixed", MULTI_MIXED },
58 { "alternative", MULTI_ALTERNATE },
59 { "digest", MULTI_DIGEST },
60 { "parallel", MULTI_PARALLEL },
61 { NULL, MULTI_UNKNOWN } /* this one must be last! */
62 };
63
64 /*
65 * Structures for MESSAGE messages
66 */
67 struct k2v SubMessage[] = {
68 { "rfc822", MESSAGE_RFC822 },
69 { "partial", MESSAGE_PARTIAL },
70 { "external-body", MESSAGE_EXTERNAL },
71 { NULL, MESSAGE_UNKNOWN } /* this one must be last! */
72 };
73
74 /*
75 * Structure for APPLICATION messages
76 */
77 struct k2v SubApplication[] = {
78 { "octet-stream", APPLICATION_OCTETS },
79 { "postscript", APPLICATION_POSTSCRIPT },
80 { NULL, APPLICATION_UNKNOWN } /* this one must be last! */
81 };
82
83
84 /* mhcachesbr.c */
85 int find_cache (CT, int, int *, char *, char *, int);
86
87 /* mhmisc.c */
88 int part_ok (CT, int);
89 int type_ok (CT, int);
90 void content_error (char *, CT, char *, ...);
91
92 /* mhfree.c */
93 void free_encoding (CT, int);
94
95 /*
96 * static prototypes
97 */
98 static CT get_content (FILE *, char *, int);
99 static int get_comment (const char *, CI, char **, int);
100
101 static int InitGeneric (CT);
102 static int InitText (CT);
103 static int InitMultiPart (CT);
104 void reverse_parts (CT);
105 static int InitMessage (CT);
106 static int InitApplication (CT);
107 static int init_encoding (CT, OpenCEFunc);
108 static unsigned long size_encoding (CT);
109 static int InitBase64 (CT);
110 static int openBase64 (CT, char **);
111 static int InitQuoted (CT);
112 static int openQuoted (CT, char **);
113 static int Init7Bit (CT);
114 static int openExternal (CT, CT, CE, char **, int *);
115 static int InitFile (CT);
116 static int openFile (CT, char **);
117 static int InitFTP (CT);
118 static int openFTP (CT, char **);
119 static int InitMail (CT);
120 static int openMail (CT, char **);
121 static int readDigest (CT, char *);
122 static int get_leftover_mp_content (CT, int);
123 static int InitURL (CT);
124 static int openURL (CT, char **);
125
126 struct str2init str2cts[] = {
127 { "application", CT_APPLICATION, InitApplication },
128 { "audio", CT_AUDIO, InitGeneric },
129 { "image", CT_IMAGE, InitGeneric },
130 { "message", CT_MESSAGE, InitMessage },
131 { "multipart", CT_MULTIPART, InitMultiPart },
132 { "text", CT_TEXT, InitText },
133 { "video", CT_VIDEO, InitGeneric },
134 { NULL, CT_EXTENSION, NULL }, /* these two must be last! */
135 { NULL, CT_UNKNOWN, NULL },
136 };
137
138 struct str2init str2ces[] = {
139 { "base64", CE_BASE64, InitBase64 },
140 { "quoted-printable", CE_QUOTED, InitQuoted },
141 { "8bit", CE_8BIT, Init7Bit },
142 { "7bit", CE_7BIT, Init7Bit },
143 { "binary", CE_BINARY, Init7Bit },
144 { NULL, CE_EXTENSION, NULL }, /* these two must be last! */
145 { NULL, CE_UNKNOWN, NULL },
146 };
147
148 /*
149 * NOTE WELL: si_key MUST NOT have value of NOTOK
150 *
151 * si_key is 1 if access method is anonymous.
152 */
153 struct str2init str2methods[] = {
154 { "afs", 1, InitFile },
155 { "anon-ftp", 1, InitFTP },
156 { "ftp", 0, InitFTP },
157 { "local-file", 0, InitFile },
158 { "mail-server", 0, InitMail },
159 { "url", 0, InitURL },
160 { NULL, 0, NULL }
161 };
162
163
164 int
165 pidcheck (int status)
166 {
167 if ((status & 0xff00) == 0xff00 || (status & 0x007f) != SIGQUIT)
168 return status;
169
170 fflush (stdout);
171 fflush (stderr);
172 done (1);
173 return 1;
174 }
175
176
177 /*
178 * Main entry point for parsing a MIME message or file.
179 * It returns the Content structure for the top level
180 * entity in the file.
181 */
182
183 CT
184 parse_mime (char *file)
185 {
186 int is_stdin;
187 char buffer[BUFSIZ];
188 FILE *fp;
189 CT ct;
190
191 /*
192 * Check if file is actually standard input
193 */
194 if ((is_stdin = !(strcmp (file, "-")))) {
195 char *tfile = m_mktemp2(NULL, invo_name, NULL, &fp);
196 if (tfile == NULL) {
197 advise("mhparse", "unable to create temporary file in %s",
198 get_temp_dir());
199 return NULL;
200 }
201 file = add (tfile, NULL);
202
203 while (fgets (buffer, sizeof(buffer), stdin))
204 fputs (buffer, fp);
205 fflush (fp);
206
207 if (ferror (stdin)) {
208 unlink (file);
209 advise ("stdin", "error reading");
210 return NULL;
211 }
212 if (ferror (fp)) {
213 unlink (file);
214 advise (file, "error writing");
215 return NULL;
216 }
217 fseek (fp, 0L, SEEK_SET);
218 } else if ((fp = fopen (file, "r")) == NULL) {
219 advise (file, "unable to read");
220 return NULL;
221 }
222
223 if (!(ct = get_content (fp, file, 1))) {
224 if (is_stdin)
225 unlink (file);
226 advise (NULL, "unable to decode %s", file);
227 return NULL;
228 }
229
230 if (is_stdin)
231 ct->c_unlink = 1; /* temp file to remove */
232
233 ct->c_fp = NULL;
234
235 if (ct->c_end == 0L) {
236 fseek (fp, 0L, SEEK_END);
237 ct->c_end = ftell (fp);
238 }
239
240 if (ct->c_ctinitfnx && (*ct->c_ctinitfnx) (ct) == NOTOK) {
241 fclose (fp);
242 free_content (ct);
243 return NULL;
244 }
245
246 fclose (fp);
247 return ct;
248 }
249
250
251 /*
252 * Main routine for reading/parsing the headers
253 * of a message content.
254 *
255 * toplevel = 1 # we are at the top level of the message
256 * toplevel = 0 # we are inside message type or multipart type
257 * # other than multipart/digest
258 * toplevel = -1 # we are inside multipart/digest
259 * NB: on failure we will fclose(in)!
260 */
261
262 static CT
263 get_content (FILE *in, char *file, int toplevel)
264 {
265 int compnum, state;
266 char buf[BUFSIZ], name[NAMESZ];
267 char *np, *vp;
268 CT ct;
269 HF hp;
270 m_getfld_state_t gstate = 0;
271
272 /* allocate the content structure */
273 if (!(ct = (CT) calloc (1, sizeof(*ct))))
274 adios (NULL, "out of memory");
275
276 ct->c_fp = in;
277 ct->c_file = add (file, NULL);
278 ct->c_begin = ftell (ct->c_fp) + 1;
279
280 /*
281 * Parse the header fields for this
282 * content into a linked list.
283 */
284 m_getfld_track_filepos (&gstate, in);
285 for (compnum = 1;;) {
286 int bufsz = sizeof buf;
287 switch (state = m_getfld (&gstate, name, buf, &bufsz, in)) {
288 case FLD:
289 case FLDPLUS:
290 compnum++;
291
292 /* get copies of the buffers */
293 np = add (name, NULL);
294 vp = add (buf, NULL);
295
296 /* if necessary, get rest of field */
297 while (state == FLDPLUS) {
298 bufsz = sizeof buf;
299 state = m_getfld (&gstate, name, buf, &bufsz, in);
300 vp = add (buf, vp); /* add to previous value */
301 }
302
303 /* Now add the header data to the list */
304 add_header (ct, np, vp);
305
306 /* continue, to see if this isn't the last header field */
307 ct->c_begin = ftell (in) + 1;
308 continue;
309
310 case BODY:
311 ct->c_begin = ftell (in) - strlen (buf);
312 break;
313
314 case FILEEOF:
315 ct->c_begin = ftell (in);
316 break;
317
318 case LENERR:
319 case FMTERR:
320 adios (NULL, "message format error in component #%d", compnum);
321
322 default:
323 adios (NULL, "getfld() returned %d", state);
324 }
325
326 /* break out of the loop */
327 break;
328 }
329 m_getfld_state_destroy (&gstate);
330
331 /*
332 * Read the content headers. We will parse the
333 * MIME related header fields into their various
334 * structures and set internal flags related to
335 * content type/subtype, etc.
336 */
337
338 hp = ct->c_first_hf; /* start at first header field */
339 while (hp) {
340 /* Get MIME-Version field */
341 if (!strcasecmp (hp->name, VRSN_FIELD)) {
342 int ucmp;
343 char c, *cp, *dp;
344
345 if (ct->c_vrsn) {
346 advise (NULL, "message %s has multiple %s: fields",
347 ct->c_file, VRSN_FIELD);
348 goto next_header;
349 }
350 ct->c_vrsn = add (hp->value, NULL);
351
352 /* Now, cleanup this field */
353 cp = ct->c_vrsn;
354
355 while (isspace ((unsigned char) *cp))
356 cp++;
357 for (dp = strchr(cp, '\n'); dp; dp = strchr(dp, '\n'))
358 *dp++ = ' ';
359 for (dp = cp + strlen (cp) - 1; dp >= cp; dp--)
360 if (!isspace ((unsigned char) *dp))
361 break;
362 *++dp = '\0';
363 if (debugsw)
364 fprintf (stderr, "%s: %s\n", VRSN_FIELD, cp);
365
366 if (*cp == '(' &&
367 get_comment (ct->c_file, &ct->c_ctinfo, &cp, 0) == NOTOK)
368 goto out;
369
370 for (dp = cp; istoken (*dp); dp++)
371 continue;
372 c = *dp;
373 *dp = '\0';
374 ucmp = !strcasecmp (cp, VRSN_VALUE);
375 *dp = c;
376 if (!ucmp) {
377 admonish (NULL, "message %s has unknown value for %s: field (%s)",
378 ct->c_file, VRSN_FIELD, cp);
379 }
380 }
381 else if (!strcasecmp (hp->name, TYPE_FIELD)) {
382 /* Get Content-Type field */
383 struct str2init *s2i;
384 CI ci = &ct->c_ctinfo;
385
386 /* Check if we've already seen a Content-Type header */
387 if (ct->c_ctline) {
388 advise (NULL, "message %s has multiple %s: fields",
389 ct->c_file, TYPE_FIELD);
390 goto next_header;
391 }
392
393 /* Parse the Content-Type field */
394 if (get_ctinfo (hp->value, ct, 0) == NOTOK)
395 goto out;
396
397 /*
398 * Set the Init function and the internal
399 * flag for this content type.
400 */
401 for (s2i = str2cts; s2i->si_key; s2i++)
402 if (!strcasecmp (ci->ci_type, s2i->si_key))
403 break;
404 if (!s2i->si_key && !uprf (ci->ci_type, "X-"))
405 s2i++;
406 ct->c_type = s2i->si_val;
407 ct->c_ctinitfnx = s2i->si_init;
408 }
409 else if (!strcasecmp (hp->name, ENCODING_FIELD)) {
410 /* Get Content-Transfer-Encoding field */
411 char c, *cp, *dp;
412 struct str2init *s2i;
413
414 /*
415 * Check if we've already seen the
416 * Content-Transfer-Encoding field
417 */
418 if (ct->c_celine) {
419 advise (NULL, "message %s has multiple %s: fields",
420 ct->c_file, ENCODING_FIELD);
421 goto next_header;
422 }
423
424 /* get copy of this field */
425 ct->c_celine = cp = add (hp->value, NULL);
426
427 while (isspace ((unsigned char) *cp))
428 cp++;
429 for (dp = cp; istoken (*dp); dp++)
430 continue;
431 c = *dp;
432 *dp = '\0';
433
434 /*
435 * Find the internal flag and Init function
436 * for this transfer encoding.
437 */
438 for (s2i = str2ces; s2i->si_key; s2i++)
439 if (!strcasecmp (cp, s2i->si_key))
440 break;
441 if (!s2i->si_key && !uprf (cp, "X-"))
442 s2i++;
443 *dp = c;
444 ct->c_encoding = s2i->si_val;
445
446 /* Call the Init function for this encoding */
447 if (s2i->si_init && (*s2i->si_init) (ct) == NOTOK)
448 goto out;
449 }
450 else if (!strcasecmp (hp->name, MD5_FIELD)) {
451 /* Get Content-MD5 field */
452 char *cp, *dp, *ep;
453
454 if (!checksw)
455 goto next_header;
456
457 if (ct->c_digested) {
458 advise (NULL, "message %s has multiple %s: fields",
459 ct->c_file, MD5_FIELD);
460 goto next_header;
461 }
462
463 ep = cp = add (hp->value, NULL); /* get a copy */
464
465 while (isspace ((unsigned char) *cp))
466 cp++;
467 for (dp = strchr(cp, '\n'); dp; dp = strchr(dp, '\n'))
468 *dp++ = ' ';
469 for (dp = cp + strlen (cp) - 1; dp >= cp; dp--)
470 if (!isspace ((unsigned char) *dp))
471 break;
472 *++dp = '\0';
473 if (debugsw)
474 fprintf (stderr, "%s: %s\n", MD5_FIELD, cp);
475
476 if (*cp == '(' &&
477 get_comment (ct->c_file, &ct->c_ctinfo, &cp, 0) == NOTOK) {
478 free (ep);
479 goto out;
480 }
481
482 for (dp = cp; *dp && !isspace ((unsigned char) *dp); dp++)
483 continue;
484 *dp = '\0';
485
486 readDigest (ct, cp);
487 free (ep);
488 ct->c_digested++;
489 }
490 else if (!strcasecmp (hp->name, ID_FIELD)) {
491 /* Get Content-ID field */
492 ct->c_id = add (hp->value, ct->c_id);
493 }
494 else if (!strcasecmp (hp->name, DESCR_FIELD)) {
495 /* Get Content-Description field */
496 ct->c_descr = add (hp->value, ct->c_descr);
497 }
498 else if (!strcasecmp (hp->name, DISPO_FIELD)) {
499 /* Get Content-Disposition field */
500 ct->c_dispo = add (hp->value, ct->c_dispo);
501 }
502
503 next_header:
504 hp = hp->next; /* next header field */
505 }
506
507 /*
508 * Check if we saw a Content-Type field.
509 * If not, then assign a default value for
510 * it, and the Init function.
511 */
512 if (!ct->c_ctline) {
513 /*
514 * If we are inside a multipart/digest message,
515 * so default type is message/rfc822
516 */
517 if (toplevel < 0) {
518 if (get_ctinfo ("message/rfc822", ct, 0) == NOTOK)
519 goto out;
520 ct->c_type = CT_MESSAGE;
521 ct->c_ctinitfnx = InitMessage;
522 } else {
523 /*
524 * Else default type is text/plain
525 */
526 if (get_ctinfo ("text/plain", ct, 0) == NOTOK)
527 goto out;
528 ct->c_type = CT_TEXT;
529 ct->c_ctinitfnx = InitText;
530 }
531 }
532
533 /* Use default Transfer-Encoding, if necessary */
534 if (!ct->c_celine) {
535 ct->c_encoding = CE_7BIT;
536 Init7Bit (ct);
537 }
538
539 return ct;
540
541 out:
542 free_content (ct);
543 return NULL;
544 }
545
546
547 /*
548 * small routine to add header field to list
549 */
550
551 int
552 add_header (CT ct, char *name, char *value)
553 {
554 HF hp;
555
556 /* allocate header field structure */
557 hp = mh_xmalloc (sizeof(*hp));
558
559 /* link data into header structure */
560 hp->name = name;
561 hp->value = value;
562 hp->next = NULL;
563
564 /* link header structure into the list */
565 if (ct->c_first_hf == NULL) {
566 ct->c_first_hf = hp; /* this is the first */
567 ct->c_last_hf = hp;
568 } else {
569 ct->c_last_hf->next = hp; /* add it to the end */
570 ct->c_last_hf = hp;
571 }
572
573 return 0;
574 }
575
576
577 /* Make sure that buf contains at least one appearance of name,
578 followed by =. If not, insert both name and value, just after
579 first semicolon, if any. Note that name should not contain a
580 trailing =. And quotes will be added around the value. Typical
581 usage: make sure that a Content-Disposition header contains
582 filename="foo". If it doesn't and value does, use value from
583 that. */
584 static char *
585 incl_name_value (char *buf, char *name, char *value) {
586 char *newbuf = buf;
587
588 /* Assume that name is non-null. */
589 if (buf && value) {
590 char *name_plus_equal = concat (name, "=", NULL);
591
592 if (! strstr (buf, name_plus_equal)) {
593 char *insertion;
594 char *cp, *prefix, *suffix;
595
596 /* Trim trailing space, esp. newline. */
597 for (cp = &buf[strlen (buf) - 1];
598 cp >= buf && isspace ((unsigned char) *cp);
599 --cp) {
600 *cp = '\0';
601 }
602
603 insertion = concat ("; ", name, "=", "\"", value, "\"", NULL);
604
605 /* Insert at first semicolon, if any. If none, append to
606 end. */
607 prefix = add (buf, NULL);
608 if ((cp = strchr (prefix, ';'))) {
609 suffix = concat (cp, NULL);
610 *cp = '\0';
611 newbuf = concat (prefix, insertion, suffix, "\n", NULL);
612 free (suffix);
613 } else {
614 /* Append to end. */
615 newbuf = concat (buf, insertion, "\n", NULL);
616 }
617
618 free (prefix);
619 free (insertion);
620 free (buf);
621 }
622
623 free (name_plus_equal);
624 }
625
626 return newbuf;
627 }
628
629 /* Extract just name_suffix="foo", if any, from value. If there isn't
630 one, return the entire value. Note that, for example, a name_suffix
631 of name will match filename="foo", and return foo. */
632 static char *
633 extract_name_value (char *name_suffix, char *value) {
634 char *extracted_name_value = value;
635 char *name_suffix_plus_quote = concat (name_suffix, "=\"", NULL);
636 char *name_suffix_equals = strstr (value, name_suffix_plus_quote);
637 char *cp;
638
639 free (name_suffix_plus_quote);
640 if (name_suffix_equals) {
641 char *name_suffix_begin;
642
643 /* Find first \". */
644 for (cp = name_suffix_equals; *cp != '"'; ++cp) /* empty */;
645 name_suffix_begin = ++cp;
646 /* Find second \". */
647 for (; *cp != '"'; ++cp) /* empty */;
648
649 extracted_name_value = mh_xmalloc (cp - name_suffix_begin + 1);
650 memcpy (extracted_name_value,
651 name_suffix_begin,
652 cp - name_suffix_begin);
653 extracted_name_value[cp - name_suffix_begin] = '\0';
654 }
655
656 return extracted_name_value;
657 }
658
659 /*
660 * Parse Content-Type line and (if `magic' is non-zero) mhbuild composition
661 * directives. Fills in the information of the CTinfo structure.
662 */
663 int
664 get_ctinfo (char *cp, CT ct, int magic)
665 {
666 int i;
667 char *dp;
668 char c;
669 CI ci;
670 int status;
671
672 ci = &ct->c_ctinfo;
673 i = strlen (invo_name) + 2;
674
675 /* store copy of Content-Type line */
676 cp = ct->c_ctline = add (cp, NULL);
677
678 while (isspace ((unsigned char) *cp)) /* trim leading spaces */
679 cp++;
680
681 /* change newlines to spaces */
682 for (dp = strchr(cp, '\n'); dp; dp = strchr(dp, '\n'))
683 *dp++ = ' ';
684
685 /* trim trailing spaces */
686 for (dp = cp + strlen (cp) - 1; dp >= cp; dp--)
687 if (!isspace ((unsigned char) *dp))
688 break;
689 *++dp = '\0';
690
691 if (debugsw)
692 fprintf (stderr, "%s: %s\n", TYPE_FIELD, cp);
693
694 if (*cp == '(' && get_comment (ct->c_file, &ct->c_ctinfo, &cp, 1) == NOTOK)
695 return NOTOK;
696
697 for (dp = cp; istoken (*dp); dp++)
698 continue;
699 c = *dp, *dp = '\0';
700 ci->ci_type = add (cp, NULL); /* store content type */
701 *dp = c, cp = dp;
702
703 if (!*ci->ci_type) {
704 advise (NULL, "invalid %s: field in message %s (empty type)",
705 TYPE_FIELD, ct->c_file);
706 return NOTOK;
707 }
708
709 /* down case the content type string */
710 for (dp = ci->ci_type; *dp; dp++)
711 if (isalpha((unsigned char) *dp) && isupper ((unsigned char) *dp))
712 *dp = tolower ((unsigned char) *dp);
713
714 while (isspace ((unsigned char) *cp))
715 cp++;
716
717 if (*cp == '(' && get_comment (ct->c_file, &ct->c_ctinfo, &cp, 1) == NOTOK)
718 return NOTOK;
719
720 if (*cp != '/') {
721 if (!magic)
722 ci->ci_subtype = add ("", NULL);
723 goto magic_skip;
724 }
725
726 cp++;
727 while (isspace ((unsigned char) *cp))
728 cp++;
729
730 if (*cp == '(' && get_comment (ct->c_file, &ct->c_ctinfo, &cp, 1) == NOTOK)
731 return NOTOK;
732
733 for (dp = cp; istoken (*dp); dp++)
734 continue;
735 c = *dp, *dp = '\0';
736 ci->ci_subtype = add (cp, NULL); /* store the content subtype */
737 *dp = c, cp = dp;
738
739 if (!*ci->ci_subtype) {
740 advise (NULL,
741 "invalid %s: field in message %s (empty subtype for \"%s\")",
742 TYPE_FIELD, ct->c_file, ci->ci_type);
743 return NOTOK;
744 }
745
746 /* down case the content subtype string */
747 for (dp = ci->ci_subtype; *dp; dp++)
748 if (isalpha((unsigned char) *dp) && isupper ((unsigned char) *dp))
749 *dp = tolower ((unsigned char) *dp);
750
751 magic_skip:
752 while (isspace ((unsigned char) *cp))
753 cp++;
754
755 if (*cp == '(' && get_comment (ct->c_file, &ct->c_ctinfo, &cp, 1) == NOTOK)
756 return NOTOK;
757
758 if (parse_header_attrs (ct->c_file, i, &cp, ci, &status) == NOTOK) {
759 return status;
760 }
761
762 /*
763 * Get any <Content-Id> given in buffer
764 */
765 if (magic && *cp == '<') {
766 if (ct->c_id) {
767 free (ct->c_id);
768 ct->c_id = NULL;
769 }
770 if (!(dp = strchr(ct->c_id = ++cp, '>'))) {
771 advise (NULL, "invalid ID in message %s", ct->c_file);
772 return NOTOK;
773 }
774 c = *dp;
775 *dp = '\0';
776 if (*ct->c_id)
777 ct->c_id = concat ("<", ct->c_id, ">\n", NULL);
778 else
779 ct->c_id = NULL;
780 *dp++ = c;
781 cp = dp;
782
783 while (isspace ((unsigned char) *cp))
784 cp++;
785 }
786
787 /*
788 * Get any [Content-Description] given in buffer.
789 */
790 if (magic && *cp == '[') {
791 ct->c_descr = ++cp;
792 for (dp = cp + strlen (cp) - 1; dp >= cp; dp--)
793 if (*dp == ']')
794 break;
795 if (dp < cp) {
796 advise (NULL, "invalid description in message %s", ct->c_file);
797 ct->c_descr = NULL;
798 return NOTOK;
799 }
800
801 c = *dp;
802 *dp = '\0';
803 if (*ct->c_descr)
804 ct->c_descr = concat (ct->c_descr, "\n", NULL);
805 else
806 ct->c_descr = NULL;
807 *dp++ = c;
808 cp = dp;
809
810 while (isspace ((unsigned char) *cp))
811 cp++;
812 }
813
814 /*
815 * Get any {Content-Disposition} given in buffer.
816 */
817 if (magic && *cp == '{') {
818 ct->c_dispo = ++cp;
819 for (dp = cp + strlen (cp) - 1; dp >= cp; dp--)
820 if (*dp == '}')
821 break;
822 if (dp < cp) {
823 advise (NULL, "invalid disposition in message %s", ct->c_file);
824 ct->c_dispo = NULL;
825 return NOTOK;
826 }
827
828 c = *dp;
829 *dp = '\0';
830 if (*ct->c_dispo)
831 ct->c_dispo = concat (ct->c_dispo, "\n", NULL);
832 else
833 ct->c_dispo = NULL;
834 *dp++ = c;
835 cp = dp;
836
837 while (isspace ((unsigned char) *cp))
838 cp++;
839 }
840
841 /*
842 * Check if anything is left over
843 */
844 if (*cp) {
845 if (magic) {
846 ci->ci_magic = add (cp, NULL);
847
848 /* If there is a Content-Disposition header and it doesn't
849 have a *filename=, extract it from the magic contents.
850 The r1bindex call skips any leading directory
851 components. */
852 if (ct->c_dispo)
853 ct->c_dispo =
854 incl_name_value (ct->c_dispo,
855 "filename",
856 r1bindex (extract_name_value ("name",
857 ci->
858 ci_magic),
859 '/'));
860 }
861 else
862 advise (NULL,
863 "extraneous information in message %s's %s: field\n%*.*s(%s)",
864 ct->c_file, TYPE_FIELD, i, i, "", cp);
865 }
866
867 return OK;
868 }
869
870
871 static int
872 get_comment (const char *filename, CI ci, char **ap, int istype)
873 {
874 int i;
875 char *bp, *cp;
876 char c, buffer[BUFSIZ], *dp;
877
878 cp = *ap;
879 bp = buffer;
880 cp++;
881
882 for (i = 0;;) {
883 switch (c = *cp++) {
884 case '\0':
885 invalid:
886 advise (NULL, "invalid comment in message %s's %s: field",
887 filename, istype ? TYPE_FIELD : VRSN_FIELD);
888 return NOTOK;
889
890 case '\\':
891 *bp++ = c;
892 if ((c = *cp++) == '\0')
893 goto invalid;
894 *bp++ = c;
895 continue;
896
897 case '(':
898 i++;
899 /* and fall... */
900 default:
901 *bp++ = c;
902 continue;
903
904 case ')':
905 if (--i < 0)
906 break;
907 *bp++ = c;
908 continue;
909 }
910 break;
911 }
912 *bp = '\0';
913
914 if (istype) {
915 if ((dp = ci->ci_comment)) {
916 ci->ci_comment = concat (dp, " ", buffer, NULL);
917 free (dp);
918 } else {
919 ci->ci_comment = add (buffer, NULL);
920 }
921 }
922
923 while (isspace ((unsigned char) *cp))
924 cp++;
925
926 *ap = cp;
927 return OK;
928 }
929
930
931 /*
932 * CONTENTS
933 *
934 * Handles content types audio, image, and video.
935 * There's not much to do right here.
936 */
937
938 static int
939 InitGeneric (CT ct)
940 {
941 NMH_UNUSED (ct);
942
943 return OK; /* not much to do here */
944 }
945
946
947 /*
948 * TEXT
949 */
950
951 static int
952 InitText (CT ct)
953 {
954 char buffer[BUFSIZ];
955 char *chset = NULL;
956 char **ap, **ep, *cp;
957 struct k2v *kv;
958 struct text *t;
959 CI ci = &ct->c_ctinfo;
960
961 /* check for missing subtype */
962 if (!*ci->ci_subtype)
963 ci->ci_subtype = add ("plain", ci->ci_subtype);
964
965 /* match subtype */
966 for (kv = SubText; kv->kv_key; kv++)
967 if (!strcasecmp (ci->ci_subtype, kv->kv_key))
968 break;
969 ct->c_subtype = kv->kv_value;
970
971 /* allocate text character set structure */
972 if ((t = (struct text *) calloc (1, sizeof(*t))) == NULL)
973 adios (NULL, "out of memory");
974 ct->c_ctparams = (void *) t;
975
976 /* scan for charset parameter */
977 for (ap = ci->ci_attrs, ep = ci->ci_values; *ap; ap++, ep++)
978 if (!strcasecmp (*ap, "charset"))
979 break;
980
981 /* check if content specified a character set */
982 if (*ap) {
983 chset = *ep;
984 t->tx_charset = CHARSET_SPECIFIED;
985 } else {
986 t->tx_charset = CHARSET_UNSPECIFIED;
987 }
988
989 /*
990 * If we can not handle character set natively,
991 * then check profile for string to modify the
992 * terminal or display method.
993 *
994 * termproc is for mhshow, though mhlist -debug prints it, too.
995 */
996 if (chset != NULL && !check_charset (chset, strlen (chset))) {
997 snprintf (buffer, sizeof(buffer), "%s-charset-%s", invo_name, chset);
998 if ((cp = context_find (buffer)))
999 ct->c_termproc = getcpy (cp);
1000 }
1001
1002 return OK;
1003 }
1004
1005
1006 /*
1007 * MULTIPART
1008 */
1009
1010 static int
1011 InitMultiPart (CT ct)
1012 {
1013 int inout;
1014 long last, pos;
1015 char *cp, *dp, **ap, **ep;
1016 char *bp, buffer[BUFSIZ];
1017 struct multipart *m;
1018 struct k2v *kv;
1019 struct part *part, **next;
1020 CI ci = &ct->c_ctinfo;
1021 CT p;
1022 FILE *fp;
1023
1024 /*
1025 * The encoding for multipart messages must be either
1026 * 7bit, 8bit, or binary (per RFC2045).
1027 */
1028 if (! skip_mp_cte_check && ct->c_encoding != CE_7BIT &&
1029 ct->c_encoding != CE_8BIT && ct->c_encoding != CE_BINARY) {
1030 /* Copy the Content-Transfer-Encoding header field body so we can
1031 remove any trailing whitespace and leading blanks from it. */
1032 char *cte = add (ct->c_celine ? ct->c_celine : "(null)", NULL);
1033
1034 bp = cte + strlen (cte) - 1;
1035 while (bp >= cte && isspace ((unsigned char) *bp)) *bp-- = '\0';
1036 for (bp = cte; *bp && isblank ((unsigned char) *bp); ++bp) continue;
1037
1038 admonish (NULL,
1039 "\"%s/%s\" type in message %s must be encoded in\n"
1040 "7bit, 8bit, or binary, per RFC 2045 (6.4). One workaround "
1041 "is to\nmanually edit the file and change the \"%s\"\n"
1042 "Content-Transfer-Encoding to one of those. For now",
1043 ci->ci_type, ci->ci_subtype, ct->c_file, bp);
1044 free (cte);
1045
1046 return NOTOK;
1047 }
1048
1049 /* match subtype */
1050 for (kv = SubMultiPart; kv->kv_key; kv++)
1051 if (!strcasecmp (ci->ci_subtype, kv->kv_key))
1052 break;
1053 ct->c_subtype = kv->kv_value;
1054
1055 /*
1056 * Check for "boundary" parameter, which is
1057 * required for multipart messages.
1058 */
1059 bp = 0;
1060 for (ap = ci->ci_attrs, ep = ci->ci_values; *ap; ap++, ep++) {
1061 if (!strcasecmp (*ap, "boundary")) {
1062 bp = *ep;
1063 break;
1064 }
1065 }
1066
1067 /* complain if boundary parameter is missing */
1068 if (!*ap) {
1069 advise (NULL,
1070 "a \"boundary\" parameter is mandatory for \"%s/%s\" type in message %s's %s: field",
1071 ci->ci_type, ci->ci_subtype, ct->c_file, TYPE_FIELD);
1072 return NOTOK;
1073 }
1074
1075 /* allocate primary structure for multipart info */
1076 if ((m = (struct multipart *) calloc (1, sizeof(*m))) == NULL)
1077 adios (NULL, "out of memory");
1078 ct->c_ctparams = (void *) m;
1079
1080 /* check if boundary parameter contains only whitespace characters */
1081 for (cp = bp; isspace ((unsigned char) *cp); cp++)
1082 continue;
1083 if (!*cp) {
1084 advise (NULL, "invalid \"boundary\" parameter for \"%s/%s\" type in message %s's %s: field",
1085 ci->ci_type, ci->ci_subtype, ct->c_file, TYPE_FIELD);
1086 return NOTOK;
1087 }
1088
1089 /* remove trailing whitespace from boundary parameter */
1090 for (cp = bp, dp = cp + strlen (cp) - 1; dp > cp; dp--)
1091 if (!isspace ((unsigned char) *dp))
1092 break;
1093 *++dp = '\0';
1094
1095 /* record boundary separators */
1096 m->mp_start = concat (bp, "\n", NULL);
1097 m->mp_stop = concat (bp, "--\n", NULL);
1098
1099 if (!ct->c_fp && (ct->c_fp = fopen (ct->c_file, "r")) == NULL) {
1100 advise (ct->c_file, "unable to open for reading");
1101 return NOTOK;
1102 }
1103
1104 fseek (fp = ct->c_fp, pos = ct->c_begin, SEEK_SET);
1105 last = ct->c_end;
1106 next = &m->mp_parts;
1107 part = NULL;
1108 inout = 1;
1109
1110 while (fgets (buffer, sizeof(buffer) - 1, fp)) {
1111 if (pos > last)
1112 break;
1113
1114 pos += strlen (buffer);
1115 if (buffer[0] != '-' || buffer[1] != '-')
1116 continue;
1117 if (inout) {
1118 if (strcmp (buffer + 2, m->mp_start))
1119 continue;
1120 next_part:
1121 if ((part = (struct part *) calloc (1, sizeof(*part))) == NULL)
1122 adios (NULL, "out of memory");
1123 *next = part;
1124 next = &part->mp_next;
1125
1126 if (!(p = get_content (fp, ct->c_file,
1127 ct->c_subtype == MULTI_DIGEST ? -1 : 0))) {
1128 ct->c_fp = NULL;
1129 return NOTOK;
1130 }
1131 p->c_fp = NULL;
1132 part->mp_part = p;
1133 pos = p->c_begin;
1134 fseek (fp, pos, SEEK_SET);
1135 inout = 0;
1136 } else {
1137 if (strcmp (buffer + 2, m->mp_start) == 0) {
1138 inout = 1;
1139 end_part:
1140 p = part->mp_part;
1141 p->c_end = ftell(fp) - (strlen(buffer) + 1);
1142 if (p->c_end < p->c_begin)
1143 p->c_begin = p->c_end;
1144 if (inout)
1145 goto next_part;
1146 goto last_part;
1147 } else {
1148 if (strcmp (buffer + 2, m->mp_stop) == 0)
1149 goto end_part;
1150 }
1151 }
1152 }
1153
1154 if (! suppress_bogus_mp_content_warning) {
1155 advise (NULL, "bogus multipart content in message %s", ct->c_file);
1156 }
1157 bogus_mp_content = 1;
1158
1159 if (!inout && part) {
1160 p = part->mp_part;
1161 p->c_end = ct->c_end;
1162
1163 if (p->c_begin >= p->c_end) {
1164 for (next = &m->mp_parts; *next != part;
1165 next = &((*next)->mp_next))
1166 continue;
1167 *next = NULL;
1168 free_content (p);
1169 free ((char *) part);
1170 }
1171 }
1172
1173 last_part:
1174 /* reverse the order of the parts for multipart/alternative */
1175 if (ct->c_subtype == MULTI_ALTERNATE)
1176 reverse_parts (ct);
1177
1178 /*
1179 * label all subparts with part number, and
1180 * then initialize the content of the subpart.
1181 */
1182 {
1183 int partnum;
1184 char *pp;
1185 char partnam[BUFSIZ];
1186
1187 if (ct->c_partno) {
1188 snprintf (partnam, sizeof(partnam), "%s.", ct->c_partno);
1189 pp = partnam + strlen (partnam);
1190 } else {
1191 pp = partnam;
1192 }
1193
1194 for (part = m->mp_parts, partnum = 1; part;
1195 part = part->mp_next, partnum++) {
1196 p = part->mp_part;
1197
1198 sprintf (pp, "%d", partnum);
1199 p->c_partno = add (partnam, NULL);
1200
1201 /* initialize the content of the subparts */
1202 if (p->c_ctinitfnx && (*p->c_ctinitfnx) (p) == NOTOK) {
1203 fclose (ct->c_fp);
1204 ct->c_fp = NULL;
1205 return NOTOK;
1206 }
1207 }
1208 }
1209
1210 get_leftover_mp_content (ct, 1);
1211 get_leftover_mp_content (ct, 0);
1212
1213 fclose (ct->c_fp);
1214 ct->c_fp = NULL;
1215 return OK;
1216 }
1217
1218
1219 /*
1220 * reverse the order of the parts of a multipart/alternative
1221 */
1222
1223 void
1224 reverse_parts (CT ct)
1225 {
1226 struct multipart *m = (struct multipart *) ct->c_ctparams;
1227 struct part *part;
1228 struct part *next;
1229
1230 /* Reverse the order of its parts by walking the mp_parts list
1231 and pushing each node to the front. */
1232 for (part = m->mp_parts, m->mp_parts = NULL; part; part = next) {
1233 next = part->mp_next;
1234 part->mp_next = m->mp_parts;
1235 m->mp_parts = part;
1236 }
1237 }
1238
1239
1240 /*
1241 * MESSAGE
1242 */
1243
1244 static int
1245 InitMessage (CT ct)
1246 {
1247 struct k2v *kv;
1248 CI ci = &ct->c_ctinfo;
1249
1250 if ((ct->c_encoding != CE_7BIT) && (ct->c_encoding != CE_8BIT)) {
1251 admonish (NULL,
1252 "\"%s/%s\" type in message %s should be encoded in 7bit or 8bit",
1253 ci->ci_type, ci->ci_subtype, ct->c_file);
1254 return NOTOK;
1255 }
1256
1257 /* check for missing subtype */
1258 if (!*ci->ci_subtype)
1259 ci->ci_subtype = add ("rfc822", ci->ci_subtype);
1260
1261 /* match subtype */
1262 for (kv = SubMessage; kv->kv_key; kv++)
1263 if (!strcasecmp (ci->ci_subtype, kv->kv_key))
1264 break;
1265 ct->c_subtype = kv->kv_value;
1266
1267 switch (ct->c_subtype) {
1268 case MESSAGE_RFC822:
1269 break;
1270
1271 case MESSAGE_PARTIAL:
1272 {
1273 char **ap, **ep;
1274 struct partial *p;
1275
1276 if ((p = (struct partial *) calloc (1, sizeof(*p))) == NULL)
1277 adios (NULL, "out of memory");
1278 ct->c_ctparams = (void *) p;
1279
1280 /* scan for parameters "id", "number", and "total" */
1281 for (ap = ci->ci_attrs, ep = ci->ci_values; *ap; ap++, ep++) {
1282 if (!strcasecmp (*ap, "id")) {
1283 p->pm_partid = add (*ep, NULL);
1284 continue;
1285 }
1286 if (!strcasecmp (*ap, "number")) {
1287 if (sscanf (*ep, "%d", &p->pm_partno) != 1
1288 || p->pm_partno < 1) {
1289 invalid_param:
1290 advise (NULL,
1291 "invalid %s parameter for \"%s/%s\" type in message %s's %s field",
1292 *ap, ci->ci_type, ci->ci_subtype,
1293 ct->c_file, TYPE_FIELD);
1294 return NOTOK;
1295 }
1296 continue;
1297 }
1298 if (!strcasecmp (*ap, "total")) {
1299 if (sscanf (*ep, "%d", &p->pm_maxno) != 1
1300 || p->pm_maxno < 1)
1301 goto invalid_param;
1302 continue;
1303 }
1304 }
1305
1306 if (!p->pm_partid
1307 || !p->pm_partno
1308 || (p->pm_maxno && p->pm_partno > p->pm_maxno)) {
1309 advise (NULL,
1310 "invalid parameters for \"%s/%s\" type in message %s's %s field",
1311 ci->ci_type, ci->ci_subtype,
1312 ct->c_file, TYPE_FIELD);
1313 return NOTOK;
1314 }
1315 }
1316 break;
1317
1318 case MESSAGE_EXTERNAL:
1319 {
1320 int exresult;
1321 struct exbody *e;
1322 CT p;
1323 FILE *fp;
1324
1325 if ((e = (struct exbody *) calloc (1, sizeof(*e))) == NULL)
1326 adios (NULL, "out of memory");
1327 ct->c_ctparams = (void *) e;
1328
1329 if (!ct->c_fp
1330 && (ct->c_fp = fopen (ct->c_file, "r")) == NULL) {
1331 advise (ct->c_file, "unable to open for reading");
1332 return NOTOK;
1333 }
1334
1335 fseek (fp = ct->c_fp, ct->c_begin, SEEK_SET);
1336
1337 if (!(p = get_content (fp, ct->c_file, 0))) {
1338 ct->c_fp = NULL;
1339 return NOTOK;
1340 }
1341
1342 e->eb_parent = ct;
1343 e->eb_content = p;
1344 p->c_ctexbody = e;
1345 p->c_ceopenfnx = NULL;
1346 if ((exresult = params_external (ct, 0)) != NOTOK
1347 && p->c_ceopenfnx == openMail) {
1348 int cc, size;
1349 char *bp;
1350
1351 if ((size = ct->c_end - p->c_begin) <= 0) {
1352 if (!e->eb_subject)
1353 content_error (NULL, ct,
1354 "empty body for access-type=mail-server");
1355 goto no_body;
1356 }
1357
1358 e->eb_body = bp = mh_xmalloc ((unsigned) size);
1359 fseek (p->c_fp, p->c_begin, SEEK_SET);
1360 while (size > 0)
1361 switch (cc = fread (bp, sizeof(*bp), size, p->c_fp)) {
1362 case NOTOK:
1363 adios ("failed", "fread");
1364
1365 case OK:
1366 adios (NULL, "unexpected EOF from fread");
1367
1368 default:
1369 bp += cc, size -= cc;
1370 break;
1371 }
1372 *bp = 0;
1373 }
1374 no_body:
1375 p->c_fp = NULL;
1376 p->c_end = p->c_begin;
1377
1378 fclose (ct->c_fp);
1379 ct->c_fp = NULL;
1380
1381 if (exresult == NOTOK)
1382 return NOTOK;
1383 if (e->eb_flags == NOTOK)
1384 return OK;
1385
1386 switch (p->c_type) {
1387 case CT_MULTIPART:
1388 break;
1389
1390 case CT_MESSAGE:
1391 if (p->c_subtype != MESSAGE_RFC822)
1392 break;
1393 /* else fall... */
1394 default:
1395 e->eb_partno = ct->c_partno;
1396 if (p->c_ctinitfnx)
1397 (*p->c_ctinitfnx) (p);
1398 break;
1399 }
1400 }
1401 break;
1402
1403 default:
1404 break;
1405 }
1406
1407 return OK;
1408 }
1409
1410
1411 int
1412 params_external (CT ct, int composing)
1413 {
1414 char **ap, **ep;
1415 struct exbody *e = (struct exbody *) ct->c_ctparams;
1416 CI ci = &ct->c_ctinfo;
1417
1418 ct->c_ceopenfnx = NULL;
1419 for (ap = ci->ci_attrs, ep = ci->ci_values; *ap; ap++, ep++) {
1420 if (!strcasecmp (*ap, "access-type")) {
1421 struct str2init *s2i;
1422 CT p = e->eb_content;
1423
1424 for (s2i = str2methods; s2i->si_key; s2i++)
1425 if (!strcasecmp (*ep, s2i->si_key))
1426 break;
1427 if (!s2i->si_key) {
1428 e->eb_access = *ep;
1429 e->eb_flags = NOTOK;
1430 p->c_encoding = CE_EXTERNAL;
1431 continue;
1432 }
1433 e->eb_access = s2i->si_key;
1434 e->eb_flags = s2i->si_val;
1435 p->c_encoding = CE_EXTERNAL;
1436
1437 /* Call the Init function for this external type */
1438 if ((*s2i->si_init)(p) == NOTOK)
1439 return NOTOK;
1440 continue;
1441 }
1442 if (!strcasecmp (*ap, "name")) {
1443 e->eb_name = *ep;
1444 continue;
1445 }
1446 if (!strcasecmp (*ap, "permission")) {
1447 e->eb_permission = *ep;
1448 continue;
1449 }
1450 if (!strcasecmp (*ap, "site")) {
1451 e->eb_site = *ep;
1452 continue;
1453 }
1454 if (!strcasecmp (*ap, "directory")) {
1455 e->eb_dir = *ep;
1456 continue;
1457 }
1458 if (!strcasecmp (*ap, "mode")) {
1459 e->eb_mode = *ep;
1460 continue;
1461 }
1462 if (!strcasecmp (*ap, "size")) {
1463 sscanf (*ep, "%lu", &e->eb_size);
1464 continue;
1465 }
1466 if (!strcasecmp (*ap, "server")) {
1467 e->eb_server = *ep;
1468 continue;
1469 }
1470 if (!strcasecmp (*ap, "subject")) {
1471 e->eb_subject = *ep;
1472 continue;
1473 }
1474 if (!strcasecmp (*ap, "url")) {
1475 /*
1476 * According to RFC 2017, we have to remove all whitespace from
1477 * the URL
1478 */
1479
1480 char *u, *p = *ep;
1481 e->eb_url = u = mh_xmalloc(strlen(*ep) + 1);
1482
1483 for (; *p != '\0'; p++) {
1484 if (! isspace((unsigned char) *p))
1485 *u++ = *p;
1486 }
1487
1488 *u = '\0';
1489 continue;
1490 }
1491 if (composing && !strcasecmp (*ap, "body")) {
1492 e->eb_body = getcpy (*ep);
1493 continue;
1494 }
1495 }
1496
1497 if (!e->eb_access) {
1498 advise (NULL,
1499 "invalid parameters for \"%s/%s\" type in message %s's %s field",
1500 ci->ci_type, ci->ci_subtype, ct->c_file, TYPE_FIELD);
1501 return NOTOK;
1502 }
1503
1504 return OK;
1505 }
1506
1507
1508 /*
1509 * APPLICATION
1510 */
1511
1512 static int
1513 InitApplication (CT ct)
1514 {
1515 struct k2v *kv;
1516 CI ci = &ct->c_ctinfo;
1517
1518 /* match subtype */
1519 for (kv = SubApplication; kv->kv_key; kv++)
1520 if (!strcasecmp (ci->ci_subtype, kv->kv_key))
1521 break;
1522 ct->c_subtype = kv->kv_value;
1523
1524 return OK;
1525 }
1526
1527
1528 /*
1529 * TRANSFER ENCODINGS
1530 */
1531
1532 static int
1533 init_encoding (CT ct, OpenCEFunc openfnx)
1534 {
1535 ct->c_ceopenfnx = openfnx;
1536 ct->c_ceclosefnx = close_encoding;
1537 ct->c_cesizefnx = size_encoding;
1538
1539 return OK;
1540 }
1541
1542
1543 void
1544 close_encoding (CT ct)
1545 {
1546 CE ce = &ct->c_cefile;
1547
1548 if (ce->ce_fp) {
1549 fclose (ce->ce_fp);
1550 ce->ce_fp = NULL;
1551 }
1552 }
1553
1554
1555 static unsigned long
1556 size_encoding (CT ct)
1557 {
1558 int fd;
1559 unsigned long size;
1560 char *file;
1561 CE ce = &ct->c_cefile;
1562 struct stat st;
1563
1564 if (ce->ce_fp && fstat (fileno (ce->ce_fp), &st) != NOTOK)
1565 return (long) st.st_size;
1566
1567 if (ce->ce_file) {
1568 if (stat (ce->ce_file, &st) != NOTOK)
1569 return (long) st.st_size;
1570 else
1571 return 0L;
1572 }
1573
1574 if (ct->c_encoding == CE_EXTERNAL)
1575 return (ct->c_end - ct->c_begin);
1576
1577 file = NULL;
1578 if ((fd = (*ct->c_ceopenfnx) (ct, &file)) == NOTOK)
1579 return (ct->c_end - ct->c_begin);
1580
1581 if (fstat (fd, &st) != NOTOK)
1582 size = (long) st.st_size;
1583 else
1584 size = 0L;
1585
1586 (*ct->c_ceclosefnx) (ct);
1587 return size;
1588 }
1589
1590
1591 /*
1592 * BASE64
1593 */
1594
1595 static unsigned char b642nib[0x80] = {
1596 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1597 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1598 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1599 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1600 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1601 0xff, 0xff, 0xff, 0x3e, 0xff, 0xff, 0xff, 0x3f,
1602 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b,
1603 0x3c, 0x3d, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1604 0xff, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
1605 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e,
1606 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16,
1607 0x17, 0x18, 0x19, 0xff, 0xff, 0xff, 0xff, 0xff,
1608 0xff, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20,
1609 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28,
1610 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30,
1611 0x31, 0x32, 0x33, 0xff, 0xff, 0xff, 0xff, 0xff
1612 };
1613
1614
1615 static int
1616 InitBase64 (CT ct)
1617 {
1618 return init_encoding (ct, openBase64);
1619 }
1620
1621
1622 static int
1623 openBase64 (CT ct, char **file)
1624 {
1625 int bitno, cc, digested;
1626 int fd, len, skip, own_ct_fp = 0;
1627 uint32_t bits;
1628 unsigned char value, b;
1629 char *cp, *ep, buffer[BUFSIZ];
1630 /* sbeck -- handle suffixes */
1631 CI ci;
1632 CE ce = &ct->c_cefile;
1633 MD5_CTX mdContext;
1634
1635 if (ce->ce_fp) {
1636 fseek (ce->ce_fp, 0L, SEEK_SET);
1637 goto ready_to_go;
1638 }
1639
1640 if (ce->ce_file) {
1641 if ((ce->ce_fp = fopen (ce->ce_file, "r")) == NULL) {
1642 content_error (ce->ce_file, ct, "unable to fopen for reading");
1643 return NOTOK;
1644 }
1645 goto ready_to_go;
1646 }
1647
1648 if (*file == NULL) {
1649 char *tempfile;
1650 if ((tempfile = m_mktemp2(NULL, invo_name, NULL, NULL)) == NULL) {
1651 adios(NULL, "unable to create temporary file in %s",
1652 get_temp_dir());
1653 }
1654 ce->ce_file = add (tempfile, NULL);
1655 ce->ce_unlink = 1;
1656 } else {
1657 ce->ce_file = add (*file, NULL);
1658 ce->ce_unlink = 0;
1659 }
1660
1661 /* sbeck@cise.ufl.edu -- handle suffixes */
1662 ci = &ct->c_ctinfo;
1663 snprintf (buffer, sizeof(buffer), "%s-suffix-%s/%s",
1664 invo_name, ci->ci_type, ci->ci_subtype);
1665 cp = context_find (buffer);
1666 if (cp == NULL || *cp == '\0') {
1667 snprintf (buffer, sizeof(buffer), "%s-suffix-%s", invo_name,
1668 ci->ci_type);
1669 cp = context_find (buffer);
1670 }
1671 if (cp != NULL && *cp != '\0') {
1672 if (! ce->ce_unlink) {
1673 ce->ce_file = add (cp, ce->ce_file);
1674 }
1675 }
1676
1677 if ((ce->ce_fp = fopen (ce->ce_file, "w+")) == NULL) {
1678 content_error (ce->ce_file, ct, "unable to fopen for reading/writing");
1679 return NOTOK;
1680 }
1681
1682 if ((len = ct->c_end - ct->c_begin) < 0)
1683 adios (NULL, "internal error(1)");
1684
1685 if (! ct->c_fp) {
1686 if ((ct->c_fp = fopen (ct->c_file, "r")) == NULL) {
1687 content_error (ct->c_file, ct, "unable to open for reading");
1688 return NOTOK;
1689 }
1690 own_ct_fp = 1;
1691 }
1692
1693 if ((digested = ct->c_digested))
1694 MD5Init (&mdContext);
1695
1696 bitno = 18;
1697 bits = 0L;
1698 skip = 0;
1699
1700 lseek (fd = fileno (ct->c_fp), (off_t) ct->c_begin, SEEK_SET);
1701 while (len > 0) {
1702 switch (cc = read (fd, buffer, sizeof(buffer) - 1)) {
1703 case NOTOK:
1704 content_error (ct->c_file, ct, "error reading from");
1705 goto clean_up;
1706
1707 case OK:
1708 content_error (NULL, ct, "premature eof");
1709 goto clean_up;
1710
1711 default:
1712 if (cc > len)
1713 cc = len;
1714 len -= cc;
1715
1716 for (ep = (cp = buffer) + cc; cp < ep; cp++) {
1717 switch (*cp) {
1718 default:
1719 if (isspace ((unsigned char) *cp))
1720 break;
1721 if (skip || (((unsigned char) *cp) & 0x80)
1722 || (value = b642nib[((unsigned char) *cp) & 0x7f]) > 0x3f) {
1723 if (debugsw) {
1724 fprintf (stderr, "*cp=0x%x pos=%ld skip=%d\n",
1725 (unsigned char) *cp,
1726 (long) (lseek (fd, (off_t) 0, SEEK_CUR) - (ep - cp)),
1727 skip);
1728 }
1729 content_error (NULL, ct,
1730 "invalid BASE64 encoding -- continuing");
1731 continue;
1732 }
1733
1734 bits |= value << bitno;
1735 test_end:
1736 if ((bitno -= 6) < 0) {
1737 b = (bits >> 16) & 0xff;
1738 putc ((char) b, ce->ce_fp);
1739 if (digested)
1740 MD5Update (&mdContext, &b, 1);
1741 if (skip < 2) {
1742 b = (bits >> 8) & 0xff;
1743 putc ((char) b, ce->ce_fp);
1744 if (digested)
1745 MD5Update (&mdContext, &b, 1);
1746 if (skip < 1) {
1747 b = bits & 0xff;
1748 putc ((char) b, ce->ce_fp);
1749 if (digested)
1750 MD5Update (&mdContext, &b, 1);
1751 }
1752 }
1753
1754 if (ferror (ce->ce_fp)) {
1755 content_error (ce->ce_file, ct,
1756 "error writing to");
1757 goto clean_up;
1758 }
1759 bitno = 18, bits = 0L, skip = 0;
1760 }
1761 break;
1762
1763 case '=':
1764 if (++skip > 3)
1765 goto self_delimiting;
1766 goto test_end;
1767 }
1768 }
1769 }
1770 }
1771
1772 if (bitno != 18) {
1773 if (debugsw)
1774 fprintf (stderr, "premature ending (bitno %d)\n", bitno);
1775
1776 content_error (NULL, ct, "invalid BASE64 encoding");
1777 goto clean_up;
1778 }
1779
1780 self_delimiting:
1781 fseek (ct->c_fp, 0L, SEEK_SET);
1782
1783 if (fflush (ce->ce_fp)) {
1784 content_error (ce->ce_file, ct, "error writing to");
1785 goto clean_up;
1786 }
1787
1788 if (digested) {
1789 unsigned char digest[16];
1790
1791 MD5Final (digest, &mdContext);
1792 if (memcmp((char *) digest, (char *) ct->c_digest,
1793 sizeof(digest) / sizeof(digest[0])))
1794 content_error (NULL, ct,
1795 "content integrity suspect (digest mismatch) -- continuing");
1796 else
1797 if (debugsw)
1798 fprintf (stderr, "content integrity confirmed\n");
1799 }
1800
1801 fseek (ce->ce_fp, 0L, SEEK_SET);
1802
1803 ready_to_go:
1804 *file = ce->ce_file;
1805 if (own_ct_fp) {
1806 fclose (ct->c_fp);
1807 ct->c_fp = NULL;
1808 }
1809 return fileno (ce->ce_fp);
1810
1811 clean_up:
1812 if (own_ct_fp) {
1813 fclose (ct->c_fp);
1814 ct->c_fp = NULL;
1815 }
1816 free_encoding (ct, 0);
1817 return NOTOK;
1818 }
1819
1820
1821 /*
1822 * QUOTED PRINTABLE
1823 */
1824
1825 static char hex2nib[0x80] = {
1826 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1827 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1828 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1829 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1830 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1831 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1832 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
1833 0x08, 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1834 0x00, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x00,
1835 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1836 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1837 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1838 0x00, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x00,
1839 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1840 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1841 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
1842 };
1843
1844
1845 static int
1846 InitQuoted (CT ct)
1847 {
1848 return init_encoding (ct, openQuoted);
1849 }
1850
1851
1852 static int
1853 openQuoted (CT ct, char **file)
1854 {
1855 int cc, digested, len, quoted, own_ct_fp = 0;
1856 char *cp, *ep;
1857 char buffer[BUFSIZ];
1858 unsigned char mask;
1859 CE ce = &ct->c_cefile;
1860 /* sbeck -- handle suffixes */
1861 CI ci;
1862 MD5_CTX mdContext;
1863
1864 if (ce->ce_fp) {
1865 fseek (ce->ce_fp, 0L, SEEK_SET);
1866 goto ready_to_go;
1867 }
1868
1869 if (ce->ce_file) {
1870 if ((ce->ce_fp = fopen (ce->ce_file, "r")) == NULL) {
1871 content_error (ce->ce_file, ct, "unable to fopen for reading");
1872 return NOTOK;
1873 }
1874 goto ready_to_go;
1875 }
1876
1877 if (*file == NULL) {
1878 char *tempfile;
1879 if ((tempfile = m_mktemp2(NULL, invo_name, NULL, NULL)) == NULL) {
1880 adios(NULL, "unable to create temporary file in %s",
1881 get_temp_dir());
1882 }
1883 ce->ce_file = add (tempfile, NULL);
1884 ce->ce_unlink = 1;
1885 } else {
1886 ce->ce_file = add (*file, NULL);
1887 ce->ce_unlink = 0;
1888 }
1889
1890 /* sbeck@cise.ufl.edu -- handle suffixes */
1891 ci = &ct->c_ctinfo;
1892 snprintf (buffer, sizeof(buffer), "%s-suffix-%s/%s",
1893 invo_name, ci->ci_type, ci->ci_subtype);
1894 cp = context_find (buffer);
1895 if (cp == NULL || *cp == '\0') {
1896 snprintf (buffer, sizeof(buffer), "%s-suffix-%s", invo_name,
1897 ci->ci_type);
1898 cp = context_find (buffer);
1899 }
1900 if (cp != NULL && *cp != '\0') {
1901 if (! ce->ce_unlink) {
1902 ce->ce_file = add (cp, ce->ce_file);
1903 }
1904 }
1905
1906 if ((ce->ce_fp = fopen (ce->ce_file, "w+")) == NULL) {
1907 content_error (ce->ce_file, ct, "unable to fopen for reading/writing");
1908 return NOTOK;
1909 }
1910
1911 if ((len = ct->c_end - ct->c_begin) < 0)
1912 adios (NULL, "internal error(2)");
1913
1914 if (! ct->c_fp) {
1915 if ((ct->c_fp = fopen (ct->c_file, "r")) == NULL) {
1916 content_error (ct->c_file, ct, "unable to open for reading");
1917 return NOTOK;
1918 }
1919 own_ct_fp = 1;
1920 }
1921
1922 if ((digested = ct->c_digested))
1923 MD5Init (&mdContext);
1924
1925 quoted = 0;
1926 #ifdef lint
1927 mask = 0;
1928 #endif
1929
1930 fseek (ct->c_fp, ct->c_begin, SEEK_SET);
1931 while (len > 0) {
1932 if (fgets (buffer, sizeof(buffer) - 1, ct->c_fp) == NULL) {
1933 content_error (NULL, ct, "premature eof");
1934 goto clean_up;
1935 }
1936
1937 if ((cc = strlen (buffer)) > len)
1938 cc = len;
1939 len -= cc;
1940
1941 for (ep = (cp = buffer) + cc - 1; cp <= ep; ep--)
1942 if (!isspace ((unsigned char) *ep))
1943 break;
1944 *++ep = '\n', ep++;
1945
1946 for (; cp < ep; cp++) {
1947 if (quoted > 0) {
1948 /* in an escape sequence */
1949 if (quoted == 1) {
1950 /* at byte 1 of an escape sequence */
1951 mask = hex2nib[((unsigned char) *cp) & 0x7f];
1952 /* next is byte 2 */
1953 quoted = 2;
1954 } else {
1955 /* at byte 2 of an escape sequence */
1956 mask <<= 4;
1957 mask |= hex2nib[((unsigned char) *cp) & 0x7f];
1958 putc (mask, ce->ce_fp);
1959 if (digested)
1960 MD5Update (&mdContext, &mask, 1);
1961 if (ferror (ce->ce_fp)) {
1962 content_error (ce->ce_file, ct, "error writing to");
1963 goto clean_up;
1964 }
1965 /* finished escape sequence; next may be literal or a new
1966 * escape sequence */
1967 quoted = 0;
1968 }
1969 /* on to next byte */
1970 continue;
1971 }
1972
1973 /* not in an escape sequence */
1974 if (*cp == '=') {
1975 /* starting an escape sequence, or invalid '='? */
1976 if (cp + 1 < ep && cp[1] == '\n') {
1977 /* "=\n" soft line break, eat the \n */
1978 cp++;
1979 continue;
1980 }
1981 if (cp + 1 >= ep || cp + 2 >= ep) {
1982 /* We don't have 2 bytes left, so this is an invalid
1983 * escape sequence; just show the raw bytes (below). */
1984 } else if (isxdigit ((unsigned char) cp[1]) &&
1985 isxdigit ((unsigned char) cp[2])) {
1986 /* Next 2 bytes are hex digits, making this a valid escape
1987 * sequence; let's decode it (above). */
1988 quoted = 1;
1989 continue;
1990 } else {
1991 /* One or both of the next 2 is out of range, making this
1992 * an invalid escape sequence; just show the raw bytes
1993 * (below). */
1994 }
1995 }
1996
1997 /* Just show the raw byte. */
1998 putc (*cp, ce->ce_fp);
1999 if (digested) {
2000 if (*cp == '\n') {
2001 MD5Update (&mdContext, (unsigned char *) "\r\n",2);
2002 } else {
2003 MD5Update (&mdContext, (unsigned char *) cp, 1);
2004 }
2005 }
2006 if (ferror (ce->ce_fp)) {
2007 content_error (ce->ce_file, ct, "error writing to");
2008 goto clean_up;
2009 }
2010 }
2011 }
2012 if (quoted) {
2013 content_error (NULL, ct,
2014 "invalid QUOTED-PRINTABLE encoding -- end-of-content while still quoting");
2015 goto clean_up;
2016 }
2017
2018 fseek (ct->c_fp, 0L, SEEK_SET);
2019
2020 if (fflush (ce->ce_fp)) {
2021 content_error (ce->ce_file, ct, "error writing to");
2022 goto clean_up;
2023 }
2024
2025 if (digested) {
2026 unsigned char digest[16];
2027
2028 MD5Final (digest, &mdContext);
2029 if (memcmp((char *) digest, (char *) ct->c_digest,
2030 sizeof(digest) / sizeof(digest[0])))
2031 content_error (NULL, ct,
2032 "content integrity suspect (digest mismatch) -- continuing");
2033 else
2034 if (debugsw)
2035 fprintf (stderr, "content integrity confirmed\n");
2036 }
2037
2038 fseek (ce->ce_fp, 0L, SEEK_SET);
2039
2040 ready_to_go:
2041 *file = ce->ce_file;
2042 if (own_ct_fp) {
2043 fclose (ct->c_fp);
2044 ct->c_fp = NULL;
2045 }
2046 return fileno (ce->ce_fp);
2047
2048 clean_up:
2049 free_encoding (ct, 0);
2050 if (own_ct_fp) {
2051 fclose (ct->c_fp);
2052 ct->c_fp = NULL;
2053 }
2054 return NOTOK;
2055 }
2056
2057
2058 /*
2059 * 7BIT
2060 */
2061
2062 static int
2063 Init7Bit (CT ct)
2064 {
2065 if (init_encoding (ct, open7Bit) == NOTOK)
2066 return NOTOK;
2067
2068 ct->c_cesizefnx = NULL; /* no need to decode for real size */
2069 return OK;
2070 }
2071
2072
2073 int
2074 open7Bit (CT ct, char **file)
2075 {
2076 int cc, fd, len, own_ct_fp = 0;
2077 char buffer[BUFSIZ];
2078 /* sbeck -- handle suffixes */
2079 char *cp;
2080 CI ci;
2081 CE ce = &ct->c_cefile;
2082
2083 if (ce->ce_fp) {
2084 fseek (ce->ce_fp, 0L, SEEK_SET);
2085 goto ready_to_go;
2086 }
2087
2088 if (ce->ce_file) {
2089 if ((ce->ce_fp = fopen (ce->ce_file, "r")) == NULL) {
2090 content_error (ce->ce_file, ct, "unable to fopen for reading");
2091 return NOTOK;
2092 }
2093 goto ready_to_go;
2094 }
2095
2096 if (*file == NULL) {
2097 char *tempfile;
2098 if ((tempfile = m_mktemp2(NULL, invo_name, NULL, NULL)) == NULL) {
2099 adios(NULL, "unable to create temporary file in %s",
2100 get_temp_dir());
2101 }
2102 ce->ce_file = add (tempfile, NULL);
2103 ce->ce_unlink = 1;
2104 } else {
2105 ce->ce_file = add (*file, NULL);
2106 ce->ce_unlink = 0;
2107 }
2108
2109 /* sbeck@cise.ufl.edu -- handle suffixes */
2110 ci = &ct->c_ctinfo;
2111 snprintf (buffer, sizeof(buffer), "%s-suffix-%s/%s",
2112 invo_name, ci->ci_type, ci->ci_subtype);
2113 cp = context_find (buffer);
2114 if (cp == NULL || *cp == '\0') {
2115 snprintf (buffer, sizeof(buffer), "%s-suffix-%s", invo_name,
2116 ci->ci_type);
2117 cp = context_find (buffer);
2118 }
2119 if (cp != NULL && *cp != '\0') {
2120 if (! ce->ce_unlink) {
2121 ce->ce_file = add (cp, ce->ce_file);
2122 }
2123 }
2124
2125 if ((ce->ce_fp = fopen (ce->ce_file, "w+")) == NULL) {
2126 content_error (ce->ce_file, ct, "unable to fopen for reading/writing");
2127 return NOTOK;
2128 }
2129
2130 if (ct->c_type == CT_MULTIPART) {
2131 char **ap, **ep;
2132 CI ci = &ct->c_ctinfo;
2133
2134 len = 0;
2135 fprintf (ce->ce_fp, "%s: %s/%s", TYPE_FIELD, ci->ci_type, ci->ci_subtype);
2136 len += strlen (TYPE_FIELD) + 2 + strlen (ci->ci_type)
2137 + 1 + strlen (ci->ci_subtype);
2138 for (ap = ci->ci_attrs, ep = ci->ci_values; *ap; ap++, ep++) {
2139 putc (';', ce->ce_fp);
2140 len++;
2141
2142 snprintf (buffer, sizeof(buffer), "%s=\"%s\"", *ap, *ep);
2143
2144 if (len + 1 + (cc = strlen (buffer)) >= CPERLIN) {
2145 fputs ("\n\t", ce->ce_fp);
2146 len = 8;
2147 } else {
2148 putc (' ', ce->ce_fp);
2149 len++;
2150 }
2151 fprintf (ce->ce_fp, "%s", buffer);
2152 len += cc;
2153 }
2154
2155 if (ci->ci_comment) {
2156 if (len + 1 + (cc = 2 + strlen (ci->ci_comment)) >= CPERLIN) {
2157 fputs ("\n\t", ce->ce_fp);
2158 len = 8;
2159 }
2160 else {
2161 putc (' ', ce->ce_fp);
2162 len++;
2163 }
2164 fprintf (ce->ce_fp, "(%s)", ci->ci_comment);
2165 len += cc;
2166 }
2167 fprintf (ce->ce_fp, "\n");
2168 if (ct->c_id)
2169 fprintf (ce->ce_fp, "%s:%s", ID_FIELD, ct->c_id);
2170 if (ct->c_descr)
2171 fprintf (ce->ce_fp, "%s:%s", DESCR_FIELD, ct->c_descr);
2172 if (ct->c_dispo)
2173 fprintf (ce->ce_fp, "%s:%s", DISPO_FIELD, ct->c_dispo);
2174 fprintf (ce->ce_fp, "\n");
2175 }
2176
2177 if ((len = ct->c_end - ct->c_begin) < 0)
2178 adios (NULL, "internal error(3)");
2179
2180 if (! ct->c_fp) {
2181 if ((ct->c_fp = fopen (ct->c_file, "r")) == NULL) {
2182 content_error (ct->c_file, ct, "unable to open for reading");
2183 return NOTOK;
2184 }
2185 own_ct_fp = 1;
2186 }
2187
2188 lseek (fd = fileno (ct->c_fp), (off_t) ct->c_begin, SEEK_SET);
2189 while (len > 0)
2190 switch (cc = read (fd, buffer, sizeof(buffer) - 1)) {
2191 case NOTOK:
2192 content_error (ct->c_file, ct, "error reading from");
2193 goto clean_up;
2194
2195 case OK:
2196 content_error (NULL, ct, "premature eof");
2197 goto clean_up;
2198
2199 default:
2200 if (cc > len)
2201 cc = len;
2202 len -= cc;
2203
2204 fwrite (buffer, sizeof(*buffer), cc, ce->ce_fp);
2205 if (ferror (ce->ce_fp)) {
2206 content_error (ce->ce_file, ct, "error writing to");
2207 goto clean_up;
2208 }
2209 }
2210
2211 fseek (ct->c_fp, 0L, SEEK_SET);
2212
2213 if (fflush (ce->ce_fp)) {
2214 content_error (ce->ce_file, ct, "error writing to");
2215 goto clean_up;
2216 }
2217
2218 fseek (ce->ce_fp, 0L, SEEK_SET);
2219
2220 ready_to_go:
2221 *file = ce->ce_file;
2222 if (own_ct_fp) {
2223 fclose (ct->c_fp);
2224 ct->c_fp = NULL;
2225 }
2226 return fileno (ce->ce_fp);
2227
2228 clean_up:
2229 free_encoding (ct, 0);
2230 if (own_ct_fp) {
2231 fclose (ct->c_fp);
2232 ct->c_fp = NULL;
2233 }
2234 return NOTOK;
2235 }
2236
2237
2238 /*
2239 * External
2240 */
2241
2242 static int
2243 openExternal (CT ct, CT cb, CE ce, char **file, int *fd)
2244 {
2245 char cachefile[BUFSIZ];
2246
2247 if (ce->ce_fp) {
2248 fseek (ce->ce_fp, 0L, SEEK_SET);
2249 goto ready_already;
2250 }
2251
2252 if (ce->ce_file) {
2253 if ((ce->ce_fp = fopen (ce->ce_file, "r")) == NULL) {
2254 content_error (ce->ce_file, ct, "unable to fopen for reading");
2255 return NOTOK;
2256 }
2257 goto ready_already;
2258 }
2259
2260 if (find_cache (ct, rcachesw, (int *) 0, cb->c_id,
2261 cachefile, sizeof(cachefile)) != NOTOK) {
2262 if ((ce->ce_fp = fopen (cachefile, "r"))) {
2263 ce->ce_file = getcpy (cachefile);
2264 ce->ce_unlink = 0;
2265 goto ready_already;
2266 } else {
2267 admonish (cachefile, "unable to fopen for reading");
2268 }
2269 }
2270
2271 return OK;
2272
2273 ready_already:
2274 *file = ce->ce_file;
2275 *fd = fileno (ce->ce_fp);
2276 return DONE;
2277 }
2278
2279 /*
2280 * File
2281 */
2282
2283 static int
2284 InitFile (CT ct)
2285 {
2286 return init_encoding (ct, openFile);
2287 }
2288
2289
2290 static int
2291 openFile (CT ct, char **file)
2292 {
2293 int fd, cachetype;
2294 char cachefile[BUFSIZ];
2295 struct exbody *e = ct->c_ctexbody;
2296 CE ce = &ct->c_cefile;
2297
2298 switch (openExternal (e->eb_parent, e->eb_content, ce, file, &fd)) {
2299 case NOTOK:
2300 return NOTOK;
2301
2302 case OK:
2303 break;
2304
2305 case DONE:
2306 return fd;
2307 }
2308
2309 if (!e->eb_name) {
2310 content_error (NULL, ct, "missing name parameter");
2311 return NOTOK;
2312 }
2313
2314 ce->ce_file = getcpy (e->eb_name);
2315 ce->ce_unlink = 0;
2316
2317 if ((ce->ce_fp = fopen (ce->ce_file, "r")) == NULL) {
2318 content_error (ce->ce_file, ct, "unable to fopen for reading");
2319 return NOTOK;
2320 }
2321
2322 if ((!e->eb_permission || strcasecmp (e->eb_permission, "read-write"))
2323 && find_cache (NULL, wcachesw, &cachetype, e->eb_content->c_id,
2324 cachefile, sizeof(cachefile)) != NOTOK) {
2325 int mask;
2326 FILE *fp;
2327
2328 mask = umask (cachetype ? ~m_gmprot () : 0222);
2329 if ((fp = fopen (cachefile, "w"))) {
2330 int cc;
2331 char buffer[BUFSIZ];
2332 FILE *gp = ce->ce_fp;
2333
2334 fseek (gp, 0L, SEEK_SET);
2335
2336 while ((cc = fread (buffer, sizeof(*buffer), sizeof(buffer), gp))
2337 > 0)
2338 fwrite (buffer, sizeof(*buffer), cc, fp);
2339 fflush (fp);
2340
2341 if (ferror (gp)) {
2342 admonish (ce->ce_file, "error reading");
2343 unlink (cachefile);
2344 }
2345 else
2346 if (ferror (fp)) {
2347 admonish (cachefile, "error writing");
2348 unlink (cachefile);
2349 }
2350 fclose (fp);
2351 }
2352 umask (mask);
2353 }
2354
2355 fseek (ce->ce_fp, 0L, SEEK_SET);
2356 *file = ce->ce_file;
2357 return fileno (ce->ce_fp);
2358 }
2359
2360 /*
2361 * FTP
2362 */
2363
2364 static int
2365 InitFTP (CT ct)
2366 {
2367 return init_encoding (ct, openFTP);
2368 }
2369
2370
2371 static int
2372 openFTP (CT ct, char **file)
2373 {
2374 int cachetype, caching, fd;
2375 int len, buflen;
2376 char *bp, *ftp, *user, *pass;
2377 char buffer[BUFSIZ], cachefile[BUFSIZ];
2378 struct exbody *e;
2379 CE ce = &ct->c_cefile;
2380 static char *username = NULL;
2381 static char *password = NULL;
2382
2383 e = ct->c_ctexbody;
2384
2385 if ((ftp = context_find (nmhaccessftp)) && !*ftp)
2386 ftp = NULL;
2387
2388 if (!ftp)
2389 return NOTOK;
2390
2391 switch (openExternal (e->eb_parent, e->eb_content, ce, file, &fd)) {
2392 case NOTOK:
2393 return NOTOK;
2394
2395 case OK:
2396 break;
2397
2398 case DONE:
2399 return fd;
2400 }
2401
2402 if (!e->eb_name || !e->eb_site) {
2403 content_error (NULL, ct, "missing %s parameter",
2404 e->eb_name ? "site": "name");
2405 return NOTOK;
2406 }
2407
2408 if (xpid) {
2409 if (xpid < 0)
2410 xpid = -xpid;
2411 pidcheck (pidwait (xpid, NOTOK));
2412 xpid = 0;
2413 }
2414
2415 /* Get the buffer ready to go */
2416 bp = buffer;
2417 buflen = sizeof(buffer);
2418
2419 /*
2420 * Construct the query message for user
2421 */
2422 snprintf (bp, buflen, "Retrieve %s", e->eb_name);
2423 len = strlen (bp);
2424 bp += len;
2425 buflen -= len;
2426
2427 if (e->eb_partno) {
2428 snprintf (bp, buflen, " (content %s)", e->eb_partno);
2429 len = strlen (bp);
2430 bp += len;
2431 buflen -= len;
2432 }
2433
2434 snprintf (bp, buflen, "\n using %sFTP from site %s",
2435 e->eb_flags ? "anonymous " : "", e->eb_site);
2436 len = strlen (bp);
2437 bp += len;
2438 buflen -= len;
2439
2440 if (e->eb_size > 0) {
2441 snprintf (bp, buflen, " (%lu octets)", e->eb_size);
2442 len = strlen (bp);
2443 bp += len;
2444 buflen -= len;
2445 }
2446 snprintf (bp, buflen, "? ");
2447
2448 /*
2449 * Now, check the answer
2450 */
2451 if (!getanswer (buffer))
2452 return NOTOK;
2453
2454 if (e->eb_flags) {
2455 user = "anonymous";
2456 snprintf (buffer, sizeof(buffer), "%s@%s", getusername (),
2457 LocalName (1));
2458 pass = buffer;
2459 } else {
2460 ruserpass (e->eb_site, &username, &password);
2461 user = username;
2462 pass = password;
2463 }
2464
2465 ce->ce_unlink = (*file == NULL);
2466 caching = 0;
2467 cachefile[0] = '\0';
2468 if ((!e->eb_permission || strcasecmp (e->eb_permission, "read-write"))
2469 && find_cache (NULL, wcachesw, &cachetype, e->eb_content->c_id,
2470 cachefile, sizeof(cachefile)) != NOTOK) {
2471 if (*file == NULL) {
2472 ce->ce_unlink = 0;
2473 caching = 1;
2474 }
2475 }
2476
2477 if (*file)
2478 ce->ce_file = add (*file, NULL);
2479 else if (caching)
2480 ce->ce_file = add (cachefile, NULL);
2481 else {
2482 char *tempfile;
2483 if ((tempfile = m_mktemp2(NULL, invo_name, NULL, NULL)) == NULL) {
2484 adios(NULL, "unable to create temporary file in %s",
2485 get_temp_dir());
2486 }
2487 ce->ce_file = add (tempfile, NULL);
2488 }
2489
2490 if ((ce->ce_fp = fopen (ce->ce_file, "w+")) == NULL) {
2491 content_error (ce->ce_file, ct, "unable to fopen for reading/writing");
2492 return NOTOK;
2493 }
2494
2495 {
2496 int child_id, i, vecp;
2497 char *vec[9];
2498
2499 vecp = 0;
2500 vec[vecp++] = r1bindex (ftp, '/');
2501 vec[vecp++] = e->eb_site;
2502 vec[vecp++] = user;
2503 vec[vecp++] = pass;
2504 vec[vecp++] = e->eb_dir;
2505 vec[vecp++] = e->eb_name;
2506 vec[vecp++] = ce->ce_file,
2507 vec[vecp++] = e->eb_mode && !strcasecmp (e->eb_mode, "ascii")
2508 ? "ascii" : "binary";
2509 vec[vecp] = NULL;
2510
2511 fflush (stdout);
2512
2513 for (i = 0; (child_id = fork()) == NOTOK && i < 5; i++)
2514 sleep (5);
2515 switch (child_id) {
2516 case NOTOK:
2517 adios ("fork", "unable to");
2518 /* NOTREACHED */
2519
2520 case OK:
2521 close (fileno (ce->ce_fp));
2522 execvp (ftp, vec);
2523 fprintf (stderr, "unable to exec ");
2524 perror (ftp);
2525 _exit (-1);
2526 /* NOTREACHED */
2527
2528 default:
2529 if (pidXwait (child_id, NULL)) {
2530 username = password = NULL;
2531 ce->ce_unlink = 1;
2532 return NOTOK;
2533 }
2534 break;
2535 }
2536 }
2537
2538 if (cachefile[0]) {
2539 if (caching)
2540 chmod (cachefile, cachetype ? m_gmprot () : 0444);
2541 else {
2542 int mask;
2543 FILE *fp;
2544
2545 mask = umask (cachetype ? ~m_gmprot () : 0222);
2546 if ((fp = fopen (cachefile, "w"))) {
2547 int cc;
2548 FILE *gp = ce->ce_fp;
2549
2550 fseek (gp, 0L, SEEK_SET);
2551
2552 while ((cc= fread (buffer, sizeof(*buffer), sizeof(buffer), gp))
2553 > 0)
2554 fwrite (buffer, sizeof(*buffer), cc, fp);
2555 fflush (fp);
2556
2557 if (ferror (gp)) {
2558 admonish (ce->ce_file, "error reading");
2559 unlink (cachefile);
2560 }
2561 else
2562 if (ferror (fp)) {
2563 admonish (cachefile, "error writing");
2564 unlink (cachefile);
2565 }
2566 fclose (fp);
2567 }
2568 umask (mask);
2569 }
2570 }
2571
2572 fseek (ce->ce_fp, 0L, SEEK_SET);
2573 *file = ce->ce_file;
2574 return fileno (ce->ce_fp);
2575 }
2576
2577
2578 /*
2579 * Mail
2580 */
2581
2582 static int
2583 InitMail (CT ct)
2584 {
2585 return init_encoding (ct, openMail);
2586 }
2587
2588
2589 static int
2590 openMail (CT ct, char **file)
2591 {
2592 int child_id, fd, i, vecp;
2593 int len, buflen;
2594 char *bp, buffer[BUFSIZ], *vec[7];
2595 struct exbody *e = ct->c_ctexbody;
2596 CE ce = &ct->c_cefile;
2597
2598 switch (openExternal (e->eb_parent, e->eb_content, ce, file, &fd)) {
2599 case NOTOK:
2600 return NOTOK;
2601
2602 case OK:
2603 break;
2604
2605 case DONE:
2606 return fd;
2607 }
2608
2609 if (!e->eb_server) {
2610 content_error (NULL, ct, "missing server parameter");
2611 return NOTOK;
2612 }
2613
2614 if (xpid) {
2615 if (xpid < 0)
2616 xpid = -xpid;
2617 pidcheck (pidwait (xpid, NOTOK));
2618 xpid = 0;
2619 }
2620
2621 /* Get buffer ready to go */
2622 bp = buffer;
2623 buflen = sizeof(buffer);
2624
2625 /* Now, construct query message */
2626 snprintf (bp, buflen, "Retrieve content");
2627 len = strlen (bp);
2628 bp += len;
2629 buflen -= len;
2630
2631 if (e->eb_partno) {
2632 snprintf (bp, buflen, " %s", e->eb_partno);
2633 len = strlen (bp);
2634 bp += len;
2635 buflen -= len;
2636 }
2637
2638 snprintf (bp, buflen, " by asking %s\n\n%s\n? ",
2639 e->eb_server,
2640 e->eb_subject ? e->eb_subject : e->eb_body);
2641
2642 /* Now, check answer */
2643 if (!getanswer (buffer))
2644 return NOTOK;
2645
2646 vecp = 0;
2647 vec[vecp++] = r1bindex (mailproc, '/');
2648 vec[vecp++] = e->eb_server;
2649 vec[vecp++] = "-subject";
2650 vec[vecp++] = e->eb_subject ? e->eb_subject : "mail-server request";
2651 vec[vecp++] = "-body";
2652 vec[vecp++] = e->eb_body;
2653 vec[vecp] = NULL;
2654
2655 for (i = 0; (child_id = fork()) == NOTOK && i < 5; i++)
2656 sleep (5);
2657 switch (child_id) {
2658 case NOTOK:
2659 advise ("fork", "unable to");
2660 return NOTOK;
2661
2662 case OK:
2663 execvp (mailproc, vec);
2664 fprintf (stderr, "unable to exec ");
2665 perror (mailproc);
2666 _exit (-1);
2667 /* NOTREACHED */
2668
2669 default:
2670 if (pidXwait (child_id, NULL) == OK)
2671 advise (NULL, "request sent");
2672 break;
2673 }
2674
2675 if (*file == NULL) {
2676 char *tempfile;
2677 if ((tempfile = m_mktemp2(NULL, invo_name, NULL, NULL)) == NULL) {
2678 adios(NULL, "unable to create temporary file in %s",
2679 get_temp_dir());
2680 }
2681 ce->ce_file = add (tempfile, NULL);
2682 ce->ce_unlink = 1;
2683 } else {
2684 ce->ce_file = add (*file, NULL);
2685 ce->ce_unlink = 0;
2686 }
2687
2688 if ((ce->ce_fp = fopen (ce->ce_file, "w+")) == NULL) {
2689 content_error (ce->ce_file, ct, "unable to fopen for reading/writing");
2690 return NOTOK;
2691 }
2692
2693 /* showproc is for mhshow and mhstore, though mhlist -debug
2694 * prints it, too. */
2695 if (ct->c_showproc)
2696 free (ct->c_showproc);
2697 ct->c_showproc = add ("true", NULL);
2698
2699 fseek (ce->ce_fp, 0L, SEEK_SET);
2700 *file = ce->ce_file;
2701 return fileno (ce->ce_fp);
2702 }
2703
2704
2705 /*
2706 * URL
2707 */
2708
2709 static int
2710 InitURL (CT ct)
2711 {
2712 return init_encoding (ct, openURL);
2713 }
2714
2715
2716 static int
2717 openURL (CT ct, char **file)
2718 {
2719 struct exbody *e = ct->c_ctexbody;
2720 CE ce = &ct->c_cefile;
2721 char *urlprog, *program;
2722 char buffer[BUFSIZ], cachefile[BUFSIZ];
2723 int fd, caching, cachetype;
2724 struct msgs_array args = { 0, 0, NULL};
2725 pid_t child_id;
2726
2727 if ((urlprog = context_find(nmhaccessurl)) && *urlprog == '\0')
2728 urlprog = NULL;
2729
2730 if (! urlprog) {
2731 content_error(NULL, ct, "No entry for nmh-access-url in profile");
2732 return NOTOK;
2733 }
2734
2735 switch (openExternal(e->eb_parent, e->eb_content, ce, file, &fd)) {
2736 case NOTOK:
2737 return NOTOK;
2738
2739 case OK:
2740 break;
2741
2742 case DONE:
2743 return fd;
2744 }
2745
2746 if (!e->eb_url) {
2747 content_error(NULL, ct, "missing url parameter");
2748 return NOTOK;
2749 }
2750
2751 if (xpid) {
2752 if (xpid < 0)
2753 xpid = -xpid;
2754 pidcheck (pidwait (xpid, NOTOK));
2755 xpid = 0;
2756 }
2757
2758 ce->ce_unlink = (*file == NULL);
2759 caching = 0;
2760 cachefile[0] = '\0';
2761
2762 if (find_cache(NULL, wcachesw, &cachetype, e->eb_content->c_id,
2763 cachefile, sizeof(cachefile)) != NOTOK) {
2764 if (*file == NULL) {
2765 ce->ce_unlink = 0;
2766 caching = 1;
2767 }
2768 }
2769
2770 if (*file)
2771 ce->ce_file = add(*file, NULL);
2772 else if (caching)
2773 ce->ce_file = add(cachefile, NULL);
2774 else {
2775 char *tempfile;
2776 if ((tempfile = m_mktemp2(NULL, invo_name, NULL, NULL)) == NULL) {
2777 adios(NULL, "unable to create temporary file in %s",
2778 get_temp_dir());
2779 }
2780 ce->ce_file = add (tempfile, NULL);
2781 }
2782
2783 if ((ce->ce_fp = fopen(ce->ce_file, "w+")) == NULL) {
2784 content_error(ce->ce_file, ct, "unable to fopen for read/writing");
2785 return NOTOK;
2786 }
2787
2788 switch (child_id = fork()) {
2789 case NOTOK:
2790 adios ("fork", "unable to");
2791 /* NOTREACHED */
2792
2793 case OK:
2794 argsplit_msgarg(&args, urlprog, &program);
2795 app_msgarg(&args, e->eb_url);
2796 app_msgarg(&args, NULL);
2797 dup2(fileno(ce->ce_fp), 1);
2798 close(fileno(ce->ce_fp));
2799 execvp(program, args.msgs);
2800 fprintf(stderr, "Unable to exec ");
2801 perror(program);
2802 _exit(-1);
2803 /* NOTREACHED */
2804
2805 default:
2806 if (pidXwait(child_id, NULL)) {
2807 ce->ce_unlink = 1;
2808 return NOTOK;
2809 }
2810 }
2811
2812 if (cachefile[0]) {
2813 if (caching)
2814 chmod(cachefile, cachetype ? m_gmprot() : 0444);
2815 else {
2816 int mask;
2817 FILE *fp;
2818
2819 mask = umask (cachetype ? ~m_gmprot() : 0222);
2820 if ((fp = fopen(cachefile, "w"))) {
2821 int cc;
2822 FILE *gp = ce->ce_fp;
2823
2824 fseeko(gp, 0, SEEK_SET);
2825
2826 while ((cc = fread(buffer, sizeof(*buffer),
2827 sizeof(buffer), gp)) > 0)
2828 fwrite(buffer, sizeof(*buffer), cc, fp);
2829
2830 fflush(fp);
2831
2832 if (ferror(gp)) {
2833 admonish(ce->ce_file, "error reading");
2834 unlink(cachefile);
2835 }
2836 }
2837 umask(mask);
2838 }
2839 }
2840
2841 fseeko(ce->ce_fp, 0, SEEK_SET);
2842 *file = ce->ce_file;
2843 return fd;
2844 }
2845
2846 static int
2847 readDigest (CT ct, char *cp)
2848 {
2849 int bitno, skip;
2850 uint32_t bits;
2851 char *bp = cp;
2852 unsigned char *dp, value, *ep;
2853
2854 bitno = 18;
2855 bits = 0L;
2856 skip = 0;
2857
2858 for (ep = (dp = ct->c_digest)
2859 + sizeof(ct->c_digest) / sizeof(ct->c_digest[0]); *cp; cp++)
2860 switch (*cp) {
2861 default:
2862 if (skip
2863 || (*cp & 0x80)
2864 || (value = b642nib[*cp & 0x7f]) > 0x3f) {
2865 if (debugsw)
2866 fprintf (stderr, "invalid BASE64 encoding\n");
2867 return NOTOK;
2868 }
2869
2870 bits |= value << bitno;
2871 test_end:
2872 if ((bitno -= 6) < 0) {
2873 if (dp + (3 - skip) > ep)
2874 goto invalid_digest;
2875 *dp++ = (bits >> 16) & 0xff;
2876 if (skip < 2) {
2877 *dp++ = (bits >> 8) & 0xff;
2878 if (skip < 1)
2879 *dp++ = bits & 0xff;
2880 }
2881 bitno = 18;
2882 bits = 0L;
2883 skip = 0;
2884 }
2885 break;
2886
2887 case '=':
2888 if (++skip > 3)
2889 goto self_delimiting;
2890 goto test_end;
2891 }
2892 if (bitno != 18) {
2893 if (debugsw)
2894 fprintf (stderr, "premature ending (bitno %d)\n", bitno);
2895
2896 return NOTOK;
2897 }
2898 self_delimiting:
2899 if (dp != ep) {
2900 invalid_digest:
2901 if (debugsw) {
2902 while (*cp)
2903 cp++;
2904 fprintf (stderr, "invalid MD5 digest (got %d octets)\n",
2905 (int)(cp - bp));
2906 }
2907
2908 return NOTOK;
2909 }
2910
2911 if (debugsw) {
2912 fprintf (stderr, "MD5 digest=");
2913 for (dp = ct->c_digest; dp < ep; dp++)
2914 fprintf (stderr, "%02x", *dp & 0xff);
2915 fprintf (stderr, "\n");
2916 }
2917
2918 return OK;
2919 }
2920
2921
2922 /* Multipart parts might have content before the first subpart and/or
2923 after the last subpart that hasn't been stored anywhere else, so do
2924 that. */
2925 int
2926 get_leftover_mp_content (CT ct, int before /* or after */) {
2927 struct multipart *m = (struct multipart *) ct->c_ctparams;
2928 char *boundary;
2929 int found_boundary = 0;
2930 char buffer[BUFSIZ];
2931 int max = BUFSIZ;
2932 int read = 0;
2933 char *content = NULL;
2934
2935 if (! m) return NOTOK;
2936
2937 if (before) {
2938 if (! m->mp_parts || ! m->mp_parts->mp_part) return NOTOK;
2939
2940 /* Isolate the beginning of this part to the beginning of the
2941 first subpart and save any content between them. */
2942 fseeko (ct->c_fp, ct->c_begin, SEEK_SET);
2943 max = m->mp_parts->mp_part->c_begin - ct->c_begin;
2944 boundary = concat ("--", m->mp_start, NULL);
2945 } else {
2946 struct part *last_subpart = NULL;
2947 struct part *subpart;
2948
2949 /* Go to the last subpart to get its end position. */
2950 for (subpart = m->mp_parts; subpart; subpart = subpart->mp_next) {
2951 last_subpart = subpart;
2952 }
2953
2954 if (last_subpart == NULL) return NOTOK;
2955
2956 /* Isolate the end of the last subpart to the end of this part
2957 and save any content between them. */
2958 fseeko (ct->c_fp, last_subpart->mp_part->c_end, SEEK_SET);
2959 max = ct->c_end - last_subpart->mp_part->c_end;
2960 boundary = concat ("--", m->mp_stop, NULL);
2961 }
2962
2963 /* Back up by 1 to pick up the newline. */
2964 while (fgets (buffer, sizeof(buffer) - 1, ct->c_fp)) {
2965 read += strlen (buffer);
2966 /* Don't look beyond beginning of first subpart (before) or
2967 next part (after). */
2968 if (read > max) buffer[read-max] = '\0';
2969
2970 if (before) {
2971 if (! strcmp (buffer, boundary)) {
2972 found_boundary = 1;
2973 }
2974 } else {
2975 if (! found_boundary && ! strcmp (buffer, boundary)) {
2976 found_boundary = 1;
2977 continue;
2978 }
2979 }
2980
2981 if ((before && ! found_boundary) || (! before && found_boundary)) {
2982 if (content) {
2983 char *old_content = content;
2984 content = concat (content, buffer, NULL);
2985 free (old_content);
2986 } else {
2987 content = before
2988 ? concat ("\n", buffer, NULL)
2989 : concat (buffer, NULL);
2990 }
2991 }
2992
2993 if (before) {
2994 if (found_boundary || read > max) break;
2995 } else {
2996 if (read > max) break;
2997 }
2998 }
2999
3000 /* Skip the newline if that's all there is. */
3001 if (content) {
3002 char *cp;
3003
3004 /* Remove trailing newline, except at EOF. */
3005 if ((before || ! feof (ct->c_fp)) &&
3006 (cp = content + strlen (content)) > content &&
3007 *--cp == '\n') {
3008 *cp = '\0';
3009 }
3010
3011 if (strlen (content) > 1) {
3012 if (before) {
3013 m->mp_content_before = content;
3014 } else {
3015 m->mp_content_after = content;
3016 }
3017 } else {
3018 free (content);
3019 }
3020 }
3021
3022 free (boundary);
3023
3024 return OK;
3025 }
3026
3027
3028 char *
3029 ct_type_str (int type) {
3030 switch (type) {
3031 case CT_APPLICATION:
3032 return "application";
3033 case CT_AUDIO:
3034 return "audio";
3035 case CT_IMAGE:
3036 return "image";
3037 case CT_MESSAGE:
3038 return "message";
3039 case CT_MULTIPART:
3040 return "multipart";
3041 case CT_TEXT:
3042 return "text";
3043 case CT_VIDEO:
3044 return "video";
3045 case CT_EXTENSION:
3046 return "extension";
3047 default:
3048 return "unknown_type";
3049 }
3050 }
3051
3052
3053 char *
3054 ct_subtype_str (int type, int subtype) {
3055 switch (type) {
3056 case CT_APPLICATION:
3057 switch (subtype) {
3058 case APPLICATION_OCTETS:
3059 return "octets";
3060 case APPLICATION_POSTSCRIPT:
3061 return "postscript";
3062 default:
3063 return "unknown_app_subtype";
3064 }
3065 case CT_MESSAGE:
3066 switch (subtype) {
3067 case MESSAGE_RFC822:
3068 return "rfc822";
3069 case MESSAGE_PARTIAL:
3070 return "partial";
3071 case MESSAGE_EXTERNAL:
3072 return "external";
3073 default:
3074 return "unknown_msg_subtype";
3075 }
3076 case CT_MULTIPART:
3077 switch (subtype) {
3078 case MULTI_MIXED:
3079 return "mixed";
3080 case MULTI_ALTERNATE:
3081 return "alternative";
3082 case MULTI_DIGEST:
3083 return "digest";
3084 case MULTI_PARALLEL:
3085 return "parallel";
3086 default:
3087 return "unknown_multipart_subtype";
3088 }
3089 case CT_TEXT:
3090 switch (subtype) {
3091 case TEXT_PLAIN:
3092 return "plain";
3093 case TEXT_RICHTEXT:
3094 return "richtext";
3095 case TEXT_ENRICHED:
3096 return "enriched";
3097 default:
3098 return "unknown_text_subtype";
3099 }
3100 default:
3101 return "unknown_type";
3102 }
3103 }
3104
3105
3106 /* Find the content type and InitFunc for the CT. */
3107 const struct str2init *
3108 get_ct_init (int type) {
3109 const struct str2init *sp;
3110
3111 for (sp = str2cts; sp->si_key; ++sp) {
3112 if (type == sp->si_val) {
3113 return sp;
3114 }
3115 }
3116
3117 return NULL;
3118 }
3119
3120 const char *
3121 ce_str (int encoding) {
3122 switch (encoding) {
3123 case CE_BASE64:
3124 return "base64";
3125 case CE_QUOTED:
3126 return "quoted-printable";
3127 case CE_8BIT:
3128 return "8bit";
3129 case CE_7BIT:
3130 return "7bit";
3131 case CE_BINARY:
3132 return "binary";
3133 case CE_EXTENSION:
3134 return "extension";
3135 case CE_EXTERNAL:
3136 return "external";
3137 default:
3138 return "unknown";
3139 }
3140 }
3141
3142 /* Find the content type and InitFunc for the content encoding method. */
3143 const struct str2init *
3144 get_ce_method (const char *method) {
3145 struct str2init *sp;
3146
3147 for (sp = str2ces; sp->si_key; ++sp) {
3148 if (! strcasecmp (method, sp->si_key)) {
3149 return sp;
3150 }
3151 }
3152
3153 return NULL;
3154 }
3155
3156 int
3157 parse_header_attrs (const char *filename, int len, char **header_attrp, CI ci,
3158 int *status) {
3159 char **attr = ci->ci_attrs;
3160 char *cp = *header_attrp;
3161
3162 while (*cp == ';') {
3163 char *dp, *vp, *up, c;
3164
3165 /* Relies on knowledge of this declaration:
3166 * char *ci_attrs[NPARMS + 2];
3167 */
3168 if (attr >= ci->ci_attrs + sizeof ci->ci_attrs/sizeof (char *) - 2) {
3169 advise (NULL,
3170 "too many parameters in message %s's %s: field (%d max)",
3171 filename, TYPE_FIELD, NPARMS);
3172 *status = NOTOK;
3173 return NOTOK;
3174 }
3175
3176 cp++;
3177 while (isspace ((unsigned char) *cp))
3178 cp++;
3179
3180 if (*cp == '(' &&
3181 get_comment (filename, ci, &cp, 1) == NOTOK) {
3182 *status = NOTOK;
3183 return NOTOK;
3184 }
3185
3186 if (*cp == 0) {
3187 advise (NULL,
3188 "extraneous trailing ';' in message %s's %s: "
3189 "parameter list",
3190 filename, TYPE_FIELD);
3191 *status = OK;
3192 return NOTOK;
3193 }
3194
3195 /* down case the attribute name */
3196 for (dp = cp; istoken ((unsigned char) *dp); dp++)
3197 if (isalpha((unsigned char) *dp) && isupper ((unsigned char) *dp))
3198 *dp = tolower ((unsigned char) *dp);
3199
3200 for (up = dp; isspace ((unsigned char) *dp);)
3201 dp++;
3202 if (dp == cp || *dp != '=') {
3203 advise (NULL,
3204 "invalid parameter in message %s's %s: "
3205 "field\n%*.*sparameter %s (error detected at offset %d)",
3206 filename, TYPE_FIELD, len, len, "", cp, dp - cp);
3207 *status = NOTOK;
3208 return NOTOK;
3209 }
3210
3211 vp = (*attr = add (cp, NULL)) + (up - cp);
3212 *vp = '\0';
3213 for (dp++; isspace ((unsigned char) *dp);)
3214 dp++;
3215
3216 /* Now store the attribute value. */
3217 ci->ci_values[attr - ci->ci_attrs] = vp = *attr + (dp - cp);
3218
3219 if (*dp == '"') {
3220 for (cp = ++dp, dp = vp;;) {
3221 switch (c = *cp++) {
3222 case '\0':
3223 bad_quote:
3224 advise (NULL,
3225 "invalid quoted-string in message %s's %s: "
3226 "field\n%*.*s(parameter %s)",
3227 filename, TYPE_FIELD, len, len, "", *attr);
3228 *status = NOTOK;
3229 return NOTOK;
3230
3231 case '\\':
3232 *dp++ = c;
3233 if ((c = *cp++) == '\0')
3234 goto bad_quote;
3235 /* else fall... */
3236
3237 default:
3238 *dp++ = c;
3239 continue;
3240
3241 case '"':
3242 *dp = '\0';
3243 break;
3244 }
3245 break;
3246 }
3247 } else {
3248 for (cp = dp, dp = vp; istoken (*cp); cp++, dp++)
3249 continue;
3250 *dp = '\0';
3251 }
3252 if (!*vp) {
3253 advise (NULL,
3254 "invalid parameter in message %s's %s: "
3255 "field\n%*.*s(parameter %s)",
3256 filename, TYPE_FIELD, len, len, "", *attr);
3257 *status = NOTOK;
3258 return NOTOK;
3259 }
3260
3261 while (isspace ((unsigned char) *cp))
3262 cp++;
3263
3264 if (*cp == '(' &&
3265 get_comment (filename, ci, &cp, 1) == NOTOK) {
3266 *status = NOTOK;
3267 return NOTOK;
3268 }
3269
3270 ++attr;
3271 }
3272
3273 *header_attrp = cp;
3274 return OK;
3275 }