xref: /onnv-gate/usr/src/cmd/cmd-inet/sbin/dhcpagent/defaults.c (revision 2546:3e3e9857b7e6)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*2546Scarlsonj  * Common Development and Distribution License (the "License").
6*2546Scarlsonj  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*2546Scarlsonj  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
270Sstevel@tonic-gate 
280Sstevel@tonic-gate #include <sys/types.h>
290Sstevel@tonic-gate #include <netinet/in.h>
300Sstevel@tonic-gate #include <netinet/inetutil.h>
310Sstevel@tonic-gate #include <netinet/dhcp.h>
320Sstevel@tonic-gate #include <stdlib.h>
330Sstevel@tonic-gate #include <string.h>
340Sstevel@tonic-gate #include <ctype.h>
350Sstevel@tonic-gate #include <dhcpmsg.h>
360Sstevel@tonic-gate #include <stdio.h>
370Sstevel@tonic-gate #include <sys/stat.h>
380Sstevel@tonic-gate #include <libnvpair.h>
390Sstevel@tonic-gate 
400Sstevel@tonic-gate #include "defaults.h"
410Sstevel@tonic-gate 
420Sstevel@tonic-gate struct dhcp_default {
430Sstevel@tonic-gate 
440Sstevel@tonic-gate 	const char	*df_name;	/* parameter name */
450Sstevel@tonic-gate 	const char	*df_default;	/* default value */
460Sstevel@tonic-gate 	int		df_min;		/* min value if type DF_INTEGER */
470Sstevel@tonic-gate 	int		df_max;		/* max value if type DF_INTEGER */
480Sstevel@tonic-gate };
490Sstevel@tonic-gate 
500Sstevel@tonic-gate /*
510Sstevel@tonic-gate  * note: keep in the same order as tunable parameter constants in defaults.h
520Sstevel@tonic-gate  */
530Sstevel@tonic-gate 
540Sstevel@tonic-gate static struct dhcp_default defaults[] = {
550Sstevel@tonic-gate 
560Sstevel@tonic-gate 	{ "RELEASE_ON_SIGTERM",  "0",	 0,   0	  },
57*2546Scarlsonj 	{ "IGNORE_FAILED_ARP",	 "1",	 0,   -1  },
580Sstevel@tonic-gate 	{ "OFFER_WAIT",		 "3",	 1,   20  },
59*2546Scarlsonj 	{ "ARP_WAIT",		 "1000", 0,   -1  },
600Sstevel@tonic-gate 	{ "CLIENT_ID",		 NULL,	 0,   0	  },
610Sstevel@tonic-gate 	{ "PARAM_REQUEST_LIST",  NULL,	 0,   0    },
620Sstevel@tonic-gate 	{ "REQUEST_HOSTNAME",	 "1",	 0,   0	  }
630Sstevel@tonic-gate };
640Sstevel@tonic-gate 
650Sstevel@tonic-gate /*
660Sstevel@tonic-gate  * df_build_cache(): builds the defaults nvlist cache
670Sstevel@tonic-gate  *
680Sstevel@tonic-gate  *   input: void
690Sstevel@tonic-gate  *  output: a pointer to an nvlist of the current defaults, or NULL on failure
700Sstevel@tonic-gate  */
710Sstevel@tonic-gate 
720Sstevel@tonic-gate static nvlist_t *
730Sstevel@tonic-gate df_build_cache(void)
740Sstevel@tonic-gate {
750Sstevel@tonic-gate 	char		entry[1024];
760Sstevel@tonic-gate 	int		i;
770Sstevel@tonic-gate 	char		*param, *value, *end;
780Sstevel@tonic-gate 	FILE		*fp;
790Sstevel@tonic-gate 	nvlist_t 	*nvlist;
80*2546Scarlsonj 	struct dhcp_default *defp;
810Sstevel@tonic-gate 
820Sstevel@tonic-gate 	if ((fp = fopen(DHCP_AGENT_DEFAULTS, "r")) == NULL)
830Sstevel@tonic-gate 		return (NULL);
840Sstevel@tonic-gate 
850Sstevel@tonic-gate 	if (nvlist_alloc(&nvlist, NV_UNIQUE_NAME, 0) != 0) {
860Sstevel@tonic-gate 		dhcpmsg(MSG_WARNING, "cannot build default value cache; "
870Sstevel@tonic-gate 		    "using built-in defaults");
880Sstevel@tonic-gate 		(void) fclose(fp);
890Sstevel@tonic-gate 		return (NULL);
900Sstevel@tonic-gate 	}
910Sstevel@tonic-gate 
920Sstevel@tonic-gate 	while (fgets(entry, sizeof (entry), fp) != NULL) {
930Sstevel@tonic-gate 		for (i = 0; entry[i] == ' '; i++)
940Sstevel@tonic-gate 			;
950Sstevel@tonic-gate 
960Sstevel@tonic-gate 		end = strrchr(entry, '\n');
970Sstevel@tonic-gate 		value = strchr(entry, '=');
980Sstevel@tonic-gate 		if (end == NULL || value == NULL || entry[i] == '#')
990Sstevel@tonic-gate 			continue;
1000Sstevel@tonic-gate 
1010Sstevel@tonic-gate 		*end = '\0';
1020Sstevel@tonic-gate 		*value++ = '\0';
1030Sstevel@tonic-gate 
1040Sstevel@tonic-gate 		/*
1050Sstevel@tonic-gate 		 * to be compatible with the old defread()-based code
1060Sstevel@tonic-gate 		 * which ignored case, store the parameters (except for the
1070Sstevel@tonic-gate 		 * leading interface name) in upper case.
1080Sstevel@tonic-gate 		 */
1090Sstevel@tonic-gate 
1100Sstevel@tonic-gate 		if ((param = strchr(entry, '.')) == NULL)
1110Sstevel@tonic-gate 			param = entry;
1120Sstevel@tonic-gate 		else
1130Sstevel@tonic-gate 			param++;
1140Sstevel@tonic-gate 
115*2546Scarlsonj 		for (defp = defaults;
116*2546Scarlsonj 		    (char *)defp < (char *)defaults + sizeof (defaults);
117*2546Scarlsonj 		    defp++) {
118*2546Scarlsonj 			if (strcasecmp(param, defp->df_name) == 0) {
119*2546Scarlsonj 				if (defp->df_max == -1) {
120*2546Scarlsonj 					dhcpmsg(MSG_WARNING, "parameter %s is "
121*2546Scarlsonj 					    "obsolete; ignored", defp->df_name);
122*2546Scarlsonj 				}
123*2546Scarlsonj 				break;
124*2546Scarlsonj 			}
125*2546Scarlsonj 		}
126*2546Scarlsonj 
1270Sstevel@tonic-gate 		for (; *param != '\0'; param++)
1280Sstevel@tonic-gate 			*param = toupper(*param);
1290Sstevel@tonic-gate 
1300Sstevel@tonic-gate 		if (nvlist_add_string(nvlist, &entry[i], value) != 0) {
1310Sstevel@tonic-gate 			dhcpmsg(MSG_WARNING, "cannot build default value cache;"
1320Sstevel@tonic-gate 			    " using built-in defaults");
1330Sstevel@tonic-gate 			nvlist_free(nvlist);
1340Sstevel@tonic-gate 			nvlist = NULL;
1350Sstevel@tonic-gate 			break;
1360Sstevel@tonic-gate 		}
1370Sstevel@tonic-gate 	}
1380Sstevel@tonic-gate 
1390Sstevel@tonic-gate 	(void) fclose(fp);
1400Sstevel@tonic-gate 	return (nvlist);
1410Sstevel@tonic-gate }
1420Sstevel@tonic-gate 
1430Sstevel@tonic-gate /*
1440Sstevel@tonic-gate  * df_get_string(): gets the string value of a given user-tunable parameter
1450Sstevel@tonic-gate  *
1460Sstevel@tonic-gate  *   input: const char *: the interface the parameter applies to
1470Sstevel@tonic-gate  *	    unsigned int: the parameter number to look up
1480Sstevel@tonic-gate  *  output: const char *: the parameter's value, or default if not set
1490Sstevel@tonic-gate  *			  (must be copied by caller to be kept)
1500Sstevel@tonic-gate  *    NOTE: df_get_string() is both used by functions outside this source
1510Sstevel@tonic-gate  *	    file to retrieve strings from the defaults file, *and*
1520Sstevel@tonic-gate  *	    internally by other df_get_*() functions.
1530Sstevel@tonic-gate  */
1540Sstevel@tonic-gate 
1550Sstevel@tonic-gate const char *
1560Sstevel@tonic-gate df_get_string(const char *if_name, unsigned int p)
1570Sstevel@tonic-gate {
1580Sstevel@tonic-gate 	char			*value;
1590Sstevel@tonic-gate 	char			param[256];
1600Sstevel@tonic-gate 	struct stat		statbuf;
1610Sstevel@tonic-gate 	static struct stat	df_statbuf;
1620Sstevel@tonic-gate 	static boolean_t	df_unavail_msg = B_FALSE;
1630Sstevel@tonic-gate 	static nvlist_t		*df_nvlist = NULL;
1640Sstevel@tonic-gate 
1650Sstevel@tonic-gate 	if (p >= (sizeof (defaults) / sizeof (*defaults)))
1660Sstevel@tonic-gate 		return (NULL);
1670Sstevel@tonic-gate 
1680Sstevel@tonic-gate 	if (stat(DHCP_AGENT_DEFAULTS, &statbuf) != 0) {
1690Sstevel@tonic-gate 		if (!df_unavail_msg) {
1700Sstevel@tonic-gate 			dhcpmsg(MSG_WARNING, "cannot access %s; using "
1710Sstevel@tonic-gate 			    "built-in defaults", DHCP_AGENT_DEFAULTS);
1720Sstevel@tonic-gate 			df_unavail_msg = B_TRUE;
1730Sstevel@tonic-gate 		}
1740Sstevel@tonic-gate 		return (defaults[p].df_default);
1750Sstevel@tonic-gate 	}
1760Sstevel@tonic-gate 
1770Sstevel@tonic-gate 	/*
1780Sstevel@tonic-gate 	 * if our cached parameters are stale, rebuild.
1790Sstevel@tonic-gate 	 */
1800Sstevel@tonic-gate 
1810Sstevel@tonic-gate 	if (statbuf.st_mtime != df_statbuf.st_mtime ||
1820Sstevel@tonic-gate 	    statbuf.st_size != df_statbuf.st_size) {
1830Sstevel@tonic-gate 		df_statbuf = statbuf;
1840Sstevel@tonic-gate 		if (df_nvlist != NULL)
1850Sstevel@tonic-gate 			nvlist_free(df_nvlist);
1860Sstevel@tonic-gate 		df_nvlist = df_build_cache();
1870Sstevel@tonic-gate 	}
1880Sstevel@tonic-gate 
1890Sstevel@tonic-gate 	(void) snprintf(param, sizeof (param), "%s.%s", if_name,
1900Sstevel@tonic-gate 	    defaults[p].df_name);
1910Sstevel@tonic-gate 
1920Sstevel@tonic-gate 	/*
1930Sstevel@tonic-gate 	 * first look for `if_name.param', then `param'.  if neither
1940Sstevel@tonic-gate 	 * has been set, use the built-in default.
1950Sstevel@tonic-gate 	 */
1960Sstevel@tonic-gate 
1970Sstevel@tonic-gate 	if (nvlist_lookup_string(df_nvlist, param, &value) == 0 ||
1980Sstevel@tonic-gate 	    nvlist_lookup_string(df_nvlist, defaults[p].df_name, &value) == 0)
1990Sstevel@tonic-gate 		return (value);
2000Sstevel@tonic-gate 
2010Sstevel@tonic-gate 	return (defaults[p].df_default);
2020Sstevel@tonic-gate }
2030Sstevel@tonic-gate 
2040Sstevel@tonic-gate /*
2050Sstevel@tonic-gate  * df_get_octet(): gets the integer value of a given user-tunable parameter
2060Sstevel@tonic-gate  *
2070Sstevel@tonic-gate  *   input: const char *: the interface the parameter applies to
2080Sstevel@tonic-gate  *	    unsigned int: the parameter number to look up
2090Sstevel@tonic-gate  *	    unsigned int *: the length of the returned value
2100Sstevel@tonic-gate  *  output: uchar_t *: a pointer to byte array (default value if not set)
2110Sstevel@tonic-gate  *		       (must be copied by caller to be kept)
2120Sstevel@tonic-gate  */
2130Sstevel@tonic-gate 
2140Sstevel@tonic-gate uchar_t *
2150Sstevel@tonic-gate df_get_octet(const char *if_name, unsigned int p, unsigned int *len)
2160Sstevel@tonic-gate {
2170Sstevel@tonic-gate 	const char	*value;
2180Sstevel@tonic-gate 	static uchar_t	octet_value[256]; /* as big as defread() returns */
2190Sstevel@tonic-gate 
2200Sstevel@tonic-gate 	if (p >= (sizeof (defaults) / sizeof (*defaults)))
2210Sstevel@tonic-gate 		return (NULL);
2220Sstevel@tonic-gate 
2230Sstevel@tonic-gate 	value = df_get_string(if_name, p);
2240Sstevel@tonic-gate 	if (value == NULL)
2250Sstevel@tonic-gate 		goto do_default;
2260Sstevel@tonic-gate 
2270Sstevel@tonic-gate 	if (strncasecmp("0x", value, 2) != 0) {
2280Sstevel@tonic-gate 		*len = strlen(value);			/* no NUL */
2290Sstevel@tonic-gate 		return ((uchar_t *)value);
2300Sstevel@tonic-gate 	}
2310Sstevel@tonic-gate 
2320Sstevel@tonic-gate 	/* skip past the 0x and convert the value to binary */
2330Sstevel@tonic-gate 	value += 2;
2340Sstevel@tonic-gate 	*len = sizeof (octet_value);
2350Sstevel@tonic-gate 	if (hexascii_to_octet(value, strlen(value), octet_value, len) != 0) {
2360Sstevel@tonic-gate 		dhcpmsg(MSG_WARNING, "df_get_octet: cannot convert value "
2370Sstevel@tonic-gate 		    "for parameter `%s', using default", defaults[p].df_name);
2380Sstevel@tonic-gate 		goto do_default;
2390Sstevel@tonic-gate 	}
2400Sstevel@tonic-gate 	return (octet_value);
2410Sstevel@tonic-gate 
2420Sstevel@tonic-gate do_default:
2430Sstevel@tonic-gate 	if (defaults[p].df_default == NULL) {
2440Sstevel@tonic-gate 		*len = 0;
2450Sstevel@tonic-gate 		return (NULL);
2460Sstevel@tonic-gate 	}
2470Sstevel@tonic-gate 
2480Sstevel@tonic-gate 	*len = strlen(defaults[p].df_default);		/* no NUL */
2490Sstevel@tonic-gate 	return ((uchar_t *)defaults[p].df_default);
2500Sstevel@tonic-gate }
2510Sstevel@tonic-gate 
2520Sstevel@tonic-gate /*
2530Sstevel@tonic-gate  * df_get_int(): gets the integer value of a given user-tunable parameter
2540Sstevel@tonic-gate  *
2550Sstevel@tonic-gate  *   input: const char *: the interface the parameter applies to
2560Sstevel@tonic-gate  *	    unsigned int: the parameter number to look up
2570Sstevel@tonic-gate  *  output: int: the parameter's value, or default if not set
2580Sstevel@tonic-gate  */
2590Sstevel@tonic-gate 
2600Sstevel@tonic-gate int
2610Sstevel@tonic-gate df_get_int(const char *if_name, unsigned int p)
2620Sstevel@tonic-gate {
2630Sstevel@tonic-gate 	const char	*value;
2640Sstevel@tonic-gate 	int		value_int;
2650Sstevel@tonic-gate 
2660Sstevel@tonic-gate 	if (p >= (sizeof (defaults) / sizeof (*defaults)))
2670Sstevel@tonic-gate 		return (0);
2680Sstevel@tonic-gate 
2690Sstevel@tonic-gate 	value = df_get_string(if_name, p);
2700Sstevel@tonic-gate 	if (value == NULL || !isdigit(*value))
2710Sstevel@tonic-gate 		goto failure;
2720Sstevel@tonic-gate 
2730Sstevel@tonic-gate 	value_int = atoi(value);
2740Sstevel@tonic-gate 	if (value_int > defaults[p].df_max || value_int < defaults[p].df_min)
2750Sstevel@tonic-gate 		goto failure;
2760Sstevel@tonic-gate 
2770Sstevel@tonic-gate 	return (value_int);
2780Sstevel@tonic-gate 
2790Sstevel@tonic-gate failure:
2800Sstevel@tonic-gate 	dhcpmsg(MSG_WARNING, "df_get_int: parameter `%s' is not between %d and "
2810Sstevel@tonic-gate 	    "%d, defaulting to `%s'", defaults[p].df_name, defaults[p].df_min,
2820Sstevel@tonic-gate 	    defaults[p].df_max, defaults[p].df_default);
2830Sstevel@tonic-gate 	return (atoi(defaults[p].df_default));
2840Sstevel@tonic-gate }
2850Sstevel@tonic-gate 
2860Sstevel@tonic-gate /*
2870Sstevel@tonic-gate  * df_get_bool(): gets the boolean value of a given user-tunable parameter
2880Sstevel@tonic-gate  *
2890Sstevel@tonic-gate  *   input: const char *: the interface the parameter applies to
2900Sstevel@tonic-gate  *	    unsigned int: the parameter number to look up
2910Sstevel@tonic-gate  *  output: boolean_t: B_TRUE if true, B_FALSE if false, default if not set
2920Sstevel@tonic-gate  */
2930Sstevel@tonic-gate 
2940Sstevel@tonic-gate boolean_t
2950Sstevel@tonic-gate df_get_bool(const char *if_name, unsigned int p)
2960Sstevel@tonic-gate {
2970Sstevel@tonic-gate 	const char	*value;
2980Sstevel@tonic-gate 
2990Sstevel@tonic-gate 	if (p >= (sizeof (defaults) / sizeof (*defaults)))
3000Sstevel@tonic-gate 		return (0);
3010Sstevel@tonic-gate 
3020Sstevel@tonic-gate 	value = df_get_string(if_name, p);
3030Sstevel@tonic-gate 	if (value != NULL) {
3040Sstevel@tonic-gate 
3050Sstevel@tonic-gate 		if (strcasecmp(value, "true") == 0 ||
3060Sstevel@tonic-gate 		    strcasecmp(value, "yes") == 0 || strcmp(value, "1") == 0)
3070Sstevel@tonic-gate 			return (B_TRUE);
3080Sstevel@tonic-gate 
3090Sstevel@tonic-gate 		if (strcasecmp(value, "false") == 0 ||
3100Sstevel@tonic-gate 		    strcasecmp(value, "no") == 0 || strcmp(value, "0") == 0)
3110Sstevel@tonic-gate 			return (B_FALSE);
3120Sstevel@tonic-gate 	}
3130Sstevel@tonic-gate 
3140Sstevel@tonic-gate 	dhcpmsg(MSG_WARNING, "df_get_bool: parameter `%s' has invalid value "
3150Sstevel@tonic-gate 	    "`%s', defaulting to `%s'", defaults[p].df_name,
3160Sstevel@tonic-gate 	    value ? value : "NULL", defaults[p].df_default);
3170Sstevel@tonic-gate 
3180Sstevel@tonic-gate 	return ((atoi(defaults[p].df_default) == 0) ? B_FALSE : B_TRUE);
3190Sstevel@tonic-gate }
320