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