xref: /titanic_53/usr/src/lib/libzonecfg/common/libzonecfg.c (revision ad4023c40b055806dce2bde9ee9e87e5016b5135)
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"
937c478bd9Sstevel@tonic-gate #define	DTD_ATTR_MATCH		(const xmlChar *) "match"
947c478bd9Sstevel@tonic-gate #define	DTD_ATTR_NAME		(const xmlChar *) "name"
957c478bd9Sstevel@tonic-gate #define	DTD_ATTR_PHYSICAL	(const xmlChar *) "physical"
967c478bd9Sstevel@tonic-gate #define	DTD_ATTR_POOL		(const xmlChar *) "pool"
977c478bd9Sstevel@tonic-gate #define	DTD_ATTR_PRIV		(const xmlChar *) "priv"
987c478bd9Sstevel@tonic-gate #define	DTD_ATTR_RAW		(const xmlChar *) "raw"
997c478bd9Sstevel@tonic-gate #define	DTD_ATTR_SPECIAL	(const xmlChar *) "special"
1007c478bd9Sstevel@tonic-gate #define	DTD_ATTR_TYPE		(const xmlChar *) "type"
1017c478bd9Sstevel@tonic-gate #define	DTD_ATTR_VALUE		(const xmlChar *) "value"
1027c478bd9Sstevel@tonic-gate #define	DTD_ATTR_ZONEPATH	(const xmlChar *) "zonepath"
103ee519a1fSgjelinek #define	DTD_ATTR_VERSION	(const xmlChar *) "version"
104ee519a1fSgjelinek #define	DTD_ATTR_ID		(const xmlChar *) "id"
105ee519a1fSgjelinek #define	DTD_ATTR_UID		(const xmlChar *) "uid"
106ee519a1fSgjelinek #define	DTD_ATTR_GID		(const xmlChar *) "gid"
107ee519a1fSgjelinek #define	DTD_ATTR_MODE		(const xmlChar *) "mode"
108ee519a1fSgjelinek #define	DTD_ATTR_ACL		(const xmlChar *) "acl"
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate #define	DTD_ENTITY_BOOLEAN	"boolean"
1117c478bd9Sstevel@tonic-gate #define	DTD_ENTITY_DEVPATH	"devpath"
1127c478bd9Sstevel@tonic-gate #define	DTD_ENTITY_DRIVER	"driver"
1137c478bd9Sstevel@tonic-gate #define	DTD_ENTITY_DRVMIN	"drv_min"
1147c478bd9Sstevel@tonic-gate #define	DTD_ENTITY_FALSE	"false"
1157c478bd9Sstevel@tonic-gate #define	DTD_ENTITY_INT		"int"
1167c478bd9Sstevel@tonic-gate #define	DTD_ENTITY_STRING	"string"
1177c478bd9Sstevel@tonic-gate #define	DTD_ENTITY_TRUE		"true"
1187c478bd9Sstevel@tonic-gate #define	DTD_ENTITY_UINT		"uint"
1197c478bd9Sstevel@tonic-gate 
120a1be23daSdp #define	DTD_ENTITY_BOOL_LEN	6	/* "false" */
121a1be23daSdp 
122ee519a1fSgjelinek #define	DETACHED	"SUNWdetached.xml"
123ee519a1fSgjelinek #define	ATTACH_FORCED	"SUNWattached.xml"
124ee519a1fSgjelinek #define	PKG_PATH	"/var/sadm/pkg"
125ee519a1fSgjelinek #define	CONTENTS_FILE	"/var/sadm/install/contents"
12645916cd2Sjpk #define	SUNW_PKG_ALL_ZONES	"SUNW_PKG_ALLZONES=true\n"
12745916cd2Sjpk #define	SUNW_PKG_THIS_ZONE	"SUNW_PKG_THISZONE=true\n"
128ee519a1fSgjelinek #define	VERSION		"VERSION="
129ee519a1fSgjelinek #define	PATCHLIST	"PATCHLIST="
130ee519a1fSgjelinek #define	PATCHINFO	"PATCH_INFO_"
131ee519a1fSgjelinek #define	PKGINFO_RD_LEN	128
132ee519a1fSgjelinek 
1337c478bd9Sstevel@tonic-gate struct zone_dochandle {
1347c478bd9Sstevel@tonic-gate 	char		*zone_dh_rootdir;
1357c478bd9Sstevel@tonic-gate 	xmlDocPtr	zone_dh_doc;
1367c478bd9Sstevel@tonic-gate 	xmlNodePtr	zone_dh_cur;
1377c478bd9Sstevel@tonic-gate 	xmlNodePtr	zone_dh_top;
138087719fdSdp 	boolean_t	zone_dh_newzone;
139087719fdSdp 	boolean_t	zone_dh_snapshot;
140ee519a1fSgjelinek 	boolean_t	zone_dh_sw_inv;
141087719fdSdp 	char		zone_dh_delete_name[ZONENAME_MAX];
1427c478bd9Sstevel@tonic-gate };
1437c478bd9Sstevel@tonic-gate 
144cf8f45c7Sdstaff struct znotify {
145cf8f45c7Sdstaff 	void * zn_private;
146cf8f45c7Sdstaff 	evchan_t *zn_eventchan;
147cf8f45c7Sdstaff 	int (*zn_callback)(const  char *zonename, zoneid_t zid,
148cf8f45c7Sdstaff 	    const char *newstate, const char *oldstate, hrtime_t when, void *p);
149cf8f45c7Sdstaff 	pthread_mutex_t zn_mutex;
150cf8f45c7Sdstaff 	pthread_cond_t zn_cond;
151cf8f45c7Sdstaff 	pthread_mutex_t zn_bigmutex;
152cf8f45c7Sdstaff 	volatile enum {ZN_UNLOCKED, ZN_LOCKED, ZN_PING_INFLIGHT,
153cf8f45c7Sdstaff 	    ZN_PING_RECEIVED} zn_state;
154cf8f45c7Sdstaff 	char zn_subscriber_id[MAX_SUBID_LEN];
155cf8f45c7Sdstaff 	volatile boolean_t zn_failed;
156cf8f45c7Sdstaff 	int zn_failure_count;
157cf8f45c7Sdstaff };
158cf8f45c7Sdstaff 
159ee519a1fSgjelinek struct zone_pkginfo {
160ee519a1fSgjelinek 	boolean_t	zpi_all_zones;
161ee519a1fSgjelinek 	boolean_t	zpi_this_zone;
162ee519a1fSgjelinek 	int		zpi_patch_cnt;
163ee519a1fSgjelinek 	char		*zpi_version;
164ee519a1fSgjelinek 	char		**zpi_patchinfo;
165ee519a1fSgjelinek };
166ee519a1fSgjelinek 
167108322fbScarlsonj char *zonecfg_root = "";
168108322fbScarlsonj 
1697c478bd9Sstevel@tonic-gate /*
1707c478bd9Sstevel@tonic-gate  * For functions which return int, which is most of the functions herein,
1717c478bd9Sstevel@tonic-gate  * the return values should be from the Z_foo set defined in <libzonecfg.h>.
1727c478bd9Sstevel@tonic-gate  * In some instances, we take pains mapping some libc errno values to Z_foo
1737c478bd9Sstevel@tonic-gate  * values from this set.
1747c478bd9Sstevel@tonic-gate  */
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate /*
177108322fbScarlsonj  * Set the root (/) path for all zonecfg configuration files.  This is a
178108322fbScarlsonj  * private interface used by Live Upgrade extensions to access zone
179108322fbScarlsonj  * configuration inside mounted alternate boot environments.
180108322fbScarlsonj  */
181108322fbScarlsonj void
182108322fbScarlsonj zonecfg_set_root(const char *rootpath)
183108322fbScarlsonj {
184108322fbScarlsonj 	if (*zonecfg_root != '\0')
185108322fbScarlsonj 		free(zonecfg_root);
186108322fbScarlsonj 	if (rootpath == NULL || rootpath[0] == '\0' || rootpath[1] == '\0' ||
187108322fbScarlsonj 	    (zonecfg_root = strdup(rootpath)) == NULL)
188108322fbScarlsonj 		zonecfg_root = "";
189108322fbScarlsonj }
190108322fbScarlsonj 
191108322fbScarlsonj const char *
192108322fbScarlsonj zonecfg_get_root(void)
193108322fbScarlsonj {
194108322fbScarlsonj 	return (zonecfg_root);
195108322fbScarlsonj }
196108322fbScarlsonj 
197108322fbScarlsonj boolean_t
198108322fbScarlsonj zonecfg_in_alt_root(void)
199108322fbScarlsonj {
200108322fbScarlsonj 	return (*zonecfg_root != '\0');
201108322fbScarlsonj }
202108322fbScarlsonj 
203108322fbScarlsonj /*
2047c478bd9Sstevel@tonic-gate  * Callers of the _file_path() functions are expected to have the second
2057c478bd9Sstevel@tonic-gate  * parameter be a (char foo[MAXPATHLEN]).
2067c478bd9Sstevel@tonic-gate  */
2077c478bd9Sstevel@tonic-gate 
208108322fbScarlsonj static boolean_t
2097c478bd9Sstevel@tonic-gate config_file_path(const char *zonename, char *answer)
2107c478bd9Sstevel@tonic-gate {
211108322fbScarlsonj 	return (snprintf(answer, MAXPATHLEN, "%s%s/%s.xml", zonecfg_root,
212108322fbScarlsonj 	    ZONE_CONFIG_ROOT, zonename) < MAXPATHLEN);
2137c478bd9Sstevel@tonic-gate }
2147c478bd9Sstevel@tonic-gate 
215108322fbScarlsonj static boolean_t
216108322fbScarlsonj snap_file_path(const char *zonename, char *answer)
2177c478bd9Sstevel@tonic-gate {
218108322fbScarlsonj 	return (snprintf(answer, MAXPATHLEN, "%s%s/%s.snapshot.xml",
219108322fbScarlsonj 	    zonecfg_root, ZONE_SNAPSHOT_ROOT, zonename) < MAXPATHLEN);
2207c478bd9Sstevel@tonic-gate }
2217c478bd9Sstevel@tonic-gate 
2227c478bd9Sstevel@tonic-gate /*ARGSUSED*/
2237c478bd9Sstevel@tonic-gate static void
2247c478bd9Sstevel@tonic-gate zonecfg_error_func(void *ctx, const char *msg, ...)
2257c478bd9Sstevel@tonic-gate {
2267c478bd9Sstevel@tonic-gate 	/*
2277c478bd9Sstevel@tonic-gate 	 * This function does nothing by design.  Its purpose is to prevent
2287c478bd9Sstevel@tonic-gate 	 * libxml from dumping unwanted messages to stdout/stderr.
2297c478bd9Sstevel@tonic-gate 	 */
2307c478bd9Sstevel@tonic-gate }
2317c478bd9Sstevel@tonic-gate 
2327c478bd9Sstevel@tonic-gate zone_dochandle_t
2337c478bd9Sstevel@tonic-gate zonecfg_init_handle(void)
2347c478bd9Sstevel@tonic-gate {
235087719fdSdp 	zone_dochandle_t handle = calloc(1, sizeof (struct zone_dochandle));
2367c478bd9Sstevel@tonic-gate 	if (handle == NULL) {
2377c478bd9Sstevel@tonic-gate 		errno = Z_NOMEM;
2387c478bd9Sstevel@tonic-gate 		return (NULL);
2397c478bd9Sstevel@tonic-gate 	}
2407c478bd9Sstevel@tonic-gate 
2417c478bd9Sstevel@tonic-gate 	/* generic libxml initialization */
2427c478bd9Sstevel@tonic-gate 	xmlLineNumbersDefault(1);
2437c478bd9Sstevel@tonic-gate 	xmlLoadExtDtdDefaultValue |= XML_DETECT_IDS;
2447c478bd9Sstevel@tonic-gate 	xmlDoValidityCheckingDefaultValue = 1;
2457c478bd9Sstevel@tonic-gate 	(void) xmlKeepBlanksDefault(0);
2467c478bd9Sstevel@tonic-gate 	xmlGetWarningsDefaultValue = 0;
2477c478bd9Sstevel@tonic-gate 	xmlSetGenericErrorFunc(NULL, zonecfg_error_func);
2487c478bd9Sstevel@tonic-gate 
2497c478bd9Sstevel@tonic-gate 	return (handle);
2507c478bd9Sstevel@tonic-gate }
2517c478bd9Sstevel@tonic-gate 
2527c478bd9Sstevel@tonic-gate int
2537c478bd9Sstevel@tonic-gate zonecfg_check_handle(zone_dochandle_t handle)
2547c478bd9Sstevel@tonic-gate {
2557c478bd9Sstevel@tonic-gate 	if (handle == NULL || handle->zone_dh_doc == NULL)
2567c478bd9Sstevel@tonic-gate 		return (Z_BAD_HANDLE);
2577c478bd9Sstevel@tonic-gate 	return (Z_OK);
2587c478bd9Sstevel@tonic-gate }
2597c478bd9Sstevel@tonic-gate 
2607c478bd9Sstevel@tonic-gate void
2617c478bd9Sstevel@tonic-gate zonecfg_fini_handle(zone_dochandle_t handle)
2627c478bd9Sstevel@tonic-gate {
2637c478bd9Sstevel@tonic-gate 	if (zonecfg_check_handle(handle) == Z_OK)
2647c478bd9Sstevel@tonic-gate 		xmlFreeDoc(handle->zone_dh_doc);
2657c478bd9Sstevel@tonic-gate 	if (handle != NULL)
2667c478bd9Sstevel@tonic-gate 		free(handle);
2677c478bd9Sstevel@tonic-gate }
2687c478bd9Sstevel@tonic-gate 
2697c478bd9Sstevel@tonic-gate static int
2707c478bd9Sstevel@tonic-gate zonecfg_destroy_impl(char *filename)
2717c478bd9Sstevel@tonic-gate {
2727c478bd9Sstevel@tonic-gate 	if (unlink(filename) == -1) {
2737c478bd9Sstevel@tonic-gate 		if (errno == EACCES)
2747c478bd9Sstevel@tonic-gate 			return (Z_ACCES);
2757c478bd9Sstevel@tonic-gate 		if (errno == ENOENT)
2767c478bd9Sstevel@tonic-gate 			return (Z_NO_ZONE);
2777c478bd9Sstevel@tonic-gate 		return (Z_MISC_FS);
2787c478bd9Sstevel@tonic-gate 	}
2797c478bd9Sstevel@tonic-gate 	return (Z_OK);
2807c478bd9Sstevel@tonic-gate }
2817c478bd9Sstevel@tonic-gate 
2827c478bd9Sstevel@tonic-gate int
283087719fdSdp zonecfg_destroy(const char *zonename, boolean_t force)
2847c478bd9Sstevel@tonic-gate {
2857c478bd9Sstevel@tonic-gate 	char path[MAXPATHLEN];
286087719fdSdp 	struct zoneent ze;
287087719fdSdp 	int err, state_err;
288087719fdSdp 	zone_state_t state;
2897c478bd9Sstevel@tonic-gate 
290108322fbScarlsonj 	if (!config_file_path(zonename, path))
291108322fbScarlsonj 		return (Z_MISC_FS);
292087719fdSdp 
293087719fdSdp 	state_err = zone_get_state((char *)zonename, &state);
294087719fdSdp 	err = access(path, W_OK);
295087719fdSdp 
296087719fdSdp 	/*
297087719fdSdp 	 * If there is no file, and no index entry, reliably indicate that no
298087719fdSdp 	 * such zone exists.
299087719fdSdp 	 */
300087719fdSdp 	if ((state_err == Z_NO_ZONE) && (err == -1) && (errno == ENOENT))
301087719fdSdp 		return (Z_NO_ZONE);
302087719fdSdp 
303087719fdSdp 	/*
304087719fdSdp 	 * Handle any other filesystem related errors (except if the XML
305087719fdSdp 	 * file is missing, which we treat silently), unless we're forcing,
306087719fdSdp 	 * in which case we plow on.
307087719fdSdp 	 */
308087719fdSdp 	if (err == -1 && errno != ENOENT) {
309087719fdSdp 		if (errno == EACCES)
310087719fdSdp 			return (Z_ACCES);
311087719fdSdp 		else if (!force)
312087719fdSdp 			return (Z_MISC_FS);
313087719fdSdp 	}
314087719fdSdp 
315087719fdSdp 	if (state > ZONE_STATE_INSTALLED)
316087719fdSdp 		return (Z_BAD_ZONE_STATE);
317087719fdSdp 
318087719fdSdp 	if (!force && state > ZONE_STATE_CONFIGURED)
319087719fdSdp 		return (Z_BAD_ZONE_STATE);
320087719fdSdp 
321087719fdSdp 	/*
322087719fdSdp 	 * Index deletion succeeds even if the entry doesn't exist.  So this
323087719fdSdp 	 * will fail only if we've had some more severe problem.
324087719fdSdp 	 */
325087719fdSdp 	bzero(&ze, sizeof (ze));
326087719fdSdp 	(void) strlcpy(ze.zone_name, zonename, sizeof (ze.zone_name));
327087719fdSdp 	if ((err = putzoneent(&ze, PZE_REMOVE)) != Z_OK)
328087719fdSdp 		if (!force)
329087719fdSdp 			return (err);
330087719fdSdp 
331087719fdSdp 	err = zonecfg_destroy_impl(path);
332087719fdSdp 
333087719fdSdp 	/*
334087719fdSdp 	 * Treat failure to find the XML file silently, since, well, it's
335087719fdSdp 	 * gone, and with the index file cleaned up, we're done.
336087719fdSdp 	 */
337087719fdSdp 	if (err == Z_OK || err == Z_NO_ZONE)
338087719fdSdp 		return (Z_OK);
339087719fdSdp 	return (err);
3407c478bd9Sstevel@tonic-gate }
3417c478bd9Sstevel@tonic-gate 
3427c478bd9Sstevel@tonic-gate int
343108322fbScarlsonj zonecfg_destroy_snapshot(const char *zonename)
3447c478bd9Sstevel@tonic-gate {
3457c478bd9Sstevel@tonic-gate 	char path[MAXPATHLEN];
3467c478bd9Sstevel@tonic-gate 
347108322fbScarlsonj 	if (!snap_file_path(zonename, path))
348108322fbScarlsonj 		return (Z_MISC_FS);
3497c478bd9Sstevel@tonic-gate 	return (zonecfg_destroy_impl(path));
3507c478bd9Sstevel@tonic-gate }
3517c478bd9Sstevel@tonic-gate 
3527c478bd9Sstevel@tonic-gate static int
353a1be23daSdp getroot(zone_dochandle_t handle, xmlNodePtr *root)
3547c478bd9Sstevel@tonic-gate {
3557c478bd9Sstevel@tonic-gate 	if (zonecfg_check_handle(handle) == Z_BAD_HANDLE)
3567c478bd9Sstevel@tonic-gate 		return (Z_BAD_HANDLE);
3577c478bd9Sstevel@tonic-gate 
358a1be23daSdp 	*root = xmlDocGetRootElement(handle->zone_dh_doc);
359a1be23daSdp 
360a1be23daSdp 	if (*root == NULL)
3617c478bd9Sstevel@tonic-gate 		return (Z_EMPTY_DOCUMENT);
362a1be23daSdp 
363a1be23daSdp 	if (xmlStrcmp((*root)->name, DTD_ELEM_ZONE))
364a1be23daSdp 		return (Z_WRONG_DOC_TYPE);
365a1be23daSdp 
366a1be23daSdp 	return (Z_OK);
3677c478bd9Sstevel@tonic-gate }
3687c478bd9Sstevel@tonic-gate 
369a1be23daSdp static int
370a1be23daSdp operation_prep(zone_dochandle_t handle)
371a1be23daSdp {
372a1be23daSdp 	xmlNodePtr root;
373a1be23daSdp 	int err;
374a1be23daSdp 
375a1be23daSdp 	if ((err = getroot(handle, &root)) != 0)
376a1be23daSdp 		return (err);
377a1be23daSdp 
378a1be23daSdp 	handle->zone_dh_cur = root;
379a1be23daSdp 	handle->zone_dh_top = root;
380a1be23daSdp 	return (Z_OK);
3817c478bd9Sstevel@tonic-gate }
382a1be23daSdp 
383a1be23daSdp static int
384ffbafc53Scomay fetchprop(xmlNodePtr cur, const xmlChar *propname, char *dst, size_t dstsize)
385ffbafc53Scomay {
386ffbafc53Scomay 	xmlChar *property;
387ffbafc53Scomay 	size_t srcsize;
388ffbafc53Scomay 
389ffbafc53Scomay 	if ((property = xmlGetProp(cur, propname)) == NULL)
390ffbafc53Scomay 		return (Z_BAD_PROPERTY);
391ffbafc53Scomay 	srcsize = strlcpy(dst, (char *)property, dstsize);
392ffbafc53Scomay 	xmlFree(property);
393ffbafc53Scomay 	if (srcsize >= dstsize)
394ffbafc53Scomay 		return (Z_TOO_BIG);
395ffbafc53Scomay 	return (Z_OK);
396ffbafc53Scomay }
397ffbafc53Scomay 
398ffbafc53Scomay static int
399ffbafc53Scomay fetch_alloc_prop(xmlNodePtr cur, const xmlChar *propname, char **dst)
400ffbafc53Scomay {
401ffbafc53Scomay 	xmlChar *property;
402ffbafc53Scomay 
403ffbafc53Scomay 	if ((property = xmlGetProp(cur, propname)) == NULL)
404ffbafc53Scomay 		return (Z_BAD_PROPERTY);
405ffbafc53Scomay 	if ((*dst = strdup((char *)property)) == NULL) {
406ffbafc53Scomay 		xmlFree(property);
407ffbafc53Scomay 		return (Z_NOMEM);
408ffbafc53Scomay 	}
409ffbafc53Scomay 	xmlFree(property);
410ffbafc53Scomay 	return (Z_OK);
411ffbafc53Scomay }
412ffbafc53Scomay 
413ffbafc53Scomay static int
414a1be23daSdp getrootattr(zone_dochandle_t handle, const xmlChar *propname,
415a1be23daSdp     char *propval, size_t propsize)
416a1be23daSdp {
417a1be23daSdp 	xmlNodePtr root;
418a1be23daSdp 	int err;
419a1be23daSdp 
420a1be23daSdp 	if ((err = getroot(handle, &root)) != 0)
421a1be23daSdp 		return (err);
422a1be23daSdp 
423ffbafc53Scomay 	return (fetchprop(root, propname, propval, propsize));
424ffbafc53Scomay }
425ffbafc53Scomay 
426ffbafc53Scomay static int
427ffbafc53Scomay get_alloc_rootattr(zone_dochandle_t handle, const xmlChar *propname,
428ffbafc53Scomay     char **propval)
429ffbafc53Scomay {
430ffbafc53Scomay 	xmlNodePtr root;
431ffbafc53Scomay 	int err;
432ffbafc53Scomay 
433ffbafc53Scomay 	if ((err = getroot(handle, &root)) != 0)
434ffbafc53Scomay 		return (err);
435ffbafc53Scomay 
436ffbafc53Scomay 	return (fetch_alloc_prop(root, propname, propval));
437a1be23daSdp }
438a1be23daSdp 
439a1be23daSdp static int
440108322fbScarlsonj setrootattr(zone_dochandle_t handle, const xmlChar *propname,
441108322fbScarlsonj     const char *propval)
442a1be23daSdp {
443a1be23daSdp 	int err;
444a1be23daSdp 	xmlNodePtr root;
445a1be23daSdp 
446a1be23daSdp 	if (propval == NULL)
447a1be23daSdp 		return (Z_INVAL);
448a1be23daSdp 
449a1be23daSdp 	if ((err = getroot(handle, &root)) != Z_OK)
450a1be23daSdp 		return (err);
451a1be23daSdp 
452a1be23daSdp 	if (xmlSetProp(root, propname, (const xmlChar *) propval) == NULL)
453a1be23daSdp 		return (Z_INVAL);
4547c478bd9Sstevel@tonic-gate 	return (Z_OK);
4557c478bd9Sstevel@tonic-gate }
4567c478bd9Sstevel@tonic-gate 
457087719fdSdp static void
458087719fdSdp addcomment(zone_dochandle_t handle, const char *comment)
459087719fdSdp {
460087719fdSdp 	xmlNodePtr node;
461087719fdSdp 	node = xmlNewComment((xmlChar *) comment);
462087719fdSdp 
463087719fdSdp 	if (node != NULL)
464087719fdSdp 		(void) xmlAddPrevSibling(handle->zone_dh_top, node);
465087719fdSdp }
466087719fdSdp 
467087719fdSdp static void
468087719fdSdp stripcomments(zone_dochandle_t handle)
469087719fdSdp {
470087719fdSdp 	xmlDocPtr top;
471087719fdSdp 	xmlNodePtr child, next;
472087719fdSdp 
473087719fdSdp 	top = handle->zone_dh_doc;
474087719fdSdp 	for (child = top->xmlChildrenNode; child != NULL; child = next) {
475087719fdSdp 		next = child->next;
476087719fdSdp 		if (child->name == NULL)
477087719fdSdp 			continue;
478087719fdSdp 		if (xmlStrcmp(child->name, DTD_ELEM_COMMENT) == 0) {
479087719fdSdp 			next = child->next;
480087719fdSdp 			xmlUnlinkNode(child);
481087719fdSdp 			xmlFreeNode(child);
482087719fdSdp 		}
483087719fdSdp 	}
484087719fdSdp }
485087719fdSdp 
486ee519a1fSgjelinek static void
487ee519a1fSgjelinek strip_sw_inv(zone_dochandle_t handle)
488ee519a1fSgjelinek {
489ee519a1fSgjelinek 	xmlNodePtr root, child, next;
490ee519a1fSgjelinek 
491ee519a1fSgjelinek 	root = xmlDocGetRootElement(handle->zone_dh_doc);
492ee519a1fSgjelinek 	for (child = root->xmlChildrenNode; child != NULL; child = next) {
493ee519a1fSgjelinek 		next = child->next;
494ee519a1fSgjelinek 		if (child->name == NULL)
495ee519a1fSgjelinek 			continue;
496ee519a1fSgjelinek 		if (xmlStrcmp(child->name, DTD_ELEM_PACKAGE) == 0 ||
497ee519a1fSgjelinek 		    xmlStrcmp(child->name, DTD_ELEM_PATCH) == 0) {
498ee519a1fSgjelinek 			next = child->next;
499ee519a1fSgjelinek 			xmlUnlinkNode(child);
500ee519a1fSgjelinek 			xmlFreeNode(child);
501ee519a1fSgjelinek 		}
502ee519a1fSgjelinek 	}
503ee519a1fSgjelinek }
504ee519a1fSgjelinek 
5057c478bd9Sstevel@tonic-gate static int
506108322fbScarlsonj zonecfg_get_handle_impl(const char *zonename, const char *filename,
507108322fbScarlsonj     zone_dochandle_t handle)
5087c478bd9Sstevel@tonic-gate {
5097c478bd9Sstevel@tonic-gate 	xmlValidCtxtPtr cvp;
5107c478bd9Sstevel@tonic-gate 	struct stat statbuf;
5117c478bd9Sstevel@tonic-gate 	int valid;
5127c478bd9Sstevel@tonic-gate 
5137c478bd9Sstevel@tonic-gate 	if (zonename == NULL)
5147c478bd9Sstevel@tonic-gate 		return (Z_NO_ZONE);
5157c478bd9Sstevel@tonic-gate 	if ((handle->zone_dh_doc = xmlParseFile(filename)) == NULL) {
5167c478bd9Sstevel@tonic-gate 		/* distinguish file not found vs. found but not parsed */
5177c478bd9Sstevel@tonic-gate 		if (stat(filename, &statbuf) == 0)
5187c478bd9Sstevel@tonic-gate 			return (Z_INVALID_DOCUMENT);
5197c478bd9Sstevel@tonic-gate 		return (Z_NO_ZONE);
5207c478bd9Sstevel@tonic-gate 	}
5217c478bd9Sstevel@tonic-gate 	if ((cvp = xmlNewValidCtxt()) == NULL)
5227c478bd9Sstevel@tonic-gate 		return (Z_NOMEM);
5237c478bd9Sstevel@tonic-gate 	cvp->error = zonecfg_error_func;
5247c478bd9Sstevel@tonic-gate 	cvp->warning = zonecfg_error_func;
5257c478bd9Sstevel@tonic-gate 	valid = xmlValidateDocument(cvp, handle->zone_dh_doc);
5267c478bd9Sstevel@tonic-gate 	xmlFreeValidCtxt(cvp);
5277c478bd9Sstevel@tonic-gate 	if (valid == 0)
5287c478bd9Sstevel@tonic-gate 		return (Z_INVALID_DOCUMENT);
529087719fdSdp 
5307c478bd9Sstevel@tonic-gate 	/* delete any comments such as inherited Sun copyright / ident str */
531087719fdSdp 	stripcomments(handle);
5327c478bd9Sstevel@tonic-gate 	return (Z_OK);
5337c478bd9Sstevel@tonic-gate }
5347c478bd9Sstevel@tonic-gate 
5357c478bd9Sstevel@tonic-gate int
536108322fbScarlsonj zonecfg_get_handle(const char *zonename, zone_dochandle_t handle)
5377c478bd9Sstevel@tonic-gate {
5387c478bd9Sstevel@tonic-gate 	char path[MAXPATHLEN];
5397c478bd9Sstevel@tonic-gate 
540108322fbScarlsonj 	if (!config_file_path(zonename, path))
541108322fbScarlsonj 		return (Z_MISC_FS);
542087719fdSdp 	handle->zone_dh_newzone = B_FALSE;
543087719fdSdp 
5447c478bd9Sstevel@tonic-gate 	return (zonecfg_get_handle_impl(zonename, path, handle));
5457c478bd9Sstevel@tonic-gate }
5467c478bd9Sstevel@tonic-gate 
5477c478bd9Sstevel@tonic-gate int
548ee519a1fSgjelinek zonecfg_get_attach_handle(const char *path, const char *zonename,
549ee519a1fSgjelinek     boolean_t preserve_sw, zone_dochandle_t handle)
550ee519a1fSgjelinek {
551ee519a1fSgjelinek 	char		migpath[MAXPATHLEN];
552ee519a1fSgjelinek 	int		err;
553ee519a1fSgjelinek 	struct stat	buf;
554ee519a1fSgjelinek 
555ee519a1fSgjelinek 	if (snprintf(migpath, sizeof (migpath), "%s/root", path) >=
556ee519a1fSgjelinek 	    sizeof (migpath))
557ee519a1fSgjelinek 		return (Z_NOMEM);
558ee519a1fSgjelinek 
559ee519a1fSgjelinek 	if (stat(migpath, &buf) == -1 || !S_ISDIR(buf.st_mode))
560ee519a1fSgjelinek 		return (Z_NO_ZONE);
561ee519a1fSgjelinek 
562ee519a1fSgjelinek 	if (snprintf(migpath, sizeof (migpath), "%s/%s", path, DETACHED) >=
563ee519a1fSgjelinek 	    sizeof (migpath))
564ee519a1fSgjelinek 		return (Z_NOMEM);
565ee519a1fSgjelinek 
566ee519a1fSgjelinek 	if ((err = zonecfg_get_handle_impl(zonename, migpath, handle)) != Z_OK)
567ee519a1fSgjelinek 		return (err);
568ee519a1fSgjelinek 
569ee519a1fSgjelinek 	if (!preserve_sw)
570ee519a1fSgjelinek 		strip_sw_inv(handle);
571ee519a1fSgjelinek 
572ee519a1fSgjelinek 	handle->zone_dh_newzone = B_TRUE;
573ee519a1fSgjelinek 	if ((err = setrootattr(handle, DTD_ATTR_ZONEPATH, path)) != Z_OK)
574ee519a1fSgjelinek 		return (err);
575ee519a1fSgjelinek 
576ee519a1fSgjelinek 	return (setrootattr(handle, DTD_ATTR_NAME, zonename));
577ee519a1fSgjelinek }
578ee519a1fSgjelinek 
579ee519a1fSgjelinek int
580108322fbScarlsonj zonecfg_get_snapshot_handle(const char *zonename, zone_dochandle_t handle)
5817c478bd9Sstevel@tonic-gate {
5827c478bd9Sstevel@tonic-gate 	char path[MAXPATHLEN];
5837c478bd9Sstevel@tonic-gate 
584108322fbScarlsonj 	if (!snap_file_path(zonename, path))
585108322fbScarlsonj 		return (Z_MISC_FS);
586087719fdSdp 	handle->zone_dh_newzone = B_FALSE;
5877c478bd9Sstevel@tonic-gate 	return (zonecfg_get_handle_impl(zonename, path, handle));
5887c478bd9Sstevel@tonic-gate }
5897c478bd9Sstevel@tonic-gate 
5907c478bd9Sstevel@tonic-gate int
591108322fbScarlsonj zonecfg_get_template_handle(const char *template, const char *zonename,
592087719fdSdp     zone_dochandle_t handle)
593087719fdSdp {
594087719fdSdp 	char path[MAXPATHLEN];
595087719fdSdp 	int err;
596087719fdSdp 
597108322fbScarlsonj 	if (!config_file_path(template, path))
598108322fbScarlsonj 		return (Z_MISC_FS);
599087719fdSdp 
600087719fdSdp 	if ((err = zonecfg_get_handle_impl(template, path, handle)) != Z_OK)
601087719fdSdp 		return (err);
602087719fdSdp 	handle->zone_dh_newzone = B_TRUE;
603087719fdSdp 	return (setrootattr(handle, DTD_ATTR_NAME, zonename));
604087719fdSdp }
605087719fdSdp 
606087719fdSdp static boolean_t
607087719fdSdp is_renaming(zone_dochandle_t handle)
608087719fdSdp {
609087719fdSdp 	if (handle->zone_dh_newzone)
610087719fdSdp 		return (B_FALSE);
611087719fdSdp 	if (strlen(handle->zone_dh_delete_name) > 0)
612087719fdSdp 		return (B_TRUE);
613087719fdSdp 	return (B_FALSE);
614087719fdSdp }
615087719fdSdp 
616087719fdSdp static boolean_t
617087719fdSdp is_new(zone_dochandle_t handle)
618087719fdSdp {
619087719fdSdp 	return (handle->zone_dh_newzone || handle->zone_dh_snapshot);
620087719fdSdp }
621087719fdSdp 
622087719fdSdp static boolean_t
623087719fdSdp is_snapshot(zone_dochandle_t handle)
624087719fdSdp {
625087719fdSdp 	return (handle->zone_dh_snapshot);
626087719fdSdp }
627087719fdSdp 
628087719fdSdp /*
629087719fdSdp  * It would be great to be able to use libc's ctype(3c) macros, but we
630087719fdSdp  * can't, as they are locale sensitive, and it would break our limited thread
631087719fdSdp  * safety if this routine had to change the app locale on the fly.
632087719fdSdp  */
633087719fdSdp int
634108322fbScarlsonj zonecfg_validate_zonename(const char *zone)
635087719fdSdp {
636087719fdSdp 	int i;
637087719fdSdp 
638087719fdSdp 	if (strcmp(zone, GLOBAL_ZONENAME) == 0)
639087719fdSdp 		return (Z_BOGUS_ZONE_NAME);
640087719fdSdp 
641087719fdSdp 	if (strlen(zone) >= ZONENAME_MAX)
642087719fdSdp 		return (Z_BOGUS_ZONE_NAME);
643087719fdSdp 
644087719fdSdp 	if (!((zone[0] >= 'a' && zone[0] <= 'z') ||
645087719fdSdp 	    (zone[0] >= 'A' && zone[0] <= 'Z') ||
646087719fdSdp 	    (zone[0] >= '0' && zone[0] <= '9')))
647087719fdSdp 		return (Z_BOGUS_ZONE_NAME);
648087719fdSdp 
649087719fdSdp 	for (i = 1; zone[i] != '\0'; i++) {
650087719fdSdp 		if (!((zone[i] >= 'a' && zone[i] <= 'z') ||
651087719fdSdp 		    (zone[i] >= 'A' && zone[i] <= 'Z') ||
652087719fdSdp 		    (zone[i] >= '0' && zone[i] <= '9') ||
653087719fdSdp 		    (zone[i] == '-') || (zone[i] == '_') || (zone[i] == '.')))
654087719fdSdp 			return (Z_BOGUS_ZONE_NAME);
655087719fdSdp 	}
656087719fdSdp 
657087719fdSdp 	return (Z_OK);
658087719fdSdp }
659087719fdSdp 
660087719fdSdp /*
661087719fdSdp  * Changing the zone name requires us to track both the old and new
662087719fdSdp  * name of the zone until commit time.
663087719fdSdp  */
664087719fdSdp int
6657c478bd9Sstevel@tonic-gate zonecfg_get_name(zone_dochandle_t handle, char *name, size_t namesize)
6667c478bd9Sstevel@tonic-gate {
667a1be23daSdp 	return (getrootattr(handle, DTD_ATTR_NAME, name, namesize));
6687c478bd9Sstevel@tonic-gate }
6697c478bd9Sstevel@tonic-gate 
670a1be23daSdp int
671a1be23daSdp zonecfg_set_name(zone_dochandle_t handle, char *name)
672a1be23daSdp {
673087719fdSdp 	zone_state_t state;
674087719fdSdp 	char curname[ZONENAME_MAX], old_delname[ZONENAME_MAX];
675087719fdSdp 	int err;
676087719fdSdp 
677087719fdSdp 	if ((err = getrootattr(handle, DTD_ATTR_NAME, curname,
678087719fdSdp 	    sizeof (curname))) != Z_OK)
679087719fdSdp 		return (err);
680087719fdSdp 
681087719fdSdp 	if (strcmp(name, curname) == 0)
682087719fdSdp 		return (Z_OK);
683087719fdSdp 
684087719fdSdp 	/*
685087719fdSdp 	 * Switching zone names to one beginning with SUNW is not permitted.
686087719fdSdp 	 */
687087719fdSdp 	if (strncmp(name, "SUNW", 4) == 0)
688087719fdSdp 		return (Z_BOGUS_ZONE_NAME);
689087719fdSdp 
690087719fdSdp 	if ((err = zonecfg_validate_zonename(name)) != Z_OK)
691087719fdSdp 		return (err);
692087719fdSdp 
693087719fdSdp 	/*
694087719fdSdp 	 * Setting the name back to the original name (effectively a revert of
695087719fdSdp 	 * the name) is fine.  But if we carry on, we'll falsely identify the
696087719fdSdp 	 * name as "in use," so special case here.
697087719fdSdp 	 */
698087719fdSdp 	if (strcmp(name, handle->zone_dh_delete_name) == 0) {
699087719fdSdp 		err = setrootattr(handle, DTD_ATTR_NAME, name);
700087719fdSdp 		handle->zone_dh_delete_name[0] = '\0';
701087719fdSdp 		return (err);
702087719fdSdp 	}
703087719fdSdp 
704087719fdSdp 	/* Check to see if new name chosen is already in use */
705087719fdSdp 	if (zone_get_state(name, &state) != Z_NO_ZONE)
706087719fdSdp 		return (Z_NAME_IN_USE);
707087719fdSdp 
708087719fdSdp 	/*
709087719fdSdp 	 * If this isn't already "new" or in a renaming transition, then
710087719fdSdp 	 * we're initiating a rename here; so stash the "delete name"
711087719fdSdp 	 * (i.e. the name of the zone we'll be removing) for the rename.
712087719fdSdp 	 */
713087719fdSdp 	(void) strlcpy(old_delname, handle->zone_dh_delete_name,
714087719fdSdp 	    sizeof (old_delname));
715087719fdSdp 	if (!is_new(handle) && !is_renaming(handle)) {
716087719fdSdp 		/*
717087719fdSdp 		 * Name change is allowed only when the zone we're altering
718087719fdSdp 		 * is not ready or running.
719087719fdSdp 		 */
720087719fdSdp 		err = zone_get_state(curname, &state);
721087719fdSdp 		if (err == Z_OK) {
722087719fdSdp 			if (state > ZONE_STATE_INSTALLED)
723087719fdSdp 				return (Z_BAD_ZONE_STATE);
724087719fdSdp 		} else if (err != Z_NO_ZONE) {
725087719fdSdp 			return (err);
726087719fdSdp 		}
727087719fdSdp 
728087719fdSdp 		(void) strlcpy(handle->zone_dh_delete_name, curname,
729087719fdSdp 		    sizeof (handle->zone_dh_delete_name));
730087719fdSdp 		assert(is_renaming(handle));
731087719fdSdp 	} else if (is_renaming(handle)) {
732087719fdSdp 		err = zone_get_state(handle->zone_dh_delete_name, &state);
733087719fdSdp 		if (err == Z_OK) {
734087719fdSdp 			if (state > ZONE_STATE_INSTALLED)
735087719fdSdp 				return (Z_BAD_ZONE_STATE);
736087719fdSdp 		} else if (err != Z_NO_ZONE) {
737087719fdSdp 			return (err);
738087719fdSdp 		}
739087719fdSdp 	}
740087719fdSdp 
741087719fdSdp 	if ((err = setrootattr(handle, DTD_ATTR_NAME, name)) != Z_OK) {
742087719fdSdp 		/*
743087719fdSdp 		 * Restore the deletename to whatever it was at the
744087719fdSdp 		 * top of the routine, since we've had a failure.
745087719fdSdp 		 */
746087719fdSdp 		(void) strlcpy(handle->zone_dh_delete_name, old_delname,
747087719fdSdp 		    sizeof (handle->zone_dh_delete_name));
748087719fdSdp 		return (err);
749087719fdSdp 	}
750087719fdSdp 
751087719fdSdp 	return (Z_OK);
7527c478bd9Sstevel@tonic-gate }
753a1be23daSdp 
754a1be23daSdp int
755a1be23daSdp zonecfg_get_zonepath(zone_dochandle_t handle, char *path, size_t pathsize)
756a1be23daSdp {
757108322fbScarlsonj 	size_t len;
758108322fbScarlsonj 
759108322fbScarlsonj 	if ((len = strlcpy(path, zonecfg_root, pathsize)) >= pathsize)
760108322fbScarlsonj 		return (Z_TOO_BIG);
761108322fbScarlsonj 	return (getrootattr(handle, DTD_ATTR_ZONEPATH, path + len,
762108322fbScarlsonj 	    pathsize - len));
763a1be23daSdp }
764a1be23daSdp 
765a1be23daSdp int
766a1be23daSdp zonecfg_set_zonepath(zone_dochandle_t handle, char *zonepath)
767a1be23daSdp {
768a1be23daSdp 	return (setrootattr(handle, DTD_ATTR_ZONEPATH, zonepath));
769a1be23daSdp }
770a1be23daSdp 
771a1be23daSdp int
772a1be23daSdp zonecfg_get_autoboot(zone_dochandle_t handle, boolean_t *autoboot)
773a1be23daSdp {
774a1be23daSdp 	char autobootstr[DTD_ENTITY_BOOL_LEN];
775a1be23daSdp 	int ret;
776a1be23daSdp 
777a1be23daSdp 	if ((ret = getrootattr(handle, DTD_ATTR_AUTOBOOT, autobootstr,
778a1be23daSdp 	    sizeof (autobootstr))) != Z_OK)
779a1be23daSdp 		return (ret);
780a1be23daSdp 
781a1be23daSdp 	if (strcmp(autobootstr, DTD_ENTITY_TRUE) == 0)
782a1be23daSdp 		*autoboot = B_TRUE;
783a1be23daSdp 	else if (strcmp(autobootstr, DTD_ENTITY_FALSE) == 0)
784a1be23daSdp 		*autoboot = B_FALSE;
785a1be23daSdp 	else
786a1be23daSdp 		ret = Z_BAD_PROPERTY;
787a1be23daSdp 	return (ret);
788a1be23daSdp }
789a1be23daSdp 
790a1be23daSdp int
791a1be23daSdp zonecfg_set_autoboot(zone_dochandle_t handle, boolean_t autoboot)
792a1be23daSdp {
793a1be23daSdp 	return (setrootattr(handle, DTD_ATTR_AUTOBOOT,
794a1be23daSdp 	    autoboot ? DTD_ENTITY_TRUE : DTD_ENTITY_FALSE));
795a1be23daSdp }
796a1be23daSdp 
797a1be23daSdp int
798a1be23daSdp zonecfg_get_pool(zone_dochandle_t handle, char *pool, size_t poolsize)
799a1be23daSdp {
800a1be23daSdp 	return (getrootattr(handle, DTD_ATTR_POOL, pool, poolsize));
801a1be23daSdp }
802a1be23daSdp 
803a1be23daSdp int
804a1be23daSdp zonecfg_set_pool(zone_dochandle_t handle, char *pool)
805a1be23daSdp {
806a1be23daSdp 	return (setrootattr(handle, DTD_ATTR_POOL, pool));
807a1be23daSdp }
808a1be23daSdp 
809ffbafc53Scomay int
810ffbafc53Scomay zonecfg_get_limitpriv(zone_dochandle_t handle, char **limitpriv)
811ffbafc53Scomay {
812ffbafc53Scomay 	return (get_alloc_rootattr(handle, DTD_ATTR_LIMITPRIV, limitpriv));
813ffbafc53Scomay }
814ffbafc53Scomay 
815ffbafc53Scomay int
816ffbafc53Scomay zonecfg_set_limitpriv(zone_dochandle_t handle, char *limitprivsize)
817ffbafc53Scomay {
818ffbafc53Scomay 	return (setrootattr(handle, DTD_ATTR_LIMITPRIV, limitprivsize));
819ffbafc53Scomay }
820ffbafc53Scomay 
821a1be23daSdp /*
822a1be23daSdp  * /etc/zones/index caches a vital piece of information which is also
823a1be23daSdp  * in the <zonename>.xml file: the path to the zone.  This is for performance,
824a1be23daSdp  * since we need to walk all zonepath's in order to be able to detect conflicts
825a1be23daSdp  * (see crosscheck_zonepaths() in the zoneadm command).
826087719fdSdp  *
827087719fdSdp  * An additional complexity is that when doing a rename, we'd like the entire
828087719fdSdp  * index update operation (rename, and potential state changes) to be atomic.
829087719fdSdp  * In general, the operation of this function should succeed or fail as
830087719fdSdp  * a unit.
831a1be23daSdp  */
832a1be23daSdp int
833a1be23daSdp zonecfg_refresh_index_file(zone_dochandle_t handle)
834a1be23daSdp {
835a1be23daSdp 	char name[ZONENAME_MAX], zonepath[MAXPATHLEN];
836a1be23daSdp 	struct zoneent ze;
837a1be23daSdp 	int err;
838087719fdSdp 	int opcode;
839087719fdSdp 	char *zn;
840087719fdSdp 
841087719fdSdp 	bzero(&ze, sizeof (ze));
842087719fdSdp 	ze.zone_state = -1;	/* Preserve existing state in index */
843a1be23daSdp 
844a1be23daSdp 	if ((err = zonecfg_get_name(handle, name, sizeof (name))) != Z_OK)
845a1be23daSdp 		return (err);
846087719fdSdp 	(void) strlcpy(ze.zone_name, name, sizeof (ze.zone_name));
847087719fdSdp 
848a1be23daSdp 	if ((err = zonecfg_get_zonepath(handle, zonepath,
849a1be23daSdp 	    sizeof (zonepath))) != Z_OK)
850a1be23daSdp 		return (err);
851a1be23daSdp 	(void) strlcpy(ze.zone_path, zonepath, sizeof (ze.zone_path));
852087719fdSdp 
853087719fdSdp 	if (is_renaming(handle)) {
854087719fdSdp 		opcode = PZE_MODIFY;
855087719fdSdp 		(void) strlcpy(ze.zone_name, handle->zone_dh_delete_name,
856087719fdSdp 		    sizeof (ze.zone_name));
857087719fdSdp 		(void) strlcpy(ze.zone_newname, name, sizeof (ze.zone_newname));
858087719fdSdp 	} else if (is_new(handle)) {
859087719fdSdp 		FILE *cookie;
860087719fdSdp 		/*
861087719fdSdp 		 * Be tolerant of the zone already existing in the index file,
862087719fdSdp 		 * since we might be forcibly overwriting an existing
863087719fdSdp 		 * configuration with a new one (for example 'create -F'
864087719fdSdp 		 * in zonecfg).
865087719fdSdp 		 */
866087719fdSdp 		opcode = PZE_ADD;
867087719fdSdp 		cookie = setzoneent();
868087719fdSdp 		while ((zn = getzoneent(cookie)) != NULL) {
869087719fdSdp 			if (strcmp(zn, name) == 0) {
870087719fdSdp 				opcode = PZE_MODIFY;
871087719fdSdp 				free(zn);
872087719fdSdp 				break;
873087719fdSdp 			}
874087719fdSdp 			free(zn);
875087719fdSdp 		}
876087719fdSdp 		endzoneent(cookie);
877087719fdSdp 		ze.zone_state = ZONE_STATE_CONFIGURED;
878087719fdSdp 	} else {
879087719fdSdp 		opcode = PZE_MODIFY;
8807c478bd9Sstevel@tonic-gate 	}
8817c478bd9Sstevel@tonic-gate 
882087719fdSdp 	if ((err = putzoneent(&ze, opcode)) != Z_OK)
883087719fdSdp 		return (err);
884087719fdSdp 
885087719fdSdp 	return (Z_OK);
886087719fdSdp }
887087719fdSdp 
888087719fdSdp /*
889087719fdSdp  * The goal of this routine is to cause the index file update and the
890087719fdSdp  * document save to happen as an atomic operation.  We do the document
891087719fdSdp  * first, saving a backup copy using a hard link; if that succeeds, we go
892087719fdSdp  * on to the index.  If that fails, we roll the document back into place.
893087719fdSdp  *
894087719fdSdp  * Strategy:
895087719fdSdp  *
896087719fdSdp  * New zone 'foo' configuration:
897087719fdSdp  * 	Create tmpfile (zonecfg.xxxxxx)
898087719fdSdp  * 	Write XML to tmpfile
899087719fdSdp  * 	Rename tmpfile to xmlfile (zonecfg.xxxxxx -> foo.xml)
900087719fdSdp  * 	Add entry to index file
901087719fdSdp  * 	If it fails, delete foo.xml, leaving nothing behind.
902087719fdSdp  *
903087719fdSdp  * Save existing zone 'foo':
904087719fdSdp  * 	Make backup of foo.xml -> .backup
905087719fdSdp  * 	Create tmpfile (zonecfg.xxxxxx)
906087719fdSdp  * 	Write XML to tmpfile
907087719fdSdp  * 	Rename tmpfile to xmlfile (zonecfg.xxxxxx -> foo.xml)
908087719fdSdp  * 	Modify index file as needed
909087719fdSdp  * 	If it fails, recover from .backup -> foo.xml
910087719fdSdp  *
911087719fdSdp  * Rename 'foo' to 'bar':
912087719fdSdp  * 	Create tmpfile (zonecfg.xxxxxx)
913087719fdSdp  * 	Write XML to tmpfile
914087719fdSdp  * 	Rename tmpfile to xmlfile (zonecfg.xxxxxx -> bar.xml)
915087719fdSdp  * 	Add entry for 'bar' to index file, Remove entry for 'foo' (refresh)
916087719fdSdp  * 	If it fails, delete bar.xml; foo.xml is left behind.
917087719fdSdp  */
9187c478bd9Sstevel@tonic-gate static int
9197c478bd9Sstevel@tonic-gate zonecfg_save_impl(zone_dochandle_t handle, char *filename)
9207c478bd9Sstevel@tonic-gate {
9217c478bd9Sstevel@tonic-gate 	char tmpfile[MAXPATHLEN];
922087719fdSdp 	char bakdir[MAXPATHLEN], bakbase[MAXPATHLEN], bakfile[MAXPATHLEN];
923087719fdSdp 	int tmpfd, err;
9247c478bd9Sstevel@tonic-gate 	xmlValidCtxt cvp = { NULL };
925087719fdSdp 	boolean_t backup;
9267c478bd9Sstevel@tonic-gate 
9277c478bd9Sstevel@tonic-gate 	(void) strlcpy(tmpfile, filename, sizeof (tmpfile));
9287c478bd9Sstevel@tonic-gate 	(void) dirname(tmpfile);
9297c478bd9Sstevel@tonic-gate 	(void) strlcat(tmpfile, _PATH_TMPFILE, sizeof (tmpfile));
9307c478bd9Sstevel@tonic-gate 
9317c478bd9Sstevel@tonic-gate 	tmpfd = mkstemp(tmpfile);
9327c478bd9Sstevel@tonic-gate 	if (tmpfd == -1) {
9337c478bd9Sstevel@tonic-gate 		(void) unlink(tmpfile);
9347c478bd9Sstevel@tonic-gate 		return (Z_TEMP_FILE);
9357c478bd9Sstevel@tonic-gate 	}
9367c478bd9Sstevel@tonic-gate 	(void) close(tmpfd);
9377c478bd9Sstevel@tonic-gate 
9387c478bd9Sstevel@tonic-gate 	cvp.error = zonecfg_error_func;
9397c478bd9Sstevel@tonic-gate 	cvp.warning = zonecfg_error_func;
9407c478bd9Sstevel@tonic-gate 
941087719fdSdp 	/*
942087719fdSdp 	 * We do a final validation of the document-- but the library has
943087719fdSdp 	 * malfunctioned if it fails to validate, so it's an assert.
944087719fdSdp 	 */
945087719fdSdp 	assert(xmlValidateDocument(&cvp, handle->zone_dh_doc) != 0);
9467c478bd9Sstevel@tonic-gate 
9477c478bd9Sstevel@tonic-gate 	if (xmlSaveFormatFile(tmpfile, handle->zone_dh_doc, 1) <= 0)
9487c478bd9Sstevel@tonic-gate 		goto err;
949087719fdSdp 
9507c478bd9Sstevel@tonic-gate 	(void) chmod(tmpfile, 0644);
9517c478bd9Sstevel@tonic-gate 
952087719fdSdp 	/*
953087719fdSdp 	 * In the event we are doing a standard save, hard link a copy of the
954087719fdSdp 	 * original file in .backup.<pid>.filename so we can restore it if
955087719fdSdp 	 * something goes wrong.
956087719fdSdp 	 */
957087719fdSdp 	if (!is_new(handle) && !is_renaming(handle)) {
958087719fdSdp 		backup = B_TRUE;
959087719fdSdp 
960087719fdSdp 		(void) strlcpy(bakdir, filename, sizeof (bakdir));
961087719fdSdp 		(void) strlcpy(bakbase, filename, sizeof (bakbase));
962087719fdSdp 		(void) snprintf(bakfile, sizeof (bakfile), "%s/.backup.%d.%s",
963087719fdSdp 		    dirname(bakdir), getpid(), basename(bakbase));
964087719fdSdp 
965087719fdSdp 		if (link(filename, bakfile) == -1) {
966087719fdSdp 			err = errno;
9677c478bd9Sstevel@tonic-gate 			(void) unlink(tmpfile);
9687c478bd9Sstevel@tonic-gate 			if (errno == EACCES)
9697c478bd9Sstevel@tonic-gate 				return (Z_ACCES);
9707c478bd9Sstevel@tonic-gate 			return (Z_MISC_FS);
9717c478bd9Sstevel@tonic-gate 		}
972087719fdSdp 	}
973a1be23daSdp 
974087719fdSdp 	/*
975087719fdSdp 	 * Move the new document over top of the old.
976087719fdSdp 	 * i.e.:   zonecfg.XXXXXX  ->  myzone.xml
977087719fdSdp 	 */
978087719fdSdp 	if (rename(tmpfile, filename) == -1) {
979087719fdSdp 		err = errno;
980087719fdSdp 		(void) unlink(tmpfile);
981087719fdSdp 		if (backup)
982087719fdSdp 			(void) unlink(bakfile);
983087719fdSdp 		if (err == EACCES)
984087719fdSdp 			return (Z_ACCES);
985087719fdSdp 		return (Z_MISC_FS);
986087719fdSdp 	}
987087719fdSdp 
988087719fdSdp 	/*
989087719fdSdp 	 * If this is a snapshot, we're done-- don't add an index entry.
990087719fdSdp 	 */
991087719fdSdp 	if (is_snapshot(handle))
992087719fdSdp 		return (Z_OK);
993087719fdSdp 
994087719fdSdp 	/* now update the index file to reflect whatever we just did */
995087719fdSdp 	if ((err = zonecfg_refresh_index_file(handle)) != Z_OK) {
996087719fdSdp 		if (backup) {
997087719fdSdp 			/*
998087719fdSdp 			 * Try to restore from our backup.
999087719fdSdp 			 */
1000087719fdSdp 			(void) unlink(filename);
1001087719fdSdp 			(void) rename(bakfile, filename);
1002087719fdSdp 		} else {
1003087719fdSdp 			/*
1004087719fdSdp 			 * Either the zone is new, in which case we can delete
1005087719fdSdp 			 * new.xml, or we're doing a rename, so ditto.
1006087719fdSdp 			 */
1007087719fdSdp 			assert(is_new(handle) || is_renaming(handle));
1008087719fdSdp 			(void) unlink(filename);
1009087719fdSdp 		}
1010087719fdSdp 		return (Z_UPDATING_INDEX);
1011087719fdSdp 	}
1012087719fdSdp 
1013087719fdSdp 	if (backup)
1014087719fdSdp 		(void) unlink(bakfile);
1015087719fdSdp 
1016087719fdSdp 	return (Z_OK);
10177c478bd9Sstevel@tonic-gate 
10187c478bd9Sstevel@tonic-gate err:
10197c478bd9Sstevel@tonic-gate 	(void) unlink(tmpfile);
10207c478bd9Sstevel@tonic-gate 	return (Z_SAVING_FILE);
10217c478bd9Sstevel@tonic-gate }
10227c478bd9Sstevel@tonic-gate 
10237c478bd9Sstevel@tonic-gate int
10247c478bd9Sstevel@tonic-gate zonecfg_save(zone_dochandle_t handle)
10257c478bd9Sstevel@tonic-gate {
1026087719fdSdp 	char zname[ZONENAME_MAX], path[MAXPATHLEN];
1027087719fdSdp 	char delpath[MAXPATHLEN];
1028087719fdSdp 	int err = Z_SAVING_FILE;
10297c478bd9Sstevel@tonic-gate 
1030087719fdSdp 	if (zonecfg_check_handle(handle) != Z_OK)
1031087719fdSdp 		return (Z_BAD_HANDLE);
1032087719fdSdp 
1033087719fdSdp 	/*
1034ee519a1fSgjelinek 	 * We don't support saving snapshots or a tree containing a sw
1035ee519a1fSgjelinek 	 * inventory at this time.
1036087719fdSdp 	 */
1037ee519a1fSgjelinek 	if (handle->zone_dh_snapshot || handle->zone_dh_sw_inv)
1038087719fdSdp 		return (Z_INVAL);
1039087719fdSdp 
1040087719fdSdp 	if ((err = zonecfg_get_name(handle, zname, sizeof (zname))) != Z_OK)
10417c478bd9Sstevel@tonic-gate 		return (err);
1042087719fdSdp 
1043108322fbScarlsonj 	if (!config_file_path(zname, path))
1044108322fbScarlsonj 		return (Z_MISC_FS);
1045087719fdSdp 
1046087719fdSdp 	addcomment(handle, "\n    DO NOT EDIT THIS "
1047087719fdSdp 	    "FILE.  Use zonecfg(1M) instead.\n");
1048087719fdSdp 
1049087719fdSdp 	err = zonecfg_save_impl(handle, path);
1050087719fdSdp 
1051087719fdSdp 	stripcomments(handle);
1052087719fdSdp 
1053087719fdSdp 	if (err != Z_OK)
1054087719fdSdp 		return (err);
1055087719fdSdp 
1056087719fdSdp 	handle->zone_dh_newzone = B_FALSE;
1057087719fdSdp 
1058087719fdSdp 	if (is_renaming(handle)) {
1059108322fbScarlsonj 		if (config_file_path(handle->zone_dh_delete_name, delpath))
1060087719fdSdp 			(void) unlink(delpath);
1061087719fdSdp 		handle->zone_dh_delete_name[0] = '\0';
1062087719fdSdp 	}
1063087719fdSdp 
1064087719fdSdp 	return (Z_OK);
10657c478bd9Sstevel@tonic-gate }
10667c478bd9Sstevel@tonic-gate 
1067ee519a1fSgjelinek int
1068ee519a1fSgjelinek zonecfg_detach_save(zone_dochandle_t handle)
1069ee519a1fSgjelinek {
1070ee519a1fSgjelinek 	char zname[ZONENAME_MAX];
1071ee519a1fSgjelinek 	char path[MAXPATHLEN];
1072ee519a1fSgjelinek 	char migpath[MAXPATHLEN];
1073ee519a1fSgjelinek 	xmlValidCtxt cvp = { NULL };
1074ee519a1fSgjelinek 	int err = Z_SAVING_FILE;
1075ee519a1fSgjelinek 
1076ee519a1fSgjelinek 	if (zonecfg_check_handle(handle) != Z_OK)
1077ee519a1fSgjelinek 		return (Z_BAD_HANDLE);
1078ee519a1fSgjelinek 
1079ee519a1fSgjelinek 	/*
1080ee519a1fSgjelinek 	 * We can only detach if we have taken a sw inventory.
1081ee519a1fSgjelinek 	 */
1082ee519a1fSgjelinek 	if (!handle->zone_dh_sw_inv)
1083ee519a1fSgjelinek 		return (Z_INVAL);
1084ee519a1fSgjelinek 
1085ee519a1fSgjelinek 	if ((err = zonecfg_get_name(handle, zname, sizeof (zname))) != Z_OK)
1086ee519a1fSgjelinek 		return (err);
1087ee519a1fSgjelinek 
1088ee519a1fSgjelinek 	if ((err = zone_get_zonepath(zname, path, sizeof (path))) != Z_OK)
1089ee519a1fSgjelinek 		return (err);
1090ee519a1fSgjelinek 
1091ee519a1fSgjelinek 	if (snprintf(migpath, sizeof (migpath), "%s/%s", path, DETACHED) >=
1092ee519a1fSgjelinek 	    sizeof (migpath))
1093ee519a1fSgjelinek 		return (Z_NOMEM);
1094ee519a1fSgjelinek 
1095ee519a1fSgjelinek 	if ((err = operation_prep(handle)) != Z_OK)
1096ee519a1fSgjelinek 		return (err);
1097ee519a1fSgjelinek 
1098ee519a1fSgjelinek 	addcomment(handle, "\n    DO NOT EDIT THIS FILE.  "
1099ee519a1fSgjelinek 	    "Use zonecfg(1M) and zoneadm(1M) attach.\n");
1100ee519a1fSgjelinek 
1101ee519a1fSgjelinek 	cvp.error = zonecfg_error_func;
1102ee519a1fSgjelinek 	cvp.warning = zonecfg_error_func;
1103ee519a1fSgjelinek 
1104ee519a1fSgjelinek 	/*
1105ee519a1fSgjelinek 	 * We do a final validation of the document-- but the library has
1106ee519a1fSgjelinek 	 * malfunctioned if it fails to validate, so it's an assert.
1107ee519a1fSgjelinek 	 */
1108ee519a1fSgjelinek 	assert(xmlValidateDocument(&cvp, handle->zone_dh_doc) != 0);
1109ee519a1fSgjelinek 
1110ee519a1fSgjelinek 	if (xmlSaveFormatFile(migpath, handle->zone_dh_doc, 1) <= 0)
1111ee519a1fSgjelinek 		return (Z_SAVING_FILE);
1112ee519a1fSgjelinek 
1113ee519a1fSgjelinek 	(void) chmod(migpath, 0644);
1114ee519a1fSgjelinek 
1115ee519a1fSgjelinek 	stripcomments(handle);
1116ee519a1fSgjelinek 
1117ee519a1fSgjelinek 	handle->zone_dh_newzone = B_FALSE;
1118ee519a1fSgjelinek 
1119ee519a1fSgjelinek 	return (Z_OK);
1120ee519a1fSgjelinek }
1121ee519a1fSgjelinek 
1122ee519a1fSgjelinek boolean_t
1123ee519a1fSgjelinek zonecfg_detached(const char *path)
1124ee519a1fSgjelinek {
1125ee519a1fSgjelinek 	char		migpath[MAXPATHLEN];
1126ee519a1fSgjelinek 	struct stat	buf;
1127ee519a1fSgjelinek 
1128ee519a1fSgjelinek 	if (snprintf(migpath, sizeof (migpath), "%s/%s", path, DETACHED) >=
1129ee519a1fSgjelinek 	    sizeof (migpath))
1130ee519a1fSgjelinek 		return (B_FALSE);
1131ee519a1fSgjelinek 
1132ee519a1fSgjelinek 	if (stat(migpath, &buf) != -1)
1133ee519a1fSgjelinek 		return (B_TRUE);
1134ee519a1fSgjelinek 
1135ee519a1fSgjelinek 	return (B_FALSE);
1136ee519a1fSgjelinek }
1137ee519a1fSgjelinek 
1138ee519a1fSgjelinek void
1139ee519a1fSgjelinek zonecfg_rm_detached(zone_dochandle_t handle, boolean_t forced)
1140ee519a1fSgjelinek {
1141ee519a1fSgjelinek 	char zname[ZONENAME_MAX];
1142ee519a1fSgjelinek 	char path[MAXPATHLEN];
1143ee519a1fSgjelinek 	char detached[MAXPATHLEN];
1144ee519a1fSgjelinek 	char attached[MAXPATHLEN];
1145ee519a1fSgjelinek 
1146ee519a1fSgjelinek 	if (zonecfg_check_handle(handle) != Z_OK)
1147ee519a1fSgjelinek 		return;
1148ee519a1fSgjelinek 
1149ee519a1fSgjelinek 	if (zonecfg_get_name(handle, zname, sizeof (zname)) != Z_OK)
1150ee519a1fSgjelinek 		return;
1151ee519a1fSgjelinek 
1152ee519a1fSgjelinek 	if (zone_get_zonepath(zname, path, sizeof (path)) != Z_OK)
1153ee519a1fSgjelinek 		return;
1154ee519a1fSgjelinek 
1155ee519a1fSgjelinek 	(void) snprintf(detached, sizeof (detached), "%s/%s", path, DETACHED);
1156ee519a1fSgjelinek 	(void) snprintf(attached, sizeof (attached), "%s/%s", path,
1157ee519a1fSgjelinek 	    ATTACH_FORCED);
1158ee519a1fSgjelinek 
1159ee519a1fSgjelinek 	if (forced) {
1160ee519a1fSgjelinek 		(void) rename(detached, attached);
1161ee519a1fSgjelinek 	} else {
1162ee519a1fSgjelinek 		(void) unlink(attached);
1163ee519a1fSgjelinek 		(void) unlink(detached);
1164ee519a1fSgjelinek 	}
1165ee519a1fSgjelinek }
1166ee519a1fSgjelinek 
11677c478bd9Sstevel@tonic-gate /*
11687c478bd9Sstevel@tonic-gate  * Special case: if access(2) fails with ENOENT, then try again using
11697c478bd9Sstevel@tonic-gate  * ZONE_CONFIG_ROOT instead of config_file_path(zonename).  This is how we
11707c478bd9Sstevel@tonic-gate  * work around the case of a config file which has not been created yet:
11717c478bd9Sstevel@tonic-gate  * the user will need access to the directory so use that as a heuristic.
11727c478bd9Sstevel@tonic-gate  */
11737c478bd9Sstevel@tonic-gate 
11747c478bd9Sstevel@tonic-gate int
11757c478bd9Sstevel@tonic-gate zonecfg_access(const char *zonename, int amode)
11767c478bd9Sstevel@tonic-gate {
11777c478bd9Sstevel@tonic-gate 	char path[MAXPATHLEN];
11787c478bd9Sstevel@tonic-gate 
1179108322fbScarlsonj 	if (!config_file_path(zonename, path))
1180108322fbScarlsonj 		return (Z_INVAL);
11817c478bd9Sstevel@tonic-gate 	if (access(path, amode) == 0)
11827c478bd9Sstevel@tonic-gate 		return (Z_OK);
1183108322fbScarlsonj 	if (errno == ENOENT) {
1184108322fbScarlsonj 		if (snprintf(path, sizeof (path), "%s%s", zonecfg_root,
1185108322fbScarlsonj 		    ZONE_CONFIG_ROOT) >= sizeof (path))
1186108322fbScarlsonj 			return (Z_INVAL);
1187108322fbScarlsonj 		if (access(path, amode) == 0)
11887c478bd9Sstevel@tonic-gate 			return (Z_OK);
1189108322fbScarlsonj 	}
11907c478bd9Sstevel@tonic-gate 	if (errno == EACCES)
11917c478bd9Sstevel@tonic-gate 		return (Z_ACCES);
11927c478bd9Sstevel@tonic-gate 	if (errno == EINVAL)
11937c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
11947c478bd9Sstevel@tonic-gate 	return (Z_MISC_FS);
11957c478bd9Sstevel@tonic-gate }
11967c478bd9Sstevel@tonic-gate 
11977c478bd9Sstevel@tonic-gate int
1198108322fbScarlsonj zonecfg_create_snapshot(const char *zonename)
11997c478bd9Sstevel@tonic-gate {
12007c478bd9Sstevel@tonic-gate 	zone_dochandle_t handle;
12017c478bd9Sstevel@tonic-gate 	char path[MAXPATHLEN], zonepath[MAXPATHLEN], rpath[MAXPATHLEN];
12027c478bd9Sstevel@tonic-gate 	int error = Z_OK, res;
12037c478bd9Sstevel@tonic-gate 
12047c478bd9Sstevel@tonic-gate 	if ((handle = zonecfg_init_handle()) == NULL) {
12057c478bd9Sstevel@tonic-gate 		return (Z_NOMEM);
12067c478bd9Sstevel@tonic-gate 	}
12077c478bd9Sstevel@tonic-gate 
1208087719fdSdp 	handle->zone_dh_newzone = B_TRUE;
1209087719fdSdp 	handle->zone_dh_snapshot = B_TRUE;
1210087719fdSdp 
12117c478bd9Sstevel@tonic-gate 	if ((error = zonecfg_get_handle(zonename, handle)) != Z_OK)
12127c478bd9Sstevel@tonic-gate 		goto out;
12137c478bd9Sstevel@tonic-gate 	if ((error = operation_prep(handle)) != Z_OK)
12147c478bd9Sstevel@tonic-gate 		goto out;
12157c478bd9Sstevel@tonic-gate 	error = zonecfg_get_zonepath(handle, zonepath, sizeof (zonepath));
12167c478bd9Sstevel@tonic-gate 	if (error != Z_OK)
12177c478bd9Sstevel@tonic-gate 		goto out;
12187c478bd9Sstevel@tonic-gate 	if ((res = resolvepath(zonepath, rpath, sizeof (rpath))) == -1) {
12197c478bd9Sstevel@tonic-gate 		error = Z_RESOLVED_PATH;
12207c478bd9Sstevel@tonic-gate 		goto out;
12217c478bd9Sstevel@tonic-gate 	}
12227c478bd9Sstevel@tonic-gate 	/*
12237c478bd9Sstevel@tonic-gate 	 * If the resolved path is not the same as the original path, then
12247c478bd9Sstevel@tonic-gate 	 * save the resolved path in the snapshot, thus preventing any
12257c478bd9Sstevel@tonic-gate 	 * potential problems down the line when zoneadmd goes to unmount
12267c478bd9Sstevel@tonic-gate 	 * file systems and depends on initial string matches with resolved
12277c478bd9Sstevel@tonic-gate 	 * paths.
12287c478bd9Sstevel@tonic-gate 	 */
12297c478bd9Sstevel@tonic-gate 	rpath[res] = '\0';
12307c478bd9Sstevel@tonic-gate 	if (strcmp(zonepath, rpath) != 0) {
12317c478bd9Sstevel@tonic-gate 		if ((error = zonecfg_set_zonepath(handle, rpath)) != Z_OK)
12327c478bd9Sstevel@tonic-gate 			goto out;
12337c478bd9Sstevel@tonic-gate 	}
1234108322fbScarlsonj 	if (snprintf(path, sizeof (path), "%s%s", zonecfg_root,
1235108322fbScarlsonj 	    ZONE_SNAPSHOT_ROOT) >= sizeof (path)) {
1236108322fbScarlsonj 		error = Z_MISC_FS;
1237108322fbScarlsonj 		goto out;
1238108322fbScarlsonj 	}
1239108322fbScarlsonj 	if ((mkdir(path, S_IRWXU) == -1) && (errno != EEXIST)) {
12407c478bd9Sstevel@tonic-gate 		error = Z_MISC_FS;
12417c478bd9Sstevel@tonic-gate 		goto out;
12427c478bd9Sstevel@tonic-gate 	}
12437c478bd9Sstevel@tonic-gate 
1244108322fbScarlsonj 	if (!snap_file_path(zonename, path)) {
1245108322fbScarlsonj 		error = Z_MISC_FS;
1246108322fbScarlsonj 		goto out;
1247108322fbScarlsonj 	}
1248087719fdSdp 
1249087719fdSdp 	addcomment(handle, "\n    DO NOT EDIT THIS FILE.  "
1250087719fdSdp 	    "It is a snapshot of running zone state.\n");
1251087719fdSdp 
12527c478bd9Sstevel@tonic-gate 	error = zonecfg_save_impl(handle, path);
12537c478bd9Sstevel@tonic-gate 
1254087719fdSdp 	stripcomments(handle);
1255087719fdSdp 
12567c478bd9Sstevel@tonic-gate out:
12577c478bd9Sstevel@tonic-gate 	zonecfg_fini_handle(handle);
12587c478bd9Sstevel@tonic-gate 	return (error);
12597c478bd9Sstevel@tonic-gate }
12607c478bd9Sstevel@tonic-gate 
12617c478bd9Sstevel@tonic-gate static int
1262a1be23daSdp newprop(xmlNodePtr node, const xmlChar *attrname, char *src)
12637c478bd9Sstevel@tonic-gate {
12647c478bd9Sstevel@tonic-gate 	xmlAttrPtr newattr;
12657c478bd9Sstevel@tonic-gate 
12667c478bd9Sstevel@tonic-gate 	newattr = xmlNewProp(node, attrname, (xmlChar *)src);
12677c478bd9Sstevel@tonic-gate 	if (newattr == NULL) {
12687c478bd9Sstevel@tonic-gate 		xmlUnlinkNode(node);
12697c478bd9Sstevel@tonic-gate 		xmlFreeNode(node);
12707c478bd9Sstevel@tonic-gate 		return (Z_BAD_PROPERTY);
12717c478bd9Sstevel@tonic-gate 	}
12727c478bd9Sstevel@tonic-gate 	return (Z_OK);
12737c478bd9Sstevel@tonic-gate }
12747c478bd9Sstevel@tonic-gate 
12757c478bd9Sstevel@tonic-gate static int
12767c478bd9Sstevel@tonic-gate zonecfg_add_filesystem_core(zone_dochandle_t handle, struct zone_fstab *tabptr)
12777c478bd9Sstevel@tonic-gate {
12787c478bd9Sstevel@tonic-gate 	xmlNodePtr newnode, cur = handle->zone_dh_cur, options_node;
12797c478bd9Sstevel@tonic-gate 	zone_fsopt_t *ptr;
12807c478bd9Sstevel@tonic-gate 	int err;
12817c478bd9Sstevel@tonic-gate 
12827c478bd9Sstevel@tonic-gate 	newnode = xmlNewTextChild(cur, NULL, DTD_ELEM_FS, NULL);
1283a1be23daSdp 	if ((err = newprop(newnode, DTD_ATTR_SPECIAL,
12847c478bd9Sstevel@tonic-gate 	    tabptr->zone_fs_special)) != Z_OK)
12857c478bd9Sstevel@tonic-gate 		return (err);
12867c478bd9Sstevel@tonic-gate 	if (tabptr->zone_fs_raw[0] != '\0' &&
1287a1be23daSdp 	    (err = newprop(newnode, DTD_ATTR_RAW, tabptr->zone_fs_raw)) != Z_OK)
12887c478bd9Sstevel@tonic-gate 		return (err);
1289a1be23daSdp 	if ((err = newprop(newnode, DTD_ATTR_DIR, tabptr->zone_fs_dir)) != Z_OK)
12907c478bd9Sstevel@tonic-gate 		return (err);
1291a1be23daSdp 	if ((err = newprop(newnode, DTD_ATTR_TYPE,
12927c478bd9Sstevel@tonic-gate 	    tabptr->zone_fs_type)) != Z_OK)
12937c478bd9Sstevel@tonic-gate 		return (err);
12947c478bd9Sstevel@tonic-gate 	if (tabptr->zone_fs_options != NULL) {
12957c478bd9Sstevel@tonic-gate 		for (ptr = tabptr->zone_fs_options; ptr != NULL;
12967c478bd9Sstevel@tonic-gate 		    ptr = ptr->zone_fsopt_next) {
12977c478bd9Sstevel@tonic-gate 			options_node = xmlNewTextChild(newnode, NULL,
12987c478bd9Sstevel@tonic-gate 			    DTD_ELEM_FSOPTION, NULL);
1299a1be23daSdp 			if ((err = newprop(options_node, DTD_ATTR_NAME,
13007c478bd9Sstevel@tonic-gate 			    ptr->zone_fsopt_opt)) != Z_OK)
13017c478bd9Sstevel@tonic-gate 				return (err);
13027c478bd9Sstevel@tonic-gate 		}
13037c478bd9Sstevel@tonic-gate 	}
13047c478bd9Sstevel@tonic-gate 	return (Z_OK);
13057c478bd9Sstevel@tonic-gate }
13067c478bd9Sstevel@tonic-gate 
13077c478bd9Sstevel@tonic-gate int
13087c478bd9Sstevel@tonic-gate zonecfg_add_filesystem(zone_dochandle_t handle, struct zone_fstab *tabptr)
13097c478bd9Sstevel@tonic-gate {
13107c478bd9Sstevel@tonic-gate 	int err;
13117c478bd9Sstevel@tonic-gate 
13127c478bd9Sstevel@tonic-gate 	if (tabptr == NULL)
13137c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
13147c478bd9Sstevel@tonic-gate 
13157c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
13167c478bd9Sstevel@tonic-gate 		return (err);
13177c478bd9Sstevel@tonic-gate 
13187c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_add_filesystem_core(handle, tabptr)) != Z_OK)
13197c478bd9Sstevel@tonic-gate 		return (err);
13207c478bd9Sstevel@tonic-gate 
13217c478bd9Sstevel@tonic-gate 	return (Z_OK);
13227c478bd9Sstevel@tonic-gate }
13237c478bd9Sstevel@tonic-gate 
13247c478bd9Sstevel@tonic-gate static int
13257c478bd9Sstevel@tonic-gate zonecfg_add_ipd_core(zone_dochandle_t handle, struct zone_fstab *tabptr)
13267c478bd9Sstevel@tonic-gate {
13277c478bd9Sstevel@tonic-gate 	xmlNodePtr newnode, cur = handle->zone_dh_cur;
13287c478bd9Sstevel@tonic-gate 	int err;
13297c478bd9Sstevel@tonic-gate 
13307c478bd9Sstevel@tonic-gate 	newnode = xmlNewTextChild(cur, NULL, DTD_ELEM_IPD, NULL);
1331a1be23daSdp 	if ((err = newprop(newnode, DTD_ATTR_DIR, tabptr->zone_fs_dir)) != Z_OK)
13327c478bd9Sstevel@tonic-gate 		return (err);
13337c478bd9Sstevel@tonic-gate 	return (Z_OK);
13347c478bd9Sstevel@tonic-gate }
13357c478bd9Sstevel@tonic-gate 
13367c478bd9Sstevel@tonic-gate int
13377c478bd9Sstevel@tonic-gate zonecfg_add_ipd(zone_dochandle_t handle, struct zone_fstab *tabptr)
13387c478bd9Sstevel@tonic-gate {
13397c478bd9Sstevel@tonic-gate 	int err;
13407c478bd9Sstevel@tonic-gate 
13417c478bd9Sstevel@tonic-gate 	if (tabptr == NULL)
13427c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
13437c478bd9Sstevel@tonic-gate 
13447c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
13457c478bd9Sstevel@tonic-gate 		return (err);
13467c478bd9Sstevel@tonic-gate 
13477c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_add_ipd_core(handle, tabptr)) != Z_OK)
13487c478bd9Sstevel@tonic-gate 		return (err);
13497c478bd9Sstevel@tonic-gate 
13507c478bd9Sstevel@tonic-gate 	return (Z_OK);
13517c478bd9Sstevel@tonic-gate }
13527c478bd9Sstevel@tonic-gate 
13537c478bd9Sstevel@tonic-gate int
13547c478bd9Sstevel@tonic-gate zonecfg_add_fs_option(struct zone_fstab *tabptr, char *option)
13557c478bd9Sstevel@tonic-gate {
13567c478bd9Sstevel@tonic-gate 	zone_fsopt_t *last, *old, *new;
13577c478bd9Sstevel@tonic-gate 
13587c478bd9Sstevel@tonic-gate 	last = tabptr->zone_fs_options;
13597c478bd9Sstevel@tonic-gate 	for (old = last; old != NULL; old = old->zone_fsopt_next)
13607c478bd9Sstevel@tonic-gate 		last = old;	/* walk to the end of the list */
13617c478bd9Sstevel@tonic-gate 	new = (zone_fsopt_t *)malloc(sizeof (zone_fsopt_t));
13627c478bd9Sstevel@tonic-gate 	if (new == NULL)
13637c478bd9Sstevel@tonic-gate 		return (Z_NOMEM);
13647c478bd9Sstevel@tonic-gate 	(void) strlcpy(new->zone_fsopt_opt, option,
13657c478bd9Sstevel@tonic-gate 	    sizeof (new->zone_fsopt_opt));
13667c478bd9Sstevel@tonic-gate 	new->zone_fsopt_next = NULL;
13677c478bd9Sstevel@tonic-gate 	if (last == NULL)
13687c478bd9Sstevel@tonic-gate 		tabptr->zone_fs_options = new;
13697c478bd9Sstevel@tonic-gate 	else
13707c478bd9Sstevel@tonic-gate 		last->zone_fsopt_next = new;
13717c478bd9Sstevel@tonic-gate 	return (Z_OK);
13727c478bd9Sstevel@tonic-gate }
13737c478bd9Sstevel@tonic-gate 
13747c478bd9Sstevel@tonic-gate int
13757c478bd9Sstevel@tonic-gate zonecfg_remove_fs_option(struct zone_fstab *tabptr, char *option)
13767c478bd9Sstevel@tonic-gate {
13777c478bd9Sstevel@tonic-gate 	zone_fsopt_t *last, *this, *next;
13787c478bd9Sstevel@tonic-gate 
13797c478bd9Sstevel@tonic-gate 	last = tabptr->zone_fs_options;
13807c478bd9Sstevel@tonic-gate 	for (this = last; this != NULL; this = this->zone_fsopt_next) {
13817c478bd9Sstevel@tonic-gate 		if (strcmp(this->zone_fsopt_opt, option) == 0) {
13827c478bd9Sstevel@tonic-gate 			next = this->zone_fsopt_next;
13837c478bd9Sstevel@tonic-gate 			if (this == tabptr->zone_fs_options)
13847c478bd9Sstevel@tonic-gate 				tabptr->zone_fs_options = next;
13857c478bd9Sstevel@tonic-gate 			else
13867c478bd9Sstevel@tonic-gate 				last->zone_fsopt_next = next;
13877c478bd9Sstevel@tonic-gate 			free(this);
13887c478bd9Sstevel@tonic-gate 			return (Z_OK);
13897c478bd9Sstevel@tonic-gate 		} else
13907c478bd9Sstevel@tonic-gate 			last = this;
13917c478bd9Sstevel@tonic-gate 	}
13927c478bd9Sstevel@tonic-gate 	return (Z_NO_PROPERTY_ID);
13937c478bd9Sstevel@tonic-gate }
13947c478bd9Sstevel@tonic-gate 
13957c478bd9Sstevel@tonic-gate void
13967c478bd9Sstevel@tonic-gate zonecfg_free_fs_option_list(zone_fsopt_t *list)
13977c478bd9Sstevel@tonic-gate {
13987c478bd9Sstevel@tonic-gate 	zone_fsopt_t *this, *next;
13997c478bd9Sstevel@tonic-gate 
14007c478bd9Sstevel@tonic-gate 	for (this = list; this != NULL; this = next) {
14017c478bd9Sstevel@tonic-gate 		next = this->zone_fsopt_next;
14027c478bd9Sstevel@tonic-gate 		free(this);
14037c478bd9Sstevel@tonic-gate 	}
14047c478bd9Sstevel@tonic-gate }
14057c478bd9Sstevel@tonic-gate 
14067c478bd9Sstevel@tonic-gate void
14077c478bd9Sstevel@tonic-gate zonecfg_free_rctl_value_list(struct zone_rctlvaltab *valtab)
14087c478bd9Sstevel@tonic-gate {
14097c478bd9Sstevel@tonic-gate 	if (valtab == NULL)
14107c478bd9Sstevel@tonic-gate 		return;
14117c478bd9Sstevel@tonic-gate 	zonecfg_free_rctl_value_list(valtab->zone_rctlval_next);
14127c478bd9Sstevel@tonic-gate 	free(valtab);
14137c478bd9Sstevel@tonic-gate }
14147c478bd9Sstevel@tonic-gate 
14157c478bd9Sstevel@tonic-gate static boolean_t
14167c478bd9Sstevel@tonic-gate match_prop(xmlNodePtr cur, const xmlChar *attr, char *user_prop)
14177c478bd9Sstevel@tonic-gate {
14187c478bd9Sstevel@tonic-gate 	xmlChar *gotten_prop;
14197c478bd9Sstevel@tonic-gate 	int prop_result;
14207c478bd9Sstevel@tonic-gate 
14217c478bd9Sstevel@tonic-gate 	gotten_prop = xmlGetProp(cur, attr);
14227c478bd9Sstevel@tonic-gate 	if (gotten_prop == NULL)	/* shouldn't happen */
14237c478bd9Sstevel@tonic-gate 		return (B_FALSE);
14247c478bd9Sstevel@tonic-gate 	prop_result = xmlStrcmp(gotten_prop, (const xmlChar *) user_prop);
14257c478bd9Sstevel@tonic-gate 	xmlFree(gotten_prop);
14267c478bd9Sstevel@tonic-gate 	return ((prop_result == 0));
14277c478bd9Sstevel@tonic-gate }
14287c478bd9Sstevel@tonic-gate 
14297c478bd9Sstevel@tonic-gate static int
14307c478bd9Sstevel@tonic-gate zonecfg_delete_filesystem_core(zone_dochandle_t handle,
14317c478bd9Sstevel@tonic-gate     struct zone_fstab *tabptr)
14327c478bd9Sstevel@tonic-gate {
14337c478bd9Sstevel@tonic-gate 	xmlNodePtr cur = handle->zone_dh_cur;
14347c478bd9Sstevel@tonic-gate 	boolean_t dir_match, spec_match, raw_match, type_match;
14357c478bd9Sstevel@tonic-gate 
14367c478bd9Sstevel@tonic-gate 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
14377c478bd9Sstevel@tonic-gate 		if (xmlStrcmp(cur->name, DTD_ELEM_FS))
14387c478bd9Sstevel@tonic-gate 			continue;
14397c478bd9Sstevel@tonic-gate 		dir_match = match_prop(cur, DTD_ATTR_DIR, tabptr->zone_fs_dir);
14407c478bd9Sstevel@tonic-gate 		spec_match = match_prop(cur, DTD_ATTR_SPECIAL,
14417c478bd9Sstevel@tonic-gate 		    tabptr->zone_fs_special);
14427c478bd9Sstevel@tonic-gate 		raw_match = match_prop(cur, DTD_ATTR_RAW,
14437c478bd9Sstevel@tonic-gate 		    tabptr->zone_fs_raw);
14447c478bd9Sstevel@tonic-gate 		type_match = match_prop(cur, DTD_ATTR_TYPE,
14457c478bd9Sstevel@tonic-gate 		    tabptr->zone_fs_type);
14467c478bd9Sstevel@tonic-gate 		if (dir_match && spec_match && raw_match && type_match) {
14477c478bd9Sstevel@tonic-gate 			xmlUnlinkNode(cur);
14487c478bd9Sstevel@tonic-gate 			xmlFreeNode(cur);
14497c478bd9Sstevel@tonic-gate 			return (Z_OK);
14507c478bd9Sstevel@tonic-gate 		}
14517c478bd9Sstevel@tonic-gate 	}
14527c478bd9Sstevel@tonic-gate 	return (Z_NO_RESOURCE_ID);
14537c478bd9Sstevel@tonic-gate }
14547c478bd9Sstevel@tonic-gate 
14557c478bd9Sstevel@tonic-gate int
14567c478bd9Sstevel@tonic-gate zonecfg_delete_filesystem(zone_dochandle_t handle, struct zone_fstab *tabptr)
14577c478bd9Sstevel@tonic-gate {
14587c478bd9Sstevel@tonic-gate 	int err;
14597c478bd9Sstevel@tonic-gate 
14607c478bd9Sstevel@tonic-gate 	if (tabptr == NULL)
14617c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
14627c478bd9Sstevel@tonic-gate 
14637c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
14647c478bd9Sstevel@tonic-gate 		return (err);
14657c478bd9Sstevel@tonic-gate 
14667c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_delete_filesystem_core(handle, tabptr)) != Z_OK)
14677c478bd9Sstevel@tonic-gate 		return (err);
14687c478bd9Sstevel@tonic-gate 
14697c478bd9Sstevel@tonic-gate 	return (Z_OK);
14707c478bd9Sstevel@tonic-gate }
14717c478bd9Sstevel@tonic-gate 
14727c478bd9Sstevel@tonic-gate int
14737c478bd9Sstevel@tonic-gate zonecfg_modify_filesystem(
14747c478bd9Sstevel@tonic-gate 	zone_dochandle_t handle,
14757c478bd9Sstevel@tonic-gate 	struct zone_fstab *oldtabptr,
14767c478bd9Sstevel@tonic-gate 	struct zone_fstab *newtabptr)
14777c478bd9Sstevel@tonic-gate {
14787c478bd9Sstevel@tonic-gate 	int err;
14797c478bd9Sstevel@tonic-gate 
14807c478bd9Sstevel@tonic-gate 	if (oldtabptr == NULL || newtabptr == NULL)
14817c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
14827c478bd9Sstevel@tonic-gate 
14837c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
14847c478bd9Sstevel@tonic-gate 		return (err);
14857c478bd9Sstevel@tonic-gate 
14867c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_delete_filesystem_core(handle, oldtabptr)) != Z_OK)
14877c478bd9Sstevel@tonic-gate 		return (err);
14887c478bd9Sstevel@tonic-gate 
14897c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_add_filesystem_core(handle, newtabptr)) != Z_OK)
14907c478bd9Sstevel@tonic-gate 		return (err);
14917c478bd9Sstevel@tonic-gate 
14927c478bd9Sstevel@tonic-gate 	return (Z_OK);
14937c478bd9Sstevel@tonic-gate }
14947c478bd9Sstevel@tonic-gate 
14957c478bd9Sstevel@tonic-gate static int
14967c478bd9Sstevel@tonic-gate zonecfg_delete_ipd_core(zone_dochandle_t handle, struct zone_fstab *tabptr)
14977c478bd9Sstevel@tonic-gate {
14987c478bd9Sstevel@tonic-gate 	xmlNodePtr cur = handle->zone_dh_cur;
14997c478bd9Sstevel@tonic-gate 
15007c478bd9Sstevel@tonic-gate 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
15017c478bd9Sstevel@tonic-gate 		if (xmlStrcmp(cur->name, DTD_ELEM_IPD))
15027c478bd9Sstevel@tonic-gate 			continue;
15037c478bd9Sstevel@tonic-gate 		if (match_prop(cur, DTD_ATTR_DIR, tabptr->zone_fs_dir)) {
15047c478bd9Sstevel@tonic-gate 			xmlUnlinkNode(cur);
15057c478bd9Sstevel@tonic-gate 			xmlFreeNode(cur);
15067c478bd9Sstevel@tonic-gate 			return (Z_OK);
15077c478bd9Sstevel@tonic-gate 		}
15087c478bd9Sstevel@tonic-gate 	}
15097c478bd9Sstevel@tonic-gate 	return (Z_NO_RESOURCE_ID);
15107c478bd9Sstevel@tonic-gate }
15117c478bd9Sstevel@tonic-gate 
15127c478bd9Sstevel@tonic-gate int
15137c478bd9Sstevel@tonic-gate zonecfg_delete_ipd(zone_dochandle_t handle, struct zone_fstab *tabptr)
15147c478bd9Sstevel@tonic-gate {
15157c478bd9Sstevel@tonic-gate 	int err;
15167c478bd9Sstevel@tonic-gate 
15177c478bd9Sstevel@tonic-gate 	if (tabptr == NULL)
15187c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
15197c478bd9Sstevel@tonic-gate 
15207c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
15217c478bd9Sstevel@tonic-gate 		return (err);
15227c478bd9Sstevel@tonic-gate 
15237c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_delete_ipd_core(handle, tabptr)) != Z_OK)
15247c478bd9Sstevel@tonic-gate 		return (err);
15257c478bd9Sstevel@tonic-gate 
15267c478bd9Sstevel@tonic-gate 	return (Z_OK);
15277c478bd9Sstevel@tonic-gate }
15287c478bd9Sstevel@tonic-gate 
15297c478bd9Sstevel@tonic-gate int
15307c478bd9Sstevel@tonic-gate zonecfg_modify_ipd(zone_dochandle_t handle, struct zone_fstab *oldtabptr,
15317c478bd9Sstevel@tonic-gate     struct zone_fstab *newtabptr)
15327c478bd9Sstevel@tonic-gate {
15337c478bd9Sstevel@tonic-gate 	int err;
15347c478bd9Sstevel@tonic-gate 
15357c478bd9Sstevel@tonic-gate 	if (oldtabptr == NULL || newtabptr == NULL)
15367c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
15377c478bd9Sstevel@tonic-gate 
15387c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
15397c478bd9Sstevel@tonic-gate 		return (err);
15407c478bd9Sstevel@tonic-gate 
15417c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_delete_ipd_core(handle, oldtabptr)) != Z_OK)
15427c478bd9Sstevel@tonic-gate 		return (err);
15437c478bd9Sstevel@tonic-gate 
15447c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_add_ipd_core(handle, newtabptr)) != Z_OK)
15457c478bd9Sstevel@tonic-gate 		return (err);
15467c478bd9Sstevel@tonic-gate 
15477c478bd9Sstevel@tonic-gate 	return (Z_OK);
15487c478bd9Sstevel@tonic-gate }
15497c478bd9Sstevel@tonic-gate 
15507c478bd9Sstevel@tonic-gate int
15517c478bd9Sstevel@tonic-gate zonecfg_lookup_filesystem(
15527c478bd9Sstevel@tonic-gate 	zone_dochandle_t handle,
15537c478bd9Sstevel@tonic-gate 	struct zone_fstab *tabptr)
15547c478bd9Sstevel@tonic-gate {
15557c478bd9Sstevel@tonic-gate 	xmlNodePtr cur, options, firstmatch;
15567c478bd9Sstevel@tonic-gate 	int err;
15577c478bd9Sstevel@tonic-gate 	char dirname[MAXPATHLEN], special[MAXPATHLEN], raw[MAXPATHLEN];
15587c478bd9Sstevel@tonic-gate 	char type[FSTYPSZ];
15597c478bd9Sstevel@tonic-gate 	char options_str[MAX_MNTOPT_STR];
15607c478bd9Sstevel@tonic-gate 
15617c478bd9Sstevel@tonic-gate 	if (tabptr == NULL)
15627c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
15637c478bd9Sstevel@tonic-gate 
15647c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
15657c478bd9Sstevel@tonic-gate 		return (err);
15667c478bd9Sstevel@tonic-gate 
15677c478bd9Sstevel@tonic-gate 	/*
15687c478bd9Sstevel@tonic-gate 	 * Walk the list of children looking for matches on any properties
15697c478bd9Sstevel@tonic-gate 	 * specified in the fstab parameter.  If more than one resource
15707c478bd9Sstevel@tonic-gate 	 * matches, we return Z_INSUFFICIENT_SPEC; if none match, we return
15717c478bd9Sstevel@tonic-gate 	 * Z_NO_RESOURCE_ID.
15727c478bd9Sstevel@tonic-gate 	 */
15737c478bd9Sstevel@tonic-gate 	cur = handle->zone_dh_cur;
15747c478bd9Sstevel@tonic-gate 	firstmatch = NULL;
15757c478bd9Sstevel@tonic-gate 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
15767c478bd9Sstevel@tonic-gate 		if (xmlStrcmp(cur->name, DTD_ELEM_FS))
15777c478bd9Sstevel@tonic-gate 			continue;
15787c478bd9Sstevel@tonic-gate 		if (strlen(tabptr->zone_fs_dir) > 0) {
15797c478bd9Sstevel@tonic-gate 			if ((fetchprop(cur, DTD_ATTR_DIR, dirname,
15807c478bd9Sstevel@tonic-gate 			    sizeof (dirname)) == Z_OK) &&
15817c478bd9Sstevel@tonic-gate 			    (strcmp(tabptr->zone_fs_dir, dirname) == 0)) {
15827c478bd9Sstevel@tonic-gate 				if (firstmatch == NULL)
15837c478bd9Sstevel@tonic-gate 					firstmatch = cur;
15847c478bd9Sstevel@tonic-gate 				else
15857c478bd9Sstevel@tonic-gate 					return (Z_INSUFFICIENT_SPEC);
15867c478bd9Sstevel@tonic-gate 			}
15877c478bd9Sstevel@tonic-gate 		}
15887c478bd9Sstevel@tonic-gate 		if (strlen(tabptr->zone_fs_special) > 0) {
15897c478bd9Sstevel@tonic-gate 			if ((fetchprop(cur, DTD_ATTR_SPECIAL, special,
15907c478bd9Sstevel@tonic-gate 			    sizeof (special)) == Z_OK)) {
15917c478bd9Sstevel@tonic-gate 				if (strcmp(tabptr->zone_fs_special,
15927c478bd9Sstevel@tonic-gate 				    special) == 0) {
15937c478bd9Sstevel@tonic-gate 					if (firstmatch == NULL)
15947c478bd9Sstevel@tonic-gate 						firstmatch = cur;
15957c478bd9Sstevel@tonic-gate 					else if (firstmatch != cur)
15967c478bd9Sstevel@tonic-gate 						return (Z_INSUFFICIENT_SPEC);
15977c478bd9Sstevel@tonic-gate 				} else {
15987c478bd9Sstevel@tonic-gate 					/*
15997c478bd9Sstevel@tonic-gate 					 * If another property matched but this
16007c478bd9Sstevel@tonic-gate 					 * one doesn't then reset firstmatch.
16017c478bd9Sstevel@tonic-gate 					 */
16027c478bd9Sstevel@tonic-gate 					if (firstmatch == cur)
16037c478bd9Sstevel@tonic-gate 						firstmatch = NULL;
16047c478bd9Sstevel@tonic-gate 				}
16057c478bd9Sstevel@tonic-gate 			}
16067c478bd9Sstevel@tonic-gate 		}
16077c478bd9Sstevel@tonic-gate 		if (strlen(tabptr->zone_fs_raw) > 0) {
16087c478bd9Sstevel@tonic-gate 			if ((fetchprop(cur, DTD_ATTR_RAW, raw,
16097c478bd9Sstevel@tonic-gate 			    sizeof (raw)) == Z_OK)) {
16107c478bd9Sstevel@tonic-gate 				if (strcmp(tabptr->zone_fs_raw, raw) == 0) {
16117c478bd9Sstevel@tonic-gate 					if (firstmatch == NULL)
16127c478bd9Sstevel@tonic-gate 						firstmatch = cur;
16137c478bd9Sstevel@tonic-gate 					else if (firstmatch != cur)
16147c478bd9Sstevel@tonic-gate 						return (Z_INSUFFICIENT_SPEC);
16157c478bd9Sstevel@tonic-gate 				} else {
16167c478bd9Sstevel@tonic-gate 					/*
16177c478bd9Sstevel@tonic-gate 					 * If another property matched but this
16187c478bd9Sstevel@tonic-gate 					 * one doesn't then reset firstmatch.
16197c478bd9Sstevel@tonic-gate 					 */
16207c478bd9Sstevel@tonic-gate 					if (firstmatch == cur)
16217c478bd9Sstevel@tonic-gate 						firstmatch = NULL;
16227c478bd9Sstevel@tonic-gate 				}
16237c478bd9Sstevel@tonic-gate 			}
16247c478bd9Sstevel@tonic-gate 		}
16257c478bd9Sstevel@tonic-gate 		if (strlen(tabptr->zone_fs_type) > 0) {
16267c478bd9Sstevel@tonic-gate 			if ((fetchprop(cur, DTD_ATTR_TYPE, type,
16277c478bd9Sstevel@tonic-gate 			    sizeof (type)) == Z_OK)) {
16287c478bd9Sstevel@tonic-gate 				if (strcmp(tabptr->zone_fs_type, type) == 0) {
16297c478bd9Sstevel@tonic-gate 					if (firstmatch == NULL)
16307c478bd9Sstevel@tonic-gate 						firstmatch = cur;
16317c478bd9Sstevel@tonic-gate 					else if (firstmatch != cur)
16327c478bd9Sstevel@tonic-gate 						return (Z_INSUFFICIENT_SPEC);
16337c478bd9Sstevel@tonic-gate 				} else {
16347c478bd9Sstevel@tonic-gate 					/*
16357c478bd9Sstevel@tonic-gate 					 * If another property matched but this
16367c478bd9Sstevel@tonic-gate 					 * one doesn't then reset firstmatch.
16377c478bd9Sstevel@tonic-gate 					 */
16387c478bd9Sstevel@tonic-gate 					if (firstmatch == cur)
16397c478bd9Sstevel@tonic-gate 						firstmatch = NULL;
16407c478bd9Sstevel@tonic-gate 				}
16417c478bd9Sstevel@tonic-gate 			}
16427c478bd9Sstevel@tonic-gate 		}
16437c478bd9Sstevel@tonic-gate 	}
16447c478bd9Sstevel@tonic-gate 
16457c478bd9Sstevel@tonic-gate 	if (firstmatch == NULL)
16467c478bd9Sstevel@tonic-gate 		return (Z_NO_RESOURCE_ID);
16477c478bd9Sstevel@tonic-gate 
16487c478bd9Sstevel@tonic-gate 	cur = firstmatch;
16497c478bd9Sstevel@tonic-gate 
16507c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_DIR, tabptr->zone_fs_dir,
16517c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_fs_dir))) != Z_OK)
16527c478bd9Sstevel@tonic-gate 		return (err);
16537c478bd9Sstevel@tonic-gate 
16547c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_SPECIAL, tabptr->zone_fs_special,
16557c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_fs_special))) != Z_OK)
16567c478bd9Sstevel@tonic-gate 		return (err);
16577c478bd9Sstevel@tonic-gate 
16587c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_RAW, tabptr->zone_fs_raw,
16597c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_fs_raw))) != Z_OK)
16607c478bd9Sstevel@tonic-gate 		return (err);
16617c478bd9Sstevel@tonic-gate 
16627c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_TYPE, tabptr->zone_fs_type,
16637c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_fs_type))) != Z_OK)
16647c478bd9Sstevel@tonic-gate 		return (err);
16657c478bd9Sstevel@tonic-gate 
16667c478bd9Sstevel@tonic-gate 	/* options are optional */
16677c478bd9Sstevel@tonic-gate 	tabptr->zone_fs_options = NULL;
16687c478bd9Sstevel@tonic-gate 	for (options = cur->xmlChildrenNode; options != NULL;
16697c478bd9Sstevel@tonic-gate 	    options = options->next) {
16707c478bd9Sstevel@tonic-gate 		if ((fetchprop(options, DTD_ATTR_NAME, options_str,
16717c478bd9Sstevel@tonic-gate 		    sizeof (options_str)) != Z_OK))
16727c478bd9Sstevel@tonic-gate 			break;
16737c478bd9Sstevel@tonic-gate 		if (zonecfg_add_fs_option(tabptr, options_str) != Z_OK)
16747c478bd9Sstevel@tonic-gate 			break;
16757c478bd9Sstevel@tonic-gate 	}
16767c478bd9Sstevel@tonic-gate 	return (Z_OK);
16777c478bd9Sstevel@tonic-gate }
16787c478bd9Sstevel@tonic-gate 
16797c478bd9Sstevel@tonic-gate int
16807c478bd9Sstevel@tonic-gate zonecfg_lookup_ipd(zone_dochandle_t handle, struct zone_fstab *tabptr)
16817c478bd9Sstevel@tonic-gate {
16827c478bd9Sstevel@tonic-gate 	xmlNodePtr cur, match;
16837c478bd9Sstevel@tonic-gate 	int err;
16847c478bd9Sstevel@tonic-gate 	char dirname[MAXPATHLEN];
16857c478bd9Sstevel@tonic-gate 
16867c478bd9Sstevel@tonic-gate 	if (tabptr == NULL)
16877c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
16887c478bd9Sstevel@tonic-gate 
16897c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
16907c478bd9Sstevel@tonic-gate 		return (err);
16917c478bd9Sstevel@tonic-gate 
16927c478bd9Sstevel@tonic-gate 	/*
16937c478bd9Sstevel@tonic-gate 	 * General algorithm:
16947c478bd9Sstevel@tonic-gate 	 * Walk the list of children looking for matches on any properties
16957c478bd9Sstevel@tonic-gate 	 * specified in the fstab parameter.  If more than one resource
16967c478bd9Sstevel@tonic-gate 	 * matches, we return Z_INSUFFICIENT_SPEC; if none match, we return
16977c478bd9Sstevel@tonic-gate 	 * Z_NO_RESOURCE_ID.
16987c478bd9Sstevel@tonic-gate 	 */
16997c478bd9Sstevel@tonic-gate 	cur = handle->zone_dh_cur;
17007c478bd9Sstevel@tonic-gate 	match = NULL;
17017c478bd9Sstevel@tonic-gate 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
17027c478bd9Sstevel@tonic-gate 		if (xmlStrcmp(cur->name, DTD_ELEM_IPD))
17037c478bd9Sstevel@tonic-gate 			continue;
17047c478bd9Sstevel@tonic-gate 		if (strlen(tabptr->zone_fs_dir) > 0) {
17057c478bd9Sstevel@tonic-gate 			if ((fetchprop(cur, DTD_ATTR_DIR, dirname,
17067c478bd9Sstevel@tonic-gate 			    sizeof (dirname)) == Z_OK) &&
17077c478bd9Sstevel@tonic-gate 			    (strcmp(tabptr->zone_fs_dir, dirname) == 0)) {
17087c478bd9Sstevel@tonic-gate 				if (match == NULL)
17097c478bd9Sstevel@tonic-gate 					match = cur;
17107c478bd9Sstevel@tonic-gate 				else
17117c478bd9Sstevel@tonic-gate 					return (Z_INSUFFICIENT_SPEC);
17127c478bd9Sstevel@tonic-gate 			}
17137c478bd9Sstevel@tonic-gate 		}
17147c478bd9Sstevel@tonic-gate 	}
17157c478bd9Sstevel@tonic-gate 
17167c478bd9Sstevel@tonic-gate 	if (match == NULL)
17177c478bd9Sstevel@tonic-gate 		return (Z_NO_RESOURCE_ID);
17187c478bd9Sstevel@tonic-gate 
17197c478bd9Sstevel@tonic-gate 	cur = match;
17207c478bd9Sstevel@tonic-gate 
17217c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_DIR, tabptr->zone_fs_dir,
17227c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_fs_dir))) != Z_OK)
17237c478bd9Sstevel@tonic-gate 		return (err);
17247c478bd9Sstevel@tonic-gate 
17257c478bd9Sstevel@tonic-gate 	return (Z_OK);
17267c478bd9Sstevel@tonic-gate }
17277c478bd9Sstevel@tonic-gate 
17287c478bd9Sstevel@tonic-gate /*
17297c478bd9Sstevel@tonic-gate  * Compare two IP addresses in string form.  Allow for the possibility that
17307c478bd9Sstevel@tonic-gate  * one might have "/<prefix-length>" at the end: allow a match on just the
17317c478bd9Sstevel@tonic-gate  * IP address (or host name) part.
17327c478bd9Sstevel@tonic-gate  */
17337c478bd9Sstevel@tonic-gate 
17347c478bd9Sstevel@tonic-gate boolean_t
17357c478bd9Sstevel@tonic-gate zonecfg_same_net_address(char *a1, char *a2)
17367c478bd9Sstevel@tonic-gate {
17377c478bd9Sstevel@tonic-gate 	char *slashp, *slashp1, *slashp2;
17387c478bd9Sstevel@tonic-gate 	int result;
17397c478bd9Sstevel@tonic-gate 
17407c478bd9Sstevel@tonic-gate 	if (strcmp(a1, a2) == 0)
17417c478bd9Sstevel@tonic-gate 		return (B_TRUE);
17427c478bd9Sstevel@tonic-gate 
17437c478bd9Sstevel@tonic-gate 	/*
17447c478bd9Sstevel@tonic-gate 	 * If neither has a slash or both do, they need to match to be
17457c478bd9Sstevel@tonic-gate 	 * considered the same, but they did not match above, so fail.
17467c478bd9Sstevel@tonic-gate 	 */
17477c478bd9Sstevel@tonic-gate 	slashp1 = strchr(a1, '/');
17487c478bd9Sstevel@tonic-gate 	slashp2 = strchr(a2, '/');
17497c478bd9Sstevel@tonic-gate 	if ((slashp1 == NULL && slashp2 == NULL) ||
17507c478bd9Sstevel@tonic-gate 	    (slashp1 != NULL && slashp2 != NULL))
17517c478bd9Sstevel@tonic-gate 		return (B_FALSE);
17527c478bd9Sstevel@tonic-gate 
17537c478bd9Sstevel@tonic-gate 	/*
17547c478bd9Sstevel@tonic-gate 	 * Only one had a slash: pick that one, zero out the slash, compare
17557c478bd9Sstevel@tonic-gate 	 * the "address only" strings, restore the slash, and return the
17567c478bd9Sstevel@tonic-gate 	 * result of the comparison.
17577c478bd9Sstevel@tonic-gate 	 */
17587c478bd9Sstevel@tonic-gate 	slashp = (slashp1 == NULL) ? slashp2 : slashp1;
17597c478bd9Sstevel@tonic-gate 	*slashp = '\0';
17607c478bd9Sstevel@tonic-gate 	result = strcmp(a1, a2);
17617c478bd9Sstevel@tonic-gate 	*slashp = '/';
17627c478bd9Sstevel@tonic-gate 	return ((result == 0));
17637c478bd9Sstevel@tonic-gate }
17647c478bd9Sstevel@tonic-gate 
17657c478bd9Sstevel@tonic-gate int
17667c478bd9Sstevel@tonic-gate zonecfg_valid_net_address(char *address, struct lifreq *lifr)
17677c478bd9Sstevel@tonic-gate {
17687c478bd9Sstevel@tonic-gate 	struct sockaddr_in *sin4;
17697c478bd9Sstevel@tonic-gate 	struct sockaddr_in6 *sin6;
17707c478bd9Sstevel@tonic-gate 	struct addrinfo hints, *result;
17717c478bd9Sstevel@tonic-gate 	char *slashp = strchr(address, '/');
17727c478bd9Sstevel@tonic-gate 
17737c478bd9Sstevel@tonic-gate 	bzero(lifr, sizeof (struct lifreq));
17747c478bd9Sstevel@tonic-gate 	sin4 = (struct sockaddr_in *)&lifr->lifr_addr;
17757c478bd9Sstevel@tonic-gate 	sin6 = (struct sockaddr_in6 *)&lifr->lifr_addr;
17767c478bd9Sstevel@tonic-gate 	if (slashp != NULL)
17777c478bd9Sstevel@tonic-gate 		*slashp = '\0';
17787c478bd9Sstevel@tonic-gate 	if (inet_pton(AF_INET, address, &sin4->sin_addr) == 1) {
17797c478bd9Sstevel@tonic-gate 		sin4->sin_family = AF_INET;
17807c478bd9Sstevel@tonic-gate 	} else if (inet_pton(AF_INET6, address, &sin6->sin6_addr) == 1) {
17817c478bd9Sstevel@tonic-gate 		if (slashp == NULL)
17827c478bd9Sstevel@tonic-gate 			return (Z_IPV6_ADDR_PREFIX_LEN);
17837c478bd9Sstevel@tonic-gate 		sin6->sin6_family = AF_INET6;
17847c478bd9Sstevel@tonic-gate 	} else {
17857c478bd9Sstevel@tonic-gate 		/* "address" may be a host name */
17867c478bd9Sstevel@tonic-gate 		(void) memset(&hints, 0, sizeof (hints));
17877c478bd9Sstevel@tonic-gate 		hints.ai_family = PF_INET;
17887c478bd9Sstevel@tonic-gate 		if (getaddrinfo(address, NULL, &hints, &result) != 0)
17897c478bd9Sstevel@tonic-gate 			return (Z_BOGUS_ADDRESS);
17907c478bd9Sstevel@tonic-gate 		sin4->sin_family = result->ai_family;
1791a1be23daSdp 
17927c478bd9Sstevel@tonic-gate 		(void) memcpy(&sin4->sin_addr,
17937c478bd9Sstevel@tonic-gate 		    /* LINTED E_BAD_PTR_CAST_ALIGN */
17947c478bd9Sstevel@tonic-gate 		    &((struct sockaddr_in *)result->ai_addr)->sin_addr,
17957c478bd9Sstevel@tonic-gate 		    sizeof (struct in_addr));
1796a1be23daSdp 
17977c478bd9Sstevel@tonic-gate 		freeaddrinfo(result);
17987c478bd9Sstevel@tonic-gate 	}
17997c478bd9Sstevel@tonic-gate 	return (Z_OK);
18007c478bd9Sstevel@tonic-gate }
18017c478bd9Sstevel@tonic-gate 
18027c478bd9Sstevel@tonic-gate int
18037c478bd9Sstevel@tonic-gate zonecfg_lookup_nwif(zone_dochandle_t handle, struct zone_nwiftab *tabptr)
18047c478bd9Sstevel@tonic-gate {
18057c478bd9Sstevel@tonic-gate 	xmlNodePtr cur, firstmatch;
18067c478bd9Sstevel@tonic-gate 	int err;
18077c478bd9Sstevel@tonic-gate 	char address[INET6_ADDRSTRLEN], physical[LIFNAMSIZ];
18087c478bd9Sstevel@tonic-gate 
18097c478bd9Sstevel@tonic-gate 	if (tabptr == NULL)
18107c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
18117c478bd9Sstevel@tonic-gate 
18127c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
18137c478bd9Sstevel@tonic-gate 		return (err);
18147c478bd9Sstevel@tonic-gate 
18157c478bd9Sstevel@tonic-gate 	cur = handle->zone_dh_cur;
18167c478bd9Sstevel@tonic-gate 	firstmatch = NULL;
18177c478bd9Sstevel@tonic-gate 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
18187c478bd9Sstevel@tonic-gate 		if (xmlStrcmp(cur->name, DTD_ELEM_NET))
18197c478bd9Sstevel@tonic-gate 			continue;
18207c478bd9Sstevel@tonic-gate 		if (strlen(tabptr->zone_nwif_physical) > 0) {
18217c478bd9Sstevel@tonic-gate 			if ((fetchprop(cur, DTD_ATTR_PHYSICAL, physical,
18227c478bd9Sstevel@tonic-gate 			    sizeof (physical)) == Z_OK) &&
18237c478bd9Sstevel@tonic-gate 			    (strcmp(tabptr->zone_nwif_physical,
18247c478bd9Sstevel@tonic-gate 			    physical) == 0)) {
18257c478bd9Sstevel@tonic-gate 				if (firstmatch == NULL)
18267c478bd9Sstevel@tonic-gate 					firstmatch = cur;
18277c478bd9Sstevel@tonic-gate 				else
18287c478bd9Sstevel@tonic-gate 					return (Z_INSUFFICIENT_SPEC);
18297c478bd9Sstevel@tonic-gate 			}
18307c478bd9Sstevel@tonic-gate 		}
18317c478bd9Sstevel@tonic-gate 		if (strlen(tabptr->zone_nwif_address) > 0) {
18327c478bd9Sstevel@tonic-gate 			if ((fetchprop(cur, DTD_ATTR_ADDRESS, address,
18337c478bd9Sstevel@tonic-gate 			    sizeof (address)) == Z_OK)) {
18347c478bd9Sstevel@tonic-gate 				if (zonecfg_same_net_address(
18357c478bd9Sstevel@tonic-gate 				    tabptr->zone_nwif_address, address)) {
18367c478bd9Sstevel@tonic-gate 					if (firstmatch == NULL)
18377c478bd9Sstevel@tonic-gate 						firstmatch = cur;
18387c478bd9Sstevel@tonic-gate 					else if (firstmatch != cur)
18397c478bd9Sstevel@tonic-gate 						return (Z_INSUFFICIENT_SPEC);
18407c478bd9Sstevel@tonic-gate 				} else {
18417c478bd9Sstevel@tonic-gate 					/*
18427c478bd9Sstevel@tonic-gate 					 * If another property matched but this
18437c478bd9Sstevel@tonic-gate 					 * one doesn't then reset firstmatch.
18447c478bd9Sstevel@tonic-gate 					 */
18457c478bd9Sstevel@tonic-gate 					if (firstmatch == cur)
18467c478bd9Sstevel@tonic-gate 						firstmatch = NULL;
18477c478bd9Sstevel@tonic-gate 				}
18487c478bd9Sstevel@tonic-gate 			}
18497c478bd9Sstevel@tonic-gate 		}
18507c478bd9Sstevel@tonic-gate 	}
18517c478bd9Sstevel@tonic-gate 	if (firstmatch == NULL)
18527c478bd9Sstevel@tonic-gate 		return (Z_NO_RESOURCE_ID);
18537c478bd9Sstevel@tonic-gate 
18547c478bd9Sstevel@tonic-gate 	cur = firstmatch;
18557c478bd9Sstevel@tonic-gate 
18567c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_PHYSICAL, tabptr->zone_nwif_physical,
18577c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_nwif_physical))) != Z_OK)
18587c478bd9Sstevel@tonic-gate 		return (err);
18597c478bd9Sstevel@tonic-gate 
18607c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_ADDRESS, tabptr->zone_nwif_address,
18617c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_nwif_address))) != Z_OK)
18627c478bd9Sstevel@tonic-gate 		return (err);
18637c478bd9Sstevel@tonic-gate 
18647c478bd9Sstevel@tonic-gate 	return (Z_OK);
18657c478bd9Sstevel@tonic-gate }
18667c478bd9Sstevel@tonic-gate 
18677c478bd9Sstevel@tonic-gate static int
18687c478bd9Sstevel@tonic-gate zonecfg_add_nwif_core(zone_dochandle_t handle, struct zone_nwiftab *tabptr)
18697c478bd9Sstevel@tonic-gate {
18707c478bd9Sstevel@tonic-gate 	xmlNodePtr newnode, cur = handle->zone_dh_cur;
18717c478bd9Sstevel@tonic-gate 	int err;
18727c478bd9Sstevel@tonic-gate 
18737c478bd9Sstevel@tonic-gate 	newnode = xmlNewTextChild(cur, NULL, DTD_ELEM_NET, NULL);
1874a1be23daSdp 	if ((err = newprop(newnode, DTD_ATTR_ADDRESS,
18757c478bd9Sstevel@tonic-gate 	    tabptr->zone_nwif_address)) != Z_OK)
18767c478bd9Sstevel@tonic-gate 		return (err);
1877a1be23daSdp 	if ((err = newprop(newnode, DTD_ATTR_PHYSICAL,
18787c478bd9Sstevel@tonic-gate 	    tabptr->zone_nwif_physical)) != Z_OK)
18797c478bd9Sstevel@tonic-gate 		return (err);
18807c478bd9Sstevel@tonic-gate 	return (Z_OK);
18817c478bd9Sstevel@tonic-gate }
18827c478bd9Sstevel@tonic-gate 
18837c478bd9Sstevel@tonic-gate int
18847c478bd9Sstevel@tonic-gate zonecfg_add_nwif(zone_dochandle_t handle, struct zone_nwiftab *tabptr)
18857c478bd9Sstevel@tonic-gate {
18867c478bd9Sstevel@tonic-gate 	int err;
18877c478bd9Sstevel@tonic-gate 
18887c478bd9Sstevel@tonic-gate 	if (tabptr == NULL)
18897c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
18907c478bd9Sstevel@tonic-gate 
18917c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
18927c478bd9Sstevel@tonic-gate 		return (err);
18937c478bd9Sstevel@tonic-gate 
18947c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_add_nwif_core(handle, tabptr)) != Z_OK)
18957c478bd9Sstevel@tonic-gate 		return (err);
18967c478bd9Sstevel@tonic-gate 
18977c478bd9Sstevel@tonic-gate 	return (Z_OK);
18987c478bd9Sstevel@tonic-gate }
18997c478bd9Sstevel@tonic-gate 
19007c478bd9Sstevel@tonic-gate static int
19017c478bd9Sstevel@tonic-gate zonecfg_delete_nwif_core(zone_dochandle_t handle, struct zone_nwiftab *tabptr)
19027c478bd9Sstevel@tonic-gate {
19037c478bd9Sstevel@tonic-gate 	xmlNodePtr cur = handle->zone_dh_cur;
19047c478bd9Sstevel@tonic-gate 	boolean_t addr_match, phys_match;
19057c478bd9Sstevel@tonic-gate 
19067c478bd9Sstevel@tonic-gate 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
19077c478bd9Sstevel@tonic-gate 		if (xmlStrcmp(cur->name, DTD_ELEM_NET))
19087c478bd9Sstevel@tonic-gate 			continue;
19097c478bd9Sstevel@tonic-gate 
19107c478bd9Sstevel@tonic-gate 		addr_match = match_prop(cur, DTD_ATTR_ADDRESS,
19117c478bd9Sstevel@tonic-gate 		    tabptr->zone_nwif_address);
19127c478bd9Sstevel@tonic-gate 		phys_match = match_prop(cur, DTD_ATTR_PHYSICAL,
19137c478bd9Sstevel@tonic-gate 		    tabptr->zone_nwif_physical);
19147c478bd9Sstevel@tonic-gate 
19157c478bd9Sstevel@tonic-gate 		if (addr_match && phys_match) {
19167c478bd9Sstevel@tonic-gate 			xmlUnlinkNode(cur);
19177c478bd9Sstevel@tonic-gate 			xmlFreeNode(cur);
19187c478bd9Sstevel@tonic-gate 			return (Z_OK);
19197c478bd9Sstevel@tonic-gate 		}
19207c478bd9Sstevel@tonic-gate 	}
19217c478bd9Sstevel@tonic-gate 	return (Z_NO_RESOURCE_ID);
19227c478bd9Sstevel@tonic-gate }
19237c478bd9Sstevel@tonic-gate 
19247c478bd9Sstevel@tonic-gate int
19257c478bd9Sstevel@tonic-gate zonecfg_delete_nwif(zone_dochandle_t handle, struct zone_nwiftab *tabptr)
19267c478bd9Sstevel@tonic-gate {
19277c478bd9Sstevel@tonic-gate 	int err;
19287c478bd9Sstevel@tonic-gate 
19297c478bd9Sstevel@tonic-gate 	if (tabptr == NULL)
19307c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
19317c478bd9Sstevel@tonic-gate 
19327c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
19337c478bd9Sstevel@tonic-gate 		return (err);
19347c478bd9Sstevel@tonic-gate 
19357c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_delete_nwif_core(handle, tabptr)) != Z_OK)
19367c478bd9Sstevel@tonic-gate 		return (err);
19377c478bd9Sstevel@tonic-gate 
19387c478bd9Sstevel@tonic-gate 	return (Z_OK);
19397c478bd9Sstevel@tonic-gate }
19407c478bd9Sstevel@tonic-gate 
19417c478bd9Sstevel@tonic-gate int
19427c478bd9Sstevel@tonic-gate zonecfg_modify_nwif(
19437c478bd9Sstevel@tonic-gate 	zone_dochandle_t handle,
19447c478bd9Sstevel@tonic-gate 	struct zone_nwiftab *oldtabptr,
19457c478bd9Sstevel@tonic-gate 	struct zone_nwiftab *newtabptr)
19467c478bd9Sstevel@tonic-gate {
19477c478bd9Sstevel@tonic-gate 	int err;
19487c478bd9Sstevel@tonic-gate 
19497c478bd9Sstevel@tonic-gate 	if (oldtabptr == NULL || newtabptr == NULL)
19507c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
19517c478bd9Sstevel@tonic-gate 
19527c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
19537c478bd9Sstevel@tonic-gate 		return (err);
19547c478bd9Sstevel@tonic-gate 
19557c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_delete_nwif_core(handle, oldtabptr)) != Z_OK)
19567c478bd9Sstevel@tonic-gate 		return (err);
19577c478bd9Sstevel@tonic-gate 
19587c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_add_nwif_core(handle, newtabptr)) != Z_OK)
19597c478bd9Sstevel@tonic-gate 		return (err);
19607c478bd9Sstevel@tonic-gate 
19617c478bd9Sstevel@tonic-gate 	return (Z_OK);
19627c478bd9Sstevel@tonic-gate }
19637c478bd9Sstevel@tonic-gate 
19647c478bd9Sstevel@tonic-gate int
19657c478bd9Sstevel@tonic-gate zonecfg_lookup_dev(zone_dochandle_t handle, struct zone_devtab *tabptr)
19667c478bd9Sstevel@tonic-gate {
19677c478bd9Sstevel@tonic-gate 	xmlNodePtr cur, firstmatch;
19687c478bd9Sstevel@tonic-gate 	int err;
19697c478bd9Sstevel@tonic-gate 	char match[MAXPATHLEN];
19707c478bd9Sstevel@tonic-gate 
19717c478bd9Sstevel@tonic-gate 	if (tabptr == NULL)
19727c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
19737c478bd9Sstevel@tonic-gate 
19747c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
19757c478bd9Sstevel@tonic-gate 		return (err);
19767c478bd9Sstevel@tonic-gate 
19777c478bd9Sstevel@tonic-gate 	cur = handle->zone_dh_cur;
19787c478bd9Sstevel@tonic-gate 	firstmatch = NULL;
19797c478bd9Sstevel@tonic-gate 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
19807c478bd9Sstevel@tonic-gate 		if (xmlStrcmp(cur->name, DTD_ELEM_DEVICE))
19817c478bd9Sstevel@tonic-gate 			continue;
19827c478bd9Sstevel@tonic-gate 		if (strlen(tabptr->zone_dev_match) == 0)
19837c478bd9Sstevel@tonic-gate 			continue;
19847c478bd9Sstevel@tonic-gate 
19857c478bd9Sstevel@tonic-gate 		if ((fetchprop(cur, DTD_ATTR_MATCH, match,
19867c478bd9Sstevel@tonic-gate 		    sizeof (match)) == Z_OK)) {
19877c478bd9Sstevel@tonic-gate 			if (strcmp(tabptr->zone_dev_match,
19887c478bd9Sstevel@tonic-gate 			    match) == 0) {
19897c478bd9Sstevel@tonic-gate 				if (firstmatch == NULL)
19907c478bd9Sstevel@tonic-gate 					firstmatch = cur;
19917c478bd9Sstevel@tonic-gate 				else if (firstmatch != cur)
19927c478bd9Sstevel@tonic-gate 					return (Z_INSUFFICIENT_SPEC);
19937c478bd9Sstevel@tonic-gate 			} else {
19947c478bd9Sstevel@tonic-gate 				/*
19957c478bd9Sstevel@tonic-gate 				 * If another property matched but this
19967c478bd9Sstevel@tonic-gate 				 * one doesn't then reset firstmatch.
19977c478bd9Sstevel@tonic-gate 				 */
19987c478bd9Sstevel@tonic-gate 				if (firstmatch == cur)
19997c478bd9Sstevel@tonic-gate 					firstmatch = NULL;
20007c478bd9Sstevel@tonic-gate 			}
20017c478bd9Sstevel@tonic-gate 		}
20027c478bd9Sstevel@tonic-gate 	}
20037c478bd9Sstevel@tonic-gate 	if (firstmatch == NULL)
20047c478bd9Sstevel@tonic-gate 		return (Z_NO_RESOURCE_ID);
20057c478bd9Sstevel@tonic-gate 
20067c478bd9Sstevel@tonic-gate 	cur = firstmatch;
20077c478bd9Sstevel@tonic-gate 
20087c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_MATCH, tabptr->zone_dev_match,
20097c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_dev_match))) != Z_OK)
20107c478bd9Sstevel@tonic-gate 		return (err);
20117c478bd9Sstevel@tonic-gate 
20127c478bd9Sstevel@tonic-gate 	return (Z_OK);
20137c478bd9Sstevel@tonic-gate }
20147c478bd9Sstevel@tonic-gate 
20157c478bd9Sstevel@tonic-gate static int
20167c478bd9Sstevel@tonic-gate zonecfg_add_dev_core(zone_dochandle_t handle, struct zone_devtab *tabptr)
20177c478bd9Sstevel@tonic-gate {
20187c478bd9Sstevel@tonic-gate 	xmlNodePtr newnode, cur = handle->zone_dh_cur;
20197c478bd9Sstevel@tonic-gate 	int err;
20207c478bd9Sstevel@tonic-gate 
20217c478bd9Sstevel@tonic-gate 	newnode = xmlNewTextChild(cur, NULL, DTD_ELEM_DEVICE, NULL);
20227c478bd9Sstevel@tonic-gate 
2023a1be23daSdp 	if ((err = newprop(newnode, DTD_ATTR_MATCH,
20247c478bd9Sstevel@tonic-gate 	    tabptr->zone_dev_match)) != Z_OK)
20257c478bd9Sstevel@tonic-gate 		return (err);
20267c478bd9Sstevel@tonic-gate 
20277c478bd9Sstevel@tonic-gate 	return (Z_OK);
20287c478bd9Sstevel@tonic-gate }
20297c478bd9Sstevel@tonic-gate 
20307c478bd9Sstevel@tonic-gate int
20317c478bd9Sstevel@tonic-gate zonecfg_add_dev(zone_dochandle_t handle, struct zone_devtab *tabptr)
20327c478bd9Sstevel@tonic-gate {
20337c478bd9Sstevel@tonic-gate 	int err;
20347c478bd9Sstevel@tonic-gate 
20357c478bd9Sstevel@tonic-gate 	if (tabptr == NULL)
20367c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
20377c478bd9Sstevel@tonic-gate 
20387c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
20397c478bd9Sstevel@tonic-gate 		return (err);
20407c478bd9Sstevel@tonic-gate 
20417c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_add_dev_core(handle, tabptr)) != Z_OK)
20427c478bd9Sstevel@tonic-gate 		return (err);
20437c478bd9Sstevel@tonic-gate 
20447c478bd9Sstevel@tonic-gate 	return (Z_OK);
20457c478bd9Sstevel@tonic-gate }
20467c478bd9Sstevel@tonic-gate 
20477c478bd9Sstevel@tonic-gate static int
20487c478bd9Sstevel@tonic-gate zonecfg_delete_dev_core(zone_dochandle_t handle, struct zone_devtab *tabptr)
20497c478bd9Sstevel@tonic-gate {
20507c478bd9Sstevel@tonic-gate 	xmlNodePtr cur = handle->zone_dh_cur;
20517c478bd9Sstevel@tonic-gate 	int match_match;
20527c478bd9Sstevel@tonic-gate 
20537c478bd9Sstevel@tonic-gate 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
20547c478bd9Sstevel@tonic-gate 		if (xmlStrcmp(cur->name, DTD_ELEM_DEVICE))
20557c478bd9Sstevel@tonic-gate 			continue;
20567c478bd9Sstevel@tonic-gate 
20577c478bd9Sstevel@tonic-gate 		match_match = match_prop(cur, DTD_ATTR_MATCH,
20587c478bd9Sstevel@tonic-gate 		    tabptr->zone_dev_match);
20597c478bd9Sstevel@tonic-gate 
20607c478bd9Sstevel@tonic-gate 		if (match_match) {
20617c478bd9Sstevel@tonic-gate 			xmlUnlinkNode(cur);
20627c478bd9Sstevel@tonic-gate 			xmlFreeNode(cur);
20637c478bd9Sstevel@tonic-gate 			return (Z_OK);
20647c478bd9Sstevel@tonic-gate 		}
20657c478bd9Sstevel@tonic-gate 	}
20667c478bd9Sstevel@tonic-gate 	return (Z_NO_RESOURCE_ID);
20677c478bd9Sstevel@tonic-gate }
20687c478bd9Sstevel@tonic-gate 
20697c478bd9Sstevel@tonic-gate int
20707c478bd9Sstevel@tonic-gate zonecfg_delete_dev(zone_dochandle_t handle, struct zone_devtab *tabptr)
20717c478bd9Sstevel@tonic-gate {
20727c478bd9Sstevel@tonic-gate 	int err;
20737c478bd9Sstevel@tonic-gate 
20747c478bd9Sstevel@tonic-gate 	if (tabptr == NULL)
20757c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
20767c478bd9Sstevel@tonic-gate 
20777c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
20787c478bd9Sstevel@tonic-gate 		return (err);
20797c478bd9Sstevel@tonic-gate 
20807c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_delete_dev_core(handle, tabptr)) != Z_OK)
20817c478bd9Sstevel@tonic-gate 		return (err);
20827c478bd9Sstevel@tonic-gate 
20837c478bd9Sstevel@tonic-gate 	return (Z_OK);
20847c478bd9Sstevel@tonic-gate }
20857c478bd9Sstevel@tonic-gate 
20867c478bd9Sstevel@tonic-gate int
20877c478bd9Sstevel@tonic-gate zonecfg_modify_dev(
20887c478bd9Sstevel@tonic-gate 	zone_dochandle_t handle,
20897c478bd9Sstevel@tonic-gate 	struct zone_devtab *oldtabptr,
20907c478bd9Sstevel@tonic-gate 	struct zone_devtab *newtabptr)
20917c478bd9Sstevel@tonic-gate {
20927c478bd9Sstevel@tonic-gate 	int err;
20937c478bd9Sstevel@tonic-gate 
20947c478bd9Sstevel@tonic-gate 	if (oldtabptr == NULL || newtabptr == NULL)
20957c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
20967c478bd9Sstevel@tonic-gate 
20977c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
20987c478bd9Sstevel@tonic-gate 		return (err);
20997c478bd9Sstevel@tonic-gate 
21007c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_delete_dev_core(handle, oldtabptr)) != Z_OK)
21017c478bd9Sstevel@tonic-gate 		return (err);
21027c478bd9Sstevel@tonic-gate 
21037c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_add_dev_core(handle, newtabptr)) != Z_OK)
21047c478bd9Sstevel@tonic-gate 		return (err);
21057c478bd9Sstevel@tonic-gate 
21067c478bd9Sstevel@tonic-gate 	return (Z_OK);
21077c478bd9Sstevel@tonic-gate }
21087c478bd9Sstevel@tonic-gate 
2109ee519a1fSgjelinek /* Lock to serialize all zonecfg_devwalks */
2110ee519a1fSgjelinek static pthread_mutex_t zonecfg_devwalk_lock = PTHREAD_MUTEX_INITIALIZER;
2111ee519a1fSgjelinek /*
2112ee519a1fSgjelinek  * Global variables used to pass data from zonecfg_devwalk to the nftw
2113ee519a1fSgjelinek  * call-back (zonecfg_devwalk_cb).  g_devwalk_data is really the void*
2114ee519a1fSgjelinek  * parameter and g_devwalk_cb is really the *cb parameter from zonecfg_devwalk.
2115ee519a1fSgjelinek  */
2116ee519a1fSgjelinek static void *g_devwalk_data;
2117ee519a1fSgjelinek static int (*g_devwalk_cb)(const char *, uid_t, gid_t, mode_t, const char *,
2118ee519a1fSgjelinek     void *);
2119ee519a1fSgjelinek static size_t g_devwalk_skip_prefix;
2120ee519a1fSgjelinek 
2121ee519a1fSgjelinek /*
2122ee519a1fSgjelinek  * This is the nftw call-back function used by zonecfg_devwalk.  It is
2123ee519a1fSgjelinek  * responsible for calling the actual call-back that is passed in to
2124ee519a1fSgjelinek  * zonecfg_devwalk as the *cb argument.
2125ee519a1fSgjelinek  */
2126ee519a1fSgjelinek /* ARGSUSED2 */
2127ee519a1fSgjelinek static int
2128ee519a1fSgjelinek zonecfg_devwalk_cb(const char *path, const struct stat *st, int f,
2129ee519a1fSgjelinek     struct FTW *ftw)
2130ee519a1fSgjelinek {
2131ee519a1fSgjelinek 	acl_t *acl;
2132ee519a1fSgjelinek 	char *acl_txt = NULL;
2133ee519a1fSgjelinek 
2134ee519a1fSgjelinek 	/* skip all but character and block devices */
2135ee519a1fSgjelinek 	if (!S_ISBLK(st->st_mode) && !S_ISCHR(st->st_mode))
2136ee519a1fSgjelinek 		return (0);
2137ee519a1fSgjelinek 
2138ee519a1fSgjelinek 	if ((acl_get(path, ACL_NO_TRIVIAL, &acl) == 0) &&
2139ee519a1fSgjelinek 	    acl != NULL) {
2140ee519a1fSgjelinek 		acl_txt = acl_totext(acl, ACL_NORESOLVE);
2141ee519a1fSgjelinek 		acl_free(acl);
2142ee519a1fSgjelinek 	}
2143ee519a1fSgjelinek 
2144ee519a1fSgjelinek 	if (strlen(path) <= g_devwalk_skip_prefix)
2145ee519a1fSgjelinek 		return (0);
2146ee519a1fSgjelinek 
2147ee519a1fSgjelinek 	g_devwalk_cb(path + g_devwalk_skip_prefix, st->st_uid, st->st_gid,
2148ee519a1fSgjelinek 	    st->st_mode & S_IAMB, acl_txt != NULL ? acl_txt : "",
2149ee519a1fSgjelinek 	    g_devwalk_data);
2150ee519a1fSgjelinek 	free(acl_txt);
2151ee519a1fSgjelinek 	return (0);
2152ee519a1fSgjelinek }
2153ee519a1fSgjelinek 
2154ee519a1fSgjelinek /*
2155ee519a1fSgjelinek  * Walk the dev tree for the zone specified by hdl and call the call-back (cb)
2156ee519a1fSgjelinek  * function for each entry in the tree.  The call-back will be passed the
2157ee519a1fSgjelinek  * name, uid, gid, mode, acl string and the void *data input parameter
2158ee519a1fSgjelinek  * for each dev entry.
2159ee519a1fSgjelinek  *
2160ee519a1fSgjelinek  * Data is passed to the zonecfg_devwalk_cb through the global variables
2161ee519a1fSgjelinek  * g_devwalk_data, *g_devwalk_cb, and g_devwalk_skip_prefix.  The
2162ee519a1fSgjelinek  * zonecfg_devwalk_cb function will actually call *cb.
2163ee519a1fSgjelinek  */
2164ee519a1fSgjelinek int
2165ee519a1fSgjelinek zonecfg_devwalk(zone_dochandle_t hdl,
2166ee519a1fSgjelinek     int (*cb)(const char *, uid_t, gid_t, mode_t, const char *, void *),
2167ee519a1fSgjelinek     void *data)
2168ee519a1fSgjelinek {
2169ee519a1fSgjelinek 	char path[MAXPATHLEN];
2170ee519a1fSgjelinek 	int ret;
2171ee519a1fSgjelinek 
2172ee519a1fSgjelinek 	if ((ret = zonecfg_get_zonepath(hdl, path, sizeof (path))) != Z_OK)
2173ee519a1fSgjelinek 		return (ret);
2174ee519a1fSgjelinek 
2175ee519a1fSgjelinek 	if (strlcat(path, "/dev", sizeof (path)) >= sizeof (path))
2176ee519a1fSgjelinek 		return (Z_TOO_BIG);
2177ee519a1fSgjelinek 	g_devwalk_skip_prefix = strlen(path) + 1;
2178ee519a1fSgjelinek 
2179ee519a1fSgjelinek 	/*
2180ee519a1fSgjelinek 	 * We have to serialize all zonecfg_devwalks in the same process
2181ee519a1fSgjelinek 	 * (which should be fine), since nftw() is so badly designed.
2182ee519a1fSgjelinek 	 */
2183ee519a1fSgjelinek 	(void) pthread_mutex_lock(&zonecfg_devwalk_lock);
2184ee519a1fSgjelinek 
2185ee519a1fSgjelinek 	g_devwalk_data = data;
2186ee519a1fSgjelinek 	g_devwalk_cb = cb;
2187ee519a1fSgjelinek 	(void) nftw(path, zonecfg_devwalk_cb, 0, FTW_PHYS);
2188ee519a1fSgjelinek 
2189ee519a1fSgjelinek 	(void) pthread_mutex_unlock(&zonecfg_devwalk_lock);
2190ee519a1fSgjelinek 	return (Z_OK);
2191ee519a1fSgjelinek }
2192ee519a1fSgjelinek 
2193ee519a1fSgjelinek /*
2194ee519a1fSgjelinek  * Update the owner, group, mode and acl on the specified dev (inpath) for
2195ee519a1fSgjelinek  * the zone (hdl).  This function can be used to fix up the dev tree after
2196ee519a1fSgjelinek  * attaching a migrated zone.
2197ee519a1fSgjelinek  */
2198ee519a1fSgjelinek int
2199ee519a1fSgjelinek zonecfg_devperms_apply(zone_dochandle_t hdl, const char *inpath, uid_t owner,
2200ee519a1fSgjelinek     gid_t group, mode_t mode, const char *acltxt)
2201ee519a1fSgjelinek {
2202ee519a1fSgjelinek 	int ret;
2203ee519a1fSgjelinek 	char path[MAXPATHLEN];
2204ee519a1fSgjelinek 	struct stat st;
2205ee519a1fSgjelinek 	acl_t *aclp;
2206ee519a1fSgjelinek 
2207ee519a1fSgjelinek 	if ((ret = zonecfg_get_zonepath(hdl, path, sizeof (path))) != Z_OK)
2208ee519a1fSgjelinek 		return (ret);
2209ee519a1fSgjelinek 
2210ee519a1fSgjelinek 	if (strlcat(path, "/dev/", sizeof (path)) >= sizeof (path))
2211ee519a1fSgjelinek 		return (Z_TOO_BIG);
2212ee519a1fSgjelinek 	if (strlcat(path, inpath, sizeof (path)) >= sizeof (path))
2213ee519a1fSgjelinek 		return (Z_TOO_BIG);
2214ee519a1fSgjelinek 
2215ee519a1fSgjelinek 	if (stat(path, &st) == -1)
2216ee519a1fSgjelinek 		return (Z_INVAL);
2217ee519a1fSgjelinek 
2218ee519a1fSgjelinek 	/* make sure we're only touching device nodes */
2219ee519a1fSgjelinek 	if (!S_ISCHR(st.st_mode) && !S_ISBLK(st.st_mode))
2220ee519a1fSgjelinek 		return (Z_INVAL);
2221ee519a1fSgjelinek 
2222ee519a1fSgjelinek 	if (chown(path, owner, group) == -1)
2223ee519a1fSgjelinek 		return (Z_SYSTEM);
2224ee519a1fSgjelinek 
2225ee519a1fSgjelinek 	if (chmod(path, mode) == -1)
2226ee519a1fSgjelinek 		return (Z_SYSTEM);
2227ee519a1fSgjelinek 
2228ee519a1fSgjelinek 	if ((acltxt == NULL) || (strcmp(acltxt, "") == 0))
2229ee519a1fSgjelinek 		return (Z_OK);
2230ee519a1fSgjelinek 
2231ee519a1fSgjelinek 	if (acl_fromtext(acltxt, &aclp) != 0)
2232ee519a1fSgjelinek 		return (Z_SYSTEM);
2233ee519a1fSgjelinek 
2234ee519a1fSgjelinek 	errno = 0;
2235ee519a1fSgjelinek 	if (acl_set(path, aclp) == -1) {
2236ee519a1fSgjelinek 		free(aclp);
2237ee519a1fSgjelinek 		return (Z_SYSTEM);
2238ee519a1fSgjelinek 	}
2239ee519a1fSgjelinek 
2240ee519a1fSgjelinek 	free(aclp);
2241ee519a1fSgjelinek 	return (Z_OK);
2242ee519a1fSgjelinek }
2243ee519a1fSgjelinek 
22447c478bd9Sstevel@tonic-gate /*
22457c478bd9Sstevel@tonic-gate  * This is the set of devices which must be present in every zone.  Users
22467c478bd9Sstevel@tonic-gate  * can augment this list with additional device rules in their zone
22477c478bd9Sstevel@tonic-gate  * configuration, but at present cannot remove any of the this set of
22487c478bd9Sstevel@tonic-gate  * standard devices.  All matching is done by /dev pathname (the "/dev"
22497c478bd9Sstevel@tonic-gate  * part is implicit.  Try to keep rules which match a large number of
22507c478bd9Sstevel@tonic-gate  * devices (like the pts rule) first.
22517c478bd9Sstevel@tonic-gate  */
22527c478bd9Sstevel@tonic-gate static const char *standard_devs[] = {
22537c478bd9Sstevel@tonic-gate 	"pts/*",
22547c478bd9Sstevel@tonic-gate 	"ptmx",
22557c478bd9Sstevel@tonic-gate 	"random",
22567c478bd9Sstevel@tonic-gate 	"urandom",
22577c478bd9Sstevel@tonic-gate 	"poll",
22587c478bd9Sstevel@tonic-gate 	"pool",
22597c478bd9Sstevel@tonic-gate 	"kstat",
22607c478bd9Sstevel@tonic-gate 	"zero",
22617c478bd9Sstevel@tonic-gate 	"null",
22627c478bd9Sstevel@tonic-gate 	"crypto",
22637c478bd9Sstevel@tonic-gate 	"cryptoadm",
22647c478bd9Sstevel@tonic-gate 	"ticots",
22657c478bd9Sstevel@tonic-gate 	"ticotsord",
22667c478bd9Sstevel@tonic-gate 	"ticlts",
22677c478bd9Sstevel@tonic-gate 	"lo0",
22687c478bd9Sstevel@tonic-gate 	"lo1",
22697c478bd9Sstevel@tonic-gate 	"lo2",
22707c478bd9Sstevel@tonic-gate 	"lo3",
22717c478bd9Sstevel@tonic-gate 	"sad/user",
22727c478bd9Sstevel@tonic-gate 	"tty",
22737c478bd9Sstevel@tonic-gate 	"logindmux",
22747c478bd9Sstevel@tonic-gate 	"log",
22757c478bd9Sstevel@tonic-gate 	"conslog",
22767c478bd9Sstevel@tonic-gate 	"arp",
22777c478bd9Sstevel@tonic-gate 	"tcp",
22787c478bd9Sstevel@tonic-gate 	"tcp6",
22797c478bd9Sstevel@tonic-gate 	"udp",
22807c478bd9Sstevel@tonic-gate 	"udp6",
22817c478bd9Sstevel@tonic-gate 	"sysevent",
22827c478bd9Sstevel@tonic-gate #ifdef __sparc
22837c478bd9Sstevel@tonic-gate 	"openprom",
22847c478bd9Sstevel@tonic-gate #endif
22857c478bd9Sstevel@tonic-gate 	"cpu/self/cpuid",
2286*ad4023c4Sdp 	"dtrace/*",
2287*ad4023c4Sdp 	"dtrace/provider/*",
2288fa9e4066Sahrens 	"zfs",
22897c478bd9Sstevel@tonic-gate 	NULL
22907c478bd9Sstevel@tonic-gate };
22917c478bd9Sstevel@tonic-gate 
22927c478bd9Sstevel@tonic-gate /*
22937c478bd9Sstevel@tonic-gate  * This function finds everything mounted under a zone's rootpath.
22947c478bd9Sstevel@tonic-gate  * This returns the number of mounts under rootpath, or -1 on error.
22957c478bd9Sstevel@tonic-gate  * callback is called once per mount found with the first argument
22967c478bd9Sstevel@tonic-gate  * pointing to the  mount point.
22977c478bd9Sstevel@tonic-gate  *
22987c478bd9Sstevel@tonic-gate  * If the callback function returns non-zero zonecfg_find_mounts
22997c478bd9Sstevel@tonic-gate  * aborts with an error.
23007c478bd9Sstevel@tonic-gate  */
23017c478bd9Sstevel@tonic-gate 
23027c478bd9Sstevel@tonic-gate int
23037c478bd9Sstevel@tonic-gate zonecfg_find_mounts(char *rootpath, int (*callback)(const char *, void *),
23047c478bd9Sstevel@tonic-gate     void *priv) {
23057c478bd9Sstevel@tonic-gate 	FILE *mnttab;
23067c478bd9Sstevel@tonic-gate 	struct mnttab m;
23077c478bd9Sstevel@tonic-gate 	size_t l;
230807b574eeSgjelinek 	int zfsl;
23097c478bd9Sstevel@tonic-gate 	int rv = 0;
231007b574eeSgjelinek 	char zfs_path[MAXPATHLEN];
23117c478bd9Sstevel@tonic-gate 
23127c478bd9Sstevel@tonic-gate 	assert(rootpath != NULL);
23137c478bd9Sstevel@tonic-gate 
231407b574eeSgjelinek 	if ((zfsl = snprintf(zfs_path, sizeof (zfs_path), "%s/.zfs/", rootpath))
231507b574eeSgjelinek 	    >= sizeof (zfs_path))
231607b574eeSgjelinek 		return (-1);
231707b574eeSgjelinek 
23187c478bd9Sstevel@tonic-gate 	l = strlen(rootpath);
23197c478bd9Sstevel@tonic-gate 
23207c478bd9Sstevel@tonic-gate 	mnttab = fopen("/etc/mnttab", "r");
23217c478bd9Sstevel@tonic-gate 
23227c478bd9Sstevel@tonic-gate 	if (mnttab == NULL)
23237c478bd9Sstevel@tonic-gate 		return (-1);
23247c478bd9Sstevel@tonic-gate 
23257c478bd9Sstevel@tonic-gate 	if (ioctl(fileno(mnttab), MNTIOC_SHOWHIDDEN, NULL) < 0)  {
23267c478bd9Sstevel@tonic-gate 		rv = -1;
23277c478bd9Sstevel@tonic-gate 		goto out;
23287c478bd9Sstevel@tonic-gate 	}
23297c478bd9Sstevel@tonic-gate 
23307c478bd9Sstevel@tonic-gate 	while (!getmntent(mnttab, &m)) {
23317c478bd9Sstevel@tonic-gate 		if ((strncmp(rootpath, m.mnt_mountp, l) == 0) &&
233207b574eeSgjelinek 		    (m.mnt_mountp[l] == '/') &&
233307b574eeSgjelinek 		    (strncmp(zfs_path, m.mnt_mountp, zfsl) != 0)) {
23347c478bd9Sstevel@tonic-gate 			rv++;
23357c478bd9Sstevel@tonic-gate 			if (callback == NULL)
23367c478bd9Sstevel@tonic-gate 				continue;
23377c478bd9Sstevel@tonic-gate 			if (callback(m.mnt_mountp, priv)) {
23387c478bd9Sstevel@tonic-gate 				rv = -1;
23397c478bd9Sstevel@tonic-gate 				goto out;
23407c478bd9Sstevel@tonic-gate 
23417c478bd9Sstevel@tonic-gate 			}
23427c478bd9Sstevel@tonic-gate 		}
23437c478bd9Sstevel@tonic-gate 	}
23447c478bd9Sstevel@tonic-gate 
23457c478bd9Sstevel@tonic-gate out:
23467c478bd9Sstevel@tonic-gate 	(void) fclose(mnttab);
23477c478bd9Sstevel@tonic-gate 	return (rv);
23487c478bd9Sstevel@tonic-gate }
23497c478bd9Sstevel@tonic-gate 
23507c478bd9Sstevel@tonic-gate /*
23517c478bd9Sstevel@tonic-gate  * This routine is used to determine if a given device should appear in the
23527c478bd9Sstevel@tonic-gate  * zone represented by 'handle'.  First it consults the list of "standard"
23537c478bd9Sstevel@tonic-gate  * zone devices.  Then it scans the user-supplied device entries.
23547c478bd9Sstevel@tonic-gate  */
23557c478bd9Sstevel@tonic-gate int
23567c478bd9Sstevel@tonic-gate zonecfg_match_dev(zone_dochandle_t handle, char *devpath,
23577c478bd9Sstevel@tonic-gate     struct zone_devtab *out_match)
23587c478bd9Sstevel@tonic-gate {
23597c478bd9Sstevel@tonic-gate 	int err;
23607c478bd9Sstevel@tonic-gate 	boolean_t found = B_FALSE;
23617c478bd9Sstevel@tonic-gate 	char match[MAXPATHLEN];
23627c478bd9Sstevel@tonic-gate 	const char **stdmatch;
23637c478bd9Sstevel@tonic-gate 	xmlNodePtr cur;
23647c478bd9Sstevel@tonic-gate 
23657c478bd9Sstevel@tonic-gate 	if (handle == NULL || devpath == NULL)
23667c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
23677c478bd9Sstevel@tonic-gate 
23687c478bd9Sstevel@tonic-gate 	/*
23697c478bd9Sstevel@tonic-gate 	 * Check the "standard" devices which we require to be present.
23707c478bd9Sstevel@tonic-gate 	 */
23717c478bd9Sstevel@tonic-gate 	for (stdmatch = &standard_devs[0]; *stdmatch != NULL; stdmatch++) {
23727c478bd9Sstevel@tonic-gate 		/*
23737c478bd9Sstevel@tonic-gate 		 * fnmatch gives us simple but powerful shell-style matching.
23747c478bd9Sstevel@tonic-gate 		 */
23757c478bd9Sstevel@tonic-gate 		if (fnmatch(*stdmatch, devpath, FNM_PATHNAME) == 0) {
23767c478bd9Sstevel@tonic-gate 			if (!out_match)
23777c478bd9Sstevel@tonic-gate 				return (Z_OK);
23787c478bd9Sstevel@tonic-gate 			(void) snprintf(out_match->zone_dev_match,
23797c478bd9Sstevel@tonic-gate 			    sizeof (out_match->zone_dev_match),
23807c478bd9Sstevel@tonic-gate 			    "/dev/%s", *stdmatch);
23817c478bd9Sstevel@tonic-gate 			return (Z_OK);
23827c478bd9Sstevel@tonic-gate 		}
23837c478bd9Sstevel@tonic-gate 	}
23847c478bd9Sstevel@tonic-gate 
23857c478bd9Sstevel@tonic-gate 	/*
23867c478bd9Sstevel@tonic-gate 	 * We got no hits in the set of standard devices.  On to the user
23877c478bd9Sstevel@tonic-gate 	 * supplied ones.
23887c478bd9Sstevel@tonic-gate 	 */
23897c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK) {
23907c478bd9Sstevel@tonic-gate 		handle->zone_dh_cur = NULL;
23917c478bd9Sstevel@tonic-gate 		return (err);
23927c478bd9Sstevel@tonic-gate 	}
23937c478bd9Sstevel@tonic-gate 
23947c478bd9Sstevel@tonic-gate 	cur = handle->zone_dh_cur;
23957c478bd9Sstevel@tonic-gate 	cur = cur->xmlChildrenNode;
23967c478bd9Sstevel@tonic-gate 	if (cur == NULL)
23977c478bd9Sstevel@tonic-gate 		return (Z_NO_ENTRY);
23987c478bd9Sstevel@tonic-gate 	handle->zone_dh_cur = cur;
23997c478bd9Sstevel@tonic-gate 
24007c478bd9Sstevel@tonic-gate 	for (; cur != NULL; cur = cur->next) {
24017c478bd9Sstevel@tonic-gate 		char *m;
24027c478bd9Sstevel@tonic-gate 		if (xmlStrcmp(cur->name, DTD_ELEM_DEVICE) != 0)
24037c478bd9Sstevel@tonic-gate 			continue;
24047c478bd9Sstevel@tonic-gate 		if ((err = fetchprop(cur, DTD_ATTR_MATCH, match,
24057c478bd9Sstevel@tonic-gate 		    sizeof (match))) != Z_OK) {
24067c478bd9Sstevel@tonic-gate 			handle->zone_dh_cur = handle->zone_dh_top;
24077c478bd9Sstevel@tonic-gate 			return (err);
24087c478bd9Sstevel@tonic-gate 		}
24097c478bd9Sstevel@tonic-gate 		m = match;
24107c478bd9Sstevel@tonic-gate 		/*
24117c478bd9Sstevel@tonic-gate 		 * fnmatch gives us simple but powerful shell-style matching;
24127c478bd9Sstevel@tonic-gate 		 * but first, we need to strip out /dev/ from the matching rule.
24137c478bd9Sstevel@tonic-gate 		 */
24147c478bd9Sstevel@tonic-gate 		if (strncmp(m, "/dev/", 5) == 0)
24157c478bd9Sstevel@tonic-gate 			m += 5;
24167c478bd9Sstevel@tonic-gate 
24177c478bd9Sstevel@tonic-gate 		if (fnmatch(m, devpath, FNM_PATHNAME) == 0) {
24187c478bd9Sstevel@tonic-gate 			found = B_TRUE;
24197c478bd9Sstevel@tonic-gate 			break;
24207c478bd9Sstevel@tonic-gate 		}
24217c478bd9Sstevel@tonic-gate 	}
24227c478bd9Sstevel@tonic-gate 
24237c478bd9Sstevel@tonic-gate 	if (!found)
24247c478bd9Sstevel@tonic-gate 		return (Z_NO_ENTRY);
24257c478bd9Sstevel@tonic-gate 
24267c478bd9Sstevel@tonic-gate 	if (!out_match)
24277c478bd9Sstevel@tonic-gate 		return (Z_OK);
24287c478bd9Sstevel@tonic-gate 
24297c478bd9Sstevel@tonic-gate 	(void) strlcpy(out_match->zone_dev_match, match,
24307c478bd9Sstevel@tonic-gate 	    sizeof (out_match->zone_dev_match));
24317c478bd9Sstevel@tonic-gate 	return (Z_OK);
24327c478bd9Sstevel@tonic-gate }
24337c478bd9Sstevel@tonic-gate 
24347c478bd9Sstevel@tonic-gate int
24357c478bd9Sstevel@tonic-gate zonecfg_lookup_attr(zone_dochandle_t handle, struct zone_attrtab *tabptr)
24367c478bd9Sstevel@tonic-gate {
24377c478bd9Sstevel@tonic-gate 	xmlNodePtr cur, firstmatch;
24387c478bd9Sstevel@tonic-gate 	int err;
24397c478bd9Sstevel@tonic-gate 	char name[MAXNAMELEN], type[MAXNAMELEN], value[MAXNAMELEN];
24407c478bd9Sstevel@tonic-gate 
24417c478bd9Sstevel@tonic-gate 	if (tabptr == NULL)
24427c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
24437c478bd9Sstevel@tonic-gate 
24447c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
24457c478bd9Sstevel@tonic-gate 		return (err);
24467c478bd9Sstevel@tonic-gate 
24477c478bd9Sstevel@tonic-gate 	cur = handle->zone_dh_cur;
24487c478bd9Sstevel@tonic-gate 	firstmatch = NULL;
24497c478bd9Sstevel@tonic-gate 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
24507c478bd9Sstevel@tonic-gate 		if (xmlStrcmp(cur->name, DTD_ELEM_ATTR))
24517c478bd9Sstevel@tonic-gate 			continue;
24527c478bd9Sstevel@tonic-gate 		if (strlen(tabptr->zone_attr_name) > 0) {
24537c478bd9Sstevel@tonic-gate 			if ((fetchprop(cur, DTD_ATTR_NAME, name,
24547c478bd9Sstevel@tonic-gate 			    sizeof (name)) == Z_OK) &&
24557c478bd9Sstevel@tonic-gate 			    (strcmp(tabptr->zone_attr_name, name) == 0)) {
24567c478bd9Sstevel@tonic-gate 				if (firstmatch == NULL)
24577c478bd9Sstevel@tonic-gate 					firstmatch = cur;
24587c478bd9Sstevel@tonic-gate 				else
24597c478bd9Sstevel@tonic-gate 					return (Z_INSUFFICIENT_SPEC);
24607c478bd9Sstevel@tonic-gate 			}
24617c478bd9Sstevel@tonic-gate 		}
24627c478bd9Sstevel@tonic-gate 		if (strlen(tabptr->zone_attr_type) > 0) {
24637c478bd9Sstevel@tonic-gate 			if ((fetchprop(cur, DTD_ATTR_TYPE, type,
24647c478bd9Sstevel@tonic-gate 			    sizeof (type)) == Z_OK)) {
24657c478bd9Sstevel@tonic-gate 				if (strcmp(tabptr->zone_attr_type, type) == 0) {
24667c478bd9Sstevel@tonic-gate 					if (firstmatch == NULL)
24677c478bd9Sstevel@tonic-gate 						firstmatch = cur;
24687c478bd9Sstevel@tonic-gate 					else if (firstmatch != cur)
24697c478bd9Sstevel@tonic-gate 						return (Z_INSUFFICIENT_SPEC);
24707c478bd9Sstevel@tonic-gate 				} else {
24717c478bd9Sstevel@tonic-gate 					/*
24727c478bd9Sstevel@tonic-gate 					 * If another property matched but this
24737c478bd9Sstevel@tonic-gate 					 * one doesn't then reset firstmatch.
24747c478bd9Sstevel@tonic-gate 					 */
24757c478bd9Sstevel@tonic-gate 					if (firstmatch == cur)
24767c478bd9Sstevel@tonic-gate 						firstmatch = NULL;
24777c478bd9Sstevel@tonic-gate 				}
24787c478bd9Sstevel@tonic-gate 			}
24797c478bd9Sstevel@tonic-gate 		}
24807c478bd9Sstevel@tonic-gate 		if (strlen(tabptr->zone_attr_value) > 0) {
24817c478bd9Sstevel@tonic-gate 			if ((fetchprop(cur, DTD_ATTR_VALUE, value,
24827c478bd9Sstevel@tonic-gate 			    sizeof (value)) == Z_OK)) {
24837c478bd9Sstevel@tonic-gate 				if (strcmp(tabptr->zone_attr_value, value) ==
24847c478bd9Sstevel@tonic-gate 				    0) {
24857c478bd9Sstevel@tonic-gate 					if (firstmatch == NULL)
24867c478bd9Sstevel@tonic-gate 						firstmatch = cur;
24877c478bd9Sstevel@tonic-gate 					else if (firstmatch != cur)
24887c478bd9Sstevel@tonic-gate 						return (Z_INSUFFICIENT_SPEC);
24897c478bd9Sstevel@tonic-gate 				} else {
24907c478bd9Sstevel@tonic-gate 					/*
24917c478bd9Sstevel@tonic-gate 					 * If another property matched but this
24927c478bd9Sstevel@tonic-gate 					 * one doesn't then reset firstmatch.
24937c478bd9Sstevel@tonic-gate 					 */
24947c478bd9Sstevel@tonic-gate 					if (firstmatch == cur)
24957c478bd9Sstevel@tonic-gate 						firstmatch = NULL;
24967c478bd9Sstevel@tonic-gate 				}
24977c478bd9Sstevel@tonic-gate 			}
24987c478bd9Sstevel@tonic-gate 		}
24997c478bd9Sstevel@tonic-gate 	}
25007c478bd9Sstevel@tonic-gate 	if (firstmatch == NULL)
25017c478bd9Sstevel@tonic-gate 		return (Z_NO_RESOURCE_ID);
25027c478bd9Sstevel@tonic-gate 
25037c478bd9Sstevel@tonic-gate 	cur = firstmatch;
25047c478bd9Sstevel@tonic-gate 
25057c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_NAME, tabptr->zone_attr_name,
25067c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_attr_name))) != Z_OK)
25077c478bd9Sstevel@tonic-gate 		return (err);
25087c478bd9Sstevel@tonic-gate 
25097c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_TYPE, tabptr->zone_attr_type,
25107c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_attr_type))) != Z_OK)
25117c478bd9Sstevel@tonic-gate 		return (err);
25127c478bd9Sstevel@tonic-gate 
25137c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_VALUE, tabptr->zone_attr_value,
25147c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_attr_value))) != Z_OK)
25157c478bd9Sstevel@tonic-gate 		return (err);
25167c478bd9Sstevel@tonic-gate 
25177c478bd9Sstevel@tonic-gate 	return (Z_OK);
25187c478bd9Sstevel@tonic-gate }
25197c478bd9Sstevel@tonic-gate 
25207c478bd9Sstevel@tonic-gate static int
25217c478bd9Sstevel@tonic-gate zonecfg_add_attr_core(zone_dochandle_t handle, struct zone_attrtab *tabptr)
25227c478bd9Sstevel@tonic-gate {
25237c478bd9Sstevel@tonic-gate 	xmlNodePtr newnode, cur = handle->zone_dh_cur;
25247c478bd9Sstevel@tonic-gate 	int err;
25257c478bd9Sstevel@tonic-gate 
25267c478bd9Sstevel@tonic-gate 	newnode = xmlNewTextChild(cur, NULL, DTD_ELEM_ATTR, NULL);
2527a1be23daSdp 	err = newprop(newnode, DTD_ATTR_NAME, tabptr->zone_attr_name);
25287c478bd9Sstevel@tonic-gate 	if (err != Z_OK)
25297c478bd9Sstevel@tonic-gate 		return (err);
2530a1be23daSdp 	err = newprop(newnode, DTD_ATTR_TYPE, tabptr->zone_attr_type);
25317c478bd9Sstevel@tonic-gate 	if (err != Z_OK)
25327c478bd9Sstevel@tonic-gate 		return (err);
2533a1be23daSdp 	err = newprop(newnode, DTD_ATTR_VALUE, tabptr->zone_attr_value);
25347c478bd9Sstevel@tonic-gate 	if (err != Z_OK)
25357c478bd9Sstevel@tonic-gate 		return (err);
25367c478bd9Sstevel@tonic-gate 	return (Z_OK);
25377c478bd9Sstevel@tonic-gate }
25387c478bd9Sstevel@tonic-gate 
25397c478bd9Sstevel@tonic-gate int
25407c478bd9Sstevel@tonic-gate zonecfg_add_attr(zone_dochandle_t handle, struct zone_attrtab *tabptr)
25417c478bd9Sstevel@tonic-gate {
25427c478bd9Sstevel@tonic-gate 	int err;
25437c478bd9Sstevel@tonic-gate 
25447c478bd9Sstevel@tonic-gate 	if (tabptr == NULL)
25457c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
25467c478bd9Sstevel@tonic-gate 
25477c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
25487c478bd9Sstevel@tonic-gate 		return (err);
25497c478bd9Sstevel@tonic-gate 
25507c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_add_attr_core(handle, tabptr)) != Z_OK)
25517c478bd9Sstevel@tonic-gate 		return (err);
25527c478bd9Sstevel@tonic-gate 
25537c478bd9Sstevel@tonic-gate 	return (Z_OK);
25547c478bd9Sstevel@tonic-gate }
25557c478bd9Sstevel@tonic-gate 
25567c478bd9Sstevel@tonic-gate static int
25577c478bd9Sstevel@tonic-gate zonecfg_delete_attr_core(zone_dochandle_t handle, struct zone_attrtab *tabptr)
25587c478bd9Sstevel@tonic-gate {
25597c478bd9Sstevel@tonic-gate 	xmlNodePtr cur = handle->zone_dh_cur;
25607c478bd9Sstevel@tonic-gate 	int name_match, type_match, value_match;
25617c478bd9Sstevel@tonic-gate 
25627c478bd9Sstevel@tonic-gate 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
25637c478bd9Sstevel@tonic-gate 		if (xmlStrcmp(cur->name, DTD_ELEM_ATTR))
25647c478bd9Sstevel@tonic-gate 			continue;
25657c478bd9Sstevel@tonic-gate 
25667c478bd9Sstevel@tonic-gate 		name_match = match_prop(cur, DTD_ATTR_NAME,
25677c478bd9Sstevel@tonic-gate 		    tabptr->zone_attr_name);
25687c478bd9Sstevel@tonic-gate 		type_match = match_prop(cur, DTD_ATTR_TYPE,
25697c478bd9Sstevel@tonic-gate 		    tabptr->zone_attr_type);
25707c478bd9Sstevel@tonic-gate 		value_match = match_prop(cur, DTD_ATTR_VALUE,
25717c478bd9Sstevel@tonic-gate 		    tabptr->zone_attr_value);
25727c478bd9Sstevel@tonic-gate 
25737c478bd9Sstevel@tonic-gate 		if (name_match && type_match && value_match) {
25747c478bd9Sstevel@tonic-gate 			xmlUnlinkNode(cur);
25757c478bd9Sstevel@tonic-gate 			xmlFreeNode(cur);
25767c478bd9Sstevel@tonic-gate 			return (Z_OK);
25777c478bd9Sstevel@tonic-gate 		}
25787c478bd9Sstevel@tonic-gate 	}
25797c478bd9Sstevel@tonic-gate 	return (Z_NO_RESOURCE_ID);
25807c478bd9Sstevel@tonic-gate }
25817c478bd9Sstevel@tonic-gate 
25827c478bd9Sstevel@tonic-gate int
25837c478bd9Sstevel@tonic-gate zonecfg_delete_attr(zone_dochandle_t handle, struct zone_attrtab *tabptr)
25847c478bd9Sstevel@tonic-gate {
25857c478bd9Sstevel@tonic-gate 	int err;
25867c478bd9Sstevel@tonic-gate 
25877c478bd9Sstevel@tonic-gate 	if (tabptr == NULL)
25887c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
25897c478bd9Sstevel@tonic-gate 
25907c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
25917c478bd9Sstevel@tonic-gate 		return (err);
25927c478bd9Sstevel@tonic-gate 
25937c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_delete_attr_core(handle, tabptr)) != Z_OK)
25947c478bd9Sstevel@tonic-gate 		return (err);
25957c478bd9Sstevel@tonic-gate 
25967c478bd9Sstevel@tonic-gate 	return (Z_OK);
25977c478bd9Sstevel@tonic-gate }
25987c478bd9Sstevel@tonic-gate 
25997c478bd9Sstevel@tonic-gate int
26007c478bd9Sstevel@tonic-gate zonecfg_modify_attr(
26017c478bd9Sstevel@tonic-gate 	zone_dochandle_t handle,
26027c478bd9Sstevel@tonic-gate 	struct zone_attrtab *oldtabptr,
26037c478bd9Sstevel@tonic-gate 	struct zone_attrtab *newtabptr)
26047c478bd9Sstevel@tonic-gate {
26057c478bd9Sstevel@tonic-gate 	int err;
26067c478bd9Sstevel@tonic-gate 
26077c478bd9Sstevel@tonic-gate 	if (oldtabptr == NULL || newtabptr == NULL)
26087c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
26097c478bd9Sstevel@tonic-gate 
26107c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
26117c478bd9Sstevel@tonic-gate 		return (err);
26127c478bd9Sstevel@tonic-gate 
26137c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_delete_attr_core(handle, oldtabptr)) != Z_OK)
26147c478bd9Sstevel@tonic-gate 		return (err);
26157c478bd9Sstevel@tonic-gate 
26167c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_add_attr_core(handle, newtabptr)) != Z_OK)
26177c478bd9Sstevel@tonic-gate 		return (err);
26187c478bd9Sstevel@tonic-gate 
26197c478bd9Sstevel@tonic-gate 	return (Z_OK);
26207c478bd9Sstevel@tonic-gate }
26217c478bd9Sstevel@tonic-gate 
26227c478bd9Sstevel@tonic-gate int
26237c478bd9Sstevel@tonic-gate zonecfg_get_attr_boolean(const struct zone_attrtab *attr, boolean_t *value)
26247c478bd9Sstevel@tonic-gate {
26257c478bd9Sstevel@tonic-gate 	if (attr == NULL)
26267c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
26277c478bd9Sstevel@tonic-gate 
26287c478bd9Sstevel@tonic-gate 	if (strcmp(attr->zone_attr_type, DTD_ENTITY_BOOLEAN) != 0)
26297c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
26307c478bd9Sstevel@tonic-gate 
26317c478bd9Sstevel@tonic-gate 	if (strcmp(attr->zone_attr_value, DTD_ENTITY_TRUE) == 0) {
26327c478bd9Sstevel@tonic-gate 		*value = B_TRUE;
26337c478bd9Sstevel@tonic-gate 		return (Z_OK);
26347c478bd9Sstevel@tonic-gate 	}
26357c478bd9Sstevel@tonic-gate 	if (strcmp(attr->zone_attr_value, DTD_ENTITY_FALSE) == 0) {
26367c478bd9Sstevel@tonic-gate 		*value = B_FALSE;
26377c478bd9Sstevel@tonic-gate 		return (Z_OK);
26387c478bd9Sstevel@tonic-gate 	}
26397c478bd9Sstevel@tonic-gate 	return (Z_INVAL);
26407c478bd9Sstevel@tonic-gate }
26417c478bd9Sstevel@tonic-gate 
26427c478bd9Sstevel@tonic-gate int
26437c478bd9Sstevel@tonic-gate zonecfg_get_attr_int(const struct zone_attrtab *attr, int64_t *value)
26447c478bd9Sstevel@tonic-gate {
26457c478bd9Sstevel@tonic-gate 	long long result;
26467c478bd9Sstevel@tonic-gate 	char *endptr;
26477c478bd9Sstevel@tonic-gate 
26487c478bd9Sstevel@tonic-gate 	if (attr == NULL)
26497c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
26507c478bd9Sstevel@tonic-gate 
26517c478bd9Sstevel@tonic-gate 	if (strcmp(attr->zone_attr_type, DTD_ENTITY_INT) != 0)
26527c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
26537c478bd9Sstevel@tonic-gate 
26547c478bd9Sstevel@tonic-gate 	errno = 0;
26557c478bd9Sstevel@tonic-gate 	result = strtoll(attr->zone_attr_value, &endptr, 10);
26567c478bd9Sstevel@tonic-gate 	if (errno != 0 || *endptr != '\0')
26577c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
26587c478bd9Sstevel@tonic-gate 	*value = result;
26597c478bd9Sstevel@tonic-gate 	return (Z_OK);
26607c478bd9Sstevel@tonic-gate }
26617c478bd9Sstevel@tonic-gate 
26627c478bd9Sstevel@tonic-gate int
26637c478bd9Sstevel@tonic-gate zonecfg_get_attr_string(const struct zone_attrtab *attr, char *value,
26647c478bd9Sstevel@tonic-gate     size_t val_sz)
26657c478bd9Sstevel@tonic-gate {
26667c478bd9Sstevel@tonic-gate 	if (attr == NULL)
26677c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
26687c478bd9Sstevel@tonic-gate 
26697c478bd9Sstevel@tonic-gate 	if (strcmp(attr->zone_attr_type, DTD_ENTITY_STRING) != 0)
26707c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
26717c478bd9Sstevel@tonic-gate 
26727c478bd9Sstevel@tonic-gate 	if (strlcpy(value, attr->zone_attr_value, val_sz) >= val_sz)
26737c478bd9Sstevel@tonic-gate 		return (Z_TOO_BIG);
26747c478bd9Sstevel@tonic-gate 	return (Z_OK);
26757c478bd9Sstevel@tonic-gate }
26767c478bd9Sstevel@tonic-gate 
26777c478bd9Sstevel@tonic-gate int
26787c478bd9Sstevel@tonic-gate zonecfg_get_attr_uint(const struct zone_attrtab *attr, uint64_t *value)
26797c478bd9Sstevel@tonic-gate {
26807c478bd9Sstevel@tonic-gate 	unsigned long long result;
26817c478bd9Sstevel@tonic-gate 	long long neg_result;
26827c478bd9Sstevel@tonic-gate 	char *endptr;
26837c478bd9Sstevel@tonic-gate 
26847c478bd9Sstevel@tonic-gate 	if (attr == NULL)
26857c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
26867c478bd9Sstevel@tonic-gate 
26877c478bd9Sstevel@tonic-gate 	if (strcmp(attr->zone_attr_type, DTD_ENTITY_UINT) != 0)
26887c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
26897c478bd9Sstevel@tonic-gate 
26907c478bd9Sstevel@tonic-gate 	errno = 0;
26917c478bd9Sstevel@tonic-gate 	result = strtoull(attr->zone_attr_value, &endptr, 10);
26927c478bd9Sstevel@tonic-gate 	if (errno != 0 || *endptr != '\0')
26937c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
26947c478bd9Sstevel@tonic-gate 	errno = 0;
26957c478bd9Sstevel@tonic-gate 	neg_result = strtoll(attr->zone_attr_value, &endptr, 10);
26967c478bd9Sstevel@tonic-gate 	/*
26977c478bd9Sstevel@tonic-gate 	 * Incredibly, strtoull("<negative number>", ...) will not fail but
26987c478bd9Sstevel@tonic-gate 	 * return whatever (negative) number cast as a u_longlong_t, so we
26997c478bd9Sstevel@tonic-gate 	 * need to look for this here.
27007c478bd9Sstevel@tonic-gate 	 */
27017c478bd9Sstevel@tonic-gate 	if (errno == 0 && neg_result < 0)
27027c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
27037c478bd9Sstevel@tonic-gate 	*value = result;
27047c478bd9Sstevel@tonic-gate 	return (Z_OK);
27057c478bd9Sstevel@tonic-gate }
27067c478bd9Sstevel@tonic-gate 
27077c478bd9Sstevel@tonic-gate int
27087c478bd9Sstevel@tonic-gate zonecfg_lookup_rctl(zone_dochandle_t handle, struct zone_rctltab *tabptr)
27097c478bd9Sstevel@tonic-gate {
27107c478bd9Sstevel@tonic-gate 	xmlNodePtr cur, val;
27117c478bd9Sstevel@tonic-gate 	char savedname[MAXNAMELEN];
27127c478bd9Sstevel@tonic-gate 	struct zone_rctlvaltab *valptr;
27137c478bd9Sstevel@tonic-gate 	int err;
27147c478bd9Sstevel@tonic-gate 
27157c478bd9Sstevel@tonic-gate 	if (tabptr->zone_rctl_name == NULL ||
27167c478bd9Sstevel@tonic-gate 	    strlen(tabptr->zone_rctl_name) == 0)
27177c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
27187c478bd9Sstevel@tonic-gate 
27197c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
27207c478bd9Sstevel@tonic-gate 		return (err);
27217c478bd9Sstevel@tonic-gate 
27227c478bd9Sstevel@tonic-gate 	cur = handle->zone_dh_cur;
27237c478bd9Sstevel@tonic-gate 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
27247c478bd9Sstevel@tonic-gate 		if (xmlStrcmp(cur->name, DTD_ELEM_RCTL))
27257c478bd9Sstevel@tonic-gate 			continue;
27267c478bd9Sstevel@tonic-gate 		if ((fetchprop(cur, DTD_ATTR_NAME, savedname,
27277c478bd9Sstevel@tonic-gate 		    sizeof (savedname)) == Z_OK) &&
27287c478bd9Sstevel@tonic-gate 		    (strcmp(savedname, tabptr->zone_rctl_name) == 0)) {
27297c478bd9Sstevel@tonic-gate 			tabptr->zone_rctl_valptr = NULL;
27307c478bd9Sstevel@tonic-gate 			for (val = cur->xmlChildrenNode; val != NULL;
27317c478bd9Sstevel@tonic-gate 			    val = val->next) {
27327c478bd9Sstevel@tonic-gate 				valptr = (struct zone_rctlvaltab *)malloc(
27337c478bd9Sstevel@tonic-gate 				    sizeof (struct zone_rctlvaltab));
27347c478bd9Sstevel@tonic-gate 				if (valptr == NULL)
27357c478bd9Sstevel@tonic-gate 					return (Z_NOMEM);
27367c478bd9Sstevel@tonic-gate 				if ((fetchprop(val, DTD_ATTR_PRIV,
27377c478bd9Sstevel@tonic-gate 				    valptr->zone_rctlval_priv,
27387c478bd9Sstevel@tonic-gate 				    sizeof (valptr->zone_rctlval_priv)) !=
27397c478bd9Sstevel@tonic-gate 				    Z_OK))
27407c478bd9Sstevel@tonic-gate 					break;
27417c478bd9Sstevel@tonic-gate 				if ((fetchprop(val, DTD_ATTR_LIMIT,
27427c478bd9Sstevel@tonic-gate 				    valptr->zone_rctlval_limit,
27437c478bd9Sstevel@tonic-gate 				    sizeof (valptr->zone_rctlval_limit)) !=
27447c478bd9Sstevel@tonic-gate 				    Z_OK))
27457c478bd9Sstevel@tonic-gate 					break;
27467c478bd9Sstevel@tonic-gate 				if ((fetchprop(val, DTD_ATTR_ACTION,
27477c478bd9Sstevel@tonic-gate 				    valptr->zone_rctlval_action,
27487c478bd9Sstevel@tonic-gate 				    sizeof (valptr->zone_rctlval_action)) !=
27497c478bd9Sstevel@tonic-gate 				    Z_OK))
27507c478bd9Sstevel@tonic-gate 					break;
27517c478bd9Sstevel@tonic-gate 				if (zonecfg_add_rctl_value(tabptr, valptr) !=
27527c478bd9Sstevel@tonic-gate 				    Z_OK)
27537c478bd9Sstevel@tonic-gate 					break;
27547c478bd9Sstevel@tonic-gate 			}
27557c478bd9Sstevel@tonic-gate 			return (Z_OK);
27567c478bd9Sstevel@tonic-gate 		}
27577c478bd9Sstevel@tonic-gate 	}
27587c478bd9Sstevel@tonic-gate 	return (Z_NO_RESOURCE_ID);
27597c478bd9Sstevel@tonic-gate }
27607c478bd9Sstevel@tonic-gate 
27617c478bd9Sstevel@tonic-gate static int
27627c478bd9Sstevel@tonic-gate zonecfg_add_rctl_core(zone_dochandle_t handle, struct zone_rctltab *tabptr)
27637c478bd9Sstevel@tonic-gate {
27647c478bd9Sstevel@tonic-gate 	xmlNodePtr newnode, cur = handle->zone_dh_cur, valnode;
27657c478bd9Sstevel@tonic-gate 	struct zone_rctlvaltab *valptr;
27667c478bd9Sstevel@tonic-gate 	int err;
27677c478bd9Sstevel@tonic-gate 
27687c478bd9Sstevel@tonic-gate 	newnode = xmlNewTextChild(cur, NULL, DTD_ELEM_RCTL, NULL);
2769a1be23daSdp 	err = newprop(newnode, DTD_ATTR_NAME, tabptr->zone_rctl_name);
27707c478bd9Sstevel@tonic-gate 	if (err != Z_OK)
27717c478bd9Sstevel@tonic-gate 		return (err);
27727c478bd9Sstevel@tonic-gate 	for (valptr = tabptr->zone_rctl_valptr; valptr != NULL;
27737c478bd9Sstevel@tonic-gate 	    valptr = valptr->zone_rctlval_next) {
27747c478bd9Sstevel@tonic-gate 		valnode = xmlNewTextChild(newnode, NULL,
27757c478bd9Sstevel@tonic-gate 		    DTD_ELEM_RCTLVALUE, NULL);
2776a1be23daSdp 		err = newprop(valnode, DTD_ATTR_PRIV,
27777c478bd9Sstevel@tonic-gate 		    valptr->zone_rctlval_priv);
27787c478bd9Sstevel@tonic-gate 		if (err != Z_OK)
27797c478bd9Sstevel@tonic-gate 			return (err);
2780a1be23daSdp 		err = newprop(valnode, DTD_ATTR_LIMIT,
27817c478bd9Sstevel@tonic-gate 		    valptr->zone_rctlval_limit);
27827c478bd9Sstevel@tonic-gate 		if (err != Z_OK)
27837c478bd9Sstevel@tonic-gate 			return (err);
2784a1be23daSdp 		err = newprop(valnode, DTD_ATTR_ACTION,
27857c478bd9Sstevel@tonic-gate 		    valptr->zone_rctlval_action);
27867c478bd9Sstevel@tonic-gate 		if (err != Z_OK)
27877c478bd9Sstevel@tonic-gate 			return (err);
27887c478bd9Sstevel@tonic-gate 	}
27897c478bd9Sstevel@tonic-gate 	return (Z_OK);
27907c478bd9Sstevel@tonic-gate }
27917c478bd9Sstevel@tonic-gate 
27927c478bd9Sstevel@tonic-gate int
27937c478bd9Sstevel@tonic-gate zonecfg_add_rctl(zone_dochandle_t handle, struct zone_rctltab *tabptr)
27947c478bd9Sstevel@tonic-gate {
27957c478bd9Sstevel@tonic-gate 	int err;
27967c478bd9Sstevel@tonic-gate 
27977c478bd9Sstevel@tonic-gate 	if (tabptr == NULL || tabptr->zone_rctl_name == NULL)
27987c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
27997c478bd9Sstevel@tonic-gate 
28007c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
28017c478bd9Sstevel@tonic-gate 		return (err);
28027c478bd9Sstevel@tonic-gate 
28037c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_add_rctl_core(handle, tabptr)) != Z_OK)
28047c478bd9Sstevel@tonic-gate 		return (err);
28057c478bd9Sstevel@tonic-gate 
28067c478bd9Sstevel@tonic-gate 	return (Z_OK);
28077c478bd9Sstevel@tonic-gate }
28087c478bd9Sstevel@tonic-gate 
28097c478bd9Sstevel@tonic-gate static int
28107c478bd9Sstevel@tonic-gate zonecfg_delete_rctl_core(zone_dochandle_t handle, struct zone_rctltab *tabptr)
28117c478bd9Sstevel@tonic-gate {
28127c478bd9Sstevel@tonic-gate 	xmlNodePtr cur = handle->zone_dh_cur;
28137c478bd9Sstevel@tonic-gate 	xmlChar *savedname;
28147c478bd9Sstevel@tonic-gate 	int name_result;
28157c478bd9Sstevel@tonic-gate 
28167c478bd9Sstevel@tonic-gate 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
28177c478bd9Sstevel@tonic-gate 		if (xmlStrcmp(cur->name, DTD_ELEM_RCTL))
28187c478bd9Sstevel@tonic-gate 			continue;
28197c478bd9Sstevel@tonic-gate 
28207c478bd9Sstevel@tonic-gate 		savedname = xmlGetProp(cur, DTD_ATTR_NAME);
28217c478bd9Sstevel@tonic-gate 		if (savedname == NULL)	/* shouldn't happen */
28227c478bd9Sstevel@tonic-gate 			continue;
28237c478bd9Sstevel@tonic-gate 		name_result = xmlStrcmp(savedname,
28247c478bd9Sstevel@tonic-gate 		    (const xmlChar *) tabptr->zone_rctl_name);
28257c478bd9Sstevel@tonic-gate 		xmlFree(savedname);
28267c478bd9Sstevel@tonic-gate 
28277c478bd9Sstevel@tonic-gate 		if (name_result == 0) {
28287c478bd9Sstevel@tonic-gate 			xmlUnlinkNode(cur);
28297c478bd9Sstevel@tonic-gate 			xmlFreeNode(cur);
28307c478bd9Sstevel@tonic-gate 			return (Z_OK);
28317c478bd9Sstevel@tonic-gate 		}
28327c478bd9Sstevel@tonic-gate 	}
28337c478bd9Sstevel@tonic-gate 	return (Z_NO_RESOURCE_ID);
28347c478bd9Sstevel@tonic-gate }
28357c478bd9Sstevel@tonic-gate 
28367c478bd9Sstevel@tonic-gate int
28377c478bd9Sstevel@tonic-gate zonecfg_delete_rctl(zone_dochandle_t handle, struct zone_rctltab *tabptr)
28387c478bd9Sstevel@tonic-gate {
28397c478bd9Sstevel@tonic-gate 	int err;
28407c478bd9Sstevel@tonic-gate 
28417c478bd9Sstevel@tonic-gate 	if (tabptr == NULL || tabptr->zone_rctl_name == NULL)
28427c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
28437c478bd9Sstevel@tonic-gate 
28447c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
28457c478bd9Sstevel@tonic-gate 		return (err);
28467c478bd9Sstevel@tonic-gate 
28477c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_delete_rctl_core(handle, tabptr)) != Z_OK)
28487c478bd9Sstevel@tonic-gate 		return (err);
28497c478bd9Sstevel@tonic-gate 
28507c478bd9Sstevel@tonic-gate 	return (Z_OK);
28517c478bd9Sstevel@tonic-gate }
28527c478bd9Sstevel@tonic-gate 
28537c478bd9Sstevel@tonic-gate int
28547c478bd9Sstevel@tonic-gate zonecfg_modify_rctl(
28557c478bd9Sstevel@tonic-gate 	zone_dochandle_t handle,
28567c478bd9Sstevel@tonic-gate 	struct zone_rctltab *oldtabptr,
28577c478bd9Sstevel@tonic-gate 	struct zone_rctltab *newtabptr)
28587c478bd9Sstevel@tonic-gate {
28597c478bd9Sstevel@tonic-gate 	int err;
28607c478bd9Sstevel@tonic-gate 
28617c478bd9Sstevel@tonic-gate 	if (oldtabptr == NULL || oldtabptr->zone_rctl_name == NULL ||
28627c478bd9Sstevel@tonic-gate 	    newtabptr == NULL || newtabptr->zone_rctl_name == NULL)
28637c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
28647c478bd9Sstevel@tonic-gate 
28657c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
28667c478bd9Sstevel@tonic-gate 		return (err);
28677c478bd9Sstevel@tonic-gate 
28687c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_delete_rctl_core(handle, oldtabptr)) != Z_OK)
28697c478bd9Sstevel@tonic-gate 		return (err);
28707c478bd9Sstevel@tonic-gate 
28717c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_add_rctl_core(handle, newtabptr)) != Z_OK)
28727c478bd9Sstevel@tonic-gate 		return (err);
28737c478bd9Sstevel@tonic-gate 
28747c478bd9Sstevel@tonic-gate 	return (Z_OK);
28757c478bd9Sstevel@tonic-gate }
28767c478bd9Sstevel@tonic-gate 
28777c478bd9Sstevel@tonic-gate int
28787c478bd9Sstevel@tonic-gate zonecfg_add_rctl_value(
28797c478bd9Sstevel@tonic-gate 	struct zone_rctltab *tabptr,
28807c478bd9Sstevel@tonic-gate 	struct zone_rctlvaltab *valtabptr)
28817c478bd9Sstevel@tonic-gate {
28827c478bd9Sstevel@tonic-gate 	struct zone_rctlvaltab *last, *old, *new;
28837c478bd9Sstevel@tonic-gate 	rctlblk_t *rctlblk = alloca(rctlblk_size());
28847c478bd9Sstevel@tonic-gate 
28857c478bd9Sstevel@tonic-gate 	last = tabptr->zone_rctl_valptr;
28867c478bd9Sstevel@tonic-gate 	for (old = last; old != NULL; old = old->zone_rctlval_next)
28877c478bd9Sstevel@tonic-gate 		last = old;	/* walk to the end of the list */
28887c478bd9Sstevel@tonic-gate 	new = valtabptr;	/* alloc'd by caller */
28897c478bd9Sstevel@tonic-gate 	new->zone_rctlval_next = NULL;
28907c478bd9Sstevel@tonic-gate 	if (zonecfg_construct_rctlblk(valtabptr, rctlblk) != Z_OK)
28917c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
28927c478bd9Sstevel@tonic-gate 	if (!zonecfg_valid_rctlblk(rctlblk))
28937c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
28947c478bd9Sstevel@tonic-gate 	if (last == NULL)
28957c478bd9Sstevel@tonic-gate 		tabptr->zone_rctl_valptr = new;
28967c478bd9Sstevel@tonic-gate 	else
28977c478bd9Sstevel@tonic-gate 		last->zone_rctlval_next = new;
28987c478bd9Sstevel@tonic-gate 	return (Z_OK);
28997c478bd9Sstevel@tonic-gate }
29007c478bd9Sstevel@tonic-gate 
29017c478bd9Sstevel@tonic-gate int
29027c478bd9Sstevel@tonic-gate zonecfg_remove_rctl_value(
29037c478bd9Sstevel@tonic-gate 	struct zone_rctltab *tabptr,
29047c478bd9Sstevel@tonic-gate 	struct zone_rctlvaltab *valtabptr)
29057c478bd9Sstevel@tonic-gate {
29067c478bd9Sstevel@tonic-gate 	struct zone_rctlvaltab *last, *this, *next;
29077c478bd9Sstevel@tonic-gate 
29087c478bd9Sstevel@tonic-gate 	last = tabptr->zone_rctl_valptr;
29097c478bd9Sstevel@tonic-gate 	for (this = last; this != NULL; this = this->zone_rctlval_next) {
29107c478bd9Sstevel@tonic-gate 		if (strcmp(this->zone_rctlval_priv,
29117c478bd9Sstevel@tonic-gate 		    valtabptr->zone_rctlval_priv) == 0 &&
29127c478bd9Sstevel@tonic-gate 		    strcmp(this->zone_rctlval_limit,
29137c478bd9Sstevel@tonic-gate 		    valtabptr->zone_rctlval_limit) == 0 &&
29147c478bd9Sstevel@tonic-gate 		    strcmp(this->zone_rctlval_action,
29157c478bd9Sstevel@tonic-gate 		    valtabptr->zone_rctlval_action) == 0) {
29167c478bd9Sstevel@tonic-gate 			next = this->zone_rctlval_next;
29177c478bd9Sstevel@tonic-gate 			if (this == tabptr->zone_rctl_valptr)
29187c478bd9Sstevel@tonic-gate 				tabptr->zone_rctl_valptr = next;
29197c478bd9Sstevel@tonic-gate 			else
29207c478bd9Sstevel@tonic-gate 				last->zone_rctlval_next = next;
29217c478bd9Sstevel@tonic-gate 			free(this);
29227c478bd9Sstevel@tonic-gate 			return (Z_OK);
29237c478bd9Sstevel@tonic-gate 		} else
29247c478bd9Sstevel@tonic-gate 			last = this;
29257c478bd9Sstevel@tonic-gate 	}
29267c478bd9Sstevel@tonic-gate 	return (Z_NO_PROPERTY_ID);
29277c478bd9Sstevel@tonic-gate }
29287c478bd9Sstevel@tonic-gate 
29297c478bd9Sstevel@tonic-gate char *
29307c478bd9Sstevel@tonic-gate zonecfg_strerror(int errnum)
29317c478bd9Sstevel@tonic-gate {
29327c478bd9Sstevel@tonic-gate 	switch (errnum) {
29337c478bd9Sstevel@tonic-gate 	case Z_OK:
29347c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "OK"));
29357c478bd9Sstevel@tonic-gate 	case Z_EMPTY_DOCUMENT:
29367c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Empty document"));
29377c478bd9Sstevel@tonic-gate 	case Z_WRONG_DOC_TYPE:
29387c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Wrong document type"));
29397c478bd9Sstevel@tonic-gate 	case Z_BAD_PROPERTY:
29407c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Bad document property"));
29417c478bd9Sstevel@tonic-gate 	case Z_TEMP_FILE:
29427c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN,
29437c478bd9Sstevel@tonic-gate 		    "Problem creating temporary file"));
29447c478bd9Sstevel@tonic-gate 	case Z_SAVING_FILE:
29457c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Problem saving file"));
29467c478bd9Sstevel@tonic-gate 	case Z_NO_ENTRY:
29477c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "No such entry"));
29487c478bd9Sstevel@tonic-gate 	case Z_BOGUS_ZONE_NAME:
29497c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Bogus zone name"));
29507c478bd9Sstevel@tonic-gate 	case Z_REQD_RESOURCE_MISSING:
29517c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Required resource missing"));
29527c478bd9Sstevel@tonic-gate 	case Z_REQD_PROPERTY_MISSING:
29537c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Required property missing"));
29547c478bd9Sstevel@tonic-gate 	case Z_BAD_HANDLE:
29557c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Bad handle"));
29567c478bd9Sstevel@tonic-gate 	case Z_NOMEM:
29577c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Out of memory"));
29587c478bd9Sstevel@tonic-gate 	case Z_INVAL:
29597c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Invalid argument"));
29607c478bd9Sstevel@tonic-gate 	case Z_ACCES:
29617c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Permission denied"));
29627c478bd9Sstevel@tonic-gate 	case Z_TOO_BIG:
29637c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Argument list too long"));
29647c478bd9Sstevel@tonic-gate 	case Z_MISC_FS:
29657c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN,
29667c478bd9Sstevel@tonic-gate 		    "Miscellaneous file system error"));
29677c478bd9Sstevel@tonic-gate 	case Z_NO_ZONE:
29687c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "No such zone configured"));
29697c478bd9Sstevel@tonic-gate 	case Z_NO_RESOURCE_TYPE:
29707c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "No such resource type"));
29717c478bd9Sstevel@tonic-gate 	case Z_NO_RESOURCE_ID:
29727c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "No such resource with that id"));
29737c478bd9Sstevel@tonic-gate 	case Z_NO_PROPERTY_TYPE:
29747c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "No such property type"));
29757c478bd9Sstevel@tonic-gate 	case Z_NO_PROPERTY_ID:
29767c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "No such property with that id"));
2977087719fdSdp 	case Z_BAD_ZONE_STATE:
29787c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN,
2979087719fdSdp 		    "Zone state is invalid for the requested operation"));
29807c478bd9Sstevel@tonic-gate 	case Z_INVALID_DOCUMENT:
29817c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Invalid document"));
2982087719fdSdp 	case Z_NAME_IN_USE:
2983087719fdSdp 		return (dgettext(TEXT_DOMAIN, "Zone name already in use"));
29847c478bd9Sstevel@tonic-gate 	case Z_NO_SUCH_ID:
29857c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "No such zone ID"));
29867c478bd9Sstevel@tonic-gate 	case Z_UPDATING_INDEX:
29877c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Problem updating index file"));
29887c478bd9Sstevel@tonic-gate 	case Z_LOCKING_FILE:
29897c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Locking index file"));
29907c478bd9Sstevel@tonic-gate 	case Z_UNLOCKING_FILE:
29917c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Unlocking index file"));
29927c478bd9Sstevel@tonic-gate 	case Z_INSUFFICIENT_SPEC:
29937c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Insufficient specification"));
29947c478bd9Sstevel@tonic-gate 	case Z_RESOLVED_PATH:
29957c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Resolved path mismatch"));
29967c478bd9Sstevel@tonic-gate 	case Z_IPV6_ADDR_PREFIX_LEN:
29977c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN,
29987c478bd9Sstevel@tonic-gate 		    "IPv6 address missing required prefix length"));
29997c478bd9Sstevel@tonic-gate 	case Z_BOGUS_ADDRESS:
30007c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN,
30017c478bd9Sstevel@tonic-gate 		    "Neither an IPv4 nor an IPv6 address nor a host name"));
3002ffbafc53Scomay 	case Z_PRIV_PROHIBITED:
3003ffbafc53Scomay 		return (dgettext(TEXT_DOMAIN,
3004ffbafc53Scomay 		    "Specified privilege is prohibited"));
3005ffbafc53Scomay 	case Z_PRIV_REQUIRED:
3006ffbafc53Scomay 		return (dgettext(TEXT_DOMAIN,
3007ffbafc53Scomay 		    "Required privilege is missing"));
3008ffbafc53Scomay 	case Z_PRIV_UNKNOWN:
3009ffbafc53Scomay 		return (dgettext(TEXT_DOMAIN,
3010ffbafc53Scomay 		    "Specified privilege is unknown"));
30117c478bd9Sstevel@tonic-gate 	default:
30127c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Unknown error"));
30137c478bd9Sstevel@tonic-gate 	}
30147c478bd9Sstevel@tonic-gate }
30157c478bd9Sstevel@tonic-gate 
30167c478bd9Sstevel@tonic-gate /*
30177c478bd9Sstevel@tonic-gate  * Note that the zonecfg_setXent() and zonecfg_endXent() calls are all the
30187c478bd9Sstevel@tonic-gate  * same, as they just turn around and call zonecfg_setent() / zonecfg_endent().
30197c478bd9Sstevel@tonic-gate  */
30207c478bd9Sstevel@tonic-gate 
30217c478bd9Sstevel@tonic-gate static int
30227c478bd9Sstevel@tonic-gate zonecfg_setent(zone_dochandle_t handle)
30237c478bd9Sstevel@tonic-gate {
30247c478bd9Sstevel@tonic-gate 	xmlNodePtr cur;
30257c478bd9Sstevel@tonic-gate 	int err;
30267c478bd9Sstevel@tonic-gate 
30277c478bd9Sstevel@tonic-gate 	if (handle == NULL)
30287c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
30297c478bd9Sstevel@tonic-gate 
30307c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK) {
30317c478bd9Sstevel@tonic-gate 		handle->zone_dh_cur = NULL;
30327c478bd9Sstevel@tonic-gate 		return (err);
30337c478bd9Sstevel@tonic-gate 	}
30347c478bd9Sstevel@tonic-gate 	cur = handle->zone_dh_cur;
30357c478bd9Sstevel@tonic-gate 	cur = cur->xmlChildrenNode;
30367c478bd9Sstevel@tonic-gate 	handle->zone_dh_cur = cur;
30377c478bd9Sstevel@tonic-gate 	return (Z_OK);
30387c478bd9Sstevel@tonic-gate }
30397c478bd9Sstevel@tonic-gate 
30407c478bd9Sstevel@tonic-gate static int
30417c478bd9Sstevel@tonic-gate zonecfg_endent(zone_dochandle_t handle)
30427c478bd9Sstevel@tonic-gate {
30437c478bd9Sstevel@tonic-gate 	if (handle == NULL)
30447c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
30457c478bd9Sstevel@tonic-gate 
30467c478bd9Sstevel@tonic-gate 	handle->zone_dh_cur = handle->zone_dh_top;
30477c478bd9Sstevel@tonic-gate 	return (Z_OK);
30487c478bd9Sstevel@tonic-gate }
30497c478bd9Sstevel@tonic-gate 
30507c478bd9Sstevel@tonic-gate int
30517c478bd9Sstevel@tonic-gate zonecfg_setfsent(zone_dochandle_t handle)
30527c478bd9Sstevel@tonic-gate {
30537c478bd9Sstevel@tonic-gate 	return (zonecfg_setent(handle));
30547c478bd9Sstevel@tonic-gate }
30557c478bd9Sstevel@tonic-gate 
30567c478bd9Sstevel@tonic-gate int
30577c478bd9Sstevel@tonic-gate zonecfg_getfsent(zone_dochandle_t handle, struct zone_fstab *tabptr)
30587c478bd9Sstevel@tonic-gate {
30597c478bd9Sstevel@tonic-gate 	xmlNodePtr cur, options;
30607c478bd9Sstevel@tonic-gate 	char options_str[MAX_MNTOPT_STR];
30617c478bd9Sstevel@tonic-gate 	int err;
30627c478bd9Sstevel@tonic-gate 
30637c478bd9Sstevel@tonic-gate 	if (handle == NULL)
30647c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
30657c478bd9Sstevel@tonic-gate 
30667c478bd9Sstevel@tonic-gate 	if ((cur = handle->zone_dh_cur) == NULL)
30677c478bd9Sstevel@tonic-gate 		return (Z_NO_ENTRY);
30687c478bd9Sstevel@tonic-gate 
30697c478bd9Sstevel@tonic-gate 	for (; cur != NULL; cur = cur->next)
30707c478bd9Sstevel@tonic-gate 		if (!xmlStrcmp(cur->name, DTD_ELEM_FS))
30717c478bd9Sstevel@tonic-gate 			break;
30727c478bd9Sstevel@tonic-gate 	if (cur == NULL) {
30737c478bd9Sstevel@tonic-gate 		handle->zone_dh_cur = handle->zone_dh_top;
30747c478bd9Sstevel@tonic-gate 		return (Z_NO_ENTRY);
30757c478bd9Sstevel@tonic-gate 	}
30767c478bd9Sstevel@tonic-gate 
30777c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_SPECIAL, tabptr->zone_fs_special,
30787c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_fs_special))) != Z_OK) {
30797c478bd9Sstevel@tonic-gate 		handle->zone_dh_cur = handle->zone_dh_top;
30807c478bd9Sstevel@tonic-gate 		return (err);
30817c478bd9Sstevel@tonic-gate 	}
30827c478bd9Sstevel@tonic-gate 
30837c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_RAW, tabptr->zone_fs_raw,
30847c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_fs_raw))) != Z_OK) {
30857c478bd9Sstevel@tonic-gate 		handle->zone_dh_cur = handle->zone_dh_top;
30867c478bd9Sstevel@tonic-gate 		return (err);
30877c478bd9Sstevel@tonic-gate 	}
30887c478bd9Sstevel@tonic-gate 
30897c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_DIR, tabptr->zone_fs_dir,
30907c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_fs_dir))) != Z_OK) {
30917c478bd9Sstevel@tonic-gate 		handle->zone_dh_cur = handle->zone_dh_top;
30927c478bd9Sstevel@tonic-gate 		return (err);
30937c478bd9Sstevel@tonic-gate 	}
30947c478bd9Sstevel@tonic-gate 
30957c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_TYPE, tabptr->zone_fs_type,
30967c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_fs_type))) != Z_OK) {
30977c478bd9Sstevel@tonic-gate 		handle->zone_dh_cur = handle->zone_dh_top;
30987c478bd9Sstevel@tonic-gate 		return (err);
30997c478bd9Sstevel@tonic-gate 	}
31007c478bd9Sstevel@tonic-gate 
31017c478bd9Sstevel@tonic-gate 	/* OK for options to be NULL */
31027c478bd9Sstevel@tonic-gate 	tabptr->zone_fs_options = NULL;
31037c478bd9Sstevel@tonic-gate 	for (options = cur->xmlChildrenNode; options != NULL;
31047c478bd9Sstevel@tonic-gate 	    options = options->next) {
31057c478bd9Sstevel@tonic-gate 		if (fetchprop(options, DTD_ATTR_NAME, options_str,
31067c478bd9Sstevel@tonic-gate 		    sizeof (options_str)) != Z_OK)
31077c478bd9Sstevel@tonic-gate 			break;
31087c478bd9Sstevel@tonic-gate 		if (zonecfg_add_fs_option(tabptr, options_str) != Z_OK)
31097c478bd9Sstevel@tonic-gate 			break;
31107c478bd9Sstevel@tonic-gate 	}
31117c478bd9Sstevel@tonic-gate 
31127c478bd9Sstevel@tonic-gate 	handle->zone_dh_cur = cur->next;
31137c478bd9Sstevel@tonic-gate 	return (Z_OK);
31147c478bd9Sstevel@tonic-gate }
31157c478bd9Sstevel@tonic-gate 
31167c478bd9Sstevel@tonic-gate int
31177c478bd9Sstevel@tonic-gate zonecfg_endfsent(zone_dochandle_t handle)
31187c478bd9Sstevel@tonic-gate {
31197c478bd9Sstevel@tonic-gate 	return (zonecfg_endent(handle));
31207c478bd9Sstevel@tonic-gate }
31217c478bd9Sstevel@tonic-gate 
31227c478bd9Sstevel@tonic-gate int
31237c478bd9Sstevel@tonic-gate zonecfg_setipdent(zone_dochandle_t handle)
31247c478bd9Sstevel@tonic-gate {
31257c478bd9Sstevel@tonic-gate 	return (zonecfg_setent(handle));
31267c478bd9Sstevel@tonic-gate }
31277c478bd9Sstevel@tonic-gate 
31287c478bd9Sstevel@tonic-gate int
31297c478bd9Sstevel@tonic-gate zonecfg_getipdent(zone_dochandle_t handle, struct zone_fstab *tabptr)
31307c478bd9Sstevel@tonic-gate {
31317c478bd9Sstevel@tonic-gate 	xmlNodePtr cur;
31327c478bd9Sstevel@tonic-gate 	int err;
31337c478bd9Sstevel@tonic-gate 
31347c478bd9Sstevel@tonic-gate 	if (handle == NULL)
31357c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
31367c478bd9Sstevel@tonic-gate 
31377c478bd9Sstevel@tonic-gate 	if ((cur = handle->zone_dh_cur) == NULL)
31387c478bd9Sstevel@tonic-gate 		return (Z_NO_ENTRY);
31397c478bd9Sstevel@tonic-gate 
31407c478bd9Sstevel@tonic-gate 	for (; cur != NULL; cur = cur->next)
31417c478bd9Sstevel@tonic-gate 		if (!xmlStrcmp(cur->name, DTD_ELEM_IPD))
31427c478bd9Sstevel@tonic-gate 			break;
31437c478bd9Sstevel@tonic-gate 	if (cur == NULL) {
31447c478bd9Sstevel@tonic-gate 		handle->zone_dh_cur = handle->zone_dh_top;
31457c478bd9Sstevel@tonic-gate 		return (Z_NO_ENTRY);
31467c478bd9Sstevel@tonic-gate 	}
31477c478bd9Sstevel@tonic-gate 
31487c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_DIR, tabptr->zone_fs_dir,
31497c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_fs_dir))) != Z_OK) {
31507c478bd9Sstevel@tonic-gate 		handle->zone_dh_cur = handle->zone_dh_top;
31517c478bd9Sstevel@tonic-gate 		return (err);
31527c478bd9Sstevel@tonic-gate 	}
31537c478bd9Sstevel@tonic-gate 
31547c478bd9Sstevel@tonic-gate 	handle->zone_dh_cur = cur->next;
31557c478bd9Sstevel@tonic-gate 	return (Z_OK);
31567c478bd9Sstevel@tonic-gate }
31577c478bd9Sstevel@tonic-gate 
31587c478bd9Sstevel@tonic-gate int
31597c478bd9Sstevel@tonic-gate zonecfg_endipdent(zone_dochandle_t handle)
31607c478bd9Sstevel@tonic-gate {
31617c478bd9Sstevel@tonic-gate 	return (zonecfg_endent(handle));
31627c478bd9Sstevel@tonic-gate }
31637c478bd9Sstevel@tonic-gate 
31647c478bd9Sstevel@tonic-gate int
31657c478bd9Sstevel@tonic-gate zonecfg_setnwifent(zone_dochandle_t handle)
31667c478bd9Sstevel@tonic-gate {
31677c478bd9Sstevel@tonic-gate 	return (zonecfg_setent(handle));
31687c478bd9Sstevel@tonic-gate }
31697c478bd9Sstevel@tonic-gate 
31707c478bd9Sstevel@tonic-gate int
31717c478bd9Sstevel@tonic-gate zonecfg_getnwifent(zone_dochandle_t handle, struct zone_nwiftab *tabptr)
31727c478bd9Sstevel@tonic-gate {
31737c478bd9Sstevel@tonic-gate 	xmlNodePtr cur;
31747c478bd9Sstevel@tonic-gate 	int err;
31757c478bd9Sstevel@tonic-gate 
31767c478bd9Sstevel@tonic-gate 	if (handle == NULL)
31777c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
31787c478bd9Sstevel@tonic-gate 
31797c478bd9Sstevel@tonic-gate 	if ((cur = handle->zone_dh_cur) == NULL)
31807c478bd9Sstevel@tonic-gate 		return (Z_NO_ENTRY);
31817c478bd9Sstevel@tonic-gate 
31827c478bd9Sstevel@tonic-gate 	for (; cur != NULL; cur = cur->next)
31837c478bd9Sstevel@tonic-gate 		if (!xmlStrcmp(cur->name, DTD_ELEM_NET))
31847c478bd9Sstevel@tonic-gate 			break;
31857c478bd9Sstevel@tonic-gate 	if (cur == NULL) {
31867c478bd9Sstevel@tonic-gate 		handle->zone_dh_cur = handle->zone_dh_top;
31877c478bd9Sstevel@tonic-gate 		return (Z_NO_ENTRY);
31887c478bd9Sstevel@tonic-gate 	}
31897c478bd9Sstevel@tonic-gate 
31907c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_ADDRESS, tabptr->zone_nwif_address,
31917c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_nwif_address))) != Z_OK) {
31927c478bd9Sstevel@tonic-gate 		handle->zone_dh_cur = handle->zone_dh_top;
31937c478bd9Sstevel@tonic-gate 		return (err);
31947c478bd9Sstevel@tonic-gate 	}
31957c478bd9Sstevel@tonic-gate 
31967c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_PHYSICAL, tabptr->zone_nwif_physical,
31977c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_nwif_physical))) != Z_OK) {
31987c478bd9Sstevel@tonic-gate 		handle->zone_dh_cur = handle->zone_dh_top;
31997c478bd9Sstevel@tonic-gate 		return (err);
32007c478bd9Sstevel@tonic-gate 	}
32017c478bd9Sstevel@tonic-gate 
32027c478bd9Sstevel@tonic-gate 	handle->zone_dh_cur = cur->next;
32037c478bd9Sstevel@tonic-gate 	return (Z_OK);
32047c478bd9Sstevel@tonic-gate }
32057c478bd9Sstevel@tonic-gate 
32067c478bd9Sstevel@tonic-gate int
32077c478bd9Sstevel@tonic-gate zonecfg_endnwifent(zone_dochandle_t handle)
32087c478bd9Sstevel@tonic-gate {
32097c478bd9Sstevel@tonic-gate 	return (zonecfg_endent(handle));
32107c478bd9Sstevel@tonic-gate }
32117c478bd9Sstevel@tonic-gate 
32127c478bd9Sstevel@tonic-gate int
32137c478bd9Sstevel@tonic-gate zonecfg_setdevent(zone_dochandle_t handle)
32147c478bd9Sstevel@tonic-gate {
32157c478bd9Sstevel@tonic-gate 	return (zonecfg_setent(handle));
32167c478bd9Sstevel@tonic-gate }
32177c478bd9Sstevel@tonic-gate 
32187c478bd9Sstevel@tonic-gate int
32197c478bd9Sstevel@tonic-gate zonecfg_getdevent(zone_dochandle_t handle, struct zone_devtab *tabptr)
32207c478bd9Sstevel@tonic-gate {
32217c478bd9Sstevel@tonic-gate 	xmlNodePtr cur;
32227c478bd9Sstevel@tonic-gate 	int err;
32237c478bd9Sstevel@tonic-gate 
32247c478bd9Sstevel@tonic-gate 	if (handle == NULL)
32257c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
32267c478bd9Sstevel@tonic-gate 
32277c478bd9Sstevel@tonic-gate 	if ((cur = handle->zone_dh_cur) == NULL)
32287c478bd9Sstevel@tonic-gate 		return (Z_NO_ENTRY);
32297c478bd9Sstevel@tonic-gate 
32307c478bd9Sstevel@tonic-gate 	for (; cur != NULL; cur = cur->next)
32317c478bd9Sstevel@tonic-gate 		if (!xmlStrcmp(cur->name, DTD_ELEM_DEVICE))
32327c478bd9Sstevel@tonic-gate 			break;
32337c478bd9Sstevel@tonic-gate 	if (cur == NULL) {
32347c478bd9Sstevel@tonic-gate 		handle->zone_dh_cur = handle->zone_dh_top;
32357c478bd9Sstevel@tonic-gate 		return (Z_NO_ENTRY);
32367c478bd9Sstevel@tonic-gate 	}
32377c478bd9Sstevel@tonic-gate 
32387c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_MATCH, tabptr->zone_dev_match,
32397c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_dev_match))) != Z_OK) {
32407c478bd9Sstevel@tonic-gate 		handle->zone_dh_cur = handle->zone_dh_top;
32417c478bd9Sstevel@tonic-gate 		return (err);
32427c478bd9Sstevel@tonic-gate 	}
32437c478bd9Sstevel@tonic-gate 
32447c478bd9Sstevel@tonic-gate 	handle->zone_dh_cur = cur->next;
32457c478bd9Sstevel@tonic-gate 	return (Z_OK);
32467c478bd9Sstevel@tonic-gate }
32477c478bd9Sstevel@tonic-gate 
32487c478bd9Sstevel@tonic-gate int
32497c478bd9Sstevel@tonic-gate zonecfg_enddevent(zone_dochandle_t handle)
32507c478bd9Sstevel@tonic-gate {
32517c478bd9Sstevel@tonic-gate 	return (zonecfg_endent(handle));
32527c478bd9Sstevel@tonic-gate }
32537c478bd9Sstevel@tonic-gate 
32547c478bd9Sstevel@tonic-gate int
32557c478bd9Sstevel@tonic-gate zonecfg_setrctlent(zone_dochandle_t handle)
32567c478bd9Sstevel@tonic-gate {
32577c478bd9Sstevel@tonic-gate 	return (zonecfg_setent(handle));
32587c478bd9Sstevel@tonic-gate }
32597c478bd9Sstevel@tonic-gate 
32607c478bd9Sstevel@tonic-gate int
32617c478bd9Sstevel@tonic-gate zonecfg_getrctlent(zone_dochandle_t handle, struct zone_rctltab *tabptr)
32627c478bd9Sstevel@tonic-gate {
32637c478bd9Sstevel@tonic-gate 	xmlNodePtr cur, val;
32647c478bd9Sstevel@tonic-gate 	struct zone_rctlvaltab *valptr;
32657c478bd9Sstevel@tonic-gate 	int err;
32667c478bd9Sstevel@tonic-gate 
32677c478bd9Sstevel@tonic-gate 	if (handle == NULL)
32687c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
32697c478bd9Sstevel@tonic-gate 
32707c478bd9Sstevel@tonic-gate 	if ((cur = handle->zone_dh_cur) == NULL)
32717c478bd9Sstevel@tonic-gate 		return (Z_NO_ENTRY);
32727c478bd9Sstevel@tonic-gate 
32737c478bd9Sstevel@tonic-gate 	for (; cur != NULL; cur = cur->next)
32747c478bd9Sstevel@tonic-gate 		if (!xmlStrcmp(cur->name, DTD_ELEM_RCTL))
32757c478bd9Sstevel@tonic-gate 			break;
32767c478bd9Sstevel@tonic-gate 	if (cur == NULL) {
32777c478bd9Sstevel@tonic-gate 		handle->zone_dh_cur = handle->zone_dh_top;
32787c478bd9Sstevel@tonic-gate 		return (Z_NO_ENTRY);
32797c478bd9Sstevel@tonic-gate 	}
32807c478bd9Sstevel@tonic-gate 
32817c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_NAME, tabptr->zone_rctl_name,
32827c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_rctl_name))) != Z_OK) {
32837c478bd9Sstevel@tonic-gate 		handle->zone_dh_cur = handle->zone_dh_top;
32847c478bd9Sstevel@tonic-gate 		return (err);
32857c478bd9Sstevel@tonic-gate 	}
32867c478bd9Sstevel@tonic-gate 
32877c478bd9Sstevel@tonic-gate 	tabptr->zone_rctl_valptr = NULL;
32887c478bd9Sstevel@tonic-gate 	for (val = cur->xmlChildrenNode; val != NULL; val = val->next) {
32897c478bd9Sstevel@tonic-gate 		valptr = (struct zone_rctlvaltab *)malloc(
32907c478bd9Sstevel@tonic-gate 		    sizeof (struct zone_rctlvaltab));
32917c478bd9Sstevel@tonic-gate 		if (valptr == NULL)
32927c478bd9Sstevel@tonic-gate 			return (Z_NOMEM);
32937c478bd9Sstevel@tonic-gate 		if (fetchprop(val, DTD_ATTR_PRIV, valptr->zone_rctlval_priv,
32947c478bd9Sstevel@tonic-gate 		    sizeof (valptr->zone_rctlval_priv)) != Z_OK)
32957c478bd9Sstevel@tonic-gate 			break;
32967c478bd9Sstevel@tonic-gate 		if (fetchprop(val, DTD_ATTR_LIMIT, valptr->zone_rctlval_limit,
32977c478bd9Sstevel@tonic-gate 		    sizeof (valptr->zone_rctlval_limit)) != Z_OK)
32987c478bd9Sstevel@tonic-gate 			break;
32997c478bd9Sstevel@tonic-gate 		if (fetchprop(val, DTD_ATTR_ACTION, valptr->zone_rctlval_action,
33007c478bd9Sstevel@tonic-gate 		    sizeof (valptr->zone_rctlval_action)) != Z_OK)
33017c478bd9Sstevel@tonic-gate 			break;
33027c478bd9Sstevel@tonic-gate 		if (zonecfg_add_rctl_value(tabptr, valptr) != Z_OK)
33037c478bd9Sstevel@tonic-gate 			break;
33047c478bd9Sstevel@tonic-gate 	}
33057c478bd9Sstevel@tonic-gate 
33067c478bd9Sstevel@tonic-gate 	handle->zone_dh_cur = cur->next;
33077c478bd9Sstevel@tonic-gate 	return (Z_OK);
33087c478bd9Sstevel@tonic-gate }
33097c478bd9Sstevel@tonic-gate 
33107c478bd9Sstevel@tonic-gate int
33117c478bd9Sstevel@tonic-gate zonecfg_endrctlent(zone_dochandle_t handle)
33127c478bd9Sstevel@tonic-gate {
33137c478bd9Sstevel@tonic-gate 	return (zonecfg_endent(handle));
33147c478bd9Sstevel@tonic-gate }
33157c478bd9Sstevel@tonic-gate 
33167c478bd9Sstevel@tonic-gate int
33177c478bd9Sstevel@tonic-gate zonecfg_setattrent(zone_dochandle_t handle)
33187c478bd9Sstevel@tonic-gate {
33197c478bd9Sstevel@tonic-gate 	return (zonecfg_setent(handle));
33207c478bd9Sstevel@tonic-gate }
33217c478bd9Sstevel@tonic-gate 
33227c478bd9Sstevel@tonic-gate int
33237c478bd9Sstevel@tonic-gate zonecfg_getattrent(zone_dochandle_t handle, struct zone_attrtab *tabptr)
33247c478bd9Sstevel@tonic-gate {
33257c478bd9Sstevel@tonic-gate 	xmlNodePtr cur;
33267c478bd9Sstevel@tonic-gate 	int err;
33277c478bd9Sstevel@tonic-gate 
33287c478bd9Sstevel@tonic-gate 	if (handle == NULL)
33297c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
33307c478bd9Sstevel@tonic-gate 
33317c478bd9Sstevel@tonic-gate 	if ((cur = handle->zone_dh_cur) == NULL)
33327c478bd9Sstevel@tonic-gate 		return (Z_NO_ENTRY);
33337c478bd9Sstevel@tonic-gate 
33347c478bd9Sstevel@tonic-gate 	for (; cur != NULL; cur = cur->next)
33357c478bd9Sstevel@tonic-gate 		if (!xmlStrcmp(cur->name, DTD_ELEM_ATTR))
33367c478bd9Sstevel@tonic-gate 			break;
33377c478bd9Sstevel@tonic-gate 	if (cur == NULL) {
33387c478bd9Sstevel@tonic-gate 		handle->zone_dh_cur = handle->zone_dh_top;
33397c478bd9Sstevel@tonic-gate 		return (Z_NO_ENTRY);
33407c478bd9Sstevel@tonic-gate 	}
33417c478bd9Sstevel@tonic-gate 
33427c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_NAME, tabptr->zone_attr_name,
33437c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_attr_name))) != Z_OK) {
33447c478bd9Sstevel@tonic-gate 		handle->zone_dh_cur = handle->zone_dh_top;
33457c478bd9Sstevel@tonic-gate 		return (err);
33467c478bd9Sstevel@tonic-gate 	}
33477c478bd9Sstevel@tonic-gate 
33487c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_TYPE, tabptr->zone_attr_type,
33497c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_attr_type))) != Z_OK) {
33507c478bd9Sstevel@tonic-gate 		handle->zone_dh_cur = handle->zone_dh_top;
33517c478bd9Sstevel@tonic-gate 		return (err);
33527c478bd9Sstevel@tonic-gate 	}
33537c478bd9Sstevel@tonic-gate 
33547c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_VALUE, tabptr->zone_attr_value,
33557c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_attr_value))) != Z_OK) {
33567c478bd9Sstevel@tonic-gate 		handle->zone_dh_cur = handle->zone_dh_top;
33577c478bd9Sstevel@tonic-gate 		return (err);
33587c478bd9Sstevel@tonic-gate 	}
33597c478bd9Sstevel@tonic-gate 
33607c478bd9Sstevel@tonic-gate 	handle->zone_dh_cur = cur->next;
33617c478bd9Sstevel@tonic-gate 	return (Z_OK);
33627c478bd9Sstevel@tonic-gate }
33637c478bd9Sstevel@tonic-gate 
33647c478bd9Sstevel@tonic-gate int
33657c478bd9Sstevel@tonic-gate zonecfg_endattrent(zone_dochandle_t handle)
33667c478bd9Sstevel@tonic-gate {
33677c478bd9Sstevel@tonic-gate 	return (zonecfg_endent(handle));
33687c478bd9Sstevel@tonic-gate }
33697c478bd9Sstevel@tonic-gate 
3370ffbafc53Scomay /*
3371ffbafc53Scomay  * The privileges available on the system and described in privileges(5)
3372ffbafc53Scomay  * fall into four categories with respect to non-global zones; those that
3373ffbafc53Scomay  * are required in order for a non-global zone to boot, those which are in
3374ffbafc53Scomay  * the default set of privileges available to non-global zones, those
3375ffbafc53Scomay  * privileges which should not be allowed to be given to non-global zones
3376ffbafc53Scomay  * and all other privileges, which are optional and potentially useful for
3377ffbafc53Scomay  * processes executing inside a non-global zone.
3378ffbafc53Scomay  *
3379ffbafc53Scomay  * When privileges are added to the system, a determination needs to be
3380ffbafc53Scomay  * made as to which category the privilege belongs to.  Ideally,
3381ffbafc53Scomay  * privileges should be fine-grained enough and the mechanisms they cover
3382ffbafc53Scomay  * virtualized enough so that they can be made available to non-global
3383ffbafc53Scomay  * zones.
3384ffbafc53Scomay  */
3385ffbafc53Scomay 
3386ffbafc53Scomay /*
3387ffbafc53Scomay  * Set of privileges required in order to get a zone booted and init(1M)
3388ffbafc53Scomay  * started.  These cannot be removed from the zone's privilege set.
3389ffbafc53Scomay  */
3390ffbafc53Scomay static const char *required_priv_list[] = {
3391ffbafc53Scomay 	PRIV_PROC_EXEC,
3392ffbafc53Scomay 	PRIV_PROC_FORK,
3393ffbafc53Scomay 	PRIV_SYS_MOUNT,
3394ffbafc53Scomay 	NULL
3395ffbafc53Scomay };
3396ffbafc53Scomay 
3397ffbafc53Scomay /*
3398ffbafc53Scomay  * Default set of privileges considered safe for all non-global zones.
3399ffbafc53Scomay  * These privileges are "safe" in the sense that a privileged process in
3400ffbafc53Scomay  * the zone cannot affect processes in other non-global zones on the
3401ffbafc53Scomay  * system or in the global zone.  Privileges which are considered by
3402ffbafc53Scomay  * default, "unsafe", include ones which affect a global resource, such as
3403ffbafc53Scomay  * the system clock or physical memory.
3404ffbafc53Scomay  */
3405ffbafc53Scomay static const char *default_priv_list[] = {
3406ffbafc53Scomay 	PRIV_CONTRACT_EVENT,
3407ffbafc53Scomay 	PRIV_CONTRACT_OBSERVER,
34087c478bd9Sstevel@tonic-gate 	PRIV_FILE_CHOWN,
34097c478bd9Sstevel@tonic-gate 	PRIV_FILE_CHOWN_SELF,
34107c478bd9Sstevel@tonic-gate 	PRIV_FILE_DAC_EXECUTE,
34117c478bd9Sstevel@tonic-gate 	PRIV_FILE_DAC_READ,
34127c478bd9Sstevel@tonic-gate 	PRIV_FILE_DAC_SEARCH,
34137c478bd9Sstevel@tonic-gate 	PRIV_FILE_DAC_WRITE,
34147c478bd9Sstevel@tonic-gate 	PRIV_FILE_OWNER,
34157c478bd9Sstevel@tonic-gate 	PRIV_FILE_SETID,
34167c478bd9Sstevel@tonic-gate 	PRIV_IPC_DAC_READ,
34177c478bd9Sstevel@tonic-gate 	PRIV_IPC_DAC_WRITE,
34187c478bd9Sstevel@tonic-gate 	PRIV_IPC_OWNER,
341945916cd2Sjpk 	PRIV_NET_BINDMLP,
34207c478bd9Sstevel@tonic-gate 	PRIV_NET_ICMPACCESS,
342145916cd2Sjpk 	PRIV_NET_MAC_AWARE,
34227c478bd9Sstevel@tonic-gate 	PRIV_NET_PRIVADDR,
34237c478bd9Sstevel@tonic-gate 	PRIV_PROC_CHROOT,
34247c478bd9Sstevel@tonic-gate 	PRIV_SYS_AUDIT,
34257c478bd9Sstevel@tonic-gate 	PRIV_PROC_AUDIT,
34267c478bd9Sstevel@tonic-gate 	PRIV_PROC_OWNER,
34277c478bd9Sstevel@tonic-gate 	PRIV_PROC_SETID,
34287c478bd9Sstevel@tonic-gate 	PRIV_PROC_TASKID,
34297c478bd9Sstevel@tonic-gate 	PRIV_SYS_ACCT,
34307c478bd9Sstevel@tonic-gate 	PRIV_SYS_ADMIN,
34317c478bd9Sstevel@tonic-gate 	PRIV_SYS_MOUNT,
34327c478bd9Sstevel@tonic-gate 	PRIV_SYS_NFS,
34337c478bd9Sstevel@tonic-gate 	PRIV_SYS_RESOURCE,
34347c478bd9Sstevel@tonic-gate 	NULL
34357c478bd9Sstevel@tonic-gate };
34367c478bd9Sstevel@tonic-gate 
3437ffbafc53Scomay /*
3438ffbafc53Scomay  * Set of privileges not currently permitted within a non-global zone.
3439ffbafc53Scomay  * Some of these privileges are overly broad and cover more than one
3440ffbafc53Scomay  * mechanism in the system.  In other cases, there has not been sufficient
3441ffbafc53Scomay  * virtualization in the parts of the system the privilege covers to allow
3442ffbafc53Scomay  * its use within a non-global zone.
3443ffbafc53Scomay  */
3444ffbafc53Scomay static const char *prohibited_priv_list[] = {
3445ffbafc53Scomay 	PRIV_DTRACE_KERNEL,
3446ffbafc53Scomay 	PRIV_PROC_ZONE,
3447ffbafc53Scomay 	PRIV_SYS_CONFIG,
3448ffbafc53Scomay 	PRIV_SYS_DEVICES,
3449ffbafc53Scomay 	PRIV_SYS_LINKDIR,
3450ffbafc53Scomay 	PRIV_SYS_NET_CONFIG,
3451ffbafc53Scomay 	PRIV_SYS_RES_CONFIG,
3452ffbafc53Scomay 	PRIV_SYS_SUSER_COMPAT,
3453ffbafc53Scomay 	NULL
3454ffbafc53Scomay };
3455ffbafc53Scomay 
3456ffbafc53Scomay /*
3457ffbafc53Scomay  * Define some of the tokens that priv_str_to_set(3C) recognizes.  Since
3458ffbafc53Scomay  * the privilege string separator can be any character, although it is
3459ffbafc53Scomay  * usually a comma character, define these here as well in the event that
3460ffbafc53Scomay  * they change or are augmented in the future.
3461ffbafc53Scomay  */
3462ffbafc53Scomay #define	BASIC_TOKEN		"basic"
3463ffbafc53Scomay #define	DEFAULT_TOKEN		"default"
3464ffbafc53Scomay #define	ZONE_TOKEN		"zone"
3465ffbafc53Scomay #define	TOKEN_PRIV_CHAR		','
3466ffbafc53Scomay #define	TOKEN_PRIV_STR		","
3467ffbafc53Scomay 
34687c478bd9Sstevel@tonic-gate int
3469ffbafc53Scomay zonecfg_default_privset(priv_set_t *privs)
34707c478bd9Sstevel@tonic-gate {
34717c478bd9Sstevel@tonic-gate 	const char **strp;
3472ffbafc53Scomay 	priv_set_t *basic;
34737c478bd9Sstevel@tonic-gate 
3474ffbafc53Scomay 	basic = priv_str_to_set(BASIC_TOKEN, TOKEN_PRIV_STR, NULL);
34757c478bd9Sstevel@tonic-gate 	if (basic == NULL)
3476ffbafc53Scomay 		return (errno == ENOMEM ? Z_NOMEM : Z_INVAL);
34777c478bd9Sstevel@tonic-gate 
34787c478bd9Sstevel@tonic-gate 	priv_union(basic, privs);
34797c478bd9Sstevel@tonic-gate 	priv_freeset(basic);
34807c478bd9Sstevel@tonic-gate 
3481ffbafc53Scomay 	for (strp = default_priv_list; *strp != NULL; strp++) {
34827c478bd9Sstevel@tonic-gate 		if (priv_addset(privs, *strp) != 0) {
34837c478bd9Sstevel@tonic-gate 			return (Z_INVAL);
34847c478bd9Sstevel@tonic-gate 		}
34857c478bd9Sstevel@tonic-gate 	}
34867c478bd9Sstevel@tonic-gate 	return (Z_OK);
34877c478bd9Sstevel@tonic-gate }
34887c478bd9Sstevel@tonic-gate 
3489ffbafc53Scomay void
3490ffbafc53Scomay append_priv_token(char *priv, char *str, size_t strlen)
3491ffbafc53Scomay {
3492ffbafc53Scomay 	if (*str != '\0')
3493ffbafc53Scomay 		(void) strlcat(str, TOKEN_PRIV_STR, strlen);
3494ffbafc53Scomay 	(void) strlcat(str, priv, strlen);
3495ffbafc53Scomay }
3496ffbafc53Scomay 
3497ffbafc53Scomay /*
3498ffbafc53Scomay  * Verify that the supplied string is a valid privilege limit set for a
3499ffbafc53Scomay  * non-global zone.  This string must not only be acceptable to
3500ffbafc53Scomay  * priv_str_to_set(3C) which parses it, but it also must resolve to a
3501ffbafc53Scomay  * privilege set that includes certain required privileges and lacks
3502ffbafc53Scomay  * certain prohibited privileges.
3503ffbafc53Scomay  */
3504ffbafc53Scomay static int
3505ffbafc53Scomay verify_privset(char *privbuf, priv_set_t *privs, char **privname,
3506ffbafc53Scomay     boolean_t add_default)
3507ffbafc53Scomay {
3508ffbafc53Scomay 	char *cp;
3509ffbafc53Scomay 	char *lasts;
3510ffbafc53Scomay 	size_t len;
3511ffbafc53Scomay 	priv_set_t *mergeset;
3512ffbafc53Scomay 	const char **strp;
3513ffbafc53Scomay 	char *tmp;
3514ffbafc53Scomay 	const char *token;
3515ffbafc53Scomay 
3516ffbafc53Scomay 	/*
3517ffbafc53Scomay 	 * The verification of the privilege string occurs in several
3518ffbafc53Scomay 	 * phases.  In the first phase, the supplied string is scanned for
3519ffbafc53Scomay 	 * the ZONE_TOKEN token which is not support as part of the
3520ffbafc53Scomay 	 * "limitpriv" property.
3521ffbafc53Scomay 	 *
3522ffbafc53Scomay 	 * Duplicate the supplied privilege string since strtok_r(3C)
3523ffbafc53Scomay 	 * tokenizes its input by null-terminating the tokens.
3524ffbafc53Scomay 	 */
3525ffbafc53Scomay 	if ((tmp = strdup(privbuf)) == NULL)
3526ffbafc53Scomay 		return (Z_NOMEM);
3527ffbafc53Scomay 	for (cp = strtok_r(tmp, TOKEN_PRIV_STR, &lasts); cp != NULL;
3528ffbafc53Scomay 	    cp = strtok_r(NULL, TOKEN_PRIV_STR, &lasts)) {
3529ffbafc53Scomay 		if (strcmp(cp, ZONE_TOKEN) == 0) {
3530ffbafc53Scomay 			free(tmp);
3531ffbafc53Scomay 			if ((*privname = strdup(ZONE_TOKEN)) == NULL)
3532ffbafc53Scomay 				return (Z_NOMEM);
3533ffbafc53Scomay 			else
3534ffbafc53Scomay 				return (Z_PRIV_UNKNOWN);
3535ffbafc53Scomay 		}
3536ffbafc53Scomay 	}
3537ffbafc53Scomay 	free(tmp);
3538ffbafc53Scomay 
3539ffbafc53Scomay 	if (add_default) {
3540ffbafc53Scomay 		/*
3541ffbafc53Scomay 		 * If DEFAULT_TOKEN was specified, a string needs to be
3542ffbafc53Scomay 		 * built containing the privileges from the default, safe
3543ffbafc53Scomay 		 * set along with those of the "limitpriv" property.
3544ffbafc53Scomay 		 */
3545ffbafc53Scomay 		len = strlen(privbuf) + sizeof (BASIC_TOKEN) + 2;
3546ffbafc53Scomay 		for (strp = default_priv_list; *strp != NULL; strp++)
3547ffbafc53Scomay 			len += strlen(*strp) + 1;
3548ffbafc53Scomay 		tmp = alloca(len);
3549ffbafc53Scomay 		*tmp = '\0';
3550ffbafc53Scomay 
3551ffbafc53Scomay 		append_priv_token(BASIC_TOKEN, tmp, len);
3552ffbafc53Scomay 		for (strp = default_priv_list; *strp != NULL; strp++)
3553ffbafc53Scomay 			append_priv_token((char *)*strp, tmp, len);
3554ffbafc53Scomay 		(void) strlcat(tmp, TOKEN_PRIV_STR, len);
3555ffbafc53Scomay 		(void) strlcat(tmp, privbuf, len);
3556ffbafc53Scomay 	} else {
3557ffbafc53Scomay 		tmp = privbuf;
3558ffbafc53Scomay 	}
3559ffbafc53Scomay 
3560ffbafc53Scomay 
3561ffbafc53Scomay 	/*
3562ffbafc53Scomay 	 * In the next phase, attempt to convert the merged privilege
3563ffbafc53Scomay 	 * string into a privilege set.  In the case of an error, either
3564ffbafc53Scomay 	 * there was a memory allocation failure or there was an invalid
3565ffbafc53Scomay 	 * privilege token in the string.  In either case, return an
3566ffbafc53Scomay 	 * appropriate error code but in the event of an invalid token,
3567ffbafc53Scomay 	 * allocate a string containing its name and return that back to
3568ffbafc53Scomay 	 * the caller.
3569ffbafc53Scomay 	 */
3570ffbafc53Scomay 	mergeset = priv_str_to_set(tmp, TOKEN_PRIV_STR, &token);
3571ffbafc53Scomay 	if (mergeset == NULL) {
3572ffbafc53Scomay 		if (token == NULL)
3573ffbafc53Scomay 			return (Z_NOMEM);
3574ffbafc53Scomay 		if ((cp = strchr(token, TOKEN_PRIV_CHAR)) != NULL)
3575ffbafc53Scomay 			*cp = '\0';
3576ffbafc53Scomay 		if ((*privname = strdup(token)) == NULL)
3577ffbafc53Scomay 			return (Z_NOMEM);
3578ffbafc53Scomay 		else
3579ffbafc53Scomay 			return (Z_PRIV_UNKNOWN);
3580ffbafc53Scomay 	}
3581ffbafc53Scomay 
3582ffbafc53Scomay 	/*
3583ffbafc53Scomay 	 * Next, verify that none of the prohibited zone privileges are
3584ffbafc53Scomay 	 * present in the merged privilege set.
3585ffbafc53Scomay 	 */
3586ffbafc53Scomay 	for (strp = prohibited_priv_list; *strp != NULL; strp++) {
3587ffbafc53Scomay 		if (priv_ismember(mergeset, *strp)) {
3588ffbafc53Scomay 			priv_freeset(mergeset);
3589ffbafc53Scomay 			if ((*privname = strdup(*strp)) == NULL)
3590ffbafc53Scomay 				return (Z_NOMEM);
3591ffbafc53Scomay 			else
3592ffbafc53Scomay 				return (Z_PRIV_PROHIBITED);
3593ffbafc53Scomay 		}
3594ffbafc53Scomay 	}
3595ffbafc53Scomay 
3596ffbafc53Scomay 	/*
3597ffbafc53Scomay 	 * Finally, verify that all of the required zone privileges are
3598ffbafc53Scomay 	 * present in the merged privilege set.
3599ffbafc53Scomay 	 */
3600ffbafc53Scomay 	for (strp = required_priv_list; *strp != NULL; strp++) {
3601ffbafc53Scomay 		if (!priv_ismember(mergeset, *strp)) {
3602ffbafc53Scomay 			priv_freeset(mergeset);
3603ffbafc53Scomay 			if ((*privname = strdup(*strp)) == NULL)
3604ffbafc53Scomay 				return (Z_NOMEM);
3605ffbafc53Scomay 			else
3606ffbafc53Scomay 				return (Z_PRIV_REQUIRED);
3607ffbafc53Scomay 		}
3608ffbafc53Scomay 	}
3609ffbafc53Scomay 
3610ffbafc53Scomay 	priv_copyset(mergeset, privs);
3611ffbafc53Scomay 	priv_freeset(mergeset);
3612ffbafc53Scomay 	return (Z_OK);
3613ffbafc53Scomay }
3614ffbafc53Scomay 
3615ffbafc53Scomay /*
3616ffbafc53Scomay  * Fill in the supplied privilege set with either the default, safe set of
3617ffbafc53Scomay  * privileges suitable for a non-global zone, or one based on the
3618ffbafc53Scomay  * "limitpriv" property in the zone's configuration.
3619ffbafc53Scomay  *
3620ffbafc53Scomay  * In the event of an invalid privilege specification in the
3621ffbafc53Scomay  * configuration, a string is allocated and returned containing the
3622ffbafc53Scomay  * "privilege" causing the issue.  It is the caller's responsibility to
3623ffbafc53Scomay  * free this memory when it is done with it.
3624ffbafc53Scomay  */
3625ffbafc53Scomay int
3626ffbafc53Scomay zonecfg_get_privset(zone_dochandle_t handle, priv_set_t *privs,
3627ffbafc53Scomay     char **privname)
3628ffbafc53Scomay {
3629ffbafc53Scomay 	char *cp;
3630ffbafc53Scomay 	int err;
3631ffbafc53Scomay 	int limitlen;
3632ffbafc53Scomay 	char *limitpriv = NULL;
3633ffbafc53Scomay 
3634ffbafc53Scomay 	/*
3635ffbafc53Scomay 	 * Attempt to lookup the "limitpriv" property.  If it does not
3636ffbafc53Scomay 	 * exist or matches the string DEFAULT_TOKEN exactly, then the
3637ffbafc53Scomay 	 * default, safe privilege set is returned.
3638ffbafc53Scomay 	 */
3639ffbafc53Scomay 	err = zonecfg_get_limitpriv(handle, &limitpriv);
3640ffbafc53Scomay 	if (err != Z_OK)
3641ffbafc53Scomay 		return (err);
3642ffbafc53Scomay 	limitlen = strlen(limitpriv);
3643ffbafc53Scomay 	if (limitlen == 0 || strcmp(limitpriv, DEFAULT_TOKEN) == 0) {
3644ffbafc53Scomay 		free(limitpriv);
3645ffbafc53Scomay 		return (zonecfg_default_privset(privs));
3646ffbafc53Scomay 	}
3647ffbafc53Scomay 
3648ffbafc53Scomay 	/*
3649ffbafc53Scomay 	 * Check if the string DEFAULT_TOKEN is the first token in a list
3650ffbafc53Scomay 	 * of privileges.
3651ffbafc53Scomay 	 */
3652ffbafc53Scomay 	cp = strchr(limitpriv, TOKEN_PRIV_CHAR);
3653ffbafc53Scomay 	if (cp != NULL &&
3654ffbafc53Scomay 	    strncmp(limitpriv, DEFAULT_TOKEN, cp - limitpriv) == 0)
3655ffbafc53Scomay 		err = verify_privset(cp + 1, privs, privname, B_TRUE);
3656ffbafc53Scomay 	else
3657ffbafc53Scomay 		err = verify_privset(limitpriv, privs, privname, B_FALSE);
3658ffbafc53Scomay 
3659ffbafc53Scomay 	free(limitpriv);
3660ffbafc53Scomay 	return (err);
3661ffbafc53Scomay }
3662ffbafc53Scomay 
36637c478bd9Sstevel@tonic-gate int
36647c478bd9Sstevel@tonic-gate zone_get_zonepath(char *zone_name, char *zonepath, size_t rp_sz)
36657c478bd9Sstevel@tonic-gate {
36667c478bd9Sstevel@tonic-gate 	zone_dochandle_t handle;
36677c478bd9Sstevel@tonic-gate 	boolean_t found = B_FALSE;
36687c478bd9Sstevel@tonic-gate 	struct zoneent *ze;
36697c478bd9Sstevel@tonic-gate 	FILE *cookie;
36707c478bd9Sstevel@tonic-gate 	int err;
3671108322fbScarlsonj 	char *cp;
36727c478bd9Sstevel@tonic-gate 
36737c478bd9Sstevel@tonic-gate 	if (zone_name == NULL)
36747c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
36757c478bd9Sstevel@tonic-gate 
3676108322fbScarlsonj 	(void) strlcpy(zonepath, zonecfg_root, rp_sz);
3677108322fbScarlsonj 	cp = zonepath + strlen(zonepath);
3678108322fbScarlsonj 	while (cp > zonepath && cp[-1] == '/')
3679108322fbScarlsonj 		*--cp = '\0';
3680108322fbScarlsonj 
36817c478bd9Sstevel@tonic-gate 	if (strcmp(zone_name, GLOBAL_ZONENAME) == 0) {
3682108322fbScarlsonj 		if (zonepath[0] == '\0')
36837c478bd9Sstevel@tonic-gate 			(void) strlcpy(zonepath, "/", rp_sz);
36847c478bd9Sstevel@tonic-gate 		return (Z_OK);
36857c478bd9Sstevel@tonic-gate 	}
36867c478bd9Sstevel@tonic-gate 
36877c478bd9Sstevel@tonic-gate 	/*
36887c478bd9Sstevel@tonic-gate 	 * First check the index file.  Because older versions did not have
36897c478bd9Sstevel@tonic-gate 	 * a copy of the zone path, allow for it to be zero length, in which
36907c478bd9Sstevel@tonic-gate 	 * case we ignore this result and fall back to the XML files.
36917c478bd9Sstevel@tonic-gate 	 */
36927c478bd9Sstevel@tonic-gate 	cookie = setzoneent();
36937c478bd9Sstevel@tonic-gate 	while ((ze = getzoneent_private(cookie)) != NULL) {
36947c478bd9Sstevel@tonic-gate 		if (strcmp(ze->zone_name, zone_name) == 0) {
36957c478bd9Sstevel@tonic-gate 			found = B_TRUE;
3696108322fbScarlsonj 			if (ze->zone_path[0] != '\0')
3697108322fbScarlsonj 				(void) strlcpy(cp, ze->zone_path,
3698108322fbScarlsonj 				    rp_sz - (cp - zonepath));
36997c478bd9Sstevel@tonic-gate 		}
37007c478bd9Sstevel@tonic-gate 		free(ze);
37017c478bd9Sstevel@tonic-gate 		if (found)
37027c478bd9Sstevel@tonic-gate 			break;
37037c478bd9Sstevel@tonic-gate 	}
37047c478bd9Sstevel@tonic-gate 	endzoneent(cookie);
3705108322fbScarlsonj 	if (found && *cp != '\0')
37067c478bd9Sstevel@tonic-gate 		return (Z_OK);
37077c478bd9Sstevel@tonic-gate 
37087c478bd9Sstevel@tonic-gate 	/* Fall back to the XML files. */
37097c478bd9Sstevel@tonic-gate 	if ((handle = zonecfg_init_handle()) == NULL)
37107c478bd9Sstevel@tonic-gate 		return (Z_NOMEM);
37117c478bd9Sstevel@tonic-gate 
37127c478bd9Sstevel@tonic-gate 	/*
37137c478bd9Sstevel@tonic-gate 	 * Check the snapshot first: if a zone is running, its zonepath
37147c478bd9Sstevel@tonic-gate 	 * may have changed.
37157c478bd9Sstevel@tonic-gate 	 */
37167c478bd9Sstevel@tonic-gate 	if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) {
37177c478bd9Sstevel@tonic-gate 		if ((err = zonecfg_get_handle(zone_name, handle)) != Z_OK)
37187c478bd9Sstevel@tonic-gate 			return (err);
37197c478bd9Sstevel@tonic-gate 	}
37207c478bd9Sstevel@tonic-gate 	err = zonecfg_get_zonepath(handle, zonepath, rp_sz);
37217c478bd9Sstevel@tonic-gate 	zonecfg_fini_handle(handle);
37227c478bd9Sstevel@tonic-gate 	return (err);
37237c478bd9Sstevel@tonic-gate }
37247c478bd9Sstevel@tonic-gate 
37257c478bd9Sstevel@tonic-gate int
37267c478bd9Sstevel@tonic-gate zone_get_rootpath(char *zone_name, char *rootpath, size_t rp_sz)
37277c478bd9Sstevel@tonic-gate {
37287c478bd9Sstevel@tonic-gate 	int err;
37297c478bd9Sstevel@tonic-gate 
37307c478bd9Sstevel@tonic-gate 	/* This function makes sense for non-global zones only. */
37317c478bd9Sstevel@tonic-gate 	if (strcmp(zone_name, GLOBAL_ZONENAME) == 0)
37327c478bd9Sstevel@tonic-gate 		return (Z_BOGUS_ZONE_NAME);
37337c478bd9Sstevel@tonic-gate 	if ((err = zone_get_zonepath(zone_name, rootpath, rp_sz)) != Z_OK)
37347c478bd9Sstevel@tonic-gate 		return (err);
37357c478bd9Sstevel@tonic-gate 	if (strlcat(rootpath, "/root", rp_sz) >= rp_sz)
37367c478bd9Sstevel@tonic-gate 		return (Z_TOO_BIG);
37377c478bd9Sstevel@tonic-gate 	return (Z_OK);
37387c478bd9Sstevel@tonic-gate }
37397c478bd9Sstevel@tonic-gate 
37407c478bd9Sstevel@tonic-gate static zone_state_t
3741108322fbScarlsonj kernel_state_to_user_state(zoneid_t zoneid, zone_status_t kernel_state)
37427c478bd9Sstevel@tonic-gate {
3743108322fbScarlsonj 	char zoneroot[MAXPATHLEN];
3744108322fbScarlsonj 	size_t zlen;
3745108322fbScarlsonj 
37467c478bd9Sstevel@tonic-gate 	assert(kernel_state <= ZONE_MAX_STATE);
37477c478bd9Sstevel@tonic-gate 	switch (kernel_state) {
37487c478bd9Sstevel@tonic-gate 		case ZONE_IS_UNINITIALIZED:
3749108322fbScarlsonj 			return (ZONE_STATE_READY);
37507c478bd9Sstevel@tonic-gate 		case ZONE_IS_READY:
3751108322fbScarlsonj 			/*
3752108322fbScarlsonj 			 * If the zone's root is mounted on $ZONEPATH/lu, then
3753108322fbScarlsonj 			 * it's a mounted scratch zone.
3754108322fbScarlsonj 			 */
3755108322fbScarlsonj 			if (zone_getattr(zoneid, ZONE_ATTR_ROOT, zoneroot,
3756108322fbScarlsonj 			    sizeof (zoneroot)) >= 0) {
3757108322fbScarlsonj 				zlen = strlen(zoneroot);
3758108322fbScarlsonj 				if (zlen > 3 &&
3759108322fbScarlsonj 				    strcmp(zoneroot + zlen - 3, "/lu") == 0)
3760108322fbScarlsonj 					return (ZONE_STATE_MOUNTED);
3761108322fbScarlsonj 			}
37627c478bd9Sstevel@tonic-gate 			return (ZONE_STATE_READY);
37637c478bd9Sstevel@tonic-gate 		case ZONE_IS_BOOTING:
37647c478bd9Sstevel@tonic-gate 		case ZONE_IS_RUNNING:
37657c478bd9Sstevel@tonic-gate 			return (ZONE_STATE_RUNNING);
37667c478bd9Sstevel@tonic-gate 		case ZONE_IS_SHUTTING_DOWN:
37677c478bd9Sstevel@tonic-gate 		case ZONE_IS_EMPTY:
37687c478bd9Sstevel@tonic-gate 			return (ZONE_STATE_SHUTTING_DOWN);
37697c478bd9Sstevel@tonic-gate 		case ZONE_IS_DOWN:
37707c478bd9Sstevel@tonic-gate 		case ZONE_IS_DYING:
37717c478bd9Sstevel@tonic-gate 		case ZONE_IS_DEAD:
37727c478bd9Sstevel@tonic-gate 		default:
37737c478bd9Sstevel@tonic-gate 			return (ZONE_STATE_DOWN);
37747c478bd9Sstevel@tonic-gate 	}
37757c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
37767c478bd9Sstevel@tonic-gate }
37777c478bd9Sstevel@tonic-gate 
37787c478bd9Sstevel@tonic-gate int
37797c478bd9Sstevel@tonic-gate zone_get_state(char *zone_name, zone_state_t *state_num)
37807c478bd9Sstevel@tonic-gate {
37817c478bd9Sstevel@tonic-gate 	zone_status_t status;
37827c478bd9Sstevel@tonic-gate 	zoneid_t zone_id;
37837c478bd9Sstevel@tonic-gate 	struct zoneent *ze;
37847c478bd9Sstevel@tonic-gate 	boolean_t found = B_FALSE;
37857c478bd9Sstevel@tonic-gate 	FILE *cookie;
3786108322fbScarlsonj 	char kernzone[ZONENAME_MAX];
3787108322fbScarlsonj 	FILE *fp;
37887c478bd9Sstevel@tonic-gate 
37897c478bd9Sstevel@tonic-gate 	if (zone_name == NULL)
37907c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
37917c478bd9Sstevel@tonic-gate 
3792108322fbScarlsonj 	/*
3793108322fbScarlsonj 	 * If we're looking at an alternate root, then we need to query the
3794108322fbScarlsonj 	 * kernel using the scratch zone name.
3795108322fbScarlsonj 	 */
3796108322fbScarlsonj 	zone_id = -1;
3797108322fbScarlsonj 	if (*zonecfg_root != '\0' && !zonecfg_is_scratch(zone_name)) {
3798108322fbScarlsonj 		if ((fp = zonecfg_open_scratch("", B_FALSE)) != NULL) {
3799108322fbScarlsonj 			if (zonecfg_find_scratch(fp, zone_name, zonecfg_root,
3800108322fbScarlsonj 			    kernzone, sizeof (kernzone)) == 0)
3801108322fbScarlsonj 				zone_id = getzoneidbyname(kernzone);
3802108322fbScarlsonj 			zonecfg_close_scratch(fp);
3803108322fbScarlsonj 		}
3804108322fbScarlsonj 	} else {
3805108322fbScarlsonj 		zone_id = getzoneidbyname(zone_name);
3806108322fbScarlsonj 	}
3807108322fbScarlsonj 
38087c478bd9Sstevel@tonic-gate 	/* check to see if zone is running */
3809108322fbScarlsonj 	if (zone_id != -1 &&
38107c478bd9Sstevel@tonic-gate 	    zone_getattr(zone_id, ZONE_ATTR_STATUS, &status,
38117c478bd9Sstevel@tonic-gate 	    sizeof (status)) >= 0) {
3812108322fbScarlsonj 		*state_num = kernel_state_to_user_state(zone_id, status);
38137c478bd9Sstevel@tonic-gate 		return (Z_OK);
38147c478bd9Sstevel@tonic-gate 	}
38157c478bd9Sstevel@tonic-gate 
38167c478bd9Sstevel@tonic-gate 	cookie = setzoneent();
38177c478bd9Sstevel@tonic-gate 	while ((ze = getzoneent_private(cookie)) != NULL) {
38187c478bd9Sstevel@tonic-gate 		if (strcmp(ze->zone_name, zone_name) == 0) {
38197c478bd9Sstevel@tonic-gate 			found = B_TRUE;
38207c478bd9Sstevel@tonic-gate 			*state_num = ze->zone_state;
38217c478bd9Sstevel@tonic-gate 		}
38227c478bd9Sstevel@tonic-gate 		free(ze);
38237c478bd9Sstevel@tonic-gate 		if (found)
38247c478bd9Sstevel@tonic-gate 			break;
38257c478bd9Sstevel@tonic-gate 	}
38267c478bd9Sstevel@tonic-gate 	endzoneent(cookie);
38277c478bd9Sstevel@tonic-gate 	return ((found) ? Z_OK : Z_NO_ZONE);
38287c478bd9Sstevel@tonic-gate }
38297c478bd9Sstevel@tonic-gate 
38307c478bd9Sstevel@tonic-gate int
38317c478bd9Sstevel@tonic-gate zone_set_state(char *zone, zone_state_t state)
38327c478bd9Sstevel@tonic-gate {
38337c478bd9Sstevel@tonic-gate 	struct zoneent ze;
38347c478bd9Sstevel@tonic-gate 
38357c478bd9Sstevel@tonic-gate 	if (state != ZONE_STATE_CONFIGURED && state != ZONE_STATE_INSTALLED &&
38367c478bd9Sstevel@tonic-gate 	    state != ZONE_STATE_INCOMPLETE)
38377c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
38387c478bd9Sstevel@tonic-gate 
3839087719fdSdp 	bzero(&ze, sizeof (ze));
38407c478bd9Sstevel@tonic-gate 	(void) strlcpy(ze.zone_name, zone, sizeof (ze.zone_name));
38417c478bd9Sstevel@tonic-gate 	ze.zone_state = state;
38427c478bd9Sstevel@tonic-gate 	(void) strlcpy(ze.zone_path, "", sizeof (ze.zone_path));
38437c478bd9Sstevel@tonic-gate 	return (putzoneent(&ze, PZE_MODIFY));
38447c478bd9Sstevel@tonic-gate }
38457c478bd9Sstevel@tonic-gate 
38467c478bd9Sstevel@tonic-gate /*
38477c478bd9Sstevel@tonic-gate  * Get id (if any) for specified zone.  There are four possible outcomes:
38487c478bd9Sstevel@tonic-gate  * - If the string corresponds to the numeric id of an active (booted)
38497c478bd9Sstevel@tonic-gate  *   zone, sets *zip to the zone id and returns 0.
38507c478bd9Sstevel@tonic-gate  * - If the string corresponds to the name of an active (booted) zone,
38517c478bd9Sstevel@tonic-gate  *   sets *zip to the zone id and returns 0.
38527c478bd9Sstevel@tonic-gate  * - If the string is a name in the configuration but is not booted,
38537c478bd9Sstevel@tonic-gate  *   sets *zip to ZONE_ID_UNDEFINED and returns 0.
38547c478bd9Sstevel@tonic-gate  * - Otherwise, leaves *zip unchanged and returns -1.
38557c478bd9Sstevel@tonic-gate  *
38567c478bd9Sstevel@tonic-gate  * This function acts as an auxiliary filter on the function of the same
38577c478bd9Sstevel@tonic-gate  * name in libc; the linker binds to this version if libzonecfg exists,
38587c478bd9Sstevel@tonic-gate  * and the libc version if it doesn't.  Any changes to this version of
38597c478bd9Sstevel@tonic-gate  * the function should probably be reflected in the libc version as well.
38607c478bd9Sstevel@tonic-gate  */
38617c478bd9Sstevel@tonic-gate int
38627c478bd9Sstevel@tonic-gate zone_get_id(const char *str, zoneid_t *zip)
38637c478bd9Sstevel@tonic-gate {
38647c478bd9Sstevel@tonic-gate 	zone_dochandle_t hdl;
38657c478bd9Sstevel@tonic-gate 	zoneid_t zoneid;
38667c478bd9Sstevel@tonic-gate 	char *cp;
38677c478bd9Sstevel@tonic-gate 	int err;
38687c478bd9Sstevel@tonic-gate 
38697c478bd9Sstevel@tonic-gate 	/* first try looking for active zone by id */
38707c478bd9Sstevel@tonic-gate 	errno = 0;
38717c478bd9Sstevel@tonic-gate 	zoneid = (zoneid_t)strtol(str, &cp, 0);
38727c478bd9Sstevel@tonic-gate 	if (errno == 0 && cp != str && *cp == '\0' &&
38737c478bd9Sstevel@tonic-gate 	    getzonenamebyid(zoneid, NULL, 0) != -1) {
38747c478bd9Sstevel@tonic-gate 		*zip = zoneid;
38757c478bd9Sstevel@tonic-gate 		return (0);
38767c478bd9Sstevel@tonic-gate 	}
38777c478bd9Sstevel@tonic-gate 
38787c478bd9Sstevel@tonic-gate 	/* then look for active zone by name */
38797c478bd9Sstevel@tonic-gate 	if ((zoneid = getzoneidbyname(str)) != -1) {
38807c478bd9Sstevel@tonic-gate 		*zip = zoneid;
38817c478bd9Sstevel@tonic-gate 		return (0);
38827c478bd9Sstevel@tonic-gate 	}
38837c478bd9Sstevel@tonic-gate 
38847c478bd9Sstevel@tonic-gate 	/* if in global zone, try looking up name in configuration database */
38857c478bd9Sstevel@tonic-gate 	if (getzoneid() != GLOBAL_ZONEID ||
38867c478bd9Sstevel@tonic-gate 	    (hdl = zonecfg_init_handle()) == NULL)
38877c478bd9Sstevel@tonic-gate 		return (-1);
38887c478bd9Sstevel@tonic-gate 
3889108322fbScarlsonj 	if (zonecfg_get_handle(str, hdl) == Z_OK) {
38907c478bd9Sstevel@tonic-gate 		/* zone exists but isn't active */
38917c478bd9Sstevel@tonic-gate 		*zip = ZONE_ID_UNDEFINED;
38927c478bd9Sstevel@tonic-gate 		err = 0;
38937c478bd9Sstevel@tonic-gate 	} else {
38947c478bd9Sstevel@tonic-gate 		err = -1;
38957c478bd9Sstevel@tonic-gate 	}
38967c478bd9Sstevel@tonic-gate 
38977c478bd9Sstevel@tonic-gate 	zonecfg_fini_handle(hdl);
38987c478bd9Sstevel@tonic-gate 	return (err);
38997c478bd9Sstevel@tonic-gate }
39007c478bd9Sstevel@tonic-gate 
39017c478bd9Sstevel@tonic-gate char *
39027c478bd9Sstevel@tonic-gate zone_state_str(zone_state_t state_num)
39037c478bd9Sstevel@tonic-gate {
39047c478bd9Sstevel@tonic-gate 	switch (state_num) {
39057c478bd9Sstevel@tonic-gate 	case ZONE_STATE_CONFIGURED:
39067c478bd9Sstevel@tonic-gate 		return (ZONE_STATE_STR_CONFIGURED);
39077c478bd9Sstevel@tonic-gate 	case ZONE_STATE_INCOMPLETE:
39087c478bd9Sstevel@tonic-gate 		return (ZONE_STATE_STR_INCOMPLETE);
39097c478bd9Sstevel@tonic-gate 	case ZONE_STATE_INSTALLED:
39107c478bd9Sstevel@tonic-gate 		return (ZONE_STATE_STR_INSTALLED);
39117c478bd9Sstevel@tonic-gate 	case ZONE_STATE_READY:
39127c478bd9Sstevel@tonic-gate 		return (ZONE_STATE_STR_READY);
3913108322fbScarlsonj 	case ZONE_STATE_MOUNTED:
3914108322fbScarlsonj 		return (ZONE_STATE_STR_MOUNTED);
39157c478bd9Sstevel@tonic-gate 	case ZONE_STATE_RUNNING:
39167c478bd9Sstevel@tonic-gate 		return (ZONE_STATE_STR_RUNNING);
39177c478bd9Sstevel@tonic-gate 	case ZONE_STATE_SHUTTING_DOWN:
39187c478bd9Sstevel@tonic-gate 		return (ZONE_STATE_STR_SHUTTING_DOWN);
39197c478bd9Sstevel@tonic-gate 	case ZONE_STATE_DOWN:
39207c478bd9Sstevel@tonic-gate 		return (ZONE_STATE_STR_DOWN);
39217c478bd9Sstevel@tonic-gate 	default:
39227c478bd9Sstevel@tonic-gate 		return ("unknown");
39237c478bd9Sstevel@tonic-gate 	}
39247c478bd9Sstevel@tonic-gate }
39257c478bd9Sstevel@tonic-gate 
39267c478bd9Sstevel@tonic-gate /*
3927108322fbScarlsonj  * Given a UUID value, find an associated zone name.  This is intended to be
3928108322fbScarlsonj  * used by callers who set up some 'default' name (corresponding to the
3929108322fbScarlsonj  * expected name for the zone) in the zonename buffer, and thus the function
3930108322fbScarlsonj  * doesn't touch this buffer on failure.
3931108322fbScarlsonj  */
3932108322fbScarlsonj int
3933108322fbScarlsonj zonecfg_get_name_by_uuid(const uuid_t uuid, char *zonename, size_t namelen)
3934108322fbScarlsonj {
3935108322fbScarlsonj 	FILE *fp;
3936108322fbScarlsonj 	struct zoneent *ze;
3937108322fbScarlsonj 
3938108322fbScarlsonj 	/*
3939108322fbScarlsonj 	 * A small amount of subterfuge via casts is necessary here because
3940108322fbScarlsonj 	 * libuuid doesn't use const correctly, but we don't want to export
3941108322fbScarlsonj 	 * this brokenness to our clients.
3942108322fbScarlsonj 	 */
3943108322fbScarlsonj 	if (uuid_is_null(*(uuid_t *)&uuid))
3944108322fbScarlsonj 		return (Z_NO_ZONE);
3945108322fbScarlsonj 	if ((fp = setzoneent()) == NULL)
3946108322fbScarlsonj 		return (Z_NO_ZONE);
3947108322fbScarlsonj 	while ((ze = getzoneent_private(fp)) != NULL) {
3948108322fbScarlsonj 		if (uuid_compare(*(uuid_t *)&uuid, ze->zone_uuid) == 0)
3949108322fbScarlsonj 			break;
3950108322fbScarlsonj 		free(ze);
3951108322fbScarlsonj 	}
3952108322fbScarlsonj 	endzoneent(fp);
3953108322fbScarlsonj 	if (ze != NULL) {
3954108322fbScarlsonj 		(void) strlcpy(zonename, ze->zone_name, namelen);
3955108322fbScarlsonj 		free(ze);
3956108322fbScarlsonj 		return (Z_OK);
3957108322fbScarlsonj 	} else {
3958108322fbScarlsonj 		return (Z_NO_ZONE);
3959108322fbScarlsonj 	}
3960108322fbScarlsonj }
3961108322fbScarlsonj 
3962108322fbScarlsonj /*
3963108322fbScarlsonj  * Given a zone name, get its UUID.  Returns a "NULL" UUID value if the zone
3964108322fbScarlsonj  * exists but the file doesn't have a value set yet.  Returns an error if the
3965108322fbScarlsonj  * zone cannot be located.
3966108322fbScarlsonj  */
3967108322fbScarlsonj int
3968108322fbScarlsonj zonecfg_get_uuid(const char *zonename, uuid_t uuid)
3969108322fbScarlsonj {
3970108322fbScarlsonj 	FILE *fp;
3971108322fbScarlsonj 	struct zoneent *ze;
3972108322fbScarlsonj 
3973108322fbScarlsonj 	if ((fp = setzoneent()) == NULL)
3974108322fbScarlsonj 		return (Z_NO_ZONE);
3975108322fbScarlsonj 	while ((ze = getzoneent_private(fp)) != NULL) {
3976108322fbScarlsonj 		if (strcmp(ze->zone_name, zonename) == 0)
3977108322fbScarlsonj 			break;
3978108322fbScarlsonj 		free(ze);
3979108322fbScarlsonj 	}
3980108322fbScarlsonj 	endzoneent(fp);
3981108322fbScarlsonj 	if (ze != NULL) {
3982108322fbScarlsonj 		uuid_copy(uuid, ze->zone_uuid);
3983108322fbScarlsonj 		free(ze);
3984108322fbScarlsonj 		return (Z_OK);
3985108322fbScarlsonj 	} else {
3986108322fbScarlsonj 		return (Z_NO_ZONE);
3987108322fbScarlsonj 	}
3988108322fbScarlsonj }
3989108322fbScarlsonj 
3990108322fbScarlsonj /*
39917c478bd9Sstevel@tonic-gate  * File-system convenience functions.
39927c478bd9Sstevel@tonic-gate  */
39937c478bd9Sstevel@tonic-gate boolean_t
39947c478bd9Sstevel@tonic-gate zonecfg_valid_fs_type(const char *type)
39957c478bd9Sstevel@tonic-gate {
39967c478bd9Sstevel@tonic-gate 	/*
39977c478bd9Sstevel@tonic-gate 	 * We already know which FS types don't work.
39987c478bd9Sstevel@tonic-gate 	 */
39997c478bd9Sstevel@tonic-gate 	if (strcmp(type, "proc") == 0 ||
40007c478bd9Sstevel@tonic-gate 	    strcmp(type, "mntfs") == 0 ||
40017c478bd9Sstevel@tonic-gate 	    strcmp(type, "autofs") == 0 ||
40027c478bd9Sstevel@tonic-gate 	    strncmp(type, "nfs", sizeof ("nfs") - 1) == 0 ||
40037c478bd9Sstevel@tonic-gate 	    strcmp(type, "cachefs") == 0)
40047c478bd9Sstevel@tonic-gate 		return (B_FALSE);
40057c478bd9Sstevel@tonic-gate 	/*
40067c478bd9Sstevel@tonic-gate 	 * The caller may do more detailed verification to make sure other
40077c478bd9Sstevel@tonic-gate 	 * aspects of this filesystem type make sense.
40087c478bd9Sstevel@tonic-gate 	 */
40097c478bd9Sstevel@tonic-gate 	return (B_TRUE);
40107c478bd9Sstevel@tonic-gate }
40117c478bd9Sstevel@tonic-gate 
40127c478bd9Sstevel@tonic-gate /*
40137c478bd9Sstevel@tonic-gate  * Generally uninteresting rctl convenience functions.
40147c478bd9Sstevel@tonic-gate  */
40157c478bd9Sstevel@tonic-gate 
40167c478bd9Sstevel@tonic-gate int
40177c478bd9Sstevel@tonic-gate zonecfg_construct_rctlblk(const struct zone_rctlvaltab *rctlval,
40187c478bd9Sstevel@tonic-gate     rctlblk_t *rctlblk)
40197c478bd9Sstevel@tonic-gate {
40207c478bd9Sstevel@tonic-gate 	unsigned long long ull;
40217c478bd9Sstevel@tonic-gate 	char *endp;
40227c478bd9Sstevel@tonic-gate 	rctl_priv_t priv;
40237c478bd9Sstevel@tonic-gate 	rctl_qty_t limit;
40247c478bd9Sstevel@tonic-gate 	uint_t action;
40257c478bd9Sstevel@tonic-gate 
40267c478bd9Sstevel@tonic-gate 	/* Get the privilege */
40277c478bd9Sstevel@tonic-gate 	if (strcmp(rctlval->zone_rctlval_priv, "basic") == 0) {
40287c478bd9Sstevel@tonic-gate 		priv = RCPRIV_BASIC;
40297c478bd9Sstevel@tonic-gate 	} else if (strcmp(rctlval->zone_rctlval_priv, "privileged") == 0) {
40307c478bd9Sstevel@tonic-gate 		priv = RCPRIV_PRIVILEGED;
40317c478bd9Sstevel@tonic-gate 	} else {
40327c478bd9Sstevel@tonic-gate 		/* Invalid privilege */
40337c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
40347c478bd9Sstevel@tonic-gate 	}
40357c478bd9Sstevel@tonic-gate 
40367c478bd9Sstevel@tonic-gate 	/* deal with negative input; strtoull(3c) doesn't do what we want */
40377c478bd9Sstevel@tonic-gate 	if (rctlval->zone_rctlval_limit[0] == '-')
40387c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
40397c478bd9Sstevel@tonic-gate 	/* Get the limit */
40407c478bd9Sstevel@tonic-gate 	errno = 0;
40417c478bd9Sstevel@tonic-gate 	ull = strtoull(rctlval->zone_rctlval_limit, &endp, 0);
40427c478bd9Sstevel@tonic-gate 	if (errno != 0 || *endp != '\0') {
40437c478bd9Sstevel@tonic-gate 		/* parse failed */
40447c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
40457c478bd9Sstevel@tonic-gate 	}
40467c478bd9Sstevel@tonic-gate 	limit = (rctl_qty_t)ull;
40477c478bd9Sstevel@tonic-gate 
40487c478bd9Sstevel@tonic-gate 	/* Get the action */
40497c478bd9Sstevel@tonic-gate 	if (strcmp(rctlval->zone_rctlval_action, "none") == 0) {
40507c478bd9Sstevel@tonic-gate 		action = RCTL_LOCAL_NOACTION;
40517c478bd9Sstevel@tonic-gate 	} else if (strcmp(rctlval->zone_rctlval_action, "signal") == 0) {
40527c478bd9Sstevel@tonic-gate 		action = RCTL_LOCAL_SIGNAL;
40537c478bd9Sstevel@tonic-gate 	} else if (strcmp(rctlval->zone_rctlval_action, "deny") == 0) {
40547c478bd9Sstevel@tonic-gate 		action = RCTL_LOCAL_DENY;
40557c478bd9Sstevel@tonic-gate 	} else {
40567c478bd9Sstevel@tonic-gate 		/* Invalid Action */
40577c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
40587c478bd9Sstevel@tonic-gate 	}
40597c478bd9Sstevel@tonic-gate 	rctlblk_set_local_action(rctlblk, action, 0);
40607c478bd9Sstevel@tonic-gate 	rctlblk_set_privilege(rctlblk, priv);
40617c478bd9Sstevel@tonic-gate 	rctlblk_set_value(rctlblk, limit);
40627c478bd9Sstevel@tonic-gate 	return (Z_OK);
40637c478bd9Sstevel@tonic-gate }
40647c478bd9Sstevel@tonic-gate 
40657c478bd9Sstevel@tonic-gate static int
40667c478bd9Sstevel@tonic-gate rctl_check(const char *rctlname, void *arg)
40677c478bd9Sstevel@tonic-gate {
40687c478bd9Sstevel@tonic-gate 	const char *attrname = arg;
40697c478bd9Sstevel@tonic-gate 
40707c478bd9Sstevel@tonic-gate 	/*
40717c478bd9Sstevel@tonic-gate 	 * Returning 1 here is our signal to zonecfg_is_rctl() that it is
40727c478bd9Sstevel@tonic-gate 	 * indeed an rctl name recognized by the system.
40737c478bd9Sstevel@tonic-gate 	 */
40747c478bd9Sstevel@tonic-gate 	return (strcmp(rctlname, attrname) == 0 ? 1 : 0);
40757c478bd9Sstevel@tonic-gate }
40767c478bd9Sstevel@tonic-gate 
40777c478bd9Sstevel@tonic-gate boolean_t
40787c478bd9Sstevel@tonic-gate zonecfg_is_rctl(const char *name)
40797c478bd9Sstevel@tonic-gate {
40807c478bd9Sstevel@tonic-gate 	return (rctl_walk(rctl_check, (void *)name) == 1);
40817c478bd9Sstevel@tonic-gate }
40827c478bd9Sstevel@tonic-gate 
40837c478bd9Sstevel@tonic-gate boolean_t
40847c478bd9Sstevel@tonic-gate zonecfg_valid_rctlname(const char *name)
40857c478bd9Sstevel@tonic-gate {
40867c478bd9Sstevel@tonic-gate 	const char *c;
40877c478bd9Sstevel@tonic-gate 
40887c478bd9Sstevel@tonic-gate 	if (strncmp(name, "zone.", sizeof ("zone.") - 1) != 0)
40897c478bd9Sstevel@tonic-gate 		return (B_FALSE);
40907c478bd9Sstevel@tonic-gate 	if (strlen(name) == sizeof ("zone.") - 1)
40917c478bd9Sstevel@tonic-gate 		return (B_FALSE);
40927c478bd9Sstevel@tonic-gate 	for (c = name + sizeof ("zone.") - 1; *c != '\0'; c++) {
40937c478bd9Sstevel@tonic-gate 		if (!isalpha(*c) && *c != '-')
40947c478bd9Sstevel@tonic-gate 			return (B_FALSE);
40957c478bd9Sstevel@tonic-gate 	}
40967c478bd9Sstevel@tonic-gate 	return (B_TRUE);
40977c478bd9Sstevel@tonic-gate }
40987c478bd9Sstevel@tonic-gate 
40997c478bd9Sstevel@tonic-gate boolean_t
41007c478bd9Sstevel@tonic-gate zonecfg_valid_rctlblk(const rctlblk_t *rctlblk)
41017c478bd9Sstevel@tonic-gate {
41027c478bd9Sstevel@tonic-gate 	rctl_priv_t priv = rctlblk_get_privilege((rctlblk_t *)rctlblk);
41037c478bd9Sstevel@tonic-gate 	uint_t action = rctlblk_get_local_action((rctlblk_t *)rctlblk, NULL);
41047c478bd9Sstevel@tonic-gate 
41057c478bd9Sstevel@tonic-gate 	if (priv != RCPRIV_PRIVILEGED)
41067c478bd9Sstevel@tonic-gate 		return (B_FALSE);
41077c478bd9Sstevel@tonic-gate 	if (action != RCTL_LOCAL_NOACTION && action != RCTL_LOCAL_DENY)
41087c478bd9Sstevel@tonic-gate 		return (B_FALSE);
41097c478bd9Sstevel@tonic-gate 	return (B_TRUE);
41107c478bd9Sstevel@tonic-gate }
41117c478bd9Sstevel@tonic-gate 
41127c478bd9Sstevel@tonic-gate boolean_t
41137c478bd9Sstevel@tonic-gate zonecfg_valid_rctl(const char *name, const rctlblk_t *rctlblk)
41147c478bd9Sstevel@tonic-gate {
41157c478bd9Sstevel@tonic-gate 	rctlblk_t *current, *next;
41167c478bd9Sstevel@tonic-gate 	rctl_qty_t limit = rctlblk_get_value((rctlblk_t *)rctlblk);
41177c478bd9Sstevel@tonic-gate 	uint_t action = rctlblk_get_local_action((rctlblk_t *)rctlblk, NULL);
41187c478bd9Sstevel@tonic-gate 	uint_t global_flags;
41197c478bd9Sstevel@tonic-gate 
41207c478bd9Sstevel@tonic-gate 	if (!zonecfg_valid_rctlblk(rctlblk))
41217c478bd9Sstevel@tonic-gate 		return (B_FALSE);
41227c478bd9Sstevel@tonic-gate 	if (!zonecfg_valid_rctlname(name))
41237c478bd9Sstevel@tonic-gate 		return (B_FALSE);
41247c478bd9Sstevel@tonic-gate 
41257c478bd9Sstevel@tonic-gate 	current = alloca(rctlblk_size());
41267c478bd9Sstevel@tonic-gate 	if (getrctl(name, NULL, current, RCTL_FIRST) != 0)
41277c478bd9Sstevel@tonic-gate 		return (B_TRUE);	/* not an rctl on this system */
41287c478bd9Sstevel@tonic-gate 	/*
41297c478bd9Sstevel@tonic-gate 	 * Make sure the proposed value isn't greater than the current system
41307c478bd9Sstevel@tonic-gate 	 * value.
41317c478bd9Sstevel@tonic-gate 	 */
41327c478bd9Sstevel@tonic-gate 	next = alloca(rctlblk_size());
41337c478bd9Sstevel@tonic-gate 	while (rctlblk_get_privilege(current) != RCPRIV_SYSTEM) {
41347c478bd9Sstevel@tonic-gate 		rctlblk_t *tmp;
41357c478bd9Sstevel@tonic-gate 
41367c478bd9Sstevel@tonic-gate 		if (getrctl(name, current, next, RCTL_NEXT) != 0)
41377c478bd9Sstevel@tonic-gate 			return (B_FALSE);	/* shouldn't happen */
41387c478bd9Sstevel@tonic-gate 		tmp = current;
41397c478bd9Sstevel@tonic-gate 		current = next;
41407c478bd9Sstevel@tonic-gate 		next = tmp;
41417c478bd9Sstevel@tonic-gate 	}
41427c478bd9Sstevel@tonic-gate 	if (limit > rctlblk_get_value(current))
41437c478bd9Sstevel@tonic-gate 		return (B_FALSE);
41447c478bd9Sstevel@tonic-gate 
41457c478bd9Sstevel@tonic-gate 	/*
41467c478bd9Sstevel@tonic-gate 	 * Make sure the proposed action is allowed.
41477c478bd9Sstevel@tonic-gate 	 */
41487c478bd9Sstevel@tonic-gate 	global_flags = rctlblk_get_global_flags(current);
41497c478bd9Sstevel@tonic-gate 	if ((global_flags & RCTL_GLOBAL_DENY_NEVER) &&
41507c478bd9Sstevel@tonic-gate 	    action == RCTL_LOCAL_DENY)
41517c478bd9Sstevel@tonic-gate 		return (B_FALSE);
41527c478bd9Sstevel@tonic-gate 	if ((global_flags & RCTL_GLOBAL_DENY_ALWAYS) &&
41537c478bd9Sstevel@tonic-gate 	    action == RCTL_LOCAL_NOACTION)
41547c478bd9Sstevel@tonic-gate 		return (B_FALSE);
41557c478bd9Sstevel@tonic-gate 
41567c478bd9Sstevel@tonic-gate 	return (B_TRUE);
41577c478bd9Sstevel@tonic-gate }
4158fa9e4066Sahrens 
4159cf8f45c7Sdstaff /*
4160cf8f45c7Sdstaff  * There is always a race condition between reading the initial copy of
4161cf8f45c7Sdstaff  * a zones state and its state changing.  We address this by providing
4162cf8f45c7Sdstaff  * zonecfg_notify_critical_enter and zonecfg_noticy_critical_exit functions.
4163cf8f45c7Sdstaff  * When zonecfg_critical_enter is called, sets the state field to LOCKED
4164cf8f45c7Sdstaff  * and aquires biglock. Biglock protects against other threads executing
4165cf8f45c7Sdstaff  * critical_enter and the state field protects against state changes during
4166cf8f45c7Sdstaff  * the critical period.
4167cf8f45c7Sdstaff  *
4168cf8f45c7Sdstaff  * If any state changes occur, zn_cb will set the failed field of the znotify
4169cf8f45c7Sdstaff  * structure.  This will cause the critical_exit function to re-lock the
4170cf8f45c7Sdstaff  * channel and return an error. Since evsnts may be delayed, the critical_exit
4171cf8f45c7Sdstaff  * function "flushes" the queue by putting an event on the queue and waiting for
4172cf8f45c7Sdstaff  * zn_cb to notify critical_exit that it received the ping event.
4173cf8f45c7Sdstaff  */
4174cf8f45c7Sdstaff static const char *
4175cf8f45c7Sdstaff string_get_tok(const char *in, char delim, int num)
4176cf8f45c7Sdstaff {
4177cf8f45c7Sdstaff 	int i = 0;
4178cf8f45c7Sdstaff 
4179cf8f45c7Sdstaff 	for (; i < num; in++) {
4180cf8f45c7Sdstaff 		if (*in == delim)
4181cf8f45c7Sdstaff 			i++;
4182cf8f45c7Sdstaff 		if (*in == 0)
4183cf8f45c7Sdstaff 			return (NULL);
4184cf8f45c7Sdstaff 	}
4185cf8f45c7Sdstaff 	return (in);
4186cf8f45c7Sdstaff }
4187cf8f45c7Sdstaff 
4188cf8f45c7Sdstaff static boolean_t
4189cf8f45c7Sdstaff is_ping(sysevent_t *ev)
4190cf8f45c7Sdstaff {
4191cf8f45c7Sdstaff 	if (strcmp(sysevent_get_subclass_name(ev),
4192cf8f45c7Sdstaff 	    ZONE_EVENT_PING_SUBCLASS) == 0) {
4193cf8f45c7Sdstaff 		return (B_TRUE);
4194cf8f45c7Sdstaff 	} else {
4195cf8f45c7Sdstaff 		return (B_FALSE);
4196cf8f45c7Sdstaff 	}
4197cf8f45c7Sdstaff }
4198cf8f45c7Sdstaff 
4199cf8f45c7Sdstaff static boolean_t
4200cf8f45c7Sdstaff is_my_ping(sysevent_t *ev)
4201cf8f45c7Sdstaff {
4202cf8f45c7Sdstaff 	const char *sender;
4203cf8f45c7Sdstaff 	char mypid[sizeof (pid_t) * 3 + 1];
4204cf8f45c7Sdstaff 
4205cf8f45c7Sdstaff 	(void) snprintf(mypid, sizeof (mypid), "%i", getpid());
4206cf8f45c7Sdstaff 	sender = string_get_tok(sysevent_get_pub(ev), ':', 3);
4207cf8f45c7Sdstaff 	if (sender == NULL)
4208cf8f45c7Sdstaff 		return (B_FALSE);
4209cf8f45c7Sdstaff 	if (strcmp(sender, mypid) != 0)
4210cf8f45c7Sdstaff 		return (B_FALSE);
4211cf8f45c7Sdstaff 	return (B_TRUE);
4212cf8f45c7Sdstaff }
4213cf8f45c7Sdstaff 
4214cf8f45c7Sdstaff static int
4215cf8f45c7Sdstaff do_callback(struct znotify *zevtchan, sysevent_t *ev)
4216cf8f45c7Sdstaff {
4217cf8f45c7Sdstaff 	nvlist_t *l;
4218cf8f45c7Sdstaff 	int zid;
4219cf8f45c7Sdstaff 	char *zonename;
4220cf8f45c7Sdstaff 	char *newstate;
4221cf8f45c7Sdstaff 	char *oldstate;
4222cf8f45c7Sdstaff 	int ret;
4223cf8f45c7Sdstaff 	hrtime_t when;
4224cf8f45c7Sdstaff 
4225cf8f45c7Sdstaff 	if (strcmp(sysevent_get_subclass_name(ev),
4226cf8f45c7Sdstaff 	    ZONE_EVENT_STATUS_SUBCLASS) == 0) {
4227cf8f45c7Sdstaff 
4228cf8f45c7Sdstaff 		if (sysevent_get_attr_list(ev, &l) != 0) {
4229cf8f45c7Sdstaff 			if (errno == ENOMEM) {
4230cf8f45c7Sdstaff 				zevtchan->zn_failure_count++;
4231cf8f45c7Sdstaff 				return (EAGAIN);
4232cf8f45c7Sdstaff 			}
4233cf8f45c7Sdstaff 			return (0);
4234cf8f45c7Sdstaff 		}
4235cf8f45c7Sdstaff 		ret = 0;
4236cf8f45c7Sdstaff 
4237cf8f45c7Sdstaff 		if ((nvlist_lookup_string(l, ZONE_CB_NAME, &zonename) == 0) &&
4238cf8f45c7Sdstaff 		    (nvlist_lookup_string(l, ZONE_CB_NEWSTATE, &newstate)
4239cf8f45c7Sdstaff 		    == 0) &&
4240cf8f45c7Sdstaff 		    (nvlist_lookup_string(l, ZONE_CB_OLDSTATE, &oldstate)
4241cf8f45c7Sdstaff 		    == 0) &&
4242cf8f45c7Sdstaff 		    (nvlist_lookup_uint64(l, ZONE_CB_TIMESTAMP,
4243cf8f45c7Sdstaff 		    (uint64_t *)&when) == 0) &&
4244cf8f45c7Sdstaff 		    (nvlist_lookup_int32(l, ZONE_CB_ZONEID, &zid) == 0)) {
4245cf8f45c7Sdstaff 			ret = zevtchan->zn_callback(zonename, zid, newstate,
4246cf8f45c7Sdstaff 			    oldstate, when, zevtchan->zn_private);
4247cf8f45c7Sdstaff 		}
4248cf8f45c7Sdstaff 
4249cf8f45c7Sdstaff 		zevtchan->zn_failure_count = 0;
4250cf8f45c7Sdstaff 		nvlist_free(l);
4251cf8f45c7Sdstaff 		return (ret);
4252cf8f45c7Sdstaff 	} else {
4253cf8f45c7Sdstaff 		/*
4254cf8f45c7Sdstaff 		 * We have received an event in an unknown subclass. Ignore.
4255cf8f45c7Sdstaff 		 */
4256cf8f45c7Sdstaff 		zevtchan->zn_failure_count = 0;
4257cf8f45c7Sdstaff 		return (0);
4258cf8f45c7Sdstaff 	}
4259cf8f45c7Sdstaff }
4260cf8f45c7Sdstaff 
4261cf8f45c7Sdstaff static int
4262cf8f45c7Sdstaff zn_cb(sysevent_t *ev, void *p)
4263cf8f45c7Sdstaff {
4264cf8f45c7Sdstaff 	struct znotify *zevtchan = p;
4265cf8f45c7Sdstaff 	int error;
4266cf8f45c7Sdstaff 
4267cf8f45c7Sdstaff 	(void) pthread_mutex_lock(&(zevtchan->zn_mutex));
4268cf8f45c7Sdstaff 
4269cf8f45c7Sdstaff 	if (is_ping(ev) && !is_my_ping(ev)) {
4270cf8f45c7Sdstaff 		(void) pthread_mutex_unlock((&zevtchan->zn_mutex));
4271cf8f45c7Sdstaff 		return (0);
4272cf8f45c7Sdstaff 	}
4273cf8f45c7Sdstaff 
4274cf8f45c7Sdstaff 	if (zevtchan->zn_state == ZN_LOCKED) {
4275cf8f45c7Sdstaff 		assert(!is_ping(ev));
4276cf8f45c7Sdstaff 		zevtchan->zn_failed = B_TRUE;
4277cf8f45c7Sdstaff 		(void) pthread_mutex_unlock(&(zevtchan->zn_mutex));
4278cf8f45c7Sdstaff 		return (0);
4279cf8f45c7Sdstaff 	}
4280cf8f45c7Sdstaff 
4281cf8f45c7Sdstaff 	if (zevtchan->zn_state == ZN_PING_INFLIGHT) {
4282cf8f45c7Sdstaff 		if (is_ping(ev)) {
4283cf8f45c7Sdstaff 			zevtchan->zn_state = ZN_PING_RECEIVED;
4284cf8f45c7Sdstaff 			(void) pthread_cond_signal(&(zevtchan->zn_cond));
4285cf8f45c7Sdstaff 			(void) pthread_mutex_unlock(&(zevtchan->zn_mutex));
4286cf8f45c7Sdstaff 			return (0);
4287cf8f45c7Sdstaff 		} else {
4288cf8f45c7Sdstaff 			zevtchan->zn_failed = B_TRUE;
4289cf8f45c7Sdstaff 			(void) pthread_mutex_unlock(&(zevtchan->zn_mutex));
4290cf8f45c7Sdstaff 			return (0);
4291cf8f45c7Sdstaff 		}
4292cf8f45c7Sdstaff 	}
4293cf8f45c7Sdstaff 
4294cf8f45c7Sdstaff 	if (zevtchan->zn_state == ZN_UNLOCKED) {
4295cf8f45c7Sdstaff 
4296cf8f45c7Sdstaff 		error = do_callback(zevtchan, ev);
4297cf8f45c7Sdstaff 		(void) pthread_mutex_unlock(&(zevtchan->zn_mutex));
4298cf8f45c7Sdstaff 		/*
4299cf8f45c7Sdstaff 		 * Every ENOMEM failure causes do_callback to increment
4300cf8f45c7Sdstaff 		 * zn_failure_count and every success causes it to
4301cf8f45c7Sdstaff 		 * set zn_failure_count to zero.  If we got EAGAIN,
4302cf8f45c7Sdstaff 		 * we will sleep for zn_failure_count seconds and return
4303cf8f45c7Sdstaff 		 * EAGAIN to gpec to try again.
4304cf8f45c7Sdstaff 		 *
4305cf8f45c7Sdstaff 		 * After 55 seconds, or 10 try's we give up and drop the
4306cf8f45c7Sdstaff 		 * event.
4307cf8f45c7Sdstaff 		 */
4308cf8f45c7Sdstaff 		if (error == EAGAIN) {
4309cf8f45c7Sdstaff 			if (zevtchan->zn_failure_count > ZONE_CB_RETRY_COUNT) {
4310cf8f45c7Sdstaff 				return (0);
4311cf8f45c7Sdstaff 			}
4312cf8f45c7Sdstaff 			(void) sleep(zevtchan->zn_failure_count);
4313cf8f45c7Sdstaff 		}
4314cf8f45c7Sdstaff 		return (error);
4315cf8f45c7Sdstaff 	}
4316cf8f45c7Sdstaff 
4317cf8f45c7Sdstaff 	if (zevtchan->zn_state == ZN_PING_RECEIVED) {
4318cf8f45c7Sdstaff 		(void) pthread_mutex_unlock(&(zevtchan->zn_mutex));
4319cf8f45c7Sdstaff 		return (0);
4320cf8f45c7Sdstaff 	}
4321cf8f45c7Sdstaff 
4322cf8f45c7Sdstaff 	abort();
4323cf8f45c7Sdstaff 	return (0);
4324cf8f45c7Sdstaff }
4325cf8f45c7Sdstaff 
4326cf8f45c7Sdstaff void
4327cf8f45c7Sdstaff zonecfg_notify_critical_enter(void *h)
4328cf8f45c7Sdstaff {
4329cf8f45c7Sdstaff 	struct znotify *zevtchan = h;
4330cf8f45c7Sdstaff 
4331cf8f45c7Sdstaff 	(void) pthread_mutex_lock(&(zevtchan->zn_bigmutex));
4332cf8f45c7Sdstaff 	zevtchan->zn_state = ZN_LOCKED;
4333cf8f45c7Sdstaff }
4334cf8f45c7Sdstaff 
4335cf8f45c7Sdstaff int
4336cf8f45c7Sdstaff zonecfg_notify_critical_exit(void * h)
4337cf8f45c7Sdstaff {
4338cf8f45c7Sdstaff 
4339cf8f45c7Sdstaff 	struct znotify *zevtchan = h;
4340cf8f45c7Sdstaff 
4341cf8f45c7Sdstaff 	if (zevtchan->zn_state == ZN_UNLOCKED)
4342cf8f45c7Sdstaff 		return (0);
4343cf8f45c7Sdstaff 
4344cf8f45c7Sdstaff 	(void) pthread_mutex_lock(&(zevtchan->zn_mutex));
4345cf8f45c7Sdstaff 	zevtchan->zn_state = ZN_PING_INFLIGHT;
4346cf8f45c7Sdstaff 
4347ee519a1fSgjelinek 	(void) sysevent_evc_publish(zevtchan->zn_eventchan,
4348ee519a1fSgjelinek 	    ZONE_EVENT_STATUS_CLASS,
4349cf8f45c7Sdstaff 	    ZONE_EVENT_PING_SUBCLASS, ZONE_EVENT_PING_PUBLISHER,
4350cf8f45c7Sdstaff 	    zevtchan->zn_subscriber_id, NULL, EVCH_SLEEP);
4351cf8f45c7Sdstaff 
4352cf8f45c7Sdstaff 	while (zevtchan->zn_state != ZN_PING_RECEIVED) {
4353cf8f45c7Sdstaff 		(void) pthread_cond_wait(&(zevtchan->zn_cond),
4354cf8f45c7Sdstaff 		    &(zevtchan->zn_mutex));
4355cf8f45c7Sdstaff 	}
4356cf8f45c7Sdstaff 
4357cf8f45c7Sdstaff 	if (zevtchan->zn_failed == B_TRUE) {
4358cf8f45c7Sdstaff 		zevtchan->zn_state = ZN_LOCKED;
4359cf8f45c7Sdstaff 		zevtchan->zn_failed = B_FALSE;
4360cf8f45c7Sdstaff 		(void) pthread_mutex_unlock(&(zevtchan->zn_mutex));
4361cf8f45c7Sdstaff 		return (1);
4362cf8f45c7Sdstaff 	}
4363cf8f45c7Sdstaff 
4364cf8f45c7Sdstaff 	zevtchan->zn_state = ZN_UNLOCKED;
4365cf8f45c7Sdstaff 	(void) pthread_mutex_unlock(&(zevtchan->zn_mutex));
4366cf8f45c7Sdstaff 	(void) pthread_mutex_unlock(&(zevtchan->zn_bigmutex));
4367cf8f45c7Sdstaff 	return (0);
4368cf8f45c7Sdstaff }
4369cf8f45c7Sdstaff 
4370cf8f45c7Sdstaff void
4371cf8f45c7Sdstaff zonecfg_notify_critical_abort(void *h)
4372cf8f45c7Sdstaff {
4373cf8f45c7Sdstaff 	struct znotify *zevtchan = h;
4374cf8f45c7Sdstaff 
4375cf8f45c7Sdstaff 	zevtchan->zn_state = ZN_UNLOCKED;
4376cf8f45c7Sdstaff 	zevtchan->zn_failed = B_FALSE;
4377cf8f45c7Sdstaff 	/*
4378cf8f45c7Sdstaff 	 * Don't do anything about zn_lock. If it is held, it could only be
4379cf8f45c7Sdstaff 	 * held by zn_cb and it will be unlocked soon.
4380cf8f45c7Sdstaff 	 */
4381cf8f45c7Sdstaff 	(void) pthread_mutex_unlock(&(zevtchan->zn_bigmutex));
4382cf8f45c7Sdstaff }
4383cf8f45c7Sdstaff 
4384cf8f45c7Sdstaff void *
4385cf8f45c7Sdstaff zonecfg_notify_bind(int(*func)(const char *zonename, zoneid_t zid,
4386cf8f45c7Sdstaff     const char *newstate, const char *oldstate, hrtime_t when, void *p),
4387cf8f45c7Sdstaff     void *p)
4388cf8f45c7Sdstaff {
4389cf8f45c7Sdstaff 	struct znotify *zevtchan;
4390cf8f45c7Sdstaff 	int i = 1;
4391cf8f45c7Sdstaff 	int r;
4392cf8f45c7Sdstaff 
4393cf8f45c7Sdstaff 	zevtchan = malloc(sizeof (struct znotify));
4394cf8f45c7Sdstaff 
4395cf8f45c7Sdstaff 	if (zevtchan == NULL)
4396cf8f45c7Sdstaff 		return (NULL);
4397cf8f45c7Sdstaff 
4398cf8f45c7Sdstaff 	zevtchan->zn_private = p;
4399cf8f45c7Sdstaff 	zevtchan->zn_callback = func;
4400cf8f45c7Sdstaff 	zevtchan->zn_state = ZN_UNLOCKED;
4401cf8f45c7Sdstaff 	zevtchan->zn_failed = B_FALSE;
4402cf8f45c7Sdstaff 
4403cf8f45c7Sdstaff 	if (pthread_mutex_init(&(zevtchan->zn_mutex), NULL))
44049d4be64eSdstaff 		goto out3;
4405cf8f45c7Sdstaff 	if (pthread_cond_init(&(zevtchan->zn_cond), NULL)) {
4406cf8f45c7Sdstaff 		(void) pthread_mutex_destroy(&(zevtchan->zn_mutex));
44079d4be64eSdstaff 		goto out3;
4408cf8f45c7Sdstaff 	}
4409cf8f45c7Sdstaff 	if (pthread_mutex_init(&(zevtchan->zn_bigmutex), NULL)) {
4410cf8f45c7Sdstaff 		(void) pthread_mutex_destroy(&(zevtchan->zn_mutex));
4411cf8f45c7Sdstaff 		(void) pthread_cond_destroy(&(zevtchan->zn_cond));
44129d4be64eSdstaff 		goto out3;
4413cf8f45c7Sdstaff 	}
4414cf8f45c7Sdstaff 
4415cf8f45c7Sdstaff 	if (sysevent_evc_bind(ZONE_EVENT_CHANNEL, &(zevtchan->zn_eventchan),
4416cf8f45c7Sdstaff 	    0) != 0)
4417cf8f45c7Sdstaff 		goto out2;
4418cf8f45c7Sdstaff 
4419cf8f45c7Sdstaff 	do {
4420cf8f45c7Sdstaff 		/*
4421cf8f45c7Sdstaff 		 * At 4 digits the subscriber ID gets too long and we have
4422cf8f45c7Sdstaff 		 * no chance of successfully registering.
4423cf8f45c7Sdstaff 		 */
4424cf8f45c7Sdstaff 		if (i > 999)
44259d4be64eSdstaff 			goto out1;
4426cf8f45c7Sdstaff 
4427cf8f45c7Sdstaff 		(void) sprintf(zevtchan->zn_subscriber_id, "zone_%li_%i",
4428cf8f45c7Sdstaff 		    getpid() % 999999l, i);
4429cf8f45c7Sdstaff 
4430cf8f45c7Sdstaff 		r = sysevent_evc_subscribe(zevtchan->zn_eventchan,
4431cf8f45c7Sdstaff 		    zevtchan->zn_subscriber_id, ZONE_EVENT_STATUS_CLASS, zn_cb,
4432cf8f45c7Sdstaff 		    zevtchan, 0);
4433cf8f45c7Sdstaff 
4434cf8f45c7Sdstaff 		i++;
4435cf8f45c7Sdstaff 
4436cf8f45c7Sdstaff 	} while (r);
4437cf8f45c7Sdstaff 
4438cf8f45c7Sdstaff 	return (zevtchan);
44399d4be64eSdstaff out1:
4440cf8f45c7Sdstaff 	sysevent_evc_unbind(zevtchan->zn_eventchan);
44419d4be64eSdstaff out2:
4442cf8f45c7Sdstaff 	(void) pthread_mutex_destroy(&zevtchan->zn_mutex);
4443cf8f45c7Sdstaff 	(void) pthread_cond_destroy(&zevtchan->zn_cond);
4444cf8f45c7Sdstaff 	(void) pthread_mutex_destroy(&(zevtchan->zn_bigmutex));
44459d4be64eSdstaff out3:
4446cf8f45c7Sdstaff 	free(zevtchan);
4447cf8f45c7Sdstaff 
4448cf8f45c7Sdstaff 	return (NULL);
4449cf8f45c7Sdstaff }
4450cf8f45c7Sdstaff 
4451cf8f45c7Sdstaff void
4452cf8f45c7Sdstaff zonecfg_notify_unbind(void *handle)
4453cf8f45c7Sdstaff {
4454cf8f45c7Sdstaff 
4455cf8f45c7Sdstaff 	int ret;
4456cf8f45c7Sdstaff 
4457cf8f45c7Sdstaff 	sysevent_evc_unbind(((struct znotify *)handle)->zn_eventchan);
4458cf8f45c7Sdstaff 	/*
4459cf8f45c7Sdstaff 	 * Check that all evc threads have gone away. This should be
4460cf8f45c7Sdstaff 	 * enforced by sysevent_evc_unbind.
4461cf8f45c7Sdstaff 	 */
4462cf8f45c7Sdstaff 	ret = pthread_mutex_trylock(&((struct znotify *)handle)->zn_mutex);
4463cf8f45c7Sdstaff 
4464cf8f45c7Sdstaff 	if (ret)
4465cf8f45c7Sdstaff 		abort();
4466cf8f45c7Sdstaff 
4467cf8f45c7Sdstaff 	(void) pthread_mutex_unlock(&((struct znotify *)handle)->zn_mutex);
4468cf8f45c7Sdstaff 	(void) pthread_mutex_destroy(&((struct znotify *)handle)->zn_mutex);
4469cf8f45c7Sdstaff 	(void) pthread_cond_destroy(&((struct znotify *)handle)->zn_cond);
4470cf8f45c7Sdstaff 	(void) pthread_mutex_destroy(&((struct znotify *)handle)->zn_bigmutex);
4471cf8f45c7Sdstaff 
4472cf8f45c7Sdstaff 	free(handle);
4473cf8f45c7Sdstaff }
4474cf8f45c7Sdstaff 
4475fa9e4066Sahrens static int
4476fa9e4066Sahrens zonecfg_add_ds_core(zone_dochandle_t handle, struct zone_dstab *tabptr)
4477fa9e4066Sahrens {
4478fa9e4066Sahrens 	xmlNodePtr newnode, cur = handle->zone_dh_cur;
4479fa9e4066Sahrens 	int err;
4480fa9e4066Sahrens 
4481fa9e4066Sahrens 	newnode = xmlNewTextChild(cur, NULL, DTD_ELEM_DATASET, NULL);
4482fa9e4066Sahrens 	if ((err = newprop(newnode, DTD_ATTR_NAME,
4483fa9e4066Sahrens 	    tabptr->zone_dataset_name)) != Z_OK)
4484fa9e4066Sahrens 		return (err);
4485fa9e4066Sahrens 	return (Z_OK);
4486fa9e4066Sahrens }
4487fa9e4066Sahrens 
4488fa9e4066Sahrens int
4489fa9e4066Sahrens zonecfg_add_ds(zone_dochandle_t handle, struct zone_dstab *tabptr)
4490fa9e4066Sahrens {
4491fa9e4066Sahrens 	int err;
4492fa9e4066Sahrens 
4493fa9e4066Sahrens 	if (tabptr == NULL)
4494fa9e4066Sahrens 		return (Z_INVAL);
4495fa9e4066Sahrens 
4496fa9e4066Sahrens 	if ((err = operation_prep(handle)) != Z_OK)
4497fa9e4066Sahrens 		return (err);
4498fa9e4066Sahrens 
4499fa9e4066Sahrens 	if ((err = zonecfg_add_ds_core(handle, tabptr)) != Z_OK)
4500fa9e4066Sahrens 		return (err);
4501fa9e4066Sahrens 
4502fa9e4066Sahrens 	return (Z_OK);
4503fa9e4066Sahrens }
4504fa9e4066Sahrens 
4505fa9e4066Sahrens static int
4506fa9e4066Sahrens zonecfg_delete_ds_core(zone_dochandle_t handle, struct zone_dstab *tabptr)
4507fa9e4066Sahrens {
4508fa9e4066Sahrens 	xmlNodePtr cur = handle->zone_dh_cur;
4509fa9e4066Sahrens 
4510fa9e4066Sahrens 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
4511fa9e4066Sahrens 		if (xmlStrcmp(cur->name, DTD_ELEM_DATASET))
4512fa9e4066Sahrens 			continue;
4513fa9e4066Sahrens 
4514fa9e4066Sahrens 		if (match_prop(cur, DTD_ATTR_NAME,
4515fa9e4066Sahrens 		    tabptr->zone_dataset_name)) {
4516fa9e4066Sahrens 			xmlUnlinkNode(cur);
4517fa9e4066Sahrens 			xmlFreeNode(cur);
4518fa9e4066Sahrens 			return (Z_OK);
4519fa9e4066Sahrens 		}
4520fa9e4066Sahrens 	}
4521fa9e4066Sahrens 	return (Z_NO_RESOURCE_ID);
4522fa9e4066Sahrens }
4523fa9e4066Sahrens 
4524fa9e4066Sahrens int
4525fa9e4066Sahrens zonecfg_delete_ds(zone_dochandle_t handle, struct zone_dstab *tabptr)
4526fa9e4066Sahrens {
4527fa9e4066Sahrens 	int err;
4528fa9e4066Sahrens 
4529fa9e4066Sahrens 	if (tabptr == NULL)
4530fa9e4066Sahrens 		return (Z_INVAL);
4531fa9e4066Sahrens 
4532fa9e4066Sahrens 	if ((err = operation_prep(handle)) != Z_OK)
4533fa9e4066Sahrens 		return (err);
4534fa9e4066Sahrens 
4535fa9e4066Sahrens 	if ((err = zonecfg_delete_ds_core(handle, tabptr)) != Z_OK)
4536fa9e4066Sahrens 		return (err);
4537fa9e4066Sahrens 
4538fa9e4066Sahrens 	return (Z_OK);
4539fa9e4066Sahrens }
4540fa9e4066Sahrens 
4541fa9e4066Sahrens int
4542fa9e4066Sahrens zonecfg_modify_ds(
4543fa9e4066Sahrens 	zone_dochandle_t handle,
4544fa9e4066Sahrens 	struct zone_dstab *oldtabptr,
4545fa9e4066Sahrens 	struct zone_dstab *newtabptr)
4546fa9e4066Sahrens {
4547fa9e4066Sahrens 	int err;
4548fa9e4066Sahrens 
4549fa9e4066Sahrens 	if (oldtabptr == NULL || newtabptr == NULL)
4550fa9e4066Sahrens 		return (Z_INVAL);
4551fa9e4066Sahrens 
4552fa9e4066Sahrens 	if ((err = operation_prep(handle)) != Z_OK)
4553fa9e4066Sahrens 		return (err);
4554fa9e4066Sahrens 
4555fa9e4066Sahrens 	if ((err = zonecfg_delete_ds_core(handle, oldtabptr)) != Z_OK)
4556fa9e4066Sahrens 		return (err);
4557fa9e4066Sahrens 
4558fa9e4066Sahrens 	if ((err = zonecfg_add_ds_core(handle, newtabptr)) != Z_OK)
4559fa9e4066Sahrens 		return (err);
4560fa9e4066Sahrens 
4561fa9e4066Sahrens 	return (Z_OK);
4562fa9e4066Sahrens }
4563fa9e4066Sahrens 
4564fa9e4066Sahrens int
4565fa9e4066Sahrens zonecfg_lookup_ds(zone_dochandle_t handle, struct zone_dstab *tabptr)
4566fa9e4066Sahrens {
4567fa9e4066Sahrens 	xmlNodePtr cur, firstmatch;
4568fa9e4066Sahrens 	int err;
4569fa9e4066Sahrens 	char dataset[MAXNAMELEN];
4570fa9e4066Sahrens 
4571fa9e4066Sahrens 	if (tabptr == NULL)
4572fa9e4066Sahrens 		return (Z_INVAL);
4573fa9e4066Sahrens 
4574fa9e4066Sahrens 	if ((err = operation_prep(handle)) != Z_OK)
4575fa9e4066Sahrens 		return (err);
4576fa9e4066Sahrens 
4577fa9e4066Sahrens 	cur = handle->zone_dh_cur;
4578fa9e4066Sahrens 	firstmatch = NULL;
4579fa9e4066Sahrens 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
4580fa9e4066Sahrens 		if (xmlStrcmp(cur->name, DTD_ELEM_DATASET))
4581fa9e4066Sahrens 			continue;
4582fa9e4066Sahrens 		if (strlen(tabptr->zone_dataset_name) > 0) {
4583fa9e4066Sahrens 			if ((fetchprop(cur, DTD_ATTR_NAME, dataset,
4584fa9e4066Sahrens 			    sizeof (dataset)) == Z_OK) &&
4585fa9e4066Sahrens 			    (strcmp(tabptr->zone_dataset_name,
4586fa9e4066Sahrens 			    dataset) == 0)) {
4587fa9e4066Sahrens 				if (firstmatch == NULL)
4588fa9e4066Sahrens 					firstmatch = cur;
4589fa9e4066Sahrens 				else
4590fa9e4066Sahrens 					return (Z_INSUFFICIENT_SPEC);
4591fa9e4066Sahrens 			}
4592fa9e4066Sahrens 		}
4593fa9e4066Sahrens 	}
4594fa9e4066Sahrens 	if (firstmatch == NULL)
4595fa9e4066Sahrens 		return (Z_NO_RESOURCE_ID);
4596fa9e4066Sahrens 
4597fa9e4066Sahrens 	cur = firstmatch;
4598fa9e4066Sahrens 
4599fa9e4066Sahrens 	if ((err = fetchprop(cur, DTD_ATTR_NAME, tabptr->zone_dataset_name,
4600fa9e4066Sahrens 	    sizeof (tabptr->zone_dataset_name))) != Z_OK)
4601fa9e4066Sahrens 		return (err);
4602fa9e4066Sahrens 
4603fa9e4066Sahrens 	return (Z_OK);
4604fa9e4066Sahrens }
4605fa9e4066Sahrens 
4606fa9e4066Sahrens int
4607fa9e4066Sahrens zonecfg_setdsent(zone_dochandle_t handle)
4608fa9e4066Sahrens {
4609fa9e4066Sahrens 	return (zonecfg_setent(handle));
4610fa9e4066Sahrens }
4611fa9e4066Sahrens 
4612fa9e4066Sahrens int
4613fa9e4066Sahrens zonecfg_getdsent(zone_dochandle_t handle, struct zone_dstab *tabptr)
4614fa9e4066Sahrens {
4615fa9e4066Sahrens 	xmlNodePtr cur;
4616fa9e4066Sahrens 	int err;
4617fa9e4066Sahrens 
4618fa9e4066Sahrens 	if (handle == NULL)
4619fa9e4066Sahrens 		return (Z_INVAL);
4620fa9e4066Sahrens 
4621fa9e4066Sahrens 	if ((cur = handle->zone_dh_cur) == NULL)
4622fa9e4066Sahrens 		return (Z_NO_ENTRY);
4623fa9e4066Sahrens 
4624fa9e4066Sahrens 	for (; cur != NULL; cur = cur->next)
4625fa9e4066Sahrens 		if (!xmlStrcmp(cur->name, DTD_ELEM_DATASET))
4626fa9e4066Sahrens 			break;
4627fa9e4066Sahrens 	if (cur == NULL) {
4628fa9e4066Sahrens 		handle->zone_dh_cur = handle->zone_dh_top;
4629fa9e4066Sahrens 		return (Z_NO_ENTRY);
4630fa9e4066Sahrens 	}
4631fa9e4066Sahrens 
4632fa9e4066Sahrens 	if ((err = fetchprop(cur, DTD_ATTR_NAME, tabptr->zone_dataset_name,
4633fa9e4066Sahrens 	    sizeof (tabptr->zone_dataset_name))) != Z_OK) {
4634fa9e4066Sahrens 		handle->zone_dh_cur = handle->zone_dh_top;
4635fa9e4066Sahrens 		return (err);
4636fa9e4066Sahrens 	}
4637fa9e4066Sahrens 
4638fa9e4066Sahrens 	handle->zone_dh_cur = cur->next;
4639fa9e4066Sahrens 	return (Z_OK);
4640fa9e4066Sahrens }
4641fa9e4066Sahrens 
4642fa9e4066Sahrens int
4643fa9e4066Sahrens zonecfg_enddsent(zone_dochandle_t handle)
4644fa9e4066Sahrens {
4645fa9e4066Sahrens 	return (zonecfg_endent(handle));
4646fa9e4066Sahrens }
4647ee519a1fSgjelinek 
4648ee519a1fSgjelinek int
4649ee519a1fSgjelinek zonecfg_setpkgent(zone_dochandle_t handle)
4650ee519a1fSgjelinek {
4651ee519a1fSgjelinek 	return (zonecfg_setent(handle));
4652ee519a1fSgjelinek }
4653ee519a1fSgjelinek 
4654ee519a1fSgjelinek int
4655ee519a1fSgjelinek zonecfg_getpkgent(zone_dochandle_t handle, struct zone_pkgtab *tabptr)
4656ee519a1fSgjelinek {
4657ee519a1fSgjelinek 	xmlNodePtr cur;
4658ee519a1fSgjelinek 	int err;
4659ee519a1fSgjelinek 
4660ee519a1fSgjelinek 	if (handle == NULL)
4661ee519a1fSgjelinek 		return (Z_INVAL);
4662ee519a1fSgjelinek 
4663ee519a1fSgjelinek 	if ((cur = handle->zone_dh_cur) == NULL)
4664ee519a1fSgjelinek 		return (Z_NO_ENTRY);
4665ee519a1fSgjelinek 
4666ee519a1fSgjelinek 	for (; cur != NULL; cur = cur->next)
4667ee519a1fSgjelinek 		if (!xmlStrcmp(cur->name, DTD_ELEM_PACKAGE))
4668ee519a1fSgjelinek 			break;
4669ee519a1fSgjelinek 	if (cur == NULL) {
4670ee519a1fSgjelinek 		handle->zone_dh_cur = handle->zone_dh_top;
4671ee519a1fSgjelinek 		return (Z_NO_ENTRY);
4672ee519a1fSgjelinek 	}
4673ee519a1fSgjelinek 
4674ee519a1fSgjelinek 	if ((err = fetchprop(cur, DTD_ATTR_NAME, tabptr->zone_pkg_name,
4675ee519a1fSgjelinek 	    sizeof (tabptr->zone_pkg_name))) != Z_OK) {
4676ee519a1fSgjelinek 		handle->zone_dh_cur = handle->zone_dh_top;
4677ee519a1fSgjelinek 		return (err);
4678ee519a1fSgjelinek 	}
4679ee519a1fSgjelinek 
4680ee519a1fSgjelinek 	if ((err = fetchprop(cur, DTD_ATTR_VERSION, tabptr->zone_pkg_version,
4681ee519a1fSgjelinek 	    sizeof (tabptr->zone_pkg_version))) != Z_OK) {
4682ee519a1fSgjelinek 		handle->zone_dh_cur = handle->zone_dh_top;
4683ee519a1fSgjelinek 		return (err);
4684ee519a1fSgjelinek 	}
4685ee519a1fSgjelinek 
4686ee519a1fSgjelinek 	handle->zone_dh_cur = cur->next;
4687ee519a1fSgjelinek 	return (Z_OK);
4688ee519a1fSgjelinek }
4689ee519a1fSgjelinek 
4690ee519a1fSgjelinek int
4691ee519a1fSgjelinek zonecfg_endpkgent(zone_dochandle_t handle)
4692ee519a1fSgjelinek {
4693ee519a1fSgjelinek 	return (zonecfg_endent(handle));
4694ee519a1fSgjelinek }
4695ee519a1fSgjelinek 
4696ee519a1fSgjelinek int
4697ee519a1fSgjelinek zonecfg_setpatchent(zone_dochandle_t handle)
4698ee519a1fSgjelinek {
4699ee519a1fSgjelinek 	return (zonecfg_setent(handle));
4700ee519a1fSgjelinek }
4701ee519a1fSgjelinek 
4702ee519a1fSgjelinek int
4703ee519a1fSgjelinek zonecfg_getpatchent(zone_dochandle_t handle, struct zone_patchtab *tabptr)
4704ee519a1fSgjelinek {
4705ee519a1fSgjelinek 	xmlNodePtr cur;
4706ee519a1fSgjelinek 	int err;
4707ee519a1fSgjelinek 
4708ee519a1fSgjelinek 	if (handle == NULL)
4709ee519a1fSgjelinek 		return (Z_INVAL);
4710ee519a1fSgjelinek 
4711ee519a1fSgjelinek 	if ((cur = handle->zone_dh_cur) == NULL)
4712ee519a1fSgjelinek 		return (Z_NO_ENTRY);
4713ee519a1fSgjelinek 
4714ee519a1fSgjelinek 	for (; cur != NULL; cur = cur->next)
4715ee519a1fSgjelinek 		if (!xmlStrcmp(cur->name, DTD_ELEM_PATCH))
4716ee519a1fSgjelinek 			break;
4717ee519a1fSgjelinek 	if (cur == NULL) {
4718ee519a1fSgjelinek 		handle->zone_dh_cur = handle->zone_dh_top;
4719ee519a1fSgjelinek 		return (Z_NO_ENTRY);
4720ee519a1fSgjelinek 	}
4721ee519a1fSgjelinek 
4722ee519a1fSgjelinek 	if ((err = fetchprop(cur, DTD_ATTR_ID, tabptr->zone_patch_id,
4723ee519a1fSgjelinek 	    sizeof (tabptr->zone_patch_id))) != Z_OK) {
4724ee519a1fSgjelinek 		handle->zone_dh_cur = handle->zone_dh_top;
4725ee519a1fSgjelinek 		return (err);
4726ee519a1fSgjelinek 	}
4727ee519a1fSgjelinek 
4728ee519a1fSgjelinek 	handle->zone_dh_cur = cur->next;
4729ee519a1fSgjelinek 	return (Z_OK);
4730ee519a1fSgjelinek }
4731ee519a1fSgjelinek 
4732ee519a1fSgjelinek int
4733ee519a1fSgjelinek zonecfg_endpatchent(zone_dochandle_t handle)
4734ee519a1fSgjelinek {
4735ee519a1fSgjelinek 	return (zonecfg_endent(handle));
4736ee519a1fSgjelinek }
4737ee519a1fSgjelinek 
4738ee519a1fSgjelinek int
4739ee519a1fSgjelinek zonecfg_setdevperment(zone_dochandle_t handle)
4740ee519a1fSgjelinek {
4741ee519a1fSgjelinek 	return (zonecfg_setent(handle));
4742ee519a1fSgjelinek }
4743ee519a1fSgjelinek 
4744ee519a1fSgjelinek int
4745ee519a1fSgjelinek zonecfg_getdevperment(zone_dochandle_t handle, struct zone_devpermtab *tabptr)
4746ee519a1fSgjelinek {
4747ee519a1fSgjelinek 	xmlNodePtr cur;
4748ee519a1fSgjelinek 	int err;
4749ee519a1fSgjelinek 	char buf[128];
4750ee519a1fSgjelinek 
4751ee519a1fSgjelinek 	tabptr->zone_devperm_acl = NULL;
4752ee519a1fSgjelinek 
4753ee519a1fSgjelinek 	if (handle == NULL)
4754ee519a1fSgjelinek 		return (Z_INVAL);
4755ee519a1fSgjelinek 
4756ee519a1fSgjelinek 	if ((cur = handle->zone_dh_cur) == NULL)
4757ee519a1fSgjelinek 		return (Z_NO_ENTRY);
4758ee519a1fSgjelinek 
4759ee519a1fSgjelinek 	for (; cur != NULL; cur = cur->next)
4760ee519a1fSgjelinek 		if (!xmlStrcmp(cur->name, DTD_ELEM_DEV_PERM))
4761ee519a1fSgjelinek 			break;
4762ee519a1fSgjelinek 	if (cur == NULL) {
4763ee519a1fSgjelinek 		handle->zone_dh_cur = handle->zone_dh_top;
4764ee519a1fSgjelinek 		return (Z_NO_ENTRY);
4765ee519a1fSgjelinek 	}
4766ee519a1fSgjelinek 
4767ee519a1fSgjelinek 	if ((err = fetchprop(cur, DTD_ATTR_NAME, tabptr->zone_devperm_name,
4768ee519a1fSgjelinek 	    sizeof (tabptr->zone_devperm_name))) != Z_OK) {
4769ee519a1fSgjelinek 		handle->zone_dh_cur = handle->zone_dh_top;
4770ee519a1fSgjelinek 		return (err);
4771ee519a1fSgjelinek 	}
4772ee519a1fSgjelinek 
4773ee519a1fSgjelinek 	if ((err = fetchprop(cur, DTD_ATTR_UID, buf, sizeof (buf))) != Z_OK) {
4774ee519a1fSgjelinek 		handle->zone_dh_cur = handle->zone_dh_top;
4775ee519a1fSgjelinek 		return (err);
4776ee519a1fSgjelinek 	}
4777ee519a1fSgjelinek 	tabptr->zone_devperm_uid = (uid_t)atol(buf);
4778ee519a1fSgjelinek 
4779ee519a1fSgjelinek 	if ((err = fetchprop(cur, DTD_ATTR_GID, buf, sizeof (buf))) != Z_OK) {
4780ee519a1fSgjelinek 		handle->zone_dh_cur = handle->zone_dh_top;
4781ee519a1fSgjelinek 		return (err);
4782ee519a1fSgjelinek 	}
4783ee519a1fSgjelinek 	tabptr->zone_devperm_gid = (gid_t)atol(buf);
4784ee519a1fSgjelinek 
4785ee519a1fSgjelinek 	if ((err = fetchprop(cur, DTD_ATTR_MODE, buf, sizeof (buf))) != Z_OK) {
4786ee519a1fSgjelinek 		handle->zone_dh_cur = handle->zone_dh_top;
4787ee519a1fSgjelinek 		return (err);
4788ee519a1fSgjelinek 	}
4789ee519a1fSgjelinek 	tabptr->zone_devperm_mode = (mode_t)strtol(buf, (char **)NULL, 8);
4790ee519a1fSgjelinek 
4791ee519a1fSgjelinek 	if ((err = fetch_alloc_prop(cur, DTD_ATTR_ACL,
4792ee519a1fSgjelinek 	    &(tabptr->zone_devperm_acl))) != Z_OK) {
4793ee519a1fSgjelinek 		handle->zone_dh_cur = handle->zone_dh_top;
4794ee519a1fSgjelinek 		return (err);
4795ee519a1fSgjelinek 	}
4796ee519a1fSgjelinek 
4797ee519a1fSgjelinek 	handle->zone_dh_cur = cur->next;
4798ee519a1fSgjelinek 	return (Z_OK);
4799ee519a1fSgjelinek }
4800ee519a1fSgjelinek 
4801ee519a1fSgjelinek int
4802ee519a1fSgjelinek zonecfg_enddevperment(zone_dochandle_t handle)
4803ee519a1fSgjelinek {
4804ee519a1fSgjelinek 	return (zonecfg_endent(handle));
4805ee519a1fSgjelinek }
4806ee519a1fSgjelinek 
4807ee519a1fSgjelinek /*
4808ee519a1fSgjelinek  * Process a list of pkgs from an entry in the contents file, adding each pkg
4809ee519a1fSgjelinek  * name to the list of pkgs.
4810ee519a1fSgjelinek  *
4811ee519a1fSgjelinek  * It is possible for the pkg name to be preceeded by a special character
4812ee519a1fSgjelinek  * which indicates some bookkeeping information for pkging.  Check if the
4813ee519a1fSgjelinek  * first char is not an Alpha char.  If so, skip over it.
4814ee519a1fSgjelinek  */
4815ee519a1fSgjelinek static int
4816ee519a1fSgjelinek add_pkg_list(char *lastp, char ***plist, int *pcnt)
4817ee519a1fSgjelinek {
4818ee519a1fSgjelinek 	char	*p;
4819ee519a1fSgjelinek 	int	pkg_cnt = *pcnt;
4820ee519a1fSgjelinek 	char	**pkgs = *plist;
4821ee519a1fSgjelinek 	int	res = Z_OK;
4822ee519a1fSgjelinek 
4823ee519a1fSgjelinek 	while ((p = strtok_r(NULL, " ", &lastp)) != NULL) {
4824ee519a1fSgjelinek 		char	**tmpp;
4825ee519a1fSgjelinek 		int	i;
4826ee519a1fSgjelinek 
4827ee519a1fSgjelinek 		/* skip over any special pkg bookkeeping char */
4828ee519a1fSgjelinek 		if (!isalpha(*p))
4829ee519a1fSgjelinek 			p++;
4830ee519a1fSgjelinek 
4831ee519a1fSgjelinek 		/* Check if the pkg is already in the list */
4832ee519a1fSgjelinek 		for (i = 0; i < pkg_cnt; i++) {
4833ee519a1fSgjelinek 			if (strcmp(p, pkgs[i]) == 0)
4834ee519a1fSgjelinek 				break;
4835ee519a1fSgjelinek 		}
4836ee519a1fSgjelinek 
4837ee519a1fSgjelinek 		if (i < pkg_cnt)
4838ee519a1fSgjelinek 			continue;
4839ee519a1fSgjelinek 
4840ee519a1fSgjelinek 		/* The pkg is not in the list; add it. */
4841ee519a1fSgjelinek 		if ((tmpp = (char **)realloc(pkgs,
4842ee519a1fSgjelinek 		    sizeof (char *) * (pkg_cnt + 1))) == NULL) {
4843ee519a1fSgjelinek 			res = Z_NOMEM;
4844ee519a1fSgjelinek 			break;
4845ee519a1fSgjelinek 		}
4846ee519a1fSgjelinek 		pkgs = tmpp;
4847ee519a1fSgjelinek 
4848ee519a1fSgjelinek 		if ((pkgs[pkg_cnt] = strdup(p)) == NULL) {
4849ee519a1fSgjelinek 			res = Z_NOMEM;
4850ee519a1fSgjelinek 			break;
4851ee519a1fSgjelinek 		}
4852ee519a1fSgjelinek 		pkg_cnt++;
4853ee519a1fSgjelinek 	}
4854ee519a1fSgjelinek 
4855ee519a1fSgjelinek 	*plist = pkgs;
4856ee519a1fSgjelinek 	*pcnt = pkg_cnt;
4857ee519a1fSgjelinek 
4858ee519a1fSgjelinek 	return (res);
4859ee519a1fSgjelinek }
4860ee519a1fSgjelinek 
4861ee519a1fSgjelinek /*
4862ee519a1fSgjelinek  * Process an entry from the contents file (type "directory") and if the
4863ee519a1fSgjelinek  * directory path is in the list of paths, add the associated list of pkgs
4864ee519a1fSgjelinek  * to the pkg list.  The input parameter "entry" will be broken up by
4865ee519a1fSgjelinek  * the parser within this function so its value will be modified when this
4866ee519a1fSgjelinek  * function exits.
4867ee519a1fSgjelinek  *
4868ee519a1fSgjelinek  * The entries we are looking for will look something like:
4869ee519a1fSgjelinek  *	/usr d none 0755 root sys SUNWctpls SUNWidnl SUNWlibCf ....
4870ee519a1fSgjelinek  */
4871ee519a1fSgjelinek static int
4872ee519a1fSgjelinek get_path_pkgs(char *entry, char **paths, int cnt, char ***pkgs, int *pkg_cnt)
4873ee519a1fSgjelinek {
4874ee519a1fSgjelinek 	char	*f1;
4875ee519a1fSgjelinek 	char	*f2;
4876ee519a1fSgjelinek 	char	*lastp;
4877ee519a1fSgjelinek 	int	i;
4878ee519a1fSgjelinek 	int	res = Z_OK;
4879ee519a1fSgjelinek 
4880ee519a1fSgjelinek 	if ((f1 = strtok_r(entry, " ", &lastp)) == NULL ||
4881ee519a1fSgjelinek 	    (f2 = strtok_r(NULL, " ", &lastp)) == NULL || strcmp(f2, "d") != 0)
4882ee519a1fSgjelinek 		return (Z_OK);
4883ee519a1fSgjelinek 
4884ee519a1fSgjelinek 	/* Check if this directory entry is in the list of paths. */
4885ee519a1fSgjelinek 	for (i = 0; i < cnt; i++) {
4886ee519a1fSgjelinek 		if (fnmatch(paths[i], f1, FNM_PATHNAME) == 0) {
4887ee519a1fSgjelinek 			/*
4888ee519a1fSgjelinek 			 * We do want the pkgs for this path.  First, skip
4889ee519a1fSgjelinek 			 * over the next 4 fields in the entry so that we call
4890ee519a1fSgjelinek 			 * add_pkg_list starting with the pkg names.
4891ee519a1fSgjelinek 			 */
4892ee519a1fSgjelinek 			int j;
489307b574eeSgjelinek 			char	*nlp;
4894ee519a1fSgjelinek 
4895ee519a1fSgjelinek 			for (j = 0; j < 4 &&
4896ffbafc53Scomay 			    strtok_r(NULL, " ", &lastp) != NULL; j++)
4897ffbafc53Scomay 				;
4898ee519a1fSgjelinek 			/*
4899ee519a1fSgjelinek 			 * If there are < 4 fields this entry is corrupt,
4900ee519a1fSgjelinek 			 * just skip it.
4901ee519a1fSgjelinek 			 */
4902ee519a1fSgjelinek 			if (j < 4)
4903ee519a1fSgjelinek 				return (Z_OK);
4904ee519a1fSgjelinek 
490507b574eeSgjelinek 			/* strip newline from the line */
490607b574eeSgjelinek 			nlp = (lastp + strlen(lastp) - 1);
490707b574eeSgjelinek 			if (*nlp == '\n')
490807b574eeSgjelinek 				*nlp = '\0';
490907b574eeSgjelinek 
4910ee519a1fSgjelinek 			res = add_pkg_list(lastp, pkgs, pkg_cnt);
4911ee519a1fSgjelinek 			break;
4912ee519a1fSgjelinek 		}
4913ee519a1fSgjelinek 	}
4914ee519a1fSgjelinek 
4915ee519a1fSgjelinek 	return (res);
4916ee519a1fSgjelinek }
4917ee519a1fSgjelinek 
4918ee519a1fSgjelinek /*
4919ee519a1fSgjelinek  * Read an entry from a pkginfo or contents file.  Some of these lines can
4920ee519a1fSgjelinek  * either be arbitrarily long or be continued by a backslash at the end of
4921ee519a1fSgjelinek  * the line.  This function coalesces lines that are longer than the read
4922ee519a1fSgjelinek  * buffer, and lines that are continued, into one buffer which is returned.
4923ee519a1fSgjelinek  * The caller must free this memory.  NULL is returned when we hit EOF or
4924ee519a1fSgjelinek  * if we run out of memory (errno is set to ENOMEM).
4925ee519a1fSgjelinek  */
4926ee519a1fSgjelinek static char *
4927ee519a1fSgjelinek read_pkg_data(FILE *fp)
4928ee519a1fSgjelinek {
4929ee519a1fSgjelinek 	char *start;
4930ee519a1fSgjelinek 	char *inp;
4931ee519a1fSgjelinek 	char *p;
4932ee519a1fSgjelinek 	int char_cnt = 0;
4933ee519a1fSgjelinek 
4934ee519a1fSgjelinek 	errno = 0;
4935ee519a1fSgjelinek 	if ((start = (char *)malloc(PKGINFO_RD_LEN)) == NULL) {
4936ee519a1fSgjelinek 		errno = ENOMEM;
4937ee519a1fSgjelinek 		return (NULL);
4938ee519a1fSgjelinek 	}
4939ee519a1fSgjelinek 
4940ee519a1fSgjelinek 	inp = start;
4941ee519a1fSgjelinek 	while ((p = fgets(inp, PKGINFO_RD_LEN, fp)) != NULL) {
4942ee519a1fSgjelinek 		int len;
4943ee519a1fSgjelinek 
4944ee519a1fSgjelinek 		len = strlen(inp);
4945ee519a1fSgjelinek 		if (inp[len - 1] == '\n' &&
4946ee519a1fSgjelinek 		    (len == 1 || inp[len - 2] != '\\')) {
4947ee519a1fSgjelinek 			char_cnt = len;
4948ee519a1fSgjelinek 			break;
4949ee519a1fSgjelinek 		}
4950ee519a1fSgjelinek 
4951ee519a1fSgjelinek 		if (inp[len - 2] == '\\')
4952ee519a1fSgjelinek 			char_cnt += len - 2;
4953ee519a1fSgjelinek 		else
4954ee519a1fSgjelinek 			char_cnt += PKGINFO_RD_LEN - 1;
4955ee519a1fSgjelinek 
4956ee519a1fSgjelinek 		if ((p = realloc(start, char_cnt + PKGINFO_RD_LEN)) == NULL) {
4957ee519a1fSgjelinek 			errno = ENOMEM;
4958ee519a1fSgjelinek 			break;
4959ee519a1fSgjelinek 		}
4960ee519a1fSgjelinek 
4961ee519a1fSgjelinek 		start = p;
4962ee519a1fSgjelinek 		inp = start + char_cnt;
4963ee519a1fSgjelinek 	}
4964ee519a1fSgjelinek 
4965ee519a1fSgjelinek 	if (errno == ENOMEM || (p == NULL && char_cnt == 0)) {
4966ee519a1fSgjelinek 		free(start);
4967ee519a1fSgjelinek 		start = NULL;
4968ee519a1fSgjelinek 	}
4969ee519a1fSgjelinek 
4970ee519a1fSgjelinek 	return (start);
4971ee519a1fSgjelinek }
4972ee519a1fSgjelinek 
4973ee519a1fSgjelinek static void
4974ee519a1fSgjelinek free_ipd_pkgs(char **pkgs, int cnt)
4975ee519a1fSgjelinek {
4976ee519a1fSgjelinek 	int i;
4977ee519a1fSgjelinek 
4978ee519a1fSgjelinek 	for (i = 0; i < cnt; i++)
4979ee519a1fSgjelinek 		free(pkgs[i]);
4980ee519a1fSgjelinek 	free(pkgs);
4981ee519a1fSgjelinek }
4982ee519a1fSgjelinek 
4983ee519a1fSgjelinek /*
4984ee519a1fSgjelinek  * Get the list of inherited-pkg-dirs (ipd) for the zone and then get the
4985ee519a1fSgjelinek  * list of pkgs that deliver into those dirs.
4986ee519a1fSgjelinek  */
4987ee519a1fSgjelinek static int
4988ee519a1fSgjelinek get_ipd_pkgs(zone_dochandle_t handle, char ***pkg_list, int *cnt)
4989ee519a1fSgjelinek {
4990ee519a1fSgjelinek 	int	res;
4991ee519a1fSgjelinek 	struct zone_fstab fstab;
4992ee519a1fSgjelinek 	int	ipd_cnt = 0;
4993ee519a1fSgjelinek 	char	**ipds = NULL;
4994ee519a1fSgjelinek 	int	pkg_cnt = 0;
4995ee519a1fSgjelinek 	char	**pkgs = NULL;
4996ee519a1fSgjelinek 	int	i;
4997ee519a1fSgjelinek 
4998ee519a1fSgjelinek 	if ((res = zonecfg_setipdent(handle)) != Z_OK)
4999ee519a1fSgjelinek 		return (res);
5000ee519a1fSgjelinek 
5001ee519a1fSgjelinek 	while (zonecfg_getipdent(handle, &fstab) == Z_OK) {
5002ee519a1fSgjelinek 		char	**p;
5003ee519a1fSgjelinek 		int	len;
5004ee519a1fSgjelinek 
5005ee519a1fSgjelinek 		if ((p = (char **)realloc(ipds,
5006ee519a1fSgjelinek 		    sizeof (char *) * (ipd_cnt + 2))) == NULL) {
5007ee519a1fSgjelinek 			res = Z_NOMEM;
5008ee519a1fSgjelinek 			break;
5009ee519a1fSgjelinek 		}
5010ee519a1fSgjelinek 		ipds = p;
5011ee519a1fSgjelinek 
5012ee519a1fSgjelinek 		if ((ipds[ipd_cnt] = strdup(fstab.zone_fs_dir)) == NULL) {
5013ee519a1fSgjelinek 			res = Z_NOMEM;
5014ee519a1fSgjelinek 			break;
5015ee519a1fSgjelinek 		}
5016ee519a1fSgjelinek 		ipd_cnt++;
5017ee519a1fSgjelinek 
5018ee519a1fSgjelinek 		len = strlen(fstab.zone_fs_dir) + 3;
5019ee519a1fSgjelinek 		if ((ipds[ipd_cnt] = malloc(len)) == NULL) {
5020ee519a1fSgjelinek 			res = Z_NOMEM;
5021ee519a1fSgjelinek 			break;
5022ee519a1fSgjelinek 		}
5023ee519a1fSgjelinek 
5024ee519a1fSgjelinek 		(void) snprintf(ipds[ipd_cnt], len, "%s/*", fstab.zone_fs_dir);
5025ee519a1fSgjelinek 		ipd_cnt++;
5026ee519a1fSgjelinek 	}
5027ee519a1fSgjelinek 
5028ee519a1fSgjelinek 	(void) zonecfg_endipdent(handle);
5029ee519a1fSgjelinek 
5030ee519a1fSgjelinek 	if (res != Z_OK) {
5031ee519a1fSgjelinek 		for (i = 0; i < ipd_cnt; i++)
5032ee519a1fSgjelinek 			free(ipds[i]);
5033ee519a1fSgjelinek 		free(ipds);
5034ee519a1fSgjelinek 		return (res);
5035ee519a1fSgjelinek 	}
5036ee519a1fSgjelinek 
5037ee519a1fSgjelinek 	/* We only have to process the contents file if we have ipds. */
5038ee519a1fSgjelinek 	if (ipd_cnt > 0) {
5039ee519a1fSgjelinek 		FILE	*fp;
5040ee519a1fSgjelinek 
5041ee519a1fSgjelinek 		if ((fp = fopen(CONTENTS_FILE, "r")) != NULL) {
5042ee519a1fSgjelinek 			char	*buf;
5043ee519a1fSgjelinek 
5044ee519a1fSgjelinek 			while ((buf = read_pkg_data(fp)) != NULL) {
5045ee519a1fSgjelinek 				res = get_path_pkgs(buf, ipds, ipd_cnt, &pkgs,
5046ee519a1fSgjelinek 				    &pkg_cnt);
5047ee519a1fSgjelinek 				free(buf);
5048ee519a1fSgjelinek 				if (res != Z_OK)
5049ee519a1fSgjelinek 					break;
5050ee519a1fSgjelinek 			}
5051ee519a1fSgjelinek 
5052ee519a1fSgjelinek 			(void) fclose(fp);
5053ee519a1fSgjelinek 		}
5054ee519a1fSgjelinek 	}
5055ee519a1fSgjelinek 
5056ee519a1fSgjelinek 	for (i = 0; i < ipd_cnt; i++)
5057ee519a1fSgjelinek 		free(ipds[i]);
5058ee519a1fSgjelinek 	free(ipds);
5059ee519a1fSgjelinek 
5060ee519a1fSgjelinek 	if (res != Z_OK) {
5061ee519a1fSgjelinek 		free_ipd_pkgs(pkgs, pkg_cnt);
5062ee519a1fSgjelinek 	} else {
5063ee519a1fSgjelinek 		*pkg_list = pkgs;
5064ee519a1fSgjelinek 		*cnt = pkg_cnt;
5065ee519a1fSgjelinek 	}
5066ee519a1fSgjelinek 
5067ee519a1fSgjelinek 	return (res);
5068ee519a1fSgjelinek }
5069ee519a1fSgjelinek 
5070ee519a1fSgjelinek /*
5071ee519a1fSgjelinek  * Return true if pkg_name is in the list of pkgs that deliver into an
5072ee519a1fSgjelinek  * inherited pkg directory for the zone.
5073ee519a1fSgjelinek  */
5074ee519a1fSgjelinek static boolean_t
5075ee519a1fSgjelinek dir_pkg(char *pkg_name, char **pkg_list, int cnt)
5076ee519a1fSgjelinek {
5077ee519a1fSgjelinek 	int i;
5078ee519a1fSgjelinek 
5079ee519a1fSgjelinek 	for (i = 0; i < cnt; i++) {
5080ee519a1fSgjelinek 		if (strcmp(pkg_name, pkg_list[i]) == 0)
5081ee519a1fSgjelinek 			return (B_TRUE);
5082ee519a1fSgjelinek 	}
5083ee519a1fSgjelinek 
5084ee519a1fSgjelinek 	return (B_FALSE);
5085ee519a1fSgjelinek }
5086ee519a1fSgjelinek 
5087ee519a1fSgjelinek /*
5088ee519a1fSgjelinek  * Start by adding the patch to the sw inventory on the handle.
5089ee519a1fSgjelinek  *
5090ee519a1fSgjelinek  * The info parameter will be the portion of the PATCH_INFO_ entry following
5091ee519a1fSgjelinek  * the '='.  For example:
5092ee519a1fSgjelinek  * Installed: Wed Dec  7 07:13:51 PST 2005 From: mum Obsoletes: 120777-03 \
5093ee519a1fSgjelinek  *	121087-02 119108-07 Requires: 119575-02 119255-06 Incompatibles:
5094ee519a1fSgjelinek  *
509507b574eeSgjelinek  * A backed out patch will have an info line of "backed out\n".  We should
509607b574eeSgjelinek  * skip these patches.
509707b574eeSgjelinek  *
5098ee519a1fSgjelinek  * We also want to add the Obsolete and Incompatible patches to the
5099ee519a1fSgjelinek  * sw inventory description of this patch.
5100ee519a1fSgjelinek  */
5101ee519a1fSgjelinek static int
5102ee519a1fSgjelinek add_patch(zone_dochandle_t handle, char *patch, char *info)
5103ee519a1fSgjelinek {
5104ee519a1fSgjelinek 	xmlNodePtr	node;
5105ee519a1fSgjelinek 	xmlNodePtr	cur;
5106ee519a1fSgjelinek 	int		err;
5107ee519a1fSgjelinek 	char		*p;
5108ee519a1fSgjelinek 	char		*lastp;
5109ee519a1fSgjelinek 	boolean_t	add_info = B_FALSE;
5110ee519a1fSgjelinek 	boolean_t	obsolete;
5111ee519a1fSgjelinek 
511207b574eeSgjelinek 	if (strcmp(info, "backed out\n") == 0)
511307b574eeSgjelinek 		return (Z_OK);
511407b574eeSgjelinek 
5115ee519a1fSgjelinek 	if ((err = operation_prep(handle)) != Z_OK)
5116ee519a1fSgjelinek 		return (err);
5117ee519a1fSgjelinek 
5118ee519a1fSgjelinek 	cur = handle->zone_dh_cur;
5119ee519a1fSgjelinek 	node = xmlNewTextChild(cur, NULL, DTD_ELEM_PATCH, NULL);
5120ee519a1fSgjelinek 	if ((err = newprop(node, DTD_ATTR_ID, patch)) != Z_OK)
5121ee519a1fSgjelinek 		return (err);
5122ee519a1fSgjelinek 
5123ee519a1fSgjelinek 	/*
5124ee519a1fSgjelinek 	 * Start with the first token.  This will probably be "Installed:".
5125ee519a1fSgjelinek 	 * If we can't tokenize this entry, just return.
5126ee519a1fSgjelinek 	 */
5127ee519a1fSgjelinek 	if ((p = strtok_r(info, " ", &lastp)) == NULL)
5128ee519a1fSgjelinek 		return (Z_OK);
5129ee519a1fSgjelinek 
5130ee519a1fSgjelinek 	do {
5131ee519a1fSgjelinek 		xmlNodePtr new_node;
5132ee519a1fSgjelinek 		char	*nlp;
5133ee519a1fSgjelinek 
5134ee519a1fSgjelinek 		if (strcmp(p, "Installed:") == 0 ||
5135ee519a1fSgjelinek 		    strcmp(p, "Requires:") == 0 ||
5136ee519a1fSgjelinek 		    strcmp(p, "From:") == 0) {
5137ee519a1fSgjelinek 			add_info = B_FALSE;
5138ee519a1fSgjelinek 			continue;
5139ee519a1fSgjelinek 		} else if (strcmp(p, "Obsoletes:") == 0) {
5140ee519a1fSgjelinek 			obsolete = B_TRUE;
5141ee519a1fSgjelinek 			add_info = B_TRUE;
5142ee519a1fSgjelinek 			continue;
5143ee519a1fSgjelinek 		} else if (strcmp(p, "Incompatibles:") == 0) {
5144ee519a1fSgjelinek 			obsolete = B_FALSE;
5145ee519a1fSgjelinek 			add_info = B_TRUE;
5146ee519a1fSgjelinek 			continue;
5147ee519a1fSgjelinek 		}
5148ee519a1fSgjelinek 
5149ee519a1fSgjelinek 		if (!add_info)
5150ee519a1fSgjelinek 			continue;
5151ee519a1fSgjelinek 
5152ee519a1fSgjelinek 		/* strip newline from last patch in the line */
5153ee519a1fSgjelinek 		nlp = (p + strlen(p) - 1);
5154ee519a1fSgjelinek 		if (*nlp == '\n')
5155ee519a1fSgjelinek 			*nlp = '\0';
5156ee519a1fSgjelinek 
5157ee519a1fSgjelinek 		if (obsolete)
5158ee519a1fSgjelinek 			new_node = xmlNewTextChild(node, NULL,
5159ee519a1fSgjelinek 			    DTD_ELEM_OBSOLETES, NULL);
5160ee519a1fSgjelinek 		else
5161ee519a1fSgjelinek 			new_node = xmlNewTextChild(node, NULL,
5162ee519a1fSgjelinek 			    DTD_ELEM_INCOMPATIBLE, NULL);
5163ee519a1fSgjelinek 
5164ee519a1fSgjelinek 		if ((err = newprop(new_node, DTD_ATTR_ID, p)) != Z_OK)
5165ee519a1fSgjelinek 			return (err);
5166ee519a1fSgjelinek 
5167ee519a1fSgjelinek 	} while ((p = strtok_r(NULL, " ", &lastp)) != NULL);
5168ee519a1fSgjelinek 
5169ee519a1fSgjelinek 	return (Z_OK);
5170ee519a1fSgjelinek }
5171ee519a1fSgjelinek 
5172ee519a1fSgjelinek static boolean_t
5173ee519a1fSgjelinek unique_patch(zone_dochandle_t handle, char *patch)
5174ee519a1fSgjelinek {
5175ee519a1fSgjelinek 	xmlNodePtr	cur;
5176ee519a1fSgjelinek 	char		id[MAXNAMELEN];
5177ee519a1fSgjelinek 
5178ee519a1fSgjelinek 	cur = xmlDocGetRootElement(handle->zone_dh_doc);
5179ee519a1fSgjelinek 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
5180ee519a1fSgjelinek 		if (xmlStrcmp(cur->name, DTD_ELEM_PATCH) == 0) {
5181ee519a1fSgjelinek 			if (fetchprop(cur, DTD_ATTR_ID, id, sizeof (id))
5182ee519a1fSgjelinek 			    != Z_OK)
5183ee519a1fSgjelinek 				continue;
5184ee519a1fSgjelinek 
5185ee519a1fSgjelinek 			if (strcmp(patch, id) == 0)
5186ee519a1fSgjelinek 				return (B_FALSE);
5187ee519a1fSgjelinek 		}
5188ee519a1fSgjelinek 	}
5189ee519a1fSgjelinek 
5190ee519a1fSgjelinek 	return (B_TRUE);
5191ee519a1fSgjelinek }
5192ee519a1fSgjelinek 
5193ee519a1fSgjelinek /*
5194ee519a1fSgjelinek  * Add the unique patches associated with this pkg to the sw inventory on the
5195ee519a1fSgjelinek  * handle.
5196ee519a1fSgjelinek  *
5197ee519a1fSgjelinek  * We are processing entries of the form:
5198ee519a1fSgjelinek  * PATCH_INFO_121454-02=Installed: Wed Dec  7 07:13:51 PST 2005 From: mum \
5199ee519a1fSgjelinek  *	Obsoletes: 120777-03 121087-02 119108-07 Requires: 119575-02 \
5200ee519a1fSgjelinek  *	119255-06 Incompatibles:
5201ee519a1fSgjelinek  *
5202ee519a1fSgjelinek  */
5203ee519a1fSgjelinek static int
5204ee519a1fSgjelinek add_patches(zone_dochandle_t handle, struct zone_pkginfo *infop)
5205ee519a1fSgjelinek {
5206ee519a1fSgjelinek 	int i;
5207ee519a1fSgjelinek 	int res = Z_OK;
5208ee519a1fSgjelinek 
5209ee519a1fSgjelinek 	for (i = 0; i < infop->zpi_patch_cnt; i++) {
5210ee519a1fSgjelinek 		char *p, *ep;
5211ee519a1fSgjelinek 
5212ee519a1fSgjelinek 		if (strlen(infop->zpi_patchinfo[i]) < (sizeof (PATCHINFO) - 1))
5213ee519a1fSgjelinek 			continue;
5214ee519a1fSgjelinek 
5215ee519a1fSgjelinek 		/* Skip over "PATCH_INFO_" to get the patch id. */
5216ee519a1fSgjelinek 		p = infop->zpi_patchinfo[i] + sizeof (PATCHINFO) - 1;
5217ee519a1fSgjelinek 		if ((ep = strchr(p, '=')) == NULL)
5218ee519a1fSgjelinek 			continue;
5219ee519a1fSgjelinek 
5220ee519a1fSgjelinek 		*ep = '\0';
5221ee519a1fSgjelinek 		if (unique_patch(handle, p))
522207b574eeSgjelinek 			if ((res = add_patch(handle, p, ep + 1)) != Z_OK)
522307b574eeSgjelinek 				break;
5224ee519a1fSgjelinek 	}
5225ee519a1fSgjelinek 
5226ee519a1fSgjelinek 	return (res);
5227ee519a1fSgjelinek }
5228ee519a1fSgjelinek 
5229ee519a1fSgjelinek /*
5230ee519a1fSgjelinek  * Add the pkg to the sw inventory on the handle.
5231ee519a1fSgjelinek  */
5232ee519a1fSgjelinek static int
5233ee519a1fSgjelinek add_pkg(zone_dochandle_t handle, char *name, char *version)
5234ee519a1fSgjelinek {
5235ee519a1fSgjelinek 	xmlNodePtr newnode;
5236ee519a1fSgjelinek 	xmlNodePtr cur;
5237ee519a1fSgjelinek 	int err;
5238ee519a1fSgjelinek 
5239ee519a1fSgjelinek 	if ((err = operation_prep(handle)) != Z_OK)
5240ee519a1fSgjelinek 		return (err);
5241ee519a1fSgjelinek 
5242ee519a1fSgjelinek 	cur = handle->zone_dh_cur;
5243ee519a1fSgjelinek 	newnode = xmlNewTextChild(cur, NULL, DTD_ELEM_PACKAGE, NULL);
5244ee519a1fSgjelinek 	if ((err = newprop(newnode, DTD_ATTR_NAME, name)) != Z_OK)
5245ee519a1fSgjelinek 		return (err);
5246ee519a1fSgjelinek 	if ((err = newprop(newnode, DTD_ATTR_VERSION, version)) != Z_OK)
5247ee519a1fSgjelinek 		return (err);
5248ee519a1fSgjelinek 	return (Z_OK);
5249ee519a1fSgjelinek }
5250ee519a1fSgjelinek 
5251ee519a1fSgjelinek static void
5252ee519a1fSgjelinek free_pkginfo(struct zone_pkginfo *infop)
5253ee519a1fSgjelinek {
5254ee519a1fSgjelinek 	free(infop->zpi_version);
5255ee519a1fSgjelinek 	if (infop->zpi_patch_cnt > 0) {
5256ee519a1fSgjelinek 		int i;
5257ee519a1fSgjelinek 
5258ee519a1fSgjelinek 		for (i = 0; i < infop->zpi_patch_cnt; i++)
5259ee519a1fSgjelinek 			free(infop->zpi_patchinfo[i]);
5260ee519a1fSgjelinek 		free(infop->zpi_patchinfo);
5261ee519a1fSgjelinek 	}
5262ee519a1fSgjelinek }
5263ee519a1fSgjelinek 
5264ee519a1fSgjelinek /*
5265ee519a1fSgjelinek  * Read the pkginfo file and populate the structure with the data we need
5266ee519a1fSgjelinek  * from this pkg for a sw inventory.
5267ee519a1fSgjelinek  */
5268ee519a1fSgjelinek static int
5269ee519a1fSgjelinek get_pkginfo(char *pkginfo, struct zone_pkginfo *infop)
5270ee519a1fSgjelinek {
5271ee519a1fSgjelinek 	FILE	*fp;
5272ee519a1fSgjelinek 	char	*buf;
5273ee519a1fSgjelinek 	int	err = 0;
5274ee519a1fSgjelinek 
5275ee519a1fSgjelinek 	infop->zpi_all_zones = B_FALSE;
5276ee519a1fSgjelinek 	infop->zpi_this_zone = B_FALSE;
5277ee519a1fSgjelinek 	infop->zpi_version = NULL;
5278ee519a1fSgjelinek 	infop->zpi_patch_cnt = 0;
5279ee519a1fSgjelinek 	infop->zpi_patchinfo = NULL;
5280ee519a1fSgjelinek 
5281ee519a1fSgjelinek 	if ((fp = fopen(pkginfo, "r")) == NULL)
5282ee519a1fSgjelinek 		return (errno);
5283ee519a1fSgjelinek 
5284ee519a1fSgjelinek 	while ((buf = read_pkg_data(fp)) != NULL) {
5285ee519a1fSgjelinek 		if (strncmp(buf, VERSION, sizeof (VERSION) - 1) == 0) {
5286ee519a1fSgjelinek 			int len;
5287ee519a1fSgjelinek 
5288ee519a1fSgjelinek 			if ((infop->zpi_version =
5289ee519a1fSgjelinek 			    strdup(buf + sizeof (VERSION) - 1)) == NULL) {
5290ee519a1fSgjelinek 				err = ENOMEM;
5291ee519a1fSgjelinek 				break;
5292ee519a1fSgjelinek 			}
5293ee519a1fSgjelinek 
5294ee519a1fSgjelinek 			/* remove trailing newline */
5295ee519a1fSgjelinek 			len = strlen(infop->zpi_version);
5296ee519a1fSgjelinek 			*(infop->zpi_version + len - 1) = 0;
5297ee519a1fSgjelinek 
529845916cd2Sjpk 		} else if (strcmp(buf, SUNW_PKG_ALL_ZONES) == 0) {
5299ee519a1fSgjelinek 			infop->zpi_all_zones = B_TRUE;
5300ee519a1fSgjelinek 
530145916cd2Sjpk 		} else if (strcmp(buf, SUNW_PKG_THIS_ZONE) == 0) {
5302ee519a1fSgjelinek 			infop->zpi_this_zone = B_TRUE;
5303ee519a1fSgjelinek 
5304ee519a1fSgjelinek 		} else if (strncmp(buf, PATCHINFO, sizeof (PATCHINFO) - 1)
5305ee519a1fSgjelinek 		    == 0) {
5306ee519a1fSgjelinek 			char **p;
5307ee519a1fSgjelinek 
5308ee519a1fSgjelinek 			if ((p = (char **)realloc(infop->zpi_patchinfo,
5309ee519a1fSgjelinek 			    sizeof (char *) * (infop->zpi_patch_cnt + 1)))
5310ee519a1fSgjelinek 			    == NULL) {
5311ee519a1fSgjelinek 				err = ENOMEM;
5312ee519a1fSgjelinek 				break;
5313ee519a1fSgjelinek 			}
5314ee519a1fSgjelinek 			infop->zpi_patchinfo = p;
5315ee519a1fSgjelinek 
5316ee519a1fSgjelinek 			if ((infop->zpi_patchinfo[infop->zpi_patch_cnt] =
5317ee519a1fSgjelinek 			    strdup(buf)) == NULL) {
5318ee519a1fSgjelinek 				err = ENOMEM;
5319ee519a1fSgjelinek 				break;
5320ee519a1fSgjelinek 			}
5321ee519a1fSgjelinek 			infop->zpi_patch_cnt++;
5322ee519a1fSgjelinek 		}
5323ee519a1fSgjelinek 
5324ee519a1fSgjelinek 		free(buf);
5325ee519a1fSgjelinek 	}
5326ee519a1fSgjelinek 
5327ee519a1fSgjelinek 	free(buf);
5328ee519a1fSgjelinek 
5329ee519a1fSgjelinek 	if (errno == ENOMEM) {
5330ee519a1fSgjelinek 		err = ENOMEM;
5331ee519a1fSgjelinek 		/* Clean up anything we did manage to allocate. */
5332ee519a1fSgjelinek 		free_pkginfo(infop);
5333ee519a1fSgjelinek 	}
5334ee519a1fSgjelinek 
5335ee519a1fSgjelinek 	(void) fclose(fp);
5336ee519a1fSgjelinek 
5337ee519a1fSgjelinek 	return (err);
5338ee519a1fSgjelinek }
5339ee519a1fSgjelinek 
5340ee519a1fSgjelinek /*
5341ee519a1fSgjelinek  * Take a software inventory of the global zone.  We need to get the set of
5342ee519a1fSgjelinek  * packages and patches that are on the global zone that the specified
5343ee519a1fSgjelinek  * non-global zone depends on.  The packages we need in the inventory are:
5344ee519a1fSgjelinek  *
5345ee519a1fSgjelinek  * - skip the package if SUNW_PKG_THISZONE is 'true'
5346ee519a1fSgjelinek  * otherwise,
5347ee519a1fSgjelinek  * - add the package if
5348ee519a1fSgjelinek  * a) SUNW_PKG_ALLZONES is 'true',
5349ee519a1fSgjelinek  * or
5350ee519a1fSgjelinek  * b) any file delivered by the package is in a file system that is inherited
5351ee519a1fSgjelinek  * from the global zone.
5352ee519a1fSgjelinek  * If the zone does not inherit any file systems (whole root)
5353ee519a1fSgjelinek  * then (b) will be skipped.
5354ee519a1fSgjelinek  *
5355ee519a1fSgjelinek  * For each of the packages that is being added to the inventory, we will also
5356ee519a1fSgjelinek  * add all of the associated, unique patches to the inventory.
5357ee519a1fSgjelinek  */
5358ee519a1fSgjelinek static int
5359ee519a1fSgjelinek zonecfg_sw_inventory(zone_dochandle_t handle)
5360ee519a1fSgjelinek {
5361ee519a1fSgjelinek 	char		pkginfo[MAXPATHLEN];
5362ee519a1fSgjelinek 	int		res;
5363ee519a1fSgjelinek 	struct dirent	*dp;
5364ee519a1fSgjelinek 	DIR		*dirp;
5365ee519a1fSgjelinek 	struct stat	buf;
5366ee519a1fSgjelinek 	struct zone_pkginfo	info;
5367ee519a1fSgjelinek 	int		pkg_cnt = 0;
5368ee519a1fSgjelinek 	char		**pkgs = NULL;
5369ee519a1fSgjelinek 
5370ee519a1fSgjelinek 	if ((res = get_ipd_pkgs(handle, &pkgs, &pkg_cnt)) != Z_OK)
5371ee519a1fSgjelinek 		return (res);
5372ee519a1fSgjelinek 
5373ee519a1fSgjelinek 	if ((dirp = opendir(PKG_PATH)) == NULL) {
5374ee519a1fSgjelinek 		free_ipd_pkgs(pkgs, pkg_cnt);
5375ee519a1fSgjelinek 		return (Z_OK);
5376ee519a1fSgjelinek 	}
5377ee519a1fSgjelinek 
5378ee519a1fSgjelinek 	while ((dp = readdir(dirp)) != (struct dirent *)0) {
5379ee519a1fSgjelinek 		if (strcmp(dp->d_name, ".") == 0 ||
5380ee519a1fSgjelinek 		    strcmp(dp->d_name, "..") == 0)
5381ee519a1fSgjelinek 			continue;
5382ee519a1fSgjelinek 
5383ee519a1fSgjelinek 		(void) snprintf(pkginfo, sizeof (pkginfo), "%s/%s/pkginfo",
5384ee519a1fSgjelinek 		    PKG_PATH, dp->d_name);
5385ee519a1fSgjelinek 
5386ee519a1fSgjelinek 		if (stat(pkginfo, &buf) == -1 || !S_ISREG(buf.st_mode))
5387ee519a1fSgjelinek 			continue;
5388ee519a1fSgjelinek 
5389ee519a1fSgjelinek 		if (get_pkginfo(pkginfo, &info) != 0) {
5390ee519a1fSgjelinek 			res = Z_NOMEM;
5391ee519a1fSgjelinek 			break;
5392ee519a1fSgjelinek 		}
5393ee519a1fSgjelinek 
5394ee519a1fSgjelinek 		if (!info.zpi_this_zone &&
5395ee519a1fSgjelinek 		    (info.zpi_all_zones ||
5396ee519a1fSgjelinek 		    dir_pkg(dp->d_name, pkgs, pkg_cnt))) {
5397ee519a1fSgjelinek 			if ((res = add_pkg(handle, dp->d_name,
5398ee519a1fSgjelinek 			    info.zpi_version)) == Z_OK) {
5399ee519a1fSgjelinek 				if (info.zpi_patch_cnt > 0)
5400ee519a1fSgjelinek 					res = add_patches(handle, &info);
5401ee519a1fSgjelinek 			}
5402ee519a1fSgjelinek 		}
5403ee519a1fSgjelinek 
5404ee519a1fSgjelinek 		free_pkginfo(&info);
5405ee519a1fSgjelinek 
5406ee519a1fSgjelinek 		if (res != Z_OK)
5407ee519a1fSgjelinek 			break;
5408ee519a1fSgjelinek 	}
5409ee519a1fSgjelinek 
5410ee519a1fSgjelinek 	(void) closedir(dirp);
5411ee519a1fSgjelinek 
5412ee519a1fSgjelinek 	free_ipd_pkgs(pkgs, pkg_cnt);
5413ee519a1fSgjelinek 
5414ee519a1fSgjelinek 	if (res == Z_OK)
5415ee519a1fSgjelinek 		handle->zone_dh_sw_inv = B_TRUE;
5416ee519a1fSgjelinek 
5417ee519a1fSgjelinek 	return (res);
5418ee519a1fSgjelinek }
5419ee519a1fSgjelinek 
5420ee519a1fSgjelinek /*
5421ee519a1fSgjelinek  * zonecfg_devwalk call-back function used during detach to generate the
5422ee519a1fSgjelinek  * dev info in the manifest.
5423ee519a1fSgjelinek  */
5424ee519a1fSgjelinek static int
5425ee519a1fSgjelinek get_detach_dev_entry(const char *name, uid_t uid, gid_t gid, mode_t mode,
5426ee519a1fSgjelinek     const char *acl, void *hdl)
5427ee519a1fSgjelinek {
5428ee519a1fSgjelinek 	zone_dochandle_t handle = (zone_dochandle_t)hdl;
5429ee519a1fSgjelinek 	xmlNodePtr newnode;
5430ee519a1fSgjelinek 	xmlNodePtr cur;
5431ee519a1fSgjelinek 	int err;
5432ee519a1fSgjelinek 	char buf[128];
5433ee519a1fSgjelinek 
5434ee519a1fSgjelinek 	if ((err = operation_prep(handle)) != Z_OK)
5435ee519a1fSgjelinek 		return (err);
5436ee519a1fSgjelinek 
5437ee519a1fSgjelinek 	cur = handle->zone_dh_cur;
5438ee519a1fSgjelinek 	newnode = xmlNewTextChild(cur, NULL, DTD_ELEM_DEV_PERM, NULL);
5439ee519a1fSgjelinek 	if ((err = newprop(newnode, DTD_ATTR_NAME, (char *)name)) != Z_OK)
5440ee519a1fSgjelinek 		return (err);
5441ee519a1fSgjelinek 	(void) snprintf(buf, sizeof (buf), "%lu", uid);
5442ee519a1fSgjelinek 	if ((err = newprop(newnode, DTD_ATTR_UID, buf)) != Z_OK)
5443ee519a1fSgjelinek 		return (err);
5444ee519a1fSgjelinek 	(void) snprintf(buf, sizeof (buf), "%lu", gid);
5445ee519a1fSgjelinek 	if ((err = newprop(newnode, DTD_ATTR_GID, buf)) != Z_OK)
5446ee519a1fSgjelinek 		return (err);
5447ee519a1fSgjelinek 	(void) snprintf(buf, sizeof (buf), "%o", mode);
5448ee519a1fSgjelinek 	if ((err = newprop(newnode, DTD_ATTR_MODE, buf)) != Z_OK)
5449ee519a1fSgjelinek 		return (err);
5450ee519a1fSgjelinek 	if ((err = newprop(newnode, DTD_ATTR_ACL, (char *)acl)) != Z_OK)
5451ee519a1fSgjelinek 		return (err);
5452ee519a1fSgjelinek 	return (Z_OK);
5453ee519a1fSgjelinek }
5454ee519a1fSgjelinek 
5455ee519a1fSgjelinek /*
5456ee519a1fSgjelinek  * Get the information required to support detaching a zone.  This is
5457ee519a1fSgjelinek  * called on the source system when detaching (the detaching parameter should
5458ee519a1fSgjelinek  * be set to true) and on the destination system before attaching (the
5459ee519a1fSgjelinek  * detaching parameter should be false).
5460ee519a1fSgjelinek  *
5461ee519a1fSgjelinek  * For native Solaris zones, the detach/attach process involves validating
5462ee519a1fSgjelinek  * that the software on the global zone can support the zone when we attach.
5463ee519a1fSgjelinek  * To do this we take a software inventory of the global zone.  We also
5464ee519a1fSgjelinek  * have to keep track of the device configuration so that we can properly
5465ee519a1fSgjelinek  * recreate it on the destination.
5466ee519a1fSgjelinek  */
5467ee519a1fSgjelinek int
5468ee519a1fSgjelinek zonecfg_get_detach_info(zone_dochandle_t handle, boolean_t detaching)
5469ee519a1fSgjelinek {
5470ee519a1fSgjelinek 	int		res;
5471ee519a1fSgjelinek 
5472ee519a1fSgjelinek 	if ((res = zonecfg_sw_inventory(handle)) != Z_OK)
5473ee519a1fSgjelinek 		return (res);
5474ee519a1fSgjelinek 
5475ee519a1fSgjelinek 	if (detaching)
5476ee519a1fSgjelinek 		res = zonecfg_devwalk(handle, get_detach_dev_entry, handle);
5477ee519a1fSgjelinek 
5478ee519a1fSgjelinek 	return (res);
5479ee519a1fSgjelinek }
5480