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*58789Seric static char sccsid[] = "@(#)headers.c	6.25 (Berkeley) 03/23/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;
10858737Seric 	if (!def && !bitset(EF_QUEUERUN, e->e_flags) && 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:
156*58789Seric **		field -- the name of the header field.  It must be
157*58789Seric **			lower-cased.
158*58789Seric **		value -- the value of the field.
1596980Seric **		e -- the envelope to add them to.
1606980Seric **
1616980Seric **	Returns:
1626980Seric **		none.
1636980Seric **
1646980Seric **	Side Effects:
1656980Seric **		adds the field on the list of headers for this envelope.
1666980Seric */
1676980Seric 
1686980Seric addheader(field, value, e)
1696980Seric 	char *field;
1706980Seric 	char *value;
1716980Seric 	ENVELOPE *e;
1726980Seric {
1736980Seric 	register HDR *h;
1746980Seric 	register struct hdrinfo *hi;
1756980Seric 	HDR **hp;
1766980Seric 
1776980Seric 	/* find info struct */
1786980Seric 	for (hi = HdrInfo; hi->hi_field != NULL; hi++)
1796980Seric 	{
1806980Seric 		if (strcmp(field, hi->hi_field) == 0)
1816980Seric 			break;
1826980Seric 	}
1836980Seric 
1846980Seric 	/* find current place in list -- keep back pointer? */
1856980Seric 	for (hp = &e->e_header; (h = *hp) != NULL; hp = &h->h_link)
1866980Seric 	{
1876980Seric 		if (strcmp(field, h->h_field) == 0)
1886980Seric 			break;
1896980Seric 	}
1906980Seric 
1916980Seric 	/* allocate space for new header */
1926980Seric 	h = (HDR *) xalloc(sizeof *h);
1936980Seric 	h->h_field = field;
1946980Seric 	h->h_value = newstr(value);
1957368Seric 	h->h_link = *hp;
1966980Seric 	h->h_flags = hi->hi_flags | H_DEFAULT;
19710689Seric 	clrbitmap(h->h_mflags);
1986980Seric 	*hp = h;
1996980Seric }
2006980Seric /*
2014091Seric **  HVALUE -- return value of a header.
2024091Seric **
2034091Seric **	Only "real" fields (i.e., ones that have not been supplied
2044091Seric **	as a default) are used.
2054091Seric **
2064091Seric **	Parameters:
2074091Seric **		field -- the field name.
20855012Seric **		e -- the envelope containing the header.
2094091Seric **
2104091Seric **	Returns:
2114091Seric **		pointer to the value part.
2124091Seric **		NULL if not found.
2134091Seric **
2144091Seric **	Side Effects:
2159382Seric **		none.
2164091Seric */
2174091Seric 
2184091Seric char *
21955012Seric hvalue(field, e)
2204091Seric 	char *field;
22155012Seric 	register ENVELOPE *e;
2224091Seric {
2234091Seric 	register HDR *h;
2244091Seric 
22555012Seric 	for (h = e->e_header; h != NULL; h = h->h_link)
2264091Seric 	{
2274091Seric 		if (!bitset(H_DEFAULT, h->h_flags) && strcmp(h->h_field, field) == 0)
2284091Seric 			return (h->h_value);
2294091Seric 	}
2304091Seric 	return (NULL);
2314091Seric }
2324091Seric /*
2334091Seric **  ISHEADER -- predicate telling if argument is a header.
2344091Seric **
2354319Seric **	A line is a header if it has a single word followed by
2364319Seric **	optional white space followed by a colon.
2374319Seric **
2384091Seric **	Parameters:
2394091Seric **		s -- string to check for possible headerness.
2404091Seric **
2414091Seric **	Returns:
2424091Seric **		TRUE if s is a header.
2434091Seric **		FALSE otherwise.
2444091Seric **
2454091Seric **	Side Effects:
2464091Seric **		none.
2474091Seric */
2484091Seric 
2494091Seric bool
2504091Seric isheader(s)
2514091Seric 	register char *s;
2524091Seric {
2539382Seric 	while (*s > ' ' && *s != ':' && *s != '\0')
2544091Seric 		s++;
2559382Seric 
2569382Seric 	/* following technically violates RFC822 */
25758050Seric 	while (isascii(*s) && isspace(*s))
2584091Seric 		s++;
2599382Seric 
2604091Seric 	return (*s == ':');
2614091Seric }
2625919Seric /*
2637783Seric **  EATHEADER -- run through the stored header and extract info.
2647783Seric **
2657783Seric **	Parameters:
2669382Seric **		e -- the envelope to process.
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 
27758737Seric eatheader(e)
2789382Seric 	register ENVELOPE *e;
2797783Seric {
2807783Seric 	register HDR *h;
2817783Seric 	register char *p;
2829382Seric 	int hopcnt = 0;
28357208Seric 	char *msgid;
28458688Seric 	char buf[MAXLINE];
2857783Seric 
28658688Seric 	/*
28758688Seric 	**  Set up macros for possible expansion in headers.
28858688Seric 	*/
28958688Seric 
29058704Seric 	define('f', e->e_sender, e);
29158704Seric 	define('g', e->e_sender, e);
29258688Seric 
2939382Seric 	if (tTd(32, 1))
2949382Seric 		printf("----- collected header -----\n");
29557208Seric 	msgid = "<none>";
2969382Seric 	for (h = e->e_header; h != NULL; h = h->h_link)
2977783Seric 	{
2987783Seric 		extern char *capitalize();
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))
3127783Seric 			printf("%s: %s\n", capitalize(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 */
32858737Seric 		if (!bitset(EF_QUEUERUN, e->e_flags) && h->h_value != NULL &&
32911299Seric 		    strcmp(h->h_field, "message-id") == 0)
33011290Seric 		{
33157208Seric 			msgid = h->h_value;
33211290Seric 		}
33357359Seric 
33457359Seric 		/* see if this is a return-receipt header */
33557359Seric 		if (bitset(H_RECEIPTTO, h->h_flags))
33657359Seric 			e->e_receiptto = h->h_value;
33757359Seric 
33857359Seric 		/* see if this is an errors-to header */
33957359Seric 		if (bitset(H_ERRORSTO, h->h_flags))
34058082Seric 			(void) sendtolist(h->h_value, (ADDRESS *) NULL,
34158082Seric 					  &e->e_errorqueue, e);
3429382Seric 	}
3439382Seric 	if (tTd(32, 1))
3447783Seric 		printf("----------------------------\n");
3457783Seric 
34658145Seric 	/* if we are just verifying (that is, sendmail -t -bv), drop out now */
34758145Seric 	if (OpMode == MD_VERIFY)
34858145Seric 		return;
34958145Seric 
3509382Seric 	/* store hop count */
3519382Seric 	if (hopcnt > e->e_hopcount)
3529382Seric 		e->e_hopcount = hopcnt;
3539382Seric 
3547783Seric 	/* message priority */
35555012Seric 	p = hvalue("precedence", e);
3569382Seric 	if (p != NULL)
3579382Seric 		e->e_class = priencode(p);
35858737Seric 	if (!bitset(EF_QUEUERUN, e->e_flags))
35925013Seric 		e->e_msgpriority = e->e_msgsize
36024981Seric 				 - e->e_class * WkClassFact
36124981Seric 				 + e->e_nrcpts * WkRecipFact;
3627783Seric 
3637783Seric 	/* full name of from person */
36455012Seric 	p = hvalue("full-name", e);
3657783Seric 	if (p != NULL)
3669382Seric 		define('x', p, e);
3677783Seric 
3687783Seric 	/* date message originated */
36955012Seric 	p = hvalue("posted-date", e);
3707783Seric 	if (p == NULL)
37155012Seric 		p = hvalue("date", e);
3727783Seric 	if (p != NULL)
3739382Seric 		define('a', p, e);
37411290Seric 
37511290Seric 	/*
37611290Seric 	**  Log collection information.
37711290Seric 	*/
37811290Seric 
37911290Seric # ifdef LOG
38058737Seric 	if (!bitset(EF_QUEUERUN, e->e_flags) && LogLevel > 4)
38111290Seric 	{
38257359Seric 		char *name;
38357232Seric 		char hbuf[MAXNAME];
38457359Seric 		char sbuf[MAXLINE];
38536230Skarels 
38658667Seric 		if (bitset(EF_RESPONSE, e->e_flags))
38758667Seric 			name = "[RESPONSE]";
38858667Seric 		else if (RealHostName[0] == '[')
38936230Skarels 			name = RealHostName;
39036230Skarels 		else
39157359Seric 		{
39258755Seric 			extern char *anynet_ntoa();
39357359Seric 
39457359Seric 			name = hbuf;
39554999Seric 			(void)sprintf(hbuf, "%.80s (%s)",
39658755Seric 			    RealHostName, anynet_ntoa(&RealHostAddr));
39757359Seric 		}
39857359Seric 
39957359Seric 		/* some versions of syslog only take 5 printf args */
40057589Seric 		sprintf(sbuf, "from=%.200s, size=%ld, class=%d, pri=%ld, nrcpts=%d, msgid=%.100s",
40158558Seric 		    e->e_from.q_paddr, e->e_msgsize, e->e_class,
40257589Seric 		    e->e_msgpriority, e->e_nrcpts, msgid);
40358686Seric 		syslog(LOG_INFO, "%s: %s, relay=%s",
40457359Seric 		    e->e_id, sbuf, name);
40511290Seric 	}
40656795Seric # endif /* LOG */
4077783Seric }
4087783Seric /*
4097783Seric **  PRIENCODE -- encode external priority names into internal values.
4107783Seric **
4117783Seric **	Parameters:
4127783Seric **		p -- priority in ascii.
4137783Seric **
4147783Seric **	Returns:
4157783Seric **		priority as a numeric level.
4167783Seric **
4177783Seric **	Side Effects:
4187783Seric **		none.
4197783Seric */
4207783Seric 
4217783Seric priencode(p)
4227783Seric 	char *p;
4237783Seric {
4248253Seric 	register int i;
4257783Seric 
4268253Seric 	for (i = 0; i < NumPriorities; i++)
4277783Seric 	{
42833725Sbostic 		if (!strcasecmp(p, Priorities[i].pri_name))
4298253Seric 			return (Priorities[i].pri_val);
4307783Seric 	}
4318253Seric 
4328253Seric 	/* unknown priority */
4338253Seric 	return (0);
4347783Seric }
4357783Seric /*
4367890Seric **  CRACKADDR -- parse an address and turn it into a macro
4377783Seric **
4387783Seric **	This doesn't actually parse the address -- it just extracts
4397783Seric **	it and replaces it with "$g".  The parse is totally ad hoc
4407783Seric **	and isn't even guaranteed to leave something syntactically
4417783Seric **	identical to what it started with.  However, it does leave
4427783Seric **	something semantically identical.
4437783Seric **
44451379Seric **	This algorithm has been cleaned up to handle a wider range
44551379Seric **	of cases -- notably quoted and backslash escaped strings.
44651379Seric **	This modification makes it substantially better at preserving
44751379Seric **	the original syntax.
4487783Seric **
4497783Seric **	Parameters:
4507890Seric **		addr -- the address to be cracked.
4517783Seric **
4527783Seric **	Returns:
4537783Seric **		a pointer to the new version.
4547783Seric **
4557783Seric **	Side Effects:
4567890Seric **		none.
4577783Seric **
4587783Seric **	Warning:
4597783Seric **		The return value is saved in local storage and should
4607783Seric **		be copied if it is to be reused.
4617783Seric */
4627783Seric 
4637783Seric char *
4647890Seric crackaddr(addr)
4657890Seric 	register char *addr;
4667783Seric {
4677783Seric 	register char *p;
46851379Seric 	register char c;
46951379Seric 	int cmtlev;
47056764Seric 	int realcmtlev;
47156764Seric 	int anglelev, realanglelev;
47251379Seric 	int copylev;
47351379Seric 	bool qmode;
47456764Seric 	bool realqmode;
47556764Seric 	bool skipping;
47651379Seric 	bool putgmac = FALSE;
47756735Seric 	bool quoteit = FALSE;
47851379Seric 	register char *bp;
47956764Seric 	char *buflim;
4807783Seric 	static char buf[MAXNAME];
4817783Seric 
4827783Seric 	if (tTd(33, 1))
4837890Seric 		printf("crackaddr(%s)\n", addr);
4847783Seric 
4858082Seric 	/* strip leading spaces */
48658050Seric 	while (*addr != '\0' && isascii(*addr) && isspace(*addr))
4878082Seric 		addr++;
4888082Seric 
4897783Seric 	/*
49051379Seric 	**  Start by assuming we have no angle brackets.  This will be
49151379Seric 	**  adjusted later if we find them.
4927783Seric 	*/
4937783Seric 
49451379Seric 	bp = buf;
49556764Seric 	buflim = &buf[sizeof buf - 5];
49651379Seric 	p = addr;
49756764Seric 	copylev = anglelev = realanglelev = cmtlev = realcmtlev = 0;
49856764Seric 	qmode = realqmode = FALSE;
49951379Seric 
50051379Seric 	while ((c = *p++) != '\0')
5017783Seric 	{
50256764Seric 		/*
50356764Seric 		**  If the buffer is overful, go into a special "skipping"
50456764Seric 		**  mode that tries to keep legal syntax but doesn't actually
50556764Seric 		**  output things.
50656764Seric 		*/
5077783Seric 
50856764Seric 		skipping = bp >= buflim;
50956735Seric 
51056764Seric 		if (copylev > 0 && !skipping)
51156764Seric 			*bp++ = c;
51256735Seric 
51351379Seric 		/* check for backslash escapes */
51451379Seric 		if (c == '\\')
5157783Seric 		{
51651379Seric 			if ((c = *p++) == '\0')
5177783Seric 			{
51851379Seric 				/* too far */
51951379Seric 				p--;
52051379Seric 				goto putg;
5217783Seric 			}
52256764Seric 			if (copylev > 0 && !skipping)
52351379Seric 				*bp++ = c;
52451379Seric 			goto putg;
5257783Seric 		}
5267783Seric 
52751379Seric 		/* check for quoted strings */
52851379Seric 		if (c == '"')
5297783Seric 		{
53051379Seric 			qmode = !qmode;
53156764Seric 			if (copylev > 0 && !skipping)
53256764Seric 				realqmode = !realqmode;
53351379Seric 			continue;
5347783Seric 		}
53551379Seric 		if (qmode)
53651379Seric 			goto putg;
5377783Seric 
53851379Seric 		/* check for comments */
53951379Seric 		if (c == '(')
5407783Seric 		{
54151379Seric 			cmtlev++;
54256764Seric 
54356764Seric 			/* allow space for closing paren */
54456764Seric 			if (!skipping)
54556764Seric 			{
54656764Seric 				buflim--;
54756764Seric 				realcmtlev++;
54856764Seric 				if (copylev++ <= 0)
54956764Seric 				{
55056764Seric 					*bp++ = ' ';
55156764Seric 					*bp++ = c;
55256764Seric 				}
55356764Seric 			}
55451379Seric 		}
55551379Seric 		if (cmtlev > 0)
55651379Seric 		{
55751379Seric 			if (c == ')')
5587783Seric 			{
55951379Seric 				cmtlev--;
56051379Seric 				copylev--;
56156764Seric 				if (!skipping)
56256764Seric 				{
56356764Seric 					realcmtlev--;
56456764Seric 					buflim++;
56556764Seric 				}
5667783Seric 			}
5677783Seric 			continue;
5687783Seric 		}
56956764Seric 		else if (c == ')')
57056764Seric 		{
57156764Seric 			/* syntax error: unmatched ) */
57256764Seric 			if (!skipping)
57356764Seric 				bp--;
57456764Seric 		}
5757783Seric 
57656764Seric 
57756764Seric 		/* check for characters that may have to be quoted */
57857175Seric 		if (strchr(".'@,;:\\()", c) != NULL)
57956764Seric 		{
58056764Seric 			/*
58156764Seric 			**  If these occur as the phrase part of a <>
58256764Seric 			**  construct, but are not inside of () or already
58356764Seric 			**  quoted, they will have to be quoted.  Note that
58456764Seric 			**  now (but don't actually do the quoting).
58556764Seric 			*/
58656764Seric 
58756764Seric 			if (cmtlev <= 0 && !qmode)
58856764Seric 				quoteit = TRUE;
58956764Seric 		}
59056764Seric 
59151379Seric 		/* check for angle brackets */
59251379Seric 		if (c == '<')
59351379Seric 		{
59456735Seric 			register char *q;
59556735Seric 
59651379Seric 			/* oops -- have to change our mind */
59756764Seric 			anglelev++;
59856764Seric 			if (!skipping)
59956764Seric 				realanglelev++;
60056764Seric 
60156735Seric 			bp = buf;
60256735Seric 			if (quoteit)
60356735Seric 			{
60456735Seric 				*bp++ = '"';
60556735Seric 
60656735Seric 				/* back up over the '<' and any spaces */
60756735Seric 				--p;
60858050Seric 				while (isascii(*--p) && isspace(*p))
60956735Seric 					continue;
61056735Seric 				p++;
61156735Seric 			}
61256735Seric 			for (q = addr; q < p; )
61356735Seric 			{
61456735Seric 				c = *q++;
61556764Seric 				if (bp < buflim)
61656764Seric 				{
61756764Seric 					if (quoteit && c == '"')
61856764Seric 						*bp++ = '\\';
61956764Seric 					*bp++ = c;
62056764Seric 				}
62156735Seric 			}
62256735Seric 			if (quoteit)
62356735Seric 			{
62456735Seric 				*bp++ = '"';
62556764Seric 				while ((c = *p++) != '<')
62656764Seric 				{
62756764Seric 					if (bp < buflim)
62856764Seric 						*bp++ = c;
62956764Seric 				}
63056764Seric 				*bp++ = c;
63156735Seric 			}
63251379Seric 			copylev = 0;
63356735Seric 			putgmac = quoteit = FALSE;
63451379Seric 			continue;
63551379Seric 		}
6367783Seric 
63751379Seric 		if (c == '>')
6387783Seric 		{
63956764Seric 			if (anglelev > 0)
64056764Seric 			{
64156764Seric 				anglelev--;
64256764Seric 				if (!skipping)
64356764Seric 				{
64456764Seric 					realanglelev--;
64556764Seric 					buflim++;
64656764Seric 				}
64756764Seric 			}
64856764Seric 			else if (!skipping)
64956764Seric 			{
65056764Seric 				/* syntax error: unmatched > */
65156764Seric 				if (copylev > 0)
65256764Seric 					bp--;
65356764Seric 				continue;
65456764Seric 			}
65551379Seric 			if (copylev++ <= 0)
65651379Seric 				*bp++ = c;
65751379Seric 			continue;
6587783Seric 		}
65951379Seric 
66051379Seric 		/* must be a real address character */
66151379Seric 	putg:
66251379Seric 		if (copylev <= 0 && !putgmac)
66351379Seric 		{
66458050Seric 			*bp++ = MACROEXPAND;
66551379Seric 			*bp++ = 'g';
66651379Seric 			putgmac = TRUE;
66751379Seric 		}
6687783Seric 	}
6697783Seric 
67056764Seric 	/* repair any syntactic damage */
67156764Seric 	if (realqmode)
67256764Seric 		*bp++ = '"';
67356764Seric 	while (realcmtlev-- > 0)
67456764Seric 		*bp++ = ')';
67556764Seric 	while (realanglelev-- > 0)
67656764Seric 		*bp++ = '>';
67751379Seric 	*bp++ = '\0';
6787783Seric 
6797783Seric 	if (tTd(33, 1))
6807944Seric 		printf("crackaddr=>`%s'\n", buf);
6817783Seric 
6827783Seric 	return (buf);
6837783Seric }
6849382Seric /*
6859382Seric **  PUTHEADER -- put the header part of a message from the in-core copy
6869382Seric **
6879382Seric **	Parameters:
6889382Seric **		fp -- file to put it on.
6899382Seric **		m -- mailer to use.
6909382Seric **		e -- envelope to use.
6919382Seric **
6929382Seric **	Returns:
6939382Seric **		none.
6949382Seric **
6959382Seric **	Side Effects:
6969382Seric **		none.
6979382Seric */
6989382Seric 
69957589Seric /*
70057589Seric  * Macro for fast max (not available in e.g. DG/UX, 386/ix).
70157589Seric  */
70257589Seric #ifndef MAX
70357589Seric # define MAX(a,b) (((a)>(b))?(a):(b))
70457589Seric #endif
70557589Seric 
70610176Seric putheader(fp, m, e)
7079382Seric 	register FILE *fp;
7089382Seric 	register MAILER *m;
7099382Seric 	register ENVELOPE *e;
7109382Seric {
71157135Seric 	char buf[MAX(MAXLINE,BUFSIZ)];
7129382Seric 	register HDR *h;
71357135Seric 	char obuf[MAXLINE];
7149382Seric 
7159382Seric 	for (h = e->e_header; h != NULL; h = h->h_link)
7169382Seric 	{
7179382Seric 		register char *p;
71810689Seric 		extern bool bitintersect();
7199382Seric 
7209382Seric 		if (bitset(H_CHECK|H_ACHECK, h->h_flags) &&
72110689Seric 		    !bitintersect(h->h_mflags, m->m_flags))
7229382Seric 			continue;
7239382Seric 
72411414Seric 		/* handle Resent-... headers specially */
72511414Seric 		if (bitset(H_RESENT, h->h_flags) && !bitset(EF_RESENT, e->e_flags))
72611414Seric 			continue;
72711414Seric 
7289382Seric 		p = h->h_value;
7299382Seric 		if (bitset(H_DEFAULT, h->h_flags))
7309382Seric 		{
7319382Seric 			/* macro expand value if generated internally */
7329382Seric 			expand(p, buf, &buf[sizeof buf], e);
7339382Seric 			p = buf;
7349382Seric 			if (p == NULL || *p == '\0')
7359382Seric 				continue;
7369382Seric 		}
7379382Seric 
7389382Seric 		if (bitset(H_FROM|H_RCPT, h->h_flags))
7399382Seric 		{
7409382Seric 			/* address field */
7419382Seric 			bool oldstyle = bitset(EF_OLDSTYLE, e->e_flags);
7429382Seric 
7439382Seric 			if (bitset(H_FROM, h->h_flags))
7449382Seric 				oldstyle = FALSE;
74555012Seric 			commaize(h, p, fp, oldstyle, m, e);
7469382Seric 		}
7479382Seric 		else
7489382Seric 		{
7499382Seric 			/* vanilla header line */
75012159Seric 			register char *nlp;
75157642Seric 			extern char *capitalize();
75212159Seric 
75312159Seric 			(void) sprintf(obuf, "%s: ", capitalize(h->h_field));
75456795Seric 			while ((nlp = strchr(p, '\n')) != NULL)
75512159Seric 			{
75612159Seric 				*nlp = '\0';
75712159Seric 				(void) strcat(obuf, p);
75812159Seric 				*nlp = '\n';
75912159Seric 				putline(obuf, fp, m);
76012159Seric 				p = ++nlp;
76112161Seric 				obuf[0] = '\0';
76212159Seric 			}
76312159Seric 			(void) strcat(obuf, p);
76410176Seric 			putline(obuf, fp, m);
7659382Seric 		}
7669382Seric 	}
7679382Seric }
7689382Seric /*
7699382Seric **  COMMAIZE -- output a header field, making a comma-translated list.
7709382Seric **
7719382Seric **	Parameters:
7729382Seric **		h -- the header field to output.
7739382Seric **		p -- the value to put in it.
7749382Seric **		fp -- file to put it to.
7759382Seric **		oldstyle -- TRUE if this is an old style header.
7769382Seric **		m -- a pointer to the mailer descriptor.  If NULL,
7779382Seric **			don't transform the name at all.
77855012Seric **		e -- the envelope containing the message.
7799382Seric **
7809382Seric **	Returns:
7819382Seric **		none.
7829382Seric **
7839382Seric **	Side Effects:
7849382Seric **		outputs "p" to file "fp".
7859382Seric */
7869382Seric 
78755012Seric commaize(h, p, fp, oldstyle, m, e)
7889382Seric 	register HDR *h;
7899382Seric 	register char *p;
7909382Seric 	FILE *fp;
7919382Seric 	bool oldstyle;
7929382Seric 	register MAILER *m;
79355012Seric 	register ENVELOPE *e;
7949382Seric {
7959382Seric 	register char *obp;
7969382Seric 	int opos;
7979382Seric 	bool firstone = TRUE;
79811157Seric 	char obuf[MAXLINE + 3];
79957642Seric 	extern char *capitalize();
8009382Seric 
8019382Seric 	/*
8029382Seric 	**  Output the address list translated by the
8039382Seric 	**  mailer and with commas.
8049382Seric 	*/
8059382Seric 
8069382Seric 	if (tTd(14, 2))
8079382Seric 		printf("commaize(%s: %s)\n", h->h_field, p);
8089382Seric 
8099382Seric 	obp = obuf;
8109382Seric 	(void) sprintf(obp, "%s: ", capitalize(h->h_field));
8119382Seric 	opos = strlen(h->h_field) + 2;
8129382Seric 	obp += opos;
8139382Seric 
8149382Seric 	/*
8159382Seric 	**  Run through the list of values.
8169382Seric 	*/
8179382Seric 
8189382Seric 	while (*p != '\0')
8199382Seric 	{
8209382Seric 		register char *name;
82154983Seric 		register int c;
8229382Seric 		char savechar;
8239382Seric 		extern char *remotename();
8249382Seric 
8259382Seric 		/*
8269382Seric 		**  Find the end of the name.  New style names
8279382Seric 		**  end with a comma, old style names end with
8289382Seric 		**  a space character.  However, spaces do not
8299382Seric 		**  necessarily delimit an old-style name -- at
8309382Seric 		**  signs mean keep going.
8319382Seric 		*/
8329382Seric 
8339382Seric 		/* find end of name */
83458050Seric 		while ((isascii(*p) && isspace(*p)) || *p == ',')
8359382Seric 			p++;
8369382Seric 		name = p;
8379382Seric 		for (;;)
8389382Seric 		{
83958333Seric 			auto char *oldp;
84016909Seric 			char pvpbuf[PSBUFSIZE];
8419382Seric 			extern char **prescan();
8429382Seric 
84358333Seric 			(void) prescan(p, oldstyle ? ' ' : ',', pvpbuf, &oldp);
84458333Seric 			p = oldp;
8459382Seric 
8469382Seric 			/* look to see if we have an at sign */
84758050Seric 			while (*p != '\0' && isascii(*p) && isspace(*p))
8489382Seric 				p++;
8499382Seric 
85058170Seric 			if (*p != '@')
8519382Seric 			{
8529382Seric 				p = oldp;
8539382Seric 				break;
8549382Seric 			}
8559382Seric 			p += *p == '@' ? 1 : 2;
85658050Seric 			while (*p != '\0' && isascii(*p) && isspace(*p))
8579382Seric 				p++;
8589382Seric 		}
8599382Seric 		/* at the end of one complete name */
8609382Seric 
8619382Seric 		/* strip off trailing white space */
86258050Seric 		while (p >= name &&
86358050Seric 		       ((isascii(*p) && isspace(*p)) || *p == ',' || *p == '\0'))
8649382Seric 			p--;
8659382Seric 		if (++p == name)
8669382Seric 			continue;
8679382Seric 		savechar = *p;
8689382Seric 		*p = '\0';
8699382Seric 
8709382Seric 		/* translate the name to be relative */
87158020Seric 		name = remotename(name, m, bitset(H_FROM, h->h_flags),
87258509Seric 				  TRUE, FALSE, TRUE, e);
8739382Seric 		if (*name == '\0')
8749382Seric 		{
8759382Seric 			*p = savechar;
8769382Seric 			continue;
8779382Seric 		}
8789382Seric 
8799382Seric 		/* output the name with nice formatting */
88054983Seric 		opos += strlen(name);
8819382Seric 		if (!firstone)
8829382Seric 			opos += 2;
8839382Seric 		if (opos > 78 && !firstone)
8849382Seric 		{
88510178Seric 			(void) strcpy(obp, ",\n");
88610176Seric 			putline(obuf, fp, m);
8879382Seric 			obp = obuf;
8889382Seric 			(void) sprintf(obp, "        ");
88910161Seric 			opos = strlen(obp);
89010161Seric 			obp += opos;
89154983Seric 			opos += strlen(name);
8929382Seric 		}
8939382Seric 		else if (!firstone)
8949382Seric 		{
8959382Seric 			(void) sprintf(obp, ", ");
8969382Seric 			obp += 2;
8979382Seric 		}
8989382Seric 
8999382Seric 		/* strip off quote bits as we output */
90054983Seric 		while ((c = *name++) != '\0' && obp < &obuf[MAXLINE])
9019382Seric 		{
90254983Seric 			if (bitnset(M_7BITS, m->m_flags))
90354983Seric 				c &= 0177;
90454983Seric 			*obp++ = c;
9059382Seric 		}
9069382Seric 		firstone = FALSE;
9079382Seric 		*p = savechar;
9089382Seric 	}
9099382Seric 	(void) strcpy(obp, "\n");
91010176Seric 	putline(obuf, fp, m);
9119382Seric }
9129382Seric /*
91358170Seric **  COPYHEADER -- copy header list
9149382Seric **
91558170Seric **	This routine is the equivalent of newstr for header lists
91658170Seric **
9179382Seric **	Parameters:
91858170Seric **		header -- list of header structures to copy.
9199382Seric **
9209382Seric **	Returns:
92158170Seric **		a copy of 'header'.
9229382Seric **
9239382Seric **	Side Effects:
9249382Seric **		none.
9259382Seric */
9269382Seric 
92758170Seric HDR *
92858170Seric copyheader(header)
92958170Seric 	register HDR *header;
9309382Seric {
93158170Seric 	register HDR *newhdr;
93258170Seric 	HDR *ret;
93358170Seric 	register HDR **tail = &ret;
9349382Seric 
93558170Seric 	while (header != NULL)
93658170Seric 	{
93758170Seric 		newhdr = (HDR *) xalloc(sizeof(HDR));
93858170Seric 		STRUCTCOPY(*header, *newhdr);
93958170Seric 		*tail = newhdr;
94058170Seric 		tail = &newhdr->h_link;
94158170Seric 		header = header->h_link;
94258170Seric 	}
94358170Seric 	*tail = NULL;
94458170Seric 
94558170Seric 	return ret;
9469382Seric }
947