1 1.19 rillig /* $NetBSD: mime_decode.c,v 1.19 2025/07/09 16:59:54 rillig 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 * 19 1.1 christos * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 1.1 christos * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 1.1 christos * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 1.1 christos * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 1.1 christos * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 1.1 christos * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 1.1 christos * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 1.1 christos * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 1.1 christos * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 1.1 christos * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 1.1 christos * POSSIBILITY OF SUCH DAMAGE. 30 1.1 christos */ 31 1.1 christos 32 1.1 christos 33 1.1 christos #ifdef MIME_SUPPORT 34 1.1 christos 35 1.1 christos #include <sys/cdefs.h> 36 1.1 christos #ifndef __lint__ 37 1.19 rillig __RCSID("$NetBSD: mime_decode.c,v 1.19 2025/07/09 16:59:54 rillig Exp $"); 38 1.1 christos #endif /* not __lint__ */ 39 1.1 christos 40 1.1 christos #include <assert.h> 41 1.1 christos #include <err.h> 42 1.1 christos #include <fcntl.h> 43 1.1 christos #include <libgen.h> 44 1.1 christos #include <signal.h> 45 1.1 christos #include <stdio.h> 46 1.1 christos #include <stdlib.h> 47 1.1 christos #include <string.h> 48 1.1 christos #include <unistd.h> 49 1.1 christos #include <iconv.h> 50 1.1 christos 51 1.1 christos #include "def.h" 52 1.1 christos #include "extern.h" 53 1.1 christos #ifdef USE_EDITLINE 54 1.1 christos #include "complete.h" 55 1.1 christos #endif 56 1.1 christos #ifdef MIME_SUPPORT 57 1.1 christos #include "mime.h" 58 1.1 christos #include "mime_child.h" 59 1.1 christos #include "mime_codecs.h" 60 1.1 christos #include "mime_header.h" 61 1.4 christos #include "mime_detach.h" 62 1.1 christos #endif 63 1.1 christos #include "glob.h" 64 1.4 christos #include "thread.h" 65 1.1 christos 66 1.1 christos #if 0 67 1.1 christos #ifndef __lint__ 68 1.1 christos /* 69 1.1 christos * XXX - This block for debugging only and eventually should go away. 70 1.1 christos */ 71 1.1 christos static void 72 1.1 christos show_one_mime_info(FILE *fp, struct mime_info *mip) 73 1.1 christos { 74 1.1 christos #define XX(a) (a) ? (a) : "<null>" 75 1.1 christos 76 1.1 christos (void)fprintf(fp, ">> --------\n"); 77 1.1 christos (void)fprintf(fp, "mip %d:\n", mip->mi_partnum); 78 1.1 christos (void)fprintf(fp, "** Version: %s\n", XX(mip->mi_version)); 79 1.1 christos (void)fprintf(fp, "** type: %s\n", XX(mip->mi_type)); 80 1.1 christos (void)fprintf(fp, "** subtype: %s\n", XX(mip->mi_subtype)); 81 1.4 christos (void)fprintf(fp, "** boundary: %s\n", XX(mip->mi_boundary)); 82 1.1 christos (void)fprintf(fp, "** charset: %s\n", XX(mip->mi_charset)); 83 1.1 christos (void)fprintf(fp, "** encoding: %s\n", XX(mip->mi_encoding)); 84 1.4 christos (void)fprintf(fp, "** disposition: %s\n", XX(mip->mi_disposition)); 85 1.4 christos (void)fprintf(fp, "** filename: %s\n", XX(mip->mi_filename)); 86 1.1 christos (void)fprintf(fp, "** %p: flag: 0x%x, block: %ld, offset: %d, size: %lld, lines: %ld:%ld\n", 87 1.1 christos mip->mp, 88 1.1 christos mip->mp->m_flag, 89 1.1 christos mip->mp->m_block, mip->mp->m_offset, mip->mp->m_size, 90 1.1 christos mip->mp->m_lines, mip->mp->m_blines); 91 1.1 christos (void)fprintf(fp, "** mip: %p\n", mip); 92 1.1 christos (void)fprintf(fp, "** mi_flink: %p\n", mip->mi_flink); 93 1.1 christos (void)fprintf(fp, "** mi_blink: %p\n", mip->mi_blink); 94 1.1 christos (void)fprintf(fp, "** mip %p, mp %p, parent_mip %p, parent_mp %p\n", 95 1.1 christos mip, mip->mp, mip->mi_parent.mip, mip->mi_parent.mp); 96 1.1 christos 97 1.1 christos (void)fprintf(fp, "** mi_fo %p, mi_head_end %p, mi_pipe_end %p\n", 98 1.1 christos mip->mi_fo, mip->mi_head_end, mip->mi_pipe_end); 99 1.1 christos 100 1.4 christos (void)fprintf(fp, "** mi_ignore_body: %d\n", mip->mi_ignore_body); 101 1.1 christos (void)fprintf(fp, "** mi_partnum: %d\n", mip->mi_partnum); 102 1.4 christos (void)fprintf(fp, "** mi_partstr: %s\n", mip->mi_partstr); 103 1.8 christos (void)fprintf(fp, "** mi_msgstr: %s\n", mip->mi_msgstr); 104 1.1 christos 105 1.1 christos (void)fflush(fp); 106 1.10 christos 107 1.1 christos #undef XX 108 1.1 christos } 109 1.1 christos 110 1.1 christos __unused 111 1.1 christos static void 112 1.1 christos show_mime_info(FILE *fp, struct mime_info *mip, struct mime_info *end_mip) 113 1.1 christos { 114 1.18 msaitoh for (/* EMPTY */; mip != end_mip; mip = mip->mi_flink) 115 1.1 christos show_one_mime_info(fp, mip); 116 1.10 christos 117 1.1 christos (void)fprintf(fp, "++ =========\n"); 118 1.1 christos (void)fflush(fp); 119 1.1 christos } 120 1.1 christos #endif /* __lint__ */ 121 1.1 christos #endif /* #if */ 122 1.1 christos 123 1.1 christos 124 1.1 christos /* 125 1.1 christos * Our interface to the file registry in popen.c 126 1.1 christos */ 127 1.4 christos PUBLIC FILE * 128 1.1 christos pipe_end(struct mime_info *mip) 129 1.1 christos { 130 1.1 christos FILE *fp; 131 1.7 christos fp = last_registered_file(0); /* get last registered file or pipe */ 132 1.1 christos if (fp == NULL) 133 1.1 christos fp = mip->mi_fo; 134 1.1 christos return fp; 135 1.1 christos } 136 1.1 christos 137 1.1 christos /* 138 1.1 christos * Copy the first ';' delimited substring from 'src' (null terminated) 139 1.1 christos * into 'dst', expanding quotes and removing comments (as per RFC 140 1.1 christos * 822). Returns a pointer in src to the next non-white character 141 1.1 christos * following ';'. The caller is responsible for ensuring 'dst' is 142 1.1 christos * sufficiently large to hold the result. 143 1.1 christos */ 144 1.1 christos static char * 145 1.1 christos get_param(char *dst, char *src) 146 1.1 christos { 147 1.1 christos char *lastq; 148 1.1 christos char *cp; 149 1.1 christos char *cp2; 150 1.1 christos int nesting; 151 1.1 christos 152 1.1 christos cp2 = dst; 153 1.1 christos lastq = dst; 154 1.1 christos for (cp = src; *cp && *cp != ';'; cp++) { 155 1.1 christos switch (*cp) { 156 1.1 christos case '"': /* start of quoted string */ 157 1.1 christos for (cp++; *cp; cp++) { 158 1.1 christos if (*cp == '"') 159 1.1 christos break; 160 1.1 christos if (*cp == '\\' && cp[1] != '\0') 161 1.1 christos ++cp; 162 1.1 christos *cp2++ = *cp; 163 1.1 christos } 164 1.1 christos lastq = cp2-1; 165 1.1 christos break; 166 1.1 christos case '(': /* start of comment */ 167 1.1 christos nesting = 1; 168 1.1 christos while (nesting > 0 && *++cp) { 169 1.1 christos if (*cp == '\\' && cp[1] != '\0') 170 1.1 christos cp++; 171 1.1 christos if (*cp == '(') 172 1.1 christos nesting++; 173 1.1 christos if (*cp == ')') 174 1.1 christos nesting--; 175 1.1 christos } 176 1.1 christos break; 177 1.1 christos default: 178 1.1 christos *cp2++ = *cp; 179 1.1 christos break; 180 1.1 christos } 181 1.1 christos } 182 1.1 christos /* remove trailing white space */ 183 1.10 christos while (cp2 > lastq && is_WSP(cp2[-1])) 184 1.1 christos cp2--; 185 1.1 christos *cp2 = '\0'; 186 1.1 christos if (*cp == ';') 187 1.1 christos cp++; 188 1.10 christos cp = skip_WSP(cp); 189 1.1 christos return cp; 190 1.1 christos } 191 1.1 christos 192 1.1 christos /* 193 1.1 christos * Content parameter 194 1.1 christos * if field is NULL, return the content "specifier". 195 1.1 christos */ 196 1.1 christos static char* 197 1.1 christos cparam(const char field[], char *src, int downcase) 198 1.1 christos { 199 1.1 christos char *cp; 200 1.1 christos char *dst; 201 1.1 christos 202 1.1 christos if (src == NULL) 203 1.1 christos return NULL; 204 1.1 christos 205 1.1 christos dst = salloc(strlen(src) + 1); /* large enough for any param in src */ 206 1.10 christos cp = skip_WSP(src); 207 1.1 christos cp = get_param(dst, cp); 208 1.10 christos 209 1.1 christos if (field == NULL) 210 1.1 christos return dst; 211 1.10 christos 212 1.1 christos while (*cp != '\0') { 213 1.1 christos size_t len = strlen(field); 214 1.1 christos cp = get_param(dst, cp); 215 1.1 christos if (strncasecmp(dst, field, len) == 0 && dst[len] == '=') { 216 1.1 christos char *cp2; 217 1.1 christos cp2 = dst + len + 1; 218 1.1 christos if (downcase) 219 1.1 christos istrcpy(cp2, cp2); 220 1.1 christos return cp2; 221 1.1 christos } 222 1.1 christos } 223 1.1 christos return NULL; 224 1.1 christos } 225 1.1 christos 226 1.1 christos 227 1.1 christos static void 228 1.1 christos get_content(struct mime_info *mip) 229 1.1 christos { 230 1.4 christos char *mime_disposition_field; 231 1.1 christos char *mime_type_field; 232 1.4 christos char *filename; 233 1.1 christos struct message *mp; 234 1.19 rillig char *cp, *mime_type; 235 1.1 christos 236 1.1 christos mp = mip->mp; 237 1.4 christos mip->mi_version = cparam(NULL, hfield(MIME_HDR_VERSION, mp), 0); 238 1.1 christos mip->mi_encoding = cparam(NULL, hfield(MIME_HDR_ENCODING, mp), 1); 239 1.1 christos 240 1.1 christos mime_type_field = hfield(MIME_HDR_TYPE, mp); 241 1.19 rillig mip->mi_type = mime_type = cparam(NULL, mime_type_field, 1); 242 1.1 christos if (mip->mi_type) { 243 1.19 rillig cp = strchr(mime_type, '/'); 244 1.1 christos if (cp) 245 1.1 christos *cp++ = '\0'; 246 1.1 christos mip->mi_subtype = cp; 247 1.1 christos } 248 1.1 christos mip->mi_boundary = cparam("boundary", mime_type_field, 0); 249 1.4 christos mip->mi_charset = cparam("charset", mime_type_field, 1); 250 1.4 christos 251 1.4 christos mime_disposition_field = hfield(MIME_HDR_DISPOSITION, mp); 252 1.4 christos mip->mi_disposition = cparam(NULL, mime_disposition_field, 1); 253 1.4 christos /* 254 1.4 christos * The type field typically has a "name" parameter for "image" 255 1.4 christos * and "video" types, and I assume for other types as well. 256 1.4 christos * We grab it, but override it if the disposition field has a 257 1.4 christos * filename parameter as it often does for "attachments". 258 1.4 christos * More careful analysis could be done, but this seems to work 259 1.4 christos * pretty well. 260 1.4 christos */ 261 1.4 christos filename = cparam("name", mime_type_field, 0); 262 1.4 christos if ((cp = cparam("filename", mime_disposition_field, 0)) != NULL) 263 1.4 christos filename = cp; 264 1.4 christos if (filename) { 265 1.4 christos filename = basename(filename); /* avoid absolute pathnames */ 266 1.4 christos filename = savestr(filename); /* save it! */ 267 1.4 christos } 268 1.4 christos mip->mi_filename = filename; 269 1.16 christos 270 1.16 christos /* 271 1.16 christos * XXX: If we have a "Content-Type" in the header, then assume 272 1.16 christos * we also have a "MIME-Version: 1.0". This fixes some broken 273 1.16 christos * MIME headers that I have seen occasionally. 274 1.16 christos */ 275 1.16 christos if (mip->mi_version == NULL && mip->mi_type != NULL) 276 1.16 christos mip->mi_version = MIME_VERSION; 277 1.1 christos } 278 1.1 christos 279 1.1 christos static struct message * 280 1.1 christos salloc_message(int flag, long block, short offset) 281 1.1 christos { 282 1.1 christos struct message *mp; 283 1.1 christos /* use csalloc in case someone adds a field someday! */ 284 1.1 christos mp = csalloc(1, sizeof(*mp)); 285 1.1 christos mp->m_flag = flag; 286 1.1 christos mp->m_block = block; 287 1.1 christos mp->m_offset = offset; 288 1.1 christos #if 0 289 1.1 christos mp->m_lines = 0; 290 1.1 christos mp->m_size = 0; 291 1.1 christos mp->m_blines = 0; 292 1.1 christos #endif 293 1.1 christos return mp; 294 1.1 christos } 295 1.1 christos 296 1.1 christos static struct mime_info * 297 1.4 christos insert_new_mip(struct mime_info *this_mip, struct mime_info *top_mip, 298 1.4 christos struct message *top_mp, off_t end_pos, int partnum) 299 1.1 christos { 300 1.1 christos struct mime_info *new_mip; 301 1.4 christos 302 1.1 christos new_mip = csalloc(1, sizeof(*new_mip)); 303 1.1 christos new_mip->mi_blink = this_mip; 304 1.1 christos new_mip->mi_flink = this_mip->mi_flink; 305 1.1 christos this_mip->mi_flink = new_mip; 306 1.4 christos 307 1.4 christos new_mip->mp = salloc_message(this_mip->mp->m_flag, 308 1.9 dogcow (long)blockof(end_pos), blkoffsetof(end_pos)); 309 1.4 christos 310 1.4 christos new_mip->mi_parent.mip = top_mip; 311 1.4 christos new_mip->mi_parent.mp = top_mp; 312 1.4 christos new_mip->mi_partnum = partnum; 313 1.4 christos 314 1.1 christos return new_mip; 315 1.1 christos } 316 1.1 christos 317 1.1 christos static void 318 1.1 christos split_multipart(struct mime_info *top_mip) 319 1.1 christos { 320 1.1 christos FILE *fp; 321 1.1 christos struct message *top_mp; 322 1.1 christos struct message *this_mp; 323 1.1 christos struct mime_info *this_mip; 324 1.1 christos off_t beg_pos; 325 1.1 christos const char *boundary; 326 1.1 christos size_t boundary_len; 327 1.1 christos long lines_left; /* must be signed and same size as m_lines */ 328 1.1 christos int partnum; 329 1.1 christos int in_header; 330 1.1 christos 331 1.1 christos top_mp = top_mip->mp; 332 1.1 christos this_mp = salloc_message(top_mp->m_flag, top_mp->m_block, top_mp->m_offset); 333 1.1 christos this_mip = top_mip; 334 1.1 christos this_mip->mp = this_mp; 335 1.1 christos 336 1.1 christos partnum = 1; 337 1.1 christos /* top_mip->mi_partnum = partnum++; */ /* Keep the number set by the caller */ 338 1.1 christos in_header = 1; 339 1.1 christos boundary = top_mip->mi_boundary; 340 1.1 christos boundary_len = boundary ? strlen(boundary) : 0; 341 1.1 christos 342 1.1 christos fp = setinput(top_mp); 343 1.1 christos beg_pos = ftello(fp); 344 1.4 christos #if 0 345 1.4 christos warnx("beg_pos: %lld, m_lines: %ld, m_blines: %ld", 346 1.4 christos beg_pos, top_mp->m_lines, top_mp->m_blines); 347 1.4 christos #endif 348 1.1 christos for (lines_left = top_mp->m_lines - 1; lines_left >= 0; lines_left--) { 349 1.1 christos char *line; 350 1.1 christos size_t line_len; 351 1.1 christos 352 1.1 christos line = fgetln(fp, &line_len); 353 1.1 christos 354 1.1 christos this_mp->m_lines++; /* count the message lines */ 355 1.1 christos 356 1.1 christos if (!in_header) 357 1.1 christos this_mp->m_blines++; /* count the body lines */ 358 1.1 christos 359 1.1 christos if (lines_left == 0 || ( 360 1.1 christos !in_header && 361 1.1 christos line_len >= boundary_len + 2 && 362 1.1 christos line[0] == '-' && line[1] == '-' && 363 1.1 christos strncmp(line + 2, boundary, boundary_len) == 0)) { 364 1.1 christos off_t cur_pos; 365 1.1 christos off_t end_pos; 366 1.1 christos 367 1.1 christos cur_pos = ftello(fp); 368 1.1 christos 369 1.1 christos /* the boundary belongs to the next part */ 370 1.1 christos end_pos = cur_pos - line_len; 371 1.1 christos this_mp->m_lines -= 1; 372 1.1 christos this_mp->m_blines -= 1; 373 1.1 christos 374 1.1 christos this_mp->m_size = end_pos - beg_pos; 375 1.4 christos #if 0 376 1.4 christos warnx("end_pos: %lld, m_lines: %ld, m_blines: %ld", 377 1.4 christos end_pos, this_mp->m_lines, this_mp->m_blines); 378 1.4 christos #endif 379 1.1 christos if (line[boundary_len + 2] == '-' && 380 1.1 christos line[boundary_len + 3] == '-') {/* end of multipart */ 381 1.1 christos /* do a sanity check on the EOM */ 382 1.4 christos if (lines_left != 1) { 383 1.1 christos /* 384 1.1 christos * XXX - this can happen! 385 1.1 christos * Should we display the 386 1.1 christos * trailing garbage or check 387 1.1 christos * that it is blank or just 388 1.1 christos * ignore it? 389 1.1 christos */ 390 1.4 christos #if 0 391 1.4 christos (void)printf("EOM: lines left: %ld\n", lines_left); 392 1.4 christos #endif 393 1.1 christos } 394 1.1 christos break; /* XXX - stop at this point or grab the rest? */ 395 1.1 christos } 396 1.4 christos this_mip = insert_new_mip(this_mip, top_mip, top_mp, end_pos, partnum++); 397 1.4 christos this_mp = this_mip->mp; 398 1.4 christos this_mp->m_lines = 1; /* already read the first line in the header! */ 399 1.1 christos beg_pos = end_pos; 400 1.1 christos in_header = 1; 401 1.1 christos } 402 1.1 christos 403 1.1 christos if (line_len == 1) 404 1.1 christos in_header = 0; 405 1.1 christos } 406 1.1 christos } 407 1.1 christos 408 1.1 christos static void 409 1.1 christos split_message(struct mime_info *top_mip) 410 1.1 christos { 411 1.1 christos struct mime_info *this_mip; 412 1.1 christos struct message *top_mp; 413 1.1 christos struct message *this_mp; 414 1.1 christos FILE *fp; 415 1.1 christos off_t beg_pos; 416 1.1 christos long lines_left; /* must be same size as m_lines */ 417 1.1 christos int in_header; 418 1.1 christos 419 1.1 christos top_mp = top_mip->mp; 420 1.1 christos this_mp = salloc_message(top_mp->m_flag, top_mp->m_block, top_mp->m_offset); 421 1.1 christos this_mip = top_mip; 422 1.1 christos this_mip->mp = this_mp; 423 1.1 christos 424 1.1 christos in_header = 1; 425 1.1 christos 426 1.1 christos fp = setinput(top_mp); 427 1.1 christos beg_pos = ftello(fp); 428 1.1 christos 429 1.1 christos for (lines_left = top_mp->m_lines; lines_left > 0; lines_left--) { 430 1.1 christos size_t line_len; 431 1.1 christos 432 1.1 christos (void)fgetln(fp, &line_len); 433 1.1 christos 434 1.1 christos this_mp->m_lines++; /* count the message lines */ 435 1.1 christos if (!in_header) 436 1.1 christos this_mp->m_blines++; /* count the body lines */ 437 1.1 christos 438 1.1 christos if (in_header && line_len == 1) { /* end of header */ 439 1.1 christos off_t end_pos; 440 1.1 christos end_pos = ftello(fp); 441 1.1 christos this_mp->m_size = end_pos - beg_pos; 442 1.4 christos this_mip = insert_new_mip(this_mip, top_mip,top_mp, end_pos, 0); 443 1.4 christos this_mp = this_mip->mp; 444 1.4 christos this_mp->m_lines = 1; /* we already counted one line in the header! */ 445 1.1 christos beg_pos = end_pos; 446 1.1 christos in_header = 0; /* never in header again */ 447 1.1 christos } 448 1.1 christos } 449 1.10 christos 450 1.1 christos /* close the last message */ 451 1.1 christos this_mp->m_size = ftello(fp) - beg_pos; 452 1.1 christos } 453 1.1 christos 454 1.1 christos 455 1.1 christos static const char * 456 1.1 christos get_command_hook(struct mime_info *mip, const char *domain) 457 1.1 christos { 458 1.1 christos char *key; 459 1.1 christos char *cmd; 460 1.1 christos 461 1.1 christos if (mip->mi_type == NULL) 462 1.1 christos return NULL; 463 1.1 christos 464 1.1 christos /* XXX - should we use easprintf() here? We are probably 465 1.1 christos * hosed elsewhere if this fails anyway. */ 466 1.1 christos 467 1.1 christos cmd = NULL; 468 1.1 christos if (mip->mi_subtype) { 469 1.1 christos if (asprintf(&key, "mime%s-%s-%s", 470 1.1 christos domain, mip->mi_type, mip->mi_subtype) == -1) { 471 1.1 christos warn("get_command_hook: subtupe: asprintf"); 472 1.1 christos return NULL; 473 1.1 christos } 474 1.1 christos cmd = value(key); 475 1.1 christos free(key); 476 1.1 christos } 477 1.1 christos if (cmd == NULL) { 478 1.1 christos if (asprintf(&key, "mime%s-%s", domain, mip->mi_type) == -1) { 479 1.1 christos warn("get_command_hook: type: asprintf"); 480 1.1 christos return NULL; 481 1.1 christos } 482 1.1 christos cmd = value(key); 483 1.1 christos free(key); 484 1.1 christos } 485 1.1 christos return cmd; 486 1.1 christos } 487 1.1 christos 488 1.1 christos 489 1.1 christos static int 490 1.1 christos is_basic_alternative(struct mime_info *mip) 491 1.1 christos { 492 1.1 christos return 493 1.1 christos strcasecmp(mip->mi_type, "text") == 0 && 494 1.1 christos strcasecmp(mip->mi_subtype, "plain") == 0; 495 1.1 christos } 496 1.1 christos 497 1.1 christos static struct mime_info * 498 1.1 christos select_alternative(struct mime_info *top_mip, struct mime_info *end_mip) 499 1.1 christos { 500 1.1 christos struct mime_info *the_mip; /* the chosen alternate */ 501 1.1 christos struct mime_info *this_mip; 502 1.1 christos /* 503 1.1 christos * The alternates are supposed to occur in order of 504 1.1 christos * increasing "complexity". So: if there is at least 505 1.1 christos * one alternate of type "text/plain", use the last 506 1.1 christos * one, otherwise default to the first alternate. 507 1.1 christos */ 508 1.1 christos the_mip = top_mip->mi_flink; 509 1.1 christos for (this_mip = top_mip->mi_flink; 510 1.1 christos this_mip != end_mip; 511 1.1 christos this_mip = this_mip->mi_flink) { 512 1.1 christos const char *cmd; 513 1.1 christos 514 1.1 christos if (this_mip->mi_type == NULL || 515 1.1 christos this_mip->mi_subtype == NULL) 516 1.1 christos continue; 517 1.1 christos 518 1.1 christos if (is_basic_alternative(this_mip)) 519 1.1 christos the_mip = this_mip; 520 1.1 christos else if ( 521 1.1 christos (cmd = get_command_hook(this_mip, "-hook")) || 522 1.1 christos (cmd = get_command_hook(this_mip, "-head")) || 523 1.1 christos (cmd = get_command_hook(this_mip, "-body"))) { 524 1.1 christos int flags; 525 1.1 christos /* just get the flags. */ 526 1.1 christos flags = mime_run_command(cmd, NULL); 527 1.1 christos if ((flags & CMD_FLAG_ALTERNATIVE) != 0) 528 1.1 christos the_mip = this_mip; 529 1.1 christos } 530 1.1 christos } 531 1.1 christos return the_mip; 532 1.1 christos } 533 1.1 christos 534 1.1 christos 535 1.1 christos static inline int 536 1.1 christos is_multipart(struct mime_info *mip) 537 1.1 christos { 538 1.1 christos return mip->mi_type && 539 1.1 christos strcasecmp("multipart", mip->mi_type) == 0; 540 1.1 christos } 541 1.1 christos static inline int 542 1.1 christos is_message(struct mime_info *mip) 543 1.1 christos { 544 1.1 christos return mip->mi_type && 545 1.1 christos strcasecmp("message", mip->mi_type) == 0; 546 1.1 christos } 547 1.1 christos 548 1.1 christos static inline int 549 1.1 christos is_alternative(struct mime_info *mip) 550 1.1 christos { 551 1.1 christos return mip->mi_subtype && 552 1.1 christos strcasecmp("alternative", mip->mi_subtype) == 0; 553 1.1 christos } 554 1.1 christos 555 1.1 christos 556 1.1 christos /* 557 1.1 christos * Take a mime_info pointer and expand it recursively into all its 558 1.1 christos * mime parts. Only "multipart" and "message" types recursed into; 559 1.1 christos * they are handled separately. 560 1.1 christos */ 561 1.1 christos static struct mime_info * 562 1.1 christos expand_mip(struct mime_info *top_mip) 563 1.1 christos { 564 1.1 christos struct mime_info *this_mip; 565 1.1 christos struct mime_info *next_mip; 566 1.1 christos 567 1.4 christos if (top_mip->mi_partnum == 0) { 568 1.4 christos if (top_mip->mi_blink) 569 1.4 christos top_mip->mi_partstr = top_mip->mi_blink->mi_partstr; 570 1.4 christos } 571 1.4 christos else if (top_mip->mi_parent.mip) { 572 1.4 christos const char *prefix; 573 1.4 christos char *cp; 574 1.4 christos prefix = top_mip->mi_parent.mip->mi_partstr; 575 1.4 christos (void)sasprintf(&cp, "%s%s%d", prefix, 576 1.4 christos *prefix ? "." : "", top_mip->mi_partnum); 577 1.4 christos top_mip->mi_partstr = cp; 578 1.4 christos } 579 1.4 christos 580 1.1 christos next_mip = top_mip->mi_flink; 581 1.1 christos 582 1.1 christos if (is_multipart(top_mip)) { 583 1.4 christos top_mip->mi_ignore_body = 1; /* the first body is ignored */ 584 1.1 christos split_multipart(top_mip); 585 1.1 christos 586 1.1 christos for (this_mip = top_mip->mi_flink; 587 1.1 christos this_mip != next_mip; 588 1.1 christos this_mip = this_mip->mi_flink) { 589 1.1 christos get_content(this_mip); 590 1.1 christos } 591 1.1 christos if (is_alternative(top_mip)) { 592 1.1 christos this_mip = select_alternative(top_mip, next_mip); 593 1.1 christos this_mip->mi_partnum = 0; /* suppress partnum display */ 594 1.1 christos this_mip->mi_flink = next_mip; 595 1.1 christos this_mip->mi_blink = top_mip; 596 1.1 christos top_mip->mi_flink = this_mip; 597 1.1 christos } 598 1.1 christos /* 599 1.1 christos * Recurse into each part. 600 1.1 christos */ 601 1.1 christos for (this_mip = top_mip->mi_flink; 602 1.1 christos this_mip != next_mip; 603 1.1 christos this_mip = expand_mip(this_mip)) 604 1.1 christos continue; 605 1.1 christos } 606 1.1 christos else if (is_message(top_mip)) { 607 1.4 christos top_mip->mi_ignore_body = 1; /* the first body is ignored */ 608 1.1 christos split_message(top_mip); 609 1.1 christos 610 1.1 christos this_mip = top_mip->mi_flink; 611 1.1 christos if (this_mip) { 612 1.1 christos get_content(this_mip); 613 1.1 christos /* 614 1.1 christos * If the one part is MIME encoded, recurse into it. 615 1.1 christos * XXX - Should this be conditional on subtype "rcs822"? 616 1.1 christos */ 617 1.1 christos if (this_mip->mi_type && 618 1.1 christos this_mip->mi_version && 619 1.1 christos equal(this_mip->mi_version, MIME_VERSION)) { 620 1.1 christos this_mip->mi_partnum = 0; 621 1.1 christos (void)expand_mip(this_mip); 622 1.1 christos } 623 1.1 christos } 624 1.1 christos } 625 1.1 christos return next_mip; 626 1.1 christos } 627 1.1 christos 628 1.1 christos 629 1.4 christos #if 0 630 1.1 christos static int 631 1.1 christos show_partnum(FILE *fp, struct mime_info *mip) 632 1.1 christos { 633 1.1 christos int need_dot; 634 1.1 christos need_dot = 0; 635 1.1 christos if (mip->mi_parent.mip && mip->mi_parent.mip->mi_parent.mip) 636 1.1 christos need_dot = show_partnum(fp, mip->mi_parent.mip); 637 1.1 christos 638 1.1 christos if (mip->mi_partnum) { 639 1.1 christos (void)fprintf(fp, "%s%d", need_dot ? "." : "", mip->mi_partnum); 640 1.1 christos need_dot = 1; 641 1.1 christos } 642 1.1 christos return need_dot; 643 1.1 christos } 644 1.4 christos #endif 645 1.1 christos 646 1.1 christos 647 1.1 christos PUBLIC struct mime_info * 648 1.1 christos mime_decode_open(struct message *mp) 649 1.1 christos { 650 1.1 christos struct mime_info *mip; 651 1.4 christos struct mime_info *p; 652 1.1 christos 653 1.1 christos mip = csalloc(1, sizeof(*mip)); 654 1.1 christos mip->mp = salloc(sizeof(*mip->mp)); 655 1.4 christos *mip->mp = *mp; /* copy this so we don't trash the master mp */ 656 1.1 christos 657 1.1 christos get_content(mip); 658 1.1 christos 659 1.1 christos /* RFC 2049 - sec 2 item 1 */ 660 1.1 christos if (mip->mi_version == NULL || 661 1.1 christos !equal(mip->mi_version, MIME_VERSION)) 662 1.1 christos return NULL; 663 1.1 christos 664 1.4 christos mip->mi_partstr = ""; 665 1.1 christos if (mip->mi_type) 666 1.1 christos (void)expand_mip(mip); 667 1.1 christos 668 1.4 christos /* 669 1.4 christos * Get the pipe_end and propagate it down the chain. 670 1.4 christos */ 671 1.4 christos mip->mi_pipe_end = last_registered_file(0); /* for mime_decode_close() */ 672 1.4 christos for (p = mip->mi_flink; p; p = p->mi_flink) 673 1.4 christos p->mi_pipe_end = mip->mi_pipe_end; 674 1.4 christos 675 1.1 christos /* show_mime_info(stderr, mip, NULL); */ 676 1.1 christos 677 1.1 christos return mip; 678 1.1 christos } 679 1.1 christos 680 1.1 christos 681 1.1 christos PUBLIC void 682 1.1 christos mime_decode_close(struct mime_info *mip) 683 1.1 christos { 684 1.1 christos if (mip) 685 1.1 christos close_top_files(mip->mi_pipe_end); 686 1.1 christos } 687 1.1 christos 688 1.1 christos 689 1.1 christos struct prefix_line_args_s { 690 1.1 christos const char *prefix; 691 1.1 christos size_t prefixlen; 692 1.1 christos }; 693 1.1 christos 694 1.1 christos static void 695 1.1 christos prefix_line(FILE *fi, FILE *fo, void *cookie) 696 1.1 christos { 697 1.1 christos struct prefix_line_args_s *args; 698 1.1 christos const char *line; 699 1.1 christos const char *prefix; 700 1.1 christos size_t prefixlen; 701 1.1 christos size_t length; 702 1.1 christos 703 1.1 christos args = cookie; 704 1.1 christos prefix = args->prefix; 705 1.1 christos prefixlen = args->prefixlen; 706 1.1 christos 707 1.1 christos while ((line = fgetln(fi, &length)) != NULL) { 708 1.1 christos if (length > 1) 709 1.1 christos (void)fputs(prefix, fo); 710 1.1 christos else 711 1.11 christos (void)fwrite(prefix, sizeof(*prefix), 712 1.1 christos prefixlen, fo); 713 1.1 christos (void)fwrite(line, sizeof(*line), length, fo); 714 1.1 christos } 715 1.1 christos (void)fflush(fo); 716 1.1 christos } 717 1.1 christos 718 1.1 christos PUBLIC int 719 1.4 christos mime_sendmessage(struct message *mp, FILE *obuf, struct ignoretab *igntab, 720 1.1 christos const char *prefix, struct mime_info *mip) 721 1.1 christos { 722 1.1 christos int error; 723 1.4 christos int detachall_flag; 724 1.4 christos const char *detachdir; 725 1.1 christos FILE *end_of_prefix; 726 1.1 christos 727 1.1 christos if (mip == NULL) 728 1.4 christos return obuf ? /* were we trying to detach? */ 729 1.4 christos sendmessage(mp, obuf, igntab, prefix, NULL) : 0; 730 1.4 christos /* 731 1.4 christos * The prefix has two meanigs which we handle here: 732 1.7 christos * 1) If obuf == NULL, then we are detaching to the 'prefix' directory. 733 1.4 christos * 2) If obuf != NULL, then the prefix is prepended to each line. 734 1.4 christos */ 735 1.4 christos detachdir = NULL; 736 1.4 christos detachall_flag = igntab == detachall; 737 1.4 christos if (obuf == NULL) { 738 1.4 christos assert(prefix != NULL); /* coding error! */ 739 1.6 christos if ((obuf = last_registered_file(0)) == NULL) 740 1.6 christos obuf = stdout; 741 1.4 christos detachdir = prefix; 742 1.4 christos prefix = NULL; 743 1.4 christos igntab = ignoreall; /* always ignore the headers */ 744 1.4 christos } 745 1.1 christos /* 746 1.4 christos * Set this early so pipe_end() will work! 747 1.1 christos */ 748 1.1 christos mip->mi_fo = obuf; 749 1.1 christos 750 1.3 christos (void)fflush(obuf); /* Be safe and flush! XXX - necessary? */ 751 1.3 christos 752 1.1 christos /* 753 1.1 christos * Handle the prefix as a pipe stage so it doesn't get seen by 754 1.1 christos * any decoding or hooks. 755 1.1 christos */ 756 1.1 christos if (prefix != NULL) { 757 1.1 christos static struct prefix_line_args_s prefix_line_args; 758 1.1 christos const char *dp, *dp2 = NULL; 759 1.1 christos for (dp = prefix; *dp; dp++) 760 1.10 christos if (!is_WSP(*dp)) 761 1.1 christos dp2 = dp; 762 1.1 christos prefix_line_args.prefixlen = dp2 == 0 ? 0 : dp2 - prefix + 1; 763 1.1 christos prefix_line_args.prefix = prefix; 764 1.1 christos mime_run_function(prefix_line, pipe_end(mip), (void*)&prefix_line_args); 765 1.1 christos } 766 1.1 christos 767 1.1 christos end_of_prefix = last_registered_file(0); 768 1.1 christos error = 0; 769 1.10 christos for (/*EMPTY*/; mip; mip = mip->mi_flink) { 770 1.1 christos mip->mi_fo = obuf; 771 1.6 christos mip->mi_head_end = obuf; 772 1.4 christos mip->mi_detachdir = detachdir; 773 1.4 christos mip->mi_detachall = detachall_flag; 774 1.4 christos error |= sendmessage(mip->mp, pipe_end(mip), igntab, NULL, mip); 775 1.1 christos close_top_files(end_of_prefix); /* don't close the prefixer! */ 776 1.1 christos } 777 1.1 christos return error; 778 1.1 christos } 779 1.1 christos 780 1.1 christos 781 1.1 christos #ifdef CHARSET_SUPPORT 782 1.1 christos /********************************************** 783 1.1 christos * higher level interface to run mime_ficonv(). 784 1.1 christos */ 785 1.1 christos static void 786 1.1 christos run_mime_ficonv(struct mime_info *mip, const char *charset) 787 1.1 christos { 788 1.1 christos FILE *fo; 789 1.1 christos iconv_t cd; 790 1.1 christos 791 1.1 christos fo = pipe_end(mip); 792 1.1 christos 793 1.1 christos if (charset == NULL || 794 1.1 christos mip->mi_charset == NULL || 795 1.1 christos strcasecmp(mip->mi_charset, charset) == 0 || 796 1.1 christos strcasecmp(mip->mi_charset, "unknown") == 0) 797 1.1 christos return; 798 1.1 christos 799 1.1 christos cd = iconv_open(charset, mip->mi_charset); 800 1.1 christos if (cd == (iconv_t)-1) { 801 1.1 christos (void)fprintf(fo, "\t [ iconv_open failed: %s ]\n\n", 802 1.1 christos strerror(errno)); 803 1.1 christos (void)fflush(fo); /* flush here or see double! */ 804 1.1 christos return; 805 1.1 christos } 806 1.10 christos 807 1.4 christos if (mip->mi_detachdir == NULL && /* don't contaminate the detach! */ 808 1.4 christos value(ENAME_MIME_CHARSET_VERBOSE)) 809 1.4 christos (void)fprintf(fo, "\t[ converting %s -> %s ]\n\n", 810 1.4 christos mip->mi_charset, charset); 811 1.1 christos 812 1.1 christos mime_run_function(mime_ficonv, fo, cd); 813 1.10 christos 814 1.5 christos (void)iconv_close(cd); 815 1.1 christos } 816 1.1 christos #endif /* CHARSET_SUPPORT */ 817 1.1 christos 818 1.1 christos 819 1.4 christos PUBLIC void 820 1.1 christos run_decoder(struct mime_info *mip, void(*fn)(FILE*, FILE*, void *)) 821 1.1 christos { 822 1.1 christos #ifdef CHARSET_SUPPORT 823 1.1 christos char *charset; 824 1.10 christos 825 1.1 christos charset = value(ENAME_MIME_CHARSET); 826 1.1 christos if (charset && mip->mi_type && strcasecmp(mip->mi_type, "text") == 0) 827 1.1 christos run_mime_ficonv(mip, charset); 828 1.1 christos #endif /* CHARSET_SUPPORT */ 829 1.1 christos 830 1.4 christos if (mip->mi_detachdir == NULL && 831 1.4 christos fn == mime_fio_copy)/* XXX - avoid an extra unnecessary pipe stage */ 832 1.1 christos return; 833 1.1 christos 834 1.4 christos mime_run_function(fn, pipe_end(mip), 835 1.4 christos mip->mi_detachdir ? NULL : __UNCONST("add_lf")); 836 1.1 christos } 837 1.1 christos 838 1.1 christos 839 1.1 christos /* 840 1.1 christos * Determine how to handle the display based on the type and subtype 841 1.1 christos * fields. 842 1.1 christos */ 843 1.1 christos enum dispmode_e { 844 1.1 christos DM_IGNORE = 0x00, /* silently ignore part - must be zero! */ 845 1.1 christos DM_DISPLAY, /* decode and display the part */ 846 1.1 christos DM_UNKNOWN, /* unknown display */ 847 1.1 christos DM_BINARY, /* indicate binary data */ 848 1.1 christos DM_PGPSIGN, /* OpenPGP signed part */ 849 1.1 christos DM_PGPENCR, /* OpenPGP encrypted part */ 850 1.14 lukem DM_PGPKEYS, /* OpenPGP keys part */ 851 1.14 lukem DM_SENTINEL /* end marker; shouldn't be used */ 852 1.1 christos }; 853 1.1 christos #define APPLICATION_OCTET_STREAM DM_BINARY 854 1.1 christos 855 1.1 christos static enum dispmode_e 856 1.1 christos get_display_mode(struct mime_info *mip, mime_codec_t dec) 857 1.1 christos { 858 1.1 christos struct mime_subtype_s { 859 1.1 christos const char *st_name; 860 1.1 christos enum dispmode_e st_dispmode; 861 1.1 christos }; 862 1.1 christos struct mime_type_s { 863 1.1 christos const char *mt_type; 864 1.1 christos const struct mime_subtype_s *mt_subtype; 865 1.1 christos enum dispmode_e mt_dispmode; /* default if NULL subtype */ 866 1.1 christos }; 867 1.1 christos static const struct mime_subtype_s text_subtype_tbl[] = { 868 1.1 christos { "plain", DM_DISPLAY }, 869 1.1 christos { "html", DM_DISPLAY }, /* rfc2854 */ 870 1.1 christos { "rfc822-headers", DM_DISPLAY }, 871 1.1 christos { "css", DM_DISPLAY }, /* rfc2318 */ 872 1.1 christos { "enriched", DM_DISPLAY }, /* rfc1523/rfc1563/rfc1896 */ 873 1.1 christos { "graphics", DM_DISPLAY }, /* rfc0553 */ 874 1.1 christos { "nroff", DM_DISPLAY }, /* rfc4263 */ 875 1.1 christos { "red", DM_DISPLAY }, /* rfc4102 */ 876 1.1 christos { NULL, DM_DISPLAY } /* default */ 877 1.1 christos }; 878 1.1 christos static const struct mime_subtype_s image_subtype_tbl[] = { 879 1.1 christos { "tiff", DM_BINARY }, /* rfc2302/rfc3302 */ 880 1.1 christos { "tiff-fx", DM_BINARY }, /* rfc3250/rfc3950 */ 881 1.1 christos { "t38", DM_BINARY }, /* rfc3362 */ 882 1.1 christos { NULL, DM_BINARY } /* default */ 883 1.1 christos }; 884 1.1 christos static const struct mime_subtype_s audio_subtype_tbl[] = { 885 1.1 christos { "mpeg", DM_BINARY }, /* rfc3003 */ 886 1.1 christos { "t38", DM_BINARY }, /* rfc4612 */ 887 1.1 christos { NULL, DM_BINARY } /* default */ 888 1.1 christos }; 889 1.1 christos static const struct mime_subtype_s video_subtype_tbl[] = { 890 1.1 christos { NULL, DM_BINARY } /* default */ 891 1.1 christos }; 892 1.1 christos static const struct mime_subtype_s application_subtype_tbl[] = { 893 1.1 christos { "octet-stream", APPLICATION_OCTET_STREAM }, 894 1.10 christos { "pgp-encrypted", DM_PGPENCR }, /* rfc3156 */ 895 1.1 christos { "pgp-keys", DM_PGPKEYS }, /* rfc3156 */ 896 1.1 christos { "pgp-signature", DM_PGPSIGN }, /* rfc3156 */ 897 1.1 christos { "pdf", DM_BINARY }, /* rfc3778 */ 898 1.1 christos { "whoispp-query", DM_UNKNOWN }, /* rfc2957 */ 899 1.1 christos { "whoispp-response", DM_UNKNOWN }, /* rfc2958 */ 900 1.1 christos { "font-tdpfr", DM_UNKNOWN }, /* rfc3073 */ 901 1.1 christos { "xhtml+xml", DM_UNKNOWN }, /* rfc3236 */ 902 1.1 christos { "ogg", DM_UNKNOWN }, /* rfc3534 */ 903 1.1 christos { "rdf+xml", DM_UNKNOWN }, /* rfc3870 */ 904 1.1 christos { "soap+xml", DM_UNKNOWN }, /* rfc3902 */ 905 1.1 christos { "mbox", DM_UNKNOWN }, /* rfc4155 */ 906 1.1 christos { "xv+xml", DM_UNKNOWN }, /* rfc4374 */ 907 1.1 christos { "smil", DM_UNKNOWN }, /* rfc4536 */ 908 1.1 christos { "smil+xml", DM_UNKNOWN }, /* rfc4536 */ 909 1.1 christos { "json", DM_UNKNOWN }, /* rfc4627 */ 910 1.1 christos { "voicexml+xml", DM_UNKNOWN }, /* rfc4267 */ 911 1.1 christos { "ssml+xml", DM_UNKNOWN }, /* rfc4267 */ 912 1.1 christos { "srgs", DM_UNKNOWN }, /* rfc4267 */ 913 1.1 christos { "srgs+xml", DM_UNKNOWN }, /* rfc4267 */ 914 1.1 christos { "ccxml+xml", DM_UNKNOWN }, /* rfc4267 */ 915 1.1 christos { "pls+xml.", DM_UNKNOWN }, /* rfc4267 */ 916 1.1 christos { NULL, APPLICATION_OCTET_STREAM } /* default */ 917 1.1 christos }; 918 1.1 christos static const struct mime_type_s mime_type_tbl[] = { 919 1.1 christos { "text", text_subtype_tbl, DM_DISPLAY }, 920 1.1 christos { "image", image_subtype_tbl, DM_IGNORE }, 921 1.1 christos { "audio", audio_subtype_tbl, DM_IGNORE }, 922 1.1 christos { "video", video_subtype_tbl, DM_IGNORE }, 923 1.1 christos { "application", application_subtype_tbl, APPLICATION_OCTET_STREAM }, 924 1.8 christos { NULL, NULL, DM_UNKNOWN }, /* default */ 925 1.1 christos }; 926 1.1 christos const struct mime_type_s *mtp; 927 1.1 christos const struct mime_subtype_s *stp; 928 1.1 christos const char *mi_type; 929 1.1 christos const char *mi_subtype; 930 1.1 christos 931 1.1 christos /* 932 1.1 christos * Silently ignore all multipart bodies. 933 1.1 christos * 1) In the case of "multipart" types, this typically 934 1.1 christos * contains a message for non-mime enabled mail readers. 935 1.1 christos * 2) In the case of "message" type, there should be no body. 936 1.1 christos */ 937 1.4 christos if (mip->mi_ignore_body) /*is_multipart(mip) || is_message(mip))*/ 938 1.1 christos return DM_IGNORE; 939 1.1 christos 940 1.1 christos /* 941 1.1 christos * If the encoding type given but not recognized, treat block 942 1.1 christos * as "application/octet-stream". rfc 2049 sec 2 part 2. 943 1.1 christos */ 944 1.1 christos if (mip->mi_encoding && dec == NULL) 945 1.1 christos return APPLICATION_OCTET_STREAM; 946 1.1 christos 947 1.1 christos mi_type = mip->mi_type; 948 1.1 christos mi_subtype = mip->mi_type ? mip->mi_subtype : NULL; 949 1.1 christos 950 1.1 christos /* 951 1.1 christos * If there was no type specified, display anyway so we don't 952 1.1 christos * miss anything. (The encoding type is known.) 953 1.1 christos */ 954 1.1 christos if (mi_type == NULL) 955 1.1 christos return DM_DISPLAY; /* XXX - default to something safe! */ 956 1.1 christos 957 1.1 christos for (mtp = mime_type_tbl; mtp->mt_type; mtp++) { 958 1.1 christos if (strcasecmp(mtp->mt_type, mi_type) == 0) { 959 1.1 christos if (mi_subtype == NULL) 960 1.1 christos return mtp->mt_dispmode; 961 1.1 christos for (stp = mtp->mt_subtype; stp->st_name; stp++) { 962 1.1 christos if (strcasecmp(stp->st_name, mi_subtype) == 0) 963 1.1 christos return stp->st_dispmode; 964 1.1 christos } 965 1.1 christos return stp->st_dispmode; 966 1.1 christos } 967 1.1 christos } 968 1.1 christos return mtp->mt_dispmode; 969 1.1 christos } 970 1.1 christos 971 1.1 christos 972 1.1 christos PUBLIC FILE * 973 1.1 christos mime_decode_body(struct mime_info *mip) 974 1.1 christos { 975 1.1 christos static enum dispmode_e dispmode; 976 1.1 christos mime_codec_t dec; 977 1.1 christos const char *cmd; 978 1.1 christos 979 1.1 christos /* close anything left over from mime_decode_head() */ 980 1.1 christos close_top_files(mip->mi_head_end); 981 1.1 christos 982 1.1 christos /* 983 1.1 christos * Make sure we flush everything down the pipe so children 984 1.1 christos * don't see it. 985 1.1 christos */ 986 1.1 christos (void)fflush(pipe_end(mip)); 987 1.1 christos 988 1.4 christos if (mip->mi_detachdir) /* We are detaching! Ignore the hooks. */ 989 1.4 christos return mime_detach_parts(mip); 990 1.4 christos 991 1.1 christos cmd = NULL; 992 1.1 christos if (mip->mi_command_hook == NULL) 993 1.1 christos cmd = get_command_hook(mip, "-body"); 994 1.1 christos 995 1.1 christos dec = mime_fio_decoder(mip->mi_encoding); 996 1.4 christos 997 1.1 christos /* 998 1.1 christos * If there is a filter running, we need to send the message 999 1.1 christos * to it. Otherwise, get the default display mode for this body. 1000 1.1 christos */ 1001 1.1 christos dispmode = cmd || mip->mi_command_hook ? DM_DISPLAY : get_display_mode(mip, dec); 1002 1.1 christos 1003 1.1 christos if (dec == NULL) /* make sure we have a usable decoder */ 1004 1.1 christos dec = mime_fio_decoder(MIME_TRANSFER_7BIT); 1005 1.1 christos 1006 1.1 christos if (dispmode == DM_DISPLAY) { 1007 1.1 christos int flags; 1008 1.1 christos if (cmd == NULL) 1009 1.1 christos /* just get the flags */ 1010 1.1 christos flags = mime_run_command(mip->mi_command_hook, NULL); 1011 1.1 christos else 1012 1.1 christos flags = mime_run_command(cmd, pipe_end(mip)); 1013 1.1 christos if ((flags & CMD_FLAG_NO_DECODE) == 0) 1014 1.1 christos run_decoder(mip, dec); 1015 1.1 christos return pipe_end(mip); 1016 1.1 christos } 1017 1.1 christos else { 1018 1.1 christos static const struct msg_tbl_s { 1019 1.1 christos enum dispmode_e dm; 1020 1.1 christos const char *msg; 1021 1.1 christos } msg_tbl[] = { 1022 1.1 christos { DM_BINARY, "binary content" }, 1023 1.1 christos { DM_PGPSIGN, "OpenPGP signature" }, 1024 1.1 christos { DM_PGPENCR, "OpenPGP encrypted" }, 1025 1.1 christos { DM_PGPKEYS, "OpenPGP keys" }, 1026 1.1 christos { DM_UNKNOWN, "unknown data" }, 1027 1.1 christos { DM_IGNORE, NULL }, 1028 1.14 lukem { DM_SENTINEL, NULL }, 1029 1.1 christos }; 1030 1.1 christos const struct msg_tbl_s *mp; 1031 1.1 christos 1032 1.14 lukem for (mp = msg_tbl; mp->dm != DM_SENTINEL; mp++) 1033 1.1 christos if (mp->dm == dispmode) 1034 1.1 christos break; 1035 1.1 christos 1036 1.14 lukem assert(mp->dm != DM_SENTINEL); /* msg_tbl is short if this happens! */ 1037 1.1 christos 1038 1.1 christos if (mp->msg) 1039 1.1 christos (void)fprintf(pipe_end(mip), " [%s]\n\n", mp->msg); 1040 1.1 christos 1041 1.1 christos return NULL; 1042 1.1 christos } 1043 1.1 christos } 1044 1.1 christos 1045 1.1 christos 1046 1.1 christos /************************************************************************ 1047 1.1 christos * Higher level header decoding interface. 1048 1.1 christos * 1049 1.1 christos * The core routines are in mime_header.c. 1050 1.1 christos */ 1051 1.1 christos 1052 1.12 christos /* 1053 1.12 christos * Decode a portion of the header field. 1054 1.12 christos * 1055 1.12 christos * linebuf buffer to decode into. 1056 1.12 christos * bufsize size of linebuf. 1057 1.12 christos * hdrline full header line including header name. 1058 1.12 christos * srcstr pointer to string to decode 1059 1.12 christos */ 1060 1.1 christos PUBLIC char * 1061 1.12 christos mime_decode_hfield(char *linebuf, size_t bufsize, const char *hdrline, char *srcstr) 1062 1.1 christos { 1063 1.1 christos hfield_decoder_t decode; 1064 1.12 christos decode = mime_hfield_decoder(hdrline); 1065 1.1 christos if (decode) { 1066 1.12 christos decode(linebuf, bufsize, srcstr); 1067 1.1 christos return linebuf; 1068 1.1 christos } 1069 1.12 christos return srcstr; 1070 1.1 christos } 1071 1.1 christos 1072 1.1 christos /* 1073 1.12 christos * Return the next header field found in the input stream. 1074 1.12 christos * Return 0 if something found, -1 otherwise. 1075 1.12 christos * For a proper header, "*colon" is set to point to the colon 1076 1.12 christos * terminating the header name. Otherwise it is NULL. 1077 1.12 christos * 1078 1.12 christos * NOTE: unlike gethfield() in support.c this: 1079 1.12 christos * 1) preserves folding (newlines), 1080 1.12 christos * 2) reads until fgetln() gets an EOF, 1081 1.12 christos * 3) only sets *colon if there is a "proper" one. 1082 1.1 christos */ 1083 1.1 christos static int 1084 1.12 christos get_folded_hfield(FILE *f, char *linebuf, size_t bufsize, char **colon) 1085 1.1 christos { 1086 1.1 christos char *cp, *cp2; 1087 1.1 christos char *line; 1088 1.1 christos size_t len; 1089 1.10 christos 1090 1.12 christos if ((cp = fgetln(f, &len)) == NULL) 1091 1.12 christos return -1; 1092 1.12 christos for (cp2 = cp; 1093 1.12 christos cp2 < cp + len && isprint((unsigned char)*cp2) && 1094 1.12 christos !is_WSP(*cp2) && *cp2 != ':'; 1095 1.12 christos cp2++) 1096 1.12 christos continue; 1097 1.12 christos len = MIN(bufsize - 1, len); 1098 1.12 christos bufsize -= len; 1099 1.12 christos (void)memcpy(linebuf, cp, len); 1100 1.12 christos *colon = *cp2 == ':' ? linebuf + (cp2 - cp) : NULL; 1101 1.12 christos line = linebuf + len; 1102 1.1 christos for (;;) { 1103 1.12 christos int c; 1104 1.12 christos (void)ungetc(c = getc(f), f); 1105 1.12 christos if (!is_WSP(c)) 1106 1.12 christos break; 1107 1.12 christos 1108 1.1 christos if ((cp = fgetln(f, &len)) == NULL) 1109 1.12 christos break; 1110 1.1 christos len = MIN(bufsize - 1, len); 1111 1.1 christos bufsize -= len; 1112 1.12 christos if (len == 0) 1113 1.12 christos break; 1114 1.12 christos (void)memcpy(line, cp, len); 1115 1.12 christos line += len; 1116 1.1 christos } 1117 1.12 christos *line = 0; 1118 1.12 christos return 0; 1119 1.1 christos } 1120 1.1 christos 1121 1.1 christos static void 1122 1.1 christos decode_header(FILE *fi, FILE *fo, void *cookie __unused) 1123 1.1 christos { 1124 1.1 christos char linebuf[LINESIZE]; 1125 1.1 christos char *colon; 1126 1.1 christos #ifdef __lint__ 1127 1.1 christos cookie = cookie; 1128 1.1 christos #endif 1129 1.12 christos while (get_folded_hfield(fi, linebuf, sizeof(linebuf), &colon) >= 0) { 1130 1.1 christos char decbuf[LINESIZE]; 1131 1.1 christos char *hdrstr; 1132 1.1 christos hdrstr = linebuf; 1133 1.1 christos if (colon) 1134 1.12 christos hdrstr = mime_decode_hfield(decbuf, sizeof(decbuf), hdrstr, hdrstr); 1135 1.17 christos (void)fputs(hdrstr, fo); 1136 1.1 christos } 1137 1.1 christos } 1138 1.1 christos 1139 1.1 christos PUBLIC FILE * 1140 1.1 christos mime_decode_header(struct mime_info *mip) 1141 1.1 christos { 1142 1.1 christos int flags; 1143 1.1 christos const char *cmd; 1144 1.1 christos FILE *fo; 1145 1.1 christos 1146 1.1 christos fo = pipe_end(mip); 1147 1.1 christos 1148 1.4 christos if (mip->mi_detachdir) { /* We are detaching. Don't run anything! */ 1149 1.4 christos (void)fflush(fo); 1150 1.4 christos return pipe_end(mip); 1151 1.1 christos } 1152 1.4 christos 1153 1.4 christos if (mip->mi_partnum) 1154 1.4 christos (void)fprintf(fo, "----- Part %s -----\n", mip->mi_partstr); 1155 1.4 christos 1156 1.4 christos (void)fflush(fo); /* Flush so the childern don't see it. */ 1157 1.10 christos 1158 1.1 christos /* 1159 1.1 christos * install the message hook before the head hook. 1160 1.1 christos */ 1161 1.1 christos cmd = get_command_hook(mip, "-hook"); 1162 1.1 christos mip->mi_command_hook = cmd; 1163 1.1 christos if (cmd) { 1164 1.1 christos flags = mime_run_command(cmd, pipe_end(mip)); 1165 1.1 christos mip->mi_head_end = last_registered_file(0); 1166 1.1 christos } 1167 1.1 christos else { 1168 1.1 christos cmd = get_command_hook(mip, "-head"); 1169 1.1 christos mip->mi_head_end = last_registered_file(0); 1170 1.1 christos flags = mime_run_command(cmd, pipe_end(mip)); 1171 1.10 christos } 1172 1.1 christos 1173 1.1 christos if (value(ENAME_MIME_DECODE_HDR) && (flags & CMD_FLAG_NO_DECODE) == 0) 1174 1.1 christos mime_run_function(decode_header, pipe_end(mip), NULL); 1175 1.10 christos 1176 1.1 christos return pipe_end(mip); 1177 1.1 christos } 1178 1.1 christos 1179 1.1 christos #endif /* MIME_SUPPORT */ 1180