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*58890Seric static char sccsid[] = "@(#)headers.c	6.27 (Berkeley) 03/31/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.
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;
39558798Seric 			(void) sprintf(hbuf, "%.80s", RealHostName);
39658798Seric 			if (RealHostAddr.sa.sa_family != 0)
39758798Seric 			{
39858798Seric 				p = &hbuf[strlen(hbuf)];
39958798Seric 				(void) sprintf(p, " (%s)",
40058798Seric 					anynet_ntoa(&RealHostAddr));
40158798Seric 			}
40257359Seric 		}
40357359Seric 
40457359Seric 		/* some versions of syslog only take 5 printf args */
40557589Seric 		sprintf(sbuf, "from=%.200s, size=%ld, class=%d, pri=%ld, nrcpts=%d, msgid=%.100s",
40658558Seric 		    e->e_from.q_paddr, e->e_msgsize, e->e_class,
40757589Seric 		    e->e_msgpriority, e->e_nrcpts, msgid);
40858686Seric 		syslog(LOG_INFO, "%s: %s, relay=%s",
40957359Seric 		    e->e_id, sbuf, name);
41011290Seric 	}
41156795Seric # endif /* LOG */
4127783Seric }
4137783Seric /*
4147783Seric **  PRIENCODE -- encode external priority names into internal values.
4157783Seric **
4167783Seric **	Parameters:
4177783Seric **		p -- priority in ascii.
4187783Seric **
4197783Seric **	Returns:
4207783Seric **		priority as a numeric level.
4217783Seric **
4227783Seric **	Side Effects:
4237783Seric **		none.
4247783Seric */
4257783Seric 
4267783Seric priencode(p)
4277783Seric 	char *p;
4287783Seric {
4298253Seric 	register int i;
4307783Seric 
4318253Seric 	for (i = 0; i < NumPriorities; i++)
4327783Seric 	{
43333725Sbostic 		if (!strcasecmp(p, Priorities[i].pri_name))
4348253Seric 			return (Priorities[i].pri_val);
4357783Seric 	}
4368253Seric 
4378253Seric 	/* unknown priority */
4388253Seric 	return (0);
4397783Seric }
4407783Seric /*
4417890Seric **  CRACKADDR -- parse an address and turn it into a macro
4427783Seric **
4437783Seric **	This doesn't actually parse the address -- it just extracts
4447783Seric **	it and replaces it with "$g".  The parse is totally ad hoc
4457783Seric **	and isn't even guaranteed to leave something syntactically
4467783Seric **	identical to what it started with.  However, it does leave
4477783Seric **	something semantically identical.
4487783Seric **
44951379Seric **	This algorithm has been cleaned up to handle a wider range
45051379Seric **	of cases -- notably quoted and backslash escaped strings.
45151379Seric **	This modification makes it substantially better at preserving
45251379Seric **	the original syntax.
4537783Seric **
4547783Seric **	Parameters:
4557890Seric **		addr -- the address to be cracked.
4567783Seric **
4577783Seric **	Returns:
4587783Seric **		a pointer to the new version.
4597783Seric **
4607783Seric **	Side Effects:
4617890Seric **		none.
4627783Seric **
4637783Seric **	Warning:
4647783Seric **		The return value is saved in local storage and should
4657783Seric **		be copied if it is to be reused.
4667783Seric */
4677783Seric 
4687783Seric char *
4697890Seric crackaddr(addr)
4707890Seric 	register char *addr;
4717783Seric {
4727783Seric 	register char *p;
47351379Seric 	register char c;
47451379Seric 	int cmtlev;
47556764Seric 	int realcmtlev;
47656764Seric 	int anglelev, realanglelev;
47751379Seric 	int copylev;
47851379Seric 	bool qmode;
47956764Seric 	bool realqmode;
48056764Seric 	bool skipping;
48151379Seric 	bool putgmac = FALSE;
48256735Seric 	bool quoteit = FALSE;
48351379Seric 	register char *bp;
48456764Seric 	char *buflim;
4857783Seric 	static char buf[MAXNAME];
4867783Seric 
4877783Seric 	if (tTd(33, 1))
4887890Seric 		printf("crackaddr(%s)\n", addr);
4897783Seric 
4908082Seric 	/* strip leading spaces */
49158050Seric 	while (*addr != '\0' && isascii(*addr) && isspace(*addr))
4928082Seric 		addr++;
4938082Seric 
4947783Seric 	/*
49551379Seric 	**  Start by assuming we have no angle brackets.  This will be
49651379Seric 	**  adjusted later if we find them.
4977783Seric 	*/
4987783Seric 
49951379Seric 	bp = buf;
50056764Seric 	buflim = &buf[sizeof buf - 5];
50151379Seric 	p = addr;
50256764Seric 	copylev = anglelev = realanglelev = cmtlev = realcmtlev = 0;
50356764Seric 	qmode = realqmode = FALSE;
50451379Seric 
50551379Seric 	while ((c = *p++) != '\0')
5067783Seric 	{
50756764Seric 		/*
50856764Seric 		**  If the buffer is overful, go into a special "skipping"
50956764Seric 		**  mode that tries to keep legal syntax but doesn't actually
51056764Seric 		**  output things.
51156764Seric 		*/
5127783Seric 
51356764Seric 		skipping = bp >= buflim;
51456735Seric 
51556764Seric 		if (copylev > 0 && !skipping)
51656764Seric 			*bp++ = c;
51756735Seric 
51851379Seric 		/* check for backslash escapes */
51951379Seric 		if (c == '\\')
5207783Seric 		{
521*58890Seric 			/* arrange to quote the address */
522*58890Seric 			if (cmtlev <= 0 && !qmode)
523*58890Seric 				quoteit = TRUE;
524*58890Seric 
52551379Seric 			if ((c = *p++) == '\0')
5267783Seric 			{
52751379Seric 				/* too far */
52851379Seric 				p--;
52951379Seric 				goto putg;
5307783Seric 			}
53156764Seric 			if (copylev > 0 && !skipping)
53251379Seric 				*bp++ = c;
53351379Seric 			goto putg;
5347783Seric 		}
5357783Seric 
53651379Seric 		/* check for quoted strings */
53751379Seric 		if (c == '"')
5387783Seric 		{
53951379Seric 			qmode = !qmode;
54056764Seric 			if (copylev > 0 && !skipping)
54156764Seric 				realqmode = !realqmode;
54251379Seric 			continue;
5437783Seric 		}
54451379Seric 		if (qmode)
54551379Seric 			goto putg;
5467783Seric 
54751379Seric 		/* check for comments */
54851379Seric 		if (c == '(')
5497783Seric 		{
55051379Seric 			cmtlev++;
55156764Seric 
55256764Seric 			/* allow space for closing paren */
55356764Seric 			if (!skipping)
55456764Seric 			{
55556764Seric 				buflim--;
55656764Seric 				realcmtlev++;
55756764Seric 				if (copylev++ <= 0)
55856764Seric 				{
55956764Seric 					*bp++ = ' ';
56056764Seric 					*bp++ = c;
56156764Seric 				}
56256764Seric 			}
56351379Seric 		}
56451379Seric 		if (cmtlev > 0)
56551379Seric 		{
56651379Seric 			if (c == ')')
5677783Seric 			{
56851379Seric 				cmtlev--;
56951379Seric 				copylev--;
57056764Seric 				if (!skipping)
57156764Seric 				{
57256764Seric 					realcmtlev--;
57356764Seric 					buflim++;
57456764Seric 				}
5757783Seric 			}
5767783Seric 			continue;
5777783Seric 		}
57856764Seric 		else if (c == ')')
57956764Seric 		{
58056764Seric 			/* syntax error: unmatched ) */
58156764Seric 			if (!skipping)
58256764Seric 				bp--;
58356764Seric 		}
5847783Seric 
58556764Seric 
58656764Seric 		/* check for characters that may have to be quoted */
58757175Seric 		if (strchr(".'@,;:\\()", c) != NULL)
58856764Seric 		{
58956764Seric 			/*
59056764Seric 			**  If these occur as the phrase part of a <>
59156764Seric 			**  construct, but are not inside of () or already
59256764Seric 			**  quoted, they will have to be quoted.  Note that
59356764Seric 			**  now (but don't actually do the quoting).
59456764Seric 			*/
59556764Seric 
59656764Seric 			if (cmtlev <= 0 && !qmode)
59756764Seric 				quoteit = TRUE;
59856764Seric 		}
59956764Seric 
60051379Seric 		/* check for angle brackets */
60151379Seric 		if (c == '<')
60251379Seric 		{
60356735Seric 			register char *q;
60456735Seric 
60551379Seric 			/* oops -- have to change our mind */
60656764Seric 			anglelev++;
60756764Seric 			if (!skipping)
60856764Seric 				realanglelev++;
60956764Seric 
61056735Seric 			bp = buf;
61156735Seric 			if (quoteit)
61256735Seric 			{
61356735Seric 				*bp++ = '"';
61456735Seric 
61556735Seric 				/* back up over the '<' and any spaces */
61656735Seric 				--p;
61758050Seric 				while (isascii(*--p) && isspace(*p))
61856735Seric 					continue;
61956735Seric 				p++;
62056735Seric 			}
62156735Seric 			for (q = addr; q < p; )
62256735Seric 			{
62356735Seric 				c = *q++;
62456764Seric 				if (bp < buflim)
62556764Seric 				{
62656764Seric 					if (quoteit && c == '"')
62756764Seric 						*bp++ = '\\';
62856764Seric 					*bp++ = c;
62956764Seric 				}
63056735Seric 			}
63156735Seric 			if (quoteit)
63256735Seric 			{
63356735Seric 				*bp++ = '"';
63456764Seric 				while ((c = *p++) != '<')
63556764Seric 				{
63656764Seric 					if (bp < buflim)
63756764Seric 						*bp++ = c;
63856764Seric 				}
63956764Seric 				*bp++ = c;
64056735Seric 			}
64151379Seric 			copylev = 0;
64256735Seric 			putgmac = quoteit = FALSE;
64351379Seric 			continue;
64451379Seric 		}
6457783Seric 
64651379Seric 		if (c == '>')
6477783Seric 		{
64856764Seric 			if (anglelev > 0)
64956764Seric 			{
65056764Seric 				anglelev--;
65156764Seric 				if (!skipping)
65256764Seric 				{
65356764Seric 					realanglelev--;
65456764Seric 					buflim++;
65556764Seric 				}
65656764Seric 			}
65756764Seric 			else if (!skipping)
65856764Seric 			{
65956764Seric 				/* syntax error: unmatched > */
66056764Seric 				if (copylev > 0)
66156764Seric 					bp--;
66256764Seric 				continue;
66356764Seric 			}
66451379Seric 			if (copylev++ <= 0)
66551379Seric 				*bp++ = c;
66651379Seric 			continue;
6677783Seric 		}
66851379Seric 
66951379Seric 		/* must be a real address character */
67051379Seric 	putg:
67151379Seric 		if (copylev <= 0 && !putgmac)
67251379Seric 		{
67358050Seric 			*bp++ = MACROEXPAND;
67451379Seric 			*bp++ = 'g';
67551379Seric 			putgmac = TRUE;
67651379Seric 		}
6777783Seric 	}
6787783Seric 
67956764Seric 	/* repair any syntactic damage */
68056764Seric 	if (realqmode)
68156764Seric 		*bp++ = '"';
68256764Seric 	while (realcmtlev-- > 0)
68356764Seric 		*bp++ = ')';
68456764Seric 	while (realanglelev-- > 0)
68556764Seric 		*bp++ = '>';
68651379Seric 	*bp++ = '\0';
6877783Seric 
6887783Seric 	if (tTd(33, 1))
6897944Seric 		printf("crackaddr=>`%s'\n", buf);
6907783Seric 
6917783Seric 	return (buf);
6927783Seric }
6939382Seric /*
6949382Seric **  PUTHEADER -- put the header part of a message from the in-core copy
6959382Seric **
6969382Seric **	Parameters:
6979382Seric **		fp -- file to put it on.
6989382Seric **		m -- mailer to use.
6999382Seric **		e -- envelope to use.
7009382Seric **
7019382Seric **	Returns:
7029382Seric **		none.
7039382Seric **
7049382Seric **	Side Effects:
7059382Seric **		none.
7069382Seric */
7079382Seric 
70857589Seric /*
70957589Seric  * Macro for fast max (not available in e.g. DG/UX, 386/ix).
71057589Seric  */
71157589Seric #ifndef MAX
71257589Seric # define MAX(a,b) (((a)>(b))?(a):(b))
71357589Seric #endif
71457589Seric 
71510176Seric putheader(fp, m, e)
7169382Seric 	register FILE *fp;
7179382Seric 	register MAILER *m;
7189382Seric 	register ENVELOPE *e;
7199382Seric {
72057135Seric 	char buf[MAX(MAXLINE,BUFSIZ)];
7219382Seric 	register HDR *h;
72257135Seric 	char obuf[MAXLINE];
7239382Seric 
7249382Seric 	for (h = e->e_header; h != NULL; h = h->h_link)
7259382Seric 	{
7269382Seric 		register char *p;
72710689Seric 		extern bool bitintersect();
7289382Seric 
7299382Seric 		if (bitset(H_CHECK|H_ACHECK, h->h_flags) &&
73010689Seric 		    !bitintersect(h->h_mflags, m->m_flags))
7319382Seric 			continue;
7329382Seric 
73311414Seric 		/* handle Resent-... headers specially */
73411414Seric 		if (bitset(H_RESENT, h->h_flags) && !bitset(EF_RESENT, e->e_flags))
73511414Seric 			continue;
73611414Seric 
7379382Seric 		p = h->h_value;
7389382Seric 		if (bitset(H_DEFAULT, h->h_flags))
7399382Seric 		{
7409382Seric 			/* macro expand value if generated internally */
7419382Seric 			expand(p, buf, &buf[sizeof buf], e);
7429382Seric 			p = buf;
7439382Seric 			if (p == NULL || *p == '\0')
7449382Seric 				continue;
7459382Seric 		}
7469382Seric 
7479382Seric 		if (bitset(H_FROM|H_RCPT, h->h_flags))
7489382Seric 		{
7499382Seric 			/* address field */
7509382Seric 			bool oldstyle = bitset(EF_OLDSTYLE, e->e_flags);
7519382Seric 
7529382Seric 			if (bitset(H_FROM, h->h_flags))
7539382Seric 				oldstyle = FALSE;
75455012Seric 			commaize(h, p, fp, oldstyle, m, e);
7559382Seric 		}
7569382Seric 		else
7579382Seric 		{
7589382Seric 			/* vanilla header line */
75912159Seric 			register char *nlp;
76057642Seric 			extern char *capitalize();
76112159Seric 
76212159Seric 			(void) sprintf(obuf, "%s: ", capitalize(h->h_field));
76356795Seric 			while ((nlp = strchr(p, '\n')) != NULL)
76412159Seric 			{
76512159Seric 				*nlp = '\0';
76612159Seric 				(void) strcat(obuf, p);
76712159Seric 				*nlp = '\n';
76812159Seric 				putline(obuf, fp, m);
76912159Seric 				p = ++nlp;
77012161Seric 				obuf[0] = '\0';
77112159Seric 			}
77212159Seric 			(void) strcat(obuf, p);
77310176Seric 			putline(obuf, fp, m);
7749382Seric 		}
7759382Seric 	}
7769382Seric }
7779382Seric /*
7789382Seric **  COMMAIZE -- output a header field, making a comma-translated list.
7799382Seric **
7809382Seric **	Parameters:
7819382Seric **		h -- the header field to output.
7829382Seric **		p -- the value to put in it.
7839382Seric **		fp -- file to put it to.
7849382Seric **		oldstyle -- TRUE if this is an old style header.
7859382Seric **		m -- a pointer to the mailer descriptor.  If NULL,
7869382Seric **			don't transform the name at all.
78755012Seric **		e -- the envelope containing the message.
7889382Seric **
7899382Seric **	Returns:
7909382Seric **		none.
7919382Seric **
7929382Seric **	Side Effects:
7939382Seric **		outputs "p" to file "fp".
7949382Seric */
7959382Seric 
79655012Seric commaize(h, p, fp, oldstyle, m, e)
7979382Seric 	register HDR *h;
7989382Seric 	register char *p;
7999382Seric 	FILE *fp;
8009382Seric 	bool oldstyle;
8019382Seric 	register MAILER *m;
80255012Seric 	register ENVELOPE *e;
8039382Seric {
8049382Seric 	register char *obp;
8059382Seric 	int opos;
8069382Seric 	bool firstone = TRUE;
80711157Seric 	char obuf[MAXLINE + 3];
80857642Seric 	extern char *capitalize();
8099382Seric 
8109382Seric 	/*
8119382Seric 	**  Output the address list translated by the
8129382Seric 	**  mailer and with commas.
8139382Seric 	*/
8149382Seric 
8159382Seric 	if (tTd(14, 2))
8169382Seric 		printf("commaize(%s: %s)\n", h->h_field, p);
8179382Seric 
8189382Seric 	obp = obuf;
8199382Seric 	(void) sprintf(obp, "%s: ", capitalize(h->h_field));
8209382Seric 	opos = strlen(h->h_field) + 2;
8219382Seric 	obp += opos;
8229382Seric 
8239382Seric 	/*
8249382Seric 	**  Run through the list of values.
8259382Seric 	*/
8269382Seric 
8279382Seric 	while (*p != '\0')
8289382Seric 	{
8299382Seric 		register char *name;
83054983Seric 		register int c;
8319382Seric 		char savechar;
8329382Seric 		extern char *remotename();
8339382Seric 
8349382Seric 		/*
8359382Seric 		**  Find the end of the name.  New style names
8369382Seric 		**  end with a comma, old style names end with
8379382Seric 		**  a space character.  However, spaces do not
8389382Seric 		**  necessarily delimit an old-style name -- at
8399382Seric 		**  signs mean keep going.
8409382Seric 		*/
8419382Seric 
8429382Seric 		/* find end of name */
84358050Seric 		while ((isascii(*p) && isspace(*p)) || *p == ',')
8449382Seric 			p++;
8459382Seric 		name = p;
8469382Seric 		for (;;)
8479382Seric 		{
84858333Seric 			auto char *oldp;
84916909Seric 			char pvpbuf[PSBUFSIZE];
8509382Seric 			extern char **prescan();
8519382Seric 
85258333Seric 			(void) prescan(p, oldstyle ? ' ' : ',', pvpbuf, &oldp);
85358333Seric 			p = oldp;
8549382Seric 
8559382Seric 			/* look to see if we have an at sign */
85658050Seric 			while (*p != '\0' && isascii(*p) && isspace(*p))
8579382Seric 				p++;
8589382Seric 
85958170Seric 			if (*p != '@')
8609382Seric 			{
8619382Seric 				p = oldp;
8629382Seric 				break;
8639382Seric 			}
8649382Seric 			p += *p == '@' ? 1 : 2;
86558050Seric 			while (*p != '\0' && isascii(*p) && isspace(*p))
8669382Seric 				p++;
8679382Seric 		}
8689382Seric 		/* at the end of one complete name */
8699382Seric 
8709382Seric 		/* strip off trailing white space */
87158050Seric 		while (p >= name &&
87258050Seric 		       ((isascii(*p) && isspace(*p)) || *p == ',' || *p == '\0'))
8739382Seric 			p--;
8749382Seric 		if (++p == name)
8759382Seric 			continue;
8769382Seric 		savechar = *p;
8779382Seric 		*p = '\0';
8789382Seric 
8799382Seric 		/* translate the name to be relative */
88058020Seric 		name = remotename(name, m, bitset(H_FROM, h->h_flags),
88158509Seric 				  TRUE, FALSE, TRUE, e);
8829382Seric 		if (*name == '\0')
8839382Seric 		{
8849382Seric 			*p = savechar;
8859382Seric 			continue;
8869382Seric 		}
8879382Seric 
8889382Seric 		/* output the name with nice formatting */
88954983Seric 		opos += strlen(name);
8909382Seric 		if (!firstone)
8919382Seric 			opos += 2;
8929382Seric 		if (opos > 78 && !firstone)
8939382Seric 		{
89410178Seric 			(void) strcpy(obp, ",\n");
89510176Seric 			putline(obuf, fp, m);
8969382Seric 			obp = obuf;
8979382Seric 			(void) sprintf(obp, "        ");
89810161Seric 			opos = strlen(obp);
89910161Seric 			obp += opos;
90054983Seric 			opos += strlen(name);
9019382Seric 		}
9029382Seric 		else if (!firstone)
9039382Seric 		{
9049382Seric 			(void) sprintf(obp, ", ");
9059382Seric 			obp += 2;
9069382Seric 		}
9079382Seric 
9089382Seric 		/* strip off quote bits as we output */
90954983Seric 		while ((c = *name++) != '\0' && obp < &obuf[MAXLINE])
9109382Seric 		{
91154983Seric 			if (bitnset(M_7BITS, m->m_flags))
91254983Seric 				c &= 0177;
91354983Seric 			*obp++ = c;
9149382Seric 		}
9159382Seric 		firstone = FALSE;
9169382Seric 		*p = savechar;
9179382Seric 	}
9189382Seric 	(void) strcpy(obp, "\n");
91910176Seric 	putline(obuf, fp, m);
9209382Seric }
9219382Seric /*
92258170Seric **  COPYHEADER -- copy header list
9239382Seric **
92458170Seric **	This routine is the equivalent of newstr for header lists
92558170Seric **
9269382Seric **	Parameters:
92758170Seric **		header -- list of header structures to copy.
9289382Seric **
9299382Seric **	Returns:
93058170Seric **		a copy of 'header'.
9319382Seric **
9329382Seric **	Side Effects:
9339382Seric **		none.
9349382Seric */
9359382Seric 
93658170Seric HDR *
93758170Seric copyheader(header)
93858170Seric 	register HDR *header;
9399382Seric {
94058170Seric 	register HDR *newhdr;
94158170Seric 	HDR *ret;
94258170Seric 	register HDR **tail = &ret;
9439382Seric 
94458170Seric 	while (header != NULL)
94558170Seric 	{
94658170Seric 		newhdr = (HDR *) xalloc(sizeof(HDR));
94758170Seric 		STRUCTCOPY(*header, *newhdr);
94858170Seric 		*tail = newhdr;
94958170Seric 		tail = &newhdr->h_link;
95058170Seric 		header = header->h_link;
95158170Seric 	}
95258170Seric 	*tail = NULL;
95358170Seric 
95458170Seric 	return ret;
9559382Seric }
956