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*63839Seric static char sccsid[] = "@(#)headers.c	8.3 (Berkeley) 07/16/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);
13213012Seric 	h->h_value = NULL;
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 	if (h->h_value != NULL)
1429351Seric 		free((char *) h->h_value);
1438066Seric 	h->h_value = newstr(fvalue);
1444091Seric 
1455937Seric 	/* hack to see if this is a new format message */
1468095Seric 	if (!def && bitset(H_RCPT|H_FROM, h->h_flags) &&
14756795Seric 	    (strchr(fvalue, ',') != NULL || strchr(fvalue, '(') != NULL ||
14856795Seric 	     strchr(fvalue, '<') != NULL || strchr(fvalue, ';') != NULL))
1498089Seric 	{
15055012Seric 		e->e_flags &= ~EF_OLDSTYLE;
1518089Seric 	}
1525937Seric 
1534091Seric 	return (h->h_flags);
1544091Seric }
1554091Seric /*
1566980Seric **  ADDHEADER -- add a header entry to the end of the queue.
1576980Seric **
1586980Seric **	This bypasses the special checking of chompheader.
1596980Seric **
1606980Seric **	Parameters:
16159579Seric **		field -- the name of the header field.
16258789Seric **		value -- the value of the field.
1636980Seric **		e -- the envelope to add them to.
1646980Seric **
1656980Seric **	Returns:
1666980Seric **		none.
1676980Seric **
1686980Seric **	Side Effects:
1696980Seric **		adds the field on the list of headers for this envelope.
1706980Seric */
1716980Seric 
1726980Seric addheader(field, value, e)
1736980Seric 	char *field;
1746980Seric 	char *value;
1756980Seric 	ENVELOPE *e;
1766980Seric {
1776980Seric 	register HDR *h;
1786980Seric 	register struct hdrinfo *hi;
1796980Seric 	HDR **hp;
1806980Seric 
1816980Seric 	/* find info struct */
1826980Seric 	for (hi = HdrInfo; hi->hi_field != NULL; hi++)
1836980Seric 	{
18459579Seric 		if (strcasecmp(field, hi->hi_field) == 0)
1856980Seric 			break;
1866980Seric 	}
1876980Seric 
1886980Seric 	/* find current place in list -- keep back pointer? */
1896980Seric 	for (hp = &e->e_header; (h = *hp) != NULL; hp = &h->h_link)
1906980Seric 	{
19159579Seric 		if (strcasecmp(field, h->h_field) == 0)
1926980Seric 			break;
1936980Seric 	}
1946980Seric 
1956980Seric 	/* allocate space for new header */
1966980Seric 	h = (HDR *) xalloc(sizeof *h);
1976980Seric 	h->h_field = field;
1986980Seric 	h->h_value = newstr(value);
1997368Seric 	h->h_link = *hp;
2006980Seric 	h->h_flags = hi->hi_flags | H_DEFAULT;
20110689Seric 	clrbitmap(h->h_mflags);
2026980Seric 	*hp = h;
2036980Seric }
2046980Seric /*
2054091Seric **  HVALUE -- return value of a header.
2064091Seric **
2074091Seric **	Only "real" fields (i.e., ones that have not been supplied
2084091Seric **	as a default) are used.
2094091Seric **
2104091Seric **	Parameters:
2114091Seric **		field -- the field name.
21255012Seric **		e -- the envelope containing the header.
2134091Seric **
2144091Seric **	Returns:
2154091Seric **		pointer to the value part.
2164091Seric **		NULL if not found.
2174091Seric **
2184091Seric **	Side Effects:
2199382Seric **		none.
2204091Seric */
2214091Seric 
2224091Seric char *
22355012Seric hvalue(field, e)
2244091Seric 	char *field;
22555012Seric 	register ENVELOPE *e;
2264091Seric {
2274091Seric 	register HDR *h;
2284091Seric 
22955012Seric 	for (h = e->e_header; h != NULL; h = h->h_link)
2304091Seric 	{
23159579Seric 		if (!bitset(H_DEFAULT, h->h_flags) &&
23259579Seric 		    strcasecmp(h->h_field, field) == 0)
2334091Seric 			return (h->h_value);
2344091Seric 	}
2354091Seric 	return (NULL);
2364091Seric }
2374091Seric /*
2384091Seric **  ISHEADER -- predicate telling if argument is a header.
2394091Seric **
2404319Seric **	A line is a header if it has a single word followed by
2414319Seric **	optional white space followed by a colon.
2424319Seric **
2434091Seric **	Parameters:
2444091Seric **		s -- string to check for possible headerness.
2454091Seric **
2464091Seric **	Returns:
2474091Seric **		TRUE if s is a header.
2484091Seric **		FALSE otherwise.
2494091Seric **
2504091Seric **	Side Effects:
2514091Seric **		none.
2524091Seric */
2534091Seric 
2544091Seric bool
2554091Seric isheader(s)
2564091Seric 	register char *s;
2574091Seric {
2589382Seric 	while (*s > ' ' && *s != ':' && *s != '\0')
2594091Seric 		s++;
2609382Seric 
2619382Seric 	/* following technically violates RFC822 */
26258050Seric 	while (isascii(*s) && isspace(*s))
2634091Seric 		s++;
2649382Seric 
2654091Seric 	return (*s == ':');
2664091Seric }
2675919Seric /*
2687783Seric **  EATHEADER -- run through the stored header and extract info.
2697783Seric **
2707783Seric **	Parameters:
2719382Seric **		e -- the envelope to process.
27258929Seric **		full -- if set, do full processing (e.g., compute
27358929Seric **			message priority).
2747783Seric **
2757783Seric **	Returns:
2767783Seric **		none.
2777783Seric **
2787783Seric **	Side Effects:
2797783Seric **		Sets a bunch of global variables from information
2809382Seric **			in the collected header.
2819382Seric **		Aborts the message if the hop count is exceeded.
2827783Seric */
2837783Seric 
28458929Seric eatheader(e, full)
2859382Seric 	register ENVELOPE *e;
28658929Seric 	bool full;
2877783Seric {
2887783Seric 	register HDR *h;
2897783Seric 	register char *p;
2909382Seric 	int hopcnt = 0;
29157208Seric 	char *msgid;
29258688Seric 	char buf[MAXLINE];
2937783Seric 
29458688Seric 	/*
29558688Seric 	**  Set up macros for possible expansion in headers.
29658688Seric 	*/
29758688Seric 
29858704Seric 	define('f', e->e_sender, e);
29958704Seric 	define('g', e->e_sender, e);
30058688Seric 
3019382Seric 	if (tTd(32, 1))
3029382Seric 		printf("----- collected header -----\n");
30357208Seric 	msgid = "<none>";
3049382Seric 	for (h = e->e_header; h != NULL; h = h->h_link)
3057783Seric 	{
30658688Seric 		/* do early binding */
30758688Seric 		if (bitset(H_DEFAULT, h->h_flags) && h->h_value != NULL)
30858688Seric 		{
30958688Seric 			expand(h->h_value, buf, &buf[sizeof buf], e);
31058688Seric 			if (buf[0] != '\0')
31158688Seric 			{
31258688Seric 				h->h_value = newstr(buf);
31358688Seric 				h->h_flags &= ~H_DEFAULT;
31458688Seric 			}
31558688Seric 		}
31658688Seric 
3179382Seric 		if (tTd(32, 1))
31859579Seric 			printf("%s: %s\n", h->h_field, h->h_value);
31957359Seric 
32011414Seric 		/* count the number of times it has been processed */
3219382Seric 		if (bitset(H_TRACE, h->h_flags))
3229382Seric 			hopcnt++;
32311414Seric 
32411414Seric 		/* send to this person if we so desire */
32511414Seric 		if (GrabTo && bitset(H_RCPT, h->h_flags) &&
32611414Seric 		    !bitset(H_DEFAULT, h->h_flags) &&
32755012Seric 		    (!bitset(EF_RESENT, e->e_flags) || bitset(H_RESENT, h->h_flags)))
32811414Seric 		{
329*63839Seric 			int saveflags = e->e_flags;
330*63839Seric 
33158082Seric 			(void) sendtolist(h->h_value, (ADDRESS *) NULL,
33258082Seric 					  &e->e_sendqueue, e);
333*63839Seric 
334*63839Seric 			/* delete fatal errors generated by this address */
335*63839Seric 			if (!bitset(EF_FATALERRS, saveflags))
336*63839Seric 				e->e_flags &= ~EF_FATALERRS;
33711414Seric 		}
33811414Seric 
33957208Seric 		/* save the message-id for logging */
34058929Seric 		if (full && h->h_value != NULL &&
34159579Seric 		    strcasecmp(h->h_field, "message-id") == 0)
34211290Seric 		{
34357208Seric 			msgid = h->h_value;
34459859Seric 			while (isascii(*msgid) && isspace(*msgid))
34559859Seric 				msgid++;
34611290Seric 		}
34757359Seric 
34857359Seric 		/* see if this is a return-receipt header */
34957359Seric 		if (bitset(H_RECEIPTTO, h->h_flags))
35057359Seric 			e->e_receiptto = h->h_value;
35157359Seric 
35257359Seric 		/* see if this is an errors-to header */
35361104Seric 		if (UseErrorsTo && bitset(H_ERRORSTO, h->h_flags))
35458082Seric 			(void) sendtolist(h->h_value, (ADDRESS *) NULL,
35558082Seric 					  &e->e_errorqueue, e);
3569382Seric 	}
3579382Seric 	if (tTd(32, 1))
3587783Seric 		printf("----------------------------\n");
3597783Seric 
36058145Seric 	/* if we are just verifying (that is, sendmail -t -bv), drop out now */
36158145Seric 	if (OpMode == MD_VERIFY)
36258145Seric 		return;
36358145Seric 
3649382Seric 	/* store hop count */
3659382Seric 	if (hopcnt > e->e_hopcount)
3669382Seric 		e->e_hopcount = hopcnt;
3679382Seric 
3687783Seric 	/* message priority */
36955012Seric 	p = hvalue("precedence", e);
3709382Seric 	if (p != NULL)
3719382Seric 		e->e_class = priencode(p);
37258929Seric 	if (full)
37325013Seric 		e->e_msgpriority = e->e_msgsize
37424981Seric 				 - e->e_class * WkClassFact
37524981Seric 				 + e->e_nrcpts * WkRecipFact;
3767783Seric 
3777783Seric 	/* full name of from person */
37855012Seric 	p = hvalue("full-name", e);
3797783Seric 	if (p != NULL)
3809382Seric 		define('x', p, e);
3817783Seric 
3827783Seric 	/* date message originated */
38355012Seric 	p = hvalue("posted-date", e);
3847783Seric 	if (p == NULL)
38555012Seric 		p = hvalue("date", e);
3867783Seric 	if (p != NULL)
3879382Seric 		define('a', p, e);
38811290Seric 
38911290Seric 	/*
39011290Seric 	**  Log collection information.
39111290Seric 	*/
39211290Seric 
39311290Seric # ifdef LOG
39458929Seric 	if (full && LogLevel > 4)
39511290Seric 	{
39657359Seric 		char *name;
39760575Seric 		register char *sbp;
39857232Seric 		char hbuf[MAXNAME];
39957359Seric 		char sbuf[MAXLINE];
40036230Skarels 
40158667Seric 		if (bitset(EF_RESPONSE, e->e_flags))
40258667Seric 			name = "[RESPONSE]";
40358951Seric 		else if ((name = macvalue('_', e)) != NULL)
40458951Seric 			;
40558667Seric 		else if (RealHostName[0] == '[')
40636230Skarels 			name = RealHostName;
40736230Skarels 		else
40857359Seric 		{
40957359Seric 			name = hbuf;
41058798Seric 			(void) sprintf(hbuf, "%.80s", RealHostName);
41158798Seric 			if (RealHostAddr.sa.sa_family != 0)
41258798Seric 			{
41358798Seric 				p = &hbuf[strlen(hbuf)];
41458798Seric 				(void) sprintf(p, " (%s)",
41558798Seric 					anynet_ntoa(&RealHostAddr));
41658798Seric 			}
41757359Seric 		}
41857359Seric 
41957359Seric 		/* some versions of syslog only take 5 printf args */
42060575Seric 		sbp = sbuf;
42160575Seric 		sprintf(sbp, "from=%.200s, size=%ld, class=%d, pri=%ld, nrcpts=%d, msgid=%.100s",
42258558Seric 		    e->e_from.q_paddr, e->e_msgsize, e->e_class,
42357589Seric 		    e->e_msgpriority, e->e_nrcpts, msgid);
42460575Seric 		sbp += strlen(sbp);
42560575Seric 		if (e->e_bodytype != NULL)
42660575Seric 		{
42760575Seric 			(void) sprintf(sbp, ", bodytype=%.20s", e->e_bodytype);
42860575Seric 			sbp += strlen(sbp);
42960575Seric 		}
43060575Seric 		p = macvalue('r', e);
43160575Seric 		if (p != NULL)
43260575Seric 			(void) sprintf(sbp, ", proto=%.20s", p);
43358686Seric 		syslog(LOG_INFO, "%s: %s, relay=%s",
43457359Seric 		    e->e_id, sbuf, name);
43511290Seric 	}
43656795Seric # endif /* LOG */
4377783Seric }
4387783Seric /*
4397783Seric **  PRIENCODE -- encode external priority names into internal values.
4407783Seric **
4417783Seric **	Parameters:
4427783Seric **		p -- priority in ascii.
4437783Seric **
4447783Seric **	Returns:
4457783Seric **		priority as a numeric level.
4467783Seric **
4477783Seric **	Side Effects:
4487783Seric **		none.
4497783Seric */
4507783Seric 
4517783Seric priencode(p)
4527783Seric 	char *p;
4537783Seric {
4548253Seric 	register int i;
4557783Seric 
4568253Seric 	for (i = 0; i < NumPriorities; i++)
4577783Seric 	{
45833725Sbostic 		if (!strcasecmp(p, Priorities[i].pri_name))
4598253Seric 			return (Priorities[i].pri_val);
4607783Seric 	}
4618253Seric 
4628253Seric 	/* unknown priority */
4638253Seric 	return (0);
4647783Seric }
4657783Seric /*
4667890Seric **  CRACKADDR -- parse an address and turn it into a macro
4677783Seric **
4687783Seric **	This doesn't actually parse the address -- it just extracts
4697783Seric **	it and replaces it with "$g".  The parse is totally ad hoc
4707783Seric **	and isn't even guaranteed to leave something syntactically
4717783Seric **	identical to what it started with.  However, it does leave
4727783Seric **	something semantically identical.
4737783Seric **
47451379Seric **	This algorithm has been cleaned up to handle a wider range
47551379Seric **	of cases -- notably quoted and backslash escaped strings.
47651379Seric **	This modification makes it substantially better at preserving
47751379Seric **	the original syntax.
4787783Seric **
4797783Seric **	Parameters:
4807890Seric **		addr -- the address to be cracked.
4817783Seric **
4827783Seric **	Returns:
4837783Seric **		a pointer to the new version.
4847783Seric **
4857783Seric **	Side Effects:
4867890Seric **		none.
4877783Seric **
4887783Seric **	Warning:
4897783Seric **		The return value is saved in local storage and should
4907783Seric **		be copied if it is to be reused.
4917783Seric */
4927783Seric 
4937783Seric char *
4947890Seric crackaddr(addr)
4957890Seric 	register char *addr;
4967783Seric {
4977783Seric 	register char *p;
49851379Seric 	register char c;
49951379Seric 	int cmtlev;
50056764Seric 	int realcmtlev;
50156764Seric 	int anglelev, realanglelev;
50251379Seric 	int copylev;
50351379Seric 	bool qmode;
50456764Seric 	bool realqmode;
50556764Seric 	bool skipping;
50651379Seric 	bool putgmac = FALSE;
50756735Seric 	bool quoteit = FALSE;
50851379Seric 	register char *bp;
50956764Seric 	char *buflim;
5107783Seric 	static char buf[MAXNAME];
5117783Seric 
5127783Seric 	if (tTd(33, 1))
5137890Seric 		printf("crackaddr(%s)\n", addr);
5147783Seric 
5158082Seric 	/* strip leading spaces */
51658050Seric 	while (*addr != '\0' && isascii(*addr) && isspace(*addr))
5178082Seric 		addr++;
5188082Seric 
5197783Seric 	/*
52051379Seric 	**  Start by assuming we have no angle brackets.  This will be
52151379Seric 	**  adjusted later if we find them.
5227783Seric 	*/
5237783Seric 
52451379Seric 	bp = buf;
52556764Seric 	buflim = &buf[sizeof buf - 5];
52651379Seric 	p = addr;
52756764Seric 	copylev = anglelev = realanglelev = cmtlev = realcmtlev = 0;
52856764Seric 	qmode = realqmode = FALSE;
52951379Seric 
53051379Seric 	while ((c = *p++) != '\0')
5317783Seric 	{
53256764Seric 		/*
53356764Seric 		**  If the buffer is overful, go into a special "skipping"
53456764Seric 		**  mode that tries to keep legal syntax but doesn't actually
53556764Seric 		**  output things.
53656764Seric 		*/
5377783Seric 
53856764Seric 		skipping = bp >= buflim;
53956735Seric 
54056764Seric 		if (copylev > 0 && !skipping)
54156764Seric 			*bp++ = c;
54256735Seric 
54351379Seric 		/* check for backslash escapes */
54451379Seric 		if (c == '\\')
5457783Seric 		{
54658890Seric 			/* arrange to quote the address */
54758890Seric 			if (cmtlev <= 0 && !qmode)
54858890Seric 				quoteit = TRUE;
54958890Seric 
55051379Seric 			if ((c = *p++) == '\0')
5517783Seric 			{
55251379Seric 				/* too far */
55351379Seric 				p--;
55451379Seric 				goto putg;
5557783Seric 			}
55656764Seric 			if (copylev > 0 && !skipping)
55751379Seric 				*bp++ = c;
55851379Seric 			goto putg;
5597783Seric 		}
5607783Seric 
56151379Seric 		/* check for quoted strings */
56251379Seric 		if (c == '"')
5637783Seric 		{
56451379Seric 			qmode = !qmode;
56556764Seric 			if (copylev > 0 && !skipping)
56656764Seric 				realqmode = !realqmode;
56751379Seric 			continue;
5687783Seric 		}
56951379Seric 		if (qmode)
57051379Seric 			goto putg;
5717783Seric 
57251379Seric 		/* check for comments */
57351379Seric 		if (c == '(')
5747783Seric 		{
57551379Seric 			cmtlev++;
57656764Seric 
57756764Seric 			/* allow space for closing paren */
57856764Seric 			if (!skipping)
57956764Seric 			{
58056764Seric 				buflim--;
58156764Seric 				realcmtlev++;
58256764Seric 				if (copylev++ <= 0)
58356764Seric 				{
58456764Seric 					*bp++ = ' ';
58556764Seric 					*bp++ = c;
58656764Seric 				}
58756764Seric 			}
58851379Seric 		}
58951379Seric 		if (cmtlev > 0)
59051379Seric 		{
59151379Seric 			if (c == ')')
5927783Seric 			{
59351379Seric 				cmtlev--;
59451379Seric 				copylev--;
59556764Seric 				if (!skipping)
59656764Seric 				{
59756764Seric 					realcmtlev--;
59856764Seric 					buflim++;
59956764Seric 				}
6007783Seric 			}
6017783Seric 			continue;
6027783Seric 		}
60356764Seric 		else if (c == ')')
60456764Seric 		{
60556764Seric 			/* syntax error: unmatched ) */
60656764Seric 			if (!skipping)
60756764Seric 				bp--;
60856764Seric 		}
6097783Seric 
61056764Seric 
61156764Seric 		/* check for characters that may have to be quoted */
61257175Seric 		if (strchr(".'@,;:\\()", c) != NULL)
61356764Seric 		{
61456764Seric 			/*
61556764Seric 			**  If these occur as the phrase part of a <>
61656764Seric 			**  construct, but are not inside of () or already
61756764Seric 			**  quoted, they will have to be quoted.  Note that
61856764Seric 			**  now (but don't actually do the quoting).
61956764Seric 			*/
62056764Seric 
62156764Seric 			if (cmtlev <= 0 && !qmode)
62256764Seric 				quoteit = TRUE;
62356764Seric 		}
62456764Seric 
62551379Seric 		/* check for angle brackets */
62651379Seric 		if (c == '<')
62751379Seric 		{
62856735Seric 			register char *q;
62956735Seric 
63051379Seric 			/* oops -- have to change our mind */
63156764Seric 			anglelev++;
63256764Seric 			if (!skipping)
63356764Seric 				realanglelev++;
63456764Seric 
63556735Seric 			bp = buf;
63656735Seric 			if (quoteit)
63756735Seric 			{
63856735Seric 				*bp++ = '"';
63956735Seric 
64056735Seric 				/* back up over the '<' and any spaces */
64156735Seric 				--p;
64258050Seric 				while (isascii(*--p) && isspace(*p))
64356735Seric 					continue;
64456735Seric 				p++;
64556735Seric 			}
64656735Seric 			for (q = addr; q < p; )
64756735Seric 			{
64856735Seric 				c = *q++;
64956764Seric 				if (bp < buflim)
65056764Seric 				{
65156764Seric 					if (quoteit && c == '"')
65256764Seric 						*bp++ = '\\';
65356764Seric 					*bp++ = c;
65456764Seric 				}
65556735Seric 			}
65656735Seric 			if (quoteit)
65756735Seric 			{
65856735Seric 				*bp++ = '"';
65956764Seric 				while ((c = *p++) != '<')
66056764Seric 				{
66156764Seric 					if (bp < buflim)
66256764Seric 						*bp++ = c;
66356764Seric 				}
66456764Seric 				*bp++ = c;
66556735Seric 			}
66651379Seric 			copylev = 0;
66756735Seric 			putgmac = quoteit = FALSE;
66851379Seric 			continue;
66951379Seric 		}
6707783Seric 
67151379Seric 		if (c == '>')
6727783Seric 		{
67356764Seric 			if (anglelev > 0)
67456764Seric 			{
67556764Seric 				anglelev--;
67656764Seric 				if (!skipping)
67756764Seric 				{
67856764Seric 					realanglelev--;
67956764Seric 					buflim++;
68056764Seric 				}
68156764Seric 			}
68256764Seric 			else if (!skipping)
68356764Seric 			{
68456764Seric 				/* syntax error: unmatched > */
68556764Seric 				if (copylev > 0)
68656764Seric 					bp--;
68756764Seric 				continue;
68856764Seric 			}
68951379Seric 			if (copylev++ <= 0)
69051379Seric 				*bp++ = c;
69151379Seric 			continue;
6927783Seric 		}
69351379Seric 
69451379Seric 		/* must be a real address character */
69551379Seric 	putg:
69651379Seric 		if (copylev <= 0 && !putgmac)
69751379Seric 		{
69858050Seric 			*bp++ = MACROEXPAND;
69951379Seric 			*bp++ = 'g';
70051379Seric 			putgmac = TRUE;
70151379Seric 		}
7027783Seric 	}
7037783Seric 
70456764Seric 	/* repair any syntactic damage */
70556764Seric 	if (realqmode)
70656764Seric 		*bp++ = '"';
70756764Seric 	while (realcmtlev-- > 0)
70856764Seric 		*bp++ = ')';
70956764Seric 	while (realanglelev-- > 0)
71056764Seric 		*bp++ = '>';
71151379Seric 	*bp++ = '\0';
7127783Seric 
7137783Seric 	if (tTd(33, 1))
7147944Seric 		printf("crackaddr=>`%s'\n", buf);
7157783Seric 
7167783Seric 	return (buf);
7177783Seric }
7189382Seric /*
7199382Seric **  PUTHEADER -- put the header part of a message from the in-core copy
7209382Seric **
7219382Seric **	Parameters:
7229382Seric **		fp -- file to put it on.
7239382Seric **		m -- mailer to use.
7249382Seric **		e -- envelope to use.
7259382Seric **
7269382Seric **	Returns:
7279382Seric **		none.
7289382Seric **
7299382Seric **	Side Effects:
7309382Seric **		none.
7319382Seric */
7329382Seric 
73357589Seric /*
73457589Seric  * Macro for fast max (not available in e.g. DG/UX, 386/ix).
73557589Seric  */
73657589Seric #ifndef MAX
73757589Seric # define MAX(a,b) (((a)>(b))?(a):(b))
73857589Seric #endif
73957589Seric 
74010176Seric putheader(fp, m, e)
7419382Seric 	register FILE *fp;
7429382Seric 	register MAILER *m;
7439382Seric 	register ENVELOPE *e;
7449382Seric {
74557135Seric 	char buf[MAX(MAXLINE,BUFSIZ)];
7469382Seric 	register HDR *h;
74757135Seric 	char obuf[MAXLINE];
7489382Seric 
74959882Seric 	if (tTd(34, 1))
75059882Seric 		printf("--- putheader, mailer = %s ---\n", m->m_name);
75159882Seric 
7529382Seric 	for (h = e->e_header; h != NULL; h = h->h_link)
7539382Seric 	{
7549382Seric 		register char *p;
75510689Seric 		extern bool bitintersect();
7569382Seric 
75759882Seric 		if (tTd(34, 11))
75859882Seric 		{
75959882Seric 			printf("  %s: ", h->h_field);
76059882Seric 			xputs(h->h_value);
76159882Seric 		}
76259882Seric 
7639382Seric 		if (bitset(H_CHECK|H_ACHECK, h->h_flags) &&
76410689Seric 		    !bitintersect(h->h_mflags, m->m_flags))
76559882Seric 		{
76659882Seric 			if (tTd(34, 11))
76759882Seric 				printf(" (skipped)\n");
7689382Seric 			continue;
76959882Seric 		}
7709382Seric 
77111414Seric 		/* handle Resent-... headers specially */
77211414Seric 		if (bitset(H_RESENT, h->h_flags) && !bitset(EF_RESENT, e->e_flags))
77359882Seric 		{
77459882Seric 			if (tTd(34, 11))
77559882Seric 				printf(" (skipped (resent))\n");
77611414Seric 			continue;
77759882Seric 		}
77859882Seric 		if (tTd(34, 11))
77959882Seric 			printf("\n");
78011414Seric 
7819382Seric 		p = h->h_value;
7829382Seric 		if (bitset(H_DEFAULT, h->h_flags))
7839382Seric 		{
7849382Seric 			/* macro expand value if generated internally */
7859382Seric 			expand(p, buf, &buf[sizeof buf], e);
7869382Seric 			p = buf;
7879382Seric 			if (p == NULL || *p == '\0')
7889382Seric 				continue;
7899382Seric 		}
7909382Seric 
7919382Seric 		if (bitset(H_FROM|H_RCPT, h->h_flags))
7929382Seric 		{
7939382Seric 			/* address field */
7949382Seric 			bool oldstyle = bitset(EF_OLDSTYLE, e->e_flags);
7959382Seric 
7969382Seric 			if (bitset(H_FROM, h->h_flags))
7979382Seric 				oldstyle = FALSE;
79855012Seric 			commaize(h, p, fp, oldstyle, m, e);
7999382Seric 		}
8009382Seric 		else
8019382Seric 		{
8029382Seric 			/* vanilla header line */
80312159Seric 			register char *nlp;
80412159Seric 
80559579Seric 			(void) sprintf(obuf, "%s: ", h->h_field);
80656795Seric 			while ((nlp = strchr(p, '\n')) != NULL)
80712159Seric 			{
80812159Seric 				*nlp = '\0';
80912159Seric 				(void) strcat(obuf, p);
81012159Seric 				*nlp = '\n';
81112159Seric 				putline(obuf, fp, m);
81212159Seric 				p = ++nlp;
81312161Seric 				obuf[0] = '\0';
81412159Seric 			}
81512159Seric 			(void) strcat(obuf, p);
81610176Seric 			putline(obuf, fp, m);
8179382Seric 		}
8189382Seric 	}
8199382Seric }
8209382Seric /*
8219382Seric **  COMMAIZE -- output a header field, making a comma-translated list.
8229382Seric **
8239382Seric **	Parameters:
8249382Seric **		h -- the header field to output.
8259382Seric **		p -- the value to put in it.
8269382Seric **		fp -- file to put it to.
8279382Seric **		oldstyle -- TRUE if this is an old style header.
8289382Seric **		m -- a pointer to the mailer descriptor.  If NULL,
8299382Seric **			don't transform the name at all.
83055012Seric **		e -- the envelope containing the message.
8319382Seric **
8329382Seric **	Returns:
8339382Seric **		none.
8349382Seric **
8359382Seric **	Side Effects:
8369382Seric **		outputs "p" to file "fp".
8379382Seric */
8389382Seric 
83955012Seric commaize(h, p, fp, oldstyle, m, e)
8409382Seric 	register HDR *h;
8419382Seric 	register char *p;
8429382Seric 	FILE *fp;
8439382Seric 	bool oldstyle;
8449382Seric 	register MAILER *m;
84555012Seric 	register ENVELOPE *e;
8469382Seric {
8479382Seric 	register char *obp;
8489382Seric 	int opos;
8499382Seric 	bool firstone = TRUE;
85011157Seric 	char obuf[MAXLINE + 3];
8519382Seric 
8529382Seric 	/*
8539382Seric 	**  Output the address list translated by the
8549382Seric 	**  mailer and with commas.
8559382Seric 	*/
8569382Seric 
8579382Seric 	if (tTd(14, 2))
8589382Seric 		printf("commaize(%s: %s)\n", h->h_field, p);
8599382Seric 
8609382Seric 	obp = obuf;
86159579Seric 	(void) sprintf(obp, "%s: ", h->h_field);
8629382Seric 	opos = strlen(h->h_field) + 2;
8639382Seric 	obp += opos;
8649382Seric 
8659382Seric 	/*
8669382Seric 	**  Run through the list of values.
8679382Seric 	*/
8689382Seric 
8699382Seric 	while (*p != '\0')
8709382Seric 	{
8719382Seric 		register char *name;
87254983Seric 		register int c;
8739382Seric 		char savechar;
87459163Seric 		int flags;
87559163Seric 		auto int stat;
8769382Seric 
8779382Seric 		/*
8789382Seric 		**  Find the end of the name.  New style names
8799382Seric 		**  end with a comma, old style names end with
8809382Seric 		**  a space character.  However, spaces do not
8819382Seric 		**  necessarily delimit an old-style name -- at
8829382Seric 		**  signs mean keep going.
8839382Seric 		*/
8849382Seric 
8859382Seric 		/* find end of name */
88658050Seric 		while ((isascii(*p) && isspace(*p)) || *p == ',')
8879382Seric 			p++;
8889382Seric 		name = p;
8899382Seric 		for (;;)
8909382Seric 		{
89158333Seric 			auto char *oldp;
89216909Seric 			char pvpbuf[PSBUFSIZE];
8939382Seric 
89458333Seric 			(void) prescan(p, oldstyle ? ' ' : ',', pvpbuf, &oldp);
89558333Seric 			p = oldp;
8969382Seric 
8979382Seric 			/* look to see if we have an at sign */
89858050Seric 			while (*p != '\0' && isascii(*p) && isspace(*p))
8999382Seric 				p++;
9009382Seric 
90158170Seric 			if (*p != '@')
9029382Seric 			{
9039382Seric 				p = oldp;
9049382Seric 				break;
9059382Seric 			}
9069382Seric 			p += *p == '@' ? 1 : 2;
90758050Seric 			while (*p != '\0' && isascii(*p) && isspace(*p))
9089382Seric 				p++;
9099382Seric 		}
9109382Seric 		/* at the end of one complete name */
9119382Seric 
9129382Seric 		/* strip off trailing white space */
91358050Seric 		while (p >= name &&
91458050Seric 		       ((isascii(*p) && isspace(*p)) || *p == ',' || *p == '\0'))
9159382Seric 			p--;
9169382Seric 		if (++p == name)
9179382Seric 			continue;
9189382Seric 		savechar = *p;
9199382Seric 		*p = '\0';
9209382Seric 
9219382Seric 		/* translate the name to be relative */
92259163Seric 		flags = RF_HEADERADDR|RF_ADDDOMAIN;
92359163Seric 		if (bitset(H_FROM, h->h_flags))
92459163Seric 			flags |= RF_SENDERADDR;
92559163Seric 		stat = EX_OK;
92659163Seric 		name = remotename(name, m, flags, &stat, e);
9279382Seric 		if (*name == '\0')
9289382Seric 		{
9299382Seric 			*p = savechar;
9309382Seric 			continue;
9319382Seric 		}
9329382Seric 
9339382Seric 		/* output the name with nice formatting */
93454983Seric 		opos += strlen(name);
9359382Seric 		if (!firstone)
9369382Seric 			opos += 2;
9379382Seric 		if (opos > 78 && !firstone)
9389382Seric 		{
93910178Seric 			(void) strcpy(obp, ",\n");
94010176Seric 			putline(obuf, fp, m);
9419382Seric 			obp = obuf;
9429382Seric 			(void) sprintf(obp, "        ");
94310161Seric 			opos = strlen(obp);
94410161Seric 			obp += opos;
94554983Seric 			opos += strlen(name);
9469382Seric 		}
9479382Seric 		else if (!firstone)
9489382Seric 		{
9499382Seric 			(void) sprintf(obp, ", ");
9509382Seric 			obp += 2;
9519382Seric 		}
9529382Seric 
95354983Seric 		while ((c = *name++) != '\0' && obp < &obuf[MAXLINE])
95454983Seric 			*obp++ = c;
9559382Seric 		firstone = FALSE;
9569382Seric 		*p = savechar;
9579382Seric 	}
9589382Seric 	(void) strcpy(obp, "\n");
95910176Seric 	putline(obuf, fp, m);
9609382Seric }
9619382Seric /*
96258170Seric **  COPYHEADER -- copy header list
9639382Seric **
96458170Seric **	This routine is the equivalent of newstr for header lists
96558170Seric **
9669382Seric **	Parameters:
96758170Seric **		header -- list of header structures to copy.
9689382Seric **
9699382Seric **	Returns:
97058170Seric **		a copy of 'header'.
9719382Seric **
9729382Seric **	Side Effects:
9739382Seric **		none.
9749382Seric */
9759382Seric 
97658170Seric HDR *
97758170Seric copyheader(header)
97858170Seric 	register HDR *header;
9799382Seric {
98058170Seric 	register HDR *newhdr;
98158170Seric 	HDR *ret;
98258170Seric 	register HDR **tail = &ret;
9839382Seric 
98458170Seric 	while (header != NULL)
98558170Seric 	{
98658170Seric 		newhdr = (HDR *) xalloc(sizeof(HDR));
98758170Seric 		STRUCTCOPY(*header, *newhdr);
98858170Seric 		*tail = newhdr;
98958170Seric 		tail = &newhdr->h_link;
99058170Seric 		header = header->h_link;
99158170Seric 	}
99258170Seric 	*tail = NULL;
99358170Seric 
99458170Seric 	return ret;
9959382Seric }
996