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