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