122706Sdist /*
234921Sbostic  * Copyright (c) 1983 Eric P. Allman
3*62525Sbostic  * Copyright (c) 1988, 1993
4*62525Sbostic  *	The Regents of the University of California.  All rights reserved.
533729Sbostic  *
642826Sbostic  * %sccs.include.redist.c%
733729Sbostic  */
822706Sdist 
922706Sdist #ifndef lint
10*62525Sbostic static char sccsid[] = "@(#)headers.c	8.1 (Berkeley) 06/07/93";
1133729Sbostic #endif /* not lint */
1222706Sdist 
134091Seric # include <errno.h>
144091Seric # include "sendmail.h"
154091Seric 
164091Seric /*
174091Seric **  CHOMPHEADER -- process and save a header line.
184091Seric **
194091Seric **	Called by collect and by readcf to deal with header lines.
204091Seric **
214091Seric **	Parameters:
224091Seric **		line -- header as a text line.
234091Seric **		def -- if set, this is a default value.
2455012Seric **		e -- the envelope including this header.
254091Seric **
264091Seric **	Returns:
274091Seric **		flags for this header.
284091Seric **
294091Seric **	Side Effects:
304091Seric **		The header is saved on the header list.
314319Seric **		Contents of 'line' are destroyed.
324091Seric */
334091Seric 
3455012Seric chompheader(line, def, e)
354091Seric 	char *line;
364091Seric 	bool def;
3755012Seric 	register ENVELOPE *e;
384091Seric {
394091Seric 	register char *p;
404091Seric 	register HDR *h;
414091Seric 	HDR **hp;
424091Seric 	char *fname;
434091Seric 	char *fvalue;
444091Seric 	struct hdrinfo *hi;
459059Seric 	bool cond = FALSE;
4610689Seric 	BITMAP mopts;
474091Seric 
487677Seric 	if (tTd(31, 6))
497677Seric 		printf("chompheader: %s\n", line);
507677Seric 
514627Seric 	/* strip off options */
5210689Seric 	clrbitmap(mopts);
534627Seric 	p = line;
5457405Seric 	if (*p == '?')
554627Seric 	{
564627Seric 		/* have some */
5756795Seric 		register char *q = strchr(p + 1, *p);
584627Seric 
594627Seric 		if (q != NULL)
604627Seric 		{
614627Seric 			*q++ = '\0';
6210689Seric 			while (*++p != '\0')
6310689Seric 				setbitn(*p, mopts);
644627Seric 			p = q;
654627Seric 		}
664627Seric 		else
6758151Seric 			usrerr("553 header syntax error, line \"%s\"", line);
689059Seric 		cond = TRUE;
694627Seric 	}
704627Seric 
714091Seric 	/* find canonical name */
724627Seric 	fname = p;
7356795Seric 	p = strchr(p, ':');
7410118Seric 	if (p == NULL)
7510118Seric 	{
7658151Seric 		syserr("553 header syntax error, line \"%s\"", line);
7710118Seric 		return (0);
7810118Seric 	}
794091Seric 	fvalue = &p[1];
8058050Seric 	while (isascii(*--p) && isspace(*p))
814091Seric 		continue;
824091Seric 	*++p = '\0';
834091Seric 
844091Seric 	/* strip field value on front */
854091Seric 	if (*fvalue == ' ')
864091Seric 		fvalue++;
874091Seric 
884091Seric 	/* see if it is a known type */
894091Seric 	for (hi = HdrInfo; hi->hi_field != NULL; hi++)
904091Seric 	{
9159579Seric 		if (strcasecmp(hi->hi_field, fname) == 0)
924091Seric 			break;
934091Seric 	}
944091Seric 
9511414Seric 	/* see if this is a resent message */
9611930Seric 	if (!def && bitset(H_RESENT, hi->hi_flags))
9755012Seric 		e->e_flags |= EF_RESENT;
9811414Seric 
994091Seric 	/* if this means "end of header" quit now */
1004091Seric 	if (bitset(H_EOH, hi->hi_flags))
1014091Seric 		return (hi->hi_flags);
1024091Seric 
10311414Seric 	/* drop explicit From: if same as what we would generate -- for MH */
10414784Seric 	p = "resent-from";
10555012Seric 	if (!bitset(EF_RESENT, e->e_flags))
10614784Seric 		p += 7;
10759579Seric 	if (!def && !bitset(EF_QUEUERUN, e->e_flags) && strcasecmp(fname, p) == 0)
10811414Seric 	{
10955012Seric 		if (e->e_from.q_paddr != NULL &&
11055012Seric 		    strcmp(fvalue, e->e_from.q_paddr) == 0)
11111414Seric 			return (hi->hi_flags);
11211414Seric 	}
11311414Seric 
11414784Seric 	/* delete default value for this header */
11555012Seric 	for (hp = &e->e_header; (h = *hp) != NULL; hp = &h->h_link)
11614784Seric 	{
11759579Seric 		if (strcasecmp(fname, h->h_field) == 0 &&
11814784Seric 		    bitset(H_DEFAULT, h->h_flags) &&
11914784Seric 		    !bitset(H_FORCE, h->h_flags))
12014784Seric 			h->h_value = NULL;
12114784Seric 	}
12214784Seric 
12313012Seric 	/* create a new node */
12413012Seric 	h = (HDR *) xalloc(sizeof *h);
12513012Seric 	h->h_field = newstr(fname);
12613012Seric 	h->h_value = NULL;
12713012Seric 	h->h_link = NULL;
12823117Seric 	bcopy((char *) mopts, (char *) h->h_mflags, sizeof mopts);
12913012Seric 	*hp = h;
1308066Seric 	h->h_flags = hi->hi_flags;
1314091Seric 	if (def)
1324091Seric 		h->h_flags |= H_DEFAULT;
1339059Seric 	if (cond)
1349059Seric 		h->h_flags |= H_CHECK;
1354091Seric 	if (h->h_value != NULL)
1369351Seric 		free((char *) h->h_value);
1378066Seric 	h->h_value = newstr(fvalue);
1384091Seric 
1395937Seric 	/* hack to see if this is a new format message */
1408095Seric 	if (!def && bitset(H_RCPT|H_FROM, h->h_flags) &&
14156795Seric 	    (strchr(fvalue, ',') != NULL || strchr(fvalue, '(') != NULL ||
14256795Seric 	     strchr(fvalue, '<') != NULL || strchr(fvalue, ';') != NULL))
1438089Seric 	{
14455012Seric 		e->e_flags &= ~EF_OLDSTYLE;
1458089Seric 	}
1465937Seric 
1474091Seric 	return (h->h_flags);
1484091Seric }
1494091Seric /*
1506980Seric **  ADDHEADER -- add a header entry to the end of the queue.
1516980Seric **
1526980Seric **	This bypasses the special checking of chompheader.
1536980Seric **
1546980Seric **	Parameters:
15559579Seric **		field -- the name of the header field.
15658789Seric **		value -- the value of the field.
1576980Seric **		e -- the envelope to add them to.
1586980Seric **
1596980Seric **	Returns:
1606980Seric **		none.
1616980Seric **
1626980Seric **	Side Effects:
1636980Seric **		adds the field on the list of headers for this envelope.
1646980Seric */
1656980Seric 
1666980Seric addheader(field, value, e)
1676980Seric 	char *field;
1686980Seric 	char *value;
1696980Seric 	ENVELOPE *e;
1706980Seric {
1716980Seric 	register HDR *h;
1726980Seric 	register struct hdrinfo *hi;
1736980Seric 	HDR **hp;
1746980Seric 
1756980Seric 	/* find info struct */
1766980Seric 	for (hi = HdrInfo; hi->hi_field != NULL; hi++)
1776980Seric 	{
17859579Seric 		if (strcasecmp(field, hi->hi_field) == 0)
1796980Seric 			break;
1806980Seric 	}
1816980Seric 
1826980Seric 	/* find current place in list -- keep back pointer? */
1836980Seric 	for (hp = &e->e_header; (h = *hp) != NULL; hp = &h->h_link)
1846980Seric 	{
18559579Seric 		if (strcasecmp(field, h->h_field) == 0)
1866980Seric 			break;
1876980Seric 	}
1886980Seric 
1896980Seric 	/* allocate space for new header */
1906980Seric 	h = (HDR *) xalloc(sizeof *h);
1916980Seric 	h->h_field = field;
1926980Seric 	h->h_value = newstr(value);
1937368Seric 	h->h_link = *hp;
1946980Seric 	h->h_flags = hi->hi_flags | H_DEFAULT;
19510689Seric 	clrbitmap(h->h_mflags);
1966980Seric 	*hp = h;
1976980Seric }
1986980Seric /*
1994091Seric **  HVALUE -- return value of a header.
2004091Seric **
2014091Seric **	Only "real" fields (i.e., ones that have not been supplied
2024091Seric **	as a default) are used.
2034091Seric **
2044091Seric **	Parameters:
2054091Seric **		field -- the field name.
20655012Seric **		e -- the envelope containing the header.
2074091Seric **
2084091Seric **	Returns:
2094091Seric **		pointer to the value part.
2104091Seric **		NULL if not found.
2114091Seric **
2124091Seric **	Side Effects:
2139382Seric **		none.
2144091Seric */
2154091Seric 
2164091Seric char *
21755012Seric hvalue(field, e)
2184091Seric 	char *field;
21955012Seric 	register ENVELOPE *e;
2204091Seric {
2214091Seric 	register HDR *h;
2224091Seric 
22355012Seric 	for (h = e->e_header; h != NULL; h = h->h_link)
2244091Seric 	{
22559579Seric 		if (!bitset(H_DEFAULT, h->h_flags) &&
22659579Seric 		    strcasecmp(h->h_field, field) == 0)
2274091Seric 			return (h->h_value);
2284091Seric 	}
2294091Seric 	return (NULL);
2304091Seric }
2314091Seric /*
2324091Seric **  ISHEADER -- predicate telling if argument is a header.
2334091Seric **
2344319Seric **	A line is a header if it has a single word followed by
2354319Seric **	optional white space followed by a colon.
2364319Seric **
2374091Seric **	Parameters:
2384091Seric **		s -- string to check for possible headerness.
2394091Seric **
2404091Seric **	Returns:
2414091Seric **		TRUE if s is a header.
2424091Seric **		FALSE otherwise.
2434091Seric **
2444091Seric **	Side Effects:
2454091Seric **		none.
2464091Seric */
2474091Seric 
2484091Seric bool
2494091Seric isheader(s)
2504091Seric 	register char *s;
2514091Seric {
2529382Seric 	while (*s > ' ' && *s != ':' && *s != '\0')
2534091Seric 		s++;
2549382Seric 
2559382Seric 	/* following technically violates RFC822 */
25658050Seric 	while (isascii(*s) && isspace(*s))
2574091Seric 		s++;
2589382Seric 
2594091Seric 	return (*s == ':');
2604091Seric }
2615919Seric /*
2627783Seric **  EATHEADER -- run through the stored header and extract info.
2637783Seric **
2647783Seric **	Parameters:
2659382Seric **		e -- the envelope to process.
26658929Seric **		full -- if set, do full processing (e.g., compute
26758929Seric **			message priority).
2687783Seric **
2697783Seric **	Returns:
2707783Seric **		none.
2717783Seric **
2727783Seric **	Side Effects:
2737783Seric **		Sets a bunch of global variables from information
2749382Seric **			in the collected header.
2759382Seric **		Aborts the message if the hop count is exceeded.
2767783Seric */
2777783Seric 
27858929Seric eatheader(e, full)
2799382Seric 	register ENVELOPE *e;
28058929Seric 	bool full;
2817783Seric {
2827783Seric 	register HDR *h;
2837783Seric 	register char *p;
2849382Seric 	int hopcnt = 0;
28557208Seric 	char *msgid;
28658688Seric 	char buf[MAXLINE];
2877783Seric 
28858688Seric 	/*
28958688Seric 	**  Set up macros for possible expansion in headers.
29058688Seric 	*/
29158688Seric 
29258704Seric 	define('f', e->e_sender, e);
29358704Seric 	define('g', e->e_sender, e);
29458688Seric 
2959382Seric 	if (tTd(32, 1))
2969382Seric 		printf("----- collected header -----\n");
29757208Seric 	msgid = "<none>";
2989382Seric 	for (h = e->e_header; h != NULL; h = h->h_link)
2997783Seric 	{
30058688Seric 		/* do early binding */
30158688Seric 		if (bitset(H_DEFAULT, h->h_flags) && h->h_value != NULL)
30258688Seric 		{
30358688Seric 			expand(h->h_value, buf, &buf[sizeof buf], e);
30458688Seric 			if (buf[0] != '\0')
30558688Seric 			{
30658688Seric 				h->h_value = newstr(buf);
30758688Seric 				h->h_flags &= ~H_DEFAULT;
30858688Seric 			}
30958688Seric 		}
31058688Seric 
3119382Seric 		if (tTd(32, 1))
31259579Seric 			printf("%s: %s\n", h->h_field, h->h_value);
31357359Seric 
31411414Seric 		/* count the number of times it has been processed */
3159382Seric 		if (bitset(H_TRACE, h->h_flags))
3169382Seric 			hopcnt++;
31711414Seric 
31811414Seric 		/* send to this person if we so desire */
31911414Seric 		if (GrabTo && bitset(H_RCPT, h->h_flags) &&
32011414Seric 		    !bitset(H_DEFAULT, h->h_flags) &&
32155012Seric 		    (!bitset(EF_RESENT, e->e_flags) || bitset(H_RESENT, h->h_flags)))
32211414Seric 		{
32358082Seric 			(void) sendtolist(h->h_value, (ADDRESS *) NULL,
32458082Seric 					  &e->e_sendqueue, e);
32511414Seric 		}
32611414Seric 
32757208Seric 		/* save the message-id for logging */
32858929Seric 		if (full && h->h_value != NULL &&
32959579Seric 		    strcasecmp(h->h_field, "message-id") == 0)
33011290Seric 		{
33157208Seric 			msgid = h->h_value;
33259859Seric 			while (isascii(*msgid) && isspace(*msgid))
33359859Seric 				msgid++;
33411290Seric 		}
33557359Seric 
33657359Seric 		/* see if this is a return-receipt header */
33757359Seric 		if (bitset(H_RECEIPTTO, h->h_flags))
33857359Seric 			e->e_receiptto = h->h_value;
33957359Seric 
34057359Seric 		/* see if this is an errors-to header */
34161104Seric 		if (UseErrorsTo && bitset(H_ERRORSTO, h->h_flags))
34258082Seric 			(void) sendtolist(h->h_value, (ADDRESS *) NULL,
34358082Seric 					  &e->e_errorqueue, e);
3449382Seric 	}
3459382Seric 	if (tTd(32, 1))
3467783Seric 		printf("----------------------------\n");
3477783Seric 
34858145Seric 	/* if we are just verifying (that is, sendmail -t -bv), drop out now */
34958145Seric 	if (OpMode == MD_VERIFY)
35058145Seric 		return;
35158145Seric 
3529382Seric 	/* store hop count */
3539382Seric 	if (hopcnt > e->e_hopcount)
3549382Seric 		e->e_hopcount = hopcnt;
3559382Seric 
3567783Seric 	/* message priority */
35755012Seric 	p = hvalue("precedence", e);
3589382Seric 	if (p != NULL)
3599382Seric 		e->e_class = priencode(p);
36058929Seric 	if (full)
36125013Seric 		e->e_msgpriority = e->e_msgsize
36224981Seric 				 - e->e_class * WkClassFact
36324981Seric 				 + e->e_nrcpts * WkRecipFact;
3647783Seric 
3657783Seric 	/* full name of from person */
36655012Seric 	p = hvalue("full-name", e);
3677783Seric 	if (p != NULL)
3689382Seric 		define('x', p, e);
3697783Seric 
3707783Seric 	/* date message originated */
37155012Seric 	p = hvalue("posted-date", e);
3727783Seric 	if (p == NULL)
37355012Seric 		p = hvalue("date", e);
3747783Seric 	if (p != NULL)
3759382Seric 		define('a', p, e);
37611290Seric 
37711290Seric 	/*
37811290Seric 	**  Log collection information.
37911290Seric 	*/
38011290Seric 
38111290Seric # ifdef LOG
38258929Seric 	if (full && LogLevel > 4)
38311290Seric 	{
38457359Seric 		char *name;
38560575Seric 		register char *sbp;
38657232Seric 		char hbuf[MAXNAME];
38757359Seric 		char sbuf[MAXLINE];
38836230Skarels 
38958667Seric 		if (bitset(EF_RESPONSE, e->e_flags))
39058667Seric 			name = "[RESPONSE]";
39158951Seric 		else if ((name = macvalue('_', e)) != NULL)
39258951Seric 			;
39358667Seric 		else if (RealHostName[0] == '[')
39436230Skarels 			name = RealHostName;
39536230Skarels 		else
39657359Seric 		{
39757359Seric 			name = hbuf;
39858798Seric 			(void) sprintf(hbuf, "%.80s", RealHostName);
39958798Seric 			if (RealHostAddr.sa.sa_family != 0)
40058798Seric 			{
40158798Seric 				p = &hbuf[strlen(hbuf)];
40258798Seric 				(void) sprintf(p, " (%s)",
40358798Seric 					anynet_ntoa(&RealHostAddr));
40458798Seric 			}
40557359Seric 		}
40657359Seric 
40757359Seric 		/* some versions of syslog only take 5 printf args */
40860575Seric 		sbp = sbuf;
40960575Seric 		sprintf(sbp, "from=%.200s, size=%ld, class=%d, pri=%ld, nrcpts=%d, msgid=%.100s",
41058558Seric 		    e->e_from.q_paddr, e->e_msgsize, e->e_class,
41157589Seric 		    e->e_msgpriority, e->e_nrcpts, msgid);
41260575Seric 		sbp += strlen(sbp);
41360575Seric 		if (e->e_bodytype != NULL)
41460575Seric 		{
41560575Seric 			(void) sprintf(sbp, ", bodytype=%.20s", e->e_bodytype);
41660575Seric 			sbp += strlen(sbp);
41760575Seric 		}
41860575Seric 		p = macvalue('r', e);
41960575Seric 		if (p != NULL)
42060575Seric 			(void) sprintf(sbp, ", proto=%.20s", p);
42158686Seric 		syslog(LOG_INFO, "%s: %s, relay=%s",
42257359Seric 		    e->e_id, sbuf, name);
42311290Seric 	}
42456795Seric # endif /* LOG */
4257783Seric }
4267783Seric /*
4277783Seric **  PRIENCODE -- encode external priority names into internal values.
4287783Seric **
4297783Seric **	Parameters:
4307783Seric **		p -- priority in ascii.
4317783Seric **
4327783Seric **	Returns:
4337783Seric **		priority as a numeric level.
4347783Seric **
4357783Seric **	Side Effects:
4367783Seric **		none.
4377783Seric */
4387783Seric 
4397783Seric priencode(p)
4407783Seric 	char *p;
4417783Seric {
4428253Seric 	register int i;
4437783Seric 
4448253Seric 	for (i = 0; i < NumPriorities; i++)
4457783Seric 	{
44633725Sbostic 		if (!strcasecmp(p, Priorities[i].pri_name))
4478253Seric 			return (Priorities[i].pri_val);
4487783Seric 	}
4498253Seric 
4508253Seric 	/* unknown priority */
4518253Seric 	return (0);
4527783Seric }
4537783Seric /*
4547890Seric **  CRACKADDR -- parse an address and turn it into a macro
4557783Seric **
4567783Seric **	This doesn't actually parse the address -- it just extracts
4577783Seric **	it and replaces it with "$g".  The parse is totally ad hoc
4587783Seric **	and isn't even guaranteed to leave something syntactically
4597783Seric **	identical to what it started with.  However, it does leave
4607783Seric **	something semantically identical.
4617783Seric **
46251379Seric **	This algorithm has been cleaned up to handle a wider range
46351379Seric **	of cases -- notably quoted and backslash escaped strings.
46451379Seric **	This modification makes it substantially better at preserving
46551379Seric **	the original syntax.
4667783Seric **
4677783Seric **	Parameters:
4687890Seric **		addr -- the address to be cracked.
4697783Seric **
4707783Seric **	Returns:
4717783Seric **		a pointer to the new version.
4727783Seric **
4737783Seric **	Side Effects:
4747890Seric **		none.
4757783Seric **
4767783Seric **	Warning:
4777783Seric **		The return value is saved in local storage and should
4787783Seric **		be copied if it is to be reused.
4797783Seric */
4807783Seric 
4817783Seric char *
4827890Seric crackaddr(addr)
4837890Seric 	register char *addr;
4847783Seric {
4857783Seric 	register char *p;
48651379Seric 	register char c;
48751379Seric 	int cmtlev;
48856764Seric 	int realcmtlev;
48956764Seric 	int anglelev, realanglelev;
49051379Seric 	int copylev;
49151379Seric 	bool qmode;
49256764Seric 	bool realqmode;
49356764Seric 	bool skipping;
49451379Seric 	bool putgmac = FALSE;
49556735Seric 	bool quoteit = FALSE;
49651379Seric 	register char *bp;
49756764Seric 	char *buflim;
4987783Seric 	static char buf[MAXNAME];
4997783Seric 
5007783Seric 	if (tTd(33, 1))
5017890Seric 		printf("crackaddr(%s)\n", addr);
5027783Seric 
5038082Seric 	/* strip leading spaces */
50458050Seric 	while (*addr != '\0' && isascii(*addr) && isspace(*addr))
5058082Seric 		addr++;
5068082Seric 
5077783Seric 	/*
50851379Seric 	**  Start by assuming we have no angle brackets.  This will be
50951379Seric 	**  adjusted later if we find them.
5107783Seric 	*/
5117783Seric 
51251379Seric 	bp = buf;
51356764Seric 	buflim = &buf[sizeof buf - 5];
51451379Seric 	p = addr;
51556764Seric 	copylev = anglelev = realanglelev = cmtlev = realcmtlev = 0;
51656764Seric 	qmode = realqmode = FALSE;
51751379Seric 
51851379Seric 	while ((c = *p++) != '\0')
5197783Seric 	{
52056764Seric 		/*
52156764Seric 		**  If the buffer is overful, go into a special "skipping"
52256764Seric 		**  mode that tries to keep legal syntax but doesn't actually
52356764Seric 		**  output things.
52456764Seric 		*/
5257783Seric 
52656764Seric 		skipping = bp >= buflim;
52756735Seric 
52856764Seric 		if (copylev > 0 && !skipping)
52956764Seric 			*bp++ = c;
53056735Seric 
53151379Seric 		/* check for backslash escapes */
53251379Seric 		if (c == '\\')
5337783Seric 		{
53458890Seric 			/* arrange to quote the address */
53558890Seric 			if (cmtlev <= 0 && !qmode)
53658890Seric 				quoteit = TRUE;
53758890Seric 
53851379Seric 			if ((c = *p++) == '\0')
5397783Seric 			{
54051379Seric 				/* too far */
54151379Seric 				p--;
54251379Seric 				goto putg;
5437783Seric 			}
54456764Seric 			if (copylev > 0 && !skipping)
54551379Seric 				*bp++ = c;
54651379Seric 			goto putg;
5477783Seric 		}
5487783Seric 
54951379Seric 		/* check for quoted strings */
55051379Seric 		if (c == '"')
5517783Seric 		{
55251379Seric 			qmode = !qmode;
55356764Seric 			if (copylev > 0 && !skipping)
55456764Seric 				realqmode = !realqmode;
55551379Seric 			continue;
5567783Seric 		}
55751379Seric 		if (qmode)
55851379Seric 			goto putg;
5597783Seric 
56051379Seric 		/* check for comments */
56151379Seric 		if (c == '(')
5627783Seric 		{
56351379Seric 			cmtlev++;
56456764Seric 
56556764Seric 			/* allow space for closing paren */
56656764Seric 			if (!skipping)
56756764Seric 			{
56856764Seric 				buflim--;
56956764Seric 				realcmtlev++;
57056764Seric 				if (copylev++ <= 0)
57156764Seric 				{
57256764Seric 					*bp++ = ' ';
57356764Seric 					*bp++ = c;
57456764Seric 				}
57556764Seric 			}
57651379Seric 		}
57751379Seric 		if (cmtlev > 0)
57851379Seric 		{
57951379Seric 			if (c == ')')
5807783Seric 			{
58151379Seric 				cmtlev--;
58251379Seric 				copylev--;
58356764Seric 				if (!skipping)
58456764Seric 				{
58556764Seric 					realcmtlev--;
58656764Seric 					buflim++;
58756764Seric 				}
5887783Seric 			}
5897783Seric 			continue;
5907783Seric 		}
59156764Seric 		else if (c == ')')
59256764Seric 		{
59356764Seric 			/* syntax error: unmatched ) */
59456764Seric 			if (!skipping)
59556764Seric 				bp--;
59656764Seric 		}
5977783Seric 
59856764Seric 
59956764Seric 		/* check for characters that may have to be quoted */
60057175Seric 		if (strchr(".'@,;:\\()", c) != NULL)
60156764Seric 		{
60256764Seric 			/*
60356764Seric 			**  If these occur as the phrase part of a <>
60456764Seric 			**  construct, but are not inside of () or already
60556764Seric 			**  quoted, they will have to be quoted.  Note that
60656764Seric 			**  now (but don't actually do the quoting).
60756764Seric 			*/
60856764Seric 
60956764Seric 			if (cmtlev <= 0 && !qmode)
61056764Seric 				quoteit = TRUE;
61156764Seric 		}
61256764Seric 
61351379Seric 		/* check for angle brackets */
61451379Seric 		if (c == '<')
61551379Seric 		{
61656735Seric 			register char *q;
61756735Seric 
61851379Seric 			/* oops -- have to change our mind */
61956764Seric 			anglelev++;
62056764Seric 			if (!skipping)
62156764Seric 				realanglelev++;
62256764Seric 
62356735Seric 			bp = buf;
62456735Seric 			if (quoteit)
62556735Seric 			{
62656735Seric 				*bp++ = '"';
62756735Seric 
62856735Seric 				/* back up over the '<' and any spaces */
62956735Seric 				--p;
63058050Seric 				while (isascii(*--p) && isspace(*p))
63156735Seric 					continue;
63256735Seric 				p++;
63356735Seric 			}
63456735Seric 			for (q = addr; q < p; )
63556735Seric 			{
63656735Seric 				c = *q++;
63756764Seric 				if (bp < buflim)
63856764Seric 				{
63956764Seric 					if (quoteit && c == '"')
64056764Seric 						*bp++ = '\\';
64156764Seric 					*bp++ = c;
64256764Seric 				}
64356735Seric 			}
64456735Seric 			if (quoteit)
64556735Seric 			{
64656735Seric 				*bp++ = '"';
64756764Seric 				while ((c = *p++) != '<')
64856764Seric 				{
64956764Seric 					if (bp < buflim)
65056764Seric 						*bp++ = c;
65156764Seric 				}
65256764Seric 				*bp++ = c;
65356735Seric 			}
65451379Seric 			copylev = 0;
65556735Seric 			putgmac = quoteit = FALSE;
65651379Seric 			continue;
65751379Seric 		}
6587783Seric 
65951379Seric 		if (c == '>')
6607783Seric 		{
66156764Seric 			if (anglelev > 0)
66256764Seric 			{
66356764Seric 				anglelev--;
66456764Seric 				if (!skipping)
66556764Seric 				{
66656764Seric 					realanglelev--;
66756764Seric 					buflim++;
66856764Seric 				}
66956764Seric 			}
67056764Seric 			else if (!skipping)
67156764Seric 			{
67256764Seric 				/* syntax error: unmatched > */
67356764Seric 				if (copylev > 0)
67456764Seric 					bp--;
67556764Seric 				continue;
67656764Seric 			}
67751379Seric 			if (copylev++ <= 0)
67851379Seric 				*bp++ = c;
67951379Seric 			continue;
6807783Seric 		}
68151379Seric 
68251379Seric 		/* must be a real address character */
68351379Seric 	putg:
68451379Seric 		if (copylev <= 0 && !putgmac)
68551379Seric 		{
68658050Seric 			*bp++ = MACROEXPAND;
68751379Seric 			*bp++ = 'g';
68851379Seric 			putgmac = TRUE;
68951379Seric 		}
6907783Seric 	}
6917783Seric 
69256764Seric 	/* repair any syntactic damage */
69356764Seric 	if (realqmode)
69456764Seric 		*bp++ = '"';
69556764Seric 	while (realcmtlev-- > 0)
69656764Seric 		*bp++ = ')';
69756764Seric 	while (realanglelev-- > 0)
69856764Seric 		*bp++ = '>';
69951379Seric 	*bp++ = '\0';
7007783Seric 
7017783Seric 	if (tTd(33, 1))
7027944Seric 		printf("crackaddr=>`%s'\n", buf);
7037783Seric 
7047783Seric 	return (buf);
7057783Seric }
7069382Seric /*
7079382Seric **  PUTHEADER -- put the header part of a message from the in-core copy
7089382Seric **
7099382Seric **	Parameters:
7109382Seric **		fp -- file to put it on.
7119382Seric **		m -- mailer to use.
7129382Seric **		e -- envelope to use.
7139382Seric **
7149382Seric **	Returns:
7159382Seric **		none.
7169382Seric **
7179382Seric **	Side Effects:
7189382Seric **		none.
7199382Seric */
7209382Seric 
72157589Seric /*
72257589Seric  * Macro for fast max (not available in e.g. DG/UX, 386/ix).
72357589Seric  */
72457589Seric #ifndef MAX
72557589Seric # define MAX(a,b) (((a)>(b))?(a):(b))
72657589Seric #endif
72757589Seric 
72810176Seric putheader(fp, m, e)
7299382Seric 	register FILE *fp;
7309382Seric 	register MAILER *m;
7319382Seric 	register ENVELOPE *e;
7329382Seric {
73357135Seric 	char buf[MAX(MAXLINE,BUFSIZ)];
7349382Seric 	register HDR *h;
73557135Seric 	char obuf[MAXLINE];
7369382Seric 
73759882Seric 	if (tTd(34, 1))
73859882Seric 		printf("--- putheader, mailer = %s ---\n", m->m_name);
73959882Seric 
7409382Seric 	for (h = e->e_header; h != NULL; h = h->h_link)
7419382Seric 	{
7429382Seric 		register char *p;
74310689Seric 		extern bool bitintersect();
7449382Seric 
74559882Seric 		if (tTd(34, 11))
74659882Seric 		{
74759882Seric 			printf("  %s: ", h->h_field);
74859882Seric 			xputs(h->h_value);
74959882Seric 		}
75059882Seric 
7519382Seric 		if (bitset(H_CHECK|H_ACHECK, h->h_flags) &&
75210689Seric 		    !bitintersect(h->h_mflags, m->m_flags))
75359882Seric 		{
75459882Seric 			if (tTd(34, 11))
75559882Seric 				printf(" (skipped)\n");
7569382Seric 			continue;
75759882Seric 		}
7589382Seric 
75911414Seric 		/* handle Resent-... headers specially */
76011414Seric 		if (bitset(H_RESENT, h->h_flags) && !bitset(EF_RESENT, e->e_flags))
76159882Seric 		{
76259882Seric 			if (tTd(34, 11))
76359882Seric 				printf(" (skipped (resent))\n");
76411414Seric 			continue;
76559882Seric 		}
76659882Seric 		if (tTd(34, 11))
76759882Seric 			printf("\n");
76811414Seric 
7699382Seric 		p = h->h_value;
7709382Seric 		if (bitset(H_DEFAULT, h->h_flags))
7719382Seric 		{
7729382Seric 			/* macro expand value if generated internally */
7739382Seric 			expand(p, buf, &buf[sizeof buf], e);
7749382Seric 			p = buf;
7759382Seric 			if (p == NULL || *p == '\0')
7769382Seric 				continue;
7779382Seric 		}
7789382Seric 
7799382Seric 		if (bitset(H_FROM|H_RCPT, h->h_flags))
7809382Seric 		{
7819382Seric 			/* address field */
7829382Seric 			bool oldstyle = bitset(EF_OLDSTYLE, e->e_flags);
7839382Seric 
7849382Seric 			if (bitset(H_FROM, h->h_flags))
7859382Seric 				oldstyle = FALSE;
78655012Seric 			commaize(h, p, fp, oldstyle, m, e);
7879382Seric 		}
7889382Seric 		else
7899382Seric 		{
7909382Seric 			/* vanilla header line */
79112159Seric 			register char *nlp;
79212159Seric 
79359579Seric 			(void) sprintf(obuf, "%s: ", h->h_field);
79456795Seric 			while ((nlp = strchr(p, '\n')) != NULL)
79512159Seric 			{
79612159Seric 				*nlp = '\0';
79712159Seric 				(void) strcat(obuf, p);
79812159Seric 				*nlp = '\n';
79912159Seric 				putline(obuf, fp, m);
80012159Seric 				p = ++nlp;
80112161Seric 				obuf[0] = '\0';
80212159Seric 			}
80312159Seric 			(void) strcat(obuf, p);
80410176Seric 			putline(obuf, fp, m);
8059382Seric 		}
8069382Seric 	}
8079382Seric }
8089382Seric /*
8099382Seric **  COMMAIZE -- output a header field, making a comma-translated list.
8109382Seric **
8119382Seric **	Parameters:
8129382Seric **		h -- the header field to output.
8139382Seric **		p -- the value to put in it.
8149382Seric **		fp -- file to put it to.
8159382Seric **		oldstyle -- TRUE if this is an old style header.
8169382Seric **		m -- a pointer to the mailer descriptor.  If NULL,
8179382Seric **			don't transform the name at all.
81855012Seric **		e -- the envelope containing the message.
8199382Seric **
8209382Seric **	Returns:
8219382Seric **		none.
8229382Seric **
8239382Seric **	Side Effects:
8249382Seric **		outputs "p" to file "fp".
8259382Seric */
8269382Seric 
82755012Seric commaize(h, p, fp, oldstyle, m, e)
8289382Seric 	register HDR *h;
8299382Seric 	register char *p;
8309382Seric 	FILE *fp;
8319382Seric 	bool oldstyle;
8329382Seric 	register MAILER *m;
83355012Seric 	register ENVELOPE *e;
8349382Seric {
8359382Seric 	register char *obp;
8369382Seric 	int opos;
8379382Seric 	bool firstone = TRUE;
83811157Seric 	char obuf[MAXLINE + 3];
8399382Seric 
8409382Seric 	/*
8419382Seric 	**  Output the address list translated by the
8429382Seric 	**  mailer and with commas.
8439382Seric 	*/
8449382Seric 
8459382Seric 	if (tTd(14, 2))
8469382Seric 		printf("commaize(%s: %s)\n", h->h_field, p);
8479382Seric 
8489382Seric 	obp = obuf;
84959579Seric 	(void) sprintf(obp, "%s: ", h->h_field);
8509382Seric 	opos = strlen(h->h_field) + 2;
8519382Seric 	obp += opos;
8529382Seric 
8539382Seric 	/*
8549382Seric 	**  Run through the list of values.
8559382Seric 	*/
8569382Seric 
8579382Seric 	while (*p != '\0')
8589382Seric 	{
8599382Seric 		register char *name;
86054983Seric 		register int c;
8619382Seric 		char savechar;
86259163Seric 		int flags;
86359163Seric 		auto int stat;
8649382Seric 
8659382Seric 		/*
8669382Seric 		**  Find the end of the name.  New style names
8679382Seric 		**  end with a comma, old style names end with
8689382Seric 		**  a space character.  However, spaces do not
8699382Seric 		**  necessarily delimit an old-style name -- at
8709382Seric 		**  signs mean keep going.
8719382Seric 		*/
8729382Seric 
8739382Seric 		/* find end of name */
87458050Seric 		while ((isascii(*p) && isspace(*p)) || *p == ',')
8759382Seric 			p++;
8769382Seric 		name = p;
8779382Seric 		for (;;)
8789382Seric 		{
87958333Seric 			auto char *oldp;
88016909Seric 			char pvpbuf[PSBUFSIZE];
8819382Seric 
88258333Seric 			(void) prescan(p, oldstyle ? ' ' : ',', pvpbuf, &oldp);
88358333Seric 			p = oldp;
8849382Seric 
8859382Seric 			/* look to see if we have an at sign */
88658050Seric 			while (*p != '\0' && isascii(*p) && isspace(*p))
8879382Seric 				p++;
8889382Seric 
88958170Seric 			if (*p != '@')
8909382Seric 			{
8919382Seric 				p = oldp;
8929382Seric 				break;
8939382Seric 			}
8949382Seric 			p += *p == '@' ? 1 : 2;
89558050Seric 			while (*p != '\0' && isascii(*p) && isspace(*p))
8969382Seric 				p++;
8979382Seric 		}
8989382Seric 		/* at the end of one complete name */
8999382Seric 
9009382Seric 		/* strip off trailing white space */
90158050Seric 		while (p >= name &&
90258050Seric 		       ((isascii(*p) && isspace(*p)) || *p == ',' || *p == '\0'))
9039382Seric 			p--;
9049382Seric 		if (++p == name)
9059382Seric 			continue;
9069382Seric 		savechar = *p;
9079382Seric 		*p = '\0';
9089382Seric 
9099382Seric 		/* translate the name to be relative */
91059163Seric 		flags = RF_HEADERADDR|RF_ADDDOMAIN;
91159163Seric 		if (bitset(H_FROM, h->h_flags))
91259163Seric 			flags |= RF_SENDERADDR;
91359163Seric 		stat = EX_OK;
91459163Seric 		name = remotename(name, m, flags, &stat, e);
9159382Seric 		if (*name == '\0')
9169382Seric 		{
9179382Seric 			*p = savechar;
9189382Seric 			continue;
9199382Seric 		}
9209382Seric 
9219382Seric 		/* output the name with nice formatting */
92254983Seric 		opos += strlen(name);
9239382Seric 		if (!firstone)
9249382Seric 			opos += 2;
9259382Seric 		if (opos > 78 && !firstone)
9269382Seric 		{
92710178Seric 			(void) strcpy(obp, ",\n");
92810176Seric 			putline(obuf, fp, m);
9299382Seric 			obp = obuf;
9309382Seric 			(void) sprintf(obp, "        ");
93110161Seric 			opos = strlen(obp);
93210161Seric 			obp += opos;
93354983Seric 			opos += strlen(name);
9349382Seric 		}
9359382Seric 		else if (!firstone)
9369382Seric 		{
9379382Seric 			(void) sprintf(obp, ", ");
9389382Seric 			obp += 2;
9399382Seric 		}
9409382Seric 
94154983Seric 		while ((c = *name++) != '\0' && obp < &obuf[MAXLINE])
94254983Seric 			*obp++ = c;
9439382Seric 		firstone = FALSE;
9449382Seric 		*p = savechar;
9459382Seric 	}
9469382Seric 	(void) strcpy(obp, "\n");
94710176Seric 	putline(obuf, fp, m);
9489382Seric }
9499382Seric /*
95058170Seric **  COPYHEADER -- copy header list
9519382Seric **
95258170Seric **	This routine is the equivalent of newstr for header lists
95358170Seric **
9549382Seric **	Parameters:
95558170Seric **		header -- list of header structures to copy.
9569382Seric **
9579382Seric **	Returns:
95858170Seric **		a copy of 'header'.
9599382Seric **
9609382Seric **	Side Effects:
9619382Seric **		none.
9629382Seric */
9639382Seric 
96458170Seric HDR *
96558170Seric copyheader(header)
96658170Seric 	register HDR *header;
9679382Seric {
96858170Seric 	register HDR *newhdr;
96958170Seric 	HDR *ret;
97058170Seric 	register HDR **tail = &ret;
9719382Seric 
97258170Seric 	while (header != NULL)
97358170Seric 	{
97458170Seric 		newhdr = (HDR *) xalloc(sizeof(HDR));
97558170Seric 		STRUCTCOPY(*header, *newhdr);
97658170Seric 		*tail = newhdr;
97758170Seric 		tail = &newhdr->h_link;
97858170Seric 		header = header->h_link;
97958170Seric 	}
98058170Seric 	*tail = NULL;
98158170Seric 
98258170Seric 	return ret;
9839382Seric }
984