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