[comp.mail.sendmail] mangled headers containing brackets within comments

e07@nikhefh.nikhef.nl (Eric Wassenaar) (05/06/91)

Description:
	sendmail mangles mailheaders which have brackets embedded
	in quoted strings or in parenthesized comments such as
		From: "formerly <user@old>" <user@new>
		To: user@new (formerly <user@old>)

Analysis:
	The problem is within crackaddr() in headers.c which (too
	simply-minded) tries to locate an opening bracket with
		p = index(addr, '<');
		if (p != NULL)
	in the plain address string, disregarding any comments.

Suggested fix:
	Below is an attempt for a routine to do a fancier search.
	Change the two lines mentioned above into
		p = find_delim(addr, '<');
		if (*p != '\0')
	Change the entire for-loop to locate the matching bracket to
		p = find_delim(addr, '>');

/*
**  FIND_DELIM - Find the position of a delimiter in an address.
**
**	It will search for an unquoted delimiter outside comments.
**	Used by crackaddr() to locate brackets in addresses like:
**
**		"comment" <address (comment)>
**
**	Parameters:
**		addr -- the address to be cracked.
**		delim -- the desired delimiter.
**
**	Returns:
**		pointer to delim in addr if found,
**		pointer to null byte if not found.
*/

char *
find_delim(addr, delim)
	char *addr;
	char delim;
{
	bool backslash = FALSE;
	bool quoted_string = FALSE;
	int comment = 0;
	int bracket = 0;
	register char *p;

	/* strategy a la prescan() */
	for (p = addr; *p != '\0'; p++)
	{
		if (backslash)
			backslash = FALSE;
		else if (*p == '\\')
			backslash = TRUE;
		else if (quoted_string && *p == '"')
			quoted_string = FALSE;
		else if (quoted_string)
			continue;
		else if (*p == delim && comment <= 0 && bracket <= 0)
			break;
		else if (*p == '(')
			comment++;
		else if (*p == ')')
			comment--;
		else if (comment > 0)
			continue;
		else if (*p == '<')
			bracket++;
		else if (*p == '>')
			bracket--;
		else if (*p == '"')
			quoted_string = TRUE;
	}

	/* p points to the delimiter, or to a null byte */
	return (p);
}

Eric Wassenaar
-- 
Organization: NIKHEF-H, National Institute for Nuclear and High-Energy Physics
Address: Kruislaan 409, P.O. Box 41882, 1009 DB Amsterdam, the Netherlands
Phone: +31 20 592 0412, Home: +31 20 6909449, Telefax: +31 20 592 5155
Internet: e07@nikhef.nl