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*64148Seric static char sccsid[] = "@(#)headers.c	8.6 (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;
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);
13264134Seric 	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);
297*64148Seric 	if (e->e_origrcpt != NULL && *e->e_origrcpt != '\0')
298*64148Seric 		define('u', e->e_origrcpt, e);
299*64148Seric 	else
300*64148Seric 		define('u', NULL, e);
30158688Seric 
3029382Seric 	if (tTd(32, 1))
3039382Seric 		printf("----- collected header -----\n");
30457208Seric 	msgid = "<none>";
3059382Seric 	for (h = e->e_header; h != NULL; h = h->h_link)
3067783Seric 	{
30758688Seric 		/* do early binding */
30858688Seric 		if (bitset(H_DEFAULT, h->h_flags) && h->h_value != NULL)
30958688Seric 		{
31058688Seric 			expand(h->h_value, buf, &buf[sizeof buf], e);
31158688Seric 			if (buf[0] != '\0')
31258688Seric 			{
31358688Seric 				h->h_value = newstr(buf);
31458688Seric 				h->h_flags &= ~H_DEFAULT;
31558688Seric 			}
31658688Seric 		}
31758688Seric 
3189382Seric 		if (tTd(32, 1))
31959579Seric 			printf("%s: %s\n", h->h_field, h->h_value);
32057359Seric 
32111414Seric 		/* count the number of times it has been processed */
3229382Seric 		if (bitset(H_TRACE, h->h_flags))
3239382Seric 			hopcnt++;
32411414Seric 
32511414Seric 		/* send to this person if we so desire */
32611414Seric 		if (GrabTo && bitset(H_RCPT, h->h_flags) &&
32711414Seric 		    !bitset(H_DEFAULT, h->h_flags) &&
32855012Seric 		    (!bitset(EF_RESENT, e->e_flags) || bitset(H_RESENT, h->h_flags)))
32911414Seric 		{
33063839Seric 			int saveflags = e->e_flags;
33163839Seric 
33258082Seric 			(void) sendtolist(h->h_value, (ADDRESS *) NULL,
33358082Seric 					  &e->e_sendqueue, e);
33463839Seric 
33563839Seric 			/* delete fatal errors generated by this address */
336*64148Seric 			if (!GrabTo && !bitset(EF_FATALERRS, saveflags))
33763839Seric 				e->e_flags &= ~EF_FATALERRS;
33811414Seric 		}
33911414Seric 
34057208Seric 		/* save the message-id for logging */
34158929Seric 		if (full && h->h_value != NULL &&
34259579Seric 		    strcasecmp(h->h_field, "message-id") == 0)
34311290Seric 		{
34457208Seric 			msgid = h->h_value;
34559859Seric 			while (isascii(*msgid) && isspace(*msgid))
34659859Seric 				msgid++;
34711290Seric 		}
34857359Seric 
34957359Seric 		/* see if this is a return-receipt header */
35057359Seric 		if (bitset(H_RECEIPTTO, h->h_flags))
35157359Seric 			e->e_receiptto = h->h_value;
35257359Seric 
35357359Seric 		/* see if this is an errors-to header */
35461104Seric 		if (UseErrorsTo && bitset(H_ERRORSTO, h->h_flags))
35558082Seric 			(void) sendtolist(h->h_value, (ADDRESS *) NULL,
35658082Seric 					  &e->e_errorqueue, e);
3579382Seric 	}
3589382Seric 	if (tTd(32, 1))
3597783Seric 		printf("----------------------------\n");
3607783Seric 
36158145Seric 	/* if we are just verifying (that is, sendmail -t -bv), drop out now */
36258145Seric 	if (OpMode == MD_VERIFY)
36358145Seric 		return;
36458145Seric 
3659382Seric 	/* store hop count */
3669382Seric 	if (hopcnt > e->e_hopcount)
3679382Seric 		e->e_hopcount = hopcnt;
3689382Seric 
3697783Seric 	/* message priority */
37055012Seric 	p = hvalue("precedence", e);
3719382Seric 	if (p != NULL)
3729382Seric 		e->e_class = priencode(p);
37358929Seric 	if (full)
37425013Seric 		e->e_msgpriority = e->e_msgsize
37524981Seric 				 - e->e_class * WkClassFact
37624981Seric 				 + e->e_nrcpts * WkRecipFact;
3777783Seric 
3787783Seric 	/* full name of from person */
37955012Seric 	p = hvalue("full-name", e);
3807783Seric 	if (p != NULL)
3819382Seric 		define('x', p, e);
3827783Seric 
3837783Seric 	/* date message originated */
38455012Seric 	p = hvalue("posted-date", e);
3857783Seric 	if (p == NULL)
38655012Seric 		p = hvalue("date", e);
3877783Seric 	if (p != NULL)
3889382Seric 		define('a', p, e);
38911290Seric 
39011290Seric 	/*
39111290Seric 	**  Log collection information.
39211290Seric 	*/
39311290Seric 
39411290Seric # ifdef LOG
39558929Seric 	if (full && LogLevel > 4)
39611290Seric 	{
39757359Seric 		char *name;
39860575Seric 		register char *sbp;
39957232Seric 		char hbuf[MAXNAME];
40057359Seric 		char sbuf[MAXLINE];
40136230Skarels 
40258667Seric 		if (bitset(EF_RESPONSE, e->e_flags))
40358667Seric 			name = "[RESPONSE]";
40458951Seric 		else if ((name = macvalue('_', e)) != NULL)
40558951Seric 			;
40658667Seric 		else if (RealHostName[0] == '[')
40736230Skarels 			name = RealHostName;
40836230Skarels 		else
40957359Seric 		{
41057359Seric 			name = hbuf;
41158798Seric 			(void) sprintf(hbuf, "%.80s", RealHostName);
41258798Seric 			if (RealHostAddr.sa.sa_family != 0)
41358798Seric 			{
41458798Seric 				p = &hbuf[strlen(hbuf)];
41558798Seric 				(void) sprintf(p, " (%s)",
41658798Seric 					anynet_ntoa(&RealHostAddr));
41758798Seric 			}
41857359Seric 		}
41957359Seric 
42057359Seric 		/* some versions of syslog only take 5 printf args */
42160575Seric 		sbp = sbuf;
42260575Seric 		sprintf(sbp, "from=%.200s, size=%ld, class=%d, pri=%ld, nrcpts=%d, msgid=%.100s",
42358558Seric 		    e->e_from.q_paddr, e->e_msgsize, e->e_class,
42457589Seric 		    e->e_msgpriority, e->e_nrcpts, msgid);
42560575Seric 		sbp += strlen(sbp);
42660575Seric 		if (e->e_bodytype != NULL)
42760575Seric 		{
42860575Seric 			(void) sprintf(sbp, ", bodytype=%.20s", e->e_bodytype);
42960575Seric 			sbp += strlen(sbp);
43060575Seric 		}
43160575Seric 		p = macvalue('r', e);
43260575Seric 		if (p != NULL)
43360575Seric 			(void) sprintf(sbp, ", proto=%.20s", p);
43458686Seric 		syslog(LOG_INFO, "%s: %s, relay=%s",
43557359Seric 		    e->e_id, sbuf, name);
43611290Seric 	}
43756795Seric # endif /* LOG */
4387783Seric }
4397783Seric /*
4407783Seric **  PRIENCODE -- encode external priority names into internal values.
4417783Seric **
4427783Seric **	Parameters:
4437783Seric **		p -- priority in ascii.
4447783Seric **
4457783Seric **	Returns:
4467783Seric **		priority as a numeric level.
4477783Seric **
4487783Seric **	Side Effects:
4497783Seric **		none.
4507783Seric */
4517783Seric 
4527783Seric priencode(p)
4537783Seric 	char *p;
4547783Seric {
4558253Seric 	register int i;
4567783Seric 
4578253Seric 	for (i = 0; i < NumPriorities; i++)
4587783Seric 	{
45933725Sbostic 		if (!strcasecmp(p, Priorities[i].pri_name))
4608253Seric 			return (Priorities[i].pri_val);
4617783Seric 	}
4628253Seric 
4638253Seric 	/* unknown priority */
4648253Seric 	return (0);
4657783Seric }
4667783Seric /*
4677890Seric **  CRACKADDR -- parse an address and turn it into a macro
4687783Seric **
4697783Seric **	This doesn't actually parse the address -- it just extracts
4707783Seric **	it and replaces it with "$g".  The parse is totally ad hoc
4717783Seric **	and isn't even guaranteed to leave something syntactically
4727783Seric **	identical to what it started with.  However, it does leave
4737783Seric **	something semantically identical.
4747783Seric **
47551379Seric **	This algorithm has been cleaned up to handle a wider range
47651379Seric **	of cases -- notably quoted and backslash escaped strings.
47751379Seric **	This modification makes it substantially better at preserving
47851379Seric **	the original syntax.
4797783Seric **
4807783Seric **	Parameters:
4817890Seric **		addr -- the address to be cracked.
4827783Seric **
4837783Seric **	Returns:
4847783Seric **		a pointer to the new version.
4857783Seric **
4867783Seric **	Side Effects:
4877890Seric **		none.
4887783Seric **
4897783Seric **	Warning:
4907783Seric **		The return value is saved in local storage and should
4917783Seric **		be copied if it is to be reused.
4927783Seric */
4937783Seric 
4947783Seric char *
4957890Seric crackaddr(addr)
4967890Seric 	register char *addr;
4977783Seric {
4987783Seric 	register char *p;
49951379Seric 	register char c;
50051379Seric 	int cmtlev;
50156764Seric 	int realcmtlev;
50256764Seric 	int anglelev, realanglelev;
50351379Seric 	int copylev;
50451379Seric 	bool qmode;
50556764Seric 	bool realqmode;
50656764Seric 	bool skipping;
50751379Seric 	bool putgmac = FALSE;
50856735Seric 	bool quoteit = FALSE;
509*64148Seric 	bool gotangle = FALSE;
51051379Seric 	register char *bp;
51156764Seric 	char *buflim;
5127783Seric 	static char buf[MAXNAME];
5137783Seric 
5147783Seric 	if (tTd(33, 1))
5157890Seric 		printf("crackaddr(%s)\n", addr);
5167783Seric 
5178082Seric 	/* strip leading spaces */
51858050Seric 	while (*addr != '\0' && isascii(*addr) && isspace(*addr))
5198082Seric 		addr++;
5208082Seric 
5217783Seric 	/*
52251379Seric 	**  Start by assuming we have no angle brackets.  This will be
52351379Seric 	**  adjusted later if we find them.
5247783Seric 	*/
5257783Seric 
52651379Seric 	bp = buf;
52756764Seric 	buflim = &buf[sizeof buf - 5];
52851379Seric 	p = addr;
52956764Seric 	copylev = anglelev = realanglelev = cmtlev = realcmtlev = 0;
53056764Seric 	qmode = realqmode = FALSE;
53151379Seric 
53251379Seric 	while ((c = *p++) != '\0')
5337783Seric 	{
53456764Seric 		/*
53556764Seric 		**  If the buffer is overful, go into a special "skipping"
53656764Seric 		**  mode that tries to keep legal syntax but doesn't actually
53756764Seric 		**  output things.
53856764Seric 		*/
5397783Seric 
54056764Seric 		skipping = bp >= buflim;
54156735Seric 
54256764Seric 		if (copylev > 0 && !skipping)
54356764Seric 			*bp++ = c;
54456735Seric 
54551379Seric 		/* check for backslash escapes */
54651379Seric 		if (c == '\\')
5477783Seric 		{
54858890Seric 			/* arrange to quote the address */
54958890Seric 			if (cmtlev <= 0 && !qmode)
55058890Seric 				quoteit = TRUE;
55158890Seric 
55251379Seric 			if ((c = *p++) == '\0')
5537783Seric 			{
55451379Seric 				/* too far */
55551379Seric 				p--;
55651379Seric 				goto putg;
5577783Seric 			}
55856764Seric 			if (copylev > 0 && !skipping)
55951379Seric 				*bp++ = c;
56051379Seric 			goto putg;
5617783Seric 		}
5627783Seric 
56351379Seric 		/* check for quoted strings */
56451379Seric 		if (c == '"')
5657783Seric 		{
56651379Seric 			qmode = !qmode;
56756764Seric 			if (copylev > 0 && !skipping)
56856764Seric 				realqmode = !realqmode;
56951379Seric 			continue;
5707783Seric 		}
57151379Seric 		if (qmode)
57251379Seric 			goto putg;
5737783Seric 
57451379Seric 		/* check for comments */
57551379Seric 		if (c == '(')
5767783Seric 		{
57751379Seric 			cmtlev++;
57856764Seric 
57956764Seric 			/* allow space for closing paren */
58056764Seric 			if (!skipping)
58156764Seric 			{
58256764Seric 				buflim--;
58356764Seric 				realcmtlev++;
58456764Seric 				if (copylev++ <= 0)
58556764Seric 				{
58656764Seric 					*bp++ = ' ';
58756764Seric 					*bp++ = c;
58856764Seric 				}
58956764Seric 			}
59051379Seric 		}
59151379Seric 		if (cmtlev > 0)
59251379Seric 		{
59351379Seric 			if (c == ')')
5947783Seric 			{
59551379Seric 				cmtlev--;
59651379Seric 				copylev--;
59756764Seric 				if (!skipping)
59856764Seric 				{
59956764Seric 					realcmtlev--;
60056764Seric 					buflim++;
60156764Seric 				}
6027783Seric 			}
6037783Seric 			continue;
6047783Seric 		}
60556764Seric 		else if (c == ')')
60656764Seric 		{
60756764Seric 			/* syntax error: unmatched ) */
60856764Seric 			if (!skipping)
60956764Seric 				bp--;
61056764Seric 		}
6117783Seric 
61256764Seric 
61356764Seric 		/* check for characters that may have to be quoted */
614*64148Seric 		if (strchr(".'@,;:\\()[]", c) != NULL)
61556764Seric 		{
61656764Seric 			/*
61756764Seric 			**  If these occur as the phrase part of a <>
61856764Seric 			**  construct, but are not inside of () or already
61956764Seric 			**  quoted, they will have to be quoted.  Note that
62056764Seric 			**  now (but don't actually do the quoting).
62156764Seric 			*/
62256764Seric 
62356764Seric 			if (cmtlev <= 0 && !qmode)
62456764Seric 				quoteit = TRUE;
62556764Seric 		}
62656764Seric 
62751379Seric 		/* check for angle brackets */
62851379Seric 		if (c == '<')
62951379Seric 		{
63056735Seric 			register char *q;
63156735Seric 
632*64148Seric 			/* assume first of two angles is bogus */
633*64148Seric 			if (gotangle)
634*64148Seric 				quoteit = TRUE;
635*64148Seric 			gotangle = TRUE;
636*64148Seric 
63751379Seric 			/* oops -- have to change our mind */
63856764Seric 			anglelev++;
63956764Seric 			if (!skipping)
64056764Seric 				realanglelev++;
64156764Seric 
64256735Seric 			bp = buf;
64356735Seric 			if (quoteit)
64456735Seric 			{
64556735Seric 				*bp++ = '"';
64656735Seric 
64756735Seric 				/* back up over the '<' and any spaces */
64856735Seric 				--p;
64958050Seric 				while (isascii(*--p) && isspace(*p))
65056735Seric 					continue;
65156735Seric 				p++;
65256735Seric 			}
65356735Seric 			for (q = addr; q < p; )
65456735Seric 			{
65556735Seric 				c = *q++;
65656764Seric 				if (bp < buflim)
65756764Seric 				{
65856764Seric 					if (quoteit && c == '"')
65956764Seric 						*bp++ = '\\';
66056764Seric 					*bp++ = c;
66156764Seric 				}
66256735Seric 			}
66356735Seric 			if (quoteit)
66456735Seric 			{
665*64148Seric 				if (bp == &buf[1])
666*64148Seric 					bp--;
667*64148Seric 				else
668*64148Seric 					*bp++ = '"';
66956764Seric 				while ((c = *p++) != '<')
67056764Seric 				{
67156764Seric 					if (bp < buflim)
67256764Seric 						*bp++ = c;
67356764Seric 				}
67456764Seric 				*bp++ = c;
67556735Seric 			}
67651379Seric 			copylev = 0;
67756735Seric 			putgmac = quoteit = FALSE;
67851379Seric 			continue;
67951379Seric 		}
6807783Seric 
68151379Seric 		if (c == '>')
6827783Seric 		{
68356764Seric 			if (anglelev > 0)
68456764Seric 			{
68556764Seric 				anglelev--;
68656764Seric 				if (!skipping)
68756764Seric 				{
68856764Seric 					realanglelev--;
68956764Seric 					buflim++;
69056764Seric 				}
69156764Seric 			}
69256764Seric 			else if (!skipping)
69356764Seric 			{
69456764Seric 				/* syntax error: unmatched > */
69556764Seric 				if (copylev > 0)
69656764Seric 					bp--;
69756764Seric 				continue;
69856764Seric 			}
69951379Seric 			if (copylev++ <= 0)
70051379Seric 				*bp++ = c;
70151379Seric 			continue;
7027783Seric 		}
70351379Seric 
70451379Seric 		/* must be a real address character */
70551379Seric 	putg:
70651379Seric 		if (copylev <= 0 && !putgmac)
70751379Seric 		{
70858050Seric 			*bp++ = MACROEXPAND;
70951379Seric 			*bp++ = 'g';
71051379Seric 			putgmac = TRUE;
71151379Seric 		}
7127783Seric 	}
7137783Seric 
71456764Seric 	/* repair any syntactic damage */
71556764Seric 	if (realqmode)
71656764Seric 		*bp++ = '"';
71756764Seric 	while (realcmtlev-- > 0)
71856764Seric 		*bp++ = ')';
71956764Seric 	while (realanglelev-- > 0)
72056764Seric 		*bp++ = '>';
72151379Seric 	*bp++ = '\0';
7227783Seric 
7237783Seric 	if (tTd(33, 1))
7247944Seric 		printf("crackaddr=>`%s'\n", buf);
7257783Seric 
7267783Seric 	return (buf);
7277783Seric }
7289382Seric /*
7299382Seric **  PUTHEADER -- put the header part of a message from the in-core copy
7309382Seric **
7319382Seric **	Parameters:
7329382Seric **		fp -- file to put it on.
7339382Seric **		m -- mailer to use.
7349382Seric **		e -- envelope to use.
7359382Seric **
7369382Seric **	Returns:
7379382Seric **		none.
7389382Seric **
7399382Seric **	Side Effects:
7409382Seric **		none.
7419382Seric */
7429382Seric 
74357589Seric /*
74457589Seric  * Macro for fast max (not available in e.g. DG/UX, 386/ix).
74557589Seric  */
74657589Seric #ifndef MAX
74757589Seric # define MAX(a,b) (((a)>(b))?(a):(b))
74857589Seric #endif
74957589Seric 
75010176Seric putheader(fp, m, e)
7519382Seric 	register FILE *fp;
7529382Seric 	register MAILER *m;
7539382Seric 	register ENVELOPE *e;
7549382Seric {
75557135Seric 	char buf[MAX(MAXLINE,BUFSIZ)];
7569382Seric 	register HDR *h;
75757135Seric 	char obuf[MAXLINE];
7589382Seric 
75959882Seric 	if (tTd(34, 1))
76059882Seric 		printf("--- putheader, mailer = %s ---\n", m->m_name);
76159882Seric 
7629382Seric 	for (h = e->e_header; h != NULL; h = h->h_link)
7639382Seric 	{
7649382Seric 		register char *p;
76510689Seric 		extern bool bitintersect();
7669382Seric 
76759882Seric 		if (tTd(34, 11))
76859882Seric 		{
76959882Seric 			printf("  %s: ", h->h_field);
77059882Seric 			xputs(h->h_value);
77159882Seric 		}
77259882Seric 
7739382Seric 		if (bitset(H_CHECK|H_ACHECK, h->h_flags) &&
77410689Seric 		    !bitintersect(h->h_mflags, m->m_flags))
77559882Seric 		{
77659882Seric 			if (tTd(34, 11))
77759882Seric 				printf(" (skipped)\n");
7789382Seric 			continue;
77959882Seric 		}
7809382Seric 
78111414Seric 		/* handle Resent-... headers specially */
78211414Seric 		if (bitset(H_RESENT, h->h_flags) && !bitset(EF_RESENT, e->e_flags))
78359882Seric 		{
78459882Seric 			if (tTd(34, 11))
78559882Seric 				printf(" (skipped (resent))\n");
78611414Seric 			continue;
78759882Seric 		}
78859882Seric 		if (tTd(34, 11))
78959882Seric 			printf("\n");
79011414Seric 
7919382Seric 		p = h->h_value;
7929382Seric 		if (bitset(H_DEFAULT, h->h_flags))
7939382Seric 		{
7949382Seric 			/* macro expand value if generated internally */
7959382Seric 			expand(p, buf, &buf[sizeof buf], e);
7969382Seric 			p = buf;
7979382Seric 			if (p == NULL || *p == '\0')
7989382Seric 				continue;
7999382Seric 		}
8009382Seric 
8019382Seric 		if (bitset(H_FROM|H_RCPT, h->h_flags))
8029382Seric 		{
8039382Seric 			/* address field */
8049382Seric 			bool oldstyle = bitset(EF_OLDSTYLE, e->e_flags);
8059382Seric 
8069382Seric 			if (bitset(H_FROM, h->h_flags))
8079382Seric 				oldstyle = FALSE;
80855012Seric 			commaize(h, p, fp, oldstyle, m, e);
8099382Seric 		}
8109382Seric 		else
8119382Seric 		{
8129382Seric 			/* vanilla header line */
81312159Seric 			register char *nlp;
81412159Seric 
81559579Seric 			(void) sprintf(obuf, "%s: ", h->h_field);
81656795Seric 			while ((nlp = strchr(p, '\n')) != NULL)
81712159Seric 			{
81812159Seric 				*nlp = '\0';
81912159Seric 				(void) strcat(obuf, p);
82012159Seric 				*nlp = '\n';
82112159Seric 				putline(obuf, fp, m);
82212159Seric 				p = ++nlp;
82312161Seric 				obuf[0] = '\0';
82412159Seric 			}
82512159Seric 			(void) strcat(obuf, p);
82610176Seric 			putline(obuf, fp, m);
8279382Seric 		}
8289382Seric 	}
8299382Seric }
8309382Seric /*
8319382Seric **  COMMAIZE -- output a header field, making a comma-translated list.
8329382Seric **
8339382Seric **	Parameters:
8349382Seric **		h -- the header field to output.
8359382Seric **		p -- the value to put in it.
8369382Seric **		fp -- file to put it to.
8379382Seric **		oldstyle -- TRUE if this is an old style header.
8389382Seric **		m -- a pointer to the mailer descriptor.  If NULL,
8399382Seric **			don't transform the name at all.
84055012Seric **		e -- the envelope containing the message.
8419382Seric **
8429382Seric **	Returns:
8439382Seric **		none.
8449382Seric **
8459382Seric **	Side Effects:
8469382Seric **		outputs "p" to file "fp".
8479382Seric */
8489382Seric 
84955012Seric commaize(h, p, fp, oldstyle, m, e)
8509382Seric 	register HDR *h;
8519382Seric 	register char *p;
8529382Seric 	FILE *fp;
8539382Seric 	bool oldstyle;
8549382Seric 	register MAILER *m;
85555012Seric 	register ENVELOPE *e;
8569382Seric {
8579382Seric 	register char *obp;
8589382Seric 	int opos;
8599382Seric 	bool firstone = TRUE;
86011157Seric 	char obuf[MAXLINE + 3];
8619382Seric 
8629382Seric 	/*
8639382Seric 	**  Output the address list translated by the
8649382Seric 	**  mailer and with commas.
8659382Seric 	*/
8669382Seric 
8679382Seric 	if (tTd(14, 2))
8689382Seric 		printf("commaize(%s: %s)\n", h->h_field, p);
8699382Seric 
8709382Seric 	obp = obuf;
87159579Seric 	(void) sprintf(obp, "%s: ", h->h_field);
8729382Seric 	opos = strlen(h->h_field) + 2;
8739382Seric 	obp += opos;
8749382Seric 
8759382Seric 	/*
8769382Seric 	**  Run through the list of values.
8779382Seric 	*/
8789382Seric 
8799382Seric 	while (*p != '\0')
8809382Seric 	{
8819382Seric 		register char *name;
88254983Seric 		register int c;
8839382Seric 		char savechar;
88459163Seric 		int flags;
88559163Seric 		auto int stat;
8869382Seric 
8879382Seric 		/*
8889382Seric 		**  Find the end of the name.  New style names
8899382Seric 		**  end with a comma, old style names end with
8909382Seric 		**  a space character.  However, spaces do not
8919382Seric 		**  necessarily delimit an old-style name -- at
8929382Seric 		**  signs mean keep going.
8939382Seric 		*/
8949382Seric 
8959382Seric 		/* find end of name */
89658050Seric 		while ((isascii(*p) && isspace(*p)) || *p == ',')
8979382Seric 			p++;
8989382Seric 		name = p;
8999382Seric 		for (;;)
9009382Seric 		{
90158333Seric 			auto char *oldp;
90216909Seric 			char pvpbuf[PSBUFSIZE];
9039382Seric 
90458333Seric 			(void) prescan(p, oldstyle ? ' ' : ',', pvpbuf, &oldp);
90558333Seric 			p = oldp;
9069382Seric 
9079382Seric 			/* look to see if we have an at sign */
90858050Seric 			while (*p != '\0' && isascii(*p) && isspace(*p))
9099382Seric 				p++;
9109382Seric 
91158170Seric 			if (*p != '@')
9129382Seric 			{
9139382Seric 				p = oldp;
9149382Seric 				break;
9159382Seric 			}
9169382Seric 			p += *p == '@' ? 1 : 2;
91758050Seric 			while (*p != '\0' && isascii(*p) && isspace(*p))
9189382Seric 				p++;
9199382Seric 		}
9209382Seric 		/* at the end of one complete name */
9219382Seric 
9229382Seric 		/* strip off trailing white space */
92358050Seric 		while (p >= name &&
92458050Seric 		       ((isascii(*p) && isspace(*p)) || *p == ',' || *p == '\0'))
9259382Seric 			p--;
9269382Seric 		if (++p == name)
9279382Seric 			continue;
9289382Seric 		savechar = *p;
9299382Seric 		*p = '\0';
9309382Seric 
9319382Seric 		/* translate the name to be relative */
93259163Seric 		flags = RF_HEADERADDR|RF_ADDDOMAIN;
93359163Seric 		if (bitset(H_FROM, h->h_flags))
93459163Seric 			flags |= RF_SENDERADDR;
93559163Seric 		stat = EX_OK;
93659163Seric 		name = remotename(name, m, flags, &stat, e);
9379382Seric 		if (*name == '\0')
9389382Seric 		{
9399382Seric 			*p = savechar;
9409382Seric 			continue;
9419382Seric 		}
9429382Seric 
9439382Seric 		/* output the name with nice formatting */
94454983Seric 		opos += strlen(name);
9459382Seric 		if (!firstone)
9469382Seric 			opos += 2;
9479382Seric 		if (opos > 78 && !firstone)
9489382Seric 		{
94910178Seric 			(void) strcpy(obp, ",\n");
95010176Seric 			putline(obuf, fp, m);
9519382Seric 			obp = obuf;
9529382Seric 			(void) sprintf(obp, "        ");
95310161Seric 			opos = strlen(obp);
95410161Seric 			obp += opos;
95554983Seric 			opos += strlen(name);
9569382Seric 		}
9579382Seric 		else if (!firstone)
9589382Seric 		{
9599382Seric 			(void) sprintf(obp, ", ");
9609382Seric 			obp += 2;
9619382Seric 		}
9629382Seric 
96354983Seric 		while ((c = *name++) != '\0' && obp < &obuf[MAXLINE])
96454983Seric 			*obp++ = c;
9659382Seric 		firstone = FALSE;
9669382Seric 		*p = savechar;
9679382Seric 	}
9689382Seric 	(void) strcpy(obp, "\n");
96910176Seric 	putline(obuf, fp, m);
9709382Seric }
9719382Seric /*
97258170Seric **  COPYHEADER -- copy header list
9739382Seric **
97458170Seric **	This routine is the equivalent of newstr for header lists
97558170Seric **
9769382Seric **	Parameters:
97758170Seric **		header -- list of header structures to copy.
9789382Seric **
9799382Seric **	Returns:
98058170Seric **		a copy of 'header'.
9819382Seric **
9829382Seric **	Side Effects:
9839382Seric **		none.
9849382Seric */
9859382Seric 
98658170Seric HDR *
98758170Seric copyheader(header)
98858170Seric 	register HDR *header;
9899382Seric {
99058170Seric 	register HDR *newhdr;
99158170Seric 	HDR *ret;
99258170Seric 	register HDR **tail = &ret;
9939382Seric 
99458170Seric 	while (header != NULL)
99558170Seric 	{
99658170Seric 		newhdr = (HDR *) xalloc(sizeof(HDR));
99758170Seric 		STRUCTCOPY(*header, *newhdr);
99858170Seric 		*tail = newhdr;
99958170Seric 		tail = &newhdr->h_link;
100058170Seric 		header = header->h_link;
100158170Seric 	}
100258170Seric 	*tail = NULL;
100358170Seric 
100458170Seric 	return ret;
10059382Seric }
1006