]> diplodocus.org Git - nmh/blob - uip/mhparse.c
9489a8e4c01840ac64bb3c8dc459998954ecd0e7
[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 /* Temporary file already exists, so we rename to
1674 version with extension. */
1675 char *file_org = strdup(ce->ce_file);
1676 ce->ce_file = add (cp, ce->ce_file);
1677 if (rename(file_org, ce->ce_file)) {
1678 adios (ce->ce_file, "unable to rename %s to ", file_org);
1679 }
1680 free(file_org);
1681
1682 } else {
1683 ce->ce_file = add (cp, ce->ce_file);
1684 }
1685 }
1686
1687 if ((ce->ce_fp = fopen (ce->ce_file, "w+")) == NULL) {
1688 content_error (ce->ce_file, ct, "unable to fopen for reading/writing");
1689 return NOTOK;
1690 }
1691
1692 if ((len = ct->c_end - ct->c_begin) < 0)
1693 adios (NULL, "internal error(1)");
1694
1695 if (! ct->c_fp) {
1696 if ((ct->c_fp = fopen (ct->c_file, "r")) == NULL) {
1697 content_error (ct->c_file, ct, "unable to open for reading");
1698 return NOTOK;
1699 }
1700 own_ct_fp = 1;
1701 }
1702
1703 if ((digested = ct->c_digested))
1704 MD5Init (&mdContext);
1705
1706 bitno = 18;
1707 bits = 0L;
1708 skip = 0;
1709
1710 lseek (fd = fileno (ct->c_fp), (off_t) ct->c_begin, SEEK_SET);
1711 while (len > 0) {
1712 switch (cc = read (fd, buffer, sizeof(buffer) - 1)) {
1713 case NOTOK:
1714 content_error (ct->c_file, ct, "error reading from");
1715 goto clean_up;
1716
1717 case OK:
1718 content_error (NULL, ct, "premature eof");
1719 goto clean_up;
1720
1721 default:
1722 if (cc > len)
1723 cc = len;
1724 len -= cc;
1725
1726 for (ep = (cp = buffer) + cc; cp < ep; cp++) {
1727 switch (*cp) {
1728 default:
1729 if (isspace ((unsigned char) *cp))
1730 break;
1731 if (skip || (((unsigned char) *cp) & 0x80)
1732 || (value = b642nib[((unsigned char) *cp) & 0x7f]) > 0x3f) {
1733 if (debugsw) {
1734 fprintf (stderr, "*cp=0x%x pos=%ld skip=%d\n",
1735 (unsigned char) *cp,
1736 (long) (lseek (fd, (off_t) 0, SEEK_CUR) - (ep - cp)),
1737 skip);
1738 }
1739 content_error (NULL, ct,
1740 "invalid BASE64 encoding -- continuing");
1741 continue;
1742 }
1743
1744 bits |= value << bitno;
1745 test_end:
1746 if ((bitno -= 6) < 0) {
1747 b = (bits >> 16) & 0xff;
1748 putc ((char) b, ce->ce_fp);
1749 if (digested)
1750 MD5Update (&mdContext, &b, 1);
1751 if (skip < 2) {
1752 b = (bits >> 8) & 0xff;
1753 putc ((char) b, ce->ce_fp);
1754 if (digested)
1755 MD5Update (&mdContext, &b, 1);
1756 if (skip < 1) {
1757 b = bits & 0xff;
1758 putc ((char) b, ce->ce_fp);
1759 if (digested)
1760 MD5Update (&mdContext, &b, 1);
1761 }
1762 }
1763
1764 if (ferror (ce->ce_fp)) {
1765 content_error (ce->ce_file, ct,
1766 "error writing to");
1767 goto clean_up;
1768 }
1769 bitno = 18, bits = 0L, skip = 0;
1770 }
1771 break;
1772
1773 case '=':
1774 if (++skip > 3)
1775 goto self_delimiting;
1776 goto test_end;
1777 }
1778 }
1779 }
1780 }
1781
1782 if (bitno != 18) {
1783 if (debugsw)
1784 fprintf (stderr, "premature ending (bitno %d)\n", bitno);
1785
1786 content_error (NULL, ct, "invalid BASE64 encoding");
1787 goto clean_up;
1788 }
1789
1790 self_delimiting:
1791 fseek (ct->c_fp, 0L, SEEK_SET);
1792
1793 if (fflush (ce->ce_fp)) {
1794 content_error (ce->ce_file, ct, "error writing to");
1795 goto clean_up;
1796 }
1797
1798 if (digested) {
1799 unsigned char digest[16];
1800
1801 MD5Final (digest, &mdContext);
1802 if (memcmp((char *) digest, (char *) ct->c_digest,
1803 sizeof(digest) / sizeof(digest[0])))
1804 content_error (NULL, ct,
1805 "content integrity suspect (digest mismatch) -- continuing");
1806 else
1807 if (debugsw)
1808 fprintf (stderr, "content integrity confirmed\n");
1809 }
1810
1811 fseek (ce->ce_fp, 0L, SEEK_SET);
1812
1813 ready_to_go:
1814 *file = ce->ce_file;
1815 if (own_ct_fp) {
1816 fclose (ct->c_fp);
1817 ct->c_fp = NULL;
1818 }
1819 return fileno (ce->ce_fp);
1820
1821 clean_up:
1822 if (own_ct_fp) {
1823 fclose (ct->c_fp);
1824 ct->c_fp = NULL;
1825 }
1826 free_encoding (ct, 0);
1827 return NOTOK;
1828 }
1829
1830
1831 /*
1832 * QUOTED PRINTABLE
1833 */
1834
1835 static char hex2nib[0x80] = {
1836 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1837 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1838 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 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 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
1843 0x08, 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1844 0x00, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x00,
1845 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1846 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1847 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1848 0x00, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x00,
1849 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1850 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1851 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
1852 };
1853
1854
1855 static int
1856 InitQuoted (CT ct)
1857 {
1858 return init_encoding (ct, openQuoted);
1859 }
1860
1861
1862 static int
1863 openQuoted (CT ct, char **file)
1864 {
1865 int cc, digested, len, quoted, own_ct_fp = 0;
1866 char *cp, *ep;
1867 char buffer[BUFSIZ];
1868 unsigned char mask;
1869 CE ce = &ct->c_cefile;
1870 /* sbeck -- handle suffixes */
1871 CI ci;
1872 MD5_CTX mdContext;
1873
1874 if (ce->ce_fp) {
1875 fseek (ce->ce_fp, 0L, SEEK_SET);
1876 goto ready_to_go;
1877 }
1878
1879 if (ce->ce_file) {
1880 if ((ce->ce_fp = fopen (ce->ce_file, "r")) == NULL) {
1881 content_error (ce->ce_file, ct, "unable to fopen for reading");
1882 return NOTOK;
1883 }
1884 goto ready_to_go;
1885 }
1886
1887 if (*file == NULL) {
1888 char *tempfile;
1889 if ((tempfile = m_mktemp2(NULL, invo_name, NULL, NULL)) == NULL) {
1890 adios(NULL, "unable to create temporary file in %s",
1891 get_temp_dir());
1892 }
1893 ce->ce_file = add (tempfile, NULL);
1894 ce->ce_unlink = 1;
1895 } else {
1896 ce->ce_file = add (*file, NULL);
1897 ce->ce_unlink = 0;
1898 }
1899
1900 /* sbeck@cise.ufl.edu -- handle suffixes */
1901 ci = &ct->c_ctinfo;
1902 snprintf (buffer, sizeof(buffer), "%s-suffix-%s/%s",
1903 invo_name, ci->ci_type, ci->ci_subtype);
1904 cp = context_find (buffer);
1905 if (cp == NULL || *cp == '\0') {
1906 snprintf (buffer, sizeof(buffer), "%s-suffix-%s", invo_name,
1907 ci->ci_type);
1908 cp = context_find (buffer);
1909 }
1910 if (cp != NULL && *cp != '\0') {
1911 if (ce->ce_unlink) {
1912 /* Temporary file already exists, so we rename to
1913 version with extension. */
1914 char *file_org = strdup(ce->ce_file);
1915 ce->ce_file = add (cp, ce->ce_file);
1916 if (rename(file_org, ce->ce_file)) {
1917 adios (ce->ce_file, "unable to rename %s to ", file_org);
1918 }
1919 free(file_org);
1920
1921 } else {
1922 ce->ce_file = add (cp, ce->ce_file);
1923 }
1924 }
1925
1926 if ((ce->ce_fp = fopen (ce->ce_file, "w+")) == NULL) {
1927 content_error (ce->ce_file, ct, "unable to fopen for reading/writing");
1928 return NOTOK;
1929 }
1930
1931 if ((len = ct->c_end - ct->c_begin) < 0)
1932 adios (NULL, "internal error(2)");
1933
1934 if (! ct->c_fp) {
1935 if ((ct->c_fp = fopen (ct->c_file, "r")) == NULL) {
1936 content_error (ct->c_file, ct, "unable to open for reading");
1937 return NOTOK;
1938 }
1939 own_ct_fp = 1;
1940 }
1941
1942 if ((digested = ct->c_digested))
1943 MD5Init (&mdContext);
1944
1945 quoted = 0;
1946 #ifdef lint
1947 mask = 0;
1948 #endif
1949
1950 fseek (ct->c_fp, ct->c_begin, SEEK_SET);
1951 while (len > 0) {
1952 if (fgets (buffer, sizeof(buffer) - 1, ct->c_fp) == NULL) {
1953 content_error (NULL, ct, "premature eof");
1954 goto clean_up;
1955 }
1956
1957 if ((cc = strlen (buffer)) > len)
1958 cc = len;
1959 len -= cc;
1960
1961 for (ep = (cp = buffer) + cc - 1; cp <= ep; ep--)
1962 if (!isspace ((unsigned char) *ep))
1963 break;
1964 *++ep = '\n', ep++;
1965
1966 for (; cp < ep; cp++) {
1967 if (quoted > 0) {
1968 /* in an escape sequence */
1969 if (quoted == 1) {
1970 /* at byte 1 of an escape sequence */
1971 mask = hex2nib[((unsigned char) *cp) & 0x7f];
1972 /* next is byte 2 */
1973 quoted = 2;
1974 } else {
1975 /* at byte 2 of an escape sequence */
1976 mask <<= 4;
1977 mask |= hex2nib[((unsigned char) *cp) & 0x7f];
1978 putc (mask, ce->ce_fp);
1979 if (digested)
1980 MD5Update (&mdContext, &mask, 1);
1981 if (ferror (ce->ce_fp)) {
1982 content_error (ce->ce_file, ct, "error writing to");
1983 goto clean_up;
1984 }
1985 /* finished escape sequence; next may be literal or a new
1986 * escape sequence */
1987 quoted = 0;
1988 }
1989 /* on to next byte */
1990 continue;
1991 }
1992
1993 /* not in an escape sequence */
1994 if (*cp == '=') {
1995 /* starting an escape sequence, or invalid '='? */
1996 if (cp + 1 < ep && cp[1] == '\n') {
1997 /* "=\n" soft line break, eat the \n */
1998 cp++;
1999 continue;
2000 }
2001 if (cp + 1 >= ep || cp + 2 >= ep) {
2002 /* We don't have 2 bytes left, so this is an invalid
2003 * escape sequence; just show the raw bytes (below). */
2004 } else if (isxdigit ((unsigned char) cp[1]) &&
2005 isxdigit ((unsigned char) cp[2])) {
2006 /* Next 2 bytes are hex digits, making this a valid escape
2007 * sequence; let's decode it (above). */
2008 quoted = 1;
2009 continue;
2010 } else {
2011 /* One or both of the next 2 is out of range, making this
2012 * an invalid escape sequence; just show the raw bytes
2013 * (below). */
2014 }
2015 }
2016
2017 /* Just show the raw byte. */
2018 putc (*cp, ce->ce_fp);
2019 if (digested) {
2020 if (*cp == '\n') {
2021 MD5Update (&mdContext, (unsigned char *) "\r\n",2);
2022 } else {
2023 MD5Update (&mdContext, (unsigned char *) cp, 1);
2024 }
2025 }
2026 if (ferror (ce->ce_fp)) {
2027 content_error (ce->ce_file, ct, "error writing to");
2028 goto clean_up;
2029 }
2030 }
2031 }
2032 if (quoted) {
2033 content_error (NULL, ct,
2034 "invalid QUOTED-PRINTABLE encoding -- end-of-content while still quoting");
2035 goto clean_up;
2036 }
2037
2038 fseek (ct->c_fp, 0L, SEEK_SET);
2039
2040 if (fflush (ce->ce_fp)) {
2041 content_error (ce->ce_file, ct, "error writing to");
2042 goto clean_up;
2043 }
2044
2045 if (digested) {
2046 unsigned char digest[16];
2047
2048 MD5Final (digest, &mdContext);
2049 if (memcmp((char *) digest, (char *) ct->c_digest,
2050 sizeof(digest) / sizeof(digest[0])))
2051 content_error (NULL, ct,
2052 "content integrity suspect (digest mismatch) -- continuing");
2053 else
2054 if (debugsw)
2055 fprintf (stderr, "content integrity confirmed\n");
2056 }
2057
2058 fseek (ce->ce_fp, 0L, SEEK_SET);
2059
2060 ready_to_go:
2061 *file = ce->ce_file;
2062 if (own_ct_fp) {
2063 fclose (ct->c_fp);
2064 ct->c_fp = NULL;
2065 }
2066 return fileno (ce->ce_fp);
2067
2068 clean_up:
2069 free_encoding (ct, 0);
2070 if (own_ct_fp) {
2071 fclose (ct->c_fp);
2072 ct->c_fp = NULL;
2073 }
2074 return NOTOK;
2075 }
2076
2077
2078 /*
2079 * 7BIT
2080 */
2081
2082 static int
2083 Init7Bit (CT ct)
2084 {
2085 if (init_encoding (ct, open7Bit) == NOTOK)
2086 return NOTOK;
2087
2088 ct->c_cesizefnx = NULL; /* no need to decode for real size */
2089 return OK;
2090 }
2091
2092
2093 int
2094 open7Bit (CT ct, char **file)
2095 {
2096 int cc, fd, len, own_ct_fp = 0;
2097 char buffer[BUFSIZ];
2098 /* sbeck -- handle suffixes */
2099 char *cp;
2100 CI ci;
2101 CE ce = &ct->c_cefile;
2102
2103 if (ce->ce_fp) {
2104 fseek (ce->ce_fp, 0L, SEEK_SET);
2105 goto ready_to_go;
2106 }
2107
2108 if (ce->ce_file) {
2109 if ((ce->ce_fp = fopen (ce->ce_file, "r")) == NULL) {
2110 content_error (ce->ce_file, ct, "unable to fopen for reading");
2111 return NOTOK;
2112 }
2113 goto ready_to_go;
2114 }
2115
2116 if (*file == NULL) {
2117 char *tempfile;
2118 if ((tempfile = m_mktemp2(NULL, invo_name, NULL, NULL)) == NULL) {
2119 adios(NULL, "unable to create temporary file in %s",
2120 get_temp_dir());
2121 }
2122 ce->ce_file = add (tempfile, NULL);
2123 ce->ce_unlink = 1;
2124 } else {
2125 ce->ce_file = add (*file, NULL);
2126 ce->ce_unlink = 0;
2127 }
2128
2129 /* sbeck@cise.ufl.edu -- handle suffixes */
2130 ci = &ct->c_ctinfo;
2131 snprintf (buffer, sizeof(buffer), "%s-suffix-%s/%s",
2132 invo_name, ci->ci_type, ci->ci_subtype);
2133 cp = context_find (buffer);
2134 if (cp == NULL || *cp == '\0') {
2135 snprintf (buffer, sizeof(buffer), "%s-suffix-%s", invo_name,
2136 ci->ci_type);
2137 cp = context_find (buffer);
2138 }
2139 if (cp != NULL && *cp != '\0') {
2140 if (ce->ce_unlink) {
2141 /* Temporary file already exists, so we rename to
2142 version with extension. */
2143 char *file_org = strdup(ce->ce_file);
2144 ce->ce_file = add (cp, ce->ce_file);
2145 if (rename(file_org, ce->ce_file)) {
2146 adios (ce->ce_file, "unable to rename %s to ", file_org);
2147 }
2148 free(file_org);
2149
2150 } else {
2151 ce->ce_file = add (cp, ce->ce_file);
2152 }
2153 }
2154
2155 if ((ce->ce_fp = fopen (ce->ce_file, "w+")) == NULL) {
2156 content_error (ce->ce_file, ct, "unable to fopen for reading/writing");
2157 return NOTOK;
2158 }
2159
2160 if (ct->c_type == CT_MULTIPART) {
2161 char **ap, **ep;
2162 CI ci = &ct->c_ctinfo;
2163
2164 len = 0;
2165 fprintf (ce->ce_fp, "%s: %s/%s", TYPE_FIELD, ci->ci_type, ci->ci_subtype);
2166 len += strlen (TYPE_FIELD) + 2 + strlen (ci->ci_type)
2167 + 1 + strlen (ci->ci_subtype);
2168 for (ap = ci->ci_attrs, ep = ci->ci_values; *ap; ap++, ep++) {
2169 putc (';', ce->ce_fp);
2170 len++;
2171
2172 snprintf (buffer, sizeof(buffer), "%s=\"%s\"", *ap, *ep);
2173
2174 if (len + 1 + (cc = strlen (buffer)) >= CPERLIN) {
2175 fputs ("\n\t", ce->ce_fp);
2176 len = 8;
2177 } else {
2178 putc (' ', ce->ce_fp);
2179 len++;
2180 }
2181 fprintf (ce->ce_fp, "%s", buffer);
2182 len += cc;
2183 }
2184
2185 if (ci->ci_comment) {
2186 if (len + 1 + (cc = 2 + strlen (ci->ci_comment)) >= CPERLIN) {
2187 fputs ("\n\t", ce->ce_fp);
2188 len = 8;
2189 }
2190 else {
2191 putc (' ', ce->ce_fp);
2192 len++;
2193 }
2194 fprintf (ce->ce_fp, "(%s)", ci->ci_comment);
2195 len += cc;
2196 }
2197 fprintf (ce->ce_fp, "\n");
2198 if (ct->c_id)
2199 fprintf (ce->ce_fp, "%s:%s", ID_FIELD, ct->c_id);
2200 if (ct->c_descr)
2201 fprintf (ce->ce_fp, "%s:%s", DESCR_FIELD, ct->c_descr);
2202 if (ct->c_dispo)
2203 fprintf (ce->ce_fp, "%s:%s", DISPO_FIELD, ct->c_dispo);
2204 fprintf (ce->ce_fp, "\n");
2205 }
2206
2207 if ((len = ct->c_end - ct->c_begin) < 0)
2208 adios (NULL, "internal error(3)");
2209
2210 if (! ct->c_fp) {
2211 if ((ct->c_fp = fopen (ct->c_file, "r")) == NULL) {
2212 content_error (ct->c_file, ct, "unable to open for reading");
2213 return NOTOK;
2214 }
2215 own_ct_fp = 1;
2216 }
2217
2218 lseek (fd = fileno (ct->c_fp), (off_t) ct->c_begin, SEEK_SET);
2219 while (len > 0)
2220 switch (cc = read (fd, buffer, sizeof(buffer) - 1)) {
2221 case NOTOK:
2222 content_error (ct->c_file, ct, "error reading from");
2223 goto clean_up;
2224
2225 case OK:
2226 content_error (NULL, ct, "premature eof");
2227 goto clean_up;
2228
2229 default:
2230 if (cc > len)
2231 cc = len;
2232 len -= cc;
2233
2234 fwrite (buffer, sizeof(*buffer), cc, ce->ce_fp);
2235 if (ferror (ce->ce_fp)) {
2236 content_error (ce->ce_file, ct, "error writing to");
2237 goto clean_up;
2238 }
2239 }
2240
2241 fseek (ct->c_fp, 0L, SEEK_SET);
2242
2243 if (fflush (ce->ce_fp)) {
2244 content_error (ce->ce_file, ct, "error writing to");
2245 goto clean_up;
2246 }
2247
2248 fseek (ce->ce_fp, 0L, SEEK_SET);
2249
2250 ready_to_go:
2251 *file = ce->ce_file;
2252 if (own_ct_fp) {
2253 fclose (ct->c_fp);
2254 ct->c_fp = NULL;
2255 }
2256 return fileno (ce->ce_fp);
2257
2258 clean_up:
2259 free_encoding (ct, 0);
2260 if (own_ct_fp) {
2261 fclose (ct->c_fp);
2262 ct->c_fp = NULL;
2263 }
2264 return NOTOK;
2265 }
2266
2267
2268 /*
2269 * External
2270 */
2271
2272 static int
2273 openExternal (CT ct, CT cb, CE ce, char **file, int *fd)
2274 {
2275 char cachefile[BUFSIZ];
2276
2277 if (ce->ce_fp) {
2278 fseek (ce->ce_fp, 0L, SEEK_SET);
2279 goto ready_already;
2280 }
2281
2282 if (ce->ce_file) {
2283 if ((ce->ce_fp = fopen (ce->ce_file, "r")) == NULL) {
2284 content_error (ce->ce_file, ct, "unable to fopen for reading");
2285 return NOTOK;
2286 }
2287 goto ready_already;
2288 }
2289
2290 if (find_cache (ct, rcachesw, (int *) 0, cb->c_id,
2291 cachefile, sizeof(cachefile)) != NOTOK) {
2292 if ((ce->ce_fp = fopen (cachefile, "r"))) {
2293 ce->ce_file = getcpy (cachefile);
2294 ce->ce_unlink = 0;
2295 goto ready_already;
2296 } else {
2297 admonish (cachefile, "unable to fopen for reading");
2298 }
2299 }
2300
2301 return OK;
2302
2303 ready_already:
2304 *file = ce->ce_file;
2305 *fd = fileno (ce->ce_fp);
2306 return DONE;
2307 }
2308
2309 /*
2310 * File
2311 */
2312
2313 static int
2314 InitFile (CT ct)
2315 {
2316 return init_encoding (ct, openFile);
2317 }
2318
2319
2320 static int
2321 openFile (CT ct, char **file)
2322 {
2323 int fd, cachetype;
2324 char cachefile[BUFSIZ];
2325 struct exbody *e = ct->c_ctexbody;
2326 CE ce = &ct->c_cefile;
2327
2328 switch (openExternal (e->eb_parent, e->eb_content, ce, file, &fd)) {
2329 case NOTOK:
2330 return NOTOK;
2331
2332 case OK:
2333 break;
2334
2335 case DONE:
2336 return fd;
2337 }
2338
2339 if (!e->eb_name) {
2340 content_error (NULL, ct, "missing name parameter");
2341 return NOTOK;
2342 }
2343
2344 ce->ce_file = getcpy (e->eb_name);
2345 ce->ce_unlink = 0;
2346
2347 if ((ce->ce_fp = fopen (ce->ce_file, "r")) == NULL) {
2348 content_error (ce->ce_file, ct, "unable to fopen for reading");
2349 return NOTOK;
2350 }
2351
2352 if ((!e->eb_permission || strcasecmp (e->eb_permission, "read-write"))
2353 && find_cache (NULL, wcachesw, &cachetype, e->eb_content->c_id,
2354 cachefile, sizeof(cachefile)) != NOTOK) {
2355 int mask;
2356 FILE *fp;
2357
2358 mask = umask (cachetype ? ~m_gmprot () : 0222);
2359 if ((fp = fopen (cachefile, "w"))) {
2360 int cc;
2361 char buffer[BUFSIZ];
2362 FILE *gp = ce->ce_fp;
2363
2364 fseek (gp, 0L, SEEK_SET);
2365
2366 while ((cc = fread (buffer, sizeof(*buffer), sizeof(buffer), gp))
2367 > 0)
2368 fwrite (buffer, sizeof(*buffer), cc, fp);
2369 fflush (fp);
2370
2371 if (ferror (gp)) {
2372 admonish (ce->ce_file, "error reading");
2373 unlink (cachefile);
2374 }
2375 else
2376 if (ferror (fp)) {
2377 admonish (cachefile, "error writing");
2378 unlink (cachefile);
2379 }
2380 fclose (fp);
2381 }
2382 umask (mask);
2383 }
2384
2385 fseek (ce->ce_fp, 0L, SEEK_SET);
2386 *file = ce->ce_file;
2387 return fileno (ce->ce_fp);
2388 }
2389
2390 /*
2391 * FTP
2392 */
2393
2394 static int
2395 InitFTP (CT ct)
2396 {
2397 return init_encoding (ct, openFTP);
2398 }
2399
2400
2401 static int
2402 openFTP (CT ct, char **file)
2403 {
2404 int cachetype, caching, fd;
2405 int len, buflen;
2406 char *bp, *ftp, *user, *pass;
2407 char buffer[BUFSIZ], cachefile[BUFSIZ];
2408 struct exbody *e;
2409 CE ce = &ct->c_cefile;
2410 static char *username = NULL;
2411 static char *password = NULL;
2412
2413 e = ct->c_ctexbody;
2414
2415 if ((ftp = context_find (nmhaccessftp)) && !*ftp)
2416 ftp = NULL;
2417
2418 if (!ftp)
2419 return NOTOK;
2420
2421 switch (openExternal (e->eb_parent, e->eb_content, ce, file, &fd)) {
2422 case NOTOK:
2423 return NOTOK;
2424
2425 case OK:
2426 break;
2427
2428 case DONE:
2429 return fd;
2430 }
2431
2432 if (!e->eb_name || !e->eb_site) {
2433 content_error (NULL, ct, "missing %s parameter",
2434 e->eb_name ? "site": "name");
2435 return NOTOK;
2436 }
2437
2438 if (xpid) {
2439 if (xpid < 0)
2440 xpid = -xpid;
2441 pidcheck (pidwait (xpid, NOTOK));
2442 xpid = 0;
2443 }
2444
2445 /* Get the buffer ready to go */
2446 bp = buffer;
2447 buflen = sizeof(buffer);
2448
2449 /*
2450 * Construct the query message for user
2451 */
2452 snprintf (bp, buflen, "Retrieve %s", e->eb_name);
2453 len = strlen (bp);
2454 bp += len;
2455 buflen -= len;
2456
2457 if (e->eb_partno) {
2458 snprintf (bp, buflen, " (content %s)", e->eb_partno);
2459 len = strlen (bp);
2460 bp += len;
2461 buflen -= len;
2462 }
2463
2464 snprintf (bp, buflen, "\n using %sFTP from site %s",
2465 e->eb_flags ? "anonymous " : "", e->eb_site);
2466 len = strlen (bp);
2467 bp += len;
2468 buflen -= len;
2469
2470 if (e->eb_size > 0) {
2471 snprintf (bp, buflen, " (%lu octets)", e->eb_size);
2472 len = strlen (bp);
2473 bp += len;
2474 buflen -= len;
2475 }
2476 snprintf (bp, buflen, "? ");
2477
2478 /*
2479 * Now, check the answer
2480 */
2481 if (!getanswer (buffer))
2482 return NOTOK;
2483
2484 if (e->eb_flags) {
2485 user = "anonymous";
2486 snprintf (buffer, sizeof(buffer), "%s@%s", getusername (),
2487 LocalName (1));
2488 pass = buffer;
2489 } else {
2490 ruserpass (e->eb_site, &username, &password);
2491 user = username;
2492 pass = password;
2493 }
2494
2495 ce->ce_unlink = (*file == NULL);
2496 caching = 0;
2497 cachefile[0] = '\0';
2498 if ((!e->eb_permission || strcasecmp (e->eb_permission, "read-write"))
2499 && find_cache (NULL, wcachesw, &cachetype, e->eb_content->c_id,
2500 cachefile, sizeof(cachefile)) != NOTOK) {
2501 if (*file == NULL) {
2502 ce->ce_unlink = 0;
2503 caching = 1;
2504 }
2505 }
2506
2507 if (*file)
2508 ce->ce_file = add (*file, NULL);
2509 else if (caching)
2510 ce->ce_file = add (cachefile, NULL);
2511 else {
2512 char *tempfile;
2513 if ((tempfile = m_mktemp2(NULL, invo_name, NULL, NULL)) == NULL) {
2514 adios(NULL, "unable to create temporary file in %s",
2515 get_temp_dir());
2516 }
2517 ce->ce_file = add (tempfile, NULL);
2518 }
2519
2520 if ((ce->ce_fp = fopen (ce->ce_file, "w+")) == NULL) {
2521 content_error (ce->ce_file, ct, "unable to fopen for reading/writing");
2522 return NOTOK;
2523 }
2524
2525 {
2526 int child_id, i, vecp;
2527 char *vec[9];
2528
2529 vecp = 0;
2530 vec[vecp++] = r1bindex (ftp, '/');
2531 vec[vecp++] = e->eb_site;
2532 vec[vecp++] = user;
2533 vec[vecp++] = pass;
2534 vec[vecp++] = e->eb_dir;
2535 vec[vecp++] = e->eb_name;
2536 vec[vecp++] = ce->ce_file,
2537 vec[vecp++] = e->eb_mode && !strcasecmp (e->eb_mode, "ascii")
2538 ? "ascii" : "binary";
2539 vec[vecp] = NULL;
2540
2541 fflush (stdout);
2542
2543 for (i = 0; (child_id = fork()) == NOTOK && i < 5; i++)
2544 sleep (5);
2545 switch (child_id) {
2546 case NOTOK:
2547 adios ("fork", "unable to");
2548 /* NOTREACHED */
2549
2550 case OK:
2551 close (fileno (ce->ce_fp));
2552 execvp (ftp, vec);
2553 fprintf (stderr, "unable to exec ");
2554 perror (ftp);
2555 _exit (-1);
2556 /* NOTREACHED */
2557
2558 default:
2559 if (pidXwait (child_id, NULL)) {
2560 username = password = NULL;
2561 ce->ce_unlink = 1;
2562 return NOTOK;
2563 }
2564 break;
2565 }
2566 }
2567
2568 if (cachefile[0]) {
2569 if (caching)
2570 chmod (cachefile, cachetype ? m_gmprot () : 0444);
2571 else {
2572 int mask;
2573 FILE *fp;
2574
2575 mask = umask (cachetype ? ~m_gmprot () : 0222);
2576 if ((fp = fopen (cachefile, "w"))) {
2577 int cc;
2578 FILE *gp = ce->ce_fp;
2579
2580 fseek (gp, 0L, SEEK_SET);
2581
2582 while ((cc= fread (buffer, sizeof(*buffer), sizeof(buffer), gp))
2583 > 0)
2584 fwrite (buffer, sizeof(*buffer), cc, fp);
2585 fflush (fp);
2586
2587 if (ferror (gp)) {
2588 admonish (ce->ce_file, "error reading");
2589 unlink (cachefile);
2590 }
2591 else
2592 if (ferror (fp)) {
2593 admonish (cachefile, "error writing");
2594 unlink (cachefile);
2595 }
2596 fclose (fp);
2597 }
2598 umask (mask);
2599 }
2600 }
2601
2602 fseek (ce->ce_fp, 0L, SEEK_SET);
2603 *file = ce->ce_file;
2604 return fileno (ce->ce_fp);
2605 }
2606
2607
2608 /*
2609 * Mail
2610 */
2611
2612 static int
2613 InitMail (CT ct)
2614 {
2615 return init_encoding (ct, openMail);
2616 }
2617
2618
2619 static int
2620 openMail (CT ct, char **file)
2621 {
2622 int child_id, fd, i, vecp;
2623 int len, buflen;
2624 char *bp, buffer[BUFSIZ], *vec[7];
2625 struct exbody *e = ct->c_ctexbody;
2626 CE ce = &ct->c_cefile;
2627
2628 switch (openExternal (e->eb_parent, e->eb_content, ce, file, &fd)) {
2629 case NOTOK:
2630 return NOTOK;
2631
2632 case OK:
2633 break;
2634
2635 case DONE:
2636 return fd;
2637 }
2638
2639 if (!e->eb_server) {
2640 content_error (NULL, ct, "missing server parameter");
2641 return NOTOK;
2642 }
2643
2644 if (xpid) {
2645 if (xpid < 0)
2646 xpid = -xpid;
2647 pidcheck (pidwait (xpid, NOTOK));
2648 xpid = 0;
2649 }
2650
2651 /* Get buffer ready to go */
2652 bp = buffer;
2653 buflen = sizeof(buffer);
2654
2655 /* Now, construct query message */
2656 snprintf (bp, buflen, "Retrieve content");
2657 len = strlen (bp);
2658 bp += len;
2659 buflen -= len;
2660
2661 if (e->eb_partno) {
2662 snprintf (bp, buflen, " %s", e->eb_partno);
2663 len = strlen (bp);
2664 bp += len;
2665 buflen -= len;
2666 }
2667
2668 snprintf (bp, buflen, " by asking %s\n\n%s\n? ",
2669 e->eb_server,
2670 e->eb_subject ? e->eb_subject : e->eb_body);
2671
2672 /* Now, check answer */
2673 if (!getanswer (buffer))
2674 return NOTOK;
2675
2676 vecp = 0;
2677 vec[vecp++] = r1bindex (mailproc, '/');
2678 vec[vecp++] = e->eb_server;
2679 vec[vecp++] = "-subject";
2680 vec[vecp++] = e->eb_subject ? e->eb_subject : "mail-server request";
2681 vec[vecp++] = "-body";
2682 vec[vecp++] = e->eb_body;
2683 vec[vecp] = NULL;
2684
2685 for (i = 0; (child_id = fork()) == NOTOK && i < 5; i++)
2686 sleep (5);
2687 switch (child_id) {
2688 case NOTOK:
2689 advise ("fork", "unable to");
2690 return NOTOK;
2691
2692 case OK:
2693 execvp (mailproc, vec);
2694 fprintf (stderr, "unable to exec ");
2695 perror (mailproc);
2696 _exit (-1);
2697 /* NOTREACHED */
2698
2699 default:
2700 if (pidXwait (child_id, NULL) == OK)
2701 advise (NULL, "request sent");
2702 break;
2703 }
2704
2705 if (*file == NULL) {
2706 char *tempfile;
2707 if ((tempfile = m_mktemp2(NULL, invo_name, NULL, NULL)) == NULL) {
2708 adios(NULL, "unable to create temporary file in %s",
2709 get_temp_dir());
2710 }
2711 ce->ce_file = add (tempfile, NULL);
2712 ce->ce_unlink = 1;
2713 } else {
2714 ce->ce_file = add (*file, NULL);
2715 ce->ce_unlink = 0;
2716 }
2717
2718 if ((ce->ce_fp = fopen (ce->ce_file, "w+")) == NULL) {
2719 content_error (ce->ce_file, ct, "unable to fopen for reading/writing");
2720 return NOTOK;
2721 }
2722
2723 /* showproc is for mhshow and mhstore, though mhlist -debug
2724 * prints it, too. */
2725 if (ct->c_showproc)
2726 free (ct->c_showproc);
2727 ct->c_showproc = add ("true", NULL);
2728
2729 fseek (ce->ce_fp, 0L, SEEK_SET);
2730 *file = ce->ce_file;
2731 return fileno (ce->ce_fp);
2732 }
2733
2734
2735 /*
2736 * URL
2737 */
2738
2739 static int
2740 InitURL (CT ct)
2741 {
2742 return init_encoding (ct, openURL);
2743 }
2744
2745
2746 static int
2747 openURL (CT ct, char **file)
2748 {
2749 struct exbody *e = ct->c_ctexbody;
2750 CE ce = &ct->c_cefile;
2751 char *urlprog, *program;
2752 char buffer[BUFSIZ], cachefile[BUFSIZ];
2753 int fd, caching, cachetype;
2754 struct msgs_array args = { 0, 0, NULL};
2755 pid_t child_id;
2756
2757 if ((urlprog = context_find(nmhaccessurl)) && *urlprog == '\0')
2758 urlprog = NULL;
2759
2760 if (! urlprog) {
2761 content_error(NULL, ct, "No entry for nmh-access-url in profile");
2762 return NOTOK;
2763 }
2764
2765 switch (openExternal(e->eb_parent, e->eb_content, ce, file, &fd)) {
2766 case NOTOK:
2767 return NOTOK;
2768
2769 case OK:
2770 break;
2771
2772 case DONE:
2773 return fd;
2774 }
2775
2776 if (!e->eb_url) {
2777 content_error(NULL, ct, "missing url parameter");
2778 return NOTOK;
2779 }
2780
2781 if (xpid) {
2782 if (xpid < 0)
2783 xpid = -xpid;
2784 pidcheck (pidwait (xpid, NOTOK));
2785 xpid = 0;
2786 }
2787
2788 ce->ce_unlink = (*file == NULL);
2789 caching = 0;
2790 cachefile[0] = '\0';
2791
2792 if (find_cache(NULL, wcachesw, &cachetype, e->eb_content->c_id,
2793 cachefile, sizeof(cachefile)) != NOTOK) {
2794 if (*file == NULL) {
2795 ce->ce_unlink = 0;
2796 caching = 1;
2797 }
2798 }
2799
2800 if (*file)
2801 ce->ce_file = add(*file, NULL);
2802 else if (caching)
2803 ce->ce_file = add(cachefile, NULL);
2804 else {
2805 char *tempfile;
2806 if ((tempfile = m_mktemp2(NULL, invo_name, NULL, NULL)) == NULL) {
2807 adios(NULL, "unable to create temporary file in %s",
2808 get_temp_dir());
2809 }
2810 ce->ce_file = add (tempfile, NULL);
2811 }
2812
2813 if ((ce->ce_fp = fopen(ce->ce_file, "w+")) == NULL) {
2814 content_error(ce->ce_file, ct, "unable to fopen for read/writing");
2815 return NOTOK;
2816 }
2817
2818 switch (child_id = fork()) {
2819 case NOTOK:
2820 adios ("fork", "unable to");
2821 /* NOTREACHED */
2822
2823 case OK:
2824 argsplit_msgarg(&args, urlprog, &program);
2825 app_msgarg(&args, e->eb_url);
2826 app_msgarg(&args, NULL);
2827 dup2(fileno(ce->ce_fp), 1);
2828 close(fileno(ce->ce_fp));
2829 execvp(program, args.msgs);
2830 fprintf(stderr, "Unable to exec ");
2831 perror(program);
2832 _exit(-1);
2833 /* NOTREACHED */
2834
2835 default:
2836 if (pidXwait(child_id, NULL)) {
2837 ce->ce_unlink = 1;
2838 return NOTOK;
2839 }
2840 }
2841
2842 if (cachefile[0]) {
2843 if (caching)
2844 chmod(cachefile, cachetype ? m_gmprot() : 0444);
2845 else {
2846 int mask;
2847 FILE *fp;
2848
2849 mask = umask (cachetype ? ~m_gmprot() : 0222);
2850 if ((fp = fopen(cachefile, "w"))) {
2851 int cc;
2852 FILE *gp = ce->ce_fp;
2853
2854 fseeko(gp, 0, SEEK_SET);
2855
2856 while ((cc = fread(buffer, sizeof(*buffer),
2857 sizeof(buffer), gp)) > 0)
2858 fwrite(buffer, sizeof(*buffer), cc, fp);
2859
2860 fflush(fp);
2861
2862 if (ferror(gp)) {
2863 admonish(ce->ce_file, "error reading");
2864 unlink(cachefile);
2865 }
2866 }
2867 umask(mask);
2868 }
2869 }
2870
2871 fseeko(ce->ce_fp, 0, SEEK_SET);
2872 *file = ce->ce_file;
2873 return fd;
2874 }
2875
2876 static int
2877 readDigest (CT ct, char *cp)
2878 {
2879 int bitno, skip;
2880 uint32_t bits;
2881 char *bp = cp;
2882 unsigned char *dp, value, *ep;
2883
2884 bitno = 18;
2885 bits = 0L;
2886 skip = 0;
2887
2888 for (ep = (dp = ct->c_digest)
2889 + sizeof(ct->c_digest) / sizeof(ct->c_digest[0]); *cp; cp++)
2890 switch (*cp) {
2891 default:
2892 if (skip
2893 || (*cp & 0x80)
2894 || (value = b642nib[*cp & 0x7f]) > 0x3f) {
2895 if (debugsw)
2896 fprintf (stderr, "invalid BASE64 encoding\n");
2897 return NOTOK;
2898 }
2899
2900 bits |= value << bitno;
2901 test_end:
2902 if ((bitno -= 6) < 0) {
2903 if (dp + (3 - skip) > ep)
2904 goto invalid_digest;
2905 *dp++ = (bits >> 16) & 0xff;
2906 if (skip < 2) {
2907 *dp++ = (bits >> 8) & 0xff;
2908 if (skip < 1)
2909 *dp++ = bits & 0xff;
2910 }
2911 bitno = 18;
2912 bits = 0L;
2913 skip = 0;
2914 }
2915 break;
2916
2917 case '=':
2918 if (++skip > 3)
2919 goto self_delimiting;
2920 goto test_end;
2921 }
2922 if (bitno != 18) {
2923 if (debugsw)
2924 fprintf (stderr, "premature ending (bitno %d)\n", bitno);
2925
2926 return NOTOK;
2927 }
2928 self_delimiting:
2929 if (dp != ep) {
2930 invalid_digest:
2931 if (debugsw) {
2932 while (*cp)
2933 cp++;
2934 fprintf (stderr, "invalid MD5 digest (got %d octets)\n",
2935 (int)(cp - bp));
2936 }
2937
2938 return NOTOK;
2939 }
2940
2941 if (debugsw) {
2942 fprintf (stderr, "MD5 digest=");
2943 for (dp = ct->c_digest; dp < ep; dp++)
2944 fprintf (stderr, "%02x", *dp & 0xff);
2945 fprintf (stderr, "\n");
2946 }
2947
2948 return OK;
2949 }
2950
2951
2952 /* Multipart parts might have content before the first subpart and/or
2953 after the last subpart that hasn't been stored anywhere else, so do
2954 that. */
2955 int
2956 get_leftover_mp_content (CT ct, int before /* or after */) {
2957 struct multipart *m = (struct multipart *) ct->c_ctparams;
2958 char *boundary;
2959 int found_boundary = 0;
2960 char buffer[BUFSIZ];
2961 int max = BUFSIZ;
2962 int read = 0;
2963 char *content = NULL;
2964
2965 if (! m) return NOTOK;
2966
2967 if (before) {
2968 if (! m->mp_parts || ! m->mp_parts->mp_part) return NOTOK;
2969
2970 /* Isolate the beginning of this part to the beginning of the
2971 first subpart and save any content between them. */
2972 fseeko (ct->c_fp, ct->c_begin, SEEK_SET);
2973 max = m->mp_parts->mp_part->c_begin - ct->c_begin;
2974 boundary = concat ("--", m->mp_start, NULL);
2975 } else {
2976 struct part *last_subpart = NULL;
2977 struct part *subpart;
2978
2979 /* Go to the last subpart to get its end position. */
2980 for (subpart = m->mp_parts; subpart; subpart = subpart->mp_next) {
2981 last_subpart = subpart;
2982 }
2983
2984 if (last_subpart == NULL) return NOTOK;
2985
2986 /* Isolate the end of the last subpart to the end of this part
2987 and save any content between them. */
2988 fseeko (ct->c_fp, last_subpart->mp_part->c_end, SEEK_SET);
2989 max = ct->c_end - last_subpart->mp_part->c_end;
2990 boundary = concat ("--", m->mp_stop, NULL);
2991 }
2992
2993 /* Back up by 1 to pick up the newline. */
2994 while (fgets (buffer, sizeof(buffer) - 1, ct->c_fp)) {
2995 read += strlen (buffer);
2996 /* Don't look beyond beginning of first subpart (before) or
2997 next part (after). */
2998 if (read > max) buffer[read-max] = '\0';
2999
3000 if (before) {
3001 if (! strcmp (buffer, boundary)) {
3002 found_boundary = 1;
3003 }
3004 } else {
3005 if (! found_boundary && ! strcmp (buffer, boundary)) {
3006 found_boundary = 1;
3007 continue;
3008 }
3009 }
3010
3011 if ((before && ! found_boundary) || (! before && found_boundary)) {
3012 if (content) {
3013 char *old_content = content;
3014 content = concat (content, buffer, NULL);
3015 free (old_content);
3016 } else {
3017 content = before
3018 ? concat ("\n", buffer, NULL)
3019 : concat (buffer, NULL);
3020 }
3021 }
3022
3023 if (before) {
3024 if (found_boundary || read > max) break;
3025 } else {
3026 if (read > max) break;
3027 }
3028 }
3029
3030 /* Skip the newline if that's all there is. */
3031 if (content) {
3032 char *cp;
3033
3034 /* Remove trailing newline, except at EOF. */
3035 if ((before || ! feof (ct->c_fp)) &&
3036 (cp = content + strlen (content)) > content &&
3037 *--cp == '\n') {
3038 *cp = '\0';
3039 }
3040
3041 if (strlen (content) > 1) {
3042 if (before) {
3043 m->mp_content_before = content;
3044 } else {
3045 m->mp_content_after = content;
3046 }
3047 } else {
3048 free (content);
3049 }
3050 }
3051
3052 free (boundary);
3053
3054 return OK;
3055 }
3056
3057
3058 char *
3059 ct_type_str (int type) {
3060 switch (type) {
3061 case CT_APPLICATION:
3062 return "application";
3063 case CT_AUDIO:
3064 return "audio";
3065 case CT_IMAGE:
3066 return "image";
3067 case CT_MESSAGE:
3068 return "message";
3069 case CT_MULTIPART:
3070 return "multipart";
3071 case CT_TEXT:
3072 return "text";
3073 case CT_VIDEO:
3074 return "video";
3075 case CT_EXTENSION:
3076 return "extension";
3077 default:
3078 return "unknown_type";
3079 }
3080 }
3081
3082
3083 char *
3084 ct_subtype_str (int type, int subtype) {
3085 switch (type) {
3086 case CT_APPLICATION:
3087 switch (subtype) {
3088 case APPLICATION_OCTETS:
3089 return "octets";
3090 case APPLICATION_POSTSCRIPT:
3091 return "postscript";
3092 default:
3093 return "unknown_app_subtype";
3094 }
3095 case CT_MESSAGE:
3096 switch (subtype) {
3097 case MESSAGE_RFC822:
3098 return "rfc822";
3099 case MESSAGE_PARTIAL:
3100 return "partial";
3101 case MESSAGE_EXTERNAL:
3102 return "external";
3103 default:
3104 return "unknown_msg_subtype";
3105 }
3106 case CT_MULTIPART:
3107 switch (subtype) {
3108 case MULTI_MIXED:
3109 return "mixed";
3110 case MULTI_ALTERNATE:
3111 return "alternative";
3112 case MULTI_DIGEST:
3113 return "digest";
3114 case MULTI_PARALLEL:
3115 return "parallel";
3116 default:
3117 return "unknown_multipart_subtype";
3118 }
3119 case CT_TEXT:
3120 switch (subtype) {
3121 case TEXT_PLAIN:
3122 return "plain";
3123 case TEXT_RICHTEXT:
3124 return "richtext";
3125 case TEXT_ENRICHED:
3126 return "enriched";
3127 default:
3128 return "unknown_text_subtype";
3129 }
3130 default:
3131 return "unknown_type";
3132 }
3133 }
3134
3135
3136 /* Find the content type and InitFunc for the CT. */
3137 const struct str2init *
3138 get_ct_init (int type) {
3139 const struct str2init *sp;
3140
3141 for (sp = str2cts; sp->si_key; ++sp) {
3142 if (type == sp->si_val) {
3143 return sp;
3144 }
3145 }
3146
3147 return NULL;
3148 }
3149
3150 const char *
3151 ce_str (int encoding) {
3152 switch (encoding) {
3153 case CE_BASE64:
3154 return "base64";
3155 case CE_QUOTED:
3156 return "quoted-printable";
3157 case CE_8BIT:
3158 return "8bit";
3159 case CE_7BIT:
3160 return "7bit";
3161 case CE_BINARY:
3162 return "binary";
3163 case CE_EXTENSION:
3164 return "extension";
3165 case CE_EXTERNAL:
3166 return "external";
3167 default:
3168 return "unknown";
3169 }
3170 }
3171
3172 /* Find the content type and InitFunc for the content encoding method. */
3173 const struct str2init *
3174 get_ce_method (const char *method) {
3175 struct str2init *sp;
3176
3177 for (sp = str2ces; sp->si_key; ++sp) {
3178 if (! strcasecmp (method, sp->si_key)) {
3179 return sp;
3180 }
3181 }
3182
3183 return NULL;
3184 }
3185
3186 int
3187 parse_header_attrs (const char *filename, int len, char **header_attrp, CI ci,
3188 int *status) {
3189 char **attr = ci->ci_attrs;
3190 char *cp = *header_attrp;
3191
3192 while (*cp == ';') {
3193 char *dp, *vp, *up, c;
3194
3195 /* Relies on knowledge of this declaration:
3196 * char *ci_attrs[NPARMS + 2];
3197 */
3198 if (attr >= ci->ci_attrs + sizeof ci->ci_attrs/sizeof (char *) - 2) {
3199 advise (NULL,
3200 "too many parameters in message %s's %s: field (%d max)",
3201 filename, TYPE_FIELD, NPARMS);
3202 *status = NOTOK;
3203 return NOTOK;
3204 }
3205
3206 cp++;
3207 while (isspace ((unsigned char) *cp))
3208 cp++;
3209
3210 if (*cp == '(' &&
3211 get_comment (filename, ci, &cp, 1) == NOTOK) {
3212 *status = NOTOK;
3213 return NOTOK;
3214 }
3215
3216 if (*cp == 0) {
3217 advise (NULL,
3218 "extraneous trailing ';' in message %s's %s: "
3219 "parameter list",
3220 filename, TYPE_FIELD);
3221 *status = OK;
3222 return NOTOK;
3223 }
3224
3225 /* down case the attribute name */
3226 for (dp = cp; istoken ((unsigned char) *dp); dp++)
3227 if (isalpha((unsigned char) *dp) && isupper ((unsigned char) *dp))
3228 *dp = tolower ((unsigned char) *dp);
3229
3230 for (up = dp; isspace ((unsigned char) *dp);)
3231 dp++;
3232 if (dp == cp || *dp != '=') {
3233 advise (NULL,
3234 "invalid parameter in message %s's %s: "
3235 "field\n%*.*sparameter %s (error detected at offset %d)",
3236 filename, TYPE_FIELD, len, len, "", cp, dp - cp);
3237 *status = NOTOK;
3238 return NOTOK;
3239 }
3240
3241 vp = (*attr = add (cp, NULL)) + (up - cp);
3242 *vp = '\0';
3243 for (dp++; isspace ((unsigned char) *dp);)
3244 dp++;
3245
3246 /* Now store the attribute value. */
3247 ci->ci_values[attr - ci->ci_attrs] = vp = *attr + (dp - cp);
3248
3249 if (*dp == '"') {
3250 for (cp = ++dp, dp = vp;;) {
3251 switch (c = *cp++) {
3252 case '\0':
3253 bad_quote:
3254 advise (NULL,
3255 "invalid quoted-string in message %s's %s: "
3256 "field\n%*.*s(parameter %s)",
3257 filename, TYPE_FIELD, len, len, "", *attr);
3258 *status = NOTOK;
3259 return NOTOK;
3260
3261 case '\\':
3262 *dp++ = c;
3263 if ((c = *cp++) == '\0')
3264 goto bad_quote;
3265 /* else fall... */
3266
3267 default:
3268 *dp++ = c;
3269 continue;
3270
3271 case '"':
3272 *dp = '\0';
3273 break;
3274 }
3275 break;
3276 }
3277 } else {
3278 for (cp = dp, dp = vp; istoken (*cp); cp++, dp++)
3279 continue;
3280 *dp = '\0';
3281 }
3282 if (!*vp) {
3283 advise (NULL,
3284 "invalid parameter in message %s's %s: "
3285 "field\n%*.*s(parameter %s)",
3286 filename, TYPE_FIELD, len, len, "", *attr);
3287 *status = NOTOK;
3288 return NOTOK;
3289 }
3290
3291 while (isspace ((unsigned char) *cp))
3292 cp++;
3293
3294 if (*cp == '(' &&
3295 get_comment (filename, ci, &cp, 1) == NOTOK) {
3296 *status = NOTOK;
3297 return NOTOK;
3298 }
3299
3300 ++attr;
3301 }
3302
3303 *header_attrp = cp;
3304 return OK;
3305 }