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