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