mime_attach.c revision 1.2 1 1.1 christos /* $NetBSD: mime_attach.c,v 1.2 2006/10/31 20:07:32 christos Exp $ */
2 1.1 christos
3 1.1 christos /*-
4 1.1 christos * Copyright (c) 2006 The NetBSD Foundation, Inc.
5 1.1 christos * All rights reserved.
6 1.1 christos *
7 1.1 christos * This code is derived from software contributed to The NetBSD Foundation
8 1.1 christos * by Anon Ymous.
9 1.1 christos *
10 1.1 christos * Redistribution and use in source and binary forms, with or without
11 1.1 christos * modification, are permitted provided that the following conditions
12 1.1 christos * are met:
13 1.1 christos * 1. Redistributions of source code must retain the above copyright
14 1.1 christos * notice, this list of conditions and the following disclaimer.
15 1.1 christos * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 christos * notice, this list of conditions and the following disclaimer in the
17 1.1 christos * documentation and/or other materials provided with the distribution.
18 1.1 christos * 3. All advertising materials mentioning features or use of this software
19 1.1 christos * must display the following acknowledgement:
20 1.1 christos * This product includes software developed by the NetBSD
21 1.1 christos * Foundation, Inc. and its contributors.
22 1.1 christos * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.1 christos * contributors may be used to endorse or promote products derived
24 1.1 christos * from this software without specific prior written permission.
25 1.1 christos *
26 1.1 christos * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.1 christos * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.1 christos * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.1 christos * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.1 christos * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.1 christos * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.1 christos * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.1 christos * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.1 christos * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.1 christos * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.1 christos * POSSIBILITY OF SUCH DAMAGE.
37 1.1 christos */
38 1.1 christos
39 1.1 christos #ifdef MIME_SUPPORT
40 1.1 christos
41 1.1 christos #include <sys/cdefs.h>
42 1.1 christos #ifndef __lint__
43 1.1 christos __RCSID("$NetBSD: mime_attach.c,v 1.2 2006/10/31 20:07:32 christos Exp $");
44 1.1 christos #endif /* not __lint__ */
45 1.1 christos
46 1.1 christos #include <assert.h>
47 1.1 christos #include <err.h>
48 1.1 christos #include <fcntl.h>
49 1.1 christos #include <libgen.h>
50 1.1 christos #include <magic.h>
51 1.1 christos #include <setjmp.h>
52 1.1 christos #include <signal.h>
53 1.1 christos #include <stdio.h>
54 1.1 christos #include <stdlib.h>
55 1.1 christos #include <string.h>
56 1.1 christos #include <unistd.h>
57 1.1 christos #include <util.h>
58 1.1 christos
59 1.1 christos #include "def.h"
60 1.1 christos #include "extern.h"
61 1.1 christos #ifdef USE_EDITLINE
62 1.1 christos #include "complete.h"
63 1.1 christos #endif
64 1.1 christos #ifdef MIME_SUPPORT
65 1.1 christos #include "mime.h"
66 1.1 christos #include "mime_codecs.h"
67 1.1 christos #include "mime_child.h"
68 1.1 christos #endif
69 1.1 christos #include "glob.h"
70 1.1 christos
71 1.1 christos
72 1.1 christos #if 0
73 1.1 christos #ifndef __lint__
74 1.1 christos /*
75 1.1 christos * XXX - This block for debugging only and eventually should go away.
76 1.1 christos */
77 1.1 christos static void
78 1.1 christos show_name(const char *prefix, struct name *np)
79 1.1 christos {
80 1.1 christos int i;
81 1.1 christos
82 1.1 christos i = 0;
83 1.1 christos for (/* EMPTY */; np; np = np->n_flink) {
84 1.1 christos (void)printf("%s[%d]: %s\n", prefix, i, np->n_name);
85 1.1 christos i++;
86 1.1 christos }
87 1.1 christos }
88 1.1 christos
89 1.2 christos static void fput_mime_content(FILE *fp, struct Content *Cp);
90 1.2 christos
91 1.1 christos PUBLIC void
92 1.1 christos show_attach(const char *prefix, struct attachment *ap)
93 1.1 christos {
94 1.1 christos int i;
95 1.1 christos i = 1;
96 1.1 christos for (/* EMPTY */; ap; ap = ap->a_flink) {
97 1.1 christos (void)printf("%s[%d]:\n", prefix, i);
98 1.1 christos fput_mime_content(stdout, &ap->a_Content);
99 1.1 christos i++;
100 1.1 christos }
101 1.1 christos }
102 1.1 christos
103 1.1 christos PUBLIC void
104 1.1 christos show_header(struct header *hp)
105 1.1 christos {
106 1.1 christos show_name("TO", hp->h_to);
107 1.1 christos (void)printf("SUBJECT: %s\n", hp->h_subject);
108 1.1 christos show_name("CC", hp->h_cc);
109 1.1 christos show_name("BCC", hp->h_bcc);
110 1.1 christos show_name("SMOPTS", hp->h_smopts);
111 1.1 christos show_attach("ATTACH", hp->h_attach);
112 1.1 christos }
113 1.1 christos #endif /* __lint__ */
114 1.1 christos #endif
115 1.1 christos
116 1.1 christos
117 1.1 christos /***************************
118 1.1 christos * boundary string routines
119 1.1 christos */
120 1.1 christos static char *
121 1.1 christos getrandstring(size_t length)
122 1.1 christos {
123 1.1 christos void *vbin;
124 1.1 christos uint32_t *bin;
125 1.1 christos size_t binlen;
126 1.1 christos size_t i;
127 1.1 christos char *b64;
128 1.1 christos
129 1.1 christos /* XXX - check this stuff again!!! */
130 1.1 christos
131 1.1 christos binlen = 3 * roundup(length, 4) / 4; /* bytes of binary to encode base64 */
132 1.1 christos bin = vbin = salloc(roundup(binlen, 4));
133 1.1 christos for (i = 0; i < roundup(binlen, 4) / 4; i++)
134 1.1 christos bin[i] = arc4random();
135 1.1 christos
136 1.1 christos b64 = salloc(roundup(length, 4));
137 1.1 christos mime_bintob64(b64, vbin, binlen);
138 1.1 christos b64[length] = '\0';
139 1.1 christos
140 1.1 christos return b64;
141 1.1 christos }
142 1.1 christos
143 1.1 christos /*
144 1.1 christos * Generate a boundary for MIME multipart messages.
145 1.1 christos */
146 1.1 christos static char *
147 1.1 christos make_boundary(void)
148 1.1 christos {
149 1.1 christos #define BOUND_LEN 70 /* maximum length is 70 characters: RFC2046 sec 5.1.1 */
150 1.1 christos
151 1.1 christos char *bound;
152 1.1 christos time_t now;
153 1.1 christos
154 1.1 christos (void)time(&now);
155 1.1 christos bound = salloc(BOUND_LEN);
156 1.1 christos (void)snprintf(bound, BOUND_LEN, "=_%08lx.%s",
157 1.1 christos (long)now, getrandstring(BOUND_LEN - 12));
158 1.1 christos return bound;
159 1.1 christos
160 1.1 christos #undef BOUND_LEN
161 1.1 christos }
162 1.1 christos
163 1.1 christos
164 1.1 christos /***************************
165 1.1 christos * Transfer coding routines
166 1.1 christos */
167 1.1 christos /*
168 1.1 christos * We determine the recommended transfer encoding type for a file as
169 1.1 christos * follows:
170 1.1 christos *
171 1.1 christos * 1) If there is a NULL byte or a stray CR (not in a CRLF
172 1.1 christos * combination) in the file, play it safe and use base64.
173 1.1 christos *
174 1.1 christos * 2) If any high bit is set, use quoted-printable if the content type
175 1.1 christos * is "text" and base64 otherwise.
176 1.1 christos *
177 1.1 christos * 3) Otherwise:
178 1.1 christos * a) use quoted-printable if there are any long lines, control
179 1.1 christos * chars (including CR), end-of-line blank space, or a missing
180 1.1 christos * terminating NL.
181 1.1 christos * b) use 7bit in all remaining case, including an empty file.
182 1.1 christos *
183 1.1 christos * NOTE: This means that CRLF text (MSDOS) files will be encoded
184 1.1 christos * quoted-printable.
185 1.1 christos */
186 1.1 christos /*
187 1.1 christos * RFC 821 imposes the following line length limit:
188 1.1 christos * The maximum total length of a text line including the
189 1.1 christos * <CRLF> is 1000 characters (but not counting the leading
190 1.1 christos * dot duplicated for transparency).
191 1.1 christos */
192 1.1 christos #define MIME_UNENCODED_LINE_MAX (1000 - 2)
193 1.1 christos static size_t
194 1.1 christos line_limit(void)
195 1.1 christos {
196 1.1 christos int limit;
197 1.1 christos const char *cp;
198 1.1 christos limit = -1;
199 1.1 christos
200 1.1 christos if ((cp = value(ENAME_MIME_UNENC_LINE_MAX)) != NULL)
201 1.1 christos limit = atoi(cp);
202 1.1 christos
203 1.1 christos if (limit < 0 || limit > MIME_UNENCODED_LINE_MAX)
204 1.1 christos limit = MIME_UNENCODED_LINE_MAX;
205 1.1 christos
206 1.1 christos return (size_t)limit;
207 1.1 christos }
208 1.1 christos
209 1.1 christos static inline
210 1.1 christos int is_text(const char *ctype)
211 1.1 christos {
212 1.1 christos return ctype &&
213 1.1 christos strncasecmp(ctype, "text/", sizeof("text/") - 1) == 0;
214 1.1 christos }
215 1.1 christos
216 1.1 christos static const char *
217 1.1 christos content_encoding_core(void *fh, const char *ctype)
218 1.1 christos {
219 1.1 christos int c, lastc;
220 1.1 christos int ctrlchar, endwhite;
221 1.1 christos size_t curlen, maxlen;
222 1.1 christos
223 1.1 christos curlen = 0;
224 1.1 christos maxlen = 0;
225 1.1 christos ctrlchar = 0;
226 1.1 christos endwhite = 0;
227 1.1 christos lastc = EOF;
228 1.1 christos while ((c = fgetc(fh)) != EOF) {
229 1.1 christos curlen++;
230 1.1 christos
231 1.1 christos if (c == '\0' || (lastc == '\r' && c != '\n'))
232 1.1 christos return MIME_TRANSFER_BASE64;
233 1.1 christos
234 1.1 christos if (c > 0x7f) {
235 1.1 christos if (is_text(ctype))
236 1.1 christos return MIME_TRANSFER_QUOTED;
237 1.1 christos else
238 1.1 christos return MIME_TRANSFER_BASE64;
239 1.1 christos }
240 1.1 christos if (c == '\n') {
241 1.1 christos if (isblank((unsigned char)lastc))
242 1.1 christos endwhite = 1;
243 1.1 christos if (curlen > maxlen)
244 1.1 christos maxlen = curlen;
245 1.1 christos curlen = 0;
246 1.1 christos }
247 1.1 christos else if ((c < 0x20 && c != '\t') || c == 0x7f)
248 1.1 christos ctrlchar = 1;
249 1.1 christos
250 1.1 christos lastc = c;
251 1.1 christos }
252 1.1 christos if (lastc == EOF) /* no characters read */
253 1.1 christos return MIME_TRANSFER_7BIT;
254 1.1 christos
255 1.1 christos if (lastc != '\n' || ctrlchar || endwhite || maxlen > line_limit())
256 1.1 christos return MIME_TRANSFER_QUOTED;
257 1.1 christos
258 1.1 christos return MIME_TRANSFER_7BIT;
259 1.1 christos }
260 1.1 christos
261 1.1 christos static const char *
262 1.1 christos content_encoding_by_name(const char *filename, const char *ctype)
263 1.1 christos {
264 1.1 christos FILE *fp;
265 1.1 christos const char *enc;
266 1.1 christos fp = fopen(filename, "r");
267 1.1 christos if (fp == NULL) {
268 1.1 christos warn("content_encoding_by_name: %s", filename);
269 1.1 christos return MIME_TRANSFER_BASE64; /* safe */
270 1.1 christos }
271 1.1 christos enc = content_encoding_core(fp, ctype);
272 1.1 christos (void)fclose(fp);
273 1.1 christos return enc;
274 1.1 christos }
275 1.1 christos
276 1.1 christos static const char *
277 1.1 christos content_encoding_by_fileno(int fd, const char *ctype)
278 1.1 christos {
279 1.1 christos FILE *fp;
280 1.1 christos const char *encoding;
281 1.1 christos off_t cur_pos;
282 1.1 christos
283 1.1 christos cur_pos = lseek(fd, (off_t)0, SEEK_CUR);
284 1.1 christos fp = fdopen(fd, "r");
285 1.1 christos if (fp == NULL) {
286 1.1 christos warn("content_encoding_by_fileno");
287 1.1 christos return MIME_TRANSFER_BASE64;
288 1.1 christos }
289 1.1 christos encoding = content_encoding_core(fp, ctype);
290 1.1 christos (void)lseek(fd, cur_pos, SEEK_SET);
291 1.1 christos return encoding;
292 1.1 christos }
293 1.1 christos
294 1.1 christos static const char *
295 1.1 christos content_encoding(struct attachment *attach, const char *ctype)
296 1.1 christos {
297 1.1 christos switch (attach->a_type) {
298 1.1 christos case ATTACH_FNAME:
299 1.1 christos return content_encoding_by_name(attach->a_name, ctype);
300 1.1 christos case ATTACH_MSG:
301 1.1 christos errx(EXIT_FAILURE, "msgno not supported yet\n");
302 1.1 christos /* NOTREACHED */
303 1.1 christos case ATTACH_FILENO:
304 1.1 christos return content_encoding_by_fileno(attach->a_fileno, ctype);
305 1.1 christos default:
306 1.1 christos errx(EXIT_FAILURE, "invalid attach type: %d\n", attach->a_type);
307 1.1 christos }
308 1.1 christos /* NOTREACHED */
309 1.1 christos }
310 1.1 christos
311 1.1 christos
312 1.1 christos
313 1.1 christos /************************
314 1.1 christos * Content type routines
315 1.1 christos */
316 1.1 christos /*
317 1.1 christos * We use libmagic(3) to get the content type, except in the case of a
318 1.1 christos * 0 or 1 byte file, which we declare "text/plain".
319 1.1 christos */
320 1.1 christos static const char *
321 1.1 christos content_type_by_name(const char *filename)
322 1.1 christos {
323 1.1 christos const char *cp;
324 1.1 christos magic_t magic;
325 1.1 christos struct stat sb;
326 1.1 christos
327 1.1 christos /*
328 1.1 christos * libmagic produces annoying results on very short files.
329 1.1 christos * Note: a 1-byte file always consists of a newline, so size
330 1.1 christos * determines all here.
331 1.1 christos */
332 1.1 christos if ((filename != NULL && stat(filename, &sb) == 0) ||
333 1.1 christos (filename == NULL && fstat(0, &sb) == 0))
334 1.1 christos if (sb.st_size < 2 && S_ISREG(sb.st_mode))
335 1.1 christos return "text/plain";
336 1.1 christos
337 1.1 christos magic = magic_open(MAGIC_MIME);
338 1.1 christos if (magic == NULL) {
339 1.1 christos warn("magic_open: %s", magic_error(magic));
340 1.1 christos return NULL;
341 1.1 christos }
342 1.1 christos if (magic_load(magic, NULL) != 0) {
343 1.1 christos warn("magic_load: %s", magic_error(magic));
344 1.1 christos return NULL;
345 1.1 christos }
346 1.1 christos cp = magic_file(magic, filename);
347 1.1 christos if (cp == NULL) {
348 1.1 christos warn("magic_load: %s", magic_error(magic));
349 1.1 christos return NULL;
350 1.1 christos }
351 1.1 christos cp = savestr(cp);
352 1.1 christos magic_close(magic);
353 1.1 christos return cp;
354 1.1 christos }
355 1.1 christos
356 1.1 christos static const char *
357 1.1 christos content_type_by_fileno(int fd)
358 1.1 christos {
359 1.1 christos const char *cp;
360 1.1 christos off_t cur_pos;
361 1.1 christos int ofd;
362 1.1 christos
363 1.1 christos cur_pos = lseek(fd, (off_t)0, SEEK_CUR);
364 1.1 christos
365 1.1 christos ofd = dup(0); /* save stdin */
366 1.1 christos if (dup2(fd, 0) == -1) /* become stdin */
367 1.1 christos warn("dup2");
368 1.1 christos
369 1.1 christos cp = content_type_by_name(NULL);
370 1.1 christos
371 1.1 christos if (dup2(ofd, 0) == -1) /* restore stdin */
372 1.1 christos warn("dup2");
373 1.1 christos (void)close(ofd); /* close the copy */
374 1.1 christos
375 1.1 christos (void)lseek(fd, cur_pos, SEEK_SET);
376 1.1 christos return cp;
377 1.1 christos }
378 1.1 christos
379 1.1 christos
380 1.1 christos static const char *
381 1.1 christos content_type(struct attachment *attach)
382 1.1 christos {
383 1.1 christos switch (attach->a_type) {
384 1.1 christos case ATTACH_FNAME:
385 1.1 christos return content_type_by_name(attach->a_name);
386 1.1 christos case ATTACH_MSG:
387 1.1 christos return "message/rfc822";
388 1.1 christos case ATTACH_FILENO:
389 1.1 christos return content_type_by_fileno(attach->a_fileno);
390 1.1 christos default:
391 1.1 christos /* This is a coding error! */
392 1.1 christos assert(/* CONSTCOND */ 0);
393 1.1 christos return NULL;
394 1.1 christos }
395 1.1 christos }
396 1.1 christos
397 1.1 christos
398 1.1 christos /*************************
399 1.1 christos * Other content routines
400 1.1 christos */
401 1.1 christos
402 1.1 christos static const char *
403 1.1 christos content_disposition(struct attachment *ap)
404 1.1 christos {
405 1.1 christos switch (ap->a_type) {
406 1.1 christos case ATTACH_FNAME: {
407 1.1 christos char *buf;
408 1.1 christos char *disp;
409 1.1 christos (void)easprintf(&buf, "attachment; filename=\"%s\"", basename(ap->a_name));
410 1.1 christos disp = savestr(buf);
411 1.1 christos free(buf);
412 1.1 christos return disp;
413 1.1 christos }
414 1.1 christos case ATTACH_MSG:
415 1.1 christos case ATTACH_FILENO:
416 1.1 christos return "inline";
417 1.1 christos
418 1.1 christos default:
419 1.1 christos return NULL;
420 1.1 christos }
421 1.1 christos }
422 1.1 christos
423 1.1 christos /*ARGSUSED*/
424 1.1 christos static const char *
425 1.1 christos content_id(struct attachment *attach __unused)
426 1.1 christos {
427 1.1 christos /* XXX - to be written. */
428 1.1 christos
429 1.1 christos return NULL;
430 1.1 christos }
431 1.1 christos
432 1.1 christos static const char *
433 1.1 christos content_description(struct attachment *attach, int attach_num)
434 1.1 christos {
435 1.1 christos if (attach_num) {
436 1.1 christos char *description;
437 1.1 christos char *cp;
438 1.1 christos (void)easprintf(&cp, "attachment %d", attach_num);
439 1.1 christos description = savestr(cp);
440 1.1 christos free(cp);
441 1.1 christos return description;
442 1.1 christos }
443 1.1 christos else
444 1.1 christos return attach->a_Content.C_description;
445 1.1 christos }
446 1.1 christos
447 1.1 christos /*******************************************
448 1.1 christos * Routines to get the MIME content strings.
449 1.1 christos */
450 1.1 christos static struct Content
451 1.1 christos get_mime_content(struct attachment *ap, int i)
452 1.1 christos {
453 1.1 christos struct Content Cp;
454 1.1 christos
455 1.1 christos Cp.C_type = content_type(ap);
456 1.1 christos Cp.C_encoding = content_encoding(ap, Cp.C_type);
457 1.1 christos Cp.C_disposition = content_disposition(ap);
458 1.1 christos Cp.C_id = content_id(ap);
459 1.1 christos Cp.C_description = content_description(ap, i);
460 1.1 christos
461 1.1 christos return Cp;
462 1.1 christos }
463 1.1 christos
464 1.2 christos #if 0 /* XXX - dead code! */
465 1.1 christos /*
466 1.1 christos * A hook to complete the content part of the attachment struct. This
467 1.1 christos * is used when a file is attached by the '-a' to complete the process
468 1.1 christos * after the flags have been processed.
469 1.1 christos */
470 1.1 christos PUBLIC void
471 1.1 christos mime_attach_content(struct attachment *ap)
472 1.1 christos {
473 1.1 christos int i;
474 1.1 christos i = 1;
475 1.1 christos
476 1.1 christos for (/* EMPTY */; ap; ap = ap->a_flink)
477 1.1 christos ap->a_Content = get_mime_content(ap, i++);
478 1.1 christos }
479 1.2 christos #endif
480 1.1 christos
481 1.1 christos
482 1.1 christos /******************
483 1.1 christos * Output routines
484 1.1 christos */
485 1.1 christos static void
486 1.1 christos fput_mime_content(FILE *fp, struct Content *Cp)
487 1.1 christos {
488 1.1 christos (void)fprintf(fp, MIME_HDR_TYPE ": %s\n", Cp->C_type);
489 1.1 christos (void)fprintf(fp, MIME_HDR_ENCODING ": %s\n", Cp->C_encoding);
490 1.1 christos if (Cp->C_disposition)
491 1.1 christos (void)fprintf(fp, MIME_HDR_DISPOSITION ": %s\n",
492 1.1 christos Cp->C_disposition);
493 1.1 christos if (Cp->C_id)
494 1.1 christos (void)fprintf(fp, MIME_HDR_ID ": %s\n", Cp->C_id);
495 1.1 christos if (Cp->C_description)
496 1.1 christos (void)fprintf(fp, MIME_HDR_DESCRIPTION ": %s\n",
497 1.1 christos Cp->C_description);
498 1.1 christos }
499 1.1 christos
500 1.1 christos static void
501 1.1 christos fput_body(FILE *fi, FILE *fo, struct Content *Cp)
502 1.1 christos {
503 1.1 christos mime_codec_t enc;
504 1.1 christos
505 1.1 christos enc = mime_fio_encoder(Cp->C_encoding);
506 1.1 christos if (enc == NULL)
507 1.1 christos warnx("unknown transfer encoding type: %s\n", Cp->C_encoding);
508 1.1 christos else
509 1.1 christos enc(fi, fo, 0);
510 1.1 christos }
511 1.1 christos
512 1.1 christos
513 1.1 christos static void
514 1.1 christos fput_attachment(FILE *fo, struct attachment *ap)
515 1.1 christos {
516 1.1 christos FILE *fi;
517 1.1 christos struct Content *Cp = &ap->a_Content;
518 1.1 christos
519 1.1 christos fput_mime_content(fo, &ap->a_Content);
520 1.1 christos (void)putc('\n', fo);
521 1.1 christos
522 1.1 christos switch (ap->a_type) {
523 1.1 christos case ATTACH_FNAME:
524 1.1 christos fi = fopen(ap->a_name, "r");
525 1.1 christos if (fi == NULL)
526 1.1 christos err(EXIT_FAILURE, "fopen: %s", ap->a_name);
527 1.1 christos break;
528 1.1 christos
529 1.1 christos case ATTACH_FILENO:
530 1.1 christos fi = fdopen(ap->a_fileno, "r");
531 1.1 christos if (fi == NULL)
532 1.1 christos err(EXIT_FAILURE, "fdopen: %d", ap->a_fileno);
533 1.1 christos break;
534 1.1 christos
535 1.1 christos case ATTACH_MSG:
536 1.1 christos default:
537 1.1 christos errx(EXIT_FAILURE, "unsupported attachment type");
538 1.1 christos }
539 1.1 christos
540 1.1 christos fput_body(fi, fo, Cp);
541 1.1 christos
542 1.1 christos if (ap->a_type == ATTACH_FNAME)
543 1.1 christos (void)fclose(fi);
544 1.1 christos }
545 1.1 christos
546 1.1 christos /***********************************
547 1.1 christos * Higher level attachment routines.
548 1.1 christos */
549 1.1 christos
550 1.1 christos static int
551 1.1 christos mktemp_file(FILE **nfo, FILE **nfi, const char *hint)
552 1.1 christos {
553 1.1 christos char tempname[PATHSIZE];
554 1.1 christos int fd, fd2;
555 1.1 christos (void)snprintf(tempname, sizeof(tempname), "%s/%sXXXXXXXXXX",
556 1.1 christos tmpdir, hint);
557 1.1 christos if ((fd = mkstemp(tempname)) == -1 ||
558 1.1 christos (*nfo = Fdopen(fd, "w")) == NULL) {
559 1.1 christos if (fd != -1)
560 1.1 christos (void)close(fd);
561 1.1 christos warn("%s", tempname);
562 1.1 christos return -1;
563 1.1 christos }
564 1.1 christos (void)rm(tempname);
565 1.1 christos if ((fd2 = dup(fd)) == -1 ||
566 1.1 christos (*nfi = Fdopen(fd2, "r")) == NULL) {
567 1.1 christos warn("%s", tempname);
568 1.1 christos (void)Fclose(*nfo);
569 1.1 christos return -1;
570 1.1 christos }
571 1.1 christos return 0;
572 1.1 christos }
573 1.1 christos
574 1.1 christos
575 1.1 christos /*
576 1.1 christos * Repackage the mail as a multipart MIME message. This should always
577 1.1 christos * be called whenever there are attachments, but might be called even
578 1.1 christos * if there are none if we want to wrap the message in a MIME package.
579 1.1 christos */
580 1.1 christos PUBLIC FILE *
581 1.1 christos mime_encode(FILE *fi, struct header *header)
582 1.1 christos {
583 1.1 christos struct attachment map; /* fake structure for the message body */
584 1.1 christos struct attachment *attach;
585 1.1 christos struct attachment *ap;
586 1.1 christos FILE *nfi, *nfo;
587 1.1 christos
588 1.1 christos attach = header->h_attach;
589 1.1 christos
590 1.1 christos /*
591 1.1 christos * Make new phantom temporary file with read and write file
592 1.1 christos * handles: nfi and nfo, resp.
593 1.1 christos */
594 1.1 christos if (mktemp_file(&nfo, &nfi, "mail.Rs") != 0)
595 1.1 christos return fi;
596 1.1 christos
597 1.1 christos (void)memset(&map, 0, sizeof(map));
598 1.1 christos map.a_type = ATTACH_FILENO;
599 1.1 christos map.a_fileno = fileno(fi);
600 1.1 christos
601 1.1 christos map.a_Content = get_mime_content(&map, 0);
602 1.1 christos
603 1.1 christos if (attach) {
604 1.1 christos /* Multi-part message:
605 1.1 christos * Make an attachment structure for the body message
606 1.1 christos * and make that the first element in the attach list.
607 1.1 christos */
608 1.1 christos if (fsize(fi)) {
609 1.1 christos map.a_flink = attach;
610 1.1 christos attach->a_blink = ↦
611 1.1 christos attach = ↦
612 1.1 christos }
613 1.1 christos
614 1.1 christos /* Construct our MIME boundary string - used by mime_putheader() */
615 1.1 christos header->h_mime_boundary = make_boundary();
616 1.1 christos
617 1.1 christos (void)fprintf(nfo, "This is a multi-part message in MIME format.\n");
618 1.1 christos
619 1.1 christos for (ap = attach; ap; ap = ap->a_flink) {
620 1.1 christos (void)fprintf(nfo, "\n--%s\n", header->h_mime_boundary);
621 1.1 christos fput_attachment(nfo, ap);
622 1.1 christos }
623 1.1 christos
624 1.1 christos /* the final boundary with two attached dashes */
625 1.1 christos (void)fprintf(nfo, "\n--%s--\n", header->h_mime_boundary);
626 1.1 christos }
627 1.1 christos else {
628 1.1 christos /* Single-part message (no attachments):
629 1.1 christos * Update header->h_Content (used by mime_putheader()).
630 1.1 christos * Output the body contents.
631 1.1 christos */
632 1.1 christos char *encoding;
633 1.1 christos
634 1.1 christos header->h_Content = map.a_Content;
635 1.1 christos
636 1.1 christos /* check for an encoding override */
637 1.1 christos if ((encoding = value(ENAME_MIME_ENCODE_MSG)) && *encoding)
638 1.1 christos header->h_Content.C_encoding = encoding;
639 1.1 christos
640 1.1 christos fput_body(fi, nfo, &header->h_Content);
641 1.1 christos }
642 1.1 christos (void)Fclose(fi);
643 1.1 christos (void)Fclose(nfo);
644 1.1 christos rewind(nfi);
645 1.1 christos return(nfi);
646 1.1 christos }
647 1.1 christos
648 1.1 christos static char*
649 1.1 christos check_filename(char *filename, char *canon_name)
650 1.1 christos {
651 1.1 christos int fd;
652 1.1 christos struct stat sb;
653 1.1 christos char *fname = filename;
654 1.1 christos /*
655 1.1 christos * 1) check that filename is really a file.
656 1.1 christos * 2) check that filename is readable.
657 1.1 christos * 3) allocate an attachment structure.
658 1.1 christos * 4) save cananonical name for filename, so cd won't screw things later.
659 1.1 christos * 5) add the structure to the end of the chain.
660 1.1 christos */
661 1.2 christos /* We need to expand '~' if we got here from '~@'. The shell
662 1.2 christos * does this otherwise.
663 1.2 christos */
664 1.1 christos if (fname[0] == '~' && fname[1] == '/') {
665 1.2 christos if (homedir && homedir[0] != '~')
666 1.2 christos (void)easprintf(&fname, "%s/%s",
667 1.2 christos homedir, fname + 2);
668 1.1 christos }
669 1.1 christos if (realpath(fname, canon_name) == NULL) {
670 1.1 christos warn("realpath: %s", filename);
671 1.1 christos canon_name = NULL;
672 1.1 christos goto done;
673 1.1 christos }
674 1.1 christos fd = open(canon_name, O_RDONLY, 0);
675 1.1 christos if (fd == -1) {
676 1.1 christos warnx("open: cannot read %s", filename);
677 1.1 christos canon_name = NULL;
678 1.1 christos goto done;
679 1.1 christos }
680 1.1 christos if (fstat(fd, &sb) == -1) {
681 1.1 christos warn("stat: %s", canon_name);
682 1.1 christos canon_name = NULL;
683 1.1 christos goto do_close;
684 1.1 christos }
685 1.1 christos if (!S_ISREG(sb.st_mode)) {
686 1.1 christos warnx("stat: %s is not a file", filename);
687 1.1 christos canon_name = NULL;
688 1.2 christos /* goto do_close; */
689 1.1 christos }
690 1.1 christos do_close:
691 1.1 christos (void)close(fd);
692 1.1 christos done:
693 1.1 christos if (fname != filename)
694 1.1 christos free(fname);
695 1.1 christos
696 1.1 christos return canon_name;
697 1.1 christos }
698 1.1 christos
699 1.1 christos static struct attachment *
700 1.1 christos attach_one_file(struct attachment *attach, char *filename, int attach_num)
701 1.1 christos {
702 1.1 christos char canon_name[MAXPATHLEN];
703 1.1 christos struct attachment *ap, *nap;
704 1.1 christos
705 1.1 christos if (check_filename(filename, canon_name) == NULL)
706 1.1 christos return NULL;
707 1.1 christos
708 1.1 christos nap = csalloc(1, sizeof(*nap));
709 1.1 christos nap->a_type = ATTACH_FNAME;
710 1.1 christos nap->a_name = savestr(canon_name);
711 1.1 christos
712 1.1 christos if (attach == NULL)
713 1.1 christos attach = nap;
714 1.1 christos else {
715 1.1 christos for (ap = attach; ap->a_flink != NULL; ap = ap->a_flink)
716 1.1 christos continue;
717 1.1 christos ap->a_flink = nap;
718 1.1 christos nap->a_blink = ap;
719 1.1 christos }
720 1.1 christos
721 1.1 christos if (attach_num)
722 1.1 christos nap->a_Content = get_mime_content(nap, attach_num);
723 1.1 christos
724 1.1 christos return attach;
725 1.1 christos }
726 1.1 christos
727 1.1 christos
728 1.1 christos static jmp_buf intjmp;
729 1.1 christos /*ARGSUSED*/
730 1.1 christos static void
731 1.1 christos sigint(int signum __unused)
732 1.1 christos {
733 1.1 christos siglongjmp(intjmp, 1);
734 1.1 christos }
735 1.1 christos
736 1.1 christos static char *
737 1.1 christos get_line(el_mode_t *em, const char *pr, const char *str, int i)
738 1.1 christos {
739 1.1 christos sig_t saveint;
740 1.1 christos char *cp;
741 1.1 christos char *line;
742 1.1 christos char *prompt;
743 1.1 christos
744 1.1 christos saveint = signal(SIGINT, sigint);
745 1.1 christos if (sigsetjmp(intjmp, 1)) {
746 1.1 christos (void)signal(SIGINT, saveint);
747 1.1 christos (void)putc('\n', stdout);
748 1.1 christos return __UNCONST("");
749 1.1 christos }
750 1.1 christos
751 1.1 christos /* Don't use a '\t' in the format string here as completion
752 1.1 christos * seems to handle it badly. */
753 1.1 christos (void)easprintf(&prompt, "#%-8d%s: ", i, pr);
754 1.1 christos line = my_gets(em, prompt, __UNCONST(str));
755 1.1 christos /* LINTED */
756 1.1 christos line = line ? savestr(line) : __UNCONST("");
757 1.1 christos free(prompt);
758 1.1 christos
759 1.1 christos (void)signal(SIGINT, saveint);
760 1.1 christos
761 1.1 christos /* strip trailing white space */
762 1.1 christos for (cp = line + strlen(line) - 1;
763 1.1 christos cp >= line && isblank((unsigned char)*cp); cp--)
764 1.1 christos *cp = '\0';
765 1.1 christos
766 1.1 christos /* skip leading white space */
767 1.1 christos cp = skip_white(line);
768 1.1 christos
769 1.1 christos return cp;
770 1.1 christos }
771 1.1 christos
772 1.1 christos static void
773 1.1 christos sget_line(el_mode_t *em, const char *pr, const char **str, int i)
774 1.1 christos {
775 1.1 christos char *line;
776 1.1 christos line = get_line(em, pr, *str, i);
777 1.1 christos if (strcmp(line, *str) != 0)
778 1.1 christos *str = savestr(line);
779 1.1 christos }
780 1.1 christos
781 1.1 christos static void
782 1.1 christos sget_encoding(const char **str, const char *filename, const char *ctype, int num)
783 1.1 christos {
784 1.1 christos const char *ename;
785 1.1 christos const char *defename;
786 1.1 christos
787 1.1 christos defename = NULL;
788 1.1 christos ename = *str;
789 1.1 christos for (;;) {
790 1.1 christos ename = get_line(&elm.mime_enc, "encoding", ename, num);
791 1.1 christos
792 1.1 christos if (*ename == '\0') {
793 1.1 christos if (defename == NULL)
794 1.1 christos defename = content_encoding_by_name(filename, ctype);
795 1.1 christos ename = defename;
796 1.1 christos }
797 1.1 christos else if (mime_fio_encoder(ename) == NULL) {
798 1.1 christos const void *cookie;
799 1.1 christos (void)printf("Sorry: valid encoding modes are: ");
800 1.1 christos cookie = NULL;
801 1.1 christos ename = mime_next_encoding_name(&cookie);
802 1.1 christos for (;;) {
803 1.1 christos (void)printf("%s", ename);
804 1.1 christos ename = mime_next_encoding_name(&cookie);
805 1.1 christos if (ename == NULL)
806 1.1 christos break;
807 1.1 christos (void)fputc(',', stdout);
808 1.1 christos }
809 1.1 christos ename = *str;
810 1.1 christos }
811 1.1 christos else {
812 1.1 christos if (strcmp(ename, *str) != 0)
813 1.1 christos *str = savestr(ename);
814 1.1 christos break;
815 1.1 christos }
816 1.1 christos }
817 1.1 christos }
818 1.1 christos
819 1.1 christos static struct attachment *
820 1.1 christos edit_attachments(struct attachment *attach)
821 1.1 christos {
822 1.1 christos char canon_name[MAXPATHLEN];
823 1.1 christos struct attachment *ap;
824 1.1 christos char *line;
825 1.2 christos int attach_num;
826 1.1 christos
827 1.1 christos (void)printf("Attachments:\n");
828 1.1 christos
829 1.2 christos attach_num = 1;
830 1.1 christos ap = attach;
831 1.1 christos while (ap) {
832 1.2 christos line = get_line(&elm.filec, "filename", ap->a_name, attach_num);
833 1.1 christos if (*line == '\0') { /* omit this attachment */
834 1.1 christos if (ap->a_blink)
835 1.1 christos ap->a_blink->a_flink = ap->a_flink;
836 1.1 christos else
837 1.1 christos attach = ap->a_flink;
838 1.1 christos }
839 1.1 christos else {
840 1.1 christos if (strcmp(line, ap->a_name) != 0) { /* new filename */
841 1.1 christos if (check_filename(line, canon_name) == NULL)
842 1.1 christos continue;
843 1.1 christos ap->a_name = savestr(canon_name);
844 1.1 christos ap->a_Content = get_mime_content(ap, 0);
845 1.1 christos }
846 1.2 christos sget_line(&elm.string, "description",
847 1.2 christos &ap->a_Content.C_description, attach_num);
848 1.2 christos sget_encoding(&ap->a_Content.C_encoding, ap->a_name,
849 1.2 christos ap->a_Content.C_type, attach_num);
850 1.1 christos }
851 1.2 christos attach_num++;
852 1.1 christos if (ap->a_flink == NULL)
853 1.1 christos break;
854 1.1 christos
855 1.1 christos ap = ap->a_flink;
856 1.1 christos }
857 1.1 christos
858 1.1 christos do {
859 1.1 christos struct attachment *nap;
860 1.1 christos
861 1.2 christos line = get_line(&elm.filec, "filename", "", attach_num);
862 1.1 christos if (*line == '\0')
863 1.1 christos break;
864 1.1 christos
865 1.2 christos nap = attach_one_file(ap, line, attach_num);
866 1.1 christos if (nap == NULL)
867 1.1 christos continue;
868 1.1 christos
869 1.1 christos if (ap)
870 1.1 christos ap = ap->a_flink;
871 1.1 christos else
872 1.1 christos ap = attach = nap;
873 1.1 christos
874 1.2 christos sget_line(&elm.string, "description",
875 1.2 christos &ap->a_Content.C_description, attach_num);
876 1.2 christos sget_encoding(&ap->a_Content.C_encoding, ap->a_name,
877 1.2 christos ap->a_Content.C_type, attach_num);
878 1.2 christos attach_num++;
879 1.1 christos
880 1.1 christos } while (ap);
881 1.1 christos
882 1.1 christos return attach;
883 1.1 christos }
884 1.1 christos
885 1.1 christos /*
886 1.2 christos * Hook used by the '~@' escape to attach files.
887 1.1 christos */
888 1.1 christos PUBLIC struct attachment*
889 1.2 christos mime_attach_files(struct attachment *attach, char *linebuf)
890 1.1 christos {
891 1.1 christos struct attachment *ap;
892 1.1 christos char *argv[MAXARGC];
893 1.1 christos int argc;
894 1.1 christos int attach_num;
895 1.1 christos
896 1.2 christos argc = getrawlist(linebuf, argv, sizeofarray(argv));
897 1.2 christos attach_num = 1;
898 1.1 christos for (ap = attach; ap && ap->a_flink; ap = ap->a_flink)
899 1.1 christos attach_num++;
900 1.1 christos
901 1.1 christos if (argc) {
902 1.1 christos int i;
903 1.1 christos for (i = 0; i < argc; i++) {
904 1.2 christos struct attachment *ap2;
905 1.2 christos ap2 = attach_one_file(ap, argv[i], attach_num);
906 1.2 christos if (ap2 != NULL) {
907 1.2 christos ap = ap2;
908 1.2 christos if (attach == NULL)
909 1.2 christos attach = ap;
910 1.2 christos attach_num++;
911 1.2 christos }
912 1.1 christos }
913 1.1 christos }
914 1.1 christos else {
915 1.1 christos attach = edit_attachments(attach);
916 1.1 christos (void)printf("--- end attachments ---\n");
917 1.1 christos }
918 1.1 christos
919 1.1 christos return attach;
920 1.1 christos }
921 1.1 christos
922 1.1 christos /*
923 1.2 christos * Hook called in main() to attach files registered by the '-a' flag.
924 1.2 christos */
925 1.2 christos PUBLIC struct attachment *
926 1.2 christos mime_attach_optargs(struct name *optargs)
927 1.2 christos {
928 1.2 christos struct attachment *attach;
929 1.2 christos struct attachment *ap;
930 1.2 christos struct name *np;
931 1.2 christos char *expand_optargs;
932 1.2 christos int attach_num;
933 1.2 christos
934 1.2 christos expand_optargs = value(ENAME_MIME_ATTACH_LIST);
935 1.2 christos attach_num = 1;
936 1.2 christos ap = NULL;
937 1.2 christos attach = NULL;
938 1.2 christos for (np = optargs; np; np = np->n_flink) {
939 1.2 christos char *argv[MAXARGC];
940 1.2 christos int argc;
941 1.2 christos int i;
942 1.2 christos
943 1.2 christos if (expand_optargs != NULL)
944 1.2 christos argc = getrawlist(np->n_name, argv, sizeofarray(argv));
945 1.2 christos else {
946 1.2 christos if (np->n_name == '\0')
947 1.2 christos argc = 0;
948 1.2 christos else {
949 1.2 christos argc = 1;
950 1.2 christos argv[0] = np->n_name;
951 1.2 christos }
952 1.2 christos argv[argc] = NULL;/* be consistent with getrawlist() */
953 1.2 christos }
954 1.2 christos for (i = 0; i < argc; i++) {
955 1.2 christos struct attachment *ap2;
956 1.2 christos char *filename;
957 1.2 christos
958 1.2 christos if (argv[i][0] == '/') /* an absolute path */
959 1.2 christos (void)easprintf(&filename, "%s", argv[i]);
960 1.2 christos else
961 1.2 christos (void)easprintf(&filename, "%s/%s",
962 1.2 christos origdir, argv[i]);
963 1.2 christos
964 1.2 christos ap2 = attach_one_file(ap, filename, attach_num);
965 1.2 christos free(filename);
966 1.2 christos if (ap2 != NULL) {
967 1.2 christos ap = ap2;
968 1.2 christos if (attach == NULL)
969 1.2 christos attach = ap;
970 1.2 christos attach_num++;
971 1.2 christos }
972 1.2 christos }
973 1.2 christos }
974 1.2 christos return attach;
975 1.2 christos }
976 1.2 christos
977 1.2 christos /*
978 1.1 christos * Output MIME header strings as specified in the header structure.
979 1.1 christos */
980 1.1 christos PUBLIC void
981 1.1 christos mime_putheader(FILE *fp, struct header *header)
982 1.1 christos {
983 1.1 christos (void)fprintf(fp, MIME_HDR_VERSION ": " MIME_VERSION "\n");
984 1.1 christos if (header->h_attach) {
985 1.1 christos (void)fprintf(fp, MIME_HDR_TYPE ": multipart/mixed;\n");
986 1.1 christos (void)fprintf(fp, "\tboundary=\"%s\"\n", header->h_mime_boundary);
987 1.1 christos }
988 1.1 christos else {
989 1.1 christos fput_mime_content(fp, &header->h_Content);
990 1.1 christos }
991 1.1 christos }
992 1.1 christos
993 1.1 christos #endif /* MIME_SUPPORT */
994