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