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*65983Seric static char sccsid[] = "@(#)headers.c	8.24 (Berkeley) 02/03/94";
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;
4764351Seric 	char buf[MAXNAME];
484091Seric 
497677Seric 	if (tTd(31, 6))
507677Seric 		printf("chompheader: %s\n", line);
517677Seric 
524627Seric 	/* strip off options */
5310689Seric 	clrbitmap(mopts);
544627Seric 	p = line;
5557405Seric 	if (*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
6858151Seric 			usrerr("553 header syntax error, line \"%s\"", line);
699059Seric 		cond = TRUE;
704627Seric 	}
714627Seric 
724091Seric 	/* find canonical name */
734627Seric 	fname = p;
7464149Seric 	while (isascii(*p) && isgraph(*p) && *p != ':')
7564149Seric 		p++;
7664149Seric 	fvalue = p;
7764149Seric 	while (isascii(*p) && isspace(*p))
7864149Seric 		p++;
7964150Seric 	if (*p++ != ':' || fname == fvalue)
8010118Seric 	{
8158151Seric 		syserr("553 header syntax error, line \"%s\"", line);
8210118Seric 		return (0);
8310118Seric 	}
8464149Seric 	*fvalue = '\0';
8564149Seric 	fvalue = p;
864091Seric 
874091Seric 	/* strip field value on front */
884091Seric 	if (*fvalue == ' ')
894091Seric 		fvalue++;
904091Seric 
914091Seric 	/* see if it is a known type */
924091Seric 	for (hi = HdrInfo; hi->hi_field != NULL; hi++)
934091Seric 	{
9459579Seric 		if (strcasecmp(hi->hi_field, fname) == 0)
954091Seric 			break;
964091Seric 	}
974091Seric 
9864283Seric 	if (tTd(31, 9))
9964283Seric 	{
10064283Seric 		if (hi->hi_field == NULL)
10164283Seric 			printf("no header match\n");
10264283Seric 		else
10364283Seric 			printf("header match, hi_flags=%o\n", hi->hi_flags);
10464283Seric 	}
10564283Seric 
10611414Seric 	/* see if this is a resent message */
10711930Seric 	if (!def && bitset(H_RESENT, hi->hi_flags))
10855012Seric 		e->e_flags |= EF_RESENT;
10911414Seric 
1104091Seric 	/* if this means "end of header" quit now */
1114091Seric 	if (bitset(H_EOH, hi->hi_flags))
1124091Seric 		return (hi->hi_flags);
1134091Seric 
11464653Seric 	/*
11564653Seric 	**  Drop explicit From: if same as what we would generate.
11664653Seric 	**  This is to make MH (which doesn't always give a full name)
11764653Seric 	**  insert the full name information in all circumstances.
11864653Seric 	*/
11964653Seric 
12014784Seric 	p = "resent-from";
12155012Seric 	if (!bitset(EF_RESENT, e->e_flags))
12214784Seric 		p += 7;
12359579Seric 	if (!def && !bitset(EF_QUEUERUN, e->e_flags) && strcasecmp(fname, p) == 0)
12411414Seric 	{
12563753Seric 		if (tTd(31, 2))
12663753Seric 		{
12763753Seric 			printf("comparing header from (%s) against default (%s or %s)\n",
12863753Seric 				fvalue, e->e_from.q_paddr, e->e_from.q_user);
12963753Seric 		}
13055012Seric 		if (e->e_from.q_paddr != NULL &&
13163753Seric 		    (strcmp(fvalue, e->e_from.q_paddr) == 0 ||
13263753Seric 		     strcmp(fvalue, e->e_from.q_user) == 0))
13311414Seric 			return (hi->hi_flags);
13464351Seric #ifdef MAYBENEXTRELEASE		/* XXX UNTESTED XXX UNTESTED XXX UNTESTED XXX */
13564351Seric #ifdef USERDB
13664351Seric 		else
13764351Seric 		{
13864351Seric 			auto ADDRESS a;
13964351Seric 			char *fancy;
14064351Seric 			extern char *crackaddr();
14164351Seric 			extern char *udbsender();
14264351Seric 
14364653Seric 			/*
14464653Seric 			**  Try doing USERDB rewriting even on fully commented
14564653Seric 			**  names; this saves the "comment" information (such
14664653Seric 			**  as full name) and rewrites the electronic part.
14764653Seric 			*/
14864653Seric 
14964351Seric 			fancy = crackaddr(fvalue);
15064351Seric 			if (parseaddr(fvalue, &a, RF_COPYNONE, '\0', NULL, e) != NULL &&
15164351Seric 			    a.q_mailer == LocalMailer &&
15264351Seric 			    (p = udbsender(a.q_user)) != NULL)
15364351Seric 			{
15464351Seric 				char *oldg = macvalue('g', e);
15564351Seric 
15664351Seric 				define('g', p, e);
15764351Seric 				expand(fancy, buf, &buf[sizeof buf], e);
15864351Seric 				define('g', oldg, e);
15964351Seric 				fvalue = buf;
16064351Seric 			}
16164351Seric 		}
16264351Seric #endif
16364351Seric #endif
16411414Seric 	}
16511414Seric 
16614784Seric 	/* delete default value for this header */
16755012Seric 	for (hp = &e->e_header; (h = *hp) != NULL; hp = &h->h_link)
16814784Seric 	{
16959579Seric 		if (strcasecmp(fname, h->h_field) == 0 &&
17014784Seric 		    bitset(H_DEFAULT, h->h_flags) &&
17114784Seric 		    !bitset(H_FORCE, h->h_flags))
17214784Seric 			h->h_value = NULL;
17314784Seric 	}
17414784Seric 
17513012Seric 	/* create a new node */
17613012Seric 	h = (HDR *) xalloc(sizeof *h);
17713012Seric 	h->h_field = newstr(fname);
17864134Seric 	h->h_value = newstr(fvalue);
17913012Seric 	h->h_link = NULL;
18023117Seric 	bcopy((char *) mopts, (char *) h->h_mflags, sizeof mopts);
18113012Seric 	*hp = h;
1828066Seric 	h->h_flags = hi->hi_flags;
1834091Seric 	if (def)
1844091Seric 		h->h_flags |= H_DEFAULT;
1859059Seric 	if (cond)
1869059Seric 		h->h_flags |= H_CHECK;
1874091Seric 
1885937Seric 	/* hack to see if this is a new format message */
1898095Seric 	if (!def && bitset(H_RCPT|H_FROM, h->h_flags) &&
19056795Seric 	    (strchr(fvalue, ',') != NULL || strchr(fvalue, '(') != NULL ||
19156795Seric 	     strchr(fvalue, '<') != NULL || strchr(fvalue, ';') != NULL))
1928089Seric 	{
19355012Seric 		e->e_flags &= ~EF_OLDSTYLE;
1948089Seric 	}
1955937Seric 
1964091Seric 	return (h->h_flags);
1974091Seric }
1984091Seric /*
1996980Seric **  ADDHEADER -- add a header entry to the end of the queue.
2006980Seric **
2016980Seric **	This bypasses the special checking of chompheader.
2026980Seric **
2036980Seric **	Parameters:
20459579Seric **		field -- the name of the header field.
20558789Seric **		value -- the value of the field.
2066980Seric **		e -- the envelope to add them to.
2076980Seric **
2086980Seric **	Returns:
2096980Seric **		none.
2106980Seric **
2116980Seric **	Side Effects:
2126980Seric **		adds the field on the list of headers for this envelope.
2136980Seric */
2146980Seric 
2156980Seric addheader(field, value, e)
2166980Seric 	char *field;
2176980Seric 	char *value;
2186980Seric 	ENVELOPE *e;
2196980Seric {
2206980Seric 	register HDR *h;
2216980Seric 	register struct hdrinfo *hi;
2226980Seric 	HDR **hp;
2236980Seric 
2246980Seric 	/* find info struct */
2256980Seric 	for (hi = HdrInfo; hi->hi_field != NULL; hi++)
2266980Seric 	{
22759579Seric 		if (strcasecmp(field, hi->hi_field) == 0)
2286980Seric 			break;
2296980Seric 	}
2306980Seric 
2316980Seric 	/* find current place in list -- keep back pointer? */
2326980Seric 	for (hp = &e->e_header; (h = *hp) != NULL; hp = &h->h_link)
2336980Seric 	{
23459579Seric 		if (strcasecmp(field, h->h_field) == 0)
2356980Seric 			break;
2366980Seric 	}
2376980Seric 
2386980Seric 	/* allocate space for new header */
2396980Seric 	h = (HDR *) xalloc(sizeof *h);
2406980Seric 	h->h_field = field;
2416980Seric 	h->h_value = newstr(value);
2427368Seric 	h->h_link = *hp;
2436980Seric 	h->h_flags = hi->hi_flags | H_DEFAULT;
24410689Seric 	clrbitmap(h->h_mflags);
2456980Seric 	*hp = h;
2466980Seric }
2476980Seric /*
2484091Seric **  HVALUE -- return value of a header.
2494091Seric **
2504091Seric **	Only "real" fields (i.e., ones that have not been supplied
2514091Seric **	as a default) are used.
2524091Seric **
2534091Seric **	Parameters:
2544091Seric **		field -- the field name.
25555012Seric **		e -- the envelope containing the header.
2564091Seric **
2574091Seric **	Returns:
2584091Seric **		pointer to the value part.
2594091Seric **		NULL if not found.
2604091Seric **
2614091Seric **	Side Effects:
2629382Seric **		none.
2634091Seric */
2644091Seric 
2654091Seric char *
26655012Seric hvalue(field, e)
2674091Seric 	char *field;
26855012Seric 	register ENVELOPE *e;
2694091Seric {
2704091Seric 	register HDR *h;
2714091Seric 
27255012Seric 	for (h = e->e_header; h != NULL; h = h->h_link)
2734091Seric 	{
27459579Seric 		if (!bitset(H_DEFAULT, h->h_flags) &&
27559579Seric 		    strcasecmp(h->h_field, field) == 0)
2764091Seric 			return (h->h_value);
2774091Seric 	}
2784091Seric 	return (NULL);
2794091Seric }
2804091Seric /*
2814091Seric **  ISHEADER -- predicate telling if argument is a header.
2824091Seric **
2834319Seric **	A line is a header if it has a single word followed by
2844319Seric **	optional white space followed by a colon.
2854319Seric **
2864091Seric **	Parameters:
2874091Seric **		s -- string to check for possible headerness.
2884091Seric **
2894091Seric **	Returns:
2904091Seric **		TRUE if s is a header.
2914091Seric **		FALSE otherwise.
2924091Seric **
2934091Seric **	Side Effects:
2944091Seric **		none.
2954091Seric */
2964091Seric 
2974091Seric bool
2984091Seric isheader(s)
2994091Seric 	register char *s;
3004091Seric {
3019382Seric 	while (*s > ' ' && *s != ':' && *s != '\0')
3024091Seric 		s++;
3039382Seric 
3049382Seric 	/* following technically violates RFC822 */
30558050Seric 	while (isascii(*s) && isspace(*s))
3064091Seric 		s++;
3079382Seric 
3084091Seric 	return (*s == ':');
3094091Seric }
3105919Seric /*
3117783Seric **  EATHEADER -- run through the stored header and extract info.
3127783Seric **
3137783Seric **	Parameters:
3149382Seric **		e -- the envelope to process.
31558929Seric **		full -- if set, do full processing (e.g., compute
31658929Seric **			message priority).
3177783Seric **
3187783Seric **	Returns:
3197783Seric **		none.
3207783Seric **
3217783Seric **	Side Effects:
3227783Seric **		Sets a bunch of global variables from information
3239382Seric **			in the collected header.
3249382Seric **		Aborts the message if the hop count is exceeded.
3257783Seric */
3267783Seric 
32758929Seric eatheader(e, full)
3289382Seric 	register ENVELOPE *e;
32958929Seric 	bool full;
3307783Seric {
3317783Seric 	register HDR *h;
3327783Seric 	register char *p;
3339382Seric 	int hopcnt = 0;
33457208Seric 	char *msgid;
33558688Seric 	char buf[MAXLINE];
3367783Seric 
33758688Seric 	/*
33858688Seric 	**  Set up macros for possible expansion in headers.
33958688Seric 	*/
34058688Seric 
34158704Seric 	define('f', e->e_sender, e);
34258704Seric 	define('g', e->e_sender, e);
34364148Seric 	if (e->e_origrcpt != NULL && *e->e_origrcpt != '\0')
34464148Seric 		define('u', e->e_origrcpt, e);
34564148Seric 	else
34664148Seric 		define('u', NULL, e);
34758688Seric 
34865052Seric 	/* full name of from person */
34965052Seric 	p = hvalue("full-name", e);
35065052Seric 	if (p != NULL)
35165052Seric 		define('x', p, e);
35265052Seric 
3539382Seric 	if (tTd(32, 1))
3549382Seric 		printf("----- collected header -----\n");
35557208Seric 	msgid = "<none>";
3569382Seric 	for (h = e->e_header; h != NULL; h = h->h_link)
3577783Seric 	{
35864156Seric 		if (h->h_value == NULL)
35964156Seric 		{
36064156Seric 			if (tTd(32, 1))
36164156Seric 				printf("%s: <NULL>\n", h->h_field);
36264156Seric 			continue;
36364156Seric 		}
36464156Seric 
36558688Seric 		/* do early binding */
36664156Seric 		if (bitset(H_DEFAULT, h->h_flags))
36758688Seric 		{
36858688Seric 			expand(h->h_value, buf, &buf[sizeof buf], e);
36958688Seric 			if (buf[0] != '\0')
37058688Seric 			{
37158688Seric 				h->h_value = newstr(buf);
37258688Seric 				h->h_flags &= ~H_DEFAULT;
37358688Seric 			}
37458688Seric 		}
37558688Seric 
3769382Seric 		if (tTd(32, 1))
37765152Seric 		{
37865152Seric 			printf("%s: ", h->h_field);
37965152Seric 			xputs(h->h_value);
38065152Seric 			printf("\n");
38165152Seric 		}
38257359Seric 
38311414Seric 		/* count the number of times it has been processed */
3849382Seric 		if (bitset(H_TRACE, h->h_flags))
3859382Seric 			hopcnt++;
38611414Seric 
38711414Seric 		/* send to this person if we so desire */
38811414Seric 		if (GrabTo && bitset(H_RCPT, h->h_flags) &&
38911414Seric 		    !bitset(H_DEFAULT, h->h_flags) &&
39055012Seric 		    (!bitset(EF_RESENT, e->e_flags) || bitset(H_RESENT, h->h_flags)))
39111414Seric 		{
39263839Seric 			int saveflags = e->e_flags;
39363839Seric 
39464283Seric 			(void) sendtolist(h->h_value, NULLADDR,
39558082Seric 					  &e->e_sendqueue, e);
39663839Seric 
39763839Seric 			/* delete fatal errors generated by this address */
39864148Seric 			if (!GrabTo && !bitset(EF_FATALERRS, saveflags))
39963839Seric 				e->e_flags &= ~EF_FATALERRS;
40011414Seric 		}
40111414Seric 
40257208Seric 		/* save the message-id for logging */
40364156Seric 		if (full && strcasecmp(h->h_field, "message-id") == 0)
40411290Seric 		{
40557208Seric 			msgid = h->h_value;
40659859Seric 			while (isascii(*msgid) && isspace(*msgid))
40759859Seric 				msgid++;
40811290Seric 		}
40957359Seric 
41057359Seric 		/* see if this is a return-receipt header */
41157359Seric 		if (bitset(H_RECEIPTTO, h->h_flags))
41257359Seric 			e->e_receiptto = h->h_value;
41357359Seric 
41457359Seric 		/* see if this is an errors-to header */
41561104Seric 		if (UseErrorsTo && bitset(H_ERRORSTO, h->h_flags))
41664283Seric 			(void) sendtolist(h->h_value, NULLADDR,
41758082Seric 					  &e->e_errorqueue, e);
4189382Seric 	}
4199382Seric 	if (tTd(32, 1))
4207783Seric 		printf("----------------------------\n");
4217783Seric 
42258145Seric 	/* if we are just verifying (that is, sendmail -t -bv), drop out now */
42358145Seric 	if (OpMode == MD_VERIFY)
42458145Seric 		return;
42558145Seric 
4269382Seric 	/* store hop count */
4279382Seric 	if (hopcnt > e->e_hopcount)
4289382Seric 		e->e_hopcount = hopcnt;
4299382Seric 
4307783Seric 	/* message priority */
43155012Seric 	p = hvalue("precedence", e);
4329382Seric 	if (p != NULL)
4339382Seric 		e->e_class = priencode(p);
43458929Seric 	if (full)
43525013Seric 		e->e_msgpriority = e->e_msgsize
43624981Seric 				 - e->e_class * WkClassFact
43724981Seric 				 + e->e_nrcpts * WkRecipFact;
4387783Seric 
4397783Seric 	/* date message originated */
44055012Seric 	p = hvalue("posted-date", e);
4417783Seric 	if (p == NULL)
44255012Seric 		p = hvalue("date", e);
4437783Seric 	if (p != NULL)
4449382Seric 		define('a', p, e);
44511290Seric 
44611290Seric 	/*
447*65983Seric 	**  From person in antiquated ARPANET mode
448*65983Seric 	**	required by UK Grey Book e-mail gateways (sigh)
449*65983Seric 	*/
450*65983Seric 
451*65983Seric 	if (OpMode == MD_ARPAFTP)
452*65983Seric 	{
453*65983Seric 		register struct hdrinfo *hi;
454*65983Seric 
455*65983Seric 		for (hi = HdrInfo; hi->hi_field != NULL; hi++)
456*65983Seric 		{
457*65983Seric 			if (bitset(H_FROM, hi->hi_flags) &&
458*65983Seric 			    (!bitset(H_RESENT, hi->hi_flags) ||
459*65983Seric 			     bitset(EF_RESENT, e->e_flags)) &&
460*65983Seric 			    (p = hvalue(hi->hi_field, e)) != NULL)
461*65983Seric 				break;
462*65983Seric 		}
463*65983Seric 		if (hi->hi_field != NULL)
464*65983Seric 		{
465*65983Seric 			if (tTd(32, 2))
466*65983Seric 				printf("eatheader: setsender(*%s == %s)\n",
467*65983Seric 					hi->hi_field, p);
468*65983Seric 			setsender(p, e, NULL, TRUE);
469*65983Seric 		}
470*65983Seric 	}
471*65983Seric 
472*65983Seric 	/*
47311290Seric 	**  Log collection information.
47411290Seric 	*/
47511290Seric 
47611290Seric # ifdef LOG
47758929Seric 	if (full && LogLevel > 4)
47865089Seric 		logsender(e, msgid);
47965089Seric # endif /* LOG */
48065089Seric 	e->e_flags &= ~EF_LOGSENDER;
48165089Seric }
48265089Seric /*
48365089Seric **  LOGSENDER -- log sender information
48465089Seric **
48565089Seric **	Parameters:
48665089Seric **		e -- the envelope to log
48765089Seric **		msgid -- the message id
48865089Seric **
48965089Seric **	Returns:
49065089Seric **		none
49165089Seric */
49265089Seric 
49365089Seric logsender(e, msgid)
49465089Seric 	register ENVELOPE *e;
49565089Seric 	char *msgid;
49665089Seric {
49765089Seric 	char *name;
49865089Seric 	register char *sbp;
49965089Seric 	register char *p;
50065089Seric 	char hbuf[MAXNAME];
50165089Seric 	char sbuf[MAXLINE];
50265089Seric 
50365089Seric 	if (bitset(EF_RESPONSE, e->e_flags))
50465089Seric 		name = "[RESPONSE]";
50565089Seric 	else if ((name = macvalue('_', e)) != NULL)
50665089Seric 		;
50765089Seric 	else if (RealHostName[0] == '[')
50865089Seric 		name = RealHostName;
50965089Seric 	else
51011290Seric 	{
51165089Seric 		name = hbuf;
51265089Seric 		(void) sprintf(hbuf, "%.80s", RealHostName);
51365089Seric 		if (RealHostAddr.sa.sa_family != 0)
51457359Seric 		{
51565089Seric 			p = &hbuf[strlen(hbuf)];
51665089Seric 			(void) sprintf(p, " (%s)",
51765089Seric 				anynet_ntoa(&RealHostAddr));
51857359Seric 		}
51965089Seric 	}
52057359Seric 
52165089Seric 	/* some versions of syslog only take 5 printf args */
52265059Seric #  if (SYSLOG_BUFSIZE) >= 256
52365089Seric 	sbp = sbuf;
52465089Seric 	sprintf(sbp, "from=%.200s, size=%ld, class=%d, pri=%ld, nrcpts=%d",
52565089Seric 	    e->e_from.q_paddr, e->e_msgsize, e->e_class,
52665089Seric 	    e->e_msgpriority, e->e_nrcpts);
52765089Seric 	sbp += strlen(sbp);
52865089Seric 	if (msgid != NULL)
52965089Seric 	{
53065089Seric 		sprintf(sbp, ", msgid=%.100s", msgid);
53160575Seric 		sbp += strlen(sbp);
53265089Seric 	}
53365089Seric 	if (e->e_bodytype != NULL)
53465089Seric 	{
53565089Seric 		(void) sprintf(sbp, ", bodytype=%.20s", e->e_bodytype);
53665089Seric 		sbp += strlen(sbp);
53765089Seric 	}
53865089Seric 	p = macvalue('r', e);
53965089Seric 	if (p != NULL)
54065089Seric 		(void) sprintf(sbp, ", proto=%.20s", p);
54165089Seric 	syslog(LOG_INFO, "%s: %s, relay=%s",
54265089Seric 	    e->e_id, sbuf, name);
54365059Seric 
54465059Seric #  else			/* short syslog buffer */
54565059Seric 
54665089Seric 	syslog(LOG_INFO, "%s: from=%s",
54765089Seric 		e->e_id, shortenstring(e->e_from.q_paddr, 83));
54865089Seric 	syslog(LOG_INFO, "%s: size=%ld, class=%ld, pri=%ld, nrcpts=%d",
54965089Seric 		e->e_id, e->e_msgsize, e->e_class,
55065089Seric 		e->e_msgpriority, e->e_nrcpts);
55165089Seric 	if (msgid != NULL)
55265059Seric 		syslog(LOG_INFO, "%s: msgid=%s", e->e_id, msgid);
55365650Seric 	sbp = sbuf;
55465650Seric 	sprintf(sbp, "%s:", e->e_id);
55565650Seric 	sbp += strlen(sbp);
55665089Seric 	if (e->e_bodytype != NULL)
55765650Seric 	{
55865650Seric 		sprintf(sbp, " bodytype=%s,", e->e_bodytype);
55965650Seric 		sbp += strlen(sbp);
56065650Seric 	}
56165089Seric 	p = macvalue('r', e);
56265089Seric 	if (p != NULL)
56365650Seric 	{
56465731Seric 		sprintf(sbp, " proto=%s,", p);
56565650Seric 		sbp += strlen(sbp);
56665650Seric 	}
56765650Seric 	syslog(LOG_INFO, "%s relay=%s", sbuf, name);
56865059Seric #  endif
5697783Seric }
5707783Seric /*
5717783Seric **  PRIENCODE -- encode external priority names into internal values.
5727783Seric **
5737783Seric **	Parameters:
5747783Seric **		p -- priority in ascii.
5757783Seric **
5767783Seric **	Returns:
5777783Seric **		priority as a numeric level.
5787783Seric **
5797783Seric **	Side Effects:
5807783Seric **		none.
5817783Seric */
5827783Seric 
5837783Seric priencode(p)
5847783Seric 	char *p;
5857783Seric {
5868253Seric 	register int i;
5877783Seric 
5888253Seric 	for (i = 0; i < NumPriorities; i++)
5897783Seric 	{
59033725Sbostic 		if (!strcasecmp(p, Priorities[i].pri_name))
5918253Seric 			return (Priorities[i].pri_val);
5927783Seric 	}
5938253Seric 
5948253Seric 	/* unknown priority */
5958253Seric 	return (0);
5967783Seric }
5977783Seric /*
5987890Seric **  CRACKADDR -- parse an address and turn it into a macro
5997783Seric **
6007783Seric **	This doesn't actually parse the address -- it just extracts
6017783Seric **	it and replaces it with "$g".  The parse is totally ad hoc
6027783Seric **	and isn't even guaranteed to leave something syntactically
6037783Seric **	identical to what it started with.  However, it does leave
6047783Seric **	something semantically identical.
6057783Seric **
60651379Seric **	This algorithm has been cleaned up to handle a wider range
60751379Seric **	of cases -- notably quoted and backslash escaped strings.
60851379Seric **	This modification makes it substantially better at preserving
60951379Seric **	the original syntax.
6107783Seric **
6117783Seric **	Parameters:
6127890Seric **		addr -- the address to be cracked.
6137783Seric **
6147783Seric **	Returns:
6157783Seric **		a pointer to the new version.
6167783Seric **
6177783Seric **	Side Effects:
6187890Seric **		none.
6197783Seric **
6207783Seric **	Warning:
6217783Seric **		The return value is saved in local storage and should
6227783Seric **		be copied if it is to be reused.
6237783Seric */
6247783Seric 
6257783Seric char *
6267890Seric crackaddr(addr)
6277890Seric 	register char *addr;
6287783Seric {
6297783Seric 	register char *p;
63051379Seric 	register char c;
63151379Seric 	int cmtlev;
63256764Seric 	int realcmtlev;
63356764Seric 	int anglelev, realanglelev;
63451379Seric 	int copylev;
63551379Seric 	bool qmode;
63656764Seric 	bool realqmode;
63756764Seric 	bool skipping;
63851379Seric 	bool putgmac = FALSE;
63956735Seric 	bool quoteit = FALSE;
64064148Seric 	bool gotangle = FALSE;
64151379Seric 	register char *bp;
64256764Seric 	char *buflim;
6437783Seric 	static char buf[MAXNAME];
6447783Seric 
6457783Seric 	if (tTd(33, 1))
6467890Seric 		printf("crackaddr(%s)\n", addr);
6477783Seric 
6488082Seric 	/* strip leading spaces */
64958050Seric 	while (*addr != '\0' && isascii(*addr) && isspace(*addr))
6508082Seric 		addr++;
6518082Seric 
6527783Seric 	/*
65351379Seric 	**  Start by assuming we have no angle brackets.  This will be
65451379Seric 	**  adjusted later if we find them.
6557783Seric 	*/
6567783Seric 
65751379Seric 	bp = buf;
65856764Seric 	buflim = &buf[sizeof buf - 5];
65951379Seric 	p = addr;
66056764Seric 	copylev = anglelev = realanglelev = cmtlev = realcmtlev = 0;
66156764Seric 	qmode = realqmode = FALSE;
66251379Seric 
66351379Seric 	while ((c = *p++) != '\0')
6647783Seric 	{
66556764Seric 		/*
66656764Seric 		**  If the buffer is overful, go into a special "skipping"
66756764Seric 		**  mode that tries to keep legal syntax but doesn't actually
66856764Seric 		**  output things.
66956764Seric 		*/
6707783Seric 
67156764Seric 		skipping = bp >= buflim;
67256735Seric 
67356764Seric 		if (copylev > 0 && !skipping)
67456764Seric 			*bp++ = c;
67556735Seric 
67651379Seric 		/* check for backslash escapes */
67751379Seric 		if (c == '\\')
6787783Seric 		{
67958890Seric 			/* arrange to quote the address */
68058890Seric 			if (cmtlev <= 0 && !qmode)
68158890Seric 				quoteit = TRUE;
68258890Seric 
68351379Seric 			if ((c = *p++) == '\0')
6847783Seric 			{
68551379Seric 				/* too far */
68651379Seric 				p--;
68751379Seric 				goto putg;
6887783Seric 			}
68956764Seric 			if (copylev > 0 && !skipping)
69051379Seric 				*bp++ = c;
69151379Seric 			goto putg;
6927783Seric 		}
6937783Seric 
69451379Seric 		/* check for quoted strings */
69564967Seric 		if (c == '"' && cmtlev <= 0)
6967783Seric 		{
69751379Seric 			qmode = !qmode;
69856764Seric 			if (copylev > 0 && !skipping)
69956764Seric 				realqmode = !realqmode;
70051379Seric 			continue;
7017783Seric 		}
70251379Seric 		if (qmode)
70351379Seric 			goto putg;
7047783Seric 
70551379Seric 		/* check for comments */
70651379Seric 		if (c == '(')
7077783Seric 		{
70851379Seric 			cmtlev++;
70956764Seric 
71056764Seric 			/* allow space for closing paren */
71156764Seric 			if (!skipping)
71256764Seric 			{
71356764Seric 				buflim--;
71456764Seric 				realcmtlev++;
71556764Seric 				if (copylev++ <= 0)
71656764Seric 				{
71756764Seric 					*bp++ = ' ';
71856764Seric 					*bp++ = c;
71956764Seric 				}
72056764Seric 			}
72151379Seric 		}
72251379Seric 		if (cmtlev > 0)
72351379Seric 		{
72451379Seric 			if (c == ')')
7257783Seric 			{
72651379Seric 				cmtlev--;
72751379Seric 				copylev--;
72856764Seric 				if (!skipping)
72956764Seric 				{
73056764Seric 					realcmtlev--;
73156764Seric 					buflim++;
73256764Seric 				}
7337783Seric 			}
7347783Seric 			continue;
7357783Seric 		}
73656764Seric 		else if (c == ')')
73756764Seric 		{
73856764Seric 			/* syntax error: unmatched ) */
73965544Seric 			if (copylev > 0 && !skipping)
74056764Seric 				bp--;
74156764Seric 		}
7427783Seric 
74356764Seric 		/* check for characters that may have to be quoted */
74464148Seric 		if (strchr(".'@,;:\\()[]", c) != NULL)
74556764Seric 		{
74656764Seric 			/*
74756764Seric 			**  If these occur as the phrase part of a <>
74856764Seric 			**  construct, but are not inside of () or already
74956764Seric 			**  quoted, they will have to be quoted.  Note that
75056764Seric 			**  now (but don't actually do the quoting).
75156764Seric 			*/
75256764Seric 
75356764Seric 			if (cmtlev <= 0 && !qmode)
75456764Seric 				quoteit = TRUE;
75556764Seric 		}
75656764Seric 
75751379Seric 		/* check for angle brackets */
75851379Seric 		if (c == '<')
75951379Seric 		{
76056735Seric 			register char *q;
76156735Seric 
76264148Seric 			/* assume first of two angles is bogus */
76364148Seric 			if (gotangle)
76464148Seric 				quoteit = TRUE;
76564148Seric 			gotangle = TRUE;
76664148Seric 
76751379Seric 			/* oops -- have to change our mind */
76864752Seric 			anglelev = 1;
76956764Seric 			if (!skipping)
77064752Seric 				realanglelev = 1;
77156764Seric 
77256735Seric 			bp = buf;
77356735Seric 			if (quoteit)
77456735Seric 			{
77556735Seric 				*bp++ = '"';
77656735Seric 
77756735Seric 				/* back up over the '<' and any spaces */
77856735Seric 				--p;
77958050Seric 				while (isascii(*--p) && isspace(*p))
78056735Seric 					continue;
78156735Seric 				p++;
78256735Seric 			}
78356735Seric 			for (q = addr; q < p; )
78456735Seric 			{
78556735Seric 				c = *q++;
78656764Seric 				if (bp < buflim)
78756764Seric 				{
78856764Seric 					if (quoteit && c == '"')
78956764Seric 						*bp++ = '\\';
79056764Seric 					*bp++ = c;
79156764Seric 				}
79256735Seric 			}
79356735Seric 			if (quoteit)
79456735Seric 			{
79564148Seric 				if (bp == &buf[1])
79664148Seric 					bp--;
79764148Seric 				else
79864148Seric 					*bp++ = '"';
79956764Seric 				while ((c = *p++) != '<')
80056764Seric 				{
80156764Seric 					if (bp < buflim)
80256764Seric 						*bp++ = c;
80356764Seric 				}
80456764Seric 				*bp++ = c;
80556735Seric 			}
80651379Seric 			copylev = 0;
80756735Seric 			putgmac = quoteit = FALSE;
80851379Seric 			continue;
80951379Seric 		}
8107783Seric 
81151379Seric 		if (c == '>')
8127783Seric 		{
81356764Seric 			if (anglelev > 0)
81456764Seric 			{
81556764Seric 				anglelev--;
81656764Seric 				if (!skipping)
81756764Seric 				{
81856764Seric 					realanglelev--;
81956764Seric 					buflim++;
82056764Seric 				}
82156764Seric 			}
82256764Seric 			else if (!skipping)
82356764Seric 			{
82456764Seric 				/* syntax error: unmatched > */
82556764Seric 				if (copylev > 0)
82656764Seric 					bp--;
82764752Seric 				quoteit = TRUE;
82856764Seric 				continue;
82956764Seric 			}
83051379Seric 			if (copylev++ <= 0)
83151379Seric 				*bp++ = c;
83251379Seric 			continue;
8337783Seric 		}
83451379Seric 
83551379Seric 		/* must be a real address character */
83651379Seric 	putg:
83751379Seric 		if (copylev <= 0 && !putgmac)
83851379Seric 		{
83958050Seric 			*bp++ = MACROEXPAND;
84051379Seric 			*bp++ = 'g';
84151379Seric 			putgmac = TRUE;
84251379Seric 		}
8437783Seric 	}
8447783Seric 
84556764Seric 	/* repair any syntactic damage */
84656764Seric 	if (realqmode)
84756764Seric 		*bp++ = '"';
84856764Seric 	while (realcmtlev-- > 0)
84956764Seric 		*bp++ = ')';
85056764Seric 	while (realanglelev-- > 0)
85156764Seric 		*bp++ = '>';
85251379Seric 	*bp++ = '\0';
8537783Seric 
8547783Seric 	if (tTd(33, 1))
8557944Seric 		printf("crackaddr=>`%s'\n", buf);
8567783Seric 
8577783Seric 	return (buf);
8587783Seric }
8599382Seric /*
8609382Seric **  PUTHEADER -- put the header part of a message from the in-core copy
8619382Seric **
8629382Seric **	Parameters:
86365870Seric **		mci -- the connection information.
8649382Seric **		e -- envelope to use.
8659382Seric **
8669382Seric **	Returns:
8679382Seric **		none.
8689382Seric **
8699382Seric **	Side Effects:
8709382Seric **		none.
8719382Seric */
8729382Seric 
87357589Seric /*
87457589Seric  * Macro for fast max (not available in e.g. DG/UX, 386/ix).
87557589Seric  */
87657589Seric #ifndef MAX
87757589Seric # define MAX(a,b) (((a)>(b))?(a):(b))
87857589Seric #endif
87957589Seric 
88065870Seric putheader(mci, e)
88165870Seric 	register MCI *mci;
8829382Seric 	register ENVELOPE *e;
8839382Seric {
88457135Seric 	char buf[MAX(MAXLINE,BUFSIZ)];
8859382Seric 	register HDR *h;
88657135Seric 	char obuf[MAXLINE];
8879382Seric 
88859882Seric 	if (tTd(34, 1))
88965870Seric 		printf("--- putheader, mailer = %s ---\n",
89065870Seric 			mci->mci_mailer->m_name);
89159882Seric 
8929382Seric 	for (h = e->e_header; h != NULL; h = h->h_link)
8939382Seric 	{
8949382Seric 		register char *p;
89510689Seric 		extern bool bitintersect();
8969382Seric 
89759882Seric 		if (tTd(34, 11))
89859882Seric 		{
89959882Seric 			printf("  %s: ", h->h_field);
90059882Seric 			xputs(h->h_value);
90159882Seric 		}
90259882Seric 
9039382Seric 		if (bitset(H_CHECK|H_ACHECK, h->h_flags) &&
90465870Seric 		    !bitintersect(h->h_mflags, mci->mci_mailer->m_flags))
90559882Seric 		{
90659882Seric 			if (tTd(34, 11))
90759882Seric 				printf(" (skipped)\n");
9089382Seric 			continue;
90959882Seric 		}
9109382Seric 
91111414Seric 		/* handle Resent-... headers specially */
91211414Seric 		if (bitset(H_RESENT, h->h_flags) && !bitset(EF_RESENT, e->e_flags))
91359882Seric 		{
91459882Seric 			if (tTd(34, 11))
91559882Seric 				printf(" (skipped (resent))\n");
91611414Seric 			continue;
91759882Seric 		}
91811414Seric 
91965152Seric 		/* macro expand value if generated internally */
9209382Seric 		p = h->h_value;
9219382Seric 		if (bitset(H_DEFAULT, h->h_flags))
9229382Seric 		{
9239382Seric 			expand(p, buf, &buf[sizeof buf], e);
9249382Seric 			p = buf;
9259382Seric 			if (p == NULL || *p == '\0')
92665152Seric 			{
92765152Seric 				if (tTd(34, 11))
92865152Seric 					printf(" (skipped -- null value)\n");
9299382Seric 				continue;
93065152Seric 			}
9319382Seric 		}
9329382Seric 
93365152Seric 		if (tTd(34, 11))
93465152Seric 			printf("\n");
93565152Seric 
9369382Seric 		if (bitset(H_FROM|H_RCPT, h->h_flags))
9379382Seric 		{
9389382Seric 			/* address field */
9399382Seric 			bool oldstyle = bitset(EF_OLDSTYLE, e->e_flags);
9409382Seric 
9419382Seric 			if (bitset(H_FROM, h->h_flags))
9429382Seric 				oldstyle = FALSE;
94365870Seric 			commaize(h, p, oldstyle, mci, e);
9449382Seric 		}
9459382Seric 		else
9469382Seric 		{
9479382Seric 			/* vanilla header line */
94812159Seric 			register char *nlp;
94912159Seric 
95059579Seric 			(void) sprintf(obuf, "%s: ", h->h_field);
95156795Seric 			while ((nlp = strchr(p, '\n')) != NULL)
95212159Seric 			{
95312159Seric 				*nlp = '\0';
95412159Seric 				(void) strcat(obuf, p);
95512159Seric 				*nlp = '\n';
95665870Seric 				putline(obuf, mci);
95712159Seric 				p = ++nlp;
95812161Seric 				obuf[0] = '\0';
95912159Seric 			}
96012159Seric 			(void) strcat(obuf, p);
96165870Seric 			putline(obuf, mci);
9629382Seric 		}
9639382Seric 	}
9649382Seric }
9659382Seric /*
9669382Seric **  COMMAIZE -- output a header field, making a comma-translated list.
9679382Seric **
9689382Seric **	Parameters:
9699382Seric **		h -- the header field to output.
9709382Seric **		p -- the value to put in it.
9719382Seric **		oldstyle -- TRUE if this is an old style header.
97265870Seric **		mci -- the connection information.
97355012Seric **		e -- the envelope containing the message.
9749382Seric **
9759382Seric **	Returns:
9769382Seric **		none.
9779382Seric **
9789382Seric **	Side Effects:
9799382Seric **		outputs "p" to file "fp".
9809382Seric */
9819382Seric 
98265870Seric void
98365870Seric commaize(h, p, oldstyle, mci, e)
9849382Seric 	register HDR *h;
9859382Seric 	register char *p;
9869382Seric 	bool oldstyle;
98765870Seric 	register MCI *mci;
98855012Seric 	register ENVELOPE *e;
9899382Seric {
9909382Seric 	register char *obp;
9919382Seric 	int opos;
9929382Seric 	bool firstone = TRUE;
99311157Seric 	char obuf[MAXLINE + 3];
9949382Seric 
9959382Seric 	/*
9969382Seric 	**  Output the address list translated by the
9979382Seric 	**  mailer and with commas.
9989382Seric 	*/
9999382Seric 
10009382Seric 	if (tTd(14, 2))
10019382Seric 		printf("commaize(%s: %s)\n", h->h_field, p);
10029382Seric 
10039382Seric 	obp = obuf;
100459579Seric 	(void) sprintf(obp, "%s: ", h->h_field);
10059382Seric 	opos = strlen(h->h_field) + 2;
10069382Seric 	obp += opos;
10079382Seric 
10089382Seric 	/*
10099382Seric 	**  Run through the list of values.
10109382Seric 	*/
10119382Seric 
10129382Seric 	while (*p != '\0')
10139382Seric 	{
10149382Seric 		register char *name;
101554983Seric 		register int c;
10169382Seric 		char savechar;
101759163Seric 		int flags;
101859163Seric 		auto int stat;
10199382Seric 
10209382Seric 		/*
10219382Seric 		**  Find the end of the name.  New style names
10229382Seric 		**  end with a comma, old style names end with
10239382Seric 		**  a space character.  However, spaces do not
10249382Seric 		**  necessarily delimit an old-style name -- at
10259382Seric 		**  signs mean keep going.
10269382Seric 		*/
10279382Seric 
10289382Seric 		/* find end of name */
102958050Seric 		while ((isascii(*p) && isspace(*p)) || *p == ',')
10309382Seric 			p++;
10319382Seric 		name = p;
10329382Seric 		for (;;)
10339382Seric 		{
103458333Seric 			auto char *oldp;
103516909Seric 			char pvpbuf[PSBUFSIZE];
10369382Seric 
103765066Seric 			(void) prescan(p, oldstyle ? ' ' : ',', pvpbuf,
103865066Seric 				       sizeof pvpbuf, &oldp);
103958333Seric 			p = oldp;
10409382Seric 
10419382Seric 			/* look to see if we have an at sign */
104258050Seric 			while (*p != '\0' && isascii(*p) && isspace(*p))
10439382Seric 				p++;
10449382Seric 
104558170Seric 			if (*p != '@')
10469382Seric 			{
10479382Seric 				p = oldp;
10489382Seric 				break;
10499382Seric 			}
10509382Seric 			p += *p == '@' ? 1 : 2;
105158050Seric 			while (*p != '\0' && isascii(*p) && isspace(*p))
10529382Seric 				p++;
10539382Seric 		}
10549382Seric 		/* at the end of one complete name */
10559382Seric 
10569382Seric 		/* strip off trailing white space */
105758050Seric 		while (p >= name &&
105858050Seric 		       ((isascii(*p) && isspace(*p)) || *p == ',' || *p == '\0'))
10599382Seric 			p--;
10609382Seric 		if (++p == name)
10619382Seric 			continue;
10629382Seric 		savechar = *p;
10639382Seric 		*p = '\0';
10649382Seric 
10659382Seric 		/* translate the name to be relative */
106659163Seric 		flags = RF_HEADERADDR|RF_ADDDOMAIN;
106759163Seric 		if (bitset(H_FROM, h->h_flags))
106859163Seric 			flags |= RF_SENDERADDR;
106959163Seric 		stat = EX_OK;
107065870Seric 		name = remotename(name, mci->mci_mailer, flags, &stat, e);
10719382Seric 		if (*name == '\0')
10729382Seric 		{
10739382Seric 			*p = savechar;
10749382Seric 			continue;
10759382Seric 		}
10769382Seric 
10779382Seric 		/* output the name with nice formatting */
107854983Seric 		opos += strlen(name);
10799382Seric 		if (!firstone)
10809382Seric 			opos += 2;
10819382Seric 		if (opos > 78 && !firstone)
10829382Seric 		{
108310178Seric 			(void) strcpy(obp, ",\n");
108465870Seric 			putline(obuf, mci);
10859382Seric 			obp = obuf;
10869382Seric 			(void) sprintf(obp, "        ");
108710161Seric 			opos = strlen(obp);
108810161Seric 			obp += opos;
108954983Seric 			opos += strlen(name);
10909382Seric 		}
10919382Seric 		else if (!firstone)
10929382Seric 		{
10939382Seric 			(void) sprintf(obp, ", ");
10949382Seric 			obp += 2;
10959382Seric 		}
10969382Seric 
109754983Seric 		while ((c = *name++) != '\0' && obp < &obuf[MAXLINE])
109854983Seric 			*obp++ = c;
10999382Seric 		firstone = FALSE;
11009382Seric 		*p = savechar;
11019382Seric 	}
11029382Seric 	(void) strcpy(obp, "\n");
110365870Seric 	putline(obuf, mci);
11049382Seric }
11059382Seric /*
110658170Seric **  COPYHEADER -- copy header list
11079382Seric **
110858170Seric **	This routine is the equivalent of newstr for header lists
110958170Seric **
11109382Seric **	Parameters:
111158170Seric **		header -- list of header structures to copy.
11129382Seric **
11139382Seric **	Returns:
111458170Seric **		a copy of 'header'.
11159382Seric **
11169382Seric **	Side Effects:
11179382Seric **		none.
11189382Seric */
11199382Seric 
112058170Seric HDR *
112158170Seric copyheader(header)
112258170Seric 	register HDR *header;
11239382Seric {
112458170Seric 	register HDR *newhdr;
112558170Seric 	HDR *ret;
112658170Seric 	register HDR **tail = &ret;
11279382Seric 
112858170Seric 	while (header != NULL)
112958170Seric 	{
113058170Seric 		newhdr = (HDR *) xalloc(sizeof(HDR));
113158170Seric 		STRUCTCOPY(*header, *newhdr);
113258170Seric 		*tail = newhdr;
113358170Seric 		tail = &newhdr->h_link;
113458170Seric 		header = header->h_link;
113558170Seric 	}
113658170Seric 	*tail = NULL;
113758170Seric 
113858170Seric 	return ret;
11399382Seric }
1140