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