122706Sdist /*
234921Sbostic  * Copyright (c) 1983 Eric P. Allman
333729Sbostic  * Copyright (c) 1988 Regents of the University of California.
433729Sbostic  * All rights reserved.
533729Sbostic  *
642826Sbostic  * %sccs.include.redist.c%
733729Sbostic  */
822706Sdist 
922706Sdist #ifndef lint
10*59859Seric static char sccsid[] = "@(#)headers.c	6.33 (Berkeley) 05/10/93";
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.
2455012Seric **		e -- the envelope including this header.
254091Seric **
264091Seric **	Returns:
274091Seric **		flags for this header.
284091Seric **
294091Seric **	Side Effects:
304091Seric **		The header is saved on the header list.
314319Seric **		Contents of 'line' are destroyed.
324091Seric */
334091Seric 
3455012Seric chompheader(line, def, e)
354091Seric 	char *line;
364091Seric 	bool def;
3755012Seric 	register ENVELOPE *e;
384091Seric {
394091Seric 	register char *p;
404091Seric 	register HDR *h;
414091Seric 	HDR **hp;
424091Seric 	char *fname;
434091Seric 	char *fvalue;
444091Seric 	struct hdrinfo *hi;
459059Seric 	bool cond = FALSE;
4610689Seric 	BITMAP mopts;
474091Seric 
487677Seric 	if (tTd(31, 6))
497677Seric 		printf("chompheader: %s\n", line);
507677Seric 
514627Seric 	/* strip off options */
5210689Seric 	clrbitmap(mopts);
534627Seric 	p = line;
5457405Seric 	if (*p == '?')
554627Seric 	{
564627Seric 		/* have some */
5756795Seric 		register char *q = strchr(p + 1, *p);
584627Seric 
594627Seric 		if (q != NULL)
604627Seric 		{
614627Seric 			*q++ = '\0';
6210689Seric 			while (*++p != '\0')
6310689Seric 				setbitn(*p, mopts);
644627Seric 			p = q;
654627Seric 		}
664627Seric 		else
6758151Seric 			usrerr("553 header syntax error, line \"%s\"", line);
689059Seric 		cond = TRUE;
694627Seric 	}
704627Seric 
714091Seric 	/* find canonical name */
724627Seric 	fname = p;
7356795Seric 	p = strchr(p, ':');
7410118Seric 	if (p == NULL)
7510118Seric 	{
7658151Seric 		syserr("553 header syntax error, line \"%s\"", line);
7710118Seric 		return (0);
7810118Seric 	}
794091Seric 	fvalue = &p[1];
8058050Seric 	while (isascii(*--p) && isspace(*p))
814091Seric 		continue;
824091Seric 	*++p = '\0';
834091Seric 
844091Seric 	/* strip field value on front */
854091Seric 	if (*fvalue == ' ')
864091Seric 		fvalue++;
874091Seric 
884091Seric 	/* see if it is a known type */
894091Seric 	for (hi = HdrInfo; hi->hi_field != NULL; hi++)
904091Seric 	{
9159579Seric 		if (strcasecmp(hi->hi_field, fname) == 0)
924091Seric 			break;
934091Seric 	}
944091Seric 
9511414Seric 	/* see if this is a resent message */
9611930Seric 	if (!def && bitset(H_RESENT, hi->hi_flags))
9755012Seric 		e->e_flags |= EF_RESENT;
9811414Seric 
994091Seric 	/* if this means "end of header" quit now */
1004091Seric 	if (bitset(H_EOH, hi->hi_flags))
1014091Seric 		return (hi->hi_flags);
1024091Seric 
10311414Seric 	/* drop explicit From: if same as what we would generate -- for MH */
10414784Seric 	p = "resent-from";
10555012Seric 	if (!bitset(EF_RESENT, e->e_flags))
10614784Seric 		p += 7;
10759579Seric 	if (!def && !bitset(EF_QUEUERUN, e->e_flags) && strcasecmp(fname, p) == 0)
10811414Seric 	{
10955012Seric 		if (e->e_from.q_paddr != NULL &&
11055012Seric 		    strcmp(fvalue, e->e_from.q_paddr) == 0)
11111414Seric 			return (hi->hi_flags);
11211414Seric 	}
11311414Seric 
11414784Seric 	/* delete default value for this header */
11555012Seric 	for (hp = &e->e_header; (h = *hp) != NULL; hp = &h->h_link)
11614784Seric 	{
11759579Seric 		if (strcasecmp(fname, h->h_field) == 0 &&
11814784Seric 		    bitset(H_DEFAULT, h->h_flags) &&
11914784Seric 		    !bitset(H_FORCE, h->h_flags))
12014784Seric 			h->h_value = NULL;
12114784Seric 	}
12214784Seric 
12313012Seric 	/* create a new node */
12413012Seric 	h = (HDR *) xalloc(sizeof *h);
12513012Seric 	h->h_field = newstr(fname);
12613012Seric 	h->h_value = NULL;
12713012Seric 	h->h_link = NULL;
12823117Seric 	bcopy((char *) mopts, (char *) h->h_mflags, sizeof mopts);
12913012Seric 	*hp = h;
1308066Seric 	h->h_flags = hi->hi_flags;
1314091Seric 	if (def)
1324091Seric 		h->h_flags |= H_DEFAULT;
1339059Seric 	if (cond)
1349059Seric 		h->h_flags |= H_CHECK;
1354091Seric 	if (h->h_value != NULL)
1369351Seric 		free((char *) h->h_value);
1378066Seric 	h->h_value = newstr(fvalue);
1384091Seric 
1395937Seric 	/* hack to see if this is a new format message */
1408095Seric 	if (!def && bitset(H_RCPT|H_FROM, h->h_flags) &&
14156795Seric 	    (strchr(fvalue, ',') != NULL || strchr(fvalue, '(') != NULL ||
14256795Seric 	     strchr(fvalue, '<') != NULL || strchr(fvalue, ';') != NULL))
1438089Seric 	{
14455012Seric 		e->e_flags &= ~EF_OLDSTYLE;
1458089Seric 	}
1465937Seric 
1474091Seric 	return (h->h_flags);
1484091Seric }
1494091Seric /*
1506980Seric **  ADDHEADER -- add a header entry to the end of the queue.
1516980Seric **
1526980Seric **	This bypasses the special checking of chompheader.
1536980Seric **
1546980Seric **	Parameters:
15559579Seric **		field -- the name of the header field.
15658789Seric **		value -- the value of the field.
1576980Seric **		e -- the envelope to add them to.
1586980Seric **
1596980Seric **	Returns:
1606980Seric **		none.
1616980Seric **
1626980Seric **	Side Effects:
1636980Seric **		adds the field on the list of headers for this envelope.
1646980Seric */
1656980Seric 
1666980Seric addheader(field, value, e)
1676980Seric 	char *field;
1686980Seric 	char *value;
1696980Seric 	ENVELOPE *e;
1706980Seric {
1716980Seric 	register HDR *h;
1726980Seric 	register struct hdrinfo *hi;
1736980Seric 	HDR **hp;
1746980Seric 
1756980Seric 	/* find info struct */
1766980Seric 	for (hi = HdrInfo; hi->hi_field != NULL; hi++)
1776980Seric 	{
17859579Seric 		if (strcasecmp(field, hi->hi_field) == 0)
1796980Seric 			break;
1806980Seric 	}
1816980Seric 
1826980Seric 	/* find current place in list -- keep back pointer? */
1836980Seric 	for (hp = &e->e_header; (h = *hp) != NULL; hp = &h->h_link)
1846980Seric 	{
18559579Seric 		if (strcasecmp(field, h->h_field) == 0)
1866980Seric 			break;
1876980Seric 	}
1886980Seric 
1896980Seric 	/* allocate space for new header */
1906980Seric 	h = (HDR *) xalloc(sizeof *h);
1916980Seric 	h->h_field = field;
1926980Seric 	h->h_value = newstr(value);
1937368Seric 	h->h_link = *hp;
1946980Seric 	h->h_flags = hi->hi_flags | H_DEFAULT;
19510689Seric 	clrbitmap(h->h_mflags);
1966980Seric 	*hp = h;
1976980Seric }
1986980Seric /*
1994091Seric **  HVALUE -- return value of a header.
2004091Seric **
2014091Seric **	Only "real" fields (i.e., ones that have not been supplied
2024091Seric **	as a default) are used.
2034091Seric **
2044091Seric **	Parameters:
2054091Seric **		field -- the field name.
20655012Seric **		e -- the envelope containing the header.
2074091Seric **
2084091Seric **	Returns:
2094091Seric **		pointer to the value part.
2104091Seric **		NULL if not found.
2114091Seric **
2124091Seric **	Side Effects:
2139382Seric **		none.
2144091Seric */
2154091Seric 
2164091Seric char *
21755012Seric hvalue(field, e)
2184091Seric 	char *field;
21955012Seric 	register ENVELOPE *e;
2204091Seric {
2214091Seric 	register HDR *h;
2224091Seric 
22355012Seric 	for (h = e->e_header; h != NULL; h = h->h_link)
2244091Seric 	{
22559579Seric 		if (!bitset(H_DEFAULT, h->h_flags) &&
22659579Seric 		    strcasecmp(h->h_field, field) == 0)
2274091Seric 			return (h->h_value);
2284091Seric 	}
2294091Seric 	return (NULL);
2304091Seric }
2314091Seric /*
2324091Seric **  ISHEADER -- predicate telling if argument is a header.
2334091Seric **
2344319Seric **	A line is a header if it has a single word followed by
2354319Seric **	optional white space followed by a colon.
2364319Seric **
2374091Seric **	Parameters:
2384091Seric **		s -- string to check for possible headerness.
2394091Seric **
2404091Seric **	Returns:
2414091Seric **		TRUE if s is a header.
2424091Seric **		FALSE otherwise.
2434091Seric **
2444091Seric **	Side Effects:
2454091Seric **		none.
2464091Seric */
2474091Seric 
2484091Seric bool
2494091Seric isheader(s)
2504091Seric 	register char *s;
2514091Seric {
2529382Seric 	while (*s > ' ' && *s != ':' && *s != '\0')
2534091Seric 		s++;
2549382Seric 
2559382Seric 	/* following technically violates RFC822 */
25658050Seric 	while (isascii(*s) && isspace(*s))
2574091Seric 		s++;
2589382Seric 
2594091Seric 	return (*s == ':');
2604091Seric }
2615919Seric /*
2627783Seric **  EATHEADER -- run through the stored header and extract info.
2637783Seric **
2647783Seric **	Parameters:
2659382Seric **		e -- the envelope to process.
26658929Seric **		full -- if set, do full processing (e.g., compute
26758929Seric **			message priority).
2687783Seric **
2697783Seric **	Returns:
2707783Seric **		none.
2717783Seric **
2727783Seric **	Side Effects:
2737783Seric **		Sets a bunch of global variables from information
2749382Seric **			in the collected header.
2759382Seric **		Aborts the message if the hop count is exceeded.
2767783Seric */
2777783Seric 
27858929Seric eatheader(e, full)
2799382Seric 	register ENVELOPE *e;
28058929Seric 	bool full;
2817783Seric {
2827783Seric 	register HDR *h;
2837783Seric 	register char *p;
2849382Seric 	int hopcnt = 0;
28557208Seric 	char *msgid;
28658688Seric 	char buf[MAXLINE];
2877783Seric 
28858688Seric 	/*
28958688Seric 	**  Set up macros for possible expansion in headers.
29058688Seric 	*/
29158688Seric 
29258704Seric 	define('f', e->e_sender, e);
29358704Seric 	define('g', e->e_sender, e);
29458688Seric 
2959382Seric 	if (tTd(32, 1))
2969382Seric 		printf("----- collected header -----\n");
29757208Seric 	msgid = "<none>";
2989382Seric 	for (h = e->e_header; h != NULL; h = h->h_link)
2997783Seric 	{
30058688Seric 		/* do early binding */
30158688Seric 		if (bitset(H_DEFAULT, h->h_flags) && h->h_value != NULL)
30258688Seric 		{
30358688Seric 			expand(h->h_value, buf, &buf[sizeof buf], e);
30458688Seric 			if (buf[0] != '\0')
30558688Seric 			{
30658688Seric 				h->h_value = newstr(buf);
30758688Seric 				h->h_flags &= ~H_DEFAULT;
30858688Seric 			}
30958688Seric 		}
31058688Seric 
3119382Seric 		if (tTd(32, 1))
31259579Seric 			printf("%s: %s\n", h->h_field, h->h_value);
31357359Seric 
31411414Seric 		/* count the number of times it has been processed */
3159382Seric 		if (bitset(H_TRACE, h->h_flags))
3169382Seric 			hopcnt++;
31711414Seric 
31811414Seric 		/* send to this person if we so desire */
31911414Seric 		if (GrabTo && bitset(H_RCPT, h->h_flags) &&
32011414Seric 		    !bitset(H_DEFAULT, h->h_flags) &&
32155012Seric 		    (!bitset(EF_RESENT, e->e_flags) || bitset(H_RESENT, h->h_flags)))
32211414Seric 		{
32358082Seric 			(void) sendtolist(h->h_value, (ADDRESS *) NULL,
32458082Seric 					  &e->e_sendqueue, e);
32511414Seric 		}
32611414Seric 
32757208Seric 		/* save the message-id for logging */
32858929Seric 		if (full && h->h_value != NULL &&
32959579Seric 		    strcasecmp(h->h_field, "message-id") == 0)
33011290Seric 		{
33157208Seric 			msgid = h->h_value;
332*59859Seric 			while (isascii(*msgid) && isspace(*msgid))
333*59859Seric 				msgid++;
33411290Seric 		}
33557359Seric 
33657359Seric 		/* see if this is a return-receipt header */
33757359Seric 		if (bitset(H_RECEIPTTO, h->h_flags))
33857359Seric 			e->e_receiptto = h->h_value;
33957359Seric 
34057359Seric 		/* see if this is an errors-to header */
34157359Seric 		if (bitset(H_ERRORSTO, h->h_flags))
34258082Seric 			(void) sendtolist(h->h_value, (ADDRESS *) NULL,
34358082Seric 					  &e->e_errorqueue, e);
3449382Seric 	}
3459382Seric 	if (tTd(32, 1))
3467783Seric 		printf("----------------------------\n");
3477783Seric 
34858145Seric 	/* if we are just verifying (that is, sendmail -t -bv), drop out now */
34958145Seric 	if (OpMode == MD_VERIFY)
35058145Seric 		return;
35158145Seric 
3529382Seric 	/* store hop count */
3539382Seric 	if (hopcnt > e->e_hopcount)
3549382Seric 		e->e_hopcount = hopcnt;
3559382Seric 
3567783Seric 	/* message priority */
35755012Seric 	p = hvalue("precedence", e);
3589382Seric 	if (p != NULL)
3599382Seric 		e->e_class = priencode(p);
36058929Seric 	if (full)
36125013Seric 		e->e_msgpriority = e->e_msgsize
36224981Seric 				 - e->e_class * WkClassFact
36324981Seric 				 + e->e_nrcpts * WkRecipFact;
3647783Seric 
3657783Seric 	/* full name of from person */
36655012Seric 	p = hvalue("full-name", e);
3677783Seric 	if (p != NULL)
3689382Seric 		define('x', p, e);
3697783Seric 
3707783Seric 	/* date message originated */
37155012Seric 	p = hvalue("posted-date", e);
3727783Seric 	if (p == NULL)
37355012Seric 		p = hvalue("date", e);
3747783Seric 	if (p != NULL)
3759382Seric 		define('a', p, e);
37611290Seric 
37711290Seric 	/*
37811290Seric 	**  Log collection information.
37911290Seric 	*/
38011290Seric 
38111290Seric # ifdef LOG
38258929Seric 	if (full && LogLevel > 4)
38311290Seric 	{
38457359Seric 		char *name;
38557232Seric 		char hbuf[MAXNAME];
38657359Seric 		char sbuf[MAXLINE];
38758951Seric 		extern char *macvalue();
38836230Skarels 
38958667Seric 		if (bitset(EF_RESPONSE, e->e_flags))
39058667Seric 			name = "[RESPONSE]";
39158951Seric 		else if ((name = macvalue('_', e)) != NULL)
39258951Seric 			;
39358667Seric 		else if (RealHostName[0] == '[')
39436230Skarels 			name = RealHostName;
39536230Skarels 		else
39657359Seric 		{
39758755Seric 			extern char *anynet_ntoa();
39857359Seric 
39957359Seric 			name = hbuf;
40058798Seric 			(void) sprintf(hbuf, "%.80s", RealHostName);
40158798Seric 			if (RealHostAddr.sa.sa_family != 0)
40258798Seric 			{
40358798Seric 				p = &hbuf[strlen(hbuf)];
40458798Seric 				(void) sprintf(p, " (%s)",
40558798Seric 					anynet_ntoa(&RealHostAddr));
40658798Seric 			}
40757359Seric 		}
40857359Seric 
40957359Seric 		/* some versions of syslog only take 5 printf args */
41057589Seric 		sprintf(sbuf, "from=%.200s, size=%ld, class=%d, pri=%ld, nrcpts=%d, msgid=%.100s",
41158558Seric 		    e->e_from.q_paddr, e->e_msgsize, e->e_class,
41257589Seric 		    e->e_msgpriority, e->e_nrcpts, msgid);
41358686Seric 		syslog(LOG_INFO, "%s: %s, relay=%s",
41457359Seric 		    e->e_id, sbuf, name);
41511290Seric 	}
41656795Seric # endif /* LOG */
4177783Seric }
4187783Seric /*
4197783Seric **  PRIENCODE -- encode external priority names into internal values.
4207783Seric **
4217783Seric **	Parameters:
4227783Seric **		p -- priority in ascii.
4237783Seric **
4247783Seric **	Returns:
4257783Seric **		priority as a numeric level.
4267783Seric **
4277783Seric **	Side Effects:
4287783Seric **		none.
4297783Seric */
4307783Seric 
4317783Seric priencode(p)
4327783Seric 	char *p;
4337783Seric {
4348253Seric 	register int i;
4357783Seric 
4368253Seric 	for (i = 0; i < NumPriorities; i++)
4377783Seric 	{
43833725Sbostic 		if (!strcasecmp(p, Priorities[i].pri_name))
4398253Seric 			return (Priorities[i].pri_val);
4407783Seric 	}
4418253Seric 
4428253Seric 	/* unknown priority */
4438253Seric 	return (0);
4447783Seric }
4457783Seric /*
4467890Seric **  CRACKADDR -- parse an address and turn it into a macro
4477783Seric **
4487783Seric **	This doesn't actually parse the address -- it just extracts
4497783Seric **	it and replaces it with "$g".  The parse is totally ad hoc
4507783Seric **	and isn't even guaranteed to leave something syntactically
4517783Seric **	identical to what it started with.  However, it does leave
4527783Seric **	something semantically identical.
4537783Seric **
45451379Seric **	This algorithm has been cleaned up to handle a wider range
45551379Seric **	of cases -- notably quoted and backslash escaped strings.
45651379Seric **	This modification makes it substantially better at preserving
45751379Seric **	the original syntax.
4587783Seric **
4597783Seric **	Parameters:
4607890Seric **		addr -- the address to be cracked.
4617783Seric **
4627783Seric **	Returns:
4637783Seric **		a pointer to the new version.
4647783Seric **
4657783Seric **	Side Effects:
4667890Seric **		none.
4677783Seric **
4687783Seric **	Warning:
4697783Seric **		The return value is saved in local storage and should
4707783Seric **		be copied if it is to be reused.
4717783Seric */
4727783Seric 
4737783Seric char *
4747890Seric crackaddr(addr)
4757890Seric 	register char *addr;
4767783Seric {
4777783Seric 	register char *p;
47851379Seric 	register char c;
47951379Seric 	int cmtlev;
48056764Seric 	int realcmtlev;
48156764Seric 	int anglelev, realanglelev;
48251379Seric 	int copylev;
48351379Seric 	bool qmode;
48456764Seric 	bool realqmode;
48556764Seric 	bool skipping;
48651379Seric 	bool putgmac = FALSE;
48756735Seric 	bool quoteit = FALSE;
48851379Seric 	register char *bp;
48956764Seric 	char *buflim;
4907783Seric 	static char buf[MAXNAME];
4917783Seric 
4927783Seric 	if (tTd(33, 1))
4937890Seric 		printf("crackaddr(%s)\n", addr);
4947783Seric 
4958082Seric 	/* strip leading spaces */
49658050Seric 	while (*addr != '\0' && isascii(*addr) && isspace(*addr))
4978082Seric 		addr++;
4988082Seric 
4997783Seric 	/*
50051379Seric 	**  Start by assuming we have no angle brackets.  This will be
50151379Seric 	**  adjusted later if we find them.
5027783Seric 	*/
5037783Seric 
50451379Seric 	bp = buf;
50556764Seric 	buflim = &buf[sizeof buf - 5];
50651379Seric 	p = addr;
50756764Seric 	copylev = anglelev = realanglelev = cmtlev = realcmtlev = 0;
50856764Seric 	qmode = realqmode = FALSE;
50951379Seric 
51051379Seric 	while ((c = *p++) != '\0')
5117783Seric 	{
51256764Seric 		/*
51356764Seric 		**  If the buffer is overful, go into a special "skipping"
51456764Seric 		**  mode that tries to keep legal syntax but doesn't actually
51556764Seric 		**  output things.
51656764Seric 		*/
5177783Seric 
51856764Seric 		skipping = bp >= buflim;
51956735Seric 
52056764Seric 		if (copylev > 0 && !skipping)
52156764Seric 			*bp++ = c;
52256735Seric 
52351379Seric 		/* check for backslash escapes */
52451379Seric 		if (c == '\\')
5257783Seric 		{
52658890Seric 			/* arrange to quote the address */
52758890Seric 			if (cmtlev <= 0 && !qmode)
52858890Seric 				quoteit = TRUE;
52958890Seric 
53051379Seric 			if ((c = *p++) == '\0')
5317783Seric 			{
53251379Seric 				/* too far */
53351379Seric 				p--;
53451379Seric 				goto putg;
5357783Seric 			}
53656764Seric 			if (copylev > 0 && !skipping)
53751379Seric 				*bp++ = c;
53851379Seric 			goto putg;
5397783Seric 		}
5407783Seric 
54151379Seric 		/* check for quoted strings */
54251379Seric 		if (c == '"')
5437783Seric 		{
54451379Seric 			qmode = !qmode;
54556764Seric 			if (copylev > 0 && !skipping)
54656764Seric 				realqmode = !realqmode;
54751379Seric 			continue;
5487783Seric 		}
54951379Seric 		if (qmode)
55051379Seric 			goto putg;
5517783Seric 
55251379Seric 		/* check for comments */
55351379Seric 		if (c == '(')
5547783Seric 		{
55551379Seric 			cmtlev++;
55656764Seric 
55756764Seric 			/* allow space for closing paren */
55856764Seric 			if (!skipping)
55956764Seric 			{
56056764Seric 				buflim--;
56156764Seric 				realcmtlev++;
56256764Seric 				if (copylev++ <= 0)
56356764Seric 				{
56456764Seric 					*bp++ = ' ';
56556764Seric 					*bp++ = c;
56656764Seric 				}
56756764Seric 			}
56851379Seric 		}
56951379Seric 		if (cmtlev > 0)
57051379Seric 		{
57151379Seric 			if (c == ')')
5727783Seric 			{
57351379Seric 				cmtlev--;
57451379Seric 				copylev--;
57556764Seric 				if (!skipping)
57656764Seric 				{
57756764Seric 					realcmtlev--;
57856764Seric 					buflim++;
57956764Seric 				}
5807783Seric 			}
5817783Seric 			continue;
5827783Seric 		}
58356764Seric 		else if (c == ')')
58456764Seric 		{
58556764Seric 			/* syntax error: unmatched ) */
58656764Seric 			if (!skipping)
58756764Seric 				bp--;
58856764Seric 		}
5897783Seric 
59056764Seric 
59156764Seric 		/* check for characters that may have to be quoted */
59257175Seric 		if (strchr(".'@,;:\\()", c) != NULL)
59356764Seric 		{
59456764Seric 			/*
59556764Seric 			**  If these occur as the phrase part of a <>
59656764Seric 			**  construct, but are not inside of () or already
59756764Seric 			**  quoted, they will have to be quoted.  Note that
59856764Seric 			**  now (but don't actually do the quoting).
59956764Seric 			*/
60056764Seric 
60156764Seric 			if (cmtlev <= 0 && !qmode)
60256764Seric 				quoteit = TRUE;
60356764Seric 		}
60456764Seric 
60551379Seric 		/* check for angle brackets */
60651379Seric 		if (c == '<')
60751379Seric 		{
60856735Seric 			register char *q;
60956735Seric 
61051379Seric 			/* oops -- have to change our mind */
61156764Seric 			anglelev++;
61256764Seric 			if (!skipping)
61356764Seric 				realanglelev++;
61456764Seric 
61556735Seric 			bp = buf;
61656735Seric 			if (quoteit)
61756735Seric 			{
61856735Seric 				*bp++ = '"';
61956735Seric 
62056735Seric 				/* back up over the '<' and any spaces */
62156735Seric 				--p;
62258050Seric 				while (isascii(*--p) && isspace(*p))
62356735Seric 					continue;
62456735Seric 				p++;
62556735Seric 			}
62656735Seric 			for (q = addr; q < p; )
62756735Seric 			{
62856735Seric 				c = *q++;
62956764Seric 				if (bp < buflim)
63056764Seric 				{
63156764Seric 					if (quoteit && c == '"')
63256764Seric 						*bp++ = '\\';
63356764Seric 					*bp++ = c;
63456764Seric 				}
63556735Seric 			}
63656735Seric 			if (quoteit)
63756735Seric 			{
63856735Seric 				*bp++ = '"';
63956764Seric 				while ((c = *p++) != '<')
64056764Seric 				{
64156764Seric 					if (bp < buflim)
64256764Seric 						*bp++ = c;
64356764Seric 				}
64456764Seric 				*bp++ = c;
64556735Seric 			}
64651379Seric 			copylev = 0;
64756735Seric 			putgmac = quoteit = FALSE;
64851379Seric 			continue;
64951379Seric 		}
6507783Seric 
65151379Seric 		if (c == '>')
6527783Seric 		{
65356764Seric 			if (anglelev > 0)
65456764Seric 			{
65556764Seric 				anglelev--;
65656764Seric 				if (!skipping)
65756764Seric 				{
65856764Seric 					realanglelev--;
65956764Seric 					buflim++;
66056764Seric 				}
66156764Seric 			}
66256764Seric 			else if (!skipping)
66356764Seric 			{
66456764Seric 				/* syntax error: unmatched > */
66556764Seric 				if (copylev > 0)
66656764Seric 					bp--;
66756764Seric 				continue;
66856764Seric 			}
66951379Seric 			if (copylev++ <= 0)
67051379Seric 				*bp++ = c;
67151379Seric 			continue;
6727783Seric 		}
67351379Seric 
67451379Seric 		/* must be a real address character */
67551379Seric 	putg:
67651379Seric 		if (copylev <= 0 && !putgmac)
67751379Seric 		{
67858050Seric 			*bp++ = MACROEXPAND;
67951379Seric 			*bp++ = 'g';
68051379Seric 			putgmac = TRUE;
68151379Seric 		}
6827783Seric 	}
6837783Seric 
68456764Seric 	/* repair any syntactic damage */
68556764Seric 	if (realqmode)
68656764Seric 		*bp++ = '"';
68756764Seric 	while (realcmtlev-- > 0)
68856764Seric 		*bp++ = ')';
68956764Seric 	while (realanglelev-- > 0)
69056764Seric 		*bp++ = '>';
69151379Seric 	*bp++ = '\0';
6927783Seric 
6937783Seric 	if (tTd(33, 1))
6947944Seric 		printf("crackaddr=>`%s'\n", buf);
6957783Seric 
6967783Seric 	return (buf);
6977783Seric }
6989382Seric /*
6999382Seric **  PUTHEADER -- put the header part of a message from the in-core copy
7009382Seric **
7019382Seric **	Parameters:
7029382Seric **		fp -- file to put it on.
7039382Seric **		m -- mailer to use.
7049382Seric **		e -- envelope to use.
7059382Seric **
7069382Seric **	Returns:
7079382Seric **		none.
7089382Seric **
7099382Seric **	Side Effects:
7109382Seric **		none.
7119382Seric */
7129382Seric 
71357589Seric /*
71457589Seric  * Macro for fast max (not available in e.g. DG/UX, 386/ix).
71557589Seric  */
71657589Seric #ifndef MAX
71757589Seric # define MAX(a,b) (((a)>(b))?(a):(b))
71857589Seric #endif
71957589Seric 
72010176Seric putheader(fp, m, e)
7219382Seric 	register FILE *fp;
7229382Seric 	register MAILER *m;
7239382Seric 	register ENVELOPE *e;
7249382Seric {
72557135Seric 	char buf[MAX(MAXLINE,BUFSIZ)];
7269382Seric 	register HDR *h;
72757135Seric 	char obuf[MAXLINE];
7289382Seric 
7299382Seric 	for (h = e->e_header; h != NULL; h = h->h_link)
7309382Seric 	{
7319382Seric 		register char *p;
73210689Seric 		extern bool bitintersect();
7339382Seric 
7349382Seric 		if (bitset(H_CHECK|H_ACHECK, h->h_flags) &&
73510689Seric 		    !bitintersect(h->h_mflags, m->m_flags))
7369382Seric 			continue;
7379382Seric 
73811414Seric 		/* handle Resent-... headers specially */
73911414Seric 		if (bitset(H_RESENT, h->h_flags) && !bitset(EF_RESENT, e->e_flags))
74011414Seric 			continue;
74111414Seric 
7429382Seric 		p = h->h_value;
7439382Seric 		if (bitset(H_DEFAULT, h->h_flags))
7449382Seric 		{
7459382Seric 			/* macro expand value if generated internally */
7469382Seric 			expand(p, buf, &buf[sizeof buf], e);
7479382Seric 			p = buf;
7489382Seric 			if (p == NULL || *p == '\0')
7499382Seric 				continue;
7509382Seric 		}
7519382Seric 
7529382Seric 		if (bitset(H_FROM|H_RCPT, h->h_flags))
7539382Seric 		{
7549382Seric 			/* address field */
7559382Seric 			bool oldstyle = bitset(EF_OLDSTYLE, e->e_flags);
7569382Seric 
7579382Seric 			if (bitset(H_FROM, h->h_flags))
7589382Seric 				oldstyle = FALSE;
75955012Seric 			commaize(h, p, fp, oldstyle, m, e);
7609382Seric 		}
7619382Seric 		else
7629382Seric 		{
7639382Seric 			/* vanilla header line */
76412159Seric 			register char *nlp;
76512159Seric 
76659579Seric 			(void) sprintf(obuf, "%s: ", h->h_field);
76756795Seric 			while ((nlp = strchr(p, '\n')) != NULL)
76812159Seric 			{
76912159Seric 				*nlp = '\0';
77012159Seric 				(void) strcat(obuf, p);
77112159Seric 				*nlp = '\n';
77212159Seric 				putline(obuf, fp, m);
77312159Seric 				p = ++nlp;
77412161Seric 				obuf[0] = '\0';
77512159Seric 			}
77612159Seric 			(void) strcat(obuf, p);
77710176Seric 			putline(obuf, fp, m);
7789382Seric 		}
7799382Seric 	}
7809382Seric }
7819382Seric /*
7829382Seric **  COMMAIZE -- output a header field, making a comma-translated list.
7839382Seric **
7849382Seric **	Parameters:
7859382Seric **		h -- the header field to output.
7869382Seric **		p -- the value to put in it.
7879382Seric **		fp -- file to put it to.
7889382Seric **		oldstyle -- TRUE if this is an old style header.
7899382Seric **		m -- a pointer to the mailer descriptor.  If NULL,
7909382Seric **			don't transform the name at all.
79155012Seric **		e -- the envelope containing the message.
7929382Seric **
7939382Seric **	Returns:
7949382Seric **		none.
7959382Seric **
7969382Seric **	Side Effects:
7979382Seric **		outputs "p" to file "fp".
7989382Seric */
7999382Seric 
80055012Seric commaize(h, p, fp, oldstyle, m, e)
8019382Seric 	register HDR *h;
8029382Seric 	register char *p;
8039382Seric 	FILE *fp;
8049382Seric 	bool oldstyle;
8059382Seric 	register MAILER *m;
80655012Seric 	register ENVELOPE *e;
8079382Seric {
8089382Seric 	register char *obp;
8099382Seric 	int opos;
8109382Seric 	bool firstone = TRUE;
81111157Seric 	char obuf[MAXLINE + 3];
8129382Seric 
8139382Seric 	/*
8149382Seric 	**  Output the address list translated by the
8159382Seric 	**  mailer and with commas.
8169382Seric 	*/
8179382Seric 
8189382Seric 	if (tTd(14, 2))
8199382Seric 		printf("commaize(%s: %s)\n", h->h_field, p);
8209382Seric 
8219382Seric 	obp = obuf;
82259579Seric 	(void) sprintf(obp, "%s: ", h->h_field);
8239382Seric 	opos = strlen(h->h_field) + 2;
8249382Seric 	obp += opos;
8259382Seric 
8269382Seric 	/*
8279382Seric 	**  Run through the list of values.
8289382Seric 	*/
8299382Seric 
8309382Seric 	while (*p != '\0')
8319382Seric 	{
8329382Seric 		register char *name;
83354983Seric 		register int c;
8349382Seric 		char savechar;
83559163Seric 		int flags;
83659163Seric 		auto int stat;
8379382Seric 		extern char *remotename();
8389382Seric 
8399382Seric 		/*
8409382Seric 		**  Find the end of the name.  New style names
8419382Seric 		**  end with a comma, old style names end with
8429382Seric 		**  a space character.  However, spaces do not
8439382Seric 		**  necessarily delimit an old-style name -- at
8449382Seric 		**  signs mean keep going.
8459382Seric 		*/
8469382Seric 
8479382Seric 		/* find end of name */
84858050Seric 		while ((isascii(*p) && isspace(*p)) || *p == ',')
8499382Seric 			p++;
8509382Seric 		name = p;
8519382Seric 		for (;;)
8529382Seric 		{
85358333Seric 			auto char *oldp;
85416909Seric 			char pvpbuf[PSBUFSIZE];
8559382Seric 			extern char **prescan();
8569382Seric 
85758333Seric 			(void) prescan(p, oldstyle ? ' ' : ',', pvpbuf, &oldp);
85858333Seric 			p = oldp;
8599382Seric 
8609382Seric 			/* look to see if we have an at sign */
86158050Seric 			while (*p != '\0' && isascii(*p) && isspace(*p))
8629382Seric 				p++;
8639382Seric 
86458170Seric 			if (*p != '@')
8659382Seric 			{
8669382Seric 				p = oldp;
8679382Seric 				break;
8689382Seric 			}
8699382Seric 			p += *p == '@' ? 1 : 2;
87058050Seric 			while (*p != '\0' && isascii(*p) && isspace(*p))
8719382Seric 				p++;
8729382Seric 		}
8739382Seric 		/* at the end of one complete name */
8749382Seric 
8759382Seric 		/* strip off trailing white space */
87658050Seric 		while (p >= name &&
87758050Seric 		       ((isascii(*p) && isspace(*p)) || *p == ',' || *p == '\0'))
8789382Seric 			p--;
8799382Seric 		if (++p == name)
8809382Seric 			continue;
8819382Seric 		savechar = *p;
8829382Seric 		*p = '\0';
8839382Seric 
8849382Seric 		/* translate the name to be relative */
88559163Seric 		flags = RF_HEADERADDR|RF_ADDDOMAIN;
88659163Seric 		if (bitset(H_FROM, h->h_flags))
88759163Seric 			flags |= RF_SENDERADDR;
88859163Seric 		stat = EX_OK;
88959163Seric 		name = remotename(name, m, flags, &stat, e);
8909382Seric 		if (*name == '\0')
8919382Seric 		{
8929382Seric 			*p = savechar;
8939382Seric 			continue;
8949382Seric 		}
8959382Seric 
8969382Seric 		/* output the name with nice formatting */
89754983Seric 		opos += strlen(name);
8989382Seric 		if (!firstone)
8999382Seric 			opos += 2;
9009382Seric 		if (opos > 78 && !firstone)
9019382Seric 		{
90210178Seric 			(void) strcpy(obp, ",\n");
90310176Seric 			putline(obuf, fp, m);
9049382Seric 			obp = obuf;
9059382Seric 			(void) sprintf(obp, "        ");
90610161Seric 			opos = strlen(obp);
90710161Seric 			obp += opos;
90854983Seric 			opos += strlen(name);
9099382Seric 		}
9109382Seric 		else if (!firstone)
9119382Seric 		{
9129382Seric 			(void) sprintf(obp, ", ");
9139382Seric 			obp += 2;
9149382Seric 		}
9159382Seric 
91654983Seric 		while ((c = *name++) != '\0' && obp < &obuf[MAXLINE])
91754983Seric 			*obp++ = c;
9189382Seric 		firstone = FALSE;
9199382Seric 		*p = savechar;
9209382Seric 	}
9219382Seric 	(void) strcpy(obp, "\n");
92210176Seric 	putline(obuf, fp, m);
9239382Seric }
9249382Seric /*
92558170Seric **  COPYHEADER -- copy header list
9269382Seric **
92758170Seric **	This routine is the equivalent of newstr for header lists
92858170Seric **
9299382Seric **	Parameters:
93058170Seric **		header -- list of header structures to copy.
9319382Seric **
9329382Seric **	Returns:
93358170Seric **		a copy of 'header'.
9349382Seric **
9359382Seric **	Side Effects:
9369382Seric **		none.
9379382Seric */
9389382Seric 
93958170Seric HDR *
94058170Seric copyheader(header)
94158170Seric 	register HDR *header;
9429382Seric {
94358170Seric 	register HDR *newhdr;
94458170Seric 	HDR *ret;
94558170Seric 	register HDR **tail = &ret;
9469382Seric 
94758170Seric 	while (header != NULL)
94858170Seric 	{
94958170Seric 		newhdr = (HDR *) xalloc(sizeof(HDR));
95058170Seric 		STRUCTCOPY(*header, *newhdr);
95158170Seric 		*tail = newhdr;
95258170Seric 		tail = &newhdr->h_link;
95358170Seric 		header = header->h_link;
95458170Seric 	}
95558170Seric 	*tail = NULL;
95658170Seric 
95758170Seric 	return ret;
9589382Seric }
959