xref: /titanic_44/usr/src/lib/libzonecfg/common/libzonecfg.c (revision 5c51f1241dbbdf2656d0e10011981411ed0c9673)
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 <libsysevent.h>
28 #include <pthread.h>
29 #include <stdlib.h>
30 #include <errno.h>
31 #include <fnmatch.h>
32 #include <strings.h>
33 #include <unistd.h>
34 #include <assert.h>
35 #include <libgen.h>
36 #include <libintl.h>
37 #include <alloca.h>
38 #include <ctype.h>
39 #include <sys/acl.h>
40 #include <sys/stat.h>
41 #include <sys/brand.h>
42 #include <sys/mntio.h>
43 #include <sys/mnttab.h>
44 #include <sys/nvpair.h>
45 #include <sys/types.h>
46 #include <sys/sockio.h>
47 #include <sys/systeminfo.h>
48 #include <ftw.h>
49 #include <pool.h>
50 #include <libscf.h>
51 #include <libproc.h>
52 #include <sys/priocntl.h>
53 #include <libuutil.h>
54 #include <wait.h>
55 #include <bsm/adt.h>
56 
57 #include <arpa/inet.h>
58 #include <netdb.h>
59 
60 #include <libxml/xmlmemory.h>
61 #include <libxml/parser.h>
62 
63 #include <libdevinfo.h>
64 #include <uuid/uuid.h>
65 #include <dirent.h>
66 #include <libbrand.h>
67 
68 #include <libzonecfg.h>
69 #include "zonecfg_impl.h"
70 
71 #define	_PATH_TMPFILE	"/zonecfg.XXXXXX"
72 #define	ZONE_CB_RETRY_COUNT		10
73 #define	ZONE_EVENT_PING_SUBCLASS	"ping"
74 #define	ZONE_EVENT_PING_PUBLISHER	"solaris"
75 
76 /* Hard-code the DTD element/attribute/entity names just once, here. */
77 #define	DTD_ELEM_ATTR		(const xmlChar *) "attr"
78 #define	DTD_ELEM_COMMENT	(const xmlChar *) "comment"
79 #define	DTD_ELEM_DEVICE		(const xmlChar *) "device"
80 #define	DTD_ELEM_FS		(const xmlChar *) "filesystem"
81 #define	DTD_ELEM_FSOPTION	(const xmlChar *) "fsoption"
82 #define	DTD_ELEM_IPD		(const xmlChar *) "inherited-pkg-dir"
83 #define	DTD_ELEM_NET		(const xmlChar *) "network"
84 #define	DTD_ELEM_RCTL		(const xmlChar *) "rctl"
85 #define	DTD_ELEM_RCTLVALUE	(const xmlChar *) "rctl-value"
86 #define	DTD_ELEM_ZONE		(const xmlChar *) "zone"
87 #define	DTD_ELEM_DATASET	(const xmlChar *) "dataset"
88 #define	DTD_ELEM_TMPPOOL	(const xmlChar *) "tmp_pool"
89 #define	DTD_ELEM_PSET		(const xmlChar *) "pset"
90 #define	DTD_ELEM_MCAP		(const xmlChar *) "mcap"
91 #define	DTD_ELEM_PACKAGE	(const xmlChar *) "package"
92 #define	DTD_ELEM_PATCH		(const xmlChar *) "patch"
93 #define	DTD_ELEM_OBSOLETES	(const xmlChar *) "obsoletes"
94 #define	DTD_ELEM_DEV_PERM	(const xmlChar *) "dev-perm"
95 
96 #define	DTD_ATTR_ACTION		(const xmlChar *) "action"
97 #define	DTD_ATTR_ADDRESS	(const xmlChar *) "address"
98 #define	DTD_ATTR_AUTOBOOT	(const xmlChar *) "autoboot"
99 #define	DTD_ATTR_IPTYPE		(const xmlChar *) "ip-type"
100 #define	DTD_ATTR_DEFROUTER	(const xmlChar *) "defrouter"
101 #define	DTD_ATTR_DIR		(const xmlChar *) "directory"
102 #define	DTD_ATTR_LIMIT		(const xmlChar *) "limit"
103 #define	DTD_ATTR_LIMITPRIV	(const xmlChar *) "limitpriv"
104 #define	DTD_ATTR_BOOTARGS	(const xmlChar *) "bootargs"
105 #define	DTD_ATTR_SCHED		(const xmlChar *) "scheduling-class"
106 #define	DTD_ATTR_MATCH		(const xmlChar *) "match"
107 #define	DTD_ATTR_NAME		(const xmlChar *) "name"
108 #define	DTD_ATTR_PHYSICAL	(const xmlChar *) "physical"
109 #define	DTD_ATTR_POOL		(const xmlChar *) "pool"
110 #define	DTD_ATTR_PRIV		(const xmlChar *) "priv"
111 #define	DTD_ATTR_RAW		(const xmlChar *) "raw"
112 #define	DTD_ATTR_SPECIAL	(const xmlChar *) "special"
113 #define	DTD_ATTR_TYPE		(const xmlChar *) "type"
114 #define	DTD_ATTR_VALUE		(const xmlChar *) "value"
115 #define	DTD_ATTR_ZONEPATH	(const xmlChar *) "zonepath"
116 #define	DTD_ATTR_NCPU_MIN	(const xmlChar *) "ncpu_min"
117 #define	DTD_ATTR_NCPU_MAX	(const xmlChar *) "ncpu_max"
118 #define	DTD_ATTR_IMPORTANCE	(const xmlChar *) "importance"
119 #define	DTD_ATTR_PHYSCAP	(const xmlChar *) "physcap"
120 #define	DTD_ATTR_VERSION	(const xmlChar *) "version"
121 #define	DTD_ATTR_ID		(const xmlChar *) "id"
122 #define	DTD_ATTR_UID		(const xmlChar *) "uid"
123 #define	DTD_ATTR_GID		(const xmlChar *) "gid"
124 #define	DTD_ATTR_MODE		(const xmlChar *) "mode"
125 #define	DTD_ATTR_ACL		(const xmlChar *) "acl"
126 #define	DTD_ATTR_BRAND		(const xmlChar *) "brand"
127 #define	DTD_ATTR_HOSTID		(const xmlChar *) "hostid"
128 
129 #define	DTD_ENTITY_BOOLEAN	"boolean"
130 #define	DTD_ENTITY_DEVPATH	"devpath"
131 #define	DTD_ENTITY_DRIVER	"driver"
132 #define	DTD_ENTITY_DRVMIN	"drv_min"
133 #define	DTD_ENTITY_FALSE	"false"
134 #define	DTD_ENTITY_INT		"int"
135 #define	DTD_ENTITY_STRING	"string"
136 #define	DTD_ENTITY_TRUE		"true"
137 #define	DTD_ENTITY_UINT		"uint"
138 
139 #define	DTD_ENTITY_BOOL_LEN	6	/* "false" */
140 
141 #define	ATTACH_FORCED	"SUNWattached.xml"
142 
143 #define	TMP_POOL_NAME	"SUNWtmp_%s"
144 #define	MAX_TMP_POOL_NAME	(ZONENAME_MAX + 9)
145 #define	RCAP_SERVICE	"system/rcap:default"
146 #define	POOLD_SERVICE	"system/pools/dynamic:default"
147 
148 /*
149  * rctl alias definitions
150  *
151  * This holds the alias, the full rctl name, the default priv value, action
152  * and lower limit.  The functions that handle rctl aliases step through
153  * this table, matching on the alias, and using the full values for setting
154  * the rctl entry as well the limit for validation.
155  */
156 static struct alias {
157 	char *shortname;
158 	char *realname;
159 	char *priv;
160 	char *action;
161 	uint64_t low_limit;
162 } aliases[] = {
163 	{ALIAS_MAXLWPS, "zone.max-lwps", "privileged", "deny", 100},
164 	{ALIAS_MAXSHMMEM, "zone.max-shm-memory", "privileged", "deny", 0},
165 	{ALIAS_MAXSHMIDS, "zone.max-shm-ids", "privileged", "deny", 0},
166 	{ALIAS_MAXMSGIDS, "zone.max-msg-ids", "privileged", "deny", 0},
167 	{ALIAS_MAXSEMIDS, "zone.max-sem-ids", "privileged", "deny", 0},
168 	{ALIAS_MAXLOCKEDMEM, "zone.max-locked-memory", "privileged", "deny", 0},
169 	{ALIAS_MAXSWAP, "zone.max-swap", "privileged", "deny", 0},
170 	{ALIAS_SHARES, "zone.cpu-shares", "privileged", "none", 0},
171 	{ALIAS_CPUCAP, "zone.cpu-cap", "privileged", "deny", 0},
172 	{NULL, NULL, NULL, NULL, 0}
173 };
174 
175 /*
176  * Structure for applying rctls to a running zone.  It allows important
177  * process values to be passed together easily.
178  */
179 typedef struct pr_info_handle {
180 	struct ps_prochandle *pr;
181 	pid_t pid;
182 } pr_info_handle_t;
183 
184 struct zone_dochandle {
185 	char		*zone_dh_rootdir;
186 	xmlDocPtr	zone_dh_doc;
187 	xmlNodePtr	zone_dh_cur;
188 	xmlNodePtr	zone_dh_top;
189 	boolean_t	zone_dh_newzone;
190 	boolean_t	zone_dh_snapshot;
191 	boolean_t	zone_dh_sw_inv;
192 	char		zone_dh_delete_name[ZONENAME_MAX];
193 };
194 
195 struct znotify {
196 	void * zn_private;
197 	evchan_t *zn_eventchan;
198 	int (*zn_callback)(const  char *zonename, zoneid_t zid,
199 	    const char *newstate, const char *oldstate, hrtime_t when, void *p);
200 	pthread_mutex_t zn_mutex;
201 	pthread_cond_t zn_cond;
202 	pthread_mutex_t zn_bigmutex;
203 	volatile enum {ZN_UNLOCKED, ZN_LOCKED, ZN_PING_INFLIGHT,
204 	    ZN_PING_RECEIVED} zn_state;
205 	char zn_subscriber_id[MAX_SUBID_LEN];
206 	volatile boolean_t zn_failed;
207 	int zn_failure_count;
208 };
209 
210 /* used to track nested zone-lock operations */
211 static int zone_lock_cnt = 0;
212 
213 /* used to communicate lock status to children */
214 #define	LOCK_ENV_VAR	"_ZONEADM_LOCK_HELD"
215 static char zoneadm_lock_held[] = LOCK_ENV_VAR"=1";
216 static char zoneadm_lock_not_held[] = LOCK_ENV_VAR"=0";
217 
218 char *zonecfg_root = "";
219 
220 /*
221  * For functions which return int, which is most of the functions herein,
222  * the return values should be from the Z_foo set defined in <libzonecfg.h>.
223  * In some instances, we take pains mapping some libc errno values to Z_foo
224  * values from this set.
225  */
226 
227 /*
228  * Set the root (/) path for all zonecfg configuration files.  This is a
229  * private interface used by Live Upgrade extensions to access zone
230  * configuration inside mounted alternate boot environments.
231  */
232 void
233 zonecfg_set_root(const char *rootpath)
234 {
235 	if (*zonecfg_root != '\0')
236 		free(zonecfg_root);
237 	if (rootpath == NULL || rootpath[0] == '\0' || rootpath[1] == '\0' ||
238 	    (zonecfg_root = strdup(rootpath)) == NULL)
239 		zonecfg_root = "";
240 }
241 
242 const char *
243 zonecfg_get_root(void)
244 {
245 	return (zonecfg_root);
246 }
247 
248 boolean_t
249 zonecfg_in_alt_root(void)
250 {
251 	return (*zonecfg_root != '\0');
252 }
253 
254 /*
255  * Callers of the _file_path() functions are expected to have the second
256  * parameter be a (char foo[MAXPATHLEN]).
257  */
258 
259 static boolean_t
260 config_file_path(const char *zonename, char *answer)
261 {
262 	return (snprintf(answer, MAXPATHLEN, "%s%s/%s.xml", zonecfg_root,
263 	    ZONE_CONFIG_ROOT, zonename) < MAXPATHLEN);
264 }
265 
266 static boolean_t
267 snap_file_path(const char *zonename, char *answer)
268 {
269 	return (snprintf(answer, MAXPATHLEN, "%s%s/%s.snapshot.xml",
270 	    zonecfg_root, ZONE_SNAPSHOT_ROOT, zonename) < MAXPATHLEN);
271 }
272 
273 /*ARGSUSED*/
274 static void
275 zonecfg_error_func(void *ctx, const char *msg, ...)
276 {
277 	/*
278 	 * This function does nothing by design.  Its purpose is to prevent
279 	 * libxml from dumping unwanted messages to stdout/stderr.
280 	 */
281 }
282 
283 zone_dochandle_t
284 zonecfg_init_handle(void)
285 {
286 	zone_dochandle_t handle = calloc(1, sizeof (struct zone_dochandle));
287 	if (handle == NULL) {
288 		errno = Z_NOMEM;
289 		return (NULL);
290 	}
291 
292 	/* generic libxml initialization */
293 	xmlLineNumbersDefault(1);
294 	xmlLoadExtDtdDefaultValue |= XML_DETECT_IDS;
295 	xmlDoValidityCheckingDefaultValue = 1;
296 	(void) xmlKeepBlanksDefault(0);
297 	xmlGetWarningsDefaultValue = 0;
298 	xmlSetGenericErrorFunc(NULL, zonecfg_error_func);
299 
300 	return (handle);
301 }
302 
303 int
304 zonecfg_check_handle(zone_dochandle_t handle)
305 {
306 	if (handle == NULL || handle->zone_dh_doc == NULL)
307 		return (Z_BAD_HANDLE);
308 	return (Z_OK);
309 }
310 
311 void
312 zonecfg_fini_handle(zone_dochandle_t handle)
313 {
314 	if (zonecfg_check_handle(handle) == Z_OK)
315 		xmlFreeDoc(handle->zone_dh_doc);
316 	if (handle != NULL)
317 		free(handle);
318 }
319 
320 static int
321 zonecfg_destroy_impl(char *filename)
322 {
323 	if (unlink(filename) == -1) {
324 		if (errno == EACCES)
325 			return (Z_ACCES);
326 		if (errno == ENOENT)
327 			return (Z_NO_ZONE);
328 		return (Z_MISC_FS);
329 	}
330 	return (Z_OK);
331 }
332 
333 int
334 zonecfg_destroy(const char *zonename, boolean_t force)
335 {
336 	char path[MAXPATHLEN];
337 	struct zoneent ze;
338 	int err, state_err;
339 	zone_state_t state;
340 
341 	if (!config_file_path(zonename, path))
342 		return (Z_MISC_FS);
343 
344 	state_err = zone_get_state((char *)zonename, &state);
345 	err = access(path, W_OK);
346 
347 	/*
348 	 * If there is no file, and no index entry, reliably indicate that no
349 	 * such zone exists.
350 	 */
351 	if ((state_err == Z_NO_ZONE) && (err == -1) && (errno == ENOENT))
352 		return (Z_NO_ZONE);
353 
354 	/*
355 	 * Handle any other filesystem related errors (except if the XML
356 	 * file is missing, which we treat silently), unless we're forcing,
357 	 * in which case we plow on.
358 	 */
359 	if (err == -1 && errno != ENOENT) {
360 		if (errno == EACCES)
361 			return (Z_ACCES);
362 		else if (!force)
363 			return (Z_MISC_FS);
364 	}
365 
366 	if (state > ZONE_STATE_INSTALLED)
367 		return (Z_BAD_ZONE_STATE);
368 
369 	if (!force && state > ZONE_STATE_CONFIGURED)
370 		return (Z_BAD_ZONE_STATE);
371 
372 	/*
373 	 * Index deletion succeeds even if the entry doesn't exist.  So this
374 	 * will fail only if we've had some more severe problem.
375 	 */
376 	bzero(&ze, sizeof (ze));
377 	(void) strlcpy(ze.zone_name, zonename, sizeof (ze.zone_name));
378 	if ((err = putzoneent(&ze, PZE_REMOVE)) != Z_OK)
379 		if (!force)
380 			return (err);
381 
382 	err = zonecfg_destroy_impl(path);
383 
384 	/*
385 	 * Treat failure to find the XML file silently, since, well, it's
386 	 * gone, and with the index file cleaned up, we're done.
387 	 */
388 	if (err == Z_OK || err == Z_NO_ZONE)
389 		return (Z_OK);
390 	return (err);
391 }
392 
393 int
394 zonecfg_destroy_snapshot(const char *zonename)
395 {
396 	char path[MAXPATHLEN];
397 
398 	if (!snap_file_path(zonename, path))
399 		return (Z_MISC_FS);
400 	return (zonecfg_destroy_impl(path));
401 }
402 
403 static int
404 getroot(zone_dochandle_t handle, xmlNodePtr *root)
405 {
406 	if (zonecfg_check_handle(handle) == Z_BAD_HANDLE)
407 		return (Z_BAD_HANDLE);
408 
409 	*root = xmlDocGetRootElement(handle->zone_dh_doc);
410 
411 	if (*root == NULL)
412 		return (Z_EMPTY_DOCUMENT);
413 
414 	if (xmlStrcmp((*root)->name, DTD_ELEM_ZONE))
415 		return (Z_WRONG_DOC_TYPE);
416 
417 	return (Z_OK);
418 }
419 
420 static int
421 operation_prep(zone_dochandle_t handle)
422 {
423 	xmlNodePtr root;
424 	int err;
425 
426 	if ((err = getroot(handle, &root)) != 0)
427 		return (err);
428 
429 	handle->zone_dh_cur = root;
430 	handle->zone_dh_top = root;
431 	return (Z_OK);
432 }
433 
434 static int
435 fetchprop(xmlNodePtr cur, const xmlChar *propname, char *dst, size_t dstsize)
436 {
437 	xmlChar *property;
438 	size_t srcsize;
439 
440 	if ((property = xmlGetProp(cur, propname)) == NULL)
441 		return (Z_BAD_PROPERTY);
442 	srcsize = strlcpy(dst, (char *)property, dstsize);
443 	xmlFree(property);
444 	if (srcsize >= dstsize)
445 		return (Z_TOO_BIG);
446 	return (Z_OK);
447 }
448 
449 static int
450 fetch_alloc_prop(xmlNodePtr cur, const xmlChar *propname, char **dst)
451 {
452 	xmlChar *property;
453 
454 	if ((property = xmlGetProp(cur, propname)) == NULL)
455 		return (Z_BAD_PROPERTY);
456 	if ((*dst = strdup((char *)property)) == NULL) {
457 		xmlFree(property);
458 		return (Z_NOMEM);
459 	}
460 	xmlFree(property);
461 	return (Z_OK);
462 }
463 
464 static int
465 getrootattr(zone_dochandle_t handle, const xmlChar *propname,
466     char *propval, size_t propsize)
467 {
468 	xmlNodePtr root;
469 	int err;
470 
471 	if ((err = getroot(handle, &root)) != 0)
472 		return (err);
473 
474 	return (fetchprop(root, propname, propval, propsize));
475 }
476 
477 static int
478 get_alloc_rootattr(zone_dochandle_t handle, const xmlChar *propname,
479     char **propval)
480 {
481 	xmlNodePtr root;
482 	int err;
483 
484 	if ((err = getroot(handle, &root)) != 0)
485 		return (err);
486 
487 	return (fetch_alloc_prop(root, propname, propval));
488 }
489 
490 static int
491 setrootattr(zone_dochandle_t handle, const xmlChar *propname,
492     const char *propval)
493 {
494 	int err;
495 	xmlNodePtr root;
496 
497 	if ((err = getroot(handle, &root)) != Z_OK)
498 		return (err);
499 
500 	/*
501 	 * If we get a null propval remove the property (ignore return since it
502 	 * may not be set to begin with).
503 	 */
504 	if (propval == NULL) {
505 		(void) xmlUnsetProp(root, propname);
506 	} else {
507 		if (xmlSetProp(root, propname, (const xmlChar *) propval)
508 		    == NULL)
509 			return (Z_INVAL);
510 	}
511 	return (Z_OK);
512 }
513 
514 static void
515 addcomment(zone_dochandle_t handle, const char *comment)
516 {
517 	xmlNodePtr node;
518 	node = xmlNewComment((xmlChar *) comment);
519 
520 	if (node != NULL)
521 		(void) xmlAddPrevSibling(handle->zone_dh_top, node);
522 }
523 
524 static void
525 stripcomments(zone_dochandle_t handle)
526 {
527 	xmlDocPtr top;
528 	xmlNodePtr child, next;
529 
530 	top = handle->zone_dh_doc;
531 	for (child = top->xmlChildrenNode; child != NULL; child = next) {
532 		next = child->next;
533 		if (child->name == NULL)
534 			continue;
535 		if (xmlStrcmp(child->name, DTD_ELEM_COMMENT) == 0) {
536 			next = child->next;
537 			xmlUnlinkNode(child);
538 			xmlFreeNode(child);
539 		}
540 	}
541 }
542 
543 static void
544 strip_sw_inv(zone_dochandle_t handle)
545 {
546 	xmlNodePtr root, child, next;
547 
548 	root = xmlDocGetRootElement(handle->zone_dh_doc);
549 	for (child = root->xmlChildrenNode; child != NULL; child = next) {
550 		next = child->next;
551 		if (child->name == NULL)
552 			continue;
553 		if (xmlStrcmp(child->name, DTD_ELEM_PACKAGE) == 0 ||
554 		    xmlStrcmp(child->name, DTD_ELEM_PATCH) == 0) {
555 			next = child->next;
556 			xmlUnlinkNode(child);
557 			xmlFreeNode(child);
558 		}
559 	}
560 }
561 
562 static int
563 zonecfg_get_handle_impl(const char *zonename, const char *filename,
564     zone_dochandle_t handle)
565 {
566 	xmlValidCtxtPtr cvp;
567 	struct stat statbuf;
568 	int valid;
569 
570 	if (zonename == NULL)
571 		return (Z_NO_ZONE);
572 
573 	if ((handle->zone_dh_doc = xmlParseFile(filename)) == NULL) {
574 		/* distinguish file not found vs. found but not parsed */
575 		if (stat(filename, &statbuf) == 0)
576 			return (Z_INVALID_DOCUMENT);
577 		return (Z_NO_ZONE);
578 	}
579 	if ((cvp = xmlNewValidCtxt()) == NULL)
580 		return (Z_NOMEM);
581 	cvp->error = zonecfg_error_func;
582 	cvp->warning = zonecfg_error_func;
583 	valid = xmlValidateDocument(cvp, handle->zone_dh_doc);
584 	xmlFreeValidCtxt(cvp);
585 	if (valid == 0)
586 		return (Z_INVALID_DOCUMENT);
587 
588 	/* delete any comments such as inherited Sun copyright / ident str */
589 	stripcomments(handle);
590 	return (Z_OK);
591 }
592 
593 int
594 zonecfg_get_handle(const char *zonename, zone_dochandle_t handle)
595 {
596 	char path[MAXPATHLEN];
597 
598 	if (!config_file_path(zonename, path))
599 		return (Z_MISC_FS);
600 	handle->zone_dh_newzone = B_FALSE;
601 
602 	return (zonecfg_get_handle_impl(zonename, path, handle));
603 }
604 
605 int
606 zonecfg_get_attach_handle(const char *path, const char *fname,
607     const char *zonename, boolean_t preserve_sw, zone_dochandle_t handle)
608 {
609 	char		migpath[MAXPATHLEN];
610 	int		err;
611 	struct stat	buf;
612 
613 	if (snprintf(migpath, sizeof (migpath), "%s/root", path) >=
614 	    sizeof (migpath))
615 		return (Z_NOMEM);
616 
617 	if (stat(migpath, &buf) == -1 || !S_ISDIR(buf.st_mode))
618 		return (Z_NO_ZONE);
619 
620 	if (snprintf(migpath, sizeof (migpath), "%s/%s", path, fname) >=
621 	    sizeof (migpath))
622 		return (Z_NOMEM);
623 
624 	if ((err = zonecfg_get_handle_impl(zonename, migpath, handle)) != Z_OK)
625 		return (err);
626 
627 	if (!preserve_sw)
628 		strip_sw_inv(handle);
629 
630 	handle->zone_dh_newzone = B_TRUE;
631 	if ((err = setrootattr(handle, DTD_ATTR_ZONEPATH, path)) != Z_OK)
632 		return (err);
633 
634 	return (setrootattr(handle, DTD_ATTR_NAME, zonename));
635 }
636 
637 int
638 zonecfg_get_snapshot_handle(const char *zonename, zone_dochandle_t handle)
639 {
640 	char path[MAXPATHLEN];
641 
642 	if (!snap_file_path(zonename, path))
643 		return (Z_MISC_FS);
644 	handle->zone_dh_newzone = B_FALSE;
645 	return (zonecfg_get_handle_impl(zonename, path, handle));
646 }
647 
648 int
649 zonecfg_get_template_handle(const char *template, const char *zonename,
650     zone_dochandle_t handle)
651 {
652 	char path[MAXPATHLEN];
653 	int err;
654 
655 	if (!config_file_path(template, path))
656 		return (Z_MISC_FS);
657 
658 	if ((err = zonecfg_get_handle_impl(template, path, handle)) != Z_OK)
659 		return (err);
660 	handle->zone_dh_newzone = B_TRUE;
661 	return (setrootattr(handle, DTD_ATTR_NAME, zonename));
662 }
663 
664 int
665 zonecfg_get_xml_handle(const char *path, zone_dochandle_t handle)
666 {
667 	struct stat buf;
668 	int err;
669 
670 	if (stat(path, &buf) == -1)
671 		return (Z_MISC_FS);
672 
673 	if ((err = zonecfg_get_handle_impl("xml", path, handle)) != Z_OK)
674 		return (err);
675 	handle->zone_dh_newzone = B_TRUE;
676 	return (Z_OK);
677 }
678 
679 /*
680  * Initialize two handles from the manifest read on fd.  The rem_handle
681  * is initialized from the input file, including the sw inventory.  The
682  * local_handle is initialized with the same zone configuration but with
683  * no sw inventory.
684  */
685 int
686 zonecfg_attach_manifest(int fd, zone_dochandle_t local_handle,
687     zone_dochandle_t rem_handle)
688 {
689 	xmlValidCtxtPtr cvp;
690 	int valid;
691 
692 	/* load the manifest into the handle for the remote system */
693 	if ((rem_handle->zone_dh_doc = xmlReadFd(fd, NULL, NULL, 0)) == NULL) {
694 		return (Z_INVALID_DOCUMENT);
695 	}
696 	if ((cvp = xmlNewValidCtxt()) == NULL)
697 		return (Z_NOMEM);
698 	cvp->error = zonecfg_error_func;
699 	cvp->warning = zonecfg_error_func;
700 	valid = xmlValidateDocument(cvp, rem_handle->zone_dh_doc);
701 	xmlFreeValidCtxt(cvp);
702 	if (valid == 0)
703 		return (Z_INVALID_DOCUMENT);
704 
705 	/* delete any comments such as inherited Sun copyright / ident str */
706 	stripcomments(rem_handle);
707 
708 	rem_handle->zone_dh_newzone = B_TRUE;
709 	rem_handle->zone_dh_sw_inv = B_TRUE;
710 
711 	/*
712 	 * Now use the remote system handle to generate a local system handle
713 	 * with an identical zones configuration but no sw inventory.
714 	 */
715 	if ((local_handle->zone_dh_doc = xmlCopyDoc(rem_handle->zone_dh_doc,
716 	    1)) == NULL) {
717 		return (Z_INVALID_DOCUMENT);
718 	}
719 
720 	/*
721 	 * We need to re-run xmlValidateDocument on local_handle to properly
722 	 * update the in-core representation of the configuration.
723 	 */
724 	if ((cvp = xmlNewValidCtxt()) == NULL)
725 		return (Z_NOMEM);
726 	cvp->error = zonecfg_error_func;
727 	cvp->warning = zonecfg_error_func;
728 	valid = xmlValidateDocument(cvp, local_handle->zone_dh_doc);
729 	xmlFreeValidCtxt(cvp);
730 	if (valid == 0)
731 		return (Z_INVALID_DOCUMENT);
732 
733 	strip_sw_inv(local_handle);
734 
735 	local_handle->zone_dh_newzone = B_TRUE;
736 	local_handle->zone_dh_sw_inv = B_FALSE;
737 
738 	return (Z_OK);
739 }
740 
741 static boolean_t
742 is_renaming(zone_dochandle_t handle)
743 {
744 	if (handle->zone_dh_newzone)
745 		return (B_FALSE);
746 	if (strlen(handle->zone_dh_delete_name) > 0)
747 		return (B_TRUE);
748 	return (B_FALSE);
749 }
750 
751 static boolean_t
752 is_new(zone_dochandle_t handle)
753 {
754 	return (handle->zone_dh_newzone || handle->zone_dh_snapshot);
755 }
756 
757 static boolean_t
758 is_snapshot(zone_dochandle_t handle)
759 {
760 	return (handle->zone_dh_snapshot);
761 }
762 
763 /*
764  * It would be great to be able to use libc's ctype(3c) macros, but we
765  * can't, as they are locale sensitive, and it would break our limited thread
766  * safety if this routine had to change the app locale on the fly.
767  */
768 int
769 zonecfg_validate_zonename(const char *zone)
770 {
771 	int i;
772 
773 	if (strcmp(zone, GLOBAL_ZONENAME) == 0)
774 		return (Z_BOGUS_ZONE_NAME);
775 
776 	if (strlen(zone) >= ZONENAME_MAX)
777 		return (Z_BOGUS_ZONE_NAME);
778 
779 	if (!((zone[0] >= 'a' && zone[0] <= 'z') ||
780 	    (zone[0] >= 'A' && zone[0] <= 'Z') ||
781 	    (zone[0] >= '0' && zone[0] <= '9')))
782 		return (Z_BOGUS_ZONE_NAME);
783 
784 	for (i = 1; zone[i] != '\0'; i++) {
785 		if (!((zone[i] >= 'a' && zone[i] <= 'z') ||
786 		    (zone[i] >= 'A' && zone[i] <= 'Z') ||
787 		    (zone[i] >= '0' && zone[i] <= '9') ||
788 		    (zone[i] == '-') || (zone[i] == '_') || (zone[i] == '.')))
789 			return (Z_BOGUS_ZONE_NAME);
790 	}
791 
792 	return (Z_OK);
793 }
794 
795 /*
796  * Changing the zone name requires us to track both the old and new
797  * name of the zone until commit time.
798  */
799 int
800 zonecfg_get_name(zone_dochandle_t handle, char *name, size_t namesize)
801 {
802 	return (getrootattr(handle, DTD_ATTR_NAME, name, namesize));
803 }
804 
805 int
806 zonecfg_set_name(zone_dochandle_t handle, char *name)
807 {
808 	zone_state_t state;
809 	char curname[ZONENAME_MAX], old_delname[ZONENAME_MAX];
810 	int err;
811 
812 	if ((err = getrootattr(handle, DTD_ATTR_NAME, curname,
813 	    sizeof (curname))) != Z_OK)
814 		return (err);
815 
816 	if (strcmp(name, curname) == 0)
817 		return (Z_OK);
818 
819 	/*
820 	 * Switching zone names to one beginning with SUNW is not permitted.
821 	 */
822 	if (strncmp(name, "SUNW", 4) == 0)
823 		return (Z_BOGUS_ZONE_NAME);
824 
825 	if ((err = zonecfg_validate_zonename(name)) != Z_OK)
826 		return (err);
827 
828 	/*
829 	 * Setting the name back to the original name (effectively a revert of
830 	 * the name) is fine.  But if we carry on, we'll falsely identify the
831 	 * name as "in use," so special case here.
832 	 */
833 	if (strcmp(name, handle->zone_dh_delete_name) == 0) {
834 		err = setrootattr(handle, DTD_ATTR_NAME, name);
835 		handle->zone_dh_delete_name[0] = '\0';
836 		return (err);
837 	}
838 
839 	/* Check to see if new name chosen is already in use */
840 	if (zone_get_state(name, &state) != Z_NO_ZONE)
841 		return (Z_NAME_IN_USE);
842 
843 	/*
844 	 * If this isn't already "new" or in a renaming transition, then
845 	 * we're initiating a rename here; so stash the "delete name"
846 	 * (i.e. the name of the zone we'll be removing) for the rename.
847 	 */
848 	(void) strlcpy(old_delname, handle->zone_dh_delete_name,
849 	    sizeof (old_delname));
850 	if (!is_new(handle) && !is_renaming(handle)) {
851 		/*
852 		 * Name change is allowed only when the zone we're altering
853 		 * is not ready or running.
854 		 */
855 		err = zone_get_state(curname, &state);
856 		if (err == Z_OK) {
857 			if (state > ZONE_STATE_INSTALLED)
858 				return (Z_BAD_ZONE_STATE);
859 		} else if (err != Z_NO_ZONE) {
860 			return (err);
861 		}
862 
863 		(void) strlcpy(handle->zone_dh_delete_name, curname,
864 		    sizeof (handle->zone_dh_delete_name));
865 		assert(is_renaming(handle));
866 	} else if (is_renaming(handle)) {
867 		err = zone_get_state(handle->zone_dh_delete_name, &state);
868 		if (err == Z_OK) {
869 			if (state > ZONE_STATE_INSTALLED)
870 				return (Z_BAD_ZONE_STATE);
871 		} else if (err != Z_NO_ZONE) {
872 			return (err);
873 		}
874 	}
875 
876 	if ((err = setrootattr(handle, DTD_ATTR_NAME, name)) != Z_OK) {
877 		/*
878 		 * Restore the deletename to whatever it was at the
879 		 * top of the routine, since we've had a failure.
880 		 */
881 		(void) strlcpy(handle->zone_dh_delete_name, old_delname,
882 		    sizeof (handle->zone_dh_delete_name));
883 		return (err);
884 	}
885 
886 	return (Z_OK);
887 }
888 
889 int
890 zonecfg_get_zonepath(zone_dochandle_t handle, char *path, size_t pathsize)
891 {
892 	size_t len;
893 
894 	if ((len = strlcpy(path, zonecfg_root, pathsize)) >= pathsize)
895 		return (Z_TOO_BIG);
896 	return (getrootattr(handle, DTD_ATTR_ZONEPATH, path + len,
897 	    pathsize - len));
898 }
899 
900 int
901 zonecfg_set_zonepath(zone_dochandle_t handle, char *zonepath)
902 {
903 	size_t len;
904 
905 	/*
906 	 * The user deals in absolute paths in the running global zone, but the
907 	 * internal configuration files deal with boot environment relative
908 	 * paths.  Strip out the alternate root when specified.
909 	 */
910 	len = strlen(zonecfg_root);
911 	if (strncmp(zonepath, zonecfg_root, len) != 0 || zonepath[len] != '/')
912 		return (Z_BAD_PROPERTY);
913 	zonepath += len;
914 	return (setrootattr(handle, DTD_ATTR_ZONEPATH, zonepath));
915 }
916 
917 int
918 zonecfg_get_brand(zone_dochandle_t handle, char *brand, size_t brandsize)
919 {
920 	int ret, sz;
921 
922 	ret = getrootattr(handle, DTD_ATTR_BRAND, brand, brandsize);
923 
924 	/* If the zone has no brand, it is native. */
925 	if (ret == Z_OK && brand[0] == '\0') {
926 		sz = strlcpy(brand, NATIVE_BRAND_NAME, brandsize);
927 		if (sz >= brandsize)
928 			ret = Z_TOO_BIG;
929 		else
930 			ret = Z_OK;
931 	}
932 
933 	return (ret);
934 }
935 
936 int
937 zonecfg_set_brand(zone_dochandle_t handle, char *brand)
938 {
939 	return (setrootattr(handle, DTD_ATTR_BRAND, brand));
940 }
941 
942 int
943 zonecfg_get_autoboot(zone_dochandle_t handle, boolean_t *autoboot)
944 {
945 	char autobootstr[DTD_ENTITY_BOOL_LEN];
946 	int ret;
947 
948 	if ((ret = getrootattr(handle, DTD_ATTR_AUTOBOOT, autobootstr,
949 	    sizeof (autobootstr))) != Z_OK)
950 		return (ret);
951 
952 	if (strcmp(autobootstr, DTD_ENTITY_TRUE) == 0)
953 		*autoboot = B_TRUE;
954 	else if (strcmp(autobootstr, DTD_ENTITY_FALSE) == 0)
955 		*autoboot = B_FALSE;
956 	else
957 		ret = Z_BAD_PROPERTY;
958 	return (ret);
959 }
960 
961 int
962 zonecfg_set_autoboot(zone_dochandle_t handle, boolean_t autoboot)
963 {
964 	return (setrootattr(handle, DTD_ATTR_AUTOBOOT,
965 	    autoboot ? DTD_ENTITY_TRUE : DTD_ENTITY_FALSE));
966 }
967 
968 int
969 zonecfg_get_pool(zone_dochandle_t handle, char *pool, size_t poolsize)
970 {
971 	return (getrootattr(handle, DTD_ATTR_POOL, pool, poolsize));
972 }
973 
974 int
975 zonecfg_set_pool(zone_dochandle_t handle, char *pool)
976 {
977 	return (setrootattr(handle, DTD_ATTR_POOL, pool));
978 }
979 
980 int
981 zonecfg_get_limitpriv(zone_dochandle_t handle, char **limitpriv)
982 {
983 	return (get_alloc_rootattr(handle, DTD_ATTR_LIMITPRIV, limitpriv));
984 }
985 
986 int
987 zonecfg_set_limitpriv(zone_dochandle_t handle, char *limitpriv)
988 {
989 	return (setrootattr(handle, DTD_ATTR_LIMITPRIV, limitpriv));
990 }
991 
992 int
993 zonecfg_get_bootargs(zone_dochandle_t handle, char *bargs, size_t bargssize)
994 {
995 	return (getrootattr(handle, DTD_ATTR_BOOTARGS, bargs, bargssize));
996 }
997 
998 int
999 zonecfg_set_bootargs(zone_dochandle_t handle, char *bargs)
1000 {
1001 	return (setrootattr(handle, DTD_ATTR_BOOTARGS, bargs));
1002 }
1003 
1004 int
1005 zonecfg_get_sched_class(zone_dochandle_t handle, char *sched, size_t schedsize)
1006 {
1007 	return (getrootattr(handle, DTD_ATTR_SCHED, sched, schedsize));
1008 }
1009 
1010 int
1011 zonecfg_set_sched(zone_dochandle_t handle, char *sched)
1012 {
1013 	return (setrootattr(handle, DTD_ATTR_SCHED, sched));
1014 }
1015 
1016 /*
1017  * /etc/zones/index caches a vital piece of information which is also
1018  * in the <zonename>.xml file: the path to the zone.  This is for performance,
1019  * since we need to walk all zonepath's in order to be able to detect conflicts
1020  * (see crosscheck_zonepaths() in the zoneadm command).
1021  *
1022  * An additional complexity is that when doing a rename, we'd like the entire
1023  * index update operation (rename, and potential state changes) to be atomic.
1024  * In general, the operation of this function should succeed or fail as
1025  * a unit.
1026  */
1027 int
1028 zonecfg_refresh_index_file(zone_dochandle_t handle)
1029 {
1030 	char name[ZONENAME_MAX], zonepath[MAXPATHLEN];
1031 	struct zoneent ze;
1032 	int err;
1033 	int opcode;
1034 	char *zn;
1035 
1036 	bzero(&ze, sizeof (ze));
1037 	ze.zone_state = -1;	/* Preserve existing state in index */
1038 
1039 	if ((err = zonecfg_get_name(handle, name, sizeof (name))) != Z_OK)
1040 		return (err);
1041 	(void) strlcpy(ze.zone_name, name, sizeof (ze.zone_name));
1042 
1043 	if ((err = zonecfg_get_zonepath(handle, zonepath,
1044 	    sizeof (zonepath))) != Z_OK)
1045 		return (err);
1046 	(void) strlcpy(ze.zone_path, zonepath + strlen(zonecfg_root),
1047 	    sizeof (ze.zone_path));
1048 
1049 	if (is_renaming(handle)) {
1050 		opcode = PZE_MODIFY;
1051 		(void) strlcpy(ze.zone_name, handle->zone_dh_delete_name,
1052 		    sizeof (ze.zone_name));
1053 		(void) strlcpy(ze.zone_newname, name, sizeof (ze.zone_newname));
1054 	} else if (is_new(handle)) {
1055 		FILE *cookie;
1056 		/*
1057 		 * Be tolerant of the zone already existing in the index file,
1058 		 * since we might be forcibly overwriting an existing
1059 		 * configuration with a new one (for example 'create -F'
1060 		 * in zonecfg).
1061 		 */
1062 		opcode = PZE_ADD;
1063 		cookie = setzoneent();
1064 		while ((zn = getzoneent(cookie)) != NULL) {
1065 			if (strcmp(zn, name) == 0) {
1066 				opcode = PZE_MODIFY;
1067 				free(zn);
1068 				break;
1069 			}
1070 			free(zn);
1071 		}
1072 		endzoneent(cookie);
1073 		ze.zone_state = ZONE_STATE_CONFIGURED;
1074 	} else {
1075 		opcode = PZE_MODIFY;
1076 	}
1077 
1078 	if ((err = putzoneent(&ze, opcode)) != Z_OK)
1079 		return (err);
1080 
1081 	return (Z_OK);
1082 }
1083 
1084 /*
1085  * The goal of this routine is to cause the index file update and the
1086  * document save to happen as an atomic operation.  We do the document
1087  * first, saving a backup copy using a hard link; if that succeeds, we go
1088  * on to the index.  If that fails, we roll the document back into place.
1089  *
1090  * Strategy:
1091  *
1092  * New zone 'foo' configuration:
1093  * 	Create tmpfile (zonecfg.xxxxxx)
1094  * 	Write XML to tmpfile
1095  * 	Rename tmpfile to xmlfile (zonecfg.xxxxxx -> foo.xml)
1096  * 	Add entry to index file
1097  * 	If it fails, delete foo.xml, leaving nothing behind.
1098  *
1099  * Save existing zone 'foo':
1100  * 	Make backup of foo.xml -> .backup
1101  * 	Create tmpfile (zonecfg.xxxxxx)
1102  * 	Write XML to tmpfile
1103  * 	Rename tmpfile to xmlfile (zonecfg.xxxxxx -> foo.xml)
1104  * 	Modify index file as needed
1105  * 	If it fails, recover from .backup -> foo.xml
1106  *
1107  * Rename 'foo' to 'bar':
1108  * 	Create tmpfile (zonecfg.xxxxxx)
1109  * 	Write XML to tmpfile
1110  * 	Rename tmpfile to xmlfile (zonecfg.xxxxxx -> bar.xml)
1111  * 	Add entry for 'bar' to index file, Remove entry for 'foo' (refresh)
1112  * 	If it fails, delete bar.xml; foo.xml is left behind.
1113  */
1114 static int
1115 zonecfg_save_impl(zone_dochandle_t handle, char *filename)
1116 {
1117 	char tmpfile[MAXPATHLEN];
1118 	char bakdir[MAXPATHLEN], bakbase[MAXPATHLEN], bakfile[MAXPATHLEN];
1119 	int tmpfd, err, valid;
1120 	xmlValidCtxt cvp = { NULL };
1121 	boolean_t backup;
1122 
1123 	(void) strlcpy(tmpfile, filename, sizeof (tmpfile));
1124 	(void) dirname(tmpfile);
1125 	(void) strlcat(tmpfile, _PATH_TMPFILE, sizeof (tmpfile));
1126 
1127 	tmpfd = mkstemp(tmpfile);
1128 	if (tmpfd == -1) {
1129 		(void) unlink(tmpfile);
1130 		return (Z_TEMP_FILE);
1131 	}
1132 	(void) close(tmpfd);
1133 
1134 	cvp.error = zonecfg_error_func;
1135 	cvp.warning = zonecfg_error_func;
1136 
1137 	/*
1138 	 * We do a final validation of the document.  Since the library has
1139 	 * malfunctioned if it fails to validate, we follow-up with an
1140 	 * assert() that the doc is valid.
1141 	 */
1142 	valid = xmlValidateDocument(&cvp, handle->zone_dh_doc);
1143 	assert(valid != 0);
1144 
1145 	if (xmlSaveFormatFile(tmpfile, handle->zone_dh_doc, 1) <= 0)
1146 		goto err;
1147 
1148 	(void) chmod(tmpfile, 0644);
1149 
1150 	/*
1151 	 * In the event we are doing a standard save, hard link a copy of the
1152 	 * original file in .backup.<pid>.filename so we can restore it if
1153 	 * something goes wrong.
1154 	 */
1155 	if (!is_new(handle) && !is_renaming(handle)) {
1156 		backup = B_TRUE;
1157 
1158 		(void) strlcpy(bakdir, filename, sizeof (bakdir));
1159 		(void) strlcpy(bakbase, filename, sizeof (bakbase));
1160 		(void) snprintf(bakfile, sizeof (bakfile), "%s/.backup.%d.%s",
1161 		    dirname(bakdir), getpid(), basename(bakbase));
1162 
1163 		if (link(filename, bakfile) == -1) {
1164 			err = errno;
1165 			(void) unlink(tmpfile);
1166 			if (errno == EACCES)
1167 				return (Z_ACCES);
1168 			return (Z_MISC_FS);
1169 		}
1170 	}
1171 
1172 	/*
1173 	 * Move the new document over top of the old.
1174 	 * i.e.:   zonecfg.XXXXXX  ->  myzone.xml
1175 	 */
1176 	if (rename(tmpfile, filename) == -1) {
1177 		err = errno;
1178 		(void) unlink(tmpfile);
1179 		if (backup)
1180 			(void) unlink(bakfile);
1181 		if (err == EACCES)
1182 			return (Z_ACCES);
1183 		return (Z_MISC_FS);
1184 	}
1185 
1186 	/*
1187 	 * If this is a snapshot, we're done-- don't add an index entry.
1188 	 */
1189 	if (is_snapshot(handle))
1190 		return (Z_OK);
1191 
1192 	/* now update the index file to reflect whatever we just did */
1193 	if ((err = zonecfg_refresh_index_file(handle)) != Z_OK) {
1194 		if (backup) {
1195 			/*
1196 			 * Try to restore from our backup.
1197 			 */
1198 			(void) unlink(filename);
1199 			(void) rename(bakfile, filename);
1200 		} else {
1201 			/*
1202 			 * Either the zone is new, in which case we can delete
1203 			 * new.xml, or we're doing a rename, so ditto.
1204 			 */
1205 			assert(is_new(handle) || is_renaming(handle));
1206 			(void) unlink(filename);
1207 		}
1208 		return (Z_UPDATING_INDEX);
1209 	}
1210 
1211 	if (backup)
1212 		(void) unlink(bakfile);
1213 
1214 	return (Z_OK);
1215 
1216 err:
1217 	(void) unlink(tmpfile);
1218 	return (Z_SAVING_FILE);
1219 }
1220 
1221 int
1222 zonecfg_save(zone_dochandle_t handle)
1223 {
1224 	char zname[ZONENAME_MAX], path[MAXPATHLEN];
1225 	char delpath[MAXPATHLEN];
1226 	int err = Z_SAVING_FILE;
1227 
1228 	if (zonecfg_check_handle(handle) != Z_OK)
1229 		return (Z_BAD_HANDLE);
1230 
1231 	/*
1232 	 * We don't support saving snapshots or a tree containing a sw
1233 	 * inventory at this time.
1234 	 */
1235 	if (handle->zone_dh_snapshot || handle->zone_dh_sw_inv)
1236 		return (Z_INVAL);
1237 
1238 	if ((err = zonecfg_get_name(handle, zname, sizeof (zname))) != Z_OK)
1239 		return (err);
1240 
1241 	if (!config_file_path(zname, path))
1242 		return (Z_MISC_FS);
1243 
1244 	addcomment(handle, "\n    DO NOT EDIT THIS "
1245 	    "FILE.  Use zonecfg(1M) instead.\n");
1246 
1247 	err = zonecfg_save_impl(handle, path);
1248 
1249 	stripcomments(handle);
1250 
1251 	if (err != Z_OK)
1252 		return (err);
1253 
1254 	handle->zone_dh_newzone = B_FALSE;
1255 
1256 	if (is_renaming(handle)) {
1257 		if (config_file_path(handle->zone_dh_delete_name, delpath))
1258 			(void) unlink(delpath);
1259 		handle->zone_dh_delete_name[0] = '\0';
1260 	}
1261 
1262 	return (Z_OK);
1263 }
1264 
1265 int
1266 zonecfg_verify_save(zone_dochandle_t handle, char *filename)
1267 {
1268 	int valid;
1269 
1270 	xmlValidCtxt cvp = { NULL };
1271 
1272 	if (zonecfg_check_handle(handle) != Z_OK)
1273 		return (Z_BAD_HANDLE);
1274 
1275 	cvp.error = zonecfg_error_func;
1276 	cvp.warning = zonecfg_error_func;
1277 
1278 	/*
1279 	 * We do a final validation of the document.  Since the library has
1280 	 * malfunctioned if it fails to validate, we follow-up with an
1281 	 * assert() that the doc is valid.
1282 	 */
1283 	valid = xmlValidateDocument(&cvp, handle->zone_dh_doc);
1284 	assert(valid != 0);
1285 
1286 	if (xmlSaveFormatFile(filename, handle->zone_dh_doc, 1) <= 0)
1287 		return (Z_SAVING_FILE);
1288 
1289 	return (Z_OK);
1290 }
1291 
1292 int
1293 zonecfg_detach_save(zone_dochandle_t handle, uint_t flags)
1294 {
1295 	char zname[ZONENAME_MAX];
1296 	char path[MAXPATHLEN];
1297 	char migpath[MAXPATHLEN];
1298 	xmlValidCtxt cvp = { NULL };
1299 	int err = Z_SAVING_FILE;
1300 	int valid;
1301 
1302 	if (zonecfg_check_handle(handle) != Z_OK)
1303 		return (Z_BAD_HANDLE);
1304 
1305 	if (flags & ZONE_DRY_RUN) {
1306 		(void) strlcpy(migpath, "-", sizeof (migpath));
1307 	} else {
1308 		if ((err = zonecfg_get_name(handle, zname, sizeof (zname)))
1309 		    != Z_OK)
1310 			return (err);
1311 
1312 		if ((err = zone_get_zonepath(zname, path, sizeof (path)))
1313 		    != Z_OK)
1314 			return (err);
1315 
1316 		if (snprintf(migpath, sizeof (migpath), "%s/%s", path,
1317 		    ZONE_DETACHED) >= sizeof (migpath))
1318 			return (Z_NOMEM);
1319 	}
1320 
1321 	if ((err = operation_prep(handle)) != Z_OK)
1322 		return (err);
1323 
1324 	addcomment(handle, "\n    DO NOT EDIT THIS FILE.  "
1325 	    "Use zonecfg(1M) and zoneadm(1M) attach.\n");
1326 
1327 	cvp.error = zonecfg_error_func;
1328 	cvp.warning = zonecfg_error_func;
1329 
1330 	/*
1331 	 * We do a final validation of the document.  Since the library has
1332 	 * malfunctioned if it fails to validate, we follow-up with an
1333 	 * assert() that the doc is valid.
1334 	 */
1335 	valid = xmlValidateDocument(&cvp, handle->zone_dh_doc);
1336 	assert(valid != 0);
1337 
1338 	if (xmlSaveFormatFile(migpath, handle->zone_dh_doc, 1) <= 0)
1339 		return (Z_SAVING_FILE);
1340 
1341 	if (!(flags & ZONE_DRY_RUN))
1342 		(void) chmod(migpath, 0644);
1343 
1344 	stripcomments(handle);
1345 
1346 	handle->zone_dh_newzone = B_FALSE;
1347 
1348 	return (Z_OK);
1349 }
1350 
1351 boolean_t
1352 zonecfg_detached(const char *path)
1353 {
1354 	char		migpath[MAXPATHLEN];
1355 	struct stat	buf;
1356 
1357 	if (snprintf(migpath, sizeof (migpath), "%s/%s", path, ZONE_DETACHED) >=
1358 	    sizeof (migpath))
1359 		return (B_FALSE);
1360 
1361 	if (stat(migpath, &buf) != -1)
1362 		return (B_TRUE);
1363 
1364 	return (B_FALSE);
1365 }
1366 
1367 void
1368 zonecfg_rm_detached(zone_dochandle_t handle, boolean_t forced)
1369 {
1370 	char zname[ZONENAME_MAX];
1371 	char path[MAXPATHLEN];
1372 	char detached[MAXPATHLEN];
1373 	char attached[MAXPATHLEN];
1374 
1375 	if (zonecfg_check_handle(handle) != Z_OK)
1376 		return;
1377 
1378 	if (zonecfg_get_name(handle, zname, sizeof (zname)) != Z_OK)
1379 		return;
1380 
1381 	if (zone_get_zonepath(zname, path, sizeof (path)) != Z_OK)
1382 		return;
1383 
1384 	(void) snprintf(detached, sizeof (detached), "%s/%s", path,
1385 	    ZONE_DETACHED);
1386 	(void) snprintf(attached, sizeof (attached), "%s/%s", path,
1387 	    ATTACH_FORCED);
1388 
1389 	if (forced) {
1390 		(void) rename(detached, attached);
1391 	} else {
1392 		(void) unlink(attached);
1393 		(void) unlink(detached);
1394 	}
1395 }
1396 
1397 /*
1398  * Special case: if access(2) fails with ENOENT, then try again using
1399  * ZONE_CONFIG_ROOT instead of config_file_path(zonename).  This is how we
1400  * work around the case of a config file which has not been created yet:
1401  * the user will need access to the directory so use that as a heuristic.
1402  */
1403 
1404 int
1405 zonecfg_access(const char *zonename, int amode)
1406 {
1407 	char path[MAXPATHLEN];
1408 
1409 	if (!config_file_path(zonename, path))
1410 		return (Z_INVAL);
1411 	if (access(path, amode) == 0)
1412 		return (Z_OK);
1413 	if (errno == ENOENT) {
1414 		if (snprintf(path, sizeof (path), "%s%s", zonecfg_root,
1415 		    ZONE_CONFIG_ROOT) >= sizeof (path))
1416 			return (Z_INVAL);
1417 		if (access(path, amode) == 0)
1418 			return (Z_OK);
1419 	}
1420 	if (errno == EACCES)
1421 		return (Z_ACCES);
1422 	if (errno == EINVAL)
1423 		return (Z_INVAL);
1424 	return (Z_MISC_FS);
1425 }
1426 
1427 int
1428 zonecfg_create_snapshot(const char *zonename)
1429 {
1430 	zone_dochandle_t handle;
1431 	char path[MAXPATHLEN], zonepath[MAXPATHLEN], rpath[MAXPATHLEN];
1432 	int error = Z_OK, res;
1433 
1434 	if ((handle = zonecfg_init_handle()) == NULL) {
1435 		return (Z_NOMEM);
1436 	}
1437 
1438 	handle->zone_dh_newzone = B_TRUE;
1439 	handle->zone_dh_snapshot = B_TRUE;
1440 
1441 	if ((error = zonecfg_get_handle(zonename, handle)) != Z_OK)
1442 		goto out;
1443 	if ((error = operation_prep(handle)) != Z_OK)
1444 		goto out;
1445 	error = zonecfg_get_zonepath(handle, zonepath, sizeof (zonepath));
1446 	if (error != Z_OK)
1447 		goto out;
1448 	if ((res = resolvepath(zonepath, rpath, sizeof (rpath))) == -1) {
1449 		error = Z_RESOLVED_PATH;
1450 		goto out;
1451 	}
1452 	/*
1453 	 * If the resolved path is not the same as the original path, then
1454 	 * save the resolved path in the snapshot, thus preventing any
1455 	 * potential problems down the line when zoneadmd goes to unmount
1456 	 * file systems and depends on initial string matches with resolved
1457 	 * paths.
1458 	 */
1459 	rpath[res] = '\0';
1460 	if (strcmp(zonepath, rpath) != 0) {
1461 		if ((error = zonecfg_set_zonepath(handle, rpath)) != Z_OK)
1462 			goto out;
1463 	}
1464 	if (snprintf(path, sizeof (path), "%s%s", zonecfg_root,
1465 	    ZONE_SNAPSHOT_ROOT) >= sizeof (path)) {
1466 		error = Z_MISC_FS;
1467 		goto out;
1468 	}
1469 	if ((mkdir(path, S_IRWXU) == -1) && (errno != EEXIST)) {
1470 		error = Z_MISC_FS;
1471 		goto out;
1472 	}
1473 
1474 	if (!snap_file_path(zonename, path)) {
1475 		error = Z_MISC_FS;
1476 		goto out;
1477 	}
1478 
1479 	addcomment(handle, "\n    DO NOT EDIT THIS FILE.  "
1480 	    "It is a snapshot of running zone state.\n");
1481 
1482 	error = zonecfg_save_impl(handle, path);
1483 
1484 	stripcomments(handle);
1485 
1486 out:
1487 	zonecfg_fini_handle(handle);
1488 	return (error);
1489 }
1490 
1491 int
1492 zonecfg_get_iptype(zone_dochandle_t handle, zone_iptype_t *iptypep)
1493 {
1494 	char property[10]; /* 10 is big enough for "shared"/"exclusive" */
1495 	int err;
1496 
1497 	err = getrootattr(handle, DTD_ATTR_IPTYPE, property, sizeof (property));
1498 	if (err == Z_BAD_PROPERTY) {
1499 		/* Return default value */
1500 		*iptypep = ZS_SHARED;
1501 		return (Z_OK);
1502 	} else if (err != Z_OK) {
1503 		return (err);
1504 	}
1505 
1506 	if (strlen(property) == 0 ||
1507 	    strcmp(property, "shared") == 0)
1508 		*iptypep = ZS_SHARED;
1509 	else if (strcmp(property, "exclusive") == 0)
1510 		*iptypep = ZS_EXCLUSIVE;
1511 	else
1512 		return (Z_INVAL);
1513 
1514 	return (Z_OK);
1515 }
1516 
1517 int
1518 zonecfg_set_iptype(zone_dochandle_t handle, zone_iptype_t iptype)
1519 {
1520 	xmlNodePtr cur;
1521 
1522 	if (handle == NULL)
1523 		return (Z_INVAL);
1524 
1525 	cur = xmlDocGetRootElement(handle->zone_dh_doc);
1526 	if (cur == NULL) {
1527 		return (Z_EMPTY_DOCUMENT);
1528 	}
1529 
1530 	if (xmlStrcmp(cur->name, DTD_ELEM_ZONE) != 0) {
1531 		return (Z_WRONG_DOC_TYPE);
1532 	}
1533 	switch (iptype) {
1534 	case ZS_SHARED:
1535 		/*
1536 		 * Since "shared" is the default, we don't write it to the
1537 		 * configuration file, so that it's easier to migrate those
1538 		 * zones elsewhere, eg., to systems which are not IP-Instances
1539 		 * aware.
1540 		 * xmlUnsetProp only fails when the attribute doesn't exist,
1541 		 * which we don't care.
1542 		 */
1543 		(void) xmlUnsetProp(cur, DTD_ATTR_IPTYPE);
1544 		break;
1545 	case ZS_EXCLUSIVE:
1546 		if (xmlSetProp(cur, DTD_ATTR_IPTYPE,
1547 		    (const xmlChar *) "exclusive") == NULL)
1548 			return (Z_INVAL);
1549 		break;
1550 	}
1551 	return (Z_OK);
1552 }
1553 
1554 static int
1555 newprop(xmlNodePtr node, const xmlChar *attrname, char *src)
1556 {
1557 	xmlAttrPtr newattr;
1558 
1559 	newattr = xmlNewProp(node, attrname, (xmlChar *)src);
1560 	if (newattr == NULL) {
1561 		xmlUnlinkNode(node);
1562 		xmlFreeNode(node);
1563 		return (Z_BAD_PROPERTY);
1564 	}
1565 	return (Z_OK);
1566 }
1567 
1568 static int
1569 zonecfg_add_filesystem_core(zone_dochandle_t handle, struct zone_fstab *tabptr)
1570 {
1571 	xmlNodePtr newnode, cur = handle->zone_dh_cur, options_node;
1572 	zone_fsopt_t *ptr;
1573 	int err;
1574 
1575 	newnode = xmlNewTextChild(cur, NULL, DTD_ELEM_FS, NULL);
1576 	if ((err = newprop(newnode, DTD_ATTR_SPECIAL,
1577 	    tabptr->zone_fs_special)) != Z_OK)
1578 		return (err);
1579 	if (tabptr->zone_fs_raw[0] != '\0' &&
1580 	    (err = newprop(newnode, DTD_ATTR_RAW, tabptr->zone_fs_raw)) != Z_OK)
1581 		return (err);
1582 	if ((err = newprop(newnode, DTD_ATTR_DIR, tabptr->zone_fs_dir)) != Z_OK)
1583 		return (err);
1584 	if ((err = newprop(newnode, DTD_ATTR_TYPE,
1585 	    tabptr->zone_fs_type)) != Z_OK)
1586 		return (err);
1587 	if (tabptr->zone_fs_options != NULL) {
1588 		for (ptr = tabptr->zone_fs_options; ptr != NULL;
1589 		    ptr = ptr->zone_fsopt_next) {
1590 			options_node = xmlNewTextChild(newnode, NULL,
1591 			    DTD_ELEM_FSOPTION, NULL);
1592 			if ((err = newprop(options_node, DTD_ATTR_NAME,
1593 			    ptr->zone_fsopt_opt)) != Z_OK)
1594 				return (err);
1595 		}
1596 	}
1597 	return (Z_OK);
1598 }
1599 
1600 int
1601 zonecfg_add_filesystem(zone_dochandle_t handle, struct zone_fstab *tabptr)
1602 {
1603 	int err;
1604 
1605 	if (tabptr == NULL)
1606 		return (Z_INVAL);
1607 
1608 	if ((err = operation_prep(handle)) != Z_OK)
1609 		return (err);
1610 
1611 	if ((err = zonecfg_add_filesystem_core(handle, tabptr)) != Z_OK)
1612 		return (err);
1613 
1614 	return (Z_OK);
1615 }
1616 
1617 static int
1618 zonecfg_add_ipd_core(zone_dochandle_t handle, struct zone_fstab *tabptr)
1619 {
1620 	xmlNodePtr newnode, cur = handle->zone_dh_cur;
1621 	int err;
1622 
1623 	newnode = xmlNewTextChild(cur, NULL, DTD_ELEM_IPD, NULL);
1624 	if ((err = newprop(newnode, DTD_ATTR_DIR, tabptr->zone_fs_dir)) != Z_OK)
1625 		return (err);
1626 	return (Z_OK);
1627 }
1628 
1629 int
1630 zonecfg_add_ipd(zone_dochandle_t handle, struct zone_fstab *tabptr)
1631 {
1632 	int err;
1633 
1634 	if (tabptr == NULL)
1635 		return (Z_INVAL);
1636 
1637 	if ((err = operation_prep(handle)) != Z_OK)
1638 		return (err);
1639 
1640 	if ((err = zonecfg_add_ipd_core(handle, tabptr)) != Z_OK)
1641 		return (err);
1642 
1643 	return (Z_OK);
1644 }
1645 
1646 int
1647 zonecfg_add_fs_option(struct zone_fstab *tabptr, char *option)
1648 {
1649 	zone_fsopt_t *last, *old, *new;
1650 
1651 	last = tabptr->zone_fs_options;
1652 	for (old = last; old != NULL; old = old->zone_fsopt_next)
1653 		last = old;	/* walk to the end of the list */
1654 	new = (zone_fsopt_t *)malloc(sizeof (zone_fsopt_t));
1655 	if (new == NULL)
1656 		return (Z_NOMEM);
1657 	(void) strlcpy(new->zone_fsopt_opt, option,
1658 	    sizeof (new->zone_fsopt_opt));
1659 	new->zone_fsopt_next = NULL;
1660 	if (last == NULL)
1661 		tabptr->zone_fs_options = new;
1662 	else
1663 		last->zone_fsopt_next = new;
1664 	return (Z_OK);
1665 }
1666 
1667 int
1668 zonecfg_remove_fs_option(struct zone_fstab *tabptr, char *option)
1669 {
1670 	zone_fsopt_t *last, *this, *next;
1671 
1672 	last = tabptr->zone_fs_options;
1673 	for (this = last; this != NULL; this = this->zone_fsopt_next) {
1674 		if (strcmp(this->zone_fsopt_opt, option) == 0) {
1675 			next = this->zone_fsopt_next;
1676 			if (this == tabptr->zone_fs_options)
1677 				tabptr->zone_fs_options = next;
1678 			else
1679 				last->zone_fsopt_next = next;
1680 			free(this);
1681 			return (Z_OK);
1682 		} else
1683 			last = this;
1684 	}
1685 	return (Z_NO_PROPERTY_ID);
1686 }
1687 
1688 void
1689 zonecfg_free_fs_option_list(zone_fsopt_t *list)
1690 {
1691 	zone_fsopt_t *this, *next;
1692 
1693 	for (this = list; this != NULL; this = next) {
1694 		next = this->zone_fsopt_next;
1695 		free(this);
1696 	}
1697 }
1698 
1699 void
1700 zonecfg_free_rctl_value_list(struct zone_rctlvaltab *valtab)
1701 {
1702 	if (valtab == NULL)
1703 		return;
1704 	zonecfg_free_rctl_value_list(valtab->zone_rctlval_next);
1705 	free(valtab);
1706 }
1707 
1708 static boolean_t
1709 match_prop(xmlNodePtr cur, const xmlChar *attr, char *user_prop)
1710 {
1711 	xmlChar *gotten_prop;
1712 	int prop_result;
1713 
1714 	gotten_prop = xmlGetProp(cur, attr);
1715 	if (gotten_prop == NULL)	/* shouldn't happen */
1716 		return (B_FALSE);
1717 	prop_result = xmlStrcmp(gotten_prop, (const xmlChar *) user_prop);
1718 	xmlFree(gotten_prop);
1719 	return ((prop_result == 0));
1720 }
1721 
1722 static int
1723 zonecfg_delete_filesystem_core(zone_dochandle_t handle,
1724     struct zone_fstab *tabptr)
1725 {
1726 	xmlNodePtr cur = handle->zone_dh_cur;
1727 	boolean_t dir_match, spec_match, raw_match, type_match;
1728 
1729 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
1730 		if (xmlStrcmp(cur->name, DTD_ELEM_FS))
1731 			continue;
1732 		dir_match = match_prop(cur, DTD_ATTR_DIR, tabptr->zone_fs_dir);
1733 		spec_match = match_prop(cur, DTD_ATTR_SPECIAL,
1734 		    tabptr->zone_fs_special);
1735 		raw_match = match_prop(cur, DTD_ATTR_RAW,
1736 		    tabptr->zone_fs_raw);
1737 		type_match = match_prop(cur, DTD_ATTR_TYPE,
1738 		    tabptr->zone_fs_type);
1739 		if (dir_match && spec_match && raw_match && type_match) {
1740 			xmlUnlinkNode(cur);
1741 			xmlFreeNode(cur);
1742 			return (Z_OK);
1743 		}
1744 	}
1745 	return (Z_NO_RESOURCE_ID);
1746 }
1747 
1748 int
1749 zonecfg_delete_filesystem(zone_dochandle_t handle, struct zone_fstab *tabptr)
1750 {
1751 	int err;
1752 
1753 	if (tabptr == NULL)
1754 		return (Z_INVAL);
1755 
1756 	if ((err = operation_prep(handle)) != Z_OK)
1757 		return (err);
1758 
1759 	if ((err = zonecfg_delete_filesystem_core(handle, tabptr)) != Z_OK)
1760 		return (err);
1761 
1762 	return (Z_OK);
1763 }
1764 
1765 int
1766 zonecfg_modify_filesystem(
1767 	zone_dochandle_t handle,
1768 	struct zone_fstab *oldtabptr,
1769 	struct zone_fstab *newtabptr)
1770 {
1771 	int err;
1772 
1773 	if (oldtabptr == NULL || newtabptr == NULL)
1774 		return (Z_INVAL);
1775 
1776 	if ((err = operation_prep(handle)) != Z_OK)
1777 		return (err);
1778 
1779 	if ((err = zonecfg_delete_filesystem_core(handle, oldtabptr)) != Z_OK)
1780 		return (err);
1781 
1782 	if ((err = zonecfg_add_filesystem_core(handle, newtabptr)) != Z_OK)
1783 		return (err);
1784 
1785 	return (Z_OK);
1786 }
1787 
1788 static int
1789 zonecfg_delete_ipd_core(zone_dochandle_t handle, struct zone_fstab *tabptr)
1790 {
1791 	xmlNodePtr cur = handle->zone_dh_cur;
1792 
1793 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
1794 		if (xmlStrcmp(cur->name, DTD_ELEM_IPD))
1795 			continue;
1796 		if (match_prop(cur, DTD_ATTR_DIR, tabptr->zone_fs_dir)) {
1797 			xmlUnlinkNode(cur);
1798 			xmlFreeNode(cur);
1799 			return (Z_OK);
1800 		}
1801 	}
1802 	return (Z_NO_RESOURCE_ID);
1803 }
1804 
1805 int
1806 zonecfg_delete_ipd(zone_dochandle_t handle, struct zone_fstab *tabptr)
1807 {
1808 	int err;
1809 
1810 	if (tabptr == NULL)
1811 		return (Z_INVAL);
1812 
1813 	if ((err = operation_prep(handle)) != Z_OK)
1814 		return (err);
1815 
1816 	if ((err = zonecfg_delete_ipd_core(handle, tabptr)) != Z_OK)
1817 		return (err);
1818 
1819 	return (Z_OK);
1820 }
1821 
1822 int
1823 zonecfg_modify_ipd(zone_dochandle_t handle, struct zone_fstab *oldtabptr,
1824     struct zone_fstab *newtabptr)
1825 {
1826 	int err;
1827 
1828 	if (oldtabptr == NULL || newtabptr == NULL)
1829 		return (Z_INVAL);
1830 
1831 	if ((err = operation_prep(handle)) != Z_OK)
1832 		return (err);
1833 
1834 	if ((err = zonecfg_delete_ipd_core(handle, oldtabptr)) != Z_OK)
1835 		return (err);
1836 
1837 	if ((err = zonecfg_add_ipd_core(handle, newtabptr)) != Z_OK)
1838 		return (err);
1839 
1840 	return (Z_OK);
1841 }
1842 
1843 int
1844 zonecfg_lookup_filesystem(
1845 	zone_dochandle_t handle,
1846 	struct zone_fstab *tabptr)
1847 {
1848 	xmlNodePtr cur, options, firstmatch;
1849 	int err;
1850 	char dirname[MAXPATHLEN], special[MAXPATHLEN], raw[MAXPATHLEN];
1851 	char type[FSTYPSZ];
1852 	char options_str[MAX_MNTOPT_STR];
1853 
1854 	if (tabptr == NULL)
1855 		return (Z_INVAL);
1856 
1857 	if ((err = operation_prep(handle)) != Z_OK)
1858 		return (err);
1859 
1860 	/*
1861 	 * Walk the list of children looking for matches on any properties
1862 	 * specified in the fstab parameter.  If more than one resource
1863 	 * matches, we return Z_INSUFFICIENT_SPEC; if none match, we return
1864 	 * Z_NO_RESOURCE_ID.
1865 	 */
1866 	cur = handle->zone_dh_cur;
1867 	firstmatch = NULL;
1868 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
1869 		if (xmlStrcmp(cur->name, DTD_ELEM_FS))
1870 			continue;
1871 		if (strlen(tabptr->zone_fs_dir) > 0) {
1872 			if ((fetchprop(cur, DTD_ATTR_DIR, dirname,
1873 			    sizeof (dirname)) == Z_OK) &&
1874 			    (strcmp(tabptr->zone_fs_dir, dirname) == 0)) {
1875 				if (firstmatch == NULL)
1876 					firstmatch = cur;
1877 				else
1878 					return (Z_INSUFFICIENT_SPEC);
1879 			}
1880 		}
1881 		if (strlen(tabptr->zone_fs_special) > 0) {
1882 			if ((fetchprop(cur, DTD_ATTR_SPECIAL, special,
1883 			    sizeof (special)) == Z_OK)) {
1884 				if (strcmp(tabptr->zone_fs_special,
1885 				    special) == 0) {
1886 					if (firstmatch == NULL)
1887 						firstmatch = cur;
1888 					else if (firstmatch != cur)
1889 						return (Z_INSUFFICIENT_SPEC);
1890 				} else {
1891 					/*
1892 					 * If another property matched but this
1893 					 * one doesn't then reset firstmatch.
1894 					 */
1895 					if (firstmatch == cur)
1896 						firstmatch = NULL;
1897 				}
1898 			}
1899 		}
1900 		if (strlen(tabptr->zone_fs_raw) > 0) {
1901 			if ((fetchprop(cur, DTD_ATTR_RAW, raw,
1902 			    sizeof (raw)) == Z_OK)) {
1903 				if (strcmp(tabptr->zone_fs_raw, raw) == 0) {
1904 					if (firstmatch == NULL)
1905 						firstmatch = cur;
1906 					else if (firstmatch != cur)
1907 						return (Z_INSUFFICIENT_SPEC);
1908 				} else {
1909 					/*
1910 					 * If another property matched but this
1911 					 * one doesn't then reset firstmatch.
1912 					 */
1913 					if (firstmatch == cur)
1914 						firstmatch = NULL;
1915 				}
1916 			}
1917 		}
1918 		if (strlen(tabptr->zone_fs_type) > 0) {
1919 			if ((fetchprop(cur, DTD_ATTR_TYPE, type,
1920 			    sizeof (type)) == Z_OK)) {
1921 				if (strcmp(tabptr->zone_fs_type, type) == 0) {
1922 					if (firstmatch == NULL)
1923 						firstmatch = cur;
1924 					else if (firstmatch != cur)
1925 						return (Z_INSUFFICIENT_SPEC);
1926 				} else {
1927 					/*
1928 					 * If another property matched but this
1929 					 * one doesn't then reset firstmatch.
1930 					 */
1931 					if (firstmatch == cur)
1932 						firstmatch = NULL;
1933 				}
1934 			}
1935 		}
1936 	}
1937 
1938 	if (firstmatch == NULL)
1939 		return (Z_NO_RESOURCE_ID);
1940 
1941 	cur = firstmatch;
1942 
1943 	if ((err = fetchprop(cur, DTD_ATTR_DIR, tabptr->zone_fs_dir,
1944 	    sizeof (tabptr->zone_fs_dir))) != Z_OK)
1945 		return (err);
1946 
1947 	if ((err = fetchprop(cur, DTD_ATTR_SPECIAL, tabptr->zone_fs_special,
1948 	    sizeof (tabptr->zone_fs_special))) != Z_OK)
1949 		return (err);
1950 
1951 	if ((err = fetchprop(cur, DTD_ATTR_RAW, tabptr->zone_fs_raw,
1952 	    sizeof (tabptr->zone_fs_raw))) != Z_OK)
1953 		return (err);
1954 
1955 	if ((err = fetchprop(cur, DTD_ATTR_TYPE, tabptr->zone_fs_type,
1956 	    sizeof (tabptr->zone_fs_type))) != Z_OK)
1957 		return (err);
1958 
1959 	/* options are optional */
1960 	tabptr->zone_fs_options = NULL;
1961 	for (options = cur->xmlChildrenNode; options != NULL;
1962 	    options = options->next) {
1963 		if ((fetchprop(options, DTD_ATTR_NAME, options_str,
1964 		    sizeof (options_str)) != Z_OK))
1965 			break;
1966 		if (zonecfg_add_fs_option(tabptr, options_str) != Z_OK)
1967 			break;
1968 	}
1969 	return (Z_OK);
1970 }
1971 
1972 int
1973 zonecfg_lookup_ipd(zone_dochandle_t handle, struct zone_fstab *tabptr)
1974 {
1975 	xmlNodePtr cur, match;
1976 	int err;
1977 	char dirname[MAXPATHLEN];
1978 
1979 	if (tabptr == NULL)
1980 		return (Z_INVAL);
1981 
1982 	if ((err = operation_prep(handle)) != Z_OK)
1983 		return (err);
1984 
1985 	/*
1986 	 * General algorithm:
1987 	 * Walk the list of children looking for matches on any properties
1988 	 * specified in the fstab parameter.  If more than one resource
1989 	 * matches, we return Z_INSUFFICIENT_SPEC; if none match, we return
1990 	 * Z_NO_RESOURCE_ID.
1991 	 */
1992 	cur = handle->zone_dh_cur;
1993 	match = NULL;
1994 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
1995 		if (xmlStrcmp(cur->name, DTD_ELEM_IPD))
1996 			continue;
1997 		if (strlen(tabptr->zone_fs_dir) > 0) {
1998 			if ((fetchprop(cur, DTD_ATTR_DIR, dirname,
1999 			    sizeof (dirname)) == Z_OK) &&
2000 			    (strcmp(tabptr->zone_fs_dir, dirname) == 0)) {
2001 				if (match == NULL)
2002 					match = cur;
2003 				else
2004 					return (Z_INSUFFICIENT_SPEC);
2005 			}
2006 		}
2007 	}
2008 
2009 	if (match == NULL)
2010 		return (Z_NO_RESOURCE_ID);
2011 
2012 	cur = match;
2013 
2014 	if ((err = fetchprop(cur, DTD_ATTR_DIR, tabptr->zone_fs_dir,
2015 	    sizeof (tabptr->zone_fs_dir))) != Z_OK)
2016 		return (err);
2017 
2018 	return (Z_OK);
2019 }
2020 
2021 /*
2022  * Compare two IP addresses in string form.  Allow for the possibility that
2023  * one might have "/<prefix-length>" at the end: allow a match on just the
2024  * IP address (or host name) part.
2025  */
2026 
2027 boolean_t
2028 zonecfg_same_net_address(char *a1, char *a2)
2029 {
2030 	char *slashp, *slashp1, *slashp2;
2031 	int result;
2032 
2033 	if (strcmp(a1, a2) == 0)
2034 		return (B_TRUE);
2035 
2036 	/*
2037 	 * If neither has a slash or both do, they need to match to be
2038 	 * considered the same, but they did not match above, so fail.
2039 	 */
2040 	slashp1 = strchr(a1, '/');
2041 	slashp2 = strchr(a2, '/');
2042 	if ((slashp1 == NULL && slashp2 == NULL) ||
2043 	    (slashp1 != NULL && slashp2 != NULL))
2044 		return (B_FALSE);
2045 
2046 	/*
2047 	 * Only one had a slash: pick that one, zero out the slash, compare
2048 	 * the "address only" strings, restore the slash, and return the
2049 	 * result of the comparison.
2050 	 */
2051 	slashp = (slashp1 == NULL) ? slashp2 : slashp1;
2052 	*slashp = '\0';
2053 	result = strcmp(a1, a2);
2054 	*slashp = '/';
2055 	return ((result == 0));
2056 }
2057 
2058 int
2059 zonecfg_valid_net_address(char *address, struct lifreq *lifr)
2060 {
2061 	struct sockaddr_in *sin4;
2062 	struct sockaddr_in6 *sin6;
2063 	struct addrinfo hints, *result;
2064 	char *slashp = strchr(address, '/');
2065 
2066 	bzero(lifr, sizeof (struct lifreq));
2067 	sin4 = (struct sockaddr_in *)&lifr->lifr_addr;
2068 	sin6 = (struct sockaddr_in6 *)&lifr->lifr_addr;
2069 	if (slashp != NULL)
2070 		*slashp = '\0';
2071 	if (inet_pton(AF_INET, address, &sin4->sin_addr) == 1) {
2072 		sin4->sin_family = AF_INET;
2073 	} else if (inet_pton(AF_INET6, address, &sin6->sin6_addr) == 1) {
2074 		if (slashp == NULL)
2075 			return (Z_IPV6_ADDR_PREFIX_LEN);
2076 		sin6->sin6_family = AF_INET6;
2077 	} else {
2078 		/* "address" may be a host name */
2079 		(void) memset(&hints, 0, sizeof (hints));
2080 		hints.ai_family = PF_INET;
2081 		if (getaddrinfo(address, NULL, &hints, &result) != 0)
2082 			return (Z_BOGUS_ADDRESS);
2083 		sin4->sin_family = result->ai_family;
2084 
2085 		(void) memcpy(&sin4->sin_addr,
2086 		    /* LINTED E_BAD_PTR_CAST_ALIGN */
2087 		    &((struct sockaddr_in *)result->ai_addr)->sin_addr,
2088 		    sizeof (struct in_addr));
2089 
2090 		freeaddrinfo(result);
2091 	}
2092 	return (Z_OK);
2093 }
2094 
2095 boolean_t
2096 zonecfg_ifname_exists(sa_family_t af, char *ifname)
2097 {
2098 	struct lifreq lifr;
2099 	int so;
2100 	int save_errno;
2101 
2102 	(void) memset(&lifr, 0, sizeof (lifr));
2103 	(void) strlcpy(lifr.lifr_name, ifname, sizeof (lifr.lifr_name));
2104 	lifr.lifr_addr.ss_family = af;
2105 	if ((so = socket(af, SOCK_DGRAM, 0)) < 0) {
2106 		/* Odd - can't tell if the ifname exists */
2107 		return (B_FALSE);
2108 	}
2109 	if (ioctl(so, SIOCGLIFFLAGS, (caddr_t)&lifr) < 0) {
2110 		save_errno = errno;
2111 		(void) close(so);
2112 		errno = save_errno;
2113 		return (B_FALSE);
2114 	}
2115 	(void) close(so);
2116 	return (B_TRUE);
2117 }
2118 
2119 /*
2120  * Determines if the physical interface and IP address specified by 'tabptr'
2121  * are in the zone document to which 'handle' refers.  'tabptr' must have an
2122  * interface or an address or both.  If it contains both, then Z_OK is
2123  * returned iff there is exactly one match.  If it contains an interface
2124  * or an address, but not both, then Z_OK is returned iff there is exactly
2125  * one entry with that interface or address.  If there are multiple entries
2126  * matching the query, then Z_INSUFFICIENT_SPEC is returned.  If there
2127  * are no matches, then Z_NO_RESOURCE_ID is returned.
2128  *
2129  * Errors might also be returned if the entry that exactly matches the
2130  * query lacks critical network resource information.
2131  *
2132  * If there is a single exact match, then the matching entry's physical
2133  * interface, IP address, and router information is stored in 'tabptr'.
2134  */
2135 int
2136 zonecfg_lookup_nwif(zone_dochandle_t handle, struct zone_nwiftab *tabptr)
2137 {
2138 	xmlNodePtr cur;
2139 	xmlNodePtr firstmatch;
2140 	boolean_t physfound;
2141 	int err;
2142 	char address[INET6_ADDRSTRLEN];
2143 	char physical[LIFNAMSIZ];
2144 	size_t addrspec;		/* nonzero if tabptr has IP addr */
2145 	size_t physspec;		/* nonzero if tabptr has interface */
2146 
2147 	if (tabptr == NULL)
2148 		return (Z_INVAL);
2149 
2150 	/*
2151 	 * zone_nwif_address and zone_nwif_physical are arrays, so no NULL
2152 	 * checks are necessary.
2153 	 */
2154 	addrspec = strlen(tabptr->zone_nwif_address);
2155 	physspec = strlen(tabptr->zone_nwif_physical);
2156 	assert(addrspec > 0 || physspec > 0);
2157 
2158 	if ((err = operation_prep(handle)) != Z_OK)
2159 		return (err);
2160 
2161 	firstmatch = NULL;
2162 	cur = handle->zone_dh_cur;
2163 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
2164 		/* Skip non-net elements */
2165 		if (xmlStrcmp(cur->name, DTD_ELEM_NET))
2166 			continue;
2167 
2168 		/*
2169 		 * If an interface is specified, then first check if the current
2170 		 * element's interface matches the query's interface.
2171 		 */
2172 		if (physspec > 0) {
2173 			physfound = B_FALSE;
2174 			if ((fetchprop(cur, DTD_ATTR_PHYSICAL, physical,
2175 			    sizeof (physical)) == Z_OK) &&
2176 			    (strcmp(tabptr->zone_nwif_physical,
2177 			    physical) == 0)) {
2178 				if (addrspec == 0) {
2179 					if (firstmatch == NULL)
2180 						firstmatch = cur;
2181 					else
2182 						return (Z_INSUFFICIENT_SPEC);
2183 				} else {
2184 					/*
2185 					 * We're also matching based on IP
2186 					 * address, so we can't say that the
2187 					 * current element matches the query
2188 					 * yet.  Indicate that the interfaces
2189 					 * match.
2190 					 */
2191 					physfound = B_TRUE;
2192 				}
2193 			}
2194 		}
2195 		if (addrspec > 0) {
2196 			if ((fetchprop(cur, DTD_ATTR_ADDRESS, address,
2197 			    sizeof (address)) == Z_OK) &&
2198 			    (zonecfg_same_net_address(
2199 			    tabptr->zone_nwif_address, address))) {
2200 				if (physspec == 0) {
2201 					/* We're only matching IP addresses. */
2202 					if (firstmatch == NULL)
2203 						firstmatch = cur;
2204 					else
2205 						return (Z_INSUFFICIENT_SPEC);
2206 				} else if (physfound) {
2207 					/*
2208 					 * Both the interfaces and the addresses
2209 					 * match.
2210 					 */
2211 					if (firstmatch == NULL)
2212 						firstmatch = cur;
2213 					else
2214 						return (Z_INSUFFICIENT_SPEC);
2215 				}
2216 			}
2217 		}
2218 	}
2219 	if (firstmatch == NULL)
2220 		return (Z_NO_RESOURCE_ID);
2221 
2222 	cur = firstmatch;
2223 
2224 	if ((err = fetchprop(cur, DTD_ATTR_PHYSICAL, tabptr->zone_nwif_physical,
2225 	    sizeof (tabptr->zone_nwif_physical))) != Z_OK)
2226 		return (err);
2227 
2228 	if ((err = fetchprop(cur, DTD_ATTR_ADDRESS, tabptr->zone_nwif_address,
2229 	    sizeof (tabptr->zone_nwif_address))) != Z_OK)
2230 		return (err);
2231 
2232 	if ((err = fetchprop(cur, DTD_ATTR_DEFROUTER,
2233 	    tabptr->zone_nwif_defrouter,
2234 	    sizeof (tabptr->zone_nwif_defrouter))) != Z_OK)
2235 		return (err);
2236 
2237 	return (Z_OK);
2238 }
2239 
2240 static int
2241 zonecfg_add_nwif_core(zone_dochandle_t handle, struct zone_nwiftab *tabptr)
2242 {
2243 	xmlNodePtr newnode, cur = handle->zone_dh_cur;
2244 	int err;
2245 
2246 	newnode = xmlNewTextChild(cur, NULL, DTD_ELEM_NET, NULL);
2247 	if ((err = newprop(newnode, DTD_ATTR_ADDRESS,
2248 	    tabptr->zone_nwif_address)) != Z_OK)
2249 		return (err);
2250 	if ((err = newprop(newnode, DTD_ATTR_PHYSICAL,
2251 	    tabptr->zone_nwif_physical)) != Z_OK)
2252 		return (err);
2253 	/*
2254 	 * Do not add this property when it is not set, for backwards
2255 	 * compatibility and because it is optional.
2256 	 */
2257 	if ((strlen(tabptr->zone_nwif_defrouter) > 0) &&
2258 	    ((err = newprop(newnode, DTD_ATTR_DEFROUTER,
2259 	    tabptr->zone_nwif_defrouter)) != Z_OK))
2260 		return (err);
2261 	return (Z_OK);
2262 }
2263 
2264 int
2265 zonecfg_add_nwif(zone_dochandle_t handle, struct zone_nwiftab *tabptr)
2266 {
2267 	int err;
2268 
2269 	if (tabptr == NULL)
2270 		return (Z_INVAL);
2271 
2272 	if ((err = operation_prep(handle)) != Z_OK)
2273 		return (err);
2274 
2275 	if ((err = zonecfg_add_nwif_core(handle, tabptr)) != Z_OK)
2276 		return (err);
2277 
2278 	return (Z_OK);
2279 }
2280 
2281 static int
2282 zonecfg_delete_nwif_core(zone_dochandle_t handle, struct zone_nwiftab *tabptr)
2283 {
2284 	xmlNodePtr cur = handle->zone_dh_cur;
2285 	boolean_t addr_match, phys_match;
2286 
2287 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
2288 		if (xmlStrcmp(cur->name, DTD_ELEM_NET))
2289 			continue;
2290 
2291 		addr_match = match_prop(cur, DTD_ATTR_ADDRESS,
2292 		    tabptr->zone_nwif_address);
2293 		phys_match = match_prop(cur, DTD_ATTR_PHYSICAL,
2294 		    tabptr->zone_nwif_physical);
2295 
2296 		if (addr_match && phys_match) {
2297 			xmlUnlinkNode(cur);
2298 			xmlFreeNode(cur);
2299 			return (Z_OK);
2300 		}
2301 	}
2302 	return (Z_NO_RESOURCE_ID);
2303 }
2304 
2305 int
2306 zonecfg_delete_nwif(zone_dochandle_t handle, struct zone_nwiftab *tabptr)
2307 {
2308 	int err;
2309 
2310 	if (tabptr == NULL)
2311 		return (Z_INVAL);
2312 
2313 	if ((err = operation_prep(handle)) != Z_OK)
2314 		return (err);
2315 
2316 	if ((err = zonecfg_delete_nwif_core(handle, tabptr)) != Z_OK)
2317 		return (err);
2318 
2319 	return (Z_OK);
2320 }
2321 
2322 int
2323 zonecfg_modify_nwif(
2324 	zone_dochandle_t handle,
2325 	struct zone_nwiftab *oldtabptr,
2326 	struct zone_nwiftab *newtabptr)
2327 {
2328 	int err;
2329 
2330 	if (oldtabptr == NULL || newtabptr == NULL)
2331 		return (Z_INVAL);
2332 
2333 	if ((err = operation_prep(handle)) != Z_OK)
2334 		return (err);
2335 
2336 	if ((err = zonecfg_delete_nwif_core(handle, oldtabptr)) != Z_OK)
2337 		return (err);
2338 
2339 	if ((err = zonecfg_add_nwif_core(handle, newtabptr)) != Z_OK)
2340 		return (err);
2341 
2342 	return (Z_OK);
2343 }
2344 
2345 /*
2346  * Gets the zone hostid string stored in the specified zone configuration
2347  * document.  This function returns Z_OK on success.  Z_BAD_PROPERTY is returned
2348  * if the config file doesn't specify a hostid or if the hostid is blank.
2349  *
2350  * Note that buflen should be at least HW_HOSTID_LEN.
2351  */
2352 int
2353 zonecfg_get_hostid(zone_dochandle_t handle, char *bufp, size_t buflen)
2354 {
2355 	int err;
2356 
2357 	if ((err = getrootattr(handle, DTD_ATTR_HOSTID, bufp, buflen)) != Z_OK)
2358 		return (err);
2359 	if (bufp[0] == '\0')
2360 		return (Z_BAD_PROPERTY);
2361 	return (Z_OK);
2362 }
2363 
2364 /*
2365  * Sets the hostid string in the specified zone config document to the given
2366  * string value.  If 'hostidp' is NULL, then the config document's hostid
2367  * attribute is cleared.  Non-NULL hostids are validated.  This function returns
2368  * Z_OK on success.  Any other return value indicates failure.
2369  */
2370 int
2371 zonecfg_set_hostid(zone_dochandle_t handle, const char *hostidp)
2372 {
2373 	int err;
2374 
2375 	/*
2376 	 * A NULL hostid string is interpreted as a request to clear the
2377 	 * hostid.
2378 	 */
2379 	if (hostidp == NULL || (err = zonecfg_valid_hostid(hostidp)) == Z_OK)
2380 		return (setrootattr(handle, DTD_ATTR_HOSTID, hostidp));
2381 	return (err);
2382 }
2383 
2384 /*
2385  * Determines if the specified string is a valid hostid string.  This function
2386  * returns Z_OK if the string is a valid hostid string.  It returns Z_INVAL if
2387  * 'hostidp' is NULL, Z_TOO_BIG if 'hostidp' refers to a string buffer
2388  * containing a hex string with more than 8 digits, and Z_HOSTID_FUBAR if the
2389  * string has an invalid format.
2390  */
2391 int
2392 zonecfg_valid_hostid(const char *hostidp)
2393 {
2394 	char *currentp;
2395 	u_longlong_t hostidval;
2396 	size_t len;
2397 
2398 	if (hostidp == NULL)
2399 		return (Z_INVAL);
2400 
2401 	/* Empty strings and strings with whitespace are invalid. */
2402 	if (*hostidp == '\0')
2403 		return (Z_HOSTID_FUBAR);
2404 	for (currentp = (char *)hostidp; *currentp != '\0'; ++currentp) {
2405 		if (isspace(*currentp))
2406 			return (Z_HOSTID_FUBAR);
2407 	}
2408 	len = (size_t)(currentp - hostidp);
2409 
2410 	/*
2411 	 * The caller might pass a hostid that is larger than the maximum
2412 	 * unsigned 32-bit integral value.  Check for this!  Also, make sure
2413 	 * that the whole string is converted (this helps us find illegal
2414 	 * characters) and that the whole string fits within a buffer of size
2415 	 * HW_HOSTID_LEN.
2416 	 */
2417 	currentp = (char *)hostidp;
2418 	if (strncmp(hostidp, "0x", 2) == 0 || strncmp(hostidp, "0X", 2) == 0)
2419 		currentp += 2;
2420 	hostidval = strtoull(currentp, &currentp, 16);
2421 	if ((size_t)(currentp - hostidp) >= HW_HOSTID_LEN)
2422 		return (Z_TOO_BIG);
2423 	if (hostidval > UINT_MAX || hostidval == HW_INVALID_HOSTID ||
2424 	    currentp != hostidp + len)
2425 		return (Z_HOSTID_FUBAR);
2426 	return (Z_OK);
2427 }
2428 
2429 int
2430 zonecfg_lookup_dev(zone_dochandle_t handle, struct zone_devtab *tabptr)
2431 {
2432 	xmlNodePtr cur, firstmatch;
2433 	int err;
2434 	char match[MAXPATHLEN];
2435 
2436 	if (tabptr == NULL)
2437 		return (Z_INVAL);
2438 
2439 	if ((err = operation_prep(handle)) != Z_OK)
2440 		return (err);
2441 
2442 	cur = handle->zone_dh_cur;
2443 	firstmatch = NULL;
2444 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
2445 		if (xmlStrcmp(cur->name, DTD_ELEM_DEVICE))
2446 			continue;
2447 		if (strlen(tabptr->zone_dev_match) == 0)
2448 			continue;
2449 
2450 		if ((fetchprop(cur, DTD_ATTR_MATCH, match,
2451 		    sizeof (match)) == Z_OK)) {
2452 			if (strcmp(tabptr->zone_dev_match,
2453 			    match) == 0) {
2454 				if (firstmatch == NULL)
2455 					firstmatch = cur;
2456 				else if (firstmatch != cur)
2457 					return (Z_INSUFFICIENT_SPEC);
2458 			} else {
2459 				/*
2460 				 * If another property matched but this
2461 				 * one doesn't then reset firstmatch.
2462 				 */
2463 				if (firstmatch == cur)
2464 					firstmatch = NULL;
2465 			}
2466 		}
2467 	}
2468 	if (firstmatch == NULL)
2469 		return (Z_NO_RESOURCE_ID);
2470 
2471 	cur = firstmatch;
2472 
2473 	if ((err = fetchprop(cur, DTD_ATTR_MATCH, tabptr->zone_dev_match,
2474 	    sizeof (tabptr->zone_dev_match))) != Z_OK)
2475 		return (err);
2476 
2477 	return (Z_OK);
2478 }
2479 
2480 static int
2481 zonecfg_add_dev_core(zone_dochandle_t handle, struct zone_devtab *tabptr)
2482 {
2483 	xmlNodePtr newnode, cur = handle->zone_dh_cur;
2484 	int err;
2485 
2486 	newnode = xmlNewTextChild(cur, NULL, DTD_ELEM_DEVICE, NULL);
2487 
2488 	if ((err = newprop(newnode, DTD_ATTR_MATCH,
2489 	    tabptr->zone_dev_match)) != Z_OK)
2490 		return (err);
2491 
2492 	return (Z_OK);
2493 }
2494 
2495 int
2496 zonecfg_add_dev(zone_dochandle_t handle, struct zone_devtab *tabptr)
2497 {
2498 	int err;
2499 
2500 	if (tabptr == NULL)
2501 		return (Z_INVAL);
2502 
2503 	if ((err = operation_prep(handle)) != Z_OK)
2504 		return (err);
2505 
2506 	if ((err = zonecfg_add_dev_core(handle, tabptr)) != Z_OK)
2507 		return (err);
2508 
2509 	return (Z_OK);
2510 }
2511 
2512 static int
2513 zonecfg_delete_dev_core(zone_dochandle_t handle, struct zone_devtab *tabptr)
2514 {
2515 	xmlNodePtr cur = handle->zone_dh_cur;
2516 	int match_match;
2517 
2518 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
2519 		if (xmlStrcmp(cur->name, DTD_ELEM_DEVICE))
2520 			continue;
2521 
2522 		match_match = match_prop(cur, DTD_ATTR_MATCH,
2523 		    tabptr->zone_dev_match);
2524 
2525 		if (match_match) {
2526 			xmlUnlinkNode(cur);
2527 			xmlFreeNode(cur);
2528 			return (Z_OK);
2529 		}
2530 	}
2531 	return (Z_NO_RESOURCE_ID);
2532 }
2533 
2534 int
2535 zonecfg_delete_dev(zone_dochandle_t handle, struct zone_devtab *tabptr)
2536 {
2537 	int err;
2538 
2539 	if (tabptr == NULL)
2540 		return (Z_INVAL);
2541 
2542 	if ((err = operation_prep(handle)) != Z_OK)
2543 		return (err);
2544 
2545 	if ((err = zonecfg_delete_dev_core(handle, tabptr)) != Z_OK)
2546 		return (err);
2547 
2548 	return (Z_OK);
2549 }
2550 
2551 int
2552 zonecfg_modify_dev(
2553 	zone_dochandle_t handle,
2554 	struct zone_devtab *oldtabptr,
2555 	struct zone_devtab *newtabptr)
2556 {
2557 	int err;
2558 
2559 	if (oldtabptr == NULL || newtabptr == NULL)
2560 		return (Z_INVAL);
2561 
2562 	if ((err = operation_prep(handle)) != Z_OK)
2563 		return (err);
2564 
2565 	if ((err = zonecfg_delete_dev_core(handle, oldtabptr)) != Z_OK)
2566 		return (err);
2567 
2568 	if ((err = zonecfg_add_dev_core(handle, newtabptr)) != Z_OK)
2569 		return (err);
2570 
2571 	return (Z_OK);
2572 }
2573 
2574 /* Lock to serialize all devwalks */
2575 static pthread_mutex_t zonecfg_devwalk_lock = PTHREAD_MUTEX_INITIALIZER;
2576 /*
2577  * Global variables used to pass data from zonecfg_dev_manifest to the nftw
2578  * call-back (zonecfg_devwalk_cb).  g_devwalk_data is really the void*
2579  * parameter and g_devwalk_cb is really the *cb parameter from
2580  * zonecfg_dev_manifest.
2581  */
2582 typedef struct __g_devwalk_data *g_devwalk_data_t;
2583 static g_devwalk_data_t g_devwalk_data;
2584 static int (*g_devwalk_cb)(const char *, uid_t, gid_t, mode_t, const char *,
2585     void *);
2586 static size_t g_devwalk_skip_prefix;
2587 
2588 /*
2589  * zonecfg_dev_manifest call-back function used during detach to generate the
2590  * dev info in the manifest.
2591  */
2592 static int
2593 get_detach_dev_entry(const char *name, uid_t uid, gid_t gid, mode_t mode,
2594     const char *acl, void *hdl)
2595 {
2596 	zone_dochandle_t handle = (zone_dochandle_t)hdl;
2597 	xmlNodePtr newnode;
2598 	xmlNodePtr cur;
2599 	int err;
2600 	char buf[128];
2601 
2602 	if ((err = operation_prep(handle)) != Z_OK)
2603 		return (err);
2604 
2605 	cur = handle->zone_dh_cur;
2606 	newnode = xmlNewTextChild(cur, NULL, DTD_ELEM_DEV_PERM, NULL);
2607 	if ((err = newprop(newnode, DTD_ATTR_NAME, (char *)name)) != Z_OK)
2608 		return (err);
2609 	(void) snprintf(buf, sizeof (buf), "%lu", uid);
2610 	if ((err = newprop(newnode, DTD_ATTR_UID, buf)) != Z_OK)
2611 		return (err);
2612 	(void) snprintf(buf, sizeof (buf), "%lu", gid);
2613 	if ((err = newprop(newnode, DTD_ATTR_GID, buf)) != Z_OK)
2614 		return (err);
2615 	(void) snprintf(buf, sizeof (buf), "%o", mode);
2616 	if ((err = newprop(newnode, DTD_ATTR_MODE, buf)) != Z_OK)
2617 		return (err);
2618 	if ((err = newprop(newnode, DTD_ATTR_ACL, (char *)acl)) != Z_OK)
2619 		return (err);
2620 	return (Z_OK);
2621 }
2622 
2623 /*
2624  * This is the nftw call-back function used by zonecfg_dev_manifest.  It is
2625  * responsible for calling the actual call-back.
2626  */
2627 /* ARGSUSED2 */
2628 static int
2629 zonecfg_devwalk_cb(const char *path, const struct stat *st, int f,
2630     struct FTW *ftw)
2631 {
2632 	acl_t *acl;
2633 	char *acl_txt = NULL;
2634 
2635 	/* skip all but character and block devices */
2636 	if (!S_ISBLK(st->st_mode) && !S_ISCHR(st->st_mode))
2637 		return (0);
2638 
2639 	if ((acl_get(path, ACL_NO_TRIVIAL, &acl) == 0) &&
2640 	    acl != NULL) {
2641 		acl_txt = acl_totext(acl, ACL_NORESOLVE);
2642 		acl_free(acl);
2643 	}
2644 
2645 	if (strlen(path) <= g_devwalk_skip_prefix)
2646 		return (0);
2647 
2648 	g_devwalk_cb(path + g_devwalk_skip_prefix, st->st_uid, st->st_gid,
2649 	    st->st_mode & S_IAMB, acl_txt != NULL ? acl_txt : "",
2650 	    g_devwalk_data);
2651 	free(acl_txt);
2652 	return (0);
2653 }
2654 
2655 /*
2656  * Walk the dev tree for the zone specified by hdl and call the
2657  * get_detach_dev_entry call-back function for each entry in the tree.  The
2658  * call-back will be passed the name, uid, gid, mode, acl string and the
2659  * handle input parameter for each dev entry.
2660  *
2661  * Data is passed to get_detach_dev_entry through the global variables
2662  * g_devwalk_data, *g_devwalk_cb, and g_devwalk_skip_prefix.  The
2663  * zonecfg_devwalk_cb function will actually call get_detach_dev_entry.
2664  */
2665 int
2666 zonecfg_dev_manifest(zone_dochandle_t hdl)
2667 {
2668 	char path[MAXPATHLEN];
2669 	int ret;
2670 
2671 	if ((ret = zonecfg_get_zonepath(hdl, path, sizeof (path))) != Z_OK)
2672 		return (ret);
2673 
2674 	if (strlcat(path, "/dev", sizeof (path)) >= sizeof (path))
2675 		return (Z_TOO_BIG);
2676 
2677 	/*
2678 	 * We have to serialize all devwalks in the same process
2679 	 * (which should be fine), since nftw() is so badly designed.
2680 	 */
2681 	(void) pthread_mutex_lock(&zonecfg_devwalk_lock);
2682 
2683 	g_devwalk_skip_prefix = strlen(path) + 1;
2684 	g_devwalk_data = (g_devwalk_data_t)hdl;
2685 	g_devwalk_cb = get_detach_dev_entry;
2686 	(void) nftw(path, zonecfg_devwalk_cb, 0, FTW_PHYS);
2687 
2688 	(void) pthread_mutex_unlock(&zonecfg_devwalk_lock);
2689 	return (Z_OK);
2690 }
2691 
2692 /*
2693  * Update the owner, group, mode and acl on the specified dev (inpath) for
2694  * the zone (hdl).  This function can be used to fix up the dev tree after
2695  * attaching a migrated zone.
2696  */
2697 int
2698 zonecfg_devperms_apply(zone_dochandle_t hdl, const char *inpath, uid_t owner,
2699     gid_t group, mode_t mode, const char *acltxt)
2700 {
2701 	int ret;
2702 	char path[MAXPATHLEN];
2703 	struct stat st;
2704 	acl_t *aclp;
2705 
2706 	if ((ret = zonecfg_get_zonepath(hdl, path, sizeof (path))) != Z_OK)
2707 		return (ret);
2708 
2709 	if (strlcat(path, "/dev/", sizeof (path)) >= sizeof (path))
2710 		return (Z_TOO_BIG);
2711 	if (strlcat(path, inpath, sizeof (path)) >= sizeof (path))
2712 		return (Z_TOO_BIG);
2713 
2714 	if (stat(path, &st) == -1)
2715 		return (Z_INVAL);
2716 
2717 	/* make sure we're only touching device nodes */
2718 	if (!S_ISCHR(st.st_mode) && !S_ISBLK(st.st_mode))
2719 		return (Z_INVAL);
2720 
2721 	if (chown(path, owner, group) == -1)
2722 		return (Z_SYSTEM);
2723 
2724 	if (chmod(path, mode) == -1)
2725 		return (Z_SYSTEM);
2726 
2727 	if ((acltxt == NULL) || (strcmp(acltxt, "") == 0))
2728 		return (Z_OK);
2729 
2730 	if (acl_fromtext(acltxt, &aclp) != 0) {
2731 		errno = EINVAL;
2732 		return (Z_SYSTEM);
2733 	}
2734 
2735 	errno = 0;
2736 	if (acl_set(path, aclp) == -1) {
2737 		free(aclp);
2738 		return (Z_SYSTEM);
2739 	}
2740 
2741 	free(aclp);
2742 	return (Z_OK);
2743 }
2744 
2745 /*
2746  * This function finds everything mounted under a zone's rootpath.
2747  * This returns the number of mounts under rootpath, or -1 on error.
2748  * callback is called once per mount found with the first argument
2749  * pointing to the  mount point.
2750  *
2751  * If the callback function returns non-zero zonecfg_find_mounts
2752  * aborts with an error.
2753  */
2754 int
2755 zonecfg_find_mounts(char *rootpath, int (*callback)(const char *, void *),
2756     void *priv) {
2757 	FILE *mnttab;
2758 	struct mnttab m;
2759 	size_t l;
2760 	int zfsl;
2761 	int rv = 0;
2762 	char zfs_path[MAXPATHLEN];
2763 
2764 	assert(rootpath != NULL);
2765 
2766 	if ((zfsl = snprintf(zfs_path, sizeof (zfs_path), "%s/.zfs/", rootpath))
2767 	    >= sizeof (zfs_path))
2768 		return (-1);
2769 
2770 	l = strlen(rootpath);
2771 
2772 	mnttab = fopen("/etc/mnttab", "r");
2773 
2774 	if (mnttab == NULL)
2775 		return (-1);
2776 
2777 	if (ioctl(fileno(mnttab), MNTIOC_SHOWHIDDEN, NULL) < 0)  {
2778 		rv = -1;
2779 		goto out;
2780 	}
2781 
2782 	while (!getmntent(mnttab, &m)) {
2783 		if ((strncmp(rootpath, m.mnt_mountp, l) == 0) &&
2784 		    (m.mnt_mountp[l] == '/') &&
2785 		    (strncmp(zfs_path, m.mnt_mountp, zfsl) != 0)) {
2786 			rv++;
2787 			if (callback == NULL)
2788 				continue;
2789 			if (callback(m.mnt_mountp, priv)) {
2790 				rv = -1;
2791 				goto out;
2792 
2793 			}
2794 		}
2795 	}
2796 
2797 out:
2798 	(void) fclose(mnttab);
2799 	return (rv);
2800 }
2801 
2802 int
2803 zonecfg_lookup_attr(zone_dochandle_t handle, struct zone_attrtab *tabptr)
2804 {
2805 	xmlNodePtr cur, firstmatch;
2806 	int err;
2807 	char name[MAXNAMELEN], type[MAXNAMELEN], value[MAXNAMELEN];
2808 
2809 	if (tabptr == NULL)
2810 		return (Z_INVAL);
2811 
2812 	if ((err = operation_prep(handle)) != Z_OK)
2813 		return (err);
2814 
2815 	cur = handle->zone_dh_cur;
2816 	firstmatch = NULL;
2817 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
2818 		if (xmlStrcmp(cur->name, DTD_ELEM_ATTR))
2819 			continue;
2820 		if (strlen(tabptr->zone_attr_name) > 0) {
2821 			if ((fetchprop(cur, DTD_ATTR_NAME, name,
2822 			    sizeof (name)) == Z_OK) &&
2823 			    (strcmp(tabptr->zone_attr_name, name) == 0)) {
2824 				if (firstmatch == NULL)
2825 					firstmatch = cur;
2826 				else
2827 					return (Z_INSUFFICIENT_SPEC);
2828 			}
2829 		}
2830 		if (strlen(tabptr->zone_attr_type) > 0) {
2831 			if ((fetchprop(cur, DTD_ATTR_TYPE, type,
2832 			    sizeof (type)) == Z_OK)) {
2833 				if (strcmp(tabptr->zone_attr_type, type) == 0) {
2834 					if (firstmatch == NULL)
2835 						firstmatch = cur;
2836 					else if (firstmatch != cur)
2837 						return (Z_INSUFFICIENT_SPEC);
2838 				} else {
2839 					/*
2840 					 * If another property matched but this
2841 					 * one doesn't then reset firstmatch.
2842 					 */
2843 					if (firstmatch == cur)
2844 						firstmatch = NULL;
2845 				}
2846 			}
2847 		}
2848 		if (strlen(tabptr->zone_attr_value) > 0) {
2849 			if ((fetchprop(cur, DTD_ATTR_VALUE, value,
2850 			    sizeof (value)) == Z_OK)) {
2851 				if (strcmp(tabptr->zone_attr_value, value) ==
2852 				    0) {
2853 					if (firstmatch == NULL)
2854 						firstmatch = cur;
2855 					else if (firstmatch != cur)
2856 						return (Z_INSUFFICIENT_SPEC);
2857 				} else {
2858 					/*
2859 					 * If another property matched but this
2860 					 * one doesn't then reset firstmatch.
2861 					 */
2862 					if (firstmatch == cur)
2863 						firstmatch = NULL;
2864 				}
2865 			}
2866 		}
2867 	}
2868 	if (firstmatch == NULL)
2869 		return (Z_NO_RESOURCE_ID);
2870 
2871 	cur = firstmatch;
2872 
2873 	if ((err = fetchprop(cur, DTD_ATTR_NAME, tabptr->zone_attr_name,
2874 	    sizeof (tabptr->zone_attr_name))) != Z_OK)
2875 		return (err);
2876 
2877 	if ((err = fetchprop(cur, DTD_ATTR_TYPE, tabptr->zone_attr_type,
2878 	    sizeof (tabptr->zone_attr_type))) != Z_OK)
2879 		return (err);
2880 
2881 	if ((err = fetchprop(cur, DTD_ATTR_VALUE, tabptr->zone_attr_value,
2882 	    sizeof (tabptr->zone_attr_value))) != Z_OK)
2883 		return (err);
2884 
2885 	return (Z_OK);
2886 }
2887 
2888 static int
2889 zonecfg_add_attr_core(zone_dochandle_t handle, struct zone_attrtab *tabptr)
2890 {
2891 	xmlNodePtr newnode, cur = handle->zone_dh_cur;
2892 	int err;
2893 
2894 	newnode = xmlNewTextChild(cur, NULL, DTD_ELEM_ATTR, NULL);
2895 	err = newprop(newnode, DTD_ATTR_NAME, tabptr->zone_attr_name);
2896 	if (err != Z_OK)
2897 		return (err);
2898 	err = newprop(newnode, DTD_ATTR_TYPE, tabptr->zone_attr_type);
2899 	if (err != Z_OK)
2900 		return (err);
2901 	err = newprop(newnode, DTD_ATTR_VALUE, tabptr->zone_attr_value);
2902 	if (err != Z_OK)
2903 		return (err);
2904 	return (Z_OK);
2905 }
2906 
2907 int
2908 zonecfg_add_attr(zone_dochandle_t handle, struct zone_attrtab *tabptr)
2909 {
2910 	int err;
2911 
2912 	if (tabptr == NULL)
2913 		return (Z_INVAL);
2914 
2915 	if ((err = operation_prep(handle)) != Z_OK)
2916 		return (err);
2917 
2918 	if ((err = zonecfg_add_attr_core(handle, tabptr)) != Z_OK)
2919 		return (err);
2920 
2921 	return (Z_OK);
2922 }
2923 
2924 static int
2925 zonecfg_delete_attr_core(zone_dochandle_t handle, struct zone_attrtab *tabptr)
2926 {
2927 	xmlNodePtr cur = handle->zone_dh_cur;
2928 	int name_match, type_match, value_match;
2929 
2930 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
2931 		if (xmlStrcmp(cur->name, DTD_ELEM_ATTR))
2932 			continue;
2933 
2934 		name_match = match_prop(cur, DTD_ATTR_NAME,
2935 		    tabptr->zone_attr_name);
2936 		type_match = match_prop(cur, DTD_ATTR_TYPE,
2937 		    tabptr->zone_attr_type);
2938 		value_match = match_prop(cur, DTD_ATTR_VALUE,
2939 		    tabptr->zone_attr_value);
2940 
2941 		if (name_match && type_match && value_match) {
2942 			xmlUnlinkNode(cur);
2943 			xmlFreeNode(cur);
2944 			return (Z_OK);
2945 		}
2946 	}
2947 	return (Z_NO_RESOURCE_ID);
2948 }
2949 
2950 int
2951 zonecfg_delete_attr(zone_dochandle_t handle, struct zone_attrtab *tabptr)
2952 {
2953 	int err;
2954 
2955 	if (tabptr == NULL)
2956 		return (Z_INVAL);
2957 
2958 	if ((err = operation_prep(handle)) != Z_OK)
2959 		return (err);
2960 
2961 	if ((err = zonecfg_delete_attr_core(handle, tabptr)) != Z_OK)
2962 		return (err);
2963 
2964 	return (Z_OK);
2965 }
2966 
2967 int
2968 zonecfg_modify_attr(
2969 	zone_dochandle_t handle,
2970 	struct zone_attrtab *oldtabptr,
2971 	struct zone_attrtab *newtabptr)
2972 {
2973 	int err;
2974 
2975 	if (oldtabptr == NULL || newtabptr == NULL)
2976 		return (Z_INVAL);
2977 
2978 	if ((err = operation_prep(handle)) != Z_OK)
2979 		return (err);
2980 
2981 	if ((err = zonecfg_delete_attr_core(handle, oldtabptr)) != Z_OK)
2982 		return (err);
2983 
2984 	if ((err = zonecfg_add_attr_core(handle, newtabptr)) != Z_OK)
2985 		return (err);
2986 
2987 	return (Z_OK);
2988 }
2989 
2990 int
2991 zonecfg_get_attr_boolean(const struct zone_attrtab *attr, boolean_t *value)
2992 {
2993 	if (attr == NULL)
2994 		return (Z_INVAL);
2995 
2996 	if (strcmp(attr->zone_attr_type, DTD_ENTITY_BOOLEAN) != 0)
2997 		return (Z_INVAL);
2998 
2999 	if (strcmp(attr->zone_attr_value, DTD_ENTITY_TRUE) == 0) {
3000 		*value = B_TRUE;
3001 		return (Z_OK);
3002 	}
3003 	if (strcmp(attr->zone_attr_value, DTD_ENTITY_FALSE) == 0) {
3004 		*value = B_FALSE;
3005 		return (Z_OK);
3006 	}
3007 	return (Z_INVAL);
3008 }
3009 
3010 int
3011 zonecfg_get_attr_int(const struct zone_attrtab *attr, int64_t *value)
3012 {
3013 	long long result;
3014 	char *endptr;
3015 
3016 	if (attr == NULL)
3017 		return (Z_INVAL);
3018 
3019 	if (strcmp(attr->zone_attr_type, DTD_ENTITY_INT) != 0)
3020 		return (Z_INVAL);
3021 
3022 	errno = 0;
3023 	result = strtoll(attr->zone_attr_value, &endptr, 10);
3024 	if (errno != 0 || *endptr != '\0')
3025 		return (Z_INVAL);
3026 	*value = result;
3027 	return (Z_OK);
3028 }
3029 
3030 int
3031 zonecfg_get_attr_string(const struct zone_attrtab *attr, char *value,
3032     size_t val_sz)
3033 {
3034 	if (attr == NULL)
3035 		return (Z_INVAL);
3036 
3037 	if (strcmp(attr->zone_attr_type, DTD_ENTITY_STRING) != 0)
3038 		return (Z_INVAL);
3039 
3040 	if (strlcpy(value, attr->zone_attr_value, val_sz) >= val_sz)
3041 		return (Z_TOO_BIG);
3042 	return (Z_OK);
3043 }
3044 
3045 int
3046 zonecfg_get_attr_uint(const struct zone_attrtab *attr, uint64_t *value)
3047 {
3048 	unsigned long long result;
3049 	long long neg_result;
3050 	char *endptr;
3051 
3052 	if (attr == NULL)
3053 		return (Z_INVAL);
3054 
3055 	if (strcmp(attr->zone_attr_type, DTD_ENTITY_UINT) != 0)
3056 		return (Z_INVAL);
3057 
3058 	errno = 0;
3059 	result = strtoull(attr->zone_attr_value, &endptr, 10);
3060 	if (errno != 0 || *endptr != '\0')
3061 		return (Z_INVAL);
3062 	errno = 0;
3063 	neg_result = strtoll(attr->zone_attr_value, &endptr, 10);
3064 	/*
3065 	 * Incredibly, strtoull("<negative number>", ...) will not fail but
3066 	 * return whatever (negative) number cast as a u_longlong_t, so we
3067 	 * need to look for this here.
3068 	 */
3069 	if (errno == 0 && neg_result < 0)
3070 		return (Z_INVAL);
3071 	*value = result;
3072 	return (Z_OK);
3073 }
3074 
3075 int
3076 zonecfg_lookup_rctl(zone_dochandle_t handle, struct zone_rctltab *tabptr)
3077 {
3078 	xmlNodePtr cur, val;
3079 	char savedname[MAXNAMELEN];
3080 	struct zone_rctlvaltab *valptr;
3081 	int err;
3082 
3083 	if (strlen(tabptr->zone_rctl_name) == 0)
3084 		return (Z_INVAL);
3085 
3086 	if ((err = operation_prep(handle)) != Z_OK)
3087 		return (err);
3088 
3089 	cur = handle->zone_dh_cur;
3090 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
3091 		if (xmlStrcmp(cur->name, DTD_ELEM_RCTL))
3092 			continue;
3093 		if ((fetchprop(cur, DTD_ATTR_NAME, savedname,
3094 		    sizeof (savedname)) == Z_OK) &&
3095 		    (strcmp(savedname, tabptr->zone_rctl_name) == 0)) {
3096 			tabptr->zone_rctl_valptr = NULL;
3097 			for (val = cur->xmlChildrenNode; val != NULL;
3098 			    val = val->next) {
3099 				valptr = (struct zone_rctlvaltab *)malloc(
3100 				    sizeof (struct zone_rctlvaltab));
3101 				if (valptr == NULL)
3102 					return (Z_NOMEM);
3103 				if ((fetchprop(val, DTD_ATTR_PRIV,
3104 				    valptr->zone_rctlval_priv,
3105 				    sizeof (valptr->zone_rctlval_priv)) !=
3106 				    Z_OK))
3107 					break;
3108 				if ((fetchprop(val, DTD_ATTR_LIMIT,
3109 				    valptr->zone_rctlval_limit,
3110 				    sizeof (valptr->zone_rctlval_limit)) !=
3111 				    Z_OK))
3112 					break;
3113 				if ((fetchprop(val, DTD_ATTR_ACTION,
3114 				    valptr->zone_rctlval_action,
3115 				    sizeof (valptr->zone_rctlval_action)) !=
3116 				    Z_OK))
3117 					break;
3118 				if (zonecfg_add_rctl_value(tabptr, valptr) !=
3119 				    Z_OK)
3120 					break;
3121 			}
3122 			return (Z_OK);
3123 		}
3124 	}
3125 	return (Z_NO_RESOURCE_ID);
3126 }
3127 
3128 static int
3129 zonecfg_add_rctl_core(zone_dochandle_t handle, struct zone_rctltab *tabptr)
3130 {
3131 	xmlNodePtr newnode, cur = handle->zone_dh_cur, valnode;
3132 	struct zone_rctlvaltab *valptr;
3133 	int err;
3134 
3135 	newnode = xmlNewTextChild(cur, NULL, DTD_ELEM_RCTL, NULL);
3136 	err = newprop(newnode, DTD_ATTR_NAME, tabptr->zone_rctl_name);
3137 	if (err != Z_OK)
3138 		return (err);
3139 	for (valptr = tabptr->zone_rctl_valptr; valptr != NULL;
3140 	    valptr = valptr->zone_rctlval_next) {
3141 		valnode = xmlNewTextChild(newnode, NULL,
3142 		    DTD_ELEM_RCTLVALUE, NULL);
3143 		err = newprop(valnode, DTD_ATTR_PRIV,
3144 		    valptr->zone_rctlval_priv);
3145 		if (err != Z_OK)
3146 			return (err);
3147 		err = newprop(valnode, DTD_ATTR_LIMIT,
3148 		    valptr->zone_rctlval_limit);
3149 		if (err != Z_OK)
3150 			return (err);
3151 		err = newprop(valnode, DTD_ATTR_ACTION,
3152 		    valptr->zone_rctlval_action);
3153 		if (err != Z_OK)
3154 			return (err);
3155 	}
3156 	return (Z_OK);
3157 }
3158 
3159 int
3160 zonecfg_add_rctl(zone_dochandle_t handle, struct zone_rctltab *tabptr)
3161 {
3162 	int err;
3163 
3164 	if (tabptr == NULL)
3165 		return (Z_INVAL);
3166 
3167 	if ((err = operation_prep(handle)) != Z_OK)
3168 		return (err);
3169 
3170 	if ((err = zonecfg_add_rctl_core(handle, tabptr)) != Z_OK)
3171 		return (err);
3172 
3173 	return (Z_OK);
3174 }
3175 
3176 static int
3177 zonecfg_delete_rctl_core(zone_dochandle_t handle, struct zone_rctltab *tabptr)
3178 {
3179 	xmlNodePtr cur = handle->zone_dh_cur;
3180 	xmlChar *savedname;
3181 	int name_result;
3182 
3183 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
3184 		if (xmlStrcmp(cur->name, DTD_ELEM_RCTL))
3185 			continue;
3186 
3187 		savedname = xmlGetProp(cur, DTD_ATTR_NAME);
3188 		if (savedname == NULL)	/* shouldn't happen */
3189 			continue;
3190 		name_result = xmlStrcmp(savedname,
3191 		    (const xmlChar *) tabptr->zone_rctl_name);
3192 		xmlFree(savedname);
3193 
3194 		if (name_result == 0) {
3195 			xmlUnlinkNode(cur);
3196 			xmlFreeNode(cur);
3197 			return (Z_OK);
3198 		}
3199 	}
3200 	return (Z_NO_RESOURCE_ID);
3201 }
3202 
3203 int
3204 zonecfg_delete_rctl(zone_dochandle_t handle, struct zone_rctltab *tabptr)
3205 {
3206 	int err;
3207 
3208 	if (tabptr == NULL)
3209 		return (Z_INVAL);
3210 
3211 	if ((err = operation_prep(handle)) != Z_OK)
3212 		return (err);
3213 
3214 	if ((err = zonecfg_delete_rctl_core(handle, tabptr)) != Z_OK)
3215 		return (err);
3216 
3217 	return (Z_OK);
3218 }
3219 
3220 int
3221 zonecfg_modify_rctl(
3222 	zone_dochandle_t handle,
3223 	struct zone_rctltab *oldtabptr,
3224 	struct zone_rctltab *newtabptr)
3225 {
3226 	int err;
3227 
3228 	if (oldtabptr == NULL || newtabptr == NULL)
3229 		return (Z_INVAL);
3230 
3231 	if ((err = operation_prep(handle)) != Z_OK)
3232 		return (err);
3233 
3234 	if ((err = zonecfg_delete_rctl_core(handle, oldtabptr)) != Z_OK)
3235 		return (err);
3236 
3237 	if ((err = zonecfg_add_rctl_core(handle, newtabptr)) != Z_OK)
3238 		return (err);
3239 
3240 	return (Z_OK);
3241 }
3242 
3243 int
3244 zonecfg_add_rctl_value(
3245 	struct zone_rctltab *tabptr,
3246 	struct zone_rctlvaltab *valtabptr)
3247 {
3248 	struct zone_rctlvaltab *last, *old, *new;
3249 	rctlblk_t *rctlblk = alloca(rctlblk_size());
3250 
3251 	last = tabptr->zone_rctl_valptr;
3252 	for (old = last; old != NULL; old = old->zone_rctlval_next)
3253 		last = old;	/* walk to the end of the list */
3254 	new = valtabptr;	/* alloc'd by caller */
3255 	new->zone_rctlval_next = NULL;
3256 	if (zonecfg_construct_rctlblk(valtabptr, rctlblk) != Z_OK)
3257 		return (Z_INVAL);
3258 	if (!zonecfg_valid_rctlblk(rctlblk))
3259 		return (Z_INVAL);
3260 	if (last == NULL)
3261 		tabptr->zone_rctl_valptr = new;
3262 	else
3263 		last->zone_rctlval_next = new;
3264 	return (Z_OK);
3265 }
3266 
3267 int
3268 zonecfg_remove_rctl_value(
3269 	struct zone_rctltab *tabptr,
3270 	struct zone_rctlvaltab *valtabptr)
3271 {
3272 	struct zone_rctlvaltab *last, *this, *next;
3273 
3274 	last = tabptr->zone_rctl_valptr;
3275 	for (this = last; this != NULL; this = this->zone_rctlval_next) {
3276 		if (strcmp(this->zone_rctlval_priv,
3277 		    valtabptr->zone_rctlval_priv) == 0 &&
3278 		    strcmp(this->zone_rctlval_limit,
3279 		    valtabptr->zone_rctlval_limit) == 0 &&
3280 		    strcmp(this->zone_rctlval_action,
3281 		    valtabptr->zone_rctlval_action) == 0) {
3282 			next = this->zone_rctlval_next;
3283 			if (this == tabptr->zone_rctl_valptr)
3284 				tabptr->zone_rctl_valptr = next;
3285 			else
3286 				last->zone_rctlval_next = next;
3287 			free(this);
3288 			return (Z_OK);
3289 		} else
3290 			last = this;
3291 	}
3292 	return (Z_NO_PROPERTY_ID);
3293 }
3294 
3295 void
3296 zonecfg_set_swinv(zone_dochandle_t handle)
3297 {
3298 	handle->zone_dh_sw_inv = B_TRUE;
3299 }
3300 
3301 /*
3302  * Add the pkg to the sw inventory on the handle.
3303  */
3304 int
3305 zonecfg_add_pkg(zone_dochandle_t handle, char *name, char *version)
3306 {
3307 	xmlNodePtr newnode;
3308 	xmlNodePtr cur;
3309 	int err;
3310 
3311 	if ((err = operation_prep(handle)) != Z_OK)
3312 		return (err);
3313 
3314 	cur = handle->zone_dh_cur;
3315 	newnode = xmlNewTextChild(cur, NULL, DTD_ELEM_PACKAGE, NULL);
3316 	if ((err = newprop(newnode, DTD_ATTR_NAME, name)) != Z_OK)
3317 		return (err);
3318 	if ((err = newprop(newnode, DTD_ATTR_VERSION, version)) != Z_OK)
3319 		return (err);
3320 	return (Z_OK);
3321 }
3322 
3323 int
3324 zonecfg_add_patch(zone_dochandle_t handle, char *id, void **pnode)
3325 {
3326 	xmlNodePtr node = (xmlNodePtr)*pnode;
3327 	xmlNodePtr cur;
3328 	int err;
3329 
3330 	if ((err = operation_prep(handle)) != Z_OK)
3331 		return (err);
3332 
3333 	cur = handle->zone_dh_cur;
3334 	node = xmlNewTextChild(cur, NULL, DTD_ELEM_PATCH, NULL);
3335 	if ((err = newprop(node, DTD_ATTR_ID, id)) != Z_OK)
3336 		return (err);
3337 	*pnode = (void *)node;
3338 	return (Z_OK);
3339 }
3340 
3341 int
3342 zonecfg_add_patch_obs(char *id, void *cur)
3343 {
3344 	xmlNodePtr	node;
3345 	int err;
3346 
3347 	node = xmlNewTextChild((xmlNodePtr)cur, NULL, DTD_ELEM_OBSOLETES, NULL);
3348 	if ((err = newprop(node, DTD_ATTR_ID, id)) != Z_OK)
3349 		return (err);
3350 	return (Z_OK);
3351 }
3352 
3353 char *
3354 zonecfg_strerror(int errnum)
3355 {
3356 	switch (errnum) {
3357 	case Z_OK:
3358 		return (dgettext(TEXT_DOMAIN, "OK"));
3359 	case Z_EMPTY_DOCUMENT:
3360 		return (dgettext(TEXT_DOMAIN, "Empty document"));
3361 	case Z_WRONG_DOC_TYPE:
3362 		return (dgettext(TEXT_DOMAIN, "Wrong document type"));
3363 	case Z_BAD_PROPERTY:
3364 		return (dgettext(TEXT_DOMAIN, "Bad document property"));
3365 	case Z_TEMP_FILE:
3366 		return (dgettext(TEXT_DOMAIN,
3367 		    "Problem creating temporary file"));
3368 	case Z_SAVING_FILE:
3369 		return (dgettext(TEXT_DOMAIN, "Problem saving file"));
3370 	case Z_NO_ENTRY:
3371 		return (dgettext(TEXT_DOMAIN, "No such entry"));
3372 	case Z_BOGUS_ZONE_NAME:
3373 		return (dgettext(TEXT_DOMAIN, "Bogus zone name"));
3374 	case Z_REQD_RESOURCE_MISSING:
3375 		return (dgettext(TEXT_DOMAIN, "Required resource missing"));
3376 	case Z_REQD_PROPERTY_MISSING:
3377 		return (dgettext(TEXT_DOMAIN, "Required property missing"));
3378 	case Z_BAD_HANDLE:
3379 		return (dgettext(TEXT_DOMAIN, "Bad handle"));
3380 	case Z_NOMEM:
3381 		return (dgettext(TEXT_DOMAIN, "Out of memory"));
3382 	case Z_INVAL:
3383 		return (dgettext(TEXT_DOMAIN, "Invalid argument"));
3384 	case Z_ACCES:
3385 		return (dgettext(TEXT_DOMAIN, "Permission denied"));
3386 	case Z_TOO_BIG:
3387 		return (dgettext(TEXT_DOMAIN, "Argument list too long"));
3388 	case Z_MISC_FS:
3389 		return (dgettext(TEXT_DOMAIN,
3390 		    "Miscellaneous file system error"));
3391 	case Z_NO_ZONE:
3392 		return (dgettext(TEXT_DOMAIN, "No such zone configured"));
3393 	case Z_NO_RESOURCE_TYPE:
3394 		return (dgettext(TEXT_DOMAIN, "No such resource type"));
3395 	case Z_NO_RESOURCE_ID:
3396 		return (dgettext(TEXT_DOMAIN, "No such resource with that id"));
3397 	case Z_NO_PROPERTY_TYPE:
3398 		return (dgettext(TEXT_DOMAIN, "No such property type"));
3399 	case Z_NO_PROPERTY_ID:
3400 		return (dgettext(TEXT_DOMAIN, "No such property with that id"));
3401 	case Z_BAD_ZONE_STATE:
3402 		return (dgettext(TEXT_DOMAIN,
3403 		    "Zone state is invalid for the requested operation"));
3404 	case Z_INVALID_DOCUMENT:
3405 		return (dgettext(TEXT_DOMAIN, "Invalid document"));
3406 	case Z_NAME_IN_USE:
3407 		return (dgettext(TEXT_DOMAIN, "Zone name already in use"));
3408 	case Z_NO_SUCH_ID:
3409 		return (dgettext(TEXT_DOMAIN, "No such zone ID"));
3410 	case Z_UPDATING_INDEX:
3411 		return (dgettext(TEXT_DOMAIN, "Problem updating index file"));
3412 	case Z_LOCKING_FILE:
3413 		return (dgettext(TEXT_DOMAIN, "Locking index file"));
3414 	case Z_UNLOCKING_FILE:
3415 		return (dgettext(TEXT_DOMAIN, "Unlocking index file"));
3416 	case Z_INSUFFICIENT_SPEC:
3417 		return (dgettext(TEXT_DOMAIN, "Insufficient specification"));
3418 	case Z_RESOLVED_PATH:
3419 		return (dgettext(TEXT_DOMAIN, "Resolved path mismatch"));
3420 	case Z_IPV6_ADDR_PREFIX_LEN:
3421 		return (dgettext(TEXT_DOMAIN,
3422 		    "IPv6 address missing required prefix length"));
3423 	case Z_BOGUS_ADDRESS:
3424 		return (dgettext(TEXT_DOMAIN,
3425 		    "Neither an IPv4 nor an IPv6 address nor a host name"));
3426 	case Z_PRIV_PROHIBITED:
3427 		return (dgettext(TEXT_DOMAIN,
3428 		    "Specified privilege is prohibited"));
3429 	case Z_PRIV_REQUIRED:
3430 		return (dgettext(TEXT_DOMAIN,
3431 		    "Required privilege is missing"));
3432 	case Z_PRIV_UNKNOWN:
3433 		return (dgettext(TEXT_DOMAIN,
3434 		    "Specified privilege is unknown"));
3435 	case Z_BRAND_ERROR:
3436 		return (dgettext(TEXT_DOMAIN,
3437 		    "Brand-specific error"));
3438 	case Z_INCOMPATIBLE:
3439 		return (dgettext(TEXT_DOMAIN, "Incompatible settings"));
3440 	case Z_ALIAS_DISALLOW:
3441 		return (dgettext(TEXT_DOMAIN,
3442 		    "An incompatible rctl already exists for this property"));
3443 	case Z_CLEAR_DISALLOW:
3444 		return (dgettext(TEXT_DOMAIN,
3445 		    "Clearing this property is not allowed"));
3446 	case Z_POOL:
3447 		return (dgettext(TEXT_DOMAIN, "libpool(3LIB) error"));
3448 	case Z_POOLS_NOT_ACTIVE:
3449 		return (dgettext(TEXT_DOMAIN, "Pools facility not active; "
3450 		    "zone will not be bound to pool"));
3451 	case Z_POOL_ENABLE:
3452 		return (dgettext(TEXT_DOMAIN,
3453 		    "Could not enable pools facility"));
3454 	case Z_NO_POOL:
3455 		return (dgettext(TEXT_DOMAIN,
3456 		    "Pool not found; using default pool"));
3457 	case Z_POOL_CREATE:
3458 		return (dgettext(TEXT_DOMAIN,
3459 		    "Could not create a temporary pool"));
3460 	case Z_POOL_BIND:
3461 		return (dgettext(TEXT_DOMAIN, "Could not bind zone to pool"));
3462 	case Z_HOSTID_FUBAR:
3463 		return (dgettext(TEXT_DOMAIN, "Specified hostid is invalid"));
3464 	case Z_SYSTEM:
3465 		return (strerror(errno));
3466 	default:
3467 		return (dgettext(TEXT_DOMAIN, "Unknown error"));
3468 	}
3469 }
3470 
3471 /*
3472  * Note that the zonecfg_setXent() and zonecfg_endXent() calls are all the
3473  * same, as they just turn around and call zonecfg_setent() / zonecfg_endent().
3474  */
3475 
3476 static int
3477 zonecfg_setent(zone_dochandle_t handle)
3478 {
3479 	xmlNodePtr cur;
3480 	int err;
3481 
3482 	if (handle == NULL)
3483 		return (Z_INVAL);
3484 
3485 	if ((err = operation_prep(handle)) != Z_OK) {
3486 		handle->zone_dh_cur = NULL;
3487 		return (err);
3488 	}
3489 	cur = handle->zone_dh_cur;
3490 	cur = cur->xmlChildrenNode;
3491 	handle->zone_dh_cur = cur;
3492 	return (Z_OK);
3493 }
3494 
3495 static int
3496 zonecfg_endent(zone_dochandle_t handle)
3497 {
3498 	if (handle == NULL)
3499 		return (Z_INVAL);
3500 
3501 	handle->zone_dh_cur = handle->zone_dh_top;
3502 	return (Z_OK);
3503 }
3504 
3505 /*
3506  * Do the work required to manipulate a process through libproc.
3507  * If grab_process() returns no errors (0), then release_process()
3508  * must eventually be called.
3509  *
3510  * Return values:
3511  *      0 Successful creation of agent thread
3512  *      1 Error grabbing
3513  *      2 Error creating agent
3514  */
3515 static int
3516 grab_process(pr_info_handle_t *p)
3517 {
3518 	int ret;
3519 
3520 	if ((p->pr = Pgrab(p->pid, 0, &ret)) != NULL) {
3521 
3522 		if (Psetflags(p->pr, PR_RLC) != 0) {
3523 			Prelease(p->pr, 0);
3524 			return (1);
3525 		}
3526 		if (Pcreate_agent(p->pr) == 0) {
3527 			return (0);
3528 
3529 		} else {
3530 			Prelease(p->pr, 0);
3531 			return (2);
3532 		}
3533 	} else {
3534 		return (1);
3535 	}
3536 }
3537 
3538 /*
3539  * Release the specified process. This destroys the agent
3540  * and releases the process. If the process is NULL, nothing
3541  * is done. This function should only be called if grab_process()
3542  * has previously been called and returned success.
3543  *
3544  * This function is Pgrab-safe.
3545  */
3546 static void
3547 release_process(struct ps_prochandle *Pr)
3548 {
3549 	if (Pr == NULL)
3550 		return;
3551 
3552 	Pdestroy_agent(Pr);
3553 	Prelease(Pr, 0);
3554 }
3555 
3556 static boolean_t
3557 grab_zone_proc(char *zonename, pr_info_handle_t *p)
3558 {
3559 	DIR *dirp;
3560 	struct dirent *dentp;
3561 	zoneid_t zoneid;
3562 	int pid_self;
3563 	psinfo_t psinfo;
3564 
3565 	if (zone_get_id(zonename, &zoneid) != 0)
3566 		return (B_FALSE);
3567 
3568 	pid_self = getpid();
3569 
3570 	if ((dirp = opendir("/proc")) == NULL)
3571 		return (B_FALSE);
3572 
3573 	while (dentp = readdir(dirp)) {
3574 		p->pid = atoi(dentp->d_name);
3575 
3576 		/* Skip self */
3577 		if (p->pid == pid_self)
3578 			continue;
3579 
3580 		if (proc_get_psinfo(p->pid, &psinfo) != 0)
3581 			continue;
3582 
3583 		if (psinfo.pr_zoneid != zoneid)
3584 			continue;
3585 
3586 		/* attempt to grab process */
3587 		if (grab_process(p) != 0)
3588 			continue;
3589 
3590 		if (pr_getzoneid(p->pr) != zoneid) {
3591 			release_process(p->pr);
3592 			continue;
3593 		}
3594 
3595 		(void) closedir(dirp);
3596 		return (B_TRUE);
3597 	}
3598 
3599 	(void) closedir(dirp);
3600 	return (B_FALSE);
3601 }
3602 
3603 static boolean_t
3604 get_priv_rctl(struct ps_prochandle *pr, char *name, rctlblk_t *rblk)
3605 {
3606 	if (pr_getrctl(pr, name, NULL, rblk, RCTL_FIRST))
3607 		return (B_FALSE);
3608 
3609 	if (rctlblk_get_privilege(rblk) == RCPRIV_PRIVILEGED)
3610 		return (B_TRUE);
3611 
3612 	while (pr_getrctl(pr, name, rblk, rblk, RCTL_NEXT) == 0) {
3613 		if (rctlblk_get_privilege(rblk) == RCPRIV_PRIVILEGED)
3614 			return (B_TRUE);
3615 	}
3616 
3617 	return (B_FALSE);
3618 }
3619 
3620 /*
3621  * Apply the current rctl settings to the specified, running zone.
3622  */
3623 int
3624 zonecfg_apply_rctls(char *zone_name, zone_dochandle_t handle)
3625 {
3626 	int err;
3627 	int res = Z_OK;
3628 	rctlblk_t *rblk;
3629 	pr_info_handle_t p;
3630 	struct zone_rctltab rctl;
3631 
3632 	if ((err = zonecfg_setrctlent(handle)) != Z_OK)
3633 		return (err);
3634 
3635 	if ((rblk = (rctlblk_t *)malloc(rctlblk_size())) == NULL) {
3636 		(void) zonecfg_endrctlent(handle);
3637 		return (Z_NOMEM);
3638 	}
3639 
3640 	if (!grab_zone_proc(zone_name, &p)) {
3641 		(void) zonecfg_endrctlent(handle);
3642 		free(rblk);
3643 		return (Z_SYSTEM);
3644 	}
3645 
3646 	while (zonecfg_getrctlent(handle, &rctl) == Z_OK) {
3647 		char *rname;
3648 		struct zone_rctlvaltab *valptr;
3649 
3650 		rname = rctl.zone_rctl_name;
3651 
3652 		/* first delete all current privileged settings for this rctl */
3653 		while (get_priv_rctl(p.pr, rname, rblk)) {
3654 			if (pr_setrctl(p.pr, rname, NULL, rblk, RCTL_DELETE) !=
3655 			    0) {
3656 				res = Z_SYSTEM;
3657 				goto done;
3658 			}
3659 		}
3660 
3661 		/* now set each new value for the rctl */
3662 		for (valptr = rctl.zone_rctl_valptr; valptr != NULL;
3663 		    valptr = valptr->zone_rctlval_next) {
3664 			if ((err = zonecfg_construct_rctlblk(valptr, rblk))
3665 			    != Z_OK) {
3666 				res = errno = err;
3667 				goto done;
3668 			}
3669 
3670 			if (pr_setrctl(p.pr, rname, NULL, rblk, RCTL_INSERT)) {
3671 				res = Z_SYSTEM;
3672 				goto done;
3673 			}
3674 		}
3675 	}
3676 
3677 done:
3678 	release_process(p.pr);
3679 	free(rblk);
3680 	(void) zonecfg_endrctlent(handle);
3681 
3682 	return (res);
3683 }
3684 
3685 static const xmlChar *
3686 nm_to_dtd(char *nm)
3687 {
3688 	if (strcmp(nm, "device") == 0)
3689 		return (DTD_ELEM_DEVICE);
3690 	if (strcmp(nm, "fs") == 0)
3691 		return (DTD_ELEM_FS);
3692 	if (strcmp(nm, "inherit-pkg-dir") == 0)
3693 		return (DTD_ELEM_IPD);
3694 	if (strcmp(nm, "net") == 0)
3695 		return (DTD_ELEM_NET);
3696 	if (strcmp(nm, "attr") == 0)
3697 		return (DTD_ELEM_ATTR);
3698 	if (strcmp(nm, "rctl") == 0)
3699 		return (DTD_ELEM_RCTL);
3700 	if (strcmp(nm, "dataset") == 0)
3701 		return (DTD_ELEM_DATASET);
3702 
3703 	return (NULL);
3704 }
3705 
3706 int
3707 zonecfg_num_resources(zone_dochandle_t handle, char *rsrc)
3708 {
3709 	int num = 0;
3710 	const xmlChar *dtd;
3711 	xmlNodePtr cur;
3712 
3713 	if ((dtd = nm_to_dtd(rsrc)) == NULL)
3714 		return (num);
3715 
3716 	if (zonecfg_setent(handle) != Z_OK)
3717 		return (num);
3718 
3719 	for (cur = handle->zone_dh_cur; cur != NULL; cur = cur->next)
3720 		if (xmlStrcmp(cur->name, dtd) == 0)
3721 			num++;
3722 
3723 	(void) zonecfg_endent(handle);
3724 
3725 	return (num);
3726 }
3727 
3728 int
3729 zonecfg_del_all_resources(zone_dochandle_t handle, char *rsrc)
3730 {
3731 	int err;
3732 	const xmlChar *dtd;
3733 	xmlNodePtr cur;
3734 
3735 	if ((dtd = nm_to_dtd(rsrc)) == NULL)
3736 		return (Z_NO_RESOURCE_TYPE);
3737 
3738 	if ((err = zonecfg_setent(handle)) != Z_OK)
3739 		return (err);
3740 
3741 	cur = handle->zone_dh_cur;
3742 	while (cur != NULL) {
3743 		xmlNodePtr tmp;
3744 
3745 		if (xmlStrcmp(cur->name, dtd)) {
3746 			cur = cur->next;
3747 			continue;
3748 		}
3749 
3750 		tmp = cur->next;
3751 		xmlUnlinkNode(cur);
3752 		xmlFreeNode(cur);
3753 		cur = tmp;
3754 	}
3755 
3756 	(void) zonecfg_endent(handle);
3757 	return (Z_OK);
3758 }
3759 
3760 static boolean_t
3761 valid_uint(char *s, uint64_t *n)
3762 {
3763 	char *endp;
3764 
3765 	/* strtoull accepts '-'?! so we want to flag that as an error */
3766 	if (strchr(s, '-') != NULL)
3767 		return (B_FALSE);
3768 
3769 	errno = 0;
3770 	*n = strtoull(s, &endp, 10);
3771 
3772 	if (errno != 0 || *endp != '\0')
3773 		return (B_FALSE);
3774 	return (B_TRUE);
3775 }
3776 
3777 /*
3778  * Convert a string representing a number (possibly a fraction) into an integer.
3779  * The string can have a modifier (K, M, G or T).   The modifiers are treated
3780  * as powers of two (not 10).
3781  */
3782 int
3783 zonecfg_str_to_bytes(char *str, uint64_t *bytes)
3784 {
3785 	long double val;
3786 	char *unitp;
3787 	uint64_t scale;
3788 
3789 	if ((val = strtold(str, &unitp)) < 0)
3790 		return (-1);
3791 
3792 	/* remove any leading white space from units string */
3793 	while (isspace(*unitp) != 0)
3794 		++unitp;
3795 
3796 	/* if no units explicitly set, error */
3797 	if (unitp == NULL || *unitp == '\0') {
3798 		scale = 1;
3799 	} else {
3800 		int i;
3801 		char *units[] = {"K", "M", "G", "T", NULL};
3802 
3803 		scale = 1024;
3804 
3805 		/* update scale based on units */
3806 		for (i = 0; units[i] != NULL; i++) {
3807 			if (strcasecmp(unitp, units[i]) == 0)
3808 				break;
3809 			scale <<= 10;
3810 		}
3811 
3812 		if (units[i] == NULL)
3813 			return (-1);
3814 	}
3815 
3816 	*bytes = (uint64_t)(val * scale);
3817 	return (0);
3818 }
3819 
3820 boolean_t
3821 zonecfg_valid_ncpus(char *lowstr, char *highstr)
3822 {
3823 	uint64_t low, high;
3824 
3825 	if (!valid_uint(lowstr, &low) || !valid_uint(highstr, &high) ||
3826 	    low < 1 || low > high)
3827 		return (B_FALSE);
3828 
3829 	return (B_TRUE);
3830 }
3831 
3832 boolean_t
3833 zonecfg_valid_importance(char *impstr)
3834 {
3835 	uint64_t num;
3836 
3837 	if (!valid_uint(impstr, &num))
3838 		return (B_FALSE);
3839 
3840 	return (B_TRUE);
3841 }
3842 
3843 boolean_t
3844 zonecfg_valid_alias_limit(char *name, char *limitstr, uint64_t *limit)
3845 {
3846 	int i;
3847 
3848 	for (i = 0; aliases[i].shortname != NULL; i++)
3849 		if (strcmp(name, aliases[i].shortname) == 0)
3850 			break;
3851 
3852 	if (aliases[i].shortname == NULL)
3853 		return (B_FALSE);
3854 
3855 	if (!valid_uint(limitstr, limit) || *limit < aliases[i].low_limit)
3856 		return (B_FALSE);
3857 
3858 	return (B_TRUE);
3859 }
3860 
3861 boolean_t
3862 zonecfg_valid_memlimit(char *memstr, uint64_t *mem_val)
3863 {
3864 	if (zonecfg_str_to_bytes(memstr, mem_val) != 0)
3865 		return (B_FALSE);
3866 
3867 	return (B_TRUE);
3868 }
3869 
3870 static int
3871 zerr_pool(char *pool_err, int err_size, int res)
3872 {
3873 	(void) strlcpy(pool_err, pool_strerror(pool_error()), err_size);
3874 	return (res);
3875 }
3876 
3877 static int
3878 create_tmp_pset(char *pool_err, int err_size, pool_conf_t *pconf, pool_t *pool,
3879     char *name, int min, int max)
3880 {
3881 	pool_resource_t *res;
3882 	pool_elem_t *elem;
3883 	pool_value_t *val;
3884 
3885 	if ((res = pool_resource_create(pconf, "pset", name)) == NULL)
3886 		return (zerr_pool(pool_err, err_size, Z_POOL));
3887 
3888 	if (pool_associate(pconf, pool, res) != PO_SUCCESS)
3889 		return (zerr_pool(pool_err, err_size, Z_POOL));
3890 
3891 	if ((elem = pool_resource_to_elem(pconf, res)) == NULL)
3892 		return (zerr_pool(pool_err, err_size, Z_POOL));
3893 
3894 	if ((val = pool_value_alloc()) == NULL)
3895 		return (zerr_pool(pool_err, err_size, Z_POOL));
3896 
3897 	/* set the maximum number of cpus for the pset */
3898 	pool_value_set_uint64(val, (uint64_t)max);
3899 
3900 	if (pool_put_property(pconf, elem, "pset.max", val) != PO_SUCCESS) {
3901 		pool_value_free(val);
3902 		return (zerr_pool(pool_err, err_size, Z_POOL));
3903 	}
3904 
3905 	/* set the minimum number of cpus for the pset */
3906 	pool_value_set_uint64(val, (uint64_t)min);
3907 
3908 	if (pool_put_property(pconf, elem, "pset.min", val) != PO_SUCCESS) {
3909 		pool_value_free(val);
3910 		return (zerr_pool(pool_err, err_size, Z_POOL));
3911 	}
3912 
3913 	pool_value_free(val);
3914 
3915 	return (Z_OK);
3916 }
3917 
3918 static int
3919 create_tmp_pool(char *pool_err, int err_size, pool_conf_t *pconf, char *name,
3920     struct zone_psettab *pset_tab)
3921 {
3922 	pool_t *pool;
3923 	int res = Z_OK;
3924 
3925 	/* create a temporary pool configuration */
3926 	if (pool_conf_open(pconf, NULL, PO_TEMP) != PO_SUCCESS) {
3927 		res = zerr_pool(pool_err, err_size, Z_POOL);
3928 		return (res);
3929 	}
3930 
3931 	if ((pool = pool_create(pconf, name)) == NULL) {
3932 		res = zerr_pool(pool_err, err_size, Z_POOL_CREATE);
3933 		goto done;
3934 	}
3935 
3936 	/* set pool importance */
3937 	if (pset_tab->zone_importance[0] != '\0') {
3938 		pool_elem_t *elem;
3939 		pool_value_t *val;
3940 
3941 		if ((elem = pool_to_elem(pconf, pool)) == NULL) {
3942 			res = zerr_pool(pool_err, err_size, Z_POOL);
3943 			goto done;
3944 		}
3945 
3946 		if ((val = pool_value_alloc()) == NULL) {
3947 			res = zerr_pool(pool_err, err_size, Z_POOL);
3948 			goto done;
3949 		}
3950 
3951 		pool_value_set_int64(val,
3952 		    (int64_t)atoi(pset_tab->zone_importance));
3953 
3954 		if (pool_put_property(pconf, elem, "pool.importance", val)
3955 		    != PO_SUCCESS) {
3956 			res = zerr_pool(pool_err, err_size, Z_POOL);
3957 			pool_value_free(val);
3958 			goto done;
3959 		}
3960 
3961 		pool_value_free(val);
3962 	}
3963 
3964 	if ((res = create_tmp_pset(pool_err, err_size, pconf, pool, name,
3965 	    atoi(pset_tab->zone_ncpu_min),
3966 	    atoi(pset_tab->zone_ncpu_max))) != Z_OK)
3967 		goto done;
3968 
3969 	/* validation */
3970 	if (pool_conf_status(pconf) == POF_INVALID) {
3971 		res = zerr_pool(pool_err, err_size, Z_POOL);
3972 		goto done;
3973 	}
3974 
3975 	/*
3976 	 * This validation is the one we expect to fail if the user specified
3977 	 * an invalid configuration (too many cpus) for this system.
3978 	 */
3979 	if (pool_conf_validate(pconf, POV_RUNTIME) != PO_SUCCESS) {
3980 		res = zerr_pool(pool_err, err_size, Z_POOL_CREATE);
3981 		goto done;
3982 	}
3983 
3984 	/*
3985 	 * Commit the dynamic configuration but not the pool configuration
3986 	 * file.
3987 	 */
3988 	if (pool_conf_commit(pconf, 1) != PO_SUCCESS)
3989 		res = zerr_pool(pool_err, err_size, Z_POOL);
3990 
3991 done:
3992 	(void) pool_conf_close(pconf);
3993 	return (res);
3994 }
3995 
3996 static int
3997 get_running_tmp_pset(pool_conf_t *pconf, pool_t *pool, pool_resource_t *pset,
3998     struct zone_psettab *pset_tab)
3999 {
4000 	int nfound = 0;
4001 	pool_elem_t *pe;
4002 	pool_value_t *pv = pool_value_alloc();
4003 	uint64_t val_uint;
4004 
4005 	if (pool != NULL) {
4006 		pe = pool_to_elem(pconf, pool);
4007 		if (pool_get_property(pconf, pe, "pool.importance", pv)
4008 		    != POC_INVAL) {
4009 			int64_t val_int;
4010 
4011 			(void) pool_value_get_int64(pv, &val_int);
4012 			(void) snprintf(pset_tab->zone_importance,
4013 			    sizeof (pset_tab->zone_importance), "%d", val_int);
4014 			nfound++;
4015 		}
4016 	}
4017 
4018 	if (pset != NULL) {
4019 		pe = pool_resource_to_elem(pconf, pset);
4020 		if (pool_get_property(pconf, pe, "pset.min", pv) != POC_INVAL) {
4021 			(void) pool_value_get_uint64(pv, &val_uint);
4022 			(void) snprintf(pset_tab->zone_ncpu_min,
4023 			    sizeof (pset_tab->zone_ncpu_min), "%u", val_uint);
4024 			nfound++;
4025 		}
4026 
4027 		if (pool_get_property(pconf, pe, "pset.max", pv) != POC_INVAL) {
4028 			(void) pool_value_get_uint64(pv, &val_uint);
4029 			(void) snprintf(pset_tab->zone_ncpu_max,
4030 			    sizeof (pset_tab->zone_ncpu_max), "%u", val_uint);
4031 			nfound++;
4032 		}
4033 	}
4034 
4035 	pool_value_free(pv);
4036 
4037 	if (nfound == 3)
4038 		return (PO_SUCCESS);
4039 
4040 	return (PO_FAIL);
4041 }
4042 
4043 /*
4044  * Determine if a tmp pool is configured and if so, if the configuration is
4045  * still valid or if it has been changed since the tmp pool was created.
4046  * If the tmp pool configuration is no longer valid, delete the tmp pool.
4047  *
4048  * Set *valid=B_TRUE if there is an existing, valid tmp pool configuration.
4049  */
4050 static int
4051 verify_del_tmp_pool(pool_conf_t *pconf, char *tmp_name, char *pool_err,
4052     int err_size, struct zone_psettab *pset_tab, boolean_t *exists)
4053 {
4054 	int res = Z_OK;
4055 	pool_t *pool;
4056 	pool_resource_t *pset;
4057 	struct zone_psettab pset_current;
4058 
4059 	*exists = B_FALSE;
4060 
4061 	if (pool_conf_open(pconf, pool_dynamic_location(), PO_RDWR)
4062 	    != PO_SUCCESS) {
4063 		res = zerr_pool(pool_err, err_size, Z_POOL);
4064 		return (res);
4065 	}
4066 
4067 	pool = pool_get_pool(pconf, tmp_name);
4068 	pset = pool_get_resource(pconf, "pset", tmp_name);
4069 
4070 	if (pool == NULL && pset == NULL) {
4071 		/* no tmp pool configured */
4072 		goto done;
4073 	}
4074 
4075 	/*
4076 	 * If an existing tmp pool for this zone is configured with the proper
4077 	 * settings, then the tmp pool is valid.
4078 	 */
4079 	if (get_running_tmp_pset(pconf, pool, pset, &pset_current)
4080 	    == PO_SUCCESS &&
4081 	    strcmp(pset_tab->zone_ncpu_min,
4082 	    pset_current.zone_ncpu_min) == 0 &&
4083 	    strcmp(pset_tab->zone_ncpu_max,
4084 	    pset_current.zone_ncpu_max) == 0 &&
4085 	    strcmp(pset_tab->zone_importance,
4086 	    pset_current.zone_importance) == 0) {
4087 		*exists = B_TRUE;
4088 
4089 	} else {
4090 		/*
4091 		 * An out-of-date tmp pool configuration exists.  Delete it
4092 		 * so that we can create the correct tmp pool config.
4093 		 */
4094 		if (pset != NULL &&
4095 		    pool_resource_destroy(pconf, pset) != PO_SUCCESS) {
4096 			res = zerr_pool(pool_err, err_size, Z_POOL);
4097 			goto done;
4098 		}
4099 
4100 		if (pool != NULL &&
4101 		    pool_destroy(pconf, pool) != PO_SUCCESS) {
4102 			res = zerr_pool(pool_err, err_size, Z_POOL);
4103 			goto done;
4104 		}
4105 
4106 		/* commit dynamic config */
4107 		if (pool_conf_commit(pconf, 0) != PO_SUCCESS)
4108 			res = zerr_pool(pool_err, err_size, Z_POOL);
4109 	}
4110 
4111 done:
4112 	(void) pool_conf_close(pconf);
4113 
4114 	return (res);
4115 }
4116 
4117 /*
4118  * Destroy any existing tmp pool.
4119  */
4120 int
4121 zonecfg_destroy_tmp_pool(char *zone_name, char *pool_err, int err_size)
4122 {
4123 	int status;
4124 	int res = Z_OK;
4125 	pool_conf_t *pconf;
4126 	pool_t *pool;
4127 	pool_resource_t *pset;
4128 	char tmp_name[MAX_TMP_POOL_NAME];
4129 
4130 	/* if pools not enabled then nothing to do */
4131 	if (pool_get_status(&status) != PO_SUCCESS || status != POOL_ENABLED)
4132 		return (Z_OK);
4133 
4134 	if ((pconf = pool_conf_alloc()) == NULL)
4135 		return (zerr_pool(pool_err, err_size, Z_POOL));
4136 
4137 	(void) snprintf(tmp_name, sizeof (tmp_name), TMP_POOL_NAME, zone_name);
4138 
4139 	if (pool_conf_open(pconf, pool_dynamic_location(), PO_RDWR)
4140 	    != PO_SUCCESS) {
4141 		res = zerr_pool(pool_err, err_size, Z_POOL);
4142 		pool_conf_free(pconf);
4143 		return (res);
4144 	}
4145 
4146 	pool = pool_get_pool(pconf, tmp_name);
4147 	pset = pool_get_resource(pconf, "pset", tmp_name);
4148 
4149 	if (pool == NULL && pset == NULL) {
4150 		/* nothing to destroy, we're done */
4151 		goto done;
4152 	}
4153 
4154 	if (pset != NULL && pool_resource_destroy(pconf, pset) != PO_SUCCESS) {
4155 		res = zerr_pool(pool_err, err_size, Z_POOL);
4156 		goto done;
4157 	}
4158 
4159 	if (pool != NULL && pool_destroy(pconf, pool) != PO_SUCCESS) {
4160 		res = zerr_pool(pool_err, err_size, Z_POOL);
4161 		goto done;
4162 	}
4163 
4164 	/* commit dynamic config */
4165 	if (pool_conf_commit(pconf, 0) != PO_SUCCESS)
4166 		res = zerr_pool(pool_err, err_size, Z_POOL);
4167 
4168 done:
4169 	(void) pool_conf_close(pconf);
4170 	pool_conf_free(pconf);
4171 
4172 	return (res);
4173 }
4174 
4175 /*
4176  * Attempt to bind to a tmp pool for this zone.  If there is no tmp pool
4177  * configured, we just return Z_OK.
4178  *
4179  * We either attempt to create the tmp pool for this zone or rebind to an
4180  * existing tmp pool for this zone.
4181  *
4182  * Rebinding is used when a zone with a tmp pool reboots so that we don't have
4183  * to recreate the tmp pool.  To do this we need to be sure we work correctly
4184  * for the following cases:
4185  *
4186  *	- there is an existing, properly configured tmp pool.
4187  *	- zonecfg added tmp pool after zone was booted, must now create.
4188  *	- zonecfg updated tmp pool config after zone was booted, in this case
4189  *	  we destroy the old tmp pool and create a new one.
4190  */
4191 int
4192 zonecfg_bind_tmp_pool(zone_dochandle_t handle, zoneid_t zoneid, char *pool_err,
4193     int err_size)
4194 {
4195 	struct zone_psettab pset_tab;
4196 	int err;
4197 	int status;
4198 	pool_conf_t *pconf;
4199 	boolean_t exists;
4200 	char zone_name[ZONENAME_MAX];
4201 	char tmp_name[MAX_TMP_POOL_NAME];
4202 
4203 	(void) getzonenamebyid(zoneid, zone_name, sizeof (zone_name));
4204 
4205 	err = zonecfg_lookup_pset(handle, &pset_tab);
4206 
4207 	/* if no temporary pool configured, we're done */
4208 	if (err == Z_NO_ENTRY)
4209 		return (Z_OK);
4210 
4211 	/*
4212 	 * importance might not have a value but we need to validate it here,
4213 	 * so set the default.
4214 	 */
4215 	if (pset_tab.zone_importance[0] == '\0')
4216 		(void) strlcpy(pset_tab.zone_importance, "1",
4217 		    sizeof (pset_tab.zone_importance));
4218 
4219 	/* if pools not enabled, enable them now */
4220 	if (pool_get_status(&status) != PO_SUCCESS || status != POOL_ENABLED) {
4221 		if (pool_set_status(POOL_ENABLED) != PO_SUCCESS)
4222 			return (Z_POOL_ENABLE);
4223 	}
4224 
4225 	if ((pconf = pool_conf_alloc()) == NULL)
4226 		return (zerr_pool(pool_err, err_size, Z_POOL));
4227 
4228 	(void) snprintf(tmp_name, sizeof (tmp_name), TMP_POOL_NAME, zone_name);
4229 
4230 	/*
4231 	 * Check if a valid tmp pool/pset already exists.  If so, we just
4232 	 * reuse it.
4233 	 */
4234 	if ((err = verify_del_tmp_pool(pconf, tmp_name, pool_err, err_size,
4235 	    &pset_tab, &exists)) != Z_OK) {
4236 		pool_conf_free(pconf);
4237 		return (err);
4238 	}
4239 
4240 	if (!exists)
4241 		err = create_tmp_pool(pool_err, err_size, pconf, tmp_name,
4242 		    &pset_tab);
4243 
4244 	pool_conf_free(pconf);
4245 
4246 	if (err != Z_OK)
4247 		return (err);
4248 
4249 	/* Bind the zone to the pool. */
4250 	if (pool_set_binding(tmp_name, P_ZONEID, zoneid) != PO_SUCCESS)
4251 		return (zerr_pool(pool_err, err_size, Z_POOL_BIND));
4252 
4253 	return (Z_OK);
4254 }
4255 
4256 /*
4257  * Attempt to bind to a permanent pool for this zone.  If there is no
4258  * permanent pool configured, we just return Z_OK.
4259  */
4260 int
4261 zonecfg_bind_pool(zone_dochandle_t handle, zoneid_t zoneid, char *pool_err,
4262     int err_size)
4263 {
4264 	pool_conf_t *poolconf;
4265 	pool_t *pool;
4266 	char poolname[MAXPATHLEN];
4267 	int status;
4268 	int error;
4269 
4270 	/*
4271 	 * Find the pool mentioned in the zone configuration, and bind to it.
4272 	 */
4273 	error = zonecfg_get_pool(handle, poolname, sizeof (poolname));
4274 	if (error == Z_NO_ENTRY || (error == Z_OK && strlen(poolname) == 0)) {
4275 		/*
4276 		 * The property is not set on the zone, so the pool
4277 		 * should be bound to the default pool.  But that's
4278 		 * already done by the kernel, so we can just return.
4279 		 */
4280 		return (Z_OK);
4281 	}
4282 	if (error != Z_OK) {
4283 		/*
4284 		 * Not an error, even though it shouldn't be happening.
4285 		 */
4286 		return (Z_OK);
4287 	}
4288 	/*
4289 	 * Don't do anything if pools aren't enabled.
4290 	 */
4291 	if (pool_get_status(&status) != PO_SUCCESS || status != POOL_ENABLED)
4292 		return (Z_POOLS_NOT_ACTIVE);
4293 
4294 	/*
4295 	 * Try to provide a sane error message if the requested pool doesn't
4296 	 * exist.
4297 	 */
4298 	if ((poolconf = pool_conf_alloc()) == NULL)
4299 		return (zerr_pool(pool_err, err_size, Z_POOL));
4300 
4301 	if (pool_conf_open(poolconf, pool_dynamic_location(), PO_RDONLY) !=
4302 	    PO_SUCCESS) {
4303 		pool_conf_free(poolconf);
4304 		return (zerr_pool(pool_err, err_size, Z_POOL));
4305 	}
4306 	pool = pool_get_pool(poolconf, poolname);
4307 	(void) pool_conf_close(poolconf);
4308 	pool_conf_free(poolconf);
4309 	if (pool == NULL)
4310 		return (Z_NO_POOL);
4311 
4312 	/*
4313 	 * Bind the zone to the pool.
4314 	 */
4315 	if (pool_set_binding(poolname, P_ZONEID, zoneid) != PO_SUCCESS) {
4316 		/* if bind fails, return poolname for the error msg */
4317 		(void) strlcpy(pool_err, poolname, err_size);
4318 		return (Z_POOL_BIND);
4319 	}
4320 
4321 	return (Z_OK);
4322 }
4323 
4324 
4325 static boolean_t
4326 svc_enabled(char *svc_name)
4327 {
4328 	scf_simple_prop_t	*prop;
4329 	boolean_t		found = B_FALSE;
4330 
4331 	prop = scf_simple_prop_get(NULL, svc_name, SCF_PG_GENERAL,
4332 	    SCF_PROPERTY_ENABLED);
4333 
4334 	if (scf_simple_prop_numvalues(prop) == 1 &&
4335 	    *scf_simple_prop_next_boolean(prop) != 0)
4336 		found = B_TRUE;
4337 
4338 	scf_simple_prop_free(prop);
4339 
4340 	return (found);
4341 }
4342 
4343 /*
4344  * If the zone has capped-memory, make sure the rcap service is enabled.
4345  */
4346 int
4347 zonecfg_enable_rcapd(char *err, int size)
4348 {
4349 	if (!svc_enabled(RCAP_SERVICE) &&
4350 	    smf_enable_instance(RCAP_SERVICE, 0) == -1) {
4351 		(void) strlcpy(err, scf_strerror(scf_error()), size);
4352 		return (Z_SYSTEM);
4353 	}
4354 
4355 	return (Z_OK);
4356 }
4357 
4358 /*
4359  * Return true if pset has cpu range specified and poold is not enabled.
4360  */
4361 boolean_t
4362 zonecfg_warn_poold(zone_dochandle_t handle)
4363 {
4364 	struct zone_psettab pset_tab;
4365 	int min, max;
4366 	int err;
4367 
4368 	err = zonecfg_lookup_pset(handle, &pset_tab);
4369 
4370 	/* if no temporary pool configured, we're done */
4371 	if (err == Z_NO_ENTRY)
4372 		return (B_FALSE);
4373 
4374 	min = atoi(pset_tab.zone_ncpu_min);
4375 	max = atoi(pset_tab.zone_ncpu_max);
4376 
4377 	/* range not specified, no need for poold */
4378 	if (min == max)
4379 		return (B_FALSE);
4380 
4381 	/* we have a range, check if poold service is enabled */
4382 	if (svc_enabled(POOLD_SERVICE))
4383 		return (B_FALSE);
4384 
4385 	return (B_TRUE);
4386 }
4387 
4388 static int
4389 get_pool_sched_class(char *poolname, char *class, int clsize)
4390 {
4391 	int status;
4392 	pool_conf_t *poolconf;
4393 	pool_t *pool;
4394 	pool_elem_t *pe;
4395 	pool_value_t *pv = pool_value_alloc();
4396 	const char *sched_str;
4397 
4398 	if (pool_get_status(&status) != PO_SUCCESS || status != POOL_ENABLED)
4399 		return (Z_NO_POOL);
4400 
4401 	if ((poolconf = pool_conf_alloc()) == NULL)
4402 		return (Z_NO_POOL);
4403 
4404 	if (pool_conf_open(poolconf, pool_dynamic_location(), PO_RDONLY) !=
4405 	    PO_SUCCESS) {
4406 		pool_conf_free(poolconf);
4407 		return (Z_NO_POOL);
4408 	}
4409 
4410 	if ((pool = pool_get_pool(poolconf, poolname)) == NULL) {
4411 		(void) pool_conf_close(poolconf);
4412 		pool_conf_free(poolconf);
4413 		return (Z_NO_POOL);
4414 	}
4415 
4416 	pe = pool_to_elem(poolconf, pool);
4417 	if (pool_get_property(poolconf, pe, "pool.scheduler", pv)
4418 	    != POC_INVAL) {
4419 		(void) pool_value_get_string(pv, &sched_str);
4420 		if (strlcpy(class, sched_str, clsize) >= clsize)
4421 			return (Z_TOO_BIG);
4422 	}
4423 
4424 	(void) pool_conf_close(poolconf);
4425 	pool_conf_free(poolconf);
4426 	return (Z_OK);
4427 }
4428 
4429 /*
4430  * Get the default scheduling class for the zone.  This will either be the
4431  * class set on the zone's pool or the system default scheduling class.
4432  */
4433 int
4434 zonecfg_get_dflt_sched_class(zone_dochandle_t handle, char *class, int clsize)
4435 {
4436 	char poolname[MAXPATHLEN];
4437 
4438 	if (zonecfg_get_pool(handle, poolname, sizeof (poolname)) == Z_OK) {
4439 		/* check if the zone's pool specified a sched class */
4440 		if (get_pool_sched_class(poolname, class, clsize) == Z_OK)
4441 			return (Z_OK);
4442 	}
4443 
4444 	if (priocntl(0, 0, PC_GETDFLCL, class, (uint64_t)clsize) == -1)
4445 		return (Z_TOO_BIG);
4446 
4447 	return (Z_OK);
4448 }
4449 
4450 int
4451 zonecfg_setfsent(zone_dochandle_t handle)
4452 {
4453 	return (zonecfg_setent(handle));
4454 }
4455 
4456 int
4457 zonecfg_getfsent(zone_dochandle_t handle, struct zone_fstab *tabptr)
4458 {
4459 	xmlNodePtr cur, options;
4460 	char options_str[MAX_MNTOPT_STR];
4461 	int err;
4462 
4463 	if (handle == NULL)
4464 		return (Z_INVAL);
4465 
4466 	if ((cur = handle->zone_dh_cur) == NULL)
4467 		return (Z_NO_ENTRY);
4468 
4469 	for (; cur != NULL; cur = cur->next)
4470 		if (!xmlStrcmp(cur->name, DTD_ELEM_FS))
4471 			break;
4472 	if (cur == NULL) {
4473 		handle->zone_dh_cur = handle->zone_dh_top;
4474 		return (Z_NO_ENTRY);
4475 	}
4476 
4477 	if ((err = fetchprop(cur, DTD_ATTR_SPECIAL, tabptr->zone_fs_special,
4478 	    sizeof (tabptr->zone_fs_special))) != Z_OK) {
4479 		handle->zone_dh_cur = handle->zone_dh_top;
4480 		return (err);
4481 	}
4482 
4483 	if ((err = fetchprop(cur, DTD_ATTR_RAW, tabptr->zone_fs_raw,
4484 	    sizeof (tabptr->zone_fs_raw))) != Z_OK) {
4485 		handle->zone_dh_cur = handle->zone_dh_top;
4486 		return (err);
4487 	}
4488 
4489 	if ((err = fetchprop(cur, DTD_ATTR_DIR, tabptr->zone_fs_dir,
4490 	    sizeof (tabptr->zone_fs_dir))) != Z_OK) {
4491 		handle->zone_dh_cur = handle->zone_dh_top;
4492 		return (err);
4493 	}
4494 
4495 	if ((err = fetchprop(cur, DTD_ATTR_TYPE, tabptr->zone_fs_type,
4496 	    sizeof (tabptr->zone_fs_type))) != Z_OK) {
4497 		handle->zone_dh_cur = handle->zone_dh_top;
4498 		return (err);
4499 	}
4500 
4501 	/* OK for options to be NULL */
4502 	tabptr->zone_fs_options = NULL;
4503 	for (options = cur->xmlChildrenNode; options != NULL;
4504 	    options = options->next) {
4505 		if (fetchprop(options, DTD_ATTR_NAME, options_str,
4506 		    sizeof (options_str)) != Z_OK)
4507 			break;
4508 		if (zonecfg_add_fs_option(tabptr, options_str) != Z_OK)
4509 			break;
4510 	}
4511 
4512 	handle->zone_dh_cur = cur->next;
4513 	return (Z_OK);
4514 }
4515 
4516 int
4517 zonecfg_endfsent(zone_dochandle_t handle)
4518 {
4519 	return (zonecfg_endent(handle));
4520 }
4521 
4522 int
4523 zonecfg_setipdent(zone_dochandle_t handle)
4524 {
4525 	return (zonecfg_setent(handle));
4526 }
4527 
4528 int
4529 zonecfg_getipdent(zone_dochandle_t handle, struct zone_fstab *tabptr)
4530 {
4531 	xmlNodePtr cur;
4532 	int err;
4533 
4534 	if (handle == NULL)
4535 		return (Z_INVAL);
4536 
4537 	if ((cur = handle->zone_dh_cur) == NULL)
4538 		return (Z_NO_ENTRY);
4539 
4540 	for (; cur != NULL; cur = cur->next)
4541 		if (!xmlStrcmp(cur->name, DTD_ELEM_IPD))
4542 			break;
4543 	if (cur == NULL) {
4544 		handle->zone_dh_cur = handle->zone_dh_top;
4545 		return (Z_NO_ENTRY);
4546 	}
4547 
4548 	if ((err = fetchprop(cur, DTD_ATTR_DIR, tabptr->zone_fs_dir,
4549 	    sizeof (tabptr->zone_fs_dir))) != Z_OK) {
4550 		handle->zone_dh_cur = handle->zone_dh_top;
4551 		return (err);
4552 	}
4553 
4554 	handle->zone_dh_cur = cur->next;
4555 	return (Z_OK);
4556 }
4557 
4558 int
4559 zonecfg_endipdent(zone_dochandle_t handle)
4560 {
4561 	return (zonecfg_endent(handle));
4562 }
4563 
4564 int
4565 zonecfg_setnwifent(zone_dochandle_t handle)
4566 {
4567 	return (zonecfg_setent(handle));
4568 }
4569 
4570 int
4571 zonecfg_getnwifent(zone_dochandle_t handle, struct zone_nwiftab *tabptr)
4572 {
4573 	xmlNodePtr cur;
4574 	int err;
4575 
4576 	if (handle == NULL)
4577 		return (Z_INVAL);
4578 
4579 	if ((cur = handle->zone_dh_cur) == NULL)
4580 		return (Z_NO_ENTRY);
4581 
4582 	for (; cur != NULL; cur = cur->next)
4583 		if (!xmlStrcmp(cur->name, DTD_ELEM_NET))
4584 			break;
4585 	if (cur == NULL) {
4586 		handle->zone_dh_cur = handle->zone_dh_top;
4587 		return (Z_NO_ENTRY);
4588 	}
4589 
4590 	if ((err = fetchprop(cur, DTD_ATTR_ADDRESS, tabptr->zone_nwif_address,
4591 	    sizeof (tabptr->zone_nwif_address))) != Z_OK) {
4592 		handle->zone_dh_cur = handle->zone_dh_top;
4593 		return (err);
4594 	}
4595 
4596 	if ((err = fetchprop(cur, DTD_ATTR_PHYSICAL, tabptr->zone_nwif_physical,
4597 	    sizeof (tabptr->zone_nwif_physical))) != Z_OK) {
4598 		handle->zone_dh_cur = handle->zone_dh_top;
4599 		return (err);
4600 	}
4601 
4602 	if ((err = fetchprop(cur, DTD_ATTR_DEFROUTER,
4603 	    tabptr->zone_nwif_defrouter,
4604 	    sizeof (tabptr->zone_nwif_defrouter))) != Z_OK) {
4605 		handle->zone_dh_cur = handle->zone_dh_top;
4606 		return (err);
4607 	}
4608 
4609 	handle->zone_dh_cur = cur->next;
4610 	return (Z_OK);
4611 }
4612 
4613 int
4614 zonecfg_endnwifent(zone_dochandle_t handle)
4615 {
4616 	return (zonecfg_endent(handle));
4617 }
4618 
4619 int
4620 zonecfg_setdevent(zone_dochandle_t handle)
4621 {
4622 	return (zonecfg_setent(handle));
4623 }
4624 
4625 int
4626 zonecfg_getdevent(zone_dochandle_t handle, struct zone_devtab *tabptr)
4627 {
4628 	xmlNodePtr cur;
4629 	int err;
4630 
4631 	if (handle == NULL)
4632 		return (Z_INVAL);
4633 
4634 	if ((cur = handle->zone_dh_cur) == NULL)
4635 		return (Z_NO_ENTRY);
4636 
4637 	for (; cur != NULL; cur = cur->next)
4638 		if (!xmlStrcmp(cur->name, DTD_ELEM_DEVICE))
4639 			break;
4640 	if (cur == NULL) {
4641 		handle->zone_dh_cur = handle->zone_dh_top;
4642 		return (Z_NO_ENTRY);
4643 	}
4644 
4645 	if ((err = fetchprop(cur, DTD_ATTR_MATCH, tabptr->zone_dev_match,
4646 	    sizeof (tabptr->zone_dev_match))) != Z_OK) {
4647 		handle->zone_dh_cur = handle->zone_dh_top;
4648 		return (err);
4649 	}
4650 
4651 	handle->zone_dh_cur = cur->next;
4652 	return (Z_OK);
4653 }
4654 
4655 int
4656 zonecfg_enddevent(zone_dochandle_t handle)
4657 {
4658 	return (zonecfg_endent(handle));
4659 }
4660 
4661 int
4662 zonecfg_setrctlent(zone_dochandle_t handle)
4663 {
4664 	return (zonecfg_setent(handle));
4665 }
4666 
4667 int
4668 zonecfg_getrctlent(zone_dochandle_t handle, struct zone_rctltab *tabptr)
4669 {
4670 	xmlNodePtr cur, val;
4671 	struct zone_rctlvaltab *valptr;
4672 	int err;
4673 
4674 	if (handle == NULL)
4675 		return (Z_INVAL);
4676 
4677 	if ((cur = handle->zone_dh_cur) == NULL)
4678 		return (Z_NO_ENTRY);
4679 
4680 	for (; cur != NULL; cur = cur->next)
4681 		if (!xmlStrcmp(cur->name, DTD_ELEM_RCTL))
4682 			break;
4683 	if (cur == NULL) {
4684 		handle->zone_dh_cur = handle->zone_dh_top;
4685 		return (Z_NO_ENTRY);
4686 	}
4687 
4688 	if ((err = fetchprop(cur, DTD_ATTR_NAME, tabptr->zone_rctl_name,
4689 	    sizeof (tabptr->zone_rctl_name))) != Z_OK) {
4690 		handle->zone_dh_cur = handle->zone_dh_top;
4691 		return (err);
4692 	}
4693 
4694 	tabptr->zone_rctl_valptr = NULL;
4695 	for (val = cur->xmlChildrenNode; val != NULL; val = val->next) {
4696 		valptr = (struct zone_rctlvaltab *)malloc(
4697 		    sizeof (struct zone_rctlvaltab));
4698 		if (valptr == NULL)
4699 			return (Z_NOMEM);
4700 		if (fetchprop(val, DTD_ATTR_PRIV, valptr->zone_rctlval_priv,
4701 		    sizeof (valptr->zone_rctlval_priv)) != Z_OK)
4702 			break;
4703 		if (fetchprop(val, DTD_ATTR_LIMIT, valptr->zone_rctlval_limit,
4704 		    sizeof (valptr->zone_rctlval_limit)) != Z_OK)
4705 			break;
4706 		if (fetchprop(val, DTD_ATTR_ACTION, valptr->zone_rctlval_action,
4707 		    sizeof (valptr->zone_rctlval_action)) != Z_OK)
4708 			break;
4709 		if (zonecfg_add_rctl_value(tabptr, valptr) != Z_OK)
4710 			break;
4711 	}
4712 
4713 	handle->zone_dh_cur = cur->next;
4714 	return (Z_OK);
4715 }
4716 
4717 int
4718 zonecfg_endrctlent(zone_dochandle_t handle)
4719 {
4720 	return (zonecfg_endent(handle));
4721 }
4722 
4723 int
4724 zonecfg_setattrent(zone_dochandle_t handle)
4725 {
4726 	return (zonecfg_setent(handle));
4727 }
4728 
4729 int
4730 zonecfg_getattrent(zone_dochandle_t handle, struct zone_attrtab *tabptr)
4731 {
4732 	xmlNodePtr cur;
4733 	int err;
4734 
4735 	if (handle == NULL)
4736 		return (Z_INVAL);
4737 
4738 	if ((cur = handle->zone_dh_cur) == NULL)
4739 		return (Z_NO_ENTRY);
4740 
4741 	for (; cur != NULL; cur = cur->next)
4742 		if (!xmlStrcmp(cur->name, DTD_ELEM_ATTR))
4743 			break;
4744 	if (cur == NULL) {
4745 		handle->zone_dh_cur = handle->zone_dh_top;
4746 		return (Z_NO_ENTRY);
4747 	}
4748 
4749 	if ((err = fetchprop(cur, DTD_ATTR_NAME, tabptr->zone_attr_name,
4750 	    sizeof (tabptr->zone_attr_name))) != Z_OK) {
4751 		handle->zone_dh_cur = handle->zone_dh_top;
4752 		return (err);
4753 	}
4754 
4755 	if ((err = fetchprop(cur, DTD_ATTR_TYPE, tabptr->zone_attr_type,
4756 	    sizeof (tabptr->zone_attr_type))) != Z_OK) {
4757 		handle->zone_dh_cur = handle->zone_dh_top;
4758 		return (err);
4759 	}
4760 
4761 	if ((err = fetchprop(cur, DTD_ATTR_VALUE, tabptr->zone_attr_value,
4762 	    sizeof (tabptr->zone_attr_value))) != Z_OK) {
4763 		handle->zone_dh_cur = handle->zone_dh_top;
4764 		return (err);
4765 	}
4766 
4767 	handle->zone_dh_cur = cur->next;
4768 	return (Z_OK);
4769 }
4770 
4771 int
4772 zonecfg_endattrent(zone_dochandle_t handle)
4773 {
4774 	return (zonecfg_endent(handle));
4775 }
4776 
4777 /*
4778  * The privileges available on the system and described in privileges(5)
4779  * fall into four categories with respect to non-global zones:
4780  *
4781  *      Default set of privileges considered safe for all non-global
4782  *      zones.  These privileges are "safe" in the sense that a
4783  *      privileged process in the zone cannot affect processes in any
4784  *      other zone on the system.
4785  *
4786  *      Set of privileges not currently permitted within a non-global
4787  *      zone.  These privileges are considered by default, "unsafe,"
4788  *      and include ones which affect global resources (such as the
4789  *      system clock or physical memory) or are overly broad and cover
4790  *      more than one mechanism in the system.  In other cases, there
4791  *      has not been sufficient virtualization in the parts of the
4792  *      system the privilege covers to allow its use within a
4793  *      non-global zone.
4794  *
4795  *      Set of privileges required in order to get a zone booted and
4796  *      init(1M) started.  These cannot be removed from the zone's
4797  *      privilege set.
4798  *
4799  * All other privileges are optional and are potentially useful for
4800  * processes executing inside a non-global zone.
4801  *
4802  * When privileges are added to the system, a determination needs to be
4803  * made as to which category the privilege belongs to.  Ideally,
4804  * privileges should be fine-grained enough and the mechanisms they cover
4805  * virtualized enough so that they can be made available to non-global
4806  * zones.
4807  */
4808 
4809 /*
4810  * Define some of the tokens that priv_str_to_set(3C) recognizes.  Since
4811  * the privilege string separator can be any character, although it is
4812  * usually a comma character, define these here as well in the event that
4813  * they change or are augmented in the future.
4814  */
4815 #define	BASIC_TOKEN		"basic"
4816 #define	DEFAULT_TOKEN		"default"
4817 #define	ZONE_TOKEN		"zone"
4818 #define	TOKEN_PRIV_CHAR		','
4819 #define	TOKEN_PRIV_STR		","
4820 
4821 typedef struct priv_node {
4822 	struct priv_node	*pn_next;	/* Next privilege */
4823 	char			*pn_priv;	/* Privileges name */
4824 } priv_node_t;
4825 
4826 /* Privileges lists can differ across brands */
4827 typedef struct priv_lists {
4828 	/* Privileges considered safe for all non-global zones of a brand */
4829 	struct priv_node	*pl_default;
4830 
4831 	/* Privileges not permitted for all non-global zones of a brand */
4832 	struct priv_node	*pl_prohibited;
4833 
4834 	/* Privileges required for all non-global zones of a brand */
4835 	struct priv_node	*pl_required;
4836 
4837 	/*
4838 	 * ip-type of the zone these privileges lists apply to.
4839 	 * It is used to pass ip-type to the callback function,
4840 	 * priv_lists_cb, which has no way of getting the ip-type.
4841 	 */
4842 	const char		*pl_iptype;
4843 } priv_lists_t;
4844 
4845 static int
4846 priv_lists_cb(void *data, priv_iter_t *priv_iter)
4847 {
4848 	priv_lists_t *plp = (priv_lists_t *)data;
4849 	priv_node_t *pnp;
4850 
4851 	/* Skip this privilege if ip-type does not match */
4852 	if ((strcmp(priv_iter->pi_iptype, "all") != 0) &&
4853 	    (strcmp(priv_iter->pi_iptype, plp->pl_iptype) != 0))
4854 		return (0);
4855 
4856 	/* Allocate a new priv list node. */
4857 	if ((pnp = malloc(sizeof (*pnp))) == NULL)
4858 		return (-1);
4859 	if ((pnp->pn_priv = strdup(priv_iter->pi_name)) == NULL) {
4860 		free(pnp);
4861 		return (-1);
4862 	}
4863 
4864 	/* Insert the new priv list node into the right list */
4865 	if (strcmp(priv_iter->pi_set, "default") == 0) {
4866 		pnp->pn_next = plp->pl_default;
4867 		plp->pl_default = pnp;
4868 	} else if (strcmp(priv_iter->pi_set, "prohibited") == 0) {
4869 		pnp->pn_next = plp->pl_prohibited;
4870 		plp->pl_prohibited = pnp;
4871 	} else if (strcmp(priv_iter->pi_set, "required") == 0) {
4872 		pnp->pn_next = plp->pl_required;
4873 		plp->pl_required = pnp;
4874 	} else {
4875 		free(pnp->pn_priv);
4876 		free(pnp);
4877 		return (-1);
4878 	}
4879 	return (0);
4880 }
4881 
4882 static void
4883 priv_lists_destroy(priv_lists_t *plp)
4884 {
4885 	priv_node_t *pnp;
4886 
4887 	assert(plp != NULL);
4888 
4889 	while ((pnp = plp->pl_default) != NULL) {
4890 		plp->pl_default = pnp->pn_next;
4891 		free(pnp->pn_priv);
4892 		free(pnp);
4893 	}
4894 	while ((pnp = plp->pl_prohibited) != NULL) {
4895 		plp->pl_prohibited = pnp->pn_next;
4896 		free(pnp->pn_priv);
4897 		free(pnp);
4898 	}
4899 	while ((pnp = plp->pl_required) != NULL) {
4900 		plp->pl_required = pnp->pn_next;
4901 		free(pnp->pn_priv);
4902 		free(pnp);
4903 	}
4904 	free(plp);
4905 }
4906 
4907 static int
4908 priv_lists_create(zone_dochandle_t handle, priv_lists_t **plpp,
4909     const char *curr_iptype)
4910 {
4911 	priv_lists_t *plp;
4912 	brand_handle_t bh;
4913 	char brand[MAXNAMELEN];
4914 
4915 	if (handle != NULL) {
4916 		if (zonecfg_get_brand(handle, brand, sizeof (brand)) != 0)
4917 			return (Z_BRAND_ERROR);
4918 	} else {
4919 		(void) strlcpy(brand, NATIVE_BRAND_NAME, MAXNAMELEN);
4920 	}
4921 
4922 	if ((bh = brand_open(brand)) == NULL)
4923 		return (Z_BRAND_ERROR);
4924 
4925 	if ((plp = calloc(1, sizeof (priv_lists_t))) == NULL) {
4926 		brand_close(bh);
4927 		return (Z_NOMEM);
4928 	}
4929 
4930 	plp->pl_iptype = curr_iptype;
4931 
4932 	/* construct the privilege lists */
4933 	if (brand_config_iter_privilege(bh, priv_lists_cb, plp) != 0) {
4934 		priv_lists_destroy(plp);
4935 		brand_close(bh);
4936 		return (Z_BRAND_ERROR);
4937 	}
4938 
4939 	brand_close(bh);
4940 	*plpp = plp;
4941 	return (Z_OK);
4942 }
4943 
4944 static int
4945 get_default_privset(priv_set_t *privs, priv_lists_t *plp)
4946 {
4947 	priv_node_t *pnp;
4948 	priv_set_t *basic;
4949 
4950 	basic = priv_str_to_set(BASIC_TOKEN, TOKEN_PRIV_STR, NULL);
4951 	if (basic == NULL)
4952 		return (errno == ENOMEM ? Z_NOMEM : Z_INVAL);
4953 
4954 	priv_union(basic, privs);
4955 	priv_freeset(basic);
4956 
4957 	for (pnp = plp->pl_default; pnp != NULL; pnp = pnp->pn_next) {
4958 		if (priv_addset(privs, pnp->pn_priv) != 0)
4959 			return (Z_INVAL);
4960 	}
4961 
4962 	return (Z_OK);
4963 }
4964 
4965 int
4966 zonecfg_default_privset(priv_set_t *privs, const char *curr_iptype)
4967 {
4968 	priv_lists_t *plp;
4969 	int ret;
4970 
4971 	if ((ret = priv_lists_create(NULL, &plp, curr_iptype)) != Z_OK)
4972 		return (ret);
4973 	ret = get_default_privset(privs, plp);
4974 	priv_lists_destroy(plp);
4975 	return (ret);
4976 }
4977 
4978 void
4979 append_priv_token(char *priv, char *str, size_t strlen)
4980 {
4981 	if (*str != '\0')
4982 		(void) strlcat(str, TOKEN_PRIV_STR, strlen);
4983 	(void) strlcat(str, priv, strlen);
4984 }
4985 
4986 /*
4987  * Verify that the supplied string is a valid privilege limit set for a
4988  * non-global zone.  This string must not only be acceptable to
4989  * priv_str_to_set(3C) which parses it, but it also must resolve to a
4990  * privilege set that includes certain required privileges and lacks
4991  * certain prohibited privileges.
4992  */
4993 static int
4994 verify_privset(char *privbuf, priv_set_t *privs, char **privname,
4995     boolean_t add_default, priv_lists_t *plp)
4996 {
4997 	priv_node_t *pnp;
4998 	char *tmp, *cp, *lasts;
4999 	size_t len;
5000 	priv_set_t *mergeset;
5001 	const char *token;
5002 
5003 	/*
5004 	 * The verification of the privilege string occurs in several
5005 	 * phases.  In the first phase, the supplied string is scanned for
5006 	 * the ZONE_TOKEN token which is not support as part of the
5007 	 * "limitpriv" property.
5008 	 *
5009 	 * Duplicate the supplied privilege string since strtok_r(3C)
5010 	 * tokenizes its input by null-terminating the tokens.
5011 	 */
5012 	if ((tmp = strdup(privbuf)) == NULL)
5013 		return (Z_NOMEM);
5014 	for (cp = strtok_r(tmp, TOKEN_PRIV_STR, &lasts); cp != NULL;
5015 	    cp = strtok_r(NULL, TOKEN_PRIV_STR, &lasts)) {
5016 		if (strcmp(cp, ZONE_TOKEN) == 0) {
5017 			free(tmp);
5018 			if ((*privname = strdup(ZONE_TOKEN)) == NULL)
5019 				return (Z_NOMEM);
5020 			else
5021 				return (Z_PRIV_UNKNOWN);
5022 		}
5023 	}
5024 	free(tmp);
5025 
5026 	if (add_default) {
5027 		/*
5028 		 * If DEFAULT_TOKEN was specified, a string needs to be
5029 		 * built containing the privileges from the default, safe
5030 		 * set along with those of the "limitpriv" property.
5031 		 */
5032 		len = strlen(privbuf) + sizeof (BASIC_TOKEN) + 2;
5033 
5034 		for (pnp = plp->pl_default; pnp != NULL; pnp = pnp->pn_next)
5035 			len += strlen(pnp->pn_priv) + 1;
5036 		tmp = alloca(len);
5037 		*tmp = '\0';
5038 
5039 		append_priv_token(BASIC_TOKEN, tmp, len);
5040 		for (pnp = plp->pl_default; pnp != NULL; pnp = pnp->pn_next)
5041 			append_priv_token(pnp->pn_priv, tmp, len);
5042 		(void) strlcat(tmp, TOKEN_PRIV_STR, len);
5043 		(void) strlcat(tmp, privbuf, len);
5044 	} else {
5045 		tmp = privbuf;
5046 	}
5047 
5048 
5049 	/*
5050 	 * In the next phase, attempt to convert the merged privilege
5051 	 * string into a privilege set.  In the case of an error, either
5052 	 * there was a memory allocation failure or there was an invalid
5053 	 * privilege token in the string.  In either case, return an
5054 	 * appropriate error code but in the event of an invalid token,
5055 	 * allocate a string containing its name and return that back to
5056 	 * the caller.
5057 	 */
5058 	mergeset = priv_str_to_set(tmp, TOKEN_PRIV_STR, &token);
5059 	if (mergeset == NULL) {
5060 		if (token == NULL)
5061 			return (Z_NOMEM);
5062 		if ((cp = strchr(token, TOKEN_PRIV_CHAR)) != NULL)
5063 			*cp = '\0';
5064 		if ((*privname = strdup(token)) == NULL)
5065 			return (Z_NOMEM);
5066 		else
5067 			return (Z_PRIV_UNKNOWN);
5068 	}
5069 
5070 	/*
5071 	 * Next, verify that none of the prohibited zone privileges are
5072 	 * present in the merged privilege set.
5073 	 */
5074 	for (pnp = plp->pl_prohibited; pnp != NULL; pnp = pnp->pn_next) {
5075 		if (priv_ismember(mergeset, pnp->pn_priv)) {
5076 			priv_freeset(mergeset);
5077 			if ((*privname = strdup(pnp->pn_priv)) == NULL)
5078 				return (Z_NOMEM);
5079 			else
5080 				return (Z_PRIV_PROHIBITED);
5081 		}
5082 	}
5083 
5084 	/*
5085 	 * Finally, verify that all of the required zone privileges are
5086 	 * present in the merged privilege set.
5087 	 */
5088 	for (pnp = plp->pl_required; pnp != NULL; pnp = pnp->pn_next) {
5089 		if (!priv_ismember(mergeset, pnp->pn_priv)) {
5090 			priv_freeset(mergeset);
5091 			if ((*privname = strdup(pnp->pn_priv)) == NULL)
5092 				return (Z_NOMEM);
5093 			else
5094 				return (Z_PRIV_REQUIRED);
5095 		}
5096 	}
5097 
5098 	priv_copyset(mergeset, privs);
5099 	priv_freeset(mergeset);
5100 	return (Z_OK);
5101 }
5102 
5103 /*
5104  * Fill in the supplied privilege set with either the default, safe set of
5105  * privileges suitable for a non-global zone, or one based on the
5106  * "limitpriv" property in the zone's configuration.
5107  *
5108  * In the event of an invalid privilege specification in the
5109  * configuration, a string is allocated and returned containing the
5110  * "privilege" causing the issue.  It is the caller's responsibility to
5111  * free this memory when it is done with it.
5112  */
5113 int
5114 zonecfg_get_privset(zone_dochandle_t handle, priv_set_t *privs,
5115     char **privname)
5116 {
5117 	priv_lists_t *plp;
5118 	char *cp, *limitpriv = NULL;
5119 	int err, limitlen;
5120 	zone_iptype_t iptype;
5121 	const char *curr_iptype;
5122 
5123 	/*
5124 	 * Attempt to lookup the "limitpriv" property.  If it does not
5125 	 * exist or matches the string DEFAULT_TOKEN exactly, then the
5126 	 * default, safe privilege set is returned.
5127 	 */
5128 	if ((err = zonecfg_get_limitpriv(handle, &limitpriv)) != Z_OK)
5129 		return (err);
5130 
5131 	if ((err = zonecfg_get_iptype(handle, &iptype)) != Z_OK)
5132 		return (err);
5133 
5134 	switch (iptype) {
5135 	case ZS_SHARED:
5136 		curr_iptype = "shared";
5137 		break;
5138 	case ZS_EXCLUSIVE:
5139 		curr_iptype = "exclusive";
5140 		break;
5141 	}
5142 
5143 	if ((err = priv_lists_create(handle, &plp, curr_iptype)) != Z_OK)
5144 		return (err);
5145 
5146 	limitlen = strlen(limitpriv);
5147 	if (limitlen == 0 || strcmp(limitpriv, DEFAULT_TOKEN) == 0) {
5148 		free(limitpriv);
5149 		err = get_default_privset(privs, plp);
5150 		priv_lists_destroy(plp);
5151 		return (err);
5152 	}
5153 
5154 	/*
5155 	 * Check if the string DEFAULT_TOKEN is the first token in a list
5156 	 * of privileges.
5157 	 */
5158 	cp = strchr(limitpriv, TOKEN_PRIV_CHAR);
5159 	if (cp != NULL &&
5160 	    strncmp(limitpriv, DEFAULT_TOKEN, cp - limitpriv) == 0)
5161 		err = verify_privset(cp + 1, privs, privname, B_TRUE, plp);
5162 	else
5163 		err = verify_privset(limitpriv, privs, privname, B_FALSE, plp);
5164 
5165 	free(limitpriv);
5166 	priv_lists_destroy(plp);
5167 	return (err);
5168 }
5169 
5170 int
5171 zone_get_zonepath(char *zone_name, char *zonepath, size_t rp_sz)
5172 {
5173 	zone_dochandle_t handle;
5174 	boolean_t found = B_FALSE;
5175 	struct zoneent *ze;
5176 	FILE *cookie;
5177 	int err;
5178 	char *cp;
5179 
5180 	if (zone_name == NULL)
5181 		return (Z_INVAL);
5182 
5183 	(void) strlcpy(zonepath, zonecfg_root, rp_sz);
5184 	cp = zonepath + strlen(zonepath);
5185 	while (cp > zonepath && cp[-1] == '/')
5186 		*--cp = '\0';
5187 
5188 	if (strcmp(zone_name, GLOBAL_ZONENAME) == 0) {
5189 		if (zonepath[0] == '\0')
5190 			(void) strlcpy(zonepath, "/", rp_sz);
5191 		return (Z_OK);
5192 	}
5193 
5194 	/*
5195 	 * First check the index file.  Because older versions did not have
5196 	 * a copy of the zone path, allow for it to be zero length, in which
5197 	 * case we ignore this result and fall back to the XML files.
5198 	 */
5199 	cookie = setzoneent();
5200 	while ((ze = getzoneent_private(cookie)) != NULL) {
5201 		if (strcmp(ze->zone_name, zone_name) == 0) {
5202 			found = B_TRUE;
5203 			if (ze->zone_path[0] != '\0')
5204 				(void) strlcpy(cp, ze->zone_path,
5205 				    rp_sz - (cp - zonepath));
5206 		}
5207 		free(ze);
5208 		if (found)
5209 			break;
5210 	}
5211 	endzoneent(cookie);
5212 	if (found && *cp != '\0')
5213 		return (Z_OK);
5214 
5215 	/* Fall back to the XML files. */
5216 	if ((handle = zonecfg_init_handle()) == NULL)
5217 		return (Z_NOMEM);
5218 
5219 	/*
5220 	 * Check the snapshot first: if a zone is running, its zonepath
5221 	 * may have changed.
5222 	 */
5223 	if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) {
5224 		if ((err = zonecfg_get_handle(zone_name, handle)) != Z_OK) {
5225 			zonecfg_fini_handle(handle);
5226 			return (err);
5227 		}
5228 	}
5229 	err = zonecfg_get_zonepath(handle, zonepath, rp_sz);
5230 	zonecfg_fini_handle(handle);
5231 	return (err);
5232 }
5233 
5234 int
5235 zone_get_rootpath(char *zone_name, char *rootpath, size_t rp_sz)
5236 {
5237 	int err;
5238 
5239 	/* This function makes sense for non-global zones only. */
5240 	if (strcmp(zone_name, GLOBAL_ZONENAME) == 0)
5241 		return (Z_BOGUS_ZONE_NAME);
5242 	if ((err = zone_get_zonepath(zone_name, rootpath, rp_sz)) != Z_OK)
5243 		return (err);
5244 	if (strlcat(rootpath, "/root", rp_sz) >= rp_sz)
5245 		return (Z_TOO_BIG);
5246 	return (Z_OK);
5247 }
5248 
5249 int
5250 zone_get_brand(char *zone_name, char *brandname, size_t rp_sz)
5251 {
5252 	int err;
5253 	zone_dochandle_t handle;
5254 	char myzone[MAXNAMELEN];
5255 	int myzoneid = getzoneid();
5256 
5257 	/*
5258 	 * If we are not in the global zone, then we don't have the zone
5259 	 * .xml files with the brand name available.  Thus, we are going to
5260 	 * have to ask the kernel for the information.
5261 	 */
5262 	if (myzoneid != GLOBAL_ZONEID) {
5263 		if (is_system_labeled()) {
5264 			(void) strlcpy(brandname, NATIVE_BRAND_NAME, rp_sz);
5265 			return (Z_OK);
5266 		}
5267 		if (zone_getattr(myzoneid, ZONE_ATTR_NAME, myzone,
5268 		    sizeof (myzone)) < 0)
5269 			return (Z_NO_ZONE);
5270 		if (!zonecfg_is_scratch(myzone)) {
5271 			if (strncmp(zone_name, myzone, MAXNAMELEN) != 0)
5272 				return (Z_NO_ZONE);
5273 		}
5274 		err = zone_getattr(myzoneid, ZONE_ATTR_BRAND, brandname, rp_sz);
5275 		if (err < 0)
5276 			return ((errno == EFAULT) ? Z_TOO_BIG : Z_INVAL);
5277 
5278 		return (Z_OK);
5279 	}
5280 
5281 	if (strcmp(zone_name, "global") == 0) {
5282 		(void) strlcpy(brandname, NATIVE_BRAND_NAME, rp_sz);
5283 		return (Z_OK);
5284 	}
5285 	if ((handle = zonecfg_init_handle()) == NULL)
5286 		return (Z_NOMEM);
5287 
5288 	err = zonecfg_get_handle((char *)zone_name, handle);
5289 	if (err == Z_OK)
5290 		err = zonecfg_get_brand(handle, brandname, rp_sz);
5291 
5292 	zonecfg_fini_handle(handle);
5293 	return (err);
5294 }
5295 
5296 /*
5297  * Return the appropriate root for the active /dev.
5298  * For normal zone, the path is $ZONEPATH/root;
5299  * for scratch zone, the dev path is $ZONEPATH/lu.
5300  */
5301 int
5302 zone_get_devroot(char *zone_name, char *devroot, size_t rp_sz)
5303 {
5304 	int err;
5305 	char *suffix;
5306 	zone_state_t state;
5307 
5308 	/* This function makes sense for non-global zones only. */
5309 	if (strcmp(zone_name, GLOBAL_ZONENAME) == 0)
5310 		return (Z_BOGUS_ZONE_NAME);
5311 	if ((err = zone_get_zonepath(zone_name, devroot, rp_sz)) != Z_OK)
5312 		return (err);
5313 
5314 	if (zone_get_state(zone_name, &state) == Z_OK &&
5315 	    state == ZONE_STATE_MOUNTED)
5316 		suffix = "/lu";
5317 	else
5318 		suffix = "/root";
5319 	if (strlcat(devroot, suffix, rp_sz) >= rp_sz)
5320 		return (Z_TOO_BIG);
5321 	return (Z_OK);
5322 }
5323 
5324 static zone_state_t
5325 kernel_state_to_user_state(zoneid_t zoneid, zone_status_t kernel_state)
5326 {
5327 	char zoneroot[MAXPATHLEN];
5328 	size_t zlen;
5329 
5330 	assert(kernel_state <= ZONE_MAX_STATE);
5331 	switch (kernel_state) {
5332 		case ZONE_IS_UNINITIALIZED:
5333 		case ZONE_IS_INITIALIZED:
5334 			/* The kernel will not return these two states */
5335 			return (ZONE_STATE_READY);
5336 		case ZONE_IS_READY:
5337 			/*
5338 			 * If the zone's root is mounted on $ZONEPATH/lu, then
5339 			 * it's a mounted scratch zone.
5340 			 */
5341 			if (zone_getattr(zoneid, ZONE_ATTR_ROOT, zoneroot,
5342 			    sizeof (zoneroot)) >= 0) {
5343 				zlen = strlen(zoneroot);
5344 				if (zlen > 3 &&
5345 				    strcmp(zoneroot + zlen - 3, "/lu") == 0)
5346 					return (ZONE_STATE_MOUNTED);
5347 			}
5348 			return (ZONE_STATE_READY);
5349 		case ZONE_IS_BOOTING:
5350 		case ZONE_IS_RUNNING:
5351 			return (ZONE_STATE_RUNNING);
5352 		case ZONE_IS_SHUTTING_DOWN:
5353 		case ZONE_IS_EMPTY:
5354 			return (ZONE_STATE_SHUTTING_DOWN);
5355 		case ZONE_IS_DOWN:
5356 		case ZONE_IS_DYING:
5357 		case ZONE_IS_DEAD:
5358 		default:
5359 			return (ZONE_STATE_DOWN);
5360 	}
5361 	/* NOTREACHED */
5362 }
5363 
5364 int
5365 zone_get_state(char *zone_name, zone_state_t *state_num)
5366 {
5367 	zone_status_t status;
5368 	zoneid_t zone_id;
5369 	struct zoneent *ze;
5370 	boolean_t found = B_FALSE;
5371 	FILE *cookie;
5372 	char kernzone[ZONENAME_MAX];
5373 	FILE *fp;
5374 
5375 	if (zone_name == NULL)
5376 		return (Z_INVAL);
5377 
5378 	/*
5379 	 * If we're looking at an alternate root, then we need to query the
5380 	 * kernel using the scratch zone name.
5381 	 */
5382 	zone_id = -1;
5383 	if (*zonecfg_root != '\0' && !zonecfg_is_scratch(zone_name)) {
5384 		if ((fp = zonecfg_open_scratch("", B_FALSE)) != NULL) {
5385 			if (zonecfg_find_scratch(fp, zone_name, zonecfg_root,
5386 			    kernzone, sizeof (kernzone)) == 0)
5387 				zone_id = getzoneidbyname(kernzone);
5388 			zonecfg_close_scratch(fp);
5389 		}
5390 	} else {
5391 		zone_id = getzoneidbyname(zone_name);
5392 	}
5393 
5394 	/* check to see if zone is running */
5395 	if (zone_id != -1 &&
5396 	    zone_getattr(zone_id, ZONE_ATTR_STATUS, &status,
5397 	    sizeof (status)) >= 0) {
5398 		*state_num = kernel_state_to_user_state(zone_id, status);
5399 		return (Z_OK);
5400 	}
5401 
5402 	cookie = setzoneent();
5403 	while ((ze = getzoneent_private(cookie)) != NULL) {
5404 		if (strcmp(ze->zone_name, zone_name) == 0) {
5405 			found = B_TRUE;
5406 			*state_num = ze->zone_state;
5407 		}
5408 		free(ze);
5409 		if (found)
5410 			break;
5411 	}
5412 	endzoneent(cookie);
5413 	return ((found) ? Z_OK : Z_NO_ZONE);
5414 }
5415 
5416 int
5417 zone_set_state(char *zone, zone_state_t state)
5418 {
5419 	struct zoneent ze;
5420 
5421 	if (state != ZONE_STATE_CONFIGURED && state != ZONE_STATE_INSTALLED &&
5422 	    state != ZONE_STATE_INCOMPLETE)
5423 		return (Z_INVAL);
5424 
5425 	bzero(&ze, sizeof (ze));
5426 	(void) strlcpy(ze.zone_name, zone, sizeof (ze.zone_name));
5427 	ze.zone_state = state;
5428 	(void) strlcpy(ze.zone_path, "", sizeof (ze.zone_path));
5429 	return (putzoneent(&ze, PZE_MODIFY));
5430 }
5431 
5432 /*
5433  * Get id (if any) for specified zone.  There are four possible outcomes:
5434  * - If the string corresponds to the numeric id of an active (booted)
5435  *   zone, sets *zip to the zone id and returns 0.
5436  * - If the string corresponds to the name of an active (booted) zone,
5437  *   sets *zip to the zone id and returns 0.
5438  * - If the string is a name in the configuration but is not booted,
5439  *   sets *zip to ZONE_ID_UNDEFINED and returns 0.
5440  * - Otherwise, leaves *zip unchanged and returns -1.
5441  *
5442  * This function acts as an auxiliary filter on the function of the same
5443  * name in libc; the linker binds to this version if libzonecfg exists,
5444  * and the libc version if it doesn't.  Any changes to this version of
5445  * the function should probably be reflected in the libc version as well.
5446  */
5447 int
5448 zone_get_id(const char *str, zoneid_t *zip)
5449 {
5450 	zone_dochandle_t hdl;
5451 	zoneid_t zoneid;
5452 	char *cp;
5453 	int err;
5454 
5455 	/* first try looking for active zone by id */
5456 	errno = 0;
5457 	zoneid = (zoneid_t)strtol(str, &cp, 0);
5458 	if (errno == 0 && cp != str && *cp == '\0' &&
5459 	    getzonenamebyid(zoneid, NULL, 0) != -1) {
5460 		*zip = zoneid;
5461 		return (0);
5462 	}
5463 
5464 	/* then look for active zone by name */
5465 	if ((zoneid = getzoneidbyname(str)) != -1) {
5466 		*zip = zoneid;
5467 		return (0);
5468 	}
5469 
5470 	/* if in global zone, try looking up name in configuration database */
5471 	if (getzoneid() != GLOBAL_ZONEID ||
5472 	    (hdl = zonecfg_init_handle()) == NULL)
5473 		return (-1);
5474 
5475 	if (zonecfg_get_handle(str, hdl) == Z_OK) {
5476 		/* zone exists but isn't active */
5477 		*zip = ZONE_ID_UNDEFINED;
5478 		err = 0;
5479 	} else {
5480 		err = -1;
5481 	}
5482 
5483 	zonecfg_fini_handle(hdl);
5484 	return (err);
5485 }
5486 
5487 char *
5488 zone_state_str(zone_state_t state_num)
5489 {
5490 	switch (state_num) {
5491 	case ZONE_STATE_CONFIGURED:
5492 		return (ZONE_STATE_STR_CONFIGURED);
5493 	case ZONE_STATE_INCOMPLETE:
5494 		return (ZONE_STATE_STR_INCOMPLETE);
5495 	case ZONE_STATE_INSTALLED:
5496 		return (ZONE_STATE_STR_INSTALLED);
5497 	case ZONE_STATE_READY:
5498 		return (ZONE_STATE_STR_READY);
5499 	case ZONE_STATE_MOUNTED:
5500 		return (ZONE_STATE_STR_MOUNTED);
5501 	case ZONE_STATE_RUNNING:
5502 		return (ZONE_STATE_STR_RUNNING);
5503 	case ZONE_STATE_SHUTTING_DOWN:
5504 		return (ZONE_STATE_STR_SHUTTING_DOWN);
5505 	case ZONE_STATE_DOWN:
5506 		return (ZONE_STATE_STR_DOWN);
5507 	default:
5508 		return ("unknown");
5509 	}
5510 }
5511 
5512 /*
5513  * Given a UUID value, find an associated zone name.  This is intended to be
5514  * used by callers who set up some 'default' name (corresponding to the
5515  * expected name for the zone) in the zonename buffer, and thus the function
5516  * doesn't touch this buffer on failure.
5517  */
5518 int
5519 zonecfg_get_name_by_uuid(const uuid_t uuidin, char *zonename, size_t namelen)
5520 {
5521 	FILE *fp;
5522 	struct zoneent *ze;
5523 	uchar_t *uuid;
5524 
5525 	/*
5526 	 * A small amount of subterfuge via casts is necessary here because
5527 	 * libuuid doesn't use const correctly, but we don't want to export
5528 	 * this brokenness to our clients.
5529 	 */
5530 	uuid = (uchar_t *)uuidin;
5531 	if (uuid_is_null(uuid))
5532 		return (Z_NO_ZONE);
5533 	if ((fp = setzoneent()) == NULL)
5534 		return (Z_NO_ZONE);
5535 	while ((ze = getzoneent_private(fp)) != NULL) {
5536 		if (uuid_compare(uuid, ze->zone_uuid) == 0)
5537 			break;
5538 		free(ze);
5539 	}
5540 	endzoneent(fp);
5541 	if (ze != NULL) {
5542 		(void) strlcpy(zonename, ze->zone_name, namelen);
5543 		free(ze);
5544 		return (Z_OK);
5545 	} else {
5546 		return (Z_NO_ZONE);
5547 	}
5548 }
5549 
5550 /*
5551  * Given a zone name, get its UUID.  Returns a "NULL" UUID value if the zone
5552  * exists but the file doesn't have a value set yet.  Returns an error if the
5553  * zone cannot be located.
5554  */
5555 int
5556 zonecfg_get_uuid(const char *zonename, uuid_t uuid)
5557 {
5558 	FILE *fp;
5559 	struct zoneent *ze;
5560 
5561 	if ((fp = setzoneent()) == NULL)
5562 		return (Z_NO_ZONE);
5563 	while ((ze = getzoneent_private(fp)) != NULL) {
5564 		if (strcmp(ze->zone_name, zonename) == 0)
5565 			break;
5566 		free(ze);
5567 	}
5568 	endzoneent(fp);
5569 	if (ze != NULL) {
5570 		uuid_copy(uuid, ze->zone_uuid);
5571 		free(ze);
5572 		return (Z_OK);
5573 	} else {
5574 		return (Z_NO_ZONE);
5575 	}
5576 }
5577 
5578 /*
5579  * File-system convenience functions.
5580  */
5581 boolean_t
5582 zonecfg_valid_fs_type(const char *type)
5583 {
5584 	/*
5585 	 * We already know which FS types don't work.
5586 	 */
5587 	if (strcmp(type, "proc") == 0 ||
5588 	    strcmp(type, "mntfs") == 0 ||
5589 	    strcmp(type, "autofs") == 0 ||
5590 	    strncmp(type, "nfs", sizeof ("nfs") - 1) == 0 ||
5591 	    strcmp(type, "cachefs") == 0)
5592 		return (B_FALSE);
5593 	/*
5594 	 * The caller may do more detailed verification to make sure other
5595 	 * aspects of this filesystem type make sense.
5596 	 */
5597 	return (B_TRUE);
5598 }
5599 
5600 /*
5601  * Generally uninteresting rctl convenience functions.
5602  */
5603 
5604 int
5605 zonecfg_construct_rctlblk(const struct zone_rctlvaltab *rctlval,
5606     rctlblk_t *rctlblk)
5607 {
5608 	unsigned long long ull;
5609 	char *endp;
5610 	rctl_priv_t priv;
5611 	rctl_qty_t limit;
5612 	uint_t action;
5613 
5614 	/* Get the privilege */
5615 	if (strcmp(rctlval->zone_rctlval_priv, "basic") == 0) {
5616 		priv = RCPRIV_BASIC;
5617 	} else if (strcmp(rctlval->zone_rctlval_priv, "privileged") == 0) {
5618 		priv = RCPRIV_PRIVILEGED;
5619 	} else {
5620 		/* Invalid privilege */
5621 		return (Z_INVAL);
5622 	}
5623 
5624 	/* deal with negative input; strtoull(3c) doesn't do what we want */
5625 	if (rctlval->zone_rctlval_limit[0] == '-')
5626 		return (Z_INVAL);
5627 	/* Get the limit */
5628 	errno = 0;
5629 	ull = strtoull(rctlval->zone_rctlval_limit, &endp, 0);
5630 	if (errno != 0 || *endp != '\0') {
5631 		/* parse failed */
5632 		return (Z_INVAL);
5633 	}
5634 	limit = (rctl_qty_t)ull;
5635 
5636 	/* Get the action */
5637 	if (strcmp(rctlval->zone_rctlval_action, "none") == 0) {
5638 		action = RCTL_LOCAL_NOACTION;
5639 	} else if (strcmp(rctlval->zone_rctlval_action, "signal") == 0) {
5640 		action = RCTL_LOCAL_SIGNAL;
5641 	} else if (strcmp(rctlval->zone_rctlval_action, "deny") == 0) {
5642 		action = RCTL_LOCAL_DENY;
5643 	} else {
5644 		/* Invalid Action */
5645 		return (Z_INVAL);
5646 	}
5647 	rctlblk_set_local_action(rctlblk, action, 0);
5648 	rctlblk_set_privilege(rctlblk, priv);
5649 	rctlblk_set_value(rctlblk, limit);
5650 	return (Z_OK);
5651 }
5652 
5653 static int
5654 rctl_check(const char *rctlname, void *arg)
5655 {
5656 	const char *attrname = arg;
5657 
5658 	/*
5659 	 * Returning 1 here is our signal to zonecfg_is_rctl() that it is
5660 	 * indeed an rctl name recognized by the system.
5661 	 */
5662 	return (strcmp(rctlname, attrname) == 0 ? 1 : 0);
5663 }
5664 
5665 boolean_t
5666 zonecfg_is_rctl(const char *name)
5667 {
5668 	return (rctl_walk(rctl_check, (void *)name) == 1);
5669 }
5670 
5671 boolean_t
5672 zonecfg_valid_rctlname(const char *name)
5673 {
5674 	const char *c;
5675 
5676 	if (strncmp(name, "zone.", sizeof ("zone.") - 1) != 0)
5677 		return (B_FALSE);
5678 	if (strlen(name) == sizeof ("zone.") - 1)
5679 		return (B_FALSE);
5680 	for (c = name + sizeof ("zone.") - 1; *c != '\0'; c++) {
5681 		if (!isalpha(*c) && *c != '-')
5682 			return (B_FALSE);
5683 	}
5684 	return (B_TRUE);
5685 }
5686 
5687 boolean_t
5688 zonecfg_valid_rctlblk(const rctlblk_t *rctlblk)
5689 {
5690 	rctl_priv_t priv = rctlblk_get_privilege((rctlblk_t *)rctlblk);
5691 	uint_t action = rctlblk_get_local_action((rctlblk_t *)rctlblk, NULL);
5692 
5693 	if (priv != RCPRIV_PRIVILEGED)
5694 		return (B_FALSE);
5695 	if (action != RCTL_LOCAL_NOACTION && action != RCTL_LOCAL_DENY)
5696 		return (B_FALSE);
5697 	return (B_TRUE);
5698 }
5699 
5700 boolean_t
5701 zonecfg_valid_rctl(const char *name, const rctlblk_t *rctlblk)
5702 {
5703 	rctlblk_t *current, *next;
5704 	rctl_qty_t limit = rctlblk_get_value((rctlblk_t *)rctlblk);
5705 	uint_t action = rctlblk_get_local_action((rctlblk_t *)rctlblk, NULL);
5706 	uint_t global_flags;
5707 
5708 	if (!zonecfg_valid_rctlblk(rctlblk))
5709 		return (B_FALSE);
5710 	if (!zonecfg_valid_rctlname(name))
5711 		return (B_FALSE);
5712 
5713 	current = alloca(rctlblk_size());
5714 	if (getrctl(name, NULL, current, RCTL_FIRST) != 0)
5715 		return (B_TRUE);	/* not an rctl on this system */
5716 	/*
5717 	 * Make sure the proposed value isn't greater than the current system
5718 	 * value.
5719 	 */
5720 	next = alloca(rctlblk_size());
5721 	while (rctlblk_get_privilege(current) != RCPRIV_SYSTEM) {
5722 		rctlblk_t *tmp;
5723 
5724 		if (getrctl(name, current, next, RCTL_NEXT) != 0)
5725 			return (B_FALSE);	/* shouldn't happen */
5726 		tmp = current;
5727 		current = next;
5728 		next = tmp;
5729 	}
5730 	if (limit > rctlblk_get_value(current))
5731 		return (B_FALSE);
5732 
5733 	/*
5734 	 * Make sure the proposed action is allowed.
5735 	 */
5736 	global_flags = rctlblk_get_global_flags(current);
5737 	if ((global_flags & RCTL_GLOBAL_DENY_NEVER) &&
5738 	    action == RCTL_LOCAL_DENY)
5739 		return (B_FALSE);
5740 	if ((global_flags & RCTL_GLOBAL_DENY_ALWAYS) &&
5741 	    action == RCTL_LOCAL_NOACTION)
5742 		return (B_FALSE);
5743 
5744 	return (B_TRUE);
5745 }
5746 
5747 /*
5748  * There is always a race condition between reading the initial copy of
5749  * a zones state and its state changing.  We address this by providing
5750  * zonecfg_notify_critical_enter and zonecfg_noticy_critical_exit functions.
5751  * When zonecfg_critical_enter is called, sets the state field to LOCKED
5752  * and aquires biglock. Biglock protects against other threads executing
5753  * critical_enter and the state field protects against state changes during
5754  * the critical period.
5755  *
5756  * If any state changes occur, zn_cb will set the failed field of the znotify
5757  * structure.  This will cause the critical_exit function to re-lock the
5758  * channel and return an error. Since evsnts may be delayed, the critical_exit
5759  * function "flushes" the queue by putting an event on the queue and waiting for
5760  * zn_cb to notify critical_exit that it received the ping event.
5761  */
5762 static const char *
5763 string_get_tok(const char *in, char delim, int num)
5764 {
5765 	int i = 0;
5766 
5767 	for (; i < num; in++) {
5768 		if (*in == delim)
5769 			i++;
5770 		if (*in == 0)
5771 			return (NULL);
5772 	}
5773 	return (in);
5774 }
5775 
5776 static boolean_t
5777 is_ping(sysevent_t *ev)
5778 {
5779 	if (strcmp(sysevent_get_subclass_name(ev),
5780 	    ZONE_EVENT_PING_SUBCLASS) == 0) {
5781 		return (B_TRUE);
5782 	} else {
5783 		return (B_FALSE);
5784 	}
5785 }
5786 
5787 static boolean_t
5788 is_my_ping(sysevent_t *ev)
5789 {
5790 	const char *sender;
5791 	char mypid[sizeof (pid_t) * 3 + 1];
5792 
5793 	(void) snprintf(mypid, sizeof (mypid), "%i", getpid());
5794 	sender = string_get_tok(sysevent_get_pub(ev), ':', 3);
5795 	if (sender == NULL)
5796 		return (B_FALSE);
5797 	if (strcmp(sender, mypid) != 0)
5798 		return (B_FALSE);
5799 	return (B_TRUE);
5800 }
5801 
5802 static int
5803 do_callback(struct znotify *zevtchan, sysevent_t *ev)
5804 {
5805 	nvlist_t *l;
5806 	int zid;
5807 	char *zonename;
5808 	char *newstate;
5809 	char *oldstate;
5810 	int ret;
5811 	hrtime_t when;
5812 
5813 	if (strcmp(sysevent_get_subclass_name(ev),
5814 	    ZONE_EVENT_STATUS_SUBCLASS) == 0) {
5815 
5816 		if (sysevent_get_attr_list(ev, &l) != 0) {
5817 			if (errno == ENOMEM) {
5818 				zevtchan->zn_failure_count++;
5819 				return (EAGAIN);
5820 			}
5821 			return (0);
5822 		}
5823 		ret = 0;
5824 
5825 		if ((nvlist_lookup_string(l, ZONE_CB_NAME, &zonename) == 0) &&
5826 		    (nvlist_lookup_string(l, ZONE_CB_NEWSTATE, &newstate)
5827 		    == 0) &&
5828 		    (nvlist_lookup_string(l, ZONE_CB_OLDSTATE, &oldstate)
5829 		    == 0) &&
5830 		    (nvlist_lookup_uint64(l, ZONE_CB_TIMESTAMP,
5831 		    (uint64_t *)&when) == 0) &&
5832 		    (nvlist_lookup_int32(l, ZONE_CB_ZONEID, &zid) == 0)) {
5833 			ret = zevtchan->zn_callback(zonename, zid, newstate,
5834 			    oldstate, when, zevtchan->zn_private);
5835 		}
5836 
5837 		zevtchan->zn_failure_count = 0;
5838 		nvlist_free(l);
5839 		return (ret);
5840 	} else {
5841 		/*
5842 		 * We have received an event in an unknown subclass. Ignore.
5843 		 */
5844 		zevtchan->zn_failure_count = 0;
5845 		return (0);
5846 	}
5847 }
5848 
5849 static int
5850 zn_cb(sysevent_t *ev, void *p)
5851 {
5852 	struct znotify *zevtchan = p;
5853 	int error;
5854 
5855 	(void) pthread_mutex_lock(&(zevtchan->zn_mutex));
5856 
5857 	if (is_ping(ev) && !is_my_ping(ev)) {
5858 		(void) pthread_mutex_unlock((&zevtchan->zn_mutex));
5859 		return (0);
5860 	}
5861 
5862 	if (zevtchan->zn_state == ZN_LOCKED) {
5863 		assert(!is_ping(ev));
5864 		zevtchan->zn_failed = B_TRUE;
5865 		(void) pthread_mutex_unlock(&(zevtchan->zn_mutex));
5866 		return (0);
5867 	}
5868 
5869 	if (zevtchan->zn_state == ZN_PING_INFLIGHT) {
5870 		if (is_ping(ev)) {
5871 			zevtchan->zn_state = ZN_PING_RECEIVED;
5872 			(void) pthread_cond_signal(&(zevtchan->zn_cond));
5873 			(void) pthread_mutex_unlock(&(zevtchan->zn_mutex));
5874 			return (0);
5875 		} else {
5876 			zevtchan->zn_failed = B_TRUE;
5877 			(void) pthread_mutex_unlock(&(zevtchan->zn_mutex));
5878 			return (0);
5879 		}
5880 	}
5881 
5882 	if (zevtchan->zn_state == ZN_UNLOCKED) {
5883 
5884 		error = do_callback(zevtchan, ev);
5885 		(void) pthread_mutex_unlock(&(zevtchan->zn_mutex));
5886 		/*
5887 		 * Every ENOMEM failure causes do_callback to increment
5888 		 * zn_failure_count and every success causes it to
5889 		 * set zn_failure_count to zero.  If we got EAGAIN,
5890 		 * we will sleep for zn_failure_count seconds and return
5891 		 * EAGAIN to gpec to try again.
5892 		 *
5893 		 * After 55 seconds, or 10 try's we give up and drop the
5894 		 * event.
5895 		 */
5896 		if (error == EAGAIN) {
5897 			if (zevtchan->zn_failure_count > ZONE_CB_RETRY_COUNT) {
5898 				return (0);
5899 			}
5900 			(void) sleep(zevtchan->zn_failure_count);
5901 		}
5902 		return (error);
5903 	}
5904 
5905 	if (zevtchan->zn_state == ZN_PING_RECEIVED) {
5906 		(void) pthread_mutex_unlock(&(zevtchan->zn_mutex));
5907 		return (0);
5908 	}
5909 
5910 	abort();
5911 	return (0);
5912 }
5913 
5914 void
5915 zonecfg_notify_critical_enter(void *h)
5916 {
5917 	struct znotify *zevtchan = h;
5918 
5919 	(void) pthread_mutex_lock(&(zevtchan->zn_bigmutex));
5920 	zevtchan->zn_state = ZN_LOCKED;
5921 }
5922 
5923 int
5924 zonecfg_notify_critical_exit(void * h)
5925 {
5926 
5927 	struct znotify *zevtchan = h;
5928 
5929 	if (zevtchan->zn_state == ZN_UNLOCKED)
5930 		return (0);
5931 
5932 	(void) pthread_mutex_lock(&(zevtchan->zn_mutex));
5933 	zevtchan->zn_state = ZN_PING_INFLIGHT;
5934 
5935 	(void) sysevent_evc_publish(zevtchan->zn_eventchan,
5936 	    ZONE_EVENT_STATUS_CLASS,
5937 	    ZONE_EVENT_PING_SUBCLASS, ZONE_EVENT_PING_PUBLISHER,
5938 	    zevtchan->zn_subscriber_id, NULL, EVCH_SLEEP);
5939 
5940 	while (zevtchan->zn_state != ZN_PING_RECEIVED) {
5941 		(void) pthread_cond_wait(&(zevtchan->zn_cond),
5942 		    &(zevtchan->zn_mutex));
5943 	}
5944 
5945 	if (zevtchan->zn_failed == B_TRUE) {
5946 		zevtchan->zn_state = ZN_LOCKED;
5947 		zevtchan->zn_failed = B_FALSE;
5948 		(void) pthread_mutex_unlock(&(zevtchan->zn_mutex));
5949 		return (1);
5950 	}
5951 
5952 	zevtchan->zn_state = ZN_UNLOCKED;
5953 	(void) pthread_mutex_unlock(&(zevtchan->zn_mutex));
5954 	(void) pthread_mutex_unlock(&(zevtchan->zn_bigmutex));
5955 	return (0);
5956 }
5957 
5958 void
5959 zonecfg_notify_critical_abort(void *h)
5960 {
5961 	struct znotify *zevtchan = h;
5962 
5963 	zevtchan->zn_state = ZN_UNLOCKED;
5964 	zevtchan->zn_failed = B_FALSE;
5965 	/*
5966 	 * Don't do anything about zn_lock. If it is held, it could only be
5967 	 * held by zn_cb and it will be unlocked soon.
5968 	 */
5969 	(void) pthread_mutex_unlock(&(zevtchan->zn_bigmutex));
5970 }
5971 
5972 void *
5973 zonecfg_notify_bind(int(*func)(const char *zonename, zoneid_t zid,
5974     const char *newstate, const char *oldstate, hrtime_t when, void *p),
5975     void *p)
5976 {
5977 	struct znotify *zevtchan;
5978 	int i = 1;
5979 	int r;
5980 
5981 	zevtchan = malloc(sizeof (struct znotify));
5982 
5983 	if (zevtchan == NULL)
5984 		return (NULL);
5985 
5986 	zevtchan->zn_private = p;
5987 	zevtchan->zn_callback = func;
5988 	zevtchan->zn_state = ZN_UNLOCKED;
5989 	zevtchan->zn_failed = B_FALSE;
5990 
5991 	if (pthread_mutex_init(&(zevtchan->zn_mutex), NULL))
5992 		goto out3;
5993 	if (pthread_cond_init(&(zevtchan->zn_cond), NULL)) {
5994 		(void) pthread_mutex_destroy(&(zevtchan->zn_mutex));
5995 		goto out3;
5996 	}
5997 	if (pthread_mutex_init(&(zevtchan->zn_bigmutex), NULL)) {
5998 		(void) pthread_mutex_destroy(&(zevtchan->zn_mutex));
5999 		(void) pthread_cond_destroy(&(zevtchan->zn_cond));
6000 		goto out3;
6001 	}
6002 
6003 	if (sysevent_evc_bind(ZONE_EVENT_CHANNEL, &(zevtchan->zn_eventchan),
6004 	    0) != 0)
6005 		goto out2;
6006 
6007 	do {
6008 		/*
6009 		 * At 4 digits the subscriber ID gets too long and we have
6010 		 * no chance of successfully registering.
6011 		 */
6012 		if (i > 999)
6013 			goto out1;
6014 
6015 		(void) sprintf(zevtchan->zn_subscriber_id, "zone_%li_%i",
6016 		    getpid() % 999999l, i);
6017 
6018 		r = sysevent_evc_subscribe(zevtchan->zn_eventchan,
6019 		    zevtchan->zn_subscriber_id, ZONE_EVENT_STATUS_CLASS, zn_cb,
6020 		    zevtchan, 0);
6021 
6022 		i++;
6023 
6024 	} while (r);
6025 
6026 	return (zevtchan);
6027 out1:
6028 	sysevent_evc_unbind(zevtchan->zn_eventchan);
6029 out2:
6030 	(void) pthread_mutex_destroy(&zevtchan->zn_mutex);
6031 	(void) pthread_cond_destroy(&zevtchan->zn_cond);
6032 	(void) pthread_mutex_destroy(&(zevtchan->zn_bigmutex));
6033 out3:
6034 	free(zevtchan);
6035 
6036 	return (NULL);
6037 }
6038 
6039 void
6040 zonecfg_notify_unbind(void *handle)
6041 {
6042 
6043 	int ret;
6044 
6045 	sysevent_evc_unbind(((struct znotify *)handle)->zn_eventchan);
6046 	/*
6047 	 * Check that all evc threads have gone away. This should be
6048 	 * enforced by sysevent_evc_unbind.
6049 	 */
6050 	ret = pthread_mutex_trylock(&((struct znotify *)handle)->zn_mutex);
6051 
6052 	if (ret)
6053 		abort();
6054 
6055 	(void) pthread_mutex_unlock(&((struct znotify *)handle)->zn_mutex);
6056 	(void) pthread_mutex_destroy(&((struct znotify *)handle)->zn_mutex);
6057 	(void) pthread_cond_destroy(&((struct znotify *)handle)->zn_cond);
6058 	(void) pthread_mutex_destroy(&((struct znotify *)handle)->zn_bigmutex);
6059 
6060 	free(handle);
6061 }
6062 
6063 static int
6064 zonecfg_add_ds_core(zone_dochandle_t handle, struct zone_dstab *tabptr)
6065 {
6066 	xmlNodePtr newnode, cur = handle->zone_dh_cur;
6067 	int err;
6068 
6069 	newnode = xmlNewTextChild(cur, NULL, DTD_ELEM_DATASET, NULL);
6070 	if ((err = newprop(newnode, DTD_ATTR_NAME,
6071 	    tabptr->zone_dataset_name)) != Z_OK)
6072 		return (err);
6073 	return (Z_OK);
6074 }
6075 
6076 int
6077 zonecfg_add_ds(zone_dochandle_t handle, struct zone_dstab *tabptr)
6078 {
6079 	int err;
6080 
6081 	if (tabptr == NULL)
6082 		return (Z_INVAL);
6083 
6084 	if ((err = operation_prep(handle)) != Z_OK)
6085 		return (err);
6086 
6087 	if ((err = zonecfg_add_ds_core(handle, tabptr)) != Z_OK)
6088 		return (err);
6089 
6090 	return (Z_OK);
6091 }
6092 
6093 static int
6094 zonecfg_delete_ds_core(zone_dochandle_t handle, struct zone_dstab *tabptr)
6095 {
6096 	xmlNodePtr cur = handle->zone_dh_cur;
6097 
6098 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
6099 		if (xmlStrcmp(cur->name, DTD_ELEM_DATASET))
6100 			continue;
6101 
6102 		if (match_prop(cur, DTD_ATTR_NAME,
6103 		    tabptr->zone_dataset_name)) {
6104 			xmlUnlinkNode(cur);
6105 			xmlFreeNode(cur);
6106 			return (Z_OK);
6107 		}
6108 	}
6109 	return (Z_NO_RESOURCE_ID);
6110 }
6111 
6112 int
6113 zonecfg_delete_ds(zone_dochandle_t handle, struct zone_dstab *tabptr)
6114 {
6115 	int err;
6116 
6117 	if (tabptr == NULL)
6118 		return (Z_INVAL);
6119 
6120 	if ((err = operation_prep(handle)) != Z_OK)
6121 		return (err);
6122 
6123 	if ((err = zonecfg_delete_ds_core(handle, tabptr)) != Z_OK)
6124 		return (err);
6125 
6126 	return (Z_OK);
6127 }
6128 
6129 int
6130 zonecfg_modify_ds(
6131 	zone_dochandle_t handle,
6132 	struct zone_dstab *oldtabptr,
6133 	struct zone_dstab *newtabptr)
6134 {
6135 	int err;
6136 
6137 	if (oldtabptr == NULL || newtabptr == NULL)
6138 		return (Z_INVAL);
6139 
6140 	if ((err = operation_prep(handle)) != Z_OK)
6141 		return (err);
6142 
6143 	if ((err = zonecfg_delete_ds_core(handle, oldtabptr)) != Z_OK)
6144 		return (err);
6145 
6146 	if ((err = zonecfg_add_ds_core(handle, newtabptr)) != Z_OK)
6147 		return (err);
6148 
6149 	return (Z_OK);
6150 }
6151 
6152 int
6153 zonecfg_lookup_ds(zone_dochandle_t handle, struct zone_dstab *tabptr)
6154 {
6155 	xmlNodePtr cur, firstmatch;
6156 	int err;
6157 	char dataset[MAXNAMELEN];
6158 
6159 	if (tabptr == NULL)
6160 		return (Z_INVAL);
6161 
6162 	if ((err = operation_prep(handle)) != Z_OK)
6163 		return (err);
6164 
6165 	cur = handle->zone_dh_cur;
6166 	firstmatch = NULL;
6167 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
6168 		if (xmlStrcmp(cur->name, DTD_ELEM_DATASET))
6169 			continue;
6170 		if (strlen(tabptr->zone_dataset_name) > 0) {
6171 			if ((fetchprop(cur, DTD_ATTR_NAME, dataset,
6172 			    sizeof (dataset)) == Z_OK) &&
6173 			    (strcmp(tabptr->zone_dataset_name,
6174 			    dataset) == 0)) {
6175 				if (firstmatch == NULL)
6176 					firstmatch = cur;
6177 				else
6178 					return (Z_INSUFFICIENT_SPEC);
6179 			}
6180 		}
6181 	}
6182 	if (firstmatch == NULL)
6183 		return (Z_NO_RESOURCE_ID);
6184 
6185 	cur = firstmatch;
6186 
6187 	if ((err = fetchprop(cur, DTD_ATTR_NAME, tabptr->zone_dataset_name,
6188 	    sizeof (tabptr->zone_dataset_name))) != Z_OK)
6189 		return (err);
6190 
6191 	return (Z_OK);
6192 }
6193 
6194 int
6195 zonecfg_setdsent(zone_dochandle_t handle)
6196 {
6197 	return (zonecfg_setent(handle));
6198 }
6199 
6200 int
6201 zonecfg_getdsent(zone_dochandle_t handle, struct zone_dstab *tabptr)
6202 {
6203 	xmlNodePtr cur;
6204 	int err;
6205 
6206 	if (handle == NULL)
6207 		return (Z_INVAL);
6208 
6209 	if ((cur = handle->zone_dh_cur) == NULL)
6210 		return (Z_NO_ENTRY);
6211 
6212 	for (; cur != NULL; cur = cur->next)
6213 		if (!xmlStrcmp(cur->name, DTD_ELEM_DATASET))
6214 			break;
6215 	if (cur == NULL) {
6216 		handle->zone_dh_cur = handle->zone_dh_top;
6217 		return (Z_NO_ENTRY);
6218 	}
6219 
6220 	if ((err = fetchprop(cur, DTD_ATTR_NAME, tabptr->zone_dataset_name,
6221 	    sizeof (tabptr->zone_dataset_name))) != Z_OK) {
6222 		handle->zone_dh_cur = handle->zone_dh_top;
6223 		return (err);
6224 	}
6225 
6226 	handle->zone_dh_cur = cur->next;
6227 	return (Z_OK);
6228 }
6229 
6230 int
6231 zonecfg_enddsent(zone_dochandle_t handle)
6232 {
6233 	return (zonecfg_endent(handle));
6234 }
6235 
6236 /*
6237  * Support for aliased rctls; that is, rctls that have simplified names in
6238  * zonecfg.  For example, max-lwps is an alias for a well defined zone.max-lwps
6239  * rctl.  If there are multiple existing values for one of these rctls or if
6240  * there is a single value that does not match the well defined template (i.e.
6241  * it has a different action) then we cannot treat the rctl as having an alias
6242  * so we return Z_ALIAS_DISALLOW.  That means that the rctl cannot be
6243  * managed in zonecfg via an alias and that the standard rctl syntax must be
6244  * used.
6245  *
6246  * The possible return values are:
6247  *	Z_NO_PROPERTY_ID - invalid alias name
6248  *	Z_ALIAS_DISALLOW - pre-existing, incompatible rctl definition
6249  *	Z_NO_ENTRY - no rctl is configured for this alias
6250  *	Z_OK - we got a valid rctl for the specified alias
6251  */
6252 int
6253 zonecfg_get_aliased_rctl(zone_dochandle_t handle, char *name, uint64_t *rval)
6254 {
6255 	boolean_t found = B_FALSE;
6256 	boolean_t found_val = B_FALSE;
6257 	xmlNodePtr cur, val;
6258 	char savedname[MAXNAMELEN];
6259 	struct zone_rctlvaltab rctl;
6260 	int i;
6261 	int err;
6262 
6263 	for (i = 0; aliases[i].shortname != NULL; i++)
6264 		if (strcmp(name, aliases[i].shortname) == 0)
6265 			break;
6266 
6267 	if (aliases[i].shortname == NULL)
6268 		return (Z_NO_PROPERTY_ID);
6269 
6270 	if ((err = operation_prep(handle)) != Z_OK)
6271 		return (err);
6272 
6273 	cur = handle->zone_dh_cur;
6274 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
6275 		if (xmlStrcmp(cur->name, DTD_ELEM_RCTL) != 0)
6276 			continue;
6277 		if ((fetchprop(cur, DTD_ATTR_NAME, savedname,
6278 		    sizeof (savedname)) == Z_OK) &&
6279 		    (strcmp(savedname, aliases[i].realname) == 0)) {
6280 
6281 			/*
6282 			 * If we already saw one of these, we can't have an
6283 			 * alias since we just found another.
6284 			 */
6285 			if (found)
6286 				return (Z_ALIAS_DISALLOW);
6287 			found = B_TRUE;
6288 
6289 			for (val = cur->xmlChildrenNode; val != NULL;
6290 			    val = val->next) {
6291 				/*
6292 				 * If we already have one value, we can't have
6293 				 * an alias since we just found another.
6294 				 */
6295 				if (found_val)
6296 					return (Z_ALIAS_DISALLOW);
6297 				found_val = B_TRUE;
6298 
6299 				if ((fetchprop(val, DTD_ATTR_PRIV,
6300 				    rctl.zone_rctlval_priv,
6301 				    sizeof (rctl.zone_rctlval_priv)) != Z_OK))
6302 					break;
6303 				if ((fetchprop(val, DTD_ATTR_LIMIT,
6304 				    rctl.zone_rctlval_limit,
6305 				    sizeof (rctl.zone_rctlval_limit)) != Z_OK))
6306 					break;
6307 				if ((fetchprop(val, DTD_ATTR_ACTION,
6308 				    rctl.zone_rctlval_action,
6309 				    sizeof (rctl.zone_rctlval_action)) != Z_OK))
6310 					break;
6311 			}
6312 
6313 			/* check priv and action match the expected vals */
6314 			if (strcmp(rctl.zone_rctlval_priv,
6315 			    aliases[i].priv) != 0 ||
6316 			    strcmp(rctl.zone_rctlval_action,
6317 			    aliases[i].action) != 0)
6318 				return (Z_ALIAS_DISALLOW);
6319 		}
6320 	}
6321 
6322 	if (found) {
6323 		*rval = strtoull(rctl.zone_rctlval_limit, NULL, 10);
6324 		return (Z_OK);
6325 	}
6326 
6327 	return (Z_NO_ENTRY);
6328 }
6329 
6330 int
6331 zonecfg_rm_aliased_rctl(zone_dochandle_t handle, char *name)
6332 {
6333 	int i;
6334 	uint64_t val;
6335 	struct zone_rctltab rctltab;
6336 
6337 	/*
6338 	 * First check that we have a valid aliased rctl to remove.
6339 	 * This will catch an rctl entry with non-standard values or
6340 	 * multiple rctl values for this name.  We need to ignore those
6341 	 * rctl entries.
6342 	 */
6343 	if (zonecfg_get_aliased_rctl(handle, name, &val) != Z_OK)
6344 		return (Z_OK);
6345 
6346 	for (i = 0; aliases[i].shortname != NULL; i++)
6347 		if (strcmp(name, aliases[i].shortname) == 0)
6348 			break;
6349 
6350 	if (aliases[i].shortname == NULL)
6351 		return (Z_NO_RESOURCE_ID);
6352 
6353 	(void) strlcpy(rctltab.zone_rctl_name, aliases[i].realname,
6354 	    sizeof (rctltab.zone_rctl_name));
6355 
6356 	return (zonecfg_delete_rctl(handle, &rctltab));
6357 }
6358 
6359 boolean_t
6360 zonecfg_aliased_rctl_ok(zone_dochandle_t handle, char *name)
6361 {
6362 	uint64_t tmp_val;
6363 
6364 	switch (zonecfg_get_aliased_rctl(handle, name, &tmp_val)) {
6365 	case Z_OK:
6366 		/*FALLTHRU*/
6367 	case Z_NO_ENTRY:
6368 		return (B_TRUE);
6369 	default:
6370 		return (B_FALSE);
6371 	}
6372 }
6373 
6374 int
6375 zonecfg_set_aliased_rctl(zone_dochandle_t handle, char *name, uint64_t val)
6376 {
6377 	int i;
6378 	int err;
6379 	struct zone_rctltab rctltab;
6380 	struct zone_rctlvaltab *rctlvaltab;
6381 	char buf[128];
6382 
6383 	if (!zonecfg_aliased_rctl_ok(handle, name))
6384 		return (Z_ALIAS_DISALLOW);
6385 
6386 	for (i = 0; aliases[i].shortname != NULL; i++)
6387 		if (strcmp(name, aliases[i].shortname) == 0)
6388 			break;
6389 
6390 	if (aliases[i].shortname == NULL)
6391 		return (Z_NO_RESOURCE_ID);
6392 
6393 	/* remove any pre-existing definition for this rctl */
6394 	(void) zonecfg_rm_aliased_rctl(handle, name);
6395 
6396 	(void) strlcpy(rctltab.zone_rctl_name, aliases[i].realname,
6397 	    sizeof (rctltab.zone_rctl_name));
6398 
6399 	rctltab.zone_rctl_valptr = NULL;
6400 
6401 	if ((rctlvaltab = calloc(1, sizeof (struct zone_rctlvaltab))) == NULL)
6402 		return (Z_NOMEM);
6403 
6404 	(void) snprintf(buf, sizeof (buf), "%llu", (long long)val);
6405 
6406 	(void) strlcpy(rctlvaltab->zone_rctlval_priv, aliases[i].priv,
6407 	    sizeof (rctlvaltab->zone_rctlval_priv));
6408 	(void) strlcpy(rctlvaltab->zone_rctlval_limit, buf,
6409 	    sizeof (rctlvaltab->zone_rctlval_limit));
6410 	(void) strlcpy(rctlvaltab->zone_rctlval_action, aliases[i].action,
6411 	    sizeof (rctlvaltab->zone_rctlval_action));
6412 
6413 	rctlvaltab->zone_rctlval_next = NULL;
6414 
6415 	if ((err = zonecfg_add_rctl_value(&rctltab, rctlvaltab)) != Z_OK)
6416 		return (err);
6417 
6418 	return (zonecfg_add_rctl(handle, &rctltab));
6419 }
6420 
6421 static int
6422 delete_tmp_pool(zone_dochandle_t handle)
6423 {
6424 	int err;
6425 	xmlNodePtr cur = handle->zone_dh_cur;
6426 
6427 	if ((err = operation_prep(handle)) != Z_OK)
6428 		return (err);
6429 
6430 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
6431 		if (xmlStrcmp(cur->name, DTD_ELEM_TMPPOOL) == 0) {
6432 			xmlUnlinkNode(cur);
6433 			xmlFreeNode(cur);
6434 			return (Z_OK);
6435 		}
6436 	}
6437 
6438 	return (Z_NO_RESOURCE_ID);
6439 }
6440 
6441 static int
6442 modify_tmp_pool(zone_dochandle_t handle, char *pool_importance)
6443 {
6444 	int err;
6445 	xmlNodePtr cur = handle->zone_dh_cur;
6446 	xmlNodePtr newnode;
6447 
6448 	err = delete_tmp_pool(handle);
6449 	if (err != Z_OK && err != Z_NO_RESOURCE_ID)
6450 		return (err);
6451 
6452 	if (*pool_importance != '\0') {
6453 		if ((err = operation_prep(handle)) != Z_OK)
6454 			return (err);
6455 
6456 		newnode = xmlNewTextChild(cur, NULL, DTD_ELEM_TMPPOOL, NULL);
6457 		if ((err = newprop(newnode, DTD_ATTR_IMPORTANCE,
6458 		    pool_importance)) != Z_OK)
6459 			return (err);
6460 	}
6461 
6462 	return (Z_OK);
6463 }
6464 
6465 static int
6466 add_pset_core(zone_dochandle_t handle, struct zone_psettab *tabptr)
6467 {
6468 	xmlNodePtr newnode, cur = handle->zone_dh_cur;
6469 	int err;
6470 
6471 	newnode = xmlNewTextChild(cur, NULL, DTD_ELEM_PSET, NULL);
6472 	if ((err = newprop(newnode, DTD_ATTR_NCPU_MIN,
6473 	    tabptr->zone_ncpu_min)) != Z_OK)
6474 		return (err);
6475 	if ((err = newprop(newnode, DTD_ATTR_NCPU_MAX,
6476 	    tabptr->zone_ncpu_max)) != Z_OK)
6477 		return (err);
6478 
6479 	if ((err = modify_tmp_pool(handle, tabptr->zone_importance)) != Z_OK)
6480 		return (err);
6481 
6482 	return (Z_OK);
6483 }
6484 
6485 int
6486 zonecfg_add_pset(zone_dochandle_t handle, struct zone_psettab *tabptr)
6487 {
6488 	int err;
6489 
6490 	if (tabptr == NULL)
6491 		return (Z_INVAL);
6492 
6493 	if ((err = operation_prep(handle)) != Z_OK)
6494 		return (err);
6495 
6496 	if ((err = add_pset_core(handle, tabptr)) != Z_OK)
6497 		return (err);
6498 
6499 	return (Z_OK);
6500 }
6501 
6502 int
6503 zonecfg_delete_pset(zone_dochandle_t handle)
6504 {
6505 	int err;
6506 	int res = Z_NO_RESOURCE_ID;
6507 	xmlNodePtr cur = handle->zone_dh_cur;
6508 
6509 	if ((err = operation_prep(handle)) != Z_OK)
6510 		return (err);
6511 
6512 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
6513 		if (xmlStrcmp(cur->name, DTD_ELEM_PSET) == 0) {
6514 			xmlUnlinkNode(cur);
6515 			xmlFreeNode(cur);
6516 			res = Z_OK;
6517 			break;
6518 		}
6519 	}
6520 
6521 	/*
6522 	 * Once we have msets, we should check that a mset
6523 	 * do not exist before we delete the tmp_pool data.
6524 	 */
6525 	err = delete_tmp_pool(handle);
6526 	if (err != Z_OK && err != Z_NO_RESOURCE_ID)
6527 		return (err);
6528 
6529 	return (res);
6530 }
6531 
6532 int
6533 zonecfg_modify_pset(zone_dochandle_t handle, struct zone_psettab *tabptr)
6534 {
6535 	int err;
6536 
6537 	if (tabptr == NULL)
6538 		return (Z_INVAL);
6539 
6540 	if ((err = zonecfg_delete_pset(handle)) != Z_OK)
6541 		return (err);
6542 
6543 	if ((err = add_pset_core(handle, tabptr)) != Z_OK)
6544 		return (err);
6545 
6546 	return (Z_OK);
6547 }
6548 
6549 int
6550 zonecfg_lookup_pset(zone_dochandle_t handle, struct zone_psettab *tabptr)
6551 {
6552 	xmlNodePtr cur;
6553 	int err;
6554 	int res = Z_NO_ENTRY;
6555 
6556 	if (tabptr == NULL)
6557 		return (Z_INVAL);
6558 
6559 	if ((err = operation_prep(handle)) != Z_OK)
6560 		return (err);
6561 
6562 	/* this is an optional component */
6563 	tabptr->zone_importance[0] = '\0';
6564 
6565 	cur = handle->zone_dh_cur;
6566 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
6567 		if (xmlStrcmp(cur->name, DTD_ELEM_PSET) == 0) {
6568 			if ((err = fetchprop(cur, DTD_ATTR_NCPU_MIN,
6569 			    tabptr->zone_ncpu_min,
6570 			    sizeof (tabptr->zone_ncpu_min))) != Z_OK) {
6571 				handle->zone_dh_cur = handle->zone_dh_top;
6572 				return (err);
6573 			}
6574 
6575 			if ((err = fetchprop(cur, DTD_ATTR_NCPU_MAX,
6576 			    tabptr->zone_ncpu_max,
6577 			    sizeof (tabptr->zone_ncpu_max))) != Z_OK) {
6578 				handle->zone_dh_cur = handle->zone_dh_top;
6579 				return (err);
6580 			}
6581 
6582 			res = Z_OK;
6583 
6584 		} else if (xmlStrcmp(cur->name, DTD_ELEM_TMPPOOL) == 0) {
6585 			if ((err = fetchprop(cur, DTD_ATTR_IMPORTANCE,
6586 			    tabptr->zone_importance,
6587 			    sizeof (tabptr->zone_importance))) != Z_OK) {
6588 				handle->zone_dh_cur = handle->zone_dh_top;
6589 				return (err);
6590 			}
6591 		}
6592 	}
6593 
6594 	return (res);
6595 }
6596 
6597 int
6598 zonecfg_getpsetent(zone_dochandle_t handle, struct zone_psettab *tabptr)
6599 {
6600 	int err;
6601 
6602 	if ((err = zonecfg_setent(handle)) != Z_OK)
6603 		return (err);
6604 
6605 	err = zonecfg_lookup_pset(handle, tabptr);
6606 
6607 	(void) zonecfg_endent(handle);
6608 
6609 	return (err);
6610 }
6611 
6612 static int
6613 add_mcap(zone_dochandle_t handle, struct zone_mcaptab *tabptr)
6614 {
6615 	xmlNodePtr newnode, cur = handle->zone_dh_cur;
6616 	int err;
6617 
6618 	newnode = xmlNewTextChild(cur, NULL, DTD_ELEM_MCAP, NULL);
6619 	if ((err = newprop(newnode, DTD_ATTR_PHYSCAP, tabptr->zone_physmem_cap))
6620 	    != Z_OK)
6621 		return (err);
6622 
6623 	return (Z_OK);
6624 }
6625 
6626 int
6627 zonecfg_delete_mcap(zone_dochandle_t handle)
6628 {
6629 	int err;
6630 	xmlNodePtr cur = handle->zone_dh_cur;
6631 
6632 	if ((err = operation_prep(handle)) != Z_OK)
6633 		return (err);
6634 
6635 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
6636 		if (xmlStrcmp(cur->name, DTD_ELEM_MCAP) != 0)
6637 			continue;
6638 
6639 		xmlUnlinkNode(cur);
6640 		xmlFreeNode(cur);
6641 		return (Z_OK);
6642 	}
6643 	return (Z_NO_RESOURCE_ID);
6644 }
6645 
6646 int
6647 zonecfg_modify_mcap(zone_dochandle_t handle, struct zone_mcaptab *tabptr)
6648 {
6649 	int err;
6650 
6651 	if (tabptr == NULL)
6652 		return (Z_INVAL);
6653 
6654 	err = zonecfg_delete_mcap(handle);
6655 	/* it is ok if there is no mcap entry */
6656 	if (err != Z_OK && err != Z_NO_RESOURCE_ID)
6657 		return (err);
6658 
6659 	if ((err = add_mcap(handle, tabptr)) != Z_OK)
6660 		return (err);
6661 
6662 	return (Z_OK);
6663 }
6664 
6665 int
6666 zonecfg_lookup_mcap(zone_dochandle_t handle, struct zone_mcaptab *tabptr)
6667 {
6668 	xmlNodePtr cur;
6669 	int err;
6670 
6671 	if (tabptr == NULL)
6672 		return (Z_INVAL);
6673 
6674 	if ((err = operation_prep(handle)) != Z_OK)
6675 		return (err);
6676 
6677 	cur = handle->zone_dh_cur;
6678 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
6679 		if (xmlStrcmp(cur->name, DTD_ELEM_MCAP) != 0)
6680 			continue;
6681 		if ((err = fetchprop(cur, DTD_ATTR_PHYSCAP,
6682 		    tabptr->zone_physmem_cap,
6683 		    sizeof (tabptr->zone_physmem_cap))) != Z_OK) {
6684 			handle->zone_dh_cur = handle->zone_dh_top;
6685 			return (err);
6686 		}
6687 
6688 		return (Z_OK);
6689 	}
6690 
6691 	return (Z_NO_ENTRY);
6692 }
6693 
6694 static int
6695 getmcapent_core(zone_dochandle_t handle, struct zone_mcaptab *tabptr)
6696 {
6697 	xmlNodePtr cur;
6698 	int err;
6699 
6700 	if (handle == NULL)
6701 		return (Z_INVAL);
6702 
6703 	if ((cur = handle->zone_dh_cur) == NULL)
6704 		return (Z_NO_ENTRY);
6705 
6706 	for (; cur != NULL; cur = cur->next)
6707 		if (xmlStrcmp(cur->name, DTD_ELEM_MCAP) == 0)
6708 			break;
6709 	if (cur == NULL) {
6710 		handle->zone_dh_cur = handle->zone_dh_top;
6711 		return (Z_NO_ENTRY);
6712 	}
6713 
6714 	if ((err = fetchprop(cur, DTD_ATTR_PHYSCAP, tabptr->zone_physmem_cap,
6715 	    sizeof (tabptr->zone_physmem_cap))) != Z_OK) {
6716 		handle->zone_dh_cur = handle->zone_dh_top;
6717 		return (err);
6718 	}
6719 
6720 	handle->zone_dh_cur = cur->next;
6721 	return (Z_OK);
6722 }
6723 
6724 int
6725 zonecfg_getmcapent(zone_dochandle_t handle, struct zone_mcaptab *tabptr)
6726 {
6727 	int err;
6728 
6729 	if ((err = zonecfg_setent(handle)) != Z_OK)
6730 		return (err);
6731 
6732 	err = getmcapent_core(handle, tabptr);
6733 
6734 	(void) zonecfg_endent(handle);
6735 
6736 	return (err);
6737 }
6738 
6739 /*
6740  * Get the full tree of pkg/patch metadata in a set of nested AVL trees.
6741  * pkgs_avl is an AVL tree of pkgs.  Each pkg element contains a
6742  * zpe_patches_avl member which holds an AVL tree of patches for that pkg.
6743  * The patch elements have the same zpe_patches_avl member, each of which can
6744  * hold an AVL tree of patches that are obsoleted by the patch.
6745  *
6746  * The zone xml data contains DTD_ELEM_PACKAGE elements, followed by
6747  * DTD_ELEM_PATCH elements.  The DTD_ELEM_PATCH patch element applies to the
6748  * DTD_ELEM_PACKAGE that precedes it.  The DTD_ELEM_PATCH element may have
6749  * child DTD_ELEM_OBSOLETES nodes associated with it.  The DTD_ELEM_PACKAGE
6750  * really should have had the DTD_ELEM_PATCH elements as children but it
6751  * was not defined that way initially so we are stuck with the DTD definition
6752  * now.  However, we can safely assume the ordering for compatibility.
6753  */
6754 int
6755 zonecfg_getpkgdata(zone_dochandle_t handle, uu_avl_pool_t *pkg_pool,
6756     uu_avl_t *pkgs_avl)
6757 {
6758 	xmlNodePtr cur;
6759 	int res;
6760 	zone_pkg_entry_t *pkg;
6761 	char name[MAXNAMELEN];
6762 	char version[ZONE_PKG_VERSMAX];
6763 
6764 	if (handle == NULL)
6765 		return (Z_INVAL);
6766 
6767 	if ((res = zonecfg_setent(handle)) != Z_OK)
6768 		return (res);
6769 
6770 	if ((cur = handle->zone_dh_cur) == NULL) {
6771 		res = Z_NO_ENTRY;
6772 		goto done;
6773 	}
6774 
6775 	for (; cur != NULL; cur = cur->next) {
6776 		if (xmlStrcmp(cur->name, DTD_ELEM_PACKAGE) == 0) {
6777 			uu_avl_index_t where;
6778 
6779 			if ((res = fetchprop(cur, DTD_ATTR_NAME, name,
6780 			    sizeof (name))) != Z_OK)
6781 				goto done;
6782 
6783 			if ((res = fetchprop(cur, DTD_ATTR_VERSION, version,
6784 			    sizeof (version))) != Z_OK)
6785 				goto done;
6786 
6787 			if ((pkg = (zone_pkg_entry_t *)
6788 			    malloc(sizeof (zone_pkg_entry_t))) == NULL) {
6789 				res = Z_NOMEM;
6790 				goto done;
6791 			}
6792 
6793 			if ((pkg->zpe_name = strdup(name)) == NULL) {
6794 				free(pkg);
6795 				res = Z_NOMEM;
6796 				goto done;
6797 			}
6798 
6799 			if ((pkg->zpe_vers = strdup(version)) == NULL) {
6800 				free(pkg->zpe_name);
6801 				free(pkg);
6802 				res = Z_NOMEM;
6803 				goto done;
6804 			}
6805 
6806 			pkg->zpe_patches_avl = NULL;
6807 
6808 			uu_avl_node_init(pkg, &pkg->zpe_entry, pkg_pool);
6809 			if (uu_avl_find(pkgs_avl, pkg, NULL, &where) != NULL) {
6810 				free(pkg->zpe_name);
6811 				free(pkg->zpe_vers);
6812 				free(pkg);
6813 			} else {
6814 				uu_avl_insert(pkgs_avl, pkg, where);
6815 			}
6816 
6817 		} else if (xmlStrcmp(cur->name, DTD_ELEM_PATCH) == 0) {
6818 			zone_pkg_entry_t *patch;
6819 			uu_avl_index_t where;
6820 			char *p;
6821 			char *dashp = NULL;
6822 			xmlNodePtr child;
6823 
6824 			if ((res = fetchprop(cur, DTD_ATTR_ID, name,
6825 			    sizeof (name))) != Z_OK)
6826 				goto done;
6827 
6828 			if ((patch = (zone_pkg_entry_t *)
6829 			    malloc(sizeof (zone_pkg_entry_t))) == NULL) {
6830 				res = Z_NOMEM;
6831 				goto done;
6832 			}
6833 
6834 			if ((p = strchr(name, '-')) != NULL) {
6835 				dashp = p;
6836 				*p++ = '\0';
6837 			} else {
6838 				p = "";
6839 			}
6840 
6841 			if ((patch->zpe_name = strdup(name)) == NULL) {
6842 				free(patch);
6843 				res = Z_NOMEM;
6844 				goto done;
6845 			}
6846 
6847 			if ((patch->zpe_vers = strdup(p)) == NULL) {
6848 				free(patch->zpe_name);
6849 				free(patch);
6850 				res = Z_NOMEM;
6851 				goto done;
6852 			}
6853 
6854 			if (dashp != NULL)
6855 				*dashp = '-';
6856 
6857 			patch->zpe_patches_avl = NULL;
6858 
6859 			if (pkg->zpe_patches_avl == NULL) {
6860 				pkg->zpe_patches_avl = uu_avl_create(pkg_pool,
6861 				    NULL, UU_DEFAULT);
6862 				if (pkg->zpe_patches_avl == NULL) {
6863 					free(patch->zpe_name);
6864 					free(patch->zpe_vers);
6865 					free(patch);
6866 					res = Z_NOMEM;
6867 					goto done;
6868 				}
6869 			}
6870 
6871 			uu_avl_node_init(patch, &patch->zpe_entry, pkg_pool);
6872 			if (uu_avl_find(pkg->zpe_patches_avl, patch, NULL,
6873 			    &where) != NULL) {
6874 				free(patch->zpe_name);
6875 				free(patch->zpe_vers);
6876 				free(patch);
6877 			} else {
6878 				uu_avl_insert(pkg->zpe_patches_avl, patch,
6879 				    where);
6880 			}
6881 
6882 			/* Add any patches this patch obsoletes. */
6883 			for (child = cur->xmlChildrenNode; child != NULL;
6884 			    child = child->next) {
6885 				zone_pkg_entry_t *obs;
6886 
6887 				if (xmlStrcmp(child->name, DTD_ELEM_OBSOLETES)
6888 				    != 0)
6889 					continue;
6890 
6891 				if ((res = fetchprop(child, DTD_ATTR_ID,
6892 				    name, sizeof (name))) != Z_OK)
6893 					goto done;
6894 
6895 				if ((obs = (zone_pkg_entry_t *)malloc(
6896 				    sizeof (zone_pkg_entry_t))) == NULL) {
6897 					res = Z_NOMEM;
6898 					goto done;
6899 				}
6900 
6901 				if ((obs->zpe_name = strdup(name)) == NULL) {
6902 					free(obs);
6903 					res = Z_NOMEM;
6904 					goto done;
6905 				}
6906 				/*
6907 				 * The version doesn't matter for obsoleted
6908 				 * patches.
6909 				 */
6910 				obs->zpe_vers = NULL;
6911 				obs->zpe_patches_avl = NULL;
6912 
6913 				/*
6914 				 * If this is the first obsolete patch, add an
6915 				 * AVL tree to the parent patch element.
6916 				 */
6917 				if (patch->zpe_patches_avl == NULL) {
6918 					patch->zpe_patches_avl =
6919 					    uu_avl_create(pkg_pool, NULL,
6920 					    UU_DEFAULT);
6921 					if (patch->zpe_patches_avl == NULL) {
6922 						free(obs->zpe_name);
6923 						free(obs);
6924 						res = Z_NOMEM;
6925 						goto done;
6926 					}
6927 				}
6928 
6929 				/* Insert obsolete patch into the AVL tree. */
6930 				uu_avl_node_init(obs, &obs->zpe_entry,
6931 				    pkg_pool);
6932 				if (uu_avl_find(patch->zpe_patches_avl, obs,
6933 				    NULL, &where) != NULL) {
6934 					free(obs->zpe_name);
6935 					free(obs);
6936 				} else {
6937 					uu_avl_insert(patch->zpe_patches_avl,
6938 					    obs, where);
6939 				}
6940 			}
6941 		}
6942 	}
6943 
6944 done:
6945 	(void) zonecfg_endent(handle);
6946 	return (res);
6947 }
6948 
6949 int
6950 zonecfg_setdevperment(zone_dochandle_t handle)
6951 {
6952 	return (zonecfg_setent(handle));
6953 }
6954 
6955 int
6956 zonecfg_getdevperment(zone_dochandle_t handle, struct zone_devpermtab *tabptr)
6957 {
6958 	xmlNodePtr cur;
6959 	int err;
6960 	char buf[128];
6961 
6962 	tabptr->zone_devperm_acl = NULL;
6963 
6964 	if (handle == NULL)
6965 		return (Z_INVAL);
6966 
6967 	if ((cur = handle->zone_dh_cur) == NULL)
6968 		return (Z_NO_ENTRY);
6969 
6970 	for (; cur != NULL; cur = cur->next)
6971 		if (!xmlStrcmp(cur->name, DTD_ELEM_DEV_PERM))
6972 			break;
6973 	if (cur == NULL) {
6974 		handle->zone_dh_cur = handle->zone_dh_top;
6975 		return (Z_NO_ENTRY);
6976 	}
6977 
6978 	if ((err = fetchprop(cur, DTD_ATTR_NAME, tabptr->zone_devperm_name,
6979 	    sizeof (tabptr->zone_devperm_name))) != Z_OK) {
6980 		handle->zone_dh_cur = handle->zone_dh_top;
6981 		return (err);
6982 	}
6983 
6984 	if ((err = fetchprop(cur, DTD_ATTR_UID, buf, sizeof (buf))) != Z_OK) {
6985 		handle->zone_dh_cur = handle->zone_dh_top;
6986 		return (err);
6987 	}
6988 	tabptr->zone_devperm_uid = (uid_t)atol(buf);
6989 
6990 	if ((err = fetchprop(cur, DTD_ATTR_GID, buf, sizeof (buf))) != Z_OK) {
6991 		handle->zone_dh_cur = handle->zone_dh_top;
6992 		return (err);
6993 	}
6994 	tabptr->zone_devperm_gid = (gid_t)atol(buf);
6995 
6996 	if ((err = fetchprop(cur, DTD_ATTR_MODE, buf, sizeof (buf))) != Z_OK) {
6997 		handle->zone_dh_cur = handle->zone_dh_top;
6998 		return (err);
6999 	}
7000 	tabptr->zone_devperm_mode = (mode_t)strtol(buf, (char **)NULL, 8);
7001 
7002 	if ((err = fetch_alloc_prop(cur, DTD_ATTR_ACL,
7003 	    &(tabptr->zone_devperm_acl))) != Z_OK) {
7004 		handle->zone_dh_cur = handle->zone_dh_top;
7005 		return (err);
7006 	}
7007 
7008 	handle->zone_dh_cur = cur->next;
7009 	return (Z_OK);
7010 }
7011 
7012 int
7013 zonecfg_enddevperment(zone_dochandle_t handle)
7014 {
7015 	return (zonecfg_endent(handle));
7016 }
7017 
7018 /* PRINTFLIKE1 */
7019 static void
7020 zerror(const char *zone_name, const char *fmt, ...)
7021 {
7022 	va_list alist;
7023 
7024 	va_start(alist, fmt);
7025 	(void) fprintf(stderr, "zone '%s': ", zone_name);
7026 	(void) vfprintf(stderr, fmt, alist);
7027 	(void) fprintf(stderr, "\n");
7028 	va_end(alist);
7029 }
7030 
7031 static void
7032 zperror(const char *str)
7033 {
7034 	(void) fprintf(stderr, "%s: %s\n", str, strerror(errno));
7035 }
7036 
7037 /*
7038  * The following three routines implement a simple locking mechanism to
7039  * ensure that only one instance of zoneadm at a time is able to manipulate
7040  * a given zone.  The lock is built on top of an fcntl(2) lock of
7041  * [<altroot>]/var/run/zones/<zonename>.zoneadm.lock.  If a zoneadm instance
7042  * can grab that lock, it is allowed to manipulate the zone.
7043  *
7044  * Since zoneadm may call external applications which in turn invoke
7045  * zoneadm again, we introduce the notion of "lock inheritance".  Any
7046  * instance of zoneadm that has another instance in its ancestry is assumed
7047  * to be acting on behalf of the original zoneadm, and is thus allowed to
7048  * manipulate its zone.
7049  *
7050  * This inheritance is implemented via the _ZONEADM_LOCK_HELD environment
7051  * variable.  When zoneadm is granted a lock on its zone, this environment
7052  * variable is set to 1.  When it releases the lock, the variable is set to
7053  * 0.  Since a child process inherits its parent's environment, checking
7054  * the state of this variable indicates whether or not any ancestor owns
7055  * the lock.
7056  */
7057 void
7058 zonecfg_init_lock_file(const char *zone_name, char **lock_env)
7059 {
7060 	*lock_env = getenv(LOCK_ENV_VAR);
7061 	if (*lock_env == NULL) {
7062 		if (putenv(zoneadm_lock_not_held) != 0) {
7063 			zerror(zone_name, gettext("could not set env: %s"),
7064 			    strerror(errno));
7065 			exit(1);
7066 		}
7067 	} else {
7068 		if (atoi(*lock_env) == 1)
7069 			zone_lock_cnt = 1;
7070 	}
7071 }
7072 
7073 void
7074 zonecfg_release_lock_file(const char *zone_name, int lockfd)
7075 {
7076 	/*
7077 	 * If we are cleaning up from a failed attempt to lock the zone for
7078 	 * the first time, we might have a zone_lock_cnt of 0.  In that
7079 	 * error case, we don't want to do anything but close the lock
7080 	 * file.
7081 	 */
7082 	assert(zone_lock_cnt >= 0);
7083 	if (zone_lock_cnt > 0) {
7084 		assert(getenv(LOCK_ENV_VAR) != NULL);
7085 		assert(atoi(getenv(LOCK_ENV_VAR)) == 1);
7086 		if (--zone_lock_cnt > 0) {
7087 			assert(lockfd == -1);
7088 			return;
7089 		}
7090 		if (putenv(zoneadm_lock_not_held) != 0) {
7091 			zerror(zone_name, gettext("could not set env: %s"),
7092 			    strerror(errno));
7093 			exit(1);
7094 		}
7095 	}
7096 	assert(lockfd >= 0);
7097 	(void) close(lockfd);
7098 }
7099 
7100 int
7101 zonecfg_grab_lock_file(const char *zone_name, int *lockfd)
7102 {
7103 	char pathbuf[PATH_MAX];
7104 	struct flock flock;
7105 
7106 	/*
7107 	 * If we already have the lock, we can skip this expensive song
7108 	 * and dance.
7109 	 */
7110 	assert(zone_lock_cnt >= 0);
7111 	assert(getenv(LOCK_ENV_VAR) != NULL);
7112 	if (zone_lock_cnt > 0) {
7113 		assert(atoi(getenv(LOCK_ENV_VAR)) == 1);
7114 		zone_lock_cnt++;
7115 		*lockfd = -1;
7116 		return (Z_OK);
7117 	}
7118 	assert(getenv(LOCK_ENV_VAR) != NULL);
7119 	assert(atoi(getenv(LOCK_ENV_VAR)) == 0);
7120 
7121 	if (snprintf(pathbuf, sizeof (pathbuf), "%s%s", zonecfg_get_root(),
7122 	    ZONES_TMPDIR) >= sizeof (pathbuf)) {
7123 		zerror(zone_name, gettext("alternate root path is too long"));
7124 		return (-1);
7125 	}
7126 	if (mkdir(pathbuf, S_IRWXU) < 0 && errno != EEXIST) {
7127 		zerror(zone_name, gettext("could not mkdir %s: %s"), pathbuf,
7128 		    strerror(errno));
7129 		return (-1);
7130 	}
7131 	(void) chmod(pathbuf, S_IRWXU);
7132 
7133 	/*
7134 	 * One of these lock files is created for each zone (when needed).
7135 	 * The lock files are not cleaned up (except on system reboot),
7136 	 * but since there is only one per zone, there is no resource
7137 	 * starvation issue.
7138 	 */
7139 	if (snprintf(pathbuf, sizeof (pathbuf), "%s%s/%s.zoneadm.lock",
7140 	    zonecfg_get_root(), ZONES_TMPDIR, zone_name) >= sizeof (pathbuf)) {
7141 		zerror(zone_name, gettext("alternate root path is too long"));
7142 		return (-1);
7143 	}
7144 	if ((*lockfd = open(pathbuf, O_RDWR|O_CREAT, S_IRUSR|S_IWUSR)) < 0) {
7145 		zerror(zone_name, gettext("could not open %s: %s"), pathbuf,
7146 		    strerror(errno));
7147 		return (-1);
7148 	}
7149 	/*
7150 	 * Lock the file to synchronize with other zoneadmds
7151 	 */
7152 	flock.l_type = F_WRLCK;
7153 	flock.l_whence = SEEK_SET;
7154 	flock.l_start = (off_t)0;
7155 	flock.l_len = (off_t)0;
7156 	if ((fcntl(*lockfd, F_SETLKW, &flock) < 0) ||
7157 	    (putenv(zoneadm_lock_held) != 0)) {
7158 		zerror(zone_name, gettext("unable to lock %s: %s"), pathbuf,
7159 		    strerror(errno));
7160 		zonecfg_release_lock_file(zone_name, *lockfd);
7161 		return (-1);
7162 	}
7163 	zone_lock_cnt = 1;
7164 	return (Z_OK);
7165 }
7166 
7167 boolean_t
7168 zonecfg_lock_file_held(int *lockfd)
7169 {
7170 	if (*lockfd >= 0 || zone_lock_cnt > 0)
7171 		return (B_TRUE);
7172 	return (B_FALSE);
7173 }
7174 
7175 static boolean_t
7176 get_doorname(const char *zone_name, char *buffer)
7177 {
7178 	return (snprintf(buffer, PATH_MAX, "%s" ZONE_DOOR_PATH,
7179 	    zonecfg_get_root(), zone_name) < PATH_MAX);
7180 }
7181 
7182 /*
7183  * system daemons are not audited.  For the global zone, this occurs
7184  * "naturally" since init is started with the default audit
7185  * characteristics.  Since zoneadmd is a system daemon and it starts
7186  * init for a zone, it is necessary to clear out the audit
7187  * characteristics inherited from whomever started zoneadmd.  This is
7188  * indicated by the audit id, which is set from the ruid parameter of
7189  * adt_set_user(), below.
7190  */
7191 
7192 static void
7193 prepare_audit_context(const char *zone_name)
7194 {
7195 	adt_session_data_t	*ah;
7196 	char			*failure = gettext("audit failure: %s");
7197 
7198 	if (adt_start_session(&ah, NULL, 0)) {
7199 		zerror(zone_name, failure, strerror(errno));
7200 		return;
7201 	}
7202 	if (adt_set_user(ah, ADT_NO_AUDIT, ADT_NO_AUDIT,
7203 	    ADT_NO_AUDIT, ADT_NO_AUDIT, NULL, ADT_NEW)) {
7204 		zerror(zone_name, failure, strerror(errno));
7205 		(void) adt_end_session(ah);
7206 		return;
7207 	}
7208 	if (adt_set_proc(ah))
7209 		zerror(zone_name, failure, strerror(errno));
7210 
7211 	(void) adt_end_session(ah);
7212 }
7213 
7214 static int
7215 start_zoneadmd(const char *zone_name, boolean_t lock)
7216 {
7217 	char doorpath[PATH_MAX];
7218 	pid_t child_pid;
7219 	int error = -1;
7220 	int doorfd, lockfd;
7221 	struct door_info info;
7222 
7223 	if (!get_doorname(zone_name, doorpath))
7224 		return (-1);
7225 
7226 	if (lock)
7227 		if (zonecfg_grab_lock_file(zone_name, &lockfd) != Z_OK)
7228 			return (-1);
7229 
7230 	/*
7231 	 * Now that we have the lock, re-confirm that the daemon is
7232 	 * *not* up and working fine.  If it is still down, we have a green
7233 	 * light to start it.
7234 	 */
7235 	if ((doorfd = open(doorpath, O_RDONLY)) < 0) {
7236 		if (errno != ENOENT) {
7237 			zperror(doorpath);
7238 			goto out;
7239 		}
7240 	} else {
7241 		if (door_info(doorfd, &info) == 0 &&
7242 		    ((info.di_attributes & DOOR_REVOKED) == 0)) {
7243 			error = Z_OK;
7244 			(void) close(doorfd);
7245 			goto out;
7246 		}
7247 		(void) close(doorfd);
7248 	}
7249 
7250 	if ((child_pid = fork()) == -1) {
7251 		zperror(gettext("could not fork"));
7252 		goto out;
7253 	}
7254 
7255 	if (child_pid == 0) {
7256 		const char *argv[6], **ap;
7257 
7258 		/* child process */
7259 		prepare_audit_context(zone_name);
7260 
7261 		ap = argv;
7262 		*ap++ = "zoneadmd";
7263 		*ap++ = "-z";
7264 		*ap++ = zone_name;
7265 		if (zonecfg_in_alt_root()) {
7266 			*ap++ = "-R";
7267 			*ap++ = zonecfg_get_root();
7268 		}
7269 		*ap = NULL;
7270 
7271 		(void) execv("/usr/lib/zones/zoneadmd", (char * const *)argv);
7272 		/*
7273 		 * TRANSLATION_NOTE
7274 		 * zoneadmd is a literal that should not be translated.
7275 		 */
7276 		zperror(gettext("could not exec zoneadmd"));
7277 		_exit(1);
7278 	} else {
7279 		/* parent process */
7280 		pid_t retval;
7281 		int pstatus = 0;
7282 
7283 		do {
7284 			retval = waitpid(child_pid, &pstatus, 0);
7285 		} while (retval != child_pid);
7286 		if (WIFSIGNALED(pstatus) || (WIFEXITED(pstatus) &&
7287 		    WEXITSTATUS(pstatus) != 0)) {
7288 			zerror(zone_name, gettext("could not start %s"),
7289 			    "zoneadmd");
7290 			goto out;
7291 		}
7292 	}
7293 	error = Z_OK;
7294 out:
7295 	if (lock)
7296 		zonecfg_release_lock_file(zone_name, lockfd);
7297 	return (error);
7298 }
7299 
7300 int
7301 zonecfg_ping_zoneadmd(const char *zone_name)
7302 {
7303 	char doorpath[PATH_MAX];
7304 	int doorfd;
7305 	struct door_info info;
7306 
7307 	if (!get_doorname(zone_name, doorpath))
7308 		return (-1);
7309 
7310 	if ((doorfd = open(doorpath, O_RDONLY)) < 0) {
7311 		return (-1);
7312 	}
7313 	if (door_info(doorfd, &info) == 0 &&
7314 	    ((info.di_attributes & DOOR_REVOKED) == 0)) {
7315 		(void) close(doorfd);
7316 		return (Z_OK);
7317 	}
7318 	(void) close(doorfd);
7319 	return (-1);
7320 }
7321 
7322 int
7323 zonecfg_call_zoneadmd(const char *zone_name, zone_cmd_arg_t *arg, char *locale,
7324     boolean_t lock)
7325 {
7326 	char doorpath[PATH_MAX];
7327 	int doorfd, result;
7328 	door_arg_t darg;
7329 
7330 	zoneid_t zoneid;
7331 	uint64_t uniqid = 0;
7332 
7333 	zone_cmd_rval_t *rvalp;
7334 	size_t rlen;
7335 	char *cp, *errbuf;
7336 
7337 	rlen = getpagesize();
7338 	if ((rvalp = malloc(rlen)) == NULL) {
7339 		zerror(zone_name, gettext("failed to allocate %lu bytes: %s"),
7340 		    rlen, strerror(errno));
7341 		return (-1);
7342 	}
7343 
7344 	if ((zoneid = getzoneidbyname(zone_name)) != ZONE_ID_UNDEFINED) {
7345 		(void) zone_getattr(zoneid, ZONE_ATTR_UNIQID, &uniqid,
7346 		    sizeof (uniqid));
7347 	}
7348 	arg->uniqid = uniqid;
7349 	(void) strlcpy(arg->locale, locale, sizeof (arg->locale));
7350 	if (!get_doorname(zone_name, doorpath)) {
7351 		zerror(zone_name, gettext("alternate root path is too long"));
7352 		free(rvalp);
7353 		return (-1);
7354 	}
7355 
7356 	/*
7357 	 * Loop trying to start zoneadmd; if something goes seriously
7358 	 * wrong we break out and fail.
7359 	 */
7360 	for (;;) {
7361 		if (start_zoneadmd(zone_name, lock) != Z_OK)
7362 			break;
7363 
7364 		if ((doorfd = open(doorpath, O_RDONLY)) < 0) {
7365 			zperror(gettext("failed to open zone door"));
7366 			break;
7367 		}
7368 
7369 		darg.data_ptr = (char *)arg;
7370 		darg.data_size = sizeof (*arg);
7371 		darg.desc_ptr = NULL;
7372 		darg.desc_num = 0;
7373 		darg.rbuf = (char *)rvalp;
7374 		darg.rsize = rlen;
7375 		if (door_call(doorfd, &darg) != 0) {
7376 			(void) close(doorfd);
7377 			/*
7378 			 * We'll get EBADF if the door has been revoked.
7379 			 */
7380 			if (errno != EBADF) {
7381 				zperror(gettext("door_call failed"));
7382 				break;
7383 			}
7384 			continue;	/* take another lap */
7385 		}
7386 		(void) close(doorfd);
7387 
7388 		if (darg.data_size == 0) {
7389 			/* Door server is going away; kick it again. */
7390 			continue;
7391 		}
7392 
7393 		errbuf = rvalp->errbuf;
7394 		while (*errbuf != '\0') {
7395 			/*
7396 			 * Remove any newlines since zerror()
7397 			 * will append one automatically.
7398 			 */
7399 			cp = strchr(errbuf, '\n');
7400 			if (cp != NULL)
7401 				*cp = '\0';
7402 			zerror(zone_name, "%s", errbuf);
7403 			if (cp == NULL)
7404 				break;
7405 			errbuf = cp + 1;
7406 		}
7407 		result = rvalp->rval == 0 ? 0 : -1;
7408 		free(rvalp);
7409 		return (result);
7410 	}
7411 
7412 	free(rvalp);
7413 	return (-1);
7414 }
7415