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*69926Seric static char sccsid[] = "@(#)headers.c	8.70 (Berkeley) 06/19/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 
3569748Seric int
chompheader(line,def,hdrp,e)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 
25169748Seric void
addheader(field,value,hdrlist)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 *
hvalue(field,header)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
isheader(h)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 
38069748Seric void
eatheader(e,full)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 	{
41269794Seric 		if (tTd(32, 1))
41369794Seric 			printf("%s: ", h->h_field);
41464156Seric 		if (h->h_value == NULL)
41564156Seric 		{
41664156Seric 			if (tTd(32, 1))
41769794Seric 				printf("<NULL>\n");
41864156Seric 			continue;
41964156Seric 		}
42064156Seric 
42158688Seric 		/* do early binding */
42264156Seric 		if (bitset(H_DEFAULT, h->h_flags))
42358688Seric 		{
42469794Seric 			if (tTd(32, 1))
42569794Seric 			{
42669794Seric 				printf("(");
42769794Seric 				xputs(h->h_value);
42869794Seric 				printf(") ");
42969794Seric 			}
43068529Seric 			expand(h->h_value, buf, sizeof buf, e);
43158688Seric 			if (buf[0] != '\0')
43258688Seric 			{
43358688Seric 				h->h_value = newstr(buf);
43458688Seric 				h->h_flags &= ~H_DEFAULT;
43558688Seric 			}
43658688Seric 		}
43758688Seric 
4389382Seric 		if (tTd(32, 1))
43965152Seric 		{
44065152Seric 			xputs(h->h_value);
44165152Seric 			printf("\n");
44265152Seric 		}
44357359Seric 
44411414Seric 		/* count the number of times it has been processed */
4459382Seric 		if (bitset(H_TRACE, h->h_flags))
4469382Seric 			hopcnt++;
44711414Seric 
44811414Seric 		/* send to this person if we so desire */
44911414Seric 		if (GrabTo && bitset(H_RCPT, h->h_flags) &&
45011414Seric 		    !bitset(H_DEFAULT, h->h_flags) &&
45155012Seric 		    (!bitset(EF_RESENT, e->e_flags) || bitset(H_RESENT, h->h_flags)))
45211414Seric 		{
45363839Seric 			int saveflags = e->e_flags;
45463839Seric 
45564283Seric 			(void) sendtolist(h->h_value, NULLADDR,
45667982Seric 					  &e->e_sendqueue, 0, e);
45763839Seric 
45863839Seric 			/* delete fatal errors generated by this address */
45964148Seric 			if (!GrabTo && !bitset(EF_FATALERRS, saveflags))
46063839Seric 				e->e_flags &= ~EF_FATALERRS;
46111414Seric 		}
46211414Seric 
46357208Seric 		/* save the message-id for logging */
46469683Seric 		if (strcasecmp(h->h_field, "message-id") == 0)
46511290Seric 		{
46657208Seric 			msgid = h->h_value;
46759859Seric 			while (isascii(*msgid) && isspace(*msgid))
46859859Seric 				msgid++;
46911290Seric 		}
47057359Seric 
47157359Seric 		/* see if this is a return-receipt header */
47257359Seric 		if (bitset(H_RECEIPTTO, h->h_flags))
47357359Seric 			e->e_receiptto = h->h_value;
4749382Seric 	}
4759382Seric 	if (tTd(32, 1))
4767783Seric 		printf("----------------------------\n");
4777783Seric 
47858145Seric 	/* if we are just verifying (that is, sendmail -t -bv), drop out now */
47958145Seric 	if (OpMode == MD_VERIFY)
48058145Seric 		return;
48158145Seric 
4829382Seric 	/* store hop count */
4839382Seric 	if (hopcnt > e->e_hopcount)
4849382Seric 		e->e_hopcount = hopcnt;
4859382Seric 
4867783Seric 	/* message priority */
48767546Seric 	p = hvalue("precedence", e->e_header);
4889382Seric 	if (p != NULL)
4899382Seric 		e->e_class = priencode(p);
49069683Seric 	if (e->e_class < 0)
49169683Seric 		e->e_timeoutclass = TOC_NONURGENT;
49269683Seric 	else if (e->e_class > 0)
49369683Seric 		e->e_timeoutclass = TOC_URGENT;
49458929Seric 	if (full)
49567730Seric 	{
49625013Seric 		e->e_msgpriority = e->e_msgsize
49724981Seric 				 - e->e_class * WkClassFact
49824981Seric 				 + e->e_nrcpts * WkRecipFact;
49967730Seric 	}
5007783Seric 
50167730Seric 	/* message timeout priority */
50267730Seric 	p = hvalue("priority", e->e_header);
50369683Seric 	if (p != NULL)
50467730Seric 	{
50567730Seric 		/* (this should be in the configuration file) */
50667730Seric 		if (strcasecmp(p, "urgent"))
50767730Seric 			e->e_timeoutclass = TOC_URGENT;
50867730Seric 		else if (strcasecmp(p, "normal"))
50967730Seric 			e->e_timeoutclass = TOC_NORMAL;
51067730Seric 		else if (strcasecmp(p, "non-urgent"))
51167730Seric 			e->e_timeoutclass = TOC_NONURGENT;
51267730Seric 	}
51367730Seric 
5147783Seric 	/* date message originated */
51567546Seric 	p = hvalue("posted-date", e->e_header);
5167783Seric 	if (p == NULL)
51767546Seric 		p = hvalue("date", e->e_header);
5187783Seric 	if (p != NULL)
5199382Seric 		define('a', p, e);
52011290Seric 
52168884Seric 	/* check to see if this is a MIME message */
52269922Seric 	else if ((e->e_bodytype != NULL &&
52369922Seric 		  strcasecmp(e->e_bodytype, "8BITMIME") == 0) ||
52469922Seric 		 hvalue("MIME-Version", e->e_header) != NULL)
52569105Seric 	{
52668884Seric 		e->e_flags |= EF_IS_MIME;
52769105Seric 		if (HasEightBits)
52869105Seric 			e->e_bodytype = "8BITMIME";
52969105Seric 	}
53069922Seric 	else if ((p = hvalue("Content-Type", e->e_header)) != NULL)
53169922Seric 	{
53269922Seric 		/* this may be an RFC 1049 message */
533*69926Seric 		p = strpbrk(p, ";/");
53469922Seric 		if (p == NULL || *p == ';')
53569922Seric 		{
53669922Seric 			/* yep, it is */
53769922Seric 			e->e_flags |= EF_DONT_MIME;
53869922Seric 		}
53969922Seric 	}
54068884Seric 
54111290Seric 	/*
54265983Seric 	**  From person in antiquated ARPANET mode
54365983Seric 	**	required by UK Grey Book e-mail gateways (sigh)
54465983Seric 	*/
54565983Seric 
54665983Seric 	if (OpMode == MD_ARPAFTP)
54765983Seric 	{
54865983Seric 		register struct hdrinfo *hi;
54965983Seric 
55065983Seric 		for (hi = HdrInfo; hi->hi_field != NULL; hi++)
55165983Seric 		{
55265983Seric 			if (bitset(H_FROM, hi->hi_flags) &&
55365983Seric 			    (!bitset(H_RESENT, hi->hi_flags) ||
55465983Seric 			     bitset(EF_RESENT, e->e_flags)) &&
55567546Seric 			    (p = hvalue(hi->hi_field, e->e_header)) != NULL)
55665983Seric 				break;
55765983Seric 		}
55865983Seric 		if (hi->hi_field != NULL)
55965983Seric 		{
56065983Seric 			if (tTd(32, 2))
56165983Seric 				printf("eatheader: setsender(*%s == %s)\n",
56265983Seric 					hi->hi_field, p);
56365983Seric 			setsender(p, e, NULL, TRUE);
56465983Seric 		}
56565983Seric 	}
56665983Seric 
56765983Seric 	/*
56811290Seric 	**  Log collection information.
56911290Seric 	*/
57011290Seric 
57111290Seric # ifdef LOG
57268036Seric 	if (bitset(EF_LOGSENDER, e->e_flags) && LogLevel > 4)
57365089Seric 		logsender(e, msgid);
57465089Seric # endif /* LOG */
57565089Seric 	e->e_flags &= ~EF_LOGSENDER;
57665089Seric }
57765089Seric /*
57865089Seric **  LOGSENDER -- log sender information
57965089Seric **
58065089Seric **	Parameters:
58165089Seric **		e -- the envelope to log
58265089Seric **		msgid -- the message id
58365089Seric **
58465089Seric **	Returns:
58565089Seric **		none
58665089Seric */
58765089Seric 
58869748Seric void
logsender(e,msgid)58965089Seric logsender(e, msgid)
59065089Seric 	register ENVELOPE *e;
59165089Seric 	char *msgid;
59265089Seric {
59366748Seric # ifdef LOG
59465089Seric 	char *name;
59565089Seric 	register char *sbp;
59665089Seric 	register char *p;
59767901Seric 	int l;
59868528Seric 	char hbuf[MAXNAME + 1];
59968528Seric 	char sbuf[MAXLINE + 1];
60068528Seric 	char mbuf[MAXNAME + 1];
60165089Seric 
60267901Seric 	/* don't allow newlines in the message-id */
60367901Seric 	if (msgid != NULL)
60467901Seric 	{
60567901Seric 		l = strlen(msgid);
60667901Seric 		if (l > sizeof mbuf - 1)
60767901Seric 			l = sizeof mbuf - 1;
60867901Seric 		bcopy(msgid, mbuf, l);
60967901Seric 		mbuf[l] = '\0';
61067901Seric 		p = mbuf;
61167901Seric 		while ((p = strchr(p, '\n')) != NULL)
61267901Seric 			*p++ = ' ';
61367901Seric 	}
61467901Seric 
61565089Seric 	if (bitset(EF_RESPONSE, e->e_flags))
61665089Seric 		name = "[RESPONSE]";
61765089Seric 	else if ((name = macvalue('_', e)) != NULL)
61865089Seric 		;
61966003Seric 	else if (RealHostName == NULL)
62066003Seric 		name = "localhost";
62165089Seric 	else if (RealHostName[0] == '[')
62265089Seric 		name = RealHostName;
62365089Seric 	else
62411290Seric 	{
62565089Seric 		name = hbuf;
62665089Seric 		(void) sprintf(hbuf, "%.80s", RealHostName);
62765089Seric 		if (RealHostAddr.sa.sa_family != 0)
62857359Seric 		{
62965089Seric 			p = &hbuf[strlen(hbuf)];
63065089Seric 			(void) sprintf(p, " (%s)",
63165089Seric 				anynet_ntoa(&RealHostAddr));
63257359Seric 		}
63365089Seric 	}
63457359Seric 
63565089Seric 	/* some versions of syslog only take 5 printf args */
63665059Seric #  if (SYSLOG_BUFSIZE) >= 256
63765089Seric 	sbp = sbuf;
63865089Seric 	sprintf(sbp, "from=%.200s, size=%ld, class=%d, pri=%ld, nrcpts=%d",
63967788Seric 	    e->e_from.q_paddr == NULL ? "<NONE>" : e->e_from.q_paddr,
64067788Seric 	    e->e_msgsize, e->e_class, e->e_msgpriority, e->e_nrcpts);
64165089Seric 	sbp += strlen(sbp);
64265089Seric 	if (msgid != NULL)
64365089Seric 	{
64467901Seric 		sprintf(sbp, ", msgid=%.100s", mbuf);
64560575Seric 		sbp += strlen(sbp);
64665089Seric 	}
64765089Seric 	if (e->e_bodytype != NULL)
64865089Seric 	{
64965089Seric 		(void) sprintf(sbp, ", bodytype=%.20s", e->e_bodytype);
65065089Seric 		sbp += strlen(sbp);
65165089Seric 	}
65265089Seric 	p = macvalue('r', e);
65365089Seric 	if (p != NULL)
65465089Seric 		(void) sprintf(sbp, ", proto=%.20s", p);
65565089Seric 	syslog(LOG_INFO, "%s: %s, relay=%s",
65665089Seric 	    e->e_id, sbuf, name);
65765059Seric 
65865059Seric #  else			/* short syslog buffer */
65965059Seric 
66065089Seric 	syslog(LOG_INFO, "%s: from=%s",
66167788Seric 		e->e_id, e->e_from.q_paddr == NULL ? "<NONE>" :
66267788Seric 				shortenstring(e->e_from.q_paddr, 83));
66365089Seric 	syslog(LOG_INFO, "%s: size=%ld, class=%ld, pri=%ld, nrcpts=%d",
66465089Seric 		e->e_id, e->e_msgsize, e->e_class,
66565089Seric 		e->e_msgpriority, e->e_nrcpts);
66665089Seric 	if (msgid != NULL)
66767901Seric 		syslog(LOG_INFO, "%s: msgid=%s", e->e_id, mbuf);
66865650Seric 	sbp = sbuf;
66965650Seric 	sprintf(sbp, "%s:", e->e_id);
67065650Seric 	sbp += strlen(sbp);
67165089Seric 	if (e->e_bodytype != NULL)
67265650Seric 	{
67365650Seric 		sprintf(sbp, " bodytype=%s,", e->e_bodytype);
67465650Seric 		sbp += strlen(sbp);
67565650Seric 	}
67665089Seric 	p = macvalue('r', e);
67765089Seric 	if (p != NULL)
67865650Seric 	{
67965731Seric 		sprintf(sbp, " proto=%s,", p);
68065650Seric 		sbp += strlen(sbp);
68165650Seric 	}
68265650Seric 	syslog(LOG_INFO, "%s relay=%s", sbuf, name);
68365059Seric #  endif
68466748Seric # endif
6857783Seric }
6867783Seric /*
6877783Seric **  PRIENCODE -- encode external priority names into internal values.
6887783Seric **
6897783Seric **	Parameters:
6907783Seric **		p -- priority in ascii.
6917783Seric **
6927783Seric **	Returns:
6937783Seric **		priority as a numeric level.
6947783Seric **
6957783Seric **	Side Effects:
6967783Seric **		none.
6977783Seric */
6987783Seric 
69969748Seric int
priencode(p)7007783Seric priencode(p)
7017783Seric 	char *p;
7027783Seric {
7038253Seric 	register int i;
7047783Seric 
7058253Seric 	for (i = 0; i < NumPriorities; i++)
7067783Seric 	{
70733725Sbostic 		if (!strcasecmp(p, Priorities[i].pri_name))
7088253Seric 			return (Priorities[i].pri_val);
7097783Seric 	}
7108253Seric 
7118253Seric 	/* unknown priority */
7128253Seric 	return (0);
7137783Seric }
7147783Seric /*
7157890Seric **  CRACKADDR -- parse an address and turn it into a macro
7167783Seric **
7177783Seric **	This doesn't actually parse the address -- it just extracts
7187783Seric **	it and replaces it with "$g".  The parse is totally ad hoc
7197783Seric **	and isn't even guaranteed to leave something syntactically
7207783Seric **	identical to what it started with.  However, it does leave
7217783Seric **	something semantically identical.
7227783Seric **
72351379Seric **	This algorithm has been cleaned up to handle a wider range
72451379Seric **	of cases -- notably quoted and backslash escaped strings.
72551379Seric **	This modification makes it substantially better at preserving
72651379Seric **	the original syntax.
7277783Seric **
7287783Seric **	Parameters:
7297890Seric **		addr -- the address to be cracked.
7307783Seric **
7317783Seric **	Returns:
7327783Seric **		a pointer to the new version.
7337783Seric **
7347783Seric **	Side Effects:
7357890Seric **		none.
7367783Seric **
7377783Seric **	Warning:
7387783Seric **		The return value is saved in local storage and should
7397783Seric **		be copied if it is to be reused.
7407783Seric */
7417783Seric 
7427783Seric char *
crackaddr(addr)7437890Seric crackaddr(addr)
7447890Seric 	register char *addr;
7457783Seric {
7467783Seric 	register char *p;
74751379Seric 	register char c;
74851379Seric 	int cmtlev;
74956764Seric 	int realcmtlev;
75056764Seric 	int anglelev, realanglelev;
75151379Seric 	int copylev;
75251379Seric 	bool qmode;
75356764Seric 	bool realqmode;
75456764Seric 	bool skipping;
75551379Seric 	bool putgmac = FALSE;
75656735Seric 	bool quoteit = FALSE;
75764148Seric 	bool gotangle = FALSE;
75868756Seric 	bool gotcolon = FALSE;
75951379Seric 	register char *bp;
76056764Seric 	char *buflim;
76168756Seric 	char *bufhead;
76268756Seric 	char *addrhead;
76368528Seric 	static char buf[MAXNAME + 1];
7647783Seric 
7657783Seric 	if (tTd(33, 1))
7667890Seric 		printf("crackaddr(%s)\n", addr);
7677783Seric 
7688082Seric 	/* strip leading spaces */
76958050Seric 	while (*addr != '\0' && isascii(*addr) && isspace(*addr))
7708082Seric 		addr++;
7718082Seric 
7727783Seric 	/*
77351379Seric 	**  Start by assuming we have no angle brackets.  This will be
77451379Seric 	**  adjusted later if we find them.
7757783Seric 	*/
7767783Seric 
77768756Seric 	bp = bufhead = buf;
77856764Seric 	buflim = &buf[sizeof buf - 5];
77968756Seric 	p = addrhead = addr;
78056764Seric 	copylev = anglelev = realanglelev = cmtlev = realcmtlev = 0;
78156764Seric 	qmode = realqmode = FALSE;
78251379Seric 
78351379Seric 	while ((c = *p++) != '\0')
7847783Seric 	{
78556764Seric 		/*
78656764Seric 		**  If the buffer is overful, go into a special "skipping"
78756764Seric 		**  mode that tries to keep legal syntax but doesn't actually
78856764Seric 		**  output things.
78956764Seric 		*/
7907783Seric 
79156764Seric 		skipping = bp >= buflim;
79256735Seric 
79356764Seric 		if (copylev > 0 && !skipping)
79456764Seric 			*bp++ = c;
79556735Seric 
79651379Seric 		/* check for backslash escapes */
79751379Seric 		if (c == '\\')
7987783Seric 		{
79958890Seric 			/* arrange to quote the address */
80058890Seric 			if (cmtlev <= 0 && !qmode)
80158890Seric 				quoteit = TRUE;
80258890Seric 
80351379Seric 			if ((c = *p++) == '\0')
8047783Seric 			{
80551379Seric 				/* too far */
80651379Seric 				p--;
80751379Seric 				goto putg;
8087783Seric 			}
80956764Seric 			if (copylev > 0 && !skipping)
81051379Seric 				*bp++ = c;
81151379Seric 			goto putg;
8127783Seric 		}
8137783Seric 
81451379Seric 		/* check for quoted strings */
81564967Seric 		if (c == '"' && cmtlev <= 0)
8167783Seric 		{
81751379Seric 			qmode = !qmode;
81856764Seric 			if (copylev > 0 && !skipping)
81956764Seric 				realqmode = !realqmode;
82051379Seric 			continue;
8217783Seric 		}
82251379Seric 		if (qmode)
82351379Seric 			goto putg;
8247783Seric 
82551379Seric 		/* check for comments */
82651379Seric 		if (c == '(')
8277783Seric 		{
82851379Seric 			cmtlev++;
82956764Seric 
83056764Seric 			/* allow space for closing paren */
83156764Seric 			if (!skipping)
83256764Seric 			{
83356764Seric 				buflim--;
83456764Seric 				realcmtlev++;
83556764Seric 				if (copylev++ <= 0)
83656764Seric 				{
83756764Seric 					*bp++ = ' ';
83856764Seric 					*bp++ = c;
83956764Seric 				}
84056764Seric 			}
84151379Seric 		}
84251379Seric 		if (cmtlev > 0)
84351379Seric 		{
84451379Seric 			if (c == ')')
8457783Seric 			{
84651379Seric 				cmtlev--;
84751379Seric 				copylev--;
84856764Seric 				if (!skipping)
84956764Seric 				{
85056764Seric 					realcmtlev--;
85156764Seric 					buflim++;
85256764Seric 				}
8537783Seric 			}
8547783Seric 			continue;
8557783Seric 		}
85656764Seric 		else if (c == ')')
85756764Seric 		{
85856764Seric 			/* syntax error: unmatched ) */
85965544Seric 			if (copylev > 0 && !skipping)
86056764Seric 				bp--;
86156764Seric 		}
8627783Seric 
86368756Seric 		/* check for group: list; syntax */
86468760Seric 		if (c == ':' && anglelev <= 0 && !gotcolon && !ColonOkInAddr)
86568756Seric 		{
86668756Seric 			register char *q;
86768756Seric 
86868760Seric 			if (*p == ':')
86968760Seric 			{
87068760Seric 				/* special case -- :: syntax */
87168760Seric 				if (cmtlev <= 0 && !qmode)
87268760Seric 					quoteit = TRUE;
87368760Seric 				if (copylev > 0 && !skipping)
87468760Seric 				{
87568760Seric 					*bp++ = c;
87668760Seric 					*bp++ = c;
87768760Seric 				}
87868760Seric 				p++;
87968760Seric 				goto putg;
88068760Seric 			}
88168760Seric 
88268756Seric 			gotcolon = TRUE;
88368756Seric 
88468760Seric 			bp = bufhead;
88568760Seric 			if (quoteit)
88668760Seric 			{
88768760Seric 				*bp++ = '"';
88868760Seric 
88968760Seric 				/* back up over the ':' and any spaces */
89068760Seric 				--p;
89168760Seric 				while (isascii(*--p) && isspace(*p))
89268760Seric 					continue;
89368756Seric 				p++;
89468760Seric 			}
89568756Seric 			for (q = addrhead; q < p; )
89668756Seric 			{
89768756Seric 				c = *q++;
89868756Seric 				if (bp < buflim)
89968756Seric 				{
90068760Seric 					if (quoteit && c == '"')
90168760Seric 						*bp++ = '\\';
90268756Seric 					*bp++ = c;
90368756Seric 				}
90468756Seric 			}
90568760Seric 			if (quoteit)
90668760Seric 			{
90768760Seric 				if (bp == &bufhead[1])
90868760Seric 					bp--;
90968760Seric 				else
91068760Seric 					*bp++ = '"';
91168760Seric 				while ((c = *p++) != ':')
91268760Seric 				{
91368760Seric 					if (bp < buflim)
91468760Seric 						*bp++ = c;
91568760Seric 				}
91668760Seric 				*bp++ = c;
91768760Seric 			}
91868760Seric 
91968760Seric 			/* any trailing white space is part of group: */
92068760Seric 			while (isascii(*p) && isspace(*p) && bp < buflim)
92168760Seric 				*bp++ = *p++;
92268756Seric 			copylev = 0;
92368760Seric 			putgmac = quoteit = FALSE;
92468756Seric 			bufhead = bp;
92568756Seric 			addrhead = p;
92668756Seric 			continue;
92768756Seric 		}
92868756Seric 
92968756Seric 		if (c == ';' && copylev <= 0 && !ColonOkInAddr)
93068756Seric 		{
93168756Seric 			if (bp < buflim)
93268756Seric 				*bp++ = c;
93368756Seric 		}
93468756Seric 
93556764Seric 		/* check for characters that may have to be quoted */
93664148Seric 		if (strchr(".'@,;:\\()[]", c) != NULL)
93756764Seric 		{
93856764Seric 			/*
93956764Seric 			**  If these occur as the phrase part of a <>
94056764Seric 			**  construct, but are not inside of () or already
94156764Seric 			**  quoted, they will have to be quoted.  Note that
94256764Seric 			**  now (but don't actually do the quoting).
94356764Seric 			*/
94456764Seric 
94556764Seric 			if (cmtlev <= 0 && !qmode)
94656764Seric 				quoteit = TRUE;
94756764Seric 		}
94856764Seric 
94951379Seric 		/* check for angle brackets */
95051379Seric 		if (c == '<')
95151379Seric 		{
95256735Seric 			register char *q;
95356735Seric 
95464148Seric 			/* assume first of two angles is bogus */
95564148Seric 			if (gotangle)
95664148Seric 				quoteit = TRUE;
95764148Seric 			gotangle = TRUE;
95864148Seric 
95951379Seric 			/* oops -- have to change our mind */
96064752Seric 			anglelev = 1;
96156764Seric 			if (!skipping)
96264752Seric 				realanglelev = 1;
96356764Seric 
96468756Seric 			bp = bufhead;
96556735Seric 			if (quoteit)
96656735Seric 			{
96756735Seric 				*bp++ = '"';
96856735Seric 
96956735Seric 				/* back up over the '<' and any spaces */
97056735Seric 				--p;
97158050Seric 				while (isascii(*--p) && isspace(*p))
97256735Seric 					continue;
97356735Seric 				p++;
97456735Seric 			}
97568756Seric 			for (q = addrhead; q < p; )
97656735Seric 			{
97756735Seric 				c = *q++;
97856764Seric 				if (bp < buflim)
97956764Seric 				{
98056764Seric 					if (quoteit && c == '"')
98156764Seric 						*bp++ = '\\';
98256764Seric 					*bp++ = c;
98356764Seric 				}
98456735Seric 			}
98556735Seric 			if (quoteit)
98656735Seric 			{
98764148Seric 				if (bp == &buf[1])
98864148Seric 					bp--;
98964148Seric 				else
99064148Seric 					*bp++ = '"';
99156764Seric 				while ((c = *p++) != '<')
99256764Seric 				{
99356764Seric 					if (bp < buflim)
99456764Seric 						*bp++ = c;
99556764Seric 				}
99656764Seric 				*bp++ = c;
99756735Seric 			}
99851379Seric 			copylev = 0;
99956735Seric 			putgmac = quoteit = FALSE;
100051379Seric 			continue;
100151379Seric 		}
10027783Seric 
100351379Seric 		if (c == '>')
10047783Seric 		{
100556764Seric 			if (anglelev > 0)
100656764Seric 			{
100756764Seric 				anglelev--;
100856764Seric 				if (!skipping)
100956764Seric 				{
101056764Seric 					realanglelev--;
101156764Seric 					buflim++;
101256764Seric 				}
101356764Seric 			}
101456764Seric 			else if (!skipping)
101556764Seric 			{
101656764Seric 				/* syntax error: unmatched > */
101756764Seric 				if (copylev > 0)
101856764Seric 					bp--;
101964752Seric 				quoteit = TRUE;
102056764Seric 				continue;
102156764Seric 			}
102251379Seric 			if (copylev++ <= 0)
102351379Seric 				*bp++ = c;
102451379Seric 			continue;
10257783Seric 		}
102651379Seric 
102751379Seric 		/* must be a real address character */
102851379Seric 	putg:
102951379Seric 		if (copylev <= 0 && !putgmac)
103051379Seric 		{
103158050Seric 			*bp++ = MACROEXPAND;
103251379Seric 			*bp++ = 'g';
103351379Seric 			putgmac = TRUE;
103451379Seric 		}
10357783Seric 	}
10367783Seric 
103756764Seric 	/* repair any syntactic damage */
103856764Seric 	if (realqmode)
103956764Seric 		*bp++ = '"';
104056764Seric 	while (realcmtlev-- > 0)
104156764Seric 		*bp++ = ')';
104256764Seric 	while (realanglelev-- > 0)
104356764Seric 		*bp++ = '>';
104451379Seric 	*bp++ = '\0';
10457783Seric 
10467783Seric 	if (tTd(33, 1))
10477944Seric 		printf("crackaddr=>`%s'\n", buf);
10487783Seric 
10497783Seric 	return (buf);
10507783Seric }
10519382Seric /*
10529382Seric **  PUTHEADER -- put the header part of a message from the in-core copy
10539382Seric **
10549382Seric **	Parameters:
105565870Seric **		mci -- the connection information.
105667546Seric **		h -- the header to put.
10579382Seric **		e -- envelope to use.
10589382Seric **
10599382Seric **	Returns:
10609382Seric **		none.
10619382Seric **
10629382Seric **	Side Effects:
10639382Seric **		none.
10649382Seric */
10659382Seric 
106657589Seric /*
106757589Seric  * Macro for fast max (not available in e.g. DG/UX, 386/ix).
106857589Seric  */
106957589Seric #ifndef MAX
107057589Seric # define MAX(a,b) (((a)>(b))?(a):(b))
107157589Seric #endif
107257589Seric 
107369748Seric void
putheader(mci,h,e)107468228Seric putheader(mci, h, e)
107565870Seric 	register MCI *mci;
107667546Seric 	register HDR *h;
10779382Seric 	register ENVELOPE *e;
10789382Seric {
107957135Seric 	char buf[MAX(MAXLINE,BUFSIZ)];
108057135Seric 	char obuf[MAXLINE];
10819382Seric 
108259882Seric 	if (tTd(34, 1))
108365870Seric 		printf("--- putheader, mailer = %s ---\n",
108465870Seric 			mci->mci_mailer->m_name);
108559882Seric 
108667546Seric 	mci->mci_flags |= MCIF_INHEADER;
108767546Seric 	for (; h != NULL; h = h->h_link)
10889382Seric 	{
108969474Seric 		register char *p = h->h_value;
109010689Seric 		extern bool bitintersect();
10919382Seric 
109259882Seric 		if (tTd(34, 11))
109359882Seric 		{
109459882Seric 			printf("  %s: ", h->h_field);
109569474Seric 			xputs(p);
109659882Seric 		}
109759882Seric 
109869281Seric 		/* suppress Content-Transfer-Encoding: if we are MIMEing */
109969281Seric 		if (bitset(H_CTE, h->h_flags) &&
110069281Seric 		    bitset(MCIF_CVT8TO7|MCIF_INMIME, mci->mci_flags))
110169281Seric 		{
110269281Seric 			if (tTd(34, 11))
110369281Seric 				printf(" (skipped (content-transfer-encoding))\n");
110469281Seric 			continue;
110569281Seric 		}
110669281Seric 
110769281Seric 		if (bitset(MCIF_INMIME, mci->mci_flags))
110869281Seric 			goto vanilla;
110969281Seric 
11109382Seric 		if (bitset(H_CHECK|H_ACHECK, h->h_flags) &&
111165870Seric 		    !bitintersect(h->h_mflags, mci->mci_mailer->m_flags))
111259882Seric 		{
111359882Seric 			if (tTd(34, 11))
111459882Seric 				printf(" (skipped)\n");
11159382Seric 			continue;
111659882Seric 		}
11179382Seric 
111811414Seric 		/* handle Resent-... headers specially */
111911414Seric 		if (bitset(H_RESENT, h->h_flags) && !bitset(EF_RESENT, e->e_flags))
112059882Seric 		{
112159882Seric 			if (tTd(34, 11))
112259882Seric 				printf(" (skipped (resent))\n");
112311414Seric 			continue;
112459882Seric 		}
112511414Seric 
112666784Seric 		/* suppress return receipts if requested */
112766784Seric 		if (bitset(H_RECEIPTTO, h->h_flags) &&
112866784Seric 		    bitset(EF_NORECEIPT, e->e_flags))
112966784Seric 		{
113066784Seric 			if (tTd(34, 11))
113166784Seric 				printf(" (skipped (receipt))\n");
113266784Seric 			continue;
113366784Seric 		}
113466784Seric 
113565152Seric 		/* macro expand value if generated internally */
11369382Seric 		if (bitset(H_DEFAULT, h->h_flags))
11379382Seric 		{
113868529Seric 			expand(p, buf, sizeof buf, e);
11399382Seric 			p = buf;
11409382Seric 			if (p == NULL || *p == '\0')
114165152Seric 			{
114265152Seric 				if (tTd(34, 11))
114365152Seric 					printf(" (skipped -- null value)\n");
11449382Seric 				continue;
114565152Seric 			}
11469382Seric 		}
11479382Seric 
114865152Seric 		if (tTd(34, 11))
114965152Seric 			printf("\n");
115065152Seric 
115168449Seric 		if (bitset(H_STRIPVAL, h->h_flags))
11529382Seric 		{
115368449Seric 			/* empty field */
115468449Seric 			(void) sprintf(obuf, "%s:", h->h_field);
115568449Seric 			putline(obuf, mci);
115668449Seric 		}
115768449Seric 		else if (bitset(H_FROM|H_RCPT, h->h_flags))
115868449Seric 		{
11599382Seric 			/* address field */
11609382Seric 			bool oldstyle = bitset(EF_OLDSTYLE, e->e_flags);
11619382Seric 
11629382Seric 			if (bitset(H_FROM, h->h_flags))
11639382Seric 				oldstyle = FALSE;
116465870Seric 			commaize(h, p, oldstyle, mci, e);
11659382Seric 		}
11669382Seric 		else
11679382Seric 		{
11689382Seric 			/* vanilla header line */
116912159Seric 			register char *nlp;
117012159Seric 
117169281Seric vanilla:
117259579Seric 			(void) sprintf(obuf, "%s: ", h->h_field);
117356795Seric 			while ((nlp = strchr(p, '\n')) != NULL)
117412159Seric 			{
117512159Seric 				*nlp = '\0';
117612159Seric 				(void) strcat(obuf, p);
117712159Seric 				*nlp = '\n';
117865870Seric 				putline(obuf, mci);
117912159Seric 				p = ++nlp;
118012161Seric 				obuf[0] = '\0';
118112159Seric 			}
118212159Seric 			(void) strcat(obuf, p);
118365870Seric 			putline(obuf, mci);
11849382Seric 		}
11859382Seric 	}
118667887Seric 
118767887Seric 	/*
118867887Seric 	**  If we are converting this to a MIME message, add the
118967889Seric 	**  MIME headers.
119067887Seric 	*/
119167887Seric 
119269480Seric #if MIME8TO7
119367887Seric 	if (bitset(MM_MIME8BIT, MimeMode) &&
119467887Seric 	    bitset(EF_HAS8BIT, e->e_flags) &&
119569922Seric 	    !bitset(EF_DONT_MIME, e->e_flags) &&
119667887Seric 	    !bitnset(M_8BITS, mci->mci_mailer->m_flags) &&
119767889Seric 	    !bitset(MCIF_CVT8TO7, mci->mci_flags))
119867887Seric 	{
119967889Seric 		if (hvalue("MIME-Version", e->e_header) == NULL)
120067889Seric 			putline("MIME-Version: 1.0", mci);
120167889Seric 		if (hvalue("Content-Type", e->e_header) == NULL)
120267889Seric 		{
120367889Seric 			sprintf(obuf, "Content-Type: text/plain; charset=%s",
120467896Seric 				defcharset(e));
120567889Seric 			putline(obuf, mci);
120667889Seric 		}
120767889Seric 		if (hvalue("Content-Transfer-Encoding", e->e_header) == NULL)
120867889Seric 			putline("Content-Transfer-Encoding: 8bit", mci);
120967887Seric 	}
121069480Seric #endif
12119382Seric }
12129382Seric /*
12139382Seric **  COMMAIZE -- output a header field, making a comma-translated list.
12149382Seric **
12159382Seric **	Parameters:
12169382Seric **		h -- the header field to output.
12179382Seric **		p -- the value to put in it.
12189382Seric **		oldstyle -- TRUE if this is an old style header.
121965870Seric **		mci -- the connection information.
122055012Seric **		e -- the envelope containing the message.
12219382Seric **
12229382Seric **	Returns:
12239382Seric **		none.
12249382Seric **
12259382Seric **	Side Effects:
12269382Seric **		outputs "p" to file "fp".
12279382Seric */
12289382Seric 
122965870Seric void
commaize(h,p,oldstyle,mci,e)123065870Seric commaize(h, p, oldstyle, mci, e)
12319382Seric 	register HDR *h;
12329382Seric 	register char *p;
12339382Seric 	bool oldstyle;
123465870Seric 	register MCI *mci;
123555012Seric 	register ENVELOPE *e;
12369382Seric {
12379382Seric 	register char *obp;
12389382Seric 	int opos;
123966004Seric 	int omax;
12409382Seric 	bool firstone = TRUE;
124111157Seric 	char obuf[MAXLINE + 3];
12429382Seric 
12439382Seric 	/*
12449382Seric 	**  Output the address list translated by the
12459382Seric 	**  mailer and with commas.
12469382Seric 	*/
12479382Seric 
12489382Seric 	if (tTd(14, 2))
12499382Seric 		printf("commaize(%s: %s)\n", h->h_field, p);
12509382Seric 
12519382Seric 	obp = obuf;
125259579Seric 	(void) sprintf(obp, "%s: ", h->h_field);
12539382Seric 	opos = strlen(h->h_field) + 2;
12549382Seric 	obp += opos;
125566254Seric 	omax = mci->mci_mailer->m_linelimit - 2;
125666254Seric 	if (omax < 0 || omax > 78)
125766254Seric 		omax = 78;
12589382Seric 
12599382Seric 	/*
12609382Seric 	**  Run through the list of values.
12619382Seric 	*/
12629382Seric 
12639382Seric 	while (*p != '\0')
12649382Seric 	{
12659382Seric 		register char *name;
126654983Seric 		register int c;
12679382Seric 		char savechar;
126859163Seric 		int flags;
126959163Seric 		auto int stat;
12709382Seric 
12719382Seric 		/*
12729382Seric 		**  Find the end of the name.  New style names
12739382Seric 		**  end with a comma, old style names end with
12749382Seric 		**  a space character.  However, spaces do not
12759382Seric 		**  necessarily delimit an old-style name -- at
12769382Seric 		**  signs mean keep going.
12779382Seric 		*/
12789382Seric 
12799382Seric 		/* find end of name */
128058050Seric 		while ((isascii(*p) && isspace(*p)) || *p == ',')
12819382Seric 			p++;
12829382Seric 		name = p;
12839382Seric 		for (;;)
12849382Seric 		{
128558333Seric 			auto char *oldp;
128616909Seric 			char pvpbuf[PSBUFSIZE];
12879382Seric 
128865066Seric 			(void) prescan(p, oldstyle ? ' ' : ',', pvpbuf,
128968711Seric 				       sizeof pvpbuf, &oldp, NULL);
129058333Seric 			p = oldp;
12919382Seric 
12929382Seric 			/* look to see if we have an at sign */
129358050Seric 			while (*p != '\0' && isascii(*p) && isspace(*p))
12949382Seric 				p++;
12959382Seric 
129658170Seric 			if (*p != '@')
12979382Seric 			{
12989382Seric 				p = oldp;
12999382Seric 				break;
13009382Seric 			}
13019382Seric 			p += *p == '@' ? 1 : 2;
130258050Seric 			while (*p != '\0' && isascii(*p) && isspace(*p))
13039382Seric 				p++;
13049382Seric 		}
13059382Seric 		/* at the end of one complete name */
13069382Seric 
13079382Seric 		/* strip off trailing white space */
130858050Seric 		while (p >= name &&
130958050Seric 		       ((isascii(*p) && isspace(*p)) || *p == ',' || *p == '\0'))
13109382Seric 			p--;
13119382Seric 		if (++p == name)
13129382Seric 			continue;
13139382Seric 		savechar = *p;
13149382Seric 		*p = '\0';
13159382Seric 
13169382Seric 		/* translate the name to be relative */
131759163Seric 		flags = RF_HEADERADDR|RF_ADDDOMAIN;
131859163Seric 		if (bitset(H_FROM, h->h_flags))
131959163Seric 			flags |= RF_SENDERADDR;
132069713Seric #if USERDB
132168522Seric 		else if (e->e_from.q_mailer != NULL &&
132268522Seric 			 bitnset(M_UDBRECIPIENT, e->e_from.q_mailer->m_flags))
132368522Seric 		{
132468522Seric 			extern char *udbsender();
132568522Seric 
132668522Seric 			name = udbsender(name);
132768522Seric 		}
132868522Seric #endif
132959163Seric 		stat = EX_OK;
133065870Seric 		name = remotename(name, mci->mci_mailer, flags, &stat, e);
13319382Seric 		if (*name == '\0')
13329382Seric 		{
13339382Seric 			*p = savechar;
13349382Seric 			continue;
13359382Seric 		}
13369382Seric 
13379382Seric 		/* output the name with nice formatting */
133854983Seric 		opos += strlen(name);
13399382Seric 		if (!firstone)
13409382Seric 			opos += 2;
134166004Seric 		if (opos > omax && !firstone)
13429382Seric 		{
134310178Seric 			(void) strcpy(obp, ",\n");
134465870Seric 			putline(obuf, mci);
13459382Seric 			obp = obuf;
134666255Seric 			(void) strcpy(obp, "        ");
134710161Seric 			opos = strlen(obp);
134810161Seric 			obp += opos;
134954983Seric 			opos += strlen(name);
13509382Seric 		}
13519382Seric 		else if (!firstone)
13529382Seric 		{
135366255Seric 			(void) strcpy(obp, ", ");
13549382Seric 			obp += 2;
13559382Seric 		}
13569382Seric 
135754983Seric 		while ((c = *name++) != '\0' && obp < &obuf[MAXLINE])
135854983Seric 			*obp++ = c;
13599382Seric 		firstone = FALSE;
13609382Seric 		*p = savechar;
13619382Seric 	}
13629382Seric 	(void) strcpy(obp, "\n");
136365870Seric 	putline(obuf, mci);
13649382Seric }
13659382Seric /*
136658170Seric **  COPYHEADER -- copy header list
13679382Seric **
136858170Seric **	This routine is the equivalent of newstr for header lists
136958170Seric **
13709382Seric **	Parameters:
137158170Seric **		header -- list of header structures to copy.
13729382Seric **
13739382Seric **	Returns:
137458170Seric **		a copy of 'header'.
13759382Seric **
13769382Seric **	Side Effects:
13779382Seric **		none.
13789382Seric */
13799382Seric 
138058170Seric HDR *
copyheader(header)138158170Seric copyheader(header)
138258170Seric 	register HDR *header;
13839382Seric {
138458170Seric 	register HDR *newhdr;
138558170Seric 	HDR *ret;
138658170Seric 	register HDR **tail = &ret;
13879382Seric 
138858170Seric 	while (header != NULL)
138958170Seric 	{
139058170Seric 		newhdr = (HDR *) xalloc(sizeof(HDR));
139158170Seric 		STRUCTCOPY(*header, *newhdr);
139258170Seric 		*tail = newhdr;
139358170Seric 		tail = &newhdr->h_link;
139458170Seric 		header = header->h_link;
139558170Seric 	}
139658170Seric 	*tail = NULL;
139758170Seric 
139858170Seric 	return ret;
13999382Seric }
1400