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 /*
239d4be64eSdstaff  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
287c478bd9Sstevel@tonic-gate 
29cf8f45c7Sdstaff #include <libsysevent.h>
30cf8f45c7Sdstaff #include <pthread.h>
31cf8f45c7Sdstaff #include <stdlib.h>
327c478bd9Sstevel@tonic-gate #include <errno.h>
337c478bd9Sstevel@tonic-gate #include <fnmatch.h>
347c478bd9Sstevel@tonic-gate #include <strings.h>
357c478bd9Sstevel@tonic-gate #include <unistd.h>
367c478bd9Sstevel@tonic-gate #include <assert.h>
377c478bd9Sstevel@tonic-gate #include <libgen.h>
387c478bd9Sstevel@tonic-gate #include <libintl.h>
397c478bd9Sstevel@tonic-gate #include <alloca.h>
407c478bd9Sstevel@tonic-gate #include <ctype.h>
419acbbeafSnn #include <sys/acl.h>
429acbbeafSnn #include <sys/stat.h>
439acbbeafSnn #include <sys/brand.h>
447c478bd9Sstevel@tonic-gate #include <sys/mntio.h>
457c478bd9Sstevel@tonic-gate #include <sys/mnttab.h>
46cf8f45c7Sdstaff #include <sys/nvpair.h>
479acbbeafSnn #include <sys/types.h>
48ee519a1fSgjelinek #include <ftw.h>
49*0209230bSgjelinek #include <pool.h>
50*0209230bSgjelinek #include <libscf.h>
51*0209230bSgjelinek #include <libproc.h>
52*0209230bSgjelinek #include <sys/priocntl.h>
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate #include <arpa/inet.h>
557c478bd9Sstevel@tonic-gate #include <netdb.h>
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate #include <libxml/xmlmemory.h>
587c478bd9Sstevel@tonic-gate #include <libxml/parser.h>
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate #include <libdevinfo.h>
61108322fbScarlsonj #include <uuid/uuid.h>
62ee519a1fSgjelinek #include <dirent.h>
639acbbeafSnn #include <libbrand.h>
64ee519a1fSgjelinek 
657c478bd9Sstevel@tonic-gate #include <libzonecfg.h>
667c478bd9Sstevel@tonic-gate #include "zonecfg_impl.h"
677c478bd9Sstevel@tonic-gate 
68cf8f45c7Sdstaff 
697c478bd9Sstevel@tonic-gate #define	_PATH_TMPFILE	"/zonecfg.XXXXXX"
70cf8f45c7Sdstaff #define	ZONE_CB_RETRY_COUNT		10
71cf8f45c7Sdstaff #define	ZONE_EVENT_PING_SUBCLASS	"ping"
72cf8f45c7Sdstaff #define	ZONE_EVENT_PING_PUBLISHER	"solaris"
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate /* Hard-code the DTD element/attribute/entity names just once, here. */
757c478bd9Sstevel@tonic-gate #define	DTD_ELEM_ATTR		(const xmlChar *) "attr"
767c478bd9Sstevel@tonic-gate #define	DTD_ELEM_COMMENT	(const xmlChar *) "comment"
777c478bd9Sstevel@tonic-gate #define	DTD_ELEM_DEVICE		(const xmlChar *) "device"
787c478bd9Sstevel@tonic-gate #define	DTD_ELEM_FS		(const xmlChar *) "filesystem"
797c478bd9Sstevel@tonic-gate #define	DTD_ELEM_FSOPTION	(const xmlChar *) "fsoption"
807c478bd9Sstevel@tonic-gate #define	DTD_ELEM_IPD		(const xmlChar *) "inherited-pkg-dir"
817c478bd9Sstevel@tonic-gate #define	DTD_ELEM_NET		(const xmlChar *) "network"
827c478bd9Sstevel@tonic-gate #define	DTD_ELEM_RCTL		(const xmlChar *) "rctl"
837c478bd9Sstevel@tonic-gate #define	DTD_ELEM_RCTLVALUE	(const xmlChar *) "rctl-value"
847c478bd9Sstevel@tonic-gate #define	DTD_ELEM_ZONE		(const xmlChar *) "zone"
85fa9e4066Sahrens #define	DTD_ELEM_DATASET	(const xmlChar *) "dataset"
86*0209230bSgjelinek #define	DTD_ELEM_TMPPOOL	(const xmlChar *) "tmp_pool"
87*0209230bSgjelinek #define	DTD_ELEM_PSET		(const xmlChar *) "pset"
88*0209230bSgjelinek #define	DTD_ELEM_MCAP		(const xmlChar *) "mcap"
89ee519a1fSgjelinek #define	DTD_ELEM_PACKAGE	(const xmlChar *) "package"
90ee519a1fSgjelinek #define	DTD_ELEM_PATCH		(const xmlChar *) "patch"
91ee519a1fSgjelinek #define	DTD_ELEM_OBSOLETES	(const xmlChar *) "obsoletes"
92ee519a1fSgjelinek #define	DTD_ELEM_INCOMPATIBLE	(const xmlChar *) "incompatible"
93ee519a1fSgjelinek #define	DTD_ELEM_DEV_PERM	(const xmlChar *) "dev-perm"
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate #define	DTD_ATTR_ACTION		(const xmlChar *) "action"
967c478bd9Sstevel@tonic-gate #define	DTD_ATTR_ADDRESS	(const xmlChar *) "address"
977c478bd9Sstevel@tonic-gate #define	DTD_ATTR_AUTOBOOT	(const xmlChar *) "autoboot"
987c478bd9Sstevel@tonic-gate #define	DTD_ATTR_DIR		(const xmlChar *) "directory"
997c478bd9Sstevel@tonic-gate #define	DTD_ATTR_LIMIT		(const xmlChar *) "limit"
100ffbafc53Scomay #define	DTD_ATTR_LIMITPRIV	(const xmlChar *) "limitpriv"
1013f2f09c1Sdp #define	DTD_ATTR_BOOTARGS	(const xmlChar *) "bootargs"
102*0209230bSgjelinek #define	DTD_ATTR_SCHED		(const xmlChar *) "scheduling-class"
1037c478bd9Sstevel@tonic-gate #define	DTD_ATTR_MATCH		(const xmlChar *) "match"
1047c478bd9Sstevel@tonic-gate #define	DTD_ATTR_NAME		(const xmlChar *) "name"
1057c478bd9Sstevel@tonic-gate #define	DTD_ATTR_PHYSICAL	(const xmlChar *) "physical"
1067c478bd9Sstevel@tonic-gate #define	DTD_ATTR_POOL		(const xmlChar *) "pool"
1077c478bd9Sstevel@tonic-gate #define	DTD_ATTR_PRIV		(const xmlChar *) "priv"
1087c478bd9Sstevel@tonic-gate #define	DTD_ATTR_RAW		(const xmlChar *) "raw"
1097c478bd9Sstevel@tonic-gate #define	DTD_ATTR_SPECIAL	(const xmlChar *) "special"
1107c478bd9Sstevel@tonic-gate #define	DTD_ATTR_TYPE		(const xmlChar *) "type"
1117c478bd9Sstevel@tonic-gate #define	DTD_ATTR_VALUE		(const xmlChar *) "value"
1127c478bd9Sstevel@tonic-gate #define	DTD_ATTR_ZONEPATH	(const xmlChar *) "zonepath"
113*0209230bSgjelinek #define	DTD_ATTR_NCPU_MIN	(const xmlChar *) "ncpu_min"
114*0209230bSgjelinek #define	DTD_ATTR_NCPU_MAX	(const xmlChar *) "ncpu_max"
115*0209230bSgjelinek #define	DTD_ATTR_IMPORTANCE	(const xmlChar *) "importance"
116*0209230bSgjelinek #define	DTD_ATTR_PHYSCAP	(const xmlChar *) "physcap"
117ee519a1fSgjelinek #define	DTD_ATTR_VERSION	(const xmlChar *) "version"
118ee519a1fSgjelinek #define	DTD_ATTR_ID		(const xmlChar *) "id"
119ee519a1fSgjelinek #define	DTD_ATTR_UID		(const xmlChar *) "uid"
120ee519a1fSgjelinek #define	DTD_ATTR_GID		(const xmlChar *) "gid"
121ee519a1fSgjelinek #define	DTD_ATTR_MODE		(const xmlChar *) "mode"
122ee519a1fSgjelinek #define	DTD_ATTR_ACL		(const xmlChar *) "acl"
1239acbbeafSnn #define	DTD_ATTR_BRAND		(const xmlChar *) "brand"
1247c478bd9Sstevel@tonic-gate 
1257c478bd9Sstevel@tonic-gate #define	DTD_ENTITY_BOOLEAN	"boolean"
1267c478bd9Sstevel@tonic-gate #define	DTD_ENTITY_DEVPATH	"devpath"
1277c478bd9Sstevel@tonic-gate #define	DTD_ENTITY_DRIVER	"driver"
1287c478bd9Sstevel@tonic-gate #define	DTD_ENTITY_DRVMIN	"drv_min"
1297c478bd9Sstevel@tonic-gate #define	DTD_ENTITY_FALSE	"false"
1307c478bd9Sstevel@tonic-gate #define	DTD_ENTITY_INT		"int"
1317c478bd9Sstevel@tonic-gate #define	DTD_ENTITY_STRING	"string"
1327c478bd9Sstevel@tonic-gate #define	DTD_ENTITY_TRUE		"true"
1337c478bd9Sstevel@tonic-gate #define	DTD_ENTITY_UINT		"uint"
1347c478bd9Sstevel@tonic-gate 
135a1be23daSdp #define	DTD_ENTITY_BOOL_LEN	6	/* "false" */
136a1be23daSdp 
137ee519a1fSgjelinek #define	DETACHED	"SUNWdetached.xml"
138ee519a1fSgjelinek #define	ATTACH_FORCED	"SUNWattached.xml"
139ee519a1fSgjelinek #define	PKG_PATH	"/var/sadm/pkg"
140ee519a1fSgjelinek #define	CONTENTS_FILE	"/var/sadm/install/contents"
14145916cd2Sjpk #define	SUNW_PKG_ALL_ZONES	"SUNW_PKG_ALLZONES=true\n"
14245916cd2Sjpk #define	SUNW_PKG_THIS_ZONE	"SUNW_PKG_THISZONE=true\n"
143ee519a1fSgjelinek #define	VERSION		"VERSION="
144ee519a1fSgjelinek #define	PATCHLIST	"PATCHLIST="
145ee519a1fSgjelinek #define	PATCHINFO	"PATCH_INFO_"
146ee519a1fSgjelinek #define	PKGINFO_RD_LEN	128
147ee519a1fSgjelinek 
148*0209230bSgjelinek #define	TMP_POOL_NAME	"SUNWtmp_%s"
149*0209230bSgjelinek #define	MAX_TMP_POOL_NAME	(ZONENAME_MAX + 9)
150*0209230bSgjelinek #define	RCAP_SERVICE	"system/rcap:default"
151*0209230bSgjelinek #define	POOLD_SERVICE	"system/pools/dynamic:default"
152*0209230bSgjelinek 
153*0209230bSgjelinek /*
154*0209230bSgjelinek  * rctl alias definitions
155*0209230bSgjelinek  *
156*0209230bSgjelinek  * This holds the alias, the full rctl name, the default priv value, action
157*0209230bSgjelinek  * and lower limit.  The functions that handle rctl aliases step through
158*0209230bSgjelinek  * this table, matching on the alias, and using the full values for setting
159*0209230bSgjelinek  * the rctl entry as well the limit for validation.
160*0209230bSgjelinek  */
161*0209230bSgjelinek static struct alias {
162*0209230bSgjelinek 	char *shortname;
163*0209230bSgjelinek 	char *realname;
164*0209230bSgjelinek 	char *priv;
165*0209230bSgjelinek 	char *action;
166*0209230bSgjelinek 	uint64_t low_limit;
167*0209230bSgjelinek } aliases[] = {
168*0209230bSgjelinek 	{ALIAS_MAXLWPS, "zone.max-lwps", "privileged", "deny", 100},
169*0209230bSgjelinek 	{ALIAS_MAXSHMMEM, "zone.max-shm-memory", "privileged", "deny", 0},
170*0209230bSgjelinek 	{ALIAS_MAXSHMIDS, "zone.max-shm-ids", "privileged", "deny", 0},
171*0209230bSgjelinek 	{ALIAS_MAXMSGIDS, "zone.max-msg-ids", "privileged", "deny", 0},
172*0209230bSgjelinek 	{ALIAS_MAXSEMIDS, "zone.max-sem-ids", "privileged", "deny", 0},
173*0209230bSgjelinek 	{ALIAS_MAXLOCKEDMEM, "zone.max-locked-memory", "privileged", "deny", 0},
174*0209230bSgjelinek 	{ALIAS_MAXSWAP, "zone.max-swap", "privileged", "deny", 0},
175*0209230bSgjelinek 	{ALIAS_SHARES, "zone.cpu-shares", "privileged", "none", 0},
176*0209230bSgjelinek 	{NULL, NULL, NULL, NULL, 0}
177*0209230bSgjelinek };
178*0209230bSgjelinek 
179*0209230bSgjelinek /*
180*0209230bSgjelinek  * Structure for applying rctls to a running zone.  It allows important
181*0209230bSgjelinek  * process values to be passed together easily.
182*0209230bSgjelinek  */
183*0209230bSgjelinek typedef struct pr_info_handle {
184*0209230bSgjelinek 	struct ps_prochandle *pr;
185*0209230bSgjelinek 	pid_t pid;
186*0209230bSgjelinek } pr_info_handle_t;
187*0209230bSgjelinek 
1887c478bd9Sstevel@tonic-gate struct zone_dochandle {
1897c478bd9Sstevel@tonic-gate 	char		*zone_dh_rootdir;
1907c478bd9Sstevel@tonic-gate 	xmlDocPtr	zone_dh_doc;
1917c478bd9Sstevel@tonic-gate 	xmlNodePtr	zone_dh_cur;
1927c478bd9Sstevel@tonic-gate 	xmlNodePtr	zone_dh_top;
193087719fdSdp 	boolean_t	zone_dh_newzone;
194087719fdSdp 	boolean_t	zone_dh_snapshot;
195ee519a1fSgjelinek 	boolean_t	zone_dh_sw_inv;
196087719fdSdp 	char		zone_dh_delete_name[ZONENAME_MAX];
1977c478bd9Sstevel@tonic-gate };
1987c478bd9Sstevel@tonic-gate 
199cf8f45c7Sdstaff struct znotify {
200cf8f45c7Sdstaff 	void * zn_private;
201cf8f45c7Sdstaff 	evchan_t *zn_eventchan;
202cf8f45c7Sdstaff 	int (*zn_callback)(const  char *zonename, zoneid_t zid,
203cf8f45c7Sdstaff 	    const char *newstate, const char *oldstate, hrtime_t when, void *p);
204cf8f45c7Sdstaff 	pthread_mutex_t zn_mutex;
205cf8f45c7Sdstaff 	pthread_cond_t zn_cond;
206cf8f45c7Sdstaff 	pthread_mutex_t zn_bigmutex;
207cf8f45c7Sdstaff 	volatile enum {ZN_UNLOCKED, ZN_LOCKED, ZN_PING_INFLIGHT,
208cf8f45c7Sdstaff 	    ZN_PING_RECEIVED} zn_state;
209cf8f45c7Sdstaff 	char zn_subscriber_id[MAX_SUBID_LEN];
210cf8f45c7Sdstaff 	volatile boolean_t zn_failed;
211cf8f45c7Sdstaff 	int zn_failure_count;
212cf8f45c7Sdstaff };
213cf8f45c7Sdstaff 
214ee519a1fSgjelinek struct zone_pkginfo {
215ee519a1fSgjelinek 	boolean_t	zpi_all_zones;
216ee519a1fSgjelinek 	boolean_t	zpi_this_zone;
217ee519a1fSgjelinek 	int		zpi_patch_cnt;
218ee519a1fSgjelinek 	char		*zpi_version;
219ee519a1fSgjelinek 	char		**zpi_patchinfo;
220ee519a1fSgjelinek };
221ee519a1fSgjelinek 
222108322fbScarlsonj char *zonecfg_root = "";
223108322fbScarlsonj 
2247c478bd9Sstevel@tonic-gate /*
2257c478bd9Sstevel@tonic-gate  * For functions which return int, which is most of the functions herein,
2267c478bd9Sstevel@tonic-gate  * the return values should be from the Z_foo set defined in <libzonecfg.h>.
2277c478bd9Sstevel@tonic-gate  * In some instances, we take pains mapping some libc errno values to Z_foo
2287c478bd9Sstevel@tonic-gate  * values from this set.
2297c478bd9Sstevel@tonic-gate  */
2307c478bd9Sstevel@tonic-gate 
231108322fbScarlsonj /*
232108322fbScarlsonj  * Set the root (/) path for all zonecfg configuration files.  This is a
233108322fbScarlsonj  * private interface used by Live Upgrade extensions to access zone
234108322fbScarlsonj  * configuration inside mounted alternate boot environments.
235108322fbScarlsonj  */
236108322fbScarlsonj void
237108322fbScarlsonj zonecfg_set_root(const char *rootpath)
238108322fbScarlsonj {
239108322fbScarlsonj 	if (*zonecfg_root != '\0')
240108322fbScarlsonj 		free(zonecfg_root);
241108322fbScarlsonj 	if (rootpath == NULL || rootpath[0] == '\0' || rootpath[1] == '\0' ||
242108322fbScarlsonj 	    (zonecfg_root = strdup(rootpath)) == NULL)
243108322fbScarlsonj 		zonecfg_root = "";
244108322fbScarlsonj }
245108322fbScarlsonj 
246108322fbScarlsonj const char *
247108322fbScarlsonj zonecfg_get_root(void)
248108322fbScarlsonj {
249108322fbScarlsonj 	return (zonecfg_root);
250108322fbScarlsonj }
251108322fbScarlsonj 
252108322fbScarlsonj boolean_t
253108322fbScarlsonj zonecfg_in_alt_root(void)
254108322fbScarlsonj {
255108322fbScarlsonj 	return (*zonecfg_root != '\0');
256108322fbScarlsonj }
257108322fbScarlsonj 
2587c478bd9Sstevel@tonic-gate /*
2597c478bd9Sstevel@tonic-gate  * Callers of the _file_path() functions are expected to have the second
2607c478bd9Sstevel@tonic-gate  * parameter be a (char foo[MAXPATHLEN]).
2617c478bd9Sstevel@tonic-gate  */
2627c478bd9Sstevel@tonic-gate 
263108322fbScarlsonj static boolean_t
2647c478bd9Sstevel@tonic-gate config_file_path(const char *zonename, char *answer)
2657c478bd9Sstevel@tonic-gate {
266108322fbScarlsonj 	return (snprintf(answer, MAXPATHLEN, "%s%s/%s.xml", zonecfg_root,
267108322fbScarlsonj 	    ZONE_CONFIG_ROOT, zonename) < MAXPATHLEN);
2687c478bd9Sstevel@tonic-gate }
2697c478bd9Sstevel@tonic-gate 
270108322fbScarlsonj static boolean_t
271108322fbScarlsonj snap_file_path(const char *zonename, char *answer)
2727c478bd9Sstevel@tonic-gate {
273108322fbScarlsonj 	return (snprintf(answer, MAXPATHLEN, "%s%s/%s.snapshot.xml",
274108322fbScarlsonj 	    zonecfg_root, ZONE_SNAPSHOT_ROOT, zonename) < MAXPATHLEN);
2757c478bd9Sstevel@tonic-gate }
2767c478bd9Sstevel@tonic-gate 
2777c478bd9Sstevel@tonic-gate /*ARGSUSED*/
2787c478bd9Sstevel@tonic-gate static void
2797c478bd9Sstevel@tonic-gate zonecfg_error_func(void *ctx, const char *msg, ...)
2807c478bd9Sstevel@tonic-gate {
2817c478bd9Sstevel@tonic-gate 	/*
2827c478bd9Sstevel@tonic-gate 	 * This function does nothing by design.  Its purpose is to prevent
2837c478bd9Sstevel@tonic-gate 	 * libxml from dumping unwanted messages to stdout/stderr.
2847c478bd9Sstevel@tonic-gate 	 */
2857c478bd9Sstevel@tonic-gate }
2867c478bd9Sstevel@tonic-gate 
2877c478bd9Sstevel@tonic-gate zone_dochandle_t
2887c478bd9Sstevel@tonic-gate zonecfg_init_handle(void)
2897c478bd9Sstevel@tonic-gate {
290087719fdSdp 	zone_dochandle_t handle = calloc(1, sizeof (struct zone_dochandle));
2917c478bd9Sstevel@tonic-gate 	if (handle == NULL) {
2927c478bd9Sstevel@tonic-gate 		errno = Z_NOMEM;
2937c478bd9Sstevel@tonic-gate 		return (NULL);
2947c478bd9Sstevel@tonic-gate 	}
2957c478bd9Sstevel@tonic-gate 
2967c478bd9Sstevel@tonic-gate 	/* generic libxml initialization */
2977c478bd9Sstevel@tonic-gate 	xmlLineNumbersDefault(1);
2987c478bd9Sstevel@tonic-gate 	xmlLoadExtDtdDefaultValue |= XML_DETECT_IDS;
2997c478bd9Sstevel@tonic-gate 	xmlDoValidityCheckingDefaultValue = 1;
3007c478bd9Sstevel@tonic-gate 	(void) xmlKeepBlanksDefault(0);
3017c478bd9Sstevel@tonic-gate 	xmlGetWarningsDefaultValue = 0;
3027c478bd9Sstevel@tonic-gate 	xmlSetGenericErrorFunc(NULL, zonecfg_error_func);
3037c478bd9Sstevel@tonic-gate 
3047c478bd9Sstevel@tonic-gate 	return (handle);
3057c478bd9Sstevel@tonic-gate }
3067c478bd9Sstevel@tonic-gate 
3077c478bd9Sstevel@tonic-gate int
3087c478bd9Sstevel@tonic-gate zonecfg_check_handle(zone_dochandle_t handle)
3097c478bd9Sstevel@tonic-gate {
3107c478bd9Sstevel@tonic-gate 	if (handle == NULL || handle->zone_dh_doc == NULL)
3117c478bd9Sstevel@tonic-gate 		return (Z_BAD_HANDLE);
3127c478bd9Sstevel@tonic-gate 	return (Z_OK);
3137c478bd9Sstevel@tonic-gate }
3147c478bd9Sstevel@tonic-gate 
3157c478bd9Sstevel@tonic-gate void
3167c478bd9Sstevel@tonic-gate zonecfg_fini_handle(zone_dochandle_t handle)
3177c478bd9Sstevel@tonic-gate {
3187c478bd9Sstevel@tonic-gate 	if (zonecfg_check_handle(handle) == Z_OK)
3197c478bd9Sstevel@tonic-gate 		xmlFreeDoc(handle->zone_dh_doc);
3207c478bd9Sstevel@tonic-gate 	if (handle != NULL)
3217c478bd9Sstevel@tonic-gate 		free(handle);
3227c478bd9Sstevel@tonic-gate }
3237c478bd9Sstevel@tonic-gate 
3247c478bd9Sstevel@tonic-gate static int
3257c478bd9Sstevel@tonic-gate zonecfg_destroy_impl(char *filename)
3267c478bd9Sstevel@tonic-gate {
3277c478bd9Sstevel@tonic-gate 	if (unlink(filename) == -1) {
3287c478bd9Sstevel@tonic-gate 		if (errno == EACCES)
3297c478bd9Sstevel@tonic-gate 			return (Z_ACCES);
3307c478bd9Sstevel@tonic-gate 		if (errno == ENOENT)
3317c478bd9Sstevel@tonic-gate 			return (Z_NO_ZONE);
3327c478bd9Sstevel@tonic-gate 		return (Z_MISC_FS);
3337c478bd9Sstevel@tonic-gate 	}
3347c478bd9Sstevel@tonic-gate 	return (Z_OK);
3357c478bd9Sstevel@tonic-gate }
3367c478bd9Sstevel@tonic-gate 
3377c478bd9Sstevel@tonic-gate int
338087719fdSdp zonecfg_destroy(const char *zonename, boolean_t force)
3397c478bd9Sstevel@tonic-gate {
3407c478bd9Sstevel@tonic-gate 	char path[MAXPATHLEN];
341087719fdSdp 	struct zoneent ze;
342087719fdSdp 	int err, state_err;
343087719fdSdp 	zone_state_t state;
3447c478bd9Sstevel@tonic-gate 
345108322fbScarlsonj 	if (!config_file_path(zonename, path))
346108322fbScarlsonj 		return (Z_MISC_FS);
347087719fdSdp 
348087719fdSdp 	state_err = zone_get_state((char *)zonename, &state);
349087719fdSdp 	err = access(path, W_OK);
350087719fdSdp 
351087719fdSdp 	/*
352087719fdSdp 	 * If there is no file, and no index entry, reliably indicate that no
353087719fdSdp 	 * such zone exists.
354087719fdSdp 	 */
355087719fdSdp 	if ((state_err == Z_NO_ZONE) && (err == -1) && (errno == ENOENT))
356087719fdSdp 		return (Z_NO_ZONE);
357087719fdSdp 
358087719fdSdp 	/*
359087719fdSdp 	 * Handle any other filesystem related errors (except if the XML
360087719fdSdp 	 * file is missing, which we treat silently), unless we're forcing,
361087719fdSdp 	 * in which case we plow on.
362087719fdSdp 	 */
363087719fdSdp 	if (err == -1 && errno != ENOENT) {
364087719fdSdp 		if (errno == EACCES)
365087719fdSdp 			return (Z_ACCES);
366087719fdSdp 		else if (!force)
367087719fdSdp 			return (Z_MISC_FS);
368087719fdSdp 	}
369087719fdSdp 
370087719fdSdp 	if (state > ZONE_STATE_INSTALLED)
371087719fdSdp 		return (Z_BAD_ZONE_STATE);
372087719fdSdp 
373087719fdSdp 	if (!force && state > ZONE_STATE_CONFIGURED)
374087719fdSdp 		return (Z_BAD_ZONE_STATE);
375087719fdSdp 
376087719fdSdp 	/*
377087719fdSdp 	 * Index deletion succeeds even if the entry doesn't exist.  So this
378087719fdSdp 	 * will fail only if we've had some more severe problem.
379087719fdSdp 	 */
380087719fdSdp 	bzero(&ze, sizeof (ze));
381087719fdSdp 	(void) strlcpy(ze.zone_name, zonename, sizeof (ze.zone_name));
382087719fdSdp 	if ((err = putzoneent(&ze, PZE_REMOVE)) != Z_OK)
383087719fdSdp 		if (!force)
384087719fdSdp 			return (err);
385087719fdSdp 
386087719fdSdp 	err = zonecfg_destroy_impl(path);
387087719fdSdp 
388087719fdSdp 	/*
389087719fdSdp 	 * Treat failure to find the XML file silently, since, well, it's
390087719fdSdp 	 * gone, and with the index file cleaned up, we're done.
391087719fdSdp 	 */
392087719fdSdp 	if (err == Z_OK || err == Z_NO_ZONE)
393087719fdSdp 		return (Z_OK);
394087719fdSdp 	return (err);
3957c478bd9Sstevel@tonic-gate }
3967c478bd9Sstevel@tonic-gate 
3977c478bd9Sstevel@tonic-gate int
398108322fbScarlsonj zonecfg_destroy_snapshot(const char *zonename)
3997c478bd9Sstevel@tonic-gate {
4007c478bd9Sstevel@tonic-gate 	char path[MAXPATHLEN];
4017c478bd9Sstevel@tonic-gate 
402108322fbScarlsonj 	if (!snap_file_path(zonename, path))
403108322fbScarlsonj 		return (Z_MISC_FS);
4047c478bd9Sstevel@tonic-gate 	return (zonecfg_destroy_impl(path));
4057c478bd9Sstevel@tonic-gate }
4067c478bd9Sstevel@tonic-gate 
4077c478bd9Sstevel@tonic-gate static int
408a1be23daSdp getroot(zone_dochandle_t handle, xmlNodePtr *root)
4097c478bd9Sstevel@tonic-gate {
4107c478bd9Sstevel@tonic-gate 	if (zonecfg_check_handle(handle) == Z_BAD_HANDLE)
4117c478bd9Sstevel@tonic-gate 		return (Z_BAD_HANDLE);
4127c478bd9Sstevel@tonic-gate 
413a1be23daSdp 	*root = xmlDocGetRootElement(handle->zone_dh_doc);
414a1be23daSdp 
415a1be23daSdp 	if (*root == NULL)
4167c478bd9Sstevel@tonic-gate 		return (Z_EMPTY_DOCUMENT);
4177c478bd9Sstevel@tonic-gate 
418a1be23daSdp 	if (xmlStrcmp((*root)->name, DTD_ELEM_ZONE))
4197c478bd9Sstevel@tonic-gate 		return (Z_WRONG_DOC_TYPE);
420a1be23daSdp 
421a1be23daSdp 	return (Z_OK);
422a1be23daSdp }
423a1be23daSdp 
424a1be23daSdp static int
425a1be23daSdp operation_prep(zone_dochandle_t handle)
426a1be23daSdp {
427a1be23daSdp 	xmlNodePtr root;
428a1be23daSdp 	int err;
429a1be23daSdp 
430a1be23daSdp 	if ((err = getroot(handle, &root)) != 0)
431a1be23daSdp 		return (err);
432a1be23daSdp 
433a1be23daSdp 	handle->zone_dh_cur = root;
434a1be23daSdp 	handle->zone_dh_top = root;
435a1be23daSdp 	return (Z_OK);
436a1be23daSdp }
437a1be23daSdp 
438ffbafc53Scomay static int
439ffbafc53Scomay fetchprop(xmlNodePtr cur, const xmlChar *propname, char *dst, size_t dstsize)
440ffbafc53Scomay {
441ffbafc53Scomay 	xmlChar *property;
442ffbafc53Scomay 	size_t srcsize;
443ffbafc53Scomay 
444ffbafc53Scomay 	if ((property = xmlGetProp(cur, propname)) == NULL)
445ffbafc53Scomay 		return (Z_BAD_PROPERTY);
446ffbafc53Scomay 	srcsize = strlcpy(dst, (char *)property, dstsize);
447ffbafc53Scomay 	xmlFree(property);
448ffbafc53Scomay 	if (srcsize >= dstsize)
449ffbafc53Scomay 		return (Z_TOO_BIG);
450ffbafc53Scomay 	return (Z_OK);
451ffbafc53Scomay }
452ffbafc53Scomay 
453ffbafc53Scomay static int
454ffbafc53Scomay fetch_alloc_prop(xmlNodePtr cur, const xmlChar *propname, char **dst)
455ffbafc53Scomay {
456ffbafc53Scomay 	xmlChar *property;
457ffbafc53Scomay 
458ffbafc53Scomay 	if ((property = xmlGetProp(cur, propname)) == NULL)
459ffbafc53Scomay 		return (Z_BAD_PROPERTY);
460ffbafc53Scomay 	if ((*dst = strdup((char *)property)) == NULL) {
461ffbafc53Scomay 		xmlFree(property);
462ffbafc53Scomay 		return (Z_NOMEM);
463ffbafc53Scomay 	}
464ffbafc53Scomay 	xmlFree(property);
465ffbafc53Scomay 	return (Z_OK);
466ffbafc53Scomay }
467ffbafc53Scomay 
468a1be23daSdp static int
469a1be23daSdp getrootattr(zone_dochandle_t handle, const xmlChar *propname,
470a1be23daSdp     char *propval, size_t propsize)
471a1be23daSdp {
472a1be23daSdp 	xmlNodePtr root;
473a1be23daSdp 	int err;
474a1be23daSdp 
475a1be23daSdp 	if ((err = getroot(handle, &root)) != 0)
476a1be23daSdp 		return (err);
477a1be23daSdp 
478ffbafc53Scomay 	return (fetchprop(root, propname, propval, propsize));
479ffbafc53Scomay }
480ffbafc53Scomay 
481ffbafc53Scomay static int
482ffbafc53Scomay get_alloc_rootattr(zone_dochandle_t handle, const xmlChar *propname,
483ffbafc53Scomay     char **propval)
484ffbafc53Scomay {
485ffbafc53Scomay 	xmlNodePtr root;
486ffbafc53Scomay 	int err;
487ffbafc53Scomay 
488ffbafc53Scomay 	if ((err = getroot(handle, &root)) != 0)
489ffbafc53Scomay 		return (err);
490ffbafc53Scomay 
491ffbafc53Scomay 	return (fetch_alloc_prop(root, propname, propval));
492a1be23daSdp }
493a1be23daSdp 
494a1be23daSdp static int
495108322fbScarlsonj setrootattr(zone_dochandle_t handle, const xmlChar *propname,
496108322fbScarlsonj     const char *propval)
497a1be23daSdp {
498a1be23daSdp 	int err;
499a1be23daSdp 	xmlNodePtr root;
500a1be23daSdp 
501a1be23daSdp 	if ((err = getroot(handle, &root)) != Z_OK)
502a1be23daSdp 		return (err);
503a1be23daSdp 
504*0209230bSgjelinek 	/*
505*0209230bSgjelinek 	 * If we get a null propval remove the property (ignore return since it
506*0209230bSgjelinek 	 * may not be set to begin with).
507*0209230bSgjelinek 	 */
508*0209230bSgjelinek 	if (propval == NULL) {
509*0209230bSgjelinek 		(void) xmlUnsetProp(root, propname);
510*0209230bSgjelinek 	} else {
511*0209230bSgjelinek 		if (xmlSetProp(root, propname, (const xmlChar *) propval)
512*0209230bSgjelinek 		    == NULL)
513*0209230bSgjelinek 			return (Z_INVAL);
514*0209230bSgjelinek 	}
5157c478bd9Sstevel@tonic-gate 	return (Z_OK);
5167c478bd9Sstevel@tonic-gate }
5177c478bd9Sstevel@tonic-gate 
518087719fdSdp static void
519087719fdSdp addcomment(zone_dochandle_t handle, const char *comment)
520087719fdSdp {
521087719fdSdp 	xmlNodePtr node;
522087719fdSdp 	node = xmlNewComment((xmlChar *) comment);
523087719fdSdp 
524087719fdSdp 	if (node != NULL)
525087719fdSdp 		(void) xmlAddPrevSibling(handle->zone_dh_top, node);
526087719fdSdp }
527087719fdSdp 
528087719fdSdp static void
529087719fdSdp stripcomments(zone_dochandle_t handle)
530087719fdSdp {
531087719fdSdp 	xmlDocPtr top;
532087719fdSdp 	xmlNodePtr child, next;
533087719fdSdp 
534087719fdSdp 	top = handle->zone_dh_doc;
535087719fdSdp 	for (child = top->xmlChildrenNode; child != NULL; child = next) {
536087719fdSdp 		next = child->next;
537087719fdSdp 		if (child->name == NULL)
538087719fdSdp 			continue;
539087719fdSdp 		if (xmlStrcmp(child->name, DTD_ELEM_COMMENT) == 0) {
540087719fdSdp 			next = child->next;
541087719fdSdp 			xmlUnlinkNode(child);
542087719fdSdp 			xmlFreeNode(child);
543087719fdSdp 		}
544087719fdSdp 	}
545087719fdSdp }
546087719fdSdp 
547ee519a1fSgjelinek static void
548ee519a1fSgjelinek strip_sw_inv(zone_dochandle_t handle)
549ee519a1fSgjelinek {
550ee519a1fSgjelinek 	xmlNodePtr root, child, next;
551ee519a1fSgjelinek 
552ee519a1fSgjelinek 	root = xmlDocGetRootElement(handle->zone_dh_doc);
553ee519a1fSgjelinek 	for (child = root->xmlChildrenNode; child != NULL; child = next) {
554ee519a1fSgjelinek 		next = child->next;
555ee519a1fSgjelinek 		if (child->name == NULL)
556ee519a1fSgjelinek 			continue;
557ee519a1fSgjelinek 		if (xmlStrcmp(child->name, DTD_ELEM_PACKAGE) == 0 ||
558ee519a1fSgjelinek 		    xmlStrcmp(child->name, DTD_ELEM_PATCH) == 0) {
559ee519a1fSgjelinek 			next = child->next;
560ee519a1fSgjelinek 			xmlUnlinkNode(child);
561ee519a1fSgjelinek 			xmlFreeNode(child);
562ee519a1fSgjelinek 		}
563ee519a1fSgjelinek 	}
564ee519a1fSgjelinek }
565ee519a1fSgjelinek 
5667c478bd9Sstevel@tonic-gate static int
567108322fbScarlsonj zonecfg_get_handle_impl(const char *zonename, const char *filename,
568108322fbScarlsonj     zone_dochandle_t handle)
5697c478bd9Sstevel@tonic-gate {
5707c478bd9Sstevel@tonic-gate 	xmlValidCtxtPtr cvp;
5717c478bd9Sstevel@tonic-gate 	struct stat statbuf;
5727c478bd9Sstevel@tonic-gate 	int valid;
5737c478bd9Sstevel@tonic-gate 
5747c478bd9Sstevel@tonic-gate 	if (zonename == NULL)
5757c478bd9Sstevel@tonic-gate 		return (Z_NO_ZONE);
5769acbbeafSnn 
5777c478bd9Sstevel@tonic-gate 	if ((handle->zone_dh_doc = xmlParseFile(filename)) == NULL) {
5787c478bd9Sstevel@tonic-gate 		/* distinguish file not found vs. found but not parsed */
5797c478bd9Sstevel@tonic-gate 		if (stat(filename, &statbuf) == 0)
5807c478bd9Sstevel@tonic-gate 			return (Z_INVALID_DOCUMENT);
5817c478bd9Sstevel@tonic-gate 		return (Z_NO_ZONE);
5827c478bd9Sstevel@tonic-gate 	}
5837c478bd9Sstevel@tonic-gate 	if ((cvp = xmlNewValidCtxt()) == NULL)
5847c478bd9Sstevel@tonic-gate 		return (Z_NOMEM);
5857c478bd9Sstevel@tonic-gate 	cvp->error = zonecfg_error_func;
5867c478bd9Sstevel@tonic-gate 	cvp->warning = zonecfg_error_func;
5877c478bd9Sstevel@tonic-gate 	valid = xmlValidateDocument(cvp, handle->zone_dh_doc);
5887c478bd9Sstevel@tonic-gate 	xmlFreeValidCtxt(cvp);
5897c478bd9Sstevel@tonic-gate 	if (valid == 0)
5907c478bd9Sstevel@tonic-gate 		return (Z_INVALID_DOCUMENT);
591087719fdSdp 
5927c478bd9Sstevel@tonic-gate 	/* delete any comments such as inherited Sun copyright / ident str */
593087719fdSdp 	stripcomments(handle);
5947c478bd9Sstevel@tonic-gate 	return (Z_OK);
5957c478bd9Sstevel@tonic-gate }
5967c478bd9Sstevel@tonic-gate 
5977c478bd9Sstevel@tonic-gate int
598108322fbScarlsonj zonecfg_get_handle(const char *zonename, zone_dochandle_t handle)
5997c478bd9Sstevel@tonic-gate {
6007c478bd9Sstevel@tonic-gate 	char path[MAXPATHLEN];
6017c478bd9Sstevel@tonic-gate 
602108322fbScarlsonj 	if (!config_file_path(zonename, path))
603108322fbScarlsonj 		return (Z_MISC_FS);
604087719fdSdp 	handle->zone_dh_newzone = B_FALSE;
605087719fdSdp 
6067c478bd9Sstevel@tonic-gate 	return (zonecfg_get_handle_impl(zonename, path, handle));
6077c478bd9Sstevel@tonic-gate }
6087c478bd9Sstevel@tonic-gate 
609ee519a1fSgjelinek int
610ee519a1fSgjelinek zonecfg_get_attach_handle(const char *path, const char *zonename,
611ee519a1fSgjelinek     boolean_t preserve_sw, zone_dochandle_t handle)
612ee519a1fSgjelinek {
613ee519a1fSgjelinek 	char		migpath[MAXPATHLEN];
614ee519a1fSgjelinek 	int		err;
615ee519a1fSgjelinek 	struct stat	buf;
616ee519a1fSgjelinek 
617ee519a1fSgjelinek 	if (snprintf(migpath, sizeof (migpath), "%s/root", path) >=
618ee519a1fSgjelinek 	    sizeof (migpath))
619ee519a1fSgjelinek 		return (Z_NOMEM);
620ee519a1fSgjelinek 
621ee519a1fSgjelinek 	if (stat(migpath, &buf) == -1 || !S_ISDIR(buf.st_mode))
622ee519a1fSgjelinek 		return (Z_NO_ZONE);
623ee519a1fSgjelinek 
624ee519a1fSgjelinek 	if (snprintf(migpath, sizeof (migpath), "%s/%s", path, DETACHED) >=
625ee519a1fSgjelinek 	    sizeof (migpath))
626ee519a1fSgjelinek 		return (Z_NOMEM);
627ee519a1fSgjelinek 
628ee519a1fSgjelinek 	if ((err = zonecfg_get_handle_impl(zonename, migpath, handle)) != Z_OK)
629ee519a1fSgjelinek 		return (err);
630ee519a1fSgjelinek 
631ee519a1fSgjelinek 	if (!preserve_sw)
632ee519a1fSgjelinek 		strip_sw_inv(handle);
633ee519a1fSgjelinek 
634ee519a1fSgjelinek 	handle->zone_dh_newzone = B_TRUE;
635ee519a1fSgjelinek 	if ((err = setrootattr(handle, DTD_ATTR_ZONEPATH, path)) != Z_OK)
636ee519a1fSgjelinek 		return (err);
637ee519a1fSgjelinek 
638ee519a1fSgjelinek 	return (setrootattr(handle, DTD_ATTR_NAME, zonename));
639ee519a1fSgjelinek }
640ee519a1fSgjelinek 
6417c478bd9Sstevel@tonic-gate int
642108322fbScarlsonj zonecfg_get_snapshot_handle(const char *zonename, zone_dochandle_t handle)
6437c478bd9Sstevel@tonic-gate {
6447c478bd9Sstevel@tonic-gate 	char path[MAXPATHLEN];
6457c478bd9Sstevel@tonic-gate 
646108322fbScarlsonj 	if (!snap_file_path(zonename, path))
647108322fbScarlsonj 		return (Z_MISC_FS);
648087719fdSdp 	handle->zone_dh_newzone = B_FALSE;
6497c478bd9Sstevel@tonic-gate 	return (zonecfg_get_handle_impl(zonename, path, handle));
6507c478bd9Sstevel@tonic-gate }
6517c478bd9Sstevel@tonic-gate 
652087719fdSdp int
653108322fbScarlsonj zonecfg_get_template_handle(const char *template, const char *zonename,
654087719fdSdp     zone_dochandle_t handle)
655087719fdSdp {
656087719fdSdp 	char path[MAXPATHLEN];
657087719fdSdp 	int err;
658087719fdSdp 
659108322fbScarlsonj 	if (!config_file_path(template, path))
660108322fbScarlsonj 		return (Z_MISC_FS);
661087719fdSdp 
662087719fdSdp 	if ((err = zonecfg_get_handle_impl(template, path, handle)) != Z_OK)
663087719fdSdp 		return (err);
664087719fdSdp 	handle->zone_dh_newzone = B_TRUE;
665087719fdSdp 	return (setrootattr(handle, DTD_ATTR_NAME, zonename));
666087719fdSdp }
667087719fdSdp 
6689acbbeafSnn int
6699acbbeafSnn zonecfg_get_xml_handle(const char *path, zone_dochandle_t handle)
6709acbbeafSnn {
6719acbbeafSnn 	struct stat buf;
6729acbbeafSnn 	int err;
6739acbbeafSnn 
6749acbbeafSnn 	if (stat(path, &buf) == -1)
6759acbbeafSnn 		return (Z_MISC_FS);
6769acbbeafSnn 
6779acbbeafSnn 	if ((err = zonecfg_get_handle_impl("xml", path, handle)) != Z_OK)
6789acbbeafSnn 		return (err);
6799acbbeafSnn 	handle->zone_dh_newzone = B_TRUE;
6809acbbeafSnn 	return (Z_OK);
6819acbbeafSnn }
6829acbbeafSnn 
6838cd327d5Sgjelinek /*
6848cd327d5Sgjelinek  * Initialize two handles from the manifest read on fd.  The rem_handle
6858cd327d5Sgjelinek  * is initialized from the input file, including the sw inventory.  The
6868cd327d5Sgjelinek  * local_handle is initialized with the same zone configuration but with
6878cd327d5Sgjelinek  * no sw inventory.
6888cd327d5Sgjelinek  */
6898cd327d5Sgjelinek int
6908cd327d5Sgjelinek zonecfg_attach_manifest(int fd, zone_dochandle_t local_handle,
6918cd327d5Sgjelinek     zone_dochandle_t rem_handle)
6928cd327d5Sgjelinek {
6938cd327d5Sgjelinek 	xmlValidCtxtPtr cvp;
6948cd327d5Sgjelinek 	int valid;
6958cd327d5Sgjelinek 
6968cd327d5Sgjelinek 	/* load the manifest into the handle for the remote system */
6978cd327d5Sgjelinek 	if ((rem_handle->zone_dh_doc = xmlReadFd(fd, NULL, NULL, 0)) == NULL) {
6988cd327d5Sgjelinek 		return (Z_INVALID_DOCUMENT);
6998cd327d5Sgjelinek 	}
7008cd327d5Sgjelinek 	if ((cvp = xmlNewValidCtxt()) == NULL)
7018cd327d5Sgjelinek 		return (Z_NOMEM);
7028cd327d5Sgjelinek 	cvp->error = zonecfg_error_func;
7038cd327d5Sgjelinek 	cvp->warning = zonecfg_error_func;
7048cd327d5Sgjelinek 	valid = xmlValidateDocument(cvp, rem_handle->zone_dh_doc);
7058cd327d5Sgjelinek 	xmlFreeValidCtxt(cvp);
7068cd327d5Sgjelinek 	if (valid == 0)
7078cd327d5Sgjelinek 		return (Z_INVALID_DOCUMENT);
7088cd327d5Sgjelinek 
7098cd327d5Sgjelinek 	/* delete any comments such as inherited Sun copyright / ident str */
7108cd327d5Sgjelinek 	stripcomments(rem_handle);
7118cd327d5Sgjelinek 
7128cd327d5Sgjelinek 	rem_handle->zone_dh_newzone = B_TRUE;
7138cd327d5Sgjelinek 	rem_handle->zone_dh_sw_inv = B_TRUE;
7148cd327d5Sgjelinek 
7158cd327d5Sgjelinek 	/*
7168cd327d5Sgjelinek 	 * Now use the remote system handle to generate a local system handle
7178cd327d5Sgjelinek 	 * with an identical zones configuration but no sw inventory.
7188cd327d5Sgjelinek 	 */
7198cd327d5Sgjelinek 	if ((local_handle->zone_dh_doc = xmlCopyDoc(rem_handle->zone_dh_doc,
7208cd327d5Sgjelinek 	    1)) == NULL) {
7218cd327d5Sgjelinek 		return (Z_INVALID_DOCUMENT);
7228cd327d5Sgjelinek 	}
7238cd327d5Sgjelinek 
7248cd327d5Sgjelinek 	/*
7258cd327d5Sgjelinek 	 * We need to re-run xmlValidateDocument on local_handle to properly
7268cd327d5Sgjelinek 	 * update the in-core representation of the configuration.
7278cd327d5Sgjelinek 	 */
7288cd327d5Sgjelinek 	if ((cvp = xmlNewValidCtxt()) == NULL)
7298cd327d5Sgjelinek 		return (Z_NOMEM);
7308cd327d5Sgjelinek 	cvp->error = zonecfg_error_func;
7318cd327d5Sgjelinek 	cvp->warning = zonecfg_error_func;
7328cd327d5Sgjelinek 	valid = xmlValidateDocument(cvp, local_handle->zone_dh_doc);
7338cd327d5Sgjelinek 	xmlFreeValidCtxt(cvp);
7348cd327d5Sgjelinek 	if (valid == 0)
7358cd327d5Sgjelinek 		return (Z_INVALID_DOCUMENT);
7368cd327d5Sgjelinek 
7378cd327d5Sgjelinek 	strip_sw_inv(local_handle);
7388cd327d5Sgjelinek 
7398cd327d5Sgjelinek 	local_handle->zone_dh_newzone = B_TRUE;
7408cd327d5Sgjelinek 	local_handle->zone_dh_sw_inv = B_FALSE;
7418cd327d5Sgjelinek 
7428cd327d5Sgjelinek 	return (Z_OK);
7438cd327d5Sgjelinek }
7448cd327d5Sgjelinek 
745087719fdSdp static boolean_t
746087719fdSdp is_renaming(zone_dochandle_t handle)
747087719fdSdp {
748087719fdSdp 	if (handle->zone_dh_newzone)
749087719fdSdp 		return (B_FALSE);
750087719fdSdp 	if (strlen(handle->zone_dh_delete_name) > 0)
751087719fdSdp 		return (B_TRUE);
752087719fdSdp 	return (B_FALSE);
753087719fdSdp }
754087719fdSdp 
755087719fdSdp static boolean_t
756087719fdSdp is_new(zone_dochandle_t handle)
757087719fdSdp {
758087719fdSdp 	return (handle->zone_dh_newzone || handle->zone_dh_snapshot);
759087719fdSdp }
760087719fdSdp 
761087719fdSdp static boolean_t
762087719fdSdp is_snapshot(zone_dochandle_t handle)
763087719fdSdp {
764087719fdSdp 	return (handle->zone_dh_snapshot);
765087719fdSdp }
766087719fdSdp 
767087719fdSdp /*
768087719fdSdp  * It would be great to be able to use libc's ctype(3c) macros, but we
769087719fdSdp  * can't, as they are locale sensitive, and it would break our limited thread
770087719fdSdp  * safety if this routine had to change the app locale on the fly.
771087719fdSdp  */
772087719fdSdp int
773108322fbScarlsonj zonecfg_validate_zonename(const char *zone)
774087719fdSdp {
775087719fdSdp 	int i;
776087719fdSdp 
777087719fdSdp 	if (strcmp(zone, GLOBAL_ZONENAME) == 0)
778087719fdSdp 		return (Z_BOGUS_ZONE_NAME);
779087719fdSdp 
780087719fdSdp 	if (strlen(zone) >= ZONENAME_MAX)
781087719fdSdp 		return (Z_BOGUS_ZONE_NAME);
782087719fdSdp 
783087719fdSdp 	if (!((zone[0] >= 'a' && zone[0] <= 'z') ||
784087719fdSdp 	    (zone[0] >= 'A' && zone[0] <= 'Z') ||
785087719fdSdp 	    (zone[0] >= '0' && zone[0] <= '9')))
786087719fdSdp 		return (Z_BOGUS_ZONE_NAME);
787087719fdSdp 
788087719fdSdp 	for (i = 1; zone[i] != '\0'; i++) {
789087719fdSdp 		if (!((zone[i] >= 'a' && zone[i] <= 'z') ||
790087719fdSdp 		    (zone[i] >= 'A' && zone[i] <= 'Z') ||
791087719fdSdp 		    (zone[i] >= '0' && zone[i] <= '9') ||
792087719fdSdp 		    (zone[i] == '-') || (zone[i] == '_') || (zone[i] == '.')))
793087719fdSdp 			return (Z_BOGUS_ZONE_NAME);
794087719fdSdp 	}
795087719fdSdp 
796087719fdSdp 	return (Z_OK);
797087719fdSdp }
798087719fdSdp 
799087719fdSdp /*
800087719fdSdp  * Changing the zone name requires us to track both the old and new
801087719fdSdp  * name of the zone until commit time.
802087719fdSdp  */
8037c478bd9Sstevel@tonic-gate int
8047c478bd9Sstevel@tonic-gate zonecfg_get_name(zone_dochandle_t handle, char *name, size_t namesize)
8057c478bd9Sstevel@tonic-gate {
806a1be23daSdp 	return (getrootattr(handle, DTD_ATTR_NAME, name, namesize));
807a1be23daSdp }
8087c478bd9Sstevel@tonic-gate 
809a1be23daSdp int
810a1be23daSdp zonecfg_set_name(zone_dochandle_t handle, char *name)
811a1be23daSdp {
812087719fdSdp 	zone_state_t state;
813087719fdSdp 	char curname[ZONENAME_MAX], old_delname[ZONENAME_MAX];
814087719fdSdp 	int err;
815087719fdSdp 
816087719fdSdp 	if ((err = getrootattr(handle, DTD_ATTR_NAME, curname,
817087719fdSdp 	    sizeof (curname))) != Z_OK)
818087719fdSdp 		return (err);
819087719fdSdp 
820087719fdSdp 	if (strcmp(name, curname) == 0)
821087719fdSdp 		return (Z_OK);
822087719fdSdp 
823087719fdSdp 	/*
824087719fdSdp 	 * Switching zone names to one beginning with SUNW is not permitted.
825087719fdSdp 	 */
826087719fdSdp 	if (strncmp(name, "SUNW", 4) == 0)
827087719fdSdp 		return (Z_BOGUS_ZONE_NAME);
828087719fdSdp 
829087719fdSdp 	if ((err = zonecfg_validate_zonename(name)) != Z_OK)
830087719fdSdp 		return (err);
831087719fdSdp 
832087719fdSdp 	/*
833087719fdSdp 	 * Setting the name back to the original name (effectively a revert of
834087719fdSdp 	 * the name) is fine.  But if we carry on, we'll falsely identify the
835087719fdSdp 	 * name as "in use," so special case here.
836087719fdSdp 	 */
837087719fdSdp 	if (strcmp(name, handle->zone_dh_delete_name) == 0) {
838087719fdSdp 		err = setrootattr(handle, DTD_ATTR_NAME, name);
839087719fdSdp 		handle->zone_dh_delete_name[0] = '\0';
840087719fdSdp 		return (err);
841087719fdSdp 	}
842087719fdSdp 
843087719fdSdp 	/* Check to see if new name chosen is already in use */
844087719fdSdp 	if (zone_get_state(name, &state) != Z_NO_ZONE)
845087719fdSdp 		return (Z_NAME_IN_USE);
846087719fdSdp 
847087719fdSdp 	/*
848087719fdSdp 	 * If this isn't already "new" or in a renaming transition, then
849087719fdSdp 	 * we're initiating a rename here; so stash the "delete name"
850087719fdSdp 	 * (i.e. the name of the zone we'll be removing) for the rename.
851087719fdSdp 	 */
852087719fdSdp 	(void) strlcpy(old_delname, handle->zone_dh_delete_name,
853087719fdSdp 	    sizeof (old_delname));
854087719fdSdp 	if (!is_new(handle) && !is_renaming(handle)) {
855087719fdSdp 		/*
856087719fdSdp 		 * Name change is allowed only when the zone we're altering
857087719fdSdp 		 * is not ready or running.
858087719fdSdp 		 */
859087719fdSdp 		err = zone_get_state(curname, &state);
860087719fdSdp 		if (err == Z_OK) {
861087719fdSdp 			if (state > ZONE_STATE_INSTALLED)
862087719fdSdp 				return (Z_BAD_ZONE_STATE);
863087719fdSdp 		} else if (err != Z_NO_ZONE) {
864087719fdSdp 			return (err);
865087719fdSdp 		}
866087719fdSdp 
867087719fdSdp 		(void) strlcpy(handle->zone_dh_delete_name, curname,
868087719fdSdp 		    sizeof (handle->zone_dh_delete_name));
869087719fdSdp 		assert(is_renaming(handle));
870087719fdSdp 	} else if (is_renaming(handle)) {
871087719fdSdp 		err = zone_get_state(handle->zone_dh_delete_name, &state);
872087719fdSdp 		if (err == Z_OK) {
873087719fdSdp 			if (state > ZONE_STATE_INSTALLED)
874087719fdSdp 				return (Z_BAD_ZONE_STATE);
875087719fdSdp 		} else if (err != Z_NO_ZONE) {
876087719fdSdp 			return (err);
877087719fdSdp 		}
878087719fdSdp 	}
879087719fdSdp 
880087719fdSdp 	if ((err = setrootattr(handle, DTD_ATTR_NAME, name)) != Z_OK) {
881087719fdSdp 		/*
882087719fdSdp 		 * Restore the deletename to whatever it was at the
883087719fdSdp 		 * top of the routine, since we've had a failure.
884087719fdSdp 		 */
885087719fdSdp 		(void) strlcpy(handle->zone_dh_delete_name, old_delname,
886087719fdSdp 		    sizeof (handle->zone_dh_delete_name));
887087719fdSdp 		return (err);
888087719fdSdp 	}
889087719fdSdp 
890087719fdSdp 	return (Z_OK);
891a1be23daSdp }
8927c478bd9Sstevel@tonic-gate 
893a1be23daSdp int
894a1be23daSdp zonecfg_get_zonepath(zone_dochandle_t handle, char *path, size_t pathsize)
895a1be23daSdp {
896108322fbScarlsonj 	size_t len;
897108322fbScarlsonj 
898108322fbScarlsonj 	if ((len = strlcpy(path, zonecfg_root, pathsize)) >= pathsize)
899108322fbScarlsonj 		return (Z_TOO_BIG);
900108322fbScarlsonj 	return (getrootattr(handle, DTD_ATTR_ZONEPATH, path + len,
901108322fbScarlsonj 	    pathsize - len));
902a1be23daSdp }
9037c478bd9Sstevel@tonic-gate 
904a1be23daSdp int
905a1be23daSdp zonecfg_set_zonepath(zone_dochandle_t handle, char *zonepath)
906a1be23daSdp {
907555afedfScarlsonj 	size_t len;
908555afedfScarlsonj 
909555afedfScarlsonj 	/*
910555afedfScarlsonj 	 * The user deals in absolute paths in the running global zone, but the
911555afedfScarlsonj 	 * internal configuration files deal with boot environment relative
912555afedfScarlsonj 	 * paths.  Strip out the alternate root when specified.
913555afedfScarlsonj 	 */
914555afedfScarlsonj 	len = strlen(zonecfg_root);
915555afedfScarlsonj 	if (strncmp(zonepath, zonecfg_root, len) != 0 || zonepath[len] != '/')
916555afedfScarlsonj 		return (Z_BAD_PROPERTY);
917555afedfScarlsonj 	zonepath += len;
918a1be23daSdp 	return (setrootattr(handle, DTD_ATTR_ZONEPATH, zonepath));
919a1be23daSdp }
920a1be23daSdp 
9219acbbeafSnn int
9229acbbeafSnn zonecfg_get_brand(zone_dochandle_t handle, char *brand, size_t brandsize)
9239acbbeafSnn {
9249acbbeafSnn 	int ret, sz;
9259acbbeafSnn 
9269acbbeafSnn 	ret = getrootattr(handle, DTD_ATTR_BRAND, brand, brandsize);
9279acbbeafSnn 
9289acbbeafSnn 	/* If the zone has no brand, it is native. */
9299acbbeafSnn 	if (ret == Z_OK && brand[0] == '\0') {
9309acbbeafSnn 		sz = strlcpy(brand, NATIVE_BRAND_NAME, brandsize);
9319acbbeafSnn 		if (sz >= brandsize)
9329acbbeafSnn 			ret = Z_TOO_BIG;
9339acbbeafSnn 		else
9349acbbeafSnn 			ret = Z_OK;
9359acbbeafSnn 	}
9369acbbeafSnn 
9379acbbeafSnn 	return (ret);
9389acbbeafSnn }
9399acbbeafSnn 
9409acbbeafSnn int
9419acbbeafSnn zonecfg_set_brand(zone_dochandle_t handle, char *brand)
9429acbbeafSnn {
9439acbbeafSnn 	return (setrootattr(handle, DTD_ATTR_BRAND, brand));
9449acbbeafSnn }
9459acbbeafSnn 
946a1be23daSdp int
947a1be23daSdp zonecfg_get_autoboot(zone_dochandle_t handle, boolean_t *autoboot)
948a1be23daSdp {
949a1be23daSdp 	char autobootstr[DTD_ENTITY_BOOL_LEN];
950a1be23daSdp 	int ret;
951a1be23daSdp 
952a1be23daSdp 	if ((ret = getrootattr(handle, DTD_ATTR_AUTOBOOT, autobootstr,
953a1be23daSdp 	    sizeof (autobootstr))) != Z_OK)
954a1be23daSdp 		return (ret);
955a1be23daSdp 
956a1be23daSdp 	if (strcmp(autobootstr, DTD_ENTITY_TRUE) == 0)
957a1be23daSdp 		*autoboot = B_TRUE;
958a1be23daSdp 	else if (strcmp(autobootstr, DTD_ENTITY_FALSE) == 0)
959a1be23daSdp 		*autoboot = B_FALSE;
960a1be23daSdp 	else
961a1be23daSdp 		ret = Z_BAD_PROPERTY;
962a1be23daSdp 	return (ret);
963a1be23daSdp }
964a1be23daSdp 
965a1be23daSdp int
966a1be23daSdp zonecfg_set_autoboot(zone_dochandle_t handle, boolean_t autoboot)
967a1be23daSdp {
968a1be23daSdp 	return (setrootattr(handle, DTD_ATTR_AUTOBOOT,
969a1be23daSdp 	    autoboot ? DTD_ENTITY_TRUE : DTD_ENTITY_FALSE));
970a1be23daSdp }
971a1be23daSdp 
972a1be23daSdp int
973a1be23daSdp zonecfg_get_pool(zone_dochandle_t handle, char *pool, size_t poolsize)
974a1be23daSdp {
975a1be23daSdp 	return (getrootattr(handle, DTD_ATTR_POOL, pool, poolsize));
976a1be23daSdp }
977a1be23daSdp 
978a1be23daSdp int
979a1be23daSdp zonecfg_set_pool(zone_dochandle_t handle, char *pool)
980a1be23daSdp {
981a1be23daSdp 	return (setrootattr(handle, DTD_ATTR_POOL, pool));
982a1be23daSdp }
983a1be23daSdp 
984ffbafc53Scomay int
985ffbafc53Scomay zonecfg_get_limitpriv(zone_dochandle_t handle, char **limitpriv)
986ffbafc53Scomay {
987ffbafc53Scomay 	return (get_alloc_rootattr(handle, DTD_ATTR_LIMITPRIV, limitpriv));
988ffbafc53Scomay }
989ffbafc53Scomay 
990ffbafc53Scomay int
9913f2f09c1Sdp zonecfg_set_limitpriv(zone_dochandle_t handle, char *limitpriv)
992ffbafc53Scomay {
9933f2f09c1Sdp 	return (setrootattr(handle, DTD_ATTR_LIMITPRIV, limitpriv));
9943f2f09c1Sdp }
9953f2f09c1Sdp 
9963f2f09c1Sdp int
9973f2f09c1Sdp zonecfg_get_bootargs(zone_dochandle_t handle, char *bargs, size_t bargssize)
9983f2f09c1Sdp {
9993f2f09c1Sdp 	return (getrootattr(handle, DTD_ATTR_BOOTARGS, bargs, bargssize));
10003f2f09c1Sdp }
10013f2f09c1Sdp 
10023f2f09c1Sdp int
10033f2f09c1Sdp zonecfg_set_bootargs(zone_dochandle_t handle, char *bargs)
10043f2f09c1Sdp {
10053f2f09c1Sdp 	return (setrootattr(handle, DTD_ATTR_BOOTARGS, bargs));
1006ffbafc53Scomay }
1007ffbafc53Scomay 
1008*0209230bSgjelinek int
1009*0209230bSgjelinek zonecfg_get_sched_class(zone_dochandle_t handle, char *sched, size_t schedsize)
1010*0209230bSgjelinek {
1011*0209230bSgjelinek 	return (getrootattr(handle, DTD_ATTR_SCHED, sched, schedsize));
1012*0209230bSgjelinek }
1013*0209230bSgjelinek 
1014*0209230bSgjelinek int
1015*0209230bSgjelinek zonecfg_set_sched(zone_dochandle_t handle, char *sched)
1016*0209230bSgjelinek {
1017*0209230bSgjelinek 	return (setrootattr(handle, DTD_ATTR_SCHED, sched));
1018*0209230bSgjelinek }
1019*0209230bSgjelinek 
1020a1be23daSdp /*
1021a1be23daSdp  * /etc/zones/index caches a vital piece of information which is also
1022a1be23daSdp  * in the <zonename>.xml file: the path to the zone.  This is for performance,
1023a1be23daSdp  * since we need to walk all zonepath's in order to be able to detect conflicts
1024a1be23daSdp  * (see crosscheck_zonepaths() in the zoneadm command).
1025087719fdSdp  *
1026087719fdSdp  * An additional complexity is that when doing a rename, we'd like the entire
1027087719fdSdp  * index update operation (rename, and potential state changes) to be atomic.
1028087719fdSdp  * In general, the operation of this function should succeed or fail as
1029087719fdSdp  * a unit.
1030a1be23daSdp  */
1031a1be23daSdp int
1032a1be23daSdp zonecfg_refresh_index_file(zone_dochandle_t handle)
1033a1be23daSdp {
1034a1be23daSdp 	char name[ZONENAME_MAX], zonepath[MAXPATHLEN];
1035a1be23daSdp 	struct zoneent ze;
1036a1be23daSdp 	int err;
1037087719fdSdp 	int opcode;
1038087719fdSdp 	char *zn;
1039087719fdSdp 
1040087719fdSdp 	bzero(&ze, sizeof (ze));
1041087719fdSdp 	ze.zone_state = -1;	/* Preserve existing state in index */
1042a1be23daSdp 
1043a1be23daSdp 	if ((err = zonecfg_get_name(handle, name, sizeof (name))) != Z_OK)
1044a1be23daSdp 		return (err);
1045087719fdSdp 	(void) strlcpy(ze.zone_name, name, sizeof (ze.zone_name));
1046087719fdSdp 
1047a1be23daSdp 	if ((err = zonecfg_get_zonepath(handle, zonepath,
1048a1be23daSdp 	    sizeof (zonepath))) != Z_OK)
1049a1be23daSdp 		return (err);
1050555afedfScarlsonj 	(void) strlcpy(ze.zone_path, zonepath + strlen(zonecfg_root),
1051555afedfScarlsonj 	    sizeof (ze.zone_path));
1052087719fdSdp 
1053087719fdSdp 	if (is_renaming(handle)) {
1054087719fdSdp 		opcode = PZE_MODIFY;
1055087719fdSdp 		(void) strlcpy(ze.zone_name, handle->zone_dh_delete_name,
1056087719fdSdp 		    sizeof (ze.zone_name));
1057087719fdSdp 		(void) strlcpy(ze.zone_newname, name, sizeof (ze.zone_newname));
1058087719fdSdp 	} else if (is_new(handle)) {
1059087719fdSdp 		FILE *cookie;
1060087719fdSdp 		/*
1061087719fdSdp 		 * Be tolerant of the zone already existing in the index file,
1062087719fdSdp 		 * since we might be forcibly overwriting an existing
1063087719fdSdp 		 * configuration with a new one (for example 'create -F'
1064087719fdSdp 		 * in zonecfg).
1065087719fdSdp 		 */
1066087719fdSdp 		opcode = PZE_ADD;
1067087719fdSdp 		cookie = setzoneent();
1068087719fdSdp 		while ((zn = getzoneent(cookie)) != NULL) {
1069087719fdSdp 			if (strcmp(zn, name) == 0) {
1070087719fdSdp 				opcode = PZE_MODIFY;
1071087719fdSdp 				free(zn);
1072087719fdSdp 				break;
1073087719fdSdp 			}
1074087719fdSdp 			free(zn);
1075087719fdSdp 		}
1076087719fdSdp 		endzoneent(cookie);
1077087719fdSdp 		ze.zone_state = ZONE_STATE_CONFIGURED;
1078087719fdSdp 	} else {
1079087719fdSdp 		opcode = PZE_MODIFY;
1080087719fdSdp 	}
1081087719fdSdp 
1082087719fdSdp 	if ((err = putzoneent(&ze, opcode)) != Z_OK)
1083087719fdSdp 		return (err);
1084087719fdSdp 
1085087719fdSdp 	return (Z_OK);
10867c478bd9Sstevel@tonic-gate }
10877c478bd9Sstevel@tonic-gate 
1088087719fdSdp /*
1089087719fdSdp  * The goal of this routine is to cause the index file update and the
1090087719fdSdp  * document save to happen as an atomic operation.  We do the document
1091087719fdSdp  * first, saving a backup copy using a hard link; if that succeeds, we go
1092087719fdSdp  * on to the index.  If that fails, we roll the document back into place.
1093087719fdSdp  *
1094087719fdSdp  * Strategy:
1095087719fdSdp  *
1096087719fdSdp  * New zone 'foo' configuration:
1097087719fdSdp  * 	Create tmpfile (zonecfg.xxxxxx)
1098087719fdSdp  * 	Write XML to tmpfile
1099087719fdSdp  * 	Rename tmpfile to xmlfile (zonecfg.xxxxxx -> foo.xml)
1100087719fdSdp  * 	Add entry to index file
1101087719fdSdp  * 	If it fails, delete foo.xml, leaving nothing behind.
1102087719fdSdp  *
1103087719fdSdp  * Save existing zone 'foo':
1104087719fdSdp  * 	Make backup of foo.xml -> .backup
1105087719fdSdp  * 	Create tmpfile (zonecfg.xxxxxx)
1106087719fdSdp  * 	Write XML to tmpfile
1107087719fdSdp  * 	Rename tmpfile to xmlfile (zonecfg.xxxxxx -> foo.xml)
1108087719fdSdp  * 	Modify index file as needed
1109087719fdSdp  * 	If it fails, recover from .backup -> foo.xml
1110087719fdSdp  *
1111087719fdSdp  * Rename 'foo' to 'bar':
1112087719fdSdp  * 	Create tmpfile (zonecfg.xxxxxx)
1113087719fdSdp  * 	Write XML to tmpfile
1114087719fdSdp  * 	Rename tmpfile to xmlfile (zonecfg.xxxxxx -> bar.xml)
1115087719fdSdp  * 	Add entry for 'bar' to index file, Remove entry for 'foo' (refresh)
1116087719fdSdp  * 	If it fails, delete bar.xml; foo.xml is left behind.
1117087719fdSdp  */
11187c478bd9Sstevel@tonic-gate static int
11197c478bd9Sstevel@tonic-gate zonecfg_save_impl(zone_dochandle_t handle, char *filename)
11207c478bd9Sstevel@tonic-gate {
11217c478bd9Sstevel@tonic-gate 	char tmpfile[MAXPATHLEN];
1122087719fdSdp 	char bakdir[MAXPATHLEN], bakbase[MAXPATHLEN], bakfile[MAXPATHLEN];
11239acbbeafSnn 	int tmpfd, err, valid;
11247c478bd9Sstevel@tonic-gate 	xmlValidCtxt cvp = { NULL };
1125087719fdSdp 	boolean_t backup;
11267c478bd9Sstevel@tonic-gate 
11277c478bd9Sstevel@tonic-gate 	(void) strlcpy(tmpfile, filename, sizeof (tmpfile));
11287c478bd9Sstevel@tonic-gate 	(void) dirname(tmpfile);
11297c478bd9Sstevel@tonic-gate 	(void) strlcat(tmpfile, _PATH_TMPFILE, sizeof (tmpfile));
11307c478bd9Sstevel@tonic-gate 
11317c478bd9Sstevel@tonic-gate 	tmpfd = mkstemp(tmpfile);
11327c478bd9Sstevel@tonic-gate 	if (tmpfd == -1) {
11337c478bd9Sstevel@tonic-gate 		(void) unlink(tmpfile);
11347c478bd9Sstevel@tonic-gate 		return (Z_TEMP_FILE);
11357c478bd9Sstevel@tonic-gate 	}
11367c478bd9Sstevel@tonic-gate 	(void) close(tmpfd);
11377c478bd9Sstevel@tonic-gate 
11387c478bd9Sstevel@tonic-gate 	cvp.error = zonecfg_error_func;
11397c478bd9Sstevel@tonic-gate 	cvp.warning = zonecfg_error_func;
11407c478bd9Sstevel@tonic-gate 
1141087719fdSdp 	/*
11429acbbeafSnn 	 * We do a final validation of the document.  Since the library has
11439acbbeafSnn 	 * malfunctioned if it fails to validate, we follow-up with an
11449acbbeafSnn 	 * assert() that the doc is valid.
1145087719fdSdp 	 */
11469acbbeafSnn 	valid = xmlValidateDocument(&cvp, handle->zone_dh_doc);
11479acbbeafSnn 	assert(valid != 0);
11487c478bd9Sstevel@tonic-gate 
11497c478bd9Sstevel@tonic-gate 	if (xmlSaveFormatFile(tmpfile, handle->zone_dh_doc, 1) <= 0)
11507c478bd9Sstevel@tonic-gate 		goto err;
1151087719fdSdp 
11527c478bd9Sstevel@tonic-gate 	(void) chmod(tmpfile, 0644);
11537c478bd9Sstevel@tonic-gate 
1154087719fdSdp 	/*
1155087719fdSdp 	 * In the event we are doing a standard save, hard link a copy of the
1156087719fdSdp 	 * original file in .backup.<pid>.filename so we can restore it if
1157087719fdSdp 	 * something goes wrong.
1158087719fdSdp 	 */
1159087719fdSdp 	if (!is_new(handle) && !is_renaming(handle)) {
1160087719fdSdp 		backup = B_TRUE;
1161087719fdSdp 
1162087719fdSdp 		(void) strlcpy(bakdir, filename, sizeof (bakdir));
1163087719fdSdp 		(void) strlcpy(bakbase, filename, sizeof (bakbase));
1164087719fdSdp 		(void) snprintf(bakfile, sizeof (bakfile), "%s/.backup.%d.%s",
1165087719fdSdp 		    dirname(bakdir), getpid(), basename(bakbase));
1166087719fdSdp 
1167087719fdSdp 		if (link(filename, bakfile) == -1) {
1168087719fdSdp 			err = errno;
1169087719fdSdp 			(void) unlink(tmpfile);
1170087719fdSdp 			if (errno == EACCES)
1171087719fdSdp 				return (Z_ACCES);
1172087719fdSdp 			return (Z_MISC_FS);
1173087719fdSdp 		}
1174087719fdSdp 	}
1175087719fdSdp 
1176087719fdSdp 	/*
1177087719fdSdp 	 * Move the new document over top of the old.
1178087719fdSdp 	 * i.e.:   zonecfg.XXXXXX  ->  myzone.xml
1179087719fdSdp 	 */
11807c478bd9Sstevel@tonic-gate 	if (rename(tmpfile, filename) == -1) {
1181087719fdSdp 		err = errno;
11827c478bd9Sstevel@tonic-gate 		(void) unlink(tmpfile);
1183087719fdSdp 		if (backup)
1184087719fdSdp 			(void) unlink(bakfile);
1185087719fdSdp 		if (err == EACCES)
11867c478bd9Sstevel@tonic-gate 			return (Z_ACCES);
11877c478bd9Sstevel@tonic-gate 		return (Z_MISC_FS);
11887c478bd9Sstevel@tonic-gate 	}
1189a1be23daSdp 
1190087719fdSdp 	/*
1191087719fdSdp 	 * If this is a snapshot, we're done-- don't add an index entry.
1192087719fdSdp 	 */
1193087719fdSdp 	if (is_snapshot(handle))
1194087719fdSdp 		return (Z_OK);
1195087719fdSdp 
1196087719fdSdp 	/* now update the index file to reflect whatever we just did */
1197087719fdSdp 	if ((err = zonecfg_refresh_index_file(handle)) != Z_OK) {
1198087719fdSdp 		if (backup) {
1199087719fdSdp 			/*
1200087719fdSdp 			 * Try to restore from our backup.
1201087719fdSdp 			 */
1202087719fdSdp 			(void) unlink(filename);
1203087719fdSdp 			(void) rename(bakfile, filename);
1204087719fdSdp 		} else {
1205087719fdSdp 			/*
1206087719fdSdp 			 * Either the zone is new, in which case we can delete
1207087719fdSdp 			 * new.xml, or we're doing a rename, so ditto.
1208087719fdSdp 			 */
1209087719fdSdp 			assert(is_new(handle) || is_renaming(handle));
1210087719fdSdp 			(void) unlink(filename);
1211087719fdSdp 		}
1212087719fdSdp 		return (Z_UPDATING_INDEX);
1213087719fdSdp 	}
1214087719fdSdp 
1215087719fdSdp 	if (backup)
1216087719fdSdp 		(void) unlink(bakfile);
1217087719fdSdp 
1218087719fdSdp 	return (Z_OK);
12197c478bd9Sstevel@tonic-gate 
12207c478bd9Sstevel@tonic-gate err:
12217c478bd9Sstevel@tonic-gate 	(void) unlink(tmpfile);
12227c478bd9Sstevel@tonic-gate 	return (Z_SAVING_FILE);
12237c478bd9Sstevel@tonic-gate }
12247c478bd9Sstevel@tonic-gate 
12257c478bd9Sstevel@tonic-gate int
12267c478bd9Sstevel@tonic-gate zonecfg_save(zone_dochandle_t handle)
12277c478bd9Sstevel@tonic-gate {
1228087719fdSdp 	char zname[ZONENAME_MAX], path[MAXPATHLEN];
1229087719fdSdp 	char delpath[MAXPATHLEN];
1230087719fdSdp 	int err = Z_SAVING_FILE;
1231087719fdSdp 
1232087719fdSdp 	if (zonecfg_check_handle(handle) != Z_OK)
1233087719fdSdp 		return (Z_BAD_HANDLE);
12347c478bd9Sstevel@tonic-gate 
1235087719fdSdp 	/*
1236ee519a1fSgjelinek 	 * We don't support saving snapshots or a tree containing a sw
1237ee519a1fSgjelinek 	 * inventory at this time.
1238087719fdSdp 	 */
1239ee519a1fSgjelinek 	if (handle->zone_dh_snapshot || handle->zone_dh_sw_inv)
1240087719fdSdp 		return (Z_INVAL);
1241087719fdSdp 
1242087719fdSdp 	if ((err = zonecfg_get_name(handle, zname, sizeof (zname))) != Z_OK)
12437c478bd9Sstevel@tonic-gate 		return (err);
1244087719fdSdp 
1245108322fbScarlsonj 	if (!config_file_path(zname, path))
1246108322fbScarlsonj 		return (Z_MISC_FS);
1247087719fdSdp 
1248087719fdSdp 	addcomment(handle, "\n    DO NOT EDIT THIS "
1249087719fdSdp 	    "FILE.  Use zonecfg(1M) instead.\n");
1250087719fdSdp 
1251087719fdSdp 	err = zonecfg_save_impl(handle, path);
1252087719fdSdp 
1253087719fdSdp 	stripcomments(handle);
1254087719fdSdp 
1255087719fdSdp 	if (err != Z_OK)
1256087719fdSdp 		return (err);
1257087719fdSdp 
1258087719fdSdp 	handle->zone_dh_newzone = B_FALSE;
1259087719fdSdp 
1260087719fdSdp 	if (is_renaming(handle)) {
1261108322fbScarlsonj 		if (config_file_path(handle->zone_dh_delete_name, delpath))
1262108322fbScarlsonj 			(void) unlink(delpath);
1263087719fdSdp 		handle->zone_dh_delete_name[0] = '\0';
1264087719fdSdp 	}
1265087719fdSdp 
1266087719fdSdp 	return (Z_OK);
12677c478bd9Sstevel@tonic-gate }
12687c478bd9Sstevel@tonic-gate 
12699acbbeafSnn int
12709acbbeafSnn zonecfg_verify_save(zone_dochandle_t handle, char *filename)
12719acbbeafSnn {
12729acbbeafSnn 	int valid;
12739acbbeafSnn 
12749acbbeafSnn 	xmlValidCtxt cvp = { NULL };
12759acbbeafSnn 
12769acbbeafSnn 	if (zonecfg_check_handle(handle) != Z_OK)
12779acbbeafSnn 		return (Z_BAD_HANDLE);
12789acbbeafSnn 
12799acbbeafSnn 	cvp.error = zonecfg_error_func;
12809acbbeafSnn 	cvp.warning = zonecfg_error_func;
12819acbbeafSnn 
12829acbbeafSnn 	/*
12839acbbeafSnn 	 * We do a final validation of the document.  Since the library has
12849acbbeafSnn 	 * malfunctioned if it fails to validate, we follow-up with an
12859acbbeafSnn 	 * assert() that the doc is valid.
12869acbbeafSnn 	 */
12879acbbeafSnn 	valid = xmlValidateDocument(&cvp, handle->zone_dh_doc);
12889acbbeafSnn 	assert(valid != 0);
12899acbbeafSnn 
12909acbbeafSnn 	if (xmlSaveFormatFile(filename, handle->zone_dh_doc, 1) <= 0)
12919acbbeafSnn 		return (Z_SAVING_FILE);
12929acbbeafSnn 
12939acbbeafSnn 	return (Z_OK);
12949acbbeafSnn }
12959acbbeafSnn 
1296ee519a1fSgjelinek int
12978cd327d5Sgjelinek zonecfg_detach_save(zone_dochandle_t handle, uint_t flags)
1298ee519a1fSgjelinek {
1299ee519a1fSgjelinek 	char zname[ZONENAME_MAX];
1300ee519a1fSgjelinek 	char path[MAXPATHLEN];
1301ee519a1fSgjelinek 	char migpath[MAXPATHLEN];
1302ee519a1fSgjelinek 	xmlValidCtxt cvp = { NULL };
1303ee519a1fSgjelinek 	int err = Z_SAVING_FILE;
13049acbbeafSnn 	int valid;
1305ee519a1fSgjelinek 
1306ee519a1fSgjelinek 	if (zonecfg_check_handle(handle) != Z_OK)
1307ee519a1fSgjelinek 		return (Z_BAD_HANDLE);
1308ee519a1fSgjelinek 
1309ee519a1fSgjelinek 	/*
1310ee519a1fSgjelinek 	 * We can only detach if we have taken a sw inventory.
1311ee519a1fSgjelinek 	 */
1312ee519a1fSgjelinek 	if (!handle->zone_dh_sw_inv)
1313ee519a1fSgjelinek 		return (Z_INVAL);
1314ee519a1fSgjelinek 
13158cd327d5Sgjelinek 	if (flags & ZONE_DRY_RUN) {
13168cd327d5Sgjelinek 		(void) strlcpy(migpath, "-", sizeof (migpath));
13178cd327d5Sgjelinek 	} else {
13188cd327d5Sgjelinek 		if ((err = zonecfg_get_name(handle, zname, sizeof (zname)))
13198cd327d5Sgjelinek 		    != Z_OK)
13208cd327d5Sgjelinek 			return (err);
1321ee519a1fSgjelinek 
13228cd327d5Sgjelinek 		if ((err = zone_get_zonepath(zname, path, sizeof (path)))
13238cd327d5Sgjelinek 		    != Z_OK)
13248cd327d5Sgjelinek 			return (err);
1325ee519a1fSgjelinek 
13268cd327d5Sgjelinek 		if (snprintf(migpath, sizeof (migpath), "%s/%s", path, DETACHED)
13278cd327d5Sgjelinek 		    >= sizeof (migpath))
13288cd327d5Sgjelinek 			return (Z_NOMEM);
13298cd327d5Sgjelinek 	}
1330ee519a1fSgjelinek 
1331ee519a1fSgjelinek 	if ((err = operation_prep(handle)) != Z_OK)
1332ee519a1fSgjelinek 		return (err);
1333ee519a1fSgjelinek 
1334ee519a1fSgjelinek 	addcomment(handle, "\n    DO NOT EDIT THIS FILE.  "
1335ee519a1fSgjelinek 	    "Use zonecfg(1M) and zoneadm(1M) attach.\n");
1336ee519a1fSgjelinek 
1337ee519a1fSgjelinek 	cvp.error = zonecfg_error_func;
1338ee519a1fSgjelinek 	cvp.warning = zonecfg_error_func;
1339ee519a1fSgjelinek 
1340ee519a1fSgjelinek 	/*
13419acbbeafSnn 	 * We do a final validation of the document.  Since the library has
13429acbbeafSnn 	 * malfunctioned if it fails to validate, we follow-up with an
13439acbbeafSnn 	 * assert() that the doc is valid.
1344ee519a1fSgjelinek 	 */
13459acbbeafSnn 	valid = xmlValidateDocument(&cvp, handle->zone_dh_doc);
13469acbbeafSnn 	assert(valid != 0);
1347ee519a1fSgjelinek 
1348ee519a1fSgjelinek 	if (xmlSaveFormatFile(migpath, handle->zone_dh_doc, 1) <= 0)
1349ee519a1fSgjelinek 		return (Z_SAVING_FILE);
1350ee519a1fSgjelinek 
13518cd327d5Sgjelinek 	if (!(flags & ZONE_DRY_RUN))
13528cd327d5Sgjelinek 		(void) chmod(migpath, 0644);
1353ee519a1fSgjelinek 
1354ee519a1fSgjelinek 	stripcomments(handle);
1355ee519a1fSgjelinek 
1356ee519a1fSgjelinek 	handle->zone_dh_newzone = B_FALSE;
1357ee519a1fSgjelinek 
1358ee519a1fSgjelinek 	return (Z_OK);
1359ee519a1fSgjelinek }
1360ee519a1fSgjelinek 
1361ee519a1fSgjelinek boolean_t
1362ee519a1fSgjelinek zonecfg_detached(const char *path)
1363ee519a1fSgjelinek {
1364ee519a1fSgjelinek 	char		migpath[MAXPATHLEN];
1365ee519a1fSgjelinek 	struct stat	buf;
1366ee519a1fSgjelinek 
1367ee519a1fSgjelinek 	if (snprintf(migpath, sizeof (migpath), "%s/%s", path, DETACHED) >=
1368ee519a1fSgjelinek 	    sizeof (migpath))
1369ee519a1fSgjelinek 		return (B_FALSE);
1370ee519a1fSgjelinek 
1371ee519a1fSgjelinek 	if (stat(migpath, &buf) != -1)
1372ee519a1fSgjelinek 		return (B_TRUE);
1373ee519a1fSgjelinek 
1374ee519a1fSgjelinek 	return (B_FALSE);
1375ee519a1fSgjelinek }
1376ee519a1fSgjelinek 
1377ee519a1fSgjelinek void
1378ee519a1fSgjelinek zonecfg_rm_detached(zone_dochandle_t handle, boolean_t forced)
1379ee519a1fSgjelinek {
1380ee519a1fSgjelinek 	char zname[ZONENAME_MAX];
1381ee519a1fSgjelinek 	char path[MAXPATHLEN];
1382ee519a1fSgjelinek 	char detached[MAXPATHLEN];
1383ee519a1fSgjelinek 	char attached[MAXPATHLEN];
1384ee519a1fSgjelinek 
1385ee519a1fSgjelinek 	if (zonecfg_check_handle(handle) != Z_OK)
1386ee519a1fSgjelinek 		return;
1387ee519a1fSgjelinek 
1388ee519a1fSgjelinek 	if (zonecfg_get_name(handle, zname, sizeof (zname)) != Z_OK)
1389ee519a1fSgjelinek 		return;
1390ee519a1fSgjelinek 
1391ee519a1fSgjelinek 	if (zone_get_zonepath(zname, path, sizeof (path)) != Z_OK)
1392ee519a1fSgjelinek 		return;
1393ee519a1fSgjelinek 
1394ee519a1fSgjelinek 	(void) snprintf(detached, sizeof (detached), "%s/%s", path, DETACHED);
1395ee519a1fSgjelinek 	(void) snprintf(attached, sizeof (attached), "%s/%s", path,
1396ee519a1fSgjelinek 	    ATTACH_FORCED);
1397ee519a1fSgjelinek 
1398ee519a1fSgjelinek 	if (forced) {
1399ee519a1fSgjelinek 		(void) rename(detached, attached);
1400ee519a1fSgjelinek 	} else {
1401ee519a1fSgjelinek 		(void) unlink(attached);
1402ee519a1fSgjelinek 		(void) unlink(detached);
1403ee519a1fSgjelinek 	}
1404ee519a1fSgjelinek }
1405ee519a1fSgjelinek 
14067c478bd9Sstevel@tonic-gate /*
14077c478bd9Sstevel@tonic-gate  * Special case: if access(2) fails with ENOENT, then try again using
14087c478bd9Sstevel@tonic-gate  * ZONE_CONFIG_ROOT instead of config_file_path(zonename).  This is how we
14097c478bd9Sstevel@tonic-gate  * work around the case of a config file which has not been created yet:
14107c478bd9Sstevel@tonic-gate  * the user will need access to the directory so use that as a heuristic.
14117c478bd9Sstevel@tonic-gate  */
14127c478bd9Sstevel@tonic-gate 
14137c478bd9Sstevel@tonic-gate int
14147c478bd9Sstevel@tonic-gate zonecfg_access(const char *zonename, int amode)
14157c478bd9Sstevel@tonic-gate {
14167c478bd9Sstevel@tonic-gate 	char path[MAXPATHLEN];
14177c478bd9Sstevel@tonic-gate 
1418108322fbScarlsonj 	if (!config_file_path(zonename, path))
1419108322fbScarlsonj 		return (Z_INVAL);
14207c478bd9Sstevel@tonic-gate 	if (access(path, amode) == 0)
14217c478bd9Sstevel@tonic-gate 		return (Z_OK);
1422108322fbScarlsonj 	if (errno == ENOENT) {
1423108322fbScarlsonj 		if (snprintf(path, sizeof (path), "%s%s", zonecfg_root,
1424108322fbScarlsonj 		    ZONE_CONFIG_ROOT) >= sizeof (path))
1425108322fbScarlsonj 			return (Z_INVAL);
1426108322fbScarlsonj 		if (access(path, amode) == 0)
1427108322fbScarlsonj 			return (Z_OK);
1428108322fbScarlsonj 	}
14297c478bd9Sstevel@tonic-gate 	if (errno == EACCES)
14307c478bd9Sstevel@tonic-gate 		return (Z_ACCES);
14317c478bd9Sstevel@tonic-gate 	if (errno == EINVAL)
14327c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
14337c478bd9Sstevel@tonic-gate 	return (Z_MISC_FS);
14347c478bd9Sstevel@tonic-gate }
14357c478bd9Sstevel@tonic-gate 
14367c478bd9Sstevel@tonic-gate int
1437108322fbScarlsonj zonecfg_create_snapshot(const char *zonename)
14387c478bd9Sstevel@tonic-gate {
14397c478bd9Sstevel@tonic-gate 	zone_dochandle_t handle;
14407c478bd9Sstevel@tonic-gate 	char path[MAXPATHLEN], zonepath[MAXPATHLEN], rpath[MAXPATHLEN];
14417c478bd9Sstevel@tonic-gate 	int error = Z_OK, res;
14427c478bd9Sstevel@tonic-gate 
14437c478bd9Sstevel@tonic-gate 	if ((handle = zonecfg_init_handle()) == NULL) {
14447c478bd9Sstevel@tonic-gate 		return (Z_NOMEM);
14457c478bd9Sstevel@tonic-gate 	}
14467c478bd9Sstevel@tonic-gate 
1447087719fdSdp 	handle->zone_dh_newzone = B_TRUE;
1448087719fdSdp 	handle->zone_dh_snapshot = B_TRUE;
1449087719fdSdp 
14507c478bd9Sstevel@tonic-gate 	if ((error = zonecfg_get_handle(zonename, handle)) != Z_OK)
14517c478bd9Sstevel@tonic-gate 		goto out;
14527c478bd9Sstevel@tonic-gate 	if ((error = operation_prep(handle)) != Z_OK)
14537c478bd9Sstevel@tonic-gate 		goto out;
14547c478bd9Sstevel@tonic-gate 	error = zonecfg_get_zonepath(handle, zonepath, sizeof (zonepath));
14557c478bd9Sstevel@tonic-gate 	if (error != Z_OK)
14567c478bd9Sstevel@tonic-gate 		goto out;
14577c478bd9Sstevel@tonic-gate 	if ((res = resolvepath(zonepath, rpath, sizeof (rpath))) == -1) {
14587c478bd9Sstevel@tonic-gate 		error = Z_RESOLVED_PATH;
14597c478bd9Sstevel@tonic-gate 		goto out;
14607c478bd9Sstevel@tonic-gate 	}
14617c478bd9Sstevel@tonic-gate 	/*
14627c478bd9Sstevel@tonic-gate 	 * If the resolved path is not the same as the original path, then
14637c478bd9Sstevel@tonic-gate 	 * save the resolved path in the snapshot, thus preventing any
14647c478bd9Sstevel@tonic-gate 	 * potential problems down the line when zoneadmd goes to unmount
14657c478bd9Sstevel@tonic-gate 	 * file systems and depends on initial string matches with resolved
14667c478bd9Sstevel@tonic-gate 	 * paths.
14677c478bd9Sstevel@tonic-gate 	 */
14687c478bd9Sstevel@tonic-gate 	rpath[res] = '\0';
14697c478bd9Sstevel@tonic-gate 	if (strcmp(zonepath, rpath) != 0) {
14707c478bd9Sstevel@tonic-gate 		if ((error = zonecfg_set_zonepath(handle, rpath)) != Z_OK)
14717c478bd9Sstevel@tonic-gate 			goto out;
14727c478bd9Sstevel@tonic-gate 	}
1473108322fbScarlsonj 	if (snprintf(path, sizeof (path), "%s%s", zonecfg_root,
1474108322fbScarlsonj 	    ZONE_SNAPSHOT_ROOT) >= sizeof (path)) {
1475108322fbScarlsonj 		error = Z_MISC_FS;
1476108322fbScarlsonj 		goto out;
1477108322fbScarlsonj 	}
1478108322fbScarlsonj 	if ((mkdir(path, S_IRWXU) == -1) && (errno != EEXIST)) {
14797c478bd9Sstevel@tonic-gate 		error = Z_MISC_FS;
14807c478bd9Sstevel@tonic-gate 		goto out;
14817c478bd9Sstevel@tonic-gate 	}
14827c478bd9Sstevel@tonic-gate 
1483108322fbScarlsonj 	if (!snap_file_path(zonename, path)) {
1484108322fbScarlsonj 		error = Z_MISC_FS;
1485108322fbScarlsonj 		goto out;
1486108322fbScarlsonj 	}
1487087719fdSdp 
1488087719fdSdp 	addcomment(handle, "\n    DO NOT EDIT THIS FILE.  "
1489087719fdSdp 	    "It is a snapshot of running zone state.\n");
1490087719fdSdp 
14917c478bd9Sstevel@tonic-gate 	error = zonecfg_save_impl(handle, path);
14927c478bd9Sstevel@tonic-gate 
1493087719fdSdp 	stripcomments(handle);
1494087719fdSdp 
14957c478bd9Sstevel@tonic-gate out:
14967c478bd9Sstevel@tonic-gate 	zonecfg_fini_handle(handle);
14977c478bd9Sstevel@tonic-gate 	return (error);
14987c478bd9Sstevel@tonic-gate }
14997c478bd9Sstevel@tonic-gate 
15007c478bd9Sstevel@tonic-gate static int
1501a1be23daSdp newprop(xmlNodePtr node, const xmlChar *attrname, char *src)
15027c478bd9Sstevel@tonic-gate {
15037c478bd9Sstevel@tonic-gate 	xmlAttrPtr newattr;
15047c478bd9Sstevel@tonic-gate 
15057c478bd9Sstevel@tonic-gate 	newattr = xmlNewProp(node, attrname, (xmlChar *)src);
15067c478bd9Sstevel@tonic-gate 	if (newattr == NULL) {
15077c478bd9Sstevel@tonic-gate 		xmlUnlinkNode(node);
15087c478bd9Sstevel@tonic-gate 		xmlFreeNode(node);
15097c478bd9Sstevel@tonic-gate 		return (Z_BAD_PROPERTY);
15107c478bd9Sstevel@tonic-gate 	}
15117c478bd9Sstevel@tonic-gate 	return (Z_OK);
15127c478bd9Sstevel@tonic-gate }
15137c478bd9Sstevel@tonic-gate 
15147c478bd9Sstevel@tonic-gate static int
15157c478bd9Sstevel@tonic-gate zonecfg_add_filesystem_core(zone_dochandle_t handle, struct zone_fstab *tabptr)
15167c478bd9Sstevel@tonic-gate {
15177c478bd9Sstevel@tonic-gate 	xmlNodePtr newnode, cur = handle->zone_dh_cur, options_node;
15187c478bd9Sstevel@tonic-gate 	zone_fsopt_t *ptr;
15197c478bd9Sstevel@tonic-gate 	int err;
15207c478bd9Sstevel@tonic-gate 
15217c478bd9Sstevel@tonic-gate 	newnode = xmlNewTextChild(cur, NULL, DTD_ELEM_FS, NULL);
1522a1be23daSdp 	if ((err = newprop(newnode, DTD_ATTR_SPECIAL,
15237c478bd9Sstevel@tonic-gate 	    tabptr->zone_fs_special)) != Z_OK)
15247c478bd9Sstevel@tonic-gate 		return (err);
15257c478bd9Sstevel@tonic-gate 	if (tabptr->zone_fs_raw[0] != '\0' &&
1526a1be23daSdp 	    (err = newprop(newnode, DTD_ATTR_RAW, tabptr->zone_fs_raw)) != Z_OK)
15277c478bd9Sstevel@tonic-gate 		return (err);
1528a1be23daSdp 	if ((err = newprop(newnode, DTD_ATTR_DIR, tabptr->zone_fs_dir)) != Z_OK)
15297c478bd9Sstevel@tonic-gate 		return (err);
1530a1be23daSdp 	if ((err = newprop(newnode, DTD_ATTR_TYPE,
15317c478bd9Sstevel@tonic-gate 	    tabptr->zone_fs_type)) != Z_OK)
15327c478bd9Sstevel@tonic-gate 		return (err);
15337c478bd9Sstevel@tonic-gate 	if (tabptr->zone_fs_options != NULL) {
15347c478bd9Sstevel@tonic-gate 		for (ptr = tabptr->zone_fs_options; ptr != NULL;
15357c478bd9Sstevel@tonic-gate 		    ptr = ptr->zone_fsopt_next) {
15367c478bd9Sstevel@tonic-gate 			options_node = xmlNewTextChild(newnode, NULL,
15377c478bd9Sstevel@tonic-gate 			    DTD_ELEM_FSOPTION, NULL);
1538a1be23daSdp 			if ((err = newprop(options_node, DTD_ATTR_NAME,
15397c478bd9Sstevel@tonic-gate 			    ptr->zone_fsopt_opt)) != Z_OK)
15407c478bd9Sstevel@tonic-gate 				return (err);
15417c478bd9Sstevel@tonic-gate 		}
15427c478bd9Sstevel@tonic-gate 	}
15437c478bd9Sstevel@tonic-gate 	return (Z_OK);
15447c478bd9Sstevel@tonic-gate }
15457c478bd9Sstevel@tonic-gate 
15467c478bd9Sstevel@tonic-gate int
15477c478bd9Sstevel@tonic-gate zonecfg_add_filesystem(zone_dochandle_t handle, struct zone_fstab *tabptr)
15487c478bd9Sstevel@tonic-gate {
15497c478bd9Sstevel@tonic-gate 	int err;
15507c478bd9Sstevel@tonic-gate 
15517c478bd9Sstevel@tonic-gate 	if (tabptr == NULL)
15527c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
15537c478bd9Sstevel@tonic-gate 
15547c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
15557c478bd9Sstevel@tonic-gate 		return (err);
15567c478bd9Sstevel@tonic-gate 
15577c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_add_filesystem_core(handle, tabptr)) != Z_OK)
15587c478bd9Sstevel@tonic-gate 		return (err);
15597c478bd9Sstevel@tonic-gate 
15607c478bd9Sstevel@tonic-gate 	return (Z_OK);
15617c478bd9Sstevel@tonic-gate }
15627c478bd9Sstevel@tonic-gate 
15637c478bd9Sstevel@tonic-gate static int
15647c478bd9Sstevel@tonic-gate zonecfg_add_ipd_core(zone_dochandle_t handle, struct zone_fstab *tabptr)
15657c478bd9Sstevel@tonic-gate {
15667c478bd9Sstevel@tonic-gate 	xmlNodePtr newnode, cur = handle->zone_dh_cur;
15677c478bd9Sstevel@tonic-gate 	int err;
15687c478bd9Sstevel@tonic-gate 
15697c478bd9Sstevel@tonic-gate 	newnode = xmlNewTextChild(cur, NULL, DTD_ELEM_IPD, NULL);
1570a1be23daSdp 	if ((err = newprop(newnode, DTD_ATTR_DIR, tabptr->zone_fs_dir)) != Z_OK)
15717c478bd9Sstevel@tonic-gate 		return (err);
15727c478bd9Sstevel@tonic-gate 	return (Z_OK);
15737c478bd9Sstevel@tonic-gate }
15747c478bd9Sstevel@tonic-gate 
15757c478bd9Sstevel@tonic-gate int
15767c478bd9Sstevel@tonic-gate zonecfg_add_ipd(zone_dochandle_t handle, struct zone_fstab *tabptr)
15777c478bd9Sstevel@tonic-gate {
15787c478bd9Sstevel@tonic-gate 	int err;
15797c478bd9Sstevel@tonic-gate 
15807c478bd9Sstevel@tonic-gate 	if (tabptr == NULL)
15817c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
15827c478bd9Sstevel@tonic-gate 
15837c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
15847c478bd9Sstevel@tonic-gate 		return (err);
15857c478bd9Sstevel@tonic-gate 
15867c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_add_ipd_core(handle, tabptr)) != Z_OK)
15877c478bd9Sstevel@tonic-gate 		return (err);
15887c478bd9Sstevel@tonic-gate 
15897c478bd9Sstevel@tonic-gate 	return (Z_OK);
15907c478bd9Sstevel@tonic-gate }
15917c478bd9Sstevel@tonic-gate 
15927c478bd9Sstevel@tonic-gate int
15937c478bd9Sstevel@tonic-gate zonecfg_add_fs_option(struct zone_fstab *tabptr, char *option)
15947c478bd9Sstevel@tonic-gate {
15957c478bd9Sstevel@tonic-gate 	zone_fsopt_t *last, *old, *new;
15967c478bd9Sstevel@tonic-gate 
15977c478bd9Sstevel@tonic-gate 	last = tabptr->zone_fs_options;
15987c478bd9Sstevel@tonic-gate 	for (old = last; old != NULL; old = old->zone_fsopt_next)
15997c478bd9Sstevel@tonic-gate 		last = old;	/* walk to the end of the list */
16007c478bd9Sstevel@tonic-gate 	new = (zone_fsopt_t *)malloc(sizeof (zone_fsopt_t));
16017c478bd9Sstevel@tonic-gate 	if (new == NULL)
16027c478bd9Sstevel@tonic-gate 		return (Z_NOMEM);
16037c478bd9Sstevel@tonic-gate 	(void) strlcpy(new->zone_fsopt_opt, option,
16047c478bd9Sstevel@tonic-gate 	    sizeof (new->zone_fsopt_opt));
16057c478bd9Sstevel@tonic-gate 	new->zone_fsopt_next = NULL;
16067c478bd9Sstevel@tonic-gate 	if (last == NULL)
16077c478bd9Sstevel@tonic-gate 		tabptr->zone_fs_options = new;
16087c478bd9Sstevel@tonic-gate 	else
16097c478bd9Sstevel@tonic-gate 		last->zone_fsopt_next = new;
16107c478bd9Sstevel@tonic-gate 	return (Z_OK);
16117c478bd9Sstevel@tonic-gate }
16127c478bd9Sstevel@tonic-gate 
16137c478bd9Sstevel@tonic-gate int
16147c478bd9Sstevel@tonic-gate zonecfg_remove_fs_option(struct zone_fstab *tabptr, char *option)
16157c478bd9Sstevel@tonic-gate {
16167c478bd9Sstevel@tonic-gate 	zone_fsopt_t *last, *this, *next;
16177c478bd9Sstevel@tonic-gate 
16187c478bd9Sstevel@tonic-gate 	last = tabptr->zone_fs_options;
16197c478bd9Sstevel@tonic-gate 	for (this = last; this != NULL; this = this->zone_fsopt_next) {
16207c478bd9Sstevel@tonic-gate 		if (strcmp(this->zone_fsopt_opt, option) == 0) {
16217c478bd9Sstevel@tonic-gate 			next = this->zone_fsopt_next;
16227c478bd9Sstevel@tonic-gate 			if (this == tabptr->zone_fs_options)
16237c478bd9Sstevel@tonic-gate 				tabptr->zone_fs_options = next;
16247c478bd9Sstevel@tonic-gate 			else
16257c478bd9Sstevel@tonic-gate 				last->zone_fsopt_next = next;
16267c478bd9Sstevel@tonic-gate 			free(this);
16277c478bd9Sstevel@tonic-gate 			return (Z_OK);
16287c478bd9Sstevel@tonic-gate 		} else
16297c478bd9Sstevel@tonic-gate 			last = this;
16307c478bd9Sstevel@tonic-gate 	}
16317c478bd9Sstevel@tonic-gate 	return (Z_NO_PROPERTY_ID);
16327c478bd9Sstevel@tonic-gate }
16337c478bd9Sstevel@tonic-gate 
16347c478bd9Sstevel@tonic-gate void
16357c478bd9Sstevel@tonic-gate zonecfg_free_fs_option_list(zone_fsopt_t *list)
16367c478bd9Sstevel@tonic-gate {
16377c478bd9Sstevel@tonic-gate 	zone_fsopt_t *this, *next;
16387c478bd9Sstevel@tonic-gate 
16397c478bd9Sstevel@tonic-gate 	for (this = list; this != NULL; this = next) {
16407c478bd9Sstevel@tonic-gate 		next = this->zone_fsopt_next;
16417c478bd9Sstevel@tonic-gate 		free(this);
16427c478bd9Sstevel@tonic-gate 	}
16437c478bd9Sstevel@tonic-gate }
16447c478bd9Sstevel@tonic-gate 
16457c478bd9Sstevel@tonic-gate void
16467c478bd9Sstevel@tonic-gate zonecfg_free_rctl_value_list(struct zone_rctlvaltab *valtab)
16477c478bd9Sstevel@tonic-gate {
16487c478bd9Sstevel@tonic-gate 	if (valtab == NULL)
16497c478bd9Sstevel@tonic-gate 		return;
16507c478bd9Sstevel@tonic-gate 	zonecfg_free_rctl_value_list(valtab->zone_rctlval_next);
16517c478bd9Sstevel@tonic-gate 	free(valtab);
16527c478bd9Sstevel@tonic-gate }
16537c478bd9Sstevel@tonic-gate 
16547c478bd9Sstevel@tonic-gate static boolean_t
16557c478bd9Sstevel@tonic-gate match_prop(xmlNodePtr cur, const xmlChar *attr, char *user_prop)
16567c478bd9Sstevel@tonic-gate {
16577c478bd9Sstevel@tonic-gate 	xmlChar *gotten_prop;
16587c478bd9Sstevel@tonic-gate 	int prop_result;
16597c478bd9Sstevel@tonic-gate 
16607c478bd9Sstevel@tonic-gate 	gotten_prop = xmlGetProp(cur, attr);
16617c478bd9Sstevel@tonic-gate 	if (gotten_prop == NULL)	/* shouldn't happen */
16627c478bd9Sstevel@tonic-gate 		return (B_FALSE);
16637c478bd9Sstevel@tonic-gate 	prop_result = xmlStrcmp(gotten_prop, (const xmlChar *) user_prop);
16647c478bd9Sstevel@tonic-gate 	xmlFree(gotten_prop);
16657c478bd9Sstevel@tonic-gate 	return ((prop_result == 0));
16667c478bd9Sstevel@tonic-gate }
16677c478bd9Sstevel@tonic-gate 
16687c478bd9Sstevel@tonic-gate static int
16697c478bd9Sstevel@tonic-gate zonecfg_delete_filesystem_core(zone_dochandle_t handle,
16707c478bd9Sstevel@tonic-gate     struct zone_fstab *tabptr)
16717c478bd9Sstevel@tonic-gate {
16727c478bd9Sstevel@tonic-gate 	xmlNodePtr cur = handle->zone_dh_cur;
16737c478bd9Sstevel@tonic-gate 	boolean_t dir_match, spec_match, raw_match, type_match;
16747c478bd9Sstevel@tonic-gate 
16757c478bd9Sstevel@tonic-gate 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
16767c478bd9Sstevel@tonic-gate 		if (xmlStrcmp(cur->name, DTD_ELEM_FS))
16777c478bd9Sstevel@tonic-gate 			continue;
16787c478bd9Sstevel@tonic-gate 		dir_match = match_prop(cur, DTD_ATTR_DIR, tabptr->zone_fs_dir);
16797c478bd9Sstevel@tonic-gate 		spec_match = match_prop(cur, DTD_ATTR_SPECIAL,
16807c478bd9Sstevel@tonic-gate 		    tabptr->zone_fs_special);
16817c478bd9Sstevel@tonic-gate 		raw_match = match_prop(cur, DTD_ATTR_RAW,
16827c478bd9Sstevel@tonic-gate 		    tabptr->zone_fs_raw);
16837c478bd9Sstevel@tonic-gate 		type_match = match_prop(cur, DTD_ATTR_TYPE,
16847c478bd9Sstevel@tonic-gate 		    tabptr->zone_fs_type);
16857c478bd9Sstevel@tonic-gate 		if (dir_match && spec_match && raw_match && type_match) {
16867c478bd9Sstevel@tonic-gate 			xmlUnlinkNode(cur);
16877c478bd9Sstevel@tonic-gate 			xmlFreeNode(cur);
16887c478bd9Sstevel@tonic-gate 			return (Z_OK);
16897c478bd9Sstevel@tonic-gate 		}
16907c478bd9Sstevel@tonic-gate 	}
16917c478bd9Sstevel@tonic-gate 	return (Z_NO_RESOURCE_ID);
16927c478bd9Sstevel@tonic-gate }
16937c478bd9Sstevel@tonic-gate 
16947c478bd9Sstevel@tonic-gate int
16957c478bd9Sstevel@tonic-gate zonecfg_delete_filesystem(zone_dochandle_t handle, struct zone_fstab *tabptr)
16967c478bd9Sstevel@tonic-gate {
16977c478bd9Sstevel@tonic-gate 	int err;
16987c478bd9Sstevel@tonic-gate 
16997c478bd9Sstevel@tonic-gate 	if (tabptr == NULL)
17007c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
17017c478bd9Sstevel@tonic-gate 
17027c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
17037c478bd9Sstevel@tonic-gate 		return (err);
17047c478bd9Sstevel@tonic-gate 
17057c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_delete_filesystem_core(handle, tabptr)) != Z_OK)
17067c478bd9Sstevel@tonic-gate 		return (err);
17077c478bd9Sstevel@tonic-gate 
17087c478bd9Sstevel@tonic-gate 	return (Z_OK);
17097c478bd9Sstevel@tonic-gate }
17107c478bd9Sstevel@tonic-gate 
17117c478bd9Sstevel@tonic-gate int
17127c478bd9Sstevel@tonic-gate zonecfg_modify_filesystem(
17137c478bd9Sstevel@tonic-gate 	zone_dochandle_t handle,
17147c478bd9Sstevel@tonic-gate 	struct zone_fstab *oldtabptr,
17157c478bd9Sstevel@tonic-gate 	struct zone_fstab *newtabptr)
17167c478bd9Sstevel@tonic-gate {
17177c478bd9Sstevel@tonic-gate 	int err;
17187c478bd9Sstevel@tonic-gate 
17197c478bd9Sstevel@tonic-gate 	if (oldtabptr == NULL || newtabptr == NULL)
17207c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
17217c478bd9Sstevel@tonic-gate 
17227c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
17237c478bd9Sstevel@tonic-gate 		return (err);
17247c478bd9Sstevel@tonic-gate 
17257c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_delete_filesystem_core(handle, oldtabptr)) != Z_OK)
17267c478bd9Sstevel@tonic-gate 		return (err);
17277c478bd9Sstevel@tonic-gate 
17287c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_add_filesystem_core(handle, newtabptr)) != Z_OK)
17297c478bd9Sstevel@tonic-gate 		return (err);
17307c478bd9Sstevel@tonic-gate 
17317c478bd9Sstevel@tonic-gate 	return (Z_OK);
17327c478bd9Sstevel@tonic-gate }
17337c478bd9Sstevel@tonic-gate 
17347c478bd9Sstevel@tonic-gate static int
17357c478bd9Sstevel@tonic-gate zonecfg_delete_ipd_core(zone_dochandle_t handle, struct zone_fstab *tabptr)
17367c478bd9Sstevel@tonic-gate {
17377c478bd9Sstevel@tonic-gate 	xmlNodePtr cur = handle->zone_dh_cur;
17387c478bd9Sstevel@tonic-gate 
17397c478bd9Sstevel@tonic-gate 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
17407c478bd9Sstevel@tonic-gate 		if (xmlStrcmp(cur->name, DTD_ELEM_IPD))
17417c478bd9Sstevel@tonic-gate 			continue;
17427c478bd9Sstevel@tonic-gate 		if (match_prop(cur, DTD_ATTR_DIR, tabptr->zone_fs_dir)) {
17437c478bd9Sstevel@tonic-gate 			xmlUnlinkNode(cur);
17447c478bd9Sstevel@tonic-gate 			xmlFreeNode(cur);
17457c478bd9Sstevel@tonic-gate 			return (Z_OK);
17467c478bd9Sstevel@tonic-gate 		}
17477c478bd9Sstevel@tonic-gate 	}
17487c478bd9Sstevel@tonic-gate 	return (Z_NO_RESOURCE_ID);
17497c478bd9Sstevel@tonic-gate }
17507c478bd9Sstevel@tonic-gate 
17517c478bd9Sstevel@tonic-gate int
17527c478bd9Sstevel@tonic-gate zonecfg_delete_ipd(zone_dochandle_t handle, struct zone_fstab *tabptr)
17537c478bd9Sstevel@tonic-gate {
17547c478bd9Sstevel@tonic-gate 	int err;
17557c478bd9Sstevel@tonic-gate 
17567c478bd9Sstevel@tonic-gate 	if (tabptr == NULL)
17577c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
17587c478bd9Sstevel@tonic-gate 
17597c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
17607c478bd9Sstevel@tonic-gate 		return (err);
17617c478bd9Sstevel@tonic-gate 
17627c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_delete_ipd_core(handle, tabptr)) != Z_OK)
17637c478bd9Sstevel@tonic-gate 		return (err);
17647c478bd9Sstevel@tonic-gate 
17657c478bd9Sstevel@tonic-gate 	return (Z_OK);
17667c478bd9Sstevel@tonic-gate }
17677c478bd9Sstevel@tonic-gate 
17687c478bd9Sstevel@tonic-gate int
17697c478bd9Sstevel@tonic-gate zonecfg_modify_ipd(zone_dochandle_t handle, struct zone_fstab *oldtabptr,
17707c478bd9Sstevel@tonic-gate     struct zone_fstab *newtabptr)
17717c478bd9Sstevel@tonic-gate {
17727c478bd9Sstevel@tonic-gate 	int err;
17737c478bd9Sstevel@tonic-gate 
17747c478bd9Sstevel@tonic-gate 	if (oldtabptr == NULL || newtabptr == NULL)
17757c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
17767c478bd9Sstevel@tonic-gate 
17777c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
17787c478bd9Sstevel@tonic-gate 		return (err);
17797c478bd9Sstevel@tonic-gate 
17807c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_delete_ipd_core(handle, oldtabptr)) != Z_OK)
17817c478bd9Sstevel@tonic-gate 		return (err);
17827c478bd9Sstevel@tonic-gate 
17837c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_add_ipd_core(handle, newtabptr)) != Z_OK)
17847c478bd9Sstevel@tonic-gate 		return (err);
17857c478bd9Sstevel@tonic-gate 
17867c478bd9Sstevel@tonic-gate 	return (Z_OK);
17877c478bd9Sstevel@tonic-gate }
17887c478bd9Sstevel@tonic-gate 
17897c478bd9Sstevel@tonic-gate int
17907c478bd9Sstevel@tonic-gate zonecfg_lookup_filesystem(
17917c478bd9Sstevel@tonic-gate 	zone_dochandle_t handle,
17927c478bd9Sstevel@tonic-gate 	struct zone_fstab *tabptr)
17937c478bd9Sstevel@tonic-gate {
17947c478bd9Sstevel@tonic-gate 	xmlNodePtr cur, options, firstmatch;
17957c478bd9Sstevel@tonic-gate 	int err;
17967c478bd9Sstevel@tonic-gate 	char dirname[MAXPATHLEN], special[MAXPATHLEN], raw[MAXPATHLEN];
17977c478bd9Sstevel@tonic-gate 	char type[FSTYPSZ];
17987c478bd9Sstevel@tonic-gate 	char options_str[MAX_MNTOPT_STR];
17997c478bd9Sstevel@tonic-gate 
18007c478bd9Sstevel@tonic-gate 	if (tabptr == NULL)
18017c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
18027c478bd9Sstevel@tonic-gate 
18037c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
18047c478bd9Sstevel@tonic-gate 		return (err);
18057c478bd9Sstevel@tonic-gate 
18067c478bd9Sstevel@tonic-gate 	/*
18077c478bd9Sstevel@tonic-gate 	 * Walk the list of children looking for matches on any properties
18087c478bd9Sstevel@tonic-gate 	 * specified in the fstab parameter.  If more than one resource
18097c478bd9Sstevel@tonic-gate 	 * matches, we return Z_INSUFFICIENT_SPEC; if none match, we return
18107c478bd9Sstevel@tonic-gate 	 * Z_NO_RESOURCE_ID.
18117c478bd9Sstevel@tonic-gate 	 */
18127c478bd9Sstevel@tonic-gate 	cur = handle->zone_dh_cur;
18137c478bd9Sstevel@tonic-gate 	firstmatch = NULL;
18147c478bd9Sstevel@tonic-gate 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
18157c478bd9Sstevel@tonic-gate 		if (xmlStrcmp(cur->name, DTD_ELEM_FS))
18167c478bd9Sstevel@tonic-gate 			continue;
18177c478bd9Sstevel@tonic-gate 		if (strlen(tabptr->zone_fs_dir) > 0) {
18187c478bd9Sstevel@tonic-gate 			if ((fetchprop(cur, DTD_ATTR_DIR, dirname,
18197c478bd9Sstevel@tonic-gate 			    sizeof (dirname)) == Z_OK) &&
18207c478bd9Sstevel@tonic-gate 			    (strcmp(tabptr->zone_fs_dir, dirname) == 0)) {
18217c478bd9Sstevel@tonic-gate 				if (firstmatch == NULL)
18227c478bd9Sstevel@tonic-gate 					firstmatch = cur;
18237c478bd9Sstevel@tonic-gate 				else
18247c478bd9Sstevel@tonic-gate 					return (Z_INSUFFICIENT_SPEC);
18257c478bd9Sstevel@tonic-gate 			}
18267c478bd9Sstevel@tonic-gate 		}
18277c478bd9Sstevel@tonic-gate 		if (strlen(tabptr->zone_fs_special) > 0) {
18287c478bd9Sstevel@tonic-gate 			if ((fetchprop(cur, DTD_ATTR_SPECIAL, special,
18297c478bd9Sstevel@tonic-gate 			    sizeof (special)) == Z_OK)) {
18307c478bd9Sstevel@tonic-gate 				if (strcmp(tabptr->zone_fs_special,
18317c478bd9Sstevel@tonic-gate 				    special) == 0) {
18327c478bd9Sstevel@tonic-gate 					if (firstmatch == NULL)
18337c478bd9Sstevel@tonic-gate 						firstmatch = cur;
18347c478bd9Sstevel@tonic-gate 					else if (firstmatch != cur)
18357c478bd9Sstevel@tonic-gate 						return (Z_INSUFFICIENT_SPEC);
18367c478bd9Sstevel@tonic-gate 				} else {
18377c478bd9Sstevel@tonic-gate 					/*
18387c478bd9Sstevel@tonic-gate 					 * If another property matched but this
18397c478bd9Sstevel@tonic-gate 					 * one doesn't then reset firstmatch.
18407c478bd9Sstevel@tonic-gate 					 */
18417c478bd9Sstevel@tonic-gate 					if (firstmatch == cur)
18427c478bd9Sstevel@tonic-gate 						firstmatch = NULL;
18437c478bd9Sstevel@tonic-gate 				}
18447c478bd9Sstevel@tonic-gate 			}
18457c478bd9Sstevel@tonic-gate 		}
18467c478bd9Sstevel@tonic-gate 		if (strlen(tabptr->zone_fs_raw) > 0) {
18477c478bd9Sstevel@tonic-gate 			if ((fetchprop(cur, DTD_ATTR_RAW, raw,
18487c478bd9Sstevel@tonic-gate 			    sizeof (raw)) == Z_OK)) {
18497c478bd9Sstevel@tonic-gate 				if (strcmp(tabptr->zone_fs_raw, raw) == 0) {
18507c478bd9Sstevel@tonic-gate 					if (firstmatch == NULL)
18517c478bd9Sstevel@tonic-gate 						firstmatch = cur;
18527c478bd9Sstevel@tonic-gate 					else if (firstmatch != cur)
18537c478bd9Sstevel@tonic-gate 						return (Z_INSUFFICIENT_SPEC);
18547c478bd9Sstevel@tonic-gate 				} else {
18557c478bd9Sstevel@tonic-gate 					/*
18567c478bd9Sstevel@tonic-gate 					 * If another property matched but this
18577c478bd9Sstevel@tonic-gate 					 * one doesn't then reset firstmatch.
18587c478bd9Sstevel@tonic-gate 					 */
18597c478bd9Sstevel@tonic-gate 					if (firstmatch == cur)
18607c478bd9Sstevel@tonic-gate 						firstmatch = NULL;
18617c478bd9Sstevel@tonic-gate 				}
18627c478bd9Sstevel@tonic-gate 			}
18637c478bd9Sstevel@tonic-gate 		}
18647c478bd9Sstevel@tonic-gate 		if (strlen(tabptr->zone_fs_type) > 0) {
18657c478bd9Sstevel@tonic-gate 			if ((fetchprop(cur, DTD_ATTR_TYPE, type,
18667c478bd9Sstevel@tonic-gate 			    sizeof (type)) == Z_OK)) {
18677c478bd9Sstevel@tonic-gate 				if (strcmp(tabptr->zone_fs_type, type) == 0) {
18687c478bd9Sstevel@tonic-gate 					if (firstmatch == NULL)
18697c478bd9Sstevel@tonic-gate 						firstmatch = cur;
18707c478bd9Sstevel@tonic-gate 					else if (firstmatch != cur)
18717c478bd9Sstevel@tonic-gate 						return (Z_INSUFFICIENT_SPEC);
18727c478bd9Sstevel@tonic-gate 				} else {
18737c478bd9Sstevel@tonic-gate 					/*
18747c478bd9Sstevel@tonic-gate 					 * If another property matched but this
18757c478bd9Sstevel@tonic-gate 					 * one doesn't then reset firstmatch.
18767c478bd9Sstevel@tonic-gate 					 */
18777c478bd9Sstevel@tonic-gate 					if (firstmatch == cur)
18787c478bd9Sstevel@tonic-gate 						firstmatch = NULL;
18797c478bd9Sstevel@tonic-gate 				}
18807c478bd9Sstevel@tonic-gate 			}
18817c478bd9Sstevel@tonic-gate 		}
18827c478bd9Sstevel@tonic-gate 	}
18837c478bd9Sstevel@tonic-gate 
18847c478bd9Sstevel@tonic-gate 	if (firstmatch == NULL)
18857c478bd9Sstevel@tonic-gate 		return (Z_NO_RESOURCE_ID);
18867c478bd9Sstevel@tonic-gate 
18877c478bd9Sstevel@tonic-gate 	cur = firstmatch;
18887c478bd9Sstevel@tonic-gate 
18897c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_DIR, tabptr->zone_fs_dir,
18907c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_fs_dir))) != Z_OK)
18917c478bd9Sstevel@tonic-gate 		return (err);
18927c478bd9Sstevel@tonic-gate 
18937c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_SPECIAL, tabptr->zone_fs_special,
18947c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_fs_special))) != Z_OK)
18957c478bd9Sstevel@tonic-gate 		return (err);
18967c478bd9Sstevel@tonic-gate 
18977c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_RAW, tabptr->zone_fs_raw,
18987c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_fs_raw))) != Z_OK)
18997c478bd9Sstevel@tonic-gate 		return (err);
19007c478bd9Sstevel@tonic-gate 
19017c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_TYPE, tabptr->zone_fs_type,
19027c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_fs_type))) != Z_OK)
19037c478bd9Sstevel@tonic-gate 		return (err);
19047c478bd9Sstevel@tonic-gate 
19057c478bd9Sstevel@tonic-gate 	/* options are optional */
19067c478bd9Sstevel@tonic-gate 	tabptr->zone_fs_options = NULL;
19077c478bd9Sstevel@tonic-gate 	for (options = cur->xmlChildrenNode; options != NULL;
19087c478bd9Sstevel@tonic-gate 	    options = options->next) {
19097c478bd9Sstevel@tonic-gate 		if ((fetchprop(options, DTD_ATTR_NAME, options_str,
19107c478bd9Sstevel@tonic-gate 		    sizeof (options_str)) != Z_OK))
19117c478bd9Sstevel@tonic-gate 			break;
19127c478bd9Sstevel@tonic-gate 		if (zonecfg_add_fs_option(tabptr, options_str) != Z_OK)
19137c478bd9Sstevel@tonic-gate 			break;
19147c478bd9Sstevel@tonic-gate 	}
19157c478bd9Sstevel@tonic-gate 	return (Z_OK);
19167c478bd9Sstevel@tonic-gate }
19177c478bd9Sstevel@tonic-gate 
19187c478bd9Sstevel@tonic-gate int
19197c478bd9Sstevel@tonic-gate zonecfg_lookup_ipd(zone_dochandle_t handle, struct zone_fstab *tabptr)
19207c478bd9Sstevel@tonic-gate {
19217c478bd9Sstevel@tonic-gate 	xmlNodePtr cur, match;
19227c478bd9Sstevel@tonic-gate 	int err;
19237c478bd9Sstevel@tonic-gate 	char dirname[MAXPATHLEN];
19247c478bd9Sstevel@tonic-gate 
19257c478bd9Sstevel@tonic-gate 	if (tabptr == NULL)
19267c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
19277c478bd9Sstevel@tonic-gate 
19287c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
19297c478bd9Sstevel@tonic-gate 		return (err);
19307c478bd9Sstevel@tonic-gate 
19317c478bd9Sstevel@tonic-gate 	/*
19327c478bd9Sstevel@tonic-gate 	 * General algorithm:
19337c478bd9Sstevel@tonic-gate 	 * Walk the list of children looking for matches on any properties
19347c478bd9Sstevel@tonic-gate 	 * specified in the fstab parameter.  If more than one resource
19357c478bd9Sstevel@tonic-gate 	 * matches, we return Z_INSUFFICIENT_SPEC; if none match, we return
19367c478bd9Sstevel@tonic-gate 	 * Z_NO_RESOURCE_ID.
19377c478bd9Sstevel@tonic-gate 	 */
19387c478bd9Sstevel@tonic-gate 	cur = handle->zone_dh_cur;
19397c478bd9Sstevel@tonic-gate 	match = NULL;
19407c478bd9Sstevel@tonic-gate 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
19417c478bd9Sstevel@tonic-gate 		if (xmlStrcmp(cur->name, DTD_ELEM_IPD))
19427c478bd9Sstevel@tonic-gate 			continue;
19437c478bd9Sstevel@tonic-gate 		if (strlen(tabptr->zone_fs_dir) > 0) {
19447c478bd9Sstevel@tonic-gate 			if ((fetchprop(cur, DTD_ATTR_DIR, dirname,
19457c478bd9Sstevel@tonic-gate 			    sizeof (dirname)) == Z_OK) &&
19467c478bd9Sstevel@tonic-gate 			    (strcmp(tabptr->zone_fs_dir, dirname) == 0)) {
19477c478bd9Sstevel@tonic-gate 				if (match == NULL)
19487c478bd9Sstevel@tonic-gate 					match = cur;
19497c478bd9Sstevel@tonic-gate 				else
19507c478bd9Sstevel@tonic-gate 					return (Z_INSUFFICIENT_SPEC);
19517c478bd9Sstevel@tonic-gate 			}
19527c478bd9Sstevel@tonic-gate 		}
19537c478bd9Sstevel@tonic-gate 	}
19547c478bd9Sstevel@tonic-gate 
19557c478bd9Sstevel@tonic-gate 	if (match == NULL)
19567c478bd9Sstevel@tonic-gate 		return (Z_NO_RESOURCE_ID);
19577c478bd9Sstevel@tonic-gate 
19587c478bd9Sstevel@tonic-gate 	cur = match;
19597c478bd9Sstevel@tonic-gate 
19607c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_DIR, tabptr->zone_fs_dir,
19617c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_fs_dir))) != Z_OK)
19627c478bd9Sstevel@tonic-gate 		return (err);
19637c478bd9Sstevel@tonic-gate 
19647c478bd9Sstevel@tonic-gate 	return (Z_OK);
19657c478bd9Sstevel@tonic-gate }
19667c478bd9Sstevel@tonic-gate 
19677c478bd9Sstevel@tonic-gate /*
19687c478bd9Sstevel@tonic-gate  * Compare two IP addresses in string form.  Allow for the possibility that
19697c478bd9Sstevel@tonic-gate  * one might have "/<prefix-length>" at the end: allow a match on just the
19707c478bd9Sstevel@tonic-gate  * IP address (or host name) part.
19717c478bd9Sstevel@tonic-gate  */
19727c478bd9Sstevel@tonic-gate 
19737c478bd9Sstevel@tonic-gate boolean_t
19747c478bd9Sstevel@tonic-gate zonecfg_same_net_address(char *a1, char *a2)
19757c478bd9Sstevel@tonic-gate {
19767c478bd9Sstevel@tonic-gate 	char *slashp, *slashp1, *slashp2;
19777c478bd9Sstevel@tonic-gate 	int result;
19787c478bd9Sstevel@tonic-gate 
19797c478bd9Sstevel@tonic-gate 	if (strcmp(a1, a2) == 0)
19807c478bd9Sstevel@tonic-gate 		return (B_TRUE);
19817c478bd9Sstevel@tonic-gate 
19827c478bd9Sstevel@tonic-gate 	/*
19837c478bd9Sstevel@tonic-gate 	 * If neither has a slash or both do, they need to match to be
19847c478bd9Sstevel@tonic-gate 	 * considered the same, but they did not match above, so fail.
19857c478bd9Sstevel@tonic-gate 	 */
19867c478bd9Sstevel@tonic-gate 	slashp1 = strchr(a1, '/');
19877c478bd9Sstevel@tonic-gate 	slashp2 = strchr(a2, '/');
19887c478bd9Sstevel@tonic-gate 	if ((slashp1 == NULL && slashp2 == NULL) ||
19897c478bd9Sstevel@tonic-gate 	    (slashp1 != NULL && slashp2 != NULL))
19907c478bd9Sstevel@tonic-gate 		return (B_FALSE);
19917c478bd9Sstevel@tonic-gate 
19927c478bd9Sstevel@tonic-gate 	/*
19937c478bd9Sstevel@tonic-gate 	 * Only one had a slash: pick that one, zero out the slash, compare
19947c478bd9Sstevel@tonic-gate 	 * the "address only" strings, restore the slash, and return the
19957c478bd9Sstevel@tonic-gate 	 * result of the comparison.
19967c478bd9Sstevel@tonic-gate 	 */
19977c478bd9Sstevel@tonic-gate 	slashp = (slashp1 == NULL) ? slashp2 : slashp1;
19987c478bd9Sstevel@tonic-gate 	*slashp = '\0';
19997c478bd9Sstevel@tonic-gate 	result = strcmp(a1, a2);
20007c478bd9Sstevel@tonic-gate 	*slashp = '/';
20017c478bd9Sstevel@tonic-gate 	return ((result == 0));
20027c478bd9Sstevel@tonic-gate }
20037c478bd9Sstevel@tonic-gate 
20047c478bd9Sstevel@tonic-gate int
20057c478bd9Sstevel@tonic-gate zonecfg_valid_net_address(char *address, struct lifreq *lifr)
20067c478bd9Sstevel@tonic-gate {
20077c478bd9Sstevel@tonic-gate 	struct sockaddr_in *sin4;
20087c478bd9Sstevel@tonic-gate 	struct sockaddr_in6 *sin6;
20097c478bd9Sstevel@tonic-gate 	struct addrinfo hints, *result;
20107c478bd9Sstevel@tonic-gate 	char *slashp = strchr(address, '/');
20117c478bd9Sstevel@tonic-gate 
20127c478bd9Sstevel@tonic-gate 	bzero(lifr, sizeof (struct lifreq));
20137c478bd9Sstevel@tonic-gate 	sin4 = (struct sockaddr_in *)&lifr->lifr_addr;
20147c478bd9Sstevel@tonic-gate 	sin6 = (struct sockaddr_in6 *)&lifr->lifr_addr;
20157c478bd9Sstevel@tonic-gate 	if (slashp != NULL)
20167c478bd9Sstevel@tonic-gate 		*slashp = '\0';
20177c478bd9Sstevel@tonic-gate 	if (inet_pton(AF_INET, address, &sin4->sin_addr) == 1) {
20187c478bd9Sstevel@tonic-gate 		sin4->sin_family = AF_INET;
20197c478bd9Sstevel@tonic-gate 	} else if (inet_pton(AF_INET6, address, &sin6->sin6_addr) == 1) {
20207c478bd9Sstevel@tonic-gate 		if (slashp == NULL)
20217c478bd9Sstevel@tonic-gate 			return (Z_IPV6_ADDR_PREFIX_LEN);
20227c478bd9Sstevel@tonic-gate 		sin6->sin6_family = AF_INET6;
20237c478bd9Sstevel@tonic-gate 	} else {
20247c478bd9Sstevel@tonic-gate 		/* "address" may be a host name */
20257c478bd9Sstevel@tonic-gate 		(void) memset(&hints, 0, sizeof (hints));
20267c478bd9Sstevel@tonic-gate 		hints.ai_family = PF_INET;
20277c478bd9Sstevel@tonic-gate 		if (getaddrinfo(address, NULL, &hints, &result) != 0)
20287c478bd9Sstevel@tonic-gate 			return (Z_BOGUS_ADDRESS);
20297c478bd9Sstevel@tonic-gate 		sin4->sin_family = result->ai_family;
2030a1be23daSdp 
20317c478bd9Sstevel@tonic-gate 		(void) memcpy(&sin4->sin_addr,
20327c478bd9Sstevel@tonic-gate 		    /* LINTED E_BAD_PTR_CAST_ALIGN */
20337c478bd9Sstevel@tonic-gate 		    &((struct sockaddr_in *)result->ai_addr)->sin_addr,
20347c478bd9Sstevel@tonic-gate 		    sizeof (struct in_addr));
2035a1be23daSdp 
20367c478bd9Sstevel@tonic-gate 		freeaddrinfo(result);
20377c478bd9Sstevel@tonic-gate 	}
20387c478bd9Sstevel@tonic-gate 	return (Z_OK);
20397c478bd9Sstevel@tonic-gate }
20407c478bd9Sstevel@tonic-gate 
20417c478bd9Sstevel@tonic-gate int
20427c478bd9Sstevel@tonic-gate zonecfg_lookup_nwif(zone_dochandle_t handle, struct zone_nwiftab *tabptr)
20437c478bd9Sstevel@tonic-gate {
20447c478bd9Sstevel@tonic-gate 	xmlNodePtr cur, firstmatch;
20457c478bd9Sstevel@tonic-gate 	int err;
20467c478bd9Sstevel@tonic-gate 	char address[INET6_ADDRSTRLEN], physical[LIFNAMSIZ];
20477c478bd9Sstevel@tonic-gate 
20487c478bd9Sstevel@tonic-gate 	if (tabptr == NULL)
20497c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
20507c478bd9Sstevel@tonic-gate 
20517c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
20527c478bd9Sstevel@tonic-gate 		return (err);
20537c478bd9Sstevel@tonic-gate 
20547c478bd9Sstevel@tonic-gate 	cur = handle->zone_dh_cur;
20557c478bd9Sstevel@tonic-gate 	firstmatch = NULL;
20567c478bd9Sstevel@tonic-gate 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
20577c478bd9Sstevel@tonic-gate 		if (xmlStrcmp(cur->name, DTD_ELEM_NET))
20587c478bd9Sstevel@tonic-gate 			continue;
20597c478bd9Sstevel@tonic-gate 		if (strlen(tabptr->zone_nwif_physical) > 0) {
20607c478bd9Sstevel@tonic-gate 			if ((fetchprop(cur, DTD_ATTR_PHYSICAL, physical,
20617c478bd9Sstevel@tonic-gate 			    sizeof (physical)) == Z_OK) &&
20627c478bd9Sstevel@tonic-gate 			    (strcmp(tabptr->zone_nwif_physical,
20637c478bd9Sstevel@tonic-gate 			    physical) == 0)) {
20647c478bd9Sstevel@tonic-gate 				if (firstmatch == NULL)
20657c478bd9Sstevel@tonic-gate 					firstmatch = cur;
20667c478bd9Sstevel@tonic-gate 				else
20677c478bd9Sstevel@tonic-gate 					return (Z_INSUFFICIENT_SPEC);
20687c478bd9Sstevel@tonic-gate 			}
20697c478bd9Sstevel@tonic-gate 		}
20707c478bd9Sstevel@tonic-gate 		if (strlen(tabptr->zone_nwif_address) > 0) {
20717c478bd9Sstevel@tonic-gate 			if ((fetchprop(cur, DTD_ATTR_ADDRESS, address,
20727c478bd9Sstevel@tonic-gate 			    sizeof (address)) == Z_OK)) {
20737c478bd9Sstevel@tonic-gate 				if (zonecfg_same_net_address(
20747c478bd9Sstevel@tonic-gate 				    tabptr->zone_nwif_address, address)) {
20757c478bd9Sstevel@tonic-gate 					if (firstmatch == NULL)
20767c478bd9Sstevel@tonic-gate 						firstmatch = cur;
20777c478bd9Sstevel@tonic-gate 					else if (firstmatch != cur)
20787c478bd9Sstevel@tonic-gate 						return (Z_INSUFFICIENT_SPEC);
20797c478bd9Sstevel@tonic-gate 				} else {
20807c478bd9Sstevel@tonic-gate 					/*
20817c478bd9Sstevel@tonic-gate 					 * If another property matched but this
20827c478bd9Sstevel@tonic-gate 					 * one doesn't then reset firstmatch.
20837c478bd9Sstevel@tonic-gate 					 */
20847c478bd9Sstevel@tonic-gate 					if (firstmatch == cur)
20857c478bd9Sstevel@tonic-gate 						firstmatch = NULL;
20867c478bd9Sstevel@tonic-gate 				}
20877c478bd9Sstevel@tonic-gate 			}
20887c478bd9Sstevel@tonic-gate 		}
20897c478bd9Sstevel@tonic-gate 	}
20907c478bd9Sstevel@tonic-gate 	if (firstmatch == NULL)
20917c478bd9Sstevel@tonic-gate 		return (Z_NO_RESOURCE_ID);
20927c478bd9Sstevel@tonic-gate 
20937c478bd9Sstevel@tonic-gate 	cur = firstmatch;
20947c478bd9Sstevel@tonic-gate 
20957c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_PHYSICAL, tabptr->zone_nwif_physical,
20967c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_nwif_physical))) != Z_OK)
20977c478bd9Sstevel@tonic-gate 		return (err);
20987c478bd9Sstevel@tonic-gate 
20997c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_ADDRESS, tabptr->zone_nwif_address,
21007c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_nwif_address))) != Z_OK)
21017c478bd9Sstevel@tonic-gate 		return (err);
21027c478bd9Sstevel@tonic-gate 
21037c478bd9Sstevel@tonic-gate 	return (Z_OK);
21047c478bd9Sstevel@tonic-gate }
21057c478bd9Sstevel@tonic-gate 
21067c478bd9Sstevel@tonic-gate static int
21077c478bd9Sstevel@tonic-gate zonecfg_add_nwif_core(zone_dochandle_t handle, struct zone_nwiftab *tabptr)
21087c478bd9Sstevel@tonic-gate {
21097c478bd9Sstevel@tonic-gate 	xmlNodePtr newnode, cur = handle->zone_dh_cur;
21107c478bd9Sstevel@tonic-gate 	int err;
21117c478bd9Sstevel@tonic-gate 
21127c478bd9Sstevel@tonic-gate 	newnode = xmlNewTextChild(cur, NULL, DTD_ELEM_NET, NULL);
2113a1be23daSdp 	if ((err = newprop(newnode, DTD_ATTR_ADDRESS,
21147c478bd9Sstevel@tonic-gate 	    tabptr->zone_nwif_address)) != Z_OK)
21157c478bd9Sstevel@tonic-gate 		return (err);
2116a1be23daSdp 	if ((err = newprop(newnode, DTD_ATTR_PHYSICAL,
21177c478bd9Sstevel@tonic-gate 	    tabptr->zone_nwif_physical)) != Z_OK)
21187c478bd9Sstevel@tonic-gate 		return (err);
21197c478bd9Sstevel@tonic-gate 	return (Z_OK);
21207c478bd9Sstevel@tonic-gate }
21217c478bd9Sstevel@tonic-gate 
21227c478bd9Sstevel@tonic-gate int
21237c478bd9Sstevel@tonic-gate zonecfg_add_nwif(zone_dochandle_t handle, struct zone_nwiftab *tabptr)
21247c478bd9Sstevel@tonic-gate {
21257c478bd9Sstevel@tonic-gate 	int err;
21267c478bd9Sstevel@tonic-gate 
21277c478bd9Sstevel@tonic-gate 	if (tabptr == NULL)
21287c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
21297c478bd9Sstevel@tonic-gate 
21307c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
21317c478bd9Sstevel@tonic-gate 		return (err);
21327c478bd9Sstevel@tonic-gate 
21337c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_add_nwif_core(handle, tabptr)) != Z_OK)
21347c478bd9Sstevel@tonic-gate 		return (err);
21357c478bd9Sstevel@tonic-gate 
21367c478bd9Sstevel@tonic-gate 	return (Z_OK);
21377c478bd9Sstevel@tonic-gate }
21387c478bd9Sstevel@tonic-gate 
21397c478bd9Sstevel@tonic-gate static int
21407c478bd9Sstevel@tonic-gate zonecfg_delete_nwif_core(zone_dochandle_t handle, struct zone_nwiftab *tabptr)
21417c478bd9Sstevel@tonic-gate {
21427c478bd9Sstevel@tonic-gate 	xmlNodePtr cur = handle->zone_dh_cur;
21437c478bd9Sstevel@tonic-gate 	boolean_t addr_match, phys_match;
21447c478bd9Sstevel@tonic-gate 
21457c478bd9Sstevel@tonic-gate 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
21467c478bd9Sstevel@tonic-gate 		if (xmlStrcmp(cur->name, DTD_ELEM_NET))
21477c478bd9Sstevel@tonic-gate 			continue;
21487c478bd9Sstevel@tonic-gate 
21497c478bd9Sstevel@tonic-gate 		addr_match = match_prop(cur, DTD_ATTR_ADDRESS,
21507c478bd9Sstevel@tonic-gate 		    tabptr->zone_nwif_address);
21517c478bd9Sstevel@tonic-gate 		phys_match = match_prop(cur, DTD_ATTR_PHYSICAL,
21527c478bd9Sstevel@tonic-gate 		    tabptr->zone_nwif_physical);
21537c478bd9Sstevel@tonic-gate 
21547c478bd9Sstevel@tonic-gate 		if (addr_match && phys_match) {
21557c478bd9Sstevel@tonic-gate 			xmlUnlinkNode(cur);
21567c478bd9Sstevel@tonic-gate 			xmlFreeNode(cur);
21577c478bd9Sstevel@tonic-gate 			return (Z_OK);
21587c478bd9Sstevel@tonic-gate 		}
21597c478bd9Sstevel@tonic-gate 	}
21607c478bd9Sstevel@tonic-gate 	return (Z_NO_RESOURCE_ID);
21617c478bd9Sstevel@tonic-gate }
21627c478bd9Sstevel@tonic-gate 
21637c478bd9Sstevel@tonic-gate int
21647c478bd9Sstevel@tonic-gate zonecfg_delete_nwif(zone_dochandle_t handle, struct zone_nwiftab *tabptr)
21657c478bd9Sstevel@tonic-gate {
21667c478bd9Sstevel@tonic-gate 	int err;
21677c478bd9Sstevel@tonic-gate 
21687c478bd9Sstevel@tonic-gate 	if (tabptr == NULL)
21697c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
21707c478bd9Sstevel@tonic-gate 
21717c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
21727c478bd9Sstevel@tonic-gate 		return (err);
21737c478bd9Sstevel@tonic-gate 
21747c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_delete_nwif_core(handle, tabptr)) != Z_OK)
21757c478bd9Sstevel@tonic-gate 		return (err);
21767c478bd9Sstevel@tonic-gate 
21777c478bd9Sstevel@tonic-gate 	return (Z_OK);
21787c478bd9Sstevel@tonic-gate }
21797c478bd9Sstevel@tonic-gate 
21807c478bd9Sstevel@tonic-gate int
21817c478bd9Sstevel@tonic-gate zonecfg_modify_nwif(
21827c478bd9Sstevel@tonic-gate 	zone_dochandle_t handle,
21837c478bd9Sstevel@tonic-gate 	struct zone_nwiftab *oldtabptr,
21847c478bd9Sstevel@tonic-gate 	struct zone_nwiftab *newtabptr)
21857c478bd9Sstevel@tonic-gate {
21867c478bd9Sstevel@tonic-gate 	int err;
21877c478bd9Sstevel@tonic-gate 
21887c478bd9Sstevel@tonic-gate 	if (oldtabptr == NULL || newtabptr == NULL)
21897c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
21907c478bd9Sstevel@tonic-gate 
21917c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
21927c478bd9Sstevel@tonic-gate 		return (err);
21937c478bd9Sstevel@tonic-gate 
21947c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_delete_nwif_core(handle, oldtabptr)) != Z_OK)
21957c478bd9Sstevel@tonic-gate 		return (err);
21967c478bd9Sstevel@tonic-gate 
21977c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_add_nwif_core(handle, newtabptr)) != Z_OK)
21987c478bd9Sstevel@tonic-gate 		return (err);
21997c478bd9Sstevel@tonic-gate 
22007c478bd9Sstevel@tonic-gate 	return (Z_OK);
22017c478bd9Sstevel@tonic-gate }
22027c478bd9Sstevel@tonic-gate 
22037c478bd9Sstevel@tonic-gate int
22047c478bd9Sstevel@tonic-gate zonecfg_lookup_dev(zone_dochandle_t handle, struct zone_devtab *tabptr)
22057c478bd9Sstevel@tonic-gate {
22067c478bd9Sstevel@tonic-gate 	xmlNodePtr cur, firstmatch;
22077c478bd9Sstevel@tonic-gate 	int err;
22087c478bd9Sstevel@tonic-gate 	char match[MAXPATHLEN];
22097c478bd9Sstevel@tonic-gate 
22107c478bd9Sstevel@tonic-gate 	if (tabptr == NULL)
22117c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
22127c478bd9Sstevel@tonic-gate 
22137c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
22147c478bd9Sstevel@tonic-gate 		return (err);
22157c478bd9Sstevel@tonic-gate 
22167c478bd9Sstevel@tonic-gate 	cur = handle->zone_dh_cur;
22177c478bd9Sstevel@tonic-gate 	firstmatch = NULL;
22187c478bd9Sstevel@tonic-gate 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
22197c478bd9Sstevel@tonic-gate 		if (xmlStrcmp(cur->name, DTD_ELEM_DEVICE))
22207c478bd9Sstevel@tonic-gate 			continue;
22217c478bd9Sstevel@tonic-gate 		if (strlen(tabptr->zone_dev_match) == 0)
22227c478bd9Sstevel@tonic-gate 			continue;
22237c478bd9Sstevel@tonic-gate 
22247c478bd9Sstevel@tonic-gate 		if ((fetchprop(cur, DTD_ATTR_MATCH, match,
22257c478bd9Sstevel@tonic-gate 		    sizeof (match)) == Z_OK)) {
22267c478bd9Sstevel@tonic-gate 			if (strcmp(tabptr->zone_dev_match,
22277c478bd9Sstevel@tonic-gate 			    match) == 0) {
22287c478bd9Sstevel@tonic-gate 				if (firstmatch == NULL)
22297c478bd9Sstevel@tonic-gate 					firstmatch = cur;
22307c478bd9Sstevel@tonic-gate 				else if (firstmatch != cur)
22317c478bd9Sstevel@tonic-gate 					return (Z_INSUFFICIENT_SPEC);
22327c478bd9Sstevel@tonic-gate 			} else {
22337c478bd9Sstevel@tonic-gate 				/*
22347c478bd9Sstevel@tonic-gate 				 * If another property matched but this
22357c478bd9Sstevel@tonic-gate 				 * one doesn't then reset firstmatch.
22367c478bd9Sstevel@tonic-gate 				 */
22377c478bd9Sstevel@tonic-gate 				if (firstmatch == cur)
22387c478bd9Sstevel@tonic-gate 					firstmatch = NULL;
22397c478bd9Sstevel@tonic-gate 			}
22407c478bd9Sstevel@tonic-gate 		}
22417c478bd9Sstevel@tonic-gate 	}
22427c478bd9Sstevel@tonic-gate 	if (firstmatch == NULL)
22437c478bd9Sstevel@tonic-gate 		return (Z_NO_RESOURCE_ID);
22447c478bd9Sstevel@tonic-gate 
22457c478bd9Sstevel@tonic-gate 	cur = firstmatch;
22467c478bd9Sstevel@tonic-gate 
22477c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_MATCH, tabptr->zone_dev_match,
22487c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_dev_match))) != Z_OK)
22497c478bd9Sstevel@tonic-gate 		return (err);
22507c478bd9Sstevel@tonic-gate 
22517c478bd9Sstevel@tonic-gate 	return (Z_OK);
22527c478bd9Sstevel@tonic-gate }
22537c478bd9Sstevel@tonic-gate 
22547c478bd9Sstevel@tonic-gate static int
22557c478bd9Sstevel@tonic-gate zonecfg_add_dev_core(zone_dochandle_t handle, struct zone_devtab *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_DEVICE, NULL);
22617c478bd9Sstevel@tonic-gate 
2262a1be23daSdp 	if ((err = newprop(newnode, DTD_ATTR_MATCH,
22637c478bd9Sstevel@tonic-gate 	    tabptr->zone_dev_match)) != Z_OK)
22647c478bd9Sstevel@tonic-gate 		return (err);
22657c478bd9Sstevel@tonic-gate 
22667c478bd9Sstevel@tonic-gate 	return (Z_OK);
22677c478bd9Sstevel@tonic-gate }
22687c478bd9Sstevel@tonic-gate 
22697c478bd9Sstevel@tonic-gate int
22707c478bd9Sstevel@tonic-gate zonecfg_add_dev(zone_dochandle_t handle, struct zone_devtab *tabptr)
22717c478bd9Sstevel@tonic-gate {
22727c478bd9Sstevel@tonic-gate 	int err;
22737c478bd9Sstevel@tonic-gate 
22747c478bd9Sstevel@tonic-gate 	if (tabptr == NULL)
22757c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
22767c478bd9Sstevel@tonic-gate 
22777c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
22787c478bd9Sstevel@tonic-gate 		return (err);
22797c478bd9Sstevel@tonic-gate 
22807c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_add_dev_core(handle, tabptr)) != Z_OK)
22817c478bd9Sstevel@tonic-gate 		return (err);
22827c478bd9Sstevel@tonic-gate 
22837c478bd9Sstevel@tonic-gate 	return (Z_OK);
22847c478bd9Sstevel@tonic-gate }
22857c478bd9Sstevel@tonic-gate 
22867c478bd9Sstevel@tonic-gate static int
22877c478bd9Sstevel@tonic-gate zonecfg_delete_dev_core(zone_dochandle_t handle, struct zone_devtab *tabptr)
22887c478bd9Sstevel@tonic-gate {
2289facf4a8dSllai 	xmlNodePtr cur = handle->zone_dh_cur;
22907c478bd9Sstevel@tonic-gate 	int match_match;
22917c478bd9Sstevel@tonic-gate 
22927c478bd9Sstevel@tonic-gate 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
22937c478bd9Sstevel@tonic-gate 		if (xmlStrcmp(cur->name, DTD_ELEM_DEVICE))
22947c478bd9Sstevel@tonic-gate 			continue;
22957c478bd9Sstevel@tonic-gate 
22967c478bd9Sstevel@tonic-gate 		match_match = match_prop(cur, DTD_ATTR_MATCH,
22977c478bd9Sstevel@tonic-gate 		    tabptr->zone_dev_match);
22987c478bd9Sstevel@tonic-gate 
22997c478bd9Sstevel@tonic-gate 		if (match_match) {
23007c478bd9Sstevel@tonic-gate 			xmlUnlinkNode(cur);
23017c478bd9Sstevel@tonic-gate 			xmlFreeNode(cur);
23027c478bd9Sstevel@tonic-gate 			return (Z_OK);
23037c478bd9Sstevel@tonic-gate 		}
23047c478bd9Sstevel@tonic-gate 	}
23057c478bd9Sstevel@tonic-gate 	return (Z_NO_RESOURCE_ID);
23067c478bd9Sstevel@tonic-gate }
23077c478bd9Sstevel@tonic-gate 
23087c478bd9Sstevel@tonic-gate int
23097c478bd9Sstevel@tonic-gate zonecfg_delete_dev(zone_dochandle_t handle, struct zone_devtab *tabptr)
23107c478bd9Sstevel@tonic-gate {
23117c478bd9Sstevel@tonic-gate 	int err;
23127c478bd9Sstevel@tonic-gate 
23137c478bd9Sstevel@tonic-gate 	if (tabptr == NULL)
23147c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
23157c478bd9Sstevel@tonic-gate 
23167c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
23177c478bd9Sstevel@tonic-gate 		return (err);
23187c478bd9Sstevel@tonic-gate 
23197c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_delete_dev_core(handle, tabptr)) != Z_OK)
23207c478bd9Sstevel@tonic-gate 		return (err);
23217c478bd9Sstevel@tonic-gate 
23227c478bd9Sstevel@tonic-gate 	return (Z_OK);
23237c478bd9Sstevel@tonic-gate }
23247c478bd9Sstevel@tonic-gate 
23257c478bd9Sstevel@tonic-gate int
23267c478bd9Sstevel@tonic-gate zonecfg_modify_dev(
23277c478bd9Sstevel@tonic-gate 	zone_dochandle_t handle,
23287c478bd9Sstevel@tonic-gate 	struct zone_devtab *oldtabptr,
23297c478bd9Sstevel@tonic-gate 	struct zone_devtab *newtabptr)
23307c478bd9Sstevel@tonic-gate {
23317c478bd9Sstevel@tonic-gate 	int err;
23327c478bd9Sstevel@tonic-gate 
23337c478bd9Sstevel@tonic-gate 	if (oldtabptr == NULL || newtabptr == NULL)
23347c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
23357c478bd9Sstevel@tonic-gate 
23367c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
23377c478bd9Sstevel@tonic-gate 		return (err);
23387c478bd9Sstevel@tonic-gate 
23397c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_delete_dev_core(handle, oldtabptr)) != Z_OK)
23407c478bd9Sstevel@tonic-gate 		return (err);
23417c478bd9Sstevel@tonic-gate 
23427c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_add_dev_core(handle, newtabptr)) != Z_OK)
23437c478bd9Sstevel@tonic-gate 		return (err);
23447c478bd9Sstevel@tonic-gate 
23457c478bd9Sstevel@tonic-gate 	return (Z_OK);
23467c478bd9Sstevel@tonic-gate }
23477c478bd9Sstevel@tonic-gate 
2348ee519a1fSgjelinek /* Lock to serialize all zonecfg_devwalks */
2349ee519a1fSgjelinek static pthread_mutex_t zonecfg_devwalk_lock = PTHREAD_MUTEX_INITIALIZER;
2350ee519a1fSgjelinek /*
2351ee519a1fSgjelinek  * Global variables used to pass data from zonecfg_devwalk to the nftw
2352ee519a1fSgjelinek  * call-back (zonecfg_devwalk_cb).  g_devwalk_data is really the void*
2353ee519a1fSgjelinek  * parameter and g_devwalk_cb is really the *cb parameter from zonecfg_devwalk.
2354ee519a1fSgjelinek  */
2355ee519a1fSgjelinek static void *g_devwalk_data;
2356ee519a1fSgjelinek static int (*g_devwalk_cb)(const char *, uid_t, gid_t, mode_t, const char *,
2357ee519a1fSgjelinek     void *);
2358ee519a1fSgjelinek static size_t g_devwalk_skip_prefix;
2359ee519a1fSgjelinek 
2360ee519a1fSgjelinek /*
2361ee519a1fSgjelinek  * This is the nftw call-back function used by zonecfg_devwalk.  It is
2362ee519a1fSgjelinek  * responsible for calling the actual call-back that is passed in to
2363ee519a1fSgjelinek  * zonecfg_devwalk as the *cb argument.
2364ee519a1fSgjelinek  */
2365ee519a1fSgjelinek /* ARGSUSED2 */
2366ee519a1fSgjelinek static int
2367ee519a1fSgjelinek zonecfg_devwalk_cb(const char *path, const struct stat *st, int f,
2368ee519a1fSgjelinek     struct FTW *ftw)
2369ee519a1fSgjelinek {
2370ee519a1fSgjelinek 	acl_t *acl;
2371ee519a1fSgjelinek 	char *acl_txt = NULL;
2372ee519a1fSgjelinek 
2373ee519a1fSgjelinek 	/* skip all but character and block devices */
2374ee519a1fSgjelinek 	if (!S_ISBLK(st->st_mode) && !S_ISCHR(st->st_mode))
2375ee519a1fSgjelinek 		return (0);
2376ee519a1fSgjelinek 
2377ee519a1fSgjelinek 	if ((acl_get(path, ACL_NO_TRIVIAL, &acl) == 0) &&
2378ee519a1fSgjelinek 	    acl != NULL) {
2379ee519a1fSgjelinek 		acl_txt = acl_totext(acl, ACL_NORESOLVE);
2380ee519a1fSgjelinek 		acl_free(acl);
2381ee519a1fSgjelinek 	}
2382ee519a1fSgjelinek 
2383ee519a1fSgjelinek 	if (strlen(path) <= g_devwalk_skip_prefix)
2384ee519a1fSgjelinek 		return (0);
2385ee519a1fSgjelinek 
2386ee519a1fSgjelinek 	g_devwalk_cb(path + g_devwalk_skip_prefix, st->st_uid, st->st_gid,
2387ee519a1fSgjelinek 	    st->st_mode & S_IAMB, acl_txt != NULL ? acl_txt : "",
2388ee519a1fSgjelinek 	    g_devwalk_data);
2389ee519a1fSgjelinek 	free(acl_txt);
2390ee519a1fSgjelinek 	return (0);
2391ee519a1fSgjelinek }
2392ee519a1fSgjelinek 
2393ee519a1fSgjelinek /*
2394ee519a1fSgjelinek  * Walk the dev tree for the zone specified by hdl and call the call-back (cb)
2395ee519a1fSgjelinek  * function for each entry in the tree.  The call-back will be passed the
2396ee519a1fSgjelinek  * name, uid, gid, mode, acl string and the void *data input parameter
2397ee519a1fSgjelinek  * for each dev entry.
2398ee519a1fSgjelinek  *
2399ee519a1fSgjelinek  * Data is passed to the zonecfg_devwalk_cb through the global variables
2400ee519a1fSgjelinek  * g_devwalk_data, *g_devwalk_cb, and g_devwalk_skip_prefix.  The
2401ee519a1fSgjelinek  * zonecfg_devwalk_cb function will actually call *cb.
2402ee519a1fSgjelinek  */
2403ee519a1fSgjelinek int
2404ee519a1fSgjelinek zonecfg_devwalk(zone_dochandle_t hdl,
2405ee519a1fSgjelinek     int (*cb)(const char *, uid_t, gid_t, mode_t, const char *, void *),
2406ee519a1fSgjelinek     void *data)
2407ee519a1fSgjelinek {
2408ee519a1fSgjelinek 	char path[MAXPATHLEN];
2409ee519a1fSgjelinek 	int ret;
2410ee519a1fSgjelinek 
2411ee519a1fSgjelinek 	if ((ret = zonecfg_get_zonepath(hdl, path, sizeof (path))) != Z_OK)
2412ee519a1fSgjelinek 		return (ret);
2413ee519a1fSgjelinek 
2414ee519a1fSgjelinek 	if (strlcat(path, "/dev", sizeof (path)) >= sizeof (path))
2415ee519a1fSgjelinek 		return (Z_TOO_BIG);
2416ee519a1fSgjelinek 	g_devwalk_skip_prefix = strlen(path) + 1;
2417ee519a1fSgjelinek 
2418ee519a1fSgjelinek 	/*
2419ee519a1fSgjelinek 	 * We have to serialize all zonecfg_devwalks in the same process
2420ee519a1fSgjelinek 	 * (which should be fine), since nftw() is so badly designed.
2421ee519a1fSgjelinek 	 */
2422ee519a1fSgjelinek 	(void) pthread_mutex_lock(&zonecfg_devwalk_lock);
2423ee519a1fSgjelinek 
2424ee519a1fSgjelinek 	g_devwalk_data = data;
2425ee519a1fSgjelinek 	g_devwalk_cb = cb;
2426ee519a1fSgjelinek 	(void) nftw(path, zonecfg_devwalk_cb, 0, FTW_PHYS);
2427ee519a1fSgjelinek 
2428ee519a1fSgjelinek 	(void) pthread_mutex_unlock(&zonecfg_devwalk_lock);
2429ee519a1fSgjelinek 	return (Z_OK);
2430ee519a1fSgjelinek }
2431ee519a1fSgjelinek 
2432ee519a1fSgjelinek /*
2433ee519a1fSgjelinek  * Update the owner, group, mode and acl on the specified dev (inpath) for
2434ee519a1fSgjelinek  * the zone (hdl).  This function can be used to fix up the dev tree after
2435ee519a1fSgjelinek  * attaching a migrated zone.
2436ee519a1fSgjelinek  */
2437ee519a1fSgjelinek int
2438ee519a1fSgjelinek zonecfg_devperms_apply(zone_dochandle_t hdl, const char *inpath, uid_t owner,
2439ee519a1fSgjelinek     gid_t group, mode_t mode, const char *acltxt)
2440ee519a1fSgjelinek {
2441ee519a1fSgjelinek 	int ret;
2442ee519a1fSgjelinek 	char path[MAXPATHLEN];
2443ee519a1fSgjelinek 	struct stat st;
2444ee519a1fSgjelinek 	acl_t *aclp;
2445ee519a1fSgjelinek 
2446ee519a1fSgjelinek 	if ((ret = zonecfg_get_zonepath(hdl, path, sizeof (path))) != Z_OK)
2447ee519a1fSgjelinek 		return (ret);
2448ee519a1fSgjelinek 
2449ee519a1fSgjelinek 	if (strlcat(path, "/dev/", sizeof (path)) >= sizeof (path))
2450ee519a1fSgjelinek 		return (Z_TOO_BIG);
2451ee519a1fSgjelinek 	if (strlcat(path, inpath, sizeof (path)) >= sizeof (path))
2452ee519a1fSgjelinek 		return (Z_TOO_BIG);
2453ee519a1fSgjelinek 
2454ee519a1fSgjelinek 	if (stat(path, &st) == -1)
2455ee519a1fSgjelinek 		return (Z_INVAL);
2456ee519a1fSgjelinek 
2457ee519a1fSgjelinek 	/* make sure we're only touching device nodes */
2458ee519a1fSgjelinek 	if (!S_ISCHR(st.st_mode) && !S_ISBLK(st.st_mode))
2459ee519a1fSgjelinek 		return (Z_INVAL);
2460ee519a1fSgjelinek 
2461ee519a1fSgjelinek 	if (chown(path, owner, group) == -1)
2462ee519a1fSgjelinek 		return (Z_SYSTEM);
2463ee519a1fSgjelinek 
2464ee519a1fSgjelinek 	if (chmod(path, mode) == -1)
2465ee519a1fSgjelinek 		return (Z_SYSTEM);
2466ee519a1fSgjelinek 
2467ee519a1fSgjelinek 	if ((acltxt == NULL) || (strcmp(acltxt, "") == 0))
2468ee519a1fSgjelinek 		return (Z_OK);
2469ee519a1fSgjelinek 
2470ee519a1fSgjelinek 	if (acl_fromtext(acltxt, &aclp) != 0)
2471ee519a1fSgjelinek 		return (Z_SYSTEM);
2472ee519a1fSgjelinek 
2473ee519a1fSgjelinek 	errno = 0;
2474ee519a1fSgjelinek 	if (acl_set(path, aclp) == -1) {
2475ee519a1fSgjelinek 		free(aclp);
2476ee519a1fSgjelinek 		return (Z_SYSTEM);
2477ee519a1fSgjelinek 	}
2478ee519a1fSgjelinek 
2479ee519a1fSgjelinek 	free(aclp);
2480ee519a1fSgjelinek 	return (Z_OK);
2481ee519a1fSgjelinek }
2482ee519a1fSgjelinek 
24837c478bd9Sstevel@tonic-gate /*
24847c478bd9Sstevel@tonic-gate  * This function finds everything mounted under a zone's rootpath.
24857c478bd9Sstevel@tonic-gate  * This returns the number of mounts under rootpath, or -1 on error.
24867c478bd9Sstevel@tonic-gate  * callback is called once per mount found with the first argument
24877c478bd9Sstevel@tonic-gate  * pointing to the  mount point.
24887c478bd9Sstevel@tonic-gate  *
24897c478bd9Sstevel@tonic-gate  * If the callback function returns non-zero zonecfg_find_mounts
24907c478bd9Sstevel@tonic-gate  * aborts with an error.
24917c478bd9Sstevel@tonic-gate  */
24927c478bd9Sstevel@tonic-gate int
24937c478bd9Sstevel@tonic-gate zonecfg_find_mounts(char *rootpath, int (*callback)(const char *, void *),
24947c478bd9Sstevel@tonic-gate     void *priv) {
24957c478bd9Sstevel@tonic-gate 	FILE *mnttab;
24967c478bd9Sstevel@tonic-gate 	struct mnttab m;
24977c478bd9Sstevel@tonic-gate 	size_t l;
249807b574eeSgjelinek 	int zfsl;
24997c478bd9Sstevel@tonic-gate 	int rv = 0;
250007b574eeSgjelinek 	char zfs_path[MAXPATHLEN];
25017c478bd9Sstevel@tonic-gate 
25027c478bd9Sstevel@tonic-gate 	assert(rootpath != NULL);
25037c478bd9Sstevel@tonic-gate 
250407b574eeSgjelinek 	if ((zfsl = snprintf(zfs_path, sizeof (zfs_path), "%s/.zfs/", rootpath))
250507b574eeSgjelinek 	    >= sizeof (zfs_path))
250607b574eeSgjelinek 		return (-1);
250707b574eeSgjelinek 
25087c478bd9Sstevel@tonic-gate 	l = strlen(rootpath);
25097c478bd9Sstevel@tonic-gate 
25107c478bd9Sstevel@tonic-gate 	mnttab = fopen("/etc/mnttab", "r");
25117c478bd9Sstevel@tonic-gate 
25127c478bd9Sstevel@tonic-gate 	if (mnttab == NULL)
25137c478bd9Sstevel@tonic-gate 		return (-1);
25147c478bd9Sstevel@tonic-gate 
25157c478bd9Sstevel@tonic-gate 	if (ioctl(fileno(mnttab), MNTIOC_SHOWHIDDEN, NULL) < 0)  {
25167c478bd9Sstevel@tonic-gate 		rv = -1;
25177c478bd9Sstevel@tonic-gate 		goto out;
25187c478bd9Sstevel@tonic-gate 	}
25197c478bd9Sstevel@tonic-gate 
25207c478bd9Sstevel@tonic-gate 	while (!getmntent(mnttab, &m)) {
25217c478bd9Sstevel@tonic-gate 		if ((strncmp(rootpath, m.mnt_mountp, l) == 0) &&
252207b574eeSgjelinek 		    (m.mnt_mountp[l] == '/') &&
252307b574eeSgjelinek 		    (strncmp(zfs_path, m.mnt_mountp, zfsl) != 0)) {
25247c478bd9Sstevel@tonic-gate 			rv++;
25257c478bd9Sstevel@tonic-gate 			if (callback == NULL)
25267c478bd9Sstevel@tonic-gate 				continue;
25277c478bd9Sstevel@tonic-gate 			if (callback(m.mnt_mountp, priv)) {
25287c478bd9Sstevel@tonic-gate 				rv = -1;
25297c478bd9Sstevel@tonic-gate 				goto out;
25307c478bd9Sstevel@tonic-gate 
25317c478bd9Sstevel@tonic-gate 			}
25327c478bd9Sstevel@tonic-gate 		}
25337c478bd9Sstevel@tonic-gate 	}
25347c478bd9Sstevel@tonic-gate 
25357c478bd9Sstevel@tonic-gate out:
25367c478bd9Sstevel@tonic-gate 	(void) fclose(mnttab);
25377c478bd9Sstevel@tonic-gate 	return (rv);
25387c478bd9Sstevel@tonic-gate }
25397c478bd9Sstevel@tonic-gate 
25407c478bd9Sstevel@tonic-gate int
25417c478bd9Sstevel@tonic-gate zonecfg_lookup_attr(zone_dochandle_t handle, struct zone_attrtab *tabptr)
25427c478bd9Sstevel@tonic-gate {
25437c478bd9Sstevel@tonic-gate 	xmlNodePtr cur, firstmatch;
25447c478bd9Sstevel@tonic-gate 	int err;
25457c478bd9Sstevel@tonic-gate 	char name[MAXNAMELEN], type[MAXNAMELEN], value[MAXNAMELEN];
25467c478bd9Sstevel@tonic-gate 
25477c478bd9Sstevel@tonic-gate 	if (tabptr == NULL)
25487c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
25497c478bd9Sstevel@tonic-gate 
25507c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
25517c478bd9Sstevel@tonic-gate 		return (err);
25527c478bd9Sstevel@tonic-gate 
25537c478bd9Sstevel@tonic-gate 	cur = handle->zone_dh_cur;
25547c478bd9Sstevel@tonic-gate 	firstmatch = NULL;
25557c478bd9Sstevel@tonic-gate 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
25567c478bd9Sstevel@tonic-gate 		if (xmlStrcmp(cur->name, DTD_ELEM_ATTR))
25577c478bd9Sstevel@tonic-gate 			continue;
25587c478bd9Sstevel@tonic-gate 		if (strlen(tabptr->zone_attr_name) > 0) {
25597c478bd9Sstevel@tonic-gate 			if ((fetchprop(cur, DTD_ATTR_NAME, name,
25607c478bd9Sstevel@tonic-gate 			    sizeof (name)) == Z_OK) &&
25617c478bd9Sstevel@tonic-gate 			    (strcmp(tabptr->zone_attr_name, name) == 0)) {
25627c478bd9Sstevel@tonic-gate 				if (firstmatch == NULL)
25637c478bd9Sstevel@tonic-gate 					firstmatch = cur;
25647c478bd9Sstevel@tonic-gate 				else
25657c478bd9Sstevel@tonic-gate 					return (Z_INSUFFICIENT_SPEC);
25667c478bd9Sstevel@tonic-gate 			}
25677c478bd9Sstevel@tonic-gate 		}
25687c478bd9Sstevel@tonic-gate 		if (strlen(tabptr->zone_attr_type) > 0) {
25697c478bd9Sstevel@tonic-gate 			if ((fetchprop(cur, DTD_ATTR_TYPE, type,
25707c478bd9Sstevel@tonic-gate 			    sizeof (type)) == Z_OK)) {
25717c478bd9Sstevel@tonic-gate 				if (strcmp(tabptr->zone_attr_type, type) == 0) {
25727c478bd9Sstevel@tonic-gate 					if (firstmatch == NULL)
25737c478bd9Sstevel@tonic-gate 						firstmatch = cur;
25747c478bd9Sstevel@tonic-gate 					else if (firstmatch != cur)
25757c478bd9Sstevel@tonic-gate 						return (Z_INSUFFICIENT_SPEC);
25767c478bd9Sstevel@tonic-gate 				} else {
25777c478bd9Sstevel@tonic-gate 					/*
25787c478bd9Sstevel@tonic-gate 					 * If another property matched but this
25797c478bd9Sstevel@tonic-gate 					 * one doesn't then reset firstmatch.
25807c478bd9Sstevel@tonic-gate 					 */
25817c478bd9Sstevel@tonic-gate 					if (firstmatch == cur)
25827c478bd9Sstevel@tonic-gate 						firstmatch = NULL;
25837c478bd9Sstevel@tonic-gate 				}
25847c478bd9Sstevel@tonic-gate 			}
25857c478bd9Sstevel@tonic-gate 		}
25867c478bd9Sstevel@tonic-gate 		if (strlen(tabptr->zone_attr_value) > 0) {
25877c478bd9Sstevel@tonic-gate 			if ((fetchprop(cur, DTD_ATTR_VALUE, value,
25887c478bd9Sstevel@tonic-gate 			    sizeof (value)) == Z_OK)) {
25897c478bd9Sstevel@tonic-gate 				if (strcmp(tabptr->zone_attr_value, value) ==
25907c478bd9Sstevel@tonic-gate 				    0) {
25917c478bd9Sstevel@tonic-gate 					if (firstmatch == NULL)
25927c478bd9Sstevel@tonic-gate 						firstmatch = cur;
25937c478bd9Sstevel@tonic-gate 					else if (firstmatch != cur)
25947c478bd9Sstevel@tonic-gate 						return (Z_INSUFFICIENT_SPEC);
25957c478bd9Sstevel@tonic-gate 				} else {
25967c478bd9Sstevel@tonic-gate 					/*
25977c478bd9Sstevel@tonic-gate 					 * If another property matched but this
25987c478bd9Sstevel@tonic-gate 					 * one doesn't then reset firstmatch.
25997c478bd9Sstevel@tonic-gate 					 */
26007c478bd9Sstevel@tonic-gate 					if (firstmatch == cur)
26017c478bd9Sstevel@tonic-gate 						firstmatch = NULL;
26027c478bd9Sstevel@tonic-gate 				}
26037c478bd9Sstevel@tonic-gate 			}
26047c478bd9Sstevel@tonic-gate 		}
26057c478bd9Sstevel@tonic-gate 	}
26067c478bd9Sstevel@tonic-gate 	if (firstmatch == NULL)
26077c478bd9Sstevel@tonic-gate 		return (Z_NO_RESOURCE_ID);
26087c478bd9Sstevel@tonic-gate 
26097c478bd9Sstevel@tonic-gate 	cur = firstmatch;
26107c478bd9Sstevel@tonic-gate 
26117c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_NAME, tabptr->zone_attr_name,
26127c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_attr_name))) != Z_OK)
26137c478bd9Sstevel@tonic-gate 		return (err);
26147c478bd9Sstevel@tonic-gate 
26157c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_TYPE, tabptr->zone_attr_type,
26167c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_attr_type))) != Z_OK)
26177c478bd9Sstevel@tonic-gate 		return (err);
26187c478bd9Sstevel@tonic-gate 
26197c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_VALUE, tabptr->zone_attr_value,
26207c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_attr_value))) != Z_OK)
26217c478bd9Sstevel@tonic-gate 		return (err);
26227c478bd9Sstevel@tonic-gate 
26237c478bd9Sstevel@tonic-gate 	return (Z_OK);
26247c478bd9Sstevel@tonic-gate }
26257c478bd9Sstevel@tonic-gate 
26267c478bd9Sstevel@tonic-gate static int
26277c478bd9Sstevel@tonic-gate zonecfg_add_attr_core(zone_dochandle_t handle, struct zone_attrtab *tabptr)
26287c478bd9Sstevel@tonic-gate {
26297c478bd9Sstevel@tonic-gate 	xmlNodePtr newnode, cur = handle->zone_dh_cur;
26307c478bd9Sstevel@tonic-gate 	int err;
26317c478bd9Sstevel@tonic-gate 
26327c478bd9Sstevel@tonic-gate 	newnode = xmlNewTextChild(cur, NULL, DTD_ELEM_ATTR, NULL);
2633a1be23daSdp 	err = newprop(newnode, DTD_ATTR_NAME, tabptr->zone_attr_name);
26347c478bd9Sstevel@tonic-gate 	if (err != Z_OK)
26357c478bd9Sstevel@tonic-gate 		return (err);
2636a1be23daSdp 	err = newprop(newnode, DTD_ATTR_TYPE, tabptr->zone_attr_type);
26377c478bd9Sstevel@tonic-gate 	if (err != Z_OK)
26387c478bd9Sstevel@tonic-gate 		return (err);
2639a1be23daSdp 	err = newprop(newnode, DTD_ATTR_VALUE, tabptr->zone_attr_value);
26407c478bd9Sstevel@tonic-gate 	if (err != Z_OK)
26417c478bd9Sstevel@tonic-gate 		return (err);
26427c478bd9Sstevel@tonic-gate 	return (Z_OK);
26437c478bd9Sstevel@tonic-gate }
26447c478bd9Sstevel@tonic-gate 
26457c478bd9Sstevel@tonic-gate int
26467c478bd9Sstevel@tonic-gate zonecfg_add_attr(zone_dochandle_t handle, struct zone_attrtab *tabptr)
26477c478bd9Sstevel@tonic-gate {
26487c478bd9Sstevel@tonic-gate 	int err;
26497c478bd9Sstevel@tonic-gate 
26507c478bd9Sstevel@tonic-gate 	if (tabptr == NULL)
26517c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
26527c478bd9Sstevel@tonic-gate 
26537c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
26547c478bd9Sstevel@tonic-gate 		return (err);
26557c478bd9Sstevel@tonic-gate 
26567c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_add_attr_core(handle, tabptr)) != Z_OK)
26577c478bd9Sstevel@tonic-gate 		return (err);
26587c478bd9Sstevel@tonic-gate 
26597c478bd9Sstevel@tonic-gate 	return (Z_OK);
26607c478bd9Sstevel@tonic-gate }
26617c478bd9Sstevel@tonic-gate 
26627c478bd9Sstevel@tonic-gate static int
26637c478bd9Sstevel@tonic-gate zonecfg_delete_attr_core(zone_dochandle_t handle, struct zone_attrtab *tabptr)
26647c478bd9Sstevel@tonic-gate {
26657c478bd9Sstevel@tonic-gate 	xmlNodePtr cur = handle->zone_dh_cur;
26667c478bd9Sstevel@tonic-gate 	int name_match, type_match, value_match;
26677c478bd9Sstevel@tonic-gate 
26687c478bd9Sstevel@tonic-gate 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
26697c478bd9Sstevel@tonic-gate 		if (xmlStrcmp(cur->name, DTD_ELEM_ATTR))
26707c478bd9Sstevel@tonic-gate 			continue;
26717c478bd9Sstevel@tonic-gate 
26727c478bd9Sstevel@tonic-gate 		name_match = match_prop(cur, DTD_ATTR_NAME,
26737c478bd9Sstevel@tonic-gate 		    tabptr->zone_attr_name);
26747c478bd9Sstevel@tonic-gate 		type_match = match_prop(cur, DTD_ATTR_TYPE,
26757c478bd9Sstevel@tonic-gate 		    tabptr->zone_attr_type);
26767c478bd9Sstevel@tonic-gate 		value_match = match_prop(cur, DTD_ATTR_VALUE,
26777c478bd9Sstevel@tonic-gate 		    tabptr->zone_attr_value);
26787c478bd9Sstevel@tonic-gate 
26797c478bd9Sstevel@tonic-gate 		if (name_match && type_match && value_match) {
26807c478bd9Sstevel@tonic-gate 			xmlUnlinkNode(cur);
26817c478bd9Sstevel@tonic-gate 			xmlFreeNode(cur);
26827c478bd9Sstevel@tonic-gate 			return (Z_OK);
26837c478bd9Sstevel@tonic-gate 		}
26847c478bd9Sstevel@tonic-gate 	}
26857c478bd9Sstevel@tonic-gate 	return (Z_NO_RESOURCE_ID);
26867c478bd9Sstevel@tonic-gate }
26877c478bd9Sstevel@tonic-gate 
26887c478bd9Sstevel@tonic-gate int
26897c478bd9Sstevel@tonic-gate zonecfg_delete_attr(zone_dochandle_t handle, struct zone_attrtab *tabptr)
26907c478bd9Sstevel@tonic-gate {
26917c478bd9Sstevel@tonic-gate 	int err;
26927c478bd9Sstevel@tonic-gate 
26937c478bd9Sstevel@tonic-gate 	if (tabptr == NULL)
26947c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
26957c478bd9Sstevel@tonic-gate 
26967c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
26977c478bd9Sstevel@tonic-gate 		return (err);
26987c478bd9Sstevel@tonic-gate 
26997c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_delete_attr_core(handle, tabptr)) != Z_OK)
27007c478bd9Sstevel@tonic-gate 		return (err);
27017c478bd9Sstevel@tonic-gate 
27027c478bd9Sstevel@tonic-gate 	return (Z_OK);
27037c478bd9Sstevel@tonic-gate }
27047c478bd9Sstevel@tonic-gate 
27057c478bd9Sstevel@tonic-gate int
27067c478bd9Sstevel@tonic-gate zonecfg_modify_attr(
27077c478bd9Sstevel@tonic-gate 	zone_dochandle_t handle,
27087c478bd9Sstevel@tonic-gate 	struct zone_attrtab *oldtabptr,
27097c478bd9Sstevel@tonic-gate 	struct zone_attrtab *newtabptr)
27107c478bd9Sstevel@tonic-gate {
27117c478bd9Sstevel@tonic-gate 	int err;
27127c478bd9Sstevel@tonic-gate 
27137c478bd9Sstevel@tonic-gate 	if (oldtabptr == NULL || newtabptr == NULL)
27147c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
27157c478bd9Sstevel@tonic-gate 
27167c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
27177c478bd9Sstevel@tonic-gate 		return (err);
27187c478bd9Sstevel@tonic-gate 
27197c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_delete_attr_core(handle, oldtabptr)) != Z_OK)
27207c478bd9Sstevel@tonic-gate 		return (err);
27217c478bd9Sstevel@tonic-gate 
27227c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_add_attr_core(handle, newtabptr)) != Z_OK)
27237c478bd9Sstevel@tonic-gate 		return (err);
27247c478bd9Sstevel@tonic-gate 
27257c478bd9Sstevel@tonic-gate 	return (Z_OK);
27267c478bd9Sstevel@tonic-gate }
27277c478bd9Sstevel@tonic-gate 
27287c478bd9Sstevel@tonic-gate int
27297c478bd9Sstevel@tonic-gate zonecfg_get_attr_boolean(const struct zone_attrtab *attr, boolean_t *value)
27307c478bd9Sstevel@tonic-gate {
27317c478bd9Sstevel@tonic-gate 	if (attr == NULL)
27327c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
27337c478bd9Sstevel@tonic-gate 
27347c478bd9Sstevel@tonic-gate 	if (strcmp(attr->zone_attr_type, DTD_ENTITY_BOOLEAN) != 0)
27357c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
27367c478bd9Sstevel@tonic-gate 
27377c478bd9Sstevel@tonic-gate 	if (strcmp(attr->zone_attr_value, DTD_ENTITY_TRUE) == 0) {
27387c478bd9Sstevel@tonic-gate 		*value = B_TRUE;
27397c478bd9Sstevel@tonic-gate 		return (Z_OK);
27407c478bd9Sstevel@tonic-gate 	}
27417c478bd9Sstevel@tonic-gate 	if (strcmp(attr->zone_attr_value, DTD_ENTITY_FALSE) == 0) {
27427c478bd9Sstevel@tonic-gate 		*value = B_FALSE;
27437c478bd9Sstevel@tonic-gate 		return (Z_OK);
27447c478bd9Sstevel@tonic-gate 	}
27457c478bd9Sstevel@tonic-gate 	return (Z_INVAL);
27467c478bd9Sstevel@tonic-gate }
27477c478bd9Sstevel@tonic-gate 
27487c478bd9Sstevel@tonic-gate int
27497c478bd9Sstevel@tonic-gate zonecfg_get_attr_int(const struct zone_attrtab *attr, int64_t *value)
27507c478bd9Sstevel@tonic-gate {
27517c478bd9Sstevel@tonic-gate 	long long result;
27527c478bd9Sstevel@tonic-gate 	char *endptr;
27537c478bd9Sstevel@tonic-gate 
27547c478bd9Sstevel@tonic-gate 	if (attr == NULL)
27557c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
27567c478bd9Sstevel@tonic-gate 
27577c478bd9Sstevel@tonic-gate 	if (strcmp(attr->zone_attr_type, DTD_ENTITY_INT) != 0)
27587c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
27597c478bd9Sstevel@tonic-gate 
27607c478bd9Sstevel@tonic-gate 	errno = 0;
27617c478bd9Sstevel@tonic-gate 	result = strtoll(attr->zone_attr_value, &endptr, 10);
27627c478bd9Sstevel@tonic-gate 	if (errno != 0 || *endptr != '\0')
27637c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
27647c478bd9Sstevel@tonic-gate 	*value = result;
27657c478bd9Sstevel@tonic-gate 	return (Z_OK);
27667c478bd9Sstevel@tonic-gate }
27677c478bd9Sstevel@tonic-gate 
27687c478bd9Sstevel@tonic-gate int
27697c478bd9Sstevel@tonic-gate zonecfg_get_attr_string(const struct zone_attrtab *attr, char *value,
27707c478bd9Sstevel@tonic-gate     size_t val_sz)
27717c478bd9Sstevel@tonic-gate {
27727c478bd9Sstevel@tonic-gate 	if (attr == NULL)
27737c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
27747c478bd9Sstevel@tonic-gate 
27757c478bd9Sstevel@tonic-gate 	if (strcmp(attr->zone_attr_type, DTD_ENTITY_STRING) != 0)
27767c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
27777c478bd9Sstevel@tonic-gate 
27787c478bd9Sstevel@tonic-gate 	if (strlcpy(value, attr->zone_attr_value, val_sz) >= val_sz)
27797c478bd9Sstevel@tonic-gate 		return (Z_TOO_BIG);
27807c478bd9Sstevel@tonic-gate 	return (Z_OK);
27817c478bd9Sstevel@tonic-gate }
27827c478bd9Sstevel@tonic-gate 
27837c478bd9Sstevel@tonic-gate int
27847c478bd9Sstevel@tonic-gate zonecfg_get_attr_uint(const struct zone_attrtab *attr, uint64_t *value)
27857c478bd9Sstevel@tonic-gate {
27867c478bd9Sstevel@tonic-gate 	unsigned long long result;
27877c478bd9Sstevel@tonic-gate 	long long neg_result;
27887c478bd9Sstevel@tonic-gate 	char *endptr;
27897c478bd9Sstevel@tonic-gate 
27907c478bd9Sstevel@tonic-gate 	if (attr == NULL)
27917c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
27927c478bd9Sstevel@tonic-gate 
27937c478bd9Sstevel@tonic-gate 	if (strcmp(attr->zone_attr_type, DTD_ENTITY_UINT) != 0)
27947c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
27957c478bd9Sstevel@tonic-gate 
27967c478bd9Sstevel@tonic-gate 	errno = 0;
27977c478bd9Sstevel@tonic-gate 	result = strtoull(attr->zone_attr_value, &endptr, 10);
27987c478bd9Sstevel@tonic-gate 	if (errno != 0 || *endptr != '\0')
27997c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
28007c478bd9Sstevel@tonic-gate 	errno = 0;
28017c478bd9Sstevel@tonic-gate 	neg_result = strtoll(attr->zone_attr_value, &endptr, 10);
28027c478bd9Sstevel@tonic-gate 	/*
28037c478bd9Sstevel@tonic-gate 	 * Incredibly, strtoull("<negative number>", ...) will not fail but
28047c478bd9Sstevel@tonic-gate 	 * return whatever (negative) number cast as a u_longlong_t, so we
28057c478bd9Sstevel@tonic-gate 	 * need to look for this here.
28067c478bd9Sstevel@tonic-gate 	 */
28077c478bd9Sstevel@tonic-gate 	if (errno == 0 && neg_result < 0)
28087c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
28097c478bd9Sstevel@tonic-gate 	*value = result;
28107c478bd9Sstevel@tonic-gate 	return (Z_OK);
28117c478bd9Sstevel@tonic-gate }
28127c478bd9Sstevel@tonic-gate 
28137c478bd9Sstevel@tonic-gate int
28147c478bd9Sstevel@tonic-gate zonecfg_lookup_rctl(zone_dochandle_t handle, struct zone_rctltab *tabptr)
28157c478bd9Sstevel@tonic-gate {
28167c478bd9Sstevel@tonic-gate 	xmlNodePtr cur, val;
28177c478bd9Sstevel@tonic-gate 	char savedname[MAXNAMELEN];
28187c478bd9Sstevel@tonic-gate 	struct zone_rctlvaltab *valptr;
28197c478bd9Sstevel@tonic-gate 	int err;
28207c478bd9Sstevel@tonic-gate 
28217c478bd9Sstevel@tonic-gate 	if (tabptr->zone_rctl_name == NULL ||
28227c478bd9Sstevel@tonic-gate 	    strlen(tabptr->zone_rctl_name) == 0)
28237c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
28247c478bd9Sstevel@tonic-gate 
28257c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
28267c478bd9Sstevel@tonic-gate 		return (err);
28277c478bd9Sstevel@tonic-gate 
28287c478bd9Sstevel@tonic-gate 	cur = handle->zone_dh_cur;
28297c478bd9Sstevel@tonic-gate 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
28307c478bd9Sstevel@tonic-gate 		if (xmlStrcmp(cur->name, DTD_ELEM_RCTL))
28317c478bd9Sstevel@tonic-gate 			continue;
28327c478bd9Sstevel@tonic-gate 		if ((fetchprop(cur, DTD_ATTR_NAME, savedname,
28337c478bd9Sstevel@tonic-gate 		    sizeof (savedname)) == Z_OK) &&
28347c478bd9Sstevel@tonic-gate 		    (strcmp(savedname, tabptr->zone_rctl_name) == 0)) {
28357c478bd9Sstevel@tonic-gate 			tabptr->zone_rctl_valptr = NULL;
28367c478bd9Sstevel@tonic-gate 			for (val = cur->xmlChildrenNode; val != NULL;
28377c478bd9Sstevel@tonic-gate 			    val = val->next) {
28387c478bd9Sstevel@tonic-gate 				valptr = (struct zone_rctlvaltab *)malloc(
28397c478bd9Sstevel@tonic-gate 				    sizeof (struct zone_rctlvaltab));
28407c478bd9Sstevel@tonic-gate 				if (valptr == NULL)
28417c478bd9Sstevel@tonic-gate 					return (Z_NOMEM);
28427c478bd9Sstevel@tonic-gate 				if ((fetchprop(val, DTD_ATTR_PRIV,
28437c478bd9Sstevel@tonic-gate 				    valptr->zone_rctlval_priv,
28447c478bd9Sstevel@tonic-gate 				    sizeof (valptr->zone_rctlval_priv)) !=
28457c478bd9Sstevel@tonic-gate 				    Z_OK))
28467c478bd9Sstevel@tonic-gate 					break;
28477c478bd9Sstevel@tonic-gate 				if ((fetchprop(val, DTD_ATTR_LIMIT,
28487c478bd9Sstevel@tonic-gate 				    valptr->zone_rctlval_limit,
28497c478bd9Sstevel@tonic-gate 				    sizeof (valptr->zone_rctlval_limit)) !=
28507c478bd9Sstevel@tonic-gate 				    Z_OK))
28517c478bd9Sstevel@tonic-gate 					break;
28527c478bd9Sstevel@tonic-gate 				if ((fetchprop(val, DTD_ATTR_ACTION,
28537c478bd9Sstevel@tonic-gate 				    valptr->zone_rctlval_action,
28547c478bd9Sstevel@tonic-gate 				    sizeof (valptr->zone_rctlval_action)) !=
28557c478bd9Sstevel@tonic-gate 				    Z_OK))
28567c478bd9Sstevel@tonic-gate 					break;
28577c478bd9Sstevel@tonic-gate 				if (zonecfg_add_rctl_value(tabptr, valptr) !=
28587c478bd9Sstevel@tonic-gate 				    Z_OK)
28597c478bd9Sstevel@tonic-gate 					break;
28607c478bd9Sstevel@tonic-gate 			}
28617c478bd9Sstevel@tonic-gate 			return (Z_OK);
28627c478bd9Sstevel@tonic-gate 		}
28637c478bd9Sstevel@tonic-gate 	}
28647c478bd9Sstevel@tonic-gate 	return (Z_NO_RESOURCE_ID);
28657c478bd9Sstevel@tonic-gate }
28667c478bd9Sstevel@tonic-gate 
28677c478bd9Sstevel@tonic-gate static int
28687c478bd9Sstevel@tonic-gate zonecfg_add_rctl_core(zone_dochandle_t handle, struct zone_rctltab *tabptr)
28697c478bd9Sstevel@tonic-gate {
28707c478bd9Sstevel@tonic-gate 	xmlNodePtr newnode, cur = handle->zone_dh_cur, valnode;
28717c478bd9Sstevel@tonic-gate 	struct zone_rctlvaltab *valptr;
28727c478bd9Sstevel@tonic-gate 	int err;
28737c478bd9Sstevel@tonic-gate 
28747c478bd9Sstevel@tonic-gate 	newnode = xmlNewTextChild(cur, NULL, DTD_ELEM_RCTL, NULL);
2875a1be23daSdp 	err = newprop(newnode, DTD_ATTR_NAME, tabptr->zone_rctl_name);
28767c478bd9Sstevel@tonic-gate 	if (err != Z_OK)
28777c478bd9Sstevel@tonic-gate 		return (err);
28787c478bd9Sstevel@tonic-gate 	for (valptr = tabptr->zone_rctl_valptr; valptr != NULL;
28797c478bd9Sstevel@tonic-gate 	    valptr = valptr->zone_rctlval_next) {
28807c478bd9Sstevel@tonic-gate 		valnode = xmlNewTextChild(newnode, NULL,
28817c478bd9Sstevel@tonic-gate 		    DTD_ELEM_RCTLVALUE, NULL);
2882a1be23daSdp 		err = newprop(valnode, DTD_ATTR_PRIV,
28837c478bd9Sstevel@tonic-gate 		    valptr->zone_rctlval_priv);
28847c478bd9Sstevel@tonic-gate 		if (err != Z_OK)
28857c478bd9Sstevel@tonic-gate 			return (err);
2886a1be23daSdp 		err = newprop(valnode, DTD_ATTR_LIMIT,
28877c478bd9Sstevel@tonic-gate 		    valptr->zone_rctlval_limit);
28887c478bd9Sstevel@tonic-gate 		if (err != Z_OK)
28897c478bd9Sstevel@tonic-gate 			return (err);
2890a1be23daSdp 		err = newprop(valnode, DTD_ATTR_ACTION,
28917c478bd9Sstevel@tonic-gate 		    valptr->zone_rctlval_action);
28927c478bd9Sstevel@tonic-gate 		if (err != Z_OK)
28937c478bd9Sstevel@tonic-gate 			return (err);
28947c478bd9Sstevel@tonic-gate 	}
28957c478bd9Sstevel@tonic-gate 	return (Z_OK);
28967c478bd9Sstevel@tonic-gate }
28977c478bd9Sstevel@tonic-gate 
28987c478bd9Sstevel@tonic-gate int
28997c478bd9Sstevel@tonic-gate zonecfg_add_rctl(zone_dochandle_t handle, struct zone_rctltab *tabptr)
29007c478bd9Sstevel@tonic-gate {
29017c478bd9Sstevel@tonic-gate 	int err;
29027c478bd9Sstevel@tonic-gate 
29037c478bd9Sstevel@tonic-gate 	if (tabptr == NULL || tabptr->zone_rctl_name == NULL)
29047c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
29057c478bd9Sstevel@tonic-gate 
29067c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
29077c478bd9Sstevel@tonic-gate 		return (err);
29087c478bd9Sstevel@tonic-gate 
29097c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_add_rctl_core(handle, tabptr)) != Z_OK)
29107c478bd9Sstevel@tonic-gate 		return (err);
29117c478bd9Sstevel@tonic-gate 
29127c478bd9Sstevel@tonic-gate 	return (Z_OK);
29137c478bd9Sstevel@tonic-gate }
29147c478bd9Sstevel@tonic-gate 
29157c478bd9Sstevel@tonic-gate static int
29167c478bd9Sstevel@tonic-gate zonecfg_delete_rctl_core(zone_dochandle_t handle, struct zone_rctltab *tabptr)
29177c478bd9Sstevel@tonic-gate {
29187c478bd9Sstevel@tonic-gate 	xmlNodePtr cur = handle->zone_dh_cur;
29197c478bd9Sstevel@tonic-gate 	xmlChar *savedname;
29207c478bd9Sstevel@tonic-gate 	int name_result;
29217c478bd9Sstevel@tonic-gate 
29227c478bd9Sstevel@tonic-gate 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
29237c478bd9Sstevel@tonic-gate 		if (xmlStrcmp(cur->name, DTD_ELEM_RCTL))
29247c478bd9Sstevel@tonic-gate 			continue;
29257c478bd9Sstevel@tonic-gate 
29267c478bd9Sstevel@tonic-gate 		savedname = xmlGetProp(cur, DTD_ATTR_NAME);
29277c478bd9Sstevel@tonic-gate 		if (savedname == NULL)	/* shouldn't happen */
29287c478bd9Sstevel@tonic-gate 			continue;
29297c478bd9Sstevel@tonic-gate 		name_result = xmlStrcmp(savedname,
29307c478bd9Sstevel@tonic-gate 		    (const xmlChar *) tabptr->zone_rctl_name);
29317c478bd9Sstevel@tonic-gate 		xmlFree(savedname);
29327c478bd9Sstevel@tonic-gate 
29337c478bd9Sstevel@tonic-gate 		if (name_result == 0) {
29347c478bd9Sstevel@tonic-gate 			xmlUnlinkNode(cur);
29357c478bd9Sstevel@tonic-gate 			xmlFreeNode(cur);
29367c478bd9Sstevel@tonic-gate 			return (Z_OK);
29377c478bd9Sstevel@tonic-gate 		}
29387c478bd9Sstevel@tonic-gate 	}
29397c478bd9Sstevel@tonic-gate 	return (Z_NO_RESOURCE_ID);
29407c478bd9Sstevel@tonic-gate }
29417c478bd9Sstevel@tonic-gate 
29427c478bd9Sstevel@tonic-gate int
29437c478bd9Sstevel@tonic-gate zonecfg_delete_rctl(zone_dochandle_t handle, struct zone_rctltab *tabptr)
29447c478bd9Sstevel@tonic-gate {
29457c478bd9Sstevel@tonic-gate 	int err;
29467c478bd9Sstevel@tonic-gate 
29477c478bd9Sstevel@tonic-gate 	if (tabptr == NULL || tabptr->zone_rctl_name == NULL)
29487c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
29497c478bd9Sstevel@tonic-gate 
29507c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
29517c478bd9Sstevel@tonic-gate 		return (err);
29527c478bd9Sstevel@tonic-gate 
29537c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_delete_rctl_core(handle, tabptr)) != Z_OK)
29547c478bd9Sstevel@tonic-gate 		return (err);
29557c478bd9Sstevel@tonic-gate 
29567c478bd9Sstevel@tonic-gate 	return (Z_OK);
29577c478bd9Sstevel@tonic-gate }
29587c478bd9Sstevel@tonic-gate 
29597c478bd9Sstevel@tonic-gate int
29607c478bd9Sstevel@tonic-gate zonecfg_modify_rctl(
29617c478bd9Sstevel@tonic-gate 	zone_dochandle_t handle,
29627c478bd9Sstevel@tonic-gate 	struct zone_rctltab *oldtabptr,
29637c478bd9Sstevel@tonic-gate 	struct zone_rctltab *newtabptr)
29647c478bd9Sstevel@tonic-gate {
29657c478bd9Sstevel@tonic-gate 	int err;
29667c478bd9Sstevel@tonic-gate 
29677c478bd9Sstevel@tonic-gate 	if (oldtabptr == NULL || oldtabptr->zone_rctl_name == NULL ||
29687c478bd9Sstevel@tonic-gate 	    newtabptr == NULL || newtabptr->zone_rctl_name == NULL)
29697c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
29707c478bd9Sstevel@tonic-gate 
29717c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
29727c478bd9Sstevel@tonic-gate 		return (err);
29737c478bd9Sstevel@tonic-gate 
29747c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_delete_rctl_core(handle, oldtabptr)) != Z_OK)
29757c478bd9Sstevel@tonic-gate 		return (err);
29767c478bd9Sstevel@tonic-gate 
29777c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_add_rctl_core(handle, newtabptr)) != Z_OK)
29787c478bd9Sstevel@tonic-gate 		return (err);
29797c478bd9Sstevel@tonic-gate 
29807c478bd9Sstevel@tonic-gate 	return (Z_OK);
29817c478bd9Sstevel@tonic-gate }
29827c478bd9Sstevel@tonic-gate 
29837c478bd9Sstevel@tonic-gate int
29847c478bd9Sstevel@tonic-gate zonecfg_add_rctl_value(
29857c478bd9Sstevel@tonic-gate 	struct zone_rctltab *tabptr,
29867c478bd9Sstevel@tonic-gate 	struct zone_rctlvaltab *valtabptr)
29877c478bd9Sstevel@tonic-gate {
29887c478bd9Sstevel@tonic-gate 	struct zone_rctlvaltab *last, *old, *new;
29897c478bd9Sstevel@tonic-gate 	rctlblk_t *rctlblk = alloca(rctlblk_size());
29907c478bd9Sstevel@tonic-gate 
29917c478bd9Sstevel@tonic-gate 	last = tabptr->zone_rctl_valptr;
29927c478bd9Sstevel@tonic-gate 	for (old = last; old != NULL; old = old->zone_rctlval_next)
29937c478bd9Sstevel@tonic-gate 		last = old;	/* walk to the end of the list */
29947c478bd9Sstevel@tonic-gate 	new = valtabptr;	/* alloc'd by caller */
29957c478bd9Sstevel@tonic-gate 	new->zone_rctlval_next = NULL;
29967c478bd9Sstevel@tonic-gate 	if (zonecfg_construct_rctlblk(valtabptr, rctlblk) != Z_OK)
29977c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
29987c478bd9Sstevel@tonic-gate 	if (!zonecfg_valid_rctlblk(rctlblk))
29997c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
30007c478bd9Sstevel@tonic-gate 	if (last == NULL)
30017c478bd9Sstevel@tonic-gate 		tabptr->zone_rctl_valptr = new;
30027c478bd9Sstevel@tonic-gate 	else
30037c478bd9Sstevel@tonic-gate 		last->zone_rctlval_next = new;
30047c478bd9Sstevel@tonic-gate 	return (Z_OK);
30057c478bd9Sstevel@tonic-gate }
30067c478bd9Sstevel@tonic-gate 
30077c478bd9Sstevel@tonic-gate int
30087c478bd9Sstevel@tonic-gate zonecfg_remove_rctl_value(
30097c478bd9Sstevel@tonic-gate 	struct zone_rctltab *tabptr,
30107c478bd9Sstevel@tonic-gate 	struct zone_rctlvaltab *valtabptr)
30117c478bd9Sstevel@tonic-gate {
30127c478bd9Sstevel@tonic-gate 	struct zone_rctlvaltab *last, *this, *next;
30137c478bd9Sstevel@tonic-gate 
30147c478bd9Sstevel@tonic-gate 	last = tabptr->zone_rctl_valptr;
30157c478bd9Sstevel@tonic-gate 	for (this = last; this != NULL; this = this->zone_rctlval_next) {
30167c478bd9Sstevel@tonic-gate 		if (strcmp(this->zone_rctlval_priv,
30177c478bd9Sstevel@tonic-gate 		    valtabptr->zone_rctlval_priv) == 0 &&
30187c478bd9Sstevel@tonic-gate 		    strcmp(this->zone_rctlval_limit,
30197c478bd9Sstevel@tonic-gate 		    valtabptr->zone_rctlval_limit) == 0 &&
30207c478bd9Sstevel@tonic-gate 		    strcmp(this->zone_rctlval_action,
30217c478bd9Sstevel@tonic-gate 		    valtabptr->zone_rctlval_action) == 0) {
30227c478bd9Sstevel@tonic-gate 			next = this->zone_rctlval_next;
30237c478bd9Sstevel@tonic-gate 			if (this == tabptr->zone_rctl_valptr)
30247c478bd9Sstevel@tonic-gate 				tabptr->zone_rctl_valptr = next;
30257c478bd9Sstevel@tonic-gate 			else
30267c478bd9Sstevel@tonic-gate 				last->zone_rctlval_next = next;
30277c478bd9Sstevel@tonic-gate 			free(this);
30287c478bd9Sstevel@tonic-gate 			return (Z_OK);
30297c478bd9Sstevel@tonic-gate 		} else
30307c478bd9Sstevel@tonic-gate 			last = this;
30317c478bd9Sstevel@tonic-gate 	}
30327c478bd9Sstevel@tonic-gate 	return (Z_NO_PROPERTY_ID);
30337c478bd9Sstevel@tonic-gate }
30347c478bd9Sstevel@tonic-gate 
30357c478bd9Sstevel@tonic-gate char *
30367c478bd9Sstevel@tonic-gate zonecfg_strerror(int errnum)
30377c478bd9Sstevel@tonic-gate {
30387c478bd9Sstevel@tonic-gate 	switch (errnum) {
30397c478bd9Sstevel@tonic-gate 	case Z_OK:
30407c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "OK"));
30417c478bd9Sstevel@tonic-gate 	case Z_EMPTY_DOCUMENT:
30427c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Empty document"));
30437c478bd9Sstevel@tonic-gate 	case Z_WRONG_DOC_TYPE:
30447c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Wrong document type"));
30457c478bd9Sstevel@tonic-gate 	case Z_BAD_PROPERTY:
30467c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Bad document property"));
30477c478bd9Sstevel@tonic-gate 	case Z_TEMP_FILE:
30487c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN,
30497c478bd9Sstevel@tonic-gate 		    "Problem creating temporary file"));
30507c478bd9Sstevel@tonic-gate 	case Z_SAVING_FILE:
30517c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Problem saving file"));
30527c478bd9Sstevel@tonic-gate 	case Z_NO_ENTRY:
30537c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "No such entry"));
30547c478bd9Sstevel@tonic-gate 	case Z_BOGUS_ZONE_NAME:
30557c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Bogus zone name"));
30567c478bd9Sstevel@tonic-gate 	case Z_REQD_RESOURCE_MISSING:
30577c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Required resource missing"));
30587c478bd9Sstevel@tonic-gate 	case Z_REQD_PROPERTY_MISSING:
30597c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Required property missing"));
30607c478bd9Sstevel@tonic-gate 	case Z_BAD_HANDLE:
30617c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Bad handle"));
30627c478bd9Sstevel@tonic-gate 	case Z_NOMEM:
30637c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Out of memory"));
30647c478bd9Sstevel@tonic-gate 	case Z_INVAL:
30657c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Invalid argument"));
30667c478bd9Sstevel@tonic-gate 	case Z_ACCES:
30677c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Permission denied"));
30687c478bd9Sstevel@tonic-gate 	case Z_TOO_BIG:
30697c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Argument list too long"));
30707c478bd9Sstevel@tonic-gate 	case Z_MISC_FS:
30717c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN,
30727c478bd9Sstevel@tonic-gate 		    "Miscellaneous file system error"));
30737c478bd9Sstevel@tonic-gate 	case Z_NO_ZONE:
30747c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "No such zone configured"));
30757c478bd9Sstevel@tonic-gate 	case Z_NO_RESOURCE_TYPE:
30767c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "No such resource type"));
30777c478bd9Sstevel@tonic-gate 	case Z_NO_RESOURCE_ID:
30787c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "No such resource with that id"));
30797c478bd9Sstevel@tonic-gate 	case Z_NO_PROPERTY_TYPE:
30807c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "No such property type"));
30817c478bd9Sstevel@tonic-gate 	case Z_NO_PROPERTY_ID:
30827c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "No such property with that id"));
3083087719fdSdp 	case Z_BAD_ZONE_STATE:
30847c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN,
3085087719fdSdp 		    "Zone state is invalid for the requested operation"));
30867c478bd9Sstevel@tonic-gate 	case Z_INVALID_DOCUMENT:
30877c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Invalid document"));
3088087719fdSdp 	case Z_NAME_IN_USE:
3089087719fdSdp 		return (dgettext(TEXT_DOMAIN, "Zone name already in use"));
30907c478bd9Sstevel@tonic-gate 	case Z_NO_SUCH_ID:
30917c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "No such zone ID"));
30927c478bd9Sstevel@tonic-gate 	case Z_UPDATING_INDEX:
30937c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Problem updating index file"));
30947c478bd9Sstevel@tonic-gate 	case Z_LOCKING_FILE:
30957c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Locking index file"));
30967c478bd9Sstevel@tonic-gate 	case Z_UNLOCKING_FILE:
30977c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Unlocking index file"));
30987c478bd9Sstevel@tonic-gate 	case Z_INSUFFICIENT_SPEC:
30997c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Insufficient specification"));
31007c478bd9Sstevel@tonic-gate 	case Z_RESOLVED_PATH:
31017c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Resolved path mismatch"));
31027c478bd9Sstevel@tonic-gate 	case Z_IPV6_ADDR_PREFIX_LEN:
31037c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN,
31047c478bd9Sstevel@tonic-gate 		    "IPv6 address missing required prefix length"));
31057c478bd9Sstevel@tonic-gate 	case Z_BOGUS_ADDRESS:
31067c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN,
31077c478bd9Sstevel@tonic-gate 		    "Neither an IPv4 nor an IPv6 address nor a host name"));
3108ffbafc53Scomay 	case Z_PRIV_PROHIBITED:
3109ffbafc53Scomay 		return (dgettext(TEXT_DOMAIN,
3110ffbafc53Scomay 		    "Specified privilege is prohibited"));
3111ffbafc53Scomay 	case Z_PRIV_REQUIRED:
3112ffbafc53Scomay 		return (dgettext(TEXT_DOMAIN,
3113ffbafc53Scomay 		    "Required privilege is missing"));
3114ffbafc53Scomay 	case Z_PRIV_UNKNOWN:
3115ffbafc53Scomay 		return (dgettext(TEXT_DOMAIN,
3116ffbafc53Scomay 		    "Specified privilege is unknown"));
31179acbbeafSnn 	case Z_BRAND_ERROR:
31189acbbeafSnn 		return (dgettext(TEXT_DOMAIN,
31199acbbeafSnn 		    "Brand-specific error"));
3120*0209230bSgjelinek 	case Z_INCOMPATIBLE:
3121*0209230bSgjelinek 		return (dgettext(TEXT_DOMAIN, "Incompatible settings"));
3122*0209230bSgjelinek 	case Z_ALIAS_DISALLOW:
3123*0209230bSgjelinek 		return (dgettext(TEXT_DOMAIN,
3124*0209230bSgjelinek 		    "An incompatible rctl already exists for this property"));
3125*0209230bSgjelinek 	case Z_CLEAR_DISALLOW:
3126*0209230bSgjelinek 		return (dgettext(TEXT_DOMAIN,
3127*0209230bSgjelinek 		    "Clearing this property is not allowed"));
3128*0209230bSgjelinek 	case Z_POOL:
3129*0209230bSgjelinek 		return (dgettext(TEXT_DOMAIN, "libpool(3LIB) error"));
3130*0209230bSgjelinek 	case Z_POOLS_NOT_ACTIVE:
3131*0209230bSgjelinek 		return (dgettext(TEXT_DOMAIN, "Pools facility not active; "
3132*0209230bSgjelinek 		    "zone will not be bound to pool"));
3133*0209230bSgjelinek 	case Z_POOL_ENABLE:
3134*0209230bSgjelinek 		return (dgettext(TEXT_DOMAIN,
3135*0209230bSgjelinek 		    "Could not enable pools facility"));
3136*0209230bSgjelinek 	case Z_NO_POOL:
3137*0209230bSgjelinek 		return (dgettext(TEXT_DOMAIN,
3138*0209230bSgjelinek 		    "Pool not found; using default pool"));
3139*0209230bSgjelinek 	case Z_POOL_CREATE:
3140*0209230bSgjelinek 		return (dgettext(TEXT_DOMAIN,
3141*0209230bSgjelinek 		    "Could not create a temporary pool"));
3142*0209230bSgjelinek 	case Z_POOL_BIND:
3143*0209230bSgjelinek 		return (dgettext(TEXT_DOMAIN, "Could not bind zone to pool"));
31447c478bd9Sstevel@tonic-gate 	default:
31457c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Unknown error"));
31467c478bd9Sstevel@tonic-gate 	}
31477c478bd9Sstevel@tonic-gate }
31487c478bd9Sstevel@tonic-gate 
31497c478bd9Sstevel@tonic-gate /*
31507c478bd9Sstevel@tonic-gate  * Note that the zonecfg_setXent() and zonecfg_endXent() calls are all the
31517c478bd9Sstevel@tonic-gate  * same, as they just turn around and call zonecfg_setent() / zonecfg_endent().
31527c478bd9Sstevel@tonic-gate  */
31537c478bd9Sstevel@tonic-gate 
31547c478bd9Sstevel@tonic-gate static int
31557c478bd9Sstevel@tonic-gate zonecfg_setent(zone_dochandle_t handle)
31567c478bd9Sstevel@tonic-gate {
31577c478bd9Sstevel@tonic-gate 	xmlNodePtr cur;
31587c478bd9Sstevel@tonic-gate 	int err;
31597c478bd9Sstevel@tonic-gate 
31607c478bd9Sstevel@tonic-gate 	if (handle == NULL)
31617c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
31627c478bd9Sstevel@tonic-gate 
31637c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK) {
31647c478bd9Sstevel@tonic-gate 		handle->zone_dh_cur = NULL;
31657c478bd9Sstevel@tonic-gate 		return (err);
31667c478bd9Sstevel@tonic-gate 	}
31677c478bd9Sstevel@tonic-gate 	cur = handle->zone_dh_cur;
31687c478bd9Sstevel@tonic-gate 	cur = cur->xmlChildrenNode;
31697c478bd9Sstevel@tonic-gate 	handle->zone_dh_cur = cur;
31707c478bd9Sstevel@tonic-gate 	return (Z_OK);
31717c478bd9Sstevel@tonic-gate }
31727c478bd9Sstevel@tonic-gate 
31737c478bd9Sstevel@tonic-gate static int
31747c478bd9Sstevel@tonic-gate zonecfg_endent(zone_dochandle_t handle)
31757c478bd9Sstevel@tonic-gate {
31767c478bd9Sstevel@tonic-gate 	if (handle == NULL)
31777c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
31787c478bd9Sstevel@tonic-gate 
31797c478bd9Sstevel@tonic-gate 	handle->zone_dh_cur = handle->zone_dh_top;
31807c478bd9Sstevel@tonic-gate 	return (Z_OK);
31817c478bd9Sstevel@tonic-gate }
31827c478bd9Sstevel@tonic-gate 
3183*0209230bSgjelinek /*
3184*0209230bSgjelinek  * Do the work required to manipulate a process through libproc.
3185*0209230bSgjelinek  * If grab_process() returns no errors (0), then release_process()
3186*0209230bSgjelinek  * must eventually be called.
3187*0209230bSgjelinek  *
3188*0209230bSgjelinek  * Return values:
3189*0209230bSgjelinek  *      0 Successful creation of agent thread
3190*0209230bSgjelinek  *      1 Error grabbing
3191*0209230bSgjelinek  *      2 Error creating agent
3192*0209230bSgjelinek  */
3193*0209230bSgjelinek static int
3194*0209230bSgjelinek grab_process(pr_info_handle_t *p)
31957c478bd9Sstevel@tonic-gate {
3196*0209230bSgjelinek 	int ret;
3197*0209230bSgjelinek 
3198*0209230bSgjelinek 	if ((p->pr = Pgrab(p->pid, 0, &ret)) != NULL) {
3199*0209230bSgjelinek 
3200*0209230bSgjelinek 		if (Psetflags(p->pr, PR_RLC) != 0) {
3201*0209230bSgjelinek 			Prelease(p->pr, 0);
3202*0209230bSgjelinek 			return (1);
3203*0209230bSgjelinek 		}
3204*0209230bSgjelinek 		if (Pcreate_agent(p->pr) == 0) {
3205*0209230bSgjelinek 			return (0);
3206*0209230bSgjelinek 
3207*0209230bSgjelinek 		} else {
3208*0209230bSgjelinek 			Prelease(p->pr, 0);
3209*0209230bSgjelinek 			return (2);
3210*0209230bSgjelinek 		}
3211*0209230bSgjelinek 	} else {
3212*0209230bSgjelinek 		return (1);
3213*0209230bSgjelinek 	}
32147c478bd9Sstevel@tonic-gate }
32157c478bd9Sstevel@tonic-gate 
3216*0209230bSgjelinek /*
3217*0209230bSgjelinek  * Release the specified process. This destroys the agent
3218*0209230bSgjelinek  * and releases the process. If the process is NULL, nothing
3219*0209230bSgjelinek  * is done. This function should only be called if grab_process()
3220*0209230bSgjelinek  * has previously been called and returned success.
3221*0209230bSgjelinek  *
3222*0209230bSgjelinek  * This function is Pgrab-safe.
3223*0209230bSgjelinek  */
3224*0209230bSgjelinek static void
3225*0209230bSgjelinek release_process(struct ps_prochandle *Pr)
32267c478bd9Sstevel@tonic-gate {
3227*0209230bSgjelinek 	if (Pr == NULL)
3228*0209230bSgjelinek 		return;
32297c478bd9Sstevel@tonic-gate 
3230*0209230bSgjelinek 	Pdestroy_agent(Pr);
3231*0209230bSgjelinek 	Prelease(Pr, 0);
3232*0209230bSgjelinek }
32337c478bd9Sstevel@tonic-gate 
3234*0209230bSgjelinek static boolean_t
3235*0209230bSgjelinek grab_zone_proc(char *zonename, pr_info_handle_t *p)
3236*0209230bSgjelinek {
3237*0209230bSgjelinek 	DIR *dirp;
3238*0209230bSgjelinek 	struct dirent *dentp;
3239*0209230bSgjelinek 	zoneid_t zoneid;
3240*0209230bSgjelinek 	int pid_self;
3241*0209230bSgjelinek 	psinfo_t psinfo;
32427c478bd9Sstevel@tonic-gate 
3243*0209230bSgjelinek 	if (zone_get_id(zonename, &zoneid) != 0)
3244*0209230bSgjelinek 		return (B_FALSE);
32457c478bd9Sstevel@tonic-gate 
3246*0209230bSgjelinek 	pid_self = getpid();
32477c478bd9Sstevel@tonic-gate 
3248*0209230bSgjelinek 	if ((dirp = opendir("/proc")) == NULL)
3249*0209230bSgjelinek 		return (B_FALSE);
32507c478bd9Sstevel@tonic-gate 
3251*0209230bSgjelinek 	while (dentp = readdir(dirp)) {
3252*0209230bSgjelinek 		p->pid = atoi(dentp->d_name);
32537c478bd9Sstevel@tonic-gate 
3254*0209230bSgjelinek 		/* Skip self */
3255*0209230bSgjelinek 		if (p->pid == pid_self)
3256*0209230bSgjelinek 			continue;
32577c478bd9Sstevel@tonic-gate 
3258*0209230bSgjelinek 		if (proc_get_psinfo(p->pid, &psinfo) != 0)
3259*0209230bSgjelinek 			continue;
3260*0209230bSgjelinek 
3261*0209230bSgjelinek 		if (psinfo.pr_zoneid != zoneid)
3262*0209230bSgjelinek 			continue;
3263*0209230bSgjelinek 
3264*0209230bSgjelinek 		/* attempt to grab process */
3265*0209230bSgjelinek 		if (grab_process(p) != 0)
3266*0209230bSgjelinek 			continue;
3267*0209230bSgjelinek 
3268*0209230bSgjelinek 		if (pr_getzoneid(p->pr) != zoneid) {
3269*0209230bSgjelinek 			release_process(p->pr);
3270*0209230bSgjelinek 			continue;
3271*0209230bSgjelinek 		}
3272*0209230bSgjelinek 
3273*0209230bSgjelinek 		(void) closedir(dirp);
3274*0209230bSgjelinek 		return (B_TRUE);
32757c478bd9Sstevel@tonic-gate 	}
32767c478bd9Sstevel@tonic-gate 
3277*0209230bSgjelinek 	(void) closedir(dirp);
3278*0209230bSgjelinek 	return (B_FALSE);
32797c478bd9Sstevel@tonic-gate }
32807c478bd9Sstevel@tonic-gate 
3281*0209230bSgjelinek static boolean_t
3282*0209230bSgjelinek get_priv_rctl(struct ps_prochandle *pr, char *name, rctlblk_t *rblk)
32837c478bd9Sstevel@tonic-gate {
3284*0209230bSgjelinek 	if (pr_getrctl(pr, name, NULL, rblk, RCTL_FIRST))
3285*0209230bSgjelinek 		return (B_FALSE);
32867c478bd9Sstevel@tonic-gate 
3287*0209230bSgjelinek 	if (rctlblk_get_privilege(rblk) == RCPRIV_PRIVILEGED)
3288*0209230bSgjelinek 		return (B_TRUE);
3289*0209230bSgjelinek 
3290*0209230bSgjelinek 	while (pr_getrctl(pr, name, rblk, rblk, RCTL_NEXT) == 0) {
3291*0209230bSgjelinek 		if (rctlblk_get_privilege(rblk) == RCPRIV_PRIVILEGED)
3292*0209230bSgjelinek 			return (B_TRUE);
3293*0209230bSgjelinek 	}
3294*0209230bSgjelinek 
3295*0209230bSgjelinek 	return (B_FALSE);
32967c478bd9Sstevel@tonic-gate }
32977c478bd9Sstevel@tonic-gate 
3298*0209230bSgjelinek /*
3299*0209230bSgjelinek  * Apply the current rctl settings to the specified, running zone.
3300*0209230bSgjelinek  */
33017c478bd9Sstevel@tonic-gate int
3302*0209230bSgjelinek zonecfg_apply_rctls(char *zone_name, zone_dochandle_t handle)
33037c478bd9Sstevel@tonic-gate {
33047c478bd9Sstevel@tonic-gate 	int err;
3305*0209230bSgjelinek 	int res = Z_OK;
3306*0209230bSgjelinek 	rctlblk_t *rblk;
3307*0209230bSgjelinek 	pr_info_handle_t p;
3308*0209230bSgjelinek 	struct zone_rctltab rctl;
33097c478bd9Sstevel@tonic-gate 
3310*0209230bSgjelinek 	if ((err = zonecfg_setrctlent(handle)) != Z_OK)
3311*0209230bSgjelinek 		return (err);
33127c478bd9Sstevel@tonic-gate 
3313*0209230bSgjelinek 	if ((rblk = (rctlblk_t *)malloc(rctlblk_size())) == NULL) {
3314*0209230bSgjelinek 		(void) zonecfg_endrctlent(handle);
3315*0209230bSgjelinek 		return (Z_NOMEM);
3316*0209230bSgjelinek 	}
33177c478bd9Sstevel@tonic-gate 
3318*0209230bSgjelinek 	if (!grab_zone_proc(zone_name, &p)) {
3319*0209230bSgjelinek 		(void) zonecfg_endrctlent(handle);
3320*0209230bSgjelinek 		free(rblk);
3321*0209230bSgjelinek 		return (Z_SYSTEM);
33227c478bd9Sstevel@tonic-gate 	}
33237c478bd9Sstevel@tonic-gate 
3324*0209230bSgjelinek 	while (zonecfg_getrctlent(handle, &rctl) == Z_OK) {
3325*0209230bSgjelinek 		char *rname;
3326*0209230bSgjelinek 		struct zone_rctlvaltab *valptr;
3327*0209230bSgjelinek 
3328*0209230bSgjelinek 		rname = rctl.zone_rctl_name;
3329*0209230bSgjelinek 
3330*0209230bSgjelinek 		/* first delete all current privileged settings for this rctl */
3331*0209230bSgjelinek 		while (get_priv_rctl(p.pr, rname, rblk)) {
3332*0209230bSgjelinek 			if (pr_setrctl(p.pr, rname, NULL, rblk, RCTL_DELETE) !=
3333*0209230bSgjelinek 			    0) {
3334*0209230bSgjelinek 				res = Z_SYSTEM;
3335*0209230bSgjelinek 				goto done;
3336*0209230bSgjelinek 			}
3337*0209230bSgjelinek 		}
3338*0209230bSgjelinek 
3339*0209230bSgjelinek 		/* now set each new value for the rctl */
3340*0209230bSgjelinek 		for (valptr = rctl.zone_rctl_valptr; valptr != NULL;
3341*0209230bSgjelinek 		    valptr = valptr->zone_rctlval_next) {
3342*0209230bSgjelinek 			if ((err = zonecfg_construct_rctlblk(valptr, rblk))
3343*0209230bSgjelinek 			    != Z_OK) {
3344*0209230bSgjelinek 				res = errno = err;
3345*0209230bSgjelinek 				goto done;
3346*0209230bSgjelinek 			}
3347*0209230bSgjelinek 
3348*0209230bSgjelinek 			if (pr_setrctl(p.pr, rname, NULL, rblk, RCTL_INSERT)) {
3349*0209230bSgjelinek 				res = Z_SYSTEM;
3350*0209230bSgjelinek 				goto done;
3351*0209230bSgjelinek 			}
3352*0209230bSgjelinek 		}
33537c478bd9Sstevel@tonic-gate 	}
33547c478bd9Sstevel@tonic-gate 
3355*0209230bSgjelinek done:
3356*0209230bSgjelinek 	release_process(p.pr);
3357*0209230bSgjelinek 	free(rblk);
3358*0209230bSgjelinek 	(void) zonecfg_endrctlent(handle);
3359*0209230bSgjelinek 
3360*0209230bSgjelinek 	return (res);
33617c478bd9Sstevel@tonic-gate }
33627c478bd9Sstevel@tonic-gate 
3363*0209230bSgjelinek static const xmlChar *
3364*0209230bSgjelinek nm_to_dtd(char *nm)
33657c478bd9Sstevel@tonic-gate {
3366*0209230bSgjelinek 	if (strcmp(nm, "device") == 0)
3367*0209230bSgjelinek 		return (DTD_ELEM_DEVICE);
3368*0209230bSgjelinek 	if (strcmp(nm, "fs") == 0)
3369*0209230bSgjelinek 		return (DTD_ELEM_FS);
3370*0209230bSgjelinek 	if (strcmp(nm, "inherit-pkg-dir") == 0)
3371*0209230bSgjelinek 		return (DTD_ELEM_IPD);
3372*0209230bSgjelinek 	if (strcmp(nm, "net") == 0)
3373*0209230bSgjelinek 		return (DTD_ELEM_NET);
3374*0209230bSgjelinek 	if (strcmp(nm, "attr") == 0)
3375*0209230bSgjelinek 		return (DTD_ELEM_ATTR);
3376*0209230bSgjelinek 	if (strcmp(nm, "rctl") == 0)
3377*0209230bSgjelinek 		return (DTD_ELEM_RCTL);
3378*0209230bSgjelinek 	if (strcmp(nm, "dataset") == 0)
3379*0209230bSgjelinek 		return (DTD_ELEM_DATASET);
3380*0209230bSgjelinek 
3381*0209230bSgjelinek 	return (NULL);
33827c478bd9Sstevel@tonic-gate }
33837c478bd9Sstevel@tonic-gate 
33847c478bd9Sstevel@tonic-gate int
3385*0209230bSgjelinek zonecfg_num_resources(zone_dochandle_t handle, char *rsrc)
33867c478bd9Sstevel@tonic-gate {
3387*0209230bSgjelinek 	int num = 0;
3388*0209230bSgjelinek 	const xmlChar *dtd;
3389*0209230bSgjelinek 	xmlNodePtr cur;
3390*0209230bSgjelinek 
3391*0209230bSgjelinek 	if ((dtd = nm_to_dtd(rsrc)) == NULL)
3392*0209230bSgjelinek 		return (num);
3393*0209230bSgjelinek 
3394*0209230bSgjelinek 	if (zonecfg_setent(handle) != Z_OK)
3395*0209230bSgjelinek 		return (num);
3396*0209230bSgjelinek 
3397*0209230bSgjelinek 	for (cur = handle->zone_dh_cur; cur != NULL; cur = cur->next)
3398*0209230bSgjelinek 		if (xmlStrcmp(cur->name, dtd) == 0)
3399*0209230bSgjelinek 			num++;
3400*0209230bSgjelinek 
3401*0209230bSgjelinek 	(void) zonecfg_endent(handle);
3402*0209230bSgjelinek 
3403*0209230bSgjelinek 	return (num);
34047c478bd9Sstevel@tonic-gate }
34057c478bd9Sstevel@tonic-gate 
34067c478bd9Sstevel@tonic-gate int
3407*0209230bSgjelinek zonecfg_del_all_resources(zone_dochandle_t handle, char *rsrc)
34087c478bd9Sstevel@tonic-gate {
34097c478bd9Sstevel@tonic-gate 	int err;
3410*0209230bSgjelinek 	const xmlChar *dtd;
3411*0209230bSgjelinek 	xmlNodePtr cur;
34127c478bd9Sstevel@tonic-gate 
3413*0209230bSgjelinek 	if ((dtd = nm_to_dtd(rsrc)) == NULL)
3414*0209230bSgjelinek 		return (Z_NO_RESOURCE_TYPE);
34157c478bd9Sstevel@tonic-gate 
3416*0209230bSgjelinek 	if ((err = zonecfg_setent(handle)) != Z_OK)
3417*0209230bSgjelinek 		return (err);
34187c478bd9Sstevel@tonic-gate 
3419*0209230bSgjelinek 	cur = handle->zone_dh_cur;
3420*0209230bSgjelinek 	while (cur != NULL) {
3421*0209230bSgjelinek 		xmlNodePtr tmp;
34227c478bd9Sstevel@tonic-gate 
3423*0209230bSgjelinek 		if (xmlStrcmp(cur->name, dtd)) {
3424*0209230bSgjelinek 			cur = cur->next;
3425*0209230bSgjelinek 			continue;
3426*0209230bSgjelinek 		}
34277c478bd9Sstevel@tonic-gate 
3428*0209230bSgjelinek 		tmp = cur->next;
3429*0209230bSgjelinek 		xmlUnlinkNode(cur);
3430*0209230bSgjelinek 		xmlFreeNode(cur);
3431*0209230bSgjelinek 		cur = tmp;
34327c478bd9Sstevel@tonic-gate 	}
34337c478bd9Sstevel@tonic-gate 
3434*0209230bSgjelinek 	(void) zonecfg_endent(handle);
34357c478bd9Sstevel@tonic-gate 	return (Z_OK);
34367c478bd9Sstevel@tonic-gate }
34377c478bd9Sstevel@tonic-gate 
3438*0209230bSgjelinek static boolean_t
3439*0209230bSgjelinek valid_uint(char *s, uint64_t *n)
34407c478bd9Sstevel@tonic-gate {
3441*0209230bSgjelinek 	char *endp;
34427c478bd9Sstevel@tonic-gate 
3443*0209230bSgjelinek 	/* strtoull accepts '-'?! so we want to flag that as an error */
3444*0209230bSgjelinek 	if (strchr(s, '-') != NULL)
3445*0209230bSgjelinek 		return (B_FALSE);
3446*0209230bSgjelinek 
3447*0209230bSgjelinek 	errno = 0;
3448*0209230bSgjelinek 	*n = strtoull(s, &endp, 10);
3449*0209230bSgjelinek 
3450*0209230bSgjelinek 	if (errno != 0 || *endp != '\0')
3451*0209230bSgjelinek 		return (B_FALSE);
3452*0209230bSgjelinek 	return (B_TRUE);
34537c478bd9Sstevel@tonic-gate }
34547c478bd9Sstevel@tonic-gate 
3455*0209230bSgjelinek /*
3456*0209230bSgjelinek  * Convert a string representing a number (possibly a fraction) into an integer.
3457*0209230bSgjelinek  * The string can have a modifier (K, M, G or T).   The modifiers are treated
3458*0209230bSgjelinek  * as powers of two (not 10).
3459*0209230bSgjelinek  */
34607c478bd9Sstevel@tonic-gate int
3461*0209230bSgjelinek zonecfg_str_to_bytes(char *str, uint64_t *bytes)
34627c478bd9Sstevel@tonic-gate {
3463*0209230bSgjelinek 	long double val;
3464*0209230bSgjelinek 	char *unitp;
3465*0209230bSgjelinek 	uint64_t scale;
34667c478bd9Sstevel@tonic-gate 
3467*0209230bSgjelinek 	if ((val = strtold(str, &unitp)) < 0)
3468*0209230bSgjelinek 		return (-1);
34697c478bd9Sstevel@tonic-gate 
3470*0209230bSgjelinek 	/* remove any leading white space from units string */
3471*0209230bSgjelinek 	while (isspace(*unitp) != 0)
3472*0209230bSgjelinek 		++unitp;
34737c478bd9Sstevel@tonic-gate 
3474*0209230bSgjelinek 	/* if no units explicitly set, error */
3475*0209230bSgjelinek 	if (unitp == NULL || *unitp == '\0') {
3476*0209230bSgjelinek 		scale = 1;
3477*0209230bSgjelinek 	} else {
3478*0209230bSgjelinek 		int i;
3479*0209230bSgjelinek 		char *units[] = {"K", "M", "G", "T", NULL};
34807c478bd9Sstevel@tonic-gate 
3481*0209230bSgjelinek 		scale = 1024;
3482*0209230bSgjelinek 
3483*0209230bSgjelinek 		/* update scale based on units */
3484*0209230bSgjelinek 		for (i = 0; units[i] != NULL; i++) {
3485*0209230bSgjelinek 			if (strcasecmp(unitp, units[i]) == 0)
3486*0209230bSgjelinek 				break;
3487*0209230bSgjelinek 			scale <<= 10;
3488*0209230bSgjelinek 		}
3489*0209230bSgjelinek 
3490*0209230bSgjelinek 		if (units[i] == NULL)
3491*0209230bSgjelinek 			return (-1);
34927c478bd9Sstevel@tonic-gate 	}
34937c478bd9Sstevel@tonic-gate 
3494*0209230bSgjelinek 	*bytes = (uint64_t)(val * scale);
3495*0209230bSgjelinek 	return (0);
34967c478bd9Sstevel@tonic-gate }
34977c478bd9Sstevel@tonic-gate 
3498*0209230bSgjelinek boolean_t
3499*0209230bSgjelinek zonecfg_valid_ncpus(char *lowstr, char *highstr)
35007c478bd9Sstevel@tonic-gate {
3501*0209230bSgjelinek 	uint64_t low, high;
35027c478bd9Sstevel@tonic-gate 
3503*0209230bSgjelinek 	if (!valid_uint(lowstr, &low) || !valid_uint(highstr, &high) ||
3504*0209230bSgjelinek 	    low < 1 || low > high)
3505*0209230bSgjelinek 		return (B_FALSE);
3506*0209230bSgjelinek 
3507*0209230bSgjelinek 	return (B_TRUE);
35087c478bd9Sstevel@tonic-gate }
35097c478bd9Sstevel@tonic-gate 
3510*0209230bSgjelinek boolean_t
3511*0209230bSgjelinek zonecfg_valid_importance(char *impstr)
35127c478bd9Sstevel@tonic-gate {
3513*0209230bSgjelinek 	uint64_t num;
35147c478bd9Sstevel@tonic-gate 
3515*0209230bSgjelinek 	if (!valid_uint(impstr, &num))
3516*0209230bSgjelinek 		return (B_FALSE);
35177c478bd9Sstevel@tonic-gate 
3518*0209230bSgjelinek 	return (B_TRUE);
3519*0209230bSgjelinek }
35207c478bd9Sstevel@tonic-gate 
3521*0209230bSgjelinek boolean_t
3522*0209230bSgjelinek zonecfg_valid_alias_limit(char *name, char *limitstr, uint64_t *limit)
3523*0209230bSgjelinek {
3524*0209230bSgjelinek 	int i;
3525*0209230bSgjelinek 
3526*0209230bSgjelinek 	for (i = 0; aliases[i].shortname != NULL; i++)
3527*0209230bSgjelinek 		if (strcmp(name, aliases[i].shortname) == 0)
35287c478bd9Sstevel@tonic-gate 			break;
35297c478bd9Sstevel@tonic-gate 
3530*0209230bSgjelinek 	if (aliases[i].shortname == NULL)
3531*0209230bSgjelinek 		return (B_FALSE);
35327c478bd9Sstevel@tonic-gate 
3533*0209230bSgjelinek 	if (!valid_uint(limitstr, limit) || *limit < aliases[i].low_limit)
3534*0209230bSgjelinek 		return (B_FALSE);
35357c478bd9Sstevel@tonic-gate 
3536*0209230bSgjelinek 	return (B_TRUE);
35377c478bd9Sstevel@tonic-gate }
35387c478bd9Sstevel@tonic-gate 
3539*0209230bSgjelinek boolean_t
3540*0209230bSgjelinek zonecfg_valid_memlimit(char *memstr, uint64_t *mem_val)
35417c478bd9Sstevel@tonic-gate {
3542*0209230bSgjelinek 	if (zonecfg_str_to_bytes(memstr, mem_val) != 0)
3543*0209230bSgjelinek 		return (B_FALSE);
3544*0209230bSgjelinek 
3545*0209230bSgjelinek 	return (B_TRUE);
35467c478bd9Sstevel@tonic-gate }
35477c478bd9Sstevel@tonic-gate 
3548*0209230bSgjelinek static int
3549*0209230bSgjelinek zerr_pool(char *pool_err, int err_size, int res)
35507c478bd9Sstevel@tonic-gate {
3551*0209230bSgjelinek 	(void) strlcpy(pool_err, pool_strerror(pool_error()), err_size);
3552*0209230bSgjelinek 	return (res);
35537c478bd9Sstevel@tonic-gate }
35547c478bd9Sstevel@tonic-gate 
3555*0209230bSgjelinek static int
3556*0209230bSgjelinek create_tmp_pset(char *pool_err, int err_size, pool_conf_t *pconf, pool_t *pool,
3557*0209230bSgjelinek     char *name, int min, int max)
35587c478bd9Sstevel@tonic-gate {
3559*0209230bSgjelinek 	pool_resource_t *res;
3560*0209230bSgjelinek 	pool_elem_t *elem;
3561*0209230bSgjelinek 	pool_value_t *val;
35627c478bd9Sstevel@tonic-gate 
3563*0209230bSgjelinek 	if ((res = pool_resource_create(pconf, "pset", name)) == NULL)
3564*0209230bSgjelinek 		return (zerr_pool(pool_err, err_size, Z_POOL));
35657c478bd9Sstevel@tonic-gate 
3566*0209230bSgjelinek 	if (pool_associate(pconf, pool, res) != PO_SUCCESS)
3567*0209230bSgjelinek 		return (zerr_pool(pool_err, err_size, Z_POOL));
35687c478bd9Sstevel@tonic-gate 
3569*0209230bSgjelinek 	if ((elem = pool_resource_to_elem(pconf, res)) == NULL)
3570*0209230bSgjelinek 		return (zerr_pool(pool_err, err_size, Z_POOL));
35717c478bd9Sstevel@tonic-gate 
3572*0209230bSgjelinek 	if ((val = pool_value_alloc()) == NULL)
3573*0209230bSgjelinek 		return (zerr_pool(pool_err, err_size, Z_POOL));
35747c478bd9Sstevel@tonic-gate 
3575*0209230bSgjelinek 	/* set the maximum number of cpus for the pset */
3576*0209230bSgjelinek 	pool_value_set_uint64(val, (uint64_t)max);
3577*0209230bSgjelinek 
3578*0209230bSgjelinek 	if (pool_put_property(pconf, elem, "pset.max", val) != PO_SUCCESS) {
3579*0209230bSgjelinek 		pool_value_free(val);
3580*0209230bSgjelinek 		return (zerr_pool(pool_err, err_size, Z_POOL));
35817c478bd9Sstevel@tonic-gate 	}
35827c478bd9Sstevel@tonic-gate 
3583*0209230bSgjelinek 	/* set the minimum number of cpus for the pset */
3584*0209230bSgjelinek 	pool_value_set_uint64(val, (uint64_t)min);
3585*0209230bSgjelinek 
3586*0209230bSgjelinek 	if (pool_put_property(pconf, elem, "pset.min", val) != PO_SUCCESS) {
3587*0209230bSgjelinek 		pool_value_free(val);
3588*0209230bSgjelinek 		return (zerr_pool(pool_err, err_size, Z_POOL));
35897c478bd9Sstevel@tonic-gate 	}
35907c478bd9Sstevel@tonic-gate 
3591*0209230bSgjelinek 	pool_value_free(val);
3592*0209230bSgjelinek 
35937c478bd9Sstevel@tonic-gate 	return (Z_OK);
35947c478bd9Sstevel@tonic-gate }
35957c478bd9Sstevel@tonic-gate 
3596*0209230bSgjelinek static int
3597*0209230bSgjelinek create_tmp_pool(char *pool_err, int err_size, pool_conf_t *pconf, char *name,
3598*0209230bSgjelinek     struct zone_psettab *pset_tab)
35997c478bd9Sstevel@tonic-gate {
3600*0209230bSgjelinek 	pool_t *pool;
3601*0209230bSgjelinek 	int res = Z_OK;
36027c478bd9Sstevel@tonic-gate 
3603*0209230bSgjelinek 	/* create a temporary pool configuration */
3604*0209230bSgjelinek 	if (pool_conf_open(pconf, NULL, PO_TEMP) != PO_SUCCESS) {
3605*0209230bSgjelinek 		res = zerr_pool(pool_err, err_size, Z_POOL);
3606*0209230bSgjelinek 		return (res);
3607*0209230bSgjelinek 	}
3608ffbafc53Scomay 
3609*0209230bSgjelinek 	if ((pool = pool_create(pconf, name)) == NULL) {
3610*0209230bSgjelinek 		res = zerr_pool(pool_err, err_size, Z_POOL_CREATE);
3611*0209230bSgjelinek 		goto done;
3612*0209230bSgjelinek 	}
3613ffbafc53Scomay 
3614*0209230bSgjelinek 	/* set pool importance */
3615*0209230bSgjelinek 	if (pset_tab->zone_importance[0] != '\0') {
3616*0209230bSgjelinek 		pool_elem_t *elem;
3617*0209230bSgjelinek 		pool_value_t *val;
36189acbbeafSnn 
3619*0209230bSgjelinek 		if ((elem = pool_to_elem(pconf, pool)) == NULL) {
3620*0209230bSgjelinek 			res = zerr_pool(pool_err, err_size, Z_POOL);
3621*0209230bSgjelinek 			goto done;
3622*0209230bSgjelinek 		}
36239acbbeafSnn 
3624*0209230bSgjelinek 		if ((val = pool_value_alloc()) == NULL) {
3625*0209230bSgjelinek 			res = zerr_pool(pool_err, err_size, Z_POOL);
3626*0209230bSgjelinek 			goto done;
3627*0209230bSgjelinek 		}
36289acbbeafSnn 
3629*0209230bSgjelinek 		pool_value_set_int64(val,
3630*0209230bSgjelinek 		    (int64_t)atoi(pset_tab->zone_importance));
36319acbbeafSnn 
3632*0209230bSgjelinek 		if (pool_put_property(pconf, elem, "pool.importance", val)
3633*0209230bSgjelinek 		    != PO_SUCCESS) {
3634*0209230bSgjelinek 			res = zerr_pool(pool_err, err_size, Z_POOL);
3635*0209230bSgjelinek 			pool_value_free(val);
3636*0209230bSgjelinek 			goto done;
3637*0209230bSgjelinek 		}
36389acbbeafSnn 
3639*0209230bSgjelinek 		pool_value_free(val);
36409acbbeafSnn 	}
36419acbbeafSnn 
3642*0209230bSgjelinek 	if ((res = create_tmp_pset(pool_err, err_size, pconf, pool, name,
3643*0209230bSgjelinek 	    atoi(pset_tab->zone_ncpu_min),
3644*0209230bSgjelinek 	    atoi(pset_tab->zone_ncpu_max))) != Z_OK)
3645*0209230bSgjelinek 		goto done;
3646*0209230bSgjelinek 
3647*0209230bSgjelinek 	/* validation */
3648*0209230bSgjelinek 	if (pool_conf_status(pconf) == POF_INVALID) {
3649*0209230bSgjelinek 		res = zerr_pool(pool_err, err_size, Z_POOL);
3650*0209230bSgjelinek 		goto done;
36519acbbeafSnn 	}
36529acbbeafSnn 
3653*0209230bSgjelinek 	/*
3654*0209230bSgjelinek 	 * This validation is the one we expect to fail if the user specified
3655*0209230bSgjelinek 	 * an invalid configuration (too many cpus) for this system.
3656*0209230bSgjelinek 	 */
3657*0209230bSgjelinek 	if (pool_conf_validate(pconf, POV_RUNTIME) != PO_SUCCESS) {
3658*0209230bSgjelinek 		res = zerr_pool(pool_err, err_size, Z_POOL_CREATE);
3659*0209230bSgjelinek 		goto done;
3660*0209230bSgjelinek 	}
36619acbbeafSnn 
3662*0209230bSgjelinek 	/*
3663*0209230bSgjelinek 	 * Commit the dynamic configuration but not the pool configuration
3664*0209230bSgjelinek 	 * file.
3665*0209230bSgjelinek 	 */
3666*0209230bSgjelinek 	if (pool_conf_commit(pconf, 1) != PO_SUCCESS)
3667*0209230bSgjelinek 		res = zerr_pool(pool_err, err_size, Z_POOL);
36689acbbeafSnn 
3669*0209230bSgjelinek done:
3670*0209230bSgjelinek 	(void) pool_conf_close(pconf);
3671*0209230bSgjelinek 	return (res);
36729acbbeafSnn }
36739acbbeafSnn 
36749acbbeafSnn static int
3675*0209230bSgjelinek get_running_tmp_pset(pool_conf_t *pconf, pool_t *pool, pool_resource_t *pset,
3676*0209230bSgjelinek     struct zone_psettab *pset_tab)
3677*0209230bSgjelinek {
3678*0209230bSgjelinek 	int nfound = 0;
3679*0209230bSgjelinek 	pool_elem_t *pe;
3680*0209230bSgjelinek 	pool_value_t *pv = pool_value_alloc();
3681*0209230bSgjelinek 	uint64_t val_uint;
3682*0209230bSgjelinek 
3683*0209230bSgjelinek 	if (pool != NULL) {
3684*0209230bSgjelinek 		pe = pool_to_elem(pconf, pool);
3685*0209230bSgjelinek 		if (pool_get_property(pconf, pe, "pool.importance", pv)
3686*0209230bSgjelinek 		    != POC_INVAL) {
3687*0209230bSgjelinek 			int64_t val_int;
3688*0209230bSgjelinek 
3689*0209230bSgjelinek 			(void) pool_value_get_int64(pv, &val_int);
3690*0209230bSgjelinek 			(void) snprintf(pset_tab->zone_importance,
3691*0209230bSgjelinek 			    sizeof (pset_tab->zone_importance), "%d", val_int);
3692*0209230bSgjelinek 			nfound++;
3693*0209230bSgjelinek 		}
36949acbbeafSnn 	}
36959acbbeafSnn 
3696*0209230bSgjelinek 	if (pset != NULL) {
3697*0209230bSgjelinek 		pe = pool_resource_to_elem(pconf, pset);
3698*0209230bSgjelinek 		if (pool_get_property(pconf, pe, "pset.min", pv) != POC_INVAL) {
3699*0209230bSgjelinek 			(void) pool_value_get_uint64(pv, &val_uint);
3700*0209230bSgjelinek 			(void) snprintf(pset_tab->zone_ncpu_min,
3701*0209230bSgjelinek 			    sizeof (pset_tab->zone_ncpu_min), "%u", val_uint);
3702*0209230bSgjelinek 			nfound++;
3703*0209230bSgjelinek 		}
37049acbbeafSnn 
3705*0209230bSgjelinek 		if (pool_get_property(pconf, pe, "pset.max", pv) != POC_INVAL) {
3706*0209230bSgjelinek 			(void) pool_value_get_uint64(pv, &val_uint);
3707*0209230bSgjelinek 			(void) snprintf(pset_tab->zone_ncpu_max,
3708*0209230bSgjelinek 			    sizeof (pset_tab->zone_ncpu_max), "%u", val_uint);
3709*0209230bSgjelinek 			nfound++;
3710*0209230bSgjelinek 		}
37119acbbeafSnn 	}
37129acbbeafSnn 
3713*0209230bSgjelinek 	pool_value_free(pv);
37149acbbeafSnn 
3715*0209230bSgjelinek 	if (nfound == 3)
3716*0209230bSgjelinek 		return (PO_SUCCESS);
3717*0209230bSgjelinek 
3718*0209230bSgjelinek 	return (PO_FAIL);
37199acbbeafSnn }
37209acbbeafSnn 
3721*0209230bSgjelinek /*
3722*0209230bSgjelinek  * Determine if a tmp pool is configured and if so, if the configuration is
3723*0209230bSgjelinek  * still valid or if it has been changed since the tmp pool was created.
3724*0209230bSgjelinek  * If the tmp pool configuration is no longer valid, delete the tmp pool.
3725*0209230bSgjelinek  *
3726*0209230bSgjelinek  * Set *valid=B_TRUE if there is an existing, valid tmp pool configuration.
3727*0209230bSgjelinek  */
37289acbbeafSnn static int
3729*0209230bSgjelinek verify_del_tmp_pool(pool_conf_t *pconf, char *tmp_name, char *pool_err,
3730*0209230bSgjelinek     int err_size, struct zone_psettab *pset_tab, boolean_t *exists)
37319acbbeafSnn {
3732*0209230bSgjelinek 	int res = Z_OK;
3733*0209230bSgjelinek 	pool_t *pool;
3734*0209230bSgjelinek 	pool_resource_t *pset;
3735*0209230bSgjelinek 	struct zone_psettab pset_current;
37367c478bd9Sstevel@tonic-gate 
3737*0209230bSgjelinek 	*exists = B_FALSE;
37387c478bd9Sstevel@tonic-gate 
3739*0209230bSgjelinek 	if (pool_conf_open(pconf, pool_dynamic_location(), PO_RDWR)
3740*0209230bSgjelinek 	    != PO_SUCCESS) {
3741*0209230bSgjelinek 		res = zerr_pool(pool_err, err_size, Z_POOL);
3742*0209230bSgjelinek 		return (res);
3743*0209230bSgjelinek 	}
37447c478bd9Sstevel@tonic-gate 
3745*0209230bSgjelinek 	pool = pool_get_pool(pconf, tmp_name);
3746*0209230bSgjelinek 	pset = pool_get_resource(pconf, "pset", tmp_name);
3747*0209230bSgjelinek 
3748*0209230bSgjelinek 	if (pool == NULL && pset == NULL) {
3749*0209230bSgjelinek 		/* no tmp pool configured */
3750*0209230bSgjelinek 		goto done;
37517c478bd9Sstevel@tonic-gate 	}
37529acbbeafSnn 
3753*0209230bSgjelinek 	/*
3754*0209230bSgjelinek 	 * If an existing tmp pool for this zone is configured with the proper
3755*0209230bSgjelinek 	 * settings, then the tmp pool is valid.
3756*0209230bSgjelinek 	 */
3757*0209230bSgjelinek 	if (get_running_tmp_pset(pconf, pool, pset, &pset_current)
3758*0209230bSgjelinek 	    == PO_SUCCESS &&
3759*0209230bSgjelinek 	    strcmp(pset_tab->zone_ncpu_min,
3760*0209230bSgjelinek 	    pset_current.zone_ncpu_min) == 0 &&
3761*0209230bSgjelinek 	    strcmp(pset_tab->zone_ncpu_max,
3762*0209230bSgjelinek 	    pset_current.zone_ncpu_max) == 0 &&
3763*0209230bSgjelinek 	    strcmp(pset_tab->zone_importance,
3764*0209230bSgjelinek 	    pset_current.zone_importance) == 0) {
3765*0209230bSgjelinek 		*exists = B_TRUE;
37667c478bd9Sstevel@tonic-gate 
3767*0209230bSgjelinek 	} else {
3768*0209230bSgjelinek 		/*
3769*0209230bSgjelinek 		 * An out-of-date tmp pool configuration exists.  Delete it
3770*0209230bSgjelinek 		 * so that we can create the correct tmp pool config.
3771*0209230bSgjelinek 		 */
3772*0209230bSgjelinek 		if (pset != NULL &&
3773*0209230bSgjelinek 		    pool_resource_destroy(pconf, pset) != PO_SUCCESS) {
3774*0209230bSgjelinek 			res = zerr_pool(pool_err, err_size, Z_POOL);
3775*0209230bSgjelinek 			goto done;
3776*0209230bSgjelinek 		}
37779acbbeafSnn 
3778*0209230bSgjelinek 		if (pool != NULL &&
3779*0209230bSgjelinek 		    pool_destroy(pconf, pool) != PO_SUCCESS) {
3780*0209230bSgjelinek 			res = zerr_pool(pool_err, err_size, Z_POOL);
3781*0209230bSgjelinek 			goto done;
3782*0209230bSgjelinek 		}
37839acbbeafSnn 
3784*0209230bSgjelinek 		/* commit dynamic config */
3785*0209230bSgjelinek 		if (pool_conf_commit(pconf, 0) != PO_SUCCESS)
3786*0209230bSgjelinek 			res = zerr_pool(pool_err, err_size, Z_POOL);
3787*0209230bSgjelinek 	}
3788*0209230bSgjelinek 
3789*0209230bSgjelinek done:
3790*0209230bSgjelinek 	(void) pool_conf_close(pconf);
3791*0209230bSgjelinek 
3792*0209230bSgjelinek 	return (res);
3793ffbafc53Scomay }
3794ffbafc53Scomay 
3795ffbafc53Scomay /*
3796*0209230bSgjelinek  * Destroy any existing tmp pool.
3797ffbafc53Scomay  */
3798*0209230bSgjelinek int
3799*0209230bSgjelinek zonecfg_destroy_tmp_pool(char *zone_name, char *pool_err, int err_size)
3800ffbafc53Scomay {
3801*0209230bSgjelinek 	int status;
3802*0209230bSgjelinek 	int res = Z_OK;
3803*0209230bSgjelinek 	pool_conf_t *pconf;
3804*0209230bSgjelinek 	pool_t *pool;
3805*0209230bSgjelinek 	pool_resource_t *pset;
3806*0209230bSgjelinek 	char tmp_name[MAX_TMP_POOL_NAME];
3807ffbafc53Scomay 
3808*0209230bSgjelinek 	/* if pools not enabled then nothing to do */
3809*0209230bSgjelinek 	if (pool_get_status(&status) != PO_SUCCESS || status != POOL_ENABLED)
3810*0209230bSgjelinek 		return (Z_OK);
3811ffbafc53Scomay 
3812*0209230bSgjelinek 	if ((pconf = pool_conf_alloc()) == NULL)
3813*0209230bSgjelinek 		return (zerr_pool(pool_err, err_size, Z_POOL));
38149acbbeafSnn 
3815*0209230bSgjelinek 	(void) snprintf(tmp_name, sizeof (tmp_name), TMP_POOL_NAME, zone_name);
3816ffbafc53Scomay 
3817*0209230bSgjelinek 	if (pool_conf_open(pconf, pool_dynamic_location(), PO_RDWR)
3818*0209230bSgjelinek 	    != PO_SUCCESS) {
3819*0209230bSgjelinek 		res = zerr_pool(pool_err, err_size, Z_POOL);
3820*0209230bSgjelinek 		pool_conf_free(pconf);
3821*0209230bSgjelinek 		return (res);
3822ffbafc53Scomay 	}
3823ffbafc53Scomay 
3824*0209230bSgjelinek 	pool = pool_get_pool(pconf, tmp_name);
3825*0209230bSgjelinek 	pset = pool_get_resource(pconf, "pset", tmp_name);
3826ffbafc53Scomay 
3827*0209230bSgjelinek 	if (pool == NULL && pset == NULL) {
3828*0209230bSgjelinek 		/* nothing to destroy, we're done */
3829*0209230bSgjelinek 		goto done;
3830ffbafc53Scomay 	}
3831ffbafc53Scomay 
3832*0209230bSgjelinek 	if (pset != NULL && pool_resource_destroy(pconf, pset) != PO_SUCCESS) {
3833*0209230bSgjelinek 		res = zerr_pool(pool_err, err_size, Z_POOL);
3834*0209230bSgjelinek 		goto done;
3835ffbafc53Scomay 	}
3836ffbafc53Scomay 
3837*0209230bSgjelinek 	if (pool != NULL && pool_destroy(pconf, pool) != PO_SUCCESS) {
3838*0209230bSgjelinek 		res = zerr_pool(pool_err, err_size, Z_POOL);
3839*0209230bSgjelinek 		goto done;
3840ffbafc53Scomay 	}
3841ffbafc53Scomay 
3842*0209230bSgjelinek 	/* commit dynamic config */
3843*0209230bSgjelinek 	if (pool_conf_commit(pconf, 0) != PO_SUCCESS)
3844*0209230bSgjelinek 		res = zerr_pool(pool_err, err_size, Z_POOL);
3845*0209230bSgjelinek 
3846*0209230bSgjelinek done:
3847*0209230bSgjelinek 	(void) pool_conf_close(pconf);
3848*0209230bSgjelinek 	pool_conf_free(pconf);
3849*0209230bSgjelinek 
3850*0209230bSgjelinek 	return (res);
3851ffbafc53Scomay }
3852ffbafc53Scomay 
3853ffbafc53Scomay /*
3854*0209230bSgjelinek  * Attempt to bind to a tmp pool for this zone.  If there is no tmp pool
3855*0209230bSgjelinek  * configured, we just return Z_OK.
3856ffbafc53Scomay  *
3857*0209230bSgjelinek  * We either attempt to create the tmp pool for this zone or rebind to an
3858*0209230bSgjelinek  * existing tmp pool for this zone.
3859*0209230bSgjelinek  *
3860*0209230bSgjelinek  * Rebinding is used when a zone with a tmp pool reboots so that we don't have
3861*0209230bSgjelinek  * to recreate the tmp pool.  To do this we need to be sure we work correctly
3862*0209230bSgjelinek  * for the following cases:
3863*0209230bSgjelinek  *
3864*0209230bSgjelinek  *	- there is an existing, properly configured tmp pool.
3865*0209230bSgjelinek  *	- zonecfg added tmp pool after zone was booted, must now create.
3866*0209230bSgjelinek  *	- zonecfg updated tmp pool config after zone was booted, in this case
3867*0209230bSgjelinek  *	  we destroy the old tmp pool and create a new one.
3868ffbafc53Scomay  */
3869ffbafc53Scomay int
3870*0209230bSgjelinek zonecfg_bind_tmp_pool(zone_dochandle_t handle, zoneid_t zoneid, char *pool_err,
3871*0209230bSgjelinek     int err_size)
3872ffbafc53Scomay {
3873*0209230bSgjelinek 	struct zone_psettab pset_tab;
3874*0209230bSgjelinek 	int err;
3875*0209230bSgjelinek 	int status;
3876*0209230bSgjelinek 	pool_conf_t *pconf;
3877*0209230bSgjelinek 	boolean_t exists;
3878*0209230bSgjelinek 	char zone_name[ZONENAME_MAX];
3879*0209230bSgjelinek 	char tmp_name[MAX_TMP_POOL_NAME];
3880*0209230bSgjelinek 
3881*0209230bSgjelinek 	(void) getzonenamebyid(zoneid, zone_name, sizeof (zone_name));
3882*0209230bSgjelinek 
3883*0209230bSgjelinek 	err = zonecfg_lookup_pset(handle, &pset_tab);
3884*0209230bSgjelinek 
3885*0209230bSgjelinek 	/* if no temporary pool configured, we're done */
3886*0209230bSgjelinek 	if (err == Z_NO_ENTRY)
3887*0209230bSgjelinek 		return (Z_OK);
3888ffbafc53Scomay 
3889ffbafc53Scomay 	/*
3890*0209230bSgjelinek 	 * importance might not have a value but we need to validate it here,
3891*0209230bSgjelinek 	 * so set the default.
3892ffbafc53Scomay 	 */
3893*0209230bSgjelinek 	if (pset_tab.zone_importance[0] == '\0')
3894*0209230bSgjelinek 		(void) strlcpy(pset_tab.zone_importance, "1",
3895*0209230bSgjelinek 		    sizeof (pset_tab.zone_importance));
38969acbbeafSnn 
3897*0209230bSgjelinek 	/* if pools not enabled, enable them now */
3898*0209230bSgjelinek 	if (pool_get_status(&status) != PO_SUCCESS || status != POOL_ENABLED) {
3899*0209230bSgjelinek 		if (pool_set_status(POOL_ENABLED) != PO_SUCCESS)
3900*0209230bSgjelinek 			return (Z_POOL_ENABLE);
3901ffbafc53Scomay 	}
3902ffbafc53Scomay 
3903*0209230bSgjelinek 	if ((pconf = pool_conf_alloc()) == NULL)
3904*0209230bSgjelinek 		return (zerr_pool(pool_err, err_size, Z_POOL));
3905*0209230bSgjelinek 
3906*0209230bSgjelinek 	(void) snprintf(tmp_name, sizeof (tmp_name), TMP_POOL_NAME, zone_name);
3907*0209230bSgjelinek 
3908ffbafc53Scomay 	/*
3909*0209230bSgjelinek 	 * Check if a valid tmp pool/pset already exists.  If so, we just
3910*0209230bSgjelinek 	 * reuse it.
3911ffbafc53Scomay 	 */
3912*0209230bSgjelinek 	if ((err = verify_del_tmp_pool(pconf, tmp_name, pool_err, err_size,
3913*0209230bSgjelinek 	    &pset_tab, &exists)) != Z_OK) {
3914*0209230bSgjelinek 		pool_conf_free(pconf);
3915*0209230bSgjelinek 		return (err);
3916*0209230bSgjelinek 	}
3917ffbafc53Scomay 
3918*0209230bSgjelinek 	if (!exists)
3919*0209230bSgjelinek 		err = create_tmp_pool(pool_err, err_size, pconf, tmp_name,
3920*0209230bSgjelinek 		    &pset_tab);
3921*0209230bSgjelinek 
3922*0209230bSgjelinek 	pool_conf_free(pconf);
3923*0209230bSgjelinek 
3924*0209230bSgjelinek 	if (err != Z_OK)
3925*0209230bSgjelinek 		return (err);
3926*0209230bSgjelinek 
3927*0209230bSgjelinek 	/* Bind the zone to the pool. */
3928*0209230bSgjelinek 	if (pool_set_binding(tmp_name, P_ZONEID, zoneid) != PO_SUCCESS)
3929*0209230bSgjelinek 		return (zerr_pool(pool_err, err_size, Z_POOL_BIND));
3930*0209230bSgjelinek 
3931*0209230bSgjelinek 	return (Z_OK);
3932ffbafc53Scomay }
3933ffbafc53Scomay 
3934*0209230bSgjelinek /*
3935*0209230bSgjelinek  * Attempt to bind to a permanent pool for this zone.  If there is no
3936*0209230bSgjelinek  * permanent pool configured, we just return Z_OK.
3937*0209230bSgjelinek  */
39387c478bd9Sstevel@tonic-gate int
3939*0209230bSgjelinek zonecfg_bind_pool(zone_dochandle_t handle, zoneid_t zoneid, char *pool_err,
3940*0209230bSgjelinek     int err_size)
39417c478bd9Sstevel@tonic-gate {
3942*0209230bSgjelinek 	pool_conf_t *poolconf;
3943*0209230bSgjelinek 	pool_t *pool;
3944*0209230bSgjelinek 	char poolname[MAXPATHLEN];
3945*0209230bSgjelinek 	int status;
3946*0209230bSgjelinek 	int error;
3947108322fbScarlsonj 
3948*0209230bSgjelinek 	/*
3949*0209230bSgjelinek 	 * Find the pool mentioned in the zone configuration, and bind to it.
3950*0209230bSgjelinek 	 */
3951*0209230bSgjelinek 	error = zonecfg_get_pool(handle, poolname, sizeof (poolname));
3952*0209230bSgjelinek 	if (error == Z_NO_ENTRY || (error == Z_OK && strlen(poolname) == 0)) {
3953*0209230bSgjelinek 		/*
3954*0209230bSgjelinek 		 * The property is not set on the zone, so the pool
3955*0209230bSgjelinek 		 * should be bound to the default pool.  But that's
3956*0209230bSgjelinek 		 * already done by the kernel, so we can just return.
3957*0209230bSgjelinek 		 */
39587c478bd9Sstevel@tonic-gate 		return (Z_OK);
39597c478bd9Sstevel@tonic-gate 	}
3960*0209230bSgjelinek 	if (error != Z_OK) {
3961*0209230bSgjelinek 		/*
3962*0209230bSgjelinek 		 * Not an error, even though it shouldn't be happening.
3963*0209230bSgjelinek 		 */
3964*0209230bSgjelinek 		return (Z_OK);
3965*0209230bSgjelinek 	}
3966*0209230bSgjelinek 	/*
3967*0209230bSgjelinek 	 * Don't do anything if pools aren't enabled.
3968*0209230bSgjelinek 	 */
3969*0209230bSgjelinek 	if (pool_get_status(&status) != PO_SUCCESS || status != POOL_ENABLED)
3970*0209230bSgjelinek 		return (Z_POOLS_NOT_ACTIVE);
39717c478bd9Sstevel@tonic-gate 
39727c478bd9Sstevel@tonic-gate 	/*
3973*0209230bSgjelinek 	 * Try to provide a sane error message if the requested pool doesn't
3974*0209230bSgjelinek 	 * exist.
39757c478bd9Sstevel@tonic-gate 	 */
3976*0209230bSgjelinek 	if ((poolconf = pool_conf_alloc()) == NULL)
3977*0209230bSgjelinek 		return (zerr_pool(pool_err, err_size, Z_POOL));
39787c478bd9Sstevel@tonic-gate 
3979*0209230bSgjelinek 	if (pool_conf_open(poolconf, pool_dynamic_location(), PO_RDONLY) !=
3980*0209230bSgjelinek 	    PO_SUCCESS) {
3981*0209230bSgjelinek 		pool_conf_free(poolconf);
3982*0209230bSgjelinek 		return (zerr_pool(pool_err, err_size, Z_POOL));
3983*0209230bSgjelinek 	}
3984*0209230bSgjelinek 	pool = pool_get_pool(poolconf, poolname);
3985*0209230bSgjelinek 	(void) pool_conf_close(poolconf);
3986*0209230bSgjelinek 	pool_conf_free(poolconf);
3987*0209230bSgjelinek 	if (pool == NULL)
3988*0209230bSgjelinek 		return (Z_NO_POOL);
39897c478bd9Sstevel@tonic-gate 
39907c478bd9Sstevel@tonic-gate 	/*
3991*0209230bSgjelinek 	 * Bind the zone to the pool.
39927c478bd9Sstevel@tonic-gate 	 */
3993*0209230bSgjelinek 	if (pool_set_binding(poolname, P_ZONEID, zoneid) != PO_SUCCESS) {
3994*0209230bSgjelinek 		/* if bind fails, return poolname for the error msg */
3995*0209230bSgjelinek 		(void) strlcpy(pool_err, poolname, err_size);
3996*0209230bSgjelinek 		return (Z_POOL_BIND);
39977c478bd9Sstevel@tonic-gate 	}
3998*0209230bSgjelinek 
3999*0209230bSgjelinek 	return (Z_OK);
4000*0209230bSgjelinek }
4001*0209230bSgjelinek 
4002*0209230bSgjelinek 
4003*0209230bSgjelinek static boolean_t
4004*0209230bSgjelinek svc_enabled(char *svc_name)
4005*0209230bSgjelinek {
4006*0209230bSgjelinek 	scf_simple_prop_t	*prop;
4007*0209230bSgjelinek 	boolean_t		found = B_FALSE;
4008*0209230bSgjelinek 
4009*0209230bSgjelinek 	prop = scf_simple_prop_get(NULL, svc_name, SCF_PG_GENERAL,
4010*0209230bSgjelinek 	    SCF_PROPERTY_ENABLED);
4011*0209230bSgjelinek 
4012*0209230bSgjelinek 	if (scf_simple_prop_numvalues(prop) == 1 &&
4013*0209230bSgjelinek 	    *scf_simple_prop_next_boolean(prop) != 0)
4014*0209230bSgjelinek 		found = B_TRUE;
4015*0209230bSgjelinek 
4016*0209230bSgjelinek 	scf_simple_prop_free(prop);
4017*0209230bSgjelinek 
4018*0209230bSgjelinek 	return (found);
40197c478bd9Sstevel@tonic-gate }
40207c478bd9Sstevel@tonic-gate 
4021*0209230bSgjelinek /*
4022*0209230bSgjelinek  * If the zone has capped-memory, make sure the rcap service is enabled.
4023*0209230bSgjelinek  */
40247c478bd9Sstevel@tonic-gate int
4025*0209230bSgjelinek zonecfg_enable_rcapd(char *err, int size)
40267c478bd9Sstevel@tonic-gate {
4027*0209230bSgjelinek 	if (!svc_enabled(RCAP_SERVICE) &&
4028*0209230bSgjelinek 	    smf_enable_instance(RCAP_SERVICE, 0) == -1) {
4029*0209230bSgjelinek 		(void) strlcpy(err, scf_strerror(scf_error()), size);
4030*0209230bSgjelinek 		return (Z_SYSTEM);
4031*0209230bSgjelinek 	}
40327c478bd9Sstevel@tonic-gate 
40337c478bd9Sstevel@tonic-gate 	return (Z_OK);
40347c478bd9Sstevel@tonic-gate }
40357c478bd9Sstevel@tonic-gate 
4036*0209230bSgjelinek /*
4037*0209230bSgjelinek  * Return true if pset has cpu range specified and poold is not enabled.
4038*0209230bSgjelinek  */
4039*0209230bSgjelinek boolean_t
4040*0209230bSgjelinek zonecfg_warn_poold(zone_dochandle_t handle)
40419acbbeafSnn {
4042*0209230bSgjelinek 	struct zone_psettab pset_tab;
4043*0209230bSgjelinek 	int min, max;
40449acbbeafSnn 	int err;
40459acbbeafSnn 
4046*0209230bSgjelinek 	err = zonecfg_lookup_pset(handle, &pset_tab);
4047*0209230bSgjelinek 
4048*0209230bSgjelinek 	/* if no temporary pool configured, we're done */
4049*0209230bSgjelinek 	if (err == Z_NO_ENTRY)
4050*0209230bSgjelinek 		return (B_FALSE);
4051*0209230bSgjelinek 
4052*0209230bSgjelinek 	min = atoi(pset_tab.zone_ncpu_min);
4053*0209230bSgjelinek 	max = atoi(pset_tab.zone_ncpu_max);
4054*0209230bSgjelinek 
4055*0209230bSgjelinek 	/* range not specified, no need for poold */
4056*0209230bSgjelinek 	if (min == max)
4057*0209230bSgjelinek 		return (B_FALSE);
4058*0209230bSgjelinek 
4059*0209230bSgjelinek 	/* we have a range, check if poold service is enabled */
4060*0209230bSgjelinek 	if (svc_enabled(POOLD_SERVICE))
4061*0209230bSgjelinek 		return (B_FALSE);
4062*0209230bSgjelinek 
4063*0209230bSgjelinek 	return (B_TRUE);
4064*0209230bSgjelinek }
4065*0209230bSgjelinek 
4066*0209230bSgjelinek static int
4067*0209230bSgjelinek get_pool_sched_class(char *poolname, char *class, int clsize)
4068*0209230bSgjelinek {
4069*0209230bSgjelinek 	int status;
4070*0209230bSgjelinek 	pool_conf_t *poolconf;
4071*0209230bSgjelinek 	pool_t *pool;
4072*0209230bSgjelinek 	pool_elem_t *pe;
4073*0209230bSgjelinek 	pool_value_t *pv = pool_value_alloc();
4074*0209230bSgjelinek 	const char *sched_str;
4075*0209230bSgjelinek 
4076*0209230bSgjelinek 	if (pool_get_status(&status) != PO_SUCCESS || status != POOL_ENABLED)
4077*0209230bSgjelinek 		return (Z_NO_POOL);
4078*0209230bSgjelinek 
4079*0209230bSgjelinek 	if ((poolconf = pool_conf_alloc()) == NULL)
4080*0209230bSgjelinek 		return (Z_NO_POOL);
4081*0209230bSgjelinek 
4082*0209230bSgjelinek 	if (pool_conf_open(poolconf, pool_dynamic_location(), PO_RDONLY) !=
4083*0209230bSgjelinek 	    PO_SUCCESS) {
4084*0209230bSgjelinek 		pool_conf_free(poolconf);
4085*0209230bSgjelinek 		return (Z_NO_POOL);
40869acbbeafSnn 	}
40879acbbeafSnn 
4088*0209230bSgjelinek 	if ((pool = pool_get_pool(poolconf, poolname)) == NULL) {
4089*0209230bSgjelinek 		(void) pool_conf_close(poolconf);
4090*0209230bSgjelinek 		pool_conf_free(poolconf);
4091*0209230bSgjelinek 		return (Z_NO_POOL);
40929acbbeafSnn 	}
40939acbbeafSnn 
4094*0209230bSgjelinek 	pe = pool_to_elem(poolconf, pool);
4095*0209230bSgjelinek 	if (pool_get_property(poolconf, pe, "pool.scheduler", pv)
4096*0209230bSgjelinek 	    != POC_INVAL) {
4097*0209230bSgjelinek 		(void) pool_value_get_string(pv, &sched_str);
4098*0209230bSgjelinek 		if (strlcpy(class, sched_str, clsize) >= clsize)
4099*0209230bSgjelinek 			return (Z_TOO_BIG);
4100*0209230bSgjelinek 	}
41019acbbeafSnn 
4102*0209230bSgjelinek 	(void) pool_conf_close(poolconf);
4103*0209230bSgjelinek 	pool_conf_free(poolconf);
4104*0209230bSgjelinek 	return (Z_OK);
41059acbbeafSnn }
41069acbbeafSnn 
4107facf4a8dSllai /*
4108*0209230bSgjelinek  * Get the default scheduling class for the zone.  This will either be the
4109*0209230bSgjelinek  * class set on the zone's pool or the system default scheduling class.
4110facf4a8dSllai  */
4111facf4a8dSllai int
4112*0209230bSgjelinek zonecfg_get_dflt_sched_class(zone_dochandle_t handle, char *class, int clsize)
4113facf4a8dSllai {
4114*0209230bSgjelinek 	char poolname[MAXPATHLEN];
4115facf4a8dSllai 
4116*0209230bSgjelinek 	if (zonecfg_get_pool(handle, poolname, sizeof (poolname)) == Z_OK) {
4117*0209230bSgjelinek 		/* check if the zone's pool specified a sched class */
4118*0209230bSgjelinek 		if (get_pool_sched_class(poolname, class, clsize) == Z_OK)
4119*0209230bSgjelinek 			return (Z_OK);
4120*0209230bSgjelinek 	}
4121facf4a8dSllai 
4122*0209230bSgjelinek 	if (priocntl(0, 0, PC_GETDFLCL, class, (uint64_t)clsize) == -1)
4123facf4a8dSllai 		return (Z_TOO_BIG);
4124*0209230bSgjelinek 
4125facf4a8dSllai 	return (Z_OK);
4126facf4a8dSllai }
4127facf4a8dSllai 
4128*0209230bSgjelinek int
4129*0209230bSgjelinek zonecfg_setfsent(zone_dochandle_t handle)
41307c478bd9Sstevel@tonic-gate {
4131*0209230bSgjelinek 	return (zonecfg_setent(handle));
41327c478bd9Sstevel@tonic-gate }
41337c478bd9Sstevel@tonic-gate 
41347c478bd9Sstevel@tonic-gate int
4135*0209230bSgjelinek zonecfg_getfsent(zone_dochandle_t handle, struct zone_fstab *tabptr)
41367c478bd9Sstevel@tonic-gate {
4137*0209230bSgjelinek 	xmlNodePtr cur, options;
4138*0209230bSgjelinek 	char options_str[MAX_MNTOPT_STR];
4139*0209230bSgjelinek 	int err;
41407c478bd9Sstevel@tonic-gate 
4141*0209230bSgjelinek 	if (handle == NULL)
41427c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
41437c478bd9Sstevel@tonic-gate 
4144*0209230bSgjelinek 	if ((cur = handle->zone_dh_cur) == NULL)
4145*0209230bSgjelinek 		return (Z_NO_ENTRY);
4146*0209230bSgjelinek 
4147*0209230bSgjelinek 	for (; cur != NULL; cur = cur->next)
4148*0209230bSgjelinek 		if (!xmlStrcmp(cur->name, DTD_ELEM_FS))
4149*0209230bSgjelinek 			break;
4150*0209230bSgjelinek 	if (cur == NULL) {
4151*0209230bSgjelinek 		handle->zone_dh_cur = handle->zone_dh_top;
4152*0209230bSgjelinek 		return (Z_NO_ENTRY);
4153108322fbScarlsonj 	}
4154108322fbScarlsonj 
4155*0209230bSgjelinek 	if ((err = fetchprop(cur, DTD_ATTR_SPECIAL, tabptr->zone_fs_special,
4156*0209230bSgjelinek 	    sizeof (tabptr->zone_fs_special))) != Z_OK) {
4157*0209230bSgjelinek 		handle->zone_dh_cur = handle->zone_dh_top;
4158*0209230bSgjelinek 		return (err);
41597c478bd9Sstevel@tonic-gate 	}
41607c478bd9Sstevel@tonic-gate 
4161*0209230bSgjelinek 	if ((err = fetchprop(cur, DTD_ATTR_RAW, tabptr->zone_fs_raw,
4162*0209230bSgjelinek 	    sizeof (tabptr->zone_fs_raw))) != Z_OK) {
4163*0209230bSgjelinek 		handle->zone_dh_cur = handle->zone_dh_top;
4164*0209230bSgjelinek 		return (err);
4165*0209230bSgjelinek 	}
4166*0209230bSgjelinek 
4167*0209230bSgjelinek 	if ((err = fetchprop(cur, DTD_ATTR_DIR, tabptr->zone_fs_dir,
4168*0209230bSgjelinek 	    sizeof (tabptr->zone_fs_dir))) != Z_OK) {
4169*0209230bSgjelinek 		handle->zone_dh_cur = handle->zone_dh_top;
4170*0209230bSgjelinek 		return (err);
4171*0209230bSgjelinek 	}
4172*0209230bSgjelinek 
4173*0209230bSgjelinek 	if ((err = fetchprop(cur, DTD_ATTR_TYPE, tabptr->zone_fs_type,
4174*0209230bSgjelinek 	    sizeof (tabptr->zone_fs_type))) != Z_OK) {
4175*0209230bSgjelinek 		handle->zone_dh_cur = handle->zone_dh_top;
4176*0209230bSgjelinek 		return (err);
4177*0209230bSgjelinek 	}
4178*0209230bSgjelinek 
4179*0209230bSgjelinek 	/* OK for options to be NULL */
4180*0209230bSgjelinek 	tabptr->zone_fs_options = NULL;
4181*0209230bSgjelinek 	for (options = cur->xmlChildrenNode; options != NULL;
4182*0209230bSgjelinek 	    options = options->next) {
4183*0209230bSgjelinek 		if (fetchprop(options, DTD_ATTR_NAME, options_str,
4184*0209230bSgjelinek 		    sizeof (options_str)) != Z_OK)
4185*0209230bSgjelinek 			break;
4186*0209230bSgjelinek 		if (zonecfg_add_fs_option(tabptr, options_str) != Z_OK)
41877c478bd9Sstevel@tonic-gate 			break;
41887c478bd9Sstevel@tonic-gate 	}
4189*0209230bSgjelinek 
4190*0209230bSgjelinek 	handle->zone_dh_cur = cur->next;
4191*0209230bSgjelinek 	return (Z_OK);
41927c478bd9Sstevel@tonic-gate }
41937c478bd9Sstevel@tonic-gate 
41947c478bd9Sstevel@tonic-gate int
4195*0209230bSgjelinek zonecfg_endfsent(zone_dochandle_t handle)
41967c478bd9Sstevel@tonic-gate {
4197*0209230bSgjelinek 	return (zonecfg_endent(handle));
41987c478bd9Sstevel@tonic-gate }
41997c478bd9Sstevel@tonic-gate 
42007c478bd9Sstevel@tonic-gate int
4201*0209230bSgjelinek zonecfg_setipdent(zone_dochandle_t handle)
42027c478bd9Sstevel@tonic-gate {
4203*0209230bSgjelinek 	return (zonecfg_setent(handle));
4204*0209230bSgjelinek }
4205*0209230bSgjelinek 
4206*0209230bSgjelinek int
4207*0209230bSgjelinek zonecfg_getipdent(zone_dochandle_t handle, struct zone_fstab *tabptr)
4208*0209230bSgjelinek {
4209*0209230bSgjelinek 	xmlNodePtr cur;
42107c478bd9Sstevel@tonic-gate 	int err;
42117c478bd9Sstevel@tonic-gate 
4212*0209230bSgjelinek 	if (handle == NULL)
4213*0209230bSgjelinek 		return (Z_INVAL);
42147c478bd9Sstevel@tonic-gate 
4215*0209230bSgjelinek 	if ((cur = handle->zone_dh_cur) == NULL)
4216*0209230bSgjelinek 		return (Z_NO_ENTRY);
42177c478bd9Sstevel@tonic-gate 
4218*0209230bSgjelinek 	for (; cur != NULL; cur = cur->next)
4219*0209230bSgjelinek 		if (!xmlStrcmp(cur->name, DTD_ELEM_IPD))
4220*0209230bSgjelinek 			break;
4221*0209230bSgjelinek 	if (cur == NULL) {
4222*0209230bSgjelinek 		handle->zone_dh_cur = handle->zone_dh_top;
4223*0209230bSgjelinek 		return (Z_NO_ENTRY);
4224*0209230bSgjelinek 	}
42257c478bd9Sstevel@tonic-gate 
4226*0209230bSgjelinek 	if ((err = fetchprop(cur, DTD_ATTR_DIR, tabptr->zone_fs_dir,
4227*0209230bSgjelinek 	    sizeof (tabptr->zone_fs_dir))) != Z_OK) {
4228*0209230bSgjelinek 		handle->zone_dh_cur = handle->zone_dh_top;
4229*0209230bSgjelinek 		return (err);
42307c478bd9Sstevel@tonic-gate 	}
42317c478bd9Sstevel@tonic-gate 
4232*0209230bSgjelinek 	handle->zone_dh_cur = cur->next;
4233*0209230bSgjelinek 	return (Z_OK);
42347c478bd9Sstevel@tonic-gate }
42357c478bd9Sstevel@tonic-gate 
4236*0209230bSgjelinek int
4237*0209230bSgjelinek zonecfg_endipdent(zone_dochandle_t handle)
42387c478bd9Sstevel@tonic-gate {
4239*0209230bSgjelinek 	return (zonecfg_endent(handle));
42407c478bd9Sstevel@tonic-gate }
42417c478bd9Sstevel@tonic-gate 
4242108322fbScarlsonj int
4243*0209230bSgjelinek zonecfg_setnwifent(zone_dochandle_t handle)
4244108322fbScarlsonj {
4245*0209230bSgjelinek 	return (zonecfg_setent(handle));
4246108322fbScarlsonj }
4247108322fbScarlsonj 
4248108322fbScarlsonj int
4249*0209230bSgjelinek zonecfg_getnwifent(zone_dochandle_t handle, struct zone_nwiftab *tabptr)
4250108322fbScarlsonj {
4251*0209230bSgjelinek 	xmlNodePtr cur;
4252*0209230bSgjelinek 	int err;
4253108322fbScarlsonj 
4254*0209230bSgjelinek 	if (handle == NULL)
4255*0209230bSgjelinek 		return (Z_INVAL);
4256*0209230bSgjelinek 
4257*0209230bSgjelinek 	if ((cur = handle->zone_dh_cur) == NULL)
4258*0209230bSgjelinek 		return (Z_NO_ENTRY);
4259*0209230bSgjelinek 
4260*0209230bSgjelinek 	for (; cur != NULL; cur = cur->next)
4261*0209230bSgjelinek 		if (!xmlStrcmp(cur->name, DTD_ELEM_NET))
4262108322fbScarlsonj 			break;
4263*0209230bSgjelinek 	if (cur == NULL) {
4264*0209230bSgjelinek 		handle->zone_dh_cur = handle->zone_dh_top;
4265*0209230bSgjelinek 		return (Z_NO_ENTRY);
4266108322fbScarlsonj 	}
4267*0209230bSgjelinek 
4268*0209230bSgjelinek 	if ((err = fetchprop(cur, DTD_ATTR_ADDRESS, tabptr->zone_nwif_address,
4269*0209230bSgjelinek 	    sizeof (tabptr->zone_nwif_address))) != Z_OK) {
4270*0209230bSgjelinek 		handle->zone_dh_cur = handle->zone_dh_top;
4271*0209230bSgjelinek 		return (err);
4272*0209230bSgjelinek 	}
4273*0209230bSgjelinek 
4274*0209230bSgjelinek 	if ((err = fetchprop(cur, DTD_ATTR_PHYSICAL, tabptr->zone_nwif_physical,
4275*0209230bSgjelinek 	    sizeof (tabptr->zone_nwif_physical))) != Z_OK) {
4276*0209230bSgjelinek 		handle->zone_dh_cur = handle->zone_dh_top;
4277*0209230bSgjelinek 		return (err);
4278108322fbScarlsonj 	}
4279*0209230bSgjelinek 
4280*0209230bSgjelinek 	handle->zone_dh_cur = cur->next;
4281*0209230bSgjelinek 	return (Z_OK);
4282108322fbScarlsonj }
4283108322fbScarlsonj 
4284*0209230bSgjelinek int
4285*0209230bSgjelinek zonecfg_endnwifent(zone_dochandle_t handle)
42867c478bd9Sstevel@tonic-gate {
4287*0209230bSgjelinek 	return (zonecfg_endent(handle));
42887c478bd9Sstevel@tonic-gate }
42897c478bd9Sstevel@tonic-gate 
4290*0209230bSgjelinek int
4291*0209230bSgjelinek zonecfg_setdevent(zone_dochandle_t handle)
4292*0209230bSgjelinek {
4293*0209230bSgjelinek 	return (zonecfg_setent(handle));
4294*0209230bSgjelinek }
42957c478bd9Sstevel@tonic-gate 
42967c478bd9Sstevel@tonic-gate int
4297*0209230bSgjelinek zonecfg_getdevent(zone_dochandle_t handle, struct zone_devtab *tabptr)
42987c478bd9Sstevel@tonic-gate {
4299*0209230bSgjelinek 	xmlNodePtr cur;
4300*0209230bSgjelinek 	int err;
43017c478bd9Sstevel@tonic-gate 
4302*0209230bSgjelinek 	if (handle == NULL)
43037c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
43047c478bd9Sstevel@tonic-gate 
4305*0209230bSgjelinek 	if ((cur = handle->zone_dh_cur) == NULL)
4306*0209230bSgjelinek 		return (Z_NO_ENTRY);
4307*0209230bSgjelinek 
4308*0209230bSgjelinek 	for (; cur != NULL; cur = cur->next)
4309*0209230bSgjelinek 		if (!xmlStrcmp(cur->name, DTD_ELEM_DEVICE))
4310*0209230bSgjelinek 			break;
4311*0209230bSgjelinek 	if (cur == NULL) {
4312*0209230bSgjelinek 		handle->zone_dh_cur = handle->zone_dh_top;
4313*0209230bSgjelinek 		return (Z_NO_ENTRY);
43147c478bd9Sstevel@tonic-gate 	}
43157c478bd9Sstevel@tonic-gate 
4316*0209230bSgjelinek 	if ((err = fetchprop(cur, DTD_ATTR_MATCH, tabptr->zone_dev_match,
4317*0209230bSgjelinek 	    sizeof (tabptr->zone_dev_match))) != Z_OK) {
4318*0209230bSgjelinek 		handle->zone_dh_cur = handle->zone_dh_top;
4319*0209230bSgjelinek 		return (err);
43207c478bd9Sstevel@tonic-gate 	}
4321*0209230bSgjelinek 
4322*0209230bSgjelinek 	handle->zone_dh_cur = cur->next;
43237c478bd9Sstevel@tonic-gate 	return (Z_OK);
43247c478bd9Sstevel@tonic-gate }
43257c478bd9Sstevel@tonic-gate 
4326*0209230bSgjelinek int
4327*0209230bSgjelinek zonecfg_enddevent(zone_dochandle_t handle)
43287c478bd9Sstevel@tonic-gate {
4329*0209230bSgjelinek 	return (zonecfg_endent(handle));
4330*0209230bSgjelinek }
43317c478bd9Sstevel@tonic-gate 
4332*0209230bSgjelinek int
4333*0209230bSgjelinek zonecfg_setrctlent(zone_dochandle_t handle)
4334*0209230bSgjelinek {
4335*0209230bSgjelinek 	return (zonecfg_setent(handle));
4336*0209230bSgjelinek }
4337*0209230bSgjelinek 
4338*0209230bSgjelinek int
4339*0209230bSgjelinek zonecfg_getrctlent(zone_dochandle_t handle, struct zone_rctltab *tabptr)
4340*0209230bSgjelinek {
4341*0209230bSgjelinek 	xmlNodePtr cur, val;
4342*0209230bSgjelinek 	struct zone_rctlvaltab *valptr;
4343*0209230bSgjelinek 	int err;
4344*0209230bSgjelinek 
4345*0209230bSgjelinek 	if (handle == NULL)
4346*0209230bSgjelinek 		return (Z_INVAL);
4347*0209230bSgjelinek 
4348*0209230bSgjelinek 	if ((cur = handle->zone_dh_cur) == NULL)
4349*0209230bSgjelinek 		return (Z_NO_ENTRY);
4350*0209230bSgjelinek 
4351*0209230bSgjelinek 	for (; cur != NULL; cur = cur->next)
4352*0209230bSgjelinek 		if (!xmlStrcmp(cur->name, DTD_ELEM_RCTL))
4353*0209230bSgjelinek 			break;
4354*0209230bSgjelinek 	if (cur == NULL) {
4355*0209230bSgjelinek 		handle->zone_dh_cur = handle->zone_dh_top;
4356*0209230bSgjelinek 		return (Z_NO_ENTRY);
4357*0209230bSgjelinek 	}
4358*0209230bSgjelinek 
4359*0209230bSgjelinek 	if ((err = fetchprop(cur, DTD_ATTR_NAME, tabptr->zone_rctl_name,
4360*0209230bSgjelinek 	    sizeof (tabptr->zone_rctl_name))) != Z_OK) {
4361*0209230bSgjelinek 		handle->zone_dh_cur = handle->zone_dh_top;
4362*0209230bSgjelinek 		return (err);
4363*0209230bSgjelinek 	}
4364*0209230bSgjelinek 
4365*0209230bSgjelinek 	tabptr->zone_rctl_valptr = NULL;
4366*0209230bSgjelinek 	for (val = cur->xmlChildrenNode; val != NULL; val = val->next) {
4367*0209230bSgjelinek 		valptr = (struct zone_rctlvaltab *)malloc(
4368*0209230bSgjelinek 		    sizeof (struct zone_rctlvaltab));
4369*0209230bSgjelinek 		if (valptr == NULL)
4370*0209230bSgjelinek 			return (Z_NOMEM);
4371*0209230bSgjelinek 		if (fetchprop(val, DTD_ATTR_PRIV, valptr->zone_rctlval_priv,
4372*0209230bSgjelinek 		    sizeof (valptr->zone_rctlval_priv)) != Z_OK)
4373*0209230bSgjelinek 			break;
4374*0209230bSgjelinek 		if (fetchprop(val, DTD_ATTR_LIMIT, valptr->zone_rctlval_limit,
4375*0209230bSgjelinek 		    sizeof (valptr->zone_rctlval_limit)) != Z_OK)
4376*0209230bSgjelinek 			break;
4377*0209230bSgjelinek 		if (fetchprop(val, DTD_ATTR_ACTION, valptr->zone_rctlval_action,
4378*0209230bSgjelinek 		    sizeof (valptr->zone_rctlval_action)) != Z_OK)
4379*0209230bSgjelinek 			break;
4380*0209230bSgjelinek 		if (zonecfg_add_rctl_value(tabptr, valptr) != Z_OK)
4381*0209230bSgjelinek 			break;
4382*0209230bSgjelinek 	}
4383*0209230bSgjelinek 
4384*0209230bSgjelinek 	handle->zone_dh_cur = cur->next;
4385*0209230bSgjelinek 	return (Z_OK);
4386*0209230bSgjelinek }
4387*0209230bSgjelinek 
4388*0209230bSgjelinek int
4389*0209230bSgjelinek zonecfg_endrctlent(zone_dochandle_t handle)
4390*0209230bSgjelinek {
4391*0209230bSgjelinek 	return (zonecfg_endent(handle));
4392*0209230bSgjelinek }
4393*0209230bSgjelinek 
4394*0209230bSgjelinek int
4395*0209230bSgjelinek zonecfg_setattrent(zone_dochandle_t handle)
4396*0209230bSgjelinek {
4397*0209230bSgjelinek 	return (zonecfg_setent(handle));
4398*0209230bSgjelinek }
4399*0209230bSgjelinek 
4400*0209230bSgjelinek int
4401*0209230bSgjelinek zonecfg_getattrent(zone_dochandle_t handle, struct zone_attrtab *tabptr)
4402*0209230bSgjelinek {
4403*0209230bSgjelinek 	xmlNodePtr cur;
4404*0209230bSgjelinek 	int err;
4405*0209230bSgjelinek 
4406*0209230bSgjelinek 	if (handle == NULL)
4407*0209230bSgjelinek 		return (Z_INVAL);
4408*0209230bSgjelinek 
4409*0209230bSgjelinek 	if ((cur = handle->zone_dh_cur) == NULL)
4410*0209230bSgjelinek 		return (Z_NO_ENTRY);
4411*0209230bSgjelinek 
4412*0209230bSgjelinek 	for (; cur != NULL; cur = cur->next)
4413*0209230bSgjelinek 		if (!xmlStrcmp(cur->name, DTD_ELEM_ATTR))
4414*0209230bSgjelinek 			break;
4415*0209230bSgjelinek 	if (cur == NULL) {
4416*0209230bSgjelinek 		handle->zone_dh_cur = handle->zone_dh_top;
4417*0209230bSgjelinek 		return (Z_NO_ENTRY);
4418*0209230bSgjelinek 	}
4419*0209230bSgjelinek 
4420*0209230bSgjelinek 	if ((err = fetchprop(cur, DTD_ATTR_NAME, tabptr->zone_attr_name,
4421*0209230bSgjelinek 	    sizeof (tabptr->zone_attr_name))) != Z_OK) {
4422*0209230bSgjelinek 		handle->zone_dh_cur = handle->zone_dh_top;
4423*0209230bSgjelinek 		return (err);
4424*0209230bSgjelinek 	}
4425*0209230bSgjelinek 
4426*0209230bSgjelinek 	if ((err = fetchprop(cur, DTD_ATTR_TYPE, tabptr->zone_attr_type,
4427*0209230bSgjelinek 	    sizeof (tabptr->zone_attr_type))) != Z_OK) {
4428*0209230bSgjelinek 		handle->zone_dh_cur = handle->zone_dh_top;
4429*0209230bSgjelinek 		return (err);
4430*0209230bSgjelinek 	}
4431*0209230bSgjelinek 
4432*0209230bSgjelinek 	if ((err = fetchprop(cur, DTD_ATTR_VALUE, tabptr->zone_attr_value,
4433*0209230bSgjelinek 	    sizeof (tabptr->zone_attr_value))) != Z_OK) {
4434*0209230bSgjelinek 		handle->zone_dh_cur = handle->zone_dh_top;
4435*0209230bSgjelinek 		return (err);
4436*0209230bSgjelinek 	}
4437*0209230bSgjelinek 
4438*0209230bSgjelinek 	handle->zone_dh_cur = cur->next;
4439*0209230bSgjelinek 	return (Z_OK);
4440*0209230bSgjelinek }
4441*0209230bSgjelinek 
4442*0209230bSgjelinek int
4443*0209230bSgjelinek zonecfg_endattrent(zone_dochandle_t handle)
4444*0209230bSgjelinek {
4445*0209230bSgjelinek 	return (zonecfg_endent(handle));
4446*0209230bSgjelinek }
4447*0209230bSgjelinek 
4448*0209230bSgjelinek /*
4449*0209230bSgjelinek  * The privileges available on the system and described in privileges(5)
4450*0209230bSgjelinek  * fall into four categories with respect to non-global zones:
4451*0209230bSgjelinek  *
4452*0209230bSgjelinek  *      Default set of privileges considered safe for all non-global
4453*0209230bSgjelinek  *      zones.  These privileges are "safe" in the sense that a
4454*0209230bSgjelinek  *      privileged process in the zone cannot affect processes in any
4455*0209230bSgjelinek  *      other zone on the system.
4456*0209230bSgjelinek  *
4457*0209230bSgjelinek  *      Set of privileges not currently permitted within a non-global
4458*0209230bSgjelinek  *      zone.  These privileges are considered by default, "unsafe,"
4459*0209230bSgjelinek  *      and include ones which affect global resources (such as the
4460*0209230bSgjelinek  *      system clock or physical memory) or are overly broad and cover
4461*0209230bSgjelinek  *      more than one mechanism in the system.  In other cases, there
4462*0209230bSgjelinek  *      has not been sufficient virtualization in the parts of the
4463*0209230bSgjelinek  *      system the privilege covers to allow its use within a
4464*0209230bSgjelinek  *      non-global zone.
4465*0209230bSgjelinek  *
4466*0209230bSgjelinek  *      Set of privileges required in order to get a zone booted and
4467*0209230bSgjelinek  *      init(1M) started.  These cannot be removed from the zone's
4468*0209230bSgjelinek  *      privilege set.
4469*0209230bSgjelinek  *
4470*0209230bSgjelinek  * All other privileges are optional and are potentially useful for
4471*0209230bSgjelinek  * processes executing inside a non-global zone.
4472*0209230bSgjelinek  *
4473*0209230bSgjelinek  * When privileges are added to the system, a determination needs to be
4474*0209230bSgjelinek  * made as to which category the privilege belongs to.  Ideally,
4475*0209230bSgjelinek  * privileges should be fine-grained enough and the mechanisms they cover
4476*0209230bSgjelinek  * virtualized enough so that they can be made available to non-global
4477*0209230bSgjelinek  * zones.
4478*0209230bSgjelinek  */
4479*0209230bSgjelinek 
4480*0209230bSgjelinek /*
4481*0209230bSgjelinek  * Define some of the tokens that priv_str_to_set(3C) recognizes.  Since
4482*0209230bSgjelinek  * the privilege string separator can be any character, although it is
4483*0209230bSgjelinek  * usually a comma character, define these here as well in the event that
4484*0209230bSgjelinek  * they change or are augmented in the future.
4485*0209230bSgjelinek  */
4486*0209230bSgjelinek #define	BASIC_TOKEN		"basic"
4487*0209230bSgjelinek #define	DEFAULT_TOKEN		"default"
4488*0209230bSgjelinek #define	ZONE_TOKEN		"zone"
4489*0209230bSgjelinek #define	TOKEN_PRIV_CHAR		','
4490*0209230bSgjelinek #define	TOKEN_PRIV_STR		","
4491*0209230bSgjelinek 
4492*0209230bSgjelinek typedef struct priv_node {
4493*0209230bSgjelinek 	struct priv_node	*pn_next;	/* Next privilege */
4494*0209230bSgjelinek 	char			*pn_priv;	/* Privileges name */
4495*0209230bSgjelinek } priv_node_t;
4496*0209230bSgjelinek 
4497*0209230bSgjelinek /* Privileges lists can differ across brands */
4498*0209230bSgjelinek typedef struct priv_lists {
4499*0209230bSgjelinek 	/* Privileges considered safe for all non-global zones of a brand */
4500*0209230bSgjelinek 	struct priv_node	*pl_default;
4501*0209230bSgjelinek 
4502*0209230bSgjelinek 	/* Privileges not permitted for all non-global zones of a brand */
4503*0209230bSgjelinek 	struct priv_node	*pl_prohibited;
4504*0209230bSgjelinek 
4505*0209230bSgjelinek 	/* Privileges required for all non-global zones of a brand */
4506*0209230bSgjelinek 	struct priv_node	*pl_required;
4507*0209230bSgjelinek } priv_lists_t;
4508*0209230bSgjelinek 
4509*0209230bSgjelinek static int
4510*0209230bSgjelinek priv_lists_cb(void *data, const char *name, const char *set)
4511*0209230bSgjelinek {
4512*0209230bSgjelinek 	priv_lists_t *plp = (priv_lists_t *)data;
4513*0209230bSgjelinek 	priv_node_t *pnp;
4514*0209230bSgjelinek 
4515*0209230bSgjelinek 	/* Allocate a new priv list node. */
4516*0209230bSgjelinek 	if ((pnp = malloc(sizeof (*pnp))) == NULL)
4517*0209230bSgjelinek 		return (-1);
4518*0209230bSgjelinek 	if ((pnp->pn_priv = strdup(name)) == NULL) {
4519*0209230bSgjelinek 		free(pnp);
4520*0209230bSgjelinek 		return (-1);
4521*0209230bSgjelinek 	}
4522*0209230bSgjelinek 
4523*0209230bSgjelinek 	/* Insert the new priv list node into the right list */
4524*0209230bSgjelinek 	if (strcmp(set, "default") == 0) {
4525*0209230bSgjelinek 		pnp->pn_next = plp->pl_default;
4526*0209230bSgjelinek 		plp->pl_default = pnp;
4527*0209230bSgjelinek 	} else if (strcmp(set, "prohibited") == 0) {
4528*0209230bSgjelinek 		pnp->pn_next = plp->pl_prohibited;
4529*0209230bSgjelinek 		plp->pl_prohibited = pnp;
4530*0209230bSgjelinek 	} else if (strcmp(set, "required") == 0) {
4531*0209230bSgjelinek 		pnp->pn_next = plp->pl_required;
4532*0209230bSgjelinek 		plp->pl_required = pnp;
4533*0209230bSgjelinek 	} else {
4534*0209230bSgjelinek 		free(pnp->pn_priv);
4535*0209230bSgjelinek 		free(pnp);
4536*0209230bSgjelinek 		return (-1);
4537*0209230bSgjelinek 	}
4538*0209230bSgjelinek 	return (0);
4539*0209230bSgjelinek }
4540*0209230bSgjelinek 
4541*0209230bSgjelinek static void
4542*0209230bSgjelinek priv_lists_destroy(priv_lists_t *plp)
4543*0209230bSgjelinek {
4544*0209230bSgjelinek 	priv_node_t *pnp;
4545*0209230bSgjelinek 
4546*0209230bSgjelinek 	assert(plp != NULL);
4547*0209230bSgjelinek 
4548*0209230bSgjelinek 	while ((pnp = plp->pl_default) != NULL) {
4549*0209230bSgjelinek 		plp->pl_default = pnp->pn_next;
4550*0209230bSgjelinek 		free(pnp->pn_priv);
4551*0209230bSgjelinek 		free(pnp);
4552*0209230bSgjelinek 	}
4553*0209230bSgjelinek 	while ((pnp = plp->pl_prohibited) != NULL) {
4554*0209230bSgjelinek 		plp->pl_prohibited = pnp->pn_next;
4555*0209230bSgjelinek 		free(pnp->pn_priv);
4556*0209230bSgjelinek 		free(pnp);
4557*0209230bSgjelinek 	}
4558*0209230bSgjelinek 	while ((pnp = plp->pl_required) != NULL) {
4559*0209230bSgjelinek 		plp->pl_required = pnp->pn_next;
4560*0209230bSgjelinek 		free(pnp->pn_priv);
4561*0209230bSgjelinek 		free(pnp);
4562*0209230bSgjelinek 	}
4563*0209230bSgjelinek 	free(plp);
4564*0209230bSgjelinek }
4565*0209230bSgjelinek 
4566*0209230bSgjelinek static int
4567*0209230bSgjelinek priv_lists_create(zone_dochandle_t handle, priv_lists_t **plpp)
4568*0209230bSgjelinek {
4569*0209230bSgjelinek 	priv_lists_t *plp;
4570*0209230bSgjelinek 	brand_handle_t bh;
4571*0209230bSgjelinek 	char brand[MAXNAMELEN];
4572*0209230bSgjelinek 
4573*0209230bSgjelinek 	if (handle != NULL) {
4574*0209230bSgjelinek 		if (zonecfg_get_brand(handle, brand, sizeof (brand)) != 0)
4575*0209230bSgjelinek 			return (Z_BRAND_ERROR);
4576*0209230bSgjelinek 	} else {
4577*0209230bSgjelinek 		(void) strlcpy(brand, NATIVE_BRAND_NAME, MAXNAMELEN);
4578*0209230bSgjelinek 	}
4579*0209230bSgjelinek 
4580*0209230bSgjelinek 	if ((bh = brand_open(brand)) == NULL)
4581*0209230bSgjelinek 		return (Z_BRAND_ERROR);
4582*0209230bSgjelinek 
4583*0209230bSgjelinek 	if ((plp = calloc(1, sizeof (priv_lists_t))) == NULL) {
4584*0209230bSgjelinek 		brand_close(bh);
4585*0209230bSgjelinek 		return (Z_NOMEM);
4586*0209230bSgjelinek 	}
4587*0209230bSgjelinek 
4588*0209230bSgjelinek 	/* construct the privilege lists */
4589*0209230bSgjelinek 	if (brand_config_iter_privilege(bh, priv_lists_cb, plp) != 0) {
4590*0209230bSgjelinek 		priv_lists_destroy(plp);
4591*0209230bSgjelinek 		brand_close(bh);
4592*0209230bSgjelinek 		return (Z_BRAND_ERROR);
4593*0209230bSgjelinek 	}
4594*0209230bSgjelinek 
4595*0209230bSgjelinek 	brand_close(bh);
4596*0209230bSgjelinek 	*plpp = plp;
4597*0209230bSgjelinek 	return (Z_OK);
4598*0209230bSgjelinek }
4599*0209230bSgjelinek 
4600*0209230bSgjelinek static int
4601*0209230bSgjelinek get_default_privset(priv_set_t *privs, priv_lists_t *plp)
4602*0209230bSgjelinek {
4603*0209230bSgjelinek 	priv_node_t *pnp;
4604*0209230bSgjelinek 	priv_set_t *basic;
4605*0209230bSgjelinek 
4606*0209230bSgjelinek 	basic = priv_str_to_set(BASIC_TOKEN, TOKEN_PRIV_STR, NULL);
4607*0209230bSgjelinek 	if (basic == NULL)
4608*0209230bSgjelinek 		return (errno == ENOMEM ? Z_NOMEM : Z_INVAL);
4609*0209230bSgjelinek 
4610*0209230bSgjelinek 	priv_union(basic, privs);
4611*0209230bSgjelinek 	priv_freeset(basic);
4612*0209230bSgjelinek 
4613*0209230bSgjelinek 	for (pnp = plp->pl_default; pnp != NULL; pnp = pnp->pn_next) {
4614*0209230bSgjelinek 		if (priv_addset(privs, pnp->pn_priv) != 0)
4615*0209230bSgjelinek 			return (Z_INVAL);
4616*0209230bSgjelinek 	}
4617*0209230bSgjelinek 
4618*0209230bSgjelinek 	return (Z_OK);
4619*0209230bSgjelinek }
4620*0209230bSgjelinek 
4621*0209230bSgjelinek int
4622*0209230bSgjelinek zonecfg_default_privset(priv_set_t *privs)
4623*0209230bSgjelinek {
4624*0209230bSgjelinek 	priv_lists_t *plp;
4625*0209230bSgjelinek 	int ret;
4626*0209230bSgjelinek 
4627*0209230bSgjelinek 	if ((ret = priv_lists_create(NULL, &plp)) != Z_OK)
4628*0209230bSgjelinek 		return (ret);
4629*0209230bSgjelinek 	ret = get_default_privset(privs, plp);
4630*0209230bSgjelinek 	priv_lists_destroy(plp);
4631*0209230bSgjelinek 	return (ret);
4632*0209230bSgjelinek }
4633*0209230bSgjelinek 
4634*0209230bSgjelinek void
4635*0209230bSgjelinek append_priv_token(char *priv, char *str, size_t strlen)
4636*0209230bSgjelinek {
4637*0209230bSgjelinek 	if (*str != '\0')
4638*0209230bSgjelinek 		(void) strlcat(str, TOKEN_PRIV_STR, strlen);
4639*0209230bSgjelinek 	(void) strlcat(str, priv, strlen);
4640*0209230bSgjelinek }
4641*0209230bSgjelinek 
4642*0209230bSgjelinek /*
4643*0209230bSgjelinek  * Verify that the supplied string is a valid privilege limit set for a
4644*0209230bSgjelinek  * non-global zone.  This string must not only be acceptable to
4645*0209230bSgjelinek  * priv_str_to_set(3C) which parses it, but it also must resolve to a
4646*0209230bSgjelinek  * privilege set that includes certain required privileges and lacks
4647*0209230bSgjelinek  * certain prohibited privileges.
4648*0209230bSgjelinek  */
4649*0209230bSgjelinek static int
4650*0209230bSgjelinek verify_privset(char *privbuf, priv_set_t *privs, char **privname,
4651*0209230bSgjelinek     boolean_t add_default, priv_lists_t *plp)
4652*0209230bSgjelinek {
4653*0209230bSgjelinek 	priv_node_t *pnp;
4654*0209230bSgjelinek 	char *tmp, *cp, *lasts;
4655*0209230bSgjelinek 	size_t len;
4656*0209230bSgjelinek 	priv_set_t *mergeset;
4657*0209230bSgjelinek 	const char *token;
4658*0209230bSgjelinek 
4659*0209230bSgjelinek 	/*
4660*0209230bSgjelinek 	 * The verification of the privilege string occurs in several
4661*0209230bSgjelinek 	 * phases.  In the first phase, the supplied string is scanned for
4662*0209230bSgjelinek 	 * the ZONE_TOKEN token which is not support as part of the
4663*0209230bSgjelinek 	 * "limitpriv" property.
4664*0209230bSgjelinek 	 *
4665*0209230bSgjelinek 	 * Duplicate the supplied privilege string since strtok_r(3C)
4666*0209230bSgjelinek 	 * tokenizes its input by null-terminating the tokens.
4667*0209230bSgjelinek 	 */
4668*0209230bSgjelinek 	if ((tmp = strdup(privbuf)) == NULL)
4669*0209230bSgjelinek 		return (Z_NOMEM);
4670*0209230bSgjelinek 	for (cp = strtok_r(tmp, TOKEN_PRIV_STR, &lasts); cp != NULL;
4671*0209230bSgjelinek 	    cp = strtok_r(NULL, TOKEN_PRIV_STR, &lasts)) {
4672*0209230bSgjelinek 		if (strcmp(cp, ZONE_TOKEN) == 0) {
4673*0209230bSgjelinek 			free(tmp);
4674*0209230bSgjelinek 			if ((*privname = strdup(ZONE_TOKEN)) == NULL)
4675*0209230bSgjelinek 				return (Z_NOMEM);
4676*0209230bSgjelinek 			else
4677*0209230bSgjelinek 				return (Z_PRIV_UNKNOWN);
4678*0209230bSgjelinek 		}
4679*0209230bSgjelinek 	}
4680*0209230bSgjelinek 	free(tmp);
4681*0209230bSgjelinek 
4682*0209230bSgjelinek 	if (add_default) {
4683*0209230bSgjelinek 		/*
4684*0209230bSgjelinek 		 * If DEFAULT_TOKEN was specified, a string needs to be
4685*0209230bSgjelinek 		 * built containing the privileges from the default, safe
4686*0209230bSgjelinek 		 * set along with those of the "limitpriv" property.
4687*0209230bSgjelinek 		 */
4688*0209230bSgjelinek 		len = strlen(privbuf) + sizeof (BASIC_TOKEN) + 2;
4689*0209230bSgjelinek 
4690*0209230bSgjelinek 		for (pnp = plp->pl_default; pnp != NULL; pnp = pnp->pn_next)
4691*0209230bSgjelinek 			len += strlen(pnp->pn_priv) + 1;
4692*0209230bSgjelinek 		tmp = alloca(len);
4693*0209230bSgjelinek 		*tmp = '\0';
4694*0209230bSgjelinek 
4695*0209230bSgjelinek 		append_priv_token(BASIC_TOKEN, tmp, len);
4696*0209230bSgjelinek 		for (pnp = plp->pl_default; pnp != NULL; pnp = pnp->pn_next)
4697*0209230bSgjelinek 			append_priv_token(pnp->pn_priv, tmp, len);
4698*0209230bSgjelinek 		(void) strlcat(tmp, TOKEN_PRIV_STR, len);
4699*0209230bSgjelinek 		(void) strlcat(tmp, privbuf, len);
4700*0209230bSgjelinek 	} else {
4701*0209230bSgjelinek 		tmp = privbuf;
4702*0209230bSgjelinek 	}
4703*0209230bSgjelinek 
4704*0209230bSgjelinek 
4705*0209230bSgjelinek 	/*
4706*0209230bSgjelinek 	 * In the next phase, attempt to convert the merged privilege
4707*0209230bSgjelinek 	 * string into a privilege set.  In the case of an error, either
4708*0209230bSgjelinek 	 * there was a memory allocation failure or there was an invalid
4709*0209230bSgjelinek 	 * privilege token in the string.  In either case, return an
4710*0209230bSgjelinek 	 * appropriate error code but in the event of an invalid token,
4711*0209230bSgjelinek 	 * allocate a string containing its name and return that back to
4712*0209230bSgjelinek 	 * the caller.
4713*0209230bSgjelinek 	 */
4714*0209230bSgjelinek 	mergeset = priv_str_to_set(tmp, TOKEN_PRIV_STR, &token);
4715*0209230bSgjelinek 	if (mergeset == NULL) {
4716*0209230bSgjelinek 		if (token == NULL)
4717*0209230bSgjelinek 			return (Z_NOMEM);
4718*0209230bSgjelinek 		if ((cp = strchr(token, TOKEN_PRIV_CHAR)) != NULL)
4719*0209230bSgjelinek 			*cp = '\0';
4720*0209230bSgjelinek 		if ((*privname = strdup(token)) == NULL)
4721*0209230bSgjelinek 			return (Z_NOMEM);
4722*0209230bSgjelinek 		else
4723*0209230bSgjelinek 			return (Z_PRIV_UNKNOWN);
4724*0209230bSgjelinek 	}
4725*0209230bSgjelinek 
4726*0209230bSgjelinek 	/*
4727*0209230bSgjelinek 	 * Next, verify that none of the prohibited zone privileges are
4728*0209230bSgjelinek 	 * present in the merged privilege set.
4729*0209230bSgjelinek 	 */
4730*0209230bSgjelinek 	for (pnp = plp->pl_prohibited; pnp != NULL; pnp = pnp->pn_next) {
4731*0209230bSgjelinek 		if (priv_ismember(mergeset, pnp->pn_priv)) {
4732*0209230bSgjelinek 			priv_freeset(mergeset);
4733*0209230bSgjelinek 			if ((*privname = strdup(pnp->pn_priv)) == NULL)
4734*0209230bSgjelinek 				return (Z_NOMEM);
4735*0209230bSgjelinek 			else
4736*0209230bSgjelinek 				return (Z_PRIV_PROHIBITED);
4737*0209230bSgjelinek 		}
4738*0209230bSgjelinek 	}
4739*0209230bSgjelinek 
4740*0209230bSgjelinek 	/*
4741*0209230bSgjelinek 	 * Finally, verify that all of the required zone privileges are
4742*0209230bSgjelinek 	 * present in the merged privilege set.
4743*0209230bSgjelinek 	 */
4744*0209230bSgjelinek 	for (pnp = plp->pl_required; pnp != NULL; pnp = pnp->pn_next) {
4745*0209230bSgjelinek 		if (!priv_ismember(mergeset, pnp->pn_priv)) {
4746*0209230bSgjelinek 			priv_freeset(mergeset);
4747*0209230bSgjelinek 			if ((*privname = strdup(pnp->pn_priv)) == NULL)
4748*0209230bSgjelinek 				return (Z_NOMEM);
4749*0209230bSgjelinek 			else
4750*0209230bSgjelinek 				return (Z_PRIV_REQUIRED);
4751*0209230bSgjelinek 		}
4752*0209230bSgjelinek 	}
4753*0209230bSgjelinek 
4754*0209230bSgjelinek 	priv_copyset(mergeset, privs);
4755*0209230bSgjelinek 	priv_freeset(mergeset);
4756*0209230bSgjelinek 	return (Z_OK);
4757*0209230bSgjelinek }
4758*0209230bSgjelinek 
4759*0209230bSgjelinek /*
4760*0209230bSgjelinek  * Fill in the supplied privilege set with either the default, safe set of
4761*0209230bSgjelinek  * privileges suitable for a non-global zone, or one based on the
4762*0209230bSgjelinek  * "limitpriv" property in the zone's configuration.
4763*0209230bSgjelinek  *
4764*0209230bSgjelinek  * In the event of an invalid privilege specification in the
4765*0209230bSgjelinek  * configuration, a string is allocated and returned containing the
4766*0209230bSgjelinek  * "privilege" causing the issue.  It is the caller's responsibility to
4767*0209230bSgjelinek  * free this memory when it is done with it.
4768*0209230bSgjelinek  */
4769*0209230bSgjelinek int
4770*0209230bSgjelinek zonecfg_get_privset(zone_dochandle_t handle, priv_set_t *privs,
4771*0209230bSgjelinek     char **privname)
4772*0209230bSgjelinek {
4773*0209230bSgjelinek 	priv_lists_t *plp;
4774*0209230bSgjelinek 	char *cp, *limitpriv = NULL;
4775*0209230bSgjelinek 	int err, limitlen;
4776*0209230bSgjelinek 
4777*0209230bSgjelinek 	/*
4778*0209230bSgjelinek 	 * Attempt to lookup the "limitpriv" property.  If it does not
4779*0209230bSgjelinek 	 * exist or matches the string DEFAULT_TOKEN exactly, then the
4780*0209230bSgjelinek 	 * default, safe privilege set is returned.
4781*0209230bSgjelinek 	 */
4782*0209230bSgjelinek 	if ((err = zonecfg_get_limitpriv(handle, &limitpriv)) != Z_OK)
4783*0209230bSgjelinek 		return (err);
4784*0209230bSgjelinek 
4785*0209230bSgjelinek 	if ((err = priv_lists_create(handle, &plp)) != Z_OK)
4786*0209230bSgjelinek 		return (err);
4787*0209230bSgjelinek 
4788*0209230bSgjelinek 	limitlen = strlen(limitpriv);
4789*0209230bSgjelinek 	if (limitlen == 0 || strcmp(limitpriv, DEFAULT_TOKEN) == 0) {
4790*0209230bSgjelinek 		free(limitpriv);
4791*0209230bSgjelinek 		err = get_default_privset(privs, plp);
4792*0209230bSgjelinek 		priv_lists_destroy(plp);
4793*0209230bSgjelinek 		return (err);
4794*0209230bSgjelinek 	}
4795*0209230bSgjelinek 
4796*0209230bSgjelinek 	/*
4797*0209230bSgjelinek 	 * Check if the string DEFAULT_TOKEN is the first token in a list
4798*0209230bSgjelinek 	 * of privileges.
4799*0209230bSgjelinek 	 */
4800*0209230bSgjelinek 	cp = strchr(limitpriv, TOKEN_PRIV_CHAR);
4801*0209230bSgjelinek 	if (cp != NULL &&
4802*0209230bSgjelinek 	    strncmp(limitpriv, DEFAULT_TOKEN, cp - limitpriv) == 0)
4803*0209230bSgjelinek 		err = verify_privset(cp + 1, privs, privname, B_TRUE, plp);
4804*0209230bSgjelinek 	else
4805*0209230bSgjelinek 		err = verify_privset(limitpriv, privs, privname, B_FALSE, plp);
4806*0209230bSgjelinek 
4807*0209230bSgjelinek 	free(limitpriv);
4808*0209230bSgjelinek 	priv_lists_destroy(plp);
4809*0209230bSgjelinek 	return (err);
4810*0209230bSgjelinek }
4811*0209230bSgjelinek 
4812*0209230bSgjelinek int
4813*0209230bSgjelinek zone_get_zonepath(char *zone_name, char *zonepath, size_t rp_sz)
4814*0209230bSgjelinek {
4815*0209230bSgjelinek 	zone_dochandle_t handle;
4816*0209230bSgjelinek 	boolean_t found = B_FALSE;
4817*0209230bSgjelinek 	struct zoneent *ze;
4818*0209230bSgjelinek 	FILE *cookie;
4819*0209230bSgjelinek 	int err;
4820*0209230bSgjelinek 	char *cp;
4821*0209230bSgjelinek 
4822*0209230bSgjelinek 	if (zone_name == NULL)
4823*0209230bSgjelinek 		return (Z_INVAL);
4824*0209230bSgjelinek 
4825*0209230bSgjelinek 	(void) strlcpy(zonepath, zonecfg_root, rp_sz);
4826*0209230bSgjelinek 	cp = zonepath + strlen(zonepath);
4827*0209230bSgjelinek 	while (cp > zonepath && cp[-1] == '/')
4828*0209230bSgjelinek 		*--cp = '\0';
4829*0209230bSgjelinek 
4830*0209230bSgjelinek 	if (strcmp(zone_name, GLOBAL_ZONENAME) == 0) {
4831*0209230bSgjelinek 		if (zonepath[0] == '\0')
4832*0209230bSgjelinek 			(void) strlcpy(zonepath, "/", rp_sz);
4833*0209230bSgjelinek 		return (Z_OK);
4834*0209230bSgjelinek 	}
4835*0209230bSgjelinek 
4836*0209230bSgjelinek 	/*
4837*0209230bSgjelinek 	 * First check the index file.  Because older versions did not have
4838*0209230bSgjelinek 	 * a copy of the zone path, allow for it to be zero length, in which
4839*0209230bSgjelinek 	 * case we ignore this result and fall back to the XML files.
4840*0209230bSgjelinek 	 */
4841*0209230bSgjelinek 	cookie = setzoneent();
4842*0209230bSgjelinek 	while ((ze = getzoneent_private(cookie)) != NULL) {
4843*0209230bSgjelinek 		if (strcmp(ze->zone_name, zone_name) == 0) {
4844*0209230bSgjelinek 			found = B_TRUE;
4845*0209230bSgjelinek 			if (ze->zone_path[0] != '\0')
4846*0209230bSgjelinek 				(void) strlcpy(cp, ze->zone_path,
4847*0209230bSgjelinek 				    rp_sz - (cp - zonepath));
4848*0209230bSgjelinek 		}
4849*0209230bSgjelinek 		free(ze);
4850*0209230bSgjelinek 		if (found)
4851*0209230bSgjelinek 			break;
4852*0209230bSgjelinek 	}
4853*0209230bSgjelinek 	endzoneent(cookie);
4854*0209230bSgjelinek 	if (found && *cp != '\0')
4855*0209230bSgjelinek 		return (Z_OK);
4856*0209230bSgjelinek 
4857*0209230bSgjelinek 	/* Fall back to the XML files. */
4858*0209230bSgjelinek 	if ((handle = zonecfg_init_handle()) == NULL)
4859*0209230bSgjelinek 		return (Z_NOMEM);
4860*0209230bSgjelinek 
4861*0209230bSgjelinek 	/*
4862*0209230bSgjelinek 	 * Check the snapshot first: if a zone is running, its zonepath
4863*0209230bSgjelinek 	 * may have changed.
4864*0209230bSgjelinek 	 */
4865*0209230bSgjelinek 	if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) {
4866*0209230bSgjelinek 		if ((err = zonecfg_get_handle(zone_name, handle)) != Z_OK)
4867*0209230bSgjelinek 			return (err);
4868*0209230bSgjelinek 	}
4869*0209230bSgjelinek 	err = zonecfg_get_zonepath(handle, zonepath, rp_sz);
4870*0209230bSgjelinek 	zonecfg_fini_handle(handle);
4871*0209230bSgjelinek 	return (err);
4872*0209230bSgjelinek }
4873*0209230bSgjelinek 
4874*0209230bSgjelinek int
4875*0209230bSgjelinek zone_get_rootpath(char *zone_name, char *rootpath, size_t rp_sz)
4876*0209230bSgjelinek {
4877*0209230bSgjelinek 	int err;
4878*0209230bSgjelinek 
4879*0209230bSgjelinek 	/* This function makes sense for non-global zones only. */
4880*0209230bSgjelinek 	if (strcmp(zone_name, GLOBAL_ZONENAME) == 0)
4881*0209230bSgjelinek 		return (Z_BOGUS_ZONE_NAME);
4882*0209230bSgjelinek 	if ((err = zone_get_zonepath(zone_name, rootpath, rp_sz)) != Z_OK)
4883*0209230bSgjelinek 		return (err);
4884*0209230bSgjelinek 	if (strlcat(rootpath, "/root", rp_sz) >= rp_sz)
4885*0209230bSgjelinek 		return (Z_TOO_BIG);
4886*0209230bSgjelinek 	return (Z_OK);
4887*0209230bSgjelinek }
4888*0209230bSgjelinek 
4889*0209230bSgjelinek int
4890*0209230bSgjelinek zone_get_brand(char *zone_name, char *brandname, size_t rp_sz)
4891*0209230bSgjelinek {
4892*0209230bSgjelinek 	int err;
4893*0209230bSgjelinek 	zone_dochandle_t handle;
4894*0209230bSgjelinek 	char myzone[MAXNAMELEN];
4895*0209230bSgjelinek 	int myzoneid = getzoneid();
4896*0209230bSgjelinek 
4897*0209230bSgjelinek 	/*
4898*0209230bSgjelinek 	 * If we are not in the global zone, then we don't have the zone
4899*0209230bSgjelinek 	 * .xml files with the brand name available.  Thus, we are going to
4900*0209230bSgjelinek 	 * have to ask the kernel for the information.
4901*0209230bSgjelinek 	 */
4902*0209230bSgjelinek 	if (myzoneid != GLOBAL_ZONEID) {
4903*0209230bSgjelinek 		if (zone_getattr(myzoneid, ZONE_ATTR_NAME, myzone,
4904*0209230bSgjelinek 		    sizeof (myzone)) < 0)
4905*0209230bSgjelinek 			return (Z_NO_ZONE);
4906*0209230bSgjelinek 		if (strncmp(zone_name, myzone, MAXNAMELEN) != NULL)
4907*0209230bSgjelinek 			return (Z_NO_ZONE);
4908*0209230bSgjelinek 		err = zone_getattr(myzoneid, ZONE_ATTR_BRAND, brandname, rp_sz);
4909*0209230bSgjelinek 		if (err < 0)
4910*0209230bSgjelinek 			return ((errno == EFAULT) ? Z_TOO_BIG : Z_INVAL);
4911*0209230bSgjelinek 		return (Z_OK);
4912*0209230bSgjelinek 	}
4913*0209230bSgjelinek 
4914*0209230bSgjelinek 	if (strcmp(zone_name, "global") == NULL) {
4915*0209230bSgjelinek 		(void) strlcpy(brandname, NATIVE_BRAND_NAME, rp_sz);
4916*0209230bSgjelinek 		return (0);
4917*0209230bSgjelinek 	}
4918*0209230bSgjelinek 	if ((handle = zonecfg_init_handle()) == NULL)
4919*0209230bSgjelinek 		return (Z_NOMEM);
4920*0209230bSgjelinek 
4921*0209230bSgjelinek 	err = zonecfg_get_handle((char *)zone_name, handle);
4922*0209230bSgjelinek 	if (err == Z_OK)
4923*0209230bSgjelinek 		err = zonecfg_get_brand(handle, brandname, rp_sz);
4924*0209230bSgjelinek 
4925*0209230bSgjelinek 	zonecfg_fini_handle(handle);
4926*0209230bSgjelinek 	return (err);
4927*0209230bSgjelinek }
4928*0209230bSgjelinek 
4929*0209230bSgjelinek /*
4930*0209230bSgjelinek  * Return the appropriate root for the active /dev.
4931*0209230bSgjelinek  * For normal zone, the path is $ZONEPATH/root;
4932*0209230bSgjelinek  * for scratch zone, the dev path is $ZONEPATH/lu.
4933*0209230bSgjelinek  */
4934*0209230bSgjelinek int
4935*0209230bSgjelinek zone_get_devroot(char *zone_name, char *devroot, size_t rp_sz)
4936*0209230bSgjelinek {
4937*0209230bSgjelinek 	int err;
4938*0209230bSgjelinek 	char *suffix;
4939*0209230bSgjelinek 	zone_state_t state;
4940*0209230bSgjelinek 
4941*0209230bSgjelinek 	/* This function makes sense for non-global zones only. */
4942*0209230bSgjelinek 	if (strcmp(zone_name, GLOBAL_ZONENAME) == 0)
4943*0209230bSgjelinek 		return (Z_BOGUS_ZONE_NAME);
4944*0209230bSgjelinek 	if ((err = zone_get_zonepath(zone_name, devroot, rp_sz)) != Z_OK)
4945*0209230bSgjelinek 		return (err);
4946*0209230bSgjelinek 
4947*0209230bSgjelinek 	if (zone_get_state(zone_name, &state) == Z_OK &&
4948*0209230bSgjelinek 	    state == ZONE_STATE_MOUNTED)
4949*0209230bSgjelinek 		suffix = "/lu";
4950*0209230bSgjelinek 	else
4951*0209230bSgjelinek 		suffix = "/root";
4952*0209230bSgjelinek 	if (strlcat(devroot, suffix, rp_sz) >= rp_sz)
4953*0209230bSgjelinek 		return (Z_TOO_BIG);
4954*0209230bSgjelinek 	return (Z_OK);
4955*0209230bSgjelinek }
4956*0209230bSgjelinek 
4957*0209230bSgjelinek static zone_state_t
4958*0209230bSgjelinek kernel_state_to_user_state(zoneid_t zoneid, zone_status_t kernel_state)
4959*0209230bSgjelinek {
4960*0209230bSgjelinek 	char zoneroot[MAXPATHLEN];
4961*0209230bSgjelinek 	size_t zlen;
4962*0209230bSgjelinek 
4963*0209230bSgjelinek 	assert(kernel_state <= ZONE_MAX_STATE);
4964*0209230bSgjelinek 	switch (kernel_state) {
4965*0209230bSgjelinek 		case ZONE_IS_UNINITIALIZED:
4966*0209230bSgjelinek 			return (ZONE_STATE_READY);
4967*0209230bSgjelinek 		case ZONE_IS_READY:
4968*0209230bSgjelinek 			/*
4969*0209230bSgjelinek 			 * If the zone's root is mounted on $ZONEPATH/lu, then
4970*0209230bSgjelinek 			 * it's a mounted scratch zone.
4971*0209230bSgjelinek 			 */
4972*0209230bSgjelinek 			if (zone_getattr(zoneid, ZONE_ATTR_ROOT, zoneroot,
4973*0209230bSgjelinek 			    sizeof (zoneroot)) >= 0) {
4974*0209230bSgjelinek 				zlen = strlen(zoneroot);
4975*0209230bSgjelinek 				if (zlen > 3 &&
4976*0209230bSgjelinek 				    strcmp(zoneroot + zlen - 3, "/lu") == 0)
4977*0209230bSgjelinek 					return (ZONE_STATE_MOUNTED);
4978*0209230bSgjelinek 			}
4979*0209230bSgjelinek 			return (ZONE_STATE_READY);
4980*0209230bSgjelinek 		case ZONE_IS_BOOTING:
4981*0209230bSgjelinek 		case ZONE_IS_RUNNING:
4982*0209230bSgjelinek 			return (ZONE_STATE_RUNNING);
4983*0209230bSgjelinek 		case ZONE_IS_SHUTTING_DOWN:
4984*0209230bSgjelinek 		case ZONE_IS_EMPTY:
4985*0209230bSgjelinek 			return (ZONE_STATE_SHUTTING_DOWN);
4986*0209230bSgjelinek 		case ZONE_IS_DOWN:
4987*0209230bSgjelinek 		case ZONE_IS_DYING:
4988*0209230bSgjelinek 		case ZONE_IS_DEAD:
4989*0209230bSgjelinek 		default:
4990*0209230bSgjelinek 			return (ZONE_STATE_DOWN);
4991*0209230bSgjelinek 	}
4992*0209230bSgjelinek 	/* NOTREACHED */
4993*0209230bSgjelinek }
4994*0209230bSgjelinek 
4995*0209230bSgjelinek int
4996*0209230bSgjelinek zone_get_state(char *zone_name, zone_state_t *state_num)
4997*0209230bSgjelinek {
4998*0209230bSgjelinek 	zone_status_t status;
4999*0209230bSgjelinek 	zoneid_t zone_id;
5000*0209230bSgjelinek 	struct zoneent *ze;
5001*0209230bSgjelinek 	boolean_t found = B_FALSE;
5002*0209230bSgjelinek 	FILE *cookie;
5003*0209230bSgjelinek 	char kernzone[ZONENAME_MAX];
5004*0209230bSgjelinek 	FILE *fp;
5005*0209230bSgjelinek 
5006*0209230bSgjelinek 	if (zone_name == NULL)
5007*0209230bSgjelinek 		return (Z_INVAL);
5008*0209230bSgjelinek 
5009*0209230bSgjelinek 	/*
5010*0209230bSgjelinek 	 * If we're looking at an alternate root, then we need to query the
5011*0209230bSgjelinek 	 * kernel using the scratch zone name.
5012*0209230bSgjelinek 	 */
5013*0209230bSgjelinek 	zone_id = -1;
5014*0209230bSgjelinek 	if (*zonecfg_root != '\0' && !zonecfg_is_scratch(zone_name)) {
5015*0209230bSgjelinek 		if ((fp = zonecfg_open_scratch("", B_FALSE)) != NULL) {
5016*0209230bSgjelinek 			if (zonecfg_find_scratch(fp, zone_name, zonecfg_root,
5017*0209230bSgjelinek 			    kernzone, sizeof (kernzone)) == 0)
5018*0209230bSgjelinek 				zone_id = getzoneidbyname(kernzone);
5019*0209230bSgjelinek 			zonecfg_close_scratch(fp);
5020*0209230bSgjelinek 		}
5021*0209230bSgjelinek 	} else {
5022*0209230bSgjelinek 		zone_id = getzoneidbyname(zone_name);
5023*0209230bSgjelinek 	}
5024*0209230bSgjelinek 
5025*0209230bSgjelinek 	/* check to see if zone is running */
5026*0209230bSgjelinek 	if (zone_id != -1 &&
5027*0209230bSgjelinek 	    zone_getattr(zone_id, ZONE_ATTR_STATUS, &status,
5028*0209230bSgjelinek 	    sizeof (status)) >= 0) {
5029*0209230bSgjelinek 		*state_num = kernel_state_to_user_state(zone_id, status);
5030*0209230bSgjelinek 		return (Z_OK);
5031*0209230bSgjelinek 	}
5032*0209230bSgjelinek 
5033*0209230bSgjelinek 	cookie = setzoneent();
5034*0209230bSgjelinek 	while ((ze = getzoneent_private(cookie)) != NULL) {
5035*0209230bSgjelinek 		if (strcmp(ze->zone_name, zone_name) == 0) {
5036*0209230bSgjelinek 			found = B_TRUE;
5037*0209230bSgjelinek 			*state_num = ze->zone_state;
5038*0209230bSgjelinek 		}
5039*0209230bSgjelinek 		free(ze);
5040*0209230bSgjelinek 		if (found)
5041*0209230bSgjelinek 			break;
5042*0209230bSgjelinek 	}
5043*0209230bSgjelinek 	endzoneent(cookie);
5044*0209230bSgjelinek 	return ((found) ? Z_OK : Z_NO_ZONE);
5045*0209230bSgjelinek }
5046*0209230bSgjelinek 
5047*0209230bSgjelinek int
5048*0209230bSgjelinek zone_set_state(char *zone, zone_state_t state)
5049*0209230bSgjelinek {
5050*0209230bSgjelinek 	struct zoneent ze;
5051*0209230bSgjelinek 
5052*0209230bSgjelinek 	if (state != ZONE_STATE_CONFIGURED && state != ZONE_STATE_INSTALLED &&
5053*0209230bSgjelinek 	    state != ZONE_STATE_INCOMPLETE)
5054*0209230bSgjelinek 		return (Z_INVAL);
5055*0209230bSgjelinek 
5056*0209230bSgjelinek 	bzero(&ze, sizeof (ze));
5057*0209230bSgjelinek 	(void) strlcpy(ze.zone_name, zone, sizeof (ze.zone_name));
5058*0209230bSgjelinek 	ze.zone_state = state;
5059*0209230bSgjelinek 	(void) strlcpy(ze.zone_path, "", sizeof (ze.zone_path));
5060*0209230bSgjelinek 	return (putzoneent(&ze, PZE_MODIFY));
5061*0209230bSgjelinek }
5062*0209230bSgjelinek 
5063*0209230bSgjelinek /*
5064*0209230bSgjelinek  * Get id (if any) for specified zone.  There are four possible outcomes:
5065*0209230bSgjelinek  * - If the string corresponds to the numeric id of an active (booted)
5066*0209230bSgjelinek  *   zone, sets *zip to the zone id and returns 0.
5067*0209230bSgjelinek  * - If the string corresponds to the name of an active (booted) zone,
5068*0209230bSgjelinek  *   sets *zip to the zone id and returns 0.
5069*0209230bSgjelinek  * - If the string is a name in the configuration but is not booted,
5070*0209230bSgjelinek  *   sets *zip to ZONE_ID_UNDEFINED and returns 0.
5071*0209230bSgjelinek  * - Otherwise, leaves *zip unchanged and returns -1.
5072*0209230bSgjelinek  *
5073*0209230bSgjelinek  * This function acts as an auxiliary filter on the function of the same
5074*0209230bSgjelinek  * name in libc; the linker binds to this version if libzonecfg exists,
5075*0209230bSgjelinek  * and the libc version if it doesn't.  Any changes to this version of
5076*0209230bSgjelinek  * the function should probably be reflected in the libc version as well.
5077*0209230bSgjelinek  */
5078*0209230bSgjelinek int
5079*0209230bSgjelinek zone_get_id(const char *str, zoneid_t *zip)
5080*0209230bSgjelinek {
5081*0209230bSgjelinek 	zone_dochandle_t hdl;
5082*0209230bSgjelinek 	zoneid_t zoneid;
5083*0209230bSgjelinek 	char *cp;
5084*0209230bSgjelinek 	int err;
5085*0209230bSgjelinek 
5086*0209230bSgjelinek 	/* first try looking for active zone by id */
5087*0209230bSgjelinek 	errno = 0;
5088*0209230bSgjelinek 	zoneid = (zoneid_t)strtol(str, &cp, 0);
5089*0209230bSgjelinek 	if (errno == 0 && cp != str && *cp == '\0' &&
5090*0209230bSgjelinek 	    getzonenamebyid(zoneid, NULL, 0) != -1) {
5091*0209230bSgjelinek 		*zip = zoneid;
5092*0209230bSgjelinek 		return (0);
5093*0209230bSgjelinek 	}
5094*0209230bSgjelinek 
5095*0209230bSgjelinek 	/* then look for active zone by name */
5096*0209230bSgjelinek 	if ((zoneid = getzoneidbyname(str)) != -1) {
5097*0209230bSgjelinek 		*zip = zoneid;
5098*0209230bSgjelinek 		return (0);
5099*0209230bSgjelinek 	}
5100*0209230bSgjelinek 
5101*0209230bSgjelinek 	/* if in global zone, try looking up name in configuration database */
5102*0209230bSgjelinek 	if (getzoneid() != GLOBAL_ZONEID ||
5103*0209230bSgjelinek 	    (hdl = zonecfg_init_handle()) == NULL)
5104*0209230bSgjelinek 		return (-1);
5105*0209230bSgjelinek 
5106*0209230bSgjelinek 	if (zonecfg_get_handle(str, hdl) == Z_OK) {
5107*0209230bSgjelinek 		/* zone exists but isn't active */
5108*0209230bSgjelinek 		*zip = ZONE_ID_UNDEFINED;
5109*0209230bSgjelinek 		err = 0;
5110*0209230bSgjelinek 	} else {
5111*0209230bSgjelinek 		err = -1;
5112*0209230bSgjelinek 	}
5113*0209230bSgjelinek 
5114*0209230bSgjelinek 	zonecfg_fini_handle(hdl);
5115*0209230bSgjelinek 	return (err);
5116*0209230bSgjelinek }
5117*0209230bSgjelinek 
5118*0209230bSgjelinek char *
5119*0209230bSgjelinek zone_state_str(zone_state_t state_num)
5120*0209230bSgjelinek {
5121*0209230bSgjelinek 	switch (state_num) {
5122*0209230bSgjelinek 	case ZONE_STATE_CONFIGURED:
5123*0209230bSgjelinek 		return (ZONE_STATE_STR_CONFIGURED);
5124*0209230bSgjelinek 	case ZONE_STATE_INCOMPLETE:
5125*0209230bSgjelinek 		return (ZONE_STATE_STR_INCOMPLETE);
5126*0209230bSgjelinek 	case ZONE_STATE_INSTALLED:
5127*0209230bSgjelinek 		return (ZONE_STATE_STR_INSTALLED);
5128*0209230bSgjelinek 	case ZONE_STATE_READY:
5129*0209230bSgjelinek 		return (ZONE_STATE_STR_READY);
5130*0209230bSgjelinek 	case ZONE_STATE_MOUNTED:
5131*0209230bSgjelinek 		return (ZONE_STATE_STR_MOUNTED);
5132*0209230bSgjelinek 	case ZONE_STATE_RUNNING:
5133*0209230bSgjelinek 		return (ZONE_STATE_STR_RUNNING);
5134*0209230bSgjelinek 	case ZONE_STATE_SHUTTING_DOWN:
5135*0209230bSgjelinek 		return (ZONE_STATE_STR_SHUTTING_DOWN);
5136*0209230bSgjelinek 	case ZONE_STATE_DOWN:
5137*0209230bSgjelinek 		return (ZONE_STATE_STR_DOWN);
5138*0209230bSgjelinek 	default:
5139*0209230bSgjelinek 		return ("unknown");
5140*0209230bSgjelinek 	}
5141*0209230bSgjelinek }
5142*0209230bSgjelinek 
5143*0209230bSgjelinek /*
5144*0209230bSgjelinek  * Given a UUID value, find an associated zone name.  This is intended to be
5145*0209230bSgjelinek  * used by callers who set up some 'default' name (corresponding to the
5146*0209230bSgjelinek  * expected name for the zone) in the zonename buffer, and thus the function
5147*0209230bSgjelinek  * doesn't touch this buffer on failure.
5148*0209230bSgjelinek  */
5149*0209230bSgjelinek int
5150*0209230bSgjelinek zonecfg_get_name_by_uuid(const uuid_t uuidin, char *zonename, size_t namelen)
5151*0209230bSgjelinek {
5152*0209230bSgjelinek 	FILE *fp;
5153*0209230bSgjelinek 	struct zoneent *ze;
5154*0209230bSgjelinek 	uchar_t *uuid;
5155*0209230bSgjelinek 
5156*0209230bSgjelinek 	/*
5157*0209230bSgjelinek 	 * A small amount of subterfuge via casts is necessary here because
5158*0209230bSgjelinek 	 * libuuid doesn't use const correctly, but we don't want to export
5159*0209230bSgjelinek 	 * this brokenness to our clients.
5160*0209230bSgjelinek 	 */
5161*0209230bSgjelinek 	uuid = (uchar_t *)uuidin;
5162*0209230bSgjelinek 	if (uuid_is_null(uuid))
5163*0209230bSgjelinek 		return (Z_NO_ZONE);
5164*0209230bSgjelinek 	if ((fp = setzoneent()) == NULL)
5165*0209230bSgjelinek 		return (Z_NO_ZONE);
5166*0209230bSgjelinek 	while ((ze = getzoneent_private(fp)) != NULL) {
5167*0209230bSgjelinek 		if (uuid_compare(uuid, ze->zone_uuid) == 0)
5168*0209230bSgjelinek 			break;
5169*0209230bSgjelinek 		free(ze);
5170*0209230bSgjelinek 	}
5171*0209230bSgjelinek 	endzoneent(fp);
5172*0209230bSgjelinek 	if (ze != NULL) {
5173*0209230bSgjelinek 		(void) strlcpy(zonename, ze->zone_name, namelen);
5174*0209230bSgjelinek 		free(ze);
5175*0209230bSgjelinek 		return (Z_OK);
5176*0209230bSgjelinek 	} else {
5177*0209230bSgjelinek 		return (Z_NO_ZONE);
5178*0209230bSgjelinek 	}
5179*0209230bSgjelinek }
5180*0209230bSgjelinek 
5181*0209230bSgjelinek /*
5182*0209230bSgjelinek  * Given a zone name, get its UUID.  Returns a "NULL" UUID value if the zone
5183*0209230bSgjelinek  * exists but the file doesn't have a value set yet.  Returns an error if the
5184*0209230bSgjelinek  * zone cannot be located.
5185*0209230bSgjelinek  */
5186*0209230bSgjelinek int
5187*0209230bSgjelinek zonecfg_get_uuid(const char *zonename, uuid_t uuid)
5188*0209230bSgjelinek {
5189*0209230bSgjelinek 	FILE *fp;
5190*0209230bSgjelinek 	struct zoneent *ze;
5191*0209230bSgjelinek 
5192*0209230bSgjelinek 	if ((fp = setzoneent()) == NULL)
5193*0209230bSgjelinek 		return (Z_NO_ZONE);
5194*0209230bSgjelinek 	while ((ze = getzoneent_private(fp)) != NULL) {
5195*0209230bSgjelinek 		if (strcmp(ze->zone_name, zonename) == 0)
5196*0209230bSgjelinek 			break;
5197*0209230bSgjelinek 		free(ze);
5198*0209230bSgjelinek 	}
5199*0209230bSgjelinek 	endzoneent(fp);
5200*0209230bSgjelinek 	if (ze != NULL) {
5201*0209230bSgjelinek 		uuid_copy(uuid, ze->zone_uuid);
5202*0209230bSgjelinek 		free(ze);
5203*0209230bSgjelinek 		return (Z_OK);
5204*0209230bSgjelinek 	} else {
5205*0209230bSgjelinek 		return (Z_NO_ZONE);
5206*0209230bSgjelinek 	}
5207*0209230bSgjelinek }
5208*0209230bSgjelinek 
5209*0209230bSgjelinek /*
5210*0209230bSgjelinek  * File-system convenience functions.
5211*0209230bSgjelinek  */
5212*0209230bSgjelinek boolean_t
5213*0209230bSgjelinek zonecfg_valid_fs_type(const char *type)
5214*0209230bSgjelinek {
5215*0209230bSgjelinek 	/*
5216*0209230bSgjelinek 	 * We already know which FS types don't work.
5217*0209230bSgjelinek 	 */
5218*0209230bSgjelinek 	if (strcmp(type, "proc") == 0 ||
5219*0209230bSgjelinek 	    strcmp(type, "mntfs") == 0 ||
5220*0209230bSgjelinek 	    strcmp(type, "autofs") == 0 ||
5221*0209230bSgjelinek 	    strncmp(type, "nfs", sizeof ("nfs") - 1) == 0 ||
5222*0209230bSgjelinek 	    strcmp(type, "cachefs") == 0)
5223*0209230bSgjelinek 		return (B_FALSE);
5224*0209230bSgjelinek 	/*
5225*0209230bSgjelinek 	 * The caller may do more detailed verification to make sure other
5226*0209230bSgjelinek 	 * aspects of this filesystem type make sense.
5227*0209230bSgjelinek 	 */
5228*0209230bSgjelinek 	return (B_TRUE);
5229*0209230bSgjelinek }
5230*0209230bSgjelinek 
5231*0209230bSgjelinek /*
5232*0209230bSgjelinek  * Generally uninteresting rctl convenience functions.
5233*0209230bSgjelinek  */
5234*0209230bSgjelinek 
5235*0209230bSgjelinek int
5236*0209230bSgjelinek zonecfg_construct_rctlblk(const struct zone_rctlvaltab *rctlval,
5237*0209230bSgjelinek     rctlblk_t *rctlblk)
5238*0209230bSgjelinek {
5239*0209230bSgjelinek 	unsigned long long ull;
5240*0209230bSgjelinek 	char *endp;
5241*0209230bSgjelinek 	rctl_priv_t priv;
5242*0209230bSgjelinek 	rctl_qty_t limit;
5243*0209230bSgjelinek 	uint_t action;
5244*0209230bSgjelinek 
5245*0209230bSgjelinek 	/* Get the privilege */
5246*0209230bSgjelinek 	if (strcmp(rctlval->zone_rctlval_priv, "basic") == 0) {
5247*0209230bSgjelinek 		priv = RCPRIV_BASIC;
5248*0209230bSgjelinek 	} else if (strcmp(rctlval->zone_rctlval_priv, "privileged") == 0) {
5249*0209230bSgjelinek 		priv = RCPRIV_PRIVILEGED;
5250*0209230bSgjelinek 	} else {
5251*0209230bSgjelinek 		/* Invalid privilege */
5252*0209230bSgjelinek 		return (Z_INVAL);
5253*0209230bSgjelinek 	}
5254*0209230bSgjelinek 
5255*0209230bSgjelinek 	/* deal with negative input; strtoull(3c) doesn't do what we want */
5256*0209230bSgjelinek 	if (rctlval->zone_rctlval_limit[0] == '-')
5257*0209230bSgjelinek 		return (Z_INVAL);
5258*0209230bSgjelinek 	/* Get the limit */
5259*0209230bSgjelinek 	errno = 0;
5260*0209230bSgjelinek 	ull = strtoull(rctlval->zone_rctlval_limit, &endp, 0);
5261*0209230bSgjelinek 	if (errno != 0 || *endp != '\0') {
5262*0209230bSgjelinek 		/* parse failed */
5263*0209230bSgjelinek 		return (Z_INVAL);
5264*0209230bSgjelinek 	}
5265*0209230bSgjelinek 	limit = (rctl_qty_t)ull;
5266*0209230bSgjelinek 
5267*0209230bSgjelinek 	/* Get the action */
5268*0209230bSgjelinek 	if (strcmp(rctlval->zone_rctlval_action, "none") == 0) {
5269*0209230bSgjelinek 		action = RCTL_LOCAL_NOACTION;
5270*0209230bSgjelinek 	} else if (strcmp(rctlval->zone_rctlval_action, "signal") == 0) {
5271*0209230bSgjelinek 		action = RCTL_LOCAL_SIGNAL;
5272*0209230bSgjelinek 	} else if (strcmp(rctlval->zone_rctlval_action, "deny") == 0) {
5273*0209230bSgjelinek 		action = RCTL_LOCAL_DENY;
5274*0209230bSgjelinek 	} else {
5275*0209230bSgjelinek 		/* Invalid Action */
5276*0209230bSgjelinek 		return (Z_INVAL);
5277*0209230bSgjelinek 	}
5278*0209230bSgjelinek 	rctlblk_set_local_action(rctlblk, action, 0);
5279*0209230bSgjelinek 	rctlblk_set_privilege(rctlblk, priv);
5280*0209230bSgjelinek 	rctlblk_set_value(rctlblk, limit);
5281*0209230bSgjelinek 	return (Z_OK);
5282*0209230bSgjelinek }
5283*0209230bSgjelinek 
5284*0209230bSgjelinek static int
5285*0209230bSgjelinek rctl_check(const char *rctlname, void *arg)
5286*0209230bSgjelinek {
5287*0209230bSgjelinek 	const char *attrname = arg;
5288*0209230bSgjelinek 
5289*0209230bSgjelinek 	/*
5290*0209230bSgjelinek 	 * Returning 1 here is our signal to zonecfg_is_rctl() that it is
52917c478bd9Sstevel@tonic-gate 	 * indeed an rctl name recognized by the system.
52927c478bd9Sstevel@tonic-gate 	 */
5293*0209230bSgjelinek 	return (strcmp(rctlname, attrname) == 0 ? 1 : 0);
5294*0209230bSgjelinek }
5295*0209230bSgjelinek 
5296*0209230bSgjelinek boolean_t
5297*0209230bSgjelinek zonecfg_is_rctl(const char *name)
5298*0209230bSgjelinek {
5299*0209230bSgjelinek 	return (rctl_walk(rctl_check, (void *)name) == 1);
5300*0209230bSgjelinek }
5301*0209230bSgjelinek 
5302*0209230bSgjelinek boolean_t
5303*0209230bSgjelinek zonecfg_valid_rctlname(const char *name)
5304*0209230bSgjelinek {
5305*0209230bSgjelinek 	const char *c;
5306*0209230bSgjelinek 
5307*0209230bSgjelinek 	if (strncmp(name, "zone.", sizeof ("zone.") - 1) != 0)
5308*0209230bSgjelinek 		return (B_FALSE);
5309*0209230bSgjelinek 	if (strlen(name) == sizeof ("zone.") - 1)
5310*0209230bSgjelinek 		return (B_FALSE);
5311*0209230bSgjelinek 	for (c = name + sizeof ("zone.") - 1; *c != '\0'; c++) {
5312*0209230bSgjelinek 		if (!isalpha(*c) && *c != '-')
5313*0209230bSgjelinek 			return (B_FALSE);
5314*0209230bSgjelinek 	}
5315*0209230bSgjelinek 	return (B_TRUE);
5316*0209230bSgjelinek }
5317*0209230bSgjelinek 
5318*0209230bSgjelinek boolean_t
5319*0209230bSgjelinek zonecfg_valid_rctlblk(const rctlblk_t *rctlblk)
5320*0209230bSgjelinek {
5321*0209230bSgjelinek 	rctl_priv_t priv = rctlblk_get_privilege((rctlblk_t *)rctlblk);
5322*0209230bSgjelinek 	uint_t action = rctlblk_get_local_action((rctlblk_t *)rctlblk, NULL);
5323*0209230bSgjelinek 
5324*0209230bSgjelinek 	if (priv != RCPRIV_PRIVILEGED)
5325*0209230bSgjelinek 		return (B_FALSE);
5326*0209230bSgjelinek 	if (action != RCTL_LOCAL_NOACTION && action != RCTL_LOCAL_DENY)
5327*0209230bSgjelinek 		return (B_FALSE);
5328*0209230bSgjelinek 	return (B_TRUE);
5329*0209230bSgjelinek }
5330*0209230bSgjelinek 
5331*0209230bSgjelinek boolean_t
5332*0209230bSgjelinek zonecfg_valid_rctl(const char *name, const rctlblk_t *rctlblk)
5333*0209230bSgjelinek {
5334*0209230bSgjelinek 	rctlblk_t *current, *next;
5335*0209230bSgjelinek 	rctl_qty_t limit = rctlblk_get_value((rctlblk_t *)rctlblk);
5336*0209230bSgjelinek 	uint_t action = rctlblk_get_local_action((rctlblk_t *)rctlblk, NULL);
5337*0209230bSgjelinek 	uint_t global_flags;
5338*0209230bSgjelinek 
5339*0209230bSgjelinek 	if (!zonecfg_valid_rctlblk(rctlblk))
5340*0209230bSgjelinek 		return (B_FALSE);
5341*0209230bSgjelinek 	if (!zonecfg_valid_rctlname(name))
5342*0209230bSgjelinek 		return (B_FALSE);
5343*0209230bSgjelinek 
5344*0209230bSgjelinek 	current = alloca(rctlblk_size());
5345*0209230bSgjelinek 	if (getrctl(name, NULL, current, RCTL_FIRST) != 0)
5346*0209230bSgjelinek 		return (B_TRUE);	/* not an rctl on this system */
5347*0209230bSgjelinek 	/*
5348*0209230bSgjelinek 	 * Make sure the proposed value isn't greater than the current system
5349*0209230bSgjelinek 	 * value.
5350*0209230bSgjelinek 	 */
5351*0209230bSgjelinek 	next = alloca(rctlblk_size());
5352*0209230bSgjelinek 	while (rctlblk_get_privilege(current) != RCPRIV_SYSTEM) {
5353*0209230bSgjelinek 		rctlblk_t *tmp;
5354*0209230bSgjelinek 
5355*0209230bSgjelinek 		if (getrctl(name, current, next, RCTL_NEXT) != 0)
5356*0209230bSgjelinek 			return (B_FALSE);	/* shouldn't happen */
5357*0209230bSgjelinek 		tmp = current;
5358*0209230bSgjelinek 		current = next;
5359*0209230bSgjelinek 		next = tmp;
5360*0209230bSgjelinek 	}
5361*0209230bSgjelinek 	if (limit > rctlblk_get_value(current))
5362*0209230bSgjelinek 		return (B_FALSE);
5363*0209230bSgjelinek 
5364*0209230bSgjelinek 	/*
5365*0209230bSgjelinek 	 * Make sure the proposed action is allowed.
5366*0209230bSgjelinek 	 */
5367*0209230bSgjelinek 	global_flags = rctlblk_get_global_flags(current);
5368*0209230bSgjelinek 	if ((global_flags & RCTL_GLOBAL_DENY_NEVER) &&
5369*0209230bSgjelinek 	    action == RCTL_LOCAL_DENY)
5370*0209230bSgjelinek 		return (B_FALSE);
5371*0209230bSgjelinek 	if ((global_flags & RCTL_GLOBAL_DENY_ALWAYS) &&
5372*0209230bSgjelinek 	    action == RCTL_LOCAL_NOACTION)
5373*0209230bSgjelinek 		return (B_FALSE);
5374*0209230bSgjelinek 
5375*0209230bSgjelinek 	return (B_TRUE);
5376*0209230bSgjelinek }
5377*0209230bSgjelinek 
5378*0209230bSgjelinek /*
5379*0209230bSgjelinek  * There is always a race condition between reading the initial copy of
5380*0209230bSgjelinek  * a zones state and its state changing.  We address this by providing
5381*0209230bSgjelinek  * zonecfg_notify_critical_enter and zonecfg_noticy_critical_exit functions.
5382*0209230bSgjelinek  * When zonecfg_critical_enter is called, sets the state field to LOCKED
5383*0209230bSgjelinek  * and aquires biglock. Biglock protects against other threads executing
5384*0209230bSgjelinek  * critical_enter and the state field protects against state changes during
5385*0209230bSgjelinek  * the critical period.
5386*0209230bSgjelinek  *
5387*0209230bSgjelinek  * If any state changes occur, zn_cb will set the failed field of the znotify
5388*0209230bSgjelinek  * structure.  This will cause the critical_exit function to re-lock the
5389*0209230bSgjelinek  * channel and return an error. Since evsnts may be delayed, the critical_exit
5390*0209230bSgjelinek  * function "flushes" the queue by putting an event on the queue and waiting for
5391*0209230bSgjelinek  * zn_cb to notify critical_exit that it received the ping event.
5392*0209230bSgjelinek  */
5393*0209230bSgjelinek static const char *
5394*0209230bSgjelinek string_get_tok(const char *in, char delim, int num)
5395*0209230bSgjelinek {
5396*0209230bSgjelinek 	int i = 0;
5397*0209230bSgjelinek 
5398*0209230bSgjelinek 	for (; i < num; in++) {
5399*0209230bSgjelinek 		if (*in == delim)
5400*0209230bSgjelinek 			i++;
5401*0209230bSgjelinek 		if (*in == 0)
5402*0209230bSgjelinek 			return (NULL);
5403*0209230bSgjelinek 	}
5404*0209230bSgjelinek 	return (in);
5405*0209230bSgjelinek }
5406*0209230bSgjelinek 
5407*0209230bSgjelinek static boolean_t
5408*0209230bSgjelinek is_ping(sysevent_t *ev)
5409*0209230bSgjelinek {
5410*0209230bSgjelinek 	if (strcmp(sysevent_get_subclass_name(ev),
5411*0209230bSgjelinek 	    ZONE_EVENT_PING_SUBCLASS) == 0) {
5412*0209230bSgjelinek 		return (B_TRUE);
5413*0209230bSgjelinek 	} else {
5414*0209230bSgjelinek 		return (B_FALSE);
5415*0209230bSgjelinek 	}
5416*0209230bSgjelinek }
5417*0209230bSgjelinek 
5418*0209230bSgjelinek static boolean_t
5419*0209230bSgjelinek is_my_ping(sysevent_t *ev)
5420*0209230bSgjelinek {
5421*0209230bSgjelinek 	const char *sender;
5422*0209230bSgjelinek 	char mypid[sizeof (pid_t) * 3 + 1];
5423*0209230bSgjelinek 
5424*0209230bSgjelinek 	(void) snprintf(mypid, sizeof (mypid), "%i", getpid());
5425*0209230bSgjelinek 	sender = string_get_tok(sysevent_get_pub(ev), ':', 3);
5426*0209230bSgjelinek 	if (sender == NULL)
5427*0209230bSgjelinek 		return (B_FALSE);
5428*0209230bSgjelinek 	if (strcmp(sender, mypid) != 0)
5429*0209230bSgjelinek 		return (B_FALSE);
5430*0209230bSgjelinek 	return (B_TRUE);
5431*0209230bSgjelinek }
5432*0209230bSgjelinek 
5433*0209230bSgjelinek static int
5434*0209230bSgjelinek do_callback(struct znotify *zevtchan, sysevent_t *ev)
5435*0209230bSgjelinek {
5436*0209230bSgjelinek 	nvlist_t *l;
5437*0209230bSgjelinek 	int zid;
5438*0209230bSgjelinek 	char *zonename;
5439*0209230bSgjelinek 	char *newstate;
5440*0209230bSgjelinek 	char *oldstate;
5441*0209230bSgjelinek 	int ret;
5442*0209230bSgjelinek 	hrtime_t when;
5443*0209230bSgjelinek 
5444*0209230bSgjelinek 	if (strcmp(sysevent_get_subclass_name(ev),
5445*0209230bSgjelinek 	    ZONE_EVENT_STATUS_SUBCLASS) == 0) {
5446*0209230bSgjelinek 
5447*0209230bSgjelinek 		if (sysevent_get_attr_list(ev, &l) != 0) {
5448*0209230bSgjelinek 			if (errno == ENOMEM) {
5449*0209230bSgjelinek 				zevtchan->zn_failure_count++;
5450*0209230bSgjelinek 				return (EAGAIN);
5451*0209230bSgjelinek 			}
5452*0209230bSgjelinek 			return (0);
5453*0209230bSgjelinek 		}
5454*0209230bSgjelinek 		ret = 0;
5455*0209230bSgjelinek 
5456*0209230bSgjelinek 		if ((nvlist_lookup_string(l, ZONE_CB_NAME, &zonename) == 0) &&
5457*0209230bSgjelinek 		    (nvlist_lookup_string(l, ZONE_CB_NEWSTATE, &newstate)
5458*0209230bSgjelinek 		    == 0) &&
5459*0209230bSgjelinek 		    (nvlist_lookup_string(l, ZONE_CB_OLDSTATE, &oldstate)
5460*0209230bSgjelinek 		    == 0) &&
5461*0209230bSgjelinek 		    (nvlist_lookup_uint64(l, ZONE_CB_TIMESTAMP,
5462*0209230bSgjelinek 		    (uint64_t *)&when) == 0) &&
5463*0209230bSgjelinek 		    (nvlist_lookup_int32(l, ZONE_CB_ZONEID, &zid) == 0)) {
5464*0209230bSgjelinek 			ret = zevtchan->zn_callback(zonename, zid, newstate,
5465*0209230bSgjelinek 			    oldstate, when, zevtchan->zn_private);
5466*0209230bSgjelinek 		}
5467*0209230bSgjelinek 
5468*0209230bSgjelinek 		zevtchan->zn_failure_count = 0;
5469*0209230bSgjelinek 		nvlist_free(l);
5470*0209230bSgjelinek 		return (ret);
5471*0209230bSgjelinek 	} else {
5472*0209230bSgjelinek 		/*
5473*0209230bSgjelinek 		 * We have received an event in an unknown subclass. Ignore.
5474*0209230bSgjelinek 		 */
5475*0209230bSgjelinek 		zevtchan->zn_failure_count = 0;
5476*0209230bSgjelinek 		return (0);
5477*0209230bSgjelinek 	}
5478*0209230bSgjelinek }
5479*0209230bSgjelinek 
5480*0209230bSgjelinek static int
5481*0209230bSgjelinek zn_cb(sysevent_t *ev, void *p)
5482*0209230bSgjelinek {
5483*0209230bSgjelinek 	struct znotify *zevtchan = p;
5484*0209230bSgjelinek 	int error;
5485*0209230bSgjelinek 
5486*0209230bSgjelinek 	(void) pthread_mutex_lock(&(zevtchan->zn_mutex));
5487*0209230bSgjelinek 
5488*0209230bSgjelinek 	if (is_ping(ev) && !is_my_ping(ev)) {
5489*0209230bSgjelinek 		(void) pthread_mutex_unlock((&zevtchan->zn_mutex));
5490*0209230bSgjelinek 		return (0);
5491*0209230bSgjelinek 	}
5492*0209230bSgjelinek 
5493*0209230bSgjelinek 	if (zevtchan->zn_state == ZN_LOCKED) {
5494*0209230bSgjelinek 		assert(!is_ping(ev));
5495*0209230bSgjelinek 		zevtchan->zn_failed = B_TRUE;
5496*0209230bSgjelinek 		(void) pthread_mutex_unlock(&(zevtchan->zn_mutex));
5497*0209230bSgjelinek 		return (0);
5498*0209230bSgjelinek 	}
5499*0209230bSgjelinek 
5500*0209230bSgjelinek 	if (zevtchan->zn_state == ZN_PING_INFLIGHT) {
5501*0209230bSgjelinek 		if (is_ping(ev)) {
5502*0209230bSgjelinek 			zevtchan->zn_state = ZN_PING_RECEIVED;
5503*0209230bSgjelinek 			(void) pthread_cond_signal(&(zevtchan->zn_cond));
5504*0209230bSgjelinek 			(void) pthread_mutex_unlock(&(zevtchan->zn_mutex));
5505*0209230bSgjelinek 			return (0);
5506*0209230bSgjelinek 		} else {
5507*0209230bSgjelinek 			zevtchan->zn_failed = B_TRUE;
5508*0209230bSgjelinek 			(void) pthread_mutex_unlock(&(zevtchan->zn_mutex));
5509*0209230bSgjelinek 			return (0);
5510*0209230bSgjelinek 		}
5511*0209230bSgjelinek 	}
5512*0209230bSgjelinek 
5513*0209230bSgjelinek 	if (zevtchan->zn_state == ZN_UNLOCKED) {
5514*0209230bSgjelinek 
5515*0209230bSgjelinek 		error = do_callback(zevtchan, ev);
5516*0209230bSgjelinek 		(void) pthread_mutex_unlock(&(zevtchan->zn_mutex));
5517*0209230bSgjelinek 		/*
5518*0209230bSgjelinek 		 * Every ENOMEM failure causes do_callback to increment
5519*0209230bSgjelinek 		 * zn_failure_count and every success causes it to
5520*0209230bSgjelinek 		 * set zn_failure_count to zero.  If we got EAGAIN,
5521*0209230bSgjelinek 		 * we will sleep for zn_failure_count seconds and return
5522*0209230bSgjelinek 		 * EAGAIN to gpec to try again.
5523*0209230bSgjelinek 		 *
5524*0209230bSgjelinek 		 * After 55 seconds, or 10 try's we give up and drop the
5525*0209230bSgjelinek 		 * event.
5526*0209230bSgjelinek 		 */
5527*0209230bSgjelinek 		if (error == EAGAIN) {
5528*0209230bSgjelinek 			if (zevtchan->zn_failure_count > ZONE_CB_RETRY_COUNT) {
5529*0209230bSgjelinek 				return (0);
5530*0209230bSgjelinek 			}
5531*0209230bSgjelinek 			(void) sleep(zevtchan->zn_failure_count);
5532*0209230bSgjelinek 		}
5533*0209230bSgjelinek 		return (error);
5534*0209230bSgjelinek 	}
5535*0209230bSgjelinek 
5536*0209230bSgjelinek 	if (zevtchan->zn_state == ZN_PING_RECEIVED) {
5537*0209230bSgjelinek 		(void) pthread_mutex_unlock(&(zevtchan->zn_mutex));
5538*0209230bSgjelinek 		return (0);
5539*0209230bSgjelinek 	}
5540*0209230bSgjelinek 
5541*0209230bSgjelinek 	abort();
5542*0209230bSgjelinek 	return (0);
5543*0209230bSgjelinek }
5544*0209230bSgjelinek 
5545*0209230bSgjelinek void
5546*0209230bSgjelinek zonecfg_notify_critical_enter(void *h)
5547*0209230bSgjelinek {
5548*0209230bSgjelinek 	struct znotify *zevtchan = h;
5549*0209230bSgjelinek 
5550*0209230bSgjelinek 	(void) pthread_mutex_lock(&(zevtchan->zn_bigmutex));
5551*0209230bSgjelinek 	zevtchan->zn_state = ZN_LOCKED;
5552*0209230bSgjelinek }
5553*0209230bSgjelinek 
5554*0209230bSgjelinek int
5555*0209230bSgjelinek zonecfg_notify_critical_exit(void * h)
5556*0209230bSgjelinek {
5557*0209230bSgjelinek 
5558*0209230bSgjelinek 	struct znotify *zevtchan = h;
5559*0209230bSgjelinek 
5560*0209230bSgjelinek 	if (zevtchan->zn_state == ZN_UNLOCKED)
5561*0209230bSgjelinek 		return (0);
5562*0209230bSgjelinek 
5563*0209230bSgjelinek 	(void) pthread_mutex_lock(&(zevtchan->zn_mutex));
5564*0209230bSgjelinek 	zevtchan->zn_state = ZN_PING_INFLIGHT;
5565*0209230bSgjelinek 
5566*0209230bSgjelinek 	(void) sysevent_evc_publish(zevtchan->zn_eventchan,
5567*0209230bSgjelinek 	    ZONE_EVENT_STATUS_CLASS,
5568*0209230bSgjelinek 	    ZONE_EVENT_PING_SUBCLASS, ZONE_EVENT_PING_PUBLISHER,
5569*0209230bSgjelinek 	    zevtchan->zn_subscriber_id, NULL, EVCH_SLEEP);
5570*0209230bSgjelinek 
5571*0209230bSgjelinek 	while (zevtchan->zn_state != ZN_PING_RECEIVED) {
5572*0209230bSgjelinek 		(void) pthread_cond_wait(&(zevtchan->zn_cond),
5573*0209230bSgjelinek 		    &(zevtchan->zn_mutex));
5574*0209230bSgjelinek 	}
5575*0209230bSgjelinek 
5576*0209230bSgjelinek 	if (zevtchan->zn_failed == B_TRUE) {
5577*0209230bSgjelinek 		zevtchan->zn_state = ZN_LOCKED;
5578*0209230bSgjelinek 		zevtchan->zn_failed = B_FALSE;
5579*0209230bSgjelinek 		(void) pthread_mutex_unlock(&(zevtchan->zn_mutex));
5580*0209230bSgjelinek 		return (1);
5581*0209230bSgjelinek 	}
5582*0209230bSgjelinek 
5583*0209230bSgjelinek 	zevtchan->zn_state = ZN_UNLOCKED;
5584*0209230bSgjelinek 	(void) pthread_mutex_unlock(&(zevtchan->zn_mutex));
5585*0209230bSgjelinek 	(void) pthread_mutex_unlock(&(zevtchan->zn_bigmutex));
5586*0209230bSgjelinek 	return (0);
55877c478bd9Sstevel@tonic-gate }
55887c478bd9Sstevel@tonic-gate 
5589*0209230bSgjelinek void
5590*0209230bSgjelinek zonecfg_notify_critical_abort(void *h)
55917c478bd9Sstevel@tonic-gate {
5592*0209230bSgjelinek 	struct znotify *zevtchan = h;
5593*0209230bSgjelinek 
5594*0209230bSgjelinek 	zevtchan->zn_state = ZN_UNLOCKED;
5595*0209230bSgjelinek 	zevtchan->zn_failed = B_FALSE;
5596*0209230bSgjelinek 	/*
5597*0209230bSgjelinek 	 * Don't do anything about zn_lock. If it is held, it could only be
5598*0209230bSgjelinek 	 * held by zn_cb and it will be unlocked soon.
5599*0209230bSgjelinek 	 */
5600*0209230bSgjelinek 	(void) pthread_mutex_unlock(&(zevtchan->zn_bigmutex));
56017c478bd9Sstevel@tonic-gate }
56027c478bd9Sstevel@tonic-gate 
5603*0209230bSgjelinek void *
5604*0209230bSgjelinek zonecfg_notify_bind(int(*func)(const char *zonename, zoneid_t zid,
5605*0209230bSgjelinek     const char *newstate, const char *oldstate, hrtime_t when, void *p),
5606*0209230bSgjelinek     void *p)
56077c478bd9Sstevel@tonic-gate {
5608*0209230bSgjelinek 	struct znotify *zevtchan;
5609*0209230bSgjelinek 	int i = 1;
5610*0209230bSgjelinek 	int r;
56117c478bd9Sstevel@tonic-gate 
5612*0209230bSgjelinek 	zevtchan = malloc(sizeof (struct znotify));
5613*0209230bSgjelinek 
5614*0209230bSgjelinek 	if (zevtchan == NULL)
5615*0209230bSgjelinek 		return (NULL);
5616*0209230bSgjelinek 
5617*0209230bSgjelinek 	zevtchan->zn_private = p;
5618*0209230bSgjelinek 	zevtchan->zn_callback = func;
5619*0209230bSgjelinek 	zevtchan->zn_state = ZN_UNLOCKED;
5620*0209230bSgjelinek 	zevtchan->zn_failed = B_FALSE;
5621*0209230bSgjelinek 
5622*0209230bSgjelinek 	if (pthread_mutex_init(&(zevtchan->zn_mutex), NULL))
5623*0209230bSgjelinek 		goto out3;
5624*0209230bSgjelinek 	if (pthread_cond_init(&(zevtchan->zn_cond), NULL)) {
5625*0209230bSgjelinek 		(void) pthread_mutex_destroy(&(zevtchan->zn_mutex));
5626*0209230bSgjelinek 		goto out3;
5627*0209230bSgjelinek 	}
5628*0209230bSgjelinek 	if (pthread_mutex_init(&(zevtchan->zn_bigmutex), NULL)) {
5629*0209230bSgjelinek 		(void) pthread_mutex_destroy(&(zevtchan->zn_mutex));
5630*0209230bSgjelinek 		(void) pthread_cond_destroy(&(zevtchan->zn_cond));
5631*0209230bSgjelinek 		goto out3;
56327c478bd9Sstevel@tonic-gate 	}
56337c478bd9Sstevel@tonic-gate 
5634*0209230bSgjelinek 	if (sysevent_evc_bind(ZONE_EVENT_CHANNEL, &(zevtchan->zn_eventchan),
5635*0209230bSgjelinek 	    0) != 0)
5636*0209230bSgjelinek 		goto out2;
56377c478bd9Sstevel@tonic-gate 
5638*0209230bSgjelinek 	do {
5639*0209230bSgjelinek 		/*
5640*0209230bSgjelinek 		 * At 4 digits the subscriber ID gets too long and we have
5641*0209230bSgjelinek 		 * no chance of successfully registering.
5642*0209230bSgjelinek 		 */
5643*0209230bSgjelinek 		if (i > 999)
5644*0209230bSgjelinek 			goto out1;
5645*0209230bSgjelinek 
5646*0209230bSgjelinek 		(void) sprintf(zevtchan->zn_subscriber_id, "zone_%li_%i",
5647*0209230bSgjelinek 		    getpid() % 999999l, i);
5648*0209230bSgjelinek 
5649*0209230bSgjelinek 		r = sysevent_evc_subscribe(zevtchan->zn_eventchan,
5650*0209230bSgjelinek 		    zevtchan->zn_subscriber_id, ZONE_EVENT_STATUS_CLASS, zn_cb,
5651*0209230bSgjelinek 		    zevtchan, 0);
5652*0209230bSgjelinek 
5653*0209230bSgjelinek 		i++;
5654*0209230bSgjelinek 
5655*0209230bSgjelinek 	} while (r);
5656*0209230bSgjelinek 
5657*0209230bSgjelinek 	return (zevtchan);
5658*0209230bSgjelinek out1:
5659*0209230bSgjelinek 	sysevent_evc_unbind(zevtchan->zn_eventchan);
5660*0209230bSgjelinek out2:
5661*0209230bSgjelinek 	(void) pthread_mutex_destroy(&zevtchan->zn_mutex);
5662*0209230bSgjelinek 	(void) pthread_cond_destroy(&zevtchan->zn_cond);
5663*0209230bSgjelinek 	(void) pthread_mutex_destroy(&(zevtchan->zn_bigmutex));
5664*0209230bSgjelinek out3:
5665*0209230bSgjelinek 	free(zevtchan);
5666*0209230bSgjelinek 
5667*0209230bSgjelinek 	return (NULL);
56687c478bd9Sstevel@tonic-gate }
56697c478bd9Sstevel@tonic-gate 
5670*0209230bSgjelinek void
5671*0209230bSgjelinek zonecfg_notify_unbind(void *handle)
56727c478bd9Sstevel@tonic-gate {
56737c478bd9Sstevel@tonic-gate 
5674*0209230bSgjelinek 	int ret;
56757c478bd9Sstevel@tonic-gate 
5676*0209230bSgjelinek 	sysevent_evc_unbind(((struct znotify *)handle)->zn_eventchan);
56777c478bd9Sstevel@tonic-gate 	/*
5678*0209230bSgjelinek 	 * Check that all evc threads have gone away. This should be
5679*0209230bSgjelinek 	 * enforced by sysevent_evc_unbind.
56807c478bd9Sstevel@tonic-gate 	 */
5681*0209230bSgjelinek 	ret = pthread_mutex_trylock(&((struct znotify *)handle)->zn_mutex);
56827c478bd9Sstevel@tonic-gate 
5683*0209230bSgjelinek 	if (ret)
5684*0209230bSgjelinek 		abort();
56857c478bd9Sstevel@tonic-gate 
5686*0209230bSgjelinek 	(void) pthread_mutex_unlock(&((struct znotify *)handle)->zn_mutex);
5687*0209230bSgjelinek 	(void) pthread_mutex_destroy(&((struct znotify *)handle)->zn_mutex);
5688*0209230bSgjelinek 	(void) pthread_cond_destroy(&((struct znotify *)handle)->zn_cond);
5689*0209230bSgjelinek 	(void) pthread_mutex_destroy(&((struct znotify *)handle)->zn_bigmutex);
56907c478bd9Sstevel@tonic-gate 
5691*0209230bSgjelinek 	free(handle);
56927c478bd9Sstevel@tonic-gate }
5693fa9e4066Sahrens 
5694*0209230bSgjelinek static int
5695*0209230bSgjelinek zonecfg_add_ds_core(zone_dochandle_t handle, struct zone_dstab *tabptr)
5696cf8f45c7Sdstaff {
5697*0209230bSgjelinek 	xmlNodePtr newnode, cur = handle->zone_dh_cur;
5698*0209230bSgjelinek 	int err;
5699cf8f45c7Sdstaff 
5700*0209230bSgjelinek 	newnode = xmlNewTextChild(cur, NULL, DTD_ELEM_DATASET, NULL);
5701*0209230bSgjelinek 	if ((err = newprop(newnode, DTD_ATTR_NAME,
5702*0209230bSgjelinek 	    tabptr->zone_dataset_name)) != Z_OK)
5703*0209230bSgjelinek 		return (err);
5704*0209230bSgjelinek 	return (Z_OK);
5705cf8f45c7Sdstaff }
5706cf8f45c7Sdstaff 
5707*0209230bSgjelinek int
5708*0209230bSgjelinek zonecfg_add_ds(zone_dochandle_t handle, struct zone_dstab *tabptr)
5709cf8f45c7Sdstaff {
5710*0209230bSgjelinek 	int err;
5711*0209230bSgjelinek 
5712*0209230bSgjelinek 	if (tabptr == NULL)
5713*0209230bSgjelinek 		return (Z_INVAL);
5714*0209230bSgjelinek 
5715*0209230bSgjelinek 	if ((err = operation_prep(handle)) != Z_OK)
5716*0209230bSgjelinek 		return (err);
5717*0209230bSgjelinek 
5718*0209230bSgjelinek 	if ((err = zonecfg_add_ds_core(handle, tabptr)) != Z_OK)
5719*0209230bSgjelinek 		return (err);
5720*0209230bSgjelinek 
5721*0209230bSgjelinek 	return (Z_OK);
5722*0209230bSgjelinek }
5723*0209230bSgjelinek 
5724*0209230bSgjelinek static int
5725*0209230bSgjelinek zonecfg_delete_ds_core(zone_dochandle_t handle, struct zone_dstab *tabptr)
5726*0209230bSgjelinek {
5727*0209230bSgjelinek 	xmlNodePtr cur = handle->zone_dh_cur;
5728*0209230bSgjelinek 
5729*0209230bSgjelinek 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
5730*0209230bSgjelinek 		if (xmlStrcmp(cur->name, DTD_ELEM_DATASET))
5731*0209230bSgjelinek 			continue;
5732*0209230bSgjelinek 
5733*0209230bSgjelinek 		if (match_prop(cur, DTD_ATTR_NAME,
5734*0209230bSgjelinek 		    tabptr->zone_dataset_name)) {
5735*0209230bSgjelinek 			xmlUnlinkNode(cur);
5736*0209230bSgjelinek 			xmlFreeNode(cur);
5737*0209230bSgjelinek 			return (Z_OK);
5738*0209230bSgjelinek 		}
5739cf8f45c7Sdstaff 	}
5740*0209230bSgjelinek 	return (Z_NO_RESOURCE_ID);
5741*0209230bSgjelinek }
5742*0209230bSgjelinek 
5743*0209230bSgjelinek int
5744*0209230bSgjelinek zonecfg_delete_ds(zone_dochandle_t handle, struct zone_dstab *tabptr)
5745*0209230bSgjelinek {
5746*0209230bSgjelinek 	int err;
5747*0209230bSgjelinek 
5748*0209230bSgjelinek 	if (tabptr == NULL)
5749*0209230bSgjelinek 		return (Z_INVAL);
5750*0209230bSgjelinek 
5751*0209230bSgjelinek 	if ((err = operation_prep(handle)) != Z_OK)
5752*0209230bSgjelinek 		return (err);
5753*0209230bSgjelinek 
5754*0209230bSgjelinek 	if ((err = zonecfg_delete_ds_core(handle, tabptr)) != Z_OK)
5755*0209230bSgjelinek 		return (err);
5756*0209230bSgjelinek 
5757*0209230bSgjelinek 	return (Z_OK);
5758cf8f45c7Sdstaff }
5759cf8f45c7Sdstaff 
5760*0209230bSgjelinek int
5761*0209230bSgjelinek zonecfg_modify_ds(
5762*0209230bSgjelinek 	zone_dochandle_t handle,
5763*0209230bSgjelinek 	struct zone_dstab *oldtabptr,
5764*0209230bSgjelinek 	struct zone_dstab *newtabptr)
5765*0209230bSgjelinek {
5766*0209230bSgjelinek 	int err;
5767*0209230bSgjelinek 
5768*0209230bSgjelinek 	if (oldtabptr == NULL || newtabptr == NULL)
5769*0209230bSgjelinek 		return (Z_INVAL);
5770*0209230bSgjelinek 
5771*0209230bSgjelinek 	if ((err = operation_prep(handle)) != Z_OK)
5772*0209230bSgjelinek 		return (err);
5773cf8f45c7Sdstaff 
5774*0209230bSgjelinek 	if ((err = zonecfg_delete_ds_core(handle, oldtabptr)) != Z_OK)
5775*0209230bSgjelinek 		return (err);
5776*0209230bSgjelinek 
5777*0209230bSgjelinek 	if ((err = zonecfg_add_ds_core(handle, newtabptr)) != Z_OK)
5778*0209230bSgjelinek 		return (err);
5779*0209230bSgjelinek 
5780*0209230bSgjelinek 	return (Z_OK);
5781cf8f45c7Sdstaff }
5782cf8f45c7Sdstaff 
5783*0209230bSgjelinek int
5784*0209230bSgjelinek zonecfg_lookup_ds(zone_dochandle_t handle, struct zone_dstab *tabptr)
5785cf8f45c7Sdstaff {
5786*0209230bSgjelinek 	xmlNodePtr cur, firstmatch;
5787*0209230bSgjelinek 	int err;
5788*0209230bSgjelinek 	char dataset[MAXNAMELEN];
5789cf8f45c7Sdstaff 
5790*0209230bSgjelinek 	if (tabptr == NULL)
5791*0209230bSgjelinek 		return (Z_INVAL);
5792cf8f45c7Sdstaff 
5793*0209230bSgjelinek 	if ((err = operation_prep(handle)) != Z_OK)
5794*0209230bSgjelinek 		return (err);
5795*0209230bSgjelinek 
5796*0209230bSgjelinek 	cur = handle->zone_dh_cur;
5797*0209230bSgjelinek 	firstmatch = NULL;
5798*0209230bSgjelinek 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
5799*0209230bSgjelinek 		if (xmlStrcmp(cur->name, DTD_ELEM_DATASET))
5800*0209230bSgjelinek 			continue;
5801*0209230bSgjelinek 		if (strlen(tabptr->zone_dataset_name) > 0) {
5802*0209230bSgjelinek 			if ((fetchprop(cur, DTD_ATTR_NAME, dataset,
5803*0209230bSgjelinek 			    sizeof (dataset)) == Z_OK) &&
5804*0209230bSgjelinek 			    (strcmp(tabptr->zone_dataset_name,
5805*0209230bSgjelinek 			    dataset) == 0)) {
5806*0209230bSgjelinek 				if (firstmatch == NULL)
5807*0209230bSgjelinek 					firstmatch = cur;
5808*0209230bSgjelinek 				else
5809*0209230bSgjelinek 					return (Z_INSUFFICIENT_SPEC);
5810cf8f45c7Sdstaff 			}
5811cf8f45c7Sdstaff 		}
5812*0209230bSgjelinek 	}
5813*0209230bSgjelinek 	if (firstmatch == NULL)
5814*0209230bSgjelinek 		return (Z_NO_RESOURCE_ID);
5815cf8f45c7Sdstaff 
5816*0209230bSgjelinek 	cur = firstmatch;
5817cf8f45c7Sdstaff 
5818*0209230bSgjelinek 	if ((err = fetchprop(cur, DTD_ATTR_NAME, tabptr->zone_dataset_name,
5819*0209230bSgjelinek 	    sizeof (tabptr->zone_dataset_name))) != Z_OK)
5820*0209230bSgjelinek 		return (err);
5821*0209230bSgjelinek 
5822*0209230bSgjelinek 	return (Z_OK);
5823cf8f45c7Sdstaff }
5824cf8f45c7Sdstaff 
5825*0209230bSgjelinek int
5826*0209230bSgjelinek zonecfg_setdsent(zone_dochandle_t handle)
5827cf8f45c7Sdstaff {
5828*0209230bSgjelinek 	return (zonecfg_setent(handle));
5829*0209230bSgjelinek }
5830cf8f45c7Sdstaff 
5831*0209230bSgjelinek int
5832*0209230bSgjelinek zonecfg_getdsent(zone_dochandle_t handle, struct zone_dstab *tabptr)
5833*0209230bSgjelinek {
5834*0209230bSgjelinek 	xmlNodePtr cur;
5835*0209230bSgjelinek 	int err;
5836cf8f45c7Sdstaff 
5837*0209230bSgjelinek 	if (handle == NULL)
5838*0209230bSgjelinek 		return (Z_INVAL);
5839cf8f45c7Sdstaff 
5840*0209230bSgjelinek 	if ((cur = handle->zone_dh_cur) == NULL)
5841*0209230bSgjelinek 		return (Z_NO_ENTRY);
5842cf8f45c7Sdstaff 
5843*0209230bSgjelinek 	for (; cur != NULL; cur = cur->next)
5844*0209230bSgjelinek 		if (!xmlStrcmp(cur->name, DTD_ELEM_DATASET))
5845*0209230bSgjelinek 			break;
5846*0209230bSgjelinek 	if (cur == NULL) {
5847*0209230bSgjelinek 		handle->zone_dh_cur = handle->zone_dh_top;
5848*0209230bSgjelinek 		return (Z_NO_ENTRY);
5849cf8f45c7Sdstaff 	}
5850cf8f45c7Sdstaff 
5851*0209230bSgjelinek 	if ((err = fetchprop(cur, DTD_ATTR_NAME, tabptr->zone_dataset_name,
5852*0209230bSgjelinek 	    sizeof (tabptr->zone_dataset_name))) != Z_OK) {
5853*0209230bSgjelinek 		handle->zone_dh_cur = handle->zone_dh_top;
5854*0209230bSgjelinek 		return (err);
5855cf8f45c7Sdstaff 	}
5856cf8f45c7Sdstaff 
5857*0209230bSgjelinek 	handle->zone_dh_cur = cur->next;
5858*0209230bSgjelinek 	return (Z_OK);
5859cf8f45c7Sdstaff }
5860cf8f45c7Sdstaff 
5861*0209230bSgjelinek int
5862*0209230bSgjelinek zonecfg_enddsent(zone_dochandle_t handle)
5863cf8f45c7Sdstaff {
5864*0209230bSgjelinek 	return (zonecfg_endent(handle));
5865cf8f45c7Sdstaff }
5866cf8f45c7Sdstaff 
5867*0209230bSgjelinek /*
5868*0209230bSgjelinek  * Support for aliased rctls; that is, rctls that have simplified names in
5869*0209230bSgjelinek  * zonecfg.  For example, max-lwps is an alias for a well defined zone.max-lwps
5870*0209230bSgjelinek  * rctl.  If there are multiple existing values for one of these rctls or if
5871*0209230bSgjelinek  * there is a single value that does not match the well defined template (i.e.
5872*0209230bSgjelinek  * it has a different action) then we cannot treat the rctl as having an alias
5873*0209230bSgjelinek  * so we return Z_ALIAS_DISALLOW.  That means that the rctl cannot be
5874*0209230bSgjelinek  * managed in zonecfg via an alias and that the standard rctl syntax must be
5875*0209230bSgjelinek  * used.
5876*0209230bSgjelinek  *
5877*0209230bSgjelinek  * The possible return values are:
5878*0209230bSgjelinek  *	Z_NO_PROPERTY_ID - invalid alias name
5879*0209230bSgjelinek  *	Z_ALIAS_DISALLOW - pre-existing, incompatible rctl definition
5880*0209230bSgjelinek  *	Z_NO_ENTRY - no rctl is configured for this alias
5881*0209230bSgjelinek  *	Z_OK - we got a valid rctl for the specified alias
5882*0209230bSgjelinek  */
5883cf8f45c7Sdstaff int
5884*0209230bSgjelinek zonecfg_get_aliased_rctl(zone_dochandle_t handle, char *name, uint64_t *rval)
5885cf8f45c7Sdstaff {
5886*0209230bSgjelinek 	boolean_t found = B_FALSE;
5887*0209230bSgjelinek 	boolean_t found_val = B_FALSE;
5888*0209230bSgjelinek 	xmlNodePtr cur, val;
5889*0209230bSgjelinek 	char savedname[MAXNAMELEN];
5890*0209230bSgjelinek 	struct zone_rctlvaltab rctl;
5891*0209230bSgjelinek 	int i;
5892*0209230bSgjelinek 	int err;
5893cf8f45c7Sdstaff 
5894*0209230bSgjelinek 	for (i = 0; aliases[i].shortname != NULL; i++)
5895*0209230bSgjelinek 		if (strcmp(name, aliases[i].shortname) == 0)
5896*0209230bSgjelinek 			break;
5897cf8f45c7Sdstaff 
5898*0209230bSgjelinek 	if (aliases[i].shortname == NULL)
5899*0209230bSgjelinek 		return (Z_NO_PROPERTY_ID);
5900cf8f45c7Sdstaff 
5901*0209230bSgjelinek 	if ((err = operation_prep(handle)) != Z_OK)
5902*0209230bSgjelinek 		return (err);
5903cf8f45c7Sdstaff 
5904*0209230bSgjelinek 	cur = handle->zone_dh_cur;
5905*0209230bSgjelinek 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
5906*0209230bSgjelinek 		if (xmlStrcmp(cur->name, DTD_ELEM_RCTL) != 0)
5907*0209230bSgjelinek 			continue;
5908*0209230bSgjelinek 		if ((fetchprop(cur, DTD_ATTR_NAME, savedname,
5909*0209230bSgjelinek 		    sizeof (savedname)) == Z_OK) &&
5910*0209230bSgjelinek 		    (strcmp(savedname, aliases[i].realname) == 0)) {
5911cf8f45c7Sdstaff 
5912*0209230bSgjelinek 			/*
5913*0209230bSgjelinek 			 * If we already saw one of these, we can't have an
5914*0209230bSgjelinek 			 * alias since we just found another.
5915*0209230bSgjelinek 			 */
5916*0209230bSgjelinek 			if (found)
5917*0209230bSgjelinek 				return (Z_ALIAS_DISALLOW);
5918*0209230bSgjelinek 			found = B_TRUE;
5919*0209230bSgjelinek 
5920*0209230bSgjelinek 			for (val = cur->xmlChildrenNode; val != NULL;
5921*0209230bSgjelinek 			    val = val->next) {
5922*0209230bSgjelinek 				/*
5923*0209230bSgjelinek 				 * If we already have one value, we can't have
5924*0209230bSgjelinek 				 * an alias since we just found another.
5925*0209230bSgjelinek 				 */
5926*0209230bSgjelinek 				if (found_val)
5927*0209230bSgjelinek 					return (Z_ALIAS_DISALLOW);
5928*0209230bSgjelinek 				found_val = B_TRUE;
5929*0209230bSgjelinek 
5930*0209230bSgjelinek 				if ((fetchprop(val, DTD_ATTR_PRIV,
5931*0209230bSgjelinek 				    rctl.zone_rctlval_priv,
5932*0209230bSgjelinek 				    sizeof (rctl.zone_rctlval_priv)) != Z_OK))
5933*0209230bSgjelinek 					break;
5934*0209230bSgjelinek 				if ((fetchprop(val, DTD_ATTR_LIMIT,
5935*0209230bSgjelinek 				    rctl.zone_rctlval_limit,
5936*0209230bSgjelinek 				    sizeof (rctl.zone_rctlval_limit)) != Z_OK))
5937*0209230bSgjelinek 					break;
5938*0209230bSgjelinek 				if ((fetchprop(val, DTD_ATTR_ACTION,
5939*0209230bSgjelinek 				    rctl.zone_rctlval_action,
5940*0209230bSgjelinek 				    sizeof (rctl.zone_rctlval_action)) != Z_OK))
5941*0209230bSgjelinek 					break;
5942*0209230bSgjelinek 			}
5943*0209230bSgjelinek 
5944*0209230bSgjelinek 			/* check priv and action match the expected vals */
5945*0209230bSgjelinek 			if (strcmp(rctl.zone_rctlval_priv,
5946*0209230bSgjelinek 			    aliases[i].priv) != 0 ||
5947*0209230bSgjelinek 			    strcmp(rctl.zone_rctlval_action,
5948*0209230bSgjelinek 			    aliases[i].action) != 0)
5949*0209230bSgjelinek 				return (Z_ALIAS_DISALLOW);
5950*0209230bSgjelinek 		}
5951cf8f45c7Sdstaff 	}
5952cf8f45c7Sdstaff 
5953*0209230bSgjelinek 	if (found) {
5954*0209230bSgjelinek 		*rval = strtoull(rctl.zone_rctlval_limit, NULL, 10);
5955*0209230bSgjelinek 		return (Z_OK);
5956cf8f45c7Sdstaff 	}
5957cf8f45c7Sdstaff 
5958*0209230bSgjelinek 	return (Z_NO_ENTRY);
5959cf8f45c7Sdstaff }
5960cf8f45c7Sdstaff 
5961*0209230bSgjelinek int
5962*0209230bSgjelinek zonecfg_rm_aliased_rctl(zone_dochandle_t handle, char *name)
5963cf8f45c7Sdstaff {
5964*0209230bSgjelinek 	int i;
5965*0209230bSgjelinek 	uint64_t val;
5966*0209230bSgjelinek 	struct zone_rctltab rctltab;
5967cf8f45c7Sdstaff 
5968cf8f45c7Sdstaff 	/*
5969*0209230bSgjelinek 	 * First check that we have a valid aliased rctl to remove.
5970*0209230bSgjelinek 	 * This will catch an rctl entry with non-standard values or
5971*0209230bSgjelinek 	 * multiple rctl values for this name.  We need to ignore those
5972*0209230bSgjelinek 	 * rctl entries.
5973cf8f45c7Sdstaff 	 */
5974*0209230bSgjelinek 	if (zonecfg_get_aliased_rctl(handle, name, &val) != Z_OK)
5975*0209230bSgjelinek 		return (Z_OK);
5976*0209230bSgjelinek 
5977*0209230bSgjelinek 	for (i = 0; aliases[i].shortname != NULL; i++)
5978*0209230bSgjelinek 		if (strcmp(name, aliases[i].shortname) == 0)
5979*0209230bSgjelinek 			break;
5980*0209230bSgjelinek 
5981*0209230bSgjelinek 	if (aliases[i].shortname == NULL)
5982*0209230bSgjelinek 		return (Z_NO_RESOURCE_ID);
5983*0209230bSgjelinek 
5984*0209230bSgjelinek 	(void) strlcpy(rctltab.zone_rctl_name, aliases[i].realname,
5985*0209230bSgjelinek 	    sizeof (rctltab.zone_rctl_name));
5986*0209230bSgjelinek 
5987*0209230bSgjelinek 	return (zonecfg_delete_rctl(handle, &rctltab));
5988*0209230bSgjelinek }
5989*0209230bSgjelinek 
5990*0209230bSgjelinek boolean_t
5991*0209230bSgjelinek zonecfg_aliased_rctl_ok(zone_dochandle_t handle, char *name)
5992*0209230bSgjelinek {
5993*0209230bSgjelinek 	uint64_t tmp_val;
5994*0209230bSgjelinek 
5995*0209230bSgjelinek 	switch (zonecfg_get_aliased_rctl(handle, name, &tmp_val)) {
5996*0209230bSgjelinek 	case Z_OK:
5997*0209230bSgjelinek 		/*FALLTHRU*/
5998*0209230bSgjelinek 	case Z_NO_ENTRY:
5999*0209230bSgjelinek 		return (B_TRUE);
6000*0209230bSgjelinek 	default:
6001*0209230bSgjelinek 		return (B_FALSE);
6002*0209230bSgjelinek 	}
6003cf8f45c7Sdstaff }
6004cf8f45c7Sdstaff 
6005*0209230bSgjelinek int
6006*0209230bSgjelinek zonecfg_set_aliased_rctl(zone_dochandle_t handle, char *name, uint64_t val)
6007cf8f45c7Sdstaff {
6008*0209230bSgjelinek 	int i;
6009*0209230bSgjelinek 	int err;
6010*0209230bSgjelinek 	struct zone_rctltab rctltab;
6011*0209230bSgjelinek 	struct zone_rctlvaltab *rctlvaltab;
6012*0209230bSgjelinek 	char buf[128];
6013cf8f45c7Sdstaff 
6014*0209230bSgjelinek 	if (!zonecfg_aliased_rctl_ok(handle, name))
6015*0209230bSgjelinek 		return (Z_ALIAS_DISALLOW);
6016cf8f45c7Sdstaff 
6017*0209230bSgjelinek 	for (i = 0; aliases[i].shortname != NULL; i++)
6018*0209230bSgjelinek 		if (strcmp(name, aliases[i].shortname) == 0)
6019*0209230bSgjelinek 			break;
6020cf8f45c7Sdstaff 
6021*0209230bSgjelinek 	if (aliases[i].shortname == NULL)
6022*0209230bSgjelinek 		return (Z_NO_RESOURCE_ID);
6023cf8f45c7Sdstaff 
6024*0209230bSgjelinek 	/* remove any pre-existing definition for this rctl */
6025*0209230bSgjelinek 	(void) zonecfg_rm_aliased_rctl(handle, name);
6026cf8f45c7Sdstaff 
6027*0209230bSgjelinek 	(void) strlcpy(rctltab.zone_rctl_name, aliases[i].realname,
6028*0209230bSgjelinek 	    sizeof (rctltab.zone_rctl_name));
6029cf8f45c7Sdstaff 
6030*0209230bSgjelinek 	rctltab.zone_rctl_valptr = NULL;
6031cf8f45c7Sdstaff 
6032*0209230bSgjelinek 	if ((rctlvaltab = calloc(1, sizeof (struct zone_rctlvaltab))) == NULL)
6033*0209230bSgjelinek 		return (Z_NOMEM);
6034cf8f45c7Sdstaff 
6035*0209230bSgjelinek 	(void) snprintf(buf, sizeof (buf), "%llu", (long long)val);
6036cf8f45c7Sdstaff 
6037*0209230bSgjelinek 	(void) strlcpy(rctlvaltab->zone_rctlval_priv, aliases[i].priv,
6038*0209230bSgjelinek 	    sizeof (rctlvaltab->zone_rctlval_priv));
6039*0209230bSgjelinek 	(void) strlcpy(rctlvaltab->zone_rctlval_limit, buf,
6040*0209230bSgjelinek 	    sizeof (rctlvaltab->zone_rctlval_limit));
6041*0209230bSgjelinek 	(void) strlcpy(rctlvaltab->zone_rctlval_action, aliases[i].action,
6042*0209230bSgjelinek 	    sizeof (rctlvaltab->zone_rctlval_action));
6043cf8f45c7Sdstaff 
6044*0209230bSgjelinek 	rctlvaltab->zone_rctlval_next = NULL;
6045cf8f45c7Sdstaff 
6046*0209230bSgjelinek 	if ((err = zonecfg_add_rctl_value(&rctltab, rctlvaltab)) != Z_OK)
6047*0209230bSgjelinek 		return (err);
6048cf8f45c7Sdstaff 
6049*0209230bSgjelinek 	return (zonecfg_add_rctl(handle, &rctltab));
6050cf8f45c7Sdstaff }
6051cf8f45c7Sdstaff 
6052*0209230bSgjelinek static int
6053*0209230bSgjelinek delete_tmp_pool(zone_dochandle_t handle)
6054cf8f45c7Sdstaff {
6055*0209230bSgjelinek 	int err;
6056*0209230bSgjelinek 	xmlNodePtr cur = handle->zone_dh_cur;
6057cf8f45c7Sdstaff 
6058*0209230bSgjelinek 	if ((err = operation_prep(handle)) != Z_OK)
6059*0209230bSgjelinek 		return (err);
6060cf8f45c7Sdstaff 
6061*0209230bSgjelinek 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
6062*0209230bSgjelinek 		if (xmlStrcmp(cur->name, DTD_ELEM_TMPPOOL) == 0) {
6063*0209230bSgjelinek 			xmlUnlinkNode(cur);
6064*0209230bSgjelinek 			xmlFreeNode(cur);
6065*0209230bSgjelinek 			return (Z_OK);
6066*0209230bSgjelinek 		}
6067*0209230bSgjelinek 	}
6068cf8f45c7Sdstaff 
6069*0209230bSgjelinek 	return (Z_NO_RESOURCE_ID);
6070*0209230bSgjelinek }
6071cf8f45c7Sdstaff 
6072*0209230bSgjelinek static int
6073*0209230bSgjelinek modify_tmp_pool(zone_dochandle_t handle, char *pool_importance)
6074*0209230bSgjelinek {
6075*0209230bSgjelinek 	int err;
6076*0209230bSgjelinek 	xmlNodePtr cur = handle->zone_dh_cur;
6077*0209230bSgjelinek 	xmlNodePtr newnode;
6078cf8f45c7Sdstaff 
6079*0209230bSgjelinek 	err = delete_tmp_pool(handle);
6080*0209230bSgjelinek 	if (err != Z_OK && err != Z_NO_RESOURCE_ID)
6081*0209230bSgjelinek 		return (err);
6082*0209230bSgjelinek 
6083*0209230bSgjelinek 	if (*pool_importance != '\0') {
6084*0209230bSgjelinek 		if ((err = operation_prep(handle)) != Z_OK)
6085*0209230bSgjelinek 			return (err);
6086*0209230bSgjelinek 
6087*0209230bSgjelinek 		newnode = xmlNewTextChild(cur, NULL, DTD_ELEM_TMPPOOL, NULL);
6088*0209230bSgjelinek 		if ((err = newprop(newnode, DTD_ATTR_IMPORTANCE,
6089*0209230bSgjelinek 		    pool_importance)) != Z_OK)
6090*0209230bSgjelinek 			return (err);
6091*0209230bSgjelinek 	}
6092*0209230bSgjelinek 
6093*0209230bSgjelinek 	return (Z_OK);
6094cf8f45c7Sdstaff }
6095cf8f45c7Sdstaff 
6096fa9e4066Sahrens static int
6097*0209230bSgjelinek add_pset_core(zone_dochandle_t handle, struct zone_psettab *tabptr)
6098fa9e4066Sahrens {
6099fa9e4066Sahrens 	xmlNodePtr newnode, cur = handle->zone_dh_cur;
6100fa9e4066Sahrens 	int err;
6101fa9e4066Sahrens 
6102*0209230bSgjelinek 	newnode = xmlNewTextChild(cur, NULL, DTD_ELEM_PSET, NULL);
6103*0209230bSgjelinek 	if ((err = newprop(newnode, DTD_ATTR_NCPU_MIN,
6104*0209230bSgjelinek 	    tabptr->zone_ncpu_min)) != Z_OK)
6105*0209230bSgjelinek 		return (err);
6106*0209230bSgjelinek 	if ((err = newprop(newnode, DTD_ATTR_NCPU_MAX,
6107*0209230bSgjelinek 	    tabptr->zone_ncpu_max)) != Z_OK)
6108*0209230bSgjelinek 		return (err);
6109*0209230bSgjelinek 
6110*0209230bSgjelinek 	if ((err = modify_tmp_pool(handle, tabptr->zone_importance)) != Z_OK)
6111fa9e4066Sahrens 		return (err);
6112*0209230bSgjelinek 
6113fa9e4066Sahrens 	return (Z_OK);
6114fa9e4066Sahrens }
6115fa9e4066Sahrens 
6116fa9e4066Sahrens int
6117*0209230bSgjelinek zonecfg_add_pset(zone_dochandle_t handle, struct zone_psettab *tabptr)
6118fa9e4066Sahrens {
6119fa9e4066Sahrens 	int err;
6120fa9e4066Sahrens 
6121fa9e4066Sahrens 	if (tabptr == NULL)
6122fa9e4066Sahrens 		return (Z_INVAL);
6123fa9e4066Sahrens 
6124fa9e4066Sahrens 	if ((err = operation_prep(handle)) != Z_OK)
6125fa9e4066Sahrens 		return (err);
6126fa9e4066Sahrens 
6127*0209230bSgjelinek 	if ((err = add_pset_core(handle, tabptr)) != Z_OK)
6128fa9e4066Sahrens 		return (err);
6129fa9e4066Sahrens 
6130fa9e4066Sahrens 	return (Z_OK);
6131fa9e4066Sahrens }
6132fa9e4066Sahrens 
6133*0209230bSgjelinek int
6134*0209230bSgjelinek zonecfg_delete_pset(zone_dochandle_t handle)
6135fa9e4066Sahrens {
6136*0209230bSgjelinek 	int err;
6137*0209230bSgjelinek 	int res = Z_NO_RESOURCE_ID;
6138fa9e4066Sahrens 	xmlNodePtr cur = handle->zone_dh_cur;
6139fa9e4066Sahrens 
6140*0209230bSgjelinek 	if ((err = operation_prep(handle)) != Z_OK)
6141*0209230bSgjelinek 		return (err);
6142fa9e4066Sahrens 
6143*0209230bSgjelinek 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
6144*0209230bSgjelinek 		if (xmlStrcmp(cur->name, DTD_ELEM_PSET) == 0) {
6145fa9e4066Sahrens 			xmlUnlinkNode(cur);
6146fa9e4066Sahrens 			xmlFreeNode(cur);
6147*0209230bSgjelinek 			res = Z_OK;
6148*0209230bSgjelinek 			break;
6149fa9e4066Sahrens 		}
6150fa9e4066Sahrens 	}
6151*0209230bSgjelinek 
6152*0209230bSgjelinek 	/*
6153*0209230bSgjelinek 	 * Once we have msets, we should check that a mset
6154*0209230bSgjelinek 	 * do not exist before we delete the tmp_pool data.
6155*0209230bSgjelinek 	 */
6156*0209230bSgjelinek 	err = delete_tmp_pool(handle);
6157*0209230bSgjelinek 	if (err != Z_OK && err != Z_NO_RESOURCE_ID)
6158*0209230bSgjelinek 		return (err);
6159*0209230bSgjelinek 
6160*0209230bSgjelinek 	return (res);
6161fa9e4066Sahrens }
6162fa9e4066Sahrens 
6163fa9e4066Sahrens int
6164*0209230bSgjelinek zonecfg_modify_pset(zone_dochandle_t handle, struct zone_psettab *tabptr)
6165fa9e4066Sahrens {
6166fa9e4066Sahrens 	int err;
6167fa9e4066Sahrens 
6168fa9e4066Sahrens 	if (tabptr == NULL)
6169fa9e4066Sahrens 		return (Z_INVAL);
6170fa9e4066Sahrens 
6171*0209230bSgjelinek 	if ((err = zonecfg_delete_pset(handle)) != Z_OK)
6172fa9e4066Sahrens 		return (err);
6173fa9e4066Sahrens 
6174*0209230bSgjelinek 	if ((err = add_pset_core(handle, tabptr)) != Z_OK)
6175fa9e4066Sahrens 		return (err);
6176fa9e4066Sahrens 
6177fa9e4066Sahrens 	return (Z_OK);
6178fa9e4066Sahrens }
6179fa9e4066Sahrens 
6180fa9e4066Sahrens int
6181*0209230bSgjelinek zonecfg_lookup_pset(zone_dochandle_t handle, struct zone_psettab *tabptr)
6182fa9e4066Sahrens {
6183*0209230bSgjelinek 	xmlNodePtr cur;
6184fa9e4066Sahrens 	int err;
6185*0209230bSgjelinek 	int res = Z_NO_ENTRY;
6186fa9e4066Sahrens 
6187*0209230bSgjelinek 	if (tabptr == NULL)
6188fa9e4066Sahrens 		return (Z_INVAL);
6189fa9e4066Sahrens 
6190fa9e4066Sahrens 	if ((err = operation_prep(handle)) != Z_OK)
6191fa9e4066Sahrens 		return (err);
6192fa9e4066Sahrens 
6193*0209230bSgjelinek 	/* this is an optional component */
6194*0209230bSgjelinek 	tabptr->zone_importance[0] = '\0';
6195*0209230bSgjelinek 
6196*0209230bSgjelinek 	cur = handle->zone_dh_cur;
6197*0209230bSgjelinek 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
6198*0209230bSgjelinek 		if (xmlStrcmp(cur->name, DTD_ELEM_PSET) == 0) {
6199*0209230bSgjelinek 			if ((err = fetchprop(cur, DTD_ATTR_NCPU_MIN,
6200*0209230bSgjelinek 			    tabptr->zone_ncpu_min,
6201*0209230bSgjelinek 			    sizeof (tabptr->zone_ncpu_min))) != Z_OK) {
6202*0209230bSgjelinek 				handle->zone_dh_cur = handle->zone_dh_top;
6203*0209230bSgjelinek 				return (err);
6204*0209230bSgjelinek 			}
6205*0209230bSgjelinek 
6206*0209230bSgjelinek 			if ((err = fetchprop(cur, DTD_ATTR_NCPU_MAX,
6207*0209230bSgjelinek 			    tabptr->zone_ncpu_max,
6208*0209230bSgjelinek 			    sizeof (tabptr->zone_ncpu_max))) != Z_OK) {
6209*0209230bSgjelinek 				handle->zone_dh_cur = handle->zone_dh_top;
6210*0209230bSgjelinek 				return (err);
6211*0209230bSgjelinek 			}
6212*0209230bSgjelinek 
6213*0209230bSgjelinek 			res = Z_OK;
6214*0209230bSgjelinek 
6215*0209230bSgjelinek 		} else if (xmlStrcmp(cur->name, DTD_ELEM_TMPPOOL) == 0) {
6216*0209230bSgjelinek 			if ((err = fetchprop(cur, DTD_ATTR_IMPORTANCE,
6217*0209230bSgjelinek 			    tabptr->zone_importance,
6218*0209230bSgjelinek 			    sizeof (tabptr->zone_importance))) != Z_OK) {
6219*0209230bSgjelinek 				handle->zone_dh_cur = handle->zone_dh_top;
6220*0209230bSgjelinek 				return (err);
6221*0209230bSgjelinek 			}
6222*0209230bSgjelinek 		}
6223*0209230bSgjelinek 	}
6224*0209230bSgjelinek 
6225*0209230bSgjelinek 	return (res);
6226*0209230bSgjelinek }
6227*0209230bSgjelinek 
6228*0209230bSgjelinek int
6229*0209230bSgjelinek zonecfg_getpsetent(zone_dochandle_t handle, struct zone_psettab *tabptr)
6230*0209230bSgjelinek {
6231*0209230bSgjelinek 	int err;
6232*0209230bSgjelinek 
6233*0209230bSgjelinek 	if ((err = zonecfg_setent(handle)) != Z_OK)
6234fa9e4066Sahrens 		return (err);
6235fa9e4066Sahrens 
6236*0209230bSgjelinek 	err = zonecfg_lookup_pset(handle, tabptr);
6237*0209230bSgjelinek 
6238*0209230bSgjelinek 	(void) zonecfg_endent(handle);
6239*0209230bSgjelinek 
6240*0209230bSgjelinek 	return (err);
6241*0209230bSgjelinek }
6242*0209230bSgjelinek 
6243*0209230bSgjelinek static int
6244*0209230bSgjelinek add_mcap(zone_dochandle_t handle, struct zone_mcaptab *tabptr)
6245*0209230bSgjelinek {
6246*0209230bSgjelinek 	xmlNodePtr newnode, cur = handle->zone_dh_cur;
6247*0209230bSgjelinek 	int err;
6248*0209230bSgjelinek 
6249*0209230bSgjelinek 	newnode = xmlNewTextChild(cur, NULL, DTD_ELEM_MCAP, NULL);
6250*0209230bSgjelinek 	if ((err = newprop(newnode, DTD_ATTR_PHYSCAP, tabptr->zone_physmem_cap))
6251*0209230bSgjelinek 	    != Z_OK)
6252fa9e4066Sahrens 		return (err);
6253fa9e4066Sahrens 
6254fa9e4066Sahrens 	return (Z_OK);
6255fa9e4066Sahrens }
6256fa9e4066Sahrens 
6257fa9e4066Sahrens int
6258*0209230bSgjelinek zonecfg_delete_mcap(zone_dochandle_t handle)
6259fa9e4066Sahrens {
6260fa9e4066Sahrens 	int err;
6261*0209230bSgjelinek 	xmlNodePtr cur = handle->zone_dh_cur;
6262fa9e4066Sahrens 
6263fa9e4066Sahrens 	if ((err = operation_prep(handle)) != Z_OK)
6264fa9e4066Sahrens 		return (err);
6265fa9e4066Sahrens 
6266fa9e4066Sahrens 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
6267*0209230bSgjelinek 		if (xmlStrcmp(cur->name, DTD_ELEM_MCAP) != 0)
6268fa9e4066Sahrens 			continue;
6269*0209230bSgjelinek 
6270*0209230bSgjelinek 		xmlUnlinkNode(cur);
6271*0209230bSgjelinek 		xmlFreeNode(cur);
6272*0209230bSgjelinek 		return (Z_OK);
6273fa9e4066Sahrens 	}
6274*0209230bSgjelinek 	return (Z_NO_RESOURCE_ID);
6275*0209230bSgjelinek }
6276fa9e4066Sahrens 
6277*0209230bSgjelinek int
6278*0209230bSgjelinek zonecfg_modify_mcap(zone_dochandle_t handle, struct zone_mcaptab *tabptr)
6279*0209230bSgjelinek {
6280*0209230bSgjelinek 	int err;
6281fa9e4066Sahrens 
6282*0209230bSgjelinek 	if (tabptr == NULL)
6283*0209230bSgjelinek 		return (Z_INVAL);
6284*0209230bSgjelinek 
6285*0209230bSgjelinek 	err = zonecfg_delete_mcap(handle);
6286*0209230bSgjelinek 	/* it is ok if there is no mcap entry */
6287*0209230bSgjelinek 	if (err != Z_OK && err != Z_NO_RESOURCE_ID)
6288*0209230bSgjelinek 		return (err);
6289*0209230bSgjelinek 
6290*0209230bSgjelinek 	if ((err = add_mcap(handle, tabptr)) != Z_OK)
6291fa9e4066Sahrens 		return (err);
6292fa9e4066Sahrens 
6293fa9e4066Sahrens 	return (Z_OK);
6294fa9e4066Sahrens }
6295fa9e4066Sahrens 
6296fa9e4066Sahrens int
6297*0209230bSgjelinek zonecfg_lookup_mcap(zone_dochandle_t handle, struct zone_mcaptab *tabptr)
6298fa9e4066Sahrens {
6299*0209230bSgjelinek 	xmlNodePtr cur;
6300*0209230bSgjelinek 	int err;
6301*0209230bSgjelinek 
6302*0209230bSgjelinek 	if (tabptr == NULL)
6303*0209230bSgjelinek 		return (Z_INVAL);
6304*0209230bSgjelinek 
6305*0209230bSgjelinek 	if ((err = operation_prep(handle)) != Z_OK)
6306*0209230bSgjelinek 		return (err);
6307*0209230bSgjelinek 
6308*0209230bSgjelinek 	cur = handle->zone_dh_cur;
6309*0209230bSgjelinek 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
6310*0209230bSgjelinek 		if (xmlStrcmp(cur->name, DTD_ELEM_MCAP) != 0)
6311*0209230bSgjelinek 			continue;
6312*0209230bSgjelinek 		if ((err = fetchprop(cur, DTD_ATTR_PHYSCAP,
6313*0209230bSgjelinek 		    tabptr->zone_physmem_cap,
6314*0209230bSgjelinek 		    sizeof (tabptr->zone_physmem_cap))) != Z_OK) {
6315*0209230bSgjelinek 			handle->zone_dh_cur = handle->zone_dh_top;
6316*0209230bSgjelinek 			return (err);
6317*0209230bSgjelinek 		}
6318*0209230bSgjelinek 
6319*0209230bSgjelinek 		return (Z_OK);
6320*0209230bSgjelinek 	}
6321*0209230bSgjelinek 
6322*0209230bSgjelinek 	return (Z_NO_ENTRY);
6323fa9e4066Sahrens }
6324fa9e4066Sahrens 
6325*0209230bSgjelinek static int
6326*0209230bSgjelinek getmcapent_core(zone_dochandle_t handle, struct zone_mcaptab *tabptr)
6327fa9e4066Sahrens {
6328fa9e4066Sahrens 	xmlNodePtr cur;
6329fa9e4066Sahrens 	int err;
6330fa9e4066Sahrens 
6331fa9e4066Sahrens 	if (handle == NULL)
6332fa9e4066Sahrens 		return (Z_INVAL);
6333fa9e4066Sahrens 
6334fa9e4066Sahrens 	if ((cur = handle->zone_dh_cur) == NULL)
6335fa9e4066Sahrens 		return (Z_NO_ENTRY);
6336fa9e4066Sahrens 
6337fa9e4066Sahrens 	for (; cur != NULL; cur = cur->next)
6338*0209230bSgjelinek 		if (xmlStrcmp(cur->name, DTD_ELEM_MCAP) == 0)
6339fa9e4066Sahrens 			break;
6340fa9e4066Sahrens 	if (cur == NULL) {
6341fa9e4066Sahrens 		handle->zone_dh_cur = handle->zone_dh_top;
6342fa9e4066Sahrens 		return (Z_NO_ENTRY);
6343fa9e4066Sahrens 	}
6344fa9e4066Sahrens 
6345*0209230bSgjelinek 	if ((err = fetchprop(cur, DTD_ATTR_PHYSCAP, tabptr->zone_physmem_cap,
6346*0209230bSgjelinek 	    sizeof (tabptr->zone_physmem_cap))) != Z_OK) {
6347fa9e4066Sahrens 		handle->zone_dh_cur = handle->zone_dh_top;
6348fa9e4066Sahrens 		return (err);
6349fa9e4066Sahrens 	}
6350fa9e4066Sahrens 
6351fa9e4066Sahrens 	handle->zone_dh_cur = cur->next;
6352fa9e4066Sahrens 	return (Z_OK);
6353fa9e4066Sahrens }
6354fa9e4066Sahrens 
6355fa9e4066Sahrens int
6356*0209230bSgjelinek zonecfg_getmcapent(zone_dochandle_t handle, struct zone_mcaptab *tabptr)
6357fa9e4066Sahrens {
6358*0209230bSgjelinek 	int err;
6359*0209230bSgjelinek 
6360*0209230bSgjelinek 	if ((err = zonecfg_setent(handle)) != Z_OK)
6361*0209230bSgjelinek 		return (err);
6362*0209230bSgjelinek 
6363*0209230bSgjelinek 	err = getmcapent_core(handle, tabptr);
6364*0209230bSgjelinek 
6365*0209230bSgjelinek 	(void) zonecfg_endent(handle);
6366*0209230bSgjelinek 
6367*0209230bSgjelinek 	return (err);
6368fa9e4066Sahrens }
6369ee519a1fSgjelinek 
6370ee519a1fSgjelinek int
6371ee519a1fSgjelinek zonecfg_setpkgent(zone_dochandle_t handle)
6372ee519a1fSgjelinek {
6373ee519a1fSgjelinek 	return (zonecfg_setent(handle));
6374ee519a1fSgjelinek }
6375ee519a1fSgjelinek 
6376ee519a1fSgjelinek int
6377ee519a1fSgjelinek zonecfg_getpkgent(zone_dochandle_t handle, struct zone_pkgtab *tabptr)
6378ee519a1fSgjelinek {
6379ee519a1fSgjelinek 	xmlNodePtr cur;
6380ee519a1fSgjelinek 	int err;
6381ee519a1fSgjelinek 
6382ee519a1fSgjelinek 	if (handle == NULL)
6383ee519a1fSgjelinek 		return (Z_INVAL);
6384ee519a1fSgjelinek 
6385ee519a1fSgjelinek 	if ((cur = handle->zone_dh_cur) == NULL)
6386ee519a1fSgjelinek 		return (Z_NO_ENTRY);
6387ee519a1fSgjelinek 
6388ee519a1fSgjelinek 	for (; cur != NULL; cur = cur->next)
6389ee519a1fSgjelinek 		if (!xmlStrcmp(cur->name, DTD_ELEM_PACKAGE))
6390ee519a1fSgjelinek 			break;
6391ee519a1fSgjelinek 	if (cur == NULL) {
6392ee519a1fSgjelinek 		handle->zone_dh_cur = handle->zone_dh_top;
6393ee519a1fSgjelinek 		return (Z_NO_ENTRY);
6394ee519a1fSgjelinek 	}
6395ee519a1fSgjelinek 
6396ee519a1fSgjelinek 	if ((err = fetchprop(cur, DTD_ATTR_NAME, tabptr->zone_pkg_name,
6397ee519a1fSgjelinek 	    sizeof (tabptr->zone_pkg_name))) != Z_OK) {
6398ee519a1fSgjelinek 		handle->zone_dh_cur = handle->zone_dh_top;
6399ee519a1fSgjelinek 		return (err);
6400ee519a1fSgjelinek 	}
6401ee519a1fSgjelinek 
6402ee519a1fSgjelinek 	if ((err = fetchprop(cur, DTD_ATTR_VERSION, tabptr->zone_pkg_version,
6403ee519a1fSgjelinek 	    sizeof (tabptr->zone_pkg_version))) != Z_OK) {
6404ee519a1fSgjelinek 		handle->zone_dh_cur = handle->zone_dh_top;
6405ee519a1fSgjelinek 		return (err);
6406ee519a1fSgjelinek 	}
6407ee519a1fSgjelinek 
6408ee519a1fSgjelinek 	handle->zone_dh_cur = cur->next;
6409ee519a1fSgjelinek 	return (Z_OK);
6410ee519a1fSgjelinek }
6411ee519a1fSgjelinek 
6412ee519a1fSgjelinek int
6413ee519a1fSgjelinek zonecfg_endpkgent(zone_dochandle_t handle)
6414ee519a1fSgjelinek {
6415ee519a1fSgjelinek 	return (zonecfg_endent(handle));
6416ee519a1fSgjelinek }
6417ee519a1fSgjelinek 
6418ee519a1fSgjelinek int
6419ee519a1fSgjelinek zonecfg_setpatchent(zone_dochandle_t handle)
6420ee519a1fSgjelinek {
6421ee519a1fSgjelinek 	return (zonecfg_setent(handle));
6422ee519a1fSgjelinek }
6423ee519a1fSgjelinek 
6424ee519a1fSgjelinek int
6425ee519a1fSgjelinek zonecfg_getpatchent(zone_dochandle_t handle, struct zone_patchtab *tabptr)
6426ee519a1fSgjelinek {
6427ee519a1fSgjelinek 	xmlNodePtr cur;
6428ee519a1fSgjelinek 	int err;
6429ee519a1fSgjelinek 
6430ee519a1fSgjelinek 	if (handle == NULL)
6431ee519a1fSgjelinek 		return (Z_INVAL);
6432ee519a1fSgjelinek 
6433ee519a1fSgjelinek 	if ((cur = handle->zone_dh_cur) == NULL)
6434ee519a1fSgjelinek 		return (Z_NO_ENTRY);
6435ee519a1fSgjelinek 
6436ee519a1fSgjelinek 	for (; cur != NULL; cur = cur->next)
6437ee519a1fSgjelinek 		if (!xmlStrcmp(cur->name, DTD_ELEM_PATCH))
6438ee519a1fSgjelinek 			break;
6439ee519a1fSgjelinek 	if (cur == NULL) {
6440ee519a1fSgjelinek 		handle->zone_dh_cur = handle->zone_dh_top;
6441ee519a1fSgjelinek 		return (Z_NO_ENTRY);
6442ee519a1fSgjelinek 	}
6443ee519a1fSgjelinek 
6444ee519a1fSgjelinek 	if ((err = fetchprop(cur, DTD_ATTR_ID, tabptr->zone_patch_id,
6445ee519a1fSgjelinek 	    sizeof (tabptr->zone_patch_id))) != Z_OK) {
6446ee519a1fSgjelinek 		handle->zone_dh_cur = handle->zone_dh_top;
6447ee519a1fSgjelinek 		return (err);
6448ee519a1fSgjelinek 	}
6449ee519a1fSgjelinek 
6450ee519a1fSgjelinek 	handle->zone_dh_cur = cur->next;
6451ee519a1fSgjelinek 	return (Z_OK);
6452ee519a1fSgjelinek }
6453ee519a1fSgjelinek 
6454ee519a1fSgjelinek int
6455ee519a1fSgjelinek zonecfg_endpatchent(zone_dochandle_t handle)
6456ee519a1fSgjelinek {
6457ee519a1fSgjelinek 	return (zonecfg_endent(handle));
6458ee519a1fSgjelinek }
6459ee519a1fSgjelinek 
6460ee519a1fSgjelinek int
6461ee519a1fSgjelinek zonecfg_setdevperment(zone_dochandle_t handle)
6462ee519a1fSgjelinek {
6463ee519a1fSgjelinek 	return (zonecfg_setent(handle));
6464ee519a1fSgjelinek }
6465ee519a1fSgjelinek 
6466ee519a1fSgjelinek int
6467ee519a1fSgjelinek zonecfg_getdevperment(zone_dochandle_t handle, struct zone_devpermtab *tabptr)
6468ee519a1fSgjelinek {
6469ee519a1fSgjelinek 	xmlNodePtr cur;
6470ee519a1fSgjelinek 	int err;
6471ee519a1fSgjelinek 	char buf[128];
6472ee519a1fSgjelinek 
6473ee519a1fSgjelinek 	tabptr->zone_devperm_acl = NULL;
6474ee519a1fSgjelinek 
6475ee519a1fSgjelinek 	if (handle == NULL)
6476ee519a1fSgjelinek 		return (Z_INVAL);
6477ee519a1fSgjelinek 
6478ee519a1fSgjelinek 	if ((cur = handle->zone_dh_cur) == NULL)
6479ee519a1fSgjelinek 		return (Z_NO_ENTRY);
6480ee519a1fSgjelinek 
6481ee519a1fSgjelinek 	for (; cur != NULL; cur = cur->next)
6482ee519a1fSgjelinek 		if (!xmlStrcmp(cur->name, DTD_ELEM_DEV_PERM))
6483ee519a1fSgjelinek 			break;
6484ee519a1fSgjelinek 	if (cur == NULL) {
6485ee519a1fSgjelinek 		handle->zone_dh_cur = handle->zone_dh_top;
6486ee519a1fSgjelinek 		return (Z_NO_ENTRY);
6487ee519a1fSgjelinek 	}
6488ee519a1fSgjelinek 
6489ee519a1fSgjelinek 	if ((err = fetchprop(cur, DTD_ATTR_NAME, tabptr->zone_devperm_name,
6490ee519a1fSgjelinek 	    sizeof (tabptr->zone_devperm_name))) != Z_OK) {
6491ee519a1fSgjelinek 		handle->zone_dh_cur = handle->zone_dh_top;
6492ee519a1fSgjelinek 		return (err);
6493ee519a1fSgjelinek 	}
6494ee519a1fSgjelinek 
6495ee519a1fSgjelinek 	if ((err = fetchprop(cur, DTD_ATTR_UID, buf, sizeof (buf))) != Z_OK) {
6496ee519a1fSgjelinek 		handle->zone_dh_cur = handle->zone_dh_top;
6497ee519a1fSgjelinek 		return (err);
6498ee519a1fSgjelinek 	}
6499ee519a1fSgjelinek 	tabptr->zone_devperm_uid = (uid_t)atol(buf);
6500ee519a1fSgjelinek 
6501ee519a1fSgjelinek 	if ((err = fetchprop(cur, DTD_ATTR_GID, buf, sizeof (buf))) != Z_OK) {
6502ee519a1fSgjelinek 		handle->zone_dh_cur = handle->zone_dh_top;
6503ee519a1fSgjelinek 		return (err);
6504ee519a1fSgjelinek 	}
6505ee519a1fSgjelinek 	tabptr->zone_devperm_gid = (gid_t)atol(buf);
6506ee519a1fSgjelinek 
6507ee519a1fSgjelinek 	if ((err = fetchprop(cur, DTD_ATTR_MODE, buf, sizeof (buf))) != Z_OK) {
6508ee519a1fSgjelinek 		handle->zone_dh_cur = handle->zone_dh_top;
6509ee519a1fSgjelinek 		return (err);
6510ee519a1fSgjelinek 	}
6511ee519a1fSgjelinek 	tabptr->zone_devperm_mode = (mode_t)strtol(buf, (char **)NULL, 8);
6512ee519a1fSgjelinek 
6513ee519a1fSgjelinek 	if ((err = fetch_alloc_prop(cur, DTD_ATTR_ACL,
6514ee519a1fSgjelinek 	    &(tabptr->zone_devperm_acl))) != Z_OK) {
6515ee519a1fSgjelinek 		handle->zone_dh_cur = handle->zone_dh_top;
6516ee519a1fSgjelinek 		return (err);
6517ee519a1fSgjelinek 	}
6518ee519a1fSgjelinek 
6519ee519a1fSgjelinek 	handle->zone_dh_cur = cur->next;
6520ee519a1fSgjelinek 	return (Z_OK);
6521ee519a1fSgjelinek }
6522ee519a1fSgjelinek 
6523ee519a1fSgjelinek int
6524ee519a1fSgjelinek zonecfg_enddevperment(zone_dochandle_t handle)
6525ee519a1fSgjelinek {
6526ee519a1fSgjelinek 	return (zonecfg_endent(handle));
6527ee519a1fSgjelinek }
6528ee519a1fSgjelinek 
6529ee519a1fSgjelinek /*
6530ee519a1fSgjelinek  * Process a list of pkgs from an entry in the contents file, adding each pkg
6531ee519a1fSgjelinek  * name to the list of pkgs.
6532ee519a1fSgjelinek  *
6533ee519a1fSgjelinek  * It is possible for the pkg name to be preceeded by a special character
6534ee519a1fSgjelinek  * which indicates some bookkeeping information for pkging.  Check if the
6535ee519a1fSgjelinek  * first char is not an Alpha char.  If so, skip over it.
6536ee519a1fSgjelinek  */
6537ee519a1fSgjelinek static int
6538ee519a1fSgjelinek add_pkg_list(char *lastp, char ***plist, int *pcnt)
6539ee519a1fSgjelinek {
6540ee519a1fSgjelinek 	char	*p;
6541ee519a1fSgjelinek 	int	pkg_cnt = *pcnt;
6542ee519a1fSgjelinek 	char	**pkgs = *plist;
6543ee519a1fSgjelinek 	int	res = Z_OK;
6544ee519a1fSgjelinek 
6545ee519a1fSgjelinek 	while ((p = strtok_r(NULL, " ", &lastp)) != NULL) {
6546ee519a1fSgjelinek 		char	**tmpp;
6547ee519a1fSgjelinek 		int	i;
6548ee519a1fSgjelinek 
6549ee519a1fSgjelinek 		/* skip over any special pkg bookkeeping char */
6550ee519a1fSgjelinek 		if (!isalpha(*p))
6551ee519a1fSgjelinek 			p++;
6552ee519a1fSgjelinek 
6553ee519a1fSgjelinek 		/* Check if the pkg is already in the list */
6554ee519a1fSgjelinek 		for (i = 0; i < pkg_cnt; i++) {
6555ee519a1fSgjelinek 			if (strcmp(p, pkgs[i]) == 0)
6556ee519a1fSgjelinek 				break;
6557ee519a1fSgjelinek 		}
6558ee519a1fSgjelinek 
6559ee519a1fSgjelinek 		if (i < pkg_cnt)
6560ee519a1fSgjelinek 			continue;
6561ee519a1fSgjelinek 
6562ee519a1fSgjelinek 		/* The pkg is not in the list; add it. */
6563ee519a1fSgjelinek 		if ((tmpp = (char **)realloc(pkgs,
6564ee519a1fSgjelinek 		    sizeof (char *) * (pkg_cnt + 1))) == NULL) {
6565ee519a1fSgjelinek 			res = Z_NOMEM;
6566ee519a1fSgjelinek 			break;
6567ee519a1fSgjelinek 		}
6568ee519a1fSgjelinek 		pkgs = tmpp;
6569ee519a1fSgjelinek 
6570ee519a1fSgjelinek 		if ((pkgs[pkg_cnt] = strdup(p)) == NULL) {
6571ee519a1fSgjelinek 			res = Z_NOMEM;
6572ee519a1fSgjelinek 			break;
6573ee519a1fSgjelinek 		}
6574ee519a1fSgjelinek 		pkg_cnt++;
6575ee519a1fSgjelinek 	}
6576ee519a1fSgjelinek 
6577ee519a1fSgjelinek 	*plist = pkgs;
6578ee519a1fSgjelinek 	*pcnt = pkg_cnt;
6579ee519a1fSgjelinek 
6580ee519a1fSgjelinek 	return (res);
6581ee519a1fSgjelinek }
6582ee519a1fSgjelinek 
6583ee519a1fSgjelinek /*
6584ee519a1fSgjelinek  * Process an entry from the contents file (type "directory") and if the
6585ee519a1fSgjelinek  * directory path is in the list of paths, add the associated list of pkgs
6586ee519a1fSgjelinek  * to the pkg list.  The input parameter "entry" will be broken up by
6587ee519a1fSgjelinek  * the parser within this function so its value will be modified when this
6588ee519a1fSgjelinek  * function exits.
6589ee519a1fSgjelinek  *
6590ee519a1fSgjelinek  * The entries we are looking for will look something like:
6591ee519a1fSgjelinek  *	/usr d none 0755 root sys SUNWctpls SUNWidnl SUNWlibCf ....
6592ee519a1fSgjelinek  */
6593ee519a1fSgjelinek static int
6594ee519a1fSgjelinek get_path_pkgs(char *entry, char **paths, int cnt, char ***pkgs, int *pkg_cnt)
6595ee519a1fSgjelinek {
6596ee519a1fSgjelinek 	char	*f1;
6597ee519a1fSgjelinek 	char	*f2;
6598ee519a1fSgjelinek 	char	*lastp;
6599ee519a1fSgjelinek 	int	i;
6600ee519a1fSgjelinek 	int	res = Z_OK;
6601ee519a1fSgjelinek 
6602ee519a1fSgjelinek 	if ((f1 = strtok_r(entry, " ", &lastp)) == NULL ||
6603ee519a1fSgjelinek 	    (f2 = strtok_r(NULL, " ", &lastp)) == NULL || strcmp(f2, "d") != 0)
6604ee519a1fSgjelinek 		return (Z_OK);
6605ee519a1fSgjelinek 
6606ee519a1fSgjelinek 	/* Check if this directory entry is in the list of paths. */
6607ee519a1fSgjelinek 	for (i = 0; i < cnt; i++) {
6608ee519a1fSgjelinek 		if (fnmatch(paths[i], f1, FNM_PATHNAME) == 0) {
6609ee519a1fSgjelinek 			/*
6610ee519a1fSgjelinek 			 * We do want the pkgs for this path.  First, skip
6611ee519a1fSgjelinek 			 * over the next 4 fields in the entry so that we call
6612ee519a1fSgjelinek 			 * add_pkg_list starting with the pkg names.
6613ee519a1fSgjelinek 			 */
6614ee519a1fSgjelinek 			int j;
661507b574eeSgjelinek 			char	*nlp;
6616ee519a1fSgjelinek 
6617ee519a1fSgjelinek 			for (j = 0; j < 4 &&
6618ffbafc53Scomay 			    strtok_r(NULL, " ", &lastp) != NULL; j++)
6619ffbafc53Scomay 				;
6620ee519a1fSgjelinek 			/*
6621ee519a1fSgjelinek 			 * If there are < 4 fields this entry is corrupt,
6622ee519a1fSgjelinek 			 * just skip it.
6623ee519a1fSgjelinek 			 */
6624ee519a1fSgjelinek 			if (j < 4)
6625ee519a1fSgjelinek 				return (Z_OK);
6626ee519a1fSgjelinek 
662707b574eeSgjelinek 			/* strip newline from the line */
662807b574eeSgjelinek 			nlp = (lastp + strlen(lastp) - 1);
662907b574eeSgjelinek 			if (*nlp == '\n')
663007b574eeSgjelinek 				*nlp = '\0';
663107b574eeSgjelinek 
6632ee519a1fSgjelinek 			res = add_pkg_list(lastp, pkgs, pkg_cnt);
6633ee519a1fSgjelinek 			break;
6634ee519a1fSgjelinek 		}
6635ee519a1fSgjelinek 	}
6636ee519a1fSgjelinek 
6637ee519a1fSgjelinek 	return (res);
6638ee519a1fSgjelinek }
6639ee519a1fSgjelinek 
6640ee519a1fSgjelinek /*
6641ee519a1fSgjelinek  * Read an entry from a pkginfo or contents file.  Some of these lines can
6642ee519a1fSgjelinek  * either be arbitrarily long or be continued by a backslash at the end of
6643ee519a1fSgjelinek  * the line.  This function coalesces lines that are longer than the read
6644ee519a1fSgjelinek  * buffer, and lines that are continued, into one buffer which is returned.
6645ee519a1fSgjelinek  * The caller must free this memory.  NULL is returned when we hit EOF or
6646ee519a1fSgjelinek  * if we run out of memory (errno is set to ENOMEM).
6647ee519a1fSgjelinek  */
6648ee519a1fSgjelinek static char *
6649ee519a1fSgjelinek read_pkg_data(FILE *fp)
6650ee519a1fSgjelinek {
6651ee519a1fSgjelinek 	char *start;
6652ee519a1fSgjelinek 	char *inp;
6653ee519a1fSgjelinek 	char *p;
6654ee519a1fSgjelinek 	int char_cnt = 0;
6655ee519a1fSgjelinek 
6656ee519a1fSgjelinek 	errno = 0;
6657ee519a1fSgjelinek 	if ((start = (char *)malloc(PKGINFO_RD_LEN)) == NULL) {
6658ee519a1fSgjelinek 		errno = ENOMEM;
6659ee519a1fSgjelinek 		return (NULL);
6660ee519a1fSgjelinek 	}
6661ee519a1fSgjelinek 
6662ee519a1fSgjelinek 	inp = start;
6663ee519a1fSgjelinek 	while ((p = fgets(inp, PKGINFO_RD_LEN, fp)) != NULL) {
6664ee519a1fSgjelinek 		int len;
6665ee519a1fSgjelinek 
6666ee519a1fSgjelinek 		len = strlen(inp);
6667ee519a1fSgjelinek 		if (inp[len - 1] == '\n' &&
6668ee519a1fSgjelinek 		    (len == 1 || inp[len - 2] != '\\')) {
6669ee519a1fSgjelinek 			char_cnt = len;
6670ee519a1fSgjelinek 			break;
6671ee519a1fSgjelinek 		}
6672ee519a1fSgjelinek 
6673ee519a1fSgjelinek 		if (inp[len - 2] == '\\')
6674ee519a1fSgjelinek 			char_cnt += len - 2;
6675ee519a1fSgjelinek 		else
6676ee519a1fSgjelinek 			char_cnt += PKGINFO_RD_LEN - 1;
6677ee519a1fSgjelinek 
6678ee519a1fSgjelinek 		if ((p = realloc(start, char_cnt + PKGINFO_RD_LEN)) == NULL) {
6679ee519a1fSgjelinek 			errno = ENOMEM;
6680ee519a1fSgjelinek 			break;
6681ee519a1fSgjelinek 		}
6682ee519a1fSgjelinek 
6683ee519a1fSgjelinek 		start = p;
6684ee519a1fSgjelinek 		inp = start + char_cnt;
6685ee519a1fSgjelinek 	}
6686ee519a1fSgjelinek 
6687ee519a1fSgjelinek 	if (errno == ENOMEM || (p == NULL && char_cnt == 0)) {
6688ee519a1fSgjelinek 		free(start);
6689ee519a1fSgjelinek 		start = NULL;
6690ee519a1fSgjelinek 	}
6691ee519a1fSgjelinek 
6692ee519a1fSgjelinek 	return (start);
6693ee519a1fSgjelinek }
6694ee519a1fSgjelinek 
6695ee519a1fSgjelinek static void
6696ee519a1fSgjelinek free_ipd_pkgs(char **pkgs, int cnt)
6697ee519a1fSgjelinek {
6698ee519a1fSgjelinek 	int i;
6699ee519a1fSgjelinek 
6700ee519a1fSgjelinek 	for (i = 0; i < cnt; i++)
6701ee519a1fSgjelinek 		free(pkgs[i]);
6702ee519a1fSgjelinek 	free(pkgs);
6703ee519a1fSgjelinek }
6704ee519a1fSgjelinek 
6705ee519a1fSgjelinek /*
6706ee519a1fSgjelinek  * Get the list of inherited-pkg-dirs (ipd) for the zone and then get the
6707ee519a1fSgjelinek  * list of pkgs that deliver into those dirs.
6708ee519a1fSgjelinek  */
6709ee519a1fSgjelinek static int
6710ee519a1fSgjelinek get_ipd_pkgs(zone_dochandle_t handle, char ***pkg_list, int *cnt)
6711ee519a1fSgjelinek {
6712ee519a1fSgjelinek 	int	res;
6713ee519a1fSgjelinek 	struct zone_fstab fstab;
6714ee519a1fSgjelinek 	int	ipd_cnt = 0;
6715ee519a1fSgjelinek 	char	**ipds = NULL;
6716ee519a1fSgjelinek 	int	pkg_cnt = 0;
6717ee519a1fSgjelinek 	char	**pkgs = NULL;
6718ee519a1fSgjelinek 	int	i;
6719ee519a1fSgjelinek 
6720ee519a1fSgjelinek 	if ((res = zonecfg_setipdent(handle)) != Z_OK)
6721ee519a1fSgjelinek 		return (res);
6722ee519a1fSgjelinek 
6723ee519a1fSgjelinek 	while (zonecfg_getipdent(handle, &fstab) == Z_OK) {
6724ee519a1fSgjelinek 		char	**p;
6725ee519a1fSgjelinek 		int	len;
6726ee519a1fSgjelinek 
6727ee519a1fSgjelinek 		if ((p = (char **)realloc(ipds,
6728ee519a1fSgjelinek 		    sizeof (char *) * (ipd_cnt + 2))) == NULL) {
6729ee519a1fSgjelinek 			res = Z_NOMEM;
6730ee519a1fSgjelinek 			break;
6731ee519a1fSgjelinek 		}
6732ee519a1fSgjelinek 		ipds = p;
6733ee519a1fSgjelinek 
6734ee519a1fSgjelinek 		if ((ipds[ipd_cnt] = strdup(fstab.zone_fs_dir)) == NULL) {
6735ee519a1fSgjelinek 			res = Z_NOMEM;
6736ee519a1fSgjelinek 			break;
6737ee519a1fSgjelinek 		}
6738ee519a1fSgjelinek 		ipd_cnt++;
6739ee519a1fSgjelinek 
6740ee519a1fSgjelinek 		len = strlen(fstab.zone_fs_dir) + 3;
6741ee519a1fSgjelinek 		if ((ipds[ipd_cnt] = malloc(len)) == NULL) {
6742ee519a1fSgjelinek 			res = Z_NOMEM;
6743ee519a1fSgjelinek 			break;
6744ee519a1fSgjelinek 		}
6745ee519a1fSgjelinek 
6746ee519a1fSgjelinek 		(void) snprintf(ipds[ipd_cnt], len, "%s/*", fstab.zone_fs_dir);
6747ee519a1fSgjelinek 		ipd_cnt++;
6748ee519a1fSgjelinek 	}
6749ee519a1fSgjelinek 
6750ee519a1fSgjelinek 	(void) zonecfg_endipdent(handle);
6751ee519a1fSgjelinek 
6752ee519a1fSgjelinek 	if (res != Z_OK) {
6753ee519a1fSgjelinek 		for (i = 0; i < ipd_cnt; i++)
6754ee519a1fSgjelinek 			free(ipds[i]);
6755ee519a1fSgjelinek 		free(ipds);
6756ee519a1fSgjelinek 		return (res);
6757ee519a1fSgjelinek 	}
6758ee519a1fSgjelinek 
6759ee519a1fSgjelinek 	/* We only have to process the contents file if we have ipds. */
6760ee519a1fSgjelinek 	if (ipd_cnt > 0) {
6761ee519a1fSgjelinek 		FILE	*fp;
6762ee519a1fSgjelinek 
6763ee519a1fSgjelinek 		if ((fp = fopen(CONTENTS_FILE, "r")) != NULL) {
6764ee519a1fSgjelinek 			char	*buf;
6765ee519a1fSgjelinek 
6766ee519a1fSgjelinek 			while ((buf = read_pkg_data(fp)) != NULL) {
6767ee519a1fSgjelinek 				res = get_path_pkgs(buf, ipds, ipd_cnt, &pkgs,
6768ee519a1fSgjelinek 				    &pkg_cnt);
6769ee519a1fSgjelinek 				free(buf);
6770ee519a1fSgjelinek 				if (res != Z_OK)
6771ee519a1fSgjelinek 					break;
6772ee519a1fSgjelinek 			}
6773ee519a1fSgjelinek 
6774ee519a1fSgjelinek 			(void) fclose(fp);
6775ee519a1fSgjelinek 		}
6776ee519a1fSgjelinek 	}
6777ee519a1fSgjelinek 
6778ee519a1fSgjelinek 	for (i = 0; i < ipd_cnt; i++)
6779ee519a1fSgjelinek 		free(ipds[i]);
6780ee519a1fSgjelinek 	free(ipds);
6781ee519a1fSgjelinek 
6782ee519a1fSgjelinek 	if (res != Z_OK) {
6783ee519a1fSgjelinek 		free_ipd_pkgs(pkgs, pkg_cnt);
6784ee519a1fSgjelinek 	} else {
6785ee519a1fSgjelinek 		*pkg_list = pkgs;
6786ee519a1fSgjelinek 		*cnt = pkg_cnt;
6787ee519a1fSgjelinek 	}
6788ee519a1fSgjelinek 
6789ee519a1fSgjelinek 	return (res);
6790ee519a1fSgjelinek }
6791ee519a1fSgjelinek 
6792ee519a1fSgjelinek /*
6793ee519a1fSgjelinek  * Return true if pkg_name is in the list of pkgs that deliver into an
6794ee519a1fSgjelinek  * inherited pkg directory for the zone.
6795ee519a1fSgjelinek  */
6796ee519a1fSgjelinek static boolean_t
6797ee519a1fSgjelinek dir_pkg(char *pkg_name, char **pkg_list, int cnt)
6798ee519a1fSgjelinek {
6799ee519a1fSgjelinek 	int i;
6800ee519a1fSgjelinek 
6801ee519a1fSgjelinek 	for (i = 0; i < cnt; i++) {
6802ee519a1fSgjelinek 		if (strcmp(pkg_name, pkg_list[i]) == 0)
6803ee519a1fSgjelinek 			return (B_TRUE);
6804ee519a1fSgjelinek 	}
6805ee519a1fSgjelinek 
6806ee519a1fSgjelinek 	return (B_FALSE);
6807ee519a1fSgjelinek }
6808ee519a1fSgjelinek 
6809ee519a1fSgjelinek /*
6810ee519a1fSgjelinek  * Start by adding the patch to the sw inventory on the handle.
6811ee519a1fSgjelinek  *
6812ee519a1fSgjelinek  * The info parameter will be the portion of the PATCH_INFO_ entry following
6813ee519a1fSgjelinek  * the '='.  For example:
6814ee519a1fSgjelinek  * Installed: Wed Dec  7 07:13:51 PST 2005 From: mum Obsoletes: 120777-03 \
6815ee519a1fSgjelinek  *	121087-02 119108-07 Requires: 119575-02 119255-06 Incompatibles:
6816ee519a1fSgjelinek  *
681707b574eeSgjelinek  * A backed out patch will have an info line of "backed out\n".  We should
681807b574eeSgjelinek  * skip these patches.
681907b574eeSgjelinek  *
6820ee519a1fSgjelinek  * We also want to add the Obsolete and Incompatible patches to the
6821ee519a1fSgjelinek  * sw inventory description of this patch.
6822ee519a1fSgjelinek  */
6823ee519a1fSgjelinek static int
6824ee519a1fSgjelinek add_patch(zone_dochandle_t handle, char *patch, char *info)
6825ee519a1fSgjelinek {
6826ee519a1fSgjelinek 	xmlNodePtr	node;
6827ee519a1fSgjelinek 	xmlNodePtr	cur;
6828ee519a1fSgjelinek 	int		err;
6829ee519a1fSgjelinek 	char		*p;
6830ee519a1fSgjelinek 	char		*lastp;
6831ee519a1fSgjelinek 	boolean_t	add_info = B_FALSE;
6832ee519a1fSgjelinek 	boolean_t	obsolete;
6833ee519a1fSgjelinek 
683407b574eeSgjelinek 	if (strcmp(info, "backed out\n") == 0)
683507b574eeSgjelinek 		return (Z_OK);
683607b574eeSgjelinek 
6837ee519a1fSgjelinek 	if ((err = operation_prep(handle)) != Z_OK)
6838ee519a1fSgjelinek 		return (err);
6839ee519a1fSgjelinek 
6840ee519a1fSgjelinek 	cur = handle->zone_dh_cur;
6841ee519a1fSgjelinek 	node = xmlNewTextChild(cur, NULL, DTD_ELEM_PATCH, NULL);
6842ee519a1fSgjelinek 	if ((err = newprop(node, DTD_ATTR_ID, patch)) != Z_OK)
6843ee519a1fSgjelinek 		return (err);
6844ee519a1fSgjelinek 
6845ee519a1fSgjelinek 	/*
6846ee519a1fSgjelinek 	 * Start with the first token.  This will probably be "Installed:".
6847ee519a1fSgjelinek 	 * If we can't tokenize this entry, just return.
6848ee519a1fSgjelinek 	 */
6849ee519a1fSgjelinek 	if ((p = strtok_r(info, " ", &lastp)) == NULL)
6850ee519a1fSgjelinek 		return (Z_OK);
6851ee519a1fSgjelinek 
6852ee519a1fSgjelinek 	do {
6853ee519a1fSgjelinek 		xmlNodePtr new_node;
6854ee519a1fSgjelinek 		char	*nlp;
6855ee519a1fSgjelinek 
6856ee519a1fSgjelinek 		if (strcmp(p, "Installed:") == 0 ||
6857ee519a1fSgjelinek 		    strcmp(p, "Requires:") == 0 ||
6858ee519a1fSgjelinek 		    strcmp(p, "From:") == 0) {
6859ee519a1fSgjelinek 			add_info = B_FALSE;
6860ee519a1fSgjelinek 			continue;
6861ee519a1fSgjelinek 		} else if (strcmp(p, "Obsoletes:") == 0) {
6862ee519a1fSgjelinek 			obsolete = B_TRUE;
6863ee519a1fSgjelinek 			add_info = B_TRUE;
6864ee519a1fSgjelinek 			continue;
6865ee519a1fSgjelinek 		} else if (strcmp(p, "Incompatibles:") == 0) {
6866ee519a1fSgjelinek 			obsolete = B_FALSE;
6867ee519a1fSgjelinek 			add_info = B_TRUE;
6868ee519a1fSgjelinek 			continue;
6869ee519a1fSgjelinek 		}
6870ee519a1fSgjelinek 
6871ee519a1fSgjelinek 		if (!add_info)
6872ee519a1fSgjelinek 			continue;
6873ee519a1fSgjelinek 
6874ee519a1fSgjelinek 		/* strip newline from last patch in the line */
6875ee519a1fSgjelinek 		nlp = (p + strlen(p) - 1);
6876ee519a1fSgjelinek 		if (*nlp == '\n')
6877ee519a1fSgjelinek 			*nlp = '\0';
6878ee519a1fSgjelinek 
6879ee519a1fSgjelinek 		if (obsolete)
6880ee519a1fSgjelinek 			new_node = xmlNewTextChild(node, NULL,
6881ee519a1fSgjelinek 			    DTD_ELEM_OBSOLETES, NULL);
6882ee519a1fSgjelinek 		else
6883ee519a1fSgjelinek 			new_node = xmlNewTextChild(node, NULL,
6884ee519a1fSgjelinek 			    DTD_ELEM_INCOMPATIBLE, NULL);
6885ee519a1fSgjelinek 
6886ee519a1fSgjelinek 		if ((err = newprop(new_node, DTD_ATTR_ID, p)) != Z_OK)
6887ee519a1fSgjelinek 			return (err);
6888ee519a1fSgjelinek 
6889ee519a1fSgjelinek 	} while ((p = strtok_r(NULL, " ", &lastp)) != NULL);
6890ee519a1fSgjelinek 
6891ee519a1fSgjelinek 	return (Z_OK);
6892ee519a1fSgjelinek }
6893ee519a1fSgjelinek 
6894ee519a1fSgjelinek static boolean_t
6895ee519a1fSgjelinek unique_patch(zone_dochandle_t handle, char *patch)
6896ee519a1fSgjelinek {
6897ee519a1fSgjelinek 	xmlNodePtr	cur;
6898ee519a1fSgjelinek 	char		id[MAXNAMELEN];
6899ee519a1fSgjelinek 
6900ee519a1fSgjelinek 	cur = xmlDocGetRootElement(handle->zone_dh_doc);
6901ee519a1fSgjelinek 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
6902ee519a1fSgjelinek 		if (xmlStrcmp(cur->name, DTD_ELEM_PATCH) == 0) {
6903ee519a1fSgjelinek 			if (fetchprop(cur, DTD_ATTR_ID, id, sizeof (id))
6904ee519a1fSgjelinek 			    != Z_OK)
6905ee519a1fSgjelinek 				continue;
6906ee519a1fSgjelinek 
6907ee519a1fSgjelinek 			if (strcmp(patch, id) == 0)
6908ee519a1fSgjelinek 				return (B_FALSE);
6909ee519a1fSgjelinek 		}
6910ee519a1fSgjelinek 	}
6911ee519a1fSgjelinek 
6912ee519a1fSgjelinek 	return (B_TRUE);
6913ee519a1fSgjelinek }
6914ee519a1fSgjelinek 
6915ee519a1fSgjelinek /*
6916ee519a1fSgjelinek  * Add the unique patches associated with this pkg to the sw inventory on the
6917ee519a1fSgjelinek  * handle.
6918ee519a1fSgjelinek  *
6919ee519a1fSgjelinek  * We are processing entries of the form:
6920ee519a1fSgjelinek  * PATCH_INFO_121454-02=Installed: Wed Dec  7 07:13:51 PST 2005 From: mum \
6921ee519a1fSgjelinek  *	Obsoletes: 120777-03 121087-02 119108-07 Requires: 119575-02 \
6922ee519a1fSgjelinek  *	119255-06 Incompatibles:
6923ee519a1fSgjelinek  *
6924ee519a1fSgjelinek  */
6925ee519a1fSgjelinek static int
6926ee519a1fSgjelinek add_patches(zone_dochandle_t handle, struct zone_pkginfo *infop)
6927ee519a1fSgjelinek {
6928ee519a1fSgjelinek 	int i;
6929ee519a1fSgjelinek 	int res = Z_OK;
6930ee519a1fSgjelinek 
6931ee519a1fSgjelinek 	for (i = 0; i < infop->zpi_patch_cnt; i++) {
6932ee519a1fSgjelinek 		char *p, *ep;
6933ee519a1fSgjelinek 
6934ee519a1fSgjelinek 		if (strlen(infop->zpi_patchinfo[i]) < (sizeof (PATCHINFO) - 1))
6935ee519a1fSgjelinek 			continue;
6936ee519a1fSgjelinek 
6937ee519a1fSgjelinek 		/* Skip over "PATCH_INFO_" to get the patch id. */
6938ee519a1fSgjelinek 		p = infop->zpi_patchinfo[i] + sizeof (PATCHINFO) - 1;
6939ee519a1fSgjelinek 		if ((ep = strchr(p, '=')) == NULL)
6940ee519a1fSgjelinek 			continue;
6941ee519a1fSgjelinek 
6942ee519a1fSgjelinek 		*ep = '\0';
6943ee519a1fSgjelinek 		if (unique_patch(handle, p))
694407b574eeSgjelinek 			if ((res = add_patch(handle, p, ep + 1)) != Z_OK)
694507b574eeSgjelinek 				break;
6946ee519a1fSgjelinek 	}
6947ee519a1fSgjelinek 
6948ee519a1fSgjelinek 	return (res);
6949ee519a1fSgjelinek }
6950ee519a1fSgjelinek 
6951ee519a1fSgjelinek /*
6952ee519a1fSgjelinek  * Add the pkg to the sw inventory on the handle.
6953ee519a1fSgjelinek  */
6954ee519a1fSgjelinek static int
6955ee519a1fSgjelinek add_pkg(zone_dochandle_t handle, char *name, char *version)
6956ee519a1fSgjelinek {
6957ee519a1fSgjelinek 	xmlNodePtr newnode;
6958ee519a1fSgjelinek 	xmlNodePtr cur;
6959ee519a1fSgjelinek 	int err;
6960ee519a1fSgjelinek 
6961ee519a1fSgjelinek 	if ((err = operation_prep(handle)) != Z_OK)
6962ee519a1fSgjelinek 		return (err);
6963ee519a1fSgjelinek 
6964ee519a1fSgjelinek 	cur = handle->zone_dh_cur;
6965ee519a1fSgjelinek 	newnode = xmlNewTextChild(cur, NULL, DTD_ELEM_PACKAGE, NULL);
6966ee519a1fSgjelinek 	if ((err = newprop(newnode, DTD_ATTR_NAME, name)) != Z_OK)
6967ee519a1fSgjelinek 		return (err);
6968ee519a1fSgjelinek 	if ((err = newprop(newnode, DTD_ATTR_VERSION, version)) != Z_OK)
6969ee519a1fSgjelinek 		return (err);
6970ee519a1fSgjelinek 	return (Z_OK);
6971ee519a1fSgjelinek }
6972ee519a1fSgjelinek 
6973ee519a1fSgjelinek static void
6974ee519a1fSgjelinek free_pkginfo(struct zone_pkginfo *infop)
6975ee519a1fSgjelinek {
6976ee519a1fSgjelinek 	free(infop->zpi_version);
6977ee519a1fSgjelinek 	if (infop->zpi_patch_cnt > 0) {
6978ee519a1fSgjelinek 		int i;
6979ee519a1fSgjelinek 
6980ee519a1fSgjelinek 		for (i = 0; i < infop->zpi_patch_cnt; i++)
6981ee519a1fSgjelinek 			free(infop->zpi_patchinfo[i]);
6982ee519a1fSgjelinek 		free(infop->zpi_patchinfo);
6983ee519a1fSgjelinek 	}
6984ee519a1fSgjelinek }
6985ee519a1fSgjelinek 
6986ee519a1fSgjelinek /*
6987ee519a1fSgjelinek  * Read the pkginfo file and populate the structure with the data we need
6988ee519a1fSgjelinek  * from this pkg for a sw inventory.
6989ee519a1fSgjelinek  */
6990ee519a1fSgjelinek static int
6991ee519a1fSgjelinek get_pkginfo(char *pkginfo, struct zone_pkginfo *infop)
6992ee519a1fSgjelinek {
6993ee519a1fSgjelinek 	FILE	*fp;
6994ee519a1fSgjelinek 	char	*buf;
6995ee519a1fSgjelinek 	int	err = 0;
6996ee519a1fSgjelinek 
6997ee519a1fSgjelinek 	infop->zpi_all_zones = B_FALSE;
6998ee519a1fSgjelinek 	infop->zpi_this_zone = B_FALSE;
6999ee519a1fSgjelinek 	infop->zpi_version = NULL;
7000ee519a1fSgjelinek 	infop->zpi_patch_cnt = 0;
7001ee519a1fSgjelinek 	infop->zpi_patchinfo = NULL;
7002ee519a1fSgjelinek 
7003ee519a1fSgjelinek 	if ((fp = fopen(pkginfo, "r")) == NULL)
7004ee519a1fSgjelinek 		return (errno);
7005ee519a1fSgjelinek 
7006ee519a1fSgjelinek 	while ((buf = read_pkg_data(fp)) != NULL) {
7007ee519a1fSgjelinek 		if (strncmp(buf, VERSION, sizeof (VERSION) - 1) == 0) {
7008ee519a1fSgjelinek 			int len;
7009ee519a1fSgjelinek 
7010ee519a1fSgjelinek 			if ((infop->zpi_version =
7011ee519a1fSgjelinek 			    strdup(buf + sizeof (VERSION) - 1)) == NULL) {
7012ee519a1fSgjelinek 				err = ENOMEM;
7013ee519a1fSgjelinek 				break;
7014ee519a1fSgjelinek 			}
7015ee519a1fSgjelinek 
7016ee519a1fSgjelinek 			/* remove trailing newline */
7017ee519a1fSgjelinek 			len = strlen(infop->zpi_version);
7018ee519a1fSgjelinek 			*(infop->zpi_version + len - 1) = 0;
7019ee519a1fSgjelinek 
702045916cd2Sjpk 		} else if (strcmp(buf, SUNW_PKG_ALL_ZONES) == 0) {
7021ee519a1fSgjelinek 			infop->zpi_all_zones = B_TRUE;
7022ee519a1fSgjelinek 
702345916cd2Sjpk 		} else if (strcmp(buf, SUNW_PKG_THIS_ZONE) == 0) {
7024ee519a1fSgjelinek 			infop->zpi_this_zone = B_TRUE;
7025ee519a1fSgjelinek 
7026ee519a1fSgjelinek 		} else if (strncmp(buf, PATCHINFO, sizeof (PATCHINFO) - 1)
7027ee519a1fSgjelinek 		    == 0) {
7028ee519a1fSgjelinek 			char **p;
7029ee519a1fSgjelinek 
7030ee519a1fSgjelinek 			if ((p = (char **)realloc(infop->zpi_patchinfo,
7031ee519a1fSgjelinek 			    sizeof (char *) * (infop->zpi_patch_cnt + 1)))
7032ee519a1fSgjelinek 			    == NULL) {
7033ee519a1fSgjelinek 				err = ENOMEM;
7034ee519a1fSgjelinek 				break;
7035ee519a1fSgjelinek 			}
7036ee519a1fSgjelinek 			infop->zpi_patchinfo = p;
7037ee519a1fSgjelinek 
7038ee519a1fSgjelinek 			if ((infop->zpi_patchinfo[infop->zpi_patch_cnt] =
7039ee519a1fSgjelinek 			    strdup(buf)) == NULL) {
7040ee519a1fSgjelinek 				err = ENOMEM;
7041ee519a1fSgjelinek 				break;
7042ee519a1fSgjelinek 			}
7043ee519a1fSgjelinek 			infop->zpi_patch_cnt++;
7044ee519a1fSgjelinek 		}
7045ee519a1fSgjelinek 
7046ee519a1fSgjelinek 		free(buf);
7047ee519a1fSgjelinek 	}
7048ee519a1fSgjelinek 
7049ee519a1fSgjelinek 	free(buf);
7050ee519a1fSgjelinek 
7051ee519a1fSgjelinek 	if (errno == ENOMEM) {
7052ee519a1fSgjelinek 		err = ENOMEM;
7053ee519a1fSgjelinek 		/* Clean up anything we did manage to allocate. */
7054ee519a1fSgjelinek 		free_pkginfo(infop);
7055ee519a1fSgjelinek 	}
7056ee519a1fSgjelinek 
7057ee519a1fSgjelinek 	(void) fclose(fp);
7058ee519a1fSgjelinek 
7059ee519a1fSgjelinek 	return (err);
7060ee519a1fSgjelinek }
7061ee519a1fSgjelinek 
7062ee519a1fSgjelinek /*
7063ee519a1fSgjelinek  * Take a software inventory of the global zone.  We need to get the set of
7064ee519a1fSgjelinek  * packages and patches that are on the global zone that the specified
7065ee519a1fSgjelinek  * non-global zone depends on.  The packages we need in the inventory are:
7066ee519a1fSgjelinek  *
7067ee519a1fSgjelinek  * - skip the package if SUNW_PKG_THISZONE is 'true'
7068ee519a1fSgjelinek  * otherwise,
7069ee519a1fSgjelinek  * - add the package if
7070ee519a1fSgjelinek  * a) SUNW_PKG_ALLZONES is 'true',
7071ee519a1fSgjelinek  * or
7072ee519a1fSgjelinek  * b) any file delivered by the package is in a file system that is inherited
7073ee519a1fSgjelinek  * from the global zone.
7074ee519a1fSgjelinek  * If the zone does not inherit any file systems (whole root)
7075ee519a1fSgjelinek  * then (b) will be skipped.
7076ee519a1fSgjelinek  *
7077ee519a1fSgjelinek  * For each of the packages that is being added to the inventory, we will also
7078ee519a1fSgjelinek  * add all of the associated, unique patches to the inventory.
7079ee519a1fSgjelinek  */
7080ee519a1fSgjelinek static int
7081ee519a1fSgjelinek zonecfg_sw_inventory(zone_dochandle_t handle)
7082ee519a1fSgjelinek {
7083ee519a1fSgjelinek 	char		pkginfo[MAXPATHLEN];
7084ee519a1fSgjelinek 	int		res;
7085ee519a1fSgjelinek 	struct dirent	*dp;
7086ee519a1fSgjelinek 	DIR		*dirp;
7087ee519a1fSgjelinek 	struct stat	buf;
7088ee519a1fSgjelinek 	struct zone_pkginfo	info;
7089ee519a1fSgjelinek 	int		pkg_cnt = 0;
7090ee519a1fSgjelinek 	char		**pkgs = NULL;
7091ee519a1fSgjelinek 
7092ee519a1fSgjelinek 	if ((res = get_ipd_pkgs(handle, &pkgs, &pkg_cnt)) != Z_OK)
7093ee519a1fSgjelinek 		return (res);
7094ee519a1fSgjelinek 
7095ee519a1fSgjelinek 	if ((dirp = opendir(PKG_PATH)) == NULL) {
7096ee519a1fSgjelinek 		free_ipd_pkgs(pkgs, pkg_cnt);
7097ee519a1fSgjelinek 		return (Z_OK);
7098ee519a1fSgjelinek 	}
7099ee519a1fSgjelinek 
7100ee519a1fSgjelinek 	while ((dp = readdir(dirp)) != (struct dirent *)0) {
7101ee519a1fSgjelinek 		if (strcmp(dp->d_name, ".") == 0 ||
7102ee519a1fSgjelinek 		    strcmp(dp->d_name, "..") == 0)
7103ee519a1fSgjelinek 			continue;
7104ee519a1fSgjelinek 
7105ee519a1fSgjelinek 		(void) snprintf(pkginfo, sizeof (pkginfo), "%s/%s/pkginfo",
7106ee519a1fSgjelinek 		    PKG_PATH, dp->d_name);
7107ee519a1fSgjelinek 
7108ee519a1fSgjelinek 		if (stat(pkginfo, &buf) == -1 || !S_ISREG(buf.st_mode))
7109ee519a1fSgjelinek 			continue;
7110ee519a1fSgjelinek 
7111ee519a1fSgjelinek 		if (get_pkginfo(pkginfo, &info) != 0) {
7112ee519a1fSgjelinek 			res = Z_NOMEM;
7113ee519a1fSgjelinek 			break;
7114ee519a1fSgjelinek 		}
7115ee519a1fSgjelinek 
7116ee519a1fSgjelinek 		if (!info.zpi_this_zone &&
7117ee519a1fSgjelinek 		    (info.zpi_all_zones ||
7118ee519a1fSgjelinek 		    dir_pkg(dp->d_name, pkgs, pkg_cnt))) {
7119ee519a1fSgjelinek 			if ((res = add_pkg(handle, dp->d_name,
7120ee519a1fSgjelinek 			    info.zpi_version)) == Z_OK) {
7121ee519a1fSgjelinek 				if (info.zpi_patch_cnt > 0)
7122ee519a1fSgjelinek 					res = add_patches(handle, &info);
7123ee519a1fSgjelinek 			}
7124ee519a1fSgjelinek 		}
7125ee519a1fSgjelinek 
7126ee519a1fSgjelinek 		free_pkginfo(&info);
7127ee519a1fSgjelinek 
7128ee519a1fSgjelinek 		if (res != Z_OK)
7129ee519a1fSgjelinek 			break;
7130ee519a1fSgjelinek 	}
7131ee519a1fSgjelinek 
7132ee519a1fSgjelinek 	(void) closedir(dirp);
7133ee519a1fSgjelinek 
7134ee519a1fSgjelinek 	free_ipd_pkgs(pkgs, pkg_cnt);
7135ee519a1fSgjelinek 
7136ee519a1fSgjelinek 	if (res == Z_OK)
7137ee519a1fSgjelinek 		handle->zone_dh_sw_inv = B_TRUE;
7138ee519a1fSgjelinek 
7139ee519a1fSgjelinek 	return (res);
7140ee519a1fSgjelinek }
7141ee519a1fSgjelinek 
7142ee519a1fSgjelinek /*
7143ee519a1fSgjelinek  * zonecfg_devwalk call-back function used during detach to generate the
7144ee519a1fSgjelinek  * dev info in the manifest.
7145ee519a1fSgjelinek  */
7146ee519a1fSgjelinek static int
7147ee519a1fSgjelinek get_detach_dev_entry(const char *name, uid_t uid, gid_t gid, mode_t mode,
7148ee519a1fSgjelinek     const char *acl, void *hdl)
7149ee519a1fSgjelinek {
7150ee519a1fSgjelinek 	zone_dochandle_t handle = (zone_dochandle_t)hdl;
7151ee519a1fSgjelinek 	xmlNodePtr newnode;
7152ee519a1fSgjelinek 	xmlNodePtr cur;
7153ee519a1fSgjelinek 	int err;
7154ee519a1fSgjelinek 	char buf[128];
7155ee519a1fSgjelinek 
7156ee519a1fSgjelinek 	if ((err = operation_prep(handle)) != Z_OK)
7157ee519a1fSgjelinek 		return (err);
7158ee519a1fSgjelinek 
7159ee519a1fSgjelinek 	cur = handle->zone_dh_cur;
7160ee519a1fSgjelinek 	newnode = xmlNewTextChild(cur, NULL, DTD_ELEM_DEV_PERM, NULL);
7161ee519a1fSgjelinek 	if ((err = newprop(newnode, DTD_ATTR_NAME, (char *)name)) != Z_OK)
7162ee519a1fSgjelinek 		return (err);
7163ee519a1fSgjelinek 	(void) snprintf(buf, sizeof (buf), "%lu", uid);
7164ee519a1fSgjelinek 	if ((err = newprop(newnode, DTD_ATTR_UID, buf)) != Z_OK)
7165ee519a1fSgjelinek 		return (err);
7166ee519a1fSgjelinek 	(void) snprintf(buf, sizeof (buf), "%lu", gid);
7167ee519a1fSgjelinek 	if ((err = newprop(newnode, DTD_ATTR_GID, buf)) != Z_OK)
7168ee519a1fSgjelinek 		return (err);
7169ee519a1fSgjelinek 	(void) snprintf(buf, sizeof (buf), "%o", mode);
7170ee519a1fSgjelinek 	if ((err = newprop(newnode, DTD_ATTR_MODE, buf)) != Z_OK)
7171ee519a1fSgjelinek 		return (err);
7172ee519a1fSgjelinek 	if ((err = newprop(newnode, DTD_ATTR_ACL, (char *)acl)) != Z_OK)
7173ee519a1fSgjelinek 		return (err);
7174ee519a1fSgjelinek 	return (Z_OK);
7175ee519a1fSgjelinek }
7176ee519a1fSgjelinek 
7177ee519a1fSgjelinek /*
7178ee519a1fSgjelinek  * Get the information required to support detaching a zone.  This is
7179ee519a1fSgjelinek  * called on the source system when detaching (the detaching parameter should
7180ee519a1fSgjelinek  * be set to true) and on the destination system before attaching (the
7181ee519a1fSgjelinek  * detaching parameter should be false).
7182ee519a1fSgjelinek  *
7183ee519a1fSgjelinek  * For native Solaris zones, the detach/attach process involves validating
7184ee519a1fSgjelinek  * that the software on the global zone can support the zone when we attach.
7185ee519a1fSgjelinek  * To do this we take a software inventory of the global zone.  We also
7186ee519a1fSgjelinek  * have to keep track of the device configuration so that we can properly
7187ee519a1fSgjelinek  * recreate it on the destination.
7188ee519a1fSgjelinek  */
7189ee519a1fSgjelinek int
7190ee519a1fSgjelinek zonecfg_get_detach_info(zone_dochandle_t handle, boolean_t detaching)
7191ee519a1fSgjelinek {
7192ee519a1fSgjelinek 	int		res;
7193ee519a1fSgjelinek 
7194ee519a1fSgjelinek 	if ((res = zonecfg_sw_inventory(handle)) != Z_OK)
7195ee519a1fSgjelinek 		return (res);
7196ee519a1fSgjelinek 
7197ee519a1fSgjelinek 	if (detaching)
7198ee519a1fSgjelinek 		res = zonecfg_devwalk(handle, get_detach_dev_entry, handle);
7199ee519a1fSgjelinek 
7200ee519a1fSgjelinek 	return (res);
7201ee519a1fSgjelinek }
7202