17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*69bb4bb4Scarlsonj * Common Development and Distribution License (the "License"). 6*69bb4bb4Scarlsonj * You may not use this file except in compliance with the License. 77c478bd9Sstevel@tonic-gate * 87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 117c478bd9Sstevel@tonic-gate * and limitations under the License. 127c478bd9Sstevel@tonic-gate * 137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 187c478bd9Sstevel@tonic-gate * 197c478bd9Sstevel@tonic-gate * CDDL HEADER END 207c478bd9Sstevel@tonic-gate */ 217c478bd9Sstevel@tonic-gate /* 22*69bb4bb4Scarlsonj * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 237c478bd9Sstevel@tonic-gate * Use is subject to license terms. 247c478bd9Sstevel@tonic-gate */ 257c478bd9Sstevel@tonic-gate 267c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 277c478bd9Sstevel@tonic-gate 287c478bd9Sstevel@tonic-gate #include <sys/types.h> 297c478bd9Sstevel@tonic-gate #include <netinet/in.h> 307c478bd9Sstevel@tonic-gate #include <netinet/inetutil.h> 317c478bd9Sstevel@tonic-gate #include <netinet/dhcp.h> 327c478bd9Sstevel@tonic-gate #include <stdlib.h> 337c478bd9Sstevel@tonic-gate #include <string.h> 347c478bd9Sstevel@tonic-gate #include <ctype.h> 357c478bd9Sstevel@tonic-gate #include <dhcpmsg.h> 367c478bd9Sstevel@tonic-gate #include <stdio.h> 377c478bd9Sstevel@tonic-gate #include <sys/stat.h> 387c478bd9Sstevel@tonic-gate #include <libnvpair.h> 397c478bd9Sstevel@tonic-gate 407c478bd9Sstevel@tonic-gate #include "defaults.h" 417c478bd9Sstevel@tonic-gate 427c478bd9Sstevel@tonic-gate struct dhcp_default { 437c478bd9Sstevel@tonic-gate 447c478bd9Sstevel@tonic-gate const char *df_name; /* parameter name */ 457c478bd9Sstevel@tonic-gate const char *df_default; /* default value */ 467c478bd9Sstevel@tonic-gate int df_min; /* min value if type DF_INTEGER */ 477c478bd9Sstevel@tonic-gate int df_max; /* max value if type DF_INTEGER */ 487c478bd9Sstevel@tonic-gate }; 497c478bd9Sstevel@tonic-gate 507c478bd9Sstevel@tonic-gate /* 517c478bd9Sstevel@tonic-gate * note: keep in the same order as tunable parameter constants in defaults.h 527c478bd9Sstevel@tonic-gate */ 537c478bd9Sstevel@tonic-gate 547c478bd9Sstevel@tonic-gate static struct dhcp_default defaults[] = { 557c478bd9Sstevel@tonic-gate 567c478bd9Sstevel@tonic-gate { "RELEASE_ON_SIGTERM", "0", 0, 0 }, 57*69bb4bb4Scarlsonj { "IGNORE_FAILED_ARP", "1", 0, -1 }, 587c478bd9Sstevel@tonic-gate { "OFFER_WAIT", "3", 1, 20 }, 59*69bb4bb4Scarlsonj { "ARP_WAIT", "1000", 0, -1 }, 607c478bd9Sstevel@tonic-gate { "CLIENT_ID", NULL, 0, 0 }, 617c478bd9Sstevel@tonic-gate { "PARAM_REQUEST_LIST", NULL, 0, 0 }, 627c478bd9Sstevel@tonic-gate { "REQUEST_HOSTNAME", "1", 0, 0 } 637c478bd9Sstevel@tonic-gate }; 647c478bd9Sstevel@tonic-gate 657c478bd9Sstevel@tonic-gate /* 667c478bd9Sstevel@tonic-gate * df_build_cache(): builds the defaults nvlist cache 677c478bd9Sstevel@tonic-gate * 687c478bd9Sstevel@tonic-gate * input: void 697c478bd9Sstevel@tonic-gate * output: a pointer to an nvlist of the current defaults, or NULL on failure 707c478bd9Sstevel@tonic-gate */ 717c478bd9Sstevel@tonic-gate 727c478bd9Sstevel@tonic-gate static nvlist_t * 737c478bd9Sstevel@tonic-gate df_build_cache(void) 747c478bd9Sstevel@tonic-gate { 757c478bd9Sstevel@tonic-gate char entry[1024]; 767c478bd9Sstevel@tonic-gate int i; 777c478bd9Sstevel@tonic-gate char *param, *value, *end; 787c478bd9Sstevel@tonic-gate FILE *fp; 797c478bd9Sstevel@tonic-gate nvlist_t *nvlist; 80*69bb4bb4Scarlsonj struct dhcp_default *defp; 817c478bd9Sstevel@tonic-gate 827c478bd9Sstevel@tonic-gate if ((fp = fopen(DHCP_AGENT_DEFAULTS, "r")) == NULL) 837c478bd9Sstevel@tonic-gate return (NULL); 847c478bd9Sstevel@tonic-gate 857c478bd9Sstevel@tonic-gate if (nvlist_alloc(&nvlist, NV_UNIQUE_NAME, 0) != 0) { 867c478bd9Sstevel@tonic-gate dhcpmsg(MSG_WARNING, "cannot build default value cache; " 877c478bd9Sstevel@tonic-gate "using built-in defaults"); 887c478bd9Sstevel@tonic-gate (void) fclose(fp); 897c478bd9Sstevel@tonic-gate return (NULL); 907c478bd9Sstevel@tonic-gate } 917c478bd9Sstevel@tonic-gate 927c478bd9Sstevel@tonic-gate while (fgets(entry, sizeof (entry), fp) != NULL) { 937c478bd9Sstevel@tonic-gate for (i = 0; entry[i] == ' '; i++) 947c478bd9Sstevel@tonic-gate ; 957c478bd9Sstevel@tonic-gate 967c478bd9Sstevel@tonic-gate end = strrchr(entry, '\n'); 977c478bd9Sstevel@tonic-gate value = strchr(entry, '='); 987c478bd9Sstevel@tonic-gate if (end == NULL || value == NULL || entry[i] == '#') 997c478bd9Sstevel@tonic-gate continue; 1007c478bd9Sstevel@tonic-gate 1017c478bd9Sstevel@tonic-gate *end = '\0'; 1027c478bd9Sstevel@tonic-gate *value++ = '\0'; 1037c478bd9Sstevel@tonic-gate 1047c478bd9Sstevel@tonic-gate /* 1057c478bd9Sstevel@tonic-gate * to be compatible with the old defread()-based code 1067c478bd9Sstevel@tonic-gate * which ignored case, store the parameters (except for the 1077c478bd9Sstevel@tonic-gate * leading interface name) in upper case. 1087c478bd9Sstevel@tonic-gate */ 1097c478bd9Sstevel@tonic-gate 1107c478bd9Sstevel@tonic-gate if ((param = strchr(entry, '.')) == NULL) 1117c478bd9Sstevel@tonic-gate param = entry; 1127c478bd9Sstevel@tonic-gate else 1137c478bd9Sstevel@tonic-gate param++; 1147c478bd9Sstevel@tonic-gate 115*69bb4bb4Scarlsonj for (defp = defaults; 116*69bb4bb4Scarlsonj (char *)defp < (char *)defaults + sizeof (defaults); 117*69bb4bb4Scarlsonj defp++) { 118*69bb4bb4Scarlsonj if (strcasecmp(param, defp->df_name) == 0) { 119*69bb4bb4Scarlsonj if (defp->df_max == -1) { 120*69bb4bb4Scarlsonj dhcpmsg(MSG_WARNING, "parameter %s is " 121*69bb4bb4Scarlsonj "obsolete; ignored", defp->df_name); 122*69bb4bb4Scarlsonj } 123*69bb4bb4Scarlsonj break; 124*69bb4bb4Scarlsonj } 125*69bb4bb4Scarlsonj } 126*69bb4bb4Scarlsonj 1277c478bd9Sstevel@tonic-gate for (; *param != '\0'; param++) 1287c478bd9Sstevel@tonic-gate *param = toupper(*param); 1297c478bd9Sstevel@tonic-gate 1307c478bd9Sstevel@tonic-gate if (nvlist_add_string(nvlist, &entry[i], value) != 0) { 1317c478bd9Sstevel@tonic-gate dhcpmsg(MSG_WARNING, "cannot build default value cache;" 1327c478bd9Sstevel@tonic-gate " using built-in defaults"); 1337c478bd9Sstevel@tonic-gate nvlist_free(nvlist); 1347c478bd9Sstevel@tonic-gate nvlist = NULL; 1357c478bd9Sstevel@tonic-gate break; 1367c478bd9Sstevel@tonic-gate } 1377c478bd9Sstevel@tonic-gate } 1387c478bd9Sstevel@tonic-gate 1397c478bd9Sstevel@tonic-gate (void) fclose(fp); 1407c478bd9Sstevel@tonic-gate return (nvlist); 1417c478bd9Sstevel@tonic-gate } 1427c478bd9Sstevel@tonic-gate 1437c478bd9Sstevel@tonic-gate /* 1447c478bd9Sstevel@tonic-gate * df_get_string(): gets the string value of a given user-tunable parameter 1457c478bd9Sstevel@tonic-gate * 1467c478bd9Sstevel@tonic-gate * input: const char *: the interface the parameter applies to 1477c478bd9Sstevel@tonic-gate * unsigned int: the parameter number to look up 1487c478bd9Sstevel@tonic-gate * output: const char *: the parameter's value, or default if not set 1497c478bd9Sstevel@tonic-gate * (must be copied by caller to be kept) 1507c478bd9Sstevel@tonic-gate * NOTE: df_get_string() is both used by functions outside this source 1517c478bd9Sstevel@tonic-gate * file to retrieve strings from the defaults file, *and* 1527c478bd9Sstevel@tonic-gate * internally by other df_get_*() functions. 1537c478bd9Sstevel@tonic-gate */ 1547c478bd9Sstevel@tonic-gate 1557c478bd9Sstevel@tonic-gate const char * 1567c478bd9Sstevel@tonic-gate df_get_string(const char *if_name, unsigned int p) 1577c478bd9Sstevel@tonic-gate { 1587c478bd9Sstevel@tonic-gate char *value; 1597c478bd9Sstevel@tonic-gate char param[256]; 1607c478bd9Sstevel@tonic-gate struct stat statbuf; 1617c478bd9Sstevel@tonic-gate static struct stat df_statbuf; 1627c478bd9Sstevel@tonic-gate static boolean_t df_unavail_msg = B_FALSE; 1637c478bd9Sstevel@tonic-gate static nvlist_t *df_nvlist = NULL; 1647c478bd9Sstevel@tonic-gate 1657c478bd9Sstevel@tonic-gate if (p >= (sizeof (defaults) / sizeof (*defaults))) 1667c478bd9Sstevel@tonic-gate return (NULL); 1677c478bd9Sstevel@tonic-gate 1687c478bd9Sstevel@tonic-gate if (stat(DHCP_AGENT_DEFAULTS, &statbuf) != 0) { 1697c478bd9Sstevel@tonic-gate if (!df_unavail_msg) { 1707c478bd9Sstevel@tonic-gate dhcpmsg(MSG_WARNING, "cannot access %s; using " 1717c478bd9Sstevel@tonic-gate "built-in defaults", DHCP_AGENT_DEFAULTS); 1727c478bd9Sstevel@tonic-gate df_unavail_msg = B_TRUE; 1737c478bd9Sstevel@tonic-gate } 1747c478bd9Sstevel@tonic-gate return (defaults[p].df_default); 1757c478bd9Sstevel@tonic-gate } 1767c478bd9Sstevel@tonic-gate 1777c478bd9Sstevel@tonic-gate /* 1787c478bd9Sstevel@tonic-gate * if our cached parameters are stale, rebuild. 1797c478bd9Sstevel@tonic-gate */ 1807c478bd9Sstevel@tonic-gate 1817c478bd9Sstevel@tonic-gate if (statbuf.st_mtime != df_statbuf.st_mtime || 1827c478bd9Sstevel@tonic-gate statbuf.st_size != df_statbuf.st_size) { 1837c478bd9Sstevel@tonic-gate df_statbuf = statbuf; 1847c478bd9Sstevel@tonic-gate if (df_nvlist != NULL) 1857c478bd9Sstevel@tonic-gate nvlist_free(df_nvlist); 1867c478bd9Sstevel@tonic-gate df_nvlist = df_build_cache(); 1877c478bd9Sstevel@tonic-gate } 1887c478bd9Sstevel@tonic-gate 1897c478bd9Sstevel@tonic-gate (void) snprintf(param, sizeof (param), "%s.%s", if_name, 1907c478bd9Sstevel@tonic-gate defaults[p].df_name); 1917c478bd9Sstevel@tonic-gate 1927c478bd9Sstevel@tonic-gate /* 1937c478bd9Sstevel@tonic-gate * first look for `if_name.param', then `param'. if neither 1947c478bd9Sstevel@tonic-gate * has been set, use the built-in default. 1957c478bd9Sstevel@tonic-gate */ 1967c478bd9Sstevel@tonic-gate 1977c478bd9Sstevel@tonic-gate if (nvlist_lookup_string(df_nvlist, param, &value) == 0 || 1987c478bd9Sstevel@tonic-gate nvlist_lookup_string(df_nvlist, defaults[p].df_name, &value) == 0) 1997c478bd9Sstevel@tonic-gate return (value); 2007c478bd9Sstevel@tonic-gate 2017c478bd9Sstevel@tonic-gate return (defaults[p].df_default); 2027c478bd9Sstevel@tonic-gate } 2037c478bd9Sstevel@tonic-gate 2047c478bd9Sstevel@tonic-gate /* 2057c478bd9Sstevel@tonic-gate * df_get_octet(): gets the integer value of a given user-tunable parameter 2067c478bd9Sstevel@tonic-gate * 2077c478bd9Sstevel@tonic-gate * input: const char *: the interface the parameter applies to 2087c478bd9Sstevel@tonic-gate * unsigned int: the parameter number to look up 2097c478bd9Sstevel@tonic-gate * unsigned int *: the length of the returned value 2107c478bd9Sstevel@tonic-gate * output: uchar_t *: a pointer to byte array (default value if not set) 2117c478bd9Sstevel@tonic-gate * (must be copied by caller to be kept) 2127c478bd9Sstevel@tonic-gate */ 2137c478bd9Sstevel@tonic-gate 2147c478bd9Sstevel@tonic-gate uchar_t * 2157c478bd9Sstevel@tonic-gate df_get_octet(const char *if_name, unsigned int p, unsigned int *len) 2167c478bd9Sstevel@tonic-gate { 2177c478bd9Sstevel@tonic-gate const char *value; 2187c478bd9Sstevel@tonic-gate static uchar_t octet_value[256]; /* as big as defread() returns */ 2197c478bd9Sstevel@tonic-gate 2207c478bd9Sstevel@tonic-gate if (p >= (sizeof (defaults) / sizeof (*defaults))) 2217c478bd9Sstevel@tonic-gate return (NULL); 2227c478bd9Sstevel@tonic-gate 2237c478bd9Sstevel@tonic-gate value = df_get_string(if_name, p); 2247c478bd9Sstevel@tonic-gate if (value == NULL) 2257c478bd9Sstevel@tonic-gate goto do_default; 2267c478bd9Sstevel@tonic-gate 2277c478bd9Sstevel@tonic-gate if (strncasecmp("0x", value, 2) != 0) { 2287c478bd9Sstevel@tonic-gate *len = strlen(value); /* no NUL */ 2297c478bd9Sstevel@tonic-gate return ((uchar_t *)value); 2307c478bd9Sstevel@tonic-gate } 2317c478bd9Sstevel@tonic-gate 2327c478bd9Sstevel@tonic-gate /* skip past the 0x and convert the value to binary */ 2337c478bd9Sstevel@tonic-gate value += 2; 2347c478bd9Sstevel@tonic-gate *len = sizeof (octet_value); 2357c478bd9Sstevel@tonic-gate if (hexascii_to_octet(value, strlen(value), octet_value, len) != 0) { 2367c478bd9Sstevel@tonic-gate dhcpmsg(MSG_WARNING, "df_get_octet: cannot convert value " 2377c478bd9Sstevel@tonic-gate "for parameter `%s', using default", defaults[p].df_name); 2387c478bd9Sstevel@tonic-gate goto do_default; 2397c478bd9Sstevel@tonic-gate } 2407c478bd9Sstevel@tonic-gate return (octet_value); 2417c478bd9Sstevel@tonic-gate 2427c478bd9Sstevel@tonic-gate do_default: 2437c478bd9Sstevel@tonic-gate if (defaults[p].df_default == NULL) { 2447c478bd9Sstevel@tonic-gate *len = 0; 2457c478bd9Sstevel@tonic-gate return (NULL); 2467c478bd9Sstevel@tonic-gate } 2477c478bd9Sstevel@tonic-gate 2487c478bd9Sstevel@tonic-gate *len = strlen(defaults[p].df_default); /* no NUL */ 2497c478bd9Sstevel@tonic-gate return ((uchar_t *)defaults[p].df_default); 2507c478bd9Sstevel@tonic-gate } 2517c478bd9Sstevel@tonic-gate 2527c478bd9Sstevel@tonic-gate /* 2537c478bd9Sstevel@tonic-gate * df_get_int(): gets the integer value of a given user-tunable parameter 2547c478bd9Sstevel@tonic-gate * 2557c478bd9Sstevel@tonic-gate * input: const char *: the interface the parameter applies to 2567c478bd9Sstevel@tonic-gate * unsigned int: the parameter number to look up 2577c478bd9Sstevel@tonic-gate * output: int: the parameter's value, or default if not set 2587c478bd9Sstevel@tonic-gate */ 2597c478bd9Sstevel@tonic-gate 2607c478bd9Sstevel@tonic-gate int 2617c478bd9Sstevel@tonic-gate df_get_int(const char *if_name, unsigned int p) 2627c478bd9Sstevel@tonic-gate { 2637c478bd9Sstevel@tonic-gate const char *value; 2647c478bd9Sstevel@tonic-gate int value_int; 2657c478bd9Sstevel@tonic-gate 2667c478bd9Sstevel@tonic-gate if (p >= (sizeof (defaults) / sizeof (*defaults))) 2677c478bd9Sstevel@tonic-gate return (0); 2687c478bd9Sstevel@tonic-gate 2697c478bd9Sstevel@tonic-gate value = df_get_string(if_name, p); 2707c478bd9Sstevel@tonic-gate if (value == NULL || !isdigit(*value)) 2717c478bd9Sstevel@tonic-gate goto failure; 2727c478bd9Sstevel@tonic-gate 2737c478bd9Sstevel@tonic-gate value_int = atoi(value); 2747c478bd9Sstevel@tonic-gate if (value_int > defaults[p].df_max || value_int < defaults[p].df_min) 2757c478bd9Sstevel@tonic-gate goto failure; 2767c478bd9Sstevel@tonic-gate 2777c478bd9Sstevel@tonic-gate return (value_int); 2787c478bd9Sstevel@tonic-gate 2797c478bd9Sstevel@tonic-gate failure: 2807c478bd9Sstevel@tonic-gate dhcpmsg(MSG_WARNING, "df_get_int: parameter `%s' is not between %d and " 2817c478bd9Sstevel@tonic-gate "%d, defaulting to `%s'", defaults[p].df_name, defaults[p].df_min, 2827c478bd9Sstevel@tonic-gate defaults[p].df_max, defaults[p].df_default); 2837c478bd9Sstevel@tonic-gate return (atoi(defaults[p].df_default)); 2847c478bd9Sstevel@tonic-gate } 2857c478bd9Sstevel@tonic-gate 2867c478bd9Sstevel@tonic-gate /* 2877c478bd9Sstevel@tonic-gate * df_get_bool(): gets the boolean value of a given user-tunable parameter 2887c478bd9Sstevel@tonic-gate * 2897c478bd9Sstevel@tonic-gate * input: const char *: the interface the parameter applies to 2907c478bd9Sstevel@tonic-gate * unsigned int: the parameter number to look up 2917c478bd9Sstevel@tonic-gate * output: boolean_t: B_TRUE if true, B_FALSE if false, default if not set 2927c478bd9Sstevel@tonic-gate */ 2937c478bd9Sstevel@tonic-gate 2947c478bd9Sstevel@tonic-gate boolean_t 2957c478bd9Sstevel@tonic-gate df_get_bool(const char *if_name, unsigned int p) 2967c478bd9Sstevel@tonic-gate { 2977c478bd9Sstevel@tonic-gate const char *value; 2987c478bd9Sstevel@tonic-gate 2997c478bd9Sstevel@tonic-gate if (p >= (sizeof (defaults) / sizeof (*defaults))) 3007c478bd9Sstevel@tonic-gate return (0); 3017c478bd9Sstevel@tonic-gate 3027c478bd9Sstevel@tonic-gate value = df_get_string(if_name, p); 3037c478bd9Sstevel@tonic-gate if (value != NULL) { 3047c478bd9Sstevel@tonic-gate 3057c478bd9Sstevel@tonic-gate if (strcasecmp(value, "true") == 0 || 3067c478bd9Sstevel@tonic-gate strcasecmp(value, "yes") == 0 || strcmp(value, "1") == 0) 3077c478bd9Sstevel@tonic-gate return (B_TRUE); 3087c478bd9Sstevel@tonic-gate 3097c478bd9Sstevel@tonic-gate if (strcasecmp(value, "false") == 0 || 3107c478bd9Sstevel@tonic-gate strcasecmp(value, "no") == 0 || strcmp(value, "0") == 0) 3117c478bd9Sstevel@tonic-gate return (B_FALSE); 3127c478bd9Sstevel@tonic-gate } 3137c478bd9Sstevel@tonic-gate 3147c478bd9Sstevel@tonic-gate dhcpmsg(MSG_WARNING, "df_get_bool: parameter `%s' has invalid value " 3157c478bd9Sstevel@tonic-gate "`%s', defaulting to `%s'", defaults[p].df_name, 3167c478bd9Sstevel@tonic-gate value ? value : "NULL", defaults[p].df_default); 3177c478bd9Sstevel@tonic-gate 3187c478bd9Sstevel@tonic-gate return ((atoi(defaults[p].df_default) == 0) ? B_FALSE : B_TRUE); 3197c478bd9Sstevel@tonic-gate } 320