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