xref: /onnv-gate/usr/src/common/net/wanboot/bootconf.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2003 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
27*0Sstevel@tonic-gate 
28*0Sstevel@tonic-gate /*
29*0Sstevel@tonic-gate  * Functions for accessing the wanboot.conf(4) file.
30*0Sstevel@tonic-gate  */
31*0Sstevel@tonic-gate 
32*0Sstevel@tonic-gate #include <stdio.h>
33*0Sstevel@tonic-gate #include <string.h>
34*0Sstevel@tonic-gate #include <sys/types.h>
35*0Sstevel@tonic-gate #include <parseURL.h>
36*0Sstevel@tonic-gate #include <netboot_paths.h>
37*0Sstevel@tonic-gate #include <wanboot_conf.h>
38*0Sstevel@tonic-gate 
39*0Sstevel@tonic-gate /*
40*0Sstevel@tonic-gate  * Parser helper macros:
41*0Sstevel@tonic-gate  */
42*0Sstevel@tonic-gate #define	is_whitespace(c)	((c) == ' ' || (c) == '\t')
43*0Sstevel@tonic-gate #define	skip_whitespace(p)	while (is_whitespace(*(p))) ++p
44*0Sstevel@tonic-gate 
45*0Sstevel@tonic-gate /*
46*0Sstevel@tonic-gate  * Table of valid wanboot.conf(4) names:
47*0Sstevel@tonic-gate  */
48*0Sstevel@tonic-gate static const char *bootconf_names[] = {
49*0Sstevel@tonic-gate 	BC_BOOT_FILE,
50*0Sstevel@tonic-gate 	BC_ROOT_SERVER,
51*0Sstevel@tonic-gate 	BC_ROOT_FILE,
52*0Sstevel@tonic-gate 	BC_ENCRYPTION_TYPE,
53*0Sstevel@tonic-gate 	BC_SIGNATURE_TYPE,
54*0Sstevel@tonic-gate 	BC_CLIENT_AUTHENTICATION,
55*0Sstevel@tonic-gate 	BC_SERVER_AUTHENTICATION,
56*0Sstevel@tonic-gate 	BC_BOOT_LOGGER,
57*0Sstevel@tonic-gate 	BC_RESOLVE_HOSTS,
58*0Sstevel@tonic-gate 	BC_SYSTEM_CONF,
59*0Sstevel@tonic-gate 	NULL
60*0Sstevel@tonic-gate };
61*0Sstevel@tonic-gate 
62*0Sstevel@tonic-gate /*
63*0Sstevel@tonic-gate  * Check whether 'name' is valid within wanboot.conf(4).
64*0Sstevel@tonic-gate  */
65*0Sstevel@tonic-gate static boolean_t
valid_name(const char * name)66*0Sstevel@tonic-gate valid_name(const char *name)
67*0Sstevel@tonic-gate {
68*0Sstevel@tonic-gate 	int	i;
69*0Sstevel@tonic-gate 
70*0Sstevel@tonic-gate 	for (i = 0; bootconf_names[i] != NULL; ++i) {
71*0Sstevel@tonic-gate 		if (strcmp(name, bootconf_names[i]) == 0) {
72*0Sstevel@tonic-gate 			return (B_TRUE);
73*0Sstevel@tonic-gate 		}
74*0Sstevel@tonic-gate 	}
75*0Sstevel@tonic-gate 
76*0Sstevel@tonic-gate 	return (B_FALSE);
77*0Sstevel@tonic-gate }
78*0Sstevel@tonic-gate 
79*0Sstevel@tonic-gate /*
80*0Sstevel@tonic-gate  * parse_bootconf() parses a wanboot.conf(4) file and, if there are no
81*0Sstevel@tonic-gate  * errors, creates an nvpair list of the name-value pairs defined therein.
82*0Sstevel@tonic-gate  *
83*0Sstevel@tonic-gate  * Lines must be blank or of the form:
84*0Sstevel@tonic-gate  *	[name=value] [# comment]
85*0Sstevel@tonic-gate  *
86*0Sstevel@tonic-gate  * Returns:
87*0Sstevel@tonic-gate  *	B_TRUE	- success
88*0Sstevel@tonic-gate  *	B_FALSE	- error (return code in handle->bc_error_code, line number
89*0Sstevel@tonic-gate  *		  on which the error occurred in handle->bc_error_pos)
90*0Sstevel@tonic-gate  */
91*0Sstevel@tonic-gate static boolean_t
parse_bootconf(bc_handle_t * handle,const char * bootconf)92*0Sstevel@tonic-gate parse_bootconf(bc_handle_t *handle, const char *bootconf)
93*0Sstevel@tonic-gate {
94*0Sstevel@tonic-gate 	FILE		*fp = NULL;
95*0Sstevel@tonic-gate 	nvlist_t	*nvl = NULL;
96*0Sstevel@tonic-gate 	char		line[BC_MAX_LINE_LENGTH];
97*0Sstevel@tonic-gate 
98*0Sstevel@tonic-gate 	if ((fp = fopen(bootconf, "r")) == NULL) {
99*0Sstevel@tonic-gate 		handle->bc_error_code = BC_E_ACCESS;
100*0Sstevel@tonic-gate 		goto cleanup;
101*0Sstevel@tonic-gate 	}
102*0Sstevel@tonic-gate 
103*0Sstevel@tonic-gate 	if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) {
104*0Sstevel@tonic-gate 		handle->bc_error_code = BC_E_NVLIST;
105*0Sstevel@tonic-gate 		goto cleanup;
106*0Sstevel@tonic-gate 	}
107*0Sstevel@tonic-gate 
108*0Sstevel@tonic-gate 	while (fgets(line, sizeof (line), fp) != NULL) {
109*0Sstevel@tonic-gate 		int	i;
110*0Sstevel@tonic-gate 		char	*p = line;
111*0Sstevel@tonic-gate 		char	*ks, *ke, *vs, *ve;
112*0Sstevel@tonic-gate 		char	quote;
113*0Sstevel@tonic-gate 
114*0Sstevel@tonic-gate 		++(handle->bc_error_pos);
115*0Sstevel@tonic-gate 
116*0Sstevel@tonic-gate 		/*
117*0Sstevel@tonic-gate 		 * Strip off the '\n' at the end of the line.
118*0Sstevel@tonic-gate 		 */
119*0Sstevel@tonic-gate 		if ((i = strlen(line)) < 1) {
120*0Sstevel@tonic-gate 			handle->bc_error_code = BC_E_IOERR;
121*0Sstevel@tonic-gate 			goto cleanup;
122*0Sstevel@tonic-gate 		} else if (line[i - 1] != '\n') {
123*0Sstevel@tonic-gate 			handle->bc_error_code = BC_E_TOO_LONG;
124*0Sstevel@tonic-gate 			goto cleanup;
125*0Sstevel@tonic-gate 		}
126*0Sstevel@tonic-gate 		line[i - 1] = '\0';
127*0Sstevel@tonic-gate 
128*0Sstevel@tonic-gate 		/*
129*0Sstevel@tonic-gate 		 * Skip leading whitespace.
130*0Sstevel@tonic-gate 		 */
131*0Sstevel@tonic-gate 		skip_whitespace(p);
132*0Sstevel@tonic-gate 
133*0Sstevel@tonic-gate 		/*
134*0Sstevel@tonic-gate 		 * Blank line/comment-only line?
135*0Sstevel@tonic-gate 		 */
136*0Sstevel@tonic-gate 		if (*p == '\0' || *p == '#') {
137*0Sstevel@tonic-gate 			continue;
138*0Sstevel@tonic-gate 		}
139*0Sstevel@tonic-gate 
140*0Sstevel@tonic-gate 		/*
141*0Sstevel@tonic-gate 		 * Get start and end pointers to the 'name'.
142*0Sstevel@tonic-gate 		 */
143*0Sstevel@tonic-gate 		ks = p;
144*0Sstevel@tonic-gate 		while (!is_whitespace(*p) && *p != '=') {
145*0Sstevel@tonic-gate 			++p;
146*0Sstevel@tonic-gate 		}
147*0Sstevel@tonic-gate 		ke = p;
148*0Sstevel@tonic-gate 
149*0Sstevel@tonic-gate 		/*
150*0Sstevel@tonic-gate 		 * Must be of the form "name=value"; skip leading and
151*0Sstevel@tonic-gate 		 * trailing whitespace.
152*0Sstevel@tonic-gate 		 */
153*0Sstevel@tonic-gate 		skip_whitespace(p);
154*0Sstevel@tonic-gate 		if (*p == '=') {
155*0Sstevel@tonic-gate 			++p;		/* skip '=' */
156*0Sstevel@tonic-gate 			skip_whitespace(p);
157*0Sstevel@tonic-gate 		} else {
158*0Sstevel@tonic-gate 			handle->bc_error_code = BC_E_SYNTAX;
159*0Sstevel@tonic-gate 			goto cleanup;
160*0Sstevel@tonic-gate 		}
161*0Sstevel@tonic-gate 
162*0Sstevel@tonic-gate 		/*
163*0Sstevel@tonic-gate 		 * The 'value' may be quoted.
164*0Sstevel@tonic-gate 		 */
165*0Sstevel@tonic-gate 		if (*p == '"' || *p == '\'') {
166*0Sstevel@tonic-gate 			quote = *p;
167*0Sstevel@tonic-gate 			++p;		/* skip '"' */
168*0Sstevel@tonic-gate 		} else {
169*0Sstevel@tonic-gate 			quote = '\0';
170*0Sstevel@tonic-gate 		}
171*0Sstevel@tonic-gate 
172*0Sstevel@tonic-gate 		/*
173*0Sstevel@tonic-gate 		 * Get start and end pointers to the 'value' string.
174*0Sstevel@tonic-gate 		 * Note that 'value' may be the empty string.
175*0Sstevel@tonic-gate 		 */
176*0Sstevel@tonic-gate 		vs = p;
177*0Sstevel@tonic-gate 		if (quote != '\0' || *p != '#') {
178*0Sstevel@tonic-gate 			while (*p != '\0' && *p != quote) {
179*0Sstevel@tonic-gate 				/*
180*0Sstevel@tonic-gate 				 * White space that is not part of a quoted
181*0Sstevel@tonic-gate 				 * value signals end of value.
182*0Sstevel@tonic-gate 				 */
183*0Sstevel@tonic-gate 				if (is_whitespace(*p) && quote == '\0') {
184*0Sstevel@tonic-gate 					break;
185*0Sstevel@tonic-gate 				}
186*0Sstevel@tonic-gate 				++p;
187*0Sstevel@tonic-gate 			}
188*0Sstevel@tonic-gate 		}
189*0Sstevel@tonic-gate 		ve = p;
190*0Sstevel@tonic-gate 
191*0Sstevel@tonic-gate 		/*
192*0Sstevel@tonic-gate 		 * If 'value' string was quoted, ensure that there is a
193*0Sstevel@tonic-gate 		 * balancing close-quote and skip it.
194*0Sstevel@tonic-gate 		 */
195*0Sstevel@tonic-gate 		if (quote != '\0') {
196*0Sstevel@tonic-gate 			if (*p == quote) {
197*0Sstevel@tonic-gate 				++p;
198*0Sstevel@tonic-gate 			} else {
199*0Sstevel@tonic-gate 				handle->bc_error_code = BC_E_SYNTAX;
200*0Sstevel@tonic-gate 				goto cleanup;
201*0Sstevel@tonic-gate 			}
202*0Sstevel@tonic-gate 		}
203*0Sstevel@tonic-gate 
204*0Sstevel@tonic-gate 		/*
205*0Sstevel@tonic-gate 		 * Verify line is well-formed; the rest of the line should
206*0Sstevel@tonic-gate 		 * be blank or comment.
207*0Sstevel@tonic-gate 		 */
208*0Sstevel@tonic-gate 		skip_whitespace(p);
209*0Sstevel@tonic-gate 		if (*p != '\0' && *p != '#') {
210*0Sstevel@tonic-gate 			handle->bc_error_code = BC_E_SYNTAX;
211*0Sstevel@tonic-gate 			goto cleanup;
212*0Sstevel@tonic-gate 		}
213*0Sstevel@tonic-gate 
214*0Sstevel@tonic-gate 		/*
215*0Sstevel@tonic-gate 		 * Nul-terminate both the 'name' and the 'value' string.
216*0Sstevel@tonic-gate 		 */
217*0Sstevel@tonic-gate 		*ke = '\0';
218*0Sstevel@tonic-gate 		*ve = '\0';
219*0Sstevel@tonic-gate 
220*0Sstevel@tonic-gate 		/*
221*0Sstevel@tonic-gate 		 * Check that this is a valid parameter name.
222*0Sstevel@tonic-gate 		 */
223*0Sstevel@tonic-gate 		if (!valid_name(ks)) {
224*0Sstevel@tonic-gate 			handle->bc_error_code = BC_E_UNKNOWN_NAME;
225*0Sstevel@tonic-gate 			goto cleanup;
226*0Sstevel@tonic-gate 		}
227*0Sstevel@tonic-gate 
228*0Sstevel@tonic-gate 		/*
229*0Sstevel@tonic-gate 		 * Add the name-value pair to the nvpair list.
230*0Sstevel@tonic-gate 		 */
231*0Sstevel@tonic-gate 		if (nvlist_add_string(nvl, ks, vs) != 0) {
232*0Sstevel@tonic-gate 			handle->bc_error_code = BC_E_NVLIST;
233*0Sstevel@tonic-gate 			goto cleanup;
234*0Sstevel@tonic-gate 		}
235*0Sstevel@tonic-gate 	}
236*0Sstevel@tonic-gate 
237*0Sstevel@tonic-gate 	/*
238*0Sstevel@tonic-gate 	 * Verify that we didn't exit the parsing loop because of an
239*0Sstevel@tonic-gate 	 * input error.
240*0Sstevel@tonic-gate 	 */
241*0Sstevel@tonic-gate 	if (ferror(fp)) {
242*0Sstevel@tonic-gate 		handle->bc_error_code = BC_E_IOERR;
243*0Sstevel@tonic-gate 		goto cleanup;
244*0Sstevel@tonic-gate 	}
245*0Sstevel@tonic-gate 
246*0Sstevel@tonic-gate cleanup:
247*0Sstevel@tonic-gate 	/*
248*0Sstevel@tonic-gate 	 * Close the file if open and free the nvlist if an error occurred.
249*0Sstevel@tonic-gate 	 */
250*0Sstevel@tonic-gate 	if (fp != NULL && fclose(fp) != 0) {
251*0Sstevel@tonic-gate 		handle->bc_error_code = BC_E_IOERR;
252*0Sstevel@tonic-gate 	}
253*0Sstevel@tonic-gate 	if (handle->bc_error_code != BC_E_NOERROR) {
254*0Sstevel@tonic-gate 		if (nvl != NULL) {
255*0Sstevel@tonic-gate 			nvlist_free(nvl);
256*0Sstevel@tonic-gate 		}
257*0Sstevel@tonic-gate 		return (B_FALSE);
258*0Sstevel@tonic-gate 	}
259*0Sstevel@tonic-gate 
260*0Sstevel@tonic-gate 	/*
261*0Sstevel@tonic-gate 	 * All is well.
262*0Sstevel@tonic-gate 	 */
263*0Sstevel@tonic-gate 	handle->bc_nvl = nvl;
264*0Sstevel@tonic-gate 
265*0Sstevel@tonic-gate 	return (B_TRUE);
266*0Sstevel@tonic-gate }
267*0Sstevel@tonic-gate 
268*0Sstevel@tonic-gate /*
269*0Sstevel@tonic-gate  * valid_encryption() validitate the encryption type value
270*0Sstevel@tonic-gate  *
271*0Sstevel@tonic-gate  * Returns:
272*0Sstevel@tonic-gate  *	B_TRUE	- success
273*0Sstevel@tonic-gate  *	B_FALSE	- error (return code in handle->bc_error_code)
274*0Sstevel@tonic-gate  */
275*0Sstevel@tonic-gate static boolean_t
valid_encryption(bc_handle_t * handle,boolean_t * is_encrypted)276*0Sstevel@tonic-gate valid_encryption(bc_handle_t *handle, boolean_t *is_encrypted)
277*0Sstevel@tonic-gate {
278*0Sstevel@tonic-gate 	nvlist_t	*nvl = handle->bc_nvl;
279*0Sstevel@tonic-gate 	char		*strval;
280*0Sstevel@tonic-gate 
281*0Sstevel@tonic-gate 	/*
282*0Sstevel@tonic-gate 	 * Until proven otherwise, encryption is not enabled.
283*0Sstevel@tonic-gate 	 */
284*0Sstevel@tonic-gate 	*is_encrypted = B_FALSE;
285*0Sstevel@tonic-gate 
286*0Sstevel@tonic-gate 	/*
287*0Sstevel@tonic-gate 	 * If encryption_type was specified then it must be either
288*0Sstevel@tonic-gate 	 * "3des", "aes" or "".
289*0Sstevel@tonic-gate 	 */
290*0Sstevel@tonic-gate 	if (nvlist_lookup_string(nvl, BC_ENCRYPTION_TYPE, &strval) == 0) {
291*0Sstevel@tonic-gate 		if (strlen(strval) > 0) {
292*0Sstevel@tonic-gate 			if (strcmp(strval, BC_ENCRYPTION_3DES) != 0 &&
293*0Sstevel@tonic-gate 			    strcmp(strval, BC_ENCRYPTION_AES) != 0) {
294*0Sstevel@tonic-gate 				handle->bc_error_code = BC_E_ENCRYPTION_ILLEGAL;
295*0Sstevel@tonic-gate 				return (B_FALSE);
296*0Sstevel@tonic-gate 			}
297*0Sstevel@tonic-gate 			*is_encrypted = B_TRUE;
298*0Sstevel@tonic-gate 		}
299*0Sstevel@tonic-gate 	}
300*0Sstevel@tonic-gate 	return (B_TRUE);
301*0Sstevel@tonic-gate }
302*0Sstevel@tonic-gate 
303*0Sstevel@tonic-gate /*
304*0Sstevel@tonic-gate  * valid_signature() validates the signature type value
305*0Sstevel@tonic-gate  *
306*0Sstevel@tonic-gate  * Returns:
307*0Sstevel@tonic-gate  *	B_TRUE	- success
308*0Sstevel@tonic-gate  *	B_FALSE	- error (return code in handle->bc_error_code)
309*0Sstevel@tonic-gate  */
310*0Sstevel@tonic-gate static boolean_t
valid_signature(bc_handle_t * handle,boolean_t * is_signed)311*0Sstevel@tonic-gate valid_signature(bc_handle_t *handle, boolean_t *is_signed)
312*0Sstevel@tonic-gate {
313*0Sstevel@tonic-gate 	nvlist_t	*nvl = handle->bc_nvl;
314*0Sstevel@tonic-gate 	char		*strval;
315*0Sstevel@tonic-gate 
316*0Sstevel@tonic-gate 	/*
317*0Sstevel@tonic-gate 	 * Until proven otherwise, signing is not enabled.
318*0Sstevel@tonic-gate 	 */
319*0Sstevel@tonic-gate 	*is_signed = B_FALSE;
320*0Sstevel@tonic-gate 
321*0Sstevel@tonic-gate 	/*
322*0Sstevel@tonic-gate 	 * If signature_type was specified then it must be either
323*0Sstevel@tonic-gate 	 * "sha1" or "".
324*0Sstevel@tonic-gate 	 */
325*0Sstevel@tonic-gate 	if (nvlist_lookup_string(nvl, BC_SIGNATURE_TYPE, &strval) == 0) {
326*0Sstevel@tonic-gate 		if (strlen(strval) > 0) {
327*0Sstevel@tonic-gate 			if (strcmp(strval, BC_SIGNATURE_SHA1) != 0) {
328*0Sstevel@tonic-gate 				handle->bc_error_code = BC_E_SIGNATURE_ILLEGAL;
329*0Sstevel@tonic-gate 				return (B_FALSE);
330*0Sstevel@tonic-gate 			}
331*0Sstevel@tonic-gate 			*is_signed = B_TRUE;
332*0Sstevel@tonic-gate 		}
333*0Sstevel@tonic-gate 	}
334*0Sstevel@tonic-gate 
335*0Sstevel@tonic-gate 	return (B_TRUE);
336*0Sstevel@tonic-gate }
337*0Sstevel@tonic-gate 
338*0Sstevel@tonic-gate /*
339*0Sstevel@tonic-gate  * valid_client_authentication() validates the client authentication value
340*0Sstevel@tonic-gate  *
341*0Sstevel@tonic-gate  * Returns:
342*0Sstevel@tonic-gate  *	B_TRUE	- success
343*0Sstevel@tonic-gate  *	B_FALSE	- error (return code in handle->bc_error_code)
344*0Sstevel@tonic-gate  */
345*0Sstevel@tonic-gate static boolean_t
valid_client_authentication(bc_handle_t * handle,boolean_t * is_authenticated)346*0Sstevel@tonic-gate valid_client_authentication(bc_handle_t *handle, boolean_t *is_authenticated)
347*0Sstevel@tonic-gate {
348*0Sstevel@tonic-gate 	nvlist_t	*nvl = handle->bc_nvl;
349*0Sstevel@tonic-gate 	char		*strval;
350*0Sstevel@tonic-gate 
351*0Sstevel@tonic-gate 	/*
352*0Sstevel@tonic-gate 	 * Until proven otherwise, authentication is not enabled.
353*0Sstevel@tonic-gate 	 */
354*0Sstevel@tonic-gate 	*is_authenticated = B_FALSE;
355*0Sstevel@tonic-gate 
356*0Sstevel@tonic-gate 	/*
357*0Sstevel@tonic-gate 	 * If client_authentication was specified then it must be either
358*0Sstevel@tonic-gate 	 * "yes" or "no".
359*0Sstevel@tonic-gate 	 */
360*0Sstevel@tonic-gate 	if (nvlist_lookup_string(nvl, BC_CLIENT_AUTHENTICATION, &strval) == 0) {
361*0Sstevel@tonic-gate 		if (strcmp(strval, BC_YES) == 0) {
362*0Sstevel@tonic-gate 			*is_authenticated = B_TRUE;
363*0Sstevel@tonic-gate 		} else if (strcmp(strval, BC_NO) != 0) {
364*0Sstevel@tonic-gate 			handle->bc_error_code = BC_E_CLIENT_AUTH_ILLEGAL;
365*0Sstevel@tonic-gate 			return (B_FALSE);
366*0Sstevel@tonic-gate 		}
367*0Sstevel@tonic-gate 	}
368*0Sstevel@tonic-gate 
369*0Sstevel@tonic-gate 	return (B_TRUE);
370*0Sstevel@tonic-gate }
371*0Sstevel@tonic-gate 
372*0Sstevel@tonic-gate /*
373*0Sstevel@tonic-gate  * valid_server_authentication() validates the server authentication value
374*0Sstevel@tonic-gate  *
375*0Sstevel@tonic-gate  * Returns:
376*0Sstevel@tonic-gate  *	B_TRUE	- success
377*0Sstevel@tonic-gate  *	B_FALSE	- error (return code in handle->bc_error_code)
378*0Sstevel@tonic-gate  */
379*0Sstevel@tonic-gate static boolean_t
valid_server_authentication(bc_handle_t * handle,boolean_t * is_authenticated)380*0Sstevel@tonic-gate valid_server_authentication(bc_handle_t *handle, boolean_t *is_authenticated)
381*0Sstevel@tonic-gate {
382*0Sstevel@tonic-gate 	nvlist_t	*nvl = handle->bc_nvl;
383*0Sstevel@tonic-gate 	char		*strval;
384*0Sstevel@tonic-gate 
385*0Sstevel@tonic-gate 	/*
386*0Sstevel@tonic-gate 	 * Until proven otherwise, authentication is not enabled.
387*0Sstevel@tonic-gate 	 */
388*0Sstevel@tonic-gate 	*is_authenticated = B_FALSE;
389*0Sstevel@tonic-gate 
390*0Sstevel@tonic-gate 	/*
391*0Sstevel@tonic-gate 	 * If server_authentication was specified then it must be either
392*0Sstevel@tonic-gate 	 * "yes" or"no".
393*0Sstevel@tonic-gate 	 */
394*0Sstevel@tonic-gate 	if (nvlist_lookup_string(nvl, BC_SERVER_AUTHENTICATION, &strval) == 0) {
395*0Sstevel@tonic-gate 		if (strcmp(strval, BC_YES) == 0) {
396*0Sstevel@tonic-gate 			*is_authenticated = B_TRUE;
397*0Sstevel@tonic-gate 		} else if (strcmp(strval, BC_NO) != 0) {
398*0Sstevel@tonic-gate 			handle->bc_error_code = BC_E_SERVER_AUTH_ILLEGAL;
399*0Sstevel@tonic-gate 			return (B_FALSE);
400*0Sstevel@tonic-gate 		}
401*0Sstevel@tonic-gate 	}
402*0Sstevel@tonic-gate 
403*0Sstevel@tonic-gate 	return (B_TRUE);
404*0Sstevel@tonic-gate }
405*0Sstevel@tonic-gate 
406*0Sstevel@tonic-gate /*
407*0Sstevel@tonic-gate  * valid_root_server() validates the root server and root file values
408*0Sstevel@tonic-gate  *
409*0Sstevel@tonic-gate  * Returns:
410*0Sstevel@tonic-gate  *	B_TRUE	- success
411*0Sstevel@tonic-gate  *	B_FALSE	- error (return code in handle->bc_error_code)
412*0Sstevel@tonic-gate  */
413*0Sstevel@tonic-gate static boolean_t
valid_root_server(bc_handle_t * handle,boolean_t * is_https)414*0Sstevel@tonic-gate valid_root_server(bc_handle_t *handle, boolean_t *is_https)
415*0Sstevel@tonic-gate {
416*0Sstevel@tonic-gate 	nvlist_t	*nvl = handle->bc_nvl;
417*0Sstevel@tonic-gate 	char		*strval;
418*0Sstevel@tonic-gate 	url_t		url;
419*0Sstevel@tonic-gate 
420*0Sstevel@tonic-gate 	/*
421*0Sstevel@tonic-gate 	 * Until proven otherwise, assume not https.
422*0Sstevel@tonic-gate 	 */
423*0Sstevel@tonic-gate 	*is_https = B_FALSE;
424*0Sstevel@tonic-gate 
425*0Sstevel@tonic-gate 	/*
426*0Sstevel@tonic-gate 	 * Check whether a root_server URL was specified, and if so whether
427*0Sstevel@tonic-gate 	 * it is a secure URL (of the form https://...).
428*0Sstevel@tonic-gate 	 */
429*0Sstevel@tonic-gate 	if (nvlist_lookup_string(nvl, BC_ROOT_SERVER, &strval) == 0) {
430*0Sstevel@tonic-gate 		if (url_parse(strval, &url) != URL_PARSE_SUCCESS) {
431*0Sstevel@tonic-gate 			handle->bc_error_code = BC_E_ROOT_SERVER_BAD;
432*0Sstevel@tonic-gate 			return (B_FALSE);
433*0Sstevel@tonic-gate 		}
434*0Sstevel@tonic-gate 		*is_https = url.https;
435*0Sstevel@tonic-gate 
436*0Sstevel@tonic-gate 		/*
437*0Sstevel@tonic-gate 		 * Ensure that a root_file was also specified.
438*0Sstevel@tonic-gate 		 */
439*0Sstevel@tonic-gate 		if (nvlist_lookup_string(nvl, BC_ROOT_FILE, &strval) != 0 ||
440*0Sstevel@tonic-gate 		    strlen(strval) == 0) {
441*0Sstevel@tonic-gate 			handle->bc_error_code = BC_E_ROOT_FILE_ABSENT;
442*0Sstevel@tonic-gate 			return (B_FALSE);
443*0Sstevel@tonic-gate 		}
444*0Sstevel@tonic-gate 	} else {
445*0Sstevel@tonic-gate 		handle->bc_error_code = BC_E_ROOT_SERVER_ABSENT;
446*0Sstevel@tonic-gate 		return (B_FALSE);
447*0Sstevel@tonic-gate 	}
448*0Sstevel@tonic-gate 
449*0Sstevel@tonic-gate 	return (B_TRUE);
450*0Sstevel@tonic-gate }
451*0Sstevel@tonic-gate 
452*0Sstevel@tonic-gate /*
453*0Sstevel@tonic-gate  * valid_boot_logger() validates the boot_logger value
454*0Sstevel@tonic-gate  *
455*0Sstevel@tonic-gate  * Returns:
456*0Sstevel@tonic-gate  *	B_TRUE	- success
457*0Sstevel@tonic-gate  *	B_FALSE	- error (return code in handle->bc_error_code)
458*0Sstevel@tonic-gate  */
459*0Sstevel@tonic-gate static boolean_t
valid_boot_logger(bc_handle_t * handle,boolean_t * is_https)460*0Sstevel@tonic-gate valid_boot_logger(bc_handle_t *handle, boolean_t *is_https)
461*0Sstevel@tonic-gate {
462*0Sstevel@tonic-gate 	nvlist_t	*nvl = handle->bc_nvl;
463*0Sstevel@tonic-gate 	char		*strval;
464*0Sstevel@tonic-gate 	url_t		url;
465*0Sstevel@tonic-gate 
466*0Sstevel@tonic-gate 	/*
467*0Sstevel@tonic-gate 	 * Until proven otherwise, assume not https.
468*0Sstevel@tonic-gate 	 */
469*0Sstevel@tonic-gate 	*is_https = B_FALSE;
470*0Sstevel@tonic-gate 
471*0Sstevel@tonic-gate 	/*
472*0Sstevel@tonic-gate 	 * If boot_logger was specified, make sure that it is a valid URL.
473*0Sstevel@tonic-gate 	 */
474*0Sstevel@tonic-gate 	if (nvlist_lookup_string(nvl, BC_BOOT_LOGGER, &strval) == 0 &&
475*0Sstevel@tonic-gate 	    strlen(strval) > 0) {
476*0Sstevel@tonic-gate 		if (url_parse(strval, &url) != URL_PARSE_SUCCESS) {
477*0Sstevel@tonic-gate 			handle->bc_error_code = BC_E_BOOT_LOGGER_BAD;
478*0Sstevel@tonic-gate 			return (B_FALSE);
479*0Sstevel@tonic-gate 		}
480*0Sstevel@tonic-gate 		*is_https = url.https;
481*0Sstevel@tonic-gate 	}
482*0Sstevel@tonic-gate 
483*0Sstevel@tonic-gate 	return (B_TRUE);
484*0Sstevel@tonic-gate }
485*0Sstevel@tonic-gate 
486*0Sstevel@tonic-gate /*
487*0Sstevel@tonic-gate  * validate_bootconf() checks the consistency of the nvpair list representation
488*0Sstevel@tonic-gate  * of a wanboot.conf(4) file as returned by the parse_bootconf() function.
489*0Sstevel@tonic-gate  *
490*0Sstevel@tonic-gate  * Returns:
491*0Sstevel@tonic-gate  *	B_TRUE	- success
492*0Sstevel@tonic-gate  *	B_FALSE	- error (return code in handle->bc_error_code)
493*0Sstevel@tonic-gate  */
494*0Sstevel@tonic-gate static boolean_t
validate_bootconf(bc_handle_t * handle)495*0Sstevel@tonic-gate validate_bootconf(bc_handle_t *handle)
496*0Sstevel@tonic-gate {
497*0Sstevel@tonic-gate 	boolean_t	is_encrypted;
498*0Sstevel@tonic-gate 	boolean_t	is_signed;
499*0Sstevel@tonic-gate 	boolean_t	client_is_authenticated;
500*0Sstevel@tonic-gate 	boolean_t	server_is_authenticated;
501*0Sstevel@tonic-gate 	boolean_t	rootserver_is_https;
502*0Sstevel@tonic-gate 	boolean_t	bootlogger_is_https;
503*0Sstevel@tonic-gate 
504*0Sstevel@tonic-gate 	/*
505*0Sstevel@tonic-gate 	 * Check to make sure option values are valid.
506*0Sstevel@tonic-gate 	 */
507*0Sstevel@tonic-gate 	if (!valid_encryption(handle, &is_encrypted) ||
508*0Sstevel@tonic-gate 	    !valid_signature(handle, &is_signed) ||
509*0Sstevel@tonic-gate 	    !valid_client_authentication(handle, &client_is_authenticated) ||
510*0Sstevel@tonic-gate 	    !valid_server_authentication(handle, &server_is_authenticated) ||
511*0Sstevel@tonic-gate 	    !valid_root_server(handle, &rootserver_is_https) ||
512*0Sstevel@tonic-gate 	    !valid_boot_logger(handle, &bootlogger_is_https))
513*0Sstevel@tonic-gate 		return (B_FALSE);
514*0Sstevel@tonic-gate 
515*0Sstevel@tonic-gate 	/*
516*0Sstevel@tonic-gate 	 * Now do consistency checking between bootconf settings.
517*0Sstevel@tonic-gate 	 */
518*0Sstevel@tonic-gate 	if (is_encrypted && !is_signed) {
519*0Sstevel@tonic-gate 		handle->bc_error_code = BC_E_ENCRYPTED_NOT_SIGNED;
520*0Sstevel@tonic-gate 		return (B_FALSE);
521*0Sstevel@tonic-gate 	}
522*0Sstevel@tonic-gate 	if (client_is_authenticated) {
523*0Sstevel@tonic-gate 		if (!(is_encrypted && is_signed)) {
524*0Sstevel@tonic-gate 			handle->bc_error_code = BC_E_CLIENT_AUTH_NOT_ENCRYPTED;
525*0Sstevel@tonic-gate 			return (B_FALSE);
526*0Sstevel@tonic-gate 		}
527*0Sstevel@tonic-gate 
528*0Sstevel@tonic-gate 		if (!server_is_authenticated) {
529*0Sstevel@tonic-gate 			handle->bc_error_code = BC_E_CLIENT_AUTH_NOT_SERVER;
530*0Sstevel@tonic-gate 			return (B_FALSE);
531*0Sstevel@tonic-gate 		}
532*0Sstevel@tonic-gate 	}
533*0Sstevel@tonic-gate 	if (server_is_authenticated) {
534*0Sstevel@tonic-gate 		if (!is_signed) {
535*0Sstevel@tonic-gate 			handle->bc_error_code = BC_E_SERVER_AUTH_NOT_SIGNED;
536*0Sstevel@tonic-gate 			return (B_FALSE);
537*0Sstevel@tonic-gate 		}
538*0Sstevel@tonic-gate 
539*0Sstevel@tonic-gate 		if (!rootserver_is_https) {
540*0Sstevel@tonic-gate 			handle->bc_error_code = BC_E_SERVER_AUTH_NOT_HTTPS;
541*0Sstevel@tonic-gate 			return (B_FALSE);
542*0Sstevel@tonic-gate 		}
543*0Sstevel@tonic-gate 	} else if (rootserver_is_https) {
544*0Sstevel@tonic-gate 		handle->bc_error_code = BC_E_SERVER_AUTH_NOT_HTTP;
545*0Sstevel@tonic-gate 		return (B_FALSE);
546*0Sstevel@tonic-gate 	} else if (bootlogger_is_https) {
547*0Sstevel@tonic-gate 		handle->bc_error_code = BC_E_BOOTLOGGER_AUTH_NOT_HTTP;
548*0Sstevel@tonic-gate 		return (B_FALSE);
549*0Sstevel@tonic-gate 	}
550*0Sstevel@tonic-gate 
551*0Sstevel@tonic-gate 	return (B_TRUE);
552*0Sstevel@tonic-gate }
553*0Sstevel@tonic-gate 
554*0Sstevel@tonic-gate 
555*0Sstevel@tonic-gate /*
556*0Sstevel@tonic-gate  * bootconf_end() cleans up once we're done accessing the nvpair list
557*0Sstevel@tonic-gate  * representation of wanboot.conf(4).
558*0Sstevel@tonic-gate  */
559*0Sstevel@tonic-gate void
bootconf_end(bc_handle_t * handle)560*0Sstevel@tonic-gate bootconf_end(bc_handle_t *handle)
561*0Sstevel@tonic-gate {
562*0Sstevel@tonic-gate 	if (handle->bc_nvl != NULL) {
563*0Sstevel@tonic-gate 		nvlist_free(handle->bc_nvl);
564*0Sstevel@tonic-gate 		handle->bc_nvl = NULL;
565*0Sstevel@tonic-gate 	}
566*0Sstevel@tonic-gate }
567*0Sstevel@tonic-gate 
568*0Sstevel@tonic-gate /*
569*0Sstevel@tonic-gate  * bootconf_init() must be called to initialize 'handle' before bootconf_get()
570*0Sstevel@tonic-gate  * can be used to access values from the wanboot.conf(4) file.
571*0Sstevel@tonic-gate  */
572*0Sstevel@tonic-gate int
bootconf_init(bc_handle_t * handle,const char * bootconf)573*0Sstevel@tonic-gate bootconf_init(bc_handle_t *handle, const char *bootconf)
574*0Sstevel@tonic-gate {
575*0Sstevel@tonic-gate 	/*
576*0Sstevel@tonic-gate 	 * Initalise the handle's fields to sensible values.
577*0Sstevel@tonic-gate 	 */
578*0Sstevel@tonic-gate 	handle->bc_nvl = NULL;
579*0Sstevel@tonic-gate 	handle->bc_error_code = BC_E_NOERROR;
580*0Sstevel@tonic-gate 	handle->bc_error_pos = 0;
581*0Sstevel@tonic-gate 
582*0Sstevel@tonic-gate 	/*
583*0Sstevel@tonic-gate 	 * Provide a default path for the bootconf file if none was given.
584*0Sstevel@tonic-gate 	 */
585*0Sstevel@tonic-gate 	if (bootconf == NULL) {
586*0Sstevel@tonic-gate 		bootconf = NB_WANBOOT_CONF_PATH;
587*0Sstevel@tonic-gate 	}
588*0Sstevel@tonic-gate 
589*0Sstevel@tonic-gate 	/*
590*0Sstevel@tonic-gate 	 * Check that we can successfully parse and validate the file.
591*0Sstevel@tonic-gate 	 */
592*0Sstevel@tonic-gate 	if (parse_bootconf(handle, bootconf) && validate_bootconf(handle)) {
593*0Sstevel@tonic-gate 		return (BC_SUCCESS);
594*0Sstevel@tonic-gate 	}
595*0Sstevel@tonic-gate 
596*0Sstevel@tonic-gate 	/*
597*0Sstevel@tonic-gate 	 * Parse/validate error; free any allocated resources.
598*0Sstevel@tonic-gate 	 */
599*0Sstevel@tonic-gate 	bootconf_end(handle);
600*0Sstevel@tonic-gate 
601*0Sstevel@tonic-gate 	return (BC_FAILURE);
602*0Sstevel@tonic-gate }
603*0Sstevel@tonic-gate 
604*0Sstevel@tonic-gate /*
605*0Sstevel@tonic-gate  * bootconf_get() returns the value of a parameter in the wanboot.conf(4) file.
606*0Sstevel@tonic-gate  *
607*0Sstevel@tonic-gate  * Returns:
608*0Sstevel@tonic-gate  *	!= NULL	- the given value
609*0Sstevel@tonic-gate  *	== NULL	- value not found or is empty
610*0Sstevel@tonic-gate  */
611*0Sstevel@tonic-gate char *
bootconf_get(bc_handle_t * handle,const char * name)612*0Sstevel@tonic-gate bootconf_get(bc_handle_t *handle, const char *name)
613*0Sstevel@tonic-gate {
614*0Sstevel@tonic-gate 	char	*strval;
615*0Sstevel@tonic-gate 
616*0Sstevel@tonic-gate 	/*
617*0Sstevel@tonic-gate 	 * Look up the name in bc_nvl and return its value if found.
618*0Sstevel@tonic-gate 	 */
619*0Sstevel@tonic-gate 	if (handle->bc_nvl != NULL &&
620*0Sstevel@tonic-gate 	    nvlist_lookup_string(handle->bc_nvl, (char *)name, &strval) == 0) {
621*0Sstevel@tonic-gate 		return (strlen(strval) == 0 ? NULL : strval);
622*0Sstevel@tonic-gate 	}
623*0Sstevel@tonic-gate 
624*0Sstevel@tonic-gate 	return (NULL);
625*0Sstevel@tonic-gate }
626