xref: /csrg-svn/usr.sbin/sendmail/src/mime.c (revision 68711)
167545Seric /*
267545Seric  * Copyright (c) 1994 Eric P. Allman
367545Seric  * Copyright (c) 1994
467545Seric  *	The Regents of the University of California.  All rights reserved.
567545Seric  *
667545Seric  * %sccs.include.redist.c%
767545Seric  */
867545Seric 
967545Seric # include "sendmail.h"
1067545Seric # include <string.h>
1167545Seric 
1267545Seric #ifndef lint
13*68711Seric static char sccsid[] = "@(#)mime.c	8.13 (Berkeley) 04/02/95";
1467545Seric #endif /* not lint */
1567545Seric 
1667545Seric /*
1767545Seric **  MIME support.
1867545Seric **
1967545Seric **	I am indebted to John Beck of Hewlett-Packard, who contributed
2067545Seric **	his code to me for inclusion.  As it turns out, I did not use
2167545Seric **	his code since he used a "minimum change" approach that used
2267545Seric **	several temp files, and I wanted a "minimum impact" approach
2367545Seric **	that would avoid copying.  However, looking over his code
2467545Seric **	helped me cement my understanding of the problem.
2567545Seric **
2667545Seric **	I also looked at, but did not directly use, Nathaniel
2767545Seric **	Borenstein's "code.c" module.  Again, it functioned as
2867545Seric **	a file-to-file translator, which did not fit within my
2967545Seric **	design bounds, but it was a useful base for understanding
3067545Seric **	the problem.
3167545Seric */
3267545Seric 
3367545Seric 
3467545Seric /* character set for hex and base64 encoding */
3567545Seric char	Base16Code[] =	"0123456789ABCDEF";
3667545Seric char	Base64Code[] =	"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
3767545Seric 
3867545Seric /* types of MIME boundaries */
3967545Seric #define MBT_SYNTAX	0	/* syntax error */
4067545Seric #define MBT_NOTSEP	1	/* not a boundary */
4167545Seric #define MBT_INTERMED	2	/* intermediate boundary (no trailing --) */
4267545Seric #define MBT_FINAL	3	/* final boundary (trailing -- included) */
4367547Seric 
4467547Seric static int	MimeBoundaryType;	/* internal linkage */
4567545Seric /*
4667545Seric **  MIME8TO7 -- output 8 bit body in 7 bit format
4767545Seric **
4867545Seric **	The header has already been output -- this has to do the
4967545Seric **	8 to 7 bit conversion.  It would be easy if we didn't have
5067545Seric **	to deal with nested formats (multipart/xxx and message/rfc822).
5167545Seric **
5267545Seric **	We won't be called if we don't have to do a conversion, and
5367545Seric **	appropriate MIME-Version: and Content-Type: fields have been
5467545Seric **	output.  Any Content-Transfer-Encoding: field has not been
5567545Seric **	output, and we can add it here.
5667545Seric **
5767545Seric **	Parameters:
5867545Seric **		mci -- mailer connection information.
5967545Seric **		header -- the header for this body part.
6067545Seric **		e -- envelope.
6168517Seric **		boundaries -- the currently pending message boundaries.
6268517Seric **			NULL if we are processing the outer portion.
6368517Seric **		flags -- to tweak processing.
6467545Seric **
6567545Seric **	Returns:
6667545Seric **		An indicator of what terminated the message part:
6767545Seric **		  MBT_FINAL -- the final boundary
6867545Seric **		  MBT_INTERMED -- an intermediate boundary
6967545Seric **		  MBT_NOTSEP -- an end of file
7067545Seric */
7167545Seric 
7268517Seric struct args
7368517Seric {
7468517Seric 	char	*field;		/* name of field */
7568517Seric 	char	*value;		/* value of that field */
7668517Seric };
7768517Seric 
7867545Seric int
7968517Seric mime8to7(mci, header, e, boundaries, flags)
8067545Seric 	register MCI *mci;
8168515Seric 	HDR *header; register ENVELOPE *e;
8268517Seric 	char **boundaries;
8368517Seric 	int flags;
8467545Seric {
8567545Seric 	register char *p;
8667545Seric 	int linelen;
8767545Seric 	int bt;
8867545Seric 	off_t offset;
8967545Seric 	size_t sectionsize, sectionhighbits;
9068517Seric 	int i;
9168517Seric 	char *type;
9268517Seric 	char *subtype;
9368517Seric 	char **pvp;
9468517Seric 	int argc = 0;
9568517Seric 	struct args argv[MAXMIMEARGS];
9667545Seric 	char bbuf[128];
9767545Seric 	char buf[MAXLINE];
9868517Seric 	char pvpbuf[MAXLINE];
99*68711Seric 	extern char MimeTokenTab[256];
10067545Seric 
10167545Seric 	if (tTd(43, 1))
10267545Seric 	{
10367545Seric 		printf("mime8to7: boundary=%s\n",
10468517Seric 			boundaries[0] == NULL ? "<none>" : boundaries[0]);
10568517Seric 		for (i = 1; boundaries[i] != NULL; i++)
106*68711Seric 			printf("\t%s\n", boundaries[i]);
10767545Seric 	}
10868517Seric 	type = subtype = "-none-";
10967545Seric 	p = hvalue("Content-Type", header);
11068517Seric 	if (p != NULL &&
111*68711Seric 	    (pvp = prescan(p, '\0', pvpbuf, sizeof pvpbuf, NULL,
112*68711Seric 			   MimeTokenTab)) != NULL &&
11368517Seric 	    pvp[0] != NULL)
11467545Seric 	{
11568517Seric 		type = *pvp++;
11668517Seric 		if (*pvp != NULL && strcmp(*pvp, "/") == 0 &&
11768517Seric 		    *++pvp != NULL)
11868517Seric 		{
11968517Seric 			subtype = *pvp++;
12068517Seric 		}
12168517Seric 
12268517Seric 		/* break out parameters */
12368517Seric 		while (*pvp != NULL && argc < MAXMIMEARGS)
12468517Seric 		{
12568517Seric 			/* skip to semicolon separator */
12668517Seric 			while (*pvp != NULL && strcmp(*pvp, ";") != 0)
12768517Seric 				pvp++;
12868517Seric 			if (*pvp++ == NULL || *pvp == NULL)
12968517Seric 				break;
13068517Seric 
13168517Seric 			/* extract field name */
13268517Seric 			argv[argc].field = *pvp++;
13368517Seric 
13468517Seric 			/* see if there is a value */
13568517Seric 			if (*pvp != NULL && strcmp(*pvp, "=") == 0 &&
13668517Seric 			    (*++pvp == NULL || strcmp(*pvp, ";") != 0))
13768517Seric 			{
13868517Seric 				argv[argc].value = *pvp;
13968517Seric 				argc++;
14068517Seric 			}
14168517Seric 		}
14268517Seric 	}
14368517Seric 	if (strcasecmp(type, "multipart") == 0)
14468517Seric 	{
14567545Seric 		register char *q;
14667545Seric 
14768517Seric 		for (i = 0; i < argc; i++)
14867545Seric 		{
14968517Seric 			if (strcasecmp(argv[i].field, "boundary") == 0)
15068517Seric 				break;
15168517Seric 		}
15268517Seric 		if (i >= argc)
15368517Seric 		{
15467545Seric 			syserr("mime8to7: Content-Type: %s missing boundary", p);
15567545Seric 			p = "---";
15667545Seric 		}
15767545Seric 		else
15868517Seric 			p = argv[i].value;
15967545Seric 		if (*p == '"')
160*68711Seric 			q = strchr(++p, '"');
16167545Seric 		else
16267545Seric 			q = p + strlen(p);
16367545Seric 		if (q - p > sizeof bbuf - 1)
16467545Seric 		{
16567545Seric 			syserr("mime8to7: multipart boundary \"%.*s\" too long",
16667545Seric 				q - p, p);
16767545Seric 			q = p + sizeof bbuf - 1;
16867545Seric 		}
16967545Seric 		strncpy(bbuf, p, q - p);
17067545Seric 		bbuf[q - p] = '\0';
17167545Seric 		if (tTd(43, 1))
17267545Seric 		{
17367545Seric 			printf("mime8to7: multipart boundary \"%s\"\n", bbuf);
17467545Seric 		}
17568517Seric 		for (i = 0; i < MAXMIMENESTING; i++)
17668517Seric 			if (boundaries[i] == NULL)
17768517Seric 				break;
17868517Seric 		if (i >= MAXMIMENESTING)
17968517Seric 			syserr("mime8to7: multipart nesting boundary too deep");
18068517Seric 		else
18168517Seric 		{
18268517Seric 			boundaries[i] = bbuf;
18368517Seric 			boundaries[i + 1] = NULL;
18468517Seric 		}
18567545Seric 
18668517Seric 		/* flag subtypes that can't have any 8-bit data */
18768517Seric 		if (strcasecmp(subtype, "signed") == 0)
18868517Seric 			flags |= M87F_NO8BIT;
18968517Seric 
19067545Seric 		/* skip the early "comment" prologue */
19167545Seric 		bt = MBT_FINAL;
19267545Seric 		while (fgets(buf, sizeof buf, e->e_dfp) != NULL)
19367545Seric 		{
19468517Seric 			bt = mimeboundary(buf, boundaries);
19567545Seric 			if (bt != MBT_NOTSEP)
19667545Seric 				break;
19767545Seric 			putline(buf, mci);
19867545Seric 		}
19967545Seric 		while (bt != MBT_FINAL)
20067545Seric 		{
20167545Seric 			auto HDR *hdr = NULL;
20267545Seric 
20367545Seric 			sprintf(buf, "--%s", bbuf);
20467545Seric 			putline(buf, mci);
20567545Seric 			collect(e->e_dfp, FALSE, FALSE, &hdr, e);
20667936Seric 			putheader(mci, hdr, e, 0);
20768517Seric 			bt = mime8to7(mci, hdr, e, boundaries, flags);
20867545Seric 		}
20967545Seric 		sprintf(buf, "--%s--", bbuf);
21067545Seric 		putline(buf, mci);
21167545Seric 
21267545Seric 		/* skip the late "comment" epilogue */
21367545Seric 		while (fgets(buf, sizeof buf, e->e_dfp) != NULL)
21467545Seric 		{
21567545Seric 			putline(buf, mci);
21668517Seric 			bt = mimeboundary(buf, boundaries);
21767545Seric 			if (bt != MBT_NOTSEP)
21867545Seric 				break;
21967545Seric 		}
22068517Seric 		boundaries[i] = NULL;
22167545Seric 		return bt;
22267545Seric 	}
22367545Seric 
22467545Seric 	/*
22567545Seric 	**  Non-compound body type
22667545Seric 	**
22767545Seric 	**	Compute the ratio of seven to eight bit characters;
22867545Seric 	**	use that as a heuristic to decide how to do the
22967545Seric 	**	encoding.
23067545Seric 	*/
23167545Seric 
23268517Seric 	/* handle types that cannot have 8-bit data internally */
23368517Seric 	sprintf(buf, "%s/%s", type, subtype);
23468517Seric 	if (wordinclass(buf, 'n'))
23568517Seric 		flags |= M87F_NO8BIT;
23668517Seric 
23767545Seric 	sectionsize = sectionhighbits = 0;
23868517Seric 	if (!bitset(M87F_NO8BIT, flags))
23967545Seric 	{
24068515Seric 		/* remember where we were */
24168515Seric 		offset = ftell(e->e_dfp);
24268515Seric 		if (offset == -1)
24368564Seric 			syserr("mime8to7: cannot ftell on df%s", e->e_id);
24468515Seric 
24568515Seric 		/* do a scan of this body type to count character types */
24668515Seric 		while (fgets(buf, sizeof buf, e->e_dfp) != NULL)
24767545Seric 		{
24868517Seric 			bt = mimeboundary(buf, boundaries);
24968515Seric 			if (bt != MBT_NOTSEP)
25068515Seric 				break;
25168515Seric 			for (p = buf; *p != '\0'; p++)
25268515Seric 			{
25368515Seric 				/* count bytes with the high bit set */
25468515Seric 				sectionsize++;
25568515Seric 				if (bitset(0200, *p))
25668515Seric 					sectionhighbits++;
25768515Seric 			}
25868515Seric 
25968515Seric 			/*
26068515Seric 			**  Heuristic: if 1/4 of the first 4K bytes are 8-bit,
26168515Seric 			**  assume base64.  This heuristic avoids double-reading
26268515Seric 			**  large graphics or video files.
26368515Seric 			*/
26468515Seric 
26568515Seric 			if (sectionsize >= 4096 &&
26668515Seric 			    sectionhighbits > sectionsize / 4)
26768515Seric 				break;
26867545Seric 		}
26968515Seric 		if (feof(e->e_dfp))
27068515Seric 			bt = MBT_FINAL;
27167547Seric 
27268515Seric 		/* return to the original offset for processing */
27368515Seric 		/* XXX use relative seeks to handle >31 bit file sizes? */
27468515Seric 		if (fseek(e->e_dfp, offset, SEEK_SET) < 0)
27568564Seric 			syserr("mime8to7: cannot fseek on df%s", e->e_id);
27667545Seric 	}
27767545Seric 
27867547Seric 	/*
27967547Seric 	**  Heuristically determine encoding method.
28067547Seric 	**	If more than 1/8 of the total characters have the
28167547Seric 	**	eighth bit set, use base64; else use quoted-printable.
28267547Seric 	*/
28367547Seric 
28467545Seric 	if (tTd(43, 8))
28567545Seric 	{
28667545Seric 		printf("mime8to7: %ld high bits in %ld bytes\n",
28767545Seric 			sectionhighbits, sectionsize);
28867545Seric 	}
28967554Seric 	if (sectionhighbits == 0)
29067545Seric 	{
29167554Seric 		/* no encoding necessary */
29267695Seric 		p = hvalue("content-transfer-encoding", header);
29367695Seric 		if (p != NULL)
29467695Seric 		{
29567695Seric 			sprintf(buf, "Content-Transfer-Encoding: %s", p);
29667695Seric 			putline(buf, mci);
29767695Seric 		}
29867554Seric 		putline("", mci);
29967554Seric 		mci->mci_flags &= ~MCIF_INHEADER;
30067554Seric 		while (fgets(buf, sizeof buf, e->e_dfp) != NULL)
30167554Seric 		{
30268517Seric 			bt = mimeboundary(buf, boundaries);
30367554Seric 			if (bt != MBT_NOTSEP)
30467554Seric 				break;
30567554Seric 			if (buf[0] == 'F' &&
30667554Seric 			    bitnset(M_ESCFROM, mci->mci_mailer->m_flags) &&
30767554Seric 			    strncmp(buf, "From ", 5) == 0)
30867554Seric 				(void) putc('>', mci->mci_out);
30967554Seric 			putline(buf, mci);
31067554Seric 		}
31167554Seric 	}
31267554Seric 	else if (sectionsize / 8 < sectionhighbits)
31367554Seric 	{
31467545Seric 		/* use base64 encoding */
31567545Seric 		int c1, c2;
31667545Seric 
31767545Seric 		putline("Content-Transfer-Encoding: base64", mci);
31867545Seric 		putline("", mci);
31967545Seric 		mci->mci_flags &= ~MCIF_INHEADER;
32067545Seric 		linelen = 0;
32168517Seric 		while ((c1 = mime_getchar(e->e_dfp, boundaries)) != EOF)
32267545Seric 		{
32367545Seric 			if (linelen > 71)
32467545Seric 			{
32567545Seric 				fputs(mci->mci_mailer->m_eol, mci->mci_out);
32667545Seric 				linelen = 0;
32767545Seric 			}
32867545Seric 			linelen += 4;
32967545Seric 			fputc(Base64Code[c1 >> 2], mci->mci_out);
33067545Seric 			c1 = (c1 & 0x03) << 4;
33168517Seric 			c2 = mime_getchar(e->e_dfp, boundaries);
33267545Seric 			if (c2 == EOF)
33367545Seric 			{
33467545Seric 				fputc(Base64Code[c1], mci->mci_out);
33567545Seric 				fputc('=', mci->mci_out);
33667545Seric 				fputc('=', mci->mci_out);
33767545Seric 				break;
33867545Seric 			}
33967545Seric 			c1 |= (c2 >> 4) & 0x0f;
34067545Seric 			fputc(Base64Code[c1], mci->mci_out);
34167545Seric 			c1 = (c2 & 0x0f) << 2;
34268517Seric 			c2 = mime_getchar(e->e_dfp, boundaries);
34367545Seric 			if (c2 == EOF)
34467545Seric 			{
34567545Seric 				fputc(Base64Code[c1], mci->mci_out);
34667545Seric 				fputc('=', mci->mci_out);
34767545Seric 				break;
34867545Seric 			}
34967545Seric 			c1 |= (c2 >> 6) & 0x03;
35067545Seric 			fputc(Base64Code[c1], mci->mci_out);
35167545Seric 			fputc(Base64Code[c2 & 0x3f], mci->mci_out);
35267545Seric 		}
35367545Seric 	}
35467545Seric 	else
35567545Seric 	{
35667545Seric 		/* use quoted-printable encoding */
35767545Seric 		int c1, c2;
35868515Seric 		int fromstate;
35967545Seric 
36067545Seric 		putline("Content-Transfer-Encoding: quoted-printable", mci);
36167545Seric 		putline("", mci);
36267545Seric 		mci->mci_flags &= ~MCIF_INHEADER;
36368515Seric 		linelen = fromstate = 0;
36467554Seric 		c2 = '\n';
36568517Seric 		while ((c1 = mime_getchar(e->e_dfp, boundaries)) != EOF)
36667545Seric 		{
36767545Seric 			if (c1 == '\n')
36867545Seric 			{
36967545Seric 				if (c2 == ' ' || c2 == '\t')
37067545Seric 				{
37167545Seric 					fputc('=', mci->mci_out);
37267840Seric 					fputc(Base16Code[(c2 >> 4) & 0x0f],
37367840Seric 								mci->mci_out);
37467840Seric 					fputc(Base16Code[c2 & 0x0f],
37567840Seric 								mci->mci_out);
37667840Seric 					fputs(mci->mci_mailer->m_eol,
37767840Seric 								mci->mci_out);
37867545Seric 				}
37967545Seric 				fputs(mci->mci_mailer->m_eol, mci->mci_out);
38068515Seric 				linelen = fromstate = 0;
38167545Seric 				c2 = c1;
38267545Seric 				continue;
38367545Seric 			}
38468515Seric 			if (c2 == ' ' && linelen == 4 && fromstate == 4 &&
38568515Seric 			    bitnset(M_ESCFROM, mci->mci_mailer->m_flags))
38667840Seric 			{
38768515Seric 				fputs("=20", mci->mci_out);
38868515Seric 				linelen += 3;
38968515Seric 			}
39068515Seric 			else if (c2 == ' ' || c2 == '\t')
39168515Seric 			{
39267840Seric 				fputc(c2, mci->mci_out);
39367840Seric 				linelen++;
39467840Seric 			}
39567545Seric 			if (linelen > 72)
39667545Seric 			{
39767545Seric 				fputc('=', mci->mci_out);
39867545Seric 				fputs(mci->mci_mailer->m_eol, mci->mci_out);
39968515Seric 				linelen = fromstate = 0;
40067554Seric 				c2 = '\n';
40167545Seric 			}
40267761Seric 			if (c2 == '\n' && c1 == '.' &&
40367761Seric 				 bitnset(M_XDOT, mci->mci_mailer->m_flags))
40467761Seric 			{
40567761Seric 				fputc('.', mci->mci_out);
40667761Seric 				linelen++;
40767761Seric 			}
40867547Seric 			if ((c1 < 0x20 && c1 != '\t') || c1 >= 0x7f || c1 == '=')
40967545Seric 			{
41067545Seric 				fputc('=', mci->mci_out);
41167545Seric 				fputc(Base16Code[(c1 >> 4) & 0x0f], mci->mci_out);
41267545Seric 				fputc(Base16Code[c1 & 0x0f], mci->mci_out);
41367545Seric 				linelen += 3;
41467545Seric 			}
41567840Seric 			else if (c1 != ' ' && c1 != '\t')
41667545Seric 			{
41768515Seric 				if (linelen < 4 && c1 == "From"[linelen])
41868515Seric 					fromstate++;
41967545Seric 				fputc(c1, mci->mci_out);
42067545Seric 				linelen++;
42167545Seric 			}
42267545Seric 			c2 = c1;
42367545Seric 		}
42467840Seric 
42567840Seric 		/* output any saved character */
42667840Seric 		if (c2 == ' ' || c2 == '\t')
42767840Seric 		{
42868515Seric 			fputc('=', mci->mci_out);
42968515Seric 			fputc(Base16Code[(c2 >> 4) & 0x0f], mci->mci_out);
43068515Seric 			fputc(Base16Code[c2 & 0x0f], mci->mci_out);
43168515Seric 			linelen += 3;
43267840Seric 		}
43367545Seric 	}
43467545Seric 	if (linelen > 0)
43567545Seric 		fputs(mci->mci_mailer->m_eol, mci->mci_out);
43667547Seric 	return MimeBoundaryType;
43767545Seric }
43868515Seric /*
43968515Seric **  MIME_GETCHAR -- get a character for MIME processing
44068515Seric **
44168515Seric **	Treats boundaries as EOF.
44268515Seric **
44368515Seric **	Parameters:
44468515Seric **		fp -- the input file.
44568517Seric **		boundaries -- the current MIME boundaries.
44668515Seric **
44768515Seric **	Returns:
44868515Seric **		The next character in the input stream.
44968515Seric */
45067545Seric 
45167545Seric int
45268517Seric mime_getchar(fp, boundaries)
45367545Seric 	register FILE *fp;
45468517Seric 	char **boundaries;
45567545Seric {
45667545Seric 	int c;
45767545Seric 	static char *bp = NULL;
45867545Seric 	static int buflen = 0;
45967545Seric 	static bool atbol = TRUE;	/* at beginning of line */
46067545Seric 	static char buf[128];		/* need not be a full line */
46167545Seric 
46267545Seric 	if (buflen > 0)
46367545Seric 	{
46467545Seric 		buflen--;
46567545Seric 		return *bp++;
46667545Seric 	}
46768515Seric 	bp = buf;
46868515Seric 	buflen = 0;
46967545Seric 	c = fgetc(fp);
47068515Seric 	if (c == '\n')
47168515Seric 	{
47268515Seric 		/* might be part of a MIME boundary */
47368515Seric 		*bp++ = c;
47468515Seric 		atbol = TRUE;
47568515Seric 		c = fgetc(fp);
47668515Seric 	}
47768515Seric 	if (c != EOF)
47868515Seric 		*bp++ = c;
47968517Seric 	if (atbol && c == '-')
48067545Seric 	{
48167545Seric 		/* check for a message boundary */
48267545Seric 		c = fgetc(fp);
48367545Seric 		if (c != '-')
48467545Seric 		{
48567545Seric 			if (c != EOF)
48668515Seric 				*bp++ = c;
48768515Seric 			buflen = bp - buf - 1;
48868515Seric 			bp = buf;
48968515Seric 			return *bp++;
49067545Seric 		}
49167545Seric 
49267545Seric 		/* got "--", now check for rest of separator */
49367545Seric 		*bp++ = '-';
49467545Seric 		while (bp < &buf[sizeof buf - 1] &&
49567545Seric 		       (c = fgetc(fp)) != EOF && c != '\n')
49667545Seric 		{
49767545Seric 			*bp++ = c;
49867545Seric 		}
49967545Seric 		*bp = '\0';
50068517Seric 		MimeBoundaryType = mimeboundary(buf, boundaries);
50167547Seric 		switch (MimeBoundaryType)
50267545Seric 		{
50367545Seric 		  case MBT_FINAL:
50467545Seric 		  case MBT_INTERMED:
50567545Seric 			/* we have a message boundary */
50667545Seric 			buflen = 0;
50767545Seric 			return EOF;
50867545Seric 		}
50967545Seric 
51067545Seric 		atbol = c == '\n';
51167545Seric 		if (c != EOF)
51267545Seric 			*bp++ = c;
51367545Seric 	}
51467545Seric 
51568515Seric 	buflen = bp - buf - 1;
51668515Seric 	if (buflen < 0)
51768515Seric 		return EOF;
51868515Seric 	bp = buf;
51968515Seric 	return *bp++;
52067545Seric }
52167545Seric /*
52267545Seric **  MIMEBOUNDARY -- determine if this line is a MIME boundary & its type
52367545Seric **
52467545Seric **	Parameters:
52567545Seric **		line -- the input line.
52668517Seric **		boundaries -- the set of currently pending boundaries.
52767545Seric **
52867545Seric **	Returns:
52967545Seric **		MBT_NOTSEP -- if this is not a separator line
53067545Seric **		MBT_INTERMED -- if this is an intermediate separator
53167545Seric **		MBT_FINAL -- if this is a final boundary
53267545Seric **		MBT_SYNTAX -- if this is a boundary for the wrong
53367545Seric **			enclosure -- i.e., a syntax error.
53467545Seric */
53567545Seric 
53667545Seric int
53768517Seric mimeboundary(line, boundaries)
53867545Seric 	register char *line;
53968517Seric 	char **boundaries;
54067545Seric {
54167545Seric 	int type;
54267545Seric 	int i;
54368517Seric 	int savec;
54467545Seric 
54568517Seric 	if (line[0] != '-' || line[1] != '-' || boundaries == NULL)
54667545Seric 		return MBT_NOTSEP;
54767545Seric 	if (tTd(43, 5))
54868517Seric 		printf("mimeboundary: line=\"%s\"... ", line);
54967545Seric 	i = strlen(line);
55067545Seric 	if (line[i - 1] == '\n')
55167545Seric 		i--;
55268515Seric 	while (line[i - 1] == ' ' || line[i - 1] == '\t')
55368515Seric 		i--;
55467545Seric 	if (i > 2 && strncmp(&line[i - 2], "--", 2) == 0)
55567545Seric 	{
55667545Seric 		type = MBT_FINAL;
55767545Seric 		i -= 2;
55867545Seric 	}
55967545Seric 	else
56067545Seric 		type = MBT_INTERMED;
56167545Seric 
56268517Seric 	savec = line[i];
56368517Seric 	line[i] = '\0';
56467545Seric 	/* XXX should check for improper nesting here */
56568517Seric 	if (isboundary(&line[2], boundaries) < 0)
56667545Seric 		type = MBT_NOTSEP;
56768517Seric 	line[i] = savec;
56867545Seric 	if (tTd(43, 5))
56967545Seric 		printf("%d\n", type);
57067545Seric 	return type;
57167545Seric }
57267896Seric /*
57367896Seric **  DEFCHARSET -- return default character set for message
57467896Seric **
57567896Seric **	The first choice for character set is for the mailer
57667896Seric **	corresponding to the envelope sender.  If neither that
57767896Seric **	nor the global configuration file has a default character
57867896Seric **	set defined, return "unknown-8bit" as recommended by
57967896Seric **	RFC 1428 section 3.
58067896Seric **
58167896Seric **	Parameters:
58267896Seric **		e -- the envelope for this message.
58367896Seric **
58467896Seric **	Returns:
58567896Seric **		The default character set for that mailer.
58667896Seric */
58767896Seric 
58867896Seric char *
58967896Seric defcharset(e)
59067896Seric 	register ENVELOPE *e;
59167896Seric {
59267896Seric 	if (e != NULL && e->e_from.q_mailer != NULL &&
59367896Seric 	    e->e_from.q_mailer->m_defcharset != NULL)
59467896Seric 		return e->e_from.q_mailer->m_defcharset;
59567896Seric 	if (DefaultCharSet != NULL)
59667896Seric 		return DefaultCharSet;
59767896Seric 	return "unknown-8bit";
59867896Seric }
59968517Seric /*
60068517Seric **  ISBOUNDARY -- is a given string a currently valid boundary?
60168517Seric **
60268517Seric **	Parameters:
60368517Seric **		line -- the current input line.
60468517Seric **		boundaries -- the list of valid boundaries.
60568517Seric **
60668517Seric **	Returns:
60768517Seric **		The index number in boundaries if the line is found.
60868517Seric **		-1 -- otherwise.
60968517Seric **
61068517Seric */
61168517Seric 
61268517Seric int
61368517Seric isboundary(line, boundaries)
61468517Seric 	char *line;
61568517Seric 	char **boundaries;
61668517Seric {
61768517Seric 	register int i;
61868517Seric 
619*68711Seric 	for (i = 0; boundaries[i] != NULL; i++)
62068517Seric 	{
62168517Seric 		if (strcmp(line, boundaries[i]) == 0)
62268517Seric 			return i;
62368517Seric 	}
62468517Seric 	return -1;
62568517Seric }
626