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