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*58951Seric static char sccsid[] = "@(#)headers.c	6.29 (Berkeley) 04/04/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.
26758929Seric **		full -- if set, do full processing (e.g., compute
26858929Seric **			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 
27958929Seric eatheader(e, full)
2809382Seric 	register ENVELOPE *e;
28158929Seric 	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 */
33158929Seric 		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);
36158929Seric 	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
38358929Seric 	if (full && LogLevel > 4)
38411290Seric 	{
38557359Seric 		char *name;
38657232Seric 		char hbuf[MAXNAME];
38757359Seric 		char sbuf[MAXLINE];
388*58951Seric 		extern char *macvalue();
38936230Skarels 
39058667Seric 		if (bitset(EF_RESPONSE, e->e_flags))
39158667Seric 			name = "[RESPONSE]";
392*58951Seric 		else if ((name = macvalue('_', e)) != NULL)
393*58951Seric 			;
39458667Seric 		else if (RealHostName[0] == '[')
39536230Skarels 			name = RealHostName;
39636230Skarels 		else
39757359Seric 		{
39858755Seric 			extern char *anynet_ntoa();
39957359Seric 
40057359Seric 			name = hbuf;
40158798Seric 			(void) sprintf(hbuf, "%.80s", RealHostName);
40258798Seric 			if (RealHostAddr.sa.sa_family != 0)
40358798Seric 			{
40458798Seric 				p = &hbuf[strlen(hbuf)];
40558798Seric 				(void) sprintf(p, " (%s)",
40658798Seric 					anynet_ntoa(&RealHostAddr));
40758798Seric 			}
40857359Seric 		}
40957359Seric 
41057359Seric 		/* some versions of syslog only take 5 printf args */
41157589Seric 		sprintf(sbuf, "from=%.200s, size=%ld, class=%d, pri=%ld, nrcpts=%d, msgid=%.100s",
41258558Seric 		    e->e_from.q_paddr, e->e_msgsize, e->e_class,
41357589Seric 		    e->e_msgpriority, e->e_nrcpts, msgid);
41458686Seric 		syslog(LOG_INFO, "%s: %s, relay=%s",
41557359Seric 		    e->e_id, sbuf, name);
41611290Seric 	}
41756795Seric # endif /* LOG */
4187783Seric }
4197783Seric /*
4207783Seric **  PRIENCODE -- encode external priority names into internal values.
4217783Seric **
4227783Seric **	Parameters:
4237783Seric **		p -- priority in ascii.
4247783Seric **
4257783Seric **	Returns:
4267783Seric **		priority as a numeric level.
4277783Seric **
4287783Seric **	Side Effects:
4297783Seric **		none.
4307783Seric */
4317783Seric 
4327783Seric priencode(p)
4337783Seric 	char *p;
4347783Seric {
4358253Seric 	register int i;
4367783Seric 
4378253Seric 	for (i = 0; i < NumPriorities; i++)
4387783Seric 	{
43933725Sbostic 		if (!strcasecmp(p, Priorities[i].pri_name))
4408253Seric 			return (Priorities[i].pri_val);
4417783Seric 	}
4428253Seric 
4438253Seric 	/* unknown priority */
4448253Seric 	return (0);
4457783Seric }
4467783Seric /*
4477890Seric **  CRACKADDR -- parse an address and turn it into a macro
4487783Seric **
4497783Seric **	This doesn't actually parse the address -- it just extracts
4507783Seric **	it and replaces it with "$g".  The parse is totally ad hoc
4517783Seric **	and isn't even guaranteed to leave something syntactically
4527783Seric **	identical to what it started with.  However, it does leave
4537783Seric **	something semantically identical.
4547783Seric **
45551379Seric **	This algorithm has been cleaned up to handle a wider range
45651379Seric **	of cases -- notably quoted and backslash escaped strings.
45751379Seric **	This modification makes it substantially better at preserving
45851379Seric **	the original syntax.
4597783Seric **
4607783Seric **	Parameters:
4617890Seric **		addr -- the address to be cracked.
4627783Seric **
4637783Seric **	Returns:
4647783Seric **		a pointer to the new version.
4657783Seric **
4667783Seric **	Side Effects:
4677890Seric **		none.
4687783Seric **
4697783Seric **	Warning:
4707783Seric **		The return value is saved in local storage and should
4717783Seric **		be copied if it is to be reused.
4727783Seric */
4737783Seric 
4747783Seric char *
4757890Seric crackaddr(addr)
4767890Seric 	register char *addr;
4777783Seric {
4787783Seric 	register char *p;
47951379Seric 	register char c;
48051379Seric 	int cmtlev;
48156764Seric 	int realcmtlev;
48256764Seric 	int anglelev, realanglelev;
48351379Seric 	int copylev;
48451379Seric 	bool qmode;
48556764Seric 	bool realqmode;
48656764Seric 	bool skipping;
48751379Seric 	bool putgmac = FALSE;
48856735Seric 	bool quoteit = FALSE;
48951379Seric 	register char *bp;
49056764Seric 	char *buflim;
4917783Seric 	static char buf[MAXNAME];
4927783Seric 
4937783Seric 	if (tTd(33, 1))
4947890Seric 		printf("crackaddr(%s)\n", addr);
4957783Seric 
4968082Seric 	/* strip leading spaces */
49758050Seric 	while (*addr != '\0' && isascii(*addr) && isspace(*addr))
4988082Seric 		addr++;
4998082Seric 
5007783Seric 	/*
50151379Seric 	**  Start by assuming we have no angle brackets.  This will be
50251379Seric 	**  adjusted later if we find them.
5037783Seric 	*/
5047783Seric 
50551379Seric 	bp = buf;
50656764Seric 	buflim = &buf[sizeof buf - 5];
50751379Seric 	p = addr;
50856764Seric 	copylev = anglelev = realanglelev = cmtlev = realcmtlev = 0;
50956764Seric 	qmode = realqmode = FALSE;
51051379Seric 
51151379Seric 	while ((c = *p++) != '\0')
5127783Seric 	{
51356764Seric 		/*
51456764Seric 		**  If the buffer is overful, go into a special "skipping"
51556764Seric 		**  mode that tries to keep legal syntax but doesn't actually
51656764Seric 		**  output things.
51756764Seric 		*/
5187783Seric 
51956764Seric 		skipping = bp >= buflim;
52056735Seric 
52156764Seric 		if (copylev > 0 && !skipping)
52256764Seric 			*bp++ = c;
52356735Seric 
52451379Seric 		/* check for backslash escapes */
52551379Seric 		if (c == '\\')
5267783Seric 		{
52758890Seric 			/* arrange to quote the address */
52858890Seric 			if (cmtlev <= 0 && !qmode)
52958890Seric 				quoteit = TRUE;
53058890Seric 
53151379Seric 			if ((c = *p++) == '\0')
5327783Seric 			{
53351379Seric 				/* too far */
53451379Seric 				p--;
53551379Seric 				goto putg;
5367783Seric 			}
53756764Seric 			if (copylev > 0 && !skipping)
53851379Seric 				*bp++ = c;
53951379Seric 			goto putg;
5407783Seric 		}
5417783Seric 
54251379Seric 		/* check for quoted strings */
54351379Seric 		if (c == '"')
5447783Seric 		{
54551379Seric 			qmode = !qmode;
54656764Seric 			if (copylev > 0 && !skipping)
54756764Seric 				realqmode = !realqmode;
54851379Seric 			continue;
5497783Seric 		}
55051379Seric 		if (qmode)
55151379Seric 			goto putg;
5527783Seric 
55351379Seric 		/* check for comments */
55451379Seric 		if (c == '(')
5557783Seric 		{
55651379Seric 			cmtlev++;
55756764Seric 
55856764Seric 			/* allow space for closing paren */
55956764Seric 			if (!skipping)
56056764Seric 			{
56156764Seric 				buflim--;
56256764Seric 				realcmtlev++;
56356764Seric 				if (copylev++ <= 0)
56456764Seric 				{
56556764Seric 					*bp++ = ' ';
56656764Seric 					*bp++ = c;
56756764Seric 				}
56856764Seric 			}
56951379Seric 		}
57051379Seric 		if (cmtlev > 0)
57151379Seric 		{
57251379Seric 			if (c == ')')
5737783Seric 			{
57451379Seric 				cmtlev--;
57551379Seric 				copylev--;
57656764Seric 				if (!skipping)
57756764Seric 				{
57856764Seric 					realcmtlev--;
57956764Seric 					buflim++;
58056764Seric 				}
5817783Seric 			}
5827783Seric 			continue;
5837783Seric 		}
58456764Seric 		else if (c == ')')
58556764Seric 		{
58656764Seric 			/* syntax error: unmatched ) */
58756764Seric 			if (!skipping)
58856764Seric 				bp--;
58956764Seric 		}
5907783Seric 
59156764Seric 
59256764Seric 		/* check for characters that may have to be quoted */
59357175Seric 		if (strchr(".'@,;:\\()", c) != NULL)
59456764Seric 		{
59556764Seric 			/*
59656764Seric 			**  If these occur as the phrase part of a <>
59756764Seric 			**  construct, but are not inside of () or already
59856764Seric 			**  quoted, they will have to be quoted.  Note that
59956764Seric 			**  now (but don't actually do the quoting).
60056764Seric 			*/
60156764Seric 
60256764Seric 			if (cmtlev <= 0 && !qmode)
60356764Seric 				quoteit = TRUE;
60456764Seric 		}
60556764Seric 
60651379Seric 		/* check for angle brackets */
60751379Seric 		if (c == '<')
60851379Seric 		{
60956735Seric 			register char *q;
61056735Seric 
61151379Seric 			/* oops -- have to change our mind */
61256764Seric 			anglelev++;
61356764Seric 			if (!skipping)
61456764Seric 				realanglelev++;
61556764Seric 
61656735Seric 			bp = buf;
61756735Seric 			if (quoteit)
61856735Seric 			{
61956735Seric 				*bp++ = '"';
62056735Seric 
62156735Seric 				/* back up over the '<' and any spaces */
62256735Seric 				--p;
62358050Seric 				while (isascii(*--p) && isspace(*p))
62456735Seric 					continue;
62556735Seric 				p++;
62656735Seric 			}
62756735Seric 			for (q = addr; q < p; )
62856735Seric 			{
62956735Seric 				c = *q++;
63056764Seric 				if (bp < buflim)
63156764Seric 				{
63256764Seric 					if (quoteit && c == '"')
63356764Seric 						*bp++ = '\\';
63456764Seric 					*bp++ = c;
63556764Seric 				}
63656735Seric 			}
63756735Seric 			if (quoteit)
63856735Seric 			{
63956735Seric 				*bp++ = '"';
64056764Seric 				while ((c = *p++) != '<')
64156764Seric 				{
64256764Seric 					if (bp < buflim)
64356764Seric 						*bp++ = c;
64456764Seric 				}
64556764Seric 				*bp++ = c;
64656735Seric 			}
64751379Seric 			copylev = 0;
64856735Seric 			putgmac = quoteit = FALSE;
64951379Seric 			continue;
65051379Seric 		}
6517783Seric 
65251379Seric 		if (c == '>')
6537783Seric 		{
65456764Seric 			if (anglelev > 0)
65556764Seric 			{
65656764Seric 				anglelev--;
65756764Seric 				if (!skipping)
65856764Seric 				{
65956764Seric 					realanglelev--;
66056764Seric 					buflim++;
66156764Seric 				}
66256764Seric 			}
66356764Seric 			else if (!skipping)
66456764Seric 			{
66556764Seric 				/* syntax error: unmatched > */
66656764Seric 				if (copylev > 0)
66756764Seric 					bp--;
66856764Seric 				continue;
66956764Seric 			}
67051379Seric 			if (copylev++ <= 0)
67151379Seric 				*bp++ = c;
67251379Seric 			continue;
6737783Seric 		}
67451379Seric 
67551379Seric 		/* must be a real address character */
67651379Seric 	putg:
67751379Seric 		if (copylev <= 0 && !putgmac)
67851379Seric 		{
67958050Seric 			*bp++ = MACROEXPAND;
68051379Seric 			*bp++ = 'g';
68151379Seric 			putgmac = TRUE;
68251379Seric 		}
6837783Seric 	}
6847783Seric 
68556764Seric 	/* repair any syntactic damage */
68656764Seric 	if (realqmode)
68756764Seric 		*bp++ = '"';
68856764Seric 	while (realcmtlev-- > 0)
68956764Seric 		*bp++ = ')';
69056764Seric 	while (realanglelev-- > 0)
69156764Seric 		*bp++ = '>';
69251379Seric 	*bp++ = '\0';
6937783Seric 
6947783Seric 	if (tTd(33, 1))
6957944Seric 		printf("crackaddr=>`%s'\n", buf);
6967783Seric 
6977783Seric 	return (buf);
6987783Seric }
6999382Seric /*
7009382Seric **  PUTHEADER -- put the header part of a message from the in-core copy
7019382Seric **
7029382Seric **	Parameters:
7039382Seric **		fp -- file to put it on.
7049382Seric **		m -- mailer to use.
7059382Seric **		e -- envelope to use.
7069382Seric **
7079382Seric **	Returns:
7089382Seric **		none.
7099382Seric **
7109382Seric **	Side Effects:
7119382Seric **		none.
7129382Seric */
7139382Seric 
71457589Seric /*
71557589Seric  * Macro for fast max (not available in e.g. DG/UX, 386/ix).
71657589Seric  */
71757589Seric #ifndef MAX
71857589Seric # define MAX(a,b) (((a)>(b))?(a):(b))
71957589Seric #endif
72057589Seric 
72110176Seric putheader(fp, m, e)
7229382Seric 	register FILE *fp;
7239382Seric 	register MAILER *m;
7249382Seric 	register ENVELOPE *e;
7259382Seric {
72657135Seric 	char buf[MAX(MAXLINE,BUFSIZ)];
7279382Seric 	register HDR *h;
72857135Seric 	char obuf[MAXLINE];
7299382Seric 
7309382Seric 	for (h = e->e_header; h != NULL; h = h->h_link)
7319382Seric 	{
7329382Seric 		register char *p;
73310689Seric 		extern bool bitintersect();
7349382Seric 
7359382Seric 		if (bitset(H_CHECK|H_ACHECK, h->h_flags) &&
73610689Seric 		    !bitintersect(h->h_mflags, m->m_flags))
7379382Seric 			continue;
7389382Seric 
73911414Seric 		/* handle Resent-... headers specially */
74011414Seric 		if (bitset(H_RESENT, h->h_flags) && !bitset(EF_RESENT, e->e_flags))
74111414Seric 			continue;
74211414Seric 
7439382Seric 		p = h->h_value;
7449382Seric 		if (bitset(H_DEFAULT, h->h_flags))
7459382Seric 		{
7469382Seric 			/* macro expand value if generated internally */
7479382Seric 			expand(p, buf, &buf[sizeof buf], e);
7489382Seric 			p = buf;
7499382Seric 			if (p == NULL || *p == '\0')
7509382Seric 				continue;
7519382Seric 		}
7529382Seric 
7539382Seric 		if (bitset(H_FROM|H_RCPT, h->h_flags))
7549382Seric 		{
7559382Seric 			/* address field */
7569382Seric 			bool oldstyle = bitset(EF_OLDSTYLE, e->e_flags);
7579382Seric 
7589382Seric 			if (bitset(H_FROM, h->h_flags))
7599382Seric 				oldstyle = FALSE;
76055012Seric 			commaize(h, p, fp, oldstyle, m, e);
7619382Seric 		}
7629382Seric 		else
7639382Seric 		{
7649382Seric 			/* vanilla header line */
76512159Seric 			register char *nlp;
76657642Seric 			extern char *capitalize();
76712159Seric 
76812159Seric 			(void) sprintf(obuf, "%s: ", capitalize(h->h_field));
76956795Seric 			while ((nlp = strchr(p, '\n')) != NULL)
77012159Seric 			{
77112159Seric 				*nlp = '\0';
77212159Seric 				(void) strcat(obuf, p);
77312159Seric 				*nlp = '\n';
77412159Seric 				putline(obuf, fp, m);
77512159Seric 				p = ++nlp;
77612161Seric 				obuf[0] = '\0';
77712159Seric 			}
77812159Seric 			(void) strcat(obuf, p);
77910176Seric 			putline(obuf, fp, m);
7809382Seric 		}
7819382Seric 	}
7829382Seric }
7839382Seric /*
7849382Seric **  COMMAIZE -- output a header field, making a comma-translated list.
7859382Seric **
7869382Seric **	Parameters:
7879382Seric **		h -- the header field to output.
7889382Seric **		p -- the value to put in it.
7899382Seric **		fp -- file to put it to.
7909382Seric **		oldstyle -- TRUE if this is an old style header.
7919382Seric **		m -- a pointer to the mailer descriptor.  If NULL,
7929382Seric **			don't transform the name at all.
79355012Seric **		e -- the envelope containing the message.
7949382Seric **
7959382Seric **	Returns:
7969382Seric **		none.
7979382Seric **
7989382Seric **	Side Effects:
7999382Seric **		outputs "p" to file "fp".
8009382Seric */
8019382Seric 
80255012Seric commaize(h, p, fp, oldstyle, m, e)
8039382Seric 	register HDR *h;
8049382Seric 	register char *p;
8059382Seric 	FILE *fp;
8069382Seric 	bool oldstyle;
8079382Seric 	register MAILER *m;
80855012Seric 	register ENVELOPE *e;
8099382Seric {
8109382Seric 	register char *obp;
8119382Seric 	int opos;
8129382Seric 	bool firstone = TRUE;
81311157Seric 	char obuf[MAXLINE + 3];
81457642Seric 	extern char *capitalize();
8159382Seric 
8169382Seric 	/*
8179382Seric 	**  Output the address list translated by the
8189382Seric 	**  mailer and with commas.
8199382Seric 	*/
8209382Seric 
8219382Seric 	if (tTd(14, 2))
8229382Seric 		printf("commaize(%s: %s)\n", h->h_field, p);
8239382Seric 
8249382Seric 	obp = obuf;
8259382Seric 	(void) sprintf(obp, "%s: ", capitalize(h->h_field));
8269382Seric 	opos = strlen(h->h_field) + 2;
8279382Seric 	obp += opos;
8289382Seric 
8299382Seric 	/*
8309382Seric 	**  Run through the list of values.
8319382Seric 	*/
8329382Seric 
8339382Seric 	while (*p != '\0')
8349382Seric 	{
8359382Seric 		register char *name;
83654983Seric 		register int c;
8379382Seric 		char savechar;
8389382Seric 		extern char *remotename();
8399382Seric 
8409382Seric 		/*
8419382Seric 		**  Find the end of the name.  New style names
8429382Seric 		**  end with a comma, old style names end with
8439382Seric 		**  a space character.  However, spaces do not
8449382Seric 		**  necessarily delimit an old-style name -- at
8459382Seric 		**  signs mean keep going.
8469382Seric 		*/
8479382Seric 
8489382Seric 		/* find end of name */
84958050Seric 		while ((isascii(*p) && isspace(*p)) || *p == ',')
8509382Seric 			p++;
8519382Seric 		name = p;
8529382Seric 		for (;;)
8539382Seric 		{
85458333Seric 			auto char *oldp;
85516909Seric 			char pvpbuf[PSBUFSIZE];
8569382Seric 			extern char **prescan();
8579382Seric 
85858333Seric 			(void) prescan(p, oldstyle ? ' ' : ',', pvpbuf, &oldp);
85958333Seric 			p = oldp;
8609382Seric 
8619382Seric 			/* look to see if we have an at sign */
86258050Seric 			while (*p != '\0' && isascii(*p) && isspace(*p))
8639382Seric 				p++;
8649382Seric 
86558170Seric 			if (*p != '@')
8669382Seric 			{
8679382Seric 				p = oldp;
8689382Seric 				break;
8699382Seric 			}
8709382Seric 			p += *p == '@' ? 1 : 2;
87158050Seric 			while (*p != '\0' && isascii(*p) && isspace(*p))
8729382Seric 				p++;
8739382Seric 		}
8749382Seric 		/* at the end of one complete name */
8759382Seric 
8769382Seric 		/* strip off trailing white space */
87758050Seric 		while (p >= name &&
87858050Seric 		       ((isascii(*p) && isspace(*p)) || *p == ',' || *p == '\0'))
8799382Seric 			p--;
8809382Seric 		if (++p == name)
8819382Seric 			continue;
8829382Seric 		savechar = *p;
8839382Seric 		*p = '\0';
8849382Seric 
8859382Seric 		/* translate the name to be relative */
88658020Seric 		name = remotename(name, m, bitset(H_FROM, h->h_flags),
88758509Seric 				  TRUE, FALSE, TRUE, e);
8889382Seric 		if (*name == '\0')
8899382Seric 		{
8909382Seric 			*p = savechar;
8919382Seric 			continue;
8929382Seric 		}
8939382Seric 
8949382Seric 		/* output the name with nice formatting */
89554983Seric 		opos += strlen(name);
8969382Seric 		if (!firstone)
8979382Seric 			opos += 2;
8989382Seric 		if (opos > 78 && !firstone)
8999382Seric 		{
90010178Seric 			(void) strcpy(obp, ",\n");
90110176Seric 			putline(obuf, fp, m);
9029382Seric 			obp = obuf;
9039382Seric 			(void) sprintf(obp, "        ");
90410161Seric 			opos = strlen(obp);
90510161Seric 			obp += opos;
90654983Seric 			opos += strlen(name);
9079382Seric 		}
9089382Seric 		else if (!firstone)
9099382Seric 		{
9109382Seric 			(void) sprintf(obp, ", ");
9119382Seric 			obp += 2;
9129382Seric 		}
9139382Seric 
9149382Seric 		/* strip off quote bits as we output */
91554983Seric 		while ((c = *name++) != '\0' && obp < &obuf[MAXLINE])
9169382Seric 		{
91754983Seric 			if (bitnset(M_7BITS, m->m_flags))
91854983Seric 				c &= 0177;
91954983Seric 			*obp++ = c;
9209382Seric 		}
9219382Seric 		firstone = FALSE;
9229382Seric 		*p = savechar;
9239382Seric 	}
9249382Seric 	(void) strcpy(obp, "\n");
92510176Seric 	putline(obuf, fp, m);
9269382Seric }
9279382Seric /*
92858170Seric **  COPYHEADER -- copy header list
9299382Seric **
93058170Seric **	This routine is the equivalent of newstr for header lists
93158170Seric **
9329382Seric **	Parameters:
93358170Seric **		header -- list of header structures to copy.
9349382Seric **
9359382Seric **	Returns:
93658170Seric **		a copy of 'header'.
9379382Seric **
9389382Seric **	Side Effects:
9399382Seric **		none.
9409382Seric */
9419382Seric 
94258170Seric HDR *
94358170Seric copyheader(header)
94458170Seric 	register HDR *header;
9459382Seric {
94658170Seric 	register HDR *newhdr;
94758170Seric 	HDR *ret;
94858170Seric 	register HDR **tail = &ret;
9499382Seric 
95058170Seric 	while (header != NULL)
95158170Seric 	{
95258170Seric 		newhdr = (HDR *) xalloc(sizeof(HDR));
95358170Seric 		STRUCTCOPY(*header, *newhdr);
95458170Seric 		*tail = newhdr;
95558170Seric 		tail = &newhdr->h_link;
95658170Seric 		header = header->h_link;
95758170Seric 	}
95858170Seric 	*tail = NULL;
95958170Seric 
96058170Seric 	return ret;
9619382Seric }
962