xref: /titanic_50/usr/src/lib/libzonecfg/common/libzonecfg.c (revision 7522f2fc4a704256ba53f51fc6fe14e17f45b9bd)
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 
917e5816e35SEdward Pilatowicz static int
918e5816e35SEdward Pilatowicz i_zonecfg_get_brand(zone_dochandle_t handle, char *brand, size_t brandsize,
919e5816e35SEdward Pilatowicz     boolean_t default_query)
9209acbbeafSnn35248 {
9219acbbeafSnn35248 	int ret, sz;
9229acbbeafSnn35248 
9239acbbeafSnn35248 	ret = getrootattr(handle, DTD_ATTR_BRAND, brand, brandsize);
9249acbbeafSnn35248 
925e5816e35SEdward Pilatowicz 	/*
926e5816e35SEdward Pilatowicz 	 * If the lookup failed, or succeeded in finding a non-null brand
927e5816e35SEdward Pilatowicz 	 * string then return.
928e5816e35SEdward Pilatowicz 	 */
929e5816e35SEdward Pilatowicz 	if (ret != Z_OK || brand[0] != '\0')
930e5816e35SEdward Pilatowicz 		return (ret);
931e5816e35SEdward Pilatowicz 
932e5816e35SEdward Pilatowicz 	if (!default_query) {
933e5816e35SEdward Pilatowicz 		/* If the zone has no brand, it is the default brand. */
934b08d9f75SEdward Pilatowicz 		return (zonecfg_default_brand(brand, brandsize));
9359acbbeafSnn35248 	}
9369acbbeafSnn35248 
937e5816e35SEdward Pilatowicz 	/* if SUNWdefault didn't specify a brand, fallback to "native" */
938e5816e35SEdward Pilatowicz 	sz = strlcpy(brand, NATIVE_BRAND_NAME, brandsize);
939e5816e35SEdward Pilatowicz 	if (sz >= brandsize)
940e5816e35SEdward Pilatowicz 		return (Z_TOO_BIG);
941e5816e35SEdward Pilatowicz 	return (Z_OK);
942e5816e35SEdward Pilatowicz }
943e5816e35SEdward Pilatowicz 
944e5816e35SEdward Pilatowicz int
945e5816e35SEdward Pilatowicz zonecfg_get_brand(zone_dochandle_t handle, char *brand, size_t brandsize)
946e5816e35SEdward Pilatowicz {
947e5816e35SEdward Pilatowicz 	return (i_zonecfg_get_brand(handle, brand, brandsize, B_FALSE));
9489acbbeafSnn35248 }
9499acbbeafSnn35248 
9509acbbeafSnn35248 int
9519acbbeafSnn35248 zonecfg_set_brand(zone_dochandle_t handle, char *brand)
9529acbbeafSnn35248 {
9539acbbeafSnn35248 	return (setrootattr(handle, DTD_ATTR_BRAND, brand));
9549acbbeafSnn35248 }
9559acbbeafSnn35248 
9569acbbeafSnn35248 int
957a1be23daSdp zonecfg_get_autoboot(zone_dochandle_t handle, boolean_t *autoboot)
958a1be23daSdp {
959a1be23daSdp 	char autobootstr[DTD_ENTITY_BOOL_LEN];
960a1be23daSdp 	int ret;
961a1be23daSdp 
962a1be23daSdp 	if ((ret = getrootattr(handle, DTD_ATTR_AUTOBOOT, autobootstr,
963a1be23daSdp 	    sizeof (autobootstr))) != Z_OK)
964a1be23daSdp 		return (ret);
965a1be23daSdp 
966a1be23daSdp 	if (strcmp(autobootstr, DTD_ENTITY_TRUE) == 0)
967a1be23daSdp 		*autoboot = B_TRUE;
968a1be23daSdp 	else if (strcmp(autobootstr, DTD_ENTITY_FALSE) == 0)
969a1be23daSdp 		*autoboot = B_FALSE;
970a1be23daSdp 	else
971a1be23daSdp 		ret = Z_BAD_PROPERTY;
972a1be23daSdp 	return (ret);
973a1be23daSdp }
974a1be23daSdp 
975a1be23daSdp int
976a1be23daSdp zonecfg_set_autoboot(zone_dochandle_t handle, boolean_t autoboot)
977a1be23daSdp {
978a1be23daSdp 	return (setrootattr(handle, DTD_ATTR_AUTOBOOT,
979a1be23daSdp 	    autoboot ? DTD_ENTITY_TRUE : DTD_ENTITY_FALSE));
980a1be23daSdp }
981a1be23daSdp 
982a1be23daSdp int
983a1be23daSdp zonecfg_get_pool(zone_dochandle_t handle, char *pool, size_t poolsize)
984a1be23daSdp {
985a1be23daSdp 	return (getrootattr(handle, DTD_ATTR_POOL, pool, poolsize));
986a1be23daSdp }
987a1be23daSdp 
988a1be23daSdp int
989a1be23daSdp zonecfg_set_pool(zone_dochandle_t handle, char *pool)
990a1be23daSdp {
991a1be23daSdp 	return (setrootattr(handle, DTD_ATTR_POOL, pool));
992a1be23daSdp }
993a1be23daSdp 
994ffbafc53Scomay int
995ffbafc53Scomay zonecfg_get_limitpriv(zone_dochandle_t handle, char **limitpriv)
996ffbafc53Scomay {
997ffbafc53Scomay 	return (get_alloc_rootattr(handle, DTD_ATTR_LIMITPRIV, limitpriv));
998ffbafc53Scomay }
999ffbafc53Scomay 
1000ffbafc53Scomay int
10013f2f09c1Sdp zonecfg_set_limitpriv(zone_dochandle_t handle, char *limitpriv)
1002ffbafc53Scomay {
10033f2f09c1Sdp 	return (setrootattr(handle, DTD_ATTR_LIMITPRIV, limitpriv));
10043f2f09c1Sdp }
10053f2f09c1Sdp 
10063f2f09c1Sdp int
10073f2f09c1Sdp zonecfg_get_bootargs(zone_dochandle_t handle, char *bargs, size_t bargssize)
10083f2f09c1Sdp {
10093f2f09c1Sdp 	return (getrootattr(handle, DTD_ATTR_BOOTARGS, bargs, bargssize));
10103f2f09c1Sdp }
10113f2f09c1Sdp 
10123f2f09c1Sdp int
10133f2f09c1Sdp zonecfg_set_bootargs(zone_dochandle_t handle, char *bargs)
10143f2f09c1Sdp {
10153f2f09c1Sdp 	return (setrootattr(handle, DTD_ATTR_BOOTARGS, bargs));
1016ffbafc53Scomay }
1017ffbafc53Scomay 
10180209230bSgjelinek int
10190209230bSgjelinek zonecfg_get_sched_class(zone_dochandle_t handle, char *sched, size_t schedsize)
10200209230bSgjelinek {
10210209230bSgjelinek 	return (getrootattr(handle, DTD_ATTR_SCHED, sched, schedsize));
10220209230bSgjelinek }
10230209230bSgjelinek 
10240209230bSgjelinek int
10250209230bSgjelinek zonecfg_set_sched(zone_dochandle_t handle, char *sched)
10260209230bSgjelinek {
10270209230bSgjelinek 	return (setrootattr(handle, DTD_ATTR_SCHED, sched));
10280209230bSgjelinek }
10290209230bSgjelinek 
1030a1be23daSdp /*
1031a1be23daSdp  * /etc/zones/index caches a vital piece of information which is also
1032a1be23daSdp  * in the <zonename>.xml file: the path to the zone.  This is for performance,
1033a1be23daSdp  * since we need to walk all zonepath's in order to be able to detect conflicts
1034a1be23daSdp  * (see crosscheck_zonepaths() in the zoneadm command).
1035087719fdSdp  *
1036087719fdSdp  * An additional complexity is that when doing a rename, we'd like the entire
1037087719fdSdp  * index update operation (rename, and potential state changes) to be atomic.
1038087719fdSdp  * In general, the operation of this function should succeed or fail as
1039087719fdSdp  * a unit.
1040a1be23daSdp  */
1041a1be23daSdp int
1042a1be23daSdp zonecfg_refresh_index_file(zone_dochandle_t handle)
1043a1be23daSdp {
1044a1be23daSdp 	char name[ZONENAME_MAX], zonepath[MAXPATHLEN];
1045a1be23daSdp 	struct zoneent ze;
1046a1be23daSdp 	int err;
1047087719fdSdp 	int opcode;
1048087719fdSdp 	char *zn;
1049087719fdSdp 
1050087719fdSdp 	bzero(&ze, sizeof (ze));
1051087719fdSdp 	ze.zone_state = -1;	/* Preserve existing state in index */
1052a1be23daSdp 
1053a1be23daSdp 	if ((err = zonecfg_get_name(handle, name, sizeof (name))) != Z_OK)
1054a1be23daSdp 		return (err);
1055087719fdSdp 	(void) strlcpy(ze.zone_name, name, sizeof (ze.zone_name));
1056087719fdSdp 
1057a1be23daSdp 	if ((err = zonecfg_get_zonepath(handle, zonepath,
1058a1be23daSdp 	    sizeof (zonepath))) != Z_OK)
1059a1be23daSdp 		return (err);
1060555afedfScarlsonj 	(void) strlcpy(ze.zone_path, zonepath + strlen(zonecfg_root),
1061555afedfScarlsonj 	    sizeof (ze.zone_path));
1062087719fdSdp 
1063087719fdSdp 	if (is_renaming(handle)) {
1064087719fdSdp 		opcode = PZE_MODIFY;
1065087719fdSdp 		(void) strlcpy(ze.zone_name, handle->zone_dh_delete_name,
1066087719fdSdp 		    sizeof (ze.zone_name));
1067087719fdSdp 		(void) strlcpy(ze.zone_newname, name, sizeof (ze.zone_newname));
1068087719fdSdp 	} else if (is_new(handle)) {
1069087719fdSdp 		FILE *cookie;
1070087719fdSdp 		/*
1071087719fdSdp 		 * Be tolerant of the zone already existing in the index file,
1072087719fdSdp 		 * since we might be forcibly overwriting an existing
1073087719fdSdp 		 * configuration with a new one (for example 'create -F'
1074087719fdSdp 		 * in zonecfg).
1075087719fdSdp 		 */
1076087719fdSdp 		opcode = PZE_ADD;
1077087719fdSdp 		cookie = setzoneent();
1078087719fdSdp 		while ((zn = getzoneent(cookie)) != NULL) {
1079087719fdSdp 			if (strcmp(zn, name) == 0) {
1080087719fdSdp 				opcode = PZE_MODIFY;
1081087719fdSdp 				free(zn);
1082087719fdSdp 				break;
1083087719fdSdp 			}
1084087719fdSdp 			free(zn);
1085087719fdSdp 		}
1086087719fdSdp 		endzoneent(cookie);
1087087719fdSdp 		ze.zone_state = ZONE_STATE_CONFIGURED;
1088087719fdSdp 	} else {
1089087719fdSdp 		opcode = PZE_MODIFY;
10907c478bd9Sstevel@tonic-gate 	}
10917c478bd9Sstevel@tonic-gate 
1092087719fdSdp 	if ((err = putzoneent(&ze, opcode)) != Z_OK)
1093087719fdSdp 		return (err);
1094087719fdSdp 
1095087719fdSdp 	return (Z_OK);
1096087719fdSdp }
1097087719fdSdp 
1098087719fdSdp /*
1099087719fdSdp  * The goal of this routine is to cause the index file update and the
1100087719fdSdp  * document save to happen as an atomic operation.  We do the document
1101087719fdSdp  * first, saving a backup copy using a hard link; if that succeeds, we go
1102087719fdSdp  * on to the index.  If that fails, we roll the document back into place.
1103087719fdSdp  *
1104087719fdSdp  * Strategy:
1105087719fdSdp  *
1106087719fdSdp  * New zone 'foo' configuration:
1107087719fdSdp  * 	Create tmpfile (zonecfg.xxxxxx)
1108087719fdSdp  * 	Write XML to tmpfile
1109087719fdSdp  * 	Rename tmpfile to xmlfile (zonecfg.xxxxxx -> foo.xml)
1110087719fdSdp  * 	Add entry to index file
1111087719fdSdp  * 	If it fails, delete foo.xml, leaving nothing behind.
1112087719fdSdp  *
1113087719fdSdp  * Save existing zone 'foo':
1114087719fdSdp  * 	Make backup of foo.xml -> .backup
1115087719fdSdp  * 	Create tmpfile (zonecfg.xxxxxx)
1116087719fdSdp  * 	Write XML to tmpfile
1117087719fdSdp  * 	Rename tmpfile to xmlfile (zonecfg.xxxxxx -> foo.xml)
1118087719fdSdp  * 	Modify index file as needed
1119087719fdSdp  * 	If it fails, recover from .backup -> foo.xml
1120087719fdSdp  *
1121087719fdSdp  * Rename 'foo' to 'bar':
1122087719fdSdp  * 	Create tmpfile (zonecfg.xxxxxx)
1123087719fdSdp  * 	Write XML to tmpfile
1124087719fdSdp  * 	Rename tmpfile to xmlfile (zonecfg.xxxxxx -> bar.xml)
1125087719fdSdp  * 	Add entry for 'bar' to index file, Remove entry for 'foo' (refresh)
1126087719fdSdp  * 	If it fails, delete bar.xml; foo.xml is left behind.
1127087719fdSdp  */
11287c478bd9Sstevel@tonic-gate static int
11297c478bd9Sstevel@tonic-gate zonecfg_save_impl(zone_dochandle_t handle, char *filename)
11307c478bd9Sstevel@tonic-gate {
11317c478bd9Sstevel@tonic-gate 	char tmpfile[MAXPATHLEN];
1132087719fdSdp 	char bakdir[MAXPATHLEN], bakbase[MAXPATHLEN], bakfile[MAXPATHLEN];
11339acbbeafSnn35248 	int tmpfd, err, valid;
11347c478bd9Sstevel@tonic-gate 	xmlValidCtxt cvp = { NULL };
1135087719fdSdp 	boolean_t backup;
11367c478bd9Sstevel@tonic-gate 
11377c478bd9Sstevel@tonic-gate 	(void) strlcpy(tmpfile, filename, sizeof (tmpfile));
11387c478bd9Sstevel@tonic-gate 	(void) dirname(tmpfile);
11397c478bd9Sstevel@tonic-gate 	(void) strlcat(tmpfile, _PATH_TMPFILE, sizeof (tmpfile));
11407c478bd9Sstevel@tonic-gate 
11417c478bd9Sstevel@tonic-gate 	tmpfd = mkstemp(tmpfile);
11427c478bd9Sstevel@tonic-gate 	if (tmpfd == -1) {
11437c478bd9Sstevel@tonic-gate 		(void) unlink(tmpfile);
11447c478bd9Sstevel@tonic-gate 		return (Z_TEMP_FILE);
11457c478bd9Sstevel@tonic-gate 	}
11467c478bd9Sstevel@tonic-gate 	(void) close(tmpfd);
11477c478bd9Sstevel@tonic-gate 
11487c478bd9Sstevel@tonic-gate 	cvp.error = zonecfg_error_func;
11497c478bd9Sstevel@tonic-gate 	cvp.warning = zonecfg_error_func;
11507c478bd9Sstevel@tonic-gate 
1151087719fdSdp 	/*
11529acbbeafSnn35248 	 * We do a final validation of the document.  Since the library has
11539acbbeafSnn35248 	 * malfunctioned if it fails to validate, we follow-up with an
11549acbbeafSnn35248 	 * assert() that the doc is valid.
1155087719fdSdp 	 */
11569acbbeafSnn35248 	valid = xmlValidateDocument(&cvp, handle->zone_dh_doc);
11579acbbeafSnn35248 	assert(valid != 0);
11587c478bd9Sstevel@tonic-gate 
11597c478bd9Sstevel@tonic-gate 	if (xmlSaveFormatFile(tmpfile, handle->zone_dh_doc, 1) <= 0)
11607c478bd9Sstevel@tonic-gate 		goto err;
1161087719fdSdp 
11627c478bd9Sstevel@tonic-gate 	(void) chmod(tmpfile, 0644);
11637c478bd9Sstevel@tonic-gate 
1164087719fdSdp 	/*
1165087719fdSdp 	 * In the event we are doing a standard save, hard link a copy of the
1166087719fdSdp 	 * original file in .backup.<pid>.filename so we can restore it if
1167087719fdSdp 	 * something goes wrong.
1168087719fdSdp 	 */
1169087719fdSdp 	if (!is_new(handle) && !is_renaming(handle)) {
1170087719fdSdp 		backup = B_TRUE;
1171087719fdSdp 
1172087719fdSdp 		(void) strlcpy(bakdir, filename, sizeof (bakdir));
1173087719fdSdp 		(void) strlcpy(bakbase, filename, sizeof (bakbase));
1174087719fdSdp 		(void) snprintf(bakfile, sizeof (bakfile), "%s/.backup.%d.%s",
1175087719fdSdp 		    dirname(bakdir), getpid(), basename(bakbase));
1176087719fdSdp 
1177087719fdSdp 		if (link(filename, bakfile) == -1) {
1178087719fdSdp 			err = errno;
11797c478bd9Sstevel@tonic-gate 			(void) unlink(tmpfile);
11807c478bd9Sstevel@tonic-gate 			if (errno == EACCES)
11817c478bd9Sstevel@tonic-gate 				return (Z_ACCES);
11827c478bd9Sstevel@tonic-gate 			return (Z_MISC_FS);
11837c478bd9Sstevel@tonic-gate 		}
1184087719fdSdp 	}
1185a1be23daSdp 
1186087719fdSdp 	/*
1187087719fdSdp 	 * Move the new document over top of the old.
1188087719fdSdp 	 * i.e.:   zonecfg.XXXXXX  ->  myzone.xml
1189087719fdSdp 	 */
1190087719fdSdp 	if (rename(tmpfile, filename) == -1) {
1191087719fdSdp 		err = errno;
1192087719fdSdp 		(void) unlink(tmpfile);
1193087719fdSdp 		if (backup)
1194087719fdSdp 			(void) unlink(bakfile);
1195087719fdSdp 		if (err == EACCES)
1196087719fdSdp 			return (Z_ACCES);
1197087719fdSdp 		return (Z_MISC_FS);
1198087719fdSdp 	}
1199087719fdSdp 
1200087719fdSdp 	/*
1201087719fdSdp 	 * If this is a snapshot, we're done-- don't add an index entry.
1202087719fdSdp 	 */
1203087719fdSdp 	if (is_snapshot(handle))
1204087719fdSdp 		return (Z_OK);
1205087719fdSdp 
1206087719fdSdp 	/* now update the index file to reflect whatever we just did */
1207087719fdSdp 	if ((err = zonecfg_refresh_index_file(handle)) != Z_OK) {
1208087719fdSdp 		if (backup) {
1209087719fdSdp 			/*
1210087719fdSdp 			 * Try to restore from our backup.
1211087719fdSdp 			 */
1212087719fdSdp 			(void) unlink(filename);
1213087719fdSdp 			(void) rename(bakfile, filename);
1214087719fdSdp 		} else {
1215087719fdSdp 			/*
1216087719fdSdp 			 * Either the zone is new, in which case we can delete
1217087719fdSdp 			 * new.xml, or we're doing a rename, so ditto.
1218087719fdSdp 			 */
1219087719fdSdp 			assert(is_new(handle) || is_renaming(handle));
1220087719fdSdp 			(void) unlink(filename);
1221087719fdSdp 		}
1222087719fdSdp 		return (Z_UPDATING_INDEX);
1223087719fdSdp 	}
1224087719fdSdp 
1225087719fdSdp 	if (backup)
1226087719fdSdp 		(void) unlink(bakfile);
1227087719fdSdp 
1228087719fdSdp 	return (Z_OK);
12297c478bd9Sstevel@tonic-gate 
12307c478bd9Sstevel@tonic-gate err:
12317c478bd9Sstevel@tonic-gate 	(void) unlink(tmpfile);
12327c478bd9Sstevel@tonic-gate 	return (Z_SAVING_FILE);
12337c478bd9Sstevel@tonic-gate }
12347c478bd9Sstevel@tonic-gate 
12357c478bd9Sstevel@tonic-gate int
12367c478bd9Sstevel@tonic-gate zonecfg_save(zone_dochandle_t handle)
12377c478bd9Sstevel@tonic-gate {
1238087719fdSdp 	char zname[ZONENAME_MAX], path[MAXPATHLEN];
1239087719fdSdp 	char delpath[MAXPATHLEN];
1240087719fdSdp 	int err = Z_SAVING_FILE;
12417c478bd9Sstevel@tonic-gate 
1242087719fdSdp 	if (zonecfg_check_handle(handle) != Z_OK)
1243087719fdSdp 		return (Z_BAD_HANDLE);
1244087719fdSdp 
1245087719fdSdp 	/*
1246ee519a1fSgjelinek 	 * We don't support saving snapshots or a tree containing a sw
1247ee519a1fSgjelinek 	 * inventory at this time.
1248087719fdSdp 	 */
1249ee519a1fSgjelinek 	if (handle->zone_dh_snapshot || handle->zone_dh_sw_inv)
1250087719fdSdp 		return (Z_INVAL);
1251087719fdSdp 
1252087719fdSdp 	if ((err = zonecfg_get_name(handle, zname, sizeof (zname))) != Z_OK)
12537c478bd9Sstevel@tonic-gate 		return (err);
1254087719fdSdp 
1255108322fbScarlsonj 	if (!config_file_path(zname, path))
1256108322fbScarlsonj 		return (Z_MISC_FS);
1257087719fdSdp 
1258087719fdSdp 	addcomment(handle, "\n    DO NOT EDIT THIS "
1259087719fdSdp 	    "FILE.  Use zonecfg(1M) instead.\n");
1260087719fdSdp 
1261087719fdSdp 	err = zonecfg_save_impl(handle, path);
1262087719fdSdp 
1263087719fdSdp 	stripcomments(handle);
1264087719fdSdp 
1265087719fdSdp 	if (err != Z_OK)
1266087719fdSdp 		return (err);
1267087719fdSdp 
1268087719fdSdp 	handle->zone_dh_newzone = B_FALSE;
1269087719fdSdp 
1270087719fdSdp 	if (is_renaming(handle)) {
1271108322fbScarlsonj 		if (config_file_path(handle->zone_dh_delete_name, delpath))
1272087719fdSdp 			(void) unlink(delpath);
1273087719fdSdp 		handle->zone_dh_delete_name[0] = '\0';
1274087719fdSdp 	}
1275087719fdSdp 
1276087719fdSdp 	return (Z_OK);
12777c478bd9Sstevel@tonic-gate }
12787c478bd9Sstevel@tonic-gate 
1279ee519a1fSgjelinek int
12809acbbeafSnn35248 zonecfg_verify_save(zone_dochandle_t handle, char *filename)
12819acbbeafSnn35248 {
12829acbbeafSnn35248 	int valid;
12839acbbeafSnn35248 
12849acbbeafSnn35248 	xmlValidCtxt cvp = { NULL };
12859acbbeafSnn35248 
12869acbbeafSnn35248 	if (zonecfg_check_handle(handle) != Z_OK)
12879acbbeafSnn35248 		return (Z_BAD_HANDLE);
12889acbbeafSnn35248 
12899acbbeafSnn35248 	cvp.error = zonecfg_error_func;
12909acbbeafSnn35248 	cvp.warning = zonecfg_error_func;
12919acbbeafSnn35248 
12929acbbeafSnn35248 	/*
12939acbbeafSnn35248 	 * We do a final validation of the document.  Since the library has
12949acbbeafSnn35248 	 * malfunctioned if it fails to validate, we follow-up with an
12959acbbeafSnn35248 	 * assert() that the doc is valid.
12969acbbeafSnn35248 	 */
12979acbbeafSnn35248 	valid = xmlValidateDocument(&cvp, handle->zone_dh_doc);
12989acbbeafSnn35248 	assert(valid != 0);
12999acbbeafSnn35248 
13009acbbeafSnn35248 	if (xmlSaveFormatFile(filename, handle->zone_dh_doc, 1) <= 0)
13019acbbeafSnn35248 		return (Z_SAVING_FILE);
13029acbbeafSnn35248 
13039acbbeafSnn35248 	return (Z_OK);
13049acbbeafSnn35248 }
13059acbbeafSnn35248 
13069acbbeafSnn35248 int
13078cd327d5Sgjelinek zonecfg_detach_save(zone_dochandle_t handle, uint_t flags)
1308ee519a1fSgjelinek {
1309ee519a1fSgjelinek 	char zname[ZONENAME_MAX];
1310ee519a1fSgjelinek 	char path[MAXPATHLEN];
1311ee519a1fSgjelinek 	char migpath[MAXPATHLEN];
1312ee519a1fSgjelinek 	xmlValidCtxt cvp = { NULL };
1313ee519a1fSgjelinek 	int err = Z_SAVING_FILE;
13149acbbeafSnn35248 	int valid;
1315ee519a1fSgjelinek 
1316ee519a1fSgjelinek 	if (zonecfg_check_handle(handle) != Z_OK)
1317ee519a1fSgjelinek 		return (Z_BAD_HANDLE);
1318ee519a1fSgjelinek 
13198cd327d5Sgjelinek 	if (flags & ZONE_DRY_RUN) {
13208cd327d5Sgjelinek 		(void) strlcpy(migpath, "-", sizeof (migpath));
13218cd327d5Sgjelinek 	} else {
13228cd327d5Sgjelinek 		if ((err = zonecfg_get_name(handle, zname, sizeof (zname)))
13238cd327d5Sgjelinek 		    != Z_OK)
1324ee519a1fSgjelinek 			return (err);
1325ee519a1fSgjelinek 
13268cd327d5Sgjelinek 		if ((err = zone_get_zonepath(zname, path, sizeof (path)))
13278cd327d5Sgjelinek 		    != Z_OK)
1328ee519a1fSgjelinek 			return (err);
1329ee519a1fSgjelinek 
133016ab8c7bSgjelinek 		if (snprintf(migpath, sizeof (migpath), "%s/%s", path,
133116ab8c7bSgjelinek 		    ZONE_DETACHED) >= sizeof (migpath))
1332ee519a1fSgjelinek 			return (Z_NOMEM);
13338cd327d5Sgjelinek 	}
1334ee519a1fSgjelinek 
1335ee519a1fSgjelinek 	if ((err = operation_prep(handle)) != Z_OK)
1336ee519a1fSgjelinek 		return (err);
1337ee519a1fSgjelinek 
1338ee519a1fSgjelinek 	addcomment(handle, "\n    DO NOT EDIT THIS FILE.  "
1339ee519a1fSgjelinek 	    "Use zonecfg(1M) and zoneadm(1M) attach.\n");
1340ee519a1fSgjelinek 
1341ee519a1fSgjelinek 	cvp.error = zonecfg_error_func;
1342ee519a1fSgjelinek 	cvp.warning = zonecfg_error_func;
1343ee519a1fSgjelinek 
1344ee519a1fSgjelinek 	/*
13459acbbeafSnn35248 	 * We do a final validation of the document.  Since the library has
13469acbbeafSnn35248 	 * malfunctioned if it fails to validate, we follow-up with an
13479acbbeafSnn35248 	 * assert() that the doc is valid.
1348ee519a1fSgjelinek 	 */
13499acbbeafSnn35248 	valid = xmlValidateDocument(&cvp, handle->zone_dh_doc);
13509acbbeafSnn35248 	assert(valid != 0);
1351ee519a1fSgjelinek 
1352ee519a1fSgjelinek 	if (xmlSaveFormatFile(migpath, handle->zone_dh_doc, 1) <= 0)
1353ee519a1fSgjelinek 		return (Z_SAVING_FILE);
1354ee519a1fSgjelinek 
13558cd327d5Sgjelinek 	if (!(flags & ZONE_DRY_RUN))
1356ee519a1fSgjelinek 		(void) chmod(migpath, 0644);
1357ee519a1fSgjelinek 
1358ee519a1fSgjelinek 	stripcomments(handle);
1359ee519a1fSgjelinek 
1360ee519a1fSgjelinek 	handle->zone_dh_newzone = B_FALSE;
1361ee519a1fSgjelinek 
1362ee519a1fSgjelinek 	return (Z_OK);
1363ee519a1fSgjelinek }
1364ee519a1fSgjelinek 
1365ee519a1fSgjelinek boolean_t
1366ee519a1fSgjelinek zonecfg_detached(const char *path)
1367ee519a1fSgjelinek {
1368ee519a1fSgjelinek 	char		migpath[MAXPATHLEN];
1369ee519a1fSgjelinek 	struct stat	buf;
1370ee519a1fSgjelinek 
137116ab8c7bSgjelinek 	if (snprintf(migpath, sizeof (migpath), "%s/%s", path, ZONE_DETACHED) >=
1372ee519a1fSgjelinek 	    sizeof (migpath))
1373ee519a1fSgjelinek 		return (B_FALSE);
1374ee519a1fSgjelinek 
1375ee519a1fSgjelinek 	if (stat(migpath, &buf) != -1)
1376ee519a1fSgjelinek 		return (B_TRUE);
1377ee519a1fSgjelinek 
1378ee519a1fSgjelinek 	return (B_FALSE);
1379ee519a1fSgjelinek }
1380ee519a1fSgjelinek 
1381ee519a1fSgjelinek void
1382ee519a1fSgjelinek zonecfg_rm_detached(zone_dochandle_t handle, boolean_t forced)
1383ee519a1fSgjelinek {
1384ee519a1fSgjelinek 	char zname[ZONENAME_MAX];
1385ee519a1fSgjelinek 	char path[MAXPATHLEN];
1386ee519a1fSgjelinek 	char detached[MAXPATHLEN];
1387ee519a1fSgjelinek 	char attached[MAXPATHLEN];
1388ee519a1fSgjelinek 
1389ee519a1fSgjelinek 	if (zonecfg_check_handle(handle) != Z_OK)
1390ee519a1fSgjelinek 		return;
1391ee519a1fSgjelinek 
1392ee519a1fSgjelinek 	if (zonecfg_get_name(handle, zname, sizeof (zname)) != Z_OK)
1393ee519a1fSgjelinek 		return;
1394ee519a1fSgjelinek 
1395ee519a1fSgjelinek 	if (zone_get_zonepath(zname, path, sizeof (path)) != Z_OK)
1396ee519a1fSgjelinek 		return;
1397ee519a1fSgjelinek 
139816ab8c7bSgjelinek 	(void) snprintf(detached, sizeof (detached), "%s/%s", path,
139916ab8c7bSgjelinek 	    ZONE_DETACHED);
1400ee519a1fSgjelinek 	(void) snprintf(attached, sizeof (attached), "%s/%s", path,
1401ee519a1fSgjelinek 	    ATTACH_FORCED);
1402ee519a1fSgjelinek 
1403ee519a1fSgjelinek 	if (forced) {
1404ee519a1fSgjelinek 		(void) rename(detached, attached);
1405ee519a1fSgjelinek 	} else {
1406ee519a1fSgjelinek 		(void) unlink(attached);
1407ee519a1fSgjelinek 		(void) unlink(detached);
1408ee519a1fSgjelinek 	}
1409ee519a1fSgjelinek }
1410ee519a1fSgjelinek 
14117c478bd9Sstevel@tonic-gate /*
14127c478bd9Sstevel@tonic-gate  * Special case: if access(2) fails with ENOENT, then try again using
14137c478bd9Sstevel@tonic-gate  * ZONE_CONFIG_ROOT instead of config_file_path(zonename).  This is how we
14147c478bd9Sstevel@tonic-gate  * work around the case of a config file which has not been created yet:
14157c478bd9Sstevel@tonic-gate  * the user will need access to the directory so use that as a heuristic.
14167c478bd9Sstevel@tonic-gate  */
14177c478bd9Sstevel@tonic-gate 
14187c478bd9Sstevel@tonic-gate int
14197c478bd9Sstevel@tonic-gate zonecfg_access(const char *zonename, int amode)
14207c478bd9Sstevel@tonic-gate {
14217c478bd9Sstevel@tonic-gate 	char path[MAXPATHLEN];
14227c478bd9Sstevel@tonic-gate 
1423108322fbScarlsonj 	if (!config_file_path(zonename, path))
1424108322fbScarlsonj 		return (Z_INVAL);
14257c478bd9Sstevel@tonic-gate 	if (access(path, amode) == 0)
14267c478bd9Sstevel@tonic-gate 		return (Z_OK);
1427108322fbScarlsonj 	if (errno == ENOENT) {
1428108322fbScarlsonj 		if (snprintf(path, sizeof (path), "%s%s", zonecfg_root,
1429108322fbScarlsonj 		    ZONE_CONFIG_ROOT) >= sizeof (path))
1430108322fbScarlsonj 			return (Z_INVAL);
1431108322fbScarlsonj 		if (access(path, amode) == 0)
14327c478bd9Sstevel@tonic-gate 			return (Z_OK);
1433108322fbScarlsonj 	}
14347c478bd9Sstevel@tonic-gate 	if (errno == EACCES)
14357c478bd9Sstevel@tonic-gate 		return (Z_ACCES);
14367c478bd9Sstevel@tonic-gate 	if (errno == EINVAL)
14377c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
14387c478bd9Sstevel@tonic-gate 	return (Z_MISC_FS);
14397c478bd9Sstevel@tonic-gate }
14407c478bd9Sstevel@tonic-gate 
14417c478bd9Sstevel@tonic-gate int
1442108322fbScarlsonj zonecfg_create_snapshot(const char *zonename)
14437c478bd9Sstevel@tonic-gate {
14447c478bd9Sstevel@tonic-gate 	zone_dochandle_t handle;
14457c478bd9Sstevel@tonic-gate 	char path[MAXPATHLEN], zonepath[MAXPATHLEN], rpath[MAXPATHLEN];
14467c478bd9Sstevel@tonic-gate 	int error = Z_OK, res;
14477c478bd9Sstevel@tonic-gate 
14487c478bd9Sstevel@tonic-gate 	if ((handle = zonecfg_init_handle()) == NULL) {
14497c478bd9Sstevel@tonic-gate 		return (Z_NOMEM);
14507c478bd9Sstevel@tonic-gate 	}
14517c478bd9Sstevel@tonic-gate 
1452087719fdSdp 	handle->zone_dh_newzone = B_TRUE;
1453087719fdSdp 	handle->zone_dh_snapshot = B_TRUE;
1454087719fdSdp 
14557c478bd9Sstevel@tonic-gate 	if ((error = zonecfg_get_handle(zonename, handle)) != Z_OK)
14567c478bd9Sstevel@tonic-gate 		goto out;
14577c478bd9Sstevel@tonic-gate 	if ((error = operation_prep(handle)) != Z_OK)
14587c478bd9Sstevel@tonic-gate 		goto out;
14597c478bd9Sstevel@tonic-gate 	error = zonecfg_get_zonepath(handle, zonepath, sizeof (zonepath));
14607c478bd9Sstevel@tonic-gate 	if (error != Z_OK)
14617c478bd9Sstevel@tonic-gate 		goto out;
14627c478bd9Sstevel@tonic-gate 	if ((res = resolvepath(zonepath, rpath, sizeof (rpath))) == -1) {
14637c478bd9Sstevel@tonic-gate 		error = Z_RESOLVED_PATH;
14647c478bd9Sstevel@tonic-gate 		goto out;
14657c478bd9Sstevel@tonic-gate 	}
14667c478bd9Sstevel@tonic-gate 	/*
14677c478bd9Sstevel@tonic-gate 	 * If the resolved path is not the same as the original path, then
14687c478bd9Sstevel@tonic-gate 	 * save the resolved path in the snapshot, thus preventing any
14697c478bd9Sstevel@tonic-gate 	 * potential problems down the line when zoneadmd goes to unmount
14707c478bd9Sstevel@tonic-gate 	 * file systems and depends on initial string matches with resolved
14717c478bd9Sstevel@tonic-gate 	 * paths.
14727c478bd9Sstevel@tonic-gate 	 */
14737c478bd9Sstevel@tonic-gate 	rpath[res] = '\0';
14747c478bd9Sstevel@tonic-gate 	if (strcmp(zonepath, rpath) != 0) {
14757c478bd9Sstevel@tonic-gate 		if ((error = zonecfg_set_zonepath(handle, rpath)) != Z_OK)
14767c478bd9Sstevel@tonic-gate 			goto out;
14777c478bd9Sstevel@tonic-gate 	}
1478108322fbScarlsonj 	if (snprintf(path, sizeof (path), "%s%s", zonecfg_root,
1479108322fbScarlsonj 	    ZONE_SNAPSHOT_ROOT) >= sizeof (path)) {
1480108322fbScarlsonj 		error = Z_MISC_FS;
1481108322fbScarlsonj 		goto out;
1482108322fbScarlsonj 	}
1483108322fbScarlsonj 	if ((mkdir(path, S_IRWXU) == -1) && (errno != EEXIST)) {
14847c478bd9Sstevel@tonic-gate 		error = Z_MISC_FS;
14857c478bd9Sstevel@tonic-gate 		goto out;
14867c478bd9Sstevel@tonic-gate 	}
14877c478bd9Sstevel@tonic-gate 
1488108322fbScarlsonj 	if (!snap_file_path(zonename, path)) {
1489108322fbScarlsonj 		error = Z_MISC_FS;
1490108322fbScarlsonj 		goto out;
1491108322fbScarlsonj 	}
1492087719fdSdp 
1493087719fdSdp 	addcomment(handle, "\n    DO NOT EDIT THIS FILE.  "
1494087719fdSdp 	    "It is a snapshot of running zone state.\n");
1495087719fdSdp 
14967c478bd9Sstevel@tonic-gate 	error = zonecfg_save_impl(handle, path);
14977c478bd9Sstevel@tonic-gate 
1498087719fdSdp 	stripcomments(handle);
1499087719fdSdp 
15007c478bd9Sstevel@tonic-gate out:
15017c478bd9Sstevel@tonic-gate 	zonecfg_fini_handle(handle);
15027c478bd9Sstevel@tonic-gate 	return (error);
15037c478bd9Sstevel@tonic-gate }
15047c478bd9Sstevel@tonic-gate 
1505f4b3ec61Sdh155122 int
1506f4b3ec61Sdh155122 zonecfg_get_iptype(zone_dochandle_t handle, zone_iptype_t *iptypep)
1507f4b3ec61Sdh155122 {
1508f4b3ec61Sdh155122 	char property[10]; /* 10 is big enough for "shared"/"exclusive" */
1509f4b3ec61Sdh155122 	int err;
1510f4b3ec61Sdh155122 
1511f4b3ec61Sdh155122 	err = getrootattr(handle, DTD_ATTR_IPTYPE, property, sizeof (property));
1512f4b3ec61Sdh155122 	if (err == Z_BAD_PROPERTY) {
1513f4b3ec61Sdh155122 		/* Return default value */
1514f4b3ec61Sdh155122 		*iptypep = ZS_SHARED;
1515f4b3ec61Sdh155122 		return (Z_OK);
1516f4b3ec61Sdh155122 	} else if (err != Z_OK) {
1517f4b3ec61Sdh155122 		return (err);
1518f4b3ec61Sdh155122 	}
1519f4b3ec61Sdh155122 
1520f4b3ec61Sdh155122 	if (strlen(property) == 0 ||
1521f4b3ec61Sdh155122 	    strcmp(property, "shared") == 0)
1522f4b3ec61Sdh155122 		*iptypep = ZS_SHARED;
1523f4b3ec61Sdh155122 	else if (strcmp(property, "exclusive") == 0)
1524f4b3ec61Sdh155122 		*iptypep = ZS_EXCLUSIVE;
1525f4b3ec61Sdh155122 	else
1526f4b3ec61Sdh155122 		return (Z_INVAL);
1527f4b3ec61Sdh155122 
1528f4b3ec61Sdh155122 	return (Z_OK);
1529f4b3ec61Sdh155122 }
1530f4b3ec61Sdh155122 
1531f4b3ec61Sdh155122 int
1532f4b3ec61Sdh155122 zonecfg_set_iptype(zone_dochandle_t handle, zone_iptype_t iptype)
1533f4b3ec61Sdh155122 {
1534f4b3ec61Sdh155122 	xmlNodePtr cur;
1535f4b3ec61Sdh155122 
1536f4b3ec61Sdh155122 	if (handle == NULL)
1537f4b3ec61Sdh155122 		return (Z_INVAL);
1538f4b3ec61Sdh155122 
1539f4b3ec61Sdh155122 	cur = xmlDocGetRootElement(handle->zone_dh_doc);
1540f4b3ec61Sdh155122 	if (cur == NULL) {
1541f4b3ec61Sdh155122 		return (Z_EMPTY_DOCUMENT);
1542f4b3ec61Sdh155122 	}
1543f4b3ec61Sdh155122 
1544f4b3ec61Sdh155122 	if (xmlStrcmp(cur->name, DTD_ELEM_ZONE) != 0) {
1545f4b3ec61Sdh155122 		return (Z_WRONG_DOC_TYPE);
1546f4b3ec61Sdh155122 	}
1547f4b3ec61Sdh155122 	switch (iptype) {
1548f4b3ec61Sdh155122 	case ZS_SHARED:
1549f4b3ec61Sdh155122 		/*
1550f4b3ec61Sdh155122 		 * Since "shared" is the default, we don't write it to the
1551f4b3ec61Sdh155122 		 * configuration file, so that it's easier to migrate those
1552f4b3ec61Sdh155122 		 * zones elsewhere, eg., to systems which are not IP-Instances
1553f4b3ec61Sdh155122 		 * aware.
1554f4b3ec61Sdh155122 		 * xmlUnsetProp only fails when the attribute doesn't exist,
1555f4b3ec61Sdh155122 		 * which we don't care.
1556f4b3ec61Sdh155122 		 */
1557f4b3ec61Sdh155122 		(void) xmlUnsetProp(cur, DTD_ATTR_IPTYPE);
1558f4b3ec61Sdh155122 		break;
1559f4b3ec61Sdh155122 	case ZS_EXCLUSIVE:
1560f4b3ec61Sdh155122 		if (xmlSetProp(cur, DTD_ATTR_IPTYPE,
1561f4b3ec61Sdh155122 		    (const xmlChar *) "exclusive") == NULL)
1562f4b3ec61Sdh155122 			return (Z_INVAL);
1563f4b3ec61Sdh155122 		break;
1564f4b3ec61Sdh155122 	}
1565f4b3ec61Sdh155122 	return (Z_OK);
1566f4b3ec61Sdh155122 }
1567f4b3ec61Sdh155122 
15687c478bd9Sstevel@tonic-gate static int
1569a1be23daSdp newprop(xmlNodePtr node, const xmlChar *attrname, char *src)
15707c478bd9Sstevel@tonic-gate {
15717c478bd9Sstevel@tonic-gate 	xmlAttrPtr newattr;
15727c478bd9Sstevel@tonic-gate 
15737c478bd9Sstevel@tonic-gate 	newattr = xmlNewProp(node, attrname, (xmlChar *)src);
15747c478bd9Sstevel@tonic-gate 	if (newattr == NULL) {
15757c478bd9Sstevel@tonic-gate 		xmlUnlinkNode(node);
15767c478bd9Sstevel@tonic-gate 		xmlFreeNode(node);
15777c478bd9Sstevel@tonic-gate 		return (Z_BAD_PROPERTY);
15787c478bd9Sstevel@tonic-gate 	}
15797c478bd9Sstevel@tonic-gate 	return (Z_OK);
15807c478bd9Sstevel@tonic-gate }
15817c478bd9Sstevel@tonic-gate 
15827c478bd9Sstevel@tonic-gate static int
15837c478bd9Sstevel@tonic-gate zonecfg_add_filesystem_core(zone_dochandle_t handle, struct zone_fstab *tabptr)
15847c478bd9Sstevel@tonic-gate {
15857c478bd9Sstevel@tonic-gate 	xmlNodePtr newnode, cur = handle->zone_dh_cur, options_node;
15867c478bd9Sstevel@tonic-gate 	zone_fsopt_t *ptr;
15877c478bd9Sstevel@tonic-gate 	int err;
15887c478bd9Sstevel@tonic-gate 
15897c478bd9Sstevel@tonic-gate 	newnode = xmlNewTextChild(cur, NULL, DTD_ELEM_FS, NULL);
1590a1be23daSdp 	if ((err = newprop(newnode, DTD_ATTR_SPECIAL,
15917c478bd9Sstevel@tonic-gate 	    tabptr->zone_fs_special)) != Z_OK)
15927c478bd9Sstevel@tonic-gate 		return (err);
15937c478bd9Sstevel@tonic-gate 	if (tabptr->zone_fs_raw[0] != '\0' &&
1594a1be23daSdp 	    (err = newprop(newnode, DTD_ATTR_RAW, tabptr->zone_fs_raw)) != Z_OK)
15957c478bd9Sstevel@tonic-gate 		return (err);
1596a1be23daSdp 	if ((err = newprop(newnode, DTD_ATTR_DIR, tabptr->zone_fs_dir)) != Z_OK)
15977c478bd9Sstevel@tonic-gate 		return (err);
1598a1be23daSdp 	if ((err = newprop(newnode, DTD_ATTR_TYPE,
15997c478bd9Sstevel@tonic-gate 	    tabptr->zone_fs_type)) != Z_OK)
16007c478bd9Sstevel@tonic-gate 		return (err);
16017c478bd9Sstevel@tonic-gate 	if (tabptr->zone_fs_options != NULL) {
16027c478bd9Sstevel@tonic-gate 		for (ptr = tabptr->zone_fs_options; ptr != NULL;
16037c478bd9Sstevel@tonic-gate 		    ptr = ptr->zone_fsopt_next) {
16047c478bd9Sstevel@tonic-gate 			options_node = xmlNewTextChild(newnode, NULL,
16057c478bd9Sstevel@tonic-gate 			    DTD_ELEM_FSOPTION, NULL);
1606a1be23daSdp 			if ((err = newprop(options_node, DTD_ATTR_NAME,
16077c478bd9Sstevel@tonic-gate 			    ptr->zone_fsopt_opt)) != Z_OK)
16087c478bd9Sstevel@tonic-gate 				return (err);
16097c478bd9Sstevel@tonic-gate 		}
16107c478bd9Sstevel@tonic-gate 	}
16117c478bd9Sstevel@tonic-gate 	return (Z_OK);
16127c478bd9Sstevel@tonic-gate }
16137c478bd9Sstevel@tonic-gate 
16147c478bd9Sstevel@tonic-gate int
16157c478bd9Sstevel@tonic-gate zonecfg_add_filesystem(zone_dochandle_t handle, struct zone_fstab *tabptr)
16167c478bd9Sstevel@tonic-gate {
16177c478bd9Sstevel@tonic-gate 	int err;
16187c478bd9Sstevel@tonic-gate 
16197c478bd9Sstevel@tonic-gate 	if (tabptr == NULL)
16207c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
16217c478bd9Sstevel@tonic-gate 
16227c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
16237c478bd9Sstevel@tonic-gate 		return (err);
16247c478bd9Sstevel@tonic-gate 
16257c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_add_filesystem_core(handle, tabptr)) != Z_OK)
16267c478bd9Sstevel@tonic-gate 		return (err);
16277c478bd9Sstevel@tonic-gate 
16287c478bd9Sstevel@tonic-gate 	return (Z_OK);
16297c478bd9Sstevel@tonic-gate }
16307c478bd9Sstevel@tonic-gate 
16317c478bd9Sstevel@tonic-gate static int
16327c478bd9Sstevel@tonic-gate zonecfg_add_ipd_core(zone_dochandle_t handle, struct zone_fstab *tabptr)
16337c478bd9Sstevel@tonic-gate {
16347c478bd9Sstevel@tonic-gate 	xmlNodePtr newnode, cur = handle->zone_dh_cur;
16357c478bd9Sstevel@tonic-gate 	int err;
16367c478bd9Sstevel@tonic-gate 
16377c478bd9Sstevel@tonic-gate 	newnode = xmlNewTextChild(cur, NULL, DTD_ELEM_IPD, NULL);
1638a1be23daSdp 	if ((err = newprop(newnode, DTD_ATTR_DIR, tabptr->zone_fs_dir)) != Z_OK)
16397c478bd9Sstevel@tonic-gate 		return (err);
16407c478bd9Sstevel@tonic-gate 	return (Z_OK);
16417c478bd9Sstevel@tonic-gate }
16427c478bd9Sstevel@tonic-gate 
16437c478bd9Sstevel@tonic-gate int
16447c478bd9Sstevel@tonic-gate zonecfg_add_ipd(zone_dochandle_t handle, struct zone_fstab *tabptr)
16457c478bd9Sstevel@tonic-gate {
16467c478bd9Sstevel@tonic-gate 	int err;
16477c478bd9Sstevel@tonic-gate 
16487c478bd9Sstevel@tonic-gate 	if (tabptr == NULL)
16497c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
16507c478bd9Sstevel@tonic-gate 
16517c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
16527c478bd9Sstevel@tonic-gate 		return (err);
16537c478bd9Sstevel@tonic-gate 
16547c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_add_ipd_core(handle, tabptr)) != Z_OK)
16557c478bd9Sstevel@tonic-gate 		return (err);
16567c478bd9Sstevel@tonic-gate 
16577c478bd9Sstevel@tonic-gate 	return (Z_OK);
16587c478bd9Sstevel@tonic-gate }
16597c478bd9Sstevel@tonic-gate 
16607c478bd9Sstevel@tonic-gate int
16617c478bd9Sstevel@tonic-gate zonecfg_add_fs_option(struct zone_fstab *tabptr, char *option)
16627c478bd9Sstevel@tonic-gate {
16637c478bd9Sstevel@tonic-gate 	zone_fsopt_t *last, *old, *new;
16647c478bd9Sstevel@tonic-gate 
16657c478bd9Sstevel@tonic-gate 	last = tabptr->zone_fs_options;
16667c478bd9Sstevel@tonic-gate 	for (old = last; old != NULL; old = old->zone_fsopt_next)
16677c478bd9Sstevel@tonic-gate 		last = old;	/* walk to the end of the list */
16687c478bd9Sstevel@tonic-gate 	new = (zone_fsopt_t *)malloc(sizeof (zone_fsopt_t));
16697c478bd9Sstevel@tonic-gate 	if (new == NULL)
16707c478bd9Sstevel@tonic-gate 		return (Z_NOMEM);
16717c478bd9Sstevel@tonic-gate 	(void) strlcpy(new->zone_fsopt_opt, option,
16727c478bd9Sstevel@tonic-gate 	    sizeof (new->zone_fsopt_opt));
16737c478bd9Sstevel@tonic-gate 	new->zone_fsopt_next = NULL;
16747c478bd9Sstevel@tonic-gate 	if (last == NULL)
16757c478bd9Sstevel@tonic-gate 		tabptr->zone_fs_options = new;
16767c478bd9Sstevel@tonic-gate 	else
16777c478bd9Sstevel@tonic-gate 		last->zone_fsopt_next = new;
16787c478bd9Sstevel@tonic-gate 	return (Z_OK);
16797c478bd9Sstevel@tonic-gate }
16807c478bd9Sstevel@tonic-gate 
16817c478bd9Sstevel@tonic-gate int
16827c478bd9Sstevel@tonic-gate zonecfg_remove_fs_option(struct zone_fstab *tabptr, char *option)
16837c478bd9Sstevel@tonic-gate {
16847c478bd9Sstevel@tonic-gate 	zone_fsopt_t *last, *this, *next;
16857c478bd9Sstevel@tonic-gate 
16867c478bd9Sstevel@tonic-gate 	last = tabptr->zone_fs_options;
16877c478bd9Sstevel@tonic-gate 	for (this = last; this != NULL; this = this->zone_fsopt_next) {
16887c478bd9Sstevel@tonic-gate 		if (strcmp(this->zone_fsopt_opt, option) == 0) {
16897c478bd9Sstevel@tonic-gate 			next = this->zone_fsopt_next;
16907c478bd9Sstevel@tonic-gate 			if (this == tabptr->zone_fs_options)
16917c478bd9Sstevel@tonic-gate 				tabptr->zone_fs_options = next;
16927c478bd9Sstevel@tonic-gate 			else
16937c478bd9Sstevel@tonic-gate 				last->zone_fsopt_next = next;
16947c478bd9Sstevel@tonic-gate 			free(this);
16957c478bd9Sstevel@tonic-gate 			return (Z_OK);
16967c478bd9Sstevel@tonic-gate 		} else
16977c478bd9Sstevel@tonic-gate 			last = this;
16987c478bd9Sstevel@tonic-gate 	}
16997c478bd9Sstevel@tonic-gate 	return (Z_NO_PROPERTY_ID);
17007c478bd9Sstevel@tonic-gate }
17017c478bd9Sstevel@tonic-gate 
17027c478bd9Sstevel@tonic-gate void
17037c478bd9Sstevel@tonic-gate zonecfg_free_fs_option_list(zone_fsopt_t *list)
17047c478bd9Sstevel@tonic-gate {
17057c478bd9Sstevel@tonic-gate 	zone_fsopt_t *this, *next;
17067c478bd9Sstevel@tonic-gate 
17077c478bd9Sstevel@tonic-gate 	for (this = list; this != NULL; this = next) {
17087c478bd9Sstevel@tonic-gate 		next = this->zone_fsopt_next;
17097c478bd9Sstevel@tonic-gate 		free(this);
17107c478bd9Sstevel@tonic-gate 	}
17117c478bd9Sstevel@tonic-gate }
17127c478bd9Sstevel@tonic-gate 
17137c478bd9Sstevel@tonic-gate void
17147c478bd9Sstevel@tonic-gate zonecfg_free_rctl_value_list(struct zone_rctlvaltab *valtab)
17157c478bd9Sstevel@tonic-gate {
17167c478bd9Sstevel@tonic-gate 	if (valtab == NULL)
17177c478bd9Sstevel@tonic-gate 		return;
17187c478bd9Sstevel@tonic-gate 	zonecfg_free_rctl_value_list(valtab->zone_rctlval_next);
17197c478bd9Sstevel@tonic-gate 	free(valtab);
17207c478bd9Sstevel@tonic-gate }
17217c478bd9Sstevel@tonic-gate 
17227c478bd9Sstevel@tonic-gate static boolean_t
17237c478bd9Sstevel@tonic-gate match_prop(xmlNodePtr cur, const xmlChar *attr, char *user_prop)
17247c478bd9Sstevel@tonic-gate {
17257c478bd9Sstevel@tonic-gate 	xmlChar *gotten_prop;
17267c478bd9Sstevel@tonic-gate 	int prop_result;
17277c478bd9Sstevel@tonic-gate 
17287c478bd9Sstevel@tonic-gate 	gotten_prop = xmlGetProp(cur, attr);
17297c478bd9Sstevel@tonic-gate 	if (gotten_prop == NULL)	/* shouldn't happen */
17307c478bd9Sstevel@tonic-gate 		return (B_FALSE);
17317c478bd9Sstevel@tonic-gate 	prop_result = xmlStrcmp(gotten_prop, (const xmlChar *) user_prop);
17327c478bd9Sstevel@tonic-gate 	xmlFree(gotten_prop);
17337c478bd9Sstevel@tonic-gate 	return ((prop_result == 0));
17347c478bd9Sstevel@tonic-gate }
17357c478bd9Sstevel@tonic-gate 
17367c478bd9Sstevel@tonic-gate static int
17377c478bd9Sstevel@tonic-gate zonecfg_delete_filesystem_core(zone_dochandle_t handle,
17387c478bd9Sstevel@tonic-gate     struct zone_fstab *tabptr)
17397c478bd9Sstevel@tonic-gate {
17407c478bd9Sstevel@tonic-gate 	xmlNodePtr cur = handle->zone_dh_cur;
17417c478bd9Sstevel@tonic-gate 	boolean_t dir_match, spec_match, raw_match, type_match;
17427c478bd9Sstevel@tonic-gate 
17437c478bd9Sstevel@tonic-gate 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
17447c478bd9Sstevel@tonic-gate 		if (xmlStrcmp(cur->name, DTD_ELEM_FS))
17457c478bd9Sstevel@tonic-gate 			continue;
17467c478bd9Sstevel@tonic-gate 		dir_match = match_prop(cur, DTD_ATTR_DIR, tabptr->zone_fs_dir);
17477c478bd9Sstevel@tonic-gate 		spec_match = match_prop(cur, DTD_ATTR_SPECIAL,
17487c478bd9Sstevel@tonic-gate 		    tabptr->zone_fs_special);
17497c478bd9Sstevel@tonic-gate 		raw_match = match_prop(cur, DTD_ATTR_RAW,
17507c478bd9Sstevel@tonic-gate 		    tabptr->zone_fs_raw);
17517c478bd9Sstevel@tonic-gate 		type_match = match_prop(cur, DTD_ATTR_TYPE,
17527c478bd9Sstevel@tonic-gate 		    tabptr->zone_fs_type);
17537c478bd9Sstevel@tonic-gate 		if (dir_match && spec_match && raw_match && type_match) {
17547c478bd9Sstevel@tonic-gate 			xmlUnlinkNode(cur);
17557c478bd9Sstevel@tonic-gate 			xmlFreeNode(cur);
17567c478bd9Sstevel@tonic-gate 			return (Z_OK);
17577c478bd9Sstevel@tonic-gate 		}
17587c478bd9Sstevel@tonic-gate 	}
17597c478bd9Sstevel@tonic-gate 	return (Z_NO_RESOURCE_ID);
17607c478bd9Sstevel@tonic-gate }
17617c478bd9Sstevel@tonic-gate 
17627c478bd9Sstevel@tonic-gate int
17637c478bd9Sstevel@tonic-gate zonecfg_delete_filesystem(zone_dochandle_t handle, struct zone_fstab *tabptr)
17647c478bd9Sstevel@tonic-gate {
17657c478bd9Sstevel@tonic-gate 	int err;
17667c478bd9Sstevel@tonic-gate 
17677c478bd9Sstevel@tonic-gate 	if (tabptr == NULL)
17687c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
17697c478bd9Sstevel@tonic-gate 
17707c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
17717c478bd9Sstevel@tonic-gate 		return (err);
17727c478bd9Sstevel@tonic-gate 
17737c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_delete_filesystem_core(handle, tabptr)) != Z_OK)
17747c478bd9Sstevel@tonic-gate 		return (err);
17757c478bd9Sstevel@tonic-gate 
17767c478bd9Sstevel@tonic-gate 	return (Z_OK);
17777c478bd9Sstevel@tonic-gate }
17787c478bd9Sstevel@tonic-gate 
17797c478bd9Sstevel@tonic-gate int
17807c478bd9Sstevel@tonic-gate zonecfg_modify_filesystem(
17817c478bd9Sstevel@tonic-gate 	zone_dochandle_t handle,
17827c478bd9Sstevel@tonic-gate 	struct zone_fstab *oldtabptr,
17837c478bd9Sstevel@tonic-gate 	struct zone_fstab *newtabptr)
17847c478bd9Sstevel@tonic-gate {
17857c478bd9Sstevel@tonic-gate 	int err;
17867c478bd9Sstevel@tonic-gate 
17877c478bd9Sstevel@tonic-gate 	if (oldtabptr == NULL || newtabptr == NULL)
17887c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
17897c478bd9Sstevel@tonic-gate 
17907c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
17917c478bd9Sstevel@tonic-gate 		return (err);
17927c478bd9Sstevel@tonic-gate 
17937c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_delete_filesystem_core(handle, oldtabptr)) != Z_OK)
17947c478bd9Sstevel@tonic-gate 		return (err);
17957c478bd9Sstevel@tonic-gate 
17967c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_add_filesystem_core(handle, newtabptr)) != Z_OK)
17977c478bd9Sstevel@tonic-gate 		return (err);
17987c478bd9Sstevel@tonic-gate 
17997c478bd9Sstevel@tonic-gate 	return (Z_OK);
18007c478bd9Sstevel@tonic-gate }
18017c478bd9Sstevel@tonic-gate 
18027c478bd9Sstevel@tonic-gate static int
18037c478bd9Sstevel@tonic-gate zonecfg_delete_ipd_core(zone_dochandle_t handle, struct zone_fstab *tabptr)
18047c478bd9Sstevel@tonic-gate {
18057c478bd9Sstevel@tonic-gate 	xmlNodePtr cur = handle->zone_dh_cur;
18067c478bd9Sstevel@tonic-gate 
18077c478bd9Sstevel@tonic-gate 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
18087c478bd9Sstevel@tonic-gate 		if (xmlStrcmp(cur->name, DTD_ELEM_IPD))
18097c478bd9Sstevel@tonic-gate 			continue;
18107c478bd9Sstevel@tonic-gate 		if (match_prop(cur, DTD_ATTR_DIR, tabptr->zone_fs_dir)) {
18117c478bd9Sstevel@tonic-gate 			xmlUnlinkNode(cur);
18127c478bd9Sstevel@tonic-gate 			xmlFreeNode(cur);
18137c478bd9Sstevel@tonic-gate 			return (Z_OK);
18147c478bd9Sstevel@tonic-gate 		}
18157c478bd9Sstevel@tonic-gate 	}
18167c478bd9Sstevel@tonic-gate 	return (Z_NO_RESOURCE_ID);
18177c478bd9Sstevel@tonic-gate }
18187c478bd9Sstevel@tonic-gate 
18197c478bd9Sstevel@tonic-gate int
18207c478bd9Sstevel@tonic-gate zonecfg_delete_ipd(zone_dochandle_t handle, struct zone_fstab *tabptr)
18217c478bd9Sstevel@tonic-gate {
18227c478bd9Sstevel@tonic-gate 	int err;
18237c478bd9Sstevel@tonic-gate 
18247c478bd9Sstevel@tonic-gate 	if (tabptr == NULL)
18257c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
18267c478bd9Sstevel@tonic-gate 
18277c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
18287c478bd9Sstevel@tonic-gate 		return (err);
18297c478bd9Sstevel@tonic-gate 
18307c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_delete_ipd_core(handle, tabptr)) != Z_OK)
18317c478bd9Sstevel@tonic-gate 		return (err);
18327c478bd9Sstevel@tonic-gate 
18337c478bd9Sstevel@tonic-gate 	return (Z_OK);
18347c478bd9Sstevel@tonic-gate }
18357c478bd9Sstevel@tonic-gate 
18367c478bd9Sstevel@tonic-gate int
18377c478bd9Sstevel@tonic-gate zonecfg_modify_ipd(zone_dochandle_t handle, struct zone_fstab *oldtabptr,
18387c478bd9Sstevel@tonic-gate     struct zone_fstab *newtabptr)
18397c478bd9Sstevel@tonic-gate {
18407c478bd9Sstevel@tonic-gate 	int err;
18417c478bd9Sstevel@tonic-gate 
18427c478bd9Sstevel@tonic-gate 	if (oldtabptr == NULL || newtabptr == NULL)
18437c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
18447c478bd9Sstevel@tonic-gate 
18457c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
18467c478bd9Sstevel@tonic-gate 		return (err);
18477c478bd9Sstevel@tonic-gate 
18487c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_delete_ipd_core(handle, oldtabptr)) != Z_OK)
18497c478bd9Sstevel@tonic-gate 		return (err);
18507c478bd9Sstevel@tonic-gate 
18517c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_add_ipd_core(handle, newtabptr)) != Z_OK)
18527c478bd9Sstevel@tonic-gate 		return (err);
18537c478bd9Sstevel@tonic-gate 
18547c478bd9Sstevel@tonic-gate 	return (Z_OK);
18557c478bd9Sstevel@tonic-gate }
18567c478bd9Sstevel@tonic-gate 
18577c478bd9Sstevel@tonic-gate int
18587c478bd9Sstevel@tonic-gate zonecfg_lookup_filesystem(
18597c478bd9Sstevel@tonic-gate 	zone_dochandle_t handle,
18607c478bd9Sstevel@tonic-gate 	struct zone_fstab *tabptr)
18617c478bd9Sstevel@tonic-gate {
18627c478bd9Sstevel@tonic-gate 	xmlNodePtr cur, options, firstmatch;
18637c478bd9Sstevel@tonic-gate 	int err;
18647c478bd9Sstevel@tonic-gate 	char dirname[MAXPATHLEN], special[MAXPATHLEN], raw[MAXPATHLEN];
18657c478bd9Sstevel@tonic-gate 	char type[FSTYPSZ];
18667c478bd9Sstevel@tonic-gate 	char options_str[MAX_MNTOPT_STR];
18677c478bd9Sstevel@tonic-gate 
18687c478bd9Sstevel@tonic-gate 	if (tabptr == NULL)
18697c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
18707c478bd9Sstevel@tonic-gate 
18717c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
18727c478bd9Sstevel@tonic-gate 		return (err);
18737c478bd9Sstevel@tonic-gate 
18747c478bd9Sstevel@tonic-gate 	/*
18757c478bd9Sstevel@tonic-gate 	 * Walk the list of children looking for matches on any properties
18767c478bd9Sstevel@tonic-gate 	 * specified in the fstab parameter.  If more than one resource
18777c478bd9Sstevel@tonic-gate 	 * matches, we return Z_INSUFFICIENT_SPEC; if none match, we return
18787c478bd9Sstevel@tonic-gate 	 * Z_NO_RESOURCE_ID.
18797c478bd9Sstevel@tonic-gate 	 */
18807c478bd9Sstevel@tonic-gate 	cur = handle->zone_dh_cur;
18817c478bd9Sstevel@tonic-gate 	firstmatch = NULL;
18827c478bd9Sstevel@tonic-gate 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
18837c478bd9Sstevel@tonic-gate 		if (xmlStrcmp(cur->name, DTD_ELEM_FS))
18847c478bd9Sstevel@tonic-gate 			continue;
18857c478bd9Sstevel@tonic-gate 		if (strlen(tabptr->zone_fs_dir) > 0) {
18867c478bd9Sstevel@tonic-gate 			if ((fetchprop(cur, DTD_ATTR_DIR, dirname,
18877c478bd9Sstevel@tonic-gate 			    sizeof (dirname)) == Z_OK) &&
18887c478bd9Sstevel@tonic-gate 			    (strcmp(tabptr->zone_fs_dir, dirname) == 0)) {
18897c478bd9Sstevel@tonic-gate 				if (firstmatch == NULL)
18907c478bd9Sstevel@tonic-gate 					firstmatch = cur;
18917c478bd9Sstevel@tonic-gate 				else
18927c478bd9Sstevel@tonic-gate 					return (Z_INSUFFICIENT_SPEC);
18937c478bd9Sstevel@tonic-gate 			}
18947c478bd9Sstevel@tonic-gate 		}
18957c478bd9Sstevel@tonic-gate 		if (strlen(tabptr->zone_fs_special) > 0) {
18967c478bd9Sstevel@tonic-gate 			if ((fetchprop(cur, DTD_ATTR_SPECIAL, special,
18977c478bd9Sstevel@tonic-gate 			    sizeof (special)) == Z_OK)) {
18987c478bd9Sstevel@tonic-gate 				if (strcmp(tabptr->zone_fs_special,
18997c478bd9Sstevel@tonic-gate 				    special) == 0) {
19007c478bd9Sstevel@tonic-gate 					if (firstmatch == NULL)
19017c478bd9Sstevel@tonic-gate 						firstmatch = cur;
19027c478bd9Sstevel@tonic-gate 					else if (firstmatch != cur)
19037c478bd9Sstevel@tonic-gate 						return (Z_INSUFFICIENT_SPEC);
19047c478bd9Sstevel@tonic-gate 				} else {
19057c478bd9Sstevel@tonic-gate 					/*
19067c478bd9Sstevel@tonic-gate 					 * If another property matched but this
19077c478bd9Sstevel@tonic-gate 					 * one doesn't then reset firstmatch.
19087c478bd9Sstevel@tonic-gate 					 */
19097c478bd9Sstevel@tonic-gate 					if (firstmatch == cur)
19107c478bd9Sstevel@tonic-gate 						firstmatch = NULL;
19117c478bd9Sstevel@tonic-gate 				}
19127c478bd9Sstevel@tonic-gate 			}
19137c478bd9Sstevel@tonic-gate 		}
19147c478bd9Sstevel@tonic-gate 		if (strlen(tabptr->zone_fs_raw) > 0) {
19157c478bd9Sstevel@tonic-gate 			if ((fetchprop(cur, DTD_ATTR_RAW, raw,
19167c478bd9Sstevel@tonic-gate 			    sizeof (raw)) == Z_OK)) {
19177c478bd9Sstevel@tonic-gate 				if (strcmp(tabptr->zone_fs_raw, raw) == 0) {
19187c478bd9Sstevel@tonic-gate 					if (firstmatch == NULL)
19197c478bd9Sstevel@tonic-gate 						firstmatch = cur;
19207c478bd9Sstevel@tonic-gate 					else if (firstmatch != cur)
19217c478bd9Sstevel@tonic-gate 						return (Z_INSUFFICIENT_SPEC);
19227c478bd9Sstevel@tonic-gate 				} else {
19237c478bd9Sstevel@tonic-gate 					/*
19247c478bd9Sstevel@tonic-gate 					 * If another property matched but this
19257c478bd9Sstevel@tonic-gate 					 * one doesn't then reset firstmatch.
19267c478bd9Sstevel@tonic-gate 					 */
19277c478bd9Sstevel@tonic-gate 					if (firstmatch == cur)
19287c478bd9Sstevel@tonic-gate 						firstmatch = NULL;
19297c478bd9Sstevel@tonic-gate 				}
19307c478bd9Sstevel@tonic-gate 			}
19317c478bd9Sstevel@tonic-gate 		}
19327c478bd9Sstevel@tonic-gate 		if (strlen(tabptr->zone_fs_type) > 0) {
19337c478bd9Sstevel@tonic-gate 			if ((fetchprop(cur, DTD_ATTR_TYPE, type,
19347c478bd9Sstevel@tonic-gate 			    sizeof (type)) == Z_OK)) {
19357c478bd9Sstevel@tonic-gate 				if (strcmp(tabptr->zone_fs_type, type) == 0) {
19367c478bd9Sstevel@tonic-gate 					if (firstmatch == NULL)
19377c478bd9Sstevel@tonic-gate 						firstmatch = cur;
19387c478bd9Sstevel@tonic-gate 					else if (firstmatch != cur)
19397c478bd9Sstevel@tonic-gate 						return (Z_INSUFFICIENT_SPEC);
19407c478bd9Sstevel@tonic-gate 				} else {
19417c478bd9Sstevel@tonic-gate 					/*
19427c478bd9Sstevel@tonic-gate 					 * If another property matched but this
19437c478bd9Sstevel@tonic-gate 					 * one doesn't then reset firstmatch.
19447c478bd9Sstevel@tonic-gate 					 */
19457c478bd9Sstevel@tonic-gate 					if (firstmatch == cur)
19467c478bd9Sstevel@tonic-gate 						firstmatch = NULL;
19477c478bd9Sstevel@tonic-gate 				}
19487c478bd9Sstevel@tonic-gate 			}
19497c478bd9Sstevel@tonic-gate 		}
19507c478bd9Sstevel@tonic-gate 	}
19517c478bd9Sstevel@tonic-gate 
19527c478bd9Sstevel@tonic-gate 	if (firstmatch == NULL)
19537c478bd9Sstevel@tonic-gate 		return (Z_NO_RESOURCE_ID);
19547c478bd9Sstevel@tonic-gate 
19557c478bd9Sstevel@tonic-gate 	cur = firstmatch;
19567c478bd9Sstevel@tonic-gate 
19577c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_DIR, tabptr->zone_fs_dir,
19587c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_fs_dir))) != Z_OK)
19597c478bd9Sstevel@tonic-gate 		return (err);
19607c478bd9Sstevel@tonic-gate 
19617c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_SPECIAL, tabptr->zone_fs_special,
19627c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_fs_special))) != Z_OK)
19637c478bd9Sstevel@tonic-gate 		return (err);
19647c478bd9Sstevel@tonic-gate 
19657c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_RAW, tabptr->zone_fs_raw,
19667c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_fs_raw))) != Z_OK)
19677c478bd9Sstevel@tonic-gate 		return (err);
19687c478bd9Sstevel@tonic-gate 
19697c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_TYPE, tabptr->zone_fs_type,
19707c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_fs_type))) != Z_OK)
19717c478bd9Sstevel@tonic-gate 		return (err);
19727c478bd9Sstevel@tonic-gate 
19737c478bd9Sstevel@tonic-gate 	/* options are optional */
19747c478bd9Sstevel@tonic-gate 	tabptr->zone_fs_options = NULL;
19757c478bd9Sstevel@tonic-gate 	for (options = cur->xmlChildrenNode; options != NULL;
19767c478bd9Sstevel@tonic-gate 	    options = options->next) {
19777c478bd9Sstevel@tonic-gate 		if ((fetchprop(options, DTD_ATTR_NAME, options_str,
19787c478bd9Sstevel@tonic-gate 		    sizeof (options_str)) != Z_OK))
19797c478bd9Sstevel@tonic-gate 			break;
19807c478bd9Sstevel@tonic-gate 		if (zonecfg_add_fs_option(tabptr, options_str) != Z_OK)
19817c478bd9Sstevel@tonic-gate 			break;
19827c478bd9Sstevel@tonic-gate 	}
19837c478bd9Sstevel@tonic-gate 	return (Z_OK);
19847c478bd9Sstevel@tonic-gate }
19857c478bd9Sstevel@tonic-gate 
19867c478bd9Sstevel@tonic-gate int
19877c478bd9Sstevel@tonic-gate zonecfg_lookup_ipd(zone_dochandle_t handle, struct zone_fstab *tabptr)
19887c478bd9Sstevel@tonic-gate {
19897c478bd9Sstevel@tonic-gate 	xmlNodePtr cur, match;
19907c478bd9Sstevel@tonic-gate 	int err;
19917c478bd9Sstevel@tonic-gate 	char dirname[MAXPATHLEN];
19927c478bd9Sstevel@tonic-gate 
19937c478bd9Sstevel@tonic-gate 	if (tabptr == NULL)
19947c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
19957c478bd9Sstevel@tonic-gate 
19967c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
19977c478bd9Sstevel@tonic-gate 		return (err);
19987c478bd9Sstevel@tonic-gate 
19997c478bd9Sstevel@tonic-gate 	/*
20007c478bd9Sstevel@tonic-gate 	 * General algorithm:
20017c478bd9Sstevel@tonic-gate 	 * Walk the list of children looking for matches on any properties
20027c478bd9Sstevel@tonic-gate 	 * specified in the fstab parameter.  If more than one resource
20037c478bd9Sstevel@tonic-gate 	 * matches, we return Z_INSUFFICIENT_SPEC; if none match, we return
20047c478bd9Sstevel@tonic-gate 	 * Z_NO_RESOURCE_ID.
20057c478bd9Sstevel@tonic-gate 	 */
20067c478bd9Sstevel@tonic-gate 	cur = handle->zone_dh_cur;
20077c478bd9Sstevel@tonic-gate 	match = NULL;
20087c478bd9Sstevel@tonic-gate 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
20097c478bd9Sstevel@tonic-gate 		if (xmlStrcmp(cur->name, DTD_ELEM_IPD))
20107c478bd9Sstevel@tonic-gate 			continue;
20117c478bd9Sstevel@tonic-gate 		if (strlen(tabptr->zone_fs_dir) > 0) {
20127c478bd9Sstevel@tonic-gate 			if ((fetchprop(cur, DTD_ATTR_DIR, dirname,
20137c478bd9Sstevel@tonic-gate 			    sizeof (dirname)) == Z_OK) &&
20147c478bd9Sstevel@tonic-gate 			    (strcmp(tabptr->zone_fs_dir, dirname) == 0)) {
20157c478bd9Sstevel@tonic-gate 				if (match == NULL)
20167c478bd9Sstevel@tonic-gate 					match = cur;
20177c478bd9Sstevel@tonic-gate 				else
20187c478bd9Sstevel@tonic-gate 					return (Z_INSUFFICIENT_SPEC);
20197c478bd9Sstevel@tonic-gate 			}
20207c478bd9Sstevel@tonic-gate 		}
20217c478bd9Sstevel@tonic-gate 	}
20227c478bd9Sstevel@tonic-gate 
20237c478bd9Sstevel@tonic-gate 	if (match == NULL)
20247c478bd9Sstevel@tonic-gate 		return (Z_NO_RESOURCE_ID);
20257c478bd9Sstevel@tonic-gate 
20267c478bd9Sstevel@tonic-gate 	cur = match;
20277c478bd9Sstevel@tonic-gate 
20287c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_DIR, tabptr->zone_fs_dir,
20297c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_fs_dir))) != Z_OK)
20307c478bd9Sstevel@tonic-gate 		return (err);
20317c478bd9Sstevel@tonic-gate 
20327c478bd9Sstevel@tonic-gate 	return (Z_OK);
20337c478bd9Sstevel@tonic-gate }
20347c478bd9Sstevel@tonic-gate 
20357c478bd9Sstevel@tonic-gate /*
20367c478bd9Sstevel@tonic-gate  * Compare two IP addresses in string form.  Allow for the possibility that
20377c478bd9Sstevel@tonic-gate  * one might have "/<prefix-length>" at the end: allow a match on just the
20387c478bd9Sstevel@tonic-gate  * IP address (or host name) part.
20397c478bd9Sstevel@tonic-gate  */
20407c478bd9Sstevel@tonic-gate 
20417c478bd9Sstevel@tonic-gate boolean_t
20427c478bd9Sstevel@tonic-gate zonecfg_same_net_address(char *a1, char *a2)
20437c478bd9Sstevel@tonic-gate {
20447c478bd9Sstevel@tonic-gate 	char *slashp, *slashp1, *slashp2;
20457c478bd9Sstevel@tonic-gate 	int result;
20467c478bd9Sstevel@tonic-gate 
20477c478bd9Sstevel@tonic-gate 	if (strcmp(a1, a2) == 0)
20487c478bd9Sstevel@tonic-gate 		return (B_TRUE);
20497c478bd9Sstevel@tonic-gate 
20507c478bd9Sstevel@tonic-gate 	/*
20517c478bd9Sstevel@tonic-gate 	 * If neither has a slash or both do, they need to match to be
20527c478bd9Sstevel@tonic-gate 	 * considered the same, but they did not match above, so fail.
20537c478bd9Sstevel@tonic-gate 	 */
20547c478bd9Sstevel@tonic-gate 	slashp1 = strchr(a1, '/');
20557c478bd9Sstevel@tonic-gate 	slashp2 = strchr(a2, '/');
20567c478bd9Sstevel@tonic-gate 	if ((slashp1 == NULL && slashp2 == NULL) ||
20577c478bd9Sstevel@tonic-gate 	    (slashp1 != NULL && slashp2 != NULL))
20587c478bd9Sstevel@tonic-gate 		return (B_FALSE);
20597c478bd9Sstevel@tonic-gate 
20607c478bd9Sstevel@tonic-gate 	/*
20617c478bd9Sstevel@tonic-gate 	 * Only one had a slash: pick that one, zero out the slash, compare
20627c478bd9Sstevel@tonic-gate 	 * the "address only" strings, restore the slash, and return the
20637c478bd9Sstevel@tonic-gate 	 * result of the comparison.
20647c478bd9Sstevel@tonic-gate 	 */
20657c478bd9Sstevel@tonic-gate 	slashp = (slashp1 == NULL) ? slashp2 : slashp1;
20667c478bd9Sstevel@tonic-gate 	*slashp = '\0';
20677c478bd9Sstevel@tonic-gate 	result = strcmp(a1, a2);
20687c478bd9Sstevel@tonic-gate 	*slashp = '/';
20697c478bd9Sstevel@tonic-gate 	return ((result == 0));
20707c478bd9Sstevel@tonic-gate }
20717c478bd9Sstevel@tonic-gate 
20727c478bd9Sstevel@tonic-gate int
20737c478bd9Sstevel@tonic-gate zonecfg_valid_net_address(char *address, struct lifreq *lifr)
20747c478bd9Sstevel@tonic-gate {
20757c478bd9Sstevel@tonic-gate 	struct sockaddr_in *sin4;
20767c478bd9Sstevel@tonic-gate 	struct sockaddr_in6 *sin6;
20777c478bd9Sstevel@tonic-gate 	struct addrinfo hints, *result;
20787c478bd9Sstevel@tonic-gate 	char *slashp = strchr(address, '/');
20797c478bd9Sstevel@tonic-gate 
20807c478bd9Sstevel@tonic-gate 	bzero(lifr, sizeof (struct lifreq));
20817c478bd9Sstevel@tonic-gate 	sin4 = (struct sockaddr_in *)&lifr->lifr_addr;
20827c478bd9Sstevel@tonic-gate 	sin6 = (struct sockaddr_in6 *)&lifr->lifr_addr;
20837c478bd9Sstevel@tonic-gate 	if (slashp != NULL)
20847c478bd9Sstevel@tonic-gate 		*slashp = '\0';
20857c478bd9Sstevel@tonic-gate 	if (inet_pton(AF_INET, address, &sin4->sin_addr) == 1) {
20867c478bd9Sstevel@tonic-gate 		sin4->sin_family = AF_INET;
20877c478bd9Sstevel@tonic-gate 	} else if (inet_pton(AF_INET6, address, &sin6->sin6_addr) == 1) {
20887c478bd9Sstevel@tonic-gate 		if (slashp == NULL)
20897c478bd9Sstevel@tonic-gate 			return (Z_IPV6_ADDR_PREFIX_LEN);
20907c478bd9Sstevel@tonic-gate 		sin6->sin6_family = AF_INET6;
20917c478bd9Sstevel@tonic-gate 	} else {
20927c478bd9Sstevel@tonic-gate 		/* "address" may be a host name */
20937c478bd9Sstevel@tonic-gate 		(void) memset(&hints, 0, sizeof (hints));
20947c478bd9Sstevel@tonic-gate 		hints.ai_family = PF_INET;
20957c478bd9Sstevel@tonic-gate 		if (getaddrinfo(address, NULL, &hints, &result) != 0)
20967c478bd9Sstevel@tonic-gate 			return (Z_BOGUS_ADDRESS);
20977c478bd9Sstevel@tonic-gate 		sin4->sin_family = result->ai_family;
2098a1be23daSdp 
20997c478bd9Sstevel@tonic-gate 		(void) memcpy(&sin4->sin_addr,
21007c478bd9Sstevel@tonic-gate 		    /* LINTED E_BAD_PTR_CAST_ALIGN */
21017c478bd9Sstevel@tonic-gate 		    &((struct sockaddr_in *)result->ai_addr)->sin_addr,
21027c478bd9Sstevel@tonic-gate 		    sizeof (struct in_addr));
2103a1be23daSdp 
21047c478bd9Sstevel@tonic-gate 		freeaddrinfo(result);
21057c478bd9Sstevel@tonic-gate 	}
21067c478bd9Sstevel@tonic-gate 	return (Z_OK);
21077c478bd9Sstevel@tonic-gate }
21087c478bd9Sstevel@tonic-gate 
2109f4b3ec61Sdh155122 boolean_t
2110f4b3ec61Sdh155122 zonecfg_ifname_exists(sa_family_t af, char *ifname)
2111f4b3ec61Sdh155122 {
2112f4b3ec61Sdh155122 	struct lifreq lifr;
2113f4b3ec61Sdh155122 	int so;
2114f4b3ec61Sdh155122 	int save_errno;
2115f4b3ec61Sdh155122 
2116f4b3ec61Sdh155122 	(void) memset(&lifr, 0, sizeof (lifr));
2117f4b3ec61Sdh155122 	(void) strlcpy(lifr.lifr_name, ifname, sizeof (lifr.lifr_name));
2118f4b3ec61Sdh155122 	lifr.lifr_addr.ss_family = af;
2119f4b3ec61Sdh155122 	if ((so = socket(af, SOCK_DGRAM, 0)) < 0) {
2120f4b3ec61Sdh155122 		/* Odd - can't tell if the ifname exists */
2121f4b3ec61Sdh155122 		return (B_FALSE);
2122f4b3ec61Sdh155122 	}
2123f4b3ec61Sdh155122 	if (ioctl(so, SIOCGLIFFLAGS, (caddr_t)&lifr) < 0) {
2124f4b3ec61Sdh155122 		save_errno = errno;
2125f4b3ec61Sdh155122 		(void) close(so);
2126f4b3ec61Sdh155122 		errno = save_errno;
2127f4b3ec61Sdh155122 		return (B_FALSE);
2128f4b3ec61Sdh155122 	}
2129f4b3ec61Sdh155122 	(void) close(so);
2130f4b3ec61Sdh155122 	return (B_TRUE);
2131f4b3ec61Sdh155122 }
2132f4b3ec61Sdh155122 
2133a8053275Sjv227347 /*
2134a8053275Sjv227347  * Determines if the physical interface and IP address specified by 'tabptr'
2135a8053275Sjv227347  * are in the zone document to which 'handle' refers.  'tabptr' must have an
2136a8053275Sjv227347  * interface or an address or both.  If it contains both, then Z_OK is
2137a8053275Sjv227347  * returned iff there is exactly one match.  If it contains an interface
2138a8053275Sjv227347  * or an address, but not both, then Z_OK is returned iff there is exactly
2139a8053275Sjv227347  * one entry with that interface or address.  If there are multiple entries
2140a8053275Sjv227347  * matching the query, then Z_INSUFFICIENT_SPEC is returned.  If there
2141a8053275Sjv227347  * are no matches, then Z_NO_RESOURCE_ID is returned.
2142a8053275Sjv227347  *
2143a8053275Sjv227347  * Errors might also be returned if the entry that exactly matches the
2144a8053275Sjv227347  * query lacks critical network resource information.
2145a8053275Sjv227347  *
2146a8053275Sjv227347  * If there is a single exact match, then the matching entry's physical
2147a8053275Sjv227347  * interface, IP address, and router information is stored in 'tabptr'.
2148a8053275Sjv227347  */
21497c478bd9Sstevel@tonic-gate int
21507c478bd9Sstevel@tonic-gate zonecfg_lookup_nwif(zone_dochandle_t handle, struct zone_nwiftab *tabptr)
21517c478bd9Sstevel@tonic-gate {
2152a8053275Sjv227347 	xmlNodePtr cur;
2153a8053275Sjv227347 	xmlNodePtr firstmatch;
2154a8053275Sjv227347 	boolean_t physfound;
21557c478bd9Sstevel@tonic-gate 	int err;
2156a8053275Sjv227347 	char address[INET6_ADDRSTRLEN];
2157a8053275Sjv227347 	char physical[LIFNAMSIZ];
2158a8053275Sjv227347 	size_t addrspec;		/* nonzero if tabptr has IP addr */
2159a8053275Sjv227347 	size_t physspec;		/* nonzero if tabptr has interface */
21607c478bd9Sstevel@tonic-gate 
21617c478bd9Sstevel@tonic-gate 	if (tabptr == NULL)
21627c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
21637c478bd9Sstevel@tonic-gate 
2164a8053275Sjv227347 	/*
2165a8053275Sjv227347 	 * zone_nwif_address and zone_nwif_physical are arrays, so no NULL
2166a8053275Sjv227347 	 * checks are necessary.
2167a8053275Sjv227347 	 */
2168a8053275Sjv227347 	addrspec = strlen(tabptr->zone_nwif_address);
2169a8053275Sjv227347 	physspec = strlen(tabptr->zone_nwif_physical);
2170a8053275Sjv227347 	assert(addrspec > 0 || physspec > 0);
2171a8053275Sjv227347 
21727c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
21737c478bd9Sstevel@tonic-gate 		return (err);
21747c478bd9Sstevel@tonic-gate 
21757c478bd9Sstevel@tonic-gate 	firstmatch = NULL;
2176a8053275Sjv227347 	cur = handle->zone_dh_cur;
21777c478bd9Sstevel@tonic-gate 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
2178a8053275Sjv227347 		/* Skip non-net elements */
21797c478bd9Sstevel@tonic-gate 		if (xmlStrcmp(cur->name, DTD_ELEM_NET))
21807c478bd9Sstevel@tonic-gate 			continue;
2181a8053275Sjv227347 
2182a8053275Sjv227347 		/*
2183a8053275Sjv227347 		 * If an interface is specified, then first check if the current
2184a8053275Sjv227347 		 * element's interface matches the query's interface.
2185a8053275Sjv227347 		 */
2186a8053275Sjv227347 		if (physspec > 0) {
2187a8053275Sjv227347 			physfound = B_FALSE;
21887c478bd9Sstevel@tonic-gate 			if ((fetchprop(cur, DTD_ATTR_PHYSICAL, physical,
21897c478bd9Sstevel@tonic-gate 			    sizeof (physical)) == Z_OK) &&
21907c478bd9Sstevel@tonic-gate 			    (strcmp(tabptr->zone_nwif_physical,
21917c478bd9Sstevel@tonic-gate 			    physical) == 0)) {
2192a8053275Sjv227347 				if (addrspec == 0) {
21937c478bd9Sstevel@tonic-gate 					if (firstmatch == NULL)
21947c478bd9Sstevel@tonic-gate 						firstmatch = cur;
21957c478bd9Sstevel@tonic-gate 					else
21967c478bd9Sstevel@tonic-gate 						return (Z_INSUFFICIENT_SPEC);
21977c478bd9Sstevel@tonic-gate 				} else {
21987c478bd9Sstevel@tonic-gate 					/*
2199a8053275Sjv227347 					 * We're also matching based on IP
2200a8053275Sjv227347 					 * address, so we can't say that the
2201a8053275Sjv227347 					 * current element matches the query
2202a8053275Sjv227347 					 * yet.  Indicate that the interfaces
2203a8053275Sjv227347 					 * match.
22047c478bd9Sstevel@tonic-gate 					 */
2205a8053275Sjv227347 					physfound = B_TRUE;
2206a8053275Sjv227347 				}
2207a8053275Sjv227347 			}
2208a8053275Sjv227347 		}
2209a8053275Sjv227347 		if (addrspec > 0) {
2210a8053275Sjv227347 			if ((fetchprop(cur, DTD_ATTR_ADDRESS, address,
2211a8053275Sjv227347 			    sizeof (address)) == Z_OK) &&
2212a8053275Sjv227347 			    (zonecfg_same_net_address(
2213a8053275Sjv227347 			    tabptr->zone_nwif_address, address))) {
2214a8053275Sjv227347 				if (physspec == 0) {
2215a8053275Sjv227347 					/* We're only matching IP addresses. */
2216a8053275Sjv227347 					if (firstmatch == NULL)
2217a8053275Sjv227347 						firstmatch = cur;
2218a8053275Sjv227347 					else
2219a8053275Sjv227347 						return (Z_INSUFFICIENT_SPEC);
2220a8053275Sjv227347 				} else if (physfound) {
2221a8053275Sjv227347 					/*
2222a8053275Sjv227347 					 * Both the interfaces and the addresses
2223a8053275Sjv227347 					 * match.
2224a8053275Sjv227347 					 */
2225a8053275Sjv227347 					if (firstmatch == NULL)
2226a8053275Sjv227347 						firstmatch = cur;
2227a8053275Sjv227347 					else
2228a8053275Sjv227347 						return (Z_INSUFFICIENT_SPEC);
22297c478bd9Sstevel@tonic-gate 				}
22307c478bd9Sstevel@tonic-gate 			}
22317c478bd9Sstevel@tonic-gate 		}
22327c478bd9Sstevel@tonic-gate 	}
22337c478bd9Sstevel@tonic-gate 	if (firstmatch == NULL)
22347c478bd9Sstevel@tonic-gate 		return (Z_NO_RESOURCE_ID);
22357c478bd9Sstevel@tonic-gate 
22367c478bd9Sstevel@tonic-gate 	cur = firstmatch;
22377c478bd9Sstevel@tonic-gate 
22387c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_PHYSICAL, tabptr->zone_nwif_physical,
22397c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_nwif_physical))) != Z_OK)
22407c478bd9Sstevel@tonic-gate 		return (err);
22417c478bd9Sstevel@tonic-gate 
22427c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_ADDRESS, tabptr->zone_nwif_address,
22437c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_nwif_address))) != Z_OK)
22447c478bd9Sstevel@tonic-gate 		return (err);
22457c478bd9Sstevel@tonic-gate 
2246de860bd9Sgfaden 	if ((err = fetchprop(cur, DTD_ATTR_DEFROUTER,
2247de860bd9Sgfaden 	    tabptr->zone_nwif_defrouter,
2248de860bd9Sgfaden 	    sizeof (tabptr->zone_nwif_defrouter))) != Z_OK)
2249de860bd9Sgfaden 		return (err);
2250de860bd9Sgfaden 
22517c478bd9Sstevel@tonic-gate 	return (Z_OK);
22527c478bd9Sstevel@tonic-gate }
22537c478bd9Sstevel@tonic-gate 
22547c478bd9Sstevel@tonic-gate static int
22557c478bd9Sstevel@tonic-gate zonecfg_add_nwif_core(zone_dochandle_t handle, struct zone_nwiftab *tabptr)
22567c478bd9Sstevel@tonic-gate {
22577c478bd9Sstevel@tonic-gate 	xmlNodePtr newnode, cur = handle->zone_dh_cur;
22587c478bd9Sstevel@tonic-gate 	int err;
22597c478bd9Sstevel@tonic-gate 
22607c478bd9Sstevel@tonic-gate 	newnode = xmlNewTextChild(cur, NULL, DTD_ELEM_NET, NULL);
2261a1be23daSdp 	if ((err = newprop(newnode, DTD_ATTR_ADDRESS,
22627c478bd9Sstevel@tonic-gate 	    tabptr->zone_nwif_address)) != Z_OK)
22637c478bd9Sstevel@tonic-gate 		return (err);
2264a1be23daSdp 	if ((err = newprop(newnode, DTD_ATTR_PHYSICAL,
22657c478bd9Sstevel@tonic-gate 	    tabptr->zone_nwif_physical)) != Z_OK)
22667c478bd9Sstevel@tonic-gate 		return (err);
226744ce818bSjparcel 	/*
226844ce818bSjparcel 	 * Do not add this property when it is not set, for backwards
226944ce818bSjparcel 	 * compatibility and because it is optional.
227044ce818bSjparcel 	 */
227144ce818bSjparcel 	if ((strlen(tabptr->zone_nwif_defrouter) > 0) &&
227244ce818bSjparcel 	    ((err = newprop(newnode, DTD_ATTR_DEFROUTER,
227344ce818bSjparcel 	    tabptr->zone_nwif_defrouter)) != Z_OK))
2274de860bd9Sgfaden 		return (err);
22757c478bd9Sstevel@tonic-gate 	return (Z_OK);
22767c478bd9Sstevel@tonic-gate }
22777c478bd9Sstevel@tonic-gate 
22787c478bd9Sstevel@tonic-gate int
22797c478bd9Sstevel@tonic-gate zonecfg_add_nwif(zone_dochandle_t handle, struct zone_nwiftab *tabptr)
22807c478bd9Sstevel@tonic-gate {
22817c478bd9Sstevel@tonic-gate 	int err;
22827c478bd9Sstevel@tonic-gate 
22837c478bd9Sstevel@tonic-gate 	if (tabptr == NULL)
22847c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
22857c478bd9Sstevel@tonic-gate 
22867c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
22877c478bd9Sstevel@tonic-gate 		return (err);
22887c478bd9Sstevel@tonic-gate 
22897c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_add_nwif_core(handle, tabptr)) != Z_OK)
22907c478bd9Sstevel@tonic-gate 		return (err);
22917c478bd9Sstevel@tonic-gate 
22927c478bd9Sstevel@tonic-gate 	return (Z_OK);
22937c478bd9Sstevel@tonic-gate }
22947c478bd9Sstevel@tonic-gate 
22957c478bd9Sstevel@tonic-gate static int
22967c478bd9Sstevel@tonic-gate zonecfg_delete_nwif_core(zone_dochandle_t handle, struct zone_nwiftab *tabptr)
22977c478bd9Sstevel@tonic-gate {
22987c478bd9Sstevel@tonic-gate 	xmlNodePtr cur = handle->zone_dh_cur;
22997c478bd9Sstevel@tonic-gate 	boolean_t addr_match, phys_match;
23007c478bd9Sstevel@tonic-gate 
23017c478bd9Sstevel@tonic-gate 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
23027c478bd9Sstevel@tonic-gate 		if (xmlStrcmp(cur->name, DTD_ELEM_NET))
23037c478bd9Sstevel@tonic-gate 			continue;
23047c478bd9Sstevel@tonic-gate 
23057c478bd9Sstevel@tonic-gate 		addr_match = match_prop(cur, DTD_ATTR_ADDRESS,
23067c478bd9Sstevel@tonic-gate 		    tabptr->zone_nwif_address);
23077c478bd9Sstevel@tonic-gate 		phys_match = match_prop(cur, DTD_ATTR_PHYSICAL,
23087c478bd9Sstevel@tonic-gate 		    tabptr->zone_nwif_physical);
23097c478bd9Sstevel@tonic-gate 
23107c478bd9Sstevel@tonic-gate 		if (addr_match && phys_match) {
23117c478bd9Sstevel@tonic-gate 			xmlUnlinkNode(cur);
23127c478bd9Sstevel@tonic-gate 			xmlFreeNode(cur);
23137c478bd9Sstevel@tonic-gate 			return (Z_OK);
23147c478bd9Sstevel@tonic-gate 		}
23157c478bd9Sstevel@tonic-gate 	}
23167c478bd9Sstevel@tonic-gate 	return (Z_NO_RESOURCE_ID);
23177c478bd9Sstevel@tonic-gate }
23187c478bd9Sstevel@tonic-gate 
23197c478bd9Sstevel@tonic-gate int
23207c478bd9Sstevel@tonic-gate zonecfg_delete_nwif(zone_dochandle_t handle, struct zone_nwiftab *tabptr)
23217c478bd9Sstevel@tonic-gate {
23227c478bd9Sstevel@tonic-gate 	int err;
23237c478bd9Sstevel@tonic-gate 
23247c478bd9Sstevel@tonic-gate 	if (tabptr == NULL)
23257c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
23267c478bd9Sstevel@tonic-gate 
23277c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
23287c478bd9Sstevel@tonic-gate 		return (err);
23297c478bd9Sstevel@tonic-gate 
23307c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_delete_nwif_core(handle, tabptr)) != Z_OK)
23317c478bd9Sstevel@tonic-gate 		return (err);
23327c478bd9Sstevel@tonic-gate 
23337c478bd9Sstevel@tonic-gate 	return (Z_OK);
23347c478bd9Sstevel@tonic-gate }
23357c478bd9Sstevel@tonic-gate 
23367c478bd9Sstevel@tonic-gate int
23377c478bd9Sstevel@tonic-gate zonecfg_modify_nwif(
23387c478bd9Sstevel@tonic-gate 	zone_dochandle_t handle,
23397c478bd9Sstevel@tonic-gate 	struct zone_nwiftab *oldtabptr,
23407c478bd9Sstevel@tonic-gate 	struct zone_nwiftab *newtabptr)
23417c478bd9Sstevel@tonic-gate {
23427c478bd9Sstevel@tonic-gate 	int err;
23437c478bd9Sstevel@tonic-gate 
23447c478bd9Sstevel@tonic-gate 	if (oldtabptr == NULL || newtabptr == NULL)
23457c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
23467c478bd9Sstevel@tonic-gate 
23477c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
23487c478bd9Sstevel@tonic-gate 		return (err);
23497c478bd9Sstevel@tonic-gate 
23507c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_delete_nwif_core(handle, oldtabptr)) != Z_OK)
23517c478bd9Sstevel@tonic-gate 		return (err);
23527c478bd9Sstevel@tonic-gate 
23537c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_add_nwif_core(handle, newtabptr)) != Z_OK)
23547c478bd9Sstevel@tonic-gate 		return (err);
23557c478bd9Sstevel@tonic-gate 
23567c478bd9Sstevel@tonic-gate 	return (Z_OK);
23577c478bd9Sstevel@tonic-gate }
23587c478bd9Sstevel@tonic-gate 
23595679c89fSjv227347 /*
23605679c89fSjv227347  * Gets the zone hostid string stored in the specified zone configuration
23615679c89fSjv227347  * document.  This function returns Z_OK on success.  Z_BAD_PROPERTY is returned
23625679c89fSjv227347  * if the config file doesn't specify a hostid or if the hostid is blank.
23635679c89fSjv227347  *
23645679c89fSjv227347  * Note that buflen should be at least HW_HOSTID_LEN.
23655679c89fSjv227347  */
23665679c89fSjv227347 int
23675679c89fSjv227347 zonecfg_get_hostid(zone_dochandle_t handle, char *bufp, size_t buflen)
23685679c89fSjv227347 {
23695679c89fSjv227347 	int err;
23705679c89fSjv227347 
23715679c89fSjv227347 	if ((err = getrootattr(handle, DTD_ATTR_HOSTID, bufp, buflen)) != Z_OK)
23725679c89fSjv227347 		return (err);
23735679c89fSjv227347 	if (bufp[0] == '\0')
23745679c89fSjv227347 		return (Z_BAD_PROPERTY);
23755679c89fSjv227347 	return (Z_OK);
23765679c89fSjv227347 }
23775679c89fSjv227347 
23785679c89fSjv227347 /*
23795679c89fSjv227347  * Sets the hostid string in the specified zone config document to the given
23805679c89fSjv227347  * string value.  If 'hostidp' is NULL, then the config document's hostid
23815679c89fSjv227347  * attribute is cleared.  Non-NULL hostids are validated.  This function returns
23825679c89fSjv227347  * Z_OK on success.  Any other return value indicates failure.
23835679c89fSjv227347  */
23845679c89fSjv227347 int
23855679c89fSjv227347 zonecfg_set_hostid(zone_dochandle_t handle, const char *hostidp)
23865679c89fSjv227347 {
23875679c89fSjv227347 	int err;
23885679c89fSjv227347 
23895679c89fSjv227347 	/*
23905679c89fSjv227347 	 * A NULL hostid string is interpreted as a request to clear the
23915679c89fSjv227347 	 * hostid.
23925679c89fSjv227347 	 */
23935679c89fSjv227347 	if (hostidp == NULL || (err = zonecfg_valid_hostid(hostidp)) == Z_OK)
23945679c89fSjv227347 		return (setrootattr(handle, DTD_ATTR_HOSTID, hostidp));
23955679c89fSjv227347 	return (err);
23965679c89fSjv227347 }
23975679c89fSjv227347 
23985679c89fSjv227347 /*
23995679c89fSjv227347  * Determines if the specified string is a valid hostid string.  This function
24005679c89fSjv227347  * returns Z_OK if the string is a valid hostid string.  It returns Z_INVAL if
24015679c89fSjv227347  * 'hostidp' is NULL, Z_TOO_BIG if 'hostidp' refers to a string buffer
24025679c89fSjv227347  * containing a hex string with more than 8 digits, and Z_HOSTID_FUBAR if the
24035679c89fSjv227347  * string has an invalid format.
24045679c89fSjv227347  */
24055679c89fSjv227347 int
24065679c89fSjv227347 zonecfg_valid_hostid(const char *hostidp)
24075679c89fSjv227347 {
24085679c89fSjv227347 	char *currentp;
24095679c89fSjv227347 	u_longlong_t hostidval;
24105679c89fSjv227347 	size_t len;
24115679c89fSjv227347 
24125679c89fSjv227347 	if (hostidp == NULL)
24135679c89fSjv227347 		return (Z_INVAL);
24145679c89fSjv227347 
24155679c89fSjv227347 	/* Empty strings and strings with whitespace are invalid. */
24165679c89fSjv227347 	if (*hostidp == '\0')
24175679c89fSjv227347 		return (Z_HOSTID_FUBAR);
24185679c89fSjv227347 	for (currentp = (char *)hostidp; *currentp != '\0'; ++currentp) {
24195679c89fSjv227347 		if (isspace(*currentp))
24205679c89fSjv227347 			return (Z_HOSTID_FUBAR);
24215679c89fSjv227347 	}
24225679c89fSjv227347 	len = (size_t)(currentp - hostidp);
24235679c89fSjv227347 
24245679c89fSjv227347 	/*
24255679c89fSjv227347 	 * The caller might pass a hostid that is larger than the maximum
24265679c89fSjv227347 	 * unsigned 32-bit integral value.  Check for this!  Also, make sure
24275679c89fSjv227347 	 * that the whole string is converted (this helps us find illegal
24285679c89fSjv227347 	 * characters) and that the whole string fits within a buffer of size
24295679c89fSjv227347 	 * HW_HOSTID_LEN.
24305679c89fSjv227347 	 */
24315679c89fSjv227347 	currentp = (char *)hostidp;
24325679c89fSjv227347 	if (strncmp(hostidp, "0x", 2) == 0 || strncmp(hostidp, "0X", 2) == 0)
24335679c89fSjv227347 		currentp += 2;
24345679c89fSjv227347 	hostidval = strtoull(currentp, &currentp, 16);
24355679c89fSjv227347 	if ((size_t)(currentp - hostidp) >= HW_HOSTID_LEN)
24365679c89fSjv227347 		return (Z_TOO_BIG);
24375679c89fSjv227347 	if (hostidval > UINT_MAX || hostidval == HW_INVALID_HOSTID ||
24385679c89fSjv227347 	    currentp != hostidp + len)
24395679c89fSjv227347 		return (Z_HOSTID_FUBAR);
24405679c89fSjv227347 	return (Z_OK);
24415679c89fSjv227347 }
24425679c89fSjv227347 
24437c478bd9Sstevel@tonic-gate int
24447c478bd9Sstevel@tonic-gate zonecfg_lookup_dev(zone_dochandle_t handle, struct zone_devtab *tabptr)
24457c478bd9Sstevel@tonic-gate {
24467c478bd9Sstevel@tonic-gate 	xmlNodePtr cur, firstmatch;
24477c478bd9Sstevel@tonic-gate 	int err;
24487c478bd9Sstevel@tonic-gate 	char match[MAXPATHLEN];
24497c478bd9Sstevel@tonic-gate 
24507c478bd9Sstevel@tonic-gate 	if (tabptr == NULL)
24517c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
24527c478bd9Sstevel@tonic-gate 
24537c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
24547c478bd9Sstevel@tonic-gate 		return (err);
24557c478bd9Sstevel@tonic-gate 
24567c478bd9Sstevel@tonic-gate 	cur = handle->zone_dh_cur;
24577c478bd9Sstevel@tonic-gate 	firstmatch = NULL;
24587c478bd9Sstevel@tonic-gate 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
24597c478bd9Sstevel@tonic-gate 		if (xmlStrcmp(cur->name, DTD_ELEM_DEVICE))
24607c478bd9Sstevel@tonic-gate 			continue;
24617c478bd9Sstevel@tonic-gate 		if (strlen(tabptr->zone_dev_match) == 0)
24627c478bd9Sstevel@tonic-gate 			continue;
24637c478bd9Sstevel@tonic-gate 
24647c478bd9Sstevel@tonic-gate 		if ((fetchprop(cur, DTD_ATTR_MATCH, match,
24657c478bd9Sstevel@tonic-gate 		    sizeof (match)) == Z_OK)) {
24667c478bd9Sstevel@tonic-gate 			if (strcmp(tabptr->zone_dev_match,
24677c478bd9Sstevel@tonic-gate 			    match) == 0) {
24687c478bd9Sstevel@tonic-gate 				if (firstmatch == NULL)
24697c478bd9Sstevel@tonic-gate 					firstmatch = cur;
24707c478bd9Sstevel@tonic-gate 				else if (firstmatch != cur)
24717c478bd9Sstevel@tonic-gate 					return (Z_INSUFFICIENT_SPEC);
24727c478bd9Sstevel@tonic-gate 			} else {
24737c478bd9Sstevel@tonic-gate 				/*
24747c478bd9Sstevel@tonic-gate 				 * If another property matched but this
24757c478bd9Sstevel@tonic-gate 				 * one doesn't then reset firstmatch.
24767c478bd9Sstevel@tonic-gate 				 */
24777c478bd9Sstevel@tonic-gate 				if (firstmatch == cur)
24787c478bd9Sstevel@tonic-gate 					firstmatch = NULL;
24797c478bd9Sstevel@tonic-gate 			}
24807c478bd9Sstevel@tonic-gate 		}
24817c478bd9Sstevel@tonic-gate 	}
24827c478bd9Sstevel@tonic-gate 	if (firstmatch == NULL)
24837c478bd9Sstevel@tonic-gate 		return (Z_NO_RESOURCE_ID);
24847c478bd9Sstevel@tonic-gate 
24857c478bd9Sstevel@tonic-gate 	cur = firstmatch;
24867c478bd9Sstevel@tonic-gate 
24877c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_MATCH, tabptr->zone_dev_match,
24887c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_dev_match))) != Z_OK)
24897c478bd9Sstevel@tonic-gate 		return (err);
24907c478bd9Sstevel@tonic-gate 
24917c478bd9Sstevel@tonic-gate 	return (Z_OK);
24927c478bd9Sstevel@tonic-gate }
24937c478bd9Sstevel@tonic-gate 
24947c478bd9Sstevel@tonic-gate static int
24957c478bd9Sstevel@tonic-gate zonecfg_add_dev_core(zone_dochandle_t handle, struct zone_devtab *tabptr)
24967c478bd9Sstevel@tonic-gate {
24977c478bd9Sstevel@tonic-gate 	xmlNodePtr newnode, cur = handle->zone_dh_cur;
24987c478bd9Sstevel@tonic-gate 	int err;
24997c478bd9Sstevel@tonic-gate 
25007c478bd9Sstevel@tonic-gate 	newnode = xmlNewTextChild(cur, NULL, DTD_ELEM_DEVICE, NULL);
25017c478bd9Sstevel@tonic-gate 
2502a1be23daSdp 	if ((err = newprop(newnode, DTD_ATTR_MATCH,
25037c478bd9Sstevel@tonic-gate 	    tabptr->zone_dev_match)) != Z_OK)
25047c478bd9Sstevel@tonic-gate 		return (err);
25057c478bd9Sstevel@tonic-gate 
25067c478bd9Sstevel@tonic-gate 	return (Z_OK);
25077c478bd9Sstevel@tonic-gate }
25087c478bd9Sstevel@tonic-gate 
25097c478bd9Sstevel@tonic-gate int
25107c478bd9Sstevel@tonic-gate zonecfg_add_dev(zone_dochandle_t handle, struct zone_devtab *tabptr)
25117c478bd9Sstevel@tonic-gate {
25127c478bd9Sstevel@tonic-gate 	int err;
25137c478bd9Sstevel@tonic-gate 
25147c478bd9Sstevel@tonic-gate 	if (tabptr == NULL)
25157c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
25167c478bd9Sstevel@tonic-gate 
25177c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
25187c478bd9Sstevel@tonic-gate 		return (err);
25197c478bd9Sstevel@tonic-gate 
25207c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_add_dev_core(handle, tabptr)) != Z_OK)
25217c478bd9Sstevel@tonic-gate 		return (err);
25227c478bd9Sstevel@tonic-gate 
25237c478bd9Sstevel@tonic-gate 	return (Z_OK);
25247c478bd9Sstevel@tonic-gate }
25257c478bd9Sstevel@tonic-gate 
25267c478bd9Sstevel@tonic-gate static int
25277c478bd9Sstevel@tonic-gate zonecfg_delete_dev_core(zone_dochandle_t handle, struct zone_devtab *tabptr)
25287c478bd9Sstevel@tonic-gate {
2529facf4a8dSllai1 	xmlNodePtr cur = handle->zone_dh_cur;
25307c478bd9Sstevel@tonic-gate 	int match_match;
25317c478bd9Sstevel@tonic-gate 
25327c478bd9Sstevel@tonic-gate 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
25337c478bd9Sstevel@tonic-gate 		if (xmlStrcmp(cur->name, DTD_ELEM_DEVICE))
25347c478bd9Sstevel@tonic-gate 			continue;
25357c478bd9Sstevel@tonic-gate 
25367c478bd9Sstevel@tonic-gate 		match_match = match_prop(cur, DTD_ATTR_MATCH,
25377c478bd9Sstevel@tonic-gate 		    tabptr->zone_dev_match);
25387c478bd9Sstevel@tonic-gate 
25397c478bd9Sstevel@tonic-gate 		if (match_match) {
25407c478bd9Sstevel@tonic-gate 			xmlUnlinkNode(cur);
25417c478bd9Sstevel@tonic-gate 			xmlFreeNode(cur);
25427c478bd9Sstevel@tonic-gate 			return (Z_OK);
25437c478bd9Sstevel@tonic-gate 		}
25447c478bd9Sstevel@tonic-gate 	}
25457c478bd9Sstevel@tonic-gate 	return (Z_NO_RESOURCE_ID);
25467c478bd9Sstevel@tonic-gate }
25477c478bd9Sstevel@tonic-gate 
25487c478bd9Sstevel@tonic-gate int
25497c478bd9Sstevel@tonic-gate zonecfg_delete_dev(zone_dochandle_t handle, struct zone_devtab *tabptr)
25507c478bd9Sstevel@tonic-gate {
25517c478bd9Sstevel@tonic-gate 	int err;
25527c478bd9Sstevel@tonic-gate 
25537c478bd9Sstevel@tonic-gate 	if (tabptr == NULL)
25547c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
25557c478bd9Sstevel@tonic-gate 
25567c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
25577c478bd9Sstevel@tonic-gate 		return (err);
25587c478bd9Sstevel@tonic-gate 
25597c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_delete_dev_core(handle, tabptr)) != Z_OK)
25607c478bd9Sstevel@tonic-gate 		return (err);
25617c478bd9Sstevel@tonic-gate 
25627c478bd9Sstevel@tonic-gate 	return (Z_OK);
25637c478bd9Sstevel@tonic-gate }
25647c478bd9Sstevel@tonic-gate 
25657c478bd9Sstevel@tonic-gate int
25667c478bd9Sstevel@tonic-gate zonecfg_modify_dev(
25677c478bd9Sstevel@tonic-gate 	zone_dochandle_t handle,
25687c478bd9Sstevel@tonic-gate 	struct zone_devtab *oldtabptr,
25697c478bd9Sstevel@tonic-gate 	struct zone_devtab *newtabptr)
25707c478bd9Sstevel@tonic-gate {
25717c478bd9Sstevel@tonic-gate 	int err;
25727c478bd9Sstevel@tonic-gate 
25737c478bd9Sstevel@tonic-gate 	if (oldtabptr == NULL || newtabptr == NULL)
25747c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
25757c478bd9Sstevel@tonic-gate 
25767c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
25777c478bd9Sstevel@tonic-gate 		return (err);
25787c478bd9Sstevel@tonic-gate 
25797c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_delete_dev_core(handle, oldtabptr)) != Z_OK)
25807c478bd9Sstevel@tonic-gate 		return (err);
25817c478bd9Sstevel@tonic-gate 
25827c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_add_dev_core(handle, newtabptr)) != Z_OK)
25837c478bd9Sstevel@tonic-gate 		return (err);
25847c478bd9Sstevel@tonic-gate 
25857c478bd9Sstevel@tonic-gate 	return (Z_OK);
25867c478bd9Sstevel@tonic-gate }
25877c478bd9Sstevel@tonic-gate 
2588ff17c8bfSgjelinek /* Lock to serialize all devwalks */
2589ee519a1fSgjelinek static pthread_mutex_t zonecfg_devwalk_lock = PTHREAD_MUTEX_INITIALIZER;
2590ee519a1fSgjelinek /*
2591ff17c8bfSgjelinek  * Global variables used to pass data from zonecfg_dev_manifest to the nftw
2592ee519a1fSgjelinek  * call-back (zonecfg_devwalk_cb).  g_devwalk_data is really the void*
2593ff17c8bfSgjelinek  * parameter and g_devwalk_cb is really the *cb parameter from
2594ff17c8bfSgjelinek  * zonecfg_dev_manifest.
2595ee519a1fSgjelinek  */
2596ff17c8bfSgjelinek typedef struct __g_devwalk_data *g_devwalk_data_t;
2597ff17c8bfSgjelinek static g_devwalk_data_t g_devwalk_data;
2598ee519a1fSgjelinek static int (*g_devwalk_cb)(const char *, uid_t, gid_t, mode_t, const char *,
2599ee519a1fSgjelinek     void *);
2600ee519a1fSgjelinek static size_t g_devwalk_skip_prefix;
2601ee519a1fSgjelinek 
2602ee519a1fSgjelinek /*
2603ff17c8bfSgjelinek  * zonecfg_dev_manifest call-back function used during detach to generate the
2604ff17c8bfSgjelinek  * dev info in the manifest.
2605ff17c8bfSgjelinek  */
2606ff17c8bfSgjelinek static int
2607ff17c8bfSgjelinek get_detach_dev_entry(const char *name, uid_t uid, gid_t gid, mode_t mode,
2608ff17c8bfSgjelinek     const char *acl, void *hdl)
2609ff17c8bfSgjelinek {
2610ff17c8bfSgjelinek 	zone_dochandle_t handle = (zone_dochandle_t)hdl;
2611ff17c8bfSgjelinek 	xmlNodePtr newnode;
2612ff17c8bfSgjelinek 	xmlNodePtr cur;
2613ff17c8bfSgjelinek 	int err;
2614ff17c8bfSgjelinek 	char buf[128];
2615ff17c8bfSgjelinek 
2616ff17c8bfSgjelinek 	if ((err = operation_prep(handle)) != Z_OK)
2617ff17c8bfSgjelinek 		return (err);
2618ff17c8bfSgjelinek 
2619ff17c8bfSgjelinek 	cur = handle->zone_dh_cur;
2620ff17c8bfSgjelinek 	newnode = xmlNewTextChild(cur, NULL, DTD_ELEM_DEV_PERM, NULL);
2621ff17c8bfSgjelinek 	if ((err = newprop(newnode, DTD_ATTR_NAME, (char *)name)) != Z_OK)
2622ff17c8bfSgjelinek 		return (err);
2623ff17c8bfSgjelinek 	(void) snprintf(buf, sizeof (buf), "%lu", uid);
2624ff17c8bfSgjelinek 	if ((err = newprop(newnode, DTD_ATTR_UID, buf)) != Z_OK)
2625ff17c8bfSgjelinek 		return (err);
2626ff17c8bfSgjelinek 	(void) snprintf(buf, sizeof (buf), "%lu", gid);
2627ff17c8bfSgjelinek 	if ((err = newprop(newnode, DTD_ATTR_GID, buf)) != Z_OK)
2628ff17c8bfSgjelinek 		return (err);
2629ff17c8bfSgjelinek 	(void) snprintf(buf, sizeof (buf), "%o", mode);
2630ff17c8bfSgjelinek 	if ((err = newprop(newnode, DTD_ATTR_MODE, buf)) != Z_OK)
2631ff17c8bfSgjelinek 		return (err);
2632ff17c8bfSgjelinek 	if ((err = newprop(newnode, DTD_ATTR_ACL, (char *)acl)) != Z_OK)
2633ff17c8bfSgjelinek 		return (err);
2634ff17c8bfSgjelinek 	return (Z_OK);
2635ff17c8bfSgjelinek }
2636ff17c8bfSgjelinek 
2637ff17c8bfSgjelinek /*
2638ff17c8bfSgjelinek  * This is the nftw call-back function used by zonecfg_dev_manifest.  It is
2639ff17c8bfSgjelinek  * responsible for calling the actual call-back.
2640ee519a1fSgjelinek  */
2641ee519a1fSgjelinek /* ARGSUSED2 */
2642ee519a1fSgjelinek static int
2643ee519a1fSgjelinek zonecfg_devwalk_cb(const char *path, const struct stat *st, int f,
2644ee519a1fSgjelinek     struct FTW *ftw)
2645ee519a1fSgjelinek {
2646ee519a1fSgjelinek 	acl_t *acl;
2647ee519a1fSgjelinek 	char *acl_txt = NULL;
2648ee519a1fSgjelinek 
2649ee519a1fSgjelinek 	/* skip all but character and block devices */
2650ee519a1fSgjelinek 	if (!S_ISBLK(st->st_mode) && !S_ISCHR(st->st_mode))
2651ee519a1fSgjelinek 		return (0);
2652ee519a1fSgjelinek 
2653ee519a1fSgjelinek 	if ((acl_get(path, ACL_NO_TRIVIAL, &acl) == 0) &&
2654ee519a1fSgjelinek 	    acl != NULL) {
2655ee519a1fSgjelinek 		acl_txt = acl_totext(acl, ACL_NORESOLVE);
2656ee519a1fSgjelinek 		acl_free(acl);
2657ee519a1fSgjelinek 	}
2658ee519a1fSgjelinek 
2659ee519a1fSgjelinek 	if (strlen(path) <= g_devwalk_skip_prefix)
2660ee519a1fSgjelinek 		return (0);
2661ee519a1fSgjelinek 
2662ee519a1fSgjelinek 	g_devwalk_cb(path + g_devwalk_skip_prefix, st->st_uid, st->st_gid,
2663ee519a1fSgjelinek 	    st->st_mode & S_IAMB, acl_txt != NULL ? acl_txt : "",
2664ee519a1fSgjelinek 	    g_devwalk_data);
2665ee519a1fSgjelinek 	free(acl_txt);
2666ee519a1fSgjelinek 	return (0);
2667ee519a1fSgjelinek }
2668ee519a1fSgjelinek 
2669ee519a1fSgjelinek /*
2670ff17c8bfSgjelinek  * Walk the dev tree for the zone specified by hdl and call the
2671ff17c8bfSgjelinek  * get_detach_dev_entry call-back function for each entry in the tree.  The
2672ff17c8bfSgjelinek  * call-back will be passed the name, uid, gid, mode, acl string and the
2673ff17c8bfSgjelinek  * handle input parameter for each dev entry.
2674ee519a1fSgjelinek  *
2675ff17c8bfSgjelinek  * Data is passed to get_detach_dev_entry through the global variables
2676ee519a1fSgjelinek  * g_devwalk_data, *g_devwalk_cb, and g_devwalk_skip_prefix.  The
2677ff17c8bfSgjelinek  * zonecfg_devwalk_cb function will actually call get_detach_dev_entry.
2678ee519a1fSgjelinek  */
2679ee519a1fSgjelinek int
2680ff17c8bfSgjelinek zonecfg_dev_manifest(zone_dochandle_t hdl)
2681ee519a1fSgjelinek {
2682ee519a1fSgjelinek 	char path[MAXPATHLEN];
2683ee519a1fSgjelinek 	int ret;
2684ee519a1fSgjelinek 
2685ee519a1fSgjelinek 	if ((ret = zonecfg_get_zonepath(hdl, path, sizeof (path))) != Z_OK)
2686ee519a1fSgjelinek 		return (ret);
2687ee519a1fSgjelinek 
2688ee519a1fSgjelinek 	if (strlcat(path, "/dev", sizeof (path)) >= sizeof (path))
2689ee519a1fSgjelinek 		return (Z_TOO_BIG);
2690ee519a1fSgjelinek 
2691ee519a1fSgjelinek 	/*
2692ff17c8bfSgjelinek 	 * We have to serialize all devwalks in the same process
2693ee519a1fSgjelinek 	 * (which should be fine), since nftw() is so badly designed.
2694ee519a1fSgjelinek 	 */
2695ee519a1fSgjelinek 	(void) pthread_mutex_lock(&zonecfg_devwalk_lock);
2696ee519a1fSgjelinek 
2697ff17c8bfSgjelinek 	g_devwalk_skip_prefix = strlen(path) + 1;
2698ff17c8bfSgjelinek 	g_devwalk_data = (g_devwalk_data_t)hdl;
2699ff17c8bfSgjelinek 	g_devwalk_cb = get_detach_dev_entry;
2700ee519a1fSgjelinek 	(void) nftw(path, zonecfg_devwalk_cb, 0, FTW_PHYS);
2701ee519a1fSgjelinek 
2702ee519a1fSgjelinek 	(void) pthread_mutex_unlock(&zonecfg_devwalk_lock);
2703ee519a1fSgjelinek 	return (Z_OK);
2704ee519a1fSgjelinek }
2705ee519a1fSgjelinek 
2706ee519a1fSgjelinek /*
2707ee519a1fSgjelinek  * Update the owner, group, mode and acl on the specified dev (inpath) for
2708ee519a1fSgjelinek  * the zone (hdl).  This function can be used to fix up the dev tree after
2709ee519a1fSgjelinek  * attaching a migrated zone.
2710ee519a1fSgjelinek  */
2711ee519a1fSgjelinek int
2712ee519a1fSgjelinek zonecfg_devperms_apply(zone_dochandle_t hdl, const char *inpath, uid_t owner,
2713ee519a1fSgjelinek     gid_t group, mode_t mode, const char *acltxt)
2714ee519a1fSgjelinek {
2715ee519a1fSgjelinek 	int ret;
2716ee519a1fSgjelinek 	char path[MAXPATHLEN];
2717ee519a1fSgjelinek 	struct stat st;
2718ee519a1fSgjelinek 	acl_t *aclp;
2719ee519a1fSgjelinek 
2720ee519a1fSgjelinek 	if ((ret = zonecfg_get_zonepath(hdl, path, sizeof (path))) != Z_OK)
2721ee519a1fSgjelinek 		return (ret);
2722ee519a1fSgjelinek 
2723ee519a1fSgjelinek 	if (strlcat(path, "/dev/", sizeof (path)) >= sizeof (path))
2724ee519a1fSgjelinek 		return (Z_TOO_BIG);
2725ee519a1fSgjelinek 	if (strlcat(path, inpath, sizeof (path)) >= sizeof (path))
2726ee519a1fSgjelinek 		return (Z_TOO_BIG);
2727ee519a1fSgjelinek 
2728ee519a1fSgjelinek 	if (stat(path, &st) == -1)
2729ee519a1fSgjelinek 		return (Z_INVAL);
2730ee519a1fSgjelinek 
2731ee519a1fSgjelinek 	/* make sure we're only touching device nodes */
2732ee519a1fSgjelinek 	if (!S_ISCHR(st.st_mode) && !S_ISBLK(st.st_mode))
2733ee519a1fSgjelinek 		return (Z_INVAL);
2734ee519a1fSgjelinek 
2735ee519a1fSgjelinek 	if (chown(path, owner, group) == -1)
2736ee519a1fSgjelinek 		return (Z_SYSTEM);
2737ee519a1fSgjelinek 
2738ee519a1fSgjelinek 	if (chmod(path, mode) == -1)
2739ee519a1fSgjelinek 		return (Z_SYSTEM);
2740ee519a1fSgjelinek 
2741ee519a1fSgjelinek 	if ((acltxt == NULL) || (strcmp(acltxt, "") == 0))
2742ee519a1fSgjelinek 		return (Z_OK);
2743ee519a1fSgjelinek 
27442ad45a84Sjv227347 	if (acl_fromtext(acltxt, &aclp) != 0) {
27452ad45a84Sjv227347 		errno = EINVAL;
2746ee519a1fSgjelinek 		return (Z_SYSTEM);
27472ad45a84Sjv227347 	}
2748ee519a1fSgjelinek 
2749ee519a1fSgjelinek 	errno = 0;
2750ee519a1fSgjelinek 	if (acl_set(path, aclp) == -1) {
2751ee519a1fSgjelinek 		free(aclp);
2752ee519a1fSgjelinek 		return (Z_SYSTEM);
2753ee519a1fSgjelinek 	}
2754ee519a1fSgjelinek 
2755ee519a1fSgjelinek 	free(aclp);
2756ee519a1fSgjelinek 	return (Z_OK);
2757ee519a1fSgjelinek }
2758ee519a1fSgjelinek 
27597c478bd9Sstevel@tonic-gate /*
27607c478bd9Sstevel@tonic-gate  * This function finds everything mounted under a zone's rootpath.
27617c478bd9Sstevel@tonic-gate  * This returns the number of mounts under rootpath, or -1 on error.
27627c478bd9Sstevel@tonic-gate  * callback is called once per mount found with the first argument
27637c478bd9Sstevel@tonic-gate  * pointing to the  mount point.
27647c478bd9Sstevel@tonic-gate  *
27657c478bd9Sstevel@tonic-gate  * If the callback function returns non-zero zonecfg_find_mounts
27667c478bd9Sstevel@tonic-gate  * aborts with an error.
27677c478bd9Sstevel@tonic-gate  */
27687c478bd9Sstevel@tonic-gate int
27697c478bd9Sstevel@tonic-gate zonecfg_find_mounts(char *rootpath, int (*callback)(const char *, void *),
27707c478bd9Sstevel@tonic-gate     void *priv) {
27717c478bd9Sstevel@tonic-gate 	FILE *mnttab;
27727c478bd9Sstevel@tonic-gate 	struct mnttab m;
27737c478bd9Sstevel@tonic-gate 	size_t l;
277407b574eeSgjelinek 	int zfsl;
27757c478bd9Sstevel@tonic-gate 	int rv = 0;
277607b574eeSgjelinek 	char zfs_path[MAXPATHLEN];
27777c478bd9Sstevel@tonic-gate 
27787c478bd9Sstevel@tonic-gate 	assert(rootpath != NULL);
27797c478bd9Sstevel@tonic-gate 
278007b574eeSgjelinek 	if ((zfsl = snprintf(zfs_path, sizeof (zfs_path), "%s/.zfs/", rootpath))
278107b574eeSgjelinek 	    >= sizeof (zfs_path))
278207b574eeSgjelinek 		return (-1);
278307b574eeSgjelinek 
27847c478bd9Sstevel@tonic-gate 	l = strlen(rootpath);
27857c478bd9Sstevel@tonic-gate 
27867c478bd9Sstevel@tonic-gate 	mnttab = fopen("/etc/mnttab", "r");
27877c478bd9Sstevel@tonic-gate 
27887c478bd9Sstevel@tonic-gate 	if (mnttab == NULL)
27897c478bd9Sstevel@tonic-gate 		return (-1);
27907c478bd9Sstevel@tonic-gate 
27917c478bd9Sstevel@tonic-gate 	if (ioctl(fileno(mnttab), MNTIOC_SHOWHIDDEN, NULL) < 0)  {
27927c478bd9Sstevel@tonic-gate 		rv = -1;
27937c478bd9Sstevel@tonic-gate 		goto out;
27947c478bd9Sstevel@tonic-gate 	}
27957c478bd9Sstevel@tonic-gate 
27967c478bd9Sstevel@tonic-gate 	while (!getmntent(mnttab, &m)) {
27977c478bd9Sstevel@tonic-gate 		if ((strncmp(rootpath, m.mnt_mountp, l) == 0) &&
279807b574eeSgjelinek 		    (m.mnt_mountp[l] == '/') &&
279907b574eeSgjelinek 		    (strncmp(zfs_path, m.mnt_mountp, zfsl) != 0)) {
28007c478bd9Sstevel@tonic-gate 			rv++;
28017c478bd9Sstevel@tonic-gate 			if (callback == NULL)
28027c478bd9Sstevel@tonic-gate 				continue;
28037c478bd9Sstevel@tonic-gate 			if (callback(m.mnt_mountp, priv)) {
28047c478bd9Sstevel@tonic-gate 				rv = -1;
28057c478bd9Sstevel@tonic-gate 				goto out;
28067c478bd9Sstevel@tonic-gate 
28077c478bd9Sstevel@tonic-gate 			}
28087c478bd9Sstevel@tonic-gate 		}
28097c478bd9Sstevel@tonic-gate 	}
28107c478bd9Sstevel@tonic-gate 
28117c478bd9Sstevel@tonic-gate out:
28127c478bd9Sstevel@tonic-gate 	(void) fclose(mnttab);
28137c478bd9Sstevel@tonic-gate 	return (rv);
28147c478bd9Sstevel@tonic-gate }
28157c478bd9Sstevel@tonic-gate 
28167c478bd9Sstevel@tonic-gate int
28177c478bd9Sstevel@tonic-gate zonecfg_lookup_attr(zone_dochandle_t handle, struct zone_attrtab *tabptr)
28187c478bd9Sstevel@tonic-gate {
28197c478bd9Sstevel@tonic-gate 	xmlNodePtr cur, firstmatch;
28207c478bd9Sstevel@tonic-gate 	int err;
28217c478bd9Sstevel@tonic-gate 	char name[MAXNAMELEN], type[MAXNAMELEN], value[MAXNAMELEN];
28227c478bd9Sstevel@tonic-gate 
28237c478bd9Sstevel@tonic-gate 	if (tabptr == NULL)
28247c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
28257c478bd9Sstevel@tonic-gate 
28267c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
28277c478bd9Sstevel@tonic-gate 		return (err);
28287c478bd9Sstevel@tonic-gate 
28297c478bd9Sstevel@tonic-gate 	cur = handle->zone_dh_cur;
28307c478bd9Sstevel@tonic-gate 	firstmatch = NULL;
28317c478bd9Sstevel@tonic-gate 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
28327c478bd9Sstevel@tonic-gate 		if (xmlStrcmp(cur->name, DTD_ELEM_ATTR))
28337c478bd9Sstevel@tonic-gate 			continue;
28347c478bd9Sstevel@tonic-gate 		if (strlen(tabptr->zone_attr_name) > 0) {
28357c478bd9Sstevel@tonic-gate 			if ((fetchprop(cur, DTD_ATTR_NAME, name,
28367c478bd9Sstevel@tonic-gate 			    sizeof (name)) == Z_OK) &&
28377c478bd9Sstevel@tonic-gate 			    (strcmp(tabptr->zone_attr_name, name) == 0)) {
28387c478bd9Sstevel@tonic-gate 				if (firstmatch == NULL)
28397c478bd9Sstevel@tonic-gate 					firstmatch = cur;
28407c478bd9Sstevel@tonic-gate 				else
28417c478bd9Sstevel@tonic-gate 					return (Z_INSUFFICIENT_SPEC);
28427c478bd9Sstevel@tonic-gate 			}
28437c478bd9Sstevel@tonic-gate 		}
28447c478bd9Sstevel@tonic-gate 		if (strlen(tabptr->zone_attr_type) > 0) {
28457c478bd9Sstevel@tonic-gate 			if ((fetchprop(cur, DTD_ATTR_TYPE, type,
28467c478bd9Sstevel@tonic-gate 			    sizeof (type)) == Z_OK)) {
28477c478bd9Sstevel@tonic-gate 				if (strcmp(tabptr->zone_attr_type, type) == 0) {
28487c478bd9Sstevel@tonic-gate 					if (firstmatch == NULL)
28497c478bd9Sstevel@tonic-gate 						firstmatch = cur;
28507c478bd9Sstevel@tonic-gate 					else if (firstmatch != cur)
28517c478bd9Sstevel@tonic-gate 						return (Z_INSUFFICIENT_SPEC);
28527c478bd9Sstevel@tonic-gate 				} else {
28537c478bd9Sstevel@tonic-gate 					/*
28547c478bd9Sstevel@tonic-gate 					 * If another property matched but this
28557c478bd9Sstevel@tonic-gate 					 * one doesn't then reset firstmatch.
28567c478bd9Sstevel@tonic-gate 					 */
28577c478bd9Sstevel@tonic-gate 					if (firstmatch == cur)
28587c478bd9Sstevel@tonic-gate 						firstmatch = NULL;
28597c478bd9Sstevel@tonic-gate 				}
28607c478bd9Sstevel@tonic-gate 			}
28617c478bd9Sstevel@tonic-gate 		}
28627c478bd9Sstevel@tonic-gate 		if (strlen(tabptr->zone_attr_value) > 0) {
28637c478bd9Sstevel@tonic-gate 			if ((fetchprop(cur, DTD_ATTR_VALUE, value,
28647c478bd9Sstevel@tonic-gate 			    sizeof (value)) == Z_OK)) {
28657c478bd9Sstevel@tonic-gate 				if (strcmp(tabptr->zone_attr_value, value) ==
28667c478bd9Sstevel@tonic-gate 				    0) {
28677c478bd9Sstevel@tonic-gate 					if (firstmatch == NULL)
28687c478bd9Sstevel@tonic-gate 						firstmatch = cur;
28697c478bd9Sstevel@tonic-gate 					else if (firstmatch != cur)
28707c478bd9Sstevel@tonic-gate 						return (Z_INSUFFICIENT_SPEC);
28717c478bd9Sstevel@tonic-gate 				} else {
28727c478bd9Sstevel@tonic-gate 					/*
28737c478bd9Sstevel@tonic-gate 					 * If another property matched but this
28747c478bd9Sstevel@tonic-gate 					 * one doesn't then reset firstmatch.
28757c478bd9Sstevel@tonic-gate 					 */
28767c478bd9Sstevel@tonic-gate 					if (firstmatch == cur)
28777c478bd9Sstevel@tonic-gate 						firstmatch = NULL;
28787c478bd9Sstevel@tonic-gate 				}
28797c478bd9Sstevel@tonic-gate 			}
28807c478bd9Sstevel@tonic-gate 		}
28817c478bd9Sstevel@tonic-gate 	}
28827c478bd9Sstevel@tonic-gate 	if (firstmatch == NULL)
28837c478bd9Sstevel@tonic-gate 		return (Z_NO_RESOURCE_ID);
28847c478bd9Sstevel@tonic-gate 
28857c478bd9Sstevel@tonic-gate 	cur = firstmatch;
28867c478bd9Sstevel@tonic-gate 
28877c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_NAME, tabptr->zone_attr_name,
28887c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_attr_name))) != Z_OK)
28897c478bd9Sstevel@tonic-gate 		return (err);
28907c478bd9Sstevel@tonic-gate 
28917c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_TYPE, tabptr->zone_attr_type,
28927c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_attr_type))) != Z_OK)
28937c478bd9Sstevel@tonic-gate 		return (err);
28947c478bd9Sstevel@tonic-gate 
28957c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_VALUE, tabptr->zone_attr_value,
28967c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_attr_value))) != Z_OK)
28977c478bd9Sstevel@tonic-gate 		return (err);
28987c478bd9Sstevel@tonic-gate 
28997c478bd9Sstevel@tonic-gate 	return (Z_OK);
29007c478bd9Sstevel@tonic-gate }
29017c478bd9Sstevel@tonic-gate 
29027c478bd9Sstevel@tonic-gate static int
29037c478bd9Sstevel@tonic-gate zonecfg_add_attr_core(zone_dochandle_t handle, struct zone_attrtab *tabptr)
29047c478bd9Sstevel@tonic-gate {
29057c478bd9Sstevel@tonic-gate 	xmlNodePtr newnode, cur = handle->zone_dh_cur;
29067c478bd9Sstevel@tonic-gate 	int err;
29077c478bd9Sstevel@tonic-gate 
29087c478bd9Sstevel@tonic-gate 	newnode = xmlNewTextChild(cur, NULL, DTD_ELEM_ATTR, NULL);
2909a1be23daSdp 	err = newprop(newnode, DTD_ATTR_NAME, tabptr->zone_attr_name);
29107c478bd9Sstevel@tonic-gate 	if (err != Z_OK)
29117c478bd9Sstevel@tonic-gate 		return (err);
2912a1be23daSdp 	err = newprop(newnode, DTD_ATTR_TYPE, tabptr->zone_attr_type);
29137c478bd9Sstevel@tonic-gate 	if (err != Z_OK)
29147c478bd9Sstevel@tonic-gate 		return (err);
2915a1be23daSdp 	err = newprop(newnode, DTD_ATTR_VALUE, tabptr->zone_attr_value);
29167c478bd9Sstevel@tonic-gate 	if (err != Z_OK)
29177c478bd9Sstevel@tonic-gate 		return (err);
29187c478bd9Sstevel@tonic-gate 	return (Z_OK);
29197c478bd9Sstevel@tonic-gate }
29207c478bd9Sstevel@tonic-gate 
29217c478bd9Sstevel@tonic-gate int
29227c478bd9Sstevel@tonic-gate zonecfg_add_attr(zone_dochandle_t handle, struct zone_attrtab *tabptr)
29237c478bd9Sstevel@tonic-gate {
29247c478bd9Sstevel@tonic-gate 	int err;
29257c478bd9Sstevel@tonic-gate 
29267c478bd9Sstevel@tonic-gate 	if (tabptr == NULL)
29277c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
29287c478bd9Sstevel@tonic-gate 
29297c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
29307c478bd9Sstevel@tonic-gate 		return (err);
29317c478bd9Sstevel@tonic-gate 
29327c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_add_attr_core(handle, tabptr)) != Z_OK)
29337c478bd9Sstevel@tonic-gate 		return (err);
29347c478bd9Sstevel@tonic-gate 
29357c478bd9Sstevel@tonic-gate 	return (Z_OK);
29367c478bd9Sstevel@tonic-gate }
29377c478bd9Sstevel@tonic-gate 
29387c478bd9Sstevel@tonic-gate static int
29397c478bd9Sstevel@tonic-gate zonecfg_delete_attr_core(zone_dochandle_t handle, struct zone_attrtab *tabptr)
29407c478bd9Sstevel@tonic-gate {
29417c478bd9Sstevel@tonic-gate 	xmlNodePtr cur = handle->zone_dh_cur;
29427c478bd9Sstevel@tonic-gate 	int name_match, type_match, value_match;
29437c478bd9Sstevel@tonic-gate 
29447c478bd9Sstevel@tonic-gate 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
29457c478bd9Sstevel@tonic-gate 		if (xmlStrcmp(cur->name, DTD_ELEM_ATTR))
29467c478bd9Sstevel@tonic-gate 			continue;
29477c478bd9Sstevel@tonic-gate 
29487c478bd9Sstevel@tonic-gate 		name_match = match_prop(cur, DTD_ATTR_NAME,
29497c478bd9Sstevel@tonic-gate 		    tabptr->zone_attr_name);
29507c478bd9Sstevel@tonic-gate 		type_match = match_prop(cur, DTD_ATTR_TYPE,
29517c478bd9Sstevel@tonic-gate 		    tabptr->zone_attr_type);
29527c478bd9Sstevel@tonic-gate 		value_match = match_prop(cur, DTD_ATTR_VALUE,
29537c478bd9Sstevel@tonic-gate 		    tabptr->zone_attr_value);
29547c478bd9Sstevel@tonic-gate 
29557c478bd9Sstevel@tonic-gate 		if (name_match && type_match && value_match) {
29567c478bd9Sstevel@tonic-gate 			xmlUnlinkNode(cur);
29577c478bd9Sstevel@tonic-gate 			xmlFreeNode(cur);
29587c478bd9Sstevel@tonic-gate 			return (Z_OK);
29597c478bd9Sstevel@tonic-gate 		}
29607c478bd9Sstevel@tonic-gate 	}
29617c478bd9Sstevel@tonic-gate 	return (Z_NO_RESOURCE_ID);
29627c478bd9Sstevel@tonic-gate }
29637c478bd9Sstevel@tonic-gate 
29647c478bd9Sstevel@tonic-gate int
29657c478bd9Sstevel@tonic-gate zonecfg_delete_attr(zone_dochandle_t handle, struct zone_attrtab *tabptr)
29667c478bd9Sstevel@tonic-gate {
29677c478bd9Sstevel@tonic-gate 	int err;
29687c478bd9Sstevel@tonic-gate 
29697c478bd9Sstevel@tonic-gate 	if (tabptr == NULL)
29707c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
29717c478bd9Sstevel@tonic-gate 
29727c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
29737c478bd9Sstevel@tonic-gate 		return (err);
29747c478bd9Sstevel@tonic-gate 
29757c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_delete_attr_core(handle, tabptr)) != Z_OK)
29767c478bd9Sstevel@tonic-gate 		return (err);
29777c478bd9Sstevel@tonic-gate 
29787c478bd9Sstevel@tonic-gate 	return (Z_OK);
29797c478bd9Sstevel@tonic-gate }
29807c478bd9Sstevel@tonic-gate 
29817c478bd9Sstevel@tonic-gate int
29827c478bd9Sstevel@tonic-gate zonecfg_modify_attr(
29837c478bd9Sstevel@tonic-gate 	zone_dochandle_t handle,
29847c478bd9Sstevel@tonic-gate 	struct zone_attrtab *oldtabptr,
29857c478bd9Sstevel@tonic-gate 	struct zone_attrtab *newtabptr)
29867c478bd9Sstevel@tonic-gate {
29877c478bd9Sstevel@tonic-gate 	int err;
29887c478bd9Sstevel@tonic-gate 
29897c478bd9Sstevel@tonic-gate 	if (oldtabptr == NULL || newtabptr == NULL)
29907c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
29917c478bd9Sstevel@tonic-gate 
29927c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
29937c478bd9Sstevel@tonic-gate 		return (err);
29947c478bd9Sstevel@tonic-gate 
29957c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_delete_attr_core(handle, oldtabptr)) != Z_OK)
29967c478bd9Sstevel@tonic-gate 		return (err);
29977c478bd9Sstevel@tonic-gate 
29987c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_add_attr_core(handle, newtabptr)) != Z_OK)
29997c478bd9Sstevel@tonic-gate 		return (err);
30007c478bd9Sstevel@tonic-gate 
30017c478bd9Sstevel@tonic-gate 	return (Z_OK);
30027c478bd9Sstevel@tonic-gate }
30037c478bd9Sstevel@tonic-gate 
30047c478bd9Sstevel@tonic-gate int
30057c478bd9Sstevel@tonic-gate zonecfg_get_attr_boolean(const struct zone_attrtab *attr, boolean_t *value)
30067c478bd9Sstevel@tonic-gate {
30077c478bd9Sstevel@tonic-gate 	if (attr == NULL)
30087c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
30097c478bd9Sstevel@tonic-gate 
30107c478bd9Sstevel@tonic-gate 	if (strcmp(attr->zone_attr_type, DTD_ENTITY_BOOLEAN) != 0)
30117c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
30127c478bd9Sstevel@tonic-gate 
30137c478bd9Sstevel@tonic-gate 	if (strcmp(attr->zone_attr_value, DTD_ENTITY_TRUE) == 0) {
30147c478bd9Sstevel@tonic-gate 		*value = B_TRUE;
30157c478bd9Sstevel@tonic-gate 		return (Z_OK);
30167c478bd9Sstevel@tonic-gate 	}
30177c478bd9Sstevel@tonic-gate 	if (strcmp(attr->zone_attr_value, DTD_ENTITY_FALSE) == 0) {
30187c478bd9Sstevel@tonic-gate 		*value = B_FALSE;
30197c478bd9Sstevel@tonic-gate 		return (Z_OK);
30207c478bd9Sstevel@tonic-gate 	}
30217c478bd9Sstevel@tonic-gate 	return (Z_INVAL);
30227c478bd9Sstevel@tonic-gate }
30237c478bd9Sstevel@tonic-gate 
30247c478bd9Sstevel@tonic-gate int
30257c478bd9Sstevel@tonic-gate zonecfg_get_attr_int(const struct zone_attrtab *attr, int64_t *value)
30267c478bd9Sstevel@tonic-gate {
30277c478bd9Sstevel@tonic-gate 	long long result;
30287c478bd9Sstevel@tonic-gate 	char *endptr;
30297c478bd9Sstevel@tonic-gate 
30307c478bd9Sstevel@tonic-gate 	if (attr == NULL)
30317c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
30327c478bd9Sstevel@tonic-gate 
30337c478bd9Sstevel@tonic-gate 	if (strcmp(attr->zone_attr_type, DTD_ENTITY_INT) != 0)
30347c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
30357c478bd9Sstevel@tonic-gate 
30367c478bd9Sstevel@tonic-gate 	errno = 0;
30377c478bd9Sstevel@tonic-gate 	result = strtoll(attr->zone_attr_value, &endptr, 10);
30387c478bd9Sstevel@tonic-gate 	if (errno != 0 || *endptr != '\0')
30397c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
30407c478bd9Sstevel@tonic-gate 	*value = result;
30417c478bd9Sstevel@tonic-gate 	return (Z_OK);
30427c478bd9Sstevel@tonic-gate }
30437c478bd9Sstevel@tonic-gate 
30447c478bd9Sstevel@tonic-gate int
30457c478bd9Sstevel@tonic-gate zonecfg_get_attr_string(const struct zone_attrtab *attr, char *value,
30467c478bd9Sstevel@tonic-gate     size_t val_sz)
30477c478bd9Sstevel@tonic-gate {
30487c478bd9Sstevel@tonic-gate 	if (attr == NULL)
30497c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
30507c478bd9Sstevel@tonic-gate 
30517c478bd9Sstevel@tonic-gate 	if (strcmp(attr->zone_attr_type, DTD_ENTITY_STRING) != 0)
30527c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
30537c478bd9Sstevel@tonic-gate 
30547c478bd9Sstevel@tonic-gate 	if (strlcpy(value, attr->zone_attr_value, val_sz) >= val_sz)
30557c478bd9Sstevel@tonic-gate 		return (Z_TOO_BIG);
30567c478bd9Sstevel@tonic-gate 	return (Z_OK);
30577c478bd9Sstevel@tonic-gate }
30587c478bd9Sstevel@tonic-gate 
30597c478bd9Sstevel@tonic-gate int
30607c478bd9Sstevel@tonic-gate zonecfg_get_attr_uint(const struct zone_attrtab *attr, uint64_t *value)
30617c478bd9Sstevel@tonic-gate {
30627c478bd9Sstevel@tonic-gate 	unsigned long long result;
30637c478bd9Sstevel@tonic-gate 	long long neg_result;
30647c478bd9Sstevel@tonic-gate 	char *endptr;
30657c478bd9Sstevel@tonic-gate 
30667c478bd9Sstevel@tonic-gate 	if (attr == NULL)
30677c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
30687c478bd9Sstevel@tonic-gate 
30697c478bd9Sstevel@tonic-gate 	if (strcmp(attr->zone_attr_type, DTD_ENTITY_UINT) != 0)
30707c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
30717c478bd9Sstevel@tonic-gate 
30727c478bd9Sstevel@tonic-gate 	errno = 0;
30737c478bd9Sstevel@tonic-gate 	result = strtoull(attr->zone_attr_value, &endptr, 10);
30747c478bd9Sstevel@tonic-gate 	if (errno != 0 || *endptr != '\0')
30757c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
30767c478bd9Sstevel@tonic-gate 	errno = 0;
30777c478bd9Sstevel@tonic-gate 	neg_result = strtoll(attr->zone_attr_value, &endptr, 10);
30787c478bd9Sstevel@tonic-gate 	/*
30797c478bd9Sstevel@tonic-gate 	 * Incredibly, strtoull("<negative number>", ...) will not fail but
30807c478bd9Sstevel@tonic-gate 	 * return whatever (negative) number cast as a u_longlong_t, so we
30817c478bd9Sstevel@tonic-gate 	 * need to look for this here.
30827c478bd9Sstevel@tonic-gate 	 */
30837c478bd9Sstevel@tonic-gate 	if (errno == 0 && neg_result < 0)
30847c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
30857c478bd9Sstevel@tonic-gate 	*value = result;
30867c478bd9Sstevel@tonic-gate 	return (Z_OK);
30877c478bd9Sstevel@tonic-gate }
30887c478bd9Sstevel@tonic-gate 
30897c478bd9Sstevel@tonic-gate int
30907c478bd9Sstevel@tonic-gate zonecfg_lookup_rctl(zone_dochandle_t handle, struct zone_rctltab *tabptr)
30917c478bd9Sstevel@tonic-gate {
30927c478bd9Sstevel@tonic-gate 	xmlNodePtr cur, val;
30937c478bd9Sstevel@tonic-gate 	char savedname[MAXNAMELEN];
30947c478bd9Sstevel@tonic-gate 	struct zone_rctlvaltab *valptr;
30957c478bd9Sstevel@tonic-gate 	int err;
30967c478bd9Sstevel@tonic-gate 
30972ad45a84Sjv227347 	if (strlen(tabptr->zone_rctl_name) == 0)
30987c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
30997c478bd9Sstevel@tonic-gate 
31007c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
31017c478bd9Sstevel@tonic-gate 		return (err);
31027c478bd9Sstevel@tonic-gate 
31037c478bd9Sstevel@tonic-gate 	cur = handle->zone_dh_cur;
31047c478bd9Sstevel@tonic-gate 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
31057c478bd9Sstevel@tonic-gate 		if (xmlStrcmp(cur->name, DTD_ELEM_RCTL))
31067c478bd9Sstevel@tonic-gate 			continue;
31077c478bd9Sstevel@tonic-gate 		if ((fetchprop(cur, DTD_ATTR_NAME, savedname,
31087c478bd9Sstevel@tonic-gate 		    sizeof (savedname)) == Z_OK) &&
31097c478bd9Sstevel@tonic-gate 		    (strcmp(savedname, tabptr->zone_rctl_name) == 0)) {
31107c478bd9Sstevel@tonic-gate 			tabptr->zone_rctl_valptr = NULL;
31117c478bd9Sstevel@tonic-gate 			for (val = cur->xmlChildrenNode; val != NULL;
31127c478bd9Sstevel@tonic-gate 			    val = val->next) {
31137c478bd9Sstevel@tonic-gate 				valptr = (struct zone_rctlvaltab *)malloc(
31147c478bd9Sstevel@tonic-gate 				    sizeof (struct zone_rctlvaltab));
31157c478bd9Sstevel@tonic-gate 				if (valptr == NULL)
31167c478bd9Sstevel@tonic-gate 					return (Z_NOMEM);
31177c478bd9Sstevel@tonic-gate 				if ((fetchprop(val, DTD_ATTR_PRIV,
31187c478bd9Sstevel@tonic-gate 				    valptr->zone_rctlval_priv,
31197c478bd9Sstevel@tonic-gate 				    sizeof (valptr->zone_rctlval_priv)) !=
31207c478bd9Sstevel@tonic-gate 				    Z_OK))
31217c478bd9Sstevel@tonic-gate 					break;
31227c478bd9Sstevel@tonic-gate 				if ((fetchprop(val, DTD_ATTR_LIMIT,
31237c478bd9Sstevel@tonic-gate 				    valptr->zone_rctlval_limit,
31247c478bd9Sstevel@tonic-gate 				    sizeof (valptr->zone_rctlval_limit)) !=
31257c478bd9Sstevel@tonic-gate 				    Z_OK))
31267c478bd9Sstevel@tonic-gate 					break;
31277c478bd9Sstevel@tonic-gate 				if ((fetchprop(val, DTD_ATTR_ACTION,
31287c478bd9Sstevel@tonic-gate 				    valptr->zone_rctlval_action,
31297c478bd9Sstevel@tonic-gate 				    sizeof (valptr->zone_rctlval_action)) !=
31307c478bd9Sstevel@tonic-gate 				    Z_OK))
31317c478bd9Sstevel@tonic-gate 					break;
31327c478bd9Sstevel@tonic-gate 				if (zonecfg_add_rctl_value(tabptr, valptr) !=
31337c478bd9Sstevel@tonic-gate 				    Z_OK)
31347c478bd9Sstevel@tonic-gate 					break;
31357c478bd9Sstevel@tonic-gate 			}
31367c478bd9Sstevel@tonic-gate 			return (Z_OK);
31377c478bd9Sstevel@tonic-gate 		}
31387c478bd9Sstevel@tonic-gate 	}
31397c478bd9Sstevel@tonic-gate 	return (Z_NO_RESOURCE_ID);
31407c478bd9Sstevel@tonic-gate }
31417c478bd9Sstevel@tonic-gate 
31427c478bd9Sstevel@tonic-gate static int
31437c478bd9Sstevel@tonic-gate zonecfg_add_rctl_core(zone_dochandle_t handle, struct zone_rctltab *tabptr)
31447c478bd9Sstevel@tonic-gate {
31457c478bd9Sstevel@tonic-gate 	xmlNodePtr newnode, cur = handle->zone_dh_cur, valnode;
31467c478bd9Sstevel@tonic-gate 	struct zone_rctlvaltab *valptr;
31477c478bd9Sstevel@tonic-gate 	int err;
31487c478bd9Sstevel@tonic-gate 
31497c478bd9Sstevel@tonic-gate 	newnode = xmlNewTextChild(cur, NULL, DTD_ELEM_RCTL, NULL);
3150a1be23daSdp 	err = newprop(newnode, DTD_ATTR_NAME, tabptr->zone_rctl_name);
31517c478bd9Sstevel@tonic-gate 	if (err != Z_OK)
31527c478bd9Sstevel@tonic-gate 		return (err);
31537c478bd9Sstevel@tonic-gate 	for (valptr = tabptr->zone_rctl_valptr; valptr != NULL;
31547c478bd9Sstevel@tonic-gate 	    valptr = valptr->zone_rctlval_next) {
31557c478bd9Sstevel@tonic-gate 		valnode = xmlNewTextChild(newnode, NULL,
31567c478bd9Sstevel@tonic-gate 		    DTD_ELEM_RCTLVALUE, NULL);
3157a1be23daSdp 		err = newprop(valnode, DTD_ATTR_PRIV,
31587c478bd9Sstevel@tonic-gate 		    valptr->zone_rctlval_priv);
31597c478bd9Sstevel@tonic-gate 		if (err != Z_OK)
31607c478bd9Sstevel@tonic-gate 			return (err);
3161a1be23daSdp 		err = newprop(valnode, DTD_ATTR_LIMIT,
31627c478bd9Sstevel@tonic-gate 		    valptr->zone_rctlval_limit);
31637c478bd9Sstevel@tonic-gate 		if (err != Z_OK)
31647c478bd9Sstevel@tonic-gate 			return (err);
3165a1be23daSdp 		err = newprop(valnode, DTD_ATTR_ACTION,
31667c478bd9Sstevel@tonic-gate 		    valptr->zone_rctlval_action);
31677c478bd9Sstevel@tonic-gate 		if (err != Z_OK)
31687c478bd9Sstevel@tonic-gate 			return (err);
31697c478bd9Sstevel@tonic-gate 	}
31707c478bd9Sstevel@tonic-gate 	return (Z_OK);
31717c478bd9Sstevel@tonic-gate }
31727c478bd9Sstevel@tonic-gate 
31737c478bd9Sstevel@tonic-gate int
31747c478bd9Sstevel@tonic-gate zonecfg_add_rctl(zone_dochandle_t handle, struct zone_rctltab *tabptr)
31757c478bd9Sstevel@tonic-gate {
31767c478bd9Sstevel@tonic-gate 	int err;
31777c478bd9Sstevel@tonic-gate 
31782ad45a84Sjv227347 	if (tabptr == NULL)
31797c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
31807c478bd9Sstevel@tonic-gate 
31817c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
31827c478bd9Sstevel@tonic-gate 		return (err);
31837c478bd9Sstevel@tonic-gate 
31847c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_add_rctl_core(handle, tabptr)) != Z_OK)
31857c478bd9Sstevel@tonic-gate 		return (err);
31867c478bd9Sstevel@tonic-gate 
31877c478bd9Sstevel@tonic-gate 	return (Z_OK);
31887c478bd9Sstevel@tonic-gate }
31897c478bd9Sstevel@tonic-gate 
31907c478bd9Sstevel@tonic-gate static int
31917c478bd9Sstevel@tonic-gate zonecfg_delete_rctl_core(zone_dochandle_t handle, struct zone_rctltab *tabptr)
31927c478bd9Sstevel@tonic-gate {
31937c478bd9Sstevel@tonic-gate 	xmlNodePtr cur = handle->zone_dh_cur;
31947c478bd9Sstevel@tonic-gate 	xmlChar *savedname;
31957c478bd9Sstevel@tonic-gate 	int name_result;
31967c478bd9Sstevel@tonic-gate 
31977c478bd9Sstevel@tonic-gate 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
31987c478bd9Sstevel@tonic-gate 		if (xmlStrcmp(cur->name, DTD_ELEM_RCTL))
31997c478bd9Sstevel@tonic-gate 			continue;
32007c478bd9Sstevel@tonic-gate 
32017c478bd9Sstevel@tonic-gate 		savedname = xmlGetProp(cur, DTD_ATTR_NAME);
32027c478bd9Sstevel@tonic-gate 		if (savedname == NULL)	/* shouldn't happen */
32037c478bd9Sstevel@tonic-gate 			continue;
32047c478bd9Sstevel@tonic-gate 		name_result = xmlStrcmp(savedname,
32057c478bd9Sstevel@tonic-gate 		    (const xmlChar *) tabptr->zone_rctl_name);
32067c478bd9Sstevel@tonic-gate 		xmlFree(savedname);
32077c478bd9Sstevel@tonic-gate 
32087c478bd9Sstevel@tonic-gate 		if (name_result == 0) {
32097c478bd9Sstevel@tonic-gate 			xmlUnlinkNode(cur);
32107c478bd9Sstevel@tonic-gate 			xmlFreeNode(cur);
32117c478bd9Sstevel@tonic-gate 			return (Z_OK);
32127c478bd9Sstevel@tonic-gate 		}
32137c478bd9Sstevel@tonic-gate 	}
32147c478bd9Sstevel@tonic-gate 	return (Z_NO_RESOURCE_ID);
32157c478bd9Sstevel@tonic-gate }
32167c478bd9Sstevel@tonic-gate 
32177c478bd9Sstevel@tonic-gate int
32187c478bd9Sstevel@tonic-gate zonecfg_delete_rctl(zone_dochandle_t handle, struct zone_rctltab *tabptr)
32197c478bd9Sstevel@tonic-gate {
32207c478bd9Sstevel@tonic-gate 	int err;
32217c478bd9Sstevel@tonic-gate 
32222ad45a84Sjv227347 	if (tabptr == NULL)
32237c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
32247c478bd9Sstevel@tonic-gate 
32257c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
32267c478bd9Sstevel@tonic-gate 		return (err);
32277c478bd9Sstevel@tonic-gate 
32287c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_delete_rctl_core(handle, tabptr)) != Z_OK)
32297c478bd9Sstevel@tonic-gate 		return (err);
32307c478bd9Sstevel@tonic-gate 
32317c478bd9Sstevel@tonic-gate 	return (Z_OK);
32327c478bd9Sstevel@tonic-gate }
32337c478bd9Sstevel@tonic-gate 
32347c478bd9Sstevel@tonic-gate int
32357c478bd9Sstevel@tonic-gate zonecfg_modify_rctl(
32367c478bd9Sstevel@tonic-gate 	zone_dochandle_t handle,
32377c478bd9Sstevel@tonic-gate 	struct zone_rctltab *oldtabptr,
32387c478bd9Sstevel@tonic-gate 	struct zone_rctltab *newtabptr)
32397c478bd9Sstevel@tonic-gate {
32407c478bd9Sstevel@tonic-gate 	int err;
32417c478bd9Sstevel@tonic-gate 
32422ad45a84Sjv227347 	if (oldtabptr == NULL || newtabptr == NULL)
32437c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
32447c478bd9Sstevel@tonic-gate 
32457c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
32467c478bd9Sstevel@tonic-gate 		return (err);
32477c478bd9Sstevel@tonic-gate 
32487c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_delete_rctl_core(handle, oldtabptr)) != Z_OK)
32497c478bd9Sstevel@tonic-gate 		return (err);
32507c478bd9Sstevel@tonic-gate 
32517c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_add_rctl_core(handle, newtabptr)) != Z_OK)
32527c478bd9Sstevel@tonic-gate 		return (err);
32537c478bd9Sstevel@tonic-gate 
32547c478bd9Sstevel@tonic-gate 	return (Z_OK);
32557c478bd9Sstevel@tonic-gate }
32567c478bd9Sstevel@tonic-gate 
32577c478bd9Sstevel@tonic-gate int
32587c478bd9Sstevel@tonic-gate zonecfg_add_rctl_value(
32597c478bd9Sstevel@tonic-gate 	struct zone_rctltab *tabptr,
32607c478bd9Sstevel@tonic-gate 	struct zone_rctlvaltab *valtabptr)
32617c478bd9Sstevel@tonic-gate {
32627c478bd9Sstevel@tonic-gate 	struct zone_rctlvaltab *last, *old, *new;
32637c478bd9Sstevel@tonic-gate 	rctlblk_t *rctlblk = alloca(rctlblk_size());
32647c478bd9Sstevel@tonic-gate 
32657c478bd9Sstevel@tonic-gate 	last = tabptr->zone_rctl_valptr;
32667c478bd9Sstevel@tonic-gate 	for (old = last; old != NULL; old = old->zone_rctlval_next)
32677c478bd9Sstevel@tonic-gate 		last = old;	/* walk to the end of the list */
32687c478bd9Sstevel@tonic-gate 	new = valtabptr;	/* alloc'd by caller */
32697c478bd9Sstevel@tonic-gate 	new->zone_rctlval_next = NULL;
32707c478bd9Sstevel@tonic-gate 	if (zonecfg_construct_rctlblk(valtabptr, rctlblk) != Z_OK)
32717c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
32727c478bd9Sstevel@tonic-gate 	if (!zonecfg_valid_rctlblk(rctlblk))
32737c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
32747c478bd9Sstevel@tonic-gate 	if (last == NULL)
32757c478bd9Sstevel@tonic-gate 		tabptr->zone_rctl_valptr = new;
32767c478bd9Sstevel@tonic-gate 	else
32777c478bd9Sstevel@tonic-gate 		last->zone_rctlval_next = new;
32787c478bd9Sstevel@tonic-gate 	return (Z_OK);
32797c478bd9Sstevel@tonic-gate }
32807c478bd9Sstevel@tonic-gate 
32817c478bd9Sstevel@tonic-gate int
32827c478bd9Sstevel@tonic-gate zonecfg_remove_rctl_value(
32837c478bd9Sstevel@tonic-gate 	struct zone_rctltab *tabptr,
32847c478bd9Sstevel@tonic-gate 	struct zone_rctlvaltab *valtabptr)
32857c478bd9Sstevel@tonic-gate {
32867c478bd9Sstevel@tonic-gate 	struct zone_rctlvaltab *last, *this, *next;
32877c478bd9Sstevel@tonic-gate 
32887c478bd9Sstevel@tonic-gate 	last = tabptr->zone_rctl_valptr;
32897c478bd9Sstevel@tonic-gate 	for (this = last; this != NULL; this = this->zone_rctlval_next) {
32907c478bd9Sstevel@tonic-gate 		if (strcmp(this->zone_rctlval_priv,
32917c478bd9Sstevel@tonic-gate 		    valtabptr->zone_rctlval_priv) == 0 &&
32927c478bd9Sstevel@tonic-gate 		    strcmp(this->zone_rctlval_limit,
32937c478bd9Sstevel@tonic-gate 		    valtabptr->zone_rctlval_limit) == 0 &&
32947c478bd9Sstevel@tonic-gate 		    strcmp(this->zone_rctlval_action,
32957c478bd9Sstevel@tonic-gate 		    valtabptr->zone_rctlval_action) == 0) {
32967c478bd9Sstevel@tonic-gate 			next = this->zone_rctlval_next;
32977c478bd9Sstevel@tonic-gate 			if (this == tabptr->zone_rctl_valptr)
32987c478bd9Sstevel@tonic-gate 				tabptr->zone_rctl_valptr = next;
32997c478bd9Sstevel@tonic-gate 			else
33007c478bd9Sstevel@tonic-gate 				last->zone_rctlval_next = next;
33017c478bd9Sstevel@tonic-gate 			free(this);
33027c478bd9Sstevel@tonic-gate 			return (Z_OK);
33037c478bd9Sstevel@tonic-gate 		} else
33047c478bd9Sstevel@tonic-gate 			last = this;
33057c478bd9Sstevel@tonic-gate 	}
33067c478bd9Sstevel@tonic-gate 	return (Z_NO_PROPERTY_ID);
33077c478bd9Sstevel@tonic-gate }
33087c478bd9Sstevel@tonic-gate 
3309ff17c8bfSgjelinek void
3310ff17c8bfSgjelinek zonecfg_set_swinv(zone_dochandle_t handle)
3311ff17c8bfSgjelinek {
3312ff17c8bfSgjelinek 	handle->zone_dh_sw_inv = B_TRUE;
3313ff17c8bfSgjelinek }
3314ff17c8bfSgjelinek 
3315ff17c8bfSgjelinek /*
3316ff17c8bfSgjelinek  * Add the pkg to the sw inventory on the handle.
3317ff17c8bfSgjelinek  */
3318ff17c8bfSgjelinek int
3319ff17c8bfSgjelinek zonecfg_add_pkg(zone_dochandle_t handle, char *name, char *version)
3320ff17c8bfSgjelinek {
3321ff17c8bfSgjelinek 	xmlNodePtr newnode;
3322ff17c8bfSgjelinek 	xmlNodePtr cur;
3323ff17c8bfSgjelinek 	int err;
3324ff17c8bfSgjelinek 
3325ff17c8bfSgjelinek 	if ((err = operation_prep(handle)) != Z_OK)
3326ff17c8bfSgjelinek 		return (err);
3327ff17c8bfSgjelinek 
3328ff17c8bfSgjelinek 	cur = handle->zone_dh_cur;
3329ff17c8bfSgjelinek 	newnode = xmlNewTextChild(cur, NULL, DTD_ELEM_PACKAGE, NULL);
3330ff17c8bfSgjelinek 	if ((err = newprop(newnode, DTD_ATTR_NAME, name)) != Z_OK)
3331ff17c8bfSgjelinek 		return (err);
3332ff17c8bfSgjelinek 	if ((err = newprop(newnode, DTD_ATTR_VERSION, version)) != Z_OK)
3333ff17c8bfSgjelinek 		return (err);
3334ff17c8bfSgjelinek 	return (Z_OK);
3335ff17c8bfSgjelinek }
3336ff17c8bfSgjelinek 
3337ff17c8bfSgjelinek int
3338ff17c8bfSgjelinek zonecfg_add_patch(zone_dochandle_t handle, char *id, void **pnode)
3339ff17c8bfSgjelinek {
3340ff17c8bfSgjelinek 	xmlNodePtr node = (xmlNodePtr)*pnode;
3341ff17c8bfSgjelinek 	xmlNodePtr cur;
3342ff17c8bfSgjelinek 	int err;
3343ff17c8bfSgjelinek 
3344ff17c8bfSgjelinek 	if ((err = operation_prep(handle)) != Z_OK)
3345ff17c8bfSgjelinek 		return (err);
3346ff17c8bfSgjelinek 
3347ff17c8bfSgjelinek 	cur = handle->zone_dh_cur;
3348ff17c8bfSgjelinek 	node = xmlNewTextChild(cur, NULL, DTD_ELEM_PATCH, NULL);
3349ff17c8bfSgjelinek 	if ((err = newprop(node, DTD_ATTR_ID, id)) != Z_OK)
3350ff17c8bfSgjelinek 		return (err);
3351ff17c8bfSgjelinek 	*pnode = (void *)node;
3352ff17c8bfSgjelinek 	return (Z_OK);
3353ff17c8bfSgjelinek }
3354ff17c8bfSgjelinek 
3355ff17c8bfSgjelinek int
3356ff17c8bfSgjelinek zonecfg_add_patch_obs(char *id, void *cur)
3357ff17c8bfSgjelinek {
3358ff17c8bfSgjelinek 	xmlNodePtr	node;
3359ff17c8bfSgjelinek 	int err;
3360ff17c8bfSgjelinek 
3361ff17c8bfSgjelinek 	node = xmlNewTextChild((xmlNodePtr)cur, NULL, DTD_ELEM_OBSOLETES, NULL);
3362ff17c8bfSgjelinek 	if ((err = newprop(node, DTD_ATTR_ID, id)) != Z_OK)
3363ff17c8bfSgjelinek 		return (err);
3364ff17c8bfSgjelinek 	return (Z_OK);
3365ff17c8bfSgjelinek }
3366ff17c8bfSgjelinek 
33677c478bd9Sstevel@tonic-gate char *
33687c478bd9Sstevel@tonic-gate zonecfg_strerror(int errnum)
33697c478bd9Sstevel@tonic-gate {
33707c478bd9Sstevel@tonic-gate 	switch (errnum) {
33717c478bd9Sstevel@tonic-gate 	case Z_OK:
33727c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "OK"));
33737c478bd9Sstevel@tonic-gate 	case Z_EMPTY_DOCUMENT:
33747c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Empty document"));
33757c478bd9Sstevel@tonic-gate 	case Z_WRONG_DOC_TYPE:
33767c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Wrong document type"));
33777c478bd9Sstevel@tonic-gate 	case Z_BAD_PROPERTY:
33787c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Bad document property"));
33797c478bd9Sstevel@tonic-gate 	case Z_TEMP_FILE:
33807c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN,
33817c478bd9Sstevel@tonic-gate 		    "Problem creating temporary file"));
33827c478bd9Sstevel@tonic-gate 	case Z_SAVING_FILE:
33837c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Problem saving file"));
33847c478bd9Sstevel@tonic-gate 	case Z_NO_ENTRY:
33857c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "No such entry"));
33867c478bd9Sstevel@tonic-gate 	case Z_BOGUS_ZONE_NAME:
33877c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Bogus zone name"));
33887c478bd9Sstevel@tonic-gate 	case Z_REQD_RESOURCE_MISSING:
33897c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Required resource missing"));
33907c478bd9Sstevel@tonic-gate 	case Z_REQD_PROPERTY_MISSING:
33917c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Required property missing"));
33927c478bd9Sstevel@tonic-gate 	case Z_BAD_HANDLE:
33937c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Bad handle"));
33947c478bd9Sstevel@tonic-gate 	case Z_NOMEM:
33957c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Out of memory"));
33967c478bd9Sstevel@tonic-gate 	case Z_INVAL:
33977c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Invalid argument"));
33987c478bd9Sstevel@tonic-gate 	case Z_ACCES:
33997c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Permission denied"));
34007c478bd9Sstevel@tonic-gate 	case Z_TOO_BIG:
34017c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Argument list too long"));
34027c478bd9Sstevel@tonic-gate 	case Z_MISC_FS:
34037c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN,
34047c478bd9Sstevel@tonic-gate 		    "Miscellaneous file system error"));
34057c478bd9Sstevel@tonic-gate 	case Z_NO_ZONE:
34067c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "No such zone configured"));
34077c478bd9Sstevel@tonic-gate 	case Z_NO_RESOURCE_TYPE:
34087c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "No such resource type"));
34097c478bd9Sstevel@tonic-gate 	case Z_NO_RESOURCE_ID:
34107c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "No such resource with that id"));
34117c478bd9Sstevel@tonic-gate 	case Z_NO_PROPERTY_TYPE:
34127c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "No such property type"));
34137c478bd9Sstevel@tonic-gate 	case Z_NO_PROPERTY_ID:
34147c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "No such property with that id"));
3415087719fdSdp 	case Z_BAD_ZONE_STATE:
34167c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN,
3417087719fdSdp 		    "Zone state is invalid for the requested operation"));
34187c478bd9Sstevel@tonic-gate 	case Z_INVALID_DOCUMENT:
34197c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Invalid document"));
3420087719fdSdp 	case Z_NAME_IN_USE:
3421087719fdSdp 		return (dgettext(TEXT_DOMAIN, "Zone name already in use"));
34227c478bd9Sstevel@tonic-gate 	case Z_NO_SUCH_ID:
34237c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "No such zone ID"));
34247c478bd9Sstevel@tonic-gate 	case Z_UPDATING_INDEX:
34257c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Problem updating index file"));
34267c478bd9Sstevel@tonic-gate 	case Z_LOCKING_FILE:
34277c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Locking index file"));
34287c478bd9Sstevel@tonic-gate 	case Z_UNLOCKING_FILE:
34297c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Unlocking index file"));
34307c478bd9Sstevel@tonic-gate 	case Z_INSUFFICIENT_SPEC:
34317c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Insufficient specification"));
34327c478bd9Sstevel@tonic-gate 	case Z_RESOLVED_PATH:
34337c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Resolved path mismatch"));
34347c478bd9Sstevel@tonic-gate 	case Z_IPV6_ADDR_PREFIX_LEN:
34357c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN,
34367c478bd9Sstevel@tonic-gate 		    "IPv6 address missing required prefix length"));
34377c478bd9Sstevel@tonic-gate 	case Z_BOGUS_ADDRESS:
34387c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN,
34397c478bd9Sstevel@tonic-gate 		    "Neither an IPv4 nor an IPv6 address nor a host name"));
3440ffbafc53Scomay 	case Z_PRIV_PROHIBITED:
3441ffbafc53Scomay 		return (dgettext(TEXT_DOMAIN,
3442ffbafc53Scomay 		    "Specified privilege is prohibited"));
3443ffbafc53Scomay 	case Z_PRIV_REQUIRED:
3444ffbafc53Scomay 		return (dgettext(TEXT_DOMAIN,
3445ffbafc53Scomay 		    "Required privilege is missing"));
3446ffbafc53Scomay 	case Z_PRIV_UNKNOWN:
3447ffbafc53Scomay 		return (dgettext(TEXT_DOMAIN,
3448ffbafc53Scomay 		    "Specified privilege is unknown"));
34499acbbeafSnn35248 	case Z_BRAND_ERROR:
34509acbbeafSnn35248 		return (dgettext(TEXT_DOMAIN,
34519acbbeafSnn35248 		    "Brand-specific error"));
34520209230bSgjelinek 	case Z_INCOMPATIBLE:
34530209230bSgjelinek 		return (dgettext(TEXT_DOMAIN, "Incompatible settings"));
34540209230bSgjelinek 	case Z_ALIAS_DISALLOW:
34550209230bSgjelinek 		return (dgettext(TEXT_DOMAIN,
34560209230bSgjelinek 		    "An incompatible rctl already exists for this property"));
34570209230bSgjelinek 	case Z_CLEAR_DISALLOW:
34580209230bSgjelinek 		return (dgettext(TEXT_DOMAIN,
34590209230bSgjelinek 		    "Clearing this property is not allowed"));
34600209230bSgjelinek 	case Z_POOL:
34610209230bSgjelinek 		return (dgettext(TEXT_DOMAIN, "libpool(3LIB) error"));
34620209230bSgjelinek 	case Z_POOLS_NOT_ACTIVE:
34630209230bSgjelinek 		return (dgettext(TEXT_DOMAIN, "Pools facility not active; "
34640209230bSgjelinek 		    "zone will not be bound to pool"));
34650209230bSgjelinek 	case Z_POOL_ENABLE:
34660209230bSgjelinek 		return (dgettext(TEXT_DOMAIN,
34670209230bSgjelinek 		    "Could not enable pools facility"));
34680209230bSgjelinek 	case Z_NO_POOL:
34690209230bSgjelinek 		return (dgettext(TEXT_DOMAIN,
34700209230bSgjelinek 		    "Pool not found; using default pool"));
34710209230bSgjelinek 	case Z_POOL_CREATE:
34720209230bSgjelinek 		return (dgettext(TEXT_DOMAIN,
34730209230bSgjelinek 		    "Could not create a temporary pool"));
34740209230bSgjelinek 	case Z_POOL_BIND:
34750209230bSgjelinek 		return (dgettext(TEXT_DOMAIN, "Could not bind zone to pool"));
34765679c89fSjv227347 	case Z_HOSTID_FUBAR:
34775679c89fSjv227347 		return (dgettext(TEXT_DOMAIN, "Specified hostid is invalid"));
34782ad45a84Sjv227347 	case Z_SYSTEM:
34792ad45a84Sjv227347 		return (strerror(errno));
34807c478bd9Sstevel@tonic-gate 	default:
34817c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Unknown error"));
34827c478bd9Sstevel@tonic-gate 	}
34837c478bd9Sstevel@tonic-gate }
34847c478bd9Sstevel@tonic-gate 
34857c478bd9Sstevel@tonic-gate /*
34867c478bd9Sstevel@tonic-gate  * Note that the zonecfg_setXent() and zonecfg_endXent() calls are all the
34877c478bd9Sstevel@tonic-gate  * same, as they just turn around and call zonecfg_setent() / zonecfg_endent().
34887c478bd9Sstevel@tonic-gate  */
34897c478bd9Sstevel@tonic-gate 
34907c478bd9Sstevel@tonic-gate static int
34917c478bd9Sstevel@tonic-gate zonecfg_setent(zone_dochandle_t handle)
34927c478bd9Sstevel@tonic-gate {
34937c478bd9Sstevel@tonic-gate 	xmlNodePtr cur;
34947c478bd9Sstevel@tonic-gate 	int err;
34957c478bd9Sstevel@tonic-gate 
34967c478bd9Sstevel@tonic-gate 	if (handle == NULL)
34977c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
34987c478bd9Sstevel@tonic-gate 
34997c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK) {
35007c478bd9Sstevel@tonic-gate 		handle->zone_dh_cur = NULL;
35017c478bd9Sstevel@tonic-gate 		return (err);
35027c478bd9Sstevel@tonic-gate 	}
35037c478bd9Sstevel@tonic-gate 	cur = handle->zone_dh_cur;
35047c478bd9Sstevel@tonic-gate 	cur = cur->xmlChildrenNode;
35057c478bd9Sstevel@tonic-gate 	handle->zone_dh_cur = cur;
35067c478bd9Sstevel@tonic-gate 	return (Z_OK);
35077c478bd9Sstevel@tonic-gate }
35087c478bd9Sstevel@tonic-gate 
35097c478bd9Sstevel@tonic-gate static int
35107c478bd9Sstevel@tonic-gate zonecfg_endent(zone_dochandle_t handle)
35117c478bd9Sstevel@tonic-gate {
35127c478bd9Sstevel@tonic-gate 	if (handle == NULL)
35137c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
35147c478bd9Sstevel@tonic-gate 
35157c478bd9Sstevel@tonic-gate 	handle->zone_dh_cur = handle->zone_dh_top;
35167c478bd9Sstevel@tonic-gate 	return (Z_OK);
35177c478bd9Sstevel@tonic-gate }
35187c478bd9Sstevel@tonic-gate 
35190209230bSgjelinek /*
35200209230bSgjelinek  * Do the work required to manipulate a process through libproc.
35210209230bSgjelinek  * If grab_process() returns no errors (0), then release_process()
35220209230bSgjelinek  * must eventually be called.
35230209230bSgjelinek  *
35240209230bSgjelinek  * Return values:
35250209230bSgjelinek  *      0 Successful creation of agent thread
35260209230bSgjelinek  *      1 Error grabbing
35270209230bSgjelinek  *      2 Error creating agent
35280209230bSgjelinek  */
35290209230bSgjelinek static int
35300209230bSgjelinek grab_process(pr_info_handle_t *p)
35310209230bSgjelinek {
35320209230bSgjelinek 	int ret;
35330209230bSgjelinek 
35340209230bSgjelinek 	if ((p->pr = Pgrab(p->pid, 0, &ret)) != NULL) {
35350209230bSgjelinek 
35360209230bSgjelinek 		if (Psetflags(p->pr, PR_RLC) != 0) {
35370209230bSgjelinek 			Prelease(p->pr, 0);
35380209230bSgjelinek 			return (1);
35390209230bSgjelinek 		}
35400209230bSgjelinek 		if (Pcreate_agent(p->pr) == 0) {
35410209230bSgjelinek 			return (0);
35420209230bSgjelinek 
35430209230bSgjelinek 		} else {
35440209230bSgjelinek 			Prelease(p->pr, 0);
35450209230bSgjelinek 			return (2);
35460209230bSgjelinek 		}
35470209230bSgjelinek 	} else {
35480209230bSgjelinek 		return (1);
35490209230bSgjelinek 	}
35500209230bSgjelinek }
35510209230bSgjelinek 
35520209230bSgjelinek /*
35530209230bSgjelinek  * Release the specified process. This destroys the agent
35540209230bSgjelinek  * and releases the process. If the process is NULL, nothing
35550209230bSgjelinek  * is done. This function should only be called if grab_process()
35560209230bSgjelinek  * has previously been called and returned success.
35570209230bSgjelinek  *
35580209230bSgjelinek  * This function is Pgrab-safe.
35590209230bSgjelinek  */
35600209230bSgjelinek static void
35610209230bSgjelinek release_process(struct ps_prochandle *Pr)
35620209230bSgjelinek {
35630209230bSgjelinek 	if (Pr == NULL)
35640209230bSgjelinek 		return;
35650209230bSgjelinek 
35660209230bSgjelinek 	Pdestroy_agent(Pr);
35670209230bSgjelinek 	Prelease(Pr, 0);
35680209230bSgjelinek }
35690209230bSgjelinek 
35700209230bSgjelinek static boolean_t
35710209230bSgjelinek grab_zone_proc(char *zonename, pr_info_handle_t *p)
35720209230bSgjelinek {
35730209230bSgjelinek 	DIR *dirp;
35740209230bSgjelinek 	struct dirent *dentp;
35750209230bSgjelinek 	zoneid_t zoneid;
35760209230bSgjelinek 	int pid_self;
35770209230bSgjelinek 	psinfo_t psinfo;
35780209230bSgjelinek 
35790209230bSgjelinek 	if (zone_get_id(zonename, &zoneid) != 0)
35800209230bSgjelinek 		return (B_FALSE);
35810209230bSgjelinek 
35820209230bSgjelinek 	pid_self = getpid();
35830209230bSgjelinek 
35840209230bSgjelinek 	if ((dirp = opendir("/proc")) == NULL)
35850209230bSgjelinek 		return (B_FALSE);
35860209230bSgjelinek 
35870209230bSgjelinek 	while (dentp = readdir(dirp)) {
35880209230bSgjelinek 		p->pid = atoi(dentp->d_name);
35890209230bSgjelinek 
35900209230bSgjelinek 		/* Skip self */
35910209230bSgjelinek 		if (p->pid == pid_self)
35920209230bSgjelinek 			continue;
35930209230bSgjelinek 
35940209230bSgjelinek 		if (proc_get_psinfo(p->pid, &psinfo) != 0)
35950209230bSgjelinek 			continue;
35960209230bSgjelinek 
35970209230bSgjelinek 		if (psinfo.pr_zoneid != zoneid)
35980209230bSgjelinek 			continue;
35990209230bSgjelinek 
36000209230bSgjelinek 		/* attempt to grab process */
36010209230bSgjelinek 		if (grab_process(p) != 0)
36020209230bSgjelinek 			continue;
36030209230bSgjelinek 
36040209230bSgjelinek 		if (pr_getzoneid(p->pr) != zoneid) {
36050209230bSgjelinek 			release_process(p->pr);
36060209230bSgjelinek 			continue;
36070209230bSgjelinek 		}
36080209230bSgjelinek 
36090209230bSgjelinek 		(void) closedir(dirp);
36100209230bSgjelinek 		return (B_TRUE);
36110209230bSgjelinek 	}
36120209230bSgjelinek 
36130209230bSgjelinek 	(void) closedir(dirp);
36140209230bSgjelinek 	return (B_FALSE);
36150209230bSgjelinek }
36160209230bSgjelinek 
36170209230bSgjelinek static boolean_t
36180209230bSgjelinek get_priv_rctl(struct ps_prochandle *pr, char *name, rctlblk_t *rblk)
36190209230bSgjelinek {
36200209230bSgjelinek 	if (pr_getrctl(pr, name, NULL, rblk, RCTL_FIRST))
36210209230bSgjelinek 		return (B_FALSE);
36220209230bSgjelinek 
36230209230bSgjelinek 	if (rctlblk_get_privilege(rblk) == RCPRIV_PRIVILEGED)
36240209230bSgjelinek 		return (B_TRUE);
36250209230bSgjelinek 
36260209230bSgjelinek 	while (pr_getrctl(pr, name, rblk, rblk, RCTL_NEXT) == 0) {
36270209230bSgjelinek 		if (rctlblk_get_privilege(rblk) == RCPRIV_PRIVILEGED)
36280209230bSgjelinek 			return (B_TRUE);
36290209230bSgjelinek 	}
36300209230bSgjelinek 
36310209230bSgjelinek 	return (B_FALSE);
36320209230bSgjelinek }
36330209230bSgjelinek 
36340209230bSgjelinek /*
36350209230bSgjelinek  * Apply the current rctl settings to the specified, running zone.
36360209230bSgjelinek  */
36370209230bSgjelinek int
36380209230bSgjelinek zonecfg_apply_rctls(char *zone_name, zone_dochandle_t handle)
36390209230bSgjelinek {
36400209230bSgjelinek 	int err;
36410209230bSgjelinek 	int res = Z_OK;
36420209230bSgjelinek 	rctlblk_t *rblk;
36430209230bSgjelinek 	pr_info_handle_t p;
36440209230bSgjelinek 	struct zone_rctltab rctl;
36450209230bSgjelinek 
36460209230bSgjelinek 	if ((err = zonecfg_setrctlent(handle)) != Z_OK)
36470209230bSgjelinek 		return (err);
36480209230bSgjelinek 
36490209230bSgjelinek 	if ((rblk = (rctlblk_t *)malloc(rctlblk_size())) == NULL) {
36500209230bSgjelinek 		(void) zonecfg_endrctlent(handle);
36510209230bSgjelinek 		return (Z_NOMEM);
36520209230bSgjelinek 	}
36530209230bSgjelinek 
36540209230bSgjelinek 	if (!grab_zone_proc(zone_name, &p)) {
36550209230bSgjelinek 		(void) zonecfg_endrctlent(handle);
36560209230bSgjelinek 		free(rblk);
36570209230bSgjelinek 		return (Z_SYSTEM);
36580209230bSgjelinek 	}
36590209230bSgjelinek 
36600209230bSgjelinek 	while (zonecfg_getrctlent(handle, &rctl) == Z_OK) {
36610209230bSgjelinek 		char *rname;
36620209230bSgjelinek 		struct zone_rctlvaltab *valptr;
36630209230bSgjelinek 
36640209230bSgjelinek 		rname = rctl.zone_rctl_name;
36650209230bSgjelinek 
36660209230bSgjelinek 		/* first delete all current privileged settings for this rctl */
36670209230bSgjelinek 		while (get_priv_rctl(p.pr, rname, rblk)) {
36680209230bSgjelinek 			if (pr_setrctl(p.pr, rname, NULL, rblk, RCTL_DELETE) !=
36690209230bSgjelinek 			    0) {
36700209230bSgjelinek 				res = Z_SYSTEM;
36710209230bSgjelinek 				goto done;
36720209230bSgjelinek 			}
36730209230bSgjelinek 		}
36740209230bSgjelinek 
36750209230bSgjelinek 		/* now set each new value for the rctl */
36760209230bSgjelinek 		for (valptr = rctl.zone_rctl_valptr; valptr != NULL;
36770209230bSgjelinek 		    valptr = valptr->zone_rctlval_next) {
36780209230bSgjelinek 			if ((err = zonecfg_construct_rctlblk(valptr, rblk))
36790209230bSgjelinek 			    != Z_OK) {
36800209230bSgjelinek 				res = errno = err;
36810209230bSgjelinek 				goto done;
36820209230bSgjelinek 			}
36830209230bSgjelinek 
36840209230bSgjelinek 			if (pr_setrctl(p.pr, rname, NULL, rblk, RCTL_INSERT)) {
36850209230bSgjelinek 				res = Z_SYSTEM;
36860209230bSgjelinek 				goto done;
36870209230bSgjelinek 			}
36880209230bSgjelinek 		}
36890209230bSgjelinek 	}
36900209230bSgjelinek 
36910209230bSgjelinek done:
36920209230bSgjelinek 	release_process(p.pr);
36930209230bSgjelinek 	free(rblk);
36940209230bSgjelinek 	(void) zonecfg_endrctlent(handle);
36950209230bSgjelinek 
36960209230bSgjelinek 	return (res);
36970209230bSgjelinek }
36980209230bSgjelinek 
36990209230bSgjelinek static const xmlChar *
37000209230bSgjelinek nm_to_dtd(char *nm)
37010209230bSgjelinek {
37020209230bSgjelinek 	if (strcmp(nm, "device") == 0)
37030209230bSgjelinek 		return (DTD_ELEM_DEVICE);
37040209230bSgjelinek 	if (strcmp(nm, "fs") == 0)
37050209230bSgjelinek 		return (DTD_ELEM_FS);
37060209230bSgjelinek 	if (strcmp(nm, "inherit-pkg-dir") == 0)
37070209230bSgjelinek 		return (DTD_ELEM_IPD);
37080209230bSgjelinek 	if (strcmp(nm, "net") == 0)
37090209230bSgjelinek 		return (DTD_ELEM_NET);
37100209230bSgjelinek 	if (strcmp(nm, "attr") == 0)
37110209230bSgjelinek 		return (DTD_ELEM_ATTR);
37120209230bSgjelinek 	if (strcmp(nm, "rctl") == 0)
37130209230bSgjelinek 		return (DTD_ELEM_RCTL);
37140209230bSgjelinek 	if (strcmp(nm, "dataset") == 0)
37150209230bSgjelinek 		return (DTD_ELEM_DATASET);
37160209230bSgjelinek 
37170209230bSgjelinek 	return (NULL);
37180209230bSgjelinek }
37190209230bSgjelinek 
37200209230bSgjelinek int
37210209230bSgjelinek zonecfg_num_resources(zone_dochandle_t handle, char *rsrc)
37220209230bSgjelinek {
37230209230bSgjelinek 	int num = 0;
37240209230bSgjelinek 	const xmlChar *dtd;
37250209230bSgjelinek 	xmlNodePtr cur;
37260209230bSgjelinek 
37270209230bSgjelinek 	if ((dtd = nm_to_dtd(rsrc)) == NULL)
37280209230bSgjelinek 		return (num);
37290209230bSgjelinek 
37300209230bSgjelinek 	if (zonecfg_setent(handle) != Z_OK)
37310209230bSgjelinek 		return (num);
37320209230bSgjelinek 
37330209230bSgjelinek 	for (cur = handle->zone_dh_cur; cur != NULL; cur = cur->next)
37340209230bSgjelinek 		if (xmlStrcmp(cur->name, dtd) == 0)
37350209230bSgjelinek 			num++;
37360209230bSgjelinek 
37370209230bSgjelinek 	(void) zonecfg_endent(handle);
37380209230bSgjelinek 
37390209230bSgjelinek 	return (num);
37400209230bSgjelinek }
37410209230bSgjelinek 
37420209230bSgjelinek int
37430209230bSgjelinek zonecfg_del_all_resources(zone_dochandle_t handle, char *rsrc)
37440209230bSgjelinek {
37450209230bSgjelinek 	int err;
37460209230bSgjelinek 	const xmlChar *dtd;
37470209230bSgjelinek 	xmlNodePtr cur;
37480209230bSgjelinek 
37490209230bSgjelinek 	if ((dtd = nm_to_dtd(rsrc)) == NULL)
37500209230bSgjelinek 		return (Z_NO_RESOURCE_TYPE);
37510209230bSgjelinek 
37520209230bSgjelinek 	if ((err = zonecfg_setent(handle)) != Z_OK)
37530209230bSgjelinek 		return (err);
37540209230bSgjelinek 
37550209230bSgjelinek 	cur = handle->zone_dh_cur;
37560209230bSgjelinek 	while (cur != NULL) {
37570209230bSgjelinek 		xmlNodePtr tmp;
37580209230bSgjelinek 
37590209230bSgjelinek 		if (xmlStrcmp(cur->name, dtd)) {
37600209230bSgjelinek 			cur = cur->next;
37610209230bSgjelinek 			continue;
37620209230bSgjelinek 		}
37630209230bSgjelinek 
37640209230bSgjelinek 		tmp = cur->next;
37650209230bSgjelinek 		xmlUnlinkNode(cur);
37660209230bSgjelinek 		xmlFreeNode(cur);
37670209230bSgjelinek 		cur = tmp;
37680209230bSgjelinek 	}
37690209230bSgjelinek 
37700209230bSgjelinek 	(void) zonecfg_endent(handle);
37710209230bSgjelinek 	return (Z_OK);
37720209230bSgjelinek }
37730209230bSgjelinek 
37740209230bSgjelinek static boolean_t
37750209230bSgjelinek valid_uint(char *s, uint64_t *n)
37760209230bSgjelinek {
37770209230bSgjelinek 	char *endp;
37780209230bSgjelinek 
37790209230bSgjelinek 	/* strtoull accepts '-'?! so we want to flag that as an error */
37800209230bSgjelinek 	if (strchr(s, '-') != NULL)
37810209230bSgjelinek 		return (B_FALSE);
37820209230bSgjelinek 
37830209230bSgjelinek 	errno = 0;
37840209230bSgjelinek 	*n = strtoull(s, &endp, 10);
37850209230bSgjelinek 
37860209230bSgjelinek 	if (errno != 0 || *endp != '\0')
37870209230bSgjelinek 		return (B_FALSE);
37880209230bSgjelinek 	return (B_TRUE);
37890209230bSgjelinek }
37900209230bSgjelinek 
37910209230bSgjelinek /*
37920209230bSgjelinek  * Convert a string representing a number (possibly a fraction) into an integer.
37930209230bSgjelinek  * The string can have a modifier (K, M, G or T).   The modifiers are treated
37940209230bSgjelinek  * as powers of two (not 10).
37950209230bSgjelinek  */
37960209230bSgjelinek int
37970209230bSgjelinek zonecfg_str_to_bytes(char *str, uint64_t *bytes)
37980209230bSgjelinek {
37990209230bSgjelinek 	long double val;
38000209230bSgjelinek 	char *unitp;
38010209230bSgjelinek 	uint64_t scale;
38020209230bSgjelinek 
38030209230bSgjelinek 	if ((val = strtold(str, &unitp)) < 0)
38040209230bSgjelinek 		return (-1);
38050209230bSgjelinek 
38060209230bSgjelinek 	/* remove any leading white space from units string */
38070209230bSgjelinek 	while (isspace(*unitp) != 0)
38080209230bSgjelinek 		++unitp;
38090209230bSgjelinek 
38100209230bSgjelinek 	/* if no units explicitly set, error */
38110209230bSgjelinek 	if (unitp == NULL || *unitp == '\0') {
38120209230bSgjelinek 		scale = 1;
38130209230bSgjelinek 	} else {
38140209230bSgjelinek 		int i;
38150209230bSgjelinek 		char *units[] = {"K", "M", "G", "T", NULL};
38160209230bSgjelinek 
38170209230bSgjelinek 		scale = 1024;
38180209230bSgjelinek 
38190209230bSgjelinek 		/* update scale based on units */
38200209230bSgjelinek 		for (i = 0; units[i] != NULL; i++) {
38210209230bSgjelinek 			if (strcasecmp(unitp, units[i]) == 0)
38220209230bSgjelinek 				break;
38230209230bSgjelinek 			scale <<= 10;
38240209230bSgjelinek 		}
38250209230bSgjelinek 
38260209230bSgjelinek 		if (units[i] == NULL)
38270209230bSgjelinek 			return (-1);
38280209230bSgjelinek 	}
38290209230bSgjelinek 
38300209230bSgjelinek 	*bytes = (uint64_t)(val * scale);
38310209230bSgjelinek 	return (0);
38320209230bSgjelinek }
38330209230bSgjelinek 
38340209230bSgjelinek boolean_t
38350209230bSgjelinek zonecfg_valid_ncpus(char *lowstr, char *highstr)
38360209230bSgjelinek {
38370209230bSgjelinek 	uint64_t low, high;
38380209230bSgjelinek 
38390209230bSgjelinek 	if (!valid_uint(lowstr, &low) || !valid_uint(highstr, &high) ||
38400209230bSgjelinek 	    low < 1 || low > high)
38410209230bSgjelinek 		return (B_FALSE);
38420209230bSgjelinek 
38430209230bSgjelinek 	return (B_TRUE);
38440209230bSgjelinek }
38450209230bSgjelinek 
38460209230bSgjelinek boolean_t
38470209230bSgjelinek zonecfg_valid_importance(char *impstr)
38480209230bSgjelinek {
38490209230bSgjelinek 	uint64_t num;
38500209230bSgjelinek 
38510209230bSgjelinek 	if (!valid_uint(impstr, &num))
38520209230bSgjelinek 		return (B_FALSE);
38530209230bSgjelinek 
38540209230bSgjelinek 	return (B_TRUE);
38550209230bSgjelinek }
38560209230bSgjelinek 
38570209230bSgjelinek boolean_t
38580209230bSgjelinek zonecfg_valid_alias_limit(char *name, char *limitstr, uint64_t *limit)
38590209230bSgjelinek {
38600209230bSgjelinek 	int i;
38610209230bSgjelinek 
38620209230bSgjelinek 	for (i = 0; aliases[i].shortname != NULL; i++)
38630209230bSgjelinek 		if (strcmp(name, aliases[i].shortname) == 0)
38640209230bSgjelinek 			break;
38650209230bSgjelinek 
38660209230bSgjelinek 	if (aliases[i].shortname == NULL)
38670209230bSgjelinek 		return (B_FALSE);
38680209230bSgjelinek 
38690209230bSgjelinek 	if (!valid_uint(limitstr, limit) || *limit < aliases[i].low_limit)
38700209230bSgjelinek 		return (B_FALSE);
38710209230bSgjelinek 
38720209230bSgjelinek 	return (B_TRUE);
38730209230bSgjelinek }
38740209230bSgjelinek 
38750209230bSgjelinek boolean_t
38760209230bSgjelinek zonecfg_valid_memlimit(char *memstr, uint64_t *mem_val)
38770209230bSgjelinek {
38780209230bSgjelinek 	if (zonecfg_str_to_bytes(memstr, mem_val) != 0)
38790209230bSgjelinek 		return (B_FALSE);
38800209230bSgjelinek 
38810209230bSgjelinek 	return (B_TRUE);
38820209230bSgjelinek }
38830209230bSgjelinek 
38840209230bSgjelinek static int
38850209230bSgjelinek zerr_pool(char *pool_err, int err_size, int res)
38860209230bSgjelinek {
38870209230bSgjelinek 	(void) strlcpy(pool_err, pool_strerror(pool_error()), err_size);
38880209230bSgjelinek 	return (res);
38890209230bSgjelinek }
38900209230bSgjelinek 
38910209230bSgjelinek static int
38920209230bSgjelinek create_tmp_pset(char *pool_err, int err_size, pool_conf_t *pconf, pool_t *pool,
38930209230bSgjelinek     char *name, int min, int max)
38940209230bSgjelinek {
38950209230bSgjelinek 	pool_resource_t *res;
38960209230bSgjelinek 	pool_elem_t *elem;
38970209230bSgjelinek 	pool_value_t *val;
38980209230bSgjelinek 
38990209230bSgjelinek 	if ((res = pool_resource_create(pconf, "pset", name)) == NULL)
39000209230bSgjelinek 		return (zerr_pool(pool_err, err_size, Z_POOL));
39010209230bSgjelinek 
39020209230bSgjelinek 	if (pool_associate(pconf, pool, res) != PO_SUCCESS)
39030209230bSgjelinek 		return (zerr_pool(pool_err, err_size, Z_POOL));
39040209230bSgjelinek 
39050209230bSgjelinek 	if ((elem = pool_resource_to_elem(pconf, res)) == NULL)
39060209230bSgjelinek 		return (zerr_pool(pool_err, err_size, Z_POOL));
39070209230bSgjelinek 
39080209230bSgjelinek 	if ((val = pool_value_alloc()) == NULL)
39090209230bSgjelinek 		return (zerr_pool(pool_err, err_size, Z_POOL));
39100209230bSgjelinek 
39110209230bSgjelinek 	/* set the maximum number of cpus for the pset */
39120209230bSgjelinek 	pool_value_set_uint64(val, (uint64_t)max);
39130209230bSgjelinek 
39140209230bSgjelinek 	if (pool_put_property(pconf, elem, "pset.max", val) != PO_SUCCESS) {
39150209230bSgjelinek 		pool_value_free(val);
39160209230bSgjelinek 		return (zerr_pool(pool_err, err_size, Z_POOL));
39170209230bSgjelinek 	}
39180209230bSgjelinek 
39190209230bSgjelinek 	/* set the minimum number of cpus for the pset */
39200209230bSgjelinek 	pool_value_set_uint64(val, (uint64_t)min);
39210209230bSgjelinek 
39220209230bSgjelinek 	if (pool_put_property(pconf, elem, "pset.min", val) != PO_SUCCESS) {
39230209230bSgjelinek 		pool_value_free(val);
39240209230bSgjelinek 		return (zerr_pool(pool_err, err_size, Z_POOL));
39250209230bSgjelinek 	}
39260209230bSgjelinek 
39270209230bSgjelinek 	pool_value_free(val);
39280209230bSgjelinek 
39290209230bSgjelinek 	return (Z_OK);
39300209230bSgjelinek }
39310209230bSgjelinek 
39320209230bSgjelinek static int
39330209230bSgjelinek create_tmp_pool(char *pool_err, int err_size, pool_conf_t *pconf, char *name,
39340209230bSgjelinek     struct zone_psettab *pset_tab)
39350209230bSgjelinek {
39360209230bSgjelinek 	pool_t *pool;
39370209230bSgjelinek 	int res = Z_OK;
39380209230bSgjelinek 
39390209230bSgjelinek 	/* create a temporary pool configuration */
39400209230bSgjelinek 	if (pool_conf_open(pconf, NULL, PO_TEMP) != PO_SUCCESS) {
39410209230bSgjelinek 		res = zerr_pool(pool_err, err_size, Z_POOL);
39420209230bSgjelinek 		return (res);
39430209230bSgjelinek 	}
39440209230bSgjelinek 
39450209230bSgjelinek 	if ((pool = pool_create(pconf, name)) == NULL) {
39460209230bSgjelinek 		res = zerr_pool(pool_err, err_size, Z_POOL_CREATE);
39470209230bSgjelinek 		goto done;
39480209230bSgjelinek 	}
39490209230bSgjelinek 
39500209230bSgjelinek 	/* set pool importance */
39510209230bSgjelinek 	if (pset_tab->zone_importance[0] != '\0') {
39520209230bSgjelinek 		pool_elem_t *elem;
39530209230bSgjelinek 		pool_value_t *val;
39540209230bSgjelinek 
39550209230bSgjelinek 		if ((elem = pool_to_elem(pconf, pool)) == NULL) {
39560209230bSgjelinek 			res = zerr_pool(pool_err, err_size, Z_POOL);
39570209230bSgjelinek 			goto done;
39580209230bSgjelinek 		}
39590209230bSgjelinek 
39600209230bSgjelinek 		if ((val = pool_value_alloc()) == NULL) {
39610209230bSgjelinek 			res = zerr_pool(pool_err, err_size, Z_POOL);
39620209230bSgjelinek 			goto done;
39630209230bSgjelinek 		}
39640209230bSgjelinek 
39650209230bSgjelinek 		pool_value_set_int64(val,
39660209230bSgjelinek 		    (int64_t)atoi(pset_tab->zone_importance));
39670209230bSgjelinek 
39680209230bSgjelinek 		if (pool_put_property(pconf, elem, "pool.importance", val)
39690209230bSgjelinek 		    != PO_SUCCESS) {
39700209230bSgjelinek 			res = zerr_pool(pool_err, err_size, Z_POOL);
39710209230bSgjelinek 			pool_value_free(val);
39720209230bSgjelinek 			goto done;
39730209230bSgjelinek 		}
39740209230bSgjelinek 
39750209230bSgjelinek 		pool_value_free(val);
39760209230bSgjelinek 	}
39770209230bSgjelinek 
39780209230bSgjelinek 	if ((res = create_tmp_pset(pool_err, err_size, pconf, pool, name,
39790209230bSgjelinek 	    atoi(pset_tab->zone_ncpu_min),
39800209230bSgjelinek 	    atoi(pset_tab->zone_ncpu_max))) != Z_OK)
39810209230bSgjelinek 		goto done;
39820209230bSgjelinek 
39830209230bSgjelinek 	/* validation */
39840209230bSgjelinek 	if (pool_conf_status(pconf) == POF_INVALID) {
39850209230bSgjelinek 		res = zerr_pool(pool_err, err_size, Z_POOL);
39860209230bSgjelinek 		goto done;
39870209230bSgjelinek 	}
39880209230bSgjelinek 
39890209230bSgjelinek 	/*
39900209230bSgjelinek 	 * This validation is the one we expect to fail if the user specified
39910209230bSgjelinek 	 * an invalid configuration (too many cpus) for this system.
39920209230bSgjelinek 	 */
39930209230bSgjelinek 	if (pool_conf_validate(pconf, POV_RUNTIME) != PO_SUCCESS) {
39940209230bSgjelinek 		res = zerr_pool(pool_err, err_size, Z_POOL_CREATE);
39950209230bSgjelinek 		goto done;
39960209230bSgjelinek 	}
39970209230bSgjelinek 
39980209230bSgjelinek 	/*
39990209230bSgjelinek 	 * Commit the dynamic configuration but not the pool configuration
40000209230bSgjelinek 	 * file.
40010209230bSgjelinek 	 */
40020209230bSgjelinek 	if (pool_conf_commit(pconf, 1) != PO_SUCCESS)
40030209230bSgjelinek 		res = zerr_pool(pool_err, err_size, Z_POOL);
40040209230bSgjelinek 
40050209230bSgjelinek done:
40060209230bSgjelinek 	(void) pool_conf_close(pconf);
40070209230bSgjelinek 	return (res);
40080209230bSgjelinek }
40090209230bSgjelinek 
40100209230bSgjelinek static int
40110209230bSgjelinek get_running_tmp_pset(pool_conf_t *pconf, pool_t *pool, pool_resource_t *pset,
40120209230bSgjelinek     struct zone_psettab *pset_tab)
40130209230bSgjelinek {
40140209230bSgjelinek 	int nfound = 0;
40150209230bSgjelinek 	pool_elem_t *pe;
40160209230bSgjelinek 	pool_value_t *pv = pool_value_alloc();
40170209230bSgjelinek 	uint64_t val_uint;
40180209230bSgjelinek 
40190209230bSgjelinek 	if (pool != NULL) {
40200209230bSgjelinek 		pe = pool_to_elem(pconf, pool);
40210209230bSgjelinek 		if (pool_get_property(pconf, pe, "pool.importance", pv)
40220209230bSgjelinek 		    != POC_INVAL) {
40230209230bSgjelinek 			int64_t val_int;
40240209230bSgjelinek 
40250209230bSgjelinek 			(void) pool_value_get_int64(pv, &val_int);
40260209230bSgjelinek 			(void) snprintf(pset_tab->zone_importance,
40270209230bSgjelinek 			    sizeof (pset_tab->zone_importance), "%d", val_int);
40280209230bSgjelinek 			nfound++;
40290209230bSgjelinek 		}
40300209230bSgjelinek 	}
40310209230bSgjelinek 
40320209230bSgjelinek 	if (pset != NULL) {
40330209230bSgjelinek 		pe = pool_resource_to_elem(pconf, pset);
40340209230bSgjelinek 		if (pool_get_property(pconf, pe, "pset.min", pv) != POC_INVAL) {
40350209230bSgjelinek 			(void) pool_value_get_uint64(pv, &val_uint);
40360209230bSgjelinek 			(void) snprintf(pset_tab->zone_ncpu_min,
40370209230bSgjelinek 			    sizeof (pset_tab->zone_ncpu_min), "%u", val_uint);
40380209230bSgjelinek 			nfound++;
40390209230bSgjelinek 		}
40400209230bSgjelinek 
40410209230bSgjelinek 		if (pool_get_property(pconf, pe, "pset.max", pv) != POC_INVAL) {
40420209230bSgjelinek 			(void) pool_value_get_uint64(pv, &val_uint);
40430209230bSgjelinek 			(void) snprintf(pset_tab->zone_ncpu_max,
40440209230bSgjelinek 			    sizeof (pset_tab->zone_ncpu_max), "%u", val_uint);
40450209230bSgjelinek 			nfound++;
40460209230bSgjelinek 		}
40470209230bSgjelinek 	}
40480209230bSgjelinek 
40490209230bSgjelinek 	pool_value_free(pv);
40500209230bSgjelinek 
40510209230bSgjelinek 	if (nfound == 3)
40520209230bSgjelinek 		return (PO_SUCCESS);
40530209230bSgjelinek 
40540209230bSgjelinek 	return (PO_FAIL);
40550209230bSgjelinek }
40560209230bSgjelinek 
40570209230bSgjelinek /*
40580209230bSgjelinek  * Determine if a tmp pool is configured and if so, if the configuration is
40590209230bSgjelinek  * still valid or if it has been changed since the tmp pool was created.
40600209230bSgjelinek  * If the tmp pool configuration is no longer valid, delete the tmp pool.
40610209230bSgjelinek  *
40620209230bSgjelinek  * Set *valid=B_TRUE if there is an existing, valid tmp pool configuration.
40630209230bSgjelinek  */
40640209230bSgjelinek static int
40650209230bSgjelinek verify_del_tmp_pool(pool_conf_t *pconf, char *tmp_name, char *pool_err,
40660209230bSgjelinek     int err_size, struct zone_psettab *pset_tab, boolean_t *exists)
40670209230bSgjelinek {
40680209230bSgjelinek 	int res = Z_OK;
40690209230bSgjelinek 	pool_t *pool;
40700209230bSgjelinek 	pool_resource_t *pset;
40710209230bSgjelinek 	struct zone_psettab pset_current;
40720209230bSgjelinek 
40730209230bSgjelinek 	*exists = B_FALSE;
40740209230bSgjelinek 
40750209230bSgjelinek 	if (pool_conf_open(pconf, pool_dynamic_location(), PO_RDWR)
40760209230bSgjelinek 	    != PO_SUCCESS) {
40770209230bSgjelinek 		res = zerr_pool(pool_err, err_size, Z_POOL);
40780209230bSgjelinek 		return (res);
40790209230bSgjelinek 	}
40800209230bSgjelinek 
40810209230bSgjelinek 	pool = pool_get_pool(pconf, tmp_name);
40820209230bSgjelinek 	pset = pool_get_resource(pconf, "pset", tmp_name);
40830209230bSgjelinek 
40840209230bSgjelinek 	if (pool == NULL && pset == NULL) {
40850209230bSgjelinek 		/* no tmp pool configured */
40860209230bSgjelinek 		goto done;
40870209230bSgjelinek 	}
40880209230bSgjelinek 
40890209230bSgjelinek 	/*
40900209230bSgjelinek 	 * If an existing tmp pool for this zone is configured with the proper
40910209230bSgjelinek 	 * settings, then the tmp pool is valid.
40920209230bSgjelinek 	 */
40930209230bSgjelinek 	if (get_running_tmp_pset(pconf, pool, pset, &pset_current)
40940209230bSgjelinek 	    == PO_SUCCESS &&
40950209230bSgjelinek 	    strcmp(pset_tab->zone_ncpu_min,
40960209230bSgjelinek 	    pset_current.zone_ncpu_min) == 0 &&
40970209230bSgjelinek 	    strcmp(pset_tab->zone_ncpu_max,
40980209230bSgjelinek 	    pset_current.zone_ncpu_max) == 0 &&
40990209230bSgjelinek 	    strcmp(pset_tab->zone_importance,
41000209230bSgjelinek 	    pset_current.zone_importance) == 0) {
41010209230bSgjelinek 		*exists = B_TRUE;
41020209230bSgjelinek 
41030209230bSgjelinek 	} else {
41040209230bSgjelinek 		/*
41050209230bSgjelinek 		 * An out-of-date tmp pool configuration exists.  Delete it
41060209230bSgjelinek 		 * so that we can create the correct tmp pool config.
41070209230bSgjelinek 		 */
41080209230bSgjelinek 		if (pset != NULL &&
41090209230bSgjelinek 		    pool_resource_destroy(pconf, pset) != PO_SUCCESS) {
41100209230bSgjelinek 			res = zerr_pool(pool_err, err_size, Z_POOL);
41110209230bSgjelinek 			goto done;
41120209230bSgjelinek 		}
41130209230bSgjelinek 
41140209230bSgjelinek 		if (pool != NULL &&
41150209230bSgjelinek 		    pool_destroy(pconf, pool) != PO_SUCCESS) {
41160209230bSgjelinek 			res = zerr_pool(pool_err, err_size, Z_POOL);
41170209230bSgjelinek 			goto done;
41180209230bSgjelinek 		}
41190209230bSgjelinek 
41200209230bSgjelinek 		/* commit dynamic config */
41210209230bSgjelinek 		if (pool_conf_commit(pconf, 0) != PO_SUCCESS)
41220209230bSgjelinek 			res = zerr_pool(pool_err, err_size, Z_POOL);
41230209230bSgjelinek 	}
41240209230bSgjelinek 
41250209230bSgjelinek done:
41260209230bSgjelinek 	(void) pool_conf_close(pconf);
41270209230bSgjelinek 
41280209230bSgjelinek 	return (res);
41290209230bSgjelinek }
41300209230bSgjelinek 
41310209230bSgjelinek /*
41320209230bSgjelinek  * Destroy any existing tmp pool.
41330209230bSgjelinek  */
41340209230bSgjelinek int
41350209230bSgjelinek zonecfg_destroy_tmp_pool(char *zone_name, char *pool_err, int err_size)
41360209230bSgjelinek {
41370209230bSgjelinek 	int status;
41380209230bSgjelinek 	int res = Z_OK;
41390209230bSgjelinek 	pool_conf_t *pconf;
41400209230bSgjelinek 	pool_t *pool;
41410209230bSgjelinek 	pool_resource_t *pset;
41420209230bSgjelinek 	char tmp_name[MAX_TMP_POOL_NAME];
41430209230bSgjelinek 
41440209230bSgjelinek 	/* if pools not enabled then nothing to do */
41450209230bSgjelinek 	if (pool_get_status(&status) != PO_SUCCESS || status != POOL_ENABLED)
41460209230bSgjelinek 		return (Z_OK);
41470209230bSgjelinek 
41480209230bSgjelinek 	if ((pconf = pool_conf_alloc()) == NULL)
41490209230bSgjelinek 		return (zerr_pool(pool_err, err_size, Z_POOL));
41500209230bSgjelinek 
41510209230bSgjelinek 	(void) snprintf(tmp_name, sizeof (tmp_name), TMP_POOL_NAME, zone_name);
41520209230bSgjelinek 
41530209230bSgjelinek 	if (pool_conf_open(pconf, pool_dynamic_location(), PO_RDWR)
41540209230bSgjelinek 	    != PO_SUCCESS) {
41550209230bSgjelinek 		res = zerr_pool(pool_err, err_size, Z_POOL);
41560209230bSgjelinek 		pool_conf_free(pconf);
41570209230bSgjelinek 		return (res);
41580209230bSgjelinek 	}
41590209230bSgjelinek 
41600209230bSgjelinek 	pool = pool_get_pool(pconf, tmp_name);
41610209230bSgjelinek 	pset = pool_get_resource(pconf, "pset", tmp_name);
41620209230bSgjelinek 
41630209230bSgjelinek 	if (pool == NULL && pset == NULL) {
41640209230bSgjelinek 		/* nothing to destroy, we're done */
41650209230bSgjelinek 		goto done;
41660209230bSgjelinek 	}
41670209230bSgjelinek 
41680209230bSgjelinek 	if (pset != NULL && pool_resource_destroy(pconf, pset) != PO_SUCCESS) {
41690209230bSgjelinek 		res = zerr_pool(pool_err, err_size, Z_POOL);
41700209230bSgjelinek 		goto done;
41710209230bSgjelinek 	}
41720209230bSgjelinek 
41730209230bSgjelinek 	if (pool != NULL && pool_destroy(pconf, pool) != PO_SUCCESS) {
41740209230bSgjelinek 		res = zerr_pool(pool_err, err_size, Z_POOL);
41750209230bSgjelinek 		goto done;
41760209230bSgjelinek 	}
41770209230bSgjelinek 
41780209230bSgjelinek 	/* commit dynamic config */
41790209230bSgjelinek 	if (pool_conf_commit(pconf, 0) != PO_SUCCESS)
41800209230bSgjelinek 		res = zerr_pool(pool_err, err_size, Z_POOL);
41810209230bSgjelinek 
41820209230bSgjelinek done:
41830209230bSgjelinek 	(void) pool_conf_close(pconf);
41840209230bSgjelinek 	pool_conf_free(pconf);
41850209230bSgjelinek 
41860209230bSgjelinek 	return (res);
41870209230bSgjelinek }
41880209230bSgjelinek 
41890209230bSgjelinek /*
41900209230bSgjelinek  * Attempt to bind to a tmp pool for this zone.  If there is no tmp pool
41910209230bSgjelinek  * configured, we just return Z_OK.
41920209230bSgjelinek  *
41930209230bSgjelinek  * We either attempt to create the tmp pool for this zone or rebind to an
41940209230bSgjelinek  * existing tmp pool for this zone.
41950209230bSgjelinek  *
41960209230bSgjelinek  * Rebinding is used when a zone with a tmp pool reboots so that we don't have
41970209230bSgjelinek  * to recreate the tmp pool.  To do this we need to be sure we work correctly
41980209230bSgjelinek  * for the following cases:
41990209230bSgjelinek  *
42000209230bSgjelinek  *	- there is an existing, properly configured tmp pool.
42010209230bSgjelinek  *	- zonecfg added tmp pool after zone was booted, must now create.
42020209230bSgjelinek  *	- zonecfg updated tmp pool config after zone was booted, in this case
42030209230bSgjelinek  *	  we destroy the old tmp pool and create a new one.
42040209230bSgjelinek  */
42050209230bSgjelinek int
42060209230bSgjelinek zonecfg_bind_tmp_pool(zone_dochandle_t handle, zoneid_t zoneid, char *pool_err,
42070209230bSgjelinek     int err_size)
42080209230bSgjelinek {
42090209230bSgjelinek 	struct zone_psettab pset_tab;
42100209230bSgjelinek 	int err;
42110209230bSgjelinek 	int status;
42120209230bSgjelinek 	pool_conf_t *pconf;
42130209230bSgjelinek 	boolean_t exists;
42140209230bSgjelinek 	char zone_name[ZONENAME_MAX];
42150209230bSgjelinek 	char tmp_name[MAX_TMP_POOL_NAME];
42160209230bSgjelinek 
42170209230bSgjelinek 	(void) getzonenamebyid(zoneid, zone_name, sizeof (zone_name));
42180209230bSgjelinek 
42190209230bSgjelinek 	err = zonecfg_lookup_pset(handle, &pset_tab);
42200209230bSgjelinek 
42210209230bSgjelinek 	/* if no temporary pool configured, we're done */
42220209230bSgjelinek 	if (err == Z_NO_ENTRY)
42230209230bSgjelinek 		return (Z_OK);
42240209230bSgjelinek 
42250209230bSgjelinek 	/*
42260209230bSgjelinek 	 * importance might not have a value but we need to validate it here,
42270209230bSgjelinek 	 * so set the default.
42280209230bSgjelinek 	 */
42290209230bSgjelinek 	if (pset_tab.zone_importance[0] == '\0')
42300209230bSgjelinek 		(void) strlcpy(pset_tab.zone_importance, "1",
42310209230bSgjelinek 		    sizeof (pset_tab.zone_importance));
42320209230bSgjelinek 
42330209230bSgjelinek 	/* if pools not enabled, enable them now */
42340209230bSgjelinek 	if (pool_get_status(&status) != PO_SUCCESS || status != POOL_ENABLED) {
42350209230bSgjelinek 		if (pool_set_status(POOL_ENABLED) != PO_SUCCESS)
42360209230bSgjelinek 			return (Z_POOL_ENABLE);
42370209230bSgjelinek 	}
42380209230bSgjelinek 
42390209230bSgjelinek 	if ((pconf = pool_conf_alloc()) == NULL)
42400209230bSgjelinek 		return (zerr_pool(pool_err, err_size, Z_POOL));
42410209230bSgjelinek 
42420209230bSgjelinek 	(void) snprintf(tmp_name, sizeof (tmp_name), TMP_POOL_NAME, zone_name);
42430209230bSgjelinek 
42440209230bSgjelinek 	/*
42450209230bSgjelinek 	 * Check if a valid tmp pool/pset already exists.  If so, we just
42460209230bSgjelinek 	 * reuse it.
42470209230bSgjelinek 	 */
42480209230bSgjelinek 	if ((err = verify_del_tmp_pool(pconf, tmp_name, pool_err, err_size,
42490209230bSgjelinek 	    &pset_tab, &exists)) != Z_OK) {
42500209230bSgjelinek 		pool_conf_free(pconf);
42510209230bSgjelinek 		return (err);
42520209230bSgjelinek 	}
42530209230bSgjelinek 
42540209230bSgjelinek 	if (!exists)
42550209230bSgjelinek 		err = create_tmp_pool(pool_err, err_size, pconf, tmp_name,
42560209230bSgjelinek 		    &pset_tab);
42570209230bSgjelinek 
42580209230bSgjelinek 	pool_conf_free(pconf);
42590209230bSgjelinek 
42600209230bSgjelinek 	if (err != Z_OK)
42610209230bSgjelinek 		return (err);
42620209230bSgjelinek 
42630209230bSgjelinek 	/* Bind the zone to the pool. */
42640209230bSgjelinek 	if (pool_set_binding(tmp_name, P_ZONEID, zoneid) != PO_SUCCESS)
42650209230bSgjelinek 		return (zerr_pool(pool_err, err_size, Z_POOL_BIND));
42660209230bSgjelinek 
42670209230bSgjelinek 	return (Z_OK);
42680209230bSgjelinek }
42690209230bSgjelinek 
42700209230bSgjelinek /*
42710209230bSgjelinek  * Attempt to bind to a permanent pool for this zone.  If there is no
42720209230bSgjelinek  * permanent pool configured, we just return Z_OK.
42730209230bSgjelinek  */
42740209230bSgjelinek int
42750209230bSgjelinek zonecfg_bind_pool(zone_dochandle_t handle, zoneid_t zoneid, char *pool_err,
42760209230bSgjelinek     int err_size)
42770209230bSgjelinek {
42780209230bSgjelinek 	pool_conf_t *poolconf;
42790209230bSgjelinek 	pool_t *pool;
42800209230bSgjelinek 	char poolname[MAXPATHLEN];
42810209230bSgjelinek 	int status;
42820209230bSgjelinek 	int error;
42830209230bSgjelinek 
42840209230bSgjelinek 	/*
42850209230bSgjelinek 	 * Find the pool mentioned in the zone configuration, and bind to it.
42860209230bSgjelinek 	 */
42870209230bSgjelinek 	error = zonecfg_get_pool(handle, poolname, sizeof (poolname));
42880209230bSgjelinek 	if (error == Z_NO_ENTRY || (error == Z_OK && strlen(poolname) == 0)) {
42890209230bSgjelinek 		/*
42900209230bSgjelinek 		 * The property is not set on the zone, so the pool
42910209230bSgjelinek 		 * should be bound to the default pool.  But that's
42920209230bSgjelinek 		 * already done by the kernel, so we can just return.
42930209230bSgjelinek 		 */
42940209230bSgjelinek 		return (Z_OK);
42950209230bSgjelinek 	}
42960209230bSgjelinek 	if (error != Z_OK) {
42970209230bSgjelinek 		/*
42980209230bSgjelinek 		 * Not an error, even though it shouldn't be happening.
42990209230bSgjelinek 		 */
43000209230bSgjelinek 		return (Z_OK);
43010209230bSgjelinek 	}
43020209230bSgjelinek 	/*
43030209230bSgjelinek 	 * Don't do anything if pools aren't enabled.
43040209230bSgjelinek 	 */
43050209230bSgjelinek 	if (pool_get_status(&status) != PO_SUCCESS || status != POOL_ENABLED)
43060209230bSgjelinek 		return (Z_POOLS_NOT_ACTIVE);
43070209230bSgjelinek 
43080209230bSgjelinek 	/*
43090209230bSgjelinek 	 * Try to provide a sane error message if the requested pool doesn't
43100209230bSgjelinek 	 * exist.
43110209230bSgjelinek 	 */
43120209230bSgjelinek 	if ((poolconf = pool_conf_alloc()) == NULL)
43130209230bSgjelinek 		return (zerr_pool(pool_err, err_size, Z_POOL));
43140209230bSgjelinek 
43150209230bSgjelinek 	if (pool_conf_open(poolconf, pool_dynamic_location(), PO_RDONLY) !=
43160209230bSgjelinek 	    PO_SUCCESS) {
43170209230bSgjelinek 		pool_conf_free(poolconf);
43180209230bSgjelinek 		return (zerr_pool(pool_err, err_size, Z_POOL));
43190209230bSgjelinek 	}
43200209230bSgjelinek 	pool = pool_get_pool(poolconf, poolname);
43210209230bSgjelinek 	(void) pool_conf_close(poolconf);
43220209230bSgjelinek 	pool_conf_free(poolconf);
43230209230bSgjelinek 	if (pool == NULL)
43240209230bSgjelinek 		return (Z_NO_POOL);
43250209230bSgjelinek 
43260209230bSgjelinek 	/*
43270209230bSgjelinek 	 * Bind the zone to the pool.
43280209230bSgjelinek 	 */
43290209230bSgjelinek 	if (pool_set_binding(poolname, P_ZONEID, zoneid) != PO_SUCCESS) {
43300209230bSgjelinek 		/* if bind fails, return poolname for the error msg */
43310209230bSgjelinek 		(void) strlcpy(pool_err, poolname, err_size);
43320209230bSgjelinek 		return (Z_POOL_BIND);
43330209230bSgjelinek 	}
43340209230bSgjelinek 
43350209230bSgjelinek 	return (Z_OK);
43360209230bSgjelinek }
43370209230bSgjelinek 
43380209230bSgjelinek 
43390209230bSgjelinek static boolean_t
43400209230bSgjelinek svc_enabled(char *svc_name)
43410209230bSgjelinek {
43420209230bSgjelinek 	scf_simple_prop_t	*prop;
43430209230bSgjelinek 	boolean_t		found = B_FALSE;
43440209230bSgjelinek 
43450209230bSgjelinek 	prop = scf_simple_prop_get(NULL, svc_name, SCF_PG_GENERAL,
43460209230bSgjelinek 	    SCF_PROPERTY_ENABLED);
43470209230bSgjelinek 
43480209230bSgjelinek 	if (scf_simple_prop_numvalues(prop) == 1 &&
43490209230bSgjelinek 	    *scf_simple_prop_next_boolean(prop) != 0)
43500209230bSgjelinek 		found = B_TRUE;
43510209230bSgjelinek 
43520209230bSgjelinek 	scf_simple_prop_free(prop);
43530209230bSgjelinek 
43540209230bSgjelinek 	return (found);
43550209230bSgjelinek }
43560209230bSgjelinek 
43570209230bSgjelinek /*
43580209230bSgjelinek  * If the zone has capped-memory, make sure the rcap service is enabled.
43590209230bSgjelinek  */
43600209230bSgjelinek int
43610209230bSgjelinek zonecfg_enable_rcapd(char *err, int size)
43620209230bSgjelinek {
43630209230bSgjelinek 	if (!svc_enabled(RCAP_SERVICE) &&
43640209230bSgjelinek 	    smf_enable_instance(RCAP_SERVICE, 0) == -1) {
43650209230bSgjelinek 		(void) strlcpy(err, scf_strerror(scf_error()), size);
43660209230bSgjelinek 		return (Z_SYSTEM);
43670209230bSgjelinek 	}
43680209230bSgjelinek 
43690209230bSgjelinek 	return (Z_OK);
43700209230bSgjelinek }
43710209230bSgjelinek 
43720209230bSgjelinek /*
43730209230bSgjelinek  * Return true if pset has cpu range specified and poold is not enabled.
43740209230bSgjelinek  */
43750209230bSgjelinek boolean_t
43760209230bSgjelinek zonecfg_warn_poold(zone_dochandle_t handle)
43770209230bSgjelinek {
43780209230bSgjelinek 	struct zone_psettab pset_tab;
43790209230bSgjelinek 	int min, max;
43800209230bSgjelinek 	int err;
43810209230bSgjelinek 
43820209230bSgjelinek 	err = zonecfg_lookup_pset(handle, &pset_tab);
43830209230bSgjelinek 
43840209230bSgjelinek 	/* if no temporary pool configured, we're done */
43850209230bSgjelinek 	if (err == Z_NO_ENTRY)
43860209230bSgjelinek 		return (B_FALSE);
43870209230bSgjelinek 
43880209230bSgjelinek 	min = atoi(pset_tab.zone_ncpu_min);
43890209230bSgjelinek 	max = atoi(pset_tab.zone_ncpu_max);
43900209230bSgjelinek 
43910209230bSgjelinek 	/* range not specified, no need for poold */
43920209230bSgjelinek 	if (min == max)
43930209230bSgjelinek 		return (B_FALSE);
43940209230bSgjelinek 
43950209230bSgjelinek 	/* we have a range, check if poold service is enabled */
43960209230bSgjelinek 	if (svc_enabled(POOLD_SERVICE))
43970209230bSgjelinek 		return (B_FALSE);
43980209230bSgjelinek 
43990209230bSgjelinek 	return (B_TRUE);
44000209230bSgjelinek }
44010209230bSgjelinek 
44020209230bSgjelinek static int
44030209230bSgjelinek get_pool_sched_class(char *poolname, char *class, int clsize)
44040209230bSgjelinek {
44050209230bSgjelinek 	int status;
44060209230bSgjelinek 	pool_conf_t *poolconf;
44070209230bSgjelinek 	pool_t *pool;
44080209230bSgjelinek 	pool_elem_t *pe;
44090209230bSgjelinek 	pool_value_t *pv = pool_value_alloc();
44100209230bSgjelinek 	const char *sched_str;
44110209230bSgjelinek 
44120209230bSgjelinek 	if (pool_get_status(&status) != PO_SUCCESS || status != POOL_ENABLED)
44130209230bSgjelinek 		return (Z_NO_POOL);
44140209230bSgjelinek 
44150209230bSgjelinek 	if ((poolconf = pool_conf_alloc()) == NULL)
44160209230bSgjelinek 		return (Z_NO_POOL);
44170209230bSgjelinek 
44180209230bSgjelinek 	if (pool_conf_open(poolconf, pool_dynamic_location(), PO_RDONLY) !=
44190209230bSgjelinek 	    PO_SUCCESS) {
44200209230bSgjelinek 		pool_conf_free(poolconf);
44210209230bSgjelinek 		return (Z_NO_POOL);
44220209230bSgjelinek 	}
44230209230bSgjelinek 
44240209230bSgjelinek 	if ((pool = pool_get_pool(poolconf, poolname)) == NULL) {
44250209230bSgjelinek 		(void) pool_conf_close(poolconf);
44260209230bSgjelinek 		pool_conf_free(poolconf);
44270209230bSgjelinek 		return (Z_NO_POOL);
44280209230bSgjelinek 	}
44290209230bSgjelinek 
44300209230bSgjelinek 	pe = pool_to_elem(poolconf, pool);
44310209230bSgjelinek 	if (pool_get_property(poolconf, pe, "pool.scheduler", pv)
44320209230bSgjelinek 	    != POC_INVAL) {
44330209230bSgjelinek 		(void) pool_value_get_string(pv, &sched_str);
44340209230bSgjelinek 		if (strlcpy(class, sched_str, clsize) >= clsize)
44350209230bSgjelinek 			return (Z_TOO_BIG);
44360209230bSgjelinek 	}
44370209230bSgjelinek 
44380209230bSgjelinek 	(void) pool_conf_close(poolconf);
44390209230bSgjelinek 	pool_conf_free(poolconf);
44400209230bSgjelinek 	return (Z_OK);
44410209230bSgjelinek }
44420209230bSgjelinek 
44430209230bSgjelinek /*
44440209230bSgjelinek  * Get the default scheduling class for the zone.  This will either be the
44450209230bSgjelinek  * class set on the zone's pool or the system default scheduling class.
44460209230bSgjelinek  */
44470209230bSgjelinek int
44480209230bSgjelinek zonecfg_get_dflt_sched_class(zone_dochandle_t handle, char *class, int clsize)
44490209230bSgjelinek {
44500209230bSgjelinek 	char poolname[MAXPATHLEN];
44510209230bSgjelinek 
44520209230bSgjelinek 	if (zonecfg_get_pool(handle, poolname, sizeof (poolname)) == Z_OK) {
44530209230bSgjelinek 		/* check if the zone's pool specified a sched class */
44540209230bSgjelinek 		if (get_pool_sched_class(poolname, class, clsize) == Z_OK)
44550209230bSgjelinek 			return (Z_OK);
44560209230bSgjelinek 	}
44570209230bSgjelinek 
44580209230bSgjelinek 	if (priocntl(0, 0, PC_GETDFLCL, class, (uint64_t)clsize) == -1)
44590209230bSgjelinek 		return (Z_TOO_BIG);
44600209230bSgjelinek 
44610209230bSgjelinek 	return (Z_OK);
44620209230bSgjelinek }
44630209230bSgjelinek 
44647c478bd9Sstevel@tonic-gate int
44657c478bd9Sstevel@tonic-gate zonecfg_setfsent(zone_dochandle_t handle)
44667c478bd9Sstevel@tonic-gate {
44677c478bd9Sstevel@tonic-gate 	return (zonecfg_setent(handle));
44687c478bd9Sstevel@tonic-gate }
44697c478bd9Sstevel@tonic-gate 
44707c478bd9Sstevel@tonic-gate int
44717c478bd9Sstevel@tonic-gate zonecfg_getfsent(zone_dochandle_t handle, struct zone_fstab *tabptr)
44727c478bd9Sstevel@tonic-gate {
44737c478bd9Sstevel@tonic-gate 	xmlNodePtr cur, options;
44747c478bd9Sstevel@tonic-gate 	char options_str[MAX_MNTOPT_STR];
44757c478bd9Sstevel@tonic-gate 	int err;
44767c478bd9Sstevel@tonic-gate 
44777c478bd9Sstevel@tonic-gate 	if (handle == NULL)
44787c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
44797c478bd9Sstevel@tonic-gate 
44807c478bd9Sstevel@tonic-gate 	if ((cur = handle->zone_dh_cur) == NULL)
44817c478bd9Sstevel@tonic-gate 		return (Z_NO_ENTRY);
44827c478bd9Sstevel@tonic-gate 
44837c478bd9Sstevel@tonic-gate 	for (; cur != NULL; cur = cur->next)
44847c478bd9Sstevel@tonic-gate 		if (!xmlStrcmp(cur->name, DTD_ELEM_FS))
44857c478bd9Sstevel@tonic-gate 			break;
44867c478bd9Sstevel@tonic-gate 	if (cur == NULL) {
44877c478bd9Sstevel@tonic-gate 		handle->zone_dh_cur = handle->zone_dh_top;
44887c478bd9Sstevel@tonic-gate 		return (Z_NO_ENTRY);
44897c478bd9Sstevel@tonic-gate 	}
44907c478bd9Sstevel@tonic-gate 
44917c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_SPECIAL, tabptr->zone_fs_special,
44927c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_fs_special))) != Z_OK) {
44937c478bd9Sstevel@tonic-gate 		handle->zone_dh_cur = handle->zone_dh_top;
44947c478bd9Sstevel@tonic-gate 		return (err);
44957c478bd9Sstevel@tonic-gate 	}
44967c478bd9Sstevel@tonic-gate 
44977c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_RAW, tabptr->zone_fs_raw,
44987c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_fs_raw))) != Z_OK) {
44997c478bd9Sstevel@tonic-gate 		handle->zone_dh_cur = handle->zone_dh_top;
45007c478bd9Sstevel@tonic-gate 		return (err);
45017c478bd9Sstevel@tonic-gate 	}
45027c478bd9Sstevel@tonic-gate 
45037c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_DIR, tabptr->zone_fs_dir,
45047c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_fs_dir))) != Z_OK) {
45057c478bd9Sstevel@tonic-gate 		handle->zone_dh_cur = handle->zone_dh_top;
45067c478bd9Sstevel@tonic-gate 		return (err);
45077c478bd9Sstevel@tonic-gate 	}
45087c478bd9Sstevel@tonic-gate 
45097c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_TYPE, tabptr->zone_fs_type,
45107c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_fs_type))) != Z_OK) {
45117c478bd9Sstevel@tonic-gate 		handle->zone_dh_cur = handle->zone_dh_top;
45127c478bd9Sstevel@tonic-gate 		return (err);
45137c478bd9Sstevel@tonic-gate 	}
45147c478bd9Sstevel@tonic-gate 
45157c478bd9Sstevel@tonic-gate 	/* OK for options to be NULL */
45167c478bd9Sstevel@tonic-gate 	tabptr->zone_fs_options = NULL;
45177c478bd9Sstevel@tonic-gate 	for (options = cur->xmlChildrenNode; options != NULL;
45187c478bd9Sstevel@tonic-gate 	    options = options->next) {
45197c478bd9Sstevel@tonic-gate 		if (fetchprop(options, DTD_ATTR_NAME, options_str,
45207c478bd9Sstevel@tonic-gate 		    sizeof (options_str)) != Z_OK)
45217c478bd9Sstevel@tonic-gate 			break;
45227c478bd9Sstevel@tonic-gate 		if (zonecfg_add_fs_option(tabptr, options_str) != Z_OK)
45237c478bd9Sstevel@tonic-gate 			break;
45247c478bd9Sstevel@tonic-gate 	}
45257c478bd9Sstevel@tonic-gate 
45267c478bd9Sstevel@tonic-gate 	handle->zone_dh_cur = cur->next;
45277c478bd9Sstevel@tonic-gate 	return (Z_OK);
45287c478bd9Sstevel@tonic-gate }
45297c478bd9Sstevel@tonic-gate 
45307c478bd9Sstevel@tonic-gate int
45317c478bd9Sstevel@tonic-gate zonecfg_endfsent(zone_dochandle_t handle)
45327c478bd9Sstevel@tonic-gate {
45337c478bd9Sstevel@tonic-gate 	return (zonecfg_endent(handle));
45347c478bd9Sstevel@tonic-gate }
45357c478bd9Sstevel@tonic-gate 
45367c478bd9Sstevel@tonic-gate int
45377c478bd9Sstevel@tonic-gate zonecfg_setipdent(zone_dochandle_t handle)
45387c478bd9Sstevel@tonic-gate {
45397c478bd9Sstevel@tonic-gate 	return (zonecfg_setent(handle));
45407c478bd9Sstevel@tonic-gate }
45417c478bd9Sstevel@tonic-gate 
45427c478bd9Sstevel@tonic-gate int
45437c478bd9Sstevel@tonic-gate zonecfg_getipdent(zone_dochandle_t handle, struct zone_fstab *tabptr)
45447c478bd9Sstevel@tonic-gate {
45457c478bd9Sstevel@tonic-gate 	xmlNodePtr cur;
45467c478bd9Sstevel@tonic-gate 	int err;
45477c478bd9Sstevel@tonic-gate 
45487c478bd9Sstevel@tonic-gate 	if (handle == NULL)
45497c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
45507c478bd9Sstevel@tonic-gate 
45517c478bd9Sstevel@tonic-gate 	if ((cur = handle->zone_dh_cur) == NULL)
45527c478bd9Sstevel@tonic-gate 		return (Z_NO_ENTRY);
45537c478bd9Sstevel@tonic-gate 
45547c478bd9Sstevel@tonic-gate 	for (; cur != NULL; cur = cur->next)
45557c478bd9Sstevel@tonic-gate 		if (!xmlStrcmp(cur->name, DTD_ELEM_IPD))
45567c478bd9Sstevel@tonic-gate 			break;
45577c478bd9Sstevel@tonic-gate 	if (cur == NULL) {
45587c478bd9Sstevel@tonic-gate 		handle->zone_dh_cur = handle->zone_dh_top;
45597c478bd9Sstevel@tonic-gate 		return (Z_NO_ENTRY);
45607c478bd9Sstevel@tonic-gate 	}
45617c478bd9Sstevel@tonic-gate 
45627c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_DIR, tabptr->zone_fs_dir,
45637c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_fs_dir))) != Z_OK) {
45647c478bd9Sstevel@tonic-gate 		handle->zone_dh_cur = handle->zone_dh_top;
45657c478bd9Sstevel@tonic-gate 		return (err);
45667c478bd9Sstevel@tonic-gate 	}
45677c478bd9Sstevel@tonic-gate 
45687c478bd9Sstevel@tonic-gate 	handle->zone_dh_cur = cur->next;
45697c478bd9Sstevel@tonic-gate 	return (Z_OK);
45707c478bd9Sstevel@tonic-gate }
45717c478bd9Sstevel@tonic-gate 
45727c478bd9Sstevel@tonic-gate int
45737c478bd9Sstevel@tonic-gate zonecfg_endipdent(zone_dochandle_t handle)
45747c478bd9Sstevel@tonic-gate {
45757c478bd9Sstevel@tonic-gate 	return (zonecfg_endent(handle));
45767c478bd9Sstevel@tonic-gate }
45777c478bd9Sstevel@tonic-gate 
45787c478bd9Sstevel@tonic-gate int
45797c478bd9Sstevel@tonic-gate zonecfg_setnwifent(zone_dochandle_t handle)
45807c478bd9Sstevel@tonic-gate {
45817c478bd9Sstevel@tonic-gate 	return (zonecfg_setent(handle));
45827c478bd9Sstevel@tonic-gate }
45837c478bd9Sstevel@tonic-gate 
45847c478bd9Sstevel@tonic-gate int
45857c478bd9Sstevel@tonic-gate zonecfg_getnwifent(zone_dochandle_t handle, struct zone_nwiftab *tabptr)
45867c478bd9Sstevel@tonic-gate {
45877c478bd9Sstevel@tonic-gate 	xmlNodePtr cur;
45887c478bd9Sstevel@tonic-gate 	int err;
45897c478bd9Sstevel@tonic-gate 
45907c478bd9Sstevel@tonic-gate 	if (handle == NULL)
45917c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
45927c478bd9Sstevel@tonic-gate 
45937c478bd9Sstevel@tonic-gate 	if ((cur = handle->zone_dh_cur) == NULL)
45947c478bd9Sstevel@tonic-gate 		return (Z_NO_ENTRY);
45957c478bd9Sstevel@tonic-gate 
45967c478bd9Sstevel@tonic-gate 	for (; cur != NULL; cur = cur->next)
45977c478bd9Sstevel@tonic-gate 		if (!xmlStrcmp(cur->name, DTD_ELEM_NET))
45987c478bd9Sstevel@tonic-gate 			break;
45997c478bd9Sstevel@tonic-gate 	if (cur == NULL) {
46007c478bd9Sstevel@tonic-gate 		handle->zone_dh_cur = handle->zone_dh_top;
46017c478bd9Sstevel@tonic-gate 		return (Z_NO_ENTRY);
46027c478bd9Sstevel@tonic-gate 	}
46037c478bd9Sstevel@tonic-gate 
46047c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_ADDRESS, tabptr->zone_nwif_address,
46057c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_nwif_address))) != Z_OK) {
46067c478bd9Sstevel@tonic-gate 		handle->zone_dh_cur = handle->zone_dh_top;
46077c478bd9Sstevel@tonic-gate 		return (err);
46087c478bd9Sstevel@tonic-gate 	}
46097c478bd9Sstevel@tonic-gate 
46107c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_PHYSICAL, tabptr->zone_nwif_physical,
46117c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_nwif_physical))) != Z_OK) {
46127c478bd9Sstevel@tonic-gate 		handle->zone_dh_cur = handle->zone_dh_top;
46137c478bd9Sstevel@tonic-gate 		return (err);
46147c478bd9Sstevel@tonic-gate 	}
46157c478bd9Sstevel@tonic-gate 
4616de860bd9Sgfaden 	if ((err = fetchprop(cur, DTD_ATTR_DEFROUTER,
4617de860bd9Sgfaden 	    tabptr->zone_nwif_defrouter,
4618de860bd9Sgfaden 	    sizeof (tabptr->zone_nwif_defrouter))) != Z_OK) {
4619de860bd9Sgfaden 		handle->zone_dh_cur = handle->zone_dh_top;
4620de860bd9Sgfaden 		return (err);
4621de860bd9Sgfaden 	}
4622de860bd9Sgfaden 
46237c478bd9Sstevel@tonic-gate 	handle->zone_dh_cur = cur->next;
46247c478bd9Sstevel@tonic-gate 	return (Z_OK);
46257c478bd9Sstevel@tonic-gate }
46267c478bd9Sstevel@tonic-gate 
46277c478bd9Sstevel@tonic-gate int
46287c478bd9Sstevel@tonic-gate zonecfg_endnwifent(zone_dochandle_t handle)
46297c478bd9Sstevel@tonic-gate {
46307c478bd9Sstevel@tonic-gate 	return (zonecfg_endent(handle));
46317c478bd9Sstevel@tonic-gate }
46327c478bd9Sstevel@tonic-gate 
46337c478bd9Sstevel@tonic-gate int
46347c478bd9Sstevel@tonic-gate zonecfg_setdevent(zone_dochandle_t handle)
46357c478bd9Sstevel@tonic-gate {
46367c478bd9Sstevel@tonic-gate 	return (zonecfg_setent(handle));
46377c478bd9Sstevel@tonic-gate }
46387c478bd9Sstevel@tonic-gate 
46397c478bd9Sstevel@tonic-gate int
46407c478bd9Sstevel@tonic-gate zonecfg_getdevent(zone_dochandle_t handle, struct zone_devtab *tabptr)
46417c478bd9Sstevel@tonic-gate {
46427c478bd9Sstevel@tonic-gate 	xmlNodePtr cur;
46437c478bd9Sstevel@tonic-gate 	int err;
46447c478bd9Sstevel@tonic-gate 
46457c478bd9Sstevel@tonic-gate 	if (handle == NULL)
46467c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
46477c478bd9Sstevel@tonic-gate 
46487c478bd9Sstevel@tonic-gate 	if ((cur = handle->zone_dh_cur) == NULL)
46497c478bd9Sstevel@tonic-gate 		return (Z_NO_ENTRY);
46507c478bd9Sstevel@tonic-gate 
46517c478bd9Sstevel@tonic-gate 	for (; cur != NULL; cur = cur->next)
46527c478bd9Sstevel@tonic-gate 		if (!xmlStrcmp(cur->name, DTD_ELEM_DEVICE))
46537c478bd9Sstevel@tonic-gate 			break;
46547c478bd9Sstevel@tonic-gate 	if (cur == NULL) {
46557c478bd9Sstevel@tonic-gate 		handle->zone_dh_cur = handle->zone_dh_top;
46567c478bd9Sstevel@tonic-gate 		return (Z_NO_ENTRY);
46577c478bd9Sstevel@tonic-gate 	}
46587c478bd9Sstevel@tonic-gate 
46597c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_MATCH, tabptr->zone_dev_match,
46607c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_dev_match))) != Z_OK) {
46617c478bd9Sstevel@tonic-gate 		handle->zone_dh_cur = handle->zone_dh_top;
46627c478bd9Sstevel@tonic-gate 		return (err);
46637c478bd9Sstevel@tonic-gate 	}
46647c478bd9Sstevel@tonic-gate 
46657c478bd9Sstevel@tonic-gate 	handle->zone_dh_cur = cur->next;
46667c478bd9Sstevel@tonic-gate 	return (Z_OK);
46677c478bd9Sstevel@tonic-gate }
46687c478bd9Sstevel@tonic-gate 
46697c478bd9Sstevel@tonic-gate int
46707c478bd9Sstevel@tonic-gate zonecfg_enddevent(zone_dochandle_t handle)
46717c478bd9Sstevel@tonic-gate {
46727c478bd9Sstevel@tonic-gate 	return (zonecfg_endent(handle));
46737c478bd9Sstevel@tonic-gate }
46747c478bd9Sstevel@tonic-gate 
46757c478bd9Sstevel@tonic-gate int
46767c478bd9Sstevel@tonic-gate zonecfg_setrctlent(zone_dochandle_t handle)
46777c478bd9Sstevel@tonic-gate {
46787c478bd9Sstevel@tonic-gate 	return (zonecfg_setent(handle));
46797c478bd9Sstevel@tonic-gate }
46807c478bd9Sstevel@tonic-gate 
46817c478bd9Sstevel@tonic-gate int
46827c478bd9Sstevel@tonic-gate zonecfg_getrctlent(zone_dochandle_t handle, struct zone_rctltab *tabptr)
46837c478bd9Sstevel@tonic-gate {
46847c478bd9Sstevel@tonic-gate 	xmlNodePtr cur, val;
46857c478bd9Sstevel@tonic-gate 	struct zone_rctlvaltab *valptr;
46867c478bd9Sstevel@tonic-gate 	int err;
46877c478bd9Sstevel@tonic-gate 
46887c478bd9Sstevel@tonic-gate 	if (handle == NULL)
46897c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
46907c478bd9Sstevel@tonic-gate 
46917c478bd9Sstevel@tonic-gate 	if ((cur = handle->zone_dh_cur) == NULL)
46927c478bd9Sstevel@tonic-gate 		return (Z_NO_ENTRY);
46937c478bd9Sstevel@tonic-gate 
46947c478bd9Sstevel@tonic-gate 	for (; cur != NULL; cur = cur->next)
46957c478bd9Sstevel@tonic-gate 		if (!xmlStrcmp(cur->name, DTD_ELEM_RCTL))
46967c478bd9Sstevel@tonic-gate 			break;
46977c478bd9Sstevel@tonic-gate 	if (cur == NULL) {
46987c478bd9Sstevel@tonic-gate 		handle->zone_dh_cur = handle->zone_dh_top;
46997c478bd9Sstevel@tonic-gate 		return (Z_NO_ENTRY);
47007c478bd9Sstevel@tonic-gate 	}
47017c478bd9Sstevel@tonic-gate 
47027c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_NAME, tabptr->zone_rctl_name,
47037c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_rctl_name))) != Z_OK) {
47047c478bd9Sstevel@tonic-gate 		handle->zone_dh_cur = handle->zone_dh_top;
47057c478bd9Sstevel@tonic-gate 		return (err);
47067c478bd9Sstevel@tonic-gate 	}
47077c478bd9Sstevel@tonic-gate 
47087c478bd9Sstevel@tonic-gate 	tabptr->zone_rctl_valptr = NULL;
47097c478bd9Sstevel@tonic-gate 	for (val = cur->xmlChildrenNode; val != NULL; val = val->next) {
47107c478bd9Sstevel@tonic-gate 		valptr = (struct zone_rctlvaltab *)malloc(
47117c478bd9Sstevel@tonic-gate 		    sizeof (struct zone_rctlvaltab));
47127c478bd9Sstevel@tonic-gate 		if (valptr == NULL)
47137c478bd9Sstevel@tonic-gate 			return (Z_NOMEM);
47147c478bd9Sstevel@tonic-gate 		if (fetchprop(val, DTD_ATTR_PRIV, valptr->zone_rctlval_priv,
47157c478bd9Sstevel@tonic-gate 		    sizeof (valptr->zone_rctlval_priv)) != Z_OK)
47167c478bd9Sstevel@tonic-gate 			break;
47177c478bd9Sstevel@tonic-gate 		if (fetchprop(val, DTD_ATTR_LIMIT, valptr->zone_rctlval_limit,
47187c478bd9Sstevel@tonic-gate 		    sizeof (valptr->zone_rctlval_limit)) != Z_OK)
47197c478bd9Sstevel@tonic-gate 			break;
47207c478bd9Sstevel@tonic-gate 		if (fetchprop(val, DTD_ATTR_ACTION, valptr->zone_rctlval_action,
47217c478bd9Sstevel@tonic-gate 		    sizeof (valptr->zone_rctlval_action)) != Z_OK)
47227c478bd9Sstevel@tonic-gate 			break;
47237c478bd9Sstevel@tonic-gate 		if (zonecfg_add_rctl_value(tabptr, valptr) != Z_OK)
47247c478bd9Sstevel@tonic-gate 			break;
47257c478bd9Sstevel@tonic-gate 	}
47267c478bd9Sstevel@tonic-gate 
47277c478bd9Sstevel@tonic-gate 	handle->zone_dh_cur = cur->next;
47287c478bd9Sstevel@tonic-gate 	return (Z_OK);
47297c478bd9Sstevel@tonic-gate }
47307c478bd9Sstevel@tonic-gate 
47317c478bd9Sstevel@tonic-gate int
47327c478bd9Sstevel@tonic-gate zonecfg_endrctlent(zone_dochandle_t handle)
47337c478bd9Sstevel@tonic-gate {
47347c478bd9Sstevel@tonic-gate 	return (zonecfg_endent(handle));
47357c478bd9Sstevel@tonic-gate }
47367c478bd9Sstevel@tonic-gate 
47377c478bd9Sstevel@tonic-gate int
47387c478bd9Sstevel@tonic-gate zonecfg_setattrent(zone_dochandle_t handle)
47397c478bd9Sstevel@tonic-gate {
47407c478bd9Sstevel@tonic-gate 	return (zonecfg_setent(handle));
47417c478bd9Sstevel@tonic-gate }
47427c478bd9Sstevel@tonic-gate 
47437c478bd9Sstevel@tonic-gate int
47447c478bd9Sstevel@tonic-gate zonecfg_getattrent(zone_dochandle_t handle, struct zone_attrtab *tabptr)
47457c478bd9Sstevel@tonic-gate {
47467c478bd9Sstevel@tonic-gate 	xmlNodePtr cur;
47477c478bd9Sstevel@tonic-gate 	int err;
47487c478bd9Sstevel@tonic-gate 
47497c478bd9Sstevel@tonic-gate 	if (handle == NULL)
47507c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
47517c478bd9Sstevel@tonic-gate 
47527c478bd9Sstevel@tonic-gate 	if ((cur = handle->zone_dh_cur) == NULL)
47537c478bd9Sstevel@tonic-gate 		return (Z_NO_ENTRY);
47547c478bd9Sstevel@tonic-gate 
47557c478bd9Sstevel@tonic-gate 	for (; cur != NULL; cur = cur->next)
47567c478bd9Sstevel@tonic-gate 		if (!xmlStrcmp(cur->name, DTD_ELEM_ATTR))
47577c478bd9Sstevel@tonic-gate 			break;
47587c478bd9Sstevel@tonic-gate 	if (cur == NULL) {
47597c478bd9Sstevel@tonic-gate 		handle->zone_dh_cur = handle->zone_dh_top;
47607c478bd9Sstevel@tonic-gate 		return (Z_NO_ENTRY);
47617c478bd9Sstevel@tonic-gate 	}
47627c478bd9Sstevel@tonic-gate 
47637c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_NAME, tabptr->zone_attr_name,
47647c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_attr_name))) != Z_OK) {
47657c478bd9Sstevel@tonic-gate 		handle->zone_dh_cur = handle->zone_dh_top;
47667c478bd9Sstevel@tonic-gate 		return (err);
47677c478bd9Sstevel@tonic-gate 	}
47687c478bd9Sstevel@tonic-gate 
47697c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_TYPE, tabptr->zone_attr_type,
47707c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_attr_type))) != Z_OK) {
47717c478bd9Sstevel@tonic-gate 		handle->zone_dh_cur = handle->zone_dh_top;
47727c478bd9Sstevel@tonic-gate 		return (err);
47737c478bd9Sstevel@tonic-gate 	}
47747c478bd9Sstevel@tonic-gate 
47757c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_VALUE, tabptr->zone_attr_value,
47767c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_attr_value))) != Z_OK) {
47777c478bd9Sstevel@tonic-gate 		handle->zone_dh_cur = handle->zone_dh_top;
47787c478bd9Sstevel@tonic-gate 		return (err);
47797c478bd9Sstevel@tonic-gate 	}
47807c478bd9Sstevel@tonic-gate 
47817c478bd9Sstevel@tonic-gate 	handle->zone_dh_cur = cur->next;
47827c478bd9Sstevel@tonic-gate 	return (Z_OK);
47837c478bd9Sstevel@tonic-gate }
47847c478bd9Sstevel@tonic-gate 
47857c478bd9Sstevel@tonic-gate int
47867c478bd9Sstevel@tonic-gate zonecfg_endattrent(zone_dochandle_t handle)
47877c478bd9Sstevel@tonic-gate {
47887c478bd9Sstevel@tonic-gate 	return (zonecfg_endent(handle));
47897c478bd9Sstevel@tonic-gate }
47907c478bd9Sstevel@tonic-gate 
4791ffbafc53Scomay /*
4792ffbafc53Scomay  * The privileges available on the system and described in privileges(5)
47939acbbeafSnn35248  * fall into four categories with respect to non-global zones:
47949acbbeafSnn35248  *
47959acbbeafSnn35248  *      Default set of privileges considered safe for all non-global
47969acbbeafSnn35248  *      zones.  These privileges are "safe" in the sense that a
47979acbbeafSnn35248  *      privileged process in the zone cannot affect processes in any
47989acbbeafSnn35248  *      other zone on the system.
47999acbbeafSnn35248  *
48009acbbeafSnn35248  *      Set of privileges not currently permitted within a non-global
48019acbbeafSnn35248  *      zone.  These privileges are considered by default, "unsafe,"
48029acbbeafSnn35248  *      and include ones which affect global resources (such as the
48039acbbeafSnn35248  *      system clock or physical memory) or are overly broad and cover
48049acbbeafSnn35248  *      more than one mechanism in the system.  In other cases, there
48059acbbeafSnn35248  *      has not been sufficient virtualization in the parts of the
48069acbbeafSnn35248  *      system the privilege covers to allow its use within a
48079acbbeafSnn35248  *      non-global zone.
48089acbbeafSnn35248  *
48099acbbeafSnn35248  *      Set of privileges required in order to get a zone booted and
48109acbbeafSnn35248  *      init(1M) started.  These cannot be removed from the zone's
48119acbbeafSnn35248  *      privilege set.
48129acbbeafSnn35248  *
48139acbbeafSnn35248  * All other privileges are optional and are potentially useful for
4814ffbafc53Scomay  * processes executing inside a non-global zone.
4815ffbafc53Scomay  *
4816ffbafc53Scomay  * When privileges are added to the system, a determination needs to be
4817ffbafc53Scomay  * made as to which category the privilege belongs to.  Ideally,
4818ffbafc53Scomay  * privileges should be fine-grained enough and the mechanisms they cover
4819ffbafc53Scomay  * virtualized enough so that they can be made available to non-global
4820ffbafc53Scomay  * zones.
4821ffbafc53Scomay  */
4822ffbafc53Scomay 
4823ffbafc53Scomay /*
4824ffbafc53Scomay  * Define some of the tokens that priv_str_to_set(3C) recognizes.  Since
4825ffbafc53Scomay  * the privilege string separator can be any character, although it is
4826ffbafc53Scomay  * usually a comma character, define these here as well in the event that
4827ffbafc53Scomay  * they change or are augmented in the future.
4828ffbafc53Scomay  */
4829ffbafc53Scomay #define	BASIC_TOKEN		"basic"
4830ffbafc53Scomay #define	DEFAULT_TOKEN		"default"
4831ffbafc53Scomay #define	ZONE_TOKEN		"zone"
4832ffbafc53Scomay #define	TOKEN_PRIV_CHAR		','
4833ffbafc53Scomay #define	TOKEN_PRIV_STR		","
4834ffbafc53Scomay 
48359acbbeafSnn35248 typedef struct priv_node {
48369acbbeafSnn35248 	struct priv_node	*pn_next;	/* Next privilege */
48379acbbeafSnn35248 	char			*pn_priv;	/* Privileges name */
48389acbbeafSnn35248 } priv_node_t;
48399acbbeafSnn35248 
48409acbbeafSnn35248 /* Privileges lists can differ across brands */
48419acbbeafSnn35248 typedef struct priv_lists {
48429acbbeafSnn35248 	/* Privileges considered safe for all non-global zones of a brand */
48439acbbeafSnn35248 	struct priv_node	*pl_default;
48449acbbeafSnn35248 
48459acbbeafSnn35248 	/* Privileges not permitted for all non-global zones of a brand */
48469acbbeafSnn35248 	struct priv_node	*pl_prohibited;
48479acbbeafSnn35248 
48489acbbeafSnn35248 	/* Privileges required for all non-global zones of a brand */
48499acbbeafSnn35248 	struct priv_node	*pl_required;
4850bf1d7e28Sdh155122 
4851bf1d7e28Sdh155122 	/*
4852bf1d7e28Sdh155122 	 * ip-type of the zone these privileges lists apply to.
4853bf1d7e28Sdh155122 	 * It is used to pass ip-type to the callback function,
4854bf1d7e28Sdh155122 	 * priv_lists_cb, which has no way of getting the ip-type.
4855bf1d7e28Sdh155122 	 */
4856bf1d7e28Sdh155122 	const char		*pl_iptype;
48579acbbeafSnn35248 } priv_lists_t;
48589acbbeafSnn35248 
48599acbbeafSnn35248 static int
4860bf1d7e28Sdh155122 priv_lists_cb(void *data, priv_iter_t *priv_iter)
48617c478bd9Sstevel@tonic-gate {
48629acbbeafSnn35248 	priv_lists_t *plp = (priv_lists_t *)data;
48639acbbeafSnn35248 	priv_node_t *pnp;
48649acbbeafSnn35248 
4865bf1d7e28Sdh155122 	/* Skip this privilege if ip-type does not match */
4866bf1d7e28Sdh155122 	if ((strcmp(priv_iter->pi_iptype, "all") != 0) &&
4867bf1d7e28Sdh155122 	    (strcmp(priv_iter->pi_iptype, plp->pl_iptype) != 0))
4868bf1d7e28Sdh155122 		return (0);
4869bf1d7e28Sdh155122 
48709acbbeafSnn35248 	/* Allocate a new priv list node. */
48719acbbeafSnn35248 	if ((pnp = malloc(sizeof (*pnp))) == NULL)
48729acbbeafSnn35248 		return (-1);
4873bf1d7e28Sdh155122 	if ((pnp->pn_priv = strdup(priv_iter->pi_name)) == NULL) {
48749acbbeafSnn35248 		free(pnp);
48759acbbeafSnn35248 		return (-1);
48769acbbeafSnn35248 	}
48779acbbeafSnn35248 
48789acbbeafSnn35248 	/* Insert the new priv list node into the right list */
4879bf1d7e28Sdh155122 	if (strcmp(priv_iter->pi_set, "default") == 0) {
48809acbbeafSnn35248 		pnp->pn_next = plp->pl_default;
48819acbbeafSnn35248 		plp->pl_default = pnp;
4882bf1d7e28Sdh155122 	} else if (strcmp(priv_iter->pi_set, "prohibited") == 0) {
48839acbbeafSnn35248 		pnp->pn_next = plp->pl_prohibited;
48849acbbeafSnn35248 		plp->pl_prohibited = pnp;
4885bf1d7e28Sdh155122 	} else if (strcmp(priv_iter->pi_set, "required") == 0) {
48869acbbeafSnn35248 		pnp->pn_next = plp->pl_required;
48879acbbeafSnn35248 		plp->pl_required = pnp;
48889acbbeafSnn35248 	} else {
48899acbbeafSnn35248 		free(pnp->pn_priv);
48909acbbeafSnn35248 		free(pnp);
48919acbbeafSnn35248 		return (-1);
48929acbbeafSnn35248 	}
48939acbbeafSnn35248 	return (0);
48949acbbeafSnn35248 }
48959acbbeafSnn35248 
48969acbbeafSnn35248 static void
48979acbbeafSnn35248 priv_lists_destroy(priv_lists_t *plp)
48989acbbeafSnn35248 {
48999acbbeafSnn35248 	priv_node_t *pnp;
49009acbbeafSnn35248 
49019acbbeafSnn35248 	assert(plp != NULL);
49029acbbeafSnn35248 
49039acbbeafSnn35248 	while ((pnp = plp->pl_default) != NULL) {
49049acbbeafSnn35248 		plp->pl_default = pnp->pn_next;
49059acbbeafSnn35248 		free(pnp->pn_priv);
49069acbbeafSnn35248 		free(pnp);
49079acbbeafSnn35248 	}
49089acbbeafSnn35248 	while ((pnp = plp->pl_prohibited) != NULL) {
49099acbbeafSnn35248 		plp->pl_prohibited = pnp->pn_next;
49109acbbeafSnn35248 		free(pnp->pn_priv);
49119acbbeafSnn35248 		free(pnp);
49129acbbeafSnn35248 	}
49139acbbeafSnn35248 	while ((pnp = plp->pl_required) != NULL) {
49149acbbeafSnn35248 		plp->pl_required = pnp->pn_next;
49159acbbeafSnn35248 		free(pnp->pn_priv);
49169acbbeafSnn35248 		free(pnp);
49179acbbeafSnn35248 	}
49189acbbeafSnn35248 	free(plp);
49199acbbeafSnn35248 }
49209acbbeafSnn35248 
49219acbbeafSnn35248 static int
4922e5816e35SEdward Pilatowicz priv_lists_create(zone_dochandle_t handle, char *brand, priv_lists_t **plpp,
4923bf1d7e28Sdh155122     const char *curr_iptype)
49249acbbeafSnn35248 {
49259acbbeafSnn35248 	priv_lists_t *plp;
4926123807fbSedp 	brand_handle_t bh;
4927e5816e35SEdward Pilatowicz 	char brand_str[MAXNAMELEN];
4928e5816e35SEdward Pilatowicz 
4929e5816e35SEdward Pilatowicz 	/* handle or brand must be set, but never both */
4930e5816e35SEdward Pilatowicz 	assert((handle != NULL) || (brand != NULL));
4931e5816e35SEdward Pilatowicz 	assert((handle == NULL) || (brand == NULL));
49329acbbeafSnn35248 
49339acbbeafSnn35248 	if (handle != NULL) {
4934e5816e35SEdward Pilatowicz 		brand = brand_str;
4935e5816e35SEdward Pilatowicz 		if (zonecfg_get_brand(handle, brand, sizeof (brand_str)) != 0)
49369acbbeafSnn35248 			return (Z_BRAND_ERROR);
49379acbbeafSnn35248 	}
49389acbbeafSnn35248 
4939123807fbSedp 	if ((bh = brand_open(brand)) == NULL)
49409acbbeafSnn35248 		return (Z_BRAND_ERROR);
49419acbbeafSnn35248 
49429acbbeafSnn35248 	if ((plp = calloc(1, sizeof (priv_lists_t))) == NULL) {
4943123807fbSedp 		brand_close(bh);
49449acbbeafSnn35248 		return (Z_NOMEM);
49459acbbeafSnn35248 	}
49469acbbeafSnn35248 
4947bf1d7e28Sdh155122 	plp->pl_iptype = curr_iptype;
4948bf1d7e28Sdh155122 
49499acbbeafSnn35248 	/* construct the privilege lists */
4950123807fbSedp 	if (brand_config_iter_privilege(bh, priv_lists_cb, plp) != 0) {
49519acbbeafSnn35248 		priv_lists_destroy(plp);
4952123807fbSedp 		brand_close(bh);
49539acbbeafSnn35248 		return (Z_BRAND_ERROR);
49549acbbeafSnn35248 	}
49559acbbeafSnn35248 
4956123807fbSedp 	brand_close(bh);
49579acbbeafSnn35248 	*plpp = plp;
49589acbbeafSnn35248 	return (Z_OK);
49599acbbeafSnn35248 }
49609acbbeafSnn35248 
49619acbbeafSnn35248 static int
49629acbbeafSnn35248 get_default_privset(priv_set_t *privs, priv_lists_t *plp)
49639acbbeafSnn35248 {
49649acbbeafSnn35248 	priv_node_t *pnp;
4965ffbafc53Scomay 	priv_set_t *basic;
49667c478bd9Sstevel@tonic-gate 
4967ffbafc53Scomay 	basic = priv_str_to_set(BASIC_TOKEN, TOKEN_PRIV_STR, NULL);
49687c478bd9Sstevel@tonic-gate 	if (basic == NULL)
4969ffbafc53Scomay 		return (errno == ENOMEM ? Z_NOMEM : Z_INVAL);
49707c478bd9Sstevel@tonic-gate 
49717c478bd9Sstevel@tonic-gate 	priv_union(basic, privs);
49727c478bd9Sstevel@tonic-gate 	priv_freeset(basic);
49737c478bd9Sstevel@tonic-gate 
49749acbbeafSnn35248 	for (pnp = plp->pl_default; pnp != NULL; pnp = pnp->pn_next) {
49759acbbeafSnn35248 		if (priv_addset(privs, pnp->pn_priv) != 0)
49767c478bd9Sstevel@tonic-gate 			return (Z_INVAL);
49777c478bd9Sstevel@tonic-gate 	}
49789acbbeafSnn35248 
49797c478bd9Sstevel@tonic-gate 	return (Z_OK);
49807c478bd9Sstevel@tonic-gate }
49817c478bd9Sstevel@tonic-gate 
49829acbbeafSnn35248 int
4983e5816e35SEdward Pilatowicz zonecfg_default_brand(char *brand, size_t brandsize)
4984e5816e35SEdward Pilatowicz {
4985e5816e35SEdward Pilatowicz 	zone_dochandle_t handle;
4986*7522f2fcSEdward Pilatowicz 	int myzoneid = getzoneid();
4987e5816e35SEdward Pilatowicz 	int ret;
4988e5816e35SEdward Pilatowicz 
4989*7522f2fcSEdward Pilatowicz 	/*
4990*7522f2fcSEdward Pilatowicz 	 * If we're running within a zone, then the default brand is the
4991*7522f2fcSEdward Pilatowicz 	 * current zone's brand.
4992*7522f2fcSEdward Pilatowicz 	 */
4993*7522f2fcSEdward Pilatowicz 	if (myzoneid != GLOBAL_ZONEID) {
4994*7522f2fcSEdward Pilatowicz 		ret = zone_getattr(myzoneid, ZONE_ATTR_BRAND, brand, brandsize);
4995*7522f2fcSEdward Pilatowicz 		if (ret < 0)
4996*7522f2fcSEdward Pilatowicz 			return ((errno == EFAULT) ? Z_TOO_BIG : Z_INVAL);
4997*7522f2fcSEdward Pilatowicz 		return (Z_OK);
4998*7522f2fcSEdward Pilatowicz 	}
4999*7522f2fcSEdward Pilatowicz 
5000e5816e35SEdward Pilatowicz 	if ((handle = zonecfg_init_handle()) == NULL)
5001e5816e35SEdward Pilatowicz 		return (Z_NOMEM);
5002e5816e35SEdward Pilatowicz 	if ((ret = zonecfg_get_handle("SUNWdefault", handle)) == Z_OK) {
5003e5816e35SEdward Pilatowicz 		ret = i_zonecfg_get_brand(handle, brand, brandsize, B_TRUE);
5004e5816e35SEdward Pilatowicz 		zonecfg_fini_handle(handle);
5005e5816e35SEdward Pilatowicz 		return (ret);
5006e5816e35SEdward Pilatowicz 	}
5007e5816e35SEdward Pilatowicz 	return (ret);
5008e5816e35SEdward Pilatowicz }
5009e5816e35SEdward Pilatowicz 
5010e5816e35SEdward Pilatowicz int
5011bf1d7e28Sdh155122 zonecfg_default_privset(priv_set_t *privs, const char *curr_iptype)
50129acbbeafSnn35248 {
50139acbbeafSnn35248 	priv_lists_t *plp;
5014e5816e35SEdward Pilatowicz 	char buf[MAXNAMELEN];
50159acbbeafSnn35248 	int ret;
50169acbbeafSnn35248 
5017e5816e35SEdward Pilatowicz 	if ((ret = zonecfg_default_brand(buf, sizeof (buf))) != Z_OK)
5018e5816e35SEdward Pilatowicz 		return (ret);
5019e5816e35SEdward Pilatowicz 	if ((ret = priv_lists_create(NULL, buf, &plp, curr_iptype)) != Z_OK)
50209acbbeafSnn35248 		return (ret);
50219acbbeafSnn35248 	ret = get_default_privset(privs, plp);
50229acbbeafSnn35248 	priv_lists_destroy(plp);
50239acbbeafSnn35248 	return (ret);
50249acbbeafSnn35248 }
50259acbbeafSnn35248 
5026ffbafc53Scomay void
5027ffbafc53Scomay append_priv_token(char *priv, char *str, size_t strlen)
5028ffbafc53Scomay {
5029ffbafc53Scomay 	if (*str != '\0')
5030ffbafc53Scomay 		(void) strlcat(str, TOKEN_PRIV_STR, strlen);
5031ffbafc53Scomay 	(void) strlcat(str, priv, strlen);
5032ffbafc53Scomay }
5033ffbafc53Scomay 
5034ffbafc53Scomay /*
5035ffbafc53Scomay  * Verify that the supplied string is a valid privilege limit set for a
5036ffbafc53Scomay  * non-global zone.  This string must not only be acceptable to
5037ffbafc53Scomay  * priv_str_to_set(3C) which parses it, but it also must resolve to a
5038ffbafc53Scomay  * privilege set that includes certain required privileges and lacks
5039ffbafc53Scomay  * certain prohibited privileges.
5040ffbafc53Scomay  */
5041ffbafc53Scomay static int
5042ffbafc53Scomay verify_privset(char *privbuf, priv_set_t *privs, char **privname,
50439acbbeafSnn35248     boolean_t add_default, priv_lists_t *plp)
5044ffbafc53Scomay {
50459acbbeafSnn35248 	priv_node_t *pnp;
50469acbbeafSnn35248 	char *tmp, *cp, *lasts;
5047ffbafc53Scomay 	size_t len;
5048ffbafc53Scomay 	priv_set_t *mergeset;
5049ffbafc53Scomay 	const char *token;
5050ffbafc53Scomay 
5051ffbafc53Scomay 	/*
5052ffbafc53Scomay 	 * The verification of the privilege string occurs in several
5053ffbafc53Scomay 	 * phases.  In the first phase, the supplied string is scanned for
5054ffbafc53Scomay 	 * the ZONE_TOKEN token which is not support as part of the
5055ffbafc53Scomay 	 * "limitpriv" property.
5056ffbafc53Scomay 	 *
5057ffbafc53Scomay 	 * Duplicate the supplied privilege string since strtok_r(3C)
5058ffbafc53Scomay 	 * tokenizes its input by null-terminating the tokens.
5059ffbafc53Scomay 	 */
5060ffbafc53Scomay 	if ((tmp = strdup(privbuf)) == NULL)
5061ffbafc53Scomay 		return (Z_NOMEM);
5062ffbafc53Scomay 	for (cp = strtok_r(tmp, TOKEN_PRIV_STR, &lasts); cp != NULL;
5063ffbafc53Scomay 	    cp = strtok_r(NULL, TOKEN_PRIV_STR, &lasts)) {
5064ffbafc53Scomay 		if (strcmp(cp, ZONE_TOKEN) == 0) {
5065ffbafc53Scomay 			free(tmp);
5066ffbafc53Scomay 			if ((*privname = strdup(ZONE_TOKEN)) == NULL)
5067ffbafc53Scomay 				return (Z_NOMEM);
5068ffbafc53Scomay 			else
5069ffbafc53Scomay 				return (Z_PRIV_UNKNOWN);
5070ffbafc53Scomay 		}
5071ffbafc53Scomay 	}
5072ffbafc53Scomay 	free(tmp);
5073ffbafc53Scomay 
5074ffbafc53Scomay 	if (add_default) {
5075ffbafc53Scomay 		/*
5076ffbafc53Scomay 		 * If DEFAULT_TOKEN was specified, a string needs to be
5077ffbafc53Scomay 		 * built containing the privileges from the default, safe
5078ffbafc53Scomay 		 * set along with those of the "limitpriv" property.
5079ffbafc53Scomay 		 */
5080ffbafc53Scomay 		len = strlen(privbuf) + sizeof (BASIC_TOKEN) + 2;
50819acbbeafSnn35248 
50829acbbeafSnn35248 		for (pnp = plp->pl_default; pnp != NULL; pnp = pnp->pn_next)
50839acbbeafSnn35248 			len += strlen(pnp->pn_priv) + 1;
5084ffbafc53Scomay 		tmp = alloca(len);
5085ffbafc53Scomay 		*tmp = '\0';
5086ffbafc53Scomay 
5087ffbafc53Scomay 		append_priv_token(BASIC_TOKEN, tmp, len);
50889acbbeafSnn35248 		for (pnp = plp->pl_default; pnp != NULL; pnp = pnp->pn_next)
50899acbbeafSnn35248 			append_priv_token(pnp->pn_priv, tmp, len);
5090ffbafc53Scomay 		(void) strlcat(tmp, TOKEN_PRIV_STR, len);
5091ffbafc53Scomay 		(void) strlcat(tmp, privbuf, len);
5092ffbafc53Scomay 	} else {
5093ffbafc53Scomay 		tmp = privbuf;
5094ffbafc53Scomay 	}
5095ffbafc53Scomay 
5096ffbafc53Scomay 
5097ffbafc53Scomay 	/*
5098ffbafc53Scomay 	 * In the next phase, attempt to convert the merged privilege
5099ffbafc53Scomay 	 * string into a privilege set.  In the case of an error, either
5100ffbafc53Scomay 	 * there was a memory allocation failure or there was an invalid
5101ffbafc53Scomay 	 * privilege token in the string.  In either case, return an
5102ffbafc53Scomay 	 * appropriate error code but in the event of an invalid token,
5103ffbafc53Scomay 	 * allocate a string containing its name and return that back to
5104ffbafc53Scomay 	 * the caller.
5105ffbafc53Scomay 	 */
5106ffbafc53Scomay 	mergeset = priv_str_to_set(tmp, TOKEN_PRIV_STR, &token);
5107ffbafc53Scomay 	if (mergeset == NULL) {
5108ffbafc53Scomay 		if (token == NULL)
5109ffbafc53Scomay 			return (Z_NOMEM);
5110ffbafc53Scomay 		if ((cp = strchr(token, TOKEN_PRIV_CHAR)) != NULL)
5111ffbafc53Scomay 			*cp = '\0';
5112ffbafc53Scomay 		if ((*privname = strdup(token)) == NULL)
5113ffbafc53Scomay 			return (Z_NOMEM);
5114ffbafc53Scomay 		else
5115ffbafc53Scomay 			return (Z_PRIV_UNKNOWN);
5116ffbafc53Scomay 	}
5117ffbafc53Scomay 
5118ffbafc53Scomay 	/*
5119ffbafc53Scomay 	 * Next, verify that none of the prohibited zone privileges are
5120ffbafc53Scomay 	 * present in the merged privilege set.
5121ffbafc53Scomay 	 */
51229acbbeafSnn35248 	for (pnp = plp->pl_prohibited; pnp != NULL; pnp = pnp->pn_next) {
51239acbbeafSnn35248 		if (priv_ismember(mergeset, pnp->pn_priv)) {
5124ffbafc53Scomay 			priv_freeset(mergeset);
51259acbbeafSnn35248 			if ((*privname = strdup(pnp->pn_priv)) == NULL)
5126ffbafc53Scomay 				return (Z_NOMEM);
5127ffbafc53Scomay 			else
5128ffbafc53Scomay 				return (Z_PRIV_PROHIBITED);
5129ffbafc53Scomay 		}
5130ffbafc53Scomay 	}
5131ffbafc53Scomay 
5132ffbafc53Scomay 	/*
5133ffbafc53Scomay 	 * Finally, verify that all of the required zone privileges are
5134ffbafc53Scomay 	 * present in the merged privilege set.
5135ffbafc53Scomay 	 */
51369acbbeafSnn35248 	for (pnp = plp->pl_required; pnp != NULL; pnp = pnp->pn_next) {
51379acbbeafSnn35248 		if (!priv_ismember(mergeset, pnp->pn_priv)) {
5138ffbafc53Scomay 			priv_freeset(mergeset);
51399acbbeafSnn35248 			if ((*privname = strdup(pnp->pn_priv)) == NULL)
5140ffbafc53Scomay 				return (Z_NOMEM);
5141ffbafc53Scomay 			else
5142ffbafc53Scomay 				return (Z_PRIV_REQUIRED);
5143ffbafc53Scomay 		}
5144ffbafc53Scomay 	}
5145ffbafc53Scomay 
5146ffbafc53Scomay 	priv_copyset(mergeset, privs);
5147ffbafc53Scomay 	priv_freeset(mergeset);
5148ffbafc53Scomay 	return (Z_OK);
5149ffbafc53Scomay }
5150ffbafc53Scomay 
5151ffbafc53Scomay /*
5152ffbafc53Scomay  * Fill in the supplied privilege set with either the default, safe set of
5153ffbafc53Scomay  * privileges suitable for a non-global zone, or one based on the
5154ffbafc53Scomay  * "limitpriv" property in the zone's configuration.
5155ffbafc53Scomay  *
5156ffbafc53Scomay  * In the event of an invalid privilege specification in the
5157ffbafc53Scomay  * configuration, a string is allocated and returned containing the
5158ffbafc53Scomay  * "privilege" causing the issue.  It is the caller's responsibility to
5159ffbafc53Scomay  * free this memory when it is done with it.
5160ffbafc53Scomay  */
5161ffbafc53Scomay int
5162ffbafc53Scomay zonecfg_get_privset(zone_dochandle_t handle, priv_set_t *privs,
5163ffbafc53Scomay     char **privname)
5164ffbafc53Scomay {
51659acbbeafSnn35248 	priv_lists_t *plp;
51669acbbeafSnn35248 	char *cp, *limitpriv = NULL;
51679acbbeafSnn35248 	int err, limitlen;
5168bf1d7e28Sdh155122 	zone_iptype_t iptype;
5169bf1d7e28Sdh155122 	const char *curr_iptype;
5170ffbafc53Scomay 
5171ffbafc53Scomay 	/*
5172ffbafc53Scomay 	 * Attempt to lookup the "limitpriv" property.  If it does not
5173ffbafc53Scomay 	 * exist or matches the string DEFAULT_TOKEN exactly, then the
5174ffbafc53Scomay 	 * default, safe privilege set is returned.
5175ffbafc53Scomay 	 */
51769acbbeafSnn35248 	if ((err = zonecfg_get_limitpriv(handle, &limitpriv)) != Z_OK)
5177ffbafc53Scomay 		return (err);
51789acbbeafSnn35248 
5179bf1d7e28Sdh155122 	if ((err = zonecfg_get_iptype(handle, &iptype)) != Z_OK)
5180bf1d7e28Sdh155122 		return (err);
5181bf1d7e28Sdh155122 
5182bf1d7e28Sdh155122 	switch (iptype) {
5183bf1d7e28Sdh155122 	case ZS_SHARED:
5184bf1d7e28Sdh155122 		curr_iptype = "shared";
5185bf1d7e28Sdh155122 		break;
5186bf1d7e28Sdh155122 	case ZS_EXCLUSIVE:
5187bf1d7e28Sdh155122 		curr_iptype = "exclusive";
5188bf1d7e28Sdh155122 		break;
5189bf1d7e28Sdh155122 	}
5190bf1d7e28Sdh155122 
5191e5816e35SEdward Pilatowicz 	if ((err = priv_lists_create(handle, NULL, &plp, curr_iptype)) != Z_OK)
51929acbbeafSnn35248 		return (err);
51939acbbeafSnn35248 
5194ffbafc53Scomay 	limitlen = strlen(limitpriv);
5195ffbafc53Scomay 	if (limitlen == 0 || strcmp(limitpriv, DEFAULT_TOKEN) == 0) {
5196ffbafc53Scomay 		free(limitpriv);
51979acbbeafSnn35248 		err = get_default_privset(privs, plp);
51989acbbeafSnn35248 		priv_lists_destroy(plp);
51999acbbeafSnn35248 		return (err);
5200ffbafc53Scomay 	}
5201ffbafc53Scomay 
5202ffbafc53Scomay 	/*
5203ffbafc53Scomay 	 * Check if the string DEFAULT_TOKEN is the first token in a list
5204ffbafc53Scomay 	 * of privileges.
5205ffbafc53Scomay 	 */
5206ffbafc53Scomay 	cp = strchr(limitpriv, TOKEN_PRIV_CHAR);
5207ffbafc53Scomay 	if (cp != NULL &&
5208ffbafc53Scomay 	    strncmp(limitpriv, DEFAULT_TOKEN, cp - limitpriv) == 0)
52099acbbeafSnn35248 		err = verify_privset(cp + 1, privs, privname, B_TRUE, plp);
5210ffbafc53Scomay 	else
52119acbbeafSnn35248 		err = verify_privset(limitpriv, privs, privname, B_FALSE, plp);
5212ffbafc53Scomay 
5213ffbafc53Scomay 	free(limitpriv);
52149acbbeafSnn35248 	priv_lists_destroy(plp);
5215ffbafc53Scomay 	return (err);
5216ffbafc53Scomay }
5217ffbafc53Scomay 
52187c478bd9Sstevel@tonic-gate int
52197c478bd9Sstevel@tonic-gate zone_get_zonepath(char *zone_name, char *zonepath, size_t rp_sz)
52207c478bd9Sstevel@tonic-gate {
52217c478bd9Sstevel@tonic-gate 	zone_dochandle_t handle;
52227c478bd9Sstevel@tonic-gate 	boolean_t found = B_FALSE;
52237c478bd9Sstevel@tonic-gate 	struct zoneent *ze;
52247c478bd9Sstevel@tonic-gate 	FILE *cookie;
52257c478bd9Sstevel@tonic-gate 	int err;
5226108322fbScarlsonj 	char *cp;
52277c478bd9Sstevel@tonic-gate 
52287c478bd9Sstevel@tonic-gate 	if (zone_name == NULL)
52297c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
52307c478bd9Sstevel@tonic-gate 
5231108322fbScarlsonj 	(void) strlcpy(zonepath, zonecfg_root, rp_sz);
5232108322fbScarlsonj 	cp = zonepath + strlen(zonepath);
5233108322fbScarlsonj 	while (cp > zonepath && cp[-1] == '/')
5234108322fbScarlsonj 		*--cp = '\0';
5235108322fbScarlsonj 
52367c478bd9Sstevel@tonic-gate 	if (strcmp(zone_name, GLOBAL_ZONENAME) == 0) {
5237108322fbScarlsonj 		if (zonepath[0] == '\0')
52387c478bd9Sstevel@tonic-gate 			(void) strlcpy(zonepath, "/", rp_sz);
52397c478bd9Sstevel@tonic-gate 		return (Z_OK);
52407c478bd9Sstevel@tonic-gate 	}
52417c478bd9Sstevel@tonic-gate 
52427c478bd9Sstevel@tonic-gate 	/*
52437c478bd9Sstevel@tonic-gate 	 * First check the index file.  Because older versions did not have
52447c478bd9Sstevel@tonic-gate 	 * a copy of the zone path, allow for it to be zero length, in which
52457c478bd9Sstevel@tonic-gate 	 * case we ignore this result and fall back to the XML files.
52467c478bd9Sstevel@tonic-gate 	 */
52477c478bd9Sstevel@tonic-gate 	cookie = setzoneent();
52487c478bd9Sstevel@tonic-gate 	while ((ze = getzoneent_private(cookie)) != NULL) {
52497c478bd9Sstevel@tonic-gate 		if (strcmp(ze->zone_name, zone_name) == 0) {
52507c478bd9Sstevel@tonic-gate 			found = B_TRUE;
5251108322fbScarlsonj 			if (ze->zone_path[0] != '\0')
5252108322fbScarlsonj 				(void) strlcpy(cp, ze->zone_path,
5253108322fbScarlsonj 				    rp_sz - (cp - zonepath));
52547c478bd9Sstevel@tonic-gate 		}
52557c478bd9Sstevel@tonic-gate 		free(ze);
52567c478bd9Sstevel@tonic-gate 		if (found)
52577c478bd9Sstevel@tonic-gate 			break;
52587c478bd9Sstevel@tonic-gate 	}
52597c478bd9Sstevel@tonic-gate 	endzoneent(cookie);
5260108322fbScarlsonj 	if (found && *cp != '\0')
52617c478bd9Sstevel@tonic-gate 		return (Z_OK);
52627c478bd9Sstevel@tonic-gate 
52637c478bd9Sstevel@tonic-gate 	/* Fall back to the XML files. */
52647c478bd9Sstevel@tonic-gate 	if ((handle = zonecfg_init_handle()) == NULL)
52657c478bd9Sstevel@tonic-gate 		return (Z_NOMEM);
52667c478bd9Sstevel@tonic-gate 
52677c478bd9Sstevel@tonic-gate 	/*
52687c478bd9Sstevel@tonic-gate 	 * Check the snapshot first: if a zone is running, its zonepath
52697c478bd9Sstevel@tonic-gate 	 * may have changed.
52707c478bd9Sstevel@tonic-gate 	 */
52717c478bd9Sstevel@tonic-gate 	if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) {
5272e767a340Sgjelinek 		if ((err = zonecfg_get_handle(zone_name, handle)) != Z_OK) {
5273e767a340Sgjelinek 			zonecfg_fini_handle(handle);
52747c478bd9Sstevel@tonic-gate 			return (err);
52757c478bd9Sstevel@tonic-gate 		}
5276e767a340Sgjelinek 	}
52777c478bd9Sstevel@tonic-gate 	err = zonecfg_get_zonepath(handle, zonepath, rp_sz);
52787c478bd9Sstevel@tonic-gate 	zonecfg_fini_handle(handle);
52797c478bd9Sstevel@tonic-gate 	return (err);
52807c478bd9Sstevel@tonic-gate }
52817c478bd9Sstevel@tonic-gate 
52827c478bd9Sstevel@tonic-gate int
52837c478bd9Sstevel@tonic-gate zone_get_rootpath(char *zone_name, char *rootpath, size_t rp_sz)
52847c478bd9Sstevel@tonic-gate {
52857c478bd9Sstevel@tonic-gate 	int err;
52867c478bd9Sstevel@tonic-gate 
52877c478bd9Sstevel@tonic-gate 	/* This function makes sense for non-global zones only. */
52887c478bd9Sstevel@tonic-gate 	if (strcmp(zone_name, GLOBAL_ZONENAME) == 0)
52897c478bd9Sstevel@tonic-gate 		return (Z_BOGUS_ZONE_NAME);
52907c478bd9Sstevel@tonic-gate 	if ((err = zone_get_zonepath(zone_name, rootpath, rp_sz)) != Z_OK)
52917c478bd9Sstevel@tonic-gate 		return (err);
52927c478bd9Sstevel@tonic-gate 	if (strlcat(rootpath, "/root", rp_sz) >= rp_sz)
52937c478bd9Sstevel@tonic-gate 		return (Z_TOO_BIG);
52947c478bd9Sstevel@tonic-gate 	return (Z_OK);
52957c478bd9Sstevel@tonic-gate }
52967c478bd9Sstevel@tonic-gate 
52979acbbeafSnn35248 int
52989acbbeafSnn35248 zone_get_brand(char *zone_name, char *brandname, size_t rp_sz)
52999acbbeafSnn35248 {
53009acbbeafSnn35248 	int err;
53019acbbeafSnn35248 	zone_dochandle_t handle;
53029acbbeafSnn35248 	char myzone[MAXNAMELEN];
53039acbbeafSnn35248 	int myzoneid = getzoneid();
53049acbbeafSnn35248 
53059acbbeafSnn35248 	/*
53069acbbeafSnn35248 	 * If we are not in the global zone, then we don't have the zone
53079acbbeafSnn35248 	 * .xml files with the brand name available.  Thus, we are going to
53089acbbeafSnn35248 	 * have to ask the kernel for the information.
53099acbbeafSnn35248 	 */
53109acbbeafSnn35248 	if (myzoneid != GLOBAL_ZONEID) {
53112ec67e04Sgjelinek 		if (is_system_labeled()) {
53122ec67e04Sgjelinek 			(void) strlcpy(brandname, NATIVE_BRAND_NAME, rp_sz);
53132ec67e04Sgjelinek 			return (Z_OK);
53142ec67e04Sgjelinek 		}
53159acbbeafSnn35248 		if (zone_getattr(myzoneid, ZONE_ATTR_NAME, myzone,
53169acbbeafSnn35248 		    sizeof (myzone)) < 0)
53179acbbeafSnn35248 			return (Z_NO_ZONE);
53181d6979b7Ssaurabh vyas - Sun Microsystems - Bangalore India 		if (!zonecfg_is_scratch(myzone)) {
53191d6979b7Ssaurabh vyas - Sun Microsystems - Bangalore India 			if (strncmp(zone_name, myzone, MAXNAMELEN) != 0)
53209acbbeafSnn35248 				return (Z_NO_ZONE);
53211d6979b7Ssaurabh vyas - Sun Microsystems - Bangalore India 		}
53229acbbeafSnn35248 		err = zone_getattr(myzoneid, ZONE_ATTR_BRAND, brandname, rp_sz);
53239acbbeafSnn35248 		if (err < 0)
53249acbbeafSnn35248 			return ((errno == EFAULT) ? Z_TOO_BIG : Z_INVAL);
53251d6979b7Ssaurabh vyas - Sun Microsystems - Bangalore India 
53269acbbeafSnn35248 		return (Z_OK);
53279acbbeafSnn35248 	}
53289acbbeafSnn35248 
5329e5816e35SEdward Pilatowicz 	if (strcmp(zone_name, "global") == 0)
5330e5816e35SEdward Pilatowicz 		return (zonecfg_default_brand(brandname, rp_sz));
5331e5816e35SEdward Pilatowicz 
53329acbbeafSnn35248 	if ((handle = zonecfg_init_handle()) == NULL)
53339acbbeafSnn35248 		return (Z_NOMEM);
53349acbbeafSnn35248 
53359acbbeafSnn35248 	err = zonecfg_get_handle((char *)zone_name, handle);
53369acbbeafSnn35248 	if (err == Z_OK)
53379acbbeafSnn35248 		err = zonecfg_get_brand(handle, brandname, rp_sz);
53389acbbeafSnn35248 
53399acbbeafSnn35248 	zonecfg_fini_handle(handle);
53409acbbeafSnn35248 	return (err);
53419acbbeafSnn35248 }
53429acbbeafSnn35248 
5343facf4a8dSllai1 /*
5344facf4a8dSllai1  * Return the appropriate root for the active /dev.
5345facf4a8dSllai1  * For normal zone, the path is $ZONEPATH/root;
5346facf4a8dSllai1  * for scratch zone, the dev path is $ZONEPATH/lu.
5347facf4a8dSllai1  */
5348facf4a8dSllai1 int
5349facf4a8dSllai1 zone_get_devroot(char *zone_name, char *devroot, size_t rp_sz)
5350facf4a8dSllai1 {
5351facf4a8dSllai1 	int err;
5352facf4a8dSllai1 	char *suffix;
5353facf4a8dSllai1 	zone_state_t state;
5354facf4a8dSllai1 
5355facf4a8dSllai1 	/* This function makes sense for non-global zones only. */
5356facf4a8dSllai1 	if (strcmp(zone_name, GLOBAL_ZONENAME) == 0)
5357facf4a8dSllai1 		return (Z_BOGUS_ZONE_NAME);
5358facf4a8dSllai1 	if ((err = zone_get_zonepath(zone_name, devroot, rp_sz)) != Z_OK)
5359facf4a8dSllai1 		return (err);
5360facf4a8dSllai1 
5361facf4a8dSllai1 	if (zone_get_state(zone_name, &state) == Z_OK &&
5362facf4a8dSllai1 	    state == ZONE_STATE_MOUNTED)
5363facf4a8dSllai1 		suffix = "/lu";
5364facf4a8dSllai1 	else
5365facf4a8dSllai1 		suffix = "/root";
5366facf4a8dSllai1 	if (strlcat(devroot, suffix, rp_sz) >= rp_sz)
5367facf4a8dSllai1 		return (Z_TOO_BIG);
5368facf4a8dSllai1 	return (Z_OK);
5369facf4a8dSllai1 }
5370facf4a8dSllai1 
53717c478bd9Sstevel@tonic-gate static zone_state_t
5372108322fbScarlsonj kernel_state_to_user_state(zoneid_t zoneid, zone_status_t kernel_state)
53737c478bd9Sstevel@tonic-gate {
5374108322fbScarlsonj 	char zoneroot[MAXPATHLEN];
5375108322fbScarlsonj 	size_t zlen;
5376108322fbScarlsonj 
53777c478bd9Sstevel@tonic-gate 	assert(kernel_state <= ZONE_MAX_STATE);
53787c478bd9Sstevel@tonic-gate 	switch (kernel_state) {
53797c478bd9Sstevel@tonic-gate 		case ZONE_IS_UNINITIALIZED:
5380bd41d0a8Snordmark 		case ZONE_IS_INITIALIZED:
5381bd41d0a8Snordmark 			/* The kernel will not return these two states */
5382108322fbScarlsonj 			return (ZONE_STATE_READY);
53837c478bd9Sstevel@tonic-gate 		case ZONE_IS_READY:
5384108322fbScarlsonj 			/*
5385108322fbScarlsonj 			 * If the zone's root is mounted on $ZONEPATH/lu, then
5386108322fbScarlsonj 			 * it's a mounted scratch zone.
5387108322fbScarlsonj 			 */
5388108322fbScarlsonj 			if (zone_getattr(zoneid, ZONE_ATTR_ROOT, zoneroot,
5389108322fbScarlsonj 			    sizeof (zoneroot)) >= 0) {
5390108322fbScarlsonj 				zlen = strlen(zoneroot);
5391108322fbScarlsonj 				if (zlen > 3 &&
5392108322fbScarlsonj 				    strcmp(zoneroot + zlen - 3, "/lu") == 0)
5393108322fbScarlsonj 					return (ZONE_STATE_MOUNTED);
5394108322fbScarlsonj 			}
53957c478bd9Sstevel@tonic-gate 			return (ZONE_STATE_READY);
53967c478bd9Sstevel@tonic-gate 		case ZONE_IS_BOOTING:
53977c478bd9Sstevel@tonic-gate 		case ZONE_IS_RUNNING:
53987c478bd9Sstevel@tonic-gate 			return (ZONE_STATE_RUNNING);
53997c478bd9Sstevel@tonic-gate 		case ZONE_IS_SHUTTING_DOWN:
54007c478bd9Sstevel@tonic-gate 		case ZONE_IS_EMPTY:
54017c478bd9Sstevel@tonic-gate 			return (ZONE_STATE_SHUTTING_DOWN);
54027c478bd9Sstevel@tonic-gate 		case ZONE_IS_DOWN:
54037c478bd9Sstevel@tonic-gate 		case ZONE_IS_DYING:
54047c478bd9Sstevel@tonic-gate 		case ZONE_IS_DEAD:
54057c478bd9Sstevel@tonic-gate 		default:
54067c478bd9Sstevel@tonic-gate 			return (ZONE_STATE_DOWN);
54077c478bd9Sstevel@tonic-gate 	}
54087c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
54097c478bd9Sstevel@tonic-gate }
54107c478bd9Sstevel@tonic-gate 
54117c478bd9Sstevel@tonic-gate int
54127c478bd9Sstevel@tonic-gate zone_get_state(char *zone_name, zone_state_t *state_num)
54137c478bd9Sstevel@tonic-gate {
54147c478bd9Sstevel@tonic-gate 	zone_status_t status;
54157c478bd9Sstevel@tonic-gate 	zoneid_t zone_id;
54167c478bd9Sstevel@tonic-gate 	struct zoneent *ze;
54177c478bd9Sstevel@tonic-gate 	boolean_t found = B_FALSE;
54187c478bd9Sstevel@tonic-gate 	FILE *cookie;
5419108322fbScarlsonj 	char kernzone[ZONENAME_MAX];
5420108322fbScarlsonj 	FILE *fp;
54217c478bd9Sstevel@tonic-gate 
54227c478bd9Sstevel@tonic-gate 	if (zone_name == NULL)
54237c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
54247c478bd9Sstevel@tonic-gate 
5425108322fbScarlsonj 	/*
5426108322fbScarlsonj 	 * If we're looking at an alternate root, then we need to query the
5427108322fbScarlsonj 	 * kernel using the scratch zone name.
5428108322fbScarlsonj 	 */
5429108322fbScarlsonj 	zone_id = -1;
5430108322fbScarlsonj 	if (*zonecfg_root != '\0' && !zonecfg_is_scratch(zone_name)) {
5431108322fbScarlsonj 		if ((fp = zonecfg_open_scratch("", B_FALSE)) != NULL) {
5432108322fbScarlsonj 			if (zonecfg_find_scratch(fp, zone_name, zonecfg_root,
5433108322fbScarlsonj 			    kernzone, sizeof (kernzone)) == 0)
5434108322fbScarlsonj 				zone_id = getzoneidbyname(kernzone);
5435108322fbScarlsonj 			zonecfg_close_scratch(fp);
5436108322fbScarlsonj 		}
5437108322fbScarlsonj 	} else {
5438108322fbScarlsonj 		zone_id = getzoneidbyname(zone_name);
5439108322fbScarlsonj 	}
5440108322fbScarlsonj 
54417c478bd9Sstevel@tonic-gate 	/* check to see if zone is running */
5442108322fbScarlsonj 	if (zone_id != -1 &&
54437c478bd9Sstevel@tonic-gate 	    zone_getattr(zone_id, ZONE_ATTR_STATUS, &status,
54447c478bd9Sstevel@tonic-gate 	    sizeof (status)) >= 0) {
5445108322fbScarlsonj 		*state_num = kernel_state_to_user_state(zone_id, status);
54467c478bd9Sstevel@tonic-gate 		return (Z_OK);
54477c478bd9Sstevel@tonic-gate 	}
54487c478bd9Sstevel@tonic-gate 
54497c478bd9Sstevel@tonic-gate 	cookie = setzoneent();
54507c478bd9Sstevel@tonic-gate 	while ((ze = getzoneent_private(cookie)) != NULL) {
54517c478bd9Sstevel@tonic-gate 		if (strcmp(ze->zone_name, zone_name) == 0) {
54527c478bd9Sstevel@tonic-gate 			found = B_TRUE;
54537c478bd9Sstevel@tonic-gate 			*state_num = ze->zone_state;
54547c478bd9Sstevel@tonic-gate 		}
54557c478bd9Sstevel@tonic-gate 		free(ze);
54567c478bd9Sstevel@tonic-gate 		if (found)
54577c478bd9Sstevel@tonic-gate 			break;
54587c478bd9Sstevel@tonic-gate 	}
54597c478bd9Sstevel@tonic-gate 	endzoneent(cookie);
54607c478bd9Sstevel@tonic-gate 	return ((found) ? Z_OK : Z_NO_ZONE);
54617c478bd9Sstevel@tonic-gate }
54627c478bd9Sstevel@tonic-gate 
54637c478bd9Sstevel@tonic-gate int
54647c478bd9Sstevel@tonic-gate zone_set_state(char *zone, zone_state_t state)
54657c478bd9Sstevel@tonic-gate {
54667c478bd9Sstevel@tonic-gate 	struct zoneent ze;
54677c478bd9Sstevel@tonic-gate 
54687c478bd9Sstevel@tonic-gate 	if (state != ZONE_STATE_CONFIGURED && state != ZONE_STATE_INSTALLED &&
54697c478bd9Sstevel@tonic-gate 	    state != ZONE_STATE_INCOMPLETE)
54707c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
54717c478bd9Sstevel@tonic-gate 
5472087719fdSdp 	bzero(&ze, sizeof (ze));
54737c478bd9Sstevel@tonic-gate 	(void) strlcpy(ze.zone_name, zone, sizeof (ze.zone_name));
54747c478bd9Sstevel@tonic-gate 	ze.zone_state = state;
54757c478bd9Sstevel@tonic-gate 	(void) strlcpy(ze.zone_path, "", sizeof (ze.zone_path));
54767c478bd9Sstevel@tonic-gate 	return (putzoneent(&ze, PZE_MODIFY));
54777c478bd9Sstevel@tonic-gate }
54787c478bd9Sstevel@tonic-gate 
54797c478bd9Sstevel@tonic-gate /*
54807c478bd9Sstevel@tonic-gate  * Get id (if any) for specified zone.  There are four possible outcomes:
54817c478bd9Sstevel@tonic-gate  * - If the string corresponds to the numeric id of an active (booted)
54827c478bd9Sstevel@tonic-gate  *   zone, sets *zip to the zone id and returns 0.
54837c478bd9Sstevel@tonic-gate  * - If the string corresponds to the name of an active (booted) zone,
54847c478bd9Sstevel@tonic-gate  *   sets *zip to the zone id and returns 0.
54857c478bd9Sstevel@tonic-gate  * - If the string is a name in the configuration but is not booted,
54867c478bd9Sstevel@tonic-gate  *   sets *zip to ZONE_ID_UNDEFINED and returns 0.
54877c478bd9Sstevel@tonic-gate  * - Otherwise, leaves *zip unchanged and returns -1.
54887c478bd9Sstevel@tonic-gate  *
54897c478bd9Sstevel@tonic-gate  * This function acts as an auxiliary filter on the function of the same
54907c478bd9Sstevel@tonic-gate  * name in libc; the linker binds to this version if libzonecfg exists,
54917c478bd9Sstevel@tonic-gate  * and the libc version if it doesn't.  Any changes to this version of
54927c478bd9Sstevel@tonic-gate  * the function should probably be reflected in the libc version as well.
54937c478bd9Sstevel@tonic-gate  */
54947c478bd9Sstevel@tonic-gate int
54957c478bd9Sstevel@tonic-gate zone_get_id(const char *str, zoneid_t *zip)
54967c478bd9Sstevel@tonic-gate {
54977c478bd9Sstevel@tonic-gate 	zone_dochandle_t hdl;
54987c478bd9Sstevel@tonic-gate 	zoneid_t zoneid;
54997c478bd9Sstevel@tonic-gate 	char *cp;
55007c478bd9Sstevel@tonic-gate 	int err;
55017c478bd9Sstevel@tonic-gate 
55027c478bd9Sstevel@tonic-gate 	/* first try looking for active zone by id */
55037c478bd9Sstevel@tonic-gate 	errno = 0;
55047c478bd9Sstevel@tonic-gate 	zoneid = (zoneid_t)strtol(str, &cp, 0);
55057c478bd9Sstevel@tonic-gate 	if (errno == 0 && cp != str && *cp == '\0' &&
55067c478bd9Sstevel@tonic-gate 	    getzonenamebyid(zoneid, NULL, 0) != -1) {
55077c478bd9Sstevel@tonic-gate 		*zip = zoneid;
55087c478bd9Sstevel@tonic-gate 		return (0);
55097c478bd9Sstevel@tonic-gate 	}
55107c478bd9Sstevel@tonic-gate 
55117c478bd9Sstevel@tonic-gate 	/* then look for active zone by name */
55127c478bd9Sstevel@tonic-gate 	if ((zoneid = getzoneidbyname(str)) != -1) {
55137c478bd9Sstevel@tonic-gate 		*zip = zoneid;
55147c478bd9Sstevel@tonic-gate 		return (0);
55157c478bd9Sstevel@tonic-gate 	}
55167c478bd9Sstevel@tonic-gate 
55177c478bd9Sstevel@tonic-gate 	/* if in global zone, try looking up name in configuration database */
55187c478bd9Sstevel@tonic-gate 	if (getzoneid() != GLOBAL_ZONEID ||
55197c478bd9Sstevel@tonic-gate 	    (hdl = zonecfg_init_handle()) == NULL)
55207c478bd9Sstevel@tonic-gate 		return (-1);
55217c478bd9Sstevel@tonic-gate 
5522108322fbScarlsonj 	if (zonecfg_get_handle(str, hdl) == Z_OK) {
55237c478bd9Sstevel@tonic-gate 		/* zone exists but isn't active */
55247c478bd9Sstevel@tonic-gate 		*zip = ZONE_ID_UNDEFINED;
55257c478bd9Sstevel@tonic-gate 		err = 0;
55267c478bd9Sstevel@tonic-gate 	} else {
55277c478bd9Sstevel@tonic-gate 		err = -1;
55287c478bd9Sstevel@tonic-gate 	}
55297c478bd9Sstevel@tonic-gate 
55307c478bd9Sstevel@tonic-gate 	zonecfg_fini_handle(hdl);
55317c478bd9Sstevel@tonic-gate 	return (err);
55327c478bd9Sstevel@tonic-gate }
55337c478bd9Sstevel@tonic-gate 
55347c478bd9Sstevel@tonic-gate char *
55357c478bd9Sstevel@tonic-gate zone_state_str(zone_state_t state_num)
55367c478bd9Sstevel@tonic-gate {
55377c478bd9Sstevel@tonic-gate 	switch (state_num) {
55387c478bd9Sstevel@tonic-gate 	case ZONE_STATE_CONFIGURED:
55397c478bd9Sstevel@tonic-gate 		return (ZONE_STATE_STR_CONFIGURED);
55407c478bd9Sstevel@tonic-gate 	case ZONE_STATE_INCOMPLETE:
55417c478bd9Sstevel@tonic-gate 		return (ZONE_STATE_STR_INCOMPLETE);
55427c478bd9Sstevel@tonic-gate 	case ZONE_STATE_INSTALLED:
55437c478bd9Sstevel@tonic-gate 		return (ZONE_STATE_STR_INSTALLED);
55447c478bd9Sstevel@tonic-gate 	case ZONE_STATE_READY:
55457c478bd9Sstevel@tonic-gate 		return (ZONE_STATE_STR_READY);
5546108322fbScarlsonj 	case ZONE_STATE_MOUNTED:
5547108322fbScarlsonj 		return (ZONE_STATE_STR_MOUNTED);
55487c478bd9Sstevel@tonic-gate 	case ZONE_STATE_RUNNING:
55497c478bd9Sstevel@tonic-gate 		return (ZONE_STATE_STR_RUNNING);
55507c478bd9Sstevel@tonic-gate 	case ZONE_STATE_SHUTTING_DOWN:
55517c478bd9Sstevel@tonic-gate 		return (ZONE_STATE_STR_SHUTTING_DOWN);
55527c478bd9Sstevel@tonic-gate 	case ZONE_STATE_DOWN:
55537c478bd9Sstevel@tonic-gate 		return (ZONE_STATE_STR_DOWN);
55547c478bd9Sstevel@tonic-gate 	default:
55557c478bd9Sstevel@tonic-gate 		return ("unknown");
55567c478bd9Sstevel@tonic-gate 	}
55577c478bd9Sstevel@tonic-gate }
55587c478bd9Sstevel@tonic-gate 
55597c478bd9Sstevel@tonic-gate /*
5560108322fbScarlsonj  * Given a UUID value, find an associated zone name.  This is intended to be
5561108322fbScarlsonj  * used by callers who set up some 'default' name (corresponding to the
5562108322fbScarlsonj  * expected name for the zone) in the zonename buffer, and thus the function
5563108322fbScarlsonj  * doesn't touch this buffer on failure.
5564108322fbScarlsonj  */
5565108322fbScarlsonj int
5566555afedfScarlsonj zonecfg_get_name_by_uuid(const uuid_t uuidin, char *zonename, size_t namelen)
5567108322fbScarlsonj {
5568108322fbScarlsonj 	FILE *fp;
5569108322fbScarlsonj 	struct zoneent *ze;
5570555afedfScarlsonj 	uchar_t *uuid;
5571108322fbScarlsonj 
5572108322fbScarlsonj 	/*
5573108322fbScarlsonj 	 * A small amount of subterfuge via casts is necessary here because
5574108322fbScarlsonj 	 * libuuid doesn't use const correctly, but we don't want to export
5575108322fbScarlsonj 	 * this brokenness to our clients.
5576108322fbScarlsonj 	 */
5577555afedfScarlsonj 	uuid = (uchar_t *)uuidin;
5578555afedfScarlsonj 	if (uuid_is_null(uuid))
5579108322fbScarlsonj 		return (Z_NO_ZONE);
5580108322fbScarlsonj 	if ((fp = setzoneent()) == NULL)
5581108322fbScarlsonj 		return (Z_NO_ZONE);
5582108322fbScarlsonj 	while ((ze = getzoneent_private(fp)) != NULL) {
5583555afedfScarlsonj 		if (uuid_compare(uuid, ze->zone_uuid) == 0)
5584108322fbScarlsonj 			break;
5585108322fbScarlsonj 		free(ze);
5586108322fbScarlsonj 	}
5587108322fbScarlsonj 	endzoneent(fp);
5588108322fbScarlsonj 	if (ze != NULL) {
5589108322fbScarlsonj 		(void) strlcpy(zonename, ze->zone_name, namelen);
5590108322fbScarlsonj 		free(ze);
5591108322fbScarlsonj 		return (Z_OK);
5592108322fbScarlsonj 	} else {
5593108322fbScarlsonj 		return (Z_NO_ZONE);
5594108322fbScarlsonj 	}
5595108322fbScarlsonj }
5596108322fbScarlsonj 
5597108322fbScarlsonj /*
5598108322fbScarlsonj  * Given a zone name, get its UUID.  Returns a "NULL" UUID value if the zone
5599108322fbScarlsonj  * exists but the file doesn't have a value set yet.  Returns an error if the
5600108322fbScarlsonj  * zone cannot be located.
5601108322fbScarlsonj  */
5602108322fbScarlsonj int
5603108322fbScarlsonj zonecfg_get_uuid(const char *zonename, uuid_t uuid)
5604108322fbScarlsonj {
5605108322fbScarlsonj 	FILE *fp;
5606108322fbScarlsonj 	struct zoneent *ze;
5607108322fbScarlsonj 
5608108322fbScarlsonj 	if ((fp = setzoneent()) == NULL)
5609108322fbScarlsonj 		return (Z_NO_ZONE);
5610108322fbScarlsonj 	while ((ze = getzoneent_private(fp)) != NULL) {
5611108322fbScarlsonj 		if (strcmp(ze->zone_name, zonename) == 0)
5612108322fbScarlsonj 			break;
5613108322fbScarlsonj 		free(ze);
5614108322fbScarlsonj 	}
5615108322fbScarlsonj 	endzoneent(fp);
5616108322fbScarlsonj 	if (ze != NULL) {
5617108322fbScarlsonj 		uuid_copy(uuid, ze->zone_uuid);
5618108322fbScarlsonj 		free(ze);
5619108322fbScarlsonj 		return (Z_OK);
5620108322fbScarlsonj 	} else {
5621108322fbScarlsonj 		return (Z_NO_ZONE);
5622108322fbScarlsonj 	}
5623108322fbScarlsonj }
5624108322fbScarlsonj 
5625108322fbScarlsonj /*
56267c478bd9Sstevel@tonic-gate  * File-system convenience functions.
56277c478bd9Sstevel@tonic-gate  */
56287c478bd9Sstevel@tonic-gate boolean_t
56297c478bd9Sstevel@tonic-gate zonecfg_valid_fs_type(const char *type)
56307c478bd9Sstevel@tonic-gate {
56317c478bd9Sstevel@tonic-gate 	/*
56327c478bd9Sstevel@tonic-gate 	 * We already know which FS types don't work.
56337c478bd9Sstevel@tonic-gate 	 */
56347c478bd9Sstevel@tonic-gate 	if (strcmp(type, "proc") == 0 ||
56357c478bd9Sstevel@tonic-gate 	    strcmp(type, "mntfs") == 0 ||
56367c478bd9Sstevel@tonic-gate 	    strcmp(type, "autofs") == 0 ||
56377c478bd9Sstevel@tonic-gate 	    strncmp(type, "nfs", sizeof ("nfs") - 1) == 0 ||
56387c478bd9Sstevel@tonic-gate 	    strcmp(type, "cachefs") == 0)
56397c478bd9Sstevel@tonic-gate 		return (B_FALSE);
56407c478bd9Sstevel@tonic-gate 	/*
56417c478bd9Sstevel@tonic-gate 	 * The caller may do more detailed verification to make sure other
56427c478bd9Sstevel@tonic-gate 	 * aspects of this filesystem type make sense.
56437c478bd9Sstevel@tonic-gate 	 */
56447c478bd9Sstevel@tonic-gate 	return (B_TRUE);
56457c478bd9Sstevel@tonic-gate }
56467c478bd9Sstevel@tonic-gate 
56477c478bd9Sstevel@tonic-gate /*
56487c478bd9Sstevel@tonic-gate  * Generally uninteresting rctl convenience functions.
56497c478bd9Sstevel@tonic-gate  */
56507c478bd9Sstevel@tonic-gate 
56517c478bd9Sstevel@tonic-gate int
56527c478bd9Sstevel@tonic-gate zonecfg_construct_rctlblk(const struct zone_rctlvaltab *rctlval,
56537c478bd9Sstevel@tonic-gate     rctlblk_t *rctlblk)
56547c478bd9Sstevel@tonic-gate {
56557c478bd9Sstevel@tonic-gate 	unsigned long long ull;
56567c478bd9Sstevel@tonic-gate 	char *endp;
56577c478bd9Sstevel@tonic-gate 	rctl_priv_t priv;
56587c478bd9Sstevel@tonic-gate 	rctl_qty_t limit;
56597c478bd9Sstevel@tonic-gate 	uint_t action;
56607c478bd9Sstevel@tonic-gate 
56617c478bd9Sstevel@tonic-gate 	/* Get the privilege */
56627c478bd9Sstevel@tonic-gate 	if (strcmp(rctlval->zone_rctlval_priv, "basic") == 0) {
56637c478bd9Sstevel@tonic-gate 		priv = RCPRIV_BASIC;
56647c478bd9Sstevel@tonic-gate 	} else if (strcmp(rctlval->zone_rctlval_priv, "privileged") == 0) {
56657c478bd9Sstevel@tonic-gate 		priv = RCPRIV_PRIVILEGED;
56667c478bd9Sstevel@tonic-gate 	} else {
56677c478bd9Sstevel@tonic-gate 		/* Invalid privilege */
56687c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
56697c478bd9Sstevel@tonic-gate 	}
56707c478bd9Sstevel@tonic-gate 
56717c478bd9Sstevel@tonic-gate 	/* deal with negative input; strtoull(3c) doesn't do what we want */
56727c478bd9Sstevel@tonic-gate 	if (rctlval->zone_rctlval_limit[0] == '-')
56737c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
56747c478bd9Sstevel@tonic-gate 	/* Get the limit */
56757c478bd9Sstevel@tonic-gate 	errno = 0;
56767c478bd9Sstevel@tonic-gate 	ull = strtoull(rctlval->zone_rctlval_limit, &endp, 0);
56777c478bd9Sstevel@tonic-gate 	if (errno != 0 || *endp != '\0') {
56787c478bd9Sstevel@tonic-gate 		/* parse failed */
56797c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
56807c478bd9Sstevel@tonic-gate 	}
56817c478bd9Sstevel@tonic-gate 	limit = (rctl_qty_t)ull;
56827c478bd9Sstevel@tonic-gate 
56837c478bd9Sstevel@tonic-gate 	/* Get the action */
56847c478bd9Sstevel@tonic-gate 	if (strcmp(rctlval->zone_rctlval_action, "none") == 0) {
56857c478bd9Sstevel@tonic-gate 		action = RCTL_LOCAL_NOACTION;
56867c478bd9Sstevel@tonic-gate 	} else if (strcmp(rctlval->zone_rctlval_action, "signal") == 0) {
56877c478bd9Sstevel@tonic-gate 		action = RCTL_LOCAL_SIGNAL;
56887c478bd9Sstevel@tonic-gate 	} else if (strcmp(rctlval->zone_rctlval_action, "deny") == 0) {
56897c478bd9Sstevel@tonic-gate 		action = RCTL_LOCAL_DENY;
56907c478bd9Sstevel@tonic-gate 	} else {
56917c478bd9Sstevel@tonic-gate 		/* Invalid Action */
56927c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
56937c478bd9Sstevel@tonic-gate 	}
56947c478bd9Sstevel@tonic-gate 	rctlblk_set_local_action(rctlblk, action, 0);
56957c478bd9Sstevel@tonic-gate 	rctlblk_set_privilege(rctlblk, priv);
56967c478bd9Sstevel@tonic-gate 	rctlblk_set_value(rctlblk, limit);
56977c478bd9Sstevel@tonic-gate 	return (Z_OK);
56987c478bd9Sstevel@tonic-gate }
56997c478bd9Sstevel@tonic-gate 
57007c478bd9Sstevel@tonic-gate static int
57017c478bd9Sstevel@tonic-gate rctl_check(const char *rctlname, void *arg)
57027c478bd9Sstevel@tonic-gate {
57037c478bd9Sstevel@tonic-gate 	const char *attrname = arg;
57047c478bd9Sstevel@tonic-gate 
57057c478bd9Sstevel@tonic-gate 	/*
57067c478bd9Sstevel@tonic-gate 	 * Returning 1 here is our signal to zonecfg_is_rctl() that it is
57077c478bd9Sstevel@tonic-gate 	 * indeed an rctl name recognized by the system.
57087c478bd9Sstevel@tonic-gate 	 */
57097c478bd9Sstevel@tonic-gate 	return (strcmp(rctlname, attrname) == 0 ? 1 : 0);
57107c478bd9Sstevel@tonic-gate }
57117c478bd9Sstevel@tonic-gate 
57127c478bd9Sstevel@tonic-gate boolean_t
57137c478bd9Sstevel@tonic-gate zonecfg_is_rctl(const char *name)
57147c478bd9Sstevel@tonic-gate {
57157c478bd9Sstevel@tonic-gate 	return (rctl_walk(rctl_check, (void *)name) == 1);
57167c478bd9Sstevel@tonic-gate }
57177c478bd9Sstevel@tonic-gate 
57187c478bd9Sstevel@tonic-gate boolean_t
57197c478bd9Sstevel@tonic-gate zonecfg_valid_rctlname(const char *name)
57207c478bd9Sstevel@tonic-gate {
57217c478bd9Sstevel@tonic-gate 	const char *c;
57227c478bd9Sstevel@tonic-gate 
57237c478bd9Sstevel@tonic-gate 	if (strncmp(name, "zone.", sizeof ("zone.") - 1) != 0)
57247c478bd9Sstevel@tonic-gate 		return (B_FALSE);
57257c478bd9Sstevel@tonic-gate 	if (strlen(name) == sizeof ("zone.") - 1)
57267c478bd9Sstevel@tonic-gate 		return (B_FALSE);
57277c478bd9Sstevel@tonic-gate 	for (c = name + sizeof ("zone.") - 1; *c != '\0'; c++) {
57287c478bd9Sstevel@tonic-gate 		if (!isalpha(*c) && *c != '-')
57297c478bd9Sstevel@tonic-gate 			return (B_FALSE);
57307c478bd9Sstevel@tonic-gate 	}
57317c478bd9Sstevel@tonic-gate 	return (B_TRUE);
57327c478bd9Sstevel@tonic-gate }
57337c478bd9Sstevel@tonic-gate 
57347c478bd9Sstevel@tonic-gate boolean_t
57357c478bd9Sstevel@tonic-gate zonecfg_valid_rctlblk(const rctlblk_t *rctlblk)
57367c478bd9Sstevel@tonic-gate {
57377c478bd9Sstevel@tonic-gate 	rctl_priv_t priv = rctlblk_get_privilege((rctlblk_t *)rctlblk);
57387c478bd9Sstevel@tonic-gate 	uint_t action = rctlblk_get_local_action((rctlblk_t *)rctlblk, NULL);
57397c478bd9Sstevel@tonic-gate 
57407c478bd9Sstevel@tonic-gate 	if (priv != RCPRIV_PRIVILEGED)
57417c478bd9Sstevel@tonic-gate 		return (B_FALSE);
57427c478bd9Sstevel@tonic-gate 	if (action != RCTL_LOCAL_NOACTION && action != RCTL_LOCAL_DENY)
57437c478bd9Sstevel@tonic-gate 		return (B_FALSE);
57447c478bd9Sstevel@tonic-gate 	return (B_TRUE);
57457c478bd9Sstevel@tonic-gate }
57467c478bd9Sstevel@tonic-gate 
57477c478bd9Sstevel@tonic-gate boolean_t
57487c478bd9Sstevel@tonic-gate zonecfg_valid_rctl(const char *name, const rctlblk_t *rctlblk)
57497c478bd9Sstevel@tonic-gate {
57507c478bd9Sstevel@tonic-gate 	rctlblk_t *current, *next;
57517c478bd9Sstevel@tonic-gate 	rctl_qty_t limit = rctlblk_get_value((rctlblk_t *)rctlblk);
57527c478bd9Sstevel@tonic-gate 	uint_t action = rctlblk_get_local_action((rctlblk_t *)rctlblk, NULL);
57537c478bd9Sstevel@tonic-gate 	uint_t global_flags;
57547c478bd9Sstevel@tonic-gate 
57557c478bd9Sstevel@tonic-gate 	if (!zonecfg_valid_rctlblk(rctlblk))
57567c478bd9Sstevel@tonic-gate 		return (B_FALSE);
57577c478bd9Sstevel@tonic-gate 	if (!zonecfg_valid_rctlname(name))
57587c478bd9Sstevel@tonic-gate 		return (B_FALSE);
57597c478bd9Sstevel@tonic-gate 
57607c478bd9Sstevel@tonic-gate 	current = alloca(rctlblk_size());
57617c478bd9Sstevel@tonic-gate 	if (getrctl(name, NULL, current, RCTL_FIRST) != 0)
57627c478bd9Sstevel@tonic-gate 		return (B_TRUE);	/* not an rctl on this system */
57637c478bd9Sstevel@tonic-gate 	/*
57647c478bd9Sstevel@tonic-gate 	 * Make sure the proposed value isn't greater than the current system
57657c478bd9Sstevel@tonic-gate 	 * value.
57667c478bd9Sstevel@tonic-gate 	 */
57677c478bd9Sstevel@tonic-gate 	next = alloca(rctlblk_size());
57687c478bd9Sstevel@tonic-gate 	while (rctlblk_get_privilege(current) != RCPRIV_SYSTEM) {
57697c478bd9Sstevel@tonic-gate 		rctlblk_t *tmp;
57707c478bd9Sstevel@tonic-gate 
57717c478bd9Sstevel@tonic-gate 		if (getrctl(name, current, next, RCTL_NEXT) != 0)
57727c478bd9Sstevel@tonic-gate 			return (B_FALSE);	/* shouldn't happen */
57737c478bd9Sstevel@tonic-gate 		tmp = current;
57747c478bd9Sstevel@tonic-gate 		current = next;
57757c478bd9Sstevel@tonic-gate 		next = tmp;
57767c478bd9Sstevel@tonic-gate 	}
57777c478bd9Sstevel@tonic-gate 	if (limit > rctlblk_get_value(current))
57787c478bd9Sstevel@tonic-gate 		return (B_FALSE);
57797c478bd9Sstevel@tonic-gate 
57807c478bd9Sstevel@tonic-gate 	/*
57817c478bd9Sstevel@tonic-gate 	 * Make sure the proposed action is allowed.
57827c478bd9Sstevel@tonic-gate 	 */
57837c478bd9Sstevel@tonic-gate 	global_flags = rctlblk_get_global_flags(current);
57847c478bd9Sstevel@tonic-gate 	if ((global_flags & RCTL_GLOBAL_DENY_NEVER) &&
57857c478bd9Sstevel@tonic-gate 	    action == RCTL_LOCAL_DENY)
57867c478bd9Sstevel@tonic-gate 		return (B_FALSE);
57877c478bd9Sstevel@tonic-gate 	if ((global_flags & RCTL_GLOBAL_DENY_ALWAYS) &&
57887c478bd9Sstevel@tonic-gate 	    action == RCTL_LOCAL_NOACTION)
57897c478bd9Sstevel@tonic-gate 		return (B_FALSE);
57907c478bd9Sstevel@tonic-gate 
57917c478bd9Sstevel@tonic-gate 	return (B_TRUE);
57927c478bd9Sstevel@tonic-gate }
5793fa9e4066Sahrens 
5794cf8f45c7Sdstaff /*
5795cf8f45c7Sdstaff  * There is always a race condition between reading the initial copy of
5796cf8f45c7Sdstaff  * a zones state and its state changing.  We address this by providing
5797cf8f45c7Sdstaff  * zonecfg_notify_critical_enter and zonecfg_noticy_critical_exit functions.
5798cf8f45c7Sdstaff  * When zonecfg_critical_enter is called, sets the state field to LOCKED
5799cf8f45c7Sdstaff  * and aquires biglock. Biglock protects against other threads executing
5800cf8f45c7Sdstaff  * critical_enter and the state field protects against state changes during
5801cf8f45c7Sdstaff  * the critical period.
5802cf8f45c7Sdstaff  *
5803cf8f45c7Sdstaff  * If any state changes occur, zn_cb will set the failed field of the znotify
5804cf8f45c7Sdstaff  * structure.  This will cause the critical_exit function to re-lock the
5805cf8f45c7Sdstaff  * channel and return an error. Since evsnts may be delayed, the critical_exit
5806cf8f45c7Sdstaff  * function "flushes" the queue by putting an event on the queue and waiting for
5807cf8f45c7Sdstaff  * zn_cb to notify critical_exit that it received the ping event.
5808cf8f45c7Sdstaff  */
5809cf8f45c7Sdstaff static const char *
5810cf8f45c7Sdstaff string_get_tok(const char *in, char delim, int num)
5811cf8f45c7Sdstaff {
5812cf8f45c7Sdstaff 	int i = 0;
5813cf8f45c7Sdstaff 
5814cf8f45c7Sdstaff 	for (; i < num; in++) {
5815cf8f45c7Sdstaff 		if (*in == delim)
5816cf8f45c7Sdstaff 			i++;
5817cf8f45c7Sdstaff 		if (*in == 0)
5818cf8f45c7Sdstaff 			return (NULL);
5819cf8f45c7Sdstaff 	}
5820cf8f45c7Sdstaff 	return (in);
5821cf8f45c7Sdstaff }
5822cf8f45c7Sdstaff 
5823cf8f45c7Sdstaff static boolean_t
5824cf8f45c7Sdstaff is_ping(sysevent_t *ev)
5825cf8f45c7Sdstaff {
5826cf8f45c7Sdstaff 	if (strcmp(sysevent_get_subclass_name(ev),
5827cf8f45c7Sdstaff 	    ZONE_EVENT_PING_SUBCLASS) == 0) {
5828cf8f45c7Sdstaff 		return (B_TRUE);
5829cf8f45c7Sdstaff 	} else {
5830cf8f45c7Sdstaff 		return (B_FALSE);
5831cf8f45c7Sdstaff 	}
5832cf8f45c7Sdstaff }
5833cf8f45c7Sdstaff 
5834cf8f45c7Sdstaff static boolean_t
5835cf8f45c7Sdstaff is_my_ping(sysevent_t *ev)
5836cf8f45c7Sdstaff {
5837cf8f45c7Sdstaff 	const char *sender;
5838cf8f45c7Sdstaff 	char mypid[sizeof (pid_t) * 3 + 1];
5839cf8f45c7Sdstaff 
5840cf8f45c7Sdstaff 	(void) snprintf(mypid, sizeof (mypid), "%i", getpid());
5841cf8f45c7Sdstaff 	sender = string_get_tok(sysevent_get_pub(ev), ':', 3);
5842cf8f45c7Sdstaff 	if (sender == NULL)
5843cf8f45c7Sdstaff 		return (B_FALSE);
5844cf8f45c7Sdstaff 	if (strcmp(sender, mypid) != 0)
5845cf8f45c7Sdstaff 		return (B_FALSE);
5846cf8f45c7Sdstaff 	return (B_TRUE);
5847cf8f45c7Sdstaff }
5848cf8f45c7Sdstaff 
5849cf8f45c7Sdstaff static int
5850cf8f45c7Sdstaff do_callback(struct znotify *zevtchan, sysevent_t *ev)
5851cf8f45c7Sdstaff {
5852cf8f45c7Sdstaff 	nvlist_t *l;
5853cf8f45c7Sdstaff 	int zid;
5854cf8f45c7Sdstaff 	char *zonename;
5855cf8f45c7Sdstaff 	char *newstate;
5856cf8f45c7Sdstaff 	char *oldstate;
5857cf8f45c7Sdstaff 	int ret;
5858cf8f45c7Sdstaff 	hrtime_t when;
5859cf8f45c7Sdstaff 
5860cf8f45c7Sdstaff 	if (strcmp(sysevent_get_subclass_name(ev),
5861cf8f45c7Sdstaff 	    ZONE_EVENT_STATUS_SUBCLASS) == 0) {
5862cf8f45c7Sdstaff 
5863cf8f45c7Sdstaff 		if (sysevent_get_attr_list(ev, &l) != 0) {
5864cf8f45c7Sdstaff 			if (errno == ENOMEM) {
5865cf8f45c7Sdstaff 				zevtchan->zn_failure_count++;
5866cf8f45c7Sdstaff 				return (EAGAIN);
5867cf8f45c7Sdstaff 			}
5868cf8f45c7Sdstaff 			return (0);
5869cf8f45c7Sdstaff 		}
5870cf8f45c7Sdstaff 		ret = 0;
5871cf8f45c7Sdstaff 
5872cf8f45c7Sdstaff 		if ((nvlist_lookup_string(l, ZONE_CB_NAME, &zonename) == 0) &&
5873cf8f45c7Sdstaff 		    (nvlist_lookup_string(l, ZONE_CB_NEWSTATE, &newstate)
5874cf8f45c7Sdstaff 		    == 0) &&
5875cf8f45c7Sdstaff 		    (nvlist_lookup_string(l, ZONE_CB_OLDSTATE, &oldstate)
5876cf8f45c7Sdstaff 		    == 0) &&
5877cf8f45c7Sdstaff 		    (nvlist_lookup_uint64(l, ZONE_CB_TIMESTAMP,
5878cf8f45c7Sdstaff 		    (uint64_t *)&when) == 0) &&
5879cf8f45c7Sdstaff 		    (nvlist_lookup_int32(l, ZONE_CB_ZONEID, &zid) == 0)) {
5880cf8f45c7Sdstaff 			ret = zevtchan->zn_callback(zonename, zid, newstate,
5881cf8f45c7Sdstaff 			    oldstate, when, zevtchan->zn_private);
5882cf8f45c7Sdstaff 		}
5883cf8f45c7Sdstaff 
5884cf8f45c7Sdstaff 		zevtchan->zn_failure_count = 0;
5885cf8f45c7Sdstaff 		nvlist_free(l);
5886cf8f45c7Sdstaff 		return (ret);
5887cf8f45c7Sdstaff 	} else {
5888cf8f45c7Sdstaff 		/*
5889cf8f45c7Sdstaff 		 * We have received an event in an unknown subclass. Ignore.
5890cf8f45c7Sdstaff 		 */
5891cf8f45c7Sdstaff 		zevtchan->zn_failure_count = 0;
5892cf8f45c7Sdstaff 		return (0);
5893cf8f45c7Sdstaff 	}
5894cf8f45c7Sdstaff }
5895cf8f45c7Sdstaff 
5896cf8f45c7Sdstaff static int
5897cf8f45c7Sdstaff zn_cb(sysevent_t *ev, void *p)
5898cf8f45c7Sdstaff {
5899cf8f45c7Sdstaff 	struct znotify *zevtchan = p;
5900cf8f45c7Sdstaff 	int error;
5901cf8f45c7Sdstaff 
5902cf8f45c7Sdstaff 	(void) pthread_mutex_lock(&(zevtchan->zn_mutex));
5903cf8f45c7Sdstaff 
5904cf8f45c7Sdstaff 	if (is_ping(ev) && !is_my_ping(ev)) {
5905cf8f45c7Sdstaff 		(void) pthread_mutex_unlock((&zevtchan->zn_mutex));
5906cf8f45c7Sdstaff 		return (0);
5907cf8f45c7Sdstaff 	}
5908cf8f45c7Sdstaff 
5909cf8f45c7Sdstaff 	if (zevtchan->zn_state == ZN_LOCKED) {
5910cf8f45c7Sdstaff 		assert(!is_ping(ev));
5911cf8f45c7Sdstaff 		zevtchan->zn_failed = B_TRUE;
5912cf8f45c7Sdstaff 		(void) pthread_mutex_unlock(&(zevtchan->zn_mutex));
5913cf8f45c7Sdstaff 		return (0);
5914cf8f45c7Sdstaff 	}
5915cf8f45c7Sdstaff 
5916cf8f45c7Sdstaff 	if (zevtchan->zn_state == ZN_PING_INFLIGHT) {
5917cf8f45c7Sdstaff 		if (is_ping(ev)) {
5918cf8f45c7Sdstaff 			zevtchan->zn_state = ZN_PING_RECEIVED;
5919cf8f45c7Sdstaff 			(void) pthread_cond_signal(&(zevtchan->zn_cond));
5920cf8f45c7Sdstaff 			(void) pthread_mutex_unlock(&(zevtchan->zn_mutex));
5921cf8f45c7Sdstaff 			return (0);
5922cf8f45c7Sdstaff 		} else {
5923cf8f45c7Sdstaff 			zevtchan->zn_failed = B_TRUE;
5924cf8f45c7Sdstaff 			(void) pthread_mutex_unlock(&(zevtchan->zn_mutex));
5925cf8f45c7Sdstaff 			return (0);
5926cf8f45c7Sdstaff 		}
5927cf8f45c7Sdstaff 	}
5928cf8f45c7Sdstaff 
5929cf8f45c7Sdstaff 	if (zevtchan->zn_state == ZN_UNLOCKED) {
5930cf8f45c7Sdstaff 
5931cf8f45c7Sdstaff 		error = do_callback(zevtchan, ev);
5932cf8f45c7Sdstaff 		(void) pthread_mutex_unlock(&(zevtchan->zn_mutex));
5933cf8f45c7Sdstaff 		/*
5934cf8f45c7Sdstaff 		 * Every ENOMEM failure causes do_callback to increment
5935cf8f45c7Sdstaff 		 * zn_failure_count and every success causes it to
5936cf8f45c7Sdstaff 		 * set zn_failure_count to zero.  If we got EAGAIN,
5937cf8f45c7Sdstaff 		 * we will sleep for zn_failure_count seconds and return
5938cf8f45c7Sdstaff 		 * EAGAIN to gpec to try again.
5939cf8f45c7Sdstaff 		 *
5940cf8f45c7Sdstaff 		 * After 55 seconds, or 10 try's we give up and drop the
5941cf8f45c7Sdstaff 		 * event.
5942cf8f45c7Sdstaff 		 */
5943cf8f45c7Sdstaff 		if (error == EAGAIN) {
5944cf8f45c7Sdstaff 			if (zevtchan->zn_failure_count > ZONE_CB_RETRY_COUNT) {
5945cf8f45c7Sdstaff 				return (0);
5946cf8f45c7Sdstaff 			}
5947cf8f45c7Sdstaff 			(void) sleep(zevtchan->zn_failure_count);
5948cf8f45c7Sdstaff 		}
5949cf8f45c7Sdstaff 		return (error);
5950cf8f45c7Sdstaff 	}
5951cf8f45c7Sdstaff 
5952cf8f45c7Sdstaff 	if (zevtchan->zn_state == ZN_PING_RECEIVED) {
5953cf8f45c7Sdstaff 		(void) pthread_mutex_unlock(&(zevtchan->zn_mutex));
5954cf8f45c7Sdstaff 		return (0);
5955cf8f45c7Sdstaff 	}
5956cf8f45c7Sdstaff 
5957cf8f45c7Sdstaff 	abort();
5958cf8f45c7Sdstaff 	return (0);
5959cf8f45c7Sdstaff }
5960cf8f45c7Sdstaff 
5961cf8f45c7Sdstaff void
5962cf8f45c7Sdstaff zonecfg_notify_critical_enter(void *h)
5963cf8f45c7Sdstaff {
5964cf8f45c7Sdstaff 	struct znotify *zevtchan = h;
5965cf8f45c7Sdstaff 
5966cf8f45c7Sdstaff 	(void) pthread_mutex_lock(&(zevtchan->zn_bigmutex));
5967cf8f45c7Sdstaff 	zevtchan->zn_state = ZN_LOCKED;
5968cf8f45c7Sdstaff }
5969cf8f45c7Sdstaff 
5970cf8f45c7Sdstaff int
5971cf8f45c7Sdstaff zonecfg_notify_critical_exit(void * h)
5972cf8f45c7Sdstaff {
5973cf8f45c7Sdstaff 
5974cf8f45c7Sdstaff 	struct znotify *zevtchan = h;
5975cf8f45c7Sdstaff 
5976cf8f45c7Sdstaff 	if (zevtchan->zn_state == ZN_UNLOCKED)
5977cf8f45c7Sdstaff 		return (0);
5978cf8f45c7Sdstaff 
5979cf8f45c7Sdstaff 	(void) pthread_mutex_lock(&(zevtchan->zn_mutex));
5980cf8f45c7Sdstaff 	zevtchan->zn_state = ZN_PING_INFLIGHT;
5981cf8f45c7Sdstaff 
5982ee519a1fSgjelinek 	(void) sysevent_evc_publish(zevtchan->zn_eventchan,
5983ee519a1fSgjelinek 	    ZONE_EVENT_STATUS_CLASS,
5984cf8f45c7Sdstaff 	    ZONE_EVENT_PING_SUBCLASS, ZONE_EVENT_PING_PUBLISHER,
5985cf8f45c7Sdstaff 	    zevtchan->zn_subscriber_id, NULL, EVCH_SLEEP);
5986cf8f45c7Sdstaff 
5987cf8f45c7Sdstaff 	while (zevtchan->zn_state != ZN_PING_RECEIVED) {
5988cf8f45c7Sdstaff 		(void) pthread_cond_wait(&(zevtchan->zn_cond),
5989cf8f45c7Sdstaff 		    &(zevtchan->zn_mutex));
5990cf8f45c7Sdstaff 	}
5991cf8f45c7Sdstaff 
5992cf8f45c7Sdstaff 	if (zevtchan->zn_failed == B_TRUE) {
5993cf8f45c7Sdstaff 		zevtchan->zn_state = ZN_LOCKED;
5994cf8f45c7Sdstaff 		zevtchan->zn_failed = B_FALSE;
5995cf8f45c7Sdstaff 		(void) pthread_mutex_unlock(&(zevtchan->zn_mutex));
5996cf8f45c7Sdstaff 		return (1);
5997cf8f45c7Sdstaff 	}
5998cf8f45c7Sdstaff 
5999cf8f45c7Sdstaff 	zevtchan->zn_state = ZN_UNLOCKED;
6000cf8f45c7Sdstaff 	(void) pthread_mutex_unlock(&(zevtchan->zn_mutex));
6001cf8f45c7Sdstaff 	(void) pthread_mutex_unlock(&(zevtchan->zn_bigmutex));
6002cf8f45c7Sdstaff 	return (0);
6003cf8f45c7Sdstaff }
6004cf8f45c7Sdstaff 
6005cf8f45c7Sdstaff void
6006cf8f45c7Sdstaff zonecfg_notify_critical_abort(void *h)
6007cf8f45c7Sdstaff {
6008cf8f45c7Sdstaff 	struct znotify *zevtchan = h;
6009cf8f45c7Sdstaff 
6010cf8f45c7Sdstaff 	zevtchan->zn_state = ZN_UNLOCKED;
6011cf8f45c7Sdstaff 	zevtchan->zn_failed = B_FALSE;
6012cf8f45c7Sdstaff 	/*
6013cf8f45c7Sdstaff 	 * Don't do anything about zn_lock. If it is held, it could only be
6014cf8f45c7Sdstaff 	 * held by zn_cb and it will be unlocked soon.
6015cf8f45c7Sdstaff 	 */
6016cf8f45c7Sdstaff 	(void) pthread_mutex_unlock(&(zevtchan->zn_bigmutex));
6017cf8f45c7Sdstaff }
6018cf8f45c7Sdstaff 
6019cf8f45c7Sdstaff void *
6020cf8f45c7Sdstaff zonecfg_notify_bind(int(*func)(const char *zonename, zoneid_t zid,
6021cf8f45c7Sdstaff     const char *newstate, const char *oldstate, hrtime_t when, void *p),
6022cf8f45c7Sdstaff     void *p)
6023cf8f45c7Sdstaff {
6024cf8f45c7Sdstaff 	struct znotify *zevtchan;
6025cf8f45c7Sdstaff 	int i = 1;
6026cf8f45c7Sdstaff 	int r;
6027cf8f45c7Sdstaff 
6028cf8f45c7Sdstaff 	zevtchan = malloc(sizeof (struct znotify));
6029cf8f45c7Sdstaff 
6030cf8f45c7Sdstaff 	if (zevtchan == NULL)
6031cf8f45c7Sdstaff 		return (NULL);
6032cf8f45c7Sdstaff 
6033cf8f45c7Sdstaff 	zevtchan->zn_private = p;
6034cf8f45c7Sdstaff 	zevtchan->zn_callback = func;
6035cf8f45c7Sdstaff 	zevtchan->zn_state = ZN_UNLOCKED;
6036cf8f45c7Sdstaff 	zevtchan->zn_failed = B_FALSE;
6037cf8f45c7Sdstaff 
6038cf8f45c7Sdstaff 	if (pthread_mutex_init(&(zevtchan->zn_mutex), NULL))
60399d4be64eSdstaff 		goto out3;
6040cf8f45c7Sdstaff 	if (pthread_cond_init(&(zevtchan->zn_cond), NULL)) {
6041cf8f45c7Sdstaff 		(void) pthread_mutex_destroy(&(zevtchan->zn_mutex));
60429d4be64eSdstaff 		goto out3;
6043cf8f45c7Sdstaff 	}
6044cf8f45c7Sdstaff 	if (pthread_mutex_init(&(zevtchan->zn_bigmutex), NULL)) {
6045cf8f45c7Sdstaff 		(void) pthread_mutex_destroy(&(zevtchan->zn_mutex));
6046cf8f45c7Sdstaff 		(void) pthread_cond_destroy(&(zevtchan->zn_cond));
60479d4be64eSdstaff 		goto out3;
6048cf8f45c7Sdstaff 	}
6049cf8f45c7Sdstaff 
6050cf8f45c7Sdstaff 	if (sysevent_evc_bind(ZONE_EVENT_CHANNEL, &(zevtchan->zn_eventchan),
6051cf8f45c7Sdstaff 	    0) != 0)
6052cf8f45c7Sdstaff 		goto out2;
6053cf8f45c7Sdstaff 
6054cf8f45c7Sdstaff 	do {
6055cf8f45c7Sdstaff 		/*
6056cf8f45c7Sdstaff 		 * At 4 digits the subscriber ID gets too long and we have
6057cf8f45c7Sdstaff 		 * no chance of successfully registering.
6058cf8f45c7Sdstaff 		 */
6059cf8f45c7Sdstaff 		if (i > 999)
60609d4be64eSdstaff 			goto out1;
6061cf8f45c7Sdstaff 
6062cf8f45c7Sdstaff 		(void) sprintf(zevtchan->zn_subscriber_id, "zone_%li_%i",
6063cf8f45c7Sdstaff 		    getpid() % 999999l, i);
6064cf8f45c7Sdstaff 
6065cf8f45c7Sdstaff 		r = sysevent_evc_subscribe(zevtchan->zn_eventchan,
6066cf8f45c7Sdstaff 		    zevtchan->zn_subscriber_id, ZONE_EVENT_STATUS_CLASS, zn_cb,
6067cf8f45c7Sdstaff 		    zevtchan, 0);
6068cf8f45c7Sdstaff 
6069cf8f45c7Sdstaff 		i++;
6070cf8f45c7Sdstaff 
6071cf8f45c7Sdstaff 	} while (r);
6072cf8f45c7Sdstaff 
6073cf8f45c7Sdstaff 	return (zevtchan);
60749d4be64eSdstaff out1:
607549b225e1SGavin Maltby 	(void) sysevent_evc_unbind(zevtchan->zn_eventchan);
60769d4be64eSdstaff out2:
6077cf8f45c7Sdstaff 	(void) pthread_mutex_destroy(&zevtchan->zn_mutex);
6078cf8f45c7Sdstaff 	(void) pthread_cond_destroy(&zevtchan->zn_cond);
6079cf8f45c7Sdstaff 	(void) pthread_mutex_destroy(&(zevtchan->zn_bigmutex));
60809d4be64eSdstaff out3:
6081cf8f45c7Sdstaff 	free(zevtchan);
6082cf8f45c7Sdstaff 
6083cf8f45c7Sdstaff 	return (NULL);
6084cf8f45c7Sdstaff }
6085cf8f45c7Sdstaff 
6086cf8f45c7Sdstaff void
6087cf8f45c7Sdstaff zonecfg_notify_unbind(void *handle)
6088cf8f45c7Sdstaff {
6089cf8f45c7Sdstaff 
6090cf8f45c7Sdstaff 	int ret;
6091cf8f45c7Sdstaff 
609249b225e1SGavin Maltby 	(void) sysevent_evc_unbind(((struct znotify *)handle)->zn_eventchan);
6093cf8f45c7Sdstaff 	/*
6094cf8f45c7Sdstaff 	 * Check that all evc threads have gone away. This should be
6095cf8f45c7Sdstaff 	 * enforced by sysevent_evc_unbind.
6096cf8f45c7Sdstaff 	 */
6097cf8f45c7Sdstaff 	ret = pthread_mutex_trylock(&((struct znotify *)handle)->zn_mutex);
6098cf8f45c7Sdstaff 
6099cf8f45c7Sdstaff 	if (ret)
6100cf8f45c7Sdstaff 		abort();
6101cf8f45c7Sdstaff 
6102cf8f45c7Sdstaff 	(void) pthread_mutex_unlock(&((struct znotify *)handle)->zn_mutex);
6103cf8f45c7Sdstaff 	(void) pthread_mutex_destroy(&((struct znotify *)handle)->zn_mutex);
6104cf8f45c7Sdstaff 	(void) pthread_cond_destroy(&((struct znotify *)handle)->zn_cond);
6105cf8f45c7Sdstaff 	(void) pthread_mutex_destroy(&((struct znotify *)handle)->zn_bigmutex);
6106cf8f45c7Sdstaff 
6107cf8f45c7Sdstaff 	free(handle);
6108cf8f45c7Sdstaff }
6109cf8f45c7Sdstaff 
6110fa9e4066Sahrens static int
6111fa9e4066Sahrens zonecfg_add_ds_core(zone_dochandle_t handle, struct zone_dstab *tabptr)
6112fa9e4066Sahrens {
6113fa9e4066Sahrens 	xmlNodePtr newnode, cur = handle->zone_dh_cur;
6114fa9e4066Sahrens 	int err;
6115fa9e4066Sahrens 
6116fa9e4066Sahrens 	newnode = xmlNewTextChild(cur, NULL, DTD_ELEM_DATASET, NULL);
6117fa9e4066Sahrens 	if ((err = newprop(newnode, DTD_ATTR_NAME,
6118fa9e4066Sahrens 	    tabptr->zone_dataset_name)) != Z_OK)
6119fa9e4066Sahrens 		return (err);
6120fa9e4066Sahrens 	return (Z_OK);
6121fa9e4066Sahrens }
6122fa9e4066Sahrens 
6123fa9e4066Sahrens int
6124fa9e4066Sahrens zonecfg_add_ds(zone_dochandle_t handle, struct zone_dstab *tabptr)
6125fa9e4066Sahrens {
6126fa9e4066Sahrens 	int err;
6127fa9e4066Sahrens 
6128fa9e4066Sahrens 	if (tabptr == NULL)
6129fa9e4066Sahrens 		return (Z_INVAL);
6130fa9e4066Sahrens 
6131fa9e4066Sahrens 	if ((err = operation_prep(handle)) != Z_OK)
6132fa9e4066Sahrens 		return (err);
6133fa9e4066Sahrens 
6134fa9e4066Sahrens 	if ((err = zonecfg_add_ds_core(handle, tabptr)) != Z_OK)
6135fa9e4066Sahrens 		return (err);
6136fa9e4066Sahrens 
6137fa9e4066Sahrens 	return (Z_OK);
6138fa9e4066Sahrens }
6139fa9e4066Sahrens 
6140fa9e4066Sahrens static int
6141fa9e4066Sahrens zonecfg_delete_ds_core(zone_dochandle_t handle, struct zone_dstab *tabptr)
6142fa9e4066Sahrens {
6143fa9e4066Sahrens 	xmlNodePtr cur = handle->zone_dh_cur;
6144fa9e4066Sahrens 
6145fa9e4066Sahrens 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
6146fa9e4066Sahrens 		if (xmlStrcmp(cur->name, DTD_ELEM_DATASET))
6147fa9e4066Sahrens 			continue;
6148fa9e4066Sahrens 
6149fa9e4066Sahrens 		if (match_prop(cur, DTD_ATTR_NAME,
6150fa9e4066Sahrens 		    tabptr->zone_dataset_name)) {
6151fa9e4066Sahrens 			xmlUnlinkNode(cur);
6152fa9e4066Sahrens 			xmlFreeNode(cur);
6153fa9e4066Sahrens 			return (Z_OK);
6154fa9e4066Sahrens 		}
6155fa9e4066Sahrens 	}
6156fa9e4066Sahrens 	return (Z_NO_RESOURCE_ID);
6157fa9e4066Sahrens }
6158fa9e4066Sahrens 
6159fa9e4066Sahrens int
6160fa9e4066Sahrens zonecfg_delete_ds(zone_dochandle_t handle, struct zone_dstab *tabptr)
6161fa9e4066Sahrens {
6162fa9e4066Sahrens 	int err;
6163fa9e4066Sahrens 
6164fa9e4066Sahrens 	if (tabptr == NULL)
6165fa9e4066Sahrens 		return (Z_INVAL);
6166fa9e4066Sahrens 
6167fa9e4066Sahrens 	if ((err = operation_prep(handle)) != Z_OK)
6168fa9e4066Sahrens 		return (err);
6169fa9e4066Sahrens 
6170fa9e4066Sahrens 	if ((err = zonecfg_delete_ds_core(handle, tabptr)) != Z_OK)
6171fa9e4066Sahrens 		return (err);
6172fa9e4066Sahrens 
6173fa9e4066Sahrens 	return (Z_OK);
6174fa9e4066Sahrens }
6175fa9e4066Sahrens 
6176fa9e4066Sahrens int
6177fa9e4066Sahrens zonecfg_modify_ds(
6178fa9e4066Sahrens 	zone_dochandle_t handle,
6179fa9e4066Sahrens 	struct zone_dstab *oldtabptr,
6180fa9e4066Sahrens 	struct zone_dstab *newtabptr)
6181fa9e4066Sahrens {
6182fa9e4066Sahrens 	int err;
6183fa9e4066Sahrens 
6184fa9e4066Sahrens 	if (oldtabptr == NULL || newtabptr == NULL)
6185fa9e4066Sahrens 		return (Z_INVAL);
6186fa9e4066Sahrens 
6187fa9e4066Sahrens 	if ((err = operation_prep(handle)) != Z_OK)
6188fa9e4066Sahrens 		return (err);
6189fa9e4066Sahrens 
6190fa9e4066Sahrens 	if ((err = zonecfg_delete_ds_core(handle, oldtabptr)) != Z_OK)
6191fa9e4066Sahrens 		return (err);
6192fa9e4066Sahrens 
6193fa9e4066Sahrens 	if ((err = zonecfg_add_ds_core(handle, newtabptr)) != Z_OK)
6194fa9e4066Sahrens 		return (err);
6195fa9e4066Sahrens 
6196fa9e4066Sahrens 	return (Z_OK);
6197fa9e4066Sahrens }
6198fa9e4066Sahrens 
6199fa9e4066Sahrens int
6200fa9e4066Sahrens zonecfg_lookup_ds(zone_dochandle_t handle, struct zone_dstab *tabptr)
6201fa9e4066Sahrens {
6202fa9e4066Sahrens 	xmlNodePtr cur, firstmatch;
6203fa9e4066Sahrens 	int err;
6204fa9e4066Sahrens 	char dataset[MAXNAMELEN];
6205fa9e4066Sahrens 
6206fa9e4066Sahrens 	if (tabptr == NULL)
6207fa9e4066Sahrens 		return (Z_INVAL);
6208fa9e4066Sahrens 
6209fa9e4066Sahrens 	if ((err = operation_prep(handle)) != Z_OK)
6210fa9e4066Sahrens 		return (err);
6211fa9e4066Sahrens 
6212fa9e4066Sahrens 	cur = handle->zone_dh_cur;
6213fa9e4066Sahrens 	firstmatch = NULL;
6214fa9e4066Sahrens 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
6215fa9e4066Sahrens 		if (xmlStrcmp(cur->name, DTD_ELEM_DATASET))
6216fa9e4066Sahrens 			continue;
6217fa9e4066Sahrens 		if (strlen(tabptr->zone_dataset_name) > 0) {
6218fa9e4066Sahrens 			if ((fetchprop(cur, DTD_ATTR_NAME, dataset,
6219fa9e4066Sahrens 			    sizeof (dataset)) == Z_OK) &&
6220fa9e4066Sahrens 			    (strcmp(tabptr->zone_dataset_name,
6221fa9e4066Sahrens 			    dataset) == 0)) {
6222fa9e4066Sahrens 				if (firstmatch == NULL)
6223fa9e4066Sahrens 					firstmatch = cur;
6224fa9e4066Sahrens 				else
6225fa9e4066Sahrens 					return (Z_INSUFFICIENT_SPEC);
6226fa9e4066Sahrens 			}
6227fa9e4066Sahrens 		}
6228fa9e4066Sahrens 	}
6229fa9e4066Sahrens 	if (firstmatch == NULL)
6230fa9e4066Sahrens 		return (Z_NO_RESOURCE_ID);
6231fa9e4066Sahrens 
6232fa9e4066Sahrens 	cur = firstmatch;
6233fa9e4066Sahrens 
6234fa9e4066Sahrens 	if ((err = fetchprop(cur, DTD_ATTR_NAME, tabptr->zone_dataset_name,
6235fa9e4066Sahrens 	    sizeof (tabptr->zone_dataset_name))) != Z_OK)
6236fa9e4066Sahrens 		return (err);
6237fa9e4066Sahrens 
6238fa9e4066Sahrens 	return (Z_OK);
6239fa9e4066Sahrens }
6240fa9e4066Sahrens 
6241fa9e4066Sahrens int
6242fa9e4066Sahrens zonecfg_setdsent(zone_dochandle_t handle)
6243fa9e4066Sahrens {
6244fa9e4066Sahrens 	return (zonecfg_setent(handle));
6245fa9e4066Sahrens }
6246fa9e4066Sahrens 
6247fa9e4066Sahrens int
6248fa9e4066Sahrens zonecfg_getdsent(zone_dochandle_t handle, struct zone_dstab *tabptr)
6249fa9e4066Sahrens {
6250fa9e4066Sahrens 	xmlNodePtr cur;
6251fa9e4066Sahrens 	int err;
6252fa9e4066Sahrens 
6253fa9e4066Sahrens 	if (handle == NULL)
6254fa9e4066Sahrens 		return (Z_INVAL);
6255fa9e4066Sahrens 
6256fa9e4066Sahrens 	if ((cur = handle->zone_dh_cur) == NULL)
6257fa9e4066Sahrens 		return (Z_NO_ENTRY);
6258fa9e4066Sahrens 
6259fa9e4066Sahrens 	for (; cur != NULL; cur = cur->next)
6260fa9e4066Sahrens 		if (!xmlStrcmp(cur->name, DTD_ELEM_DATASET))
6261fa9e4066Sahrens 			break;
6262fa9e4066Sahrens 	if (cur == NULL) {
6263fa9e4066Sahrens 		handle->zone_dh_cur = handle->zone_dh_top;
6264fa9e4066Sahrens 		return (Z_NO_ENTRY);
6265fa9e4066Sahrens 	}
6266fa9e4066Sahrens 
6267fa9e4066Sahrens 	if ((err = fetchprop(cur, DTD_ATTR_NAME, tabptr->zone_dataset_name,
6268fa9e4066Sahrens 	    sizeof (tabptr->zone_dataset_name))) != Z_OK) {
6269fa9e4066Sahrens 		handle->zone_dh_cur = handle->zone_dh_top;
6270fa9e4066Sahrens 		return (err);
6271fa9e4066Sahrens 	}
6272fa9e4066Sahrens 
6273fa9e4066Sahrens 	handle->zone_dh_cur = cur->next;
6274fa9e4066Sahrens 	return (Z_OK);
6275fa9e4066Sahrens }
6276fa9e4066Sahrens 
6277fa9e4066Sahrens int
6278fa9e4066Sahrens zonecfg_enddsent(zone_dochandle_t handle)
6279fa9e4066Sahrens {
6280fa9e4066Sahrens 	return (zonecfg_endent(handle));
6281fa9e4066Sahrens }
6282ee519a1fSgjelinek 
62830209230bSgjelinek /*
62840209230bSgjelinek  * Support for aliased rctls; that is, rctls that have simplified names in
62850209230bSgjelinek  * zonecfg.  For example, max-lwps is an alias for a well defined zone.max-lwps
62860209230bSgjelinek  * rctl.  If there are multiple existing values for one of these rctls or if
62870209230bSgjelinek  * there is a single value that does not match the well defined template (i.e.
62880209230bSgjelinek  * it has a different action) then we cannot treat the rctl as having an alias
62890209230bSgjelinek  * so we return Z_ALIAS_DISALLOW.  That means that the rctl cannot be
62900209230bSgjelinek  * managed in zonecfg via an alias and that the standard rctl syntax must be
62910209230bSgjelinek  * used.
62920209230bSgjelinek  *
62930209230bSgjelinek  * The possible return values are:
62940209230bSgjelinek  *	Z_NO_PROPERTY_ID - invalid alias name
62950209230bSgjelinek  *	Z_ALIAS_DISALLOW - pre-existing, incompatible rctl definition
62960209230bSgjelinek  *	Z_NO_ENTRY - no rctl is configured for this alias
62970209230bSgjelinek  *	Z_OK - we got a valid rctl for the specified alias
62980209230bSgjelinek  */
62990209230bSgjelinek int
63000209230bSgjelinek zonecfg_get_aliased_rctl(zone_dochandle_t handle, char *name, uint64_t *rval)
63010209230bSgjelinek {
63020209230bSgjelinek 	boolean_t found = B_FALSE;
63030209230bSgjelinek 	boolean_t found_val = B_FALSE;
63040209230bSgjelinek 	xmlNodePtr cur, val;
63050209230bSgjelinek 	char savedname[MAXNAMELEN];
63060209230bSgjelinek 	struct zone_rctlvaltab rctl;
63070209230bSgjelinek 	int i;
63080209230bSgjelinek 	int err;
63090209230bSgjelinek 
63100209230bSgjelinek 	for (i = 0; aliases[i].shortname != NULL; i++)
63110209230bSgjelinek 		if (strcmp(name, aliases[i].shortname) == 0)
63120209230bSgjelinek 			break;
63130209230bSgjelinek 
63140209230bSgjelinek 	if (aliases[i].shortname == NULL)
63150209230bSgjelinek 		return (Z_NO_PROPERTY_ID);
63160209230bSgjelinek 
63170209230bSgjelinek 	if ((err = operation_prep(handle)) != Z_OK)
63180209230bSgjelinek 		return (err);
63190209230bSgjelinek 
63200209230bSgjelinek 	cur = handle->zone_dh_cur;
63210209230bSgjelinek 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
63220209230bSgjelinek 		if (xmlStrcmp(cur->name, DTD_ELEM_RCTL) != 0)
63230209230bSgjelinek 			continue;
63240209230bSgjelinek 		if ((fetchprop(cur, DTD_ATTR_NAME, savedname,
63250209230bSgjelinek 		    sizeof (savedname)) == Z_OK) &&
63260209230bSgjelinek 		    (strcmp(savedname, aliases[i].realname) == 0)) {
63270209230bSgjelinek 
63280209230bSgjelinek 			/*
63290209230bSgjelinek 			 * If we already saw one of these, we can't have an
63300209230bSgjelinek 			 * alias since we just found another.
63310209230bSgjelinek 			 */
63320209230bSgjelinek 			if (found)
63330209230bSgjelinek 				return (Z_ALIAS_DISALLOW);
63340209230bSgjelinek 			found = B_TRUE;
63350209230bSgjelinek 
63360209230bSgjelinek 			for (val = cur->xmlChildrenNode; val != NULL;
63370209230bSgjelinek 			    val = val->next) {
63380209230bSgjelinek 				/*
63390209230bSgjelinek 				 * If we already have one value, we can't have
63400209230bSgjelinek 				 * an alias since we just found another.
63410209230bSgjelinek 				 */
63420209230bSgjelinek 				if (found_val)
63430209230bSgjelinek 					return (Z_ALIAS_DISALLOW);
63440209230bSgjelinek 				found_val = B_TRUE;
63450209230bSgjelinek 
63460209230bSgjelinek 				if ((fetchprop(val, DTD_ATTR_PRIV,
63470209230bSgjelinek 				    rctl.zone_rctlval_priv,
63480209230bSgjelinek 				    sizeof (rctl.zone_rctlval_priv)) != Z_OK))
63490209230bSgjelinek 					break;
63500209230bSgjelinek 				if ((fetchprop(val, DTD_ATTR_LIMIT,
63510209230bSgjelinek 				    rctl.zone_rctlval_limit,
63520209230bSgjelinek 				    sizeof (rctl.zone_rctlval_limit)) != Z_OK))
63530209230bSgjelinek 					break;
63540209230bSgjelinek 				if ((fetchprop(val, DTD_ATTR_ACTION,
63550209230bSgjelinek 				    rctl.zone_rctlval_action,
63560209230bSgjelinek 				    sizeof (rctl.zone_rctlval_action)) != Z_OK))
63570209230bSgjelinek 					break;
63580209230bSgjelinek 			}
63590209230bSgjelinek 
63600209230bSgjelinek 			/* check priv and action match the expected vals */
63610209230bSgjelinek 			if (strcmp(rctl.zone_rctlval_priv,
63620209230bSgjelinek 			    aliases[i].priv) != 0 ||
63630209230bSgjelinek 			    strcmp(rctl.zone_rctlval_action,
63640209230bSgjelinek 			    aliases[i].action) != 0)
63650209230bSgjelinek 				return (Z_ALIAS_DISALLOW);
63660209230bSgjelinek 		}
63670209230bSgjelinek 	}
63680209230bSgjelinek 
63690209230bSgjelinek 	if (found) {
63700209230bSgjelinek 		*rval = strtoull(rctl.zone_rctlval_limit, NULL, 10);
63710209230bSgjelinek 		return (Z_OK);
63720209230bSgjelinek 	}
63730209230bSgjelinek 
63740209230bSgjelinek 	return (Z_NO_ENTRY);
63750209230bSgjelinek }
63760209230bSgjelinek 
63770209230bSgjelinek int
63780209230bSgjelinek zonecfg_rm_aliased_rctl(zone_dochandle_t handle, char *name)
63790209230bSgjelinek {
63800209230bSgjelinek 	int i;
63810209230bSgjelinek 	uint64_t val;
63820209230bSgjelinek 	struct zone_rctltab rctltab;
63830209230bSgjelinek 
63840209230bSgjelinek 	/*
63850209230bSgjelinek 	 * First check that we have a valid aliased rctl to remove.
63860209230bSgjelinek 	 * This will catch an rctl entry with non-standard values or
63870209230bSgjelinek 	 * multiple rctl values for this name.  We need to ignore those
63880209230bSgjelinek 	 * rctl entries.
63890209230bSgjelinek 	 */
63900209230bSgjelinek 	if (zonecfg_get_aliased_rctl(handle, name, &val) != Z_OK)
63910209230bSgjelinek 		return (Z_OK);
63920209230bSgjelinek 
63930209230bSgjelinek 	for (i = 0; aliases[i].shortname != NULL; i++)
63940209230bSgjelinek 		if (strcmp(name, aliases[i].shortname) == 0)
63950209230bSgjelinek 			break;
63960209230bSgjelinek 
63970209230bSgjelinek 	if (aliases[i].shortname == NULL)
63980209230bSgjelinek 		return (Z_NO_RESOURCE_ID);
63990209230bSgjelinek 
64000209230bSgjelinek 	(void) strlcpy(rctltab.zone_rctl_name, aliases[i].realname,
64010209230bSgjelinek 	    sizeof (rctltab.zone_rctl_name));
64020209230bSgjelinek 
64030209230bSgjelinek 	return (zonecfg_delete_rctl(handle, &rctltab));
64040209230bSgjelinek }
64050209230bSgjelinek 
64060209230bSgjelinek boolean_t
64070209230bSgjelinek zonecfg_aliased_rctl_ok(zone_dochandle_t handle, char *name)
64080209230bSgjelinek {
64090209230bSgjelinek 	uint64_t tmp_val;
64100209230bSgjelinek 
64110209230bSgjelinek 	switch (zonecfg_get_aliased_rctl(handle, name, &tmp_val)) {
64120209230bSgjelinek 	case Z_OK:
64130209230bSgjelinek 		/*FALLTHRU*/
64140209230bSgjelinek 	case Z_NO_ENTRY:
64150209230bSgjelinek 		return (B_TRUE);
64160209230bSgjelinek 	default:
64170209230bSgjelinek 		return (B_FALSE);
64180209230bSgjelinek 	}
64190209230bSgjelinek }
64200209230bSgjelinek 
64210209230bSgjelinek int
64220209230bSgjelinek zonecfg_set_aliased_rctl(zone_dochandle_t handle, char *name, uint64_t val)
64230209230bSgjelinek {
64240209230bSgjelinek 	int i;
64250209230bSgjelinek 	int err;
64260209230bSgjelinek 	struct zone_rctltab rctltab;
64270209230bSgjelinek 	struct zone_rctlvaltab *rctlvaltab;
64280209230bSgjelinek 	char buf[128];
64290209230bSgjelinek 
64300209230bSgjelinek 	if (!zonecfg_aliased_rctl_ok(handle, name))
64310209230bSgjelinek 		return (Z_ALIAS_DISALLOW);
64320209230bSgjelinek 
64330209230bSgjelinek 	for (i = 0; aliases[i].shortname != NULL; i++)
64340209230bSgjelinek 		if (strcmp(name, aliases[i].shortname) == 0)
64350209230bSgjelinek 			break;
64360209230bSgjelinek 
64370209230bSgjelinek 	if (aliases[i].shortname == NULL)
64380209230bSgjelinek 		return (Z_NO_RESOURCE_ID);
64390209230bSgjelinek 
64400209230bSgjelinek 	/* remove any pre-existing definition for this rctl */
64410209230bSgjelinek 	(void) zonecfg_rm_aliased_rctl(handle, name);
64420209230bSgjelinek 
64430209230bSgjelinek 	(void) strlcpy(rctltab.zone_rctl_name, aliases[i].realname,
64440209230bSgjelinek 	    sizeof (rctltab.zone_rctl_name));
64450209230bSgjelinek 
64460209230bSgjelinek 	rctltab.zone_rctl_valptr = NULL;
64470209230bSgjelinek 
64480209230bSgjelinek 	if ((rctlvaltab = calloc(1, sizeof (struct zone_rctlvaltab))) == NULL)
64490209230bSgjelinek 		return (Z_NOMEM);
64500209230bSgjelinek 
64510209230bSgjelinek 	(void) snprintf(buf, sizeof (buf), "%llu", (long long)val);
64520209230bSgjelinek 
64530209230bSgjelinek 	(void) strlcpy(rctlvaltab->zone_rctlval_priv, aliases[i].priv,
64540209230bSgjelinek 	    sizeof (rctlvaltab->zone_rctlval_priv));
64550209230bSgjelinek 	(void) strlcpy(rctlvaltab->zone_rctlval_limit, buf,
64560209230bSgjelinek 	    sizeof (rctlvaltab->zone_rctlval_limit));
64570209230bSgjelinek 	(void) strlcpy(rctlvaltab->zone_rctlval_action, aliases[i].action,
64580209230bSgjelinek 	    sizeof (rctlvaltab->zone_rctlval_action));
64590209230bSgjelinek 
64600209230bSgjelinek 	rctlvaltab->zone_rctlval_next = NULL;
64610209230bSgjelinek 
64620209230bSgjelinek 	if ((err = zonecfg_add_rctl_value(&rctltab, rctlvaltab)) != Z_OK)
64630209230bSgjelinek 		return (err);
64640209230bSgjelinek 
64650209230bSgjelinek 	return (zonecfg_add_rctl(handle, &rctltab));
64660209230bSgjelinek }
64670209230bSgjelinek 
64680209230bSgjelinek static int
64690209230bSgjelinek delete_tmp_pool(zone_dochandle_t handle)
64700209230bSgjelinek {
64710209230bSgjelinek 	int err;
64720209230bSgjelinek 	xmlNodePtr cur = handle->zone_dh_cur;
64730209230bSgjelinek 
64740209230bSgjelinek 	if ((err = operation_prep(handle)) != Z_OK)
64750209230bSgjelinek 		return (err);
64760209230bSgjelinek 
64770209230bSgjelinek 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
64780209230bSgjelinek 		if (xmlStrcmp(cur->name, DTD_ELEM_TMPPOOL) == 0) {
64790209230bSgjelinek 			xmlUnlinkNode(cur);
64800209230bSgjelinek 			xmlFreeNode(cur);
64810209230bSgjelinek 			return (Z_OK);
64820209230bSgjelinek 		}
64830209230bSgjelinek 	}
64840209230bSgjelinek 
64850209230bSgjelinek 	return (Z_NO_RESOURCE_ID);
64860209230bSgjelinek }
64870209230bSgjelinek 
64880209230bSgjelinek static int
64890209230bSgjelinek modify_tmp_pool(zone_dochandle_t handle, char *pool_importance)
64900209230bSgjelinek {
64910209230bSgjelinek 	int err;
64920209230bSgjelinek 	xmlNodePtr cur = handle->zone_dh_cur;
64930209230bSgjelinek 	xmlNodePtr newnode;
64940209230bSgjelinek 
64950209230bSgjelinek 	err = delete_tmp_pool(handle);
64960209230bSgjelinek 	if (err != Z_OK && err != Z_NO_RESOURCE_ID)
64970209230bSgjelinek 		return (err);
64980209230bSgjelinek 
64990209230bSgjelinek 	if (*pool_importance != '\0') {
65000209230bSgjelinek 		if ((err = operation_prep(handle)) != Z_OK)
65010209230bSgjelinek 			return (err);
65020209230bSgjelinek 
65030209230bSgjelinek 		newnode = xmlNewTextChild(cur, NULL, DTD_ELEM_TMPPOOL, NULL);
65040209230bSgjelinek 		if ((err = newprop(newnode, DTD_ATTR_IMPORTANCE,
65050209230bSgjelinek 		    pool_importance)) != Z_OK)
65060209230bSgjelinek 			return (err);
65070209230bSgjelinek 	}
65080209230bSgjelinek 
65090209230bSgjelinek 	return (Z_OK);
65100209230bSgjelinek }
65110209230bSgjelinek 
65120209230bSgjelinek static int
65130209230bSgjelinek add_pset_core(zone_dochandle_t handle, struct zone_psettab *tabptr)
65140209230bSgjelinek {
65150209230bSgjelinek 	xmlNodePtr newnode, cur = handle->zone_dh_cur;
65160209230bSgjelinek 	int err;
65170209230bSgjelinek 
65180209230bSgjelinek 	newnode = xmlNewTextChild(cur, NULL, DTD_ELEM_PSET, NULL);
65190209230bSgjelinek 	if ((err = newprop(newnode, DTD_ATTR_NCPU_MIN,
65200209230bSgjelinek 	    tabptr->zone_ncpu_min)) != Z_OK)
65210209230bSgjelinek 		return (err);
65220209230bSgjelinek 	if ((err = newprop(newnode, DTD_ATTR_NCPU_MAX,
65230209230bSgjelinek 	    tabptr->zone_ncpu_max)) != Z_OK)
65240209230bSgjelinek 		return (err);
65250209230bSgjelinek 
65260209230bSgjelinek 	if ((err = modify_tmp_pool(handle, tabptr->zone_importance)) != Z_OK)
65270209230bSgjelinek 		return (err);
65280209230bSgjelinek 
65290209230bSgjelinek 	return (Z_OK);
65300209230bSgjelinek }
65310209230bSgjelinek 
65320209230bSgjelinek int
65330209230bSgjelinek zonecfg_add_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 = operation_prep(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_delete_pset(zone_dochandle_t handle)
65510209230bSgjelinek {
65520209230bSgjelinek 	int err;
65530209230bSgjelinek 	int res = Z_NO_RESOURCE_ID;
65540209230bSgjelinek 	xmlNodePtr cur = handle->zone_dh_cur;
65550209230bSgjelinek 
65560209230bSgjelinek 	if ((err = operation_prep(handle)) != Z_OK)
65570209230bSgjelinek 		return (err);
65580209230bSgjelinek 
65590209230bSgjelinek 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
65600209230bSgjelinek 		if (xmlStrcmp(cur->name, DTD_ELEM_PSET) == 0) {
65610209230bSgjelinek 			xmlUnlinkNode(cur);
65620209230bSgjelinek 			xmlFreeNode(cur);
65630209230bSgjelinek 			res = Z_OK;
65640209230bSgjelinek 			break;
65650209230bSgjelinek 		}
65660209230bSgjelinek 	}
65670209230bSgjelinek 
65680209230bSgjelinek 	/*
65690209230bSgjelinek 	 * Once we have msets, we should check that a mset
65700209230bSgjelinek 	 * do not exist before we delete the tmp_pool data.
65710209230bSgjelinek 	 */
65720209230bSgjelinek 	err = delete_tmp_pool(handle);
65730209230bSgjelinek 	if (err != Z_OK && err != Z_NO_RESOURCE_ID)
65740209230bSgjelinek 		return (err);
65750209230bSgjelinek 
65760209230bSgjelinek 	return (res);
65770209230bSgjelinek }
65780209230bSgjelinek 
65790209230bSgjelinek int
65800209230bSgjelinek zonecfg_modify_pset(zone_dochandle_t handle, struct zone_psettab *tabptr)
65810209230bSgjelinek {
65820209230bSgjelinek 	int err;
65830209230bSgjelinek 
65840209230bSgjelinek 	if (tabptr == NULL)
65850209230bSgjelinek 		return (Z_INVAL);
65860209230bSgjelinek 
65870209230bSgjelinek 	if ((err = zonecfg_delete_pset(handle)) != Z_OK)
65880209230bSgjelinek 		return (err);
65890209230bSgjelinek 
65900209230bSgjelinek 	if ((err = add_pset_core(handle, tabptr)) != Z_OK)
65910209230bSgjelinek 		return (err);
65920209230bSgjelinek 
65930209230bSgjelinek 	return (Z_OK);
65940209230bSgjelinek }
65950209230bSgjelinek 
65960209230bSgjelinek int
65970209230bSgjelinek zonecfg_lookup_pset(zone_dochandle_t handle, struct zone_psettab *tabptr)
65980209230bSgjelinek {
65990209230bSgjelinek 	xmlNodePtr cur;
66000209230bSgjelinek 	int err;
66010209230bSgjelinek 	int res = Z_NO_ENTRY;
66020209230bSgjelinek 
66030209230bSgjelinek 	if (tabptr == NULL)
66040209230bSgjelinek 		return (Z_INVAL);
66050209230bSgjelinek 
66060209230bSgjelinek 	if ((err = operation_prep(handle)) != Z_OK)
66070209230bSgjelinek 		return (err);
66080209230bSgjelinek 
66090209230bSgjelinek 	/* this is an optional component */
66100209230bSgjelinek 	tabptr->zone_importance[0] = '\0';
66110209230bSgjelinek 
66120209230bSgjelinek 	cur = handle->zone_dh_cur;
66130209230bSgjelinek 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
66140209230bSgjelinek 		if (xmlStrcmp(cur->name, DTD_ELEM_PSET) == 0) {
66150209230bSgjelinek 			if ((err = fetchprop(cur, DTD_ATTR_NCPU_MIN,
66160209230bSgjelinek 			    tabptr->zone_ncpu_min,
66170209230bSgjelinek 			    sizeof (tabptr->zone_ncpu_min))) != Z_OK) {
66180209230bSgjelinek 				handle->zone_dh_cur = handle->zone_dh_top;
66190209230bSgjelinek 				return (err);
66200209230bSgjelinek 			}
66210209230bSgjelinek 
66220209230bSgjelinek 			if ((err = fetchprop(cur, DTD_ATTR_NCPU_MAX,
66230209230bSgjelinek 			    tabptr->zone_ncpu_max,
66240209230bSgjelinek 			    sizeof (tabptr->zone_ncpu_max))) != Z_OK) {
66250209230bSgjelinek 				handle->zone_dh_cur = handle->zone_dh_top;
66260209230bSgjelinek 				return (err);
66270209230bSgjelinek 			}
66280209230bSgjelinek 
66290209230bSgjelinek 			res = Z_OK;
66300209230bSgjelinek 
66310209230bSgjelinek 		} else if (xmlStrcmp(cur->name, DTD_ELEM_TMPPOOL) == 0) {
66320209230bSgjelinek 			if ((err = fetchprop(cur, DTD_ATTR_IMPORTANCE,
66330209230bSgjelinek 			    tabptr->zone_importance,
66340209230bSgjelinek 			    sizeof (tabptr->zone_importance))) != Z_OK) {
66350209230bSgjelinek 				handle->zone_dh_cur = handle->zone_dh_top;
66360209230bSgjelinek 				return (err);
66370209230bSgjelinek 			}
66380209230bSgjelinek 		}
66390209230bSgjelinek 	}
66400209230bSgjelinek 
66410209230bSgjelinek 	return (res);
66420209230bSgjelinek }
66430209230bSgjelinek 
66440209230bSgjelinek int
66450209230bSgjelinek zonecfg_getpsetent(zone_dochandle_t handle, struct zone_psettab *tabptr)
66460209230bSgjelinek {
66470209230bSgjelinek 	int err;
66480209230bSgjelinek 
66490209230bSgjelinek 	if ((err = zonecfg_setent(handle)) != Z_OK)
66500209230bSgjelinek 		return (err);
66510209230bSgjelinek 
66520209230bSgjelinek 	err = zonecfg_lookup_pset(handle, tabptr);
66530209230bSgjelinek 
66540209230bSgjelinek 	(void) zonecfg_endent(handle);
66550209230bSgjelinek 
66560209230bSgjelinek 	return (err);
66570209230bSgjelinek }
66580209230bSgjelinek 
66590209230bSgjelinek static int
66600209230bSgjelinek add_mcap(zone_dochandle_t handle, struct zone_mcaptab *tabptr)
66610209230bSgjelinek {
66620209230bSgjelinek 	xmlNodePtr newnode, cur = handle->zone_dh_cur;
66630209230bSgjelinek 	int err;
66640209230bSgjelinek 
66650209230bSgjelinek 	newnode = xmlNewTextChild(cur, NULL, DTD_ELEM_MCAP, NULL);
66660209230bSgjelinek 	if ((err = newprop(newnode, DTD_ATTR_PHYSCAP, tabptr->zone_physmem_cap))
66670209230bSgjelinek 	    != Z_OK)
66680209230bSgjelinek 		return (err);
66690209230bSgjelinek 
66700209230bSgjelinek 	return (Z_OK);
66710209230bSgjelinek }
66720209230bSgjelinek 
66730209230bSgjelinek int
66740209230bSgjelinek zonecfg_delete_mcap(zone_dochandle_t handle)
66750209230bSgjelinek {
66760209230bSgjelinek 	int err;
66770209230bSgjelinek 	xmlNodePtr cur = handle->zone_dh_cur;
66780209230bSgjelinek 
66790209230bSgjelinek 	if ((err = operation_prep(handle)) != Z_OK)
66800209230bSgjelinek 		return (err);
66810209230bSgjelinek 
66820209230bSgjelinek 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
66830209230bSgjelinek 		if (xmlStrcmp(cur->name, DTD_ELEM_MCAP) != 0)
66840209230bSgjelinek 			continue;
66850209230bSgjelinek 
66860209230bSgjelinek 		xmlUnlinkNode(cur);
66870209230bSgjelinek 		xmlFreeNode(cur);
66880209230bSgjelinek 		return (Z_OK);
66890209230bSgjelinek 	}
66900209230bSgjelinek 	return (Z_NO_RESOURCE_ID);
66910209230bSgjelinek }
66920209230bSgjelinek 
66930209230bSgjelinek int
66940209230bSgjelinek zonecfg_modify_mcap(zone_dochandle_t handle, struct zone_mcaptab *tabptr)
66950209230bSgjelinek {
66960209230bSgjelinek 	int err;
66970209230bSgjelinek 
66980209230bSgjelinek 	if (tabptr == NULL)
66990209230bSgjelinek 		return (Z_INVAL);
67000209230bSgjelinek 
67010209230bSgjelinek 	err = zonecfg_delete_mcap(handle);
67020209230bSgjelinek 	/* it is ok if there is no mcap entry */
67030209230bSgjelinek 	if (err != Z_OK && err != Z_NO_RESOURCE_ID)
67040209230bSgjelinek 		return (err);
67050209230bSgjelinek 
67060209230bSgjelinek 	if ((err = add_mcap(handle, tabptr)) != Z_OK)
67070209230bSgjelinek 		return (err);
67080209230bSgjelinek 
67090209230bSgjelinek 	return (Z_OK);
67100209230bSgjelinek }
67110209230bSgjelinek 
67120209230bSgjelinek int
67130209230bSgjelinek zonecfg_lookup_mcap(zone_dochandle_t handle, struct zone_mcaptab *tabptr)
67140209230bSgjelinek {
67150209230bSgjelinek 	xmlNodePtr cur;
67160209230bSgjelinek 	int err;
67170209230bSgjelinek 
67180209230bSgjelinek 	if (tabptr == NULL)
67190209230bSgjelinek 		return (Z_INVAL);
67200209230bSgjelinek 
67210209230bSgjelinek 	if ((err = operation_prep(handle)) != Z_OK)
67220209230bSgjelinek 		return (err);
67230209230bSgjelinek 
67240209230bSgjelinek 	cur = handle->zone_dh_cur;
67250209230bSgjelinek 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
67260209230bSgjelinek 		if (xmlStrcmp(cur->name, DTD_ELEM_MCAP) != 0)
67270209230bSgjelinek 			continue;
67280209230bSgjelinek 		if ((err = fetchprop(cur, DTD_ATTR_PHYSCAP,
67290209230bSgjelinek 		    tabptr->zone_physmem_cap,
67300209230bSgjelinek 		    sizeof (tabptr->zone_physmem_cap))) != Z_OK) {
67310209230bSgjelinek 			handle->zone_dh_cur = handle->zone_dh_top;
67320209230bSgjelinek 			return (err);
67330209230bSgjelinek 		}
67340209230bSgjelinek 
67350209230bSgjelinek 		return (Z_OK);
67360209230bSgjelinek 	}
67370209230bSgjelinek 
67380209230bSgjelinek 	return (Z_NO_ENTRY);
67390209230bSgjelinek }
67400209230bSgjelinek 
67410209230bSgjelinek static int
67420209230bSgjelinek getmcapent_core(zone_dochandle_t handle, struct zone_mcaptab *tabptr)
67430209230bSgjelinek {
67440209230bSgjelinek 	xmlNodePtr cur;
67450209230bSgjelinek 	int err;
67460209230bSgjelinek 
67470209230bSgjelinek 	if (handle == NULL)
67480209230bSgjelinek 		return (Z_INVAL);
67490209230bSgjelinek 
67500209230bSgjelinek 	if ((cur = handle->zone_dh_cur) == NULL)
67510209230bSgjelinek 		return (Z_NO_ENTRY);
67520209230bSgjelinek 
67530209230bSgjelinek 	for (; cur != NULL; cur = cur->next)
67540209230bSgjelinek 		if (xmlStrcmp(cur->name, DTD_ELEM_MCAP) == 0)
67550209230bSgjelinek 			break;
67560209230bSgjelinek 	if (cur == NULL) {
67570209230bSgjelinek 		handle->zone_dh_cur = handle->zone_dh_top;
67580209230bSgjelinek 		return (Z_NO_ENTRY);
67590209230bSgjelinek 	}
67600209230bSgjelinek 
67610209230bSgjelinek 	if ((err = fetchprop(cur, DTD_ATTR_PHYSCAP, tabptr->zone_physmem_cap,
67620209230bSgjelinek 	    sizeof (tabptr->zone_physmem_cap))) != Z_OK) {
67630209230bSgjelinek 		handle->zone_dh_cur = handle->zone_dh_top;
67640209230bSgjelinek 		return (err);
67650209230bSgjelinek 	}
67660209230bSgjelinek 
67670209230bSgjelinek 	handle->zone_dh_cur = cur->next;
67680209230bSgjelinek 	return (Z_OK);
67690209230bSgjelinek }
67700209230bSgjelinek 
67710209230bSgjelinek int
67720209230bSgjelinek zonecfg_getmcapent(zone_dochandle_t handle, struct zone_mcaptab *tabptr)
67730209230bSgjelinek {
67740209230bSgjelinek 	int err;
67750209230bSgjelinek 
67760209230bSgjelinek 	if ((err = zonecfg_setent(handle)) != Z_OK)
67770209230bSgjelinek 		return (err);
67780209230bSgjelinek 
67790209230bSgjelinek 	err = getmcapent_core(handle, tabptr);
67800209230bSgjelinek 
67810209230bSgjelinek 	(void) zonecfg_endent(handle);
67820209230bSgjelinek 
67830209230bSgjelinek 	return (err);
67840209230bSgjelinek }
67850209230bSgjelinek 
67866cfd72c6Sgjelinek /*
67876cfd72c6Sgjelinek  * Get the full tree of pkg/patch metadata in a set of nested AVL trees.
67886cfd72c6Sgjelinek  * pkgs_avl is an AVL tree of pkgs.  Each pkg element contains a
67896cfd72c6Sgjelinek  * zpe_patches_avl member which holds an AVL tree of patches for that pkg.
67906cfd72c6Sgjelinek  * The patch elements have the same zpe_patches_avl member, each of which can
67916cfd72c6Sgjelinek  * hold an AVL tree of patches that are obsoleted by the patch.
67926cfd72c6Sgjelinek  *
67936cfd72c6Sgjelinek  * The zone xml data contains DTD_ELEM_PACKAGE elements, followed by
67946cfd72c6Sgjelinek  * DTD_ELEM_PATCH elements.  The DTD_ELEM_PATCH patch element applies to the
67956cfd72c6Sgjelinek  * DTD_ELEM_PACKAGE that precedes it.  The DTD_ELEM_PATCH element may have
67966cfd72c6Sgjelinek  * child DTD_ELEM_OBSOLETES nodes associated with it.  The DTD_ELEM_PACKAGE
67976cfd72c6Sgjelinek  * really should have had the DTD_ELEM_PATCH elements as children but it
67986cfd72c6Sgjelinek  * was not defined that way initially so we are stuck with the DTD definition
67996cfd72c6Sgjelinek  * now.  However, we can safely assume the ordering for compatibility.
68006cfd72c6Sgjelinek  */
6801ee519a1fSgjelinek int
68026cfd72c6Sgjelinek zonecfg_getpkgdata(zone_dochandle_t handle, uu_avl_pool_t *pkg_pool,
68036cfd72c6Sgjelinek     uu_avl_t *pkgs_avl)
6804ee519a1fSgjelinek {
6805ee519a1fSgjelinek 	xmlNodePtr cur;
68066cfd72c6Sgjelinek 	int res;
68076cfd72c6Sgjelinek 	zone_pkg_entry_t *pkg;
68086cfd72c6Sgjelinek 	char name[MAXNAMELEN];
68096cfd72c6Sgjelinek 	char version[ZONE_PKG_VERSMAX];
6810ee519a1fSgjelinek 
6811ee519a1fSgjelinek 	if (handle == NULL)
6812ee519a1fSgjelinek 		return (Z_INVAL);
6813ee519a1fSgjelinek 
68146cfd72c6Sgjelinek 	if ((res = zonecfg_setent(handle)) != Z_OK)
68156cfd72c6Sgjelinek 		return (res);
6816ee519a1fSgjelinek 
68176cfd72c6Sgjelinek 	if ((cur = handle->zone_dh_cur) == NULL) {
68186cfd72c6Sgjelinek 		res = Z_NO_ENTRY;
68196cfd72c6Sgjelinek 		goto done;
6820ee519a1fSgjelinek 	}
6821ee519a1fSgjelinek 
68226cfd72c6Sgjelinek 	for (; cur != NULL; cur = cur->next) {
68236cfd72c6Sgjelinek 		if (xmlStrcmp(cur->name, DTD_ELEM_PACKAGE) == 0) {
68246cfd72c6Sgjelinek 			uu_avl_index_t where;
68256cfd72c6Sgjelinek 
68266cfd72c6Sgjelinek 			if ((res = fetchprop(cur, DTD_ATTR_NAME, name,
68276cfd72c6Sgjelinek 			    sizeof (name))) != Z_OK)
68286cfd72c6Sgjelinek 				goto done;
68296cfd72c6Sgjelinek 
68306cfd72c6Sgjelinek 			if ((res = fetchprop(cur, DTD_ATTR_VERSION, version,
68316cfd72c6Sgjelinek 			    sizeof (version))) != Z_OK)
68326cfd72c6Sgjelinek 				goto done;
68336cfd72c6Sgjelinek 
68346cfd72c6Sgjelinek 			if ((pkg = (zone_pkg_entry_t *)
68356cfd72c6Sgjelinek 			    malloc(sizeof (zone_pkg_entry_t))) == NULL) {
68366cfd72c6Sgjelinek 				res = Z_NOMEM;
68376cfd72c6Sgjelinek 				goto done;
6838ee519a1fSgjelinek 			}
6839ee519a1fSgjelinek 
68406cfd72c6Sgjelinek 			if ((pkg->zpe_name = strdup(name)) == NULL) {
68416cfd72c6Sgjelinek 				free(pkg);
68426cfd72c6Sgjelinek 				res = Z_NOMEM;
68436cfd72c6Sgjelinek 				goto done;
6844ee519a1fSgjelinek 			}
6845ee519a1fSgjelinek 
68466cfd72c6Sgjelinek 			if ((pkg->zpe_vers = strdup(version)) == NULL) {
68476cfd72c6Sgjelinek 				free(pkg->zpe_name);
68486cfd72c6Sgjelinek 				free(pkg);
68496cfd72c6Sgjelinek 				res = Z_NOMEM;
68506cfd72c6Sgjelinek 				goto done;
6851ee519a1fSgjelinek 			}
6852ee519a1fSgjelinek 
68536cfd72c6Sgjelinek 			pkg->zpe_patches_avl = NULL;
68546cfd72c6Sgjelinek 
68556cfd72c6Sgjelinek 			uu_avl_node_init(pkg, &pkg->zpe_entry, pkg_pool);
68566cfd72c6Sgjelinek 			if (uu_avl_find(pkgs_avl, pkg, NULL, &where) != NULL) {
68576cfd72c6Sgjelinek 				free(pkg->zpe_name);
68586cfd72c6Sgjelinek 				free(pkg->zpe_vers);
68596cfd72c6Sgjelinek 				free(pkg);
68606cfd72c6Sgjelinek 			} else {
68616cfd72c6Sgjelinek 				uu_avl_insert(pkgs_avl, pkg, where);
6862ee519a1fSgjelinek 			}
6863ee519a1fSgjelinek 
68646cfd72c6Sgjelinek 		} else if (xmlStrcmp(cur->name, DTD_ELEM_PATCH) == 0) {
68656cfd72c6Sgjelinek 			zone_pkg_entry_t *patch;
68666cfd72c6Sgjelinek 			uu_avl_index_t where;
68676cfd72c6Sgjelinek 			char *p;
68686cfd72c6Sgjelinek 			char *dashp = NULL;
68696cfd72c6Sgjelinek 			xmlNodePtr child;
68706cfd72c6Sgjelinek 
68716cfd72c6Sgjelinek 			if ((res = fetchprop(cur, DTD_ATTR_ID, name,
68726cfd72c6Sgjelinek 			    sizeof (name))) != Z_OK)
68736cfd72c6Sgjelinek 				goto done;
68746cfd72c6Sgjelinek 
68756cfd72c6Sgjelinek 			if ((patch = (zone_pkg_entry_t *)
68766cfd72c6Sgjelinek 			    malloc(sizeof (zone_pkg_entry_t))) == NULL) {
68776cfd72c6Sgjelinek 				res = Z_NOMEM;
68786cfd72c6Sgjelinek 				goto done;
6879ee519a1fSgjelinek 			}
6880ee519a1fSgjelinek 
68816cfd72c6Sgjelinek 			if ((p = strchr(name, '-')) != NULL) {
68826cfd72c6Sgjelinek 				dashp = p;
68836cfd72c6Sgjelinek 				*p++ = '\0';
68846cfd72c6Sgjelinek 			} else {
68856cfd72c6Sgjelinek 				p = "";
6886ee519a1fSgjelinek 			}
6887ee519a1fSgjelinek 
68886cfd72c6Sgjelinek 			if ((patch->zpe_name = strdup(name)) == NULL) {
68896cfd72c6Sgjelinek 				free(patch);
68906cfd72c6Sgjelinek 				res = Z_NOMEM;
68916cfd72c6Sgjelinek 				goto done;
6892ee519a1fSgjelinek 			}
6893ee519a1fSgjelinek 
68946cfd72c6Sgjelinek 			if ((patch->zpe_vers = strdup(p)) == NULL) {
68956cfd72c6Sgjelinek 				free(patch->zpe_name);
68966cfd72c6Sgjelinek 				free(patch);
68976cfd72c6Sgjelinek 				res = Z_NOMEM;
68986cfd72c6Sgjelinek 				goto done;
6899ee519a1fSgjelinek 			}
6900ee519a1fSgjelinek 
69016cfd72c6Sgjelinek 			if (dashp != NULL)
69026cfd72c6Sgjelinek 				*dashp = '-';
69036cfd72c6Sgjelinek 
69046cfd72c6Sgjelinek 			patch->zpe_patches_avl = NULL;
69056cfd72c6Sgjelinek 
69066cfd72c6Sgjelinek 			if (pkg->zpe_patches_avl == NULL) {
69076cfd72c6Sgjelinek 				pkg->zpe_patches_avl = uu_avl_create(pkg_pool,
69086cfd72c6Sgjelinek 				    NULL, UU_DEFAULT);
69096cfd72c6Sgjelinek 				if (pkg->zpe_patches_avl == NULL) {
69106cfd72c6Sgjelinek 					free(patch->zpe_name);
69116cfd72c6Sgjelinek 					free(patch->zpe_vers);
69126cfd72c6Sgjelinek 					free(patch);
69136cfd72c6Sgjelinek 					res = Z_NOMEM;
69146cfd72c6Sgjelinek 					goto done;
69156cfd72c6Sgjelinek 				}
69166cfd72c6Sgjelinek 			}
69176cfd72c6Sgjelinek 
69186cfd72c6Sgjelinek 			uu_avl_node_init(patch, &patch->zpe_entry, pkg_pool);
69196cfd72c6Sgjelinek 			if (uu_avl_find(pkg->zpe_patches_avl, patch, NULL,
69206cfd72c6Sgjelinek 			    &where) != NULL) {
69216cfd72c6Sgjelinek 				free(patch->zpe_name);
69226cfd72c6Sgjelinek 				free(patch->zpe_vers);
69236cfd72c6Sgjelinek 				free(patch);
69246cfd72c6Sgjelinek 			} else {
69256cfd72c6Sgjelinek 				uu_avl_insert(pkg->zpe_patches_avl, patch,
69266cfd72c6Sgjelinek 				    where);
69276cfd72c6Sgjelinek 			}
69286cfd72c6Sgjelinek 
69296cfd72c6Sgjelinek 			/* Add any patches this patch obsoletes. */
69306cfd72c6Sgjelinek 			for (child = cur->xmlChildrenNode; child != NULL;
69316cfd72c6Sgjelinek 			    child = child->next) {
69326cfd72c6Sgjelinek 				zone_pkg_entry_t *obs;
69336cfd72c6Sgjelinek 
69346cfd72c6Sgjelinek 				if (xmlStrcmp(child->name, DTD_ELEM_OBSOLETES)
69356cfd72c6Sgjelinek 				    != 0)
69366cfd72c6Sgjelinek 					continue;
69376cfd72c6Sgjelinek 
69386cfd72c6Sgjelinek 				if ((res = fetchprop(child, DTD_ATTR_ID,
69396cfd72c6Sgjelinek 				    name, sizeof (name))) != Z_OK)
69406cfd72c6Sgjelinek 					goto done;
69416cfd72c6Sgjelinek 
69426cfd72c6Sgjelinek 				if ((obs = (zone_pkg_entry_t *)malloc(
69436cfd72c6Sgjelinek 				    sizeof (zone_pkg_entry_t))) == NULL) {
69446cfd72c6Sgjelinek 					res = Z_NOMEM;
69456cfd72c6Sgjelinek 					goto done;
69466cfd72c6Sgjelinek 				}
69476cfd72c6Sgjelinek 
69486cfd72c6Sgjelinek 				if ((obs->zpe_name = strdup(name)) == NULL) {
69496cfd72c6Sgjelinek 					free(obs);
69506cfd72c6Sgjelinek 					res = Z_NOMEM;
69516cfd72c6Sgjelinek 					goto done;
69526cfd72c6Sgjelinek 				}
69536cfd72c6Sgjelinek 				/*
69546cfd72c6Sgjelinek 				 * The version doesn't matter for obsoleted
69556cfd72c6Sgjelinek 				 * patches.
69566cfd72c6Sgjelinek 				 */
69576cfd72c6Sgjelinek 				obs->zpe_vers = NULL;
69586cfd72c6Sgjelinek 				obs->zpe_patches_avl = NULL;
69596cfd72c6Sgjelinek 
69606cfd72c6Sgjelinek 				/*
69616cfd72c6Sgjelinek 				 * If this is the first obsolete patch, add an
69626cfd72c6Sgjelinek 				 * AVL tree to the parent patch element.
69636cfd72c6Sgjelinek 				 */
69646cfd72c6Sgjelinek 				if (patch->zpe_patches_avl == NULL) {
69656cfd72c6Sgjelinek 					patch->zpe_patches_avl =
69666cfd72c6Sgjelinek 					    uu_avl_create(pkg_pool, NULL,
69676cfd72c6Sgjelinek 					    UU_DEFAULT);
69686cfd72c6Sgjelinek 					if (patch->zpe_patches_avl == NULL) {
69696cfd72c6Sgjelinek 						free(obs->zpe_name);
69706cfd72c6Sgjelinek 						free(obs);
69716cfd72c6Sgjelinek 						res = Z_NOMEM;
69726cfd72c6Sgjelinek 						goto done;
69736cfd72c6Sgjelinek 					}
69746cfd72c6Sgjelinek 				}
69756cfd72c6Sgjelinek 
69766cfd72c6Sgjelinek 				/* Insert obsolete patch into the AVL tree. */
69776cfd72c6Sgjelinek 				uu_avl_node_init(obs, &obs->zpe_entry,
69786cfd72c6Sgjelinek 				    pkg_pool);
69796cfd72c6Sgjelinek 				if (uu_avl_find(patch->zpe_patches_avl, obs,
69806cfd72c6Sgjelinek 				    NULL, &where) != NULL) {
69816cfd72c6Sgjelinek 					free(obs->zpe_name);
69826cfd72c6Sgjelinek 					free(obs);
69836cfd72c6Sgjelinek 				} else {
69846cfd72c6Sgjelinek 					uu_avl_insert(patch->zpe_patches_avl,
69856cfd72c6Sgjelinek 					    obs, where);
69866cfd72c6Sgjelinek 				}
69876cfd72c6Sgjelinek 			}
69886cfd72c6Sgjelinek 		}
69896cfd72c6Sgjelinek 	}
69906cfd72c6Sgjelinek 
69916cfd72c6Sgjelinek done:
69926cfd72c6Sgjelinek 	(void) zonecfg_endent(handle);
69936cfd72c6Sgjelinek 	return (res);
6994ee519a1fSgjelinek }
6995ee519a1fSgjelinek 
6996ee519a1fSgjelinek int
6997ee519a1fSgjelinek zonecfg_setdevperment(zone_dochandle_t handle)
6998ee519a1fSgjelinek {
6999ee519a1fSgjelinek 	return (zonecfg_setent(handle));
7000ee519a1fSgjelinek }
7001ee519a1fSgjelinek 
7002ee519a1fSgjelinek int
7003ee519a1fSgjelinek zonecfg_getdevperment(zone_dochandle_t handle, struct zone_devpermtab *tabptr)
7004ee519a1fSgjelinek {
7005ee519a1fSgjelinek 	xmlNodePtr cur;
7006ee519a1fSgjelinek 	int err;
7007ee519a1fSgjelinek 	char buf[128];
7008ee519a1fSgjelinek 
7009ee519a1fSgjelinek 	tabptr->zone_devperm_acl = NULL;
7010ee519a1fSgjelinek 
7011ee519a1fSgjelinek 	if (handle == NULL)
7012ee519a1fSgjelinek 		return (Z_INVAL);
7013ee519a1fSgjelinek 
7014ee519a1fSgjelinek 	if ((cur = handle->zone_dh_cur) == NULL)
7015ee519a1fSgjelinek 		return (Z_NO_ENTRY);
7016ee519a1fSgjelinek 
7017ee519a1fSgjelinek 	for (; cur != NULL; cur = cur->next)
7018ee519a1fSgjelinek 		if (!xmlStrcmp(cur->name, DTD_ELEM_DEV_PERM))
7019ee519a1fSgjelinek 			break;
7020ee519a1fSgjelinek 	if (cur == NULL) {
7021ee519a1fSgjelinek 		handle->zone_dh_cur = handle->zone_dh_top;
7022ee519a1fSgjelinek 		return (Z_NO_ENTRY);
7023ee519a1fSgjelinek 	}
7024ee519a1fSgjelinek 
7025ee519a1fSgjelinek 	if ((err = fetchprop(cur, DTD_ATTR_NAME, tabptr->zone_devperm_name,
7026ee519a1fSgjelinek 	    sizeof (tabptr->zone_devperm_name))) != Z_OK) {
7027ee519a1fSgjelinek 		handle->zone_dh_cur = handle->zone_dh_top;
7028ee519a1fSgjelinek 		return (err);
7029ee519a1fSgjelinek 	}
7030ee519a1fSgjelinek 
7031ee519a1fSgjelinek 	if ((err = fetchprop(cur, DTD_ATTR_UID, buf, sizeof (buf))) != Z_OK) {
7032ee519a1fSgjelinek 		handle->zone_dh_cur = handle->zone_dh_top;
7033ee519a1fSgjelinek 		return (err);
7034ee519a1fSgjelinek 	}
7035ee519a1fSgjelinek 	tabptr->zone_devperm_uid = (uid_t)atol(buf);
7036ee519a1fSgjelinek 
7037ee519a1fSgjelinek 	if ((err = fetchprop(cur, DTD_ATTR_GID, buf, sizeof (buf))) != Z_OK) {
7038ee519a1fSgjelinek 		handle->zone_dh_cur = handle->zone_dh_top;
7039ee519a1fSgjelinek 		return (err);
7040ee519a1fSgjelinek 	}
7041ee519a1fSgjelinek 	tabptr->zone_devperm_gid = (gid_t)atol(buf);
7042ee519a1fSgjelinek 
7043ee519a1fSgjelinek 	if ((err = fetchprop(cur, DTD_ATTR_MODE, buf, sizeof (buf))) != Z_OK) {
7044ee519a1fSgjelinek 		handle->zone_dh_cur = handle->zone_dh_top;
7045ee519a1fSgjelinek 		return (err);
7046ee519a1fSgjelinek 	}
7047ee519a1fSgjelinek 	tabptr->zone_devperm_mode = (mode_t)strtol(buf, (char **)NULL, 8);
7048ee519a1fSgjelinek 
7049ee519a1fSgjelinek 	if ((err = fetch_alloc_prop(cur, DTD_ATTR_ACL,
7050ee519a1fSgjelinek 	    &(tabptr->zone_devperm_acl))) != Z_OK) {
7051ee519a1fSgjelinek 		handle->zone_dh_cur = handle->zone_dh_top;
7052ee519a1fSgjelinek 		return (err);
7053ee519a1fSgjelinek 	}
7054ee519a1fSgjelinek 
7055ee519a1fSgjelinek 	handle->zone_dh_cur = cur->next;
7056ee519a1fSgjelinek 	return (Z_OK);
7057ee519a1fSgjelinek }
7058ee519a1fSgjelinek 
7059ee519a1fSgjelinek int
7060ee519a1fSgjelinek zonecfg_enddevperment(zone_dochandle_t handle)
7061ee519a1fSgjelinek {
7062ee519a1fSgjelinek 	return (zonecfg_endent(handle));
7063ee519a1fSgjelinek }
7064ee519a1fSgjelinek 
7065ff17c8bfSgjelinek /* PRINTFLIKE1 */
7066ff17c8bfSgjelinek static void
7067ff17c8bfSgjelinek zerror(const char *zone_name, const char *fmt, ...)
706839935be5Sgjelinek {
7069ff17c8bfSgjelinek 	va_list alist;
707039935be5Sgjelinek 
7071ff17c8bfSgjelinek 	va_start(alist, fmt);
7072ff17c8bfSgjelinek 	(void) fprintf(stderr, "zone '%s': ", zone_name);
7073ff17c8bfSgjelinek 	(void) vfprintf(stderr, fmt, alist);
7074ff17c8bfSgjelinek 	(void) fprintf(stderr, "\n");
7075ff17c8bfSgjelinek 	va_end(alist);
7076ee519a1fSgjelinek }
7077ee519a1fSgjelinek 
7078ee519a1fSgjelinek static void
7079ff17c8bfSgjelinek zperror(const char *str)
7080ee519a1fSgjelinek {
7081ff17c8bfSgjelinek 	(void) fprintf(stderr, "%s: %s\n", str, strerror(errno));
7082ee519a1fSgjelinek }
7083ee519a1fSgjelinek 
7084ee519a1fSgjelinek /*
7085ff17c8bfSgjelinek  * The following three routines implement a simple locking mechanism to
7086ff17c8bfSgjelinek  * ensure that only one instance of zoneadm at a time is able to manipulate
7087ff17c8bfSgjelinek  * a given zone.  The lock is built on top of an fcntl(2) lock of
7088ff17c8bfSgjelinek  * [<altroot>]/var/run/zones/<zonename>.zoneadm.lock.  If a zoneadm instance
7089ff17c8bfSgjelinek  * can grab that lock, it is allowed to manipulate the zone.
7090ff17c8bfSgjelinek  *
7091ff17c8bfSgjelinek  * Since zoneadm may call external applications which in turn invoke
7092ff17c8bfSgjelinek  * zoneadm again, we introduce the notion of "lock inheritance".  Any
7093ff17c8bfSgjelinek  * instance of zoneadm that has another instance in its ancestry is assumed
7094ff17c8bfSgjelinek  * to be acting on behalf of the original zoneadm, and is thus allowed to
7095ff17c8bfSgjelinek  * manipulate its zone.
7096ff17c8bfSgjelinek  *
7097ff17c8bfSgjelinek  * This inheritance is implemented via the _ZONEADM_LOCK_HELD environment
7098ff17c8bfSgjelinek  * variable.  When zoneadm is granted a lock on its zone, this environment
7099ff17c8bfSgjelinek  * variable is set to 1.  When it releases the lock, the variable is set to
7100ff17c8bfSgjelinek  * 0.  Since a child process inherits its parent's environment, checking
7101ff17c8bfSgjelinek  * the state of this variable indicates whether or not any ancestor owns
7102ff17c8bfSgjelinek  * the lock.
710339935be5Sgjelinek  */
7104ff17c8bfSgjelinek void
7105ff17c8bfSgjelinek zonecfg_init_lock_file(const char *zone_name, char **lock_env)
710639935be5Sgjelinek {
7107ff17c8bfSgjelinek 	*lock_env = getenv(LOCK_ENV_VAR);
7108ff17c8bfSgjelinek 	if (*lock_env == NULL) {
7109ff17c8bfSgjelinek 		if (putenv(zoneadm_lock_not_held) != 0) {
7110ff17c8bfSgjelinek 			zerror(zone_name, gettext("could not set env: %s"),
7111ff17c8bfSgjelinek 			    strerror(errno));
7112ff17c8bfSgjelinek 			exit(1);
7113ff17c8bfSgjelinek 		}
711439935be5Sgjelinek 	} else {
7115ff17c8bfSgjelinek 		if (atoi(*lock_env) == 1)
7116ff17c8bfSgjelinek 			zone_lock_cnt = 1;
711739935be5Sgjelinek 	}
711839935be5Sgjelinek }
711939935be5Sgjelinek 
7120ff17c8bfSgjelinek void
7121ff17c8bfSgjelinek zonecfg_release_lock_file(const char *zone_name, int lockfd)
7122ff17c8bfSgjelinek {
7123ff17c8bfSgjelinek 	/*
7124ff17c8bfSgjelinek 	 * If we are cleaning up from a failed attempt to lock the zone for
7125ff17c8bfSgjelinek 	 * the first time, we might have a zone_lock_cnt of 0.  In that
7126ff17c8bfSgjelinek 	 * error case, we don't want to do anything but close the lock
7127ff17c8bfSgjelinek 	 * file.
7128ff17c8bfSgjelinek 	 */
7129ff17c8bfSgjelinek 	assert(zone_lock_cnt >= 0);
7130ff17c8bfSgjelinek 	if (zone_lock_cnt > 0) {
7131ff17c8bfSgjelinek 		assert(getenv(LOCK_ENV_VAR) != NULL);
7132ff17c8bfSgjelinek 		assert(atoi(getenv(LOCK_ENV_VAR)) == 1);
7133ff17c8bfSgjelinek 		if (--zone_lock_cnt > 0) {
7134ff17c8bfSgjelinek 			assert(lockfd == -1);
7135ff17c8bfSgjelinek 			return;
713639935be5Sgjelinek 		}
7137ff17c8bfSgjelinek 		if (putenv(zoneadm_lock_not_held) != 0) {
7138ff17c8bfSgjelinek 			zerror(zone_name, gettext("could not set env: %s"),
7139ff17c8bfSgjelinek 			    strerror(errno));
7140ff17c8bfSgjelinek 			exit(1);
7141ff17c8bfSgjelinek 		}
7142ff17c8bfSgjelinek 	}
7143ff17c8bfSgjelinek 	assert(lockfd >= 0);
7144ff17c8bfSgjelinek 	(void) close(lockfd);
714539935be5Sgjelinek }
714639935be5Sgjelinek 
7147ff17c8bfSgjelinek int
7148ff17c8bfSgjelinek zonecfg_grab_lock_file(const char *zone_name, int *lockfd)
7149ff17c8bfSgjelinek {
7150ff17c8bfSgjelinek 	char pathbuf[PATH_MAX];
7151ff17c8bfSgjelinek 	struct flock flock;
7152ff17c8bfSgjelinek 
7153ff17c8bfSgjelinek 	/*
7154ff17c8bfSgjelinek 	 * If we already have the lock, we can skip this expensive song
7155ff17c8bfSgjelinek 	 * and dance.
7156ff17c8bfSgjelinek 	 */
7157ff17c8bfSgjelinek 	assert(zone_lock_cnt >= 0);
7158ff17c8bfSgjelinek 	assert(getenv(LOCK_ENV_VAR) != NULL);
7159ff17c8bfSgjelinek 	if (zone_lock_cnt > 0) {
7160ff17c8bfSgjelinek 		assert(atoi(getenv(LOCK_ENV_VAR)) == 1);
7161ff17c8bfSgjelinek 		zone_lock_cnt++;
7162ff17c8bfSgjelinek 		*lockfd = -1;
716339935be5Sgjelinek 		return (Z_OK);
716439935be5Sgjelinek 	}
7165ff17c8bfSgjelinek 	assert(getenv(LOCK_ENV_VAR) != NULL);
7166ff17c8bfSgjelinek 	assert(atoi(getenv(LOCK_ENV_VAR)) == 0);
716739935be5Sgjelinek 
7168ff17c8bfSgjelinek 	if (snprintf(pathbuf, sizeof (pathbuf), "%s%s", zonecfg_get_root(),
7169ff17c8bfSgjelinek 	    ZONES_TMPDIR) >= sizeof (pathbuf)) {
7170ff17c8bfSgjelinek 		zerror(zone_name, gettext("alternate root path is too long"));
717139935be5Sgjelinek 		return (-1);
717239935be5Sgjelinek 	}
7173ff17c8bfSgjelinek 	if (mkdir(pathbuf, S_IRWXU) < 0 && errno != EEXIST) {
7174ff17c8bfSgjelinek 		zerror(zone_name, gettext("could not mkdir %s: %s"), pathbuf,
7175ff17c8bfSgjelinek 		    strerror(errno));
7176ff17c8bfSgjelinek 		return (-1);
7177ff17c8bfSgjelinek 	}
7178ff17c8bfSgjelinek 	(void) chmod(pathbuf, S_IRWXU);
717939935be5Sgjelinek 
718039935be5Sgjelinek 	/*
7181ff17c8bfSgjelinek 	 * One of these lock files is created for each zone (when needed).
7182ff17c8bfSgjelinek 	 * The lock files are not cleaned up (except on system reboot),
7183ff17c8bfSgjelinek 	 * but since there is only one per zone, there is no resource
7184ff17c8bfSgjelinek 	 * starvation issue.
718539935be5Sgjelinek 	 */
7186ff17c8bfSgjelinek 	if (snprintf(pathbuf, sizeof (pathbuf), "%s%s/%s.zoneadm.lock",
7187ff17c8bfSgjelinek 	    zonecfg_get_root(), ZONES_TMPDIR, zone_name) >= sizeof (pathbuf)) {
7188ff17c8bfSgjelinek 		zerror(zone_name, gettext("alternate root path is too long"));
7189ff17c8bfSgjelinek 		return (-1);
7190ff17c8bfSgjelinek 	}
7191ff17c8bfSgjelinek 	if ((*lockfd = open(pathbuf, O_RDWR|O_CREAT, S_IRUSR|S_IWUSR)) < 0) {
7192ff17c8bfSgjelinek 		zerror(zone_name, gettext("could not open %s: %s"), pathbuf,
7193ff17c8bfSgjelinek 		    strerror(errno));
7194ff17c8bfSgjelinek 		return (-1);
7195ff17c8bfSgjelinek 	}
7196ff17c8bfSgjelinek 	/*
7197ff17c8bfSgjelinek 	 * Lock the file to synchronize with other zoneadmds
7198ff17c8bfSgjelinek 	 */
7199ff17c8bfSgjelinek 	flock.l_type = F_WRLCK;
7200ff17c8bfSgjelinek 	flock.l_whence = SEEK_SET;
7201ff17c8bfSgjelinek 	flock.l_start = (off_t)0;
7202ff17c8bfSgjelinek 	flock.l_len = (off_t)0;
7203ff17c8bfSgjelinek 	if ((fcntl(*lockfd, F_SETLKW, &flock) < 0) ||
7204ff17c8bfSgjelinek 	    (putenv(zoneadm_lock_held) != 0)) {
7205ff17c8bfSgjelinek 		zerror(zone_name, gettext("unable to lock %s: %s"), pathbuf,
7206ff17c8bfSgjelinek 		    strerror(errno));
7207ff17c8bfSgjelinek 		zonecfg_release_lock_file(zone_name, *lockfd);
7208ff17c8bfSgjelinek 		return (-1);
7209ff17c8bfSgjelinek 	}
7210ff17c8bfSgjelinek 	zone_lock_cnt = 1;
7211ff17c8bfSgjelinek 	return (Z_OK);
7212ff17c8bfSgjelinek }
7213ff17c8bfSgjelinek 
7214edfa49ffS boolean_t
7215edfa49ffS zonecfg_lock_file_held(int *lockfd)
7216edfa49ffS {
7217edfa49ffS 	if (*lockfd >= 0 || zone_lock_cnt > 0)
7218edfa49ffS 		return (B_TRUE);
7219edfa49ffS 	return (B_FALSE);
7220edfa49ffS }
7221edfa49ffS 
7222ff17c8bfSgjelinek static boolean_t
7223ff17c8bfSgjelinek get_doorname(const char *zone_name, char *buffer)
722439935be5Sgjelinek {
7225ff17c8bfSgjelinek 	return (snprintf(buffer, PATH_MAX, "%s" ZONE_DOOR_PATH,
7226ff17c8bfSgjelinek 	    zonecfg_get_root(), zone_name) < PATH_MAX);
7227e66285fdSgjelinek }
7228ee519a1fSgjelinek 
7229ee519a1fSgjelinek /*
7230ff17c8bfSgjelinek  * system daemons are not audited.  For the global zone, this occurs
7231ff17c8bfSgjelinek  * "naturally" since init is started with the default audit
7232ff17c8bfSgjelinek  * characteristics.  Since zoneadmd is a system daemon and it starts
7233ff17c8bfSgjelinek  * init for a zone, it is necessary to clear out the audit
7234ff17c8bfSgjelinek  * characteristics inherited from whomever started zoneadmd.  This is
7235ff17c8bfSgjelinek  * indicated by the audit id, which is set from the ruid parameter of
7236ff17c8bfSgjelinek  * adt_set_user(), below.
7237ee519a1fSgjelinek  */
7238ff17c8bfSgjelinek 
7239ff17c8bfSgjelinek static void
7240ff17c8bfSgjelinek prepare_audit_context(const char *zone_name)
7241ff17c8bfSgjelinek {
7242ff17c8bfSgjelinek 	adt_session_data_t	*ah;
7243ff17c8bfSgjelinek 	char			*failure = gettext("audit failure: %s");
7244ff17c8bfSgjelinek 
7245ff17c8bfSgjelinek 	if (adt_start_session(&ah, NULL, 0)) {
7246ff17c8bfSgjelinek 		zerror(zone_name, failure, strerror(errno));
7247ff17c8bfSgjelinek 		return;
7248ff17c8bfSgjelinek 	}
7249ff17c8bfSgjelinek 	if (adt_set_user(ah, ADT_NO_AUDIT, ADT_NO_AUDIT,
7250ff17c8bfSgjelinek 	    ADT_NO_AUDIT, ADT_NO_AUDIT, NULL, ADT_NEW)) {
7251ff17c8bfSgjelinek 		zerror(zone_name, failure, strerror(errno));
7252ff17c8bfSgjelinek 		(void) adt_end_session(ah);
7253ff17c8bfSgjelinek 		return;
7254ff17c8bfSgjelinek 	}
7255ff17c8bfSgjelinek 	if (adt_set_proc(ah))
7256ff17c8bfSgjelinek 		zerror(zone_name, failure, strerror(errno));
7257ff17c8bfSgjelinek 
7258ff17c8bfSgjelinek 	(void) adt_end_session(ah);
7259ff17c8bfSgjelinek }
7260ff17c8bfSgjelinek 
7261ff17c8bfSgjelinek static int
7262ff17c8bfSgjelinek start_zoneadmd(const char *zone_name, boolean_t lock)
7263ff17c8bfSgjelinek {
7264ff17c8bfSgjelinek 	char doorpath[PATH_MAX];
7265ff17c8bfSgjelinek 	pid_t child_pid;
7266ff17c8bfSgjelinek 	int error = -1;
7267ff17c8bfSgjelinek 	int doorfd, lockfd;
7268ff17c8bfSgjelinek 	struct door_info info;
7269ff17c8bfSgjelinek 
7270ff17c8bfSgjelinek 	if (!get_doorname(zone_name, doorpath))
7271ff17c8bfSgjelinek 		return (-1);
7272ff17c8bfSgjelinek 
7273ff17c8bfSgjelinek 	if (lock)
7274ff17c8bfSgjelinek 		if (zonecfg_grab_lock_file(zone_name, &lockfd) != Z_OK)
7275ff17c8bfSgjelinek 			return (-1);
7276ff17c8bfSgjelinek 
7277ff17c8bfSgjelinek 	/*
7278ff17c8bfSgjelinek 	 * Now that we have the lock, re-confirm that the daemon is
7279ff17c8bfSgjelinek 	 * *not* up and working fine.  If it is still down, we have a green
7280ff17c8bfSgjelinek 	 * light to start it.
7281ff17c8bfSgjelinek 	 */
7282ff17c8bfSgjelinek 	if ((doorfd = open(doorpath, O_RDONLY)) < 0) {
7283ff17c8bfSgjelinek 		if (errno != ENOENT) {
7284ff17c8bfSgjelinek 			zperror(doorpath);
7285ff17c8bfSgjelinek 			goto out;
7286ff17c8bfSgjelinek 		}
7287ff17c8bfSgjelinek 	} else {
7288ff17c8bfSgjelinek 		if (door_info(doorfd, &info) == 0 &&
7289ff17c8bfSgjelinek 		    ((info.di_attributes & DOOR_REVOKED) == 0)) {
7290ff17c8bfSgjelinek 			error = Z_OK;
7291ff17c8bfSgjelinek 			(void) close(doorfd);
7292ff17c8bfSgjelinek 			goto out;
7293ff17c8bfSgjelinek 		}
7294ff17c8bfSgjelinek 		(void) close(doorfd);
7295ff17c8bfSgjelinek 	}
7296ff17c8bfSgjelinek 
7297ff17c8bfSgjelinek 	if ((child_pid = fork()) == -1) {
7298ff17c8bfSgjelinek 		zperror(gettext("could not fork"));
7299ff17c8bfSgjelinek 		goto out;
7300ff17c8bfSgjelinek 	}
7301ff17c8bfSgjelinek 
7302ff17c8bfSgjelinek 	if (child_pid == 0) {
7303ff17c8bfSgjelinek 		const char *argv[6], **ap;
7304ff17c8bfSgjelinek 
7305ff17c8bfSgjelinek 		/* child process */
7306ff17c8bfSgjelinek 		prepare_audit_context(zone_name);
7307ff17c8bfSgjelinek 
7308ff17c8bfSgjelinek 		ap = argv;
7309ff17c8bfSgjelinek 		*ap++ = "zoneadmd";
7310ff17c8bfSgjelinek 		*ap++ = "-z";
7311ff17c8bfSgjelinek 		*ap++ = zone_name;
7312ff17c8bfSgjelinek 		if (zonecfg_in_alt_root()) {
7313ff17c8bfSgjelinek 			*ap++ = "-R";
7314ff17c8bfSgjelinek 			*ap++ = zonecfg_get_root();
7315ff17c8bfSgjelinek 		}
7316ff17c8bfSgjelinek 		*ap = NULL;
7317ff17c8bfSgjelinek 
7318ff17c8bfSgjelinek 		(void) execv("/usr/lib/zones/zoneadmd", (char * const *)argv);
7319ff17c8bfSgjelinek 		/*
7320ff17c8bfSgjelinek 		 * TRANSLATION_NOTE
7321ff17c8bfSgjelinek 		 * zoneadmd is a literal that should not be translated.
7322ff17c8bfSgjelinek 		 */
7323ff17c8bfSgjelinek 		zperror(gettext("could not exec zoneadmd"));
7324ff17c8bfSgjelinek 		_exit(1);
7325ff17c8bfSgjelinek 	} else {
7326ff17c8bfSgjelinek 		/* parent process */
7327ff17c8bfSgjelinek 		pid_t retval;
7328ff17c8bfSgjelinek 		int pstatus = 0;
7329ee519a1fSgjelinek 
7330ee519a1fSgjelinek 		do {
7331ff17c8bfSgjelinek 			retval = waitpid(child_pid, &pstatus, 0);
7332ff17c8bfSgjelinek 		} while (retval != child_pid);
7333ff17c8bfSgjelinek 		if (WIFSIGNALED(pstatus) || (WIFEXITED(pstatus) &&
7334ff17c8bfSgjelinek 		    WEXITSTATUS(pstatus) != 0)) {
7335ff17c8bfSgjelinek 			zerror(zone_name, gettext("could not start %s"),
7336ff17c8bfSgjelinek 			    "zoneadmd");
7337ff17c8bfSgjelinek 			goto out;
73386cfd72c6Sgjelinek 		}
73396cfd72c6Sgjelinek 	}
7340ff17c8bfSgjelinek 	error = Z_OK;
7341ff17c8bfSgjelinek out:
7342ff17c8bfSgjelinek 	if (lock)
7343ff17c8bfSgjelinek 		zonecfg_release_lock_file(zone_name, lockfd);
7344ff17c8bfSgjelinek 	return (error);
7345ee519a1fSgjelinek }
7346ee519a1fSgjelinek 
7347ee519a1fSgjelinek int
7348ff17c8bfSgjelinek zonecfg_ping_zoneadmd(const char *zone_name)
7349ee519a1fSgjelinek {
7350ff17c8bfSgjelinek 	char doorpath[PATH_MAX];
7351ff17c8bfSgjelinek 	int doorfd;
7352ff17c8bfSgjelinek 	struct door_info info;
7353ee519a1fSgjelinek 
7354ff17c8bfSgjelinek 	if (!get_doorname(zone_name, doorpath))
7355ff17c8bfSgjelinek 		return (-1);
7356ee519a1fSgjelinek 
7357ff17c8bfSgjelinek 	if ((doorfd = open(doorpath, O_RDONLY)) < 0) {
7358ff17c8bfSgjelinek 		return (-1);
7359ff17c8bfSgjelinek 	}
7360ff17c8bfSgjelinek 	if (door_info(doorfd, &info) == 0 &&
7361ff17c8bfSgjelinek 	    ((info.di_attributes & DOOR_REVOKED) == 0)) {
7362ff17c8bfSgjelinek 		(void) close(doorfd);
7363ff17c8bfSgjelinek 		return (Z_OK);
7364ff17c8bfSgjelinek 	}
7365ff17c8bfSgjelinek 	(void) close(doorfd);
7366ff17c8bfSgjelinek 	return (-1);
7367ff17c8bfSgjelinek }
7368ee519a1fSgjelinek 
7369ff17c8bfSgjelinek int
7370ff17c8bfSgjelinek zonecfg_call_zoneadmd(const char *zone_name, zone_cmd_arg_t *arg, char *locale,
7371ff17c8bfSgjelinek     boolean_t lock)
7372ff17c8bfSgjelinek {
7373ff17c8bfSgjelinek 	char doorpath[PATH_MAX];
7374ff17c8bfSgjelinek 	int doorfd, result;
7375ff17c8bfSgjelinek 	door_arg_t darg;
7376ff17c8bfSgjelinek 
7377ff17c8bfSgjelinek 	zoneid_t zoneid;
7378ff17c8bfSgjelinek 	uint64_t uniqid = 0;
7379ff17c8bfSgjelinek 
7380ff17c8bfSgjelinek 	zone_cmd_rval_t *rvalp;
7381ff17c8bfSgjelinek 	size_t rlen;
7382ff17c8bfSgjelinek 	char *cp, *errbuf;
7383ff17c8bfSgjelinek 
7384ff17c8bfSgjelinek 	rlen = getpagesize();
7385ff17c8bfSgjelinek 	if ((rvalp = malloc(rlen)) == NULL) {
7386ff17c8bfSgjelinek 		zerror(zone_name, gettext("failed to allocate %lu bytes: %s"),
7387ff17c8bfSgjelinek 		    rlen, strerror(errno));
7388ff17c8bfSgjelinek 		return (-1);
7389ff17c8bfSgjelinek 	}
7390ff17c8bfSgjelinek 
7391ff17c8bfSgjelinek 	if ((zoneid = getzoneidbyname(zone_name)) != ZONE_ID_UNDEFINED) {
7392ff17c8bfSgjelinek 		(void) zone_getattr(zoneid, ZONE_ATTR_UNIQID, &uniqid,
7393ff17c8bfSgjelinek 		    sizeof (uniqid));
7394ff17c8bfSgjelinek 	}
7395ff17c8bfSgjelinek 	arg->uniqid = uniqid;
7396ff17c8bfSgjelinek 	(void) strlcpy(arg->locale, locale, sizeof (arg->locale));
7397ff17c8bfSgjelinek 	if (!get_doorname(zone_name, doorpath)) {
7398ff17c8bfSgjelinek 		zerror(zone_name, gettext("alternate root path is too long"));
7399ff17c8bfSgjelinek 		free(rvalp);
7400ff17c8bfSgjelinek 		return (-1);
7401ff17c8bfSgjelinek 	}
7402ff17c8bfSgjelinek 
7403ff17c8bfSgjelinek 	/*
7404ff17c8bfSgjelinek 	 * Loop trying to start zoneadmd; if something goes seriously
7405ff17c8bfSgjelinek 	 * wrong we break out and fail.
7406ff17c8bfSgjelinek 	 */
7407ff17c8bfSgjelinek 	for (;;) {
7408ff17c8bfSgjelinek 		if (start_zoneadmd(zone_name, lock) != Z_OK)
7409ff17c8bfSgjelinek 			break;
7410ff17c8bfSgjelinek 
7411ff17c8bfSgjelinek 		if ((doorfd = open(doorpath, O_RDONLY)) < 0) {
7412ff17c8bfSgjelinek 			zperror(gettext("failed to open zone door"));
7413ff17c8bfSgjelinek 			break;
7414ff17c8bfSgjelinek 		}
7415ff17c8bfSgjelinek 
7416ff17c8bfSgjelinek 		darg.data_ptr = (char *)arg;
7417ff17c8bfSgjelinek 		darg.data_size = sizeof (*arg);
7418ff17c8bfSgjelinek 		darg.desc_ptr = NULL;
7419ff17c8bfSgjelinek 		darg.desc_num = 0;
7420ff17c8bfSgjelinek 		darg.rbuf = (char *)rvalp;
7421ff17c8bfSgjelinek 		darg.rsize = rlen;
7422ff17c8bfSgjelinek 		if (door_call(doorfd, &darg) != 0) {
7423ff17c8bfSgjelinek 			(void) close(doorfd);
7424ff17c8bfSgjelinek 			/*
7425ff17c8bfSgjelinek 			 * We'll get EBADF if the door has been revoked.
7426ff17c8bfSgjelinek 			 */
7427ff17c8bfSgjelinek 			if (errno != EBADF) {
7428ff17c8bfSgjelinek 				zperror(gettext("door_call failed"));
7429ff17c8bfSgjelinek 				break;
7430ff17c8bfSgjelinek 			}
7431ff17c8bfSgjelinek 			continue;	/* take another lap */
7432ff17c8bfSgjelinek 		}
7433ff17c8bfSgjelinek 		(void) close(doorfd);
7434ff17c8bfSgjelinek 
7435ff17c8bfSgjelinek 		if (darg.data_size == 0) {
7436ff17c8bfSgjelinek 			/* Door server is going away; kick it again. */
7437ff17c8bfSgjelinek 			continue;
7438ff17c8bfSgjelinek 		}
7439ff17c8bfSgjelinek 
7440ff17c8bfSgjelinek 		errbuf = rvalp->errbuf;
7441ff17c8bfSgjelinek 		while (*errbuf != '\0') {
7442ff17c8bfSgjelinek 			/*
7443ff17c8bfSgjelinek 			 * Remove any newlines since zerror()
7444ff17c8bfSgjelinek 			 * will append one automatically.
7445ff17c8bfSgjelinek 			 */
7446ff17c8bfSgjelinek 			cp = strchr(errbuf, '\n');
7447ff17c8bfSgjelinek 			if (cp != NULL)
7448ff17c8bfSgjelinek 				*cp = '\0';
7449ff17c8bfSgjelinek 			zerror(zone_name, "%s", errbuf);
7450ff17c8bfSgjelinek 			if (cp == NULL)
7451ff17c8bfSgjelinek 				break;
7452ff17c8bfSgjelinek 			errbuf = cp + 1;
7453ff17c8bfSgjelinek 		}
7454ff17c8bfSgjelinek 		result = rvalp->rval == 0 ? 0 : -1;
7455ff17c8bfSgjelinek 		free(rvalp);
7456ff17c8bfSgjelinek 		return (result);
7457ff17c8bfSgjelinek 	}
7458ff17c8bfSgjelinek 
7459ff17c8bfSgjelinek 	free(rvalp);
7460ff17c8bfSgjelinek 	return (-1);
7461ee519a1fSgjelinek }
7462