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*66255Seric static char sccsid[] = "@(#)headers.c	8.30 (Berkeley) 02/25/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;
14066123Seric 			bool oldSuprErrs = SuprErrs;
14164351Seric 			extern char *crackaddr();
14264351Seric 			extern char *udbsender();
14364351Seric 
14464653Seric 			/*
14564653Seric 			**  Try doing USERDB rewriting even on fully commented
14664653Seric 			**  names; this saves the "comment" information (such
14764653Seric 			**  as full name) and rewrites the electronic part.
14866106Seric 			**
14966106Seric 			** XXX	This code doesn't belong here -- parsing should
15066106Seric 			** XXX	not be done during collect() phase because
15166106Seric 			** XXX	error messages can confuse the SMTP phase.
15266123Seric 			** XXX	Setting SuprErrs is a crude hack around this
15366106Seric 			** XXX	problem.
15464653Seric 			*/
15564653Seric 
15666106Seric 			if (OpMode == MD_SMTP || OpMode == MD_ARPAFTP)
15766123Seric 				SuprErrs = TRUE;
15864351Seric 			fancy = crackaddr(fvalue);
15964351Seric 			if (parseaddr(fvalue, &a, RF_COPYNONE, '\0', NULL, e) != NULL &&
16064351Seric 			    a.q_mailer == LocalMailer &&
16164351Seric 			    (p = udbsender(a.q_user)) != NULL)
16264351Seric 			{
16364351Seric 				char *oldg = macvalue('g', e);
16464351Seric 
16564351Seric 				define('g', p, e);
16664351Seric 				expand(fancy, buf, &buf[sizeof buf], e);
16764351Seric 				define('g', oldg, e);
16864351Seric 				fvalue = buf;
16964351Seric 			}
17066123Seric 			SuprErrs = oldSuprErrs;
17164351Seric 		}
17264351Seric #endif
17364351Seric #endif
17411414Seric 	}
17511414Seric 
17614784Seric 	/* delete default value for this header */
17755012Seric 	for (hp = &e->e_header; (h = *hp) != NULL; hp = &h->h_link)
17814784Seric 	{
17959579Seric 		if (strcasecmp(fname, h->h_field) == 0 &&
18014784Seric 		    bitset(H_DEFAULT, h->h_flags) &&
18114784Seric 		    !bitset(H_FORCE, h->h_flags))
18214784Seric 			h->h_value = NULL;
18314784Seric 	}
18414784Seric 
18513012Seric 	/* create a new node */
18613012Seric 	h = (HDR *) xalloc(sizeof *h);
18713012Seric 	h->h_field = newstr(fname);
18864134Seric 	h->h_value = newstr(fvalue);
18913012Seric 	h->h_link = NULL;
19023117Seric 	bcopy((char *) mopts, (char *) h->h_mflags, sizeof mopts);
19113012Seric 	*hp = h;
1928066Seric 	h->h_flags = hi->hi_flags;
1934091Seric 	if (def)
1944091Seric 		h->h_flags |= H_DEFAULT;
1959059Seric 	if (cond)
1969059Seric 		h->h_flags |= H_CHECK;
1974091Seric 
1985937Seric 	/* hack to see if this is a new format message */
1998095Seric 	if (!def && bitset(H_RCPT|H_FROM, h->h_flags) &&
20056795Seric 	    (strchr(fvalue, ',') != NULL || strchr(fvalue, '(') != NULL ||
20156795Seric 	     strchr(fvalue, '<') != NULL || strchr(fvalue, ';') != NULL))
2028089Seric 	{
20355012Seric 		e->e_flags &= ~EF_OLDSTYLE;
2048089Seric 	}
2055937Seric 
2064091Seric 	return (h->h_flags);
2074091Seric }
2084091Seric /*
2096980Seric **  ADDHEADER -- add a header entry to the end of the queue.
2106980Seric **
2116980Seric **	This bypasses the special checking of chompheader.
2126980Seric **
2136980Seric **	Parameters:
21459579Seric **		field -- the name of the header field.
21558789Seric **		value -- the value of the field.
2166980Seric **		e -- the envelope to add them to.
2176980Seric **
2186980Seric **	Returns:
2196980Seric **		none.
2206980Seric **
2216980Seric **	Side Effects:
2226980Seric **		adds the field on the list of headers for this envelope.
2236980Seric */
2246980Seric 
2256980Seric addheader(field, value, e)
2266980Seric 	char *field;
2276980Seric 	char *value;
2286980Seric 	ENVELOPE *e;
2296980Seric {
2306980Seric 	register HDR *h;
2316980Seric 	register struct hdrinfo *hi;
2326980Seric 	HDR **hp;
2336980Seric 
2346980Seric 	/* find info struct */
2356980Seric 	for (hi = HdrInfo; hi->hi_field != NULL; hi++)
2366980Seric 	{
23759579Seric 		if (strcasecmp(field, hi->hi_field) == 0)
2386980Seric 			break;
2396980Seric 	}
2406980Seric 
2416980Seric 	/* find current place in list -- keep back pointer? */
2426980Seric 	for (hp = &e->e_header; (h = *hp) != NULL; hp = &h->h_link)
2436980Seric 	{
24459579Seric 		if (strcasecmp(field, h->h_field) == 0)
2456980Seric 			break;
2466980Seric 	}
2476980Seric 
2486980Seric 	/* allocate space for new header */
2496980Seric 	h = (HDR *) xalloc(sizeof *h);
2506980Seric 	h->h_field = field;
2516980Seric 	h->h_value = newstr(value);
2527368Seric 	h->h_link = *hp;
2536980Seric 	h->h_flags = hi->hi_flags | H_DEFAULT;
25410689Seric 	clrbitmap(h->h_mflags);
2556980Seric 	*hp = h;
2566980Seric }
2576980Seric /*
2584091Seric **  HVALUE -- return value of a header.
2594091Seric **
2604091Seric **	Only "real" fields (i.e., ones that have not been supplied
2614091Seric **	as a default) are used.
2624091Seric **
2634091Seric **	Parameters:
2644091Seric **		field -- the field name.
26555012Seric **		e -- the envelope containing the header.
2664091Seric **
2674091Seric **	Returns:
2684091Seric **		pointer to the value part.
2694091Seric **		NULL if not found.
2704091Seric **
2714091Seric **	Side Effects:
2729382Seric **		none.
2734091Seric */
2744091Seric 
2754091Seric char *
27655012Seric hvalue(field, e)
2774091Seric 	char *field;
27855012Seric 	register ENVELOPE *e;
2794091Seric {
2804091Seric 	register HDR *h;
2814091Seric 
28255012Seric 	for (h = e->e_header; h != NULL; h = h->h_link)
2834091Seric 	{
28459579Seric 		if (!bitset(H_DEFAULT, h->h_flags) &&
28559579Seric 		    strcasecmp(h->h_field, field) == 0)
2864091Seric 			return (h->h_value);
2874091Seric 	}
2884091Seric 	return (NULL);
2894091Seric }
2904091Seric /*
2914091Seric **  ISHEADER -- predicate telling if argument is a header.
2924091Seric **
2934319Seric **	A line is a header if it has a single word followed by
2944319Seric **	optional white space followed by a colon.
2954319Seric **
2964091Seric **	Parameters:
2974091Seric **		s -- string to check for possible headerness.
2984091Seric **
2994091Seric **	Returns:
3004091Seric **		TRUE if s is a header.
3014091Seric **		FALSE otherwise.
3024091Seric **
3034091Seric **	Side Effects:
3044091Seric **		none.
3054091Seric */
3064091Seric 
3074091Seric bool
3084091Seric isheader(s)
3094091Seric 	register char *s;
3104091Seric {
3119382Seric 	while (*s > ' ' && *s != ':' && *s != '\0')
3124091Seric 		s++;
3139382Seric 
3149382Seric 	/* following technically violates RFC822 */
31558050Seric 	while (isascii(*s) && isspace(*s))
3164091Seric 		s++;
3179382Seric 
3184091Seric 	return (*s == ':');
3194091Seric }
3205919Seric /*
3217783Seric **  EATHEADER -- run through the stored header and extract info.
3227783Seric **
3237783Seric **	Parameters:
3249382Seric **		e -- the envelope to process.
32558929Seric **		full -- if set, do full processing (e.g., compute
32658929Seric **			message priority).
3277783Seric **
3287783Seric **	Returns:
3297783Seric **		none.
3307783Seric **
3317783Seric **	Side Effects:
3327783Seric **		Sets a bunch of global variables from information
3339382Seric **			in the collected header.
3349382Seric **		Aborts the message if the hop count is exceeded.
3357783Seric */
3367783Seric 
33758929Seric eatheader(e, full)
3389382Seric 	register ENVELOPE *e;
33958929Seric 	bool full;
3407783Seric {
3417783Seric 	register HDR *h;
3427783Seric 	register char *p;
3439382Seric 	int hopcnt = 0;
34457208Seric 	char *msgid;
34558688Seric 	char buf[MAXLINE];
3467783Seric 
34758688Seric 	/*
34858688Seric 	**  Set up macros for possible expansion in headers.
34958688Seric 	*/
35058688Seric 
35158704Seric 	define('f', e->e_sender, e);
35258704Seric 	define('g', e->e_sender, e);
35364148Seric 	if (e->e_origrcpt != NULL && *e->e_origrcpt != '\0')
35464148Seric 		define('u', e->e_origrcpt, e);
35564148Seric 	else
35664148Seric 		define('u', NULL, e);
35758688Seric 
35865052Seric 	/* full name of from person */
35965052Seric 	p = hvalue("full-name", e);
36065052Seric 	if (p != NULL)
36165052Seric 		define('x', p, e);
36265052Seric 
3639382Seric 	if (tTd(32, 1))
3649382Seric 		printf("----- collected header -----\n");
36557208Seric 	msgid = "<none>";
3669382Seric 	for (h = e->e_header; h != NULL; h = h->h_link)
3677783Seric 	{
36864156Seric 		if (h->h_value == NULL)
36964156Seric 		{
37064156Seric 			if (tTd(32, 1))
37164156Seric 				printf("%s: <NULL>\n", h->h_field);
37264156Seric 			continue;
37364156Seric 		}
37464156Seric 
37558688Seric 		/* do early binding */
37664156Seric 		if (bitset(H_DEFAULT, h->h_flags))
37758688Seric 		{
37858688Seric 			expand(h->h_value, buf, &buf[sizeof buf], e);
37958688Seric 			if (buf[0] != '\0')
38058688Seric 			{
38158688Seric 				h->h_value = newstr(buf);
38258688Seric 				h->h_flags &= ~H_DEFAULT;
38358688Seric 			}
38458688Seric 		}
38558688Seric 
3869382Seric 		if (tTd(32, 1))
38765152Seric 		{
38865152Seric 			printf("%s: ", h->h_field);
38965152Seric 			xputs(h->h_value);
39065152Seric 			printf("\n");
39165152Seric 		}
39257359Seric 
39311414Seric 		/* count the number of times it has been processed */
3949382Seric 		if (bitset(H_TRACE, h->h_flags))
3959382Seric 			hopcnt++;
39611414Seric 
39711414Seric 		/* send to this person if we so desire */
39811414Seric 		if (GrabTo && bitset(H_RCPT, h->h_flags) &&
39911414Seric 		    !bitset(H_DEFAULT, h->h_flags) &&
40055012Seric 		    (!bitset(EF_RESENT, e->e_flags) || bitset(H_RESENT, h->h_flags)))
40111414Seric 		{
40263839Seric 			int saveflags = e->e_flags;
40363839Seric 
40464283Seric 			(void) sendtolist(h->h_value, NULLADDR,
40558082Seric 					  &e->e_sendqueue, e);
40663839Seric 
40763839Seric 			/* delete fatal errors generated by this address */
40864148Seric 			if (!GrabTo && !bitset(EF_FATALERRS, saveflags))
40963839Seric 				e->e_flags &= ~EF_FATALERRS;
41011414Seric 		}
41111414Seric 
41257208Seric 		/* save the message-id for logging */
41364156Seric 		if (full && strcasecmp(h->h_field, "message-id") == 0)
41411290Seric 		{
41557208Seric 			msgid = h->h_value;
41659859Seric 			while (isascii(*msgid) && isspace(*msgid))
41759859Seric 				msgid++;
41811290Seric 		}
41957359Seric 
42057359Seric 		/* see if this is a return-receipt header */
42157359Seric 		if (bitset(H_RECEIPTTO, h->h_flags))
42257359Seric 			e->e_receiptto = h->h_value;
42357359Seric 
42457359Seric 		/* see if this is an errors-to header */
42561104Seric 		if (UseErrorsTo && bitset(H_ERRORSTO, h->h_flags))
42664283Seric 			(void) sendtolist(h->h_value, NULLADDR,
42758082Seric 					  &e->e_errorqueue, e);
4289382Seric 	}
4299382Seric 	if (tTd(32, 1))
4307783Seric 		printf("----------------------------\n");
4317783Seric 
43258145Seric 	/* if we are just verifying (that is, sendmail -t -bv), drop out now */
43358145Seric 	if (OpMode == MD_VERIFY)
43458145Seric 		return;
43558145Seric 
4369382Seric 	/* store hop count */
4379382Seric 	if (hopcnt > e->e_hopcount)
4389382Seric 		e->e_hopcount = hopcnt;
4399382Seric 
4407783Seric 	/* message priority */
44155012Seric 	p = hvalue("precedence", e);
4429382Seric 	if (p != NULL)
4439382Seric 		e->e_class = priencode(p);
44458929Seric 	if (full)
44525013Seric 		e->e_msgpriority = e->e_msgsize
44624981Seric 				 - e->e_class * WkClassFact
44724981Seric 				 + e->e_nrcpts * WkRecipFact;
4487783Seric 
4497783Seric 	/* date message originated */
45055012Seric 	p = hvalue("posted-date", e);
4517783Seric 	if (p == NULL)
45255012Seric 		p = hvalue("date", e);
4537783Seric 	if (p != NULL)
4549382Seric 		define('a', p, e);
45511290Seric 
45611290Seric 	/*
45765983Seric 	**  From person in antiquated ARPANET mode
45865983Seric 	**	required by UK Grey Book e-mail gateways (sigh)
45965983Seric 	*/
46065983Seric 
46165983Seric 	if (OpMode == MD_ARPAFTP)
46265983Seric 	{
46365983Seric 		register struct hdrinfo *hi;
46465983Seric 
46565983Seric 		for (hi = HdrInfo; hi->hi_field != NULL; hi++)
46665983Seric 		{
46765983Seric 			if (bitset(H_FROM, hi->hi_flags) &&
46865983Seric 			    (!bitset(H_RESENT, hi->hi_flags) ||
46965983Seric 			     bitset(EF_RESENT, e->e_flags)) &&
47065983Seric 			    (p = hvalue(hi->hi_field, e)) != NULL)
47165983Seric 				break;
47265983Seric 		}
47365983Seric 		if (hi->hi_field != NULL)
47465983Seric 		{
47565983Seric 			if (tTd(32, 2))
47665983Seric 				printf("eatheader: setsender(*%s == %s)\n",
47765983Seric 					hi->hi_field, p);
47865983Seric 			setsender(p, e, NULL, TRUE);
47965983Seric 		}
48065983Seric 	}
48165983Seric 
48265983Seric 	/*
48311290Seric 	**  Log collection information.
48411290Seric 	*/
48511290Seric 
48611290Seric # ifdef LOG
48758929Seric 	if (full && LogLevel > 4)
48865089Seric 		logsender(e, msgid);
48965089Seric # endif /* LOG */
49065089Seric 	e->e_flags &= ~EF_LOGSENDER;
49165089Seric }
49265089Seric /*
49365089Seric **  LOGSENDER -- log sender information
49465089Seric **
49565089Seric **	Parameters:
49665089Seric **		e -- the envelope to log
49765089Seric **		msgid -- the message id
49865089Seric **
49965089Seric **	Returns:
50065089Seric **		none
50165089Seric */
50265089Seric 
50365089Seric logsender(e, msgid)
50465089Seric 	register ENVELOPE *e;
50565089Seric 	char *msgid;
50665089Seric {
50765089Seric 	char *name;
50865089Seric 	register char *sbp;
50965089Seric 	register char *p;
51065089Seric 	char hbuf[MAXNAME];
51165089Seric 	char sbuf[MAXLINE];
51265089Seric 
51365089Seric 	if (bitset(EF_RESPONSE, e->e_flags))
51465089Seric 		name = "[RESPONSE]";
51565089Seric 	else if ((name = macvalue('_', e)) != NULL)
51665089Seric 		;
51766003Seric 	else if (RealHostName == NULL)
51866003Seric 		name = "localhost";
51965089Seric 	else if (RealHostName[0] == '[')
52065089Seric 		name = RealHostName;
52165089Seric 	else
52211290Seric 	{
52365089Seric 		name = hbuf;
52465089Seric 		(void) sprintf(hbuf, "%.80s", RealHostName);
52565089Seric 		if (RealHostAddr.sa.sa_family != 0)
52657359Seric 		{
52765089Seric 			p = &hbuf[strlen(hbuf)];
52865089Seric 			(void) sprintf(p, " (%s)",
52965089Seric 				anynet_ntoa(&RealHostAddr));
53057359Seric 		}
53165089Seric 	}
53257359Seric 
53365089Seric 	/* some versions of syslog only take 5 printf args */
53465059Seric #  if (SYSLOG_BUFSIZE) >= 256
53565089Seric 	sbp = sbuf;
53665089Seric 	sprintf(sbp, "from=%.200s, size=%ld, class=%d, pri=%ld, nrcpts=%d",
53765089Seric 	    e->e_from.q_paddr, e->e_msgsize, e->e_class,
53865089Seric 	    e->e_msgpriority, e->e_nrcpts);
53965089Seric 	sbp += strlen(sbp);
54065089Seric 	if (msgid != NULL)
54165089Seric 	{
54265089Seric 		sprintf(sbp, ", msgid=%.100s", msgid);
54360575Seric 		sbp += strlen(sbp);
54465089Seric 	}
54565089Seric 	if (e->e_bodytype != NULL)
54665089Seric 	{
54765089Seric 		(void) sprintf(sbp, ", bodytype=%.20s", e->e_bodytype);
54865089Seric 		sbp += strlen(sbp);
54965089Seric 	}
55065089Seric 	p = macvalue('r', e);
55165089Seric 	if (p != NULL)
55265089Seric 		(void) sprintf(sbp, ", proto=%.20s", p);
55365089Seric 	syslog(LOG_INFO, "%s: %s, relay=%s",
55465089Seric 	    e->e_id, sbuf, name);
55565059Seric 
55665059Seric #  else			/* short syslog buffer */
55765059Seric 
55865089Seric 	syslog(LOG_INFO, "%s: from=%s",
55965089Seric 		e->e_id, shortenstring(e->e_from.q_paddr, 83));
56065089Seric 	syslog(LOG_INFO, "%s: size=%ld, class=%ld, pri=%ld, nrcpts=%d",
56165089Seric 		e->e_id, e->e_msgsize, e->e_class,
56265089Seric 		e->e_msgpriority, e->e_nrcpts);
56365089Seric 	if (msgid != NULL)
56465059Seric 		syslog(LOG_INFO, "%s: msgid=%s", e->e_id, msgid);
56565650Seric 	sbp = sbuf;
56665650Seric 	sprintf(sbp, "%s:", e->e_id);
56765650Seric 	sbp += strlen(sbp);
56865089Seric 	if (e->e_bodytype != NULL)
56965650Seric 	{
57065650Seric 		sprintf(sbp, " bodytype=%s,", e->e_bodytype);
57165650Seric 		sbp += strlen(sbp);
57265650Seric 	}
57365089Seric 	p = macvalue('r', e);
57465089Seric 	if (p != NULL)
57565650Seric 	{
57665731Seric 		sprintf(sbp, " proto=%s,", p);
57765650Seric 		sbp += strlen(sbp);
57865650Seric 	}
57965650Seric 	syslog(LOG_INFO, "%s relay=%s", sbuf, name);
58065059Seric #  endif
5817783Seric }
5827783Seric /*
5837783Seric **  PRIENCODE -- encode external priority names into internal values.
5847783Seric **
5857783Seric **	Parameters:
5867783Seric **		p -- priority in ascii.
5877783Seric **
5887783Seric **	Returns:
5897783Seric **		priority as a numeric level.
5907783Seric **
5917783Seric **	Side Effects:
5927783Seric **		none.
5937783Seric */
5947783Seric 
5957783Seric priencode(p)
5967783Seric 	char *p;
5977783Seric {
5988253Seric 	register int i;
5997783Seric 
6008253Seric 	for (i = 0; i < NumPriorities; i++)
6017783Seric 	{
60233725Sbostic 		if (!strcasecmp(p, Priorities[i].pri_name))
6038253Seric 			return (Priorities[i].pri_val);
6047783Seric 	}
6058253Seric 
6068253Seric 	/* unknown priority */
6078253Seric 	return (0);
6087783Seric }
6097783Seric /*
6107890Seric **  CRACKADDR -- parse an address and turn it into a macro
6117783Seric **
6127783Seric **	This doesn't actually parse the address -- it just extracts
6137783Seric **	it and replaces it with "$g".  The parse is totally ad hoc
6147783Seric **	and isn't even guaranteed to leave something syntactically
6157783Seric **	identical to what it started with.  However, it does leave
6167783Seric **	something semantically identical.
6177783Seric **
61851379Seric **	This algorithm has been cleaned up to handle a wider range
61951379Seric **	of cases -- notably quoted and backslash escaped strings.
62051379Seric **	This modification makes it substantially better at preserving
62151379Seric **	the original syntax.
6227783Seric **
6237783Seric **	Parameters:
6247890Seric **		addr -- the address to be cracked.
6257783Seric **
6267783Seric **	Returns:
6277783Seric **		a pointer to the new version.
6287783Seric **
6297783Seric **	Side Effects:
6307890Seric **		none.
6317783Seric **
6327783Seric **	Warning:
6337783Seric **		The return value is saved in local storage and should
6347783Seric **		be copied if it is to be reused.
6357783Seric */
6367783Seric 
6377783Seric char *
6387890Seric crackaddr(addr)
6397890Seric 	register char *addr;
6407783Seric {
6417783Seric 	register char *p;
64251379Seric 	register char c;
64351379Seric 	int cmtlev;
64456764Seric 	int realcmtlev;
64556764Seric 	int anglelev, realanglelev;
64651379Seric 	int copylev;
64751379Seric 	bool qmode;
64856764Seric 	bool realqmode;
64956764Seric 	bool skipping;
65051379Seric 	bool putgmac = FALSE;
65156735Seric 	bool quoteit = FALSE;
65264148Seric 	bool gotangle = FALSE;
65351379Seric 	register char *bp;
65456764Seric 	char *buflim;
6557783Seric 	static char buf[MAXNAME];
6567783Seric 
6577783Seric 	if (tTd(33, 1))
6587890Seric 		printf("crackaddr(%s)\n", addr);
6597783Seric 
6608082Seric 	/* strip leading spaces */
66158050Seric 	while (*addr != '\0' && isascii(*addr) && isspace(*addr))
6628082Seric 		addr++;
6638082Seric 
6647783Seric 	/*
66551379Seric 	**  Start by assuming we have no angle brackets.  This will be
66651379Seric 	**  adjusted later if we find them.
6677783Seric 	*/
6687783Seric 
66951379Seric 	bp = buf;
67056764Seric 	buflim = &buf[sizeof buf - 5];
67151379Seric 	p = addr;
67256764Seric 	copylev = anglelev = realanglelev = cmtlev = realcmtlev = 0;
67356764Seric 	qmode = realqmode = FALSE;
67451379Seric 
67551379Seric 	while ((c = *p++) != '\0')
6767783Seric 	{
67756764Seric 		/*
67856764Seric 		**  If the buffer is overful, go into a special "skipping"
67956764Seric 		**  mode that tries to keep legal syntax but doesn't actually
68056764Seric 		**  output things.
68156764Seric 		*/
6827783Seric 
68356764Seric 		skipping = bp >= buflim;
68456735Seric 
68556764Seric 		if (copylev > 0 && !skipping)
68656764Seric 			*bp++ = c;
68756735Seric 
68851379Seric 		/* check for backslash escapes */
68951379Seric 		if (c == '\\')
6907783Seric 		{
69158890Seric 			/* arrange to quote the address */
69258890Seric 			if (cmtlev <= 0 && !qmode)
69358890Seric 				quoteit = TRUE;
69458890Seric 
69551379Seric 			if ((c = *p++) == '\0')
6967783Seric 			{
69751379Seric 				/* too far */
69851379Seric 				p--;
69951379Seric 				goto putg;
7007783Seric 			}
70156764Seric 			if (copylev > 0 && !skipping)
70251379Seric 				*bp++ = c;
70351379Seric 			goto putg;
7047783Seric 		}
7057783Seric 
70651379Seric 		/* check for quoted strings */
70764967Seric 		if (c == '"' && cmtlev <= 0)
7087783Seric 		{
70951379Seric 			qmode = !qmode;
71056764Seric 			if (copylev > 0 && !skipping)
71156764Seric 				realqmode = !realqmode;
71251379Seric 			continue;
7137783Seric 		}
71451379Seric 		if (qmode)
71551379Seric 			goto putg;
7167783Seric 
71751379Seric 		/* check for comments */
71851379Seric 		if (c == '(')
7197783Seric 		{
72051379Seric 			cmtlev++;
72156764Seric 
72256764Seric 			/* allow space for closing paren */
72356764Seric 			if (!skipping)
72456764Seric 			{
72556764Seric 				buflim--;
72656764Seric 				realcmtlev++;
72756764Seric 				if (copylev++ <= 0)
72856764Seric 				{
72956764Seric 					*bp++ = ' ';
73056764Seric 					*bp++ = c;
73156764Seric 				}
73256764Seric 			}
73351379Seric 		}
73451379Seric 		if (cmtlev > 0)
73551379Seric 		{
73651379Seric 			if (c == ')')
7377783Seric 			{
73851379Seric 				cmtlev--;
73951379Seric 				copylev--;
74056764Seric 				if (!skipping)
74156764Seric 				{
74256764Seric 					realcmtlev--;
74356764Seric 					buflim++;
74456764Seric 				}
7457783Seric 			}
7467783Seric 			continue;
7477783Seric 		}
74856764Seric 		else if (c == ')')
74956764Seric 		{
75056764Seric 			/* syntax error: unmatched ) */
75165544Seric 			if (copylev > 0 && !skipping)
75256764Seric 				bp--;
75356764Seric 		}
7547783Seric 
75556764Seric 		/* check for characters that may have to be quoted */
75664148Seric 		if (strchr(".'@,;:\\()[]", c) != NULL)
75756764Seric 		{
75856764Seric 			/*
75956764Seric 			**  If these occur as the phrase part of a <>
76056764Seric 			**  construct, but are not inside of () or already
76156764Seric 			**  quoted, they will have to be quoted.  Note that
76256764Seric 			**  now (but don't actually do the quoting).
76356764Seric 			*/
76456764Seric 
76556764Seric 			if (cmtlev <= 0 && !qmode)
76656764Seric 				quoteit = TRUE;
76756764Seric 		}
76856764Seric 
76951379Seric 		/* check for angle brackets */
77051379Seric 		if (c == '<')
77151379Seric 		{
77256735Seric 			register char *q;
77356735Seric 
77464148Seric 			/* assume first of two angles is bogus */
77564148Seric 			if (gotangle)
77664148Seric 				quoteit = TRUE;
77764148Seric 			gotangle = TRUE;
77864148Seric 
77951379Seric 			/* oops -- have to change our mind */
78064752Seric 			anglelev = 1;
78156764Seric 			if (!skipping)
78264752Seric 				realanglelev = 1;
78356764Seric 
78456735Seric 			bp = buf;
78556735Seric 			if (quoteit)
78656735Seric 			{
78756735Seric 				*bp++ = '"';
78856735Seric 
78956735Seric 				/* back up over the '<' and any spaces */
79056735Seric 				--p;
79158050Seric 				while (isascii(*--p) && isspace(*p))
79256735Seric 					continue;
79356735Seric 				p++;
79456735Seric 			}
79556735Seric 			for (q = addr; q < p; )
79656735Seric 			{
79756735Seric 				c = *q++;
79856764Seric 				if (bp < buflim)
79956764Seric 				{
80056764Seric 					if (quoteit && c == '"')
80156764Seric 						*bp++ = '\\';
80256764Seric 					*bp++ = c;
80356764Seric 				}
80456735Seric 			}
80556735Seric 			if (quoteit)
80656735Seric 			{
80764148Seric 				if (bp == &buf[1])
80864148Seric 					bp--;
80964148Seric 				else
81064148Seric 					*bp++ = '"';
81156764Seric 				while ((c = *p++) != '<')
81256764Seric 				{
81356764Seric 					if (bp < buflim)
81456764Seric 						*bp++ = c;
81556764Seric 				}
81656764Seric 				*bp++ = c;
81756735Seric 			}
81851379Seric 			copylev = 0;
81956735Seric 			putgmac = quoteit = FALSE;
82051379Seric 			continue;
82151379Seric 		}
8227783Seric 
82351379Seric 		if (c == '>')
8247783Seric 		{
82556764Seric 			if (anglelev > 0)
82656764Seric 			{
82756764Seric 				anglelev--;
82856764Seric 				if (!skipping)
82956764Seric 				{
83056764Seric 					realanglelev--;
83156764Seric 					buflim++;
83256764Seric 				}
83356764Seric 			}
83456764Seric 			else if (!skipping)
83556764Seric 			{
83656764Seric 				/* syntax error: unmatched > */
83756764Seric 				if (copylev > 0)
83856764Seric 					bp--;
83964752Seric 				quoteit = TRUE;
84056764Seric 				continue;
84156764Seric 			}
84251379Seric 			if (copylev++ <= 0)
84351379Seric 				*bp++ = c;
84451379Seric 			continue;
8457783Seric 		}
84651379Seric 
84751379Seric 		/* must be a real address character */
84851379Seric 	putg:
84951379Seric 		if (copylev <= 0 && !putgmac)
85051379Seric 		{
85158050Seric 			*bp++ = MACROEXPAND;
85251379Seric 			*bp++ = 'g';
85351379Seric 			putgmac = TRUE;
85451379Seric 		}
8557783Seric 	}
8567783Seric 
85756764Seric 	/* repair any syntactic damage */
85856764Seric 	if (realqmode)
85956764Seric 		*bp++ = '"';
86056764Seric 	while (realcmtlev-- > 0)
86156764Seric 		*bp++ = ')';
86256764Seric 	while (realanglelev-- > 0)
86356764Seric 		*bp++ = '>';
86451379Seric 	*bp++ = '\0';
8657783Seric 
8667783Seric 	if (tTd(33, 1))
8677944Seric 		printf("crackaddr=>`%s'\n", buf);
8687783Seric 
8697783Seric 	return (buf);
8707783Seric }
8719382Seric /*
8729382Seric **  PUTHEADER -- put the header part of a message from the in-core copy
8739382Seric **
8749382Seric **	Parameters:
87565870Seric **		mci -- the connection information.
8769382Seric **		e -- envelope to use.
8779382Seric **
8789382Seric **	Returns:
8799382Seric **		none.
8809382Seric **
8819382Seric **	Side Effects:
8829382Seric **		none.
8839382Seric */
8849382Seric 
88557589Seric /*
88657589Seric  * Macro for fast max (not available in e.g. DG/UX, 386/ix).
88757589Seric  */
88857589Seric #ifndef MAX
88957589Seric # define MAX(a,b) (((a)>(b))?(a):(b))
89057589Seric #endif
89157589Seric 
89265870Seric putheader(mci, e)
89365870Seric 	register MCI *mci;
8949382Seric 	register ENVELOPE *e;
8959382Seric {
89657135Seric 	char buf[MAX(MAXLINE,BUFSIZ)];
8979382Seric 	register HDR *h;
89857135Seric 	char obuf[MAXLINE];
8999382Seric 
90059882Seric 	if (tTd(34, 1))
90165870Seric 		printf("--- putheader, mailer = %s ---\n",
90265870Seric 			mci->mci_mailer->m_name);
90359882Seric 
9049382Seric 	for (h = e->e_header; h != NULL; h = h->h_link)
9059382Seric 	{
9069382Seric 		register char *p;
90710689Seric 		extern bool bitintersect();
9089382Seric 
90959882Seric 		if (tTd(34, 11))
91059882Seric 		{
91159882Seric 			printf("  %s: ", h->h_field);
91259882Seric 			xputs(h->h_value);
91359882Seric 		}
91459882Seric 
9159382Seric 		if (bitset(H_CHECK|H_ACHECK, h->h_flags) &&
91665870Seric 		    !bitintersect(h->h_mflags, mci->mci_mailer->m_flags))
91759882Seric 		{
91859882Seric 			if (tTd(34, 11))
91959882Seric 				printf(" (skipped)\n");
9209382Seric 			continue;
92159882Seric 		}
9229382Seric 
92311414Seric 		/* handle Resent-... headers specially */
92411414Seric 		if (bitset(H_RESENT, h->h_flags) && !bitset(EF_RESENT, e->e_flags))
92559882Seric 		{
92659882Seric 			if (tTd(34, 11))
92759882Seric 				printf(" (skipped (resent))\n");
92811414Seric 			continue;
92959882Seric 		}
93011414Seric 
93165152Seric 		/* macro expand value if generated internally */
9329382Seric 		p = h->h_value;
9339382Seric 		if (bitset(H_DEFAULT, h->h_flags))
9349382Seric 		{
9359382Seric 			expand(p, buf, &buf[sizeof buf], e);
9369382Seric 			p = buf;
9379382Seric 			if (p == NULL || *p == '\0')
93865152Seric 			{
93965152Seric 				if (tTd(34, 11))
94065152Seric 					printf(" (skipped -- null value)\n");
9419382Seric 				continue;
94265152Seric 			}
9439382Seric 		}
9449382Seric 
94565152Seric 		if (tTd(34, 11))
94665152Seric 			printf("\n");
94765152Seric 
9489382Seric 		if (bitset(H_FROM|H_RCPT, h->h_flags))
9499382Seric 		{
9509382Seric 			/* address field */
9519382Seric 			bool oldstyle = bitset(EF_OLDSTYLE, e->e_flags);
9529382Seric 
9539382Seric 			if (bitset(H_FROM, h->h_flags))
9549382Seric 				oldstyle = FALSE;
95565870Seric 			commaize(h, p, oldstyle, mci, e);
9569382Seric 		}
9579382Seric 		else
9589382Seric 		{
9599382Seric 			/* vanilla header line */
96012159Seric 			register char *nlp;
96112159Seric 
96259579Seric 			(void) sprintf(obuf, "%s: ", h->h_field);
96356795Seric 			while ((nlp = strchr(p, '\n')) != NULL)
96412159Seric 			{
96512159Seric 				*nlp = '\0';
96612159Seric 				(void) strcat(obuf, p);
96712159Seric 				*nlp = '\n';
96865870Seric 				putline(obuf, mci);
96912159Seric 				p = ++nlp;
97012161Seric 				obuf[0] = '\0';
97112159Seric 			}
97212159Seric 			(void) strcat(obuf, p);
97365870Seric 			putline(obuf, mci);
9749382Seric 		}
9759382Seric 	}
9769382Seric }
9779382Seric /*
9789382Seric **  COMMAIZE -- output a header field, making a comma-translated list.
9799382Seric **
9809382Seric **	Parameters:
9819382Seric **		h -- the header field to output.
9829382Seric **		p -- the value to put in it.
9839382Seric **		oldstyle -- TRUE if this is an old style header.
98465870Seric **		mci -- the connection information.
98555012Seric **		e -- the envelope containing the message.
9869382Seric **
9879382Seric **	Returns:
9889382Seric **		none.
9899382Seric **
9909382Seric **	Side Effects:
9919382Seric **		outputs "p" to file "fp".
9929382Seric */
9939382Seric 
99465870Seric void
99565870Seric commaize(h, p, oldstyle, mci, e)
9969382Seric 	register HDR *h;
9979382Seric 	register char *p;
9989382Seric 	bool oldstyle;
99965870Seric 	register MCI *mci;
100055012Seric 	register ENVELOPE *e;
10019382Seric {
10029382Seric 	register char *obp;
10039382Seric 	int opos;
100466004Seric 	int omax;
10059382Seric 	bool firstone = TRUE;
100611157Seric 	char obuf[MAXLINE + 3];
10079382Seric 
10089382Seric 	/*
10099382Seric 	**  Output the address list translated by the
10109382Seric 	**  mailer and with commas.
10119382Seric 	*/
10129382Seric 
10139382Seric 	if (tTd(14, 2))
10149382Seric 		printf("commaize(%s: %s)\n", h->h_field, p);
10159382Seric 
10169382Seric 	obp = obuf;
101759579Seric 	(void) sprintf(obp, "%s: ", h->h_field);
10189382Seric 	opos = strlen(h->h_field) + 2;
10199382Seric 	obp += opos;
102066254Seric 	omax = mci->mci_mailer->m_linelimit - 2;
102166254Seric 	if (omax < 0 || omax > 78)
102266254Seric 		omax = 78;
10239382Seric 
10249382Seric 	/*
10259382Seric 	**  Run through the list of values.
10269382Seric 	*/
10279382Seric 
10289382Seric 	while (*p != '\0')
10299382Seric 	{
10309382Seric 		register char *name;
103154983Seric 		register int c;
10329382Seric 		char savechar;
103359163Seric 		int flags;
103459163Seric 		auto int stat;
10359382Seric 
10369382Seric 		/*
10379382Seric 		**  Find the end of the name.  New style names
10389382Seric 		**  end with a comma, old style names end with
10399382Seric 		**  a space character.  However, spaces do not
10409382Seric 		**  necessarily delimit an old-style name -- at
10419382Seric 		**  signs mean keep going.
10429382Seric 		*/
10439382Seric 
10449382Seric 		/* find end of name */
104558050Seric 		while ((isascii(*p) && isspace(*p)) || *p == ',')
10469382Seric 			p++;
10479382Seric 		name = p;
10489382Seric 		for (;;)
10499382Seric 		{
105058333Seric 			auto char *oldp;
105116909Seric 			char pvpbuf[PSBUFSIZE];
10529382Seric 
105365066Seric 			(void) prescan(p, oldstyle ? ' ' : ',', pvpbuf,
105465066Seric 				       sizeof pvpbuf, &oldp);
105558333Seric 			p = oldp;
10569382Seric 
10579382Seric 			/* look to see if we have an at sign */
105858050Seric 			while (*p != '\0' && isascii(*p) && isspace(*p))
10599382Seric 				p++;
10609382Seric 
106158170Seric 			if (*p != '@')
10629382Seric 			{
10639382Seric 				p = oldp;
10649382Seric 				break;
10659382Seric 			}
10669382Seric 			p += *p == '@' ? 1 : 2;
106758050Seric 			while (*p != '\0' && isascii(*p) && isspace(*p))
10689382Seric 				p++;
10699382Seric 		}
10709382Seric 		/* at the end of one complete name */
10719382Seric 
10729382Seric 		/* strip off trailing white space */
107358050Seric 		while (p >= name &&
107458050Seric 		       ((isascii(*p) && isspace(*p)) || *p == ',' || *p == '\0'))
10759382Seric 			p--;
10769382Seric 		if (++p == name)
10779382Seric 			continue;
10789382Seric 		savechar = *p;
10799382Seric 		*p = '\0';
10809382Seric 
10819382Seric 		/* translate the name to be relative */
108259163Seric 		flags = RF_HEADERADDR|RF_ADDDOMAIN;
108359163Seric 		if (bitset(H_FROM, h->h_flags))
108459163Seric 			flags |= RF_SENDERADDR;
108559163Seric 		stat = EX_OK;
108665870Seric 		name = remotename(name, mci->mci_mailer, flags, &stat, e);
10879382Seric 		if (*name == '\0')
10889382Seric 		{
10899382Seric 			*p = savechar;
10909382Seric 			continue;
10919382Seric 		}
10929382Seric 
10939382Seric 		/* output the name with nice formatting */
109454983Seric 		opos += strlen(name);
10959382Seric 		if (!firstone)
10969382Seric 			opos += 2;
109766004Seric 		if (opos > omax && !firstone)
10989382Seric 		{
109910178Seric 			(void) strcpy(obp, ",\n");
110065870Seric 			putline(obuf, mci);
11019382Seric 			obp = obuf;
1102*66255Seric 			(void) strcpy(obp, "        ");
110310161Seric 			opos = strlen(obp);
110410161Seric 			obp += opos;
110554983Seric 			opos += strlen(name);
11069382Seric 		}
11079382Seric 		else if (!firstone)
11089382Seric 		{
1109*66255Seric 			(void) strcpy(obp, ", ");
11109382Seric 			obp += 2;
11119382Seric 		}
11129382Seric 
111354983Seric 		while ((c = *name++) != '\0' && obp < &obuf[MAXLINE])
111454983Seric 			*obp++ = c;
11159382Seric 		firstone = FALSE;
11169382Seric 		*p = savechar;
11179382Seric 	}
11189382Seric 	(void) strcpy(obp, "\n");
111965870Seric 	putline(obuf, mci);
11209382Seric }
11219382Seric /*
112258170Seric **  COPYHEADER -- copy header list
11239382Seric **
112458170Seric **	This routine is the equivalent of newstr for header lists
112558170Seric **
11269382Seric **	Parameters:
112758170Seric **		header -- list of header structures to copy.
11289382Seric **
11299382Seric **	Returns:
113058170Seric **		a copy of 'header'.
11319382Seric **
11329382Seric **	Side Effects:
11339382Seric **		none.
11349382Seric */
11359382Seric 
113658170Seric HDR *
113758170Seric copyheader(header)
113858170Seric 	register HDR *header;
11399382Seric {
114058170Seric 	register HDR *newhdr;
114158170Seric 	HDR *ret;
114258170Seric 	register HDR **tail = &ret;
11439382Seric 
114458170Seric 	while (header != NULL)
114558170Seric 	{
114658170Seric 		newhdr = (HDR *) xalloc(sizeof(HDR));
114758170Seric 		STRUCTCOPY(*header, *newhdr);
114858170Seric 		*tail = newhdr;
114958170Seric 		tail = &newhdr->h_link;
115058170Seric 		header = header->h_link;
115158170Seric 	}
115258170Seric 	*tail = NULL;
115358170Seric 
115458170Seric 	return ret;
11559382Seric }
1156