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.
12687c478bd9Sstevel@tonic-gate ** fp -- file to read from.
12697c478bd9Sstevel@tonic-gate ** timeout -- the timeout before error occurs.
12707c478bd9Sstevel@tonic-gate ** during -- what we are trying to read (for error messages).
12717c478bd9Sstevel@tonic-gate **
12727c478bd9Sstevel@tonic-gate ** Returns:
12737c478bd9Sstevel@tonic-gate ** NULL on error (including timeout). This may also leave
12747c478bd9Sstevel@tonic-gate ** buf containing a null string.
12757c478bd9Sstevel@tonic-gate ** buf otherwise.
12767c478bd9Sstevel@tonic-gate */
12777c478bd9Sstevel@tonic-gate
12787c478bd9Sstevel@tonic-gate
12797c478bd9Sstevel@tonic-gate char *
sfgets(buf,siz,fp,timeout,during)12807c478bd9Sstevel@tonic-gate sfgets(buf, siz, fp, timeout, during)
12817c478bd9Sstevel@tonic-gate char *buf;
12827c478bd9Sstevel@tonic-gate int siz;
12837c478bd9Sstevel@tonic-gate SM_FILE_T *fp;
12847c478bd9Sstevel@tonic-gate time_t timeout;
12857c478bd9Sstevel@tonic-gate char *during;
12867c478bd9Sstevel@tonic-gate {
12877c478bd9Sstevel@tonic-gate register char *p;
12887c478bd9Sstevel@tonic-gate int save_errno;
12897c478bd9Sstevel@tonic-gate int io_timeout;
12907c478bd9Sstevel@tonic-gate
12917c478bd9Sstevel@tonic-gate SM_REQUIRE(siz > 0);
12927c478bd9Sstevel@tonic-gate SM_REQUIRE(buf != NULL);
12937c478bd9Sstevel@tonic-gate
12947c478bd9Sstevel@tonic-gate if (fp == NULL)
12957c478bd9Sstevel@tonic-gate {
12967c478bd9Sstevel@tonic-gate buf[0] = '\0';
12977c478bd9Sstevel@tonic-gate errno = EBADF;
12987c478bd9Sstevel@tonic-gate return NULL;
12997c478bd9Sstevel@tonic-gate }
13007c478bd9Sstevel@tonic-gate
13017c478bd9Sstevel@tonic-gate /* try to read */
13027c478bd9Sstevel@tonic-gate p = NULL;
13037c478bd9Sstevel@tonic-gate errno = 0;
13047c478bd9Sstevel@tonic-gate
13057c478bd9Sstevel@tonic-gate /* convert the timeout to sm_io notation */
13067c478bd9Sstevel@tonic-gate io_timeout = (timeout <= 0) ? SM_TIME_DEFAULT : timeout * 1000;
13077c478bd9Sstevel@tonic-gate while (!sm_io_eof(fp) && !sm_io_error(fp))
13087c478bd9Sstevel@tonic-gate {
13097c478bd9Sstevel@tonic-gate errno = 0;
13107c478bd9Sstevel@tonic-gate p = sm_io_fgets(fp, io_timeout, buf, siz);
13117c478bd9Sstevel@tonic-gate if (p == NULL && errno == EAGAIN)
13127c478bd9Sstevel@tonic-gate {
13137c478bd9Sstevel@tonic-gate /* The sm_io_fgets() call timedout */
13147c478bd9Sstevel@tonic-gate if (LogLevel > 1)
13157c478bd9Sstevel@tonic-gate sm_syslog(LOG_NOTICE, CurEnv->e_id,
13167c478bd9Sstevel@tonic-gate "timeout waiting for input from %.100s during %s",
13177c478bd9Sstevel@tonic-gate CURHOSTNAME,
13187c478bd9Sstevel@tonic-gate during);
13197c478bd9Sstevel@tonic-gate buf[0] = '\0';
13207c478bd9Sstevel@tonic-gate #if XDEBUG
13217c478bd9Sstevel@tonic-gate checkfd012(during);
13227c478bd9Sstevel@tonic-gate #endif /* XDEBUG */
13237c478bd9Sstevel@tonic-gate if (TrafficLogFile != NULL)
13247c478bd9Sstevel@tonic-gate (void) sm_io_fprintf(TrafficLogFile,
13257c478bd9Sstevel@tonic-gate SM_TIME_DEFAULT,
13267c478bd9Sstevel@tonic-gate "%05d <<< [TIMEOUT]\n",
13277c478bd9Sstevel@tonic-gate (int) CurrentPid);
13287c478bd9Sstevel@tonic-gate errno = ETIMEDOUT;
13297c478bd9Sstevel@tonic-gate return NULL;
13307c478bd9Sstevel@tonic-gate }
13317c478bd9Sstevel@tonic-gate if (p != NULL || errno != EINTR)
13327c478bd9Sstevel@tonic-gate break;
13337c478bd9Sstevel@tonic-gate (void) sm_io_clearerr(fp);
13347c478bd9Sstevel@tonic-gate }
13357c478bd9Sstevel@tonic-gate save_errno = errno;
13367c478bd9Sstevel@tonic-gate
13377c478bd9Sstevel@tonic-gate /* clean up the books and exit */
13387c478bd9Sstevel@tonic-gate LineNumber++;
13397c478bd9Sstevel@tonic-gate if (p == NULL)
13407c478bd9Sstevel@tonic-gate {
13417c478bd9Sstevel@tonic-gate buf[0] = '\0';
13427c478bd9Sstevel@tonic-gate if (TrafficLogFile != NULL)
13437c478bd9Sstevel@tonic-gate (void) sm_io_fprintf(TrafficLogFile, SM_TIME_DEFAULT,
13447c478bd9Sstevel@tonic-gate "%05d <<< [EOF]\n",
13457c478bd9Sstevel@tonic-gate (int) CurrentPid);
13467c478bd9Sstevel@tonic-gate errno = save_errno;
13477c478bd9Sstevel@tonic-gate return NULL;
13487c478bd9Sstevel@tonic-gate }
13497c478bd9Sstevel@tonic-gate if (TrafficLogFile != NULL)
13507c478bd9Sstevel@tonic-gate (void) sm_io_fprintf(TrafficLogFile, SM_TIME_DEFAULT,
13517c478bd9Sstevel@tonic-gate "%05d <<< %s", (int) CurrentPid, buf);
13527c478bd9Sstevel@tonic-gate if (SevenBitInput)
13537c478bd9Sstevel@tonic-gate {
13547c478bd9Sstevel@tonic-gate for (p = buf; *p != '\0'; p++)
13557c478bd9Sstevel@tonic-gate *p &= ~0200;
13567c478bd9Sstevel@tonic-gate }
13577c478bd9Sstevel@tonic-gate else if (!HasEightBits)
13587c478bd9Sstevel@tonic-gate {
13597c478bd9Sstevel@tonic-gate for (p = buf; *p != '\0'; p++)
13607c478bd9Sstevel@tonic-gate {
13617c478bd9Sstevel@tonic-gate if (bitset(0200, *p))
13627c478bd9Sstevel@tonic-gate {
13637c478bd9Sstevel@tonic-gate HasEightBits = true;
13647c478bd9Sstevel@tonic-gate break;
13657c478bd9Sstevel@tonic-gate }
13667c478bd9Sstevel@tonic-gate }
13677c478bd9Sstevel@tonic-gate }
13687c478bd9Sstevel@tonic-gate return buf;
13697c478bd9Sstevel@tonic-gate }
1370058561cbSjbeck
13717c478bd9Sstevel@tonic-gate /*
13727c478bd9Sstevel@tonic-gate ** FGETFOLDED -- like fgets, but knows about folded lines.
13737c478bd9Sstevel@tonic-gate **
13747c478bd9Sstevel@tonic-gate ** Parameters:
13757c478bd9Sstevel@tonic-gate ** buf -- place to put result.
1376058561cbSjbeck ** np -- pointer to bytes available; will be updated with
1377058561cbSjbeck ** the actual buffer size (not number of bytes filled)
1378058561cbSjbeck ** on return.
13797c478bd9Sstevel@tonic-gate ** f -- file to read from.
13807c478bd9Sstevel@tonic-gate **
13817c478bd9Sstevel@tonic-gate ** Returns:
13827c478bd9Sstevel@tonic-gate ** input line(s) on success, NULL on error or SM_IO_EOF.
13837c478bd9Sstevel@tonic-gate ** This will normally be buf -- unless the line is too
13847c478bd9Sstevel@tonic-gate ** long, when it will be sm_malloc_x()ed.
13857c478bd9Sstevel@tonic-gate **
13867c478bd9Sstevel@tonic-gate ** Side Effects:
13877c478bd9Sstevel@tonic-gate ** buf gets lines from f, with continuation lines (lines
13887c478bd9Sstevel@tonic-gate ** with leading white space) appended. CRLF's are mapped
13897c478bd9Sstevel@tonic-gate ** into single newlines. Any trailing NL is stripped.
13907c478bd9Sstevel@tonic-gate */
13917c478bd9Sstevel@tonic-gate
13927c478bd9Sstevel@tonic-gate char *
fgetfolded(buf,np,f)1393058561cbSjbeck fgetfolded(buf, np, f)
13947c478bd9Sstevel@tonic-gate char *buf;
1395058561cbSjbeck int *np;
13967c478bd9Sstevel@tonic-gate SM_FILE_T *f;
13977c478bd9Sstevel@tonic-gate {
13987c478bd9Sstevel@tonic-gate register char *p = buf;
13997c478bd9Sstevel@tonic-gate char *bp = buf;
14007c478bd9Sstevel@tonic-gate register int i;
1401058561cbSjbeck int n;
14027c478bd9Sstevel@tonic-gate
1403058561cbSjbeck SM_REQUIRE(np != NULL);
1404058561cbSjbeck n = *np;
14057c478bd9Sstevel@tonic-gate SM_REQUIRE(n > 0);
14067c478bd9Sstevel@tonic-gate SM_REQUIRE(buf != NULL);
14077c478bd9Sstevel@tonic-gate if (f == NULL)
14087c478bd9Sstevel@tonic-gate {
14097c478bd9Sstevel@tonic-gate buf[0] = '\0';
14107c478bd9Sstevel@tonic-gate errno = EBADF;
14117c478bd9Sstevel@tonic-gate return NULL;
14127c478bd9Sstevel@tonic-gate }
14137c478bd9Sstevel@tonic-gate
14147c478bd9Sstevel@tonic-gate n--;
14157c478bd9Sstevel@tonic-gate while ((i = sm_io_getc(f, SM_TIME_DEFAULT)) != SM_IO_EOF)
14167c478bd9Sstevel@tonic-gate {
14177c478bd9Sstevel@tonic-gate if (i == '\r')
14187c478bd9Sstevel@tonic-gate {
14197c478bd9Sstevel@tonic-gate i = sm_io_getc(f, SM_TIME_DEFAULT);
14207c478bd9Sstevel@tonic-gate if (i != '\n')
14217c478bd9Sstevel@tonic-gate {
14227c478bd9Sstevel@tonic-gate if (i != SM_IO_EOF)
14237c478bd9Sstevel@tonic-gate (void) sm_io_ungetc(f, SM_TIME_DEFAULT,
14247c478bd9Sstevel@tonic-gate i);
14257c478bd9Sstevel@tonic-gate i = '\r';
14267c478bd9Sstevel@tonic-gate }
14277c478bd9Sstevel@tonic-gate }
14287c478bd9Sstevel@tonic-gate if (--n <= 0)
14297c478bd9Sstevel@tonic-gate {
14307c478bd9Sstevel@tonic-gate /* allocate new space */
14317c478bd9Sstevel@tonic-gate char *nbp;
14327c478bd9Sstevel@tonic-gate int nn;
14337c478bd9Sstevel@tonic-gate
14347c478bd9Sstevel@tonic-gate nn = (p - bp);
14357c478bd9Sstevel@tonic-gate if (nn < MEMCHUNKSIZE)
14367c478bd9Sstevel@tonic-gate nn *= 2;
14377c478bd9Sstevel@tonic-gate else
14387c478bd9Sstevel@tonic-gate nn += MEMCHUNKSIZE;
14397c478bd9Sstevel@tonic-gate nbp = sm_malloc_x(nn);
14407c478bd9Sstevel@tonic-gate memmove(nbp, bp, p - bp);
14417c478bd9Sstevel@tonic-gate p = &nbp[p - bp];
14427c478bd9Sstevel@tonic-gate if (bp != buf)
14437c478bd9Sstevel@tonic-gate sm_free(bp);
14447c478bd9Sstevel@tonic-gate bp = nbp;
14457c478bd9Sstevel@tonic-gate n = nn - (p - bp);
1446058561cbSjbeck *np = nn;
14477c478bd9Sstevel@tonic-gate }
14487c478bd9Sstevel@tonic-gate *p++ = i;
14497c478bd9Sstevel@tonic-gate if (i == '\n')
14507c478bd9Sstevel@tonic-gate {
14517c478bd9Sstevel@tonic-gate LineNumber++;
14527c478bd9Sstevel@tonic-gate i = sm_io_getc(f, SM_TIME_DEFAULT);
14537c478bd9Sstevel@tonic-gate if (i != SM_IO_EOF)
14547c478bd9Sstevel@tonic-gate (void) sm_io_ungetc(f, SM_TIME_DEFAULT, i);
14557c478bd9Sstevel@tonic-gate if (i != ' ' && i != '\t')
14567c478bd9Sstevel@tonic-gate break;
14577c478bd9Sstevel@tonic-gate }
14587c478bd9Sstevel@tonic-gate }
14597c478bd9Sstevel@tonic-gate if (p == bp)
14607c478bd9Sstevel@tonic-gate return NULL;
14617c478bd9Sstevel@tonic-gate if (p[-1] == '\n')
14627c478bd9Sstevel@tonic-gate p--;
14637c478bd9Sstevel@tonic-gate *p = '\0';
14647c478bd9Sstevel@tonic-gate return bp;
14657c478bd9Sstevel@tonic-gate }
1466058561cbSjbeck
14677c478bd9Sstevel@tonic-gate /*
14687c478bd9Sstevel@tonic-gate ** CURTIME -- return current time.
14697c478bd9Sstevel@tonic-gate **
14707c478bd9Sstevel@tonic-gate ** Parameters:
14717c478bd9Sstevel@tonic-gate ** none.
14727c478bd9Sstevel@tonic-gate **
14737c478bd9Sstevel@tonic-gate ** Returns:
14747c478bd9Sstevel@tonic-gate ** the current time.
14757c478bd9Sstevel@tonic-gate */
14767c478bd9Sstevel@tonic-gate
14777c478bd9Sstevel@tonic-gate time_t
curtime()14787c478bd9Sstevel@tonic-gate curtime()
14797c478bd9Sstevel@tonic-gate {
14807c478bd9Sstevel@tonic-gate auto time_t t;
14817c478bd9Sstevel@tonic-gate
14827c478bd9Sstevel@tonic-gate (void) time(&t);
14837c478bd9Sstevel@tonic-gate return t;
14847c478bd9Sstevel@tonic-gate }
1485058561cbSjbeck
14867c478bd9Sstevel@tonic-gate /*
14877c478bd9Sstevel@tonic-gate ** ATOBOOL -- convert a string representation to boolean.
14887c478bd9Sstevel@tonic-gate **
14897c478bd9Sstevel@tonic-gate ** Defaults to false
14907c478bd9Sstevel@tonic-gate **
14917c478bd9Sstevel@tonic-gate ** Parameters:
14927c478bd9Sstevel@tonic-gate ** s -- string to convert. Takes "tTyY", empty, and NULL as true,
14937c478bd9Sstevel@tonic-gate ** others as false.
14947c478bd9Sstevel@tonic-gate **
14957c478bd9Sstevel@tonic-gate ** Returns:
14967c478bd9Sstevel@tonic-gate ** A boolean representation of the string.
14977c478bd9Sstevel@tonic-gate */
14987c478bd9Sstevel@tonic-gate
14997c478bd9Sstevel@tonic-gate bool
atobool(s)15007c478bd9Sstevel@tonic-gate atobool(s)
15017c478bd9Sstevel@tonic-gate register char *s;
15027c478bd9Sstevel@tonic-gate {
15037c478bd9Sstevel@tonic-gate if (s == NULL || *s == '\0' || strchr("tTyY", *s) != NULL)
15047c478bd9Sstevel@tonic-gate return true;
15057c478bd9Sstevel@tonic-gate return false;
15067c478bd9Sstevel@tonic-gate }
1507058561cbSjbeck
15087c478bd9Sstevel@tonic-gate /*
15097c478bd9Sstevel@tonic-gate ** ATOOCT -- convert a string representation to octal.
15107c478bd9Sstevel@tonic-gate **
15117c478bd9Sstevel@tonic-gate ** Parameters:
15127c478bd9Sstevel@tonic-gate ** s -- string to convert.
15137c478bd9Sstevel@tonic-gate **
15147c478bd9Sstevel@tonic-gate ** Returns:
15157c478bd9Sstevel@tonic-gate ** An integer representing the string interpreted as an
15167c478bd9Sstevel@tonic-gate ** octal number.
15177c478bd9Sstevel@tonic-gate */
15187c478bd9Sstevel@tonic-gate
15197c478bd9Sstevel@tonic-gate int
atooct(s)15207c478bd9Sstevel@tonic-gate atooct(s)
15217c478bd9Sstevel@tonic-gate register char *s;
15227c478bd9Sstevel@tonic-gate {
15237c478bd9Sstevel@tonic-gate register int i = 0;
15247c478bd9Sstevel@tonic-gate
15257c478bd9Sstevel@tonic-gate while (*s >= '0' && *s <= '7')
15267c478bd9Sstevel@tonic-gate i = (i << 3) | (*s++ - '0');
15277c478bd9Sstevel@tonic-gate return i;
15287c478bd9Sstevel@tonic-gate }
1529058561cbSjbeck
15307c478bd9Sstevel@tonic-gate /*
15317c478bd9Sstevel@tonic-gate ** BITINTERSECT -- tell if two bitmaps intersect
15327c478bd9Sstevel@tonic-gate **
15337c478bd9Sstevel@tonic-gate ** Parameters:
15347c478bd9Sstevel@tonic-gate ** a, b -- the bitmaps in question
15357c478bd9Sstevel@tonic-gate **
15367c478bd9Sstevel@tonic-gate ** Returns:
15377c478bd9Sstevel@tonic-gate ** true if they have a non-null intersection
15387c478bd9Sstevel@tonic-gate ** false otherwise
15397c478bd9Sstevel@tonic-gate */
15407c478bd9Sstevel@tonic-gate
15417c478bd9Sstevel@tonic-gate bool
bitintersect(a,b)15427c478bd9Sstevel@tonic-gate bitintersect(a, b)
15437c478bd9Sstevel@tonic-gate BITMAP256 a;
15447c478bd9Sstevel@tonic-gate BITMAP256 b;
15457c478bd9Sstevel@tonic-gate {
15467c478bd9Sstevel@tonic-gate int i;
15477c478bd9Sstevel@tonic-gate
1548058561cbSjbeck for (i = BITMAPBYTES / sizeof(int); --i >= 0; )
15497c478bd9Sstevel@tonic-gate {
15507c478bd9Sstevel@tonic-gate if ((a[i] & b[i]) != 0)
15517c478bd9Sstevel@tonic-gate return true;
15527c478bd9Sstevel@tonic-gate }
15537c478bd9Sstevel@tonic-gate return false;
15547c478bd9Sstevel@tonic-gate }
1555058561cbSjbeck
15567c478bd9Sstevel@tonic-gate /*
15577c478bd9Sstevel@tonic-gate ** BITZEROP -- tell if a bitmap is all zero
15587c478bd9Sstevel@tonic-gate **
15597c478bd9Sstevel@tonic-gate ** Parameters:
15607c478bd9Sstevel@tonic-gate ** map -- the bit map to check
15617c478bd9Sstevel@tonic-gate **
15627c478bd9Sstevel@tonic-gate ** Returns:
15637c478bd9Sstevel@tonic-gate ** true if map is all zero.
15647c478bd9Sstevel@tonic-gate ** false if there are any bits set in map.
15657c478bd9Sstevel@tonic-gate */
15667c478bd9Sstevel@tonic-gate
15677c478bd9Sstevel@tonic-gate bool
bitzerop(map)15687c478bd9Sstevel@tonic-gate bitzerop(map)
15697c478bd9Sstevel@tonic-gate BITMAP256 map;
15707c478bd9Sstevel@tonic-gate {
15717c478bd9Sstevel@tonic-gate int i;
15727c478bd9Sstevel@tonic-gate
1573058561cbSjbeck for (i = BITMAPBYTES / sizeof(int); --i >= 0; )
15747c478bd9Sstevel@tonic-gate {
15757c478bd9Sstevel@tonic-gate if (map[i] != 0)
15767c478bd9Sstevel@tonic-gate return false;
15777c478bd9Sstevel@tonic-gate }
15787c478bd9Sstevel@tonic-gate return true;
15797c478bd9Sstevel@tonic-gate }
1580058561cbSjbeck
15817c478bd9Sstevel@tonic-gate /*
15827c478bd9Sstevel@tonic-gate ** STRCONTAINEDIN -- tell if one string is contained in another
15837c478bd9Sstevel@tonic-gate **
15847c478bd9Sstevel@tonic-gate ** Parameters:
15857c478bd9Sstevel@tonic-gate ** icase -- ignore case?
15867c478bd9Sstevel@tonic-gate ** a -- possible substring.
15877c478bd9Sstevel@tonic-gate ** b -- possible superstring.
15887c478bd9Sstevel@tonic-gate **
15897c478bd9Sstevel@tonic-gate ** Returns:
15907c478bd9Sstevel@tonic-gate ** true if a is contained in b (case insensitive).
15917c478bd9Sstevel@tonic-gate ** false otherwise.
15927c478bd9Sstevel@tonic-gate */
15937c478bd9Sstevel@tonic-gate
15947c478bd9Sstevel@tonic-gate bool
strcontainedin(icase,a,b)15957c478bd9Sstevel@tonic-gate strcontainedin(icase, a, b)
15967c478bd9Sstevel@tonic-gate bool icase;
15977c478bd9Sstevel@tonic-gate register char *a;
15987c478bd9Sstevel@tonic-gate register char *b;
15997c478bd9Sstevel@tonic-gate {
16007c478bd9Sstevel@tonic-gate int la;
16017c478bd9Sstevel@tonic-gate int lb;
16027c478bd9Sstevel@tonic-gate int c;
16037c478bd9Sstevel@tonic-gate
16047c478bd9Sstevel@tonic-gate la = strlen(a);
16057c478bd9Sstevel@tonic-gate lb = strlen(b);
16067c478bd9Sstevel@tonic-gate c = *a;
16077c478bd9Sstevel@tonic-gate if (icase && isascii(c) && isupper(c))
16087c478bd9Sstevel@tonic-gate c = tolower(c);
16097c478bd9Sstevel@tonic-gate for (; lb-- >= la; b++)
16107c478bd9Sstevel@tonic-gate {
16117c478bd9Sstevel@tonic-gate if (icase)
16127c478bd9Sstevel@tonic-gate {
16137c478bd9Sstevel@tonic-gate if (*b != c &&
16147c478bd9Sstevel@tonic-gate isascii(*b) && isupper(*b) && tolower(*b) != c)
16157c478bd9Sstevel@tonic-gate continue;
16167c478bd9Sstevel@tonic-gate if (sm_strncasecmp(a, b, la) == 0)
16177c478bd9Sstevel@tonic-gate return true;
16187c478bd9Sstevel@tonic-gate }
16197c478bd9Sstevel@tonic-gate else
16207c478bd9Sstevel@tonic-gate {
16217c478bd9Sstevel@tonic-gate if (*b != c)
16227c478bd9Sstevel@tonic-gate continue;
16237c478bd9Sstevel@tonic-gate if (strncmp(a, b, la) == 0)
16247c478bd9Sstevel@tonic-gate return true;
16257c478bd9Sstevel@tonic-gate }
16267c478bd9Sstevel@tonic-gate }
16277c478bd9Sstevel@tonic-gate return false;
16287c478bd9Sstevel@tonic-gate }
1629058561cbSjbeck
16307c478bd9Sstevel@tonic-gate /*
16317c478bd9Sstevel@tonic-gate ** CHECKFD012 -- check low numbered file descriptors
16327c478bd9Sstevel@tonic-gate **
16337c478bd9Sstevel@tonic-gate ** File descriptors 0, 1, and 2 should be open at all times.
16347c478bd9Sstevel@tonic-gate ** This routine verifies that, and fixes it if not true.
16357c478bd9Sstevel@tonic-gate **
16367c478bd9Sstevel@tonic-gate ** Parameters:
16377c478bd9Sstevel@tonic-gate ** where -- a tag printed if the assertion failed
16387c478bd9Sstevel@tonic-gate **
16397c478bd9Sstevel@tonic-gate ** Returns:
16407c478bd9Sstevel@tonic-gate ** none
16417c478bd9Sstevel@tonic-gate */
16427c478bd9Sstevel@tonic-gate
16437c478bd9Sstevel@tonic-gate void
checkfd012(where)16447c478bd9Sstevel@tonic-gate checkfd012(where)
16457c478bd9Sstevel@tonic-gate char *where;
16467c478bd9Sstevel@tonic-gate {
16477c478bd9Sstevel@tonic-gate #if XDEBUG
16487c478bd9Sstevel@tonic-gate register int i;
16497c478bd9Sstevel@tonic-gate
16507c478bd9Sstevel@tonic-gate for (i = 0; i < 3; i++)
16517c478bd9Sstevel@tonic-gate fill_fd(i, where);
16527c478bd9Sstevel@tonic-gate #endif /* XDEBUG */
16537c478bd9Sstevel@tonic-gate }
1654058561cbSjbeck
16557c478bd9Sstevel@tonic-gate /*
16567c478bd9Sstevel@tonic-gate ** CHECKFDOPEN -- make sure file descriptor is open -- for extended debugging
16577c478bd9Sstevel@tonic-gate **
16587c478bd9Sstevel@tonic-gate ** Parameters:
16597c478bd9Sstevel@tonic-gate ** fd -- file descriptor to check.
16607c478bd9Sstevel@tonic-gate ** where -- tag to print on failure.
16617c478bd9Sstevel@tonic-gate **
16627c478bd9Sstevel@tonic-gate ** Returns:
16637c478bd9Sstevel@tonic-gate ** none.
16647c478bd9Sstevel@tonic-gate */
16657c478bd9Sstevel@tonic-gate
16667c478bd9Sstevel@tonic-gate void
checkfdopen(fd,where)16677c478bd9Sstevel@tonic-gate checkfdopen(fd, where)
16687c478bd9Sstevel@tonic-gate int fd;
16697c478bd9Sstevel@tonic-gate char *where;
16707c478bd9Sstevel@tonic-gate {
16717c478bd9Sstevel@tonic-gate #if XDEBUG
16727c478bd9Sstevel@tonic-gate struct stat st;
16737c478bd9Sstevel@tonic-gate
16747c478bd9Sstevel@tonic-gate if (fstat(fd, &st) < 0 && errno == EBADF)
16757c478bd9Sstevel@tonic-gate {
16767c478bd9Sstevel@tonic-gate syserr("checkfdopen(%d): %s not open as expected!", fd, where);
16777c478bd9Sstevel@tonic-gate printopenfds(true);
16787c478bd9Sstevel@tonic-gate }
16797c478bd9Sstevel@tonic-gate #endif /* XDEBUG */
16807c478bd9Sstevel@tonic-gate }
1681058561cbSjbeck
16827c478bd9Sstevel@tonic-gate /*
16837c478bd9Sstevel@tonic-gate ** CHECKFDS -- check for new or missing file descriptors
16847c478bd9Sstevel@tonic-gate **
16857c478bd9Sstevel@tonic-gate ** Parameters:
16867c478bd9Sstevel@tonic-gate ** where -- tag for printing. If null, take a base line.
16877c478bd9Sstevel@tonic-gate **
16887c478bd9Sstevel@tonic-gate ** Returns:
16897c478bd9Sstevel@tonic-gate ** none
16907c478bd9Sstevel@tonic-gate **
16917c478bd9Sstevel@tonic-gate ** Side Effects:
16927c478bd9Sstevel@tonic-gate ** If where is set, shows changes since the last call.
16937c478bd9Sstevel@tonic-gate */
16947c478bd9Sstevel@tonic-gate
16957c478bd9Sstevel@tonic-gate void
checkfds(where)16967c478bd9Sstevel@tonic-gate checkfds(where)
16977c478bd9Sstevel@tonic-gate char *where;
16987c478bd9Sstevel@tonic-gate {
16997c478bd9Sstevel@tonic-gate int maxfd;
17007c478bd9Sstevel@tonic-gate register int fd;
17017c478bd9Sstevel@tonic-gate bool printhdr = true;
17027c478bd9Sstevel@tonic-gate int save_errno = errno;
17037c478bd9Sstevel@tonic-gate static BITMAP256 baseline;
17047c478bd9Sstevel@tonic-gate extern int DtableSize;
17057c478bd9Sstevel@tonic-gate
17067c478bd9Sstevel@tonic-gate if (DtableSize > BITMAPBITS)
17077c478bd9Sstevel@tonic-gate maxfd = BITMAPBITS;
17087c478bd9Sstevel@tonic-gate else
17097c478bd9Sstevel@tonic-gate maxfd = DtableSize;
17107c478bd9Sstevel@tonic-gate if (where == NULL)
17117c478bd9Sstevel@tonic-gate clrbitmap(baseline);
17127c478bd9Sstevel@tonic-gate
17137c478bd9Sstevel@tonic-gate for (fd = 0; fd < maxfd; fd++)
17147c478bd9Sstevel@tonic-gate {
17157c478bd9Sstevel@tonic-gate struct stat stbuf;
17167c478bd9Sstevel@tonic-gate
17177c478bd9Sstevel@tonic-gate if (fstat(fd, &stbuf) < 0 && errno != EOPNOTSUPP)
17187c478bd9Sstevel@tonic-gate {
17197c478bd9Sstevel@tonic-gate if (!bitnset(fd, baseline))
17207c478bd9Sstevel@tonic-gate continue;
17217c478bd9Sstevel@tonic-gate clrbitn(fd, baseline);
17227c478bd9Sstevel@tonic-gate }
17237c478bd9Sstevel@tonic-gate else if (!bitnset(fd, baseline))
17247c478bd9Sstevel@tonic-gate setbitn(fd, baseline);
17257c478bd9Sstevel@tonic-gate else
17267c478bd9Sstevel@tonic-gate continue;
17277c478bd9Sstevel@tonic-gate
17287c478bd9Sstevel@tonic-gate /* file state has changed */
17297c478bd9Sstevel@tonic-gate if (where == NULL)
17307c478bd9Sstevel@tonic-gate continue;
17317c478bd9Sstevel@tonic-gate if (printhdr)
17327c478bd9Sstevel@tonic-gate {
17337c478bd9Sstevel@tonic-gate sm_syslog(LOG_DEBUG, CurEnv->e_id,
17347c478bd9Sstevel@tonic-gate "%s: changed fds:",
17357c478bd9Sstevel@tonic-gate where);
17367c478bd9Sstevel@tonic-gate printhdr = false;
17377c478bd9Sstevel@tonic-gate }
17387c478bd9Sstevel@tonic-gate dumpfd(fd, true, true);
17397c478bd9Sstevel@tonic-gate }
17407c478bd9Sstevel@tonic-gate errno = save_errno;
17417c478bd9Sstevel@tonic-gate }
1742058561cbSjbeck
17437c478bd9Sstevel@tonic-gate /*
17447c478bd9Sstevel@tonic-gate ** PRINTOPENFDS -- print the open file descriptors (for debugging)
17457c478bd9Sstevel@tonic-gate **
17467c478bd9Sstevel@tonic-gate ** Parameters:
17477c478bd9Sstevel@tonic-gate ** logit -- if set, send output to syslog; otherwise
17487c478bd9Sstevel@tonic-gate ** print for debugging.
17497c478bd9Sstevel@tonic-gate **
17507c478bd9Sstevel@tonic-gate ** Returns:
17517c478bd9Sstevel@tonic-gate ** none.
17527c478bd9Sstevel@tonic-gate */
17537c478bd9Sstevel@tonic-gate
17547c478bd9Sstevel@tonic-gate #if NETINET || NETINET6
17557c478bd9Sstevel@tonic-gate # include <arpa/inet.h>
17567c478bd9Sstevel@tonic-gate #endif /* NETINET || NETINET6 */
17577c478bd9Sstevel@tonic-gate
17587c478bd9Sstevel@tonic-gate void
printopenfds(logit)17597c478bd9Sstevel@tonic-gate printopenfds(logit)
17607c478bd9Sstevel@tonic-gate bool logit;
17617c478bd9Sstevel@tonic-gate {
17627c478bd9Sstevel@tonic-gate register int fd;
17637c478bd9Sstevel@tonic-gate extern int DtableSize;
17647c478bd9Sstevel@tonic-gate
17657c478bd9Sstevel@tonic-gate for (fd = 0; fd < DtableSize; fd++)
17667c478bd9Sstevel@tonic-gate dumpfd(fd, false, logit);
17677c478bd9Sstevel@tonic-gate }
1768058561cbSjbeck
17697c478bd9Sstevel@tonic-gate /*
17707c478bd9Sstevel@tonic-gate ** DUMPFD -- dump a file descriptor
17717c478bd9Sstevel@tonic-gate **
17727c478bd9Sstevel@tonic-gate ** Parameters:
17737c478bd9Sstevel@tonic-gate ** fd -- the file descriptor to dump.
17747c478bd9Sstevel@tonic-gate ** printclosed -- if set, print a notification even if
17757c478bd9Sstevel@tonic-gate ** it is closed; otherwise print nothing.
17767c478bd9Sstevel@tonic-gate ** logit -- if set, use sm_syslog instead of sm_dprintf()
17777c478bd9Sstevel@tonic-gate **
17787c478bd9Sstevel@tonic-gate ** Returns:
17797c478bd9Sstevel@tonic-gate ** none.
17807c478bd9Sstevel@tonic-gate */
17817c478bd9Sstevel@tonic-gate
17827c478bd9Sstevel@tonic-gate void
dumpfd(fd,printclosed,logit)17837c478bd9Sstevel@tonic-gate dumpfd(fd, printclosed, logit)
17847c478bd9Sstevel@tonic-gate int fd;
17857c478bd9Sstevel@tonic-gate bool printclosed;
17867c478bd9Sstevel@tonic-gate bool logit;
17877c478bd9Sstevel@tonic-gate {
17887c478bd9Sstevel@tonic-gate register char *p;
17897c478bd9Sstevel@tonic-gate char *hp;
17907c478bd9Sstevel@tonic-gate #ifdef S_IFSOCK
17917c478bd9Sstevel@tonic-gate SOCKADDR sa;
17927c478bd9Sstevel@tonic-gate #endif /* S_IFSOCK */
17937c478bd9Sstevel@tonic-gate auto SOCKADDR_LEN_T slen;
17947c478bd9Sstevel@tonic-gate int i;
17957c478bd9Sstevel@tonic-gate #if STAT64 > 0
17967c478bd9Sstevel@tonic-gate struct stat64 st;
17977c478bd9Sstevel@tonic-gate #else /* STAT64 > 0 */
17987c478bd9Sstevel@tonic-gate struct stat st;
17997c478bd9Sstevel@tonic-gate #endif /* STAT64 > 0 */
18007c478bd9Sstevel@tonic-gate char buf[200];
18017c478bd9Sstevel@tonic-gate
18027c478bd9Sstevel@tonic-gate p = buf;
18037c478bd9Sstevel@tonic-gate (void) sm_snprintf(p, SPACELEFT(buf, p), "%3d: ", fd);
18047c478bd9Sstevel@tonic-gate p += strlen(p);
18057c478bd9Sstevel@tonic-gate
18067c478bd9Sstevel@tonic-gate if (
18077c478bd9Sstevel@tonic-gate #if STAT64 > 0
18087c478bd9Sstevel@tonic-gate fstat64(fd, &st)
18097c478bd9Sstevel@tonic-gate #else /* STAT64 > 0 */
18107c478bd9Sstevel@tonic-gate fstat(fd, &st)
18117c478bd9Sstevel@tonic-gate #endif /* STAT64 > 0 */
18127c478bd9Sstevel@tonic-gate < 0)
18137c478bd9Sstevel@tonic-gate {
18147c478bd9Sstevel@tonic-gate if (errno != EBADF)
18157c478bd9Sstevel@tonic-gate {
18167c478bd9Sstevel@tonic-gate (void) sm_snprintf(p, SPACELEFT(buf, p),
18177c478bd9Sstevel@tonic-gate "CANNOT STAT (%s)",
18187c478bd9Sstevel@tonic-gate sm_errstring(errno));
18197c478bd9Sstevel@tonic-gate goto printit;
18207c478bd9Sstevel@tonic-gate }
18217c478bd9Sstevel@tonic-gate else if (printclosed)
18227c478bd9Sstevel@tonic-gate {
18237c478bd9Sstevel@tonic-gate (void) sm_snprintf(p, SPACELEFT(buf, p), "CLOSED");
18247c478bd9Sstevel@tonic-gate goto printit;
18257c478bd9Sstevel@tonic-gate }
18267c478bd9Sstevel@tonic-gate return;
18277c478bd9Sstevel@tonic-gate }
18287c478bd9Sstevel@tonic-gate
18297c478bd9Sstevel@tonic-gate i = fcntl(fd, F_GETFL, 0);
18307c478bd9Sstevel@tonic-gate if (i != -1)
18317c478bd9Sstevel@tonic-gate {
18327c478bd9Sstevel@tonic-gate (void) sm_snprintf(p, SPACELEFT(buf, p), "fl=0x%x, ", i);
18337c478bd9Sstevel@tonic-gate p += strlen(p);
18347c478bd9Sstevel@tonic-gate }
18357c478bd9Sstevel@tonic-gate
18367c478bd9Sstevel@tonic-gate (void) sm_snprintf(p, SPACELEFT(buf, p), "mode=%o: ",
18377c478bd9Sstevel@tonic-gate (int) st.st_mode);
18387c478bd9Sstevel@tonic-gate p += strlen(p);
18397c478bd9Sstevel@tonic-gate switch (st.st_mode & S_IFMT)
18407c478bd9Sstevel@tonic-gate {
18417c478bd9Sstevel@tonic-gate #ifdef S_IFSOCK
18427c478bd9Sstevel@tonic-gate case S_IFSOCK:
18437c478bd9Sstevel@tonic-gate (void) sm_snprintf(p, SPACELEFT(buf, p), "SOCK ");
18447c478bd9Sstevel@tonic-gate p += strlen(p);
1845058561cbSjbeck memset(&sa, '\0', sizeof(sa));
1846058561cbSjbeck slen = sizeof(sa);
18477c478bd9Sstevel@tonic-gate if (getsockname(fd, &sa.sa, &slen) < 0)
18487c478bd9Sstevel@tonic-gate (void) sm_snprintf(p, SPACELEFT(buf, p), "(%s)",
18497c478bd9Sstevel@tonic-gate sm_errstring(errno));
18507c478bd9Sstevel@tonic-gate else
18517c478bd9Sstevel@tonic-gate {
18527c478bd9Sstevel@tonic-gate hp = hostnamebyanyaddr(&sa);
18537c478bd9Sstevel@tonic-gate if (hp == NULL)
18547c478bd9Sstevel@tonic-gate {
18557c478bd9Sstevel@tonic-gate /* EMPTY */
18567c478bd9Sstevel@tonic-gate /* do nothing */
18577c478bd9Sstevel@tonic-gate }
18587c478bd9Sstevel@tonic-gate # if NETINET
18597c478bd9Sstevel@tonic-gate else if (sa.sa.sa_family == AF_INET)
18607c478bd9Sstevel@tonic-gate (void) sm_snprintf(p, SPACELEFT(buf, p),
18617c478bd9Sstevel@tonic-gate "%s/%d", hp, ntohs(sa.sin.sin_port));
18627c478bd9Sstevel@tonic-gate # endif /* NETINET */
18637c478bd9Sstevel@tonic-gate # if NETINET6
18647c478bd9Sstevel@tonic-gate else if (sa.sa.sa_family == AF_INET6)
18657c478bd9Sstevel@tonic-gate (void) sm_snprintf(p, SPACELEFT(buf, p),
18667c478bd9Sstevel@tonic-gate "%s/%d", hp, ntohs(sa.sin6.sin6_port));
18677c478bd9Sstevel@tonic-gate # endif /* NETINET6 */
18687c478bd9Sstevel@tonic-gate else
18697c478bd9Sstevel@tonic-gate (void) sm_snprintf(p, SPACELEFT(buf, p),
18707c478bd9Sstevel@tonic-gate "%s", hp);
18717c478bd9Sstevel@tonic-gate }
18727c478bd9Sstevel@tonic-gate p += strlen(p);
18737c478bd9Sstevel@tonic-gate (void) sm_snprintf(p, SPACELEFT(buf, p), "->");
18747c478bd9Sstevel@tonic-gate p += strlen(p);
1875058561cbSjbeck slen = sizeof(sa);
18767c478bd9Sstevel@tonic-gate if (getpeername(fd, &sa.sa, &slen) < 0)
18777c478bd9Sstevel@tonic-gate (void) sm_snprintf(p, SPACELEFT(buf, p), "(%s)",
18787c478bd9Sstevel@tonic-gate sm_errstring(errno));
18797c478bd9Sstevel@tonic-gate else
18807c478bd9Sstevel@tonic-gate {
18817c478bd9Sstevel@tonic-gate hp = hostnamebyanyaddr(&sa);
18827c478bd9Sstevel@tonic-gate if (hp == NULL)
18837c478bd9Sstevel@tonic-gate {
18847c478bd9Sstevel@tonic-gate /* EMPTY */
18857c478bd9Sstevel@tonic-gate /* do nothing */
18867c478bd9Sstevel@tonic-gate }
18877c478bd9Sstevel@tonic-gate # if NETINET
18887c478bd9Sstevel@tonic-gate else if (sa.sa.sa_family == AF_INET)
18897c478bd9Sstevel@tonic-gate (void) sm_snprintf(p, SPACELEFT(buf, p),
18907c478bd9Sstevel@tonic-gate "%s/%d", hp, ntohs(sa.sin.sin_port));
18917c478bd9Sstevel@tonic-gate # endif /* NETINET */
18927c478bd9Sstevel@tonic-gate # if NETINET6
18937c478bd9Sstevel@tonic-gate else if (sa.sa.sa_family == AF_INET6)
18947c478bd9Sstevel@tonic-gate (void) sm_snprintf(p, SPACELEFT(buf, p),
18957c478bd9Sstevel@tonic-gate "%s/%d", hp, ntohs(sa.sin6.sin6_port));
18967c478bd9Sstevel@tonic-gate # endif /* NETINET6 */
18977c478bd9Sstevel@tonic-gate else
18987c478bd9Sstevel@tonic-gate (void) sm_snprintf(p, SPACELEFT(buf, p),
18997c478bd9Sstevel@tonic-gate "%s", hp);
19007c478bd9Sstevel@tonic-gate }
19017c478bd9Sstevel@tonic-gate break;
19027c478bd9Sstevel@tonic-gate #endif /* S_IFSOCK */
19037c478bd9Sstevel@tonic-gate
19047c478bd9Sstevel@tonic-gate case S_IFCHR:
19057c478bd9Sstevel@tonic-gate (void) sm_snprintf(p, SPACELEFT(buf, p), "CHR: ");
19067c478bd9Sstevel@tonic-gate p += strlen(p);
19077c478bd9Sstevel@tonic-gate goto defprint;
19087c478bd9Sstevel@tonic-gate
19097c478bd9Sstevel@tonic-gate #ifdef S_IFBLK
19107c478bd9Sstevel@tonic-gate case S_IFBLK:
19117c478bd9Sstevel@tonic-gate (void) sm_snprintf(p, SPACELEFT(buf, p), "BLK: ");
19127c478bd9Sstevel@tonic-gate p += strlen(p);
19137c478bd9Sstevel@tonic-gate goto defprint;
19147c478bd9Sstevel@tonic-gate #endif /* S_IFBLK */
19157c478bd9Sstevel@tonic-gate
19167c478bd9Sstevel@tonic-gate #if defined(S_IFIFO) && (!defined(S_IFSOCK) || S_IFIFO != S_IFSOCK)
19177c478bd9Sstevel@tonic-gate case S_IFIFO:
19187c478bd9Sstevel@tonic-gate (void) sm_snprintf(p, SPACELEFT(buf, p), "FIFO: ");
19197c478bd9Sstevel@tonic-gate p += strlen(p);
19207c478bd9Sstevel@tonic-gate goto defprint;
19217c478bd9Sstevel@tonic-gate #endif /* defined(S_IFIFO) && (!defined(S_IFSOCK) || S_IFIFO != S_IFSOCK) */
19227c478bd9Sstevel@tonic-gate
19237c478bd9Sstevel@tonic-gate #ifdef S_IFDIR
19247c478bd9Sstevel@tonic-gate case S_IFDIR:
19257c478bd9Sstevel@tonic-gate (void) sm_snprintf(p, SPACELEFT(buf, p), "DIR: ");
19267c478bd9Sstevel@tonic-gate p += strlen(p);
19277c478bd9Sstevel@tonic-gate goto defprint;
19287c478bd9Sstevel@tonic-gate #endif /* S_IFDIR */
19297c478bd9Sstevel@tonic-gate
19307c478bd9Sstevel@tonic-gate #ifdef S_IFLNK
19317c478bd9Sstevel@tonic-gate case S_IFLNK:
19327c478bd9Sstevel@tonic-gate (void) sm_snprintf(p, SPACELEFT(buf, p), "LNK: ");
19337c478bd9Sstevel@tonic-gate p += strlen(p);
19347c478bd9Sstevel@tonic-gate goto defprint;
19357c478bd9Sstevel@tonic-gate #endif /* S_IFLNK */
19367c478bd9Sstevel@tonic-gate
19377c478bd9Sstevel@tonic-gate default:
19387c478bd9Sstevel@tonic-gate defprint:
19397c478bd9Sstevel@tonic-gate (void) sm_snprintf(p, SPACELEFT(buf, p),
19407c478bd9Sstevel@tonic-gate "dev=%d/%d, ino=%llu, nlink=%d, u/gid=%d/%d, ",
19417c478bd9Sstevel@tonic-gate major(st.st_dev), minor(st.st_dev),
19427c478bd9Sstevel@tonic-gate (ULONGLONG_T) st.st_ino,
19437c478bd9Sstevel@tonic-gate (int) st.st_nlink, (int) st.st_uid,
19447c478bd9Sstevel@tonic-gate (int) st.st_gid);
19457c478bd9Sstevel@tonic-gate p += strlen(p);
19467c478bd9Sstevel@tonic-gate (void) sm_snprintf(p, SPACELEFT(buf, p), "size=%llu",
19477c478bd9Sstevel@tonic-gate (ULONGLONG_T) st.st_size);
19487c478bd9Sstevel@tonic-gate break;
19497c478bd9Sstevel@tonic-gate }
19507c478bd9Sstevel@tonic-gate
19517c478bd9Sstevel@tonic-gate printit:
19527c478bd9Sstevel@tonic-gate if (logit)
19537c478bd9Sstevel@tonic-gate sm_syslog(LOG_DEBUG, CurEnv ? CurEnv->e_id : NULL,
19547c478bd9Sstevel@tonic-gate "%.800s", buf);
19557c478bd9Sstevel@tonic-gate else
19567c478bd9Sstevel@tonic-gate sm_dprintf("%s\n", buf);
19577c478bd9Sstevel@tonic-gate }
1958058561cbSjbeck
19597c478bd9Sstevel@tonic-gate /*
19607c478bd9Sstevel@tonic-gate ** SHORTEN_HOSTNAME -- strip local domain information off of hostname.
19617c478bd9Sstevel@tonic-gate **
19627c478bd9Sstevel@tonic-gate ** Parameters:
19637c478bd9Sstevel@tonic-gate ** host -- the host to shorten (stripped in place).
19647c478bd9Sstevel@tonic-gate **
19657c478bd9Sstevel@tonic-gate ** Returns:
19667c478bd9Sstevel@tonic-gate ** place where string was truncated, NULL if not truncated.
19677c478bd9Sstevel@tonic-gate */
19687c478bd9Sstevel@tonic-gate
19697c478bd9Sstevel@tonic-gate char *
shorten_hostname(host)19707c478bd9Sstevel@tonic-gate shorten_hostname(host)
19717c478bd9Sstevel@tonic-gate char host[];
19727c478bd9Sstevel@tonic-gate {
19737c478bd9Sstevel@tonic-gate register char *p;
19747c478bd9Sstevel@tonic-gate char *mydom;
19757c478bd9Sstevel@tonic-gate int i;
19767c478bd9Sstevel@tonic-gate bool canon = false;
19777c478bd9Sstevel@tonic-gate
19787c478bd9Sstevel@tonic-gate /* strip off final dot */
19797c478bd9Sstevel@tonic-gate i = strlen(host);
19807c478bd9Sstevel@tonic-gate p = &host[(i == 0) ? 0 : i - 1];
19817c478bd9Sstevel@tonic-gate if (*p == '.')
19827c478bd9Sstevel@tonic-gate {
19837c478bd9Sstevel@tonic-gate *p = '\0';
19847c478bd9Sstevel@tonic-gate canon = true;
19857c478bd9Sstevel@tonic-gate }
19867c478bd9Sstevel@tonic-gate
19877c478bd9Sstevel@tonic-gate /* see if there is any domain at all -- if not, we are done */
19887c478bd9Sstevel@tonic-gate p = strchr(host, '.');
19897c478bd9Sstevel@tonic-gate if (p == NULL)
19907c478bd9Sstevel@tonic-gate return NULL;
19917c478bd9Sstevel@tonic-gate
19927c478bd9Sstevel@tonic-gate /* yes, we have a domain -- see if it looks like us */
19937c478bd9Sstevel@tonic-gate mydom = macvalue('m', CurEnv);
19947c478bd9Sstevel@tonic-gate if (mydom == NULL)
19957c478bd9Sstevel@tonic-gate mydom = "";
19967c478bd9Sstevel@tonic-gate i = strlen(++p);
19977c478bd9Sstevel@tonic-gate if ((canon ? sm_strcasecmp(p, mydom)
19987c478bd9Sstevel@tonic-gate : sm_strncasecmp(p, mydom, i)) == 0 &&
19997c478bd9Sstevel@tonic-gate (mydom[i] == '.' || mydom[i] == '\0'))
20007c478bd9Sstevel@tonic-gate {
20017c478bd9Sstevel@tonic-gate *--p = '\0';
20027c478bd9Sstevel@tonic-gate return p;
20037c478bd9Sstevel@tonic-gate }
20047c478bd9Sstevel@tonic-gate return NULL;
20057c478bd9Sstevel@tonic-gate }
2006058561cbSjbeck
20077c478bd9Sstevel@tonic-gate /*
20087c478bd9Sstevel@tonic-gate ** PROG_OPEN -- open a program for reading
20097c478bd9Sstevel@tonic-gate **
20107c478bd9Sstevel@tonic-gate ** Parameters:
20117c478bd9Sstevel@tonic-gate ** argv -- the argument list.
20127c478bd9Sstevel@tonic-gate ** pfd -- pointer to a place to store the file descriptor.
20137c478bd9Sstevel@tonic-gate ** e -- the current envelope.
20147c478bd9Sstevel@tonic-gate **
20157c478bd9Sstevel@tonic-gate ** Returns:
20167c478bd9Sstevel@tonic-gate ** pid of the process -- -1 if it failed.
20177c478bd9Sstevel@tonic-gate */
20187c478bd9Sstevel@tonic-gate
20197c478bd9Sstevel@tonic-gate pid_t
prog_open(argv,pfd,e)20207c478bd9Sstevel@tonic-gate prog_open(argv, pfd, e)
20217c478bd9Sstevel@tonic-gate char **argv;
20227c478bd9Sstevel@tonic-gate int *pfd;
20237c478bd9Sstevel@tonic-gate ENVELOPE *e;
20247c478bd9Sstevel@tonic-gate {
20257c478bd9Sstevel@tonic-gate pid_t pid;
20267c478bd9Sstevel@tonic-gate int save_errno;
20277c478bd9Sstevel@tonic-gate int sff;
20287c478bd9Sstevel@tonic-gate int ret;
20297c478bd9Sstevel@tonic-gate int fdv[2];
20307c478bd9Sstevel@tonic-gate char *p, *q;
20317c478bd9Sstevel@tonic-gate char buf[MAXPATHLEN];
20327c478bd9Sstevel@tonic-gate extern int DtableSize;
20337c478bd9Sstevel@tonic-gate
20347c478bd9Sstevel@tonic-gate if (pipe(fdv) < 0)
20357c478bd9Sstevel@tonic-gate {
20367c478bd9Sstevel@tonic-gate syserr("%s: cannot create pipe for stdout", argv[0]);
20377c478bd9Sstevel@tonic-gate return -1;
20387c478bd9Sstevel@tonic-gate }
20397c478bd9Sstevel@tonic-gate pid = fork();
20407c478bd9Sstevel@tonic-gate if (pid < 0)
20417c478bd9Sstevel@tonic-gate {
20427c478bd9Sstevel@tonic-gate syserr("%s: cannot fork", argv[0]);
20437c478bd9Sstevel@tonic-gate (void) close(fdv[0]);
20447c478bd9Sstevel@tonic-gate (void) close(fdv[1]);
20457c478bd9Sstevel@tonic-gate return -1;
20467c478bd9Sstevel@tonic-gate }
20477c478bd9Sstevel@tonic-gate if (pid > 0)
20487c478bd9Sstevel@tonic-gate {
20497c478bd9Sstevel@tonic-gate /* parent */
20507c478bd9Sstevel@tonic-gate (void) close(fdv[1]);
20517c478bd9Sstevel@tonic-gate *pfd = fdv[0];
20527c478bd9Sstevel@tonic-gate return pid;
20537c478bd9Sstevel@tonic-gate }
20547c478bd9Sstevel@tonic-gate
20557c478bd9Sstevel@tonic-gate /* Reset global flags */
20567c478bd9Sstevel@tonic-gate RestartRequest = NULL;
20577c478bd9Sstevel@tonic-gate RestartWorkGroup = false;
20587c478bd9Sstevel@tonic-gate ShutdownRequest = NULL;
20597c478bd9Sstevel@tonic-gate PendingSignal = 0;
20607c478bd9Sstevel@tonic-gate CurrentPid = getpid();
20617c478bd9Sstevel@tonic-gate
20627c478bd9Sstevel@tonic-gate /*
20637c478bd9Sstevel@tonic-gate ** Initialize exception stack and default exception
20647c478bd9Sstevel@tonic-gate ** handler for child process.
20657c478bd9Sstevel@tonic-gate */
20667c478bd9Sstevel@tonic-gate
20677c478bd9Sstevel@tonic-gate sm_exc_newthread(fatal_error);
20687c478bd9Sstevel@tonic-gate
20697c478bd9Sstevel@tonic-gate /* child -- close stdin */
20707c478bd9Sstevel@tonic-gate (void) close(0);
20717c478bd9Sstevel@tonic-gate
20727c478bd9Sstevel@tonic-gate /* stdout goes back to parent */
20737c478bd9Sstevel@tonic-gate (void) close(fdv[0]);
20747c478bd9Sstevel@tonic-gate if (dup2(fdv[1], 1) < 0)
20757c478bd9Sstevel@tonic-gate {
20767c478bd9Sstevel@tonic-gate syserr("%s: cannot dup2 for stdout", argv[0]);
20777c478bd9Sstevel@tonic-gate _exit(EX_OSERR);
20787c478bd9Sstevel@tonic-gate }
20797c478bd9Sstevel@tonic-gate (void) close(fdv[1]);
20807c478bd9Sstevel@tonic-gate
20817c478bd9Sstevel@tonic-gate /* stderr goes to transcript if available */
20827c478bd9Sstevel@tonic-gate if (e->e_xfp != NULL)
20837c478bd9Sstevel@tonic-gate {
20847c478bd9Sstevel@tonic-gate int xfd;
20857c478bd9Sstevel@tonic-gate
20867c478bd9Sstevel@tonic-gate xfd = sm_io_getinfo(e->e_xfp, SM_IO_WHAT_FD, NULL);
20877c478bd9Sstevel@tonic-gate if (xfd >= 0 && dup2(xfd, 2) < 0)
20887c478bd9Sstevel@tonic-gate {
20897c478bd9Sstevel@tonic-gate syserr("%s: cannot dup2 for stderr", argv[0]);
20907c478bd9Sstevel@tonic-gate _exit(EX_OSERR);
20917c478bd9Sstevel@tonic-gate }
20927c478bd9Sstevel@tonic-gate }
20937c478bd9Sstevel@tonic-gate
20947c478bd9Sstevel@tonic-gate /* this process has no right to the queue file */
20957c478bd9Sstevel@tonic-gate if (e->e_lockfp != NULL)
20963ee0e492Sjbeck {
20973ee0e492Sjbeck int fd;
20983ee0e492Sjbeck
20993ee0e492Sjbeck fd = sm_io_getinfo(e->e_lockfp, SM_IO_WHAT_FD, NULL);
21003ee0e492Sjbeck if (fd >= 0)
21013ee0e492Sjbeck (void) close(fd);
21023ee0e492Sjbeck else
21033ee0e492Sjbeck syserr("%s: lockfp does not have a fd", argv[0]);
21043ee0e492Sjbeck }
21057c478bd9Sstevel@tonic-gate
21067c478bd9Sstevel@tonic-gate /* chroot to the program mailer directory, if defined */
21077c478bd9Sstevel@tonic-gate if (ProgMailer != NULL && ProgMailer->m_rootdir != NULL)
21087c478bd9Sstevel@tonic-gate {
2109058561cbSjbeck expand(ProgMailer->m_rootdir, buf, sizeof(buf), e);
21107c478bd9Sstevel@tonic-gate if (chroot(buf) < 0)
21117c478bd9Sstevel@tonic-gate {
21127c478bd9Sstevel@tonic-gate syserr("prog_open: cannot chroot(%s)", buf);
21137c478bd9Sstevel@tonic-gate exit(EX_TEMPFAIL);
21147c478bd9Sstevel@tonic-gate }
21157c478bd9Sstevel@tonic-gate if (chdir("/") < 0)
21167c478bd9Sstevel@tonic-gate {
21177c478bd9Sstevel@tonic-gate syserr("prog_open: cannot chdir(/)");
21187c478bd9Sstevel@tonic-gate exit(EX_TEMPFAIL);
21197c478bd9Sstevel@tonic-gate }
21207c478bd9Sstevel@tonic-gate }
21217c478bd9Sstevel@tonic-gate
21227c478bd9Sstevel@tonic-gate /* run as default user */
21237c478bd9Sstevel@tonic-gate endpwent();
21247c478bd9Sstevel@tonic-gate sm_mbdb_terminate();
2125445f2479Sjbeck #if _FFR_MEMSTAT
2126445f2479Sjbeck (void) sm_memstat_close();
2127445f2479Sjbeck #endif /* _FFR_MEMSTAT */
21287c478bd9Sstevel@tonic-gate if (setgid(DefGid) < 0 && geteuid() == 0)
21297c478bd9Sstevel@tonic-gate {
21307c478bd9Sstevel@tonic-gate syserr("prog_open: setgid(%ld) failed", (long) DefGid);
21317c478bd9Sstevel@tonic-gate exit(EX_TEMPFAIL);
21327c478bd9Sstevel@tonic-gate }
21337c478bd9Sstevel@tonic-gate if (setuid(DefUid) < 0 && geteuid() == 0)
21347c478bd9Sstevel@tonic-gate {
21357c478bd9Sstevel@tonic-gate syserr("prog_open: setuid(%ld) failed", (long) DefUid);
21367c478bd9Sstevel@tonic-gate exit(EX_TEMPFAIL);
21377c478bd9Sstevel@tonic-gate }
21387c478bd9Sstevel@tonic-gate
21397c478bd9Sstevel@tonic-gate /* run in some directory */
21407c478bd9Sstevel@tonic-gate if (ProgMailer != NULL)
21417c478bd9Sstevel@tonic-gate p = ProgMailer->m_execdir;
21427c478bd9Sstevel@tonic-gate else
21437c478bd9Sstevel@tonic-gate p = NULL;
21447c478bd9Sstevel@tonic-gate for (; p != NULL; p = q)
21457c478bd9Sstevel@tonic-gate {
21467c478bd9Sstevel@tonic-gate q = strchr(p, ':');
21477c478bd9Sstevel@tonic-gate if (q != NULL)
21487c478bd9Sstevel@tonic-gate *q = '\0';
2149058561cbSjbeck expand(p, buf, sizeof(buf), e);
21507c478bd9Sstevel@tonic-gate if (q != NULL)
21517c478bd9Sstevel@tonic-gate *q++ = ':';
21527c478bd9Sstevel@tonic-gate if (buf[0] != '\0' && chdir(buf) >= 0)
21537c478bd9Sstevel@tonic-gate break;
21547c478bd9Sstevel@tonic-gate }
21557c478bd9Sstevel@tonic-gate if (p == NULL)
21567c478bd9Sstevel@tonic-gate {
21577c478bd9Sstevel@tonic-gate /* backup directories */
21587c478bd9Sstevel@tonic-gate if (chdir("/tmp") < 0)
21597c478bd9Sstevel@tonic-gate (void) chdir("/");
21607c478bd9Sstevel@tonic-gate }
21617c478bd9Sstevel@tonic-gate
21627c478bd9Sstevel@tonic-gate /* Check safety of program to be run */
21637c478bd9Sstevel@tonic-gate sff = SFF_ROOTOK|SFF_EXECOK;
21647c478bd9Sstevel@tonic-gate if (!bitnset(DBS_RUNWRITABLEPROGRAM, DontBlameSendmail))
21657c478bd9Sstevel@tonic-gate sff |= SFF_NOGWFILES|SFF_NOWWFILES;
21667c478bd9Sstevel@tonic-gate if (bitnset(DBS_RUNPROGRAMINUNSAFEDIRPATH, DontBlameSendmail))
21677c478bd9Sstevel@tonic-gate sff |= SFF_NOPATHCHECK;
21687c478bd9Sstevel@tonic-gate else
21697c478bd9Sstevel@tonic-gate sff |= SFF_SAFEDIRPATH;
21707c478bd9Sstevel@tonic-gate ret = safefile(argv[0], DefUid, DefGid, DefUser, sff, 0, NULL);
21717c478bd9Sstevel@tonic-gate if (ret != 0)
21727c478bd9Sstevel@tonic-gate sm_syslog(LOG_INFO, e->e_id,
21737c478bd9Sstevel@tonic-gate "Warning: prog_open: program %s unsafe: %s",
21747c478bd9Sstevel@tonic-gate argv[0], sm_errstring(ret));
21757c478bd9Sstevel@tonic-gate
21767c478bd9Sstevel@tonic-gate /* arrange for all the files to be closed */
21777c478bd9Sstevel@tonic-gate sm_close_on_exec(STDERR_FILENO + 1, DtableSize);
21787c478bd9Sstevel@tonic-gate
21797c478bd9Sstevel@tonic-gate /* now exec the process */
21807c478bd9Sstevel@tonic-gate (void) execve(argv[0], (ARGV_T) argv, (ARGV_T) UserEnviron);
21817c478bd9Sstevel@tonic-gate
21827c478bd9Sstevel@tonic-gate /* woops! failed */
21837c478bd9Sstevel@tonic-gate save_errno = errno;
21847c478bd9Sstevel@tonic-gate syserr("%s: cannot exec", argv[0]);
21857c478bd9Sstevel@tonic-gate if (transienterror(save_errno))
21867c478bd9Sstevel@tonic-gate _exit(EX_OSERR);
21877c478bd9Sstevel@tonic-gate _exit(EX_CONFIG);
21887c478bd9Sstevel@tonic-gate return -1; /* avoid compiler warning on IRIX */
21897c478bd9Sstevel@tonic-gate }
2190058561cbSjbeck
21917c478bd9Sstevel@tonic-gate /*
21927c478bd9Sstevel@tonic-gate ** GET_COLUMN -- look up a Column in a line buffer
21937c478bd9Sstevel@tonic-gate **
21947c478bd9Sstevel@tonic-gate ** Parameters:
21957c478bd9Sstevel@tonic-gate ** line -- the raw text line to search.
21967c478bd9Sstevel@tonic-gate ** col -- the column number to fetch.
21977c478bd9Sstevel@tonic-gate ** delim -- the delimiter between columns. If null,
21987c478bd9Sstevel@tonic-gate ** use white space.
21997c478bd9Sstevel@tonic-gate ** buf -- the output buffer.
22007c478bd9Sstevel@tonic-gate ** buflen -- the length of buf.
22017c478bd9Sstevel@tonic-gate **
22027c478bd9Sstevel@tonic-gate ** Returns:
22037c478bd9Sstevel@tonic-gate ** buf if successful.
22047c478bd9Sstevel@tonic-gate ** NULL otherwise.
22057c478bd9Sstevel@tonic-gate */
22067c478bd9Sstevel@tonic-gate
22077c478bd9Sstevel@tonic-gate char *
get_column(line,col,delim,buf,buflen)22087c478bd9Sstevel@tonic-gate get_column(line, col, delim, buf, buflen)
22097c478bd9Sstevel@tonic-gate char line[];
22107c478bd9Sstevel@tonic-gate int col;
22117c478bd9Sstevel@tonic-gate int delim;
22127c478bd9Sstevel@tonic-gate char buf[];
22137c478bd9Sstevel@tonic-gate int buflen;
22147c478bd9Sstevel@tonic-gate {
22157c478bd9Sstevel@tonic-gate char *p;
22167c478bd9Sstevel@tonic-gate char *begin, *end;
22177c478bd9Sstevel@tonic-gate int i;
22187c478bd9Sstevel@tonic-gate char delimbuf[4];
22197c478bd9Sstevel@tonic-gate
22207c478bd9Sstevel@tonic-gate if ((char) delim == '\0')
2221058561cbSjbeck (void) sm_strlcpy(delimbuf, "\n\t ", sizeof(delimbuf));
22227c478bd9Sstevel@tonic-gate else
22237c478bd9Sstevel@tonic-gate {
22247c478bd9Sstevel@tonic-gate delimbuf[0] = (char) delim;
22257c478bd9Sstevel@tonic-gate delimbuf[1] = '\0';
22267c478bd9Sstevel@tonic-gate }
22277c478bd9Sstevel@tonic-gate
22287c478bd9Sstevel@tonic-gate p = line;
22297c478bd9Sstevel@tonic-gate if (*p == '\0')
22307c478bd9Sstevel@tonic-gate return NULL; /* line empty */
22317c478bd9Sstevel@tonic-gate if (*p == (char) delim && col == 0)
22327c478bd9Sstevel@tonic-gate return NULL; /* first column empty */
22337c478bd9Sstevel@tonic-gate
22347c478bd9Sstevel@tonic-gate begin = line;
22357c478bd9Sstevel@tonic-gate
22367c478bd9Sstevel@tonic-gate if (col == 0 && (char) delim == '\0')
22377c478bd9Sstevel@tonic-gate {
22387c478bd9Sstevel@tonic-gate while (*begin != '\0' && isascii(*begin) && isspace(*begin))
22397c478bd9Sstevel@tonic-gate begin++;
22407c478bd9Sstevel@tonic-gate }
22417c478bd9Sstevel@tonic-gate
22427c478bd9Sstevel@tonic-gate for (i = 0; i < col; i++)
22437c478bd9Sstevel@tonic-gate {
22447c478bd9Sstevel@tonic-gate if ((begin = strpbrk(begin, delimbuf)) == NULL)
22457c478bd9Sstevel@tonic-gate return NULL; /* no such column */
22467c478bd9Sstevel@tonic-gate begin++;
22477c478bd9Sstevel@tonic-gate if ((char) delim == '\0')
22487c478bd9Sstevel@tonic-gate {
22497c478bd9Sstevel@tonic-gate while (*begin != '\0' && isascii(*begin) && isspace(*begin))
22507c478bd9Sstevel@tonic-gate begin++;
22517c478bd9Sstevel@tonic-gate }
22527c478bd9Sstevel@tonic-gate }
22537c478bd9Sstevel@tonic-gate
22547c478bd9Sstevel@tonic-gate end = strpbrk(begin, delimbuf);
22557c478bd9Sstevel@tonic-gate if (end == NULL)
22567c478bd9Sstevel@tonic-gate i = strlen(begin);
22577c478bd9Sstevel@tonic-gate else
22587c478bd9Sstevel@tonic-gate i = end - begin;
22597c478bd9Sstevel@tonic-gate if (i >= buflen)
22607c478bd9Sstevel@tonic-gate i = buflen - 1;
22617c478bd9Sstevel@tonic-gate (void) sm_strlcpy(buf, begin, i + 1);
22627c478bd9Sstevel@tonic-gate return buf;
22637c478bd9Sstevel@tonic-gate }
2264058561cbSjbeck
22657c478bd9Sstevel@tonic-gate /*
22667c478bd9Sstevel@tonic-gate ** CLEANSTRCPY -- copy string keeping out bogus characters
22677c478bd9Sstevel@tonic-gate **
22687c478bd9Sstevel@tonic-gate ** Parameters:
22697c478bd9Sstevel@tonic-gate ** t -- "to" string.
22707c478bd9Sstevel@tonic-gate ** f -- "from" string.
22717c478bd9Sstevel@tonic-gate ** l -- length of space available in "to" string.
22727c478bd9Sstevel@tonic-gate **
22737c478bd9Sstevel@tonic-gate ** Returns:
22747c478bd9Sstevel@tonic-gate ** none.
22757c478bd9Sstevel@tonic-gate */
22767c478bd9Sstevel@tonic-gate
22777c478bd9Sstevel@tonic-gate void
cleanstrcpy(t,f,l)22787c478bd9Sstevel@tonic-gate cleanstrcpy(t, f, l)
22797c478bd9Sstevel@tonic-gate register char *t;
22807c478bd9Sstevel@tonic-gate register char *f;
22817c478bd9Sstevel@tonic-gate int l;
22827c478bd9Sstevel@tonic-gate {
22837c478bd9Sstevel@tonic-gate /* check for newlines and log if necessary */
22847c478bd9Sstevel@tonic-gate (void) denlstring(f, true, true);
22857c478bd9Sstevel@tonic-gate
22867c478bd9Sstevel@tonic-gate if (l <= 0)
22877c478bd9Sstevel@tonic-gate syserr("!cleanstrcpy: length == 0");
22887c478bd9Sstevel@tonic-gate
22897c478bd9Sstevel@tonic-gate l--;
22907c478bd9Sstevel@tonic-gate while (l > 0 && *f != '\0')
22917c478bd9Sstevel@tonic-gate {
22927c478bd9Sstevel@tonic-gate if (isascii(*f) &&
22937c478bd9Sstevel@tonic-gate (isalnum(*f) || strchr("!#$%&'*+-./^_`{|}~", *f) != NULL))
22947c478bd9Sstevel@tonic-gate {
22957c478bd9Sstevel@tonic-gate l--;
22967c478bd9Sstevel@tonic-gate *t++ = *f;
22977c478bd9Sstevel@tonic-gate }
22987c478bd9Sstevel@tonic-gate f++;
22997c478bd9Sstevel@tonic-gate }
23007c478bd9Sstevel@tonic-gate *t = '\0';
23017c478bd9Sstevel@tonic-gate }
2302058561cbSjbeck
23037c478bd9Sstevel@tonic-gate /*
23047c478bd9Sstevel@tonic-gate ** DENLSTRING -- convert newlines in a string to spaces
23057c478bd9Sstevel@tonic-gate **
23067c478bd9Sstevel@tonic-gate ** Parameters:
23077c478bd9Sstevel@tonic-gate ** s -- the input string
23087c478bd9Sstevel@tonic-gate ** strict -- if set, don't permit continuation lines.
23097c478bd9Sstevel@tonic-gate ** logattacks -- if set, log attempted attacks.
23107c478bd9Sstevel@tonic-gate **
23117c478bd9Sstevel@tonic-gate ** Returns:
23127c478bd9Sstevel@tonic-gate ** A pointer to a version of the string with newlines
23137c478bd9Sstevel@tonic-gate ** mapped to spaces. This should be copied.
23147c478bd9Sstevel@tonic-gate */
23157c478bd9Sstevel@tonic-gate
23167c478bd9Sstevel@tonic-gate char *
denlstring(s,strict,logattacks)23177c478bd9Sstevel@tonic-gate denlstring(s, strict, logattacks)
23187c478bd9Sstevel@tonic-gate char *s;
23197c478bd9Sstevel@tonic-gate bool strict;
23207c478bd9Sstevel@tonic-gate bool logattacks;
23217c478bd9Sstevel@tonic-gate {
23227c478bd9Sstevel@tonic-gate register char *p;
23237c478bd9Sstevel@tonic-gate int l;
23247c478bd9Sstevel@tonic-gate static char *bp = NULL;
23257c478bd9Sstevel@tonic-gate static int bl = 0;
23267c478bd9Sstevel@tonic-gate
23277c478bd9Sstevel@tonic-gate p = s;
23287c478bd9Sstevel@tonic-gate while ((p = strchr(p, '\n')) != NULL)
23297c478bd9Sstevel@tonic-gate if (strict || (*++p != ' ' && *p != '\t'))
23307c478bd9Sstevel@tonic-gate break;
23317c478bd9Sstevel@tonic-gate if (p == NULL)
23327c478bd9Sstevel@tonic-gate return s;
23337c478bd9Sstevel@tonic-gate
23347c478bd9Sstevel@tonic-gate l = strlen(s) + 1;
23357c478bd9Sstevel@tonic-gate if (bl < l)
23367c478bd9Sstevel@tonic-gate {
23377c478bd9Sstevel@tonic-gate /* allocate more space */
23387c478bd9Sstevel@tonic-gate char *nbp = sm_pmalloc_x(l);
23397c478bd9Sstevel@tonic-gate
23407c478bd9Sstevel@tonic-gate if (bp != NULL)
23417c478bd9Sstevel@tonic-gate sm_free(bp);
23427c478bd9Sstevel@tonic-gate bp = nbp;
23437c478bd9Sstevel@tonic-gate bl = l;
23447c478bd9Sstevel@tonic-gate }
23457c478bd9Sstevel@tonic-gate (void) sm_strlcpy(bp, s, l);
23467c478bd9Sstevel@tonic-gate for (p = bp; (p = strchr(p, '\n')) != NULL; )
23477c478bd9Sstevel@tonic-gate *p++ = ' ';
23487c478bd9Sstevel@tonic-gate
23497c478bd9Sstevel@tonic-gate if (logattacks)
23507c478bd9Sstevel@tonic-gate {
2351058561cbSjbeck sm_syslog(LOG_NOTICE, CurEnv ? CurEnv->e_id : NULL,
23527c478bd9Sstevel@tonic-gate "POSSIBLE ATTACK from %.100s: newline in string \"%s\"",
23537c478bd9Sstevel@tonic-gate RealHostName == NULL ? "[UNKNOWN]" : RealHostName,
23547c478bd9Sstevel@tonic-gate shortenstring(bp, MAXSHORTSTR));
23557c478bd9Sstevel@tonic-gate }
23567c478bd9Sstevel@tonic-gate
23577c478bd9Sstevel@tonic-gate return bp;
23587c478bd9Sstevel@tonic-gate }
23597c478bd9Sstevel@tonic-gate
23607c478bd9Sstevel@tonic-gate /*
23617c478bd9Sstevel@tonic-gate ** STRREPLNONPRT -- replace "unprintable" characters in a string with subst
23627c478bd9Sstevel@tonic-gate **
23637c478bd9Sstevel@tonic-gate ** Parameters:
23647c478bd9Sstevel@tonic-gate ** s -- string to manipulate (in place)
23657c478bd9Sstevel@tonic-gate ** subst -- character to use as replacement
23667c478bd9Sstevel@tonic-gate **
23677c478bd9Sstevel@tonic-gate ** Returns:
23687c478bd9Sstevel@tonic-gate ** true iff string did not contain "unprintable" characters
23697c478bd9Sstevel@tonic-gate */
23707c478bd9Sstevel@tonic-gate
23717c478bd9Sstevel@tonic-gate bool
strreplnonprt(s,c)23727c478bd9Sstevel@tonic-gate strreplnonprt(s, c)
23737c478bd9Sstevel@tonic-gate char *s;
23747c478bd9Sstevel@tonic-gate int c;
23757c478bd9Sstevel@tonic-gate {
23767c478bd9Sstevel@tonic-gate bool ok;
23777c478bd9Sstevel@tonic-gate
23787c478bd9Sstevel@tonic-gate ok = true;
23797c478bd9Sstevel@tonic-gate if (s == NULL)
23807c478bd9Sstevel@tonic-gate return ok;
23817c478bd9Sstevel@tonic-gate while (*s != '\0')
23827c478bd9Sstevel@tonic-gate {
23837c478bd9Sstevel@tonic-gate if (!(isascii(*s) && isprint(*s)))
23847c478bd9Sstevel@tonic-gate {
23857c478bd9Sstevel@tonic-gate *s = c;
23867c478bd9Sstevel@tonic-gate ok = false;
23877c478bd9Sstevel@tonic-gate }
23887c478bd9Sstevel@tonic-gate ++s;
23897c478bd9Sstevel@tonic-gate }
23907c478bd9Sstevel@tonic-gate return ok;
23917c478bd9Sstevel@tonic-gate }
23927c478bd9Sstevel@tonic-gate
23937c478bd9Sstevel@tonic-gate /*
23947c478bd9Sstevel@tonic-gate ** PATH_IS_DIR -- check to see if file exists and is a directory.
23957c478bd9Sstevel@tonic-gate **
23967c478bd9Sstevel@tonic-gate ** There are some additional checks for security violations in
23977c478bd9Sstevel@tonic-gate ** here. This routine is intended to be used for the host status
23987c478bd9Sstevel@tonic-gate ** support.
23997c478bd9Sstevel@tonic-gate **
24007c478bd9Sstevel@tonic-gate ** Parameters:
24017c478bd9Sstevel@tonic-gate ** pathname -- pathname to check for directory-ness.
24027c478bd9Sstevel@tonic-gate ** createflag -- if set, create directory if needed.
24037c478bd9Sstevel@tonic-gate **
24047c478bd9Sstevel@tonic-gate ** Returns:
24057c478bd9Sstevel@tonic-gate ** true -- if the indicated pathname is a directory
24067c478bd9Sstevel@tonic-gate ** false -- otherwise
24077c478bd9Sstevel@tonic-gate */
24087c478bd9Sstevel@tonic-gate
24097c478bd9Sstevel@tonic-gate bool
path_is_dir(pathname,createflag)24107c478bd9Sstevel@tonic-gate path_is_dir(pathname, createflag)
24117c478bd9Sstevel@tonic-gate char *pathname;
24127c478bd9Sstevel@tonic-gate bool createflag;
24137c478bd9Sstevel@tonic-gate {
24147c478bd9Sstevel@tonic-gate struct stat statbuf;
24157c478bd9Sstevel@tonic-gate
24167c478bd9Sstevel@tonic-gate #if HASLSTAT
24177c478bd9Sstevel@tonic-gate if (lstat(pathname, &statbuf) < 0)
24187c478bd9Sstevel@tonic-gate #else /* HASLSTAT */
24197c478bd9Sstevel@tonic-gate if (stat(pathname, &statbuf) < 0)
24207c478bd9Sstevel@tonic-gate #endif /* HASLSTAT */
24217c478bd9Sstevel@tonic-gate {
24227c478bd9Sstevel@tonic-gate if (errno != ENOENT || !createflag)
24237c478bd9Sstevel@tonic-gate return false;
24247c478bd9Sstevel@tonic-gate if (mkdir(pathname, 0755) < 0)
24257c478bd9Sstevel@tonic-gate return false;
24267c478bd9Sstevel@tonic-gate return true;
24277c478bd9Sstevel@tonic-gate }
24287c478bd9Sstevel@tonic-gate if (!S_ISDIR(statbuf.st_mode))
24297c478bd9Sstevel@tonic-gate {
24307c478bd9Sstevel@tonic-gate errno = ENOTDIR;
24317c478bd9Sstevel@tonic-gate return false;
24327c478bd9Sstevel@tonic-gate }
24337c478bd9Sstevel@tonic-gate
24347c478bd9Sstevel@tonic-gate /* security: don't allow writable directories */
24357c478bd9Sstevel@tonic-gate if (bitset(S_IWGRP|S_IWOTH, statbuf.st_mode))
24367c478bd9Sstevel@tonic-gate {
24377c478bd9Sstevel@tonic-gate errno = EACCES;
24387c478bd9Sstevel@tonic-gate return false;
24397c478bd9Sstevel@tonic-gate }
24407c478bd9Sstevel@tonic-gate return true;
24417c478bd9Sstevel@tonic-gate }
2442058561cbSjbeck
24437c478bd9Sstevel@tonic-gate /*
24447c478bd9Sstevel@tonic-gate ** PROC_LIST_ADD -- add process id to list of our children
24457c478bd9Sstevel@tonic-gate **
24467c478bd9Sstevel@tonic-gate ** Parameters:
24477c478bd9Sstevel@tonic-gate ** pid -- pid to add to list.
24487c478bd9Sstevel@tonic-gate ** task -- task of pid.
24497c478bd9Sstevel@tonic-gate ** type -- type of process.
24507c478bd9Sstevel@tonic-gate ** count -- number of processes.
24517c478bd9Sstevel@tonic-gate ** other -- other information for this type.
24527c478bd9Sstevel@tonic-gate **
24537c478bd9Sstevel@tonic-gate ** Returns:
24547c478bd9Sstevel@tonic-gate ** none
24557c478bd9Sstevel@tonic-gate **
24567c478bd9Sstevel@tonic-gate ** Side Effects:
24577c478bd9Sstevel@tonic-gate ** May increase CurChildren. May grow ProcList.
24587c478bd9Sstevel@tonic-gate */
24597c478bd9Sstevel@tonic-gate
24607c478bd9Sstevel@tonic-gate typedef struct procs PROCS_T;
24617c478bd9Sstevel@tonic-gate
24627c478bd9Sstevel@tonic-gate struct procs
24637c478bd9Sstevel@tonic-gate {
24647c478bd9Sstevel@tonic-gate pid_t proc_pid;
24657c478bd9Sstevel@tonic-gate char *proc_task;
24667c478bd9Sstevel@tonic-gate int proc_type;
24677c478bd9Sstevel@tonic-gate int proc_count;
24687c478bd9Sstevel@tonic-gate int proc_other;
24697c478bd9Sstevel@tonic-gate SOCKADDR proc_hostaddr;
24707c478bd9Sstevel@tonic-gate };
24717c478bd9Sstevel@tonic-gate
24727c478bd9Sstevel@tonic-gate static PROCS_T *volatile ProcListVec = NULL;
24737c478bd9Sstevel@tonic-gate static int ProcListSize = 0;
24747c478bd9Sstevel@tonic-gate
24757c478bd9Sstevel@tonic-gate void
proc_list_add(pid,task,type,count,other,hostaddr)24767c478bd9Sstevel@tonic-gate proc_list_add(pid, task, type, count, other, hostaddr)
24777c478bd9Sstevel@tonic-gate pid_t pid;
24787c478bd9Sstevel@tonic-gate char *task;
24797c478bd9Sstevel@tonic-gate int type;
24807c478bd9Sstevel@tonic-gate int count;
24817c478bd9Sstevel@tonic-gate int other;
24827c478bd9Sstevel@tonic-gate SOCKADDR *hostaddr;
24837c478bd9Sstevel@tonic-gate {
24847c478bd9Sstevel@tonic-gate int i;
24857c478bd9Sstevel@tonic-gate
24867c478bd9Sstevel@tonic-gate for (i = 0; i < ProcListSize; i++)
24877c478bd9Sstevel@tonic-gate {
24887c478bd9Sstevel@tonic-gate if (ProcListVec[i].proc_pid == NO_PID)
24897c478bd9Sstevel@tonic-gate break;
24907c478bd9Sstevel@tonic-gate }
24917c478bd9Sstevel@tonic-gate if (i >= ProcListSize)
24927c478bd9Sstevel@tonic-gate {
24937c478bd9Sstevel@tonic-gate /* probe the existing vector to avoid growing infinitely */
24947c478bd9Sstevel@tonic-gate proc_list_probe();
24957c478bd9Sstevel@tonic-gate
24967c478bd9Sstevel@tonic-gate /* now scan again */
24977c478bd9Sstevel@tonic-gate for (i = 0; i < ProcListSize; i++)
24987c478bd9Sstevel@tonic-gate {
24997c478bd9Sstevel@tonic-gate if (ProcListVec[i].proc_pid == NO_PID)
25007c478bd9Sstevel@tonic-gate break;
25017c478bd9Sstevel@tonic-gate }
25027c478bd9Sstevel@tonic-gate }
25037c478bd9Sstevel@tonic-gate if (i >= ProcListSize)
25047c478bd9Sstevel@tonic-gate {
25057c478bd9Sstevel@tonic-gate /* grow process list */
2506058561cbSjbeck int chldwasblocked;
25077c478bd9Sstevel@tonic-gate PROCS_T *npv;
25087c478bd9Sstevel@tonic-gate
25097c478bd9Sstevel@tonic-gate SM_ASSERT(ProcListSize < INT_MAX - PROC_LIST_SEG);
2510058561cbSjbeck npv = (PROCS_T *) sm_pmalloc_x((sizeof(*npv)) *
25117c478bd9Sstevel@tonic-gate (ProcListSize + PROC_LIST_SEG));
2512058561cbSjbeck
2513058561cbSjbeck /* Block SIGCHLD so reapchild() doesn't mess with us */
2514058561cbSjbeck chldwasblocked = sm_blocksignal(SIGCHLD);
25157c478bd9Sstevel@tonic-gate if (ProcListSize > 0)
25167c478bd9Sstevel@tonic-gate {
25177c478bd9Sstevel@tonic-gate memmove(npv, ProcListVec,
2518058561cbSjbeck ProcListSize * sizeof(PROCS_T));
25197c478bd9Sstevel@tonic-gate sm_free(ProcListVec);
25207c478bd9Sstevel@tonic-gate }
25217c478bd9Sstevel@tonic-gate
25227c478bd9Sstevel@tonic-gate /* XXX just use memset() to initialize this part? */
25237c478bd9Sstevel@tonic-gate for (i = ProcListSize; i < ProcListSize + PROC_LIST_SEG; i++)
25247c478bd9Sstevel@tonic-gate {
25257c478bd9Sstevel@tonic-gate npv[i].proc_pid = NO_PID;
25267c478bd9Sstevel@tonic-gate npv[i].proc_task = NULL;
25277c478bd9Sstevel@tonic-gate npv[i].proc_type = PROC_NONE;
25287c478bd9Sstevel@tonic-gate }
25297c478bd9Sstevel@tonic-gate i = ProcListSize;
25307c478bd9Sstevel@tonic-gate ProcListSize += PROC_LIST_SEG;
25317c478bd9Sstevel@tonic-gate ProcListVec = npv;
2532058561cbSjbeck if (chldwasblocked == 0)
2533058561cbSjbeck (void) sm_releasesignal(SIGCHLD);
25347c478bd9Sstevel@tonic-gate }
25357c478bd9Sstevel@tonic-gate ProcListVec[i].proc_pid = pid;
25367c478bd9Sstevel@tonic-gate PSTRSET(ProcListVec[i].proc_task, task);
25377c478bd9Sstevel@tonic-gate ProcListVec[i].proc_type = type;
25387c478bd9Sstevel@tonic-gate ProcListVec[i].proc_count = count;
25397c478bd9Sstevel@tonic-gate ProcListVec[i].proc_other = other;
25407c478bd9Sstevel@tonic-gate if (hostaddr != NULL)
25417c478bd9Sstevel@tonic-gate ProcListVec[i].proc_hostaddr = *hostaddr;
25427c478bd9Sstevel@tonic-gate else
25437c478bd9Sstevel@tonic-gate memset(&ProcListVec[i].proc_hostaddr, 0,
25447c478bd9Sstevel@tonic-gate sizeof(ProcListVec[i].proc_hostaddr));
25457c478bd9Sstevel@tonic-gate
25467c478bd9Sstevel@tonic-gate /* if process adding itself, it's not a child */
25477c478bd9Sstevel@tonic-gate if (pid != CurrentPid)
25487c478bd9Sstevel@tonic-gate {
25497c478bd9Sstevel@tonic-gate SM_ASSERT(CurChildren < INT_MAX);
25507c478bd9Sstevel@tonic-gate CurChildren++;
25517c478bd9Sstevel@tonic-gate }
25527c478bd9Sstevel@tonic-gate }
2553058561cbSjbeck
25547c478bd9Sstevel@tonic-gate /*
25557c478bd9Sstevel@tonic-gate ** PROC_LIST_SET -- set pid task in process list
25567c478bd9Sstevel@tonic-gate **
25577c478bd9Sstevel@tonic-gate ** Parameters:
25587c478bd9Sstevel@tonic-gate ** pid -- pid to set
25597c478bd9Sstevel@tonic-gate ** task -- task of pid
25607c478bd9Sstevel@tonic-gate **
25617c478bd9Sstevel@tonic-gate ** Returns:
25627c478bd9Sstevel@tonic-gate ** none.
25637c478bd9Sstevel@tonic-gate */
25647c478bd9Sstevel@tonic-gate
25657c478bd9Sstevel@tonic-gate void
proc_list_set(pid,task)25667c478bd9Sstevel@tonic-gate proc_list_set(pid, task)
25677c478bd9Sstevel@tonic-gate pid_t pid;
25687c478bd9Sstevel@tonic-gate char *task;
25697c478bd9Sstevel@tonic-gate {
25707c478bd9Sstevel@tonic-gate int i;
25717c478bd9Sstevel@tonic-gate
25727c478bd9Sstevel@tonic-gate for (i = 0; i < ProcListSize; i++)
25737c478bd9Sstevel@tonic-gate {
25747c478bd9Sstevel@tonic-gate if (ProcListVec[i].proc_pid == pid)
25757c478bd9Sstevel@tonic-gate {
25767c478bd9Sstevel@tonic-gate PSTRSET(ProcListVec[i].proc_task, task);
25777c478bd9Sstevel@tonic-gate break;
25787c478bd9Sstevel@tonic-gate }
25797c478bd9Sstevel@tonic-gate }
25807c478bd9Sstevel@tonic-gate }
2581058561cbSjbeck
25827c478bd9Sstevel@tonic-gate /*
25837c478bd9Sstevel@tonic-gate ** PROC_LIST_DROP -- drop pid from process list
25847c478bd9Sstevel@tonic-gate **
25857c478bd9Sstevel@tonic-gate ** Parameters:
25867c478bd9Sstevel@tonic-gate ** pid -- pid to drop
25877c478bd9Sstevel@tonic-gate ** st -- process status
25887c478bd9Sstevel@tonic-gate ** other -- storage for proc_other (return).
25897c478bd9Sstevel@tonic-gate **
25907c478bd9Sstevel@tonic-gate ** Returns:
25917c478bd9Sstevel@tonic-gate ** none.
25927c478bd9Sstevel@tonic-gate **
25937c478bd9Sstevel@tonic-gate ** Side Effects:
25947c478bd9Sstevel@tonic-gate ** May decrease CurChildren, CurRunners, or
25957c478bd9Sstevel@tonic-gate ** set RestartRequest or ShutdownRequest.
25967c478bd9Sstevel@tonic-gate **
25977c478bd9Sstevel@tonic-gate ** NOTE: THIS CAN BE CALLED FROM A SIGNAL HANDLER. DO NOT ADD
25987c478bd9Sstevel@tonic-gate ** ANYTHING TO THIS ROUTINE UNLESS YOU KNOW WHAT YOU ARE
25997c478bd9Sstevel@tonic-gate ** DOING.
26007c478bd9Sstevel@tonic-gate */
26017c478bd9Sstevel@tonic-gate
26027c478bd9Sstevel@tonic-gate void
proc_list_drop(pid,st,other)26037c478bd9Sstevel@tonic-gate proc_list_drop(pid, st, other)
26047c478bd9Sstevel@tonic-gate pid_t pid;
26057c478bd9Sstevel@tonic-gate int st;
26067c478bd9Sstevel@tonic-gate int *other;
26077c478bd9Sstevel@tonic-gate {
26087c478bd9Sstevel@tonic-gate int i;
26097c478bd9Sstevel@tonic-gate int type = PROC_NONE;
26107c478bd9Sstevel@tonic-gate
26117c478bd9Sstevel@tonic-gate for (i = 0; i < ProcListSize; i++)
26127c478bd9Sstevel@tonic-gate {
26137c478bd9Sstevel@tonic-gate if (ProcListVec[i].proc_pid == pid)
26147c478bd9Sstevel@tonic-gate {
26157c478bd9Sstevel@tonic-gate ProcListVec[i].proc_pid = NO_PID;
26167c478bd9Sstevel@tonic-gate type = ProcListVec[i].proc_type;
26177c478bd9Sstevel@tonic-gate if (other != NULL)
26187c478bd9Sstevel@tonic-gate *other = ProcListVec[i].proc_other;
2619445f2479Sjbeck if (CurChildren > 0)
2620445f2479Sjbeck CurChildren--;
26217c478bd9Sstevel@tonic-gate break;
26227c478bd9Sstevel@tonic-gate }
26237c478bd9Sstevel@tonic-gate }
26247c478bd9Sstevel@tonic-gate
26257c478bd9Sstevel@tonic-gate
26267c478bd9Sstevel@tonic-gate if (type == PROC_CONTROL && WIFEXITED(st))
26277c478bd9Sstevel@tonic-gate {
26287c478bd9Sstevel@tonic-gate /* if so, see if we need to restart or shutdown */
26297c478bd9Sstevel@tonic-gate if (WEXITSTATUS(st) == EX_RESTART)
26307c478bd9Sstevel@tonic-gate RestartRequest = "control socket";
26317c478bd9Sstevel@tonic-gate else if (WEXITSTATUS(st) == EX_SHUTDOWN)
26327c478bd9Sstevel@tonic-gate ShutdownRequest = "control socket";
26337c478bd9Sstevel@tonic-gate }
26347c478bd9Sstevel@tonic-gate else if (type == PROC_QUEUE_CHILD && !WIFSTOPPED(st) &&
26357c478bd9Sstevel@tonic-gate ProcListVec[i].proc_other > -1)
26367c478bd9Sstevel@tonic-gate {
26377c478bd9Sstevel@tonic-gate /* restart this persistent runner */
26387c478bd9Sstevel@tonic-gate mark_work_group_restart(ProcListVec[i].proc_other, st);
26397c478bd9Sstevel@tonic-gate }
26407c478bd9Sstevel@tonic-gate else if (type == PROC_QUEUE)
26417c478bd9Sstevel@tonic-gate CurRunners -= ProcListVec[i].proc_count;
26427c478bd9Sstevel@tonic-gate }
2643058561cbSjbeck
26447c478bd9Sstevel@tonic-gate /*
26457c478bd9Sstevel@tonic-gate ** PROC_LIST_CLEAR -- clear the process list
26467c478bd9Sstevel@tonic-gate **
26477c478bd9Sstevel@tonic-gate ** Parameters:
26487c478bd9Sstevel@tonic-gate ** none.
26497c478bd9Sstevel@tonic-gate **
26507c478bd9Sstevel@tonic-gate ** Returns:
26517c478bd9Sstevel@tonic-gate ** none.
26527c478bd9Sstevel@tonic-gate **
26537c478bd9Sstevel@tonic-gate ** Side Effects:
26547c478bd9Sstevel@tonic-gate ** Sets CurChildren to zero.
26557c478bd9Sstevel@tonic-gate */
26567c478bd9Sstevel@tonic-gate
26577c478bd9Sstevel@tonic-gate void
proc_list_clear()26587c478bd9Sstevel@tonic-gate proc_list_clear()
26597c478bd9Sstevel@tonic-gate {
26607c478bd9Sstevel@tonic-gate int i;
26617c478bd9Sstevel@tonic-gate
26627c478bd9Sstevel@tonic-gate /* start from 1 since 0 is the daemon itself */
26637c478bd9Sstevel@tonic-gate for (i = 1; i < ProcListSize; i++)
26647c478bd9Sstevel@tonic-gate ProcListVec[i].proc_pid = NO_PID;
26657c478bd9Sstevel@tonic-gate CurChildren = 0;
26667c478bd9Sstevel@tonic-gate }
2667058561cbSjbeck
26687c478bd9Sstevel@tonic-gate /*
26697c478bd9Sstevel@tonic-gate ** PROC_LIST_PROBE -- probe processes in the list to see if they still exist
26707c478bd9Sstevel@tonic-gate **
26717c478bd9Sstevel@tonic-gate ** Parameters:
26727c478bd9Sstevel@tonic-gate ** none
26737c478bd9Sstevel@tonic-gate **
26747c478bd9Sstevel@tonic-gate ** Returns:
26757c478bd9Sstevel@tonic-gate ** none
26767c478bd9Sstevel@tonic-gate **
26777c478bd9Sstevel@tonic-gate ** Side Effects:
26787c478bd9Sstevel@tonic-gate ** May decrease CurChildren.
26797c478bd9Sstevel@tonic-gate */
26807c478bd9Sstevel@tonic-gate
26817c478bd9Sstevel@tonic-gate void
proc_list_probe()26827c478bd9Sstevel@tonic-gate proc_list_probe()
26837c478bd9Sstevel@tonic-gate {
2684445f2479Sjbeck int i, children;
2685445f2479Sjbeck int chldwasblocked;
2686445f2479Sjbeck pid_t pid;
2687445f2479Sjbeck
2688445f2479Sjbeck children = 0;
2689445f2479Sjbeck chldwasblocked = sm_blocksignal(SIGCHLD);
26907c478bd9Sstevel@tonic-gate
26917c478bd9Sstevel@tonic-gate /* start from 1 since 0 is the daemon itself */
26927c478bd9Sstevel@tonic-gate for (i = 1; i < ProcListSize; i++)
26937c478bd9Sstevel@tonic-gate {
2694445f2479Sjbeck pid = ProcListVec[i].proc_pid;
2695445f2479Sjbeck if (pid == NO_PID || pid == CurrentPid)
26967c478bd9Sstevel@tonic-gate continue;
2697445f2479Sjbeck if (kill(pid, 0) < 0)
26987c478bd9Sstevel@tonic-gate {
26997c478bd9Sstevel@tonic-gate if (LogLevel > 3)
27007c478bd9Sstevel@tonic-gate sm_syslog(LOG_DEBUG, CurEnv->e_id,
27017c478bd9Sstevel@tonic-gate "proc_list_probe: lost pid %d",
27027c478bd9Sstevel@tonic-gate (int) ProcListVec[i].proc_pid);
27037c478bd9Sstevel@tonic-gate ProcListVec[i].proc_pid = NO_PID;
27047c478bd9Sstevel@tonic-gate SM_FREE_CLR(ProcListVec[i].proc_task);
27057c478bd9Sstevel@tonic-gate CurChildren--;
27067c478bd9Sstevel@tonic-gate }
2707445f2479Sjbeck else
2708445f2479Sjbeck {
2709445f2479Sjbeck ++children;
2710445f2479Sjbeck }
27117c478bd9Sstevel@tonic-gate }
27127c478bd9Sstevel@tonic-gate if (CurChildren < 0)
27137c478bd9Sstevel@tonic-gate CurChildren = 0;
2714445f2479Sjbeck if (chldwasblocked == 0)
2715445f2479Sjbeck (void) sm_releasesignal(SIGCHLD);
27163ee0e492Sjbeck if (LogLevel > 10 && children != CurChildren && CurrentPid == DaemonPid)
2717445f2479Sjbeck {
2718445f2479Sjbeck sm_syslog(LOG_ERR, NOQID,
2719445f2479Sjbeck "proc_list_probe: found %d children, expected %d",
2720445f2479Sjbeck children, CurChildren);
2721445f2479Sjbeck }
27227c478bd9Sstevel@tonic-gate }
27237c478bd9Sstevel@tonic-gate
27247c478bd9Sstevel@tonic-gate /*
27257c478bd9Sstevel@tonic-gate ** PROC_LIST_DISPLAY -- display the process list
27267c478bd9Sstevel@tonic-gate **
27277c478bd9Sstevel@tonic-gate ** Parameters:
27287c478bd9Sstevel@tonic-gate ** out -- output file pointer
27297c478bd9Sstevel@tonic-gate ** prefix -- string to output in front of each line.
27307c478bd9Sstevel@tonic-gate **
27317c478bd9Sstevel@tonic-gate ** Returns:
27327c478bd9Sstevel@tonic-gate ** none.
27337c478bd9Sstevel@tonic-gate */
27347c478bd9Sstevel@tonic-gate
27357c478bd9Sstevel@tonic-gate void
proc_list_display(out,prefix)27367c478bd9Sstevel@tonic-gate proc_list_display(out, prefix)
27377c478bd9Sstevel@tonic-gate SM_FILE_T *out;
27387c478bd9Sstevel@tonic-gate char *prefix;
27397c478bd9Sstevel@tonic-gate {
27407c478bd9Sstevel@tonic-gate int i;
27417c478bd9Sstevel@tonic-gate
27427c478bd9Sstevel@tonic-gate for (i = 0; i < ProcListSize; i++)
27437c478bd9Sstevel@tonic-gate {
27447c478bd9Sstevel@tonic-gate if (ProcListVec[i].proc_pid == NO_PID)
27457c478bd9Sstevel@tonic-gate continue;
27467c478bd9Sstevel@tonic-gate
27477c478bd9Sstevel@tonic-gate (void) sm_io_fprintf(out, SM_TIME_DEFAULT, "%s%d %s%s\n",
27487c478bd9Sstevel@tonic-gate prefix,
27497c478bd9Sstevel@tonic-gate (int) ProcListVec[i].proc_pid,
27507c478bd9Sstevel@tonic-gate ProcListVec[i].proc_task != NULL ?
27517c478bd9Sstevel@tonic-gate ProcListVec[i].proc_task : "(unknown)",
27527c478bd9Sstevel@tonic-gate (OpMode == MD_SMTP ||
27537c478bd9Sstevel@tonic-gate OpMode == MD_DAEMON ||
27547c478bd9Sstevel@tonic-gate OpMode == MD_ARPAFTP) ? "\r" : "");
27557c478bd9Sstevel@tonic-gate }
27567c478bd9Sstevel@tonic-gate }
27577c478bd9Sstevel@tonic-gate
27587c478bd9Sstevel@tonic-gate /*
27597c478bd9Sstevel@tonic-gate ** PROC_LIST_SIGNAL -- send a signal to a type of process in the list
27607c478bd9Sstevel@tonic-gate **
27617c478bd9Sstevel@tonic-gate ** Parameters:
27627c478bd9Sstevel@tonic-gate ** type -- type of process to signal
27637c478bd9Sstevel@tonic-gate ** signal -- the type of signal to send
27647c478bd9Sstevel@tonic-gate **
27657c478bd9Sstevel@tonic-gate ** Results:
27667c478bd9Sstevel@tonic-gate ** none.
27677c478bd9Sstevel@tonic-gate **
27687c478bd9Sstevel@tonic-gate ** NOTE: THIS CAN BE CALLED FROM A SIGNAL HANDLER. DO NOT ADD
27697c478bd9Sstevel@tonic-gate ** ANYTHING TO THIS ROUTINE UNLESS YOU KNOW WHAT YOU ARE
27707c478bd9Sstevel@tonic-gate ** DOING.
27717c478bd9Sstevel@tonic-gate */
27727c478bd9Sstevel@tonic-gate
27737c478bd9Sstevel@tonic-gate void
proc_list_signal(type,signal)27747c478bd9Sstevel@tonic-gate proc_list_signal(type, signal)
27757c478bd9Sstevel@tonic-gate int type;
27767c478bd9Sstevel@tonic-gate int signal;
27777c478bd9Sstevel@tonic-gate {
27787c478bd9Sstevel@tonic-gate int chldwasblocked;
27797c478bd9Sstevel@tonic-gate int alrmwasblocked;
27807c478bd9Sstevel@tonic-gate int i;
27817c478bd9Sstevel@tonic-gate pid_t mypid = getpid();
27827c478bd9Sstevel@tonic-gate
27837c478bd9Sstevel@tonic-gate /* block these signals so that we may signal cleanly */
27847c478bd9Sstevel@tonic-gate chldwasblocked = sm_blocksignal(SIGCHLD);
27857c478bd9Sstevel@tonic-gate alrmwasblocked = sm_blocksignal(SIGALRM);
27867c478bd9Sstevel@tonic-gate
27877c478bd9Sstevel@tonic-gate /* Find all processes of type and send signal */
27887c478bd9Sstevel@tonic-gate for (i = 0; i < ProcListSize; i++)
27897c478bd9Sstevel@tonic-gate {
27907c478bd9Sstevel@tonic-gate if (ProcListVec[i].proc_pid == NO_PID ||
27917c478bd9Sstevel@tonic-gate ProcListVec[i].proc_pid == mypid)
27927c478bd9Sstevel@tonic-gate continue;
27937c478bd9Sstevel@tonic-gate if (ProcListVec[i].proc_type != type)
27947c478bd9Sstevel@tonic-gate continue;
27957c478bd9Sstevel@tonic-gate (void) kill(ProcListVec[i].proc_pid, signal);
27967c478bd9Sstevel@tonic-gate }
27977c478bd9Sstevel@tonic-gate
27987c478bd9Sstevel@tonic-gate /* restore the signals */
27997c478bd9Sstevel@tonic-gate if (alrmwasblocked == 0)
28007c478bd9Sstevel@tonic-gate (void) sm_releasesignal(SIGALRM);
28017c478bd9Sstevel@tonic-gate if (chldwasblocked == 0)
28027c478bd9Sstevel@tonic-gate (void) sm_releasesignal(SIGCHLD);
28037c478bd9Sstevel@tonic-gate }
28047c478bd9Sstevel@tonic-gate
28057c478bd9Sstevel@tonic-gate /*
28067c478bd9Sstevel@tonic-gate ** COUNT_OPEN_CONNECTIONS
28077c478bd9Sstevel@tonic-gate **
28087c478bd9Sstevel@tonic-gate ** Parameters:
28097c478bd9Sstevel@tonic-gate ** hostaddr - ClientAddress
28107c478bd9Sstevel@tonic-gate **
28117c478bd9Sstevel@tonic-gate ** Returns:
28127c478bd9Sstevel@tonic-gate ** the number of open connections for this client
28137c478bd9Sstevel@tonic-gate **
28147c478bd9Sstevel@tonic-gate */
28157c478bd9Sstevel@tonic-gate
28167c478bd9Sstevel@tonic-gate int
count_open_connections(hostaddr)28177c478bd9Sstevel@tonic-gate count_open_connections(hostaddr)
28187c478bd9Sstevel@tonic-gate SOCKADDR *hostaddr;
28197c478bd9Sstevel@tonic-gate {
28207c478bd9Sstevel@tonic-gate int i, n;
28217c478bd9Sstevel@tonic-gate
28227c478bd9Sstevel@tonic-gate if (hostaddr == NULL)
28237c478bd9Sstevel@tonic-gate return 0;
28247800901eSjbeck
28257800901eSjbeck /*
2826d4660949Sjbeck ** This code gets called before proc_list_add() gets called,
2827d4660949Sjbeck ** so we (the daemon child for this connection) have not yet
2828d4660949Sjbeck ** counted ourselves. Hence initialize the counter to 1
2829d4660949Sjbeck ** instead of 0 to compensate.
28307800901eSjbeck */
28317800901eSjbeck
28327800901eSjbeck n = 1;
28337c478bd9Sstevel@tonic-gate for (i = 0; i < ProcListSize; i++)
28347c478bd9Sstevel@tonic-gate {
28357c478bd9Sstevel@tonic-gate if (ProcListVec[i].proc_pid == NO_PID)
28367c478bd9Sstevel@tonic-gate continue;
28377c478bd9Sstevel@tonic-gate if (hostaddr->sa.sa_family !=
28387c478bd9Sstevel@tonic-gate ProcListVec[i].proc_hostaddr.sa.sa_family)
28397c478bd9Sstevel@tonic-gate continue;
28407c478bd9Sstevel@tonic-gate #if NETINET
28417c478bd9Sstevel@tonic-gate if (hostaddr->sa.sa_family == AF_INET &&
28427c478bd9Sstevel@tonic-gate (hostaddr->sin.sin_addr.s_addr ==
28437c478bd9Sstevel@tonic-gate ProcListVec[i].proc_hostaddr.sin.sin_addr.s_addr))
28447c478bd9Sstevel@tonic-gate n++;
28457c478bd9Sstevel@tonic-gate #endif /* NETINET */
28467c478bd9Sstevel@tonic-gate #if NETINET6
28477c478bd9Sstevel@tonic-gate if (hostaddr->sa.sa_family == AF_INET6 &&
28487c478bd9Sstevel@tonic-gate IN6_ARE_ADDR_EQUAL(&(hostaddr->sin6.sin6_addr),
28497c478bd9Sstevel@tonic-gate &(ProcListVec[i].proc_hostaddr.sin6.sin6_addr)))
28507c478bd9Sstevel@tonic-gate n++;
28517c478bd9Sstevel@tonic-gate #endif /* NETINET6 */
28527c478bd9Sstevel@tonic-gate }
28537c478bd9Sstevel@tonic-gate return n;
28547c478bd9Sstevel@tonic-gate }
2855