xref: /illumos-gate/usr/src/cmd/sendmail/src/util.c (revision e9af4bc0)
17c478bd9Sstevel@tonic-gate /*
2*e9af4bc0SJohn Beck  * Copyright (c) 1998-2007, 2009 Sendmail, Inc. and its suppliers.
37c478bd9Sstevel@tonic-gate  *	All rights reserved.
47c478bd9Sstevel@tonic-gate  * Copyright (c) 1983, 1995-1997 Eric P. Allman.  All rights reserved.
57c478bd9Sstevel@tonic-gate  * Copyright (c) 1988, 1993
67c478bd9Sstevel@tonic-gate  *	The Regents of the University of California.  All rights reserved.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * By using this file, you agree to the terms and conditions set
97c478bd9Sstevel@tonic-gate  * forth in the LICENSE file which can be found at the top level of
107c478bd9Sstevel@tonic-gate  * the sendmail distribution.
117c478bd9Sstevel@tonic-gate  *
127c478bd9Sstevel@tonic-gate  */
137c478bd9Sstevel@tonic-gate 
147c478bd9Sstevel@tonic-gate #include <sendmail.h>
157c478bd9Sstevel@tonic-gate 
16*e9af4bc0SJohn Beck SM_RCSID("@(#)$Id: util.c,v 8.416 2009/12/18 17:05:26 ca Exp $")
177c478bd9Sstevel@tonic-gate 
18058561cbSjbeck #include <sm/sendmail.h>
197c478bd9Sstevel@tonic-gate #include <sysexits.h>
207c478bd9Sstevel@tonic-gate #include <sm/xtrap.h>
217c478bd9Sstevel@tonic-gate 
227c478bd9Sstevel@tonic-gate /*
237c478bd9Sstevel@tonic-gate **  NEWSTR -- Create a copy of a C string
247c478bd9Sstevel@tonic-gate **
257c478bd9Sstevel@tonic-gate **	Parameters:
267c478bd9Sstevel@tonic-gate **		s -- the string to copy.
277c478bd9Sstevel@tonic-gate **
287c478bd9Sstevel@tonic-gate **	Returns:
297c478bd9Sstevel@tonic-gate **		pointer to newly allocated string.
307c478bd9Sstevel@tonic-gate */
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate char *
337c478bd9Sstevel@tonic-gate newstr(s)
347c478bd9Sstevel@tonic-gate 	const char *s;
357c478bd9Sstevel@tonic-gate {
367c478bd9Sstevel@tonic-gate 	size_t l;
377c478bd9Sstevel@tonic-gate 	char *n;
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate 	l = strlen(s);
407c478bd9Sstevel@tonic-gate 	SM_ASSERT(l + 1 > l);
417c478bd9Sstevel@tonic-gate 	n = xalloc(l + 1);
427c478bd9Sstevel@tonic-gate 	sm_strlcpy(n, s, l + 1);
437c478bd9Sstevel@tonic-gate 	return n;
447c478bd9Sstevel@tonic-gate }
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate /*
477c478bd9Sstevel@tonic-gate **  ADDQUOTES -- Adds quotes & quote bits to a string.
487c478bd9Sstevel@tonic-gate **
497c478bd9Sstevel@tonic-gate **	Runs through a string and adds backslashes and quote bits.
507c478bd9Sstevel@tonic-gate **
517c478bd9Sstevel@tonic-gate **	Parameters:
527c478bd9Sstevel@tonic-gate **		s -- the string to modify.
537c478bd9Sstevel@tonic-gate **		rpool -- resource pool from which to allocate result
547c478bd9Sstevel@tonic-gate **
557c478bd9Sstevel@tonic-gate **	Returns:
567c478bd9Sstevel@tonic-gate **		pointer to quoted string.
577c478bd9Sstevel@tonic-gate */
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate char *
addquotes(s,rpool)607c478bd9Sstevel@tonic-gate addquotes(s, rpool)
617c478bd9Sstevel@tonic-gate 	char *s;
627c478bd9Sstevel@tonic-gate 	SM_RPOOL_T *rpool;
637c478bd9Sstevel@tonic-gate {
647c478bd9Sstevel@tonic-gate 	int len = 0;
657c478bd9Sstevel@tonic-gate 	char c;
667c478bd9Sstevel@tonic-gate 	char *p = s, *q, *r;
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate 	if (s == NULL)
697c478bd9Sstevel@tonic-gate 		return NULL;
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate 	/* Find length of quoted string */
727c478bd9Sstevel@tonic-gate 	while ((c = *p++) != '\0')
737c478bd9Sstevel@tonic-gate 	{
747c478bd9Sstevel@tonic-gate 		len++;
757c478bd9Sstevel@tonic-gate 		if (c == '\\' || c == '"')
767c478bd9Sstevel@tonic-gate 			len++;
777c478bd9Sstevel@tonic-gate 	}
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate 	q = r = sm_rpool_malloc_x(rpool, len + 3);
807c478bd9Sstevel@tonic-gate 	p = s;
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate 	/* add leading quote */
837c478bd9Sstevel@tonic-gate 	*q++ = '"';
847c478bd9Sstevel@tonic-gate 	while ((c = *p++) != '\0')
857c478bd9Sstevel@tonic-gate 	{
867c478bd9Sstevel@tonic-gate 		/* quote \ or " */
877c478bd9Sstevel@tonic-gate 		if (c == '\\' || c == '"')
887c478bd9Sstevel@tonic-gate 			*q++ = '\\';
897c478bd9Sstevel@tonic-gate 		*q++ = c;
907c478bd9Sstevel@tonic-gate 	}
917c478bd9Sstevel@tonic-gate 	*q++ = '"';
927c478bd9Sstevel@tonic-gate 	*q = '\0';
937c478bd9Sstevel@tonic-gate 	return r;
947c478bd9Sstevel@tonic-gate }
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate /*
977c478bd9Sstevel@tonic-gate **  STRIPBACKSLASH -- Strip all leading backslashes from a string, provided
987c478bd9Sstevel@tonic-gate **	the following character is alpha-numerical.
997c478bd9Sstevel@tonic-gate **
1007c478bd9Sstevel@tonic-gate **	This is done in place.
1017c478bd9Sstevel@tonic-gate **
1027c478bd9Sstevel@tonic-gate **	Parameters:
1037c478bd9Sstevel@tonic-gate **		s -- the string to strip.
1047c478bd9Sstevel@tonic-gate **
1057c478bd9Sstevel@tonic-gate **	Returns:
1067c478bd9Sstevel@tonic-gate **		none.
1077c478bd9Sstevel@tonic-gate */
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate void
stripbackslash(s)1107c478bd9Sstevel@tonic-gate stripbackslash(s)
1117c478bd9Sstevel@tonic-gate 	char *s;
1127c478bd9Sstevel@tonic-gate {
1137c478bd9Sstevel@tonic-gate 	char *p, *q, c;
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate 	if (s == NULL || *s == '\0')
1167c478bd9Sstevel@tonic-gate 		return;
1177c478bd9Sstevel@tonic-gate 	p = q = s;
1187c478bd9Sstevel@tonic-gate 	while (*p == '\\' && (p[1] == '\\' || (isascii(p[1]) && isalnum(p[1]))))
1197c478bd9Sstevel@tonic-gate 		p++;
1207c478bd9Sstevel@tonic-gate 	do
1217c478bd9Sstevel@tonic-gate 	{
1227c478bd9Sstevel@tonic-gate 		c = *q++ = *p++;
1237c478bd9Sstevel@tonic-gate 	} while (c != '\0');
1247c478bd9Sstevel@tonic-gate }
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate /*
1277c478bd9Sstevel@tonic-gate **  RFC822_STRING -- Checks string for proper RFC822 string quoting.
1287c478bd9Sstevel@tonic-gate **
1297c478bd9Sstevel@tonic-gate **	Runs through a string and verifies RFC822 special characters
1307c478bd9Sstevel@tonic-gate **	are only found inside comments, quoted strings, or backslash
1317c478bd9Sstevel@tonic-gate **	escaped.  Also verified balanced quotes and parenthesis.
1327c478bd9Sstevel@tonic-gate **
1337c478bd9Sstevel@tonic-gate **	Parameters:
1347c478bd9Sstevel@tonic-gate **		s -- the string to modify.
1357c478bd9Sstevel@tonic-gate **
1367c478bd9Sstevel@tonic-gate **	Returns:
1377c478bd9Sstevel@tonic-gate **		true iff the string is RFC822 compliant, false otherwise.
1387c478bd9Sstevel@tonic-gate */
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate bool
rfc822_string(s)1417c478bd9Sstevel@tonic-gate rfc822_string(s)
1427c478bd9Sstevel@tonic-gate 	char *s;
1437c478bd9Sstevel@tonic-gate {
1447c478bd9Sstevel@tonic-gate 	bool quoted = false;
1457c478bd9Sstevel@tonic-gate 	int commentlev = 0;
1467c478bd9Sstevel@tonic-gate 	char *c = s;
1477c478bd9Sstevel@tonic-gate 
1487c478bd9Sstevel@tonic-gate 	if (s == NULL)
1497c478bd9Sstevel@tonic-gate 		return false;
1507c478bd9Sstevel@tonic-gate 
1517c478bd9Sstevel@tonic-gate 	while (*c != '\0')
1527c478bd9Sstevel@tonic-gate 	{
1537c478bd9Sstevel@tonic-gate 		/* escaped character */
1547c478bd9Sstevel@tonic-gate 		if (*c == '\\')
1557c478bd9Sstevel@tonic-gate 		{
1567c478bd9Sstevel@tonic-gate 			c++;
1577c478bd9Sstevel@tonic-gate 			if (*c == '\0')
1587c478bd9Sstevel@tonic-gate 				return false;
1597c478bd9Sstevel@tonic-gate 		}
1607c478bd9Sstevel@tonic-gate 		else if (commentlev == 0 && *c == '"')
1617c478bd9Sstevel@tonic-gate 			quoted = !quoted;
1627c478bd9Sstevel@tonic-gate 		else if (!quoted)
1637c478bd9Sstevel@tonic-gate 		{
1647c478bd9Sstevel@tonic-gate 			if (*c == ')')
1657c478bd9Sstevel@tonic-gate 			{
1667c478bd9Sstevel@tonic-gate 				/* unbalanced ')' */
1677c478bd9Sstevel@tonic-gate 				if (commentlev == 0)
1687c478bd9Sstevel@tonic-gate 					return false;
1697c478bd9Sstevel@tonic-gate 				else
1707c478bd9Sstevel@tonic-gate 					commentlev--;
1717c478bd9Sstevel@tonic-gate 			}
1727c478bd9Sstevel@tonic-gate 			else if (*c == '(')
1737c478bd9Sstevel@tonic-gate 				commentlev++;
1747c478bd9Sstevel@tonic-gate 			else if (commentlev == 0 &&
1757c478bd9Sstevel@tonic-gate 				 strchr(MustQuoteChars, *c) != NULL)
1767c478bd9Sstevel@tonic-gate 				return false;
1777c478bd9Sstevel@tonic-gate 		}
1787c478bd9Sstevel@tonic-gate 		c++;
1797c478bd9Sstevel@tonic-gate 	}
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate 	/* unbalanced '"' or '(' */
1827c478bd9Sstevel@tonic-gate 	return !quoted && commentlev == 0;
1837c478bd9Sstevel@tonic-gate }
184058561cbSjbeck 
1857c478bd9Sstevel@tonic-gate /*
1867c478bd9Sstevel@tonic-gate **  SHORTEN_RFC822_STRING -- Truncate and rebalance an RFC822 string
1877c478bd9Sstevel@tonic-gate **
1887c478bd9Sstevel@tonic-gate **	Arbitrarily shorten (in place) an RFC822 string and rebalance
1897c478bd9Sstevel@tonic-gate **	comments and quotes.
1907c478bd9Sstevel@tonic-gate **
1917c478bd9Sstevel@tonic-gate **	Parameters:
1927c478bd9Sstevel@tonic-gate **		string -- the string to shorten
1937c478bd9Sstevel@tonic-gate **		length -- the maximum size, 0 if no maximum
1947c478bd9Sstevel@tonic-gate **
1957c478bd9Sstevel@tonic-gate **	Returns:
1967c478bd9Sstevel@tonic-gate **		true if string is changed, false otherwise
1977c478bd9Sstevel@tonic-gate **
1987c478bd9Sstevel@tonic-gate **	Side Effects:
1997c478bd9Sstevel@tonic-gate **		Changes string in place, possibly resulting
2007c478bd9Sstevel@tonic-gate **		in a shorter string.
2017c478bd9Sstevel@tonic-gate */
2027c478bd9Sstevel@tonic-gate 
2037c478bd9Sstevel@tonic-gate bool
shorten_rfc822_string(string,length)2047c478bd9Sstevel@tonic-gate shorten_rfc822_string(string, length)
2057c478bd9Sstevel@tonic-gate 	char *string;
2067c478bd9Sstevel@tonic-gate 	size_t length;
2077c478bd9Sstevel@tonic-gate {
2087c478bd9Sstevel@tonic-gate 	bool backslash = false;
2097c478bd9Sstevel@tonic-gate 	bool modified = false;
2107c478bd9Sstevel@tonic-gate 	bool quoted = false;
2117c478bd9Sstevel@tonic-gate 	size_t slen;
2127c478bd9Sstevel@tonic-gate 	int parencount = 0;
2137c478bd9Sstevel@tonic-gate 	char *ptr = string;
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate 	/*
2167c478bd9Sstevel@tonic-gate 	**  If have to rebalance an already short enough string,
2177c478bd9Sstevel@tonic-gate 	**  need to do it within allocated space.
2187c478bd9Sstevel@tonic-gate 	*/
2197c478bd9Sstevel@tonic-gate 
2207c478bd9Sstevel@tonic-gate 	slen = strlen(string);
2217c478bd9Sstevel@tonic-gate 	if (length == 0 || slen < length)
2227c478bd9Sstevel@tonic-gate 		length = slen;
2237c478bd9Sstevel@tonic-gate 
2247c478bd9Sstevel@tonic-gate 	while (*ptr != '\0')
2257c478bd9Sstevel@tonic-gate 	{
2267c478bd9Sstevel@tonic-gate 		if (backslash)
2277c478bd9Sstevel@tonic-gate 		{
2287c478bd9Sstevel@tonic-gate 			backslash = false;
2297c478bd9Sstevel@tonic-gate 			goto increment;
2307c478bd9Sstevel@tonic-gate 		}
2317c478bd9Sstevel@tonic-gate 
2327c478bd9Sstevel@tonic-gate 		if (*ptr == '\\')
2337c478bd9Sstevel@tonic-gate 			backslash = true;
2347c478bd9Sstevel@tonic-gate 		else if (*ptr == '(')
2357c478bd9Sstevel@tonic-gate 		{
2367c478bd9Sstevel@tonic-gate 			if (!quoted)
2377c478bd9Sstevel@tonic-gate 				parencount++;
2387c478bd9Sstevel@tonic-gate 		}
2397c478bd9Sstevel@tonic-gate 		else if (*ptr == ')')
2407c478bd9Sstevel@tonic-gate 		{
2417c478bd9Sstevel@tonic-gate 			if (--parencount < 0)
2427c478bd9Sstevel@tonic-gate 				parencount = 0;
2437c478bd9Sstevel@tonic-gate 		}
2447c478bd9Sstevel@tonic-gate 
2457c478bd9Sstevel@tonic-gate 		/* Inside a comment, quotes don't matter */
2467c478bd9Sstevel@tonic-gate 		if (parencount <= 0 && *ptr == '"')
2477c478bd9Sstevel@tonic-gate 			quoted = !quoted;
2487c478bd9Sstevel@tonic-gate 
2497c478bd9Sstevel@tonic-gate increment:
2507c478bd9Sstevel@tonic-gate 		/* Check for sufficient space for next character */
2517c478bd9Sstevel@tonic-gate 		if (length - (ptr - string) <= (size_t) ((backslash ? 1 : 0) +
2527c478bd9Sstevel@tonic-gate 						parencount +
2537c478bd9Sstevel@tonic-gate 						(quoted ? 1 : 0)))
2547c478bd9Sstevel@tonic-gate 		{
2557c478bd9Sstevel@tonic-gate 			/* Not enough, backtrack */
2567c478bd9Sstevel@tonic-gate 			if (*ptr == '\\')
2577c478bd9Sstevel@tonic-gate 				backslash = false;
2587c478bd9Sstevel@tonic-gate 			else if (*ptr == '(' && !quoted)
2597c478bd9Sstevel@tonic-gate 				parencount--;
2607c478bd9Sstevel@tonic-gate 			else if (*ptr == '"' && parencount == 0)
2617c478bd9Sstevel@tonic-gate 				quoted = false;
2627c478bd9Sstevel@tonic-gate 			break;
2637c478bd9Sstevel@tonic-gate 		}
2647c478bd9Sstevel@tonic-gate 		ptr++;
2657c478bd9Sstevel@tonic-gate 	}
2667c478bd9Sstevel@tonic-gate 
2677c478bd9Sstevel@tonic-gate 	/* Rebalance */
2687c478bd9Sstevel@tonic-gate 	while (parencount-- > 0)
2697c478bd9Sstevel@tonic-gate 	{
2707c478bd9Sstevel@tonic-gate 		if (*ptr != ')')
2717c478bd9Sstevel@tonic-gate 		{
2727c478bd9Sstevel@tonic-gate 			modified = true;
2737c478bd9Sstevel@tonic-gate 			*ptr = ')';
2747c478bd9Sstevel@tonic-gate 		}
2757c478bd9Sstevel@tonic-gate 		ptr++;
2767c478bd9Sstevel@tonic-gate 	}
2777c478bd9Sstevel@tonic-gate 	if (quoted)
2787c478bd9Sstevel@tonic-gate 	{
2797c478bd9Sstevel@tonic-gate 		if (*ptr != '"')
2807c478bd9Sstevel@tonic-gate 		{
2817c478bd9Sstevel@tonic-gate 			modified = true;
2827c478bd9Sstevel@tonic-gate 			*ptr = '"';
2837c478bd9Sstevel@tonic-gate 		}
2847c478bd9Sstevel@tonic-gate 		ptr++;
2857c478bd9Sstevel@tonic-gate 	}
2867c478bd9Sstevel@tonic-gate 	if (*ptr != '\0')
2877c478bd9Sstevel@tonic-gate 	{
2887c478bd9Sstevel@tonic-gate 		modified = true;
2897c478bd9Sstevel@tonic-gate 		*ptr = '\0';
2907c478bd9Sstevel@tonic-gate 	}
2917c478bd9Sstevel@tonic-gate 	return modified;
2927c478bd9Sstevel@tonic-gate }
293058561cbSjbeck 
2947c478bd9Sstevel@tonic-gate /*
2957c478bd9Sstevel@tonic-gate **  FIND_CHARACTER -- find an unquoted character in an RFC822 string
2967c478bd9Sstevel@tonic-gate **
2977c478bd9Sstevel@tonic-gate **	Find an unquoted, non-commented character in an RFC822
2987c478bd9Sstevel@tonic-gate **	string and return a pointer to its location in the
2997c478bd9Sstevel@tonic-gate **	string.
3007c478bd9Sstevel@tonic-gate **
3017c478bd9Sstevel@tonic-gate **	Parameters:
3027c478bd9Sstevel@tonic-gate **		string -- the string to search
3037c478bd9Sstevel@tonic-gate **		character -- the character to find
3047c478bd9Sstevel@tonic-gate **
3057c478bd9Sstevel@tonic-gate **	Returns:
3067c478bd9Sstevel@tonic-gate **		pointer to the character, or
3077c478bd9Sstevel@tonic-gate **		a pointer to the end of the line if character is not found
3087c478bd9Sstevel@tonic-gate */
3097c478bd9Sstevel@tonic-gate 
3107c478bd9Sstevel@tonic-gate char *
find_character(string,character)3117c478bd9Sstevel@tonic-gate find_character(string, character)
3127c478bd9Sstevel@tonic-gate 	char *string;
3137c478bd9Sstevel@tonic-gate 	int character;
3147c478bd9Sstevel@tonic-gate {
3157c478bd9Sstevel@tonic-gate 	bool backslash = false;
3167c478bd9Sstevel@tonic-gate 	bool quoted = false;
3177c478bd9Sstevel@tonic-gate 	int parencount = 0;
3187c478bd9Sstevel@tonic-gate 
3197c478bd9Sstevel@tonic-gate 	while (string != NULL && *string != '\0')
3207c478bd9Sstevel@tonic-gate 	{
3217c478bd9Sstevel@tonic-gate 		if (backslash)
3227c478bd9Sstevel@tonic-gate 		{
3237c478bd9Sstevel@tonic-gate 			backslash = false;
3247c478bd9Sstevel@tonic-gate 			if (!quoted && character == '\\' && *string == '\\')
3257c478bd9Sstevel@tonic-gate 				break;
3267c478bd9Sstevel@tonic-gate 			string++;
3277c478bd9Sstevel@tonic-gate 			continue;
3287c478bd9Sstevel@tonic-gate 		}
3297c478bd9Sstevel@tonic-gate 		switch (*string)
3307c478bd9Sstevel@tonic-gate 		{
3317c478bd9Sstevel@tonic-gate 		  case '\\':
3327c478bd9Sstevel@tonic-gate 			backslash = true;
3337c478bd9Sstevel@tonic-gate 			break;
3347c478bd9Sstevel@tonic-gate 
3357c478bd9Sstevel@tonic-gate 		  case '(':
3367c478bd9Sstevel@tonic-gate 			if (!quoted)
3377c478bd9Sstevel@tonic-gate 				parencount++;
3387c478bd9Sstevel@tonic-gate 			break;
3397c478bd9Sstevel@tonic-gate 
3407c478bd9Sstevel@tonic-gate 		  case ')':
3417c478bd9Sstevel@tonic-gate 			if (--parencount < 0)
3427c478bd9Sstevel@tonic-gate 				parencount = 0;
3437c478bd9Sstevel@tonic-gate 			break;
3447c478bd9Sstevel@tonic-gate 		}
3457c478bd9Sstevel@tonic-gate 
3467c478bd9Sstevel@tonic-gate 		/* Inside a comment, nothing matters */
3477c478bd9Sstevel@tonic-gate 		if (parencount > 0)
3487c478bd9Sstevel@tonic-gate 		{
3497c478bd9Sstevel@tonic-gate 			string++;
3507c478bd9Sstevel@tonic-gate 			continue;
3517c478bd9Sstevel@tonic-gate 		}
3527c478bd9Sstevel@tonic-gate 
3537c478bd9Sstevel@tonic-gate 		if (*string == '"')
3547c478bd9Sstevel@tonic-gate 			quoted = !quoted;
3557c478bd9Sstevel@tonic-gate 		else if (*string == character && !quoted)
3567c478bd9Sstevel@tonic-gate 			break;
3577c478bd9Sstevel@tonic-gate 		string++;
3587c478bd9Sstevel@tonic-gate 	}
3597c478bd9Sstevel@tonic-gate 
3607c478bd9Sstevel@tonic-gate 	/* Return pointer to the character */
3617c478bd9Sstevel@tonic-gate 	return string;
3627c478bd9Sstevel@tonic-gate }
3637c478bd9Sstevel@tonic-gate 
3647c478bd9Sstevel@tonic-gate /*
3657c478bd9Sstevel@tonic-gate **  CHECK_BODYTYPE -- check bodytype parameter
3667c478bd9Sstevel@tonic-gate **
3677c478bd9Sstevel@tonic-gate **	Parameters:
3687c478bd9Sstevel@tonic-gate **		bodytype -- bodytype parameter
3697c478bd9Sstevel@tonic-gate **
3707c478bd9Sstevel@tonic-gate **	Returns:
3717c478bd9Sstevel@tonic-gate **		BODYTYPE_* according to parameter
3727c478bd9Sstevel@tonic-gate **
3737c478bd9Sstevel@tonic-gate */
3747c478bd9Sstevel@tonic-gate 
3757c478bd9Sstevel@tonic-gate int
check_bodytype(bodytype)3767c478bd9Sstevel@tonic-gate check_bodytype(bodytype)
3777c478bd9Sstevel@tonic-gate 	char *bodytype;
3787c478bd9Sstevel@tonic-gate {
3797c478bd9Sstevel@tonic-gate 	/* check body type for legality */
3807c478bd9Sstevel@tonic-gate 	if (bodytype == NULL)
3817c478bd9Sstevel@tonic-gate 		return BODYTYPE_NONE;
3827c478bd9Sstevel@tonic-gate 	if (sm_strcasecmp(bodytype, "7BIT") == 0)
3837c478bd9Sstevel@tonic-gate 		return BODYTYPE_7BIT;
3847c478bd9Sstevel@tonic-gate 	if (sm_strcasecmp(bodytype, "8BITMIME") == 0)
3857c478bd9Sstevel@tonic-gate 		return BODYTYPE_8BITMIME;
3867c478bd9Sstevel@tonic-gate 	return BODYTYPE_ILLEGAL;
3877c478bd9Sstevel@tonic-gate }
3887c478bd9Sstevel@tonic-gate 
3897c478bd9Sstevel@tonic-gate /*
3907c478bd9Sstevel@tonic-gate **  TRUNCATE_AT_DELIM -- truncate string at a delimiter and append "..."
3917c478bd9Sstevel@tonic-gate **
3927c478bd9Sstevel@tonic-gate **	Parameters:
3937c478bd9Sstevel@tonic-gate **		str -- string to truncate
3947c478bd9Sstevel@tonic-gate **		len -- maximum length (including '\0') (0 for unlimited)
3957c478bd9Sstevel@tonic-gate **		delim -- delimiter character
3967c478bd9Sstevel@tonic-gate **
3977c478bd9Sstevel@tonic-gate **	Returns:
3987c478bd9Sstevel@tonic-gate **		None.
3997c478bd9Sstevel@tonic-gate */
4007c478bd9Sstevel@tonic-gate 
4017c478bd9Sstevel@tonic-gate void
truncate_at_delim(str,len,delim)4027c478bd9Sstevel@tonic-gate truncate_at_delim(str, len, delim)
4037c478bd9Sstevel@tonic-gate 	char *str;
4047c478bd9Sstevel@tonic-gate 	size_t len;
4057c478bd9Sstevel@tonic-gate 	int delim;
4067c478bd9Sstevel@tonic-gate {
4077c478bd9Sstevel@tonic-gate 	char *p;
4087c478bd9Sstevel@tonic-gate 
4097c478bd9Sstevel@tonic-gate 	if (str == NULL || len == 0 || strlen(str) < len)
4107c478bd9Sstevel@tonic-gate 		return;
4117c478bd9Sstevel@tonic-gate 
4127c478bd9Sstevel@tonic-gate 	*(str + len - 1) = '\0';
4137c478bd9Sstevel@tonic-gate 	while ((p = strrchr(str, delim)) != NULL)
4147c478bd9Sstevel@tonic-gate 	{
4157c478bd9Sstevel@tonic-gate 		*p = '\0';
4167c478bd9Sstevel@tonic-gate 		if (p - str + 4 < len)
4177c478bd9Sstevel@tonic-gate 		{
4187c478bd9Sstevel@tonic-gate 			*p++ = (char) delim;
4197c478bd9Sstevel@tonic-gate 			*p = '\0';
4207c478bd9Sstevel@tonic-gate 			(void) sm_strlcat(str, "...", len);
4217c478bd9Sstevel@tonic-gate 			return;
4227c478bd9Sstevel@tonic-gate 		}
4237c478bd9Sstevel@tonic-gate 	}
4247c478bd9Sstevel@tonic-gate 
4257c478bd9Sstevel@tonic-gate 	/* Couldn't find a place to append "..." */
4267c478bd9Sstevel@tonic-gate 	if (len > 3)
4277c478bd9Sstevel@tonic-gate 		(void) sm_strlcpy(str, "...", len);
4287c478bd9Sstevel@tonic-gate 	else
4297c478bd9Sstevel@tonic-gate 		str[0] = '\0';
4307c478bd9Sstevel@tonic-gate }
431058561cbSjbeck 
4327c478bd9Sstevel@tonic-gate /*
4337c478bd9Sstevel@tonic-gate **  XALLOC -- Allocate memory, raise an exception on error
4347c478bd9Sstevel@tonic-gate **
4357c478bd9Sstevel@tonic-gate **	Parameters:
4367c478bd9Sstevel@tonic-gate **		sz -- size of area to allocate.
4377c478bd9Sstevel@tonic-gate **
4387c478bd9Sstevel@tonic-gate **	Returns:
4397c478bd9Sstevel@tonic-gate **		pointer to data region.
4407c478bd9Sstevel@tonic-gate **
4417c478bd9Sstevel@tonic-gate **	Exceptions:
4427c478bd9Sstevel@tonic-gate **		SmHeapOutOfMemory (F:sm.heap) -- cannot allocate memory
4437c478bd9Sstevel@tonic-gate **
4447c478bd9Sstevel@tonic-gate **	Side Effects:
4457c478bd9Sstevel@tonic-gate **		Memory is allocated.
4467c478bd9Sstevel@tonic-gate */
4477c478bd9Sstevel@tonic-gate 
4487c478bd9Sstevel@tonic-gate char *
4497c478bd9Sstevel@tonic-gate #if SM_HEAP_CHECK
xalloc_tagged(sz,file,line)4507c478bd9Sstevel@tonic-gate xalloc_tagged(sz, file, line)
4517c478bd9Sstevel@tonic-gate 	register int sz;
4527c478bd9Sstevel@tonic-gate 	char *file;
4537c478bd9Sstevel@tonic-gate 	int line;
4547c478bd9Sstevel@tonic-gate #else /* SM_HEAP_CHECK */
4557c478bd9Sstevel@tonic-gate xalloc(sz)
4567c478bd9Sstevel@tonic-gate 	register int sz;
4577c478bd9Sstevel@tonic-gate #endif /* SM_HEAP_CHECK */
4587c478bd9Sstevel@tonic-gate {
4597c478bd9Sstevel@tonic-gate 	register char *p;
4607c478bd9Sstevel@tonic-gate 
461445f2479Sjbeck 	SM_REQUIRE(sz >= 0);
462445f2479Sjbeck 
4637c478bd9Sstevel@tonic-gate 	/* some systems can't handle size zero mallocs */
4647c478bd9Sstevel@tonic-gate 	if (sz <= 0)
4657c478bd9Sstevel@tonic-gate 		sz = 1;
4667c478bd9Sstevel@tonic-gate 
4677c478bd9Sstevel@tonic-gate 	/* scaffolding for testing error handling code */
4687c478bd9Sstevel@tonic-gate 	sm_xtrap_raise_x(&SmHeapOutOfMemory);
4697c478bd9Sstevel@tonic-gate 
4707c478bd9Sstevel@tonic-gate 	p = sm_malloc_tagged((unsigned) sz, file, line, sm_heap_group());
4717c478bd9Sstevel@tonic-gate 	if (p == NULL)
4727c478bd9Sstevel@tonic-gate 	{
4737c478bd9Sstevel@tonic-gate 		sm_exc_raise_x(&SmHeapOutOfMemory);
4747c478bd9Sstevel@tonic-gate 	}
4757c478bd9Sstevel@tonic-gate 	return p;
4767c478bd9Sstevel@tonic-gate }
477058561cbSjbeck 
4787c478bd9Sstevel@tonic-gate /*
4797c478bd9Sstevel@tonic-gate **  COPYPLIST -- copy list of pointers.
4807c478bd9Sstevel@tonic-gate **
4817c478bd9Sstevel@tonic-gate **	This routine is the equivalent of strdup for lists of
4827c478bd9Sstevel@tonic-gate **	pointers.
4837c478bd9Sstevel@tonic-gate **
4847c478bd9Sstevel@tonic-gate **	Parameters:
4857c478bd9Sstevel@tonic-gate **		list -- list of pointers to copy.
4867c478bd9Sstevel@tonic-gate **			Must be NULL terminated.
4877c478bd9Sstevel@tonic-gate **		copycont -- if true, copy the contents of the vector
4887c478bd9Sstevel@tonic-gate **			(which must be a string) also.
4897c478bd9Sstevel@tonic-gate **		rpool -- resource pool from which to allocate storage,
4907c478bd9Sstevel@tonic-gate **			or NULL
4917c478bd9Sstevel@tonic-gate **
4927c478bd9Sstevel@tonic-gate **	Returns:
4937c478bd9Sstevel@tonic-gate **		a copy of 'list'.
4947c478bd9Sstevel@tonic-gate */
4957c478bd9Sstevel@tonic-gate 
4967c478bd9Sstevel@tonic-gate char **
copyplist(list,copycont,rpool)4977c478bd9Sstevel@tonic-gate copyplist(list, copycont, rpool)
4987c478bd9Sstevel@tonic-gate 	char **list;
4997c478bd9Sstevel@tonic-gate 	bool copycont;
5007c478bd9Sstevel@tonic-gate 	SM_RPOOL_T *rpool;
5017c478bd9Sstevel@tonic-gate {
5027c478bd9Sstevel@tonic-gate 	register char **vp;
5037c478bd9Sstevel@tonic-gate 	register char **newvp;
5047c478bd9Sstevel@tonic-gate 
5057c478bd9Sstevel@tonic-gate 	for (vp = list; *vp != NULL; vp++)
5067c478bd9Sstevel@tonic-gate 		continue;
5077c478bd9Sstevel@tonic-gate 
5087c478bd9Sstevel@tonic-gate 	vp++;
5097c478bd9Sstevel@tonic-gate 
510058561cbSjbeck 	newvp = (char **) sm_rpool_malloc_x(rpool, (vp - list) * sizeof(*vp));
511058561cbSjbeck 	memmove((char *) newvp, (char *) list, (int) (vp - list) * sizeof(*vp));
5127c478bd9Sstevel@tonic-gate 
5137c478bd9Sstevel@tonic-gate 	if (copycont)
5147c478bd9Sstevel@tonic-gate 	{
5157c478bd9Sstevel@tonic-gate 		for (vp = newvp; *vp != NULL; vp++)
5167c478bd9Sstevel@tonic-gate 			*vp = sm_rpool_strdup_x(rpool, *vp);
5177c478bd9Sstevel@tonic-gate 	}
5187c478bd9Sstevel@tonic-gate 
5197c478bd9Sstevel@tonic-gate 	return newvp;
5207c478bd9Sstevel@tonic-gate }
521058561cbSjbeck 
5227c478bd9Sstevel@tonic-gate /*
5237c478bd9Sstevel@tonic-gate **  COPYQUEUE -- copy address queue.
5247c478bd9Sstevel@tonic-gate **
5257c478bd9Sstevel@tonic-gate **	This routine is the equivalent of strdup for address queues;
5267c478bd9Sstevel@tonic-gate **	addresses marked as QS_IS_DEAD() aren't copied
5277c478bd9Sstevel@tonic-gate **
5287c478bd9Sstevel@tonic-gate **	Parameters:
5297c478bd9Sstevel@tonic-gate **		addr -- list of address structures to copy.
5307c478bd9Sstevel@tonic-gate **		rpool -- resource pool from which to allocate storage
5317c478bd9Sstevel@tonic-gate **
5327c478bd9Sstevel@tonic-gate **	Returns:
5337c478bd9Sstevel@tonic-gate **		a copy of 'addr'.
5347c478bd9Sstevel@tonic-gate */
5357c478bd9Sstevel@tonic-gate 
5367c478bd9Sstevel@tonic-gate ADDRESS *
copyqueue(addr,rpool)5377c478bd9Sstevel@tonic-gate copyqueue(addr, rpool)
5387c478bd9Sstevel@tonic-gate 	ADDRESS *addr;
5397c478bd9Sstevel@tonic-gate 	SM_RPOOL_T *rpool;
5407c478bd9Sstevel@tonic-gate {
5417c478bd9Sstevel@tonic-gate 	register ADDRESS *newaddr;
5427c478bd9Sstevel@tonic-gate 	ADDRESS *ret;
5437c478bd9Sstevel@tonic-gate 	register ADDRESS **tail = &ret;
5447c478bd9Sstevel@tonic-gate 
5457c478bd9Sstevel@tonic-gate 	while (addr != NULL)
5467c478bd9Sstevel@tonic-gate 	{
5477c478bd9Sstevel@tonic-gate 		if (!QS_IS_DEAD(addr->q_state))
5487c478bd9Sstevel@tonic-gate 		{
5497c478bd9Sstevel@tonic-gate 			newaddr = (ADDRESS *) sm_rpool_malloc_x(rpool,
550058561cbSjbeck 							sizeof(*newaddr));
5517c478bd9Sstevel@tonic-gate 			STRUCTCOPY(*addr, *newaddr);
5527c478bd9Sstevel@tonic-gate 			*tail = newaddr;
5537c478bd9Sstevel@tonic-gate 			tail = &newaddr->q_next;
5547c478bd9Sstevel@tonic-gate 		}
5557c478bd9Sstevel@tonic-gate 		addr = addr->q_next;
5567c478bd9Sstevel@tonic-gate 	}
5577c478bd9Sstevel@tonic-gate 	*tail = NULL;
5587c478bd9Sstevel@tonic-gate 
5597c478bd9Sstevel@tonic-gate 	return ret;
5607c478bd9Sstevel@tonic-gate }
561058561cbSjbeck 
5627c478bd9Sstevel@tonic-gate /*
5637c478bd9Sstevel@tonic-gate **  LOG_SENDMAIL_PID -- record sendmail pid and command line.
5647c478bd9Sstevel@tonic-gate **
5657c478bd9Sstevel@tonic-gate **	Parameters:
5667c478bd9Sstevel@tonic-gate **		e -- the current envelope.
5677c478bd9Sstevel@tonic-gate **
5687c478bd9Sstevel@tonic-gate **	Returns:
5697c478bd9Sstevel@tonic-gate **		none.
5707c478bd9Sstevel@tonic-gate **
5717c478bd9Sstevel@tonic-gate **	Side Effects:
5727c478bd9Sstevel@tonic-gate **		writes pidfile, logs command line.
5737c478bd9Sstevel@tonic-gate **		keeps file open and locked to prevent overwrite of active file
5747c478bd9Sstevel@tonic-gate */
5757c478bd9Sstevel@tonic-gate 
5767c478bd9Sstevel@tonic-gate static SM_FILE_T	*Pidf = NULL;
5777c478bd9Sstevel@tonic-gate 
5787c478bd9Sstevel@tonic-gate void
log_sendmail_pid(e)5797c478bd9Sstevel@tonic-gate log_sendmail_pid(e)
5807c478bd9Sstevel@tonic-gate 	ENVELOPE *e;
5817c478bd9Sstevel@tonic-gate {
5827c478bd9Sstevel@tonic-gate 	long sff;
5837c478bd9Sstevel@tonic-gate 	char pidpath[MAXPATHLEN];
5847c478bd9Sstevel@tonic-gate 	extern char *CommandLineArgs;
5857c478bd9Sstevel@tonic-gate 
5867c478bd9Sstevel@tonic-gate 	/* write the pid to the log file for posterity */
5877c478bd9Sstevel@tonic-gate 	sff = SFF_NOLINK|SFF_ROOTOK|SFF_REGONLY|SFF_CREAT|SFF_NBLOCK;
5887c478bd9Sstevel@tonic-gate 	if (TrustedUid != 0 && RealUid == TrustedUid)
5897c478bd9Sstevel@tonic-gate 		sff |= SFF_OPENASROOT;
590058561cbSjbeck 	expand(PidFile, pidpath, sizeof(pidpath), e);
5917c478bd9Sstevel@tonic-gate 	Pidf = safefopen(pidpath, O_WRONLY|O_TRUNC, FileMode, sff);
5927c478bd9Sstevel@tonic-gate 	if (Pidf == NULL)
5937c478bd9Sstevel@tonic-gate 	{
5947c478bd9Sstevel@tonic-gate 		if (errno == EWOULDBLOCK)
5957c478bd9Sstevel@tonic-gate 			sm_syslog(LOG_ERR, NOQID,
5967c478bd9Sstevel@tonic-gate 				  "unable to write pid to %s: file in use by another process",
5977c478bd9Sstevel@tonic-gate 				  pidpath);
5987c478bd9Sstevel@tonic-gate 		else
5997c478bd9Sstevel@tonic-gate 			sm_syslog(LOG_ERR, NOQID,
6007c478bd9Sstevel@tonic-gate 				  "unable to write pid to %s: %s",
6017c478bd9Sstevel@tonic-gate 				  pidpath, sm_errstring(errno));
6027c478bd9Sstevel@tonic-gate 	}
6037c478bd9Sstevel@tonic-gate 	else
6047c478bd9Sstevel@tonic-gate 	{
6057c478bd9Sstevel@tonic-gate 		PidFilePid = getpid();
6067c478bd9Sstevel@tonic-gate 
6077c478bd9Sstevel@tonic-gate 		/* write the process id on line 1 */
6087c478bd9Sstevel@tonic-gate 		(void) sm_io_fprintf(Pidf, SM_TIME_DEFAULT, "%ld\n",
6097c478bd9Sstevel@tonic-gate 				     (long) PidFilePid);
6107c478bd9Sstevel@tonic-gate 
6117c478bd9Sstevel@tonic-gate 		/* line 2 contains all command line flags */
6127c478bd9Sstevel@tonic-gate 		(void) sm_io_fprintf(Pidf, SM_TIME_DEFAULT, "%s\n",
6137c478bd9Sstevel@tonic-gate 				     CommandLineArgs);
6147c478bd9Sstevel@tonic-gate 
6157c478bd9Sstevel@tonic-gate 		/* flush */
6167c478bd9Sstevel@tonic-gate 		(void) sm_io_flush(Pidf, SM_TIME_DEFAULT);
6177c478bd9Sstevel@tonic-gate 
6187c478bd9Sstevel@tonic-gate 		/*
6197c478bd9Sstevel@tonic-gate 		**  Leave pid file open until process ends
6207c478bd9Sstevel@tonic-gate 		**  so it's not overwritten by another
6217c478bd9Sstevel@tonic-gate 		**  process.
6227c478bd9Sstevel@tonic-gate 		*/
6237c478bd9Sstevel@tonic-gate 	}
6247c478bd9Sstevel@tonic-gate 	if (LogLevel > 9)
6257c478bd9Sstevel@tonic-gate 		sm_syslog(LOG_INFO, NOQID, "started as: %s", CommandLineArgs);
6267c478bd9Sstevel@tonic-gate }
6277c478bd9Sstevel@tonic-gate 
6287c478bd9Sstevel@tonic-gate /*
6297c478bd9Sstevel@tonic-gate **  CLOSE_SENDMAIL_PID -- close sendmail pid file
6307c478bd9Sstevel@tonic-gate **
6317c478bd9Sstevel@tonic-gate **	Parameters:
6327c478bd9Sstevel@tonic-gate **		none.
6337c478bd9Sstevel@tonic-gate **
6347c478bd9Sstevel@tonic-gate **	Returns:
6357c478bd9Sstevel@tonic-gate **		none.
6367c478bd9Sstevel@tonic-gate */
6377c478bd9Sstevel@tonic-gate 
6387c478bd9Sstevel@tonic-gate void
close_sendmail_pid()6397c478bd9Sstevel@tonic-gate close_sendmail_pid()
6407c478bd9Sstevel@tonic-gate {
6417c478bd9Sstevel@tonic-gate 	if (Pidf == NULL)
6427c478bd9Sstevel@tonic-gate 		return;
6437c478bd9Sstevel@tonic-gate 
6447c478bd9Sstevel@tonic-gate 	(void) sm_io_close(Pidf, SM_TIME_DEFAULT);
6457c478bd9Sstevel@tonic-gate 	Pidf = NULL;
6467c478bd9Sstevel@tonic-gate }
6477c478bd9Sstevel@tonic-gate 
6487c478bd9Sstevel@tonic-gate /*
6497c478bd9Sstevel@tonic-gate **  SET_DELIVERY_MODE -- set and record the delivery mode
6507c478bd9Sstevel@tonic-gate **
6517c478bd9Sstevel@tonic-gate **	Parameters:
6527c478bd9Sstevel@tonic-gate **		mode -- delivery mode
6537c478bd9Sstevel@tonic-gate **		e -- the current envelope.
6547c478bd9Sstevel@tonic-gate **
6557c478bd9Sstevel@tonic-gate **	Returns:
6567c478bd9Sstevel@tonic-gate **		none.
6577c478bd9Sstevel@tonic-gate **
6587c478bd9Sstevel@tonic-gate **	Side Effects:
6597c478bd9Sstevel@tonic-gate **		sets {deliveryMode} macro
6607c478bd9Sstevel@tonic-gate */
6617c478bd9Sstevel@tonic-gate 
6627c478bd9Sstevel@tonic-gate void
set_delivery_mode(mode,e)6637c478bd9Sstevel@tonic-gate set_delivery_mode(mode, e)
6647c478bd9Sstevel@tonic-gate 	int mode;
6657c478bd9Sstevel@tonic-gate 	ENVELOPE *e;
6667c478bd9Sstevel@tonic-gate {
6677c478bd9Sstevel@tonic-gate 	char buf[2];
6687c478bd9Sstevel@tonic-gate 
6697c478bd9Sstevel@tonic-gate 	e->e_sendmode = (char) mode;
6707c478bd9Sstevel@tonic-gate 	buf[0] = (char) mode;
6717c478bd9Sstevel@tonic-gate 	buf[1] = '\0';
6727c478bd9Sstevel@tonic-gate 	macdefine(&e->e_macro, A_TEMP, macid("{deliveryMode}"), buf);
6737c478bd9Sstevel@tonic-gate }
674058561cbSjbeck 
6757c478bd9Sstevel@tonic-gate /*
6767c478bd9Sstevel@tonic-gate **  SET_OP_MODE -- set and record the op mode
6777c478bd9Sstevel@tonic-gate **
6787c478bd9Sstevel@tonic-gate **	Parameters:
6797c478bd9Sstevel@tonic-gate **		mode -- op mode
6807c478bd9Sstevel@tonic-gate **		e -- the current envelope.
6817c478bd9Sstevel@tonic-gate **
6827c478bd9Sstevel@tonic-gate **	Returns:
6837c478bd9Sstevel@tonic-gate **		none.
6847c478bd9Sstevel@tonic-gate **
6857c478bd9Sstevel@tonic-gate **	Side Effects:
6867c478bd9Sstevel@tonic-gate **		sets {opMode} macro
6877c478bd9Sstevel@tonic-gate */
6887c478bd9Sstevel@tonic-gate 
6897c478bd9Sstevel@tonic-gate void
set_op_mode(mode)6907c478bd9Sstevel@tonic-gate set_op_mode(mode)
6917c478bd9Sstevel@tonic-gate 	int mode;
6927c478bd9Sstevel@tonic-gate {
6937c478bd9Sstevel@tonic-gate 	char buf[2];
6947c478bd9Sstevel@tonic-gate 	extern ENVELOPE BlankEnvelope;
6957c478bd9Sstevel@tonic-gate 
6967c478bd9Sstevel@tonic-gate 	OpMode = (char) mode;
6977c478bd9Sstevel@tonic-gate 	buf[0] = (char) mode;
6987c478bd9Sstevel@tonic-gate 	buf[1] = '\0';
6997c478bd9Sstevel@tonic-gate 	macdefine(&BlankEnvelope.e_macro, A_TEMP, MID_OPMODE, buf);
7007c478bd9Sstevel@tonic-gate }
701058561cbSjbeck 
7027c478bd9Sstevel@tonic-gate /*
7037c478bd9Sstevel@tonic-gate **  PRINTAV -- print argument vector.
7047c478bd9Sstevel@tonic-gate **
7057c478bd9Sstevel@tonic-gate **	Parameters:
7067c478bd9Sstevel@tonic-gate **		fp -- output file pointer.
7077c478bd9Sstevel@tonic-gate **		av -- argument vector.
7087c478bd9Sstevel@tonic-gate **
7097c478bd9Sstevel@tonic-gate **	Returns:
7107c478bd9Sstevel@tonic-gate **		none.
7117c478bd9Sstevel@tonic-gate **
7127c478bd9Sstevel@tonic-gate **	Side Effects:
7137c478bd9Sstevel@tonic-gate **		prints av.
7147c478bd9Sstevel@tonic-gate */
7157c478bd9Sstevel@tonic-gate 
7167c478bd9Sstevel@tonic-gate void
printav(fp,av)7177c478bd9Sstevel@tonic-gate printav(fp, av)
7187c478bd9Sstevel@tonic-gate 	SM_FILE_T *fp;
719058561cbSjbeck 	char **av;
7207c478bd9Sstevel@tonic-gate {
7217c478bd9Sstevel@tonic-gate 	while (*av != NULL)
7227c478bd9Sstevel@tonic-gate 	{
7237c478bd9Sstevel@tonic-gate 		if (tTd(0, 44))
7247c478bd9Sstevel@tonic-gate 			sm_dprintf("\n\t%08lx=", (unsigned long) *av);
7257c478bd9Sstevel@tonic-gate 		else
7267c478bd9Sstevel@tonic-gate 			(void) sm_io_putc(fp, SM_TIME_DEFAULT, ' ');
727058561cbSjbeck 		if (tTd(0, 99))
728058561cbSjbeck 			sm_dprintf("%s", str2prt(*av++));
729058561cbSjbeck 		else
730058561cbSjbeck 			xputs(fp, *av++);
7317c478bd9Sstevel@tonic-gate 	}
7327c478bd9Sstevel@tonic-gate 	(void) sm_io_putc(fp, SM_TIME_DEFAULT, '\n');
7337c478bd9Sstevel@tonic-gate }
734058561cbSjbeck 
7357c478bd9Sstevel@tonic-gate /*
7367c478bd9Sstevel@tonic-gate **  XPUTS -- put string doing control escapes.
7377c478bd9Sstevel@tonic-gate **
7387c478bd9Sstevel@tonic-gate **	Parameters:
7397c478bd9Sstevel@tonic-gate **		fp -- output file pointer.
7407c478bd9Sstevel@tonic-gate **		s -- string to put.
7417c478bd9Sstevel@tonic-gate **
7427c478bd9Sstevel@tonic-gate **	Returns:
7437c478bd9Sstevel@tonic-gate **		none.
7447c478bd9Sstevel@tonic-gate **
7457c478bd9Sstevel@tonic-gate **	Side Effects:
7467c478bd9Sstevel@tonic-gate **		output to stdout
7477c478bd9Sstevel@tonic-gate */
7487c478bd9Sstevel@tonic-gate 
7497c478bd9Sstevel@tonic-gate void
xputs(fp,s)7507c478bd9Sstevel@tonic-gate xputs(fp, s)
7517c478bd9Sstevel@tonic-gate 	SM_FILE_T *fp;
752058561cbSjbeck 	const char *s;
7537c478bd9Sstevel@tonic-gate {
754058561cbSjbeck 	int c;
755058561cbSjbeck 	struct metamac *mp;
7567c478bd9Sstevel@tonic-gate 	bool shiftout = false;
7577c478bd9Sstevel@tonic-gate 	extern struct metamac MetaMacros[];
7587c478bd9Sstevel@tonic-gate 	static SM_DEBUG_T DebugANSI = SM_DEBUG_INITIALIZER("ANSI",
7597c478bd9Sstevel@tonic-gate 		"@(#)$Debug: ANSI - enable reverse video in debug output $");
7607c478bd9Sstevel@tonic-gate 
7617c478bd9Sstevel@tonic-gate 	/*
7627c478bd9Sstevel@tonic-gate 	**  TermEscape is set here, rather than in main(),
7637c478bd9Sstevel@tonic-gate 	**  because ANSI mode can be turned on or off at any time
7647c478bd9Sstevel@tonic-gate 	**  if we are in -bt rule testing mode.
7657c478bd9Sstevel@tonic-gate 	*/
7667c478bd9Sstevel@tonic-gate 
7677c478bd9Sstevel@tonic-gate 	if (sm_debug_unknown(&DebugANSI))
7687c478bd9Sstevel@tonic-gate 	{
7697c478bd9Sstevel@tonic-gate 		if (sm_debug_active(&DebugANSI, 1))
7707c478bd9Sstevel@tonic-gate 		{
7717c478bd9Sstevel@tonic-gate 			TermEscape.te_rv_on = "\033[7m";
772058561cbSjbeck 			TermEscape.te_normal = "\033[0m";
7737c478bd9Sstevel@tonic-gate 		}
7747c478bd9Sstevel@tonic-gate 		else
7757c478bd9Sstevel@tonic-gate 		{
7767c478bd9Sstevel@tonic-gate 			TermEscape.te_rv_on = "";
777058561cbSjbeck 			TermEscape.te_normal = "";
7787c478bd9Sstevel@tonic-gate 		}
7797c478bd9Sstevel@tonic-gate 	}
7807c478bd9Sstevel@tonic-gate 
7817c478bd9Sstevel@tonic-gate 	if (s == NULL)
7827c478bd9Sstevel@tonic-gate 	{
7837c478bd9Sstevel@tonic-gate 		(void) sm_io_fprintf(fp, SM_TIME_DEFAULT, "%s<null>%s",
784058561cbSjbeck 				     TermEscape.te_rv_on, TermEscape.te_normal);
7857c478bd9Sstevel@tonic-gate 		return;
7867c478bd9Sstevel@tonic-gate 	}
7877c478bd9Sstevel@tonic-gate 	while ((c = (*s++ & 0377)) != '\0')
7887c478bd9Sstevel@tonic-gate 	{
7897c478bd9Sstevel@tonic-gate 		if (shiftout)
7907c478bd9Sstevel@tonic-gate 		{
7917c478bd9Sstevel@tonic-gate 			(void) sm_io_fprintf(fp, SM_TIME_DEFAULT, "%s",
792058561cbSjbeck 					     TermEscape.te_normal);
7937c478bd9Sstevel@tonic-gate 			shiftout = false;
7947c478bd9Sstevel@tonic-gate 		}
795058561cbSjbeck 		if (!isascii(c) && !tTd(84, 1))
7967c478bd9Sstevel@tonic-gate 		{
7977c478bd9Sstevel@tonic-gate 			if (c == MATCHREPL)
7987c478bd9Sstevel@tonic-gate 			{
7997c478bd9Sstevel@tonic-gate 				(void) sm_io_fprintf(fp, SM_TIME_DEFAULT,
8007c478bd9Sstevel@tonic-gate 						     "%s$",
8017c478bd9Sstevel@tonic-gate 						     TermEscape.te_rv_on);
8027c478bd9Sstevel@tonic-gate 				shiftout = true;
8037c478bd9Sstevel@tonic-gate 				if (*s == '\0')
8047c478bd9Sstevel@tonic-gate 					continue;
8057c478bd9Sstevel@tonic-gate 				c = *s++ & 0377;
8067c478bd9Sstevel@tonic-gate 				goto printchar;
8077c478bd9Sstevel@tonic-gate 			}
8087c478bd9Sstevel@tonic-gate 			if (c == MACROEXPAND || c == MACRODEXPAND)
8097c478bd9Sstevel@tonic-gate 			{
8107c478bd9Sstevel@tonic-gate 				(void) sm_io_fprintf(fp, SM_TIME_DEFAULT,
8117c478bd9Sstevel@tonic-gate 						     "%s$",
8127c478bd9Sstevel@tonic-gate 						     TermEscape.te_rv_on);
8137c478bd9Sstevel@tonic-gate 				if (c == MACRODEXPAND)
8147c478bd9Sstevel@tonic-gate 					(void) sm_io_putc(fp,
8157c478bd9Sstevel@tonic-gate 							  SM_TIME_DEFAULT, '&');
8167c478bd9Sstevel@tonic-gate 				shiftout = true;
8177c478bd9Sstevel@tonic-gate 				if (*s == '\0')
8187c478bd9Sstevel@tonic-gate 					continue;
8197c478bd9Sstevel@tonic-gate 				if (strchr("=~&?", *s) != NULL)
8207c478bd9Sstevel@tonic-gate 					(void) sm_io_putc(fp,
8217c478bd9Sstevel@tonic-gate 							  SM_TIME_DEFAULT,
8227c478bd9Sstevel@tonic-gate 							  *s++);
8237c478bd9Sstevel@tonic-gate 				if (bitset(0200, *s))
8247c478bd9Sstevel@tonic-gate 					(void) sm_io_fprintf(fp,
8257c478bd9Sstevel@tonic-gate 							     SM_TIME_DEFAULT,
8267c478bd9Sstevel@tonic-gate 							     "{%s}",
8277c478bd9Sstevel@tonic-gate 							     macname(bitidx(*s++)));
8287c478bd9Sstevel@tonic-gate 				else
8297c478bd9Sstevel@tonic-gate 					(void) sm_io_fprintf(fp,
8307c478bd9Sstevel@tonic-gate 							     SM_TIME_DEFAULT,
8317c478bd9Sstevel@tonic-gate 							     "%c",
8327c478bd9Sstevel@tonic-gate 							     *s++);
8337c478bd9Sstevel@tonic-gate 				continue;
8347c478bd9Sstevel@tonic-gate 			}
8357c478bd9Sstevel@tonic-gate 			for (mp = MetaMacros; mp->metaname != '\0'; mp++)
8367c478bd9Sstevel@tonic-gate 			{
8377c478bd9Sstevel@tonic-gate 				if (bitidx(mp->metaval) == c)
8387c478bd9Sstevel@tonic-gate 				{
8397c478bd9Sstevel@tonic-gate 					(void) sm_io_fprintf(fp,
8407c478bd9Sstevel@tonic-gate 							     SM_TIME_DEFAULT,
8417c478bd9Sstevel@tonic-gate 							     "%s$%c",
8427c478bd9Sstevel@tonic-gate 							     TermEscape.te_rv_on,
8437c478bd9Sstevel@tonic-gate 							     mp->metaname);
8447c478bd9Sstevel@tonic-gate 					shiftout = true;
8457c478bd9Sstevel@tonic-gate 					break;
8467c478bd9Sstevel@tonic-gate 				}
8477c478bd9Sstevel@tonic-gate 			}
8487c478bd9Sstevel@tonic-gate 			if (c == MATCHCLASS || c == MATCHNCLASS)
8497c478bd9Sstevel@tonic-gate 			{
8507c478bd9Sstevel@tonic-gate 				if (bitset(0200, *s))
8517c478bd9Sstevel@tonic-gate 					(void) sm_io_fprintf(fp,
8527c478bd9Sstevel@tonic-gate 							     SM_TIME_DEFAULT,
8537c478bd9Sstevel@tonic-gate 							     "{%s}",
8547c478bd9Sstevel@tonic-gate 							     macname(bitidx(*s++)));
8557c478bd9Sstevel@tonic-gate 				else if (*s != '\0')
8567c478bd9Sstevel@tonic-gate 					(void) sm_io_fprintf(fp,
8577c478bd9Sstevel@tonic-gate 							     SM_TIME_DEFAULT,
8587c478bd9Sstevel@tonic-gate 							     "%c",
8597c478bd9Sstevel@tonic-gate 							     *s++);
8607c478bd9Sstevel@tonic-gate 			}
8617c478bd9Sstevel@tonic-gate 			if (mp->metaname != '\0')
8627c478bd9Sstevel@tonic-gate 				continue;
8637c478bd9Sstevel@tonic-gate 
8647c478bd9Sstevel@tonic-gate 			/* unrecognized meta character */
8657c478bd9Sstevel@tonic-gate 			(void) sm_io_fprintf(fp, SM_TIME_DEFAULT, "%sM-",
8667c478bd9Sstevel@tonic-gate 					     TermEscape.te_rv_on);
8677c478bd9Sstevel@tonic-gate 			shiftout = true;
8687c478bd9Sstevel@tonic-gate 			c &= 0177;
8697c478bd9Sstevel@tonic-gate 		}
8707c478bd9Sstevel@tonic-gate   printchar:
871*e9af4bc0SJohn Beck 		if (isascii(c) && isprint(c))
8727c478bd9Sstevel@tonic-gate 		{
8737c478bd9Sstevel@tonic-gate 			(void) sm_io_putc(fp, SM_TIME_DEFAULT, c);
8747c478bd9Sstevel@tonic-gate 			continue;
8757c478bd9Sstevel@tonic-gate 		}
8767c478bd9Sstevel@tonic-gate 
8777c478bd9Sstevel@tonic-gate 		/* wasn't a meta-macro -- find another way to print it */
8787c478bd9Sstevel@tonic-gate 		switch (c)
8797c478bd9Sstevel@tonic-gate 		{
8807c478bd9Sstevel@tonic-gate 		  case '\n':
8817c478bd9Sstevel@tonic-gate 			c = 'n';
8827c478bd9Sstevel@tonic-gate 			break;
8837c478bd9Sstevel@tonic-gate 
8847c478bd9Sstevel@tonic-gate 		  case '\r':
8857c478bd9Sstevel@tonic-gate 			c = 'r';
8867c478bd9Sstevel@tonic-gate 			break;
8877c478bd9Sstevel@tonic-gate 
8887c478bd9Sstevel@tonic-gate 		  case '\t':
8897c478bd9Sstevel@tonic-gate 			c = 't';
8907c478bd9Sstevel@tonic-gate 			break;
8917c478bd9Sstevel@tonic-gate 		}
8927c478bd9Sstevel@tonic-gate 		if (!shiftout)
8937c478bd9Sstevel@tonic-gate 		{
8947c478bd9Sstevel@tonic-gate 			(void) sm_io_fprintf(fp, SM_TIME_DEFAULT, "%s",
8957c478bd9Sstevel@tonic-gate 					     TermEscape.te_rv_on);
8967c478bd9Sstevel@tonic-gate 			shiftout = true;
8977c478bd9Sstevel@tonic-gate 		}
898*e9af4bc0SJohn Beck 		if (isascii(c) && isprint(c))
8997c478bd9Sstevel@tonic-gate 		{
9007c478bd9Sstevel@tonic-gate 			(void) sm_io_putc(fp, SM_TIME_DEFAULT, '\\');
9017c478bd9Sstevel@tonic-gate 			(void) sm_io_putc(fp, SM_TIME_DEFAULT, c);
9027c478bd9Sstevel@tonic-gate 		}
903058561cbSjbeck 		else if (tTd(84, 2))
904058561cbSjbeck 			(void) sm_io_fprintf(fp, SM_TIME_DEFAULT, " %o ", c);
905058561cbSjbeck 		else if (tTd(84, 1))
906058561cbSjbeck 			(void) sm_io_fprintf(fp, SM_TIME_DEFAULT, " %#x ", c);
907058561cbSjbeck 		else if (!isascii(c) && !tTd(84, 1))
9087c478bd9Sstevel@tonic-gate 		{
9097c478bd9Sstevel@tonic-gate 			(void) sm_io_putc(fp, SM_TIME_DEFAULT, '^');
9107c478bd9Sstevel@tonic-gate 			(void) sm_io_putc(fp, SM_TIME_DEFAULT, c ^ 0100);
9117c478bd9Sstevel@tonic-gate 		}
9127c478bd9Sstevel@tonic-gate 	}
9137c478bd9Sstevel@tonic-gate 	if (shiftout)
9147c478bd9Sstevel@tonic-gate 		(void) sm_io_fprintf(fp, SM_TIME_DEFAULT, "%s",
915058561cbSjbeck 				     TermEscape.te_normal);
9167c478bd9Sstevel@tonic-gate 	(void) sm_io_flush(fp, SM_TIME_DEFAULT);
9177c478bd9Sstevel@tonic-gate }
918058561cbSjbeck 
9197c478bd9Sstevel@tonic-gate /*
9207c478bd9Sstevel@tonic-gate **  MAKELOWER -- Translate a line into lower case
9217c478bd9Sstevel@tonic-gate **
9227c478bd9Sstevel@tonic-gate **	Parameters:
9237c478bd9Sstevel@tonic-gate **		p -- the string to translate.  If NULL, return is
9247c478bd9Sstevel@tonic-gate **			immediate.
9257c478bd9Sstevel@tonic-gate **
9267c478bd9Sstevel@tonic-gate **	Returns:
9277c478bd9Sstevel@tonic-gate **		none.
9287c478bd9Sstevel@tonic-gate **
9297c478bd9Sstevel@tonic-gate **	Side Effects:
9307c478bd9Sstevel@tonic-gate **		String pointed to by p is translated to lower case.
9317c478bd9Sstevel@tonic-gate */
9327c478bd9Sstevel@tonic-gate 
9337c478bd9Sstevel@tonic-gate void
makelower(p)9347c478bd9Sstevel@tonic-gate makelower(p)
9357c478bd9Sstevel@tonic-gate 	register char *p;
9367c478bd9Sstevel@tonic-gate {
9377c478bd9Sstevel@tonic-gate 	register char c;
9387c478bd9Sstevel@tonic-gate 
9397c478bd9Sstevel@tonic-gate 	if (p == NULL)
9407c478bd9Sstevel@tonic-gate 		return;
9417c478bd9Sstevel@tonic-gate 	for (; (c = *p) != '\0'; p++)
9427c478bd9Sstevel@tonic-gate 		if (isascii(c) && isupper(c))
9437c478bd9Sstevel@tonic-gate 			*p = tolower(c);
9447c478bd9Sstevel@tonic-gate }
945058561cbSjbeck 
9467c478bd9Sstevel@tonic-gate /*
9477c478bd9Sstevel@tonic-gate **  FIXCRLF -- fix <CR><LF> in line.
9487c478bd9Sstevel@tonic-gate **
9497c478bd9Sstevel@tonic-gate **	Looks for the <CR><LF> combination and turns it into the
9507c478bd9Sstevel@tonic-gate **	UNIX canonical <NL> character.  It only takes one line,
9517c478bd9Sstevel@tonic-gate **	i.e., it is assumed that the first <NL> found is the end
9527c478bd9Sstevel@tonic-gate **	of the line.
9537c478bd9Sstevel@tonic-gate **
9547c478bd9Sstevel@tonic-gate **	Parameters:
9557c478bd9Sstevel@tonic-gate **		line -- the line to fix.
9567c478bd9Sstevel@tonic-gate **		stripnl -- if true, strip the newline also.
9577c478bd9Sstevel@tonic-gate **
9587c478bd9Sstevel@tonic-gate **	Returns:
9597c478bd9Sstevel@tonic-gate **		none.
9607c478bd9Sstevel@tonic-gate **
9617c478bd9Sstevel@tonic-gate **	Side Effects:
9627c478bd9Sstevel@tonic-gate **		line is changed in place.
9637c478bd9Sstevel@tonic-gate */
9647c478bd9Sstevel@tonic-gate 
9657c478bd9Sstevel@tonic-gate void
fixcrlf(line,stripnl)9667c478bd9Sstevel@tonic-gate fixcrlf(line, stripnl)
9677c478bd9Sstevel@tonic-gate 	char *line;
9687c478bd9Sstevel@tonic-gate 	bool stripnl;
9697c478bd9Sstevel@tonic-gate {
9707c478bd9Sstevel@tonic-gate 	register char *p;
9717c478bd9Sstevel@tonic-gate 
9727c478bd9Sstevel@tonic-gate 	p = strchr(line, '\n');
9737c478bd9Sstevel@tonic-gate 	if (p == NULL)
9747c478bd9Sstevel@tonic-gate 		return;
9757c478bd9Sstevel@tonic-gate 	if (p > line && p[-1] == '\r')
9767c478bd9Sstevel@tonic-gate 		p--;
9777c478bd9Sstevel@tonic-gate 	if (!stripnl)
9787c478bd9Sstevel@tonic-gate 		*p++ = '\n';
9797c478bd9Sstevel@tonic-gate 	*p = '\0';
9807c478bd9Sstevel@tonic-gate }
981058561cbSjbeck 
9827c478bd9Sstevel@tonic-gate /*
9837c478bd9Sstevel@tonic-gate **  PUTLINE -- put a line like fputs obeying SMTP conventions
9847c478bd9Sstevel@tonic-gate **
9857c478bd9Sstevel@tonic-gate **	This routine always guarantees outputing a newline (or CRLF,
9867c478bd9Sstevel@tonic-gate **	as appropriate) at the end of the string.
9877c478bd9Sstevel@tonic-gate **
9887c478bd9Sstevel@tonic-gate **	Parameters:
9897c478bd9Sstevel@tonic-gate **		l -- line to put.
9907c478bd9Sstevel@tonic-gate **		mci -- the mailer connection information.
9917c478bd9Sstevel@tonic-gate **
9927c478bd9Sstevel@tonic-gate **	Returns:
993445f2479Sjbeck **		true iff line was written successfully
9947c478bd9Sstevel@tonic-gate **
9957c478bd9Sstevel@tonic-gate **	Side Effects:
9967c478bd9Sstevel@tonic-gate **		output of l to mci->mci_out.
9977c478bd9Sstevel@tonic-gate */
9987c478bd9Sstevel@tonic-gate 
999445f2479Sjbeck bool
putline(l,mci)10007c478bd9Sstevel@tonic-gate putline(l, mci)
10017c478bd9Sstevel@tonic-gate 	register char *l;
10027c478bd9Sstevel@tonic-gate 	register MCI *mci;
10037c478bd9Sstevel@tonic-gate {
1004445f2479Sjbeck 	return putxline(l, strlen(l), mci, PXLF_MAPFROM);
10057c478bd9Sstevel@tonic-gate }
1006058561cbSjbeck 
10077c478bd9Sstevel@tonic-gate /*
10087c478bd9Sstevel@tonic-gate **  PUTXLINE -- putline with flags bits.
10097c478bd9Sstevel@tonic-gate **
10107c478bd9Sstevel@tonic-gate **	This routine always guarantees outputing a newline (or CRLF,
10117c478bd9Sstevel@tonic-gate **	as appropriate) at the end of the string.
10127c478bd9Sstevel@tonic-gate **
10137c478bd9Sstevel@tonic-gate **	Parameters:
10147c478bd9Sstevel@tonic-gate **		l -- line to put.
10157c478bd9Sstevel@tonic-gate **		len -- the length of the line.
10167c478bd9Sstevel@tonic-gate **		mci -- the mailer connection information.
10177c478bd9Sstevel@tonic-gate **		pxflags -- flag bits:
10187c478bd9Sstevel@tonic-gate **		    PXLF_MAPFROM -- map From_ to >From_.
10197c478bd9Sstevel@tonic-gate **		    PXLF_STRIP8BIT -- strip 8th bit.
10207c478bd9Sstevel@tonic-gate **		    PXLF_HEADER -- map bare newline in header to newline space.
10217c478bd9Sstevel@tonic-gate **		    PXLF_NOADDEOL -- don't add an EOL if one wasn't present.
1022058561cbSjbeck **		    PXLF_STRIPMQUOTE -- strip METAQUOTE bytes.
10237c478bd9Sstevel@tonic-gate **
10247c478bd9Sstevel@tonic-gate **	Returns:
1025445f2479Sjbeck **		true iff line was written successfully
10267c478bd9Sstevel@tonic-gate **
10277c478bd9Sstevel@tonic-gate **	Side Effects:
10287c478bd9Sstevel@tonic-gate **		output of l to mci->mci_out.
10297c478bd9Sstevel@tonic-gate */
10307c478bd9Sstevel@tonic-gate 
1031058561cbSjbeck 
1032058561cbSjbeck #define PUTX(limit)							\
1033058561cbSjbeck 	do								\
1034058561cbSjbeck 	{								\
1035058561cbSjbeck 		quotenext = false;					\
1036058561cbSjbeck 		while (l < limit)					\
1037058561cbSjbeck 		{							\
1038058561cbSjbeck 			unsigned char c = (unsigned char) *l++;		\
1039058561cbSjbeck 									\
1040058561cbSjbeck 			if (bitset(PXLF_STRIPMQUOTE, pxflags) &&	\
1041058561cbSjbeck 			    !quotenext && c == METAQUOTE)		\
1042058561cbSjbeck 			{						\
1043058561cbSjbeck 				quotenext = true;			\
1044058561cbSjbeck 				continue;				\
1045058561cbSjbeck 			}						\
1046058561cbSjbeck 			quotenext = false;				\
1047058561cbSjbeck 			if (strip8bit)					\
1048058561cbSjbeck 				c &= 0177;				\
1049058561cbSjbeck 			if (sm_io_putc(mci->mci_out, SM_TIME_DEFAULT,	\
1050058561cbSjbeck 				       c) == SM_IO_EOF)			\
1051058561cbSjbeck 			{						\
1052058561cbSjbeck 				dead = true;				\
1053058561cbSjbeck 				break;					\
1054058561cbSjbeck 			}						\
1055058561cbSjbeck 			if (TrafficLogFile != NULL)			\
1056058561cbSjbeck 				(void) sm_io_putc(TrafficLogFile,	\
1057058561cbSjbeck 						  SM_TIME_DEFAULT,	\
1058058561cbSjbeck 						  c);			\
1059058561cbSjbeck 		}							\
1060058561cbSjbeck 	} while (0)
1061058561cbSjbeck 
1062445f2479Sjbeck bool
putxline(l,len,mci,pxflags)10637c478bd9Sstevel@tonic-gate putxline(l, len, mci, pxflags)
10647c478bd9Sstevel@tonic-gate 	register char *l;
10657c478bd9Sstevel@tonic-gate 	size_t len;
10667c478bd9Sstevel@tonic-gate 	register MCI *mci;
10677c478bd9Sstevel@tonic-gate 	int pxflags;
10687c478bd9Sstevel@tonic-gate {
10697c478bd9Sstevel@tonic-gate 	register char *p, *end;
1070058561cbSjbeck 	int slop;
1071058561cbSjbeck 	bool dead, quotenext, strip8bit;
10727c478bd9Sstevel@tonic-gate 
10737c478bd9Sstevel@tonic-gate 	/* strip out 0200 bits -- these can look like TELNET protocol */
1074058561cbSjbeck 	strip8bit = bitset(MCIF_7BIT, mci->mci_flags) ||
1075058561cbSjbeck 		    bitset(PXLF_STRIP8BIT, pxflags);
1076058561cbSjbeck 	dead = false;
1077058561cbSjbeck 	slop = 0;
10787c478bd9Sstevel@tonic-gate 
10797c478bd9Sstevel@tonic-gate 	end = l + len;
10807c478bd9Sstevel@tonic-gate 	do
10817c478bd9Sstevel@tonic-gate 	{
10827c478bd9Sstevel@tonic-gate 		bool noeol = false;
10837c478bd9Sstevel@tonic-gate 
10847c478bd9Sstevel@tonic-gate 		/* find the end of the line */
10857c478bd9Sstevel@tonic-gate 		p = memchr(l, '\n', end - l);
10867c478bd9Sstevel@tonic-gate 		if (p == NULL)
10877c478bd9Sstevel@tonic-gate 		{
10887c478bd9Sstevel@tonic-gate 			p = end;
10897c478bd9Sstevel@tonic-gate 			noeol = true;
10907c478bd9Sstevel@tonic-gate 		}
10917c478bd9Sstevel@tonic-gate 
10927c478bd9Sstevel@tonic-gate 		if (TrafficLogFile != NULL)
10937c478bd9Sstevel@tonic-gate 			(void) sm_io_fprintf(TrafficLogFile, SM_TIME_DEFAULT,
10947c478bd9Sstevel@tonic-gate 					     "%05d >>> ", (int) CurrentPid);
10957c478bd9Sstevel@tonic-gate 
10967c478bd9Sstevel@tonic-gate 		/* check for line overflow */
10977c478bd9Sstevel@tonic-gate 		while (mci->mci_mailer->m_linelimit > 0 &&
10987c478bd9Sstevel@tonic-gate 		       (p - l + slop) > mci->mci_mailer->m_linelimit)
10997c478bd9Sstevel@tonic-gate 		{
11007c478bd9Sstevel@tonic-gate 			register char *q = &l[mci->mci_mailer->m_linelimit - slop - 1];
11017c478bd9Sstevel@tonic-gate 
11027c478bd9Sstevel@tonic-gate 			if (l[0] == '.' && slop == 0 &&
11037c478bd9Sstevel@tonic-gate 			    bitnset(M_XDOT, mci->mci_mailer->m_flags))
11047c478bd9Sstevel@tonic-gate 			{
11057c478bd9Sstevel@tonic-gate 				if (sm_io_putc(mci->mci_out, SM_TIME_DEFAULT,
11067c478bd9Sstevel@tonic-gate 					       '.') == SM_IO_EOF)
11077c478bd9Sstevel@tonic-gate 					dead = true;
11087c478bd9Sstevel@tonic-gate 				if (TrafficLogFile != NULL)
11097c478bd9Sstevel@tonic-gate 					(void) sm_io_putc(TrafficLogFile,
11107c478bd9Sstevel@tonic-gate 							  SM_TIME_DEFAULT, '.');
11117c478bd9Sstevel@tonic-gate 			}
11127c478bd9Sstevel@tonic-gate 			else if (l[0] == 'F' && slop == 0 &&
11137c478bd9Sstevel@tonic-gate 				 bitset(PXLF_MAPFROM, pxflags) &&
11147c478bd9Sstevel@tonic-gate 				 strncmp(l, "From ", 5) == 0 &&
11157c478bd9Sstevel@tonic-gate 				 bitnset(M_ESCFROM, mci->mci_mailer->m_flags))
11167c478bd9Sstevel@tonic-gate 			{
11177c478bd9Sstevel@tonic-gate 				if (sm_io_putc(mci->mci_out, SM_TIME_DEFAULT,
11187c478bd9Sstevel@tonic-gate 					       '>') == SM_IO_EOF)
11197c478bd9Sstevel@tonic-gate 					dead = true;
11207c478bd9Sstevel@tonic-gate 				if (TrafficLogFile != NULL)
11217c478bd9Sstevel@tonic-gate 					(void) sm_io_putc(TrafficLogFile,
11227c478bd9Sstevel@tonic-gate 							  SM_TIME_DEFAULT,
11237c478bd9Sstevel@tonic-gate 							  '>');
11247c478bd9Sstevel@tonic-gate 			}
11257c478bd9Sstevel@tonic-gate 			if (dead)
11267c478bd9Sstevel@tonic-gate 				break;
11277c478bd9Sstevel@tonic-gate 
1128058561cbSjbeck 			PUTX(q);
11297c478bd9Sstevel@tonic-gate 			if (dead)
11307c478bd9Sstevel@tonic-gate 				break;
11317c478bd9Sstevel@tonic-gate 
1132058561cbSjbeck 			if (sm_io_putc(mci->mci_out, SM_TIME_DEFAULT,
1133058561cbSjbeck 					'!') == SM_IO_EOF ||
11347c478bd9Sstevel@tonic-gate 			    sm_io_fputs(mci->mci_out, SM_TIME_DEFAULT,
1135058561cbSjbeck 					mci->mci_mailer->m_eol) == SM_IO_EOF ||
1136058561cbSjbeck 			    sm_io_putc(mci->mci_out, SM_TIME_DEFAULT,
1137058561cbSjbeck 					' ') == SM_IO_EOF)
11387c478bd9Sstevel@tonic-gate 			{
11397c478bd9Sstevel@tonic-gate 				dead = true;
11407c478bd9Sstevel@tonic-gate 				break;
11417c478bd9Sstevel@tonic-gate 			}
11427c478bd9Sstevel@tonic-gate 			if (TrafficLogFile != NULL)
11437c478bd9Sstevel@tonic-gate 			{
11447c478bd9Sstevel@tonic-gate 				(void) sm_io_fprintf(TrafficLogFile,
11457c478bd9Sstevel@tonic-gate 						     SM_TIME_DEFAULT,
11467c478bd9Sstevel@tonic-gate 						     "!\n%05d >>>  ",
11477c478bd9Sstevel@tonic-gate 						     (int) CurrentPid);
11487c478bd9Sstevel@tonic-gate 			}
11497c478bd9Sstevel@tonic-gate 			slop = 1;
11507c478bd9Sstevel@tonic-gate 		}
11517c478bd9Sstevel@tonic-gate 
11527c478bd9Sstevel@tonic-gate 		if (dead)
11537c478bd9Sstevel@tonic-gate 			break;
11547c478bd9Sstevel@tonic-gate 
11557c478bd9Sstevel@tonic-gate 		/* output last part */
11567c478bd9Sstevel@tonic-gate 		if (l[0] == '.' && slop == 0 &&
11577800901eSjbeck 		    bitnset(M_XDOT, mci->mci_mailer->m_flags) &&
11587800901eSjbeck 		    !bitset(MCIF_INLONGLINE, mci->mci_flags))
11597c478bd9Sstevel@tonic-gate 		{
11607c478bd9Sstevel@tonic-gate 			if (sm_io_putc(mci->mci_out, SM_TIME_DEFAULT, '.') ==
11617c478bd9Sstevel@tonic-gate 			    SM_IO_EOF)
11627c478bd9Sstevel@tonic-gate 			{
1163445f2479Sjbeck 				dead = true;
1164445f2479Sjbeck 				break;
11657c478bd9Sstevel@tonic-gate 			}
11667c478bd9Sstevel@tonic-gate 			if (TrafficLogFile != NULL)
11677c478bd9Sstevel@tonic-gate 				(void) sm_io_putc(TrafficLogFile,
11687c478bd9Sstevel@tonic-gate 						  SM_TIME_DEFAULT, '.');
11697c478bd9Sstevel@tonic-gate 		}
11707c478bd9Sstevel@tonic-gate 		else if (l[0] == 'F' && slop == 0 &&
11717c478bd9Sstevel@tonic-gate 			 bitset(PXLF_MAPFROM, pxflags) &&
11727c478bd9Sstevel@tonic-gate 			 strncmp(l, "From ", 5) == 0 &&
11737800901eSjbeck 			 bitnset(M_ESCFROM, mci->mci_mailer->m_flags) &&
11747800901eSjbeck 			 !bitset(MCIF_INLONGLINE, mci->mci_flags))
11757c478bd9Sstevel@tonic-gate 		{
11767c478bd9Sstevel@tonic-gate 			if (sm_io_putc(mci->mci_out, SM_TIME_DEFAULT, '>') ==
11777c478bd9Sstevel@tonic-gate 			    SM_IO_EOF)
11787c478bd9Sstevel@tonic-gate 			{
1179445f2479Sjbeck 				dead = true;
1180445f2479Sjbeck 				break;
11817c478bd9Sstevel@tonic-gate 			}
11827c478bd9Sstevel@tonic-gate 			if (TrafficLogFile != NULL)
11837c478bd9Sstevel@tonic-gate 				(void) sm_io_putc(TrafficLogFile,
11847c478bd9Sstevel@tonic-gate 						  SM_TIME_DEFAULT, '>');
11857c478bd9Sstevel@tonic-gate 		}
1186058561cbSjbeck 		PUTX(p);
11877c478bd9Sstevel@tonic-gate 		if (dead)
11887c478bd9Sstevel@tonic-gate 			break;
11897c478bd9Sstevel@tonic-gate 
11907c478bd9Sstevel@tonic-gate 		if (TrafficLogFile != NULL)
11917c478bd9Sstevel@tonic-gate 			(void) sm_io_putc(TrafficLogFile, SM_TIME_DEFAULT,
11927c478bd9Sstevel@tonic-gate 					  '\n');
11937800901eSjbeck 		if ((!bitset(PXLF_NOADDEOL, pxflags) || !noeol))
11947c478bd9Sstevel@tonic-gate 		{
11957800901eSjbeck 			mci->mci_flags &= ~MCIF_INLONGLINE;
11967800901eSjbeck 			if (sm_io_fputs(mci->mci_out, SM_TIME_DEFAULT,
11977800901eSjbeck 					mci->mci_mailer->m_eol) == SM_IO_EOF)
11987800901eSjbeck 			{
11997800901eSjbeck 				dead = true;
12007800901eSjbeck 				break;
12017800901eSjbeck 			}
12027c478bd9Sstevel@tonic-gate 		}
12037800901eSjbeck 		else
12047800901eSjbeck 			mci->mci_flags |= MCIF_INLONGLINE;
12057800901eSjbeck 
12067c478bd9Sstevel@tonic-gate 		if (l < end && *l == '\n')
12077c478bd9Sstevel@tonic-gate 		{
12087c478bd9Sstevel@tonic-gate 			if (*++l != ' ' && *l != '\t' && *l != '\0' &&
12097c478bd9Sstevel@tonic-gate 			    bitset(PXLF_HEADER, pxflags))
12107c478bd9Sstevel@tonic-gate 			{
12117c478bd9Sstevel@tonic-gate 				if (sm_io_putc(mci->mci_out, SM_TIME_DEFAULT,
12127c478bd9Sstevel@tonic-gate 					       ' ') == SM_IO_EOF)
12137c478bd9Sstevel@tonic-gate 				{
1214445f2479Sjbeck 					dead = true;
1215445f2479Sjbeck 					break;
12167c478bd9Sstevel@tonic-gate 				}
12177c478bd9Sstevel@tonic-gate 
12187c478bd9Sstevel@tonic-gate 				if (TrafficLogFile != NULL)
12197c478bd9Sstevel@tonic-gate 					(void) sm_io_putc(TrafficLogFile,
12207c478bd9Sstevel@tonic-gate 							  SM_TIME_DEFAULT, ' ');
12217c478bd9Sstevel@tonic-gate 			}
12227c478bd9Sstevel@tonic-gate 		}
12237c478bd9Sstevel@tonic-gate 
12247c478bd9Sstevel@tonic-gate 	} while (l < end);
1225445f2479Sjbeck 	return !dead;
12267c478bd9Sstevel@tonic-gate }
1227445f2479Sjbeck 
12287c478bd9Sstevel@tonic-gate /*
12297c478bd9Sstevel@tonic-gate **  XUNLINK -- unlink a file, doing logging as appropriate.
12307c478bd9Sstevel@tonic-gate **
12317c478bd9Sstevel@tonic-gate **	Parameters:
12327c478bd9Sstevel@tonic-gate **		f -- name of file to unlink.
12337c478bd9Sstevel@tonic-gate **
12347c478bd9Sstevel@tonic-gate **	Returns:
12357c478bd9Sstevel@tonic-gate **		return value of unlink()
12367c478bd9Sstevel@tonic-gate **
12377c478bd9Sstevel@tonic-gate **	Side Effects:
12387c478bd9Sstevel@tonic-gate **		f is unlinked.
12397c478bd9Sstevel@tonic-gate */
12407c478bd9Sstevel@tonic-gate 
12417c478bd9Sstevel@tonic-gate int
xunlink(f)12427c478bd9Sstevel@tonic-gate xunlink(f)
12437c478bd9Sstevel@tonic-gate 	char *f;
12447c478bd9Sstevel@tonic-gate {
12457c478bd9Sstevel@tonic-gate 	register int i;
12467c478bd9Sstevel@tonic-gate 	int save_errno;
12477c478bd9Sstevel@tonic-gate 
12487c478bd9Sstevel@tonic-gate 	if (LogLevel > 98)
12497c478bd9Sstevel@tonic-gate 		sm_syslog(LOG_DEBUG, CurEnv->e_id, "unlink %s", f);
12507c478bd9Sstevel@tonic-gate 
12517c478bd9Sstevel@tonic-gate 	i = unlink(f);
12527c478bd9Sstevel@tonic-gate 	save_errno = errno;
12537c478bd9Sstevel@tonic-gate 	if (i < 0 && LogLevel > 97)
12547c478bd9Sstevel@tonic-gate 		sm_syslog(LOG_DEBUG, CurEnv->e_id, "%s: unlink-fail %d",
12557c478bd9Sstevel@tonic-gate 			  f, errno);
12567c478bd9Sstevel@tonic-gate 	if (i >= 0)
12577c478bd9Sstevel@tonic-gate 		SYNC_DIR(f, false);
12587c478bd9Sstevel@tonic-gate 	errno = save_errno;
12597c478bd9Sstevel@tonic-gate 	return i;
12607c478bd9Sstevel@tonic-gate }
1261058561cbSjbeck 
12627c478bd9Sstevel@tonic-gate /*
12637c478bd9Sstevel@tonic-gate **  SFGETS -- "safe" fgets -- times out and ignores random interrupts.
12647c478bd9Sstevel@tonic-gate **
12657c478bd9Sstevel@tonic-gate **	Parameters:
12667c478bd9Sstevel@tonic-gate **		buf -- place to put the input line.
12677c478bd9Sstevel@tonic-gate **		siz -- size of buf.
12687c478bd9