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