122706Sdist /*
234921Sbostic  * Copyright (c) 1983 Eric P. Allman
333729Sbostic  * Copyright (c) 1988 Regents of the University of California.
433729Sbostic  * All rights reserved.
533729Sbostic  *
633729Sbostic  * Redistribution and use in source and binary forms are permitted
734921Sbostic  * provided that the above copyright notice and this paragraph are
834921Sbostic  * duplicated in all such forms and that any documentation,
934921Sbostic  * advertising materials, and other materials related to such
1034921Sbostic  * distribution and use acknowledge that the software was developed
1134921Sbostic  * by the University of California, Berkeley.  The name of the
1234921Sbostic  * University may not be used to endorse or promote products derived
1334921Sbostic  * from this software without specific prior written permission.
1434921Sbostic  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
1534921Sbostic  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
1634921Sbostic  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1733729Sbostic  */
1822706Sdist 
1922706Sdist #ifndef lint
20*36233Skarels static char sccsid[] = "@(#)headers.c	5.12 (Berkeley) 11/17/88";
2133729Sbostic #endif /* not lint */
2222706Sdist 
234091Seric # include <errno.h>
244091Seric # include "sendmail.h"
254091Seric 
264091Seric /*
274091Seric **  CHOMPHEADER -- process and save a header line.
284091Seric **
294091Seric **	Called by collect and by readcf to deal with header lines.
304091Seric **
314091Seric **	Parameters:
324091Seric **		line -- header as a text line.
334091Seric **		def -- if set, this is a default value.
344091Seric **
354091Seric **	Returns:
364091Seric **		flags for this header.
374091Seric **
384091Seric **	Side Effects:
394091Seric **		The header is saved on the header list.
404319Seric **		Contents of 'line' are destroyed.
414091Seric */
424091Seric 
434091Seric chompheader(line, def)
444091Seric 	char *line;
454091Seric 	bool def;
464091Seric {
474091Seric 	register char *p;
484091Seric 	register HDR *h;
494091Seric 	HDR **hp;
504091Seric 	char *fname;
514091Seric 	char *fvalue;
524091Seric 	struct hdrinfo *hi;
539059Seric 	bool cond = FALSE;
5410689Seric 	BITMAP mopts;
557890Seric 	extern char *crackaddr();
564091Seric 
577677Seric # ifdef DEBUG
587677Seric 	if (tTd(31, 6))
597677Seric 		printf("chompheader: %s\n", line);
607677Seric # endif DEBUG
617677Seric 
624627Seric 	/* strip off options */
6310689Seric 	clrbitmap(mopts);
644627Seric 	p = line;
654627Seric 	if (*p == '?')
664627Seric 	{
674627Seric 		/* have some */
684627Seric 		register char *q = index(p + 1, *p);
694627Seric 
704627Seric 		if (q != NULL)
714627Seric 		{
724627Seric 			*q++ = '\0';
7310689Seric 			while (*++p != '\0')
7410689Seric 				setbitn(*p, mopts);
754627Seric 			p = q;
764627Seric 		}
774627Seric 		else
78*36233Skarels 			usrerr("chompheader: syntax error, line \"%s\"", line);
799059Seric 		cond = TRUE;
804627Seric 	}
814627Seric 
824091Seric 	/* find canonical name */
834627Seric 	fname = p;
844627Seric 	p = index(p, ':');
8510118Seric 	if (p == NULL)
8610118Seric 	{
8710118Seric 		syserr("chompheader: syntax error, line \"%s\"", line);
8810118Seric 		return (0);
8910118Seric 	}
904091Seric 	fvalue = &p[1];
914091Seric 	while (isspace(*--p))
924091Seric 		continue;
934091Seric 	*++p = '\0';
944091Seric 	makelower(fname);
954091Seric 
964091Seric 	/* strip field value on front */
974091Seric 	if (*fvalue == ' ')
984091Seric 		fvalue++;
994091Seric 
1004091Seric 	/* see if it is a known type */
1014091Seric 	for (hi = HdrInfo; hi->hi_field != NULL; hi++)
1024091Seric 	{
1034091Seric 		if (strcmp(hi->hi_field, fname) == 0)
1044091Seric 			break;
1054091Seric 	}
1064091Seric 
10711414Seric 	/* see if this is a resent message */
10811930Seric 	if (!def && bitset(H_RESENT, hi->hi_flags))
10911414Seric 		CurEnv->e_flags |= EF_RESENT;
11011414Seric 
1114091Seric 	/* if this means "end of header" quit now */
1124091Seric 	if (bitset(H_EOH, hi->hi_flags))
1134091Seric 		return (hi->hi_flags);
1144091Seric 
11511414Seric 	/* drop explicit From: if same as what we would generate -- for MH */
11614784Seric 	p = "resent-from";
11714784Seric 	if (!bitset(EF_RESENT, CurEnv->e_flags))
11814784Seric 		p += 7;
11914784Seric 	if (!def && !QueueRun && strcmp(fname, p) == 0)
12011414Seric 	{
12124941Seric 		if (CurEnv->e_from.q_paddr != NULL &&
12224941Seric 		    strcmp(fvalue, CurEnv->e_from.q_paddr) == 0)
12311414Seric 			return (hi->hi_flags);
12411414Seric 	}
12511414Seric 
12614784Seric 	/* delete default value for this header */
12714784Seric 	for (hp = &CurEnv->e_header; (h = *hp) != NULL; hp = &h->h_link)
12814784Seric 	{
12914784Seric 		if (strcmp(fname, h->h_field) == 0 &&
13014784Seric 		    bitset(H_DEFAULT, h->h_flags) &&
13114784Seric 		    !bitset(H_FORCE, h->h_flags))
13214784Seric 			h->h_value = NULL;
13314784Seric 	}
13414784Seric 
13513012Seric 	/* create a new node */
13613012Seric 	h = (HDR *) xalloc(sizeof *h);
13713012Seric 	h->h_field = newstr(fname);
13813012Seric 	h->h_value = NULL;
13913012Seric 	h->h_link = NULL;
14023117Seric 	bcopy((char *) mopts, (char *) h->h_mflags, sizeof mopts);
14113012Seric 	*hp = h;
1428066Seric 	h->h_flags = hi->hi_flags;
1434091Seric 	if (def)
1444091Seric 		h->h_flags |= H_DEFAULT;
1459059Seric 	if (cond)
1469059Seric 		h->h_flags |= H_CHECK;
1474091Seric 	if (h->h_value != NULL)
1489351Seric 		free((char *) h->h_value);
1498066Seric 	h->h_value = newstr(fvalue);
1504091Seric 
1515937Seric 	/* hack to see if this is a new format message */
1528095Seric 	if (!def && bitset(H_RCPT|H_FROM, h->h_flags) &&
1535937Seric 	    (index(fvalue, ',') != NULL || index(fvalue, '(') != NULL ||
1548089Seric 	     index(fvalue, '<') != NULL || index(fvalue, ';') != NULL))
1558089Seric 	{
1569342Seric 		CurEnv->e_flags &= ~EF_OLDSTYLE;
1578089Seric 	}
1585937Seric 
1594091Seric 	return (h->h_flags);
1604091Seric }
1614091Seric /*
1626980Seric **  ADDHEADER -- add a header entry to the end of the queue.
1636980Seric **
1646980Seric **	This bypasses the special checking of chompheader.
1656980Seric **
1666980Seric **	Parameters:
1676980Seric **		field -- the name of the header field.
1686980Seric **		value -- the value of the field.  It must be lower-cased.
1696980Seric **		e -- the envelope to add them to.
1706980Seric **
1716980Seric **	Returns:
1726980Seric **		none.
1736980Seric **
1746980Seric **	Side Effects:
1756980Seric **		adds the field on the list of headers for this envelope.
1766980Seric */
1776980Seric 
1786980Seric addheader(field, value, e)
1796980Seric 	char *field;
1806980Seric 	char *value;
1816980Seric 	ENVELOPE *e;
1826980Seric {
1836980Seric 	register HDR *h;
1846980Seric 	register struct hdrinfo *hi;
1856980Seric 	HDR **hp;
1866980Seric 
1876980Seric 	/* find info struct */
1886980Seric 	for (hi = HdrInfo; hi->hi_field != NULL; hi++)
1896980Seric 	{
1906980Seric 		if (strcmp(field, hi->hi_field) == 0)
1916980Seric 			break;
1926980Seric 	}
1936980Seric 
1946980Seric 	/* find current place in list -- keep back pointer? */
1956980Seric 	for (hp = &e->e_header; (h = *hp) != NULL; hp = &h->h_link)
1966980Seric 	{
1976980Seric 		if (strcmp(field, h->h_field) == 0)
1986980Seric 			break;
1996980Seric 	}
2006980Seric 
2016980Seric 	/* allocate space for new header */
2026980Seric 	h = (HDR *) xalloc(sizeof *h);
2036980Seric 	h->h_field = field;
2046980Seric 	h->h_value = newstr(value);
2057368Seric 	h->h_link = *hp;
2066980Seric 	h->h_flags = hi->hi_flags | H_DEFAULT;
20710689Seric 	clrbitmap(h->h_mflags);
2086980Seric 	*hp = h;
2096980Seric }
2106980Seric /*
2114091Seric **  HVALUE -- return value of a header.
2124091Seric **
2134091Seric **	Only "real" fields (i.e., ones that have not been supplied
2144091Seric **	as a default) are used.
2154091Seric **
2164091Seric **	Parameters:
2174091Seric **		field -- the field name.
2184091Seric **
2194091Seric **	Returns:
2204091Seric **		pointer to the value part.
2214091Seric **		NULL if not found.
2224091Seric **
2234091Seric **	Side Effects:
2249382Seric **		none.
2254091Seric */
2264091Seric 
2274091Seric char *
2284091Seric hvalue(field)
2294091Seric 	char *field;
2304091Seric {
2314091Seric 	register HDR *h;
2324091Seric 
2336908Seric 	for (h = CurEnv->e_header; h != NULL; h = h->h_link)
2344091Seric 	{
2354091Seric 		if (!bitset(H_DEFAULT, h->h_flags) && strcmp(h->h_field, field) == 0)
2364091Seric 			return (h->h_value);
2374091Seric 	}
2384091Seric 	return (NULL);
2394091Seric }
2404091Seric /*
2414091Seric **  ISHEADER -- predicate telling if argument is a header.
2424091Seric **
2434319Seric **	A line is a header if it has a single word followed by
2444319Seric **	optional white space followed by a colon.
2454319Seric **
2464091Seric **	Parameters:
2474091Seric **		s -- string to check for possible headerness.
2484091Seric **
2494091Seric **	Returns:
2504091Seric **		TRUE if s is a header.
2514091Seric **		FALSE otherwise.
2524091Seric **
2534091Seric **	Side Effects:
2544091Seric **		none.
2554091Seric */
2564091Seric 
2574091Seric bool
2584091Seric isheader(s)
2594091Seric 	register char *s;
2604091Seric {
2619382Seric 	while (*s > ' ' && *s != ':' && *s != '\0')
2624091Seric 		s++;
2639382Seric 
2649382Seric 	/* following technically violates RFC822 */
2654091Seric 	while (isspace(*s))
2664091Seric 		s++;
2679382Seric 
2684091Seric 	return (*s == ':');
2694091Seric }
2705919Seric /*
2717783Seric **  EATHEADER -- run through the stored header and extract info.
2727783Seric **
2737783Seric **	Parameters:
2749382Seric **		e -- the envelope to process.
2757783Seric **
2767783Seric **	Returns:
2777783Seric **		none.
2787783Seric **
2797783Seric **	Side Effects:
2807783Seric **		Sets a bunch of global variables from information
2819382Seric **			in the collected header.
2829382Seric **		Aborts the message if the hop count is exceeded.
2837783Seric */
2847783Seric 
2859382Seric eatheader(e)
2869382Seric 	register ENVELOPE *e;
2877783Seric {
2887783Seric 	register HDR *h;
2897783Seric 	register char *p;
2909382Seric 	int hopcnt = 0;
2917783Seric 
2929382Seric #ifdef DEBUG
2939382Seric 	if (tTd(32, 1))
2949382Seric 		printf("----- collected header -----\n");
2959382Seric #endif DEBUG
2969382Seric 	for (h = e->e_header; h != NULL; h = h->h_link)
2977783Seric 	{
2989382Seric #ifdef DEBUG
2997783Seric 		extern char *capitalize();
3007783Seric 
3019382Seric 		if (tTd(32, 1))
3027783Seric 			printf("%s: %s\n", capitalize(h->h_field), h->h_value);
3039382Seric #endif DEBUG
30411414Seric 		/* count the number of times it has been processed */
3059382Seric 		if (bitset(H_TRACE, h->h_flags))
3069382Seric 			hopcnt++;
30711414Seric 
30811414Seric 		/* send to this person if we so desire */
30911414Seric 		if (GrabTo && bitset(H_RCPT, h->h_flags) &&
31011414Seric 		    !bitset(H_DEFAULT, h->h_flags) &&
31111414Seric 		    (!bitset(EF_RESENT, CurEnv->e_flags) || bitset(H_RESENT, h->h_flags)))
31211414Seric 		{
31311414Seric 			sendtolist(h->h_value, (ADDRESS *) NULL, &CurEnv->e_sendqueue);
31411414Seric 		}
31511414Seric 
31611414Seric 		/* log the message-id */
31711290Seric #ifdef LOG
31813103Seric 		if (!QueueRun && LogLevel > 8 && h->h_value != NULL &&
31911299Seric 		    strcmp(h->h_field, "message-id") == 0)
32011290Seric 		{
32111290Seric 			char buf[MAXNAME];
32211290Seric 
32311290Seric 			p = h->h_value;
32411290Seric 			if (bitset(H_DEFAULT, h->h_flags))
32511290Seric 			{
32611290Seric 				expand(p, buf, &buf[sizeof buf], e);
32711290Seric 				p = buf;
32811290Seric 			}
32911290Seric 			syslog(LOG_INFO, "%s: message-id=%s", e->e_id, p);
33011290Seric 		}
33111290Seric #endif LOG
3329382Seric 	}
3339382Seric #ifdef DEBUG
3349382Seric 	if (tTd(32, 1))
3357783Seric 		printf("----------------------------\n");
3369382Seric #endif DEBUG
3377783Seric 
3389382Seric 	/* store hop count */
3399382Seric 	if (hopcnt > e->e_hopcount)
3409382Seric 		e->e_hopcount = hopcnt;
3419382Seric 
3427783Seric 	/* message priority */
3439382Seric 	p = hvalue("precedence");
3449382Seric 	if (p != NULL)
3459382Seric 		e->e_class = priencode(p);
3467783Seric 	if (!QueueRun)
34725013Seric 		e->e_msgpriority = e->e_msgsize
34824981Seric 				 - e->e_class * WkClassFact
34924981Seric 				 + e->e_nrcpts * WkRecipFact;
3507783Seric 
3518066Seric 	/* return receipt to */
3528066Seric 	p = hvalue("return-receipt-to");
3537783Seric 	if (p != NULL)
3549382Seric 		e->e_receiptto = p;
3557783Seric 
3568253Seric 	/* errors to */
3578253Seric 	p = hvalue("errors-to");
3588253Seric 	if (p != NULL)
3599620Seric 		sendtolist(p, (ADDRESS *) NULL, &e->e_errorqueue);
3608253Seric 
3617783Seric 	/* from person */
3629285Seric 	if (OpMode == MD_ARPAFTP)
3638066Seric 	{
3648066Seric 		register struct hdrinfo *hi = HdrInfo;
3657783Seric 
3668066Seric 		for (p = NULL; p == NULL && hi->hi_field != NULL; hi++)
3678066Seric 		{
3688066Seric 			if (bitset(H_FROM, hi->hi_flags))
3698066Seric 				p = hvalue(hi->hi_field);
3708066Seric 		}
3718066Seric 		if (p != NULL)
3729382Seric 			setsender(p);
3738066Seric 	}
3748066Seric 
3757783Seric 	/* full name of from person */
3767783Seric 	p = hvalue("full-name");
3777783Seric 	if (p != NULL)
3789382Seric 		define('x', p, e);
3797783Seric 
3807783Seric 	/* date message originated */
3817783Seric 	p = hvalue("posted-date");
3827783Seric 	if (p == NULL)
3837783Seric 		p = hvalue("date");
3847783Seric 	if (p != NULL)
3857783Seric 	{
3869382Seric 		define('a', p, e);
3877783Seric 		/* we don't have a good way to do canonical conversion ....
3889382Seric 		define('d', newstr(arpatounix(p)), e);
3897783Seric 		.... so we will ignore the problem for the time being */
3907783Seric 	}
39111290Seric 
39211290Seric 	/*
39311290Seric 	**  Log collection information.
39411290Seric 	*/
39511290Seric 
39611290Seric # ifdef LOG
39711299Seric 	if (!QueueRun && LogLevel > 1)
39811290Seric 	{
39936230Skarels 		char hbuf[100], *name = hbuf;
40036230Skarels 
40136230Skarels 		if (RealHostName == NULL)
40236230Skarels 			name = "local";
40336230Skarels 		else if (RealHostName[0] == '[')
40436230Skarels 			name = RealHostName;
40536230Skarels 		else
40636230Skarels 			(void)sprintf(hbuf, "%.90s (%s)",
40736230Skarels 			    RealHostName, inet_ntoa(RealHostAddr.sin_addr));
40836230Skarels 		syslog(LOG_INFO,
40936230Skarels 		    "%s: from=%s, size=%ld, class=%d, received from %s\n",
41036230Skarels 		    CurEnv->e_id, CurEnv->e_from.q_paddr, CurEnv->e_msgsize,
41136230Skarels 		    CurEnv->e_class, name);
41211290Seric 	}
41311290Seric # endif LOG
4147783Seric }
4157783Seric /*
4167783Seric **  PRIENCODE -- encode external priority names into internal values.
4177783Seric **
4187783Seric **	Parameters:
4197783Seric **		p -- priority in ascii.
4207783Seric **
4217783Seric **	Returns:
4227783Seric **		priority as a numeric level.
4237783Seric **
4247783Seric **	Side Effects:
4257783Seric **		none.
4267783Seric */
4277783Seric 
4287783Seric priencode(p)
4297783Seric 	char *p;
4307783Seric {
4318253Seric 	register int i;
4327783Seric 
4338253Seric 	for (i = 0; i < NumPriorities; i++)
4347783Seric 	{
43533725Sbostic 		if (!strcasecmp(p, Priorities[i].pri_name))
4368253Seric 			return (Priorities[i].pri_val);
4377783Seric 	}
4388253Seric 
4398253Seric 	/* unknown priority */
4408253Seric 	return (0);
4417783Seric }
4427783Seric /*
4437890Seric **  CRACKADDR -- parse an address and turn it into a macro
4447783Seric **
4457783Seric **	This doesn't actually parse the address -- it just extracts
4467783Seric **	it and replaces it with "$g".  The parse is totally ad hoc
4477783Seric **	and isn't even guaranteed to leave something syntactically
4487783Seric **	identical to what it started with.  However, it does leave
4497783Seric **	something semantically identical.
4507783Seric **
4517783Seric **	The process is kind of strange.  There are a number of
4527783Seric **	interesting cases:
4537783Seric **		1.  comment <address> comment	==> comment <$g> comment
4547783Seric **		2.  address			==> address
4557783Seric **		3.  address (comment)		==> $g (comment)
4567783Seric **		4.  (comment) address		==> (comment) $g
4577783Seric **	And then there are the hard cases....
4587783Seric **		5.  add (comment) ress		==> $g (comment)
4597783Seric **		6.  comment <address (comment)>	==> comment <$g (comment)>
4607783Seric **		7.    .... etc ....
4617783Seric **
4627783Seric **	Parameters:
4637890Seric **		addr -- the address to be cracked.
4647783Seric **
4657783Seric **	Returns:
4667783Seric **		a pointer to the new version.
4677783Seric **
4687783Seric **	Side Effects:
4697890Seric **		none.
4707783Seric **
4717783Seric **	Warning:
4727783Seric **		The return value is saved in local storage and should
4737783Seric **		be copied if it is to be reused.
4747783Seric */
4757783Seric 
4767783Seric char *
4777890Seric crackaddr(addr)
4787890Seric 	register char *addr;
4797783Seric {
4807783Seric 	register char *p;
4817783Seric 	register int i;
4827783Seric 	static char buf[MAXNAME];
4837783Seric 	char *rhs;
4847783Seric 	bool gotaddr;
4857783Seric 	register char *bp;
4867783Seric 
4877783Seric # ifdef DEBUG
4887783Seric 	if (tTd(33, 1))
4897890Seric 		printf("crackaddr(%s)\n", addr);
4907783Seric # endif DEBUG
4917783Seric 
49223099Seric 	(void) strcpy(buf, "");
4937783Seric 	rhs = NULL;
4947783Seric 
4958082Seric 	/* strip leading spaces */
4968082Seric 	while (*addr != '\0' && isspace(*addr))
4978082Seric 		addr++;
4988082Seric 
4997783Seric 	/*
5007783Seric 	**  See if we have anything in angle brackets.  If so, that is
5017783Seric 	**  the address part, and the rest is the comment.
5027783Seric 	*/
5037783Seric 
5047890Seric 	p = index(addr, '<');
5057783Seric 	if (p != NULL)
5067783Seric 	{
5077890Seric 		/* copy the beginning of the addr field to the buffer */
5087783Seric 		*p = '\0';
50923099Seric 		(void) strcpy(buf, addr);
51023099Seric 		(void) strcat(buf, "<");
5118082Seric 		*p++ = '<';
5127783Seric 
5138082Seric 		/* skip spaces */
5148082Seric 		while (isspace(*p))
5158082Seric 			p++;
5168082Seric 
5177783Seric 		/* find the matching right angle bracket */
5188082Seric 		addr = p;
5197783Seric 		for (i = 0; *p != '\0'; p++)
5207783Seric 		{
5217783Seric 			switch (*p)
5227783Seric 			{
5237783Seric 			  case '<':
5247783Seric 				i++;
5257783Seric 				break;
5267783Seric 
5277783Seric 			  case '>':
5287783Seric 				i--;
5297783Seric 				break;
5307783Seric 			}
5317783Seric 			if (i < 0)
5327783Seric 				break;
5337783Seric 		}
5347783Seric 
5357783Seric 		/* p now points to the closing quote (or a null byte) */
5367783Seric 		if (*p != '\0')
5377783Seric 		{
5387783Seric 			/* make rhs point to the extra stuff at the end */
5397783Seric 			rhs = p;
5407783Seric 			*p++ = '\0';
5417783Seric 		}
5427783Seric 	}
5437783Seric 
5447783Seric 	/*
5457944Seric 	**  Now parse the real address part.  "addr" points to the (null
5467783Seric 	**  terminated) version of what we are inerested in; rhs points
5477783Seric 	**  to the extra stuff at the end of the line, if any.
5487783Seric 	*/
5497783Seric 
5507890Seric 	p = addr;
5517783Seric 
5527783Seric 	/* now strip out comments */
5537783Seric 	bp = &buf[strlen(buf)];
5547783Seric 	gotaddr = FALSE;
5557783Seric 	for (; *p != '\0'; p++)
5567783Seric 	{
5577783Seric 		if (*p == '(')
5587783Seric 		{
5597783Seric 			/* copy to matching close paren */
5607783Seric 			*bp++ = *p++;
5617783Seric 			for (i = 0; *p != '\0'; p++)
5627783Seric 			{
5637783Seric 				*bp++ = *p;
5647783Seric 				switch (*p)
5657783Seric 				{
5667783Seric 				  case '(':
5677783Seric 					i++;
5687783Seric 					break;
5697783Seric 
5707783Seric 				  case ')':
5717783Seric 					i--;
5727783Seric 					break;
5737783Seric 				}
5747783Seric 				if (i < 0)
5757783Seric 					break;
5767783Seric 			}
5777783Seric 			continue;
5787783Seric 		}
5797783Seric 
5807783Seric 		/*
5817783Seric 		**  If this is the first "real" character we have seen,
5827783Seric 		**  then we put the "$g" in the buffer now.
5837783Seric 		*/
5847783Seric 
5857783Seric 		if (isspace(*p))
5867783Seric 			*bp++ = *p;
5877783Seric 		else if (!gotaddr)
5887783Seric 		{
58923099Seric 			(void) strcpy(bp, "\001g");
5907783Seric 			bp += 2;
5917783Seric 			gotaddr = TRUE;
5927783Seric 		}
5937783Seric 	}
5947783Seric 
5957944Seric 	/* hack, hack.... strip trailing blanks */
5967944Seric 	do
5977944Seric 	{
5987944Seric 		*bp-- = '\0';
5997944Seric 	} while (isspace(*bp));
6007944Seric 	bp++;
6017783Seric 
6027944Seric 	/* put any right hand side back on */
6037783Seric 	if (rhs != NULL)
6047783Seric 	{
6057783Seric 		*rhs = '>';
60623099Seric 		(void) strcpy(bp, rhs);
6077783Seric 	}
6087783Seric 
6097783Seric # ifdef DEBUG
6107783Seric 	if (tTd(33, 1))
6117944Seric 		printf("crackaddr=>`%s'\n", buf);
6127783Seric # endif DEBUG
6137783Seric 
6147783Seric 	return (buf);
6157783Seric }
6169382Seric /*
6179382Seric **  PUTHEADER -- put the header part of a message from the in-core copy
6189382Seric **
6199382Seric **	Parameters:
6209382Seric **		fp -- file to put it on.
6219382Seric **		m -- mailer to use.
6229382Seric **		e -- envelope to use.
6239382Seric **
6249382Seric **	Returns:
6259382Seric **		none.
6269382Seric **
6279382Seric **	Side Effects:
6289382Seric **		none.
6299382Seric */
6309382Seric 
63110176Seric putheader(fp, m, e)
6329382Seric 	register FILE *fp;
6339382Seric 	register MAILER *m;
6349382Seric 	register ENVELOPE *e;
6359382Seric {
6369382Seric 	char buf[BUFSIZ];
6379382Seric 	register HDR *h;
6389382Seric 	extern char *arpadate();
6399382Seric 	extern char *capitalize();
6409382Seric 	char obuf[MAXLINE];
6419382Seric 
6429382Seric 	for (h = e->e_header; h != NULL; h = h->h_link)
6439382Seric 	{
6449382Seric 		register char *p;
64510689Seric 		extern bool bitintersect();
6469382Seric 
6479382Seric 		if (bitset(H_CHECK|H_ACHECK, h->h_flags) &&
64810689Seric 		    !bitintersect(h->h_mflags, m->m_flags))
6499382Seric 			continue;
6509382Seric 
65111414Seric 		/* handle Resent-... headers specially */
65211414Seric 		if (bitset(H_RESENT, h->h_flags) && !bitset(EF_RESENT, e->e_flags))
65311414Seric 			continue;
65411414Seric 
6559382Seric 		p = h->h_value;
6569382Seric 		if (bitset(H_DEFAULT, h->h_flags))
6579382Seric 		{
6589382Seric 			/* macro expand value if generated internally */
6599382Seric 			expand(p, buf, &buf[sizeof buf], e);
6609382Seric 			p = buf;
6619382Seric 			if (p == NULL || *p == '\0')
6629382Seric 				continue;
6639382Seric 		}
6649382Seric 
6659382Seric 		if (bitset(H_FROM|H_RCPT, h->h_flags))
6669382Seric 		{
6679382Seric 			/* address field */
6689382Seric 			bool oldstyle = bitset(EF_OLDSTYLE, e->e_flags);
6699382Seric 
6709382Seric 			if (bitset(H_FROM, h->h_flags))
6719382Seric 				oldstyle = FALSE;
67210176Seric 			commaize(h, p, fp, oldstyle, m);
6739382Seric 		}
6749382Seric 		else
6759382Seric 		{
6769382Seric 			/* vanilla header line */
67712159Seric 			register char *nlp;
67812159Seric 
67912159Seric 			(void) sprintf(obuf, "%s: ", capitalize(h->h_field));
68012159Seric 			while ((nlp = index(p, '\n')) != NULL)
68112159Seric 			{
68212159Seric 				*nlp = '\0';
68312159Seric 				(void) strcat(obuf, p);
68412159Seric 				*nlp = '\n';
68512159Seric 				putline(obuf, fp, m);
68612159Seric 				p = ++nlp;
68712161Seric 				obuf[0] = '\0';
68812159Seric 			}
68912159Seric 			(void) strcat(obuf, p);
69010176Seric 			putline(obuf, fp, m);
6919382Seric 		}
6929382Seric 	}
6939382Seric }
6949382Seric /*
6959382Seric **  COMMAIZE -- output a header field, making a comma-translated list.
6969382Seric **
6979382Seric **	Parameters:
6989382Seric **		h -- the header field to output.
6999382Seric **		p -- the value to put in it.
7009382Seric **		fp -- file to put it to.
7019382Seric **		oldstyle -- TRUE if this is an old style header.
7029382Seric **		m -- a pointer to the mailer descriptor.  If NULL,
7039382Seric **			don't transform the name at all.
7049382Seric **
7059382Seric **	Returns:
7069382Seric **		none.
7079382Seric **
7089382Seric **	Side Effects:
7099382Seric **		outputs "p" to file "fp".
7109382Seric */
7119382Seric 
71210176Seric commaize(h, p, fp, oldstyle, m)
7139382Seric 	register HDR *h;
7149382Seric 	register char *p;
7159382Seric 	FILE *fp;
7169382Seric 	bool oldstyle;
7179382Seric 	register MAILER *m;
7189382Seric {
7199382Seric 	register char *obp;
7209382Seric 	int opos;
7219382Seric 	bool firstone = TRUE;
72211157Seric 	char obuf[MAXLINE + 3];
7239382Seric 
7249382Seric 	/*
7259382Seric 	**  Output the address list translated by the
7269382Seric 	**  mailer and with commas.
7279382Seric 	*/
7289382Seric 
7299382Seric # ifdef DEBUG
7309382Seric 	if (tTd(14, 2))
7319382Seric 		printf("commaize(%s: %s)\n", h->h_field, p);
7329382Seric # endif DEBUG
7339382Seric 
7349382Seric 	obp = obuf;
7359382Seric 	(void) sprintf(obp, "%s: ", capitalize(h->h_field));
7369382Seric 	opos = strlen(h->h_field) + 2;
7379382Seric 	obp += opos;
7389382Seric 
7399382Seric 	/*
7409382Seric 	**  Run through the list of values.
7419382Seric 	*/
7429382Seric 
7439382Seric 	while (*p != '\0')
7449382Seric 	{
7459382Seric 		register char *name;
7469382Seric 		char savechar;
7479382Seric 		extern char *remotename();
7489382Seric 		extern char *DelimChar;		/* defined in prescan */
7499382Seric 
7509382Seric 		/*
7519382Seric 		**  Find the end of the name.  New style names
7529382Seric 		**  end with a comma, old style names end with
7539382Seric 		**  a space character.  However, spaces do not
7549382Seric 		**  necessarily delimit an old-style name -- at
7559382Seric 		**  signs mean keep going.
7569382Seric 		*/
7579382Seric 
7589382Seric 		/* find end of name */
7599382Seric 		while (isspace(*p) || *p == ',')
7609382Seric 			p++;
7619382Seric 		name = p;
7629382Seric 		for (;;)
7639382Seric 		{
7649382Seric 			char *oldp;
76516909Seric 			char pvpbuf[PSBUFSIZE];
7669382Seric 			extern bool isatword();
7679382Seric 			extern char **prescan();
7689382Seric 
76916909Seric 			(void) prescan(p, oldstyle ? ' ' : ',', pvpbuf);
7709382Seric 			p = DelimChar;
7719382Seric 
7729382Seric 			/* look to see if we have an at sign */
7739382Seric 			oldp = p;
7749382Seric 			while (*p != '\0' && isspace(*p))
7759382Seric 				p++;
7769382Seric 
7779382Seric 			if (*p != '@' && !isatword(p))
7789382Seric 			{
7799382Seric 				p = oldp;
7809382Seric 				break;
7819382Seric 			}
7829382Seric 			p += *p == '@' ? 1 : 2;
7839382Seric 			while (*p != '\0' && isspace(*p))
7849382Seric 				p++;
7859382Seric 		}
7869382Seric 		/* at the end of one complete name */
7879382Seric 
7889382Seric 		/* strip off trailing white space */
7899382Seric 		while (p >= name && (isspace(*p) || *p == ',' || *p == '\0'))
7909382Seric 			p--;
7919382Seric 		if (++p == name)
7929382Seric 			continue;
7939382Seric 		savechar = *p;
7949382Seric 		*p = '\0';
7959382Seric 
7969382Seric 		/* translate the name to be relative */
79710309Seric 		name = remotename(name, m, bitset(H_FROM, h->h_flags), FALSE);
7989382Seric 		if (*name == '\0')
7999382Seric 		{
8009382Seric 			*p = savechar;
8019382Seric 			continue;
8029382Seric 		}
8039382Seric 
8049382Seric 		/* output the name with nice formatting */
8059382Seric 		opos += qstrlen(name);
8069382Seric 		if (!firstone)
8079382Seric 			opos += 2;
8089382Seric 		if (opos > 78 && !firstone)
8099382Seric 		{
81010178Seric 			(void) strcpy(obp, ",\n");
81110176Seric 			putline(obuf, fp, m);
8129382Seric 			obp = obuf;
8139382Seric 			(void) sprintf(obp, "        ");
81410161Seric 			opos = strlen(obp);
81510161Seric 			obp += opos;
81610161Seric 			opos += qstrlen(name);
8179382Seric 		}
8189382Seric 		else if (!firstone)
8199382Seric 		{
8209382Seric 			(void) sprintf(obp, ", ");
8219382Seric 			obp += 2;
8229382Seric 		}
8239382Seric 
8249382Seric 		/* strip off quote bits as we output */
82511157Seric 		while (*name != '\0' && obp < &obuf[MAXLINE])
8269382Seric 		{
8279382Seric 			if (bitset(0200, *name))
8289382Seric 				*obp++ = '\\';
8299382Seric 			*obp++ = *name++ & ~0200;
8309382Seric 		}
8319382Seric 		firstone = FALSE;
8329382Seric 		*p = savechar;
8339382Seric 	}
8349382Seric 	(void) strcpy(obp, "\n");
83510176Seric 	putline(obuf, fp, m);
8369382Seric }
8379382Seric /*
8389382Seric **  ISATWORD -- tell if the word we are pointing to is "at".
8399382Seric **
8409382Seric **	Parameters:
8419382Seric **		p -- word to check.
8429382Seric **
8439382Seric **	Returns:
8449382Seric **		TRUE -- if p is the word at.
8459382Seric **		FALSE -- otherwise.
8469382Seric **
8479382Seric **	Side Effects:
8489382Seric **		none.
8499382Seric */
8509382Seric 
8519382Seric bool
8529382Seric isatword(p)
8539382Seric 	register char *p;
8549382Seric {
8559382Seric 	extern char lower();
8569382Seric 
8579382Seric 	if (lower(p[0]) == 'a' && lower(p[1]) == 't' &&
8589382Seric 	    p[2] != '\0' && isspace(p[2]))
8599382Seric 		return (TRUE);
8609382Seric 	return (FALSE);
8619382Seric }
862