122706Sdist /*
234921Sbostic  * Copyright (c) 1983 Eric P. Allman
333729Sbostic  * Copyright (c) 1988 Regents of the University of California.
433729Sbostic  * All rights reserved.
533729Sbostic  *
642826Sbostic  * %sccs.include.redist.c%
733729Sbostic  */
822706Sdist 
922706Sdist #ifndef lint
10*57208Seric static char sccsid[] = "@(#)headers.c	5.29 (Berkeley) 12/18/92";
1133729Sbostic #endif /* not lint */
1222706Sdist 
1340960Sbostic # include <sys/param.h>
144091Seric # include <errno.h>
154091Seric # include "sendmail.h"
164091Seric 
174091Seric /*
184091Seric **  CHOMPHEADER -- process and save a header line.
194091Seric **
204091Seric **	Called by collect and by readcf to deal with header lines.
214091Seric **
224091Seric **	Parameters:
234091Seric **		line -- header as a text line.
244091Seric **		def -- if set, this is a default value.
2555012Seric **		e -- the envelope including this header.
264091Seric **
274091Seric **	Returns:
284091Seric **		flags for this header.
294091Seric **
304091Seric **	Side Effects:
314091Seric **		The header is saved on the header list.
324319Seric **		Contents of 'line' are destroyed.
334091Seric */
344091Seric 
3555012Seric chompheader(line, def, e)
364091Seric 	char *line;
374091Seric 	bool def;
3855012Seric 	register ENVELOPE *e;
394091Seric {
404091Seric 	register char *p;
414091Seric 	register HDR *h;
424091Seric 	HDR **hp;
434091Seric 	char *fname;
444091Seric 	char *fvalue;
454091Seric 	struct hdrinfo *hi;
469059Seric 	bool cond = FALSE;
4710689Seric 	BITMAP mopts;
484091Seric 
497677Seric 	if (tTd(31, 6))
507677Seric 		printf("chompheader: %s\n", line);
517677Seric 
524627Seric 	/* strip off options */
5310689Seric 	clrbitmap(mopts);
544627Seric 	p = line;
5556678Seric 	if (def && *p == '?')
564627Seric 	{
574627Seric 		/* have some */
5856795Seric 		register char *q = strchr(p + 1, *p);
594627Seric 
604627Seric 		if (q != NULL)
614627Seric 		{
624627Seric 			*q++ = '\0';
6310689Seric 			while (*++p != '\0')
6410689Seric 				setbitn(*p, mopts);
654627Seric 			p = q;
664627Seric 		}
674627Seric 		else
6836233Skarels 			usrerr("chompheader: syntax error, line \"%s\"", line);
699059Seric 		cond = TRUE;
704627Seric 	}
714627Seric 
724091Seric 	/* find canonical name */
734627Seric 	fname = p;
7456795Seric 	p = strchr(p, ':');
7510118Seric 	if (p == NULL)
7610118Seric 	{
7710118Seric 		syserr("chompheader: syntax error, line \"%s\"", line);
7810118Seric 		return (0);
7910118Seric 	}
804091Seric 	fvalue = &p[1];
814091Seric 	while (isspace(*--p))
824091Seric 		continue;
834091Seric 	*++p = '\0';
844091Seric 	makelower(fname);
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 	{
934091Seric 		if (strcmp(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;
10914784Seric 	if (!def && !QueueRun && strcmp(fname, p) == 0)
11011414Seric 	{
11155012Seric 		if (e->e_from.q_paddr != NULL &&
11255012Seric 		    strcmp(fvalue, e->e_from.q_paddr) == 0)
11311414Seric 			return (hi->hi_flags);
11411414Seric 	}
11511414Seric 
11614784Seric 	/* delete default value for this header */
11755012Seric 	for (hp = &e->e_header; (h = *hp) != NULL; hp = &h->h_link)
11814784Seric 	{
11914784Seric 		if (strcmp(fname, h->h_field) == 0 &&
12014784Seric 		    bitset(H_DEFAULT, h->h_flags) &&
12114784Seric 		    !bitset(H_FORCE, h->h_flags))
12214784Seric 			h->h_value = NULL;
12314784Seric 	}
12414784Seric 
12513012Seric 	/* create a new node */
12613012Seric 	h = (HDR *) xalloc(sizeof *h);
12713012Seric 	h->h_field = newstr(fname);
12813012Seric 	h->h_value = NULL;
12913012Seric 	h->h_link = NULL;
13023117Seric 	bcopy((char *) mopts, (char *) h->h_mflags, sizeof mopts);
13113012Seric 	*hp = h;
1328066Seric 	h->h_flags = hi->hi_flags;
1334091Seric 	if (def)
1344091Seric 		h->h_flags |= H_DEFAULT;
1359059Seric 	if (cond)
1369059Seric 		h->h_flags |= H_CHECK;
1374091Seric 	if (h->h_value != NULL)
1389351Seric 		free((char *) h->h_value);
1398066Seric 	h->h_value = newstr(fvalue);
1404091Seric 
1415937Seric 	/* hack to see if this is a new format message */
1428095Seric 	if (!def && bitset(H_RCPT|H_FROM, h->h_flags) &&
14356795Seric 	    (strchr(fvalue, ',') != NULL || strchr(fvalue, '(') != NULL ||
14456795Seric 	     strchr(fvalue, '<') != NULL || strchr(fvalue, ';') != NULL))
1458089Seric 	{
14655012Seric 		e->e_flags &= ~EF_OLDSTYLE;
1478089Seric 	}
1485937Seric 
1494091Seric 	return (h->h_flags);
1504091Seric }
1514091Seric /*
1526980Seric **  ADDHEADER -- add a header entry to the end of the queue.
1536980Seric **
1546980Seric **	This bypasses the special checking of chompheader.
1556980Seric **
1566980Seric **	Parameters:
1576980Seric **		field -- the name of the header field.
1586980Seric **		value -- the value of the field.  It must be lower-cased.
1596980Seric **		e -- the envelope to add them to.
1606980Seric **
1616980Seric **	Returns:
1626980Seric **		none.
1636980Seric **
1646980Seric **	Side Effects:
1656980Seric **		adds the field on the list of headers for this envelope.
1666980Seric */
1676980Seric 
1686980Seric addheader(field, value, e)
1696980Seric 	char *field;
1706980Seric 	char *value;
1716980Seric 	ENVELOPE *e;
1726980Seric {
1736980Seric 	register HDR *h;
1746980Seric 	register struct hdrinfo *hi;
1756980Seric 	HDR **hp;
1766980Seric 
1776980Seric 	/* find info struct */
1786980Seric 	for (hi = HdrInfo; hi->hi_field != NULL; hi++)
1796980Seric 	{
1806980Seric 		if (strcmp(field, hi->hi_field) == 0)
1816980Seric 			break;
1826980Seric 	}
1836980Seric 
1846980Seric 	/* find current place in list -- keep back pointer? */
1856980Seric 	for (hp = &e->e_header; (h = *hp) != NULL; hp = &h->h_link)
1866980Seric 	{
1876980Seric 		if (strcmp(field, h->h_field) == 0)
1886980Seric 			break;
1896980Seric 	}
1906980Seric 
1916980Seric 	/* allocate space for new header */
1926980Seric 	h = (HDR *) xalloc(sizeof *h);
1936980Seric 	h->h_field = field;
1946980Seric 	h->h_value = newstr(value);
1957368Seric 	h->h_link = *hp;
1966980Seric 	h->h_flags = hi->hi_flags | H_DEFAULT;
19710689Seric 	clrbitmap(h->h_mflags);
1986980Seric 	*hp = h;
1996980Seric }
2006980Seric /*
2014091Seric **  HVALUE -- return value of a header.
2024091Seric **
2034091Seric **	Only "real" fields (i.e., ones that have not been supplied
2044091Seric **	as a default) are used.
2054091Seric **
2064091Seric **	Parameters:
2074091Seric **		field -- the field name.
20855012Seric **		e -- the envelope containing the header.
2094091Seric **
2104091Seric **	Returns:
2114091Seric **		pointer to the value part.
2124091Seric **		NULL if not found.
2134091Seric **
2144091Seric **	Side Effects:
2159382Seric **		none.
2164091Seric */
2174091Seric 
2184091Seric char *
21955012Seric hvalue(field, e)
2204091Seric 	char *field;
22155012Seric 	register ENVELOPE *e;
2224091Seric {
2234091Seric 	register HDR *h;
2244091Seric 
22555012Seric 	for (h = e->e_header; h != NULL; h = h->h_link)
2264091Seric 	{
2274091Seric 		if (!bitset(H_DEFAULT, h->h_flags) && strcmp(h->h_field, field) == 0)
2284091Seric 			return (h->h_value);
2294091Seric 	}
2304091Seric 	return (NULL);
2314091Seric }
2324091Seric /*
2334091Seric **  ISHEADER -- predicate telling if argument is a header.
2344091Seric **
2354319Seric **	A line is a header if it has a single word followed by
2364319Seric **	optional white space followed by a colon.
2374319Seric **
2384091Seric **	Parameters:
2394091Seric **		s -- string to check for possible headerness.
2404091Seric **
2414091Seric **	Returns:
2424091Seric **		TRUE if s is a header.
2434091Seric **		FALSE otherwise.
2444091Seric **
2454091Seric **	Side Effects:
2464091Seric **		none.
2474091Seric */
2484091Seric 
2494091Seric bool
2504091Seric isheader(s)
2514091Seric 	register char *s;
2524091Seric {
2539382Seric 	while (*s > ' ' && *s != ':' && *s != '\0')
2544091Seric 		s++;
2559382Seric 
2569382Seric 	/* following technically violates RFC822 */
2574091Seric 	while (isspace(*s))
2584091Seric 		s++;
2599382Seric 
2604091Seric 	return (*s == ':');
2614091Seric }
2625919Seric /*
2637783Seric **  EATHEADER -- run through the stored header and extract info.
2647783Seric **
2657783Seric **	Parameters:
2669382Seric **		e -- the envelope to process.
2677783Seric **
2687783Seric **	Returns:
2697783Seric **		none.
2707783Seric **
2717783Seric **	Side Effects:
2727783Seric **		Sets a bunch of global variables from information
2739382Seric **			in the collected header.
2749382Seric **		Aborts the message if the hop count is exceeded.
2757783Seric */
2767783Seric 
2779382Seric eatheader(e)
2789382Seric 	register ENVELOPE *e;
2797783Seric {
2807783Seric 	register HDR *h;
2817783Seric 	register char *p;
2829382Seric 	int hopcnt = 0;
283*57208Seric 	char *msgid;
284*57208Seric 	char msgidbuf[MAXNAME];
2857783Seric 
2869382Seric 	if (tTd(32, 1))
2879382Seric 		printf("----- collected header -----\n");
288*57208Seric 	msgid = "<none>";
2899382Seric 	for (h = e->e_header; h != NULL; h = h->h_link)
2907783Seric 	{
2917783Seric 		extern char *capitalize();
2927783Seric 
2939382Seric 		if (tTd(32, 1))
2947783Seric 			printf("%s: %s\n", capitalize(h->h_field), h->h_value);
29511414Seric 		/* count the number of times it has been processed */
2969382Seric 		if (bitset(H_TRACE, h->h_flags))
2979382Seric 			hopcnt++;
29811414Seric 
29911414Seric 		/* send to this person if we so desire */
30011414Seric 		if (GrabTo && bitset(H_RCPT, h->h_flags) &&
30111414Seric 		    !bitset(H_DEFAULT, h->h_flags) &&
30255012Seric 		    (!bitset(EF_RESENT, e->e_flags) || bitset(H_RESENT, h->h_flags)))
30311414Seric 		{
30455012Seric 			sendtolist(h->h_value, (ADDRESS *) NULL,
30555012Seric 				   &e->e_sendqueue, e);
30611414Seric 		}
30711414Seric 
308*57208Seric 		/* save the message-id for logging */
309*57208Seric 		if (!QueueRun && h->h_value != NULL &&
31011299Seric 		    strcmp(h->h_field, "message-id") == 0)
31111290Seric 		{
312*57208Seric 			msgid = h->h_value;
31311290Seric 			if (bitset(H_DEFAULT, h->h_flags))
31411290Seric 			{
315*57208Seric 				expand(msgid, msgidbuf,
316*57208Seric 					&msgidbuf[sizeof msgidbuf], e);
317*57208Seric 				msgid = msgidbuf;
31811290Seric 			}
31911290Seric 		}
3209382Seric 	}
3219382Seric 	if (tTd(32, 1))
3227783Seric 		printf("----------------------------\n");
3237783Seric 
3249382Seric 	/* store hop count */
3259382Seric 	if (hopcnt > e->e_hopcount)
3269382Seric 		e->e_hopcount = hopcnt;
3279382Seric 
3287783Seric 	/* message priority */
32955012Seric 	p = hvalue("precedence", e);
3309382Seric 	if (p != NULL)
3319382Seric 		e->e_class = priencode(p);
3327783Seric 	if (!QueueRun)
33325013Seric 		e->e_msgpriority = e->e_msgsize
33424981Seric 				 - e->e_class * WkClassFact
33524981Seric 				 + e->e_nrcpts * WkRecipFact;
3367783Seric 
3378066Seric 	/* return receipt to */
33855012Seric 	p = hvalue("return-receipt-to", e);
3397783Seric 	if (p != NULL)
3409382Seric 		e->e_receiptto = p;
3417783Seric 
3428253Seric 	/* errors to */
34355012Seric 	p = hvalue("errors-to", e);
3448253Seric 	if (p != NULL)
34555012Seric 		sendtolist(p, (ADDRESS *) NULL, &e->e_errorqueue, e);
3468253Seric 
3477783Seric 	/* full name of from person */
34855012Seric 	p = hvalue("full-name", e);
3497783Seric 	if (p != NULL)
3509382Seric 		define('x', p, e);
3517783Seric 
3527783Seric 	/* date message originated */
35355012Seric 	p = hvalue("posted-date", e);
3547783Seric 	if (p == NULL)
35555012Seric 		p = hvalue("date", e);
3567783Seric 	if (p != NULL)
3577783Seric 	{
3589382Seric 		define('a', p, e);
3597783Seric 		/* we don't have a good way to do canonical conversion ....
3609382Seric 		define('d', newstr(arpatounix(p)), e);
3617783Seric 		.... so we will ignore the problem for the time being */
3627783Seric 	}
36311290Seric 
36411290Seric 	/*
36511290Seric 	**  Log collection information.
36611290Seric 	*/
36711290Seric 
36811290Seric # ifdef LOG
36911299Seric 	if (!QueueRun && LogLevel > 1)
37011290Seric 	{
37155019Seric 		char hbuf[100];
37255019Seric 		char *name = hbuf;
37355019Seric 		extern char *inet_ntoa();
37436230Skarels 
37536230Skarels 		if (RealHostName == NULL)
37636230Skarels 			name = "local";
37736230Skarels 		else if (RealHostName[0] == '[')
37836230Skarels 			name = RealHostName;
37936230Skarels 		else
38054999Seric 			(void)sprintf(hbuf, "%.80s (%s)",
38136230Skarels 			    RealHostName, inet_ntoa(RealHostAddr.sin_addr));
38236230Skarels 		syslog(LOG_INFO,
383*57208Seric 		    "%s: from=%s, size=%ld, class=%d, msgid=%s, received from %s\n",
38455012Seric 		    e->e_id, e->e_from.q_paddr, e->e_msgsize,
385*57208Seric 		    e->e_class, msgid, name);
38611290Seric 	}
38756795Seric # endif /* LOG */
3887783Seric }
3897783Seric /*
3907783Seric **  PRIENCODE -- encode external priority names into internal values.
3917783Seric **
3927783Seric **	Parameters:
3937783Seric **		p -- priority in ascii.
3947783Seric **
3957783Seric **	Returns:
3967783Seric **		priority as a numeric level.
3977783Seric **
3987783Seric **	Side Effects:
3997783Seric **		none.
4007783Seric */
4017783Seric 
4027783Seric priencode(p)
4037783Seric 	char *p;
4047783Seric {
4058253Seric 	register int i;
4067783Seric 
4078253Seric 	for (i = 0; i < NumPriorities; i++)
4087783Seric 	{
40933725Sbostic 		if (!strcasecmp(p, Priorities[i].pri_name))
4108253Seric 			return (Priorities[i].pri_val);
4117783Seric 	}
4128253Seric 
4138253Seric 	/* unknown priority */
4148253Seric 	return (0);
4157783Seric }
4167783Seric /*
4177890Seric **  CRACKADDR -- parse an address and turn it into a macro
4187783Seric **
4197783Seric **	This doesn't actually parse the address -- it just extracts
4207783Seric **	it and replaces it with "$g".  The parse is totally ad hoc
4217783Seric **	and isn't even guaranteed to leave something syntactically
4227783Seric **	identical to what it started with.  However, it does leave
4237783Seric **	something semantically identical.
4247783Seric **
42551379Seric **	This algorithm has been cleaned up to handle a wider range
42651379Seric **	of cases -- notably quoted and backslash escaped strings.
42751379Seric **	This modification makes it substantially better at preserving
42851379Seric **	the original syntax.
4297783Seric **
4307783Seric **	Parameters:
4317890Seric **		addr -- the address to be cracked.
4327783Seric **
4337783Seric **	Returns:
4347783Seric **		a pointer to the new version.
4357783Seric **
4367783Seric **	Side Effects:
4377890Seric **		none.
4387783Seric **
4397783Seric **	Warning:
4407783Seric **		The return value is saved in local storage and should
4417783Seric **		be copied if it is to be reused.
4427783Seric */
4437783Seric 
4447783Seric char *
4457890Seric crackaddr(addr)
4467890Seric 	register char *addr;
4477783Seric {
4487783Seric 	register char *p;
44951379Seric 	register char c;
45051379Seric 	int cmtlev;
45156764Seric 	int realcmtlev;
45256764Seric 	int anglelev, realanglelev;
45351379Seric 	int copylev;
45451379Seric 	bool qmode;
45556764Seric 	bool realqmode;
45656764Seric 	bool skipping;
45751379Seric 	bool putgmac = FALSE;
45856735Seric 	bool quoteit = FALSE;
45951379Seric 	register char *bp;
46056764Seric 	char *buflim;
4617783Seric 	static char buf[MAXNAME];
4627783Seric 
4637783Seric 	if (tTd(33, 1))
4647890Seric 		printf("crackaddr(%s)\n", addr);
4657783Seric 
4668082Seric 	/* strip leading spaces */
4678082Seric 	while (*addr != '\0' && isspace(*addr))
4688082Seric 		addr++;
4698082Seric 
4707783Seric 	/*
47151379Seric 	**  Start by assuming we have no angle brackets.  This will be
47251379Seric 	**  adjusted later if we find them.
4737783Seric 	*/
4747783Seric 
47551379Seric 	bp = buf;
47656764Seric 	buflim = &buf[sizeof buf - 5];
47751379Seric 	p = addr;
47856764Seric 	copylev = anglelev = realanglelev = cmtlev = realcmtlev = 0;
47956764Seric 	qmode = realqmode = FALSE;
48051379Seric 
48151379Seric 	while ((c = *p++) != '\0')
4827783Seric 	{
48356764Seric 		/*
48456764Seric 		**  If the buffer is overful, go into a special "skipping"
48556764Seric 		**  mode that tries to keep legal syntax but doesn't actually
48656764Seric 		**  output things.
48756764Seric 		*/
4887783Seric 
48956764Seric 		skipping = bp >= buflim;
49056735Seric 
49156764Seric 		if (copylev > 0 && !skipping)
49256764Seric 			*bp++ = c;
49356735Seric 
49451379Seric 		/* check for backslash escapes */
49551379Seric 		if (c == '\\')
4967783Seric 		{
49751379Seric 			if ((c = *p++) == '\0')
4987783Seric 			{
49951379Seric 				/* too far */
50051379Seric 				p--;
50151379Seric 				goto putg;
5027783Seric 			}
50356764Seric 			if (copylev > 0 && !skipping)
50451379Seric 				*bp++ = c;
50551379Seric 			goto putg;
5067783Seric 		}
5077783Seric 
50851379Seric 		/* check for quoted strings */
50951379Seric 		if (c == '"')
5107783Seric 		{
51151379Seric 			qmode = !qmode;
51256764Seric 			if (copylev > 0 && !skipping)
51356764Seric 				realqmode = !realqmode;
51451379Seric 			continue;
5157783Seric 		}
51651379Seric 		if (qmode)
51751379Seric 			goto putg;
5187783Seric 
51951379Seric 		/* check for comments */
52051379Seric 		if (c == '(')
5217783Seric 		{
52251379Seric 			cmtlev++;
52356764Seric 
52456764Seric 			/* allow space for closing paren */
52556764Seric 			if (!skipping)
52656764Seric 			{
52756764Seric 				buflim--;
52856764Seric 				realcmtlev++;
52956764Seric 				if (copylev++ <= 0)
53056764Seric 				{
53156764Seric 					*bp++ = ' ';
53256764Seric 					*bp++ = c;
53356764Seric 				}
53456764Seric 			}
53551379Seric 		}
53651379Seric 		if (cmtlev > 0)
53751379Seric 		{
53851379Seric 			if (c == ')')
5397783Seric 			{
54051379Seric 				cmtlev--;
54151379Seric 				copylev--;
54256764Seric 				if (!skipping)
54356764Seric 				{
54456764Seric 					realcmtlev--;
54556764Seric 					buflim++;
54656764Seric 				}
5477783Seric 			}
5487783Seric 			continue;
5497783Seric 		}
55056764Seric 		else if (c == ')')
55156764Seric 		{
55256764Seric 			/* syntax error: unmatched ) */
55356764Seric 			if (!skipping)
55456764Seric 				bp--;
55556764Seric 		}
5567783Seric 
55756764Seric 
55856764Seric 		/* check for characters that may have to be quoted */
55957175Seric 		if (strchr(".'@,;:\\()", c) != NULL)
56056764Seric 		{
56156764Seric 			/*
56256764Seric 			**  If these occur as the phrase part of a <>
56356764Seric 			**  construct, but are not inside of () or already
56456764Seric 			**  quoted, they will have to be quoted.  Note that
56556764Seric 			**  now (but don't actually do the quoting).
56656764Seric 			*/
56756764Seric 
56856764Seric 			if (cmtlev <= 0 && !qmode)
56956764Seric 				quoteit = TRUE;
57056764Seric 		}
57156764Seric 
57251379Seric 		/* check for angle brackets */
57351379Seric 		if (c == '<')
57451379Seric 		{
57556735Seric 			register char *q;
57656735Seric 
57751379Seric 			/* oops -- have to change our mind */
57856764Seric 			anglelev++;
57956764Seric 			if (!skipping)
58056764Seric 				realanglelev++;
58156764Seric 
58256735Seric 			bp = buf;
58356735Seric 			if (quoteit)
58456735Seric 			{
58556735Seric 				*bp++ = '"';
58656735Seric 
58756735Seric 				/* back up over the '<' and any spaces */
58856735Seric 				--p;
58956735Seric 				while (isspace(*--p))
59056735Seric 					continue;
59156735Seric 				p++;
59256735Seric 			}
59356735Seric 			for (q = addr; q < p; )
59456735Seric 			{
59556735Seric 				c = *q++;
59656764Seric 				if (bp < buflim)
59756764Seric 				{
59856764Seric 					if (quoteit && c == '"')
59956764Seric 						*bp++ = '\\';
60056764Seric 					*bp++ = c;
60156764Seric 				}
60256735Seric 			}
60356735Seric 			if (quoteit)
60456735Seric 			{
60556735Seric 				*bp++ = '"';
60656764Seric 				while ((c = *p++) != '<')
60756764Seric 				{
60856764Seric 					if (bp < buflim)
60956764Seric 						*bp++ = c;
61056764Seric 				}
61156764Seric 				*bp++ = c;
61256735Seric 			}
61351379Seric 			copylev = 0;
61456735Seric 			putgmac = quoteit = FALSE;
61551379Seric 			continue;
61651379Seric 		}
6177783Seric 
61851379Seric 		if (c == '>')
6197783Seric 		{
62056764Seric 			if (anglelev > 0)
62156764Seric 			{
62256764Seric 				anglelev--;
62356764Seric 				if (!skipping)
62456764Seric 				{
62556764Seric 					realanglelev--;
62656764Seric 					buflim++;
62756764Seric 				}
62856764Seric 			}
62956764Seric 			else if (!skipping)
63056764Seric 			{
63156764Seric 				/* syntax error: unmatched > */
63256764Seric 				if (copylev > 0)
63356764Seric 					bp--;
63456764Seric 				continue;
63556764Seric 			}
63651379Seric 			if (copylev++ <= 0)
63751379Seric 				*bp++ = c;
63851379Seric 			continue;
6397783Seric 		}
64051379Seric 
64151379Seric 		/* must be a real address character */
64251379Seric 	putg:
64351379Seric 		if (copylev <= 0 && !putgmac)
64451379Seric 		{
64551379Seric 			*bp++ = '\001';
64651379Seric 			*bp++ = 'g';
64751379Seric 			putgmac = TRUE;
64851379Seric 		}
6497783Seric 	}
6507783Seric 
65156764Seric 	/* repair any syntactic damage */
65256764Seric 	if (realqmode)
65356764Seric 		*bp++ = '"';
65456764Seric 	while (realcmtlev-- > 0)
65556764Seric 		*bp++ = ')';
65656764Seric 	while (realanglelev-- > 0)
65756764Seric 		*bp++ = '>';
65851379Seric 	*bp++ = '\0';
6597783Seric 
6607783Seric 	if (tTd(33, 1))
6617944Seric 		printf("crackaddr=>`%s'\n", buf);
6627783Seric 
6637783Seric 	return (buf);
6647783Seric }
6659382Seric /*
6669382Seric **  PUTHEADER -- put the header part of a message from the in-core copy
6679382Seric **
6689382Seric **	Parameters:
6699382Seric **		fp -- file to put it on.
6709382Seric **		m -- mailer to use.
6719382Seric **		e -- envelope to use.
6729382Seric **
6739382Seric **	Returns:
6749382Seric **		none.
6759382Seric **
6769382Seric **	Side Effects:
6779382Seric **		none.
6789382Seric */
6799382Seric 
68010176Seric putheader(fp, m, e)
6819382Seric 	register FILE *fp;
6829382Seric 	register MAILER *m;
6839382Seric 	register ENVELOPE *e;
6849382Seric {
68557135Seric 	char buf[MAX(MAXLINE,BUFSIZ)];
6869382Seric 	register HDR *h;
6879382Seric 	extern char *arpadate();
6889382Seric 	extern char *capitalize();
68957135Seric 	char obuf[MAXLINE];
6909382Seric 
6919382Seric 	for (h = e->e_header; h != NULL; h = h->h_link)
6929382Seric 	{
6939382Seric 		register char *p;
69410689Seric 		extern bool bitintersect();
6959382Seric 
6969382Seric 		if (bitset(H_CHECK|H_ACHECK, h->h_flags) &&
69710689Seric 		    !bitintersect(h->h_mflags, m->m_flags))
6989382Seric 			continue;
6999382Seric 
70011414Seric 		/* handle Resent-... headers specially */
70111414Seric 		if (bitset(H_RESENT, h->h_flags) && !bitset(EF_RESENT, e->e_flags))
70211414Seric 			continue;
70311414Seric 
7049382Seric 		p = h->h_value;
7059382Seric 		if (bitset(H_DEFAULT, h->h_flags))
7069382Seric 		{
7079382Seric 			/* macro expand value if generated internally */
7089382Seric 			expand(p, buf, &buf[sizeof buf], e);
7099382Seric 			p = buf;
7109382Seric 			if (p == NULL || *p == '\0')
7119382Seric 				continue;
7129382Seric 		}
7139382Seric 
7149382Seric 		if (bitset(H_FROM|H_RCPT, h->h_flags))
7159382Seric 		{
7169382Seric 			/* address field */
7179382Seric 			bool oldstyle = bitset(EF_OLDSTYLE, e->e_flags);
7189382Seric 
7199382Seric 			if (bitset(H_FROM, h->h_flags))
7209382Seric 				oldstyle = FALSE;
72155012Seric 			commaize(h, p, fp, oldstyle, m, e);
7229382Seric 		}
7239382Seric 		else
7249382Seric 		{
7259382Seric 			/* vanilla header line */
72612159Seric 			register char *nlp;
72712159Seric 
72812159Seric 			(void) sprintf(obuf, "%s: ", capitalize(h->h_field));
72956795Seric 			while ((nlp = strchr(p, '\n')) != NULL)
73012159Seric 			{
73112159Seric 				*nlp = '\0';
73212159Seric 				(void) strcat(obuf, p);
73312159Seric 				*nlp = '\n';
73412159Seric 				putline(obuf, fp, m);
73512159Seric 				p = ++nlp;
73612161Seric 				obuf[0] = '\0';
73712159Seric 			}
73812159Seric 			(void) strcat(obuf, p);
73910176Seric 			putline(obuf, fp, m);
7409382Seric 		}
7419382Seric 	}
7429382Seric }
7439382Seric /*
7449382Seric **  COMMAIZE -- output a header field, making a comma-translated list.
7459382Seric **
7469382Seric **	Parameters:
7479382Seric **		h -- the header field to output.
7489382Seric **		p -- the value to put in it.
7499382Seric **		fp -- file to put it to.
7509382Seric **		oldstyle -- TRUE if this is an old style header.
7519382Seric **		m -- a pointer to the mailer descriptor.  If NULL,
7529382Seric **			don't transform the name at all.
75355012Seric **		e -- the envelope containing the message.
7549382Seric **
7559382Seric **	Returns:
7569382Seric **		none.
7579382Seric **
7589382Seric **	Side Effects:
7599382Seric **		outputs "p" to file "fp".
7609382Seric */
7619382Seric 
76255012Seric commaize(h, p, fp, oldstyle, m, e)
7639382Seric 	register HDR *h;
7649382Seric 	register char *p;
7659382Seric 	FILE *fp;
7669382Seric 	bool oldstyle;
7679382Seric 	register MAILER *m;
76855012Seric 	register ENVELOPE *e;
7699382Seric {
7709382Seric 	register char *obp;
7719382Seric 	int opos;
7729382Seric 	bool firstone = TRUE;
77311157Seric 	char obuf[MAXLINE + 3];
7749382Seric 
7759382Seric 	/*
7769382Seric 	**  Output the address list translated by the
7779382Seric 	**  mailer and with commas.
7789382Seric 	*/
7799382Seric 
7809382Seric 	if (tTd(14, 2))
7819382Seric 		printf("commaize(%s: %s)\n", h->h_field, p);
7829382Seric 
7839382Seric 	obp = obuf;
7849382Seric 	(void) sprintf(obp, "%s: ", capitalize(h->h_field));
7859382Seric 	opos = strlen(h->h_field) + 2;
7869382Seric 	obp += opos;
7879382Seric 
7889382Seric 	/*
7899382Seric 	**  Run through the list of values.
7909382Seric 	*/
7919382Seric 
7929382Seric 	while (*p != '\0')
7939382Seric 	{
7949382Seric 		register char *name;
79554983Seric 		register int c;
7969382Seric 		char savechar;
7979382Seric 		extern char *remotename();
7989382Seric 		extern char *DelimChar;		/* defined in prescan */
7999382Seric 
8009382Seric 		/*
8019382Seric 		**  Find the end of the name.  New style names
8029382Seric 		**  end with a comma, old style names end with
8039382Seric 		**  a space character.  However, spaces do not
8049382Seric 		**  necessarily delimit an old-style name -- at
8059382Seric 		**  signs mean keep going.
8069382Seric 		*/
8079382Seric 
8089382Seric 		/* find end of name */
8099382Seric 		while (isspace(*p) || *p == ',')
8109382Seric 			p++;
8119382Seric 		name = p;
8129382Seric 		for (;;)
8139382Seric 		{
8149382Seric 			char *oldp;
81516909Seric 			char pvpbuf[PSBUFSIZE];
8169382Seric 			extern bool isatword();
8179382Seric 			extern char **prescan();
8189382Seric 
81916909Seric 			(void) prescan(p, oldstyle ? ' ' : ',', pvpbuf);
8209382Seric 			p = DelimChar;
8219382Seric 
8229382Seric 			/* look to see if we have an at sign */
8239382Seric 			oldp = p;
8249382Seric 			while (*p != '\0' && isspace(*p))
8259382Seric 				p++;
8269382Seric 
8279382Seric 			if (*p != '@' && !isatword(p))
8289382Seric 			{
8299382Seric 				p = oldp;
8309382Seric 				break;
8319382Seric 			}
8329382Seric 			p += *p == '@' ? 1 : 2;
8339382Seric 			while (*p != '\0' && isspace(*p))
8349382Seric 				p++;
8359382Seric 		}
8369382Seric 		/* at the end of one complete name */
8379382Seric 
8389382Seric 		/* strip off trailing white space */
8399382Seric 		while (p >= name && (isspace(*p) || *p == ',' || *p == '\0'))
8409382Seric 			p--;
8419382Seric 		if (++p == name)
8429382Seric 			continue;
8439382Seric 		savechar = *p;
8449382Seric 		*p = '\0';
8459382Seric 
8469382Seric 		/* translate the name to be relative */
84755012Seric 		name = remotename(name, m, bitset(H_FROM, h->h_flags), FALSE, e);
8489382Seric 		if (*name == '\0')
8499382Seric 		{
8509382Seric 			*p = savechar;
8519382Seric 			continue;
8529382Seric 		}
8539382Seric 
8549382Seric 		/* output the name with nice formatting */
85554983Seric 		opos += strlen(name);
8569382Seric 		if (!firstone)
8579382Seric 			opos += 2;
8589382Seric 		if (opos > 78 && !firstone)
8599382Seric 		{
86010178Seric 			(void) strcpy(obp, ",\n");
86110176Seric 			putline(obuf, fp, m);
8629382Seric 			obp = obuf;
8639382Seric 			(void) sprintf(obp, "        ");
86410161Seric 			opos = strlen(obp);
86510161Seric 			obp += opos;
86654983Seric 			opos += strlen(name);
8679382Seric 		}
8689382Seric 		else if (!firstone)
8699382Seric 		{
8709382Seric 			(void) sprintf(obp, ", ");
8719382Seric 			obp += 2;
8729382Seric 		}
8739382Seric 
8749382Seric 		/* strip off quote bits as we output */
87554983Seric 		while ((c = *name++) != '\0' && obp < &obuf[MAXLINE])
8769382Seric 		{
87754983Seric 			if (bitnset(M_7BITS, m->m_flags))
87854983Seric 				c &= 0177;
87954983Seric 			*obp++ = c;
8809382Seric 		}
8819382Seric 		firstone = FALSE;
8829382Seric 		*p = savechar;
8839382Seric 	}
8849382Seric 	(void) strcpy(obp, "\n");
88510176Seric 	putline(obuf, fp, m);
8869382Seric }
8879382Seric /*
8889382Seric **  ISATWORD -- tell if the word we are pointing to is "at".
8899382Seric **
8909382Seric **	Parameters:
8919382Seric **		p -- word to check.
8929382Seric **
8939382Seric **	Returns:
8949382Seric **		TRUE -- if p is the word at.
8959382Seric **		FALSE -- otherwise.
8969382Seric **
8979382Seric **	Side Effects:
8989382Seric **		none.
8999382Seric */
9009382Seric 
9019382Seric bool
9029382Seric isatword(p)
9039382Seric 	register char *p;
9049382Seric {
9059382Seric 	extern char lower();
9069382Seric 
9079382Seric 	if (lower(p[0]) == 'a' && lower(p[1]) == 't' &&
9089382Seric 	    p[2] != '\0' && isspace(p[2]))
9099382Seric 		return (TRUE);
9109382Seric 	return (FALSE);
9119382Seric }
912