17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * Copyright (c) 1998-2003 Sendmail, Inc. and its suppliers. 37c478bd9Sstevel@tonic-gate * All rights reserved. 47c478bd9Sstevel@tonic-gate * Copyright (c) 1994, 1996-1997 Eric P. Allman. All rights reserved. 57c478bd9Sstevel@tonic-gate * Copyright (c) 1994 67c478bd9Sstevel@tonic-gate * The Regents of the University of California. All rights reserved. 77c478bd9Sstevel@tonic-gate * 87c478bd9Sstevel@tonic-gate * By using this file, you agree to the terms and conditions set 97c478bd9Sstevel@tonic-gate * forth in the LICENSE file which can be found at the top level of 107c478bd9Sstevel@tonic-gate * the sendmail distribution. 117c478bd9Sstevel@tonic-gate * 127c478bd9Sstevel@tonic-gate */ 137c478bd9Sstevel@tonic-gate 147c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 157c478bd9Sstevel@tonic-gate 167c478bd9Sstevel@tonic-gate #include <sendmail.h> 177c478bd9Sstevel@tonic-gate #include <string.h> 187c478bd9Sstevel@tonic-gate 19*445f2479Sjbeck SM_RCSID("@(#)$Id: mime.c,v 8.139 2006/03/01 18:07:45 ca Exp $") 207c478bd9Sstevel@tonic-gate 217c478bd9Sstevel@tonic-gate /* 227c478bd9Sstevel@tonic-gate ** MIME support. 237c478bd9Sstevel@tonic-gate ** 247c478bd9Sstevel@tonic-gate ** I am indebted to John Beck of Hewlett-Packard, who contributed 257c478bd9Sstevel@tonic-gate ** his code to me for inclusion. As it turns out, I did not use 267c478bd9Sstevel@tonic-gate ** his code since he used a "minimum change" approach that used 277c478bd9Sstevel@tonic-gate ** several temp files, and I wanted a "minimum impact" approach 287c478bd9Sstevel@tonic-gate ** that would avoid copying. However, looking over his code 297c478bd9Sstevel@tonic-gate ** helped me cement my understanding of the problem. 307c478bd9Sstevel@tonic-gate ** 317c478bd9Sstevel@tonic-gate ** I also looked at, but did not directly use, Nathaniel 327c478bd9Sstevel@tonic-gate ** Borenstein's "code.c" module. Again, it functioned as 337c478bd9Sstevel@tonic-gate ** a file-to-file translator, which did not fit within my 347c478bd9Sstevel@tonic-gate ** design bounds, but it was a useful base for understanding 357c478bd9Sstevel@tonic-gate ** the problem. 367c478bd9Sstevel@tonic-gate */ 377c478bd9Sstevel@tonic-gate 387c478bd9Sstevel@tonic-gate /* use "old" mime 7 to 8 algorithm by default */ 397c478bd9Sstevel@tonic-gate #ifndef MIME7TO8_OLD 407c478bd9Sstevel@tonic-gate # define MIME7TO8_OLD 1 417c478bd9Sstevel@tonic-gate #endif /* ! MIME7TO8_OLD */ 427c478bd9Sstevel@tonic-gate 437c478bd9Sstevel@tonic-gate #if MIME8TO7 447c478bd9Sstevel@tonic-gate static int isboundary __P((char *, char **)); 457c478bd9Sstevel@tonic-gate static int mimeboundary __P((char *, char **)); 467c478bd9Sstevel@tonic-gate static int mime_getchar __P((SM_FILE_T *, char **, int *)); 477c478bd9Sstevel@tonic-gate static int mime_getchar_crlf __P((SM_FILE_T *, char **, int *)); 487c478bd9Sstevel@tonic-gate 497c478bd9Sstevel@tonic-gate /* character set for hex and base64 encoding */ 507c478bd9Sstevel@tonic-gate static char Base16Code[] = "0123456789ABCDEF"; 517c478bd9Sstevel@tonic-gate static char Base64Code[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; 527c478bd9Sstevel@tonic-gate 537c478bd9Sstevel@tonic-gate /* types of MIME boundaries */ 547c478bd9Sstevel@tonic-gate # define MBT_SYNTAX 0 /* syntax error */ 557c478bd9Sstevel@tonic-gate # define MBT_NOTSEP 1 /* not a boundary */ 567c478bd9Sstevel@tonic-gate # define MBT_INTERMED 2 /* intermediate boundary (no trailing --) */ 577c478bd9Sstevel@tonic-gate # define MBT_FINAL 3 /* final boundary (trailing -- included) */ 587c478bd9Sstevel@tonic-gate 597c478bd9Sstevel@tonic-gate static char *MimeBoundaryNames[] = 607c478bd9Sstevel@tonic-gate { 617c478bd9Sstevel@tonic-gate "SYNTAX", "NOTSEP", "INTERMED", "FINAL" 627c478bd9Sstevel@tonic-gate }; 637c478bd9Sstevel@tonic-gate 647c478bd9Sstevel@tonic-gate static bool MapNLtoCRLF; 657c478bd9Sstevel@tonic-gate 667c478bd9Sstevel@tonic-gate /* 677c478bd9Sstevel@tonic-gate ** MIME8TO7 -- output 8 bit body in 7 bit format 687c478bd9Sstevel@tonic-gate ** 697c478bd9Sstevel@tonic-gate ** The header has already been output -- this has to do the 707c478bd9Sstevel@tonic-gate ** 8 to 7 bit conversion. It would be easy if we didn't have 717c478bd9Sstevel@tonic-gate ** to deal with nested formats (multipart/xxx and message/rfc822). 727c478bd9Sstevel@tonic-gate ** 737c478bd9Sstevel@tonic-gate ** We won't be called if we don't have to do a conversion, and 747c478bd9Sstevel@tonic-gate ** appropriate MIME-Version: and Content-Type: fields have been 757c478bd9Sstevel@tonic-gate ** output. Any Content-Transfer-Encoding: field has not been 767c478bd9Sstevel@tonic-gate ** output, and we can add it here. 777c478bd9Sstevel@tonic-gate ** 787c478bd9Sstevel@tonic-gate ** Parameters: 797c478bd9Sstevel@tonic-gate ** mci -- mailer connection information. 807c478bd9Sstevel@tonic-gate ** header -- the header for this body part. 817c478bd9Sstevel@tonic-gate ** e -- envelope. 827c478bd9Sstevel@tonic-gate ** boundaries -- the currently pending message boundaries. 837c478bd9Sstevel@tonic-gate ** NULL if we are processing the outer portion. 847c478bd9Sstevel@tonic-gate ** flags -- to tweak processing. 857c478bd9Sstevel@tonic-gate ** 867c478bd9Sstevel@tonic-gate ** Returns: 877c478bd9Sstevel@tonic-gate ** An indicator of what terminated the message part: 887c478bd9Sstevel@tonic-gate ** MBT_FINAL -- the final boundary 897c478bd9Sstevel@tonic-gate ** MBT_INTERMED -- an intermediate boundary 907c478bd9Sstevel@tonic-gate ** MBT_NOTSEP -- an end of file 91*445f2479Sjbeck ** SM_IO_EOF -- I/O error occurred 927c478bd9Sstevel@tonic-gate */ 937c478bd9Sstevel@tonic-gate 947c478bd9Sstevel@tonic-gate struct args 957c478bd9Sstevel@tonic-gate { 967c478bd9Sstevel@tonic-gate char *a_field; /* name of field */ 977c478bd9Sstevel@tonic-gate char *a_value; /* value of that field */ 987c478bd9Sstevel@tonic-gate }; 997c478bd9Sstevel@tonic-gate 1007c478bd9Sstevel@tonic-gate int 1017c478bd9Sstevel@tonic-gate mime8to7(mci, header, e, boundaries, flags) 1027c478bd9Sstevel@tonic-gate register MCI *mci; 1037c478bd9Sstevel@tonic-gate HDR *header; 1047c478bd9Sstevel@tonic-gate register ENVELOPE *e; 1057c478bd9Sstevel@tonic-gate char **boundaries; 1067c478bd9Sstevel@tonic-gate int flags; 1077c478bd9Sstevel@tonic-gate { 1087c478bd9Sstevel@tonic-gate register char *p; 1097c478bd9Sstevel@tonic-gate int linelen; 1107c478bd9Sstevel@tonic-gate int bt; 1117c478bd9Sstevel@tonic-gate off_t offset; 1127c478bd9Sstevel@tonic-gate size_t sectionsize, sectionhighbits; 1137c478bd9Sstevel@tonic-gate int i; 1147c478bd9Sstevel@tonic-gate char *type; 1157c478bd9Sstevel@tonic-gate char *subtype; 1167c478bd9Sstevel@tonic-gate char *cte; 1177c478bd9Sstevel@tonic-gate char **pvp; 1187c478bd9Sstevel@tonic-gate int argc = 0; 1197c478bd9Sstevel@tonic-gate char *bp; 1207c478bd9Sstevel@tonic-gate bool use_qp = false; 1217c478bd9Sstevel@tonic-gate struct args argv[MAXMIMEARGS]; 1227c478bd9Sstevel@tonic-gate char bbuf[128]; 1237c478bd9Sstevel@tonic-gate char buf[MAXLINE]; 1247c478bd9Sstevel@tonic-gate char pvpbuf[MAXLINE]; 1257c478bd9Sstevel@tonic-gate extern unsigned char MimeTokenTab[256]; 1267c478bd9Sstevel@tonic-gate 1277c478bd9Sstevel@tonic-gate if (tTd(43, 1)) 1287c478bd9Sstevel@tonic-gate { 1297c478bd9Sstevel@tonic-gate sm_dprintf("mime8to7: flags = %x, boundaries =", flags); 1307c478bd9Sstevel@tonic-gate if (boundaries[0] == NULL) 1317c478bd9Sstevel@tonic-gate sm_dprintf(" <none>"); 1327c478bd9Sstevel@tonic-gate else 1337c478bd9Sstevel@tonic-gate { 1347c478bd9Sstevel@tonic-gate for (i = 0; boundaries[i] != NULL; i++) 1357c478bd9Sstevel@tonic-gate sm_dprintf(" %s", boundaries[i]); 1367c478bd9Sstevel@tonic-gate } 1377c478bd9Sstevel@tonic-gate sm_dprintf("\n"); 1387c478bd9Sstevel@tonic-gate } 1397c478bd9Sstevel@tonic-gate MapNLtoCRLF = true; 1407c478bd9Sstevel@tonic-gate p = hvalue("Content-Transfer-Encoding", header); 1417c478bd9Sstevel@tonic-gate if (p == NULL || 1427c478bd9Sstevel@tonic-gate (pvp = prescan(p, '\0', pvpbuf, sizeof pvpbuf, NULL, 1437c478bd9Sstevel@tonic-gate MimeTokenTab, false)) == NULL || 1447c478bd9Sstevel@tonic-gate pvp[0] == NULL) 1457c478bd9Sstevel@tonic-gate { 1467c478bd9Sstevel@tonic-gate cte = NULL; 1477c478bd9Sstevel@tonic-gate } 1487c478bd9Sstevel@tonic-gate else 1497c478bd9Sstevel@tonic-gate { 1507c478bd9Sstevel@tonic-gate cataddr(pvp, NULL, buf, sizeof buf, '\0'); 1517c478bd9Sstevel@tonic-gate cte = sm_rpool_strdup_x(e->e_rpool, buf); 1527c478bd9Sstevel@tonic-gate } 1537c478bd9Sstevel@tonic-gate 1547c478bd9Sstevel@tonic-gate type = subtype = NULL; 1557c478bd9Sstevel@tonic-gate p = hvalue("Content-Type", header); 1567c478bd9Sstevel@tonic-gate if (p == NULL) 1577c478bd9Sstevel@tonic-gate { 1587c478bd9Sstevel@tonic-gate if (bitset(M87F_DIGEST, flags)) 1597c478bd9Sstevel@tonic-gate p = "message/rfc822"; 1607c478bd9Sstevel@tonic-gate else 1617c478bd9Sstevel@tonic-gate p = "text/plain"; 1627c478bd9Sstevel@tonic-gate } 1637c478bd9Sstevel@tonic-gate if (p != NULL && 1647c478bd9Sstevel@tonic-gate (pvp = prescan(p, '\0', pvpbuf, sizeof pvpbuf, NULL, 1657c478bd9Sstevel@tonic-gate MimeTokenTab, false)) != NULL && 1667c478bd9Sstevel@tonic-gate pvp[0] != NULL) 1677c478bd9Sstevel@tonic-gate { 1687c478bd9Sstevel@tonic-gate if (tTd(43, 40)) 1697c478bd9Sstevel@tonic-gate { 1707c478bd9Sstevel@tonic-gate for (i = 0; pvp[i] != NULL; i++) 1717c478bd9Sstevel@tonic-gate sm_dprintf("pvp[%d] = \"%s\"\n", i, pvp[i]); 1727c478bd9Sstevel@tonic-gate } 1737c478bd9Sstevel@tonic-gate type = *pvp++; 1747c478bd9Sstevel@tonic-gate if (*pvp != NULL && strcmp(*pvp, "/") == 0 && 1757c478bd9Sstevel@tonic-gate *++pvp != NULL) 1767c478bd9Sstevel@tonic-gate { 1777c478bd9Sstevel@tonic-gate subtype = *pvp++; 1787c478bd9Sstevel@tonic-gate } 1797c478bd9Sstevel@tonic-gate 1807c478bd9Sstevel@tonic-gate /* break out parameters */ 1817c478bd9Sstevel@tonic-gate while (*pvp != NULL && argc < MAXMIMEARGS) 1827c478bd9Sstevel@tonic-gate { 1837c478bd9Sstevel@tonic-gate /* skip to semicolon separator */ 1847c478bd9Sstevel@tonic-gate while (*pvp != NULL && strcmp(*pvp, ";") != 0) 1857c478bd9Sstevel@tonic-gate pvp++; 1867c478bd9Sstevel@tonic-gate if (*pvp++ == NULL || *pvp == NULL) 1877c478bd9Sstevel@tonic-gate break; 1887c478bd9Sstevel@tonic-gate 1897c478bd9Sstevel@tonic-gate /* complain about empty values */ 1907c478bd9Sstevel@tonic-gate if (strcmp(*pvp, ";") == 0) 1917c478bd9Sstevel@tonic-gate { 1927c478bd9Sstevel@tonic-gate usrerr("mime8to7: Empty parameter in Content-Type header"); 1937c478bd9Sstevel@tonic-gate 1947c478bd9Sstevel@tonic-gate /* avoid bounce loops */ 1957c478bd9Sstevel@tonic-gate e->e_flags |= EF_DONT_MIME; 1967c478bd9Sstevel@tonic-gate continue; 1977c478bd9Sstevel@tonic-gate } 1987c478bd9Sstevel@tonic-gate 1997c478bd9Sstevel@tonic-gate /* extract field name */ 2007c478bd9Sstevel@tonic-gate argv[argc].a_field = *pvp++; 2017c478bd9Sstevel@tonic-gate 2027c478bd9Sstevel@tonic-gate /* see if there is a value */ 2037c478bd9Sstevel@tonic-gate if (*pvp != NULL && strcmp(*pvp, "=") == 0 && 2047c478bd9Sstevel@tonic-gate (*++pvp == NULL || strcmp(*pvp, ";") != 0)) 2057c478bd9Sstevel@tonic-gate { 2067c478bd9Sstevel@tonic-gate argv[argc].a_value = *pvp; 2077c478bd9Sstevel@tonic-gate argc++; 2087c478bd9Sstevel@tonic-gate } 2097c478bd9Sstevel@tonic-gate } 2107c478bd9Sstevel@tonic-gate } 2117c478bd9Sstevel@tonic-gate 2127c478bd9Sstevel@tonic-gate /* check for disaster cases */ 2137c478bd9Sstevel@tonic-gate if (type == NULL) 2147c478bd9Sstevel@tonic-gate type = "-none-"; 2157c478bd9Sstevel@tonic-gate if (subtype == NULL) 2167c478bd9Sstevel@tonic-gate subtype = "-none-"; 2177c478bd9Sstevel@tonic-gate 2187c478bd9Sstevel@tonic-gate /* don't propogate some flags more than one level into the message */ 2197c478bd9Sstevel@tonic-gate flags &= ~M87F_DIGEST; 2207c478bd9Sstevel@tonic-gate 2217c478bd9Sstevel@tonic-gate /* 2227c478bd9Sstevel@tonic-gate ** Check for cases that can not be encoded. 2237c478bd9Sstevel@tonic-gate ** 2247c478bd9Sstevel@tonic-gate ** For example, you can't encode certain kinds of types 2257c478bd9Sstevel@tonic-gate ** or already-encoded messages. If we find this case, 2267c478bd9Sstevel@tonic-gate ** just copy it through. 2277c478bd9Sstevel@tonic-gate */ 2287c478bd9Sstevel@tonic-gate 2297c478bd9Sstevel@tonic-gate (void) sm_snprintf(buf, sizeof buf, "%.100s/%.100s", type, subtype); 2307c478bd9Sstevel@tonic-gate if (wordinclass(buf, 'n') || (cte != NULL && !wordinclass(cte, 'e'))) 2317c478bd9Sstevel@tonic-gate flags |= M87F_NO8BIT; 2327c478bd9Sstevel@tonic-gate 2337c478bd9Sstevel@tonic-gate # ifdef USE_B_CLASS 2347c478bd9Sstevel@tonic-gate if (wordinclass(buf, 'b') || wordinclass(type, 'b')) 2357c478bd9Sstevel@tonic-gate MapNLtoCRLF = false; 2367c478bd9Sstevel@tonic-gate # endif /* USE_B_CLASS */ 2377c478bd9Sstevel@tonic-gate if (wordinclass(buf, 'q') || wordinclass(type, 'q')) 2387c478bd9Sstevel@tonic-gate use_qp = true; 2397c478bd9Sstevel@tonic-gate 2407c478bd9Sstevel@tonic-gate /* 2417c478bd9Sstevel@tonic-gate ** Multipart requires special processing. 2427c478bd9Sstevel@tonic-gate ** 2437c478bd9Sstevel@tonic-gate ** Do a recursive descent into the message. 2447c478bd9Sstevel@tonic-gate */ 2457c478bd9Sstevel@tonic-gate 2467c478bd9Sstevel@tonic-gate if (sm_strcasecmp(type, "multipart") == 0 && 2477c478bd9Sstevel@tonic-gate (!bitset(M87F_NO8BIT, flags) || bitset(M87F_NO8TO7, flags))) 2487c478bd9Sstevel@tonic-gate { 2497c478bd9Sstevel@tonic-gate 2507c478bd9Sstevel@tonic-gate if (sm_strcasecmp(subtype, "digest") == 0) 2517c478bd9Sstevel@tonic-gate flags |= M87F_DIGEST; 2527c478bd9Sstevel@tonic-gate 2537c478bd9Sstevel@tonic-gate for (i = 0; i < argc; i++) 2547c478bd9Sstevel@tonic-gate { 2557c478bd9Sstevel@tonic-gate if (sm_strcasecmp(argv[i].a_field, "boundary") == 0) 2567c478bd9Sstevel@tonic-gate break; 2577c478bd9Sstevel@tonic-gate } 2587c478bd9Sstevel@tonic-gate if (i >= argc || argv[i].a_value == NULL) 2597c478bd9Sstevel@tonic-gate { 2607c478bd9Sstevel@tonic-gate usrerr("mime8to7: Content-Type: \"%s\": %s boundary", 2617c478bd9Sstevel@tonic-gate i >= argc ? "missing" : "bogus", p); 2627c478bd9Sstevel@tonic-gate p = "---"; 2637c478bd9Sstevel@tonic-gate 2647c478bd9Sstevel@tonic-gate /* avoid bounce loops */ 2657c478bd9Sstevel@tonic-gate e->e_flags |= EF_DONT_MIME; 2667c478bd9Sstevel@tonic-gate } 2677c478bd9Sstevel@tonic-gate else 2687c478bd9Sstevel@tonic-gate { 2697c478bd9Sstevel@tonic-gate p = argv[i].a_value; 2707c478bd9Sstevel@tonic-gate stripquotes(p); 2717c478bd9Sstevel@tonic-gate } 2727c478bd9Sstevel@tonic-gate if (sm_strlcpy(bbuf, p, sizeof bbuf) >= sizeof bbuf) 2737c478bd9Sstevel@tonic-gate { 2747c478bd9Sstevel@tonic-gate usrerr("mime8to7: multipart boundary \"%s\" too long", 2757c478bd9Sstevel@tonic-gate p); 2767c478bd9Sstevel@tonic-gate 2777c478bd9Sstevel@tonic-gate /* avoid bounce loops */ 2787c478bd9Sstevel@tonic-gate e->e_flags |= EF_DONT_MIME; 2797c478bd9Sstevel@tonic-gate } 2807c478bd9Sstevel@tonic-gate 2817c478bd9Sstevel@tonic-gate if (tTd(43, 1)) 2827c478bd9Sstevel@tonic-gate sm_dprintf("mime8to7: multipart boundary \"%s\"\n", 2837c478bd9Sstevel@tonic-gate bbuf); 2847c478bd9Sstevel@tonic-gate for (i = 0; i < MAXMIMENESTING; i++) 2857c478bd9Sstevel@tonic-gate { 2867c478bd9Sstevel@tonic-gate if (boundaries[i] == NULL) 2877c478bd9Sstevel@tonic-gate break; 2887c478bd9Sstevel@tonic-gate } 2897c478bd9Sstevel@tonic-gate if (i >= MAXMIMENESTING) 2907c478bd9Sstevel@tonic-gate { 2917c478bd9Sstevel@tonic-gate usrerr("mime8to7: multipart nesting boundary too deep"); 2927c478bd9Sstevel@tonic-gate 2937c478bd9Sstevel@tonic-gate /* avoid bounce loops */ 2947c478bd9Sstevel@tonic-gate e->e_flags |= EF_DONT_MIME; 2957c478bd9Sstevel@tonic-gate } 2967c478bd9Sstevel@tonic-gate else 2977c478bd9Sstevel@tonic-gate { 2987c478bd9Sstevel@tonic-gate boundaries[i] = bbuf; 2997c478bd9Sstevel@tonic-gate boundaries[i + 1] = NULL; 3007c478bd9Sstevel@tonic-gate } 3017c478bd9Sstevel@tonic-gate mci->mci_flags |= MCIF_INMIME; 3027c478bd9Sstevel@tonic-gate 3037c478bd9Sstevel@tonic-gate /* skip the early "comment" prologue */ 304*445f2479Sjbeck if (!putline("", mci)) 305*445f2479Sjbeck goto writeerr; 3067c478bd9Sstevel@tonic-gate mci->mci_flags &= ~MCIF_INHEADER; 3077c478bd9Sstevel@tonic-gate bt = MBT_FINAL; 3087c478bd9Sstevel@tonic-gate while (sm_io_fgets(e->e_dfp, SM_TIME_DEFAULT, buf, sizeof buf) 3097c478bd9Sstevel@tonic-gate != NULL) 3107c478bd9Sstevel@tonic-gate { 3117c478bd9Sstevel@tonic-gate bt = mimeboundary(buf, boundaries); 3127c478bd9Sstevel@tonic-gate if (bt != MBT_NOTSEP) 3137c478bd9Sstevel@tonic-gate break; 314*445f2479Sjbeck if (!putxline(buf, strlen(buf), mci, 315*445f2479Sjbeck PXLF_MAPFROM|PXLF_STRIP8BIT)) 316*445f2479Sjbeck goto writeerr; 3177c478bd9Sstevel@tonic-gate if (tTd(43, 99)) 3187c478bd9Sstevel@tonic-gate sm_dprintf(" ...%s", buf); 3197c478bd9Sstevel@tonic-gate } 3207c478bd9Sstevel@tonic-gate if (sm_io_eof(e->e_dfp)) 3217c478bd9Sstevel@tonic-gate bt = MBT_FINAL; 3227c478bd9Sstevel@tonic-gate while (bt != MBT_FINAL) 3237c478bd9Sstevel@tonic-gate { 3247c478bd9Sstevel@tonic-gate auto HDR *hdr = NULL; 3257c478bd9Sstevel@tonic-gate 3267c478bd9Sstevel@tonic-gate (void) sm_strlcpyn(buf, sizeof buf, 2, "--", bbuf); 327*445f2479Sjbeck if (!putline(buf, mci)) 328*445f2479Sjbeck goto writeerr; 3297c478bd9Sstevel@tonic-gate if (tTd(43, 35)) 3307c478bd9Sstevel@tonic-gate sm_dprintf(" ...%s\n", buf); 3317c478bd9Sstevel@tonic-gate collect(e->e_dfp, false, &hdr, e, false); 3327c478bd9Sstevel@tonic-gate if (tTd(43, 101)) 3337c478bd9Sstevel@tonic-gate putline("+++after collect", mci); 334*445f2479Sjbeck if (!putheader(mci, hdr, e, flags)) 335*445f2479Sjbeck goto writeerr; 3367c478bd9Sstevel@tonic-gate if (tTd(43, 101)) 3377c478bd9Sstevel@tonic-gate putline("+++after putheader", mci); 3387c478bd9Sstevel@tonic-gate bt = mime8to7(mci, hdr, e, boundaries, flags); 339*445f2479Sjbeck if (bt == SM_IO_EOF) 340*445f2479Sjbeck goto writeerr; 3417c478bd9Sstevel@tonic-gate } 3427c478bd9Sstevel@tonic-gate (void) sm_strlcpyn(buf, sizeof buf, 3, "--", bbuf, "--"); 343*445f2479Sjbeck if (!putline(buf, mci)) 344*445f2479Sjbeck goto writeerr; 3457c478bd9Sstevel@tonic-gate if (tTd(43, 35)) 3467c478bd9Sstevel@tonic-gate sm_dprintf(" ...%s\n", buf); 3477c478bd9Sstevel@tonic-gate boundaries[i] = NULL; 3487c478bd9Sstevel@tonic-gate mci->mci_flags &= ~MCIF_INMIME; 3497c478bd9Sstevel@tonic-gate 3507c478bd9Sstevel@tonic-gate /* skip the late "comment" epilogue */ 3517c478bd9Sstevel@tonic-gate while (sm_io_fgets(e->e_dfp, SM_TIME_DEFAULT, buf, sizeof buf) 3527c478bd9Sstevel@tonic-gate != NULL) 3537c478bd9Sstevel@tonic-gate { 3547c478bd9Sstevel@tonic-gate bt = mimeboundary(buf, boundaries); 3557c478bd9Sstevel@tonic-gate if (bt != MBT_NOTSEP) 3567c478bd9Sstevel@tonic-gate break; 357*445f2479Sjbeck if (!putxline(buf, strlen(buf), mci, 358*445f2479Sjbeck PXLF_MAPFROM|PXLF_STRIP8BIT)) 359*445f2479Sjbeck goto writeerr; 3607c478bd9Sstevel@tonic-gate if (tTd(43, 99)) 3617c478bd9Sstevel@tonic-gate sm_dprintf(" ...%s", buf); 3627c478bd9Sstevel@tonic-gate } 3637c478bd9Sstevel@tonic-gate if (sm_io_eof(e->e_dfp)) 3647c478bd9Sstevel@tonic-gate bt = MBT_FINAL; 3657c478bd9Sstevel@tonic-gate if (tTd(43, 3)) 3667c478bd9Sstevel@tonic-gate sm_dprintf("\t\t\tmime8to7=>%s (multipart)\n", 3677c478bd9Sstevel@tonic-gate MimeBoundaryNames[bt]); 3687c478bd9Sstevel@tonic-gate return bt; 3697c478bd9Sstevel@tonic-gate } 3707c478bd9Sstevel@tonic-gate 3717c478bd9Sstevel@tonic-gate /* 3727c478bd9Sstevel@tonic-gate ** Message/xxx types -- recurse exactly once. 3737c478bd9Sstevel@tonic-gate ** 3747c478bd9Sstevel@tonic-gate ** Class 's' is predefined to have "rfc822" only. 3757c478bd9Sstevel@tonic-gate */ 3767c478bd9Sstevel@tonic-gate 3777c478bd9Sstevel@tonic-gate if (sm_strcasecmp(type, "message") == 0) 3787c478bd9Sstevel@tonic-gate { 3797c478bd9Sstevel@tonic-gate if (!wordinclass(subtype, 's')) 3807c478bd9Sstevel@tonic-gate { 3817c478bd9Sstevel@tonic-gate flags |= M87F_NO8BIT; 3827c478bd9Sstevel@tonic-gate } 3837c478bd9Sstevel@tonic-gate else 3847c478bd9Sstevel@tonic-gate { 3857c478bd9Sstevel@tonic-gate auto HDR *hdr = NULL; 3867c478bd9Sstevel@tonic-gate 387*445f2479Sjbeck if (!putline("", mci)) 388*445f2479Sjbeck goto writeerr; 3897c478bd9Sstevel@tonic-gate 3907c478bd9Sstevel@tonic-gate mci->mci_flags |= MCIF_INMIME; 3917c478bd9Sstevel@tonic-gate collect(e->e_dfp, false, &hdr, e, false); 3927c478bd9Sstevel@tonic-gate if (tTd(43, 101)) 3937c478bd9Sstevel@tonic-gate putline("+++after collect", mci); 394*445f2479Sjbeck if (!putheader(mci, hdr, e, flags)) 395*445f2479Sjbeck goto writeerr; 3967c478bd9Sstevel@tonic-gate if (tTd(43, 101)) 3977c478bd9Sstevel@tonic-gate putline("+++after putheader", mci); 3987c478bd9Sstevel@tonic-gate if (hvalue("MIME-Version", hdr) == NULL && 399*445f2479Sjbeck !bitset(M87F_NO8TO7, flags) && 400*445f2479Sjbeck !putline("MIME-Version: 1.0", mci)) 401*445f2479Sjbeck goto writeerr; 4027c478bd9Sstevel@tonic-gate bt = mime8to7(mci, hdr, e, boundaries, flags); 4037c478bd9Sstevel@tonic-gate mci->mci_flags &= ~MCIF_INMIME; 4047c478bd9Sstevel@tonic-gate return bt; 4057c478bd9Sstevel@tonic-gate } 4067c478bd9Sstevel@tonic-gate } 4077c478bd9Sstevel@tonic-gate 4087c478bd9Sstevel@tonic-gate /* 4097c478bd9Sstevel@tonic-gate ** Non-compound body type 4107c478bd9Sstevel@tonic-gate ** 4117c478bd9Sstevel@tonic-gate ** Compute the ratio of seven to eight bit characters; 4127c478bd9Sstevel@tonic-gate ** use that as a heuristic to decide how to do the 4137c478bd9Sstevel@tonic-gate ** encoding. 4147c478bd9Sstevel@tonic-gate */ 4157c478bd9Sstevel@tonic-gate 4167c478bd9Sstevel@tonic-gate sectionsize = sectionhighbits = 0; 4177c478bd9Sstevel@tonic-gate if (!bitset(M87F_NO8BIT|M87F_NO8TO7, flags)) 4187c478bd9Sstevel@tonic-gate { 4197c478bd9Sstevel@tonic-gate /* remember where we were */ 4207c478bd9Sstevel@tonic-gate offset = sm_io_tell(e->e_dfp, SM_TIME_DEFAULT); 4217c478bd9Sstevel@tonic-gate if (offset == -1) 4227c478bd9Sstevel@tonic-gate syserr("mime8to7: cannot sm_io_tell on %cf%s", 4237c478bd9Sstevel@tonic-gate DATAFL_LETTER, e->e_id); 4247c478bd9Sstevel@tonic-gate 4257c478bd9Sstevel@tonic-gate /* do a scan of this body type to count character types */ 4267c478bd9Sstevel@tonic-gate while (sm_io_fgets(e->e_dfp, SM_TIME_DEFAULT, buf, sizeof buf) 4277c478bd9Sstevel@tonic-gate != NULL) 4287c478bd9Sstevel@tonic-gate { 4297c478bd9Sstevel@tonic-gate if (mimeboundary(buf, boundaries) != MBT_NOTSEP) 4307c478bd9Sstevel@tonic-gate break; 4317c478bd9Sstevel@tonic-gate for (p = buf; *p != '\0'; p++) 4327c478bd9Sstevel@tonic-gate { 4337c478bd9Sstevel@tonic-gate /* count bytes with the high bit set */ 4347c478bd9Sstevel@tonic-gate sectionsize++; 4357c478bd9Sstevel@tonic-gate if (bitset(0200, *p)) 4367c478bd9Sstevel@tonic-gate sectionhighbits++; 4377c478bd9Sstevel@tonic-gate } 4387c478bd9Sstevel@tonic-gate 4397c478bd9Sstevel@tonic-gate /* 4407c478bd9Sstevel@tonic-gate ** Heuristic: if 1/4 of the first 4K bytes are 8-bit, 4417c478bd9Sstevel@tonic-gate ** assume base64. This heuristic avoids double-reading 4427c478bd9Sstevel@tonic-gate ** large graphics or video files. 4437c478bd9Sstevel@tonic-gate */ 4447c478bd9Sstevel@tonic-gate 4457c478bd9Sstevel@tonic-gate if (sectionsize >= 4096 && 4467c478bd9Sstevel@tonic-gate sectionhighbits > sectionsize / 4) 4477c478bd9Sstevel@tonic-gate break; 4487c478bd9Sstevel@tonic-gate } 4497c478bd9Sstevel@tonic-gate 4507c478bd9Sstevel@tonic-gate /* return to the original offset for processing */ 4517c478bd9Sstevel@tonic-gate /* XXX use relative seeks to handle >31 bit file sizes? */ 4527c478bd9Sstevel@tonic-gate if (sm_io_seek(e->e_dfp, SM_TIME_DEFAULT, offset, SEEK_SET) < 0) 4537c478bd9Sstevel@tonic-gate syserr("mime8to7: cannot sm_io_fseek on %cf%s", 4547c478bd9Sstevel@tonic-gate DATAFL_LETTER, e->e_id); 4557c478bd9Sstevel@tonic-gate else 4567c478bd9Sstevel@tonic-gate sm_io_clearerr(e->e_dfp); 4577c478bd9Sstevel@tonic-gate } 4587c478bd9Sstevel@tonic-gate 4597c478bd9Sstevel@tonic-gate /* 4607c478bd9Sstevel@tonic-gate ** Heuristically determine encoding method. 4617c478bd9Sstevel@tonic-gate ** If more than 1/8 of the total characters have the 4627c478bd9Sstevel@tonic-gate ** eighth bit set, use base64; else use quoted-printable. 4637c478bd9Sstevel@tonic-gate ** However, only encode binary encoded data as base64, 4647c478bd9Sstevel@tonic-gate ** since otherwise the NL=>CRLF mapping will be a problem. 4657c478bd9Sstevel@tonic-gate */ 4667c478bd9Sstevel@tonic-gate 4677c478bd9Sstevel@tonic-gate if (tTd(43, 8)) 4687c478bd9Sstevel@tonic-gate { 4697c478bd9Sstevel@tonic-gate sm_dprintf("mime8to7: %ld high bit(s) in %ld byte(s), cte=%s, type=%s/%s\n", 4707c478bd9Sstevel@tonic-gate (long) sectionhighbits, (long) sectionsize, 4717c478bd9Sstevel@tonic-gate cte == NULL ? "[none]" : cte, 4727c478bd9Sstevel@tonic-gate type == NULL ? "[none]" : type, 4737c478bd9Sstevel@tonic-gate subtype == NULL ? "[none]" : subtype); 4747c478bd9Sstevel@tonic-gate } 4757c478bd9Sstevel@tonic-gate if (cte != NULL && sm_strcasecmp(cte, "binary") == 0) 4767c478bd9Sstevel@tonic-gate sectionsize = sectionhighbits; 4777c478bd9Sstevel@tonic-gate linelen = 0; 4787c478bd9Sstevel@tonic-gate bp = buf; 4797c478bd9Sstevel@tonic-gate if (sectionhighbits == 0) 4807c478bd9Sstevel@tonic-gate { 4817c478bd9Sstevel@tonic-gate /* no encoding necessary */ 4827c478bd9Sstevel@tonic-gate if (cte != NULL && 4837c478bd9Sstevel@tonic-gate bitset(MCIF_CVT8TO7|MCIF_CVT7TO8|MCIF_INMIME, 4847c478bd9Sstevel@tonic-gate mci->mci_flags) && 4857c478bd9Sstevel@tonic-gate !bitset(M87F_NO8TO7, flags)) 4867c478bd9Sstevel@tonic-gate { 4877c478bd9Sstevel@tonic-gate /* 4887c478bd9Sstevel@tonic-gate ** Skip _unless_ in MIME mode and potentially 4897c478bd9Sstevel@tonic-gate ** converting from 8 bit to 7 bit MIME. See 4907c478bd9Sstevel@tonic-gate ** putheader() for the counterpart where the 4917c478bd9Sstevel@tonic-gate ** CTE header is skipped in the opposite 4927c478bd9Sstevel@tonic-gate ** situation. 4937c478bd9Sstevel@tonic-gate */ 4947c478bd9Sstevel@tonic-gate 4957c478bd9Sstevel@tonic-gate (void) sm_snprintf(buf, sizeof buf, 4967c478bd9Sstevel@tonic-gate "Content-Transfer-Encoding: %.200s", cte); 497*445f2479Sjbeck if (!putline(buf, mci)) 498*445f2479Sjbeck goto writeerr; 4997c478bd9Sstevel@tonic-gate if (tTd(43, 36)) 5007c478bd9Sstevel@tonic-gate sm_dprintf(" ...%s\n", buf); 5017c478bd9Sstevel@tonic-gate } 502*445f2479Sjbeck if (!putline("", mci)) 503*445f2479Sjbeck goto writeerr; 5047c478bd9Sstevel@tonic-gate mci->mci_flags &= ~MCIF_INHEADER; 5057c478bd9Sstevel@tonic-gate while (sm_io_fgets(e->e_dfp, SM_TIME_DEFAULT, buf, sizeof buf) 5067c478bd9Sstevel@tonic-gate != NULL) 5077c478bd9Sstevel@tonic-gate { 5087c478bd9Sstevel@tonic-gate bt = mimeboundary(buf, boundaries); 5097c478bd9Sstevel@tonic-gate if (bt != MBT_NOTSEP) 5107c478bd9Sstevel@tonic-gate break; 511*445f2479Sjbeck if (!putline(buf, mci)) 512*445f2479Sjbeck goto writeerr; 5137c478bd9Sstevel@tonic-gate } 5147c478bd9Sstevel@tonic-gate if (sm_io_eof(e->e_dfp)) 5157c478bd9Sstevel@tonic-gate bt = MBT_FINAL; 5167c478bd9Sstevel@tonic-gate } 5177c478bd9Sstevel@tonic-gate else if (!MapNLtoCRLF || 5187c478bd9Sstevel@tonic-gate (sectionsize / 8 < sectionhighbits && !use_qp)) 5197c478bd9Sstevel@tonic-gate { 5207c478bd9Sstevel@tonic-gate /* use base64 encoding */ 5217c478bd9Sstevel@tonic-gate int c1, c2; 5227c478bd9Sstevel@tonic-gate 5237c478bd9Sstevel@tonic-gate if (tTd(43, 36)) 5247c478bd9Sstevel@tonic-gate sm_dprintf(" ...Content-Transfer-Encoding: base64\n"); 525*445f2479Sjbeck if (!putline("Content-Transfer-Encoding: base64", mci)) 526*445f2479Sjbeck goto writeerr; 5277c478bd9Sstevel@tonic-gate (void) sm_snprintf(buf, sizeof buf, 5287c478bd9Sstevel@tonic-gate "X-MIME-Autoconverted: from 8bit to base64 by %s id %s", 5297c478bd9Sstevel@tonic-gate MyHostName, e->e_id); 530*445f2479Sjbeck if (!putline(buf, mci) || !putline("", mci)) 531*445f2479Sjbeck goto writeerr; 5327c478bd9Sstevel@tonic-gate mci->mci_flags &= ~MCIF_INHEADER; 5337c478bd9Sstevel@tonic-gate while ((c1 = mime_getchar_crlf(e->e_dfp, boundaries, &bt)) != 5347c478bd9Sstevel@tonic-gate SM_IO_EOF) 5357c478bd9Sstevel@tonic-gate { 5367c478bd9Sstevel@tonic-gate if (linelen > 71) 5377c478bd9Sstevel@tonic-gate { 5387c478bd9Sstevel@tonic-gate *bp = '\0'; 539*445f2479Sjbeck if (!putline(buf, mci)) 540*445f2479Sjbeck goto writeerr; 5417c478bd9Sstevel@tonic-gate linelen = 0; 5427c478bd9Sstevel@tonic-gate bp = buf; 5437c478bd9Sstevel@tonic-gate } 5447c478bd9Sstevel@tonic-gate linelen += 4; 5457c478bd9Sstevel@tonic-gate *bp++ = Base64Code[(c1 >> 2)]; 5467c478bd9Sstevel@tonic-gate c1 = (c1 & 0x03) << 4; 5477c478bd9Sstevel@tonic-gate c2 = mime_getchar_crlf(e->e_dfp, boundaries, &bt); 5487c478bd9Sstevel@tonic-gate if (c2 == SM_IO_EOF) 5497c478bd9Sstevel@tonic-gate { 5507c478bd9Sstevel@tonic-gate *bp++ = Base64Code[c1]; 5517c478bd9Sstevel@tonic-gate *bp++ = '='; 5527c478bd9Sstevel@tonic-gate *bp++ = '='; 5537c478bd9Sstevel@tonic-gate break; 5547c478bd9Sstevel@tonic-gate } 5557c478bd9Sstevel@tonic-gate c1 |= (c2 >> 4) & 0x0f; 5567c478bd9Sstevel@tonic-gate *bp++ = Base64Code[c1]; 5577c478bd9Sstevel@tonic-gate c1 = (c2 & 0x0f) << 2; 5587c478bd9Sstevel@tonic-gate c2 = mime_getchar_crlf(e->e_dfp, boundaries, &bt); 5597c478bd9Sstevel@tonic-gate if (c2 == SM_IO_EOF) 5607c478bd9Sstevel@tonic-gate { 5617c478bd9Sstevel@tonic-gate *bp++ = Base64Code[c1]; 5627c478bd9Sstevel@tonic-gate *bp++ = '='; 5637c478bd9Sstevel@tonic-gate break; 5647c478bd9Sstevel@tonic-gate } 5657c478bd9Sstevel@tonic-gate c1 |= (c2 >> 6) & 0x03; 5667c478bd9Sstevel@tonic-gate *bp++ = Base64Code[c1]; 5677c478bd9Sstevel@tonic-gate *bp++ = Base64Code[c2 & 0x3f]; 5687c478bd9Sstevel@tonic-gate } 5697c478bd9Sstevel@tonic-gate *bp = '\0'; 570*445f2479Sjbeck if (!putline(buf, mci)) 571*445f2479Sjbeck goto writeerr; 5727c478bd9Sstevel@tonic-gate } 5737c478bd9Sstevel@tonic-gate else 5747c478bd9Sstevel@tonic-gate { 5757c478bd9Sstevel@tonic-gate /* use quoted-printable encoding */ 5767c478bd9Sstevel@tonic-gate int c1, c2; 5777c478bd9Sstevel@tonic-gate int fromstate; 5787c478bd9Sstevel@tonic-gate BITMAP256 badchars; 5797c478bd9Sstevel@tonic-gate 5807c478bd9Sstevel@tonic-gate /* set up map of characters that must be mapped */ 5817c478bd9Sstevel@tonic-gate clrbitmap(badchars); 5827c478bd9Sstevel@tonic-gate for (c1 = 0x00; c1 < 0x20; c1++) 5837c478bd9Sstevel@tonic-gate setbitn(c1, badchars); 5847c478bd9Sstevel@tonic-gate clrbitn('\t', badchars); 5857c478bd9Sstevel@tonic-gate for (c1 = 0x7f; c1 < 0x100; c1++) 5867c478bd9Sstevel@tonic-gate setbitn(c1, badchars); 5877c478bd9Sstevel@tonic-gate setbitn('=', badchars); 5887c478bd9Sstevel@tonic-gate if (bitnset(M_EBCDIC, mci->mci_mailer->m_flags)) 5897c478bd9Sstevel@tonic-gate for (p = "!\"#$@[\\]^`{|}~"; *p != '\0'; p++) 5907c478bd9Sstevel@tonic-gate setbitn(*p, badchars); 5917c478bd9Sstevel@tonic-gate 5927c478bd9Sstevel@tonic-gate if (tTd(43, 36)) 5937c478bd9Sstevel@tonic-gate sm_dprintf(" ...Content-Transfer-Encoding: quoted-printable\n"); 594*445f2479Sjbeck if (!putline("Content-Transfer-Encoding: quoted-printable", 595*445f2479Sjbeck mci)) 596*445f2479Sjbeck goto writeerr; 5977c478bd9Sstevel@tonic-gate (void) sm_snprintf(buf, sizeof buf, 5987c478bd9Sstevel@tonic-gate "X-MIME-Autoconverted: from 8bit to quoted-printable by %s id %s", 5997c478bd9Sstevel@tonic-gate MyHostName, e->e_id); 600*445f2479Sjbeck if (!putline(buf, mci) || !putline("", mci)) 601*445f2479Sjbeck goto writeerr; 6027c478bd9Sstevel@tonic-gate mci->mci_flags &= ~MCIF_INHEADER; 6037c478bd9Sstevel@tonic-gate fromstate = 0; 6047c478bd9Sstevel@tonic-gate c2 = '\n'; 6057c478bd9Sstevel@tonic-gate while ((c1 = mime_getchar(e->e_dfp, boundaries, &bt)) != 6067c478bd9Sstevel@tonic-gate SM_IO_EOF) 6077c478bd9Sstevel@tonic-gate { 6087c478bd9Sstevel@tonic-gate if (c1 == '\n') 6097c478bd9Sstevel@tonic-gate { 6107c478bd9Sstevel@tonic-gate if (c2 == ' ' || c2 == '\t') 6117c478bd9Sstevel@tonic-gate { 6127c478bd9Sstevel@tonic-gate *bp++ = '='; 6137c478bd9Sstevel@tonic-gate *bp++ = Base16Code[(c2 >> 4) & 0x0f]; 6147c478bd9Sstevel@tonic-gate *bp++ = Base16Code[c2 & 0x0f]; 6157c478bd9Sstevel@tonic-gate } 6167c478bd9Sstevel@tonic-gate if (buf[0] == '.' && bp == &buf[1]) 6177c478bd9Sstevel@tonic-gate { 6187c478bd9Sstevel@tonic-gate buf[0] = '='; 6197c478bd9Sstevel@tonic-gate *bp++ = Base16Code[('.' >> 4) & 0x0f]; 6207c478bd9Sstevel@tonic-gate *bp++ = Base16Code['.' & 0x0f]; 6217c478bd9Sstevel@tonic-gate } 6227c478bd9Sstevel@tonic-gate *bp = '\0'; 623*445f2479Sjbeck if (!putline(buf, mci)) 624*445f2479Sjbeck goto writeerr; 6257c478bd9Sstevel@tonic-gate linelen = fromstate = 0; 6267c478bd9Sstevel@tonic-gate bp = buf; 6277c478bd9Sstevel@tonic-gate c2 = c1; 6287c478bd9Sstevel@tonic-gate continue; 6297c478bd9Sstevel@tonic-gate } 6307c478bd9Sstevel@tonic-gate if (c2 == ' ' && linelen == 4 && fromstate == 4 && 6317c478bd9Sstevel@tonic-gate bitnset(M_ESCFROM, mci->mci_mailer->m_flags)) 6327c478bd9Sstevel@tonic-gate { 6337c478bd9Sstevel@tonic-gate *bp++ = '='; 6347c478bd9Sstevel@tonic-gate *bp++ = '2'; 6357c478bd9Sstevel@tonic-gate *bp++ = '0'; 6367c478bd9Sstevel@tonic-gate linelen += 3; 6377c478bd9Sstevel@tonic-gate } 6387c478bd9Sstevel@tonic-gate else if (c2 == ' ' || c2 == '\t') 6397c478bd9Sstevel@tonic-gate { 6407c478bd9Sstevel@tonic-gate *bp++ = c2; 6417c478bd9Sstevel@tonic-gate linelen++; 6427c478bd9Sstevel@tonic-gate } 6437c478bd9Sstevel@tonic-gate if (linelen > 72 && 6447c478bd9Sstevel@tonic-gate (linelen > 75 || c1 != '.' || 6457c478bd9Sstevel@tonic-gate (linelen > 73 && c2 == '.'))) 6467c478bd9Sstevel@tonic-gate { 6477c478bd9Sstevel@tonic-gate if (linelen > 73 && c2 == '.') 6487c478bd9Sstevel@tonic-gate bp--; 6497c478bd9Sstevel@tonic-gate else 6507c478bd9Sstevel@tonic-gate c2 = '\n'; 6517c478bd9Sstevel@tonic-gate *bp++ = '='; 6527c478bd9Sstevel@tonic-gate *bp = '\0'; 653*445f2479Sjbeck if (!putline(buf, mci)) 654*445f2479Sjbeck goto writeerr; 6557c478bd9Sstevel@tonic-gate linelen = fromstate = 0; 6567c478bd9Sstevel@tonic-gate bp = buf; 6577c478bd9Sstevel@tonic-gate if (c2 == '.') 6587c478bd9Sstevel@tonic-gate { 6597c478bd9Sstevel@tonic-gate *bp++ = '.'; 6607c478bd9Sstevel@tonic-gate linelen++; 6617c478bd9Sstevel@tonic-gate } 6627c478bd9Sstevel@tonic-gate } 6637c478bd9Sstevel@tonic-gate if (bitnset(bitidx(c1), badchars)) 6647c478bd9Sstevel@tonic-gate { 6657c478bd9Sstevel@tonic-gate *bp++ = '='; 6667c478bd9Sstevel@tonic-gate *bp++ = Base16Code[(c1 >> 4) & 0x0f]; 6677c478bd9Sstevel@tonic-gate *bp++ = Base16Code[c1 & 0x0f]; 6687c478bd9Sstevel@tonic-gate linelen += 3; 6697c478bd9Sstevel@tonic-gate } 6707c478bd9Sstevel@tonic-gate else if (c1 != ' ' && c1 != '\t') 6717c478bd9Sstevel@tonic-gate { 6727c478bd9Sstevel@tonic-gate if (linelen < 4 && c1 == "From"[linelen]) 6737c478bd9Sstevel@tonic-gate fromstate++; 6747c478bd9Sstevel@tonic-gate *bp++ = c1; 6757c478bd9Sstevel@tonic-gate linelen++; 6767c478bd9Sstevel@tonic-gate } 6777c478bd9Sstevel@tonic-gate c2 = c1; 6787c478bd9Sstevel@tonic-gate } 6797c478bd9Sstevel@tonic-gate 6807c478bd9Sstevel@tonic-gate /* output any saved character */ 6817c478bd9Sstevel@tonic-gate if (c2 == ' ' || c2 == '\t') 6827c478bd9Sstevel@tonic-gate { 6837c478bd9Sstevel@tonic-gate *bp++ = '='; 6847c478bd9Sstevel@tonic-gate *bp++ = Base16Code[(c2 >> 4) & 0x0f]; 6857c478bd9Sstevel@tonic-gate *bp++ = Base16Code[c2 & 0x0f]; 6867c478bd9Sstevel@tonic-gate linelen += 3; 6877c478bd9Sstevel@tonic-gate } 6887c478bd9Sstevel@tonic-gate 6897c478bd9Sstevel@tonic-gate if (linelen > 0 || boundaries[0] != NULL) 6907c478bd9Sstevel@tonic-gate { 6917c478bd9Sstevel@tonic-gate *bp = '\0'; 692*445f2479Sjbeck if (!putline(buf, mci)) 693*445f2479Sjbeck goto writeerr; 6947c478bd9Sstevel@tonic-gate } 6957c478bd9Sstevel@tonic-gate 6967c478bd9Sstevel@tonic-gate } 6977c478bd9Sstevel@tonic-gate if (tTd(43, 3)) 6987c478bd9Sstevel@tonic-gate sm_dprintf("\t\t\tmime8to7=>%s (basic)\n", MimeBoundaryNames[bt]); 6997c478bd9Sstevel@tonic-gate return bt; 700*445f2479Sjbeck 701*445f2479Sjbeck writeerr: 702*445f2479Sjbeck return SM_IO_EOF; 7037c478bd9Sstevel@tonic-gate } 7047c478bd9Sstevel@tonic-gate /* 7057c478bd9Sstevel@tonic-gate ** MIME_GETCHAR -- get a character for MIME processing 7067c478bd9Sstevel@tonic-gate ** 7077c478bd9Sstevel@tonic-gate ** Treats boundaries as SM_IO_EOF. 7087c478bd9Sstevel@tonic-gate ** 7097c478bd9Sstevel@tonic-gate ** Parameters: 7107c478bd9Sstevel@tonic-gate ** fp -- the input file. 7117c478bd9Sstevel@tonic-gate ** boundaries -- the current MIME boundaries. 7127c478bd9Sstevel@tonic-gate ** btp -- if the return value is SM_IO_EOF, *btp is set to 7137c478bd9Sstevel@tonic-gate ** the type of the boundary. 7147c478bd9Sstevel@tonic-gate ** 7157c478bd9Sstevel@tonic-gate ** Returns: 7167c478bd9Sstevel@tonic-gate ** The next character in the input stream. 7177c478bd9Sstevel@tonic-gate */ 7187c478bd9Sstevel@tonic-gate 7197c478bd9Sstevel@tonic-gate static int 7207c478bd9Sstevel@tonic-gate mime_getchar(fp, boundaries, btp) 7217c478bd9Sstevel@tonic-gate register SM_FILE_T *fp; 7227c478bd9Sstevel@tonic-gate char **boundaries; 7237c478bd9Sstevel@tonic-gate int *btp; 7247c478bd9Sstevel@tonic-gate { 7257c478bd9Sstevel@tonic-gate int c; 7267c478bd9Sstevel@tonic-gate static unsigned char *bp = NULL; 7277c478bd9Sstevel@tonic-gate static int buflen = 0; 7287c478bd9Sstevel@tonic-gate static bool atbol = true; /* at beginning of line */ 7297c478bd9Sstevel@tonic-gate static int bt = MBT_SYNTAX; /* boundary type of next SM_IO_EOF */ 7307c478bd9Sstevel@tonic-gate static unsigned char buf[128]; /* need not be a full line */ 7317c478bd9Sstevel@tonic-gate int start = 0; /* indicates position of - in buffer */ 7327c478bd9Sstevel@tonic-gate 7337c478bd9Sstevel@tonic-gate if (buflen == 1 && *bp == '\n') 7347c478bd9Sstevel@tonic-gate { 7357c478bd9Sstevel@tonic-gate /* last \n in buffer may be part of next MIME boundary */ 7367c478bd9Sstevel@tonic-gate c = *bp; 7377c478bd9Sstevel@tonic-gate } 7387c478bd9Sstevel@tonic-gate else if (buflen > 0) 7397c478bd9Sstevel@tonic-gate { 7407c478bd9Sstevel@tonic-gate buflen--; 7417c478bd9Sstevel@tonic-gate return *bp++; 7427c478bd9Sstevel@tonic-gate } 7437c478bd9Sstevel@tonic-gate else 7447c478bd9Sstevel@tonic-gate c = sm_io_getc(fp, SM_TIME_DEFAULT); 7457c478bd9Sstevel@tonic-gate bp = buf; 7467c478bd9Sstevel@tonic-gate buflen = 0; 7477c478bd9Sstevel@tonic-gate if (c == '\n') 7487c478bd9Sstevel@tonic-gate { 7497c478bd9Sstevel@tonic-gate /* might be part of a MIME boundary */ 7507c478bd9Sstevel@tonic-gate *bp++ = c; 7517c478bd9Sstevel@tonic-gate atbol = true; 7527c478bd9Sstevel@tonic-gate c = sm_io_getc(fp, SM_TIME_DEFAULT); 7537c478bd9Sstevel@tonic-gate if (c == '\n') 7547c478bd9Sstevel@tonic-gate { 7557c478bd9Sstevel@tonic-gate (void) sm_io_ungetc(fp, SM_TIME_DEFAULT, c); 7567c478bd9Sstevel@tonic-gate return c; 7577c478bd9Sstevel@tonic-gate } 7587c478bd9Sstevel@tonic-gate start = 1; 7597c478bd9Sstevel@tonic-gate } 7607c478bd9Sstevel@tonic-gate if (c != SM_IO_EOF) 7617c478bd9Sstevel@tonic-gate *bp++ = c; 7627c478bd9Sstevel@tonic-gate else 7637c478bd9Sstevel@tonic-gate bt = MBT_FINAL; 7647c478bd9Sstevel@tonic-gate if (atbol && c == '-') 7657c478bd9Sstevel@tonic-gate { 7667c478bd9Sstevel@tonic-gate /* check for a message boundary */ 7677c478bd9Sstevel@tonic-gate c = sm_io_getc(fp, SM_TIME_DEFAULT); 7687c478bd9Sstevel@tonic-gate if (c != '-') 7697c478bd9Sstevel@tonic-gate { 7707c478bd9Sstevel@tonic-gate if (c != SM_IO_EOF) 7717c478bd9Sstevel@tonic-gate *bp++ = c; 7727c478bd9Sstevel@tonic-gate else 7737c478bd9Sstevel@tonic-gate bt = MBT_FINAL; 7747c478bd9Sstevel@tonic-gate buflen = bp - buf - 1; 7757c478bd9Sstevel@tonic-gate bp = buf; 7767c478bd9Sstevel@tonic-gate return *bp++; 7777c478bd9Sstevel@tonic-gate } 7787c478bd9Sstevel@tonic-gate 7797c478bd9Sstevel@tonic-gate /* got "--", now check for rest of separator */ 7807c478bd9Sstevel@tonic-gate *bp++ = '-'; 7817c478bd9Sstevel@tonic-gate while (bp < &buf[sizeof buf - 2] && 7827c478bd9Sstevel@tonic-gate (c = sm_io_getc(fp, SM_TIME_DEFAULT)) != SM_IO_EOF && 7837c478bd9Sstevel@tonic-gate c != '\n') 7847c478bd9Sstevel@tonic-gate { 7857c478bd9Sstevel@tonic-gate *bp++ = c; 7867c478bd9Sstevel@tonic-gate } 7877c478bd9Sstevel@tonic-gate *bp = '\0'; /* XXX simply cut off? */ 7887c478bd9Sstevel@tonic-gate bt = mimeboundary((char *) &buf[start], boundaries); 7897c478bd9Sstevel@tonic-gate switch (bt) 7907c478bd9Sstevel@tonic-gate { 7917c478bd9Sstevel@tonic-gate case MBT_FINAL: 7927c478bd9Sstevel@tonic-gate case MBT_INTERMED: 7937c478bd9Sstevel@tonic-gate /* we have a message boundary */ 7947c478bd9Sstevel@tonic-gate buflen = 0; 7957c478bd9Sstevel@tonic-gate *btp = bt; 7967c478bd9Sstevel@tonic-gate return SM_IO_EOF; 7977c478bd9Sstevel@tonic-gate } 7987c478bd9Sstevel@tonic-gate 7997c478bd9Sstevel@tonic-gate if (bp < &buf[sizeof buf - 2] && c != SM_IO_EOF) 8007c478bd9Sstevel@tonic-gate *bp++ = c; 8017c478bd9Sstevel@tonic-gate } 8027c478bd9Sstevel@tonic-gate 8037c478bd9Sstevel@tonic-gate atbol = c == '\n'; 8047c478bd9Sstevel@tonic-gate buflen = bp - buf - 1; 8057c478bd9Sstevel@tonic-gate if (buflen < 0) 8067c478bd9Sstevel@tonic-gate { 8077c478bd9Sstevel@tonic-gate *btp = bt; 8087c478bd9Sstevel@tonic-gate return SM_IO_EOF; 8097c478bd9Sstevel@tonic-gate } 8107c478bd9Sstevel@tonic-gate bp = buf; 8117c478bd9Sstevel@tonic-gate return *bp++; 8127c478bd9Sstevel@tonic-gate } 8137c478bd9Sstevel@tonic-gate /* 8147c478bd9Sstevel@tonic-gate ** MIME_GETCHAR_CRLF -- do mime_getchar, but translate NL => CRLF 8157c478bd9Sstevel@tonic-gate ** 8167c478bd9Sstevel@tonic-gate ** Parameters: 8177c478bd9Sstevel@tonic-gate ** fp -- the input file. 8187c478bd9Sstevel@tonic-gate ** boundaries -- the current MIME boundaries. 8197c478bd9Sstevel@tonic-gate ** btp -- if the return value is SM_IO_EOF, *btp is set to 8207c478bd9Sstevel@tonic-gate ** the type of the boundary. 8217c478bd9Sstevel@tonic-gate ** 8227c478bd9Sstevel@tonic-gate ** Returns: 8237c478bd9Sstevel@tonic-gate ** The next character in the input stream. 8247c478bd9Sstevel@tonic-gate */ 8257c478bd9Sstevel@tonic-gate 8267c478bd9Sstevel@tonic-gate static int 8277c478bd9Sstevel@tonic-gate mime_getchar_crlf(fp, boundaries, btp) 8287c478bd9Sstevel@tonic-gate register SM_FILE_T *fp; 8297c478bd9Sstevel@tonic-gate char **boundaries; 8307c478bd9Sstevel@tonic-gate int *btp; 8317c478bd9Sstevel@tonic-gate { 8327c478bd9Sstevel@tonic-gate static bool sendlf = false; 8337c478bd9Sstevel@tonic-gate int c; 8347c478bd9Sstevel@tonic-gate 8357c478bd9Sstevel@tonic-gate if (sendlf) 8367c478bd9Sstevel@tonic-gate { 8377c478bd9Sstevel@tonic-gate sendlf = false; 8387c478bd9Sstevel@tonic-gate return '\n'; 8397c478bd9Sstevel@tonic-gate } 8407c478bd9Sstevel@tonic-gate c = mime_getchar(fp, boundaries, btp); 8417c478bd9Sstevel@tonic-gate if (c == '\n' && MapNLtoCRLF) 8427c478bd9Sstevel@tonic-gate { 8437c478bd9Sstevel@tonic-gate sendlf = true; 8447c478bd9Sstevel@tonic-gate return '\r'; 8457c478bd9Sstevel@tonic-gate } 8467c478bd9Sstevel@tonic-gate return c; 8477c478bd9Sstevel@tonic-gate } 8487c478bd9Sstevel@tonic-gate /* 8497c478bd9Sstevel@tonic-gate ** MIMEBOUNDARY -- determine if this line is a MIME boundary & its type 8507c478bd9Sstevel@tonic-gate ** 8517c478bd9Sstevel@tonic-gate ** Parameters: 8527c478bd9Sstevel@tonic-gate ** line -- the input line. 8537c478bd9Sstevel@tonic-gate ** boundaries -- the set of currently pending boundaries. 8547c478bd9Sstevel@tonic-gate ** 8557c478bd9Sstevel@tonic-gate ** Returns: 8567c478bd9Sstevel@tonic-gate ** MBT_NOTSEP -- if this is not a separator line 8577c478bd9Sstevel@tonic-gate ** MBT_INTERMED -- if this is an intermediate separator 8587c478bd9Sstevel@tonic-gate ** MBT_FINAL -- if this is a final boundary 8597c478bd9Sstevel@tonic-gate ** MBT_SYNTAX -- if this is a boundary for the wrong 8607c478bd9Sstevel@tonic-gate ** enclosure -- i.e., a syntax error. 8617c478bd9Sstevel@tonic-gate */ 8627c478bd9Sstevel@tonic-gate 8637c478bd9Sstevel@tonic-gate static int 8647c478bd9Sstevel@tonic-gate mimeboundary(line, boundaries) 8657c478bd9Sstevel@tonic-gate register char *line; 8667c478bd9Sstevel@tonic-gate char **boundaries; 8677c478bd9Sstevel@tonic-gate { 8687c478bd9Sstevel@tonic-gate int type = MBT_NOTSEP; 8697c478bd9Sstevel@tonic-gate int i; 8707c478bd9Sstevel@tonic-gate int savec; 8717c478bd9Sstevel@tonic-gate 8727c478bd9Sstevel@tonic-gate if (line[0] != '-' || line[1] != '-' || boundaries == NULL) 8737c478bd9Sstevel@tonic-gate return MBT_NOTSEP; 8747c478bd9Sstevel@tonic-gate i = strlen(line); 8757c478bd9Sstevel@tonic-gate if (i > 0 && line[i - 1] == '\n') 8767c478bd9Sstevel@tonic-gate i--; 8777c478bd9Sstevel@tonic-gate 8787c478bd9Sstevel@tonic-gate /* strip off trailing whitespace */ 8797c478bd9Sstevel@tonic-gate while (i > 0 && (line[i - 1] == ' ' || line[i - 1] == '\t' 8807c478bd9Sstevel@tonic-gate #if _FFR_MIME_CR_OK 8817c478bd9Sstevel@tonic-gate || line[i - 1] == '\r' 8827c478bd9Sstevel@tonic-gate #endif /* _FFR_MIME_CR_OK */ 8837c478bd9Sstevel@tonic-gate )) 8847c478bd9Sstevel@tonic-gate i--; 8857c478bd9Sstevel@tonic-gate savec = line[i]; 8867c478bd9Sstevel@tonic-gate line[i] = '\0'; 8877c478bd9Sstevel@tonic-gate 8887c478bd9Sstevel@tonic-gate if (tTd(43, 5)) 8897c478bd9Sstevel@tonic-gate sm_dprintf("mimeboundary: line=\"%s\"... ", line); 8907c478bd9Sstevel@tonic-gate 8917c478bd9Sstevel@tonic-gate /* check for this as an intermediate boundary */ 8927c478bd9Sstevel@tonic-gate if (isboundary(&line[2], boundaries) >= 0) 8937c478bd9Sstevel@tonic-gate type = MBT_INTERMED; 8947c478bd9Sstevel@tonic-gate else if (i > 2 && strncmp(&line[i - 2], "--", 2) == 0) 8957c478bd9Sstevel@tonic-gate { 8967c478bd9Sstevel@tonic-gate /* check for a final boundary */ 8977c478bd9Sstevel@tonic-gate line[i - 2] = '\0'; 8987c478bd9Sstevel@tonic-gate if (isboundary(&line[2], boundaries) >= 0) 8997c478bd9Sstevel@tonic-gate type = MBT_FINAL; 9007c478bd9Sstevel@tonic-gate line[i - 2] = '-'; 9017c478bd9Sstevel@tonic-gate } 9027c478bd9Sstevel@tonic-gate 9037c478bd9Sstevel@tonic-gate line[i] = savec; 9047c478bd9Sstevel@tonic-gate if (tTd(43, 5)) 9057c478bd9Sstevel@tonic-gate sm_dprintf("%s\n", MimeBoundaryNames[type]); 9067c478bd9Sstevel@tonic-gate return type; 9077c478bd9Sstevel@tonic-gate } 9087c478bd9Sstevel@tonic-gate /* 9097c478bd9Sstevel@tonic-gate ** DEFCHARSET -- return default character set for message 9107c478bd9Sstevel@tonic-gate ** 9117c478bd9Sstevel@tonic-gate ** The first choice for character set is for the mailer 9127c478bd9Sstevel@tonic-gate ** corresponding to the envelope sender. If neither that 9137c478bd9Sstevel@tonic-gate ** nor the global configuration file has a default character 9147c478bd9Sstevel@tonic-gate ** set defined, return "unknown-8bit" as recommended by 9157c478bd9Sstevel@tonic-gate ** RFC 1428 section 3. 9167c478bd9Sstevel@tonic-gate ** 9177c478bd9Sstevel@tonic-gate ** Parameters: 9187c478bd9Sstevel@tonic-gate ** e -- the envelope for this message. 9197c478bd9Sstevel@tonic-gate ** 9207c478bd9Sstevel@tonic-gate ** Returns: 9217c478bd9Sstevel@tonic-gate ** The default character set for that mailer. 9227c478bd9Sstevel@tonic-gate */ 9237c478bd9Sstevel@tonic-gate 9247c478bd9Sstevel@tonic-gate char * 9257c478bd9Sstevel@tonic-gate defcharset(e) 9267c478bd9Sstevel@tonic-gate register ENVELOPE *e; 9277c478bd9Sstevel@tonic-gate { 9287c478bd9Sstevel@tonic-gate if (e != NULL && e->e_from.q_mailer != NULL && 9297c478bd9Sstevel@tonic-gate e->e_from.q_mailer->m_defcharset != NULL) 9307c478bd9Sstevel@tonic-gate return e->e_from.q_mailer->m_defcharset; 9317c478bd9Sstevel@tonic-gate if (DefaultCharSet != NULL) 9327c478bd9Sstevel@tonic-gate return DefaultCharSet; 9337c478bd9Sstevel@tonic-gate return "unknown-8bit"; 9347c478bd9Sstevel@tonic-gate } 9357c478bd9Sstevel@tonic-gate /* 9367c478bd9Sstevel@tonic-gate ** ISBOUNDARY -- is a given string a currently valid boundary? 9377c478bd9Sstevel@tonic-gate ** 9387c478bd9Sstevel@tonic-gate ** Parameters: 9397c478bd9Sstevel@tonic-gate ** line -- the current input line. 9407c478bd9Sstevel@tonic-gate ** boundaries -- the list of valid boundaries. 9417c478bd9Sstevel@tonic-gate ** 9427c478bd9Sstevel@tonic-gate ** Returns: 9437c478bd9Sstevel@tonic-gate ** The index number in boundaries if the line is found. 9447c478bd9Sstevel@tonic-gate ** -1 -- otherwise. 9457c478bd9Sstevel@tonic-gate ** 9467c478bd9Sstevel@tonic-gate */ 9477c478bd9Sstevel@tonic-gate 9487c478bd9Sstevel@tonic-gate static int 9497c478bd9Sstevel@tonic-gate isboundary(line, boundaries) 9507c478bd9Sstevel@tonic-gate char *line; 9517c478bd9Sstevel@tonic-gate char **boundaries; 9527c478bd9Sstevel@tonic-gate { 9537c478bd9Sstevel@tonic-gate register int i; 9547c478bd9Sstevel@tonic-gate 9557c478bd9Sstevel@tonic-gate for (i = 0; i <= MAXMIMENESTING && boundaries[i] != NULL; i++) 9567c478bd9Sstevel@tonic-gate { 9577c478bd9Sstevel@tonic-gate if (strcmp(line, boundaries[i]) == 0) 9587c478bd9Sstevel@tonic-gate return i; 9597c478bd9Sstevel@tonic-gate } 9607c478bd9Sstevel@tonic-gate return -1; 9617c478bd9Sstevel@tonic-gate } 9627c478bd9Sstevel@tonic-gate #endif /* MIME8TO7 */ 9637c478bd9Sstevel@tonic-gate 9647c478bd9Sstevel@tonic-gate #if MIME7TO8 9657c478bd9Sstevel@tonic-gate static int mime_fromqp __P((unsigned char *, unsigned char **, int)); 9667c478bd9Sstevel@tonic-gate 9677c478bd9Sstevel@tonic-gate /* 9687c478bd9Sstevel@tonic-gate ** MIME7TO8 -- output 7 bit encoded MIME body in 8 bit format 9697c478bd9Sstevel@tonic-gate ** 9707c478bd9Sstevel@tonic-gate ** This is a hack. Supports translating the two 7-bit body-encodings 9717c478bd9Sstevel@tonic-gate ** (quoted-printable and base64) to 8-bit coded bodies. 9727c478bd9Sstevel@tonic-gate ** 9737c478bd9Sstevel@tonic-gate ** There is not much point in supporting multipart here, as the UA 9747c478bd9Sstevel@tonic-gate ** will be able to deal with encoded MIME bodies if it can parse MIME 9757c478bd9Sstevel@tonic-gate ** multipart messages. 9767c478bd9Sstevel@tonic-gate ** 9777c478bd9Sstevel@tonic-gate ** Note also that we won't be called unless it is a text/plain MIME 9787c478bd9Sstevel@tonic-gate ** message, encoded base64 or QP and mailer flag '9' has been defined 9797c478bd9Sstevel@tonic-gate ** on mailer. 9807c478bd9Sstevel@tonic-gate ** 9817c478bd9Sstevel@tonic-gate ** Contributed by Marius Olaffson <marius@rhi.hi.is>. 9827c478bd9Sstevel@tonic-gate ** 9837c478bd9Sstevel@tonic-gate ** Parameters: 9847c478bd9Sstevel@tonic-gate ** mci -- mailer connection information. 9857c478bd9Sstevel@tonic-gate ** header -- the header for this body part. 9867c478bd9Sstevel@tonic-gate ** e -- envelope. 9877c478bd9Sstevel@tonic-gate ** 9887c478bd9Sstevel@tonic-gate ** Returns: 989*445f2479Sjbeck ** true iff body was written successfully 9907c478bd9Sstevel@tonic-gate */ 9917c478bd9Sstevel@tonic-gate 9927c478bd9Sstevel@tonic-gate static char index_64[128] = 9937c478bd9Sstevel@tonic-gate { 9947c478bd9Sstevel@tonic-gate -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, 9957c478bd9Sstevel@tonic-gate -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, 9967c478bd9Sstevel@tonic-gate -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,62, -1,-1,-1,63, 9977c478bd9Sstevel@tonic-gate 52,53,54,55, 56,57,58,59, 60,61,-1,-1, -1,-1,-1,-1, 9987c478bd9Sstevel@tonic-gate -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10, 11,12,13,14, 9997c478bd9Sstevel@tonic-gate 15,16,17,18, 19,20,21,22, 23,24,25,-1, -1,-1,-1,-1, 10007c478bd9Sstevel@tonic-gate -1,26,27,28, 29,30,31,32, 33,34,35,36, 37,38,39,40, 10017c478bd9Sstevel@tonic-gate 41,42,43,44, 45,46,47,48, 49,50,51,-1, -1,-1,-1,-1 10027c478bd9Sstevel@tonic-gate }; 10037c478bd9Sstevel@tonic-gate 10047c478bd9Sstevel@tonic-gate # define CHAR64(c) (((c) < 0 || (c) > 127) ? -1 : index_64[(c)]) 10057c478bd9Sstevel@tonic-gate 1006*445f2479Sjbeck bool 10077c478bd9Sstevel@tonic-gate mime7to8(mci, header, e) 10087c478bd9Sstevel@tonic-gate register MCI *mci; 10097c478bd9Sstevel@tonic-gate HDR *header; 10107c478bd9Sstevel@tonic-gate register ENVELOPE *e; 10117c478bd9Sstevel@tonic-gate { 10127c478bd9Sstevel@tonic-gate int pxflags; 10137c478bd9Sstevel@tonic-gate register char *p; 10147c478bd9Sstevel@tonic-gate char *cte; 10157c478bd9Sstevel@tonic-gate char **pvp; 10167c478bd9Sstevel@tonic-gate unsigned char *fbufp; 10177c478bd9Sstevel@tonic-gate char buf[MAXLINE]; 10187c478bd9Sstevel@tonic-gate unsigned char fbuf[MAXLINE + 1]; 10197c478bd9Sstevel@tonic-gate char pvpbuf[MAXLINE]; 10207c478bd9Sstevel@tonic-gate extern unsigned char MimeTokenTab[256]; 10217c478bd9Sstevel@tonic-gate 10227c478bd9Sstevel@tonic-gate p = hvalue("Content-Transfer-Encoding", header); 10237c478bd9Sstevel@tonic-gate if (p == NULL || 10247c478bd9Sstevel@tonic-gate (pvp = prescan(p, '\0', pvpbuf, sizeof pvpbuf, NULL, 10257c478bd9Sstevel@tonic-gate MimeTokenTab, false)) == NULL || 10267c478bd9Sstevel@tonic-gate pvp[0] == NULL) 10277c478bd9Sstevel@tonic-gate { 10287c478bd9Sstevel@tonic-gate /* "can't happen" -- upper level should have caught this */ 10297c478bd9Sstevel@tonic-gate syserr("mime7to8: unparsable CTE %s", p == NULL ? "<NULL>" : p); 10307c478bd9Sstevel@tonic-gate 10317c478bd9Sstevel@tonic-gate /* avoid bounce loops */ 10327c478bd9Sstevel@tonic-gate e->e_flags |= EF_DONT_MIME; 10337c478bd9Sstevel@tonic-gate 10347c478bd9Sstevel@tonic-gate /* cheap failsafe algorithm -- should work on text/plain */ 10357c478bd9Sstevel@tonic-gate if (p != NULL) 10367c478bd9Sstevel@tonic-gate { 10377c478bd9Sstevel@tonic-gate (void) sm_snprintf(buf, sizeof buf, 10387c478bd9Sstevel@tonic-gate "Content-Transfer-Encoding: %s", p); 1039*445f2479Sjbeck if (!putline(buf, mci)) 1040*445f2479Sjbeck goto writeerr; 10417c478bd9Sstevel@tonic-gate } 1042*445f2479Sjbeck if (!putline("", mci)) 1043*445f2479Sjbeck goto writeerr; 10447c478bd9Sstevel@tonic-gate mci->mci_flags &= ~MCIF_INHEADER; 10457c478bd9Sstevel@tonic-gate while (sm_io_fgets(e->e_dfp, SM_TIME_DEFAULT, buf, sizeof buf) 10467c478bd9Sstevel@tonic-gate != NULL) 1047*445f2479Sjbeck { 1048*445f2479Sjbeck if (!putline(buf, mci)) 1049*445f2479Sjbeck goto writeerr; 1050*445f2479Sjbeck } 1051*445f2479Sjbeck return true; 10527c478bd9Sstevel@tonic-gate } 10537c478bd9Sstevel@tonic-gate cataddr(pvp, NULL, buf, sizeof buf, '\0'); 10547c478bd9Sstevel@tonic-gate cte = sm_rpool_strdup_x(e->e_rpool, buf); 10557c478bd9Sstevel@tonic-gate 10567c478bd9Sstevel@tonic-gate mci->mci_flags |= MCIF_INHEADER; 1057*445f2479Sjbeck if (!putline("Content-Transfer-Encoding: 8bit", mci)) 1058*445f2479Sjbeck goto writeerr; 10597c478bd9Sstevel@tonic-gate (void) sm_snprintf(buf, sizeof buf, 10607c478bd9Sstevel@tonic-gate "X-MIME-Autoconverted: from %.200s to 8bit by %s id %s", 10617c478bd9Sstevel@tonic-gate cte, MyHostName, e->e_id); 1062*445f2479Sjbeck if (!putline(buf, mci) || !putline("", mci)) 1063*445f2479Sjbeck goto writeerr; 10647c478bd9Sstevel@tonic-gate mci->mci_flags &= ~MCIF_INHEADER; 10657c478bd9Sstevel@tonic-gate 10667c478bd9Sstevel@tonic-gate /* 10677c478bd9Sstevel@tonic-gate ** Translate body encoding to 8-bit. Supports two types of 10687c478bd9Sstevel@tonic-gate ** encodings; "base64" and "quoted-printable". Assume qp if 10697c478bd9Sstevel@tonic-gate ** it is not base64. 10707c478bd9Sstevel@tonic-gate */ 10717c478bd9Sstevel@tonic-gate 10727c478bd9Sstevel@tonic-gate pxflags = PXLF_MAPFROM; 10737c478bd9Sstevel@tonic-gate if (sm_strcasecmp(cte, "base64") == 0) 10747c478bd9Sstevel@tonic-gate { 10757c478bd9Sstevel@tonic-gate int c1, c2, c3, c4; 10767c478bd9Sstevel@tonic-gate 10777c478bd9Sstevel@tonic-gate fbufp = fbuf; 10787c478bd9Sstevel@tonic-gate while ((c1 = sm_io_getc(e->e_dfp, SM_TIME_DEFAULT)) != 10797c478bd9Sstevel@tonic-gate SM_IO_EOF) 10807c478bd9Sstevel@tonic-gate { 10817c478bd9Sstevel@tonic-gate if (isascii(c1) && isspace(c1)) 10827c478bd9Sstevel@tonic-gate continue; 10837c478bd9Sstevel@tonic-gate 10847c478bd9Sstevel@tonic-gate do 10857c478bd9Sstevel@tonic-gate { 10867c478bd9Sstevel@tonic-gate c2 = sm_io_getc(e->e_dfp, SM_TIME_DEFAULT); 10877c478bd9Sstevel@tonic-gate } while (isascii(c2) && isspace(c2)); 10887c478bd9Sstevel@tonic-gate if (c2 == SM_IO_EOF) 10897c478bd9Sstevel@tonic-gate break; 10907c478bd9Sstevel@tonic-gate 10917c478bd9Sstevel@tonic-gate do 10927c478bd9Sstevel@tonic-gate { 10937c478bd9Sstevel@tonic-gate c3 = sm_io_getc(e->e_dfp, SM_TIME_DEFAULT); 10947c478bd9Sstevel@tonic-gate } while (isascii(c3) && isspace(c3)); 10957c478bd9Sstevel@tonic-gate if (c3 == SM_IO_EOF) 10967c478bd9Sstevel@tonic-gate break; 10977c478bd9Sstevel@tonic-gate 10987c478bd9Sstevel@tonic-gate do 10997c478bd9Sstevel@tonic-gate { 11007c478bd9Sstevel@tonic-gate c4 = sm_io_getc(e->e_dfp, SM_TIME_DEFAULT); 11017c478bd9Sstevel@tonic-gate } while (isascii(c4) && isspace(c4)); 11027c478bd9Sstevel@tonic-gate if (c4 == SM_IO_EOF) 11037c478bd9Sstevel@tonic-gate break; 11047c478bd9Sstevel@tonic-gate 11057c478bd9Sstevel@tonic-gate if (c1 == '=' || c2 == '=') 11067c478bd9Sstevel@tonic-gate continue; 11077c478bd9Sstevel@tonic-gate c1 = CHAR64(c1); 11087c478bd9Sstevel@tonic-gate c2 = CHAR64(c2); 11097c478bd9Sstevel@tonic-gate 11107c478bd9Sstevel@tonic-gate #if MIME7TO8_OLD 11117c478bd9Sstevel@tonic-gate #define CHK_EOL if (*--fbufp != '\n' || (fbufp > fbuf && *--fbufp != '\r')) \ 11127c478bd9Sstevel@tonic-gate ++fbufp; 11137c478bd9Sstevel@tonic-gate #else /* MIME7TO8_OLD */ 11147c478bd9Sstevel@tonic-gate #define CHK_EOL if (*--fbufp != '\n' || (fbufp > fbuf && *--fbufp != '\r')) \ 11157c478bd9Sstevel@tonic-gate { \ 11167c478bd9Sstevel@tonic-gate ++fbufp; \ 11177c478bd9Sstevel@tonic-gate pxflags |= PXLF_NOADDEOL; \ 11187c478bd9Sstevel@tonic-gate } 11197c478bd9Sstevel@tonic-gate #endif /* MIME7TO8_OLD */ 11207c478bd9Sstevel@tonic-gate 11217c478bd9Sstevel@tonic-gate #define PUTLINE64 \ 11227c478bd9Sstevel@tonic-gate do \ 11237c478bd9Sstevel@tonic-gate { \ 11247c478bd9Sstevel@tonic-gate if (*fbufp++ == '\n' || fbufp >= &fbuf[MAXLINE]) \ 11257c478bd9Sstevel@tonic-gate { \ 11267c478bd9Sstevel@tonic-gate CHK_EOL; \ 1127*445f2479Sjbeck if (!putxline((char *) fbuf, fbufp - fbuf, mci, pxflags)) \ 1128*445f2479Sjbeck goto writeerr; \ 11297c478bd9Sstevel@tonic-gate pxflags &= ~PXLF_NOADDEOL; \ 11307c478bd9Sstevel@tonic-gate fbufp = fbuf; \ 11317c478bd9Sstevel@tonic-gate } \ 11327c478bd9Sstevel@tonic-gate } while (0) 11337c478bd9Sstevel@tonic-gate 11347c478bd9Sstevel@tonic-gate *fbufp = (c1 << 2) | ((c2 & 0x30) >> 4); 11357c478bd9Sstevel@tonic-gate PUTLINE64; 11367c478bd9Sstevel@tonic-gate if (c3 == '=') 11377c478bd9Sstevel@tonic-gate continue; 11387c478bd9Sstevel@tonic-gate c3 = CHAR64(c3); 11397c478bd9Sstevel@tonic-gate *fbufp = ((c2 & 0x0f) << 4) | ((c3 & 0x3c) >> 2); 11407c478bd9Sstevel@tonic-gate PUTLINE64; 11417c478bd9Sstevel@tonic-gate if (c4 == '=') 11427c478bd9Sstevel@tonic-gate continue; 11437c478bd9Sstevel@tonic-gate c4 = CHAR64(c4); 11447c478bd9Sstevel@tonic-gate *fbufp = ((c3 & 0x03) << 6) | c4; 11457c478bd9Sstevel@tonic-gate PUTLINE64; 11467c478bd9Sstevel@tonic-gate } 11477c478bd9Sstevel@tonic-gate } 11487c478bd9Sstevel@tonic-gate else 11497c478bd9Sstevel@tonic-gate { 11507c478bd9Sstevel@tonic-gate int off; 11517c478bd9Sstevel@tonic-gate 11527c478bd9Sstevel@tonic-gate /* quoted-printable */ 11537c478bd9Sstevel@tonic-gate pxflags |= PXLF_NOADDEOL; 11547c478bd9Sstevel@tonic-gate fbufp = fbuf; 11557c478bd9Sstevel@tonic-gate while (sm_io_fgets(e->e_dfp, SM_TIME_DEFAULT, buf, 11567c478bd9Sstevel@tonic-gate sizeof buf) != NULL) 11577c478bd9Sstevel@tonic-gate { 11587c478bd9Sstevel@tonic-gate off = mime_fromqp((unsigned char *) buf, &fbufp, 11597c478bd9Sstevel@tonic-gate &fbuf[MAXLINE] - fbufp); 11607c478bd9Sstevel@tonic-gate again: 11617c478bd9Sstevel@tonic-gate if (off < -1) 11627c478bd9Sstevel@tonic-gate continue; 11637c478bd9Sstevel@tonic-gate 11647c478bd9Sstevel@tonic-gate if (fbufp - fbuf > 0) 1165*445f2479Sjbeck { 1166*445f2479Sjbeck if (!putxline((char *) fbuf, fbufp - fbuf - 1, 1167*445f2479Sjbeck mci, pxflags)) 1168*445f2479Sjbeck goto writeerr; 1169*445f2479Sjbeck } 11707c478bd9Sstevel@tonic-gate fbufp = fbuf; 11717c478bd9Sstevel@tonic-gate if (off >= 0 && buf[off] != '\0') 11727c478bd9Sstevel@tonic-gate { 11737c478bd9Sstevel@tonic-gate off = mime_fromqp((unsigned char *) (buf + off), 11747c478bd9Sstevel@tonic-gate &fbufp, 11757c478bd9Sstevel@tonic-gate &fbuf[MAXLINE] - fbufp); 11767c478bd9Sstevel@tonic-gate goto again; 11777c478bd9Sstevel@tonic-gate } 11787c478bd9Sstevel@tonic-gate } 11797c478bd9Sstevel@tonic-gate } 11807c478bd9Sstevel@tonic-gate 11817c478bd9Sstevel@tonic-gate /* force out partial last line */ 11827c478bd9Sstevel@tonic-gate if (fbufp > fbuf) 11837c478bd9Sstevel@tonic-gate { 11847c478bd9Sstevel@tonic-gate *fbufp = '\0'; 1185*445f2479Sjbeck if (!putxline((char *) fbuf, fbufp - fbuf, mci, pxflags)) 1186*445f2479Sjbeck goto writeerr; 11877c478bd9Sstevel@tonic-gate } 11887c478bd9Sstevel@tonic-gate 11897c478bd9Sstevel@tonic-gate /* 11907c478bd9Sstevel@tonic-gate ** The decoded text may end without an EOL. Since this function 11917c478bd9Sstevel@tonic-gate ** is only called for text/plain MIME messages, it is safe to 11927c478bd9Sstevel@tonic-gate ** add an extra one at the end just in case. This is a hack, 11937c478bd9Sstevel@tonic-gate ** but so is auto-converting MIME in the first place. 11947c478bd9Sstevel@tonic-gate */ 11957c478bd9Sstevel@tonic-gate 1196*445f2479Sjbeck if (!putline("", mci)) 1197*445f2479Sjbeck goto writeerr; 11987c478bd9Sstevel@tonic-gate 11997c478bd9Sstevel@tonic-gate if (tTd(43, 3)) 12007c478bd9Sstevel@tonic-gate sm_dprintf("\t\t\tmime7to8 => %s to 8bit done\n", cte); 1201*445f2479Sjbeck return true; 1202*445f2479Sjbeck 1203*445f2479Sjbeck writeerr: 1204*445f2479Sjbeck return false; 12057c478bd9Sstevel@tonic-gate } 12067c478bd9Sstevel@tonic-gate /* 12077c478bd9Sstevel@tonic-gate ** The following is based on Borenstein's "codes.c" module, with simplifying 12087c478bd9Sstevel@tonic-gate ** changes as we do not deal with multipart, and to do the translation in-core, 12097c478bd9Sstevel@tonic-gate ** with an attempt to prevent overrun of output buffers. 12107c478bd9Sstevel@tonic-gate ** 12117c478bd9Sstevel@tonic-gate ** What is needed here are changes to defend this code better against 12127c478bd9Sstevel@tonic-gate ** bad encodings. Questionable to always return 0xFF for bad mappings. 12137c478bd9Sstevel@tonic-gate */ 12147c478bd9Sstevel@tonic-gate 12157c478bd9Sstevel@tonic-gate static char index_hex[128] = 12167c478bd9Sstevel@tonic-gate { 12177c478bd9Sstevel@tonic-gate -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, 12187c478bd9Sstevel@tonic-gate -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, 12197c478bd9Sstevel@tonic-gate -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, 12207c478bd9Sstevel@tonic-gate 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,-1,-1, -1,-1,-1,-1, 12217c478bd9Sstevel@tonic-gate -1,10,11,12, 13,14,15,-1, -1,-1,-1,-1, -1,-1,-1,-1, 12227c478bd9Sstevel@tonic-gate -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, 12237c478bd9Sstevel@tonic-gate -1,10,11,12, 13,14,15,-1, -1,-1,-1,-1, -1,-1,-1,-1, 12247c478bd9Sstevel@tonic-gate -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1 12257c478bd9Sstevel@tonic-gate }; 12267c478bd9Sstevel@tonic-gate 12277c478bd9Sstevel@tonic-gate # define HEXCHAR(c) (((c) < 0 || (c) > 127) ? -1 : index_hex[(c)]) 12287c478bd9Sstevel@tonic-gate 12297c478bd9Sstevel@tonic-gate /* 12307c478bd9Sstevel@tonic-gate ** MIME_FROMQP -- decode quoted printable string 12317c478bd9Sstevel@tonic-gate ** 12327c478bd9Sstevel@tonic-gate ** Parameters: 12337c478bd9Sstevel@tonic-gate ** infile -- input (encoded) string 12347c478bd9Sstevel@tonic-gate ** outfile -- output string 12357c478bd9Sstevel@tonic-gate ** maxlen -- size of output buffer 12367c478bd9Sstevel@tonic-gate ** 12377c478bd9Sstevel@tonic-gate ** Returns: 12387c478bd9Sstevel@tonic-gate ** -2 if decoding failure 12397c478bd9Sstevel@tonic-gate ** -1 if infile completely decoded into outfile 12407c478bd9Sstevel@tonic-gate ** >= 0 is the position in infile decoding 12417c478bd9Sstevel@tonic-gate ** reached before maxlen was reached 12427c478bd9Sstevel@tonic-gate */ 12437c478bd9Sstevel@tonic-gate 12447c478bd9Sstevel@tonic-gate static int 12457c478bd9Sstevel@tonic-gate mime_fromqp(infile, outfile, maxlen) 12467c478bd9Sstevel@tonic-gate unsigned char *infile; 12477c478bd9Sstevel@tonic-gate unsigned char **outfile; 12487c478bd9Sstevel@tonic-gate int maxlen; /* Max # of chars allowed in outfile */ 12497c478bd9Sstevel@tonic-gate { 12507c478bd9Sstevel@tonic-gate int c1, c2; 12517c478bd9Sstevel@tonic-gate int nchar = 0; 12527c478bd9Sstevel@tonic-gate unsigned char *b; 12537c478bd9Sstevel@tonic-gate 12547c478bd9Sstevel@tonic-gate /* decrement by one for trailing '\0', at least one other char */ 12557c478bd9Sstevel@tonic-gate if (--maxlen < 1) 12567c478bd9Sstevel@tonic-gate return 0; 12577c478bd9Sstevel@tonic-gate 12587c478bd9Sstevel@tonic-gate b = infile; 12597c478bd9Sstevel@tonic-gate while ((c1 = *infile++) != '\0' && nchar < maxlen) 12607c478bd9Sstevel@tonic-gate { 12617c478bd9Sstevel@tonic-gate if (c1 == '=') 12627c478bd9Sstevel@tonic-gate { 12637c478bd9Sstevel@tonic-gate if ((c1 = *infile++) == '\0') 12647c478bd9Sstevel@tonic-gate break; 12657c478bd9Sstevel@tonic-gate 12667c478bd9Sstevel@tonic-gate if (c1 == '\n' || (c1 = HEXCHAR(c1)) == -1) 12677c478bd9Sstevel@tonic-gate { 12687c478bd9Sstevel@tonic-gate /* ignore it and the rest of the buffer */ 12697c478bd9Sstevel@tonic-gate return -2; 12707c478bd9Sstevel@tonic-gate } 12717c478bd9Sstevel@tonic-gate else 12727c478bd9Sstevel@tonic-gate { 12737c478bd9Sstevel@tonic-gate do 12747c478bd9Sstevel@tonic-gate { 12757c478bd9Sstevel@tonic-gate if ((c2 = *infile++) == '\0') 12767c478bd9Sstevel@tonic-gate { 12777c478bd9Sstevel@tonic-gate c2 = -1; 12787c478bd9Sstevel@tonic-gate break; 12797c478bd9Sstevel@tonic-gate } 12807c478bd9Sstevel@tonic-gate } while ((c2 = HEXCHAR(c2)) == -1); 12817c478bd9Sstevel@tonic-gate 12827c478bd9Sstevel@tonic-gate if (c2 == -1) 12837c478bd9Sstevel@tonic-gate break; 12847c478bd9Sstevel@tonic-gate nchar++; 12857c478bd9Sstevel@tonic-gate *(*outfile)++ = c1 << 4 | c2; 12867c478bd9Sstevel@tonic-gate } 12877c478bd9Sstevel@tonic-gate } 12887c478bd9Sstevel@tonic-gate else 12897c478bd9Sstevel@tonic-gate { 12907c478bd9Sstevel@tonic-gate nchar++; 12917c478bd9Sstevel@tonic-gate *(*outfile)++ = c1; 12927c478bd9Sstevel@tonic-gate if (c1 == '\n') 12937c478bd9Sstevel@tonic-gate break; 12947c478bd9Sstevel@tonic-gate } 12957c478bd9Sstevel@tonic-gate } 12967c478bd9Sstevel@tonic-gate *(*outfile)++ = '\0'; 12977c478bd9Sstevel@tonic-gate if (nchar >= maxlen) 12987c478bd9Sstevel@tonic-gate return (infile - b - 1); 12997c478bd9Sstevel@tonic-gate return -1; 13007c478bd9Sstevel@tonic-gate } 13017c478bd9Sstevel@tonic-gate #endif /* MIME7TO8 */ 1302