xref: /titanic_51/usr/src/lib/libbrand/common/libbrand.c (revision 48bc00d6814e04ff3edb32cafe7d1bc580baff68)
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 /*
23  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #include <assert.h>
28 #include <dirent.h>
29 #include <errno.h>
30 #include <fnmatch.h>
31 #include <signal.h>
32 #include <stdlib.h>
33 #include <unistd.h>
34 #include <strings.h>
35 #include <synch.h>
36 #include <sys/brand.h>
37 #include <sys/fcntl.h>
38 #include <sys/param.h>
39 #include <sys/stat.h>
40 #include <sys/systeminfo.h>
41 #include <sys/types.h>
42 #include <thread.h>
43 #include <zone.h>
44 
45 #include <libbrand_impl.h>
46 #include <libbrand.h>
47 
48 #define	DTD_ELEM_ATTACH		((const xmlChar *) "attach")
49 #define	DTD_ELEM_BOOT		((const xmlChar *) "boot")
50 #define	DTD_ELEM_BRAND		((const xmlChar *) "brand")
51 #define	DTD_ELEM_CLONE		((const xmlChar *) "clone")
52 #define	DTD_ELEM_COMMENT	((const xmlChar *) "comment")
53 #define	DTD_ELEM_DETACH		((const xmlChar *) "detach")
54 #define	DTD_ELEM_DEVICE		((const xmlChar *) "device")
55 #define	DTD_ELEM_GLOBAL_MOUNT	((const xmlChar *) "global_mount")
56 #define	DTD_ELEM_HALT		((const xmlChar *) "halt")
57 #define	DTD_ELEM_INITNAME	((const xmlChar *) "initname")
58 #define	DTD_ELEM_INSTALL	((const xmlChar *) "install")
59 #define	DTD_ELEM_INSTALLOPTS	((const xmlChar *) "installopts")
60 #define	DTD_ELEM_LOGIN_CMD	((const xmlChar *) "login_cmd")
61 #define	DTD_ELEM_MODNAME	((const xmlChar *) "modname")
62 #define	DTD_ELEM_MOUNT		((const xmlChar *) "mount")
63 #define	DTD_ELEM_POSTATTACH	((const xmlChar *) "postattach")
64 #define	DTD_ELEM_POSTCLONE	((const xmlChar *) "postclone")
65 #define	DTD_ELEM_POSTINSTALL	((const xmlChar *) "postinstall")
66 #define	DTD_ELEM_POSTSNAP	((const xmlChar *) "postsnap")
67 #define	DTD_ELEM_POSTSTATECHG	((const xmlChar *) "poststatechange")
68 #define	DTD_ELEM_PREDETACH	((const xmlChar *) "predetach")
69 #define	DTD_ELEM_PRESNAP	((const xmlChar *) "presnap")
70 #define	DTD_ELEM_PRESTATECHG	((const xmlChar *) "prestatechange")
71 #define	DTD_ELEM_PREUNINSTALL	((const xmlChar *) "preuninstall")
72 #define	DTD_ELEM_PRIVILEGE	((const xmlChar *) "privilege")
73 #define	DTD_ELEM_QUERY		((const xmlChar *) "query")
74 #define	DTD_ELEM_SYMLINK	((const xmlChar *) "symlink")
75 #define	DTD_ELEM_SYSBOOT	((const xmlChar *) "sysboot")
76 #define	DTD_ELEM_UNINSTALL	((const xmlChar *) "uninstall")
77 #define	DTD_ELEM_USER_CMD	((const xmlChar *) "user_cmd")
78 #define	DTD_ELEM_VALIDSNAP	((const xmlChar *) "validatesnap")
79 #define	DTD_ELEM_VERIFY_CFG	((const xmlChar *) "verify_cfg")
80 #define	DTD_ELEM_VERIFY_ADM	((const xmlChar *) "verify_adm")
81 
82 #define	DTD_ATTR_ALLOWEXCL	((const xmlChar *) "allow-exclusive-ip")
83 #define	DTD_ATTR_ARCH		((const xmlChar *) "arch")
84 #define	DTD_ATTR_DIRECTORY	((const xmlChar *) "directory")
85 #define	DTD_ATTR_IPTYPE		((const xmlChar *) "ip-type")
86 #define	DTD_ATTR_MATCH		((const xmlChar *) "match")
87 #define	DTD_ATTR_MODE		((const xmlChar *) "mode")
88 #define	DTD_ATTR_NAME		((const xmlChar *) "name")
89 #define	DTD_ATTR_OPT		((const xmlChar *) "opt")
90 #define	DTD_ATTR_PATH		((const xmlChar *) "path")
91 #define	DTD_ATTR_SET		((const xmlChar *) "set")
92 #define	DTD_ATTR_SOURCE		((const xmlChar *) "source")
93 #define	DTD_ATTR_SPECIAL	((const xmlChar *) "special")
94 #define	DTD_ATTR_TARGET		((const xmlChar *) "target")
95 #define	DTD_ATTR_TYPE		((const xmlChar *) "type")
96 
97 #define	DTD_ENTITY_TRUE		"true"
98 
99 static volatile boolean_t	libbrand_initialized = B_FALSE;
100 static char			i_curr_arch[MAXNAMELEN];
101 static char			i_curr_zone[ZONENAME_MAX];
102 
103 /*ARGSUSED*/
104 static void
105 brand_error_func(void *ctx, const char *msg, ...)
106 {
107 	/*
108 	 * Ignore error messages from libxml
109 	 */
110 }
111 
112 static boolean_t
113 libbrand_initialize()
114 {
115 	static mutex_t initialize_lock = DEFAULTMUTEX;
116 
117 	(void) mutex_lock(&initialize_lock);
118 
119 	if (libbrand_initialized) {
120 		(void) mutex_unlock(&initialize_lock);
121 		return (B_TRUE);
122 	}
123 
124 	if (sysinfo(SI_ARCHITECTURE, i_curr_arch, sizeof (i_curr_arch)) < 0) {
125 		(void) mutex_unlock(&initialize_lock);
126 		return (B_FALSE);
127 	}
128 
129 	if (getzonenamebyid(getzoneid(), i_curr_zone,
130 	    sizeof (i_curr_zone)) < 0) {
131 		(void) mutex_unlock(&initialize_lock);
132 		return (B_FALSE);
133 	}
134 
135 	/*
136 	 * Note that here we're initializing per-process libxml2
137 	 * state.  By doing so we're implicitly assuming that
138 	 * no other code in this process is also trying to
139 	 * use libxml2.  But in most case we know this not to
140 	 * be true since we're almost always used in conjunction
141 	 * with libzonecfg, which also uses libxml2.  Lucky for
142 	 * us, libzonecfg initializes libxml2 to essentially
143 	 * the same defaults as we're using below.
144 	 */
145 	xmlLineNumbersDefault(1);
146 	xmlLoadExtDtdDefaultValue |= XML_DETECT_IDS;
147 	xmlDoValidityCheckingDefaultValue = 1;
148 	(void) xmlKeepBlanksDefault(0);
149 	xmlGetWarningsDefaultValue = 0;
150 	xmlSetGenericErrorFunc(NULL, brand_error_func);
151 
152 	libbrand_initialized = B_TRUE;
153 	(void) mutex_unlock(&initialize_lock);
154 	return (B_TRUE);
155 }
156 
157 static const char *
158 get_curr_arch(void)
159 {
160 	if (!libbrand_initialize())
161 		return (NULL);
162 
163 	return (i_curr_arch);
164 }
165 
166 static const char *
167 get_curr_zone(void)
168 {
169 	if (!libbrand_initialize())
170 		return (NULL);
171 
172 	return (i_curr_zone);
173 }
174 
175 /*
176  * Internal function to open an XML file
177  *
178  * Returns the XML doc pointer, or NULL on failure.  It will validate the
179  * document, as well as removing any comments from the document structure.
180  */
181 static xmlDocPtr
182 open_xml_file(const char *file)
183 {
184 	xmlDocPtr doc;
185 	xmlValidCtxtPtr cvp;
186 	int valid;
187 
188 	if (!libbrand_initialize())
189 		return (NULL);
190 
191 	/*
192 	 * Parse the file
193 	 */
194 	if ((doc = xmlParseFile(file)) == NULL)
195 		return (NULL);
196 
197 	/*
198 	 * Validate the file
199 	 */
200 	if ((cvp = xmlNewValidCtxt()) == NULL) {
201 		xmlFreeDoc(doc);
202 		return (NULL);
203 	}
204 	cvp->error = brand_error_func;
205 	cvp->warning = brand_error_func;
206 	valid = xmlValidateDocument(cvp, doc);
207 	xmlFreeValidCtxt(cvp);
208 	if (valid == 0) {
209 		xmlFreeDoc(doc);
210 		return (NULL);
211 	}
212 
213 	return (doc);
214 }
215 /*
216  * Open a handle to the named brand.
217  *
218  * Returns a handle to the named brand, which is used for all subsequent brand
219  * interaction, or NULL if unable to open or initialize the brand.
220  */
221 brand_handle_t
222 brand_open(const char *name)
223 {
224 	struct brand_handle *bhp;
225 	char path[MAXPATHLEN];
226 	xmlNodePtr node;
227 	xmlChar *property;
228 	struct stat statbuf;
229 
230 	/*
231 	 * Make sure brand name isn't too long
232 	 */
233 	if (strlen(name) >= MAXNAMELEN)
234 		return (NULL);
235 
236 	/*
237 	 * Check that the brand exists
238 	 */
239 	(void) snprintf(path, sizeof (path), "%s/%s", BRAND_DIR, name);
240 
241 	if (stat(path, &statbuf) != 0)
242 		return (NULL);
243 
244 	/*
245 	 * Allocate brand handle
246 	 */
247 	if ((bhp = malloc(sizeof (struct brand_handle))) == NULL)
248 		return (NULL);
249 	bzero(bhp, sizeof (struct brand_handle));
250 
251 	(void) strcpy(bhp->bh_name, name);
252 
253 	/*
254 	 * Open the configuration file
255 	 */
256 	(void) snprintf(path, sizeof (path), "%s/%s/%s", BRAND_DIR, name,
257 	    BRAND_CONFIG);
258 	if ((bhp->bh_config = open_xml_file(path)) == NULL) {
259 		brand_close((brand_handle_t)bhp);
260 		return (NULL);
261 	}
262 
263 	/*
264 	 * Verify that the name of the brand matches the directory in which it
265 	 * is installed.
266 	 */
267 	if ((node = xmlDocGetRootElement(bhp->bh_config)) == NULL) {
268 		brand_close((brand_handle_t)bhp);
269 		return (NULL);
270 	}
271 
272 	if (xmlStrcmp(node->name, DTD_ELEM_BRAND) != 0) {
273 		brand_close((brand_handle_t)bhp);
274 		return (NULL);
275 	}
276 
277 	if ((property = xmlGetProp(node, DTD_ATTR_NAME)) == NULL) {
278 		brand_close((brand_handle_t)bhp);
279 		return (NULL);
280 	}
281 
282 	if (strcmp((char *)property, name) != 0) {
283 		xmlFree(property);
284 		brand_close((brand_handle_t)bhp);
285 		return (NULL);
286 	}
287 	xmlFree(property);
288 
289 	/*
290 	 * Open handle to platform configuration file.
291 	 */
292 	(void) snprintf(path, sizeof (path), "%s/%s/%s", BRAND_DIR, name,
293 	    BRAND_PLATFORM);
294 	if ((bhp->bh_platform = open_xml_file(path)) == NULL) {
295 		brand_close((brand_handle_t)bhp);
296 		return (NULL);
297 	}
298 
299 	return ((brand_handle_t)bhp);
300 }
301 
302 /*
303  * Closes the given brand handle
304  */
305 void
306 brand_close(brand_handle_t bh)
307 {
308 	struct brand_handle *bhp = (struct brand_handle *)bh;
309 	if (bhp->bh_platform != NULL)
310 		xmlFreeDoc(bhp->bh_platform);
311 	if (bhp->bh_config != NULL)
312 		xmlFreeDoc(bhp->bh_config);
313 	free(bhp);
314 }
315 
316 static int
317 i_substitute_tokens(const char *sbuf, char *dbuf, int dbuf_size,
318     const char *zonename, const char *zonepath, const char *username,
319     const char *curr_zone)
320 {
321 	int dst, src;
322 
323 	/*
324 	 * Walk through the characters, substituting values as needed.
325 	 */
326 	dbuf[0] = '\0';
327 	dst = 0;
328 	for (src = 0; src < strlen((char *)sbuf) && dst < dbuf_size; src++) {
329 		if (sbuf[src] != '%') {
330 			dbuf[dst++] = sbuf[src];
331 			continue;
332 		}
333 
334 		switch (sbuf[++src]) {
335 		case '%':
336 			dst += strlcpy(dbuf + dst, "%", dbuf_size - dst);
337 			break;
338 		case 'R':
339 			if (zonepath == NULL)
340 				break;
341 			dst += strlcpy(dbuf + dst, zonepath, dbuf_size - dst);
342 			break;
343 		case 'u':
344 			if (username == NULL)
345 				break;
346 			dst += strlcpy(dbuf + dst, username, dbuf_size - dst);
347 			break;
348 		case 'Z':
349 			if (curr_zone == NULL)
350 				break;
351 			/* name of the zone we're running in */
352 			dst += strlcpy(dbuf + dst, curr_zone, dbuf_size - dst);
353 			break;
354 		case 'z':
355 			/* name of the zone we're operating on */
356 			if (zonename == NULL)
357 				break;
358 			dst += strlcpy(dbuf + dst, zonename, dbuf_size - dst);
359 			break;
360 		}
361 	}
362 
363 	if (dst >= dbuf_size)
364 		return (-1);
365 
366 	dbuf[dst] = '\0';
367 	return (0);
368 }
369 
370 /*
371  * Retrieve the given tag from the brand.
372  * Perform the following substitutions as necessary:
373  *
374  *	%%	%
375  *	%u	Username
376  *	%z	Name of target zone
377  *	%Z	Name of current zone
378  *	%R	Zonepath of zone
379  *
380  * Returns 0 on success, -1 on failure.
381  */
382 static int
383 brand_get_value(struct brand_handle *bhp, const char *zonename,
384     const char *zonepath, const char *username, const char *curr_zone,
385     char *buf, size_t len, const xmlChar *tagname,
386     boolean_t substitute, boolean_t optional)
387 {
388 	xmlNodePtr node;
389 	xmlChar *content;
390 	int err = 0;
391 
392 	/*
393 	 * Retrieve the specified value from the XML doc
394 	 */
395 	if ((node = xmlDocGetRootElement(bhp->bh_config)) == NULL)
396 		return (-1);
397 
398 	if (xmlStrcmp(node->name, DTD_ELEM_BRAND) != 0)
399 		return (-1);
400 
401 	for (node = node->xmlChildrenNode; node != NULL;
402 	    node = node->next) {
403 		if (xmlStrcmp(node->name, tagname) == 0)
404 			break;
405 	}
406 
407 	if (node == NULL) {
408 		if (optional) {
409 			buf[0] = '\0';
410 			return (0);
411 		} else {
412 			return (-1);
413 		}
414 	}
415 
416 	if ((content = xmlNodeGetContent(node)) == NULL)
417 		return (-1);
418 
419 	if (strlen((char *)content) == 0) {
420 		/*
421 		 * If the entry in the config file is empty, check to see
422 		 * whether this is an optional field.  If so, we return the
423 		 * empty buffer.  If not, we return an error.
424 		 */
425 		if (optional) {
426 			buf[0] = '\0';
427 		} else {
428 			err = -1;
429 		}
430 	} else {
431 		/* Substitute token values as needed. */
432 		if (substitute) {
433 			if (i_substitute_tokens((char *)content, buf, len,
434 			    zonename, zonepath, username, curr_zone) != 0)
435 				err = -1;
436 		} else {
437 			if (strlcpy(buf, (char *)content, len) >= len)
438 				err = -1;
439 		}
440 	}
441 
442 	xmlFree(content);
443 
444 	return (err);
445 }
446 
447 int
448 brand_get_attach(brand_handle_t bh, const char *zonename,
449     const char *zonepath, char *buf, size_t len)
450 {
451 	struct brand_handle *bhp = (struct brand_handle *)bh;
452 	return (brand_get_value(bhp, zonename, zonepath, NULL, NULL,
453 	    buf, len, DTD_ELEM_ATTACH, B_TRUE, B_TRUE));
454 }
455 
456 int
457 brand_get_boot(brand_handle_t bh, const char *zonename,
458     const char *zonepath, char *buf, size_t len)
459 {
460 	struct brand_handle *bhp = (struct brand_handle *)bh;
461 	return (brand_get_value(bhp, zonename, zonepath, NULL, NULL,
462 	    buf, len, DTD_ELEM_BOOT, B_TRUE, B_TRUE));
463 }
464 
465 int
466 brand_get_brandname(brand_handle_t bh, char *buf, size_t len)
467 {
468 	struct brand_handle *bhp = (struct brand_handle *)bh;
469 	if (len <= strlen(bhp->bh_name))
470 		return (-1);
471 
472 	(void) strcpy(buf, bhp->bh_name);
473 
474 	return (0);
475 }
476 
477 int
478 brand_get_clone(brand_handle_t bh, const char *zonename,
479     const char *zonepath, char *buf, size_t len)
480 {
481 	struct brand_handle *bhp = (struct brand_handle *)bh;
482 	return (brand_get_value(bhp, zonename, zonepath, NULL, NULL,
483 	    buf, len, DTD_ELEM_CLONE, B_TRUE, B_TRUE));
484 }
485 
486 int
487 brand_get_detach(brand_handle_t bh, const char *zonename,
488     const char *zonepath, char *buf, size_t len)
489 {
490 	struct brand_handle *bhp = (struct brand_handle *)bh;
491 	return (brand_get_value(bhp, zonename, zonepath, NULL, NULL,
492 	    buf, len, DTD_ELEM_DETACH, B_TRUE, B_TRUE));
493 }
494 
495 int
496 brand_get_halt(brand_handle_t bh, const char *zonename,
497     const char *zonepath, char *buf, size_t len)
498 {
499 	struct brand_handle *bhp = (struct brand_handle *)bh;
500 	return (brand_get_value(bhp, zonename, zonepath, NULL, NULL,
501 	    buf, len, DTD_ELEM_HALT, B_TRUE, B_TRUE));
502 }
503 
504 int
505 brand_get_initname(brand_handle_t bh, char *buf, size_t len)
506 {
507 	struct brand_handle *bhp = (struct brand_handle *)bh;
508 	return (brand_get_value(bhp, NULL, NULL, NULL, NULL,
509 	    buf, len, DTD_ELEM_INITNAME, B_FALSE, B_FALSE));
510 }
511 
512 int
513 brand_get_login_cmd(brand_handle_t bh, const char *username,
514     char *buf, size_t len)
515 {
516 	struct brand_handle *bhp = (struct brand_handle *)bh;
517 	const char *curr_zone = get_curr_zone();
518 	return (brand_get_value(bhp, NULL, NULL, username, curr_zone,
519 	    buf, len, DTD_ELEM_LOGIN_CMD, B_TRUE, B_FALSE));
520 }
521 
522 int
523 brand_get_user_cmd(brand_handle_t bh, const char *username,
524     char *buf, size_t len)
525 {
526 	struct brand_handle *bhp = (struct brand_handle *)bh;
527 
528 	return (brand_get_value(bhp, NULL, NULL, username, NULL,
529 	    buf, len, DTD_ELEM_USER_CMD, B_TRUE, B_FALSE));
530 }
531 
532 int
533 brand_get_install(brand_handle_t bh, const char *zonename,
534     const char *zonepath, char *buf, size_t len)
535 {
536 	struct brand_handle *bhp = (struct brand_handle *)bh;
537 	return (brand_get_value(bhp, zonename, zonepath, NULL, NULL,
538 	    buf, len, DTD_ELEM_INSTALL, B_TRUE, B_FALSE));
539 }
540 
541 int
542 brand_get_installopts(brand_handle_t bh, char *buf, size_t len)
543 {
544 	struct brand_handle *bhp = (struct brand_handle *)bh;
545 	return (brand_get_value(bhp, NULL, NULL, NULL, NULL,
546 	    buf, len, DTD_ELEM_INSTALLOPTS, B_FALSE, B_TRUE));
547 }
548 
549 int
550 brand_get_modname(brand_handle_t bh, char *buf, size_t len)
551 {
552 	struct brand_handle *bhp = (struct brand_handle *)bh;
553 	return (brand_get_value(bhp, NULL, NULL, NULL, NULL,
554 	    buf, len, DTD_ELEM_MODNAME, B_FALSE, B_TRUE));
555 }
556 
557 int
558 brand_get_postattach(brand_handle_t bh, const char *zonename,
559     const char *zonepath, char *buf, size_t len)
560 {
561 	struct brand_handle *bhp = (struct brand_handle *)bh;
562 	return (brand_get_value(bhp, zonename, zonepath, NULL, NULL,
563 	    buf, len, DTD_ELEM_POSTATTACH, B_TRUE, B_TRUE));
564 }
565 
566 int
567 brand_get_postclone(brand_handle_t bh, const char *zonename,
568     const char *zonepath, char *buf, size_t len)
569 {
570 	struct brand_handle *bhp = (struct brand_handle *)bh;
571 	return (brand_get_value(bhp, zonename, zonepath, NULL, NULL,
572 	    buf, len, DTD_ELEM_POSTCLONE, B_TRUE, B_TRUE));
573 }
574 
575 int
576 brand_get_postinstall(brand_handle_t bh, const char *zonename,
577     const char *zonepath, char *buf, size_t len)
578 {
579 	struct brand_handle *bhp = (struct brand_handle *)bh;
580 	return (brand_get_value(bhp, zonename, zonepath, NULL, NULL,
581 	    buf, len, DTD_ELEM_POSTINSTALL, B_TRUE, B_TRUE));
582 }
583 
584 int
585 brand_get_postsnap(brand_handle_t bh, const char *zonename,
586     const char *zonepath, char *buf, size_t len)
587 {
588 	struct brand_handle *bhp = (struct brand_handle *)bh;
589 	return (brand_get_value(bhp, zonename, zonepath, NULL, NULL,
590 	    buf, len, DTD_ELEM_POSTSNAP, B_TRUE, B_TRUE));
591 }
592 
593 int
594 brand_get_poststatechange(brand_handle_t bh, const char *zonename,
595     const char *zonepath, char *buf, size_t len)
596 {
597 	struct brand_handle *bhp = (struct brand_handle *)bh;
598 	return (brand_get_value(bhp, zonename, zonepath, NULL, NULL,
599 	    buf, len, DTD_ELEM_POSTSTATECHG, B_TRUE, B_TRUE));
600 }
601 
602 int
603 brand_get_predetach(brand_handle_t bh, const char *zonename,
604     const char *zonepath, char *buf, size_t len)
605 {
606 	struct brand_handle *bhp = (struct brand_handle *)bh;
607 	return (brand_get_value(bhp, zonename, zonepath, NULL, NULL,
608 	    buf, len, DTD_ELEM_PREDETACH, B_TRUE, B_TRUE));
609 }
610 
611 int
612 brand_get_presnap(brand_handle_t bh, const char *zonename,
613     const char *zonepath, char *buf, size_t len)
614 {
615 	struct brand_handle *bhp = (struct brand_handle *)bh;
616 	return (brand_get_value(bhp, zonename, zonepath, NULL, NULL,
617 	    buf, len, DTD_ELEM_PRESNAP, B_TRUE, B_TRUE));
618 }
619 
620 int
621 brand_get_prestatechange(brand_handle_t bh, const char *zonename,
622     const char *zonepath, char *buf, size_t len)
623 {
624 	struct brand_handle *bhp = (struct brand_handle *)bh;
625 	return (brand_get_value(bhp, zonename, zonepath, NULL, NULL,
626 	    buf, len, DTD_ELEM_PRESTATECHG, B_TRUE, B_TRUE));
627 }
628 
629 int
630 brand_get_preuninstall(brand_handle_t bh, const char *zonename,
631     const char *zonepath, char *buf, size_t len)
632 {
633 	struct brand_handle *bhp = (struct brand_handle *)bh;
634 	return (brand_get_value(bhp, zonename, zonepath, NULL, NULL,
635 	    buf, len, DTD_ELEM_PREUNINSTALL, B_TRUE, B_TRUE));
636 }
637 
638 int
639 brand_get_query(brand_handle_t bh, const char *zonename,
640     const char *zonepath, char *buf, size_t len)
641 {
642 	struct brand_handle *bhp = (struct brand_handle *)bh;
643 	return (brand_get_value(bhp, zonename, zonepath, NULL, NULL,
644 	    buf, len, DTD_ELEM_QUERY, B_TRUE, B_TRUE));
645 }
646 
647 int
648 brand_get_uninstall(brand_handle_t bh, const char *zonename,
649     const char *zonepath, char *buf, size_t len)
650 {
651 	struct brand_handle *bhp = (struct brand_handle *)bh;
652 	return (brand_get_value(bhp, zonename, zonepath, NULL, NULL,
653 	    buf, len, DTD_ELEM_UNINSTALL, B_TRUE, B_TRUE));
654 }
655 
656 int
657 brand_get_validatesnap(brand_handle_t bh, const char *zonename,
658     const char *zonepath, char *buf, size_t len)
659 {
660 	struct brand_handle *bhp = (struct brand_handle *)bh;
661 	return (brand_get_value(bhp, zonename, zonepath, NULL, NULL,
662 	    buf, len, DTD_ELEM_VALIDSNAP, B_TRUE, B_TRUE));
663 }
664 
665 int
666 brand_get_verify_cfg(brand_handle_t bh, char *buf, size_t len)
667 {
668 	struct brand_handle *bhp = (struct brand_handle *)bh;
669 	return (brand_get_value(bhp, NULL, NULL, NULL, NULL,
670 	    buf, len, DTD_ELEM_VERIFY_CFG, B_FALSE, B_TRUE));
671 }
672 
673 int
674 brand_get_verify_adm(brand_handle_t bh, const char *zonename,
675     const char *zonepath, char *buf, size_t len)
676 {
677 	struct brand_handle *bhp = (struct brand_handle *)bh;
678 	return (brand_get_value(bhp, zonename, zonepath, NULL, NULL,
679 	    buf, len, DTD_ELEM_VERIFY_ADM, B_TRUE, B_TRUE));
680 }
681 
682 int
683 brand_get_sysboot(brand_handle_t bh, const char *zonename,
684     const char *zonepath, char *buf, size_t len)
685 {
686 	struct brand_handle *bhp = (struct brand_handle *)bh;
687 	return (brand_get_value(bhp, zonename, zonepath, NULL, NULL,
688 	    buf, len, DTD_ELEM_SYSBOOT, B_TRUE, B_TRUE));
689 }
690 
691 int
692 brand_is_native(brand_handle_t bh)
693 {
694 	struct brand_handle *bhp = (struct brand_handle *)bh;
695 	return ((strcmp(bhp->bh_name, NATIVE_BRAND_NAME) == 0) ? 1 : 0);
696 }
697 
698 boolean_t
699 brand_allow_exclusive_ip(brand_handle_t bh)
700 {
701 	struct brand_handle	*bhp = (struct brand_handle *)bh;
702 	xmlNodePtr		node;
703 	xmlChar			*allow_excl;
704 	boolean_t		ret;
705 
706 	assert(bhp != NULL);
707 
708 	if ((node = xmlDocGetRootElement(bhp->bh_platform)) == NULL)
709 		return (B_FALSE);
710 
711 	allow_excl = xmlGetProp(node, DTD_ATTR_ALLOWEXCL);
712 	if (allow_excl == NULL)
713 		return (B_FALSE);
714 
715 	/* Note: only return B_TRUE if it's "true" */
716 	if (strcmp((char *)allow_excl, DTD_ENTITY_TRUE) == 0)
717 		ret = B_TRUE;
718 	else
719 		ret = B_FALSE;
720 
721 	xmlFree(allow_excl);
722 
723 	return (ret);
724 }
725 
726 /*
727  * Iterate over brand privileges
728  *
729  * Walks the brand config, searching for <privilege> elements, calling the
730  * specified callback for each.  Returns 0 on success, or -1 on failure.
731  */
732 int
733 brand_config_iter_privilege(brand_handle_t bh,
734     int (*func)(void *, priv_iter_t *), void *data)
735 {
736 	struct brand_handle	*bhp = (struct brand_handle *)bh;
737 	xmlNodePtr		node;
738 	xmlChar			*name, *set, *iptype;
739 	priv_iter_t		priv_iter;
740 	int			ret;
741 
742 	if ((node = xmlDocGetRootElement(bhp->bh_config)) == NULL)
743 		return (-1);
744 
745 	for (node = node->xmlChildrenNode; node != NULL; node = node->next) {
746 
747 		if (xmlStrcmp(node->name, DTD_ELEM_PRIVILEGE) != 0)
748 			continue;
749 
750 		name = xmlGetProp(node, DTD_ATTR_NAME);
751 		set = xmlGetProp(node, DTD_ATTR_SET);
752 		iptype = xmlGetProp(node, DTD_ATTR_IPTYPE);
753 
754 		if (name == NULL || set == NULL || iptype == NULL) {
755 			if (name != NULL)
756 				xmlFree(name);
757 			if (set != NULL)
758 				xmlFree(set);
759 			if (iptype != NULL)
760 				xmlFree(iptype);
761 			return (-1);
762 		}
763 
764 		priv_iter.pi_name = (char *)name;
765 		priv_iter.pi_set = (char *)set;
766 		priv_iter.pi_iptype = (char *)iptype;
767 
768 		ret = func(data, &priv_iter);
769 
770 		xmlFree(name);
771 		xmlFree(set);
772 		xmlFree(iptype);
773 
774 		if (ret != 0)
775 			return (-1);
776 	}
777 
778 	return (0);
779 }
780 
781 static int
782 i_brand_platform_iter_mounts(struct brand_handle *bhp, const char *zonepath,
783     int (*func)(void *, const char *, const char *, const char *,
784     const char *), void *data, const xmlChar *mount_type)
785 {
786 	xmlNodePtr node;
787 	xmlChar *special, *dir, *type, *opt;
788 	char special_exp[MAXPATHLEN];
789 	char opt_exp[MAXPATHLEN];
790 	int ret;
791 
792 	if ((node = xmlDocGetRootElement(bhp->bh_platform)) == NULL)
793 		return (-1);
794 
795 	for (node = node->xmlChildrenNode; node != NULL; node = node->next) {
796 
797 		if (xmlStrcmp(node->name, mount_type) != 0)
798 			continue;
799 
800 		special = xmlGetProp(node, DTD_ATTR_SPECIAL);
801 		dir = xmlGetProp(node, DTD_ATTR_DIRECTORY);
802 		type = xmlGetProp(node, DTD_ATTR_TYPE);
803 		opt = xmlGetProp(node, DTD_ATTR_OPT);
804 		if ((special == NULL) || (dir == NULL) || (type == NULL) ||
805 		    (opt == NULL)) {
806 			ret = -1;
807 			goto next;
808 		}
809 
810 		/* Substitute token values as needed. */
811 		if ((ret = i_substitute_tokens((char *)special,
812 		    special_exp, sizeof (special_exp),
813 		    NULL, zonepath, NULL, NULL)) != 0)
814 			goto next;
815 
816 		/* opt might not be defined */
817 		if (strlen((const char *)opt) == 0) {
818 			xmlFree(opt);
819 			opt = NULL;
820 		} else {
821 			if ((ret = i_substitute_tokens((char *)opt,
822 			    opt_exp, sizeof (opt_exp),
823 			    NULL, zonepath, NULL, NULL)) != 0)
824 				goto next;
825 		}
826 
827 		ret = func(data, (char *)special_exp, (char *)dir,
828 		    (char *)type, ((opt != NULL) ? opt_exp : NULL));
829 
830 next:
831 		if (special != NULL)
832 			xmlFree(special);
833 		if (dir != NULL)
834 			xmlFree(dir);
835 		if (type != NULL)
836 			xmlFree(type);
837 		if (opt != NULL)
838 			xmlFree(opt);
839 		if (ret != 0)
840 			return (-1);
841 	}
842 	return (0);
843 }
844 
845 
846 /*
847  * Iterate over global platform filesystems
848  *
849  * Walks the platform, searching for <global_mount> elements, calling the
850  * specified callback for each.  Returns 0 on success, or -1 on failure.
851  *
852  * Perform the following substitutions as necessary:
853  *
854  *	%R	Zonepath of zone
855  */
856 int
857 brand_platform_iter_gmounts(brand_handle_t bh, const char *zonepath,
858     int (*func)(void *, const char *, const char *, const char *,
859     const char *), void *data)
860 {
861 	struct brand_handle *bhp = (struct brand_handle *)bh;
862 	return (i_brand_platform_iter_mounts(bhp, zonepath, func, data,
863 	    DTD_ELEM_GLOBAL_MOUNT));
864 }
865 
866 /*
867  * Iterate over non-global zone platform filesystems
868  *
869  * Walks the platform, searching for <mount> elements, calling the
870  * specified callback for each.  Returns 0 on success, or -1 on failure.
871  */
872 int
873 brand_platform_iter_mounts(brand_handle_t bh, int (*func)(void *,
874     const char *, const char *, const char *, const char *), void *data)
875 {
876 	struct brand_handle *bhp = (struct brand_handle *)bh;
877 	return (i_brand_platform_iter_mounts(bhp, NULL, func, data,
878 	    DTD_ELEM_MOUNT));
879 }
880 
881 /*
882  * Iterate over platform symlinks
883  *
884  * Walks the platform, searching for <symlink> elements, calling the
885  * specified callback for each.  Returns 0 on success, or -1 on failure.
886  */
887 int
888 brand_platform_iter_link(brand_handle_t bh,
889     int (*func)(void *, const char *, const char *), void *data)
890 {
891 	struct brand_handle *bhp = (struct brand_handle *)bh;
892 	xmlNodePtr node;
893 	xmlChar *source, *target;
894 	int ret;
895 
896 	if ((node = xmlDocGetRootElement(bhp->bh_platform)) == NULL)
897 		return (-1);
898 
899 	for (node = node->xmlChildrenNode; node != NULL; node = node->next) {
900 
901 		if (xmlStrcmp(node->name, DTD_ELEM_SYMLINK) != 0)
902 			continue;
903 
904 		source = xmlGetProp(node, DTD_ATTR_SOURCE);
905 		target = xmlGetProp(node, DTD_ATTR_TARGET);
906 
907 		if (source == NULL || target == NULL) {
908 			if (source != NULL)
909 				xmlFree(source);
910 			if (target != NULL)
911 				xmlFree(target);
912 			return (-1);
913 		}
914 
915 		ret = func(data, (char *)source, (char *)target);
916 
917 		xmlFree(source);
918 		xmlFree(target);
919 
920 		if (ret != 0)
921 			return (-1);
922 	}
923 
924 	return (0);
925 }
926 
927 /*
928  * Iterate over platform devices
929  *
930  * Walks the platform, searching for <device> elements, calling the
931  * specified callback for each.  Returns 0 on success, or -1 on failure.
932  */
933 int
934 brand_platform_iter_devices(brand_handle_t bh, const char *zonename,
935     int (*func)(void *, const char *, const char *), void *data,
936     const char *curr_iptype)
937 {
938 	struct brand_handle	*bhp = (struct brand_handle *)bh;
939 	const char		*curr_arch = get_curr_arch();
940 	xmlNodePtr		node;
941 	xmlChar			*match, *name, *arch, *iptype;
942 	char			match_exp[MAXPATHLEN];
943 	boolean_t		err = B_FALSE;
944 	int			ret = 0;
945 
946 
947 	assert(bhp != NULL);
948 	assert(zonename != NULL);
949 	assert(func != NULL);
950 	assert(curr_iptype != NULL);
951 
952 	if ((node = xmlDocGetRootElement(bhp->bh_platform)) == NULL)
953 		return (-1);
954 
955 	for (node = node->xmlChildrenNode; node != NULL; node = node->next) {
956 
957 		if (xmlStrcmp(node->name, DTD_ELEM_DEVICE) != 0)
958 			continue;
959 
960 		match = xmlGetProp(node, DTD_ATTR_MATCH);
961 		name = xmlGetProp(node, DTD_ATTR_NAME);
962 		arch = xmlGetProp(node, DTD_ATTR_ARCH);
963 		iptype = xmlGetProp(node, DTD_ATTR_IPTYPE);
964 		if ((match == NULL) || (name == NULL) || (arch == NULL) ||
965 		    (iptype == NULL)) {
966 			err = B_TRUE;
967 			goto next;
968 		}
969 
970 		/* check if the arch matches */
971 		if ((strcmp((char *)arch, "all") != 0) &&
972 		    (strcmp((char *)arch, curr_arch) != 0))
973 			goto next;
974 
975 		/* check if the iptype matches */
976 		if ((strcmp((char *)iptype, "all") != 0) &&
977 		    (strcmp((char *)iptype, curr_iptype) != 0))
978 			goto next;
979 
980 		/* Substitute token values as needed. */
981 		if ((ret = i_substitute_tokens((char *)match,
982 		    match_exp, sizeof (match_exp),
983 		    zonename, NULL, NULL, NULL)) != 0) {
984 			err = B_TRUE;
985 			goto next;
986 		}
987 
988 		/* name might not be defined */
989 		if (strlen((const char *)name) == 0) {
990 			xmlFree(name);
991 			name = NULL;
992 		}
993 
994 		/* invoke the callback */
995 		ret = func(data, (const char *)match_exp, (const char *)name);
996 
997 next:
998 		if (match != NULL)
999 			xmlFree(match);
1000 		if (name != NULL)
1001 			xmlFree(name);
1002 		if (arch != NULL)
1003 			xmlFree(arch);
1004 		if (iptype != NULL)
1005 			xmlFree(iptype);
1006 		if (err)
1007 			return (-1);
1008 		if (ret != 0)
1009 			return (-1);
1010 	}
1011 
1012 	return (0);
1013 }
1014