1 /*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21 /*
22 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
24 * Copyright (c) 2016-2017, Chris Fraire <cfraire@me.com>.
25 */
26
27 #include <sys/types.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <ctype.h>
31 #include <dhcpmsg.h>
32 #include <stdio.h>
33 #include <sys/stat.h>
34 #include <libnvpair.h>
35
36 #include "common.h"
37 #include "defaults.h"
38
39 struct dhcp_default {
40
41 const char *df_name; /* parameter name */
42 const char *df_default; /* default value */
43 int df_min; /* min value if type DF_INTEGER */
44 int df_max; /* max value if type DF_INTEGER */
45 };
46
47 /*
48 * note: keep in the same order as tunable parameter constants in defaults.h
49 */
50
51 static struct dhcp_default defaults[] = {
52
53 { "RELEASE_ON_SIGTERM", "0", 0, 0 },
54 { "IGNORE_FAILED_ARP", "1", 0, -1 },
55 { "OFFER_WAIT", "3", 1, 20 },
56 { "ARP_WAIT", "1000", 0, -1 },
57 { "CLIENT_ID", NULL, 0, 0 },
58 { "PARAM_REQUEST_LIST", NULL, 0, 0 },
59 { "REQUEST_HOSTNAME", "1", 0, 0 },
60 { "DEBUG_LEVEL", "0", 0, 3 },
61 { "VERBOSE", "0", 0, 0 },
62 { "VERIFIED_LEASE_ONLY", "0", 0, 0 },
63 { "PARAM_IGNORE_LIST", NULL, 0, 0 },
64 { "REQUEST_FQDN", "1", 0, 0 },
65 { "V4_DEFAULT_IAID_DUID", "0", 0, 0 },
66 { "DNS_DOMAINNAME", NULL, 0, 0 },
67 { "ADOPT_DOMAINNAME", "0", 0, 0 },
68 };
69
70 /*
71 * df_build_cache(): builds the defaults nvlist cache
72 *
73 * input: void
74 * output: a pointer to an nvlist of the current defaults, or NULL on failure
75 */
76
77 static nvlist_t *
df_build_cache(void)78 df_build_cache(void)
79 {
80 char entry[1024];
81 int i;
82 char *param, *pastv6, *value, *end;
83 FILE *fp;
84 nvlist_t *nvlist;
85 struct dhcp_default *defp;
86
87 if ((fp = fopen(DHCP_AGENT_DEFAULTS, "r")) == NULL)
88 return (NULL);
89
90 if (nvlist_alloc(&nvlist, NV_UNIQUE_NAME, 0) != 0) {
91 dhcpmsg(MSG_WARNING, "cannot build default value cache; "
92 "using built-in defaults");
93 (void) fclose(fp);
94 return (NULL);
95 }
96
97 while (fgets(entry, sizeof (entry), fp) != NULL) {
98 for (i = 0; entry[i] == ' '; i++)
99 ;
100
101 end = strrchr(entry, '\n');
102 value = strchr(entry, '=');
103 if (end == NULL || value == NULL || entry[i] == '#')
104 continue;
105
106 *end = '\0';
107 *value++ = '\0';
108
109 /*
110 * to be compatible with the old defread()-based code
111 * which ignored case, store the parameters (except for the
112 * leading interface name) in upper case.
113 */
114
115 if ((param = strchr(entry, '.')) == NULL) {
116 pastv6 = param = entry;
117 } else {
118 pastv6 = ++param;
119 if (strncasecmp(param, "v6.", 3) == 0)
120 pastv6 += 3;
121 }
122
123 for (defp = defaults;
124 (char *)defp < (char *)defaults + sizeof (defaults);
125 defp++) {
126 if (strcasecmp(pastv6, defp->df_name) == 0) {
127 if (defp->df_max == -1) {
128 dhcpmsg(MSG_WARNING, "parameter %s is "
129 "obsolete; ignored", defp->df_name);
130 }
131 break;
132 }
133 }
134
135 for (; *param != '\0'; param++)
136 *param = toupper(*param);
137
138 if (nvlist_add_string(nvlist, &entry[i], value) != 0) {
139 dhcpmsg(MSG_WARNING, "cannot build default value cache;"
140 " using built-in defaults");
141 nvlist_free(nvlist);
142 nvlist = NULL;
143 break;
144 }
145 }
146
147 (void) fclose(fp);
148 return (nvlist);
149 }
150
151 /*
152 * df_get_string(): gets the string value of a given user-tunable parameter
153 *
154 * input: const char *: the interface the parameter applies to
155 * boolean_t: B_TRUE for DHCPv6, B_FALSE for IPv4 DHCP
156 * uint_t: the parameter number to look up
157 * output: const char *: the parameter's value, or default if not set
158 * (must be copied by caller to be kept)
159 * NOTE: df_get_string() is both used by functions outside this source
160 * file to retrieve strings from the defaults file, *and*
161 * internally by other df_get_*() functions.
162 */
163
164 const char *
df_get_string(const char * if_name,boolean_t isv6,uint_t param)165 df_get_string(const char *if_name, boolean_t isv6, uint_t param)
166 {
167 char *value;
168 char paramstr[256];
169 char name[256];
170 struct stat statbuf;
171 static struct stat df_statbuf;
172 static boolean_t df_unavail_msg = B_FALSE;
173 static nvlist_t *df_nvlist = NULL;
174
175 if (param >= (sizeof (defaults) / sizeof (*defaults)))
176 return (NULL);
177
178 if (stat(DHCP_AGENT_DEFAULTS, &statbuf) != 0) {
179 if (!df_unavail_msg) {
180 dhcpmsg(MSG_WARNING, "cannot access %s; using "
181 "built-in defaults", DHCP_AGENT_DEFAULTS);
182 df_unavail_msg = B_TRUE;
183 }
184 return (defaults[param].df_default);
185 }
186
187 /*
188 * if our cached parameters are stale, rebuild.
189 */
190
191 if (statbuf.st_mtime != df_statbuf.st_mtime ||
192 statbuf.st_size != df_statbuf.st_size) {
193 df_statbuf = statbuf;
194 nvlist_free(df_nvlist);
195 df_nvlist = df_build_cache();
196 }
197
198 if (isv6) {
199 (void) snprintf(name, sizeof (name), ".V6.%s",
200 defaults[param].df_name);
201 (void) snprintf(paramstr, sizeof (paramstr), "%s%s", if_name,
202 name);
203 } else {
204 (void) strlcpy(name, defaults[param].df_name, sizeof (name));
205 (void) snprintf(paramstr, sizeof (paramstr), "%s.%s", if_name,
206 name);
207 }
208
209 /*
210 * first look for `if_name.[v6.]param', then `[v6.]param'. if neither
211 * has been set, use the built-in default.
212 */
213
214 if (nvlist_lookup_string(df_nvlist, paramstr, &value) == 0 ||
215 nvlist_lookup_string(df_nvlist, name, &value) == 0)
216 return (value);
217
218 return (defaults[param].df_default);
219 }
220
221 /*
222 * df_get_int(): gets the integer value of a given user-tunable parameter
223 *
224 * input: const char *: the interface the parameter applies to
225 * boolean_t: B_TRUE for DHCPv6, B_FALSE for IPv4 DHCP
226 * uint_t: the parameter number to look up
227 * output: int: the parameter's value, or default if not set
228 */
229
230 int
df_get_int(const char * if_name,boolean_t isv6,uint_t param)231 df_get_int(const char *if_name, boolean_t isv6, uint_t param)
232 {
233 const char *value;
234 int value_int;
235
236 if (param >= (sizeof (defaults) / sizeof (*defaults)))
237 return (0);
238
239 value = df_get_string(if_name, isv6, param);
240 if (value == NULL || !isdigit(*value))
241 goto failure;
242
243 value_int = atoi(value);
244 if (value_int > defaults[param].df_max ||
245 value_int < defaults[param].df_min)
246 goto failure;
247
248 return (value_int);
249
250 failure:
251 dhcpmsg(MSG_WARNING, "df_get_int: parameter `%s' is not between %d and "
252 "%d, defaulting to `%s'", defaults[param].df_name,
253 defaults[param].df_min, defaults[param].df_max,
254 defaults[param].df_default);
255 return (atoi(defaults[param].df_default));
256 }
257
258 /*
259 * df_get_bool(): gets the boolean value of a given user-tunable parameter
260 *
261 * input: const char *: the interface the parameter applies to
262 * boolean_t: B_TRUE for DHCPv6, B_FALSE for IPv4 DHCP
263 * uint_t: the parameter number to look up
264 * output: boolean_t: B_TRUE if true, B_FALSE if false, default if not set
265 */
266
267 boolean_t
df_get_bool(const char * if_name,boolean_t isv6,uint_t param)268 df_get_bool(const char *if_name, boolean_t isv6, uint_t param)
269 {
270 const char *value;
271
272 if (param >= (sizeof (defaults) / sizeof (*defaults)))
273 return (0);
274
275 value = df_get_string(if_name, isv6, param);
276 if (value != NULL) {
277
278 if (strcasecmp(value, "true") == 0 ||
279 strcasecmp(value, "yes") == 0 || strcmp(value, "1") == 0)
280 return (B_TRUE);
281
282 if (strcasecmp(value, "false") == 0 ||
283 strcasecmp(value, "no") == 0 || strcmp(value, "0") == 0)
284 return (B_FALSE);
285 }
286
287 dhcpmsg(MSG_WARNING, "df_get_bool: parameter `%s' has invalid value "
288 "`%s', defaulting to `%s'", defaults[param].df_name,
289 value != NULL ? value : "NULL", defaults[param].df_default);
290
291 return ((atoi(defaults[param].df_default) == 0) ? B_FALSE : B_TRUE);
292 }
293