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*66748Seric static char sccsid[] = "@(#)headers.c	8.31 (Berkeley) 04/12/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 {
507*66748Seric # ifdef LOG
50865089Seric 	char *name;
50965089Seric 	register char *sbp;
51065089Seric 	register char *p;
51165089Seric 	char hbuf[MAXNAME];
51265089Seric 	char sbuf[MAXLINE];
51365089Seric 
51465089Seric 	if (bitset(EF_RESPONSE, e->e_flags))
51565089Seric 		name = "[RESPONSE]";
51665089Seric 	else if ((name = macvalue('_', e)) != NULL)
51765089Seric 		;
51866003Seric 	else if (RealHostName == NULL)
51966003Seric 		name = "localhost";
52065089Seric 	else if (RealHostName[0] == '[')
52165089Seric 		name = RealHostName;
52265089Seric 	else
52311290Seric 	{
52465089Seric 		name = hbuf;
52565089Seric 		(void) sprintf(hbuf, "%.80s", RealHostName);
52665089Seric 		if (RealHostAddr.sa.sa_family != 0)
52757359Seric 		{
52865089Seric 			p = &hbuf[strlen(hbuf)];
52965089Seric 			(void) sprintf(p, " (%s)",
53065089Seric 				anynet_ntoa(&RealHostAddr));
53157359Seric 		}
53265089Seric 	}
53357359Seric 
53465089Seric 	/* some versions of syslog only take 5 printf args */
53565059Seric #  if (SYSLOG_BUFSIZE) >= 256
53665089Seric 	sbp = sbuf;
53765089Seric 	sprintf(sbp, "from=%.200s, size=%ld, class=%d, pri=%ld, nrcpts=%d",
53865089Seric 	    e->e_from.q_paddr, e->e_msgsize, e->e_class,
53965089Seric 	    e->e_msgpriority, e->e_nrcpts);
54065089Seric 	sbp += strlen(sbp);
54165089Seric 	if (msgid != NULL)
54265089Seric 	{
54365089Seric 		sprintf(sbp, ", msgid=%.100s", msgid);
54460575Seric 		sbp += strlen(sbp);
54565089Seric 	}
54665089Seric 	if (e->e_bodytype != NULL)
54765089Seric 	{
54865089Seric 		(void) sprintf(sbp, ", bodytype=%.20s", e->e_bodytype);
54965089Seric 		sbp += strlen(sbp);
55065089Seric 	}
55165089Seric 	p = macvalue('r', e);
55265089Seric 	if (p != NULL)
55365089Seric 		(void) sprintf(sbp, ", proto=%.20s", p);
55465089Seric 	syslog(LOG_INFO, "%s: %s, relay=%s",
55565089Seric 	    e->e_id, sbuf, name);
55665059Seric 
55765059Seric #  else			/* short syslog buffer */
55865059Seric 
55965089Seric 	syslog(LOG_INFO, "%s: from=%s",
56065089Seric 		e->e_id, shortenstring(e->e_from.q_paddr, 83));
56165089Seric 	syslog(LOG_INFO, "%s: size=%ld, class=%ld, pri=%ld, nrcpts=%d",
56265089Seric 		e->e_id, e->e_msgsize, e->e_class,
56365089Seric 		e->e_msgpriority, e->e_nrcpts);
56465089Seric 	if (msgid != NULL)
56565059Seric 		syslog(LOG_INFO, "%s: msgid=%s", e->e_id, msgid);
56665650Seric 	sbp = sbuf;
56765650Seric 	sprintf(sbp, "%s:", e->e_id);
56865650Seric 	sbp += strlen(sbp);
56965089Seric 	if (e->e_bodytype != NULL)
57065650Seric 	{
57165650Seric 		sprintf(sbp, " bodytype=%s,", e->e_bodytype);
57265650Seric 		sbp += strlen(sbp);
57365650Seric 	}
57465089Seric 	p = macvalue('r', e);
57565089Seric 	if (p != NULL)
57665650Seric 	{
57765731Seric 		sprintf(sbp, " proto=%s,", p);
57865650Seric 		sbp += strlen(sbp);
57965650Seric 	}
58065650Seric 	syslog(LOG_INFO, "%s relay=%s", sbuf, name);
58165059Seric #  endif
582*66748Seric # endif
5837783Seric }
5847783Seric /*
5857783Seric **  PRIENCODE -- encode external priority names into internal values.
5867783Seric **
5877783Seric **	Parameters:
5887783Seric **		p -- priority in ascii.
5897783Seric **
5907783Seric **	Returns:
5917783Seric **		priority as a numeric level.
5927783Seric **
5937783Seric **	Side Effects:
5947783Seric **		none.
5957783Seric */
5967783Seric 
5977783Seric priencode(p)
5987783Seric 	char *p;
5997783Seric {
6008253Seric 	register int i;
6017783Seric 
6028253Seric 	for (i = 0; i < NumPriorities; i++)
6037783Seric 	{
60433725Sbostic 		if (!strcasecmp(p, Priorities[i].pri_name))
6058253Seric 			return (Priorities[i].pri_val);
6067783Seric 	}
6078253Seric 
6088253Seric 	/* unknown priority */
6098253Seric 	return (0);
6107783Seric }
6117783Seric /*
6127890Seric **  CRACKADDR -- parse an address and turn it into a macro
6137783Seric **
6147783Seric **	This doesn't actually parse the address -- it just extracts
6157783Seric **	it and replaces it with "$g".  The parse is totally ad hoc
6167783Seric **	and isn't even guaranteed to leave something syntactically
6177783Seric **	identical to what it started with.  However, it does leave
6187783Seric **	something semantically identical.
6197783Seric **
62051379Seric **	This algorithm has been cleaned up to handle a wider range
62151379Seric **	of cases -- notably quoted and backslash escaped strings.
62251379Seric **	This modification makes it substantially better at preserving
62351379Seric **	the original syntax.
6247783Seric **
6257783Seric **	Parameters:
6267890Seric **		addr -- the address to be cracked.
6277783Seric **
6287783Seric **	Returns:
6297783Seric **		a pointer to the new version.
6307783Seric **
6317783Seric **	Side Effects:
6327890Seric **		none.
6337783Seric **
6347783Seric **	Warning:
6357783Seric **		The return value is saved in local storage and should
6367783Seric **		be copied if it is to be reused.
6377783Seric */
6387783Seric 
6397783Seric char *
6407890Seric crackaddr(addr)
6417890Seric 	register char *addr;
6427783Seric {
6437783Seric 	register char *p;
64451379Seric 	register char c;
64551379Seric 	int cmtlev;
64656764Seric 	int realcmtlev;
64756764Seric 	int anglelev, realanglelev;
64851379Seric 	int copylev;
64951379Seric 	bool qmode;
65056764Seric 	bool realqmode;
65156764Seric 	bool skipping;
65251379Seric 	bool putgmac = FALSE;
65356735Seric 	bool quoteit = FALSE;
65464148Seric 	bool gotangle = FALSE;
65551379Seric 	register char *bp;
65656764Seric 	char *buflim;
6577783Seric 	static char buf[MAXNAME];
6587783Seric 
6597783Seric 	if (tTd(33, 1))
6607890Seric 		printf("crackaddr(%s)\n", addr);
6617783Seric 
6628082Seric 	/* strip leading spaces */
66358050Seric 	while (*addr != '\0' && isascii(*addr) && isspace(*addr))
6648082Seric 		addr++;
6658082Seric 
6667783Seric 	/*
66751379Seric 	**  Start by assuming we have no angle brackets.  This will be
66851379Seric 	**  adjusted later if we find them.
6697783Seric 	*/
6707783Seric 
67151379Seric 	bp = buf;
67256764Seric 	buflim = &buf[sizeof buf - 5];
67351379Seric 	p = addr;
67456764Seric 	copylev = anglelev = realanglelev = cmtlev = realcmtlev = 0;
67556764Seric 	qmode = realqmode = FALSE;
67651379Seric 
67751379Seric 	while ((c = *p++) != '\0')
6787783Seric 	{
67956764Seric 		/*
68056764Seric 		**  If the buffer is overful, go into a special "skipping"
68156764Seric 		**  mode that tries to keep legal syntax but doesn't actually
68256764Seric 		**  output things.
68356764Seric 		*/
6847783Seric 
68556764Seric 		skipping = bp >= buflim;
68656735Seric 
68756764Seric 		if (copylev > 0 && !skipping)
68856764Seric 			*bp++ = c;
68956735Seric 
69051379Seric 		/* check for backslash escapes */
69151379Seric 		if (c == '\\')
6927783Seric 		{
69358890Seric 			/* arrange to quote the address */
69458890Seric 			if (cmtlev <= 0 && !qmode)
69558890Seric 				quoteit = TRUE;
69658890Seric 
69751379Seric 			if ((c = *p++) == '\0')
6987783Seric 			{
69951379Seric 				/* too far */
70051379Seric 				p--;
70151379Seric 				goto putg;
7027783Seric 			}
70356764Seric 			if (copylev > 0 && !skipping)
70451379Seric 				*bp++ = c;
70551379Seric 			goto putg;
7067783Seric 		}
7077783Seric 
70851379Seric 		/* check for quoted strings */
70964967Seric 		if (c == '"' && cmtlev <= 0)
7107783Seric 		{
71151379Seric 			qmode = !qmode;
71256764Seric 			if (copylev > 0 && !skipping)
71356764Seric 				realqmode = !realqmode;
71451379Seric 			continue;
7157783Seric 		}
71651379Seric 		if (qmode)
71751379Seric 			goto putg;
7187783Seric 
71951379Seric 		/* check for comments */
72051379Seric 		if (c == '(')
7217783Seric 		{
72251379Seric 			cmtlev++;
72356764Seric 
72456764Seric 			/* allow space for closing paren */
72556764Seric 			if (!skipping)
72656764Seric 			{
72756764Seric 				buflim--;
72856764Seric 				realcmtlev++;
72956764Seric 				if (copylev++ <= 0)
73056764Seric 				{
73156764Seric 					*bp++ = ' ';
73256764Seric 					*bp++ = c;
73356764Seric 				}
73456764Seric 			}
73551379Seric 		}
73651379Seric 		if (cmtlev > 0)
73751379Seric 		{
73851379Seric 			if (c == ')')
7397783Seric 			{
74051379Seric 				cmtlev--;
74151379Seric 				copylev--;
74256764Seric 				if (!skipping)
74356764Seric 				{
74456764Seric 					realcmtlev--;
74556764Seric 					buflim++;
74656764Seric 				}
7477783Seric 			}
7487783Seric 			continue;
7497783Seric 		}
75056764Seric 		else if (c == ')')
75156764Seric 		{
75256764Seric 			/* syntax error: unmatched ) */
75365544Seric 			if (copylev > 0 && !skipping)
75456764Seric 				bp--;
75556764Seric 		}
7567783Seric 
75756764Seric 		/* check for characters that may have to be quoted */
75864148Seric 		if (strchr(".'@,;:\\()[]", c) != NULL)
75956764Seric 		{
76056764Seric 			/*
76156764Seric 			**  If these occur as the phrase part of a <>
76256764Seric 			**  construct, but are not inside of () or already
76356764Seric 			**  quoted, they will have to be quoted.  Note that
76456764Seric 			**  now (but don't actually do the quoting).
76556764Seric 			*/
76656764Seric 
76756764Seric 			if (cmtlev <= 0 && !qmode)
76856764Seric 				quoteit = TRUE;
76956764Seric 		}
77056764Seric 
77151379Seric 		/* check for angle brackets */
77251379Seric 		if (c == '<')
77351379Seric 		{
77456735Seric 			register char *q;
77556735Seric 
77664148Seric 			/* assume first of two angles is bogus */
77764148Seric 			if (gotangle)
77864148Seric 				quoteit = TRUE;
77964148Seric 			gotangle = TRUE;
78064148Seric 
78151379Seric 			/* oops -- have to change our mind */
78264752Seric 			anglelev = 1;
78356764Seric 			if (!skipping)
78464752Seric 				realanglelev = 1;
78556764Seric 
78656735Seric 			bp = buf;
78756735Seric 			if (quoteit)
78856735Seric 			{
78956735Seric 				*bp++ = '"';
79056735Seric 
79156735Seric 				/* back up over the '<' and any spaces */
79256735Seric 				--p;
79358050Seric 				while (isascii(*--p) && isspace(*p))
79456735Seric 					continue;
79556735Seric 				p++;
79656735Seric 			}
79756735Seric 			for (q = addr; q < p; )
79856735Seric 			{
79956735Seric 				c = *q++;
80056764Seric 				if (bp < buflim)
80156764Seric 				{
80256764Seric 					if (quoteit && c == '"')
80356764Seric 						*bp++ = '\\';
80456764Seric 					*bp++ = c;
80556764Seric 				}
80656735Seric 			}
80756735Seric 			if (quoteit)
80856735Seric 			{
80964148Seric 				if (bp == &buf[1])
81064148Seric 					bp--;
81164148Seric 				else
81264148Seric 					*bp++ = '"';
81356764Seric 				while ((c = *p++) != '<')
81456764Seric 				{
81556764Seric 					if (bp < buflim)
81656764Seric 						*bp++ = c;
81756764Seric 				}
81856764Seric 				*bp++ = c;
81956735Seric 			}
82051379Seric 			copylev = 0;
82156735Seric 			putgmac = quoteit = FALSE;
82251379Seric 			continue;
82351379Seric 		}
8247783Seric 
82551379Seric 		if (c == '>')
8267783Seric 		{
82756764Seric 			if (anglelev > 0)
82856764Seric 			{
82956764Seric 				anglelev--;
83056764Seric 				if (!skipping)
83156764Seric 				{
83256764Seric 					realanglelev--;
83356764Seric 					buflim++;
83456764Seric 				}
83556764Seric 			}
83656764Seric 			else if (!skipping)
83756764Seric 			{
83856764Seric 				/* syntax error: unmatched > */
83956764Seric 				if (copylev > 0)
84056764Seric 					bp--;
84164752Seric 				quoteit = TRUE;
84256764Seric 				continue;
84356764Seric 			}
84451379Seric 			if (copylev++ <= 0)
84551379Seric 				*bp++ = c;
84651379Seric 			continue;
8477783Seric 		}
84851379Seric 
84951379Seric 		/* must be a real address character */
85051379Seric 	putg:
85151379Seric 		if (copylev <= 0 && !putgmac)
85251379Seric 		{
85358050Seric 			*bp++ = MACROEXPAND;
85451379Seric 			*bp++ = 'g';
85551379Seric 			putgmac = TRUE;
85651379Seric 		}
8577783Seric 	}
8587783Seric 
85956764Seric 	/* repair any syntactic damage */
86056764Seric 	if (realqmode)
86156764Seric 		*bp++ = '"';
86256764Seric 	while (realcmtlev-- > 0)
86356764Seric 		*bp++ = ')';
86456764Seric 	while (realanglelev-- > 0)
86556764Seric 		*bp++ = '>';
86651379Seric 	*bp++ = '\0';
8677783Seric 
8687783Seric 	if (tTd(33, 1))
8697944Seric 		printf("crackaddr=>`%s'\n", buf);
8707783Seric 
8717783Seric 	return (buf);
8727783Seric }
8739382Seric /*
8749382Seric **  PUTHEADER -- put the header part of a message from the in-core copy
8759382Seric **
8769382Seric **	Parameters:
87765870Seric **		mci -- the connection information.
8789382Seric **		e -- envelope to use.
8799382Seric **
8809382Seric **	Returns:
8819382Seric **		none.
8829382Seric **
8839382Seric **	Side Effects:
8849382Seric **		none.
8859382Seric */
8869382Seric 
88757589Seric /*
88857589Seric  * Macro for fast max (not available in e.g. DG/UX, 386/ix).
88957589Seric  */
89057589Seric #ifndef MAX
89157589Seric # define MAX(a,b) (((a)>(b))?(a):(b))
89257589Seric #endif
89357589Seric 
89465870Seric putheader(mci, e)
89565870Seric 	register MCI *mci;
8969382Seric 	register ENVELOPE *e;
8979382Seric {
89857135Seric 	char buf[MAX(MAXLINE,BUFSIZ)];
8999382Seric 	register HDR *h;
90057135Seric 	char obuf[MAXLINE];
9019382Seric 
90259882Seric 	if (tTd(34, 1))
90365870Seric 		printf("--- putheader, mailer = %s ---\n",
90465870Seric 			mci->mci_mailer->m_name);
90559882Seric 
9069382Seric 	for (h = e->e_header; h != NULL; h = h->h_link)
9079382Seric 	{
9089382Seric 		register char *p;
90910689Seric 		extern bool bitintersect();
9109382Seric 
91159882Seric 		if (tTd(34, 11))
91259882Seric 		{
91359882Seric 			printf("  %s: ", h->h_field);
91459882Seric 			xputs(h->h_value);
91559882Seric 		}
91659882Seric 
9179382Seric 		if (bitset(H_CHECK|H_ACHECK, h->h_flags) &&
91865870Seric 		    !bitintersect(h->h_mflags, mci->mci_mailer->m_flags))
91959882Seric 		{
92059882Seric 			if (tTd(34, 11))
92159882Seric 				printf(" (skipped)\n");
9229382Seric 			continue;
92359882Seric 		}
9249382Seric 
92511414Seric 		/* handle Resent-... headers specially */
92611414Seric 		if (bitset(H_RESENT, h->h_flags) && !bitset(EF_RESENT, e->e_flags))
92759882Seric 		{
92859882Seric 			if (tTd(34, 11))
92959882Seric 				printf(" (skipped (resent))\n");
93011414Seric 			continue;
93159882Seric 		}
93211414Seric 
93365152Seric 		/* macro expand value if generated internally */
9349382Seric 		p = h->h_value;
9359382Seric 		if (bitset(H_DEFAULT, h->h_flags))
9369382Seric 		{
9379382Seric 			expand(p, buf, &buf[sizeof buf], e);
9389382Seric 			p = buf;
9399382Seric 			if (p == NULL || *p == '\0')
94065152Seric 			{
94165152Seric 				if (tTd(34, 11))
94265152Seric 					printf(" (skipped -- null value)\n");
9439382Seric 				continue;
94465152Seric 			}
9459382Seric 		}
9469382Seric 
94765152Seric 		if (tTd(34, 11))
94865152Seric 			printf("\n");
94965152Seric 
9509382Seric 		if (bitset(H_FROM|H_RCPT, h->h_flags))
9519382Seric 		{
9529382Seric 			/* address field */
9539382Seric 			bool oldstyle = bitset(EF_OLDSTYLE, e->e_flags);
9549382Seric 
9559382Seric 			if (bitset(H_FROM, h->h_flags))
9569382Seric 				oldstyle = FALSE;
95765870Seric 			commaize(h, p, oldstyle, mci, e);
9589382Seric 		}
9599382Seric 		else
9609382Seric 		{
9619382Seric 			/* vanilla header line */
96212159Seric 			register char *nlp;
96312159Seric 
96459579Seric 			(void) sprintf(obuf, "%s: ", h->h_field);
96556795Seric 			while ((nlp = strchr(p, '\n')) != NULL)
96612159Seric 			{
96712159Seric 				*nlp = '\0';
96812159Seric 				(void) strcat(obuf, p);
96912159Seric 				*nlp = '\n';
97065870Seric 				putline(obuf, mci);
97112159Seric 				p = ++nlp;
97212161Seric 				obuf[0] = '\0';
97312159Seric 			}
97412159Seric 			(void) strcat(obuf, p);
97565870Seric 			putline(obuf, mci);
9769382Seric 		}
9779382Seric 	}
9789382Seric }
9799382Seric /*
9809382Seric **  COMMAIZE -- output a header field, making a comma-translated list.
9819382Seric **
9829382Seric **	Parameters:
9839382Seric **		h -- the header field to output.
9849382Seric **		p -- the value to put in it.
9859382Seric **		oldstyle -- TRUE if this is an old style header.
98665870Seric **		mci -- the connection information.
98755012Seric **		e -- the envelope containing the message.
9889382Seric **
9899382Seric **	Returns:
9909382Seric **		none.
9919382Seric **
9929382Seric **	Side Effects:
9939382Seric **		outputs "p" to file "fp".
9949382Seric */
9959382Seric 
99665870Seric void
99765870Seric commaize(h, p, oldstyle, mci, e)
9989382Seric 	register HDR *h;
9999382Seric 	register char *p;
10009382Seric 	bool oldstyle;
100165870Seric 	register MCI *mci;
100255012Seric 	register ENVELOPE *e;
10039382Seric {
10049382Seric 	register char *obp;
10059382Seric 	int opos;
100666004Seric 	int omax;
10079382Seric 	bool firstone = TRUE;
100811157Seric 	char obuf[MAXLINE + 3];
10099382Seric 
10109382Seric 	/*
10119382Seric 	**  Output the address list translated by the
10129382Seric 	**  mailer and with commas.
10139382Seric 	*/
10149382Seric 
10159382Seric 	if (tTd(14, 2))
10169382Seric 		printf("commaize(%s: %s)\n", h->h_field, p);
10179382Seric 
10189382Seric 	obp = obuf;
101959579Seric 	(void) sprintf(obp, "%s: ", h->h_field);
10209382Seric 	opos = strlen(h->h_field) + 2;
10219382Seric 	obp += opos;
102266254Seric 	omax = mci->mci_mailer->m_linelimit - 2;
102366254Seric 	if (omax < 0 || omax > 78)
102466254Seric 		omax = 78;
10259382Seric 
10269382Seric 	/*
10279382Seric 	**  Run through the list of values.
10289382Seric 	*/
10299382Seric 
10309382Seric 	while (*p != '\0')
10319382Seric 	{
10329382Seric 		register char *name;
103354983Seric 		register int c;
10349382Seric 		char savechar;
103559163Seric 		int flags;
103659163Seric 		auto int stat;
10379382Seric 
10389382Seric 		/*
10399382Seric 		**  Find the end of the name.  New style names
10409382Seric 		**  end with a comma, old style names end with
10419382Seric 		**  a space character.  However, spaces do not
10429382Seric 		**  necessarily delimit an old-style name -- at
10439382Seric 		**  signs mean keep going.
10449382Seric 		*/
10459382Seric 
10469382Seric 		/* find end of name */
104758050Seric 		while ((isascii(*p) && isspace(*p)) || *p == ',')
10489382Seric 			p++;
10499382Seric 		name = p;
10509382Seric 		for (;;)
10519382Seric 		{
105258333Seric 			auto char *oldp;
105316909Seric 			char pvpbuf[PSBUFSIZE];
10549382Seric 
105565066Seric 			(void) prescan(p, oldstyle ? ' ' : ',', pvpbuf,
105665066Seric 				       sizeof pvpbuf, &oldp);
105758333Seric 			p = oldp;
10589382Seric 
10599382Seric 			/* look to see if we have an at sign */
106058050Seric 			while (*p != '\0' && isascii(*p) && isspace(*p))
10619382Seric 				p++;
10629382Seric 
106358170Seric 			if (*p != '@')
10649382Seric 			{
10659382Seric 				p = oldp;
10669382Seric 				break;
10679382Seric 			}
10689382Seric 			p += *p == '@' ? 1 : 2;
106958050Seric 			while (*p != '\0' && isascii(*p) && isspace(*p))
10709382Seric 				p++;
10719382Seric 		}
10729382Seric 		/* at the end of one complete name */
10739382Seric 
10749382Seric 		/* strip off trailing white space */
107558050Seric 		while (p >= name &&
107658050Seric 		       ((isascii(*p) && isspace(*p)) || *p == ',' || *p == '\0'))
10779382Seric 			p--;
10789382Seric 		if (++p == name)
10799382Seric 			continue;
10809382Seric 		savechar = *p;
10819382Seric 		*p = '\0';
10829382Seric 
10839382Seric 		/* translate the name to be relative */
108459163Seric 		flags = RF_HEADERADDR|RF_ADDDOMAIN;
108559163Seric 		if (bitset(H_FROM, h->h_flags))
108659163Seric 			flags |= RF_SENDERADDR;
108759163Seric 		stat = EX_OK;
108865870Seric 		name = remotename(name, mci->mci_mailer, flags, &stat, e);
10899382Seric 		if (*name == '\0')
10909382Seric 		{
10919382Seric 			*p = savechar;
10929382Seric 			continue;
10939382Seric 		}
10949382Seric 
10959382Seric 		/* output the name with nice formatting */
109654983Seric 		opos += strlen(name);
10979382Seric 		if (!firstone)
10989382Seric 			opos += 2;
109966004Seric 		if (opos > omax && !firstone)
11009382Seric 		{
110110178Seric 			(void) strcpy(obp, ",\n");
110265870Seric 			putline(obuf, mci);
11039382Seric 			obp = obuf;
110466255Seric 			(void) strcpy(obp, "        ");
110510161Seric 			opos = strlen(obp);
110610161Seric 			obp += opos;
110754983Seric 			opos += strlen(name);
11089382Seric 		}
11099382Seric 		else if (!firstone)
11109382Seric 		{
111166255Seric 			(void) strcpy(obp, ", ");
11129382Seric 			obp += 2;
11139382Seric 		}
11149382Seric 
111554983Seric 		while ((c = *name++) != '\0' && obp < &obuf[MAXLINE])
111654983Seric 			*obp++ = c;
11179382Seric 		firstone = FALSE;
11189382Seric 		*p = savechar;
11199382Seric 	}
11209382Seric 	(void) strcpy(obp, "\n");
112165870Seric 	putline(obuf, mci);
11229382Seric }
11239382Seric /*
112458170Seric **  COPYHEADER -- copy header list
11259382Seric **
112658170Seric **	This routine is the equivalent of newstr for header lists
112758170Seric **
11289382Seric **	Parameters:
112958170Seric **		header -- list of header structures to copy.
11309382Seric **
11319382Seric **	Returns:
113258170Seric **		a copy of 'header'.
11339382Seric **
11349382Seric **	Side Effects:
11359382Seric **		none.
11369382Seric */
11379382Seric 
113858170Seric HDR *
113958170Seric copyheader(header)
114058170Seric 	register HDR *header;
11419382Seric {
114258170Seric 	register HDR *newhdr;
114358170Seric 	HDR *ret;
114458170Seric 	register HDR **tail = &ret;
11459382Seric 
114658170Seric 	while (header != NULL)
114758170Seric 	{
114858170Seric 		newhdr = (HDR *) xalloc(sizeof(HDR));
114958170Seric 		STRUCTCOPY(*header, *newhdr);
115058170Seric 		*tail = newhdr;
115158170Seric 		tail = &newhdr->h_link;
115258170Seric 		header = header->h_link;
115358170Seric 	}
115458170Seric 	*tail = NULL;
115558170Seric 
115658170Seric 	return ret;
11579382Seric }
1158