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*58929Seric static char sccsid[] = "@(#)headers.c	6.28 (Berkeley) 04/01/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:
15658789Seric **		field -- the name of the header field.  It must be
15758789Seric **			lower-cased.
15858789Seric **		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.
267*58929Seric **		full -- if set, do full processing (e.g., compute
268*58929Seric **			message priority).
2697783Seric **
2707783Seric **	Returns:
2717783Seric **		none.
2727783Seric **
2737783Seric **	Side Effects:
2747783Seric **		Sets a bunch of global variables from information
2759382Seric **			in the collected header.
2769382Seric **		Aborts the message if the hop count is exceeded.
2777783Seric */
2787783Seric 
279*58929Seric eatheader(e, full)
2809382Seric 	register ENVELOPE *e;
281*58929Seric 	bool full;
2827783Seric {
2837783Seric 	register HDR *h;
2847783Seric 	register char *p;
2859382Seric 	int hopcnt = 0;
28657208Seric 	char *msgid;
28758688Seric 	char buf[MAXLINE];
2887783Seric 
28958688Seric 	/*
29058688Seric 	**  Set up macros for possible expansion in headers.
29158688Seric 	*/
29258688Seric 
29358704Seric 	define('f', e->e_sender, e);
29458704Seric 	define('g', e->e_sender, e);
29558688Seric 
2969382Seric 	if (tTd(32, 1))
2979382Seric 		printf("----- collected header -----\n");
29857208Seric 	msgid = "<none>";
2999382Seric 	for (h = e->e_header; h != NULL; h = h->h_link)
3007783Seric 	{
3017783Seric 		extern char *capitalize();
3027783Seric 
30358688Seric 		/* do early binding */
30458688Seric 		if (bitset(H_DEFAULT, h->h_flags) && h->h_value != NULL)
30558688Seric 		{
30658688Seric 			expand(h->h_value, buf, &buf[sizeof buf], e);
30758688Seric 			if (buf[0] != '\0')
30858688Seric 			{
30958688Seric 				h->h_value = newstr(buf);
31058688Seric 				h->h_flags &= ~H_DEFAULT;
31158688Seric 			}
31258688Seric 		}
31358688Seric 
3149382Seric 		if (tTd(32, 1))
3157783Seric 			printf("%s: %s\n", capitalize(h->h_field), h->h_value);
31657359Seric 
31711414Seric 		/* count the number of times it has been processed */
3189382Seric 		if (bitset(H_TRACE, h->h_flags))
3199382Seric 			hopcnt++;
32011414Seric 
32111414Seric 		/* send to this person if we so desire */
32211414Seric 		if (GrabTo && bitset(H_RCPT, h->h_flags) &&
32311414Seric 		    !bitset(H_DEFAULT, h->h_flags) &&
32455012Seric 		    (!bitset(EF_RESENT, e->e_flags) || bitset(H_RESENT, h->h_flags)))
32511414Seric 		{
32658082Seric 			(void) sendtolist(h->h_value, (ADDRESS *) NULL,
32758082Seric 					  &e->e_sendqueue, e);
32811414Seric 		}
32911414Seric 
33057208Seric 		/* save the message-id for logging */
331*58929Seric 		if (full && h->h_value != NULL &&
33211299Seric 		    strcmp(h->h_field, "message-id") == 0)
33311290Seric 		{
33457208Seric 			msgid = h->h_value;
33511290Seric 		}
33657359Seric 
33757359Seric 		/* see if this is a return-receipt header */
33857359Seric 		if (bitset(H_RECEIPTTO, h->h_flags))
33957359Seric 			e->e_receiptto = h->h_value;
34057359Seric 
34157359Seric 		/* see if this is an errors-to header */
34257359Seric 		if (bitset(H_ERRORSTO, h->h_flags))
34358082Seric 			(void) sendtolist(h->h_value, (ADDRESS *) NULL,
34458082Seric 					  &e->e_errorqueue, e);
3459382Seric 	}
3469382Seric 	if (tTd(32, 1))
3477783Seric 		printf("----------------------------\n");
3487783Seric 
34958145Seric 	/* if we are just verifying (that is, sendmail -t -bv), drop out now */
35058145Seric 	if (OpMode == MD_VERIFY)
35158145Seric 		return;
35258145Seric 
3539382Seric 	/* store hop count */
3549382Seric 	if (hopcnt > e->e_hopcount)
3559382Seric 		e->e_hopcount = hopcnt;
3569382Seric 
3577783Seric 	/* message priority */
35855012Seric 	p = hvalue("precedence", e);
3599382Seric 	if (p != NULL)
3609382Seric 		e->e_class = priencode(p);
361*58929Seric 	if (full)
36225013Seric 		e->e_msgpriority = e->e_msgsize
36324981Seric 				 - e->e_class * WkClassFact
36424981Seric 				 + e->e_nrcpts * WkRecipFact;
3657783Seric 
3667783Seric 	/* full name of from person */
36755012Seric 	p = hvalue("full-name", e);
3687783Seric 	if (p != NULL)
3699382Seric 		define('x', p, e);
3707783Seric 
3717783Seric 	/* date message originated */
37255012Seric 	p = hvalue("posted-date", e);
3737783Seric 	if (p == NULL)
37455012Seric 		p = hvalue("date", e);
3757783Seric 	if (p != NULL)
3769382Seric 		define('a', p, e);
37711290Seric 
37811290Seric 	/*
37911290Seric 	**  Log collection information.
38011290Seric 	*/
38111290Seric 
38211290Seric # ifdef LOG
383*58929Seric 	if (full && LogLevel > 4)
38411290Seric 	{
38557359Seric 		char *name;
38657232Seric 		char hbuf[MAXNAME];
38757359Seric 		char sbuf[MAXLINE];
38836230Skarels 
38958667Seric 		if (bitset(EF_RESPONSE, e->e_flags))
39058667Seric 			name = "[RESPONSE]";
39158667Seric 		else if (RealHostName[0] == '[')
39236230Skarels 			name = RealHostName;
39336230Skarels 		else
39457359Seric 		{
39558755Seric 			extern char *anynet_ntoa();
39657359Seric 
39757359Seric 			name = hbuf;
39858798Seric 			(void) sprintf(hbuf, "%.80s", RealHostName);
39958798Seric 			if (RealHostAddr.sa.sa_family != 0)
40058798Seric 			{
40158798Seric 				p = &hbuf[strlen(hbuf)];
40258798Seric 				(void) sprintf(p, " (%s)",
40358798Seric 					anynet_ntoa(&RealHostAddr));
40458798Seric 			}
40557359Seric 		}
40657359Seric 
40757359Seric 		/* some versions of syslog only take 5 printf args */
40857589Seric 		sprintf(sbuf, "from=%.200s, size=%ld, class=%d, pri=%ld, nrcpts=%d, msgid=%.100s",
40958558Seric 		    e->e_from.q_paddr, e->e_msgsize, e->e_class,
41057589Seric 		    e->e_msgpriority, e->e_nrcpts, msgid);
41158686Seric 		syslog(LOG_INFO, "%s: %s, relay=%s",
41257359Seric 		    e->e_id, sbuf, name);
41311290Seric 	}
41456795Seric # endif /* LOG */
4157783Seric }
4167783Seric /*
4177783Seric **  PRIENCODE -- encode external priority names into internal values.
4187783Seric **
4197783Seric **	Parameters:
4207783Seric **		p -- priority in ascii.
4217783Seric **
4227783Seric **	Returns:
4237783Seric **		priority as a numeric level.
4247783Seric **
4257783Seric **	Side Effects:
4267783Seric **		none.
4277783Seric */
4287783Seric 
4297783Seric priencode(p)
4307783Seric 	char *p;
4317783Seric {
4328253Seric 	register int i;
4337783Seric 
4348253Seric 	for (i = 0; i < NumPriorities; i++)
4357783Seric 	{
43633725Sbostic 		if (!strcasecmp(p, Priorities[i].pri_name))
4378253Seric 			return (Priorities[i].pri_val);
4387783Seric 	}
4398253Seric 
4408253Seric 	/* unknown priority */
4418253Seric 	return (0);
4427783Seric }
4437783Seric /*
4447890Seric **  CRACKADDR -- parse an address and turn it into a macro
4457783Seric **
4467783Seric **	This doesn't actually parse the address -- it just extracts
4477783Seric **	it and replaces it with "$g".  The parse is totally ad hoc
4487783Seric **	and isn't even guaranteed to leave something syntactically
4497783Seric **	identical to what it started with.  However, it does leave
4507783Seric **	something semantically identical.
4517783Seric **
45251379Seric **	This algorithm has been cleaned up to handle a wider range
45351379Seric **	of cases -- notably quoted and backslash escaped strings.
45451379Seric **	This modification makes it substantially better at preserving
45551379Seric **	the original syntax.
4567783Seric **
4577783Seric **	Parameters:
4587890Seric **		addr -- the address to be cracked.
4597783Seric **
4607783Seric **	Returns:
4617783Seric **		a pointer to the new version.
4627783Seric **
4637783Seric **	Side Effects:
4647890Seric **		none.
4657783Seric **
4667783Seric **	Warning:
4677783Seric **		The return value is saved in local storage and should
4687783Seric **		be copied if it is to be reused.
4697783Seric */
4707783Seric 
4717783Seric char *
4727890Seric crackaddr(addr)
4737890Seric 	register char *addr;
4747783Seric {
4757783Seric 	register char *p;
47651379Seric 	register char c;
47751379Seric 	int cmtlev;
47856764Seric 	int realcmtlev;
47956764Seric 	int anglelev, realanglelev;
48051379Seric 	int copylev;
48151379Seric 	bool qmode;
48256764Seric 	bool realqmode;
48356764Seric 	bool skipping;
48451379Seric 	bool putgmac = FALSE;
48556735Seric 	bool quoteit = FALSE;
48651379Seric 	register char *bp;
48756764Seric 	char *buflim;
4887783Seric 	static char buf[MAXNAME];
4897783Seric 
4907783Seric 	if (tTd(33, 1))
4917890Seric 		printf("crackaddr(%s)\n", addr);
4927783Seric 
4938082Seric 	/* strip leading spaces */
49458050Seric 	while (*addr != '\0' && isascii(*addr) && isspace(*addr))
4958082Seric 		addr++;
4968082Seric 
4977783Seric 	/*
49851379Seric 	**  Start by assuming we have no angle brackets.  This will be
49951379Seric 	**  adjusted later if we find them.
5007783Seric 	*/
5017783Seric 
50251379Seric 	bp = buf;
50356764Seric 	buflim = &buf[sizeof buf - 5];
50451379Seric 	p = addr;
50556764Seric 	copylev = anglelev = realanglelev = cmtlev = realcmtlev = 0;
50656764Seric 	qmode = realqmode = FALSE;
50751379Seric 
50851379Seric 	while ((c = *p++) != '\0')
5097783Seric 	{
51056764Seric 		/*
51156764Seric 		**  If the buffer is overful, go into a special "skipping"
51256764Seric 		**  mode that tries to keep legal syntax but doesn't actually
51356764Seric 		**  output things.
51456764Seric 		*/
5157783Seric 
51656764Seric 		skipping = bp >= buflim;
51756735Seric 
51856764Seric 		if (copylev > 0 && !skipping)
51956764Seric 			*bp++ = c;
52056735Seric 
52151379Seric 		/* check for backslash escapes */
52251379Seric 		if (c == '\\')
5237783Seric 		{
52458890Seric 			/* arrange to quote the address */
52558890Seric 			if (cmtlev <= 0 && !qmode)
52658890Seric 				quoteit = TRUE;
52758890Seric 
52851379Seric 			if ((c = *p++) == '\0')
5297783Seric 			{
53051379Seric 				/* too far */
53151379Seric 				p--;
53251379Seric 				goto putg;
5337783Seric 			}
53456764Seric 			if (copylev > 0 && !skipping)
53551379Seric 				*bp++ = c;
53651379Seric 			goto putg;
5377783Seric 		}
5387783Seric 
53951379Seric 		/* check for quoted strings */
54051379Seric 		if (c == '"')
5417783Seric 		{
54251379Seric 			qmode = !qmode;
54356764Seric 			if (copylev > 0 && !skipping)
54456764Seric 				realqmode = !realqmode;
54551379Seric 			continue;
5467783Seric 		}
54751379Seric 		if (qmode)
54851379Seric 			goto putg;
5497783Seric 
55051379Seric 		/* check for comments */
55151379Seric 		if (c == '(')
5527783Seric 		{
55351379Seric 			cmtlev++;
55456764Seric 
55556764Seric 			/* allow space for closing paren */
55656764Seric 			if (!skipping)
55756764Seric 			{
55856764Seric 				buflim--;
55956764Seric 				realcmtlev++;
56056764Seric 				if (copylev++ <= 0)
56156764Seric 				{
56256764Seric 					*bp++ = ' ';
56356764Seric 					*bp++ = c;
56456764Seric 				}
56556764Seric 			}
56651379Seric 		}
56751379Seric 		if (cmtlev > 0)
56851379Seric 		{
56951379Seric 			if (c == ')')
5707783Seric 			{
57151379Seric 				cmtlev--;
57251379Seric 				copylev--;
57356764Seric 				if (!skipping)
57456764Seric 				{
57556764Seric 					realcmtlev--;
57656764Seric 					buflim++;
57756764Seric 				}
5787783Seric 			}
5797783Seric 			continue;
5807783Seric 		}
58156764Seric 		else if (c == ')')
58256764Seric 		{
58356764Seric 			/* syntax error: unmatched ) */
58456764Seric 			if (!skipping)
58556764Seric 				bp--;
58656764Seric 		}
5877783Seric 
58856764Seric 
58956764Seric 		/* check for characters that may have to be quoted */
59057175Seric 		if (strchr(".'@,;:\\()", c) != NULL)
59156764Seric 		{
59256764Seric 			/*
59356764Seric 			**  If these occur as the phrase part of a <>
59456764Seric 			**  construct, but are not inside of () or already
59556764Seric 			**  quoted, they will have to be quoted.  Note that
59656764Seric 			**  now (but don't actually do the quoting).
59756764Seric 			*/
59856764Seric 
59956764Seric 			if (cmtlev <= 0 && !qmode)
60056764Seric 				quoteit = TRUE;
60156764Seric 		}
60256764Seric 
60351379Seric 		/* check for angle brackets */
60451379Seric 		if (c == '<')
60551379Seric 		{
60656735Seric 			register char *q;
60756735Seric 
60851379Seric 			/* oops -- have to change our mind */
60956764Seric 			anglelev++;
61056764Seric 			if (!skipping)
61156764Seric 				realanglelev++;
61256764Seric 
61356735Seric 			bp = buf;
61456735Seric 			if (quoteit)
61556735Seric 			{
61656735Seric 				*bp++ = '"';
61756735Seric 
61856735Seric 				/* back up over the '<' and any spaces */
61956735Seric 				--p;
62058050Seric 				while (isascii(*--p) && isspace(*p))
62156735Seric 					continue;
62256735Seric 				p++;
62356735Seric 			}
62456735Seric 			for (q = addr; q < p; )
62556735Seric 			{
62656735Seric 				c = *q++;
62756764Seric 				if (bp < buflim)
62856764Seric 				{
62956764Seric 					if (quoteit && c == '"')
63056764Seric 						*bp++ = '\\';
63156764Seric 					*bp++ = c;
63256764Seric 				}
63356735Seric 			}
63456735Seric 			if (quoteit)
63556735Seric 			{
63656735Seric 				*bp++ = '"';
63756764Seric 				while ((c = *p++) != '<')
63856764Seric 				{
63956764Seric 					if (bp < buflim)
64056764Seric 						*bp++ = c;
64156764Seric 				}
64256764Seric 				*bp++ = c;
64356735Seric 			}
64451379Seric 			copylev = 0;
64556735Seric 			putgmac = quoteit = FALSE;
64651379Seric 			continue;
64751379Seric 		}
6487783Seric 
64951379Seric 		if (c == '>')
6507783Seric 		{
65156764Seric 			if (anglelev > 0)
65256764Seric 			{
65356764Seric 				anglelev--;
65456764Seric 				if (!skipping)
65556764Seric 				{
65656764Seric 					realanglelev--;
65756764Seric 					buflim++;
65856764Seric 				}
65956764Seric 			}
66056764Seric 			else if (!skipping)
66156764Seric 			{
66256764Seric 				/* syntax error: unmatched > */
66356764Seric 				if (copylev > 0)
66456764Seric 					bp--;
66556764Seric 				continue;
66656764Seric 			}
66751379Seric 			if (copylev++ <= 0)
66851379Seric 				*bp++ = c;
66951379Seric 			continue;
6707783Seric 		}
67151379Seric 
67251379Seric 		/* must be a real address character */
67351379Seric 	putg:
67451379Seric 		if (copylev <= 0 && !putgmac)
67551379Seric 		{
67658050Seric 			*bp++ = MACROEXPAND;
67751379Seric 			*bp++ = 'g';
67851379Seric 			putgmac = TRUE;
67951379Seric 		}
6807783Seric 	}
6817783Seric 
68256764Seric 	/* repair any syntactic damage */
68356764Seric 	if (realqmode)
68456764Seric 		*bp++ = '"';
68556764Seric 	while (realcmtlev-- > 0)
68656764Seric 		*bp++ = ')';
68756764Seric 	while (realanglelev-- > 0)
68856764Seric 		*bp++ = '>';
68951379Seric 	*bp++ = '\0';
6907783Seric 
6917783Seric 	if (tTd(33, 1))
6927944Seric 		printf("crackaddr=>`%s'\n", buf);
6937783Seric 
6947783Seric 	return (buf);
6957783Seric }
6969382Seric /*
6979382Seric **  PUTHEADER -- put the header part of a message from the in-core copy
6989382Seric **
6999382Seric **	Parameters:
7009382Seric **		fp -- file to put it on.
7019382Seric **		m -- mailer to use.
7029382Seric **		e -- envelope to use.
7039382Seric **
7049382Seric **	Returns:
7059382Seric **		none.
7069382Seric **
7079382Seric **	Side Effects:
7089382Seric **		none.
7099382Seric */
7109382Seric 
71157589Seric /*
71257589Seric  * Macro for fast max (not available in e.g. DG/UX, 386/ix).
71357589Seric  */
71457589Seric #ifndef MAX
71557589Seric # define MAX(a,b) (((a)>(b))?(a):(b))
71657589Seric #endif
71757589Seric 
71810176Seric putheader(fp, m, e)
7199382Seric 	register FILE *fp;
7209382Seric 	register MAILER *m;
7219382Seric 	register ENVELOPE *e;
7229382Seric {
72357135Seric 	char buf[MAX(MAXLINE,BUFSIZ)];
7249382Seric 	register HDR *h;
72557135Seric 	char obuf[MAXLINE];
7269382Seric 
7279382Seric 	for (h = e->e_header; h != NULL; h = h->h_link)
7289382Seric 	{
7299382Seric 		register char *p;
73010689Seric 		extern bool bitintersect();
7319382Seric 
7329382Seric 		if (bitset(H_CHECK|H_ACHECK, h->h_flags) &&
73310689Seric 		    !bitintersect(h->h_mflags, m->m_flags))
7349382Seric 			continue;
7359382Seric 
73611414Seric 		/* handle Resent-... headers specially */
73711414Seric 		if (bitset(H_RESENT, h->h_flags) && !bitset(EF_RESENT, e->e_flags))
73811414Seric 			continue;
73911414Seric 
7409382Seric 		p = h->h_value;
7419382Seric 		if (bitset(H_DEFAULT, h->h_flags))
7429382Seric 		{
7439382Seric 			/* macro expand value if generated internally */
7449382Seric 			expand(p, buf, &buf[sizeof buf], e);
7459382Seric 			p = buf;
7469382Seric 			if (p == NULL || *p == '\0')
7479382Seric 				continue;
7489382Seric 		}
7499382Seric 
7509382Seric 		if (bitset(H_FROM|H_RCPT, h->h_flags))
7519382Seric 		{
7529382Seric 			/* address field */
7539382Seric 			bool oldstyle = bitset(EF_OLDSTYLE, e->e_flags);
7549382Seric 
7559382Seric 			if (bitset(H_FROM, h->h_flags))
7569382Seric 				oldstyle = FALSE;
75755012Seric 			commaize(h, p, fp, oldstyle, m, e);
7589382Seric 		}
7599382Seric 		else
7609382Seric 		{
7619382Seric 			/* vanilla header line */
76212159Seric 			register char *nlp;
76357642Seric 			extern char *capitalize();
76412159Seric 
76512159Seric 			(void) sprintf(obuf, "%s: ", capitalize(h->h_field));
76656795Seric 			while ((nlp = strchr(p, '\n')) != NULL)
76712159Seric 			{
76812159Seric 				*nlp = '\0';
76912159Seric 				(void) strcat(obuf, p);
77012159Seric 				*nlp = '\n';
77112159Seric 				putline(obuf, fp, m);
77212159Seric 				p = ++nlp;
77312161Seric 				obuf[0] = '\0';
77412159Seric 			}
77512159Seric 			(void) strcat(obuf, p);
77610176Seric 			putline(obuf, fp, m);
7779382Seric 		}
7789382Seric 	}
7799382Seric }
7809382Seric /*
7819382Seric **  COMMAIZE -- output a header field, making a comma-translated list.
7829382Seric **
7839382Seric **	Parameters:
7849382Seric **		h -- the header field to output.
7859382Seric **		p -- the value to put in it.
7869382Seric **		fp -- file to put it to.
7879382Seric **		oldstyle -- TRUE if this is an old style header.
7889382Seric **		m -- a pointer to the mailer descriptor.  If NULL,
7899382Seric **			don't transform the name at all.
79055012Seric **		e -- the envelope containing the message.
7919382Seric **
7929382Seric **	Returns:
7939382Seric **		none.
7949382Seric **
7959382Seric **	Side Effects:
7969382Seric **		outputs "p" to file "fp".
7979382Seric */
7989382Seric 
79955012Seric commaize(h, p, fp, oldstyle, m, e)
8009382Seric 	register HDR *h;
8019382Seric 	register char *p;
8029382Seric 	FILE *fp;
8039382Seric 	bool oldstyle;
8049382Seric 	register MAILER *m;
80555012Seric 	register ENVELOPE *e;
8069382Seric {
8079382Seric 	register char *obp;
8089382Seric 	int opos;
8099382Seric 	bool firstone = TRUE;
81011157Seric 	char obuf[MAXLINE + 3];
81157642Seric 	extern char *capitalize();
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;
8229382Seric 	(void) sprintf(obp, "%s: ", capitalize(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;
8359382Seric 		extern char *remotename();
8369382Seric 
8379382Seric 		/*
8389382Seric 		**  Find the end of the name.  New style names
8399382Seric 		**  end with a comma, old style names end with
8409382Seric 		**  a space character.  However, spaces do not
8419382Seric 		**  necessarily delimit an old-style name -- at
8429382Seric 		**  signs mean keep going.
8439382Seric 		*/
8449382Seric 
8459382Seric 		/* find end of name */
84658050Seric 		while ((isascii(*p) && isspace(*p)) || *p == ',')
8479382Seric 			p++;
8489382Seric 		name = p;
8499382Seric 		for (;;)
8509382Seric 		{
85158333Seric 			auto char *oldp;
85216909Seric 			char pvpbuf[PSBUFSIZE];
8539382Seric 			extern char **prescan();
8549382Seric 
85558333Seric 			(void) prescan(p, oldstyle ? ' ' : ',', pvpbuf, &oldp);
85658333Seric 			p = oldp;
8579382Seric 
8589382Seric 			/* look to see if we have an at sign */
85958050Seric 			while (*p != '\0' && isascii(*p) && isspace(*p))
8609382Seric 				p++;
8619382Seric 
86258170Seric 			if (*p != '@')
8639382Seric 			{
8649382Seric 				p = oldp;
8659382Seric 				break;
8669382Seric 			}
8679382Seric 			p += *p == '@' ? 1 : 2;
86858050Seric 			while (*p != '\0' && isascii(*p) && isspace(*p))
8699382Seric 				p++;
8709382Seric 		}
8719382Seric 		/* at the end of one complete name */
8729382Seric 
8739382Seric 		/* strip off trailing white space */
87458050Seric 		while (p >= name &&
87558050Seric 		       ((isascii(*p) && isspace(*p)) || *p == ',' || *p == '\0'))
8769382Seric 			p--;
8779382Seric 		if (++p == name)
8789382Seric 			continue;
8799382Seric 		savechar = *p;
8809382Seric 		*p = '\0';
8819382Seric 
8829382Seric 		/* translate the name to be relative */
88358020Seric 		name = remotename(name, m, bitset(H_FROM, h->h_flags),
88458509Seric 				  TRUE, FALSE, TRUE, e);
8859382Seric 		if (*name == '\0')
8869382Seric 		{
8879382Seric 			*p = savechar;
8889382Seric 			continue;
8899382Seric 		}
8909382Seric 
8919382Seric 		/* output the name with nice formatting */
89254983Seric 		opos += strlen(name);
8939382Seric 		if (!firstone)
8949382Seric 			opos += 2;
8959382Seric 		if (opos > 78 && !firstone)
8969382Seric 		{
89710178Seric 			(void) strcpy(obp, ",\n");
89810176Seric 			putline(obuf, fp, m);
8999382Seric 			obp = obuf;
9009382Seric 			(void) sprintf(obp, "        ");
90110161Seric 			opos = strlen(obp);
90210161Seric 			obp += opos;
90354983Seric 			opos += strlen(name);
9049382Seric 		}
9059382Seric 		else if (!firstone)
9069382Seric 		{
9079382Seric 			(void) sprintf(obp, ", ");
9089382Seric 			obp += 2;
9099382Seric 		}
9109382Seric 
9119382Seric 		/* strip off quote bits as we output */
91254983Seric 		while ((c = *name++) != '\0' && obp < &obuf[MAXLINE])
9139382Seric 		{
91454983Seric 			if (bitnset(M_7BITS, m->m_flags))
91554983Seric 				c &= 0177;
91654983Seric 			*obp++ = c;
9179382Seric 		}
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