xref: /titanic_44/usr/src/lib/libzonecfg/common/libzonecfg.c (revision 1d6979b7d6d2e03997e402187268fb05906caeb6)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5ee519a1fSgjelinek  * Common Development and Distribution License (the "License").
6ee519a1fSgjelinek  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
21ffbafc53Scomay 
227c478bd9Sstevel@tonic-gate /*
235679c89fSjv227347  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
27cf8f45c7Sdstaff #include <libsysevent.h>
28cf8f45c7Sdstaff #include <pthread.h>
29cf8f45c7Sdstaff #include <stdlib.h>
307c478bd9Sstevel@tonic-gate #include <errno.h>
317c478bd9Sstevel@tonic-gate #include <fnmatch.h>
327c478bd9Sstevel@tonic-gate #include <strings.h>
337c478bd9Sstevel@tonic-gate #include <unistd.h>
347c478bd9Sstevel@tonic-gate #include <assert.h>
357c478bd9Sstevel@tonic-gate #include <libgen.h>
367c478bd9Sstevel@tonic-gate #include <libintl.h>
377c478bd9Sstevel@tonic-gate #include <alloca.h>
387c478bd9Sstevel@tonic-gate #include <ctype.h>
399acbbeafSnn35248 #include <sys/acl.h>
409acbbeafSnn35248 #include <sys/stat.h>
419acbbeafSnn35248 #include <sys/brand.h>
427c478bd9Sstevel@tonic-gate #include <sys/mntio.h>
437c478bd9Sstevel@tonic-gate #include <sys/mnttab.h>
44cf8f45c7Sdstaff #include <sys/nvpair.h>
459acbbeafSnn35248 #include <sys/types.h>
46f4b3ec61Sdh155122 #include <sys/sockio.h>
475679c89fSjv227347 #include <sys/systeminfo.h>
48ee519a1fSgjelinek #include <ftw.h>
490209230bSgjelinek #include <pool.h>
500209230bSgjelinek #include <libscf.h>
510209230bSgjelinek #include <libproc.h>
520209230bSgjelinek #include <sys/priocntl.h>
5339935be5Sgjelinek #include <libuutil.h>
54ff17c8bfSgjelinek #include <wait.h>
55ff17c8bfSgjelinek #include <bsm/adt.h>
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate #include <arpa/inet.h>
587c478bd9Sstevel@tonic-gate #include <netdb.h>
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate #include <libxml/xmlmemory.h>
617c478bd9Sstevel@tonic-gate #include <libxml/parser.h>
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate #include <libdevinfo.h>
64108322fbScarlsonj #include <uuid/uuid.h>
65ee519a1fSgjelinek #include <dirent.h>
669acbbeafSnn35248 #include <libbrand.h>
67ee519a1fSgjelinek 
687c478bd9Sstevel@tonic-gate #include <libzonecfg.h>
697c478bd9Sstevel@tonic-gate #include "zonecfg_impl.h"
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate #define	_PATH_TMPFILE	"/zonecfg.XXXXXX"
72cf8f45c7Sdstaff #define	ZONE_CB_RETRY_COUNT		10
73cf8f45c7Sdstaff #define	ZONE_EVENT_PING_SUBCLASS	"ping"
74cf8f45c7Sdstaff #define	ZONE_EVENT_PING_PUBLISHER	"solaris"
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate /* Hard-code the DTD element/attribute/entity names just once, here. */
777c478bd9Sstevel@tonic-gate #define	DTD_ELEM_ATTR		(const xmlChar *) "attr"
787c478bd9Sstevel@tonic-gate #define	DTD_ELEM_COMMENT	(const xmlChar *) "comment"
797c478bd9Sstevel@tonic-gate #define	DTD_ELEM_DEVICE		(const xmlChar *) "device"
807c478bd9Sstevel@tonic-gate #define	DTD_ELEM_FS		(const xmlChar *) "filesystem"
817c478bd9Sstevel@tonic-gate #define	DTD_ELEM_FSOPTION	(const xmlChar *) "fsoption"
827c478bd9Sstevel@tonic-gate #define	DTD_ELEM_IPD		(const xmlChar *) "inherited-pkg-dir"
837c478bd9Sstevel@tonic-gate #define	DTD_ELEM_NET		(const xmlChar *) "network"
847c478bd9Sstevel@tonic-gate #define	DTD_ELEM_RCTL		(const xmlChar *) "rctl"
857c478bd9Sstevel@tonic-gate #define	DTD_ELEM_RCTLVALUE	(const xmlChar *) "rctl-value"
867c478bd9Sstevel@tonic-gate #define	DTD_ELEM_ZONE		(const xmlChar *) "zone"
87fa9e4066Sahrens #define	DTD_ELEM_DATASET	(const xmlChar *) "dataset"
880209230bSgjelinek #define	DTD_ELEM_TMPPOOL	(const xmlChar *) "tmp_pool"
890209230bSgjelinek #define	DTD_ELEM_PSET		(const xmlChar *) "pset"
900209230bSgjelinek #define	DTD_ELEM_MCAP		(const xmlChar *) "mcap"
91ee519a1fSgjelinek #define	DTD_ELEM_PACKAGE	(const xmlChar *) "package"
92ee519a1fSgjelinek #define	DTD_ELEM_PATCH		(const xmlChar *) "patch"
936cfd72c6Sgjelinek #define	DTD_ELEM_OBSOLETES	(const xmlChar *) "obsoletes"
94ee519a1fSgjelinek #define	DTD_ELEM_DEV_PERM	(const xmlChar *) "dev-perm"
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate #define	DTD_ATTR_ACTION		(const xmlChar *) "action"
977c478bd9Sstevel@tonic-gate #define	DTD_ATTR_ADDRESS	(const xmlChar *) "address"
987c478bd9Sstevel@tonic-gate #define	DTD_ATTR_AUTOBOOT	(const xmlChar *) "autoboot"
99f4b3ec61Sdh155122 #define	DTD_ATTR_IPTYPE		(const xmlChar *) "ip-type"
100de860bd9Sgfaden #define	DTD_ATTR_DEFROUTER	(const xmlChar *) "defrouter"
1017c478bd9Sstevel@tonic-gate #define	DTD_ATTR_DIR		(const xmlChar *) "directory"
1027c478bd9Sstevel@tonic-gate #define	DTD_ATTR_LIMIT		(const xmlChar *) "limit"
103ffbafc53Scomay #define	DTD_ATTR_LIMITPRIV	(const xmlChar *) "limitpriv"
1043f2f09c1Sdp #define	DTD_ATTR_BOOTARGS	(const xmlChar *) "bootargs"
1050209230bSgjelinek #define	DTD_ATTR_SCHED		(const xmlChar *) "scheduling-class"
1067c478bd9Sstevel@tonic-gate #define	DTD_ATTR_MATCH		(const xmlChar *) "match"
1077c478bd9Sstevel@tonic-gate #define	DTD_ATTR_NAME		(const xmlChar *) "name"
1087c478bd9Sstevel@tonic-gate #define	DTD_ATTR_PHYSICAL	(const xmlChar *) "physical"
1097c478bd9Sstevel@tonic-gate #define	DTD_ATTR_POOL		(const xmlChar *) "pool"
1107c478bd9Sstevel@tonic-gate #define	DTD_ATTR_PRIV		(const xmlChar *) "priv"
1117c478bd9Sstevel@tonic-gate #define	DTD_ATTR_RAW		(const xmlChar *) "raw"
1127c478bd9Sstevel@tonic-gate #define	DTD_ATTR_SPECIAL	(const xmlChar *) "special"
1137c478bd9Sstevel@tonic-gate #define	DTD_ATTR_TYPE		(const xmlChar *) "type"
1147c478bd9Sstevel@tonic-gate #define	DTD_ATTR_VALUE		(const xmlChar *) "value"
1157c478bd9Sstevel@tonic-gate #define	DTD_ATTR_ZONEPATH	(const xmlChar *) "zonepath"
1160209230bSgjelinek #define	DTD_ATTR_NCPU_MIN	(const xmlChar *) "ncpu_min"
1170209230bSgjelinek #define	DTD_ATTR_NCPU_MAX	(const xmlChar *) "ncpu_max"
1180209230bSgjelinek #define	DTD_ATTR_IMPORTANCE	(const xmlChar *) "importance"
1190209230bSgjelinek #define	DTD_ATTR_PHYSCAP	(const xmlChar *) "physcap"
120ee519a1fSgjelinek #define	DTD_ATTR_VERSION	(const xmlChar *) "version"
121ee519a1fSgjelinek #define	DTD_ATTR_ID		(const xmlChar *) "id"
122ee519a1fSgjelinek #define	DTD_ATTR_UID		(const xmlChar *) "uid"
123ee519a1fSgjelinek #define	DTD_ATTR_GID		(const xmlChar *) "gid"
124ee519a1fSgjelinek #define	DTD_ATTR_MODE		(const xmlChar *) "mode"
125ee519a1fSgjelinek #define	DTD_ATTR_ACL		(const xmlChar *) "acl"
1269acbbeafSnn35248 #define	DTD_ATTR_BRAND		(const xmlChar *) "brand"
1275679c89fSjv227347 #define	DTD_ATTR_HOSTID		(const xmlChar *) "hostid"
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate #define	DTD_ENTITY_BOOLEAN	"boolean"
1307c478bd9Sstevel@tonic-gate #define	DTD_ENTITY_DEVPATH	"devpath"
1317c478bd9Sstevel@tonic-gate #define	DTD_ENTITY_DRIVER	"driver"
1327c478bd9Sstevel@tonic-gate #define	DTD_ENTITY_DRVMIN	"drv_min"
1337c478bd9Sstevel@tonic-gate #define	DTD_ENTITY_FALSE	"false"
1347c478bd9Sstevel@tonic-gate #define	DTD_ENTITY_INT		"int"
1357c478bd9Sstevel@tonic-gate #define	DTD_ENTITY_STRING	"string"
1367c478bd9Sstevel@tonic-gate #define	DTD_ENTITY_TRUE		"true"
1377c478bd9Sstevel@tonic-gate #define	DTD_ENTITY_UINT		"uint"
1387c478bd9Sstevel@tonic-gate 
139a1be23daSdp #define	DTD_ENTITY_BOOL_LEN	6	/* "false" */
140a1be23daSdp 
141ee519a1fSgjelinek #define	ATTACH_FORCED	"SUNWattached.xml"
142ee519a1fSgjelinek 
1430209230bSgjelinek #define	TMP_POOL_NAME	"SUNWtmp_%s"
1440209230bSgjelinek #define	MAX_TMP_POOL_NAME	(ZONENAME_MAX + 9)
1450209230bSgjelinek #define	RCAP_SERVICE	"system/rcap:default"
1460209230bSgjelinek #define	POOLD_SERVICE	"system/pools/dynamic:default"
1470209230bSgjelinek 
1480209230bSgjelinek /*
1490209230bSgjelinek  * rctl alias definitions
1500209230bSgjelinek  *
1510209230bSgjelinek  * This holds the alias, the full rctl name, the default priv value, action
1520209230bSgjelinek  * and lower limit.  The functions that handle rctl aliases step through
1530209230bSgjelinek  * this table, matching on the alias, and using the full values for setting
1540209230bSgjelinek  * the rctl entry as well the limit for validation.
1550209230bSgjelinek  */
1560209230bSgjelinek static struct alias {
1570209230bSgjelinek 	char *shortname;
1580209230bSgjelinek 	char *realname;
1590209230bSgjelinek 	char *priv;
1600209230bSgjelinek 	char *action;
1610209230bSgjelinek 	uint64_t low_limit;
1620209230bSgjelinek } aliases[] = {
1630209230bSgjelinek 	{ALIAS_MAXLWPS, "zone.max-lwps", "privileged", "deny", 100},
1640209230bSgjelinek 	{ALIAS_MAXSHMMEM, "zone.max-shm-memory", "privileged", "deny", 0},
1650209230bSgjelinek 	{ALIAS_MAXSHMIDS, "zone.max-shm-ids", "privileged", "deny", 0},
1660209230bSgjelinek 	{ALIAS_MAXMSGIDS, "zone.max-msg-ids", "privileged", "deny", 0},
1670209230bSgjelinek 	{ALIAS_MAXSEMIDS, "zone.max-sem-ids", "privileged", "deny", 0},
1680209230bSgjelinek 	{ALIAS_MAXLOCKEDMEM, "zone.max-locked-memory", "privileged", "deny", 0},
1690209230bSgjelinek 	{ALIAS_MAXSWAP, "zone.max-swap", "privileged", "deny", 0},
1700209230bSgjelinek 	{ALIAS_SHARES, "zone.cpu-shares", "privileged", "none", 0},
171c97ad5cdSakolb 	{ALIAS_CPUCAP, "zone.cpu-cap", "privileged", "deny", 0},
1720209230bSgjelinek 	{NULL, NULL, NULL, NULL, 0}
1730209230bSgjelinek };
1740209230bSgjelinek 
1750209230bSgjelinek /*
1760209230bSgjelinek  * Structure for applying rctls to a running zone.  It allows important
1770209230bSgjelinek  * process values to be passed together easily.
1780209230bSgjelinek  */
1790209230bSgjelinek typedef struct pr_info_handle {
1800209230bSgjelinek 	struct ps_prochandle *pr;
1810209230bSgjelinek 	pid_t pid;
1820209230bSgjelinek } pr_info_handle_t;
1830209230bSgjelinek 
1847c478bd9Sstevel@tonic-gate struct zone_dochandle {
1857c478bd9Sstevel@tonic-gate 	char		*zone_dh_rootdir;
1867c478bd9Sstevel@tonic-gate 	xmlDocPtr	zone_dh_doc;
1877c478bd9Sstevel@tonic-gate 	xmlNodePtr	zone_dh_cur;
1887c478bd9Sstevel@tonic-gate 	xmlNodePtr	zone_dh_top;
189087719fdSdp 	boolean_t	zone_dh_newzone;
190087719fdSdp 	boolean_t	zone_dh_snapshot;
191ee519a1fSgjelinek 	boolean_t	zone_dh_sw_inv;
192087719fdSdp 	char		zone_dh_delete_name[ZONENAME_MAX];
1937c478bd9Sstevel@tonic-gate };
1947c478bd9Sstevel@tonic-gate 
195cf8f45c7Sdstaff struct znotify {
196cf8f45c7Sdstaff 	void * zn_private;
197cf8f45c7Sdstaff 	evchan_t *zn_eventchan;
198cf8f45c7Sdstaff 	int (*zn_callback)(const  char *zonename, zoneid_t zid,
199cf8f45c7Sdstaff 	    const char *newstate, const char *oldstate, hrtime_t when, void *p);
200cf8f45c7Sdstaff 	pthread_mutex_t zn_mutex;
201cf8f45c7Sdstaff 	pthread_cond_t zn_cond;
202cf8f45c7Sdstaff 	pthread_mutex_t zn_bigmutex;
203cf8f45c7Sdstaff 	volatile enum {ZN_UNLOCKED, ZN_LOCKED, ZN_PING_INFLIGHT,
204cf8f45c7Sdstaff 	    ZN_PING_RECEIVED} zn_state;
205cf8f45c7Sdstaff 	char zn_subscriber_id[MAX_SUBID_LEN];
206cf8f45c7Sdstaff 	volatile boolean_t zn_failed;
207cf8f45c7Sdstaff 	int zn_failure_count;
208cf8f45c7Sdstaff };
209cf8f45c7Sdstaff 
210ff17c8bfSgjelinek /* used to track nested zone-lock operations */
211ff17c8bfSgjelinek static int zone_lock_cnt = 0;
212ee519a1fSgjelinek 
213ff17c8bfSgjelinek /* used to communicate lock status to children */
214ff17c8bfSgjelinek #define	LOCK_ENV_VAR	"_ZONEADM_LOCK_HELD"
215ff17c8bfSgjelinek static char zoneadm_lock_held[] = LOCK_ENV_VAR"=1";
216ff17c8bfSgjelinek static char zoneadm_lock_not_held[] = LOCK_ENV_VAR"=0";
21739935be5Sgjelinek 
218108322fbScarlsonj char *zonecfg_root = "";
219108322fbScarlsonj 
2207c478bd9Sstevel@tonic-gate /*
2217c478bd9Sstevel@tonic-gate  * For functions which return int, which is most of the functions herein,
2227c478bd9Sstevel@tonic-gate  * the return values should be from the Z_foo set defined in <libzonecfg.h>.
2237c478bd9Sstevel@tonic-gate  * In some instances, we take pains mapping some libc errno values to Z_foo
2247c478bd9Sstevel@tonic-gate  * values from this set.
2257c478bd9Sstevel@tonic-gate  */
2267c478bd9Sstevel@tonic-gate 
2277c478bd9Sstevel@tonic-gate /*
228108322fbScarlsonj  * Set the root (/) path for all zonecfg configuration files.  This is a
229108322fbScarlsonj  * private interface used by Live Upgrade extensions to access zone
230108322fbScarlsonj  * configuration inside mounted alternate boot environments.
231108322fbScarlsonj  */
232108322fbScarlsonj void
233108322fbScarlsonj zonecfg_set_root(const char *rootpath)
234108322fbScarlsonj {
235108322fbScarlsonj 	if (*zonecfg_root != '\0')
236108322fbScarlsonj 		free(zonecfg_root);
237108322fbScarlsonj 	if (rootpath == NULL || rootpath[0] == '\0' || rootpath[1] == '\0' ||
238108322fbScarlsonj 	    (zonecfg_root = strdup(rootpath)) == NULL)
239108322fbScarlsonj 		zonecfg_root = "";
240108322fbScarlsonj }
241108322fbScarlsonj 
242108322fbScarlsonj const char *
243108322fbScarlsonj zonecfg_get_root(void)
244108322fbScarlsonj {
245108322fbScarlsonj 	return (zonecfg_root);
246108322fbScarlsonj }
247108322fbScarlsonj 
248108322fbScarlsonj boolean_t
249108322fbScarlsonj zonecfg_in_alt_root(void)
250108322fbScarlsonj {
251108322fbScarlsonj 	return (*zonecfg_root != '\0');
252108322fbScarlsonj }
253108322fbScarlsonj 
254108322fbScarlsonj /*
2557c478bd9Sstevel@tonic-gate  * Callers of the _file_path() functions are expected to have the second
2567c478bd9Sstevel@tonic-gate  * parameter be a (char foo[MAXPATHLEN]).
2577c478bd9Sstevel@tonic-gate  */
2587c478bd9Sstevel@tonic-gate 
259108322fbScarlsonj static boolean_t
2607c478bd9Sstevel@tonic-gate config_file_path(const char *zonename, char *answer)
2617c478bd9Sstevel@tonic-gate {
262108322fbScarlsonj 	return (snprintf(answer, MAXPATHLEN, "%s%s/%s.xml", zonecfg_root,
263108322fbScarlsonj 	    ZONE_CONFIG_ROOT, zonename) < MAXPATHLEN);
2647c478bd9Sstevel@tonic-gate }
2657c478bd9Sstevel@tonic-gate 
266108322fbScarlsonj static boolean_t
267108322fbScarlsonj snap_file_path(const char *zonename, char *answer)
2687c478bd9Sstevel@tonic-gate {
269108322fbScarlsonj 	return (snprintf(answer, MAXPATHLEN, "%s%s/%s.snapshot.xml",
270108322fbScarlsonj 	    zonecfg_root, ZONE_SNAPSHOT_ROOT, zonename) < MAXPATHLEN);
2717c478bd9Sstevel@tonic-gate }
2727c478bd9Sstevel@tonic-gate 
2737c478bd9Sstevel@tonic-gate /*ARGSUSED*/
2747c478bd9Sstevel@tonic-gate static void
2757c478bd9Sstevel@tonic-gate zonecfg_error_func(void *ctx, const char *msg, ...)
2767c478bd9Sstevel@tonic-gate {
2777c478bd9Sstevel@tonic-gate 	/*
2787c478bd9Sstevel@tonic-gate 	 * This function does nothing by design.  Its purpose is to prevent
2797c478bd9Sstevel@tonic-gate 	 * libxml from dumping unwanted messages to stdout/stderr.
2807c478bd9Sstevel@tonic-gate 	 */
2817c478bd9Sstevel@tonic-gate }
2827c478bd9Sstevel@tonic-gate 
2837c478bd9Sstevel@tonic-gate zone_dochandle_t
2847c478bd9Sstevel@tonic-gate zonecfg_init_handle(void)
2857c478bd9Sstevel@tonic-gate {
286087719fdSdp 	zone_dochandle_t handle = calloc(1, sizeof (struct zone_dochandle));
2877c478bd9Sstevel@tonic-gate 	if (handle == NULL) {
2887c478bd9Sstevel@tonic-gate 		errno = Z_NOMEM;
2897c478bd9Sstevel@tonic-gate 		return (NULL);
2907c478bd9Sstevel@tonic-gate 	}
2917c478bd9Sstevel@tonic-gate 
2927c478bd9Sstevel@tonic-gate 	/* generic libxml initialization */
2937c478bd9Sstevel@tonic-gate 	xmlLineNumbersDefault(1);
2947c478bd9Sstevel@tonic-gate 	xmlLoadExtDtdDefaultValue |= XML_DETECT_IDS;
2957c478bd9Sstevel@tonic-gate 	xmlDoValidityCheckingDefaultValue = 1;
2967c478bd9Sstevel@tonic-gate 	(void) xmlKeepBlanksDefault(0);
2977c478bd9Sstevel@tonic-gate 	xmlGetWarningsDefaultValue = 0;
2987c478bd9Sstevel@tonic-gate 	xmlSetGenericErrorFunc(NULL, zonecfg_error_func);
2997c478bd9Sstevel@tonic-gate 
3007c478bd9Sstevel@tonic-gate 	return (handle);
3017c478bd9Sstevel@tonic-gate }
3027c478bd9Sstevel@tonic-gate 
3037c478bd9Sstevel@tonic-gate int
3047c478bd9Sstevel@tonic-gate zonecfg_check_handle(zone_dochandle_t handle)
3057c478bd9Sstevel@tonic-gate {
3067c478bd9Sstevel@tonic-gate 	if (handle == NULL || handle->zone_dh_doc == NULL)
3077c478bd9Sstevel@tonic-gate 		return (Z_BAD_HANDLE);
3087c478bd9Sstevel@tonic-gate 	return (Z_OK);
3097c478bd9Sstevel@tonic-gate }
3107c478bd9Sstevel@tonic-gate 
3117c478bd9Sstevel@tonic-gate void
3127c478bd9Sstevel@tonic-gate zonecfg_fini_handle(zone_dochandle_t handle)
3137c478bd9Sstevel@tonic-gate {
3147c478bd9Sstevel@tonic-gate 	if (zonecfg_check_handle(handle) == Z_OK)
3157c478bd9Sstevel@tonic-gate 		xmlFreeDoc(handle->zone_dh_doc);
3167c478bd9Sstevel@tonic-gate 	if (handle != NULL)
3177c478bd9Sstevel@tonic-gate 		free(handle);
3187c478bd9Sstevel@tonic-gate }
3197c478bd9Sstevel@tonic-gate 
3207c478bd9Sstevel@tonic-gate static int
3217c478bd9Sstevel@tonic-gate zonecfg_destroy_impl(char *filename)
3227c478bd9Sstevel@tonic-gate {
3237c478bd9Sstevel@tonic-gate 	if (unlink(filename) == -1) {
3247c478bd9Sstevel@tonic-gate 		if (errno == EACCES)
3257c478bd9Sstevel@tonic-gate 			return (Z_ACCES);
3267c478bd9Sstevel@tonic-gate 		if (errno == ENOENT)
3277c478bd9Sstevel@tonic-gate 			return (Z_NO_ZONE);
3287c478bd9Sstevel@tonic-gate 		return (Z_MISC_FS);
3297c478bd9Sstevel@tonic-gate 	}
3307c478bd9Sstevel@tonic-gate 	return (Z_OK);
3317c478bd9Sstevel@tonic-gate }
3327c478bd9Sstevel@tonic-gate 
3337c478bd9Sstevel@tonic-gate int
334087719fdSdp zonecfg_destroy(const char *zonename, boolean_t force)
3357c478bd9Sstevel@tonic-gate {
3367c478bd9Sstevel@tonic-gate 	char path[MAXPATHLEN];
337087719fdSdp 	struct zoneent ze;
338087719fdSdp 	int err, state_err;
339087719fdSdp 	zone_state_t state;
3407c478bd9Sstevel@tonic-gate 
341108322fbScarlsonj 	if (!config_file_path(zonename, path))
342108322fbScarlsonj 		return (Z_MISC_FS);
343087719fdSdp 
344087719fdSdp 	state_err = zone_get_state((char *)zonename, &state);
345087719fdSdp 	err = access(path, W_OK);
346087719fdSdp 
347087719fdSdp 	/*
348087719fdSdp 	 * If there is no file, and no index entry, reliably indicate that no
349087719fdSdp 	 * such zone exists.
350087719fdSdp 	 */
351087719fdSdp 	if ((state_err == Z_NO_ZONE) && (err == -1) && (errno == ENOENT))
352087719fdSdp 		return (Z_NO_ZONE);
353087719fdSdp 
354087719fdSdp 	/*
355087719fdSdp 	 * Handle any other filesystem related errors (except if the XML
356087719fdSdp 	 * file is missing, which we treat silently), unless we're forcing,
357087719fdSdp 	 * in which case we plow on.
358087719fdSdp 	 */
359087719fdSdp 	if (err == -1 && errno != ENOENT) {
360087719fdSdp 		if (errno == EACCES)
361087719fdSdp 			return (Z_ACCES);
362087719fdSdp 		else if (!force)
363087719fdSdp 			return (Z_MISC_FS);
364087719fdSdp 	}
365087719fdSdp 
366087719fdSdp 	if (state > ZONE_STATE_INSTALLED)
367087719fdSdp 		return (Z_BAD_ZONE_STATE);
368087719fdSdp 
369087719fdSdp 	if (!force && state > ZONE_STATE_CONFIGURED)
370087719fdSdp 		return (Z_BAD_ZONE_STATE);
371087719fdSdp 
372087719fdSdp 	/*
373087719fdSdp 	 * Index deletion succeeds even if the entry doesn't exist.  So this
374087719fdSdp 	 * will fail only if we've had some more severe problem.
375087719fdSdp 	 */
376087719fdSdp 	bzero(&ze, sizeof (ze));
377087719fdSdp 	(void) strlcpy(ze.zone_name, zonename, sizeof (ze.zone_name));
378087719fdSdp 	if ((err = putzoneent(&ze, PZE_REMOVE)) != Z_OK)
379087719fdSdp 		if (!force)
380087719fdSdp 			return (err);
381087719fdSdp 
382087719fdSdp 	err = zonecfg_destroy_impl(path);
383087719fdSdp 
384087719fdSdp 	/*
385087719fdSdp 	 * Treat failure to find the XML file silently, since, well, it's
386087719fdSdp 	 * gone, and with the index file cleaned up, we're done.
387087719fdSdp 	 */
388087719fdSdp 	if (err == Z_OK || err == Z_NO_ZONE)
389087719fdSdp 		return (Z_OK);
390087719fdSdp 	return (err);
3917c478bd9Sstevel@tonic-gate }
3927c478bd9Sstevel@tonic-gate 
3937c478bd9Sstevel@tonic-gate int
394108322fbScarlsonj zonecfg_destroy_snapshot(const char *zonename)
3957c478bd9Sstevel@tonic-gate {
3967c478bd9Sstevel@tonic-gate 	char path[MAXPATHLEN];
3977c478bd9Sstevel@tonic-gate 
398108322fbScarlsonj 	if (!snap_file_path(zonename, path))
399108322fbScarlsonj 		return (Z_MISC_FS);
4007c478bd9Sstevel@tonic-gate 	return (zonecfg_destroy_impl(path));
4017c478bd9Sstevel@tonic-gate }
4027c478bd9Sstevel@tonic-gate 
4037c478bd9Sstevel@tonic-gate static int
404a1be23daSdp getroot(zone_dochandle_t handle, xmlNodePtr *root)
4057c478bd9Sstevel@tonic-gate {
4067c478bd9Sstevel@tonic-gate 	if (zonecfg_check_handle(handle) == Z_BAD_HANDLE)
4077c478bd9Sstevel@tonic-gate 		return (Z_BAD_HANDLE);
4087c478bd9Sstevel@tonic-gate 
409a1be23daSdp 	*root = xmlDocGetRootElement(handle->zone_dh_doc);
410a1be23daSdp 
411a1be23daSdp 	if (*root == NULL)
4127c478bd9Sstevel@tonic-gate 		return (Z_EMPTY_DOCUMENT);
413a1be23daSdp 
414a1be23daSdp 	if (xmlStrcmp((*root)->name, DTD_ELEM_ZONE))
415a1be23daSdp 		return (Z_WRONG_DOC_TYPE);
416a1be23daSdp 
417a1be23daSdp 	return (Z_OK);
4187c478bd9Sstevel@tonic-gate }
4197c478bd9Sstevel@tonic-gate 
420a1be23daSdp static int
421a1be23daSdp operation_prep(zone_dochandle_t handle)
422a1be23daSdp {
423a1be23daSdp 	xmlNodePtr root;
424a1be23daSdp 	int err;
425a1be23daSdp 
426a1be23daSdp 	if ((err = getroot(handle, &root)) != 0)
427a1be23daSdp 		return (err);
428a1be23daSdp 
429a1be23daSdp 	handle->zone_dh_cur = root;
430a1be23daSdp 	handle->zone_dh_top = root;
431a1be23daSdp 	return (Z_OK);
4327c478bd9Sstevel@tonic-gate }
433a1be23daSdp 
434a1be23daSdp static int
435ffbafc53Scomay fetchprop(xmlNodePtr cur, const xmlChar *propname, char *dst, size_t dstsize)
436ffbafc53Scomay {
437ffbafc53Scomay 	xmlChar *property;
438ffbafc53Scomay 	size_t srcsize;
439ffbafc53Scomay 
440ffbafc53Scomay 	if ((property = xmlGetProp(cur, propname)) == NULL)
441ffbafc53Scomay 		return (Z_BAD_PROPERTY);
442ffbafc53Scomay 	srcsize = strlcpy(dst, (char *)property, dstsize);
443ffbafc53Scomay 	xmlFree(property);
444ffbafc53Scomay 	if (srcsize >= dstsize)
445ffbafc53Scomay 		return (Z_TOO_BIG);
446ffbafc53Scomay 	return (Z_OK);
447ffbafc53Scomay }
448ffbafc53Scomay 
449ffbafc53Scomay static int
450ffbafc53Scomay fetch_alloc_prop(xmlNodePtr cur, const xmlChar *propname, char **dst)
451ffbafc53Scomay {
452ffbafc53Scomay 	xmlChar *property;
453ffbafc53Scomay 
454ffbafc53Scomay 	if ((property = xmlGetProp(cur, propname)) == NULL)
455ffbafc53Scomay 		return (Z_BAD_PROPERTY);
456ffbafc53Scomay 	if ((*dst = strdup((char *)property)) == NULL) {
457ffbafc53Scomay 		xmlFree(property);
458ffbafc53Scomay 		return (Z_NOMEM);
459ffbafc53Scomay 	}
460ffbafc53Scomay 	xmlFree(property);
461ffbafc53Scomay 	return (Z_OK);
462ffbafc53Scomay }
463ffbafc53Scomay 
464ffbafc53Scomay static int
465a1be23daSdp getrootattr(zone_dochandle_t handle, const xmlChar *propname,
466a1be23daSdp     char *propval, size_t propsize)
467a1be23daSdp {
468a1be23daSdp 	xmlNodePtr root;
469a1be23daSdp 	int err;
470a1be23daSdp 
471a1be23daSdp 	if ((err = getroot(handle, &root)) != 0)
472a1be23daSdp 		return (err);
473a1be23daSdp 
474ffbafc53Scomay 	return (fetchprop(root, propname, propval, propsize));
475ffbafc53Scomay }
476ffbafc53Scomay 
477ffbafc53Scomay static int
478ffbafc53Scomay get_alloc_rootattr(zone_dochandle_t handle, const xmlChar *propname,
479ffbafc53Scomay     char **propval)
480ffbafc53Scomay {
481ffbafc53Scomay 	xmlNodePtr root;
482ffbafc53Scomay 	int err;
483ffbafc53Scomay 
484ffbafc53Scomay 	if ((err = getroot(handle, &root)) != 0)
485ffbafc53Scomay 		return (err);
486ffbafc53Scomay 
487ffbafc53Scomay 	return (fetch_alloc_prop(root, propname, propval));
488a1be23daSdp }
489a1be23daSdp 
490a1be23daSdp static int
491108322fbScarlsonj setrootattr(zone_dochandle_t handle, const xmlChar *propname,
492108322fbScarlsonj     const char *propval)
493a1be23daSdp {
494a1be23daSdp 	int err;
495a1be23daSdp 	xmlNodePtr root;
496a1be23daSdp 
497a1be23daSdp 	if ((err = getroot(handle, &root)) != Z_OK)
498a1be23daSdp 		return (err);
499a1be23daSdp 
5000209230bSgjelinek 	/*
5010209230bSgjelinek 	 * If we get a null propval remove the property (ignore return since it
5020209230bSgjelinek 	 * may not be set to begin with).
5030209230bSgjelinek 	 */
5040209230bSgjelinek 	if (propval == NULL) {
5050209230bSgjelinek 		(void) xmlUnsetProp(root, propname);
5060209230bSgjelinek 	} else {
5070209230bSgjelinek 		if (xmlSetProp(root, propname, (const xmlChar *) propval)
5080209230bSgjelinek 		    == NULL)
509a1be23daSdp 			return (Z_INVAL);
5100209230bSgjelinek 	}
5117c478bd9Sstevel@tonic-gate 	return (Z_OK);
5127c478bd9Sstevel@tonic-gate }
5137c478bd9Sstevel@tonic-gate 
514087719fdSdp static void
515087719fdSdp addcomment(zone_dochandle_t handle, const char *comment)
516087719fdSdp {
517087719fdSdp 	xmlNodePtr node;
518087719fdSdp 	node = xmlNewComment((xmlChar *) comment);
519087719fdSdp 
520087719fdSdp 	if (node != NULL)
521087719fdSdp 		(void) xmlAddPrevSibling(handle->zone_dh_top, node);
522087719fdSdp }
523087719fdSdp 
524087719fdSdp static void
525087719fdSdp stripcomments(zone_dochandle_t handle)
526087719fdSdp {
527087719fdSdp 	xmlDocPtr top;
528087719fdSdp 	xmlNodePtr child, next;
529087719fdSdp 
530087719fdSdp 	top = handle->zone_dh_doc;
531087719fdSdp 	for (child = top->xmlChildrenNode; child != NULL; child = next) {
532087719fdSdp 		next = child->next;
533087719fdSdp 		if (child->name == NULL)
534087719fdSdp 			continue;
535087719fdSdp 		if (xmlStrcmp(child->name, DTD_ELEM_COMMENT) == 0) {
536087719fdSdp 			next = child->next;
537087719fdSdp 			xmlUnlinkNode(child);
538087719fdSdp 			xmlFreeNode(child);
539087719fdSdp 		}
540087719fdSdp 	}
541087719fdSdp }
542087719fdSdp 
543ee519a1fSgjelinek static void
544ee519a1fSgjelinek strip_sw_inv(zone_dochandle_t handle)
545ee519a1fSgjelinek {
546ee519a1fSgjelinek 	xmlNodePtr root, child, next;
547ee519a1fSgjelinek 
548ee519a1fSgjelinek 	root = xmlDocGetRootElement(handle->zone_dh_doc);
549ee519a1fSgjelinek 	for (child = root->xmlChildrenNode; child != NULL; child = next) {
550ee519a1fSgjelinek 		next = child->next;
551ee519a1fSgjelinek 		if (child->name == NULL)
552ee519a1fSgjelinek 			continue;
553ee519a1fSgjelinek 		if (xmlStrcmp(child->name, DTD_ELEM_PACKAGE) == 0 ||
554ee519a1fSgjelinek 		    xmlStrcmp(child->name, DTD_ELEM_PATCH) == 0) {
555ee519a1fSgjelinek 			next = child->next;
556ee519a1fSgjelinek 			xmlUnlinkNode(child);
557ee519a1fSgjelinek 			xmlFreeNode(child);
558ee519a1fSgjelinek 		}
559ee519a1fSgjelinek 	}
560ee519a1fSgjelinek }
561ee519a1fSgjelinek 
5627c478bd9Sstevel@tonic-gate static int
563108322fbScarlsonj zonecfg_get_handle_impl(const char *zonename, const char *filename,
564108322fbScarlsonj     zone_dochandle_t handle)
5657c478bd9Sstevel@tonic-gate {
5667c478bd9Sstevel@tonic-gate 	xmlValidCtxtPtr cvp;
5677c478bd9Sstevel@tonic-gate 	struct stat statbuf;
5687c478bd9Sstevel@tonic-gate 	int valid;
5697c478bd9Sstevel@tonic-gate 
5707c478bd9Sstevel@tonic-gate 	if (zonename == NULL)
5717c478bd9Sstevel@tonic-gate 		return (Z_NO_ZONE);
5729acbbeafSnn35248 
5737c478bd9Sstevel@tonic-gate 	if ((handle->zone_dh_doc = xmlParseFile(filename)) == NULL) {
5747c478bd9Sstevel@tonic-gate 		/* distinguish file not found vs. found but not parsed */
5757c478bd9Sstevel@tonic-gate 		if (stat(filename, &statbuf) == 0)
5767c478bd9Sstevel@tonic-gate 			return (Z_INVALID_DOCUMENT);
5777c478bd9Sstevel@tonic-gate 		return (Z_NO_ZONE);
5787c478bd9Sstevel@tonic-gate 	}
5797c478bd9Sstevel@tonic-gate 	if ((cvp = xmlNewValidCtxt()) == NULL)
5807c478bd9Sstevel@tonic-gate 		return (Z_NOMEM);
5817c478bd9Sstevel@tonic-gate 	cvp->error = zonecfg_error_func;
5827c478bd9Sstevel@tonic-gate 	cvp->warning = zonecfg_error_func;
5837c478bd9Sstevel@tonic-gate 	valid = xmlValidateDocument(cvp, handle->zone_dh_doc);
5847c478bd9Sstevel@tonic-gate 	xmlFreeValidCtxt(cvp);
5857c478bd9Sstevel@tonic-gate 	if (valid == 0)
5867c478bd9Sstevel@tonic-gate 		return (Z_INVALID_DOCUMENT);
587087719fdSdp 
5887c478bd9Sstevel@tonic-gate 	/* delete any comments such as inherited Sun copyright / ident str */
589087719fdSdp 	stripcomments(handle);
5907c478bd9Sstevel@tonic-gate 	return (Z_OK);
5917c478bd9Sstevel@tonic-gate }
5927c478bd9Sstevel@tonic-gate 
5937c478bd9Sstevel@tonic-gate int
594108322fbScarlsonj zonecfg_get_handle(const char *zonename, zone_dochandle_t handle)
5957c478bd9Sstevel@tonic-gate {
5967c478bd9Sstevel@tonic-gate 	char path[MAXPATHLEN];
5977c478bd9Sstevel@tonic-gate 
598108322fbScarlsonj 	if (!config_file_path(zonename, path))
599108322fbScarlsonj 		return (Z_MISC_FS);
600087719fdSdp 	handle->zone_dh_newzone = B_FALSE;
601087719fdSdp 
6027c478bd9Sstevel@tonic-gate 	return (zonecfg_get_handle_impl(zonename, path, handle));
6037c478bd9Sstevel@tonic-gate }
6047c478bd9Sstevel@tonic-gate 
6057c478bd9Sstevel@tonic-gate int
60616ab8c7bSgjelinek zonecfg_get_attach_handle(const char *path, const char *fname,
60716ab8c7bSgjelinek     const char *zonename, boolean_t preserve_sw, zone_dochandle_t handle)
608ee519a1fSgjelinek {
609ee519a1fSgjelinek 	char		migpath[MAXPATHLEN];
610ee519a1fSgjelinek 	int		err;
611ee519a1fSgjelinek 	struct stat	buf;
612ee519a1fSgjelinek 
613ee519a1fSgjelinek 	if (snprintf(migpath, sizeof (migpath), "%s/root", path) >=
614ee519a1fSgjelinek 	    sizeof (migpath))
615ee519a1fSgjelinek 		return (Z_NOMEM);
616ee519a1fSgjelinek 
617ee519a1fSgjelinek 	if (stat(migpath, &buf) == -1 || !S_ISDIR(buf.st_mode))
618ee519a1fSgjelinek 		return (Z_NO_ZONE);
619ee519a1fSgjelinek 
62016ab8c7bSgjelinek 	if (snprintf(migpath, sizeof (migpath), "%s/%s", path, fname) >=
621ee519a1fSgjelinek 	    sizeof (migpath))
622ee519a1fSgjelinek 		return (Z_NOMEM);
623ee519a1fSgjelinek 
624ee519a1fSgjelinek 	if ((err = zonecfg_get_handle_impl(zonename, migpath, handle)) != Z_OK)
625ee519a1fSgjelinek 		return (err);
626ee519a1fSgjelinek 
627ee519a1fSgjelinek 	if (!preserve_sw)
628ee519a1fSgjelinek 		strip_sw_inv(handle);
629ee519a1fSgjelinek 
630ee519a1fSgjelinek 	handle->zone_dh_newzone = B_TRUE;
631ee519a1fSgjelinek 	if ((err = setrootattr(handle, DTD_ATTR_ZONEPATH, path)) != Z_OK)
632ee519a1fSgjelinek 		return (err);
633ee519a1fSgjelinek 
634ee519a1fSgjelinek 	return (setrootattr(handle, DTD_ATTR_NAME, zonename));
635ee519a1fSgjelinek }
636ee519a1fSgjelinek 
637ee519a1fSgjelinek int
638108322fbScarlsonj zonecfg_get_snapshot_handle(const char *zonename, zone_dochandle_t handle)
6397c478bd9Sstevel@tonic-gate {
6407c478bd9Sstevel@tonic-gate 	char path[MAXPATHLEN];
6417c478bd9Sstevel@tonic-gate 
642108322fbScarlsonj 	if (!snap_file_path(zonename, path))
643108322fbScarlsonj 		return (Z_MISC_FS);
644087719fdSdp 	handle->zone_dh_newzone = B_FALSE;
6457c478bd9Sstevel@tonic-gate 	return (zonecfg_get_handle_impl(zonename, path, handle));
6467c478bd9Sstevel@tonic-gate }
6477c478bd9Sstevel@tonic-gate 
6487c478bd9Sstevel@tonic-gate int
649108322fbScarlsonj zonecfg_get_template_handle(const char *template, const char *zonename,
650087719fdSdp     zone_dochandle_t handle)
651087719fdSdp {
652087719fdSdp 	char path[MAXPATHLEN];
653087719fdSdp 	int err;
654087719fdSdp 
655108322fbScarlsonj 	if (!config_file_path(template, path))
656108322fbScarlsonj 		return (Z_MISC_FS);
657087719fdSdp 
658087719fdSdp 	if ((err = zonecfg_get_handle_impl(template, path, handle)) != Z_OK)
659087719fdSdp 		return (err);
660087719fdSdp 	handle->zone_dh_newzone = B_TRUE;
661087719fdSdp 	return (setrootattr(handle, DTD_ATTR_NAME, zonename));
662087719fdSdp }
663087719fdSdp 
6649acbbeafSnn35248 int
6659acbbeafSnn35248 zonecfg_get_xml_handle(const char *path, zone_dochandle_t handle)
6669acbbeafSnn35248 {
6679acbbeafSnn35248 	struct stat buf;
6689acbbeafSnn35248 	int err;
6699acbbeafSnn35248 
6709acbbeafSnn35248 	if (stat(path, &buf) == -1)
6719acbbeafSnn35248 		return (Z_MISC_FS);
6729acbbeafSnn35248 
6739acbbeafSnn35248 	if ((err = zonecfg_get_handle_impl("xml", path, handle)) != Z_OK)
6749acbbeafSnn35248 		return (err);
6759acbbeafSnn35248 	handle->zone_dh_newzone = B_TRUE;
6769acbbeafSnn35248 	return (Z_OK);
6779acbbeafSnn35248 }
6789acbbeafSnn35248 
6798cd327d5Sgjelinek /*
6808cd327d5Sgjelinek  * Initialize two handles from the manifest read on fd.  The rem_handle
6818cd327d5Sgjelinek  * is initialized from the input file, including the sw inventory.  The
6828cd327d5Sgjelinek  * local_handle is initialized with the same zone configuration but with
6838cd327d5Sgjelinek  * no sw inventory.
6848cd327d5Sgjelinek  */
6858cd327d5Sgjelinek int
6868cd327d5Sgjelinek zonecfg_attach_manifest(int fd, zone_dochandle_t local_handle,
6878cd327d5Sgjelinek     zone_dochandle_t rem_handle)
6888cd327d5Sgjelinek {
6898cd327d5Sgjelinek 	xmlValidCtxtPtr cvp;
6908cd327d5Sgjelinek 	int valid;
6918cd327d5Sgjelinek 
6928cd327d5Sgjelinek 	/* load the manifest into the handle for the remote system */
6938cd327d5Sgjelinek 	if ((rem_handle->zone_dh_doc = xmlReadFd(fd, NULL, NULL, 0)) == NULL) {
6948cd327d5Sgjelinek 		return (Z_INVALID_DOCUMENT);
6958cd327d5Sgjelinek 	}
6968cd327d5Sgjelinek 	if ((cvp = xmlNewValidCtxt()) == NULL)
6978cd327d5Sgjelinek 		return (Z_NOMEM);
6988cd327d5Sgjelinek 	cvp->error = zonecfg_error_func;
6998cd327d5Sgjelinek 	cvp->warning = zonecfg_error_func;
7008cd327d5Sgjelinek 	valid = xmlValidateDocument(cvp, rem_handle->zone_dh_doc);
7018cd327d5Sgjelinek 	xmlFreeValidCtxt(cvp);
7028cd327d5Sgjelinek 	if (valid == 0)
7038cd327d5Sgjelinek 		return (Z_INVALID_DOCUMENT);
7048cd327d5Sgjelinek 
7058cd327d5Sgjelinek 	/* delete any comments such as inherited Sun copyright / ident str */
7068cd327d5Sgjelinek 	stripcomments(rem_handle);
7078cd327d5Sgjelinek 
7088cd327d5Sgjelinek 	rem_handle->zone_dh_newzone = B_TRUE;
7098cd327d5Sgjelinek 	rem_handle->zone_dh_sw_inv = B_TRUE;
7108cd327d5Sgjelinek 
7118cd327d5Sgjelinek 	/*
7128cd327d5Sgjelinek 	 * Now use the remote system handle to generate a local system handle
7138cd327d5Sgjelinek 	 * with an identical zones configuration but no sw inventory.
7148cd327d5Sgjelinek 	 */
7158cd327d5Sgjelinek 	if ((local_handle->zone_dh_doc = xmlCopyDoc(rem_handle->zone_dh_doc,
7168cd327d5Sgjelinek 	    1)) == NULL) {
7178cd327d5Sgjelinek 		return (Z_INVALID_DOCUMENT);
7188cd327d5Sgjelinek 	}
7198cd327d5Sgjelinek 
7208cd327d5Sgjelinek 	/*
7218cd327d5Sgjelinek 	 * We need to re-run xmlValidateDocument on local_handle to properly
7228cd327d5Sgjelinek 	 * update the in-core representation of the configuration.
7238cd327d5Sgjelinek 	 */
7248cd327d5Sgjelinek 	if ((cvp = xmlNewValidCtxt()) == NULL)
7258cd327d5Sgjelinek 		return (Z_NOMEM);
7268cd327d5Sgjelinek 	cvp->error = zonecfg_error_func;
7278cd327d5Sgjelinek 	cvp->warning = zonecfg_error_func;
7288cd327d5Sgjelinek 	valid = xmlValidateDocument(cvp, local_handle->zone_dh_doc);
7298cd327d5Sgjelinek 	xmlFreeValidCtxt(cvp);
7308cd327d5Sgjelinek 	if (valid == 0)
7318cd327d5Sgjelinek 		return (Z_INVALID_DOCUMENT);
7328cd327d5Sgjelinek 
7338cd327d5Sgjelinek 	strip_sw_inv(local_handle);
7348cd327d5Sgjelinek 
7358cd327d5Sgjelinek 	local_handle->zone_dh_newzone = B_TRUE;
7368cd327d5Sgjelinek 	local_handle->zone_dh_sw_inv = B_FALSE;
7378cd327d5Sgjelinek 
7388cd327d5Sgjelinek 	return (Z_OK);
7398cd327d5Sgjelinek }
7408cd327d5Sgjelinek 
741087719fdSdp static boolean_t
742087719fdSdp is_renaming(zone_dochandle_t handle)
743087719fdSdp {
744087719fdSdp 	if (handle->zone_dh_newzone)
745087719fdSdp 		return (B_FALSE);
746087719fdSdp 	if (strlen(handle->zone_dh_delete_name) > 0)
747087719fdSdp 		return (B_TRUE);
748087719fdSdp 	return (B_FALSE);
749087719fdSdp }
750087719fdSdp 
751087719fdSdp static boolean_t
752087719fdSdp is_new(zone_dochandle_t handle)
753087719fdSdp {
754087719fdSdp 	return (handle->zone_dh_newzone || handle->zone_dh_snapshot);
755087719fdSdp }
756087719fdSdp 
757087719fdSdp static boolean_t
758087719fdSdp is_snapshot(zone_dochandle_t handle)
759087719fdSdp {
760087719fdSdp 	return (handle->zone_dh_snapshot);
761087719fdSdp }
762087719fdSdp 
763087719fdSdp /*
764087719fdSdp  * It would be great to be able to use libc's ctype(3c) macros, but we
765087719fdSdp  * can't, as they are locale sensitive, and it would break our limited thread
766087719fdSdp  * safety if this routine had to change the app locale on the fly.
767087719fdSdp  */
768087719fdSdp int
769108322fbScarlsonj zonecfg_validate_zonename(const char *zone)
770087719fdSdp {
771087719fdSdp 	int i;
772087719fdSdp 
773087719fdSdp 	if (strcmp(zone, GLOBAL_ZONENAME) == 0)
774087719fdSdp 		return (Z_BOGUS_ZONE_NAME);
775087719fdSdp 
776087719fdSdp 	if (strlen(zone) >= ZONENAME_MAX)
777087719fdSdp 		return (Z_BOGUS_ZONE_NAME);
778087719fdSdp 
779087719fdSdp 	if (!((zone[0] >= 'a' && zone[0] <= 'z') ||
780087719fdSdp 	    (zone[0] >= 'A' && zone[0] <= 'Z') ||
781087719fdSdp 	    (zone[0] >= '0' && zone[0] <= '9')))
782087719fdSdp 		return (Z_BOGUS_ZONE_NAME);
783087719fdSdp 
784087719fdSdp 	for (i = 1; zone[i] != '\0'; i++) {
785087719fdSdp 		if (!((zone[i] >= 'a' && zone[i] <= 'z') ||
786087719fdSdp 		    (zone[i] >= 'A' && zone[i] <= 'Z') ||
787087719fdSdp 		    (zone[i] >= '0' && zone[i] <= '9') ||
788087719fdSdp 		    (zone[i] == '-') || (zone[i] == '_') || (zone[i] == '.')))
789087719fdSdp 			return (Z_BOGUS_ZONE_NAME);
790087719fdSdp 	}
791087719fdSdp 
792087719fdSdp 	return (Z_OK);
793087719fdSdp }
794087719fdSdp 
795087719fdSdp /*
796087719fdSdp  * Changing the zone name requires us to track both the old and new
797087719fdSdp  * name of the zone until commit time.
798087719fdSdp  */
799087719fdSdp int
8007c478bd9Sstevel@tonic-gate zonecfg_get_name(zone_dochandle_t handle, char *name, size_t namesize)
8017c478bd9Sstevel@tonic-gate {
802a1be23daSdp 	return (getrootattr(handle, DTD_ATTR_NAME, name, namesize));
8037c478bd9Sstevel@tonic-gate }
8047c478bd9Sstevel@tonic-gate 
805a1be23daSdp int
806a1be23daSdp zonecfg_set_name(zone_dochandle_t handle, char *name)
807a1be23daSdp {
808087719fdSdp 	zone_state_t state;
809087719fdSdp 	char curname[ZONENAME_MAX], old_delname[ZONENAME_MAX];
810087719fdSdp 	int err;
811087719fdSdp 
812087719fdSdp 	if ((err = getrootattr(handle, DTD_ATTR_NAME, curname,
813087719fdSdp 	    sizeof (curname))) != Z_OK)
814087719fdSdp 		return (err);
815087719fdSdp 
816087719fdSdp 	if (strcmp(name, curname) == 0)
817087719fdSdp 		return (Z_OK);
818087719fdSdp 
819087719fdSdp 	/*
820087719fdSdp 	 * Switching zone names to one beginning with SUNW is not permitted.
821087719fdSdp 	 */
822087719fdSdp 	if (strncmp(name, "SUNW", 4) == 0)
823087719fdSdp 		return (Z_BOGUS_ZONE_NAME);
824087719fdSdp 
825087719fdSdp 	if ((err = zonecfg_validate_zonename(name)) != Z_OK)
826087719fdSdp 		return (err);
827087719fdSdp 
828087719fdSdp 	/*
829087719fdSdp 	 * Setting the name back to the original name (effectively a revert of
830087719fdSdp 	 * the name) is fine.  But if we carry on, we'll falsely identify the
831087719fdSdp 	 * name as "in use," so special case here.
832087719fdSdp 	 */
833087719fdSdp 	if (strcmp(name, handle->zone_dh_delete_name) == 0) {
834087719fdSdp 		err = setrootattr(handle, DTD_ATTR_NAME, name);
835087719fdSdp 		handle->zone_dh_delete_name[0] = '\0';
836087719fdSdp 		return (err);
837087719fdSdp 	}
838087719fdSdp 
839087719fdSdp 	/* Check to see if new name chosen is already in use */
840087719fdSdp 	if (zone_get_state(name, &state) != Z_NO_ZONE)
841087719fdSdp 		return (Z_NAME_IN_USE);
842087719fdSdp 
843087719fdSdp 	/*
844087719fdSdp 	 * If this isn't already "new" or in a renaming transition, then
845087719fdSdp 	 * we're initiating a rename here; so stash the "delete name"
846087719fdSdp 	 * (i.e. the name of the zone we'll be removing) for the rename.
847087719fdSdp 	 */
848087719fdSdp 	(void) strlcpy(old_delname, handle->zone_dh_delete_name,
849087719fdSdp 	    sizeof (old_delname));
850087719fdSdp 	if (!is_new(handle) && !is_renaming(handle)) {
851087719fdSdp 		/*
852087719fdSdp 		 * Name change is allowed only when the zone we're altering
853087719fdSdp 		 * is not ready or running.
854087719fdSdp 		 */
855087719fdSdp 		err = zone_get_state(curname, &state);
856087719fdSdp 		if (err == Z_OK) {
857087719fdSdp 			if (state > ZONE_STATE_INSTALLED)
858087719fdSdp 				return (Z_BAD_ZONE_STATE);
859087719fdSdp 		} else if (err != Z_NO_ZONE) {
860087719fdSdp 			return (err);
861087719fdSdp 		}
862087719fdSdp 
863087719fdSdp 		(void) strlcpy(handle->zone_dh_delete_name, curname,
864087719fdSdp 		    sizeof (handle->zone_dh_delete_name));
865087719fdSdp 		assert(is_renaming(handle));
866087719fdSdp 	} else if (is_renaming(handle)) {
867087719fdSdp 		err = zone_get_state(handle->zone_dh_delete_name, &state);
868087719fdSdp 		if (err == Z_OK) {
869087719fdSdp 			if (state > ZONE_STATE_INSTALLED)
870087719fdSdp 				return (Z_BAD_ZONE_STATE);
871087719fdSdp 		} else if (err != Z_NO_ZONE) {
872087719fdSdp 			return (err);
873087719fdSdp 		}
874087719fdSdp 	}
875087719fdSdp 
876087719fdSdp 	if ((err = setrootattr(handle, DTD_ATTR_NAME, name)) != Z_OK) {
877087719fdSdp 		/*
878087719fdSdp 		 * Restore the deletename to whatever it was at the
879087719fdSdp 		 * top of the routine, since we've had a failure.
880087719fdSdp 		 */
881087719fdSdp 		(void) strlcpy(handle->zone_dh_delete_name, old_delname,
882087719fdSdp 		    sizeof (handle->zone_dh_delete_name));
883087719fdSdp 		return (err);
884087719fdSdp 	}
885087719fdSdp 
886087719fdSdp 	return (Z_OK);
8877c478bd9Sstevel@tonic-gate }
888a1be23daSdp 
889a1be23daSdp int
890a1be23daSdp zonecfg_get_zonepath(zone_dochandle_t handle, char *path, size_t pathsize)
891a1be23daSdp {
892108322fbScarlsonj 	size_t len;
893108322fbScarlsonj 
894108322fbScarlsonj 	if ((len = strlcpy(path, zonecfg_root, pathsize)) >= pathsize)
895108322fbScarlsonj 		return (Z_TOO_BIG);
896108322fbScarlsonj 	return (getrootattr(handle, DTD_ATTR_ZONEPATH, path + len,
897108322fbScarlsonj 	    pathsize - len));
898a1be23daSdp }
899a1be23daSdp 
900a1be23daSdp int
901a1be23daSdp zonecfg_set_zonepath(zone_dochandle_t handle, char *zonepath)
902a1be23daSdp {
903555afedfScarlsonj 	size_t len;
904555afedfScarlsonj 
905555afedfScarlsonj 	/*
906555afedfScarlsonj 	 * The user deals in absolute paths in the running global zone, but the
907555afedfScarlsonj 	 * internal configuration files deal with boot environment relative
908555afedfScarlsonj 	 * paths.  Strip out the alternate root when specified.
909555afedfScarlsonj 	 */
910555afedfScarlsonj 	len = strlen(zonecfg_root);
911555afedfScarlsonj 	if (strncmp(zonepath, zonecfg_root, len) != 0 || zonepath[len] != '/')
912555afedfScarlsonj 		return (Z_BAD_PROPERTY);
913555afedfScarlsonj 	zonepath += len;
914a1be23daSdp 	return (setrootattr(handle, DTD_ATTR_ZONEPATH, zonepath));
915a1be23daSdp }
916a1be23daSdp 
917a1be23daSdp int
9189acbbeafSnn35248 zonecfg_get_brand(zone_dochandle_t handle, char *brand, size_t brandsize)
9199acbbeafSnn35248 {
9209acbbeafSnn35248 	int ret, sz;
9219acbbeafSnn35248 
9229acbbeafSnn35248 	ret = getrootattr(handle, DTD_ATTR_BRAND, brand, brandsize);
9239acbbeafSnn35248 
9249acbbeafSnn35248 	/* If the zone has no brand, it is native. */
9259acbbeafSnn35248 	if (ret == Z_OK && brand[0] == '\0') {
9269acbbeafSnn35248 		sz = strlcpy(brand, NATIVE_BRAND_NAME, brandsize);
9279acbbeafSnn35248 		if (sz >= brandsize)
9289acbbeafSnn35248 			ret = Z_TOO_BIG;
9299acbbeafSnn35248 		else
9309acbbeafSnn35248 			ret = Z_OK;
9319acbbeafSnn35248 	}
9329acbbeafSnn35248 
9339acbbeafSnn35248 	return (ret);
9349acbbeafSnn35248 }
9359acbbeafSnn35248 
9369acbbeafSnn35248 int
9379acbbeafSnn35248 zonecfg_set_brand(zone_dochandle_t handle, char *brand)
9389acbbeafSnn35248 {
9399acbbeafSnn35248 	return (setrootattr(handle, DTD_ATTR_BRAND, brand));
9409acbbeafSnn35248 }
9419acbbeafSnn35248 
9429acbbeafSnn35248 int
943a1be23daSdp zonecfg_get_autoboot(zone_dochandle_t handle, boolean_t *autoboot)
944a1be23daSdp {
945a1be23daSdp 	char autobootstr[DTD_ENTITY_BOOL_LEN];
946a1be23daSdp 	int ret;
947a1be23daSdp 
948a1be23daSdp 	if ((ret = getrootattr(handle, DTD_ATTR_AUTOBOOT, autobootstr,
949a1be23daSdp 	    sizeof (autobootstr))) != Z_OK)
950a1be23daSdp 		return (ret);
951a1be23daSdp 
952a1be23daSdp 	if (strcmp(autobootstr, DTD_ENTITY_TRUE) == 0)
953a1be23daSdp 		*autoboot = B_TRUE;
954a1be23daSdp 	else if (strcmp(autobootstr, DTD_ENTITY_FALSE) == 0)
955a1be23daSdp 		*autoboot = B_FALSE;
956a1be23daSdp 	else
957a1be23daSdp 		ret = Z_BAD_PROPERTY;
958a1be23daSdp 	return (ret);
959a1be23daSdp }
960a1be23daSdp 
961a1be23daSdp int
962a1be23daSdp zonecfg_set_autoboot(zone_dochandle_t handle, boolean_t autoboot)
963a1be23daSdp {
964a1be23daSdp 	return (setrootattr(handle, DTD_ATTR_AUTOBOOT,
965a1be23daSdp 	    autoboot ? DTD_ENTITY_TRUE : DTD_ENTITY_FALSE));
966a1be23daSdp }
967a1be23daSdp 
968a1be23daSdp int
969a1be23daSdp zonecfg_get_pool(zone_dochandle_t handle, char *pool, size_t poolsize)
970a1be23daSdp {
971a1be23daSdp 	return (getrootattr(handle, DTD_ATTR_POOL, pool, poolsize));
972a1be23daSdp }
973a1be23daSdp 
974a1be23daSdp int
975a1be23daSdp zonecfg_set_pool(zone_dochandle_t handle, char *pool)
976a1be23daSdp {
977a1be23daSdp 	return (setrootattr(handle, DTD_ATTR_POOL, pool));
978a1be23daSdp }
979a1be23daSdp 
980ffbafc53Scomay int
981ffbafc53Scomay zonecfg_get_limitpriv(zone_dochandle_t handle, char **limitpriv)
982ffbafc53Scomay {
983ffbafc53Scomay 	return (get_alloc_rootattr(handle, DTD_ATTR_LIMITPRIV, limitpriv));
984ffbafc53Scomay }
985ffbafc53Scomay 
986ffbafc53Scomay int
9873f2f09c1Sdp zonecfg_set_limitpriv(zone_dochandle_t handle, char *limitpriv)
988ffbafc53Scomay {
9893f2f09c1Sdp 	return (setrootattr(handle, DTD_ATTR_LIMITPRIV, limitpriv));
9903f2f09c1Sdp }
9913f2f09c1Sdp 
9923f2f09c1Sdp int
9933f2f09c1Sdp zonecfg_get_bootargs(zone_dochandle_t handle, char *bargs, size_t bargssize)
9943f2f09c1Sdp {
9953f2f09c1Sdp 	return (getrootattr(handle, DTD_ATTR_BOOTARGS, bargs, bargssize));
9963f2f09c1Sdp }
9973f2f09c1Sdp 
9983f2f09c1Sdp int
9993f2f09c1Sdp zonecfg_set_bootargs(zone_dochandle_t handle, char *bargs)
10003f2f09c1Sdp {
10013f2f09c1Sdp 	return (setrootattr(handle, DTD_ATTR_BOOTARGS, bargs));
1002ffbafc53Scomay }
1003ffbafc53Scomay 
10040209230bSgjelinek int
10050209230bSgjelinek zonecfg_get_sched_class(zone_dochandle_t handle, char *sched, size_t schedsize)
10060209230bSgjelinek {
10070209230bSgjelinek 	return (getrootattr(handle, DTD_ATTR_SCHED, sched, schedsize));
10080209230bSgjelinek }
10090209230bSgjelinek 
10100209230bSgjelinek int
10110209230bSgjelinek zonecfg_set_sched(zone_dochandle_t handle, char *sched)
10120209230bSgjelinek {
10130209230bSgjelinek 	return (setrootattr(handle, DTD_ATTR_SCHED, sched));
10140209230bSgjelinek }
10150209230bSgjelinek 
1016a1be23daSdp /*
1017a1be23daSdp  * /etc/zones/index caches a vital piece of information which is also
1018a1be23daSdp  * in the <zonename>.xml file: the path to the zone.  This is for performance,
1019a1be23daSdp  * since we need to walk all zonepath's in order to be able to detect conflicts
1020a1be23daSdp  * (see crosscheck_zonepaths() in the zoneadm command).
1021087719fdSdp  *
1022087719fdSdp  * An additional complexity is that when doing a rename, we'd like the entire
1023087719fdSdp  * index update operation (rename, and potential state changes) to be atomic.
1024087719fdSdp  * In general, the operation of this function should succeed or fail as
1025087719fdSdp  * a unit.
1026a1be23daSdp  */
1027a1be23daSdp int
1028a1be23daSdp zonecfg_refresh_index_file(zone_dochandle_t handle)
1029a1be23daSdp {
1030a1be23daSdp 	char name[ZONENAME_MAX], zonepath[MAXPATHLEN];
1031a1be23daSdp 	struct zoneent ze;
1032a1be23daSdp 	int err;
1033087719fdSdp 	int opcode;
1034087719fdSdp 	char *zn;
1035087719fdSdp 
1036087719fdSdp 	bzero(&ze, sizeof (ze));
1037087719fdSdp 	ze.zone_state = -1;	/* Preserve existing state in index */
1038a1be23daSdp 
1039a1be23daSdp 	if ((err = zonecfg_get_name(handle, name, sizeof (name))) != Z_OK)
1040a1be23daSdp 		return (err);
1041087719fdSdp 	(void) strlcpy(ze.zone_name, name, sizeof (ze.zone_name));
1042087719fdSdp 
1043a1be23daSdp 	if ((err = zonecfg_get_zonepath(handle, zonepath,
1044a1be23daSdp 	    sizeof (zonepath))) != Z_OK)
1045a1be23daSdp 		return (err);
1046555afedfScarlsonj 	(void) strlcpy(ze.zone_path, zonepath + strlen(zonecfg_root),
1047555afedfScarlsonj 	    sizeof (ze.zone_path));
1048087719fdSdp 
1049087719fdSdp 	if (is_renaming(handle)) {
1050087719fdSdp 		opcode = PZE_MODIFY;
1051087719fdSdp 		(void) strlcpy(ze.zone_name, handle->zone_dh_delete_name,
1052087719fdSdp 		    sizeof (ze.zone_name));
1053087719fdSdp 		(void) strlcpy(ze.zone_newname, name, sizeof (ze.zone_newname));
1054087719fdSdp 	} else if (is_new(handle)) {
1055087719fdSdp 		FILE *cookie;
1056087719fdSdp 		/*
1057087719fdSdp 		 * Be tolerant of the zone already existing in the index file,
1058087719fdSdp 		 * since we might be forcibly overwriting an existing
1059087719fdSdp 		 * configuration with a new one (for example 'create -F'
1060087719fdSdp 		 * in zonecfg).
1061087719fdSdp 		 */
1062087719fdSdp 		opcode = PZE_ADD;
1063087719fdSdp 		cookie = setzoneent();
1064087719fdSdp 		while ((zn = getzoneent(cookie)) != NULL) {
1065087719fdSdp 			if (strcmp(zn, name) == 0) {
1066087719fdSdp 				opcode = PZE_MODIFY;
1067087719fdSdp 				free(zn);
1068087719fdSdp 				break;
1069087719fdSdp 			}
1070087719fdSdp 			free(zn);
1071087719fdSdp 		}
1072087719fdSdp 		endzoneent(cookie);
1073087719fdSdp 		ze.zone_state = ZONE_STATE_CONFIGURED;
1074087719fdSdp 	} else {
1075087719fdSdp 		opcode = PZE_MODIFY;
10767c478bd9Sstevel@tonic-gate 	}
10777c478bd9Sstevel@tonic-gate 
1078087719fdSdp 	if ((err = putzoneent(&ze, opcode)) != Z_OK)
1079087719fdSdp 		return (err);
1080087719fdSdp 
1081087719fdSdp 	return (Z_OK);
1082087719fdSdp }
1083087719fdSdp 
1084087719fdSdp /*
1085087719fdSdp  * The goal of this routine is to cause the index file update and the
1086087719fdSdp  * document save to happen as an atomic operation.  We do the document
1087087719fdSdp  * first, saving a backup copy using a hard link; if that succeeds, we go
1088087719fdSdp  * on to the index.  If that fails, we roll the document back into place.
1089087719fdSdp  *
1090087719fdSdp  * Strategy:
1091087719fdSdp  *
1092087719fdSdp  * New zone 'foo' configuration:
1093087719fdSdp  * 	Create tmpfile (zonecfg.xxxxxx)
1094087719fdSdp  * 	Write XML to tmpfile
1095087719fdSdp  * 	Rename tmpfile to xmlfile (zonecfg.xxxxxx -> foo.xml)
1096087719fdSdp  * 	Add entry to index file
1097087719fdSdp  * 	If it fails, delete foo.xml, leaving nothing behind.
1098087719fdSdp  *
1099087719fdSdp  * Save existing zone 'foo':
1100087719fdSdp  * 	Make backup of foo.xml -> .backup
1101087719fdSdp  * 	Create tmpfile (zonecfg.xxxxxx)
1102087719fdSdp  * 	Write XML to tmpfile
1103087719fdSdp  * 	Rename tmpfile to xmlfile (zonecfg.xxxxxx -> foo.xml)
1104087719fdSdp  * 	Modify index file as needed
1105087719fdSdp  * 	If it fails, recover from .backup -> foo.xml
1106087719fdSdp  *
1107087719fdSdp  * Rename 'foo' to 'bar':
1108087719fdSdp  * 	Create tmpfile (zonecfg.xxxxxx)
1109087719fdSdp  * 	Write XML to tmpfile
1110087719fdSdp  * 	Rename tmpfile to xmlfile (zonecfg.xxxxxx -> bar.xml)
1111087719fdSdp  * 	Add entry for 'bar' to index file, Remove entry for 'foo' (refresh)
1112087719fdSdp  * 	If it fails, delete bar.xml; foo.xml is left behind.
1113087719fdSdp  */
11147c478bd9Sstevel@tonic-gate static int
11157c478bd9Sstevel@tonic-gate zonecfg_save_impl(zone_dochandle_t handle, char *filename)
11167c478bd9Sstevel@tonic-gate {
11177c478bd9Sstevel@tonic-gate 	char tmpfile[MAXPATHLEN];
1118087719fdSdp 	char bakdir[MAXPATHLEN], bakbase[MAXPATHLEN], bakfile[MAXPATHLEN];
11199acbbeafSnn35248 	int tmpfd, err, valid;
11207c478bd9Sstevel@tonic-gate 	xmlValidCtxt cvp = { NULL };
1121087719fdSdp 	boolean_t backup;
11227c478bd9Sstevel@tonic-gate 
11237c478bd9Sstevel@tonic-gate 	(void) strlcpy(tmpfile, filename, sizeof (tmpfile));
11247c478bd9Sstevel@tonic-gate 	(void) dirname(tmpfile);
11257c478bd9Sstevel@tonic-gate 	(void) strlcat(tmpfile, _PATH_TMPFILE, sizeof (tmpfile));
11267c478bd9Sstevel@tonic-gate 
11277c478bd9Sstevel@tonic-gate 	tmpfd = mkstemp(tmpfile);
11287c478bd9Sstevel@tonic-gate 	if (tmpfd == -1) {
11297c478bd9Sstevel@tonic-gate 		(void) unlink(tmpfile);
11307c478bd9Sstevel@tonic-gate 		return (Z_TEMP_FILE);
11317c478bd9Sstevel@tonic-gate 	}
11327c478bd9Sstevel@tonic-gate 	(void) close(tmpfd);
11337c478bd9Sstevel@tonic-gate 
11347c478bd9Sstevel@tonic-gate 	cvp.error = zonecfg_error_func;
11357c478bd9Sstevel@tonic-gate 	cvp.warning = zonecfg_error_func;
11367c478bd9Sstevel@tonic-gate 
1137087719fdSdp 	/*
11389acbbeafSnn35248 	 * We do a final validation of the document.  Since the library has
11399acbbeafSnn35248 	 * malfunctioned if it fails to validate, we follow-up with an
11409acbbeafSnn35248 	 * assert() that the doc is valid.
1141087719fdSdp 	 */
11429acbbeafSnn35248 	valid = xmlValidateDocument(&cvp, handle->zone_dh_doc);
11439acbbeafSnn35248 	assert(valid != 0);
11447c478bd9Sstevel@tonic-gate 
11457c478bd9Sstevel@tonic-gate 	if (xmlSaveFormatFile(tmpfile, handle->zone_dh_doc, 1) <= 0)
11467c478bd9Sstevel@tonic-gate 		goto err;
1147087719fdSdp 
11487c478bd9Sstevel@tonic-gate 	(void) chmod(tmpfile, 0644);
11497c478bd9Sstevel@tonic-gate 
1150087719fdSdp 	/*
1151087719fdSdp 	 * In the event we are doing a standard save, hard link a copy of the
1152087719fdSdp 	 * original file in .backup.<pid>.filename so we can restore it if
1153087719fdSdp 	 * something goes wrong.
1154087719fdSdp 	 */
1155087719fdSdp 	if (!is_new(handle) && !is_renaming(handle)) {
1156087719fdSdp 		backup = B_TRUE;
1157087719fdSdp 
1158087719fdSdp 		(void) strlcpy(bakdir, filename, sizeof (bakdir));
1159087719fdSdp 		(void) strlcpy(bakbase, filename, sizeof (bakbase));
1160087719fdSdp 		(void) snprintf(bakfile, sizeof (bakfile), "%s/.backup.%d.%s",
1161087719fdSdp 		    dirname(bakdir), getpid(), basename(bakbase));
1162087719fdSdp 
1163087719fdSdp 		if (link(filename, bakfile) == -1) {
1164087719fdSdp 			err = errno;
11657c478bd9Sstevel@tonic-gate 			(void) unlink(tmpfile);
11667c478bd9Sstevel@tonic-gate 			if (errno == EACCES)
11677c478bd9Sstevel@tonic-gate 				return (Z_ACCES);
11687c478bd9Sstevel@tonic-gate 			return (Z_MISC_FS);
11697c478bd9Sstevel@tonic-gate 		}
1170087719fdSdp 	}
1171a1be23daSdp 
1172087719fdSdp 	/*
1173087719fdSdp 	 * Move the new document over top of the old.
1174087719fdSdp 	 * i.e.:   zonecfg.XXXXXX  ->  myzone.xml
1175087719fdSdp 	 */
1176087719fdSdp 	if (rename(tmpfile, filename) == -1) {
1177087719fdSdp 		err = errno;
1178087719fdSdp 		(void) unlink(tmpfile);
1179087719fdSdp 		if (backup)
1180087719fdSdp 			(void) unlink(bakfile);
1181087719fdSdp 		if (err == EACCES)
1182087719fdSdp 			return (Z_ACCES);
1183087719fdSdp 		return (Z_MISC_FS);
1184087719fdSdp 	}
1185087719fdSdp 
1186087719fdSdp 	/*
1187087719fdSdp 	 * If this is a snapshot, we're done-- don't add an index entry.
1188087719fdSdp 	 */
1189087719fdSdp 	if (is_snapshot(handle))
1190087719fdSdp 		return (Z_OK);
1191087719fdSdp 
1192087719fdSdp 	/* now update the index file to reflect whatever we just did */
1193087719fdSdp 	if ((err = zonecfg_refresh_index_file(handle)) != Z_OK) {
1194087719fdSdp 		if (backup) {
1195087719fdSdp 			/*
1196087719fdSdp 			 * Try to restore from our backup.
1197087719fdSdp 			 */
1198087719fdSdp 			(void) unlink(filename);
1199087719fdSdp 			(void) rename(bakfile, filename);
1200087719fdSdp 		} else {
1201087719fdSdp 			/*
1202087719fdSdp 			 * Either the zone is new, in which case we can delete
1203087719fdSdp 			 * new.xml, or we're doing a rename, so ditto.
1204087719fdSdp 			 */
1205087719fdSdp 			assert(is_new(handle) || is_renaming(handle));
1206087719fdSdp 			(void) unlink(filename);
1207087719fdSdp 		}
1208087719fdSdp 		return (Z_UPDATING_INDEX);
1209087719fdSdp 	}
1210087719fdSdp 
1211087719fdSdp 	if (backup)
1212087719fdSdp 		(void) unlink(bakfile);
1213087719fdSdp 
1214087719fdSdp 	return (Z_OK);
12157c478bd9Sstevel@tonic-gate 
12167c478bd9Sstevel@tonic-gate err:
12177c478bd9Sstevel@tonic-gate 	(void) unlink(tmpfile);
12187c478bd9Sstevel@tonic-gate 	return (Z_SAVING_FILE);
12197c478bd9Sstevel@tonic-gate }
12207c478bd9Sstevel@tonic-gate 
12217c478bd9Sstevel@tonic-gate int
12227c478bd9Sstevel@tonic-gate zonecfg_save(zone_dochandle_t handle)
12237c478bd9Sstevel@tonic-gate {
1224087719fdSdp 	char zname[ZONENAME_MAX], path[MAXPATHLEN];
1225087719fdSdp 	char delpath[MAXPATHLEN];
1226087719fdSdp 	int err = Z_SAVING_FILE;
12277c478bd9Sstevel@tonic-gate 
1228087719fdSdp 	if (zonecfg_check_handle(handle) != Z_OK)
1229087719fdSdp 		return (Z_BAD_HANDLE);
1230087719fdSdp 
1231087719fdSdp 	/*
1232ee519a1fSgjelinek 	 * We don't support saving snapshots or a tree containing a sw
1233ee519a1fSgjelinek 	 * inventory at this time.
1234087719fdSdp 	 */
1235ee519a1fSgjelinek 	if (handle->zone_dh_snapshot || handle->zone_dh_sw_inv)
1236087719fdSdp 		return (Z_INVAL);
1237087719fdSdp 
1238087719fdSdp 	if ((err = zonecfg_get_name(handle, zname, sizeof (zname))) != Z_OK)
12397c478bd9Sstevel@tonic-gate 		return (err);
1240087719fdSdp 
1241108322fbScarlsonj 	if (!config_file_path(zname, path))
1242108322fbScarlsonj 		return (Z_MISC_FS);
1243087719fdSdp 
1244087719fdSdp 	addcomment(handle, "\n    DO NOT EDIT THIS "
1245087719fdSdp 	    "FILE.  Use zonecfg(1M) instead.\n");
1246087719fdSdp 
1247087719fdSdp 	err = zonecfg_save_impl(handle, path);
1248087719fdSdp 
1249087719fdSdp 	stripcomments(handle);
1250087719fdSdp 
1251087719fdSdp 	if (err != Z_OK)
1252087719fdSdp 		return (err);
1253087719fdSdp 
1254087719fdSdp 	handle->zone_dh_newzone = B_FALSE;
1255087719fdSdp 
1256087719fdSdp 	if (is_renaming(handle)) {
1257108322fbScarlsonj 		if (config_file_path(handle->zone_dh_delete_name, delpath))
1258087719fdSdp 			(void) unlink(delpath);
1259087719fdSdp 		handle->zone_dh_delete_name[0] = '\0';
1260087719fdSdp 	}
1261087719fdSdp 
1262087719fdSdp 	return (Z_OK);
12637c478bd9Sstevel@tonic-gate }
12647c478bd9Sstevel@tonic-gate 
1265ee519a1fSgjelinek int
12669acbbeafSnn35248 zonecfg_verify_save(zone_dochandle_t handle, char *filename)
12679acbbeafSnn35248 {
12689acbbeafSnn35248 	int valid;
12699acbbeafSnn35248 
12709acbbeafSnn35248 	xmlValidCtxt cvp = { NULL };
12719acbbeafSnn35248 
12729acbbeafSnn35248 	if (zonecfg_check_handle(handle) != Z_OK)
12739acbbeafSnn35248 		return (Z_BAD_HANDLE);
12749acbbeafSnn35248 
12759acbbeafSnn35248 	cvp.error = zonecfg_error_func;
12769acbbeafSnn35248 	cvp.warning = zonecfg_error_func;
12779acbbeafSnn35248 
12789acbbeafSnn35248 	/*
12799acbbeafSnn35248 	 * We do a final validation of the document.  Since the library has
12809acbbeafSnn35248 	 * malfunctioned if it fails to validate, we follow-up with an
12819acbbeafSnn35248 	 * assert() that the doc is valid.
12829acbbeafSnn35248 	 */
12839acbbeafSnn35248 	valid = xmlValidateDocument(&cvp, handle->zone_dh_doc);
12849acbbeafSnn35248 	assert(valid != 0);
12859acbbeafSnn35248 
12869acbbeafSnn35248 	if (xmlSaveFormatFile(filename, handle->zone_dh_doc, 1) <= 0)
12879acbbeafSnn35248 		return (Z_SAVING_FILE);
12889acbbeafSnn35248 
12899acbbeafSnn35248 	return (Z_OK);
12909acbbeafSnn35248 }
12919acbbeafSnn35248 
12929acbbeafSnn35248 int
12938cd327d5Sgjelinek zonecfg_detach_save(zone_dochandle_t handle, uint_t flags)
1294ee519a1fSgjelinek {
1295ee519a1fSgjelinek 	char zname[ZONENAME_MAX];
1296ee519a1fSgjelinek 	char path[MAXPATHLEN];
1297ee519a1fSgjelinek 	char migpath[MAXPATHLEN];
1298ee519a1fSgjelinek 	xmlValidCtxt cvp = { NULL };
1299ee519a1fSgjelinek 	int err = Z_SAVING_FILE;
13009acbbeafSnn35248 	int valid;
1301ee519a1fSgjelinek 
1302ee519a1fSgjelinek 	if (zonecfg_check_handle(handle) != Z_OK)
1303ee519a1fSgjelinek 		return (Z_BAD_HANDLE);
1304ee519a1fSgjelinek 
13058cd327d5Sgjelinek 	if (flags & ZONE_DRY_RUN) {
13068cd327d5Sgjelinek 		(void) strlcpy(migpath, "-", sizeof (migpath));
13078cd327d5Sgjelinek 	} else {
13088cd327d5Sgjelinek 		if ((err = zonecfg_get_name(handle, zname, sizeof (zname)))
13098cd327d5Sgjelinek 		    != Z_OK)
1310ee519a1fSgjelinek 			return (err);
1311ee519a1fSgjelinek 
13128cd327d5Sgjelinek 		if ((err = zone_get_zonepath(zname, path, sizeof (path)))
13138cd327d5Sgjelinek 		    != Z_OK)
1314ee519a1fSgjelinek 			return (err);
1315ee519a1fSgjelinek 
131616ab8c7bSgjelinek 		if (snprintf(migpath, sizeof (migpath), "%s/%s", path,
131716ab8c7bSgjelinek 		    ZONE_DETACHED) >= sizeof (migpath))
1318ee519a1fSgjelinek 			return (Z_NOMEM);
13198cd327d5Sgjelinek 	}
1320ee519a1fSgjelinek 
1321ee519a1fSgjelinek 	if ((err = operation_prep(handle)) != Z_OK)
1322ee519a1fSgjelinek 		return (err);
1323ee519a1fSgjelinek 
1324ee519a1fSgjelinek 	addcomment(handle, "\n    DO NOT EDIT THIS FILE.  "
1325ee519a1fSgjelinek 	    "Use zonecfg(1M) and zoneadm(1M) attach.\n");
1326ee519a1fSgjelinek 
1327ee519a1fSgjelinek 	cvp.error = zonecfg_error_func;
1328ee519a1fSgjelinek 	cvp.warning = zonecfg_error_func;
1329ee519a1fSgjelinek 
1330ee519a1fSgjelinek 	/*
13319acbbeafSnn35248 	 * We do a final validation of the document.  Since the library has
13329acbbeafSnn35248 	 * malfunctioned if it fails to validate, we follow-up with an
13339acbbeafSnn35248 	 * assert() that the doc is valid.
1334ee519a1fSgjelinek 	 */
13359acbbeafSnn35248 	valid = xmlValidateDocument(&cvp, handle->zone_dh_doc);
13369acbbeafSnn35248 	assert(valid != 0);
1337ee519a1fSgjelinek 
1338ee519a1fSgjelinek 	if (xmlSaveFormatFile(migpath, handle->zone_dh_doc, 1) <= 0)
1339ee519a1fSgjelinek 		return (Z_SAVING_FILE);
1340ee519a1fSgjelinek 
13418cd327d5Sgjelinek 	if (!(flags & ZONE_DRY_RUN))
1342ee519a1fSgjelinek 		(void) chmod(migpath, 0644);
1343ee519a1fSgjelinek 
1344ee519a1fSgjelinek 	stripcomments(handle);
1345ee519a1fSgjelinek 
1346ee519a1fSgjelinek 	handle->zone_dh_newzone = B_FALSE;
1347ee519a1fSgjelinek 
1348ee519a1fSgjelinek 	return (Z_OK);
1349ee519a1fSgjelinek }
1350ee519a1fSgjelinek 
1351ee519a1fSgjelinek boolean_t
1352ee519a1fSgjelinek zonecfg_detached(const char *path)
1353ee519a1fSgjelinek {
1354ee519a1fSgjelinek 	char		migpath[MAXPATHLEN];
1355ee519a1fSgjelinek 	struct stat	buf;
1356ee519a1fSgjelinek 
135716ab8c7bSgjelinek 	if (snprintf(migpath, sizeof (migpath), "%s/%s", path, ZONE_DETACHED) >=
1358ee519a1fSgjelinek 	    sizeof (migpath))
1359ee519a1fSgjelinek 		return (B_FALSE);
1360ee519a1fSgjelinek 
1361ee519a1fSgjelinek 	if (stat(migpath, &buf) != -1)
1362ee519a1fSgjelinek 		return (B_TRUE);
1363ee519a1fSgjelinek 
1364ee519a1fSgjelinek 	return (B_FALSE);
1365ee519a1fSgjelinek }
1366ee519a1fSgjelinek 
1367ee519a1fSgjelinek void
1368ee519a1fSgjelinek zonecfg_rm_detached(zone_dochandle_t handle, boolean_t forced)
1369ee519a1fSgjelinek {
1370ee519a1fSgjelinek 	char zname[ZONENAME_MAX];
1371ee519a1fSgjelinek 	char path[MAXPATHLEN];
1372ee519a1fSgjelinek 	char detached[MAXPATHLEN];
1373ee519a1fSgjelinek 	char attached[MAXPATHLEN];
1374ee519a1fSgjelinek 
1375ee519a1fSgjelinek 	if (zonecfg_check_handle(handle) != Z_OK)
1376ee519a1fSgjelinek 		return;
1377ee519a1fSgjelinek 
1378ee519a1fSgjelinek 	if (zonecfg_get_name(handle, zname, sizeof (zname)) != Z_OK)
1379ee519a1fSgjelinek 		return;
1380ee519a1fSgjelinek 
1381ee519a1fSgjelinek 	if (zone_get_zonepath(zname, path, sizeof (path)) != Z_OK)
1382ee519a1fSgjelinek 		return;
1383ee519a1fSgjelinek 
138416ab8c7bSgjelinek 	(void) snprintf(detached, sizeof (detached), "%s/%s", path,
138516ab8c7bSgjelinek 	    ZONE_DETACHED);
1386ee519a1fSgjelinek 	(void) snprintf(attached, sizeof (attached), "%s/%s", path,
1387ee519a1fSgjelinek 	    ATTACH_FORCED);
1388ee519a1fSgjelinek 
1389ee519a1fSgjelinek 	if (forced) {
1390ee519a1fSgjelinek 		(void) rename(detached, attached);
1391ee519a1fSgjelinek 	} else {
1392ee519a1fSgjelinek 		(void) unlink(attached);
1393ee519a1fSgjelinek 		(void) unlink(detached);
1394ee519a1fSgjelinek 	}
1395ee519a1fSgjelinek }
1396ee519a1fSgjelinek 
13977c478bd9Sstevel@tonic-gate /*
13987c478bd9Sstevel@tonic-gate  * Special case: if access(2) fails with ENOENT, then try again using
13997c478bd9Sstevel@tonic-gate  * ZONE_CONFIG_ROOT instead of config_file_path(zonename).  This is how we
14007c478bd9Sstevel@tonic-gate  * work around the case of a config file which has not been created yet:
14017c478bd9Sstevel@tonic-gate  * the user will need access to the directory so use that as a heuristic.
14027c478bd9Sstevel@tonic-gate  */
14037c478bd9Sstevel@tonic-gate 
14047c478bd9Sstevel@tonic-gate int
14057c478bd9Sstevel@tonic-gate zonecfg_access(const char *zonename, int amode)
14067c478bd9Sstevel@tonic-gate {
14077c478bd9Sstevel@tonic-gate 	char path[MAXPATHLEN];
14087c478bd9Sstevel@tonic-gate 
1409108322fbScarlsonj 	if (!config_file_path(zonename, path))
1410108322fbScarlsonj 		return (Z_INVAL);
14117c478bd9Sstevel@tonic-gate 	if (access(path, amode) == 0)
14127c478bd9Sstevel@tonic-gate 		return (Z_OK);
1413108322fbScarlsonj 	if (errno == ENOENT) {
1414108322fbScarlsonj 		if (snprintf(path, sizeof (path), "%s%s", zonecfg_root,
1415108322fbScarlsonj 		    ZONE_CONFIG_ROOT) >= sizeof (path))
1416108322fbScarlsonj 			return (Z_INVAL);
1417108322fbScarlsonj 		if (access(path, amode) == 0)
14187c478bd9Sstevel@tonic-gate 			return (Z_OK);
1419108322fbScarlsonj 	}
14207c478bd9Sstevel@tonic-gate 	if (errno == EACCES)
14217c478bd9Sstevel@tonic-gate 		return (Z_ACCES);
14227c478bd9Sstevel@tonic-gate 	if (errno == EINVAL)
14237c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
14247c478bd9Sstevel@tonic-gate 	return (Z_MISC_FS);
14257c478bd9Sstevel@tonic-gate }
14267c478bd9Sstevel@tonic-gate 
14277c478bd9Sstevel@tonic-gate int
1428108322fbScarlsonj zonecfg_create_snapshot(const char *zonename)
14297c478bd9Sstevel@tonic-gate {
14307c478bd9Sstevel@tonic-gate 	zone_dochandle_t handle;
14317c478bd9Sstevel@tonic-gate 	char path[MAXPATHLEN], zonepath[MAXPATHLEN], rpath[MAXPATHLEN];
14327c478bd9Sstevel@tonic-gate 	int error = Z_OK, res;
14337c478bd9Sstevel@tonic-gate 
14347c478bd9Sstevel@tonic-gate 	if ((handle = zonecfg_init_handle()) == NULL) {
14357c478bd9Sstevel@tonic-gate 		return (Z_NOMEM);
14367c478bd9Sstevel@tonic-gate 	}
14377c478bd9Sstevel@tonic-gate 
1438087719fdSdp 	handle->zone_dh_newzone = B_TRUE;
1439087719fdSdp 	handle->zone_dh_snapshot = B_TRUE;
1440087719fdSdp 
14417c478bd9Sstevel@tonic-gate 	if ((error = zonecfg_get_handle(zonename, handle)) != Z_OK)
14427c478bd9Sstevel@tonic-gate 		goto out;
14437c478bd9Sstevel@tonic-gate 	if ((error = operation_prep(handle)) != Z_OK)
14447c478bd9Sstevel@tonic-gate 		goto out;
14457c478bd9Sstevel@tonic-gate 	error = zonecfg_get_zonepath(handle, zonepath, sizeof (zonepath));
14467c478bd9Sstevel@tonic-gate 	if (error != Z_OK)
14477c478bd9Sstevel@tonic-gate 		goto out;
14487c478bd9Sstevel@tonic-gate 	if ((res = resolvepath(zonepath, rpath, sizeof (rpath))) == -1) {
14497c478bd9Sstevel@tonic-gate 		error = Z_RESOLVED_PATH;
14507c478bd9Sstevel@tonic-gate 		goto out;
14517c478bd9Sstevel@tonic-gate 	}
14527c478bd9Sstevel@tonic-gate 	/*
14537c478bd9Sstevel@tonic-gate 	 * If the resolved path is not the same as the original path, then
14547c478bd9Sstevel@tonic-gate 	 * save the resolved path in the snapshot, thus preventing any
14557c478bd9Sstevel@tonic-gate 	 * potential problems down the line when zoneadmd goes to unmount
14567c478bd9Sstevel@tonic-gate 	 * file systems and depends on initial string matches with resolved
14577c478bd9Sstevel@tonic-gate 	 * paths.
14587c478bd9Sstevel@tonic-gate 	 */
14597c478bd9Sstevel@tonic-gate 	rpath[res] = '\0';
14607c478bd9Sstevel@tonic-gate 	if (strcmp(zonepath, rpath) != 0) {
14617c478bd9Sstevel@tonic-gate 		if ((error = zonecfg_set_zonepath(handle, rpath)) != Z_OK)
14627c478bd9Sstevel@tonic-gate 			goto out;
14637c478bd9Sstevel@tonic-gate 	}
1464108322fbScarlsonj 	if (snprintf(path, sizeof (path), "%s%s", zonecfg_root,
1465108322fbScarlsonj 	    ZONE_SNAPSHOT_ROOT) >= sizeof (path)) {
1466108322fbScarlsonj 		error = Z_MISC_FS;
1467108322fbScarlsonj 		goto out;
1468108322fbScarlsonj 	}
1469108322fbScarlsonj 	if ((mkdir(path, S_IRWXU) == -1) && (errno != EEXIST)) {
14707c478bd9Sstevel@tonic-gate 		error = Z_MISC_FS;
14717c478bd9Sstevel@tonic-gate 		goto out;
14727c478bd9Sstevel@tonic-gate 	}
14737c478bd9Sstevel@tonic-gate 
1474108322fbScarlsonj 	if (!snap_file_path(zonename, path)) {
1475108322fbScarlsonj 		error = Z_MISC_FS;
1476108322fbScarlsonj 		goto out;
1477108322fbScarlsonj 	}
1478087719fdSdp 
1479087719fdSdp 	addcomment(handle, "\n    DO NOT EDIT THIS FILE.  "
1480087719fdSdp 	    "It is a snapshot of running zone state.\n");
1481087719fdSdp 
14827c478bd9Sstevel@tonic-gate 	error = zonecfg_save_impl(handle, path);
14837c478bd9Sstevel@tonic-gate 
1484087719fdSdp 	stripcomments(handle);
1485087719fdSdp 
14867c478bd9Sstevel@tonic-gate out:
14877c478bd9Sstevel@tonic-gate 	zonecfg_fini_handle(handle);
14887c478bd9Sstevel@tonic-gate 	return (error);
14897c478bd9Sstevel@tonic-gate }
14907c478bd9Sstevel@tonic-gate 
1491f4b3ec61Sdh155122 int
1492f4b3ec61Sdh155122 zonecfg_get_iptype(zone_dochandle_t handle, zone_iptype_t *iptypep)
1493f4b3ec61Sdh155122 {
1494f4b3ec61Sdh155122 	char property[10]; /* 10 is big enough for "shared"/"exclusive" */
1495f4b3ec61Sdh155122 	int err;
1496f4b3ec61Sdh155122 
1497f4b3ec61Sdh155122 	err = getrootattr(handle, DTD_ATTR_IPTYPE, property, sizeof (property));
1498f4b3ec61Sdh155122 	if (err == Z_BAD_PROPERTY) {
1499f4b3ec61Sdh155122 		/* Return default value */
1500f4b3ec61Sdh155122 		*iptypep = ZS_SHARED;
1501f4b3ec61Sdh155122 		return (Z_OK);
1502f4b3ec61Sdh155122 	} else if (err != Z_OK) {
1503f4b3ec61Sdh155122 		return (err);
1504f4b3ec61Sdh155122 	}
1505f4b3ec61Sdh155122 
1506f4b3ec61Sdh155122 	if (strlen(property) == 0 ||
1507f4b3ec61Sdh155122 	    strcmp(property, "shared") == 0)
1508f4b3ec61Sdh155122 		*iptypep = ZS_SHARED;
1509f4b3ec61Sdh155122 	else if (strcmp(property, "exclusive") == 0)
1510f4b3ec61Sdh155122 		*iptypep = ZS_EXCLUSIVE;
1511f4b3ec61Sdh155122 	else
1512f4b3ec61Sdh155122 		return (Z_INVAL);
1513f4b3ec61Sdh155122 
1514f4b3ec61Sdh155122 	return (Z_OK);
1515f4b3ec61Sdh155122 }
1516f4b3ec61Sdh155122 
1517f4b3ec61Sdh155122 int
1518f4b3ec61Sdh155122 zonecfg_set_iptype(zone_dochandle_t handle, zone_iptype_t iptype)
1519f4b3ec61Sdh155122 {
1520f4b3ec61Sdh155122 	xmlNodePtr cur;
1521f4b3ec61Sdh155122 
1522f4b3ec61Sdh155122 	if (handle == NULL)
1523f4b3ec61Sdh155122 		return (Z_INVAL);
1524f4b3ec61Sdh155122 
1525f4b3ec61Sdh155122 	cur = xmlDocGetRootElement(handle->zone_dh_doc);
1526f4b3ec61Sdh155122 	if (cur == NULL) {
1527f4b3ec61Sdh155122 		return (Z_EMPTY_DOCUMENT);
1528f4b3ec61Sdh155122 	}
1529f4b3ec61Sdh155122 
1530f4b3ec61Sdh155122 	if (xmlStrcmp(cur->name, DTD_ELEM_ZONE) != 0) {
1531f4b3ec61Sdh155122 		return (Z_WRONG_DOC_TYPE);
1532f4b3ec61Sdh155122 	}
1533f4b3ec61Sdh155122 	switch (iptype) {
1534f4b3ec61Sdh155122 	case ZS_SHARED:
1535f4b3ec61Sdh155122 		/*
1536f4b3ec61Sdh155122 		 * Since "shared" is the default, we don't write it to the
1537f4b3ec61Sdh155122 		 * configuration file, so that it's easier to migrate those
1538f4b3ec61Sdh155122 		 * zones elsewhere, eg., to systems which are not IP-Instances
1539f4b3ec61Sdh155122 		 * aware.
1540f4b3ec61Sdh155122 		 * xmlUnsetProp only fails when the attribute doesn't exist,
1541f4b3ec61Sdh155122 		 * which we don't care.
1542f4b3ec61Sdh155122 		 */
1543f4b3ec61Sdh155122 		(void) xmlUnsetProp(cur, DTD_ATTR_IPTYPE);
1544f4b3ec61Sdh155122 		break;
1545f4b3ec61Sdh155122 	case ZS_EXCLUSIVE:
1546f4b3ec61Sdh155122 		if (xmlSetProp(cur, DTD_ATTR_IPTYPE,
1547f4b3ec61Sdh155122 		    (const xmlChar *) "exclusive") == NULL)
1548f4b3ec61Sdh155122 			return (Z_INVAL);
1549f4b3ec61Sdh155122 		break;
1550f4b3ec61Sdh155122 	}
1551f4b3ec61Sdh155122 	return (Z_OK);
1552f4b3ec61Sdh155122 }
1553f4b3ec61Sdh155122 
15547c478bd9Sstevel@tonic-gate static int
1555a1be23daSdp newprop(xmlNodePtr node, const xmlChar *attrname, char *src)
15567c478bd9Sstevel@tonic-gate {
15577c478bd9Sstevel@tonic-gate 	xmlAttrPtr newattr;
15587c478bd9Sstevel@tonic-gate 
15597c478bd9Sstevel@tonic-gate 	newattr = xmlNewProp(node, attrname, (xmlChar *)src);
15607c478bd9Sstevel@tonic-gate 	if (newattr == NULL) {
15617c478bd9Sstevel@tonic-gate 		xmlUnlinkNode(node);
15627c478bd9Sstevel@tonic-gate 		xmlFreeNode(node);
15637c478bd9Sstevel@tonic-gate 		return (Z_BAD_PROPERTY);
15647c478bd9Sstevel@tonic-gate 	}
15657c478bd9Sstevel@tonic-gate 	return (Z_OK);
15667c478bd9Sstevel@tonic-gate }
15677c478bd9Sstevel@tonic-gate 
15687c478bd9Sstevel@tonic-gate static int
15697c478bd9Sstevel@tonic-gate zonecfg_add_filesystem_core(zone_dochandle_t handle, struct zone_fstab *tabptr)
15707c478bd9Sstevel@tonic-gate {
15717c478bd9Sstevel@tonic-gate 	xmlNodePtr newnode, cur = handle->zone_dh_cur, options_node;
15727c478bd9Sstevel@tonic-gate 	zone_fsopt_t *ptr;
15737c478bd9Sstevel@tonic-gate 	int err;
15747c478bd9Sstevel@tonic-gate 
15757c478bd9Sstevel@tonic-gate 	newnode = xmlNewTextChild(cur, NULL, DTD_ELEM_FS, NULL);
1576a1be23daSdp 	if ((err = newprop(newnode, DTD_ATTR_SPECIAL,
15777c478bd9Sstevel@tonic-gate 	    tabptr->zone_fs_special)) != Z_OK)
15787c478bd9Sstevel@tonic-gate 		return (err);
15797c478bd9Sstevel@tonic-gate 	if (tabptr->zone_fs_raw[0] != '\0' &&
1580a1be23daSdp 	    (err = newprop(newnode, DTD_ATTR_RAW, tabptr->zone_fs_raw)) != Z_OK)
15817c478bd9Sstevel@tonic-gate 		return (err);
1582a1be23daSdp 	if ((err = newprop(newnode, DTD_ATTR_DIR, tabptr->zone_fs_dir)) != Z_OK)
15837c478bd9Sstevel@tonic-gate 		return (err);
1584a1be23daSdp 	if ((err = newprop(newnode, DTD_ATTR_TYPE,
15857c478bd9Sstevel@tonic-gate 	    tabptr->zone_fs_type)) != Z_OK)
15867c478bd9Sstevel@tonic-gate 		return (err);
15877c478bd9Sstevel@tonic-gate 	if (tabptr->zone_fs_options != NULL) {
15887c478bd9Sstevel@tonic-gate 		for (ptr = tabptr->zone_fs_options; ptr != NULL;
15897c478bd9Sstevel@tonic-gate 		    ptr = ptr->zone_fsopt_next) {
15907c478bd9Sstevel@tonic-gate 			options_node = xmlNewTextChild(newnode, NULL,
15917c478bd9Sstevel@tonic-gate 			    DTD_ELEM_FSOPTION, NULL);
1592a1be23daSdp 			if ((err = newprop(options_node, DTD_ATTR_NAME,
15937c478bd9Sstevel@tonic-gate 			    ptr->zone_fsopt_opt)) != Z_OK)
15947c478bd9Sstevel@tonic-gate 				return (err);
15957c478bd9Sstevel@tonic-gate 		}
15967c478bd9Sstevel@tonic-gate 	}
15977c478bd9Sstevel@tonic-gate 	return (Z_OK);
15987c478bd9Sstevel@tonic-gate }
15997c478bd9Sstevel@tonic-gate 
16007c478bd9Sstevel@tonic-gate int
16017c478bd9Sstevel@tonic-gate zonecfg_add_filesystem(zone_dochandle_t handle, struct zone_fstab *tabptr)
16027c478bd9Sstevel@tonic-gate {
16037c478bd9Sstevel@tonic-gate 	int err;
16047c478bd9Sstevel@tonic-gate 
16057c478bd9Sstevel@tonic-gate 	if (tabptr == NULL)
16067c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
16077c478bd9Sstevel@tonic-gate 
16087c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
16097c478bd9Sstevel@tonic-gate 		return (err);
16107c478bd9Sstevel@tonic-gate 
16117c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_add_filesystem_core(handle, tabptr)) != Z_OK)
16127c478bd9Sstevel@tonic-gate 		return (err);
16137c478bd9Sstevel@tonic-gate 
16147c478bd9Sstevel@tonic-gate 	return (Z_OK);
16157c478bd9Sstevel@tonic-gate }
16167c478bd9Sstevel@tonic-gate 
16177c478bd9Sstevel@tonic-gate static int
16187c478bd9Sstevel@tonic-gate zonecfg_add_ipd_core(zone_dochandle_t handle, struct zone_fstab *tabptr)
16197c478bd9Sstevel@tonic-gate {
16207c478bd9Sstevel@tonic-gate 	xmlNodePtr newnode, cur = handle->zone_dh_cur;
16217c478bd9Sstevel@tonic-gate 	int err;
16227c478bd9Sstevel@tonic-gate 
16237c478bd9Sstevel@tonic-gate 	newnode = xmlNewTextChild(cur, NULL, DTD_ELEM_IPD, NULL);
1624a1be23daSdp 	if ((err = newprop(newnode, DTD_ATTR_DIR, tabptr->zone_fs_dir)) != Z_OK)
16257c478bd9Sstevel@tonic-gate 		return (err);
16267c478bd9Sstevel@tonic-gate 	return (Z_OK);
16277c478bd9Sstevel@tonic-gate }
16287c478bd9Sstevel@tonic-gate 
16297c478bd9Sstevel@tonic-gate int
16307c478bd9Sstevel@tonic-gate zonecfg_add_ipd(zone_dochandle_t handle, struct zone_fstab *tabptr)
16317c478bd9Sstevel@tonic-gate {
16327c478bd9Sstevel@tonic-gate 	int err;
16337c478bd9Sstevel@tonic-gate 
16347c478bd9Sstevel@tonic-gate 	if (tabptr == NULL)
16357c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
16367c478bd9Sstevel@tonic-gate 
16377c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
16387c478bd9Sstevel@tonic-gate 		return (err);
16397c478bd9Sstevel@tonic-gate 
16407c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_add_ipd_core(handle, tabptr)) != Z_OK)
16417c478bd9Sstevel@tonic-gate 		return (err);
16427c478bd9Sstevel@tonic-gate 
16437c478bd9Sstevel@tonic-gate 	return (Z_OK);
16447c478bd9Sstevel@tonic-gate }
16457c478bd9Sstevel@tonic-gate 
16467c478bd9Sstevel@tonic-gate int
16477c478bd9Sstevel@tonic-gate zonecfg_add_fs_option(struct zone_fstab *tabptr, char *option)
16487c478bd9Sstevel@tonic-gate {
16497c478bd9Sstevel@tonic-gate 	zone_fsopt_t *last, *old, *new;
16507c478bd9Sstevel@tonic-gate 
16517c478bd9Sstevel@tonic-gate 	last = tabptr->zone_fs_options;
16527c478bd9Sstevel@tonic-gate 	for (old = last; old != NULL; old = old->zone_fsopt_next)
16537c478bd9Sstevel@tonic-gate 		last = old;	/* walk to the end of the list */
16547c478bd9Sstevel@tonic-gate 	new = (zone_fsopt_t *)malloc(sizeof (zone_fsopt_t));
16557c478bd9Sstevel@tonic-gate 	if (new == NULL)
16567c478bd9Sstevel@tonic-gate 		return (Z_NOMEM);
16577c478bd9Sstevel@tonic-gate 	(void) strlcpy(new->zone_fsopt_opt, option,
16587c478bd9Sstevel@tonic-gate 	    sizeof (new->zone_fsopt_opt));
16597c478bd9Sstevel@tonic-gate 	new->zone_fsopt_next = NULL;
16607c478bd9Sstevel@tonic-gate 	if (last == NULL)
16617c478bd9Sstevel@tonic-gate 		tabptr->zone_fs_options = new;
16627c478bd9Sstevel@tonic-gate 	else
16637c478bd9Sstevel@tonic-gate 		last->zone_fsopt_next = new;
16647c478bd9Sstevel@tonic-gate 	return (Z_OK);
16657c478bd9Sstevel@tonic-gate }
16667c478bd9Sstevel@tonic-gate 
16677c478bd9Sstevel@tonic-gate int
16687c478bd9Sstevel@tonic-gate zonecfg_remove_fs_option(struct zone_fstab *tabptr, char *option)
16697c478bd9Sstevel@tonic-gate {
16707c478bd9Sstevel@tonic-gate 	zone_fsopt_t *last, *this, *next;
16717c478bd9Sstevel@tonic-gate 
16727c478bd9Sstevel@tonic-gate 	last = tabptr->zone_fs_options;
16737c478bd9Sstevel@tonic-gate 	for (this = last; this != NULL; this = this->zone_fsopt_next) {
16747c478bd9Sstevel@tonic-gate 		if (strcmp(this->zone_fsopt_opt, option) == 0) {
16757c478bd9Sstevel@tonic-gate 			next = this->zone_fsopt_next;
16767c478bd9Sstevel@tonic-gate 			if (this == tabptr->zone_fs_options)
16777c478bd9Sstevel@tonic-gate 				tabptr->zone_fs_options = next;
16787c478bd9Sstevel@tonic-gate 			else
16797c478bd9Sstevel@tonic-gate 				last->zone_fsopt_next = next;
16807c478bd9Sstevel@tonic-gate 			free(this);
16817c478bd9Sstevel@tonic-gate 			return (Z_OK);
16827c478bd9Sstevel@tonic-gate 		} else
16837c478bd9Sstevel@tonic-gate 			last = this;
16847c478bd9Sstevel@tonic-gate 	}
16857c478bd9Sstevel@tonic-gate 	return (Z_NO_PROPERTY_ID);
16867c478bd9Sstevel@tonic-gate }
16877c478bd9Sstevel@tonic-gate 
16887c478bd9Sstevel@tonic-gate void
16897c478bd9Sstevel@tonic-gate zonecfg_free_fs_option_list(zone_fsopt_t *list)
16907c478bd9Sstevel@tonic-gate {
16917c478bd9Sstevel@tonic-gate 	zone_fsopt_t *this, *next;
16927c478bd9Sstevel@tonic-gate 
16937c478bd9Sstevel@tonic-gate 	for (this = list; this != NULL; this = next) {
16947c478bd9Sstevel@tonic-gate 		next = this->zone_fsopt_next;
16957c478bd9Sstevel@tonic-gate 		free(this);
16967c478bd9Sstevel@tonic-gate 	}
16977c478bd9Sstevel@tonic-gate }
16987c478bd9Sstevel@tonic-gate 
16997c478bd9Sstevel@tonic-gate void
17007c478bd9Sstevel@tonic-gate zonecfg_free_rctl_value_list(struct zone_rctlvaltab *valtab)
17017c478bd9Sstevel@tonic-gate {
17027c478bd9Sstevel@tonic-gate 	if (valtab == NULL)
17037c478bd9Sstevel@tonic-gate 		return;
17047c478bd9Sstevel@tonic-gate 	zonecfg_free_rctl_value_list(valtab->zone_rctlval_next);
17057c478bd9Sstevel@tonic-gate 	free(valtab);
17067c478bd9Sstevel@tonic-gate }
17077c478bd9Sstevel@tonic-gate 
17087c478bd9Sstevel@tonic-gate static boolean_t
17097c478bd9Sstevel@tonic-gate match_prop(xmlNodePtr cur, const xmlChar *attr, char *user_prop)
17107c478bd9Sstevel@tonic-gate {
17117c478bd9Sstevel@tonic-gate 	xmlChar *gotten_prop;
17127c478bd9Sstevel@tonic-gate 	int prop_result;
17137c478bd9Sstevel@tonic-gate 
17147c478bd9Sstevel@tonic-gate 	gotten_prop = xmlGetProp(cur, attr);
17157c478bd9Sstevel@tonic-gate 	if (gotten_prop == NULL)	/* shouldn't happen */
17167c478bd9Sstevel@tonic-gate 		return (B_FALSE);
17177c478bd9Sstevel@tonic-gate 	prop_result = xmlStrcmp(gotten_prop, (const xmlChar *) user_prop);
17187c478bd9Sstevel@tonic-gate 	xmlFree(gotten_prop);
17197c478bd9Sstevel@tonic-gate 	return ((prop_result == 0));
17207c478bd9Sstevel@tonic-gate }
17217c478bd9Sstevel@tonic-gate 
17227c478bd9Sstevel@tonic-gate static int
17237c478bd9Sstevel@tonic-gate zonecfg_delete_filesystem_core(zone_dochandle_t handle,
17247c478bd9Sstevel@tonic-gate     struct zone_fstab *tabptr)
17257c478bd9Sstevel@tonic-gate {
17267c478bd9Sstevel@tonic-gate 	xmlNodePtr cur = handle->zone_dh_cur;
17277c478bd9Sstevel@tonic-gate 	boolean_t dir_match, spec_match, raw_match, type_match;
17287c478bd9Sstevel@tonic-gate 
17297c478bd9Sstevel@tonic-gate 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
17307c478bd9Sstevel@tonic-gate 		if (xmlStrcmp(cur->name, DTD_ELEM_FS))
17317c478bd9Sstevel@tonic-gate 			continue;
17327c478bd9Sstevel@tonic-gate 		dir_match = match_prop(cur, DTD_ATTR_DIR, tabptr->zone_fs_dir);
17337c478bd9Sstevel@tonic-gate 		spec_match = match_prop(cur, DTD_ATTR_SPECIAL,
17347c478bd9Sstevel@tonic-gate 		    tabptr->zone_fs_special);
17357c478bd9Sstevel@tonic-gate 		raw_match = match_prop(cur, DTD_ATTR_RAW,
17367c478bd9Sstevel@tonic-gate 		    tabptr->zone_fs_raw);
17377c478bd9Sstevel@tonic-gate 		type_match = match_prop(cur, DTD_ATTR_TYPE,
17387c478bd9Sstevel@tonic-gate 		    tabptr->zone_fs_type);
17397c478bd9Sstevel@tonic-gate 		if (dir_match && spec_match && raw_match && type_match) {
17407c478bd9Sstevel@tonic-gate 			xmlUnlinkNode(cur);
17417c478bd9Sstevel@tonic-gate 			xmlFreeNode(cur);
17427c478bd9Sstevel@tonic-gate 			return (Z_OK);
17437c478bd9Sstevel@tonic-gate 		}
17447c478bd9Sstevel@tonic-gate 	}
17457c478bd9Sstevel@tonic-gate 	return (Z_NO_RESOURCE_ID);
17467c478bd9Sstevel@tonic-gate }
17477c478bd9Sstevel@tonic-gate 
17487c478bd9Sstevel@tonic-gate int
17497c478bd9Sstevel@tonic-gate zonecfg_delete_filesystem(zone_dochandle_t handle, struct zone_fstab *tabptr)
17507c478bd9Sstevel@tonic-gate {
17517c478bd9Sstevel@tonic-gate 	int err;
17527c478bd9Sstevel@tonic-gate 
17537c478bd9Sstevel@tonic-gate 	if (tabptr == NULL)
17547c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
17557c478bd9Sstevel@tonic-gate 
17567c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
17577c478bd9Sstevel@tonic-gate 		return (err);
17587c478bd9Sstevel@tonic-gate 
17597c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_delete_filesystem_core(handle, tabptr)) != Z_OK)
17607c478bd9Sstevel@tonic-gate 		return (err);
17617c478bd9Sstevel@tonic-gate 
17627c478bd9Sstevel@tonic-gate 	return (Z_OK);
17637c478bd9Sstevel@tonic-gate }
17647c478bd9Sstevel@tonic-gate 
17657c478bd9Sstevel@tonic-gate int
17667c478bd9Sstevel@tonic-gate zonecfg_modify_filesystem(
17677c478bd9Sstevel@tonic-gate 	zone_dochandle_t handle,
17687c478bd9Sstevel@tonic-gate 	struct zone_fstab *oldtabptr,
17697c478bd9Sstevel@tonic-gate 	struct zone_fstab *newtabptr)
17707c478bd9Sstevel@tonic-gate {
17717c478bd9Sstevel@tonic-gate 	int err;
17727c478bd9Sstevel@tonic-gate 
17737c478bd9Sstevel@tonic-gate 	if (oldtabptr == NULL || newtabptr == NULL)
17747c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
17757c478bd9Sstevel@tonic-gate 
17767c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
17777c478bd9Sstevel@tonic-gate 		return (err);
17787c478bd9Sstevel@tonic-gate 
17797c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_delete_filesystem_core(handle, oldtabptr)) != Z_OK)
17807c478bd9Sstevel@tonic-gate 		return (err);
17817c478bd9Sstevel@tonic-gate 
17827c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_add_filesystem_core(handle, newtabptr)) != Z_OK)
17837c478bd9Sstevel@tonic-gate 		return (err);
17847c478bd9Sstevel@tonic-gate 
17857c478bd9Sstevel@tonic-gate 	return (Z_OK);
17867c478bd9Sstevel@tonic-gate }
17877c478bd9Sstevel@tonic-gate 
17887c478bd9Sstevel@tonic-gate static int
17897c478bd9Sstevel@tonic-gate zonecfg_delete_ipd_core(zone_dochandle_t handle, struct zone_fstab *tabptr)
17907c478bd9Sstevel@tonic-gate {
17917c478bd9Sstevel@tonic-gate 	xmlNodePtr cur = handle->zone_dh_cur;
17927c478bd9Sstevel@tonic-gate 
17937c478bd9Sstevel@tonic-gate 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
17947c478bd9Sstevel@tonic-gate 		if (xmlStrcmp(cur->name, DTD_ELEM_IPD))
17957c478bd9Sstevel@tonic-gate 			continue;
17967c478bd9Sstevel@tonic-gate 		if (match_prop(cur, DTD_ATTR_DIR, tabptr->zone_fs_dir)) {
17977c478bd9Sstevel@tonic-gate 			xmlUnlinkNode(cur);
17987c478bd9Sstevel@tonic-gate 			xmlFreeNode(cur);
17997c478bd9Sstevel@tonic-gate 			return (Z_OK);
18007c478bd9Sstevel@tonic-gate 		}
18017c478bd9Sstevel@tonic-gate 	}
18027c478bd9Sstevel@tonic-gate 	return (Z_NO_RESOURCE_ID);
18037c478bd9Sstevel@tonic-gate }
18047c478bd9Sstevel@tonic-gate 
18057c478bd9Sstevel@tonic-gate int
18067c478bd9Sstevel@tonic-gate zonecfg_delete_ipd(zone_dochandle_t handle, struct zone_fstab *tabptr)
18077c478bd9Sstevel@tonic-gate {
18087c478bd9Sstevel@tonic-gate 	int err;
18097c478bd9Sstevel@tonic-gate 
18107c478bd9Sstevel@tonic-gate 	if (tabptr == NULL)
18117c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
18127c478bd9Sstevel@tonic-gate 
18137c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
18147c478bd9Sstevel@tonic-gate 		return (err);
18157c478bd9Sstevel@tonic-gate 
18167c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_delete_ipd_core(handle, tabptr)) != Z_OK)
18177c478bd9Sstevel@tonic-gate 		return (err);
18187c478bd9Sstevel@tonic-gate 
18197c478bd9Sstevel@tonic-gate 	return (Z_OK);
18207c478bd9Sstevel@tonic-gate }
18217c478bd9Sstevel@tonic-gate 
18227c478bd9Sstevel@tonic-gate int
18237c478bd9Sstevel@tonic-gate zonecfg_modify_ipd(zone_dochandle_t handle, struct zone_fstab *oldtabptr,
18247c478bd9Sstevel@tonic-gate     struct zone_fstab *newtabptr)
18257c478bd9Sstevel@tonic-gate {
18267c478bd9Sstevel@tonic-gate 	int err;
18277c478bd9Sstevel@tonic-gate 
18287c478bd9Sstevel@tonic-gate 	if (oldtabptr == NULL || newtabptr == NULL)
18297c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
18307c478bd9Sstevel@tonic-gate 
18317c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
18327c478bd9Sstevel@tonic-gate 		return (err);
18337c478bd9Sstevel@tonic-gate 
18347c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_delete_ipd_core(handle, oldtabptr)) != Z_OK)
18357c478bd9Sstevel@tonic-gate 		return (err);
18367c478bd9Sstevel@tonic-gate 
18377c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_add_ipd_core(handle, newtabptr)) != Z_OK)
18387c478bd9Sstevel@tonic-gate 		return (err);
18397c478bd9Sstevel@tonic-gate 
18407c478bd9Sstevel@tonic-gate 	return (Z_OK);
18417c478bd9Sstevel@tonic-gate }
18427c478bd9Sstevel@tonic-gate 
18437c478bd9Sstevel@tonic-gate int
18447c478bd9Sstevel@tonic-gate zonecfg_lookup_filesystem(
18457c478bd9Sstevel@tonic-gate 	zone_dochandle_t handle,
18467c478bd9Sstevel@tonic-gate 	struct zone_fstab *tabptr)
18477c478bd9Sstevel@tonic-gate {
18487c478bd9Sstevel@tonic-gate 	xmlNodePtr cur, options, firstmatch;
18497c478bd9Sstevel@tonic-gate 	int err;
18507c478bd9Sstevel@tonic-gate 	char dirname[MAXPATHLEN], special[MAXPATHLEN], raw[MAXPATHLEN];
18517c478bd9Sstevel@tonic-gate 	char type[FSTYPSZ];
18527c478bd9Sstevel@tonic-gate 	char options_str[MAX_MNTOPT_STR];
18537c478bd9Sstevel@tonic-gate 
18547c478bd9Sstevel@tonic-gate 	if (tabptr == NULL)
18557c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
18567c478bd9Sstevel@tonic-gate 
18577c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
18587c478bd9Sstevel@tonic-gate 		return (err);
18597c478bd9Sstevel@tonic-gate 
18607c478bd9Sstevel@tonic-gate 	/*
18617c478bd9Sstevel@tonic-gate 	 * Walk the list of children looking for matches on any properties
18627c478bd9Sstevel@tonic-gate 	 * specified in the fstab parameter.  If more than one resource
18637c478bd9Sstevel@tonic-gate 	 * matches, we return Z_INSUFFICIENT_SPEC; if none match, we return
18647c478bd9Sstevel@tonic-gate 	 * Z_NO_RESOURCE_ID.
18657c478bd9Sstevel@tonic-gate 	 */
18667c478bd9Sstevel@tonic-gate 	cur = handle->zone_dh_cur;
18677c478bd9Sstevel@tonic-gate 	firstmatch = NULL;
18687c478bd9Sstevel@tonic-gate 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
18697c478bd9Sstevel@tonic-gate 		if (xmlStrcmp(cur->name, DTD_ELEM_FS))
18707c478bd9Sstevel@tonic-gate 			continue;
18717c478bd9Sstevel@tonic-gate 		if (strlen(tabptr->zone_fs_dir) > 0) {
18727c478bd9Sstevel@tonic-gate 			if ((fetchprop(cur, DTD_ATTR_DIR, dirname,
18737c478bd9Sstevel@tonic-gate 			    sizeof (dirname)) == Z_OK) &&
18747c478bd9Sstevel@tonic-gate 			    (strcmp(tabptr->zone_fs_dir, dirname) == 0)) {
18757c478bd9Sstevel@tonic-gate 				if (firstmatch == NULL)
18767c478bd9Sstevel@tonic-gate 					firstmatch = cur;
18777c478bd9Sstevel@tonic-gate 				else
18787c478bd9Sstevel@tonic-gate 					return (Z_INSUFFICIENT_SPEC);
18797c478bd9Sstevel@tonic-gate 			}
18807c478bd9Sstevel@tonic-gate 		}
18817c478bd9Sstevel@tonic-gate 		if (strlen(tabptr->zone_fs_special) > 0) {
18827c478bd9Sstevel@tonic-gate 			if ((fetchprop(cur, DTD_ATTR_SPECIAL, special,
18837c478bd9Sstevel@tonic-gate 			    sizeof (special)) == Z_OK)) {
18847c478bd9Sstevel@tonic-gate 				if (strcmp(tabptr->zone_fs_special,
18857c478bd9Sstevel@tonic-gate 				    special) == 0) {
18867c478bd9Sstevel@tonic-gate 					if (firstmatch == NULL)
18877c478bd9Sstevel@tonic-gate 						firstmatch = cur;
18887c478bd9Sstevel@tonic-gate 					else if (firstmatch != cur)
18897c478bd9Sstevel@tonic-gate 						return (Z_INSUFFICIENT_SPEC);
18907c478bd9Sstevel@tonic-gate 				} else {
18917c478bd9Sstevel@tonic-gate 					/*
18927c478bd9Sstevel@tonic-gate 					 * If another property matched but this
18937c478bd9Sstevel@tonic-gate 					 * one doesn't then reset firstmatch.
18947c478bd9Sstevel@tonic-gate 					 */
18957c478bd9Sstevel@tonic-gate 					if (firstmatch == cur)
18967c478bd9Sstevel@tonic-gate 						firstmatch = NULL;
18977c478bd9Sstevel@tonic-gate 				}
18987c478bd9Sstevel@tonic-gate 			}
18997c478bd9Sstevel@tonic-gate 		}
19007c478bd9Sstevel@tonic-gate 		if (strlen(tabptr->zone_fs_raw) > 0) {
19017c478bd9Sstevel@tonic-gate 			if ((fetchprop(cur, DTD_ATTR_RAW, raw,
19027c478bd9Sstevel@tonic-gate 			    sizeof (raw)) == Z_OK)) {
19037c478bd9Sstevel@tonic-gate 				if (strcmp(tabptr->zone_fs_raw, raw) == 0) {
19047c478bd9Sstevel@tonic-gate 					if (firstmatch == NULL)
19057c478bd9Sstevel@tonic-gate 						firstmatch = cur;
19067c478bd9Sstevel@tonic-gate 					else if (firstmatch != cur)
19077c478bd9Sstevel@tonic-gate 						return (Z_INSUFFICIENT_SPEC);
19087c478bd9Sstevel@tonic-gate 				} else {
19097c478bd9Sstevel@tonic-gate 					/*
19107c478bd9Sstevel@tonic-gate 					 * If another property matched but this
19117c478bd9Sstevel@tonic-gate 					 * one doesn't then reset firstmatch.
19127c478bd9Sstevel@tonic-gate 					 */
19137c478bd9Sstevel@tonic-gate 					if (firstmatch == cur)
19147c478bd9Sstevel@tonic-gate 						firstmatch = NULL;
19157c478bd9Sstevel@tonic-gate 				}
19167c478bd9Sstevel@tonic-gate 			}
19177c478bd9Sstevel@tonic-gate 		}
19187c478bd9Sstevel@tonic-gate 		if (strlen(tabptr->zone_fs_type) > 0) {
19197c478bd9Sstevel@tonic-gate 			if ((fetchprop(cur, DTD_ATTR_TYPE, type,
19207c478bd9Sstevel@tonic-gate 			    sizeof (type)) == Z_OK)) {
19217c478bd9Sstevel@tonic-gate 				if (strcmp(tabptr->zone_fs_type, type) == 0) {
19227c478bd9Sstevel@tonic-gate 					if (firstmatch == NULL)
19237c478bd9Sstevel@tonic-gate 						firstmatch = cur;
19247c478bd9Sstevel@tonic-gate 					else if (firstmatch != cur)
19257c478bd9Sstevel@tonic-gate 						return (Z_INSUFFICIENT_SPEC);
19267c478bd9Sstevel@tonic-gate 				} else {
19277c478bd9Sstevel@tonic-gate 					/*
19287c478bd9Sstevel@tonic-gate 					 * If another property matched but this
19297c478bd9Sstevel@tonic-gate 					 * one doesn't then reset firstmatch.
19307c478bd9Sstevel@tonic-gate 					 */
19317c478bd9Sstevel@tonic-gate 					if (firstmatch == cur)
19327c478bd9Sstevel@tonic-gate 						firstmatch = NULL;
19337c478bd9Sstevel@tonic-gate 				}
19347c478bd9Sstevel@tonic-gate 			}
19357c478bd9Sstevel@tonic-gate 		}
19367c478bd9Sstevel@tonic-gate 	}
19377c478bd9Sstevel@tonic-gate 
19387c478bd9Sstevel@tonic-gate 	if (firstmatch == NULL)
19397c478bd9Sstevel@tonic-gate 		return (Z_NO_RESOURCE_ID);
19407c478bd9Sstevel@tonic-gate 
19417c478bd9Sstevel@tonic-gate 	cur = firstmatch;
19427c478bd9Sstevel@tonic-gate 
19437c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_DIR, tabptr->zone_fs_dir,
19447c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_fs_dir))) != Z_OK)
19457c478bd9Sstevel@tonic-gate 		return (err);
19467c478bd9Sstevel@tonic-gate 
19477c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_SPECIAL, tabptr->zone_fs_special,
19487c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_fs_special))) != Z_OK)
19497c478bd9Sstevel@tonic-gate 		return (err);
19507c478bd9Sstevel@tonic-gate 
19517c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_RAW, tabptr->zone_fs_raw,
19527c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_fs_raw))) != Z_OK)
19537c478bd9Sstevel@tonic-gate 		return (err);
19547c478bd9Sstevel@tonic-gate 
19557c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_TYPE, tabptr->zone_fs_type,
19567c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_fs_type))) != Z_OK)
19577c478bd9Sstevel@tonic-gate 		return (err);
19587c478bd9Sstevel@tonic-gate 
19597c478bd9Sstevel@tonic-gate 	/* options are optional */
19607c478bd9Sstevel@tonic-gate 	tabptr->zone_fs_options = NULL;
19617c478bd9Sstevel@tonic-gate 	for (options = cur->xmlChildrenNode; options != NULL;
19627c478bd9Sstevel@tonic-gate 	    options = options->next) {
19637c478bd9Sstevel@tonic-gate 		if ((fetchprop(options, DTD_ATTR_NAME, options_str,
19647c478bd9Sstevel@tonic-gate 		    sizeof (options_str)) != Z_OK))
19657c478bd9Sstevel@tonic-gate 			break;
19667c478bd9Sstevel@tonic-gate 		if (zonecfg_add_fs_option(tabptr, options_str) != Z_OK)
19677c478bd9Sstevel@tonic-gate 			break;
19687c478bd9Sstevel@tonic-gate 	}
19697c478bd9Sstevel@tonic-gate 	return (Z_OK);
19707c478bd9Sstevel@tonic-gate }
19717c478bd9Sstevel@tonic-gate 
19727c478bd9Sstevel@tonic-gate int
19737c478bd9Sstevel@tonic-gate zonecfg_lookup_ipd(zone_dochandle_t handle, struct zone_fstab *tabptr)
19747c478bd9Sstevel@tonic-gate {
19757c478bd9Sstevel@tonic-gate 	xmlNodePtr cur, match;
19767c478bd9Sstevel@tonic-gate 	int err;
19777c478bd9Sstevel@tonic-gate 	char dirname[MAXPATHLEN];
19787c478bd9Sstevel@tonic-gate 
19797c478bd9Sstevel@tonic-gate 	if (tabptr == NULL)
19807c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
19817c478bd9Sstevel@tonic-gate 
19827c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
19837c478bd9Sstevel@tonic-gate 		return (err);
19847c478bd9Sstevel@tonic-gate 
19857c478bd9Sstevel@tonic-gate 	/*
19867c478bd9Sstevel@tonic-gate 	 * General algorithm:
19877c478bd9Sstevel@tonic-gate 	 * Walk the list of children looking for matches on any properties
19887c478bd9Sstevel@tonic-gate 	 * specified in the fstab parameter.  If more than one resource
19897c478bd9Sstevel@tonic-gate 	 * matches, we return Z_INSUFFICIENT_SPEC; if none match, we return
19907c478bd9Sstevel@tonic-gate 	 * Z_NO_RESOURCE_ID.
19917c478bd9Sstevel@tonic-gate 	 */
19927c478bd9Sstevel@tonic-gate 	cur = handle->zone_dh_cur;
19937c478bd9Sstevel@tonic-gate 	match = NULL;
19947c478bd9Sstevel@tonic-gate 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
19957c478bd9Sstevel@tonic-gate 		if (xmlStrcmp(cur->name, DTD_ELEM_IPD))
19967c478bd9Sstevel@tonic-gate 			continue;
19977c478bd9Sstevel@tonic-gate 		if (strlen(tabptr->zone_fs_dir) > 0) {
19987c478bd9Sstevel@tonic-gate 			if ((fetchprop(cur, DTD_ATTR_DIR, dirname,
19997c478bd9Sstevel@tonic-gate 			    sizeof (dirname)) == Z_OK) &&
20007c478bd9Sstevel@tonic-gate 			    (strcmp(tabptr->zone_fs_dir, dirname) == 0)) {
20017c478bd9Sstevel@tonic-gate 				if (match == NULL)
20027c478bd9Sstevel@tonic-gate 					match = cur;
20037c478bd9Sstevel@tonic-gate 				else
20047c478bd9Sstevel@tonic-gate 					return (Z_INSUFFICIENT_SPEC);
20057c478bd9Sstevel@tonic-gate 			}
20067c478bd9Sstevel@tonic-gate 		}
20077c478bd9Sstevel@tonic-gate 	}
20087c478bd9Sstevel@tonic-gate 
20097c478bd9Sstevel@tonic-gate 	if (match == NULL)
20107c478bd9Sstevel@tonic-gate 		return (Z_NO_RESOURCE_ID);
20117c478bd9Sstevel@tonic-gate 
20127c478bd9Sstevel@tonic-gate 	cur = match;
20137c478bd9Sstevel@tonic-gate 
20147c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_DIR, tabptr->zone_fs_dir,
20157c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_fs_dir))) != Z_OK)
20167c478bd9Sstevel@tonic-gate 		return (err);
20177c478bd9Sstevel@tonic-gate 
20187c478bd9Sstevel@tonic-gate 	return (Z_OK);
20197c478bd9Sstevel@tonic-gate }
20207c478bd9Sstevel@tonic-gate 
20217c478bd9Sstevel@tonic-gate /*
20227c478bd9Sstevel@tonic-gate  * Compare two IP addresses in string form.  Allow for the possibility that
20237c478bd9Sstevel@tonic-gate  * one might have "/<prefix-length>" at the end: allow a match on just the
20247c478bd9Sstevel@tonic-gate  * IP address (or host name) part.
20257c478bd9Sstevel@tonic-gate  */
20267c478bd9Sstevel@tonic-gate 
20277c478bd9Sstevel@tonic-gate boolean_t
20287c478bd9Sstevel@tonic-gate zonecfg_same_net_address(char *a1, char *a2)
20297c478bd9Sstevel@tonic-gate {
20307c478bd9Sstevel@tonic-gate 	char *slashp, *slashp1, *slashp2;
20317c478bd9Sstevel@tonic-gate 	int result;
20327c478bd9Sstevel@tonic-gate 
20337c478bd9Sstevel@tonic-gate 	if (strcmp(a1, a2) == 0)
20347c478bd9Sstevel@tonic-gate 		return (B_TRUE);
20357c478bd9Sstevel@tonic-gate 
20367c478bd9Sstevel@tonic-gate 	/*
20377c478bd9Sstevel@tonic-gate 	 * If neither has a slash or both do, they need to match to be
20387c478bd9Sstevel@tonic-gate 	 * considered the same, but they did not match above, so fail.
20397c478bd9Sstevel@tonic-gate 	 */
20407c478bd9Sstevel@tonic-gate 	slashp1 = strchr(a1, '/');
20417c478bd9Sstevel@tonic-gate 	slashp2 = strchr(a2, '/');
20427c478bd9Sstevel@tonic-gate 	if ((slashp1 == NULL && slashp2 == NULL) ||
20437c478bd9Sstevel@tonic-gate 	    (slashp1 != NULL && slashp2 != NULL))
20447c478bd9Sstevel@tonic-gate 		return (B_FALSE);
20457c478bd9Sstevel@tonic-gate 
20467c478bd9Sstevel@tonic-gate 	/*
20477c478bd9Sstevel@tonic-gate 	 * Only one had a slash: pick that one, zero out the slash, compare
20487c478bd9Sstevel@tonic-gate 	 * the "address only" strings, restore the slash, and return the
20497c478bd9Sstevel@tonic-gate 	 * result of the comparison.
20507c478bd9Sstevel@tonic-gate 	 */
20517c478bd9Sstevel@tonic-gate 	slashp = (slashp1 == NULL) ? slashp2 : slashp1;
20527c478bd9Sstevel@tonic-gate 	*slashp = '\0';
20537c478bd9Sstevel@tonic-gate 	result = strcmp(a1, a2);
20547c478bd9Sstevel@tonic-gate 	*slashp = '/';
20557c478bd9Sstevel@tonic-gate 	return ((result == 0));
20567c478bd9Sstevel@tonic-gate }
20577c478bd9Sstevel@tonic-gate 
20587c478bd9Sstevel@tonic-gate int
20597c478bd9Sstevel@tonic-gate zonecfg_valid_net_address(char *address, struct lifreq *lifr)
20607c478bd9Sstevel@tonic-gate {
20617c478bd9Sstevel@tonic-gate 	struct sockaddr_in *sin4;
20627c478bd9Sstevel@tonic-gate 	struct sockaddr_in6 *sin6;
20637c478bd9Sstevel@tonic-gate 	struct addrinfo hints, *result;
20647c478bd9Sstevel@tonic-gate 	char *slashp = strchr(address, '/');
20657c478bd9Sstevel@tonic-gate 
20667c478bd9Sstevel@tonic-gate 	bzero(lifr, sizeof (struct lifreq));
20677c478bd9Sstevel@tonic-gate 	sin4 = (struct sockaddr_in *)&lifr->lifr_addr;
20687c478bd9Sstevel@tonic-gate 	sin6 = (struct sockaddr_in6 *)&lifr->lifr_addr;
20697c478bd9Sstevel@tonic-gate 	if (slashp != NULL)
20707c478bd9Sstevel@tonic-gate 		*slashp = '\0';
20717c478bd9Sstevel@tonic-gate 	if (inet_pton(AF_INET, address, &sin4->sin_addr) == 1) {
20727c478bd9Sstevel@tonic-gate 		sin4->sin_family = AF_INET;
20737c478bd9Sstevel@tonic-gate 	} else if (inet_pton(AF_INET6, address, &sin6->sin6_addr) == 1) {
20747c478bd9Sstevel@tonic-gate 		if (slashp == NULL)
20757c478bd9Sstevel@tonic-gate 			return (Z_IPV6_ADDR_PREFIX_LEN);
20767c478bd9Sstevel@tonic-gate 		sin6->sin6_family = AF_INET6;
20777c478bd9Sstevel@tonic-gate 	} else {
20787c478bd9Sstevel@tonic-gate 		/* "address" may be a host name */
20797c478bd9Sstevel@tonic-gate 		(void) memset(&hints, 0, sizeof (hints));
20807c478bd9Sstevel@tonic-gate 		hints.ai_family = PF_INET;
20817c478bd9Sstevel@tonic-gate 		if (getaddrinfo(address, NULL, &hints, &result) != 0)
20827c478bd9Sstevel@tonic-gate 			return (Z_BOGUS_ADDRESS);
20837c478bd9Sstevel@tonic-gate 		sin4->sin_family = result->ai_family;
2084a1be23daSdp 
20857c478bd9Sstevel@tonic-gate 		(void) memcpy(&sin4->sin_addr,
20867c478bd9Sstevel@tonic-gate 		    /* LINTED E_BAD_PTR_CAST_ALIGN */
20877c478bd9Sstevel@tonic-gate 		    &((struct sockaddr_in *)result->ai_addr)->sin_addr,
20887c478bd9Sstevel@tonic-gate 		    sizeof (struct in_addr));
2089a1be23daSdp 
20907c478bd9Sstevel@tonic-gate 		freeaddrinfo(result);
20917c478bd9Sstevel@tonic-gate 	}
20927c478bd9Sstevel@tonic-gate 	return (Z_OK);
20937c478bd9Sstevel@tonic-gate }
20947c478bd9Sstevel@tonic-gate 
2095f4b3ec61Sdh155122 boolean_t
2096f4b3ec61Sdh155122 zonecfg_ifname_exists(sa_family_t af, char *ifname)
2097f4b3ec61Sdh155122 {
2098f4b3ec61Sdh155122 	struct lifreq lifr;
2099f4b3ec61Sdh155122 	int so;
2100f4b3ec61Sdh155122 	int save_errno;
2101f4b3ec61Sdh155122 
2102f4b3ec61Sdh155122 	(void) memset(&lifr, 0, sizeof (lifr));
2103f4b3ec61Sdh155122 	(void) strlcpy(lifr.lifr_name, ifname, sizeof (lifr.lifr_name));
2104f4b3ec61Sdh155122 	lifr.lifr_addr.ss_family = af;
2105f4b3ec61Sdh155122 	if ((so = socket(af, SOCK_DGRAM, 0)) < 0) {
2106f4b3ec61Sdh155122 		/* Odd - can't tell if the ifname exists */
2107f4b3ec61Sdh155122 		return (B_FALSE);
2108f4b3ec61Sdh155122 	}
2109f4b3ec61Sdh155122 	if (ioctl(so, SIOCGLIFFLAGS, (caddr_t)&lifr) < 0) {
2110f4b3ec61Sdh155122 		save_errno = errno;
2111f4b3ec61Sdh155122 		(void) close(so);
2112f4b3ec61Sdh155122 		errno = save_errno;
2113f4b3ec61Sdh155122 		return (B_FALSE);
2114f4b3ec61Sdh155122 	}
2115f4b3ec61Sdh155122 	(void) close(so);
2116f4b3ec61Sdh155122 	return (B_TRUE);
2117f4b3ec61Sdh155122 }
2118f4b3ec61Sdh155122 
2119a8053275Sjv227347 /*
2120a8053275Sjv227347  * Determines if the physical interface and IP address specified by 'tabptr'
2121a8053275Sjv227347  * are in the zone document to which 'handle' refers.  'tabptr' must have an
2122a8053275Sjv227347  * interface or an address or both.  If it contains both, then Z_OK is
2123a8053275Sjv227347  * returned iff there is exactly one match.  If it contains an interface
2124a8053275Sjv227347  * or an address, but not both, then Z_OK is returned iff there is exactly
2125a8053275Sjv227347  * one entry with that interface or address.  If there are multiple entries
2126a8053275Sjv227347  * matching the query, then Z_INSUFFICIENT_SPEC is returned.  If there
2127a8053275Sjv227347  * are no matches, then Z_NO_RESOURCE_ID is returned.
2128a8053275Sjv227347  *
2129a8053275Sjv227347  * Errors might also be returned if the entry that exactly matches the
2130a8053275Sjv227347  * query lacks critical network resource information.
2131a8053275Sjv227347  *
2132a8053275Sjv227347  * If there is a single exact match, then the matching entry's physical
2133a8053275Sjv227347  * interface, IP address, and router information is stored in 'tabptr'.
2134a8053275Sjv227347  */
21357c478bd9Sstevel@tonic-gate int
21367c478bd9Sstevel@tonic-gate zonecfg_lookup_nwif(zone_dochandle_t handle, struct zone_nwiftab *tabptr)
21377c478bd9Sstevel@tonic-gate {
2138a8053275Sjv227347 	xmlNodePtr cur;
2139a8053275Sjv227347 	xmlNodePtr firstmatch;
2140a8053275Sjv227347 	boolean_t physfound;
21417c478bd9Sstevel@tonic-gate 	int err;
2142a8053275Sjv227347 	char address[INET6_ADDRSTRLEN];
2143a8053275Sjv227347 	char physical[LIFNAMSIZ];
2144a8053275Sjv227347 	size_t addrspec;		/* nonzero if tabptr has IP addr */
2145a8053275Sjv227347 	size_t physspec;		/* nonzero if tabptr has interface */
21467c478bd9Sstevel@tonic-gate 
21477c478bd9Sstevel@tonic-gate 	if (tabptr == NULL)
21487c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
21497c478bd9Sstevel@tonic-gate 
2150a8053275Sjv227347 	/*
2151a8053275Sjv227347 	 * zone_nwif_address and zone_nwif_physical are arrays, so no NULL
2152a8053275Sjv227347 	 * checks are necessary.
2153a8053275Sjv227347 	 */
2154a8053275Sjv227347 	addrspec = strlen(tabptr->zone_nwif_address);
2155a8053275Sjv227347 	physspec = strlen(tabptr->zone_nwif_physical);
2156a8053275Sjv227347 	assert(addrspec > 0 || physspec > 0);
2157a8053275Sjv227347 
21587c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
21597c478bd9Sstevel@tonic-gate 		return (err);
21607c478bd9Sstevel@tonic-gate 
21617c478bd9Sstevel@tonic-gate 	firstmatch = NULL;
2162a8053275Sjv227347 	cur = handle->zone_dh_cur;
21637c478bd9Sstevel@tonic-gate 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
2164a8053275Sjv227347 		/* Skip non-net elements */
21657c478bd9Sstevel@tonic-gate 		if (xmlStrcmp(cur->name, DTD_ELEM_NET))
21667c478bd9Sstevel@tonic-gate 			continue;
2167a8053275Sjv227347 
2168a8053275Sjv227347 		/*
2169a8053275Sjv227347 		 * If an interface is specified, then first check if the current
2170a8053275Sjv227347 		 * element's interface matches the query's interface.
2171a8053275Sjv227347 		 */
2172a8053275Sjv227347 		if (physspec > 0) {
2173a8053275Sjv227347 			physfound = B_FALSE;
21747c478bd9Sstevel@tonic-gate 			if ((fetchprop(cur, DTD_ATTR_PHYSICAL, physical,
21757c478bd9Sstevel@tonic-gate 			    sizeof (physical)) == Z_OK) &&
21767c478bd9Sstevel@tonic-gate 			    (strcmp(tabptr->zone_nwif_physical,
21777c478bd9Sstevel@tonic-gate 			    physical) == 0)) {
2178a8053275Sjv227347 				if (addrspec == 0) {
21797c478bd9Sstevel@tonic-gate 					if (firstmatch == NULL)
21807c478bd9Sstevel@tonic-gate 						firstmatch = cur;
21817c478bd9Sstevel@tonic-gate 					else
21827c478bd9Sstevel@tonic-gate 						return (Z_INSUFFICIENT_SPEC);
21837c478bd9Sstevel@tonic-gate 				} else {
21847c478bd9Sstevel@tonic-gate 					/*
2185a8053275Sjv227347 					 * We're also matching based on IP
2186a8053275Sjv227347 					 * address, so we can't say that the
2187a8053275Sjv227347 					 * current element matches the query
2188a8053275Sjv227347 					 * yet.  Indicate that the interfaces
2189a8053275Sjv227347 					 * match.
21907c478bd9Sstevel@tonic-gate 					 */
2191a8053275Sjv227347 					physfound = B_TRUE;
2192a8053275Sjv227347 				}
2193a8053275Sjv227347 			}
2194a8053275Sjv227347 		}
2195a8053275Sjv227347 		if (addrspec > 0) {
2196a8053275Sjv227347 			if ((fetchprop(cur, DTD_ATTR_ADDRESS, address,
2197a8053275Sjv227347 			    sizeof (address)) == Z_OK) &&
2198a8053275Sjv227347 			    (zonecfg_same_net_address(
2199a8053275Sjv227347 			    tabptr->zone_nwif_address, address))) {
2200a8053275Sjv227347 				if (physspec == 0) {
2201a8053275Sjv227347 					/* We're only matching IP addresses. */
2202a8053275Sjv227347 					if (firstmatch == NULL)
2203a8053275Sjv227347 						firstmatch = cur;
2204a8053275Sjv227347 					else
2205a8053275Sjv227347 						return (Z_INSUFFICIENT_SPEC);
2206a8053275Sjv227347 				} else if (physfound) {
2207a8053275Sjv227347 					/*
2208a8053275Sjv227347 					 * Both the interfaces and the addresses
2209a8053275Sjv227347 					 * match.
2210a8053275Sjv227347 					 */
2211a8053275Sjv227347 					if (firstmatch == NULL)
2212a8053275Sjv227347 						firstmatch = cur;
2213a8053275Sjv227347 					else
2214a8053275Sjv227347 						return (Z_INSUFFICIENT_SPEC);
22157c478bd9Sstevel@tonic-gate 				}
22167c478bd9Sstevel@tonic-gate 			}
22177c478bd9Sstevel@tonic-gate 		}
22187c478bd9Sstevel@tonic-gate 	}
22197c478bd9Sstevel@tonic-gate 	if (firstmatch == NULL)
22207c478bd9Sstevel@tonic-gate 		return (Z_NO_RESOURCE_ID);
22217c478bd9Sstevel@tonic-gate 
22227c478bd9Sstevel@tonic-gate 	cur = firstmatch;
22237c478bd9Sstevel@tonic-gate 
22247c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_PHYSICAL, tabptr->zone_nwif_physical,
22257c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_nwif_physical))) != Z_OK)
22267c478bd9Sstevel@tonic-gate 		return (err);
22277c478bd9Sstevel@tonic-gate 
22287c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_ADDRESS, tabptr->zone_nwif_address,
22297c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_nwif_address))) != Z_OK)
22307c478bd9Sstevel@tonic-gate 		return (err);
22317c478bd9Sstevel@tonic-gate 
2232de860bd9Sgfaden 	if ((err = fetchprop(cur, DTD_ATTR_DEFROUTER,
2233de860bd9Sgfaden 	    tabptr->zone_nwif_defrouter,
2234de860bd9Sgfaden 	    sizeof (tabptr->zone_nwif_defrouter))) != Z_OK)
2235de860bd9Sgfaden 		return (err);
2236de860bd9Sgfaden 
22377c478bd9Sstevel@tonic-gate 	return (Z_OK);
22387c478bd9Sstevel@tonic-gate }
22397c478bd9Sstevel@tonic-gate 
22407c478bd9Sstevel@tonic-gate static int
22417c478bd9Sstevel@tonic-gate zonecfg_add_nwif_core(zone_dochandle_t handle, struct zone_nwiftab *tabptr)
22427c478bd9Sstevel@tonic-gate {
22437c478bd9Sstevel@tonic-gate 	xmlNodePtr newnode, cur = handle->zone_dh_cur;
22447c478bd9Sstevel@tonic-gate 	int err;
22457c478bd9Sstevel@tonic-gate 
22467c478bd9Sstevel@tonic-gate 	newnode = xmlNewTextChild(cur, NULL, DTD_ELEM_NET, NULL);
2247a1be23daSdp 	if ((err = newprop(newnode, DTD_ATTR_ADDRESS,
22487c478bd9Sstevel@tonic-gate 	    tabptr->zone_nwif_address)) != Z_OK)
22497c478bd9Sstevel@tonic-gate 		return (err);
2250a1be23daSdp 	if ((err = newprop(newnode, DTD_ATTR_PHYSICAL,
22517c478bd9Sstevel@tonic-gate 	    tabptr->zone_nwif_physical)) != Z_OK)
22527c478bd9Sstevel@tonic-gate 		return (err);
225344ce818bSjparcel 	/*
225444ce818bSjparcel 	 * Do not add this property when it is not set, for backwards
225544ce818bSjparcel 	 * compatibility and because it is optional.
225644ce818bSjparcel 	 */
225744ce818bSjparcel 	if ((strlen(tabptr->zone_nwif_defrouter) > 0) &&
225844ce818bSjparcel 	    ((err = newprop(newnode, DTD_ATTR_DEFROUTER,
225944ce818bSjparcel 	    tabptr->zone_nwif_defrouter)) != Z_OK))
2260de860bd9Sgfaden 		return (err);
22617c478bd9Sstevel@tonic-gate 	return (Z_OK);
22627c478bd9Sstevel@tonic-gate }
22637c478bd9Sstevel@tonic-gate 
22647c478bd9Sstevel@tonic-gate int
22657c478bd9Sstevel@tonic-gate zonecfg_add_nwif(zone_dochandle_t handle, struct zone_nwiftab *tabptr)
22667c478bd9Sstevel@tonic-gate {
22677c478bd9Sstevel@tonic-gate 	int err;
22687c478bd9Sstevel@tonic-gate 
22697c478bd9Sstevel@tonic-gate 	if (tabptr == NULL)
22707c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
22717c478bd9Sstevel@tonic-gate 
22727c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
22737c478bd9Sstevel@tonic-gate 		return (err);
22747c478bd9Sstevel@tonic-gate 
22757c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_add_nwif_core(handle, tabptr)) != Z_OK)
22767c478bd9Sstevel@tonic-gate 		return (err);
22777c478bd9Sstevel@tonic-gate 
22787c478bd9Sstevel@tonic-gate 	return (Z_OK);
22797c478bd9Sstevel@tonic-gate }
22807c478bd9Sstevel@tonic-gate 
22817c478bd9Sstevel@tonic-gate static int
22827c478bd9Sstevel@tonic-gate zonecfg_delete_nwif_core(zone_dochandle_t handle, struct zone_nwiftab *tabptr)
22837c478bd9Sstevel@tonic-gate {
22847c478bd9Sstevel@tonic-gate 	xmlNodePtr cur = handle->zone_dh_cur;
22857c478bd9Sstevel@tonic-gate 	boolean_t addr_match, phys_match;
22867c478bd9Sstevel@tonic-gate 
22877c478bd9Sstevel@tonic-gate 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
22887c478bd9Sstevel@tonic-gate 		if (xmlStrcmp(cur->name, DTD_ELEM_NET))
22897c478bd9Sstevel@tonic-gate 			continue;
22907c478bd9Sstevel@tonic-gate 
22917c478bd9Sstevel@tonic-gate 		addr_match = match_prop(cur, DTD_ATTR_ADDRESS,
22927c478bd9Sstevel@tonic-gate 		    tabptr->zone_nwif_address);
22937c478bd9Sstevel@tonic-gate 		phys_match = match_prop(cur, DTD_ATTR_PHYSICAL,
22947c478bd9Sstevel@tonic-gate 		    tabptr->zone_nwif_physical);
22957c478bd9Sstevel@tonic-gate 
22967c478bd9Sstevel@tonic-gate 		if (addr_match && phys_match) {
22977c478bd9Sstevel@tonic-gate 			xmlUnlinkNode(cur);
22987c478bd9Sstevel@tonic-gate 			xmlFreeNode(cur);
22997c478bd9Sstevel@tonic-gate 			return (Z_OK);
23007c478bd9Sstevel@tonic-gate 		}
23017c478bd9Sstevel@tonic-gate 	}
23027c478bd9Sstevel@tonic-gate 	return (Z_NO_RESOURCE_ID);
23037c478bd9Sstevel@tonic-gate }
23047c478bd9Sstevel@tonic-gate 
23057c478bd9Sstevel@tonic-gate int
23067c478bd9Sstevel@tonic-gate zonecfg_delete_nwif(zone_dochandle_t handle, struct zone_nwiftab *tabptr)
23077c478bd9Sstevel@tonic-gate {
23087c478bd9Sstevel@tonic-gate 	int err;
23097c478bd9Sstevel@tonic-gate 
23107c478bd9Sstevel@tonic-gate 	if (tabptr == NULL)
23117c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
23127c478bd9Sstevel@tonic-gate 
23137c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
23147c478bd9Sstevel@tonic-gate 		return (err);
23157c478bd9Sstevel@tonic-gate 
23167c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_delete_nwif_core(handle, tabptr)) != Z_OK)
23177c478bd9Sstevel@tonic-gate 		return (err);
23187c478bd9Sstevel@tonic-gate 
23197c478bd9Sstevel@tonic-gate 	return (Z_OK);
23207c478bd9Sstevel@tonic-gate }
23217c478bd9Sstevel@tonic-gate 
23227c478bd9Sstevel@tonic-gate int
23237c478bd9Sstevel@tonic-gate zonecfg_modify_nwif(
23247c478bd9Sstevel@tonic-gate 	zone_dochandle_t handle,
23257c478bd9Sstevel@tonic-gate 	struct zone_nwiftab *oldtabptr,
23267c478bd9Sstevel@tonic-gate 	struct zone_nwiftab *newtabptr)
23277c478bd9Sstevel@tonic-gate {
23287c478bd9Sstevel@tonic-gate 	int err;
23297c478bd9Sstevel@tonic-gate 
23307c478bd9Sstevel@tonic-gate 	if (oldtabptr == NULL || newtabptr == NULL)
23317c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
23327c478bd9Sstevel@tonic-gate 
23337c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
23347c478bd9Sstevel@tonic-gate 		return (err);
23357c478bd9Sstevel@tonic-gate 
23367c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_delete_nwif_core(handle, oldtabptr)) != Z_OK)
23377c478bd9Sstevel@tonic-gate 		return (err);
23387c478bd9Sstevel@tonic-gate 
23397c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_add_nwif_core(handle, newtabptr)) != Z_OK)
23407c478bd9Sstevel@tonic-gate 		return (err);
23417c478bd9Sstevel@tonic-gate 
23427c478bd9Sstevel@tonic-gate 	return (Z_OK);
23437c478bd9Sstevel@tonic-gate }
23447c478bd9Sstevel@tonic-gate 
23455679c89fSjv227347 /*
23465679c89fSjv227347  * Gets the zone hostid string stored in the specified zone configuration
23475679c89fSjv227347  * document.  This function returns Z_OK on success.  Z_BAD_PROPERTY is returned
23485679c89fSjv227347  * if the config file doesn't specify a hostid or if the hostid is blank.
23495679c89fSjv227347  *
23505679c89fSjv227347  * Note that buflen should be at least HW_HOSTID_LEN.
23515679c89fSjv227347  */
23525679c89fSjv227347 int
23535679c89fSjv227347 zonecfg_get_hostid(zone_dochandle_t handle, char *bufp, size_t buflen)
23545679c89fSjv227347 {
23555679c89fSjv227347 	int err;
23565679c89fSjv227347 
23575679c89fSjv227347 	if ((err = getrootattr(handle, DTD_ATTR_HOSTID, bufp, buflen)) != Z_OK)
23585679c89fSjv227347 		return (err);
23595679c89fSjv227347 	if (bufp[0] == '\0')
23605679c89fSjv227347 		return (Z_BAD_PROPERTY);
23615679c89fSjv227347 	return (Z_OK);
23625679c89fSjv227347 }
23635679c89fSjv227347 
23645679c89fSjv227347 /*
23655679c89fSjv227347  * Sets the hostid string in the specified zone config document to the given
23665679c89fSjv227347  * string value.  If 'hostidp' is NULL, then the config document's hostid
23675679c89fSjv227347  * attribute is cleared.  Non-NULL hostids are validated.  This function returns
23685679c89fSjv227347  * Z_OK on success.  Any other return value indicates failure.
23695679c89fSjv227347  */
23705679c89fSjv227347 int
23715679c89fSjv227347 zonecfg_set_hostid(zone_dochandle_t handle, const char *hostidp)
23725679c89fSjv227347 {
23735679c89fSjv227347 	int err;
23745679c89fSjv227347 
23755679c89fSjv227347 	/*
23765679c89fSjv227347 	 * A NULL hostid string is interpreted as a request to clear the
23775679c89fSjv227347 	 * hostid.
23785679c89fSjv227347 	 */
23795679c89fSjv227347 	if (hostidp == NULL || (err = zonecfg_valid_hostid(hostidp)) == Z_OK)
23805679c89fSjv227347 		return (setrootattr(handle, DTD_ATTR_HOSTID, hostidp));
23815679c89fSjv227347 	return (err);
23825679c89fSjv227347 }
23835679c89fSjv227347 
23845679c89fSjv227347 /*
23855679c89fSjv227347  * Determines if the specified string is a valid hostid string.  This function
23865679c89fSjv227347  * returns Z_OK if the string is a valid hostid string.  It returns Z_INVAL if
23875679c89fSjv227347  * 'hostidp' is NULL, Z_TOO_BIG if 'hostidp' refers to a string buffer
23885679c89fSjv227347  * containing a hex string with more than 8 digits, and Z_HOSTID_FUBAR if the
23895679c89fSjv227347  * string has an invalid format.
23905679c89fSjv227347  */
23915679c89fSjv227347 int
23925679c89fSjv227347 zonecfg_valid_hostid(const char *hostidp)
23935679c89fSjv227347 {
23945679c89fSjv227347 	char *currentp;
23955679c89fSjv227347 	u_longlong_t hostidval;
23965679c89fSjv227347 	size_t len;
23975679c89fSjv227347 
23985679c89fSjv227347 	if (hostidp == NULL)
23995679c89fSjv227347 		return (Z_INVAL);
24005679c89fSjv227347 
24015679c89fSjv227347 	/* Empty strings and strings with whitespace are invalid. */
24025679c89fSjv227347 	if (*hostidp == '\0')
24035679c89fSjv227347 		return (Z_HOSTID_FUBAR);
24045679c89fSjv227347 	for (currentp = (char *)hostidp; *currentp != '\0'; ++currentp) {
24055679c89fSjv227347 		if (isspace(*currentp))
24065679c89fSjv227347 			return (Z_HOSTID_FUBAR);
24075679c89fSjv227347 	}
24085679c89fSjv227347 	len = (size_t)(currentp - hostidp);
24095679c89fSjv227347 
24105679c89fSjv227347 	/*
24115679c89fSjv227347 	 * The caller might pass a hostid that is larger than the maximum
24125679c89fSjv227347 	 * unsigned 32-bit integral value.  Check for this!  Also, make sure
24135679c89fSjv227347 	 * that the whole string is converted (this helps us find illegal
24145679c89fSjv227347 	 * characters) and that the whole string fits within a buffer of size
24155679c89fSjv227347 	 * HW_HOSTID_LEN.
24165679c89fSjv227347 	 */
24175679c89fSjv227347 	currentp = (char *)hostidp;
24185679c89fSjv227347 	if (strncmp(hostidp, "0x", 2) == 0 || strncmp(hostidp, "0X", 2) == 0)
24195679c89fSjv227347 		currentp += 2;
24205679c89fSjv227347 	hostidval = strtoull(currentp, &currentp, 16);
24215679c89fSjv227347 	if ((size_t)(currentp - hostidp) >= HW_HOSTID_LEN)
24225679c89fSjv227347 		return (Z_TOO_BIG);
24235679c89fSjv227347 	if (hostidval > UINT_MAX || hostidval == HW_INVALID_HOSTID ||
24245679c89fSjv227347 	    currentp != hostidp + len)
24255679c89fSjv227347 		return (Z_HOSTID_FUBAR);
24265679c89fSjv227347 	return (Z_OK);
24275679c89fSjv227347 }
24285679c89fSjv227347 
24297c478bd9Sstevel@tonic-gate int
24307c478bd9Sstevel@tonic-gate zonecfg_lookup_dev(zone_dochandle_t handle, struct zone_devtab *tabptr)
24317c478bd9Sstevel@tonic-gate {
24327c478bd9Sstevel@tonic-gate 	xmlNodePtr cur, firstmatch;
24337c478bd9Sstevel@tonic-gate 	int err;
24347c478bd9Sstevel@tonic-gate 	char match[MAXPATHLEN];
24357c478bd9Sstevel@tonic-gate 
24367c478bd9Sstevel@tonic-gate 	if (tabptr == NULL)
24377c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
24387c478bd9Sstevel@tonic-gate 
24397c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
24407c478bd9Sstevel@tonic-gate 		return (err);
24417c478bd9Sstevel@tonic-gate 
24427c478bd9Sstevel@tonic-gate 	cur = handle->zone_dh_cur;
24437c478bd9Sstevel@tonic-gate 	firstmatch = NULL;
24447c478bd9Sstevel@tonic-gate 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
24457c478bd9Sstevel@tonic-gate 		if (xmlStrcmp(cur->name, DTD_ELEM_DEVICE))
24467c478bd9Sstevel@tonic-gate 			continue;
24477c478bd9Sstevel@tonic-gate 		if (strlen(tabptr->zone_dev_match) == 0)
24487c478bd9Sstevel@tonic-gate 			continue;
24497c478bd9Sstevel@tonic-gate 
24507c478bd9Sstevel@tonic-gate 		if ((fetchprop(cur, DTD_ATTR_MATCH, match,
24517c478bd9Sstevel@tonic-gate 		    sizeof (match)) == Z_OK)) {
24527c478bd9Sstevel@tonic-gate 			if (strcmp(tabptr->zone_dev_match,
24537c478bd9Sstevel@tonic-gate 			    match) == 0) {
24547c478bd9Sstevel@tonic-gate 				if (firstmatch == NULL)
24557c478bd9Sstevel@tonic-gate 					firstmatch = cur;
24567c478bd9Sstevel@tonic-gate 				else if (firstmatch != cur)
24577c478bd9Sstevel@tonic-gate 					return (Z_INSUFFICIENT_SPEC);
24587c478bd9Sstevel@tonic-gate 			} else {
24597c478bd9Sstevel@tonic-gate 				/*
24607c478bd9Sstevel@tonic-gate 				 * If another property matched but this
24617c478bd9Sstevel@tonic-gate 				 * one doesn't then reset firstmatch.
24627c478bd9Sstevel@tonic-gate 				 */
24637c478bd9Sstevel@tonic-gate 				if (firstmatch == cur)
24647c478bd9Sstevel@tonic-gate 					firstmatch = NULL;
24657c478bd9Sstevel@tonic-gate 			}
24667c478bd9Sstevel@tonic-gate 		}
24677c478bd9Sstevel@tonic-gate 	}
24687c478bd9Sstevel@tonic-gate 	if (firstmatch == NULL)
24697c478bd9Sstevel@tonic-gate 		return (Z_NO_RESOURCE_ID);
24707c478bd9Sstevel@tonic-gate 
24717c478bd9Sstevel@tonic-gate 	cur = firstmatch;
24727c478bd9Sstevel@tonic-gate 
24737c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_MATCH, tabptr->zone_dev_match,
24747c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_dev_match))) != Z_OK)
24757c478bd9Sstevel@tonic-gate 		return (err);
24767c478bd9Sstevel@tonic-gate 
24777c478bd9Sstevel@tonic-gate 	return (Z_OK);
24787c478bd9Sstevel@tonic-gate }
24797c478bd9Sstevel@tonic-gate 
24807c478bd9Sstevel@tonic-gate static int
24817c478bd9Sstevel@tonic-gate zonecfg_add_dev_core(zone_dochandle_t handle, struct zone_devtab *tabptr)
24827c478bd9Sstevel@tonic-gate {
24837c478bd9Sstevel@tonic-gate 	xmlNodePtr newnode, cur = handle->zone_dh_cur;
24847c478bd9Sstevel@tonic-gate 	int err;
24857c478bd9Sstevel@tonic-gate 
24867c478bd9Sstevel@tonic-gate 	newnode = xmlNewTextChild(cur, NULL, DTD_ELEM_DEVICE, NULL);
24877c478bd9Sstevel@tonic-gate 
2488a1be23daSdp 	if ((err = newprop(newnode, DTD_ATTR_MATCH,
24897c478bd9Sstevel@tonic-gate 	    tabptr->zone_dev_match)) != Z_OK)
24907c478bd9Sstevel@tonic-gate 		return (err);
24917c478bd9Sstevel@tonic-gate 
24927c478bd9Sstevel@tonic-gate 	return (Z_OK);
24937c478bd9Sstevel@tonic-gate }
24947c478bd9Sstevel@tonic-gate 
24957c478bd9Sstevel@tonic-gate int
24967c478bd9Sstevel@tonic-gate zonecfg_add_dev(zone_dochandle_t handle, struct zone_devtab *tabptr)
24977c478bd9Sstevel@tonic-gate {
24987c478bd9Sstevel@tonic-gate 	int err;
24997c478bd9Sstevel@tonic-gate 
25007c478bd9Sstevel@tonic-gate 	if (tabptr == NULL)
25017c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
25027c478bd9Sstevel@tonic-gate 
25037c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
25047c478bd9Sstevel@tonic-gate 		return (err);
25057c478bd9Sstevel@tonic-gate 
25067c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_add_dev_core(handle, tabptr)) != Z_OK)
25077c478bd9Sstevel@tonic-gate 		return (err);
25087c478bd9Sstevel@tonic-gate 
25097c478bd9Sstevel@tonic-gate 	return (Z_OK);
25107c478bd9Sstevel@tonic-gate }
25117c478bd9Sstevel@tonic-gate 
25127c478bd9Sstevel@tonic-gate static int
25137c478bd9Sstevel@tonic-gate zonecfg_delete_dev_core(zone_dochandle_t handle, struct zone_devtab *tabptr)
25147c478bd9Sstevel@tonic-gate {
2515facf4a8dSllai1 	xmlNodePtr cur = handle->zone_dh_cur;
25167c478bd9Sstevel@tonic-gate 	int match_match;
25177c478bd9Sstevel@tonic-gate 
25187c478bd9Sstevel@tonic-gate 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
25197c478bd9Sstevel@tonic-gate 		if (xmlStrcmp(cur->name, DTD_ELEM_DEVICE))
25207c478bd9Sstevel@tonic-gate 			continue;
25217c478bd9Sstevel@tonic-gate 
25227c478bd9Sstevel@tonic-gate 		match_match = match_prop(cur, DTD_ATTR_MATCH,
25237c478bd9Sstevel@tonic-gate 		    tabptr->zone_dev_match);
25247c478bd9Sstevel@tonic-gate 
25257c478bd9Sstevel@tonic-gate 		if (match_match) {
25267c478bd9Sstevel@tonic-gate 			xmlUnlinkNode(cur);
25277c478bd9Sstevel@tonic-gate 			xmlFreeNode(cur);
25287c478bd9Sstevel@tonic-gate 			return (Z_OK);
25297c478bd9Sstevel@tonic-gate 		}
25307c478bd9Sstevel@tonic-gate 	}
25317c478bd9Sstevel@tonic-gate 	return (Z_NO_RESOURCE_ID);
25327c478bd9Sstevel@tonic-gate }
25337c478bd9Sstevel@tonic-gate 
25347c478bd9Sstevel@tonic-gate int
25357c478bd9Sstevel@tonic-gate zonecfg_delete_dev(zone_dochandle_t handle, struct zone_devtab *tabptr)
25367c478bd9Sstevel@tonic-gate {
25377c478bd9Sstevel@tonic-gate 	int err;
25387c478bd9Sstevel@tonic-gate 
25397c478bd9Sstevel@tonic-gate 	if (tabptr == NULL)
25407c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
25417c478bd9Sstevel@tonic-gate 
25427c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
25437c478bd9Sstevel@tonic-gate 		return (err);
25447c478bd9Sstevel@tonic-gate 
25457c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_delete_dev_core(handle, tabptr)) != Z_OK)
25467c478bd9Sstevel@tonic-gate 		return (err);
25477c478bd9Sstevel@tonic-gate 
25487c478bd9Sstevel@tonic-gate 	return (Z_OK);
25497c478bd9Sstevel@tonic-gate }
25507c478bd9Sstevel@tonic-gate 
25517c478bd9Sstevel@tonic-gate int
25527c478bd9Sstevel@tonic-gate zonecfg_modify_dev(
25537c478bd9Sstevel@tonic-gate 	zone_dochandle_t handle,
25547c478bd9Sstevel@tonic-gate 	struct zone_devtab *oldtabptr,
25557c478bd9Sstevel@tonic-gate 	struct zone_devtab *newtabptr)
25567c478bd9Sstevel@tonic-gate {
25577c478bd9Sstevel@tonic-gate 	int err;
25587c478bd9Sstevel@tonic-gate 
25597c478bd9Sstevel@tonic-gate 	if (oldtabptr == NULL || newtabptr == NULL)
25607c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
25617c478bd9Sstevel@tonic-gate 
25627c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
25637c478bd9Sstevel@tonic-gate 		return (err);
25647c478bd9Sstevel@tonic-gate 
25657c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_delete_dev_core(handle, oldtabptr)) != Z_OK)
25667c478bd9Sstevel@tonic-gate 		return (err);
25677c478bd9Sstevel@tonic-gate 
25687c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_add_dev_core(handle, newtabptr)) != Z_OK)
25697c478bd9Sstevel@tonic-gate 		return (err);
25707c478bd9Sstevel@tonic-gate 
25717c478bd9Sstevel@tonic-gate 	return (Z_OK);
25727c478bd9Sstevel@tonic-gate }
25737c478bd9Sstevel@tonic-gate 
2574ff17c8bfSgjelinek /* Lock to serialize all devwalks */
2575ee519a1fSgjelinek static pthread_mutex_t zonecfg_devwalk_lock = PTHREAD_MUTEX_INITIALIZER;
2576ee519a1fSgjelinek /*
2577ff17c8bfSgjelinek  * Global variables used to pass data from zonecfg_dev_manifest to the nftw
2578ee519a1fSgjelinek  * call-back (zonecfg_devwalk_cb).  g_devwalk_data is really the void*
2579ff17c8bfSgjelinek  * parameter and g_devwalk_cb is really the *cb parameter from
2580ff17c8bfSgjelinek  * zonecfg_dev_manifest.
2581ee519a1fSgjelinek  */
2582ff17c8bfSgjelinek typedef struct __g_devwalk_data *g_devwalk_data_t;
2583ff17c8bfSgjelinek static g_devwalk_data_t g_devwalk_data;
2584ee519a1fSgjelinek static int (*g_devwalk_cb)(const char *, uid_t, gid_t, mode_t, const char *,
2585ee519a1fSgjelinek     void *);
2586ee519a1fSgjelinek static size_t g_devwalk_skip_prefix;
2587ee519a1fSgjelinek 
2588ee519a1fSgjelinek /*
2589ff17c8bfSgjelinek  * zonecfg_dev_manifest call-back function used during detach to generate the
2590ff17c8bfSgjelinek  * dev info in the manifest.
2591ff17c8bfSgjelinek  */
2592ff17c8bfSgjelinek static int
2593ff17c8bfSgjelinek get_detach_dev_entry(const char *name, uid_t uid, gid_t gid, mode_t mode,
2594ff17c8bfSgjelinek     const char *acl, void *hdl)
2595ff17c8bfSgjelinek {
2596ff17c8bfSgjelinek 	zone_dochandle_t handle = (zone_dochandle_t)hdl;
2597ff17c8bfSgjelinek 	xmlNodePtr newnode;
2598ff17c8bfSgjelinek 	xmlNodePtr cur;
2599ff17c8bfSgjelinek 	int err;
2600ff17c8bfSgjelinek 	char buf[128];
2601ff17c8bfSgjelinek 
2602ff17c8bfSgjelinek 	if ((err = operation_prep(handle)) != Z_OK)
2603ff17c8bfSgjelinek 		return (err);
2604ff17c8bfSgjelinek 
2605ff17c8bfSgjelinek 	cur = handle->zone_dh_cur;
2606ff17c8bfSgjelinek 	newnode = xmlNewTextChild(cur, NULL, DTD_ELEM_DEV_PERM, NULL);
2607ff17c8bfSgjelinek 	if ((err = newprop(newnode, DTD_ATTR_NAME, (char *)name)) != Z_OK)
2608ff17c8bfSgjelinek 		return (err);
2609ff17c8bfSgjelinek 	(void) snprintf(buf, sizeof (buf), "%lu", uid);
2610ff17c8bfSgjelinek 	if ((err = newprop(newnode, DTD_ATTR_UID, buf)) != Z_OK)
2611ff17c8bfSgjelinek 		return (err);
2612ff17c8bfSgjelinek 	(void) snprintf(buf, sizeof (buf), "%lu", gid);
2613ff17c8bfSgjelinek 	if ((err = newprop(newnode, DTD_ATTR_GID, buf)) != Z_OK)
2614ff17c8bfSgjelinek 		return (err);
2615ff17c8bfSgjelinek 	(void) snprintf(buf, sizeof (buf), "%o", mode);
2616ff17c8bfSgjelinek 	if ((err = newprop(newnode, DTD_ATTR_MODE, buf)) != Z_OK)
2617ff17c8bfSgjelinek 		return (err);
2618ff17c8bfSgjelinek 	if ((err = newprop(newnode, DTD_ATTR_ACL, (char *)acl)) != Z_OK)
2619ff17c8bfSgjelinek 		return (err);
2620ff17c8bfSgjelinek 	return (Z_OK);
2621ff17c8bfSgjelinek }
2622ff17c8bfSgjelinek 
2623ff17c8bfSgjelinek /*
2624ff17c8bfSgjelinek  * This is the nftw call-back function used by zonecfg_dev_manifest.  It is
2625ff17c8bfSgjelinek  * responsible for calling the actual call-back.
2626ee519a1fSgjelinek  */
2627ee519a1fSgjelinek /* ARGSUSED2 */
2628ee519a1fSgjelinek static int
2629ee519a1fSgjelinek zonecfg_devwalk_cb(const char *path, const struct stat *st, int f,
2630ee519a1fSgjelinek     struct FTW *ftw)
2631ee519a1fSgjelinek {
2632ee519a1fSgjelinek 	acl_t *acl;
2633ee519a1fSgjelinek 	char *acl_txt = NULL;
2634ee519a1fSgjelinek 
2635ee519a1fSgjelinek 	/* skip all but character and block devices */
2636ee519a1fSgjelinek 	if (!S_ISBLK(st->st_mode) && !S_ISCHR(st->st_mode))
2637ee519a1fSgjelinek 		return (0);
2638ee519a1fSgjelinek 
2639ee519a1fSgjelinek 	if ((acl_get(path, ACL_NO_TRIVIAL, &acl) == 0) &&
2640ee519a1fSgjelinek 	    acl != NULL) {
2641ee519a1fSgjelinek 		acl_txt = acl_totext(acl, ACL_NORESOLVE);
2642ee519a1fSgjelinek 		acl_free(acl);
2643ee519a1fSgjelinek 	}
2644ee519a1fSgjelinek 
2645ee519a1fSgjelinek 	if (strlen(path) <= g_devwalk_skip_prefix)
2646ee519a1fSgjelinek 		return (0);
2647ee519a1fSgjelinek 
2648ee519a1fSgjelinek 	g_devwalk_cb(path + g_devwalk_skip_prefix, st->st_uid, st->st_gid,
2649ee519a1fSgjelinek 	    st->st_mode & S_IAMB, acl_txt != NULL ? acl_txt : "",
2650ee519a1fSgjelinek 	    g_devwalk_data);
2651ee519a1fSgjelinek 	free(acl_txt);
2652ee519a1fSgjelinek 	return (0);
2653ee519a1fSgjelinek }
2654ee519a1fSgjelinek 
2655ee519a1fSgjelinek /*
2656ff17c8bfSgjelinek  * Walk the dev tree for the zone specified by hdl and call the
2657ff17c8bfSgjelinek  * get_detach_dev_entry call-back function for each entry in the tree.  The
2658ff17c8bfSgjelinek  * call-back will be passed the name, uid, gid, mode, acl string and the
2659ff17c8bfSgjelinek  * handle input parameter for each dev entry.
2660ee519a1fSgjelinek  *
2661ff17c8bfSgjelinek  * Data is passed to get_detach_dev_entry through the global variables
2662ee519a1fSgjelinek  * g_devwalk_data, *g_devwalk_cb, and g_devwalk_skip_prefix.  The
2663ff17c8bfSgjelinek  * zonecfg_devwalk_cb function will actually call get_detach_dev_entry.
2664ee519a1fSgjelinek  */
2665ee519a1fSgjelinek int
2666ff17c8bfSgjelinek zonecfg_dev_manifest(zone_dochandle_t hdl)
2667ee519a1fSgjelinek {
2668ee519a1fSgjelinek 	char path[MAXPATHLEN];
2669ee519a1fSgjelinek 	int ret;
2670ee519a1fSgjelinek 
2671ee519a1fSgjelinek 	if ((ret = zonecfg_get_zonepath(hdl, path, sizeof (path))) != Z_OK)
2672ee519a1fSgjelinek 		return (ret);
2673ee519a1fSgjelinek 
2674ee519a1fSgjelinek 	if (strlcat(path, "/dev", sizeof (path)) >= sizeof (path))
2675ee519a1fSgjelinek 		return (Z_TOO_BIG);
2676ee519a1fSgjelinek 
2677ee519a1fSgjelinek 	/*
2678ff17c8bfSgjelinek 	 * We have to serialize all devwalks in the same process
2679ee519a1fSgjelinek 	 * (which should be fine), since nftw() is so badly designed.
2680ee519a1fSgjelinek 	 */
2681ee519a1fSgjelinek 	(void) pthread_mutex_lock(&zonecfg_devwalk_lock);
2682ee519a1fSgjelinek 
2683ff17c8bfSgjelinek 	g_devwalk_skip_prefix = strlen(path) + 1;
2684ff17c8bfSgjelinek 	g_devwalk_data = (g_devwalk_data_t)hdl;
2685ff17c8bfSgjelinek 	g_devwalk_cb = get_detach_dev_entry;
2686ee519a1fSgjelinek 	(void) nftw(path, zonecfg_devwalk_cb, 0, FTW_PHYS);
2687ee519a1fSgjelinek 
2688ee519a1fSgjelinek 	(void) pthread_mutex_unlock(&zonecfg_devwalk_lock);
2689ee519a1fSgjelinek 	return (Z_OK);
2690ee519a1fSgjelinek }
2691ee519a1fSgjelinek 
2692ee519a1fSgjelinek /*
2693ee519a1fSgjelinek  * Update the owner, group, mode and acl on the specified dev (inpath) for
2694ee519a1fSgjelinek  * the zone (hdl).  This function can be used to fix up the dev tree after
2695ee519a1fSgjelinek  * attaching a migrated zone.
2696ee519a1fSgjelinek  */
2697ee519a1fSgjelinek int
2698ee519a1fSgjelinek zonecfg_devperms_apply(zone_dochandle_t hdl, const char *inpath, uid_t owner,
2699ee519a1fSgjelinek     gid_t group, mode_t mode, const char *acltxt)
2700ee519a1fSgjelinek {
2701ee519a1fSgjelinek 	int ret;
2702ee519a1fSgjelinek 	char path[MAXPATHLEN];
2703ee519a1fSgjelinek 	struct stat st;
2704ee519a1fSgjelinek 	acl_t *aclp;
2705ee519a1fSgjelinek 
2706ee519a1fSgjelinek 	if ((ret = zonecfg_get_zonepath(hdl, path, sizeof (path))) != Z_OK)
2707ee519a1fSgjelinek 		return (ret);
2708ee519a1fSgjelinek 
2709ee519a1fSgjelinek 	if (strlcat(path, "/dev/", sizeof (path)) >= sizeof (path))
2710ee519a1fSgjelinek 		return (Z_TOO_BIG);
2711ee519a1fSgjelinek 	if (strlcat(path, inpath, sizeof (path)) >= sizeof (path))
2712ee519a1fSgjelinek 		return (Z_TOO_BIG);
2713ee519a1fSgjelinek 
2714ee519a1fSgjelinek 	if (stat(path, &st) == -1)
2715ee519a1fSgjelinek 		return (Z_INVAL);
2716ee519a1fSgjelinek 
2717ee519a1fSgjelinek 	/* make sure we're only touching device nodes */
2718ee519a1fSgjelinek 	if (!S_ISCHR(st.st_mode) && !S_ISBLK(st.st_mode))
2719ee519a1fSgjelinek 		return (Z_INVAL);
2720ee519a1fSgjelinek 
2721ee519a1fSgjelinek 	if (chown(path, owner, group) == -1)
2722ee519a1fSgjelinek 		return (Z_SYSTEM);
2723ee519a1fSgjelinek 
2724ee519a1fSgjelinek 	if (chmod(path, mode) == -1)
2725ee519a1fSgjelinek 		return (Z_SYSTEM);
2726ee519a1fSgjelinek 
2727ee519a1fSgjelinek 	if ((acltxt == NULL) || (strcmp(acltxt, "") == 0))
2728ee519a1fSgjelinek 		return (Z_OK);
2729ee519a1fSgjelinek 
27302ad45a84Sjv227347 	if (acl_fromtext(acltxt, &aclp) != 0) {
27312ad45a84Sjv227347 		errno = EINVAL;
2732ee519a1fSgjelinek 		return (Z_SYSTEM);
27332ad45a84Sjv227347 	}
2734ee519a1fSgjelinek 
2735ee519a1fSgjelinek 	errno = 0;
2736ee519a1fSgjelinek 	if (acl_set(path, aclp) == -1) {
2737ee519a1fSgjelinek 		free(aclp);
2738ee519a1fSgjelinek 		return (Z_SYSTEM);
2739ee519a1fSgjelinek 	}
2740ee519a1fSgjelinek 
2741ee519a1fSgjelinek 	free(aclp);
2742ee519a1fSgjelinek 	return (Z_OK);
2743ee519a1fSgjelinek }
2744ee519a1fSgjelinek 
27457c478bd9Sstevel@tonic-gate /*
27467c478bd9Sstevel@tonic-gate  * This function finds everything mounted under a zone's rootpath.
27477c478bd9Sstevel@tonic-gate  * This returns the number of mounts under rootpath, or -1 on error.
27487c478bd9Sstevel@tonic-gate  * callback is called once per mount found with the first argument
27497c478bd9Sstevel@tonic-gate  * pointing to the  mount point.
27507c478bd9Sstevel@tonic-gate  *
27517c478bd9Sstevel@tonic-gate  * If the callback function returns non-zero zonecfg_find_mounts
27527c478bd9Sstevel@tonic-gate  * aborts with an error.
27537c478bd9Sstevel@tonic-gate  */
27547c478bd9Sstevel@tonic-gate int
27557c478bd9Sstevel@tonic-gate zonecfg_find_mounts(char *rootpath, int (*callback)(const char *, void *),
27567c478bd9Sstevel@tonic-gate     void *priv) {
27577c478bd9Sstevel@tonic-gate 	FILE *mnttab;
27587c478bd9Sstevel@tonic-gate 	struct mnttab m;
27597c478bd9Sstevel@tonic-gate 	size_t l;
276007b574eeSgjelinek 	int zfsl;
27617c478bd9Sstevel@tonic-gate 	int rv = 0;
276207b574eeSgjelinek 	char zfs_path[MAXPATHLEN];
27637c478bd9Sstevel@tonic-gate 
27647c478bd9Sstevel@tonic-gate 	assert(rootpath != NULL);
27657c478bd9Sstevel@tonic-gate 
276607b574eeSgjelinek 	if ((zfsl = snprintf(zfs_path, sizeof (zfs_path), "%s/.zfs/", rootpath))
276707b574eeSgjelinek 	    >= sizeof (zfs_path))
276807b574eeSgjelinek 		return (-1);
276907b574eeSgjelinek 
27707c478bd9Sstevel@tonic-gate 	l = strlen(rootpath);
27717c478bd9Sstevel@tonic-gate 
27727c478bd9Sstevel@tonic-gate 	mnttab = fopen("/etc/mnttab", "r");
27737c478bd9Sstevel@tonic-gate 
27747c478bd9Sstevel@tonic-gate 	if (mnttab == NULL)
27757c478bd9Sstevel@tonic-gate 		return (-1);
27767c478bd9Sstevel@tonic-gate 
27777c478bd9Sstevel@tonic-gate 	if (ioctl(fileno(mnttab), MNTIOC_SHOWHIDDEN, NULL) < 0)  {
27787c478bd9Sstevel@tonic-gate 		rv = -1;
27797c478bd9Sstevel@tonic-gate 		goto out;
27807c478bd9Sstevel@tonic-gate 	}
27817c478bd9Sstevel@tonic-gate 
27827c478bd9Sstevel@tonic-gate 	while (!getmntent(mnttab, &m)) {
27837c478bd9Sstevel@tonic-gate 		if ((strncmp(rootpath, m.mnt_mountp, l) == 0) &&
278407b574eeSgjelinek 		    (m.mnt_mountp[l] == '/') &&
278507b574eeSgjelinek 		    (strncmp(zfs_path, m.mnt_mountp, zfsl) != 0)) {
27867c478bd9Sstevel@tonic-gate 			rv++;
27877c478bd9Sstevel@tonic-gate 			if (callback == NULL)
27887c478bd9Sstevel@tonic-gate 				continue;
27897c478bd9Sstevel@tonic-gate 			if (callback(m.mnt_mountp, priv)) {
27907c478bd9Sstevel@tonic-gate 				rv = -1;
27917c478bd9Sstevel@tonic-gate 				goto out;
27927c478bd9Sstevel@tonic-gate 
27937c478bd9Sstevel@tonic-gate 			}
27947c478bd9Sstevel@tonic-gate 		}
27957c478bd9Sstevel@tonic-gate 	}
27967c478bd9Sstevel@tonic-gate 
27977c478bd9Sstevel@tonic-gate out:
27987c478bd9Sstevel@tonic-gate 	(void) fclose(mnttab);
27997c478bd9Sstevel@tonic-gate 	return (rv);
28007c478bd9Sstevel@tonic-gate }
28017c478bd9Sstevel@tonic-gate 
28027c478bd9Sstevel@tonic-gate int
28037c478bd9Sstevel@tonic-gate zonecfg_lookup_attr(zone_dochandle_t handle, struct zone_attrtab *tabptr)
28047c478bd9Sstevel@tonic-gate {
28057c478bd9Sstevel@tonic-gate 	xmlNodePtr cur, firstmatch;
28067c478bd9Sstevel@tonic-gate 	int err;
28077c478bd9Sstevel@tonic-gate 	char name[MAXNAMELEN], type[MAXNAMELEN], value[MAXNAMELEN];
28087c478bd9Sstevel@tonic-gate 
28097c478bd9Sstevel@tonic-gate 	if (tabptr == NULL)
28107c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
28117c478bd9Sstevel@tonic-gate 
28127c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
28137c478bd9Sstevel@tonic-gate 		return (err);
28147c478bd9Sstevel@tonic-gate 
28157c478bd9Sstevel@tonic-gate 	cur = handle->zone_dh_cur;
28167c478bd9Sstevel@tonic-gate 	firstmatch = NULL;
28177c478bd9Sstevel@tonic-gate 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
28187c478bd9Sstevel@tonic-gate 		if (xmlStrcmp(cur->name, DTD_ELEM_ATTR))
28197c478bd9Sstevel@tonic-gate 			continue;
28207c478bd9Sstevel@tonic-gate 		if (strlen(tabptr->zone_attr_name) > 0) {
28217c478bd9Sstevel@tonic-gate 			if ((fetchprop(cur, DTD_ATTR_NAME, name,
28227c478bd9Sstevel@tonic-gate 			    sizeof (name)) == Z_OK) &&
28237c478bd9Sstevel@tonic-gate 			    (strcmp(tabptr->zone_attr_name, name) == 0)) {
28247c478bd9Sstevel@tonic-gate 				if (firstmatch == NULL)
28257c478bd9Sstevel@tonic-gate 					firstmatch = cur;
28267c478bd9Sstevel@tonic-gate 				else
28277c478bd9Sstevel@tonic-gate 					return (Z_INSUFFICIENT_SPEC);
28287c478bd9Sstevel@tonic-gate 			}
28297c478bd9Sstevel@tonic-gate 		}
28307c478bd9Sstevel@tonic-gate 		if (strlen(tabptr->zone_attr_type) > 0) {
28317c478bd9Sstevel@tonic-gate 			if ((fetchprop(cur, DTD_ATTR_TYPE, type,
28327c478bd9Sstevel@tonic-gate 			    sizeof (type)) == Z_OK)) {
28337c478bd9Sstevel@tonic-gate 				if (strcmp(tabptr->zone_attr_type, type) == 0) {
28347c478bd9Sstevel@tonic-gate 					if (firstmatch == NULL)
28357c478bd9Sstevel@tonic-gate 						firstmatch = cur;
28367c478bd9Sstevel@tonic-gate 					else if (firstmatch != cur)
28377c478bd9Sstevel@tonic-gate 						return (Z_INSUFFICIENT_SPEC);
28387c478bd9Sstevel@tonic-gate 				} else {
28397c478bd9Sstevel@tonic-gate 					/*
28407c478bd9Sstevel@tonic-gate 					 * If another property matched but this
28417c478bd9Sstevel@tonic-gate 					 * one doesn't then reset firstmatch.
28427c478bd9Sstevel@tonic-gate 					 */
28437c478bd9Sstevel@tonic-gate 					if (firstmatch == cur)
28447c478bd9Sstevel@tonic-gate 						firstmatch = NULL;
28457c478bd9Sstevel@tonic-gate 				}
28467c478bd9Sstevel@tonic-gate 			}
28477c478bd9Sstevel@tonic-gate 		}
28487c478bd9Sstevel@tonic-gate 		if (strlen(tabptr->zone_attr_value) > 0) {
28497c478bd9Sstevel@tonic-gate 			if ((fetchprop(cur, DTD_ATTR_VALUE, value,
28507c478bd9Sstevel@tonic-gate 			    sizeof (value)) == Z_OK)) {
28517c478bd9Sstevel@tonic-gate 				if (strcmp(tabptr->zone_attr_value, value) ==
28527c478bd9Sstevel@tonic-gate 				    0) {
28537c478bd9Sstevel@tonic-gate 					if (firstmatch == NULL)
28547c478bd9Sstevel@tonic-gate 						firstmatch = cur;
28557c478bd9Sstevel@tonic-gate 					else if (firstmatch != cur)
28567c478bd9Sstevel@tonic-gate 						return (Z_INSUFFICIENT_SPEC);
28577c478bd9Sstevel@tonic-gate 				} else {
28587c478bd9Sstevel@tonic-gate 					/*
28597c478bd9Sstevel@tonic-gate 					 * If another property matched but this
28607c478bd9Sstevel@tonic-gate 					 * one doesn't then reset firstmatch.
28617c478bd9Sstevel@tonic-gate 					 */
28627c478bd9Sstevel@tonic-gate 					if (firstmatch == cur)
28637c478bd9Sstevel@tonic-gate 						firstmatch = NULL;
28647c478bd9Sstevel@tonic-gate 				}
28657c478bd9Sstevel@tonic-gate 			}
28667c478bd9Sstevel@tonic-gate 		}
28677c478bd9Sstevel@tonic-gate 	}
28687c478bd9Sstevel@tonic-gate 	if (firstmatch == NULL)
28697c478bd9Sstevel@tonic-gate 		return (Z_NO_RESOURCE_ID);
28707c478bd9Sstevel@tonic-gate 
28717c478bd9Sstevel@tonic-gate 	cur = firstmatch;
28727c478bd9Sstevel@tonic-gate 
28737c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_NAME, tabptr->zone_attr_name,
28747c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_attr_name))) != Z_OK)
28757c478bd9Sstevel@tonic-gate 		return (err);
28767c478bd9Sstevel@tonic-gate 
28777c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_TYPE, tabptr->zone_attr_type,
28787c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_attr_type))) != Z_OK)
28797c478bd9Sstevel@tonic-gate 		return (err);
28807c478bd9Sstevel@tonic-gate 
28817c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_VALUE, tabptr->zone_attr_value,
28827c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_attr_value))) != Z_OK)
28837c478bd9Sstevel@tonic-gate 		return (err);
28847c478bd9Sstevel@tonic-gate 
28857c478bd9Sstevel@tonic-gate 	return (Z_OK);
28867c478bd9Sstevel@tonic-gate }
28877c478bd9Sstevel@tonic-gate 
28887c478bd9Sstevel@tonic-gate static int
28897c478bd9Sstevel@tonic-gate zonecfg_add_attr_core(zone_dochandle_t handle, struct zone_attrtab *tabptr)
28907c478bd9Sstevel@tonic-gate {
28917c478bd9Sstevel@tonic-gate 	xmlNodePtr newnode, cur = handle->zone_dh_cur;
28927c478bd9Sstevel@tonic-gate 	int err;
28937c478bd9Sstevel@tonic-gate 
28947c478bd9Sstevel@tonic-gate 	newnode = xmlNewTextChild(cur, NULL, DTD_ELEM_ATTR, NULL);
2895a1be23daSdp 	err = newprop(newnode, DTD_ATTR_NAME, tabptr->zone_attr_name);
28967c478bd9Sstevel@tonic-gate 	if (err != Z_OK)
28977c478bd9Sstevel@tonic-gate 		return (err);
2898a1be23daSdp 	err = newprop(newnode, DTD_ATTR_TYPE, tabptr->zone_attr_type);
28997c478bd9Sstevel@tonic-gate 	if (err != Z_OK)
29007c478bd9Sstevel@tonic-gate 		return (err);
2901a1be23daSdp 	err = newprop(newnode, DTD_ATTR_VALUE, tabptr->zone_attr_value);
29027c478bd9Sstevel@tonic-gate 	if (err != Z_OK)
29037c478bd9Sstevel@tonic-gate 		return (err);
29047c478bd9Sstevel@tonic-gate 	return (Z_OK);
29057c478bd9Sstevel@tonic-gate }
29067c478bd9Sstevel@tonic-gate 
29077c478bd9Sstevel@tonic-gate int
29087c478bd9Sstevel@tonic-gate zonecfg_add_attr(zone_dochandle_t handle, struct zone_attrtab *tabptr)
29097c478bd9Sstevel@tonic-gate {
29107c478bd9Sstevel@tonic-gate 	int err;
29117c478bd9Sstevel@tonic-gate 
29127c478bd9Sstevel@tonic-gate 	if (tabptr == NULL)
29137c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
29147c478bd9Sstevel@tonic-gate 
29157c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
29167c478bd9Sstevel@tonic-gate 		return (err);
29177c478bd9Sstevel@tonic-gate 
29187c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_add_attr_core(handle, tabptr)) != Z_OK)
29197c478bd9Sstevel@tonic-gate 		return (err);
29207c478bd9Sstevel@tonic-gate 
29217c478bd9Sstevel@tonic-gate 	return (Z_OK);
29227c478bd9Sstevel@tonic-gate }
29237c478bd9Sstevel@tonic-gate 
29247c478bd9Sstevel@tonic-gate static int
29257c478bd9Sstevel@tonic-gate zonecfg_delete_attr_core(zone_dochandle_t handle, struct zone_attrtab *tabptr)
29267c478bd9Sstevel@tonic-gate {
29277c478bd9Sstevel@tonic-gate 	xmlNodePtr cur = handle->zone_dh_cur;
29287c478bd9Sstevel@tonic-gate 	int name_match, type_match, value_match;
29297c478bd9Sstevel@tonic-gate 
29307c478bd9Sstevel@tonic-gate 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
29317c478bd9Sstevel@tonic-gate 		if (xmlStrcmp(cur->name, DTD_ELEM_ATTR))
29327c478bd9Sstevel@tonic-gate 			continue;
29337c478bd9Sstevel@tonic-gate 
29347c478bd9Sstevel@tonic-gate 		name_match = match_prop(cur, DTD_ATTR_NAME,
29357c478bd9Sstevel@tonic-gate 		    tabptr->zone_attr_name);
29367c478bd9Sstevel@tonic-gate 		type_match = match_prop(cur, DTD_ATTR_TYPE,
29377c478bd9Sstevel@tonic-gate 		    tabptr->zone_attr_type);
29387c478bd9Sstevel@tonic-gate 		value_match = match_prop(cur, DTD_ATTR_VALUE,
29397c478bd9Sstevel@tonic-gate 		    tabptr->zone_attr_value);
29407c478bd9Sstevel@tonic-gate 
29417c478bd9Sstevel@tonic-gate 		if (name_match && type_match && value_match) {
29427c478bd9Sstevel@tonic-gate 			xmlUnlinkNode(cur);
29437c478bd9Sstevel@tonic-gate 			xmlFreeNode(cur);
29447c478bd9Sstevel@tonic-gate 			return (Z_OK);
29457c478bd9Sstevel@tonic-gate 		}
29467c478bd9Sstevel@tonic-gate 	}
29477c478bd9Sstevel@tonic-gate 	return (Z_NO_RESOURCE_ID);
29487c478bd9Sstevel@tonic-gate }
29497c478bd9Sstevel@tonic-gate 
29507c478bd9Sstevel@tonic-gate int
29517c478bd9Sstevel@tonic-gate zonecfg_delete_attr(zone_dochandle_t handle, struct zone_attrtab *tabptr)
29527c478bd9Sstevel@tonic-gate {
29537c478bd9Sstevel@tonic-gate 	int err;
29547c478bd9Sstevel@tonic-gate 
29557c478bd9Sstevel@tonic-gate 	if (tabptr == NULL)
29567c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
29577c478bd9Sstevel@tonic-gate 
29587c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
29597c478bd9Sstevel@tonic-gate 		return (err);
29607c478bd9Sstevel@tonic-gate 
29617c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_delete_attr_core(handle, tabptr)) != Z_OK)
29627c478bd9Sstevel@tonic-gate 		return (err);
29637c478bd9Sstevel@tonic-gate 
29647c478bd9Sstevel@tonic-gate 	return (Z_OK);
29657c478bd9Sstevel@tonic-gate }
29667c478bd9Sstevel@tonic-gate 
29677c478bd9Sstevel@tonic-gate int
29687c478bd9Sstevel@tonic-gate zonecfg_modify_attr(
29697c478bd9Sstevel@tonic-gate 	zone_dochandle_t handle,
29707c478bd9Sstevel@tonic-gate 	struct zone_attrtab *oldtabptr,
29717c478bd9Sstevel@tonic-gate 	struct zone_attrtab *newtabptr)
29727c478bd9Sstevel@tonic-gate {
29737c478bd9Sstevel@tonic-gate 	int err;
29747c478bd9Sstevel@tonic-gate 
29757c478bd9Sstevel@tonic-gate 	if (oldtabptr == NULL || newtabptr == NULL)
29767c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
29777c478bd9Sstevel@tonic-gate 
29787c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
29797c478bd9Sstevel@tonic-gate 		return (err);
29807c478bd9Sstevel@tonic-gate 
29817c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_delete_attr_core(handle, oldtabptr)) != Z_OK)
29827c478bd9Sstevel@tonic-gate 		return (err);
29837c478bd9Sstevel@tonic-gate 
29847c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_add_attr_core(handle, newtabptr)) != Z_OK)
29857c478bd9Sstevel@tonic-gate 		return (err);
29867c478bd9Sstevel@tonic-gate 
29877c478bd9Sstevel@tonic-gate 	return (Z_OK);
29887c478bd9Sstevel@tonic-gate }
29897c478bd9Sstevel@tonic-gate 
29907c478bd9Sstevel@tonic-gate int
29917c478bd9Sstevel@tonic-gate zonecfg_get_attr_boolean(const struct zone_attrtab *attr, boolean_t *value)
29927c478bd9Sstevel@tonic-gate {
29937c478bd9Sstevel@tonic-gate 	if (attr == NULL)
29947c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
29957c478bd9Sstevel@tonic-gate 
29967c478bd9Sstevel@tonic-gate 	if (strcmp(attr->zone_attr_type, DTD_ENTITY_BOOLEAN) != 0)
29977c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
29987c478bd9Sstevel@tonic-gate 
29997c478bd9Sstevel@tonic-gate 	if (strcmp(attr->zone_attr_value, DTD_ENTITY_TRUE) == 0) {
30007c478bd9Sstevel@tonic-gate 		*value = B_TRUE;
30017c478bd9Sstevel@tonic-gate 		return (Z_OK);
30027c478bd9Sstevel@tonic-gate 	}
30037c478bd9Sstevel@tonic-gate 	if (strcmp(attr->zone_attr_value, DTD_ENTITY_FALSE) == 0) {
30047c478bd9Sstevel@tonic-gate 		*value = B_FALSE;
30057c478bd9Sstevel@tonic-gate 		return (Z_OK);
30067c478bd9Sstevel@tonic-gate 	}
30077c478bd9Sstevel@tonic-gate 	return (Z_INVAL);
30087c478bd9Sstevel@tonic-gate }
30097c478bd9Sstevel@tonic-gate 
30107c478bd9Sstevel@tonic-gate int
30117c478bd9Sstevel@tonic-gate zonecfg_get_attr_int(const struct zone_attrtab *attr, int64_t *value)
30127c478bd9Sstevel@tonic-gate {
30137c478bd9Sstevel@tonic-gate 	long long result;
30147c478bd9Sstevel@tonic-gate 	char *endptr;
30157c478bd9Sstevel@tonic-gate 
30167c478bd9Sstevel@tonic-gate 	if (attr == NULL)
30177c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
30187c478bd9Sstevel@tonic-gate 
30197c478bd9Sstevel@tonic-gate 	if (strcmp(attr->zone_attr_type, DTD_ENTITY_INT) != 0)
30207c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
30217c478bd9Sstevel@tonic-gate 
30227c478bd9Sstevel@tonic-gate 	errno = 0;
30237c478bd9Sstevel@tonic-gate 	result = strtoll(attr->zone_attr_value, &endptr, 10);
30247c478bd9Sstevel@tonic-gate 	if (errno != 0 || *endptr != '\0')
30257c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
30267c478bd9Sstevel@tonic-gate 	*value = result;
30277c478bd9Sstevel@tonic-gate 	return (Z_OK);
30287c478bd9Sstevel@tonic-gate }
30297c478bd9Sstevel@tonic-gate 
30307c478bd9Sstevel@tonic-gate int
30317c478bd9Sstevel@tonic-gate zonecfg_get_attr_string(const struct zone_attrtab *attr, char *value,
30327c478bd9Sstevel@tonic-gate     size_t val_sz)
30337c478bd9Sstevel@tonic-gate {
30347c478bd9Sstevel@tonic-gate 	if (attr == NULL)
30357c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
30367c478bd9Sstevel@tonic-gate 
30377c478bd9Sstevel@tonic-gate 	if (strcmp(attr->zone_attr_type, DTD_ENTITY_STRING) != 0)
30387c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
30397c478bd9Sstevel@tonic-gate 
30407c478bd9Sstevel@tonic-gate 	if (strlcpy(value, attr->zone_attr_value, val_sz) >= val_sz)
30417c478bd9Sstevel@tonic-gate 		return (Z_TOO_BIG);
30427c478bd9Sstevel@tonic-gate 	return (Z_OK);
30437c478bd9Sstevel@tonic-gate }
30447c478bd9Sstevel@tonic-gate 
30457c478bd9Sstevel@tonic-gate int
30467c478bd9Sstevel@tonic-gate zonecfg_get_attr_uint(const struct zone_attrtab *attr, uint64_t *value)
30477c478bd9Sstevel@tonic-gate {
30487c478bd9Sstevel@tonic-gate 	unsigned long long result;
30497c478bd9Sstevel@tonic-gate 	long long neg_result;
30507c478bd9Sstevel@tonic-gate 	char *endptr;
30517c478bd9Sstevel@tonic-gate 
30527c478bd9Sstevel@tonic-gate 	if (attr == NULL)
30537c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
30547c478bd9Sstevel@tonic-gate 
30557c478bd9Sstevel@tonic-gate 	if (strcmp(attr->zone_attr_type, DTD_ENTITY_UINT) != 0)
30567c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
30577c478bd9Sstevel@tonic-gate 
30587c478bd9Sstevel@tonic-gate 	errno = 0;
30597c478bd9Sstevel@tonic-gate 	result = strtoull(attr->zone_attr_value, &endptr, 10);
30607c478bd9Sstevel@tonic-gate 	if (errno != 0 || *endptr != '\0')
30617c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
30627c478bd9Sstevel@tonic-gate 	errno = 0;
30637c478bd9Sstevel@tonic-gate 	neg_result = strtoll(attr->zone_attr_value, &endptr, 10);
30647c478bd9Sstevel@tonic-gate 	/*
30657c478bd9Sstevel@tonic-gate 	 * Incredibly, strtoull("<negative number>", ...) will not fail but
30667c478bd9Sstevel@tonic-gate 	 * return whatever (negative) number cast as a u_longlong_t, so we
30677c478bd9Sstevel@tonic-gate 	 * need to look for this here.
30687c478bd9Sstevel@tonic-gate 	 */
30697c478bd9Sstevel@tonic-gate 	if (errno == 0 && neg_result < 0)
30707c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
30717c478bd9Sstevel@tonic-gate 	*value = result;
30727c478bd9Sstevel@tonic-gate 	return (Z_OK);
30737c478bd9Sstevel@tonic-gate }
30747c478bd9Sstevel@tonic-gate 
30757c478bd9Sstevel@tonic-gate int
30767c478bd9Sstevel@tonic-gate zonecfg_lookup_rctl(zone_dochandle_t handle, struct zone_rctltab *tabptr)
30777c478bd9Sstevel@tonic-gate {
30787c478bd9Sstevel@tonic-gate 	xmlNodePtr cur, val;
30797c478bd9Sstevel@tonic-gate 	char savedname[MAXNAMELEN];
30807c478bd9Sstevel@tonic-gate 	struct zone_rctlvaltab *valptr;
30817c478bd9Sstevel@tonic-gate 	int err;
30827c478bd9Sstevel@tonic-gate 
30832ad45a84Sjv227347 	if (strlen(tabptr->zone_rctl_name) == 0)
30847c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
30857c478bd9Sstevel@tonic-gate 
30867c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
30877c478bd9Sstevel@tonic-gate 		return (err);
30887c478bd9Sstevel@tonic-gate 
30897c478bd9Sstevel@tonic-gate 	cur = handle->zone_dh_cur;
30907c478bd9Sstevel@tonic-gate 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
30917c478bd9Sstevel@tonic-gate 		if (xmlStrcmp(cur->name, DTD_ELEM_RCTL))
30927c478bd9Sstevel@tonic-gate 			continue;
30937c478bd9Sstevel@tonic-gate 		if ((fetchprop(cur, DTD_ATTR_NAME, savedname,
30947c478bd9Sstevel@tonic-gate 		    sizeof (savedname)) == Z_OK) &&
30957c478bd9Sstevel@tonic-gate 		    (strcmp(savedname, tabptr->zone_rctl_name) == 0)) {
30967c478bd9Sstevel@tonic-gate 			tabptr->zone_rctl_valptr = NULL;
30977c478bd9Sstevel@tonic-gate 			for (val = cur->xmlChildrenNode; val != NULL;
30987c478bd9Sstevel@tonic-gate 			    val = val->next) {
30997c478bd9Sstevel@tonic-gate 				valptr = (struct zone_rctlvaltab *)malloc(
31007c478bd9Sstevel@tonic-gate 				    sizeof (struct zone_rctlvaltab));
31017c478bd9Sstevel@tonic-gate 				if (valptr == NULL)
31027c478bd9Sstevel@tonic-gate 					return (Z_NOMEM);
31037c478bd9Sstevel@tonic-gate 				if ((fetchprop(val, DTD_ATTR_PRIV,
31047c478bd9Sstevel@tonic-gate 				    valptr->zone_rctlval_priv,
31057c478bd9Sstevel@tonic-gate 				    sizeof (valptr->zone_rctlval_priv)) !=
31067c478bd9Sstevel@tonic-gate 				    Z_OK))
31077c478bd9Sstevel@tonic-gate 					break;
31087c478bd9Sstevel@tonic-gate 				if ((fetchprop(val, DTD_ATTR_LIMIT,
31097c478bd9Sstevel@tonic-gate 				    valptr->zone_rctlval_limit,
31107c478bd9Sstevel@tonic-gate 				    sizeof (valptr->zone_rctlval_limit)) !=
31117c478bd9Sstevel@tonic-gate 				    Z_OK))
31127c478bd9Sstevel@tonic-gate 					break;
31137c478bd9Sstevel@tonic-gate 				if ((fetchprop(val, DTD_ATTR_ACTION,
31147c478bd9Sstevel@tonic-gate 				    valptr->zone_rctlval_action,
31157c478bd9Sstevel@tonic-gate 				    sizeof (valptr->zone_rctlval_action)) !=
31167c478bd9Sstevel@tonic-gate 				    Z_OK))
31177c478bd9Sstevel@tonic-gate 					break;
31187c478bd9Sstevel@tonic-gate 				if (zonecfg_add_rctl_value(tabptr, valptr) !=
31197c478bd9Sstevel@tonic-gate 				    Z_OK)
31207c478bd9Sstevel@tonic-gate 					break;
31217c478bd9Sstevel@tonic-gate 			}
31227c478bd9Sstevel@tonic-gate 			return (Z_OK);
31237c478bd9Sstevel@tonic-gate 		}
31247c478bd9Sstevel@tonic-gate 	}
31257c478bd9Sstevel@tonic-gate 	return (Z_NO_RESOURCE_ID);
31267c478bd9Sstevel@tonic-gate }
31277c478bd9Sstevel@tonic-gate 
31287c478bd9Sstevel@tonic-gate static int
31297c478bd9Sstevel@tonic-gate zonecfg_add_rctl_core(zone_dochandle_t handle, struct zone_rctltab *tabptr)
31307c478bd9Sstevel@tonic-gate {
31317c478bd9Sstevel@tonic-gate 	xmlNodePtr newnode, cur = handle->zone_dh_cur, valnode;
31327c478bd9Sstevel@tonic-gate 	struct zone_rctlvaltab *valptr;
31337c478bd9Sstevel@tonic-gate 	int err;
31347c478bd9Sstevel@tonic-gate 
31357c478bd9Sstevel@tonic-gate 	newnode = xmlNewTextChild(cur, NULL, DTD_ELEM_RCTL, NULL);
3136a1be23daSdp 	err = newprop(newnode, DTD_ATTR_NAME, tabptr->zone_rctl_name);
31377c478bd9Sstevel@tonic-gate 	if (err != Z_OK)
31387c478bd9Sstevel@tonic-gate 		return (err);
31397c478bd9Sstevel@tonic-gate 	for (valptr = tabptr->zone_rctl_valptr; valptr != NULL;
31407c478bd9Sstevel@tonic-gate 	    valptr = valptr->zone_rctlval_next) {
31417c478bd9Sstevel@tonic-gate 		valnode = xmlNewTextChild(newnode, NULL,
31427c478bd9Sstevel@tonic-gate 		    DTD_ELEM_RCTLVALUE, NULL);
3143a1be23daSdp 		err = newprop(valnode, DTD_ATTR_PRIV,
31447c478bd9Sstevel@tonic-gate 		    valptr->zone_rctlval_priv);
31457c478bd9Sstevel@tonic-gate 		if (err != Z_OK)
31467c478bd9Sstevel@tonic-gate 			return (err);
3147a1be23daSdp 		err = newprop(valnode, DTD_ATTR_LIMIT,
31487c478bd9Sstevel@tonic-gate 		    valptr->zone_rctlval_limit);
31497c478bd9Sstevel@tonic-gate 		if (err != Z_OK)
31507c478bd9Sstevel@tonic-gate 			return (err);
3151a1be23daSdp 		err = newprop(valnode, DTD_ATTR_ACTION,
31527c478bd9Sstevel@tonic-gate 		    valptr->zone_rctlval_action);
31537c478bd9Sstevel@tonic-gate 		if (err != Z_OK)
31547c478bd9Sstevel@tonic-gate 			return (err);
31557c478bd9Sstevel@tonic-gate 	}
31567c478bd9Sstevel@tonic-gate 	return (Z_OK);
31577c478bd9Sstevel@tonic-gate }
31587c478bd9Sstevel@tonic-gate 
31597c478bd9Sstevel@tonic-gate int
31607c478bd9Sstevel@tonic-gate zonecfg_add_rctl(zone_dochandle_t handle, struct zone_rctltab *tabptr)
31617c478bd9Sstevel@tonic-gate {
31627c478bd9Sstevel@tonic-gate 	int err;
31637c478bd9Sstevel@tonic-gate 
31642ad45a84Sjv227347 	if (tabptr == NULL)
31657c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
31667c478bd9Sstevel@tonic-gate 
31677c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
31687c478bd9Sstevel@tonic-gate 		return (err);
31697c478bd9Sstevel@tonic-gate 
31707c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_add_rctl_core(handle, tabptr)) != Z_OK)
31717c478bd9Sstevel@tonic-gate 		return (err);
31727c478bd9Sstevel@tonic-gate 
31737c478bd9Sstevel@tonic-gate 	return (Z_OK);
31747c478bd9Sstevel@tonic-gate }
31757c478bd9Sstevel@tonic-gate 
31767c478bd9Sstevel@tonic-gate static int
31777c478bd9Sstevel@tonic-gate zonecfg_delete_rctl_core(zone_dochandle_t handle, struct zone_rctltab *tabptr)
31787c478bd9Sstevel@tonic-gate {
31797c478bd9Sstevel@tonic-gate 	xmlNodePtr cur = handle->zone_dh_cur;
31807c478bd9Sstevel@tonic-gate 	xmlChar *savedname;
31817c478bd9Sstevel@tonic-gate 	int name_result;
31827c478bd9Sstevel@tonic-gate 
31837c478bd9Sstevel@tonic-gate 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
31847c478bd9Sstevel@tonic-gate 		if (xmlStrcmp(cur->name, DTD_ELEM_RCTL))
31857c478bd9Sstevel@tonic-gate 			continue;
31867c478bd9Sstevel@tonic-gate 
31877c478bd9Sstevel@tonic-gate 		savedname = xmlGetProp(cur, DTD_ATTR_NAME);
31887c478bd9Sstevel@tonic-gate 		if (savedname == NULL)	/* shouldn't happen */
31897c478bd9Sstevel@tonic-gate 			continue;
31907c478bd9Sstevel@tonic-gate 		name_result = xmlStrcmp(savedname,
31917c478bd9Sstevel@tonic-gate 		    (const xmlChar *) tabptr->zone_rctl_name);
31927c478bd9Sstevel@tonic-gate 		xmlFree(savedname);
31937c478bd9Sstevel@tonic-gate 
31947c478bd9Sstevel@tonic-gate 		if (name_result == 0) {
31957c478bd9Sstevel@tonic-gate 			xmlUnlinkNode(cur);
31967c478bd9Sstevel@tonic-gate 			xmlFreeNode(cur);
31977c478bd9Sstevel@tonic-gate 			return (Z_OK);
31987c478bd9Sstevel@tonic-gate 		}
31997c478bd9Sstevel@tonic-gate 	}
32007c478bd9Sstevel@tonic-gate 	return (Z_NO_RESOURCE_ID);
32017c478bd9Sstevel@tonic-gate }
32027c478bd9Sstevel@tonic-gate 
32037c478bd9Sstevel@tonic-gate int
32047c478bd9Sstevel@tonic-gate zonecfg_delete_rctl(zone_dochandle_t handle, struct zone_rctltab *tabptr)
32057c478bd9Sstevel@tonic-gate {
32067c478bd9Sstevel@tonic-gate 	int err;
32077c478bd9Sstevel@tonic-gate 
32082ad45a84Sjv227347 	if (tabptr == NULL)
32097c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
32107c478bd9Sstevel@tonic-gate 
32117c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
32127c478bd9Sstevel@tonic-gate 		return (err);
32137c478bd9Sstevel@tonic-gate 
32147c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_delete_rctl_core(handle, tabptr)) != Z_OK)
32157c478bd9Sstevel@tonic-gate 		return (err);
32167c478bd9Sstevel@tonic-gate 
32177c478bd9Sstevel@tonic-gate 	return (Z_OK);
32187c478bd9Sstevel@tonic-gate }
32197c478bd9Sstevel@tonic-gate 
32207c478bd9Sstevel@tonic-gate int
32217c478bd9Sstevel@tonic-gate zonecfg_modify_rctl(
32227c478bd9Sstevel@tonic-gate 	zone_dochandle_t handle,
32237c478bd9Sstevel@tonic-gate 	struct zone_rctltab *oldtabptr,
32247c478bd9Sstevel@tonic-gate 	struct zone_rctltab *newtabptr)
32257c478bd9Sstevel@tonic-gate {
32267c478bd9Sstevel@tonic-gate 	int err;
32277c478bd9Sstevel@tonic-gate 
32282ad45a84Sjv227347 	if (oldtabptr == NULL || newtabptr == NULL)
32297c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
32307c478bd9Sstevel@tonic-gate 
32317c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
32327c478bd9Sstevel@tonic-gate 		return (err);
32337c478bd9Sstevel@tonic-gate 
32347c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_delete_rctl_core(handle, oldtabptr)) != Z_OK)
32357c478bd9Sstevel@tonic-gate 		return (err);
32367c478bd9Sstevel@tonic-gate 
32377c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_add_rctl_core(handle, newtabptr)) != Z_OK)
32387c478bd9Sstevel@tonic-gate 		return (err);
32397c478bd9Sstevel@tonic-gate 
32407c478bd9Sstevel@tonic-gate 	return (Z_OK);
32417c478bd9Sstevel@tonic-gate }
32427c478bd9Sstevel@tonic-gate 
32437c478bd9Sstevel@tonic-gate int
32447c478bd9Sstevel@tonic-gate zonecfg_add_rctl_value(
32457c478bd9Sstevel@tonic-gate 	struct zone_rctltab *tabptr,
32467c478bd9Sstevel@tonic-gate 	struct zone_rctlvaltab *valtabptr)
32477c478bd9Sstevel@tonic-gate {
32487c478bd9Sstevel@tonic-gate 	struct zone_rctlvaltab *last, *old, *new;
32497c478bd9Sstevel@tonic-gate 	rctlblk_t *rctlblk = alloca(rctlblk_size());
32507c478bd9Sstevel@tonic-gate 
32517c478bd9Sstevel@tonic-gate 	last = tabptr->zone_rctl_valptr;
32527c478bd9Sstevel@tonic-gate 	for (old = last; old != NULL; old = old->zone_rctlval_next)
32537c478bd9Sstevel@tonic-gate 		last = old;	/* walk to the end of the list */
32547c478bd9Sstevel@tonic-gate 	new = valtabptr;	/* alloc'd by caller */
32557c478bd9Sstevel@tonic-gate 	new->zone_rctlval_next = NULL;
32567c478bd9Sstevel@tonic-gate 	if (zonecfg_construct_rctlblk(valtabptr, rctlblk) != Z_OK)
32577c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
32587c478bd9Sstevel@tonic-gate 	if (!zonecfg_valid_rctlblk(rctlblk))
32597c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
32607c478bd9Sstevel@tonic-gate 	if (last == NULL)
32617c478bd9Sstevel@tonic-gate 		tabptr->zone_rctl_valptr = new;
32627c478bd9Sstevel@tonic-gate 	else
32637c478bd9Sstevel@tonic-gate 		last->zone_rctlval_next = new;
32647c478bd9Sstevel@tonic-gate 	return (Z_OK);
32657c478bd9Sstevel@tonic-gate }
32667c478bd9Sstevel@tonic-gate 
32677c478bd9Sstevel@tonic-gate int
32687c478bd9Sstevel@tonic-gate zonecfg_remove_rctl_value(
32697c478bd9Sstevel@tonic-gate 	struct zone_rctltab *tabptr,
32707c478bd9Sstevel@tonic-gate 	struct zone_rctlvaltab *valtabptr)
32717c478bd9Sstevel@tonic-gate {
32727c478bd9Sstevel@tonic-gate 	struct zone_rctlvaltab *last, *this, *next;
32737c478bd9Sstevel@tonic-gate 
32747c478bd9Sstevel@tonic-gate 	last = tabptr->zone_rctl_valptr;
32757c478bd9Sstevel@tonic-gate 	for (this = last; this != NULL; this = this->zone_rctlval_next) {
32767c478bd9Sstevel@tonic-gate 		if (strcmp(this->zone_rctlval_priv,
32777c478bd9Sstevel@tonic-gate 		    valtabptr->zone_rctlval_priv) == 0 &&
32787c478bd9Sstevel@tonic-gate 		    strcmp(this->zone_rctlval_limit,
32797c478bd9Sstevel@tonic-gate 		    valtabptr->zone_rctlval_limit) == 0 &&
32807c478bd9Sstevel@tonic-gate 		    strcmp(this->zone_rctlval_action,
32817c478bd9Sstevel@tonic-gate 		    valtabptr->zone_rctlval_action) == 0) {
32827c478bd9Sstevel@tonic-gate 			next = this->zone_rctlval_next;
32837c478bd9Sstevel@tonic-gate 			if (this == tabptr->zone_rctl_valptr)
32847c478bd9Sstevel@tonic-gate 				tabptr->zone_rctl_valptr = next;
32857c478bd9Sstevel@tonic-gate 			else
32867c478bd9Sstevel@tonic-gate 				last->zone_rctlval_next = next;
32877c478bd9Sstevel@tonic-gate 			free(this);
32887c478bd9Sstevel@tonic-gate 			return (Z_OK);
32897c478bd9Sstevel@tonic-gate 		} else
32907c478bd9Sstevel@tonic-gate 			last = this;
32917c478bd9Sstevel@tonic-gate 	}
32927c478bd9Sstevel@tonic-gate 	return (Z_NO_PROPERTY_ID);
32937c478bd9Sstevel@tonic-gate }
32947c478bd9Sstevel@tonic-gate 
3295ff17c8bfSgjelinek void
3296ff17c8bfSgjelinek zonecfg_set_swinv(zone_dochandle_t handle)
3297ff17c8bfSgjelinek {
3298ff17c8bfSgjelinek 	handle->zone_dh_sw_inv = B_TRUE;
3299ff17c8bfSgjelinek }
3300ff17c8bfSgjelinek 
3301ff17c8bfSgjelinek /*
3302ff17c8bfSgjelinek  * Add the pkg to the sw inventory on the handle.
3303ff17c8bfSgjelinek  */
3304ff17c8bfSgjelinek int
3305ff17c8bfSgjelinek zonecfg_add_pkg(zone_dochandle_t handle, char *name, char *version)
3306ff17c8bfSgjelinek {
3307ff17c8bfSgjelinek 	xmlNodePtr newnode;
3308ff17c8bfSgjelinek 	xmlNodePtr cur;
3309ff17c8bfSgjelinek 	int err;
3310ff17c8bfSgjelinek 
3311ff17c8bfSgjelinek 	if ((err = operation_prep(handle)) != Z_OK)
3312ff17c8bfSgjelinek 		return (err);
3313ff17c8bfSgjelinek 
3314ff17c8bfSgjelinek 	cur = handle->zone_dh_cur;
3315ff17c8bfSgjelinek 	newnode = xmlNewTextChild(cur, NULL, DTD_ELEM_PACKAGE, NULL);
3316ff17c8bfSgjelinek 	if ((err = newprop(newnode, DTD_ATTR_NAME, name)) != Z_OK)
3317ff17c8bfSgjelinek 		return (err);
3318ff17c8bfSgjelinek 	if ((err = newprop(newnode, DTD_ATTR_VERSION, version)) != Z_OK)
3319ff17c8bfSgjelinek 		return (err);
3320ff17c8bfSgjelinek 	return (Z_OK);
3321ff17c8bfSgjelinek }
3322ff17c8bfSgjelinek 
3323ff17c8bfSgjelinek int
3324ff17c8bfSgjelinek zonecfg_add_patch(zone_dochandle_t handle, char *id, void **pnode)
3325ff17c8bfSgjelinek {
3326ff17c8bfSgjelinek 	xmlNodePtr node = (xmlNodePtr)*pnode;
3327ff17c8bfSgjelinek 	xmlNodePtr cur;
3328ff17c8bfSgjelinek 	int err;
3329ff17c8bfSgjelinek 
3330ff17c8bfSgjelinek 	if ((err = operation_prep(handle)) != Z_OK)
3331ff17c8bfSgjelinek 		return (err);
3332ff17c8bfSgjelinek 
3333ff17c8bfSgjelinek 	cur = handle->zone_dh_cur;
3334ff17c8bfSgjelinek 	node = xmlNewTextChild(cur, NULL, DTD_ELEM_PATCH, NULL);
3335ff17c8bfSgjelinek 	if ((err = newprop(node, DTD_ATTR_ID, id)) != Z_OK)
3336ff17c8bfSgjelinek 		return (err);
3337ff17c8bfSgjelinek 	*pnode = (void *)node;
3338ff17c8bfSgjelinek 	return (Z_OK);
3339ff17c8bfSgjelinek }
3340ff17c8bfSgjelinek 
3341ff17c8bfSgjelinek int
3342ff17c8bfSgjelinek zonecfg_add_patch_obs(char *id, void *cur)
3343ff17c8bfSgjelinek {
3344ff17c8bfSgjelinek 	xmlNodePtr	node;
3345ff17c8bfSgjelinek 	int err;
3346ff17c8bfSgjelinek 
3347ff17c8bfSgjelinek 	node = xmlNewTextChild((xmlNodePtr)cur, NULL, DTD_ELEM_OBSOLETES, NULL);
3348ff17c8bfSgjelinek 	if ((err = newprop(node, DTD_ATTR_ID, id)) != Z_OK)
3349ff17c8bfSgjelinek 		return (err);
3350ff17c8bfSgjelinek 	return (Z_OK);
3351ff17c8bfSgjelinek }
3352ff17c8bfSgjelinek 
33537c478bd9Sstevel@tonic-gate char *
33547c478bd9Sstevel@tonic-gate zonecfg_strerror(int errnum)
33557c478bd9Sstevel@tonic-gate {
33567c478bd9Sstevel@tonic-gate 	switch (errnum) {
33577c478bd9Sstevel@tonic-gate 	case Z_OK:
33587c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "OK"));
33597c478bd9Sstevel@tonic-gate 	case Z_EMPTY_DOCUMENT:
33607c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Empty document"));
33617c478bd9Sstevel@tonic-gate 	case Z_WRONG_DOC_TYPE:
33627c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Wrong document type"));
33637c478bd9Sstevel@tonic-gate 	case Z_BAD_PROPERTY:
33647c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Bad document property"));
33657c478bd9Sstevel@tonic-gate 	case Z_TEMP_FILE:
33667c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN,
33677c478bd9Sstevel@tonic-gate 		    "Problem creating temporary file"));
33687c478bd9Sstevel@tonic-gate 	case Z_SAVING_FILE:
33697c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Problem saving file"));
33707c478bd9Sstevel@tonic-gate 	case Z_NO_ENTRY:
33717c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "No such entry"));
33727c478bd9Sstevel@tonic-gate 	case Z_BOGUS_ZONE_NAME:
33737c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Bogus zone name"));
33747c478bd9Sstevel@tonic-gate 	case Z_REQD_RESOURCE_MISSING:
33757c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Required resource missing"));
33767c478bd9Sstevel@tonic-gate 	case Z_REQD_PROPERTY_MISSING:
33777c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Required property missing"));
33787c478bd9Sstevel@tonic-gate 	case Z_BAD_HANDLE:
33797c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Bad handle"));
33807c478bd9Sstevel@tonic-gate 	case Z_NOMEM:
33817c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Out of memory"));
33827c478bd9Sstevel@tonic-gate 	case Z_INVAL:
33837c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Invalid argument"));
33847c478bd9Sstevel@tonic-gate 	case Z_ACCES:
33857c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Permission denied"));
33867c478bd9Sstevel@tonic-gate 	case Z_TOO_BIG:
33877c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Argument list too long"));
33887c478bd9Sstevel@tonic-gate 	case Z_MISC_FS:
33897c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN,
33907c478bd9Sstevel@tonic-gate 		    "Miscellaneous file system error"));
33917c478bd9Sstevel@tonic-gate 	case Z_NO_ZONE:
33927c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "No such zone configured"));
33937c478bd9Sstevel@tonic-gate 	case Z_NO_RESOURCE_TYPE:
33947c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "No such resource type"));
33957c478bd9Sstevel@tonic-gate 	case Z_NO_RESOURCE_ID:
33967c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "No such resource with that id"));
33977c478bd9Sstevel@tonic-gate 	case Z_NO_PROPERTY_TYPE:
33987c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "No such property type"));
33997c478bd9Sstevel@tonic-gate 	case Z_NO_PROPERTY_ID:
34007c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "No such property with that id"));
3401087719fdSdp 	case Z_BAD_ZONE_STATE:
34027c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN,
3403087719fdSdp 		    "Zone state is invalid for the requested operation"));
34047c478bd9Sstevel@tonic-gate 	case Z_INVALID_DOCUMENT:
34057c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Invalid document"));
3406087719fdSdp 	case Z_NAME_IN_USE:
3407087719fdSdp 		return (dgettext(TEXT_DOMAIN, "Zone name already in use"));
34087c478bd9Sstevel@tonic-gate 	case Z_NO_SUCH_ID:
34097c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "No such zone ID"));
34107c478bd9Sstevel@tonic-gate 	case Z_UPDATING_INDEX:
34117c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Problem updating index file"));
34127c478bd9Sstevel@tonic-gate 	case Z_LOCKING_FILE:
34137c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Locking index file"));
34147c478bd9Sstevel@tonic-gate 	case Z_UNLOCKING_FILE:
34157c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Unlocking index file"));
34167c478bd9Sstevel@tonic-gate 	case Z_INSUFFICIENT_SPEC:
34177c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Insufficient specification"));
34187c478bd9Sstevel@tonic-gate 	case Z_RESOLVED_PATH:
34197c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Resolved path mismatch"));
34207c478bd9Sstevel@tonic-gate 	case Z_IPV6_ADDR_PREFIX_LEN:
34217c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN,
34227c478bd9Sstevel@tonic-gate 		    "IPv6 address missing required prefix length"));
34237c478bd9Sstevel@tonic-gate 	case Z_BOGUS_ADDRESS:
34247c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN,
34257c478bd9Sstevel@tonic-gate 		    "Neither an IPv4 nor an IPv6 address nor a host name"));
3426ffbafc53Scomay 	case Z_PRIV_PROHIBITED:
3427ffbafc53Scomay 		return (dgettext(TEXT_DOMAIN,
3428ffbafc53Scomay 		    "Specified privilege is prohibited"));
3429ffbafc53Scomay 	case Z_PRIV_REQUIRED:
3430ffbafc53Scomay 		return (dgettext(TEXT_DOMAIN,
3431ffbafc53Scomay 		    "Required privilege is missing"));
3432ffbafc53Scomay 	case Z_PRIV_UNKNOWN:
3433ffbafc53Scomay 		return (dgettext(TEXT_DOMAIN,
3434ffbafc53Scomay 		    "Specified privilege is unknown"));
34359acbbeafSnn35248 	case Z_BRAND_ERROR:
34369acbbeafSnn35248 		return (dgettext(TEXT_DOMAIN,
34379acbbeafSnn35248 		    "Brand-specific error"));
34380209230bSgjelinek 	case Z_INCOMPATIBLE:
34390209230bSgjelinek 		return (dgettext(TEXT_DOMAIN, "Incompatible settings"));
34400209230bSgjelinek 	case Z_ALIAS_DISALLOW:
34410209230bSgjelinek 		return (dgettext(TEXT_DOMAIN,
34420209230bSgjelinek 		    "An incompatible rctl already exists for this property"));
34430209230bSgjelinek 	case Z_CLEAR_DISALLOW:
34440209230bSgjelinek 		return (dgettext(TEXT_DOMAIN,
34450209230bSgjelinek 		    "Clearing this property is not allowed"));
34460209230bSgjelinek 	case Z_POOL:
34470209230bSgjelinek 		return (dgettext(TEXT_DOMAIN, "libpool(3LIB) error"));
34480209230bSgjelinek 	case Z_POOLS_NOT_ACTIVE:
34490209230bSgjelinek 		return (dgettext(TEXT_DOMAIN, "Pools facility not active; "
34500209230bSgjelinek 		    "zone will not be bound to pool"));
34510209230bSgjelinek 	case Z_POOL_ENABLE:
34520209230bSgjelinek 		return (dgettext(TEXT_DOMAIN,
34530209230bSgjelinek 		    "Could not enable pools facility"));
34540209230bSgjelinek 	case Z_NO_POOL:
34550209230bSgjelinek 		return (dgettext(TEXT_DOMAIN,
34560209230bSgjelinek 		    "Pool not found; using default pool"));
34570209230bSgjelinek 	case Z_POOL_CREATE:
34580209230bSgjelinek 		return (dgettext(TEXT_DOMAIN,
34590209230bSgjelinek 		    "Could not create a temporary pool"));
34600209230bSgjelinek 	case Z_POOL_BIND:
34610209230bSgjelinek 		return (dgettext(TEXT_DOMAIN, "Could not bind zone to pool"));
34625679c89fSjv227347 	case Z_HOSTID_FUBAR:
34635679c89fSjv227347 		return (dgettext(TEXT_DOMAIN, "Specified hostid is invalid"));
34642ad45a84Sjv227347 	case Z_SYSTEM:
34652ad45a84Sjv227347 		return (strerror(errno));
34667c478bd9Sstevel@tonic-gate 	default:
34677c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Unknown error"));
34687c478bd9Sstevel@tonic-gate 	}
34697c478bd9Sstevel@tonic-gate }
34707c478bd9Sstevel@tonic-gate 
34717c478bd9Sstevel@tonic-gate /*
34727c478bd9Sstevel@tonic-gate  * Note that the zonecfg_setXent() and zonecfg_endXent() calls are all the
34737c478bd9Sstevel@tonic-gate  * same, as they just turn around and call zonecfg_setent() / zonecfg_endent().
34747c478bd9Sstevel@tonic-gate  */
34757c478bd9Sstevel@tonic-gate 
34767c478bd9Sstevel@tonic-gate static int
34777c478bd9Sstevel@tonic-gate zonecfg_setent(zone_dochandle_t handle)
34787c478bd9Sstevel@tonic-gate {
34797c478bd9Sstevel@tonic-gate 	xmlNodePtr cur;
34807c478bd9Sstevel@tonic-gate 	int err;
34817c478bd9Sstevel@tonic-gate 
34827c478bd9Sstevel@tonic-gate 	if (handle == NULL)
34837c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
34847c478bd9Sstevel@tonic-gate 
34857c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK) {
34867c478bd9Sstevel@tonic-gate 		handle->zone_dh_cur = NULL;
34877c478bd9Sstevel@tonic-gate 		return (err);
34887c478bd9Sstevel@tonic-gate 	}
34897c478bd9Sstevel@tonic-gate 	cur = handle->zone_dh_cur;
34907c478bd9Sstevel@tonic-gate 	cur = cur->xmlChildrenNode;
34917c478bd9Sstevel@tonic-gate 	handle->zone_dh_cur = cur;
34927c478bd9Sstevel@tonic-gate 	return (Z_OK);
34937c478bd9Sstevel@tonic-gate }
34947c478bd9Sstevel@tonic-gate 
34957c478bd9Sstevel@tonic-gate static int
34967c478bd9Sstevel@tonic-gate zonecfg_endent(zone_dochandle_t handle)
34977c478bd9Sstevel@tonic-gate {
34987c478bd9Sstevel@tonic-gate 	if (handle == NULL)
34997c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
35007c478bd9Sstevel@tonic-gate 
35017c478bd9Sstevel@tonic-gate 	handle->zone_dh_cur = handle->zone_dh_top;
35027c478bd9Sstevel@tonic-gate 	return (Z_OK);
35037c478bd9Sstevel@tonic-gate }
35047c478bd9Sstevel@tonic-gate 
35050209230bSgjelinek /*
35060209230bSgjelinek  * Do the work required to manipulate a process through libproc.
35070209230bSgjelinek  * If grab_process() returns no errors (0), then release_process()
35080209230bSgjelinek  * must eventually be called.
35090209230bSgjelinek  *
35100209230bSgjelinek  * Return values:
35110209230bSgjelinek  *      0 Successful creation of agent thread
35120209230bSgjelinek  *      1 Error grabbing
35130209230bSgjelinek  *      2 Error creating agent
35140209230bSgjelinek  */
35150209230bSgjelinek static int
35160209230bSgjelinek grab_process(pr_info_handle_t *p)
35170209230bSgjelinek {
35180209230bSgjelinek 	int ret;
35190209230bSgjelinek 
35200209230bSgjelinek 	if ((p->pr = Pgrab(p->pid, 0, &ret)) != NULL) {
35210209230bSgjelinek 
35220209230bSgjelinek 		if (Psetflags(p->pr, PR_RLC) != 0) {
35230209230bSgjelinek 			Prelease(p->pr, 0);
35240209230bSgjelinek 			return (1);
35250209230bSgjelinek 		}
35260209230bSgjelinek 		if (Pcreate_agent(p->pr) == 0) {
35270209230bSgjelinek 			return (0);
35280209230bSgjelinek 
35290209230bSgjelinek 		} else {
35300209230bSgjelinek 			Prelease(p->pr, 0);
35310209230bSgjelinek 			return (2);
35320209230bSgjelinek 		}
35330209230bSgjelinek 	} else {
35340209230bSgjelinek 		return (1);
35350209230bSgjelinek 	}
35360209230bSgjelinek }
35370209230bSgjelinek 
35380209230bSgjelinek /*
35390209230bSgjelinek  * Release the specified process. This destroys the agent
35400209230bSgjelinek  * and releases the process. If the process is NULL, nothing
35410209230bSgjelinek  * is done. This function should only be called if grab_process()
35420209230bSgjelinek  * has previously been called and returned success.
35430209230bSgjelinek  *
35440209230bSgjelinek  * This function is Pgrab-safe.
35450209230bSgjelinek  */
35460209230bSgjelinek static void
35470209230bSgjelinek release_process(struct ps_prochandle *Pr)
35480209230bSgjelinek {
35490209230bSgjelinek 	if (Pr == NULL)
35500209230bSgjelinek 		return;
35510209230bSgjelinek 
35520209230bSgjelinek 	Pdestroy_agent(Pr);
35530209230bSgjelinek 	Prelease(Pr, 0);
35540209230bSgjelinek }
35550209230bSgjelinek 
35560209230bSgjelinek static boolean_t
35570209230bSgjelinek grab_zone_proc(char *zonename, pr_info_handle_t *p)
35580209230bSgjelinek {
35590209230bSgjelinek 	DIR *dirp;
35600209230bSgjelinek 	struct dirent *dentp;
35610209230bSgjelinek 	zoneid_t zoneid;
35620209230bSgjelinek 	int pid_self;
35630209230bSgjelinek 	psinfo_t psinfo;
35640209230bSgjelinek 
35650209230bSgjelinek 	if (zone_get_id(zonename, &zoneid) != 0)
35660209230bSgjelinek 		return (B_FALSE);
35670209230bSgjelinek 
35680209230bSgjelinek 	pid_self = getpid();
35690209230bSgjelinek 
35700209230bSgjelinek 	if ((dirp = opendir("/proc")) == NULL)
35710209230bSgjelinek 		return (B_FALSE);
35720209230bSgjelinek 
35730209230bSgjelinek 	while (dentp = readdir(dirp)) {
35740209230bSgjelinek 		p->pid = atoi(dentp->d_name);
35750209230bSgjelinek 
35760209230bSgjelinek 		/* Skip self */
35770209230bSgjelinek 		if (p->pid == pid_self)
35780209230bSgjelinek 			continue;
35790209230bSgjelinek 
35800209230bSgjelinek 		if (proc_get_psinfo(p->pid, &psinfo) != 0)
35810209230bSgjelinek 			continue;
35820209230bSgjelinek 
35830209230bSgjelinek 		if (psinfo.pr_zoneid != zoneid)
35840209230bSgjelinek 			continue;
35850209230bSgjelinek 
35860209230bSgjelinek 		/* attempt to grab process */
35870209230bSgjelinek 		if (grab_process(p) != 0)
35880209230bSgjelinek 			continue;
35890209230bSgjelinek 
35900209230bSgjelinek 		if (pr_getzoneid(p->pr) != zoneid) {
35910209230bSgjelinek 			release_process(p->pr);
35920209230bSgjelinek 			continue;
35930209230bSgjelinek 		}
35940209230bSgjelinek 
35950209230bSgjelinek 		(void) closedir(dirp);
35960209230bSgjelinek 		return (B_TRUE);
35970209230bSgjelinek 	}
35980209230bSgjelinek 
35990209230bSgjelinek 	(void) closedir(dirp);
36000209230bSgjelinek 	return (B_FALSE);
36010209230bSgjelinek }
36020209230bSgjelinek 
36030209230bSgjelinek static boolean_t
36040209230bSgjelinek get_priv_rctl(struct ps_prochandle *pr, char *name, rctlblk_t *rblk)
36050209230bSgjelinek {
36060209230bSgjelinek 	if (pr_getrctl(pr, name, NULL, rblk, RCTL_FIRST))
36070209230bSgjelinek 		return (B_FALSE);
36080209230bSgjelinek 
36090209230bSgjelinek 	if (rctlblk_get_privilege(rblk) == RCPRIV_PRIVILEGED)
36100209230bSgjelinek 		return (B_TRUE);
36110209230bSgjelinek 
36120209230bSgjelinek 	while (pr_getrctl(pr, name, rblk, rblk, RCTL_NEXT) == 0) {
36130209230bSgjelinek 		if (rctlblk_get_privilege(rblk) == RCPRIV_PRIVILEGED)
36140209230bSgjelinek 			return (B_TRUE);
36150209230bSgjelinek 	}
36160209230bSgjelinek 
36170209230bSgjelinek 	return (B_FALSE);
36180209230bSgjelinek }
36190209230bSgjelinek 
36200209230bSgjelinek /*
36210209230bSgjelinek  * Apply the current rctl settings to the specified, running zone.
36220209230bSgjelinek  */
36230209230bSgjelinek int
36240209230bSgjelinek zonecfg_apply_rctls(char *zone_name, zone_dochandle_t handle)
36250209230bSgjelinek {
36260209230bSgjelinek 	int err;
36270209230bSgjelinek 	int res = Z_OK;
36280209230bSgjelinek 	rctlblk_t *rblk;
36290209230bSgjelinek 	pr_info_handle_t p;
36300209230bSgjelinek 	struct zone_rctltab rctl;
36310209230bSgjelinek 
36320209230bSgjelinek 	if ((err = zonecfg_setrctlent(handle)) != Z_OK)
36330209230bSgjelinek 		return (err);
36340209230bSgjelinek 
36350209230bSgjelinek 	if ((rblk = (rctlblk_t *)malloc(rctlblk_size())) == NULL) {
36360209230bSgjelinek 		(void) zonecfg_endrctlent(handle);
36370209230bSgjelinek 		return (Z_NOMEM);
36380209230bSgjelinek 	}
36390209230bSgjelinek 
36400209230bSgjelinek 	if (!grab_zone_proc(zone_name, &p)) {
36410209230bSgjelinek 		(void) zonecfg_endrctlent(handle);
36420209230bSgjelinek 		free(rblk);
36430209230bSgjelinek 		return (Z_SYSTEM);
36440209230bSgjelinek 	}
36450209230bSgjelinek 
36460209230bSgjelinek 	while (zonecfg_getrctlent(handle, &rctl) == Z_OK) {
36470209230bSgjelinek 		char *rname;
36480209230bSgjelinek 		struct zone_rctlvaltab *valptr;
36490209230bSgjelinek 
36500209230bSgjelinek 		rname = rctl.zone_rctl_name;
36510209230bSgjelinek 
36520209230bSgjelinek 		/* first delete all current privileged settings for this rctl */
36530209230bSgjelinek 		while (get_priv_rctl(p.pr, rname, rblk)) {
36540209230bSgjelinek 			if (pr_setrctl(p.pr, rname, NULL, rblk, RCTL_DELETE) !=
36550209230bSgjelinek 			    0) {
36560209230bSgjelinek 				res = Z_SYSTEM;
36570209230bSgjelinek 				goto done;
36580209230bSgjelinek 			}
36590209230bSgjelinek 		}
36600209230bSgjelinek 
36610209230bSgjelinek 		/* now set each new value for the rctl */
36620209230bSgjelinek 		for (valptr = rctl.zone_rctl_valptr; valptr != NULL;
36630209230bSgjelinek 		    valptr = valptr->zone_rctlval_next) {
36640209230bSgjelinek 			if ((err = zonecfg_construct_rctlblk(valptr, rblk))
36650209230bSgjelinek 			    != Z_OK) {
36660209230bSgjelinek 				res = errno = err;
36670209230bSgjelinek 				goto done;
36680209230bSgjelinek 			}
36690209230bSgjelinek 
36700209230bSgjelinek 			if (pr_setrctl(p.pr, rname, NULL, rblk, RCTL_INSERT)) {
36710209230bSgjelinek 				res = Z_SYSTEM;
36720209230bSgjelinek 				goto done;
36730209230bSgjelinek 			}
36740209230bSgjelinek 		}
36750209230bSgjelinek 	}
36760209230bSgjelinek 
36770209230bSgjelinek done:
36780209230bSgjelinek 	release_process(p.pr);
36790209230bSgjelinek 	free(rblk);
36800209230bSgjelinek 	(void) zonecfg_endrctlent(handle);
36810209230bSgjelinek 
36820209230bSgjelinek 	return (res);
36830209230bSgjelinek }
36840209230bSgjelinek 
36850209230bSgjelinek static const xmlChar *
36860209230bSgjelinek nm_to_dtd(char *nm)
36870209230bSgjelinek {
36880209230bSgjelinek 	if (strcmp(nm, "device") == 0)
36890209230bSgjelinek 		return (DTD_ELEM_DEVICE);
36900209230bSgjelinek 	if (strcmp(nm, "fs") == 0)
36910209230bSgjelinek 		return (DTD_ELEM_FS);
36920209230bSgjelinek 	if (strcmp(nm, "inherit-pkg-dir") == 0)
36930209230bSgjelinek 		return (DTD_ELEM_IPD);
36940209230bSgjelinek 	if (strcmp(nm, "net") == 0)
36950209230bSgjelinek 		return (DTD_ELEM_NET);
36960209230bSgjelinek 	if (strcmp(nm, "attr") == 0)
36970209230bSgjelinek 		return (DTD_ELEM_ATTR);
36980209230bSgjelinek 	if (strcmp(nm, "rctl") == 0)
36990209230bSgjelinek 		return (DTD_ELEM_RCTL);
37000209230bSgjelinek 	if (strcmp(nm, "dataset") == 0)
37010209230bSgjelinek 		return (DTD_ELEM_DATASET);
37020209230bSgjelinek 
37030209230bSgjelinek 	return (NULL);
37040209230bSgjelinek }
37050209230bSgjelinek 
37060209230bSgjelinek int
37070209230bSgjelinek zonecfg_num_resources(zone_dochandle_t handle, char *rsrc)
37080209230bSgjelinek {
37090209230bSgjelinek 	int num = 0;
37100209230bSgjelinek 	const xmlChar *dtd;
37110209230bSgjelinek 	xmlNodePtr cur;
37120209230bSgjelinek 
37130209230bSgjelinek 	if ((dtd = nm_to_dtd(rsrc)) == NULL)
37140209230bSgjelinek 		return (num);
37150209230bSgjelinek 
37160209230bSgjelinek 	if (zonecfg_setent(handle) != Z_OK)
37170209230bSgjelinek 		return (num);
37180209230bSgjelinek 
37190209230bSgjelinek 	for (cur = handle->zone_dh_cur; cur != NULL; cur = cur->next)
37200209230bSgjelinek 		if (xmlStrcmp(cur->name, dtd) == 0)
37210209230bSgjelinek 			num++;
37220209230bSgjelinek 
37230209230bSgjelinek 	(void) zonecfg_endent(handle);
37240209230bSgjelinek 
37250209230bSgjelinek 	return (num);
37260209230bSgjelinek }
37270209230bSgjelinek 
37280209230bSgjelinek int
37290209230bSgjelinek zonecfg_del_all_resources(zone_dochandle_t handle, char *rsrc)
37300209230bSgjelinek {
37310209230bSgjelinek 	int err;
37320209230bSgjelinek 	const xmlChar *dtd;
37330209230bSgjelinek 	xmlNodePtr cur;
37340209230bSgjelinek 
37350209230bSgjelinek 	if ((dtd = nm_to_dtd(rsrc)) == NULL)
37360209230bSgjelinek 		return (Z_NO_RESOURCE_TYPE);
37370209230bSgjelinek 
37380209230bSgjelinek 	if ((err = zonecfg_setent(handle)) != Z_OK)
37390209230bSgjelinek 		return (err);
37400209230bSgjelinek 
37410209230bSgjelinek 	cur = handle->zone_dh_cur;
37420209230bSgjelinek 	while (cur != NULL) {
37430209230bSgjelinek 		xmlNodePtr tmp;
37440209230bSgjelinek 
37450209230bSgjelinek 		if (xmlStrcmp(cur->name, dtd)) {
37460209230bSgjelinek 			cur = cur->next;
37470209230bSgjelinek 			continue;
37480209230bSgjelinek 		}
37490209230bSgjelinek 
37500209230bSgjelinek 		tmp = cur->next;
37510209230bSgjelinek 		xmlUnlinkNode(cur);
37520209230bSgjelinek 		xmlFreeNode(cur);
37530209230bSgjelinek 		cur = tmp;
37540209230bSgjelinek 	}
37550209230bSgjelinek 
37560209230bSgjelinek 	(void) zonecfg_endent(handle);
37570209230bSgjelinek 	return (Z_OK);
37580209230bSgjelinek }
37590209230bSgjelinek 
37600209230bSgjelinek static boolean_t
37610209230bSgjelinek valid_uint(char *s, uint64_t *n)
37620209230bSgjelinek {
37630209230bSgjelinek 	char *endp;
37640209230bSgjelinek 
37650209230bSgjelinek 	/* strtoull accepts '-'?! so we want to flag that as an error */
37660209230bSgjelinek 	if (strchr(s, '-') != NULL)
37670209230bSgjelinek 		return (B_FALSE);
37680209230bSgjelinek 
37690209230bSgjelinek 	errno = 0;
37700209230bSgjelinek 	*n = strtoull(s, &endp, 10);
37710209230bSgjelinek 
37720209230bSgjelinek 	if (errno != 0 || *endp != '\0')
37730209230bSgjelinek 		return (B_FALSE);
37740209230bSgjelinek 	return (B_TRUE);
37750209230bSgjelinek }
37760209230bSgjelinek 
37770209230bSgjelinek /*
37780209230bSgjelinek  * Convert a string representing a number (possibly a fraction) into an integer.
37790209230bSgjelinek  * The string can have a modifier (K, M, G or T).   The modifiers are treated
37800209230bSgjelinek  * as powers of two (not 10).
37810209230bSgjelinek  */
37820209230bSgjelinek int
37830209230bSgjelinek zonecfg_str_to_bytes(char *str, uint64_t *bytes)
37840209230bSgjelinek {
37850209230bSgjelinek 	long double val;
37860209230bSgjelinek 	char *unitp;
37870209230bSgjelinek 	uint64_t scale;
37880209230bSgjelinek 
37890209230bSgjelinek 	if ((val = strtold(str, &unitp)) < 0)
37900209230bSgjelinek 		return (-1);
37910209230bSgjelinek 
37920209230bSgjelinek 	/* remove any leading white space from units string */
37930209230bSgjelinek 	while (isspace(*unitp) != 0)
37940209230bSgjelinek 		++unitp;
37950209230bSgjelinek 
37960209230bSgjelinek 	/* if no units explicitly set, error */
37970209230bSgjelinek 	if (unitp == NULL || *unitp == '\0') {
37980209230bSgjelinek 		scale = 1;
37990209230bSgjelinek 	} else {
38000209230bSgjelinek 		int i;
38010209230bSgjelinek 		char *units[] = {"K", "M", "G", "T", NULL};
38020209230bSgjelinek 
38030209230bSgjelinek 		scale = 1024;
38040209230bSgjelinek 
38050209230bSgjelinek 		/* update scale based on units */
38060209230bSgjelinek 		for (i = 0; units[i] != NULL; i++) {
38070209230bSgjelinek 			if (strcasecmp(unitp, units[i]) == 0)
38080209230bSgjelinek 				break;
38090209230bSgjelinek 			scale <<= 10;
38100209230bSgjelinek 		}
38110209230bSgjelinek 
38120209230bSgjelinek 		if (units[i] == NULL)
38130209230bSgjelinek 			return (-1);
38140209230bSgjelinek 	}
38150209230bSgjelinek 
38160209230bSgjelinek 	*bytes = (uint64_t)(val * scale);
38170209230bSgjelinek 	return (0);
38180209230bSgjelinek }
38190209230bSgjelinek 
38200209230bSgjelinek boolean_t
38210209230bSgjelinek zonecfg_valid_ncpus(char *lowstr, char *highstr)
38220209230bSgjelinek {
38230209230bSgjelinek 	uint64_t low, high;
38240209230bSgjelinek 
38250209230bSgjelinek 	if (!valid_uint(lowstr, &low) || !valid_uint(highstr, &high) ||
38260209230bSgjelinek 	    low < 1 || low > high)
38270209230bSgjelinek 		return (B_FALSE);
38280209230bSgjelinek 
38290209230bSgjelinek 	return (B_TRUE);
38300209230bSgjelinek }
38310209230bSgjelinek 
38320209230bSgjelinek boolean_t
38330209230bSgjelinek zonecfg_valid_importance(char *impstr)
38340209230bSgjelinek {
38350209230bSgjelinek 	uint64_t num;
38360209230bSgjelinek 
38370209230bSgjelinek 	if (!valid_uint(impstr, &num))
38380209230bSgjelinek 		return (B_FALSE);
38390209230bSgjelinek 
38400209230bSgjelinek 	return (B_TRUE);
38410209230bSgjelinek }
38420209230bSgjelinek 
38430209230bSgjelinek boolean_t
38440209230bSgjelinek zonecfg_valid_alias_limit(char *name, char *limitstr, uint64_t *limit)
38450209230bSgjelinek {
38460209230bSgjelinek 	int i;
38470209230bSgjelinek 
38480209230bSgjelinek 	for (i = 0; aliases[i].shortname != NULL; i++)
38490209230bSgjelinek 		if (strcmp(name, aliases[i].shortname) == 0)
38500209230bSgjelinek 			break;
38510209230bSgjelinek 
38520209230bSgjelinek 	if (aliases[i].shortname == NULL)
38530209230bSgjelinek 		return (B_FALSE);
38540209230bSgjelinek 
38550209230bSgjelinek 	if (!valid_uint(limitstr, limit) || *limit < aliases[i].low_limit)
38560209230bSgjelinek 		return (B_FALSE);
38570209230bSgjelinek 
38580209230bSgjelinek 	return (B_TRUE);
38590209230bSgjelinek }
38600209230bSgjelinek 
38610209230bSgjelinek boolean_t
38620209230bSgjelinek zonecfg_valid_memlimit(char *memstr, uint64_t *mem_val)
38630209230bSgjelinek {
38640209230bSgjelinek 	if (zonecfg_str_to_bytes(memstr, mem_val) != 0)
38650209230bSgjelinek 		return (B_FALSE);
38660209230bSgjelinek 
38670209230bSgjelinek 	return (B_TRUE);
38680209230bSgjelinek }
38690209230bSgjelinek 
38700209230bSgjelinek static int
38710209230bSgjelinek zerr_pool(char *pool_err, int err_size, int res)
38720209230bSgjelinek {
38730209230bSgjelinek 	(void) strlcpy(pool_err, pool_strerror(pool_error()), err_size);
38740209230bSgjelinek 	return (res);
38750209230bSgjelinek }
38760209230bSgjelinek 
38770209230bSgjelinek static int
38780209230bSgjelinek create_tmp_pset(char *pool_err, int err_size, pool_conf_t *pconf, pool_t *pool,
38790209230bSgjelinek     char *name, int min, int max)
38800209230bSgjelinek {
38810209230bSgjelinek 	pool_resource_t *res;
38820209230bSgjelinek 	pool_elem_t *elem;
38830209230bSgjelinek 	pool_value_t *val;
38840209230bSgjelinek 
38850209230bSgjelinek 	if ((res = pool_resource_create(pconf, "pset", name)) == NULL)
38860209230bSgjelinek 		return (zerr_pool(pool_err, err_size, Z_POOL));
38870209230bSgjelinek 
38880209230bSgjelinek 	if (pool_associate(pconf, pool, res) != PO_SUCCESS)
38890209230bSgjelinek 		return (zerr_pool(pool_err, err_size, Z_POOL));
38900209230bSgjelinek 
38910209230bSgjelinek 	if ((elem = pool_resource_to_elem(pconf, res)) == NULL)
38920209230bSgjelinek 		return (zerr_pool(pool_err, err_size, Z_POOL));
38930209230bSgjelinek 
38940209230bSgjelinek 	if ((val = pool_value_alloc()) == NULL)
38950209230bSgjelinek 		return (zerr_pool(pool_err, err_size, Z_POOL));
38960209230bSgjelinek 
38970209230bSgjelinek 	/* set the maximum number of cpus for the pset */
38980209230bSgjelinek 	pool_value_set_uint64(val, (uint64_t)max);
38990209230bSgjelinek 
39000209230bSgjelinek 	if (pool_put_property(pconf, elem, "pset.max", val) != PO_SUCCESS) {
39010209230bSgjelinek 		pool_value_free(val);
39020209230bSgjelinek 		return (zerr_pool(pool_err, err_size, Z_POOL));
39030209230bSgjelinek 	}
39040209230bSgjelinek 
39050209230bSgjelinek 	/* set the minimum number of cpus for the pset */
39060209230bSgjelinek 	pool_value_set_uint64(val, (uint64_t)min);
39070209230bSgjelinek 
39080209230bSgjelinek 	if (pool_put_property(pconf, elem, "pset.min", val) != PO_SUCCESS) {
39090209230bSgjelinek 		pool_value_free(val);
39100209230bSgjelinek 		return (zerr_pool(pool_err, err_size, Z_POOL));
39110209230bSgjelinek 	}
39120209230bSgjelinek 
39130209230bSgjelinek 	pool_value_free(val);
39140209230bSgjelinek 
39150209230bSgjelinek 	return (Z_OK);
39160209230bSgjelinek }
39170209230bSgjelinek 
39180209230bSgjelinek static int
39190209230bSgjelinek create_tmp_pool(char *pool_err, int err_size, pool_conf_t *pconf, char *name,
39200209230bSgjelinek     struct zone_psettab *pset_tab)
39210209230bSgjelinek {
39220209230bSgjelinek 	pool_t *pool;
39230209230bSgjelinek 	int res = Z_OK;
39240209230bSgjelinek 
39250209230bSgjelinek 	/* create a temporary pool configuration */
39260209230bSgjelinek 	if (pool_conf_open(pconf, NULL, PO_TEMP) != PO_SUCCESS) {
39270209230bSgjelinek 		res = zerr_pool(pool_err, err_size, Z_POOL);
39280209230bSgjelinek 		return (res);
39290209230bSgjelinek 	}
39300209230bSgjelinek 
39310209230bSgjelinek 	if ((pool = pool_create(pconf, name)) == NULL) {
39320209230bSgjelinek 		res = zerr_pool(pool_err, err_size, Z_POOL_CREATE);
39330209230bSgjelinek 		goto done;
39340209230bSgjelinek 	}
39350209230bSgjelinek 
39360209230bSgjelinek 	/* set pool importance */
39370209230bSgjelinek 	if (pset_tab->zone_importance[0] != '\0') {
39380209230bSgjelinek 		pool_elem_t *elem;
39390209230bSgjelinek 		pool_value_t *val;
39400209230bSgjelinek 
39410209230bSgjelinek 		if ((elem = pool_to_elem(pconf, pool)) == NULL) {
39420209230bSgjelinek 			res = zerr_pool(pool_err, err_size, Z_POOL);
39430209230bSgjelinek 			goto done;
39440209230bSgjelinek 		}
39450209230bSgjelinek 
39460209230bSgjelinek 		if ((val = pool_value_alloc()) == NULL) {
39470209230bSgjelinek 			res = zerr_pool(pool_err, err_size, Z_POOL);
39480209230bSgjelinek 			goto done;
39490209230bSgjelinek 		}
39500209230bSgjelinek 
39510209230bSgjelinek 		pool_value_set_int64(val,
39520209230bSgjelinek 		    (int64_t)atoi(pset_tab->zone_importance));
39530209230bSgjelinek 
39540209230bSgjelinek 		if (pool_put_property(pconf, elem, "pool.importance", val)
39550209230bSgjelinek 		    != PO_SUCCESS) {
39560209230bSgjelinek 			res = zerr_pool(pool_err, err_size, Z_POOL);
39570209230bSgjelinek 			pool_value_free(val);
39580209230bSgjelinek 			goto done;
39590209230bSgjelinek 		}
39600209230bSgjelinek 
39610209230bSgjelinek 		pool_value_free(val);
39620209230bSgjelinek 	}
39630209230bSgjelinek 
39640209230bSgjelinek 	if ((res = create_tmp_pset(pool_err, err_size, pconf, pool, name,
39650209230bSgjelinek 	    atoi(pset_tab->zone_ncpu_min),
39660209230bSgjelinek 	    atoi(pset_tab->zone_ncpu_max))) != Z_OK)
39670209230bSgjelinek 		goto done;
39680209230bSgjelinek 
39690209230bSgjelinek 	/* validation */
39700209230bSgjelinek 	if (pool_conf_status(pconf) == POF_INVALID) {
39710209230bSgjelinek 		res = zerr_pool(pool_err, err_size, Z_POOL);
39720209230bSgjelinek 		goto done;
39730209230bSgjelinek 	}
39740209230bSgjelinek 
39750209230bSgjelinek 	/*
39760209230bSgjelinek 	 * This validation is the one we expect to fail if the user specified
39770209230bSgjelinek 	 * an invalid configuration (too many cpus) for this system.
39780209230bSgjelinek 	 */
39790209230bSgjelinek 	if (pool_conf_validate(pconf, POV_RUNTIME) != PO_SUCCESS) {
39800209230bSgjelinek 		res = zerr_pool(pool_err, err_size, Z_POOL_CREATE);
39810209230bSgjelinek 		goto done;
39820209230bSgjelinek 	}
39830209230bSgjelinek 
39840209230bSgjelinek 	/*
39850209230bSgjelinek 	 * Commit the dynamic configuration but not the pool configuration
39860209230bSgjelinek 	 * file.
39870209230bSgjelinek 	 */
39880209230bSgjelinek 	if (pool_conf_commit(pconf, 1) != PO_SUCCESS)
39890209230bSgjelinek 		res = zerr_pool(pool_err, err_size, Z_POOL);
39900209230bSgjelinek 
39910209230bSgjelinek done:
39920209230bSgjelinek 	(void) pool_conf_close(pconf);
39930209230bSgjelinek 	return (res);
39940209230bSgjelinek }
39950209230bSgjelinek 
39960209230bSgjelinek static int
39970209230bSgjelinek get_running_tmp_pset(pool_conf_t *pconf, pool_t *pool, pool_resource_t *pset,
39980209230bSgjelinek     struct zone_psettab *pset_tab)
39990209230bSgjelinek {
40000209230bSgjelinek 	int nfound = 0;
40010209230bSgjelinek 	pool_elem_t *pe;
40020209230bSgjelinek 	pool_value_t *pv = pool_value_alloc();
40030209230bSgjelinek 	uint64_t val_uint;
40040209230bSgjelinek 
40050209230bSgjelinek 	if (pool != NULL) {
40060209230bSgjelinek 		pe = pool_to_elem(pconf, pool);
40070209230bSgjelinek 		if (pool_get_property(pconf, pe, "pool.importance", pv)
40080209230bSgjelinek 		    != POC_INVAL) {
40090209230bSgjelinek 			int64_t val_int;
40100209230bSgjelinek 
40110209230bSgjelinek 			(void) pool_value_get_int64(pv, &val_int);
40120209230bSgjelinek 			(void) snprintf(pset_tab->zone_importance,
40130209230bSgjelinek 			    sizeof (pset_tab->zone_importance), "%d", val_int);
40140209230bSgjelinek 			nfound++;
40150209230bSgjelinek 		}
40160209230bSgjelinek 	}
40170209230bSgjelinek 
40180209230bSgjelinek 	if (pset != NULL) {
40190209230bSgjelinek 		pe = pool_resource_to_elem(pconf, pset);
40200209230bSgjelinek 		if (pool_get_property(pconf, pe, "pset.min", pv) != POC_INVAL) {
40210209230bSgjelinek 			(void) pool_value_get_uint64(pv, &val_uint);
40220209230bSgjelinek 			(void) snprintf(pset_tab->zone_ncpu_min,
40230209230bSgjelinek 			    sizeof (pset_tab->zone_ncpu_min), "%u", val_uint);
40240209230bSgjelinek 			nfound++;
40250209230bSgjelinek 		}
40260209230bSgjelinek 
40270209230bSgjelinek 		if (pool_get_property(pconf, pe, "pset.max", pv) != POC_INVAL) {
40280209230bSgjelinek 			(void) pool_value_get_uint64(pv, &val_uint);
40290209230bSgjelinek 			(void) snprintf(pset_tab->zone_ncpu_max,
40300209230bSgjelinek 			    sizeof (pset_tab->zone_ncpu_max), "%u", val_uint);
40310209230bSgjelinek 			nfound++;
40320209230bSgjelinek 		}
40330209230bSgjelinek 	}
40340209230bSgjelinek 
40350209230bSgjelinek 	pool_value_free(pv);
40360209230bSgjelinek 
40370209230bSgjelinek 	if (nfound == 3)
40380209230bSgjelinek 		return (PO_SUCCESS);
40390209230bSgjelinek 
40400209230bSgjelinek 	return (PO_FAIL);
40410209230bSgjelinek }
40420209230bSgjelinek 
40430209230bSgjelinek /*
40440209230bSgjelinek  * Determine if a tmp pool is configured and if so, if the configuration is
40450209230bSgjelinek  * still valid or if it has been changed since the tmp pool was created.
40460209230bSgjelinek  * If the tmp pool configuration is no longer valid, delete the tmp pool.
40470209230bSgjelinek  *
40480209230bSgjelinek  * Set *valid=B_TRUE if there is an existing, valid tmp pool configuration.
40490209230bSgjelinek  */
40500209230bSgjelinek static int
40510209230bSgjelinek verify_del_tmp_pool(pool_conf_t *pconf, char *tmp_name, char *pool_err,
40520209230bSgjelinek     int err_size, struct zone_psettab *pset_tab, boolean_t *exists)
40530209230bSgjelinek {
40540209230bSgjelinek 	int res = Z_OK;
40550209230bSgjelinek 	pool_t *pool;
40560209230bSgjelinek 	pool_resource_t *pset;
40570209230bSgjelinek 	struct zone_psettab pset_current;
40580209230bSgjelinek 
40590209230bSgjelinek 	*exists = B_FALSE;
40600209230bSgjelinek 
40610209230bSgjelinek 	if (pool_conf_open(pconf, pool_dynamic_location(), PO_RDWR)
40620209230bSgjelinek 	    != PO_SUCCESS) {
40630209230bSgjelinek 		res = zerr_pool(pool_err, err_size, Z_POOL);
40640209230bSgjelinek 		return (res);
40650209230bSgjelinek 	}
40660209230bSgjelinek 
40670209230bSgjelinek 	pool = pool_get_pool(pconf, tmp_name);
40680209230bSgjelinek 	pset = pool_get_resource(pconf, "pset", tmp_name);
40690209230bSgjelinek 
40700209230bSgjelinek 	if (pool == NULL && pset == NULL) {
40710209230bSgjelinek 		/* no tmp pool configured */
40720209230bSgjelinek 		goto done;
40730209230bSgjelinek 	}
40740209230bSgjelinek 
40750209230bSgjelinek 	/*
40760209230bSgjelinek 	 * If an existing tmp pool for this zone is configured with the proper
40770209230bSgjelinek 	 * settings, then the tmp pool is valid.
40780209230bSgjelinek 	 */
40790209230bSgjelinek 	if (get_running_tmp_pset(pconf, pool, pset, &pset_current)
40800209230bSgjelinek 	    == PO_SUCCESS &&
40810209230bSgjelinek 	    strcmp(pset_tab->zone_ncpu_min,
40820209230bSgjelinek 	    pset_current.zone_ncpu_min) == 0 &&
40830209230bSgjelinek 	    strcmp(pset_tab->zone_ncpu_max,
40840209230bSgjelinek 	    pset_current.zone_ncpu_max) == 0 &&
40850209230bSgjelinek 	    strcmp(pset_tab->zone_importance,
40860209230bSgjelinek 	    pset_current.zone_importance) == 0) {
40870209230bSgjelinek 		*exists = B_TRUE;
40880209230bSgjelinek 
40890209230bSgjelinek 	} else {
40900209230bSgjelinek 		/*
40910209230bSgjelinek 		 * An out-of-date tmp pool configuration exists.  Delete it
40920209230bSgjelinek 		 * so that we can create the correct tmp pool config.
40930209230bSgjelinek 		 */
40940209230bSgjelinek 		if (pset != NULL &&
40950209230bSgjelinek 		    pool_resource_destroy(pconf, pset) != PO_SUCCESS) {
40960209230bSgjelinek 			res = zerr_pool(pool_err, err_size, Z_POOL);
40970209230bSgjelinek 			goto done;
40980209230bSgjelinek 		}
40990209230bSgjelinek 
41000209230bSgjelinek 		if (pool != NULL &&
41010209230bSgjelinek 		    pool_destroy(pconf, pool) != PO_SUCCESS) {
41020209230bSgjelinek 			res = zerr_pool(pool_err, err_size, Z_POOL);
41030209230bSgjelinek 			goto done;
41040209230bSgjelinek 		}
41050209230bSgjelinek 
41060209230bSgjelinek 		/* commit dynamic config */
41070209230bSgjelinek 		if (pool_conf_commit(pconf, 0) != PO_SUCCESS)
41080209230bSgjelinek 			res = zerr_pool(pool_err, err_size, Z_POOL);
41090209230bSgjelinek 	}
41100209230bSgjelinek 
41110209230bSgjelinek done:
41120209230bSgjelinek 	(void) pool_conf_close(pconf);
41130209230bSgjelinek 
41140209230bSgjelinek 	return (res);
41150209230bSgjelinek }
41160209230bSgjelinek 
41170209230bSgjelinek /*
41180209230bSgjelinek  * Destroy any existing tmp pool.
41190209230bSgjelinek  */
41200209230bSgjelinek int
41210209230bSgjelinek zonecfg_destroy_tmp_pool(char *zone_name, char *pool_err, int err_size)
41220209230bSgjelinek {
41230209230bSgjelinek 	int status;
41240209230bSgjelinek 	int res = Z_OK;
41250209230bSgjelinek 	pool_conf_t *pconf;
41260209230bSgjelinek 	pool_t *pool;
41270209230bSgjelinek 	pool_resource_t *pset;
41280209230bSgjelinek 	char tmp_name[MAX_TMP_POOL_NAME];
41290209230bSgjelinek 
41300209230bSgjelinek 	/* if pools not enabled then nothing to do */
41310209230bSgjelinek 	if (pool_get_status(&status) != PO_SUCCESS || status != POOL_ENABLED)
41320209230bSgjelinek 		return (Z_OK);
41330209230bSgjelinek 
41340209230bSgjelinek 	if ((pconf = pool_conf_alloc()) == NULL)
41350209230bSgjelinek 		return (zerr_pool(pool_err, err_size, Z_POOL));
41360209230bSgjelinek 
41370209230bSgjelinek 	(void) snprintf(tmp_name, sizeof (tmp_name), TMP_POOL_NAME, zone_name);
41380209230bSgjelinek 
41390209230bSgjelinek 	if (pool_conf_open(pconf, pool_dynamic_location(), PO_RDWR)
41400209230bSgjelinek 	    != PO_SUCCESS) {
41410209230bSgjelinek 		res = zerr_pool(pool_err, err_size, Z_POOL);
41420209230bSgjelinek 		pool_conf_free(pconf);
41430209230bSgjelinek 		return (res);
41440209230bSgjelinek 	}
41450209230bSgjelinek 
41460209230bSgjelinek 	pool = pool_get_pool(pconf, tmp_name);
41470209230bSgjelinek 	pset = pool_get_resource(pconf, "pset", tmp_name);
41480209230bSgjelinek 
41490209230bSgjelinek 	if (pool == NULL && pset == NULL) {
41500209230bSgjelinek 		/* nothing to destroy, we're done */
41510209230bSgjelinek 		goto done;
41520209230bSgjelinek 	}
41530209230bSgjelinek 
41540209230bSgjelinek 	if (pset != NULL && pool_resource_destroy(pconf, pset) != PO_SUCCESS) {
41550209230bSgjelinek 		res = zerr_pool(pool_err, err_size, Z_POOL);
41560209230bSgjelinek 		goto done;
41570209230bSgjelinek 	}
41580209230bSgjelinek 
41590209230bSgjelinek 	if (pool != NULL && pool_destroy(pconf, pool) != PO_SUCCESS) {
41600209230bSgjelinek 		res = zerr_pool(pool_err, err_size, Z_POOL);
41610209230bSgjelinek 		goto done;
41620209230bSgjelinek 	}
41630209230bSgjelinek 
41640209230bSgjelinek 	/* commit dynamic config */
41650209230bSgjelinek 	if (pool_conf_commit(pconf, 0) != PO_SUCCESS)
41660209230bSgjelinek 		res = zerr_pool(pool_err, err_size, Z_POOL);
41670209230bSgjelinek 
41680209230bSgjelinek done:
41690209230bSgjelinek 	(void) pool_conf_close(pconf);
41700209230bSgjelinek 	pool_conf_free(pconf);
41710209230bSgjelinek 
41720209230bSgjelinek 	return (res);
41730209230bSgjelinek }
41740209230bSgjelinek 
41750209230bSgjelinek /*
41760209230bSgjelinek  * Attempt to bind to a tmp pool for this zone.  If there is no tmp pool
41770209230bSgjelinek  * configured, we just return Z_OK.
41780209230bSgjelinek  *
41790209230bSgjelinek  * We either attempt to create the tmp pool for this zone or rebind to an
41800209230bSgjelinek  * existing tmp pool for this zone.
41810209230bSgjelinek  *
41820209230bSgjelinek  * Rebinding is used when a zone with a tmp pool reboots so that we don't have
41830209230bSgjelinek  * to recreate the tmp pool.  To do this we need to be sure we work correctly
41840209230bSgjelinek  * for the following cases:
41850209230bSgjelinek  *
41860209230bSgjelinek  *	- there is an existing, properly configured tmp pool.
41870209230bSgjelinek  *	- zonecfg added tmp pool after zone was booted, must now create.
41880209230bSgjelinek  *	- zonecfg updated tmp pool config after zone was booted, in this case
41890209230bSgjelinek  *	  we destroy the old tmp pool and create a new one.
41900209230bSgjelinek  */
41910209230bSgjelinek int
41920209230bSgjelinek zonecfg_bind_tmp_pool(zone_dochandle_t handle, zoneid_t zoneid, char *pool_err,
41930209230bSgjelinek     int err_size)
41940209230bSgjelinek {
41950209230bSgjelinek 	struct zone_psettab pset_tab;
41960209230bSgjelinek 	int err;
41970209230bSgjelinek 	int status;
41980209230bSgjelinek 	pool_conf_t *pconf;
41990209230bSgjelinek 	boolean_t exists;
42000209230bSgjelinek 	char zone_name[ZONENAME_MAX];
42010209230bSgjelinek 	char tmp_name[MAX_TMP_POOL_NAME];
42020209230bSgjelinek 
42030209230bSgjelinek 	(void) getzonenamebyid(zoneid, zone_name, sizeof (zone_name));
42040209230bSgjelinek 
42050209230bSgjelinek 	err = zonecfg_lookup_pset(handle, &pset_tab);
42060209230bSgjelinek 
42070209230bSgjelinek 	/* if no temporary pool configured, we're done */
42080209230bSgjelinek 	if (err == Z_NO_ENTRY)
42090209230bSgjelinek 		return (Z_OK);
42100209230bSgjelinek 
42110209230bSgjelinek 	/*
42120209230bSgjelinek 	 * importance might not have a value but we need to validate it here,
42130209230bSgjelinek 	 * so set the default.
42140209230bSgjelinek 	 */
42150209230bSgjelinek 	if (pset_tab.zone_importance[0] == '\0')
42160209230bSgjelinek 		(void) strlcpy(pset_tab.zone_importance, "1",
42170209230bSgjelinek 		    sizeof (pset_tab.zone_importance));
42180209230bSgjelinek 
42190209230bSgjelinek 	/* if pools not enabled, enable them now */
42200209230bSgjelinek 	if (pool_get_status(&status) != PO_SUCCESS || status != POOL_ENABLED) {
42210209230bSgjelinek 		if (pool_set_status(POOL_ENABLED) != PO_SUCCESS)
42220209230bSgjelinek 			return (Z_POOL_ENABLE);
42230209230bSgjelinek 	}
42240209230bSgjelinek 
42250209230bSgjelinek 	if ((pconf = pool_conf_alloc()) == NULL)
42260209230bSgjelinek 		return (zerr_pool(pool_err, err_size, Z_POOL));
42270209230bSgjelinek 
42280209230bSgjelinek 	(void) snprintf(tmp_name, sizeof (tmp_name), TMP_POOL_NAME, zone_name);
42290209230bSgjelinek 
42300209230bSgjelinek 	/*
42310209230bSgjelinek 	 * Check if a valid tmp pool/pset already exists.  If so, we just
42320209230bSgjelinek 	 * reuse it.
42330209230bSgjelinek 	 */
42340209230bSgjelinek 	if ((err = verify_del_tmp_pool(pconf, tmp_name, pool_err, err_size,
42350209230bSgjelinek 	    &pset_tab, &exists)) != Z_OK) {
42360209230bSgjelinek 		pool_conf_free(pconf);
42370209230bSgjelinek 		return (err);
42380209230bSgjelinek 	}
42390209230bSgjelinek 
42400209230bSgjelinek 	if (!exists)
42410209230bSgjelinek 		err = create_tmp_pool(pool_err, err_size, pconf, tmp_name,
42420209230bSgjelinek 		    &pset_tab);
42430209230bSgjelinek 
42440209230bSgjelinek 	pool_conf_free(pconf);
42450209230bSgjelinek 
42460209230bSgjelinek 	if (err != Z_OK)
42470209230bSgjelinek 		return (err);
42480209230bSgjelinek 
42490209230bSgjelinek 	/* Bind the zone to the pool. */
42500209230bSgjelinek 	if (pool_set_binding(tmp_name, P_ZONEID, zoneid) != PO_SUCCESS)
42510209230bSgjelinek 		return (zerr_pool(pool_err, err_size, Z_POOL_BIND));
42520209230bSgjelinek 
42530209230bSgjelinek 	return (Z_OK);
42540209230bSgjelinek }
42550209230bSgjelinek 
42560209230bSgjelinek /*
42570209230bSgjelinek  * Attempt to bind to a permanent pool for this zone.  If there is no
42580209230bSgjelinek  * permanent pool configured, we just return Z_OK.
42590209230bSgjelinek  */
42600209230bSgjelinek int
42610209230bSgjelinek zonecfg_bind_pool(zone_dochandle_t handle, zoneid_t zoneid, char *pool_err,
42620209230bSgjelinek     int err_size)
42630209230bSgjelinek {
42640209230bSgjelinek 	pool_conf_t *poolconf;
42650209230bSgjelinek 	pool_t *pool;
42660209230bSgjelinek 	char poolname[MAXPATHLEN];
42670209230bSgjelinek 	int status;
42680209230bSgjelinek 	int error;
42690209230bSgjelinek 
42700209230bSgjelinek 	/*
42710209230bSgjelinek 	 * Find the pool mentioned in the zone configuration, and bind to it.
42720209230bSgjelinek 	 */
42730209230bSgjelinek 	error = zonecfg_get_pool(handle, poolname, sizeof (poolname));
42740209230bSgjelinek 	if (error == Z_NO_ENTRY || (error == Z_OK && strlen(poolname) == 0)) {
42750209230bSgjelinek 		/*
42760209230bSgjelinek 		 * The property is not set on the zone, so the pool
42770209230bSgjelinek 		 * should be bound to the default pool.  But that's
42780209230bSgjelinek 		 * already done by the kernel, so we can just return.
42790209230bSgjelinek 		 */
42800209230bSgjelinek 		return (Z_OK);
42810209230bSgjelinek 	}
42820209230bSgjelinek 	if (error != Z_OK) {
42830209230bSgjelinek 		/*
42840209230bSgjelinek 		 * Not an error, even though it shouldn't be happening.
42850209230bSgjelinek 		 */
42860209230bSgjelinek 		return (Z_OK);
42870209230bSgjelinek 	}
42880209230bSgjelinek 	/*
42890209230bSgjelinek 	 * Don't do anything if pools aren't enabled.
42900209230bSgjelinek 	 */
42910209230bSgjelinek 	if (pool_get_status(&status) != PO_SUCCESS || status != POOL_ENABLED)
42920209230bSgjelinek 		return (Z_POOLS_NOT_ACTIVE);
42930209230bSgjelinek 
42940209230bSgjelinek 	/*
42950209230bSgjelinek 	 * Try to provide a sane error message if the requested pool doesn't
42960209230bSgjelinek 	 * exist.
42970209230bSgjelinek 	 */
42980209230bSgjelinek 	if ((poolconf = pool_conf_alloc()) == NULL)
42990209230bSgjelinek 		return (zerr_pool(pool_err, err_size, Z_POOL));
43000209230bSgjelinek 
43010209230bSgjelinek 	if (pool_conf_open(poolconf, pool_dynamic_location(), PO_RDONLY) !=
43020209230bSgjelinek 	    PO_SUCCESS) {
43030209230bSgjelinek 		pool_conf_free(poolconf);
43040209230bSgjelinek 		return (zerr_pool(pool_err, err_size, Z_POOL));
43050209230bSgjelinek 	}
43060209230bSgjelinek 	pool = pool_get_pool(poolconf, poolname);
43070209230bSgjelinek 	(void) pool_conf_close(poolconf);
43080209230bSgjelinek 	pool_conf_free(poolconf);
43090209230bSgjelinek 	if (pool == NULL)
43100209230bSgjelinek 		return (Z_NO_POOL);
43110209230bSgjelinek 
43120209230bSgjelinek 	/*
43130209230bSgjelinek 	 * Bind the zone to the pool.
43140209230bSgjelinek 	 */
43150209230bSgjelinek 	if (pool_set_binding(poolname, P_ZONEID, zoneid) != PO_SUCCESS) {
43160209230bSgjelinek 		/* if bind fails, return poolname for the error msg */
43170209230bSgjelinek 		(void) strlcpy(pool_err, poolname, err_size);
43180209230bSgjelinek 		return (Z_POOL_BIND);
43190209230bSgjelinek 	}
43200209230bSgjelinek 
43210209230bSgjelinek 	return (Z_OK);
43220209230bSgjelinek }
43230209230bSgjelinek 
43240209230bSgjelinek 
43250209230bSgjelinek static boolean_t
43260209230bSgjelinek svc_enabled(char *svc_name)
43270209230bSgjelinek {
43280209230bSgjelinek 	scf_simple_prop_t	*prop;
43290209230bSgjelinek 	boolean_t		found = B_FALSE;
43300209230bSgjelinek 
43310209230bSgjelinek 	prop = scf_simple_prop_get(NULL, svc_name, SCF_PG_GENERAL,
43320209230bSgjelinek 	    SCF_PROPERTY_ENABLED);
43330209230bSgjelinek 
43340209230bSgjelinek 	if (scf_simple_prop_numvalues(prop) == 1 &&
43350209230bSgjelinek 	    *scf_simple_prop_next_boolean(prop) != 0)
43360209230bSgjelinek 		found = B_TRUE;
43370209230bSgjelinek 
43380209230bSgjelinek 	scf_simple_prop_free(prop);
43390209230bSgjelinek 
43400209230bSgjelinek 	return (found);
43410209230bSgjelinek }
43420209230bSgjelinek 
43430209230bSgjelinek /*
43440209230bSgjelinek  * If the zone has capped-memory, make sure the rcap service is enabled.
43450209230bSgjelinek  */
43460209230bSgjelinek int
43470209230bSgjelinek zonecfg_enable_rcapd(char *err, int size)
43480209230bSgjelinek {
43490209230bSgjelinek 	if (!svc_enabled(RCAP_SERVICE) &&
43500209230bSgjelinek 	    smf_enable_instance(RCAP_SERVICE, 0) == -1) {
43510209230bSgjelinek 		(void) strlcpy(err, scf_strerror(scf_error()), size);
43520209230bSgjelinek 		return (Z_SYSTEM);
43530209230bSgjelinek 	}
43540209230bSgjelinek 
43550209230bSgjelinek 	return (Z_OK);
43560209230bSgjelinek }
43570209230bSgjelinek 
43580209230bSgjelinek /*
43590209230bSgjelinek  * Return true if pset has cpu range specified and poold is not enabled.
43600209230bSgjelinek  */
43610209230bSgjelinek boolean_t
43620209230bSgjelinek zonecfg_warn_poold(zone_dochandle_t handle)
43630209230bSgjelinek {
43640209230bSgjelinek 	struct zone_psettab pset_tab;
43650209230bSgjelinek 	int min, max;
43660209230bSgjelinek 	int err;
43670209230bSgjelinek 
43680209230bSgjelinek 	err = zonecfg_lookup_pset(handle, &pset_tab);
43690209230bSgjelinek 
43700209230bSgjelinek 	/* if no temporary pool configured, we're done */
43710209230bSgjelinek 	if (err == Z_NO_ENTRY)
43720209230bSgjelinek 		return (B_FALSE);
43730209230bSgjelinek 
43740209230bSgjelinek 	min = atoi(pset_tab.zone_ncpu_min);
43750209230bSgjelinek 	max = atoi(pset_tab.zone_ncpu_max);
43760209230bSgjelinek 
43770209230bSgjelinek 	/* range not specified, no need for poold */
43780209230bSgjelinek 	if (min == max)
43790209230bSgjelinek 		return (B_FALSE);
43800209230bSgjelinek 
43810209230bSgjelinek 	/* we have a range, check if poold service is enabled */
43820209230bSgjelinek 	if (svc_enabled(POOLD_SERVICE))
43830209230bSgjelinek 		return (B_FALSE);
43840209230bSgjelinek 
43850209230bSgjelinek 	return (B_TRUE);
43860209230bSgjelinek }
43870209230bSgjelinek 
43880209230bSgjelinek static int
43890209230bSgjelinek get_pool_sched_class(char *poolname, char *class, int clsize)
43900209230bSgjelinek {
43910209230bSgjelinek 	int status;
43920209230bSgjelinek 	pool_conf_t *poolconf;
43930209230bSgjelinek 	pool_t *pool;
43940209230bSgjelinek 	pool_elem_t *pe;
43950209230bSgjelinek 	pool_value_t *pv = pool_value_alloc();
43960209230bSgjelinek 	const char *sched_str;
43970209230bSgjelinek 
43980209230bSgjelinek 	if (pool_get_status(&status) != PO_SUCCESS || status != POOL_ENABLED)
43990209230bSgjelinek 		return (Z_NO_POOL);
44000209230bSgjelinek 
44010209230bSgjelinek 	if ((poolconf = pool_conf_alloc()) == NULL)
44020209230bSgjelinek 		return (Z_NO_POOL);
44030209230bSgjelinek 
44040209230bSgjelinek 	if (pool_conf_open(poolconf, pool_dynamic_location(), PO_RDONLY) !=
44050209230bSgjelinek 	    PO_SUCCESS) {
44060209230bSgjelinek 		pool_conf_free(poolconf);
44070209230bSgjelinek 		return (Z_NO_POOL);
44080209230bSgjelinek 	}
44090209230bSgjelinek 
44100209230bSgjelinek 	if ((pool = pool_get_pool(poolconf, poolname)) == NULL) {
44110209230bSgjelinek 		(void) pool_conf_close(poolconf);
44120209230bSgjelinek 		pool_conf_free(poolconf);
44130209230bSgjelinek 		return (Z_NO_POOL);
44140209230bSgjelinek 	}
44150209230bSgjelinek 
44160209230bSgjelinek 	pe = pool_to_elem(poolconf, pool);
44170209230bSgjelinek 	if (pool_get_property(poolconf, pe, "pool.scheduler", pv)
44180209230bSgjelinek 	    != POC_INVAL) {
44190209230bSgjelinek 		(void) pool_value_get_string(pv, &sched_str);
44200209230bSgjelinek 		if (strlcpy(class, sched_str, clsize) >= clsize)
44210209230bSgjelinek 			return (Z_TOO_BIG);
44220209230bSgjelinek 	}
44230209230bSgjelinek 
44240209230bSgjelinek 	(void) pool_conf_close(poolconf);
44250209230bSgjelinek 	pool_conf_free(poolconf);
44260209230bSgjelinek 	return (Z_OK);
44270209230bSgjelinek }
44280209230bSgjelinek 
44290209230bSgjelinek /*
44300209230bSgjelinek  * Get the default scheduling class for the zone.  This will either be the
44310209230bSgjelinek  * class set on the zone's pool or the system default scheduling class.
44320209230bSgjelinek  */
44330209230bSgjelinek int
44340209230bSgjelinek zonecfg_get_dflt_sched_class(zone_dochandle_t handle, char *class, int clsize)
44350209230bSgjelinek {
44360209230bSgjelinek 	char poolname[MAXPATHLEN];
44370209230bSgjelinek 
44380209230bSgjelinek 	if (zonecfg_get_pool(handle, poolname, sizeof (poolname)) == Z_OK) {
44390209230bSgjelinek 		/* check if the zone's pool specified a sched class */
44400209230bSgjelinek 		if (get_pool_sched_class(poolname, class, clsize) == Z_OK)
44410209230bSgjelinek 			return (Z_OK);
44420209230bSgjelinek 	}
44430209230bSgjelinek 
44440209230bSgjelinek 	if (priocntl(0, 0, PC_GETDFLCL, class, (uint64_t)clsize) == -1)
44450209230bSgjelinek 		return (Z_TOO_BIG);
44460209230bSgjelinek 
44470209230bSgjelinek 	return (Z_OK);
44480209230bSgjelinek }
44490209230bSgjelinek 
44507c478bd9Sstevel@tonic-gate int
44517c478bd9Sstevel@tonic-gate zonecfg_setfsent(zone_dochandle_t handle)
44527c478bd9Sstevel@tonic-gate {
44537c478bd9Sstevel@tonic-gate 	return (zonecfg_setent(handle));
44547c478bd9Sstevel@tonic-gate }
44557c478bd9Sstevel@tonic-gate 
44567c478bd9Sstevel@tonic-gate int
44577c478bd9Sstevel@tonic-gate zonecfg_getfsent(zone_dochandle_t handle, struct zone_fstab *tabptr)
44587c478bd9Sstevel@tonic-gate {
44597c478bd9Sstevel@tonic-gate 	xmlNodePtr cur, options;
44607c478bd9Sstevel@tonic-gate 	char options_str[MAX_MNTOPT_STR];
44617c478bd9Sstevel@tonic-gate 	int err;
44627c478bd9Sstevel@tonic-gate 
44637c478bd9Sstevel@tonic-gate 	if (handle == NULL)
44647c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
44657c478bd9Sstevel@tonic-gate 
44667c478bd9Sstevel@tonic-gate 	if ((cur = handle->zone_dh_cur) == NULL)
44677c478bd9Sstevel@tonic-gate 		return (Z_NO_ENTRY);
44687c478bd9Sstevel@tonic-gate 
44697c478bd9Sstevel@tonic-gate 	for (; cur != NULL; cur = cur->next)
44707c478bd9Sstevel@tonic-gate 		if (!xmlStrcmp(cur->name, DTD_ELEM_FS))
44717c478bd9Sstevel@tonic-gate 			break;
44727c478bd9Sstevel@tonic-gate 	if (cur == NULL) {
44737c478bd9Sstevel@tonic-gate 		handle->zone_dh_cur = handle->zone_dh_top;
44747c478bd9Sstevel@tonic-gate 		return (Z_NO_ENTRY);
44757c478bd9Sstevel@tonic-gate 	}
44767c478bd9Sstevel@tonic-gate 
44777c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_SPECIAL, tabptr->zone_fs_special,
44787c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_fs_special))) != Z_OK) {
44797c478bd9Sstevel@tonic-gate 		handle->zone_dh_cur = handle->zone_dh_top;
44807c478bd9Sstevel@tonic-gate 		return (err);
44817c478bd9Sstevel@tonic-gate 	}
44827c478bd9Sstevel@tonic-gate 
44837c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_RAW, tabptr->zone_fs_raw,
44847c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_fs_raw))) != Z_OK) {
44857c478bd9Sstevel@tonic-gate 		handle->zone_dh_cur = handle->zone_dh_top;
44867c478bd9Sstevel@tonic-gate 		return (err);
44877c478bd9Sstevel@tonic-gate 	}
44887c478bd9Sstevel@tonic-gate 
44897c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_DIR, tabptr->zone_fs_dir,
44907c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_fs_dir))) != Z_OK) {
44917c478bd9Sstevel@tonic-gate 		handle->zone_dh_cur = handle->zone_dh_top;
44927c478bd9Sstevel@tonic-gate 		return (err);
44937c478bd9Sstevel@tonic-gate 	}
44947c478bd9Sstevel@tonic-gate 
44957c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_TYPE, tabptr->zone_fs_type,
44967c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_fs_type))) != Z_OK) {
44977c478bd9Sstevel@tonic-gate 		handle->zone_dh_cur = handle->zone_dh_top;
44987c478bd9Sstevel@tonic-gate 		return (err);
44997c478bd9Sstevel@tonic-gate 	}
45007c478bd9Sstevel@tonic-gate 
45017c478bd9Sstevel@tonic-gate 	/* OK for options to be NULL */
45027c478bd9Sstevel@tonic-gate 	tabptr->zone_fs_options = NULL;
45037c478bd9Sstevel@tonic-gate 	for (options = cur->xmlChildrenNode; options != NULL;
45047c478bd9Sstevel@tonic-gate 	    options = options->next) {
45057c478bd9Sstevel@tonic-gate 		if (fetchprop(options, DTD_ATTR_NAME, options_str,
45067c478bd9Sstevel@tonic-gate 		    sizeof (options_str)) != Z_OK)
45077c478bd9Sstevel@tonic-gate 			break;
45087c478bd9Sstevel@tonic-gate 		if (zonecfg_add_fs_option(tabptr, options_str) != Z_OK)
45097c478bd9Sstevel@tonic-gate 			break;
45107c478bd9Sstevel@tonic-gate 	}
45117c478bd9Sstevel@tonic-gate 
45127c478bd9Sstevel@tonic-gate 	handle->zone_dh_cur = cur->next;
45137c478bd9Sstevel@tonic-gate 	return (Z_OK);
45147c478bd9Sstevel@tonic-gate }
45157c478bd9Sstevel@tonic-gate 
45167c478bd9Sstevel@tonic-gate int
45177c478bd9Sstevel@tonic-gate zonecfg_endfsent(zone_dochandle_t handle)
45187c478bd9Sstevel@tonic-gate {
45197c478bd9Sstevel@tonic-gate 	return (zonecfg_endent(handle));
45207c478bd9Sstevel@tonic-gate }
45217c478bd9Sstevel@tonic-gate 
45227c478bd9Sstevel@tonic-gate int
45237c478bd9Sstevel@tonic-gate zonecfg_setipdent(zone_dochandle_t handle)
45247c478bd9Sstevel@tonic-gate {
45257c478bd9Sstevel@tonic-gate 	return (zonecfg_setent(handle));
45267c478bd9Sstevel@tonic-gate }
45277c478bd9Sstevel@tonic-gate 
45287c478bd9Sstevel@tonic-gate int
45297c478bd9Sstevel@tonic-gate zonecfg_getipdent(zone_dochandle_t handle, struct zone_fstab *tabptr)
45307c478bd9Sstevel@tonic-gate {
45317c478bd9Sstevel@tonic-gate 	xmlNodePtr cur;
45327c478bd9Sstevel@tonic-gate 	int err;
45337c478bd9Sstevel@tonic-gate 
45347c478bd9Sstevel@tonic-gate 	if (handle == NULL)
45357c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
45367c478bd9Sstevel@tonic-gate 
45377c478bd9Sstevel@tonic-gate 	if ((cur = handle->zone_dh_cur) == NULL)
45387c478bd9Sstevel@tonic-gate 		return (Z_NO_ENTRY);
45397c478bd9Sstevel@tonic-gate 
45407c478bd9Sstevel@tonic-gate 	for (; cur != NULL; cur = cur->next)
45417c478bd9Sstevel@tonic-gate 		if (!xmlStrcmp(cur->name, DTD_ELEM_IPD))
45427c478bd9Sstevel@tonic-gate 			break;
45437c478bd9Sstevel@tonic-gate 	if (cur == NULL) {
45447c478bd9Sstevel@tonic-gate 		handle->zone_dh_cur = handle->zone_dh_top;
45457c478bd9Sstevel@tonic-gate 		return (Z_NO_ENTRY);
45467c478bd9Sstevel@tonic-gate 	}
45477c478bd9Sstevel@tonic-gate 
45487c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_DIR, tabptr->zone_fs_dir,
45497c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_fs_dir))) != Z_OK) {
45507c478bd9Sstevel@tonic-gate 		handle->zone_dh_cur = handle->zone_dh_top;
45517c478bd9Sstevel@tonic-gate 		return (err);
45527c478bd9Sstevel@tonic-gate 	}
45537c478bd9Sstevel@tonic-gate 
45547c478bd9Sstevel@tonic-gate 	handle->zone_dh_cur = cur->next;
45557c478bd9Sstevel@tonic-gate 	return (Z_OK);
45567c478bd9Sstevel@tonic-gate }
45577c478bd9Sstevel@tonic-gate 
45587c478bd9Sstevel@tonic-gate int
45597c478bd9Sstevel@tonic-gate zonecfg_endipdent(zone_dochandle_t handle)
45607c478bd9Sstevel@tonic-gate {
45617c478bd9Sstevel@tonic-gate 	return (zonecfg_endent(handle));
45627c478bd9Sstevel@tonic-gate }
45637c478bd9Sstevel@tonic-gate 
45647c478bd9Sstevel@tonic-gate int
45657c478bd9Sstevel@tonic-gate zonecfg_setnwifent(zone_dochandle_t handle)
45667c478bd9Sstevel@tonic-gate {
45677c478bd9Sstevel@tonic-gate 	return (zonecfg_setent(handle));
45687c478bd9Sstevel@tonic-gate }
45697c478bd9Sstevel@tonic-gate 
45707c478bd9Sstevel@tonic-gate int
45717c478bd9Sstevel@tonic-gate zonecfg_getnwifent(zone_dochandle_t handle, struct zone_nwiftab *tabptr)
45727c478bd9Sstevel@tonic-gate {
45737c478bd9Sstevel@tonic-gate 	xmlNodePtr cur;
45747c478bd9Sstevel@tonic-gate 	int err;
45757c478bd9Sstevel@tonic-gate 
45767c478bd9Sstevel@tonic-gate 	if (handle == NULL)
45777c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
45787c478bd9Sstevel@tonic-gate 
45797c478bd9Sstevel@tonic-gate 	if ((cur = handle->zone_dh_cur) == NULL)
45807c478bd9Sstevel@tonic-gate 		return (Z_NO_ENTRY);
45817c478bd9Sstevel@tonic-gate 
45827c478bd9Sstevel@tonic-gate 	for (; cur != NULL; cur = cur->next)
45837c478bd9Sstevel@tonic-gate 		if (!xmlStrcmp(cur->name, DTD_ELEM_NET))
45847c478bd9Sstevel@tonic-gate 			break;
45857c478bd9Sstevel@tonic-gate 	if (cur == NULL) {
45867c478bd9Sstevel@tonic-gate 		handle->zone_dh_cur = handle->zone_dh_top;
45877c478bd9Sstevel@tonic-gate 		return (Z_NO_ENTRY);
45887c478bd9Sstevel@tonic-gate 	}
45897c478bd9Sstevel@tonic-gate 
45907c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_ADDRESS, tabptr->zone_nwif_address,
45917c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_nwif_address))) != Z_OK) {
45927c478bd9Sstevel@tonic-gate 		handle->zone_dh_cur = handle->zone_dh_top;
45937c478bd9Sstevel@tonic-gate 		return (err);
45947c478bd9Sstevel@tonic-gate 	}
45957c478bd9Sstevel@tonic-gate 
45967c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_PHYSICAL, tabptr->zone_nwif_physical,
45977c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_nwif_physical))) != Z_OK) {
45987c478bd9Sstevel@tonic-gate 		handle->zone_dh_cur = handle->zone_dh_top;
45997c478bd9Sstevel@tonic-gate 		return (err);
46007c478bd9Sstevel@tonic-gate 	}
46017c478bd9Sstevel@tonic-gate 
4602de860bd9Sgfaden 	if ((err = fetchprop(cur, DTD_ATTR_DEFROUTER,
4603de860bd9Sgfaden 	    tabptr->zone_nwif_defrouter,
4604de860bd9Sgfaden 	    sizeof (tabptr->zone_nwif_defrouter))) != Z_OK) {
4605de860bd9Sgfaden 		handle->zone_dh_cur = handle->zone_dh_top;
4606de860bd9Sgfaden 		return (err);
4607de860bd9Sgfaden 	}
4608de860bd9Sgfaden 
46097c478bd9Sstevel@tonic-gate 	handle->zone_dh_cur = cur->next;
46107c478bd9Sstevel@tonic-gate 	return (Z_OK);
46117c478bd9Sstevel@tonic-gate }
46127c478bd9Sstevel@tonic-gate 
46137c478bd9Sstevel@tonic-gate int
46147c478bd9Sstevel@tonic-gate zonecfg_endnwifent(zone_dochandle_t handle)
46157c478bd9Sstevel@tonic-gate {
46167c478bd9Sstevel@tonic-gate 	return (zonecfg_endent(handle));
46177c478bd9Sstevel@tonic-gate }
46187c478bd9Sstevel@tonic-gate 
46197c478bd9Sstevel@tonic-gate int
46207c478bd9Sstevel@tonic-gate zonecfg_setdevent(zone_dochandle_t handle)
46217c478bd9Sstevel@tonic-gate {
46227c478bd9Sstevel@tonic-gate 	return (zonecfg_setent(handle));
46237c478bd9Sstevel@tonic-gate }
46247c478bd9Sstevel@tonic-gate 
46257c478bd9Sstevel@tonic-gate int
46267c478bd9Sstevel@tonic-gate zonecfg_getdevent(zone_dochandle_t handle, struct zone_devtab *tabptr)
46277c478bd9Sstevel@tonic-gate {
46287c478bd9Sstevel@tonic-gate 	xmlNodePtr cur;
46297c478bd9Sstevel@tonic-gate 	int err;
46307c478bd9Sstevel@tonic-gate 
46317c478bd9Sstevel@tonic-gate 	if (handle == NULL)
46327c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
46337c478bd9Sstevel@tonic-gate 
46347c478bd9Sstevel@tonic-gate 	if ((cur = handle->zone_dh_cur) == NULL)
46357c478bd9Sstevel@tonic-gate 		return (Z_NO_ENTRY);
46367c478bd9Sstevel@tonic-gate 
46377c478bd9Sstevel@tonic-gate 	for (; cur != NULL; cur = cur->next)
46387c478bd9Sstevel@tonic-gate 		if (!xmlStrcmp(cur->name, DTD_ELEM_DEVICE))
46397c478bd9Sstevel@tonic-gate 			break;
46407c478bd9Sstevel@tonic-gate 	if (cur == NULL) {
46417c478bd9Sstevel@tonic-gate 		handle->zone_dh_cur = handle->zone_dh_top;
46427c478bd9Sstevel@tonic-gate 		return (Z_NO_ENTRY);
46437c478bd9Sstevel@tonic-gate 	}
46447c478bd9Sstevel@tonic-gate 
46457c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_MATCH, tabptr->zone_dev_match,
46467c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_dev_match))) != Z_OK) {
46477c478bd9Sstevel@tonic-gate 		handle->zone_dh_cur = handle->zone_dh_top;
46487c478bd9Sstevel@tonic-gate 		return (err);
46497c478bd9Sstevel@tonic-gate 	}
46507c478bd9Sstevel@tonic-gate 
46517c478bd9Sstevel@tonic-gate 	handle->zone_dh_cur = cur->next;
46527c478bd9Sstevel@tonic-gate 	return (Z_OK);
46537c478bd9Sstevel@tonic-gate }
46547c478bd9Sstevel@tonic-gate 
46557c478bd9Sstevel@tonic-gate int
46567c478bd9Sstevel@tonic-gate zonecfg_enddevent(zone_dochandle_t handle)
46577c478bd9Sstevel@tonic-gate {
46587c478bd9Sstevel@tonic-gate 	return (zonecfg_endent(handle));
46597c478bd9Sstevel@tonic-gate }
46607c478bd9Sstevel@tonic-gate 
46617c478bd9Sstevel@tonic-gate int
46627c478bd9Sstevel@tonic-gate zonecfg_setrctlent(zone_dochandle_t handle)
46637c478bd9Sstevel@tonic-gate {
46647c478bd9Sstevel@tonic-gate 	return (zonecfg_setent(handle));
46657c478bd9Sstevel@tonic-gate }
46667c478bd9Sstevel@tonic-gate 
46677c478bd9Sstevel@tonic-gate int
46687c478bd9Sstevel@tonic-gate zonecfg_getrctlent(zone_dochandle_t handle, struct zone_rctltab *tabptr)
46697c478bd9Sstevel@tonic-gate {
46707c478bd9Sstevel@tonic-gate 	xmlNodePtr cur, val;
46717c478bd9Sstevel@tonic-gate 	struct zone_rctlvaltab *valptr;
46727c478bd9Sstevel@tonic-gate 	int err;
46737c478bd9Sstevel@tonic-gate 
46747c478bd9Sstevel@tonic-gate 	if (handle == NULL)
46757c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
46767c478bd9Sstevel@tonic-gate 
46777c478bd9Sstevel@tonic-gate 	if ((cur = handle->zone_dh_cur) == NULL)
46787c478bd9Sstevel@tonic-gate 		return (Z_NO_ENTRY);
46797c478bd9Sstevel@tonic-gate 
46807c478bd9Sstevel@tonic-gate 	for (; cur != NULL; cur = cur->next)
46817c478bd9Sstevel@tonic-gate 		if (!xmlStrcmp(cur->name, DTD_ELEM_RCTL))
46827c478bd9Sstevel@tonic-gate 			break;
46837c478bd9Sstevel@tonic-gate 	if (cur == NULL) {
46847c478bd9Sstevel@tonic-gate 		handle->zone_dh_cur = handle->zone_dh_top;
46857c478bd9Sstevel@tonic-gate 		return (Z_NO_ENTRY);
46867c478bd9Sstevel@tonic-gate 	}
46877c478bd9Sstevel@tonic-gate 
46887c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_NAME, tabptr->zone_rctl_name,
46897c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_rctl_name))) != Z_OK) {
46907c478bd9Sstevel@tonic-gate 		handle->zone_dh_cur = handle->zone_dh_top;
46917c478bd9Sstevel@tonic-gate 		return (err);
46927c478bd9Sstevel@tonic-gate 	}
46937c478bd9Sstevel@tonic-gate 
46947c478bd9Sstevel@tonic-gate 	tabptr->zone_rctl_valptr = NULL;
46957c478bd9Sstevel@tonic-gate 	for (val = cur->xmlChildrenNode; val != NULL; val = val->next) {
46967c478bd9Sstevel@tonic-gate 		valptr = (struct zone_rctlvaltab *)malloc(
46977c478bd9Sstevel@tonic-gate 		    sizeof (struct zone_rctlvaltab));
46987c478bd9Sstevel@tonic-gate 		if (valptr == NULL)
46997c478bd9Sstevel@tonic-gate 			return (Z_NOMEM);
47007c478bd9Sstevel@tonic-gate 		if (fetchprop(val, DTD_ATTR_PRIV, valptr->zone_rctlval_priv,
47017c478bd9Sstevel@tonic-gate 		    sizeof (valptr->zone_rctlval_priv)) != Z_OK)
47027c478bd9Sstevel@tonic-gate 			break;
47037c478bd9Sstevel@tonic-gate 		if (fetchprop(val, DTD_ATTR_LIMIT, valptr->zone_rctlval_limit,
47047c478bd9Sstevel@tonic-gate 		    sizeof (valptr->zone_rctlval_limit)) != Z_OK)
47057c478bd9Sstevel@tonic-gate 			break;
47067c478bd9Sstevel@tonic-gate 		if (fetchprop(val, DTD_ATTR_ACTION, valptr->zone_rctlval_action,
47077c478bd9Sstevel@tonic-gate 		    sizeof (valptr->zone_rctlval_action)) != Z_OK)
47087c478bd9Sstevel@tonic-gate 			break;
47097c478bd9Sstevel@tonic-gate 		if (zonecfg_add_rctl_value(tabptr, valptr) != Z_OK)
47107c478bd9Sstevel@tonic-gate 			break;
47117c478bd9Sstevel@tonic-gate 	}
47127c478bd9Sstevel@tonic-gate 
47137c478bd9Sstevel@tonic-gate 	handle->zone_dh_cur = cur->next;
47147c478bd9Sstevel@tonic-gate 	return (Z_OK);
47157c478bd9Sstevel@tonic-gate }
47167c478bd9Sstevel@tonic-gate 
47177c478bd9Sstevel@tonic-gate int
47187c478bd9Sstevel@tonic-gate zonecfg_endrctlent(zone_dochandle_t handle)
47197c478bd9Sstevel@tonic-gate {
47207c478bd9Sstevel@tonic-gate 	return (zonecfg_endent(handle));
47217c478bd9Sstevel@tonic-gate }
47227c478bd9Sstevel@tonic-gate 
47237c478bd9Sstevel@tonic-gate int
47247c478bd9Sstevel@tonic-gate zonecfg_setattrent(zone_dochandle_t handle)
47257c478bd9Sstevel@tonic-gate {
47267c478bd9Sstevel@tonic-gate 	return (zonecfg_setent(handle));
47277c478bd9Sstevel@tonic-gate }
47287c478bd9Sstevel@tonic-gate 
47297c478bd9Sstevel@tonic-gate int
47307c478bd9Sstevel@tonic-gate zonecfg_getattrent(zone_dochandle_t handle, struct zone_attrtab *tabptr)
47317c478bd9Sstevel@tonic-gate {
47327c478bd9Sstevel@tonic-gate 	xmlNodePtr cur;
47337c478bd9Sstevel@tonic-gate 	int err;
47347c478bd9Sstevel@tonic-gate 
47357c478bd9Sstevel@tonic-gate 	if (handle == NULL)
47367c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
47377c478bd9Sstevel@tonic-gate 
47387c478bd9Sstevel@tonic-gate 	if ((cur = handle->zone_dh_cur) == NULL)
47397c478bd9Sstevel@tonic-gate 		return (Z_NO_ENTRY);
47407c478bd9Sstevel@tonic-gate 
47417c478bd9Sstevel@tonic-gate 	for (; cur != NULL; cur = cur->next)
47427c478bd9Sstevel@tonic-gate 		if (!xmlStrcmp(cur->name, DTD_ELEM_ATTR))
47437c478bd9Sstevel@tonic-gate 			break;
47447c478bd9Sstevel@tonic-gate 	if (cur == NULL) {
47457c478bd9Sstevel@tonic-gate 		handle->zone_dh_cur = handle->zone_dh_top;
47467c478bd9Sstevel@tonic-gate 		return (Z_NO_ENTRY);
47477c478bd9Sstevel@tonic-gate 	}
47487c478bd9Sstevel@tonic-gate 
47497c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_NAME, tabptr->zone_attr_name,
47507c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_attr_name))) != Z_OK) {
47517c478bd9Sstevel@tonic-gate 		handle->zone_dh_cur = handle->zone_dh_top;
47527c478bd9Sstevel@tonic-gate 		return (err);
47537c478bd9Sstevel@tonic-gate 	}
47547c478bd9Sstevel@tonic-gate 
47557c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_TYPE, tabptr->zone_attr_type,
47567c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_attr_type))) != Z_OK) {
47577c478bd9Sstevel@tonic-gate 		handle->zone_dh_cur = handle->zone_dh_top;
47587c478bd9Sstevel@tonic-gate 		return (err);
47597c478bd9Sstevel@tonic-gate 	}
47607c478bd9Sstevel@tonic-gate 
47617c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_VALUE, tabptr->zone_attr_value,
47627c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_attr_value))) != Z_OK) {
47637c478bd9Sstevel@tonic-gate 		handle->zone_dh_cur = handle->zone_dh_top;
47647c478bd9Sstevel@tonic-gate 		return (err);
47657c478bd9Sstevel@tonic-gate 	}
47667c478bd9Sstevel@tonic-gate 
47677c478bd9Sstevel@tonic-gate 	handle->zone_dh_cur = cur->next;
47687c478bd9Sstevel@tonic-gate 	return (Z_OK);
47697c478bd9Sstevel@tonic-gate }
47707c478bd9Sstevel@tonic-gate 
47717c478bd9Sstevel@tonic-gate int
47727c478bd9Sstevel@tonic-gate zonecfg_endattrent(zone_dochandle_t handle)
47737c478bd9Sstevel@tonic-gate {
47747c478bd9Sstevel@tonic-gate 	return (zonecfg_endent(handle));
47757c478bd9Sstevel@tonic-gate }
47767c478bd9Sstevel@tonic-gate 
4777ffbafc53Scomay /*
4778ffbafc53Scomay  * The privileges available on the system and described in privileges(5)
47799acbbeafSnn35248  * fall into four categories with respect to non-global zones:
47809acbbeafSnn35248  *
47819acbbeafSnn35248  *      Default set of privileges considered safe for all non-global
47829acbbeafSnn35248  *      zones.  These privileges are "safe" in the sense that a
47839acbbeafSnn35248  *      privileged process in the zone cannot affect processes in any
47849acbbeafSnn35248  *      other zone on the system.
47859acbbeafSnn35248  *
47869acbbeafSnn35248  *      Set of privileges not currently permitted within a non-global
47879acbbeafSnn35248  *      zone.  These privileges are considered by default, "unsafe,"
47889acbbeafSnn35248  *      and include ones which affect global resources (such as the
47899acbbeafSnn35248  *      system clock or physical memory) or are overly broad and cover
47909acbbeafSnn35248  *      more than one mechanism in the system.  In other cases, there
47919acbbeafSnn35248  *      has not been sufficient virtualization in the parts of the
47929acbbeafSnn35248  *      system the privilege covers to allow its use within a
47939acbbeafSnn35248  *      non-global zone.
47949acbbeafSnn35248  *
47959acbbeafSnn35248  *      Set of privileges required in order to get a zone booted and
47969acbbeafSnn35248  *      init(1M) started.  These cannot be removed from the zone's
47979acbbeafSnn35248  *      privilege set.
47989acbbeafSnn35248  *
47999acbbeafSnn35248  * All other privileges are optional and are potentially useful for
4800ffbafc53Scomay  * processes executing inside a non-global zone.
4801ffbafc53Scomay  *
4802ffbafc53Scomay  * When privileges are added to the system, a determination needs to be
4803ffbafc53Scomay  * made as to which category the privilege belongs to.  Ideally,
4804ffbafc53Scomay  * privileges should be fine-grained enough and the mechanisms they cover
4805ffbafc53Scomay  * virtualized enough so that they can be made available to non-global
4806ffbafc53Scomay  * zones.
4807ffbafc53Scomay  */
4808ffbafc53Scomay 
4809ffbafc53Scomay /*
4810ffbafc53Scomay  * Define some of the tokens that priv_str_to_set(3C) recognizes.  Since
4811ffbafc53Scomay  * the privilege string separator can be any character, although it is
4812ffbafc53Scomay  * usually a comma character, define these here as well in the event that
4813ffbafc53Scomay  * they change or are augmented in the future.
4814ffbafc53Scomay  */
4815ffbafc53Scomay #define	BASIC_TOKEN		"basic"
4816ffbafc53Scomay #define	DEFAULT_TOKEN		"default"
4817ffbafc53Scomay #define	ZONE_TOKEN		"zone"
4818ffbafc53Scomay #define	TOKEN_PRIV_CHAR		','
4819ffbafc53Scomay #define	TOKEN_PRIV_STR		","
4820ffbafc53Scomay 
48219acbbeafSnn35248 typedef struct priv_node {
48229acbbeafSnn35248 	struct priv_node	*pn_next;	/* Next privilege */
48239acbbeafSnn35248 	char			*pn_priv;	/* Privileges name */
48249acbbeafSnn35248 } priv_node_t;
48259acbbeafSnn35248 
48269acbbeafSnn35248 /* Privileges lists can differ across brands */
48279acbbeafSnn35248 typedef struct priv_lists {
48289acbbeafSnn35248 	/* Privileges considered safe for all non-global zones of a brand */
48299acbbeafSnn35248 	struct priv_node	*pl_default;
48309acbbeafSnn35248 
48319acbbeafSnn35248 	/* Privileges not permitted for all non-global zones of a brand */
48329acbbeafSnn35248 	struct priv_node	*pl_prohibited;
48339acbbeafSnn35248 
48349acbbeafSnn35248 	/* Privileges required for all non-global zones of a brand */
48359acbbeafSnn35248 	struct priv_node	*pl_required;
4836bf1d7e28Sdh155122 
4837bf1d7e28Sdh155122 	/*
4838bf1d7e28Sdh155122 	 * ip-type of the zone these privileges lists apply to.
4839bf1d7e28Sdh155122 	 * It is used to pass ip-type to the callback function,
4840bf1d7e28Sdh155122 	 * priv_lists_cb, which has no way of getting the ip-type.
4841bf1d7e28Sdh155122 	 */
4842bf1d7e28Sdh155122 	const char		*pl_iptype;
48439acbbeafSnn35248 } priv_lists_t;
48449acbbeafSnn35248 
48459acbbeafSnn35248 static int
4846bf1d7e28Sdh155122 priv_lists_cb(void *data, priv_iter_t *priv_iter)
48477c478bd9Sstevel@tonic-gate {
48489acbbeafSnn35248 	priv_lists_t *plp = (priv_lists_t *)data;
48499acbbeafSnn35248 	priv_node_t *pnp;
48509acbbeafSnn35248 
4851bf1d7e28Sdh155122 	/* Skip this privilege if ip-type does not match */
4852bf1d7e28Sdh155122 	if ((strcmp(priv_iter->pi_iptype, "all") != 0) &&
4853bf1d7e28Sdh155122 	    (strcmp(priv_iter->pi_iptype, plp->pl_iptype) != 0))
4854bf1d7e28Sdh155122 		return (0);
4855bf1d7e28Sdh155122 
48569acbbeafSnn35248 	/* Allocate a new priv list node. */
48579acbbeafSnn35248 	if ((pnp = malloc(sizeof (*pnp))) == NULL)
48589acbbeafSnn35248 		return (-1);
4859bf1d7e28Sdh155122 	if ((pnp->pn_priv = strdup(priv_iter->pi_name)) == NULL) {
48609acbbeafSnn35248 		free(pnp);
48619acbbeafSnn35248 		return (-1);
48629acbbeafSnn35248 	}
48639acbbeafSnn35248 
48649acbbeafSnn35248 	/* Insert the new priv list node into the right list */
4865bf1d7e28Sdh155122 	if (strcmp(priv_iter->pi_set, "default") == 0) {
48669acbbeafSnn35248 		pnp->pn_next = plp->pl_default;
48679acbbeafSnn35248 		plp->pl_default = pnp;
4868bf1d7e28Sdh155122 	} else if (strcmp(priv_iter->pi_set, "prohibited") == 0) {
48699acbbeafSnn35248 		pnp->pn_next = plp->pl_prohibited;
48709acbbeafSnn35248 		plp->pl_prohibited = pnp;
4871bf1d7e28Sdh155122 	} else if (strcmp(priv_iter->pi_set, "required") == 0) {
48729acbbeafSnn35248 		pnp->pn_next = plp->pl_required;
48739acbbeafSnn35248 		plp->pl_required = pnp;
48749acbbeafSnn35248 	} else {
48759acbbeafSnn35248 		free(pnp->pn_priv);
48769acbbeafSnn35248 		free(pnp);
48779acbbeafSnn35248 		return (-1);
48789acbbeafSnn35248 	}
48799acbbeafSnn35248 	return (0);
48809acbbeafSnn35248 }
48819acbbeafSnn35248 
48829acbbeafSnn35248 static void
48839acbbeafSnn35248 priv_lists_destroy(priv_lists_t *plp)
48849acbbeafSnn35248 {
48859acbbeafSnn35248 	priv_node_t *pnp;
48869acbbeafSnn35248 
48879acbbeafSnn35248 	assert(plp != NULL);
48889acbbeafSnn35248 
48899acbbeafSnn35248 	while ((pnp = plp->pl_default) != NULL) {
48909acbbeafSnn35248 		plp->pl_default = pnp->pn_next;
48919acbbeafSnn35248 		free(pnp->pn_priv);
48929acbbeafSnn35248 		free(pnp);
48939acbbeafSnn35248 	}
48949acbbeafSnn35248 	while ((pnp = plp->pl_prohibited) != NULL) {
48959acbbeafSnn35248 		plp->pl_prohibited = pnp->pn_next;
48969acbbeafSnn35248 		free(pnp->pn_priv);
48979acbbeafSnn35248 		free(pnp);
48989acbbeafSnn35248 	}
48999acbbeafSnn35248 	while ((pnp = plp->pl_required) != NULL) {
49009acbbeafSnn35248 		plp->pl_required = pnp->pn_next;
49019acbbeafSnn35248 		free(pnp->pn_priv);
49029acbbeafSnn35248 		free(pnp);
49039acbbeafSnn35248 	}
49049acbbeafSnn35248 	free(plp);
49059acbbeafSnn35248 }
49069acbbeafSnn35248 
49079acbbeafSnn35248 static int
4908bf1d7e28Sdh155122 priv_lists_create(zone_dochandle_t handle, priv_lists_t **plpp,
4909bf1d7e28Sdh155122     const char *curr_iptype)
49109acbbeafSnn35248 {
49119acbbeafSnn35248 	priv_lists_t *plp;
4912123807fbSedp 	brand_handle_t bh;
49139acbbeafSnn35248 	char brand[MAXNAMELEN];
49149acbbeafSnn35248 
49159acbbeafSnn35248 	if (handle != NULL) {
49169acbbeafSnn35248 		if (zonecfg_get_brand(handle, brand, sizeof (brand)) != 0)
49179acbbeafSnn35248 			return (Z_BRAND_ERROR);
49189acbbeafSnn35248 	} else {
49199acbbeafSnn35248 		(void) strlcpy(brand, NATIVE_BRAND_NAME, MAXNAMELEN);
49209acbbeafSnn35248 	}
49219acbbeafSnn35248 
4922123807fbSedp 	if ((bh = brand_open(brand)) == NULL)
49239acbbeafSnn35248 		return (Z_BRAND_ERROR);
49249acbbeafSnn35248 
49259acbbeafSnn35248 	if ((plp = calloc(1, sizeof (priv_lists_t))) == NULL) {
4926123807fbSedp 		brand_close(bh);
49279acbbeafSnn35248 		return (Z_NOMEM);
49289acbbeafSnn35248 	}
49299acbbeafSnn35248 
4930bf1d7e28Sdh155122 	plp->pl_iptype = curr_iptype;
4931bf1d7e28Sdh155122 
49329acbbeafSnn35248 	/* construct the privilege lists */
4933123807fbSedp 	if (brand_config_iter_privilege(bh, priv_lists_cb, plp) != 0) {
49349acbbeafSnn35248 		priv_lists_destroy(plp);
4935123807fbSedp 		brand_close(bh);
49369acbbeafSnn35248 		return (Z_BRAND_ERROR);
49379acbbeafSnn35248 	}
49389acbbeafSnn35248 
4939123807fbSedp 	brand_close(bh);
49409acbbeafSnn35248 	*plpp = plp;
49419acbbeafSnn35248 	return (Z_OK);
49429acbbeafSnn35248 }
49439acbbeafSnn35248 
49449acbbeafSnn35248 static int
49459acbbeafSnn35248 get_default_privset(priv_set_t *privs, priv_lists_t *plp)
49469acbbeafSnn35248 {
49479acbbeafSnn35248 	priv_node_t *pnp;
4948ffbafc53Scomay 	priv_set_t *basic;
49497c478bd9Sstevel@tonic-gate 
4950ffbafc53Scomay 	basic = priv_str_to_set(BASIC_TOKEN, TOKEN_PRIV_STR, NULL);
49517c478bd9Sstevel@tonic-gate 	if (basic == NULL)
4952ffbafc53Scomay 		return (errno == ENOMEM ? Z_NOMEM : Z_INVAL);
49537c478bd9Sstevel@tonic-gate 
49547c478bd9Sstevel@tonic-gate 	priv_union(basic, privs);
49557c478bd9Sstevel@tonic-gate 	priv_freeset(basic);
49567c478bd9Sstevel@tonic-gate 
49579acbbeafSnn35248 	for (pnp = plp->pl_default; pnp != NULL; pnp = pnp->pn_next) {
49589acbbeafSnn35248 		if (priv_addset(privs, pnp->pn_priv) != 0)
49597c478bd9Sstevel@tonic-gate 			return (Z_INVAL);
49607c478bd9Sstevel@tonic-gate 	}
49619acbbeafSnn35248 
49627c478bd9Sstevel@tonic-gate 	return (Z_OK);
49637c478bd9Sstevel@tonic-gate }
49647c478bd9Sstevel@tonic-gate 
49659acbbeafSnn35248 int
4966bf1d7e28Sdh155122 zonecfg_default_privset(priv_set_t *privs, const char *curr_iptype)
49679acbbeafSnn35248 {
49689acbbeafSnn35248 	priv_lists_t *plp;
49699acbbeafSnn35248 	int ret;
49709acbbeafSnn35248 
4971bf1d7e28Sdh155122 	if ((ret = priv_lists_create(NULL, &plp, curr_iptype)) != Z_OK)
49729acbbeafSnn35248 		return (ret);
49739acbbeafSnn35248 	ret = get_default_privset(privs, plp);
49749acbbeafSnn35248 	priv_lists_destroy(plp);
49759acbbeafSnn35248 	return (ret);
49769acbbeafSnn35248 }
49779acbbeafSnn35248 
4978ffbafc53Scomay void
4979ffbafc53Scomay append_priv_token(char *priv, char *str, size_t strlen)
4980ffbafc53Scomay {
4981ffbafc53Scomay 	if (*str != '\0')
4982ffbafc53Scomay 		(void) strlcat(str, TOKEN_PRIV_STR, strlen);
4983ffbafc53Scomay 	(void) strlcat(str, priv, strlen);
4984ffbafc53Scomay }
4985ffbafc53Scomay 
4986ffbafc53Scomay /*
4987ffbafc53Scomay  * Verify that the supplied string is a valid privilege limit set for a
4988ffbafc53Scomay  * non-global zone.  This string must not only be acceptable to
4989ffbafc53Scomay  * priv_str_to_set(3C) which parses it, but it also must resolve to a
4990ffbafc53Scomay  * privilege set that includes certain required privileges and lacks
4991ffbafc53Scomay  * certain prohibited privileges.
4992ffbafc53Scomay  */
4993ffbafc53Scomay static int
4994ffbafc53Scomay verify_privset(char *privbuf, priv_set_t *privs, char **privname,
49959acbbeafSnn35248     boolean_t add_default, priv_lists_t *plp)
4996ffbafc53Scomay {
49979acbbeafSnn35248 	priv_node_t *pnp;
49989acbbeafSnn35248 	char *tmp, *cp, *lasts;
4999ffbafc53Scomay 	size_t len;
5000ffbafc53Scomay 	priv_set_t *mergeset;
5001ffbafc53Scomay 	const char *token;
5002ffbafc53Scomay 
5003ffbafc53Scomay 	/*
5004ffbafc53Scomay 	 * The verification of the privilege string occurs in several
5005ffbafc53Scomay 	 * phases.  In the first phase, the supplied string is scanned for
5006ffbafc53Scomay 	 * the ZONE_TOKEN token which is not support as part of the
5007ffbafc53Scomay 	 * "limitpriv" property.
5008ffbafc53Scomay 	 *
5009ffbafc53Scomay 	 * Duplicate the supplied privilege string since strtok_r(3C)
5010ffbafc53Scomay 	 * tokenizes its input by null-terminating the tokens.
5011ffbafc53Scomay 	 */
5012ffbafc53Scomay 	if ((tmp = strdup(privbuf)) == NULL)
5013ffbafc53Scomay 		return (Z_NOMEM);
5014ffbafc53Scomay 	for (cp = strtok_r(tmp, TOKEN_PRIV_STR, &lasts); cp != NULL;
5015ffbafc53Scomay 	    cp = strtok_r(NULL, TOKEN_PRIV_STR, &lasts)) {
5016ffbafc53Scomay 		if (strcmp(cp, ZONE_TOKEN) == 0) {
5017ffbafc53Scomay 			free(tmp);
5018ffbafc53Scomay 			if ((*privname = strdup(ZONE_TOKEN)) == NULL)
5019ffbafc53Scomay 				return (Z_NOMEM);
5020ffbafc53Scomay 			else
5021ffbafc53Scomay 				return (Z_PRIV_UNKNOWN);
5022ffbafc53Scomay 		}
5023ffbafc53Scomay 	}
5024ffbafc53Scomay 	free(tmp);
5025ffbafc53Scomay 
5026ffbafc53Scomay 	if (add_default) {
5027ffbafc53Scomay 		/*
5028ffbafc53Scomay 		 * If DEFAULT_TOKEN was specified, a string needs to be
5029ffbafc53Scomay 		 * built containing the privileges from the default, safe
5030ffbafc53Scomay 		 * set along with those of the "limitpriv" property.
5031ffbafc53Scomay 		 */
5032ffbafc53Scomay 		len = strlen(privbuf) + sizeof (BASIC_TOKEN) + 2;
50339acbbeafSnn35248 
50349acbbeafSnn35248 		for (pnp = plp->pl_default; pnp != NULL; pnp = pnp->pn_next)
50359acbbeafSnn35248 			len += strlen(pnp->pn_priv) + 1;
5036ffbafc53Scomay 		tmp = alloca(len);
5037ffbafc53Scomay 		*tmp = '\0';
5038ffbafc53Scomay 
5039ffbafc53Scomay 		append_priv_token(BASIC_TOKEN, tmp, len);
50409acbbeafSnn35248 		for (pnp = plp->pl_default; pnp != NULL; pnp = pnp->pn_next)
50419acbbeafSnn35248 			append_priv_token(pnp->pn_priv, tmp, len);
5042ffbafc53Scomay 		(void) strlcat(tmp, TOKEN_PRIV_STR, len);
5043ffbafc53Scomay 		(void) strlcat(tmp, privbuf, len);
5044ffbafc53Scomay 	} else {
5045ffbafc53Scomay 		tmp = privbuf;
5046ffbafc53Scomay 	}
5047ffbafc53Scomay 
5048ffbafc53Scomay 
5049ffbafc53Scomay 	/*
5050ffbafc53Scomay 	 * In the next phase, attempt to convert the merged privilege
5051ffbafc53Scomay 	 * string into a privilege set.  In the case of an error, either
5052ffbafc53Scomay 	 * there was a memory allocation failure or there was an invalid
5053ffbafc53Scomay 	 * privilege token in the string.  In either case, return an
5054ffbafc53Scomay 	 * appropriate error code but in the event of an invalid token,
5055ffbafc53Scomay 	 * allocate a string containing its name and return that back to
5056ffbafc53Scomay 	 * the caller.
5057ffbafc53Scomay 	 */
5058ffbafc53Scomay 	mergeset = priv_str_to_set(tmp, TOKEN_PRIV_STR, &token);
5059ffbafc53Scomay 	if (mergeset == NULL) {
5060ffbafc53Scomay 		if (token == NULL)
5061ffbafc53Scomay 			return (Z_NOMEM);
5062ffbafc53Scomay 		if ((cp = strchr(token, TOKEN_PRIV_CHAR)) != NULL)
5063ffbafc53Scomay 			*cp = '\0';
5064ffbafc53Scomay 		if ((*privname = strdup(token)) == NULL)
5065ffbafc53Scomay 			return (Z_NOMEM);
5066ffbafc53Scomay 		else
5067ffbafc53Scomay 			return (Z_PRIV_UNKNOWN);
5068ffbafc53Scomay 	}
5069ffbafc53Scomay 
5070ffbafc53Scomay 	/*
5071ffbafc53Scomay 	 * Next, verify that none of the prohibited zone privileges are
5072ffbafc53Scomay 	 * present in the merged privilege set.
5073ffbafc53Scomay 	 */
50749acbbeafSnn35248 	for (pnp = plp->pl_prohibited; pnp != NULL; pnp = pnp->pn_next) {
50759acbbeafSnn35248 		if (priv_ismember(mergeset, pnp->pn_priv)) {
5076ffbafc53Scomay 			priv_freeset(mergeset);
50779acbbeafSnn35248 			if ((*privname = strdup(pnp->pn_priv)) == NULL)
5078ffbafc53Scomay 				return (Z_NOMEM);
5079ffbafc53Scomay 			else
5080ffbafc53Scomay 				return (Z_PRIV_PROHIBITED);
5081ffbafc53Scomay 		}
5082ffbafc53Scomay 	}
5083ffbafc53Scomay 
5084ffbafc53Scomay 	/*
5085ffbafc53Scomay 	 * Finally, verify that all of the required zone privileges are
5086ffbafc53Scomay 	 * present in the merged privilege set.
5087ffbafc53Scomay 	 */
50889acbbeafSnn35248 	for (pnp = plp->pl_required; pnp != NULL; pnp = pnp->pn_next) {
50899acbbeafSnn35248 		if (!priv_ismember(mergeset, pnp->pn_priv)) {
5090ffbafc53Scomay 			priv_freeset(mergeset);
50919acbbeafSnn35248 			if ((*privname = strdup(pnp->pn_priv)) == NULL)
5092ffbafc53Scomay 				return (Z_NOMEM);
5093ffbafc53Scomay 			else
5094ffbafc53Scomay 				return (Z_PRIV_REQUIRED);
5095ffbafc53Scomay 		}
5096ffbafc53Scomay 	}
5097ffbafc53Scomay 
5098ffbafc53Scomay 	priv_copyset(mergeset, privs);
5099ffbafc53Scomay 	priv_freeset(mergeset);
5100ffbafc53Scomay 	return (Z_OK);
5101ffbafc53Scomay }
5102ffbafc53Scomay 
5103ffbafc53Scomay /*
5104ffbafc53Scomay  * Fill in the supplied privilege set with either the default, safe set of
5105ffbafc53Scomay  * privileges suitable for a non-global zone, or one based on the
5106ffbafc53Scomay  * "limitpriv" property in the zone's configuration.
5107ffbafc53Scomay  *
5108ffbafc53Scomay  * In the event of an invalid privilege specification in the
5109ffbafc53Scomay  * configuration, a string is allocated and returned containing the
5110ffbafc53Scomay  * "privilege" causing the issue.  It is the caller's responsibility to
5111ffbafc53Scomay  * free this memory when it is done with it.
5112ffbafc53Scomay  */
5113ffbafc53Scomay int
5114ffbafc53Scomay zonecfg_get_privset(zone_dochandle_t handle, priv_set_t *privs,
5115ffbafc53Scomay     char **privname)
5116ffbafc53Scomay {
51179acbbeafSnn35248 	priv_lists_t *plp;
51189acbbeafSnn35248 	char *cp, *limitpriv = NULL;
51199acbbeafSnn35248 	int err, limitlen;
5120bf1d7e28Sdh155122 	zone_iptype_t iptype;
5121bf1d7e28Sdh155122 	const char *curr_iptype;
5122ffbafc53Scomay 
5123ffbafc53Scomay 	/*
5124ffbafc53Scomay 	 * Attempt to lookup the "limitpriv" property.  If it does not
5125ffbafc53Scomay 	 * exist or matches the string DEFAULT_TOKEN exactly, then the
5126ffbafc53Scomay 	 * default, safe privilege set is returned.
5127ffbafc53Scomay 	 */
51289acbbeafSnn35248 	if ((err = zonecfg_get_limitpriv(handle, &limitpriv)) != Z_OK)
5129ffbafc53Scomay 		return (err);
51309acbbeafSnn35248 
5131bf1d7e28Sdh155122 	if ((err = zonecfg_get_iptype(handle, &iptype)) != Z_OK)
5132bf1d7e28Sdh155122 		return (err);
5133bf1d7e28Sdh155122 
5134bf1d7e28Sdh155122 	switch (iptype) {
5135bf1d7e28Sdh155122 	case ZS_SHARED:
5136bf1d7e28Sdh155122 		curr_iptype = "shared";
5137bf1d7e28Sdh155122 		break;
5138bf1d7e28Sdh155122 	case ZS_EXCLUSIVE:
5139bf1d7e28Sdh155122 		curr_iptype = "exclusive";
5140bf1d7e28Sdh155122 		break;
5141bf1d7e28Sdh155122 	}
5142bf1d7e28Sdh155122 
5143bf1d7e28Sdh155122 	if ((err = priv_lists_create(handle, &plp, curr_iptype)) != Z_OK)
51449acbbeafSnn35248 		return (err);
51459acbbeafSnn35248 
5146ffbafc53Scomay 	limitlen = strlen(limitpriv);
5147ffbafc53Scomay 	if (limitlen == 0 || strcmp(limitpriv, DEFAULT_TOKEN) == 0) {
5148ffbafc53Scomay 		free(limitpriv);
51499acbbeafSnn35248 		err = get_default_privset(privs, plp);
51509acbbeafSnn35248 		priv_lists_destroy(plp);
51519acbbeafSnn35248 		return (err);
5152ffbafc53Scomay 	}
5153ffbafc53Scomay 
5154ffbafc53Scomay 	/*
5155ffbafc53Scomay 	 * Check if the string DEFAULT_TOKEN is the first token in a list
5156ffbafc53Scomay 	 * of privileges.
5157ffbafc53Scomay 	 */
5158ffbafc53Scomay 	cp = strchr(limitpriv, TOKEN_PRIV_CHAR);
5159ffbafc53Scomay 	if (cp != NULL &&
5160ffbafc53Scomay 	    strncmp(limitpriv, DEFAULT_TOKEN, cp - limitpriv) == 0)
51619acbbeafSnn35248 		err = verify_privset(cp + 1, privs, privname, B_TRUE, plp);
5162ffbafc53Scomay 	else
51639acbbeafSnn35248 		err = verify_privset(limitpriv, privs, privname, B_FALSE, plp);
5164ffbafc53Scomay 
5165ffbafc53Scomay 	free(limitpriv);
51669acbbeafSnn35248 	priv_lists_destroy(plp);
5167ffbafc53Scomay 	return (err);
5168ffbafc53Scomay }
5169ffbafc53Scomay 
51707c478bd9Sstevel@tonic-gate int
51717c478bd9Sstevel@tonic-gate zone_get_zonepath(char *zone_name, char *zonepath, size_t rp_sz)
51727c478bd9Sstevel@tonic-gate {
51737c478bd9Sstevel@tonic-gate 	zone_dochandle_t handle;
51747c478bd9Sstevel@tonic-gate 	boolean_t found = B_FALSE;
51757c478bd9Sstevel@tonic-gate 	struct zoneent *ze;
51767c478bd9Sstevel@tonic-gate 	FILE *cookie;
51777c478bd9Sstevel@tonic-gate 	int err;
5178108322fbScarlsonj 	char *cp;
51797c478bd9Sstevel@tonic-gate 
51807c478bd9Sstevel@tonic-gate 	if (zone_name == NULL)
51817c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
51827c478bd9Sstevel@tonic-gate 
5183108322fbScarlsonj 	(void) strlcpy(zonepath, zonecfg_root, rp_sz);
5184108322fbScarlsonj 	cp = zonepath + strlen(zonepath);
5185108322fbScarlsonj 	while (cp > zonepath && cp[-1] == '/')
5186108322fbScarlsonj 		*--cp = '\0';
5187108322fbScarlsonj 
51887c478bd9Sstevel@tonic-gate 	if (strcmp(zone_name, GLOBAL_ZONENAME) == 0) {
5189108322fbScarlsonj 		if (zonepath[0] == '\0')
51907c478bd9Sstevel@tonic-gate 			(void) strlcpy(zonepath, "/", rp_sz);
51917c478bd9Sstevel@tonic-gate 		return (Z_OK);
51927c478bd9Sstevel@tonic-gate 	}
51937c478bd9Sstevel@tonic-gate 
51947c478bd9Sstevel@tonic-gate 	/*
51957c478bd9Sstevel@tonic-gate 	 * First check the index file.  Because older versions did not have
51967c478bd9Sstevel@tonic-gate 	 * a copy of the zone path, allow for it to be zero length, in which
51977c478bd9Sstevel@tonic-gate 	 * case we ignore this result and fall back to the XML files.
51987c478bd9Sstevel@tonic-gate 	 */
51997c478bd9Sstevel@tonic-gate 	cookie = setzoneent();
52007c478bd9Sstevel@tonic-gate 	while ((ze = getzoneent_private(cookie)) != NULL) {
52017c478bd9Sstevel@tonic-gate 		if (strcmp(ze->zone_name, zone_name) == 0) {
52027c478bd9Sstevel@tonic-gate 			found = B_TRUE;
5203108322fbScarlsonj 			if (ze->zone_path[0] != '\0')
5204108322fbScarlsonj 				(void) strlcpy(cp, ze->zone_path,
5205108322fbScarlsonj 				    rp_sz - (cp - zonepath));
52067c478bd9Sstevel@tonic-gate 		}
52077c478bd9Sstevel@tonic-gate 		free(ze);
52087c478bd9Sstevel@tonic-gate 		if (found)
52097c478bd9Sstevel@tonic-gate 			break;
52107c478bd9Sstevel@tonic-gate 	}
52117c478bd9Sstevel@tonic-gate 	endzoneent(cookie);
5212108322fbScarlsonj 	if (found && *cp != '\0')
52137c478bd9Sstevel@tonic-gate 		return (Z_OK);
52147c478bd9Sstevel@tonic-gate 
52157c478bd9Sstevel@tonic-gate 	/* Fall back to the XML files. */
52167c478bd9Sstevel@tonic-gate 	if ((handle = zonecfg_init_handle()) == NULL)
52177c478bd9Sstevel@tonic-gate 		return (Z_NOMEM);
52187c478bd9Sstevel@tonic-gate 
52197c478bd9Sstevel@tonic-gate 	/*
52207c478bd9Sstevel@tonic-gate 	 * Check the snapshot first: if a zone is running, its zonepath
52217c478bd9Sstevel@tonic-gate 	 * may have changed.
52227c478bd9Sstevel@tonic-gate 	 */
52237c478bd9Sstevel@tonic-gate 	if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) {
5224e767a340Sgjelinek 		if ((err = zonecfg_get_handle(zone_name, handle)) != Z_OK) {
5225e767a340Sgjelinek 			zonecfg_fini_handle(handle);
52267c478bd9Sstevel@tonic-gate 			return (err);
52277c478bd9Sstevel@tonic-gate 		}
5228e767a340Sgjelinek 	}
52297c478bd9Sstevel@tonic-gate 	err = zonecfg_get_zonepath(handle, zonepath, rp_sz);
52307c478bd9Sstevel@tonic-gate 	zonecfg_fini_handle(handle);
52317c478bd9Sstevel@tonic-gate 	return (err);
52327c478bd9Sstevel@tonic-gate }
52337c478bd9Sstevel@tonic-gate 
52347c478bd9Sstevel@tonic-gate int
52357c478bd9Sstevel@tonic-gate zone_get_rootpath(char *zone_name, char *rootpath, size_t rp_sz)
52367c478bd9Sstevel@tonic-gate {
52377c478bd9Sstevel@tonic-gate 	int err;
52387c478bd9Sstevel@tonic-gate 
52397c478bd9Sstevel@tonic-gate 	/* This function makes sense for non-global zones only. */
52407c478bd9Sstevel@tonic-gate 	if (strcmp(zone_name, GLOBAL_ZONENAME) == 0)
52417c478bd9Sstevel@tonic-gate 		return (Z_BOGUS_ZONE_NAME);
52427c478bd9Sstevel@tonic-gate 	if ((err = zone_get_zonepath(zone_name, rootpath, rp_sz)) != Z_OK)
52437c478bd9Sstevel@tonic-gate 		return (err);
52447c478bd9Sstevel@tonic-gate 	if (strlcat(rootpath, "/root", rp_sz) >= rp_sz)
52457c478bd9Sstevel@tonic-gate 		return (Z_TOO_BIG);
52467c478bd9Sstevel@tonic-gate 	return (Z_OK);
52477c478bd9Sstevel@tonic-gate }
52487c478bd9Sstevel@tonic-gate 
52499acbbeafSnn35248 int
52509acbbeafSnn35248 zone_get_brand(char *zone_name, char *brandname, size_t rp_sz)
52519acbbeafSnn35248 {
52529acbbeafSnn35248 	int err;
52539acbbeafSnn35248 	zone_dochandle_t handle;
52549acbbeafSnn35248 	char myzone[MAXNAMELEN];
52559acbbeafSnn35248 	int myzoneid = getzoneid();
52569acbbeafSnn35248 
52579acbbeafSnn35248 	/*
52589acbbeafSnn35248 	 * If we are not in the global zone, then we don't have the zone
52599acbbeafSnn35248 	 * .xml files with the brand name available.  Thus, we are going to
52609acbbeafSnn35248 	 * have to ask the kernel for the information.
52619acbbeafSnn35248 	 */
52629acbbeafSnn35248 	if (myzoneid != GLOBAL_ZONEID) {
52632ec67e04Sgjelinek 		if (is_system_labeled()) {
52642ec67e04Sgjelinek 			(void) strlcpy(brandname, NATIVE_BRAND_NAME, rp_sz);
52652ec67e04Sgjelinek 			return (Z_OK);
52662ec67e04Sgjelinek 		}
52679acbbeafSnn35248 		if (zone_getattr(myzoneid, ZONE_ATTR_NAME, myzone,
52689acbbeafSnn35248 		    sizeof (myzone)) < 0)
52699acbbeafSnn35248 			return (Z_NO_ZONE);
5270*1d6979b7Ssaurabh vyas - Sun Microsystems - Bangalore India 		if (!zonecfg_is_scratch(myzone)) {
5271*1d6979b7Ssaurabh vyas - Sun Microsystems - Bangalore India 			if (strncmp(zone_name, myzone, MAXNAMELEN) != 0)
52729acbbeafSnn35248 				return (Z_NO_ZONE);
5273*1d6979b7Ssaurabh vyas - Sun Microsystems - Bangalore India 		}
52749acbbeafSnn35248 		err = zone_getattr(myzoneid, ZONE_ATTR_BRAND, brandname, rp_sz);
52759acbbeafSnn35248 		if (err < 0)
52769acbbeafSnn35248 			return ((errno == EFAULT) ? Z_TOO_BIG : Z_INVAL);
5277*1d6979b7Ssaurabh vyas - Sun Microsystems - Bangalore India 
52789acbbeafSnn35248 		return (Z_OK);
52799acbbeafSnn35248 	}
52809acbbeafSnn35248 
5281*1d6979b7Ssaurabh vyas - Sun Microsystems - Bangalore India 	if (strcmp(zone_name, "global") == 0) {
52829acbbeafSnn35248 		(void) strlcpy(brandname, NATIVE_BRAND_NAME, rp_sz);
52832ec67e04Sgjelinek 		return (Z_OK);
52849acbbeafSnn35248 	}
52859acbbeafSnn35248 	if ((handle = zonecfg_init_handle()) == NULL)
52869acbbeafSnn35248 		return (Z_NOMEM);
52879acbbeafSnn35248 
52889acbbeafSnn35248 	err = zonecfg_get_handle((char *)zone_name, handle);
52899acbbeafSnn35248 	if (err == Z_OK)
52909acbbeafSnn35248 		err = zonecfg_get_brand(handle, brandname, rp_sz);
52919acbbeafSnn35248 
52929acbbeafSnn35248 	zonecfg_fini_handle(handle);
52939acbbeafSnn35248 	return (err);
52949acbbeafSnn35248 }
52959acbbeafSnn35248 
5296facf4a8dSllai1 /*
5297facf4a8dSllai1  * Return the appropriate root for the active /dev.
5298facf4a8dSllai1  * For normal zone, the path is $ZONEPATH/root;
5299facf4a8dSllai1  * for scratch zone, the dev path is $ZONEPATH/lu.
5300facf4a8dSllai1  */
5301facf4a8dSllai1 int
5302facf4a8dSllai1 zone_get_devroot(char *zone_name, char *devroot, size_t rp_sz)
5303facf4a8dSllai1 {
5304facf4a8dSllai1 	int err;
5305facf4a8dSllai1 	char *suffix;
5306facf4a8dSllai1 	zone_state_t state;
5307facf4a8dSllai1 
5308facf4a8dSllai1 	/* This function makes sense for non-global zones only. */
5309facf4a8dSllai1 	if (strcmp(zone_name, GLOBAL_ZONENAME) == 0)
5310facf4a8dSllai1 		return (Z_BOGUS_ZONE_NAME);
5311facf4a8dSllai1 	if ((err = zone_get_zonepath(zone_name, devroot, rp_sz)) != Z_OK)
5312facf4a8dSllai1 		return (err);
5313facf4a8dSllai1 
5314facf4a8dSllai1 	if (zone_get_state(zone_name, &state) == Z_OK &&
5315facf4a8dSllai1 	    state == ZONE_STATE_MOUNTED)
5316facf4a8dSllai1 		suffix = "/lu";
5317facf4a8dSllai1 	else
5318facf4a8dSllai1 		suffix = "/root";
5319facf4a8dSllai1 	if (strlcat(devroot, suffix, rp_sz) >= rp_sz)
5320facf4a8dSllai1 		return (Z_TOO_BIG);
5321facf4a8dSllai1 	return (Z_OK);
5322facf4a8dSllai1 }
5323facf4a8dSllai1 
53247c478bd9Sstevel@tonic-gate static zone_state_t
5325108322fbScarlsonj kernel_state_to_user_state(zoneid_t zoneid, zone_status_t kernel_state)
53267c478bd9Sstevel@tonic-gate {
5327108322fbScarlsonj 	char zoneroot[MAXPATHLEN];
5328108322fbScarlsonj 	size_t zlen;
5329108322fbScarlsonj 
53307c478bd9Sstevel@tonic-gate 	assert(kernel_state <= ZONE_MAX_STATE);
53317c478bd9Sstevel@tonic-gate 	switch (kernel_state) {
53327c478bd9Sstevel@tonic-gate 		case ZONE_IS_UNINITIALIZED:
5333bd41d0a8Snordmark 		case ZONE_IS_INITIALIZED:
5334bd41d0a8Snordmark 			/* The kernel will not return these two states */
5335108322fbScarlsonj 			return (ZONE_STATE_READY);
53367c478bd9Sstevel@tonic-gate 		case ZONE_IS_READY:
5337108322fbScarlsonj 			/*
5338108322fbScarlsonj 			 * If the zone's root is mounted on $ZONEPATH/lu, then
5339108322fbScarlsonj 			 * it's a mounted scratch zone.
5340108322fbScarlsonj 			 */
5341108322fbScarlsonj 			if (zone_getattr(zoneid, ZONE_ATTR_ROOT, zoneroot,
5342108322fbScarlsonj 			    sizeof (zoneroot)) >= 0) {
5343108322fbScarlsonj 				zlen = strlen(zoneroot);
5344108322fbScarlsonj 				if (zlen > 3 &&
5345108322fbScarlsonj 				    strcmp(zoneroot + zlen - 3, "/lu") == 0)
5346108322fbScarlsonj 					return (ZONE_STATE_MOUNTED);
5347108322fbScarlsonj 			}
53487c478bd9Sstevel@tonic-gate 			return (ZONE_STATE_READY);
53497c478bd9Sstevel@tonic-gate 		case ZONE_IS_BOOTING:
53507c478bd9Sstevel@tonic-gate 		case ZONE_IS_RUNNING:
53517c478bd9Sstevel@tonic-gate 			return (ZONE_STATE_RUNNING);
53527c478bd9Sstevel@tonic-gate 		case ZONE_IS_SHUTTING_DOWN:
53537c478bd9Sstevel@tonic-gate 		case ZONE_IS_EMPTY:
53547c478bd9Sstevel@tonic-gate 			return (ZONE_STATE_SHUTTING_DOWN);
53557c478bd9Sstevel@tonic-gate 		case ZONE_IS_DOWN:
53567c478bd9Sstevel@tonic-gate 		case ZONE_IS_DYING:
53577c478bd9Sstevel@tonic-gate 		case ZONE_IS_DEAD:
53587c478bd9Sstevel@tonic-gate 		default:
53597c478bd9Sstevel@tonic-gate 			return (ZONE_STATE_DOWN);
53607c478bd9Sstevel@tonic-gate 	}
53617c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
53627c478bd9Sstevel@tonic-gate }
53637c478bd9Sstevel@tonic-gate 
53647c478bd9Sstevel@tonic-gate int
53657c478bd9Sstevel@tonic-gate zone_get_state(char *zone_name, zone_state_t *state_num)
53667c478bd9Sstevel@tonic-gate {
53677c478bd9Sstevel@tonic-gate 	zone_status_t status;
53687c478bd9Sstevel@tonic-gate 	zoneid_t zone_id;
53697c478bd9Sstevel@tonic-gate 	struct zoneent *ze;
53707c478bd9Sstevel@tonic-gate 	boolean_t found = B_FALSE;
53717c478bd9Sstevel@tonic-gate 	FILE *cookie;
5372108322fbScarlsonj 	char kernzone[ZONENAME_MAX];
5373108322fbScarlsonj 	FILE *fp;
53747c478bd9Sstevel@tonic-gate 
53757c478bd9Sstevel@tonic-gate 	if (zone_name == NULL)
53767c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
53777c478bd9Sstevel@tonic-gate 
5378108322fbScarlsonj 	/*
5379108322fbScarlsonj 	 * If we're looking at an alternate root, then we need to query the
5380108322fbScarlsonj 	 * kernel using the scratch zone name.
5381108322fbScarlsonj 	 */
5382108322fbScarlsonj 	zone_id = -1;
5383108322fbScarlsonj 	if (*zonecfg_root != '\0' && !zonecfg_is_scratch(zone_name)) {
5384108322fbScarlsonj 		if ((fp = zonecfg_open_scratch("", B_FALSE)) != NULL) {
5385108322fbScarlsonj 			if (zonecfg_find_scratch(fp, zone_name, zonecfg_root,
5386108322fbScarlsonj 			    kernzone, sizeof (kernzone)) == 0)
5387108322fbScarlsonj 				zone_id = getzoneidbyname(kernzone);
5388108322fbScarlsonj 			zonecfg_close_scratch(fp);
5389108322fbScarlsonj 		}
5390108322fbScarlsonj 	} else {
5391108322fbScarlsonj 		zone_id = getzoneidbyname(zone_name);
5392108322fbScarlsonj 	}
5393108322fbScarlsonj 
53947c478bd9Sstevel@tonic-gate 	/* check to see if zone is running */
5395108322fbScarlsonj 	if (zone_id != -1 &&
53967c478bd9Sstevel@tonic-gate 	    zone_getattr(zone_id, ZONE_ATTR_STATUS, &status,
53977c478bd9Sstevel@tonic-gate 	    sizeof (status)) >= 0) {
5398108322fbScarlsonj 		*state_num = kernel_state_to_user_state(zone_id, status);
53997c478bd9Sstevel@tonic-gate 		return (Z_OK);
54007c478bd9Sstevel@tonic-gate 	}
54017c478bd9Sstevel@tonic-gate 
54027c478bd9Sstevel@tonic-gate 	cookie = setzoneent();
54037c478bd9Sstevel@tonic-gate 	while ((ze = getzoneent_private(cookie)) != NULL) {
54047c478bd9Sstevel@tonic-gate 		if (strcmp(ze->zone_name, zone_name) == 0) {
54057c478bd9Sstevel@tonic-gate 			found = B_TRUE;
54067c478bd9Sstevel@tonic-gate 			*state_num = ze->zone_state;
54077c478bd9Sstevel@tonic-gate 		}
54087c478bd9Sstevel@tonic-gate 		free(ze);
54097c478bd9Sstevel@tonic-gate 		if (found)
54107c478bd9Sstevel@tonic-gate 			break;
54117c478bd9Sstevel@tonic-gate 	}
54127c478bd9Sstevel@tonic-gate 	endzoneent(cookie);
54137c478bd9Sstevel@tonic-gate 	return ((found) ? Z_OK : Z_NO_ZONE);
54147c478bd9Sstevel@tonic-gate }
54157c478bd9Sstevel@tonic-gate 
54167c478bd9Sstevel@tonic-gate int
54177c478bd9Sstevel@tonic-gate zone_set_state(char *zone, zone_state_t state)
54187c478bd9Sstevel@tonic-gate {
54197c478bd9Sstevel@tonic-gate 	struct zoneent ze;
54207c478bd9Sstevel@tonic-gate 
54217c478bd9Sstevel@tonic-gate 	if (state != ZONE_STATE_CONFIGURED && state != ZONE_STATE_INSTALLED &&
54227c478bd9Sstevel@tonic-gate 	    state != ZONE_STATE_INCOMPLETE)
54237c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
54247c478bd9Sstevel@tonic-gate 
5425087719fdSdp 	bzero(&ze, sizeof (ze));
54267c478bd9Sstevel@tonic-gate 	(void) strlcpy(ze.zone_name, zone, sizeof (ze.zone_name));
54277c478bd9Sstevel@tonic-gate 	ze.zone_state = state;
54287c478bd9Sstevel@tonic-gate 	(void) strlcpy(ze.zone_path, "", sizeof (ze.zone_path));
54297c478bd9Sstevel@tonic-gate 	return (putzoneent(&ze, PZE_MODIFY));
54307c478bd9Sstevel@tonic-gate }
54317c478bd9Sstevel@tonic-gate 
54327c478bd9Sstevel@tonic-gate /*
54337c478bd9Sstevel@tonic-gate  * Get id (if any) for specified zone.  There are four possible outcomes:
54347c478bd9Sstevel@tonic-gate  * - If the string corresponds to the numeric id of an active (booted)
54357c478bd9Sstevel@tonic-gate  *   zone, sets *zip to the zone id and returns 0.
54367c478bd9Sstevel@tonic-gate  * - If the string corresponds to the name of an active (booted) zone,
54377c478bd9Sstevel@tonic-gate  *   sets *zip to the zone id and returns 0.
54387c478bd9Sstevel@tonic-gate  * - If the string is a name in the configuration but is not booted,
54397c478bd9Sstevel@tonic-gate  *   sets *zip to ZONE_ID_UNDEFINED and returns 0.
54407c478bd9Sstevel@tonic-gate  * - Otherwise, leaves *zip unchanged and returns -1.
54417c478bd9Sstevel@tonic-gate  *
54427c478bd9Sstevel@tonic-gate  * This function acts as an auxiliary filter on the function of the same
54437c478bd9Sstevel@tonic-gate  * name in libc; the linker binds to this version if libzonecfg exists,
54447c478bd9Sstevel@tonic-gate  * and the libc version if it doesn't.  Any changes to this version of
54457c478bd9Sstevel@tonic-gate  * the function should probably be reflected in the libc version as well.
54467c478bd9Sstevel@tonic-gate  */
54477c478bd9Sstevel@tonic-gate int
54487c478bd9Sstevel@tonic-gate zone_get_id(const char *str, zoneid_t *zip)
54497c478bd9Sstevel@tonic-gate {
54507c478bd9Sstevel@tonic-gate 	zone_dochandle_t hdl;
54517c478bd9Sstevel@tonic-gate 	zoneid_t zoneid;
54527c478bd9Sstevel@tonic-gate 	char *cp;
54537c478bd9Sstevel@tonic-gate 	int err;
54547c478bd9Sstevel@tonic-gate 
54557c478bd9Sstevel@tonic-gate 	/* first try looking for active zone by id */
54567c478bd9Sstevel@tonic-gate 	errno = 0;
54577c478bd9Sstevel@tonic-gate 	zoneid = (zoneid_t)strtol(str, &cp, 0);
54587c478bd9Sstevel@tonic-gate 	if (errno == 0 && cp != str && *cp == '\0' &&
54597c478bd9Sstevel@tonic-gate 	    getzonenamebyid(zoneid, NULL, 0) != -1) {
54607c478bd9Sstevel@tonic-gate 		*zip = zoneid;
54617c478bd9Sstevel@tonic-gate 		return (0);
54627c478bd9Sstevel@tonic-gate 	}
54637c478bd9Sstevel@tonic-gate 
54647c478bd9Sstevel@tonic-gate 	/* then look for active zone by name */
54657c478bd9Sstevel@tonic-gate 	if ((zoneid = getzoneidbyname(str)) != -1) {
54667c478bd9Sstevel@tonic-gate 		*zip = zoneid;
54677c478bd9Sstevel@tonic-gate 		return (0);
54687c478bd9Sstevel@tonic-gate 	}
54697c478bd9Sstevel@tonic-gate 
54707c478bd9Sstevel@tonic-gate 	/* if in global zone, try looking up name in configuration database */
54717c478bd9Sstevel@tonic-gate 	if (getzoneid() != GLOBAL_ZONEID ||
54727c478bd9Sstevel@tonic-gate 	    (hdl = zonecfg_init_handle()) == NULL)
54737c478bd9Sstevel@tonic-gate 		return (-1);
54747c478bd9Sstevel@tonic-gate 
5475108322fbScarlsonj 	if (zonecfg_get_handle(str, hdl) == Z_OK) {
54767c478bd9Sstevel@tonic-gate 		/* zone exists but isn't active */
54777c478bd9Sstevel@tonic-gate 		*zip = ZONE_ID_UNDEFINED;
54787c478bd9Sstevel@tonic-gate 		err = 0;
54797c478bd9Sstevel@tonic-gate 	} else {
54807c478bd9Sstevel@tonic-gate 		err = -1;
54817c478bd9Sstevel@tonic-gate 	}
54827c478bd9Sstevel@tonic-gate 
54837c478bd9Sstevel@tonic-gate 	zonecfg_fini_handle(hdl);
54847c478bd9Sstevel@tonic-gate 	return (err);
54857c478bd9Sstevel@tonic-gate }
54867c478bd9Sstevel@tonic-gate 
54877c478bd9Sstevel@tonic-gate char *
54887c478bd9Sstevel@tonic-gate zone_state_str(zone_state_t state_num)
54897c478bd9Sstevel@tonic-gate {
54907c478bd9Sstevel@tonic-gate 	switch (state_num) {
54917c478bd9Sstevel@tonic-gate 	case ZONE_STATE_CONFIGURED:
54927c478bd9Sstevel@tonic-gate 		return (ZONE_STATE_STR_CONFIGURED);
54937c478bd9Sstevel@tonic-gate 	case ZONE_STATE_INCOMPLETE:
54947c478bd9Sstevel@tonic-gate 		return (ZONE_STATE_STR_INCOMPLETE);
54957c478bd9Sstevel@tonic-gate 	case ZONE_STATE_INSTALLED:
54967c478bd9Sstevel@tonic-gate 		return (ZONE_STATE_STR_INSTALLED);
54977c478bd9Sstevel@tonic-gate 	case ZONE_STATE_READY:
54987c478bd9Sstevel@tonic-gate 		return (ZONE_STATE_STR_READY);
5499108322fbScarlsonj 	case ZONE_STATE_MOUNTED:
5500108322fbScarlsonj 		return (ZONE_STATE_STR_MOUNTED);
55017c478bd9Sstevel@tonic-gate 	case ZONE_STATE_RUNNING:
55027c478bd9Sstevel@tonic-gate 		return (ZONE_STATE_STR_RUNNING);
55037c478bd9Sstevel@tonic-gate 	case ZONE_STATE_SHUTTING_DOWN:
55047c478bd9Sstevel@tonic-gate 		return (ZONE_STATE_STR_SHUTTING_DOWN);
55057c478bd9Sstevel@tonic-gate 	case ZONE_STATE_DOWN:
55067c478bd9Sstevel@tonic-gate 		return (ZONE_STATE_STR_DOWN);
55077c478bd9Sstevel@tonic-gate 	default:
55087c478bd9Sstevel@tonic-gate 		return ("unknown");
55097c478bd9Sstevel@tonic-gate 	}
55107c478bd9Sstevel@tonic-gate }
55117c478bd9Sstevel@tonic-gate 
55127c478bd9Sstevel@tonic-gate /*
5513108322fbScarlsonj  * Given a UUID value, find an associated zone name.  This is intended to be
5514108322fbScarlsonj  * used by callers who set up some 'default' name (corresponding to the
5515108322fbScarlsonj  * expected name for the zone) in the zonename buffer, and thus the function
5516108322fbScarlsonj  * doesn't touch this buffer on failure.
5517108322fbScarlsonj  */
5518108322fbScarlsonj int
5519555afedfScarlsonj zonecfg_get_name_by_uuid(const uuid_t uuidin, char *zonename, size_t namelen)
5520108322fbScarlsonj {
5521108322fbScarlsonj 	FILE *fp;
5522108322fbScarlsonj 	struct zoneent *ze;
5523555afedfScarlsonj 	uchar_t *uuid;
5524108322fbScarlsonj 
5525108322fbScarlsonj 	/*
5526108322fbScarlsonj 	 * A small amount of subterfuge via casts is necessary here because
5527108322fbScarlsonj 	 * libuuid doesn't use const correctly, but we don't want to export
5528108322fbScarlsonj 	 * this brokenness to our clients.
5529108322fbScarlsonj 	 */
5530555afedfScarlsonj 	uuid = (uchar_t *)uuidin;
5531555afedfScarlsonj 	if (uuid_is_null(uuid))
5532108322fbScarlsonj 		return (Z_NO_ZONE);
5533108322fbScarlsonj 	if ((fp = setzoneent()) == NULL)
5534108322fbScarlsonj 		return (Z_NO_ZONE);
5535108322fbScarlsonj 	while ((ze = getzoneent_private(fp)) != NULL) {
5536555afedfScarlsonj 		if (uuid_compare(uuid, ze->zone_uuid) == 0)
5537108322fbScarlsonj 			break;
5538108322fbScarlsonj 		free(ze);
5539108322fbScarlsonj 	}
5540108322fbScarlsonj 	endzoneent(fp);
5541108322fbScarlsonj 	if (ze != NULL) {
5542108322fbScarlsonj 		(void) strlcpy(zonename, ze->zone_name, namelen);
5543108322fbScarlsonj 		free(ze);
5544108322fbScarlsonj 		return (Z_OK);
5545108322fbScarlsonj 	} else {
5546108322fbScarlsonj 		return (Z_NO_ZONE);
5547108322fbScarlsonj 	}
5548108322fbScarlsonj }
5549108322fbScarlsonj 
5550108322fbScarlsonj /*
5551108322fbScarlsonj  * Given a zone name, get its UUID.  Returns a "NULL" UUID value if the zone
5552108322fbScarlsonj  * exists but the file doesn't have a value set yet.  Returns an error if the
5553108322fbScarlsonj  * zone cannot be located.
5554108322fbScarlsonj  */
5555108322fbScarlsonj int
5556108322fbScarlsonj zonecfg_get_uuid(const char *zonename, uuid_t uuid)
5557108322fbScarlsonj {
5558108322fbScarlsonj 	FILE *fp;
5559108322fbScarlsonj 	struct zoneent *ze;
5560108322fbScarlsonj 
5561108322fbScarlsonj 	if ((fp = setzoneent()) == NULL)
5562108322fbScarlsonj 		return (Z_NO_ZONE);
5563108322fbScarlsonj 	while ((ze = getzoneent_private(fp)) != NULL) {
5564108322fbScarlsonj 		if (strcmp(ze->zone_name, zonename) == 0)
5565108322fbScarlsonj 			break;
5566108322fbScarlsonj 		free(ze);
5567108322fbScarlsonj 	}
5568108322fbScarlsonj 	endzoneent(fp);
5569108322fbScarlsonj 	if (ze != NULL) {
5570108322fbScarlsonj 		uuid_copy(uuid, ze->zone_uuid);
5571108322fbScarlsonj 		free(ze);
5572108322fbScarlsonj 		return (Z_OK);
5573108322fbScarlsonj 	} else {
5574108322fbScarlsonj 		return (Z_NO_ZONE);
5575108322fbScarlsonj 	}
5576108322fbScarlsonj }
5577108322fbScarlsonj 
5578108322fbScarlsonj /*
55797c478bd9Sstevel@tonic-gate  * File-system convenience functions.
55807c478bd9Sstevel@tonic-gate  */
55817c478bd9Sstevel@tonic-gate boolean_t
55827c478bd9Sstevel@tonic-gate zonecfg_valid_fs_type(const char *type)
55837c478bd9Sstevel@tonic-gate {
55847c478bd9Sstevel@tonic-gate 	/*
55857c478bd9Sstevel@tonic-gate 	 * We already know which FS types don't work.
55867c478bd9Sstevel@tonic-gate 	 */
55877c478bd9Sstevel@tonic-gate 	if (strcmp(type, "proc") == 0 ||
55887c478bd9Sstevel@tonic-gate 	    strcmp(type, "mntfs") == 0 ||
55897c478bd9Sstevel@tonic-gate 	    strcmp(type, "autofs") == 0 ||
55907c478bd9Sstevel@tonic-gate 	    strncmp(type, "nfs", sizeof ("nfs") - 1) == 0 ||
55917c478bd9Sstevel@tonic-gate 	    strcmp(type, "cachefs") == 0)
55927c478bd9Sstevel@tonic-gate 		return (B_FALSE);
55937c478bd9Sstevel@tonic-gate 	/*
55947c478bd9Sstevel@tonic-gate 	 * The caller may do more detailed verification to make sure other
55957c478bd9Sstevel@tonic-gate 	 * aspects of this filesystem type make sense.
55967c478bd9Sstevel@tonic-gate 	 */
55977c478bd9Sstevel@tonic-gate 	return (B_TRUE);
55987c478bd9Sstevel@tonic-gate }
55997c478bd9Sstevel@tonic-gate 
56007c478bd9Sstevel@tonic-gate /*
56017c478bd9Sstevel@tonic-gate  * Generally uninteresting rctl convenience functions.
56027c478bd9Sstevel@tonic-gate  */
56037c478bd9Sstevel@tonic-gate 
56047c478bd9Sstevel@tonic-gate int
56057c478bd9Sstevel@tonic-gate zonecfg_construct_rctlblk(const struct zone_rctlvaltab *rctlval,
56067c478bd9Sstevel@tonic-gate     rctlblk_t *rctlblk)
56077c478bd9Sstevel@tonic-gate {
56087c478bd9Sstevel@tonic-gate 	unsigned long long ull;
56097c478bd9Sstevel@tonic-gate 	char *endp;
56107c478bd9Sstevel@tonic-gate 	rctl_priv_t priv;
56117c478bd9Sstevel@tonic-gate 	rctl_qty_t limit;
56127c478bd9Sstevel@tonic-gate 	uint_t action;
56137c478bd9Sstevel@tonic-gate 
56147c478bd9Sstevel@tonic-gate 	/* Get the privilege */
56157c478bd9Sstevel@tonic-gate 	if (strcmp(rctlval->zone_rctlval_priv, "basic") == 0) {
56167c478bd9Sstevel@tonic-gate 		priv = RCPRIV_BASIC;
56177c478bd9Sstevel@tonic-gate 	} else if (strcmp(rctlval->zone_rctlval_priv, "privileged") == 0) {
56187c478bd9Sstevel@tonic-gate 		priv = RCPRIV_PRIVILEGED;
56197c478bd9Sstevel@tonic-gate 	} else {
56207c478bd9Sstevel@tonic-gate 		/* Invalid privilege */
56217c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
56227c478bd9Sstevel@tonic-gate 	}
56237c478bd9Sstevel@tonic-gate 
56247c478bd9Sstevel@tonic-gate 	/* deal with negative input; strtoull(3c) doesn't do what we want */
56257c478bd9Sstevel@tonic-gate 	if (rctlval->zone_rctlval_limit[0] == '-')
56267c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
56277c478bd9Sstevel@tonic-gate 	/* Get the limit */
56287c478bd9Sstevel@tonic-gate 	errno = 0;
56297c478bd9Sstevel@tonic-gate 	ull = strtoull(rctlval->zone_rctlval_limit, &endp, 0);
56307c478bd9Sstevel@tonic-gate 	if (errno != 0 || *endp != '\0') {
56317c478bd9Sstevel@tonic-gate 		/* parse failed */
56327c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
56337c478bd9Sstevel@tonic-gate 	}
56347c478bd9Sstevel@tonic-gate 	limit = (rctl_qty_t)ull;
56357c478bd9Sstevel@tonic-gate 
56367c478bd9Sstevel@tonic-gate 	/* Get the action */
56377c478bd9Sstevel@tonic-gate 	if (strcmp(rctlval->zone_rctlval_action, "none") == 0) {
56387c478bd9Sstevel@tonic-gate 		action = RCTL_LOCAL_NOACTION;
56397c478bd9Sstevel@tonic-gate 	} else if (strcmp(rctlval->zone_rctlval_action, "signal") == 0) {
56407c478bd9Sstevel@tonic-gate 		action = RCTL_LOCAL_SIGNAL;
56417c478bd9Sstevel@tonic-gate 	} else if (strcmp(rctlval->zone_rctlval_action, "deny") == 0) {
56427c478bd9Sstevel@tonic-gate 		action = RCTL_LOCAL_DENY;
56437c478bd9Sstevel@tonic-gate 	} else {
56447c478bd9Sstevel@tonic-gate 		/* Invalid Action */
56457c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
56467c478bd9Sstevel@tonic-gate 	}
56477c478bd9Sstevel@tonic-gate 	rctlblk_set_local_action(rctlblk, action, 0);
56487c478bd9Sstevel@tonic-gate 	rctlblk_set_privilege(rctlblk, priv);
56497c478bd9Sstevel@tonic-gate 	rctlblk_set_value(rctlblk, limit);
56507c478bd9Sstevel@tonic-gate 	return (Z_OK);
56517c478bd9Sstevel@tonic-gate }
56527c478bd9Sstevel@tonic-gate 
56537c478bd9Sstevel@tonic-gate static int
56547c478bd9Sstevel@tonic-gate rctl_check(const char *rctlname, void *arg)
56557c478bd9Sstevel@tonic-gate {
56567c478bd9Sstevel@tonic-gate 	const char *attrname = arg;
56577c478bd9Sstevel@tonic-gate 
56587c478bd9Sstevel@tonic-gate 	/*
56597c478bd9Sstevel@tonic-gate 	 * Returning 1 here is our signal to zonecfg_is_rctl() that it is
56607c478bd9Sstevel@tonic-gate 	 * indeed an rctl name recognized by the system.
56617c478bd9Sstevel@tonic-gate 	 */
56627c478bd9Sstevel@tonic-gate 	return (strcmp(rctlname, attrname) == 0 ? 1 : 0);
56637c478bd9Sstevel@tonic-gate }
56647c478bd9Sstevel@tonic-gate 
56657c478bd9Sstevel@tonic-gate boolean_t
56667c478bd9Sstevel@tonic-gate zonecfg_is_rctl(const char *name)
56677c478bd9Sstevel@tonic-gate {
56687c478bd9Sstevel@tonic-gate 	return (rctl_walk(rctl_check, (void *)name) == 1);
56697c478bd9Sstevel@tonic-gate }
56707c478bd9Sstevel@tonic-gate 
56717c478bd9Sstevel@tonic-gate boolean_t
56727c478bd9Sstevel@tonic-gate zonecfg_valid_rctlname(const char *name)
56737c478bd9Sstevel@tonic-gate {
56747c478bd9Sstevel@tonic-gate 	const char *c;
56757c478bd9Sstevel@tonic-gate 
56767c478bd9Sstevel@tonic-gate 	if (strncmp(name, "zone.", sizeof ("zone.") - 1) != 0)
56777c478bd9Sstevel@tonic-gate 		return (B_FALSE);
56787c478bd9Sstevel@tonic-gate 	if (strlen(name) == sizeof ("zone.") - 1)
56797c478bd9Sstevel@tonic-gate 		return (B_FALSE);
56807c478bd9Sstevel@tonic-gate 	for (c = name + sizeof ("zone.") - 1; *c != '\0'; c++) {
56817c478bd9Sstevel@tonic-gate 		if (!isalpha(*c) && *c != '-')
56827c478bd9Sstevel@tonic-gate 			return (B_FALSE);
56837c478bd9Sstevel@tonic-gate 	}
56847c478bd9Sstevel@tonic-gate 	return (B_TRUE);
56857c478bd9Sstevel@tonic-gate }
56867c478bd9Sstevel@tonic-gate 
56877c478bd9Sstevel@tonic-gate boolean_t
56887c478bd9Sstevel@tonic-gate zonecfg_valid_rctlblk(const rctlblk_t *rctlblk)
56897c478bd9Sstevel@tonic-gate {
56907c478bd9Sstevel@tonic-gate 	rctl_priv_t priv = rctlblk_get_privilege((rctlblk_t *)rctlblk);
56917c478bd9Sstevel@tonic-gate 	uint_t action = rctlblk_get_local_action((rctlblk_t *)rctlblk, NULL);
56927c478bd9Sstevel@tonic-gate 
56937c478bd9Sstevel@tonic-gate 	if (priv != RCPRIV_PRIVILEGED)
56947c478bd9Sstevel@tonic-gate 		return (B_FALSE);
56957c478bd9Sstevel@tonic-gate 	if (action != RCTL_LOCAL_NOACTION && action != RCTL_LOCAL_DENY)
56967c478bd9Sstevel@tonic-gate 		return (B_FALSE);
56977c478bd9Sstevel@tonic-gate 	return (B_TRUE);
56987c478bd9Sstevel@tonic-gate }
56997c478bd9Sstevel@tonic-gate 
57007c478bd9Sstevel@tonic-gate boolean_t
57017c478bd9Sstevel@tonic-gate zonecfg_valid_rctl(const char *name, const rctlblk_t *rctlblk)
57027c478bd9Sstevel@tonic-gate {
57037c478bd9Sstevel@tonic-gate 	rctlblk_t *current, *next;
57047c478bd9Sstevel@tonic-gate 	rctl_qty_t limit = rctlblk_get_value((rctlblk_t *)rctlblk);
57057c478bd9Sstevel@tonic-gate 	uint_t action = rctlblk_get_local_action((rctlblk_t *)rctlblk, NULL);
57067c478bd9Sstevel@tonic-gate 	uint_t global_flags;
57077c478bd9Sstevel@tonic-gate 
57087c478bd9Sstevel@tonic-gate 	if (!zonecfg_valid_rctlblk(rctlblk))
57097c478bd9Sstevel@tonic-gate 		return (B_FALSE);
57107c478bd9Sstevel@tonic-gate 	if (!zonecfg_valid_rctlname(name))
57117c478bd9Sstevel@tonic-gate 		return (B_FALSE);
57127c478bd9Sstevel@tonic-gate 
57137c478bd9Sstevel@tonic-gate 	current = alloca(rctlblk_size());
57147c478bd9Sstevel@tonic-gate 	if (getrctl(name, NULL, current, RCTL_FIRST) != 0)
57157c478bd9Sstevel@tonic-gate 		return (B_TRUE);	/* not an rctl on this system */
57167c478bd9Sstevel@tonic-gate 	/*
57177c478bd9Sstevel@tonic-gate 	 * Make sure the proposed value isn't greater than the current system
57187c478bd9Sstevel@tonic-gate 	 * value.
57197c478bd9Sstevel@tonic-gate 	 */
57207c478bd9Sstevel@tonic-gate 	next = alloca(rctlblk_size());
57217c478bd9Sstevel@tonic-gate 	while (rctlblk_get_privilege(current) != RCPRIV_SYSTEM) {
57227c478bd9Sstevel@tonic-gate 		rctlblk_t *tmp;
57237c478bd9Sstevel@tonic-gate 
57247c478bd9Sstevel@tonic-gate 		if (getrctl(name, current, next, RCTL_NEXT) != 0)
57257c478bd9Sstevel@tonic-gate 			return (B_FALSE);	/* shouldn't happen */
57267c478bd9Sstevel@tonic-gate 		tmp = current;
57277c478bd9Sstevel@tonic-gate 		current = next;
57287c478bd9Sstevel@tonic-gate 		next = tmp;
57297c478bd9Sstevel@tonic-gate 	}
57307c478bd9Sstevel@tonic-gate 	if (limit > rctlblk_get_value(current))
57317c478bd9Sstevel@tonic-gate 		return (B_FALSE);
57327c478bd9Sstevel@tonic-gate 
57337c478bd9Sstevel@tonic-gate 	/*
57347c478bd9Sstevel@tonic-gate 	 * Make sure the proposed action is allowed.
57357c478bd9Sstevel@tonic-gate 	 */
57367c478bd9Sstevel@tonic-gate 	global_flags = rctlblk_get_global_flags(current);
57377c478bd9Sstevel@tonic-gate 	if ((global_flags & RCTL_GLOBAL_DENY_NEVER) &&
57387c478bd9Sstevel@tonic-gate 	    action == RCTL_LOCAL_DENY)
57397c478bd9Sstevel@tonic-gate 		return (B_FALSE);
57407c478bd9Sstevel@tonic-gate 	if ((global_flags & RCTL_GLOBAL_DENY_ALWAYS) &&
57417c478bd9Sstevel@tonic-gate 	    action == RCTL_LOCAL_NOACTION)
57427c478bd9Sstevel@tonic-gate 		return (B_FALSE);
57437c478bd9Sstevel@tonic-gate 
57447c478bd9Sstevel@tonic-gate 	return (B_TRUE);
57457c478bd9Sstevel@tonic-gate }
5746fa9e4066Sahrens 
5747cf8f45c7Sdstaff /*
5748cf8f45c7Sdstaff  * There is always a race condition between reading the initial copy of
5749cf8f45c7Sdstaff  * a zones state and its state changing.  We address this by providing
5750cf8f45c7Sdstaff  * zonecfg_notify_critical_enter and zonecfg_noticy_critical_exit functions.
5751cf8f45c7Sdstaff  * When zonecfg_critical_enter is called, sets the state field to LOCKED
5752cf8f45c7Sdstaff  * and aquires biglock. Biglock protects against other threads executing
5753cf8f45c7Sdstaff  * critical_enter and the state field protects against state changes during
5754cf8f45c7Sdstaff  * the critical period.
5755cf8f45c7Sdstaff  *
5756cf8f45c7Sdstaff  * If any state changes occur, zn_cb will set the failed field of the znotify
5757cf8f45c7Sdstaff  * structure.  This will cause the critical_exit function to re-lock the
5758cf8f45c7Sdstaff  * channel and return an error. Since evsnts may be delayed, the critical_exit
5759cf8f45c7Sdstaff  * function "flushes" the queue by putting an event on the queue and waiting for
5760cf8f45c7Sdstaff  * zn_cb to notify critical_exit that it received the ping event.
5761cf8f45c7Sdstaff  */
5762cf8f45c7Sdstaff static const char *
5763cf8f45c7Sdstaff string_get_tok(const char *in, char delim, int num)
5764cf8f45c7Sdstaff {
5765cf8f45c7Sdstaff 	int i = 0;
5766cf8f45c7Sdstaff 
5767cf8f45c7Sdstaff 	for (; i < num; in++) {
5768cf8f45c7Sdstaff 		if (*in == delim)
5769cf8f45c7Sdstaff 			i++;
5770cf8f45c7Sdstaff 		if (*in == 0)
5771cf8f45c7Sdstaff 			return (NULL);
5772cf8f45c7Sdstaff 	}
5773cf8f45c7Sdstaff 	return (in);
5774cf8f45c7Sdstaff }
5775cf8f45c7Sdstaff 
5776cf8f45c7Sdstaff static boolean_t
5777cf8f45c7Sdstaff is_ping(sysevent_t *ev)
5778cf8f45c7Sdstaff {
5779cf8f45c7Sdstaff 	if (strcmp(sysevent_get_subclass_name(ev),
5780cf8f45c7Sdstaff 	    ZONE_EVENT_PING_SUBCLASS) == 0) {
5781cf8f45c7Sdstaff 		return (B_TRUE);
5782cf8f45c7Sdstaff 	} else {
5783cf8f45c7Sdstaff 		return (B_FALSE);
5784cf8f45c7Sdstaff 	}
5785cf8f45c7Sdstaff }
5786cf8f45c7Sdstaff 
5787cf8f45c7Sdstaff static boolean_t
5788cf8f45c7Sdstaff is_my_ping(sysevent_t *ev)
5789cf8f45c7Sdstaff {
5790cf8f45c7Sdstaff 	const char *sender;
5791cf8f45c7Sdstaff 	char mypid[sizeof (pid_t) * 3 + 1];
5792cf8f45c7Sdstaff 
5793cf8f45c7Sdstaff 	(void) snprintf(mypid, sizeof (mypid), "%i", getpid());
5794cf8f45c7Sdstaff 	sender = string_get_tok(sysevent_get_pub(ev), ':', 3);
5795cf8f45c7Sdstaff 	if (sender == NULL)
5796cf8f45c7Sdstaff 		return (B_FALSE);
5797cf8f45c7Sdstaff 	if (strcmp(sender, mypid) != 0)
5798cf8f45c7Sdstaff 		return (B_FALSE);
5799cf8f45c7Sdstaff 	return (B_TRUE);
5800cf8f45c7Sdstaff }
5801cf8f45c7Sdstaff 
5802cf8f45c7Sdstaff static int
5803cf8f45c7Sdstaff do_callback(struct znotify *zevtchan, sysevent_t *ev)
5804cf8f45c7Sdstaff {
5805cf8f45c7Sdstaff 	nvlist_t *l;
5806cf8f45c7Sdstaff 	int zid;
5807cf8f45c7Sdstaff 	char *zonename;
5808cf8f45c7Sdstaff 	char *newstate;
5809cf8f45c7Sdstaff 	char *oldstate;
5810cf8f45c7Sdstaff 	int ret;
5811cf8f45c7Sdstaff 	hrtime_t when;
5812cf8f45c7Sdstaff 
5813cf8f45c7Sdstaff 	if (strcmp(sysevent_get_subclass_name(ev),
5814cf8f45c7Sdstaff 	    ZONE_EVENT_STATUS_SUBCLASS) == 0) {
5815cf8f45c7Sdstaff 
5816cf8f45c7Sdstaff 		if (sysevent_get_attr_list(ev, &l) != 0) {
5817cf8f45c7Sdstaff 			if (errno == ENOMEM) {
5818cf8f45c7Sdstaff 				zevtchan->zn_failure_count++;
5819cf8f45c7Sdstaff 				return (EAGAIN);
5820cf8f45c7Sdstaff 			}
5821cf8f45c7Sdstaff 			return (0);
5822cf8f45c7Sdstaff 		}
5823cf8f45c7Sdstaff 		ret = 0;
5824cf8f45c7Sdstaff 
5825cf8f45c7Sdstaff 		if ((nvlist_lookup_string(l, ZONE_CB_NAME, &zonename) == 0) &&
5826cf8f45c7Sdstaff 		    (nvlist_lookup_string(l, ZONE_CB_NEWSTATE, &newstate)
5827cf8f45c7Sdstaff 		    == 0) &&
5828cf8f45c7Sdstaff 		    (nvlist_lookup_string(l, ZONE_CB_OLDSTATE, &oldstate)
5829cf8f45c7Sdstaff 		    == 0) &&
5830cf8f45c7Sdstaff 		    (nvlist_lookup_uint64(l, ZONE_CB_TIMESTAMP,
5831cf8f45c7Sdstaff 		    (uint64_t *)&when) == 0) &&
5832cf8f45c7Sdstaff 		    (nvlist_lookup_int32(l, ZONE_CB_ZONEID, &zid) == 0)) {
5833cf8f45c7Sdstaff 			ret = zevtchan->zn_callback(zonename, zid, newstate,
5834cf8f45c7Sdstaff 			    oldstate, when, zevtchan->zn_private);
5835cf8f45c7Sdstaff 		}
5836cf8f45c7Sdstaff 
5837cf8f45c7Sdstaff 		zevtchan->zn_failure_count = 0;
5838cf8f45c7Sdstaff 		nvlist_free(l);
5839cf8f45c7Sdstaff 		return (ret);
5840cf8f45c7Sdstaff 	} else {
5841cf8f45c7Sdstaff 		/*
5842cf8f45c7Sdstaff 		 * We have received an event in an unknown subclass. Ignore.
5843cf8f45c7Sdstaff 		 */
5844cf8f45c7Sdstaff 		zevtchan->zn_failure_count = 0;
5845cf8f45c7Sdstaff 		return (0);
5846cf8f45c7Sdstaff 	}
5847cf8f45c7Sdstaff }
5848cf8f45c7Sdstaff 
5849cf8f45c7Sdstaff static int
5850cf8f45c7Sdstaff zn_cb(sysevent_t *ev, void *p)
5851cf8f45c7Sdstaff {
5852cf8f45c7Sdstaff 	struct znotify *zevtchan = p;
5853cf8f45c7Sdstaff 	int error;
5854cf8f45c7Sdstaff 
5855cf8f45c7Sdstaff 	(void) pthread_mutex_lock(&(zevtchan->zn_mutex));
5856cf8f45c7Sdstaff 
5857cf8f45c7Sdstaff 	if (is_ping(ev) && !is_my_ping(ev)) {
5858cf8f45c7Sdstaff 		(void) pthread_mutex_unlock((&zevtchan->zn_mutex));
5859cf8f45c7Sdstaff 		return (0);
5860cf8f45c7Sdstaff 	}
5861cf8f45c7Sdstaff 
5862cf8f45c7Sdstaff 	if (zevtchan->zn_state == ZN_LOCKED) {
5863cf8f45c7Sdstaff 		assert(!is_ping(ev));
5864cf8f45c7Sdstaff 		zevtchan->zn_failed = B_TRUE;
5865cf8f45c7Sdstaff 		(void) pthread_mutex_unlock(&(zevtchan->zn_mutex));
5866cf8f45c7Sdstaff 		return (0);
5867cf8f45c7Sdstaff 	}
5868cf8f45c7Sdstaff 
5869cf8f45c7Sdstaff 	if (zevtchan->zn_state == ZN_PING_INFLIGHT) {
5870cf8f45c7Sdstaff 		if (is_ping(ev)) {
5871cf8f45c7Sdstaff 			zevtchan->zn_state = ZN_PING_RECEIVED;
5872cf8f45c7Sdstaff 			(void) pthread_cond_signal(&(zevtchan->zn_cond));
5873cf8f45c7Sdstaff 			(void) pthread_mutex_unlock(&(zevtchan->zn_mutex));
5874cf8f45c7Sdstaff 			return (0);
5875cf8f45c7Sdstaff 		} else {
5876cf8f45c7Sdstaff 			zevtchan->zn_failed = B_TRUE;
5877cf8f45c7Sdstaff 			(void) pthread_mutex_unlock(&(zevtchan->zn_mutex));
5878cf8f45c7Sdstaff 			return (0);
5879cf8f45c7Sdstaff 		}
5880cf8f45c7Sdstaff 	}
5881cf8f45c7Sdstaff 
5882cf8f45c7Sdstaff 	if (zevtchan->zn_state == ZN_UNLOCKED) {
5883cf8f45c7Sdstaff 
5884cf8f45c7Sdstaff 		error = do_callback(zevtchan, ev);
5885cf8f45c7Sdstaff 		(void) pthread_mutex_unlock(&(zevtchan->zn_mutex));
5886cf8f45c7Sdstaff 		/*
5887cf8f45c7Sdstaff 		 * Every ENOMEM failure causes do_callback to increment
5888cf8f45c7Sdstaff 		 * zn_failure_count and every success causes it to
5889cf8f45c7Sdstaff 		 * set zn_failure_count to zero.  If we got EAGAIN,
5890cf8f45c7Sdstaff 		 * we will sleep for zn_failure_count seconds and return
5891cf8f45c7Sdstaff 		 * EAGAIN to gpec to try again.
5892cf8f45c7Sdstaff 		 *
5893cf8f45c7Sdstaff 		 * After 55 seconds, or 10 try's we give up and drop the
5894cf8f45c7Sdstaff 		 * event.
5895cf8f45c7Sdstaff 		 */
5896cf8f45c7Sdstaff 		if (error == EAGAIN) {
5897cf8f45c7Sdstaff 			if (zevtchan->zn_failure_count > ZONE_CB_RETRY_COUNT) {
5898cf8f45c7Sdstaff 				return (0);
5899cf8f45c7Sdstaff 			}
5900cf8f45c7Sdstaff 			(void) sleep(zevtchan->zn_failure_count);
5901cf8f45c7Sdstaff 		}
5902cf8f45c7Sdstaff 		return (error);
5903cf8f45c7Sdstaff 	}
5904cf8f45c7Sdstaff 
5905cf8f45c7Sdstaff 	if (zevtchan->zn_state == ZN_PING_RECEIVED) {
5906cf8f45c7Sdstaff 		(void) pthread_mutex_unlock(&(zevtchan->zn_mutex));
5907cf8f45c7Sdstaff 		return (0);
5908cf8f45c7Sdstaff 	}
5909cf8f45c7Sdstaff 
5910cf8f45c7Sdstaff 	abort();
5911cf8f45c7Sdstaff 	return (0);
5912cf8f45c7Sdstaff }
5913cf8f45c7Sdstaff 
5914cf8f45c7Sdstaff void
5915cf8f45c7Sdstaff zonecfg_notify_critical_enter(void *h)
5916cf8f45c7Sdstaff {
5917cf8f45c7Sdstaff 	struct znotify *zevtchan = h;
5918cf8f45c7Sdstaff 
5919cf8f45c7Sdstaff 	(void) pthread_mutex_lock(&(zevtchan->zn_bigmutex));
5920cf8f45c7Sdstaff 	zevtchan->zn_state = ZN_LOCKED;
5921cf8f45c7Sdstaff }
5922cf8f45c7Sdstaff 
5923cf8f45c7Sdstaff int
5924cf8f45c7Sdstaff zonecfg_notify_critical_exit(void * h)
5925cf8f45c7Sdstaff {
5926cf8f45c7Sdstaff 
5927cf8f45c7Sdstaff 	struct znotify *zevtchan = h;
5928cf8f45c7Sdstaff 
5929cf8f45c7Sdstaff 	if (zevtchan->zn_state == ZN_UNLOCKED)
5930cf8f45c7Sdstaff 		return (0);
5931cf8f45c7Sdstaff 
5932cf8f45c7Sdstaff 	(void) pthread_mutex_lock(&(zevtchan->zn_mutex));
5933cf8f45c7Sdstaff 	zevtchan->zn_state = ZN_PING_INFLIGHT;
5934cf8f45c7Sdstaff 
5935ee519a1fSgjelinek 	(void) sysevent_evc_publish(zevtchan->zn_eventchan,
5936ee519a1fSgjelinek 	    ZONE_EVENT_STATUS_CLASS,
5937cf8f45c7Sdstaff 	    ZONE_EVENT_PING_SUBCLASS, ZONE_EVENT_PING_PUBLISHER,
5938cf8f45c7Sdstaff 	    zevtchan->zn_subscriber_id, NULL, EVCH_SLEEP);
5939cf8f45c7Sdstaff 
5940cf8f45c7Sdstaff 	while (zevtchan->zn_state != ZN_PING_RECEIVED) {
5941cf8f45c7Sdstaff 		(void) pthread_cond_wait(&(zevtchan->zn_cond),
5942cf8f45c7Sdstaff 		    &(zevtchan->zn_mutex));
5943cf8f45c7Sdstaff 	}
5944cf8f45c7Sdstaff 
5945cf8f45c7Sdstaff 	if (zevtchan->zn_failed == B_TRUE) {
5946cf8f45c7Sdstaff 		zevtchan->zn_state = ZN_LOCKED;
5947cf8f45c7Sdstaff 		zevtchan->zn_failed = B_FALSE;
5948cf8f45c7Sdstaff 		(void) pthread_mutex_unlock(&(zevtchan->zn_mutex));
5949cf8f45c7Sdstaff 		return (1);
5950cf8f45c7Sdstaff 	}
5951cf8f45c7Sdstaff 
5952cf8f45c7Sdstaff 	zevtchan->zn_state = ZN_UNLOCKED;
5953cf8f45c7Sdstaff 	(void) pthread_mutex_unlock(&(zevtchan->zn_mutex));
5954cf8f45c7Sdstaff 	(void) pthread_mutex_unlock(&(zevtchan->zn_bigmutex));
5955cf8f45c7Sdstaff 	return (0);
5956cf8f45c7Sdstaff }
5957cf8f45c7Sdstaff 
5958cf8f45c7Sdstaff void
5959cf8f45c7Sdstaff zonecfg_notify_critical_abort(void *h)
5960cf8f45c7Sdstaff {
5961cf8f45c7Sdstaff 	struct znotify *zevtchan = h;
5962cf8f45c7Sdstaff 
5963cf8f45c7Sdstaff 	zevtchan->zn_state = ZN_UNLOCKED;
5964cf8f45c7Sdstaff 	zevtchan->zn_failed = B_FALSE;
5965cf8f45c7Sdstaff 	/*
5966cf8f45c7Sdstaff 	 * Don't do anything about zn_lock. If it is held, it could only be
5967cf8f45c7Sdstaff 	 * held by zn_cb and it will be unlocked soon.
5968cf8f45c7Sdstaff 	 */
5969cf8f45c7Sdstaff 	(void) pthread_mutex_unlock(&(zevtchan->zn_bigmutex));
5970cf8f45c7Sdstaff }
5971cf8f45c7Sdstaff 
5972cf8f45c7Sdstaff void *
5973cf8f45c7Sdstaff zonecfg_notify_bind(int(*func)(const char *zonename, zoneid_t zid,
5974cf8f45c7Sdstaff     const char *newstate, const char *oldstate, hrtime_t when, void *p),
5975cf8f45c7Sdstaff     void *p)
5976cf8f45c7Sdstaff {
5977cf8f45c7Sdstaff 	struct znotify *zevtchan;
5978cf8f45c7Sdstaff 	int i = 1;
5979cf8f45c7Sdstaff 	int r;
5980cf8f45c7Sdstaff 
5981cf8f45c7Sdstaff 	zevtchan = malloc(sizeof (struct znotify));
5982cf8f45c7Sdstaff 
5983cf8f45c7Sdstaff 	if (zevtchan == NULL)
5984cf8f45c7Sdstaff 		return (NULL);
5985cf8f45c7Sdstaff 
5986cf8f45c7Sdstaff 	zevtchan->zn_private = p;
5987cf8f45c7Sdstaff 	zevtchan->zn_callback = func;
5988cf8f45c7Sdstaff 	zevtchan->zn_state = ZN_UNLOCKED;
5989cf8f45c7Sdstaff 	zevtchan->zn_failed = B_FALSE;
5990cf8f45c7Sdstaff 
5991cf8f45c7Sdstaff 	if (pthread_mutex_init(&(zevtchan->zn_mutex), NULL))
59929d4be64eSdstaff 		goto out3;
5993cf8f45c7Sdstaff 	if (pthread_cond_init(&(zevtchan->zn_cond), NULL)) {
5994cf8f45c7Sdstaff 		(void) pthread_mutex_destroy(&(zevtchan->zn_mutex));
59959d4be64eSdstaff 		goto out3;
5996cf8f45c7Sdstaff 	}
5997cf8f45c7Sdstaff 	if (pthread_mutex_init(&(zevtchan->zn_bigmutex), NULL)) {
5998cf8f45c7Sdstaff 		(void) pthread_mutex_destroy(&(zevtchan->zn_mutex));
5999cf8f45c7Sdstaff 		(void) pthread_cond_destroy(&(zevtchan->zn_cond));
60009d4be64eSdstaff 		goto out3;
6001cf8f45c7Sdstaff 	}
6002cf8f45c7Sdstaff 
6003cf8f45c7Sdstaff 	if (sysevent_evc_bind(ZONE_EVENT_CHANNEL, &(zevtchan->zn_eventchan),
6004cf8f45c7Sdstaff 	    0) != 0)
6005cf8f45c7Sdstaff 		goto out2;
6006cf8f45c7Sdstaff 
6007cf8f45c7Sdstaff 	do {
6008cf8f45c7Sdstaff 		/*
6009cf8f45c7Sdstaff 		 * At 4 digits the subscriber ID gets too long and we have
6010cf8f45c7Sdstaff 		 * no chance of successfully registering.
6011cf8f45c7Sdstaff 		 */
6012cf8f45c7Sdstaff 		if (i > 999)
60139d4be64eSdstaff 			goto out1;
6014cf8f45c7Sdstaff 
6015cf8f45c7Sdstaff 		(void) sprintf(zevtchan->zn_subscriber_id, "zone_%li_%i",
6016cf8f45c7Sdstaff 		    getpid() % 999999l, i);
6017cf8f45c7Sdstaff 
6018cf8f45c7Sdstaff 		r = sysevent_evc_subscribe(zevtchan->zn_eventchan,
6019cf8f45c7Sdstaff 		    zevtchan->zn_subscriber_id, ZONE_EVENT_STATUS_CLASS, zn_cb,
6020cf8f45c7Sdstaff 		    zevtchan, 0);
6021cf8f45c7Sdstaff 
6022cf8f45c7Sdstaff 		i++;
6023cf8f45c7Sdstaff 
6024cf8f45c7Sdstaff 	} while (r);
6025cf8f45c7Sdstaff 
6026cf8f45c7Sdstaff 	return (zevtchan);
60279d4be64eSdstaff out1:
6028cf8f45c7Sdstaff 	sysevent_evc_unbind(zevtchan->zn_eventchan);
60299d4be64eSdstaff out2:
6030cf8f45c7Sdstaff 	(void) pthread_mutex_destroy(&zevtchan->zn_mutex);
6031cf8f45c7Sdstaff 	(void) pthread_cond_destroy(&zevtchan->zn_cond);
6032cf8f45c7Sdstaff 	(void) pthread_mutex_destroy(&(zevtchan->zn_bigmutex));
60339d4be64eSdstaff out3:
6034cf8f45c7Sdstaff 	free(zevtchan);
6035cf8f45c7Sdstaff 
6036cf8f45c7Sdstaff 	return (NULL);
6037cf8f45c7Sdstaff }
6038cf8f45c7Sdstaff 
6039cf8f45c7Sdstaff void
6040cf8f45c7Sdstaff zonecfg_notify_unbind(void *handle)
6041cf8f45c7Sdstaff {
6042cf8f45c7Sdstaff 
6043cf8f45c7Sdstaff 	int ret;
6044cf8f45c7Sdstaff 
6045cf8f45c7Sdstaff 	sysevent_evc_unbind(((struct znotify *)handle)->zn_eventchan);
6046cf8f45c7Sdstaff 	/*
6047cf8f45c7Sdstaff 	 * Check that all evc threads have gone away. This should be
6048cf8f45c7Sdstaff 	 * enforced by sysevent_evc_unbind.
6049cf8f45c7Sdstaff 	 */
6050cf8f45c7Sdstaff 	ret = pthread_mutex_trylock(&((struct znotify *)handle)->zn_mutex);
6051cf8f45c7Sdstaff 
6052cf8f45c7Sdstaff 	if (ret)
6053cf8f45c7Sdstaff 		abort();
6054cf8f45c7Sdstaff 
6055cf8f45c7Sdstaff 	(void) pthread_mutex_unlock(&((struct znotify *)handle)->zn_mutex);
6056cf8f45c7Sdstaff 	(void) pthread_mutex_destroy(&((struct znotify *)handle)->zn_mutex);
6057cf8f45c7Sdstaff 	(void) pthread_cond_destroy(&((struct znotify *)handle)->zn_cond);
6058cf8f45c7Sdstaff 	(void) pthread_mutex_destroy(&((struct znotify *)handle)->zn_bigmutex);
6059cf8f45c7Sdstaff 
6060cf8f45c7Sdstaff 	free(handle);
6061cf8f45c7Sdstaff }
6062cf8f45c7Sdstaff 
6063fa9e4066Sahrens static int
6064fa9e4066Sahrens zonecfg_add_ds_core(zone_dochandle_t handle, struct zone_dstab *tabptr)
6065fa9e4066Sahrens {
6066fa9e4066Sahrens 	xmlNodePtr newnode, cur = handle->zone_dh_cur;
6067fa9e4066Sahrens 	int err;
6068fa9e4066Sahrens 
6069fa9e4066Sahrens 	newnode = xmlNewTextChild(cur, NULL, DTD_ELEM_DATASET, NULL);
6070fa9e4066Sahrens 	if ((err = newprop(newnode, DTD_ATTR_NAME,
6071fa9e4066Sahrens 	    tabptr->zone_dataset_name)) != Z_OK)
6072fa9e4066Sahrens 		return (err);
6073fa9e4066Sahrens 	return (Z_OK);
6074fa9e4066Sahrens }
6075fa9e4066Sahrens 
6076fa9e4066Sahrens int
6077fa9e4066Sahrens zonecfg_add_ds(zone_dochandle_t handle, struct zone_dstab *tabptr)
6078fa9e4066Sahrens {
6079fa9e4066Sahrens 	int err;
6080fa9e4066Sahrens 
6081fa9e4066Sahrens 	if (tabptr == NULL)
6082fa9e4066Sahrens 		return (Z_INVAL);
6083fa9e4066Sahrens 
6084fa9e4066Sahrens 	if ((err = operation_prep(handle)) != Z_OK)
6085fa9e4066Sahrens 		return (err);
6086fa9e4066Sahrens 
6087fa9e4066Sahrens 	if ((err = zonecfg_add_ds_core(handle, tabptr)) != Z_OK)
6088fa9e4066Sahrens 		return (err);
6089fa9e4066Sahrens 
6090fa9e4066Sahrens 	return (Z_OK);
6091fa9e4066Sahrens }
6092fa9e4066Sahrens 
6093fa9e4066Sahrens static int
6094fa9e4066Sahrens zonecfg_delete_ds_core(zone_dochandle_t handle, struct zone_dstab *tabptr)
6095fa9e4066Sahrens {
6096fa9e4066Sahrens 	xmlNodePtr cur = handle->zone_dh_cur;
6097fa9e4066Sahrens 
6098fa9e4066Sahrens 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
6099fa9e4066Sahrens 		if (xmlStrcmp(cur->name, DTD_ELEM_DATASET))
6100fa9e4066Sahrens 			continue;
6101fa9e4066Sahrens 
6102fa9e4066Sahrens 		if (match_prop(cur, DTD_ATTR_NAME,
6103fa9e4066Sahrens 		    tabptr->zone_dataset_name)) {
6104fa9e4066Sahrens 			xmlUnlinkNode(cur);
6105fa9e4066Sahrens 			xmlFreeNode(cur);
6106fa9e4066Sahrens 			return (Z_OK);
6107fa9e4066Sahrens 		}
6108fa9e4066Sahrens 	}
6109fa9e4066Sahrens 	return (Z_NO_RESOURCE_ID);
6110fa9e4066Sahrens }
6111fa9e4066Sahrens 
6112fa9e4066Sahrens int
6113fa9e4066Sahrens zonecfg_delete_ds(zone_dochandle_t handle, struct zone_dstab *tabptr)
6114fa9e4066Sahrens {
6115fa9e4066Sahrens 	int err;
6116fa9e4066Sahrens 
6117fa9e4066Sahrens 	if (tabptr == NULL)
6118fa9e4066Sahrens 		return (Z_INVAL);
6119fa9e4066Sahrens 
6120fa9e4066Sahrens 	if ((err = operation_prep(handle)) != Z_OK)
6121fa9e4066Sahrens 		return (err);
6122fa9e4066Sahrens 
6123fa9e4066Sahrens 	if ((err = zonecfg_delete_ds_core(handle, tabptr)) != Z_OK)
6124fa9e4066Sahrens 		return (err);
6125fa9e4066Sahrens 
6126fa9e4066Sahrens 	return (Z_OK);
6127fa9e4066Sahrens }
6128fa9e4066Sahrens 
6129fa9e4066Sahrens int
6130fa9e4066Sahrens zonecfg_modify_ds(
6131fa9e4066Sahrens 	zone_dochandle_t handle,
6132fa9e4066Sahrens 	struct zone_dstab *oldtabptr,
6133fa9e4066Sahrens 	struct zone_dstab *newtabptr)
6134fa9e4066Sahrens {
6135fa9e4066Sahrens 	int err;
6136fa9e4066Sahrens 
6137fa9e4066Sahrens 	if (oldtabptr == NULL || newtabptr == NULL)
6138fa9e4066Sahrens 		return (Z_INVAL);
6139fa9e4066Sahrens 
6140fa9e4066Sahrens 	if ((err = operation_prep(handle)) != Z_OK)
6141fa9e4066Sahrens 		return (err);
6142fa9e4066Sahrens 
6143fa9e4066Sahrens 	if ((err = zonecfg_delete_ds_core(handle, oldtabptr)) != Z_OK)
6144fa9e4066Sahrens 		return (err);
6145fa9e4066Sahrens 
6146fa9e4066Sahrens 	if ((err = zonecfg_add_ds_core(handle, newtabptr)) != Z_OK)
6147fa9e4066Sahrens 		return (err);
6148fa9e4066Sahrens 
6149fa9e4066Sahrens 	return (Z_OK);
6150fa9e4066Sahrens }
6151fa9e4066Sahrens 
6152fa9e4066Sahrens int
6153fa9e4066Sahrens zonecfg_lookup_ds(zone_dochandle_t handle, struct zone_dstab *tabptr)
6154fa9e4066Sahrens {
6155fa9e4066Sahrens 	xmlNodePtr cur, firstmatch;
6156fa9e4066Sahrens 	int err;
6157fa9e4066Sahrens 	char dataset[MAXNAMELEN];
6158fa9e4066Sahrens 
6159fa9e4066Sahrens 	if (tabptr == NULL)
6160fa9e4066Sahrens 		return (Z_INVAL);
6161fa9e4066Sahrens 
6162fa9e4066Sahrens 	if ((err = operation_prep(handle)) != Z_OK)
6163fa9e4066Sahrens 		return (err);
6164fa9e4066Sahrens 
6165fa9e4066Sahrens 	cur = handle->zone_dh_cur;
6166fa9e4066Sahrens 	firstmatch = NULL;
6167fa9e4066Sahrens 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
6168fa9e4066Sahrens 		if (xmlStrcmp(cur->name, DTD_ELEM_DATASET))
6169fa9e4066Sahrens 			continue;
6170fa9e4066Sahrens 		if (strlen(tabptr->zone_dataset_name) > 0) {
6171fa9e4066Sahrens 			if ((fetchprop(cur, DTD_ATTR_NAME, dataset,
6172fa9e4066Sahrens 			    sizeof (dataset)) == Z_OK) &&
6173fa9e4066Sahrens 			    (strcmp(tabptr->zone_dataset_name,
6174fa9e4066Sahrens 			    dataset) == 0)) {
6175fa9e4066Sahrens 				if (firstmatch == NULL)
6176fa9e4066Sahrens 					firstmatch = cur;
6177fa9e4066Sahrens 				else
6178fa9e4066Sahrens 					return (Z_INSUFFICIENT_SPEC);
6179fa9e4066Sahrens 			}
6180fa9e4066Sahrens 		}
6181fa9e4066Sahrens 	}
6182fa9e4066Sahrens 	if (firstmatch == NULL)
6183fa9e4066Sahrens 		return (Z_NO_RESOURCE_ID);
6184fa9e4066Sahrens 
6185fa9e4066Sahrens 	cur = firstmatch;
6186fa9e4066Sahrens 
6187fa9e4066Sahrens 	if ((err = fetchprop(cur, DTD_ATTR_NAME, tabptr->zone_dataset_name,
6188fa9e4066Sahrens 	    sizeof (tabptr->zone_dataset_name))) != Z_OK)
6189fa9e4066Sahrens 		return (err);
6190fa9e4066Sahrens 
6191fa9e4066Sahrens 	return (Z_OK);
6192fa9e4066Sahrens }
6193fa9e4066Sahrens 
6194fa9e4066Sahrens int
6195fa9e4066Sahrens zonecfg_setdsent(zone_dochandle_t handle)
6196fa9e4066Sahrens {
6197fa9e4066Sahrens 	return (zonecfg_setent(handle));
6198fa9e4066Sahrens }
6199fa9e4066Sahrens 
6200fa9e4066Sahrens int
6201fa9e4066Sahrens zonecfg_getdsent(zone_dochandle_t handle, struct zone_dstab *tabptr)
6202fa9e4066Sahrens {
6203fa9e4066Sahrens 	xmlNodePtr cur;
6204fa9e4066Sahrens 	int err;
6205fa9e4066Sahrens 
6206fa9e4066Sahrens 	if (handle == NULL)
6207fa9e4066Sahrens 		return (Z_INVAL);
6208fa9e4066Sahrens 
6209fa9e4066Sahrens 	if ((cur = handle->zone_dh_cur) == NULL)
6210fa9e4066Sahrens 		return (Z_NO_ENTRY);
6211fa9e4066Sahrens 
6212fa9e4066Sahrens 	for (; cur != NULL; cur = cur->next)
6213fa9e4066Sahrens 		if (!xmlStrcmp(cur->name, DTD_ELEM_DATASET))
6214fa9e4066Sahrens 			break;
6215fa9e4066Sahrens 	if (cur == NULL) {
6216fa9e4066Sahrens 		handle->zone_dh_cur = handle->zone_dh_top;
6217fa9e4066Sahrens 		return (Z_NO_ENTRY);
6218fa9e4066Sahrens 	}
6219fa9e4066Sahrens 
6220fa9e4066Sahrens 	if ((err = fetchprop(cur, DTD_ATTR_NAME, tabptr->zone_dataset_name,
6221fa9e4066Sahrens 	    sizeof (tabptr->zone_dataset_name))) != Z_OK) {
6222fa9e4066Sahrens 		handle->zone_dh_cur = handle->zone_dh_top;
6223fa9e4066Sahrens 		return (err);
6224fa9e4066Sahrens 	}
6225fa9e4066Sahrens 
6226fa9e4066Sahrens 	handle->zone_dh_cur = cur->next;
6227fa9e4066Sahrens 	return (Z_OK);
6228fa9e4066Sahrens }
6229fa9e4066Sahrens 
6230fa9e4066Sahrens int
6231fa9e4066Sahrens zonecfg_enddsent(zone_dochandle_t handle)
6232fa9e4066Sahrens {
6233fa9e4066Sahrens 	return (zonecfg_endent(handle));
6234fa9e4066Sahrens }
6235ee519a1fSgjelinek 
62360209230bSgjelinek /*
62370209230bSgjelinek  * Support for aliased rctls; that is, rctls that have simplified names in
62380209230bSgjelinek  * zonecfg.  For example, max-lwps is an alias for a well defined zone.max-lwps
62390209230bSgjelinek  * rctl.  If there are multiple existing values for one of these rctls or if
62400209230bSgjelinek  * there is a single value that does not match the well defined template (i.e.
62410209230bSgjelinek  * it has a different action) then we cannot treat the rctl as having an alias
62420209230bSgjelinek  * so we return Z_ALIAS_DISALLOW.  That means that the rctl cannot be
62430209230bSgjelinek  * managed in zonecfg via an alias and that the standard rctl syntax must be
62440209230bSgjelinek  * used.
62450209230bSgjelinek  *
62460209230bSgjelinek  * The possible return values are:
62470209230bSgjelinek  *	Z_NO_PROPERTY_ID - invalid alias name
62480209230bSgjelinek  *	Z_ALIAS_DISALLOW - pre-existing, incompatible rctl definition
62490209230bSgjelinek  *	Z_NO_ENTRY - no rctl is configured for this alias
62500209230bSgjelinek  *	Z_OK - we got a valid rctl for the specified alias
62510209230bSgjelinek  */
62520209230bSgjelinek int
62530209230bSgjelinek zonecfg_get_aliased_rctl(zone_dochandle_t handle, char *name, uint64_t *rval)
62540209230bSgjelinek {
62550209230bSgjelinek 	boolean_t found = B_FALSE;
62560209230bSgjelinek 	boolean_t found_val = B_FALSE;
62570209230bSgjelinek 	xmlNodePtr cur, val;
62580209230bSgjelinek 	char savedname[MAXNAMELEN];
62590209230bSgjelinek 	struct zone_rctlvaltab rctl;
62600209230bSgjelinek 	int i;
62610209230bSgjelinek 	int err;
62620209230bSgjelinek 
62630209230bSgjelinek 	for (i = 0; aliases[i].shortname != NULL; i++)
62640209230bSgjelinek 		if (strcmp(name, aliases[i].shortname) == 0)
62650209230bSgjelinek 			break;
62660209230bSgjelinek 
62670209230bSgjelinek 	if (aliases[i].shortname == NULL)
62680209230bSgjelinek 		return (Z_NO_PROPERTY_ID);
62690209230bSgjelinek 
62700209230bSgjelinek 	if ((err = operation_prep(handle)) != Z_OK)
62710209230bSgjelinek 		return (err);
62720209230bSgjelinek 
62730209230bSgjelinek 	cur = handle->zone_dh_cur;
62740209230bSgjelinek 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
62750209230bSgjelinek 		if (xmlStrcmp(cur->name, DTD_ELEM_RCTL) != 0)
62760209230bSgjelinek 			continue;
62770209230bSgjelinek 		if ((fetchprop(cur, DTD_ATTR_NAME, savedname,
62780209230bSgjelinek 		    sizeof (savedname)) == Z_OK) &&
62790209230bSgjelinek 		    (strcmp(savedname, aliases[i].realname) == 0)) {
62800209230bSgjelinek 
62810209230bSgjelinek 			/*
62820209230bSgjelinek 			 * If we already saw one of these, we can't have an
62830209230bSgjelinek 			 * alias since we just found another.
62840209230bSgjelinek 			 */
62850209230bSgjelinek 			if (found)
62860209230bSgjelinek 				return (Z_ALIAS_DISALLOW);
62870209230bSgjelinek 			found = B_TRUE;
62880209230bSgjelinek 
62890209230bSgjelinek 			for (val = cur->xmlChildrenNode; val != NULL;
62900209230bSgjelinek 			    val = val->next) {
62910209230bSgjelinek 				/*
62920209230bSgjelinek 				 * If we already have one value, we can't have
62930209230bSgjelinek 				 * an alias since we just found another.
62940209230bSgjelinek 				 */
62950209230bSgjelinek 				if (found_val)
62960209230bSgjelinek 					return (Z_ALIAS_DISALLOW);
62970209230bSgjelinek 				found_val = B_TRUE;
62980209230bSgjelinek 
62990209230bSgjelinek 				if ((fetchprop(val, DTD_ATTR_PRIV,
63000209230bSgjelinek 				    rctl.zone_rctlval_priv,
63010209230bSgjelinek 				    sizeof (rctl.zone_rctlval_priv)) != Z_OK))
63020209230bSgjelinek 					break;
63030209230bSgjelinek 				if ((fetchprop(val, DTD_ATTR_LIMIT,
63040209230bSgjelinek 				    rctl.zone_rctlval_limit,
63050209230bSgjelinek 				    sizeof (rctl.zone_rctlval_limit)) != Z_OK))
63060209230bSgjelinek 					break;
63070209230bSgjelinek 				if ((fetchprop(val, DTD_ATTR_ACTION,
63080209230bSgjelinek 				    rctl.zone_rctlval_action,
63090209230bSgjelinek 				    sizeof (rctl.zone_rctlval_action)) != Z_OK))
63100209230bSgjelinek 					break;
63110209230bSgjelinek 			}
63120209230bSgjelinek 
63130209230bSgjelinek 			/* check priv and action match the expected vals */
63140209230bSgjelinek 			if (strcmp(rctl.zone_rctlval_priv,
63150209230bSgjelinek 			    aliases[i].priv) != 0 ||
63160209230bSgjelinek 			    strcmp(rctl.zone_rctlval_action,
63170209230bSgjelinek 			    aliases[i].action) != 0)
63180209230bSgjelinek 				return (Z_ALIAS_DISALLOW);
63190209230bSgjelinek 		}
63200209230bSgjelinek 	}
63210209230bSgjelinek 
63220209230bSgjelinek 	if (found) {
63230209230bSgjelinek 		*rval = strtoull(rctl.zone_rctlval_limit, NULL, 10);
63240209230bSgjelinek 		return (Z_OK);
63250209230bSgjelinek 	}
63260209230bSgjelinek 
63270209230bSgjelinek 	return (Z_NO_ENTRY);
63280209230bSgjelinek }
63290209230bSgjelinek 
63300209230bSgjelinek int
63310209230bSgjelinek zonecfg_rm_aliased_rctl(zone_dochandle_t handle, char *name)
63320209230bSgjelinek {
63330209230bSgjelinek 	int i;
63340209230bSgjelinek 	uint64_t val;
63350209230bSgjelinek 	struct zone_rctltab rctltab;
63360209230bSgjelinek 
63370209230bSgjelinek 	/*
63380209230bSgjelinek 	 * First check that we have a valid aliased rctl to remove.
63390209230bSgjelinek 	 * This will catch an rctl entry with non-standard values or
63400209230bSgjelinek 	 * multiple rctl values for this name.  We need to ignore those
63410209230bSgjelinek 	 * rctl entries.
63420209230bSgjelinek 	 */
63430209230bSgjelinek 	if (zonecfg_get_aliased_rctl(handle, name, &val) != Z_OK)
63440209230bSgjelinek 		return (Z_OK);
63450209230bSgjelinek 
63460209230bSgjelinek 	for (i = 0; aliases[i].shortname != NULL; i++)
63470209230bSgjelinek 		if (strcmp(name, aliases[i].shortname) == 0)
63480209230bSgjelinek 			break;
63490209230bSgjelinek 
63500209230bSgjelinek 	if (aliases[i].shortname == NULL)
63510209230bSgjelinek 		return (Z_NO_RESOURCE_ID);
63520209230bSgjelinek 
63530209230bSgjelinek 	(void) strlcpy(rctltab.zone_rctl_name, aliases[i].realname,
63540209230bSgjelinek 	    sizeof (rctltab.zone_rctl_name));
63550209230bSgjelinek 
63560209230bSgjelinek 	return (zonecfg_delete_rctl(handle, &rctltab));
63570209230bSgjelinek }
63580209230bSgjelinek 
63590209230bSgjelinek boolean_t
63600209230bSgjelinek zonecfg_aliased_rctl_ok(zone_dochandle_t handle, char *name)
63610209230bSgjelinek {
63620209230bSgjelinek 	uint64_t tmp_val;
63630209230bSgjelinek 
63640209230bSgjelinek 	switch (zonecfg_get_aliased_rctl(handle, name, &tmp_val)) {
63650209230bSgjelinek 	case Z_OK:
63660209230bSgjelinek 		/*FALLTHRU*/
63670209230bSgjelinek 	case Z_NO_ENTRY:
63680209230bSgjelinek 		return (B_TRUE);
63690209230bSgjelinek 	default:
63700209230bSgjelinek 		return (B_FALSE);
63710209230bSgjelinek 	}
63720209230bSgjelinek }
63730209230bSgjelinek 
63740209230bSgjelinek int
63750209230bSgjelinek zonecfg_set_aliased_rctl(zone_dochandle_t handle, char *name, uint64_t val)
63760209230bSgjelinek {
63770209230bSgjelinek 	int i;
63780209230bSgjelinek 	int err;
63790209230bSgjelinek 	struct zone_rctltab rctltab;
63800209230bSgjelinek 	struct zone_rctlvaltab *rctlvaltab;
63810209230bSgjelinek 	char buf[128];
63820209230bSgjelinek 
63830209230bSgjelinek 	if (!zonecfg_aliased_rctl_ok(handle, name))
63840209230bSgjelinek 		return (Z_ALIAS_DISALLOW);
63850209230bSgjelinek 
63860209230bSgjelinek 	for (i = 0; aliases[i].shortname != NULL; i++)
63870209230bSgjelinek 		if (strcmp(name, aliases[i].shortname) == 0)
63880209230bSgjelinek 			break;
63890209230bSgjelinek 
63900209230bSgjelinek 	if (aliases[i].shortname == NULL)
63910209230bSgjelinek 		return (Z_NO_RESOURCE_ID);
63920209230bSgjelinek 
63930209230bSgjelinek 	/* remove any pre-existing definition for this rctl */
63940209230bSgjelinek 	(void) zonecfg_rm_aliased_rctl(handle, name);
63950209230bSgjelinek 
63960209230bSgjelinek 	(void) strlcpy(rctltab.zone_rctl_name, aliases[i].realname,
63970209230bSgjelinek 	    sizeof (rctltab.zone_rctl_name));
63980209230bSgjelinek 
63990209230bSgjelinek 	rctltab.zone_rctl_valptr = NULL;
64000209230bSgjelinek 
64010209230bSgjelinek 	if ((rctlvaltab = calloc(1, sizeof (struct zone_rctlvaltab))) == NULL)
64020209230bSgjelinek 		return (Z_NOMEM);
64030209230bSgjelinek 
64040209230bSgjelinek 	(void) snprintf(buf, sizeof (buf), "%llu", (long long)val);
64050209230bSgjelinek 
64060209230bSgjelinek 	(void) strlcpy(rctlvaltab->zone_rctlval_priv, aliases[i].priv,
64070209230bSgjelinek 	    sizeof (rctlvaltab->zone_rctlval_priv));
64080209230bSgjelinek 	(void) strlcpy(rctlvaltab->zone_rctlval_limit, buf,
64090209230bSgjelinek 	    sizeof (rctlvaltab->zone_rctlval_limit));
64100209230bSgjelinek 	(void) strlcpy(rctlvaltab->zone_rctlval_action, aliases[i].action,
64110209230bSgjelinek 	    sizeof (rctlvaltab->zone_rctlval_action));
64120209230bSgjelinek 
64130209230bSgjelinek 	rctlvaltab->zone_rctlval_next = NULL;
64140209230bSgjelinek 
64150209230bSgjelinek 	if ((err = zonecfg_add_rctl_value(&rctltab, rctlvaltab)) != Z_OK)
64160209230bSgjelinek 		return (err);
64170209230bSgjelinek 
64180209230bSgjelinek 	return (zonecfg_add_rctl(handle, &rctltab));
64190209230bSgjelinek }
64200209230bSgjelinek 
64210209230bSgjelinek static int
64220209230bSgjelinek delete_tmp_pool(zone_dochandle_t handle)
64230209230bSgjelinek {
64240209230bSgjelinek 	int err;
64250209230bSgjelinek 	xmlNodePtr cur = handle->zone_dh_cur;
64260209230bSgjelinek 
64270209230bSgjelinek 	if ((err = operation_prep(handle)) != Z_OK)
64280209230bSgjelinek 		return (err);
64290209230bSgjelinek 
64300209230bSgjelinek 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
64310209230bSgjelinek 		if (xmlStrcmp(cur->name, DTD_ELEM_TMPPOOL) == 0) {
64320209230bSgjelinek 			xmlUnlinkNode(cur);
64330209230bSgjelinek 			xmlFreeNode(cur);
64340209230bSgjelinek 			return (Z_OK);
64350209230bSgjelinek 		}
64360209230bSgjelinek 	}
64370209230bSgjelinek 
64380209230bSgjelinek 	return (Z_NO_RESOURCE_ID);
64390209230bSgjelinek }
64400209230bSgjelinek 
64410209230bSgjelinek static int
64420209230bSgjelinek modify_tmp_pool(zone_dochandle_t handle, char *pool_importance)
64430209230bSgjelinek {
64440209230bSgjelinek 	int err;
64450209230bSgjelinek 	xmlNodePtr cur = handle->zone_dh_cur;
64460209230bSgjelinek 	xmlNodePtr newnode;
64470209230bSgjelinek 
64480209230bSgjelinek 	err = delete_tmp_pool(handle);
64490209230bSgjelinek 	if (err != Z_OK && err != Z_NO_RESOURCE_ID)
64500209230bSgjelinek 		return (err);
64510209230bSgjelinek 
64520209230bSgjelinek 	if (*pool_importance != '\0') {
64530209230bSgjelinek 		if ((err = operation_prep(handle)) != Z_OK)
64540209230bSgjelinek 			return (err);
64550209230bSgjelinek 
64560209230bSgjelinek 		newnode = xmlNewTextChild(cur, NULL, DTD_ELEM_TMPPOOL, NULL);
64570209230bSgjelinek 		if ((err = newprop(newnode, DTD_ATTR_IMPORTANCE,
64580209230bSgjelinek 		    pool_importance)) != Z_OK)
64590209230bSgjelinek 			return (err);
64600209230bSgjelinek 	}
64610209230bSgjelinek 
64620209230bSgjelinek 	return (Z_OK);
64630209230bSgjelinek }
64640209230bSgjelinek 
64650209230bSgjelinek static int
64660209230bSgjelinek add_pset_core(zone_dochandle_t handle, struct zone_psettab *tabptr)
64670209230bSgjelinek {
64680209230bSgjelinek 	xmlNodePtr newnode, cur = handle->zone_dh_cur;
64690209230bSgjelinek 	int err;
64700209230bSgjelinek 
64710209230bSgjelinek 	newnode = xmlNewTextChild(cur, NULL, DTD_ELEM_PSET, NULL);
64720209230bSgjelinek 	if ((err = newprop(newnode, DTD_ATTR_NCPU_MIN,
64730209230bSgjelinek 	    tabptr->zone_ncpu_min)) != Z_OK)
64740209230bSgjelinek 		return (err);
64750209230bSgjelinek 	if ((err = newprop(newnode, DTD_ATTR_NCPU_MAX,
64760209230bSgjelinek 	    tabptr->zone_ncpu_max)) != Z_OK)
64770209230bSgjelinek 		return (err);
64780209230bSgjelinek 
64790209230bSgjelinek 	if ((err = modify_tmp_pool(handle, tabptr->zone_importance)) != Z_OK)
64800209230bSgjelinek 		return (err);
64810209230bSgjelinek 
64820209230bSgjelinek 	return (Z_OK);
64830209230bSgjelinek }
64840209230bSgjelinek 
64850209230bSgjelinek int
64860209230bSgjelinek zonecfg_add_pset(zone_dochandle_t handle, struct zone_psettab *tabptr)
64870209230bSgjelinek {
64880209230bSgjelinek 	int err;
64890209230bSgjelinek 
64900209230bSgjelinek 	if (tabptr == NULL)
64910209230bSgjelinek 		return (Z_INVAL);
64920209230bSgjelinek 
64930209230bSgjelinek 	if ((err = operation_prep(handle)) != Z_OK)
64940209230bSgjelinek 		return (err);
64950209230bSgjelinek 
64960209230bSgjelinek 	if ((err = add_pset_core(handle, tabptr)) != Z_OK)
64970209230bSgjelinek 		return (err);
64980209230bSgjelinek 
64990209230bSgjelinek 	return (Z_OK);
65000209230bSgjelinek }
65010209230bSgjelinek 
65020209230bSgjelinek int
65030209230bSgjelinek zonecfg_delete_pset(zone_dochandle_t handle)
65040209230bSgjelinek {
65050209230bSgjelinek 	int err;
65060209230bSgjelinek 	int res = Z_NO_RESOURCE_ID;
65070209230bSgjelinek 	xmlNodePtr cur = handle->zone_dh_cur;
65080209230bSgjelinek 
65090209230bSgjelinek 	if ((err = operation_prep(handle)) != Z_OK)
65100209230bSgjelinek 		return (err);
65110209230bSgjelinek 
65120209230bSgjelinek 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
65130209230bSgjelinek 		if (xmlStrcmp(cur->name, DTD_ELEM_PSET) == 0) {
65140209230bSgjelinek 			xmlUnlinkNode(cur);
65150209230bSgjelinek 			xmlFreeNode(cur);
65160209230bSgjelinek 			res = Z_OK;
65170209230bSgjelinek 			break;
65180209230bSgjelinek 		}
65190209230bSgjelinek 	}
65200209230bSgjelinek 
65210209230bSgjelinek 	/*
65220209230bSgjelinek 	 * Once we have msets, we should check that a mset
65230209230bSgjelinek 	 * do not exist before we delete the tmp_pool data.
65240209230bSgjelinek 	 */
65250209230bSgjelinek 	err = delete_tmp_pool(handle);
65260209230bSgjelinek 	if (err != Z_OK && err != Z_NO_RESOURCE_ID)
65270209230bSgjelinek 		return (err);
65280209230bSgjelinek 
65290209230bSgjelinek 	return (res);
65300209230bSgjelinek }
65310209230bSgjelinek 
65320209230bSgjelinek int
65330209230bSgjelinek zonecfg_modify_pset(zone_dochandle_t handle, struct zone_psettab *tabptr)
65340209230bSgjelinek {
65350209230bSgjelinek 	int err;
65360209230bSgjelinek 
65370209230bSgjelinek 	if (tabptr == NULL)
65380209230bSgjelinek 		return (Z_INVAL);
65390209230bSgjelinek 
65400209230bSgjelinek 	if ((err = zonecfg_delete_pset(handle)) != Z_OK)
65410209230bSgjelinek 		return (err);
65420209230bSgjelinek 
65430209230bSgjelinek 	if ((err = add_pset_core(handle, tabptr)) != Z_OK)
65440209230bSgjelinek 		return (err);
65450209230bSgjelinek 
65460209230bSgjelinek 	return (Z_OK);
65470209230bSgjelinek }
65480209230bSgjelinek 
65490209230bSgjelinek int
65500209230bSgjelinek zonecfg_lookup_pset(zone_dochandle_t handle, struct zone_psettab *tabptr)
65510209230bSgjelinek {
65520209230bSgjelinek 	xmlNodePtr cur;
65530209230bSgjelinek 	int err;
65540209230bSgjelinek 	int res = Z_NO_ENTRY;
65550209230bSgjelinek 
65560209230bSgjelinek 	if (tabptr == NULL)
65570209230bSgjelinek 		return (Z_INVAL);
65580209230bSgjelinek 
65590209230bSgjelinek 	if ((err = operation_prep(handle)) != Z_OK)
65600209230bSgjelinek 		return (err);
65610209230bSgjelinek 
65620209230bSgjelinek 	/* this is an optional component */
65630209230bSgjelinek 	tabptr->zone_importance[0] = '\0';
65640209230bSgjelinek 
65650209230bSgjelinek 	cur = handle->zone_dh_cur;
65660209230bSgjelinek 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
65670209230bSgjelinek 		if (xmlStrcmp(cur->name, DTD_ELEM_PSET) == 0) {
65680209230bSgjelinek 			if ((err = fetchprop(cur, DTD_ATTR_NCPU_MIN,
65690209230bSgjelinek 			    tabptr->zone_ncpu_min,
65700209230bSgjelinek 			    sizeof (tabptr->zone_ncpu_min))) != Z_OK) {
65710209230bSgjelinek 				handle->zone_dh_cur = handle->zone_dh_top;
65720209230bSgjelinek 				return (err);
65730209230bSgjelinek 			}
65740209230bSgjelinek 
65750209230bSgjelinek 			if ((err = fetchprop(cur, DTD_ATTR_NCPU_MAX,
65760209230bSgjelinek 			    tabptr->zone_ncpu_max,
65770209230bSgjelinek 			    sizeof (tabptr->zone_ncpu_max))) != Z_OK) {
65780209230bSgjelinek 				handle->zone_dh_cur = handle->zone_dh_top;
65790209230bSgjelinek 				return (err);
65800209230bSgjelinek 			}
65810209230bSgjelinek 
65820209230bSgjelinek 			res = Z_OK;
65830209230bSgjelinek 
65840209230bSgjelinek 		} else if (xmlStrcmp(cur->name, DTD_ELEM_TMPPOOL) == 0) {
65850209230bSgjelinek 			if ((err = fetchprop(cur, DTD_ATTR_IMPORTANCE,
65860209230bSgjelinek 			    tabptr->zone_importance,
65870209230bSgjelinek 			    sizeof (tabptr->zone_importance))) != Z_OK) {
65880209230bSgjelinek 				handle->zone_dh_cur = handle->zone_dh_top;
65890209230bSgjelinek 				return (err);
65900209230bSgjelinek 			}
65910209230bSgjelinek 		}
65920209230bSgjelinek 	}
65930209230bSgjelinek 
65940209230bSgjelinek 	return (res);
65950209230bSgjelinek }
65960209230bSgjelinek 
65970209230bSgjelinek int
65980209230bSgjelinek zonecfg_getpsetent(zone_dochandle_t handle, struct zone_psettab *tabptr)
65990209230bSgjelinek {
66000209230bSgjelinek 	int err;
66010209230bSgjelinek 
66020209230bSgjelinek 	if ((err = zonecfg_setent(handle)) != Z_OK)
66030209230bSgjelinek 		return (err);
66040209230bSgjelinek 
66050209230bSgjelinek 	err = zonecfg_lookup_pset(handle, tabptr);
66060209230bSgjelinek 
66070209230bSgjelinek 	(void) zonecfg_endent(handle);
66080209230bSgjelinek 
66090209230bSgjelinek 	return (err);
66100209230bSgjelinek }
66110209230bSgjelinek 
66120209230bSgjelinek static int
66130209230bSgjelinek add_mcap(zone_dochandle_t handle, struct zone_mcaptab *tabptr)
66140209230bSgjelinek {
66150209230bSgjelinek 	xmlNodePtr newnode, cur = handle->zone_dh_cur;
66160209230bSgjelinek 	int err;
66170209230bSgjelinek 
66180209230bSgjelinek 	newnode = xmlNewTextChild(cur, NULL, DTD_ELEM_MCAP, NULL);
66190209230bSgjelinek 	if ((err = newprop(newnode, DTD_ATTR_PHYSCAP, tabptr->zone_physmem_cap))
66200209230bSgjelinek 	    != Z_OK)
66210209230bSgjelinek 		return (err);
66220209230bSgjelinek 
66230209230bSgjelinek 	return (Z_OK);
66240209230bSgjelinek }
66250209230bSgjelinek 
66260209230bSgjelinek int
66270209230bSgjelinek zonecfg_delete_mcap(zone_dochandle_t handle)
66280209230bSgjelinek {
66290209230bSgjelinek 	int err;
66300209230bSgjelinek 	xmlNodePtr cur = handle->zone_dh_cur;
66310209230bSgjelinek 
66320209230bSgjelinek 	if ((err = operation_prep(handle)) != Z_OK)
66330209230bSgjelinek 		return (err);
66340209230bSgjelinek 
66350209230bSgjelinek 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
66360209230bSgjelinek 		if (xmlStrcmp(cur->name, DTD_ELEM_MCAP) != 0)
66370209230bSgjelinek 			continue;
66380209230bSgjelinek 
66390209230bSgjelinek 		xmlUnlinkNode(cur);
66400209230bSgjelinek 		xmlFreeNode(cur);
66410209230bSgjelinek 		return (Z_OK);
66420209230bSgjelinek 	}
66430209230bSgjelinek 	return (Z_NO_RESOURCE_ID);
66440209230bSgjelinek }
66450209230bSgjelinek 
66460209230bSgjelinek int
66470209230bSgjelinek zonecfg_modify_mcap(zone_dochandle_t handle, struct zone_mcaptab *tabptr)
66480209230bSgjelinek {
66490209230bSgjelinek 	int err;
66500209230bSgjelinek 
66510209230bSgjelinek 	if (tabptr == NULL)
66520209230bSgjelinek 		return (Z_INVAL);
66530209230bSgjelinek 
66540209230bSgjelinek 	err = zonecfg_delete_mcap(handle);
66550209230bSgjelinek 	/* it is ok if there is no mcap entry */
66560209230bSgjelinek 	if (err != Z_OK && err != Z_NO_RESOURCE_ID)
66570209230bSgjelinek 		return (err);
66580209230bSgjelinek 
66590209230bSgjelinek 	if ((err = add_mcap(handle, tabptr)) != Z_OK)
66600209230bSgjelinek 		return (err);
66610209230bSgjelinek 
66620209230bSgjelinek 	return (Z_OK);
66630209230bSgjelinek }
66640209230bSgjelinek 
66650209230bSgjelinek int
66660209230bSgjelinek zonecfg_lookup_mcap(zone_dochandle_t handle, struct zone_mcaptab *tabptr)
66670209230bSgjelinek {
66680209230bSgjelinek 	xmlNodePtr cur;
66690209230bSgjelinek 	int err;
66700209230bSgjelinek 
66710209230bSgjelinek 	if (tabptr == NULL)
66720209230bSgjelinek 		return (Z_INVAL);
66730209230bSgjelinek 
66740209230bSgjelinek 	if ((err = operation_prep(handle)) != Z_OK)
66750209230bSgjelinek 		return (err);
66760209230bSgjelinek 
66770209230bSgjelinek 	cur = handle->zone_dh_cur;
66780209230bSgjelinek 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
66790209230bSgjelinek 		if (xmlStrcmp(cur->name, DTD_ELEM_MCAP) != 0)
66800209230bSgjelinek 			continue;
66810209230bSgjelinek 		if ((err = fetchprop(cur, DTD_ATTR_PHYSCAP,
66820209230bSgjelinek 		    tabptr->zone_physmem_cap,
66830209230bSgjelinek 		    sizeof (tabptr->zone_physmem_cap))) != Z_OK) {
66840209230bSgjelinek 			handle->zone_dh_cur = handle->zone_dh_top;
66850209230bSgjelinek 			return (err);
66860209230bSgjelinek 		}
66870209230bSgjelinek 
66880209230bSgjelinek 		return (Z_OK);
66890209230bSgjelinek 	}
66900209230bSgjelinek 
66910209230bSgjelinek 	return (Z_NO_ENTRY);
66920209230bSgjelinek }
66930209230bSgjelinek 
66940209230bSgjelinek static int
66950209230bSgjelinek getmcapent_core(zone_dochandle_t handle, struct zone_mcaptab *tabptr)
66960209230bSgjelinek {
66970209230bSgjelinek 	xmlNodePtr cur;
66980209230bSgjelinek 	int err;
66990209230bSgjelinek 
67000209230bSgjelinek 	if (handle == NULL)
67010209230bSgjelinek 		return (Z_INVAL);
67020209230bSgjelinek 
67030209230bSgjelinek 	if ((cur = handle->zone_dh_cur) == NULL)
67040209230bSgjelinek 		return (Z_NO_ENTRY);
67050209230bSgjelinek 
67060209230bSgjelinek 	for (; cur != NULL; cur = cur->next)
67070209230bSgjelinek 		if (xmlStrcmp(cur->name, DTD_ELEM_MCAP) == 0)
67080209230bSgjelinek 			break;
67090209230bSgjelinek 	if (cur == NULL) {
67100209230bSgjelinek 		handle->zone_dh_cur = handle->zone_dh_top;
67110209230bSgjelinek 		return (Z_NO_ENTRY);
67120209230bSgjelinek 	}
67130209230bSgjelinek 
67140209230bSgjelinek 	if ((err = fetchprop(cur, DTD_ATTR_PHYSCAP, tabptr->zone_physmem_cap,
67150209230bSgjelinek 	    sizeof (tabptr->zone_physmem_cap))) != Z_OK) {
67160209230bSgjelinek 		handle->zone_dh_cur = handle->zone_dh_top;
67170209230bSgjelinek 		return (err);
67180209230bSgjelinek 	}
67190209230bSgjelinek 
67200209230bSgjelinek 	handle->zone_dh_cur = cur->next;
67210209230bSgjelinek 	return (Z_OK);
67220209230bSgjelinek }
67230209230bSgjelinek 
67240209230bSgjelinek int
67250209230bSgjelinek zonecfg_getmcapent(zone_dochandle_t handle, struct zone_mcaptab *tabptr)
67260209230bSgjelinek {
67270209230bSgjelinek 	int err;
67280209230bSgjelinek 
67290209230bSgjelinek 	if ((err = zonecfg_setent(handle)) != Z_OK)
67300209230bSgjelinek 		return (err);
67310209230bSgjelinek 
67320209230bSgjelinek 	err = getmcapent_core(handle, tabptr);
67330209230bSgjelinek 
67340209230bSgjelinek 	(void) zonecfg_endent(handle);
67350209230bSgjelinek 
67360209230bSgjelinek 	return (err);
67370209230bSgjelinek }
67380209230bSgjelinek 
67396cfd72c6Sgjelinek /*
67406cfd72c6Sgjelinek  * Get the full tree of pkg/patch metadata in a set of nested AVL trees.
67416cfd72c6Sgjelinek  * pkgs_avl is an AVL tree of pkgs.  Each pkg element contains a
67426cfd72c6Sgjelinek  * zpe_patches_avl member which holds an AVL tree of patches for that pkg.
67436cfd72c6Sgjelinek  * The patch elements have the same zpe_patches_avl member, each of which can
67446cfd72c6Sgjelinek  * hold an AVL tree of patches that are obsoleted by the patch.
67456cfd72c6Sgjelinek  *
67466cfd72c6Sgjelinek  * The zone xml data contains DTD_ELEM_PACKAGE elements, followed by
67476cfd72c6Sgjelinek  * DTD_ELEM_PATCH elements.  The DTD_ELEM_PATCH patch element applies to the
67486cfd72c6Sgjelinek  * DTD_ELEM_PACKAGE that precedes it.  The DTD_ELEM_PATCH element may have
67496cfd72c6Sgjelinek  * child DTD_ELEM_OBSOLETES nodes associated with it.  The DTD_ELEM_PACKAGE
67506cfd72c6Sgjelinek  * really should have had the DTD_ELEM_PATCH elements as children but it
67516cfd72c6Sgjelinek  * was not defined that way initially so we are stuck with the DTD definition
67526cfd72c6Sgjelinek  * now.  However, we can safely assume the ordering for compatibility.
67536cfd72c6Sgjelinek  */
6754ee519a1fSgjelinek int
67556cfd72c6Sgjelinek zonecfg_getpkgdata(zone_dochandle_t handle, uu_avl_pool_t *pkg_pool,
67566cfd72c6Sgjelinek     uu_avl_t *pkgs_avl)
6757ee519a1fSgjelinek {
6758ee519a1fSgjelinek 	xmlNodePtr cur;
67596cfd72c6Sgjelinek 	int res;
67606cfd72c6Sgjelinek 	zone_pkg_entry_t *pkg;
67616cfd72c6Sgjelinek 	char name[MAXNAMELEN];
67626cfd72c6Sgjelinek 	char version[ZONE_PKG_VERSMAX];
6763ee519a1fSgjelinek 
6764ee519a1fSgjelinek 	if (handle == NULL)
6765ee519a1fSgjelinek 		return (Z_INVAL);
6766ee519a1fSgjelinek 
67676cfd72c6Sgjelinek 	if ((res = zonecfg_setent(handle)) != Z_OK)
67686cfd72c6Sgjelinek 		return (res);
6769ee519a1fSgjelinek 
67706cfd72c6Sgjelinek 	if ((cur = handle->zone_dh_cur) == NULL) {
67716cfd72c6Sgjelinek 		res = Z_NO_ENTRY;
67726cfd72c6Sgjelinek 		goto done;
6773ee519a1fSgjelinek 	}
6774ee519a1fSgjelinek 
67756cfd72c6Sgjelinek 	for (; cur != NULL; cur = cur->next) {
67766cfd72c6Sgjelinek 		if (xmlStrcmp(cur->name, DTD_ELEM_PACKAGE) == 0) {
67776cfd72c6Sgjelinek 			uu_avl_index_t where;
67786cfd72c6Sgjelinek 
67796cfd72c6Sgjelinek 			if ((res = fetchprop(cur, DTD_ATTR_NAME, name,
67806cfd72c6Sgjelinek 			    sizeof (name))) != Z_OK)
67816cfd72c6Sgjelinek 				goto done;
67826cfd72c6Sgjelinek 
67836cfd72c6Sgjelinek 			if ((res = fetchprop(cur, DTD_ATTR_VERSION, version,
67846cfd72c6Sgjelinek 			    sizeof (version))) != Z_OK)
67856cfd72c6Sgjelinek 				goto done;
67866cfd72c6Sgjelinek 
67876cfd72c6Sgjelinek 			if ((pkg = (zone_pkg_entry_t *)
67886cfd72c6Sgjelinek 			    malloc(sizeof (zone_pkg_entry_t))) == NULL) {
67896cfd72c6Sgjelinek 				res = Z_NOMEM;
67906cfd72c6Sgjelinek 				goto done;
6791ee519a1fSgjelinek 			}
6792ee519a1fSgjelinek 
67936cfd72c6Sgjelinek 			if ((pkg->zpe_name = strdup(name)) == NULL) {
67946cfd72c6Sgjelinek 				free(pkg);
67956cfd72c6Sgjelinek 				res = Z_NOMEM;
67966cfd72c6Sgjelinek 				goto done;
6797ee519a1fSgjelinek 			}
6798ee519a1fSgjelinek 
67996cfd72c6Sgjelinek 			if ((pkg->zpe_vers = strdup(version)) == NULL) {
68006cfd72c6Sgjelinek 				free(pkg->zpe_name);
68016cfd72c6Sgjelinek 				free(pkg);
68026cfd72c6Sgjelinek 				res = Z_NOMEM;
68036cfd72c6Sgjelinek 				goto done;
6804ee519a1fSgjelinek 			}
6805ee519a1fSgjelinek 
68066cfd72c6Sgjelinek 			pkg->zpe_patches_avl = NULL;
68076cfd72c6Sgjelinek 
68086cfd72c6Sgjelinek 			uu_avl_node_init(pkg, &pkg->zpe_entry, pkg_pool);
68096cfd72c6Sgjelinek 			if (uu_avl_find(pkgs_avl, pkg, NULL, &where) != NULL) {
68106cfd72c6Sgjelinek 				free(pkg->zpe_name);
68116cfd72c6Sgjelinek 				free(pkg->zpe_vers);
68126cfd72c6Sgjelinek 				free(pkg);
68136cfd72c6Sgjelinek 			} else {
68146cfd72c6Sgjelinek 				uu_avl_insert(pkgs_avl, pkg, where);
6815ee519a1fSgjelinek 			}
6816ee519a1fSgjelinek 
68176cfd72c6Sgjelinek 		} else if (xmlStrcmp(cur->name, DTD_ELEM_PATCH) == 0) {
68186cfd72c6Sgjelinek 			zone_pkg_entry_t *patch;
68196cfd72c6Sgjelinek 			uu_avl_index_t where;
68206cfd72c6Sgjelinek 			char *p;
68216cfd72c6Sgjelinek 			char *dashp = NULL;
68226cfd72c6Sgjelinek 			xmlNodePtr child;
68236cfd72c6Sgjelinek 
68246cfd72c6Sgjelinek 			if ((res = fetchprop(cur, DTD_ATTR_ID, name,
68256cfd72c6Sgjelinek 			    sizeof (name))) != Z_OK)
68266cfd72c6Sgjelinek 				goto done;
68276cfd72c6Sgjelinek 
68286cfd72c6Sgjelinek 			if ((patch = (zone_pkg_entry_t *)
68296cfd72c6Sgjelinek 			    malloc(sizeof (zone_pkg_entry_t))) == NULL) {
68306cfd72c6Sgjelinek 				res = Z_NOMEM;
68316cfd72c6Sgjelinek 				goto done;
6832ee519a1fSgjelinek 			}
6833ee519a1fSgjelinek 
68346cfd72c6Sgjelinek 			if ((p = strchr(name, '-')) != NULL) {
68356cfd72c6Sgjelinek 				dashp = p;
68366cfd72c6Sgjelinek 				*p++ = '\0';
68376cfd72c6Sgjelinek 			} else {
68386cfd72c6Sgjelinek 				p = "";
6839ee519a1fSgjelinek 			}
6840ee519a1fSgjelinek 
68416cfd72c6Sgjelinek 			if ((patch->zpe_name = strdup(name)) == NULL) {
68426cfd72c6Sgjelinek 				free(patch);
68436cfd72c6Sgjelinek 				res = Z_NOMEM;
68446cfd72c6Sgjelinek 				goto done;
6845ee519a1fSgjelinek 			}
6846ee519a1fSgjelinek 
68476cfd72c6Sgjelinek 			if ((patch->zpe_vers = strdup(p)) == NULL) {
68486cfd72c6Sgjelinek 				free(patch->zpe_name);
68496cfd72c6Sgjelinek 				free(patch);
68506cfd72c6Sgjelinek 				res = Z_NOMEM;
68516cfd72c6Sgjelinek 				goto done;
6852ee519a1fSgjelinek 			}
6853ee519a1fSgjelinek 
68546cfd72c6Sgjelinek 			if (dashp != NULL)
68556cfd72c6Sgjelinek 				*dashp = '-';
68566cfd72c6Sgjelinek 
68576cfd72c6Sgjelinek 			patch->zpe_patches_avl = NULL;
68586cfd72c6Sgjelinek 
68596cfd72c6Sgjelinek 			if (pkg->zpe_patches_avl == NULL) {
68606cfd72c6Sgjelinek 				pkg->zpe_patches_avl = uu_avl_create(pkg_pool,
68616cfd72c6Sgjelinek 				    NULL, UU_DEFAULT);
68626cfd72c6Sgjelinek 				if (pkg->zpe_patches_avl == NULL) {
68636cfd72c6Sgjelinek 					free(patch->zpe_name);
68646cfd72c6Sgjelinek 					free(patch->zpe_vers);
68656cfd72c6Sgjelinek 					free(patch);
68666cfd72c6Sgjelinek 					res = Z_NOMEM;
68676cfd72c6Sgjelinek 					goto done;
68686cfd72c6Sgjelinek 				}
68696cfd72c6Sgjelinek 			}
68706cfd72c6Sgjelinek 
68716cfd72c6Sgjelinek 			uu_avl_node_init(patch, &patch->zpe_entry, pkg_pool);
68726cfd72c6Sgjelinek 			if (uu_avl_find(pkg->zpe_patches_avl, patch, NULL,
68736cfd72c6Sgjelinek 			    &where) != NULL) {
68746cfd72c6Sgjelinek 				free(patch->zpe_name);
68756cfd72c6Sgjelinek 				free(patch->zpe_vers);
68766cfd72c6Sgjelinek 				free(patch);
68776cfd72c6Sgjelinek 			} else {
68786cfd72c6Sgjelinek 				uu_avl_insert(pkg->zpe_patches_avl, patch,
68796cfd72c6Sgjelinek 				    where);
68806cfd72c6Sgjelinek 			}
68816cfd72c6Sgjelinek 
68826cfd72c6Sgjelinek 			/* Add any patches this patch obsoletes. */
68836cfd72c6Sgjelinek 			for (child = cur->xmlChildrenNode; child != NULL;
68846cfd72c6Sgjelinek 			    child = child->next) {
68856cfd72c6Sgjelinek 				zone_pkg_entry_t *obs;
68866cfd72c6Sgjelinek 
68876cfd72c6Sgjelinek 				if (xmlStrcmp(child->name, DTD_ELEM_OBSOLETES)
68886cfd72c6Sgjelinek 				    != 0)
68896cfd72c6Sgjelinek 					continue;
68906cfd72c6Sgjelinek 
68916cfd72c6Sgjelinek 				if ((res = fetchprop(child, DTD_ATTR_ID,
68926cfd72c6Sgjelinek 				    name, sizeof (name))) != Z_OK)
68936cfd72c6Sgjelinek 					goto done;
68946cfd72c6Sgjelinek 
68956cfd72c6Sgjelinek 				if ((obs = (zone_pkg_entry_t *)malloc(
68966cfd72c6Sgjelinek 				    sizeof (zone_pkg_entry_t))) == NULL) {
68976cfd72c6Sgjelinek 					res = Z_NOMEM;
68986cfd72c6Sgjelinek 					goto done;
68996cfd72c6Sgjelinek 				}
69006cfd72c6Sgjelinek 
69016cfd72c6Sgjelinek 				if ((obs->zpe_name = strdup(name)) == NULL) {
69026cfd72c6Sgjelinek 					free(obs);
69036cfd72c6Sgjelinek 					res = Z_NOMEM;
69046cfd72c6Sgjelinek 					goto done;
69056cfd72c6Sgjelinek 				}
69066cfd72c6Sgjelinek 				/*
69076cfd72c6Sgjelinek 				 * The version doesn't matter for obsoleted
69086cfd72c6Sgjelinek 				 * patches.
69096cfd72c6Sgjelinek 				 */
69106cfd72c6Sgjelinek 				obs->zpe_vers = NULL;
69116cfd72c6Sgjelinek 				obs->zpe_patches_avl = NULL;
69126cfd72c6Sgjelinek 
69136cfd72c6Sgjelinek 				/*
69146cfd72c6Sgjelinek 				 * If this is the first obsolete patch, add an
69156cfd72c6Sgjelinek 				 * AVL tree to the parent patch element.
69166cfd72c6Sgjelinek 				 */
69176cfd72c6Sgjelinek 				if (patch->zpe_patches_avl == NULL) {
69186cfd72c6Sgjelinek 					patch->zpe_patches_avl =
69196cfd72c6Sgjelinek 					    uu_avl_create(pkg_pool, NULL,
69206cfd72c6Sgjelinek 					    UU_DEFAULT);
69216cfd72c6Sgjelinek 					if (patch->zpe_patches_avl == NULL) {
69226cfd72c6Sgjelinek 						free(obs->zpe_name);
69236cfd72c6Sgjelinek 						free(obs);
69246cfd72c6Sgjelinek 						res = Z_NOMEM;
69256cfd72c6Sgjelinek 						goto done;
69266cfd72c6Sgjelinek 					}
69276cfd72c6Sgjelinek 				}
69286cfd72c6Sgjelinek 
69296cfd72c6Sgjelinek 				/* Insert obsolete patch into the AVL tree. */
69306cfd72c6Sgjelinek 				uu_avl_node_init(obs, &obs->zpe_entry,
69316cfd72c6Sgjelinek 				    pkg_pool);
69326cfd72c6Sgjelinek 				if (uu_avl_find(patch->zpe_patches_avl, obs,
69336cfd72c6Sgjelinek 				    NULL, &where) != NULL) {
69346cfd72c6Sgjelinek 					free(obs->zpe_name);
69356cfd72c6Sgjelinek 					free(obs);
69366cfd72c6Sgjelinek 				} else {
69376cfd72c6Sgjelinek 					uu_avl_insert(patch->zpe_patches_avl,
69386cfd72c6Sgjelinek 					    obs, where);
69396cfd72c6Sgjelinek 				}
69406cfd72c6Sgjelinek 			}
69416cfd72c6Sgjelinek 		}
69426cfd72c6Sgjelinek 	}
69436cfd72c6Sgjelinek 
69446cfd72c6Sgjelinek done:
69456cfd72c6Sgjelinek 	(void) zonecfg_endent(handle);
69466cfd72c6Sgjelinek 	return (res);
6947ee519a1fSgjelinek }
6948ee519a1fSgjelinek 
6949ee519a1fSgjelinek int
6950ee519a1fSgjelinek zonecfg_setdevperment(zone_dochandle_t handle)
6951ee519a1fSgjelinek {
6952ee519a1fSgjelinek 	return (zonecfg_setent(handle));
6953ee519a1fSgjelinek }
6954ee519a1fSgjelinek 
6955ee519a1fSgjelinek int
6956ee519a1fSgjelinek zonecfg_getdevperment(zone_dochandle_t handle, struct zone_devpermtab *tabptr)
6957ee519a1fSgjelinek {
6958ee519a1fSgjelinek 	xmlNodePtr cur;
6959ee519a1fSgjelinek 	int err;
6960ee519a1fSgjelinek 	char buf[128];
6961ee519a1fSgjelinek 
6962ee519a1fSgjelinek 	tabptr->zone_devperm_acl = NULL;
6963ee519a1fSgjelinek 
6964ee519a1fSgjelinek 	if (handle == NULL)
6965ee519a1fSgjelinek 		return (Z_INVAL);
6966ee519a1fSgjelinek 
6967ee519a1fSgjelinek 	if ((cur = handle->zone_dh_cur) == NULL)
6968ee519a1fSgjelinek 		return (Z_NO_ENTRY);
6969ee519a1fSgjelinek 
6970ee519a1fSgjelinek 	for (; cur != NULL; cur = cur->next)
6971ee519a1fSgjelinek 		if (!xmlStrcmp(cur->name, DTD_ELEM_DEV_PERM))
6972ee519a1fSgjelinek 			break;
6973ee519a1fSgjelinek 	if (cur == NULL) {
6974ee519a1fSgjelinek 		handle->zone_dh_cur = handle->zone_dh_top;
6975ee519a1fSgjelinek 		return (Z_NO_ENTRY);
6976ee519a1fSgjelinek 	}
6977ee519a1fSgjelinek 
6978ee519a1fSgjelinek 	if ((err = fetchprop(cur, DTD_ATTR_NAME, tabptr->zone_devperm_name,
6979ee519a1fSgjelinek 	    sizeof (tabptr->zone_devperm_name))) != Z_OK) {
6980ee519a1fSgjelinek 		handle->zone_dh_cur = handle->zone_dh_top;
6981ee519a1fSgjelinek 		return (err);
6982ee519a1fSgjelinek 	}
6983ee519a1fSgjelinek 
6984ee519a1fSgjelinek 	if ((err = fetchprop(cur, DTD_ATTR_UID, buf, sizeof (buf))) != Z_OK) {
6985ee519a1fSgjelinek 		handle->zone_dh_cur = handle->zone_dh_top;
6986ee519a1fSgjelinek 		return (err);
6987ee519a1fSgjelinek 	}
6988ee519a1fSgjelinek 	tabptr->zone_devperm_uid = (uid_t)atol(buf);
6989ee519a1fSgjelinek 
6990ee519a1fSgjelinek 	if ((err = fetchprop(cur, DTD_ATTR_GID, buf, sizeof (buf))) != Z_OK) {
6991ee519a1fSgjelinek 		handle->zone_dh_cur = handle->zone_dh_top;
6992ee519a1fSgjelinek 		return (err);
6993ee519a1fSgjelinek 	}
6994ee519a1fSgjelinek 	tabptr->zone_devperm_gid = (gid_t)atol(buf);
6995ee519a1fSgjelinek 
6996ee519a1fSgjelinek 	if ((err = fetchprop(cur, DTD_ATTR_MODE, buf, sizeof (buf))) != Z_OK) {
6997ee519a1fSgjelinek 		handle->zone_dh_cur = handle->zone_dh_top;
6998ee519a1fSgjelinek 		return (err);
6999ee519a1fSgjelinek 	}
7000ee519a1fSgjelinek 	tabptr->zone_devperm_mode = (mode_t)strtol(buf, (char **)NULL, 8);
7001ee519a1fSgjelinek 
7002ee519a1fSgjelinek 	if ((err = fetch_alloc_prop(cur, DTD_ATTR_ACL,
7003ee519a1fSgjelinek 	    &(tabptr->zone_devperm_acl))) != Z_OK) {
7004ee519a1fSgjelinek 		handle->zone_dh_cur = handle->zone_dh_top;
7005ee519a1fSgjelinek 		return (err);
7006ee519a1fSgjelinek 	}
7007ee519a1fSgjelinek 
7008ee519a1fSgjelinek 	handle->zone_dh_cur = cur->next;
7009ee519a1fSgjelinek 	return (Z_OK);
7010ee519a1fSgjelinek }
7011ee519a1fSgjelinek 
7012ee519a1fSgjelinek int
7013ee519a1fSgjelinek zonecfg_enddevperment(zone_dochandle_t handle)
7014ee519a1fSgjelinek {
7015ee519a1fSgjelinek 	return (zonecfg_endent(handle));
7016ee519a1fSgjelinek }
7017ee519a1fSgjelinek 
7018ff17c8bfSgjelinek /* PRINTFLIKE1 */
7019ff17c8bfSgjelinek static void
7020ff17c8bfSgjelinek zerror(const char *zone_name, const char *fmt, ...)
702139935be5Sgjelinek {
7022ff17c8bfSgjelinek 	va_list alist;
702339935be5Sgjelinek 
7024ff17c8bfSgjelinek 	va_start(alist, fmt);
7025ff17c8bfSgjelinek 	(void) fprintf(stderr, "zone '%s': ", zone_name);
7026ff17c8bfSgjelinek 	(void) vfprintf(stderr, fmt, alist);
7027ff17c8bfSgjelinek 	(void) fprintf(stderr, "\n");
7028ff17c8bfSgjelinek 	va_end(alist);
7029ee519a1fSgjelinek }
7030ee519a1fSgjelinek 
7031ee519a1fSgjelinek static void
7032ff17c8bfSgjelinek zperror(const char *str)
7033ee519a1fSgjelinek {
7034ff17c8bfSgjelinek 	(void) fprintf(stderr, "%s: %s\n", str, strerror(errno));
7035ee519a1fSgjelinek }
7036ee519a1fSgjelinek 
7037ee519a1fSgjelinek /*
7038ff17c8bfSgjelinek  * The following three routines implement a simple locking mechanism to
7039ff17c8bfSgjelinek  * ensure that only one instance of zoneadm at a time is able to manipulate
7040ff17c8bfSgjelinek  * a given zone.  The lock is built on top of an fcntl(2) lock of
7041ff17c8bfSgjelinek  * [<altroot>]/var/run/zones/<zonename>.zoneadm.lock.  If a zoneadm instance
7042ff17c8bfSgjelinek  * can grab that lock, it is allowed to manipulate the zone.
7043ff17c8bfSgjelinek  *
7044ff17c8bfSgjelinek  * Since zoneadm may call external applications which in turn invoke
7045ff17c8bfSgjelinek  * zoneadm again, we introduce the notion of "lock inheritance".  Any
7046ff17c8bfSgjelinek  * instance of zoneadm that has another instance in its ancestry is assumed
7047ff17c8bfSgjelinek  * to be acting on behalf of the original zoneadm, and is thus allowed to
7048ff17c8bfSgjelinek  * manipulate its zone.
7049ff17c8bfSgjelinek  *
7050ff17c8bfSgjelinek  * This inheritance is implemented via the _ZONEADM_LOCK_HELD environment
7051ff17c8bfSgjelinek  * variable.  When zoneadm is granted a lock on its zone, this environment
7052ff17c8bfSgjelinek  * variable is set to 1.  When it releases the lock, the variable is set to
7053ff17c8bfSgjelinek  * 0.  Since a child process inherits its parent's environment, checking
7054ff17c8bfSgjelinek  * the state of this variable indicates whether or not any ancestor owns
7055ff17c8bfSgjelinek  * the lock.
705639935be5Sgjelinek  */
7057ff17c8bfSgjelinek void
7058ff17c8bfSgjelinek zonecfg_init_lock_file(const char *zone_name, char **lock_env)
705939935be5Sgjelinek {
7060ff17c8bfSgjelinek 	*lock_env = getenv(LOCK_ENV_VAR);
7061ff17c8bfSgjelinek 	if (*lock_env == NULL) {
7062ff17c8bfSgjelinek 		if (putenv(zoneadm_lock_not_held) != 0) {
7063ff17c8bfSgjelinek 			zerror(zone_name, gettext("could not set env: %s"),
7064ff17c8bfSgjelinek 			    strerror(errno));
7065ff17c8bfSgjelinek 			exit(1);
7066ff17c8bfSgjelinek 		}
706739935be5Sgjelinek 	} else {
7068ff17c8bfSgjelinek 		if (atoi(*lock_env) == 1)
7069ff17c8bfSgjelinek 			zone_lock_cnt = 1;
707039935be5Sgjelinek 	}
707139935be5Sgjelinek }
707239935be5Sgjelinek 
7073ff17c8bfSgjelinek void
7074ff17c8bfSgjelinek zonecfg_release_lock_file(const char *zone_name, int lockfd)
7075ff17c8bfSgjelinek {
7076ff17c8bfSgjelinek 	/*
7077ff17c8bfSgjelinek 	 * If we are cleaning up from a failed attempt to lock the zone for
7078ff17c8bfSgjelinek 	 * the first time, we might have a zone_lock_cnt of 0.  In that
7079ff17c8bfSgjelinek 	 * error case, we don't want to do anything but close the lock
7080ff17c8bfSgjelinek 	 * file.
7081ff17c8bfSgjelinek 	 */
7082ff17c8bfSgjelinek 	assert(zone_lock_cnt >= 0);
7083ff17c8bfSgjelinek 	if (zone_lock_cnt > 0) {
7084ff17c8bfSgjelinek 		assert(getenv(LOCK_ENV_VAR) != NULL);
7085ff17c8bfSgjelinek 		assert(atoi(getenv(LOCK_ENV_VAR)) == 1);
7086ff17c8bfSgjelinek 		if (--zone_lock_cnt > 0) {
7087ff17c8bfSgjelinek 			assert(lockfd == -1);
7088ff17c8bfSgjelinek 			return;
708939935be5Sgjelinek 		}
7090ff17c8bfSgjelinek 		if (putenv(zoneadm_lock_not_held) != 0) {
7091ff17c8bfSgjelinek 			zerror(zone_name, gettext("could not set env: %s"),
7092ff17c8bfSgjelinek 			    strerror(errno));
7093ff17c8bfSgjelinek 			exit(1);
7094ff17c8bfSgjelinek 		}
7095ff17c8bfSgjelinek 	}
7096ff17c8bfSgjelinek 	assert(lockfd >= 0);
7097ff17c8bfSgjelinek 	(void) close(lockfd);
709839935be5Sgjelinek }
709939935be5Sgjelinek 
7100ff17c8bfSgjelinek int
7101ff17c8bfSgjelinek zonecfg_grab_lock_file(const char *zone_name, int *lockfd)
7102ff17c8bfSgjelinek {
7103ff17c8bfSgjelinek 	char pathbuf[PATH_MAX];
7104ff17c8bfSgjelinek 	struct flock flock;
7105ff17c8bfSgjelinek 
7106ff17c8bfSgjelinek 	/*
7107ff17c8bfSgjelinek 	 * If we already have the lock, we can skip this expensive song
7108ff17c8bfSgjelinek 	 * and dance.
7109ff17c8bfSgjelinek 	 */
7110ff17c8bfSgjelinek 	assert(zone_lock_cnt >= 0);
7111ff17c8bfSgjelinek 	assert(getenv(LOCK_ENV_VAR) != NULL);
7112ff17c8bfSgjelinek 	if (zone_lock_cnt > 0) {
7113ff17c8bfSgjelinek 		assert(atoi(getenv(LOCK_ENV_VAR)) == 1);
7114ff17c8bfSgjelinek 		zone_lock_cnt++;
7115ff17c8bfSgjelinek 		*lockfd = -1;
711639935be5Sgjelinek 		return (Z_OK);
711739935be5Sgjelinek 	}
7118ff17c8bfSgjelinek 	assert(getenv(LOCK_ENV_VAR) != NULL);
7119ff17c8bfSgjelinek 	assert(atoi(getenv(LOCK_ENV_VAR)) == 0);
712039935be5Sgjelinek 
7121ff17c8bfSgjelinek 	if (snprintf(pathbuf, sizeof (pathbuf), "%s%s", zonecfg_get_root(),
7122ff17c8bfSgjelinek 	    ZONES_TMPDIR) >= sizeof (pathbuf)) {
7123ff17c8bfSgjelinek 		zerror(zone_name, gettext("alternate root path is too long"));
712439935be5Sgjelinek 		return (-1);
712539935be5Sgjelinek 	}
7126ff17c8bfSgjelinek 	if (mkdir(pathbuf, S_IRWXU) < 0 && errno != EEXIST) {
7127ff17c8bfSgjelinek 		zerror(zone_name, gettext("could not mkdir %s: %s"), pathbuf,
7128ff17c8bfSgjelinek 		    strerror(errno));
7129ff17c8bfSgjelinek 		return (-1);
7130ff17c8bfSgjelinek 	}
7131ff17c8bfSgjelinek 	(void) chmod(pathbuf, S_IRWXU);
713239935be5Sgjelinek 
713339935be5Sgjelinek 	/*
7134ff17c8bfSgjelinek 	 * One of these lock files is created for each zone (when needed).
7135ff17c8bfSgjelinek 	 * The lock files are not cleaned up (except on system reboot),
7136ff17c8bfSgjelinek 	 * but since there is only one per zone, there is no resource
7137ff17c8bfSgjelinek 	 * starvation issue.
713839935be5Sgjelinek 	 */
7139ff17c8bfSgjelinek 	if (snprintf(pathbuf, sizeof (pathbuf), "%s%s/%s.zoneadm.lock",
7140ff17c8bfSgjelinek 	    zonecfg_get_root(), ZONES_TMPDIR, zone_name) >= sizeof (pathbuf)) {
7141ff17c8bfSgjelinek 		zerror(zone_name, gettext("alternate root path is too long"));
7142ff17c8bfSgjelinek 		return (-1);
7143ff17c8bfSgjelinek 	}
7144ff17c8bfSgjelinek 	if ((*lockfd = open(pathbuf, O_RDWR|O_CREAT, S_IRUSR|S_IWUSR)) < 0) {
7145ff17c8bfSgjelinek 		zerror(zone_name, gettext("could not open %s: %s"), pathbuf,
7146ff17c8bfSgjelinek 		    strerror(errno));
7147ff17c8bfSgjelinek 		return (-1);
7148ff17c8bfSgjelinek 	}
7149ff17c8bfSgjelinek 	/*
7150ff17c8bfSgjelinek 	 * Lock the file to synchronize with other zoneadmds
7151ff17c8bfSgjelinek 	 */
7152ff17c8bfSgjelinek 	flock.l_type = F_WRLCK;
7153ff17c8bfSgjelinek 	flock.l_whence = SEEK_SET;
7154ff17c8bfSgjelinek 	flock.l_start = (off_t)0;
7155ff17c8bfSgjelinek 	flock.l_len = (off_t)0;
7156ff17c8bfSgjelinek 	if ((fcntl(*lockfd, F_SETLKW, &flock) < 0) ||
7157ff17c8bfSgjelinek 	    (putenv(zoneadm_lock_held) != 0)) {
7158ff17c8bfSgjelinek 		zerror(zone_name, gettext("unable to lock %s: %s"), pathbuf,
7159ff17c8bfSgjelinek 		    strerror(errno));
7160ff17c8bfSgjelinek 		zonecfg_release_lock_file(zone_name, *lockfd);
7161ff17c8bfSgjelinek 		return (-1);
7162ff17c8bfSgjelinek 	}
7163ff17c8bfSgjelinek 	zone_lock_cnt = 1;
7164ff17c8bfSgjelinek 	return (Z_OK);
7165ff17c8bfSgjelinek }
7166ff17c8bfSgjelinek 
7167edfa49ffS boolean_t
7168edfa49ffS zonecfg_lock_file_held(int *lockfd)
7169edfa49ffS {
7170edfa49ffS 	if (*lockfd >= 0 || zone_lock_cnt > 0)
7171edfa49ffS 		return (B_TRUE);
7172edfa49ffS 	return (B_FALSE);
7173edfa49ffS }
7174edfa49ffS 
7175ff17c8bfSgjelinek static boolean_t
7176ff17c8bfSgjelinek get_doorname(const char *zone_name, char *buffer)
717739935be5Sgjelinek {
7178ff17c8bfSgjelinek 	return (snprintf(buffer, PATH_MAX, "%s" ZONE_DOOR_PATH,
7179ff17c8bfSgjelinek 	    zonecfg_get_root(), zone_name) < PATH_MAX);
7180e66285fdSgjelinek }
7181ee519a1fSgjelinek 
7182ee519a1fSgjelinek /*
7183ff17c8bfSgjelinek  * system daemons are not audited.  For the global zone, this occurs
7184ff17c8bfSgjelinek  * "naturally" since init is started with the default audit
7185ff17c8bfSgjelinek  * characteristics.  Since zoneadmd is a system daemon and it starts
7186ff17c8bfSgjelinek  * init for a zone, it is necessary to clear out the audit
7187ff17c8bfSgjelinek  * characteristics inherited from whomever started zoneadmd.  This is
7188ff17c8bfSgjelinek  * indicated by the audit id, which is set from the ruid parameter of
7189ff17c8bfSgjelinek  * adt_set_user(), below.
7190ee519a1fSgjelinek  */
7191ff17c8bfSgjelinek 
7192ff17c8bfSgjelinek static void
7193ff17c8bfSgjelinek prepare_audit_context(const char *zone_name)
7194ff17c8bfSgjelinek {
7195ff17c8bfSgjelinek 	adt_session_data_t	*ah;
7196ff17c8bfSgjelinek 	char			*failure = gettext("audit failure: %s");
7197ff17c8bfSgjelinek 
7198ff17c8bfSgjelinek 	if (adt_start_session(&ah, NULL, 0)) {
7199ff17c8bfSgjelinek 		zerror(zone_name, failure, strerror(errno));
7200ff17c8bfSgjelinek 		return;
7201ff17c8bfSgjelinek 	}
7202ff17c8bfSgjelinek 	if (adt_set_user(ah, ADT_NO_AUDIT, ADT_NO_AUDIT,
7203ff17c8bfSgjelinek 	    ADT_NO_AUDIT, ADT_NO_AUDIT, NULL, ADT_NEW)) {
7204ff17c8bfSgjelinek 		zerror(zone_name, failure, strerror(errno));
7205ff17c8bfSgjelinek 		(void) adt_end_session(ah);
7206ff17c8bfSgjelinek 		return;
7207ff17c8bfSgjelinek 	}
7208ff17c8bfSgjelinek 	if (adt_set_proc(ah))
7209ff17c8bfSgjelinek 		zerror(zone_name, failure, strerror(errno));
7210ff17c8bfSgjelinek 
7211ff17c8bfSgjelinek 	(void) adt_end_session(ah);
7212ff17c8bfSgjelinek }
7213ff17c8bfSgjelinek 
7214ff17c8bfSgjelinek static int
7215ff17c8bfSgjelinek start_zoneadmd(const char *zone_name, boolean_t lock)
7216ff17c8bfSgjelinek {
7217ff17c8bfSgjelinek 	char doorpath[PATH_MAX];
7218ff17c8bfSgjelinek 	pid_t child_pid;
7219ff17c8bfSgjelinek 	int error = -1;
7220ff17c8bfSgjelinek 	int doorfd, lockfd;
7221ff17c8bfSgjelinek 	struct door_info info;
7222ff17c8bfSgjelinek 
7223ff17c8bfSgjelinek 	if (!get_doorname(zone_name, doorpath))
7224ff17c8bfSgjelinek 		return (-1);
7225ff17c8bfSgjelinek 
7226ff17c8bfSgjelinek 	if (lock)
7227ff17c8bfSgjelinek 		if (zonecfg_grab_lock_file(zone_name, &lockfd) != Z_OK)
7228ff17c8bfSgjelinek 			return (-1);
7229ff17c8bfSgjelinek 
7230ff17c8bfSgjelinek 	/*
7231ff17c8bfSgjelinek 	 * Now that we have the lock, re-confirm that the daemon is
7232ff17c8bfSgjelinek 	 * *not* up and working fine.  If it is still down, we have a green
7233ff17c8bfSgjelinek 	 * light to start it.
7234ff17c8bfSgjelinek 	 */
7235ff17c8bfSgjelinek 	if ((doorfd = open(doorpath, O_RDONLY)) < 0) {
7236ff17c8bfSgjelinek 		if (errno != ENOENT) {
7237ff17c8bfSgjelinek 			zperror(doorpath);
7238ff17c8bfSgjelinek 			goto out;
7239ff17c8bfSgjelinek 		}
7240ff17c8bfSgjelinek 	} else {
7241ff17c8bfSgjelinek 		if (door_info(doorfd, &info) == 0 &&
7242ff17c8bfSgjelinek 		    ((info.di_attributes & DOOR_REVOKED) == 0)) {
7243ff17c8bfSgjelinek 			error = Z_OK;
7244ff17c8bfSgjelinek 			(void) close(doorfd);
7245ff17c8bfSgjelinek 			goto out;
7246ff17c8bfSgjelinek 		}
7247ff17c8bfSgjelinek 		(void) close(doorfd);
7248ff17c8bfSgjelinek 	}
7249ff17c8bfSgjelinek 
7250ff17c8bfSgjelinek 	if ((child_pid = fork()) == -1) {
7251ff17c8bfSgjelinek 		zperror(gettext("could not fork"));
7252ff17c8bfSgjelinek 		goto out;
7253ff17c8bfSgjelinek 	}
7254ff17c8bfSgjelinek 
7255ff17c8bfSgjelinek 	if (child_pid == 0) {
7256ff17c8bfSgjelinek 		const char *argv[6], **ap;
7257ff17c8bfSgjelinek 
7258ff17c8bfSgjelinek 		/* child process */
7259ff17c8bfSgjelinek 		prepare_audit_context(zone_name);
7260ff17c8bfSgjelinek 
7261ff17c8bfSgjelinek 		ap = argv;
7262ff17c8bfSgjelinek 		*ap++ = "zoneadmd";
7263ff17c8bfSgjelinek 		*ap++ = "-z";
7264ff17c8bfSgjelinek 		*ap++ = zone_name;
7265ff17c8bfSgjelinek 		if (zonecfg_in_alt_root()) {
7266ff17c8bfSgjelinek 			*ap++ = "-R";
7267ff17c8bfSgjelinek 			*ap++ = zonecfg_get_root();
7268ff17c8bfSgjelinek 		}
7269ff17c8bfSgjelinek 		*ap = NULL;
7270ff17c8bfSgjelinek 
7271ff17c8bfSgjelinek 		(void) execv("/usr/lib/zones/zoneadmd", (char * const *)argv);
7272ff17c8bfSgjelinek 		/*
7273ff17c8bfSgjelinek 		 * TRANSLATION_NOTE
7274ff17c8bfSgjelinek 		 * zoneadmd is a literal that should not be translated.
7275ff17c8bfSgjelinek 		 */
7276ff17c8bfSgjelinek 		zperror(gettext("could not exec zoneadmd"));
7277ff17c8bfSgjelinek 		_exit(1);
7278ff17c8bfSgjelinek 	} else {
7279ff17c8bfSgjelinek 		/* parent process */
7280ff17c8bfSgjelinek 		pid_t retval;
7281ff17c8bfSgjelinek 		int pstatus = 0;
7282ee519a1fSgjelinek 
7283ee519a1fSgjelinek 		do {
7284ff17c8bfSgjelinek 			retval = waitpid(child_pid, &pstatus, 0);
7285ff17c8bfSgjelinek 		} while (retval != child_pid);
7286ff17c8bfSgjelinek 		if (WIFSIGNALED(pstatus) || (WIFEXITED(pstatus) &&
7287ff17c8bfSgjelinek 		    WEXITSTATUS(pstatus) != 0)) {
7288ff17c8bfSgjelinek 			zerror(zone_name, gettext("could not start %s"),
7289ff17c8bfSgjelinek 			    "zoneadmd");
7290ff17c8bfSgjelinek 			goto out;
72916cfd72c6Sgjelinek 		}
72926cfd72c6Sgjelinek 	}
7293ff17c8bfSgjelinek 	error = Z_OK;
7294ff17c8bfSgjelinek out:
7295ff17c8bfSgjelinek 	if (lock)
7296ff17c8bfSgjelinek 		zonecfg_release_lock_file(zone_name, lockfd);
7297ff17c8bfSgjelinek 	return (error);
7298ee519a1fSgjelinek }
7299ee519a1fSgjelinek 
7300ee519a1fSgjelinek int
7301ff17c8bfSgjelinek zonecfg_ping_zoneadmd(const char *zone_name)
7302ee519a1fSgjelinek {
7303ff17c8bfSgjelinek 	char doorpath[PATH_MAX];
7304ff17c8bfSgjelinek 	int doorfd;
7305ff17c8bfSgjelinek 	struct door_info info;
7306ee519a1fSgjelinek 
7307ff17c8bfSgjelinek 	if (!get_doorname(zone_name, doorpath))
7308ff17c8bfSgjelinek 		return (-1);
7309ee519a1fSgjelinek 
7310ff17c8bfSgjelinek 	if ((doorfd = open(doorpath, O_RDONLY)) < 0) {
7311ff17c8bfSgjelinek 		return (-1);
7312ff17c8bfSgjelinek 	}
7313ff17c8bfSgjelinek 	if (door_info(doorfd, &info) == 0 &&
7314ff17c8bfSgjelinek 	    ((info.di_attributes & DOOR_REVOKED) == 0)) {
7315ff17c8bfSgjelinek 		(void) close(doorfd);
7316ff17c8bfSgjelinek 		return (Z_OK);
7317ff17c8bfSgjelinek 	}
7318ff17c8bfSgjelinek 	(void) close(doorfd);
7319ff17c8bfSgjelinek 	return (-1);
7320ff17c8bfSgjelinek }
7321ee519a1fSgjelinek 
7322ff17c8bfSgjelinek int
7323ff17c8bfSgjelinek zonecfg_call_zoneadmd(const char *zone_name, zone_cmd_arg_t *arg, char *locale,
7324ff17c8bfSgjelinek     boolean_t lock)
7325ff17c8bfSgjelinek {
7326ff17c8bfSgjelinek 	char doorpath[PATH_MAX];
7327ff17c8bfSgjelinek 	int doorfd, result;
7328ff17c8bfSgjelinek 	door_arg_t darg;
7329ff17c8bfSgjelinek 
7330ff17c8bfSgjelinek 	zoneid_t zoneid;
7331ff17c8bfSgjelinek 	uint64_t uniqid = 0;
7332ff17c8bfSgjelinek 
7333ff17c8bfSgjelinek 	zone_cmd_rval_t *rvalp;
7334ff17c8bfSgjelinek 	size_t rlen;
7335ff17c8bfSgjelinek 	char *cp, *errbuf;
7336ff17c8bfSgjelinek 
7337ff17c8bfSgjelinek 	rlen = getpagesize();
7338ff17c8bfSgjelinek 	if ((rvalp = malloc(rlen)) == NULL) {
7339ff17c8bfSgjelinek 		zerror(zone_name, gettext("failed to allocate %lu bytes: %s"),
7340ff17c8bfSgjelinek 		    rlen, strerror(errno));
7341ff17c8bfSgjelinek 		return (-1);
7342ff17c8bfSgjelinek 	}
7343ff17c8bfSgjelinek 
7344ff17c8bfSgjelinek 	if ((zoneid = getzoneidbyname(zone_name)) != ZONE_ID_UNDEFINED) {
7345ff17c8bfSgjelinek 		(void) zone_getattr(zoneid, ZONE_ATTR_UNIQID, &uniqid,
7346ff17c8bfSgjelinek 		    sizeof (uniqid));
7347ff17c8bfSgjelinek 	}
7348ff17c8bfSgjelinek 	arg->uniqid = uniqid;
7349ff17c8bfSgjelinek 	(void) strlcpy(arg->locale, locale, sizeof (arg->locale));
7350ff17c8bfSgjelinek 	if (!get_doorname(zone_name, doorpath)) {
7351ff17c8bfSgjelinek 		zerror(zone_name, gettext("alternate root path is too long"));
7352ff17c8bfSgjelinek 		free(rvalp);
7353ff17c8bfSgjelinek 		return (-1);
7354ff17c8bfSgjelinek 	}
7355ff17c8bfSgjelinek 
7356ff17c8bfSgjelinek 	/*
7357ff17c8bfSgjelinek 	 * Loop trying to start zoneadmd; if something goes seriously
7358ff17c8bfSgjelinek 	 * wrong we break out and fail.
7359ff17c8bfSgjelinek 	 */
7360ff17c8bfSgjelinek 	for (;;) {
7361ff17c8bfSgjelinek 		if (start_zoneadmd(zone_name, lock) != Z_OK)
7362ff17c8bfSgjelinek 			break;
7363ff17c8bfSgjelinek 
7364ff17c8bfSgjelinek 		if ((doorfd = open(doorpath, O_RDONLY)) < 0) {
7365ff17c8bfSgjelinek 			zperror(gettext("failed to open zone door"));
7366ff17c8bfSgjelinek 			break;
7367ff17c8bfSgjelinek 		}
7368ff17c8bfSgjelinek 
7369ff17c8bfSgjelinek 		darg.data_ptr = (char *)arg;
7370ff17c8bfSgjelinek 		darg.data_size = sizeof (*arg);
7371ff17c8bfSgjelinek 		darg.desc_ptr = NULL;
7372ff17c8bfSgjelinek 		darg.desc_num = 0;
7373ff17c8bfSgjelinek 		darg.rbuf = (char *)rvalp;
7374ff17c8bfSgjelinek 		darg.rsize = rlen;
7375ff17c8bfSgjelinek 		if (door_call(doorfd, &darg) != 0) {
7376ff17c8bfSgjelinek 			(void) close(doorfd);
7377ff17c8bfSgjelinek 			/*
7378ff17c8bfSgjelinek 			 * We'll get EBADF if the door has been revoked.
7379ff17c8bfSgjelinek 			 */
7380ff17c8bfSgjelinek 			if (errno != EBADF) {
7381ff17c8bfSgjelinek 				zperror(gettext("door_call failed"));
7382ff17c8bfSgjelinek 				break;
7383ff17c8bfSgjelinek 			}
7384ff17c8bfSgjelinek 			continue;	/* take another lap */
7385ff17c8bfSgjelinek 		}
7386ff17c8bfSgjelinek 		(void) close(doorfd);
7387ff17c8bfSgjelinek 
7388ff17c8bfSgjelinek 		if (darg.data_size == 0) {
7389ff17c8bfSgjelinek 			/* Door server is going away; kick it again. */
7390ff17c8bfSgjelinek 			continue;
7391ff17c8bfSgjelinek 		}
7392ff17c8bfSgjelinek 
7393ff17c8bfSgjelinek 		errbuf = rvalp->errbuf;
7394ff17c8bfSgjelinek 		while (*errbuf != '\0') {
7395ff17c8bfSgjelinek 			/*
7396ff17c8bfSgjelinek 			 * Remove any newlines since zerror()
7397ff17c8bfSgjelinek 			 * will append one automatically.
7398ff17c8bfSgjelinek 			 */
7399ff17c8bfSgjelinek 			cp = strchr(errbuf, '\n');
7400ff17c8bfSgjelinek 			if (cp != NULL)
7401ff17c8bfSgjelinek 				*cp = '\0';
7402ff17c8bfSgjelinek 			zerror(zone_name, "%s", errbuf);
7403ff17c8bfSgjelinek 			if (cp == NULL)
7404ff17c8bfSgjelinek 				break;
7405ff17c8bfSgjelinek 			errbuf = cp + 1;
7406ff17c8bfSgjelinek 		}
7407ff17c8bfSgjelinek 		result = rvalp->rval == 0 ? 0 : -1;
7408ff17c8bfSgjelinek 		free(rvalp);
7409ff17c8bfSgjelinek 		return (result);
7410ff17c8bfSgjelinek 	}
7411ff17c8bfSgjelinek 
7412ff17c8bfSgjelinek 	free(rvalp);
7413ff17c8bfSgjelinek 	return (-1);
7414ee519a1fSgjelinek }
7415