122706Sdist /*
234921Sbostic  * Copyright (c) 1983 Eric P. Allman
362525Sbostic  * Copyright (c) 1988, 1993
462525Sbostic  *	The Regents of the University of California.  All rights reserved.
533729Sbostic  *
642826Sbostic  * %sccs.include.redist.c%
733729Sbostic  */
822706Sdist 
922706Sdist #ifndef lint
10*64134Seric static char sccsid[] = "@(#)headers.c	8.4 (Berkeley) 08/07/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 
844091Seric 	/* strip field value on front */
854091Seric 	if (*fvalue == ' ')
864091Seric 		fvalue++;
874091Seric 
884091Seric 	/* see if it is a known type */
894091Seric 	for (hi = HdrInfo; hi->hi_field != NULL; hi++)
904091Seric 	{
9159579Seric 		if (strcasecmp(hi->hi_field, fname) == 0)
924091Seric 			break;
934091Seric 	}
944091Seric 
9511414Seric 	/* see if this is a resent message */
9611930Seric 	if (!def && bitset(H_RESENT, hi->hi_flags))
9755012Seric 		e->e_flags |= EF_RESENT;
9811414Seric 
994091Seric 	/* if this means "end of header" quit now */
1004091Seric 	if (bitset(H_EOH, hi->hi_flags))
1014091Seric 		return (hi->hi_flags);
1024091Seric 
10311414Seric 	/* drop explicit From: if same as what we would generate -- for MH */
10414784Seric 	p = "resent-from";
10555012Seric 	if (!bitset(EF_RESENT, e->e_flags))
10614784Seric 		p += 7;
10759579Seric 	if (!def && !bitset(EF_QUEUERUN, e->e_flags) && strcasecmp(fname, p) == 0)
10811414Seric 	{
10963753Seric 		if (tTd(31, 2))
11063753Seric 		{
11163753Seric 			printf("comparing header from (%s) against default (%s or %s)\n",
11263753Seric 				fvalue, e->e_from.q_paddr, e->e_from.q_user);
11363753Seric 		}
11455012Seric 		if (e->e_from.q_paddr != NULL &&
11563753Seric 		    (strcmp(fvalue, e->e_from.q_paddr) == 0 ||
11663753Seric 		     strcmp(fvalue, e->e_from.q_user) == 0))
11711414Seric 			return (hi->hi_flags);
11811414Seric 	}
11911414Seric 
12014784Seric 	/* delete default value for this header */
12155012Seric 	for (hp = &e->e_header; (h = *hp) != NULL; hp = &h->h_link)
12214784Seric 	{
12359579Seric 		if (strcasecmp(fname, h->h_field) == 0 &&
12414784Seric 		    bitset(H_DEFAULT, h->h_flags) &&
12514784Seric 		    !bitset(H_FORCE, h->h_flags))
12614784Seric 			h->h_value = NULL;
12714784Seric 	}
12814784Seric 
12913012Seric 	/* create a new node */
13013012Seric 	h = (HDR *) xalloc(sizeof *h);
13113012Seric 	h->h_field = newstr(fname);
132*64134Seric 	h->h_value = newstr(fvalue);
13313012Seric 	h->h_link = NULL;
13423117Seric 	bcopy((char *) mopts, (char *) h->h_mflags, sizeof mopts);
13513012Seric 	*hp = h;
1368066Seric 	h->h_flags = hi->hi_flags;
1374091Seric 	if (def)
1384091Seric 		h->h_flags |= H_DEFAULT;
1399059Seric 	if (cond)
1409059Seric 		h->h_flags |= H_CHECK;
1414091Seric 
1425937Seric 	/* hack to see if this is a new format message */
1438095Seric 	if (!def && bitset(H_RCPT|H_FROM, h->h_flags) &&
14456795Seric 	    (strchr(fvalue, ',') != NULL || strchr(fvalue, '(') != NULL ||
14556795Seric 	     strchr(fvalue, '<') != NULL || strchr(fvalue, ';') != NULL))
1468089Seric 	{
14755012Seric 		e->e_flags &= ~EF_OLDSTYLE;
1488089Seric 	}
1495937Seric 
1504091Seric 	return (h->h_flags);
1514091Seric }
1524091Seric /*
1536980Seric **  ADDHEADER -- add a header entry to the end of the queue.
1546980Seric **
1556980Seric **	This bypasses the special checking of chompheader.
1566980Seric **
1576980Seric **	Parameters:
15859579Seric **		field -- the name of the header field.
15958789Seric **		value -- the value of the field.
1606980Seric **		e -- the envelope to add them to.
1616980Seric **
1626980Seric **	Returns:
1636980Seric **		none.
1646980Seric **
1656980Seric **	Side Effects:
1666980Seric **		adds the field on the list of headers for this envelope.
1676980Seric */
1686980Seric 
1696980Seric addheader(field, value, e)
1706980Seric 	char *field;
1716980Seric 	char *value;
1726980Seric 	ENVELOPE *e;
1736980Seric {
1746980Seric 	register HDR *h;
1756980Seric 	register struct hdrinfo *hi;
1766980Seric 	HDR **hp;
1776980Seric 
1786980Seric 	/* find info struct */
1796980Seric 	for (hi = HdrInfo; hi->hi_field != NULL; hi++)
1806980Seric 	{
18159579Seric 		if (strcasecmp(field, hi->hi_field) == 0)
1826980Seric 			break;
1836980Seric 	}
1846980Seric 
1856980Seric 	/* find current place in list -- keep back pointer? */
1866980Seric 	for (hp = &e->e_header; (h = *hp) != NULL; hp = &h->h_link)
1876980Seric 	{
18859579Seric 		if (strcasecmp(field, h->h_field) == 0)
1896980Seric 			break;
1906980Seric 	}
1916980Seric 
1926980Seric 	/* allocate space for new header */
1936980Seric 	h = (HDR *) xalloc(sizeof *h);
1946980Seric 	h->h_field = field;
1956980Seric 	h->h_value = newstr(value);
1967368Seric 	h->h_link = *hp;
1976980Seric 	h->h_flags = hi->hi_flags | H_DEFAULT;
19810689Seric 	clrbitmap(h->h_mflags);
1996980Seric 	*hp = h;
2006980Seric }
2016980Seric /*
2024091Seric **  HVALUE -- return value of a header.
2034091Seric **
2044091Seric **	Only "real" fields (i.e., ones that have not been supplied
2054091Seric **	as a default) are used.
2064091Seric **
2074091Seric **	Parameters:
2084091Seric **		field -- the field name.
20955012Seric **		e -- the envelope containing the header.
2104091Seric **
2114091Seric **	Returns:
2124091Seric **		pointer to the value part.
2134091Seric **		NULL if not found.
2144091Seric **
2154091Seric **	Side Effects:
2169382Seric **		none.
2174091Seric */
2184091Seric 
2194091Seric char *
22055012Seric hvalue(field, e)
2214091Seric 	char *field;
22255012Seric 	register ENVELOPE *e;
2234091Seric {
2244091Seric 	register HDR *h;
2254091Seric 
22655012Seric 	for (h = e->e_header; h != NULL; h = h->h_link)
2274091Seric 	{
22859579Seric 		if (!bitset(H_DEFAULT, h->h_flags) &&
22959579Seric 		    strcasecmp(h->h_field, field) == 0)
2304091Seric 			return (h->h_value);
2314091Seric 	}
2324091Seric 	return (NULL);
2334091Seric }
2344091Seric /*
2354091Seric **  ISHEADER -- predicate telling if argument is a header.
2364091Seric **
2374319Seric **	A line is a header if it has a single word followed by
2384319Seric **	optional white space followed by a colon.
2394319Seric **
2404091Seric **	Parameters:
2414091Seric **		s -- string to check for possible headerness.
2424091Seric **
2434091Seric **	Returns:
2444091Seric **		TRUE if s is a header.
2454091Seric **		FALSE otherwise.
2464091Seric **
2474091Seric **	Side Effects:
2484091Seric **		none.
2494091Seric */
2504091Seric 
2514091Seric bool
2524091Seric isheader(s)
2534091Seric 	register char *s;
2544091Seric {
2559382Seric 	while (*s > ' ' && *s != ':' && *s != '\0')
2564091Seric 		s++;
2579382Seric 
2589382Seric 	/* following technically violates RFC822 */
25958050Seric 	while (isascii(*s) && isspace(*s))
2604091Seric 		s++;
2619382Seric 
2624091Seric 	return (*s == ':');
2634091Seric }
2645919Seric /*
2657783Seric **  EATHEADER -- run through the stored header and extract info.
2667783Seric **
2677783Seric **	Parameters:
2689382Seric **		e -- the envelope to process.
26958929Seric **		full -- if set, do full processing (e.g., compute
27058929Seric **			message priority).
2717783Seric **
2727783Seric **	Returns:
2737783Seric **		none.
2747783Seric **
2757783Seric **	Side Effects:
2767783Seric **		Sets a bunch of global variables from information
2779382Seric **			in the collected header.
2789382Seric **		Aborts the message if the hop count is exceeded.
2797783Seric */
2807783Seric 
28158929Seric eatheader(e, full)
2829382Seric 	register ENVELOPE *e;
28358929Seric 	bool full;
2847783Seric {
2857783Seric 	register HDR *h;
2867783Seric 	register char *p;
2879382Seric 	int hopcnt = 0;
28857208Seric 	char *msgid;
28958688Seric 	char buf[MAXLINE];
2907783Seric 
29158688Seric 	/*
29258688Seric 	**  Set up macros for possible expansion in headers.
29358688Seric 	*/
29458688Seric 
29558704Seric 	define('f', e->e_sender, e);
29658704Seric 	define('g', e->e_sender, e);
29758688Seric 
2989382Seric 	if (tTd(32, 1))
2999382Seric 		printf("----- collected header -----\n");
30057208Seric 	msgid = "<none>";
3019382Seric 	for (h = e->e_header; h != NULL; h = h->h_link)
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))
31559579Seric 			printf("%s: %s\n", 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 		{
32663839Seric 			int saveflags = e->e_flags;
32763839Seric 
32858082Seric 			(void) sendtolist(h->h_value, (ADDRESS *) NULL,
32958082Seric 					  &e->e_sendqueue, e);
33063839Seric 
33163839Seric 			/* delete fatal errors generated by this address */
33263839Seric 			if (!bitset(EF_FATALERRS, saveflags))
33363839Seric 				e->e_flags &= ~EF_FATALERRS;
33411414Seric 		}
33511414Seric 
33657208Seric 		/* save the message-id for logging */
33758929Seric 		if (full && h->h_value != NULL &&
33859579Seric 		    strcasecmp(h->h_field, "message-id") == 0)
33911290Seric 		{
34057208Seric 			msgid = h->h_value;
34159859Seric 			while (isascii(*msgid) && isspace(*msgid))
34259859Seric 				msgid++;
34311290Seric 		}
34457359Seric 
34557359Seric 		/* see if this is a return-receipt header */
34657359Seric 		if (bitset(H_RECEIPTTO, h->h_flags))
34757359Seric 			e->e_receiptto = h->h_value;
34857359Seric 
34957359Seric 		/* see if this is an errors-to header */
35061104Seric 		if (UseErrorsTo && bitset(H_ERRORSTO, h->h_flags))
35158082Seric 			(void) sendtolist(h->h_value, (ADDRESS *) NULL,
35258082Seric 					  &e->e_errorqueue, e);
3539382Seric 	}
3549382Seric 	if (tTd(32, 1))
3557783Seric 		printf("----------------------------\n");
3567783Seric 
35758145Seric 	/* if we are just verifying (that is, sendmail -t -bv), drop out now */
35858145Seric 	if (OpMode == MD_VERIFY)
35958145Seric 		return;
36058145Seric 
3619382Seric 	/* store hop count */
3629382Seric 	if (hopcnt > e->e_hopcount)
3639382Seric 		e->e_hopcount = hopcnt;
3649382Seric 
3657783Seric 	/* message priority */
36655012Seric 	p = hvalue("precedence", e);
3679382Seric 	if (p != NULL)
3689382Seric 		e->e_class = priencode(p);
36958929Seric 	if (full)
37025013Seric 		e->e_msgpriority = e->e_msgsize
37124981Seric 				 - e->e_class * WkClassFact
37224981Seric 				 + e->e_nrcpts * WkRecipFact;
3737783Seric 
3747783Seric 	/* full name of from person */
37555012Seric 	p = hvalue("full-name", e);
3767783Seric 	if (p != NULL)
3779382Seric 		define('x', p, e);
3787783Seric 
3797783Seric 	/* date message originated */
38055012Seric 	p = hvalue("posted-date", e);
3817783Seric 	if (p == NULL)
38255012Seric 		p = hvalue("date", e);
3837783Seric 	if (p != NULL)
3849382Seric 		define('a', p, e);
38511290Seric 
38611290Seric 	/*
38711290Seric 	**  Log collection information.
38811290Seric 	*/
38911290Seric 
39011290Seric # ifdef LOG
39158929Seric 	if (full && LogLevel > 4)
39211290Seric 	{
39357359Seric 		char *name;
39460575Seric 		register char *sbp;
39557232Seric 		char hbuf[MAXNAME];
39657359Seric 		char sbuf[MAXLINE];
39736230Skarels 
39858667Seric 		if (bitset(EF_RESPONSE, e->e_flags))
39958667Seric 			name = "[RESPONSE]";
40058951Seric 		else if ((name = macvalue('_', e)) != NULL)
40158951Seric 			;
40258667Seric 		else if (RealHostName[0] == '[')
40336230Skarels 			name = RealHostName;
40436230Skarels 		else
40557359Seric 		{
40657359Seric 			name = hbuf;
40758798Seric 			(void) sprintf(hbuf, "%.80s", RealHostName);
40858798Seric 			if (RealHostAddr.sa.sa_family != 0)
40958798Seric 			{
41058798Seric 				p = &hbuf[strlen(hbuf)];
41158798Seric 				(void) sprintf(p, " (%s)",
41258798Seric 					anynet_ntoa(&RealHostAddr));
41358798Seric 			}
41457359Seric 		}
41557359Seric 
41657359Seric 		/* some versions of syslog only take 5 printf args */
41760575Seric 		sbp = sbuf;
41860575Seric 		sprintf(sbp, "from=%.200s, size=%ld, class=%d, pri=%ld, nrcpts=%d, msgid=%.100s",
41958558Seric 		    e->e_from.q_paddr, e->e_msgsize, e->e_class,
42057589Seric 		    e->e_msgpriority, e->e_nrcpts, msgid);
42160575Seric 		sbp += strlen(sbp);
42260575Seric 		if (e->e_bodytype != NULL)
42360575Seric 		{
42460575Seric 			(void) sprintf(sbp, ", bodytype=%.20s", e->e_bodytype);
42560575Seric 			sbp += strlen(sbp);
42660575Seric 		}
42760575Seric 		p = macvalue('r', e);
42860575Seric 		if (p != NULL)
42960575Seric 			(void) sprintf(sbp, ", proto=%.20s", p);
43058686Seric 		syslog(LOG_INFO, "%s: %s, relay=%s",
43157359Seric 		    e->e_id, sbuf, name);
43211290Seric 	}
43356795Seric # endif /* LOG */
4347783Seric }
4357783Seric /*
4367783Seric **  PRIENCODE -- encode external priority names into internal values.
4377783Seric **
4387783Seric **	Parameters:
4397783Seric **		p -- priority in ascii.
4407783Seric **
4417783Seric **	Returns:
4427783Seric **		priority as a numeric level.
4437783Seric **
4447783Seric **	Side Effects:
4457783Seric **		none.
4467783Seric */
4477783Seric 
4487783Seric priencode(p)
4497783Seric 	char *p;
4507783Seric {
4518253Seric 	register int i;
4527783Seric 
4538253Seric 	for (i = 0; i < NumPriorities; i++)
4547783Seric 	{
45533725Sbostic 		if (!strcasecmp(p, Priorities[i].pri_name))
4568253Seric 			return (Priorities[i].pri_val);
4577783Seric 	}
4588253Seric 
4598253Seric 	/* unknown priority */
4608253Seric 	return (0);
4617783Seric }
4627783Seric /*
4637890Seric **  CRACKADDR -- parse an address and turn it into a macro
4647783Seric **
4657783Seric **	This doesn't actually parse the address -- it just extracts
4667783Seric **	it and replaces it with "$g".  The parse is totally ad hoc
4677783Seric **	and isn't even guaranteed to leave something syntactically
4687783Seric **	identical to what it started with.  However, it does leave
4697783Seric **	something semantically identical.
4707783Seric **
47151379Seric **	This algorithm has been cleaned up to handle a wider range
47251379Seric **	of cases -- notably quoted and backslash escaped strings.
47351379Seric **	This modification makes it substantially better at preserving
47451379Seric **	the original syntax.
4757783Seric **
4767783Seric **	Parameters:
4777890Seric **		addr -- the address to be cracked.
4787783Seric **
4797783Seric **	Returns:
4807783Seric **		a pointer to the new version.
4817783Seric **
4827783Seric **	Side Effects:
4837890Seric **		none.
4847783Seric **
4857783Seric **	Warning:
4867783Seric **		The return value is saved in local storage and should
4877783Seric **		be copied if it is to be reused.
4887783Seric */
4897783Seric 
4907783Seric char *
4917890Seric crackaddr(addr)
4927890Seric 	register char *addr;
4937783Seric {
4947783Seric 	register char *p;
49551379Seric 	register char c;
49651379Seric 	int cmtlev;
49756764Seric 	int realcmtlev;
49856764Seric 	int anglelev, realanglelev;
49951379Seric 	int copylev;
50051379Seric 	bool qmode;
50156764Seric 	bool realqmode;
50256764Seric 	bool skipping;
50351379Seric 	bool putgmac = FALSE;
50456735Seric 	bool quoteit = FALSE;
50551379Seric 	register char *bp;
50656764Seric 	char *buflim;
5077783Seric 	static char buf[MAXNAME];
5087783Seric 
5097783Seric 	if (tTd(33, 1))
5107890Seric 		printf("crackaddr(%s)\n", addr);
5117783Seric 
5128082Seric 	/* strip leading spaces */
51358050Seric 	while (*addr != '\0' && isascii(*addr) && isspace(*addr))
5148082Seric 		addr++;
5158082Seric 
5167783Seric 	/*
51751379Seric 	**  Start by assuming we have no angle brackets.  This will be
51851379Seric 	**  adjusted later if we find them.
5197783Seric 	*/
5207783Seric 
52151379Seric 	bp = buf;
52256764Seric 	buflim = &buf[sizeof buf - 5];
52351379Seric 	p = addr;
52456764Seric 	copylev = anglelev = realanglelev = cmtlev = realcmtlev = 0;
52556764Seric 	qmode = realqmode = FALSE;
52651379Seric 
52751379Seric 	while ((c = *p++) != '\0')
5287783Seric 	{
52956764Seric 		/*
53056764Seric 		**  If the buffer is overful, go into a special "skipping"
53156764Seric 		**  mode that tries to keep legal syntax but doesn't actually
53256764Seric 		**  output things.
53356764Seric 		*/
5347783Seric 
53556764Seric 		skipping = bp >= buflim;
53656735Seric 
53756764Seric 		if (copylev > 0 && !skipping)
53856764Seric 			*bp++ = c;
53956735Seric 
54051379Seric 		/* check for backslash escapes */
54151379Seric 		if (c == '\\')
5427783Seric 		{
54358890Seric 			/* arrange to quote the address */
54458890Seric 			if (cmtlev <= 0 && !qmode)
54558890Seric 				quoteit = TRUE;
54658890Seric 
54751379Seric 			if ((c = *p++) == '\0')
5487783Seric 			{
54951379Seric 				/* too far */
55051379Seric 				p--;
55151379Seric 				goto putg;
5527783Seric 			}
55356764Seric 			if (copylev > 0 && !skipping)
55451379Seric 				*bp++ = c;
55551379Seric 			goto putg;
5567783Seric 		}
5577783Seric 
55851379Seric 		/* check for quoted strings */
55951379Seric 		if (c == '"')
5607783Seric 		{
56151379Seric 			qmode = !qmode;
56256764Seric 			if (copylev > 0 && !skipping)
56356764Seric 				realqmode = !realqmode;
56451379Seric 			continue;
5657783Seric 		}
56651379Seric 		if (qmode)
56751379Seric 			goto putg;
5687783Seric 
56951379Seric 		/* check for comments */
57051379Seric 		if (c == '(')
5717783Seric 		{
57251379Seric 			cmtlev++;
57356764Seric 
57456764Seric 			/* allow space for closing paren */
57556764Seric 			if (!skipping)
57656764Seric 			{
57756764Seric 				buflim--;
57856764Seric 				realcmtlev++;
57956764Seric 				if (copylev++ <= 0)
58056764Seric 				{
58156764Seric 					*bp++ = ' ';
58256764Seric 					*bp++ = c;
58356764Seric 				}
58456764Seric 			}
58551379Seric 		}
58651379Seric 		if (cmtlev > 0)
58751379Seric 		{
58851379Seric 			if (c == ')')
5897783Seric 			{
59051379Seric 				cmtlev--;
59151379Seric 				copylev--;
59256764Seric 				if (!skipping)
59356764Seric 				{
59456764Seric 					realcmtlev--;
59556764Seric 					buflim++;
59656764Seric 				}
5977783Seric 			}
5987783Seric 			continue;
5997783Seric 		}
60056764Seric 		else if (c == ')')
60156764Seric 		{
60256764Seric 			/* syntax error: unmatched ) */
60356764Seric 			if (!skipping)
60456764Seric 				bp--;
60556764Seric 		}
6067783Seric 
60756764Seric 
60856764Seric 		/* check for characters that may have to be quoted */
60957175Seric 		if (strchr(".'@,;:\\()", c) != NULL)
61056764Seric 		{
61156764Seric 			/*
61256764Seric 			**  If these occur as the phrase part of a <>
61356764Seric 			**  construct, but are not inside of () or already
61456764Seric 			**  quoted, they will have to be quoted.  Note that
61556764Seric 			**  now (but don't actually do the quoting).
61656764Seric 			*/
61756764Seric 
61856764Seric 			if (cmtlev <= 0 && !qmode)
61956764Seric 				quoteit = TRUE;
62056764Seric 		}
62156764Seric 
62251379Seric 		/* check for angle brackets */
62351379Seric 		if (c == '<')
62451379Seric 		{
62556735Seric 			register char *q;
62656735Seric 
62751379Seric 			/* oops -- have to change our mind */
62856764Seric 			anglelev++;
62956764Seric 			if (!skipping)
63056764Seric 				realanglelev++;
63156764Seric 
63256735Seric 			bp = buf;
63356735Seric 			if (quoteit)
63456735Seric 			{
63556735Seric 				*bp++ = '"';
63656735Seric 
63756735Seric 				/* back up over the '<' and any spaces */
63856735Seric 				--p;
63958050Seric 				while (isascii(*--p) && isspace(*p))
64056735Seric 					continue;
64156735Seric 				p++;
64256735Seric 			}
64356735Seric 			for (q = addr; q < p; )
64456735Seric 			{
64556735Seric 				c = *q++;
64656764Seric 				if (bp < buflim)
64756764Seric 				{
64856764Seric 					if (quoteit && c == '"')
64956764Seric 						*bp++ = '\\';
65056764Seric 					*bp++ = c;
65156764Seric 				}
65256735Seric 			}
65356735Seric 			if (quoteit)
65456735Seric 			{
65556735Seric 				*bp++ = '"';
65656764Seric 				while ((c = *p++) != '<')
65756764Seric 				{
65856764Seric 					if (bp < buflim)
65956764Seric 						*bp++ = c;
66056764Seric 				}
66156764Seric 				*bp++ = c;
66256735Seric 			}
66351379Seric 			copylev = 0;
66456735Seric 			putgmac = quoteit = FALSE;
66551379Seric 			continue;
66651379Seric 		}
6677783Seric 
66851379Seric 		if (c == '>')
6697783Seric 		{
67056764Seric 			if (anglelev > 0)
67156764Seric 			{
67256764Seric 				anglelev--;
67356764Seric 				if (!skipping)
67456764Seric 				{
67556764Seric 					realanglelev--;
67656764Seric 					buflim++;
67756764Seric 				}
67856764Seric 			}
67956764Seric 			else if (!skipping)
68056764Seric 			{
68156764Seric 				/* syntax error: unmatched > */
68256764Seric 				if (copylev > 0)
68356764Seric 					bp--;
68456764Seric 				continue;
68556764Seric 			}
68651379Seric 			if (copylev++ <= 0)
68751379Seric 				*bp++ = c;
68851379Seric 			continue;
6897783Seric 		}
69051379Seric 
69151379Seric 		/* must be a real address character */
69251379Seric 	putg:
69351379Seric 		if (copylev <= 0 && !putgmac)
69451379Seric 		{
69558050Seric 			*bp++ = MACROEXPAND;
69651379Seric 			*bp++ = 'g';
69751379Seric 			putgmac = TRUE;
69851379Seric 		}
6997783Seric 	}
7007783Seric 
70156764Seric 	/* repair any syntactic damage */
70256764Seric 	if (realqmode)
70356764Seric 		*bp++ = '"';
70456764Seric 	while (realcmtlev-- > 0)
70556764Seric 		*bp++ = ')';
70656764Seric 	while (realanglelev-- > 0)
70756764Seric 		*bp++ = '>';
70851379Seric 	*bp++ = '\0';
7097783Seric 
7107783Seric 	if (tTd(33, 1))
7117944Seric 		printf("crackaddr=>`%s'\n", buf);
7127783Seric 
7137783Seric 	return (buf);
7147783Seric }
7159382Seric /*
7169382Seric **  PUTHEADER -- put the header part of a message from the in-core copy
7179382Seric **
7189382Seric **	Parameters:
7199382Seric **		fp -- file to put it on.
7209382Seric **		m -- mailer to use.
7219382Seric **		e -- envelope to use.
7229382Seric **
7239382Seric **	Returns:
7249382Seric **		none.
7259382Seric **
7269382Seric **	Side Effects:
7279382Seric **		none.
7289382Seric */
7299382Seric 
73057589Seric /*
73157589Seric  * Macro for fast max (not available in e.g. DG/UX, 386/ix).
73257589Seric  */
73357589Seric #ifndef MAX
73457589Seric # define MAX(a,b) (((a)>(b))?(a):(b))
73557589Seric #endif
73657589Seric 
73710176Seric putheader(fp, m, e)
7389382Seric 	register FILE *fp;
7399382Seric 	register MAILER *m;
7409382Seric 	register ENVELOPE *e;
7419382Seric {
74257135Seric 	char buf[MAX(MAXLINE,BUFSIZ)];
7439382Seric 	register HDR *h;
74457135Seric 	char obuf[MAXLINE];
7459382Seric 
74659882Seric 	if (tTd(34, 1))
74759882Seric 		printf("--- putheader, mailer = %s ---\n", m->m_name);
74859882Seric 
7499382Seric 	for (h = e->e_header; h != NULL; h = h->h_link)
7509382Seric 	{
7519382Seric 		register char *p;
75210689Seric 		extern bool bitintersect();
7539382Seric 
75459882Seric 		if (tTd(34, 11))
75559882Seric 		{
75659882Seric 			printf("  %s: ", h->h_field);
75759882Seric 			xputs(h->h_value);
75859882Seric 		}
75959882Seric 
7609382Seric 		if (bitset(H_CHECK|H_ACHECK, h->h_flags) &&
76110689Seric 		    !bitintersect(h->h_mflags, m->m_flags))
76259882Seric 		{
76359882Seric 			if (tTd(34, 11))
76459882Seric 				printf(" (skipped)\n");
7659382Seric 			continue;
76659882Seric 		}
7679382Seric 
76811414Seric 		/* handle Resent-... headers specially */
76911414Seric 		if (bitset(H_RESENT, h->h_flags) && !bitset(EF_RESENT, e->e_flags))
77059882Seric 		{
77159882Seric 			if (tTd(34, 11))
77259882Seric 				printf(" (skipped (resent))\n");
77311414Seric 			continue;
77459882Seric 		}
77559882Seric 		if (tTd(34, 11))
77659882Seric 			printf("\n");
77711414Seric 
7789382Seric 		p = h->h_value;
7799382Seric 		if (bitset(H_DEFAULT, h->h_flags))
7809382Seric 		{
7819382Seric 			/* macro expand value if generated internally */
7829382Seric 			expand(p, buf, &buf[sizeof buf], e);
7839382Seric 			p = buf;
7849382Seric 			if (p == NULL || *p == '\0')
7859382Seric 				continue;
7869382Seric 		}
7879382Seric 
7889382Seric 		if (bitset(H_FROM|H_RCPT, h->h_flags))
7899382Seric 		{
7909382Seric 			/* address field */
7919382Seric 			bool oldstyle = bitset(EF_OLDSTYLE, e->e_flags);
7929382Seric 
7939382Seric 			if (bitset(H_FROM, h->h_flags))
7949382Seric 				oldstyle = FALSE;
79555012Seric 			commaize(h, p, fp, oldstyle, m, e);
7969382Seric 		}
7979382Seric 		else
7989382Seric 		{
7999382Seric 			/* vanilla header line */
80012159Seric 			register char *nlp;
80112159Seric 
80259579Seric 			(void) sprintf(obuf, "%s: ", h->h_field);
80356795Seric 			while ((nlp = strchr(p, '\n')) != NULL)
80412159Seric 			{
80512159Seric 				*nlp = '\0';
80612159Seric 				(void) strcat(obuf, p);
80712159Seric 				*nlp = '\n';
80812159Seric 				putline(obuf, fp, m);
80912159Seric 				p = ++nlp;
81012161Seric 				obuf[0] = '\0';
81112159Seric 			}
81212159Seric 			(void) strcat(obuf, p);
81310176Seric 			putline(obuf, fp, m);
8149382Seric 		}
8159382Seric 	}
8169382Seric }
8179382Seric /*
8189382Seric **  COMMAIZE -- output a header field, making a comma-translated list.
8199382Seric **
8209382Seric **	Parameters:
8219382Seric **		h -- the header field to output.
8229382Seric **		p -- the value to put in it.
8239382Seric **		fp -- file to put it to.
8249382Seric **		oldstyle -- TRUE if this is an old style header.
8259382Seric **		m -- a pointer to the mailer descriptor.  If NULL,
8269382Seric **			don't transform the name at all.
82755012Seric **		e -- the envelope containing the message.
8289382Seric **
8299382Seric **	Returns:
8309382Seric **		none.
8319382Seric **
8329382Seric **	Side Effects:
8339382Seric **		outputs "p" to file "fp".
8349382Seric */
8359382Seric 
83655012Seric commaize(h, p, fp, oldstyle, m, e)
8379382Seric 	register HDR *h;
8389382Seric 	register char *p;
8399382Seric 	FILE *fp;
8409382Seric 	bool oldstyle;
8419382Seric 	register MAILER *m;
84255012Seric 	register ENVELOPE *e;
8439382Seric {
8449382Seric 	register char *obp;
8459382Seric 	int opos;
8469382Seric 	bool firstone = TRUE;
84711157Seric 	char obuf[MAXLINE + 3];
8489382Seric 
8499382Seric 	/*
8509382Seric 	**  Output the address list translated by the
8519382Seric 	**  mailer and with commas.
8529382Seric 	*/
8539382Seric 
8549382Seric 	if (tTd(14, 2))
8559382Seric 		printf("commaize(%s: %s)\n", h->h_field, p);
8569382Seric 
8579382Seric 	obp = obuf;
85859579Seric 	(void) sprintf(obp, "%s: ", h->h_field);
8599382Seric 	opos = strlen(h->h_field) + 2;
8609382Seric 	obp += opos;
8619382Seric 
8629382Seric 	/*
8639382Seric 	**  Run through the list of values.
8649382Seric 	*/
8659382Seric 
8669382Seric 	while (*p != '\0')
8679382Seric 	{
8689382Seric 		register char *name;
86954983Seric 		register int c;
8709382Seric 		char savechar;
87159163Seric 		int flags;
87259163Seric 		auto int stat;
8739382Seric 
8749382Seric 		/*
8759382Seric 		**  Find the end of the name.  New style names
8769382Seric 		**  end with a comma, old style names end with
8779382Seric 		**  a space character.  However, spaces do not
8789382Seric 		**  necessarily delimit an old-style name -- at
8799382Seric 		**  signs mean keep going.
8809382Seric 		*/
8819382Seric 
8829382Seric 		/* find end of name */
88358050Seric 		while ((isascii(*p) && isspace(*p)) || *p == ',')
8849382Seric 			p++;
8859382Seric 		name = p;
8869382Seric 		for (;;)
8879382Seric 		{
88858333Seric 			auto char *oldp;
88916909Seric 			char pvpbuf[PSBUFSIZE];
8909382Seric 
89158333Seric 			(void) prescan(p, oldstyle ? ' ' : ',', pvpbuf, &oldp);
89258333Seric 			p = oldp;
8939382Seric 
8949382Seric 			/* look to see if we have an at sign */
89558050Seric 			while (*p != '\0' && isascii(*p) && isspace(*p))
8969382Seric 				p++;
8979382Seric 
89858170Seric 			if (*p != '@')
8999382Seric 			{
9009382Seric 				p = oldp;
9019382Seric 				break;
9029382Seric 			}
9039382Seric 			p += *p == '@' ? 1 : 2;
90458050Seric 			while (*p != '\0' && isascii(*p) && isspace(*p))
9059382Seric 				p++;
9069382Seric 		}
9079382Seric 		/* at the end of one complete name */
9089382Seric 
9099382Seric 		/* strip off trailing white space */
91058050Seric 		while (p >= name &&
91158050Seric 		       ((isascii(*p) && isspace(*p)) || *p == ',' || *p == '\0'))
9129382Seric 			p--;
9139382Seric 		if (++p == name)
9149382Seric 			continue;
9159382Seric 		savechar = *p;
9169382Seric 		*p = '\0';
9179382Seric 
9189382Seric 		/* translate the name to be relative */
91959163Seric 		flags = RF_HEADERADDR|RF_ADDDOMAIN;
92059163Seric 		if (bitset(H_FROM, h->h_flags))
92159163Seric 			flags |= RF_SENDERADDR;
92259163Seric 		stat = EX_OK;
92359163Seric 		name = remotename(name, m, flags, &stat, e);
9249382Seric 		if (*name == '\0')
9259382Seric 		{
9269382Seric 			*p = savechar;
9279382Seric 			continue;
9289382Seric 		}
9299382Seric 
9309382Seric 		/* output the name with nice formatting */
93154983Seric 		opos += strlen(name);
9329382Seric 		if (!firstone)
9339382Seric 			opos += 2;
9349382Seric 		if (opos > 78 && !firstone)
9359382Seric 		{
93610178Seric 			(void) strcpy(obp, ",\n");
93710176Seric 			putline(obuf, fp, m);
9389382Seric 			obp = obuf;
9399382Seric 			(void) sprintf(obp, "        ");
94010161Seric 			opos = strlen(obp);
94110161Seric 			obp += opos;
94254983Seric 			opos += strlen(name);
9439382Seric 		}
9449382Seric 		else if (!firstone)
9459382Seric 		{
9469382Seric 			(void) sprintf(obp, ", ");
9479382Seric 			obp += 2;
9489382Seric 		}
9499382Seric 
95054983Seric 		while ((c = *name++) != '\0' && obp < &obuf[MAXLINE])
95154983Seric 			*obp++ = c;
9529382Seric 		firstone = FALSE;
9539382Seric 		*p = savechar;
9549382Seric 	}
9559382Seric 	(void) strcpy(obp, "\n");
95610176Seric 	putline(obuf, fp, m);
9579382Seric }
9589382Seric /*
95958170Seric **  COPYHEADER -- copy header list
9609382Seric **
96158170Seric **	This routine is the equivalent of newstr for header lists
96258170Seric **
9639382Seric **	Parameters:
96458170Seric **		header -- list of header structures to copy.
9659382Seric **
9669382Seric **	Returns:
96758170Seric **		a copy of 'header'.
9689382Seric **
9699382Seric **	Side Effects:
9709382Seric **		none.
9719382Seric */
9729382Seric 
97358170Seric HDR *
97458170Seric copyheader(header)
97558170Seric 	register HDR *header;
9769382Seric {
97758170Seric 	register HDR *newhdr;
97858170Seric 	HDR *ret;
97958170Seric 	register HDR **tail = &ret;
9809382Seric 
98158170Seric 	while (header != NULL)
98258170Seric 	{
98358170Seric 		newhdr = (HDR *) xalloc(sizeof(HDR));
98458170Seric 		STRUCTCOPY(*header, *newhdr);
98558170Seric 		*tail = newhdr;
98658170Seric 		tail = &newhdr->h_link;
98758170Seric 		header = header->h_link;
98858170Seric 	}
98958170Seric 	*tail = NULL;
99058170Seric 
99158170Seric 	return ret;
9929382Seric }
993