summaryrefslogtreecommitdiff
path: root/usr/src/cmd/cmd-inet/sbin/dhcpagent/defaults.c
blob: e6a88304a7528b0fbd9b53f2d795469dcd0106b2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 * Copyright (c) 2016-2017, Chris Fraire <cfraire@me.com>.
 */

#include <sys/types.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <dhcpmsg.h>
#include <stdio.h>
#include <sys/stat.h>
#include <libnvpair.h>
#include <zone.h>

#include "common.h"
#include "defaults.h"

struct dhcp_default {

	const char	*df_name;	/* parameter name */
	const char	*df_default;	/* default value */
	int		df_min;		/* min value if type DF_INTEGER */
	int		df_max;		/* max value if type DF_INTEGER */
};

/*
 * note: keep in the same order as tunable parameter constants in defaults.h
 */

static struct dhcp_default defaults[] = {

	{ "RELEASE_ON_SIGTERM",  "0",	 0,   0	  },
	{ "IGNORE_FAILED_ARP",	 "1",	 0,   -1  },
	{ "OFFER_WAIT",		 "3",	 1,   20  },
	{ "ARP_WAIT",		 "1000", 0,   -1  },
	{ "CLIENT_ID",		 NULL,	 0,   0	  },
	{ "PARAM_REQUEST_LIST",  NULL,	 0,   0   },
	{ "REQUEST_HOSTNAME",	 "1",	 0,   0	  },
	{ "DEBUG_LEVEL",	 "0",	 0,   3   },
	{ "VERBOSE",		 "0",	 0,   0   },
	{ "VERIFIED_LEASE_ONLY", "0",	 0,   0	  },
	{ "PARAM_IGNORE_LIST",	 NULL,	 0,   0   },
	{ "REQUEST_FQDN",	 "1",	 0,   0	  },
	{ "V4_DEFAULT_IAID_DUID",  "0",	 0,   0	  },
	{ "DNS_DOMAINNAME",  NULL,	 0,   0	  },
	{ "ADOPT_DOMAINNAME",	 "0",	 0,   0	  },
};


/*
 * df_find_defaults(): builds the path to the default configuration file
 *
 *   input: void
 *  output: void
 */

static const char *
df_find_defaults(void)
{
	static char agent_defaults_path[MAXPATHLEN] = { 0 };
	const char	*zroot = NULL;

	if (agent_defaults_path[0] != '\0') {
		return agent_defaults_path;
	}

	zroot = zone_get_nroot();

	(void) snprintf(agent_defaults_path, MAXPATHLEN, "%s%s",
	    zroot != NULL ?  zroot : "", DHCP_AGENT_DEFAULTS);

	return agent_defaults_path;
}

/*
 * df_build_cache(): builds the defaults nvlist cache
 *
 *   input: void
 *  output: a pointer to an nvlist of the current defaults, or NULL on failure
 */

static nvlist_t *
df_build_cache(void)
{
	const char	*agent_defaults_path = df_find_defaults();
	char		entry[1024];
	int		i;
	char		*param, *pastv6, *value, *end;
	FILE		*fp;
	nvlist_t 	*nvlist;
	struct dhcp_default *defp;

	if ((fp = fopen(agent_defaults_path, "r")) == NULL)
		return (NULL);

	if (nvlist_alloc(&nvlist, NV_UNIQUE_NAME, 0) != 0) {
		dhcpmsg(MSG_WARNING, "cannot build default value cache; "
		    "using built-in defaults");
		(void) fclose(fp);
		return (NULL);
	}

	while (fgets(entry, sizeof (entry), fp) != NULL) {
		for (i = 0; entry[i] == ' '; i++)
			;

		end = strrchr(entry, '\n');
		value = strchr(entry, '=');
		if (end == NULL || value == NULL || entry[i] == '#')
			continue;

		*end = '\0';
		*value++ = '\0';

		/*
		 * to be compatible with the old defread()-based code
		 * which ignored case, store the parameters (except for the
		 * leading interface name) in upper case.
		 */

		if ((param = strchr(entry, '.')) == NULL) {
			pastv6 = param = entry;
		} else {
			pastv6 = ++param;
			if (strncasecmp(param, "v6.", 3) == 0)
				pastv6 += 3;
		}

		for (defp = defaults;
		    (char *)defp < (char *)defaults + sizeof (defaults);
		    defp++) {
			if (strcasecmp(pastv6, defp->df_name) == 0) {
				if (defp->df_max == -1) {
					dhcpmsg(MSG_WARNING, "parameter %s is "
					    "obsolete; ignored", defp->df_name);
				}
				break;
			}
		}

		for (; *param != '\0'; param++)
			*param = toupper(*param);

		if (nvlist_add_string(nvlist, &entry[i], value) != 0) {
			dhcpmsg(MSG_WARNING, "cannot build default value cache;"
			    " using built-in defaults");
			nvlist_free(nvlist);
			nvlist = NULL;
			break;
		}
	}

	(void) fclose(fp);
	return (nvlist);
}

/*
 * df_get_string(): gets the string value of a given user-tunable parameter
 *
 *   input: const char *: the interface the parameter applies to
 *	    boolean_t: B_TRUE for DHCPv6, B_FALSE for IPv4 DHCP
 *	    uint_t: the parameter number to look up
 *  output: const char *: the parameter's value, or default if not set
 *			  (must be copied by caller to be kept)
 *    NOTE: df_get_string() is both used by functions outside this source
 *	    file to retrieve strings from the defaults file, *and*
 *	    internally by other df_get_*() functions.
 */

const char *
df_get_string(const char *if_name, boolean_t isv6, uint_t param)
{
	const char		*agent_defaults_path = df_find_defaults();
	char			*value;
	char			paramstr[256];
	char			name[256];
	struct stat		statbuf;
	static struct stat	df_statbuf;
	static boolean_t	df_unavail_msg = B_FALSE;
	static nvlist_t		*df_nvlist = NULL;

	if (param >= (sizeof (defaults) / sizeof (*defaults)))
		return (NULL);


	if (stat(agent_defaults_path, &statbuf) != 0) {
		if (!df_unavail_msg) {
			dhcpmsg(MSG_WARNING, "cannot access %s; using "
			    "built-in defaults", agent_defaults_path);
			df_unavail_msg = B_TRUE;
		}
		return (defaults[param].df_default);
	}

	/*
	 * if our cached parameters are stale, rebuild.
	 */

	if (statbuf.st_mtime != df_statbuf.st_mtime ||
	    statbuf.st_size != df_statbuf.st_size) {
		df_statbuf = statbuf;
		nvlist_free(df_nvlist);
		df_nvlist = df_build_cache();
	}

	if (isv6) {
		(void) snprintf(name, sizeof (name), ".V6.%s",
		    defaults[param].df_name);
		(void) snprintf(paramstr, sizeof (paramstr), "%s%s", if_name,
		    name);
	} else {
		(void) strlcpy(name, defaults[param].df_name, sizeof (name));
		(void) snprintf(paramstr, sizeof (paramstr), "%s.%s", if_name,
		    name);
	}

	/*
	 * first look for `if_name.[v6.]param', then `[v6.]param'.  if neither
	 * has been set, use the built-in default.
	 */

	if (nvlist_lookup_string(df_nvlist, paramstr, &value) == 0 ||
	    nvlist_lookup_string(df_nvlist, name, &value) == 0)
		return (value);

	return (defaults[param].df_default);
}

/*
 * df_get_int(): gets the integer value of a given user-tunable parameter
 *
 *   input: const char *: the interface the parameter applies to
 *	    boolean_t: B_TRUE for DHCPv6, B_FALSE for IPv4 DHCP
 *	    uint_t: the parameter number to look up
 *  output: int: the parameter's value, or default if not set
 */

int
df_get_int(const char *if_name, boolean_t isv6, uint_t param)
{
	const char	*value;
	int		value_int;

	if (param >= (sizeof (defaults) / sizeof (*defaults)))
		return (0);

	value = df_get_string(if_name, isv6, param);
	if (value == NULL || !isdigit(*value))
		goto failure;

	value_int = atoi(value);
	if (value_int > defaults[param].df_max ||
	    value_int < defaults[param].df_min)
		goto failure;

	return (value_int);

failure:
	dhcpmsg(MSG_WARNING, "df_get_int: parameter `%s' is not between %d and "
	    "%d, defaulting to `%s'", defaults[param].df_name,
	    defaults[param].df_min, defaults[param].df_max,
	    defaults[param].df_default);
	return (atoi(defaults[param].df_default));
}

/*
 * df_get_bool(): gets the boolean value of a given user-tunable parameter
 *
 *   input: const char *: the interface the parameter applies to
 *	    boolean_t: B_TRUE for DHCPv6, B_FALSE for IPv4 DHCP
 *	    uint_t: the parameter number to look up
 *  output: boolean_t: B_TRUE if true, B_FALSE if false, default if not set
 */

boolean_t
df_get_bool(const char *if_name, boolean_t isv6, uint_t param)
{
	const char	*value;

	if (param >= (sizeof (defaults) / sizeof (*defaults)))
		return (0);

	value = df_get_string(if_name, isv6, param);
	if (value != NULL) {

		if (strcasecmp(value, "true") == 0 ||
		    strcasecmp(value, "yes") == 0 || strcmp(value, "1") == 0)
			return (B_TRUE);

		if (strcasecmp(value, "false") == 0 ||
		    strcasecmp(value, "no") == 0 || strcmp(value, "0") == 0)
			return (B_FALSE);
	}

	dhcpmsg(MSG_WARNING, "df_get_bool: parameter `%s' has invalid value "
	    "`%s', defaulting to `%s'", defaults[param].df_name,
	    value != NULL ? value : "NULL", defaults[param].df_default);

	return ((atoi(defaults[param].df_default) == 0) ? B_FALSE : B_TRUE);
}