]> diplodocus.org Git - nmh/blob - uip/mhbuildsbr.c
Added -dump to w3m and elinks invocations in mhn.defaults.
[nmh] / uip / mhbuildsbr.c
1
2 /*
3 * mhbuildsbr.c -- routines to expand/translate MIME composition files
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 /*
11 * This code was originally part of mhn.c. I split it into
12 * a separate program (mhbuild.c) and then later split it
13 * again (mhbuildsbr.c). But the code still has some of
14 * the mhn.c code in it. This program needs additional
15 * streamlining and removal of unneeded code.
16 */
17
18 #include <h/mh.h>
19 #include <fcntl.h>
20 #include <h/signals.h>
21 #include <h/md5.h>
22 #include <h/mts.h>
23 #include <h/tws.h>
24 #include <h/mime.h>
25 #include <h/mhparse.h>
26 #include <h/utils.h>
27
28 #ifdef HAVE_SYS_TIME_H
29 # include <sys/time.h>
30 #endif
31 #include <time.h>
32
33
34 extern int debugsw;
35
36 extern int listsw;
37 extern int rfc934sw;
38 extern int contentidsw;
39
40 /* cache policies */
41 extern int rcachesw; /* mhcachesbr.c */
42 extern int wcachesw; /* mhcachesbr.c */
43
44 static char prefix[] = "----- =_aaaaaaaaaa";
45
46 struct attach_list {
47 char *filename;
48 struct attach_list *next;
49 };
50
51 /*
52 * Maximum size of URL token in message/external-body
53 */
54
55 #define MAXURLTOKEN 40
56
57
58 /* mhmisc.c */
59 void content_error (char *, CT, char *, ...);
60
61 /* mhcachesbr.c */
62 int find_cache (CT, int, int *, char *, char *, int);
63
64 /* mhfree.c */
65 void free_ctinfo (CT);
66 void free_encoding (CT, int);
67
68 /*
69 * static prototypes
70 */
71 static int init_decoded_content (CT);
72 static void setup_attach_content(CT, char *);
73 static char *fgetstr (char *, int, FILE *);
74 static int user_content (FILE *, char *, CT *);
75 static void set_id (CT, int);
76 static int compose_content (CT, int);
77 static int scan_content (CT, size_t);
78 static int build_headers (CT, int);
79 static char *calculate_digest (CT, int);
80
81
82 static unsigned char directives_stack[32];
83 static unsigned int directives_index;
84
85 static int do_direct(void)
86 {
87 return directives_stack[directives_index];
88 }
89
90 static void directive_onoff(int onoff)
91 {
92 if (directives_index >= sizeof(directives_stack) - 1) {
93 fprintf(stderr, "mhbuild: #on/off overflow, continuing\n");
94 return;
95 }
96 directives_stack[++directives_index] = onoff;
97 }
98
99 static void directive_init(int onoff)
100 {
101 directives_index = 0;
102 directives_stack[0] = onoff;
103 }
104
105 static void directive_pop(void)
106 {
107 if (directives_index > 0)
108 directives_index--;
109 else
110 fprintf(stderr, "mhbuild: #pop underflow, continuing\n");
111 }
112
113 /*
114 * Main routine for translating composition file
115 * into valid MIME message. It translates the draft
116 * into a content structure (actually a tree of content
117 * structures). This message then can be manipulated
118 * in various ways, including being output via
119 * output_message().
120 */
121
122 CT
123 build_mime (char *infile, int autobuild, int dist, int directives,
124 int header_encoding, size_t maxunencoded, int verbose)
125 {
126 int compnum, state;
127 char buf[BUFSIZ], name[NAMESZ];
128 char *cp, *np, *vp;
129 struct multipart *m;
130 struct part **pp;
131 CT ct;
132 FILE *in;
133 HF hp;
134 m_getfld_state_t gstate = 0;
135 struct attach_list *attach_head = NULL, *attach_tail = NULL, *at_entry;
136
137 directive_init(directives);
138
139 umask (~m_gmprot ());
140
141 /* open the composition draft */
142 if ((in = fopen (infile, "r")) == NULL)
143 adios (infile, "unable to open for reading");
144
145 /*
146 * Allocate space for primary (outside) content
147 */
148 if ((ct = (CT) calloc (1, sizeof(*ct))) == NULL)
149 adios (NULL, "out of memory");
150
151 /*
152 * Allocate structure for handling decoded content
153 * for this part. We don't really need this, but
154 * allocate it to remain consistent.
155 */
156 init_decoded_content (ct);
157
158 /*
159 * Parse some of the header fields in the composition
160 * draft into the linked list of header fields for
161 * the new MIME message.
162 */
163 m_getfld_track_filepos (&gstate, in);
164 for (compnum = 1;;) {
165 int bufsz = sizeof buf;
166 switch (state = m_getfld (&gstate, name, buf, &bufsz, in)) {
167 case FLD:
168 case FLDPLUS:
169 compnum++;
170
171 /* abort if draft has Mime-Version or C-T-E header field */
172 if (strcasecmp (name, VRSN_FIELD) == 0 ||
173 strcasecmp (name, ENCODING_FIELD) == 0) {
174 if (autobuild) {
175 fclose(in);
176 return NULL;
177 } else {
178 adios (NULL, "draft shouldn't contain %s: field", name);
179 }
180 }
181
182 /* ignore any Content-Type fields in the header */
183 if (!strcasecmp (name, TYPE_FIELD)) {
184 while (state == FLDPLUS) {
185 bufsz = sizeof buf;
186 state = m_getfld (&gstate, name, buf, &bufsz, in);
187 }
188 goto finish_field;
189 }
190
191 /* get copies of the buffers */
192 np = add (name, NULL);
193 vp = add (buf, NULL);
194
195 /* if necessary, get rest of field */
196 while (state == FLDPLUS) {
197 bufsz = sizeof buf;
198 state = m_getfld (&gstate, name, buf, &bufsz, in);
199 vp = add (buf, vp); /* add to previous value */
200 }
201
202 /*
203 * Now add the header data to the list, unless it's an attach
204 * header; in that case, add it to our attach list
205 */
206
207 if (strcasecmp(ATTACH_FIELD, np) == 0) {
208 struct attach_list *entry;
209 char *s = vp, *e = vp + strlen(vp) - 1;
210 free(np);
211
212 /*
213 * Make sure we can find the start of this filename.
214 * If it's blank, we skip completely. Otherwise, strip
215 * off any leading spaces and trailing newlines.
216 */
217
218 while (isspace((unsigned char) *s))
219 s++;
220
221 while (e > s && *e == '\n')
222 *e-- = '\0';
223
224 if (*s == '\0') {
225 free(vp);
226 goto finish_field;
227 }
228
229 entry = mh_xmalloc(sizeof(*entry));
230 entry->filename = getcpy(s);
231 entry->next = NULL;
232 free(vp);
233
234 if (attach_tail) {
235 attach_tail->next = entry;
236 attach_tail = entry;
237 } else {
238 attach_head = attach_tail = entry;
239 }
240 } else {
241 add_header (ct, np, vp);
242 }
243
244 finish_field:
245 /* if this wasn't the last header field, then continue */
246 continue;
247
248 case BODY:
249 fseek (in, (long) (-strlen (buf)), SEEK_CUR);
250 /* fall through */
251 case FILEEOF:
252 break;
253
254 case LENERR:
255 case FMTERR:
256 adios (NULL, "message format error in component #%d", compnum);
257
258 default:
259 adios (NULL, "getfld() returned %d", state);
260 }
261 break;
262 }
263 m_getfld_state_destroy (&gstate);
264
265 /*
266 * Iterate through the list of headers and call the function to MIME-ify
267 * them if required.
268 */
269
270 for (hp = ct->c_first_hf; hp != NULL; hp = hp->next) {
271 if (encode_rfc2047(hp->name, &hp->value, header_encoding, NULL)) {
272 adios(NULL, "Unable to encode header \"%s\"", hp->name);
273 }
274 }
275
276 /*
277 * Now add the MIME-Version header field
278 * to the list of header fields.
279 */
280
281 if (! dist) {
282 np = add (VRSN_FIELD, NULL);
283 vp = concat (" ", VRSN_VALUE, "\n", NULL);
284 add_header (ct, np, vp);
285 }
286
287 /*
288 * We initally assume we will find multiple contents in the
289 * draft. So create a multipart/mixed content to hold everything.
290 * We can remove this later, if it is not needed.
291 */
292 if (get_ctinfo ("multipart/mixed", ct, 0) == NOTOK)
293 done (1);
294 ct->c_type = CT_MULTIPART;
295 ct->c_subtype = MULTI_MIXED;
296 ct->c_file = add (infile, NULL);
297
298 if ((m = (struct multipart *) calloc (1, sizeof(*m))) == NULL)
299 adios (NULL, "out of memory");
300 ct->c_ctparams = (void *) m;
301 pp = &m->mp_parts;
302
303 /*
304 * read and parse the composition file
305 * and the directives it contains.
306 */
307 while (fgetstr (buf, sizeof(buf) - 1, in)) {
308 struct part *part;
309 CT p;
310
311 if (user_content (in, buf, &p) == DONE) {
312 admonish (NULL, "ignoring spurious #end");
313 continue;
314 }
315 if (!p)
316 continue;
317
318 if ((part = (struct part *) calloc (1, sizeof(*part))) == NULL)
319 adios (NULL, "out of memory");
320 *pp = part;
321 pp = &part->mp_next;
322 part->mp_part = p;
323 }
324
325 /*
326 * Add any Attach headers to the list of MIME parts at the end of the
327 * message.
328 */
329
330 for (at_entry = attach_head; at_entry; ) {
331 struct attach_list *at_prev = at_entry;
332 struct part *part;
333 CT p;
334
335 if (access(at_entry->filename, R_OK) != 0) {
336 adios("reading", "Unable to open %s for", at_entry->filename);
337 }
338
339 if ((p = (CT) calloc (1, sizeof(*p))) == NULL)
340 adios(NULL, "out of memory");
341
342 init_decoded_content(p);
343
344 /*
345 * Initialize our content structure based on the filename,
346 * and fill in all of the relevant fields. Also place MIME
347 * parameters in the attributes array.
348 */
349
350 setup_attach_content(p, at_entry->filename);
351
352 if ((part = (struct part *) calloc (1, sizeof(*part))) == NULL)
353 adios (NULL, "out of memory");
354 *pp = part;
355 pp = &part->mp_next;
356 part->mp_part = p;
357
358 at_entry = at_entry->next;
359 free(at_prev->filename);
360 free(at_prev);
361 }
362
363 /*
364 * To allow for empty message bodies, if we've found NO content at all
365 * yet cook up an empty text/plain part.
366 */
367
368 if (!m->mp_parts) {
369 CT p;
370 struct part *part;
371 struct text *t;
372
373 if ((p = (CT) calloc (1, sizeof(*p))) == NULL)
374 adios(NULL, "out of memory");
375
376 init_decoded_content(p);
377
378 if (get_ctinfo ("text/plain", p, 0) == NOTOK)
379 done (1);
380
381 p->c_type = CT_TEXT;
382 p->c_subtype = TEXT_PLAIN;
383 p->c_encoding = CE_7BIT;
384 p->c_file = getcpy(infile);
385 /*
386 * Sigh. ce_file contains the "decoded" contents of this part.
387 * So this seems like the best option available since we're going
388 * to call scan_content() on this.
389 */
390 p->c_cefile.ce_file = getcpy("/dev/null");
391 p->c_begin = ftell(in);
392 p->c_end = ftell(in);
393
394 if ((t = (struct text *) calloc (1, sizeof (*t))) == NULL)
395 adios (NULL, "out of memory");
396
397 t->tx_charset = CHARSET_SPECIFIED;
398 p->c_ctparams = t;
399
400 if ((part = (struct part *) calloc (1, sizeof(*part))) == NULL)
401 adios (NULL, "out of memory");
402 *pp = part;
403 pp = &part->mp_next;
404 part->mp_part = p;
405 }
406
407 /*
408 * close the composition draft since
409 * it's not needed any longer.
410 */
411 fclose (in);
412
413 /*
414 * If only one content was found, then remove and
415 * free the outer multipart content.
416 */
417 if (!m->mp_parts->mp_next) {
418 CT p;
419
420 p = m->mp_parts->mp_part;
421 m->mp_parts->mp_part = NULL;
422
423 /* move header fields */
424 p->c_first_hf = ct->c_first_hf;
425 p->c_last_hf = ct->c_last_hf;
426 ct->c_first_hf = NULL;
427 ct->c_last_hf = NULL;
428
429 free_content (ct);
430 ct = p;
431 } else {
432 set_id (ct, 1);
433 }
434
435 /*
436 * Fill out, or expand directives. Parse and execute
437 * commands specified by profile composition strings.
438 */
439 compose_content (ct, verbose);
440
441 if ((cp = strchr(prefix, 'a')) == NULL)
442 adios (NULL, "internal error(4)");
443
444 /*
445 * Scan the contents. Choose a transfer encoding, and
446 * check if prefix for multipart boundary clashes with
447 * any of the contents.
448 */
449 while (scan_content (ct, maxunencoded) == NOTOK) {
450 if (*cp < 'z') {
451 (*cp)++;
452 } else {
453 if (*++cp == 0)
454 adios (NULL, "giving up trying to find a unique delimiter string");
455 else
456 (*cp)++;
457 }
458 }
459
460 /* Build the rest of the header field structures */
461 if (! dist)
462 build_headers (ct, header_encoding);
463
464 return ct;
465 }
466
467
468 /*
469 * Set up structures for placing unencoded
470 * content when building parts.
471 */
472
473 static int
474 init_decoded_content (CT ct)
475 {
476 ct->c_ceopenfnx = open7Bit; /* since unencoded */
477 ct->c_ceclosefnx = close_encoding;
478 ct->c_cesizefnx = NULL; /* since unencoded */
479
480 return OK;
481 }
482
483
484 static char *
485 fgetstr (char *s, int n, FILE *stream)
486 {
487 char *cp, *ep;
488 int o_n = n;
489
490 while(1) {
491 for (ep = (cp = s) + o_n; cp < ep; ) {
492 int i;
493
494 if (!fgets (cp, n, stream))
495 return (cp != s ? s : NULL);
496
497 if (cp == s && *cp != '#')
498 return s;
499
500 cp += (i = strlen (cp)) - 1;
501 if (i <= 1 || *cp-- != '\n' || *cp != '\\')
502 break;
503 *cp = '\0';
504 n -= (i - 2);
505 }
506
507 if (strcmp(s, "#on\n") == 0) {
508 directive_onoff(1);
509 } else if (strcmp(s, "#off\n") == 0) {
510 directive_onoff(0);
511 } else if (strcmp(s, "#pop\n") == 0) {
512 directive_pop();
513 } else {
514 break;
515 }
516 }
517
518 return s;
519 }
520
521
522 /*
523 * Parse the composition draft for text and directives.
524 * Do initial setup of Content structure.
525 */
526
527 static int
528 user_content (FILE *in, char *buf, CT *ctp)
529 {
530 int extrnal, vrsn;
531 char *cp, **ap;
532 char buffer[BUFSIZ];
533 struct multipart *m;
534 struct part **pp;
535 struct stat st;
536 struct str2init *s2i;
537 CI ci;
538 CT ct;
539 CE ce;
540
541 if (buf[0] == '\n' || (do_direct() && strcmp (buf, "#\n") == 0)) {
542 *ctp = NULL;
543 return OK;
544 }
545
546 /* allocate basic Content structure */
547 if ((ct = (CT) calloc (1, sizeof(*ct))) == NULL)
548 adios (NULL, "out of memory");
549 *ctp = ct;
550
551 /* allocate basic structure for handling decoded content */
552 init_decoded_content (ct);
553 ce = &ct->c_cefile;
554
555 ci = &ct->c_ctinfo;
556 set_id (ct, 0);
557
558 /*
559 * Handle inline text. Check if line
560 * is one of the following forms:
561 *
562 * 1) doesn't begin with '#' (implicit directive)
563 * 2) begins with "##" (implicit directive)
564 * 3) begins with "#<"
565 */
566 if (!do_direct() || buf[0] != '#' || buf[1] == '#' || buf[1] == '<') {
567 int headers;
568 int inlineD;
569 long pos;
570 char content[BUFSIZ];
571 FILE *out;
572 char *cp;
573
574 if ((cp = m_mktemp2(NULL, invo_name, NULL, &out)) == NULL) {
575 adios("mhbuildsbr", "unable to create temporary file in %s",
576 get_temp_dir());
577 }
578
579 /* use a temp file to collect the plain text lines */
580 ce->ce_file = add (cp, NULL);
581 ce->ce_unlink = 1;
582
583 if (do_direct() && (buf[0] == '#' && buf[1] == '<')) {
584 strncpy (content, buf + 2, sizeof(content));
585 inlineD = 1;
586 goto rock_and_roll;
587 } else {
588 inlineD = 0;
589 }
590
591 /* the directive is implicit */
592 strncpy (content, "text/plain", sizeof(content));
593 headers = 0;
594 strncpy (buffer, (!do_direct() || buf[0] != '#') ? buf : buf + 1, sizeof(buffer));
595 for (;;) {
596 int i;
597
598 if (headers >= 0 && do_direct() && uprf (buffer, DESCR_FIELD)
599 && buffer[i = strlen (DESCR_FIELD)] == ':') {
600 headers = 1;
601
602 again_descr:
603 ct->c_descr = add (buffer + i + 1, ct->c_descr);
604 if (!fgetstr (buffer, sizeof(buffer) - 1, in))
605 adios (NULL, "end-of-file after %s: field in plaintext", DESCR_FIELD);
606 switch (buffer[0]) {
607 case ' ':
608 case '\t':
609 i = -1;
610 goto again_descr;
611
612 case '#':
613 adios (NULL, "#-directive after %s: field in plaintext", DESCR_FIELD);
614 /* NOTREACHED */
615
616 default:
617 break;
618 }
619 }
620
621 if (headers >= 0 && do_direct() && uprf (buffer, DISPO_FIELD)
622 && buffer[i = strlen (DISPO_FIELD)] == ':') {
623 headers = 1;
624
625 again_dispo:
626 ct->c_dispo = add (buffer + i + 1, ct->c_dispo);
627 if (!fgetstr (buffer, sizeof(buffer) - 1, in))
628 adios (NULL, "end-of-file after %s: field in plaintext", DISPO_FIELD);
629 switch (buffer[0]) {
630 case ' ':
631 case '\t':
632 i = -1;
633 goto again_dispo;
634
635 case '#':
636 adios (NULL, "#-directive after %s: field in plaintext", DISPO_FIELD);
637 /* NOTREACHED */
638
639 default:
640 break;
641 }
642 }
643
644 if (headers != 1 || buffer[0] != '\n')
645 fputs (buffer, out);
646
647 rock_and_roll:
648 headers = -1;
649 pos = ftell (in);
650 if ((cp = fgetstr (buffer, sizeof(buffer) - 1, in)) == NULL)
651 break;
652 if (do_direct() && buffer[0] == '#') {
653 char *bp;
654
655 if (buffer[1] != '#')
656 break;
657 for (cp = (bp = buffer) + 1; *cp; cp++)
658 *bp++ = *cp;
659 *bp = '\0';
660 }
661 }
662
663 if (listsw)
664 ct->c_end = ftell (out);
665 fclose (out);
666
667 /* parse content type */
668 if (get_ctinfo (content, ct, inlineD) == NOTOK)
669 done (1);
670
671 for (s2i = str2cts; s2i->si_key; s2i++)
672 if (!strcasecmp (ci->ci_type, s2i->si_key))
673 break;
674 if (!s2i->si_key && !uprf (ci->ci_type, "X-"))
675 s2i++;
676
677 /*
678 * check type specified (possibly implicitly)
679 */
680 switch (ct->c_type = s2i->si_val) {
681 case CT_MESSAGE:
682 if (!strcasecmp (ci->ci_subtype, "rfc822")) {
683 ct->c_encoding = CE_7BIT;
684 goto call_init;
685 }
686 /* else fall... */
687 case CT_MULTIPART:
688 adios (NULL, "it doesn't make sense to define an in-line %s content",
689 ct->c_type == CT_MESSAGE ? "message" : "multipart");
690 /* NOTREACHED */
691
692 default:
693 call_init:
694 if ((ct->c_ctinitfnx = s2i->si_init))
695 (*ct->c_ctinitfnx) (ct);
696 break;
697 }
698
699 if (cp)
700 fseek (in, pos, SEEK_SET);
701 return OK;
702 }
703
704 /*
705 * If we've reached this point, the next line
706 * must be some type of explicit directive.
707 */
708
709 /* check if directive is external-type */
710 extrnal = (buf[1] == '@');
711
712 /* parse directive */
713 if (get_ctinfo (buf + (extrnal ? 2 : 1), ct, 1) == NOTOK)
714 done (1);
715
716 /* check directive against the list of MIME types */
717 for (s2i = str2cts; s2i->si_key; s2i++)
718 if (!strcasecmp (ci->ci_type, s2i->si_key))
719 break;
720
721 /*
722 * Check if the directive specified a valid type.
723 * This will happen if it was one of the following forms:
724 *
725 * #type/subtype (or)
726 * #@type/subtype
727 */
728 if (s2i->si_key) {
729 if (!ci->ci_subtype)
730 adios (NULL, "missing subtype in \"#%s\"", ci->ci_type);
731
732 switch (ct->c_type = s2i->si_val) {
733 case CT_MULTIPART:
734 adios (NULL, "use \"#begin ... #end\" instead of \"#%s/%s\"",
735 ci->ci_type, ci->ci_subtype);
736 /* NOTREACHED */
737
738 case CT_MESSAGE:
739 if (!strcasecmp (ci->ci_subtype, "partial"))
740 adios (NULL, "sorry, \"#%s/%s\" isn't supported",
741 ci->ci_type, ci->ci_subtype);
742 if (!strcasecmp (ci->ci_subtype, "external-body"))
743 adios (NULL, "use \"#@type/subtype ... [] ...\" instead of \"#%s/%s\"",
744 ci->ci_type, ci->ci_subtype);
745 use_forw:
746 adios (NULL,
747 "use \"#forw [+folder] [msgs]\" instead of \"#%s/%s\"",
748 ci->ci_type, ci->ci_subtype);
749 /* NOTREACHED */
750
751 default:
752 if ((ct->c_ctinitfnx = s2i->si_init))
753 (*ct->c_ctinitfnx) (ct);
754 break;
755 }
756
757 /*
758 * #@type/subtype (external types directive)
759 */
760 if (extrnal) {
761 struct exbody *e;
762 CT p;
763
764 if (!ci->ci_magic)
765 adios (NULL, "need external information for \"#@%s/%s\"",
766 ci->ci_type, ci->ci_subtype);
767 p = ct;
768
769 snprintf (buffer, sizeof(buffer), "message/external-body; %s", ci->ci_magic);
770 free (ci->ci_magic);
771 ci->ci_magic = NULL;
772
773 /*
774 * Since we are using the current Content structure to
775 * hold information about the type of the external
776 * reference, we need to create another Content structure
777 * for the message/external-body to wrap it in.
778 */
779 if ((ct = (CT) calloc (1, sizeof(*ct))) == NULL)
780 adios (NULL, "out of memory");
781 *ctp = ct;
782 ci = &ct->c_ctinfo;
783 if (get_ctinfo (buffer, ct, 0) == NOTOK)
784 done (1);
785 ct->c_type = CT_MESSAGE;
786 ct->c_subtype = MESSAGE_EXTERNAL;
787
788 if ((e = (struct exbody *) calloc (1, sizeof(*e))) == NULL)
789 adios (NULL, "out of memory");
790 ct->c_ctparams = (void *) e;
791
792 e->eb_parent = ct;
793 e->eb_content = p;
794 p->c_ctexbody = e;
795
796 if (params_external (ct, 1) == NOTOK)
797 done (1);
798
799 return OK;
800 }
801
802 /* Handle [file] argument */
803 if (ci->ci_magic) {
804 /* check if specifies command to execute */
805 if (*ci->ci_magic == '|' || *ci->ci_magic == '!') {
806 for (cp = ci->ci_magic + 1; isspace ((unsigned char) *cp); cp++)
807 continue;
808 if (!*cp)
809 adios (NULL, "empty pipe command for #%s directive", ci->ci_type);
810 cp = add (cp, NULL);
811 free (ci->ci_magic);
812 ci->ci_magic = cp;
813 } else {
814 /* record filename of decoded contents */
815 ce->ce_file = ci->ci_magic;
816 if (access (ce->ce_file, R_OK) == NOTOK)
817 adios ("reading", "unable to access %s for", ce->ce_file);
818 if (listsw && stat (ce->ce_file, &st) != NOTOK)
819 ct->c_end = (long) st.st_size;
820 ci->ci_magic = NULL;
821 }
822 return OK;
823 }
824
825 /*
826 * No [file] argument, so check profile for
827 * method to compose content.
828 */
829 snprintf (buffer, sizeof(buffer), "%s-compose-%s/%s",
830 invo_name, ci->ci_type, ci->ci_subtype);
831 if ((cp = context_find (buffer)) == NULL || *cp == '\0') {
832 snprintf (buffer, sizeof(buffer), "%s-compose-%s", invo_name, ci->ci_type);
833 if ((cp = context_find (buffer)) == NULL || *cp == '\0') {
834 content_error (NULL, ct, "don't know how to compose content");
835 done (1);
836 }
837 }
838 ci->ci_magic = add (cp, NULL);
839 return OK;
840 }
841
842 if (extrnal)
843 adios (NULL, "external definition not allowed for \"#%s\"", ci->ci_type);
844
845 /*
846 * Message directive
847 * #forw [+folder] [msgs]
848 */
849 if (!strcasecmp (ci->ci_type, "forw")) {
850 int msgnum;
851 char *folder, *arguments[MAXARGS];
852 struct msgs *mp;
853
854 if (ci->ci_magic) {
855 ap = brkstring (ci->ci_magic, " ", "\n");
856 copyip (ap, arguments, MAXARGS);
857 } else {
858 arguments[0] = "cur";
859 arguments[1] = NULL;
860 }
861 folder = NULL;
862
863 /* search the arguments for a folder name */
864 for (ap = arguments; *ap; ap++) {
865 cp = *ap;
866 if (*cp == '+' || *cp == '@') {
867 if (folder)
868 adios (NULL, "only one folder per #forw directive");
869 else
870 folder = pluspath (cp);
871 }
872 }
873
874 /* else, use the current folder */
875 if (!folder)
876 folder = add (getfolder (1), NULL);
877
878 if (!(mp = folder_read (folder, 0)))
879 adios (NULL, "unable to read folder %s", folder);
880 for (ap = arguments; *ap; ap++) {
881 cp = *ap;
882 if (*cp != '+' && *cp != '@')
883 if (!m_convert (mp, cp))
884 done (1);
885 }
886 free (folder);
887 free_ctinfo (ct);
888
889 /*
890 * If there is more than one message to include, make this
891 * a content of type "multipart/digest" and insert each message
892 * as a subpart. If there is only one message, then make this
893 * a content of type "message/rfc822".
894 */
895 if (mp->numsel > 1) {
896 /* we are forwarding multiple messages */
897 if (get_ctinfo ("multipart/digest", ct, 0) == NOTOK)
898 done (1);
899 ct->c_type = CT_MULTIPART;
900 ct->c_subtype = MULTI_DIGEST;
901
902 if ((m = (struct multipart *) calloc (1, sizeof(*m))) == NULL)
903 adios (NULL, "out of memory");
904 ct->c_ctparams = (void *) m;
905 pp = &m->mp_parts;
906
907 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
908 if (is_selected(mp, msgnum)) {
909 struct part *part;
910 CT p;
911 CE pe;
912
913 if ((p = (CT) calloc (1, sizeof(*p))) == NULL)
914 adios (NULL, "out of memory");
915 init_decoded_content (p);
916 pe = &p->c_cefile;
917 if (get_ctinfo ("message/rfc822", p, 0) == NOTOK)
918 done (1);
919 p->c_type = CT_MESSAGE;
920 p->c_subtype = MESSAGE_RFC822;
921
922 snprintf (buffer, sizeof(buffer), "%s/%d", mp->foldpath, msgnum);
923 pe->ce_file = add (buffer, NULL);
924 if (listsw && stat (pe->ce_file, &st) != NOTOK)
925 p->c_end = (long) st.st_size;
926
927 if ((part = (struct part *) calloc (1, sizeof(*part))) == NULL)
928 adios (NULL, "out of memory");
929 *pp = part;
930 pp = &part->mp_next;
931 part->mp_part = p;
932 }
933 }
934 } else {
935 /* we are forwarding one message */
936 if (get_ctinfo ("message/rfc822", ct, 0) == NOTOK)
937 done (1);
938 ct->c_type = CT_MESSAGE;
939 ct->c_subtype = MESSAGE_RFC822;
940
941 msgnum = mp->lowsel;
942 snprintf (buffer, sizeof(buffer), "%s/%d", mp->foldpath, msgnum);
943 ce->ce_file = add (buffer, NULL);
944 if (listsw && stat (ce->ce_file, &st) != NOTOK)
945 ct->c_end = (long) st.st_size;
946 }
947
948 folder_free (mp); /* free folder/message structure */
949 return OK;
950 }
951
952 /*
953 * #end
954 */
955 if (!strcasecmp (ci->ci_type, "end")) {
956 free_content (ct);
957 *ctp = NULL;
958 return DONE;
959 }
960
961 /*
962 * #begin [ alternative | parallel ]
963 */
964 if (!strcasecmp (ci->ci_type, "begin")) {
965 if (!ci->ci_magic) {
966 vrsn = MULTI_MIXED;
967 cp = SubMultiPart[vrsn - 1].kv_key;
968 } else if (!strcasecmp (ci->ci_magic, "alternative")) {
969 vrsn = MULTI_ALTERNATE;
970 cp = SubMultiPart[vrsn - 1].kv_key;
971 } else if (!strcasecmp (ci->ci_magic, "parallel")) {
972 vrsn = MULTI_PARALLEL;
973 cp = SubMultiPart[vrsn - 1].kv_key;
974 } else if (uprf (ci->ci_magic, "digest")) {
975 goto use_forw;
976 } else {
977 vrsn = MULTI_UNKNOWN;
978 cp = ci->ci_magic;
979 }
980
981 free_ctinfo (ct);
982 snprintf (buffer, sizeof(buffer), "multipart/%s", cp);
983 if (get_ctinfo (buffer, ct, 0) == NOTOK)
984 done (1);
985 ct->c_type = CT_MULTIPART;
986 ct->c_subtype = vrsn;
987
988 if ((m = (struct multipart *) calloc (1, sizeof(*m))) == NULL)
989 adios (NULL, "out of memory");
990 ct->c_ctparams = (void *) m;
991
992 pp = &m->mp_parts;
993 while (fgetstr (buffer, sizeof(buffer) - 1, in)) {
994 struct part *part;
995 CT p;
996
997 if (user_content (in, buffer, &p) == DONE) {
998 if (!m->mp_parts)
999 adios (NULL, "empty \"#begin ... #end\" sequence");
1000 return OK;
1001 }
1002 if (!p)
1003 continue;
1004
1005 if ((part = (struct part *) calloc (1, sizeof(*part))) == NULL)
1006 adios (NULL, "out of memory");
1007 *pp = part;
1008 pp = &part->mp_next;
1009 part->mp_part = p;
1010 }
1011 admonish (NULL, "premature end-of-file, missing #end");
1012 return OK;
1013 }
1014
1015 /*
1016 * Unknown directive
1017 */
1018 adios (NULL, "unknown directive \"#%s\"", ci->ci_type);
1019 return NOTOK; /* NOT REACHED */
1020 }
1021
1022
1023 static void
1024 set_id (CT ct, int top)
1025 {
1026 char contentid[BUFSIZ];
1027 static int partno;
1028 static time_t clock = 0;
1029 static char *msgfmt;
1030
1031 if (clock == 0) {
1032 time (&clock);
1033 snprintf (contentid, sizeof(contentid), "%s\n", message_id (clock, 1));
1034 partno = 0;
1035 msgfmt = getcpy(contentid);
1036 }
1037 snprintf (contentid, sizeof(contentid), msgfmt, top ? 0 : ++partno);
1038 ct->c_id = getcpy (contentid);
1039 }
1040
1041
1042 /*
1043 * Fill out, or expand the various contents in the composition
1044 * draft. Read-in any necessary files. Parse and execute any
1045 * commands specified by profile composition strings.
1046 */
1047
1048 static int
1049 compose_content (CT ct, int verbose)
1050 {
1051 CE ce = &ct->c_cefile;
1052
1053 switch (ct->c_type) {
1054 case CT_MULTIPART:
1055 {
1056 int partnum;
1057 char *pp;
1058 char partnam[BUFSIZ];
1059 struct multipart *m = (struct multipart *) ct->c_ctparams;
1060 struct part *part;
1061
1062 if (ct->c_partno) {
1063 snprintf (partnam, sizeof(partnam), "%s.", ct->c_partno);
1064 pp = partnam + strlen (partnam);
1065 } else {
1066 pp = partnam;
1067 }
1068
1069 /* first, we call compose_content on all the subparts */
1070 for (part = m->mp_parts, partnum = 1; part; part = part->mp_next, partnum++) {
1071 CT p = part->mp_part;
1072
1073 sprintf (pp, "%d", partnum);
1074 p->c_partno = add (partnam, NULL);
1075 if (compose_content (p, verbose) == NOTOK)
1076 return NOTOK;
1077 }
1078
1079 /*
1080 * If the -rfc934mode switch is given, then check all
1081 * the subparts of a multipart/digest. If they are all
1082 * message/rfc822, then mark this content and all
1083 * subparts with the rfc934 compatibility mode flag.
1084 */
1085 if (rfc934sw && ct->c_subtype == MULTI_DIGEST) {
1086 int is934 = 1;
1087
1088 for (part = m->mp_parts; part; part = part->mp_next) {
1089 CT p = part->mp_part;
1090
1091 if (p->c_subtype != MESSAGE_RFC822) {
1092 is934 = 0;
1093 break;
1094 }
1095 }
1096 ct->c_rfc934 = is934;
1097 for (part = m->mp_parts; part; part = part->mp_next) {
1098 CT p = part->mp_part;
1099
1100 if ((p->c_rfc934 = is934))
1101 p->c_end++;
1102 }
1103 }
1104
1105 if (listsw) {
1106 ct->c_end = (partnum = strlen (prefix) + 2) + 2;
1107 if (ct->c_rfc934)
1108 ct->c_end += 1;
1109
1110 for (part = m->mp_parts; part; part = part->mp_next)
1111 ct->c_end += part->mp_part->c_end + partnum;
1112 }
1113 }
1114 break;
1115
1116 case CT_MESSAGE:
1117 /* Nothing to do for type message */
1118 break;
1119
1120 /*
1121 * Discrete types (text/application/audio/image/video)
1122 */
1123 default:
1124 if (!ce->ce_file) {
1125 pid_t child_id;
1126 int i, xstdout, len, buflen;
1127 char *bp, *cp;
1128 char *vec[4], buffer[BUFSIZ];
1129 FILE *out;
1130 CI ci = &ct->c_ctinfo;
1131 char *tfile = NULL;
1132
1133 if (!(cp = ci->ci_magic))
1134 adios (NULL, "internal error(5)");
1135
1136 if ((tfile = m_mktemp2(NULL, invo_name, NULL, NULL)) == NULL) {
1137 adios("mhbuildsbr", "unable to create temporary file in %s",
1138 get_temp_dir());
1139 }
1140 ce->ce_file = add (tfile, NULL);
1141 ce->ce_unlink = 1;
1142
1143 xstdout = 0;
1144
1145 /* Get buffer ready to go */
1146 bp = buffer;
1147 bp[0] = '\0';
1148 buflen = sizeof(buffer);
1149
1150 /*
1151 * Parse composition string into buffer
1152 */
1153 for ( ; *cp; cp++) {
1154 if (*cp == '%') {
1155 switch (*++cp) {
1156 case 'a':
1157 {
1158 /* insert parameters from directive */
1159 char *s = "";
1160 PM pm;
1161
1162 for (pm = ci->ci_first_pm; pm; pm = pm->pm_next) {
1163 snprintf (bp, buflen, "%s%s=\"%s\"", s,
1164 pm->pm_name, get_param_value(pm, '?'));
1165 len = strlen (bp);
1166 bp += len;
1167 buflen -= len;
1168 s = " ";
1169 }
1170 }
1171 break;
1172
1173 case 'F':
1174 /* %f, and stdout is not-redirected */
1175 xstdout = 1;
1176 /* and fall... */
1177
1178 case 'f':
1179 /*
1180 * insert temporary filename where
1181 * content should be written
1182 */
1183 snprintf (bp, buflen, "%s", ce->ce_file);
1184 break;
1185
1186 case 's':
1187 /* insert content subtype */
1188 strncpy (bp, ci->ci_subtype, buflen);
1189 break;
1190
1191 case '%':
1192 /* insert character % */
1193 goto raw;
1194
1195 default:
1196 *bp++ = *--cp;
1197 *bp = '\0';
1198 buflen--;
1199 continue;
1200 }
1201 len = strlen (bp);
1202 bp += len;
1203 buflen -= len;
1204 } else {
1205 raw:
1206 *bp++ = *cp;
1207 *bp = '\0';
1208 buflen--;
1209 }
1210 }
1211
1212 if (verbose)
1213 printf ("composing content %s/%s from command\n\t%s\n",
1214 ci->ci_type, ci->ci_subtype, buffer);
1215
1216 fflush (stdout); /* not sure if need for -noverbose */
1217
1218 vec[0] = "/bin/sh";
1219 vec[1] = "-c";
1220 vec[2] = buffer;
1221 vec[3] = NULL;
1222
1223 if ((out = fopen (ce->ce_file, "w")) == NULL)
1224 adios (ce->ce_file, "unable to open for writing");
1225
1226 for (i = 0; (child_id = fork()) == NOTOK && i > 5; i++)
1227 sleep (5);
1228 switch (child_id) {
1229 case NOTOK:
1230 adios ("fork", "unable to fork");
1231 /* NOTREACHED */
1232
1233 case OK:
1234 if (!xstdout)
1235 dup2 (fileno (out), 1);
1236 close (fileno (out));
1237 execvp ("/bin/sh", vec);
1238 fprintf (stderr, "unable to exec ");
1239 perror ("/bin/sh");
1240 _exit (-1);
1241 /* NOTREACHED */
1242
1243 default:
1244 fclose (out);
1245 if (pidXwait(child_id, NULL))
1246 done (1);
1247 break;
1248 }
1249 }
1250
1251 /* Check size of file */
1252 if (listsw && ct->c_end == 0L) {
1253 struct stat st;
1254
1255 if (stat (ce->ce_file, &st) != NOTOK)
1256 ct->c_end = (long) st.st_size;
1257 }
1258 break;
1259 }
1260
1261 return OK;
1262 }
1263
1264
1265 /*
1266 * Scan the content.
1267 *
1268 * 1) choose a transfer encoding.
1269 * 2) check for clashes with multipart boundary string.
1270 * 3) for text content, figure out which character set is being used.
1271 *
1272 * If there is a clash with one of the contents and the multipart boundary,
1273 * this function will exit with NOTOK. This will cause the scanning process
1274 * to be repeated with a different multipart boundary. It is possible
1275 * (although highly unlikely) that this scan will be repeated multiple times.
1276 */
1277
1278 static int
1279 scan_content (CT ct, size_t maxunencoded)
1280 {
1281 int len;
1282 int check8bit = 0, contains8bit = 0; /* check if contains 8bit data */
1283 int checklinelen = 0, linelen = 0; /* check for long lines */
1284 int checkllinelen = 0; /* check for extra-long lines */
1285 int checkboundary = 0, boundaryclash = 0; /* check if clashes with multipart boundary */
1286 int checklinespace = 0, linespace = 0; /* check if any line ends with space */
1287 char *cp = NULL, buffer[BUFSIZ];
1288 struct text *t = NULL;
1289 FILE *in = NULL;
1290 CE ce = &ct->c_cefile;
1291
1292 /*
1293 * handle multipart by scanning all subparts
1294 * and then checking their encoding.
1295 */
1296 if (ct->c_type == CT_MULTIPART) {
1297 struct multipart *m = (struct multipart *) ct->c_ctparams;
1298 struct part *part;
1299
1300 /* initially mark the domain of enclosing multipart as 7bit */
1301 ct->c_encoding = CE_7BIT;
1302
1303 for (part = m->mp_parts; part; part = part->mp_next) {
1304 CT p = part->mp_part;
1305
1306 if (scan_content (p, maxunencoded) == NOTOK) /* choose encoding for subpart */
1307 return NOTOK;
1308
1309 /* if necessary, enlarge encoding for enclosing multipart */
1310 if (p->c_encoding == CE_BINARY)
1311 ct->c_encoding = CE_BINARY;
1312 if (p->c_encoding == CE_8BIT && ct->c_encoding != CE_BINARY)
1313 ct->c_encoding = CE_8BIT;
1314 }
1315
1316 return OK;
1317 }
1318
1319 /*
1320 * Decide what to check while scanning this content. Note that
1321 * for text content we always check for 8bit characters if the
1322 * charset is unspecified, because that controls whether or not the
1323 * character set is us-ascii or retrieved from the locale.
1324 */
1325
1326 if (ct->c_type == CT_TEXT) {
1327 t = (struct text *) ct->c_ctparams;
1328 if (t->tx_charset == CHARSET_UNSPECIFIED)
1329 check8bit = 1;
1330 }
1331
1332 switch (ct->c_reqencoding) {
1333 case CE_8BIT:
1334 checkllinelen = 1;
1335 checkboundary = 1;
1336 break;
1337 case CE_QUOTED:
1338 checkboundary = 1;
1339 break;
1340 case CE_BASE64:
1341 break;
1342 case CE_UNKNOWN:
1343 /* Use the default rules based on content-type */
1344 switch (ct->c_type) {
1345 case CT_TEXT:
1346 checkboundary = 1;
1347 checklinelen = 1;
1348 if (ct->c_subtype == TEXT_PLAIN) {
1349 checklinespace = 0;
1350 } else {
1351 checklinespace = 1;
1352 }
1353 break;
1354
1355 case CT_APPLICATION:
1356 check8bit = 1;
1357 checklinelen = 1;
1358 checklinespace = 1;
1359 checkboundary = 1;
1360 break;
1361
1362 case CT_MESSAGE:
1363 check8bit = 0;
1364 checklinelen = 0;
1365 checklinespace = 0;
1366
1367 /* don't check anything for message/external */
1368 if (ct->c_subtype == MESSAGE_EXTERNAL)
1369 checkboundary = 0;
1370 else
1371 checkboundary = 1;
1372 break;
1373
1374 case CT_AUDIO:
1375 case CT_IMAGE:
1376 case CT_VIDEO:
1377 /*
1378 * Don't check anything for these types,
1379 * since we are forcing use of base64, unless
1380 * the content-type was specified by a mhbuild directive.
1381 */
1382 check8bit = 0;
1383 checklinelen = 0;
1384 checklinespace = 0;
1385 checkboundary = 0;
1386 break;
1387 }
1388 }
1389
1390 /*
1391 * Scan the unencoded content
1392 */
1393 if (check8bit || checklinelen || checklinespace || checkboundary ||
1394 checkllinelen) {
1395 if ((in = fopen (ce->ce_file, "r")) == NULL)
1396 adios (ce->ce_file, "unable to open for reading");
1397 len = strlen (prefix);
1398
1399 while (fgets (buffer, sizeof(buffer) - 1, in)) {
1400 /*
1401 * Check for 8bit data.
1402 */
1403 if (check8bit) {
1404 for (cp = buffer; *cp; cp++) {
1405 if (!isascii ((unsigned char) *cp)) {
1406 contains8bit = 1;
1407 check8bit = 0; /* no need to keep checking */
1408 }
1409 }
1410 }
1411
1412 /*
1413 * Check line length.
1414 */
1415 if (checklinelen && (strlen (buffer) > maxunencoded + 1)) {
1416 linelen = 1;
1417 checklinelen = 0; /* no need to keep checking */
1418 }
1419
1420 /*
1421 * RFC 5322 specifies that a message cannot contain a line
1422 * greater than 998 characters (excluding the CRLF). If we
1423 * get one of those lines and linelen is NOT set, then abort.
1424 */
1425
1426 if (checkllinelen && !linelen &&
1427 (strlen(buffer) > MAXLONGLINE + 1)) {
1428 adios(NULL, "Line in content exceeds maximum line limit (%d)",
1429 MAXLONGLINE);
1430 }
1431
1432 /*
1433 * Check if line ends with a space.
1434 */
1435 if (checklinespace && (cp = buffer + strlen (buffer) - 2) > buffer && isspace ((unsigned char) *cp)) {
1436 linespace = 1;
1437 checklinespace = 0; /* no need to keep checking */
1438 }
1439
1440 /*
1441 * Check if content contains a line that clashes
1442 * with our standard boundary for multipart messages.
1443 */
1444 if (checkboundary && buffer[0] == '-' && buffer[1] == '-') {
1445 for (cp = buffer + strlen (buffer) - 1; cp >= buffer; cp--)
1446 if (!isspace ((unsigned char) *cp))
1447 break;
1448 *++cp = '\0';
1449 if (!strncmp(buffer + 2, prefix, len) && isdigit((unsigned char) buffer[2 + len])) {
1450 boundaryclash = 1;
1451 checkboundary = 0; /* no need to keep checking */
1452 }
1453 }
1454 }
1455 fclose (in);
1456 }
1457
1458 /*
1459 * If the content is text and didn't specify a character set,
1460 * we need to figure out which one was used.
1461 */
1462
1463 if (ct->c_type == CT_TEXT) {
1464 t = (struct text *) ct->c_ctparams;
1465 if (t->tx_charset == CHARSET_UNSPECIFIED) {
1466 CI ci = &ct->c_ctinfo;
1467
1468 add_param(&ci->ci_first_pm, &ci->ci_last_pm, "charset",
1469 contains8bit ? write_charset_8bit() : "us-ascii", 0);
1470 t->tx_charset = CHARSET_SPECIFIED;
1471 }
1472 }
1473
1474 /*
1475 * Decide which transfer encoding to use.
1476 */
1477
1478 if (ct->c_reqencoding != CE_UNKNOWN)
1479 ct->c_encoding = ct->c_reqencoding;
1480 else
1481 switch (ct->c_type) {
1482 case CT_TEXT:
1483 if (contains8bit && !linelen && !linespace && !checksw)
1484 ct->c_encoding = CE_8BIT;
1485 else if (contains8bit || linelen || linespace || checksw)
1486 ct->c_encoding = CE_QUOTED;
1487 else
1488 ct->c_encoding = CE_7BIT;
1489 break;
1490
1491 case CT_APPLICATION:
1492 /* For application type, use base64, except when postscript */
1493 if (contains8bit || linelen || linespace || checksw)
1494 ct->c_encoding = (ct->c_subtype == APPLICATION_POSTSCRIPT)
1495 ? CE_QUOTED : CE_BASE64;
1496 else
1497 ct->c_encoding = CE_7BIT;
1498 break;
1499
1500 case CT_MESSAGE:
1501 ct->c_encoding = CE_7BIT;
1502 break;
1503
1504 case CT_AUDIO:
1505 case CT_IMAGE:
1506 case CT_VIDEO:
1507 /* For audio, image, and video contents, just use base64 */
1508 ct->c_encoding = CE_BASE64;
1509 break;
1510 }
1511
1512 return (boundaryclash ? NOTOK : OK);
1513 }
1514
1515
1516 /*
1517 * Scan the content structures, and build header
1518 * fields that will need to be output into the
1519 * message.
1520 */
1521
1522 static int
1523 build_headers (CT ct, int header_encoding)
1524 {
1525 int cc, mailbody, extbody, len;
1526 char *np, *vp, buffer[BUFSIZ];
1527 CI ci = &ct->c_ctinfo;
1528
1529 /*
1530 * If message is type multipart, then add the multipart
1531 * boundary to the list of attribute/value pairs.
1532 */
1533 if (ct->c_type == CT_MULTIPART) {
1534 static int level = 0; /* store nesting level */
1535
1536 snprintf (buffer, sizeof(buffer), "%s%d", prefix, level++);
1537 add_param(&ci->ci_first_pm, &ci->ci_last_pm, "boundary", buffer, 0);
1538 }
1539
1540 /*
1541 * Skip the output of Content-Type, parameters, content
1542 * description and disposition, and Content-ID if the
1543 * content is of type "message" and the rfc934 compatibility
1544 * flag is set (which means we are inside multipart/digest
1545 * and the switch -rfc934mode was given).
1546 */
1547 if (ct->c_type == CT_MESSAGE && ct->c_rfc934)
1548 goto skip_headers;
1549
1550 /*
1551 * output the content type and subtype
1552 */
1553 np = add (TYPE_FIELD, NULL);
1554 vp = concat (" ", ci->ci_type, "/", ci->ci_subtype, NULL);
1555
1556 /* keep track of length of line */
1557 len = strlen (TYPE_FIELD) + strlen (ci->ci_type)
1558 + strlen (ci->ci_subtype) + 3;
1559
1560 extbody = ct->c_type == CT_MESSAGE && ct->c_subtype == MESSAGE_EXTERNAL;
1561 mailbody = extbody && ((struct exbody *) ct->c_ctparams)->eb_body;
1562
1563 /*
1564 * Append the attribute/value pairs to
1565 * the end of the Content-Type line.
1566 */
1567
1568 if (ci->ci_first_pm) {
1569 char *s = output_params(len, ci->ci_first_pm, &len, mailbody);
1570
1571 if (!s)
1572 adios(NULL, "Internal error: failed outputting Content-Type "
1573 "parameters");
1574
1575 vp = add (s, vp);
1576 free(s);
1577 }
1578
1579 /*
1580 * Append any RFC-822 comment to the end of
1581 * the Content-Type line.
1582 */
1583 if (ci->ci_comment) {
1584 snprintf (buffer, sizeof(buffer), "(%s)", ci->ci_comment);
1585 if (len + 1 + (cc = 2 + strlen (ci->ci_comment)) >= CPERLIN) {
1586 vp = add ("\n\t", vp);
1587 len = 8;
1588 } else {
1589 vp = add (" ", vp);
1590 len++;
1591 }
1592 vp = add (buffer, vp);
1593 len += cc;
1594 }
1595 vp = add ("\n", vp);
1596 add_header (ct, np, vp);
1597
1598 /*
1599 * output the Content-ID, unless disabled by -nocontentid
1600 */
1601 if (contentidsw && ct->c_id) {
1602 np = add (ID_FIELD, NULL);
1603 vp = concat (" ", ct->c_id, NULL);
1604 add_header (ct, np, vp);
1605 }
1606 /*
1607 * output the Content-Description
1608 */
1609 if (ct->c_descr) {
1610 np = add (DESCR_FIELD, NULL);
1611 vp = concat (" ", ct->c_descr, NULL);
1612 if (encode_rfc2047(DESCR_FIELD, &vp, header_encoding, NULL))
1613 adios(NULL, "Unable to encode %s header", DESCR_FIELD);
1614 add_header (ct, np, vp);
1615 }
1616
1617 /*
1618 * output the Content-Disposition. If it's NULL but c_dispo_type is
1619 * set, then we need to build it.
1620 */
1621 if (ct->c_dispo) {
1622 np = add (DISPO_FIELD, NULL);
1623 vp = concat (" ", ct->c_dispo, NULL);
1624 add_header (ct, np, vp);
1625 } else if (ct->c_dispo_type) {
1626 vp = concat (" ", ct->c_dispo_type, NULL);
1627 len = strlen(DISPO_FIELD) + strlen(vp) + 1;
1628 np = output_params(len, ct->c_dispo_first, NULL, 0);
1629 vp = add(np, vp);
1630 vp = add("\n", vp);
1631 if (np)
1632 free(np);
1633 add_header (ct, getcpy(DISPO_FIELD), vp);
1634 }
1635
1636 skip_headers:
1637 /*
1638 * If this is the internal content structure for a
1639 * "message/external", then we are done with the
1640 * headers (since it has no body).
1641 */
1642 if (ct->c_ctexbody)
1643 return OK;
1644
1645 /*
1646 * output the Content-MD5
1647 */
1648 if (checksw) {
1649 np = add (MD5_FIELD, NULL);
1650 vp = calculate_digest (ct, (ct->c_encoding == CE_QUOTED) ? 1 : 0);
1651 add_header (ct, np, vp);
1652 }
1653
1654 /*
1655 * output the Content-Transfer-Encoding
1656 */
1657 switch (ct->c_encoding) {
1658 case CE_7BIT:
1659 /* Nothing to output */
1660 break;
1661
1662 case CE_8BIT:
1663 if (ct->c_type == CT_MESSAGE)
1664 adios (NULL, "internal error, invalid encoding");
1665
1666 np = add (ENCODING_FIELD, NULL);
1667 vp = concat (" ", "8bit", "\n", NULL);
1668 add_header (ct, np, vp);
1669 break;
1670
1671 case CE_QUOTED:
1672 if (ct->c_type == CT_MESSAGE || ct->c_type == CT_MULTIPART)
1673 adios (NULL, "internal error, invalid encoding");
1674
1675 np = add (ENCODING_FIELD, NULL);
1676 vp = concat (" ", "quoted-printable", "\n", NULL);
1677 add_header (ct, np, vp);
1678 break;
1679
1680 case CE_BASE64:
1681 if (ct->c_type == CT_MESSAGE || ct->c_type == CT_MULTIPART)
1682 adios (NULL, "internal error, invalid encoding");
1683
1684 np = add (ENCODING_FIELD, NULL);
1685 vp = concat (" ", "base64", "\n", NULL);
1686 add_header (ct, np, vp);
1687 break;
1688
1689 case CE_BINARY:
1690 if (ct->c_type == CT_MESSAGE)
1691 adios (NULL, "internal error, invalid encoding");
1692
1693 np = add (ENCODING_FIELD, NULL);
1694 vp = concat (" ", "binary", "\n", NULL);
1695 add_header (ct, np, vp);
1696 break;
1697
1698 default:
1699 adios (NULL, "unknown transfer encoding in content");
1700 break;
1701 }
1702
1703 /*
1704 * Additional content specific header processing
1705 */
1706 switch (ct->c_type) {
1707 case CT_MULTIPART:
1708 {
1709 struct multipart *m;
1710 struct part *part;
1711
1712 m = (struct multipart *) ct->c_ctparams;
1713 for (part = m->mp_parts; part; part = part->mp_next) {
1714 CT p;
1715
1716 p = part->mp_part;
1717 build_headers (p, header_encoding);
1718 }
1719 }
1720 break;
1721
1722 case CT_MESSAGE:
1723 if (ct->c_subtype == MESSAGE_EXTERNAL) {
1724 struct exbody *e;
1725
1726 e = (struct exbody *) ct->c_ctparams;
1727 build_headers (e->eb_content, header_encoding);
1728 }
1729 break;
1730
1731 default:
1732 /* Nothing to do */
1733 break;
1734 }
1735
1736 return OK;
1737 }
1738
1739
1740 static char nib2b64[0x40+1] =
1741 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
1742
1743 static char *
1744 calculate_digest (CT ct, int asciiP)
1745 {
1746 int cc;
1747 char buffer[BUFSIZ], *vp, *op;
1748 unsigned char *dp;
1749 unsigned char digest[16];
1750 unsigned char outbuf[25];
1751 MD5_CTX mdContext;
1752 CE ce = &ct->c_cefile;
1753 char *infilename = ce->ce_file ? ce->ce_file : ct->c_file;
1754 FILE *in;
1755
1756 /* open content */
1757 if ((in = fopen (infilename, "r")) == NULL)
1758 adios (infilename, "unable to open for reading");
1759
1760 /* Initialize md5 context */
1761 MD5Init (&mdContext);
1762
1763 /* calculate md5 message digest */
1764 if (asciiP) {
1765 while (fgets (buffer, sizeof(buffer) - 1, in)) {
1766 char c, *cp;
1767
1768 cp = buffer + strlen (buffer) - 1;
1769 if ((c = *cp) == '\n')
1770 *cp = '\0';
1771
1772 MD5Update (&mdContext, (unsigned char *) buffer,
1773 (unsigned int) strlen (buffer));
1774
1775 if (c == '\n')
1776 MD5Update (&mdContext, (unsigned char *) "\r\n", 2);
1777 }
1778 } else {
1779 while ((cc = fread (buffer, sizeof(*buffer), sizeof(buffer), in)) > 0)
1780 MD5Update (&mdContext, (unsigned char *) buffer, (unsigned int) cc);
1781 }
1782
1783 /* md5 finalization. Write digest and zero md5 context */
1784 MD5Final (digest, &mdContext);
1785
1786 /* close content */
1787 fclose (in);
1788
1789 /* print debugging info */
1790 if (debugsw) {
1791 unsigned char *ep;
1792
1793 fprintf (stderr, "MD5 digest=");
1794 for (ep = (dp = digest) + sizeof(digest) / sizeof(digest[0]);
1795 dp < ep; dp++)
1796 fprintf (stderr, "%02x", *dp & 0xff);
1797 fprintf (stderr, "\n");
1798 }
1799
1800 /* encode the digest using base64 */
1801 for (dp = digest, op = (char *) outbuf,
1802 cc = sizeof(digest) / sizeof(digest[0]);
1803 cc > 0; cc -= 3, op += 4) {
1804 unsigned long bits;
1805 char *bp;
1806
1807 bits = (*dp++ & 0xff) << 16;
1808 if (cc > 1) {
1809 bits |= (*dp++ & 0xff) << 8;
1810 if (cc > 2)
1811 bits |= *dp++ & 0xff;
1812 }
1813
1814 for (bp = op + 4; bp > op; bits >>= 6)
1815 *--bp = nib2b64[bits & 0x3f];
1816 if (cc < 3) {
1817 *(op + 3) = '=';
1818 if (cc < 2)
1819 *(op + 2) = '=';
1820 }
1821 }
1822
1823 /* null terminate string */
1824 outbuf[24] = '\0';
1825
1826 /* now make copy and return string */
1827 vp = concat (" ", outbuf, "\n", NULL);
1828 return vp;
1829 }
1830
1831 /*
1832 * Set things up for the content structure for file "filename" that
1833 * we want to attach
1834 */
1835
1836 static void
1837 setup_attach_content(CT ct, char *filename)
1838 {
1839 char *type, *simplename = r1bindex(filename, '/');
1840 struct str2init *s2i;
1841 PM pm;
1842
1843 if (! (type = mime_type(filename))) {
1844 adios(NULL, "Unable to determine MIME type of \"%s\"", filename);
1845 }
1846
1847 /*
1848 * Parse the Content-Type. get_ctinfo() parses MIME parameters, but
1849 * since we're just feeding it a MIME type we have to add those ourself.
1850 * Map that to a valid content-type label and call any initialization
1851 * function.
1852 */
1853
1854 if (get_ctinfo(type, ct, 0) == NOTOK)
1855 done(1);
1856
1857 free(type);
1858
1859 for (s2i = str2cts; s2i->si_key; s2i++)
1860 if (strcasecmp(ct->c_ctinfo.ci_type, s2i->si_key) == 0)
1861 break;
1862 if (!s2i->si_key && !uprf(ct->c_ctinfo.ci_type, "X-"))
1863 s2i++;
1864
1865 /*
1866 * Make sure the type isn't incompatible with what we can handle
1867 */
1868
1869 switch (ct->c_type = s2i->si_val) {
1870 case CT_MULTIPART:
1871 adios (NULL, "multipart types must be specified by mhbuild directives");
1872 /* NOTREACHED */
1873
1874 case CT_MESSAGE:
1875 if (strcasecmp(ct->c_ctinfo.ci_subtype, "partial") == 0)
1876 adios(NULL, "Sorry, %s/%s isn't supported", ct->c_ctinfo.ci_type,
1877 ct->c_ctinfo.ci_subtype);
1878 if (strcasecmp(ct->c_ctinfo.ci_subtype, "external-body") == 0)
1879 adios(NULL, "external-body messages must be specified "
1880 "by mhbuild directives");
1881 /* Fall through */
1882
1883 default:
1884 /*
1885 * This sets the subtype, if it's significant
1886 */
1887 if ((ct->c_ctinitfnx = s2i->si_init))
1888 (*ct->c_ctinitfnx)(ct);
1889 break;
1890 }
1891
1892 /*
1893 * Feed in a few attributes; specifically, the name attribute, the
1894 * content-description, and the content-disposition.
1895 */
1896
1897 for (pm = ct->c_ctinfo.ci_first_pm; pm; pm = pm->pm_next) {
1898 if (strcasecmp(pm->pm_name, "name") == 0) {
1899 if (pm->pm_value)
1900 free(pm->pm_value);
1901 pm->pm_value = getcpy(simplename);
1902 break;
1903 }
1904 }
1905
1906 if (pm == NULL)
1907 add_param(&ct->c_ctinfo.ci_first_pm, &ct->c_ctinfo.ci_last_pm,
1908 "name", simplename, 0);
1909
1910 ct->c_descr = getcpy(simplename);
1911 ct->c_descr = add("\n", ct->c_descr);
1912 ct->c_cefile.ce_file = getcpy(filename);
1913
1914 /*
1915 * If it's a text/calendar, we need to make sure it's an inline,
1916 * otherwise it won't work with some calendar programs. Otherwise
1917 * assume attachment
1918 */
1919
1920 if (strcasecmp(ct->c_ctinfo.ci_type, "text") == 0 &&
1921 strcasecmp(ct->c_ctinfo.ci_subtype, "calendar") == 0) {
1922 ct->c_dispo_type = getcpy("inline");
1923 } else {
1924 ct->c_dispo_type = getcpy("attachment");
1925 }
1926
1927 add_param(&ct->c_dispo_first, &ct->c_dispo_last, "filename", simplename, 0);
1928 }