17c478bd9Sstevel@tonic-gate /*
23ee0e492Sjbeck * Copyright (c) 1998-2003, 2006 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 #include <sendmail.h>
157c478bd9Sstevel@tonic-gate #include <string.h>
167c478bd9Sstevel@tonic-gate
17*7800901eSjbeck SM_RCSID("@(#)$Id: mime.c,v 8.147 2007/09/26 23:29:11 ca Exp $")
187c478bd9Sstevel@tonic-gate
197c478bd9Sstevel@tonic-gate /*
207c478bd9Sstevel@tonic-gate ** MIME support.
217c478bd9Sstevel@tonic-gate **
227c478bd9Sstevel@tonic-gate ** I am indebted to John Beck of Hewlett-Packard, who contributed
237c478bd9Sstevel@tonic-gate ** his code to me for inclusion. As it turns out, I did not use
247c478bd9Sstevel@tonic-gate ** his code since he used a "minimum change" approach that used
257c478bd9Sstevel@tonic-gate ** several temp files, and I wanted a "minimum impact" approach
267c478bd9Sstevel@tonic-gate ** that would avoid copying. However, looking over his code
277c478bd9Sstevel@tonic-gate ** helped me cement my understanding of the problem.
287c478bd9Sstevel@tonic-gate **
297c478bd9Sstevel@tonic-gate ** I also looked at, but did not directly use, Nathaniel
307c478bd9Sstevel@tonic-gate ** Borenstein's "code.c" module. Again, it functioned as
317c478bd9Sstevel@tonic-gate ** a file-to-file translator, which did not fit within my
327c478bd9Sstevel@tonic-gate ** design bounds, but it was a useful base for understanding
337c478bd9Sstevel@tonic-gate ** the problem.
347c478bd9Sstevel@tonic-gate */
357c478bd9Sstevel@tonic-gate
367c478bd9Sstevel@tonic-gate /* use "old" mime 7 to 8 algorithm by default */
377c478bd9Sstevel@tonic-gate #ifndef MIME7TO8_OLD
387c478bd9Sstevel@tonic-gate # define MIME7TO8_OLD 1
397c478bd9Sstevel@tonic-gate #endif /* ! MIME7TO8_OLD */
407c478bd9Sstevel@tonic-gate
417c478bd9Sstevel@tonic-gate #if MIME8TO7
427c478bd9Sstevel@tonic-gate static int isboundary __P((char *, char **));
437c478bd9Sstevel@tonic-gate static int mimeboundary __P((char *, char **));
447c478bd9Sstevel@tonic-gate static int mime_getchar __P((SM_FILE_T *, char **, int *));
457c478bd9Sstevel@tonic-gate static int mime_getchar_crlf __P((SM_FILE_T *, char **, int *));
467c478bd9Sstevel@tonic-gate
477c478bd9Sstevel@tonic-gate /* character set for hex and base64 encoding */
487c478bd9Sstevel@tonic-gate static char Base16Code[] = "0123456789ABCDEF";
497c478bd9Sstevel@tonic-gate static char Base64Code[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
507c478bd9Sstevel@tonic-gate
517c478bd9Sstevel@tonic-gate /* types of MIME boundaries */
527c478bd9Sstevel@tonic-gate # define MBT_SYNTAX 0 /* syntax error */
537c478bd9Sstevel@tonic-gate # define MBT_NOTSEP 1 /* not a boundary */
547c478bd9Sstevel@tonic-gate # define MBT_INTERMED 2 /* intermediate boundary (no trailing --) */
557c478bd9Sstevel@tonic-gate # define MBT_FINAL 3 /* final boundary (trailing -- included) */
567c478bd9Sstevel@tonic-gate
577c478bd9Sstevel@tonic-gate static char *MimeBoundaryNames[] =
587c478bd9Sstevel@tonic-gate {
597c478bd9Sstevel@tonic-gate "SYNTAX", "NOTSEP", "INTERMED", "FINAL"
607c478bd9Sstevel@tonic-gate };
617c478bd9Sstevel@tonic-gate
627c478bd9Sstevel@tonic-gate static bool MapNLtoCRLF;
637c478bd9Sstevel@tonic-gate
647c478bd9Sstevel@tonic-gate /*
657c478bd9Sstevel@tonic-gate ** MIME8TO7 -- output 8 bit body in 7 bit format
667c478bd9Sstevel@tonic-gate **
677c478bd9Sstevel@tonic-gate ** The header has already been output -- this has to do the
687c478bd9Sstevel@tonic-gate ** 8 to 7 bit conversion. It would be easy if we didn't have
697c478bd9Sstevel@tonic-gate ** to deal with nested formats (multipart/xxx and message/rfc822).
707c478bd9Sstevel@tonic-gate **
717c478bd9Sstevel@tonic-gate ** We won't be called if we don't have to do a conversion, and
727c478bd9Sstevel@tonic-gate ** appropriate MIME-Version: and Content-Type: fields have been
737c478bd9Sstevel@tonic-gate ** output. Any Content-Transfer-Encoding: field has not been
747c478bd9Sstevel@tonic-gate ** output, and we can add it here.
757c478bd9Sstevel@tonic-gate **
767c478bd9Sstevel@tonic-gate ** Parameters:
777c478bd9Sstevel@tonic-gate ** mci -- mailer connection information.
787c478bd9Sstevel@tonic-gate ** header -- the header for this body part.
797c478bd9Sstevel@tonic-gate ** e -- envelope.
807c478bd9Sstevel@tonic-gate ** boundaries -- the currently pending message boundaries.
817c478bd9Sstevel@tonic-gate ** NULL if we are processing the outer portion.
827c478bd9Sstevel@tonic-gate ** flags -- to tweak processing.
833ee0e492Sjbeck ** level -- recursion level.
847c478bd9Sstevel@tonic-gate **
857c478bd9Sstevel@tonic-gate ** Returns:
867c478bd9Sstevel@tonic-gate ** An indicator of what terminated the message part:
877c478bd9Sstevel@tonic-gate ** MBT_FINAL -- the final boundary
887c478bd9Sstevel@tonic-gate ** MBT_INTERMED -- an intermediate boundary
897c478bd9Sstevel@tonic-gate ** MBT_NOTSEP -- an end of file
90445f2479Sjbeck ** SM_IO_EOF -- I/O error occurred
917c478bd9Sstevel@tonic-gate */
927c478bd9Sstevel@tonic-gate
937c478bd9Sstevel@tonic-gate struct args
947c478bd9Sstevel@tonic-gate {
957c478bd9Sstevel@tonic-gate char *a_field; /* name of field */
967c478bd9Sstevel@tonic-gate char *a_value; /* value of that field */
977c478bd9Sstevel@tonic-gate };
987c478bd9Sstevel@tonic-gate
997c478bd9Sstevel@tonic-gate int
mime8to7(mci,header,e,boundaries,flags,level)1003ee0e492Sjbeck mime8to7(mci, header, e, boundaries, flags, level)
1017c478bd9Sstevel@tonic-gate register MCI *mci;
1027c478bd9Sstevel@tonic-gate HDR *header;
1037c478bd9Sstevel@tonic-gate register ENVELOPE *e;
1047c478bd9Sstevel@tonic-gate char **boundaries;
1057c478bd9Sstevel@tonic-gate int flags;
1063ee0e492Sjbeck int level;
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
1273ee0e492Sjbeck if (level > MAXMIMENESTING)
1283ee0e492Sjbeck {
1293ee0e492Sjbeck if (!bitset(EF_TOODEEP, e->e_flags))
1303ee0e492Sjbeck {
1313ee0e492Sjbeck if (tTd(43, 4))
1323ee0e492Sjbeck sm_dprintf("mime8to7: too deep, level=%d\n",
1333ee0e492Sjbeck level);
1343ee0e492Sjbeck usrerr("mime8to7: recursion level %d exceeded",
1353ee0e492Sjbeck level);
1363ee0e492Sjbeck e->e_flags |= EF_DONT_MIME|EF_TOODEEP;
1373ee0e492Sjbeck }
1383ee0e492Sjbeck }
1397c478bd9Sstevel@tonic-gate if (tTd(43, 1))
1407c478bd9Sstevel@tonic-gate {
1417c478bd9Sstevel@tonic-gate sm_dprintf("mime8to7: flags = %x, boundaries =", flags);
1427c478bd9Sstevel@tonic-gate if (boundaries[0] == NULL)
1437c478bd9Sstevel@tonic-gate sm_dprintf(" <none>");
1447c478bd9Sstevel@tonic-gate else
1457c478bd9Sstevel@tonic-gate {
1467c478bd9Sstevel@tonic-gate for (i = 0; boundaries[i] != NULL; i++)
1477c478bd9Sstevel@tonic-gate sm_dprintf(" %s", boundaries[i]);
1487c478bd9Sstevel@tonic-gate }
1497c478bd9Sstevel@tonic-gate sm_dprintf("\n");
1507c478bd9Sstevel@tonic-gate }
1517c478bd9Sstevel@tonic-gate MapNLtoCRLF = true;
1527c478bd9Sstevel@tonic-gate p = hvalue("Content-Transfer-Encoding", header);
1537c478bd9Sstevel@tonic-gate if (p == NULL ||
154058561cbSjbeck (pvp = prescan(p, '\0', pvpbuf, sizeof(pvpbuf), NULL,
1557c478bd9Sstevel@tonic-gate MimeTokenTab, false)) == NULL ||
1567c478bd9Sstevel@tonic-gate pvp[0] == NULL)
1577c478bd9Sstevel@tonic-gate {
1587c478bd9Sstevel@tonic-gate cte = NULL;
1597c478bd9Sstevel@tonic-gate }
1607c478bd9Sstevel@tonic-gate else
1617c478bd9Sstevel@tonic-gate {
162058561cbSjbeck cataddr(pvp, NULL, buf, sizeof(buf), '\0', false);
1637c478bd9Sstevel@tonic-gate cte = sm_rpool_strdup_x(e->e_rpool, buf);
1647c478bd9Sstevel@tonic-gate }
1657c478bd9Sstevel@tonic-gate
1667c478bd9Sstevel@tonic-gate type = subtype = NULL;
1677c478bd9Sstevel@tonic-gate p = hvalue("Content-Type", header);
1687c478bd9Sstevel@tonic-gate if (p == NULL)
1697c478bd9Sstevel@tonic-gate {
1707c478bd9Sstevel@tonic-gate if (bitset(M87F_DIGEST, flags))
1717c478bd9Sstevel@tonic-gate p = "message/rfc822";
1727c478bd9Sstevel@tonic-gate else
1737c478bd9Sstevel@tonic-gate p = "text/plain";
1747c478bd9Sstevel@tonic-gate }
1757c478bd9Sstevel@tonic-gate if (p != NULL &&
176058561cbSjbeck (pvp = prescan(p, '\0', pvpbuf, sizeof(pvpbuf), NULL,
1777c478bd9Sstevel@tonic-gate MimeTokenTab, false)) != NULL &&
1787c478bd9Sstevel@tonic-gate pvp[0] != NULL)
1797c478bd9Sstevel@tonic-gate {
1807c478bd9Sstevel@tonic-gate if (tTd(43, 40))
1817c478bd9Sstevel@tonic-gate {
1827c478bd9Sstevel@tonic-gate for (i = 0; pvp[i] != NULL; i++)
1837c478bd9Sstevel@tonic-gate sm_dprintf("pvp[%d] = \"%s\"\n", i, pvp[i]);
1847c478bd9Sstevel@tonic-gate }
1857c478bd9Sstevel@tonic-gate type = *pvp++;
1867c478bd9Sstevel@tonic-gate if (*pvp != NULL && strcmp(*pvp, "/") == 0 &&
1877c478bd9Sstevel@tonic-gate *++pvp != NULL)
1887c478bd9Sstevel@tonic-gate {
1897c478bd9Sstevel@tonic-gate subtype = *pvp++;
1907c478bd9Sstevel@tonic-gate }
1917c478bd9Sstevel@tonic-gate
1927c478bd9Sstevel@tonic-gate /* break out parameters */
1937c478bd9Sstevel@tonic-gate while (*pvp != NULL && argc < MAXMIMEARGS)
1947c478bd9Sstevel@tonic-gate {
1957c478bd9Sstevel@tonic-gate /* skip to semicolon separator */
1967c478bd9Sstevel@tonic-gate while (*pvp != NULL && strcmp(*pvp, ";") != 0)
1977c478bd9Sstevel@tonic-gate pvp++;
1987c478bd9Sstevel@tonic-gate if (*pvp++ == NULL || *pvp == NULL)
1997c478bd9Sstevel@tonic-gate break;
2007c478bd9Sstevel@tonic-gate
2017c478bd9Sstevel@tonic-gate /* complain about empty values */
2027c478bd9Sstevel@tonic-gate if (strcmp(*pvp, ";") == 0)
2037c478bd9Sstevel@tonic-gate {
2047c478bd9Sstevel@tonic-gate usrerr("mime8to7: Empty parameter in Content-Type header");
2057c478bd9Sstevel@tonic-gate
2067c478bd9Sstevel@tonic-gate /* avoid bounce loops */
2077c478bd9Sstevel@tonic-gate e->e_flags |= EF_DONT_MIME;
2087c478bd9Sstevel@tonic-gate continue;
2097c478bd9Sstevel@tonic-gate }
2107c478bd9Sstevel@tonic-gate
2117c478bd9Sstevel@tonic-gate /* extract field name */
2127c478bd9Sstevel@tonic-gate argv[argc].a_field = *pvp++;
2137c478bd9Sstevel@tonic-gate
2147c478bd9Sstevel@tonic-gate /* see if there is a value */
2157c478bd9Sstevel@tonic-gate if (*pvp != NULL && strcmp(*pvp, "=") == 0 &&
2167c478bd9Sstevel@tonic-gate (*++pvp == NULL || strcmp(*pvp, ";") != 0))
2177c478bd9Sstevel@tonic-gate {
2187c478bd9Sstevel@tonic-gate argv[argc].a_value = *pvp;
2197c478bd9Sstevel@tonic-gate argc++;
2207c478bd9Sstevel@tonic-gate }
2217c478bd9Sstevel@tonic-gate }
2227c478bd9Sstevel@tonic-gate }
2237c478bd9Sstevel@tonic-gate
2247c478bd9Sstevel@tonic-gate /* check for disaster cases */
2257c478bd9Sstevel@tonic-gate if (type == NULL)
2267c478bd9Sstevel@tonic-gate type = "-none-";
2277c478bd9Sstevel@tonic-gate if (subtype == NULL)
2287c478bd9Sstevel@tonic-gate subtype = "-none-";
2297c478bd9Sstevel@tonic-gate
2303ee0e492Sjbeck /* don't propagate some flags more than one level into the message */
2317c478bd9Sstevel@tonic-gate flags &= ~M87F_DIGEST;
2327c478bd9Sstevel@tonic-gate
2337c478bd9Sstevel@tonic-gate /*
2347c478bd9Sstevel@tonic-gate ** Check for cases that can not be encoded.
2357c478bd9Sstevel@tonic-gate **
2367c478bd9Sstevel@tonic-gate ** For example, you can't encode certain kinds of types
2377c478bd9Sstevel@tonic-gate ** or already-encoded messages. If we find this case,
2387c478bd9Sstevel@tonic-gate ** just copy it through.
2397c478bd9Sstevel@tonic-gate */
2407c478bd9Sstevel@tonic-gate
241058561cbSjbeck (void) sm_snprintf(buf, sizeof(buf), "%.100s/%.100s", type, subtype);
2427c478bd9Sstevel@tonic-gate if (wordinclass(buf, 'n') || (cte != NULL && !wordinclass(cte, 'e')))
2437c478bd9Sstevel@tonic-gate flags |= M87F_NO8BIT;
2447c478bd9Sstevel@tonic-gate
2457c478bd9Sstevel@tonic-gate # ifdef USE_B_CLASS
2467c478bd9Sstevel@tonic-gate if (wordinclass(buf, 'b') || wordinclass(type, 'b'))
2477c478bd9Sstevel@tonic-gate MapNLtoCRLF = false;
2487c478bd9Sstevel@tonic-gate # endif /* USE_B_CLASS */
2497c478bd9Sstevel@tonic-gate if (wordinclass(buf, 'q') || wordinclass(type, 'q'))
2507c478bd9Sstevel@tonic-gate use_qp = true;
2517c478bd9Sstevel@tonic-gate
2527c478bd9Sstevel@tonic-gate /*
2537c478bd9Sstevel@tonic-gate ** Multipart requires special processing.
2547c478bd9Sstevel@tonic-gate **
2557c478bd9Sstevel@tonic-gate ** Do a recursive descent into the message.
2567c478bd9Sstevel@tonic-gate */
2577c478bd9Sstevel@tonic-gate
2587c478bd9Sstevel@tonic-gate if (sm_strcasecmp(type, "multipart") == 0 &&
2593ee0e492Sjbeck (!bitset(M87F_NO8BIT, flags) || bitset(M87F_NO8TO7, flags)) &&
2603ee0e492Sjbeck !bitset(EF_TOODEEP, e->e_flags)
2613ee0e492Sjbeck )
2627c478bd9Sstevel@tonic-gate {
2637c478bd9Sstevel@tonic-gate
2647c478bd9Sstevel@tonic-gate if (sm_strcasecmp(subtype, "digest") == 0)
2657c478bd9Sstevel@tonic-gate flags |= M87F_DIGEST;
2667c478bd9Sstevel@tonic-gate
2677c478bd9Sstevel@tonic-gate for (i = 0; i < argc; i++)
2687c478bd9Sstevel@tonic-gate {
2697c478bd9Sstevel@tonic-gate if (sm_strcasecmp(argv[i].a_field, "boundary") == 0)
2707c478bd9Sstevel@tonic-gate break;
2717c478bd9Sstevel@tonic-gate }
2727c478bd9Sstevel@tonic-gate if (i >= argc || argv[i].a_value == NULL)
2737c478bd9Sstevel@tonic-gate {
2747c478bd9Sstevel@tonic-gate usrerr("mime8to7: Content-Type: \"%s\": %s boundary",
2757c478bd9Sstevel@tonic-gate i >= argc ? "missing" : "bogus", p);
2767c478bd9Sstevel@tonic-gate p = "---";
2777c478bd9Sstevel@tonic-gate
2787c478bd9Sstevel@tonic-gate /* avoid bounce loops */
2797c478bd9Sstevel@tonic-gate e->e_flags |= EF_DONT_MIME;
2807c478bd9Sstevel@tonic-gate }
2817c478bd9Sstevel@tonic-gate else
2827c478bd9Sstevel@tonic-gate {
2837c478bd9Sstevel@tonic-gate p = argv[i].a_value;
2847c478bd9Sstevel@tonic-gate stripquotes(p);
2857c478bd9Sstevel@tonic-gate }
286058561cbSjbeck if (sm_strlcpy(bbuf, p, sizeof(bbuf)) >= sizeof(bbuf))
2877c478bd9Sstevel@tonic-gate {
2887c478bd9Sstevel@tonic-gate usrerr("mime8to7: multipart boundary \"%s\" too long",
2897c478bd9Sstevel@tonic-gate p);
2907c478bd9Sstevel@tonic-gate
2917c478bd9Sstevel@tonic-gate /* avoid bounce loops */
2927c478bd9Sstevel@tonic-gate e->e_flags |= EF_DONT_MIME;
2937c478bd9Sstevel@tonic-gate }
2947c478bd9Sstevel@tonic-gate
2957c478bd9Sstevel@tonic-gate if (tTd(43, 1))
2967c478bd9Sstevel@tonic-gate sm_dprintf("mime8to7: multipart boundary \"%s\"\n",
2977c478bd9Sstevel@tonic-gate bbuf);
2987c478bd9Sstevel@tonic-gate for (i = 0; i < MAXMIMENESTING; i++)
2997c478bd9Sstevel@tonic-gate {
3007c478bd9Sstevel@tonic-gate if (boundaries[i] == NULL)
3017c478bd9Sstevel@tonic-gate break;
3027c478bd9Sstevel@tonic-gate }
3037c478bd9Sstevel@tonic-gate if (i >= MAXMIMENESTING)
3047c478bd9Sstevel@tonic-gate {
3053ee0e492Sjbeck if (tTd(43, 4))
3063ee0e492Sjbeck sm_dprintf("mime8to7: too deep, i=%d\n", i);
3073ee0e492Sjbeck if (!bitset(EF_TOODEEP, e->e_flags))
3087c478bd9Sstevel@tonic-gate usrerr("mime8to7: multipart nesting boundary too deep");
3097c478bd9Sstevel@tonic-gate
3107c478bd9Sstevel@tonic-gate /* avoid bounce loops */
3113ee0e492Sjbeck e->e_flags |= EF_DONT_MIME|EF_TOODEEP;
3127c478bd9Sstevel@tonic-gate }
3137c478bd9Sstevel@tonic-gate else
3147c478bd9Sstevel@tonic-gate {
3157c478bd9Sstevel@tonic-gate boundaries[i] = bbuf;
3167c478bd9Sstevel@tonic-gate boundaries[i + 1] = NULL;
3177c478bd9Sstevel@tonic-gate }
3187c478bd9Sstevel@tonic-gate mci->mci_flags |= MCIF_INMIME;
3197c478bd9Sstevel@tonic-gate
3207c478bd9Sstevel@tonic-gate /* skip the early "comment" prologue */
321445f2479Sjbeck if (!putline("", mci))
322445f2479Sjbeck goto writeerr;
3237c478bd9Sstevel@tonic-gate mci->mci_flags &= ~MCIF_INHEADER;
3247c478bd9Sstevel@tonic-gate bt = MBT_FINAL;
325058561cbSjbeck while (sm_io_fgets(e->e_dfp, SM_TIME_DEFAULT, buf, sizeof(buf))
3267c478bd9Sstevel@tonic-gate != NULL)
3277c478bd9Sstevel@tonic-gate {
3287c478bd9Sstevel@tonic-gate bt = mimeboundary(buf, boundaries);
3297c478bd9Sstevel@tonic-gate if (bt != MBT_NOTSEP)
3307c478bd9Sstevel@tonic-gate break;
331445f2479Sjbeck if (!putxline(buf, strlen(buf), mci,
332445f2479Sjbeck PXLF_MAPFROM|PXLF_STRIP8BIT))
333445f2479Sjbeck goto writeerr;
3347c478bd9Sstevel@tonic-gate if (tTd(43, 99))
3357c478bd9Sstevel@tonic-gate sm_dprintf(" ...%s", buf);
3367c478bd9Sstevel@tonic-gate }
3377c478bd9Sstevel@tonic-gate if (sm_io_eof(e->e_dfp))
3387c478bd9Sstevel@tonic-gate bt = MBT_FINAL;
3397c478bd9Sstevel@tonic-gate while (bt != MBT_FINAL)
3407c478bd9Sstevel@tonic-gate {
3417c478bd9Sstevel@tonic-gate auto HDR *hdr = NULL;
3427c478bd9Sstevel@tonic-gate
343058561cbSjbeck (void) sm_strlcpyn(buf, sizeof(buf), 2, "--", bbuf);
344445f2479Sjbeck if (!putline(buf, mci))
345445f2479Sjbeck goto writeerr;
3467c478bd9Sstevel@tonic-gate if (tTd(43, 35))
3477c478bd9Sstevel@tonic-gate sm_dprintf(" ...%s\n", buf);
3487c478bd9Sstevel@tonic-gate collect(e->e_dfp, false, &hdr, e, false);
3497c478bd9Sstevel@tonic-gate if (tTd(43, 101))
3507c478bd9Sstevel@tonic-gate putline("+++after collect", mci);
351445f2479Sjbeck if (!putheader(mci, hdr, e, flags))
352445f2479Sjbeck goto writeerr;
3537c478bd9Sstevel@tonic-gate if (tTd(43, 101))
3547c478bd9Sstevel@tonic-gate putline("+++after putheader", mci);
3553ee0e492Sjbeck bt = mime8to7(mci, hdr, e, boundaries, flags,
3563ee0e492Sjbeck level + 1);
357445f2479Sjbeck if (bt == SM_IO_EOF)
358445f2479Sjbeck goto writeerr;
3597c478bd9Sstevel@tonic-gate }
360058561cbSjbeck (void) sm_strlcpyn(buf, sizeof(buf), 3, "--", bbuf, "--");
361445f2479Sjbeck if (!putline(buf, mci))
362445f2479Sjbeck goto writeerr;
3637c478bd9Sstevel@tonic-gate if (tTd(43, 35))
3647c478bd9Sstevel@tonic-gate sm_dprintf(" ...%s\n", buf);
3657c478bd9Sstevel@tonic-gate boundaries[i] = NULL;
3667c478bd9Sstevel@tonic-gate mci->mci_flags &= ~MCIF_INMIME;
3677c478bd9Sstevel@tonic-gate
3687c478bd9Sstevel@tonic-gate /* skip the late "comment" epilogue */
369058561cbSjbeck while (sm_io_fgets(e->e_dfp, SM_TIME_DEFAULT, buf, sizeof(buf))
3707c478bd9Sstevel@tonic-gate != NULL)
3717c478bd9Sstevel@tonic-gate {
3727c478bd9Sstevel@tonic-gate bt = mimeboundary(buf, boundaries);
3737c478bd9Sstevel@tonic-gate if (bt != MBT_NOTSEP)
3747c478bd9Sstevel@tonic-gate break;
375445f2479Sjbeck if (!putxline(buf, strlen(buf), mci,
376445f2479Sjbeck PXLF_MAPFROM|PXLF_STRIP8BIT))
377445f2479Sjbeck goto writeerr;
3787c478bd9Sstevel@tonic-gate if (tTd(43, 99))
3797c478bd9Sstevel@tonic-gate sm_dprintf(" ...%s", buf);
3807c478bd9Sstevel@tonic-gate }
3817c478bd9Sstevel@tonic-gate if (sm_io_eof(e->e_dfp))
3827c478bd9Sstevel@tonic-gate bt = MBT_FINAL;
3837c478bd9Sstevel@tonic-gate if (tTd(43, 3))
3847c478bd9Sstevel@tonic-gate sm_dprintf("\t\t\tmime8to7=>%s (multipart)\n",
3857c478bd9Sstevel@tonic-gate MimeBoundaryNames[bt]);
3867c478bd9Sstevel@tonic-gate return bt;
3877c478bd9Sstevel@tonic-gate }
3887c478bd9Sstevel@tonic-gate
3897c478bd9Sstevel@tonic-gate /*
3907c478bd9Sstevel@tonic-gate ** Message/xxx types -- recurse exactly once.
3917c478bd9Sstevel@tonic-gate **
3927c478bd9Sstevel@tonic-gate ** Class 's' is predefined to have "rfc822" only.
3937c478bd9Sstevel@tonic-gate */
3947c478bd9Sstevel@tonic-gate
3957c478bd9Sstevel@tonic-gate if (sm_strcasecmp(type, "message") == 0)
3967c478bd9Sstevel@tonic-gate {
3973ee0e492Sjbeck if (!wordinclass(subtype, 's') ||
3983ee0e492Sjbeck bitset(EF_TOODEEP, e->e_flags))
3997c478bd9Sstevel@tonic-gate {
4007c478bd9Sstevel@tonic-gate flags |= M87F_NO8BIT;
4017c478bd9Sstevel@tonic-gate }
4027c478bd9Sstevel@tonic-gate else
4037c478bd9Sstevel@tonic-gate {
4047c478bd9Sstevel@tonic-gate auto HDR *hdr = NULL;
4057c478bd9Sstevel@tonic-gate
406445f2479Sjbeck if (!putline("", mci))
407445f2479Sjbeck goto writeerr;
4087c478bd9Sstevel@tonic-gate
4097c478bd9Sstevel@tonic-gate mci->mci_flags |= MCIF_INMIME;
4107c478bd9Sstevel@tonic-gate collect(e->e_dfp, false, &hdr, e, false);
4117c478bd9Sstevel@tonic-gate if (tTd(43, 101))
4127c478bd9Sstevel@tonic-gate putline("+++after collect", mci);
413445f2479Sjbeck if (!putheader(mci, hdr, e, flags))
414445f2479Sjbeck goto writeerr;
4157c478bd9Sstevel@tonic-gate if (tTd(43, 101))
4167c478bd9Sstevel@tonic-gate putline("+++after putheader", mci);
4177c478bd9Sstevel@tonic-gate if (hvalue("MIME-Version", hdr) == NULL &&
418445f2479Sjbeck !bitset(M87F_NO8TO7, flags) &&
419445f2479Sjbeck !putline("MIME-Version: 1.0", mci))
420445f2479Sjbeck goto writeerr;
4213ee0e492Sjbeck bt = mime8to7(mci, hdr, e, boundaries, flags,
4223ee0e492Sjbeck level + 1);
4237c478bd9Sstevel@tonic-gate mci->mci_flags &= ~MCIF_INMIME;
4247c478bd9Sstevel@tonic-gate return bt;
4257c478bd9Sstevel@tonic-gate }
4267c478bd9Sstevel@tonic-gate }
4277c478bd9Sstevel@tonic-gate
4287c478bd9Sstevel@tonic-gate /*
4297c478bd9Sstevel@tonic-gate ** Non-compound body type
4307c478bd9Sstevel@tonic-gate **
4317c478bd9Sstevel@tonic-gate ** Compute the ratio of seven to eight bit characters;
4327c478bd9Sstevel@tonic-gate ** use that as a heuristic to decide how to do the
4337c478bd9Sstevel@tonic-gate ** encoding.
4347c478bd9Sstevel@tonic-gate */
4357c478bd9Sstevel@tonic-gate
4367c478bd9Sstevel@tonic-gate sectionsize = sectionhighbits = 0;
4377c478bd9Sstevel@tonic-gate if (!bitset(M87F_NO8BIT|M87F_NO8TO7, flags))
4387c478bd9Sstevel@tonic-gate {
4397c478bd9Sstevel@tonic-gate /* remember where we were */
4407c478bd9Sstevel@tonic-gate offset = sm_io_tell(e->e_dfp, SM_TIME_DEFAULT);
4417c478bd9Sstevel@tonic-gate if (offset == -1)
4427c478bd9Sstevel@tonic-gate syserr("mime8to7: cannot sm_io_tell on %cf%s",
4437c478bd9Sstevel@tonic-gate DATAFL_LETTER, e->e_id);
4447c478bd9Sstevel@tonic-gate
4457c478bd9Sstevel@tonic-gate /* do a scan of this body type to count character types */
446058561cbSjbeck while (sm_io_fgets(e->e_dfp, SM_TIME_DEFAULT, buf, sizeof(buf))
4477c478bd9Sstevel@tonic-gate != NULL)
4487c478bd9Sstevel@tonic-gate {
4497c478bd9Sstevel@tonic-gate if (mimeboundary(buf, boundaries) != MBT_NOTSEP)
4507c478bd9Sstevel@tonic-gate break;
4517c478bd9Sstevel@tonic-gate for (p = buf; *p != '\0'; p++)
4527c478bd9Sstevel@tonic-gate {
4537c478bd9Sstevel@tonic-gate /* count bytes with the high bit set */
4547c478bd9Sstevel@tonic-gate sectionsize++;
4557c478bd9Sstevel@tonic-gate if (bitset(0200, *p))
4567c478bd9Sstevel@tonic-gate sectionhighbits++;
4577c478bd9Sstevel@tonic-gate }
4587c478bd9Sstevel@tonic-gate
4597c478bd9Sstevel@tonic-gate /*
4607c478bd9Sstevel@tonic-gate ** Heuristic: if 1/4 of the first 4K bytes are 8-bit,
4617c478bd9Sstevel@tonic-gate ** assume base64. This heuristic avoids double-reading
4627c478bd9Sstevel@tonic-gate ** large graphics or video files.
4637c478bd9Sstevel@tonic-gate */
4647c478bd9Sstevel@tonic-gate
4657c478bd9Sstevel@tonic-gate if (sectionsize >= 4096 &&
4667c478bd9Sstevel@tonic-gate sectionhighbits > sectionsize / 4)
4677c478bd9Sstevel@tonic-gate break;
4687c478bd9Sstevel@tonic-gate }
4697c478bd9Sstevel@tonic-gate
4707c478bd9Sstevel@tonic-gate /* return to the original offset for processing */
4717c478bd9Sstevel@tonic-gate /* XXX use relative seeks to handle >31 bit file sizes? */
4727c478bd9Sstevel@tonic-gate if (sm_io_seek(e->e_dfp, SM_TIME_DEFAULT, offset, SEEK_SET) < 0)
4737c478bd9Sstevel@tonic-gate syserr("mime8to7: cannot sm_io_fseek on %cf%s",
4747c478bd9Sstevel@tonic-gate DATAFL_LETTER, e->e_id);
4757c478bd9Sstevel@tonic-gate else
4767c478bd9Sstevel@tonic-gate sm_io_clearerr(e->e_dfp);
4777c478bd9Sstevel@tonic-gate }
4787c478bd9Sstevel@tonic-gate
4797c478bd9Sstevel@tonic-gate /*
4807c478bd9Sstevel@tonic-gate ** Heuristically determine encoding method.
4817c478bd9Sstevel@tonic-gate ** If more than 1/8 of the total characters have the
4827c478bd9Sstevel@tonic-gate ** eighth bit set, use base64; else use quoted-printable.
4837c478bd9Sstevel@tonic-gate ** However, only encode binary encoded data as base64,
4847c478bd9Sstevel@tonic-gate ** since otherwise the NL=>CRLF mapping will be a problem.
4857c478bd9Sstevel@tonic-gate */
4867c478bd9Sstevel@tonic-gate
4877c478bd9Sstevel@tonic-gate if (tTd(43, 8))
4887c478bd9Sstevel@tonic-gate {
4897c478bd9Sstevel@tonic-gate sm_dprintf("mime8to7: %ld high bit(s) in %ld byte(s), cte=%s, type=%s/%s\n",
4907c478bd9Sstevel@tonic-gate (long) sectionhighbits, (long) sectionsize,
4917c478bd9Sstevel@tonic-gate cte == NULL ? "[none]" : cte,
4927c478bd9Sstevel@tonic-gate type == NULL ? "[none]" : type,
4937c478bd9Sstevel@tonic-gate subtype == NULL ? "[none]" : subtype);
4947c478bd9Sstevel@tonic-gate }
4957c478bd9Sstevel@tonic-gate if (cte != NULL && sm_strcasecmp(cte, "binary") == 0)
4967c478bd9Sstevel@tonic-gate sectionsize = sectionhighbits;
4977c478bd9Sstevel@tonic-gate linelen = 0;
4987c478bd9Sstevel@tonic-gate bp = buf;
4997c478bd9Sstevel@tonic-gate if (sectionhighbits == 0)
5007c478bd9Sstevel@tonic-gate {
5017c478bd9Sstevel@tonic-gate /* no encoding necessary */
5027c478bd9Sstevel@tonic-gate if (cte != NULL &&
5037c478bd9Sstevel@tonic-gate bitset(MCIF_CVT8TO7|MCIF_CVT7TO8|MCIF_INMIME,
5047c478bd9Sstevel@tonic-gate mci->mci_flags) &&
5057c478bd9Sstevel@tonic-gate !bitset(M87F_NO8TO7, flags))
5067c478bd9Sstevel@tonic-gate {
5077c478bd9Sstevel@tonic-gate /*
5087c478bd9Sstevel@tonic-gate ** Skip _unless_ in MIME mode and potentially
5097c478bd9Sstevel@tonic-gate ** converting from 8 bit to 7 bit MIME. See
5107c478bd9Sstevel@tonic-gate ** putheader() for the counterpart where the
5117c478bd9Sstevel@tonic-gate ** CTE header is skipped in the opposite
5127c478bd9Sstevel@tonic-gate ** situation.
5137c478bd9Sstevel@tonic-gate */
5147c478bd9Sstevel@tonic-gate
515058561cbSjbeck (void) sm_snprintf(buf, sizeof(buf),
5167c478bd9Sstevel@tonic-gate "Content-Transfer-Encoding: %.200s", cte);
517445f2479Sjbeck if (!putline(buf, mci))
518445f2479Sjbeck goto writeerr;
5197c478bd9Sstevel@tonic-gate if (tTd(43, 36))
5207c478bd9Sstevel@tonic-gate sm_dprintf(" ...%s\n", buf);
5217c478bd9Sstevel@tonic-gate }
522445f2479Sjbeck if (!putline("", mci))
523445f2479Sjbeck goto writeerr;
5247c478bd9Sstevel@tonic-gate mci->mci_flags &= ~MCIF_INHEADER;
525058561cbSjbeck while (sm_io_fgets(e->e_dfp, SM_TIME_DEFAULT, buf, sizeof(buf))
5267c478bd9Sstevel@tonic-gate != NULL)
5277c478bd9Sstevel@tonic-gate {
528*7800901eSjbeck if (!bitset(MCIF_INLONGLINE, mci->mci_flags))
529*7800901eSjbeck {
5307c478bd9Sstevel@tonic-gate bt = mimeboundary(buf, boundaries);
5317c478bd9Sstevel@tonic-gate if (bt != MBT_NOTSEP)
5327c478bd9Sstevel@tonic-gate break;
533*7800901eSjbeck }
534*7800901eSjbeck if (!putxline(buf, strlen(buf), mci,
535*7800901eSjbeck PXLF_MAPFROM|PXLF_NOADDEOL))
536445f2479Sjbeck goto writeerr;
5377c478bd9Sstevel@tonic-gate }
5387c478bd9Sstevel@tonic-gate if (sm_io_eof(e->e_dfp))
5397c478bd9Sstevel@tonic-gate bt = MBT_FINAL;
5407c478bd9Sstevel@tonic-gate }
5417c478bd9Sstevel@tonic-gate else if (!MapNLtoCRLF ||
5427c478bd9Sstevel@tonic-gate (sectionsize / 8 < sectionhighbits && !use_qp))
5437c478bd9Sstevel@tonic-gate {
5447c478bd9Sstevel@tonic-gate /* use base64 encoding */
5457c478bd9Sstevel@tonic-gate int c1, c2;
5467c478bd9Sstevel@tonic-gate
5477c478bd9Sstevel@tonic-gate if (tTd(43, 36))
5487c478bd9Sstevel@tonic-gate sm_dprintf(" ...Content-Transfer-Encoding: base64\n");
549445f2479Sjbeck if (!putline("Content-Transfer-Encoding: base64", mci))
550445f2479Sjbeck goto writeerr;
551058561cbSjbeck (void) sm_snprintf(buf, sizeof(buf),
5527c478bd9Sstevel@tonic-gate "X-MIME-Autoconverted: from 8bit to base64 by %s id %s",
5537c478bd9Sstevel@tonic-gate MyHostName, e->e_id);
554445f2479Sjbeck if (!putline(buf, mci) || !putline("", mci))
555445f2479Sjbeck goto writeerr;
5567c478bd9Sstevel@tonic-gate mci->mci_flags &= ~MCIF_INHEADER;
5577c478bd9Sstevel@tonic-gate while ((c1 = mime_getchar_crlf(e->e_dfp, boundaries, &bt)) !=
5587c478bd9Sstevel@tonic-gate SM_IO_EOF)
5597c478bd9Sstevel@tonic-gate {
5607c478bd9Sstevel@tonic-gate if (linelen > 71)
5617c478bd9Sstevel@tonic-gate {
5627c478bd9Sstevel@tonic-gate *bp = '\0';
563445f2479Sjbeck if (!putline(buf, mci))
564445f2479Sjbeck goto writeerr;
5657c478bd9Sstevel@tonic-gate linelen = 0;
5667c478bd9Sstevel@tonic-gate bp = buf;
5677c478bd9Sstevel@tonic-gate }
5687c478bd9Sstevel@tonic-gate linelen += 4;
5697c478bd9Sstevel@tonic-gate *bp++ = Base64Code[(c1 >> 2)];
5707c478bd9Sstevel@tonic-gate c1 = (c1 & 0x03) << 4;
5717c478bd9Sstevel@tonic-gate c2 = mime_getchar_crlf(e->e_dfp, boundaries, &bt);
5727c478bd9Sstevel@tonic-gate if (c2 == SM_IO_EOF)
5737c478bd9Sstevel@tonic-gate {
5747c478bd9Sstevel@tonic-gate *bp++ = Base64Code[c1];
5757c478bd9Sstevel@tonic-gate *bp++ = '=';
5767c478bd9Sstevel@tonic-gate *bp++ = '=';
5777c478bd9Sstevel@tonic-gate break;
5787c478bd9Sstevel@tonic-gate }
5797c478bd9Sstevel@tonic-gate c1 |= (c2 >> 4) & 0x0f;
5807c478bd9Sstevel@tonic-gate *bp++ = Base64Code[c1];
5817c478bd9Sstevel@tonic-gate c1 = (c2 & 0x0f) << 2;
5827c478bd9Sstevel@tonic-gate c2 = mime_getchar_crlf(e->e_dfp, boundaries, &bt);
5837c478bd9Sstevel@tonic-gate if (c2 == SM_IO_EOF)
5847c478bd9Sstevel@tonic-gate {
5857c478bd9Sstevel@tonic-gate *bp++ = Base64Code[c1];
5867c478bd9Sstevel@tonic-gate *bp++ = '=';
5877c478bd9Sstevel@tonic-gate break;
5887c478bd9Sstevel@tonic-gate }
5897c478bd9Sstevel@tonic-gate c1 |= (c2 >> 6) & 0x03;
5907c478bd9Sstevel@tonic-gate *bp++ = Base64Code[c1];
5917c478bd9Sstevel@tonic-gate *bp++ = Base64Code[c2 & 0x3f];
5927c478bd9Sstevel@tonic-gate }
5937c478bd9Sstevel@tonic-gate *bp = '\0';
594445f2479Sjbeck if (!putline(buf, mci))
595445f2479Sjbeck goto writeerr;
5967c478bd9Sstevel@tonic-gate }
5977c478bd9Sstevel@tonic-gate else
5987c478bd9Sstevel@tonic-gate {
5997c478bd9Sstevel@tonic-gate /* use quoted-printable encoding */
6007c478bd9Sstevel@tonic-gate int c1, c2;
6017c478bd9Sstevel@tonic-gate int fromstate;
6027c478bd9Sstevel@tonic-gate BITMAP256 badchars;
6037c478bd9Sstevel@tonic-gate
6047c478bd9Sstevel@tonic-gate /* set up map of characters that must be mapped */
6057c478bd9Sstevel@tonic-gate clrbitmap(badchars);
6067c478bd9Sstevel@tonic-gate for (c1 = 0x00; c1 < 0x20; c1++)
6077c478bd9Sstevel@tonic-gate setbitn(c1, badchars);
6087c478bd9Sstevel@tonic-gate clrbitn('\t', badchars);
6097c478bd9Sstevel@tonic-gate for (c1 = 0x7f; c1 < 0x100; c1++)
6107c478bd9Sstevel@tonic-gate setbitn(c1, badchars);
6117c478bd9Sstevel@tonic-gate setbitn('=', badchars);
6127c478bd9Sstevel@tonic-gate if (bitnset(M_EBCDIC, mci->mci_mailer->m_flags))
6137c478bd9Sstevel@tonic-gate for (p = "!\"#$@[\\]^`{|}~"; *p != '\0'; p++)
6147c478bd9Sstevel@tonic-gate setbitn(*p, badchars);
6157c478bd9Sstevel@tonic-gate
6167c478bd9Sstevel@tonic-gate if (tTd(43, 36))
6177c478bd9Sstevel@tonic-gate sm_dprintf(" ...Content-Transfer-Encoding: quoted-printable\n");
618445f2479Sjbeck if (!putline("Content-Transfer-Encoding: quoted-printable",
619445f2479Sjbeck mci))
620445f2479Sjbeck goto writeerr;
621058561cbSjbeck (void) sm_snprintf(buf, sizeof(buf),
6227c478bd9Sstevel@tonic-gate "X-MIME-Autoconverted: from 8bit to quoted-printable by %s id %s",
6237c478bd9Sstevel@tonic-gate MyHostName, e->e_id);
624445f2479Sjbeck if (!putline(buf, mci) || !putline("", mci))
625445f2479Sjbeck goto writeerr;
6267c478bd9Sstevel@tonic-gate mci->mci_flags &= ~MCIF_INHEADER;
6277c478bd9Sstevel@tonic-gate fromstate = 0;
6287c478bd9Sstevel@tonic-gate c2 = '\n';
6297c478bd9Sstevel@tonic-gate while ((c1 = mime_getchar(e->e_dfp, boundaries, &bt)) !=
6307c478bd9Sstevel@tonic-gate SM_IO_EOF)
6317c478bd9Sstevel@tonic-gate {
6327c478bd9Sstevel@tonic-gate if (c1 == '\n')
6337c478bd9Sstevel@tonic-gate {
6347c478bd9Sstevel@tonic-gate if (c2 == ' ' || c2 == '\t')
6357c478bd9Sstevel@tonic-gate {
6367c478bd9Sstevel@tonic-gate *bp++ = '=';
6377c478bd9Sstevel@tonic-gate *bp++ = Base16Code[(c2 >> 4) & 0x0f];
6387c478bd9Sstevel@tonic-gate *bp++ = Base16Code[c2 & 0x0f];
6397c478bd9Sstevel@tonic-gate }
6407c478bd9Sstevel@tonic-gate if (buf[0] == '.' && bp == &buf[1])
6417c478bd9Sstevel@tonic-gate {
6427c478bd9Sstevel@tonic-gate buf[0] = '=';
6437c478bd9Sstevel@tonic-gate *bp++ = Base16Code[('.' >> 4) & 0x0f];
6447c478bd9Sstevel@tonic-gate *bp++ = Base16Code['.' & 0x0f];
6457c478bd9Sstevel@tonic-gate }
6467c478bd9Sstevel@tonic-gate *bp = '\0';
647445f2479Sjbeck if (!putline(buf, mci))
648445f2479Sjbeck goto writeerr;
6497c478bd9Sstevel@tonic-gate linelen = fromstate = 0;
6507c478bd9Sstevel@tonic-gate bp = buf;
6517c478bd9Sstevel@tonic-gate c2 = c1;
6527c478bd9Sstevel@tonic-gate continue;
6537c478bd9Sstevel@tonic-gate }
6547c478bd9Sstevel@tonic-gate if (c2 == ' ' && linelen == 4 && fromstate == 4 &&
6557c478bd9Sstevel@tonic-gate bitnset(M_ESCFROM, mci->mci_mailer->m_flags))
6567c478bd9Sstevel@tonic-gate {
6577c478bd9Sstevel@tonic-gate *bp++ = '=';
6587c478bd9Sstevel@tonic-gate *bp++ = '2';
6597c478bd9Sstevel@tonic-gate *bp++ = '0';
6607c478bd9Sstevel@tonic-gate linelen += 3;
6617c478bd9Sstevel@tonic-gate }
6627c478bd9Sstevel@tonic-gate else if (c2 == ' ' || c2 == '\t')
6637c478bd9Sstevel@tonic-gate {
6647c478bd9Sstevel@tonic-gate *bp++ = c2;
6657c478bd9Sstevel@tonic-gate linelen++;
6667c478bd9Sstevel@tonic-gate }
6677c478bd9Sstevel@tonic-gate if (linelen > 72 &&
6687c478bd9Sstevel@tonic-gate (linelen > 75 || c1 != '.' ||
6697c478bd9Sstevel@tonic-gate (linelen > 73 && c2 == '.')))
6707c478bd9Sstevel@tonic-gate {
6717c478bd9Sstevel@tonic-gate if (linelen > 73 && c2 == '.')
6727c478bd9Sstevel@tonic-gate bp--;
6737c478bd9Sstevel@tonic-gate else
6747c478bd9Sstevel@tonic-gate c2 = '\n';
6757c478bd9Sstevel@tonic-gate *bp++ = '=';
6767c478bd9Sstevel@tonic-gate *bp = '\0';
677445f2479Sjbeck if (!putline(buf, mci))
678445f2479Sjbeck goto writeerr;
6797c478bd9Sstevel@tonic-gate linelen = fromstate = 0;
6807c478bd9Sstevel@tonic-gate bp = buf;
6817c478bd9Sstevel@tonic-gate if (c2 == '.')
6827c478bd9Sstevel@tonic-gate {
6837c478bd9Sstevel@tonic-gate *bp++ = '.';
6847c478bd9Sstevel@tonic-gate linelen++;
6857c478bd9Sstevel@tonic-gate }
6867c478bd9Sstevel@tonic-gate }
6877c478bd9Sstevel@tonic-gate if (bitnset(bitidx(c1), badchars))
6887c478bd9Sstevel@tonic-gate {
6897c478bd9Sstevel@tonic-gate *bp++ = '=';
6907c478bd9Sstevel@tonic-gate *bp++ = Base16Code[(c1 >> 4) & 0x0f];
6917c478bd9Sstevel@tonic-gate *bp++ = Base16Code[c1 & 0x0f];
6927c478bd9Sstevel@tonic-gate linelen += 3;
6937c478bd9Sstevel@tonic-gate }
6947c478bd9Sstevel@tonic-gate else if (c1 != ' ' && c1 != '\t')
6957c478bd9Sstevel@tonic-gate {
6967c478bd9Sstevel@tonic-gate if (linelen < 4 && c1 == "From"[linelen])
6977c478bd9Sstevel@tonic-gate fromstate++;
6987c478bd9Sstevel@tonic-gate *bp++ = c1;
6997c478bd9Sstevel@tonic-gate linelen++;
7007c478bd9Sstevel@tonic-gate }
7017c478bd9Sstevel@tonic-gate c2 = c1;
7027c478bd9Sstevel@tonic-gate }
7037c478bd9Sstevel@tonic-gate
7047c478bd9Sstevel@tonic-gate /* output any saved character */
7057c478bd9Sstevel@tonic-gate if (c2 == ' ' || c2 == '\t')
7067c478bd9Sstevel@tonic-gate {
7077c478bd9Sstevel@tonic-gate *bp++ = '=';
7087c478bd9Sstevel@tonic-gate *bp++ = Base16Code[(c2 >> 4) & 0x0f];
7097c478bd9Sstevel@tonic-gate *bp++ = Base16Code[c2 & 0x0f];
7107c478bd9Sstevel@tonic-gate linelen += 3;
7117c478bd9Sstevel@tonic-gate }
7127c478bd9Sstevel@tonic-gate
7137c478bd9Sstevel@tonic-gate if (linelen > 0 || boundaries[0] != NULL)
7147c478bd9Sstevel@tonic-gate {
7157c478bd9Sstevel@tonic-gate *bp = '\0';
716445f2479Sjbeck if (!putline(buf, mci))
717445f2479Sjbeck goto writeerr;
7187c478bd9Sstevel@tonic-gate }
7197c478bd9Sstevel@tonic-gate
7207c478bd9Sstevel@tonic-gate }
7217c478bd9Sstevel@tonic-gate if (tTd(43, 3))
7227c478bd9Sstevel@tonic-gate sm_dprintf("\t\t\tmime8to7=>%s (basic)\n", MimeBoundaryNames[bt]);
7237c478bd9Sstevel@tonic-gate return bt;
724445f2479Sjbeck
725445f2479Sjbeck writeerr:
726445f2479Sjbeck return SM_IO_EOF;
7277c478bd9Sstevel@tonic-gate }
7287c478bd9Sstevel@tonic-gate /*
7297c478bd9Sstevel@tonic-gate ** MIME_GETCHAR -- get a character for MIME processing
7307c478bd9Sstevel@tonic-gate **
7317c478bd9Sstevel@tonic-gate ** Treats boundaries as SM_IO_EOF.
7327c478bd9Sstevel@tonic-gate **
7337c478bd9Sstevel@tonic-gate ** Parameters:
7347c478bd9Sstevel@tonic-gate ** fp -- the input file.
7357c478bd9Sstevel@tonic-gate ** boundaries -- the current MIME boundaries.
7367c478bd9Sstevel@tonic-gate ** btp -- if the return value is SM_IO_EOF, *btp is set to
7377c478bd9Sstevel@tonic-gate ** the type of the boundary.
7387c478bd9Sstevel@tonic-gate **
7397c478bd9Sstevel@tonic-gate ** Returns:
7407c478bd9Sstevel@tonic-gate ** The next character in the input stream.
7417c478bd9Sstevel@tonic-gate */
7427c478bd9Sstevel@tonic-gate
7437c478bd9Sstevel@tonic-gate static int
mime_getchar(fp,boundaries,btp)7447c478bd9Sstevel@tonic-gate mime_getchar(fp, boundaries, btp)
7457c478bd9Sstevel@tonic-gate register SM_FILE_T *fp;
7467c478bd9Sstevel@tonic-gate char **boundaries;
7477c478bd9Sstevel@tonic-gate int *btp;
7487c478bd9Sstevel@tonic-gate {
7497c478bd9Sstevel@tonic-gate int c;
7507c478bd9Sstevel@tonic-gate static unsigned char *bp = NULL;
7517c478bd9Sstevel@tonic-gate static int buflen = 0;
7527c478bd9Sstevel@tonic-gate static bool atbol = true; /* at beginning of line */
7537c478bd9Sstevel@tonic-gate static int bt = MBT_SYNTAX; /* boundary type of next SM_IO_EOF */
7547c478bd9Sstevel@tonic-gate static unsigned char buf[128]; /* need not be a full line */
7557c478bd9Sstevel@tonic-gate int start = 0; /* indicates position of - in buffer */
7567c478bd9Sstevel@tonic-gate
7577c478bd9Sstevel@tonic-gate if (buflen == 1 && *bp == '\n')
7587c478bd9Sstevel@tonic-gate {
7597c478bd9Sstevel@tonic-gate /* last \n in buffer may be part of next MIME boundary */
7607c478bd9Sstevel@tonic-gate c = *bp;
7617c478bd9Sstevel@tonic-gate }
7627c478bd9Sstevel@tonic-gate else if (buflen > 0)
7637c478bd9Sstevel@tonic-gate {
7647c478bd9Sstevel@tonic-gate buflen--;
7657c478bd9Sstevel@tonic-gate return *bp++;
7667c478bd9Sstevel@tonic-gate }
7677c478bd9Sstevel@tonic-gate else
7687c478bd9Sstevel@tonic-gate c = sm_io_getc(fp, SM_TIME_DEFAULT);
7697c478bd9Sstevel@tonic-gate bp = buf;
7707c478bd9Sstevel@tonic-gate buflen = 0;
7717c478bd9Sstevel@tonic-gate if (c == '\n')
7727c478bd9Sstevel@tonic-gate {
7737c478bd9Sstevel@tonic-gate /* might be part of a MIME boundary */
7747c478bd9Sstevel@tonic-gate *bp++ = c;
7757c478bd9Sstevel@tonic-gate atbol = true;
7767c478bd9Sstevel@tonic-gate c = sm_io_getc(fp, SM_TIME_DEFAULT);
7777c478bd9Sstevel@tonic-gate if (c == '\n')
7787c478bd9Sstevel@tonic-gate {
7797c478bd9Sstevel@tonic-gate (void) sm_io_ungetc(fp, SM_TIME_DEFAULT, c);
7807c478bd9Sstevel@tonic-gate return c;
7817c478bd9Sstevel@tonic-gate }
7827c478bd9Sstevel@tonic-gate start = 1;
7837c478bd9Sstevel@tonic-gate }
7847c478bd9Sstevel@tonic-gate if (c != SM_IO_EOF)
7857c478bd9Sstevel@tonic-gate *bp++ = c;
7867c478bd9Sstevel@tonic-gate else
7877c478bd9Sstevel@tonic-gate bt = MBT_FINAL;
7887c478bd9Sstevel@tonic-gate if (atbol && c == '-')
7897c478bd9Sstevel@tonic-gate {
7907c478bd9Sstevel@tonic-gate /* check for a message boundary */
7917c478bd9Sstevel@tonic-gate c = sm_io_getc(fp, SM_TIME_DEFAULT);
7927c478bd9Sstevel@tonic-gate if (c != '-')
7937c478bd9Sstevel@tonic-gate {
7947c478bd9Sstevel@tonic-gate if (c != SM_IO_EOF)
7957c478bd9Sstevel@tonic-gate *bp++ = c;
7967c478bd9Sstevel@tonic-gate else
7977c478bd9Sstevel@tonic-gate bt = MBT_FINAL;
7987c478bd9Sstevel@tonic-gate buflen = bp - buf - 1;
7997c478bd9Sstevel@tonic-gate bp = buf;
8007c478bd9Sstevel@tonic-gate return *bp++;
8017c478bd9Sstevel@tonic-gate }
8027c478bd9Sstevel@tonic-gate
8037c478bd9Sstevel@tonic-gate /* got "--", now check for rest of separator */
8047c478bd9Sstevel@tonic-gate *bp++ = '-';
805058561cbSjbeck while (bp < &buf[sizeof(buf) - 2] &&
8067c478bd9Sstevel@tonic-gate (c = sm_io_getc(fp, SM_TIME_DEFAULT)) != SM_IO_EOF &&
8077c478bd9Sstevel@tonic-gate c != '\n')
8087c478bd9Sstevel@tonic-gate {
8097c478bd9Sstevel@tonic-gate *bp++ = c;
8107c478bd9Sstevel@tonic-gate }
8117c478bd9Sstevel@tonic-gate *bp = '\0'; /* XXX simply cut off? */
8127c478bd9Sstevel@tonic-gate bt = mimeboundary((char *) &buf[start], boundaries);
8137c478bd9Sstevel@tonic-gate switch (bt)
8147c478bd9Sstevel@tonic-gate {
8157c478bd9Sstevel@tonic-gate case MBT_FINAL:
8167c478bd9Sstevel@tonic-gate case MBT_INTERMED:
8177c478bd9Sstevel@tonic-gate /* we have a message boundary */
8187c478bd9Sstevel@tonic-gate buflen = 0;
8197c478bd9Sstevel@tonic-gate *btp = bt;
8207c478bd9Sstevel@tonic-gate return SM_IO_EOF;
8217c478bd9Sstevel@tonic-gate }
8227c478bd9Sstevel@tonic-gate
823058561cbSjbeck if (bp < &buf[sizeof(buf) - 2] && c != SM_IO_EOF)
8247c478bd9Sstevel@tonic-gate *bp++ = c;
8257c478bd9Sstevel@tonic-gate }
8267c478bd9Sstevel@tonic-gate
8277c478bd9Sstevel@tonic-gate atbol = c == '\n';
8287c478bd9Sstevel@tonic-gate buflen = bp - buf - 1;
8297c478bd9Sstevel@tonic-gate if (buflen < 0)
8307c478bd9Sstevel@tonic-gate {
8317c478bd9Sstevel@tonic-gate *btp = bt;
8327c478bd9Sstevel@tonic-gate return SM_IO_EOF;
8337c478bd9Sstevel@tonic-gate }
8347c478bd9Sstevel@tonic-gate bp = buf;
8357c478bd9Sstevel@tonic-gate return *bp++;
8367c478bd9Sstevel@tonic-gate }
8377c478bd9Sstevel@tonic-gate /*
8387c478bd9Sstevel@tonic-gate ** MIME_GETCHAR_CRLF -- do mime_getchar, but translate NL => CRLF
8397c478bd9Sstevel@tonic-gate **
8407c478bd9Sstevel@tonic-gate ** Parameters:
8417c478bd9Sstevel@tonic-gate ** fp -- the input file.
8427c478bd9Sstevel@tonic-gate ** boundaries -- the current MIME boundaries.
8437c478bd9Sstevel@tonic-gate ** btp -- if the return value is SM_IO_EOF, *btp is set to
8447c478bd9Sstevel@tonic-gate ** the type of the boundary.
8457c478bd9Sstevel@tonic-gate **
8467c478bd9Sstevel@tonic-gate ** Returns:
8477c478bd9Sstevel@tonic-gate ** The next character in the input stream.
8487c478bd9Sstevel@tonic-gate */
8497c478bd9Sstevel@tonic-gate
8507c478bd9Sstevel@tonic-gate static int
mime_getchar_crlf(fp,boundaries,btp)8517c478bd9Sstevel@tonic-gate mime_getchar_crlf(fp, boundaries, btp)
8527c478bd9Sstevel@tonic-gate register SM_FILE_T *fp;
8537c478bd9Sstevel@tonic-gate char **boundaries;
8547c478bd9Sstevel@tonic-gate int *btp;
8557c478bd9Sstevel@tonic-gate {
8567c478bd9Sstevel@tonic-gate static bool sendlf = false;
8577c478bd9Sstevel@tonic-gate int c;
8587c478bd9Sstevel@tonic-gate
8597c478bd9Sstevel@tonic-gate if (sendlf)
8607c478bd9Sstevel@tonic-gate {
8617c478bd9Sstevel@tonic-gate sendlf = false;
8627c478bd9Sstevel@tonic-gate return '\n';
8637c478bd9Sstevel@tonic-gate }
8647c478bd9Sstevel@tonic-gate c = mime_getchar(fp, boundaries, btp);
8657c478bd9Sstevel@tonic-gate if (c == '\n' && MapNLtoCRLF)
8667c478bd9Sstevel@tonic-gate {
8677c478bd9Sstevel@tonic-gate sendlf = true;
8687c478bd9Sstevel@tonic-gate return '\r';
8697c478bd9Sstevel@tonic-gate }
8707c478bd9Sstevel@tonic-gate return c;
8717c478bd9Sstevel@tonic-gate }
8727c478bd9Sstevel@tonic-gate /*
8737c478bd9Sstevel@tonic-gate ** MIMEBOUNDARY -- determine if this line is a MIME boundary & its type
8747c478bd9Sstevel@tonic-gate **
8757c478bd9Sstevel@tonic-gate ** Parameters:
8767c478bd9Sstevel@tonic-gate ** line -- the input line.
8777c478bd9Sstevel@tonic-gate ** boundaries -- the set of currently pending boundaries.
8787c478bd9Sstevel@tonic-gate **
8797c478bd9Sstevel@tonic-gate ** Returns:
8807c478bd9Sstevel@tonic-gate ** MBT_NOTSEP -- if this is not a separator line
8817c478bd9Sstevel@tonic-gate ** MBT_INTERMED -- if this is an intermediate separator
8827c478bd9Sstevel@tonic-gate ** MBT_FINAL -- if this is a final boundary
8837c478bd9Sstevel@tonic-gate ** MBT_SYNTAX -- if this is a boundary for the wrong
8847c478bd9Sstevel@tonic-gate ** enclosure -- i.e., a syntax error.
8857c478bd9Sstevel@tonic-gate */
8867c478bd9Sstevel@tonic-gate
8877c478bd9Sstevel@tonic-gate static int
mimeboundary(line,boundaries)8887c478bd9Sstevel@tonic-gate mimeboundary(line, boundaries)
8897c478bd9Sstevel@tonic-gate register char *line;
8907c478bd9Sstevel@tonic-gate char **boundaries;
8917c478bd9Sstevel@tonic-gate {
8927c478bd9Sstevel@tonic-gate int type = MBT_NOTSEP;
8937c478bd9Sstevel@tonic-gate int i;
8947c478bd9Sstevel@tonic-gate int savec;
8957c478bd9Sstevel@tonic-gate
8967c478bd9Sstevel@tonic-gate if (line[0] != '-' || line[1] != '-' || boundaries == NULL)
8977c478bd9Sstevel@tonic-gate return MBT_NOTSEP;
8987c478bd9Sstevel@tonic-gate i = strlen(line);
8997c478bd9Sstevel@tonic-gate if (i > 0 && line[i - 1] == '\n')
9007c478bd9Sstevel@tonic-gate i--;
9017c478bd9Sstevel@tonic-gate
9027c478bd9Sstevel@tonic-gate /* strip off trailing whitespace */
9037c478bd9Sstevel@tonic-gate while (i > 0 && (line[i - 1] == ' ' || line[i - 1] == '\t'
9047c478bd9Sstevel@tonic-gate #if _FFR_MIME_CR_OK
9057c478bd9Sstevel@tonic-gate || line[i - 1] == '\r'
9067c478bd9Sstevel@tonic-gate #endif /* _FFR_MIME_CR_OK */
9077c478bd9Sstevel@tonic-gate ))
9087c478bd9Sstevel@tonic-gate i--;
9097c478bd9Sstevel@tonic-gate savec = line[i];
9107c478bd9Sstevel@tonic-gate line[i] = '\0';
9117c478bd9Sstevel@tonic-gate
9127c478bd9Sstevel@tonic-gate if (tTd(43, 5))
9137c478bd9Sstevel@tonic-gate sm_dprintf("mimeboundary: line=\"%s\"... ", line);
9147c478bd9Sstevel@tonic-gate
9157c478bd9Sstevel@tonic-gate /* check for this as an intermediate boundary */
9167c478bd9Sstevel@tonic-gate if (isboundary(&line[2], boundaries) >= 0)
9177c478bd9Sstevel@tonic-gate type = MBT_INTERMED;
9187c478bd9Sstevel@tonic-gate else if (i > 2 && strncmp(&line[i - 2], "--", 2) == 0)
9197c478bd9Sstevel@tonic-gate {
9207c478bd9Sstevel@tonic-gate /* check for a final boundary */
9217c478bd9Sstevel@tonic-gate line[i - 2] = '\0';
9227c478bd9Sstevel@tonic-gate if (isboundary(&line[2], boundaries) >= 0)
9237c478bd9Sstevel@tonic-gate type = MBT_FINAL;
9247c478bd9Sstevel@tonic-gate line[i - 2] = '-';
9257c478bd9Sstevel@tonic-gate }
9267c478bd9Sstevel@tonic-gate
9277c478bd9Sstevel@tonic-gate line[i] = savec;
9287c478bd9Sstevel@tonic-gate if (tTd(43, 5))
9297c478bd9Sstevel@tonic-gate sm_dprintf("%s\n", MimeBoundaryNames[type]);
9307c478bd9Sstevel@tonic-gate return type;
9317c478bd9Sstevel@tonic-gate }
9327c478bd9Sstevel@tonic-gate /*
9337c478bd9Sstevel@tonic-gate ** DEFCHARSET -- return default character set for message
9347c478bd9Sstevel@tonic-gate **
9357c478bd9Sstevel@tonic-gate ** The first choice for character set is for the mailer
9367c478bd9Sstevel@tonic-gate ** corresponding to the envelope sender. If neither that
9377c478bd9Sstevel@tonic-gate ** nor the global configuration file has a default character
9387c478bd9Sstevel@tonic-gate ** set defined, return "unknown-8bit" as recommended by
9397c478bd9Sstevel@tonic-gate ** RFC 1428 section 3.
9407c478bd9Sstevel@tonic-gate **
9417c478bd9Sstevel@tonic-gate ** Parameters:
9427c478bd9Sstevel@tonic-gate ** e -- the envelope for this message.
9437c478bd9Sstevel@tonic-gate **
9447c478bd9Sstevel@tonic-gate ** Returns:
9457c478bd9Sstevel@tonic-gate ** The default character set for that mailer.
9467c478bd9Sstevel@tonic-gate */
9477c478bd9Sstevel@tonic-gate
9487c478bd9Sstevel@tonic-gate char *
defcharset(e)9497c478bd9Sstevel@tonic-gate defcharset(e)
9507c478bd9Sstevel@tonic-gate register ENVELOPE *e;
9517c478bd9Sstevel@tonic-gate {
9527c478bd9Sstevel@tonic-gate if (e != NULL && e->e_from.q_mailer != NULL &&
9537c478bd9Sstevel@tonic-gate e->e_from.q_mailer->m_defcharset != NULL)
9547c478bd9Sstevel@tonic-gate return e->e_from.q_mailer->m_defcharset;
9557c478bd9Sstevel@tonic-gate if (DefaultCharSet != NULL)
9567c478bd9Sstevel@tonic-gate return DefaultCharSet;
9577c478bd9Sstevel@tonic-gate return "unknown-8bit";
9587c478bd9Sstevel@tonic-gate }
9597c478bd9Sstevel@tonic-gate /*
9607c478bd9Sstevel@tonic-gate ** ISBOUNDARY -- is a given string a currently valid boundary?
9617c478bd9Sstevel@tonic-gate **
9627c478bd9Sstevel@tonic-gate ** Parameters:
9637c478bd9Sstevel@tonic-gate ** line -- the current input line.
9647c478bd9Sstevel@tonic-gate ** boundaries -- the list of valid boundaries.
9657c478bd9Sstevel@tonic-gate **
9667c478bd9Sstevel@tonic-gate ** Returns:
9677c478bd9Sstevel@tonic-gate ** The index number in boundaries if the line is found.
9687c478bd9Sstevel@tonic-gate ** -1 -- otherwise.
9697c478bd9Sstevel@tonic-gate **
9707c478bd9Sstevel@tonic-gate */
9717c478bd9Sstevel@tonic-gate
9727c478bd9Sstevel@tonic-gate static int
isboundary(line,boundaries)9737c478bd9Sstevel@tonic-gate isboundary(line, boundaries)
9747c478bd9Sstevel@tonic-gate char *line;
9757c478bd9Sstevel@tonic-gate char **boundaries;
9767c478bd9Sstevel@tonic-gate {
9777c478bd9Sstevel@tonic-gate register int i;
9787c478bd9Sstevel@tonic-gate
9797c478bd9Sstevel@tonic-gate for (i = 0; i <= MAXMIMENESTING && boundaries[i] != NULL; i++)
9807c478bd9Sstevel@tonic-gate {
9817c478bd9Sstevel@tonic-gate if (strcmp(line, boundaries[i]) == 0)
9827c478bd9Sstevel@tonic-gate return i;
9837c478bd9Sstevel@tonic-gate }
9847c478bd9Sstevel@tonic-gate return -1;
9857c478bd9Sstevel@tonic-gate }
9867c478bd9Sstevel@tonic-gate #endif /* MIME8TO7 */
9877c478bd9Sstevel@tonic-gate
9887c478bd9Sstevel@tonic-gate #if MIME7TO8
9897c478bd9Sstevel@tonic-gate static int mime_fromqp __P((unsigned char *, unsigned char **, int));
9907c478bd9Sstevel@tonic-gate
9917c478bd9Sstevel@tonic-gate /*
9927c478bd9Sstevel@tonic-gate ** MIME7TO8 -- output 7 bit encoded MIME body in 8 bit format
9937c478bd9Sstevel@tonic-gate **
9947c478bd9Sstevel@tonic-gate ** This is a hack. Supports translating the two 7-bit body-encodings
9957c478bd9Sstevel@tonic-gate ** (quoted-printable and base64) to 8-bit coded bodies.
9967c478bd9Sstevel@tonic-gate **
9977c478bd9Sstevel@tonic-gate ** There is not much point in supporting multipart here, as the UA
9987c478bd9Sstevel@tonic-gate ** will be able to deal with encoded MIME bodies if it can parse MIME
9997c478bd9Sstevel@tonic-gate ** multipart messages.
10007c478bd9Sstevel@tonic-gate **
10017c478bd9Sstevel@tonic-gate ** Note also that we won't be called unless it is a text/plain MIME
10027c478bd9Sstevel@tonic-gate ** message, encoded base64 or QP and mailer flag '9' has been defined
10037c478bd9Sstevel@tonic-gate ** on mailer.
10047c478bd9Sstevel@tonic-gate **
10057c478bd9Sstevel@tonic-gate ** Contributed by Marius Olaffson <marius@rhi.hi.is>.
10067c478bd9Sstevel@tonic-gate **
10077c478bd9Sstevel@tonic-gate ** Parameters:
10087c478bd9Sstevel@tonic-gate ** mci -- mailer connection information.
10097c478bd9Sstevel@tonic-gate ** header -- the header for this body part.
10107c478bd9Sstevel@tonic-gate ** e -- envelope.
10117c478bd9Sstevel@tonic-gate **
10127c478bd9Sstevel@tonic-gate ** Returns:
1013445f2479Sjbeck ** true iff body was written successfully
10147c478bd9Sstevel@tonic-gate */
10157c478bd9Sstevel@tonic-gate
10167c478bd9Sstevel@tonic-gate static char index_64[128] =
10177c478bd9Sstevel@tonic-gate {
10187c478bd9Sstevel@tonic-gate -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
10197c478bd9Sstevel@tonic-gate -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
10207c478bd9Sstevel@tonic-gate -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,62, -1,-1,-1,63,
10217c478bd9Sstevel@tonic-gate 52,53,54,55, 56,57,58,59, 60,61,-1,-1, -1,-1,-1,-1,
10227c478bd9Sstevel@tonic-gate -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10, 11,12,13,14,
10237c478bd9Sstevel@tonic-gate 15,16,17,18, 19,20,21,22, 23,24,25,-1, -1,-1,-1,-1,
10247c478bd9Sstevel@tonic-gate -1,26,27,28, 29,30,31,32, 33,34,35,36, 37,38,39,40,
10257c478bd9Sstevel@tonic-gate 41,42,43,44, 45,46,47,48, 49,50,51,-1, -1,-1,-1,-1
10267c478bd9Sstevel@tonic-gate };
10277c478bd9Sstevel@tonic-gate
10287c478bd9Sstevel@tonic-gate # define CHAR64(c) (((c) < 0 || (c) > 127) ? -1 : index_64[(c)])
10297c478bd9Sstevel@tonic-gate
1030445f2479Sjbeck bool
mime7to8(mci,header,e)10317c478bd9Sstevel@tonic-gate mime7to8(mci, header, e)
10327c478bd9Sstevel@tonic-gate register MCI *mci;
10337c478bd9Sstevel@tonic-gate HDR *header;
10347c478bd9Sstevel@tonic-gate register ENVELOPE *e;
10357c478bd9Sstevel@tonic-gate {
10367c478bd9Sstevel@tonic-gate int pxflags;
10377c478bd9Sstevel@tonic-gate register char *p;
10387c478bd9Sstevel@tonic-gate char *cte;
10397c478bd9Sstevel@tonic-gate char **pvp;
10407c478bd9Sstevel@tonic-gate unsigned char *fbufp;
10417c478bd9Sstevel@tonic-gate char buf[MAXLINE];
10427c478bd9Sstevel@tonic-gate unsigned char fbuf[MAXLINE + 1];
10437c478bd9Sstevel@tonic-gate char pvpbuf[MAXLINE];
10447c478bd9Sstevel@tonic-gate extern unsigned char MimeTokenTab[256];
10457c478bd9Sstevel@tonic-gate
10467c478bd9Sstevel@tonic-gate p = hvalue("Content-Transfer-Encoding", header);
10477c478bd9Sstevel@tonic-gate if (p == NULL ||
1048058561cbSjbeck (pvp = prescan(p, '\0', pvpbuf, sizeof(pvpbuf), NULL,
10497c478bd9Sstevel@tonic-gate MimeTokenTab, false)) == NULL ||
10507c478bd9Sstevel@tonic-gate pvp[0] == NULL)
10517c478bd9Sstevel@tonic-gate {
10527c478bd9Sstevel@tonic-gate /* "can't happen" -- upper level should have caught this */
10537c478bd9Sstevel@tonic-gate syserr("mime7to8: unparsable CTE %s", p == NULL ? "<NULL>" : p);
10547c478bd9Sstevel@tonic-gate
10557c478bd9Sstevel@tonic-gate /* avoid bounce loops */
10567c478bd9Sstevel@tonic-gate e->e_flags |= EF_DONT_MIME;
10577c478bd9Sstevel@tonic-gate
10587c478bd9Sstevel@tonic-gate /* cheap failsafe algorithm -- should work on text/plain */
10597c478bd9Sstevel@tonic-gate if (p != NULL)
10607c478bd9Sstevel@tonic-gate {
1061058561cbSjbeck (void) sm_snprintf(buf, sizeof(buf),
10627c478bd9Sstevel@tonic-gate "Content-Transfer-Encoding: %s", p);
1063445f2479Sjbeck if (!putline(buf, mci))
1064445f2479Sjbeck goto writeerr;
10657c478bd9Sstevel@tonic-gate }
1066445f2479Sjbeck if (!putline("", mci))
1067445f2479Sjbeck goto writeerr;
10687c478bd9Sstevel@tonic-gate mci->mci_flags &= ~MCIF_INHEADER;
1069058561cbSjbeck while (sm_io_fgets(e->e_dfp, SM_TIME_DEFAULT, buf, sizeof(buf))
10707c478bd9Sstevel@tonic-gate != NULL)
1071445f2479Sjbeck {
1072445f2479Sjbeck if (!putline(buf, mci))
1073445f2479Sjbeck goto writeerr;
1074445f2479Sjbeck }
1075445f2479Sjbeck return true;
10767c478bd9Sstevel@tonic-gate }
1077058561cbSjbeck cataddr(pvp, NULL, buf, sizeof(buf), '\0', false);
10787c478bd9Sstevel@tonic-gate cte = sm_rpool_strdup_x(e->e_rpool, buf);
10797c478bd9Sstevel@tonic-gate
10807c478bd9Sstevel@tonic-gate mci->mci_flags |= MCIF_INHEADER;
1081445f2479Sjbeck if (!putline("Content-Transfer-Encoding: 8bit", mci))
1082445f2479Sjbeck goto writeerr;
1083058561cbSjbeck (void) sm_snprintf(buf, sizeof(buf),
10847c478bd9Sstevel@tonic-gate "X-MIME-Autoconverted: from %.200s to 8bit by %s id %s",
10857c478bd9Sstevel@tonic-gate cte, MyHostName, e->e_id);
1086445f2479Sjbeck if (!putline(buf, mci) || !putline("", mci))
1087445f2479Sjbeck goto writeerr;
10887c478bd9Sstevel@tonic-gate mci->mci_flags &= ~MCIF_INHEADER;
10897c478bd9Sstevel@tonic-gate
10907c478bd9Sstevel@tonic-gate /*
10917c478bd9Sstevel@tonic-gate ** Translate body encoding to 8-bit. Supports two types of
10927c478bd9Sstevel@tonic-gate ** encodings; "base64" and "quoted-printable". Assume qp if
10937c478bd9Sstevel@tonic-gate ** it is not base64.
10947c478bd9Sstevel@tonic-gate */
10957c478bd9Sstevel@tonic-gate
10967c478bd9Sstevel@tonic-gate pxflags = PXLF_MAPFROM;
10977c478bd9Sstevel@tonic-gate if (sm_strcasecmp(cte, "base64") == 0)
10987c478bd9Sstevel@tonic-gate {
10997c478bd9Sstevel@tonic-gate int c1, c2, c3, c4;
11007c478bd9Sstevel@tonic-gate
11017c478bd9Sstevel@tonic-gate fbufp = fbuf;
11027c478bd9Sstevel@tonic-gate while ((c1 = sm_io_getc(e->e_dfp, SM_TIME_DEFAULT)) !=
11037c478bd9Sstevel@tonic-gate SM_IO_EOF)
11047c478bd9Sstevel@tonic-gate {
11057c478bd9Sstevel@tonic-gate if (isascii(c1) && isspace(c1))
11067c478bd9Sstevel@tonic-gate continue;
11077c478bd9Sstevel@tonic-gate
11087c478bd9Sstevel@tonic-gate do
11097c478bd9Sstevel@tonic-gate {
11107c478bd9Sstevel@tonic-gate c2 = sm_io_getc(e->e_dfp, SM_TIME_DEFAULT);
11117c478bd9Sstevel@tonic-gate } while (isascii(c2) && isspace(c2));
11127c478bd9Sstevel@tonic-gate if (c2 == SM_IO_EOF)
11137c478bd9Sstevel@tonic-gate break;
11147c478bd9Sstevel@tonic-gate
11157c478bd9Sstevel@tonic-gate do
11167c478bd9Sstevel@tonic-gate {
11177c478bd9Sstevel@tonic-gate c3 = sm_io_getc(e->e_dfp, SM_TIME_DEFAULT);
11187c478bd9Sstevel@tonic-gate } while (isascii(c3) && isspace(c3));
11197c478bd9Sstevel@tonic-gate if (c3 == SM_IO_EOF)
11207c478bd9Sstevel@tonic-gate break;
11217c478bd9Sstevel@tonic-gate
11227c478bd9Sstevel@tonic-gate do
11237c478bd9Sstevel@tonic-gate {
11247c478bd9Sstevel@tonic-gate c4 = sm_io_getc(e->e_dfp, SM_TIME_DEFAULT);
11257c478bd9Sstevel@tonic-gate } while (isascii(c4) && isspace(c4));
11267c478bd9Sstevel@tonic-gate if (c4 == SM_IO_EOF)
11277c478bd9Sstevel@tonic-gate break;
11287c478bd9Sstevel@tonic-gate
11297c478bd9Sstevel@tonic-gate if (c1 == '=' || c2 == '=')
11307c478bd9Sstevel@tonic-gate continue;
11317c478bd9Sstevel@tonic-gate c1 = CHAR64(c1);
11327c478bd9Sstevel@tonic-gate c2 = CHAR64(c2);
11337c478bd9Sstevel@tonic-gate
11347c478bd9Sstevel@tonic-gate #if MIME7TO8_OLD
11357c478bd9Sstevel@tonic-gate #define CHK_EOL if (*--fbufp != '\n' || (fbufp > fbuf && *--fbufp != '\r')) \
11367c478bd9Sstevel@tonic-gate ++fbufp;
11377c478bd9Sstevel@tonic-gate #else /* MIME7TO8_OLD */
11387c478bd9Sstevel@tonic-gate #define CHK_EOL if (*--fbufp != '\n' || (fbufp > fbuf && *--fbufp != '\r')) \
11397c478bd9Sstevel@tonic-gate { \
11407c478bd9Sstevel@tonic-gate ++fbufp; \
11417c478bd9Sstevel@tonic-gate pxflags |= PXLF_NOADDEOL; \
11427c478bd9Sstevel@tonic-gate }
11437c478bd9Sstevel@tonic-gate #endif /* MIME7TO8_OLD */
11447c478bd9Sstevel@tonic-gate
11457c478bd9Sstevel@tonic-gate #define PUTLINE64 \
11467c478bd9Sstevel@tonic-gate do \
11477c478bd9Sstevel@tonic-gate { \
11487c478bd9Sstevel@tonic-gate if (*fbufp++ == '\n' || fbufp >= &fbuf[MAXLINE]) \
11497c478bd9Sstevel@tonic-gate { \
11507c478bd9Sstevel@tonic-gate CHK_EOL; \
1151445f2479Sjbeck if (!putxline((char *) fbuf, fbufp - fbuf, mci, pxflags)) \
1152445f2479Sjbeck goto writeerr; \
11537c478bd9Sstevel@tonic-gate pxflags &= ~PXLF_NOADDEOL; \
11547c478bd9Sstevel@tonic-gate fbufp = fbuf; \
11557c478bd9Sstevel@tonic-gate } \
11567c478bd9Sstevel@tonic-gate } while (0)
11577c478bd9Sstevel@tonic-gate
11587c478bd9Sstevel@tonic-gate *fbufp = (c1 << 2) | ((c2 & 0x30) >> 4);
11597c478bd9Sstevel@tonic-gate PUTLINE64;
11607c478bd9Sstevel@tonic-gate if (c3 == '=')
11617c478bd9Sstevel@tonic-gate continue;
11627c478bd9Sstevel@tonic-gate c3 = CHAR64(c3);
11637c478bd9Sstevel@tonic-gate *fbufp = ((c2 & 0x0f) << 4) | ((c3 & 0x3c) >> 2);
11647c478bd9Sstevel@tonic-gate PUTLINE64;
11657c478bd9Sstevel@tonic-gate if (c4 == '=')
11667c478bd9Sstevel@tonic-gate continue;
11677c478bd9Sstevel@tonic-gate c4 = CHAR64(c4);
11687c478bd9Sstevel@tonic-gate *fbufp = ((c3 & 0x03) << 6) | c4;
11697c478bd9Sstevel@tonic-gate PUTLINE64;
11707c478bd9Sstevel@tonic-gate }
11717c478bd9Sstevel@tonic-gate }
11727c478bd9Sstevel@tonic-gate else
11737c478bd9Sstevel@tonic-gate {
11747c478bd9Sstevel@tonic-gate int off;
11757c478bd9Sstevel@tonic-gate
11767c478bd9Sstevel@tonic-gate /* quoted-printable */
11777c478bd9Sstevel@tonic-gate pxflags |= PXLF_NOADDEOL;
11787c478bd9Sstevel@tonic-gate fbufp = fbuf;
11797c478bd9Sstevel@tonic-gate while (sm_io_fgets(e->e_dfp, SM_TIME_DEFAULT, buf,
1180058561cbSjbeck sizeof(buf)) != NULL)
11817c478bd9Sstevel@tonic-gate {
11827c478bd9Sstevel@tonic-gate off = mime_fromqp((unsigned char *) buf, &fbufp,
11837c478bd9Sstevel@tonic-gate &fbuf[MAXLINE] - fbufp);
11847c478bd9Sstevel@tonic-gate again:
11857c478bd9Sstevel@tonic-gate if (off < -1)
11867c478bd9Sstevel@tonic-gate continue;
11877c478bd9Sstevel@tonic-gate
11887c478bd9Sstevel@tonic-gate if (fbufp - fbuf > 0)
1189445f2479Sjbeck {
1190445f2479Sjbeck if (!putxline((char *) fbuf, fbufp - fbuf - 1,
1191445f2479Sjbeck mci, pxflags))
1192445f2479Sjbeck goto writeerr;
1193445f2479Sjbeck }
11947c478bd9Sstevel@tonic-gate fbufp = fbuf;
11957c478bd9Sstevel@tonic-gate if (off >= 0 && buf[off] != '\0')
11967c478bd9Sstevel@tonic-gate {
11977c478bd9Sstevel@tonic-gate off = mime_fromqp((unsigned char *) (buf + off),
11987c478bd9Sstevel@tonic-gate &fbufp,
11997c478bd9Sstevel@tonic-gate &fbuf[MAXLINE] - fbufp);
12007c478bd9Sstevel@tonic-gate goto again;
12017c478bd9Sstevel@tonic-gate }
12027c478bd9Sstevel@tonic-gate }
12037c478bd9Sstevel@tonic-gate }
12047c478bd9Sstevel@tonic-gate
12057c478bd9Sstevel@tonic-gate /* force out partial last line */
12067c478bd9Sstevel@tonic-gate if (fbufp > fbuf)
12077c478bd9Sstevel@tonic-gate {
12087c478bd9Sstevel@tonic-gate *fbufp = '\0';
1209445f2479Sjbeck if (!putxline((char *) fbuf, fbufp - fbuf, mci, pxflags))
1210445f2479Sjbeck goto writeerr;
12117c478bd9Sstevel@tonic-gate }
12127c478bd9Sstevel@tonic-gate
12137c478bd9Sstevel@tonic-gate /*
12147c478bd9Sstevel@tonic-gate ** The decoded text may end without an EOL. Since this function
12157c478bd9Sstevel@tonic-gate ** is only called for text/plain MIME messages, it is safe to
12167c478bd9Sstevel@tonic-gate ** add an extra one at the end just in case. This is a hack,
12177c478bd9Sstevel@tonic-gate ** but so is auto-converting MIME in the first place.
12187c478bd9Sstevel@tonic-gate */
12197c478bd9Sstevel@tonic-gate
1220445f2479Sjbeck if (!putline("", mci))
1221445f2479Sjbeck goto writeerr;
12227c478bd9Sstevel@tonic-gate
12237c478bd9Sstevel@tonic-gate if (tTd(43, 3))
12247c478bd9Sstevel@tonic-gate sm_dprintf("\t\t\tmime7to8 => %s to 8bit done\n", cte);
1225445f2479Sjbeck return true;
1226445f2479Sjbeck
1227445f2479Sjbeck writeerr:
1228445f2479Sjbeck return false;
12297c478bd9Sstevel@tonic-gate }
12307c478bd9Sstevel@tonic-gate /*
12317c478bd9Sstevel@tonic-gate ** The following is based on Borenstein's "codes.c" module, with simplifying
12327c478bd9Sstevel@tonic-gate ** changes as we do not deal with multipart, and to do the translation in-core,
12337c478bd9Sstevel@tonic-gate ** with an attempt to prevent overrun of output buffers.
12347c478bd9Sstevel@tonic-gate **
12357c478bd9Sstevel@tonic-gate ** What is needed here are changes to defend this code better against
12367c478bd9Sstevel@tonic-gate ** bad encodings. Questionable to always return 0xFF for bad mappings.
12377c478bd9Sstevel@tonic-gate */
12387c478bd9Sstevel@tonic-gate
12397c478bd9Sstevel@tonic-gate static char index_hex[128] =
12407c478bd9Sstevel@tonic-gate {
12417c478bd9Sstevel@tonic-gate -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
12427c478bd9Sstevel@tonic-gate -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
12437c478bd9Sstevel@tonic-gate -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
12447c478bd9Sstevel@tonic-gate 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,-1,-1, -1,-1,-1,-1,
12457c478bd9Sstevel@tonic-gate -1,10,11,12, 13,14,15,-1, -1,-1,-1,-1, -1,-1,-1,-1,
12467c478bd9Sstevel@tonic-gate -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
12477c478bd9Sstevel@tonic-gate -1,10,11,12, 13,14,15,-1, -1,-1,-1,-1, -1,-1,-1,-1,
12487c478bd9Sstevel@tonic-gate -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1
12497c478bd9Sstevel@tonic-gate };
12507c478bd9Sstevel@tonic-gate
12517c478bd9Sstevel@tonic-gate # define HEXCHAR(c) (((c) < 0 || (c) > 127) ? -1 : index_hex[(c)])
12527c478bd9Sstevel@tonic-gate
12537c478bd9Sstevel@tonic-gate /*
12547c478bd9Sstevel@tonic-gate ** MIME_FROMQP -- decode quoted printable string
12557c478bd9Sstevel@tonic-gate **
12567c478bd9Sstevel@tonic-gate ** Parameters:
12577c478bd9Sstevel@tonic-gate ** infile -- input (encoded) string
12587c478bd9Sstevel@tonic-gate ** outfile -- output string
12597c478bd9Sstevel@tonic-gate ** maxlen -- size of output buffer
12607c478bd9Sstevel@tonic-gate **
12617c478bd9Sstevel@tonic-gate ** Returns:
12627c478bd9Sstevel@tonic-gate ** -2 if decoding failure
12637c478bd9Sstevel@tonic-gate ** -1 if infile completely decoded into outfile
12647c478bd9Sstevel@tonic-gate ** >= 0 is the position in infile decoding
12657c478bd9Sstevel@tonic-gate ** reached before maxlen was reached
12667c478bd9Sstevel@tonic-gate */
12677c478bd9Sstevel@tonic-gate
12687c478bd9Sstevel@tonic-gate static int
mime_fromqp(infile,outfile,maxlen)12697c478bd9Sstevel@tonic-gate mime_fromqp(infile, outfile, maxlen)
12707c478bd9Sstevel@tonic-gate unsigned char *infile;
12717c478bd9Sstevel@tonic-gate unsigned char **outfile;
12727c478bd9Sstevel@tonic-gate int maxlen; /* Max # of chars allowed in outfile */
12737c478bd9Sstevel@tonic-gate {
12747c478bd9Sstevel@tonic-gate int c1, c2;
12757c478bd9Sstevel@tonic-gate int nchar = 0;
12767c478bd9Sstevel@tonic-gate unsigned char *b;
12777c478bd9Sstevel@tonic-gate
12787c478bd9Sstevel@tonic-gate /* decrement by one for trailing '\0', at least one other char */
12797c478bd9Sstevel@tonic-gate if (--maxlen < 1)
12807c478bd9Sstevel@tonic-gate return 0;
12817c478bd9Sstevel@tonic-gate
12827c478bd9Sstevel@tonic-gate b = infile;
12837c478bd9Sstevel@tonic-gate while ((c1 = *infile++) != '\0' && nchar < maxlen)
12847c478bd9Sstevel@tonic-gate {
12857c478bd9Sstevel@tonic-gate if (c1 == '=')
12867c478bd9Sstevel@tonic-gate {
12877c478bd9Sstevel@tonic-gate if ((c1 = *infile++) == '\0')
12887c478bd9Sstevel@tonic-gate break;
12897c478bd9Sstevel@tonic-gate
12907c478bd9Sstevel@tonic-gate if (c1 == '\n' || (c1 = HEXCHAR(c1)) == -1)
12917c478bd9Sstevel@tonic-gate {
12927c478bd9Sstevel@tonic-gate /* ignore it and the rest of the buffer */
12937c478bd9Sstevel@tonic-gate return -2;
12947c478bd9Sstevel@tonic-gate }
12957c478bd9Sstevel@tonic-gate else
12967c478bd9Sstevel@tonic-gate {
12977c478bd9Sstevel@tonic-gate do
12987c478bd9Sstevel@tonic-gate {
12997c478bd9Sstevel@tonic-gate if ((c2 = *infile++) == '\0')
13007c478bd9Sstevel@tonic-gate {
13017c478bd9Sstevel@tonic-gate c2 = -1;
13027c478bd9Sstevel@tonic-gate break;
13037c478bd9Sstevel@tonic-gate }
13047c478bd9Sstevel@tonic-gate } while ((c2 = HEXCHAR(c2)) == -1);
13057c478bd9Sstevel@tonic-gate
13067c478bd9Sstevel@tonic-gate if (c2 == -1)
13077c478bd9Sstevel@tonic-gate break;
13087c478bd9Sstevel@tonic-gate nchar++;
13097c478bd9Sstevel@tonic-gate *(*outfile)++ = c1 << 4 | c2;
13107c478bd9Sstevel@tonic-gate }
13117c478bd9Sstevel@tonic-gate }
13127c478bd9Sstevel@tonic-gate else
13137c478bd9Sstevel@tonic-gate {
13147c478bd9Sstevel@tonic-gate nchar++;
13157c478bd9Sstevel@tonic-gate *(*outfile)++ = c1;
13167c478bd9Sstevel@tonic-gate if (c1 == '\n')
13177c478bd9Sstevel@tonic-gate break;
13187c478bd9Sstevel@tonic-gate }
13197c478bd9Sstevel@tonic-gate }
13207c478bd9Sstevel@tonic-gate *(*outfile)++ = '\0';
13217c478bd9Sstevel@tonic-gate if (nchar >= maxlen)
13227c478bd9Sstevel@tonic-gate return (infile - b - 1);
13237c478bd9Sstevel@tonic-gate return -1;
13247c478bd9Sstevel@tonic-gate }
13257c478bd9Sstevel@tonic-gate #endif /* MIME7TO8 */
1326