xref: /titanic_50/usr/src/lib/libzonecfg/common/libzonecfg.c (revision 555afedfc38adf0cc5fbc1de696dc811973eaaca)
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 <sys/stat.h>
377c478bd9Sstevel@tonic-gate #include <assert.h>
387c478bd9Sstevel@tonic-gate #include <libgen.h>
397c478bd9Sstevel@tonic-gate #include <libintl.h>
407c478bd9Sstevel@tonic-gate #include <alloca.h>
417c478bd9Sstevel@tonic-gate #include <ctype.h>
427c478bd9Sstevel@tonic-gate #include <sys/mntio.h>
437c478bd9Sstevel@tonic-gate #include <sys/mnttab.h>
44087719fdSdp #include <sys/types.h>
45cf8f45c7Sdstaff #include <sys/nvpair.h>
46ee519a1fSgjelinek #include <sys/acl.h>
47ee519a1fSgjelinek #include <ftw.h>
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate #include <arpa/inet.h>
507c478bd9Sstevel@tonic-gate #include <netdb.h>
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate #include <libxml/xmlmemory.h>
537c478bd9Sstevel@tonic-gate #include <libxml/parser.h>
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate #include <libdevinfo.h>
56108322fbScarlsonj #include <uuid/uuid.h>
577c478bd9Sstevel@tonic-gate 
58ee519a1fSgjelinek #include <dirent.h>
59ee519a1fSgjelinek 
607c478bd9Sstevel@tonic-gate #include <libzonecfg.h>
617c478bd9Sstevel@tonic-gate #include "zonecfg_impl.h"
627c478bd9Sstevel@tonic-gate 
63cf8f45c7Sdstaff 
647c478bd9Sstevel@tonic-gate #define	_PATH_TMPFILE	"/zonecfg.XXXXXX"
65cf8f45c7Sdstaff #define	ZONE_CB_RETRY_COUNT		10
66cf8f45c7Sdstaff #define	ZONE_EVENT_PING_SUBCLASS	"ping"
67cf8f45c7Sdstaff #define	ZONE_EVENT_PING_PUBLISHER	"solaris"
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate /* Hard-code the DTD element/attribute/entity names just once, here. */
707c478bd9Sstevel@tonic-gate #define	DTD_ELEM_ATTR		(const xmlChar *) "attr"
717c478bd9Sstevel@tonic-gate #define	DTD_ELEM_COMMENT	(const xmlChar *) "comment"
727c478bd9Sstevel@tonic-gate #define	DTD_ELEM_DEVICE		(const xmlChar *) "device"
737c478bd9Sstevel@tonic-gate #define	DTD_ELEM_FS		(const xmlChar *) "filesystem"
747c478bd9Sstevel@tonic-gate #define	DTD_ELEM_FSOPTION	(const xmlChar *) "fsoption"
757c478bd9Sstevel@tonic-gate #define	DTD_ELEM_IPD		(const xmlChar *) "inherited-pkg-dir"
767c478bd9Sstevel@tonic-gate #define	DTD_ELEM_NET		(const xmlChar *) "network"
777c478bd9Sstevel@tonic-gate #define	DTD_ELEM_RCTL		(const xmlChar *) "rctl"
787c478bd9Sstevel@tonic-gate #define	DTD_ELEM_RCTLVALUE	(const xmlChar *) "rctl-value"
797c478bd9Sstevel@tonic-gate #define	DTD_ELEM_ZONE		(const xmlChar *) "zone"
80fa9e4066Sahrens #define	DTD_ELEM_DATASET	(const xmlChar *) "dataset"
81ee519a1fSgjelinek #define	DTD_ELEM_PACKAGE	(const xmlChar *) "package"
82ee519a1fSgjelinek #define	DTD_ELEM_PATCH		(const xmlChar *) "patch"
83ee519a1fSgjelinek #define	DTD_ELEM_OBSOLETES	(const xmlChar *) "obsoletes"
84ee519a1fSgjelinek #define	DTD_ELEM_INCOMPATIBLE	(const xmlChar *) "incompatible"
85ee519a1fSgjelinek #define	DTD_ELEM_DEV_PERM	(const xmlChar *) "dev-perm"
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate #define	DTD_ATTR_ACTION		(const xmlChar *) "action"
887c478bd9Sstevel@tonic-gate #define	DTD_ATTR_ADDRESS	(const xmlChar *) "address"
897c478bd9Sstevel@tonic-gate #define	DTD_ATTR_AUTOBOOT	(const xmlChar *) "autoboot"
907c478bd9Sstevel@tonic-gate #define	DTD_ATTR_DIR		(const xmlChar *) "directory"
917c478bd9Sstevel@tonic-gate #define	DTD_ATTR_LIMIT		(const xmlChar *) "limit"
92ffbafc53Scomay #define	DTD_ATTR_LIMITPRIV	(const xmlChar *) "limitpriv"
933f2f09c1Sdp #define	DTD_ATTR_BOOTARGS	(const xmlChar *) "bootargs"
947c478bd9Sstevel@tonic-gate #define	DTD_ATTR_MATCH		(const xmlChar *) "match"
957c478bd9Sstevel@tonic-gate #define	DTD_ATTR_NAME		(const xmlChar *) "name"
967c478bd9Sstevel@tonic-gate #define	DTD_ATTR_PHYSICAL	(const xmlChar *) "physical"
977c478bd9Sstevel@tonic-gate #define	DTD_ATTR_POOL		(const xmlChar *) "pool"
987c478bd9Sstevel@tonic-gate #define	DTD_ATTR_PRIV		(const xmlChar *) "priv"
997c478bd9Sstevel@tonic-gate #define	DTD_ATTR_RAW		(const xmlChar *) "raw"
1007c478bd9Sstevel@tonic-gate #define	DTD_ATTR_SPECIAL	(const xmlChar *) "special"
1017c478bd9Sstevel@tonic-gate #define	DTD_ATTR_TYPE		(const xmlChar *) "type"
1027c478bd9Sstevel@tonic-gate #define	DTD_ATTR_VALUE		(const xmlChar *) "value"
1037c478bd9Sstevel@tonic-gate #define	DTD_ATTR_ZONEPATH	(const xmlChar *) "zonepath"
104ee519a1fSgjelinek #define	DTD_ATTR_VERSION	(const xmlChar *) "version"
105ee519a1fSgjelinek #define	DTD_ATTR_ID		(const xmlChar *) "id"
106ee519a1fSgjelinek #define	DTD_ATTR_UID		(const xmlChar *) "uid"
107ee519a1fSgjelinek #define	DTD_ATTR_GID		(const xmlChar *) "gid"
108ee519a1fSgjelinek #define	DTD_ATTR_MODE		(const xmlChar *) "mode"
109ee519a1fSgjelinek #define	DTD_ATTR_ACL		(const xmlChar *) "acl"
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate #define	DTD_ENTITY_BOOLEAN	"boolean"
1127c478bd9Sstevel@tonic-gate #define	DTD_ENTITY_DEVPATH	"devpath"
1137c478bd9Sstevel@tonic-gate #define	DTD_ENTITY_DRIVER	"driver"
1147c478bd9Sstevel@tonic-gate #define	DTD_ENTITY_DRVMIN	"drv_min"
1157c478bd9Sstevel@tonic-gate #define	DTD_ENTITY_FALSE	"false"
1167c478bd9Sstevel@tonic-gate #define	DTD_ENTITY_INT		"int"
1177c478bd9Sstevel@tonic-gate #define	DTD_ENTITY_STRING	"string"
1187c478bd9Sstevel@tonic-gate #define	DTD_ENTITY_TRUE		"true"
1197c478bd9Sstevel@tonic-gate #define	DTD_ENTITY_UINT		"uint"
1207c478bd9Sstevel@tonic-gate 
121a1be23daSdp #define	DTD_ENTITY_BOOL_LEN	6	/* "false" */
122a1be23daSdp 
123ee519a1fSgjelinek #define	DETACHED	"SUNWdetached.xml"
124ee519a1fSgjelinek #define	ATTACH_FORCED	"SUNWattached.xml"
125ee519a1fSgjelinek #define	PKG_PATH	"/var/sadm/pkg"
126ee519a1fSgjelinek #define	CONTENTS_FILE	"/var/sadm/install/contents"
12745916cd2Sjpk #define	SUNW_PKG_ALL_ZONES	"SUNW_PKG_ALLZONES=true\n"
12845916cd2Sjpk #define	SUNW_PKG_THIS_ZONE	"SUNW_PKG_THISZONE=true\n"
129ee519a1fSgjelinek #define	VERSION		"VERSION="
130ee519a1fSgjelinek #define	PATCHLIST	"PATCHLIST="
131ee519a1fSgjelinek #define	PATCHINFO	"PATCH_INFO_"
132ee519a1fSgjelinek #define	PKGINFO_RD_LEN	128
133ee519a1fSgjelinek 
1347c478bd9Sstevel@tonic-gate struct zone_dochandle {
1357c478bd9Sstevel@tonic-gate 	char		*zone_dh_rootdir;
1367c478bd9Sstevel@tonic-gate 	xmlDocPtr	zone_dh_doc;
1377c478bd9Sstevel@tonic-gate 	xmlNodePtr	zone_dh_cur;
1387c478bd9Sstevel@tonic-gate 	xmlNodePtr	zone_dh_top;
139087719fdSdp 	boolean_t	zone_dh_newzone;
140087719fdSdp 	boolean_t	zone_dh_snapshot;
141ee519a1fSgjelinek 	boolean_t	zone_dh_sw_inv;
142087719fdSdp 	char		zone_dh_delete_name[ZONENAME_MAX];
1437c478bd9Sstevel@tonic-gate };
1447c478bd9Sstevel@tonic-gate 
145cf8f45c7Sdstaff struct znotify {
146cf8f45c7Sdstaff 	void * zn_private;
147cf8f45c7Sdstaff 	evchan_t *zn_eventchan;
148cf8f45c7Sdstaff 	int (*zn_callback)(const  char *zonename, zoneid_t zid,
149cf8f45c7Sdstaff 	    const char *newstate, const char *oldstate, hrtime_t when, void *p);
150cf8f45c7Sdstaff 	pthread_mutex_t zn_mutex;
151cf8f45c7Sdstaff 	pthread_cond_t zn_cond;
152cf8f45c7Sdstaff 	pthread_mutex_t zn_bigmutex;
153cf8f45c7Sdstaff 	volatile enum {ZN_UNLOCKED, ZN_LOCKED, ZN_PING_INFLIGHT,
154cf8f45c7Sdstaff 	    ZN_PING_RECEIVED} zn_state;
155cf8f45c7Sdstaff 	char zn_subscriber_id[MAX_SUBID_LEN];
156cf8f45c7Sdstaff 	volatile boolean_t zn_failed;
157cf8f45c7Sdstaff 	int zn_failure_count;
158cf8f45c7Sdstaff };
159cf8f45c7Sdstaff 
160ee519a1fSgjelinek struct zone_pkginfo {
161ee519a1fSgjelinek 	boolean_t	zpi_all_zones;
162ee519a1fSgjelinek 	boolean_t	zpi_this_zone;
163ee519a1fSgjelinek 	int		zpi_patch_cnt;
164ee519a1fSgjelinek 	char		*zpi_version;
165ee519a1fSgjelinek 	char		**zpi_patchinfo;
166ee519a1fSgjelinek };
167ee519a1fSgjelinek 
168108322fbScarlsonj char *zonecfg_root = "";
169108322fbScarlsonj 
1707c478bd9Sstevel@tonic-gate /*
1717c478bd9Sstevel@tonic-gate  * For functions which return int, which is most of the functions herein,
1727c478bd9Sstevel@tonic-gate  * the return values should be from the Z_foo set defined in <libzonecfg.h>.
1737c478bd9Sstevel@tonic-gate  * In some instances, we take pains mapping some libc errno values to Z_foo
1747c478bd9Sstevel@tonic-gate  * values from this set.
1757c478bd9Sstevel@tonic-gate  */
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate /*
178108322fbScarlsonj  * Set the root (/) path for all zonecfg configuration files.  This is a
179108322fbScarlsonj  * private interface used by Live Upgrade extensions to access zone
180108322fbScarlsonj  * configuration inside mounted alternate boot environments.
181108322fbScarlsonj  */
182108322fbScarlsonj void
183108322fbScarlsonj zonecfg_set_root(const char *rootpath)
184108322fbScarlsonj {
185108322fbScarlsonj 	if (*zonecfg_root != '\0')
186108322fbScarlsonj 		free(zonecfg_root);
187108322fbScarlsonj 	if (rootpath == NULL || rootpath[0] == '\0' || rootpath[1] == '\0' ||
188108322fbScarlsonj 	    (zonecfg_root = strdup(rootpath)) == NULL)
189108322fbScarlsonj 		zonecfg_root = "";
190108322fbScarlsonj }
191108322fbScarlsonj 
192108322fbScarlsonj const char *
193108322fbScarlsonj zonecfg_get_root(void)
194108322fbScarlsonj {
195108322fbScarlsonj 	return (zonecfg_root);
196108322fbScarlsonj }
197108322fbScarlsonj 
198108322fbScarlsonj boolean_t
199108322fbScarlsonj zonecfg_in_alt_root(void)
200108322fbScarlsonj {
201108322fbScarlsonj 	return (*zonecfg_root != '\0');
202108322fbScarlsonj }
203108322fbScarlsonj 
204108322fbScarlsonj /*
2057c478bd9Sstevel@tonic-gate  * Callers of the _file_path() functions are expected to have the second
2067c478bd9Sstevel@tonic-gate  * parameter be a (char foo[MAXPATHLEN]).
2077c478bd9Sstevel@tonic-gate  */
2087c478bd9Sstevel@tonic-gate 
209108322fbScarlsonj static boolean_t
2107c478bd9Sstevel@tonic-gate config_file_path(const char *zonename, char *answer)
2117c478bd9Sstevel@tonic-gate {
212108322fbScarlsonj 	return (snprintf(answer, MAXPATHLEN, "%s%s/%s.xml", zonecfg_root,
213108322fbScarlsonj 	    ZONE_CONFIG_ROOT, zonename) < MAXPATHLEN);
2147c478bd9Sstevel@tonic-gate }
2157c478bd9Sstevel@tonic-gate 
216108322fbScarlsonj static boolean_t
217108322fbScarlsonj snap_file_path(const char *zonename, char *answer)
2187c478bd9Sstevel@tonic-gate {
219108322fbScarlsonj 	return (snprintf(answer, MAXPATHLEN, "%s%s/%s.snapshot.xml",
220108322fbScarlsonj 	    zonecfg_root, ZONE_SNAPSHOT_ROOT, zonename) < MAXPATHLEN);
2217c478bd9Sstevel@tonic-gate }
2227c478bd9Sstevel@tonic-gate 
2237c478bd9Sstevel@tonic-gate /*ARGSUSED*/
2247c478bd9Sstevel@tonic-gate static void
2257c478bd9Sstevel@tonic-gate zonecfg_error_func(void *ctx, const char *msg, ...)
2267c478bd9Sstevel@tonic-gate {
2277c478bd9Sstevel@tonic-gate 	/*
2287c478bd9Sstevel@tonic-gate 	 * This function does nothing by design.  Its purpose is to prevent
2297c478bd9Sstevel@tonic-gate 	 * libxml from dumping unwanted messages to stdout/stderr.
2307c478bd9Sstevel@tonic-gate 	 */
2317c478bd9Sstevel@tonic-gate }
2327c478bd9Sstevel@tonic-gate 
2337c478bd9Sstevel@tonic-gate zone_dochandle_t
2347c478bd9Sstevel@tonic-gate zonecfg_init_handle(void)
2357c478bd9Sstevel@tonic-gate {
236087719fdSdp 	zone_dochandle_t handle = calloc(1, sizeof (struct zone_dochandle));
2377c478bd9Sstevel@tonic-gate 	if (handle == NULL) {
2387c478bd9Sstevel@tonic-gate 		errno = Z_NOMEM;
2397c478bd9Sstevel@tonic-gate 		return (NULL);
2407c478bd9Sstevel@tonic-gate 	}
2417c478bd9Sstevel@tonic-gate 
2427c478bd9Sstevel@tonic-gate 	/* generic libxml initialization */
2437c478bd9Sstevel@tonic-gate 	xmlLineNumbersDefault(1);
2447c478bd9Sstevel@tonic-gate 	xmlLoadExtDtdDefaultValue |= XML_DETECT_IDS;
2457c478bd9Sstevel@tonic-gate 	xmlDoValidityCheckingDefaultValue = 1;
2467c478bd9Sstevel@tonic-gate 	(void) xmlKeepBlanksDefault(0);
2477c478bd9Sstevel@tonic-gate 	xmlGetWarningsDefaultValue = 0;
2487c478bd9Sstevel@tonic-gate 	xmlSetGenericErrorFunc(NULL, zonecfg_error_func);
2497c478bd9Sstevel@tonic-gate 
2507c478bd9Sstevel@tonic-gate 	return (handle);
2517c478bd9Sstevel@tonic-gate }
2527c478bd9Sstevel@tonic-gate 
2537c478bd9Sstevel@tonic-gate int
2547c478bd9Sstevel@tonic-gate zonecfg_check_handle(zone_dochandle_t handle)
2557c478bd9Sstevel@tonic-gate {
2567c478bd9Sstevel@tonic-gate 	if (handle == NULL || handle->zone_dh_doc == NULL)
2577c478bd9Sstevel@tonic-gate 		return (Z_BAD_HANDLE);
2587c478bd9Sstevel@tonic-gate 	return (Z_OK);
2597c478bd9Sstevel@tonic-gate }
2607c478bd9Sstevel@tonic-gate 
2617c478bd9Sstevel@tonic-gate void
2627c478bd9Sstevel@tonic-gate zonecfg_fini_handle(zone_dochandle_t handle)
2637c478bd9Sstevel@tonic-gate {
2647c478bd9Sstevel@tonic-gate 	if (zonecfg_check_handle(handle) == Z_OK)
2657c478bd9Sstevel@tonic-gate 		xmlFreeDoc(handle->zone_dh_doc);
2667c478bd9Sstevel@tonic-gate 	if (handle != NULL)
2677c478bd9Sstevel@tonic-gate 		free(handle);
2687c478bd9Sstevel@tonic-gate }
2697c478bd9Sstevel@tonic-gate 
2707c478bd9Sstevel@tonic-gate static int
2717c478bd9Sstevel@tonic-gate zonecfg_destroy_impl(char *filename)
2727c478bd9Sstevel@tonic-gate {
2737c478bd9Sstevel@tonic-gate 	if (unlink(filename) == -1) {
2747c478bd9Sstevel@tonic-gate 		if (errno == EACCES)
2757c478bd9Sstevel@tonic-gate 			return (Z_ACCES);
2767c478bd9Sstevel@tonic-gate 		if (errno == ENOENT)
2777c478bd9Sstevel@tonic-gate 			return (Z_NO_ZONE);
2787c478bd9Sstevel@tonic-gate 		return (Z_MISC_FS);
2797c478bd9Sstevel@tonic-gate 	}
2807c478bd9Sstevel@tonic-gate 	return (Z_OK);
2817c478bd9Sstevel@tonic-gate }
2827c478bd9Sstevel@tonic-gate 
2837c478bd9Sstevel@tonic-gate int
284087719fdSdp zonecfg_destroy(const char *zonename, boolean_t force)
2857c478bd9Sstevel@tonic-gate {
2867c478bd9Sstevel@tonic-gate 	char path[MAXPATHLEN];
287087719fdSdp 	struct zoneent ze;
288087719fdSdp 	int err, state_err;
289087719fdSdp 	zone_state_t state;
2907c478bd9Sstevel@tonic-gate 
291108322fbScarlsonj 	if (!config_file_path(zonename, path))
292108322fbScarlsonj 		return (Z_MISC_FS);
293087719fdSdp 
294087719fdSdp 	state_err = zone_get_state((char *)zonename, &state);
295087719fdSdp 	err = access(path, W_OK);
296087719fdSdp 
297087719fdSdp 	/*
298087719fdSdp 	 * If there is no file, and no index entry, reliably indicate that no
299087719fdSdp 	 * such zone exists.
300087719fdSdp 	 */
301087719fdSdp 	if ((state_err == Z_NO_ZONE) && (err == -1) && (errno == ENOENT))
302087719fdSdp 		return (Z_NO_ZONE);
303087719fdSdp 
304087719fdSdp 	/*
305087719fdSdp 	 * Handle any other filesystem related errors (except if the XML
306087719fdSdp 	 * file is missing, which we treat silently), unless we're forcing,
307087719fdSdp 	 * in which case we plow on.
308087719fdSdp 	 */
309087719fdSdp 	if (err == -1 && errno != ENOENT) {
310087719fdSdp 		if (errno == EACCES)
311087719fdSdp 			return (Z_ACCES);
312087719fdSdp 		else if (!force)
313087719fdSdp 			return (Z_MISC_FS);
314087719fdSdp 	}
315087719fdSdp 
316087719fdSdp 	if (state > ZONE_STATE_INSTALLED)
317087719fdSdp 		return (Z_BAD_ZONE_STATE);
318087719fdSdp 
319087719fdSdp 	if (!force && state > ZONE_STATE_CONFIGURED)
320087719fdSdp 		return (Z_BAD_ZONE_STATE);
321087719fdSdp 
322087719fdSdp 	/*
323087719fdSdp 	 * Index deletion succeeds even if the entry doesn't exist.  So this
324087719fdSdp 	 * will fail only if we've had some more severe problem.
325087719fdSdp 	 */
326087719fdSdp 	bzero(&ze, sizeof (ze));
327087719fdSdp 	(void) strlcpy(ze.zone_name, zonename, sizeof (ze.zone_name));
328087719fdSdp 	if ((err = putzoneent(&ze, PZE_REMOVE)) != Z_OK)
329087719fdSdp 		if (!force)
330087719fdSdp 			return (err);
331087719fdSdp 
332087719fdSdp 	err = zonecfg_destroy_impl(path);
333087719fdSdp 
334087719fdSdp 	/*
335087719fdSdp 	 * Treat failure to find the XML file silently, since, well, it's
336087719fdSdp 	 * gone, and with the index file cleaned up, we're done.
337087719fdSdp 	 */
338087719fdSdp 	if (err == Z_OK || err == Z_NO_ZONE)
339087719fdSdp 		return (Z_OK);
340087719fdSdp 	return (err);
3417c478bd9Sstevel@tonic-gate }
3427c478bd9Sstevel@tonic-gate 
3437c478bd9Sstevel@tonic-gate int
344108322fbScarlsonj zonecfg_destroy_snapshot(const char *zonename)
3457c478bd9Sstevel@tonic-gate {
3467c478bd9Sstevel@tonic-gate 	char path[MAXPATHLEN];
3477c478bd9Sstevel@tonic-gate 
348108322fbScarlsonj 	if (!snap_file_path(zonename, path))
349108322fbScarlsonj 		return (Z_MISC_FS);
3507c478bd9Sstevel@tonic-gate 	return (zonecfg_destroy_impl(path));
3517c478bd9Sstevel@tonic-gate }
3527c478bd9Sstevel@tonic-gate 
3537c478bd9Sstevel@tonic-gate static int
354a1be23daSdp getroot(zone_dochandle_t handle, xmlNodePtr *root)
3557c478bd9Sstevel@tonic-gate {
3567c478bd9Sstevel@tonic-gate 	if (zonecfg_check_handle(handle) == Z_BAD_HANDLE)
3577c478bd9Sstevel@tonic-gate 		return (Z_BAD_HANDLE);
3587c478bd9Sstevel@tonic-gate 
359a1be23daSdp 	*root = xmlDocGetRootElement(handle->zone_dh_doc);
360a1be23daSdp 
361a1be23daSdp 	if (*root == NULL)
3627c478bd9Sstevel@tonic-gate 		return (Z_EMPTY_DOCUMENT);
363a1be23daSdp 
364a1be23daSdp 	if (xmlStrcmp((*root)->name, DTD_ELEM_ZONE))
365a1be23daSdp 		return (Z_WRONG_DOC_TYPE);
366a1be23daSdp 
367a1be23daSdp 	return (Z_OK);
3687c478bd9Sstevel@tonic-gate }
3697c478bd9Sstevel@tonic-gate 
370a1be23daSdp static int
371a1be23daSdp operation_prep(zone_dochandle_t handle)
372a1be23daSdp {
373a1be23daSdp 	xmlNodePtr root;
374a1be23daSdp 	int err;
375a1be23daSdp 
376a1be23daSdp 	if ((err = getroot(handle, &root)) != 0)
377a1be23daSdp 		return (err);
378a1be23daSdp 
379a1be23daSdp 	handle->zone_dh_cur = root;
380a1be23daSdp 	handle->zone_dh_top = root;
381a1be23daSdp 	return (Z_OK);
3827c478bd9Sstevel@tonic-gate }
383a1be23daSdp 
384a1be23daSdp static int
385ffbafc53Scomay fetchprop(xmlNodePtr cur, const xmlChar *propname, char *dst, size_t dstsize)
386ffbafc53Scomay {
387ffbafc53Scomay 	xmlChar *property;
388ffbafc53Scomay 	size_t srcsize;
389ffbafc53Scomay 
390ffbafc53Scomay 	if ((property = xmlGetProp(cur, propname)) == NULL)
391ffbafc53Scomay 		return (Z_BAD_PROPERTY);
392ffbafc53Scomay 	srcsize = strlcpy(dst, (char *)property, dstsize);
393ffbafc53Scomay 	xmlFree(property);
394ffbafc53Scomay 	if (srcsize >= dstsize)
395ffbafc53Scomay 		return (Z_TOO_BIG);
396ffbafc53Scomay 	return (Z_OK);
397ffbafc53Scomay }
398ffbafc53Scomay 
399ffbafc53Scomay static int
400ffbafc53Scomay fetch_alloc_prop(xmlNodePtr cur, const xmlChar *propname, char **dst)
401ffbafc53Scomay {
402ffbafc53Scomay 	xmlChar *property;
403ffbafc53Scomay 
404ffbafc53Scomay 	if ((property = xmlGetProp(cur, propname)) == NULL)
405ffbafc53Scomay 		return (Z_BAD_PROPERTY);
406ffbafc53Scomay 	if ((*dst = strdup((char *)property)) == NULL) {
407ffbafc53Scomay 		xmlFree(property);
408ffbafc53Scomay 		return (Z_NOMEM);
409ffbafc53Scomay 	}
410ffbafc53Scomay 	xmlFree(property);
411ffbafc53Scomay 	return (Z_OK);
412ffbafc53Scomay }
413ffbafc53Scomay 
414ffbafc53Scomay static int
415a1be23daSdp getrootattr(zone_dochandle_t handle, const xmlChar *propname,
416a1be23daSdp     char *propval, size_t propsize)
417a1be23daSdp {
418a1be23daSdp 	xmlNodePtr root;
419a1be23daSdp 	int err;
420a1be23daSdp 
421a1be23daSdp 	if ((err = getroot(handle, &root)) != 0)
422a1be23daSdp 		return (err);
423a1be23daSdp 
424ffbafc53Scomay 	return (fetchprop(root, propname, propval, propsize));
425ffbafc53Scomay }
426ffbafc53Scomay 
427ffbafc53Scomay static int
428ffbafc53Scomay get_alloc_rootattr(zone_dochandle_t handle, const xmlChar *propname,
429ffbafc53Scomay     char **propval)
430ffbafc53Scomay {
431ffbafc53Scomay 	xmlNodePtr root;
432ffbafc53Scomay 	int err;
433ffbafc53Scomay 
434ffbafc53Scomay 	if ((err = getroot(handle, &root)) != 0)
435ffbafc53Scomay 		return (err);
436ffbafc53Scomay 
437ffbafc53Scomay 	return (fetch_alloc_prop(root, propname, propval));
438a1be23daSdp }
439a1be23daSdp 
440a1be23daSdp static int
441108322fbScarlsonj setrootattr(zone_dochandle_t handle, const xmlChar *propname,
442108322fbScarlsonj     const char *propval)
443a1be23daSdp {
444a1be23daSdp 	int err;
445a1be23daSdp 	xmlNodePtr root;
446a1be23daSdp 
447a1be23daSdp 	if (propval == NULL)
448a1be23daSdp 		return (Z_INVAL);
449a1be23daSdp 
450a1be23daSdp 	if ((err = getroot(handle, &root)) != Z_OK)
451a1be23daSdp 		return (err);
452a1be23daSdp 
453a1be23daSdp 	if (xmlSetProp(root, propname, (const xmlChar *) propval) == NULL)
454a1be23daSdp 		return (Z_INVAL);
4557c478bd9Sstevel@tonic-gate 	return (Z_OK);
4567c478bd9Sstevel@tonic-gate }
4577c478bd9Sstevel@tonic-gate 
458087719fdSdp static void
459087719fdSdp addcomment(zone_dochandle_t handle, const char *comment)
460087719fdSdp {
461087719fdSdp 	xmlNodePtr node;
462087719fdSdp 	node = xmlNewComment((xmlChar *) comment);
463087719fdSdp 
464087719fdSdp 	if (node != NULL)
465087719fdSdp 		(void) xmlAddPrevSibling(handle->zone_dh_top, node);
466087719fdSdp }
467087719fdSdp 
468087719fdSdp static void
469087719fdSdp stripcomments(zone_dochandle_t handle)
470087719fdSdp {
471087719fdSdp 	xmlDocPtr top;
472087719fdSdp 	xmlNodePtr child, next;
473087719fdSdp 
474087719fdSdp 	top = handle->zone_dh_doc;
475087719fdSdp 	for (child = top->xmlChildrenNode; child != NULL; child = next) {
476087719fdSdp 		next = child->next;
477087719fdSdp 		if (child->name == NULL)
478087719fdSdp 			continue;
479087719fdSdp 		if (xmlStrcmp(child->name, DTD_ELEM_COMMENT) == 0) {
480087719fdSdp 			next = child->next;
481087719fdSdp 			xmlUnlinkNode(child);
482087719fdSdp 			xmlFreeNode(child);
483087719fdSdp 		}
484087719fdSdp 	}
485087719fdSdp }
486087719fdSdp 
487ee519a1fSgjelinek static void
488ee519a1fSgjelinek strip_sw_inv(zone_dochandle_t handle)
489ee519a1fSgjelinek {
490ee519a1fSgjelinek 	xmlNodePtr root, child, next;
491ee519a1fSgjelinek 
492ee519a1fSgjelinek 	root = xmlDocGetRootElement(handle->zone_dh_doc);
493ee519a1fSgjelinek 	for (child = root->xmlChildrenNode; child != NULL; child = next) {
494ee519a1fSgjelinek 		next = child->next;
495ee519a1fSgjelinek 		if (child->name == NULL)
496ee519a1fSgjelinek 			continue;
497ee519a1fSgjelinek 		if (xmlStrcmp(child->name, DTD_ELEM_PACKAGE) == 0 ||
498ee519a1fSgjelinek 		    xmlStrcmp(child->name, DTD_ELEM_PATCH) == 0) {
499ee519a1fSgjelinek 			next = child->next;
500ee519a1fSgjelinek 			xmlUnlinkNode(child);
501ee519a1fSgjelinek 			xmlFreeNode(child);
502ee519a1fSgjelinek 		}
503ee519a1fSgjelinek 	}
504ee519a1fSgjelinek }
505ee519a1fSgjelinek 
5067c478bd9Sstevel@tonic-gate static int
507108322fbScarlsonj zonecfg_get_handle_impl(const char *zonename, const char *filename,
508108322fbScarlsonj     zone_dochandle_t handle)
5097c478bd9Sstevel@tonic-gate {
5107c478bd9Sstevel@tonic-gate 	xmlValidCtxtPtr cvp;
5117c478bd9Sstevel@tonic-gate 	struct stat statbuf;
5127c478bd9Sstevel@tonic-gate 	int valid;
5137c478bd9Sstevel@tonic-gate 
5147c478bd9Sstevel@tonic-gate 	if (zonename == NULL)
5157c478bd9Sstevel@tonic-gate 		return (Z_NO_ZONE);
5167c478bd9Sstevel@tonic-gate 	if ((handle->zone_dh_doc = xmlParseFile(filename)) == NULL) {
5177c478bd9Sstevel@tonic-gate 		/* distinguish file not found vs. found but not parsed */
5187c478bd9Sstevel@tonic-gate 		if (stat(filename, &statbuf) == 0)
5197c478bd9Sstevel@tonic-gate 			return (Z_INVALID_DOCUMENT);
5207c478bd9Sstevel@tonic-gate 		return (Z_NO_ZONE);
5217c478bd9Sstevel@tonic-gate 	}
5227c478bd9Sstevel@tonic-gate 	if ((cvp = xmlNewValidCtxt()) == NULL)
5237c478bd9Sstevel@tonic-gate 		return (Z_NOMEM);
5247c478bd9Sstevel@tonic-gate 	cvp->error = zonecfg_error_func;
5257c478bd9Sstevel@tonic-gate 	cvp->warning = zonecfg_error_func;
5267c478bd9Sstevel@tonic-gate 	valid = xmlValidateDocument(cvp, handle->zone_dh_doc);
5277c478bd9Sstevel@tonic-gate 	xmlFreeValidCtxt(cvp);
5287c478bd9Sstevel@tonic-gate 	if (valid == 0)
5297c478bd9Sstevel@tonic-gate 		return (Z_INVALID_DOCUMENT);
530087719fdSdp 
5317c478bd9Sstevel@tonic-gate 	/* delete any comments such as inherited Sun copyright / ident str */
532087719fdSdp 	stripcomments(handle);
5337c478bd9Sstevel@tonic-gate 	return (Z_OK);
5347c478bd9Sstevel@tonic-gate }
5357c478bd9Sstevel@tonic-gate 
5367c478bd9Sstevel@tonic-gate int
537108322fbScarlsonj zonecfg_get_handle(const char *zonename, zone_dochandle_t handle)
5387c478bd9Sstevel@tonic-gate {
5397c478bd9Sstevel@tonic-gate 	char path[MAXPATHLEN];
5407c478bd9Sstevel@tonic-gate 
541108322fbScarlsonj 	if (!config_file_path(zonename, path))
542108322fbScarlsonj 		return (Z_MISC_FS);
543087719fdSdp 	handle->zone_dh_newzone = B_FALSE;
544087719fdSdp 
5457c478bd9Sstevel@tonic-gate 	return (zonecfg_get_handle_impl(zonename, path, handle));
5467c478bd9Sstevel@tonic-gate }
5477c478bd9Sstevel@tonic-gate 
5487c478bd9Sstevel@tonic-gate int
549ee519a1fSgjelinek zonecfg_get_attach_handle(const char *path, const char *zonename,
550ee519a1fSgjelinek     boolean_t preserve_sw, zone_dochandle_t handle)
551ee519a1fSgjelinek {
552ee519a1fSgjelinek 	char		migpath[MAXPATHLEN];
553ee519a1fSgjelinek 	int		err;
554ee519a1fSgjelinek 	struct stat	buf;
555ee519a1fSgjelinek 
556ee519a1fSgjelinek 	if (snprintf(migpath, sizeof (migpath), "%s/root", path) >=
557ee519a1fSgjelinek 	    sizeof (migpath))
558ee519a1fSgjelinek 		return (Z_NOMEM);
559ee519a1fSgjelinek 
560ee519a1fSgjelinek 	if (stat(migpath, &buf) == -1 || !S_ISDIR(buf.st_mode))
561ee519a1fSgjelinek 		return (Z_NO_ZONE);
562ee519a1fSgjelinek 
563ee519a1fSgjelinek 	if (snprintf(migpath, sizeof (migpath), "%s/%s", path, DETACHED) >=
564ee519a1fSgjelinek 	    sizeof (migpath))
565ee519a1fSgjelinek 		return (Z_NOMEM);
566ee519a1fSgjelinek 
567ee519a1fSgjelinek 	if ((err = zonecfg_get_handle_impl(zonename, migpath, handle)) != Z_OK)
568ee519a1fSgjelinek 		return (err);
569ee519a1fSgjelinek 
570ee519a1fSgjelinek 	if (!preserve_sw)
571ee519a1fSgjelinek 		strip_sw_inv(handle);
572ee519a1fSgjelinek 
573ee519a1fSgjelinek 	handle->zone_dh_newzone = B_TRUE;
574ee519a1fSgjelinek 	if ((err = setrootattr(handle, DTD_ATTR_ZONEPATH, path)) != Z_OK)
575ee519a1fSgjelinek 		return (err);
576ee519a1fSgjelinek 
577ee519a1fSgjelinek 	return (setrootattr(handle, DTD_ATTR_NAME, zonename));
578ee519a1fSgjelinek }
579ee519a1fSgjelinek 
580ee519a1fSgjelinek int
581108322fbScarlsonj zonecfg_get_snapshot_handle(const char *zonename, zone_dochandle_t handle)
5827c478bd9Sstevel@tonic-gate {
5837c478bd9Sstevel@tonic-gate 	char path[MAXPATHLEN];
5847c478bd9Sstevel@tonic-gate 
585108322fbScarlsonj 	if (!snap_file_path(zonename, path))
586108322fbScarlsonj 		return (Z_MISC_FS);
587087719fdSdp 	handle->zone_dh_newzone = B_FALSE;
5887c478bd9Sstevel@tonic-gate 	return (zonecfg_get_handle_impl(zonename, path, handle));
5897c478bd9Sstevel@tonic-gate }
5907c478bd9Sstevel@tonic-gate 
5917c478bd9Sstevel@tonic-gate int
592108322fbScarlsonj zonecfg_get_template_handle(const char *template, const char *zonename,
593087719fdSdp     zone_dochandle_t handle)
594087719fdSdp {
595087719fdSdp 	char path[MAXPATHLEN];
596087719fdSdp 	int err;
597087719fdSdp 
598108322fbScarlsonj 	if (!config_file_path(template, path))
599108322fbScarlsonj 		return (Z_MISC_FS);
600087719fdSdp 
601087719fdSdp 	if ((err = zonecfg_get_handle_impl(template, path, handle)) != Z_OK)
602087719fdSdp 		return (err);
603087719fdSdp 	handle->zone_dh_newzone = B_TRUE;
604087719fdSdp 	return (setrootattr(handle, DTD_ATTR_NAME, zonename));
605087719fdSdp }
606087719fdSdp 
6078cd327d5Sgjelinek /*
6088cd327d5Sgjelinek  * Initialize two handles from the manifest read on fd.  The rem_handle
6098cd327d5Sgjelinek  * is initialized from the input file, including the sw inventory.  The
6108cd327d5Sgjelinek  * local_handle is initialized with the same zone configuration but with
6118cd327d5Sgjelinek  * no sw inventory.
6128cd327d5Sgjelinek  */
6138cd327d5Sgjelinek int
6148cd327d5Sgjelinek zonecfg_attach_manifest(int fd, zone_dochandle_t local_handle,
6158cd327d5Sgjelinek     zone_dochandle_t rem_handle)
6168cd327d5Sgjelinek {
6178cd327d5Sgjelinek 	xmlValidCtxtPtr cvp;
6188cd327d5Sgjelinek 	int valid;
6198cd327d5Sgjelinek 
6208cd327d5Sgjelinek 	/* load the manifest into the handle for the remote system */
6218cd327d5Sgjelinek 	if ((rem_handle->zone_dh_doc = xmlReadFd(fd, NULL, NULL, 0)) == NULL) {
6228cd327d5Sgjelinek 		return (Z_INVALID_DOCUMENT);
6238cd327d5Sgjelinek 	}
6248cd327d5Sgjelinek 	if ((cvp = xmlNewValidCtxt()) == NULL)
6258cd327d5Sgjelinek 		return (Z_NOMEM);
6268cd327d5Sgjelinek 	cvp->error = zonecfg_error_func;
6278cd327d5Sgjelinek 	cvp->warning = zonecfg_error_func;
6288cd327d5Sgjelinek 	valid = xmlValidateDocument(cvp, rem_handle->zone_dh_doc);
6298cd327d5Sgjelinek 	xmlFreeValidCtxt(cvp);
6308cd327d5Sgjelinek 	if (valid == 0)
6318cd327d5Sgjelinek 		return (Z_INVALID_DOCUMENT);
6328cd327d5Sgjelinek 
6338cd327d5Sgjelinek 	/* delete any comments such as inherited Sun copyright / ident str */
6348cd327d5Sgjelinek 	stripcomments(rem_handle);
6358cd327d5Sgjelinek 
6368cd327d5Sgjelinek 	rem_handle->zone_dh_newzone = B_TRUE;
6378cd327d5Sgjelinek 	rem_handle->zone_dh_sw_inv = B_TRUE;
6388cd327d5Sgjelinek 
6398cd327d5Sgjelinek 	/*
6408cd327d5Sgjelinek 	 * Now use the remote system handle to generate a local system handle
6418cd327d5Sgjelinek 	 * with an identical zones configuration but no sw inventory.
6428cd327d5Sgjelinek 	 */
6438cd327d5Sgjelinek 	if ((local_handle->zone_dh_doc = xmlCopyDoc(rem_handle->zone_dh_doc,
6448cd327d5Sgjelinek 	    1)) == NULL) {
6458cd327d5Sgjelinek 		return (Z_INVALID_DOCUMENT);
6468cd327d5Sgjelinek 	}
6478cd327d5Sgjelinek 
6488cd327d5Sgjelinek 	/*
6498cd327d5Sgjelinek 	 * We need to re-run xmlValidateDocument on local_handle to properly
6508cd327d5Sgjelinek 	 * update the in-core representation of the configuration.
6518cd327d5Sgjelinek 	 */
6528cd327d5Sgjelinek 	if ((cvp = xmlNewValidCtxt()) == NULL)
6538cd327d5Sgjelinek 		return (Z_NOMEM);
6548cd327d5Sgjelinek 	cvp->error = zonecfg_error_func;
6558cd327d5Sgjelinek 	cvp->warning = zonecfg_error_func;
6568cd327d5Sgjelinek 	valid = xmlValidateDocument(cvp, local_handle->zone_dh_doc);
6578cd327d5Sgjelinek 	xmlFreeValidCtxt(cvp);
6588cd327d5Sgjelinek 	if (valid == 0)
6598cd327d5Sgjelinek 		return (Z_INVALID_DOCUMENT);
6608cd327d5Sgjelinek 
6618cd327d5Sgjelinek 	strip_sw_inv(local_handle);
6628cd327d5Sgjelinek 
6638cd327d5Sgjelinek 	local_handle->zone_dh_newzone = B_TRUE;
6648cd327d5Sgjelinek 	local_handle->zone_dh_sw_inv = B_FALSE;
6658cd327d5Sgjelinek 
6668cd327d5Sgjelinek 	return (Z_OK);
6678cd327d5Sgjelinek }
6688cd327d5Sgjelinek 
669087719fdSdp static boolean_t
670087719fdSdp is_renaming(zone_dochandle_t handle)
671087719fdSdp {
672087719fdSdp 	if (handle->zone_dh_newzone)
673087719fdSdp 		return (B_FALSE);
674087719fdSdp 	if (strlen(handle->zone_dh_delete_name) > 0)
675087719fdSdp 		return (B_TRUE);
676087719fdSdp 	return (B_FALSE);
677087719fdSdp }
678087719fdSdp 
679087719fdSdp static boolean_t
680087719fdSdp is_new(zone_dochandle_t handle)
681087719fdSdp {
682087719fdSdp 	return (handle->zone_dh_newzone || handle->zone_dh_snapshot);
683087719fdSdp }
684087719fdSdp 
685087719fdSdp static boolean_t
686087719fdSdp is_snapshot(zone_dochandle_t handle)
687087719fdSdp {
688087719fdSdp 	return (handle->zone_dh_snapshot);
689087719fdSdp }
690087719fdSdp 
691087719fdSdp /*
692087719fdSdp  * It would be great to be able to use libc's ctype(3c) macros, but we
693087719fdSdp  * can't, as they are locale sensitive, and it would break our limited thread
694087719fdSdp  * safety if this routine had to change the app locale on the fly.
695087719fdSdp  */
696087719fdSdp int
697108322fbScarlsonj zonecfg_validate_zonename(const char *zone)
698087719fdSdp {
699087719fdSdp 	int i;
700087719fdSdp 
701087719fdSdp 	if (strcmp(zone, GLOBAL_ZONENAME) == 0)
702087719fdSdp 		return (Z_BOGUS_ZONE_NAME);
703087719fdSdp 
704087719fdSdp 	if (strlen(zone) >= ZONENAME_MAX)
705087719fdSdp 		return (Z_BOGUS_ZONE_NAME);
706087719fdSdp 
707087719fdSdp 	if (!((zone[0] >= 'a' && zone[0] <= 'z') ||
708087719fdSdp 	    (zone[0] >= 'A' && zone[0] <= 'Z') ||
709087719fdSdp 	    (zone[0] >= '0' && zone[0] <= '9')))
710087719fdSdp 		return (Z_BOGUS_ZONE_NAME);
711087719fdSdp 
712087719fdSdp 	for (i = 1; zone[i] != '\0'; i++) {
713087719fdSdp 		if (!((zone[i] >= 'a' && zone[i] <= 'z') ||
714087719fdSdp 		    (zone[i] >= 'A' && zone[i] <= 'Z') ||
715087719fdSdp 		    (zone[i] >= '0' && zone[i] <= '9') ||
716087719fdSdp 		    (zone[i] == '-') || (zone[i] == '_') || (zone[i] == '.')))
717087719fdSdp 			return (Z_BOGUS_ZONE_NAME);
718087719fdSdp 	}
719087719fdSdp 
720087719fdSdp 	return (Z_OK);
721087719fdSdp }
722087719fdSdp 
723087719fdSdp /*
724087719fdSdp  * Changing the zone name requires us to track both the old and new
725087719fdSdp  * name of the zone until commit time.
726087719fdSdp  */
727087719fdSdp int
7287c478bd9Sstevel@tonic-gate zonecfg_get_name(zone_dochandle_t handle, char *name, size_t namesize)
7297c478bd9Sstevel@tonic-gate {
730a1be23daSdp 	return (getrootattr(handle, DTD_ATTR_NAME, name, namesize));
7317c478bd9Sstevel@tonic-gate }
7327c478bd9Sstevel@tonic-gate 
733a1be23daSdp int
734a1be23daSdp zonecfg_set_name(zone_dochandle_t handle, char *name)
735a1be23daSdp {
736087719fdSdp 	zone_state_t state;
737087719fdSdp 	char curname[ZONENAME_MAX], old_delname[ZONENAME_MAX];
738087719fdSdp 	int err;
739087719fdSdp 
740087719fdSdp 	if ((err = getrootattr(handle, DTD_ATTR_NAME, curname,
741087719fdSdp 	    sizeof (curname))) != Z_OK)
742087719fdSdp 		return (err);
743087719fdSdp 
744087719fdSdp 	if (strcmp(name, curname) == 0)
745087719fdSdp 		return (Z_OK);
746087719fdSdp 
747087719fdSdp 	/*
748087719fdSdp 	 * Switching zone names to one beginning with SUNW is not permitted.
749087719fdSdp 	 */
750087719fdSdp 	if (strncmp(name, "SUNW", 4) == 0)
751087719fdSdp 		return (Z_BOGUS_ZONE_NAME);
752087719fdSdp 
753087719fdSdp 	if ((err = zonecfg_validate_zonename(name)) != Z_OK)
754087719fdSdp 		return (err);
755087719fdSdp 
756087719fdSdp 	/*
757087719fdSdp 	 * Setting the name back to the original name (effectively a revert of
758087719fdSdp 	 * the name) is fine.  But if we carry on, we'll falsely identify the
759087719fdSdp 	 * name as "in use," so special case here.
760087719fdSdp 	 */
761087719fdSdp 	if (strcmp(name, handle->zone_dh_delete_name) == 0) {
762087719fdSdp 		err = setrootattr(handle, DTD_ATTR_NAME, name);
763087719fdSdp 		handle->zone_dh_delete_name[0] = '\0';
764087719fdSdp 		return (err);
765087719fdSdp 	}
766087719fdSdp 
767087719fdSdp 	/* Check to see if new name chosen is already in use */
768087719fdSdp 	if (zone_get_state(name, &state) != Z_NO_ZONE)
769087719fdSdp 		return (Z_NAME_IN_USE);
770087719fdSdp 
771087719fdSdp 	/*
772087719fdSdp 	 * If this isn't already "new" or in a renaming transition, then
773087719fdSdp 	 * we're initiating a rename here; so stash the "delete name"
774087719fdSdp 	 * (i.e. the name of the zone we'll be removing) for the rename.
775087719fdSdp 	 */
776087719fdSdp 	(void) strlcpy(old_delname, handle->zone_dh_delete_name,
777087719fdSdp 	    sizeof (old_delname));
778087719fdSdp 	if (!is_new(handle) && !is_renaming(handle)) {
779087719fdSdp 		/*
780087719fdSdp 		 * Name change is allowed only when the zone we're altering
781087719fdSdp 		 * is not ready or running.
782087719fdSdp 		 */
783087719fdSdp 		err = zone_get_state(curname, &state);
784087719fdSdp 		if (err == Z_OK) {
785087719fdSdp 			if (state > ZONE_STATE_INSTALLED)
786087719fdSdp 				return (Z_BAD_ZONE_STATE);
787087719fdSdp 		} else if (err != Z_NO_ZONE) {
788087719fdSdp 			return (err);
789087719fdSdp 		}
790087719fdSdp 
791087719fdSdp 		(void) strlcpy(handle->zone_dh_delete_name, curname,
792087719fdSdp 		    sizeof (handle->zone_dh_delete_name));
793087719fdSdp 		assert(is_renaming(handle));
794087719fdSdp 	} else if (is_renaming(handle)) {
795087719fdSdp 		err = zone_get_state(handle->zone_dh_delete_name, &state);
796087719fdSdp 		if (err == Z_OK) {
797087719fdSdp 			if (state > ZONE_STATE_INSTALLED)
798087719fdSdp 				return (Z_BAD_ZONE_STATE);
799087719fdSdp 		} else if (err != Z_NO_ZONE) {
800087719fdSdp 			return (err);
801087719fdSdp 		}
802087719fdSdp 	}
803087719fdSdp 
804087719fdSdp 	if ((err = setrootattr(handle, DTD_ATTR_NAME, name)) != Z_OK) {
805087719fdSdp 		/*
806087719fdSdp 		 * Restore the deletename to whatever it was at the
807087719fdSdp 		 * top of the routine, since we've had a failure.
808087719fdSdp 		 */
809087719fdSdp 		(void) strlcpy(handle->zone_dh_delete_name, old_delname,
810087719fdSdp 		    sizeof (handle->zone_dh_delete_name));
811087719fdSdp 		return (err);
812087719fdSdp 	}
813087719fdSdp 
814087719fdSdp 	return (Z_OK);
8157c478bd9Sstevel@tonic-gate }
816a1be23daSdp 
817a1be23daSdp int
818a1be23daSdp zonecfg_get_zonepath(zone_dochandle_t handle, char *path, size_t pathsize)
819a1be23daSdp {
820108322fbScarlsonj 	size_t len;
821108322fbScarlsonj 
822108322fbScarlsonj 	if ((len = strlcpy(path, zonecfg_root, pathsize)) >= pathsize)
823108322fbScarlsonj 		return (Z_TOO_BIG);
824108322fbScarlsonj 	return (getrootattr(handle, DTD_ATTR_ZONEPATH, path + len,
825108322fbScarlsonj 	    pathsize - len));
826a1be23daSdp }
827a1be23daSdp 
828a1be23daSdp int
829a1be23daSdp zonecfg_set_zonepath(zone_dochandle_t handle, char *zonepath)
830a1be23daSdp {
831*555afedfScarlsonj 	size_t len;
832*555afedfScarlsonj 
833*555afedfScarlsonj 	/*
834*555afedfScarlsonj 	 * The user deals in absolute paths in the running global zone, but the
835*555afedfScarlsonj 	 * internal configuration files deal with boot environment relative
836*555afedfScarlsonj 	 * paths.  Strip out the alternate root when specified.
837*555afedfScarlsonj 	 */
838*555afedfScarlsonj 	len = strlen(zonecfg_root);
839*555afedfScarlsonj 	if (strncmp(zonepath, zonecfg_root, len) != 0 || zonepath[len] != '/')
840*555afedfScarlsonj 		return (Z_BAD_PROPERTY);
841*555afedfScarlsonj 	zonepath += len;
842a1be23daSdp 	return (setrootattr(handle, DTD_ATTR_ZONEPATH, zonepath));
843a1be23daSdp }
844a1be23daSdp 
845a1be23daSdp int
846a1be23daSdp zonecfg_get_autoboot(zone_dochandle_t handle, boolean_t *autoboot)
847a1be23daSdp {
848a1be23daSdp 	char autobootstr[DTD_ENTITY_BOOL_LEN];
849a1be23daSdp 	int ret;
850a1be23daSdp 
851a1be23daSdp 	if ((ret = getrootattr(handle, DTD_ATTR_AUTOBOOT, autobootstr,
852a1be23daSdp 	    sizeof (autobootstr))) != Z_OK)
853a1be23daSdp 		return (ret);
854a1be23daSdp 
855a1be23daSdp 	if (strcmp(autobootstr, DTD_ENTITY_TRUE) == 0)
856a1be23daSdp 		*autoboot = B_TRUE;
857a1be23daSdp 	else if (strcmp(autobootstr, DTD_ENTITY_FALSE) == 0)
858a1be23daSdp 		*autoboot = B_FALSE;
859a1be23daSdp 	else
860a1be23daSdp 		ret = Z_BAD_PROPERTY;
861a1be23daSdp 	return (ret);
862a1be23daSdp }
863a1be23daSdp 
864a1be23daSdp int
865a1be23daSdp zonecfg_set_autoboot(zone_dochandle_t handle, boolean_t autoboot)
866a1be23daSdp {
867a1be23daSdp 	return (setrootattr(handle, DTD_ATTR_AUTOBOOT,
868a1be23daSdp 	    autoboot ? DTD_ENTITY_TRUE : DTD_ENTITY_FALSE));
869a1be23daSdp }
870a1be23daSdp 
871a1be23daSdp int
872a1be23daSdp zonecfg_get_pool(zone_dochandle_t handle, char *pool, size_t poolsize)
873a1be23daSdp {
874a1be23daSdp 	return (getrootattr(handle, DTD_ATTR_POOL, pool, poolsize));
875a1be23daSdp }
876a1be23daSdp 
877a1be23daSdp int
878a1be23daSdp zonecfg_set_pool(zone_dochandle_t handle, char *pool)
879a1be23daSdp {
880a1be23daSdp 	return (setrootattr(handle, DTD_ATTR_POOL, pool));
881a1be23daSdp }
882a1be23daSdp 
883ffbafc53Scomay int
884ffbafc53Scomay zonecfg_get_limitpriv(zone_dochandle_t handle, char **limitpriv)
885ffbafc53Scomay {
886ffbafc53Scomay 	return (get_alloc_rootattr(handle, DTD_ATTR_LIMITPRIV, limitpriv));
887ffbafc53Scomay }
888ffbafc53Scomay 
889ffbafc53Scomay int
8903f2f09c1Sdp zonecfg_set_limitpriv(zone_dochandle_t handle, char *limitpriv)
891ffbafc53Scomay {
8923f2f09c1Sdp 	return (setrootattr(handle, DTD_ATTR_LIMITPRIV, limitpriv));
8933f2f09c1Sdp }
8943f2f09c1Sdp 
8953f2f09c1Sdp int
8963f2f09c1Sdp zonecfg_get_bootargs(zone_dochandle_t handle, char *bargs, size_t bargssize)
8973f2f09c1Sdp {
8983f2f09c1Sdp 	return (getrootattr(handle, DTD_ATTR_BOOTARGS, bargs, bargssize));
8993f2f09c1Sdp }
9003f2f09c1Sdp 
9013f2f09c1Sdp int
9023f2f09c1Sdp zonecfg_set_bootargs(zone_dochandle_t handle, char *bargs)
9033f2f09c1Sdp {
9043f2f09c1Sdp 	return (setrootattr(handle, DTD_ATTR_BOOTARGS, bargs));
905ffbafc53Scomay }
906ffbafc53Scomay 
907a1be23daSdp /*
908a1be23daSdp  * /etc/zones/index caches a vital piece of information which is also
909a1be23daSdp  * in the <zonename>.xml file: the path to the zone.  This is for performance,
910a1be23daSdp  * since we need to walk all zonepath's in order to be able to detect conflicts
911a1be23daSdp  * (see crosscheck_zonepaths() in the zoneadm command).
912087719fdSdp  *
913087719fdSdp  * An additional complexity is that when doing a rename, we'd like the entire
914087719fdSdp  * index update operation (rename, and potential state changes) to be atomic.
915087719fdSdp  * In general, the operation of this function should succeed or fail as
916087719fdSdp  * a unit.
917a1be23daSdp  */
918a1be23daSdp int
919a1be23daSdp zonecfg_refresh_index_file(zone_dochandle_t handle)
920a1be23daSdp {
921a1be23daSdp 	char name[ZONENAME_MAX], zonepath[MAXPATHLEN];
922a1be23daSdp 	struct zoneent ze;
923a1be23daSdp 	int err;
924087719fdSdp 	int opcode;
925087719fdSdp 	char *zn;
926087719fdSdp 
927087719fdSdp 	bzero(&ze, sizeof (ze));
928087719fdSdp 	ze.zone_state = -1;	/* Preserve existing state in index */
929a1be23daSdp 
930a1be23daSdp 	if ((err = zonecfg_get_name(handle, name, sizeof (name))) != Z_OK)
931a1be23daSdp 		return (err);
932087719fdSdp 	(void) strlcpy(ze.zone_name, name, sizeof (ze.zone_name));
933087719fdSdp 
934a1be23daSdp 	if ((err = zonecfg_get_zonepath(handle, zonepath,
935a1be23daSdp 	    sizeof (zonepath))) != Z_OK)
936a1be23daSdp 		return (err);
937*555afedfScarlsonj 	(void) strlcpy(ze.zone_path, zonepath + strlen(zonecfg_root),
938*555afedfScarlsonj 	    sizeof (ze.zone_path));
939087719fdSdp 
940087719fdSdp 	if (is_renaming(handle)) {
941087719fdSdp 		opcode = PZE_MODIFY;
942087719fdSdp 		(void) strlcpy(ze.zone_name, handle->zone_dh_delete_name,
943087719fdSdp 		    sizeof (ze.zone_name));
944087719fdSdp 		(void) strlcpy(ze.zone_newname, name, sizeof (ze.zone_newname));
945087719fdSdp 	} else if (is_new(handle)) {
946087719fdSdp 		FILE *cookie;
947087719fdSdp 		/*
948087719fdSdp 		 * Be tolerant of the zone already existing in the index file,
949087719fdSdp 		 * since we might be forcibly overwriting an existing
950087719fdSdp 		 * configuration with a new one (for example 'create -F'
951087719fdSdp 		 * in zonecfg).
952087719fdSdp 		 */
953087719fdSdp 		opcode = PZE_ADD;
954087719fdSdp 		cookie = setzoneent();
955087719fdSdp 		while ((zn = getzoneent(cookie)) != NULL) {
956087719fdSdp 			if (strcmp(zn, name) == 0) {
957087719fdSdp 				opcode = PZE_MODIFY;
958087719fdSdp 				free(zn);
959087719fdSdp 				break;
960087719fdSdp 			}
961087719fdSdp 			free(zn);
962087719fdSdp 		}
963087719fdSdp 		endzoneent(cookie);
964087719fdSdp 		ze.zone_state = ZONE_STATE_CONFIGURED;
965087719fdSdp 	} else {
966087719fdSdp 		opcode = PZE_MODIFY;
9677c478bd9Sstevel@tonic-gate 	}
9687c478bd9Sstevel@tonic-gate 
969087719fdSdp 	if ((err = putzoneent(&ze, opcode)) != Z_OK)
970087719fdSdp 		return (err);
971087719fdSdp 
972087719fdSdp 	return (Z_OK);
973087719fdSdp }
974087719fdSdp 
975087719fdSdp /*
976087719fdSdp  * The goal of this routine is to cause the index file update and the
977087719fdSdp  * document save to happen as an atomic operation.  We do the document
978087719fdSdp  * first, saving a backup copy using a hard link; if that succeeds, we go
979087719fdSdp  * on to the index.  If that fails, we roll the document back into place.
980087719fdSdp  *
981087719fdSdp  * Strategy:
982087719fdSdp  *
983087719fdSdp  * New zone 'foo' configuration:
984087719fdSdp  * 	Create tmpfile (zonecfg.xxxxxx)
985087719fdSdp  * 	Write XML to tmpfile
986087719fdSdp  * 	Rename tmpfile to xmlfile (zonecfg.xxxxxx -> foo.xml)
987087719fdSdp  * 	Add entry to index file
988087719fdSdp  * 	If it fails, delete foo.xml, leaving nothing behind.
989087719fdSdp  *
990087719fdSdp  * Save existing zone 'foo':
991087719fdSdp  * 	Make backup of foo.xml -> .backup
992087719fdSdp  * 	Create tmpfile (zonecfg.xxxxxx)
993087719fdSdp  * 	Write XML to tmpfile
994087719fdSdp  * 	Rename tmpfile to xmlfile (zonecfg.xxxxxx -> foo.xml)
995087719fdSdp  * 	Modify index file as needed
996087719fdSdp  * 	If it fails, recover from .backup -> foo.xml
997087719fdSdp  *
998087719fdSdp  * Rename 'foo' to 'bar':
999087719fdSdp  * 	Create tmpfile (zonecfg.xxxxxx)
1000087719fdSdp  * 	Write XML to tmpfile
1001087719fdSdp  * 	Rename tmpfile to xmlfile (zonecfg.xxxxxx -> bar.xml)
1002087719fdSdp  * 	Add entry for 'bar' to index file, Remove entry for 'foo' (refresh)
1003087719fdSdp  * 	If it fails, delete bar.xml; foo.xml is left behind.
1004087719fdSdp  */
10057c478bd9Sstevel@tonic-gate static int
10067c478bd9Sstevel@tonic-gate zonecfg_save_impl(zone_dochandle_t handle, char *filename)
10077c478bd9Sstevel@tonic-gate {
10087c478bd9Sstevel@tonic-gate 	char tmpfile[MAXPATHLEN];
1009087719fdSdp 	char bakdir[MAXPATHLEN], bakbase[MAXPATHLEN], bakfile[MAXPATHLEN];
1010087719fdSdp 	int tmpfd, err;
10117c478bd9Sstevel@tonic-gate 	xmlValidCtxt cvp = { NULL };
1012087719fdSdp 	boolean_t backup;
10137c478bd9Sstevel@tonic-gate 
10147c478bd9Sstevel@tonic-gate 	(void) strlcpy(tmpfile, filename, sizeof (tmpfile));
10157c478bd9Sstevel@tonic-gate 	(void) dirname(tmpfile);
10167c478bd9Sstevel@tonic-gate 	(void) strlcat(tmpfile, _PATH_TMPFILE, sizeof (tmpfile));
10177c478bd9Sstevel@tonic-gate 
10187c478bd9Sstevel@tonic-gate 	tmpfd = mkstemp(tmpfile);
10197c478bd9Sstevel@tonic-gate 	if (tmpfd == -1) {
10207c478bd9Sstevel@tonic-gate 		(void) unlink(tmpfile);
10217c478bd9Sstevel@tonic-gate 		return (Z_TEMP_FILE);
10227c478bd9Sstevel@tonic-gate 	}
10237c478bd9Sstevel@tonic-gate 	(void) close(tmpfd);
10247c478bd9Sstevel@tonic-gate 
10257c478bd9Sstevel@tonic-gate 	cvp.error = zonecfg_error_func;
10267c478bd9Sstevel@tonic-gate 	cvp.warning = zonecfg_error_func;
10277c478bd9Sstevel@tonic-gate 
1028087719fdSdp 	/*
1029087719fdSdp 	 * We do a final validation of the document-- but the library has
1030087719fdSdp 	 * malfunctioned if it fails to validate, so it's an assert.
1031087719fdSdp 	 */
1032087719fdSdp 	assert(xmlValidateDocument(&cvp, handle->zone_dh_doc) != 0);
10337c478bd9Sstevel@tonic-gate 
10347c478bd9Sstevel@tonic-gate 	if (xmlSaveFormatFile(tmpfile, handle->zone_dh_doc, 1) <= 0)
10357c478bd9Sstevel@tonic-gate 		goto err;
1036087719fdSdp 
10377c478bd9Sstevel@tonic-gate 	(void) chmod(tmpfile, 0644);
10387c478bd9Sstevel@tonic-gate 
1039087719fdSdp 	/*
1040087719fdSdp 	 * In the event we are doing a standard save, hard link a copy of the
1041087719fdSdp 	 * original file in .backup.<pid>.filename so we can restore it if
1042087719fdSdp 	 * something goes wrong.
1043087719fdSdp 	 */
1044087719fdSdp 	if (!is_new(handle) && !is_renaming(handle)) {
1045087719fdSdp 		backup = B_TRUE;
1046087719fdSdp 
1047087719fdSdp 		(void) strlcpy(bakdir, filename, sizeof (bakdir));
1048087719fdSdp 		(void) strlcpy(bakbase, filename, sizeof (bakbase));
1049087719fdSdp 		(void) snprintf(bakfile, sizeof (bakfile), "%s/.backup.%d.%s",
1050087719fdSdp 		    dirname(bakdir), getpid(), basename(bakbase));
1051087719fdSdp 
1052087719fdSdp 		if (link(filename, bakfile) == -1) {
1053087719fdSdp 			err = errno;
10547c478bd9Sstevel@tonic-gate 			(void) unlink(tmpfile);
10557c478bd9Sstevel@tonic-gate 			if (errno == EACCES)
10567c478bd9Sstevel@tonic-gate 				return (Z_ACCES);
10577c478bd9Sstevel@tonic-gate 			return (Z_MISC_FS);
10587c478bd9Sstevel@tonic-gate 		}
1059087719fdSdp 	}
1060a1be23daSdp 
1061087719fdSdp 	/*
1062087719fdSdp 	 * Move the new document over top of the old.
1063087719fdSdp 	 * i.e.:   zonecfg.XXXXXX  ->  myzone.xml
1064087719fdSdp 	 */
1065087719fdSdp 	if (rename(tmpfile, filename) == -1) {
1066087719fdSdp 		err = errno;
1067087719fdSdp 		(void) unlink(tmpfile);
1068087719fdSdp 		if (backup)
1069087719fdSdp 			(void) unlink(bakfile);
1070087719fdSdp 		if (err == EACCES)
1071087719fdSdp 			return (Z_ACCES);
1072087719fdSdp 		return (Z_MISC_FS);
1073087719fdSdp 	}
1074087719fdSdp 
1075087719fdSdp 	/*
1076087719fdSdp 	 * If this is a snapshot, we're done-- don't add an index entry.
1077087719fdSdp 	 */
1078087719fdSdp 	if (is_snapshot(handle))
1079087719fdSdp 		return (Z_OK);
1080087719fdSdp 
1081087719fdSdp 	/* now update the index file to reflect whatever we just did */
1082087719fdSdp 	if ((err = zonecfg_refresh_index_file(handle)) != Z_OK) {
1083087719fdSdp 		if (backup) {
1084087719fdSdp 			/*
1085087719fdSdp 			 * Try to restore from our backup.
1086087719fdSdp 			 */
1087087719fdSdp 			(void) unlink(filename);
1088087719fdSdp 			(void) rename(bakfile, filename);
1089087719fdSdp 		} else {
1090087719fdSdp 			/*
1091087719fdSdp 			 * Either the zone is new, in which case we can delete
1092087719fdSdp 			 * new.xml, or we're doing a rename, so ditto.
1093087719fdSdp 			 */
1094087719fdSdp 			assert(is_new(handle) || is_renaming(handle));
1095087719fdSdp 			(void) unlink(filename);
1096087719fdSdp 		}
1097087719fdSdp 		return (Z_UPDATING_INDEX);
1098087719fdSdp 	}
1099087719fdSdp 
1100087719fdSdp 	if (backup)
1101087719fdSdp 		(void) unlink(bakfile);
1102087719fdSdp 
1103087719fdSdp 	return (Z_OK);
11047c478bd9Sstevel@tonic-gate 
11057c478bd9Sstevel@tonic-gate err:
11067c478bd9Sstevel@tonic-gate 	(void) unlink(tmpfile);
11077c478bd9Sstevel@tonic-gate 	return (Z_SAVING_FILE);
11087c478bd9Sstevel@tonic-gate }
11097c478bd9Sstevel@tonic-gate 
11107c478bd9Sstevel@tonic-gate int
11117c478bd9Sstevel@tonic-gate zonecfg_save(zone_dochandle_t handle)
11127c478bd9Sstevel@tonic-gate {
1113087719fdSdp 	char zname[ZONENAME_MAX], path[MAXPATHLEN];
1114087719fdSdp 	char delpath[MAXPATHLEN];
1115087719fdSdp 	int err = Z_SAVING_FILE;
11167c478bd9Sstevel@tonic-gate 
1117087719fdSdp 	if (zonecfg_check_handle(handle) != Z_OK)
1118087719fdSdp 		return (Z_BAD_HANDLE);
1119087719fdSdp 
1120087719fdSdp 	/*
1121ee519a1fSgjelinek 	 * We don't support saving snapshots or a tree containing a sw
1122ee519a1fSgjelinek 	 * inventory at this time.
1123087719fdSdp 	 */
1124ee519a1fSgjelinek 	if (handle->zone_dh_snapshot || handle->zone_dh_sw_inv)
1125087719fdSdp 		return (Z_INVAL);
1126087719fdSdp 
1127087719fdSdp 	if ((err = zonecfg_get_name(handle, zname, sizeof (zname))) != Z_OK)
11287c478bd9Sstevel@tonic-gate 		return (err);
1129087719fdSdp 
1130108322fbScarlsonj 	if (!config_file_path(zname, path))
1131108322fbScarlsonj 		return (Z_MISC_FS);
1132087719fdSdp 
1133087719fdSdp 	addcomment(handle, "\n    DO NOT EDIT THIS "
1134087719fdSdp 	    "FILE.  Use zonecfg(1M) instead.\n");
1135087719fdSdp 
1136087719fdSdp 	err = zonecfg_save_impl(handle, path);
1137087719fdSdp 
1138087719fdSdp 	stripcomments(handle);
1139087719fdSdp 
1140087719fdSdp 	if (err != Z_OK)
1141087719fdSdp 		return (err);
1142087719fdSdp 
1143087719fdSdp 	handle->zone_dh_newzone = B_FALSE;
1144087719fdSdp 
1145087719fdSdp 	if (is_renaming(handle)) {
1146108322fbScarlsonj 		if (config_file_path(handle->zone_dh_delete_name, delpath))
1147087719fdSdp 			(void) unlink(delpath);
1148087719fdSdp 		handle->zone_dh_delete_name[0] = '\0';
1149087719fdSdp 	}
1150087719fdSdp 
1151087719fdSdp 	return (Z_OK);
11527c478bd9Sstevel@tonic-gate }
11537c478bd9Sstevel@tonic-gate 
1154ee519a1fSgjelinek int
11558cd327d5Sgjelinek zonecfg_detach_save(zone_dochandle_t handle, uint_t flags)
1156ee519a1fSgjelinek {
1157ee519a1fSgjelinek 	char zname[ZONENAME_MAX];
1158ee519a1fSgjelinek 	char path[MAXPATHLEN];
1159ee519a1fSgjelinek 	char migpath[MAXPATHLEN];
1160ee519a1fSgjelinek 	xmlValidCtxt cvp = { NULL };
1161ee519a1fSgjelinek 	int err = Z_SAVING_FILE;
1162ee519a1fSgjelinek 
1163ee519a1fSgjelinek 	if (zonecfg_check_handle(handle) != Z_OK)
1164ee519a1fSgjelinek 		return (Z_BAD_HANDLE);
1165ee519a1fSgjelinek 
1166ee519a1fSgjelinek 	/*
1167ee519a1fSgjelinek 	 * We can only detach if we have taken a sw inventory.
1168ee519a1fSgjelinek 	 */
1169ee519a1fSgjelinek 	if (!handle->zone_dh_sw_inv)
1170ee519a1fSgjelinek 		return (Z_INVAL);
1171ee519a1fSgjelinek 
11728cd327d5Sgjelinek 	if (flags & ZONE_DRY_RUN) {
11738cd327d5Sgjelinek 		(void) strlcpy(migpath, "-", sizeof (migpath));
11748cd327d5Sgjelinek 	} else {
11758cd327d5Sgjelinek 		if ((err = zonecfg_get_name(handle, zname, sizeof (zname)))
11768cd327d5Sgjelinek 		    != Z_OK)
1177ee519a1fSgjelinek 			return (err);
1178ee519a1fSgjelinek 
11798cd327d5Sgjelinek 		if ((err = zone_get_zonepath(zname, path, sizeof (path)))
11808cd327d5Sgjelinek 		    != Z_OK)
1181ee519a1fSgjelinek 			return (err);
1182ee519a1fSgjelinek 
11838cd327d5Sgjelinek 		if (snprintf(migpath, sizeof (migpath), "%s/%s", path, DETACHED)
11848cd327d5Sgjelinek 		    >= sizeof (migpath))
1185ee519a1fSgjelinek 			return (Z_NOMEM);
11868cd327d5Sgjelinek 	}
1187ee519a1fSgjelinek 
1188ee519a1fSgjelinek 	if ((err = operation_prep(handle)) != Z_OK)
1189ee519a1fSgjelinek 		return (err);
1190ee519a1fSgjelinek 
1191ee519a1fSgjelinek 	addcomment(handle, "\n    DO NOT EDIT THIS FILE.  "
1192ee519a1fSgjelinek 	    "Use zonecfg(1M) and zoneadm(1M) attach.\n");
1193ee519a1fSgjelinek 
1194ee519a1fSgjelinek 	cvp.error = zonecfg_error_func;
1195ee519a1fSgjelinek 	cvp.warning = zonecfg_error_func;
1196ee519a1fSgjelinek 
1197ee519a1fSgjelinek 	/*
1198ee519a1fSgjelinek 	 * We do a final validation of the document-- but the library has
1199ee519a1fSgjelinek 	 * malfunctioned if it fails to validate, so it's an assert.
1200ee519a1fSgjelinek 	 */
1201ee519a1fSgjelinek 	assert(xmlValidateDocument(&cvp, handle->zone_dh_doc) != 0);
1202ee519a1fSgjelinek 
1203ee519a1fSgjelinek 	if (xmlSaveFormatFile(migpath, handle->zone_dh_doc, 1) <= 0)
1204ee519a1fSgjelinek 		return (Z_SAVING_FILE);
1205ee519a1fSgjelinek 
12068cd327d5Sgjelinek 	if (!(flags & ZONE_DRY_RUN))
1207ee519a1fSgjelinek 		(void) chmod(migpath, 0644);
1208ee519a1fSgjelinek 
1209ee519a1fSgjelinek 	stripcomments(handle);
1210ee519a1fSgjelinek 
1211ee519a1fSgjelinek 	handle->zone_dh_newzone = B_FALSE;
1212ee519a1fSgjelinek 
1213ee519a1fSgjelinek 	return (Z_OK);
1214ee519a1fSgjelinek }
1215ee519a1fSgjelinek 
1216ee519a1fSgjelinek boolean_t
1217ee519a1fSgjelinek zonecfg_detached(const char *path)
1218ee519a1fSgjelinek {
1219ee519a1fSgjelinek 	char		migpath[MAXPATHLEN];
1220ee519a1fSgjelinek 	struct stat	buf;
1221ee519a1fSgjelinek 
1222ee519a1fSgjelinek 	if (snprintf(migpath, sizeof (migpath), "%s/%s", path, DETACHED) >=
1223ee519a1fSgjelinek 	    sizeof (migpath))
1224ee519a1fSgjelinek 		return (B_FALSE);
1225ee519a1fSgjelinek 
1226ee519a1fSgjelinek 	if (stat(migpath, &buf) != -1)
1227ee519a1fSgjelinek 		return (B_TRUE);
1228ee519a1fSgjelinek 
1229ee519a1fSgjelinek 	return (B_FALSE);
1230ee519a1fSgjelinek }
1231ee519a1fSgjelinek 
1232ee519a1fSgjelinek void
1233ee519a1fSgjelinek zonecfg_rm_detached(zone_dochandle_t handle, boolean_t forced)
1234ee519a1fSgjelinek {
1235ee519a1fSgjelinek 	char zname[ZONENAME_MAX];
1236ee519a1fSgjelinek 	char path[MAXPATHLEN];
1237ee519a1fSgjelinek 	char detached[MAXPATHLEN];
1238ee519a1fSgjelinek 	char attached[MAXPATHLEN];
1239ee519a1fSgjelinek 
1240ee519a1fSgjelinek 	if (zonecfg_check_handle(handle) != Z_OK)
1241ee519a1fSgjelinek 		return;
1242ee519a1fSgjelinek 
1243ee519a1fSgjelinek 	if (zonecfg_get_name(handle, zname, sizeof (zname)) != Z_OK)
1244ee519a1fSgjelinek 		return;
1245ee519a1fSgjelinek 
1246ee519a1fSgjelinek 	if (zone_get_zonepath(zname, path, sizeof (path)) != Z_OK)
1247ee519a1fSgjelinek 		return;
1248ee519a1fSgjelinek 
1249ee519a1fSgjelinek 	(void) snprintf(detached, sizeof (detached), "%s/%s", path, DETACHED);
1250ee519a1fSgjelinek 	(void) snprintf(attached, sizeof (attached), "%s/%s", path,
1251ee519a1fSgjelinek 	    ATTACH_FORCED);
1252ee519a1fSgjelinek 
1253ee519a1fSgjelinek 	if (forced) {
1254ee519a1fSgjelinek 		(void) rename(detached, attached);
1255ee519a1fSgjelinek 	} else {
1256ee519a1fSgjelinek 		(void) unlink(attached);
1257ee519a1fSgjelinek 		(void) unlink(detached);
1258ee519a1fSgjelinek 	}
1259ee519a1fSgjelinek }
1260ee519a1fSgjelinek 
12617c478bd9Sstevel@tonic-gate /*
12627c478bd9Sstevel@tonic-gate  * Special case: if access(2) fails with ENOENT, then try again using
12637c478bd9Sstevel@tonic-gate  * ZONE_CONFIG_ROOT instead of config_file_path(zonename).  This is how we
12647c478bd9Sstevel@tonic-gate  * work around the case of a config file which has not been created yet:
12657c478bd9Sstevel@tonic-gate  * the user will need access to the directory so use that as a heuristic.
12667c478bd9Sstevel@tonic-gate  */
12677c478bd9Sstevel@tonic-gate 
12687c478bd9Sstevel@tonic-gate int
12697c478bd9Sstevel@tonic-gate zonecfg_access(const char *zonename, int amode)
12707c478bd9Sstevel@tonic-gate {
12717c478bd9Sstevel@tonic-gate 	char path[MAXPATHLEN];
12727c478bd9Sstevel@tonic-gate 
1273108322fbScarlsonj 	if (!config_file_path(zonename, path))
1274108322fbScarlsonj 		return (Z_INVAL);
12757c478bd9Sstevel@tonic-gate 	if (access(path, amode) == 0)
12767c478bd9Sstevel@tonic-gate 		return (Z_OK);
1277108322fbScarlsonj 	if (errno == ENOENT) {
1278108322fbScarlsonj 		if (snprintf(path, sizeof (path), "%s%s", zonecfg_root,
1279108322fbScarlsonj 		    ZONE_CONFIG_ROOT) >= sizeof (path))
1280108322fbScarlsonj 			return (Z_INVAL);
1281108322fbScarlsonj 		if (access(path, amode) == 0)
12827c478bd9Sstevel@tonic-gate 			return (Z_OK);
1283108322fbScarlsonj 	}
12847c478bd9Sstevel@tonic-gate 	if (errno == EACCES)
12857c478bd9Sstevel@tonic-gate 		return (Z_ACCES);
12867c478bd9Sstevel@tonic-gate 	if (errno == EINVAL)
12877c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
12887c478bd9Sstevel@tonic-gate 	return (Z_MISC_FS);
12897c478bd9Sstevel@tonic-gate }
12907c478bd9Sstevel@tonic-gate 
12917c478bd9Sstevel@tonic-gate int
1292108322fbScarlsonj zonecfg_create_snapshot(const char *zonename)
12937c478bd9Sstevel@tonic-gate {
12947c478bd9Sstevel@tonic-gate 	zone_dochandle_t handle;
12957c478bd9Sstevel@tonic-gate 	char path[MAXPATHLEN], zonepath[MAXPATHLEN], rpath[MAXPATHLEN];
12967c478bd9Sstevel@tonic-gate 	int error = Z_OK, res;
12977c478bd9Sstevel@tonic-gate 
12987c478bd9Sstevel@tonic-gate 	if ((handle = zonecfg_init_handle()) == NULL) {
12997c478bd9Sstevel@tonic-gate 		return (Z_NOMEM);
13007c478bd9Sstevel@tonic-gate 	}
13017c478bd9Sstevel@tonic-gate 
1302087719fdSdp 	handle->zone_dh_newzone = B_TRUE;
1303087719fdSdp 	handle->zone_dh_snapshot = B_TRUE;
1304087719fdSdp 
13057c478bd9Sstevel@tonic-gate 	if ((error = zonecfg_get_handle(zonename, handle)) != Z_OK)
13067c478bd9Sstevel@tonic-gate 		goto out;
13077c478bd9Sstevel@tonic-gate 	if ((error = operation_prep(handle)) != Z_OK)
13087c478bd9Sstevel@tonic-gate 		goto out;
13097c478bd9Sstevel@tonic-gate 	error = zonecfg_get_zonepath(handle, zonepath, sizeof (zonepath));
13107c478bd9Sstevel@tonic-gate 	if (error != Z_OK)
13117c478bd9Sstevel@tonic-gate 		goto out;
13127c478bd9Sstevel@tonic-gate 	if ((res = resolvepath(zonepath, rpath, sizeof (rpath))) == -1) {
13137c478bd9Sstevel@tonic-gate 		error = Z_RESOLVED_PATH;
13147c478bd9Sstevel@tonic-gate 		goto out;
13157c478bd9Sstevel@tonic-gate 	}
13167c478bd9Sstevel@tonic-gate 	/*
13177c478bd9Sstevel@tonic-gate 	 * If the resolved path is not the same as the original path, then
13187c478bd9Sstevel@tonic-gate 	 * save the resolved path in the snapshot, thus preventing any
13197c478bd9Sstevel@tonic-gate 	 * potential problems down the line when zoneadmd goes to unmount
13207c478bd9Sstevel@tonic-gate 	 * file systems and depends on initial string matches with resolved
13217c478bd9Sstevel@tonic-gate 	 * paths.
13227c478bd9Sstevel@tonic-gate 	 */
13237c478bd9Sstevel@tonic-gate 	rpath[res] = '\0';
13247c478bd9Sstevel@tonic-gate 	if (strcmp(zonepath, rpath) != 0) {
13257c478bd9Sstevel@tonic-gate 		if ((error = zonecfg_set_zonepath(handle, rpath)) != Z_OK)
13267c478bd9Sstevel@tonic-gate 			goto out;
13277c478bd9Sstevel@tonic-gate 	}
1328108322fbScarlsonj 	if (snprintf(path, sizeof (path), "%s%s", zonecfg_root,
1329108322fbScarlsonj 	    ZONE_SNAPSHOT_ROOT) >= sizeof (path)) {
1330108322fbScarlsonj 		error = Z_MISC_FS;
1331108322fbScarlsonj 		goto out;
1332108322fbScarlsonj 	}
1333108322fbScarlsonj 	if ((mkdir(path, S_IRWXU) == -1) && (errno != EEXIST)) {
13347c478bd9Sstevel@tonic-gate 		error = Z_MISC_FS;
13357c478bd9Sstevel@tonic-gate 		goto out;
13367c478bd9Sstevel@tonic-gate 	}
13377c478bd9Sstevel@tonic-gate 
1338108322fbScarlsonj 	if (!snap_file_path(zonename, path)) {
1339108322fbScarlsonj 		error = Z_MISC_FS;
1340108322fbScarlsonj 		goto out;
1341108322fbScarlsonj 	}
1342087719fdSdp 
1343087719fdSdp 	addcomment(handle, "\n    DO NOT EDIT THIS FILE.  "
1344087719fdSdp 	    "It is a snapshot of running zone state.\n");
1345087719fdSdp 
13467c478bd9Sstevel@tonic-gate 	error = zonecfg_save_impl(handle, path);
13477c478bd9Sstevel@tonic-gate 
1348087719fdSdp 	stripcomments(handle);
1349087719fdSdp 
13507c478bd9Sstevel@tonic-gate out:
13517c478bd9Sstevel@tonic-gate 	zonecfg_fini_handle(handle);
13527c478bd9Sstevel@tonic-gate 	return (error);
13537c478bd9Sstevel@tonic-gate }
13547c478bd9Sstevel@tonic-gate 
13557c478bd9Sstevel@tonic-gate static int
1356a1be23daSdp newprop(xmlNodePtr node, const xmlChar *attrname, char *src)
13577c478bd9Sstevel@tonic-gate {
13587c478bd9Sstevel@tonic-gate 	xmlAttrPtr newattr;
13597c478bd9Sstevel@tonic-gate 
13607c478bd9Sstevel@tonic-gate 	newattr = xmlNewProp(node, attrname, (xmlChar *)src);
13617c478bd9Sstevel@tonic-gate 	if (newattr == NULL) {
13627c478bd9Sstevel@tonic-gate 		xmlUnlinkNode(node);
13637c478bd9Sstevel@tonic-gate 		xmlFreeNode(node);
13647c478bd9Sstevel@tonic-gate 		return (Z_BAD_PROPERTY);
13657c478bd9Sstevel@tonic-gate 	}
13667c478bd9Sstevel@tonic-gate 	return (Z_OK);
13677c478bd9Sstevel@tonic-gate }
13687c478bd9Sstevel@tonic-gate 
13697c478bd9Sstevel@tonic-gate static int
13707c478bd9Sstevel@tonic-gate zonecfg_add_filesystem_core(zone_dochandle_t handle, struct zone_fstab *tabptr)
13717c478bd9Sstevel@tonic-gate {
13727c478bd9Sstevel@tonic-gate 	xmlNodePtr newnode, cur = handle->zone_dh_cur, options_node;
13737c478bd9Sstevel@tonic-gate 	zone_fsopt_t *ptr;
13747c478bd9Sstevel@tonic-gate 	int err;
13757c478bd9Sstevel@tonic-gate 
13767c478bd9Sstevel@tonic-gate 	newnode = xmlNewTextChild(cur, NULL, DTD_ELEM_FS, NULL);
1377a1be23daSdp 	if ((err = newprop(newnode, DTD_ATTR_SPECIAL,
13787c478bd9Sstevel@tonic-gate 	    tabptr->zone_fs_special)) != Z_OK)
13797c478bd9Sstevel@tonic-gate 		return (err);
13807c478bd9Sstevel@tonic-gate 	if (tabptr->zone_fs_raw[0] != '\0' &&
1381a1be23daSdp 	    (err = newprop(newnode, DTD_ATTR_RAW, tabptr->zone_fs_raw)) != Z_OK)
13827c478bd9Sstevel@tonic-gate 		return (err);
1383a1be23daSdp 	if ((err = newprop(newnode, DTD_ATTR_DIR, tabptr->zone_fs_dir)) != Z_OK)
13847c478bd9Sstevel@tonic-gate 		return (err);
1385a1be23daSdp 	if ((err = newprop(newnode, DTD_ATTR_TYPE,
13867c478bd9Sstevel@tonic-gate 	    tabptr->zone_fs_type)) != Z_OK)
13877c478bd9Sstevel@tonic-gate 		return (err);
13887c478bd9Sstevel@tonic-gate 	if (tabptr->zone_fs_options != NULL) {
13897c478bd9Sstevel@tonic-gate 		for (ptr = tabptr->zone_fs_options; ptr != NULL;
13907c478bd9Sstevel@tonic-gate 		    ptr = ptr->zone_fsopt_next) {
13917c478bd9Sstevel@tonic-gate 			options_node = xmlNewTextChild(newnode, NULL,
13927c478bd9Sstevel@tonic-gate 			    DTD_ELEM_FSOPTION, NULL);
1393a1be23daSdp 			if ((err = newprop(options_node, DTD_ATTR_NAME,
13947c478bd9Sstevel@tonic-gate 			    ptr->zone_fsopt_opt)) != Z_OK)
13957c478bd9Sstevel@tonic-gate 				return (err);
13967c478bd9Sstevel@tonic-gate 		}
13977c478bd9Sstevel@tonic-gate 	}
13987c478bd9Sstevel@tonic-gate 	return (Z_OK);
13997c478bd9Sstevel@tonic-gate }
14007c478bd9Sstevel@tonic-gate 
14017c478bd9Sstevel@tonic-gate int
14027c478bd9Sstevel@tonic-gate zonecfg_add_filesystem(zone_dochandle_t handle, struct zone_fstab *tabptr)
14037c478bd9Sstevel@tonic-gate {
14047c478bd9Sstevel@tonic-gate 	int err;
14057c478bd9Sstevel@tonic-gate 
14067c478bd9Sstevel@tonic-gate 	if (tabptr == NULL)
14077c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
14087c478bd9Sstevel@tonic-gate 
14097c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
14107c478bd9Sstevel@tonic-gate 		return (err);
14117c478bd9Sstevel@tonic-gate 
14127c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_add_filesystem_core(handle, tabptr)) != Z_OK)
14137c478bd9Sstevel@tonic-gate 		return (err);
14147c478bd9Sstevel@tonic-gate 
14157c478bd9Sstevel@tonic-gate 	return (Z_OK);
14167c478bd9Sstevel@tonic-gate }
14177c478bd9Sstevel@tonic-gate 
14187c478bd9Sstevel@tonic-gate static int
14197c478bd9Sstevel@tonic-gate zonecfg_add_ipd_core(zone_dochandle_t handle, struct zone_fstab *tabptr)
14207c478bd9Sstevel@tonic-gate {
14217c478bd9Sstevel@tonic-gate 	xmlNodePtr newnode, cur = handle->zone_dh_cur;
14227c478bd9Sstevel@tonic-gate 	int err;
14237c478bd9Sstevel@tonic-gate 
14247c478bd9Sstevel@tonic-gate 	newnode = xmlNewTextChild(cur, NULL, DTD_ELEM_IPD, NULL);
1425a1be23daSdp 	if ((err = newprop(newnode, DTD_ATTR_DIR, tabptr->zone_fs_dir)) != Z_OK)
14267c478bd9Sstevel@tonic-gate 		return (err);
14277c478bd9Sstevel@tonic-gate 	return (Z_OK);
14287c478bd9Sstevel@tonic-gate }
14297c478bd9Sstevel@tonic-gate 
14307c478bd9Sstevel@tonic-gate int
14317c478bd9Sstevel@tonic-gate zonecfg_add_ipd(zone_dochandle_t handle, struct zone_fstab *tabptr)
14327c478bd9Sstevel@tonic-gate {
14337c478bd9Sstevel@tonic-gate 	int err;
14347c478bd9Sstevel@tonic-gate 
14357c478bd9Sstevel@tonic-gate 	if (tabptr == NULL)
14367c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
14377c478bd9Sstevel@tonic-gate 
14387c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
14397c478bd9Sstevel@tonic-gate 		return (err);
14407c478bd9Sstevel@tonic-gate 
14417c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_add_ipd_core(handle, tabptr)) != Z_OK)
14427c478bd9Sstevel@tonic-gate 		return (err);
14437c478bd9Sstevel@tonic-gate 
14447c478bd9Sstevel@tonic-gate 	return (Z_OK);
14457c478bd9Sstevel@tonic-gate }
14467c478bd9Sstevel@tonic-gate 
14477c478bd9Sstevel@tonic-gate int
14487c478bd9Sstevel@tonic-gate zonecfg_add_fs_option(struct zone_fstab *tabptr, char *option)
14497c478bd9Sstevel@tonic-gate {
14507c478bd9Sstevel@tonic-gate 	zone_fsopt_t *last, *old, *new;
14517c478bd9Sstevel@tonic-gate 
14527c478bd9Sstevel@tonic-gate 	last = tabptr->zone_fs_options;
14537c478bd9Sstevel@tonic-gate 	for (old = last; old != NULL; old = old->zone_fsopt_next)
14547c478bd9Sstevel@tonic-gate 		last = old;	/* walk to the end of the list */
14557c478bd9Sstevel@tonic-gate 	new = (zone_fsopt_t *)malloc(sizeof (zone_fsopt_t));
14567c478bd9Sstevel@tonic-gate 	if (new == NULL)
14577c478bd9Sstevel@tonic-gate 		return (Z_NOMEM);
14587c478bd9Sstevel@tonic-gate 	(void) strlcpy(new->zone_fsopt_opt, option,
14597c478bd9Sstevel@tonic-gate 	    sizeof (new->zone_fsopt_opt));
14607c478bd9Sstevel@tonic-gate 	new->zone_fsopt_next = NULL;
14617c478bd9Sstevel@tonic-gate 	if (last == NULL)
14627c478bd9Sstevel@tonic-gate 		tabptr->zone_fs_options = new;
14637c478bd9Sstevel@tonic-gate 	else
14647c478bd9Sstevel@tonic-gate 		last->zone_fsopt_next = new;
14657c478bd9Sstevel@tonic-gate 	return (Z_OK);
14667c478bd9Sstevel@tonic-gate }
14677c478bd9Sstevel@tonic-gate 
14687c478bd9Sstevel@tonic-gate int
14697c478bd9Sstevel@tonic-gate zonecfg_remove_fs_option(struct zone_fstab *tabptr, char *option)
14707c478bd9Sstevel@tonic-gate {
14717c478bd9Sstevel@tonic-gate 	zone_fsopt_t *last, *this, *next;
14727c478bd9Sstevel@tonic-gate 
14737c478bd9Sstevel@tonic-gate 	last = tabptr->zone_fs_options;
14747c478bd9Sstevel@tonic-gate 	for (this = last; this != NULL; this = this->zone_fsopt_next) {
14757c478bd9Sstevel@tonic-gate 		if (strcmp(this->zone_fsopt_opt, option) == 0) {
14767c478bd9Sstevel@tonic-gate 			next = this->zone_fsopt_next;
14777c478bd9Sstevel@tonic-gate 			if (this == tabptr->zone_fs_options)
14787c478bd9Sstevel@tonic-gate 				tabptr->zone_fs_options = next;
14797c478bd9Sstevel@tonic-gate 			else
14807c478bd9Sstevel@tonic-gate 				last->zone_fsopt_next = next;
14817c478bd9Sstevel@tonic-gate 			free(this);
14827c478bd9Sstevel@tonic-gate 			return (Z_OK);
14837c478bd9Sstevel@tonic-gate 		} else
14847c478bd9Sstevel@tonic-gate 			last = this;
14857c478bd9Sstevel@tonic-gate 	}
14867c478bd9Sstevel@tonic-gate 	return (Z_NO_PROPERTY_ID);
14877c478bd9Sstevel@tonic-gate }
14887c478bd9Sstevel@tonic-gate 
14897c478bd9Sstevel@tonic-gate void
14907c478bd9Sstevel@tonic-gate zonecfg_free_fs_option_list(zone_fsopt_t *list)
14917c478bd9Sstevel@tonic-gate {
14927c478bd9Sstevel@tonic-gate 	zone_fsopt_t *this, *next;
14937c478bd9Sstevel@tonic-gate 
14947c478bd9Sstevel@tonic-gate 	for (this = list; this != NULL; this = next) {
14957c478bd9Sstevel@tonic-gate 		next = this->zone_fsopt_next;
14967c478bd9Sstevel@tonic-gate 		free(this);
14977c478bd9Sstevel@tonic-gate 	}
14987c478bd9Sstevel@tonic-gate }
14997c478bd9Sstevel@tonic-gate 
15007c478bd9Sstevel@tonic-gate void
15017c478bd9Sstevel@tonic-gate zonecfg_free_rctl_value_list(struct zone_rctlvaltab *valtab)
15027c478bd9Sstevel@tonic-gate {
15037c478bd9Sstevel@tonic-gate 	if (valtab == NULL)
15047c478bd9Sstevel@tonic-gate 		return;
15057c478bd9Sstevel@tonic-gate 	zonecfg_free_rctl_value_list(valtab->zone_rctlval_next);
15067c478bd9Sstevel@tonic-gate 	free(valtab);
15077c478bd9Sstevel@tonic-gate }
15087c478bd9Sstevel@tonic-gate 
15097c478bd9Sstevel@tonic-gate static boolean_t
15107c478bd9Sstevel@tonic-gate match_prop(xmlNodePtr cur, const xmlChar *attr, char *user_prop)
15117c478bd9Sstevel@tonic-gate {
15127c478bd9Sstevel@tonic-gate 	xmlChar *gotten_prop;
15137c478bd9Sstevel@tonic-gate 	int prop_result;
15147c478bd9Sstevel@tonic-gate 
15157c478bd9Sstevel@tonic-gate 	gotten_prop = xmlGetProp(cur, attr);
15167c478bd9Sstevel@tonic-gate 	if (gotten_prop == NULL)	/* shouldn't happen */
15177c478bd9Sstevel@tonic-gate 		return (B_FALSE);
15187c478bd9Sstevel@tonic-gate 	prop_result = xmlStrcmp(gotten_prop, (const xmlChar *) user_prop);
15197c478bd9Sstevel@tonic-gate 	xmlFree(gotten_prop);
15207c478bd9Sstevel@tonic-gate 	return ((prop_result == 0));
15217c478bd9Sstevel@tonic-gate }
15227c478bd9Sstevel@tonic-gate 
15237c478bd9Sstevel@tonic-gate static int
15247c478bd9Sstevel@tonic-gate zonecfg_delete_filesystem_core(zone_dochandle_t handle,
15257c478bd9Sstevel@tonic-gate     struct zone_fstab *tabptr)
15267c478bd9Sstevel@tonic-gate {
15277c478bd9Sstevel@tonic-gate 	xmlNodePtr cur = handle->zone_dh_cur;
15287c478bd9Sstevel@tonic-gate 	boolean_t dir_match, spec_match, raw_match, type_match;
15297c478bd9Sstevel@tonic-gate 
15307c478bd9Sstevel@tonic-gate 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
15317c478bd9Sstevel@tonic-gate 		if (xmlStrcmp(cur->name, DTD_ELEM_FS))
15327c478bd9Sstevel@tonic-gate 			continue;
15337c478bd9Sstevel@tonic-gate 		dir_match = match_prop(cur, DTD_ATTR_DIR, tabptr->zone_fs_dir);
15347c478bd9Sstevel@tonic-gate 		spec_match = match_prop(cur, DTD_ATTR_SPECIAL,
15357c478bd9Sstevel@tonic-gate 		    tabptr->zone_fs_special);
15367c478bd9Sstevel@tonic-gate 		raw_match = match_prop(cur, DTD_ATTR_RAW,
15377c478bd9Sstevel@tonic-gate 		    tabptr->zone_fs_raw);
15387c478bd9Sstevel@tonic-gate 		type_match = match_prop(cur, DTD_ATTR_TYPE,
15397c478bd9Sstevel@tonic-gate 		    tabptr->zone_fs_type);
15407c478bd9Sstevel@tonic-gate 		if (dir_match && spec_match && raw_match && type_match) {
15417c478bd9Sstevel@tonic-gate 			xmlUnlinkNode(cur);
15427c478bd9Sstevel@tonic-gate 			xmlFreeNode(cur);
15437c478bd9Sstevel@tonic-gate 			return (Z_OK);
15447c478bd9Sstevel@tonic-gate 		}
15457c478bd9Sstevel@tonic-gate 	}
15467c478bd9Sstevel@tonic-gate 	return (Z_NO_RESOURCE_ID);
15477c478bd9Sstevel@tonic-gate }
15487c478bd9Sstevel@tonic-gate 
15497c478bd9Sstevel@tonic-gate int
15507c478bd9Sstevel@tonic-gate zonecfg_delete_filesystem(zone_dochandle_t handle, struct zone_fstab *tabptr)
15517c478bd9Sstevel@tonic-gate {
15527c478bd9Sstevel@tonic-gate 	int err;
15537c478bd9Sstevel@tonic-gate 
15547c478bd9Sstevel@tonic-gate 	if (tabptr == NULL)
15557c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
15567c478bd9Sstevel@tonic-gate 
15577c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
15587c478bd9Sstevel@tonic-gate 		return (err);
15597c478bd9Sstevel@tonic-gate 
15607c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_delete_filesystem_core(handle, tabptr)) != Z_OK)
15617c478bd9Sstevel@tonic-gate 		return (err);
15627c478bd9Sstevel@tonic-gate 
15637c478bd9Sstevel@tonic-gate 	return (Z_OK);
15647c478bd9Sstevel@tonic-gate }
15657c478bd9Sstevel@tonic-gate 
15667c478bd9Sstevel@tonic-gate int
15677c478bd9Sstevel@tonic-gate zonecfg_modify_filesystem(
15687c478bd9Sstevel@tonic-gate 	zone_dochandle_t handle,
15697c478bd9Sstevel@tonic-gate 	struct zone_fstab *oldtabptr,
15707c478bd9Sstevel@tonic-gate 	struct zone_fstab *newtabptr)
15717c478bd9Sstevel@tonic-gate {
15727c478bd9Sstevel@tonic-gate 	int err;
15737c478bd9Sstevel@tonic-gate 
15747c478bd9Sstevel@tonic-gate 	if (oldtabptr == NULL || newtabptr == NULL)
15757c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
15767c478bd9Sstevel@tonic-gate 
15777c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
15787c478bd9Sstevel@tonic-gate 		return (err);
15797c478bd9Sstevel@tonic-gate 
15807c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_delete_filesystem_core(handle, oldtabptr)) != Z_OK)
15817c478bd9Sstevel@tonic-gate 		return (err);
15827c478bd9Sstevel@tonic-gate 
15837c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_add_filesystem_core(handle, newtabptr)) != Z_OK)
15847c478bd9Sstevel@tonic-gate 		return (err);
15857c478bd9Sstevel@tonic-gate 
15867c478bd9Sstevel@tonic-gate 	return (Z_OK);
15877c478bd9Sstevel@tonic-gate }
15887c478bd9Sstevel@tonic-gate 
15897c478bd9Sstevel@tonic-gate static int
15907c478bd9Sstevel@tonic-gate zonecfg_delete_ipd_core(zone_dochandle_t handle, struct zone_fstab *tabptr)
15917c478bd9Sstevel@tonic-gate {
15927c478bd9Sstevel@tonic-gate 	xmlNodePtr cur = handle->zone_dh_cur;
15937c478bd9Sstevel@tonic-gate 
15947c478bd9Sstevel@tonic-gate 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
15957c478bd9Sstevel@tonic-gate 		if (xmlStrcmp(cur->name, DTD_ELEM_IPD))
15967c478bd9Sstevel@tonic-gate 			continue;
15977c478bd9Sstevel@tonic-gate 		if (match_prop(cur, DTD_ATTR_DIR, tabptr->zone_fs_dir)) {
15987c478bd9Sstevel@tonic-gate 			xmlUnlinkNode(cur);
15997c478bd9Sstevel@tonic-gate 			xmlFreeNode(cur);
16007c478bd9Sstevel@tonic-gate 			return (Z_OK);
16017c478bd9Sstevel@tonic-gate 		}
16027c478bd9Sstevel@tonic-gate 	}
16037c478bd9Sstevel@tonic-gate 	return (Z_NO_RESOURCE_ID);
16047c478bd9Sstevel@tonic-gate }
16057c478bd9Sstevel@tonic-gate 
16067c478bd9Sstevel@tonic-gate int
16077c478bd9Sstevel@tonic-gate zonecfg_delete_ipd(zone_dochandle_t handle, struct zone_fstab *tabptr)
16087c478bd9Sstevel@tonic-gate {
16097c478bd9Sstevel@tonic-gate 	int err;
16107c478bd9Sstevel@tonic-gate 
16117c478bd9Sstevel@tonic-gate 	if (tabptr == NULL)
16127c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
16137c478bd9Sstevel@tonic-gate 
16147c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
16157c478bd9Sstevel@tonic-gate 		return (err);
16167c478bd9Sstevel@tonic-gate 
16177c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_delete_ipd_core(handle, tabptr)) != Z_OK)
16187c478bd9Sstevel@tonic-gate 		return (err);
16197c478bd9Sstevel@tonic-gate 
16207c478bd9Sstevel@tonic-gate 	return (Z_OK);
16217c478bd9Sstevel@tonic-gate }
16227c478bd9Sstevel@tonic-gate 
16237c478bd9Sstevel@tonic-gate int
16247c478bd9Sstevel@tonic-gate zonecfg_modify_ipd(zone_dochandle_t handle, struct zone_fstab *oldtabptr,
16257c478bd9Sstevel@tonic-gate     struct zone_fstab *newtabptr)
16267c478bd9Sstevel@tonic-gate {
16277c478bd9Sstevel@tonic-gate 	int err;
16287c478bd9Sstevel@tonic-gate 
16297c478bd9Sstevel@tonic-gate 	if (oldtabptr == NULL || newtabptr == NULL)
16307c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
16317c478bd9Sstevel@tonic-gate 
16327c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
16337c478bd9Sstevel@tonic-gate 		return (err);
16347c478bd9Sstevel@tonic-gate 
16357c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_delete_ipd_core(handle, oldtabptr)) != Z_OK)
16367c478bd9Sstevel@tonic-gate 		return (err);
16377c478bd9Sstevel@tonic-gate 
16387c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_add_ipd_core(handle, newtabptr)) != Z_OK)
16397c478bd9Sstevel@tonic-gate 		return (err);
16407c478bd9Sstevel@tonic-gate 
16417c478bd9Sstevel@tonic-gate 	return (Z_OK);
16427c478bd9Sstevel@tonic-gate }
16437c478bd9Sstevel@tonic-gate 
16447c478bd9Sstevel@tonic-gate int
16457c478bd9Sstevel@tonic-gate zonecfg_lookup_filesystem(
16467c478bd9Sstevel@tonic-gate 	zone_dochandle_t handle,
16477c478bd9Sstevel@tonic-gate 	struct zone_fstab *tabptr)
16487c478bd9Sstevel@tonic-gate {
16497c478bd9Sstevel@tonic-gate 	xmlNodePtr cur, options, firstmatch;
16507c478bd9Sstevel@tonic-gate 	int err;
16517c478bd9Sstevel@tonic-gate 	char dirname[MAXPATHLEN], special[MAXPATHLEN], raw[MAXPATHLEN];
16527c478bd9Sstevel@tonic-gate 	char type[FSTYPSZ];
16537c478bd9Sstevel@tonic-gate 	char options_str[MAX_MNTOPT_STR];
16547c478bd9Sstevel@tonic-gate 
16557c478bd9Sstevel@tonic-gate 	if (tabptr == NULL)
16567c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
16577c478bd9Sstevel@tonic-gate 
16587c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
16597c478bd9Sstevel@tonic-gate 		return (err);
16607c478bd9Sstevel@tonic-gate 
16617c478bd9Sstevel@tonic-gate 	/*
16627c478bd9Sstevel@tonic-gate 	 * Walk the list of children looking for matches on any properties
16637c478bd9Sstevel@tonic-gate 	 * specified in the fstab parameter.  If more than one resource
16647c478bd9Sstevel@tonic-gate 	 * matches, we return Z_INSUFFICIENT_SPEC; if none match, we return
16657c478bd9Sstevel@tonic-gate 	 * Z_NO_RESOURCE_ID.
16667c478bd9Sstevel@tonic-gate 	 */
16677c478bd9Sstevel@tonic-gate 	cur = handle->zone_dh_cur;
16687c478bd9Sstevel@tonic-gate 	firstmatch = NULL;
16697c478bd9Sstevel@tonic-gate 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
16707c478bd9Sstevel@tonic-gate 		if (xmlStrcmp(cur->name, DTD_ELEM_FS))
16717c478bd9Sstevel@tonic-gate 			continue;
16727c478bd9Sstevel@tonic-gate 		if (strlen(tabptr->zone_fs_dir) > 0) {
16737c478bd9Sstevel@tonic-gate 			if ((fetchprop(cur, DTD_ATTR_DIR, dirname,
16747c478bd9Sstevel@tonic-gate 			    sizeof (dirname)) == Z_OK) &&
16757c478bd9Sstevel@tonic-gate 			    (strcmp(tabptr->zone_fs_dir, dirname) == 0)) {
16767c478bd9Sstevel@tonic-gate 				if (firstmatch == NULL)
16777c478bd9Sstevel@tonic-gate 					firstmatch = cur;
16787c478bd9Sstevel@tonic-gate 				else
16797c478bd9Sstevel@tonic-gate 					return (Z_INSUFFICIENT_SPEC);
16807c478bd9Sstevel@tonic-gate 			}
16817c478bd9Sstevel@tonic-gate 		}
16827c478bd9Sstevel@tonic-gate 		if (strlen(tabptr->zone_fs_special) > 0) {
16837c478bd9Sstevel@tonic-gate 			if ((fetchprop(cur, DTD_ATTR_SPECIAL, special,
16847c478bd9Sstevel@tonic-gate 			    sizeof (special)) == Z_OK)) {
16857c478bd9Sstevel@tonic-gate 				if (strcmp(tabptr->zone_fs_special,
16867c478bd9Sstevel@tonic-gate 				    special) == 0) {
16877c478bd9Sstevel@tonic-gate 					if (firstmatch == NULL)
16887c478bd9Sstevel@tonic-gate 						firstmatch = cur;
16897c478bd9Sstevel@tonic-gate 					else if (firstmatch != cur)
16907c478bd9Sstevel@tonic-gate 						return (Z_INSUFFICIENT_SPEC);
16917c478bd9Sstevel@tonic-gate 				} else {
16927c478bd9Sstevel@tonic-gate 					/*
16937c478bd9Sstevel@tonic-gate 					 * If another property matched but this
16947c478bd9Sstevel@tonic-gate 					 * one doesn't then reset firstmatch.
16957c478bd9Sstevel@tonic-gate 					 */
16967c478bd9Sstevel@tonic-gate 					if (firstmatch == cur)
16977c478bd9Sstevel@tonic-gate 						firstmatch = NULL;
16987c478bd9Sstevel@tonic-gate 				}
16997c478bd9Sstevel@tonic-gate 			}
17007c478bd9Sstevel@tonic-gate 		}
17017c478bd9Sstevel@tonic-gate 		if (strlen(tabptr->zone_fs_raw) > 0) {
17027c478bd9Sstevel@tonic-gate 			if ((fetchprop(cur, DTD_ATTR_RAW, raw,
17037c478bd9Sstevel@tonic-gate 			    sizeof (raw)) == Z_OK)) {
17047c478bd9Sstevel@tonic-gate 				if (strcmp(tabptr->zone_fs_raw, raw) == 0) {
17057c478bd9Sstevel@tonic-gate 					if (firstmatch == NULL)
17067c478bd9Sstevel@tonic-gate 						firstmatch = cur;
17077c478bd9Sstevel@tonic-gate 					else if (firstmatch != cur)
17087c478bd9Sstevel@tonic-gate 						return (Z_INSUFFICIENT_SPEC);
17097c478bd9Sstevel@tonic-gate 				} else {
17107c478bd9Sstevel@tonic-gate 					/*
17117c478bd9Sstevel@tonic-gate 					 * If another property matched but this
17127c478bd9Sstevel@tonic-gate 					 * one doesn't then reset firstmatch.
17137c478bd9Sstevel@tonic-gate 					 */
17147c478bd9Sstevel@tonic-gate 					if (firstmatch == cur)
17157c478bd9Sstevel@tonic-gate 						firstmatch = NULL;
17167c478bd9Sstevel@tonic-gate 				}
17177c478bd9Sstevel@tonic-gate 			}
17187c478bd9Sstevel@tonic-gate 		}
17197c478bd9Sstevel@tonic-gate 		if (strlen(tabptr->zone_fs_type) > 0) {
17207c478bd9Sstevel@tonic-gate 			if ((fetchprop(cur, DTD_ATTR_TYPE, type,
17217c478bd9Sstevel@tonic-gate 			    sizeof (type)) == Z_OK)) {
17227c478bd9Sstevel@tonic-gate 				if (strcmp(tabptr->zone_fs_type, type) == 0) {
17237c478bd9Sstevel@tonic-gate 					if (firstmatch == NULL)
17247c478bd9Sstevel@tonic-gate 						firstmatch = cur;
17257c478bd9Sstevel@tonic-gate 					else if (firstmatch != cur)
17267c478bd9Sstevel@tonic-gate 						return (Z_INSUFFICIENT_SPEC);
17277c478bd9Sstevel@tonic-gate 				} else {
17287c478bd9Sstevel@tonic-gate 					/*
17297c478bd9Sstevel@tonic-gate 					 * If another property matched but this
17307c478bd9Sstevel@tonic-gate 					 * one doesn't then reset firstmatch.
17317c478bd9Sstevel@tonic-gate 					 */
17327c478bd9Sstevel@tonic-gate 					if (firstmatch == cur)
17337c478bd9Sstevel@tonic-gate 						firstmatch = NULL;
17347c478bd9Sstevel@tonic-gate 				}
17357c478bd9Sstevel@tonic-gate 			}
17367c478bd9Sstevel@tonic-gate 		}
17377c478bd9Sstevel@tonic-gate 	}
17387c478bd9Sstevel@tonic-gate 
17397c478bd9Sstevel@tonic-gate 	if (firstmatch == NULL)
17407c478bd9Sstevel@tonic-gate 		return (Z_NO_RESOURCE_ID);
17417c478bd9Sstevel@tonic-gate 
17427c478bd9Sstevel@tonic-gate 	cur = firstmatch;
17437c478bd9Sstevel@tonic-gate 
17447c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_DIR, tabptr->zone_fs_dir,
17457c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_fs_dir))) != Z_OK)
17467c478bd9Sstevel@tonic-gate 		return (err);
17477c478bd9Sstevel@tonic-gate 
17487c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_SPECIAL, tabptr->zone_fs_special,
17497c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_fs_special))) != Z_OK)
17507c478bd9Sstevel@tonic-gate 		return (err);
17517c478bd9Sstevel@tonic-gate 
17527c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_RAW, tabptr->zone_fs_raw,
17537c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_fs_raw))) != Z_OK)
17547c478bd9Sstevel@tonic-gate 		return (err);
17557c478bd9Sstevel@tonic-gate 
17567c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_TYPE, tabptr->zone_fs_type,
17577c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_fs_type))) != Z_OK)
17587c478bd9Sstevel@tonic-gate 		return (err);
17597c478bd9Sstevel@tonic-gate 
17607c478bd9Sstevel@tonic-gate 	/* options are optional */
17617c478bd9Sstevel@tonic-gate 	tabptr->zone_fs_options = NULL;
17627c478bd9Sstevel@tonic-gate 	for (options = cur->xmlChildrenNode; options != NULL;
17637c478bd9Sstevel@tonic-gate 	    options = options->next) {
17647c478bd9Sstevel@tonic-gate 		if ((fetchprop(options, DTD_ATTR_NAME, options_str,
17657c478bd9Sstevel@tonic-gate 		    sizeof (options_str)) != Z_OK))
17667c478bd9Sstevel@tonic-gate 			break;
17677c478bd9Sstevel@tonic-gate 		if (zonecfg_add_fs_option(tabptr, options_str) != Z_OK)
17687c478bd9Sstevel@tonic-gate 			break;
17697c478bd9Sstevel@tonic-gate 	}
17707c478bd9Sstevel@tonic-gate 	return (Z_OK);
17717c478bd9Sstevel@tonic-gate }
17727c478bd9Sstevel@tonic-gate 
17737c478bd9Sstevel@tonic-gate int
17747c478bd9Sstevel@tonic-gate zonecfg_lookup_ipd(zone_dochandle_t handle, struct zone_fstab *tabptr)
17757c478bd9Sstevel@tonic-gate {
17767c478bd9Sstevel@tonic-gate 	xmlNodePtr cur, match;
17777c478bd9Sstevel@tonic-gate 	int err;
17787c478bd9Sstevel@tonic-gate 	char dirname[MAXPATHLEN];
17797c478bd9Sstevel@tonic-gate 
17807c478bd9Sstevel@tonic-gate 	if (tabptr == NULL)
17817c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
17827c478bd9Sstevel@tonic-gate 
17837c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
17847c478bd9Sstevel@tonic-gate 		return (err);
17857c478bd9Sstevel@tonic-gate 
17867c478bd9Sstevel@tonic-gate 	/*
17877c478bd9Sstevel@tonic-gate 	 * General algorithm:
17887c478bd9Sstevel@tonic-gate 	 * Walk the list of children looking for matches on any properties
17897c478bd9Sstevel@tonic-gate 	 * specified in the fstab parameter.  If more than one resource
17907c478bd9Sstevel@tonic-gate 	 * matches, we return Z_INSUFFICIENT_SPEC; if none match, we return
17917c478bd9Sstevel@tonic-gate 	 * Z_NO_RESOURCE_ID.
17927c478bd9Sstevel@tonic-gate 	 */
17937c478bd9Sstevel@tonic-gate 	cur = handle->zone_dh_cur;
17947c478bd9Sstevel@tonic-gate 	match = NULL;
17957c478bd9Sstevel@tonic-gate 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
17967c478bd9Sstevel@tonic-gate 		if (xmlStrcmp(cur->name, DTD_ELEM_IPD))
17977c478bd9Sstevel@tonic-gate 			continue;
17987c478bd9Sstevel@tonic-gate 		if (strlen(tabptr->zone_fs_dir) > 0) {
17997c478bd9Sstevel@tonic-gate 			if ((fetchprop(cur, DTD_ATTR_DIR, dirname,
18007c478bd9Sstevel@tonic-gate 			    sizeof (dirname)) == Z_OK) &&
18017c478bd9Sstevel@tonic-gate 			    (strcmp(tabptr->zone_fs_dir, dirname) == 0)) {
18027c478bd9Sstevel@tonic-gate 				if (match == NULL)
18037c478bd9Sstevel@tonic-gate 					match = cur;
18047c478bd9Sstevel@tonic-gate 				else
18057c478bd9Sstevel@tonic-gate 					return (Z_INSUFFICIENT_SPEC);
18067c478bd9Sstevel@tonic-gate 			}
18077c478bd9Sstevel@tonic-gate 		}
18087c478bd9Sstevel@tonic-gate 	}
18097c478bd9Sstevel@tonic-gate 
18107c478bd9Sstevel@tonic-gate 	if (match == NULL)
18117c478bd9Sstevel@tonic-gate 		return (Z_NO_RESOURCE_ID);
18127c478bd9Sstevel@tonic-gate 
18137c478bd9Sstevel@tonic-gate 	cur = match;
18147c478bd9Sstevel@tonic-gate 
18157c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_DIR, tabptr->zone_fs_dir,
18167c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_fs_dir))) != Z_OK)
18177c478bd9Sstevel@tonic-gate 		return (err);
18187c478bd9Sstevel@tonic-gate 
18197c478bd9Sstevel@tonic-gate 	return (Z_OK);
18207c478bd9Sstevel@tonic-gate }
18217c478bd9Sstevel@tonic-gate 
18227c478bd9Sstevel@tonic-gate /*
18237c478bd9Sstevel@tonic-gate  * Compare two IP addresses in string form.  Allow for the possibility that
18247c478bd9Sstevel@tonic-gate  * one might have "/<prefix-length>" at the end: allow a match on just the
18257c478bd9Sstevel@tonic-gate  * IP address (or host name) part.
18267c478bd9Sstevel@tonic-gate  */
18277c478bd9Sstevel@tonic-gate 
18287c478bd9Sstevel@tonic-gate boolean_t
18297c478bd9Sstevel@tonic-gate zonecfg_same_net_address(char *a1, char *a2)
18307c478bd9Sstevel@tonic-gate {
18317c478bd9Sstevel@tonic-gate 	char *slashp, *slashp1, *slashp2;
18327c478bd9Sstevel@tonic-gate 	int result;
18337c478bd9Sstevel@tonic-gate 
18347c478bd9Sstevel@tonic-gate 	if (strcmp(a1, a2) == 0)
18357c478bd9Sstevel@tonic-gate 		return (B_TRUE);
18367c478bd9Sstevel@tonic-gate 
18377c478bd9Sstevel@tonic-gate 	/*
18387c478bd9Sstevel@tonic-gate 	 * If neither has a slash or both do, they need to match to be
18397c478bd9Sstevel@tonic-gate 	 * considered the same, but they did not match above, so fail.
18407c478bd9Sstevel@tonic-gate 	 */
18417c478bd9Sstevel@tonic-gate 	slashp1 = strchr(a1, '/');
18427c478bd9Sstevel@tonic-gate 	slashp2 = strchr(a2, '/');
18437c478bd9Sstevel@tonic-gate 	if ((slashp1 == NULL && slashp2 == NULL) ||
18447c478bd9Sstevel@tonic-gate 	    (slashp1 != NULL && slashp2 != NULL))
18457c478bd9Sstevel@tonic-gate 		return (B_FALSE);
18467c478bd9Sstevel@tonic-gate 
18477c478bd9Sstevel@tonic-gate 	/*
18487c478bd9Sstevel@tonic-gate 	 * Only one had a slash: pick that one, zero out the slash, compare
18497c478bd9Sstevel@tonic-gate 	 * the "address only" strings, restore the slash, and return the
18507c478bd9Sstevel@tonic-gate 	 * result of the comparison.
18517c478bd9Sstevel@tonic-gate 	 */
18527c478bd9Sstevel@tonic-gate 	slashp = (slashp1 == NULL) ? slashp2 : slashp1;
18537c478bd9Sstevel@tonic-gate 	*slashp = '\0';
18547c478bd9Sstevel@tonic-gate 	result = strcmp(a1, a2);
18557c478bd9Sstevel@tonic-gate 	*slashp = '/';
18567c478bd9Sstevel@tonic-gate 	return ((result == 0));
18577c478bd9Sstevel@tonic-gate }
18587c478bd9Sstevel@tonic-gate 
18597c478bd9Sstevel@tonic-gate int
18607c478bd9Sstevel@tonic-gate zonecfg_valid_net_address(char *address, struct lifreq *lifr)
18617c478bd9Sstevel@tonic-gate {
18627c478bd9Sstevel@tonic-gate 	struct sockaddr_in *sin4;
18637c478bd9Sstevel@tonic-gate 	struct sockaddr_in6 *sin6;
18647c478bd9Sstevel@tonic-gate 	struct addrinfo hints, *result;
18657c478bd9Sstevel@tonic-gate 	char *slashp = strchr(address, '/');
18667c478bd9Sstevel@tonic-gate 
18677c478bd9Sstevel@tonic-gate 	bzero(lifr, sizeof (struct lifreq));
18687c478bd9Sstevel@tonic-gate 	sin4 = (struct sockaddr_in *)&lifr->lifr_addr;
18697c478bd9Sstevel@tonic-gate 	sin6 = (struct sockaddr_in6 *)&lifr->lifr_addr;
18707c478bd9Sstevel@tonic-gate 	if (slashp != NULL)
18717c478bd9Sstevel@tonic-gate 		*slashp = '\0';
18727c478bd9Sstevel@tonic-gate 	if (inet_pton(AF_INET, address, &sin4->sin_addr) == 1) {
18737c478bd9Sstevel@tonic-gate 		sin4->sin_family = AF_INET;
18747c478bd9Sstevel@tonic-gate 	} else if (inet_pton(AF_INET6, address, &sin6->sin6_addr) == 1) {
18757c478bd9Sstevel@tonic-gate 		if (slashp == NULL)
18767c478bd9Sstevel@tonic-gate 			return (Z_IPV6_ADDR_PREFIX_LEN);
18777c478bd9Sstevel@tonic-gate 		sin6->sin6_family = AF_INET6;
18787c478bd9Sstevel@tonic-gate 	} else {
18797c478bd9Sstevel@tonic-gate 		/* "address" may be a host name */
18807c478bd9Sstevel@tonic-gate 		(void) memset(&hints, 0, sizeof (hints));
18817c478bd9Sstevel@tonic-gate 		hints.ai_family = PF_INET;
18827c478bd9Sstevel@tonic-gate 		if (getaddrinfo(address, NULL, &hints, &result) != 0)
18837c478bd9Sstevel@tonic-gate 			return (Z_BOGUS_ADDRESS);
18847c478bd9Sstevel@tonic-gate 		sin4->sin_family = result->ai_family;
1885a1be23daSdp 
18867c478bd9Sstevel@tonic-gate 		(void) memcpy(&sin4->sin_addr,
18877c478bd9Sstevel@tonic-gate 		    /* LINTED E_BAD_PTR_CAST_ALIGN */
18887c478bd9Sstevel@tonic-gate 		    &((struct sockaddr_in *)result->ai_addr)->sin_addr,
18897c478bd9Sstevel@tonic-gate 		    sizeof (struct in_addr));
1890a1be23daSdp 
18917c478bd9Sstevel@tonic-gate 		freeaddrinfo(result);
18927c478bd9Sstevel@tonic-gate 	}
18937c478bd9Sstevel@tonic-gate 	return (Z_OK);
18947c478bd9Sstevel@tonic-gate }
18957c478bd9Sstevel@tonic-gate 
18967c478bd9Sstevel@tonic-gate int
18977c478bd9Sstevel@tonic-gate zonecfg_lookup_nwif(zone_dochandle_t handle, struct zone_nwiftab *tabptr)
18987c478bd9Sstevel@tonic-gate {
18997c478bd9Sstevel@tonic-gate 	xmlNodePtr cur, firstmatch;
19007c478bd9Sstevel@tonic-gate 	int err;
19017c478bd9Sstevel@tonic-gate 	char address[INET6_ADDRSTRLEN], physical[LIFNAMSIZ];
19027c478bd9Sstevel@tonic-gate 
19037c478bd9Sstevel@tonic-gate 	if (tabptr == NULL)
19047c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
19057c478bd9Sstevel@tonic-gate 
19067c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
19077c478bd9Sstevel@tonic-gate 		return (err);
19087c478bd9Sstevel@tonic-gate 
19097c478bd9Sstevel@tonic-gate 	cur = handle->zone_dh_cur;
19107c478bd9Sstevel@tonic-gate 	firstmatch = NULL;
19117c478bd9Sstevel@tonic-gate 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
19127c478bd9Sstevel@tonic-gate 		if (xmlStrcmp(cur->name, DTD_ELEM_NET))
19137c478bd9Sstevel@tonic-gate 			continue;
19147c478bd9Sstevel@tonic-gate 		if (strlen(tabptr->zone_nwif_physical) > 0) {
19157c478bd9Sstevel@tonic-gate 			if ((fetchprop(cur, DTD_ATTR_PHYSICAL, physical,
19167c478bd9Sstevel@tonic-gate 			    sizeof (physical)) == Z_OK) &&
19177c478bd9Sstevel@tonic-gate 			    (strcmp(tabptr->zone_nwif_physical,
19187c478bd9Sstevel@tonic-gate 			    physical) == 0)) {
19197c478bd9Sstevel@tonic-gate 				if (firstmatch == NULL)
19207c478bd9Sstevel@tonic-gate 					firstmatch = cur;
19217c478bd9Sstevel@tonic-gate 				else
19227c478bd9Sstevel@tonic-gate 					return (Z_INSUFFICIENT_SPEC);
19237c478bd9Sstevel@tonic-gate 			}
19247c478bd9Sstevel@tonic-gate 		}
19257c478bd9Sstevel@tonic-gate 		if (strlen(tabptr->zone_nwif_address) > 0) {
19267c478bd9Sstevel@tonic-gate 			if ((fetchprop(cur, DTD_ATTR_ADDRESS, address,
19277c478bd9Sstevel@tonic-gate 			    sizeof (address)) == Z_OK)) {
19287c478bd9Sstevel@tonic-gate 				if (zonecfg_same_net_address(
19297c478bd9Sstevel@tonic-gate 				    tabptr->zone_nwif_address, address)) {
19307c478bd9Sstevel@tonic-gate 					if (firstmatch == NULL)
19317c478bd9Sstevel@tonic-gate 						firstmatch = cur;
19327c478bd9Sstevel@tonic-gate 					else if (firstmatch != cur)
19337c478bd9Sstevel@tonic-gate 						return (Z_INSUFFICIENT_SPEC);
19347c478bd9Sstevel@tonic-gate 				} else {
19357c478bd9Sstevel@tonic-gate 					/*
19367c478bd9Sstevel@tonic-gate 					 * If another property matched but this
19377c478bd9Sstevel@tonic-gate 					 * one doesn't then reset firstmatch.
19387c478bd9Sstevel@tonic-gate 					 */
19397c478bd9Sstevel@tonic-gate 					if (firstmatch == cur)
19407c478bd9Sstevel@tonic-gate 						firstmatch = NULL;
19417c478bd9Sstevel@tonic-gate 				}
19427c478bd9Sstevel@tonic-gate 			}
19437c478bd9Sstevel@tonic-gate 		}
19447c478bd9Sstevel@tonic-gate 	}
19457c478bd9Sstevel@tonic-gate 	if (firstmatch == NULL)
19467c478bd9Sstevel@tonic-gate 		return (Z_NO_RESOURCE_ID);
19477c478bd9Sstevel@tonic-gate 
19487c478bd9Sstevel@tonic-gate 	cur = firstmatch;
19497c478bd9Sstevel@tonic-gate 
19507c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_PHYSICAL, tabptr->zone_nwif_physical,
19517c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_nwif_physical))) != Z_OK)
19527c478bd9Sstevel@tonic-gate 		return (err);
19537c478bd9Sstevel@tonic-gate 
19547c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_ADDRESS, tabptr->zone_nwif_address,
19557c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_nwif_address))) != Z_OK)
19567c478bd9Sstevel@tonic-gate 		return (err);
19577c478bd9Sstevel@tonic-gate 
19587c478bd9Sstevel@tonic-gate 	return (Z_OK);
19597c478bd9Sstevel@tonic-gate }
19607c478bd9Sstevel@tonic-gate 
19617c478bd9Sstevel@tonic-gate static int
19627c478bd9Sstevel@tonic-gate zonecfg_add_nwif_core(zone_dochandle_t handle, struct zone_nwiftab *tabptr)
19637c478bd9Sstevel@tonic-gate {
19647c478bd9Sstevel@tonic-gate 	xmlNodePtr newnode, cur = handle->zone_dh_cur;
19657c478bd9Sstevel@tonic-gate 	int err;
19667c478bd9Sstevel@tonic-gate 
19677c478bd9Sstevel@tonic-gate 	newnode = xmlNewTextChild(cur, NULL, DTD_ELEM_NET, NULL);
1968a1be23daSdp 	if ((err = newprop(newnode, DTD_ATTR_ADDRESS,
19697c478bd9Sstevel@tonic-gate 	    tabptr->zone_nwif_address)) != Z_OK)
19707c478bd9Sstevel@tonic-gate 		return (err);
1971a1be23daSdp 	if ((err = newprop(newnode, DTD_ATTR_PHYSICAL,
19727c478bd9Sstevel@tonic-gate 	    tabptr->zone_nwif_physical)) != Z_OK)
19737c478bd9Sstevel@tonic-gate 		return (err);
19747c478bd9Sstevel@tonic-gate 	return (Z_OK);
19757c478bd9Sstevel@tonic-gate }
19767c478bd9Sstevel@tonic-gate 
19777c478bd9Sstevel@tonic-gate int
19787c478bd9Sstevel@tonic-gate zonecfg_add_nwif(zone_dochandle_t handle, struct zone_nwiftab *tabptr)
19797c478bd9Sstevel@tonic-gate {
19807c478bd9Sstevel@tonic-gate 	int err;
19817c478bd9Sstevel@tonic-gate 
19827c478bd9Sstevel@tonic-gate 	if (tabptr == NULL)
19837c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
19847c478bd9Sstevel@tonic-gate 
19857c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
19867c478bd9Sstevel@tonic-gate 		return (err);
19877c478bd9Sstevel@tonic-gate 
19887c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_add_nwif_core(handle, tabptr)) != Z_OK)
19897c478bd9Sstevel@tonic-gate 		return (err);
19907c478bd9Sstevel@tonic-gate 
19917c478bd9Sstevel@tonic-gate 	return (Z_OK);
19927c478bd9Sstevel@tonic-gate }
19937c478bd9Sstevel@tonic-gate 
19947c478bd9Sstevel@tonic-gate static int
19957c478bd9Sstevel@tonic-gate zonecfg_delete_nwif_core(zone_dochandle_t handle, struct zone_nwiftab *tabptr)
19967c478bd9Sstevel@tonic-gate {
19977c478bd9Sstevel@tonic-gate 	xmlNodePtr cur = handle->zone_dh_cur;
19987c478bd9Sstevel@tonic-gate 	boolean_t addr_match, phys_match;
19997c478bd9Sstevel@tonic-gate 
20007c478bd9Sstevel@tonic-gate 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
20017c478bd9Sstevel@tonic-gate 		if (xmlStrcmp(cur->name, DTD_ELEM_NET))
20027c478bd9Sstevel@tonic-gate 			continue;
20037c478bd9Sstevel@tonic-gate 
20047c478bd9Sstevel@tonic-gate 		addr_match = match_prop(cur, DTD_ATTR_ADDRESS,
20057c478bd9Sstevel@tonic-gate 		    tabptr->zone_nwif_address);
20067c478bd9Sstevel@tonic-gate 		phys_match = match_prop(cur, DTD_ATTR_PHYSICAL,
20077c478bd9Sstevel@tonic-gate 		    tabptr->zone_nwif_physical);
20087c478bd9Sstevel@tonic-gate 
20097c478bd9Sstevel@tonic-gate 		if (addr_match && phys_match) {
20107c478bd9Sstevel@tonic-gate 			xmlUnlinkNode(cur);
20117c478bd9Sstevel@tonic-gate 			xmlFreeNode(cur);
20127c478bd9Sstevel@tonic-gate 			return (Z_OK);
20137c478bd9Sstevel@tonic-gate 		}
20147c478bd9Sstevel@tonic-gate 	}
20157c478bd9Sstevel@tonic-gate 	return (Z_NO_RESOURCE_ID);
20167c478bd9Sstevel@tonic-gate }
20177c478bd9Sstevel@tonic-gate 
20187c478bd9Sstevel@tonic-gate int
20197c478bd9Sstevel@tonic-gate zonecfg_delete_nwif(zone_dochandle_t handle, struct zone_nwiftab *tabptr)
20207c478bd9Sstevel@tonic-gate {
20217c478bd9Sstevel@tonic-gate 	int err;
20227c478bd9Sstevel@tonic-gate 
20237c478bd9Sstevel@tonic-gate 	if (tabptr == NULL)
20247c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
20257c478bd9Sstevel@tonic-gate 
20267c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
20277c478bd9Sstevel@tonic-gate 		return (err);
20287c478bd9Sstevel@tonic-gate 
20297c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_delete_nwif_core(handle, tabptr)) != Z_OK)
20307c478bd9Sstevel@tonic-gate 		return (err);
20317c478bd9Sstevel@tonic-gate 
20327c478bd9Sstevel@tonic-gate 	return (Z_OK);
20337c478bd9Sstevel@tonic-gate }
20347c478bd9Sstevel@tonic-gate 
20357c478bd9Sstevel@tonic-gate int
20367c478bd9Sstevel@tonic-gate zonecfg_modify_nwif(
20377c478bd9Sstevel@tonic-gate 	zone_dochandle_t handle,
20387c478bd9Sstevel@tonic-gate 	struct zone_nwiftab *oldtabptr,
20397c478bd9Sstevel@tonic-gate 	struct zone_nwiftab *newtabptr)
20407c478bd9Sstevel@tonic-gate {
20417c478bd9Sstevel@tonic-gate 	int err;
20427c478bd9Sstevel@tonic-gate 
20437c478bd9Sstevel@tonic-gate 	if (oldtabptr == NULL || newtabptr == NULL)
20447c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
20457c478bd9Sstevel@tonic-gate 
20467c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
20477c478bd9Sstevel@tonic-gate 		return (err);
20487c478bd9Sstevel@tonic-gate 
20497c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_delete_nwif_core(handle, oldtabptr)) != Z_OK)
20507c478bd9Sstevel@tonic-gate 		return (err);
20517c478bd9Sstevel@tonic-gate 
20527c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_add_nwif_core(handle, newtabptr)) != Z_OK)
20537c478bd9Sstevel@tonic-gate 		return (err);
20547c478bd9Sstevel@tonic-gate 
20557c478bd9Sstevel@tonic-gate 	return (Z_OK);
20567c478bd9Sstevel@tonic-gate }
20577c478bd9Sstevel@tonic-gate 
20587c478bd9Sstevel@tonic-gate int
20597c478bd9Sstevel@tonic-gate zonecfg_lookup_dev(zone_dochandle_t handle, struct zone_devtab *tabptr)
20607c478bd9Sstevel@tonic-gate {
20617c478bd9Sstevel@tonic-gate 	xmlNodePtr cur, firstmatch;
20627c478bd9Sstevel@tonic-gate 	int err;
20637c478bd9Sstevel@tonic-gate 	char match[MAXPATHLEN];
20647c478bd9Sstevel@tonic-gate 
20657c478bd9Sstevel@tonic-gate 	if (tabptr == NULL)
20667c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
20677c478bd9Sstevel@tonic-gate 
20687c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
20697c478bd9Sstevel@tonic-gate 		return (err);
20707c478bd9Sstevel@tonic-gate 
20717c478bd9Sstevel@tonic-gate 	cur = handle->zone_dh_cur;
20727c478bd9Sstevel@tonic-gate 	firstmatch = NULL;
20737c478bd9Sstevel@tonic-gate 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
20747c478bd9Sstevel@tonic-gate 		if (xmlStrcmp(cur->name, DTD_ELEM_DEVICE))
20757c478bd9Sstevel@tonic-gate 			continue;
20767c478bd9Sstevel@tonic-gate 		if (strlen(tabptr->zone_dev_match) == 0)
20777c478bd9Sstevel@tonic-gate 			continue;
20787c478bd9Sstevel@tonic-gate 
20797c478bd9Sstevel@tonic-gate 		if ((fetchprop(cur, DTD_ATTR_MATCH, match,
20807c478bd9Sstevel@tonic-gate 		    sizeof (match)) == Z_OK)) {
20817c478bd9Sstevel@tonic-gate 			if (strcmp(tabptr->zone_dev_match,
20827c478bd9Sstevel@tonic-gate 			    match) == 0) {
20837c478bd9Sstevel@tonic-gate 				if (firstmatch == NULL)
20847c478bd9Sstevel@tonic-gate 					firstmatch = cur;
20857c478bd9Sstevel@tonic-gate 				else if (firstmatch != cur)
20867c478bd9Sstevel@tonic-gate 					return (Z_INSUFFICIENT_SPEC);
20877c478bd9Sstevel@tonic-gate 			} else {
20887c478bd9Sstevel@tonic-gate 				/*
20897c478bd9Sstevel@tonic-gate 				 * If another property matched but this
20907c478bd9Sstevel@tonic-gate 				 * one doesn't then reset firstmatch.
20917c478bd9Sstevel@tonic-gate 				 */
20927c478bd9Sstevel@tonic-gate 				if (firstmatch == cur)
20937c478bd9Sstevel@tonic-gate 					firstmatch = NULL;
20947c478bd9Sstevel@tonic-gate 			}
20957c478bd9Sstevel@tonic-gate 		}
20967c478bd9Sstevel@tonic-gate 	}
20977c478bd9Sstevel@tonic-gate 	if (firstmatch == NULL)
20987c478bd9Sstevel@tonic-gate 		return (Z_NO_RESOURCE_ID);
20997c478bd9Sstevel@tonic-gate 
21007c478bd9Sstevel@tonic-gate 	cur = firstmatch;
21017c478bd9Sstevel@tonic-gate 
21027c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_MATCH, tabptr->zone_dev_match,
21037c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_dev_match))) != Z_OK)
21047c478bd9Sstevel@tonic-gate 		return (err);
21057c478bd9Sstevel@tonic-gate 
21067c478bd9Sstevel@tonic-gate 	return (Z_OK);
21077c478bd9Sstevel@tonic-gate }
21087c478bd9Sstevel@tonic-gate 
21097c478bd9Sstevel@tonic-gate static int
21107c478bd9Sstevel@tonic-gate zonecfg_add_dev_core(zone_dochandle_t handle, struct zone_devtab *tabptr)
21117c478bd9Sstevel@tonic-gate {
21127c478bd9Sstevel@tonic-gate 	xmlNodePtr newnode, cur = handle->zone_dh_cur;
21137c478bd9Sstevel@tonic-gate 	int err;
21147c478bd9Sstevel@tonic-gate 
21157c478bd9Sstevel@tonic-gate 	newnode = xmlNewTextChild(cur, NULL, DTD_ELEM_DEVICE, NULL);
21167c478bd9Sstevel@tonic-gate 
2117a1be23daSdp 	if ((err = newprop(newnode, DTD_ATTR_MATCH,
21187c478bd9Sstevel@tonic-gate 	    tabptr->zone_dev_match)) != Z_OK)
21197c478bd9Sstevel@tonic-gate 		return (err);
21207c478bd9Sstevel@tonic-gate 
21217c478bd9Sstevel@tonic-gate 	return (Z_OK);
21227c478bd9Sstevel@tonic-gate }
21237c478bd9Sstevel@tonic-gate 
21247c478bd9Sstevel@tonic-gate int
21257c478bd9Sstevel@tonic-gate zonecfg_add_dev(zone_dochandle_t handle, struct zone_devtab *tabptr)
21267c478bd9Sstevel@tonic-gate {
21277c478bd9Sstevel@tonic-gate 	int err;
21287c478bd9Sstevel@tonic-gate 
21297c478bd9Sstevel@tonic-gate 	if (tabptr == NULL)
21307c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
21317c478bd9Sstevel@tonic-gate 
21327c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
21337c478bd9Sstevel@tonic-gate 		return (err);
21347c478bd9Sstevel@tonic-gate 
21357c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_add_dev_core(handle, tabptr)) != Z_OK)
21367c478bd9Sstevel@tonic-gate 		return (err);
21377c478bd9Sstevel@tonic-gate 
21387c478bd9Sstevel@tonic-gate 	return (Z_OK);
21397c478bd9Sstevel@tonic-gate }
21407c478bd9Sstevel@tonic-gate 
21417c478bd9Sstevel@tonic-gate static int
21427c478bd9Sstevel@tonic-gate zonecfg_delete_dev_core(zone_dochandle_t handle, struct zone_devtab *tabptr)
21437c478bd9Sstevel@tonic-gate {
21447c478bd9Sstevel@tonic-gate 	xmlNodePtr cur = handle->zone_dh_cur;
21457c478bd9Sstevel@tonic-gate 	int match_match;
21467c478bd9Sstevel@tonic-gate 
21477c478bd9Sstevel@tonic-gate 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
21487c478bd9Sstevel@tonic-gate 		if (xmlStrcmp(cur->name, DTD_ELEM_DEVICE))
21497c478bd9Sstevel@tonic-gate 			continue;
21507c478bd9Sstevel@tonic-gate 
21517c478bd9Sstevel@tonic-gate 		match_match = match_prop(cur, DTD_ATTR_MATCH,
21527c478bd9Sstevel@tonic-gate 		    tabptr->zone_dev_match);
21537c478bd9Sstevel@tonic-gate 
21547c478bd9Sstevel@tonic-gate 		if (match_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_dev(zone_dochandle_t handle, struct zone_devtab *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_dev_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_dev(
21827c478bd9Sstevel@tonic-gate 	zone_dochandle_t handle,
21837c478bd9Sstevel@tonic-gate 	struct zone_devtab *oldtabptr,
21847c478bd9Sstevel@tonic-gate 	struct zone_devtab *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_dev_core(handle, oldtabptr)) != Z_OK)
21957c478bd9Sstevel@tonic-gate 		return (err);
21967c478bd9Sstevel@tonic-gate 
21977c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_add_dev_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 
2203ee519a1fSgjelinek /* Lock to serialize all zonecfg_devwalks */
2204ee519a1fSgjelinek static pthread_mutex_t zonecfg_devwalk_lock = PTHREAD_MUTEX_INITIALIZER;
2205ee519a1fSgjelinek /*
2206ee519a1fSgjelinek  * Global variables used to pass data from zonecfg_devwalk to the nftw
2207ee519a1fSgjelinek  * call-back (zonecfg_devwalk_cb).  g_devwalk_data is really the void*
2208ee519a1fSgjelinek  * parameter and g_devwalk_cb is really the *cb parameter from zonecfg_devwalk.
2209ee519a1fSgjelinek  */
2210ee519a1fSgjelinek static void *g_devwalk_data;
2211ee519a1fSgjelinek static int (*g_devwalk_cb)(const char *, uid_t, gid_t, mode_t, const char *,
2212ee519a1fSgjelinek     void *);
2213ee519a1fSgjelinek static size_t g_devwalk_skip_prefix;
2214ee519a1fSgjelinek 
2215ee519a1fSgjelinek /*
2216ee519a1fSgjelinek  * This is the nftw call-back function used by zonecfg_devwalk.  It is
2217ee519a1fSgjelinek  * responsible for calling the actual call-back that is passed in to
2218ee519a1fSgjelinek  * zonecfg_devwalk as the *cb argument.
2219ee519a1fSgjelinek  */
2220ee519a1fSgjelinek /* ARGSUSED2 */
2221ee519a1fSgjelinek static int
2222ee519a1fSgjelinek zonecfg_devwalk_cb(const char *path, const struct stat *st, int f,
2223ee519a1fSgjelinek     struct FTW *ftw)
2224ee519a1fSgjelinek {
2225ee519a1fSgjelinek 	acl_t *acl;
2226ee519a1fSgjelinek 	char *acl_txt = NULL;
2227ee519a1fSgjelinek 
2228ee519a1fSgjelinek 	/* skip all but character and block devices */
2229ee519a1fSgjelinek 	if (!S_ISBLK(st->st_mode) && !S_ISCHR(st->st_mode))
2230ee519a1fSgjelinek 		return (0);
2231ee519a1fSgjelinek 
2232ee519a1fSgjelinek 	if ((acl_get(path, ACL_NO_TRIVIAL, &acl) == 0) &&
2233ee519a1fSgjelinek 	    acl != NULL) {
2234ee519a1fSgjelinek 		acl_txt = acl_totext(acl, ACL_NORESOLVE);
2235ee519a1fSgjelinek 		acl_free(acl);
2236ee519a1fSgjelinek 	}
2237ee519a1fSgjelinek 
2238ee519a1fSgjelinek 	if (strlen(path) <= g_devwalk_skip_prefix)
2239ee519a1fSgjelinek 		return (0);
2240ee519a1fSgjelinek 
2241ee519a1fSgjelinek 	g_devwalk_cb(path + g_devwalk_skip_prefix, st->st_uid, st->st_gid,
2242ee519a1fSgjelinek 	    st->st_mode & S_IAMB, acl_txt != NULL ? acl_txt : "",
2243ee519a1fSgjelinek 	    g_devwalk_data);
2244ee519a1fSgjelinek 	free(acl_txt);
2245ee519a1fSgjelinek 	return (0);
2246ee519a1fSgjelinek }
2247ee519a1fSgjelinek 
2248ee519a1fSgjelinek /*
2249ee519a1fSgjelinek  * Walk the dev tree for the zone specified by hdl and call the call-back (cb)
2250ee519a1fSgjelinek  * function for each entry in the tree.  The call-back will be passed the
2251ee519a1fSgjelinek  * name, uid, gid, mode, acl string and the void *data input parameter
2252ee519a1fSgjelinek  * for each dev entry.
2253ee519a1fSgjelinek  *
2254ee519a1fSgjelinek  * Data is passed to the zonecfg_devwalk_cb through the global variables
2255ee519a1fSgjelinek  * g_devwalk_data, *g_devwalk_cb, and g_devwalk_skip_prefix.  The
2256ee519a1fSgjelinek  * zonecfg_devwalk_cb function will actually call *cb.
2257ee519a1fSgjelinek  */
2258ee519a1fSgjelinek int
2259ee519a1fSgjelinek zonecfg_devwalk(zone_dochandle_t hdl,
2260ee519a1fSgjelinek     int (*cb)(const char *, uid_t, gid_t, mode_t, const char *, void *),
2261ee519a1fSgjelinek     void *data)
2262ee519a1fSgjelinek {
2263ee519a1fSgjelinek 	char path[MAXPATHLEN];
2264ee519a1fSgjelinek 	int ret;
2265ee519a1fSgjelinek 
2266ee519a1fSgjelinek 	if ((ret = zonecfg_get_zonepath(hdl, path, sizeof (path))) != Z_OK)
2267ee519a1fSgjelinek 		return (ret);
2268ee519a1fSgjelinek 
2269ee519a1fSgjelinek 	if (strlcat(path, "/dev", sizeof (path)) >= sizeof (path))
2270ee519a1fSgjelinek 		return (Z_TOO_BIG);
2271ee519a1fSgjelinek 	g_devwalk_skip_prefix = strlen(path) + 1;
2272ee519a1fSgjelinek 
2273ee519a1fSgjelinek 	/*
2274ee519a1fSgjelinek 	 * We have to serialize all zonecfg_devwalks in the same process
2275ee519a1fSgjelinek 	 * (which should be fine), since nftw() is so badly designed.
2276ee519a1fSgjelinek 	 */
2277ee519a1fSgjelinek 	(void) pthread_mutex_lock(&zonecfg_devwalk_lock);
2278ee519a1fSgjelinek 
2279ee519a1fSgjelinek 	g_devwalk_data = data;
2280ee519a1fSgjelinek 	g_devwalk_cb = cb;
2281ee519a1fSgjelinek 	(void) nftw(path, zonecfg_devwalk_cb, 0, FTW_PHYS);
2282ee519a1fSgjelinek 
2283ee519a1fSgjelinek 	(void) pthread_mutex_unlock(&zonecfg_devwalk_lock);
2284ee519a1fSgjelinek 	return (Z_OK);
2285ee519a1fSgjelinek }
2286ee519a1fSgjelinek 
2287ee519a1fSgjelinek /*
2288ee519a1fSgjelinek  * Update the owner, group, mode and acl on the specified dev (inpath) for
2289ee519a1fSgjelinek  * the zone (hdl).  This function can be used to fix up the dev tree after
2290ee519a1fSgjelinek  * attaching a migrated zone.
2291ee519a1fSgjelinek  */
2292ee519a1fSgjelinek int
2293ee519a1fSgjelinek zonecfg_devperms_apply(zone_dochandle_t hdl, const char *inpath, uid_t owner,
2294ee519a1fSgjelinek     gid_t group, mode_t mode, const char *acltxt)
2295ee519a1fSgjelinek {
2296ee519a1fSgjelinek 	int ret;
2297ee519a1fSgjelinek 	char path[MAXPATHLEN];
2298ee519a1fSgjelinek 	struct stat st;
2299ee519a1fSgjelinek 	acl_t *aclp;
2300ee519a1fSgjelinek 
2301ee519a1fSgjelinek 	if ((ret = zonecfg_get_zonepath(hdl, path, sizeof (path))) != Z_OK)
2302ee519a1fSgjelinek 		return (ret);
2303ee519a1fSgjelinek 
2304ee519a1fSgjelinek 	if (strlcat(path, "/dev/", sizeof (path)) >= sizeof (path))
2305ee519a1fSgjelinek 		return (Z_TOO_BIG);
2306ee519a1fSgjelinek 	if (strlcat(path, inpath, sizeof (path)) >= sizeof (path))
2307ee519a1fSgjelinek 		return (Z_TOO_BIG);
2308ee519a1fSgjelinek 
2309ee519a1fSgjelinek 	if (stat(path, &st) == -1)
2310ee519a1fSgjelinek 		return (Z_INVAL);
2311ee519a1fSgjelinek 
2312ee519a1fSgjelinek 	/* make sure we're only touching device nodes */
2313ee519a1fSgjelinek 	if (!S_ISCHR(st.st_mode) && !S_ISBLK(st.st_mode))
2314ee519a1fSgjelinek 		return (Z_INVAL);
2315ee519a1fSgjelinek 
2316ee519a1fSgjelinek 	if (chown(path, owner, group) == -1)
2317ee519a1fSgjelinek 		return (Z_SYSTEM);
2318ee519a1fSgjelinek 
2319ee519a1fSgjelinek 	if (chmod(path, mode) == -1)
2320ee519a1fSgjelinek 		return (Z_SYSTEM);
2321ee519a1fSgjelinek 
2322ee519a1fSgjelinek 	if ((acltxt == NULL) || (strcmp(acltxt, "") == 0))
2323ee519a1fSgjelinek 		return (Z_OK);
2324ee519a1fSgjelinek 
2325ee519a1fSgjelinek 	if (acl_fromtext(acltxt, &aclp) != 0)
2326ee519a1fSgjelinek 		return (Z_SYSTEM);
2327ee519a1fSgjelinek 
2328ee519a1fSgjelinek 	errno = 0;
2329ee519a1fSgjelinek 	if (acl_set(path, aclp) == -1) {
2330ee519a1fSgjelinek 		free(aclp);
2331ee519a1fSgjelinek 		return (Z_SYSTEM);
2332ee519a1fSgjelinek 	}
2333ee519a1fSgjelinek 
2334ee519a1fSgjelinek 	free(aclp);
2335ee519a1fSgjelinek 	return (Z_OK);
2336ee519a1fSgjelinek }
2337ee519a1fSgjelinek 
23387c478bd9Sstevel@tonic-gate /*
23397c478bd9Sstevel@tonic-gate  * This is the set of devices which must be present in every zone.  Users
23407c478bd9Sstevel@tonic-gate  * can augment this list with additional device rules in their zone
23417c478bd9Sstevel@tonic-gate  * configuration, but at present cannot remove any of the this set of
23427c478bd9Sstevel@tonic-gate  * standard devices.  All matching is done by /dev pathname (the "/dev"
23437c478bd9Sstevel@tonic-gate  * part is implicit.  Try to keep rules which match a large number of
23447c478bd9Sstevel@tonic-gate  * devices (like the pts rule) first.
23457c478bd9Sstevel@tonic-gate  */
23467c478bd9Sstevel@tonic-gate static const char *standard_devs[] = {
23477c478bd9Sstevel@tonic-gate 	"pts/*",
23487c478bd9Sstevel@tonic-gate 	"ptmx",
23497c478bd9Sstevel@tonic-gate 	"random",
23507c478bd9Sstevel@tonic-gate 	"urandom",
23517c478bd9Sstevel@tonic-gate 	"poll",
23527c478bd9Sstevel@tonic-gate 	"pool",
23537c478bd9Sstevel@tonic-gate 	"kstat",
23547c478bd9Sstevel@tonic-gate 	"zero",
23557c478bd9Sstevel@tonic-gate 	"null",
23567c478bd9Sstevel@tonic-gate 	"crypto",
23577c478bd9Sstevel@tonic-gate 	"cryptoadm",
23587c478bd9Sstevel@tonic-gate 	"ticots",
23597c478bd9Sstevel@tonic-gate 	"ticotsord",
23607c478bd9Sstevel@tonic-gate 	"ticlts",
23617c478bd9Sstevel@tonic-gate 	"lo0",
23627c478bd9Sstevel@tonic-gate 	"lo1",
23637c478bd9Sstevel@tonic-gate 	"lo2",
23647c478bd9Sstevel@tonic-gate 	"lo3",
23657c478bd9Sstevel@tonic-gate 	"sad/user",
23667c478bd9Sstevel@tonic-gate 	"tty",
23677c478bd9Sstevel@tonic-gate 	"logindmux",
23687c478bd9Sstevel@tonic-gate 	"log",
23697c478bd9Sstevel@tonic-gate 	"conslog",
23707c478bd9Sstevel@tonic-gate 	"arp",
23717c478bd9Sstevel@tonic-gate 	"tcp",
23727c478bd9Sstevel@tonic-gate 	"tcp6",
23737c478bd9Sstevel@tonic-gate 	"udp",
23747c478bd9Sstevel@tonic-gate 	"udp6",
23757c478bd9Sstevel@tonic-gate 	"sysevent",
23767c478bd9Sstevel@tonic-gate #ifdef __sparc
23777c478bd9Sstevel@tonic-gate 	"openprom",
23787c478bd9Sstevel@tonic-gate #endif
23797c478bd9Sstevel@tonic-gate 	"cpu/self/cpuid",
2380ad4023c4Sdp 	"dtrace/*",
2381ad4023c4Sdp 	"dtrace/provider/*",
2382fa9e4066Sahrens 	"zfs",
23837c478bd9Sstevel@tonic-gate 	NULL
23847c478bd9Sstevel@tonic-gate };
23857c478bd9Sstevel@tonic-gate 
23867c478bd9Sstevel@tonic-gate /*
23877c478bd9Sstevel@tonic-gate  * This function finds everything mounted under a zone's rootpath.
23887c478bd9Sstevel@tonic-gate  * This returns the number of mounts under rootpath, or -1 on error.
23897c478bd9Sstevel@tonic-gate  * callback is called once per mount found with the first argument
23907c478bd9Sstevel@tonic-gate  * pointing to the  mount point.
23917c478bd9Sstevel@tonic-gate  *
23927c478bd9Sstevel@tonic-gate  * If the callback function returns non-zero zonecfg_find_mounts
23937c478bd9Sstevel@tonic-gate  * aborts with an error.
23947c478bd9Sstevel@tonic-gate  */
23957c478bd9Sstevel@tonic-gate 
23967c478bd9Sstevel@tonic-gate int
23977c478bd9Sstevel@tonic-gate zonecfg_find_mounts(char *rootpath, int (*callback)(const char *, void *),
23987c478bd9Sstevel@tonic-gate     void *priv) {
23997c478bd9Sstevel@tonic-gate 	FILE *mnttab;
24007c478bd9Sstevel@tonic-gate 	struct mnttab m;
24017c478bd9Sstevel@tonic-gate 	size_t l;
240207b574eeSgjelinek 	int zfsl;
24037c478bd9Sstevel@tonic-gate 	int rv = 0;
240407b574eeSgjelinek 	char zfs_path[MAXPATHLEN];
24057c478bd9Sstevel@tonic-gate 
24067c478bd9Sstevel@tonic-gate 	assert(rootpath != NULL);
24077c478bd9Sstevel@tonic-gate 
240807b574eeSgjelinek 	if ((zfsl = snprintf(zfs_path, sizeof (zfs_path), "%s/.zfs/", rootpath))
240907b574eeSgjelinek 	    >= sizeof (zfs_path))
241007b574eeSgjelinek 		return (-1);
241107b574eeSgjelinek 
24127c478bd9Sstevel@tonic-gate 	l = strlen(rootpath);
24137c478bd9Sstevel@tonic-gate 
24147c478bd9Sstevel@tonic-gate 	mnttab = fopen("/etc/mnttab", "r");
24157c478bd9Sstevel@tonic-gate 
24167c478bd9Sstevel@tonic-gate 	if (mnttab == NULL)
24177c478bd9Sstevel@tonic-gate 		return (-1);
24187c478bd9Sstevel@tonic-gate 
24197c478bd9Sstevel@tonic-gate 	if (ioctl(fileno(mnttab), MNTIOC_SHOWHIDDEN, NULL) < 0)  {
24207c478bd9Sstevel@tonic-gate 		rv = -1;
24217c478bd9Sstevel@tonic-gate 		goto out;
24227c478bd9Sstevel@tonic-gate 	}
24237c478bd9Sstevel@tonic-gate 
24247c478bd9Sstevel@tonic-gate 	while (!getmntent(mnttab, &m)) {
24257c478bd9Sstevel@tonic-gate 		if ((strncmp(rootpath, m.mnt_mountp, l) == 0) &&
242607b574eeSgjelinek 		    (m.mnt_mountp[l] == '/') &&
242707b574eeSgjelinek 		    (strncmp(zfs_path, m.mnt_mountp, zfsl) != 0)) {
24287c478bd9Sstevel@tonic-gate 			rv++;
24297c478bd9Sstevel@tonic-gate 			if (callback == NULL)
24307c478bd9Sstevel@tonic-gate 				continue;
24317c478bd9Sstevel@tonic-gate 			if (callback(m.mnt_mountp, priv)) {
24327c478bd9Sstevel@tonic-gate 				rv = -1;
24337c478bd9Sstevel@tonic-gate 				goto out;
24347c478bd9Sstevel@tonic-gate 
24357c478bd9Sstevel@tonic-gate 			}
24367c478bd9Sstevel@tonic-gate 		}
24377c478bd9Sstevel@tonic-gate 	}
24387c478bd9Sstevel@tonic-gate 
24397c478bd9Sstevel@tonic-gate out:
24407c478bd9Sstevel@tonic-gate 	(void) fclose(mnttab);
24417c478bd9Sstevel@tonic-gate 	return (rv);
24427c478bd9Sstevel@tonic-gate }
24437c478bd9Sstevel@tonic-gate 
24447c478bd9Sstevel@tonic-gate /*
24457c478bd9Sstevel@tonic-gate  * This routine is used to determine if a given device should appear in the
24467c478bd9Sstevel@tonic-gate  * zone represented by 'handle'.  First it consults the list of "standard"
24477c478bd9Sstevel@tonic-gate  * zone devices.  Then it scans the user-supplied device entries.
24487c478bd9Sstevel@tonic-gate  */
24497c478bd9Sstevel@tonic-gate int
24507c478bd9Sstevel@tonic-gate zonecfg_match_dev(zone_dochandle_t handle, char *devpath,
24517c478bd9Sstevel@tonic-gate     struct zone_devtab *out_match)
24527c478bd9Sstevel@tonic-gate {
24537c478bd9Sstevel@tonic-gate 	int err;
24547c478bd9Sstevel@tonic-gate 	boolean_t found = B_FALSE;
24557c478bd9Sstevel@tonic-gate 	char match[MAXPATHLEN];
24567c478bd9Sstevel@tonic-gate 	const char **stdmatch;
24577c478bd9Sstevel@tonic-gate 	xmlNodePtr cur;
24587c478bd9Sstevel@tonic-gate 
24597c478bd9Sstevel@tonic-gate 	if (handle == NULL || devpath == NULL)
24607c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
24617c478bd9Sstevel@tonic-gate 
24627c478bd9Sstevel@tonic-gate 	/*
24637c478bd9Sstevel@tonic-gate 	 * Check the "standard" devices which we require to be present.
24647c478bd9Sstevel@tonic-gate 	 */
24657c478bd9Sstevel@tonic-gate 	for (stdmatch = &standard_devs[0]; *stdmatch != NULL; stdmatch++) {
24667c478bd9Sstevel@tonic-gate 		/*
24677c478bd9Sstevel@tonic-gate 		 * fnmatch gives us simple but powerful shell-style matching.
24687c478bd9Sstevel@tonic-gate 		 */
24697c478bd9Sstevel@tonic-gate 		if (fnmatch(*stdmatch, devpath, FNM_PATHNAME) == 0) {
24707c478bd9Sstevel@tonic-gate 			if (!out_match)
24717c478bd9Sstevel@tonic-gate 				return (Z_OK);
24727c478bd9Sstevel@tonic-gate 			(void) snprintf(out_match->zone_dev_match,
24737c478bd9Sstevel@tonic-gate 			    sizeof (out_match->zone_dev_match),
24747c478bd9Sstevel@tonic-gate 			    "/dev/%s", *stdmatch);
24757c478bd9Sstevel@tonic-gate 			return (Z_OK);
24767c478bd9Sstevel@tonic-gate 		}
24777c478bd9Sstevel@tonic-gate 	}
24787c478bd9Sstevel@tonic-gate 
24797c478bd9Sstevel@tonic-gate 	/*
24807c478bd9Sstevel@tonic-gate 	 * We got no hits in the set of standard devices.  On to the user
24817c478bd9Sstevel@tonic-gate 	 * supplied ones.
24827c478bd9Sstevel@tonic-gate 	 */
24837c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK) {
24847c478bd9Sstevel@tonic-gate 		handle->zone_dh_cur = NULL;
24857c478bd9Sstevel@tonic-gate 		return (err);
24867c478bd9Sstevel@tonic-gate 	}
24877c478bd9Sstevel@tonic-gate 
24887c478bd9Sstevel@tonic-gate 	cur = handle->zone_dh_cur;
24897c478bd9Sstevel@tonic-gate 	cur = cur->xmlChildrenNode;
24907c478bd9Sstevel@tonic-gate 	if (cur == NULL)
24917c478bd9Sstevel@tonic-gate 		return (Z_NO_ENTRY);
24927c478bd9Sstevel@tonic-gate 	handle->zone_dh_cur = cur;
24937c478bd9Sstevel@tonic-gate 
24947c478bd9Sstevel@tonic-gate 	for (; cur != NULL; cur = cur->next) {
24957c478bd9Sstevel@tonic-gate 		char *m;
24967c478bd9Sstevel@tonic-gate 		if (xmlStrcmp(cur->name, DTD_ELEM_DEVICE) != 0)
24977c478bd9Sstevel@tonic-gate 			continue;
24987c478bd9Sstevel@tonic-gate 		if ((err = fetchprop(cur, DTD_ATTR_MATCH, match,
24997c478bd9Sstevel@tonic-gate 		    sizeof (match))) != Z_OK) {
25007c478bd9Sstevel@tonic-gate 			handle->zone_dh_cur = handle->zone_dh_top;
25017c478bd9Sstevel@tonic-gate 			return (err);
25027c478bd9Sstevel@tonic-gate 		}
25037c478bd9Sstevel@tonic-gate 		m = match;
25047c478bd9Sstevel@tonic-gate 		/*
25057c478bd9Sstevel@tonic-gate 		 * fnmatch gives us simple but powerful shell-style matching;
25067c478bd9Sstevel@tonic-gate 		 * but first, we need to strip out /dev/ from the matching rule.
25077c478bd9Sstevel@tonic-gate 		 */
25087c478bd9Sstevel@tonic-gate 		if (strncmp(m, "/dev/", 5) == 0)
25097c478bd9Sstevel@tonic-gate 			m += 5;
25107c478bd9Sstevel@tonic-gate 
25117c478bd9Sstevel@tonic-gate 		if (fnmatch(m, devpath, FNM_PATHNAME) == 0) {
25127c478bd9Sstevel@tonic-gate 			found = B_TRUE;
25137c478bd9Sstevel@tonic-gate 			break;
25147c478bd9Sstevel@tonic-gate 		}
25157c478bd9Sstevel@tonic-gate 	}
25167c478bd9Sstevel@tonic-gate 
25177c478bd9Sstevel@tonic-gate 	if (!found)
25187c478bd9Sstevel@tonic-gate 		return (Z_NO_ENTRY);
25197c478bd9Sstevel@tonic-gate 
25207c478bd9Sstevel@tonic-gate 	if (!out_match)
25217c478bd9Sstevel@tonic-gate 		return (Z_OK);
25227c478bd9Sstevel@tonic-gate 
25237c478bd9Sstevel@tonic-gate 	(void) strlcpy(out_match->zone_dev_match, match,
25247c478bd9Sstevel@tonic-gate 	    sizeof (out_match->zone_dev_match));
25257c478bd9Sstevel@tonic-gate 	return (Z_OK);
25267c478bd9Sstevel@tonic-gate }
25277c478bd9Sstevel@tonic-gate 
25287c478bd9Sstevel@tonic-gate int
25297c478bd9Sstevel@tonic-gate zonecfg_lookup_attr(zone_dochandle_t handle, struct zone_attrtab *tabptr)
25307c478bd9Sstevel@tonic-gate {
25317c478bd9Sstevel@tonic-gate 	xmlNodePtr cur, firstmatch;
25327c478bd9Sstevel@tonic-gate 	int err;
25337c478bd9Sstevel@tonic-gate 	char name[MAXNAMELEN], type[MAXNAMELEN], value[MAXNAMELEN];
25347c478bd9Sstevel@tonic-gate 
25357c478bd9Sstevel@tonic-gate 	if (tabptr == NULL)
25367c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
25377c478bd9Sstevel@tonic-gate 
25387c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
25397c478bd9Sstevel@tonic-gate 		return (err);
25407c478bd9Sstevel@tonic-gate 
25417c478bd9Sstevel@tonic-gate 	cur = handle->zone_dh_cur;
25427c478bd9Sstevel@tonic-gate 	firstmatch = NULL;
25437c478bd9Sstevel@tonic-gate 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
25447c478bd9Sstevel@tonic-gate 		if (xmlStrcmp(cur->name, DTD_ELEM_ATTR))
25457c478bd9Sstevel@tonic-gate 			continue;
25467c478bd9Sstevel@tonic-gate 		if (strlen(tabptr->zone_attr_name) > 0) {
25477c478bd9Sstevel@tonic-gate 			if ((fetchprop(cur, DTD_ATTR_NAME, name,
25487c478bd9Sstevel@tonic-gate 			    sizeof (name)) == Z_OK) &&
25497c478bd9Sstevel@tonic-gate 			    (strcmp(tabptr->zone_attr_name, name) == 0)) {
25507c478bd9Sstevel@tonic-gate 				if (firstmatch == NULL)
25517c478bd9Sstevel@tonic-gate 					firstmatch = cur;
25527c478bd9Sstevel@tonic-gate 				else
25537c478bd9Sstevel@tonic-gate 					return (Z_INSUFFICIENT_SPEC);
25547c478bd9Sstevel@tonic-gate 			}
25557c478bd9Sstevel@tonic-gate 		}
25567c478bd9Sstevel@tonic-gate 		if (strlen(tabptr->zone_attr_type) > 0) {
25577c478bd9Sstevel@tonic-gate 			if ((fetchprop(cur, DTD_ATTR_TYPE, type,
25587c478bd9Sstevel@tonic-gate 			    sizeof (type)) == Z_OK)) {
25597c478bd9Sstevel@tonic-gate 				if (strcmp(tabptr->zone_attr_type, type) == 0) {
25607c478bd9Sstevel@tonic-gate 					if (firstmatch == NULL)
25617c478bd9Sstevel@tonic-gate 						firstmatch = cur;
25627c478bd9Sstevel@tonic-gate 					else if (firstmatch != cur)
25637c478bd9Sstevel@tonic-gate 						return (Z_INSUFFICIENT_SPEC);
25647c478bd9Sstevel@tonic-gate 				} else {
25657c478bd9Sstevel@tonic-gate 					/*
25667c478bd9Sstevel@tonic-gate 					 * If another property matched but this
25677c478bd9Sstevel@tonic-gate 					 * one doesn't then reset firstmatch.
25687c478bd9Sstevel@tonic-gate 					 */
25697c478bd9Sstevel@tonic-gate 					if (firstmatch == cur)
25707c478bd9Sstevel@tonic-gate 						firstmatch = NULL;
25717c478bd9Sstevel@tonic-gate 				}
25727c478bd9Sstevel@tonic-gate 			}
25737c478bd9Sstevel@tonic-gate 		}
25747c478bd9Sstevel@tonic-gate 		if (strlen(tabptr->zone_attr_value) > 0) {
25757c478bd9Sstevel@tonic-gate 			if ((fetchprop(cur, DTD_ATTR_VALUE, value,
25767c478bd9Sstevel@tonic-gate 			    sizeof (value)) == Z_OK)) {
25777c478bd9Sstevel@tonic-gate 				if (strcmp(tabptr->zone_attr_value, value) ==
25787c478bd9Sstevel@tonic-gate 				    0) {
25797c478bd9Sstevel@tonic-gate 					if (firstmatch == NULL)
25807c478bd9Sstevel@tonic-gate 						firstmatch = cur;
25817c478bd9Sstevel@tonic-gate 					else if (firstmatch != cur)
25827c478bd9Sstevel@tonic-gate 						return (Z_INSUFFICIENT_SPEC);
25837c478bd9Sstevel@tonic-gate 				} else {
25847c478bd9Sstevel@tonic-gate 					/*
25857c478bd9Sstevel@tonic-gate 					 * If another property matched but this
25867c478bd9Sstevel@tonic-gate 					 * one doesn't then reset firstmatch.
25877c478bd9Sstevel@tonic-gate 					 */
25887c478bd9Sstevel@tonic-gate 					if (firstmatch == cur)
25897c478bd9Sstevel@tonic-gate 						firstmatch = NULL;
25907c478bd9Sstevel@tonic-gate 				}
25917c478bd9Sstevel@tonic-gate 			}
25927c478bd9Sstevel@tonic-gate 		}
25937c478bd9Sstevel@tonic-gate 	}
25947c478bd9Sstevel@tonic-gate 	if (firstmatch == NULL)
25957c478bd9Sstevel@tonic-gate 		return (Z_NO_RESOURCE_ID);
25967c478bd9Sstevel@tonic-gate 
25977c478bd9Sstevel@tonic-gate 	cur = firstmatch;
25987c478bd9Sstevel@tonic-gate 
25997c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_NAME, tabptr->zone_attr_name,
26007c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_attr_name))) != Z_OK)
26017c478bd9Sstevel@tonic-gate 		return (err);
26027c478bd9Sstevel@tonic-gate 
26037c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_TYPE, tabptr->zone_attr_type,
26047c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_attr_type))) != Z_OK)
26057c478bd9Sstevel@tonic-gate 		return (err);
26067c478bd9Sstevel@tonic-gate 
26077c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_VALUE, tabptr->zone_attr_value,
26087c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_attr_value))) != Z_OK)
26097c478bd9Sstevel@tonic-gate 		return (err);
26107c478bd9Sstevel@tonic-gate 
26117c478bd9Sstevel@tonic-gate 	return (Z_OK);
26127c478bd9Sstevel@tonic-gate }
26137c478bd9Sstevel@tonic-gate 
26147c478bd9Sstevel@tonic-gate static int
26157c478bd9Sstevel@tonic-gate zonecfg_add_attr_core(zone_dochandle_t handle, struct zone_attrtab *tabptr)
26167c478bd9Sstevel@tonic-gate {
26177c478bd9Sstevel@tonic-gate 	xmlNodePtr newnode, cur = handle->zone_dh_cur;
26187c478bd9Sstevel@tonic-gate 	int err;
26197c478bd9Sstevel@tonic-gate 
26207c478bd9Sstevel@tonic-gate 	newnode = xmlNewTextChild(cur, NULL, DTD_ELEM_ATTR, NULL);
2621a1be23daSdp 	err = newprop(newnode, DTD_ATTR_NAME, tabptr->zone_attr_name);
26227c478bd9Sstevel@tonic-gate 	if (err != Z_OK)
26237c478bd9Sstevel@tonic-gate 		return (err);
2624a1be23daSdp 	err = newprop(newnode, DTD_ATTR_TYPE, tabptr->zone_attr_type);
26257c478bd9Sstevel@tonic-gate 	if (err != Z_OK)
26267c478bd9Sstevel@tonic-gate 		return (err);
2627a1be23daSdp 	err = newprop(newnode, DTD_ATTR_VALUE, tabptr->zone_attr_value);
26287c478bd9Sstevel@tonic-gate 	if (err != Z_OK)
26297c478bd9Sstevel@tonic-gate 		return (err);
26307c478bd9Sstevel@tonic-gate 	return (Z_OK);
26317c478bd9Sstevel@tonic-gate }
26327c478bd9Sstevel@tonic-gate 
26337c478bd9Sstevel@tonic-gate int
26347c478bd9Sstevel@tonic-gate zonecfg_add_attr(zone_dochandle_t handle, struct zone_attrtab *tabptr)
26357c478bd9Sstevel@tonic-gate {
26367c478bd9Sstevel@tonic-gate 	int err;
26377c478bd9Sstevel@tonic-gate 
26387c478bd9Sstevel@tonic-gate 	if (tabptr == NULL)
26397c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
26407c478bd9Sstevel@tonic-gate 
26417c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
26427c478bd9Sstevel@tonic-gate 		return (err);
26437c478bd9Sstevel@tonic-gate 
26447c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_add_attr_core(handle, tabptr)) != Z_OK)
26457c478bd9Sstevel@tonic-gate 		return (err);
26467c478bd9Sstevel@tonic-gate 
26477c478bd9Sstevel@tonic-gate 	return (Z_OK);
26487c478bd9Sstevel@tonic-gate }
26497c478bd9Sstevel@tonic-gate 
26507c478bd9Sstevel@tonic-gate static int
26517c478bd9Sstevel@tonic-gate zonecfg_delete_attr_core(zone_dochandle_t handle, struct zone_attrtab *tabptr)
26527c478bd9Sstevel@tonic-gate {
26537c478bd9Sstevel@tonic-gate 	xmlNodePtr cur = handle->zone_dh_cur;
26547c478bd9Sstevel@tonic-gate 	int name_match, type_match, value_match;
26557c478bd9Sstevel@tonic-gate 
26567c478bd9Sstevel@tonic-gate 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
26577c478bd9Sstevel@tonic-gate 		if (xmlStrcmp(cur->name, DTD_ELEM_ATTR))
26587c478bd9Sstevel@tonic-gate 			continue;
26597c478bd9Sstevel@tonic-gate 
26607c478bd9Sstevel@tonic-gate 		name_match = match_prop(cur, DTD_ATTR_NAME,
26617c478bd9Sstevel@tonic-gate 		    tabptr->zone_attr_name);
26627c478bd9Sstevel@tonic-gate 		type_match = match_prop(cur, DTD_ATTR_TYPE,
26637c478bd9Sstevel@tonic-gate 		    tabptr->zone_attr_type);
26647c478bd9Sstevel@tonic-gate 		value_match = match_prop(cur, DTD_ATTR_VALUE,
26657c478bd9Sstevel@tonic-gate 		    tabptr->zone_attr_value);
26667c478bd9Sstevel@tonic-gate 
26677c478bd9Sstevel@tonic-gate 		if (name_match && type_match && value_match) {
26687c478bd9Sstevel@tonic-gate 			xmlUnlinkNode(cur);
26697c478bd9Sstevel@tonic-gate 			xmlFreeNode(cur);
26707c478bd9Sstevel@tonic-gate 			return (Z_OK);
26717c478bd9Sstevel@tonic-gate 		}
26727c478bd9Sstevel@tonic-gate 	}
26737c478bd9Sstevel@tonic-gate 	return (Z_NO_RESOURCE_ID);
26747c478bd9Sstevel@tonic-gate }
26757c478bd9Sstevel@tonic-gate 
26767c478bd9Sstevel@tonic-gate int
26777c478bd9Sstevel@tonic-gate zonecfg_delete_attr(zone_dochandle_t handle, struct zone_attrtab *tabptr)
26787c478bd9Sstevel@tonic-gate {
26797c478bd9Sstevel@tonic-gate 	int err;
26807c478bd9Sstevel@tonic-gate 
26817c478bd9Sstevel@tonic-gate 	if (tabptr == NULL)
26827c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
26837c478bd9Sstevel@tonic-gate 
26847c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
26857c478bd9Sstevel@tonic-gate 		return (err);
26867c478bd9Sstevel@tonic-gate 
26877c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_delete_attr_core(handle, tabptr)) != Z_OK)
26887c478bd9Sstevel@tonic-gate 		return (err);
26897c478bd9Sstevel@tonic-gate 
26907c478bd9Sstevel@tonic-gate 	return (Z_OK);
26917c478bd9Sstevel@tonic-gate }
26927c478bd9Sstevel@tonic-gate 
26937c478bd9Sstevel@tonic-gate int
26947c478bd9Sstevel@tonic-gate zonecfg_modify_attr(
26957c478bd9Sstevel@tonic-gate 	zone_dochandle_t handle,
26967c478bd9Sstevel@tonic-gate 	struct zone_attrtab *oldtabptr,
26977c478bd9Sstevel@tonic-gate 	struct zone_attrtab *newtabptr)
26987c478bd9Sstevel@tonic-gate {
26997c478bd9Sstevel@tonic-gate 	int err;
27007c478bd9Sstevel@tonic-gate 
27017c478bd9Sstevel@tonic-gate 	if (oldtabptr == NULL || newtabptr == NULL)
27027c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
27037c478bd9Sstevel@tonic-gate 
27047c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
27057c478bd9Sstevel@tonic-gate 		return (err);
27067c478bd9Sstevel@tonic-gate 
27077c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_delete_attr_core(handle, oldtabptr)) != Z_OK)
27087c478bd9Sstevel@tonic-gate 		return (err);
27097c478bd9Sstevel@tonic-gate 
27107c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_add_attr_core(handle, newtabptr)) != Z_OK)
27117c478bd9Sstevel@tonic-gate 		return (err);
27127c478bd9Sstevel@tonic-gate 
27137c478bd9Sstevel@tonic-gate 	return (Z_OK);
27147c478bd9Sstevel@tonic-gate }
27157c478bd9Sstevel@tonic-gate 
27167c478bd9Sstevel@tonic-gate int
27177c478bd9Sstevel@tonic-gate zonecfg_get_attr_boolean(const struct zone_attrtab *attr, boolean_t *value)
27187c478bd9Sstevel@tonic-gate {
27197c478bd9Sstevel@tonic-gate 	if (attr == NULL)
27207c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
27217c478bd9Sstevel@tonic-gate 
27227c478bd9Sstevel@tonic-gate 	if (strcmp(attr->zone_attr_type, DTD_ENTITY_BOOLEAN) != 0)
27237c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
27247c478bd9Sstevel@tonic-gate 
27257c478bd9Sstevel@tonic-gate 	if (strcmp(attr->zone_attr_value, DTD_ENTITY_TRUE) == 0) {
27267c478bd9Sstevel@tonic-gate 		*value = B_TRUE;
27277c478bd9Sstevel@tonic-gate 		return (Z_OK);
27287c478bd9Sstevel@tonic-gate 	}
27297c478bd9Sstevel@tonic-gate 	if (strcmp(attr->zone_attr_value, DTD_ENTITY_FALSE) == 0) {
27307c478bd9Sstevel@tonic-gate 		*value = B_FALSE;
27317c478bd9Sstevel@tonic-gate 		return (Z_OK);
27327c478bd9Sstevel@tonic-gate 	}
27337c478bd9Sstevel@tonic-gate 	return (Z_INVAL);
27347c478bd9Sstevel@tonic-gate }
27357c478bd9Sstevel@tonic-gate 
27367c478bd9Sstevel@tonic-gate int
27377c478bd9Sstevel@tonic-gate zonecfg_get_attr_int(const struct zone_attrtab *attr, int64_t *value)
27387c478bd9Sstevel@tonic-gate {
27397c478bd9Sstevel@tonic-gate 	long long result;
27407c478bd9Sstevel@tonic-gate 	char *endptr;
27417c478bd9Sstevel@tonic-gate 
27427c478bd9Sstevel@tonic-gate 	if (attr == NULL)
27437c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
27447c478bd9Sstevel@tonic-gate 
27457c478bd9Sstevel@tonic-gate 	if (strcmp(attr->zone_attr_type, DTD_ENTITY_INT) != 0)
27467c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
27477c478bd9Sstevel@tonic-gate 
27487c478bd9Sstevel@tonic-gate 	errno = 0;
27497c478bd9Sstevel@tonic-gate 	result = strtoll(attr->zone_attr_value, &endptr, 10);
27507c478bd9Sstevel@tonic-gate 	if (errno != 0 || *endptr != '\0')
27517c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
27527c478bd9Sstevel@tonic-gate 	*value = result;
27537c478bd9Sstevel@tonic-gate 	return (Z_OK);
27547c478bd9Sstevel@tonic-gate }
27557c478bd9Sstevel@tonic-gate 
27567c478bd9Sstevel@tonic-gate int
27577c478bd9Sstevel@tonic-gate zonecfg_get_attr_string(const struct zone_attrtab *attr, char *value,
27587c478bd9Sstevel@tonic-gate     size_t val_sz)
27597c478bd9Sstevel@tonic-gate {
27607c478bd9Sstevel@tonic-gate 	if (attr == NULL)
27617c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
27627c478bd9Sstevel@tonic-gate 
27637c478bd9Sstevel@tonic-gate 	if (strcmp(attr->zone_attr_type, DTD_ENTITY_STRING) != 0)
27647c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
27657c478bd9Sstevel@tonic-gate 
27667c478bd9Sstevel@tonic-gate 	if (strlcpy(value, attr->zone_attr_value, val_sz) >= val_sz)
27677c478bd9Sstevel@tonic-gate 		return (Z_TOO_BIG);
27687c478bd9Sstevel@tonic-gate 	return (Z_OK);
27697c478bd9Sstevel@tonic-gate }
27707c478bd9Sstevel@tonic-gate 
27717c478bd9Sstevel@tonic-gate int
27727c478bd9Sstevel@tonic-gate zonecfg_get_attr_uint(const struct zone_attrtab *attr, uint64_t *value)
27737c478bd9Sstevel@tonic-gate {
27747c478bd9Sstevel@tonic-gate 	unsigned long long result;
27757c478bd9Sstevel@tonic-gate 	long long neg_result;
27767c478bd9Sstevel@tonic-gate 	char *endptr;
27777c478bd9Sstevel@tonic-gate 
27787c478bd9Sstevel@tonic-gate 	if (attr == NULL)
27797c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
27807c478bd9Sstevel@tonic-gate 
27817c478bd9Sstevel@tonic-gate 	if (strcmp(attr->zone_attr_type, DTD_ENTITY_UINT) != 0)
27827c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
27837c478bd9Sstevel@tonic-gate 
27847c478bd9Sstevel@tonic-gate 	errno = 0;
27857c478bd9Sstevel@tonic-gate 	result = strtoull(attr->zone_attr_value, &endptr, 10);
27867c478bd9Sstevel@tonic-gate 	if (errno != 0 || *endptr != '\0')
27877c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
27887c478bd9Sstevel@tonic-gate 	errno = 0;
27897c478bd9Sstevel@tonic-gate 	neg_result = strtoll(attr->zone_attr_value, &endptr, 10);
27907c478bd9Sstevel@tonic-gate 	/*
27917c478bd9Sstevel@tonic-gate 	 * Incredibly, strtoull("<negative number>", ...) will not fail but
27927c478bd9Sstevel@tonic-gate 	 * return whatever (negative) number cast as a u_longlong_t, so we
27937c478bd9Sstevel@tonic-gate 	 * need to look for this here.
27947c478bd9Sstevel@tonic-gate 	 */
27957c478bd9Sstevel@tonic-gate 	if (errno == 0 && neg_result < 0)
27967c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
27977c478bd9Sstevel@tonic-gate 	*value = result;
27987c478bd9Sstevel@tonic-gate 	return (Z_OK);
27997c478bd9Sstevel@tonic-gate }
28007c478bd9Sstevel@tonic-gate 
28017c478bd9Sstevel@tonic-gate int
28027c478bd9Sstevel@tonic-gate zonecfg_lookup_rctl(zone_dochandle_t handle, struct zone_rctltab *tabptr)
28037c478bd9Sstevel@tonic-gate {
28047c478bd9Sstevel@tonic-gate 	xmlNodePtr cur, val;
28057c478bd9Sstevel@tonic-gate 	char savedname[MAXNAMELEN];
28067c478bd9Sstevel@tonic-gate 	struct zone_rctlvaltab *valptr;
28077c478bd9Sstevel@tonic-gate 	int err;
28087c478bd9Sstevel@tonic-gate 
28097c478bd9Sstevel@tonic-gate 	if (tabptr->zone_rctl_name == NULL ||
28107c478bd9Sstevel@tonic-gate 	    strlen(tabptr->zone_rctl_name) == 0)
28117c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
28127c478bd9Sstevel@tonic-gate 
28137c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
28147c478bd9Sstevel@tonic-gate 		return (err);
28157c478bd9Sstevel@tonic-gate 
28167c478bd9Sstevel@tonic-gate 	cur = handle->zone_dh_cur;
28177c478bd9Sstevel@tonic-gate 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
28187c478bd9Sstevel@tonic-gate 		if (xmlStrcmp(cur->name, DTD_ELEM_RCTL))
28197c478bd9Sstevel@tonic-gate 			continue;
28207c478bd9Sstevel@tonic-gate 		if ((fetchprop(cur, DTD_ATTR_NAME, savedname,
28217c478bd9Sstevel@tonic-gate 		    sizeof (savedname)) == Z_OK) &&
28227c478bd9Sstevel@tonic-gate 		    (strcmp(savedname, tabptr->zone_rctl_name) == 0)) {
28237c478bd9Sstevel@tonic-gate 			tabptr->zone_rctl_valptr = NULL;
28247c478bd9Sstevel@tonic-gate 			for (val = cur->xmlChildrenNode; val != NULL;
28257c478bd9Sstevel@tonic-gate 			    val = val->next) {
28267c478bd9Sstevel@tonic-gate 				valptr = (struct zone_rctlvaltab *)malloc(
28277c478bd9Sstevel@tonic-gate 				    sizeof (struct zone_rctlvaltab));
28287c478bd9Sstevel@tonic-gate 				if (valptr == NULL)
28297c478bd9Sstevel@tonic-gate 					return (Z_NOMEM);
28307c478bd9Sstevel@tonic-gate 				if ((fetchprop(val, DTD_ATTR_PRIV,
28317c478bd9Sstevel@tonic-gate 				    valptr->zone_rctlval_priv,
28327c478bd9Sstevel@tonic-gate 				    sizeof (valptr->zone_rctlval_priv)) !=
28337c478bd9Sstevel@tonic-gate 				    Z_OK))
28347c478bd9Sstevel@tonic-gate 					break;
28357c478bd9Sstevel@tonic-gate 				if ((fetchprop(val, DTD_ATTR_LIMIT,
28367c478bd9Sstevel@tonic-gate 				    valptr->zone_rctlval_limit,
28377c478bd9Sstevel@tonic-gate 				    sizeof (valptr->zone_rctlval_limit)) !=
28387c478bd9Sstevel@tonic-gate 				    Z_OK))
28397c478bd9Sstevel@tonic-gate 					break;
28407c478bd9Sstevel@tonic-gate 				if ((fetchprop(val, DTD_ATTR_ACTION,
28417c478bd9Sstevel@tonic-gate 				    valptr->zone_rctlval_action,
28427c478bd9Sstevel@tonic-gate 				    sizeof (valptr->zone_rctlval_action)) !=
28437c478bd9Sstevel@tonic-gate 				    Z_OK))
28447c478bd9Sstevel@tonic-gate 					break;
28457c478bd9Sstevel@tonic-gate 				if (zonecfg_add_rctl_value(tabptr, valptr) !=
28467c478bd9Sstevel@tonic-gate 				    Z_OK)
28477c478bd9Sstevel@tonic-gate 					break;
28487c478bd9Sstevel@tonic-gate 			}
28497c478bd9Sstevel@tonic-gate 			return (Z_OK);
28507c478bd9Sstevel@tonic-gate 		}
28517c478bd9Sstevel@tonic-gate 	}
28527c478bd9Sstevel@tonic-gate 	return (Z_NO_RESOURCE_ID);
28537c478bd9Sstevel@tonic-gate }
28547c478bd9Sstevel@tonic-gate 
28557c478bd9Sstevel@tonic-gate static int
28567c478bd9Sstevel@tonic-gate zonecfg_add_rctl_core(zone_dochandle_t handle, struct zone_rctltab *tabptr)
28577c478bd9Sstevel@tonic-gate {
28587c478bd9Sstevel@tonic-gate 	xmlNodePtr newnode, cur = handle->zone_dh_cur, valnode;
28597c478bd9Sstevel@tonic-gate 	struct zone_rctlvaltab *valptr;
28607c478bd9Sstevel@tonic-gate 	int err;
28617c478bd9Sstevel@tonic-gate 
28627c478bd9Sstevel@tonic-gate 	newnode = xmlNewTextChild(cur, NULL, DTD_ELEM_RCTL, NULL);
2863a1be23daSdp 	err = newprop(newnode, DTD_ATTR_NAME, tabptr->zone_rctl_name);
28647c478bd9Sstevel@tonic-gate 	if (err != Z_OK)
28657c478bd9Sstevel@tonic-gate 		return (err);
28667c478bd9Sstevel@tonic-gate 	for (valptr = tabptr->zone_rctl_valptr; valptr != NULL;
28677c478bd9Sstevel@tonic-gate 	    valptr = valptr->zone_rctlval_next) {
28687c478bd9Sstevel@tonic-gate 		valnode = xmlNewTextChild(newnode, NULL,
28697c478bd9Sstevel@tonic-gate 		    DTD_ELEM_RCTLVALUE, NULL);
2870a1be23daSdp 		err = newprop(valnode, DTD_ATTR_PRIV,
28717c478bd9Sstevel@tonic-gate 		    valptr->zone_rctlval_priv);
28727c478bd9Sstevel@tonic-gate 		if (err != Z_OK)
28737c478bd9Sstevel@tonic-gate 			return (err);
2874a1be23daSdp 		err = newprop(valnode, DTD_ATTR_LIMIT,
28757c478bd9Sstevel@tonic-gate 		    valptr->zone_rctlval_limit);
28767c478bd9Sstevel@tonic-gate 		if (err != Z_OK)
28777c478bd9Sstevel@tonic-gate 			return (err);
2878a1be23daSdp 		err = newprop(valnode, DTD_ATTR_ACTION,
28797c478bd9Sstevel@tonic-gate 		    valptr->zone_rctlval_action);
28807c478bd9Sstevel@tonic-gate 		if (err != Z_OK)
28817c478bd9Sstevel@tonic-gate 			return (err);
28827c478bd9Sstevel@tonic-gate 	}
28837c478bd9Sstevel@tonic-gate 	return (Z_OK);
28847c478bd9Sstevel@tonic-gate }
28857c478bd9Sstevel@tonic-gate 
28867c478bd9Sstevel@tonic-gate int
28877c478bd9Sstevel@tonic-gate zonecfg_add_rctl(zone_dochandle_t handle, struct zone_rctltab *tabptr)
28887c478bd9Sstevel@tonic-gate {
28897c478bd9Sstevel@tonic-gate 	int err;
28907c478bd9Sstevel@tonic-gate 
28917c478bd9Sstevel@tonic-gate 	if (tabptr == NULL || tabptr->zone_rctl_name == NULL)
28927c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
28937c478bd9Sstevel@tonic-gate 
28947c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
28957c478bd9Sstevel@tonic-gate 		return (err);
28967c478bd9Sstevel@tonic-gate 
28977c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_add_rctl_core(handle, tabptr)) != Z_OK)
28987c478bd9Sstevel@tonic-gate 		return (err);
28997c478bd9Sstevel@tonic-gate 
29007c478bd9Sstevel@tonic-gate 	return (Z_OK);
29017c478bd9Sstevel@tonic-gate }
29027c478bd9Sstevel@tonic-gate 
29037c478bd9Sstevel@tonic-gate static int
29047c478bd9Sstevel@tonic-gate zonecfg_delete_rctl_core(zone_dochandle_t handle, struct zone_rctltab *tabptr)
29057c478bd9Sstevel@tonic-gate {
29067c478bd9Sstevel@tonic-gate 	xmlNodePtr cur = handle->zone_dh_cur;
29077c478bd9Sstevel@tonic-gate 	xmlChar *savedname;
29087c478bd9Sstevel@tonic-gate 	int name_result;
29097c478bd9Sstevel@tonic-gate 
29107c478bd9Sstevel@tonic-gate 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
29117c478bd9Sstevel@tonic-gate 		if (xmlStrcmp(cur->name, DTD_ELEM_RCTL))
29127c478bd9Sstevel@tonic-gate 			continue;
29137c478bd9Sstevel@tonic-gate 
29147c478bd9Sstevel@tonic-gate 		savedname = xmlGetProp(cur, DTD_ATTR_NAME);
29157c478bd9Sstevel@tonic-gate 		if (savedname == NULL)	/* shouldn't happen */
29167c478bd9Sstevel@tonic-gate 			continue;
29177c478bd9Sstevel@tonic-gate 		name_result = xmlStrcmp(savedname,
29187c478bd9Sstevel@tonic-gate 		    (const xmlChar *) tabptr->zone_rctl_name);
29197c478bd9Sstevel@tonic-gate 		xmlFree(savedname);
29207c478bd9Sstevel@tonic-gate 
29217c478bd9Sstevel@tonic-gate 		if (name_result == 0) {
29227c478bd9Sstevel@tonic-gate 			xmlUnlinkNode(cur);
29237c478bd9Sstevel@tonic-gate 			xmlFreeNode(cur);
29247c478bd9Sstevel@tonic-gate 			return (Z_OK);
29257c478bd9Sstevel@tonic-gate 		}
29267c478bd9Sstevel@tonic-gate 	}
29277c478bd9Sstevel@tonic-gate 	return (Z_NO_RESOURCE_ID);
29287c478bd9Sstevel@tonic-gate }
29297c478bd9Sstevel@tonic-gate 
29307c478bd9Sstevel@tonic-gate int
29317c478bd9Sstevel@tonic-gate zonecfg_delete_rctl(zone_dochandle_t handle, struct zone_rctltab *tabptr)
29327c478bd9Sstevel@tonic-gate {
29337c478bd9Sstevel@tonic-gate 	int err;
29347c478bd9Sstevel@tonic-gate 
29357c478bd9Sstevel@tonic-gate 	if (tabptr == NULL || tabptr->zone_rctl_name == NULL)
29367c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
29377c478bd9Sstevel@tonic-gate 
29387c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
29397c478bd9Sstevel@tonic-gate 		return (err);
29407c478bd9Sstevel@tonic-gate 
29417c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_delete_rctl_core(handle, tabptr)) != Z_OK)
29427c478bd9Sstevel@tonic-gate 		return (err);
29437c478bd9Sstevel@tonic-gate 
29447c478bd9Sstevel@tonic-gate 	return (Z_OK);
29457c478bd9Sstevel@tonic-gate }
29467c478bd9Sstevel@tonic-gate 
29477c478bd9Sstevel@tonic-gate int
29487c478bd9Sstevel@tonic-gate zonecfg_modify_rctl(
29497c478bd9Sstevel@tonic-gate 	zone_dochandle_t handle,
29507c478bd9Sstevel@tonic-gate 	struct zone_rctltab *oldtabptr,
29517c478bd9Sstevel@tonic-gate 	struct zone_rctltab *newtabptr)
29527c478bd9Sstevel@tonic-gate {
29537c478bd9Sstevel@tonic-gate 	int err;
29547c478bd9Sstevel@tonic-gate 
29557c478bd9Sstevel@tonic-gate 	if (oldtabptr == NULL || oldtabptr->zone_rctl_name == NULL ||
29567c478bd9Sstevel@tonic-gate 	    newtabptr == NULL || newtabptr->zone_rctl_name == NULL)
29577c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
29587c478bd9Sstevel@tonic-gate 
29597c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
29607c478bd9Sstevel@tonic-gate 		return (err);
29617c478bd9Sstevel@tonic-gate 
29627c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_delete_rctl_core(handle, oldtabptr)) != Z_OK)
29637c478bd9Sstevel@tonic-gate 		return (err);
29647c478bd9Sstevel@tonic-gate 
29657c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_add_rctl_core(handle, newtabptr)) != Z_OK)
29667c478bd9Sstevel@tonic-gate 		return (err);
29677c478bd9Sstevel@tonic-gate 
29687c478bd9Sstevel@tonic-gate 	return (Z_OK);
29697c478bd9Sstevel@tonic-gate }
29707c478bd9Sstevel@tonic-gate 
29717c478bd9Sstevel@tonic-gate int
29727c478bd9Sstevel@tonic-gate zonecfg_add_rctl_value(
29737c478bd9Sstevel@tonic-gate 	struct zone_rctltab *tabptr,
29747c478bd9Sstevel@tonic-gate 	struct zone_rctlvaltab *valtabptr)
29757c478bd9Sstevel@tonic-gate {
29767c478bd9Sstevel@tonic-gate 	struct zone_rctlvaltab *last, *old, *new;
29777c478bd9Sstevel@tonic-gate 	rctlblk_t *rctlblk = alloca(rctlblk_size());
29787c478bd9Sstevel@tonic-gate 
29797c478bd9Sstevel@tonic-gate 	last = tabptr->zone_rctl_valptr;
29807c478bd9Sstevel@tonic-gate 	for (old = last; old != NULL; old = old->zone_rctlval_next)
29817c478bd9Sstevel@tonic-gate 		last = old;	/* walk to the end of the list */
29827c478bd9Sstevel@tonic-gate 	new = valtabptr;	/* alloc'd by caller */
29837c478bd9Sstevel@tonic-gate 	new->zone_rctlval_next = NULL;
29847c478bd9Sstevel@tonic-gate 	if (zonecfg_construct_rctlblk(valtabptr, rctlblk) != Z_OK)
29857c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
29867c478bd9Sstevel@tonic-gate 	if (!zonecfg_valid_rctlblk(rctlblk))
29877c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
29887c478bd9Sstevel@tonic-gate 	if (last == NULL)
29897c478bd9Sstevel@tonic-gate 		tabptr->zone_rctl_valptr = new;
29907c478bd9Sstevel@tonic-gate 	else
29917c478bd9Sstevel@tonic-gate 		last->zone_rctlval_next = new;
29927c478bd9Sstevel@tonic-gate 	return (Z_OK);
29937c478bd9Sstevel@tonic-gate }
29947c478bd9Sstevel@tonic-gate 
29957c478bd9Sstevel@tonic-gate int
29967c478bd9Sstevel@tonic-gate zonecfg_remove_rctl_value(
29977c478bd9Sstevel@tonic-gate 	struct zone_rctltab *tabptr,
29987c478bd9Sstevel@tonic-gate 	struct zone_rctlvaltab *valtabptr)
29997c478bd9Sstevel@tonic-gate {
30007c478bd9Sstevel@tonic-gate 	struct zone_rctlvaltab *last, *this, *next;
30017c478bd9Sstevel@tonic-gate 
30027c478bd9Sstevel@tonic-gate 	last = tabptr->zone_rctl_valptr;
30037c478bd9Sstevel@tonic-gate 	for (this = last; this != NULL; this = this->zone_rctlval_next) {
30047c478bd9Sstevel@tonic-gate 		if (strcmp(this->zone_rctlval_priv,
30057c478bd9Sstevel@tonic-gate 		    valtabptr->zone_rctlval_priv) == 0 &&
30067c478bd9Sstevel@tonic-gate 		    strcmp(this->zone_rctlval_limit,
30077c478bd9Sstevel@tonic-gate 		    valtabptr->zone_rctlval_limit) == 0 &&
30087c478bd9Sstevel@tonic-gate 		    strcmp(this->zone_rctlval_action,
30097c478bd9Sstevel@tonic-gate 		    valtabptr->zone_rctlval_action) == 0) {
30107c478bd9Sstevel@tonic-gate 			next = this->zone_rctlval_next;
30117c478bd9Sstevel@tonic-gate 			if (this == tabptr->zone_rctl_valptr)
30127c478bd9Sstevel@tonic-gate 				tabptr->zone_rctl_valptr = next;
30137c478bd9Sstevel@tonic-gate 			else
30147c478bd9Sstevel@tonic-gate 				last->zone_rctlval_next = next;
30157c478bd9Sstevel@tonic-gate 			free(this);
30167c478bd9Sstevel@tonic-gate 			return (Z_OK);
30177c478bd9Sstevel@tonic-gate 		} else
30187c478bd9Sstevel@tonic-gate 			last = this;
30197c478bd9Sstevel@tonic-gate 	}
30207c478bd9Sstevel@tonic-gate 	return (Z_NO_PROPERTY_ID);
30217c478bd9Sstevel@tonic-gate }
30227c478bd9Sstevel@tonic-gate 
30237c478bd9Sstevel@tonic-gate char *
30247c478bd9Sstevel@tonic-gate zonecfg_strerror(int errnum)
30257c478bd9Sstevel@tonic-gate {
30267c478bd9Sstevel@tonic-gate 	switch (errnum) {
30277c478bd9Sstevel@tonic-gate 	case Z_OK:
30287c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "OK"));
30297c478bd9Sstevel@tonic-gate 	case Z_EMPTY_DOCUMENT:
30307c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Empty document"));
30317c478bd9Sstevel@tonic-gate 	case Z_WRONG_DOC_TYPE:
30327c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Wrong document type"));
30337c478bd9Sstevel@tonic-gate 	case Z_BAD_PROPERTY:
30347c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Bad document property"));
30357c478bd9Sstevel@tonic-gate 	case Z_TEMP_FILE:
30367c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN,
30377c478bd9Sstevel@tonic-gate 		    "Problem creating temporary file"));
30387c478bd9Sstevel@tonic-gate 	case Z_SAVING_FILE:
30397c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Problem saving file"));
30407c478bd9Sstevel@tonic-gate 	case Z_NO_ENTRY:
30417c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "No such entry"));
30427c478bd9Sstevel@tonic-gate 	case Z_BOGUS_ZONE_NAME:
30437c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Bogus zone name"));
30447c478bd9Sstevel@tonic-gate 	case Z_REQD_RESOURCE_MISSING:
30457c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Required resource missing"));
30467c478bd9Sstevel@tonic-gate 	case Z_REQD_PROPERTY_MISSING:
30477c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Required property missing"));
30487c478bd9Sstevel@tonic-gate 	case Z_BAD_HANDLE:
30497c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Bad handle"));
30507c478bd9Sstevel@tonic-gate 	case Z_NOMEM:
30517c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Out of memory"));
30527c478bd9Sstevel@tonic-gate 	case Z_INVAL:
30537c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Invalid argument"));
30547c478bd9Sstevel@tonic-gate 	case Z_ACCES:
30557c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Permission denied"));
30567c478bd9Sstevel@tonic-gate 	case Z_TOO_BIG:
30577c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Argument list too long"));
30587c478bd9Sstevel@tonic-gate 	case Z_MISC_FS:
30597c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN,
30607c478bd9Sstevel@tonic-gate 		    "Miscellaneous file system error"));
30617c478bd9Sstevel@tonic-gate 	case Z_NO_ZONE:
30627c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "No such zone configured"));
30637c478bd9Sstevel@tonic-gate 	case Z_NO_RESOURCE_TYPE:
30647c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "No such resource type"));
30657c478bd9Sstevel@tonic-gate 	case Z_NO_RESOURCE_ID:
30667c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "No such resource with that id"));
30677c478bd9Sstevel@tonic-gate 	case Z_NO_PROPERTY_TYPE:
30687c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "No such property type"));
30697c478bd9Sstevel@tonic-gate 	case Z_NO_PROPERTY_ID:
30707c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "No such property with that id"));
3071087719fdSdp 	case Z_BAD_ZONE_STATE:
30727c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN,
3073087719fdSdp 		    "Zone state is invalid for the requested operation"));
30747c478bd9Sstevel@tonic-gate 	case Z_INVALID_DOCUMENT:
30757c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Invalid document"));
3076087719fdSdp 	case Z_NAME_IN_USE:
3077087719fdSdp 		return (dgettext(TEXT_DOMAIN, "Zone name already in use"));
30787c478bd9Sstevel@tonic-gate 	case Z_NO_SUCH_ID:
30797c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "No such zone ID"));
30807c478bd9Sstevel@tonic-gate 	case Z_UPDATING_INDEX:
30817c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Problem updating index file"));
30827c478bd9Sstevel@tonic-gate 	case Z_LOCKING_FILE:
30837c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Locking index file"));
30847c478bd9Sstevel@tonic-gate 	case Z_UNLOCKING_FILE:
30857c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Unlocking index file"));
30867c478bd9Sstevel@tonic-gate 	case Z_INSUFFICIENT_SPEC:
30877c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Insufficient specification"));
30887c478bd9Sstevel@tonic-gate 	case Z_RESOLVED_PATH:
30897c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Resolved path mismatch"));
30907c478bd9Sstevel@tonic-gate 	case Z_IPV6_ADDR_PREFIX_LEN:
30917c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN,
30927c478bd9Sstevel@tonic-gate 		    "IPv6 address missing required prefix length"));
30937c478bd9Sstevel@tonic-gate 	case Z_BOGUS_ADDRESS:
30947c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN,
30957c478bd9Sstevel@tonic-gate 		    "Neither an IPv4 nor an IPv6 address nor a host name"));
3096ffbafc53Scomay 	case Z_PRIV_PROHIBITED:
3097ffbafc53Scomay 		return (dgettext(TEXT_DOMAIN,
3098ffbafc53Scomay 		    "Specified privilege is prohibited"));
3099ffbafc53Scomay 	case Z_PRIV_REQUIRED:
3100ffbafc53Scomay 		return (dgettext(TEXT_DOMAIN,
3101ffbafc53Scomay 		    "Required privilege is missing"));
3102ffbafc53Scomay 	case Z_PRIV_UNKNOWN:
3103ffbafc53Scomay 		return (dgettext(TEXT_DOMAIN,
3104ffbafc53Scomay 		    "Specified privilege is unknown"));
31057c478bd9Sstevel@tonic-gate 	default:
31067c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Unknown error"));
31077c478bd9Sstevel@tonic-gate 	}
31087c478bd9Sstevel@tonic-gate }
31097c478bd9Sstevel@tonic-gate 
31107c478bd9Sstevel@tonic-gate /*
31117c478bd9Sstevel@tonic-gate  * Note that the zonecfg_setXent() and zonecfg_endXent() calls are all the
31127c478bd9Sstevel@tonic-gate  * same, as they just turn around and call zonecfg_setent() / zonecfg_endent().
31137c478bd9Sstevel@tonic-gate  */
31147c478bd9Sstevel@tonic-gate 
31157c478bd9Sstevel@tonic-gate static int
31167c478bd9Sstevel@tonic-gate zonecfg_setent(zone_dochandle_t handle)
31177c478bd9Sstevel@tonic-gate {
31187c478bd9Sstevel@tonic-gate 	xmlNodePtr cur;
31197c478bd9Sstevel@tonic-gate 	int err;
31207c478bd9Sstevel@tonic-gate 
31217c478bd9Sstevel@tonic-gate 	if (handle == NULL)
31227c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
31237c478bd9Sstevel@tonic-gate 
31247c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK) {
31257c478bd9Sstevel@tonic-gate 		handle->zone_dh_cur = NULL;
31267c478bd9Sstevel@tonic-gate 		return (err);
31277c478bd9Sstevel@tonic-gate 	}
31287c478bd9Sstevel@tonic-gate 	cur = handle->zone_dh_cur;
31297c478bd9Sstevel@tonic-gate 	cur = cur->xmlChildrenNode;
31307c478bd9Sstevel@tonic-gate 	handle->zone_dh_cur = cur;
31317c478bd9Sstevel@tonic-gate 	return (Z_OK);
31327c478bd9Sstevel@tonic-gate }
31337c478bd9Sstevel@tonic-gate 
31347c478bd9Sstevel@tonic-gate static int
31357c478bd9Sstevel@tonic-gate zonecfg_endent(zone_dochandle_t handle)
31367c478bd9Sstevel@tonic-gate {
31377c478bd9Sstevel@tonic-gate 	if (handle == NULL)
31387c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
31397c478bd9Sstevel@tonic-gate 
31407c478bd9Sstevel@tonic-gate 	handle->zone_dh_cur = handle->zone_dh_top;
31417c478bd9Sstevel@tonic-gate 	return (Z_OK);
31427c478bd9Sstevel@tonic-gate }
31437c478bd9Sstevel@tonic-gate 
31447c478bd9Sstevel@tonic-gate int
31457c478bd9Sstevel@tonic-gate zonecfg_setfsent(zone_dochandle_t handle)
31467c478bd9Sstevel@tonic-gate {
31477c478bd9Sstevel@tonic-gate 	return (zonecfg_setent(handle));
31487c478bd9Sstevel@tonic-gate }
31497c478bd9Sstevel@tonic-gate 
31507c478bd9Sstevel@tonic-gate int
31517c478bd9Sstevel@tonic-gate zonecfg_getfsent(zone_dochandle_t handle, struct zone_fstab *tabptr)
31527c478bd9Sstevel@tonic-gate {
31537c478bd9Sstevel@tonic-gate 	xmlNodePtr cur, options;
31547c478bd9Sstevel@tonic-gate 	char options_str[MAX_MNTOPT_STR];
31557c478bd9Sstevel@tonic-gate 	int err;
31567c478bd9Sstevel@tonic-gate 
31577c478bd9Sstevel@tonic-gate 	if (handle == NULL)
31587c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
31597c478bd9Sstevel@tonic-gate 
31607c478bd9Sstevel@tonic-gate 	if ((cur = handle->zone_dh_cur) == NULL)
31617c478bd9Sstevel@tonic-gate 		return (Z_NO_ENTRY);
31627c478bd9Sstevel@tonic-gate 
31637c478bd9Sstevel@tonic-gate 	for (; cur != NULL; cur = cur->next)
31647c478bd9Sstevel@tonic-gate 		if (!xmlStrcmp(cur->name, DTD_ELEM_FS))
31657c478bd9Sstevel@tonic-gate 			break;
31667c478bd9Sstevel@tonic-gate 	if (cur == NULL) {
31677c478bd9Sstevel@tonic-gate 		handle->zone_dh_cur = handle->zone_dh_top;
31687c478bd9Sstevel@tonic-gate 		return (Z_NO_ENTRY);
31697c478bd9Sstevel@tonic-gate 	}
31707c478bd9Sstevel@tonic-gate 
31717c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_SPECIAL, tabptr->zone_fs_special,
31727c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_fs_special))) != Z_OK) {
31737c478bd9Sstevel@tonic-gate 		handle->zone_dh_cur = handle->zone_dh_top;
31747c478bd9Sstevel@tonic-gate 		return (err);
31757c478bd9Sstevel@tonic-gate 	}
31767c478bd9Sstevel@tonic-gate 
31777c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_RAW, tabptr->zone_fs_raw,
31787c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_fs_raw))) != Z_OK) {
31797c478bd9Sstevel@tonic-gate 		handle->zone_dh_cur = handle->zone_dh_top;
31807c478bd9Sstevel@tonic-gate 		return (err);
31817c478bd9Sstevel@tonic-gate 	}
31827c478bd9Sstevel@tonic-gate 
31837c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_DIR, tabptr->zone_fs_dir,
31847c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_fs_dir))) != Z_OK) {
31857c478bd9Sstevel@tonic-gate 		handle->zone_dh_cur = handle->zone_dh_top;
31867c478bd9Sstevel@tonic-gate 		return (err);
31877c478bd9Sstevel@tonic-gate 	}
31887c478bd9Sstevel@tonic-gate 
31897c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_TYPE, tabptr->zone_fs_type,
31907c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_fs_type))) != Z_OK) {
31917c478bd9Sstevel@tonic-gate 		handle->zone_dh_cur = handle->zone_dh_top;
31927c478bd9Sstevel@tonic-gate 		return (err);
31937c478bd9Sstevel@tonic-gate 	}
31947c478bd9Sstevel@tonic-gate 
31957c478bd9Sstevel@tonic-gate 	/* OK for options to be NULL */
31967c478bd9Sstevel@tonic-gate 	tabptr->zone_fs_options = NULL;
31977c478bd9Sstevel@tonic-gate 	for (options = cur->xmlChildrenNode; options != NULL;
31987c478bd9Sstevel@tonic-gate 	    options = options->next) {
31997c478bd9Sstevel@tonic-gate 		if (fetchprop(options, DTD_ATTR_NAME, options_str,
32007c478bd9Sstevel@tonic-gate 		    sizeof (options_str)) != Z_OK)
32017c478bd9Sstevel@tonic-gate 			break;
32027c478bd9Sstevel@tonic-gate 		if (zonecfg_add_fs_option(tabptr, options_str) != Z_OK)
32037c478bd9Sstevel@tonic-gate 			break;
32047c478bd9Sstevel@tonic-gate 	}
32057c478bd9Sstevel@tonic-gate 
32067c478bd9Sstevel@tonic-gate 	handle->zone_dh_cur = cur->next;
32077c478bd9Sstevel@tonic-gate 	return (Z_OK);
32087c478bd9Sstevel@tonic-gate }
32097c478bd9Sstevel@tonic-gate 
32107c478bd9Sstevel@tonic-gate int
32117c478bd9Sstevel@tonic-gate zonecfg_endfsent(zone_dochandle_t handle)
32127c478bd9Sstevel@tonic-gate {
32137c478bd9Sstevel@tonic-gate 	return (zonecfg_endent(handle));
32147c478bd9Sstevel@tonic-gate }
32157c478bd9Sstevel@tonic-gate 
32167c478bd9Sstevel@tonic-gate int
32177c478bd9Sstevel@tonic-gate zonecfg_setipdent(zone_dochandle_t handle)
32187c478bd9Sstevel@tonic-gate {
32197c478bd9Sstevel@tonic-gate 	return (zonecfg_setent(handle));
32207c478bd9Sstevel@tonic-gate }
32217c478bd9Sstevel@tonic-gate 
32227c478bd9Sstevel@tonic-gate int
32237c478bd9Sstevel@tonic-gate zonecfg_getipdent(zone_dochandle_t handle, struct zone_fstab *tabptr)
32247c478bd9Sstevel@tonic-gate {
32257c478bd9Sstevel@tonic-gate 	xmlNodePtr cur;
32267c478bd9Sstevel@tonic-gate 	int err;
32277c478bd9Sstevel@tonic-gate 
32287c478bd9Sstevel@tonic-gate 	if (handle == NULL)
32297c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
32307c478bd9Sstevel@tonic-gate 
32317c478bd9Sstevel@tonic-gate 	if ((cur = handle->zone_dh_cur) == NULL)
32327c478bd9Sstevel@tonic-gate 		return (Z_NO_ENTRY);
32337c478bd9Sstevel@tonic-gate 
32347c478bd9Sstevel@tonic-gate 	for (; cur != NULL; cur = cur->next)
32357c478bd9Sstevel@tonic-gate 		if (!xmlStrcmp(cur->name, DTD_ELEM_IPD))
32367c478bd9Sstevel@tonic-gate 			break;
32377c478bd9Sstevel@tonic-gate 	if (cur == NULL) {
32387c478bd9Sstevel@tonic-gate 		handle->zone_dh_cur = handle->zone_dh_top;
32397c478bd9Sstevel@tonic-gate 		return (Z_NO_ENTRY);
32407c478bd9Sstevel@tonic-gate 	}
32417c478bd9Sstevel@tonic-gate 
32427c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_DIR, tabptr->zone_fs_dir,
32437c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_fs_dir))) != Z_OK) {
32447c478bd9Sstevel@tonic-gate 		handle->zone_dh_cur = handle->zone_dh_top;
32457c478bd9Sstevel@tonic-gate 		return (err);
32467c478bd9Sstevel@tonic-gate 	}
32477c478bd9Sstevel@tonic-gate 
32487c478bd9Sstevel@tonic-gate 	handle->zone_dh_cur = cur->next;
32497c478bd9Sstevel@tonic-gate 	return (Z_OK);
32507c478bd9Sstevel@tonic-gate }
32517c478bd9Sstevel@tonic-gate 
32527c478bd9Sstevel@tonic-gate int
32537c478bd9Sstevel@tonic-gate zonecfg_endipdent(zone_dochandle_t handle)
32547c478bd9Sstevel@tonic-gate {
32557c478bd9Sstevel@tonic-gate 	return (zonecfg_endent(handle));
32567c478bd9Sstevel@tonic-gate }
32577c478bd9Sstevel@tonic-gate 
32587c478bd9Sstevel@tonic-gate int
32597c478bd9Sstevel@tonic-gate zonecfg_setnwifent(zone_dochandle_t handle)
32607c478bd9Sstevel@tonic-gate {
32617c478bd9Sstevel@tonic-gate 	return (zonecfg_setent(handle));
32627c478bd9Sstevel@tonic-gate }
32637c478bd9Sstevel@tonic-gate 
32647c478bd9Sstevel@tonic-gate int
32657c478bd9Sstevel@tonic-gate zonecfg_getnwifent(zone_dochandle_t handle, struct zone_nwiftab *tabptr)
32667c478bd9Sstevel@tonic-gate {
32677c478bd9Sstevel@tonic-gate 	xmlNodePtr cur;
32687c478bd9Sstevel@tonic-gate 	int err;
32697c478bd9Sstevel@tonic-gate 
32707c478bd9Sstevel@tonic-gate 	if (handle == NULL)
32717c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
32727c478bd9Sstevel@tonic-gate 
32737c478bd9Sstevel@tonic-gate 	if ((cur = handle->zone_dh_cur) == NULL)
32747c478bd9Sstevel@tonic-gate 		return (Z_NO_ENTRY);
32757c478bd9Sstevel@tonic-gate 
32767c478bd9Sstevel@tonic-gate 	for (; cur != NULL; cur = cur->next)
32777c478bd9Sstevel@tonic-gate 		if (!xmlStrcmp(cur->name, DTD_ELEM_NET))
32787c478bd9Sstevel@tonic-gate 			break;
32797c478bd9Sstevel@tonic-gate 	if (cur == NULL) {
32807c478bd9Sstevel@tonic-gate 		handle->zone_dh_cur = handle->zone_dh_top;
32817c478bd9Sstevel@tonic-gate 		return (Z_NO_ENTRY);
32827c478bd9Sstevel@tonic-gate 	}
32837c478bd9Sstevel@tonic-gate 
32847c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_ADDRESS, tabptr->zone_nwif_address,
32857c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_nwif_address))) != Z_OK) {
32867c478bd9Sstevel@tonic-gate 		handle->zone_dh_cur = handle->zone_dh_top;
32877c478bd9Sstevel@tonic-gate 		return (err);
32887c478bd9Sstevel@tonic-gate 	}
32897c478bd9Sstevel@tonic-gate 
32907c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_PHYSICAL, tabptr->zone_nwif_physical,
32917c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_nwif_physical))) != Z_OK) {
32927c478bd9Sstevel@tonic-gate 		handle->zone_dh_cur = handle->zone_dh_top;
32937c478bd9Sstevel@tonic-gate 		return (err);
32947c478bd9Sstevel@tonic-gate 	}
32957c478bd9Sstevel@tonic-gate 
32967c478bd9Sstevel@tonic-gate 	handle->zone_dh_cur = cur->next;
32977c478bd9Sstevel@tonic-gate 	return (Z_OK);
32987c478bd9Sstevel@tonic-gate }
32997c478bd9Sstevel@tonic-gate 
33007c478bd9Sstevel@tonic-gate int
33017c478bd9Sstevel@tonic-gate zonecfg_endnwifent(zone_dochandle_t handle)
33027c478bd9Sstevel@tonic-gate {
33037c478bd9Sstevel@tonic-gate 	return (zonecfg_endent(handle));
33047c478bd9Sstevel@tonic-gate }
33057c478bd9Sstevel@tonic-gate 
33067c478bd9Sstevel@tonic-gate int
33077c478bd9Sstevel@tonic-gate zonecfg_setdevent(zone_dochandle_t handle)
33087c478bd9Sstevel@tonic-gate {
33097c478bd9Sstevel@tonic-gate 	return (zonecfg_setent(handle));
33107c478bd9Sstevel@tonic-gate }
33117c478bd9Sstevel@tonic-gate 
33127c478bd9Sstevel@tonic-gate int
33137c478bd9Sstevel@tonic-gate zonecfg_getdevent(zone_dochandle_t handle, struct zone_devtab *tabptr)
33147c478bd9Sstevel@tonic-gate {
33157c478bd9Sstevel@tonic-gate 	xmlNodePtr cur;
33167c478bd9Sstevel@tonic-gate 	int err;
33177c478bd9Sstevel@tonic-gate 
33187c478bd9Sstevel@tonic-gate 	if (handle == NULL)
33197c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
33207c478bd9Sstevel@tonic-gate 
33217c478bd9Sstevel@tonic-gate 	if ((cur = handle->zone_dh_cur) == NULL)
33227c478bd9Sstevel@tonic-gate 		return (Z_NO_ENTRY);
33237c478bd9Sstevel@tonic-gate 
33247c478bd9Sstevel@tonic-gate 	for (; cur != NULL; cur = cur->next)
33257c478bd9Sstevel@tonic-gate 		if (!xmlStrcmp(cur->name, DTD_ELEM_DEVICE))
33267c478bd9Sstevel@tonic-gate 			break;
33277c478bd9Sstevel@tonic-gate 	if (cur == NULL) {
33287c478bd9Sstevel@tonic-gate 		handle->zone_dh_cur = handle->zone_dh_top;
33297c478bd9Sstevel@tonic-gate 		return (Z_NO_ENTRY);
33307c478bd9Sstevel@tonic-gate 	}
33317c478bd9Sstevel@tonic-gate 
33327c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_MATCH, tabptr->zone_dev_match,
33337c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_dev_match))) != Z_OK) {
33347c478bd9Sstevel@tonic-gate 		handle->zone_dh_cur = handle->zone_dh_top;
33357c478bd9Sstevel@tonic-gate 		return (err);
33367c478bd9Sstevel@tonic-gate 	}
33377c478bd9Sstevel@tonic-gate 
33387c478bd9Sstevel@tonic-gate 	handle->zone_dh_cur = cur->next;
33397c478bd9Sstevel@tonic-gate 	return (Z_OK);
33407c478bd9Sstevel@tonic-gate }
33417c478bd9Sstevel@tonic-gate 
33427c478bd9Sstevel@tonic-gate int
33437c478bd9Sstevel@tonic-gate zonecfg_enddevent(zone_dochandle_t handle)
33447c478bd9Sstevel@tonic-gate {
33457c478bd9Sstevel@tonic-gate 	return (zonecfg_endent(handle));
33467c478bd9Sstevel@tonic-gate }
33477c478bd9Sstevel@tonic-gate 
33487c478bd9Sstevel@tonic-gate int
33497c478bd9Sstevel@tonic-gate zonecfg_setrctlent(zone_dochandle_t handle)
33507c478bd9Sstevel@tonic-gate {
33517c478bd9Sstevel@tonic-gate 	return (zonecfg_setent(handle));
33527c478bd9Sstevel@tonic-gate }
33537c478bd9Sstevel@tonic-gate 
33547c478bd9Sstevel@tonic-gate int
33557c478bd9Sstevel@tonic-gate zonecfg_getrctlent(zone_dochandle_t handle, struct zone_rctltab *tabptr)
33567c478bd9Sstevel@tonic-gate {
33577c478bd9Sstevel@tonic-gate 	xmlNodePtr cur, val;
33587c478bd9Sstevel@tonic-gate 	struct zone_rctlvaltab *valptr;
33597c478bd9Sstevel@tonic-gate 	int err;
33607c478bd9Sstevel@tonic-gate 
33617c478bd9Sstevel@tonic-gate 	if (handle == NULL)
33627c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
33637c478bd9Sstevel@tonic-gate 
33647c478bd9Sstevel@tonic-gate 	if ((cur = handle->zone_dh_cur) == NULL)
33657c478bd9Sstevel@tonic-gate 		return (Z_NO_ENTRY);
33667c478bd9Sstevel@tonic-gate 
33677c478bd9Sstevel@tonic-gate 	for (; cur != NULL; cur = cur->next)
33687c478bd9Sstevel@tonic-gate 		if (!xmlStrcmp(cur->name, DTD_ELEM_RCTL))
33697c478bd9Sstevel@tonic-gate 			break;
33707c478bd9Sstevel@tonic-gate 	if (cur == NULL) {
33717c478bd9Sstevel@tonic-gate 		handle->zone_dh_cur = handle->zone_dh_top;
33727c478bd9Sstevel@tonic-gate 		return (Z_NO_ENTRY);
33737c478bd9Sstevel@tonic-gate 	}
33747c478bd9Sstevel@tonic-gate 
33757c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_NAME, tabptr->zone_rctl_name,
33767c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_rctl_name))) != Z_OK) {
33777c478bd9Sstevel@tonic-gate 		handle->zone_dh_cur = handle->zone_dh_top;
33787c478bd9Sstevel@tonic-gate 		return (err);
33797c478bd9Sstevel@tonic-gate 	}
33807c478bd9Sstevel@tonic-gate 
33817c478bd9Sstevel@tonic-gate 	tabptr->zone_rctl_valptr = NULL;
33827c478bd9Sstevel@tonic-gate 	for (val = cur->xmlChildrenNode; val != NULL; val = val->next) {
33837c478bd9Sstevel@tonic-gate 		valptr = (struct zone_rctlvaltab *)malloc(
33847c478bd9Sstevel@tonic-gate 		    sizeof (struct zone_rctlvaltab));
33857c478bd9Sstevel@tonic-gate 		if (valptr == NULL)
33867c478bd9Sstevel@tonic-gate 			return (Z_NOMEM);
33877c478bd9Sstevel@tonic-gate 		if (fetchprop(val, DTD_ATTR_PRIV, valptr->zone_rctlval_priv,
33887c478bd9Sstevel@tonic-gate 		    sizeof (valptr->zone_rctlval_priv)) != Z_OK)
33897c478bd9Sstevel@tonic-gate 			break;
33907c478bd9Sstevel@tonic-gate 		if (fetchprop(val, DTD_ATTR_LIMIT, valptr->zone_rctlval_limit,
33917c478bd9Sstevel@tonic-gate 		    sizeof (valptr->zone_rctlval_limit)) != Z_OK)
33927c478bd9Sstevel@tonic-gate 			break;
33937c478bd9Sstevel@tonic-gate 		if (fetchprop(val, DTD_ATTR_ACTION, valptr->zone_rctlval_action,
33947c478bd9Sstevel@tonic-gate 		    sizeof (valptr->zone_rctlval_action)) != Z_OK)
33957c478bd9Sstevel@tonic-gate 			break;
33967c478bd9Sstevel@tonic-gate 		if (zonecfg_add_rctl_value(tabptr, valptr) != Z_OK)
33977c478bd9Sstevel@tonic-gate 			break;
33987c478bd9Sstevel@tonic-gate 	}
33997c478bd9Sstevel@tonic-gate 
34007c478bd9Sstevel@tonic-gate 	handle->zone_dh_cur = cur->next;
34017c478bd9Sstevel@tonic-gate 	return (Z_OK);
34027c478bd9Sstevel@tonic-gate }
34037c478bd9Sstevel@tonic-gate 
34047c478bd9Sstevel@tonic-gate int
34057c478bd9Sstevel@tonic-gate zonecfg_endrctlent(zone_dochandle_t handle)
34067c478bd9Sstevel@tonic-gate {
34077c478bd9Sstevel@tonic-gate 	return (zonecfg_endent(handle));
34087c478bd9Sstevel@tonic-gate }
34097c478bd9Sstevel@tonic-gate 
34107c478bd9Sstevel@tonic-gate int
34117c478bd9Sstevel@tonic-gate zonecfg_setattrent(zone_dochandle_t handle)
34127c478bd9Sstevel@tonic-gate {
34137c478bd9Sstevel@tonic-gate 	return (zonecfg_setent(handle));
34147c478bd9Sstevel@tonic-gate }
34157c478bd9Sstevel@tonic-gate 
34167c478bd9Sstevel@tonic-gate int
34177c478bd9Sstevel@tonic-gate zonecfg_getattrent(zone_dochandle_t handle, struct zone_attrtab *tabptr)
34187c478bd9Sstevel@tonic-gate {
34197c478bd9Sstevel@tonic-gate 	xmlNodePtr cur;
34207c478bd9Sstevel@tonic-gate 	int err;
34217c478bd9Sstevel@tonic-gate 
34227c478bd9Sstevel@tonic-gate 	if (handle == NULL)
34237c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
34247c478bd9Sstevel@tonic-gate 
34257c478bd9Sstevel@tonic-gate 	if ((cur = handle->zone_dh_cur) == NULL)
34267c478bd9Sstevel@tonic-gate 		return (Z_NO_ENTRY);
34277c478bd9Sstevel@tonic-gate 
34287c478bd9Sstevel@tonic-gate 	for (; cur != NULL; cur = cur->next)
34297c478bd9Sstevel@tonic-gate 		if (!xmlStrcmp(cur->name, DTD_ELEM_ATTR))
34307c478bd9Sstevel@tonic-gate 			break;
34317c478bd9Sstevel@tonic-gate 	if (cur == NULL) {
34327c478bd9Sstevel@tonic-gate 		handle->zone_dh_cur = handle->zone_dh_top;
34337c478bd9Sstevel@tonic-gate 		return (Z_NO_ENTRY);
34347c478bd9Sstevel@tonic-gate 	}
34357c478bd9Sstevel@tonic-gate 
34367c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_NAME, tabptr->zone_attr_name,
34377c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_attr_name))) != Z_OK) {
34387c478bd9Sstevel@tonic-gate 		handle->zone_dh_cur = handle->zone_dh_top;
34397c478bd9Sstevel@tonic-gate 		return (err);
34407c478bd9Sstevel@tonic-gate 	}
34417c478bd9Sstevel@tonic-gate 
34427c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_TYPE, tabptr->zone_attr_type,
34437c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_attr_type))) != Z_OK) {
34447c478bd9Sstevel@tonic-gate 		handle->zone_dh_cur = handle->zone_dh_top;
34457c478bd9Sstevel@tonic-gate 		return (err);
34467c478bd9Sstevel@tonic-gate 	}
34477c478bd9Sstevel@tonic-gate 
34487c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_VALUE, tabptr->zone_attr_value,
34497c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_attr_value))) != Z_OK) {
34507c478bd9Sstevel@tonic-gate 		handle->zone_dh_cur = handle->zone_dh_top;
34517c478bd9Sstevel@tonic-gate 		return (err);
34527c478bd9Sstevel@tonic-gate 	}
34537c478bd9Sstevel@tonic-gate 
34547c478bd9Sstevel@tonic-gate 	handle->zone_dh_cur = cur->next;
34557c478bd9Sstevel@tonic-gate 	return (Z_OK);
34567c478bd9Sstevel@tonic-gate }
34577c478bd9Sstevel@tonic-gate 
34587c478bd9Sstevel@tonic-gate int
34597c478bd9Sstevel@tonic-gate zonecfg_endattrent(zone_dochandle_t handle)
34607c478bd9Sstevel@tonic-gate {
34617c478bd9Sstevel@tonic-gate 	return (zonecfg_endent(handle));
34627c478bd9Sstevel@tonic-gate }
34637c478bd9Sstevel@tonic-gate 
3464ffbafc53Scomay /*
3465ffbafc53Scomay  * The privileges available on the system and described in privileges(5)
3466ffbafc53Scomay  * fall into four categories with respect to non-global zones; those that
3467ffbafc53Scomay  * are required in order for a non-global zone to boot, those which are in
3468ffbafc53Scomay  * the default set of privileges available to non-global zones, those
3469ffbafc53Scomay  * privileges which should not be allowed to be given to non-global zones
3470ffbafc53Scomay  * and all other privileges, which are optional and potentially useful for
3471ffbafc53Scomay  * processes executing inside a non-global zone.
3472ffbafc53Scomay  *
3473ffbafc53Scomay  * When privileges are added to the system, a determination needs to be
3474ffbafc53Scomay  * made as to which category the privilege belongs to.  Ideally,
3475ffbafc53Scomay  * privileges should be fine-grained enough and the mechanisms they cover
3476ffbafc53Scomay  * virtualized enough so that they can be made available to non-global
3477ffbafc53Scomay  * zones.
3478ffbafc53Scomay  */
3479ffbafc53Scomay 
3480ffbafc53Scomay /*
3481ffbafc53Scomay  * Set of privileges required in order to get a zone booted and init(1M)
3482ffbafc53Scomay  * started.  These cannot be removed from the zone's privilege set.
3483ffbafc53Scomay  */
3484ffbafc53Scomay static const char *required_priv_list[] = {
3485ffbafc53Scomay 	PRIV_PROC_EXEC,
3486ffbafc53Scomay 	PRIV_PROC_FORK,
3487ffbafc53Scomay 	PRIV_SYS_MOUNT,
3488ffbafc53Scomay 	NULL
3489ffbafc53Scomay };
3490ffbafc53Scomay 
3491ffbafc53Scomay /*
3492ffbafc53Scomay  * Default set of privileges considered safe for all non-global zones.
3493ffbafc53Scomay  * These privileges are "safe" in the sense that a privileged process in
3494ffbafc53Scomay  * the zone cannot affect processes in other non-global zones on the
3495ffbafc53Scomay  * system or in the global zone.  Privileges which are considered by
3496ffbafc53Scomay  * default, "unsafe", include ones which affect a global resource, such as
3497ffbafc53Scomay  * the system clock or physical memory.
3498ffbafc53Scomay  */
3499ffbafc53Scomay static const char *default_priv_list[] = {
3500ffbafc53Scomay 	PRIV_CONTRACT_EVENT,
3501ffbafc53Scomay 	PRIV_CONTRACT_OBSERVER,
35027c478bd9Sstevel@tonic-gate 	PRIV_FILE_CHOWN,
35037c478bd9Sstevel@tonic-gate 	PRIV_FILE_CHOWN_SELF,
35047c478bd9Sstevel@tonic-gate 	PRIV_FILE_DAC_EXECUTE,
35057c478bd9Sstevel@tonic-gate 	PRIV_FILE_DAC_READ,
35067c478bd9Sstevel@tonic-gate 	PRIV_FILE_DAC_SEARCH,
35077c478bd9Sstevel@tonic-gate 	PRIV_FILE_DAC_WRITE,
35087c478bd9Sstevel@tonic-gate 	PRIV_FILE_OWNER,
35097c478bd9Sstevel@tonic-gate 	PRIV_FILE_SETID,
35107c478bd9Sstevel@tonic-gate 	PRIV_IPC_DAC_READ,
35117c478bd9Sstevel@tonic-gate 	PRIV_IPC_DAC_WRITE,
35127c478bd9Sstevel@tonic-gate 	PRIV_IPC_OWNER,
351345916cd2Sjpk 	PRIV_NET_BINDMLP,
35147c478bd9Sstevel@tonic-gate 	PRIV_NET_ICMPACCESS,
351545916cd2Sjpk 	PRIV_NET_MAC_AWARE,
35167c478bd9Sstevel@tonic-gate 	PRIV_NET_PRIVADDR,
35177c478bd9Sstevel@tonic-gate 	PRIV_PROC_CHROOT,
35187c478bd9Sstevel@tonic-gate 	PRIV_SYS_AUDIT,
35197c478bd9Sstevel@tonic-gate 	PRIV_PROC_AUDIT,
35207c478bd9Sstevel@tonic-gate 	PRIV_PROC_OWNER,
35217c478bd9Sstevel@tonic-gate 	PRIV_PROC_SETID,
35227c478bd9Sstevel@tonic-gate 	PRIV_PROC_TASKID,
35237c478bd9Sstevel@tonic-gate 	PRIV_SYS_ACCT,
35247c478bd9Sstevel@tonic-gate 	PRIV_SYS_ADMIN,
35257c478bd9Sstevel@tonic-gate 	PRIV_SYS_MOUNT,
35267c478bd9Sstevel@tonic-gate 	PRIV_SYS_NFS,
35277c478bd9Sstevel@tonic-gate 	PRIV_SYS_RESOURCE,
35287c478bd9Sstevel@tonic-gate 	NULL
35297c478bd9Sstevel@tonic-gate };
35307c478bd9Sstevel@tonic-gate 
3531ffbafc53Scomay /*
3532ffbafc53Scomay  * Set of privileges not currently permitted within a non-global zone.
3533ffbafc53Scomay  * Some of these privileges are overly broad and cover more than one
3534ffbafc53Scomay  * mechanism in the system.  In other cases, there has not been sufficient
3535ffbafc53Scomay  * virtualization in the parts of the system the privilege covers to allow
3536ffbafc53Scomay  * its use within a non-global zone.
3537ffbafc53Scomay  */
3538ffbafc53Scomay static const char *prohibited_priv_list[] = {
3539ffbafc53Scomay 	PRIV_DTRACE_KERNEL,
3540ffbafc53Scomay 	PRIV_PROC_ZONE,
3541ffbafc53Scomay 	PRIV_SYS_CONFIG,
3542ffbafc53Scomay 	PRIV_SYS_DEVICES,
3543ffbafc53Scomay 	PRIV_SYS_LINKDIR,
3544ffbafc53Scomay 	PRIV_SYS_NET_CONFIG,
3545ffbafc53Scomay 	PRIV_SYS_RES_CONFIG,
3546ffbafc53Scomay 	PRIV_SYS_SUSER_COMPAT,
3547ffbafc53Scomay 	NULL
3548ffbafc53Scomay };
3549ffbafc53Scomay 
3550ffbafc53Scomay /*
3551ffbafc53Scomay  * Define some of the tokens that priv_str_to_set(3C) recognizes.  Since
3552ffbafc53Scomay  * the privilege string separator can be any character, although it is
3553ffbafc53Scomay  * usually a comma character, define these here as well in the event that
3554ffbafc53Scomay  * they change or are augmented in the future.
3555ffbafc53Scomay  */
3556ffbafc53Scomay #define	BASIC_TOKEN		"basic"
3557ffbafc53Scomay #define	DEFAULT_TOKEN		"default"
3558ffbafc53Scomay #define	ZONE_TOKEN		"zone"
3559ffbafc53Scomay #define	TOKEN_PRIV_CHAR		','
3560ffbafc53Scomay #define	TOKEN_PRIV_STR		","
3561ffbafc53Scomay 
35627c478bd9Sstevel@tonic-gate int
3563ffbafc53Scomay zonecfg_default_privset(priv_set_t *privs)
35647c478bd9Sstevel@tonic-gate {
35657c478bd9Sstevel@tonic-gate 	const char **strp;
3566ffbafc53Scomay 	priv_set_t *basic;
35677c478bd9Sstevel@tonic-gate 
3568ffbafc53Scomay 	basic = priv_str_to_set(BASIC_TOKEN, TOKEN_PRIV_STR, NULL);
35697c478bd9Sstevel@tonic-gate 	if (basic == NULL)
3570ffbafc53Scomay 		return (errno == ENOMEM ? Z_NOMEM : Z_INVAL);
35717c478bd9Sstevel@tonic-gate 
35727c478bd9Sstevel@tonic-gate 	priv_union(basic, privs);
35737c478bd9Sstevel@tonic-gate 	priv_freeset(basic);
35747c478bd9Sstevel@tonic-gate 
3575ffbafc53Scomay 	for (strp = default_priv_list; *strp != NULL; strp++) {
35767c478bd9Sstevel@tonic-gate 		if (priv_addset(privs, *strp) != 0) {
35777c478bd9Sstevel@tonic-gate 			return (Z_INVAL);
35787c478bd9Sstevel@tonic-gate 		}
35797c478bd9Sstevel@tonic-gate 	}
35807c478bd9Sstevel@tonic-gate 	return (Z_OK);
35817c478bd9Sstevel@tonic-gate }
35827c478bd9Sstevel@tonic-gate 
3583ffbafc53Scomay void
3584ffbafc53Scomay append_priv_token(char *priv, char *str, size_t strlen)
3585ffbafc53Scomay {
3586ffbafc53Scomay 	if (*str != '\0')
3587ffbafc53Scomay 		(void) strlcat(str, TOKEN_PRIV_STR, strlen);
3588ffbafc53Scomay 	(void) strlcat(str, priv, strlen);
3589ffbafc53Scomay }
3590ffbafc53Scomay 
3591ffbafc53Scomay /*
3592ffbafc53Scomay  * Verify that the supplied string is a valid privilege limit set for a
3593ffbafc53Scomay  * non-global zone.  This string must not only be acceptable to
3594ffbafc53Scomay  * priv_str_to_set(3C) which parses it, but it also must resolve to a
3595ffbafc53Scomay  * privilege set that includes certain required privileges and lacks
3596ffbafc53Scomay  * certain prohibited privileges.
3597ffbafc53Scomay  */
3598ffbafc53Scomay static int
3599ffbafc53Scomay verify_privset(char *privbuf, priv_set_t *privs, char **privname,
3600ffbafc53Scomay     boolean_t add_default)
3601ffbafc53Scomay {
3602ffbafc53Scomay 	char *cp;
3603ffbafc53Scomay 	char *lasts;
3604ffbafc53Scomay 	size_t len;
3605ffbafc53Scomay 	priv_set_t *mergeset;
3606ffbafc53Scomay 	const char **strp;
3607ffbafc53Scomay 	char *tmp;
3608ffbafc53Scomay 	const char *token;
3609ffbafc53Scomay 
3610ffbafc53Scomay 	/*
3611ffbafc53Scomay 	 * The verification of the privilege string occurs in several
3612ffbafc53Scomay 	 * phases.  In the first phase, the supplied string is scanned for
3613ffbafc53Scomay 	 * the ZONE_TOKEN token which is not support as part of the
3614ffbafc53Scomay 	 * "limitpriv" property.
3615ffbafc53Scomay 	 *
3616ffbafc53Scomay 	 * Duplicate the supplied privilege string since strtok_r(3C)
3617ffbafc53Scomay 	 * tokenizes its input by null-terminating the tokens.
3618ffbafc53Scomay 	 */
3619ffbafc53Scomay 	if ((tmp = strdup(privbuf)) == NULL)
3620ffbafc53Scomay 		return (Z_NOMEM);
3621ffbafc53Scomay 	for (cp = strtok_r(tmp, TOKEN_PRIV_STR, &lasts); cp != NULL;
3622ffbafc53Scomay 	    cp = strtok_r(NULL, TOKEN_PRIV_STR, &lasts)) {
3623ffbafc53Scomay 		if (strcmp(cp, ZONE_TOKEN) == 0) {
3624ffbafc53Scomay 			free(tmp);
3625ffbafc53Scomay 			if ((*privname = strdup(ZONE_TOKEN)) == NULL)
3626ffbafc53Scomay 				return (Z_NOMEM);
3627ffbafc53Scomay 			else
3628ffbafc53Scomay 				return (Z_PRIV_UNKNOWN);
3629ffbafc53Scomay 		}
3630ffbafc53Scomay 	}
3631ffbafc53Scomay 	free(tmp);
3632ffbafc53Scomay 
3633ffbafc53Scomay 	if (add_default) {
3634ffbafc53Scomay 		/*
3635ffbafc53Scomay 		 * If DEFAULT_TOKEN was specified, a string needs to be
3636ffbafc53Scomay 		 * built containing the privileges from the default, safe
3637ffbafc53Scomay 		 * set along with those of the "limitpriv" property.
3638ffbafc53Scomay 		 */
3639ffbafc53Scomay 		len = strlen(privbuf) + sizeof (BASIC_TOKEN) + 2;
3640ffbafc53Scomay 		for (strp = default_priv_list; *strp != NULL; strp++)
3641ffbafc53Scomay 			len += strlen(*strp) + 1;
3642ffbafc53Scomay 		tmp = alloca(len);
3643ffbafc53Scomay 		*tmp = '\0';
3644ffbafc53Scomay 
3645ffbafc53Scomay 		append_priv_token(BASIC_TOKEN, tmp, len);
3646ffbafc53Scomay 		for (strp = default_priv_list; *strp != NULL; strp++)
3647ffbafc53Scomay 			append_priv_token((char *)*strp, tmp, len);
3648ffbafc53Scomay 		(void) strlcat(tmp, TOKEN_PRIV_STR, len);
3649ffbafc53Scomay 		(void) strlcat(tmp, privbuf, len);
3650ffbafc53Scomay 	} else {
3651ffbafc53Scomay 		tmp = privbuf;
3652ffbafc53Scomay 	}
3653ffbafc53Scomay 
3654ffbafc53Scomay 
3655ffbafc53Scomay 	/*
3656ffbafc53Scomay 	 * In the next phase, attempt to convert the merged privilege
3657ffbafc53Scomay 	 * string into a privilege set.  In the case of an error, either
3658ffbafc53Scomay 	 * there was a memory allocation failure or there was an invalid
3659ffbafc53Scomay 	 * privilege token in the string.  In either case, return an
3660ffbafc53Scomay 	 * appropriate error code but in the event of an invalid token,
3661ffbafc53Scomay 	 * allocate a string containing its name and return that back to
3662ffbafc53Scomay 	 * the caller.
3663ffbafc53Scomay 	 */
3664ffbafc53Scomay 	mergeset = priv_str_to_set(tmp, TOKEN_PRIV_STR, &token);
3665ffbafc53Scomay 	if (mergeset == NULL) {
3666ffbafc53Scomay 		if (token == NULL)
3667ffbafc53Scomay 			return (Z_NOMEM);
3668ffbafc53Scomay 		if ((cp = strchr(token, TOKEN_PRIV_CHAR)) != NULL)
3669ffbafc53Scomay 			*cp = '\0';
3670ffbafc53Scomay 		if ((*privname = strdup(token)) == NULL)
3671ffbafc53Scomay 			return (Z_NOMEM);
3672ffbafc53Scomay 		else
3673ffbafc53Scomay 			return (Z_PRIV_UNKNOWN);
3674ffbafc53Scomay 	}
3675ffbafc53Scomay 
3676ffbafc53Scomay 	/*
3677ffbafc53Scomay 	 * Next, verify that none of the prohibited zone privileges are
3678ffbafc53Scomay 	 * present in the merged privilege set.
3679ffbafc53Scomay 	 */
3680ffbafc53Scomay 	for (strp = prohibited_priv_list; *strp != NULL; strp++) {
3681ffbafc53Scomay 		if (priv_ismember(mergeset, *strp)) {
3682ffbafc53Scomay 			priv_freeset(mergeset);
3683ffbafc53Scomay 			if ((*privname = strdup(*strp)) == NULL)
3684ffbafc53Scomay 				return (Z_NOMEM);
3685ffbafc53Scomay 			else
3686ffbafc53Scomay 				return (Z_PRIV_PROHIBITED);
3687ffbafc53Scomay 		}
3688ffbafc53Scomay 	}
3689ffbafc53Scomay 
3690ffbafc53Scomay 	/*
3691ffbafc53Scomay 	 * Finally, verify that all of the required zone privileges are
3692ffbafc53Scomay 	 * present in the merged privilege set.
3693ffbafc53Scomay 	 */
3694ffbafc53Scomay 	for (strp = required_priv_list; *strp != NULL; strp++) {
3695ffbafc53Scomay 		if (!priv_ismember(mergeset, *strp)) {
3696ffbafc53Scomay 			priv_freeset(mergeset);
3697ffbafc53Scomay 			if ((*privname = strdup(*strp)) == NULL)
3698ffbafc53Scomay 				return (Z_NOMEM);
3699ffbafc53Scomay 			else
3700ffbafc53Scomay 				return (Z_PRIV_REQUIRED);
3701ffbafc53Scomay 		}
3702ffbafc53Scomay 	}
3703ffbafc53Scomay 
3704ffbafc53Scomay 	priv_copyset(mergeset, privs);
3705ffbafc53Scomay 	priv_freeset(mergeset);
3706ffbafc53Scomay 	return (Z_OK);
3707ffbafc53Scomay }
3708ffbafc53Scomay 
3709ffbafc53Scomay /*
3710ffbafc53Scomay  * Fill in the supplied privilege set with either the default, safe set of
3711ffbafc53Scomay  * privileges suitable for a non-global zone, or one based on the
3712ffbafc53Scomay  * "limitpriv" property in the zone's configuration.
3713ffbafc53Scomay  *
3714ffbafc53Scomay  * In the event of an invalid privilege specification in the
3715ffbafc53Scomay  * configuration, a string is allocated and returned containing the
3716ffbafc53Scomay  * "privilege" causing the issue.  It is the caller's responsibility to
3717ffbafc53Scomay  * free this memory when it is done with it.
3718ffbafc53Scomay  */
3719ffbafc53Scomay int
3720ffbafc53Scomay zonecfg_get_privset(zone_dochandle_t handle, priv_set_t *privs,
3721ffbafc53Scomay     char **privname)
3722ffbafc53Scomay {
3723ffbafc53Scomay 	char *cp;
3724ffbafc53Scomay 	int err;
3725ffbafc53Scomay 	int limitlen;
3726ffbafc53Scomay 	char *limitpriv = NULL;
3727ffbafc53Scomay 
3728ffbafc53Scomay 	/*
3729ffbafc53Scomay 	 * Attempt to lookup the "limitpriv" property.  If it does not
3730ffbafc53Scomay 	 * exist or matches the string DEFAULT_TOKEN exactly, then the
3731ffbafc53Scomay 	 * default, safe privilege set is returned.
3732ffbafc53Scomay 	 */
3733ffbafc53Scomay 	err = zonecfg_get_limitpriv(handle, &limitpriv);
3734ffbafc53Scomay 	if (err != Z_OK)
3735ffbafc53Scomay 		return (err);
3736ffbafc53Scomay 	limitlen = strlen(limitpriv);
3737ffbafc53Scomay 	if (limitlen == 0 || strcmp(limitpriv, DEFAULT_TOKEN) == 0) {
3738ffbafc53Scomay 		free(limitpriv);
3739ffbafc53Scomay 		return (zonecfg_default_privset(privs));
3740ffbafc53Scomay 	}
3741ffbafc53Scomay 
3742ffbafc53Scomay 	/*
3743ffbafc53Scomay 	 * Check if the string DEFAULT_TOKEN is the first token in a list
3744ffbafc53Scomay 	 * of privileges.
3745ffbafc53Scomay 	 */
3746ffbafc53Scomay 	cp = strchr(limitpriv, TOKEN_PRIV_CHAR);
3747ffbafc53Scomay 	if (cp != NULL &&
3748ffbafc53Scomay 	    strncmp(limitpriv, DEFAULT_TOKEN, cp - limitpriv) == 0)
3749ffbafc53Scomay 		err = verify_privset(cp + 1, privs, privname, B_TRUE);
3750ffbafc53Scomay 	else
3751ffbafc53Scomay 		err = verify_privset(limitpriv, privs, privname, B_FALSE);
3752ffbafc53Scomay 
3753ffbafc53Scomay 	free(limitpriv);
3754ffbafc53Scomay 	return (err);
3755ffbafc53Scomay }
3756ffbafc53Scomay 
37577c478bd9Sstevel@tonic-gate int
37587c478bd9Sstevel@tonic-gate zone_get_zonepath(char *zone_name, char *zonepath, size_t rp_sz)
37597c478bd9Sstevel@tonic-gate {
37607c478bd9Sstevel@tonic-gate 	zone_dochandle_t handle;
37617c478bd9Sstevel@tonic-gate 	boolean_t found = B_FALSE;
37627c478bd9Sstevel@tonic-gate 	struct zoneent *ze;
37637c478bd9Sstevel@tonic-gate 	FILE *cookie;
37647c478bd9Sstevel@tonic-gate 	int err;
3765108322fbScarlsonj 	char *cp;
37667c478bd9Sstevel@tonic-gate 
37677c478bd9Sstevel@tonic-gate 	if (zone_name == NULL)
37687c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
37697c478bd9Sstevel@tonic-gate 
3770108322fbScarlsonj 	(void) strlcpy(zonepath, zonecfg_root, rp_sz);
3771108322fbScarlsonj 	cp = zonepath + strlen(zonepath);
3772108322fbScarlsonj 	while (cp > zonepath && cp[-1] == '/')
3773108322fbScarlsonj 		*--cp = '\0';
3774108322fbScarlsonj 
37757c478bd9Sstevel@tonic-gate 	if (strcmp(zone_name, GLOBAL_ZONENAME) == 0) {
3776108322fbScarlsonj 		if (zonepath[0] == '\0')
37777c478bd9Sstevel@tonic-gate 			(void) strlcpy(zonepath, "/", rp_sz);
37787c478bd9Sstevel@tonic-gate 		return (Z_OK);
37797c478bd9Sstevel@tonic-gate 	}
37807c478bd9Sstevel@tonic-gate 
37817c478bd9Sstevel@tonic-gate 	/*
37827c478bd9Sstevel@tonic-gate 	 * First check the index file.  Because older versions did not have
37837c478bd9Sstevel@tonic-gate 	 * a copy of the zone path, allow for it to be zero length, in which
37847c478bd9Sstevel@tonic-gate 	 * case we ignore this result and fall back to the XML files.
37857c478bd9Sstevel@tonic-gate 	 */
37867c478bd9Sstevel@tonic-gate 	cookie = setzoneent();
37877c478bd9Sstevel@tonic-gate 	while ((ze = getzoneent_private(cookie)) != NULL) {
37887c478bd9Sstevel@tonic-gate 		if (strcmp(ze->zone_name, zone_name) == 0) {
37897c478bd9Sstevel@tonic-gate 			found = B_TRUE;
3790108322fbScarlsonj 			if (ze->zone_path[0] != '\0')
3791108322fbScarlsonj 				(void) strlcpy(cp, ze->zone_path,
3792108322fbScarlsonj 				    rp_sz - (cp - zonepath));
37937c478bd9Sstevel@tonic-gate 		}
37947c478bd9Sstevel@tonic-gate 		free(ze);
37957c478bd9Sstevel@tonic-gate 		if (found)
37967c478bd9Sstevel@tonic-gate 			break;
37977c478bd9Sstevel@tonic-gate 	}
37987c478bd9Sstevel@tonic-gate 	endzoneent(cookie);
3799108322fbScarlsonj 	if (found && *cp != '\0')
38007c478bd9Sstevel@tonic-gate 		return (Z_OK);
38017c478bd9Sstevel@tonic-gate 
38027c478bd9Sstevel@tonic-gate 	/* Fall back to the XML files. */
38037c478bd9Sstevel@tonic-gate 	if ((handle = zonecfg_init_handle()) == NULL)
38047c478bd9Sstevel@tonic-gate 		return (Z_NOMEM);
38057c478bd9Sstevel@tonic-gate 
38067c478bd9Sstevel@tonic-gate 	/*
38077c478bd9Sstevel@tonic-gate 	 * Check the snapshot first: if a zone is running, its zonepath
38087c478bd9Sstevel@tonic-gate 	 * may have changed.
38097c478bd9Sstevel@tonic-gate 	 */
38107c478bd9Sstevel@tonic-gate 	if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) {
38117c478bd9Sstevel@tonic-gate 		if ((err = zonecfg_get_handle(zone_name, handle)) != Z_OK)
38127c478bd9Sstevel@tonic-gate 			return (err);
38137c478bd9Sstevel@tonic-gate 	}
38147c478bd9Sstevel@tonic-gate 	err = zonecfg_get_zonepath(handle, zonepath, rp_sz);
38157c478bd9Sstevel@tonic-gate 	zonecfg_fini_handle(handle);
38167c478bd9Sstevel@tonic-gate 	return (err);
38177c478bd9Sstevel@tonic-gate }
38187c478bd9Sstevel@tonic-gate 
38197c478bd9Sstevel@tonic-gate int
38207c478bd9Sstevel@tonic-gate zone_get_rootpath(char *zone_name, char *rootpath, size_t rp_sz)
38217c478bd9Sstevel@tonic-gate {
38227c478bd9Sstevel@tonic-gate 	int err;
38237c478bd9Sstevel@tonic-gate 
38247c478bd9Sstevel@tonic-gate 	/* This function makes sense for non-global zones only. */
38257c478bd9Sstevel@tonic-gate 	if (strcmp(zone_name, GLOBAL_ZONENAME) == 0)
38267c478bd9Sstevel@tonic-gate 		return (Z_BOGUS_ZONE_NAME);
38277c478bd9Sstevel@tonic-gate 	if ((err = zone_get_zonepath(zone_name, rootpath, rp_sz)) != Z_OK)
38287c478bd9Sstevel@tonic-gate 		return (err);
38297c478bd9Sstevel@tonic-gate 	if (strlcat(rootpath, "/root", rp_sz) >= rp_sz)
38307c478bd9Sstevel@tonic-gate 		return (Z_TOO_BIG);
38317c478bd9Sstevel@tonic-gate 	return (Z_OK);
38327c478bd9Sstevel@tonic-gate }
38337c478bd9Sstevel@tonic-gate 
38347c478bd9Sstevel@tonic-gate static zone_state_t
3835108322fbScarlsonj kernel_state_to_user_state(zoneid_t zoneid, zone_status_t kernel_state)
38367c478bd9Sstevel@tonic-gate {
3837108322fbScarlsonj 	char zoneroot[MAXPATHLEN];
3838108322fbScarlsonj 	size_t zlen;
3839108322fbScarlsonj 
38407c478bd9Sstevel@tonic-gate 	assert(kernel_state <= ZONE_MAX_STATE);
38417c478bd9Sstevel@tonic-gate 	switch (kernel_state) {
38427c478bd9Sstevel@tonic-gate 		case ZONE_IS_UNINITIALIZED:
3843108322fbScarlsonj 			return (ZONE_STATE_READY);
38447c478bd9Sstevel@tonic-gate 		case ZONE_IS_READY:
3845108322fbScarlsonj 			/*
3846108322fbScarlsonj 			 * If the zone's root is mounted on $ZONEPATH/lu, then
3847108322fbScarlsonj 			 * it's a mounted scratch zone.
3848108322fbScarlsonj 			 */
3849108322fbScarlsonj 			if (zone_getattr(zoneid, ZONE_ATTR_ROOT, zoneroot,
3850108322fbScarlsonj 			    sizeof (zoneroot)) >= 0) {
3851108322fbScarlsonj 				zlen = strlen(zoneroot);
3852108322fbScarlsonj 				if (zlen > 3 &&
3853108322fbScarlsonj 				    strcmp(zoneroot + zlen - 3, "/lu") == 0)
3854108322fbScarlsonj 					return (ZONE_STATE_MOUNTED);
3855108322fbScarlsonj 			}
38567c478bd9Sstevel@tonic-gate 			return (ZONE_STATE_READY);
38577c478bd9Sstevel@tonic-gate 		case ZONE_IS_BOOTING:
38587c478bd9Sstevel@tonic-gate 		case ZONE_IS_RUNNING:
38597c478bd9Sstevel@tonic-gate 			return (ZONE_STATE_RUNNING);
38607c478bd9Sstevel@tonic-gate 		case ZONE_IS_SHUTTING_DOWN:
38617c478bd9Sstevel@tonic-gate 		case ZONE_IS_EMPTY:
38627c478bd9Sstevel@tonic-gate 			return (ZONE_STATE_SHUTTING_DOWN);
38637c478bd9Sstevel@tonic-gate 		case ZONE_IS_DOWN:
38647c478bd9Sstevel@tonic-gate 		case ZONE_IS_DYING:
38657c478bd9Sstevel@tonic-gate 		case ZONE_IS_DEAD:
38667c478bd9Sstevel@tonic-gate 		default:
38677c478bd9Sstevel@tonic-gate 			return (ZONE_STATE_DOWN);
38687c478bd9Sstevel@tonic-gate 	}
38697c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
38707c478bd9Sstevel@tonic-gate }
38717c478bd9Sstevel@tonic-gate 
38727c478bd9Sstevel@tonic-gate int
38737c478bd9Sstevel@tonic-gate zone_get_state(char *zone_name, zone_state_t *state_num)
38747c478bd9Sstevel@tonic-gate {
38757c478bd9Sstevel@tonic-gate 	zone_status_t status;
38767c478bd9Sstevel@tonic-gate 	zoneid_t zone_id;
38777c478bd9Sstevel@tonic-gate 	struct zoneent *ze;
38787c478bd9Sstevel@tonic-gate 	boolean_t found = B_FALSE;
38797c478bd9Sstevel@tonic-gate 	FILE *cookie;
3880108322fbScarlsonj 	char kernzone[ZONENAME_MAX];
3881108322fbScarlsonj 	FILE *fp;
38827c478bd9Sstevel@tonic-gate 
38837c478bd9Sstevel@tonic-gate 	if (zone_name == NULL)
38847c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
38857c478bd9Sstevel@tonic-gate 
3886108322fbScarlsonj 	/*
3887108322fbScarlsonj 	 * If we're looking at an alternate root, then we need to query the
3888108322fbScarlsonj 	 * kernel using the scratch zone name.
3889108322fbScarlsonj 	 */
3890108322fbScarlsonj 	zone_id = -1;
3891108322fbScarlsonj 	if (*zonecfg_root != '\0' && !zonecfg_is_scratch(zone_name)) {
3892108322fbScarlsonj 		if ((fp = zonecfg_open_scratch("", B_FALSE)) != NULL) {
3893108322fbScarlsonj 			if (zonecfg_find_scratch(fp, zone_name, zonecfg_root,
3894108322fbScarlsonj 			    kernzone, sizeof (kernzone)) == 0)
3895108322fbScarlsonj 				zone_id = getzoneidbyname(kernzone);
3896108322fbScarlsonj 			zonecfg_close_scratch(fp);
3897108322fbScarlsonj 		}
3898108322fbScarlsonj 	} else {
3899108322fbScarlsonj 		zone_id = getzoneidbyname(zone_name);
3900108322fbScarlsonj 	}
3901108322fbScarlsonj 
39027c478bd9Sstevel@tonic-gate 	/* check to see if zone is running */
3903108322fbScarlsonj 	if (zone_id != -1 &&
39047c478bd9Sstevel@tonic-gate 	    zone_getattr(zone_id, ZONE_ATTR_STATUS, &status,
39057c478bd9Sstevel@tonic-gate 	    sizeof (status)) >= 0) {
3906108322fbScarlsonj 		*state_num = kernel_state_to_user_state(zone_id, status);
39077c478bd9Sstevel@tonic-gate 		return (Z_OK);
39087c478bd9Sstevel@tonic-gate 	}
39097c478bd9Sstevel@tonic-gate 
39107c478bd9Sstevel@tonic-gate 	cookie = setzoneent();
39117c478bd9Sstevel@tonic-gate 	while ((ze = getzoneent_private(cookie)) != NULL) {
39127c478bd9Sstevel@tonic-gate 		if (strcmp(ze->zone_name, zone_name) == 0) {
39137c478bd9Sstevel@tonic-gate 			found = B_TRUE;
39147c478bd9Sstevel@tonic-gate 			*state_num = ze->zone_state;
39157c478bd9Sstevel@tonic-gate 		}
39167c478bd9Sstevel@tonic-gate 		free(ze);
39177c478bd9Sstevel@tonic-gate 		if (found)
39187c478bd9Sstevel@tonic-gate 			break;
39197c478bd9Sstevel@tonic-gate 	}
39207c478bd9Sstevel@tonic-gate 	endzoneent(cookie);
39217c478bd9Sstevel@tonic-gate 	return ((found) ? Z_OK : Z_NO_ZONE);
39227c478bd9Sstevel@tonic-gate }
39237c478bd9Sstevel@tonic-gate 
39247c478bd9Sstevel@tonic-gate int
39257c478bd9Sstevel@tonic-gate zone_set_state(char *zone, zone_state_t state)
39267c478bd9Sstevel@tonic-gate {
39277c478bd9Sstevel@tonic-gate 	struct zoneent ze;
39287c478bd9Sstevel@tonic-gate 
39297c478bd9Sstevel@tonic-gate 	if (state != ZONE_STATE_CONFIGURED && state != ZONE_STATE_INSTALLED &&
39307c478bd9Sstevel@tonic-gate 	    state != ZONE_STATE_INCOMPLETE)
39317c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
39327c478bd9Sstevel@tonic-gate 
3933087719fdSdp 	bzero(&ze, sizeof (ze));
39347c478bd9Sstevel@tonic-gate 	(void) strlcpy(ze.zone_name, zone, sizeof (ze.zone_name));
39357c478bd9Sstevel@tonic-gate 	ze.zone_state = state;
39367c478bd9Sstevel@tonic-gate 	(void) strlcpy(ze.zone_path, "", sizeof (ze.zone_path));
39377c478bd9Sstevel@tonic-gate 	return (putzoneent(&ze, PZE_MODIFY));
39387c478bd9Sstevel@tonic-gate }
39397c478bd9Sstevel@tonic-gate 
39407c478bd9Sstevel@tonic-gate /*
39417c478bd9Sstevel@tonic-gate  * Get id (if any) for specified zone.  There are four possible outcomes:
39427c478bd9Sstevel@tonic-gate  * - If the string corresponds to the numeric id of an active (booted)
39437c478bd9Sstevel@tonic-gate  *   zone, sets *zip to the zone id and returns 0.
39447c478bd9Sstevel@tonic-gate  * - If the string corresponds to the name of an active (booted) zone,
39457c478bd9Sstevel@tonic-gate  *   sets *zip to the zone id and returns 0.
39467c478bd9Sstevel@tonic-gate  * - If the string is a name in the configuration but is not booted,
39477c478bd9Sstevel@tonic-gate  *   sets *zip to ZONE_ID_UNDEFINED and returns 0.
39487c478bd9Sstevel@tonic-gate  * - Otherwise, leaves *zip unchanged and returns -1.
39497c478bd9Sstevel@tonic-gate  *
39507c478bd9Sstevel@tonic-gate  * This function acts as an auxiliary filter on the function of the same
39517c478bd9Sstevel@tonic-gate  * name in libc; the linker binds to this version if libzonecfg exists,
39527c478bd9Sstevel@tonic-gate  * and the libc version if it doesn't.  Any changes to this version of
39537c478bd9Sstevel@tonic-gate  * the function should probably be reflected in the libc version as well.
39547c478bd9Sstevel@tonic-gate  */
39557c478bd9Sstevel@tonic-gate int
39567c478bd9Sstevel@tonic-gate zone_get_id(const char *str, zoneid_t *zip)
39577c478bd9Sstevel@tonic-gate {
39587c478bd9Sstevel@tonic-gate 	zone_dochandle_t hdl;
39597c478bd9Sstevel@tonic-gate 	zoneid_t zoneid;
39607c478bd9Sstevel@tonic-gate 	char *cp;
39617c478bd9Sstevel@tonic-gate 	int err;
39627c478bd9Sstevel@tonic-gate 
39637c478bd9Sstevel@tonic-gate 	/* first try looking for active zone by id */
39647c478bd9Sstevel@tonic-gate 	errno = 0;
39657c478bd9Sstevel@tonic-gate 	zoneid = (zoneid_t)strtol(str, &cp, 0);
39667c478bd9Sstevel@tonic-gate 	if (errno == 0 && cp != str && *cp == '\0' &&
39677c478bd9Sstevel@tonic-gate 	    getzonenamebyid(zoneid, NULL, 0) != -1) {
39687c478bd9Sstevel@tonic-gate 		*zip = zoneid;
39697c478bd9Sstevel@tonic-gate 		return (0);
39707c478bd9Sstevel@tonic-gate 	}
39717c478bd9Sstevel@tonic-gate 
39727c478bd9Sstevel@tonic-gate 	/* then look for active zone by name */
39737c478bd9Sstevel@tonic-gate 	if ((zoneid = getzoneidbyname(str)) != -1) {
39747c478bd9Sstevel@tonic-gate 		*zip = zoneid;
39757c478bd9Sstevel@tonic-gate 		return (0);
39767c478bd9Sstevel@tonic-gate 	}
39777c478bd9Sstevel@tonic-gate 
39787c478bd9Sstevel@tonic-gate 	/* if in global zone, try looking up name in configuration database */
39797c478bd9Sstevel@tonic-gate 	if (getzoneid() != GLOBAL_ZONEID ||
39807c478bd9Sstevel@tonic-gate 	    (hdl = zonecfg_init_handle()) == NULL)
39817c478bd9Sstevel@tonic-gate 		return (-1);
39827c478bd9Sstevel@tonic-gate 
3983108322fbScarlsonj 	if (zonecfg_get_handle(str, hdl) == Z_OK) {
39847c478bd9Sstevel@tonic-gate 		/* zone exists but isn't active */
39857c478bd9Sstevel@tonic-gate 		*zip = ZONE_ID_UNDEFINED;
39867c478bd9Sstevel@tonic-gate 		err = 0;
39877c478bd9Sstevel@tonic-gate 	} else {
39887c478bd9Sstevel@tonic-gate 		err = -1;
39897c478bd9Sstevel@tonic-gate 	}
39907c478bd9Sstevel@tonic-gate 
39917c478bd9Sstevel@tonic-gate 	zonecfg_fini_handle(hdl);
39927c478bd9Sstevel@tonic-gate 	return (err);
39937c478bd9Sstevel@tonic-gate }
39947c478bd9Sstevel@tonic-gate 
39957c478bd9Sstevel@tonic-gate char *
39967c478bd9Sstevel@tonic-gate zone_state_str(zone_state_t state_num)
39977c478bd9Sstevel@tonic-gate {
39987c478bd9Sstevel@tonic-gate 	switch (state_num) {
39997c478bd9Sstevel@tonic-gate 	case ZONE_STATE_CONFIGURED:
40007c478bd9Sstevel@tonic-gate 		return (ZONE_STATE_STR_CONFIGURED);
40017c478bd9Sstevel@tonic-gate 	case ZONE_STATE_INCOMPLETE:
40027c478bd9Sstevel@tonic-gate 		return (ZONE_STATE_STR_INCOMPLETE);
40037c478bd9Sstevel@tonic-gate 	case ZONE_STATE_INSTALLED:
40047c478bd9Sstevel@tonic-gate 		return (ZONE_STATE_STR_INSTALLED);
40057c478bd9Sstevel@tonic-gate 	case ZONE_STATE_READY:
40067c478bd9Sstevel@tonic-gate 		return (ZONE_STATE_STR_READY);
4007108322fbScarlsonj 	case ZONE_STATE_MOUNTED:
4008108322fbScarlsonj 		return (ZONE_STATE_STR_MOUNTED);
40097c478bd9Sstevel@tonic-gate 	case ZONE_STATE_RUNNING:
40107c478bd9Sstevel@tonic-gate 		return (ZONE_STATE_STR_RUNNING);
40117c478bd9Sstevel@tonic-gate 	case ZONE_STATE_SHUTTING_DOWN:
40127c478bd9Sstevel@tonic-gate 		return (ZONE_STATE_STR_SHUTTING_DOWN);
40137c478bd9Sstevel@tonic-gate 	case ZONE_STATE_DOWN:
40147c478bd9Sstevel@tonic-gate 		return (ZONE_STATE_STR_DOWN);
40157c478bd9Sstevel@tonic-gate 	default:
40167c478bd9Sstevel@tonic-gate 		return ("unknown");
40177c478bd9Sstevel@tonic-gate 	}
40187c478bd9Sstevel@tonic-gate }
40197c478bd9Sstevel@tonic-gate 
40207c478bd9Sstevel@tonic-gate /*
4021108322fbScarlsonj  * Given a UUID value, find an associated zone name.  This is intended to be
4022108322fbScarlsonj  * used by callers who set up some 'default' name (corresponding to the
4023108322fbScarlsonj  * expected name for the zone) in the zonename buffer, and thus the function
4024108322fbScarlsonj  * doesn't touch this buffer on failure.
4025108322fbScarlsonj  */
4026108322fbScarlsonj int
4027*555afedfScarlsonj zonecfg_get_name_by_uuid(const uuid_t uuidin, char *zonename, size_t namelen)
4028108322fbScarlsonj {
4029108322fbScarlsonj 	FILE *fp;
4030108322fbScarlsonj 	struct zoneent *ze;
4031*555afedfScarlsonj 	uchar_t *uuid;
4032108322fbScarlsonj 
4033108322fbScarlsonj 	/*
4034108322fbScarlsonj 	 * A small amount of subterfuge via casts is necessary here because
4035108322fbScarlsonj 	 * libuuid doesn't use const correctly, but we don't want to export
4036108322fbScarlsonj 	 * this brokenness to our clients.
4037108322fbScarlsonj 	 */
4038*555afedfScarlsonj 	uuid = (uchar_t *)uuidin;
4039*555afedfScarlsonj 	if (uuid_is_null(uuid))
4040108322fbScarlsonj 		return (Z_NO_ZONE);
4041108322fbScarlsonj 	if ((fp = setzoneent()) == NULL)
4042108322fbScarlsonj 		return (Z_NO_ZONE);
4043108322fbScarlsonj 	while ((ze = getzoneent_private(fp)) != NULL) {
4044*555afedfScarlsonj 		if (uuid_compare(uuid, ze->zone_uuid) == 0)
4045108322fbScarlsonj 			break;
4046108322fbScarlsonj 		free(ze);
4047108322fbScarlsonj 	}
4048108322fbScarlsonj 	endzoneent(fp);
4049108322fbScarlsonj 	if (ze != NULL) {
4050108322fbScarlsonj 		(void) strlcpy(zonename, ze->zone_name, namelen);
4051108322fbScarlsonj 		free(ze);
4052108322fbScarlsonj 		return (Z_OK);
4053108322fbScarlsonj 	} else {
4054108322fbScarlsonj 		return (Z_NO_ZONE);
4055108322fbScarlsonj 	}
4056108322fbScarlsonj }
4057108322fbScarlsonj 
4058108322fbScarlsonj /*
4059108322fbScarlsonj  * Given a zone name, get its UUID.  Returns a "NULL" UUID value if the zone
4060108322fbScarlsonj  * exists but the file doesn't have a value set yet.  Returns an error if the
4061108322fbScarlsonj  * zone cannot be located.
4062108322fbScarlsonj  */
4063108322fbScarlsonj int
4064108322fbScarlsonj zonecfg_get_uuid(const char *zonename, uuid_t uuid)
4065108322fbScarlsonj {
4066108322fbScarlsonj 	FILE *fp;
4067108322fbScarlsonj 	struct zoneent *ze;
4068108322fbScarlsonj 
4069108322fbScarlsonj 	if ((fp = setzoneent()) == NULL)
4070108322fbScarlsonj 		return (Z_NO_ZONE);
4071108322fbScarlsonj 	while ((ze = getzoneent_private(fp)) != NULL) {
4072108322fbScarlsonj 		if (strcmp(ze->zone_name, zonename) == 0)
4073108322fbScarlsonj 			break;
4074108322fbScarlsonj 		free(ze);
4075108322fbScarlsonj 	}
4076108322fbScarlsonj 	endzoneent(fp);
4077108322fbScarlsonj 	if (ze != NULL) {
4078108322fbScarlsonj 		uuid_copy(uuid, ze->zone_uuid);
4079108322fbScarlsonj 		free(ze);
4080108322fbScarlsonj 		return (Z_OK);
4081108322fbScarlsonj 	} else {
4082108322fbScarlsonj 		return (Z_NO_ZONE);
4083108322fbScarlsonj 	}
4084108322fbScarlsonj }
4085108322fbScarlsonj 
4086108322fbScarlsonj /*
40877c478bd9Sstevel@tonic-gate  * File-system convenience functions.
40887c478bd9Sstevel@tonic-gate  */
40897c478bd9Sstevel@tonic-gate boolean_t
40907c478bd9Sstevel@tonic-gate zonecfg_valid_fs_type(const char *type)
40917c478bd9Sstevel@tonic-gate {
40927c478bd9Sstevel@tonic-gate 	/*
40937c478bd9Sstevel@tonic-gate 	 * We already know which FS types don't work.
40947c478bd9Sstevel@tonic-gate 	 */
40957c478bd9Sstevel@tonic-gate 	if (strcmp(type, "proc") == 0 ||
40967c478bd9Sstevel@tonic-gate 	    strcmp(type, "mntfs") == 0 ||
40977c478bd9Sstevel@tonic-gate 	    strcmp(type, "autofs") == 0 ||
40987c478bd9Sstevel@tonic-gate 	    strncmp(type, "nfs", sizeof ("nfs") - 1) == 0 ||
40997c478bd9Sstevel@tonic-gate 	    strcmp(type, "cachefs") == 0)
41007c478bd9Sstevel@tonic-gate 		return (B_FALSE);
41017c478bd9Sstevel@tonic-gate 	/*
41027c478bd9Sstevel@tonic-gate 	 * The caller may do more detailed verification to make sure other
41037c478bd9Sstevel@tonic-gate 	 * aspects of this filesystem type make sense.
41047c478bd9Sstevel@tonic-gate 	 */
41057c478bd9Sstevel@tonic-gate 	return (B_TRUE);
41067c478bd9Sstevel@tonic-gate }
41077c478bd9Sstevel@tonic-gate 
41087c478bd9Sstevel@tonic-gate /*
41097c478bd9Sstevel@tonic-gate  * Generally uninteresting rctl convenience functions.
41107c478bd9Sstevel@tonic-gate  */
41117c478bd9Sstevel@tonic-gate 
41127c478bd9Sstevel@tonic-gate int
41137c478bd9Sstevel@tonic-gate zonecfg_construct_rctlblk(const struct zone_rctlvaltab *rctlval,
41147c478bd9Sstevel@tonic-gate     rctlblk_t *rctlblk)
41157c478bd9Sstevel@tonic-gate {
41167c478bd9Sstevel@tonic-gate 	unsigned long long ull;
41177c478bd9Sstevel@tonic-gate 	char *endp;
41187c478bd9Sstevel@tonic-gate 	rctl_priv_t priv;
41197c478bd9Sstevel@tonic-gate 	rctl_qty_t limit;
41207c478bd9Sstevel@tonic-gate 	uint_t action;
41217c478bd9Sstevel@tonic-gate 
41227c478bd9Sstevel@tonic-gate 	/* Get the privilege */
41237c478bd9Sstevel@tonic-gate 	if (strcmp(rctlval->zone_rctlval_priv, "basic") == 0) {
41247c478bd9Sstevel@tonic-gate 		priv = RCPRIV_BASIC;
41257c478bd9Sstevel@tonic-gate 	} else if (strcmp(rctlval->zone_rctlval_priv, "privileged") == 0) {
41267c478bd9Sstevel@tonic-gate 		priv = RCPRIV_PRIVILEGED;
41277c478bd9Sstevel@tonic-gate 	} else {
41287c478bd9Sstevel@tonic-gate 		/* Invalid privilege */
41297c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
41307c478bd9Sstevel@tonic-gate 	}
41317c478bd9Sstevel@tonic-gate 
41327c478bd9Sstevel@tonic-gate 	/* deal with negative input; strtoull(3c) doesn't do what we want */
41337c478bd9Sstevel@tonic-gate 	if (rctlval->zone_rctlval_limit[0] == '-')
41347c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
41357c478bd9Sstevel@tonic-gate 	/* Get the limit */
41367c478bd9Sstevel@tonic-gate 	errno = 0;
41377c478bd9Sstevel@tonic-gate 	ull = strtoull(rctlval->zone_rctlval_limit, &endp, 0);
41387c478bd9Sstevel@tonic-gate 	if (errno != 0 || *endp != '\0') {
41397c478bd9Sstevel@tonic-gate 		/* parse failed */
41407c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
41417c478bd9Sstevel@tonic-gate 	}
41427c478bd9Sstevel@tonic-gate 	limit = (rctl_qty_t)ull;
41437c478bd9Sstevel@tonic-gate 
41447c478bd9Sstevel@tonic-gate 	/* Get the action */
41457c478bd9Sstevel@tonic-gate 	if (strcmp(rctlval->zone_rctlval_action, "none") == 0) {
41467c478bd9Sstevel@tonic-gate 		action = RCTL_LOCAL_NOACTION;
41477c478bd9Sstevel@tonic-gate 	} else if (strcmp(rctlval->zone_rctlval_action, "signal") == 0) {
41487c478bd9Sstevel@tonic-gate 		action = RCTL_LOCAL_SIGNAL;
41497c478bd9Sstevel@tonic-gate 	} else if (strcmp(rctlval->zone_rctlval_action, "deny") == 0) {
41507c478bd9Sstevel@tonic-gate 		action = RCTL_LOCAL_DENY;
41517c478bd9Sstevel@tonic-gate 	} else {
41527c478bd9Sstevel@tonic-gate 		/* Invalid Action */
41537c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
41547c478bd9Sstevel@tonic-gate 	}
41557c478bd9Sstevel@tonic-gate 	rctlblk_set_local_action(rctlblk, action, 0);
41567c478bd9Sstevel@tonic-gate 	rctlblk_set_privilege(rctlblk, priv);
41577c478bd9Sstevel@tonic-gate 	rctlblk_set_value(rctlblk, limit);
41587c478bd9Sstevel@tonic-gate 	return (Z_OK);
41597c478bd9Sstevel@tonic-gate }
41607c478bd9Sstevel@tonic-gate 
41617c478bd9Sstevel@tonic-gate static int
41627c478bd9Sstevel@tonic-gate rctl_check(const char *rctlname, void *arg)
41637c478bd9Sstevel@tonic-gate {
41647c478bd9Sstevel@tonic-gate 	const char *attrname = arg;
41657c478bd9Sstevel@tonic-gate 
41667c478bd9Sstevel@tonic-gate 	/*
41677c478bd9Sstevel@tonic-gate 	 * Returning 1 here is our signal to zonecfg_is_rctl() that it is
41687c478bd9Sstevel@tonic-gate 	 * indeed an rctl name recognized by the system.
41697c478bd9Sstevel@tonic-gate 	 */
41707c478bd9Sstevel@tonic-gate 	return (strcmp(rctlname, attrname) == 0 ? 1 : 0);
41717c478bd9Sstevel@tonic-gate }
41727c478bd9Sstevel@tonic-gate 
41737c478bd9Sstevel@tonic-gate boolean_t
41747c478bd9Sstevel@tonic-gate zonecfg_is_rctl(const char *name)
41757c478bd9Sstevel@tonic-gate {
41767c478bd9Sstevel@tonic-gate 	return (rctl_walk(rctl_check, (void *)name) == 1);
41777c478bd9Sstevel@tonic-gate }
41787c478bd9Sstevel@tonic-gate 
41797c478bd9Sstevel@tonic-gate boolean_t
41807c478bd9Sstevel@tonic-gate zonecfg_valid_rctlname(const char *name)
41817c478bd9Sstevel@tonic-gate {
41827c478bd9Sstevel@tonic-gate 	const char *c;
41837c478bd9Sstevel@tonic-gate 
41847c478bd9Sstevel@tonic-gate 	if (strncmp(name, "zone.", sizeof ("zone.") - 1) != 0)
41857c478bd9Sstevel@tonic-gate 		return (B_FALSE);
41867c478bd9Sstevel@tonic-gate 	if (strlen(name) == sizeof ("zone.") - 1)
41877c478bd9Sstevel@tonic-gate 		return (B_FALSE);
41887c478bd9Sstevel@tonic-gate 	for (c = name + sizeof ("zone.") - 1; *c != '\0'; c++) {
41897c478bd9Sstevel@tonic-gate 		if (!isalpha(*c) && *c != '-')
41907c478bd9Sstevel@tonic-gate 			return (B_FALSE);
41917c478bd9Sstevel@tonic-gate 	}
41927c478bd9Sstevel@tonic-gate 	return (B_TRUE);
41937c478bd9Sstevel@tonic-gate }
41947c478bd9Sstevel@tonic-gate 
41957c478bd9Sstevel@tonic-gate boolean_t
41967c478bd9Sstevel@tonic-gate zonecfg_valid_rctlblk(const rctlblk_t *rctlblk)
41977c478bd9Sstevel@tonic-gate {
41987c478bd9Sstevel@tonic-gate 	rctl_priv_t priv = rctlblk_get_privilege((rctlblk_t *)rctlblk);
41997c478bd9Sstevel@tonic-gate 	uint_t action = rctlblk_get_local_action((rctlblk_t *)rctlblk, NULL);
42007c478bd9Sstevel@tonic-gate 
42017c478bd9Sstevel@tonic-gate 	if (priv != RCPRIV_PRIVILEGED)
42027c478bd9Sstevel@tonic-gate 		return (B_FALSE);
42037c478bd9Sstevel@tonic-gate 	if (action != RCTL_LOCAL_NOACTION && action != RCTL_LOCAL_DENY)
42047c478bd9Sstevel@tonic-gate 		return (B_FALSE);
42057c478bd9Sstevel@tonic-gate 	return (B_TRUE);
42067c478bd9Sstevel@tonic-gate }
42077c478bd9Sstevel@tonic-gate 
42087c478bd9Sstevel@tonic-gate boolean_t
42097c478bd9Sstevel@tonic-gate zonecfg_valid_rctl(const char *name, const rctlblk_t *rctlblk)
42107c478bd9Sstevel@tonic-gate {
42117c478bd9Sstevel@tonic-gate 	rctlblk_t *current, *next;
42127c478bd9Sstevel@tonic-gate 	rctl_qty_t limit = rctlblk_get_value((rctlblk_t *)rctlblk);
42137c478bd9Sstevel@tonic-gate 	uint_t action = rctlblk_get_local_action((rctlblk_t *)rctlblk, NULL);
42147c478bd9Sstevel@tonic-gate 	uint_t global_flags;
42157c478bd9Sstevel@tonic-gate 
42167c478bd9Sstevel@tonic-gate 	if (!zonecfg_valid_rctlblk(rctlblk))
42177c478bd9Sstevel@tonic-gate 		return (B_FALSE);
42187c478bd9Sstevel@tonic-gate 	if (!zonecfg_valid_rctlname(name))
42197c478bd9Sstevel@tonic-gate 		return (B_FALSE);
42207c478bd9Sstevel@tonic-gate 
42217c478bd9Sstevel@tonic-gate 	current = alloca(rctlblk_size());
42227c478bd9Sstevel@tonic-gate 	if (getrctl(name, NULL, current, RCTL_FIRST) != 0)
42237c478bd9Sstevel@tonic-gate 		return (B_TRUE);	/* not an rctl on this system */
42247c478bd9Sstevel@tonic-gate 	/*
42257c478bd9Sstevel@tonic-gate 	 * Make sure the proposed value isn't greater than the current system
42267c478bd9Sstevel@tonic-gate 	 * value.
42277c478bd9Sstevel@tonic-gate 	 */
42287c478bd9Sstevel@tonic-gate 	next = alloca(rctlblk_size());
42297c478bd9Sstevel@tonic-gate 	while (rctlblk_get_privilege(current) != RCPRIV_SYSTEM) {
42307c478bd9Sstevel@tonic-gate 		rctlblk_t *tmp;
42317c478bd9Sstevel@tonic-gate 
42327c478bd9Sstevel@tonic-gate 		if (getrctl(name, current, next, RCTL_NEXT) != 0)
42337c478bd9Sstevel@tonic-gate 			return (B_FALSE);	/* shouldn't happen */
42347c478bd9Sstevel@tonic-gate 		tmp = current;
42357c478bd9Sstevel@tonic-gate 		current = next;
42367c478bd9Sstevel@tonic-gate 		next = tmp;
42377c478bd9Sstevel@tonic-gate 	}
42387c478bd9Sstevel@tonic-gate 	if (limit > rctlblk_get_value(current))
42397c478bd9Sstevel@tonic-gate 		return (B_FALSE);
42407c478bd9Sstevel@tonic-gate 
42417c478bd9Sstevel@tonic-gate 	/*
42427c478bd9Sstevel@tonic-gate 	 * Make sure the proposed action is allowed.
42437c478bd9Sstevel@tonic-gate 	 */
42447c478bd9Sstevel@tonic-gate 	global_flags = rctlblk_get_global_flags(current);
42457c478bd9Sstevel@tonic-gate 	if ((global_flags & RCTL_GLOBAL_DENY_NEVER) &&
42467c478bd9Sstevel@tonic-gate 	    action == RCTL_LOCAL_DENY)
42477c478bd9Sstevel@tonic-gate 		return (B_FALSE);
42487c478bd9Sstevel@tonic-gate 	if ((global_flags & RCTL_GLOBAL_DENY_ALWAYS) &&
42497c478bd9Sstevel@tonic-gate 	    action == RCTL_LOCAL_NOACTION)
42507c478bd9Sstevel@tonic-gate 		return (B_FALSE);
42517c478bd9Sstevel@tonic-gate 
42527c478bd9Sstevel@tonic-gate 	return (B_TRUE);
42537c478bd9Sstevel@tonic-gate }
4254fa9e4066Sahrens 
4255cf8f45c7Sdstaff /*
4256cf8f45c7Sdstaff  * There is always a race condition between reading the initial copy of
4257cf8f45c7Sdstaff  * a zones state and its state changing.  We address this by providing
4258cf8f45c7Sdstaff  * zonecfg_notify_critical_enter and zonecfg_noticy_critical_exit functions.
4259cf8f45c7Sdstaff  * When zonecfg_critical_enter is called, sets the state field to LOCKED
4260cf8f45c7Sdstaff  * and aquires biglock. Biglock protects against other threads executing
4261cf8f45c7Sdstaff  * critical_enter and the state field protects against state changes during
4262cf8f45c7Sdstaff  * the critical period.
4263cf8f45c7Sdstaff  *
4264cf8f45c7Sdstaff  * If any state changes occur, zn_cb will set the failed field of the znotify
4265cf8f45c7Sdstaff  * structure.  This will cause the critical_exit function to re-lock the
4266cf8f45c7Sdstaff  * channel and return an error. Since evsnts may be delayed, the critical_exit
4267cf8f45c7Sdstaff  * function "flushes" the queue by putting an event on the queue and waiting for
4268cf8f45c7Sdstaff  * zn_cb to notify critical_exit that it received the ping event.
4269cf8f45c7Sdstaff  */
4270cf8f45c7Sdstaff static const char *
4271cf8f45c7Sdstaff string_get_tok(const char *in, char delim, int num)
4272cf8f45c7Sdstaff {
4273cf8f45c7Sdstaff 	int i = 0;
4274cf8f45c7Sdstaff 
4275cf8f45c7Sdstaff 	for (; i < num; in++) {
4276cf8f45c7Sdstaff 		if (*in == delim)
4277cf8f45c7Sdstaff 			i++;
4278cf8f45c7Sdstaff 		if (*in == 0)
4279cf8f45c7Sdstaff 			return (NULL);
4280cf8f45c7Sdstaff 	}
4281cf8f45c7Sdstaff 	return (in);
4282cf8f45c7Sdstaff }
4283cf8f45c7Sdstaff 
4284cf8f45c7Sdstaff static boolean_t
4285cf8f45c7Sdstaff is_ping(sysevent_t *ev)
4286cf8f45c7Sdstaff {
4287cf8f45c7Sdstaff 	if (strcmp(sysevent_get_subclass_name(ev),
4288cf8f45c7Sdstaff 	    ZONE_EVENT_PING_SUBCLASS) == 0) {
4289cf8f45c7Sdstaff 		return (B_TRUE);
4290cf8f45c7Sdstaff 	} else {
4291cf8f45c7Sdstaff 		return (B_FALSE);
4292cf8f45c7Sdstaff 	}
4293cf8f45c7Sdstaff }
4294cf8f45c7Sdstaff 
4295cf8f45c7Sdstaff static boolean_t
4296cf8f45c7Sdstaff is_my_ping(sysevent_t *ev)
4297cf8f45c7Sdstaff {
4298cf8f45c7Sdstaff 	const char *sender;
4299cf8f45c7Sdstaff 	char mypid[sizeof (pid_t) * 3 + 1];
4300cf8f45c7Sdstaff 
4301cf8f45c7Sdstaff 	(void) snprintf(mypid, sizeof (mypid), "%i", getpid());
4302cf8f45c7Sdstaff 	sender = string_get_tok(sysevent_get_pub(ev), ':', 3);
4303cf8f45c7Sdstaff 	if (sender == NULL)
4304cf8f45c7Sdstaff 		return (B_FALSE);
4305cf8f45c7Sdstaff 	if (strcmp(sender, mypid) != 0)
4306cf8f45c7Sdstaff 		return (B_FALSE);
4307cf8f45c7Sdstaff 	return (B_TRUE);
4308cf8f45c7Sdstaff }
4309cf8f45c7Sdstaff 
4310cf8f45c7Sdstaff static int
4311cf8f45c7Sdstaff do_callback(struct znotify *zevtchan, sysevent_t *ev)
4312cf8f45c7Sdstaff {
4313cf8f45c7Sdstaff 	nvlist_t *l;
4314cf8f45c7Sdstaff 	int zid;
4315cf8f45c7Sdstaff 	char *zonename;
4316cf8f45c7Sdstaff 	char *newstate;
4317cf8f45c7Sdstaff 	char *oldstate;
4318cf8f45c7Sdstaff 	int ret;
4319cf8f45c7Sdstaff 	hrtime_t when;
4320cf8f45c7Sdstaff 
4321cf8f45c7Sdstaff 	if (strcmp(sysevent_get_subclass_name(ev),
4322cf8f45c7Sdstaff 	    ZONE_EVENT_STATUS_SUBCLASS) == 0) {
4323cf8f45c7Sdstaff 
4324cf8f45c7Sdstaff 		if (sysevent_get_attr_list(ev, &l) != 0) {
4325cf8f45c7Sdstaff 			if (errno == ENOMEM) {
4326cf8f45c7Sdstaff 				zevtchan->zn_failure_count++;
4327cf8f45c7Sdstaff 				return (EAGAIN);
4328cf8f45c7Sdstaff 			}
4329cf8f45c7Sdstaff 			return (0);
4330cf8f45c7Sdstaff 		}
4331cf8f45c7Sdstaff 		ret = 0;
4332cf8f45c7Sdstaff 
4333cf8f45c7Sdstaff 		if ((nvlist_lookup_string(l, ZONE_CB_NAME, &zonename) == 0) &&
4334cf8f45c7Sdstaff 		    (nvlist_lookup_string(l, ZONE_CB_NEWSTATE, &newstate)
4335cf8f45c7Sdstaff 		    == 0) &&
4336cf8f45c7Sdstaff 		    (nvlist_lookup_string(l, ZONE_CB_OLDSTATE, &oldstate)
4337cf8f45c7Sdstaff 		    == 0) &&
4338cf8f45c7Sdstaff 		    (nvlist_lookup_uint64(l, ZONE_CB_TIMESTAMP,
4339cf8f45c7Sdstaff 		    (uint64_t *)&when) == 0) &&
4340cf8f45c7Sdstaff 		    (nvlist_lookup_int32(l, ZONE_CB_ZONEID, &zid) == 0)) {
4341cf8f45c7Sdstaff 			ret = zevtchan->zn_callback(zonename, zid, newstate,
4342cf8f45c7Sdstaff 			    oldstate, when, zevtchan->zn_private);
4343cf8f45c7Sdstaff 		}
4344cf8f45c7Sdstaff 
4345cf8f45c7Sdstaff 		zevtchan->zn_failure_count = 0;
4346cf8f45c7Sdstaff 		nvlist_free(l);
4347cf8f45c7Sdstaff 		return (ret);
4348cf8f45c7Sdstaff 	} else {
4349cf8f45c7Sdstaff 		/*
4350cf8f45c7Sdstaff 		 * We have received an event in an unknown subclass. Ignore.
4351cf8f45c7Sdstaff 		 */
4352cf8f45c7Sdstaff 		zevtchan->zn_failure_count = 0;
4353cf8f45c7Sdstaff 		return (0);
4354cf8f45c7Sdstaff 	}
4355cf8f45c7Sdstaff }
4356cf8f45c7Sdstaff 
4357cf8f45c7Sdstaff static int
4358cf8f45c7Sdstaff zn_cb(sysevent_t *ev, void *p)
4359cf8f45c7Sdstaff {
4360cf8f45c7Sdstaff 	struct znotify *zevtchan = p;
4361cf8f45c7Sdstaff 	int error;
4362cf8f45c7Sdstaff 
4363cf8f45c7Sdstaff 	(void) pthread_mutex_lock(&(zevtchan->zn_mutex));
4364cf8f45c7Sdstaff 
4365cf8f45c7Sdstaff 	if (is_ping(ev) && !is_my_ping(ev)) {
4366cf8f45c7Sdstaff 		(void) pthread_mutex_unlock((&zevtchan->zn_mutex));
4367cf8f45c7Sdstaff 		return (0);
4368cf8f45c7Sdstaff 	}
4369cf8f45c7Sdstaff 
4370cf8f45c7Sdstaff 	if (zevtchan->zn_state == ZN_LOCKED) {
4371cf8f45c7Sdstaff 		assert(!is_ping(ev));
4372cf8f45c7Sdstaff 		zevtchan->zn_failed = B_TRUE;
4373cf8f45c7Sdstaff 		(void) pthread_mutex_unlock(&(zevtchan->zn_mutex));
4374cf8f45c7Sdstaff 		return (0);
4375cf8f45c7Sdstaff 	}
4376cf8f45c7Sdstaff 
4377cf8f45c7Sdstaff 	if (zevtchan->zn_state == ZN_PING_INFLIGHT) {
4378cf8f45c7Sdstaff 		if (is_ping(ev)) {
4379cf8f45c7Sdstaff 			zevtchan->zn_state = ZN_PING_RECEIVED;
4380cf8f45c7Sdstaff 			(void) pthread_cond_signal(&(zevtchan->zn_cond));
4381cf8f45c7Sdstaff 			(void) pthread_mutex_unlock(&(zevtchan->zn_mutex));
4382cf8f45c7Sdstaff 			return (0);
4383cf8f45c7Sdstaff 		} else {
4384cf8f45c7Sdstaff 			zevtchan->zn_failed = B_TRUE;
4385cf8f45c7Sdstaff 			(void) pthread_mutex_unlock(&(zevtchan->zn_mutex));
4386cf8f45c7Sdstaff 			return (0);
4387cf8f45c7Sdstaff 		}
4388cf8f45c7Sdstaff 	}
4389cf8f45c7Sdstaff 
4390cf8f45c7Sdstaff 	if (zevtchan->zn_state == ZN_UNLOCKED) {
4391cf8f45c7Sdstaff 
4392cf8f45c7Sdstaff 		error = do_callback(zevtchan, ev);
4393cf8f45c7Sdstaff 		(void) pthread_mutex_unlock(&(zevtchan->zn_mutex));
4394cf8f45c7Sdstaff 		/*
4395cf8f45c7Sdstaff 		 * Every ENOMEM failure causes do_callback to increment
4396cf8f45c7Sdstaff 		 * zn_failure_count and every success causes it to
4397cf8f45c7Sdstaff 		 * set zn_failure_count to zero.  If we got EAGAIN,
4398cf8f45c7Sdstaff 		 * we will sleep for zn_failure_count seconds and return
4399cf8f45c7Sdstaff 		 * EAGAIN to gpec to try again.
4400cf8f45c7Sdstaff 		 *
4401cf8f45c7Sdstaff 		 * After 55 seconds, or 10 try's we give up and drop the
4402cf8f45c7Sdstaff 		 * event.
4403cf8f45c7Sdstaff 		 */
4404cf8f45c7Sdstaff 		if (error == EAGAIN) {
4405cf8f45c7Sdstaff 			if (zevtchan->zn_failure_count > ZONE_CB_RETRY_COUNT) {
4406cf8f45c7Sdstaff 				return (0);
4407cf8f45c7Sdstaff 			}
4408cf8f45c7Sdstaff 			(void) sleep(zevtchan->zn_failure_count);
4409cf8f45c7Sdstaff 		}
4410cf8f45c7Sdstaff 		return (error);
4411cf8f45c7Sdstaff 	}
4412cf8f45c7Sdstaff 
4413cf8f45c7Sdstaff 	if (zevtchan->zn_state == ZN_PING_RECEIVED) {
4414cf8f45c7Sdstaff 		(void) pthread_mutex_unlock(&(zevtchan->zn_mutex));
4415cf8f45c7Sdstaff 		return (0);
4416cf8f45c7Sdstaff 	}
4417cf8f45c7Sdstaff 
4418cf8f45c7Sdstaff 	abort();
4419cf8f45c7Sdstaff 	return (0);
4420cf8f45c7Sdstaff }
4421cf8f45c7Sdstaff 
4422cf8f45c7Sdstaff void
4423cf8f45c7Sdstaff zonecfg_notify_critical_enter(void *h)
4424cf8f45c7Sdstaff {
4425cf8f45c7Sdstaff 	struct znotify *zevtchan = h;
4426cf8f45c7Sdstaff 
4427cf8f45c7Sdstaff 	(void) pthread_mutex_lock(&(zevtchan->zn_bigmutex));
4428cf8f45c7Sdstaff 	zevtchan->zn_state = ZN_LOCKED;
4429cf8f45c7Sdstaff }
4430cf8f45c7Sdstaff 
4431cf8f45c7Sdstaff int
4432cf8f45c7Sdstaff zonecfg_notify_critical_exit(void * h)
4433cf8f45c7Sdstaff {
4434cf8f45c7Sdstaff 
4435cf8f45c7Sdstaff 	struct znotify *zevtchan = h;
4436cf8f45c7Sdstaff 
4437cf8f45c7Sdstaff 	if (zevtchan->zn_state == ZN_UNLOCKED)
4438cf8f45c7Sdstaff 		return (0);
4439cf8f45c7Sdstaff 
4440cf8f45c7Sdstaff 	(void) pthread_mutex_lock(&(zevtchan->zn_mutex));
4441cf8f45c7Sdstaff 	zevtchan->zn_state = ZN_PING_INFLIGHT;
4442cf8f45c7Sdstaff 
4443ee519a1fSgjelinek 	(void) sysevent_evc_publish(zevtchan->zn_eventchan,
4444ee519a1fSgjelinek 	    ZONE_EVENT_STATUS_CLASS,
4445cf8f45c7Sdstaff 	    ZONE_EVENT_PING_SUBCLASS, ZONE_EVENT_PING_PUBLISHER,
4446cf8f45c7Sdstaff 	    zevtchan->zn_subscriber_id, NULL, EVCH_SLEEP);
4447cf8f45c7Sdstaff 
4448cf8f45c7Sdstaff 	while (zevtchan->zn_state != ZN_PING_RECEIVED) {
4449cf8f45c7Sdstaff 		(void) pthread_cond_wait(&(zevtchan->zn_cond),
4450cf8f45c7Sdstaff 		    &(zevtchan->zn_mutex));
4451cf8f45c7Sdstaff 	}
4452cf8f45c7Sdstaff 
4453cf8f45c7Sdstaff 	if (zevtchan->zn_failed == B_TRUE) {
4454cf8f45c7Sdstaff 		zevtchan->zn_state = ZN_LOCKED;
4455cf8f45c7Sdstaff 		zevtchan->zn_failed = B_FALSE;
4456cf8f45c7Sdstaff 		(void) pthread_mutex_unlock(&(zevtchan->zn_mutex));
4457cf8f45c7Sdstaff 		return (1);
4458cf8f45c7Sdstaff 	}
4459cf8f45c7Sdstaff 
4460cf8f45c7Sdstaff 	zevtchan->zn_state = ZN_UNLOCKED;
4461cf8f45c7Sdstaff 	(void) pthread_mutex_unlock(&(zevtchan->zn_mutex));
4462cf8f45c7Sdstaff 	(void) pthread_mutex_unlock(&(zevtchan->zn_bigmutex));
4463cf8f45c7Sdstaff 	return (0);
4464cf8f45c7Sdstaff }
4465cf8f45c7Sdstaff 
4466cf8f45c7Sdstaff void
4467cf8f45c7Sdstaff zonecfg_notify_critical_abort(void *h)
4468cf8f45c7Sdstaff {
4469cf8f45c7Sdstaff 	struct znotify *zevtchan = h;
4470cf8f45c7Sdstaff 
4471cf8f45c7Sdstaff 	zevtchan->zn_state = ZN_UNLOCKED;
4472cf8f45c7Sdstaff 	zevtchan->zn_failed = B_FALSE;
4473cf8f45c7Sdstaff 	/*
4474cf8f45c7Sdstaff 	 * Don't do anything about zn_lock. If it is held, it could only be
4475cf8f45c7Sdstaff 	 * held by zn_cb and it will be unlocked soon.
4476cf8f45c7Sdstaff 	 */
4477cf8f45c7Sdstaff 	(void) pthread_mutex_unlock(&(zevtchan->zn_bigmutex));
4478cf8f45c7Sdstaff }
4479cf8f45c7Sdstaff 
4480cf8f45c7Sdstaff void *
4481cf8f45c7Sdstaff zonecfg_notify_bind(int(*func)(const char *zonename, zoneid_t zid,
4482cf8f45c7Sdstaff     const char *newstate, const char *oldstate, hrtime_t when, void *p),
4483cf8f45c7Sdstaff     void *p)
4484cf8f45c7Sdstaff {
4485cf8f45c7Sdstaff 	struct znotify *zevtchan;
4486cf8f45c7Sdstaff 	int i = 1;
4487cf8f45c7Sdstaff 	int r;
4488cf8f45c7Sdstaff 
4489cf8f45c7Sdstaff 	zevtchan = malloc(sizeof (struct znotify));
4490cf8f45c7Sdstaff 
4491cf8f45c7Sdstaff 	if (zevtchan == NULL)
4492cf8f45c7Sdstaff 		return (NULL);
4493cf8f45c7Sdstaff 
4494cf8f45c7Sdstaff 	zevtchan->zn_private = p;
4495cf8f45c7Sdstaff 	zevtchan->zn_callback = func;
4496cf8f45c7Sdstaff 	zevtchan->zn_state = ZN_UNLOCKED;
4497cf8f45c7Sdstaff 	zevtchan->zn_failed = B_FALSE;
4498cf8f45c7Sdstaff 
4499cf8f45c7Sdstaff 	if (pthread_mutex_init(&(zevtchan->zn_mutex), NULL))
45009d4be64eSdstaff 		goto out3;
4501cf8f45c7Sdstaff 	if (pthread_cond_init(&(zevtchan->zn_cond), NULL)) {
4502cf8f45c7Sdstaff 		(void) pthread_mutex_destroy(&(zevtchan->zn_mutex));
45039d4be64eSdstaff 		goto out3;
4504cf8f45c7Sdstaff 	}
4505cf8f45c7Sdstaff 	if (pthread_mutex_init(&(zevtchan->zn_bigmutex), NULL)) {
4506cf8f45c7Sdstaff 		(void) pthread_mutex_destroy(&(zevtchan->zn_mutex));
4507cf8f45c7Sdstaff 		(void) pthread_cond_destroy(&(zevtchan->zn_cond));
45089d4be64eSdstaff 		goto out3;
4509cf8f45c7Sdstaff 	}
4510cf8f45c7Sdstaff 
4511cf8f45c7Sdstaff 	if (sysevent_evc_bind(ZONE_EVENT_CHANNEL, &(zevtchan->zn_eventchan),
4512cf8f45c7Sdstaff 	    0) != 0)
4513cf8f45c7Sdstaff 		goto out2;
4514cf8f45c7Sdstaff 
4515cf8f45c7Sdstaff 	do {
4516cf8f45c7Sdstaff 		/*
4517cf8f45c7Sdstaff 		 * At 4 digits the subscriber ID gets too long and we have
4518cf8f45c7Sdstaff 		 * no chance of successfully registering.
4519cf8f45c7Sdstaff 		 */
4520cf8f45c7Sdstaff 		if (i > 999)
45219d4be64eSdstaff 			goto out1;
4522cf8f45c7Sdstaff 
4523cf8f45c7Sdstaff 		(void) sprintf(zevtchan->zn_subscriber_id, "zone_%li_%i",
4524cf8f45c7Sdstaff 		    getpid() % 999999l, i);
4525cf8f45c7Sdstaff 
4526cf8f45c7Sdstaff 		r = sysevent_evc_subscribe(zevtchan->zn_eventchan,
4527cf8f45c7Sdstaff 		    zevtchan->zn_subscriber_id, ZONE_EVENT_STATUS_CLASS, zn_cb,
4528cf8f45c7Sdstaff 		    zevtchan, 0);
4529cf8f45c7Sdstaff 
4530cf8f45c7Sdstaff 		i++;
4531cf8f45c7Sdstaff 
4532cf8f45c7Sdstaff 	} while (r);
4533cf8f45c7Sdstaff 
4534cf8f45c7Sdstaff 	return (zevtchan);
45359d4be64eSdstaff out1:
4536cf8f45c7Sdstaff 	sysevent_evc_unbind(zevtchan->zn_eventchan);
45379d4be64eSdstaff out2:
4538cf8f45c7Sdstaff 	(void) pthread_mutex_destroy(&zevtchan->zn_mutex);
4539cf8f45c7Sdstaff 	(void) pthread_cond_destroy(&zevtchan->zn_cond);
4540cf8f45c7Sdstaff 	(void) pthread_mutex_destroy(&(zevtchan->zn_bigmutex));
45419d4be64eSdstaff out3:
4542cf8f45c7Sdstaff 	free(zevtchan);
4543cf8f45c7Sdstaff 
4544cf8f45c7Sdstaff 	return (NULL);
4545cf8f45c7Sdstaff }
4546cf8f45c7Sdstaff 
4547cf8f45c7Sdstaff void
4548cf8f45c7Sdstaff zonecfg_notify_unbind(void *handle)
4549cf8f45c7Sdstaff {
4550cf8f45c7Sdstaff 
4551cf8f45c7Sdstaff 	int ret;
4552cf8f45c7Sdstaff 
4553cf8f45c7Sdstaff 	sysevent_evc_unbind(((struct znotify *)handle)->zn_eventchan);
4554cf8f45c7Sdstaff 	/*
4555cf8f45c7Sdstaff 	 * Check that all evc threads have gone away. This should be
4556cf8f45c7Sdstaff 	 * enforced by sysevent_evc_unbind.
4557cf8f45c7Sdstaff 	 */
4558cf8f45c7Sdstaff 	ret = pthread_mutex_trylock(&((struct znotify *)handle)->zn_mutex);
4559cf8f45c7Sdstaff 
4560cf8f45c7Sdstaff 	if (ret)
4561cf8f45c7Sdstaff 		abort();
4562cf8f45c7Sdstaff 
4563cf8f45c7Sdstaff 	(void) pthread_mutex_unlock(&((struct znotify *)handle)->zn_mutex);
4564cf8f45c7Sdstaff 	(void) pthread_mutex_destroy(&((struct znotify *)handle)->zn_mutex);
4565cf8f45c7Sdstaff 	(void) pthread_cond_destroy(&((struct znotify *)handle)->zn_cond);
4566cf8f45c7Sdstaff 	(void) pthread_mutex_destroy(&((struct znotify *)handle)->zn_bigmutex);
4567cf8f45c7Sdstaff 
4568cf8f45c7Sdstaff 	free(handle);
4569cf8f45c7Sdstaff }
4570cf8f45c7Sdstaff 
4571fa9e4066Sahrens static int
4572fa9e4066Sahrens zonecfg_add_ds_core(zone_dochandle_t handle, struct zone_dstab *tabptr)
4573fa9e4066Sahrens {
4574fa9e4066Sahrens 	xmlNodePtr newnode, cur = handle->zone_dh_cur;
4575fa9e4066Sahrens 	int err;
4576fa9e4066Sahrens 
4577fa9e4066Sahrens 	newnode = xmlNewTextChild(cur, NULL, DTD_ELEM_DATASET, NULL);
4578fa9e4066Sahrens 	if ((err = newprop(newnode, DTD_ATTR_NAME,
4579fa9e4066Sahrens 	    tabptr->zone_dataset_name)) != Z_OK)
4580fa9e4066Sahrens 		return (err);
4581fa9e4066Sahrens 	return (Z_OK);
4582fa9e4066Sahrens }
4583fa9e4066Sahrens 
4584fa9e4066Sahrens int
4585fa9e4066Sahrens zonecfg_add_ds(zone_dochandle_t handle, struct zone_dstab *tabptr)
4586fa9e4066Sahrens {
4587fa9e4066Sahrens 	int err;
4588fa9e4066Sahrens 
4589fa9e4066Sahrens 	if (tabptr == NULL)
4590fa9e4066Sahrens 		return (Z_INVAL);
4591fa9e4066Sahrens 
4592fa9e4066Sahrens 	if ((err = operation_prep(handle)) != Z_OK)
4593fa9e4066Sahrens 		return (err);
4594fa9e4066Sahrens 
4595fa9e4066Sahrens 	if ((err = zonecfg_add_ds_core(handle, tabptr)) != Z_OK)
4596fa9e4066Sahrens 		return (err);
4597fa9e4066Sahrens 
4598fa9e4066Sahrens 	return (Z_OK);
4599fa9e4066Sahrens }
4600fa9e4066Sahrens 
4601fa9e4066Sahrens static int
4602fa9e4066Sahrens zonecfg_delete_ds_core(zone_dochandle_t handle, struct zone_dstab *tabptr)
4603fa9e4066Sahrens {
4604fa9e4066Sahrens 	xmlNodePtr cur = handle->zone_dh_cur;
4605fa9e4066Sahrens 
4606fa9e4066Sahrens 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
4607fa9e4066Sahrens 		if (xmlStrcmp(cur->name, DTD_ELEM_DATASET))
4608fa9e4066Sahrens 			continue;
4609fa9e4066Sahrens 
4610fa9e4066Sahrens 		if (match_prop(cur, DTD_ATTR_NAME,
4611fa9e4066Sahrens 		    tabptr->zone_dataset_name)) {
4612fa9e4066Sahrens 			xmlUnlinkNode(cur);
4613fa9e4066Sahrens 			xmlFreeNode(cur);
4614fa9e4066Sahrens 			return (Z_OK);
4615fa9e4066Sahrens 		}
4616fa9e4066Sahrens 	}
4617fa9e4066Sahrens 	return (Z_NO_RESOURCE_ID);
4618fa9e4066Sahrens }
4619fa9e4066Sahrens 
4620fa9e4066Sahrens int
4621fa9e4066Sahrens zonecfg_delete_ds(zone_dochandle_t handle, struct zone_dstab *tabptr)
4622fa9e4066Sahrens {
4623fa9e4066Sahrens 	int err;
4624fa9e4066Sahrens 
4625fa9e4066Sahrens 	if (tabptr == NULL)
4626fa9e4066Sahrens 		return (Z_INVAL);
4627fa9e4066Sahrens 
4628fa9e4066Sahrens 	if ((err = operation_prep(handle)) != Z_OK)
4629fa9e4066Sahrens 		return (err);
4630fa9e4066Sahrens 
4631fa9e4066Sahrens 	if ((err = zonecfg_delete_ds_core(handle, tabptr)) != Z_OK)
4632fa9e4066Sahrens 		return (err);
4633fa9e4066Sahrens 
4634fa9e4066Sahrens 	return (Z_OK);
4635fa9e4066Sahrens }
4636fa9e4066Sahrens 
4637fa9e4066Sahrens int
4638fa9e4066Sahrens zonecfg_modify_ds(
4639fa9e4066Sahrens 	zone_dochandle_t handle,
4640fa9e4066Sahrens 	struct zone_dstab *oldtabptr,
4641fa9e4066Sahrens 	struct zone_dstab *newtabptr)
4642fa9e4066Sahrens {
4643fa9e4066Sahrens 	int err;
4644fa9e4066Sahrens 
4645fa9e4066Sahrens 	if (oldtabptr == NULL || newtabptr == NULL)
4646fa9e4066Sahrens 		return (Z_INVAL);
4647fa9e4066Sahrens 
4648fa9e4066Sahrens 	if ((err = operation_prep(handle)) != Z_OK)
4649fa9e4066Sahrens 		return (err);
4650fa9e4066Sahrens 
4651fa9e4066Sahrens 	if ((err = zonecfg_delete_ds_core(handle, oldtabptr)) != Z_OK)
4652fa9e4066Sahrens 		return (err);
4653fa9e4066Sahrens 
4654fa9e4066Sahrens 	if ((err = zonecfg_add_ds_core(handle, newtabptr)) != Z_OK)
4655fa9e4066Sahrens 		return (err);
4656fa9e4066Sahrens 
4657fa9e4066Sahrens 	return (Z_OK);
4658fa9e4066Sahrens }
4659fa9e4066Sahrens 
4660fa9e4066Sahrens int
4661fa9e4066Sahrens zonecfg_lookup_ds(zone_dochandle_t handle, struct zone_dstab *tabptr)
4662fa9e4066Sahrens {
4663fa9e4066Sahrens 	xmlNodePtr cur, firstmatch;
4664fa9e4066Sahrens 	int err;
4665fa9e4066Sahrens 	char dataset[MAXNAMELEN];
4666fa9e4066Sahrens 
4667fa9e4066Sahrens 	if (tabptr == NULL)
4668fa9e4066Sahrens 		return (Z_INVAL);
4669fa9e4066Sahrens 
4670fa9e4066Sahrens 	if ((err = operation_prep(handle)) != Z_OK)
4671fa9e4066Sahrens 		return (err);
4672fa9e4066Sahrens 
4673fa9e4066Sahrens 	cur = handle->zone_dh_cur;
4674fa9e4066Sahrens 	firstmatch = NULL;
4675fa9e4066Sahrens 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
4676fa9e4066Sahrens 		if (xmlStrcmp(cur->name, DTD_ELEM_DATASET))
4677fa9e4066Sahrens 			continue;
4678fa9e4066Sahrens 		if (strlen(tabptr->zone_dataset_name) > 0) {
4679fa9e4066Sahrens 			if ((fetchprop(cur, DTD_ATTR_NAME, dataset,
4680fa9e4066Sahrens 			    sizeof (dataset)) == Z_OK) &&
4681fa9e4066Sahrens 			    (strcmp(tabptr->zone_dataset_name,
4682fa9e4066Sahrens 			    dataset) == 0)) {
4683fa9e4066Sahrens 				if (firstmatch == NULL)
4684fa9e4066Sahrens 					firstmatch = cur;
4685fa9e4066Sahrens 				else
4686fa9e4066Sahrens 					return (Z_INSUFFICIENT_SPEC);
4687fa9e4066Sahrens 			}
4688fa9e4066Sahrens 		}
4689fa9e4066Sahrens 	}
4690fa9e4066Sahrens 	if (firstmatch == NULL)
4691fa9e4066Sahrens 		return (Z_NO_RESOURCE_ID);
4692fa9e4066Sahrens 
4693fa9e4066Sahrens 	cur = firstmatch;
4694fa9e4066Sahrens 
4695fa9e4066Sahrens 	if ((err = fetchprop(cur, DTD_ATTR_NAME, tabptr->zone_dataset_name,
4696fa9e4066Sahrens 	    sizeof (tabptr->zone_dataset_name))) != Z_OK)
4697fa9e4066Sahrens 		return (err);
4698fa9e4066Sahrens 
4699fa9e4066Sahrens 	return (Z_OK);
4700fa9e4066Sahrens }
4701fa9e4066Sahrens 
4702fa9e4066Sahrens int
4703fa9e4066Sahrens zonecfg_setdsent(zone_dochandle_t handle)
4704fa9e4066Sahrens {
4705fa9e4066Sahrens 	return (zonecfg_setent(handle));
4706fa9e4066Sahrens }
4707fa9e4066Sahrens 
4708fa9e4066Sahrens int
4709fa9e4066Sahrens zonecfg_getdsent(zone_dochandle_t handle, struct zone_dstab *tabptr)
4710fa9e4066Sahrens {
4711fa9e4066Sahrens 	xmlNodePtr cur;
4712fa9e4066Sahrens 	int err;
4713fa9e4066Sahrens 
4714fa9e4066Sahrens 	if (handle == NULL)
4715fa9e4066Sahrens 		return (Z_INVAL);
4716fa9e4066Sahrens 
4717fa9e4066Sahrens 	if ((cur = handle->zone_dh_cur) == NULL)
4718fa9e4066Sahrens 		return (Z_NO_ENTRY);
4719fa9e4066Sahrens 
4720fa9e4066Sahrens 	for (; cur != NULL; cur = cur->next)
4721fa9e4066Sahrens 		if (!xmlStrcmp(cur->name, DTD_ELEM_DATASET))
4722fa9e4066Sahrens 			break;
4723fa9e4066Sahrens 	if (cur == NULL) {
4724fa9e4066Sahrens 		handle->zone_dh_cur = handle->zone_dh_top;
4725fa9e4066Sahrens 		return (Z_NO_ENTRY);
4726fa9e4066Sahrens 	}
4727fa9e4066Sahrens 
4728fa9e4066Sahrens 	if ((err = fetchprop(cur, DTD_ATTR_NAME, tabptr->zone_dataset_name,
4729fa9e4066Sahrens 	    sizeof (tabptr->zone_dataset_name))) != Z_OK) {
4730fa9e4066Sahrens 		handle->zone_dh_cur = handle->zone_dh_top;
4731fa9e4066Sahrens 		return (err);
4732fa9e4066Sahrens 	}
4733fa9e4066Sahrens 
4734fa9e4066Sahrens 	handle->zone_dh_cur = cur->next;
4735fa9e4066Sahrens 	return (Z_OK);
4736fa9e4066Sahrens }
4737fa9e4066Sahrens 
4738fa9e4066Sahrens int
4739fa9e4066Sahrens zonecfg_enddsent(zone_dochandle_t handle)
4740fa9e4066Sahrens {
4741fa9e4066Sahrens 	return (zonecfg_endent(handle));
4742fa9e4066Sahrens }
4743ee519a1fSgjelinek 
4744ee519a1fSgjelinek int
4745ee519a1fSgjelinek zonecfg_setpkgent(zone_dochandle_t handle)
4746ee519a1fSgjelinek {
4747ee519a1fSgjelinek 	return (zonecfg_setent(handle));
4748ee519a1fSgjelinek }
4749ee519a1fSgjelinek 
4750ee519a1fSgjelinek int
4751ee519a1fSgjelinek zonecfg_getpkgent(zone_dochandle_t handle, struct zone_pkgtab *tabptr)
4752ee519a1fSgjelinek {
4753ee519a1fSgjelinek 	xmlNodePtr cur;
4754ee519a1fSgjelinek 	int err;
4755ee519a1fSgjelinek 
4756ee519a1fSgjelinek 	if (handle == NULL)
4757ee519a1fSgjelinek 		return (Z_INVAL);
4758ee519a1fSgjelinek 
4759ee519a1fSgjelinek 	if ((cur = handle->zone_dh_cur) == NULL)
4760ee519a1fSgjelinek 		return (Z_NO_ENTRY);
4761ee519a1fSgjelinek 
4762ee519a1fSgjelinek 	for (; cur != NULL; cur = cur->next)
4763ee519a1fSgjelinek 		if (!xmlStrcmp(cur->name, DTD_ELEM_PACKAGE))
4764ee519a1fSgjelinek 			break;
4765ee519a1fSgjelinek 	if (cur == NULL) {
4766ee519a1fSgjelinek 		handle->zone_dh_cur = handle->zone_dh_top;
4767ee519a1fSgjelinek 		return (Z_NO_ENTRY);
4768ee519a1fSgjelinek 	}
4769ee519a1fSgjelinek 
4770ee519a1fSgjelinek 	if ((err = fetchprop(cur, DTD_ATTR_NAME, tabptr->zone_pkg_name,
4771ee519a1fSgjelinek 	    sizeof (tabptr->zone_pkg_name))) != Z_OK) {
4772ee519a1fSgjelinek 		handle->zone_dh_cur = handle->zone_dh_top;
4773ee519a1fSgjelinek 		return (err);
4774ee519a1fSgjelinek 	}
4775ee519a1fSgjelinek 
4776ee519a1fSgjelinek 	if ((err = fetchprop(cur, DTD_ATTR_VERSION, tabptr->zone_pkg_version,
4777ee519a1fSgjelinek 	    sizeof (tabptr->zone_pkg_version))) != Z_OK) {
4778ee519a1fSgjelinek 		handle->zone_dh_cur = handle->zone_dh_top;
4779ee519a1fSgjelinek 		return (err);
4780ee519a1fSgjelinek 	}
4781ee519a1fSgjelinek 
4782ee519a1fSgjelinek 	handle->zone_dh_cur = cur->next;
4783ee519a1fSgjelinek 	return (Z_OK);
4784ee519a1fSgjelinek }
4785ee519a1fSgjelinek 
4786ee519a1fSgjelinek int
4787ee519a1fSgjelinek zonecfg_endpkgent(zone_dochandle_t handle)
4788ee519a1fSgjelinek {
4789ee519a1fSgjelinek 	return (zonecfg_endent(handle));
4790ee519a1fSgjelinek }
4791ee519a1fSgjelinek 
4792ee519a1fSgjelinek int
4793ee519a1fSgjelinek zonecfg_setpatchent(zone_dochandle_t handle)
4794ee519a1fSgjelinek {
4795ee519a1fSgjelinek 	return (zonecfg_setent(handle));
4796ee519a1fSgjelinek }
4797ee519a1fSgjelinek 
4798ee519a1fSgjelinek int
4799ee519a1fSgjelinek zonecfg_getpatchent(zone_dochandle_t handle, struct zone_patchtab *tabptr)
4800ee519a1fSgjelinek {
4801ee519a1fSgjelinek 	xmlNodePtr cur;
4802ee519a1fSgjelinek 	int err;
4803ee519a1fSgjelinek 
4804ee519a1fSgjelinek 	if (handle == NULL)
4805ee519a1fSgjelinek 		return (Z_INVAL);
4806ee519a1fSgjelinek 
4807ee519a1fSgjelinek 	if ((cur = handle->zone_dh_cur) == NULL)
4808ee519a1fSgjelinek 		return (Z_NO_ENTRY);
4809ee519a1fSgjelinek 
4810ee519a1fSgjelinek 	for (; cur != NULL; cur = cur->next)
4811ee519a1fSgjelinek 		if (!xmlStrcmp(cur->name, DTD_ELEM_PATCH))
4812ee519a1fSgjelinek 			break;
4813ee519a1fSgjelinek 	if (cur == NULL) {
4814ee519a1fSgjelinek 		handle->zone_dh_cur = handle->zone_dh_top;
4815ee519a1fSgjelinek 		return (Z_NO_ENTRY);
4816ee519a1fSgjelinek 	}
4817ee519a1fSgjelinek 
4818ee519a1fSgjelinek 	if ((err = fetchprop(cur, DTD_ATTR_ID, tabptr->zone_patch_id,
4819ee519a1fSgjelinek 	    sizeof (tabptr->zone_patch_id))) != Z_OK) {
4820ee519a1fSgjelinek 		handle->zone_dh_cur = handle->zone_dh_top;
4821ee519a1fSgjelinek 		return (err);
4822ee519a1fSgjelinek 	}
4823ee519a1fSgjelinek 
4824ee519a1fSgjelinek 	handle->zone_dh_cur = cur->next;
4825ee519a1fSgjelinek 	return (Z_OK);
4826ee519a1fSgjelinek }
4827ee519a1fSgjelinek 
4828ee519a1fSgjelinek int
4829ee519a1fSgjelinek zonecfg_endpatchent(zone_dochandle_t handle)
4830ee519a1fSgjelinek {
4831ee519a1fSgjelinek 	return (zonecfg_endent(handle));
4832ee519a1fSgjelinek }
4833ee519a1fSgjelinek 
4834ee519a1fSgjelinek int
4835ee519a1fSgjelinek zonecfg_setdevperment(zone_dochandle_t handle)
4836ee519a1fSgjelinek {
4837ee519a1fSgjelinek 	return (zonecfg_setent(handle));
4838ee519a1fSgjelinek }
4839ee519a1fSgjelinek 
4840ee519a1fSgjelinek int
4841ee519a1fSgjelinek zonecfg_getdevperment(zone_dochandle_t handle, struct zone_devpermtab *tabptr)
4842ee519a1fSgjelinek {
4843ee519a1fSgjelinek 	xmlNodePtr cur;
4844ee519a1fSgjelinek 	int err;
4845ee519a1fSgjelinek 	char buf[128];
4846ee519a1fSgjelinek 
4847ee519a1fSgjelinek 	tabptr->zone_devperm_acl = NULL;
4848ee519a1fSgjelinek 
4849ee519a1fSgjelinek 	if (handle == NULL)
4850ee519a1fSgjelinek 		return (Z_INVAL);
4851ee519a1fSgjelinek 
4852ee519a1fSgjelinek 	if ((cur = handle->zone_dh_cur) == NULL)
4853ee519a1fSgjelinek 		return (Z_NO_ENTRY);
4854ee519a1fSgjelinek 
4855ee519a1fSgjelinek 	for (; cur != NULL; cur = cur->next)
4856ee519a1fSgjelinek 		if (!xmlStrcmp(cur->name, DTD_ELEM_DEV_PERM))
4857ee519a1fSgjelinek 			break;
4858ee519a1fSgjelinek 	if (cur == NULL) {
4859ee519a1fSgjelinek 		handle->zone_dh_cur = handle->zone_dh_top;
4860ee519a1fSgjelinek 		return (Z_NO_ENTRY);
4861ee519a1fSgjelinek 	}
4862ee519a1fSgjelinek 
4863ee519a1fSgjelinek 	if ((err = fetchprop(cur, DTD_ATTR_NAME, tabptr->zone_devperm_name,
4864ee519a1fSgjelinek 	    sizeof (tabptr->zone_devperm_name))) != Z_OK) {
4865ee519a1fSgjelinek 		handle->zone_dh_cur = handle->zone_dh_top;
4866ee519a1fSgjelinek 		return (err);
4867ee519a1fSgjelinek 	}
4868ee519a1fSgjelinek 
4869ee519a1fSgjelinek 	if ((err = fetchprop(cur, DTD_ATTR_UID, buf, sizeof (buf))) != Z_OK) {
4870ee519a1fSgjelinek 		handle->zone_dh_cur = handle->zone_dh_top;
4871ee519a1fSgjelinek 		return (err);
4872ee519a1fSgjelinek 	}
4873ee519a1fSgjelinek 	tabptr->zone_devperm_uid = (uid_t)atol(buf);
4874ee519a1fSgjelinek 
4875ee519a1fSgjelinek 	if ((err = fetchprop(cur, DTD_ATTR_GID, buf, sizeof (buf))) != Z_OK) {
4876ee519a1fSgjelinek 		handle->zone_dh_cur = handle->zone_dh_top;
4877ee519a1fSgjelinek 		return (err);
4878ee519a1fSgjelinek 	}
4879ee519a1fSgjelinek 	tabptr->zone_devperm_gid = (gid_t)atol(buf);
4880ee519a1fSgjelinek 
4881ee519a1fSgjelinek 	if ((err = fetchprop(cur, DTD_ATTR_MODE, buf, sizeof (buf))) != Z_OK) {
4882ee519a1fSgjelinek 		handle->zone_dh_cur = handle->zone_dh_top;
4883ee519a1fSgjelinek 		return (err);
4884ee519a1fSgjelinek 	}
4885ee519a1fSgjelinek 	tabptr->zone_devperm_mode = (mode_t)strtol(buf, (char **)NULL, 8);
4886ee519a1fSgjelinek 
4887ee519a1fSgjelinek 	if ((err = fetch_alloc_prop(cur, DTD_ATTR_ACL,
4888ee519a1fSgjelinek 	    &(tabptr->zone_devperm_acl))) != Z_OK) {
4889ee519a1fSgjelinek 		handle->zone_dh_cur = handle->zone_dh_top;
4890ee519a1fSgjelinek 		return (err);
4891ee519a1fSgjelinek 	}
4892ee519a1fSgjelinek 
4893ee519a1fSgjelinek 	handle->zone_dh_cur = cur->next;
4894ee519a1fSgjelinek 	return (Z_OK);
4895ee519a1fSgjelinek }
4896ee519a1fSgjelinek 
4897ee519a1fSgjelinek int
4898ee519a1fSgjelinek zonecfg_enddevperment(zone_dochandle_t handle)
4899ee519a1fSgjelinek {
4900ee519a1fSgjelinek 	return (zonecfg_endent(handle));
4901ee519a1fSgjelinek }
4902ee519a1fSgjelinek 
4903ee519a1fSgjelinek /*
4904ee519a1fSgjelinek  * Process a list of pkgs from an entry in the contents file, adding each pkg
4905ee519a1fSgjelinek  * name to the list of pkgs.
4906ee519a1fSgjelinek  *
4907ee519a1fSgjelinek  * It is possible for the pkg name to be preceeded by a special character
4908ee519a1fSgjelinek  * which indicates some bookkeeping information for pkging.  Check if the
4909ee519a1fSgjelinek  * first char is not an Alpha char.  If so, skip over it.
4910ee519a1fSgjelinek  */
4911ee519a1fSgjelinek static int
4912ee519a1fSgjelinek add_pkg_list(char *lastp, char ***plist, int *pcnt)
4913ee519a1fSgjelinek {
4914ee519a1fSgjelinek 	char	*p;
4915ee519a1fSgjelinek 	int	pkg_cnt = *pcnt;
4916ee519a1fSgjelinek 	char	**pkgs = *plist;
4917ee519a1fSgjelinek 	int	res = Z_OK;
4918ee519a1fSgjelinek 
4919ee519a1fSgjelinek 	while ((p = strtok_r(NULL, " ", &lastp)) != NULL) {
4920ee519a1fSgjelinek 		char	**tmpp;
4921ee519a1fSgjelinek 		int	i;
4922ee519a1fSgjelinek 
4923ee519a1fSgjelinek 		/* skip over any special pkg bookkeeping char */
4924ee519a1fSgjelinek 		if (!isalpha(*p))
4925ee519a1fSgjelinek 			p++;
4926ee519a1fSgjelinek 
4927ee519a1fSgjelinek 		/* Check if the pkg is already in the list */
4928ee519a1fSgjelinek 		for (i = 0; i < pkg_cnt; i++) {
4929ee519a1fSgjelinek 			if (strcmp(p, pkgs[i]) == 0)
4930ee519a1fSgjelinek 				break;
4931ee519a1fSgjelinek 		}
4932ee519a1fSgjelinek 
4933ee519a1fSgjelinek 		if (i < pkg_cnt)
4934ee519a1fSgjelinek 			continue;
4935ee519a1fSgjelinek 
4936ee519a1fSgjelinek 		/* The pkg is not in the list; add it. */
4937ee519a1fSgjelinek 		if ((tmpp = (char **)realloc(pkgs,
4938ee519a1fSgjelinek 		    sizeof (char *) * (pkg_cnt + 1))) == NULL) {
4939ee519a1fSgjelinek 			res = Z_NOMEM;
4940ee519a1fSgjelinek 			break;
4941ee519a1fSgjelinek 		}
4942ee519a1fSgjelinek 		pkgs = tmpp;
4943ee519a1fSgjelinek 
4944ee519a1fSgjelinek 		if ((pkgs[pkg_cnt] = strdup(p)) == NULL) {
4945ee519a1fSgjelinek 			res = Z_NOMEM;
4946ee519a1fSgjelinek 			break;
4947ee519a1fSgjelinek 		}
4948ee519a1fSgjelinek 		pkg_cnt++;
4949ee519a1fSgjelinek 	}
4950ee519a1fSgjelinek 
4951ee519a1fSgjelinek 	*plist = pkgs;
4952ee519a1fSgjelinek 	*pcnt = pkg_cnt;
4953ee519a1fSgjelinek 
4954ee519a1fSgjelinek 	return (res);
4955ee519a1fSgjelinek }
4956ee519a1fSgjelinek 
4957ee519a1fSgjelinek /*
4958ee519a1fSgjelinek  * Process an entry from the contents file (type "directory") and if the
4959ee519a1fSgjelinek  * directory path is in the list of paths, add the associated list of pkgs
4960ee519a1fSgjelinek  * to the pkg list.  The input parameter "entry" will be broken up by
4961ee519a1fSgjelinek  * the parser within this function so its value will be modified when this
4962ee519a1fSgjelinek  * function exits.
4963ee519a1fSgjelinek  *
4964ee519a1fSgjelinek  * The entries we are looking for will look something like:
4965ee519a1fSgjelinek  *	/usr d none 0755 root sys SUNWctpls SUNWidnl SUNWlibCf ....
4966ee519a1fSgjelinek  */
4967ee519a1fSgjelinek static int
4968ee519a1fSgjelinek get_path_pkgs(char *entry, char **paths, int cnt, char ***pkgs, int *pkg_cnt)
4969ee519a1fSgjelinek {
4970ee519a1fSgjelinek 	char	*f1;
4971ee519a1fSgjelinek 	char	*f2;
4972ee519a1fSgjelinek 	char	*lastp;
4973ee519a1fSgjelinek 	int	i;
4974ee519a1fSgjelinek 	int	res = Z_OK;
4975ee519a1fSgjelinek 
4976ee519a1fSgjelinek 	if ((f1 = strtok_r(entry, " ", &lastp)) == NULL ||
4977ee519a1fSgjelinek 	    (f2 = strtok_r(NULL, " ", &lastp)) == NULL || strcmp(f2, "d") != 0)
4978ee519a1fSgjelinek 		return (Z_OK);
4979ee519a1fSgjelinek 
4980ee519a1fSgjelinek 	/* Check if this directory entry is in the list of paths. */
4981ee519a1fSgjelinek 	for (i = 0; i < cnt; i++) {
4982ee519a1fSgjelinek 		if (fnmatch(paths[i], f1, FNM_PATHNAME) == 0) {
4983ee519a1fSgjelinek 			/*
4984ee519a1fSgjelinek 			 * We do want the pkgs for this path.  First, skip
4985ee519a1fSgjelinek 			 * over the next 4 fields in the entry so that we call
4986ee519a1fSgjelinek 			 * add_pkg_list starting with the pkg names.
4987ee519a1fSgjelinek 			 */
4988ee519a1fSgjelinek 			int j;
498907b574eeSgjelinek 			char	*nlp;
4990ee519a1fSgjelinek 
4991ee519a1fSgjelinek 			for (j = 0; j < 4 &&
4992ffbafc53Scomay 			    strtok_r(NULL, " ", &lastp) != NULL; j++)
4993ffbafc53Scomay 				;
4994ee519a1fSgjelinek 			/*
4995ee519a1fSgjelinek 			 * If there are < 4 fields this entry is corrupt,
4996ee519a1fSgjelinek 			 * just skip it.
4997ee519a1fSgjelinek 			 */
4998ee519a1fSgjelinek 			if (j < 4)
4999ee519a1fSgjelinek 				return (Z_OK);
5000ee519a1fSgjelinek 
500107b574eeSgjelinek 			/* strip newline from the line */
500207b574eeSgjelinek 			nlp = (lastp + strlen(lastp) - 1);
500307b574eeSgjelinek 			if (*nlp == '\n')
500407b574eeSgjelinek 				*nlp = '\0';
500507b574eeSgjelinek 
5006ee519a1fSgjelinek 			res = add_pkg_list(lastp, pkgs, pkg_cnt);
5007ee519a1fSgjelinek 			break;
5008ee519a1fSgjelinek 		}
5009ee519a1fSgjelinek 	}
5010ee519a1fSgjelinek 
5011ee519a1fSgjelinek 	return (res);
5012ee519a1fSgjelinek }
5013ee519a1fSgjelinek 
5014ee519a1fSgjelinek /*
5015ee519a1fSgjelinek  * Read an entry from a pkginfo or contents file.  Some of these lines can
5016ee519a1fSgjelinek  * either be arbitrarily long or be continued by a backslash at the end of
5017ee519a1fSgjelinek  * the line.  This function coalesces lines that are longer than the read
5018ee519a1fSgjelinek  * buffer, and lines that are continued, into one buffer which is returned.
5019ee519a1fSgjelinek  * The caller must free this memory.  NULL is returned when we hit EOF or
5020ee519a1fSgjelinek  * if we run out of memory (errno is set to ENOMEM).
5021ee519a1fSgjelinek  */
5022ee519a1fSgjelinek static char *
5023ee519a1fSgjelinek read_pkg_data(FILE *fp)
5024ee519a1fSgjelinek {
5025ee519a1fSgjelinek 	char *start;
5026ee519a1fSgjelinek 	char *inp;
5027ee519a1fSgjelinek 	char *p;
5028ee519a1fSgjelinek 	int char_cnt = 0;
5029ee519a1fSgjelinek 
5030ee519a1fSgjelinek 	errno = 0;
5031ee519a1fSgjelinek 	if ((start = (char *)malloc(PKGINFO_RD_LEN)) == NULL) {
5032ee519a1fSgjelinek 		errno = ENOMEM;
5033ee519a1fSgjelinek 		return (NULL);
5034ee519a1fSgjelinek 	}
5035ee519a1fSgjelinek 
5036ee519a1fSgjelinek 	inp = start;
5037ee519a1fSgjelinek 	while ((p = fgets(inp, PKGINFO_RD_LEN, fp)) != NULL) {
5038ee519a1fSgjelinek 		int len;
5039ee519a1fSgjelinek 
5040ee519a1fSgjelinek 		len = strlen(inp);
5041ee519a1fSgjelinek 		if (inp[len - 1] == '\n' &&
5042ee519a1fSgjelinek 		    (len == 1 || inp[len - 2] != '\\')) {
5043ee519a1fSgjelinek 			char_cnt = len;
5044ee519a1fSgjelinek 			break;
5045ee519a1fSgjelinek 		}
5046ee519a1fSgjelinek 
5047ee519a1fSgjelinek 		if (inp[len - 2] == '\\')
5048ee519a1fSgjelinek 			char_cnt += len - 2;
5049ee519a1fSgjelinek 		else
5050ee519a1fSgjelinek 			char_cnt += PKGINFO_RD_LEN - 1;
5051ee519a1fSgjelinek 
5052ee519a1fSgjelinek 		if ((p = realloc(start, char_cnt + PKGINFO_RD_LEN)) == NULL) {
5053ee519a1fSgjelinek 			errno = ENOMEM;
5054ee519a1fSgjelinek 			break;
5055ee519a1fSgjelinek 		}
5056ee519a1fSgjelinek 
5057ee519a1fSgjelinek 		start = p;
5058ee519a1fSgjelinek 		inp = start + char_cnt;
5059ee519a1fSgjelinek 	}
5060ee519a1fSgjelinek 
5061ee519a1fSgjelinek 	if (errno == ENOMEM || (p == NULL && char_cnt == 0)) {
5062ee519a1fSgjelinek 		free(start);
5063ee519a1fSgjelinek 		start = NULL;
5064ee519a1fSgjelinek 	}
5065ee519a1fSgjelinek 
5066ee519a1fSgjelinek 	return (start);
5067ee519a1fSgjelinek }
5068ee519a1fSgjelinek 
5069ee519a1fSgjelinek static void
5070ee519a1fSgjelinek free_ipd_pkgs(char **pkgs, int cnt)
5071ee519a1fSgjelinek {
5072ee519a1fSgjelinek 	int i;
5073ee519a1fSgjelinek 
5074ee519a1fSgjelinek 	for (i = 0; i < cnt; i++)
5075ee519a1fSgjelinek 		free(pkgs[i]);
5076ee519a1fSgjelinek 	free(pkgs);
5077ee519a1fSgjelinek }
5078ee519a1fSgjelinek 
5079ee519a1fSgjelinek /*
5080ee519a1fSgjelinek  * Get the list of inherited-pkg-dirs (ipd) for the zone and then get the
5081ee519a1fSgjelinek  * list of pkgs that deliver into those dirs.
5082ee519a1fSgjelinek  */
5083ee519a1fSgjelinek static int
5084ee519a1fSgjelinek get_ipd_pkgs(zone_dochandle_t handle, char ***pkg_list, int *cnt)
5085ee519a1fSgjelinek {
5086ee519a1fSgjelinek 	int	res;
5087ee519a1fSgjelinek 	struct zone_fstab fstab;
5088ee519a1fSgjelinek 	int	ipd_cnt = 0;
5089ee519a1fSgjelinek 	char	**ipds = NULL;
5090ee519a1fSgjelinek 	int	pkg_cnt = 0;
5091ee519a1fSgjelinek 	char	**pkgs = NULL;
5092ee519a1fSgjelinek 	int	i;
5093ee519a1fSgjelinek 
5094ee519a1fSgjelinek 	if ((res = zonecfg_setipdent(handle)) != Z_OK)
5095ee519a1fSgjelinek 		return (res);
5096ee519a1fSgjelinek 
5097ee519a1fSgjelinek 	while (zonecfg_getipdent(handle, &fstab) == Z_OK) {
5098ee519a1fSgjelinek 		char	**p;
5099ee519a1fSgjelinek 		int	len;
5100ee519a1fSgjelinek 
5101ee519a1fSgjelinek 		if ((p = (char **)realloc(ipds,
5102ee519a1fSgjelinek 		    sizeof (char *) * (ipd_cnt + 2))) == NULL) {
5103ee519a1fSgjelinek 			res = Z_NOMEM;
5104ee519a1fSgjelinek 			break;
5105ee519a1fSgjelinek 		}
5106ee519a1fSgjelinek 		ipds = p;
5107ee519a1fSgjelinek 
5108ee519a1fSgjelinek 		if ((ipds[ipd_cnt] = strdup(fstab.zone_fs_dir)) == NULL) {
5109ee519a1fSgjelinek 			res = Z_NOMEM;
5110ee519a1fSgjelinek 			break;
5111ee519a1fSgjelinek 		}
5112ee519a1fSgjelinek 		ipd_cnt++;
5113ee519a1fSgjelinek 
5114ee519a1fSgjelinek 		len = strlen(fstab.zone_fs_dir) + 3;
5115ee519a1fSgjelinek 		if ((ipds[ipd_cnt] = malloc(len)) == NULL) {
5116ee519a1fSgjelinek 			res = Z_NOMEM;
5117ee519a1fSgjelinek 			break;
5118ee519a1fSgjelinek 		}
5119ee519a1fSgjelinek 
5120ee519a1fSgjelinek 		(void) snprintf(ipds[ipd_cnt], len, "%s/*", fstab.zone_fs_dir);
5121ee519a1fSgjelinek 		ipd_cnt++;
5122ee519a1fSgjelinek 	}
5123ee519a1fSgjelinek 
5124ee519a1fSgjelinek 	(void) zonecfg_endipdent(handle);
5125ee519a1fSgjelinek 
5126ee519a1fSgjelinek 	if (res != Z_OK) {
5127ee519a1fSgjelinek 		for (i = 0; i < ipd_cnt; i++)
5128ee519a1fSgjelinek 			free(ipds[i]);
5129ee519a1fSgjelinek 		free(ipds);
5130ee519a1fSgjelinek 		return (res);
5131ee519a1fSgjelinek 	}
5132ee519a1fSgjelinek 
5133ee519a1fSgjelinek 	/* We only have to process the contents file if we have ipds. */
5134ee519a1fSgjelinek 	if (ipd_cnt > 0) {
5135ee519a1fSgjelinek 		FILE	*fp;
5136ee519a1fSgjelinek 
5137ee519a1fSgjelinek 		if ((fp = fopen(CONTENTS_FILE, "r")) != NULL) {
5138ee519a1fSgjelinek 			char	*buf;
5139ee519a1fSgjelinek 
5140ee519a1fSgjelinek 			while ((buf = read_pkg_data(fp)) != NULL) {
5141ee519a1fSgjelinek 				res = get_path_pkgs(buf, ipds, ipd_cnt, &pkgs,
5142ee519a1fSgjelinek 				    &pkg_cnt);
5143ee519a1fSgjelinek 				free(buf);
5144ee519a1fSgjelinek 				if (res != Z_OK)
5145ee519a1fSgjelinek 					break;
5146ee519a1fSgjelinek 			}
5147ee519a1fSgjelinek 
5148ee519a1fSgjelinek 			(void) fclose(fp);
5149ee519a1fSgjelinek 		}
5150ee519a1fSgjelinek 	}
5151ee519a1fSgjelinek 
5152ee519a1fSgjelinek 	for (i = 0; i < ipd_cnt; i++)
5153ee519a1fSgjelinek 		free(ipds[i]);
5154ee519a1fSgjelinek 	free(ipds);
5155ee519a1fSgjelinek 
5156ee519a1fSgjelinek 	if (res != Z_OK) {
5157ee519a1fSgjelinek 		free_ipd_pkgs(pkgs, pkg_cnt);
5158ee519a1fSgjelinek 	} else {
5159ee519a1fSgjelinek 		*pkg_list = pkgs;
5160ee519a1fSgjelinek 		*cnt = pkg_cnt;
5161ee519a1fSgjelinek 	}
5162ee519a1fSgjelinek 
5163ee519a1fSgjelinek 	return (res);
5164ee519a1fSgjelinek }
5165ee519a1fSgjelinek 
5166ee519a1fSgjelinek /*
5167ee519a1fSgjelinek  * Return true if pkg_name is in the list of pkgs that deliver into an
5168ee519a1fSgjelinek  * inherited pkg directory for the zone.
5169ee519a1fSgjelinek  */
5170ee519a1fSgjelinek static boolean_t
5171ee519a1fSgjelinek dir_pkg(char *pkg_name, char **pkg_list, int cnt)
5172ee519a1fSgjelinek {
5173ee519a1fSgjelinek 	int i;
5174ee519a1fSgjelinek 
5175ee519a1fSgjelinek 	for (i = 0; i < cnt; i++) {
5176ee519a1fSgjelinek 		if (strcmp(pkg_name, pkg_list[i]) == 0)
5177ee519a1fSgjelinek 			return (B_TRUE);
5178ee519a1fSgjelinek 	}
5179ee519a1fSgjelinek 
5180ee519a1fSgjelinek 	return (B_FALSE);
5181ee519a1fSgjelinek }
5182ee519a1fSgjelinek 
5183ee519a1fSgjelinek /*
5184ee519a1fSgjelinek  * Start by adding the patch to the sw inventory on the handle.
5185ee519a1fSgjelinek  *
5186ee519a1fSgjelinek  * The info parameter will be the portion of the PATCH_INFO_ entry following
5187ee519a1fSgjelinek  * the '='.  For example:
5188ee519a1fSgjelinek  * Installed: Wed Dec  7 07:13:51 PST 2005 From: mum Obsoletes: 120777-03 \
5189ee519a1fSgjelinek  *	121087-02 119108-07 Requires: 119575-02 119255-06 Incompatibles:
5190ee519a1fSgjelinek  *
519107b574eeSgjelinek  * A backed out patch will have an info line of "backed out\n".  We should
519207b574eeSgjelinek  * skip these patches.
519307b574eeSgjelinek  *
5194ee519a1fSgjelinek  * We also want to add the Obsolete and Incompatible patches to the
5195ee519a1fSgjelinek  * sw inventory description of this patch.
5196ee519a1fSgjelinek  */
5197ee519a1fSgjelinek static int
5198ee519a1fSgjelinek add_patch(zone_dochandle_t handle, char *patch, char *info)
5199ee519a1fSgjelinek {
5200ee519a1fSgjelinek 	xmlNodePtr	node;
5201ee519a1fSgjelinek 	xmlNodePtr	cur;
5202ee519a1fSgjelinek 	int		err;
5203ee519a1fSgjelinek 	char		*p;
5204ee519a1fSgjelinek 	char		*lastp;
5205ee519a1fSgjelinek 	boolean_t	add_info = B_FALSE;
5206ee519a1fSgjelinek 	boolean_t	obsolete;
5207ee519a1fSgjelinek 
520807b574eeSgjelinek 	if (strcmp(info, "backed out\n") == 0)
520907b574eeSgjelinek 		return (Z_OK);
521007b574eeSgjelinek 
5211ee519a1fSgjelinek 	if ((err = operation_prep(handle)) != Z_OK)
5212ee519a1fSgjelinek 		return (err);
5213ee519a1fSgjelinek 
5214ee519a1fSgjelinek 	cur = handle->zone_dh_cur;
5215ee519a1fSgjelinek 	node = xmlNewTextChild(cur, NULL, DTD_ELEM_PATCH, NULL);
5216ee519a1fSgjelinek 	if ((err = newprop(node, DTD_ATTR_ID, patch)) != Z_OK)
5217ee519a1fSgjelinek 		return (err);
5218ee519a1fSgjelinek 
5219ee519a1fSgjelinek 	/*
5220ee519a1fSgjelinek 	 * Start with the first token.  This will probably be "Installed:".
5221ee519a1fSgjelinek 	 * If we can't tokenize this entry, just return.
5222ee519a1fSgjelinek 	 */
5223ee519a1fSgjelinek 	if ((p = strtok_r(info, " ", &lastp)) == NULL)
5224ee519a1fSgjelinek 		return (Z_OK);
5225ee519a1fSgjelinek 
5226ee519a1fSgjelinek 	do {
5227ee519a1fSgjelinek 		xmlNodePtr new_node;
5228ee519a1fSgjelinek 		char	*nlp;
5229ee519a1fSgjelinek 
5230ee519a1fSgjelinek 		if (strcmp(p, "Installed:") == 0 ||
5231ee519a1fSgjelinek 		    strcmp(p, "Requires:") == 0 ||
5232ee519a1fSgjelinek 		    strcmp(p, "From:") == 0) {
5233ee519a1fSgjelinek 			add_info = B_FALSE;
5234ee519a1fSgjelinek 			continue;
5235ee519a1fSgjelinek 		} else if (strcmp(p, "Obsoletes:") == 0) {
5236ee519a1fSgjelinek 			obsolete = B_TRUE;
5237ee519a1fSgjelinek 			add_info = B_TRUE;
5238ee519a1fSgjelinek 			continue;
5239ee519a1fSgjelinek 		} else if (strcmp(p, "Incompatibles:") == 0) {
5240ee519a1fSgjelinek 			obsolete = B_FALSE;
5241ee519a1fSgjelinek 			add_info = B_TRUE;
5242ee519a1fSgjelinek 			continue;
5243ee519a1fSgjelinek 		}
5244ee519a1fSgjelinek 
5245ee519a1fSgjelinek 		if (!add_info)
5246ee519a1fSgjelinek 			continue;
5247ee519a1fSgjelinek 
5248ee519a1fSgjelinek 		/* strip newline from last patch in the line */
5249ee519a1fSgjelinek 		nlp = (p + strlen(p) - 1);
5250ee519a1fSgjelinek 		if (*nlp == '\n')
5251ee519a1fSgjelinek 			*nlp = '\0';
5252ee519a1fSgjelinek 
5253ee519a1fSgjelinek 		if (obsolete)
5254ee519a1fSgjelinek 			new_node = xmlNewTextChild(node, NULL,
5255ee519a1fSgjelinek 			    DTD_ELEM_OBSOLETES, NULL);
5256ee519a1fSgjelinek 		else
5257ee519a1fSgjelinek 			new_node = xmlNewTextChild(node, NULL,
5258ee519a1fSgjelinek 			    DTD_ELEM_INCOMPATIBLE, NULL);
5259ee519a1fSgjelinek 
5260ee519a1fSgjelinek 		if ((err = newprop(new_node, DTD_ATTR_ID, p)) != Z_OK)
5261ee519a1fSgjelinek 			return (err);
5262ee519a1fSgjelinek 
5263ee519a1fSgjelinek 	} while ((p = strtok_r(NULL, " ", &lastp)) != NULL);
5264ee519a1fSgjelinek 
5265ee519a1fSgjelinek 	return (Z_OK);
5266ee519a1fSgjelinek }
5267ee519a1fSgjelinek 
5268ee519a1fSgjelinek static boolean_t
5269ee519a1fSgjelinek unique_patch(zone_dochandle_t handle, char *patch)
5270ee519a1fSgjelinek {
5271ee519a1fSgjelinek 	xmlNodePtr	cur;
5272ee519a1fSgjelinek 	char		id[MAXNAMELEN];
5273ee519a1fSgjelinek 
5274ee519a1fSgjelinek 	cur = xmlDocGetRootElement(handle->zone_dh_doc);
5275ee519a1fSgjelinek 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
5276ee519a1fSgjelinek 		if (xmlStrcmp(cur->name, DTD_ELEM_PATCH) == 0) {
5277ee519a1fSgjelinek 			if (fetchprop(cur, DTD_ATTR_ID, id, sizeof (id))
5278ee519a1fSgjelinek 			    != Z_OK)
5279ee519a1fSgjelinek 				continue;
5280ee519a1fSgjelinek 
5281ee519a1fSgjelinek 			if (strcmp(patch, id) == 0)
5282ee519a1fSgjelinek 				return (B_FALSE);
5283ee519a1fSgjelinek 		}
5284ee519a1fSgjelinek 	}
5285ee519a1fSgjelinek 
5286ee519a1fSgjelinek 	return (B_TRUE);
5287ee519a1fSgjelinek }
5288ee519a1fSgjelinek 
5289ee519a1fSgjelinek /*
5290ee519a1fSgjelinek  * Add the unique patches associated with this pkg to the sw inventory on the
5291ee519a1fSgjelinek  * handle.
5292ee519a1fSgjelinek  *
5293ee519a1fSgjelinek  * We are processing entries of the form:
5294ee519a1fSgjelinek  * PATCH_INFO_121454-02=Installed: Wed Dec  7 07:13:51 PST 2005 From: mum \
5295ee519a1fSgjelinek  *	Obsoletes: 120777-03 121087-02 119108-07 Requires: 119575-02 \
5296ee519a1fSgjelinek  *	119255-06 Incompatibles:
5297ee519a1fSgjelinek  *
5298ee519a1fSgjelinek  */
5299ee519a1fSgjelinek static int
5300ee519a1fSgjelinek add_patches(zone_dochandle_t handle, struct zone_pkginfo *infop)
5301ee519a1fSgjelinek {
5302ee519a1fSgjelinek 	int i;
5303ee519a1fSgjelinek 	int res = Z_OK;
5304ee519a1fSgjelinek 
5305ee519a1fSgjelinek 	for (i = 0; i < infop->zpi_patch_cnt; i++) {
5306ee519a1fSgjelinek 		char *p, *ep;
5307ee519a1fSgjelinek 
5308ee519a1fSgjelinek 		if (strlen(infop->zpi_patchinfo[i]) < (sizeof (PATCHINFO) - 1))
5309ee519a1fSgjelinek 			continue;
5310ee519a1fSgjelinek 
5311ee519a1fSgjelinek 		/* Skip over "PATCH_INFO_" to get the patch id. */
5312ee519a1fSgjelinek 		p = infop->zpi_patchinfo[i] + sizeof (PATCHINFO) - 1;
5313ee519a1fSgjelinek 		if ((ep = strchr(p, '=')) == NULL)
5314ee519a1fSgjelinek 			continue;
5315ee519a1fSgjelinek 
5316ee519a1fSgjelinek 		*ep = '\0';
5317ee519a1fSgjelinek 		if (unique_patch(handle, p))
531807b574eeSgjelinek 			if ((res = add_patch(handle, p, ep + 1)) != Z_OK)
531907b574eeSgjelinek 				break;
5320ee519a1fSgjelinek 	}
5321ee519a1fSgjelinek 
5322ee519a1fSgjelinek 	return (res);
5323ee519a1fSgjelinek }
5324ee519a1fSgjelinek 
5325ee519a1fSgjelinek /*
5326ee519a1fSgjelinek  * Add the pkg to the sw inventory on the handle.
5327ee519a1fSgjelinek  */
5328ee519a1fSgjelinek static int
5329ee519a1fSgjelinek add_pkg(zone_dochandle_t handle, char *name, char *version)
5330ee519a1fSgjelinek {
5331ee519a1fSgjelinek 	xmlNodePtr newnode;
5332ee519a1fSgjelinek 	xmlNodePtr cur;
5333ee519a1fSgjelinek 	int err;
5334ee519a1fSgjelinek 
5335ee519a1fSgjelinek 	if ((err = operation_prep(handle)) != Z_OK)
5336ee519a1fSgjelinek 		return (err);
5337ee519a1fSgjelinek 
5338ee519a1fSgjelinek 	cur = handle->zone_dh_cur;
5339ee519a1fSgjelinek 	newnode = xmlNewTextChild(cur, NULL, DTD_ELEM_PACKAGE, NULL);
5340ee519a1fSgjelinek 	if ((err = newprop(newnode, DTD_ATTR_NAME, name)) != Z_OK)
5341ee519a1fSgjelinek 		return (err);
5342ee519a1fSgjelinek 	if ((err = newprop(newnode, DTD_ATTR_VERSION, version)) != Z_OK)
5343ee519a1fSgjelinek 		return (err);
5344ee519a1fSgjelinek 	return (Z_OK);
5345ee519a1fSgjelinek }
5346ee519a1fSgjelinek 
5347ee519a1fSgjelinek static void
5348ee519a1fSgjelinek free_pkginfo(struct zone_pkginfo *infop)
5349ee519a1fSgjelinek {
5350ee519a1fSgjelinek 	free(infop->zpi_version);
5351ee519a1fSgjelinek 	if (infop->zpi_patch_cnt > 0) {
5352ee519a1fSgjelinek 		int i;
5353ee519a1fSgjelinek 
5354ee519a1fSgjelinek 		for (i = 0; i < infop->zpi_patch_cnt; i++)
5355ee519a1fSgjelinek 			free(infop->zpi_patchinfo[i]);
5356ee519a1fSgjelinek 		free(infop->zpi_patchinfo);
5357ee519a1fSgjelinek 	}
5358ee519a1fSgjelinek }
5359ee519a1fSgjelinek 
5360ee519a1fSgjelinek /*
5361ee519a1fSgjelinek  * Read the pkginfo file and populate the structure with the data we need
5362ee519a1fSgjelinek  * from this pkg for a sw inventory.
5363ee519a1fSgjelinek  */
5364ee519a1fSgjelinek static int
5365ee519a1fSgjelinek get_pkginfo(char *pkginfo, struct zone_pkginfo *infop)
5366ee519a1fSgjelinek {
5367ee519a1fSgjelinek 	FILE	*fp;
5368ee519a1fSgjelinek 	char	*buf;
5369ee519a1fSgjelinek 	int	err = 0;
5370ee519a1fSgjelinek 
5371ee519a1fSgjelinek 	infop->zpi_all_zones = B_FALSE;
5372ee519a1fSgjelinek 	infop->zpi_this_zone = B_FALSE;
5373ee519a1fSgjelinek 	infop->zpi_version = NULL;
5374ee519a1fSgjelinek 	infop->zpi_patch_cnt = 0;
5375ee519a1fSgjelinek 	infop->zpi_patchinfo = NULL;
5376ee519a1fSgjelinek 
5377ee519a1fSgjelinek 	if ((fp = fopen(pkginfo, "r")) == NULL)
5378ee519a1fSgjelinek 		return (errno);
5379ee519a1fSgjelinek 
5380ee519a1fSgjelinek 	while ((buf = read_pkg_data(fp)) != NULL) {
5381ee519a1fSgjelinek 		if (strncmp(buf, VERSION, sizeof (VERSION) - 1) == 0) {
5382ee519a1fSgjelinek 			int len;
5383ee519a1fSgjelinek 
5384ee519a1fSgjelinek 			if ((infop->zpi_version =
5385ee519a1fSgjelinek 			    strdup(buf + sizeof (VERSION) - 1)) == NULL) {
5386ee519a1fSgjelinek 				err = ENOMEM;
5387ee519a1fSgjelinek 				break;
5388ee519a1fSgjelinek 			}
5389ee519a1fSgjelinek 
5390ee519a1fSgjelinek 			/* remove trailing newline */
5391ee519a1fSgjelinek 			len = strlen(infop->zpi_version);
5392ee519a1fSgjelinek 			*(infop->zpi_version + len - 1) = 0;
5393ee519a1fSgjelinek 
539445916cd2Sjpk 		} else if (strcmp(buf, SUNW_PKG_ALL_ZONES) == 0) {
5395ee519a1fSgjelinek 			infop->zpi_all_zones = B_TRUE;
5396ee519a1fSgjelinek 
539745916cd2Sjpk 		} else if (strcmp(buf, SUNW_PKG_THIS_ZONE) == 0) {
5398ee519a1fSgjelinek 			infop->zpi_this_zone = B_TRUE;
5399ee519a1fSgjelinek 
5400ee519a1fSgjelinek 		} else if (strncmp(buf, PATCHINFO, sizeof (PATCHINFO) - 1)
5401ee519a1fSgjelinek 		    == 0) {
5402ee519a1fSgjelinek 			char **p;
5403ee519a1fSgjelinek 
5404ee519a1fSgjelinek 			if ((p = (char **)realloc(infop->zpi_patchinfo,
5405ee519a1fSgjelinek 			    sizeof (char *) * (infop->zpi_patch_cnt + 1)))
5406ee519a1fSgjelinek 			    == NULL) {
5407ee519a1fSgjelinek 				err = ENOMEM;
5408ee519a1fSgjelinek 				break;
5409ee519a1fSgjelinek 			}
5410ee519a1fSgjelinek 			infop->zpi_patchinfo = p;
5411ee519a1fSgjelinek 
5412ee519a1fSgjelinek 			if ((infop->zpi_patchinfo[infop->zpi_patch_cnt] =
5413ee519a1fSgjelinek 			    strdup(buf)) == NULL) {
5414ee519a1fSgjelinek 				err = ENOMEM;
5415ee519a1fSgjelinek 				break;
5416ee519a1fSgjelinek 			}
5417ee519a1fSgjelinek 			infop->zpi_patch_cnt++;
5418ee519a1fSgjelinek 		}
5419ee519a1fSgjelinek 
5420ee519a1fSgjelinek 		free(buf);
5421ee519a1fSgjelinek 	}
5422ee519a1fSgjelinek 
5423ee519a1fSgjelinek 	free(buf);
5424ee519a1fSgjelinek 
5425ee519a1fSgjelinek 	if (errno == ENOMEM) {
5426ee519a1fSgjelinek 		err = ENOMEM;
5427ee519a1fSgjelinek 		/* Clean up anything we did manage to allocate. */
5428ee519a1fSgjelinek 		free_pkginfo(infop);
5429ee519a1fSgjelinek 	}
5430ee519a1fSgjelinek 
5431ee519a1fSgjelinek 	(void) fclose(fp);
5432ee519a1fSgjelinek 
5433ee519a1fSgjelinek 	return (err);
5434ee519a1fSgjelinek }
5435ee519a1fSgjelinek 
5436ee519a1fSgjelinek /*
5437ee519a1fSgjelinek  * Take a software inventory of the global zone.  We need to get the set of
5438ee519a1fSgjelinek  * packages and patches that are on the global zone that the specified
5439ee519a1fSgjelinek  * non-global zone depends on.  The packages we need in the inventory are:
5440ee519a1fSgjelinek  *
5441ee519a1fSgjelinek  * - skip the package if SUNW_PKG_THISZONE is 'true'
5442ee519a1fSgjelinek  * otherwise,
5443ee519a1fSgjelinek  * - add the package if
5444ee519a1fSgjelinek  * a) SUNW_PKG_ALLZONES is 'true',
5445ee519a1fSgjelinek  * or
5446ee519a1fSgjelinek  * b) any file delivered by the package is in a file system that is inherited
5447ee519a1fSgjelinek  * from the global zone.
5448ee519a1fSgjelinek  * If the zone does not inherit any file systems (whole root)
5449ee519a1fSgjelinek  * then (b) will be skipped.
5450ee519a1fSgjelinek  *
5451ee519a1fSgjelinek  * For each of the packages that is being added to the inventory, we will also
5452ee519a1fSgjelinek  * add all of the associated, unique patches to the inventory.
5453ee519a1fSgjelinek  */
5454ee519a1fSgjelinek static int
5455ee519a1fSgjelinek zonecfg_sw_inventory(zone_dochandle_t handle)
5456ee519a1fSgjelinek {
5457ee519a1fSgjelinek 	char		pkginfo[MAXPATHLEN];
5458ee519a1fSgjelinek 	int		res;
5459ee519a1fSgjelinek 	struct dirent	*dp;
5460ee519a1fSgjelinek 	DIR		*dirp;
5461ee519a1fSgjelinek 	struct stat	buf;
5462ee519a1fSgjelinek 	struct zone_pkginfo	info;
5463ee519a1fSgjelinek 	int		pkg_cnt = 0;
5464ee519a1fSgjelinek 	char		**pkgs = NULL;
5465ee519a1fSgjelinek 
5466ee519a1fSgjelinek 	if ((res = get_ipd_pkgs(handle, &pkgs, &pkg_cnt)) != Z_OK)
5467ee519a1fSgjelinek 		return (res);
5468ee519a1fSgjelinek 
5469ee519a1fSgjelinek 	if ((dirp = opendir(PKG_PATH)) == NULL) {
5470ee519a1fSgjelinek 		free_ipd_pkgs(pkgs, pkg_cnt);
5471ee519a1fSgjelinek 		return (Z_OK);
5472ee519a1fSgjelinek 	}
5473ee519a1fSgjelinek 
5474ee519a1fSgjelinek 	while ((dp = readdir(dirp)) != (struct dirent *)0) {
5475ee519a1fSgjelinek 		if (strcmp(dp->d_name, ".") == 0 ||
5476ee519a1fSgjelinek 		    strcmp(dp->d_name, "..") == 0)
5477ee519a1fSgjelinek 			continue;
5478ee519a1fSgjelinek 
5479ee519a1fSgjelinek 		(void) snprintf(pkginfo, sizeof (pkginfo), "%s/%s/pkginfo",
5480ee519a1fSgjelinek 		    PKG_PATH, dp->d_name);
5481ee519a1fSgjelinek 
5482ee519a1fSgjelinek 		if (stat(pkginfo, &buf) == -1 || !S_ISREG(buf.st_mode))
5483ee519a1fSgjelinek 			continue;
5484ee519a1fSgjelinek 
5485ee519a1fSgjelinek 		if (get_pkginfo(pkginfo, &info) != 0) {
5486ee519a1fSgjelinek 			res = Z_NOMEM;
5487ee519a1fSgjelinek 			break;
5488ee519a1fSgjelinek 		}
5489ee519a1fSgjelinek 
5490ee519a1fSgjelinek 		if (!info.zpi_this_zone &&
5491ee519a1fSgjelinek 		    (info.zpi_all_zones ||
5492ee519a1fSgjelinek 		    dir_pkg(dp->d_name, pkgs, pkg_cnt))) {
5493ee519a1fSgjelinek 			if ((res = add_pkg(handle, dp->d_name,
5494ee519a1fSgjelinek 			    info.zpi_version)) == Z_OK) {
5495ee519a1fSgjelinek 				if (info.zpi_patch_cnt > 0)
5496ee519a1fSgjelinek 					res = add_patches(handle, &info);
5497ee519a1fSgjelinek 			}
5498ee519a1fSgjelinek 		}
5499ee519a1fSgjelinek 
5500ee519a1fSgjelinek 		free_pkginfo(&info);
5501ee519a1fSgjelinek 
5502ee519a1fSgjelinek 		if (res != Z_OK)
5503ee519a1fSgjelinek 			break;
5504ee519a1fSgjelinek 	}
5505ee519a1fSgjelinek 
5506ee519a1fSgjelinek 	(void) closedir(dirp);
5507ee519a1fSgjelinek 
5508ee519a1fSgjelinek 	free_ipd_pkgs(pkgs, pkg_cnt);
5509ee519a1fSgjelinek 
5510ee519a1fSgjelinek 	if (res == Z_OK)
5511ee519a1fSgjelinek 		handle->zone_dh_sw_inv = B_TRUE;
5512ee519a1fSgjelinek 
5513ee519a1fSgjelinek 	return (res);
5514ee519a1fSgjelinek }
5515ee519a1fSgjelinek 
5516ee519a1fSgjelinek /*
5517ee519a1fSgjelinek  * zonecfg_devwalk call-back function used during detach to generate the
5518ee519a1fSgjelinek  * dev info in the manifest.
5519ee519a1fSgjelinek  */
5520ee519a1fSgjelinek static int
5521ee519a1fSgjelinek get_detach_dev_entry(const char *name, uid_t uid, gid_t gid, mode_t mode,
5522ee519a1fSgjelinek     const char *acl, void *hdl)
5523ee519a1fSgjelinek {
5524ee519a1fSgjelinek 	zone_dochandle_t handle = (zone_dochandle_t)hdl;
5525ee519a1fSgjelinek 	xmlNodePtr newnode;
5526ee519a1fSgjelinek 	xmlNodePtr cur;
5527ee519a1fSgjelinek 	int err;
5528ee519a1fSgjelinek 	char buf[128];
5529ee519a1fSgjelinek 
5530ee519a1fSgjelinek 	if ((err = operation_prep(handle)) != Z_OK)
5531ee519a1fSgjelinek 		return (err);
5532ee519a1fSgjelinek 
5533ee519a1fSgjelinek 	cur = handle->zone_dh_cur;
5534ee519a1fSgjelinek 	newnode = xmlNewTextChild(cur, NULL, DTD_ELEM_DEV_PERM, NULL);
5535ee519a1fSgjelinek 	if ((err = newprop(newnode, DTD_ATTR_NAME, (char *)name)) != Z_OK)
5536ee519a1fSgjelinek 		return (err);
5537ee519a1fSgjelinek 	(void) snprintf(buf, sizeof (buf), "%lu", uid);
5538ee519a1fSgjelinek 	if ((err = newprop(newnode, DTD_ATTR_UID, buf)) != Z_OK)
5539ee519a1fSgjelinek 		return (err);
5540ee519a1fSgjelinek 	(void) snprintf(buf, sizeof (buf), "%lu", gid);
5541ee519a1fSgjelinek 	if ((err = newprop(newnode, DTD_ATTR_GID, buf)) != Z_OK)
5542ee519a1fSgjelinek 		return (err);
5543ee519a1fSgjelinek 	(void) snprintf(buf, sizeof (buf), "%o", mode);
5544ee519a1fSgjelinek 	if ((err = newprop(newnode, DTD_ATTR_MODE, buf)) != Z_OK)
5545ee519a1fSgjelinek 		return (err);
5546ee519a1fSgjelinek 	if ((err = newprop(newnode, DTD_ATTR_ACL, (char *)acl)) != Z_OK)
5547ee519a1fSgjelinek 		return (err);
5548ee519a1fSgjelinek 	return (Z_OK);
5549ee519a1fSgjelinek }
5550ee519a1fSgjelinek 
5551ee519a1fSgjelinek /*
5552ee519a1fSgjelinek  * Get the information required to support detaching a zone.  This is
5553ee519a1fSgjelinek  * called on the source system when detaching (the detaching parameter should
5554ee519a1fSgjelinek  * be set to true) and on the destination system before attaching (the
5555ee519a1fSgjelinek  * detaching parameter should be false).
5556ee519a1fSgjelinek  *
5557ee519a1fSgjelinek  * For native Solaris zones, the detach/attach process involves validating
5558ee519a1fSgjelinek  * that the software on the global zone can support the zone when we attach.
5559ee519a1fSgjelinek  * To do this we take a software inventory of the global zone.  We also
5560ee519a1fSgjelinek  * have to keep track of the device configuration so that we can properly
5561ee519a1fSgjelinek  * recreate it on the destination.
5562ee519a1fSgjelinek  */
5563ee519a1fSgjelinek int
5564ee519a1fSgjelinek zonecfg_get_detach_info(zone_dochandle_t handle, boolean_t detaching)
5565ee519a1fSgjelinek {
5566ee519a1fSgjelinek 	int		res;
5567ee519a1fSgjelinek 
5568ee519a1fSgjelinek 	if ((res = zonecfg_sw_inventory(handle)) != Z_OK)
5569ee519a1fSgjelinek 		return (res);
5570ee519a1fSgjelinek 
5571ee519a1fSgjelinek 	if (detaching)
5572ee519a1fSgjelinek 		res = zonecfg_devwalk(handle, get_detach_dev_entry, handle);
5573ee519a1fSgjelinek 
5574ee519a1fSgjelinek 	return (res);
5575ee519a1fSgjelinek }
5576