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*64149Seric static char sccsid[] = "@(#)headers.c	8.7 (Berkeley) 08/08/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;
73*64149Seric 	while (isascii(*p) && isgraph(*p) && *p != ':')
74*64149Seric 		p++;
75*64149Seric 	fvalue = p;
76*64149Seric 	while (isascii(*p) && isspace(*p))
77*64149Seric 		p++;
78*64149Seric 	if (*p++ != ':')
7910118Seric 	{
8058151Seric 		syserr("553 header syntax error, line \"%s\"", line);
8110118Seric 		return (0);
8210118Seric 	}
83*64149Seric 	*fvalue = '\0';
84*64149Seric 	fvalue = p;
854091Seric 
864091Seric 	/* strip field value on front */
874091Seric 	if (*fvalue == ' ')
884091Seric 		fvalue++;
894091Seric 
904091Seric 	/* see if it is a known type */
914091Seric 	for (hi = HdrInfo; hi->hi_field != NULL; hi++)
924091Seric 	{
9359579Seric 		if (strcasecmp(hi->hi_field, fname) == 0)
944091Seric 			break;
954091Seric 	}
964091Seric 
9711414Seric 	/* see if this is a resent message */
9811930Seric 	if (!def && bitset(H_RESENT, hi->hi_flags))
9955012Seric 		e->e_flags |= EF_RESENT;
10011414Seric 
1014091Seric 	/* if this means "end of header" quit now */
1024091Seric 	if (bitset(H_EOH, hi->hi_flags))
1034091Seric 		return (hi->hi_flags);
1044091Seric 
10511414Seric 	/* drop explicit From: if same as what we would generate -- for MH */
10614784Seric 	p = "resent-from";
10755012Seric 	if (!bitset(EF_RESENT, e->e_flags))
10814784Seric 		p += 7;
10959579Seric 	if (!def && !bitset(EF_QUEUERUN, e->e_flags) && strcasecmp(fname, p) == 0)
11011414Seric 	{
11163753Seric 		if (tTd(31, 2))
11263753Seric 		{
11363753Seric 			printf("comparing header from (%s) against default (%s or %s)\n",
11463753Seric 				fvalue, e->e_from.q_paddr, e->e_from.q_user);
11563753Seric 		}
11655012Seric 		if (e->e_from.q_paddr != NULL &&
11763753Seric 		    (strcmp(fvalue, e->e_from.q_paddr) == 0 ||
11863753Seric 		     strcmp(fvalue, e->e_from.q_user) == 0))
11911414Seric 			return (hi->hi_flags);
12011414Seric 	}
12111414Seric 
12214784Seric 	/* delete default value for this header */
12355012Seric 	for (hp = &e->e_header; (h = *hp) != NULL; hp = &h->h_link)
12414784Seric 	{
12559579Seric 		if (strcasecmp(fname, h->h_field) == 0 &&
12614784Seric 		    bitset(H_DEFAULT, h->h_flags) &&
12714784Seric 		    !bitset(H_FORCE, h->h_flags))
12814784Seric 			h->h_value = NULL;
12914784Seric 	}
13014784Seric 
13113012Seric 	/* create a new node */
13213012Seric 	h = (HDR *) xalloc(sizeof *h);
13313012Seric 	h->h_field = newstr(fname);
13464134Seric 	h->h_value = newstr(fvalue);
13513012Seric 	h->h_link = NULL;
13623117Seric 	bcopy((char *) mopts, (char *) h->h_mflags, sizeof mopts);
13713012Seric 	*hp = h;
1388066Seric 	h->h_flags = hi->hi_flags;
1394091Seric 	if (def)
1404091Seric 		h->h_flags |= H_DEFAULT;
1419059Seric 	if (cond)
1429059Seric 		h->h_flags |= H_CHECK;
1434091Seric 
1445937Seric 	/* hack to see if this is a new format message */
1458095Seric 	if (!def && bitset(H_RCPT|H_FROM, h->h_flags) &&
14656795Seric 	    (strchr(fvalue, ',') != NULL || strchr(fvalue, '(') != NULL ||
14756795Seric 	     strchr(fvalue, '<') != NULL || strchr(fvalue, ';') != NULL))
1488089Seric 	{
14955012Seric 		e->e_flags &= ~EF_OLDSTYLE;
1508089Seric 	}
1515937Seric 
1524091Seric 	return (h->h_flags);
1534091Seric }
1544091Seric /*
1556980Seric **  ADDHEADER -- add a header entry to the end of the queue.
1566980Seric **
1576980Seric **	This bypasses the special checking of chompheader.
1586980Seric **
1596980Seric **	Parameters:
16059579Seric **		field -- the name of the header field.
16158789Seric **		value -- the value of the field.
1626980Seric **		e -- the envelope to add them to.
1636980Seric **
1646980Seric **	Returns:
1656980Seric **		none.
1666980Seric **
1676980Seric **	Side Effects:
1686980Seric **		adds the field on the list of headers for this envelope.
1696980Seric */
1706980Seric 
1716980Seric addheader(field, value, e)
1726980Seric 	char *field;
1736980Seric 	char *value;
1746980Seric 	ENVELOPE *e;
1756980Seric {
1766980Seric 	register HDR *h;
1776980Seric 	register struct hdrinfo *hi;
1786980Seric 	HDR **hp;
1796980Seric 
1806980Seric 	/* find info struct */
1816980Seric 	for (hi = HdrInfo; hi->hi_field != NULL; hi++)
1826980Seric 	{
18359579Seric 		if (strcasecmp(field, hi->hi_field) == 0)
1846980Seric 			break;
1856980Seric 	}
1866980Seric 
1876980Seric 	/* find current place in list -- keep back pointer? */
1886980Seric 	for (hp = &e->e_header; (h = *hp) != NULL; hp = &h->h_link)
1896980Seric 	{
19059579Seric 		if (strcasecmp(field, h->h_field) == 0)
1916980Seric 			break;
1926980Seric 	}
1936980Seric 
1946980Seric 	/* allocate space for new header */
1956980Seric 	h = (HDR *) xalloc(sizeof *h);
1966980Seric 	h->h_field = field;
1976980Seric 	h->h_value = newstr(value);
1987368Seric 	h->h_link = *hp;
1996980Seric 	h->h_flags = hi->hi_flags | H_DEFAULT;
20010689Seric 	clrbitmap(h->h_mflags);
2016980Seric 	*hp = h;
2026980Seric }
2036980Seric /*
2044091Seric **  HVALUE -- return value of a header.
2054091Seric **
2064091Seric **	Only "real" fields (i.e., ones that have not been supplied
2074091Seric **	as a default) are used.
2084091Seric **
2094091Seric **	Parameters:
2104091Seric **		field -- the field name.
21155012Seric **		e -- the envelope containing the header.
2124091Seric **
2134091Seric **	Returns:
2144091Seric **		pointer to the value part.
2154091Seric **		NULL if not found.
2164091Seric **
2174091Seric **	Side Effects:
2189382Seric **		none.
2194091Seric */
2204091Seric 
2214091Seric char *
22255012Seric hvalue(field, e)
2234091Seric 	char *field;
22455012Seric 	register ENVELOPE *e;
2254091Seric {
2264091Seric 	register HDR *h;
2274091Seric 
22855012Seric 	for (h = e->e_header; h != NULL; h = h->h_link)
2294091Seric 	{
23059579Seric 		if (!bitset(H_DEFAULT, h->h_flags) &&
23159579Seric 		    strcasecmp(h->h_field, field) == 0)
2324091Seric 			return (h->h_value);
2334091Seric 	}
2344091Seric 	return (NULL);
2354091Seric }
2364091Seric /*
2374091Seric **  ISHEADER -- predicate telling if argument is a header.
2384091Seric **
2394319Seric **	A line is a header if it has a single word followed by
2404319Seric **	optional white space followed by a colon.
2414319Seric **
2424091Seric **	Parameters:
2434091Seric **		s -- string to check for possible headerness.
2444091Seric **
2454091Seric **	Returns:
2464091Seric **		TRUE if s is a header.
2474091Seric **		FALSE otherwise.
2484091Seric **
2494091Seric **	Side Effects:
2504091Seric **		none.
2514091Seric */
2524091Seric 
2534091Seric bool
2544091Seric isheader(s)
2554091Seric 	register char *s;
2564091Seric {
2579382Seric 	while (*s > ' ' && *s != ':' && *s != '\0')
2584091Seric 		s++;
2599382Seric 
2609382Seric 	/* following technically violates RFC822 */
26158050Seric 	while (isascii(*s) && isspace(*s))
2624091Seric 		s++;
2639382Seric 
2644091Seric 	return (*s == ':');
2654091Seric }
2665919Seric /*
2677783Seric **  EATHEADER -- run through the stored header and extract info.
2687783Seric **
2697783Seric **	Parameters:
2709382Seric **		e -- the envelope to process.
27158929Seric **		full -- if set, do full processing (e.g., compute
27258929Seric **			message priority).
2737783Seric **
2747783Seric **	Returns:
2757783Seric **		none.
2767783Seric **
2777783Seric **	Side Effects:
2787783Seric **		Sets a bunch of global variables from information
2799382Seric **			in the collected header.
2809382Seric **		Aborts the message if the hop count is exceeded.
2817783Seric */
2827783Seric 
28358929Seric eatheader(e, full)
2849382Seric 	register ENVELOPE *e;
28558929Seric 	bool full;
2867783Seric {
2877783Seric 	register HDR *h;
2887783Seric 	register char *p;
2899382Seric 	int hopcnt = 0;
29057208Seric 	char *msgid;
29158688Seric 	char buf[MAXLINE];
2927783Seric 
29358688Seric 	/*
29458688Seric 	**  Set up macros for possible expansion in headers.
29558688Seric 	*/
29658688Seric 
29758704Seric 	define('f', e->e_sender, e);
29858704Seric 	define('g', e->e_sender, e);
29964148Seric 	if (e->e_origrcpt != NULL && *e->e_origrcpt != '\0')
30064148Seric 		define('u', e->e_origrcpt, e);
30164148Seric 	else
30264148Seric 		define('u', NULL, e);
30358688Seric 
3049382Seric 	if (tTd(32, 1))
3059382Seric 		printf("----- collected header -----\n");
30657208Seric 	msgid = "<none>";
3079382Seric 	for (h = e->e_header; h != NULL; h = h->h_link)
3087783Seric 	{
30958688Seric 		/* do early binding */
31058688Seric 		if (bitset(H_DEFAULT, h->h_flags) && h->h_value != NULL)
31158688Seric 		{
31258688Seric 			expand(h->h_value, buf, &buf[sizeof buf], e);
31358688Seric 			if (buf[0] != '\0')
31458688Seric 			{
31558688Seric 				h->h_value = newstr(buf);
31658688Seric 				h->h_flags &= ~H_DEFAULT;
31758688Seric 			}
31858688Seric 		}
31958688Seric 
3209382Seric 		if (tTd(32, 1))
32159579Seric 			printf("%s: %s\n", h->h_field, h->h_value);
32257359Seric 
32311414Seric 		/* count the number of times it has been processed */
3249382Seric 		if (bitset(H_TRACE, h->h_flags))
3259382Seric 			hopcnt++;
32611414Seric 
32711414Seric 		/* send to this person if we so desire */
32811414Seric 		if (GrabTo && bitset(H_RCPT, h->h_flags) &&
32911414Seric 		    !bitset(H_DEFAULT, h->h_flags) &&
33055012Seric 		    (!bitset(EF_RESENT, e->e_flags) || bitset(H_RESENT, h->h_flags)))
33111414Seric 		{
33263839Seric 			int saveflags = e->e_flags;
33363839Seric 
33458082Seric 			(void) sendtolist(h->h_value, (ADDRESS *) NULL,
33558082Seric 					  &e->e_sendqueue, e);
33663839Seric 
33763839Seric 			/* delete fatal errors generated by this address */
33864148Seric 			if (!GrabTo && !bitset(EF_FATALERRS, saveflags))
33963839Seric 				e->e_flags &= ~EF_FATALERRS;
34011414Seric 		}
34111414Seric 
34257208Seric 		/* save the message-id for logging */
34358929Seric 		if (full && h->h_value != NULL &&
34459579Seric 		    strcasecmp(h->h_field, "message-id") == 0)
34511290Seric 		{
34657208Seric 			msgid = h->h_value;
34759859Seric 			while (isascii(*msgid) && isspace(*msgid))
34859859Seric 				msgid++;
34911290Seric 		}
35057359Seric 
35157359Seric 		/* see if this is a return-receipt header */
35257359Seric 		if (bitset(H_RECEIPTTO, h->h_flags))
35357359Seric 			e->e_receiptto = h->h_value;
35457359Seric 
35557359Seric 		/* see if this is an errors-to header */
35661104Seric 		if (UseErrorsTo && bitset(H_ERRORSTO, h->h_flags))
35758082Seric 			(void) sendtolist(h->h_value, (ADDRESS *) NULL,
35858082Seric 					  &e->e_errorqueue, e);
3599382Seric 	}
3609382Seric 	if (tTd(32, 1))
3617783Seric 		printf("----------------------------\n");
3627783Seric 
36358145Seric 	/* if we are just verifying (that is, sendmail -t -bv), drop out now */
36458145Seric 	if (OpMode == MD_VERIFY)
36558145Seric 		return;
36658145Seric 
3679382Seric 	/* store hop count */
3689382Seric 	if (hopcnt > e->e_hopcount)
3699382Seric 		e->e_hopcount = hopcnt;
3709382Seric 
3717783Seric 	/* message priority */
37255012Seric 	p = hvalue("precedence", e);
3739382Seric 	if (p != NULL)
3749382Seric 		e->e_class = priencode(p);
37558929Seric 	if (full)
37625013Seric 		e->e_msgpriority = e->e_msgsize
37724981Seric 				 - e->e_class * WkClassFact
37824981Seric 				 + e->e_nrcpts * WkRecipFact;
3797783Seric 
3807783Seric 	/* full name of from person */
38155012Seric 	p = hvalue("full-name", e);
3827783Seric 	if (p != NULL)
3839382Seric 		define('x', p, e);
3847783Seric 
3857783Seric 	/* date message originated */
38655012Seric 	p = hvalue("posted-date", e);
3877783Seric 	if (p == NULL)
38855012Seric 		p = hvalue("date", e);
3897783Seric 	if (p != NULL)
3909382Seric 		define('a', p, e);
39111290Seric 
39211290Seric 	/*
39311290Seric 	**  Log collection information.
39411290Seric 	*/
39511290Seric 
39611290Seric # ifdef LOG
39758929Seric 	if (full && LogLevel > 4)
39811290Seric 	{
39957359Seric 		char *name;
40060575Seric 		register char *sbp;
40157232Seric 		char hbuf[MAXNAME];
40257359Seric 		char sbuf[MAXLINE];
40336230Skarels 
40458667Seric 		if (bitset(EF_RESPONSE, e->e_flags))
40558667Seric 			name = "[RESPONSE]";
40658951Seric 		else if ((name = macvalue('_', e)) != NULL)
40758951Seric 			;
40858667Seric 		else if (RealHostName[0] == '[')
40936230Skarels 			name = RealHostName;
41036230Skarels 		else
41157359Seric 		{
41257359Seric 			name = hbuf;
41358798Seric 			(void) sprintf(hbuf, "%.80s", RealHostName);
41458798Seric 			if (RealHostAddr.sa.sa_family != 0)
41558798Seric 			{
41658798Seric 				p = &hbuf[strlen(hbuf)];
41758798Seric 				(void) sprintf(p, " (%s)",
41858798Seric 					anynet_ntoa(&RealHostAddr));
41958798Seric 			}
42057359Seric 		}
42157359Seric 
42257359Seric 		/* some versions of syslog only take 5 printf args */
42360575Seric 		sbp = sbuf;
42460575Seric 		sprintf(sbp, "from=%.200s, size=%ld, class=%d, pri=%ld, nrcpts=%d, msgid=%.100s",
42558558Seric 		    e->e_from.q_paddr, e->e_msgsize, e->e_class,
42657589Seric 		    e->e_msgpriority, e->e_nrcpts, msgid);
42760575Seric 		sbp += strlen(sbp);
42860575Seric 		if (e->e_bodytype != NULL)
42960575Seric 		{
43060575Seric 			(void) sprintf(sbp, ", bodytype=%.20s", e->e_bodytype);
43160575Seric 			sbp += strlen(sbp);
43260575Seric 		}
43360575Seric 		p = macvalue('r', e);
43460575Seric 		if (p != NULL)
43560575Seric 			(void) sprintf(sbp, ", proto=%.20s", p);
43658686Seric 		syslog(LOG_INFO, "%s: %s, relay=%s",
43757359Seric 		    e->e_id, sbuf, name);
43811290Seric 	}
43956795Seric # endif /* LOG */
4407783Seric }
4417783Seric /*
4427783Seric **  PRIENCODE -- encode external priority names into internal values.
4437783Seric **
4447783Seric **	Parameters:
4457783Seric **		p -- priority in ascii.
4467783Seric **
4477783Seric **	Returns:
4487783Seric **		priority as a numeric level.
4497783Seric **
4507783Seric **	Side Effects:
4517783Seric **		none.
4527783Seric */
4537783Seric 
4547783Seric priencode(p)
4557783Seric 	char *p;
4567783Seric {
4578253Seric 	register int i;
4587783Seric 
4598253Seric 	for (i = 0; i < NumPriorities; i++)
4607783Seric 	{
46133725Sbostic 		if (!strcasecmp(p, Priorities[i].pri_name))
4628253Seric 			return (Priorities[i].pri_val);
4637783Seric 	}
4648253Seric 
4658253Seric 	/* unknown priority */
4668253Seric 	return (0);
4677783Seric }
4687783Seric /*
4697890Seric **  CRACKADDR -- parse an address and turn it into a macro
4707783Seric **
4717783Seric **	This doesn't actually parse the address -- it just extracts
4727783Seric **	it and replaces it with "$g".  The parse is totally ad hoc
4737783Seric **	and isn't even guaranteed to leave something syntactically
4747783Seric **	identical to what it started with.  However, it does leave
4757783Seric **	something semantically identical.
4767783Seric **
47751379Seric **	This algorithm has been cleaned up to handle a wider range
47851379Seric **	of cases -- notably quoted and backslash escaped strings.
47951379Seric **	This modification makes it substantially better at preserving
48051379Seric **	the original syntax.
4817783Seric **
4827783Seric **	Parameters:
4837890Seric **		addr -- the address to be cracked.
4847783Seric **
4857783Seric **	Returns:
4867783Seric **		a pointer to the new version.
4877783Seric **
4887783Seric **	Side Effects:
4897890Seric **		none.
4907783Seric **
4917783Seric **	Warning:
4927783Seric **		The return value is saved in local storage and should
4937783Seric **		be copied if it is to be reused.
4947783Seric */
4957783Seric 
4967783Seric char *
4977890Seric crackaddr(addr)
4987890Seric 	register char *addr;
4997783Seric {
5007783Seric 	register char *p;
50151379Seric 	register char c;
50251379Seric 	int cmtlev;
50356764Seric 	int realcmtlev;
50456764Seric 	int anglelev, realanglelev;
50551379Seric 	int copylev;
50651379Seric 	bool qmode;
50756764Seric 	bool realqmode;
50856764Seric 	bool skipping;
50951379Seric 	bool putgmac = FALSE;
51056735Seric 	bool quoteit = FALSE;
51164148Seric 	bool gotangle = FALSE;
51251379Seric 	register char *bp;
51356764Seric 	char *buflim;
5147783Seric 	static char buf[MAXNAME];
5157783Seric 
5167783Seric 	if (tTd(33, 1))
5177890Seric 		printf("crackaddr(%s)\n", addr);
5187783Seric 
5198082Seric 	/* strip leading spaces */
52058050Seric 	while (*addr != '\0' && isascii(*addr) && isspace(*addr))
5218082Seric 		addr++;
5228082Seric 
5237783Seric 	/*
52451379Seric 	**  Start by assuming we have no angle brackets.  This will be
52551379Seric 	**  adjusted later if we find them.
5267783Seric 	*/
5277783Seric 
52851379Seric 	bp = buf;
52956764Seric 	buflim = &buf[sizeof buf - 5];
53051379Seric 	p = addr;
53156764Seric 	copylev = anglelev = realanglelev = cmtlev = realcmtlev = 0;
53256764Seric 	qmode = realqmode = FALSE;
53351379Seric 
53451379Seric 	while ((c = *p++) != '\0')
5357783Seric 	{
53656764Seric 		/*
53756764Seric 		**  If the buffer is overful, go into a special "skipping"
53856764Seric 		**  mode that tries to keep legal syntax but doesn't actually
53956764Seric 		**  output things.
54056764Seric 		*/
5417783Seric 
54256764Seric 		skipping = bp >= buflim;
54356735Seric 
54456764Seric 		if (copylev > 0 && !skipping)
54556764Seric 			*bp++ = c;
54656735Seric 
54751379Seric 		/* check for backslash escapes */
54851379Seric 		if (c == '\\')
5497783Seric 		{
55058890Seric 			/* arrange to quote the address */
55158890Seric 			if (cmtlev <= 0 && !qmode)
55258890Seric 				quoteit = TRUE;
55358890Seric 
55451379Seric 			if ((c = *p++) == '\0')
5557783Seric 			{
55651379Seric 				/* too far */
55751379Seric 				p--;
55851379Seric 				goto putg;
5597783Seric 			}
56056764Seric 			if (copylev > 0 && !skipping)
56151379Seric 				*bp++ = c;
56251379Seric 			goto putg;
5637783Seric 		}
5647783Seric 
56551379Seric 		/* check for quoted strings */
56651379Seric 		if (c == '"')
5677783Seric 		{
56851379Seric 			qmode = !qmode;
56956764Seric 			if (copylev > 0 && !skipping)
57056764Seric 				realqmode = !realqmode;
57151379Seric 			continue;
5727783Seric 		}
57351379Seric 		if (qmode)
57451379Seric 			goto putg;
5757783Seric 
57651379Seric 		/* check for comments */
57751379Seric 		if (c == '(')
5787783Seric 		{
57951379Seric 			cmtlev++;
58056764Seric 
58156764Seric 			/* allow space for closing paren */
58256764Seric 			if (!skipping)
58356764Seric 			{
58456764Seric 				buflim--;
58556764Seric 				realcmtlev++;
58656764Seric 				if (copylev++ <= 0)
58756764Seric 				{
58856764Seric 					*bp++ = ' ';
58956764Seric 					*bp++ = c;
59056764Seric 				}
59156764Seric 			}
59251379Seric 		}
59351379Seric 		if (cmtlev > 0)
59451379Seric 		{
59551379Seric 			if (c == ')')
5967783Seric 			{
59751379Seric 				cmtlev--;
59851379Seric 				copylev--;
59956764Seric 				if (!skipping)
60056764Seric 				{
60156764Seric 					realcmtlev--;
60256764Seric 					buflim++;
60356764Seric 				}
6047783Seric 			}
6057783Seric 			continue;
6067783Seric 		}
60756764Seric 		else if (c == ')')
60856764Seric 		{
60956764Seric 			/* syntax error: unmatched ) */
61056764Seric 			if (!skipping)
61156764Seric 				bp--;
61256764Seric 		}
6137783Seric 
61456764Seric 
61556764Seric 		/* check for characters that may have to be quoted */
61664148Seric 		if (strchr(".'@,;:\\()[]", c) != NULL)
61756764Seric 		{
61856764Seric 			/*
61956764Seric 			**  If these occur as the phrase part of a <>
62056764Seric 			**  construct, but are not inside of () or already
62156764Seric 			**  quoted, they will have to be quoted.  Note that
62256764Seric 			**  now (but don't actually do the quoting).
62356764Seric 			*/
62456764Seric 
62556764Seric 			if (cmtlev <= 0 && !qmode)
62656764Seric 				quoteit = TRUE;
62756764Seric 		}
62856764Seric 
62951379Seric 		/* check for angle brackets */
63051379Seric 		if (c == '<')
63151379Seric 		{
63256735Seric 			register char *q;
63356735Seric 
63464148Seric 			/* assume first of two angles is bogus */
63564148Seric 			if (gotangle)
63664148Seric 				quoteit = TRUE;
63764148Seric 			gotangle = TRUE;
63864148Seric 
63951379Seric 			/* oops -- have to change our mind */
64056764Seric 			anglelev++;
64156764Seric 			if (!skipping)
64256764Seric 				realanglelev++;
64356764Seric 
64456735Seric 			bp = buf;
64556735Seric 			if (quoteit)
64656735Seric 			{
64756735Seric 				*bp++ = '"';
64856735Seric 
64956735Seric 				/* back up over the '<' and any spaces */
65056735Seric 				--p;
65158050Seric 				while (isascii(*--p) && isspace(*p))
65256735Seric 					continue;
65356735Seric 				p++;
65456735Seric 			}
65556735Seric 			for (q = addr; q < p; )
65656735Seric 			{
65756735Seric 				c = *q++;
65856764Seric 				if (bp < buflim)
65956764Seric 				{
66056764Seric 					if (quoteit && c == '"')
66156764Seric 						*bp++ = '\\';
66256764Seric 					*bp++ = c;
66356764Seric 				}
66456735Seric 			}
66556735Seric 			if (quoteit)
66656735Seric 			{
66764148Seric 				if (bp == &buf[1])
66864148Seric 					bp--;
66964148Seric 				else
67064148Seric 					*bp++ = '"';
67156764Seric 				while ((c = *p++) != '<')
67256764Seric 				{
67356764Seric 					if (bp < buflim)
67456764Seric 						*bp++ = c;
67556764Seric 				}
67656764Seric 				*bp++ = c;
67756735Seric 			}
67851379Seric 			copylev = 0;
67956735Seric 			putgmac = quoteit = FALSE;
68051379Seric 			continue;
68151379Seric 		}
6827783Seric 
68351379Seric 		if (c == '>')
6847783Seric 		{
68556764Seric 			if (anglelev > 0)
68656764Seric 			{
68756764Seric 				anglelev--;
68856764Seric 				if (!skipping)
68956764Seric 				{
69056764Seric 					realanglelev--;
69156764Seric 					buflim++;
69256764Seric 				}
69356764Seric 			}
69456764Seric 			else if (!skipping)
69556764Seric 			{
69656764Seric 				/* syntax error: unmatched > */
69756764Seric 				if (copylev > 0)
69856764Seric 					bp--;
69956764Seric 				continue;
70056764Seric 			}
70151379Seric 			if (copylev++ <= 0)
70251379Seric 				*bp++ = c;
70351379Seric 			continue;
7047783Seric 		}
70551379Seric 
70651379Seric 		/* must be a real address character */
70751379Seric 	putg:
70851379Seric 		if (copylev <= 0 && !putgmac)
70951379Seric 		{
71058050Seric 			*bp++ = MACROEXPAND;
71151379Seric 			*bp++ = 'g';
71251379Seric 			putgmac = TRUE;
71351379Seric 		}
7147783Seric 	}
7157783Seric 
71656764Seric 	/* repair any syntactic damage */
71756764Seric 	if (realqmode)
71856764Seric 		*bp++ = '"';
71956764Seric 	while (realcmtlev-- > 0)
72056764Seric 		*bp++ = ')';
72156764Seric 	while (realanglelev-- > 0)
72256764Seric 		*bp++ = '>';
72351379Seric 	*bp++ = '\0';
7247783Seric 
7257783Seric 	if (tTd(33, 1))
7267944Seric 		printf("crackaddr=>`%s'\n", buf);
7277783Seric 
7287783Seric 	return (buf);
7297783Seric }
7309382Seric /*
7319382Seric **  PUTHEADER -- put the header part of a message from the in-core copy
7329382Seric **
7339382Seric **	Parameters:
7349382Seric **		fp -- file to put it on.
7359382Seric **		m -- mailer to use.
7369382Seric **		e -- envelope to use.
7379382Seric **
7389382Seric **	Returns:
7399382Seric **		none.
7409382Seric **
7419382Seric **	Side Effects:
7429382Seric **		none.
7439382Seric */
7449382Seric 
74557589Seric /*
74657589Seric  * Macro for fast max (not available in e.g. DG/UX, 386/ix).
74757589Seric  */
74857589Seric #ifndef MAX
74957589Seric # define MAX(a,b) (((a)>(b))?(a):(b))
75057589Seric #endif
75157589Seric 
75210176Seric putheader(fp, m, e)
7539382Seric 	register FILE *fp;
7549382Seric 	register MAILER *m;
7559382Seric 	register ENVELOPE *e;
7569382Seric {
75757135Seric 	char buf[MAX(MAXLINE,BUFSIZ)];
7589382Seric 	register HDR *h;
75957135Seric 	char obuf[MAXLINE];
7609382Seric 
76159882Seric 	if (tTd(34, 1))
76259882Seric 		printf("--- putheader, mailer = %s ---\n", m->m_name);
76359882Seric 
7649382Seric 	for (h = e->e_header; h != NULL; h = h->h_link)
7659382Seric 	{
7669382Seric 		register char *p;
76710689Seric 		extern bool bitintersect();
7689382Seric 
76959882Seric 		if (tTd(34, 11))
77059882Seric 		{
77159882Seric 			printf("  %s: ", h->h_field);
77259882Seric 			xputs(h->h_value);
77359882Seric 		}
77459882Seric 
7759382Seric 		if (bitset(H_CHECK|H_ACHECK, h->h_flags) &&
77610689Seric 		    !bitintersect(h->h_mflags, m->m_flags))
77759882Seric 		{
77859882Seric 			if (tTd(34, 11))
77959882Seric 				printf(" (skipped)\n");
7809382Seric 			continue;
78159882Seric 		}
7829382Seric 
78311414Seric 		/* handle Resent-... headers specially */
78411414Seric 		if (bitset(H_RESENT, h->h_flags) && !bitset(EF_RESENT, e->e_flags))
78559882Seric 		{
78659882Seric 			if (tTd(34, 11))
78759882Seric 				printf(" (skipped (resent))\n");
78811414Seric 			continue;
78959882Seric 		}
79059882Seric 		if (tTd(34, 11))
79159882Seric 			printf("\n");
79211414Seric 
7939382Seric 		p = h->h_value;
7949382Seric 		if (bitset(H_DEFAULT, h->h_flags))
7959382Seric 		{
7969382Seric 			/* macro expand value if generated internally */
7979382Seric 			expand(p, buf, &buf[sizeof buf], e);
7989382Seric 			p = buf;
7999382Seric 			if (p == NULL || *p == '\0')
8009382Seric 				continue;
8019382Seric 		}
8029382Seric 
8039382Seric 		if (bitset(H_FROM|H_RCPT, h->h_flags))
8049382Seric 		{
8059382Seric 			/* address field */
8069382Seric 			bool oldstyle = bitset(EF_OLDSTYLE, e->e_flags);
8079382Seric 
8089382Seric 			if (bitset(H_FROM, h->h_flags))
8099382Seric 				oldstyle = FALSE;
81055012Seric 			commaize(h, p, fp, oldstyle, m, e);
8119382Seric 		}
8129382Seric 		else
8139382Seric 		{
8149382Seric 			/* vanilla header line */
81512159Seric 			register char *nlp;
81612159Seric 
81759579Seric 			(void) sprintf(obuf, "%s: ", h->h_field);
81856795Seric 			while ((nlp = strchr(p, '\n')) != NULL)
81912159Seric 			{
82012159Seric 				*nlp = '\0';
82112159Seric 				(void) strcat(obuf, p);
82212159Seric 				*nlp = '\n';
82312159Seric 				putline(obuf, fp, m);
82412159Seric 				p = ++nlp;
82512161Seric 				obuf[0] = '\0';
82612159Seric 			}
82712159Seric 			(void) strcat(obuf, p);
82810176Seric 			putline(obuf, fp, m);
8299382Seric 		}
8309382Seric 	}
8319382Seric }
8329382Seric /*
8339382Seric **  COMMAIZE -- output a header field, making a comma-translated list.
8349382Seric **
8359382Seric **	Parameters:
8369382Seric **		h -- the header field to output.
8379382Seric **		p -- the value to put in it.
8389382Seric **		fp -- file to put it to.
8399382Seric **		oldstyle -- TRUE if this is an old style header.
8409382Seric **		m -- a pointer to the mailer descriptor.  If NULL,
8419382Seric **			don't transform the name at all.
84255012Seric **		e -- the envelope containing the message.
8439382Seric **
8449382Seric **	Returns:
8459382Seric **		none.
8469382Seric **
8479382Seric **	Side Effects:
8489382Seric **		outputs "p" to file "fp".
8499382Seric */
8509382Seric 
85155012Seric commaize(h, p, fp, oldstyle, m, e)
8529382Seric 	register HDR *h;
8539382Seric 	register char *p;
8549382Seric 	FILE *fp;
8559382Seric 	bool oldstyle;
8569382Seric 	register MAILER *m;
85755012Seric 	register ENVELOPE *e;
8589382Seric {
8599382Seric 	register char *obp;
8609382Seric 	int opos;
8619382Seric 	bool firstone = TRUE;
86211157Seric 	char obuf[MAXLINE + 3];
8639382Seric 
8649382Seric 	/*
8659382Seric 	**  Output the address list translated by the
8669382Seric 	**  mailer and with commas.
8679382Seric 	*/
8689382Seric 
8699382Seric 	if (tTd(14, 2))
8709382Seric 		printf("commaize(%s: %s)\n", h->h_field, p);
8719382Seric 
8729382Seric 	obp = obuf;
87359579Seric 	(void) sprintf(obp, "%s: ", h->h_field);
8749382Seric 	opos = strlen(h->h_field) + 2;
8759382Seric 	obp += opos;
8769382Seric 
8779382Seric 	/*
8789382Seric 	**  Run through the list of values.
8799382Seric 	*/
8809382Seric 
8819382Seric 	while (*p != '\0')
8829382Seric 	{
8839382Seric 		register char *name;
88454983Seric 		register int c;
8859382Seric 		char savechar;
88659163Seric 		int flags;
88759163Seric 		auto int stat;
8889382Seric 
8899382Seric 		/*
8909382Seric 		**  Find the end of the name.  New style names
8919382Seric 		**  end with a comma, old style names end with
8929382Seric 		**  a space character.  However, spaces do not
8939382Seric 		**  necessarily delimit an old-style name -- at
8949382Seric 		**  signs mean keep going.
8959382Seric 		*/
8969382Seric 
8979382Seric 		/* find end of name */
89858050Seric 		while ((isascii(*p) && isspace(*p)) || *p == ',')
8999382Seric 			p++;
9009382Seric 		name = p;
9019382Seric 		for (;;)
9029382Seric 		{
90358333Seric 			auto char *oldp;
90416909Seric 			char pvpbuf[PSBUFSIZE];
9059382Seric 
90658333Seric 			(void) prescan(p, oldstyle ? ' ' : ',', pvpbuf, &oldp);
90758333Seric 			p = oldp;
9089382Seric 
9099382Seric 			/* look to see if we have an at sign */
91058050Seric 			while (*p != '\0' && isascii(*p) && isspace(*p))
9119382Seric 				p++;
9129382Seric 
91358170Seric 			if (*p != '@')
9149382Seric 			{
9159382Seric 				p = oldp;
9169382Seric 				break;
9179382Seric 			}
9189382Seric 			p += *p == '@' ? 1 : 2;
91958050Seric 			while (*p != '\0' && isascii(*p) && isspace(*p))
9209382Seric 				p++;
9219382Seric 		}
9229382Seric 		/* at the end of one complete name */
9239382Seric 
9249382Seric 		/* strip off trailing white space */
92558050Seric 		while (p >= name &&
92658050Seric 		       ((isascii(*p) && isspace(*p)) || *p == ',' || *p == '\0'))
9279382Seric 			p--;
9289382Seric 		if (++p == name)
9299382Seric 			continue;
9309382Seric 		savechar = *p;
9319382Seric 		*p = '\0';
9329382Seric 
9339382Seric 		/* translate the name to be relative */
93459163Seric 		flags = RF_HEADERADDR|RF_ADDDOMAIN;
93559163Seric 		if (bitset(H_FROM, h->h_flags))
93659163Seric 			flags |= RF_SENDERADDR;
93759163Seric 		stat = EX_OK;
93859163Seric 		name = remotename(name, m, flags, &stat, e);
9399382Seric 		if (*name == '\0')
9409382Seric 		{
9419382Seric 			*p = savechar;
9429382Seric 			continue;
9439382Seric 		}
9449382Seric 
9459382Seric 		/* output the name with nice formatting */
94654983Seric 		opos += strlen(name);
9479382Seric 		if (!firstone)
9489382Seric 			opos += 2;
9499382Seric 		if (opos > 78 && !firstone)
9509382Seric 		{
95110178Seric 			(void) strcpy(obp, ",\n");
95210176Seric 			putline(obuf, fp, m);
9539382Seric 			obp = obuf;
9549382Seric 			(void) sprintf(obp, "        ");
95510161Seric 			opos = strlen(obp);
95610161Seric 			obp += opos;
95754983Seric 			opos += strlen(name);
9589382Seric 		}
9599382Seric 		else if (!firstone)
9609382Seric 		{
9619382Seric 			(void) sprintf(obp, ", ");
9629382Seric 			obp += 2;
9639382Seric 		}
9649382Seric 
96554983Seric 		while ((c = *name++) != '\0' && obp < &obuf[MAXLINE])
96654983Seric 			*obp++ = c;
9679382Seric 		firstone = FALSE;
9689382Seric 		*p = savechar;
9699382Seric 	}
9709382Seric 	(void) strcpy(obp, "\n");
97110176Seric 	putline(obuf, fp, m);
9729382Seric }
9739382Seric /*
97458170Seric **  COPYHEADER -- copy header list
9759382Seric **
97658170Seric **	This routine is the equivalent of newstr for header lists
97758170Seric **
9789382Seric **	Parameters:
97958170Seric **		header -- list of header structures to copy.
9809382Seric **
9819382Seric **	Returns:
98258170Seric **		a copy of 'header'.
9839382Seric **
9849382Seric **	Side Effects:
9859382Seric **		none.
9869382Seric */
9879382Seric 
98858170Seric HDR *
98958170Seric copyheader(header)
99058170Seric 	register HDR *header;
9919382Seric {
99258170Seric 	register HDR *newhdr;
99358170Seric 	HDR *ret;
99458170Seric 	register HDR **tail = &ret;
9959382Seric 
99658170Seric 	while (header != NULL)
99758170Seric 	{
99858170Seric 		newhdr = (HDR *) xalloc(sizeof(HDR));
99958170Seric 		STRUCTCOPY(*header, *newhdr);
100058170Seric 		*tail = newhdr;
100158170Seric 		tail = &newhdr->h_link;
100258170Seric 		header = header->h_link;
100358170Seric 	}
100458170Seric 	*tail = NULL;
100558170Seric 
100658170Seric 	return ret;
10079382Seric }
1008