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