/*
* Copyright (c) 1998-2003, 2006 Sendmail, Inc. and its suppliers.
* All rights reserved.
* Copyright (c) 1994, 1996-1997 Eric P. Allman. All rights reserved.
* Copyright (c) 1994
* The Regents of the University of California. All rights reserved.
*
* By using this file, you agree to the terms and conditions set
* forth in the LICENSE file which can be found at the top level of
* the sendmail distribution.
*
*/
#pragma ident "%Z%%M% %I% %E% SMI"
#include <sendmail.h>
#include <string.h>
/*
** MIME support.
**
** I am indebted to John Beck of Hewlett-Packard, who contributed
** his code to me for inclusion. As it turns out, I did not use
** his code since he used a "minimum change" approach that used
** several temp files, and I wanted a "minimum impact" approach
** that would avoid copying. However, looking over his code
** helped me cement my understanding of the problem.
**
** I also looked at, but did not directly use, Nathaniel
** Borenstein's "code.c" module. Again, it functioned as
** a file-to-file translator, which did not fit within my
** design bounds, but it was a useful base for understanding
** the problem.
*/
/* use "old" mime 7 to 8 algorithm by default */
#ifndef MIME7TO8_OLD
#endif /* ! MIME7TO8_OLD */
#if MIME8TO7
static int isboundary __P((char *, char **));
static int mimeboundary __P((char *, char **));
/* character set for hex and base64 encoding */
/* types of MIME boundaries */
static char *MimeBoundaryNames[] =
{
"SYNTAX", "NOTSEP", "INTERMED", "FINAL"
};
static bool MapNLtoCRLF;
/*
** MIME8TO7 -- output 8 bit body in 7 bit format
**
** The header has already been output -- this has to do the
** 8 to 7 bit conversion. It would be easy if we didn't have
**
** We won't be called if we don't have to do a conversion, and
** appropriate MIME-Version: and Content-Type: fields have been
** output. Any Content-Transfer-Encoding: field has not been
** output, and we can add it here.
**
** Parameters:
** mci -- mailer connection information.
** header -- the header for this body part.
** e -- envelope.
** boundaries -- the currently pending message boundaries.
** NULL if we are processing the outer portion.
** flags -- to tweak processing.
** level -- recursion level.
**
** Returns:
** An indicator of what terminated the message part:
** MBT_FINAL -- the final boundary
** MBT_INTERMED -- an intermediate boundary
** MBT_NOTSEP -- an end of file
** SM_IO_EOF -- I/O error occurred
*/
struct args
{
};
int
register ENVELOPE *e;
char **boundaries;
int flags;
int level;
{
register char *p;
int linelen;
int bt;
int i;
char *type;
char *subtype;
char *cte;
char **pvp;
int argc = 0;
char *bp;
bool use_qp = false;
extern unsigned char MimeTokenTab[256];
if (level > MAXMIMENESTING)
{
{
sm_dprintf("mime8to7: too deep, level=%d\n",
level);
usrerr("mime8to7: recursion level %d exceeded",
level);
}
}
{
if (boundaries[0] == NULL)
sm_dprintf(" <none>");
else
{
for (i = 0; boundaries[i] != NULL; i++)
}
sm_dprintf("\n");
}
MapNLtoCRLF = true;
if (p == NULL ||
MimeTokenTab, false)) == NULL ||
{
}
else
{
}
if (p == NULL)
{
else
}
if (p != NULL &&
MimeTokenTab, false)) != NULL &&
{
{
}
{
}
/* break out parameters */
{
/* skip to semicolon separator */
pvp++;
break;
/* complain about empty values */
{
usrerr("mime8to7: Empty parameter in Content-Type header");
/* avoid bounce loops */
e->e_flags |= EF_DONT_MIME;
continue;
}
/* extract field name */
/* see if there is a value */
{
argc++;
}
}
}
/* check for disaster cases */
type = "-none-";
subtype = "-none-";
/* don't propagate some flags more than one level into the message */
flags &= ~M87F_DIGEST;
/*
** Check for cases that can not be encoded.
**
** For example, you can't encode certain kinds of types
** or already-encoded messages. If we find this case,
** just copy it through.
*/
flags |= M87F_NO8BIT;
# ifdef USE_B_CLASS
MapNLtoCRLF = false;
# endif /* USE_B_CLASS */
use_qp = true;
/*
** Multipart requires special processing.
**
** Do a recursive descent into the message.
*/
)
{
flags |= M87F_DIGEST;
for (i = 0; i < argc; i++)
{
break;
}
{
usrerr("mime8to7: Content-Type: \"%s\": %s boundary",
p = "---";
/* avoid bounce loops */
e->e_flags |= EF_DONT_MIME;
}
else
{
stripquotes(p);
}
{
usrerr("mime8to7: multipart boundary \"%s\" too long",
p);
/* avoid bounce loops */
e->e_flags |= EF_DONT_MIME;
}
sm_dprintf("mime8to7: multipart boundary \"%s\"\n",
bbuf);
for (i = 0; i < MAXMIMENESTING; i++)
{
if (boundaries[i] == NULL)
break;
}
if (i >= MAXMIMENESTING)
{
sm_dprintf("mime8to7: too deep, i=%d\n", i);
usrerr("mime8to7: multipart nesting boundary too deep");
/* avoid bounce loops */
}
else
{
boundaries[i] = bbuf;
}
/* skip the early "comment" prologue */
goto writeerr;
!= NULL)
{
if (bt != MBT_NOTSEP)
break;
goto writeerr;
}
{
goto writeerr;
goto writeerr;
level + 1);
goto writeerr;
}
goto writeerr;
boundaries[i] = NULL;
/* skip the late "comment" epilogue */
!= NULL)
{
if (bt != MBT_NOTSEP)
break;
goto writeerr;
}
sm_dprintf("\t\t\tmime8to7=>%s (multipart)\n",
return bt;
}
/*
**
** Class 's' is predefined to have "rfc822" only.
*/
{
{
flags |= M87F_NO8BIT;
}
else
{
goto writeerr;
goto writeerr;
goto writeerr;
level + 1);
return bt;
}
}
/*
** Non-compound body type
**
** Compute the ratio of seven to eight bit characters;
** use that as a heuristic to decide how to do the
** encoding.
*/
sectionsize = sectionhighbits = 0;
{
/* remember where we were */
if (offset == -1)
syserr("mime8to7: cannot sm_io_tell on %cf%s",
DATAFL_LETTER, e->e_id);
/* do a scan of this body type to count character types */
!= NULL)
{
break;
for (p = buf; *p != '\0'; p++)
{
/* count bytes with the high bit set */
sectionsize++;
if (bitset(0200, *p))
}
/*
** Heuristic: if 1/4 of the first 4K bytes are 8-bit,
** assume base64. This heuristic avoids double-reading
** large graphics or video files.
*/
if (sectionsize >= 4096 &&
break;
}
/* return to the original offset for processing */
/* XXX use relative seeks to handle >31 bit file sizes? */
syserr("mime8to7: cannot sm_io_fseek on %cf%s",
DATAFL_LETTER, e->e_id);
else
sm_io_clearerr(e->e_dfp);
}
/*
** Heuristically determine encoding method.
** If more than 1/8 of the total characters have the
** eighth bit set, use base64; else use quoted-printable.
** However, only encode binary encoded data as base64,
** since otherwise the NL=>CRLF mapping will be a problem.
*/
{
sm_dprintf("mime8to7: %ld high bit(s) in %ld byte(s), cte=%s, type=%s/%s\n",
(long) sectionhighbits, (long) sectionsize,
}
linelen = 0;
if (sectionhighbits == 0)
{
/* no encoding necessary */
{
/*
** Skip _unless_ in MIME mode and potentially
** converting from 8 bit to 7 bit MIME. See
** putheader() for the counterpart where the
** CTE header is skipped in the opposite
** situation.
*/
"Content-Transfer-Encoding: %.200s", cte);
goto writeerr;
}
goto writeerr;
!= NULL)
{
{
if (bt != MBT_NOTSEP)
break;
}
goto writeerr;
}
}
else if (!MapNLtoCRLF ||
{
/* use base64 encoding */
sm_dprintf(" ...Content-Transfer-Encoding: base64\n");
goto writeerr;
"X-MIME-Autoconverted: from 8bit to base64 by %s id %s",
MyHostName, e->e_id);
goto writeerr;
{
if (linelen > 71)
{
*bp = '\0';
goto writeerr;
linelen = 0;
}
linelen += 4;
{
*bp++ = '=';
*bp++ = '=';
break;
}
{
*bp++ = '=';
break;
}
}
*bp = '\0';
goto writeerr;
}
else
{
/* use quoted-printable encoding */
int fromstate;
/* set up map of characters that must be mapped */
for (p = "!\"#$@[\\]^`{|}~"; *p != '\0'; p++)
sm_dprintf(" ...Content-Transfer-Encoding: quoted-printable\n");
if (!putline("Content-Transfer-Encoding: quoted-printable",
mci))
goto writeerr;
"X-MIME-Autoconverted: from 8bit to quoted-printable by %s id %s",
MyHostName, e->e_id);
goto writeerr;
fromstate = 0;
c2 = '\n';
{
if (c1 == '\n')
{
{
*bp++ = '=';
}
{
buf[0] = '=';
}
*bp = '\0';
goto writeerr;
continue;
}
{
*bp++ = '=';
*bp++ = '2';
*bp++ = '0';
linelen += 3;
}
{
linelen++;
}
if (linelen > 72 &&
{
bp--;
else
c2 = '\n';
*bp++ = '=';
*bp = '\0';
goto writeerr;
if (c2 == '.')
{
*bp++ = '.';
linelen++;
}
}
{
*bp++ = '=';
linelen += 3;
}
{
fromstate++;
linelen++;
}
}
/* output any saved character */
{
*bp++ = '=';
linelen += 3;
}
{
*bp = '\0';
goto writeerr;
}
}
return bt;
return SM_IO_EOF;
}
/*
** MIME_GETCHAR -- get a character for MIME processing
**
** Treats boundaries as SM_IO_EOF.
**
** Parameters:
** fp -- the input file.
** boundaries -- the current MIME boundaries.
** btp -- if the return value is SM_IO_EOF, *btp is set to
** the type of the boundary.
**
** Returns:
** The next character in the input stream.
*/
static int
char **boundaries;
int *btp;
{
int c;
static int buflen = 0;
{
/* last \n in buffer may be part of next MIME boundary */
c = *bp;
}
else if (buflen > 0)
{
buflen--;
return *bp++;
}
else
buflen = 0;
if (c == '\n')
{
/* might be part of a MIME boundary */
*bp++ = c;
atbol = true;
if (c == '\n')
{
return c;
}
start = 1;
}
if (c != SM_IO_EOF)
*bp++ = c;
else
if (atbol && c == '-')
{
/* check for a message boundary */
if (c != '-')
{
if (c != SM_IO_EOF)
*bp++ = c;
else
return *bp++;
}
/* got "--", now check for rest of separator */
*bp++ = '-';
c != '\n')
{
*bp++ = c;
}
switch (bt)
{
case MBT_FINAL:
case MBT_INTERMED:
/* we have a message boundary */
buflen = 0;
return SM_IO_EOF;
}
*bp++ = c;
}
atbol = c == '\n';
if (buflen < 0)
{
return SM_IO_EOF;
}
return *bp++;
}
/*
** MIME_GETCHAR_CRLF -- do mime_getchar, but translate NL => CRLF
**
** Parameters:
** fp -- the input file.
** boundaries -- the current MIME boundaries.
** btp -- if the return value is SM_IO_EOF, *btp is set to
** the type of the boundary.
**
** Returns:
** The next character in the input stream.
*/
static int
char **boundaries;
int *btp;
{
static bool sendlf = false;
int c;
if (sendlf)
{
sendlf = false;
return '\n';
}
if (c == '\n' && MapNLtoCRLF)
{
sendlf = true;
return '\r';
}
return c;
}
/*
** MIMEBOUNDARY -- determine if this line is a MIME boundary & its type
**
** Parameters:
** line -- the input line.
** boundaries -- the set of currently pending boundaries.
**
** Returns:
** MBT_NOTSEP -- if this is not a separator line
** MBT_INTERMED -- if this is an intermediate separator
** MBT_FINAL -- if this is a final boundary
** MBT_SYNTAX -- if this is a boundary for the wrong
** enclosure -- i.e., a syntax error.
*/
static int
register char *line;
char **boundaries;
{
int i;
int savec;
return MBT_NOTSEP;
i--;
/* strip off trailing whitespace */
#if _FFR_MIME_CR_OK
#endif /* _FFR_MIME_CR_OK */
))
i--;
line[i] = '\0';
/* check for this as an intermediate boundary */
type = MBT_INTERMED;
{
/* check for a final boundary */
}
return type;
}
/*
** DEFCHARSET -- return default character set for message
**
** The first choice for character set is for the mailer
** corresponding to the envelope sender. If neither that
** nor the global configuration file has a default character
** set defined, return "unknown-8bit" as recommended by
** RFC 1428 section 3.
**
** Parameters:
** e -- the envelope for this message.
**
** Returns:
** The default character set for that mailer.
*/
char *
defcharset(e)
register ENVELOPE *e;
{
if (DefaultCharSet != NULL)
return DefaultCharSet;
return "unknown-8bit";
}
/*
** ISBOUNDARY -- is a given string a currently valid boundary?
**
** Parameters:
** line -- the current input line.
** boundaries -- the list of valid boundaries.
**
** Returns:
** The index number in boundaries if the line is found.
** -1 -- otherwise.
**
*/
static int
char *line;
char **boundaries;
{
register int i;
{
return i;
}
return -1;
}
#endif /* MIME8TO7 */
#if MIME7TO8
static int mime_fromqp __P((unsigned char *, unsigned char **, int));
/*
** MIME7TO8 -- output 7 bit encoded MIME body in 8 bit format
**
** This is a hack. Supports translating the two 7-bit body-encodings
** (quoted-printable and base64) to 8-bit coded bodies.
**
** There is not much point in supporting multipart here, as the UA
** will be able to deal with encoded MIME bodies if it can parse MIME
** multipart messages.
**
** message, encoded base64 or QP and mailer flag '9' has been defined
** on mailer.
**
** Contributed by Marius Olaffson <marius@rhi.hi.is>.
**
** Parameters:
** mci -- mailer connection information.
** header -- the header for this body part.
** e -- envelope.
**
** Returns:
** true iff body was written successfully
*/
{
-1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
-1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
-1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,62, -1,-1,-1,63,
52,53,54,55, 56,57,58,59, 60,61,-1,-1, -1,-1,-1,-1,
-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10, 11,12,13,14,
15,16,17,18, 19,20,21,22, 23,24,25,-1, -1,-1,-1,-1,
-1,26,27,28, 29,30,31,32, 33,34,35,36, 37,38,39,40,
41,42,43,44, 45,46,47,48, 49,50,51,-1, -1,-1,-1,-1
};
bool
register ENVELOPE *e;
{
int pxflags;
register char *p;
char *cte;
char **pvp;
unsigned char *fbufp;
extern unsigned char MimeTokenTab[256];
if (p == NULL ||
MimeTokenTab, false)) == NULL ||
{
/* "can't happen" -- upper level should have caught this */
/* avoid bounce loops */
e->e_flags |= EF_DONT_MIME;
if (p != NULL)
{
"Content-Transfer-Encoding: %s", p);
goto writeerr;
}
goto writeerr;
!= NULL)
{
goto writeerr;
}
return true;
}
goto writeerr;
"X-MIME-Autoconverted: from %.200s to 8bit by %s id %s",
goto writeerr;
/*
** Translate body encoding to 8-bit. Supports two types of
** encodings; "base64" and "quoted-printable". Assume qp if
** it is not base64.
*/
{
{
continue;
do
{
break;
do
{
break;
do
{
break;
continue;
#if MIME7TO8_OLD
++fbufp;
#else /* MIME7TO8_OLD */
{ \
++fbufp; \
pxflags |= PXLF_NOADDEOL; \
}
#endif /* MIME7TO8_OLD */
#define PUTLINE64 \
do \
{ \
{ \
CHK_EOL; \
goto writeerr; \
pxflags &= ~PXLF_NOADDEOL; \
} \
} while (0)
if (c3 == '=')
continue;
if (c4 == '=')
continue;
}
}
else
{
int off;
/* quoted-printable */
pxflags |= PXLF_NOADDEOL;
{
if (off < -1)
continue;
{
goto writeerr;
}
{
&fbufp,
goto again;
}
}
}
/* force out partial last line */
{
*fbufp = '\0';
goto writeerr;
}
/*
** The decoded text may end without an EOL. Since this function
** add an extra one at the end just in case. This is a hack,
** but so is auto-converting MIME in the first place.
*/
goto writeerr;
return true;
return false;
}
/*
** The following is based on Borenstein's "codes.c" module, with simplifying
** changes as we do not deal with multipart, and to do the translation in-core,
** with an attempt to prevent overrun of output buffers.
**
** What is needed here are changes to defend this code better against
** bad encodings. Questionable to always return 0xFF for bad mappings.
*/
{
-1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
-1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
-1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
0, 1, 2, 3, 4, 5, 6, 7, 8, 9,-1,-1, -1,-1,-1,-1,
-1,10,11,12, 13,14,15,-1, -1,-1,-1,-1, -1,-1,-1,-1,
-1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
-1,10,11,12, 13,14,15,-1, -1,-1,-1,-1, -1,-1,-1,-1,
-1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1
};
/*
** MIME_FROMQP -- decode quoted printable string
**
** Parameters:
** infile -- input (encoded) string
** outfile -- output string
** maxlen -- size of output buffer
**
** Returns:
** -2 if decoding failure
** -1 if infile completely decoded into outfile
** >= 0 is the position in infile decoding
** reached before maxlen was reached
*/
static int
unsigned char *infile;
unsigned char **outfile;
int maxlen; /* Max # of chars allowed in outfile */
{
int nchar = 0;
unsigned char *b;
/* decrement by one for trailing '\0', at least one other char */
if (--maxlen < 1)
return 0;
b = infile;
{
if (c1 == '=')
{
break;
{
/* ignore it and the rest of the buffer */
return -2;
}
else
{
do
{
{
c2 = -1;
break;
}
if (c2 == -1)
break;
nchar++;
}
}
else
{
nchar++;
if (c1 == '\n')
break;
}
}
*(*outfile)++ = '\0';
return (infile - b - 1);
return -1;
}
#endif /* MIME7TO8 */