122706Sdist /*
268839Seric  * Copyright (c) 1983, 1995 Eric P. Allman
362525Sbostic  * Copyright (c) 1988, 1993
462525Sbostic  *	The Regents of the University of California.  All rights reserved.
533729Sbostic  *
642826Sbostic  * %sccs.include.redist.c%
733729Sbostic  */
822706Sdist 
922706Sdist #ifndef lint
10*69748Seric static char sccsid[] = "@(#)headers.c	8.67 (Berkeley) 05/28/95";
1133729Sbostic #endif /* not lint */
1222706Sdist 
134091Seric # include <errno.h>
144091Seric # include "sendmail.h"
154091Seric 
164091Seric /*
174091Seric **  CHOMPHEADER -- process and save a header line.
184091Seric **
194091Seric **	Called by collect and by readcf to deal with header lines.
204091Seric **
214091Seric **	Parameters:
224091Seric **		line -- header as a text line.
234091Seric **		def -- if set, this is a default value.
2468717Seric **		hdrp -- a pointer to the place to save the header.
2555012Seric **		e -- the envelope including this header.
264091Seric **
274091Seric **	Returns:
284091Seric **		flags for this header.
294091Seric **
304091Seric **	Side Effects:
314091Seric **		The header is saved on the header list.
324319Seric **		Contents of 'line' are destroyed.
334091Seric */
344091Seric 
35*69748Seric int
3668717Seric chompheader(line, def, hdrp, e)
374091Seric 	char *line;
384091Seric 	bool def;
3968717Seric 	HDR **hdrp;
4055012Seric 	register ENVELOPE *e;
414091Seric {
424091Seric 	register char *p;
434091Seric 	register HDR *h;
444091Seric 	HDR **hp;
454091Seric 	char *fname;
464091Seric 	char *fvalue;
474091Seric 	struct hdrinfo *hi;
489059Seric 	bool cond = FALSE;
4968717Seric 	bool headeronly;
5010689Seric 	BITMAP mopts;
5168528Seric 	char buf[MAXNAME + 1];
524091Seric 
537677Seric 	if (tTd(31, 6))
5468812Seric 	{
5568812Seric 		printf("chompheader: ");
5668812Seric 		xputs(line);
5768812Seric 		printf("\n");
5868812Seric 	}
597677Seric 
6068717Seric 	headeronly = hdrp != NULL;
6168717Seric 	if (!headeronly)
6268717Seric 		hdrp = &e->e_header;
6368717Seric 
644627Seric 	/* strip off options */
6510689Seric 	clrbitmap(mopts);
664627Seric 	p = line;
6757405Seric 	if (*p == '?')
684627Seric 	{
694627Seric 		/* have some */
7056795Seric 		register char *q = strchr(p + 1, *p);
714627Seric 
724627Seric 		if (q != NULL)
734627Seric 		{
744627Seric 			*q++ = '\0';
7510689Seric 			while (*++p != '\0')
7610689Seric 				setbitn(*p, mopts);
774627Seric 			p = q;
784627Seric 		}
794627Seric 		else
8068690Seric 			syserr("553 header syntax error, line \"%s\"", line);
819059Seric 		cond = TRUE;
824627Seric 	}
834627Seric 
844091Seric 	/* find canonical name */
854627Seric 	fname = p;
8664149Seric 	while (isascii(*p) && isgraph(*p) && *p != ':')
8764149Seric 		p++;
8864149Seric 	fvalue = p;
8964149Seric 	while (isascii(*p) && isspace(*p))
9064149Seric 		p++;
9164150Seric 	if (*p++ != ':' || fname == fvalue)
9210118Seric 	{
9358151Seric 		syserr("553 header syntax error, line \"%s\"", line);
9410118Seric 		return (0);
9510118Seric 	}
9664149Seric 	*fvalue = '\0';
9764149Seric 	fvalue = p;
984091Seric 
994091Seric 	/* strip field value on front */
1004091Seric 	if (*fvalue == ' ')
1014091Seric 		fvalue++;
1024091Seric 
1034091Seric 	/* see if it is a known type */
1044091Seric 	for (hi = HdrInfo; hi->hi_field != NULL; hi++)
1054091Seric 	{
10659579Seric 		if (strcasecmp(hi->hi_field, fname) == 0)
1074091Seric 			break;
1084091Seric 	}
1094091Seric 
11064283Seric 	if (tTd(31, 9))
11164283Seric 	{
11264283Seric 		if (hi->hi_field == NULL)
11364283Seric 			printf("no header match\n");
11464283Seric 		else
11567694Seric 			printf("header match, hi_flags=%x\n", hi->hi_flags);
11664283Seric 	}
11764283Seric 
11811414Seric 	/* see if this is a resent message */
11968717Seric 	if (!def && !headeronly && bitset(H_RESENT, hi->hi_flags))
12055012Seric 		e->e_flags |= EF_RESENT;
12111414Seric 
12268558Seric 	/* if this is an Errors-To: header keep track of it now */
12368717Seric 	if (UseErrorsTo && !def && !headeronly &&
12468717Seric 	    bitset(H_ERRORSTO, hi->hi_flags))
12568558Seric 		(void) sendtolist(fvalue, NULLADDR, &e->e_errorqueue, 0, e);
12668558Seric 
1274091Seric 	/* if this means "end of header" quit now */
1284091Seric 	if (bitset(H_EOH, hi->hi_flags))
1294091Seric 		return (hi->hi_flags);
1304091Seric 
13164653Seric 	/*
13264653Seric 	**  Drop explicit From: if same as what we would generate.
13364653Seric 	**  This is to make MH (which doesn't always give a full name)
13464653Seric 	**  insert the full name information in all circumstances.
13564653Seric 	*/
13664653Seric 
13714784Seric 	p = "resent-from";
13855012Seric 	if (!bitset(EF_RESENT, e->e_flags))
13914784Seric 		p += 7;
14068717Seric 	if (!def && !headeronly && !bitset(EF_QUEUERUN, e->e_flags) &&
14168717Seric 	    strcasecmp(fname, p) == 0)
14211414Seric 	{
14363753Seric 		if (tTd(31, 2))
14463753Seric 		{
14563753Seric 			printf("comparing header from (%s) against default (%s or %s)\n",
14663753Seric 				fvalue, e->e_from.q_paddr, e->e_from.q_user);
14763753Seric 		}
14855012Seric 		if (e->e_from.q_paddr != NULL &&
14963753Seric 		    (strcmp(fvalue, e->e_from.q_paddr) == 0 ||
15063753Seric 		     strcmp(fvalue, e->e_from.q_user) == 0))
15111414Seric 			return (hi->hi_flags);
15264351Seric #ifdef MAYBENEXTRELEASE		/* XXX UNTESTED XXX UNTESTED XXX UNTESTED XXX */
15369713Seric #if USERDB
15464351Seric 		else
15564351Seric 		{
15664351Seric 			auto ADDRESS a;
15764351Seric 			char *fancy;
15866123Seric 			bool oldSuprErrs = SuprErrs;
15964351Seric 			extern char *crackaddr();
16064351Seric 			extern char *udbsender();
16164351Seric 
16264653Seric 			/*
16364653Seric 			**  Try doing USERDB rewriting even on fully commented
16464653Seric 			**  names; this saves the "comment" information (such
16564653Seric 			**  as full name) and rewrites the electronic part.
16666106Seric 			**
16766106Seric 			** XXX	This code doesn't belong here -- parsing should
16866106Seric 			** XXX	not be done during collect() phase because
16966106Seric 			** XXX	error messages can confuse the SMTP phase.
17066123Seric 			** XXX	Setting SuprErrs is a crude hack around this
17166106Seric 			** XXX	problem.
17264653Seric 			*/
17364653Seric 
17466106Seric 			if (OpMode == MD_SMTP || OpMode == MD_ARPAFTP)
17566123Seric 				SuprErrs = TRUE;
17664351Seric 			fancy = crackaddr(fvalue);
17764351Seric 			if (parseaddr(fvalue, &a, RF_COPYNONE, '\0', NULL, e) != NULL &&
17867472Seric 			    bitnset(M_CHECKUDB, a.q_mailer->m_flags) &&
17964351Seric 			    (p = udbsender(a.q_user)) != NULL)
18064351Seric 			{
18164351Seric 				char *oldg = macvalue('g', e);
18264351Seric 
18364351Seric 				define('g', p, e);
18468529Seric 				expand(fancy, buf, sizeof buf, e);
18564351Seric 				define('g', oldg, e);
18664351Seric 				fvalue = buf;
18764351Seric 			}
18866123Seric 			SuprErrs = oldSuprErrs;
18964351Seric 		}
19064351Seric #endif
19164351Seric #endif
19211414Seric 	}
19311414Seric 
19414784Seric 	/* delete default value for this header */
19568717Seric 	for (hp = hdrp; (h = *hp) != NULL; hp = &h->h_link)
19614784Seric 	{
19759579Seric 		if (strcasecmp(fname, h->h_field) == 0 &&
19814784Seric 		    bitset(H_DEFAULT, h->h_flags) &&
19914784Seric 		    !bitset(H_FORCE, h->h_flags))
20069726Seric 		{
20114784Seric 			h->h_value = NULL;
20269726Seric 			if (!cond)
20369726Seric 			{
20469726Seric 				/* copy conditions from default case */
20569726Seric 				bcopy((char *)h->h_mflags, (char *)mopts,
20669726Seric 						sizeof mopts);
20769726Seric 			}
20869726Seric 		}
20914784Seric 	}
21014784Seric 
21113012Seric 	/* create a new node */
21213012Seric 	h = (HDR *) xalloc(sizeof *h);
21313012Seric 	h->h_field = newstr(fname);
21464134Seric 	h->h_value = newstr(fvalue);
21513012Seric 	h->h_link = NULL;
21623117Seric 	bcopy((char *) mopts, (char *) h->h_mflags, sizeof mopts);
21713012Seric 	*hp = h;
2188066Seric 	h->h_flags = hi->hi_flags;
2194091Seric 	if (def)
2204091Seric 		h->h_flags |= H_DEFAULT;
2219059Seric 	if (cond)
2229059Seric 		h->h_flags |= H_CHECK;
2234091Seric 
2245937Seric 	/* hack to see if this is a new format message */
22568717Seric 	if (!def && !headeronly && bitset(H_RCPT|H_FROM, h->h_flags) &&
22656795Seric 	    (strchr(fvalue, ',') != NULL || strchr(fvalue, '(') != NULL ||
22756795Seric 	     strchr(fvalue, '<') != NULL || strchr(fvalue, ';') != NULL))
2288089Seric 	{
22955012Seric 		e->e_flags &= ~EF_OLDSTYLE;
2308089Seric 	}
2315937Seric 
2324091Seric 	return (h->h_flags);
2334091Seric }
2344091Seric /*
2356980Seric **  ADDHEADER -- add a header entry to the end of the queue.
2366980Seric **
2376980Seric **	This bypasses the special checking of chompheader.
2386980Seric **
2396980Seric **	Parameters:
24059579Seric **		field -- the name of the header field.
24158789Seric **		value -- the value of the field.
24267546Seric **		hp -- an indirect pointer to the header structure list.
2436980Seric **
2446980Seric **	Returns:
2456980Seric **		none.
2466980Seric **
2476980Seric **	Side Effects:
2486980Seric **		adds the field on the list of headers for this envelope.
2496980Seric */
2506980Seric 
251*69748Seric void
25267546Seric addheader(field, value, hdrlist)
2536980Seric 	char *field;
2546980Seric 	char *value;
25567546Seric 	HDR **hdrlist;
2566980Seric {
2576980Seric 	register HDR *h;
2586980Seric 	register struct hdrinfo *hi;
2596980Seric 	HDR **hp;
2606980Seric 
2616980Seric 	/* find info struct */
2626980Seric 	for (hi = HdrInfo; hi->hi_field != NULL; hi++)
2636980Seric 	{
26459579Seric 		if (strcasecmp(field, hi->hi_field) == 0)
2656980Seric 			break;
2666980Seric 	}
2676980Seric 
2686980Seric 	/* find current place in list -- keep back pointer? */
26967546Seric 	for (hp = hdrlist; (h = *hp) != NULL; hp = &h->h_link)
2706980Seric 	{
27159579Seric 		if (strcasecmp(field, h->h_field) == 0)
2726980Seric 			break;
2736980Seric 	}
2746980Seric 
2756980Seric 	/* allocate space for new header */
2766980Seric 	h = (HDR *) xalloc(sizeof *h);
2776980Seric 	h->h_field = field;
2786980Seric 	h->h_value = newstr(value);
2797368Seric 	h->h_link = *hp;
2806980Seric 	h->h_flags = hi->hi_flags | H_DEFAULT;
28110689Seric 	clrbitmap(h->h_mflags);
2826980Seric 	*hp = h;
2836980Seric }
2846980Seric /*
2854091Seric **  HVALUE -- return value of a header.
2864091Seric **
2874091Seric **	Only "real" fields (i.e., ones that have not been supplied
2884091Seric **	as a default) are used.
2894091Seric **
2904091Seric **	Parameters:
2914091Seric **		field -- the field name.
29267546Seric **		header -- the header list.
2934091Seric **
2944091Seric **	Returns:
2954091Seric **		pointer to the value part.
2964091Seric **		NULL if not found.
2974091Seric **
2984091Seric **	Side Effects:
2999382Seric **		none.
3004091Seric */
3014091Seric 
3024091Seric char *
30367546Seric hvalue(field, header)
3044091Seric 	char *field;
30567546Seric 	HDR *header;
3064091Seric {
3074091Seric 	register HDR *h;
3084091Seric 
30967546Seric 	for (h = header; h != NULL; h = h->h_link)
3104091Seric 	{
31159579Seric 		if (!bitset(H_DEFAULT, h->h_flags) &&
31259579Seric 		    strcasecmp(h->h_field, field) == 0)
3134091Seric 			return (h->h_value);
3144091Seric 	}
3154091Seric 	return (NULL);
3164091Seric }
3174091Seric /*
3184091Seric **  ISHEADER -- predicate telling if argument is a header.
3194091Seric **
3204319Seric **	A line is a header if it has a single word followed by
3214319Seric **	optional white space followed by a colon.
3224319Seric **
32368717Seric **	Header fields beginning with two dashes, although technically
32468717Seric **	permitted by RFC822, are automatically rejected in order
32568717Seric **	to make MIME work out.  Without this we could have a technically
32668717Seric **	legal header such as ``--"foo:bar"'' that would also be a legal
32768717Seric **	MIME separator.
32868717Seric **
3294091Seric **	Parameters:
33068690Seric **		h -- string to check for possible headerness.
3314091Seric **
3324091Seric **	Returns:
33368690Seric **		TRUE if h is a header.
3344091Seric **		FALSE otherwise.
3354091Seric **
3364091Seric **	Side Effects:
3374091Seric **		none.
3384091Seric */
3394091Seric 
3404091Seric bool
34168690Seric isheader(h)
34268690Seric 	char *h;
3434091Seric {
34468690Seric 	register char *s = h;
34568690Seric 
34668717Seric 	if (s[0] == '-' && s[1] == '-')
34768717Seric 		return FALSE;
34868717Seric 
3499382Seric 	while (*s > ' ' && *s != ':' && *s != '\0')
3504091Seric 		s++;
3519382Seric 
35268690Seric 	if (h == s)
35368690Seric 		return FALSE;
35468690Seric 
3559382Seric 	/* following technically violates RFC822 */
35658050Seric 	while (isascii(*s) && isspace(*s))
3574091Seric 		s++;
3589382Seric 
3594091Seric 	return (*s == ':');
3604091Seric }
3615919Seric /*
3627783Seric **  EATHEADER -- run through the stored header and extract info.
3637783Seric **
3647783Seric **	Parameters:
3659382Seric **		e -- the envelope to process.
36658929Seric **		full -- if set, do full processing (e.g., compute
36769683Seric **			message priority).  This should not be set
36869683Seric **			when reading a queue file because some info
36969683Seric **			needed to compute the priority is wrong.
3707783Seric **
3717783Seric **	Returns:
3727783Seric **		none.
3737783Seric **
3747783Seric **	Side Effects:
3757783Seric **		Sets a bunch of global variables from information
3769382Seric **			in the collected header.
3779382Seric **		Aborts the message if the hop count is exceeded.
3787783Seric */
3797783Seric 
380*69748Seric void
38158929Seric eatheader(e, full)
3829382Seric 	register ENVELOPE *e;
38358929Seric 	bool full;
3847783Seric {
3857783Seric 	register HDR *h;
3867783Seric 	register char *p;
3879382Seric 	int hopcnt = 0;
38857208Seric 	char *msgid;
38958688Seric 	char buf[MAXLINE];
3907783Seric 
39158688Seric 	/*
39258688Seric 	**  Set up macros for possible expansion in headers.
39358688Seric 	*/
39458688Seric 
39558704Seric 	define('f', e->e_sender, e);
39658704Seric 	define('g', e->e_sender, e);
39764148Seric 	if (e->e_origrcpt != NULL && *e->e_origrcpt != '\0')
39864148Seric 		define('u', e->e_origrcpt, e);
39964148Seric 	else
40064148Seric 		define('u', NULL, e);
40158688Seric 
40265052Seric 	/* full name of from person */
40367546Seric 	p = hvalue("full-name", e->e_header);
40465052Seric 	if (p != NULL)
40565052Seric 		define('x', p, e);
40665052Seric 
4079382Seric 	if (tTd(32, 1))
4089382Seric 		printf("----- collected header -----\n");
40969683Seric 	msgid = NULL;
4109382Seric 	for (h = e->e_header; h != NULL; h = h->h_link)
4117783Seric 	{
41264156Seric 		if (h->h_value == NULL)
41364156Seric 		{
41464156Seric 			if (tTd(32, 1))
41564156Seric 				printf("%s: <NULL>\n", h->h_field);
41664156Seric 			continue;
41764156Seric 		}
41864156Seric 
41958688Seric 		/* do early binding */
42064156Seric 		if (bitset(H_DEFAULT, h->h_flags))
42158688Seric 		{
42268529Seric 			expand(h->h_value, buf, sizeof buf, e);
42358688Seric 			if (buf[0] != '\0')
42458688Seric 			{
42558688Seric 				h->h_value = newstr(buf);
42658688Seric 				h->h_flags &= ~H_DEFAULT;
42758688Seric 			}
42858688Seric 		}
42958688Seric 
4309382Seric 		if (tTd(32, 1))
43165152Seric 		{
43265152Seric 			printf("%s: ", h->h_field);
43365152Seric 			xputs(h->h_value);
43465152Seric 			printf("\n");
43565152Seric 		}
43657359Seric 
43711414Seric 		/* count the number of times it has been processed */
4389382Seric 		if (bitset(H_TRACE, h->h_flags))
4399382Seric 			hopcnt++;
44011414Seric 
44111414Seric 		/* send to this person if we so desire */
44211414Seric 		if (GrabTo && bitset(H_RCPT, h->h_flags) &&
44311414Seric 		    !bitset(H_DEFAULT, h->h_flags) &&
44455012Seric 		    (!bitset(EF_RESENT, e->e_flags) || bitset(H_RESENT, h->h_flags)))
44511414Seric 		{
44663839Seric 			int saveflags = e->e_flags;
44763839Seric 
44864283Seric 			(void) sendtolist(h->h_value, NULLADDR,
44967982Seric 					  &e->e_sendqueue, 0, e);
45063839Seric 
45163839Seric 			/* delete fatal errors generated by this address */
45264148Seric 			if (!GrabTo && !bitset(EF_FATALERRS, saveflags))
45363839Seric 				e->e_flags &= ~EF_FATALERRS;
45411414Seric 		}
45511414Seric 
45657208Seric 		/* save the message-id for logging */
45769683Seric 		if (strcasecmp(h->h_field, "message-id") == 0)
45811290Seric 		{
45957208Seric 			msgid = h->h_value;
46059859Seric 			while (isascii(*msgid) && isspace(*msgid))
46159859Seric 				msgid++;
46211290Seric 		}
46357359Seric 
46457359Seric 		/* see if this is a return-receipt header */
46557359Seric 		if (bitset(H_RECEIPTTO, h->h_flags))
46657359Seric 			e->e_receiptto = h->h_value;
4679382Seric 	}
4689382Seric 	if (tTd(32, 1))
4697783Seric 		printf("----------------------------\n");
4707783Seric 
47158145Seric 	/* if we are just verifying (that is, sendmail -t -bv), drop out now */
47258145Seric 	if (OpMode == MD_VERIFY)
47358145Seric 		return;
47458145Seric 
4759382Seric 	/* store hop count */
4769382Seric 	if (hopcnt > e->e_hopcount)
4779382Seric 		e->e_hopcount = hopcnt;
4789382Seric 
4797783Seric 	/* message priority */
48067546Seric 	p = hvalue("precedence", e->e_header);
4819382Seric 	if (p != NULL)
4829382Seric 		e->e_class = priencode(p);
48369683Seric 	if (e->e_class < 0)
48469683Seric 		e->e_timeoutclass = TOC_NONURGENT;
48569683Seric 	else if (e->e_class > 0)
48669683Seric 		e->e_timeoutclass = TOC_URGENT;
48758929Seric 	if (full)
48867730Seric 	{
48925013Seric 		e->e_msgpriority = e->e_msgsize
49024981Seric 				 - e->e_class * WkClassFact
49124981Seric 				 + e->e_nrcpts * WkRecipFact;
49267730Seric 	}
4937783Seric 
49467730Seric 	/* message timeout priority */
49567730Seric 	p = hvalue("priority", e->e_header);
49669683Seric 	if (p != NULL)
49767730Seric 	{
49867730Seric 		/* (this should be in the configuration file) */
49967730Seric 		if (strcasecmp(p, "urgent"))
50067730Seric 			e->e_timeoutclass = TOC_URGENT;
50167730Seric 		else if (strcasecmp(p, "normal"))
50267730Seric 			e->e_timeoutclass = TOC_NORMAL;
50367730Seric 		else if (strcasecmp(p, "non-urgent"))
50467730Seric 			e->e_timeoutclass = TOC_NONURGENT;
50567730Seric 	}
50667730Seric 
5077783Seric 	/* date message originated */
50867546Seric 	p = hvalue("posted-date", e->e_header);
5097783Seric 	if (p == NULL)
51067546Seric 		p = hvalue("date", e->e_header);
5117783Seric 	if (p != NULL)
5129382Seric 		define('a', p, e);
51311290Seric 
51468884Seric 	/* check to see if this is a MIME message */
51569105Seric 	if ((e->e_bodytype != NULL &&
51669105Seric 	     strcasecmp(e->e_bodytype, "8BITMIME") == 0) ||
51768884Seric 	    hvalue("MIME-Version", e->e_header) != NULL)
51869105Seric 	{
51968884Seric 		e->e_flags |= EF_IS_MIME;
52069105Seric 		if (HasEightBits)
52169105Seric 			e->e_bodytype = "8BITMIME";
52269105Seric 	}
52368884Seric 
52411290Seric 	/*
52565983Seric 	**  From person in antiquated ARPANET mode
52665983Seric 	**	required by UK Grey Book e-mail gateways (sigh)
52765983Seric 	*/
52865983Seric 
52965983Seric 	if (OpMode == MD_ARPAFTP)
53065983Seric 	{
53165983Seric 		register struct hdrinfo *hi;
53265983Seric 
53365983Seric 		for (hi = HdrInfo; hi->hi_field != NULL; hi++)
53465983Seric 		{
53565983Seric 			if (bitset(H_FROM, hi->hi_flags) &&
53665983Seric 			    (!bitset(H_RESENT, hi->hi_flags) ||
53765983Seric 			     bitset(EF_RESENT, e->e_flags)) &&
53867546Seric 			    (p = hvalue(hi->hi_field, e->e_header)) != NULL)
53965983Seric 				break;
54065983Seric 		}
54165983Seric 		if (hi->hi_field != NULL)
54265983Seric 		{
54365983Seric 			if (tTd(32, 2))
54465983Seric 				printf("eatheader: setsender(*%s == %s)\n",
54565983Seric 					hi->hi_field, p);
54665983Seric 			setsender(p, e, NULL, TRUE);
54765983Seric 		}
54865983Seric 	}
54965983Seric 
55065983Seric 	/*
55111290Seric 	**  Log collection information.
55211290Seric 	*/
55311290Seric 
55411290Seric # ifdef LOG
55568036Seric 	if (bitset(EF_LOGSENDER, e->e_flags) && LogLevel > 4)
55665089Seric 		logsender(e, msgid);
55765089Seric # endif /* LOG */
55865089Seric 	e->e_flags &= ~EF_LOGSENDER;
55965089Seric }
56065089Seric /*
56165089Seric **  LOGSENDER -- log sender information
56265089Seric **
56365089Seric **	Parameters:
56465089Seric **		e -- the envelope to log
56565089Seric **		msgid -- the message id
56665089Seric **
56765089Seric **	Returns:
56865089Seric **		none
56965089Seric */
57065089Seric 
571*69748Seric void
57265089Seric logsender(e, msgid)
57365089Seric 	register ENVELOPE *e;
57465089Seric 	char *msgid;
57565089Seric {
57666748Seric # ifdef LOG
57765089Seric 	char *name;
57865089Seric 	register char *sbp;
57965089Seric 	register char *p;
58067901Seric 	int l;
58168528Seric 	char hbuf[MAXNAME + 1];
58268528Seric 	char sbuf[MAXLINE + 1];
58368528Seric 	char mbuf[MAXNAME + 1];
58465089Seric 
58567901Seric 	/* don't allow newlines in the message-id */
58667901Seric 	if (msgid != NULL)
58767901Seric 	{
58867901Seric 		l = strlen(msgid);
58967901Seric 		if (l > sizeof mbuf - 1)
59067901Seric 			l = sizeof mbuf - 1;
59167901Seric 		bcopy(msgid, mbuf, l);
59267901Seric 		mbuf[l] = '\0';
59367901Seric 		p = mbuf;
59467901Seric 		while ((p = strchr(p, '\n')) != NULL)
59567901Seric 			*p++ = ' ';
59667901Seric 	}
59767901Seric 
59865089Seric 	if (bitset(EF_RESPONSE, e->e_flags))
59965089Seric 		name = "[RESPONSE]";
60065089Seric 	else if ((name = macvalue('_', e)) != NULL)
60165089Seric 		;
60266003Seric 	else if (RealHostName == NULL)
60366003Seric 		name = "localhost";
60465089Seric 	else if (RealHostName[0] == '[')
60565089Seric 		name = RealHostName;
60665089Seric 	else
60711290Seric 	{
60865089Seric 		name = hbuf;
60965089Seric 		(void) sprintf(hbuf, "%.80s", RealHostName);
61065089Seric 		if (RealHostAddr.sa.sa_family != 0)
61157359Seric 		{
61265089Seric 			p = &hbuf[strlen(hbuf)];
61365089Seric 			(void) sprintf(p, " (%s)",
61465089Seric 				anynet_ntoa(&RealHostAddr));
61557359Seric 		}
61665089Seric 	}
61757359Seric 
61865089Seric 	/* some versions of syslog only take 5 printf args */
61965059Seric #  if (SYSLOG_BUFSIZE) >= 256
62065089Seric 	sbp = sbuf;
62165089Seric 	sprintf(sbp, "from=%.200s, size=%ld, class=%d, pri=%ld, nrcpts=%d",
62267788Seric 	    e->e_from.q_paddr == NULL ? "<NONE>" : e->e_from.q_paddr,
62367788Seric 	    e->e_msgsize, e->e_class, e->e_msgpriority, e->e_nrcpts);
62465089Seric 	sbp += strlen(sbp);
62565089Seric 	if (msgid != NULL)
62665089Seric 	{
62767901Seric 		sprintf(sbp, ", msgid=%.100s", mbuf);
62860575Seric 		sbp += strlen(sbp);
62965089Seric 	}
63065089Seric 	if (e->e_bodytype != NULL)
63165089Seric 	{
63265089Seric 		(void) sprintf(sbp, ", bodytype=%.20s", e->e_bodytype);
63365089Seric 		sbp += strlen(sbp);
63465089Seric 	}
63565089Seric 	p = macvalue('r', e);
63665089Seric 	if (p != NULL)
63765089Seric 		(void) sprintf(sbp, ", proto=%.20s", p);
63865089Seric 	syslog(LOG_INFO, "%s: %s, relay=%s",
63965089Seric 	    e->e_id, sbuf, name);
64065059Seric 
64165059Seric #  else			/* short syslog buffer */
64265059Seric 
64365089Seric 	syslog(LOG_INFO, "%s: from=%s",
64467788Seric 		e->e_id, e->e_from.q_paddr == NULL ? "<NONE>" :
64567788Seric 				shortenstring(e->e_from.q_paddr, 83));
64665089Seric 	syslog(LOG_INFO, "%s: size=%ld, class=%ld, pri=%ld, nrcpts=%d",
64765089Seric 		e->e_id, e->e_msgsize, e->e_class,
64865089Seric 		e->e_msgpriority, e->e_nrcpts);
64965089Seric 	if (msgid != NULL)
65067901Seric 		syslog(LOG_INFO, "%s: msgid=%s", e->e_id, mbuf);
65165650Seric 	sbp = sbuf;
65265650Seric 	sprintf(sbp, "%s:", e->e_id);
65365650Seric 	sbp += strlen(sbp);
65465089Seric 	if (e->e_bodytype != NULL)
65565650Seric 	{
65665650Seric 		sprintf(sbp, " bodytype=%s,", e->e_bodytype);
65765650Seric 		sbp += strlen(sbp);
65865650Seric 	}
65965089Seric 	p = macvalue('r', e);
66065089Seric 	if (p != NULL)
66165650Seric 	{
66265731Seric 		sprintf(sbp, " proto=%s,", p);
66365650Seric 		sbp += strlen(sbp);
66465650Seric 	}
66565650Seric 	syslog(LOG_INFO, "%s relay=%s", sbuf, name);
66665059Seric #  endif
66766748Seric # endif
6687783Seric }
6697783Seric /*
6707783Seric **  PRIENCODE -- encode external priority names into internal values.
6717783Seric **
6727783Seric **	Parameters:
6737783Seric **		p -- priority in ascii.
6747783Seric **
6757783Seric **	Returns:
6767783Seric **		priority as a numeric level.
6777783Seric **
6787783Seric **	Side Effects:
6797783Seric **		none.
6807783Seric */
6817783Seric 
682*69748Seric int
6837783Seric priencode(p)
6847783Seric 	char *p;
6857783Seric {
6868253Seric 	register int i;
6877783Seric 
6888253Seric 	for (i = 0; i < NumPriorities; i++)
6897783Seric 	{
69033725Sbostic 		if (!strcasecmp(p, Priorities[i].pri_name))
6918253Seric 			return (Priorities[i].pri_val);
6927783Seric 	}
6938253Seric 
6948253Seric 	/* unknown priority */
6958253Seric 	return (0);
6967783Seric }
6977783Seric /*
6987890Seric **  CRACKADDR -- parse an address and turn it into a macro
6997783Seric **
7007783Seric **	This doesn't actually parse the address -- it just extracts
7017783Seric **	it and replaces it with "$g".  The parse is totally ad hoc
7027783Seric **	and isn't even guaranteed to leave something syntactically
7037783Seric **	identical to what it started with.  However, it does leave
7047783Seric **	something semantically identical.
7057783Seric **
70651379Seric **	This algorithm has been cleaned up to handle a wider range
70751379Seric **	of cases -- notably quoted and backslash escaped strings.
70851379Seric **	This modification makes it substantially better at preserving
70951379Seric **	the original syntax.
7107783Seric **
7117783Seric **	Parameters:
7127890Seric **		addr -- the address to be cracked.
7137783Seric **
7147783Seric **	Returns:
7157783Seric **		a pointer to the new version.
7167783Seric **
7177783Seric **	Side Effects:
7187890Seric **		none.
7197783Seric **
7207783Seric **	Warning:
7217783Seric **		The return value is saved in local storage and should
7227783Seric **		be copied if it is to be reused.
7237783Seric */
7247783Seric 
7257783Seric char *
7267890Seric crackaddr(addr)
7277890Seric 	register char *addr;
7287783Seric {
7297783Seric 	register char *p;
73051379Seric 	register char c;
73151379Seric 	int cmtlev;
73256764Seric 	int realcmtlev;
73356764Seric 	int anglelev, realanglelev;
73451379Seric 	int copylev;
73551379Seric 	bool qmode;
73656764Seric 	bool realqmode;
73756764Seric 	bool skipping;
73851379Seric 	bool putgmac = FALSE;
73956735Seric 	bool quoteit = FALSE;
74064148Seric 	bool gotangle = FALSE;
74168756Seric 	bool gotcolon = FALSE;
74251379Seric 	register char *bp;
74356764Seric 	char *buflim;
74468756Seric 	char *bufhead;
74568756Seric 	char *addrhead;
74668528Seric 	static char buf[MAXNAME + 1];
7477783Seric 
7487783Seric 	if (tTd(33, 1))
7497890Seric 		printf("crackaddr(%s)\n", addr);
7507783Seric 
7518082Seric 	/* strip leading spaces */
75258050Seric 	while (*addr != '\0' && isascii(*addr) && isspace(*addr))
7538082Seric 		addr++;
7548082Seric 
7557783Seric 	/*
75651379Seric 	**  Start by assuming we have no angle brackets.  This will be
75751379Seric 	**  adjusted later if we find them.
7587783Seric 	*/
7597783Seric 
76068756Seric 	bp = bufhead = buf;
76156764Seric 	buflim = &buf[sizeof buf - 5];
76268756Seric 	p = addrhead = addr;
76356764Seric 	copylev = anglelev = realanglelev = cmtlev = realcmtlev = 0;
76456764Seric 	qmode = realqmode = FALSE;
76551379Seric 
76651379Seric 	while ((c = *p++) != '\0')
7677783Seric 	{
76856764Seric 		/*
76956764Seric 		**  If the buffer is overful, go into a special "skipping"
77056764Seric 		**  mode that tries to keep legal syntax but doesn't actually
77156764Seric 		**  output things.
77256764Seric 		*/
7737783Seric 
77456764Seric 		skipping = bp >= buflim;
77556735Seric 
77656764Seric 		if (copylev > 0 && !skipping)
77756764Seric 			*bp++ = c;
77856735Seric 
77951379Seric 		/* check for backslash escapes */
78051379Seric 		if (c == '\\')
7817783Seric 		{
78258890Seric 			/* arrange to quote the address */
78358890Seric 			if (cmtlev <= 0 && !qmode)
78458890Seric 				quoteit = TRUE;
78558890Seric 
78651379Seric 			if ((c = *p++) == '\0')
7877783Seric 			{
78851379Seric 				/* too far */
78951379Seric 				p--;
79051379Seric 				goto putg;
7917783Seric 			}
79256764Seric 			if (copylev > 0 && !skipping)
79351379Seric 				*bp++ = c;
79451379Seric 			goto putg;
7957783Seric 		}
7967783Seric 
79751379Seric 		/* check for quoted strings */
79864967Seric 		if (c == '"' && cmtlev <= 0)
7997783Seric 		{
80051379Seric 			qmode = !qmode;
80156764Seric 			if (copylev > 0 && !skipping)
80256764Seric 				realqmode = !realqmode;
80351379Seric 			continue;
8047783Seric 		}
80551379Seric 		if (qmode)
80651379Seric 			goto putg;
8077783Seric 
80851379Seric 		/* check for comments */
80951379Seric 		if (c == '(')
8107783Seric 		{
81151379Seric 			cmtlev++;
81256764Seric 
81356764Seric 			/* allow space for closing paren */
81456764Seric 			if (!skipping)
81556764Seric 			{
81656764Seric 				buflim--;
81756764Seric 				realcmtlev++;
81856764Seric 				if (copylev++ <= 0)
81956764Seric 				{
82056764Seric 					*bp++ = ' ';
82156764Seric 					*bp++ = c;
82256764Seric 				}
82356764Seric 			}
82451379Seric 		}
82551379Seric 		if (cmtlev > 0)
82651379Seric 		{
82751379Seric 			if (c == ')')
8287783Seric 			{
82951379Seric 				cmtlev--;
83051379Seric 				copylev--;
83156764Seric 				if (!skipping)
83256764Seric 				{
83356764Seric 					realcmtlev--;
83456764Seric 					buflim++;
83556764Seric 				}
8367783Seric 			}
8377783Seric 			continue;
8387783Seric 		}
83956764Seric 		else if (c == ')')
84056764Seric 		{
84156764Seric 			/* syntax error: unmatched ) */
84265544Seric 			if (copylev > 0 && !skipping)
84356764Seric 				bp--;
84456764Seric 		}
8457783Seric 
84668756Seric 		/* check for group: list; syntax */
84768760Seric 		if (c == ':' && anglelev <= 0 && !gotcolon && !ColonOkInAddr)
84868756Seric 		{
84968756Seric 			register char *q;
85068756Seric 
85168760Seric 			if (*p == ':')
85268760Seric 			{
85368760Seric 				/* special case -- :: syntax */
85468760Seric 				if (cmtlev <= 0 && !qmode)
85568760Seric 					quoteit = TRUE;
85668760Seric 				if (copylev > 0 && !skipping)
85768760Seric 				{
85868760Seric 					*bp++ = c;
85968760Seric 					*bp++ = c;
86068760Seric 				}
86168760Seric 				p++;
86268760Seric 				goto putg;
86368760Seric 			}
86468760Seric 
86568756Seric 			gotcolon = TRUE;
86668756Seric 
86768760Seric 			bp = bufhead;
86868760Seric 			if (quoteit)
86968760Seric 			{
87068760Seric 				*bp++ = '"';
87168760Seric 
87268760Seric 				/* back up over the ':' and any spaces */
87368760Seric 				--p;
87468760Seric 				while (isascii(*--p) && isspace(*p))
87568760Seric 					continue;
87668756Seric 				p++;
87768760Seric 			}
87868756Seric 			for (q = addrhead; q < p; )
87968756Seric 			{
88068756Seric 				c = *q++;
88168756Seric 				if (bp < buflim)
88268756Seric 				{
88368760Seric 					if (quoteit && c == '"')
88468760Seric 						*bp++ = '\\';
88568756Seric 					*bp++ = c;
88668756Seric 				}
88768756Seric 			}
88868760Seric 			if (quoteit)
88968760Seric 			{
89068760Seric 				if (bp == &bufhead[1])
89168760Seric 					bp--;
89268760Seric 				else
89368760Seric 					*bp++ = '"';
89468760Seric 				while ((c = *p++) != ':')
89568760Seric 				{
89668760Seric 					if (bp < buflim)
89768760Seric 						*bp++ = c;
89868760Seric 				}
89968760Seric 				*bp++ = c;
90068760Seric 			}
90168760Seric 
90268760Seric 			/* any trailing white space is part of group: */
90368760Seric 			while (isascii(*p) && isspace(*p) && bp < buflim)
90468760Seric 				*bp++ = *p++;
90568756Seric 			copylev = 0;
90668760Seric 			putgmac = quoteit = FALSE;
90768756Seric 			bufhead = bp;
90868756Seric 			addrhead = p;
90968756Seric 			continue;
91068756Seric 		}
91168756Seric 
91268756Seric 		if (c == ';' && copylev <= 0 && !ColonOkInAddr)
91368756Seric 		{
91468756Seric 			if (bp < buflim)
91568756Seric 				*bp++ = c;
91668756Seric 		}
91768756Seric 
91856764Seric 		/* check for characters that may have to be quoted */
91964148Seric 		if (strchr(".'@,;:\\()[]", c) != NULL)
92056764Seric 		{
92156764Seric 			/*
92256764Seric 			**  If these occur as the phrase part of a <>
92356764Seric 			**  construct, but are not inside of () or already
92456764Seric 			**  quoted, they will have to be quoted.  Note that
92556764Seric 			**  now (but don't actually do the quoting).
92656764Seric 			*/
92756764Seric 
92856764Seric 			if (cmtlev <= 0 && !qmode)
92956764Seric 				quoteit = TRUE;
93056764Seric 		}
93156764Seric 
93251379Seric 		/* check for angle brackets */
93351379Seric 		if (c == '<')
93451379Seric 		{
93556735Seric 			register char *q;
93656735Seric 
93764148Seric 			/* assume first of two angles is bogus */
93864148Seric 			if (gotangle)
93964148Seric 				quoteit = TRUE;
94064148Seric 			gotangle = TRUE;
94164148Seric 
94251379Seric 			/* oops -- have to change our mind */
94364752Seric 			anglelev = 1;
94456764Seric 			if (!skipping)
94564752Seric 				realanglelev = 1;
94656764Seric 
94768756Seric 			bp = bufhead;
94856735Seric 			if (quoteit)
94956735Seric 			{
95056735Seric 				*bp++ = '"';
95156735Seric 
95256735Seric 				/* back up over the '<' and any spaces */
95356735Seric 				--p;
95458050Seric 				while (isascii(*--p) && isspace(*p))
95556735Seric 					continue;
95656735Seric 				p++;
95756735Seric 			}
95868756Seric 			for (q = addrhead; q < p; )
95956735Seric 			{
96056735Seric 				c = *q++;
96156764Seric 				if (bp < buflim)
96256764Seric 				{
96356764Seric 					if (quoteit && c == '"')
96456764Seric 						*bp++ = '\\';
96556764Seric 					*bp++ = c;
96656764Seric 				}
96756735Seric 			}
96856735Seric 			if (quoteit)
96956735Seric 			{
97064148Seric 				if (bp == &buf[1])
97164148Seric 					bp--;
97264148Seric 				else
97364148Seric 					*bp++ = '"';
97456764Seric 				while ((c = *p++) != '<')
97556764Seric 				{
97656764Seric 					if (bp < buflim)
97756764Seric 						*bp++ = c;
97856764Seric 				}
97956764Seric 				*bp++ = c;
98056735Seric 			}
98151379Seric 			copylev = 0;
98256735Seric 			putgmac = quoteit = FALSE;
98351379Seric 			continue;
98451379Seric 		}
9857783Seric 
98651379Seric 		if (c == '>')
9877783Seric 		{
98856764Seric 			if (anglelev > 0)
98956764Seric 			{
99056764Seric 				anglelev--;
99156764Seric 				if (!skipping)
99256764Seric 				{
99356764Seric 					realanglelev--;
99456764Seric 					buflim++;
99556764Seric 				}
99656764Seric 			}
99756764Seric 			else if (!skipping)
99856764Seric 			{
99956764Seric 				/* syntax error: unmatched > */
100056764Seric 				if (copylev > 0)
100156764Seric 					bp--;
100264752Seric 				quoteit = TRUE;
100356764Seric 				continue;
100456764Seric 			}
100551379Seric 			if (copylev++ <= 0)
100651379Seric 				*bp++ = c;
100751379Seric 			continue;
10087783Seric 		}
100951379Seric 
101051379Seric 		/* must be a real address character */
101151379Seric 	putg:
101251379Seric 		if (copylev <= 0 && !putgmac)
101351379Seric 		{
101458050Seric 			*bp++ = MACROEXPAND;
101551379Seric 			*bp++ = 'g';
101651379Seric 			putgmac = TRUE;
101751379Seric 		}
10187783Seric 	}
10197783Seric 
102056764Seric 	/* repair any syntactic damage */
102156764Seric 	if (realqmode)
102256764Seric 		*bp++ = '"';
102356764Seric 	while (realcmtlev-- > 0)
102456764Seric 		*bp++ = ')';
102556764Seric 	while (realanglelev-- > 0)
102656764Seric 		*bp++ = '>';
102751379Seric 	*bp++ = '\0';
10287783Seric 
10297783Seric 	if (tTd(33, 1))
10307944Seric 		printf("crackaddr=>`%s'\n", buf);
10317783Seric 
10327783Seric 	return (buf);
10337783Seric }
10349382Seric /*
10359382Seric **  PUTHEADER -- put the header part of a message from the in-core copy
10369382Seric **
10379382Seric **	Parameters:
103865870Seric **		mci -- the connection information.
103967546Seric **		h -- the header to put.
10409382Seric **		e -- envelope to use.
10419382Seric **
10429382Seric **	Returns:
10439382Seric **		none.
10449382Seric **
10459382Seric **	Side Effects:
10469382Seric **		none.
10479382Seric */
10489382Seric 
104957589Seric /*
105057589Seric  * Macro for fast max (not available in e.g. DG/UX, 386/ix).
105157589Seric  */
105257589Seric #ifndef MAX
105357589Seric # define MAX(a,b) (((a)>(b))?(a):(b))
105457589Seric #endif
105557589Seric 
1056*69748Seric void
105768228Seric putheader(mci, h, e)
105865870Seric 	register MCI *mci;
105967546Seric 	register HDR *h;
10609382Seric 	register ENVELOPE *e;
10619382Seric {
106257135Seric 	char buf[MAX(MAXLINE,BUFSIZ)];
106357135Seric 	char obuf[MAXLINE];
10649382Seric 
106559882Seric 	if (tTd(34, 1))
106665870Seric 		printf("--- putheader, mailer = %s ---\n",
106765870Seric 			mci->mci_mailer->m_name);
106859882Seric 
106967546Seric 	mci->mci_flags |= MCIF_INHEADER;
107067546Seric 	for (; h != NULL; h = h->h_link)
10719382Seric 	{
107269474Seric 		register char *p = h->h_value;
107310689Seric 		extern bool bitintersect();
10749382Seric 
107559882Seric 		if (tTd(34, 11))
107659882Seric 		{
107759882Seric 			printf("  %s: ", h->h_field);
107869474Seric 			xputs(p);
107959882Seric 		}
108059882Seric 
108169281Seric 		/* suppress Content-Transfer-Encoding: if we are MIMEing */
108269281Seric 		if (bitset(H_CTE, h->h_flags) &&
108369281Seric 		    bitset(MCIF_CVT8TO7|MCIF_INMIME, mci->mci_flags))
108469281Seric 		{
108569281Seric 			if (tTd(34, 11))
108669281Seric 				printf(" (skipped (content-transfer-encoding))\n");
108769281Seric 			continue;
108869281Seric 		}
108969281Seric 
109069281Seric 		if (bitset(MCIF_INMIME, mci->mci_flags))
109169281Seric 			goto vanilla;
109269281Seric 
10939382Seric 		if (bitset(H_CHECK|H_ACHECK, h->h_flags) &&
109465870Seric 		    !bitintersect(h->h_mflags, mci->mci_mailer->m_flags))
109559882Seric 		{
109659882Seric 			if (tTd(34, 11))
109759882Seric 				printf(" (skipped)\n");
10989382Seric 			continue;
109959882Seric 		}
11009382Seric 
110111414Seric 		/* handle Resent-... headers specially */
110211414Seric 		if (bitset(H_RESENT, h->h_flags) && !bitset(EF_RESENT, e->e_flags))
110359882Seric 		{
110459882Seric 			if (tTd(34, 11))
110559882Seric 				printf(" (skipped (resent))\n");
110611414Seric 			continue;
110759882Seric 		}
110811414Seric 
110966784Seric 		/* suppress return receipts if requested */
111066784Seric 		if (bitset(H_RECEIPTTO, h->h_flags) &&
111166784Seric 		    bitset(EF_NORECEIPT, e->e_flags))
111266784Seric 		{
111366784Seric 			if (tTd(34, 11))
111466784Seric 				printf(" (skipped (receipt))\n");
111566784Seric 			continue;
111666784Seric 		}
111766784Seric 
111865152Seric 		/* macro expand value if generated internally */
11199382Seric 		if (bitset(H_DEFAULT, h->h_flags))
11209382Seric 		{
112168529Seric 			expand(p, buf, sizeof buf, e);
11229382Seric 			p = buf;
11239382Seric 			if (p == NULL || *p == '\0')
112465152Seric 			{
112565152Seric 				if (tTd(34, 11))
112665152Seric 					printf(" (skipped -- null value)\n");
11279382Seric 				continue;
112865152Seric 			}
11299382Seric 		}
11309382Seric 
113165152Seric 		if (tTd(34, 11))
113265152Seric 			printf("\n");
113365152Seric 
113468449Seric 		if (bitset(H_STRIPVAL, h->h_flags))
11359382Seric 		{
113668449Seric 			/* empty field */
113768449Seric 			(void) sprintf(obuf, "%s:", h->h_field);
113868449Seric 			putline(obuf, mci);
113968449Seric 		}
114068449Seric 		else if (bitset(H_FROM|H_RCPT, h->h_flags))
114168449Seric 		{
11429382Seric 			/* address field */
11439382Seric 			bool oldstyle = bitset(EF_OLDSTYLE, e->e_flags);
11449382Seric 
11459382Seric 			if (bitset(H_FROM, h->h_flags))
11469382Seric 				oldstyle = FALSE;
114765870Seric 			commaize(h, p, oldstyle, mci, e);
11489382Seric 		}
11499382Seric 		else
11509382Seric 		{
11519382Seric 			/* vanilla header line */
115212159Seric 			register char *nlp;
115312159Seric 
115469281Seric vanilla:
115559579Seric 			(void) sprintf(obuf, "%s: ", h->h_field);
115656795Seric 			while ((nlp = strchr(p, '\n')) != NULL)
115712159Seric 			{
115812159Seric 				*nlp = '\0';
115912159Seric 				(void) strcat(obuf, p);
116012159Seric 				*nlp = '\n';
116165870Seric 				putline(obuf, mci);
116212159Seric 				p = ++nlp;
116312161Seric 				obuf[0] = '\0';
116412159Seric 			}
116512159Seric 			(void) strcat(obuf, p);
116665870Seric 			putline(obuf, mci);
11679382Seric 		}
11689382Seric 	}
116967887Seric 
117067887Seric 	/*
117167887Seric 	**  If we are converting this to a MIME message, add the
117267889Seric 	**  MIME headers.
117367887Seric 	*/
117467887Seric 
117569480Seric #if MIME8TO7
117667887Seric 	if (bitset(MM_MIME8BIT, MimeMode) &&
117767887Seric 	    bitset(EF_HAS8BIT, e->e_flags) &&
117867887Seric 	    !bitnset(M_8BITS, mci->mci_mailer->m_flags) &&
117967889Seric 	    !bitset(MCIF_CVT8TO7, mci->mci_flags))
118067887Seric 	{
118167889Seric 		if (hvalue("MIME-Version", e->e_header) == NULL)
118267889Seric 			putline("MIME-Version: 1.0", mci);
118367889Seric 		if (hvalue("Content-Type", e->e_header) == NULL)
118467889Seric 		{
118567889Seric 			sprintf(obuf, "Content-Type: text/plain; charset=%s",
118667896Seric 				defcharset(e));
118767889Seric 			putline(obuf, mci);
118867889Seric 		}
118967889Seric 		if (hvalue("Content-Transfer-Encoding", e->e_header) == NULL)
119067889Seric 			putline("Content-Transfer-Encoding: 8bit", mci);
119167887Seric 	}
119269480Seric #endif
11939382Seric }
11949382Seric /*
11959382Seric **  COMMAIZE -- output a header field, making a comma-translated list.
11969382Seric **
11979382Seric **	Parameters:
11989382Seric **		h -- the header field to output.
11999382Seric **		p -- the value to put in it.
12009382Seric **		oldstyle -- TRUE if this is an old style header.
120165870Seric **		mci -- the connection information.
120255012Seric **		e -- the envelope containing the message.
12039382Seric **
12049382Seric **	Returns:
12059382Seric **		none.
12069382Seric **
12079382Seric **	Side Effects:
12089382Seric **		outputs "p" to file "fp".
12099382Seric */
12109382Seric 
121165870Seric void
121265870Seric commaize(h, p, oldstyle, mci, e)
12139382Seric 	register HDR *h;
12149382Seric 	register char *p;
12159382Seric 	bool oldstyle;
121665870Seric 	register MCI *mci;
121755012Seric 	register ENVELOPE *e;
12189382Seric {
12199382Seric 	register char *obp;
12209382Seric 	int opos;
122166004Seric 	int omax;
12229382Seric 	bool firstone = TRUE;
122311157Seric 	char obuf[MAXLINE + 3];
12249382Seric 
12259382Seric 	/*
12269382Seric 	**  Output the address list translated by the
12279382Seric 	**  mailer and with commas.
12289382Seric 	*/
12299382Seric 
12309382Seric 	if (tTd(14, 2))
12319382Seric 		printf("commaize(%s: %s)\n", h->h_field, p);
12329382Seric 
12339382Seric 	obp = obuf;
123459579Seric 	(void) sprintf(obp, "%s: ", h->h_field);
12359382Seric 	opos = strlen(h->h_field) + 2;
12369382Seric 	obp += opos;
123766254Seric 	omax = mci->mci_mailer->m_linelimit - 2;
123866254Seric 	if (omax < 0 || omax > 78)
123966254Seric 		omax = 78;
12409382Seric 
12419382Seric 	/*
12429382Seric 	**  Run through the list of values.
12439382Seric 	*/
12449382Seric 
12459382Seric 	while (*p != '\0')
12469382Seric 	{
12479382Seric 		register char *name;
124854983Seric 		register int c;
12499382Seric 		char savechar;
125059163Seric 		int flags;
125159163Seric 		auto int stat;
12529382Seric 
12539382Seric 		/*
12549382Seric 		**  Find the end of the name.  New style names
12559382Seric 		**  end with a comma, old style names end with
12569382Seric 		**  a space character.  However, spaces do not
12579382Seric 		**  necessarily delimit an old-style name -- at
12589382Seric 		**  signs mean keep going.
12599382Seric 		*/
12609382Seric 
12619382Seric 		/* find end of name */
126258050Seric 		while ((isascii(*p) && isspace(*p)) || *p == ',')
12639382Seric 			p++;
12649382Seric 		name = p;
12659382Seric 		for (;;)
12669382Seric 		{
126758333Seric 			auto char *oldp;
126816909Seric 			char pvpbuf[PSBUFSIZE];
12699382Seric 
127065066Seric 			(void) prescan(p, oldstyle ? ' ' : ',', pvpbuf,
127168711Seric 				       sizeof pvpbuf, &oldp, NULL);
127258333Seric 			p = oldp;
12739382Seric 
12749382Seric 			/* look to see if we have an at sign */
127558050Seric 			while (*p != '\0' && isascii(*p) && isspace(*p))
12769382Seric 				p++;
12779382Seric 
127858170Seric 			if (*p != '@')
12799382Seric 			{
12809382Seric 				p = oldp;
12819382Seric 				break;
12829382Seric 			}
12839382Seric 			p += *p == '@' ? 1 : 2;
128458050Seric 			while (*p != '\0' && isascii(*p) && isspace(*p))
12859382Seric 				p++;
12869382Seric 		}
12879382Seric 		/* at the end of one complete name */
12889382Seric 
12899382Seric 		/* strip off trailing white space */
129058050Seric 		while (p >= name &&
129158050Seric 		       ((isascii(*p) && isspace(*p)) || *p == ',' || *p == '\0'))
12929382Seric 			p--;
12939382Seric 		if (++p == name)
12949382Seric 			continue;
12959382Seric 		savechar = *p;
12969382Seric 		*p = '\0';
12979382Seric 
12989382Seric 		/* translate the name to be relative */
129959163Seric 		flags = RF_HEADERADDR|RF_ADDDOMAIN;
130059163Seric 		if (bitset(H_FROM, h->h_flags))
130159163Seric 			flags |= RF_SENDERADDR;
130269713Seric #if USERDB
130368522Seric 		else if (e->e_from.q_mailer != NULL &&
130468522Seric 			 bitnset(M_UDBRECIPIENT, e->e_from.q_mailer->m_flags))
130568522Seric 		{
130668522Seric 			extern char *udbsender();
130768522Seric 
130868522Seric 			name = udbsender(name);
130968522Seric 		}
131068522Seric #endif
131159163Seric 		stat = EX_OK;
131265870Seric 		name = remotename(name, mci->mci_mailer, flags, &stat, e);
13139382Seric 		if (*name == '\0')
13149382Seric 		{
13159382Seric 			*p = savechar;
13169382Seric 			continue;
13179382Seric 		}
13189382Seric 
13199382Seric 		/* output the name with nice formatting */
132054983Seric 		opos += strlen(name);
13219382Seric 		if (!firstone)
13229382Seric 			opos += 2;
132366004Seric 		if (opos > omax && !firstone)
13249382Seric 		{
132510178Seric 			(void) strcpy(obp, ",\n");
132665870Seric 			putline(obuf, mci);
13279382Seric 			obp = obuf;
132866255Seric 			(void) strcpy(obp, "        ");
132910161Seric 			opos = strlen(obp);
133010161Seric 			obp += opos;
133154983Seric 			opos += strlen(name);
13329382Seric 		}
13339382Seric 		else if (!firstone)
13349382Seric 		{
133566255Seric 			(void) strcpy(obp, ", ");
13369382Seric 			obp += 2;
13379382Seric 		}
13389382Seric 
133954983Seric 		while ((c = *name++) != '\0' && obp < &obuf[MAXLINE])
134054983Seric 			*obp++ = c;
13419382Seric 		firstone = FALSE;
13429382Seric 		*p = savechar;
13439382Seric 	}
13449382Seric 	(void) strcpy(obp, "\n");
134565870Seric 	putline(obuf, mci);
13469382Seric }
13479382Seric /*
134858170Seric **  COPYHEADER -- copy header list
13499382Seric **
135058170Seric **	This routine is the equivalent of newstr for header lists
135158170Seric **
13529382Seric **	Parameters:
135358170Seric **		header -- list of header structures to copy.
13549382Seric **
13559382Seric **	Returns:
135658170Seric **		a copy of 'header'.
13579382Seric **
13589382Seric **	Side Effects:
13599382Seric **		none.
13609382Seric */
13619382Seric 
136258170Seric HDR *
136358170Seric copyheader(header)
136458170Seric 	register HDR *header;
13659382Seric {
136658170Seric 	register HDR *newhdr;
136758170Seric 	HDR *ret;
136858170Seric 	register HDR **tail = &ret;
13699382Seric 
137058170Seric 	while (header != NULL)
137158170Seric 	{
137258170Seric 		newhdr = (HDR *) xalloc(sizeof(HDR));
137358170Seric 		STRUCTCOPY(*header, *newhdr);
137458170Seric 		*tail = newhdr;
137558170Seric 		tail = &newhdr->h_link;
137658170Seric 		header = header->h_link;
137758170Seric 	}
137858170Seric 	*tail = NULL;
137958170Seric 
138058170Seric 	return ret;
13819382Seric }
1382