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*58704Seric static char sccsid[] = "@(#)headers.c	6.21 (Berkeley) 03/18/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 	makelower(fname);
844091Seric 
854091Seric 	/* strip field value on front */
864091Seric 	if (*fvalue == ' ')
874091Seric 		fvalue++;
884091Seric 
894091Seric 	/* see if it is a known type */
904091Seric 	for (hi = HdrInfo; hi->hi_field != NULL; hi++)
914091Seric 	{
924091Seric 		if (strcmp(hi->hi_field, fname) == 0)
934091Seric 			break;
944091Seric 	}
954091Seric 
9611414Seric 	/* see if this is a resent message */
9711930Seric 	if (!def && bitset(H_RESENT, hi->hi_flags))
9855012Seric 		e->e_flags |= EF_RESENT;
9911414Seric 
1004091Seric 	/* if this means "end of header" quit now */
1014091Seric 	if (bitset(H_EOH, hi->hi_flags))
1024091Seric 		return (hi->hi_flags);
1034091Seric 
10411414Seric 	/* drop explicit From: if same as what we would generate -- for MH */
10514784Seric 	p = "resent-from";
10655012Seric 	if (!bitset(EF_RESENT, e->e_flags))
10714784Seric 		p += 7;
10814784Seric 	if (!def && !QueueRun && strcmp(fname, p) == 0)
10911414Seric 	{
11055012Seric 		if (e->e_from.q_paddr != NULL &&
11155012Seric 		    strcmp(fvalue, e->e_from.q_paddr) == 0)
11211414Seric 			return (hi->hi_flags);
11311414Seric 	}
11411414Seric 
11514784Seric 	/* delete default value for this header */
11655012Seric 	for (hp = &e->e_header; (h = *hp) != NULL; hp = &h->h_link)
11714784Seric 	{
11814784Seric 		if (strcmp(fname, h->h_field) == 0 &&
11914784Seric 		    bitset(H_DEFAULT, h->h_flags) &&
12014784Seric 		    !bitset(H_FORCE, h->h_flags))
12114784Seric 			h->h_value = NULL;
12214784Seric 	}
12314784Seric 
12413012Seric 	/* create a new node */
12513012Seric 	h = (HDR *) xalloc(sizeof *h);
12613012Seric 	h->h_field = newstr(fname);
12713012Seric 	h->h_value = NULL;
12813012Seric 	h->h_link = NULL;
12923117Seric 	bcopy((char *) mopts, (char *) h->h_mflags, sizeof mopts);
13013012Seric 	*hp = h;
1318066Seric 	h->h_flags = hi->hi_flags;
1324091Seric 	if (def)
1334091Seric 		h->h_flags |= H_DEFAULT;
1349059Seric 	if (cond)
1359059Seric 		h->h_flags |= H_CHECK;
1364091Seric 	if (h->h_value != NULL)
1379351Seric 		free((char *) h->h_value);
1388066Seric 	h->h_value = newstr(fvalue);
1394091Seric 
1405937Seric 	/* hack to see if this is a new format message */
1418095Seric 	if (!def && bitset(H_RCPT|H_FROM, h->h_flags) &&
14256795Seric 	    (strchr(fvalue, ',') != NULL || strchr(fvalue, '(') != NULL ||
14356795Seric 	     strchr(fvalue, '<') != NULL || strchr(fvalue, ';') != NULL))
1448089Seric 	{
14555012Seric 		e->e_flags &= ~EF_OLDSTYLE;
1468089Seric 	}
1475937Seric 
1484091Seric 	return (h->h_flags);
1494091Seric }
1504091Seric /*
1516980Seric **  ADDHEADER -- add a header entry to the end of the queue.
1526980Seric **
1536980Seric **	This bypasses the special checking of chompheader.
1546980Seric **
1556980Seric **	Parameters:
1566980Seric **		field -- the name of the header field.
1576980Seric **		value -- the value of the field.  It must be lower-cased.
1586980Seric **		e -- the envelope to add them to.
1596980Seric **
1606980Seric **	Returns:
1616980Seric **		none.
1626980Seric **
1636980Seric **	Side Effects:
1646980Seric **		adds the field on the list of headers for this envelope.
1656980Seric */
1666980Seric 
1676980Seric addheader(field, value, e)
1686980Seric 	char *field;
1696980Seric 	char *value;
1706980Seric 	ENVELOPE *e;
1716980Seric {
1726980Seric 	register HDR *h;
1736980Seric 	register struct hdrinfo *hi;
1746980Seric 	HDR **hp;
1756980Seric 
1766980Seric 	/* find info struct */
1776980Seric 	for (hi = HdrInfo; hi->hi_field != NULL; hi++)
1786980Seric 	{
1796980Seric 		if (strcmp(field, hi->hi_field) == 0)
1806980Seric 			break;
1816980Seric 	}
1826980Seric 
1836980Seric 	/* find current place in list -- keep back pointer? */
1846980Seric 	for (hp = &e->e_header; (h = *hp) != NULL; hp = &h->h_link)
1856980Seric 	{
1866980Seric 		if (strcmp(field, h->h_field) == 0)
1876980Seric 			break;
1886980Seric 	}
1896980Seric 
1906980Seric 	/* allocate space for new header */
1916980Seric 	h = (HDR *) xalloc(sizeof *h);
1926980Seric 	h->h_field = field;
1936980Seric 	h->h_value = newstr(value);
1947368Seric 	h->h_link = *hp;
1956980Seric 	h->h_flags = hi->hi_flags | H_DEFAULT;
19610689Seric 	clrbitmap(h->h_mflags);
1976980Seric 	*hp = h;
1986980Seric }
1996980Seric /*
2004091Seric **  HVALUE -- return value of a header.
2014091Seric **
2024091Seric **	Only "real" fields (i.e., ones that have not been supplied
2034091Seric **	as a default) are used.
2044091Seric **
2054091Seric **	Parameters:
2064091Seric **		field -- the field name.
20755012Seric **		e -- the envelope containing the header.
2084091Seric **
2094091Seric **	Returns:
2104091Seric **		pointer to the value part.
2114091Seric **		NULL if not found.
2124091Seric **
2134091Seric **	Side Effects:
2149382Seric **		none.
2154091Seric */
2164091Seric 
2174091Seric char *
21855012Seric hvalue(field, e)
2194091Seric 	char *field;
22055012Seric 	register ENVELOPE *e;
2214091Seric {
2224091Seric 	register HDR *h;
2234091Seric 
22455012Seric 	for (h = e->e_header; h != NULL; h = h->h_link)
2254091Seric 	{
2264091Seric 		if (!bitset(H_DEFAULT, h->h_flags) && strcmp(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.
26657642Seric **		queuejob -- set if running a queued job.
2677783Seric **
2687783Seric **	Returns:
2697783Seric **		none.
2707783Seric **
2717783Seric **	Side Effects:
2727783Seric **		Sets a bunch of global variables from information
2739382Seric **			in the collected header.
2749382Seric **		Aborts the message if the hop count is exceeded.
2757783Seric */
2767783Seric 
27757642Seric eatheader(e, queuejob)
2789382Seric 	register ENVELOPE *e;
27957642Seric 	bool queuejob;
2807783Seric {
2817783Seric 	register HDR *h;
2827783Seric 	register char *p;
2839382Seric 	int hopcnt = 0;
28457208Seric 	char *msgid;
28558688Seric 	char buf[MAXLINE];
2867783Seric 
28758688Seric 	/*
28858688Seric 	**  Set up macros for possible expansion in headers.
28958688Seric 	*/
29058688Seric 
291*58704Seric 	define('f', e->e_sender, e);
292*58704Seric 	define('g', e->e_sender, e);
29358688Seric 
2949382Seric 	if (tTd(32, 1))
2959382Seric 		printf("----- collected header -----\n");
29657208Seric 	msgid = "<none>";
2979382Seric 	for (h = e->e_header; h != NULL; h = h->h_link)
2987783Seric 	{
2997783Seric 		extern char *capitalize();
3007783Seric 
30158688Seric 		/* do early binding */
30258688Seric 		if (bitset(H_DEFAULT, h->h_flags) && h->h_value != NULL)
30358688Seric 		{
30458688Seric 			expand(h->h_value, buf, &buf[sizeof buf], e);
30558688Seric 			if (buf[0] != '\0')
30658688Seric 			{
30758688Seric 				free(h->h_value);
30858688Seric 				h->h_value = newstr(buf);
30958688Seric 				h->h_flags &= ~H_DEFAULT;
31058688Seric 			}
31158688Seric 		}
31258688Seric 
3139382Seric 		if (tTd(32, 1))
3147783Seric 			printf("%s: %s\n", capitalize(h->h_field), h->h_value);
31557359Seric 
31611414Seric 		/* count the number of times it has been processed */
3179382Seric 		if (bitset(H_TRACE, h->h_flags))
3189382Seric 			hopcnt++;
31911414Seric 
32011414Seric 		/* send to this person if we so desire */
32111414Seric 		if (GrabTo && bitset(H_RCPT, h->h_flags) &&
32211414Seric 		    !bitset(H_DEFAULT, h->h_flags) &&
32355012Seric 		    (!bitset(EF_RESENT, e->e_flags) || bitset(H_RESENT, h->h_flags)))
32411414Seric 		{
32558082Seric 			(void) sendtolist(h->h_value, (ADDRESS *) NULL,
32658082Seric 					  &e->e_sendqueue, e);
32711414Seric 		}
32811414Seric 
32957208Seric 		/* save the message-id for logging */
33057642Seric 		if (!queuejob && h->h_value != NULL &&
33111299Seric 		    strcmp(h->h_field, "message-id") == 0)
33211290Seric 		{
33357208Seric 			msgid = h->h_value;
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);
36057642Seric 	if (!queuejob)
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
38258020Seric 	if (!queuejob && LogLevel > 4)
38311290Seric 	{
38457359Seric 		char *name;
38557232Seric 		char hbuf[MAXNAME];
38657359Seric 		char sbuf[MAXLINE];
38736230Skarels 
38858667Seric 		if (bitset(EF_RESPONSE, e->e_flags))
38958667Seric 			name = "[RESPONSE]";
39058667Seric 		else if (RealHostName[0] == '[')
39136230Skarels 			name = RealHostName;
39236230Skarels 		else
39357359Seric 		{
39457359Seric 			extern char *inet_ntoa();
39557359Seric 
39657359Seric 			name = hbuf;
39754999Seric 			(void)sprintf(hbuf, "%.80s (%s)",
39836230Skarels 			    RealHostName, inet_ntoa(RealHostAddr.sin_addr));
39957359Seric 		}
40057359Seric 
40157359Seric 		/* some versions of syslog only take 5 printf args */
40257589Seric 		sprintf(sbuf, "from=%.200s, size=%ld, class=%d, pri=%ld, nrcpts=%d, msgid=%.100s",
40358558Seric 		    e->e_from.q_paddr, e->e_msgsize, e->e_class,
40457589Seric 		    e->e_msgpriority, e->e_nrcpts, msgid);
40558686Seric 		syslog(LOG_INFO, "%s: %s, relay=%s",
40657359Seric 		    e->e_id, sbuf, name);
40711290Seric 	}
40856795Seric # endif /* LOG */
4097783Seric }
4107783Seric /*
4117783Seric **  PRIENCODE -- encode external priority names into internal values.
4127783Seric **
4137783Seric **	Parameters:
4147783Seric **		p -- priority in ascii.
4157783Seric **
4167783Seric **	Returns:
4177783Seric **		priority as a numeric level.
4187783Seric **
4197783Seric **	Side Effects:
4207783Seric **		none.
4217783Seric */
4227783Seric 
4237783Seric priencode(p)
4247783Seric 	char *p;
4257783Seric {
4268253Seric 	register int i;
4277783Seric 
4288253Seric 	for (i = 0; i < NumPriorities; i++)
4297783Seric 	{
43033725Sbostic 		if (!strcasecmp(p, Priorities[i].pri_name))
4318253Seric 			return (Priorities[i].pri_val);
4327783Seric 	}
4338253Seric 
4348253Seric 	/* unknown priority */
4358253Seric 	return (0);
4367783Seric }
4377783Seric /*
4387890Seric **  CRACKADDR -- parse an address and turn it into a macro
4397783Seric **
4407783Seric **	This doesn't actually parse the address -- it just extracts
4417783Seric **	it and replaces it with "$g".  The parse is totally ad hoc
4427783Seric **	and isn't even guaranteed to leave something syntactically
4437783Seric **	identical to what it started with.  However, it does leave
4447783Seric **	something semantically identical.
4457783Seric **
44651379Seric **	This algorithm has been cleaned up to handle a wider range
44751379Seric **	of cases -- notably quoted and backslash escaped strings.
44851379Seric **	This modification makes it substantially better at preserving
44951379Seric **	the original syntax.
4507783Seric **
4517783Seric **	Parameters:
4527890Seric **		addr -- the address to be cracked.
4537783Seric **
4547783Seric **	Returns:
4557783Seric **		a pointer to the new version.
4567783Seric **
4577783Seric **	Side Effects:
4587890Seric **		none.
4597783Seric **
4607783Seric **	Warning:
4617783Seric **		The return value is saved in local storage and should
4627783Seric **		be copied if it is to be reused.
4637783Seric */
4647783Seric 
4657783Seric char *
4667890Seric crackaddr(addr)
4677890Seric 	register char *addr;
4687783Seric {
4697783Seric 	register char *p;
47051379Seric 	register char c;
47151379Seric 	int cmtlev;
47256764Seric 	int realcmtlev;
47356764Seric 	int anglelev, realanglelev;
47451379Seric 	int copylev;
47551379Seric 	bool qmode;
47656764Seric 	bool realqmode;
47756764Seric 	bool skipping;
47851379Seric 	bool putgmac = FALSE;
47956735Seric 	bool quoteit = FALSE;
48051379Seric 	register char *bp;
48156764Seric 	char *buflim;
4827783Seric 	static char buf[MAXNAME];
4837783Seric 
4847783Seric 	if (tTd(33, 1))
4857890Seric 		printf("crackaddr(%s)\n", addr);
4867783Seric 
4878082Seric 	/* strip leading spaces */
48858050Seric 	while (*addr != '\0' && isascii(*addr) && isspace(*addr))
4898082Seric 		addr++;
4908082Seric 
4917783Seric 	/*
49251379Seric 	**  Start by assuming we have no angle brackets.  This will be
49351379Seric 	**  adjusted later if we find them.
4947783Seric 	*/
4957783Seric 
49651379Seric 	bp = buf;
49756764Seric 	buflim = &buf[sizeof buf - 5];
49851379Seric 	p = addr;
49956764Seric 	copylev = anglelev = realanglelev = cmtlev = realcmtlev = 0;
50056764Seric 	qmode = realqmode = FALSE;
50151379Seric 
50251379Seric 	while ((c = *p++) != '\0')
5037783Seric 	{
50456764Seric 		/*
50556764Seric 		**  If the buffer is overful, go into a special "skipping"
50656764Seric 		**  mode that tries to keep legal syntax but doesn't actually
50756764Seric 		**  output things.
50856764Seric 		*/
5097783Seric 
51056764Seric 		skipping = bp >= buflim;
51156735Seric 
51256764Seric 		if (copylev > 0 && !skipping)
51356764Seric 			*bp++ = c;
51456735Seric 
51551379Seric 		/* check for backslash escapes */
51651379Seric 		if (c == '\\')
5177783Seric 		{
51851379Seric 			if ((c = *p++) == '\0')
5197783Seric 			{
52051379Seric 				/* too far */
52151379Seric 				p--;
52251379Seric 				goto putg;
5237783Seric 			}
52456764Seric 			if (copylev > 0 && !skipping)
52551379Seric 				*bp++ = c;
52651379Seric 			goto putg;
5277783Seric 		}
5287783Seric 
52951379Seric 		/* check for quoted strings */
53051379Seric 		if (c == '"')
5317783Seric 		{
53251379Seric 			qmode = !qmode;
53356764Seric 			if (copylev > 0 && !skipping)
53456764Seric 				realqmode = !realqmode;
53551379Seric 			continue;
5367783Seric 		}
53751379Seric 		if (qmode)
53851379Seric 			goto putg;
5397783Seric 
54051379Seric 		/* check for comments */
54151379Seric 		if (c == '(')
5427783Seric 		{
54351379Seric 			cmtlev++;
54456764Seric 
54556764Seric 			/* allow space for closing paren */
54656764Seric 			if (!skipping)
54756764Seric 			{
54856764Seric 				buflim--;
54956764Seric 				realcmtlev++;
55056764Seric 				if (copylev++ <= 0)
55156764Seric 				{
55256764Seric 					*bp++ = ' ';
55356764Seric 					*bp++ = c;
55456764Seric 				}
55556764Seric 			}
55651379Seric 		}
55751379Seric 		if (cmtlev > 0)
55851379Seric 		{
55951379Seric 			if (c == ')')
5607783Seric 			{
56151379Seric 				cmtlev--;
56251379Seric 				copylev--;
56356764Seric 				if (!skipping)
56456764Seric 				{
56556764Seric 					realcmtlev--;
56656764Seric 					buflim++;
56756764Seric 				}
5687783Seric 			}
5697783Seric 			continue;
5707783Seric 		}
57156764Seric 		else if (c == ')')
57256764Seric 		{
57356764Seric 			/* syntax error: unmatched ) */
57456764Seric 			if (!skipping)
57556764Seric 				bp--;
57656764Seric 		}
5777783Seric 
57856764Seric 
57956764Seric 		/* check for characters that may have to be quoted */
58057175Seric 		if (strchr(".'@,;:\\()", c) != NULL)
58156764Seric 		{
58256764Seric 			/*
58356764Seric 			**  If these occur as the phrase part of a <>
58456764Seric 			**  construct, but are not inside of () or already
58556764Seric 			**  quoted, they will have to be quoted.  Note that
58656764Seric 			**  now (but don't actually do the quoting).
58756764Seric 			*/
58856764Seric 
58956764Seric 			if (cmtlev <= 0 && !qmode)
59056764Seric 				quoteit = TRUE;
59156764Seric 		}
59256764Seric 
59351379Seric 		/* check for angle brackets */
59451379Seric 		if (c == '<')
59551379Seric 		{
59656735Seric 			register char *q;
59756735Seric 
59851379Seric 			/* oops -- have to change our mind */
59956764Seric 			anglelev++;
60056764Seric 			if (!skipping)
60156764Seric 				realanglelev++;
60256764Seric 
60356735Seric 			bp = buf;
60456735Seric 			if (quoteit)
60556735Seric 			{
60656735Seric 				*bp++ = '"';
60756735Seric 
60856735Seric 				/* back up over the '<' and any spaces */
60956735Seric 				--p;
61058050Seric 				while (isascii(*--p) && isspace(*p))
61156735Seric 					continue;
61256735Seric 				p++;
61356735Seric 			}
61456735Seric 			for (q = addr; q < p; )
61556735Seric 			{
61656735Seric 				c = *q++;
61756764Seric 				if (bp < buflim)
61856764Seric 				{
61956764Seric 					if (quoteit && c == '"')
62056764Seric 						*bp++ = '\\';
62156764Seric 					*bp++ = c;
62256764Seric 				}
62356735Seric 			}
62456735Seric 			if (quoteit)
62556735Seric 			{
62656735Seric 				*bp++ = '"';
62756764Seric 				while ((c = *p++) != '<')
62856764Seric 				{
62956764Seric 					if (bp < buflim)
63056764Seric 						*bp++ = c;
63156764Seric 				}
63256764Seric 				*bp++ = c;
63356735Seric 			}
63451379Seric 			copylev = 0;
63556735Seric 			putgmac = quoteit = FALSE;
63651379Seric 			continue;
63751379Seric 		}
6387783Seric 
63951379Seric 		if (c == '>')
6407783Seric 		{
64156764Seric 			if (anglelev > 0)
64256764Seric 			{
64356764Seric 				anglelev--;
64456764Seric 				if (!skipping)
64556764Seric 				{
64656764Seric 					realanglelev--;
64756764Seric 					buflim++;
64856764Seric 				}
64956764Seric 			}
65056764Seric 			else if (!skipping)
65156764Seric 			{
65256764Seric 				/* syntax error: unmatched > */
65356764Seric 				if (copylev > 0)
65456764Seric 					bp--;
65556764Seric 				continue;
65656764Seric 			}
65751379Seric 			if (copylev++ <= 0)
65851379Seric 				*bp++ = c;
65951379Seric 			continue;
6607783Seric 		}
66151379Seric 
66251379Seric 		/* must be a real address character */
66351379Seric 	putg:
66451379Seric 		if (copylev <= 0 && !putgmac)
66551379Seric 		{
66658050Seric 			*bp++ = MACROEXPAND;
66751379Seric 			*bp++ = 'g';
66851379Seric 			putgmac = TRUE;
66951379Seric 		}
6707783Seric 	}
6717783Seric 
67256764Seric 	/* repair any syntactic damage */
67356764Seric 	if (realqmode)
67456764Seric 		*bp++ = '"';
67556764Seric 	while (realcmtlev-- > 0)
67656764Seric 		*bp++ = ')';
67756764Seric 	while (realanglelev-- > 0)
67856764Seric 		*bp++ = '>';
67951379Seric 	*bp++ = '\0';
6807783Seric 
6817783Seric 	if (tTd(33, 1))
6827944Seric 		printf("crackaddr=>`%s'\n", buf);
6837783Seric 
6847783Seric 	return (buf);
6857783Seric }
6869382Seric /*
6879382Seric **  PUTHEADER -- put the header part of a message from the in-core copy
6889382Seric **
6899382Seric **	Parameters:
6909382Seric **		fp -- file to put it on.
6919382Seric **		m -- mailer to use.
6929382Seric **		e -- envelope to use.
6939382Seric **
6949382Seric **	Returns:
6959382Seric **		none.
6969382Seric **
6979382Seric **	Side Effects:
6989382Seric **		none.
6999382Seric */
7009382Seric 
70157589Seric /*
70257589Seric  * Macro for fast max (not available in e.g. DG/UX, 386/ix).
70357589Seric  */
70457589Seric #ifndef MAX
70557589Seric # define MAX(a,b) (((a)>(b))?(a):(b))
70657589Seric #endif
70757589Seric 
70810176Seric putheader(fp, m, e)
7099382Seric 	register FILE *fp;
7109382Seric 	register MAILER *m;
7119382Seric 	register ENVELOPE *e;
7129382Seric {
71357135Seric 	char buf[MAX(MAXLINE,BUFSIZ)];
7149382Seric 	register HDR *h;
71557135Seric 	char obuf[MAXLINE];
7169382Seric 
7179382Seric 	for (h = e->e_header; h != NULL; h = h->h_link)
7189382Seric 	{
7199382Seric 		register char *p;
72010689Seric 		extern bool bitintersect();
7219382Seric 
7229382Seric 		if (bitset(H_CHECK|H_ACHECK, h->h_flags) &&
72310689Seric 		    !bitintersect(h->h_mflags, m->m_flags))
7249382Seric 			continue;
7259382Seric 
72611414Seric 		/* handle Resent-... headers specially */
72711414Seric 		if (bitset(H_RESENT, h->h_flags) && !bitset(EF_RESENT, e->e_flags))
72811414Seric 			continue;
72911414Seric 
7309382Seric 		p = h->h_value;
7319382Seric 		if (bitset(H_DEFAULT, h->h_flags))
7329382Seric 		{
7339382Seric 			/* macro expand value if generated internally */
7349382Seric 			expand(p, buf, &buf[sizeof buf], e);
7359382Seric 			p = buf;
7369382Seric 			if (p == NULL || *p == '\0')
7379382Seric 				continue;
7389382Seric 		}
7399382Seric 
7409382Seric 		if (bitset(H_FROM|H_RCPT, h->h_flags))
7419382Seric 		{
7429382Seric 			/* address field */
7439382Seric 			bool oldstyle = bitset(EF_OLDSTYLE, e->e_flags);
7449382Seric 
7459382Seric 			if (bitset(H_FROM, h->h_flags))
7469382Seric 				oldstyle = FALSE;
74755012Seric 			commaize(h, p, fp, oldstyle, m, e);
7489382Seric 		}
7499382Seric 		else
7509382Seric 		{
7519382Seric 			/* vanilla header line */
75212159Seric 			register char *nlp;
75357642Seric 			extern char *capitalize();
75412159Seric 
75512159Seric 			(void) sprintf(obuf, "%s: ", capitalize(h->h_field));
75656795Seric 			while ((nlp = strchr(p, '\n')) != NULL)
75712159Seric 			{
75812159Seric 				*nlp = '\0';
75912159Seric 				(void) strcat(obuf, p);
76012159Seric 				*nlp = '\n';
76112159Seric 				putline(obuf, fp, m);
76212159Seric 				p = ++nlp;
76312161Seric 				obuf[0] = '\0';
76412159Seric 			}
76512159Seric 			(void) strcat(obuf, p);
76610176Seric 			putline(obuf, fp, m);
7679382Seric 		}
7689382Seric 	}
7699382Seric }
7709382Seric /*
7719382Seric **  COMMAIZE -- output a header field, making a comma-translated list.
7729382Seric **
7739382Seric **	Parameters:
7749382Seric **		h -- the header field to output.
7759382Seric **		p -- the value to put in it.
7769382Seric **		fp -- file to put it to.
7779382Seric **		oldstyle -- TRUE if this is an old style header.
7789382Seric **		m -- a pointer to the mailer descriptor.  If NULL,
7799382Seric **			don't transform the name at all.
78055012Seric **		e -- the envelope containing the message.
7819382Seric **
7829382Seric **	Returns:
7839382Seric **		none.
7849382Seric **
7859382Seric **	Side Effects:
7869382Seric **		outputs "p" to file "fp".
7879382Seric */
7889382Seric 
78955012Seric commaize(h, p, fp, oldstyle, m, e)
7909382Seric 	register HDR *h;
7919382Seric 	register char *p;
7929382Seric 	FILE *fp;
7939382Seric 	bool oldstyle;
7949382Seric 	register MAILER *m;
79555012Seric 	register ENVELOPE *e;
7969382Seric {
7979382Seric 	register char *obp;
7989382Seric 	int opos;
7999382Seric 	bool firstone = TRUE;
80011157Seric 	char obuf[MAXLINE + 3];
80157642Seric 	extern char *capitalize();
8029382Seric 
8039382Seric 	/*
8049382Seric 	**  Output the address list translated by the
8059382Seric 	**  mailer and with commas.
8069382Seric 	*/
8079382Seric 
8089382Seric 	if (tTd(14, 2))
8099382Seric 		printf("commaize(%s: %s)\n", h->h_field, p);
8109382Seric 
8119382Seric 	obp = obuf;
8129382Seric 	(void) sprintf(obp, "%s: ", capitalize(h->h_field));
8139382Seric 	opos = strlen(h->h_field) + 2;
8149382Seric 	obp += opos;
8159382Seric 
8169382Seric 	/*
8179382Seric 	**  Run through the list of values.
8189382Seric 	*/
8199382Seric 
8209382Seric 	while (*p != '\0')
8219382Seric 	{
8229382Seric 		register char *name;
82354983Seric 		register int c;
8249382Seric 		char savechar;
8259382Seric 		extern char *remotename();
8269382Seric 
8279382Seric 		/*
8289382Seric 		**  Find the end of the name.  New style names
8299382Seric 		**  end with a comma, old style names end with
8309382Seric 		**  a space character.  However, spaces do not
8319382Seric 		**  necessarily delimit an old-style name -- at
8329382Seric 		**  signs mean keep going.
8339382Seric 		*/
8349382Seric 
8359382Seric 		/* find end of name */
83658050Seric 		while ((isascii(*p) && isspace(*p)) || *p == ',')
8379382Seric 			p++;
8389382Seric 		name = p;
8399382Seric 		for (;;)
8409382Seric 		{
84158333Seric 			auto char *oldp;
84216909Seric 			char pvpbuf[PSBUFSIZE];
8439382Seric 			extern char **prescan();
8449382Seric 
84558333Seric 			(void) prescan(p, oldstyle ? ' ' : ',', pvpbuf, &oldp);
84658333Seric 			p = oldp;
8479382Seric 
8489382Seric 			/* look to see if we have an at sign */
84958050Seric 			while (*p != '\0' && isascii(*p) && isspace(*p))
8509382Seric 				p++;
8519382Seric 
85258170Seric 			if (*p != '@')
8539382Seric 			{
8549382Seric 				p = oldp;
8559382Seric 				break;
8569382Seric 			}
8579382Seric 			p += *p == '@' ? 1 : 2;
85858050Seric 			while (*p != '\0' && isascii(*p) && isspace(*p))
8599382Seric 				p++;
8609382Seric 		}
8619382Seric 		/* at the end of one complete name */
8629382Seric 
8639382Seric 		/* strip off trailing white space */
86458050Seric 		while (p >= name &&
86558050Seric 		       ((isascii(*p) && isspace(*p)) || *p == ',' || *p == '\0'))
8669382Seric 			p--;
8679382Seric 		if (++p == name)
8689382Seric 			continue;
8699382Seric 		savechar = *p;
8709382Seric 		*p = '\0';
8719382Seric 
8729382Seric 		/* translate the name to be relative */
87358020Seric 		name = remotename(name, m, bitset(H_FROM, h->h_flags),
87458509Seric 				  TRUE, FALSE, TRUE, e);
8759382Seric 		if (*name == '\0')
8769382Seric 		{
8779382Seric 			*p = savechar;
8789382Seric 			continue;
8799382Seric 		}
8809382Seric 
8819382Seric 		/* output the name with nice formatting */
88254983Seric 		opos += strlen(name);
8839382Seric 		if (!firstone)
8849382Seric 			opos += 2;
8859382Seric 		if (opos > 78 && !firstone)
8869382Seric 		{
88710178Seric 			(void) strcpy(obp, ",\n");
88810176Seric 			putline(obuf, fp, m);
8899382Seric 			obp = obuf;
8909382Seric 			(void) sprintf(obp, "        ");
89110161Seric 			opos = strlen(obp);
89210161Seric 			obp += opos;
89354983Seric 			opos += strlen(name);
8949382Seric 		}
8959382Seric 		else if (!firstone)
8969382Seric 		{
8979382Seric 			(void) sprintf(obp, ", ");
8989382Seric 			obp += 2;
8999382Seric 		}
9009382Seric 
9019382Seric 		/* strip off quote bits as we output */
90254983Seric 		while ((c = *name++) != '\0' && obp < &obuf[MAXLINE])
9039382Seric 		{
90454983Seric 			if (bitnset(M_7BITS, m->m_flags))
90554983Seric 				c &= 0177;
90654983Seric 			*obp++ = c;
9079382Seric 		}
9089382Seric 		firstone = FALSE;
9099382Seric 		*p = savechar;
9109382Seric 	}
9119382Seric 	(void) strcpy(obp, "\n");
91210176Seric 	putline(obuf, fp, m);
9139382Seric }
9149382Seric /*
91558170Seric **  COPYHEADER -- copy header list
9169382Seric **
91758170Seric **	This routine is the equivalent of newstr for header lists
91858170Seric **
9199382Seric **	Parameters:
92058170Seric **		header -- list of header structures to copy.
9219382Seric **
9229382Seric **	Returns:
92358170Seric **		a copy of 'header'.
9249382Seric **
9259382Seric **	Side Effects:
9269382Seric **		none.
9279382Seric */
9289382Seric 
92958170Seric HDR *
93058170Seric copyheader(header)
93158170Seric 	register HDR *header;
9329382Seric {
93358170Seric 	register HDR *newhdr;
93458170Seric 	HDR *ret;
93558170Seric 	register HDR **tail = &ret;
9369382Seric 
93758170Seric 	while (header != NULL)
93858170Seric 	{
93958170Seric 		newhdr = (HDR *) xalloc(sizeof(HDR));
94058170Seric 		STRUCTCOPY(*header, *newhdr);
94158170Seric 		*tail = newhdr;
94258170Seric 		tail = &newhdr->h_link;
94358170Seric 		header = header->h_link;
94458170Seric 	}
94558170Seric 	*tail = NULL;
94658170Seric 
94758170Seric 	return ret;
9489382Seric }
949