xref: /titanic_50/usr/src/lib/libzonecfg/common/libzonecfg.c (revision fa9e4066f08beec538e775443c5be79dd423fcab)
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
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
237c478bd9Sstevel@tonic-gate  * Copyright 2005 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 
297c478bd9Sstevel@tonic-gate #include <errno.h>
307c478bd9Sstevel@tonic-gate #include <fnmatch.h>
317c478bd9Sstevel@tonic-gate #include <strings.h>
327c478bd9Sstevel@tonic-gate #include <unistd.h>
337c478bd9Sstevel@tonic-gate #include <sys/stat.h>
347c478bd9Sstevel@tonic-gate #include <assert.h>
357c478bd9Sstevel@tonic-gate #include <libgen.h>
367c478bd9Sstevel@tonic-gate #include <libintl.h>
377c478bd9Sstevel@tonic-gate #include <alloca.h>
387c478bd9Sstevel@tonic-gate #include <ctype.h>
397c478bd9Sstevel@tonic-gate #include <sys/mntio.h>
407c478bd9Sstevel@tonic-gate #include <sys/mnttab.h>
41087719fdSdp #include <sys/types.h>
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate #include <arpa/inet.h>
447c478bd9Sstevel@tonic-gate #include <netdb.h>
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate #include <priv.h>
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate #include <libxml/xmlmemory.h>
497c478bd9Sstevel@tonic-gate #include <libxml/parser.h>
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate #include <libdevinfo.h>
52108322fbScarlsonj #include <uuid/uuid.h>
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate #include <libzonecfg.h>
557c478bd9Sstevel@tonic-gate #include "zonecfg_impl.h"
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate #define	_PATH_TMPFILE	"/zonecfg.XXXXXX"
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate /* Hard-code the DTD element/attribute/entity names just once, here. */
607c478bd9Sstevel@tonic-gate #define	DTD_ELEM_ATTR		(const xmlChar *) "attr"
617c478bd9Sstevel@tonic-gate #define	DTD_ELEM_COMMENT	(const xmlChar *) "comment"
627c478bd9Sstevel@tonic-gate #define	DTD_ELEM_DEVICE		(const xmlChar *) "device"
637c478bd9Sstevel@tonic-gate #define	DTD_ELEM_FS		(const xmlChar *) "filesystem"
647c478bd9Sstevel@tonic-gate #define	DTD_ELEM_FSOPTION	(const xmlChar *) "fsoption"
657c478bd9Sstevel@tonic-gate #define	DTD_ELEM_IPD		(const xmlChar *) "inherited-pkg-dir"
667c478bd9Sstevel@tonic-gate #define	DTD_ELEM_NET		(const xmlChar *) "network"
677c478bd9Sstevel@tonic-gate #define	DTD_ELEM_RCTL		(const xmlChar *) "rctl"
687c478bd9Sstevel@tonic-gate #define	DTD_ELEM_RCTLVALUE	(const xmlChar *) "rctl-value"
697c478bd9Sstevel@tonic-gate #define	DTD_ELEM_ZONE		(const xmlChar *) "zone"
70*fa9e4066Sahrens #define	DTD_ELEM_DATASET	(const xmlChar *) "dataset"
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate #define	DTD_ATTR_ACTION		(const xmlChar *) "action"
737c478bd9Sstevel@tonic-gate #define	DTD_ATTR_ADDRESS	(const xmlChar *) "address"
747c478bd9Sstevel@tonic-gate #define	DTD_ATTR_AUTOBOOT	(const xmlChar *) "autoboot"
757c478bd9Sstevel@tonic-gate #define	DTD_ATTR_DIR		(const xmlChar *) "directory"
767c478bd9Sstevel@tonic-gate #define	DTD_ATTR_LIMIT		(const xmlChar *) "limit"
777c478bd9Sstevel@tonic-gate #define	DTD_ATTR_MATCH		(const xmlChar *) "match"
787c478bd9Sstevel@tonic-gate #define	DTD_ATTR_NAME		(const xmlChar *) "name"
797c478bd9Sstevel@tonic-gate #define	DTD_ATTR_PHYSICAL	(const xmlChar *) "physical"
807c478bd9Sstevel@tonic-gate #define	DTD_ATTR_POOL		(const xmlChar *) "pool"
817c478bd9Sstevel@tonic-gate #define	DTD_ATTR_PRIV		(const xmlChar *) "priv"
827c478bd9Sstevel@tonic-gate #define	DTD_ATTR_RAW		(const xmlChar *) "raw"
837c478bd9Sstevel@tonic-gate #define	DTD_ATTR_SPECIAL	(const xmlChar *) "special"
847c478bd9Sstevel@tonic-gate #define	DTD_ATTR_TYPE		(const xmlChar *) "type"
857c478bd9Sstevel@tonic-gate #define	DTD_ATTR_VALUE		(const xmlChar *) "value"
867c478bd9Sstevel@tonic-gate #define	DTD_ATTR_ZONEPATH	(const xmlChar *) "zonepath"
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate #define	DTD_ENTITY_BOOLEAN	"boolean"
897c478bd9Sstevel@tonic-gate #define	DTD_ENTITY_DEVPATH	"devpath"
907c478bd9Sstevel@tonic-gate #define	DTD_ENTITY_DRIVER	"driver"
917c478bd9Sstevel@tonic-gate #define	DTD_ENTITY_DRVMIN	"drv_min"
927c478bd9Sstevel@tonic-gate #define	DTD_ENTITY_FALSE	"false"
937c478bd9Sstevel@tonic-gate #define	DTD_ENTITY_INT		"int"
947c478bd9Sstevel@tonic-gate #define	DTD_ENTITY_STRING	"string"
957c478bd9Sstevel@tonic-gate #define	DTD_ENTITY_TRUE		"true"
967c478bd9Sstevel@tonic-gate #define	DTD_ENTITY_UINT		"uint"
977c478bd9Sstevel@tonic-gate 
98a1be23daSdp #define	DTD_ENTITY_BOOL_LEN	6	/* "false" */
99a1be23daSdp 
1007c478bd9Sstevel@tonic-gate struct zone_dochandle {
1017c478bd9Sstevel@tonic-gate 	char		*zone_dh_rootdir;
1027c478bd9Sstevel@tonic-gate 	xmlDocPtr	zone_dh_doc;
1037c478bd9Sstevel@tonic-gate 	xmlNodePtr	zone_dh_cur;
1047c478bd9Sstevel@tonic-gate 	xmlNodePtr	zone_dh_top;
105087719fdSdp 	boolean_t	zone_dh_newzone;
106087719fdSdp 	boolean_t	zone_dh_snapshot;
107087719fdSdp 	char		zone_dh_delete_name[ZONENAME_MAX];
1087c478bd9Sstevel@tonic-gate };
1097c478bd9Sstevel@tonic-gate 
110108322fbScarlsonj char *zonecfg_root = "";
111108322fbScarlsonj 
1127c478bd9Sstevel@tonic-gate /*
1137c478bd9Sstevel@tonic-gate  * For functions which return int, which is most of the functions herein,
1147c478bd9Sstevel@tonic-gate  * the return values should be from the Z_foo set defined in <libzonecfg.h>.
1157c478bd9Sstevel@tonic-gate  * In some instances, we take pains mapping some libc errno values to Z_foo
1167c478bd9Sstevel@tonic-gate  * values from this set.
1177c478bd9Sstevel@tonic-gate  */
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate /*
120108322fbScarlsonj  * Set the root (/) path for all zonecfg configuration files.  This is a
121108322fbScarlsonj  * private interface used by Live Upgrade extensions to access zone
122108322fbScarlsonj  * configuration inside mounted alternate boot environments.
123108322fbScarlsonj  */
124108322fbScarlsonj void
125108322fbScarlsonj zonecfg_set_root(const char *rootpath)
126108322fbScarlsonj {
127108322fbScarlsonj 	if (*zonecfg_root != '\0')
128108322fbScarlsonj 		free(zonecfg_root);
129108322fbScarlsonj 	if (rootpath == NULL || rootpath[0] == '\0' || rootpath[1] == '\0' ||
130108322fbScarlsonj 	    (zonecfg_root = strdup(rootpath)) == NULL)
131108322fbScarlsonj 		zonecfg_root = "";
132108322fbScarlsonj }
133108322fbScarlsonj 
134108322fbScarlsonj const char *
135108322fbScarlsonj zonecfg_get_root(void)
136108322fbScarlsonj {
137108322fbScarlsonj 	return (zonecfg_root);
138108322fbScarlsonj }
139108322fbScarlsonj 
140108322fbScarlsonj boolean_t
141108322fbScarlsonj zonecfg_in_alt_root(void)
142108322fbScarlsonj {
143108322fbScarlsonj 	return (*zonecfg_root != '\0');
144108322fbScarlsonj }
145108322fbScarlsonj 
146108322fbScarlsonj /*
1477c478bd9Sstevel@tonic-gate  * Callers of the _file_path() functions are expected to have the second
1487c478bd9Sstevel@tonic-gate  * parameter be a (char foo[MAXPATHLEN]).
1497c478bd9Sstevel@tonic-gate  */
1507c478bd9Sstevel@tonic-gate 
151108322fbScarlsonj static boolean_t
1527c478bd9Sstevel@tonic-gate config_file_path(const char *zonename, char *answer)
1537c478bd9Sstevel@tonic-gate {
154108322fbScarlsonj 	return (snprintf(answer, MAXPATHLEN, "%s%s/%s.xml", zonecfg_root,
155108322fbScarlsonj 	    ZONE_CONFIG_ROOT, zonename) < MAXPATHLEN);
1567c478bd9Sstevel@tonic-gate }
1577c478bd9Sstevel@tonic-gate 
158108322fbScarlsonj static boolean_t
159108322fbScarlsonj snap_file_path(const char *zonename, char *answer)
1607c478bd9Sstevel@tonic-gate {
161108322fbScarlsonj 	return (snprintf(answer, MAXPATHLEN, "%s%s/%s.snapshot.xml",
162108322fbScarlsonj 	    zonecfg_root, ZONE_SNAPSHOT_ROOT, zonename) < MAXPATHLEN);
1637c478bd9Sstevel@tonic-gate }
1647c478bd9Sstevel@tonic-gate 
1657c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1667c478bd9Sstevel@tonic-gate static void
1677c478bd9Sstevel@tonic-gate zonecfg_error_func(void *ctx, const char *msg, ...)
1687c478bd9Sstevel@tonic-gate {
1697c478bd9Sstevel@tonic-gate 	/*
1707c478bd9Sstevel@tonic-gate 	 * This function does nothing by design.  Its purpose is to prevent
1717c478bd9Sstevel@tonic-gate 	 * libxml from dumping unwanted messages to stdout/stderr.
1727c478bd9Sstevel@tonic-gate 	 */
1737c478bd9Sstevel@tonic-gate }
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate zone_dochandle_t
1767c478bd9Sstevel@tonic-gate zonecfg_init_handle(void)
1777c478bd9Sstevel@tonic-gate {
178087719fdSdp 	zone_dochandle_t handle = calloc(1, sizeof (struct zone_dochandle));
1797c478bd9Sstevel@tonic-gate 	if (handle == NULL) {
1807c478bd9Sstevel@tonic-gate 		errno = Z_NOMEM;
1817c478bd9Sstevel@tonic-gate 		return (NULL);
1827c478bd9Sstevel@tonic-gate 	}
1837c478bd9Sstevel@tonic-gate 
1847c478bd9Sstevel@tonic-gate 	/* generic libxml initialization */
1857c478bd9Sstevel@tonic-gate 	xmlLineNumbersDefault(1);
1867c478bd9Sstevel@tonic-gate 	xmlLoadExtDtdDefaultValue |= XML_DETECT_IDS;
1877c478bd9Sstevel@tonic-gate 	xmlDoValidityCheckingDefaultValue = 1;
1887c478bd9Sstevel@tonic-gate 	(void) xmlKeepBlanksDefault(0);
1897c478bd9Sstevel@tonic-gate 	xmlGetWarningsDefaultValue = 0;
1907c478bd9Sstevel@tonic-gate 	xmlSetGenericErrorFunc(NULL, zonecfg_error_func);
1917c478bd9Sstevel@tonic-gate 
1927c478bd9Sstevel@tonic-gate 	return (handle);
1937c478bd9Sstevel@tonic-gate }
1947c478bd9Sstevel@tonic-gate 
1957c478bd9Sstevel@tonic-gate int
1967c478bd9Sstevel@tonic-gate zonecfg_check_handle(zone_dochandle_t handle)
1977c478bd9Sstevel@tonic-gate {
1987c478bd9Sstevel@tonic-gate 	if (handle == NULL || handle->zone_dh_doc == NULL)
1997c478bd9Sstevel@tonic-gate 		return (Z_BAD_HANDLE);
2007c478bd9Sstevel@tonic-gate 	return (Z_OK);
2017c478bd9Sstevel@tonic-gate }
2027c478bd9Sstevel@tonic-gate 
2037c478bd9Sstevel@tonic-gate void
2047c478bd9Sstevel@tonic-gate zonecfg_fini_handle(zone_dochandle_t handle)
2057c478bd9Sstevel@tonic-gate {
2067c478bd9Sstevel@tonic-gate 	if (zonecfg_check_handle(handle) == Z_OK)
2077c478bd9Sstevel@tonic-gate 		xmlFreeDoc(handle->zone_dh_doc);
2087c478bd9Sstevel@tonic-gate 	if (handle != NULL)
2097c478bd9Sstevel@tonic-gate 		free(handle);
2107c478bd9Sstevel@tonic-gate }
2117c478bd9Sstevel@tonic-gate 
2127c478bd9Sstevel@tonic-gate static int
2137c478bd9Sstevel@tonic-gate zonecfg_destroy_impl(char *filename)
2147c478bd9Sstevel@tonic-gate {
2157c478bd9Sstevel@tonic-gate 	if (unlink(filename) == -1) {
2167c478bd9Sstevel@tonic-gate 		if (errno == EACCES)
2177c478bd9Sstevel@tonic-gate 			return (Z_ACCES);
2187c478bd9Sstevel@tonic-gate 		if (errno == ENOENT)
2197c478bd9Sstevel@tonic-gate 			return (Z_NO_ZONE);
2207c478bd9Sstevel@tonic-gate 		return (Z_MISC_FS);
2217c478bd9Sstevel@tonic-gate 	}
2227c478bd9Sstevel@tonic-gate 	return (Z_OK);
2237c478bd9Sstevel@tonic-gate }
2247c478bd9Sstevel@tonic-gate 
2257c478bd9Sstevel@tonic-gate int
226087719fdSdp zonecfg_destroy(const char *zonename, boolean_t force)
2277c478bd9Sstevel@tonic-gate {
2287c478bd9Sstevel@tonic-gate 	char path[MAXPATHLEN];
229087719fdSdp 	struct zoneent ze;
230087719fdSdp 	int err, state_err;
231087719fdSdp 	zone_state_t state;
2327c478bd9Sstevel@tonic-gate 
233108322fbScarlsonj 	if (!config_file_path(zonename, path))
234108322fbScarlsonj 		return (Z_MISC_FS);
235087719fdSdp 
236087719fdSdp 	state_err = zone_get_state((char *)zonename, &state);
237087719fdSdp 	err = access(path, W_OK);
238087719fdSdp 
239087719fdSdp 	/*
240087719fdSdp 	 * If there is no file, and no index entry, reliably indicate that no
241087719fdSdp 	 * such zone exists.
242087719fdSdp 	 */
243087719fdSdp 	if ((state_err == Z_NO_ZONE) && (err == -1) && (errno == ENOENT))
244087719fdSdp 		return (Z_NO_ZONE);
245087719fdSdp 
246087719fdSdp 	/*
247087719fdSdp 	 * Handle any other filesystem related errors (except if the XML
248087719fdSdp 	 * file is missing, which we treat silently), unless we're forcing,
249087719fdSdp 	 * in which case we plow on.
250087719fdSdp 	 */
251087719fdSdp 	if (err == -1 && errno != ENOENT) {
252087719fdSdp 		if (errno == EACCES)
253087719fdSdp 			return (Z_ACCES);
254087719fdSdp 		else if (!force)
255087719fdSdp 			return (Z_MISC_FS);
256087719fdSdp 	}
257087719fdSdp 
258087719fdSdp 	if (state > ZONE_STATE_INSTALLED)
259087719fdSdp 		return (Z_BAD_ZONE_STATE);
260087719fdSdp 
261087719fdSdp 	if (!force && state > ZONE_STATE_CONFIGURED)
262087719fdSdp 		return (Z_BAD_ZONE_STATE);
263087719fdSdp 
264087719fdSdp 	/*
265087719fdSdp 	 * Index deletion succeeds even if the entry doesn't exist.  So this
266087719fdSdp 	 * will fail only if we've had some more severe problem.
267087719fdSdp 	 */
268087719fdSdp 	bzero(&ze, sizeof (ze));
269087719fdSdp 	(void) strlcpy(ze.zone_name, zonename, sizeof (ze.zone_name));
270087719fdSdp 	if ((err = putzoneent(&ze, PZE_REMOVE)) != Z_OK)
271087719fdSdp 		if (!force)
272087719fdSdp 			return (err);
273087719fdSdp 
274087719fdSdp 	err = zonecfg_destroy_impl(path);
275087719fdSdp 
276087719fdSdp 	/*
277087719fdSdp 	 * Treat failure to find the XML file silently, since, well, it's
278087719fdSdp 	 * gone, and with the index file cleaned up, we're done.
279087719fdSdp 	 */
280087719fdSdp 	if (err == Z_OK || err == Z_NO_ZONE)
281087719fdSdp 		return (Z_OK);
282087719fdSdp 	return (err);
2837c478bd9Sstevel@tonic-gate }
2847c478bd9Sstevel@tonic-gate 
2857c478bd9Sstevel@tonic-gate int
286108322fbScarlsonj zonecfg_destroy_snapshot(const char *zonename)
2877c478bd9Sstevel@tonic-gate {
2887c478bd9Sstevel@tonic-gate 	char path[MAXPATHLEN];
2897c478bd9Sstevel@tonic-gate 
290108322fbScarlsonj 	if (!snap_file_path(zonename, path))
291108322fbScarlsonj 		return (Z_MISC_FS);
2927c478bd9Sstevel@tonic-gate 	return (zonecfg_destroy_impl(path));
2937c478bd9Sstevel@tonic-gate }
2947c478bd9Sstevel@tonic-gate 
2957c478bd9Sstevel@tonic-gate static int
296a1be23daSdp getroot(zone_dochandle_t handle, xmlNodePtr *root)
2977c478bd9Sstevel@tonic-gate {
2987c478bd9Sstevel@tonic-gate 	if (zonecfg_check_handle(handle) == Z_BAD_HANDLE)
2997c478bd9Sstevel@tonic-gate 		return (Z_BAD_HANDLE);
3007c478bd9Sstevel@tonic-gate 
301a1be23daSdp 	*root = xmlDocGetRootElement(handle->zone_dh_doc);
302a1be23daSdp 
303a1be23daSdp 	if (*root == NULL)
3047c478bd9Sstevel@tonic-gate 		return (Z_EMPTY_DOCUMENT);
305a1be23daSdp 
306a1be23daSdp 	if (xmlStrcmp((*root)->name, DTD_ELEM_ZONE))
307a1be23daSdp 		return (Z_WRONG_DOC_TYPE);
308a1be23daSdp 
309a1be23daSdp 	return (Z_OK);
3107c478bd9Sstevel@tonic-gate }
3117c478bd9Sstevel@tonic-gate 
312a1be23daSdp static int
313a1be23daSdp operation_prep(zone_dochandle_t handle)
314a1be23daSdp {
315a1be23daSdp 	xmlNodePtr root;
316a1be23daSdp 	int err;
317a1be23daSdp 
318a1be23daSdp 	if ((err = getroot(handle, &root)) != 0)
319a1be23daSdp 		return (err);
320a1be23daSdp 
321a1be23daSdp 	handle->zone_dh_cur = root;
322a1be23daSdp 	handle->zone_dh_top = root;
323a1be23daSdp 	return (Z_OK);
3247c478bd9Sstevel@tonic-gate }
325a1be23daSdp 
326a1be23daSdp static int
327a1be23daSdp getrootattr(zone_dochandle_t handle, const xmlChar *propname,
328a1be23daSdp     char *propval, size_t propsize)
329a1be23daSdp {
330a1be23daSdp 	xmlNodePtr root;
331a1be23daSdp 	xmlChar *property;
332a1be23daSdp 	size_t srcsize;
333a1be23daSdp 	int err;
334a1be23daSdp 
335a1be23daSdp 	if ((err = getroot(handle, &root)) != 0)
336a1be23daSdp 		return (err);
337a1be23daSdp 
338a1be23daSdp 	if ((property = xmlGetProp(root, propname)) == NULL)
339a1be23daSdp 		return (Z_BAD_PROPERTY);
340a1be23daSdp 	srcsize = strlcpy(propval, (char *)property, propsize);
341a1be23daSdp 	xmlFree(property);
342a1be23daSdp 	if (srcsize >= propsize)
343a1be23daSdp 		return (Z_TOO_BIG);
344a1be23daSdp 	return (Z_OK);
345a1be23daSdp }
346a1be23daSdp 
347a1be23daSdp static int
348108322fbScarlsonj setrootattr(zone_dochandle_t handle, const xmlChar *propname,
349108322fbScarlsonj     const char *propval)
350a1be23daSdp {
351a1be23daSdp 	int err;
352a1be23daSdp 	xmlNodePtr root;
353a1be23daSdp 
354a1be23daSdp 	if (propval == NULL)
355a1be23daSdp 		return (Z_INVAL);
356a1be23daSdp 
357a1be23daSdp 	if ((err = getroot(handle, &root)) != Z_OK)
358a1be23daSdp 		return (err);
359a1be23daSdp 
360a1be23daSdp 	if (xmlSetProp(root, propname, (const xmlChar *) propval) == NULL)
361a1be23daSdp 		return (Z_INVAL);
3627c478bd9Sstevel@tonic-gate 	return (Z_OK);
3637c478bd9Sstevel@tonic-gate }
3647c478bd9Sstevel@tonic-gate 
365087719fdSdp static void
366087719fdSdp addcomment(zone_dochandle_t handle, const char *comment)
367087719fdSdp {
368087719fdSdp 	xmlNodePtr node;
369087719fdSdp 	node = xmlNewComment((xmlChar *) comment);
370087719fdSdp 
371087719fdSdp 	if (node != NULL)
372087719fdSdp 		(void) xmlAddPrevSibling(handle->zone_dh_top, node);
373087719fdSdp }
374087719fdSdp 
375087719fdSdp static void
376087719fdSdp stripcomments(zone_dochandle_t handle)
377087719fdSdp {
378087719fdSdp 	xmlDocPtr top;
379087719fdSdp 	xmlNodePtr child, next;
380087719fdSdp 
381087719fdSdp 	top = handle->zone_dh_doc;
382087719fdSdp 	for (child = top->xmlChildrenNode; child != NULL; child = next) {
383087719fdSdp 		next = child->next;
384087719fdSdp 		if (child->name == NULL)
385087719fdSdp 			continue;
386087719fdSdp 		if (xmlStrcmp(child->name, DTD_ELEM_COMMENT) == 0) {
387087719fdSdp 			next = child->next;
388087719fdSdp 			xmlUnlinkNode(child);
389087719fdSdp 			xmlFreeNode(child);
390087719fdSdp 		}
391087719fdSdp 	}
392087719fdSdp }
393087719fdSdp 
3947c478bd9Sstevel@tonic-gate static int
395108322fbScarlsonj zonecfg_get_handle_impl(const char *zonename, const char *filename,
396108322fbScarlsonj     zone_dochandle_t handle)
3977c478bd9Sstevel@tonic-gate {
3987c478bd9Sstevel@tonic-gate 	xmlValidCtxtPtr cvp;
3997c478bd9Sstevel@tonic-gate 	struct stat statbuf;
4007c478bd9Sstevel@tonic-gate 	int valid;
4017c478bd9Sstevel@tonic-gate 
4027c478bd9Sstevel@tonic-gate 	if (zonename == NULL)
4037c478bd9Sstevel@tonic-gate 		return (Z_NO_ZONE);
4047c478bd9Sstevel@tonic-gate 	if ((handle->zone_dh_doc = xmlParseFile(filename)) == NULL) {
4057c478bd9Sstevel@tonic-gate 		/* distinguish file not found vs. found but not parsed */
4067c478bd9Sstevel@tonic-gate 		if (stat(filename, &statbuf) == 0)
4077c478bd9Sstevel@tonic-gate 			return (Z_INVALID_DOCUMENT);
4087c478bd9Sstevel@tonic-gate 		return (Z_NO_ZONE);
4097c478bd9Sstevel@tonic-gate 	}
4107c478bd9Sstevel@tonic-gate 	if ((cvp = xmlNewValidCtxt()) == NULL)
4117c478bd9Sstevel@tonic-gate 		return (Z_NOMEM);
4127c478bd9Sstevel@tonic-gate 	cvp->error = zonecfg_error_func;
4137c478bd9Sstevel@tonic-gate 	cvp->warning = zonecfg_error_func;
4147c478bd9Sstevel@tonic-gate 	valid = xmlValidateDocument(cvp, handle->zone_dh_doc);
4157c478bd9Sstevel@tonic-gate 	xmlFreeValidCtxt(cvp);
4167c478bd9Sstevel@tonic-gate 	if (valid == 0)
4177c478bd9Sstevel@tonic-gate 		return (Z_INVALID_DOCUMENT);
418087719fdSdp 
4197c478bd9Sstevel@tonic-gate 	/* delete any comments such as inherited Sun copyright / ident str */
420087719fdSdp 	stripcomments(handle);
4217c478bd9Sstevel@tonic-gate 	return (Z_OK);
4227c478bd9Sstevel@tonic-gate }
4237c478bd9Sstevel@tonic-gate 
4247c478bd9Sstevel@tonic-gate int
425108322fbScarlsonj zonecfg_get_handle(const char *zonename, zone_dochandle_t handle)
4267c478bd9Sstevel@tonic-gate {
4277c478bd9Sstevel@tonic-gate 	char path[MAXPATHLEN];
4287c478bd9Sstevel@tonic-gate 
429108322fbScarlsonj 	if (!config_file_path(zonename, path))
430108322fbScarlsonj 		return (Z_MISC_FS);
431087719fdSdp 	handle->zone_dh_newzone = B_FALSE;
432087719fdSdp 
4337c478bd9Sstevel@tonic-gate 	return (zonecfg_get_handle_impl(zonename, path, handle));
4347c478bd9Sstevel@tonic-gate }
4357c478bd9Sstevel@tonic-gate 
4367c478bd9Sstevel@tonic-gate int
437108322fbScarlsonj zonecfg_get_snapshot_handle(const char *zonename, zone_dochandle_t handle)
4387c478bd9Sstevel@tonic-gate {
4397c478bd9Sstevel@tonic-gate 	char path[MAXPATHLEN];
4407c478bd9Sstevel@tonic-gate 
441108322fbScarlsonj 	if (!snap_file_path(zonename, path))
442108322fbScarlsonj 		return (Z_MISC_FS);
443087719fdSdp 	handle->zone_dh_newzone = B_FALSE;
4447c478bd9Sstevel@tonic-gate 	return (zonecfg_get_handle_impl(zonename, path, handle));
4457c478bd9Sstevel@tonic-gate }
4467c478bd9Sstevel@tonic-gate 
4477c478bd9Sstevel@tonic-gate int
448108322fbScarlsonj zonecfg_get_template_handle(const char *template, const char *zonename,
449087719fdSdp     zone_dochandle_t handle)
450087719fdSdp {
451087719fdSdp 	char path[MAXPATHLEN];
452087719fdSdp 	int err;
453087719fdSdp 
454108322fbScarlsonj 	if (!config_file_path(template, path))
455108322fbScarlsonj 		return (Z_MISC_FS);
456087719fdSdp 
457087719fdSdp 	if ((err = zonecfg_get_handle_impl(template, path, handle)) != Z_OK)
458087719fdSdp 		return (err);
459087719fdSdp 	handle->zone_dh_newzone = B_TRUE;
460087719fdSdp 	return (setrootattr(handle, DTD_ATTR_NAME, zonename));
461087719fdSdp }
462087719fdSdp 
463087719fdSdp static boolean_t
464087719fdSdp is_renaming(zone_dochandle_t handle)
465087719fdSdp {
466087719fdSdp 	if (handle->zone_dh_newzone)
467087719fdSdp 		return (B_FALSE);
468087719fdSdp 	if (strlen(handle->zone_dh_delete_name) > 0)
469087719fdSdp 		return (B_TRUE);
470087719fdSdp 	return (B_FALSE);
471087719fdSdp }
472087719fdSdp 
473087719fdSdp static boolean_t
474087719fdSdp is_new(zone_dochandle_t handle)
475087719fdSdp {
476087719fdSdp 	return (handle->zone_dh_newzone || handle->zone_dh_snapshot);
477087719fdSdp }
478087719fdSdp 
479087719fdSdp static boolean_t
480087719fdSdp is_snapshot(zone_dochandle_t handle)
481087719fdSdp {
482087719fdSdp 	return (handle->zone_dh_snapshot);
483087719fdSdp }
484087719fdSdp 
485087719fdSdp /*
486087719fdSdp  * It would be great to be able to use libc's ctype(3c) macros, but we
487087719fdSdp  * can't, as they are locale sensitive, and it would break our limited thread
488087719fdSdp  * safety if this routine had to change the app locale on the fly.
489087719fdSdp  */
490087719fdSdp int
491108322fbScarlsonj zonecfg_validate_zonename(const char *zone)
492087719fdSdp {
493087719fdSdp 	int i;
494087719fdSdp 
495087719fdSdp 	if (strcmp(zone, GLOBAL_ZONENAME) == 0)
496087719fdSdp 		return (Z_BOGUS_ZONE_NAME);
497087719fdSdp 
498087719fdSdp 	if (strlen(zone) >= ZONENAME_MAX)
499087719fdSdp 		return (Z_BOGUS_ZONE_NAME);
500087719fdSdp 
501087719fdSdp 	if (!((zone[0] >= 'a' && zone[0] <= 'z') ||
502087719fdSdp 	    (zone[0] >= 'A' && zone[0] <= 'Z') ||
503087719fdSdp 	    (zone[0] >= '0' && zone[0] <= '9')))
504087719fdSdp 		return (Z_BOGUS_ZONE_NAME);
505087719fdSdp 
506087719fdSdp 	for (i = 1; zone[i] != '\0'; i++) {
507087719fdSdp 		if (!((zone[i] >= 'a' && zone[i] <= 'z') ||
508087719fdSdp 		    (zone[i] >= 'A' && zone[i] <= 'Z') ||
509087719fdSdp 		    (zone[i] >= '0' && zone[i] <= '9') ||
510087719fdSdp 		    (zone[i] == '-') || (zone[i] == '_') || (zone[i] == '.')))
511087719fdSdp 			return (Z_BOGUS_ZONE_NAME);
512087719fdSdp 	}
513087719fdSdp 
514087719fdSdp 	return (Z_OK);
515087719fdSdp }
516087719fdSdp 
517087719fdSdp /*
518087719fdSdp  * Changing the zone name requires us to track both the old and new
519087719fdSdp  * name of the zone until commit time.
520087719fdSdp  */
521087719fdSdp int
5227c478bd9Sstevel@tonic-gate zonecfg_get_name(zone_dochandle_t handle, char *name, size_t namesize)
5237c478bd9Sstevel@tonic-gate {
524a1be23daSdp 	return (getrootattr(handle, DTD_ATTR_NAME, name, namesize));
5257c478bd9Sstevel@tonic-gate }
5267c478bd9Sstevel@tonic-gate 
527a1be23daSdp int
528a1be23daSdp zonecfg_set_name(zone_dochandle_t handle, char *name)
529a1be23daSdp {
530087719fdSdp 	zone_state_t state;
531087719fdSdp 	char curname[ZONENAME_MAX], old_delname[ZONENAME_MAX];
532087719fdSdp 	int err;
533087719fdSdp 
534087719fdSdp 	if ((err = getrootattr(handle, DTD_ATTR_NAME, curname,
535087719fdSdp 	    sizeof (curname))) != Z_OK)
536087719fdSdp 		return (err);
537087719fdSdp 
538087719fdSdp 	if (strcmp(name, curname) == 0)
539087719fdSdp 		return (Z_OK);
540087719fdSdp 
541087719fdSdp 	/*
542087719fdSdp 	 * Switching zone names to one beginning with SUNW is not permitted.
543087719fdSdp 	 */
544087719fdSdp 	if (strncmp(name, "SUNW", 4) == 0)
545087719fdSdp 		return (Z_BOGUS_ZONE_NAME);
546087719fdSdp 
547087719fdSdp 	if ((err = zonecfg_validate_zonename(name)) != Z_OK)
548087719fdSdp 		return (err);
549087719fdSdp 
550087719fdSdp 	/*
551087719fdSdp 	 * Setting the name back to the original name (effectively a revert of
552087719fdSdp 	 * the name) is fine.  But if we carry on, we'll falsely identify the
553087719fdSdp 	 * name as "in use," so special case here.
554087719fdSdp 	 */
555087719fdSdp 	if (strcmp(name, handle->zone_dh_delete_name) == 0) {
556087719fdSdp 		err = setrootattr(handle, DTD_ATTR_NAME, name);
557087719fdSdp 		handle->zone_dh_delete_name[0] = '\0';
558087719fdSdp 		return (err);
559087719fdSdp 	}
560087719fdSdp 
561087719fdSdp 	/* Check to see if new name chosen is already in use */
562087719fdSdp 	if (zone_get_state(name, &state) != Z_NO_ZONE)
563087719fdSdp 		return (Z_NAME_IN_USE);
564087719fdSdp 
565087719fdSdp 	/*
566087719fdSdp 	 * If this isn't already "new" or in a renaming transition, then
567087719fdSdp 	 * we're initiating a rename here; so stash the "delete name"
568087719fdSdp 	 * (i.e. the name of the zone we'll be removing) for the rename.
569087719fdSdp 	 */
570087719fdSdp 	(void) strlcpy(old_delname, handle->zone_dh_delete_name,
571087719fdSdp 	    sizeof (old_delname));
572087719fdSdp 	if (!is_new(handle) && !is_renaming(handle)) {
573087719fdSdp 		/*
574087719fdSdp 		 * Name change is allowed only when the zone we're altering
575087719fdSdp 		 * is not ready or running.
576087719fdSdp 		 */
577087719fdSdp 		err = zone_get_state(curname, &state);
578087719fdSdp 		if (err == Z_OK) {
579087719fdSdp 			if (state > ZONE_STATE_INSTALLED)
580087719fdSdp 				return (Z_BAD_ZONE_STATE);
581087719fdSdp 		} else if (err != Z_NO_ZONE) {
582087719fdSdp 			return (err);
583087719fdSdp 		}
584087719fdSdp 
585087719fdSdp 		(void) strlcpy(handle->zone_dh_delete_name, curname,
586087719fdSdp 		    sizeof (handle->zone_dh_delete_name));
587087719fdSdp 		assert(is_renaming(handle));
588087719fdSdp 	} else if (is_renaming(handle)) {
589087719fdSdp 		err = zone_get_state(handle->zone_dh_delete_name, &state);
590087719fdSdp 		if (err == Z_OK) {
591087719fdSdp 			if (state > ZONE_STATE_INSTALLED)
592087719fdSdp 				return (Z_BAD_ZONE_STATE);
593087719fdSdp 		} else if (err != Z_NO_ZONE) {
594087719fdSdp 			return (err);
595087719fdSdp 		}
596087719fdSdp 	}
597087719fdSdp 
598087719fdSdp 	if ((err = setrootattr(handle, DTD_ATTR_NAME, name)) != Z_OK) {
599087719fdSdp 		/*
600087719fdSdp 		 * Restore the deletename to whatever it was at the
601087719fdSdp 		 * top of the routine, since we've had a failure.
602087719fdSdp 		 */
603087719fdSdp 		(void) strlcpy(handle->zone_dh_delete_name, old_delname,
604087719fdSdp 		    sizeof (handle->zone_dh_delete_name));
605087719fdSdp 		return (err);
606087719fdSdp 	}
607087719fdSdp 
608087719fdSdp 	return (Z_OK);
6097c478bd9Sstevel@tonic-gate }
610a1be23daSdp 
611a1be23daSdp int
612a1be23daSdp zonecfg_get_zonepath(zone_dochandle_t handle, char *path, size_t pathsize)
613a1be23daSdp {
614108322fbScarlsonj 	size_t len;
615108322fbScarlsonj 
616108322fbScarlsonj 	if ((len = strlcpy(path, zonecfg_root, pathsize)) >= pathsize)
617108322fbScarlsonj 		return (Z_TOO_BIG);
618108322fbScarlsonj 	return (getrootattr(handle, DTD_ATTR_ZONEPATH, path + len,
619108322fbScarlsonj 	    pathsize - len));
620a1be23daSdp }
621a1be23daSdp 
622a1be23daSdp int
623a1be23daSdp zonecfg_set_zonepath(zone_dochandle_t handle, char *zonepath)
624a1be23daSdp {
625a1be23daSdp 	return (setrootattr(handle, DTD_ATTR_ZONEPATH, zonepath));
626a1be23daSdp }
627a1be23daSdp 
628a1be23daSdp int
629a1be23daSdp zonecfg_get_autoboot(zone_dochandle_t handle, boolean_t *autoboot)
630a1be23daSdp {
631a1be23daSdp 	char autobootstr[DTD_ENTITY_BOOL_LEN];
632a1be23daSdp 	int ret;
633a1be23daSdp 
634a1be23daSdp 	if ((ret = getrootattr(handle, DTD_ATTR_AUTOBOOT, autobootstr,
635a1be23daSdp 	    sizeof (autobootstr))) != Z_OK)
636a1be23daSdp 		return (ret);
637a1be23daSdp 
638a1be23daSdp 	if (strcmp(autobootstr, DTD_ENTITY_TRUE) == 0)
639a1be23daSdp 		*autoboot = B_TRUE;
640a1be23daSdp 	else if (strcmp(autobootstr, DTD_ENTITY_FALSE) == 0)
641a1be23daSdp 		*autoboot = B_FALSE;
642a1be23daSdp 	else
643a1be23daSdp 		ret = Z_BAD_PROPERTY;
644a1be23daSdp 	return (ret);
645a1be23daSdp }
646a1be23daSdp 
647a1be23daSdp int
648a1be23daSdp zonecfg_set_autoboot(zone_dochandle_t handle, boolean_t autoboot)
649a1be23daSdp {
650a1be23daSdp 	return (setrootattr(handle, DTD_ATTR_AUTOBOOT,
651a1be23daSdp 	    autoboot ? DTD_ENTITY_TRUE : DTD_ENTITY_FALSE));
652a1be23daSdp }
653a1be23daSdp 
654a1be23daSdp int
655a1be23daSdp zonecfg_get_pool(zone_dochandle_t handle, char *pool, size_t poolsize)
656a1be23daSdp {
657a1be23daSdp 	return (getrootattr(handle, DTD_ATTR_POOL, pool, poolsize));
658a1be23daSdp }
659a1be23daSdp 
660a1be23daSdp int
661a1be23daSdp zonecfg_set_pool(zone_dochandle_t handle, char *pool)
662a1be23daSdp {
663a1be23daSdp 	return (setrootattr(handle, DTD_ATTR_POOL, pool));
664a1be23daSdp }
665a1be23daSdp 
666a1be23daSdp /*
667a1be23daSdp  * /etc/zones/index caches a vital piece of information which is also
668a1be23daSdp  * in the <zonename>.xml file: the path to the zone.  This is for performance,
669a1be23daSdp  * since we need to walk all zonepath's in order to be able to detect conflicts
670a1be23daSdp  * (see crosscheck_zonepaths() in the zoneadm command).
671087719fdSdp  *
672087719fdSdp  * An additional complexity is that when doing a rename, we'd like the entire
673087719fdSdp  * index update operation (rename, and potential state changes) to be atomic.
674087719fdSdp  * In general, the operation of this function should succeed or fail as
675087719fdSdp  * a unit.
676a1be23daSdp  */
677a1be23daSdp int
678a1be23daSdp zonecfg_refresh_index_file(zone_dochandle_t handle)
679a1be23daSdp {
680a1be23daSdp 	char name[ZONENAME_MAX], zonepath[MAXPATHLEN];
681a1be23daSdp 	struct zoneent ze;
682a1be23daSdp 	int err;
683087719fdSdp 	int opcode;
684087719fdSdp 	char *zn;
685087719fdSdp 
686087719fdSdp 	bzero(&ze, sizeof (ze));
687087719fdSdp 	ze.zone_state = -1;	/* Preserve existing state in index */
688a1be23daSdp 
689a1be23daSdp 	if ((err = zonecfg_get_name(handle, name, sizeof (name))) != Z_OK)
690a1be23daSdp 		return (err);
691087719fdSdp 	(void) strlcpy(ze.zone_name, name, sizeof (ze.zone_name));
692087719fdSdp 
693a1be23daSdp 	if ((err = zonecfg_get_zonepath(handle, zonepath,
694a1be23daSdp 	    sizeof (zonepath))) != Z_OK)
695a1be23daSdp 		return (err);
696a1be23daSdp 	(void) strlcpy(ze.zone_path, zonepath, sizeof (ze.zone_path));
697087719fdSdp 
698087719fdSdp 	if (is_renaming(handle)) {
699087719fdSdp 		opcode = PZE_MODIFY;
700087719fdSdp 		(void) strlcpy(ze.zone_name, handle->zone_dh_delete_name,
701087719fdSdp 		    sizeof (ze.zone_name));
702087719fdSdp 		(void) strlcpy(ze.zone_newname, name, sizeof (ze.zone_newname));
703087719fdSdp 	} else if (is_new(handle)) {
704087719fdSdp 		FILE *cookie;
705087719fdSdp 		/*
706087719fdSdp 		 * Be tolerant of the zone already existing in the index file,
707087719fdSdp 		 * since we might be forcibly overwriting an existing
708087719fdSdp 		 * configuration with a new one (for example 'create -F'
709087719fdSdp 		 * in zonecfg).
710087719fdSdp 		 */
711087719fdSdp 		opcode = PZE_ADD;
712087719fdSdp 		cookie = setzoneent();
713087719fdSdp 		while ((zn = getzoneent(cookie)) != NULL) {
714087719fdSdp 			if (strcmp(zn, name) == 0) {
715087719fdSdp 				opcode = PZE_MODIFY;
716087719fdSdp 				free(zn);
717087719fdSdp 				break;
718087719fdSdp 			}
719087719fdSdp 			free(zn);
720087719fdSdp 		}
721087719fdSdp 		endzoneent(cookie);
722087719fdSdp 		ze.zone_state = ZONE_STATE_CONFIGURED;
723087719fdSdp 	} else {
724087719fdSdp 		opcode = PZE_MODIFY;
7257c478bd9Sstevel@tonic-gate 	}
7267c478bd9Sstevel@tonic-gate 
727087719fdSdp 	if ((err = putzoneent(&ze, opcode)) != Z_OK)
728087719fdSdp 		return (err);
729087719fdSdp 
730087719fdSdp 	return (Z_OK);
731087719fdSdp }
732087719fdSdp 
733087719fdSdp /*
734087719fdSdp  * The goal of this routine is to cause the index file update and the
735087719fdSdp  * document save to happen as an atomic operation.  We do the document
736087719fdSdp  * first, saving a backup copy using a hard link; if that succeeds, we go
737087719fdSdp  * on to the index.  If that fails, we roll the document back into place.
738087719fdSdp  *
739087719fdSdp  * Strategy:
740087719fdSdp  *
741087719fdSdp  * New zone 'foo' configuration:
742087719fdSdp  * 	Create tmpfile (zonecfg.xxxxxx)
743087719fdSdp  * 	Write XML to tmpfile
744087719fdSdp  * 	Rename tmpfile to xmlfile (zonecfg.xxxxxx -> foo.xml)
745087719fdSdp  * 	Add entry to index file
746087719fdSdp  * 	If it fails, delete foo.xml, leaving nothing behind.
747087719fdSdp  *
748087719fdSdp  * Save existing zone 'foo':
749087719fdSdp  * 	Make backup of foo.xml -> .backup
750087719fdSdp  * 	Create tmpfile (zonecfg.xxxxxx)
751087719fdSdp  * 	Write XML to tmpfile
752087719fdSdp  * 	Rename tmpfile to xmlfile (zonecfg.xxxxxx -> foo.xml)
753087719fdSdp  * 	Modify index file as needed
754087719fdSdp  * 	If it fails, recover from .backup -> foo.xml
755087719fdSdp  *
756087719fdSdp  * Rename 'foo' to 'bar':
757087719fdSdp  * 	Create tmpfile (zonecfg.xxxxxx)
758087719fdSdp  * 	Write XML to tmpfile
759087719fdSdp  * 	Rename tmpfile to xmlfile (zonecfg.xxxxxx -> bar.xml)
760087719fdSdp  * 	Add entry for 'bar' to index file, Remove entry for 'foo' (refresh)
761087719fdSdp  * 	If it fails, delete bar.xml; foo.xml is left behind.
762087719fdSdp  */
7637c478bd9Sstevel@tonic-gate static int
7647c478bd9Sstevel@tonic-gate zonecfg_save_impl(zone_dochandle_t handle, char *filename)
7657c478bd9Sstevel@tonic-gate {
7667c478bd9Sstevel@tonic-gate 	char tmpfile[MAXPATHLEN];
767087719fdSdp 	char bakdir[MAXPATHLEN], bakbase[MAXPATHLEN], bakfile[MAXPATHLEN];
768087719fdSdp 	int tmpfd, err;
7697c478bd9Sstevel@tonic-gate 	xmlValidCtxt cvp = { NULL };
770087719fdSdp 	boolean_t backup;
7717c478bd9Sstevel@tonic-gate 
7727c478bd9Sstevel@tonic-gate 	(void) strlcpy(tmpfile, filename, sizeof (tmpfile));
7737c478bd9Sstevel@tonic-gate 	(void) dirname(tmpfile);
7747c478bd9Sstevel@tonic-gate 	(void) strlcat(tmpfile, _PATH_TMPFILE, sizeof (tmpfile));
7757c478bd9Sstevel@tonic-gate 
7767c478bd9Sstevel@tonic-gate 	tmpfd = mkstemp(tmpfile);
7777c478bd9Sstevel@tonic-gate 	if (tmpfd == -1) {
7787c478bd9Sstevel@tonic-gate 		(void) unlink(tmpfile);
7797c478bd9Sstevel@tonic-gate 		return (Z_TEMP_FILE);
7807c478bd9Sstevel@tonic-gate 	}
7817c478bd9Sstevel@tonic-gate 	(void) close(tmpfd);
7827c478bd9Sstevel@tonic-gate 
7837c478bd9Sstevel@tonic-gate 	cvp.error = zonecfg_error_func;
7847c478bd9Sstevel@tonic-gate 	cvp.warning = zonecfg_error_func;
7857c478bd9Sstevel@tonic-gate 
786087719fdSdp 	/*
787087719fdSdp 	 * We do a final validation of the document-- but the library has
788087719fdSdp 	 * malfunctioned if it fails to validate, so it's an assert.
789087719fdSdp 	 */
790087719fdSdp 	assert(xmlValidateDocument(&cvp, handle->zone_dh_doc) != 0);
7917c478bd9Sstevel@tonic-gate 
7927c478bd9Sstevel@tonic-gate 	if (xmlSaveFormatFile(tmpfile, handle->zone_dh_doc, 1) <= 0)
7937c478bd9Sstevel@tonic-gate 		goto err;
794087719fdSdp 
7957c478bd9Sstevel@tonic-gate 	(void) chmod(tmpfile, 0644);
7967c478bd9Sstevel@tonic-gate 
797087719fdSdp 	/*
798087719fdSdp 	 * In the event we are doing a standard save, hard link a copy of the
799087719fdSdp 	 * original file in .backup.<pid>.filename so we can restore it if
800087719fdSdp 	 * something goes wrong.
801087719fdSdp 	 */
802087719fdSdp 	if (!is_new(handle) && !is_renaming(handle)) {
803087719fdSdp 		backup = B_TRUE;
804087719fdSdp 
805087719fdSdp 		(void) strlcpy(bakdir, filename, sizeof (bakdir));
806087719fdSdp 		(void) strlcpy(bakbase, filename, sizeof (bakbase));
807087719fdSdp 		(void) snprintf(bakfile, sizeof (bakfile), "%s/.backup.%d.%s",
808087719fdSdp 		    dirname(bakdir), getpid(), basename(bakbase));
809087719fdSdp 
810087719fdSdp 		if (link(filename, bakfile) == -1) {
811087719fdSdp 			err = errno;
8127c478bd9Sstevel@tonic-gate 			(void) unlink(tmpfile);
8137c478bd9Sstevel@tonic-gate 			if (errno == EACCES)
8147c478bd9Sstevel@tonic-gate 				return (Z_ACCES);
8157c478bd9Sstevel@tonic-gate 			return (Z_MISC_FS);
8167c478bd9Sstevel@tonic-gate 		}
817087719fdSdp 	}
818a1be23daSdp 
819087719fdSdp 	/*
820087719fdSdp 	 * Move the new document over top of the old.
821087719fdSdp 	 * i.e.:   zonecfg.XXXXXX  ->  myzone.xml
822087719fdSdp 	 */
823087719fdSdp 	if (rename(tmpfile, filename) == -1) {
824087719fdSdp 		err = errno;
825087719fdSdp 		(void) unlink(tmpfile);
826087719fdSdp 		if (backup)
827087719fdSdp 			(void) unlink(bakfile);
828087719fdSdp 		if (err == EACCES)
829087719fdSdp 			return (Z_ACCES);
830087719fdSdp 		return (Z_MISC_FS);
831087719fdSdp 	}
832087719fdSdp 
833087719fdSdp 	/*
834087719fdSdp 	 * If this is a snapshot, we're done-- don't add an index entry.
835087719fdSdp 	 */
836087719fdSdp 	if (is_snapshot(handle))
837087719fdSdp 		return (Z_OK);
838087719fdSdp 
839087719fdSdp 	/* now update the index file to reflect whatever we just did */
840087719fdSdp 	if ((err = zonecfg_refresh_index_file(handle)) != Z_OK) {
841087719fdSdp 		if (backup) {
842087719fdSdp 			/*
843087719fdSdp 			 * Try to restore from our backup.
844087719fdSdp 			 */
845087719fdSdp 			(void) unlink(filename);
846087719fdSdp 			(void) rename(bakfile, filename);
847087719fdSdp 		} else {
848087719fdSdp 			/*
849087719fdSdp 			 * Either the zone is new, in which case we can delete
850087719fdSdp 			 * new.xml, or we're doing a rename, so ditto.
851087719fdSdp 			 */
852087719fdSdp 			assert(is_new(handle) || is_renaming(handle));
853087719fdSdp 			(void) unlink(filename);
854087719fdSdp 		}
855087719fdSdp 		return (Z_UPDATING_INDEX);
856087719fdSdp 	}
857087719fdSdp 
858087719fdSdp 	if (backup)
859087719fdSdp 		(void) unlink(bakfile);
860087719fdSdp 
861087719fdSdp 	return (Z_OK);
8627c478bd9Sstevel@tonic-gate 
8637c478bd9Sstevel@tonic-gate err:
8647c478bd9Sstevel@tonic-gate 	(void) unlink(tmpfile);
8657c478bd9Sstevel@tonic-gate 	return (Z_SAVING_FILE);
8667c478bd9Sstevel@tonic-gate }
8677c478bd9Sstevel@tonic-gate 
8687c478bd9Sstevel@tonic-gate int
8697c478bd9Sstevel@tonic-gate zonecfg_save(zone_dochandle_t handle)
8707c478bd9Sstevel@tonic-gate {
871087719fdSdp 	char zname[ZONENAME_MAX], path[MAXPATHLEN];
872087719fdSdp 	char delpath[MAXPATHLEN];
873087719fdSdp 	int err = Z_SAVING_FILE;
8747c478bd9Sstevel@tonic-gate 
875087719fdSdp 	if (zonecfg_check_handle(handle) != Z_OK)
876087719fdSdp 		return (Z_BAD_HANDLE);
877087719fdSdp 
878087719fdSdp 	/*
879087719fdSdp 	 * We don't support saving snapshots at this time.
880087719fdSdp 	 */
881087719fdSdp 	if (handle->zone_dh_snapshot)
882087719fdSdp 		return (Z_INVAL);
883087719fdSdp 
884087719fdSdp 	if ((err = zonecfg_get_name(handle, zname, sizeof (zname))) != Z_OK)
8857c478bd9Sstevel@tonic-gate 		return (err);
886087719fdSdp 
887108322fbScarlsonj 	if (!config_file_path(zname, path))
888108322fbScarlsonj 		return (Z_MISC_FS);
889087719fdSdp 
890087719fdSdp 	addcomment(handle, "\n    DO NOT EDIT THIS "
891087719fdSdp 	    "FILE.  Use zonecfg(1M) instead.\n");
892087719fdSdp 
893087719fdSdp 	err = zonecfg_save_impl(handle, path);
894087719fdSdp 
895087719fdSdp 	stripcomments(handle);
896087719fdSdp 
897087719fdSdp 	if (err != Z_OK)
898087719fdSdp 		return (err);
899087719fdSdp 
900087719fdSdp 	handle->zone_dh_newzone = B_FALSE;
901087719fdSdp 
902087719fdSdp 	if (is_renaming(handle)) {
903108322fbScarlsonj 		if (config_file_path(handle->zone_dh_delete_name, delpath))
904087719fdSdp 			(void) unlink(delpath);
905087719fdSdp 		handle->zone_dh_delete_name[0] = '\0';
906087719fdSdp 	}
907087719fdSdp 
908087719fdSdp 	return (Z_OK);
9097c478bd9Sstevel@tonic-gate }
9107c478bd9Sstevel@tonic-gate 
9117c478bd9Sstevel@tonic-gate /*
9127c478bd9Sstevel@tonic-gate  * Special case: if access(2) fails with ENOENT, then try again using
9137c478bd9Sstevel@tonic-gate  * ZONE_CONFIG_ROOT instead of config_file_path(zonename).  This is how we
9147c478bd9Sstevel@tonic-gate  * work around the case of a config file which has not been created yet:
9157c478bd9Sstevel@tonic-gate  * the user will need access to the directory so use that as a heuristic.
9167c478bd9Sstevel@tonic-gate  */
9177c478bd9Sstevel@tonic-gate 
9187c478bd9Sstevel@tonic-gate int
9197c478bd9Sstevel@tonic-gate zonecfg_access(const char *zonename, int amode)
9207c478bd9Sstevel@tonic-gate {
9217c478bd9Sstevel@tonic-gate 	char path[MAXPATHLEN];
9227c478bd9Sstevel@tonic-gate 
923108322fbScarlsonj 	if (!config_file_path(zonename, path))
924108322fbScarlsonj 		return (Z_INVAL);
9257c478bd9Sstevel@tonic-gate 	if (access(path, amode) == 0)
9267c478bd9Sstevel@tonic-gate 		return (Z_OK);
927108322fbScarlsonj 	if (errno == ENOENT) {
928108322fbScarlsonj 		if (snprintf(path, sizeof (path), "%s%s", zonecfg_root,
929108322fbScarlsonj 		    ZONE_CONFIG_ROOT) >= sizeof (path))
930108322fbScarlsonj 			return (Z_INVAL);
931108322fbScarlsonj 		if (access(path, amode) == 0)
9327c478bd9Sstevel@tonic-gate 			return (Z_OK);
933108322fbScarlsonj 	}
9347c478bd9Sstevel@tonic-gate 	if (errno == EACCES)
9357c478bd9Sstevel@tonic-gate 		return (Z_ACCES);
9367c478bd9Sstevel@tonic-gate 	if (errno == EINVAL)
9377c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
9387c478bd9Sstevel@tonic-gate 	return (Z_MISC_FS);
9397c478bd9Sstevel@tonic-gate }
9407c478bd9Sstevel@tonic-gate 
9417c478bd9Sstevel@tonic-gate int
942108322fbScarlsonj zonecfg_create_snapshot(const char *zonename)
9437c478bd9Sstevel@tonic-gate {
9447c478bd9Sstevel@tonic-gate 	zone_dochandle_t handle;
9457c478bd9Sstevel@tonic-gate 	char path[MAXPATHLEN], zonepath[MAXPATHLEN], rpath[MAXPATHLEN];
9467c478bd9Sstevel@tonic-gate 	int error = Z_OK, res;
9477c478bd9Sstevel@tonic-gate 
9487c478bd9Sstevel@tonic-gate 	if ((handle = zonecfg_init_handle()) == NULL) {
9497c478bd9Sstevel@tonic-gate 		return (Z_NOMEM);
9507c478bd9Sstevel@tonic-gate 	}
9517c478bd9Sstevel@tonic-gate 
952087719fdSdp 	handle->zone_dh_newzone = B_TRUE;
953087719fdSdp 	handle->zone_dh_snapshot = B_TRUE;
954087719fdSdp 
9557c478bd9Sstevel@tonic-gate 	if ((error = zonecfg_get_handle(zonename, handle)) != Z_OK)
9567c478bd9Sstevel@tonic-gate 		goto out;
9577c478bd9Sstevel@tonic-gate 	if ((error = operation_prep(handle)) != Z_OK)
9587c478bd9Sstevel@tonic-gate 		goto out;
9597c478bd9Sstevel@tonic-gate 	error = zonecfg_get_zonepath(handle, zonepath, sizeof (zonepath));
9607c478bd9Sstevel@tonic-gate 	if (error != Z_OK)
9617c478bd9Sstevel@tonic-gate 		goto out;
9627c478bd9Sstevel@tonic-gate 	if ((res = resolvepath(zonepath, rpath, sizeof (rpath))) == -1) {
9637c478bd9Sstevel@tonic-gate 		error = Z_RESOLVED_PATH;
9647c478bd9Sstevel@tonic-gate 		goto out;
9657c478bd9Sstevel@tonic-gate 	}
9667c478bd9Sstevel@tonic-gate 	/*
9677c478bd9Sstevel@tonic-gate 	 * If the resolved path is not the same as the original path, then
9687c478bd9Sstevel@tonic-gate 	 * save the resolved path in the snapshot, thus preventing any
9697c478bd9Sstevel@tonic-gate 	 * potential problems down the line when zoneadmd goes to unmount
9707c478bd9Sstevel@tonic-gate 	 * file systems and depends on initial string matches with resolved
9717c478bd9Sstevel@tonic-gate 	 * paths.
9727c478bd9Sstevel@tonic-gate 	 */
9737c478bd9Sstevel@tonic-gate 	rpath[res] = '\0';
9747c478bd9Sstevel@tonic-gate 	if (strcmp(zonepath, rpath) != 0) {
9757c478bd9Sstevel@tonic-gate 		if ((error = zonecfg_set_zonepath(handle, rpath)) != Z_OK)
9767c478bd9Sstevel@tonic-gate 			goto out;
9777c478bd9Sstevel@tonic-gate 	}
978108322fbScarlsonj 	if (snprintf(path, sizeof (path), "%s%s", zonecfg_root,
979108322fbScarlsonj 	    ZONE_SNAPSHOT_ROOT) >= sizeof (path)) {
980108322fbScarlsonj 		error = Z_MISC_FS;
981108322fbScarlsonj 		goto out;
982108322fbScarlsonj 	}
983108322fbScarlsonj 	if ((mkdir(path, S_IRWXU) == -1) && (errno != EEXIST)) {
9847c478bd9Sstevel@tonic-gate 		error = Z_MISC_FS;
9857c478bd9Sstevel@tonic-gate 		goto out;
9867c478bd9Sstevel@tonic-gate 	}
9877c478bd9Sstevel@tonic-gate 
988108322fbScarlsonj 	if (!snap_file_path(zonename, path)) {
989108322fbScarlsonj 		error = Z_MISC_FS;
990108322fbScarlsonj 		goto out;
991108322fbScarlsonj 	}
992087719fdSdp 
993087719fdSdp 	addcomment(handle, "\n    DO NOT EDIT THIS FILE.  "
994087719fdSdp 	    "It is a snapshot of running zone state.\n");
995087719fdSdp 
9967c478bd9Sstevel@tonic-gate 	error = zonecfg_save_impl(handle, path);
9977c478bd9Sstevel@tonic-gate 
998087719fdSdp 	stripcomments(handle);
999087719fdSdp 
10007c478bd9Sstevel@tonic-gate out:
10017c478bd9Sstevel@tonic-gate 	zonecfg_fini_handle(handle);
10027c478bd9Sstevel@tonic-gate 	return (error);
10037c478bd9Sstevel@tonic-gate }
10047c478bd9Sstevel@tonic-gate 
10057c478bd9Sstevel@tonic-gate static int
1006a1be23daSdp newprop(xmlNodePtr node, const xmlChar *attrname, char *src)
10077c478bd9Sstevel@tonic-gate {
10087c478bd9Sstevel@tonic-gate 	xmlAttrPtr newattr;
10097c478bd9Sstevel@tonic-gate 
10107c478bd9Sstevel@tonic-gate 	newattr = xmlNewProp(node, attrname, (xmlChar *)src);
10117c478bd9Sstevel@tonic-gate 	if (newattr == NULL) {
10127c478bd9Sstevel@tonic-gate 		xmlUnlinkNode(node);
10137c478bd9Sstevel@tonic-gate 		xmlFreeNode(node);
10147c478bd9Sstevel@tonic-gate 		return (Z_BAD_PROPERTY);
10157c478bd9Sstevel@tonic-gate 	}
10167c478bd9Sstevel@tonic-gate 	return (Z_OK);
10177c478bd9Sstevel@tonic-gate }
10187c478bd9Sstevel@tonic-gate 
10197c478bd9Sstevel@tonic-gate static int
10207c478bd9Sstevel@tonic-gate zonecfg_add_filesystem_core(zone_dochandle_t handle, struct zone_fstab *tabptr)
10217c478bd9Sstevel@tonic-gate {
10227c478bd9Sstevel@tonic-gate 	xmlNodePtr newnode, cur = handle->zone_dh_cur, options_node;
10237c478bd9Sstevel@tonic-gate 	zone_fsopt_t *ptr;
10247c478bd9Sstevel@tonic-gate 	int err;
10257c478bd9Sstevel@tonic-gate 
10267c478bd9Sstevel@tonic-gate 	newnode = xmlNewTextChild(cur, NULL, DTD_ELEM_FS, NULL);
1027a1be23daSdp 	if ((err = newprop(newnode, DTD_ATTR_SPECIAL,
10287c478bd9Sstevel@tonic-gate 	    tabptr->zone_fs_special)) != Z_OK)
10297c478bd9Sstevel@tonic-gate 		return (err);
10307c478bd9Sstevel@tonic-gate 	if (tabptr->zone_fs_raw[0] != '\0' &&
1031a1be23daSdp 	    (err = newprop(newnode, DTD_ATTR_RAW, tabptr->zone_fs_raw)) != Z_OK)
10327c478bd9Sstevel@tonic-gate 		return (err);
1033a1be23daSdp 	if ((err = newprop(newnode, DTD_ATTR_DIR, tabptr->zone_fs_dir)) != Z_OK)
10347c478bd9Sstevel@tonic-gate 		return (err);
1035a1be23daSdp 	if ((err = newprop(newnode, DTD_ATTR_TYPE,
10367c478bd9Sstevel@tonic-gate 	    tabptr->zone_fs_type)) != Z_OK)
10377c478bd9Sstevel@tonic-gate 		return (err);
10387c478bd9Sstevel@tonic-gate 	if (tabptr->zone_fs_options != NULL) {
10397c478bd9Sstevel@tonic-gate 		for (ptr = tabptr->zone_fs_options; ptr != NULL;
10407c478bd9Sstevel@tonic-gate 		    ptr = ptr->zone_fsopt_next) {
10417c478bd9Sstevel@tonic-gate 			options_node = xmlNewTextChild(newnode, NULL,
10427c478bd9Sstevel@tonic-gate 			    DTD_ELEM_FSOPTION, NULL);
1043a1be23daSdp 			if ((err = newprop(options_node, DTD_ATTR_NAME,
10447c478bd9Sstevel@tonic-gate 			    ptr->zone_fsopt_opt)) != Z_OK)
10457c478bd9Sstevel@tonic-gate 				return (err);
10467c478bd9Sstevel@tonic-gate 		}
10477c478bd9Sstevel@tonic-gate 	}
10487c478bd9Sstevel@tonic-gate 	return (Z_OK);
10497c478bd9Sstevel@tonic-gate }
10507c478bd9Sstevel@tonic-gate 
10517c478bd9Sstevel@tonic-gate int
10527c478bd9Sstevel@tonic-gate zonecfg_add_filesystem(zone_dochandle_t handle, struct zone_fstab *tabptr)
10537c478bd9Sstevel@tonic-gate {
10547c478bd9Sstevel@tonic-gate 	int err;
10557c478bd9Sstevel@tonic-gate 
10567c478bd9Sstevel@tonic-gate 	if (tabptr == NULL)
10577c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
10587c478bd9Sstevel@tonic-gate 
10597c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
10607c478bd9Sstevel@tonic-gate 		return (err);
10617c478bd9Sstevel@tonic-gate 
10627c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_add_filesystem_core(handle, tabptr)) != Z_OK)
10637c478bd9Sstevel@tonic-gate 		return (err);
10647c478bd9Sstevel@tonic-gate 
10657c478bd9Sstevel@tonic-gate 	return (Z_OK);
10667c478bd9Sstevel@tonic-gate }
10677c478bd9Sstevel@tonic-gate 
10687c478bd9Sstevel@tonic-gate static int
10697c478bd9Sstevel@tonic-gate zonecfg_add_ipd_core(zone_dochandle_t handle, struct zone_fstab *tabptr)
10707c478bd9Sstevel@tonic-gate {
10717c478bd9Sstevel@tonic-gate 	xmlNodePtr newnode, cur = handle->zone_dh_cur;
10727c478bd9Sstevel@tonic-gate 	int err;
10737c478bd9Sstevel@tonic-gate 
10747c478bd9Sstevel@tonic-gate 	newnode = xmlNewTextChild(cur, NULL, DTD_ELEM_IPD, NULL);
1075a1be23daSdp 	if ((err = newprop(newnode, DTD_ATTR_DIR, tabptr->zone_fs_dir)) != Z_OK)
10767c478bd9Sstevel@tonic-gate 		return (err);
10777c478bd9Sstevel@tonic-gate 	return (Z_OK);
10787c478bd9Sstevel@tonic-gate }
10797c478bd9Sstevel@tonic-gate 
10807c478bd9Sstevel@tonic-gate int
10817c478bd9Sstevel@tonic-gate zonecfg_add_ipd(zone_dochandle_t handle, struct zone_fstab *tabptr)
10827c478bd9Sstevel@tonic-gate {
10837c478bd9Sstevel@tonic-gate 	int err;
10847c478bd9Sstevel@tonic-gate 
10857c478bd9Sstevel@tonic-gate 	if (tabptr == NULL)
10867c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
10877c478bd9Sstevel@tonic-gate 
10887c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
10897c478bd9Sstevel@tonic-gate 		return (err);
10907c478bd9Sstevel@tonic-gate 
10917c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_add_ipd_core(handle, tabptr)) != Z_OK)
10927c478bd9Sstevel@tonic-gate 		return (err);
10937c478bd9Sstevel@tonic-gate 
10947c478bd9Sstevel@tonic-gate 	return (Z_OK);
10957c478bd9Sstevel@tonic-gate }
10967c478bd9Sstevel@tonic-gate 
10977c478bd9Sstevel@tonic-gate int
10987c478bd9Sstevel@tonic-gate zonecfg_add_fs_option(struct zone_fstab *tabptr, char *option)
10997c478bd9Sstevel@tonic-gate {
11007c478bd9Sstevel@tonic-gate 	zone_fsopt_t *last, *old, *new;
11017c478bd9Sstevel@tonic-gate 
11027c478bd9Sstevel@tonic-gate 	last = tabptr->zone_fs_options;
11037c478bd9Sstevel@tonic-gate 	for (old = last; old != NULL; old = old->zone_fsopt_next)
11047c478bd9Sstevel@tonic-gate 		last = old;	/* walk to the end of the list */
11057c478bd9Sstevel@tonic-gate 	new = (zone_fsopt_t *)malloc(sizeof (zone_fsopt_t));
11067c478bd9Sstevel@tonic-gate 	if (new == NULL)
11077c478bd9Sstevel@tonic-gate 		return (Z_NOMEM);
11087c478bd9Sstevel@tonic-gate 	(void) strlcpy(new->zone_fsopt_opt, option,
11097c478bd9Sstevel@tonic-gate 	    sizeof (new->zone_fsopt_opt));
11107c478bd9Sstevel@tonic-gate 	new->zone_fsopt_next = NULL;
11117c478bd9Sstevel@tonic-gate 	if (last == NULL)
11127c478bd9Sstevel@tonic-gate 		tabptr->zone_fs_options = new;
11137c478bd9Sstevel@tonic-gate 	else
11147c478bd9Sstevel@tonic-gate 		last->zone_fsopt_next = new;
11157c478bd9Sstevel@tonic-gate 	return (Z_OK);
11167c478bd9Sstevel@tonic-gate }
11177c478bd9Sstevel@tonic-gate 
11187c478bd9Sstevel@tonic-gate int
11197c478bd9Sstevel@tonic-gate zonecfg_remove_fs_option(struct zone_fstab *tabptr, char *option)
11207c478bd9Sstevel@tonic-gate {
11217c478bd9Sstevel@tonic-gate 	zone_fsopt_t *last, *this, *next;
11227c478bd9Sstevel@tonic-gate 
11237c478bd9Sstevel@tonic-gate 	last = tabptr->zone_fs_options;
11247c478bd9Sstevel@tonic-gate 	for (this = last; this != NULL; this = this->zone_fsopt_next) {
11257c478bd9Sstevel@tonic-gate 		if (strcmp(this->zone_fsopt_opt, option) == 0) {
11267c478bd9Sstevel@tonic-gate 			next = this->zone_fsopt_next;
11277c478bd9Sstevel@tonic-gate 			if (this == tabptr->zone_fs_options)
11287c478bd9Sstevel@tonic-gate 				tabptr->zone_fs_options = next;
11297c478bd9Sstevel@tonic-gate 			else
11307c478bd9Sstevel@tonic-gate 				last->zone_fsopt_next = next;
11317c478bd9Sstevel@tonic-gate 			free(this);
11327c478bd9Sstevel@tonic-gate 			return (Z_OK);
11337c478bd9Sstevel@tonic-gate 		} else
11347c478bd9Sstevel@tonic-gate 			last = this;
11357c478bd9Sstevel@tonic-gate 	}
11367c478bd9Sstevel@tonic-gate 	return (Z_NO_PROPERTY_ID);
11377c478bd9Sstevel@tonic-gate }
11387c478bd9Sstevel@tonic-gate 
11397c478bd9Sstevel@tonic-gate void
11407c478bd9Sstevel@tonic-gate zonecfg_free_fs_option_list(zone_fsopt_t *list)
11417c478bd9Sstevel@tonic-gate {
11427c478bd9Sstevel@tonic-gate 	zone_fsopt_t *this, *next;
11437c478bd9Sstevel@tonic-gate 
11447c478bd9Sstevel@tonic-gate 	for (this = list; this != NULL; this = next) {
11457c478bd9Sstevel@tonic-gate 		next = this->zone_fsopt_next;
11467c478bd9Sstevel@tonic-gate 		free(this);
11477c478bd9Sstevel@tonic-gate 	}
11487c478bd9Sstevel@tonic-gate }
11497c478bd9Sstevel@tonic-gate 
11507c478bd9Sstevel@tonic-gate void
11517c478bd9Sstevel@tonic-gate zonecfg_free_rctl_value_list(struct zone_rctlvaltab *valtab)
11527c478bd9Sstevel@tonic-gate {
11537c478bd9Sstevel@tonic-gate 	if (valtab == NULL)
11547c478bd9Sstevel@tonic-gate 		return;
11557c478bd9Sstevel@tonic-gate 	zonecfg_free_rctl_value_list(valtab->zone_rctlval_next);
11567c478bd9Sstevel@tonic-gate 	free(valtab);
11577c478bd9Sstevel@tonic-gate }
11587c478bd9Sstevel@tonic-gate 
11597c478bd9Sstevel@tonic-gate static boolean_t
11607c478bd9Sstevel@tonic-gate match_prop(xmlNodePtr cur, const xmlChar *attr, char *user_prop)
11617c478bd9Sstevel@tonic-gate {
11627c478bd9Sstevel@tonic-gate 	xmlChar *gotten_prop;
11637c478bd9Sstevel@tonic-gate 	int prop_result;
11647c478bd9Sstevel@tonic-gate 
11657c478bd9Sstevel@tonic-gate 	gotten_prop = xmlGetProp(cur, attr);
11667c478bd9Sstevel@tonic-gate 	if (gotten_prop == NULL)	/* shouldn't happen */
11677c478bd9Sstevel@tonic-gate 		return (B_FALSE);
11687c478bd9Sstevel@tonic-gate 	prop_result = xmlStrcmp(gotten_prop, (const xmlChar *) user_prop);
11697c478bd9Sstevel@tonic-gate 	xmlFree(gotten_prop);
11707c478bd9Sstevel@tonic-gate 	return ((prop_result == 0));
11717c478bd9Sstevel@tonic-gate }
11727c478bd9Sstevel@tonic-gate 
11737c478bd9Sstevel@tonic-gate static int
11747c478bd9Sstevel@tonic-gate zonecfg_delete_filesystem_core(zone_dochandle_t handle,
11757c478bd9Sstevel@tonic-gate     struct zone_fstab *tabptr)
11767c478bd9Sstevel@tonic-gate {
11777c478bd9Sstevel@tonic-gate 	xmlNodePtr cur = handle->zone_dh_cur;
11787c478bd9Sstevel@tonic-gate 	boolean_t dir_match, spec_match, raw_match, type_match;
11797c478bd9Sstevel@tonic-gate 
11807c478bd9Sstevel@tonic-gate 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
11817c478bd9Sstevel@tonic-gate 		if (xmlStrcmp(cur->name, DTD_ELEM_FS))
11827c478bd9Sstevel@tonic-gate 			continue;
11837c478bd9Sstevel@tonic-gate 		dir_match = match_prop(cur, DTD_ATTR_DIR, tabptr->zone_fs_dir);
11847c478bd9Sstevel@tonic-gate 		spec_match = match_prop(cur, DTD_ATTR_SPECIAL,
11857c478bd9Sstevel@tonic-gate 		    tabptr->zone_fs_special);
11867c478bd9Sstevel@tonic-gate 		raw_match = match_prop(cur, DTD_ATTR_RAW,
11877c478bd9Sstevel@tonic-gate 		    tabptr->zone_fs_raw);
11887c478bd9Sstevel@tonic-gate 		type_match = match_prop(cur, DTD_ATTR_TYPE,
11897c478bd9Sstevel@tonic-gate 		    tabptr->zone_fs_type);
11907c478bd9Sstevel@tonic-gate 		if (dir_match && spec_match && raw_match && type_match) {
11917c478bd9Sstevel@tonic-gate 			xmlUnlinkNode(cur);
11927c478bd9Sstevel@tonic-gate 			xmlFreeNode(cur);
11937c478bd9Sstevel@tonic-gate 			return (Z_OK);
11947c478bd9Sstevel@tonic-gate 		}
11957c478bd9Sstevel@tonic-gate 	}
11967c478bd9Sstevel@tonic-gate 	return (Z_NO_RESOURCE_ID);
11977c478bd9Sstevel@tonic-gate }
11987c478bd9Sstevel@tonic-gate 
11997c478bd9Sstevel@tonic-gate int
12007c478bd9Sstevel@tonic-gate zonecfg_delete_filesystem(zone_dochandle_t handle, struct zone_fstab *tabptr)
12017c478bd9Sstevel@tonic-gate {
12027c478bd9Sstevel@tonic-gate 	int err;
12037c478bd9Sstevel@tonic-gate 
12047c478bd9Sstevel@tonic-gate 	if (tabptr == NULL)
12057c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
12067c478bd9Sstevel@tonic-gate 
12077c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
12087c478bd9Sstevel@tonic-gate 		return (err);
12097c478bd9Sstevel@tonic-gate 
12107c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_delete_filesystem_core(handle, tabptr)) != Z_OK)
12117c478bd9Sstevel@tonic-gate 		return (err);
12127c478bd9Sstevel@tonic-gate 
12137c478bd9Sstevel@tonic-gate 	return (Z_OK);
12147c478bd9Sstevel@tonic-gate }
12157c478bd9Sstevel@tonic-gate 
12167c478bd9Sstevel@tonic-gate int
12177c478bd9Sstevel@tonic-gate zonecfg_modify_filesystem(
12187c478bd9Sstevel@tonic-gate 	zone_dochandle_t handle,
12197c478bd9Sstevel@tonic-gate 	struct zone_fstab *oldtabptr,
12207c478bd9Sstevel@tonic-gate 	struct zone_fstab *newtabptr)
12217c478bd9Sstevel@tonic-gate {
12227c478bd9Sstevel@tonic-gate 	int err;
12237c478bd9Sstevel@tonic-gate 
12247c478bd9Sstevel@tonic-gate 	if (oldtabptr == NULL || newtabptr == NULL)
12257c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
12267c478bd9Sstevel@tonic-gate 
12277c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
12287c478bd9Sstevel@tonic-gate 		return (err);
12297c478bd9Sstevel@tonic-gate 
12307c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_delete_filesystem_core(handle, oldtabptr)) != Z_OK)
12317c478bd9Sstevel@tonic-gate 		return (err);
12327c478bd9Sstevel@tonic-gate 
12337c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_add_filesystem_core(handle, newtabptr)) != Z_OK)
12347c478bd9Sstevel@tonic-gate 		return (err);
12357c478bd9Sstevel@tonic-gate 
12367c478bd9Sstevel@tonic-gate 	return (Z_OK);
12377c478bd9Sstevel@tonic-gate }
12387c478bd9Sstevel@tonic-gate 
12397c478bd9Sstevel@tonic-gate static int
12407c478bd9Sstevel@tonic-gate zonecfg_delete_ipd_core(zone_dochandle_t handle, struct zone_fstab *tabptr)
12417c478bd9Sstevel@tonic-gate {
12427c478bd9Sstevel@tonic-gate 	xmlNodePtr cur = handle->zone_dh_cur;
12437c478bd9Sstevel@tonic-gate 
12447c478bd9Sstevel@tonic-gate 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
12457c478bd9Sstevel@tonic-gate 		if (xmlStrcmp(cur->name, DTD_ELEM_IPD))
12467c478bd9Sstevel@tonic-gate 			continue;
12477c478bd9Sstevel@tonic-gate 		if (match_prop(cur, DTD_ATTR_DIR, tabptr->zone_fs_dir)) {
12487c478bd9Sstevel@tonic-gate 			xmlUnlinkNode(cur);
12497c478bd9Sstevel@tonic-gate 			xmlFreeNode(cur);
12507c478bd9Sstevel@tonic-gate 			return (Z_OK);
12517c478bd9Sstevel@tonic-gate 		}
12527c478bd9Sstevel@tonic-gate 	}
12537c478bd9Sstevel@tonic-gate 	return (Z_NO_RESOURCE_ID);
12547c478bd9Sstevel@tonic-gate }
12557c478bd9Sstevel@tonic-gate 
12567c478bd9Sstevel@tonic-gate int
12577c478bd9Sstevel@tonic-gate zonecfg_delete_ipd(zone_dochandle_t handle, struct zone_fstab *tabptr)
12587c478bd9Sstevel@tonic-gate {
12597c478bd9Sstevel@tonic-gate 	int err;
12607c478bd9Sstevel@tonic-gate 
12617c478bd9Sstevel@tonic-gate 	if (tabptr == NULL)
12627c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
12637c478bd9Sstevel@tonic-gate 
12647c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
12657c478bd9Sstevel@tonic-gate 		return (err);
12667c478bd9Sstevel@tonic-gate 
12677c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_delete_ipd_core(handle, tabptr)) != Z_OK)
12687c478bd9Sstevel@tonic-gate 		return (err);
12697c478bd9Sstevel@tonic-gate 
12707c478bd9Sstevel@tonic-gate 	return (Z_OK);
12717c478bd9Sstevel@tonic-gate }
12727c478bd9Sstevel@tonic-gate 
12737c478bd9Sstevel@tonic-gate int
12747c478bd9Sstevel@tonic-gate zonecfg_modify_ipd(zone_dochandle_t handle, struct zone_fstab *oldtabptr,
12757c478bd9Sstevel@tonic-gate     struct zone_fstab *newtabptr)
12767c478bd9Sstevel@tonic-gate {
12777c478bd9Sstevel@tonic-gate 	int err;
12787c478bd9Sstevel@tonic-gate 
12797c478bd9Sstevel@tonic-gate 	if (oldtabptr == NULL || newtabptr == NULL)
12807c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
12817c478bd9Sstevel@tonic-gate 
12827c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
12837c478bd9Sstevel@tonic-gate 		return (err);
12847c478bd9Sstevel@tonic-gate 
12857c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_delete_ipd_core(handle, oldtabptr)) != Z_OK)
12867c478bd9Sstevel@tonic-gate 		return (err);
12877c478bd9Sstevel@tonic-gate 
12887c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_add_ipd_core(handle, newtabptr)) != Z_OK)
12897c478bd9Sstevel@tonic-gate 		return (err);
12907c478bd9Sstevel@tonic-gate 
12917c478bd9Sstevel@tonic-gate 	return (Z_OK);
12927c478bd9Sstevel@tonic-gate }
12937c478bd9Sstevel@tonic-gate 
12947c478bd9Sstevel@tonic-gate static int
12957c478bd9Sstevel@tonic-gate fetchprop(xmlNodePtr cur, const xmlChar *propname, char *dst, size_t dstsize)
12967c478bd9Sstevel@tonic-gate {
12977c478bd9Sstevel@tonic-gate 	xmlChar *property;
12987c478bd9Sstevel@tonic-gate 	size_t srcsize;
12997c478bd9Sstevel@tonic-gate 
13007c478bd9Sstevel@tonic-gate 	if ((property = xmlGetProp(cur, propname)) == NULL)
13017c478bd9Sstevel@tonic-gate 		return (Z_BAD_PROPERTY);
13027c478bd9Sstevel@tonic-gate 	srcsize = strlcpy(dst, (char *)property, dstsize);
13037c478bd9Sstevel@tonic-gate 	xmlFree(property);
13047c478bd9Sstevel@tonic-gate 	if (srcsize >= dstsize)
13057c478bd9Sstevel@tonic-gate 		return (Z_TOO_BIG);
13067c478bd9Sstevel@tonic-gate 	return (Z_OK);
13077c478bd9Sstevel@tonic-gate }
13087c478bd9Sstevel@tonic-gate 
13097c478bd9Sstevel@tonic-gate int
13107c478bd9Sstevel@tonic-gate zonecfg_lookup_filesystem(
13117c478bd9Sstevel@tonic-gate 	zone_dochandle_t handle,
13127c478bd9Sstevel@tonic-gate 	struct zone_fstab *tabptr)
13137c478bd9Sstevel@tonic-gate {
13147c478bd9Sstevel@tonic-gate 	xmlNodePtr cur, options, firstmatch;
13157c478bd9Sstevel@tonic-gate 	int err;
13167c478bd9Sstevel@tonic-gate 	char dirname[MAXPATHLEN], special[MAXPATHLEN], raw[MAXPATHLEN];
13177c478bd9Sstevel@tonic-gate 	char type[FSTYPSZ];
13187c478bd9Sstevel@tonic-gate 	char options_str[MAX_MNTOPT_STR];
13197c478bd9Sstevel@tonic-gate 
13207c478bd9Sstevel@tonic-gate 	if (tabptr == NULL)
13217c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
13227c478bd9Sstevel@tonic-gate 
13237c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
13247c478bd9Sstevel@tonic-gate 		return (err);
13257c478bd9Sstevel@tonic-gate 
13267c478bd9Sstevel@tonic-gate 	/*
13277c478bd9Sstevel@tonic-gate 	 * Walk the list of children looking for matches on any properties
13287c478bd9Sstevel@tonic-gate 	 * specified in the fstab parameter.  If more than one resource
13297c478bd9Sstevel@tonic-gate 	 * matches, we return Z_INSUFFICIENT_SPEC; if none match, we return
13307c478bd9Sstevel@tonic-gate 	 * Z_NO_RESOURCE_ID.
13317c478bd9Sstevel@tonic-gate 	 */
13327c478bd9Sstevel@tonic-gate 	cur = handle->zone_dh_cur;
13337c478bd9Sstevel@tonic-gate 	firstmatch = NULL;
13347c478bd9Sstevel@tonic-gate 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
13357c478bd9Sstevel@tonic-gate 		if (xmlStrcmp(cur->name, DTD_ELEM_FS))
13367c478bd9Sstevel@tonic-gate 			continue;
13377c478bd9Sstevel@tonic-gate 		if (strlen(tabptr->zone_fs_dir) > 0) {
13387c478bd9Sstevel@tonic-gate 			if ((fetchprop(cur, DTD_ATTR_DIR, dirname,
13397c478bd9Sstevel@tonic-gate 			    sizeof (dirname)) == Z_OK) &&
13407c478bd9Sstevel@tonic-gate 			    (strcmp(tabptr->zone_fs_dir, dirname) == 0)) {
13417c478bd9Sstevel@tonic-gate 				if (firstmatch == NULL)
13427c478bd9Sstevel@tonic-gate 					firstmatch = cur;
13437c478bd9Sstevel@tonic-gate 				else
13447c478bd9Sstevel@tonic-gate 					return (Z_INSUFFICIENT_SPEC);
13457c478bd9Sstevel@tonic-gate 			}
13467c478bd9Sstevel@tonic-gate 		}
13477c478bd9Sstevel@tonic-gate 		if (strlen(tabptr->zone_fs_special) > 0) {
13487c478bd9Sstevel@tonic-gate 			if ((fetchprop(cur, DTD_ATTR_SPECIAL, special,
13497c478bd9Sstevel@tonic-gate 			    sizeof (special)) == Z_OK)) {
13507c478bd9Sstevel@tonic-gate 				if (strcmp(tabptr->zone_fs_special,
13517c478bd9Sstevel@tonic-gate 				    special) == 0) {
13527c478bd9Sstevel@tonic-gate 					if (firstmatch == NULL)
13537c478bd9Sstevel@tonic-gate 						firstmatch = cur;
13547c478bd9Sstevel@tonic-gate 					else if (firstmatch != cur)
13557c478bd9Sstevel@tonic-gate 						return (Z_INSUFFICIENT_SPEC);
13567c478bd9Sstevel@tonic-gate 				} else {
13577c478bd9Sstevel@tonic-gate 					/*
13587c478bd9Sstevel@tonic-gate 					 * If another property matched but this
13597c478bd9Sstevel@tonic-gate 					 * one doesn't then reset firstmatch.
13607c478bd9Sstevel@tonic-gate 					 */
13617c478bd9Sstevel@tonic-gate 					if (firstmatch == cur)
13627c478bd9Sstevel@tonic-gate 						firstmatch = NULL;
13637c478bd9Sstevel@tonic-gate 				}
13647c478bd9Sstevel@tonic-gate 			}
13657c478bd9Sstevel@tonic-gate 		}
13667c478bd9Sstevel@tonic-gate 		if (strlen(tabptr->zone_fs_raw) > 0) {
13677c478bd9Sstevel@tonic-gate 			if ((fetchprop(cur, DTD_ATTR_RAW, raw,
13687c478bd9Sstevel@tonic-gate 			    sizeof (raw)) == Z_OK)) {
13697c478bd9Sstevel@tonic-gate 				if (strcmp(tabptr->zone_fs_raw, raw) == 0) {
13707c478bd9Sstevel@tonic-gate 					if (firstmatch == NULL)
13717c478bd9Sstevel@tonic-gate 						firstmatch = cur;
13727c478bd9Sstevel@tonic-gate 					else if (firstmatch != cur)
13737c478bd9Sstevel@tonic-gate 						return (Z_INSUFFICIENT_SPEC);
13747c478bd9Sstevel@tonic-gate 				} else {
13757c478bd9Sstevel@tonic-gate 					/*
13767c478bd9Sstevel@tonic-gate 					 * If another property matched but this
13777c478bd9Sstevel@tonic-gate 					 * one doesn't then reset firstmatch.
13787c478bd9Sstevel@tonic-gate 					 */
13797c478bd9Sstevel@tonic-gate 					if (firstmatch == cur)
13807c478bd9Sstevel@tonic-gate 						firstmatch = NULL;
13817c478bd9Sstevel@tonic-gate 				}
13827c478bd9Sstevel@tonic-gate 			}
13837c478bd9Sstevel@tonic-gate 		}
13847c478bd9Sstevel@tonic-gate 		if (strlen(tabptr->zone_fs_type) > 0) {
13857c478bd9Sstevel@tonic-gate 			if ((fetchprop(cur, DTD_ATTR_TYPE, type,
13867c478bd9Sstevel@tonic-gate 			    sizeof (type)) == Z_OK)) {
13877c478bd9Sstevel@tonic-gate 				if (strcmp(tabptr->zone_fs_type, type) == 0) {
13887c478bd9Sstevel@tonic-gate 					if (firstmatch == NULL)
13897c478bd9Sstevel@tonic-gate 						firstmatch = cur;
13907c478bd9Sstevel@tonic-gate 					else if (firstmatch != cur)
13917c478bd9Sstevel@tonic-gate 						return (Z_INSUFFICIENT_SPEC);
13927c478bd9Sstevel@tonic-gate 				} else {
13937c478bd9Sstevel@tonic-gate 					/*
13947c478bd9Sstevel@tonic-gate 					 * If another property matched but this
13957c478bd9Sstevel@tonic-gate 					 * one doesn't then reset firstmatch.
13967c478bd9Sstevel@tonic-gate 					 */
13977c478bd9Sstevel@tonic-gate 					if (firstmatch == cur)
13987c478bd9Sstevel@tonic-gate 						firstmatch = NULL;
13997c478bd9Sstevel@tonic-gate 				}
14007c478bd9Sstevel@tonic-gate 			}
14017c478bd9Sstevel@tonic-gate 		}
14027c478bd9Sstevel@tonic-gate 	}
14037c478bd9Sstevel@tonic-gate 
14047c478bd9Sstevel@tonic-gate 	if (firstmatch == NULL)
14057c478bd9Sstevel@tonic-gate 		return (Z_NO_RESOURCE_ID);
14067c478bd9Sstevel@tonic-gate 
14077c478bd9Sstevel@tonic-gate 	cur = firstmatch;
14087c478bd9Sstevel@tonic-gate 
14097c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_DIR, tabptr->zone_fs_dir,
14107c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_fs_dir))) != Z_OK)
14117c478bd9Sstevel@tonic-gate 		return (err);
14127c478bd9Sstevel@tonic-gate 
14137c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_SPECIAL, tabptr->zone_fs_special,
14147c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_fs_special))) != Z_OK)
14157c478bd9Sstevel@tonic-gate 		return (err);
14167c478bd9Sstevel@tonic-gate 
14177c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_RAW, tabptr->zone_fs_raw,
14187c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_fs_raw))) != Z_OK)
14197c478bd9Sstevel@tonic-gate 		return (err);
14207c478bd9Sstevel@tonic-gate 
14217c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_TYPE, tabptr->zone_fs_type,
14227c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_fs_type))) != Z_OK)
14237c478bd9Sstevel@tonic-gate 		return (err);
14247c478bd9Sstevel@tonic-gate 
14257c478bd9Sstevel@tonic-gate 	/* options are optional */
14267c478bd9Sstevel@tonic-gate 	tabptr->zone_fs_options = NULL;
14277c478bd9Sstevel@tonic-gate 	for (options = cur->xmlChildrenNode; options != NULL;
14287c478bd9Sstevel@tonic-gate 	    options = options->next) {
14297c478bd9Sstevel@tonic-gate 		if ((fetchprop(options, DTD_ATTR_NAME, options_str,
14307c478bd9Sstevel@tonic-gate 		    sizeof (options_str)) != Z_OK))
14317c478bd9Sstevel@tonic-gate 			break;
14327c478bd9Sstevel@tonic-gate 		if (zonecfg_add_fs_option(tabptr, options_str) != Z_OK)
14337c478bd9Sstevel@tonic-gate 			break;
14347c478bd9Sstevel@tonic-gate 	}
14357c478bd9Sstevel@tonic-gate 	return (Z_OK);
14367c478bd9Sstevel@tonic-gate }
14377c478bd9Sstevel@tonic-gate 
14387c478bd9Sstevel@tonic-gate int
14397c478bd9Sstevel@tonic-gate zonecfg_lookup_ipd(zone_dochandle_t handle, struct zone_fstab *tabptr)
14407c478bd9Sstevel@tonic-gate {
14417c478bd9Sstevel@tonic-gate 	xmlNodePtr cur, match;
14427c478bd9Sstevel@tonic-gate 	int err;
14437c478bd9Sstevel@tonic-gate 	char dirname[MAXPATHLEN];
14447c478bd9Sstevel@tonic-gate 
14457c478bd9Sstevel@tonic-gate 	if (tabptr == NULL)
14467c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
14477c478bd9Sstevel@tonic-gate 
14487c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
14497c478bd9Sstevel@tonic-gate 		return (err);
14507c478bd9Sstevel@tonic-gate 
14517c478bd9Sstevel@tonic-gate 	/*
14527c478bd9Sstevel@tonic-gate 	 * General algorithm:
14537c478bd9Sstevel@tonic-gate 	 * Walk the list of children looking for matches on any properties
14547c478bd9Sstevel@tonic-gate 	 * specified in the fstab parameter.  If more than one resource
14557c478bd9Sstevel@tonic-gate 	 * matches, we return Z_INSUFFICIENT_SPEC; if none match, we return
14567c478bd9Sstevel@tonic-gate 	 * Z_NO_RESOURCE_ID.
14577c478bd9Sstevel@tonic-gate 	 */
14587c478bd9Sstevel@tonic-gate 	cur = handle->zone_dh_cur;
14597c478bd9Sstevel@tonic-gate 	match = NULL;
14607c478bd9Sstevel@tonic-gate 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
14617c478bd9Sstevel@tonic-gate 		if (xmlStrcmp(cur->name, DTD_ELEM_IPD))
14627c478bd9Sstevel@tonic-gate 			continue;
14637c478bd9Sstevel@tonic-gate 		if (strlen(tabptr->zone_fs_dir) > 0) {
14647c478bd9Sstevel@tonic-gate 			if ((fetchprop(cur, DTD_ATTR_DIR, dirname,
14657c478bd9Sstevel@tonic-gate 			    sizeof (dirname)) == Z_OK) &&
14667c478bd9Sstevel@tonic-gate 			    (strcmp(tabptr->zone_fs_dir, dirname) == 0)) {
14677c478bd9Sstevel@tonic-gate 				if (match == NULL)
14687c478bd9Sstevel@tonic-gate 					match = cur;
14697c478bd9Sstevel@tonic-gate 				else
14707c478bd9Sstevel@tonic-gate 					return (Z_INSUFFICIENT_SPEC);
14717c478bd9Sstevel@tonic-gate 			}
14727c478bd9Sstevel@tonic-gate 		}
14737c478bd9Sstevel@tonic-gate 	}
14747c478bd9Sstevel@tonic-gate 
14757c478bd9Sstevel@tonic-gate 	if (match == NULL)
14767c478bd9Sstevel@tonic-gate 		return (Z_NO_RESOURCE_ID);
14777c478bd9Sstevel@tonic-gate 
14787c478bd9Sstevel@tonic-gate 	cur = match;
14797c478bd9Sstevel@tonic-gate 
14807c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_DIR, tabptr->zone_fs_dir,
14817c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_fs_dir))) != Z_OK)
14827c478bd9Sstevel@tonic-gate 		return (err);
14837c478bd9Sstevel@tonic-gate 
14847c478bd9Sstevel@tonic-gate 	return (Z_OK);
14857c478bd9Sstevel@tonic-gate }
14867c478bd9Sstevel@tonic-gate 
14877c478bd9Sstevel@tonic-gate /*
14887c478bd9Sstevel@tonic-gate  * Compare two IP addresses in string form.  Allow for the possibility that
14897c478bd9Sstevel@tonic-gate  * one might have "/<prefix-length>" at the end: allow a match on just the
14907c478bd9Sstevel@tonic-gate  * IP address (or host name) part.
14917c478bd9Sstevel@tonic-gate  */
14927c478bd9Sstevel@tonic-gate 
14937c478bd9Sstevel@tonic-gate boolean_t
14947c478bd9Sstevel@tonic-gate zonecfg_same_net_address(char *a1, char *a2)
14957c478bd9Sstevel@tonic-gate {
14967c478bd9Sstevel@tonic-gate 	char *slashp, *slashp1, *slashp2;
14977c478bd9Sstevel@tonic-gate 	int result;
14987c478bd9Sstevel@tonic-gate 
14997c478bd9Sstevel@tonic-gate 	if (strcmp(a1, a2) == 0)
15007c478bd9Sstevel@tonic-gate 		return (B_TRUE);
15017c478bd9Sstevel@tonic-gate 
15027c478bd9Sstevel@tonic-gate 	/*
15037c478bd9Sstevel@tonic-gate 	 * If neither has a slash or both do, they need to match to be
15047c478bd9Sstevel@tonic-gate 	 * considered the same, but they did not match above, so fail.
15057c478bd9Sstevel@tonic-gate 	 */
15067c478bd9Sstevel@tonic-gate 	slashp1 = strchr(a1, '/');
15077c478bd9Sstevel@tonic-gate 	slashp2 = strchr(a2, '/');
15087c478bd9Sstevel@tonic-gate 	if ((slashp1 == NULL && slashp2 == NULL) ||
15097c478bd9Sstevel@tonic-gate 	    (slashp1 != NULL && slashp2 != NULL))
15107c478bd9Sstevel@tonic-gate 		return (B_FALSE);
15117c478bd9Sstevel@tonic-gate 
15127c478bd9Sstevel@tonic-gate 	/*
15137c478bd9Sstevel@tonic-gate 	 * Only one had a slash: pick that one, zero out the slash, compare
15147c478bd9Sstevel@tonic-gate 	 * the "address only" strings, restore the slash, and return the
15157c478bd9Sstevel@tonic-gate 	 * result of the comparison.
15167c478bd9Sstevel@tonic-gate 	 */
15177c478bd9Sstevel@tonic-gate 	slashp = (slashp1 == NULL) ? slashp2 : slashp1;
15187c478bd9Sstevel@tonic-gate 	*slashp = '\0';
15197c478bd9Sstevel@tonic-gate 	result = strcmp(a1, a2);
15207c478bd9Sstevel@tonic-gate 	*slashp = '/';
15217c478bd9Sstevel@tonic-gate 	return ((result == 0));
15227c478bd9Sstevel@tonic-gate }
15237c478bd9Sstevel@tonic-gate 
15247c478bd9Sstevel@tonic-gate int
15257c478bd9Sstevel@tonic-gate zonecfg_valid_net_address(char *address, struct lifreq *lifr)
15267c478bd9Sstevel@tonic-gate {
15277c478bd9Sstevel@tonic-gate 	struct sockaddr_in *sin4;
15287c478bd9Sstevel@tonic-gate 	struct sockaddr_in6 *sin6;
15297c478bd9Sstevel@tonic-gate 	struct addrinfo hints, *result;
15307c478bd9Sstevel@tonic-gate 	char *slashp = strchr(address, '/');
15317c478bd9Sstevel@tonic-gate 
15327c478bd9Sstevel@tonic-gate 	bzero(lifr, sizeof (struct lifreq));
15337c478bd9Sstevel@tonic-gate 	sin4 = (struct sockaddr_in *)&lifr->lifr_addr;
15347c478bd9Sstevel@tonic-gate 	sin6 = (struct sockaddr_in6 *)&lifr->lifr_addr;
15357c478bd9Sstevel@tonic-gate 	if (slashp != NULL)
15367c478bd9Sstevel@tonic-gate 		*slashp = '\0';
15377c478bd9Sstevel@tonic-gate 	if (inet_pton(AF_INET, address, &sin4->sin_addr) == 1) {
15387c478bd9Sstevel@tonic-gate 		sin4->sin_family = AF_INET;
15397c478bd9Sstevel@tonic-gate 	} else if (inet_pton(AF_INET6, address, &sin6->sin6_addr) == 1) {
15407c478bd9Sstevel@tonic-gate 		if (slashp == NULL)
15417c478bd9Sstevel@tonic-gate 			return (Z_IPV6_ADDR_PREFIX_LEN);
15427c478bd9Sstevel@tonic-gate 		sin6->sin6_family = AF_INET6;
15437c478bd9Sstevel@tonic-gate 	} else {
15447c478bd9Sstevel@tonic-gate 		/* "address" may be a host name */
15457c478bd9Sstevel@tonic-gate 		(void) memset(&hints, 0, sizeof (hints));
15467c478bd9Sstevel@tonic-gate 		hints.ai_family = PF_INET;
15477c478bd9Sstevel@tonic-gate 		if (getaddrinfo(address, NULL, &hints, &result) != 0)
15487c478bd9Sstevel@tonic-gate 			return (Z_BOGUS_ADDRESS);
15497c478bd9Sstevel@tonic-gate 		sin4->sin_family = result->ai_family;
1550a1be23daSdp 
15517c478bd9Sstevel@tonic-gate 		(void) memcpy(&sin4->sin_addr,
15527c478bd9Sstevel@tonic-gate 		    /* LINTED E_BAD_PTR_CAST_ALIGN */
15537c478bd9Sstevel@tonic-gate 		    &((struct sockaddr_in *)result->ai_addr)->sin_addr,
15547c478bd9Sstevel@tonic-gate 		    sizeof (struct in_addr));
1555a1be23daSdp 
15567c478bd9Sstevel@tonic-gate 		freeaddrinfo(result);
15577c478bd9Sstevel@tonic-gate 	}
15587c478bd9Sstevel@tonic-gate 	return (Z_OK);
15597c478bd9Sstevel@tonic-gate }
15607c478bd9Sstevel@tonic-gate 
15617c478bd9Sstevel@tonic-gate int
15627c478bd9Sstevel@tonic-gate zonecfg_lookup_nwif(zone_dochandle_t handle, struct zone_nwiftab *tabptr)
15637c478bd9Sstevel@tonic-gate {
15647c478bd9Sstevel@tonic-gate 	xmlNodePtr cur, firstmatch;
15657c478bd9Sstevel@tonic-gate 	int err;
15667c478bd9Sstevel@tonic-gate 	char address[INET6_ADDRSTRLEN], physical[LIFNAMSIZ];
15677c478bd9Sstevel@tonic-gate 
15687c478bd9Sstevel@tonic-gate 	if (tabptr == NULL)
15697c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
15707c478bd9Sstevel@tonic-gate 
15717c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
15727c478bd9Sstevel@tonic-gate 		return (err);
15737c478bd9Sstevel@tonic-gate 
15747c478bd9Sstevel@tonic-gate 	cur = handle->zone_dh_cur;
15757c478bd9Sstevel@tonic-gate 	firstmatch = NULL;
15767c478bd9Sstevel@tonic-gate 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
15777c478bd9Sstevel@tonic-gate 		if (xmlStrcmp(cur->name, DTD_ELEM_NET))
15787c478bd9Sstevel@tonic-gate 			continue;
15797c478bd9Sstevel@tonic-gate 		if (strlen(tabptr->zone_nwif_physical) > 0) {
15807c478bd9Sstevel@tonic-gate 			if ((fetchprop(cur, DTD_ATTR_PHYSICAL, physical,
15817c478bd9Sstevel@tonic-gate 			    sizeof (physical)) == Z_OK) &&
15827c478bd9Sstevel@tonic-gate 			    (strcmp(tabptr->zone_nwif_physical,
15837c478bd9Sstevel@tonic-gate 			    physical) == 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_nwif_address) > 0) {
15917c478bd9Sstevel@tonic-gate 			if ((fetchprop(cur, DTD_ATTR_ADDRESS, address,
15927c478bd9Sstevel@tonic-gate 			    sizeof (address)) == Z_OK)) {
15937c478bd9Sstevel@tonic-gate 				if (zonecfg_same_net_address(
15947c478bd9Sstevel@tonic-gate 				    tabptr->zone_nwif_address, address)) {
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 	}
16107c478bd9Sstevel@tonic-gate 	if (firstmatch == NULL)
16117c478bd9Sstevel@tonic-gate 		return (Z_NO_RESOURCE_ID);
16127c478bd9Sstevel@tonic-gate 
16137c478bd9Sstevel@tonic-gate 	cur = firstmatch;
16147c478bd9Sstevel@tonic-gate 
16157c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_PHYSICAL, tabptr->zone_nwif_physical,
16167c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_nwif_physical))) != Z_OK)
16177c478bd9Sstevel@tonic-gate 		return (err);
16187c478bd9Sstevel@tonic-gate 
16197c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_ADDRESS, tabptr->zone_nwif_address,
16207c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_nwif_address))) != Z_OK)
16217c478bd9Sstevel@tonic-gate 		return (err);
16227c478bd9Sstevel@tonic-gate 
16237c478bd9Sstevel@tonic-gate 	return (Z_OK);
16247c478bd9Sstevel@tonic-gate }
16257c478bd9Sstevel@tonic-gate 
16267c478bd9Sstevel@tonic-gate static int
16277c478bd9Sstevel@tonic-gate zonecfg_add_nwif_core(zone_dochandle_t handle, struct zone_nwiftab *tabptr)
16287c478bd9Sstevel@tonic-gate {
16297c478bd9Sstevel@tonic-gate 	xmlNodePtr newnode, cur = handle->zone_dh_cur;
16307c478bd9Sstevel@tonic-gate 	int err;
16317c478bd9Sstevel@tonic-gate 
16327c478bd9Sstevel@tonic-gate 	newnode = xmlNewTextChild(cur, NULL, DTD_ELEM_NET, NULL);
1633a1be23daSdp 	if ((err = newprop(newnode, DTD_ATTR_ADDRESS,
16347c478bd9Sstevel@tonic-gate 	    tabptr->zone_nwif_address)) != Z_OK)
16357c478bd9Sstevel@tonic-gate 		return (err);
1636a1be23daSdp 	if ((err = newprop(newnode, DTD_ATTR_PHYSICAL,
16377c478bd9Sstevel@tonic-gate 	    tabptr->zone_nwif_physical)) != Z_OK)
16387c478bd9Sstevel@tonic-gate 		return (err);
16397c478bd9Sstevel@tonic-gate 	return (Z_OK);
16407c478bd9Sstevel@tonic-gate }
16417c478bd9Sstevel@tonic-gate 
16427c478bd9Sstevel@tonic-gate int
16437c478bd9Sstevel@tonic-gate zonecfg_add_nwif(zone_dochandle_t handle, struct zone_nwiftab *tabptr)
16447c478bd9Sstevel@tonic-gate {
16457c478bd9Sstevel@tonic-gate 	int err;
16467c478bd9Sstevel@tonic-gate 
16477c478bd9Sstevel@tonic-gate 	if (tabptr == NULL)
16487c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
16497c478bd9Sstevel@tonic-gate 
16507c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
16517c478bd9Sstevel@tonic-gate 		return (err);
16527c478bd9Sstevel@tonic-gate 
16537c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_add_nwif_core(handle, tabptr)) != Z_OK)
16547c478bd9Sstevel@tonic-gate 		return (err);
16557c478bd9Sstevel@tonic-gate 
16567c478bd9Sstevel@tonic-gate 	return (Z_OK);
16577c478bd9Sstevel@tonic-gate }
16587c478bd9Sstevel@tonic-gate 
16597c478bd9Sstevel@tonic-gate static int
16607c478bd9Sstevel@tonic-gate zonecfg_delete_nwif_core(zone_dochandle_t handle, struct zone_nwiftab *tabptr)
16617c478bd9Sstevel@tonic-gate {
16627c478bd9Sstevel@tonic-gate 	xmlNodePtr cur = handle->zone_dh_cur;
16637c478bd9Sstevel@tonic-gate 	boolean_t addr_match, phys_match;
16647c478bd9Sstevel@tonic-gate 
16657c478bd9Sstevel@tonic-gate 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
16667c478bd9Sstevel@tonic-gate 		if (xmlStrcmp(cur->name, DTD_ELEM_NET))
16677c478bd9Sstevel@tonic-gate 			continue;
16687c478bd9Sstevel@tonic-gate 
16697c478bd9Sstevel@tonic-gate 		addr_match = match_prop(cur, DTD_ATTR_ADDRESS,
16707c478bd9Sstevel@tonic-gate 		    tabptr->zone_nwif_address);
16717c478bd9Sstevel@tonic-gate 		phys_match = match_prop(cur, DTD_ATTR_PHYSICAL,
16727c478bd9Sstevel@tonic-gate 		    tabptr->zone_nwif_physical);
16737c478bd9Sstevel@tonic-gate 
16747c478bd9Sstevel@tonic-gate 		if (addr_match && phys_match) {
16757c478bd9Sstevel@tonic-gate 			xmlUnlinkNode(cur);
16767c478bd9Sstevel@tonic-gate 			xmlFreeNode(cur);
16777c478bd9Sstevel@tonic-gate 			return (Z_OK);
16787c478bd9Sstevel@tonic-gate 		}
16797c478bd9Sstevel@tonic-gate 	}
16807c478bd9Sstevel@tonic-gate 	return (Z_NO_RESOURCE_ID);
16817c478bd9Sstevel@tonic-gate }
16827c478bd9Sstevel@tonic-gate 
16837c478bd9Sstevel@tonic-gate int
16847c478bd9Sstevel@tonic-gate zonecfg_delete_nwif(zone_dochandle_t handle, struct zone_nwiftab *tabptr)
16857c478bd9Sstevel@tonic-gate {
16867c478bd9Sstevel@tonic-gate 	int err;
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 	if ((err = zonecfg_delete_nwif_core(handle, tabptr)) != Z_OK)
16957c478bd9Sstevel@tonic-gate 		return (err);
16967c478bd9Sstevel@tonic-gate 
16977c478bd9Sstevel@tonic-gate 	return (Z_OK);
16987c478bd9Sstevel@tonic-gate }
16997c478bd9Sstevel@tonic-gate 
17007c478bd9Sstevel@tonic-gate int
17017c478bd9Sstevel@tonic-gate zonecfg_modify_nwif(
17027c478bd9Sstevel@tonic-gate 	zone_dochandle_t handle,
17037c478bd9Sstevel@tonic-gate 	struct zone_nwiftab *oldtabptr,
17047c478bd9Sstevel@tonic-gate 	struct zone_nwiftab *newtabptr)
17057c478bd9Sstevel@tonic-gate {
17067c478bd9Sstevel@tonic-gate 	int err;
17077c478bd9Sstevel@tonic-gate 
17087c478bd9Sstevel@tonic-gate 	if (oldtabptr == NULL || newtabptr == NULL)
17097c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
17107c478bd9Sstevel@tonic-gate 
17117c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
17127c478bd9Sstevel@tonic-gate 		return (err);
17137c478bd9Sstevel@tonic-gate 
17147c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_delete_nwif_core(handle, oldtabptr)) != Z_OK)
17157c478bd9Sstevel@tonic-gate 		return (err);
17167c478bd9Sstevel@tonic-gate 
17177c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_add_nwif_core(handle, newtabptr)) != Z_OK)
17187c478bd9Sstevel@tonic-gate 		return (err);
17197c478bd9Sstevel@tonic-gate 
17207c478bd9Sstevel@tonic-gate 	return (Z_OK);
17217c478bd9Sstevel@tonic-gate }
17227c478bd9Sstevel@tonic-gate 
17237c478bd9Sstevel@tonic-gate int
17247c478bd9Sstevel@tonic-gate zonecfg_lookup_dev(zone_dochandle_t handle, struct zone_devtab *tabptr)
17257c478bd9Sstevel@tonic-gate {
17267c478bd9Sstevel@tonic-gate 	xmlNodePtr cur, firstmatch;
17277c478bd9Sstevel@tonic-gate 	int err;
17287c478bd9Sstevel@tonic-gate 	char match[MAXPATHLEN];
17297c478bd9Sstevel@tonic-gate 
17307c478bd9Sstevel@tonic-gate 	if (tabptr == NULL)
17317c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
17327c478bd9Sstevel@tonic-gate 
17337c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
17347c478bd9Sstevel@tonic-gate 		return (err);
17357c478bd9Sstevel@tonic-gate 
17367c478bd9Sstevel@tonic-gate 	cur = handle->zone_dh_cur;
17377c478bd9Sstevel@tonic-gate 	firstmatch = NULL;
17387c478bd9Sstevel@tonic-gate 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
17397c478bd9Sstevel@tonic-gate 		if (xmlStrcmp(cur->name, DTD_ELEM_DEVICE))
17407c478bd9Sstevel@tonic-gate 			continue;
17417c478bd9Sstevel@tonic-gate 		if (strlen(tabptr->zone_dev_match) == 0)
17427c478bd9Sstevel@tonic-gate 			continue;
17437c478bd9Sstevel@tonic-gate 
17447c478bd9Sstevel@tonic-gate 		if ((fetchprop(cur, DTD_ATTR_MATCH, match,
17457c478bd9Sstevel@tonic-gate 		    sizeof (match)) == Z_OK)) {
17467c478bd9Sstevel@tonic-gate 			if (strcmp(tabptr->zone_dev_match,
17477c478bd9Sstevel@tonic-gate 			    match) == 0) {
17487c478bd9Sstevel@tonic-gate 				if (firstmatch == NULL)
17497c478bd9Sstevel@tonic-gate 					firstmatch = cur;
17507c478bd9Sstevel@tonic-gate 				else if (firstmatch != cur)
17517c478bd9Sstevel@tonic-gate 					return (Z_INSUFFICIENT_SPEC);
17527c478bd9Sstevel@tonic-gate 			} else {
17537c478bd9Sstevel@tonic-gate 				/*
17547c478bd9Sstevel@tonic-gate 				 * If another property matched but this
17557c478bd9Sstevel@tonic-gate 				 * one doesn't then reset firstmatch.
17567c478bd9Sstevel@tonic-gate 				 */
17577c478bd9Sstevel@tonic-gate 				if (firstmatch == cur)
17587c478bd9Sstevel@tonic-gate 					firstmatch = NULL;
17597c478bd9Sstevel@tonic-gate 			}
17607c478bd9Sstevel@tonic-gate 		}
17617c478bd9Sstevel@tonic-gate 	}
17627c478bd9Sstevel@tonic-gate 	if (firstmatch == NULL)
17637c478bd9Sstevel@tonic-gate 		return (Z_NO_RESOURCE_ID);
17647c478bd9Sstevel@tonic-gate 
17657c478bd9Sstevel@tonic-gate 	cur = firstmatch;
17667c478bd9Sstevel@tonic-gate 
17677c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_MATCH, tabptr->zone_dev_match,
17687c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_dev_match))) != Z_OK)
17697c478bd9Sstevel@tonic-gate 		return (err);
17707c478bd9Sstevel@tonic-gate 
17717c478bd9Sstevel@tonic-gate 	return (Z_OK);
17727c478bd9Sstevel@tonic-gate }
17737c478bd9Sstevel@tonic-gate 
17747c478bd9Sstevel@tonic-gate static int
17757c478bd9Sstevel@tonic-gate zonecfg_add_dev_core(zone_dochandle_t handle, struct zone_devtab *tabptr)
17767c478bd9Sstevel@tonic-gate {
17777c478bd9Sstevel@tonic-gate 	xmlNodePtr newnode, cur = handle->zone_dh_cur;
17787c478bd9Sstevel@tonic-gate 	int err;
17797c478bd9Sstevel@tonic-gate 
17807c478bd9Sstevel@tonic-gate 	newnode = xmlNewTextChild(cur, NULL, DTD_ELEM_DEVICE, NULL);
17817c478bd9Sstevel@tonic-gate 
1782a1be23daSdp 	if ((err = newprop(newnode, DTD_ATTR_MATCH,
17837c478bd9Sstevel@tonic-gate 	    tabptr->zone_dev_match)) != Z_OK)
17847c478bd9Sstevel@tonic-gate 		return (err);
17857c478bd9Sstevel@tonic-gate 
17867c478bd9Sstevel@tonic-gate 	return (Z_OK);
17877c478bd9Sstevel@tonic-gate }
17887c478bd9Sstevel@tonic-gate 
17897c478bd9Sstevel@tonic-gate int
17907c478bd9Sstevel@tonic-gate zonecfg_add_dev(zone_dochandle_t handle, struct zone_devtab *tabptr)
17917c478bd9Sstevel@tonic-gate {
17927c478bd9Sstevel@tonic-gate 	int err;
17937c478bd9Sstevel@tonic-gate 
17947c478bd9Sstevel@tonic-gate 	if (tabptr == NULL)
17957c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
17967c478bd9Sstevel@tonic-gate 
17977c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
17987c478bd9Sstevel@tonic-gate 		return (err);
17997c478bd9Sstevel@tonic-gate 
18007c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_add_dev_core(handle, tabptr)) != Z_OK)
18017c478bd9Sstevel@tonic-gate 		return (err);
18027c478bd9Sstevel@tonic-gate 
18037c478bd9Sstevel@tonic-gate 	return (Z_OK);
18047c478bd9Sstevel@tonic-gate }
18057c478bd9Sstevel@tonic-gate 
18067c478bd9Sstevel@tonic-gate static int
18077c478bd9Sstevel@tonic-gate zonecfg_delete_dev_core(zone_dochandle_t handle, struct zone_devtab *tabptr)
18087c478bd9Sstevel@tonic-gate {
18097c478bd9Sstevel@tonic-gate 	xmlNodePtr cur = handle->zone_dh_cur;
18107c478bd9Sstevel@tonic-gate 	int match_match;
18117c478bd9Sstevel@tonic-gate 
18127c478bd9Sstevel@tonic-gate 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
18137c478bd9Sstevel@tonic-gate 		if (xmlStrcmp(cur->name, DTD_ELEM_DEVICE))
18147c478bd9Sstevel@tonic-gate 			continue;
18157c478bd9Sstevel@tonic-gate 
18167c478bd9Sstevel@tonic-gate 		match_match = match_prop(cur, DTD_ATTR_MATCH,
18177c478bd9Sstevel@tonic-gate 		    tabptr->zone_dev_match);
18187c478bd9Sstevel@tonic-gate 
18197c478bd9Sstevel@tonic-gate 		if (match_match) {
18207c478bd9Sstevel@tonic-gate 			xmlUnlinkNode(cur);
18217c478bd9Sstevel@tonic-gate 			xmlFreeNode(cur);
18227c478bd9Sstevel@tonic-gate 			return (Z_OK);
18237c478bd9Sstevel@tonic-gate 		}
18247c478bd9Sstevel@tonic-gate 	}
18257c478bd9Sstevel@tonic-gate 	return (Z_NO_RESOURCE_ID);
18267c478bd9Sstevel@tonic-gate }
18277c478bd9Sstevel@tonic-gate 
18287c478bd9Sstevel@tonic-gate int
18297c478bd9Sstevel@tonic-gate zonecfg_delete_dev(zone_dochandle_t handle, struct zone_devtab *tabptr)
18307c478bd9Sstevel@tonic-gate {
18317c478bd9Sstevel@tonic-gate 	int err;
18327c478bd9Sstevel@tonic-gate 
18337c478bd9Sstevel@tonic-gate 	if (tabptr == NULL)
18347c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
18357c478bd9Sstevel@tonic-gate 
18367c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
18377c478bd9Sstevel@tonic-gate 		return (err);
18387c478bd9Sstevel@tonic-gate 
18397c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_delete_dev_core(handle, tabptr)) != Z_OK)
18407c478bd9Sstevel@tonic-gate 		return (err);
18417c478bd9Sstevel@tonic-gate 
18427c478bd9Sstevel@tonic-gate 	return (Z_OK);
18437c478bd9Sstevel@tonic-gate }
18447c478bd9Sstevel@tonic-gate 
18457c478bd9Sstevel@tonic-gate int
18467c478bd9Sstevel@tonic-gate zonecfg_modify_dev(
18477c478bd9Sstevel@tonic-gate 	zone_dochandle_t handle,
18487c478bd9Sstevel@tonic-gate 	struct zone_devtab *oldtabptr,
18497c478bd9Sstevel@tonic-gate 	struct zone_devtab *newtabptr)
18507c478bd9Sstevel@tonic-gate {
18517c478bd9Sstevel@tonic-gate 	int err;
18527c478bd9Sstevel@tonic-gate 
18537c478bd9Sstevel@tonic-gate 	if (oldtabptr == NULL || newtabptr == NULL)
18547c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
18557c478bd9Sstevel@tonic-gate 
18567c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
18577c478bd9Sstevel@tonic-gate 		return (err);
18587c478bd9Sstevel@tonic-gate 
18597c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_delete_dev_core(handle, oldtabptr)) != Z_OK)
18607c478bd9Sstevel@tonic-gate 		return (err);
18617c478bd9Sstevel@tonic-gate 
18627c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_add_dev_core(handle, newtabptr)) != Z_OK)
18637c478bd9Sstevel@tonic-gate 		return (err);
18647c478bd9Sstevel@tonic-gate 
18657c478bd9Sstevel@tonic-gate 	return (Z_OK);
18667c478bd9Sstevel@tonic-gate }
18677c478bd9Sstevel@tonic-gate 
18687c478bd9Sstevel@tonic-gate /*
18697c478bd9Sstevel@tonic-gate  * This is the set of devices which must be present in every zone.  Users
18707c478bd9Sstevel@tonic-gate  * can augment this list with additional device rules in their zone
18717c478bd9Sstevel@tonic-gate  * configuration, but at present cannot remove any of the this set of
18727c478bd9Sstevel@tonic-gate  * standard devices.  All matching is done by /dev pathname (the "/dev"
18737c478bd9Sstevel@tonic-gate  * part is implicit.  Try to keep rules which match a large number of
18747c478bd9Sstevel@tonic-gate  * devices (like the pts rule) first.
18757c478bd9Sstevel@tonic-gate  */
18767c478bd9Sstevel@tonic-gate static const char *standard_devs[] = {
18777c478bd9Sstevel@tonic-gate 	"pts/*",
18787c478bd9Sstevel@tonic-gate 	"ptmx",
18797c478bd9Sstevel@tonic-gate 	"random",
18807c478bd9Sstevel@tonic-gate 	"urandom",
18817c478bd9Sstevel@tonic-gate 	"poll",
18827c478bd9Sstevel@tonic-gate 	"pool",
18837c478bd9Sstevel@tonic-gate 	"kstat",
18847c478bd9Sstevel@tonic-gate 	"zero",
18857c478bd9Sstevel@tonic-gate 	"null",
18867c478bd9Sstevel@tonic-gate 	"crypto",
18877c478bd9Sstevel@tonic-gate 	"cryptoadm",
18887c478bd9Sstevel@tonic-gate 	"ticots",
18897c478bd9Sstevel@tonic-gate 	"ticotsord",
18907c478bd9Sstevel@tonic-gate 	"ticlts",
18917c478bd9Sstevel@tonic-gate 	"lo0",
18927c478bd9Sstevel@tonic-gate 	"lo1",
18937c478bd9Sstevel@tonic-gate 	"lo2",
18947c478bd9Sstevel@tonic-gate 	"lo3",
18957c478bd9Sstevel@tonic-gate 	"sad/user",
18967c478bd9Sstevel@tonic-gate 	"tty",
18977c478bd9Sstevel@tonic-gate 	"logindmux",
18987c478bd9Sstevel@tonic-gate 	"log",
18997c478bd9Sstevel@tonic-gate 	"conslog",
19007c478bd9Sstevel@tonic-gate 	"arp",
19017c478bd9Sstevel@tonic-gate 	"tcp",
19027c478bd9Sstevel@tonic-gate 	"tcp6",
19037c478bd9Sstevel@tonic-gate 	"udp",
19047c478bd9Sstevel@tonic-gate 	"udp6",
19057c478bd9Sstevel@tonic-gate 	"sysevent",
19067c478bd9Sstevel@tonic-gate #ifdef __sparc
19077c478bd9Sstevel@tonic-gate 	"openprom",
19087c478bd9Sstevel@tonic-gate #endif
19097c478bd9Sstevel@tonic-gate 	"cpu/self/cpuid",
19107c478bd9Sstevel@tonic-gate 	"dtrace/helper",
1911*fa9e4066Sahrens 	"zfs",
19127c478bd9Sstevel@tonic-gate 	NULL
19137c478bd9Sstevel@tonic-gate };
19147c478bd9Sstevel@tonic-gate 
19157c478bd9Sstevel@tonic-gate /*
19167c478bd9Sstevel@tonic-gate  * This function finds everything mounted under a zone's rootpath.
19177c478bd9Sstevel@tonic-gate  * This returns the number of mounts under rootpath, or -1 on error.
19187c478bd9Sstevel@tonic-gate  * callback is called once per mount found with the first argument
19197c478bd9Sstevel@tonic-gate  * pointing to the  mount point.
19207c478bd9Sstevel@tonic-gate  *
19217c478bd9Sstevel@tonic-gate  * If the callback function returns non-zero zonecfg_find_mounts
19227c478bd9Sstevel@tonic-gate  * aborts with an error.
19237c478bd9Sstevel@tonic-gate  */
19247c478bd9Sstevel@tonic-gate 
19257c478bd9Sstevel@tonic-gate int
19267c478bd9Sstevel@tonic-gate zonecfg_find_mounts(char *rootpath, int (*callback)(const char *, void *),
19277c478bd9Sstevel@tonic-gate     void *priv) {
19287c478bd9Sstevel@tonic-gate 	FILE *mnttab;
19297c478bd9Sstevel@tonic-gate 	struct mnttab m;
19307c478bd9Sstevel@tonic-gate 	size_t l;
19317c478bd9Sstevel@tonic-gate 	int rv = 0;
19327c478bd9Sstevel@tonic-gate 
19337c478bd9Sstevel@tonic-gate 	assert(rootpath != NULL);
19347c478bd9Sstevel@tonic-gate 
19357c478bd9Sstevel@tonic-gate 	l = strlen(rootpath);
19367c478bd9Sstevel@tonic-gate 
19377c478bd9Sstevel@tonic-gate 	mnttab = fopen("/etc/mnttab", "r");
19387c478bd9Sstevel@tonic-gate 
19397c478bd9Sstevel@tonic-gate 	if (mnttab == NULL)
19407c478bd9Sstevel@tonic-gate 		return (-1);
19417c478bd9Sstevel@tonic-gate 
19427c478bd9Sstevel@tonic-gate 	if (ioctl(fileno(mnttab), MNTIOC_SHOWHIDDEN, NULL) < 0)  {
19437c478bd9Sstevel@tonic-gate 		rv = -1;
19447c478bd9Sstevel@tonic-gate 		goto out;
19457c478bd9Sstevel@tonic-gate 	}
19467c478bd9Sstevel@tonic-gate 
19477c478bd9Sstevel@tonic-gate 	while (!getmntent(mnttab, &m)) {
19487c478bd9Sstevel@tonic-gate 		if ((strncmp(rootpath, m.mnt_mountp, l) == 0) &&
19497c478bd9Sstevel@tonic-gate 		    (m.mnt_mountp[l] == '/')) {
19507c478bd9Sstevel@tonic-gate 			rv++;
19517c478bd9Sstevel@tonic-gate 			if (callback == NULL)
19527c478bd9Sstevel@tonic-gate 				continue;
19537c478bd9Sstevel@tonic-gate 			if (callback(m.mnt_mountp, priv)) {
19547c478bd9Sstevel@tonic-gate 				rv = -1;
19557c478bd9Sstevel@tonic-gate 				goto out;
19567c478bd9Sstevel@tonic-gate 
19577c478bd9Sstevel@tonic-gate 			}
19587c478bd9Sstevel@tonic-gate 		}
19597c478bd9Sstevel@tonic-gate 	}
19607c478bd9Sstevel@tonic-gate 
19617c478bd9Sstevel@tonic-gate out:
19627c478bd9Sstevel@tonic-gate 	(void) fclose(mnttab);
19637c478bd9Sstevel@tonic-gate 	return (rv);
19647c478bd9Sstevel@tonic-gate }
19657c478bd9Sstevel@tonic-gate 
19667c478bd9Sstevel@tonic-gate /*
19677c478bd9Sstevel@tonic-gate  * This routine is used to determine if a given device should appear in the
19687c478bd9Sstevel@tonic-gate  * zone represented by 'handle'.  First it consults the list of "standard"
19697c478bd9Sstevel@tonic-gate  * zone devices.  Then it scans the user-supplied device entries.
19707c478bd9Sstevel@tonic-gate  */
19717c478bd9Sstevel@tonic-gate int
19727c478bd9Sstevel@tonic-gate zonecfg_match_dev(zone_dochandle_t handle, char *devpath,
19737c478bd9Sstevel@tonic-gate     struct zone_devtab *out_match)
19747c478bd9Sstevel@tonic-gate {
19757c478bd9Sstevel@tonic-gate 	int err;
19767c478bd9Sstevel@tonic-gate 	boolean_t found = B_FALSE;
19777c478bd9Sstevel@tonic-gate 	char match[MAXPATHLEN];
19787c478bd9Sstevel@tonic-gate 	const char **stdmatch;
19797c478bd9Sstevel@tonic-gate 	xmlNodePtr cur;
19807c478bd9Sstevel@tonic-gate 
19817c478bd9Sstevel@tonic-gate 	if (handle == NULL || devpath == NULL)
19827c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
19837c478bd9Sstevel@tonic-gate 
19847c478bd9Sstevel@tonic-gate 	/*
19857c478bd9Sstevel@tonic-gate 	 * Check the "standard" devices which we require to be present.
19867c478bd9Sstevel@tonic-gate 	 */
19877c478bd9Sstevel@tonic-gate 	for (stdmatch = &standard_devs[0]; *stdmatch != NULL; stdmatch++) {
19887c478bd9Sstevel@tonic-gate 		/*
19897c478bd9Sstevel@tonic-gate 		 * fnmatch gives us simple but powerful shell-style matching.
19907c478bd9Sstevel@tonic-gate 		 */
19917c478bd9Sstevel@tonic-gate 		if (fnmatch(*stdmatch, devpath, FNM_PATHNAME) == 0) {
19927c478bd9Sstevel@tonic-gate 			if (!out_match)
19937c478bd9Sstevel@tonic-gate 				return (Z_OK);
19947c478bd9Sstevel@tonic-gate 			(void) snprintf(out_match->zone_dev_match,
19957c478bd9Sstevel@tonic-gate 			    sizeof (out_match->zone_dev_match),
19967c478bd9Sstevel@tonic-gate 			    "/dev/%s", *stdmatch);
19977c478bd9Sstevel@tonic-gate 			return (Z_OK);
19987c478bd9Sstevel@tonic-gate 		}
19997c478bd9Sstevel@tonic-gate 	}
20007c478bd9Sstevel@tonic-gate 
20017c478bd9Sstevel@tonic-gate 	/*
20027c478bd9Sstevel@tonic-gate 	 * We got no hits in the set of standard devices.  On to the user
20037c478bd9Sstevel@tonic-gate 	 * supplied ones.
20047c478bd9Sstevel@tonic-gate 	 */
20057c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK) {
20067c478bd9Sstevel@tonic-gate 		handle->zone_dh_cur = NULL;
20077c478bd9Sstevel@tonic-gate 		return (err);
20087c478bd9Sstevel@tonic-gate 	}
20097c478bd9Sstevel@tonic-gate 
20107c478bd9Sstevel@tonic-gate 	cur = handle->zone_dh_cur;
20117c478bd9Sstevel@tonic-gate 	cur = cur->xmlChildrenNode;
20127c478bd9Sstevel@tonic-gate 	if (cur == NULL)
20137c478bd9Sstevel@tonic-gate 		return (Z_NO_ENTRY);
20147c478bd9Sstevel@tonic-gate 	handle->zone_dh_cur = cur;
20157c478bd9Sstevel@tonic-gate 
20167c478bd9Sstevel@tonic-gate 	for (; cur != NULL; cur = cur->next) {
20177c478bd9Sstevel@tonic-gate 		char *m;
20187c478bd9Sstevel@tonic-gate 		if (xmlStrcmp(cur->name, DTD_ELEM_DEVICE) != 0)
20197c478bd9Sstevel@tonic-gate 			continue;
20207c478bd9Sstevel@tonic-gate 		if ((err = fetchprop(cur, DTD_ATTR_MATCH, match,
20217c478bd9Sstevel@tonic-gate 		    sizeof (match))) != Z_OK) {
20227c478bd9Sstevel@tonic-gate 			handle->zone_dh_cur = handle->zone_dh_top;
20237c478bd9Sstevel@tonic-gate 			return (err);
20247c478bd9Sstevel@tonic-gate 		}
20257c478bd9Sstevel@tonic-gate 		m = match;
20267c478bd9Sstevel@tonic-gate 		/*
20277c478bd9Sstevel@tonic-gate 		 * fnmatch gives us simple but powerful shell-style matching;
20287c478bd9Sstevel@tonic-gate 		 * but first, we need to strip out /dev/ from the matching rule.
20297c478bd9Sstevel@tonic-gate 		 */
20307c478bd9Sstevel@tonic-gate 		if (strncmp(m, "/dev/", 5) == 0)
20317c478bd9Sstevel@tonic-gate 			m += 5;
20327c478bd9Sstevel@tonic-gate 
20337c478bd9Sstevel@tonic-gate 		if (fnmatch(m, devpath, FNM_PATHNAME) == 0) {
20347c478bd9Sstevel@tonic-gate 			found = B_TRUE;
20357c478bd9Sstevel@tonic-gate 			break;
20367c478bd9Sstevel@tonic-gate 		}
20377c478bd9Sstevel@tonic-gate 	}
20387c478bd9Sstevel@tonic-gate 
20397c478bd9Sstevel@tonic-gate 	if (!found)
20407c478bd9Sstevel@tonic-gate 		return (Z_NO_ENTRY);
20417c478bd9Sstevel@tonic-gate 
20427c478bd9Sstevel@tonic-gate 	if (!out_match)
20437c478bd9Sstevel@tonic-gate 		return (Z_OK);
20447c478bd9Sstevel@tonic-gate 
20457c478bd9Sstevel@tonic-gate 	(void) strlcpy(out_match->zone_dev_match, match,
20467c478bd9Sstevel@tonic-gate 	    sizeof (out_match->zone_dev_match));
20477c478bd9Sstevel@tonic-gate 	return (Z_OK);
20487c478bd9Sstevel@tonic-gate }
20497c478bd9Sstevel@tonic-gate 
20507c478bd9Sstevel@tonic-gate int
20517c478bd9Sstevel@tonic-gate zonecfg_lookup_attr(zone_dochandle_t handle, struct zone_attrtab *tabptr)
20527c478bd9Sstevel@tonic-gate {
20537c478bd9Sstevel@tonic-gate 	xmlNodePtr cur, firstmatch;
20547c478bd9Sstevel@tonic-gate 	int err;
20557c478bd9Sstevel@tonic-gate 	char name[MAXNAMELEN], type[MAXNAMELEN], value[MAXNAMELEN];
20567c478bd9Sstevel@tonic-gate 
20577c478bd9Sstevel@tonic-gate 	if (tabptr == NULL)
20587c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
20597c478bd9Sstevel@tonic-gate 
20607c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
20617c478bd9Sstevel@tonic-gate 		return (err);
20627c478bd9Sstevel@tonic-gate 
20637c478bd9Sstevel@tonic-gate 	cur = handle->zone_dh_cur;
20647c478bd9Sstevel@tonic-gate 	firstmatch = NULL;
20657c478bd9Sstevel@tonic-gate 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
20667c478bd9Sstevel@tonic-gate 		if (xmlStrcmp(cur->name, DTD_ELEM_ATTR))
20677c478bd9Sstevel@tonic-gate 			continue;
20687c478bd9Sstevel@tonic-gate 		if (strlen(tabptr->zone_attr_name) > 0) {
20697c478bd9Sstevel@tonic-gate 			if ((fetchprop(cur, DTD_ATTR_NAME, name,
20707c478bd9Sstevel@tonic-gate 			    sizeof (name)) == Z_OK) &&
20717c478bd9Sstevel@tonic-gate 			    (strcmp(tabptr->zone_attr_name, name) == 0)) {
20727c478bd9Sstevel@tonic-gate 				if (firstmatch == NULL)
20737c478bd9Sstevel@tonic-gate 					firstmatch = cur;
20747c478bd9Sstevel@tonic-gate 				else
20757c478bd9Sstevel@tonic-gate 					return (Z_INSUFFICIENT_SPEC);
20767c478bd9Sstevel@tonic-gate 			}
20777c478bd9Sstevel@tonic-gate 		}
20787c478bd9Sstevel@tonic-gate 		if (strlen(tabptr->zone_attr_type) > 0) {
20797c478bd9Sstevel@tonic-gate 			if ((fetchprop(cur, DTD_ATTR_TYPE, type,
20807c478bd9Sstevel@tonic-gate 			    sizeof (type)) == Z_OK)) {
20817c478bd9Sstevel@tonic-gate 				if (strcmp(tabptr->zone_attr_type, type) == 0) {
20827c478bd9Sstevel@tonic-gate 					if (firstmatch == NULL)
20837c478bd9Sstevel@tonic-gate 						firstmatch = cur;
20847c478bd9Sstevel@tonic-gate 					else if (firstmatch != cur)
20857c478bd9Sstevel@tonic-gate 						return (Z_INSUFFICIENT_SPEC);
20867c478bd9Sstevel@tonic-gate 				} else {
20877c478bd9Sstevel@tonic-gate 					/*
20887c478bd9Sstevel@tonic-gate 					 * If another property matched but this
20897c478bd9Sstevel@tonic-gate 					 * one doesn't then reset firstmatch.
20907c478bd9Sstevel@tonic-gate 					 */
20917c478bd9Sstevel@tonic-gate 					if (firstmatch == cur)
20927c478bd9Sstevel@tonic-gate 						firstmatch = NULL;
20937c478bd9Sstevel@tonic-gate 				}
20947c478bd9Sstevel@tonic-gate 			}
20957c478bd9Sstevel@tonic-gate 		}
20967c478bd9Sstevel@tonic-gate 		if (strlen(tabptr->zone_attr_value) > 0) {
20977c478bd9Sstevel@tonic-gate 			if ((fetchprop(cur, DTD_ATTR_VALUE, value,
20987c478bd9Sstevel@tonic-gate 			    sizeof (value)) == Z_OK)) {
20997c478bd9Sstevel@tonic-gate 				if (strcmp(tabptr->zone_attr_value, value) ==
21007c478bd9Sstevel@tonic-gate 				    0) {
21017c478bd9Sstevel@tonic-gate 					if (firstmatch == NULL)
21027c478bd9Sstevel@tonic-gate 						firstmatch = cur;
21037c478bd9Sstevel@tonic-gate 					else if (firstmatch != cur)
21047c478bd9Sstevel@tonic-gate 						return (Z_INSUFFICIENT_SPEC);
21057c478bd9Sstevel@tonic-gate 				} else {
21067c478bd9Sstevel@tonic-gate 					/*
21077c478bd9Sstevel@tonic-gate 					 * If another property matched but this
21087c478bd9Sstevel@tonic-gate 					 * one doesn't then reset firstmatch.
21097c478bd9Sstevel@tonic-gate 					 */
21107c478bd9Sstevel@tonic-gate 					if (firstmatch == cur)
21117c478bd9Sstevel@tonic-gate 						firstmatch = NULL;
21127c478bd9Sstevel@tonic-gate 				}
21137c478bd9Sstevel@tonic-gate 			}
21147c478bd9Sstevel@tonic-gate 		}
21157c478bd9Sstevel@tonic-gate 	}
21167c478bd9Sstevel@tonic-gate 	if (firstmatch == NULL)
21177c478bd9Sstevel@tonic-gate 		return (Z_NO_RESOURCE_ID);
21187c478bd9Sstevel@tonic-gate 
21197c478bd9Sstevel@tonic-gate 	cur = firstmatch;
21207c478bd9Sstevel@tonic-gate 
21217c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_NAME, tabptr->zone_attr_name,
21227c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_attr_name))) != Z_OK)
21237c478bd9Sstevel@tonic-gate 		return (err);
21247c478bd9Sstevel@tonic-gate 
21257c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_TYPE, tabptr->zone_attr_type,
21267c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_attr_type))) != Z_OK)
21277c478bd9Sstevel@tonic-gate 		return (err);
21287c478bd9Sstevel@tonic-gate 
21297c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_VALUE, tabptr->zone_attr_value,
21307c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_attr_value))) != Z_OK)
21317c478bd9Sstevel@tonic-gate 		return (err);
21327c478bd9Sstevel@tonic-gate 
21337c478bd9Sstevel@tonic-gate 	return (Z_OK);
21347c478bd9Sstevel@tonic-gate }
21357c478bd9Sstevel@tonic-gate 
21367c478bd9Sstevel@tonic-gate static int
21377c478bd9Sstevel@tonic-gate zonecfg_add_attr_core(zone_dochandle_t handle, struct zone_attrtab *tabptr)
21387c478bd9Sstevel@tonic-gate {
21397c478bd9Sstevel@tonic-gate 	xmlNodePtr newnode, cur = handle->zone_dh_cur;
21407c478bd9Sstevel@tonic-gate 	int err;
21417c478bd9Sstevel@tonic-gate 
21427c478bd9Sstevel@tonic-gate 	newnode = xmlNewTextChild(cur, NULL, DTD_ELEM_ATTR, NULL);
2143a1be23daSdp 	err = newprop(newnode, DTD_ATTR_NAME, tabptr->zone_attr_name);
21447c478bd9Sstevel@tonic-gate 	if (err != Z_OK)
21457c478bd9Sstevel@tonic-gate 		return (err);
2146a1be23daSdp 	err = newprop(newnode, DTD_ATTR_TYPE, tabptr->zone_attr_type);
21477c478bd9Sstevel@tonic-gate 	if (err != Z_OK)
21487c478bd9Sstevel@tonic-gate 		return (err);
2149a1be23daSdp 	err = newprop(newnode, DTD_ATTR_VALUE, tabptr->zone_attr_value);
21507c478bd9Sstevel@tonic-gate 	if (err != Z_OK)
21517c478bd9Sstevel@tonic-gate 		return (err);
21527c478bd9Sstevel@tonic-gate 	return (Z_OK);
21537c478bd9Sstevel@tonic-gate }
21547c478bd9Sstevel@tonic-gate 
21557c478bd9Sstevel@tonic-gate int
21567c478bd9Sstevel@tonic-gate zonecfg_add_attr(zone_dochandle_t handle, struct zone_attrtab *tabptr)
21577c478bd9Sstevel@tonic-gate {
21587c478bd9Sstevel@tonic-gate 	int err;
21597c478bd9Sstevel@tonic-gate 
21607c478bd9Sstevel@tonic-gate 	if (tabptr == NULL)
21617c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
21627c478bd9Sstevel@tonic-gate 
21637c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
21647c478bd9Sstevel@tonic-gate 		return (err);
21657c478bd9Sstevel@tonic-gate 
21667c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_add_attr_core(handle, tabptr)) != Z_OK)
21677c478bd9Sstevel@tonic-gate 		return (err);
21687c478bd9Sstevel@tonic-gate 
21697c478bd9Sstevel@tonic-gate 	return (Z_OK);
21707c478bd9Sstevel@tonic-gate }
21717c478bd9Sstevel@tonic-gate 
21727c478bd9Sstevel@tonic-gate static int
21737c478bd9Sstevel@tonic-gate zonecfg_delete_attr_core(zone_dochandle_t handle, struct zone_attrtab *tabptr)
21747c478bd9Sstevel@tonic-gate {
21757c478bd9Sstevel@tonic-gate 	xmlNodePtr cur = handle->zone_dh_cur;
21767c478bd9Sstevel@tonic-gate 	int name_match, type_match, value_match;
21777c478bd9Sstevel@tonic-gate 
21787c478bd9Sstevel@tonic-gate 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
21797c478bd9Sstevel@tonic-gate 		if (xmlStrcmp(cur->name, DTD_ELEM_ATTR))
21807c478bd9Sstevel@tonic-gate 			continue;
21817c478bd9Sstevel@tonic-gate 
21827c478bd9Sstevel@tonic-gate 		name_match = match_prop(cur, DTD_ATTR_NAME,
21837c478bd9Sstevel@tonic-gate 		    tabptr->zone_attr_name);
21847c478bd9Sstevel@tonic-gate 		type_match = match_prop(cur, DTD_ATTR_TYPE,
21857c478bd9Sstevel@tonic-gate 		    tabptr->zone_attr_type);
21867c478bd9Sstevel@tonic-gate 		value_match = match_prop(cur, DTD_ATTR_VALUE,
21877c478bd9Sstevel@tonic-gate 		    tabptr->zone_attr_value);
21887c478bd9Sstevel@tonic-gate 
21897c478bd9Sstevel@tonic-gate 		if (name_match && type_match && value_match) {
21907c478bd9Sstevel@tonic-gate 			xmlUnlinkNode(cur);
21917c478bd9Sstevel@tonic-gate 			xmlFreeNode(cur);
21927c478bd9Sstevel@tonic-gate 			return (Z_OK);
21937c478bd9Sstevel@tonic-gate 		}
21947c478bd9Sstevel@tonic-gate 	}
21957c478bd9Sstevel@tonic-gate 	return (Z_NO_RESOURCE_ID);
21967c478bd9Sstevel@tonic-gate }
21977c478bd9Sstevel@tonic-gate 
21987c478bd9Sstevel@tonic-gate int
21997c478bd9Sstevel@tonic-gate zonecfg_delete_attr(zone_dochandle_t handle, struct zone_attrtab *tabptr)
22007c478bd9Sstevel@tonic-gate {
22017c478bd9Sstevel@tonic-gate 	int err;
22027c478bd9Sstevel@tonic-gate 
22037c478bd9Sstevel@tonic-gate 	if (tabptr == NULL)
22047c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
22057c478bd9Sstevel@tonic-gate 
22067c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
22077c478bd9Sstevel@tonic-gate 		return (err);
22087c478bd9Sstevel@tonic-gate 
22097c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_delete_attr_core(handle, tabptr)) != Z_OK)
22107c478bd9Sstevel@tonic-gate 		return (err);
22117c478bd9Sstevel@tonic-gate 
22127c478bd9Sstevel@tonic-gate 	return (Z_OK);
22137c478bd9Sstevel@tonic-gate }
22147c478bd9Sstevel@tonic-gate 
22157c478bd9Sstevel@tonic-gate int
22167c478bd9Sstevel@tonic-gate zonecfg_modify_attr(
22177c478bd9Sstevel@tonic-gate 	zone_dochandle_t handle,
22187c478bd9Sstevel@tonic-gate 	struct zone_attrtab *oldtabptr,
22197c478bd9Sstevel@tonic-gate 	struct zone_attrtab *newtabptr)
22207c478bd9Sstevel@tonic-gate {
22217c478bd9Sstevel@tonic-gate 	int err;
22227c478bd9Sstevel@tonic-gate 
22237c478bd9Sstevel@tonic-gate 	if (oldtabptr == NULL || newtabptr == NULL)
22247c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
22257c478bd9Sstevel@tonic-gate 
22267c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
22277c478bd9Sstevel@tonic-gate 		return (err);
22287c478bd9Sstevel@tonic-gate 
22297c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_delete_attr_core(handle, oldtabptr)) != Z_OK)
22307c478bd9Sstevel@tonic-gate 		return (err);
22317c478bd9Sstevel@tonic-gate 
22327c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_add_attr_core(handle, newtabptr)) != Z_OK)
22337c478bd9Sstevel@tonic-gate 		return (err);
22347c478bd9Sstevel@tonic-gate 
22357c478bd9Sstevel@tonic-gate 	return (Z_OK);
22367c478bd9Sstevel@tonic-gate }
22377c478bd9Sstevel@tonic-gate 
22387c478bd9Sstevel@tonic-gate int
22397c478bd9Sstevel@tonic-gate zonecfg_get_attr_boolean(const struct zone_attrtab *attr, boolean_t *value)
22407c478bd9Sstevel@tonic-gate {
22417c478bd9Sstevel@tonic-gate 	if (attr == NULL)
22427c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
22437c478bd9Sstevel@tonic-gate 
22447c478bd9Sstevel@tonic-gate 	if (strcmp(attr->zone_attr_type, DTD_ENTITY_BOOLEAN) != 0)
22457c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
22467c478bd9Sstevel@tonic-gate 
22477c478bd9Sstevel@tonic-gate 	if (strcmp(attr->zone_attr_value, DTD_ENTITY_TRUE) == 0) {
22487c478bd9Sstevel@tonic-gate 		*value = B_TRUE;
22497c478bd9Sstevel@tonic-gate 		return (Z_OK);
22507c478bd9Sstevel@tonic-gate 	}
22517c478bd9Sstevel@tonic-gate 	if (strcmp(attr->zone_attr_value, DTD_ENTITY_FALSE) == 0) {
22527c478bd9Sstevel@tonic-gate 		*value = B_FALSE;
22537c478bd9Sstevel@tonic-gate 		return (Z_OK);
22547c478bd9Sstevel@tonic-gate 	}
22557c478bd9Sstevel@tonic-gate 	return (Z_INVAL);
22567c478bd9Sstevel@tonic-gate }
22577c478bd9Sstevel@tonic-gate 
22587c478bd9Sstevel@tonic-gate int
22597c478bd9Sstevel@tonic-gate zonecfg_get_attr_int(const struct zone_attrtab *attr, int64_t *value)
22607c478bd9Sstevel@tonic-gate {
22617c478bd9Sstevel@tonic-gate 	long long result;
22627c478bd9Sstevel@tonic-gate 	char *endptr;
22637c478bd9Sstevel@tonic-gate 
22647c478bd9Sstevel@tonic-gate 	if (attr == NULL)
22657c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
22667c478bd9Sstevel@tonic-gate 
22677c478bd9Sstevel@tonic-gate 	if (strcmp(attr->zone_attr_type, DTD_ENTITY_INT) != 0)
22687c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
22697c478bd9Sstevel@tonic-gate 
22707c478bd9Sstevel@tonic-gate 	errno = 0;
22717c478bd9Sstevel@tonic-gate 	result = strtoll(attr->zone_attr_value, &endptr, 10);
22727c478bd9Sstevel@tonic-gate 	if (errno != 0 || *endptr != '\0')
22737c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
22747c478bd9Sstevel@tonic-gate 	*value = result;
22757c478bd9Sstevel@tonic-gate 	return (Z_OK);
22767c478bd9Sstevel@tonic-gate }
22777c478bd9Sstevel@tonic-gate 
22787c478bd9Sstevel@tonic-gate int
22797c478bd9Sstevel@tonic-gate zonecfg_get_attr_string(const struct zone_attrtab *attr, char *value,
22807c478bd9Sstevel@tonic-gate     size_t val_sz)
22817c478bd9Sstevel@tonic-gate {
22827c478bd9Sstevel@tonic-gate 	if (attr == NULL)
22837c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
22847c478bd9Sstevel@tonic-gate 
22857c478bd9Sstevel@tonic-gate 	if (strcmp(attr->zone_attr_type, DTD_ENTITY_STRING) != 0)
22867c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
22877c478bd9Sstevel@tonic-gate 
22887c478bd9Sstevel@tonic-gate 	if (strlcpy(value, attr->zone_attr_value, val_sz) >= val_sz)
22897c478bd9Sstevel@tonic-gate 		return (Z_TOO_BIG);
22907c478bd9Sstevel@tonic-gate 	return (Z_OK);
22917c478bd9Sstevel@tonic-gate }
22927c478bd9Sstevel@tonic-gate 
22937c478bd9Sstevel@tonic-gate int
22947c478bd9Sstevel@tonic-gate zonecfg_get_attr_uint(const struct zone_attrtab *attr, uint64_t *value)
22957c478bd9Sstevel@tonic-gate {
22967c478bd9Sstevel@tonic-gate 	unsigned long long result;
22977c478bd9Sstevel@tonic-gate 	long long neg_result;
22987c478bd9Sstevel@tonic-gate 	char *endptr;
22997c478bd9Sstevel@tonic-gate 
23007c478bd9Sstevel@tonic-gate 	if (attr == NULL)
23017c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
23027c478bd9Sstevel@tonic-gate 
23037c478bd9Sstevel@tonic-gate 	if (strcmp(attr->zone_attr_type, DTD_ENTITY_UINT) != 0)
23047c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
23057c478bd9Sstevel@tonic-gate 
23067c478bd9Sstevel@tonic-gate 	errno = 0;
23077c478bd9Sstevel@tonic-gate 	result = strtoull(attr->zone_attr_value, &endptr, 10);
23087c478bd9Sstevel@tonic-gate 	if (errno != 0 || *endptr != '\0')
23097c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
23107c478bd9Sstevel@tonic-gate 	errno = 0;
23117c478bd9Sstevel@tonic-gate 	neg_result = strtoll(attr->zone_attr_value, &endptr, 10);
23127c478bd9Sstevel@tonic-gate 	/*
23137c478bd9Sstevel@tonic-gate 	 * Incredibly, strtoull("<negative number>", ...) will not fail but
23147c478bd9Sstevel@tonic-gate 	 * return whatever (negative) number cast as a u_longlong_t, so we
23157c478bd9Sstevel@tonic-gate 	 * need to look for this here.
23167c478bd9Sstevel@tonic-gate 	 */
23177c478bd9Sstevel@tonic-gate 	if (errno == 0 && neg_result < 0)
23187c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
23197c478bd9Sstevel@tonic-gate 	*value = result;
23207c478bd9Sstevel@tonic-gate 	return (Z_OK);
23217c478bd9Sstevel@tonic-gate }
23227c478bd9Sstevel@tonic-gate 
23237c478bd9Sstevel@tonic-gate int
23247c478bd9Sstevel@tonic-gate zonecfg_lookup_rctl(zone_dochandle_t handle, struct zone_rctltab *tabptr)
23257c478bd9Sstevel@tonic-gate {
23267c478bd9Sstevel@tonic-gate 	xmlNodePtr cur, val;
23277c478bd9Sstevel@tonic-gate 	char savedname[MAXNAMELEN];
23287c478bd9Sstevel@tonic-gate 	struct zone_rctlvaltab *valptr;
23297c478bd9Sstevel@tonic-gate 	int err;
23307c478bd9Sstevel@tonic-gate 
23317c478bd9Sstevel@tonic-gate 	if (tabptr->zone_rctl_name == NULL ||
23327c478bd9Sstevel@tonic-gate 	    strlen(tabptr->zone_rctl_name) == 0)
23337c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
23347c478bd9Sstevel@tonic-gate 
23357c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
23367c478bd9Sstevel@tonic-gate 		return (err);
23377c478bd9Sstevel@tonic-gate 
23387c478bd9Sstevel@tonic-gate 	cur = handle->zone_dh_cur;
23397c478bd9Sstevel@tonic-gate 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
23407c478bd9Sstevel@tonic-gate 		if (xmlStrcmp(cur->name, DTD_ELEM_RCTL))
23417c478bd9Sstevel@tonic-gate 			continue;
23427c478bd9Sstevel@tonic-gate 		if ((fetchprop(cur, DTD_ATTR_NAME, savedname,
23437c478bd9Sstevel@tonic-gate 		    sizeof (savedname)) == Z_OK) &&
23447c478bd9Sstevel@tonic-gate 		    (strcmp(savedname, tabptr->zone_rctl_name) == 0)) {
23457c478bd9Sstevel@tonic-gate 			tabptr->zone_rctl_valptr = NULL;
23467c478bd9Sstevel@tonic-gate 			for (val = cur->xmlChildrenNode; val != NULL;
23477c478bd9Sstevel@tonic-gate 			    val = val->next) {
23487c478bd9Sstevel@tonic-gate 				valptr = (struct zone_rctlvaltab *)malloc(
23497c478bd9Sstevel@tonic-gate 				    sizeof (struct zone_rctlvaltab));
23507c478bd9Sstevel@tonic-gate 				if (valptr == NULL)
23517c478bd9Sstevel@tonic-gate 					return (Z_NOMEM);
23527c478bd9Sstevel@tonic-gate 				if ((fetchprop(val, DTD_ATTR_PRIV,
23537c478bd9Sstevel@tonic-gate 				    valptr->zone_rctlval_priv,
23547c478bd9Sstevel@tonic-gate 				    sizeof (valptr->zone_rctlval_priv)) !=
23557c478bd9Sstevel@tonic-gate 				    Z_OK))
23567c478bd9Sstevel@tonic-gate 					break;
23577c478bd9Sstevel@tonic-gate 				if ((fetchprop(val, DTD_ATTR_LIMIT,
23587c478bd9Sstevel@tonic-gate 				    valptr->zone_rctlval_limit,
23597c478bd9Sstevel@tonic-gate 				    sizeof (valptr->zone_rctlval_limit)) !=
23607c478bd9Sstevel@tonic-gate 				    Z_OK))
23617c478bd9Sstevel@tonic-gate 					break;
23627c478bd9Sstevel@tonic-gate 				if ((fetchprop(val, DTD_ATTR_ACTION,
23637c478bd9Sstevel@tonic-gate 				    valptr->zone_rctlval_action,
23647c478bd9Sstevel@tonic-gate 				    sizeof (valptr->zone_rctlval_action)) !=
23657c478bd9Sstevel@tonic-gate 				    Z_OK))
23667c478bd9Sstevel@tonic-gate 					break;
23677c478bd9Sstevel@tonic-gate 				if (zonecfg_add_rctl_value(tabptr, valptr) !=
23687c478bd9Sstevel@tonic-gate 				    Z_OK)
23697c478bd9Sstevel@tonic-gate 					break;
23707c478bd9Sstevel@tonic-gate 			}
23717c478bd9Sstevel@tonic-gate 			return (Z_OK);
23727c478bd9Sstevel@tonic-gate 		}
23737c478bd9Sstevel@tonic-gate 	}
23747c478bd9Sstevel@tonic-gate 	return (Z_NO_RESOURCE_ID);
23757c478bd9Sstevel@tonic-gate }
23767c478bd9Sstevel@tonic-gate 
23777c478bd9Sstevel@tonic-gate static int
23787c478bd9Sstevel@tonic-gate zonecfg_add_rctl_core(zone_dochandle_t handle, struct zone_rctltab *tabptr)
23797c478bd9Sstevel@tonic-gate {
23807c478bd9Sstevel@tonic-gate 	xmlNodePtr newnode, cur = handle->zone_dh_cur, valnode;
23817c478bd9Sstevel@tonic-gate 	struct zone_rctlvaltab *valptr;
23827c478bd9Sstevel@tonic-gate 	int err;
23837c478bd9Sstevel@tonic-gate 
23847c478bd9Sstevel@tonic-gate 	newnode = xmlNewTextChild(cur, NULL, DTD_ELEM_RCTL, NULL);
2385a1be23daSdp 	err = newprop(newnode, DTD_ATTR_NAME, tabptr->zone_rctl_name);
23867c478bd9Sstevel@tonic-gate 	if (err != Z_OK)
23877c478bd9Sstevel@tonic-gate 		return (err);
23887c478bd9Sstevel@tonic-gate 	for (valptr = tabptr->zone_rctl_valptr; valptr != NULL;
23897c478bd9Sstevel@tonic-gate 	    valptr = valptr->zone_rctlval_next) {
23907c478bd9Sstevel@tonic-gate 		valnode = xmlNewTextChild(newnode, NULL,
23917c478bd9Sstevel@tonic-gate 		    DTD_ELEM_RCTLVALUE, NULL);
2392a1be23daSdp 		err = newprop(valnode, DTD_ATTR_PRIV,
23937c478bd9Sstevel@tonic-gate 		    valptr->zone_rctlval_priv);
23947c478bd9Sstevel@tonic-gate 		if (err != Z_OK)
23957c478bd9Sstevel@tonic-gate 			return (err);
2396a1be23daSdp 		err = newprop(valnode, DTD_ATTR_LIMIT,
23977c478bd9Sstevel@tonic-gate 		    valptr->zone_rctlval_limit);
23987c478bd9Sstevel@tonic-gate 		if (err != Z_OK)
23997c478bd9Sstevel@tonic-gate 			return (err);
2400a1be23daSdp 		err = newprop(valnode, DTD_ATTR_ACTION,
24017c478bd9Sstevel@tonic-gate 		    valptr->zone_rctlval_action);
24027c478bd9Sstevel@tonic-gate 		if (err != Z_OK)
24037c478bd9Sstevel@tonic-gate 			return (err);
24047c478bd9Sstevel@tonic-gate 	}
24057c478bd9Sstevel@tonic-gate 	return (Z_OK);
24067c478bd9Sstevel@tonic-gate }
24077c478bd9Sstevel@tonic-gate 
24087c478bd9Sstevel@tonic-gate int
24097c478bd9Sstevel@tonic-gate zonecfg_add_rctl(zone_dochandle_t handle, struct zone_rctltab *tabptr)
24107c478bd9Sstevel@tonic-gate {
24117c478bd9Sstevel@tonic-gate 	int err;
24127c478bd9Sstevel@tonic-gate 
24137c478bd9Sstevel@tonic-gate 	if (tabptr == NULL || tabptr->zone_rctl_name == NULL)
24147c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
24157c478bd9Sstevel@tonic-gate 
24167c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
24177c478bd9Sstevel@tonic-gate 		return (err);
24187c478bd9Sstevel@tonic-gate 
24197c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_add_rctl_core(handle, tabptr)) != Z_OK)
24207c478bd9Sstevel@tonic-gate 		return (err);
24217c478bd9Sstevel@tonic-gate 
24227c478bd9Sstevel@tonic-gate 	return (Z_OK);
24237c478bd9Sstevel@tonic-gate }
24247c478bd9Sstevel@tonic-gate 
24257c478bd9Sstevel@tonic-gate static int
24267c478bd9Sstevel@tonic-gate zonecfg_delete_rctl_core(zone_dochandle_t handle, struct zone_rctltab *tabptr)
24277c478bd9Sstevel@tonic-gate {
24287c478bd9Sstevel@tonic-gate 	xmlNodePtr cur = handle->zone_dh_cur;
24297c478bd9Sstevel@tonic-gate 	xmlChar *savedname;
24307c478bd9Sstevel@tonic-gate 	int name_result;
24317c478bd9Sstevel@tonic-gate 
24327c478bd9Sstevel@tonic-gate 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
24337c478bd9Sstevel@tonic-gate 		if (xmlStrcmp(cur->name, DTD_ELEM_RCTL))
24347c478bd9Sstevel@tonic-gate 			continue;
24357c478bd9Sstevel@tonic-gate 
24367c478bd9Sstevel@tonic-gate 		savedname = xmlGetProp(cur, DTD_ATTR_NAME);
24377c478bd9Sstevel@tonic-gate 		if (savedname == NULL)	/* shouldn't happen */
24387c478bd9Sstevel@tonic-gate 			continue;
24397c478bd9Sstevel@tonic-gate 		name_result = xmlStrcmp(savedname,
24407c478bd9Sstevel@tonic-gate 		    (const xmlChar *) tabptr->zone_rctl_name);
24417c478bd9Sstevel@tonic-gate 		xmlFree(savedname);
24427c478bd9Sstevel@tonic-gate 
24437c478bd9Sstevel@tonic-gate 		if (name_result == 0) {
24447c478bd9Sstevel@tonic-gate 			xmlUnlinkNode(cur);
24457c478bd9Sstevel@tonic-gate 			xmlFreeNode(cur);
24467c478bd9Sstevel@tonic-gate 			return (Z_OK);
24477c478bd9Sstevel@tonic-gate 		}
24487c478bd9Sstevel@tonic-gate 	}
24497c478bd9Sstevel@tonic-gate 	return (Z_NO_RESOURCE_ID);
24507c478bd9Sstevel@tonic-gate }
24517c478bd9Sstevel@tonic-gate 
24527c478bd9Sstevel@tonic-gate int
24537c478bd9Sstevel@tonic-gate zonecfg_delete_rctl(zone_dochandle_t handle, struct zone_rctltab *tabptr)
24547c478bd9Sstevel@tonic-gate {
24557c478bd9Sstevel@tonic-gate 	int err;
24567c478bd9Sstevel@tonic-gate 
24577c478bd9Sstevel@tonic-gate 	if (tabptr == NULL || tabptr->zone_rctl_name == NULL)
24587c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
24597c478bd9Sstevel@tonic-gate 
24607c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
24617c478bd9Sstevel@tonic-gate 		return (err);
24627c478bd9Sstevel@tonic-gate 
24637c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_delete_rctl_core(handle, tabptr)) != Z_OK)
24647c478bd9Sstevel@tonic-gate 		return (err);
24657c478bd9Sstevel@tonic-gate 
24667c478bd9Sstevel@tonic-gate 	return (Z_OK);
24677c478bd9Sstevel@tonic-gate }
24687c478bd9Sstevel@tonic-gate 
24697c478bd9Sstevel@tonic-gate int
24707c478bd9Sstevel@tonic-gate zonecfg_modify_rctl(
24717c478bd9Sstevel@tonic-gate 	zone_dochandle_t handle,
24727c478bd9Sstevel@tonic-gate 	struct zone_rctltab *oldtabptr,
24737c478bd9Sstevel@tonic-gate 	struct zone_rctltab *newtabptr)
24747c478bd9Sstevel@tonic-gate {
24757c478bd9Sstevel@tonic-gate 	int err;
24767c478bd9Sstevel@tonic-gate 
24777c478bd9Sstevel@tonic-gate 	if (oldtabptr == NULL || oldtabptr->zone_rctl_name == NULL ||
24787c478bd9Sstevel@tonic-gate 	    newtabptr == NULL || newtabptr->zone_rctl_name == NULL)
24797c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
24807c478bd9Sstevel@tonic-gate 
24817c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
24827c478bd9Sstevel@tonic-gate 		return (err);
24837c478bd9Sstevel@tonic-gate 
24847c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_delete_rctl_core(handle, oldtabptr)) != Z_OK)
24857c478bd9Sstevel@tonic-gate 		return (err);
24867c478bd9Sstevel@tonic-gate 
24877c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_add_rctl_core(handle, newtabptr)) != Z_OK)
24887c478bd9Sstevel@tonic-gate 		return (err);
24897c478bd9Sstevel@tonic-gate 
24907c478bd9Sstevel@tonic-gate 	return (Z_OK);
24917c478bd9Sstevel@tonic-gate }
24927c478bd9Sstevel@tonic-gate 
24937c478bd9Sstevel@tonic-gate int
24947c478bd9Sstevel@tonic-gate zonecfg_add_rctl_value(
24957c478bd9Sstevel@tonic-gate 	struct zone_rctltab *tabptr,
24967c478bd9Sstevel@tonic-gate 	struct zone_rctlvaltab *valtabptr)
24977c478bd9Sstevel@tonic-gate {
24987c478bd9Sstevel@tonic-gate 	struct zone_rctlvaltab *last, *old, *new;
24997c478bd9Sstevel@tonic-gate 	rctlblk_t *rctlblk = alloca(rctlblk_size());
25007c478bd9Sstevel@tonic-gate 
25017c478bd9Sstevel@tonic-gate 	last = tabptr->zone_rctl_valptr;
25027c478bd9Sstevel@tonic-gate 	for (old = last; old != NULL; old = old->zone_rctlval_next)
25037c478bd9Sstevel@tonic-gate 		last = old;	/* walk to the end of the list */
25047c478bd9Sstevel@tonic-gate 	new = valtabptr;	/* alloc'd by caller */
25057c478bd9Sstevel@tonic-gate 	new->zone_rctlval_next = NULL;
25067c478bd9Sstevel@tonic-gate 	if (zonecfg_construct_rctlblk(valtabptr, rctlblk) != Z_OK)
25077c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
25087c478bd9Sstevel@tonic-gate 	if (!zonecfg_valid_rctlblk(rctlblk))
25097c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
25107c478bd9Sstevel@tonic-gate 	if (last == NULL)
25117c478bd9Sstevel@tonic-gate 		tabptr->zone_rctl_valptr = new;
25127c478bd9Sstevel@tonic-gate 	else
25137c478bd9Sstevel@tonic-gate 		last->zone_rctlval_next = new;
25147c478bd9Sstevel@tonic-gate 	return (Z_OK);
25157c478bd9Sstevel@tonic-gate }
25167c478bd9Sstevel@tonic-gate 
25177c478bd9Sstevel@tonic-gate int
25187c478bd9Sstevel@tonic-gate zonecfg_remove_rctl_value(
25197c478bd9Sstevel@tonic-gate 	struct zone_rctltab *tabptr,
25207c478bd9Sstevel@tonic-gate 	struct zone_rctlvaltab *valtabptr)
25217c478bd9Sstevel@tonic-gate {
25227c478bd9Sstevel@tonic-gate 	struct zone_rctlvaltab *last, *this, *next;
25237c478bd9Sstevel@tonic-gate 
25247c478bd9Sstevel@tonic-gate 	last = tabptr->zone_rctl_valptr;
25257c478bd9Sstevel@tonic-gate 	for (this = last; this != NULL; this = this->zone_rctlval_next) {
25267c478bd9Sstevel@tonic-gate 		if (strcmp(this->zone_rctlval_priv,
25277c478bd9Sstevel@tonic-gate 		    valtabptr->zone_rctlval_priv) == 0 &&
25287c478bd9Sstevel@tonic-gate 		    strcmp(this->zone_rctlval_limit,
25297c478bd9Sstevel@tonic-gate 		    valtabptr->zone_rctlval_limit) == 0 &&
25307c478bd9Sstevel@tonic-gate 		    strcmp(this->zone_rctlval_action,
25317c478bd9Sstevel@tonic-gate 		    valtabptr->zone_rctlval_action) == 0) {
25327c478bd9Sstevel@tonic-gate 			next = this->zone_rctlval_next;
25337c478bd9Sstevel@tonic-gate 			if (this == tabptr->zone_rctl_valptr)
25347c478bd9Sstevel@tonic-gate 				tabptr->zone_rctl_valptr = next;
25357c478bd9Sstevel@tonic-gate 			else
25367c478bd9Sstevel@tonic-gate 				last->zone_rctlval_next = next;
25377c478bd9Sstevel@tonic-gate 			free(this);
25387c478bd9Sstevel@tonic-gate 			return (Z_OK);
25397c478bd9Sstevel@tonic-gate 		} else
25407c478bd9Sstevel@tonic-gate 			last = this;
25417c478bd9Sstevel@tonic-gate 	}
25427c478bd9Sstevel@tonic-gate 	return (Z_NO_PROPERTY_ID);
25437c478bd9Sstevel@tonic-gate }
25447c478bd9Sstevel@tonic-gate 
25457c478bd9Sstevel@tonic-gate char *
25467c478bd9Sstevel@tonic-gate zonecfg_strerror(int errnum)
25477c478bd9Sstevel@tonic-gate {
25487c478bd9Sstevel@tonic-gate 	switch (errnum) {
25497c478bd9Sstevel@tonic-gate 	case Z_OK:
25507c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "OK"));
25517c478bd9Sstevel@tonic-gate 	case Z_EMPTY_DOCUMENT:
25527c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Empty document"));
25537c478bd9Sstevel@tonic-gate 	case Z_WRONG_DOC_TYPE:
25547c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Wrong document type"));
25557c478bd9Sstevel@tonic-gate 	case Z_BAD_PROPERTY:
25567c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Bad document property"));
25577c478bd9Sstevel@tonic-gate 	case Z_TEMP_FILE:
25587c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN,
25597c478bd9Sstevel@tonic-gate 		    "Problem creating temporary file"));
25607c478bd9Sstevel@tonic-gate 	case Z_SAVING_FILE:
25617c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Problem saving file"));
25627c478bd9Sstevel@tonic-gate 	case Z_NO_ENTRY:
25637c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "No such entry"));
25647c478bd9Sstevel@tonic-gate 	case Z_BOGUS_ZONE_NAME:
25657c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Bogus zone name"));
25667c478bd9Sstevel@tonic-gate 	case Z_REQD_RESOURCE_MISSING:
25677c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Required resource missing"));
25687c478bd9Sstevel@tonic-gate 	case Z_REQD_PROPERTY_MISSING:
25697c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Required property missing"));
25707c478bd9Sstevel@tonic-gate 	case Z_BAD_HANDLE:
25717c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Bad handle"));
25727c478bd9Sstevel@tonic-gate 	case Z_NOMEM:
25737c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Out of memory"));
25747c478bd9Sstevel@tonic-gate 	case Z_INVAL:
25757c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Invalid argument"));
25767c478bd9Sstevel@tonic-gate 	case Z_ACCES:
25777c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Permission denied"));
25787c478bd9Sstevel@tonic-gate 	case Z_TOO_BIG:
25797c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Argument list too long"));
25807c478bd9Sstevel@tonic-gate 	case Z_MISC_FS:
25817c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN,
25827c478bd9Sstevel@tonic-gate 		    "Miscellaneous file system error"));
25837c478bd9Sstevel@tonic-gate 	case Z_NO_ZONE:
25847c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "No such zone configured"));
25857c478bd9Sstevel@tonic-gate 	case Z_NO_RESOURCE_TYPE:
25867c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "No such resource type"));
25877c478bd9Sstevel@tonic-gate 	case Z_NO_RESOURCE_ID:
25887c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "No such resource with that id"));
25897c478bd9Sstevel@tonic-gate 	case Z_NO_PROPERTY_TYPE:
25907c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "No such property type"));
25917c478bd9Sstevel@tonic-gate 	case Z_NO_PROPERTY_ID:
25927c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "No such property with that id"));
2593087719fdSdp 	case Z_BAD_ZONE_STATE:
25947c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN,
2595087719fdSdp 		    "Zone state is invalid for the requested operation"));
25967c478bd9Sstevel@tonic-gate 	case Z_INVALID_DOCUMENT:
25977c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Invalid document"));
2598087719fdSdp 	case Z_NAME_IN_USE:
2599087719fdSdp 		return (dgettext(TEXT_DOMAIN, "Zone name already in use"));
26007c478bd9Sstevel@tonic-gate 	case Z_NO_SUCH_ID:
26017c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "No such zone ID"));
26027c478bd9Sstevel@tonic-gate 	case Z_UPDATING_INDEX:
26037c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Problem updating index file"));
26047c478bd9Sstevel@tonic-gate 	case Z_LOCKING_FILE:
26057c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Locking index file"));
26067c478bd9Sstevel@tonic-gate 	case Z_UNLOCKING_FILE:
26077c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Unlocking index file"));
26087c478bd9Sstevel@tonic-gate 	case Z_INSUFFICIENT_SPEC:
26097c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Insufficient specification"));
26107c478bd9Sstevel@tonic-gate 	case Z_RESOLVED_PATH:
26117c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Resolved path mismatch"));
26127c478bd9Sstevel@tonic-gate 	case Z_IPV6_ADDR_PREFIX_LEN:
26137c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN,
26147c478bd9Sstevel@tonic-gate 		    "IPv6 address missing required prefix length"));
26157c478bd9Sstevel@tonic-gate 	case Z_BOGUS_ADDRESS:
26167c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN,
26177c478bd9Sstevel@tonic-gate 		    "Neither an IPv4 nor an IPv6 address nor a host name"));
26187c478bd9Sstevel@tonic-gate 	default:
26197c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Unknown error"));
26207c478bd9Sstevel@tonic-gate 	}
26217c478bd9Sstevel@tonic-gate }
26227c478bd9Sstevel@tonic-gate 
26237c478bd9Sstevel@tonic-gate /*
26247c478bd9Sstevel@tonic-gate  * Note that the zonecfg_setXent() and zonecfg_endXent() calls are all the
26257c478bd9Sstevel@tonic-gate  * same, as they just turn around and call zonecfg_setent() / zonecfg_endent().
26267c478bd9Sstevel@tonic-gate  */
26277c478bd9Sstevel@tonic-gate 
26287c478bd9Sstevel@tonic-gate static int
26297c478bd9Sstevel@tonic-gate zonecfg_setent(zone_dochandle_t handle)
26307c478bd9Sstevel@tonic-gate {
26317c478bd9Sstevel@tonic-gate 	xmlNodePtr cur;
26327c478bd9Sstevel@tonic-gate 	int err;
26337c478bd9Sstevel@tonic-gate 
26347c478bd9Sstevel@tonic-gate 	if (handle == NULL)
26357c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
26367c478bd9Sstevel@tonic-gate 
26377c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK) {
26387c478bd9Sstevel@tonic-gate 		handle->zone_dh_cur = NULL;
26397c478bd9Sstevel@tonic-gate 		return (err);
26407c478bd9Sstevel@tonic-gate 	}
26417c478bd9Sstevel@tonic-gate 	cur = handle->zone_dh_cur;
26427c478bd9Sstevel@tonic-gate 	cur = cur->xmlChildrenNode;
26437c478bd9Sstevel@tonic-gate 	handle->zone_dh_cur = cur;
26447c478bd9Sstevel@tonic-gate 	return (Z_OK);
26457c478bd9Sstevel@tonic-gate }
26467c478bd9Sstevel@tonic-gate 
26477c478bd9Sstevel@tonic-gate static int
26487c478bd9Sstevel@tonic-gate zonecfg_endent(zone_dochandle_t handle)
26497c478bd9Sstevel@tonic-gate {
26507c478bd9Sstevel@tonic-gate 	if (handle == NULL)
26517c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
26527c478bd9Sstevel@tonic-gate 
26537c478bd9Sstevel@tonic-gate 	handle->zone_dh_cur = handle->zone_dh_top;
26547c478bd9Sstevel@tonic-gate 	return (Z_OK);
26557c478bd9Sstevel@tonic-gate }
26567c478bd9Sstevel@tonic-gate 
26577c478bd9Sstevel@tonic-gate int
26587c478bd9Sstevel@tonic-gate zonecfg_setfsent(zone_dochandle_t handle)
26597c478bd9Sstevel@tonic-gate {
26607c478bd9Sstevel@tonic-gate 	return (zonecfg_setent(handle));
26617c478bd9Sstevel@tonic-gate }
26627c478bd9Sstevel@tonic-gate 
26637c478bd9Sstevel@tonic-gate int
26647c478bd9Sstevel@tonic-gate zonecfg_getfsent(zone_dochandle_t handle, struct zone_fstab *tabptr)
26657c478bd9Sstevel@tonic-gate {
26667c478bd9Sstevel@tonic-gate 	xmlNodePtr cur, options;
26677c478bd9Sstevel@tonic-gate 	char options_str[MAX_MNTOPT_STR];
26687c478bd9Sstevel@tonic-gate 	int err;
26697c478bd9Sstevel@tonic-gate 
26707c478bd9Sstevel@tonic-gate 	if (handle == NULL)
26717c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
26727c478bd9Sstevel@tonic-gate 
26737c478bd9Sstevel@tonic-gate 	if ((cur = handle->zone_dh_cur) == NULL)
26747c478bd9Sstevel@tonic-gate 		return (Z_NO_ENTRY);
26757c478bd9Sstevel@tonic-gate 
26767c478bd9Sstevel@tonic-gate 	for (; cur != NULL; cur = cur->next)
26777c478bd9Sstevel@tonic-gate 		if (!xmlStrcmp(cur->name, DTD_ELEM_FS))
26787c478bd9Sstevel@tonic-gate 			break;
26797c478bd9Sstevel@tonic-gate 	if (cur == NULL) {
26807c478bd9Sstevel@tonic-gate 		handle->zone_dh_cur = handle->zone_dh_top;
26817c478bd9Sstevel@tonic-gate 		return (Z_NO_ENTRY);
26827c478bd9Sstevel@tonic-gate 	}
26837c478bd9Sstevel@tonic-gate 
26847c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_SPECIAL, tabptr->zone_fs_special,
26857c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_fs_special))) != Z_OK) {
26867c478bd9Sstevel@tonic-gate 		handle->zone_dh_cur = handle->zone_dh_top;
26877c478bd9Sstevel@tonic-gate 		return (err);
26887c478bd9Sstevel@tonic-gate 	}
26897c478bd9Sstevel@tonic-gate 
26907c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_RAW, tabptr->zone_fs_raw,
26917c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_fs_raw))) != Z_OK) {
26927c478bd9Sstevel@tonic-gate 		handle->zone_dh_cur = handle->zone_dh_top;
26937c478bd9Sstevel@tonic-gate 		return (err);
26947c478bd9Sstevel@tonic-gate 	}
26957c478bd9Sstevel@tonic-gate 
26967c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_DIR, tabptr->zone_fs_dir,
26977c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_fs_dir))) != Z_OK) {
26987c478bd9Sstevel@tonic-gate 		handle->zone_dh_cur = handle->zone_dh_top;
26997c478bd9Sstevel@tonic-gate 		return (err);
27007c478bd9Sstevel@tonic-gate 	}
27017c478bd9Sstevel@tonic-gate 
27027c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_TYPE, tabptr->zone_fs_type,
27037c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_fs_type))) != Z_OK) {
27047c478bd9Sstevel@tonic-gate 		handle->zone_dh_cur = handle->zone_dh_top;
27057c478bd9Sstevel@tonic-gate 		return (err);
27067c478bd9Sstevel@tonic-gate 	}
27077c478bd9Sstevel@tonic-gate 
27087c478bd9Sstevel@tonic-gate 	/* OK for options to be NULL */
27097c478bd9Sstevel@tonic-gate 	tabptr->zone_fs_options = NULL;
27107c478bd9Sstevel@tonic-gate 	for (options = cur->xmlChildrenNode; options != NULL;
27117c478bd9Sstevel@tonic-gate 	    options = options->next) {
27127c478bd9Sstevel@tonic-gate 		if (fetchprop(options, DTD_ATTR_NAME, options_str,
27137c478bd9Sstevel@tonic-gate 		    sizeof (options_str)) != Z_OK)
27147c478bd9Sstevel@tonic-gate 			break;
27157c478bd9Sstevel@tonic-gate 		if (zonecfg_add_fs_option(tabptr, options_str) != Z_OK)
27167c478bd9Sstevel@tonic-gate 			break;
27177c478bd9Sstevel@tonic-gate 	}
27187c478bd9Sstevel@tonic-gate 
27197c478bd9Sstevel@tonic-gate 	handle->zone_dh_cur = cur->next;
27207c478bd9Sstevel@tonic-gate 	return (Z_OK);
27217c478bd9Sstevel@tonic-gate }
27227c478bd9Sstevel@tonic-gate 
27237c478bd9Sstevel@tonic-gate int
27247c478bd9Sstevel@tonic-gate zonecfg_endfsent(zone_dochandle_t handle)
27257c478bd9Sstevel@tonic-gate {
27267c478bd9Sstevel@tonic-gate 	return (zonecfg_endent(handle));
27277c478bd9Sstevel@tonic-gate }
27287c478bd9Sstevel@tonic-gate 
27297c478bd9Sstevel@tonic-gate int
27307c478bd9Sstevel@tonic-gate zonecfg_setipdent(zone_dochandle_t handle)
27317c478bd9Sstevel@tonic-gate {
27327c478bd9Sstevel@tonic-gate 	return (zonecfg_setent(handle));
27337c478bd9Sstevel@tonic-gate }
27347c478bd9Sstevel@tonic-gate 
27357c478bd9Sstevel@tonic-gate int
27367c478bd9Sstevel@tonic-gate zonecfg_getipdent(zone_dochandle_t handle, struct zone_fstab *tabptr)
27377c478bd9Sstevel@tonic-gate {
27387c478bd9Sstevel@tonic-gate 	xmlNodePtr cur;
27397c478bd9Sstevel@tonic-gate 	int err;
27407c478bd9Sstevel@tonic-gate 
27417c478bd9Sstevel@tonic-gate 	if (handle == NULL)
27427c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
27437c478bd9Sstevel@tonic-gate 
27447c478bd9Sstevel@tonic-gate 	if ((cur = handle->zone_dh_cur) == NULL)
27457c478bd9Sstevel@tonic-gate 		return (Z_NO_ENTRY);
27467c478bd9Sstevel@tonic-gate 
27477c478bd9Sstevel@tonic-gate 	for (; cur != NULL; cur = cur->next)
27487c478bd9Sstevel@tonic-gate 		if (!xmlStrcmp(cur->name, DTD_ELEM_IPD))
27497c478bd9Sstevel@tonic-gate 			break;
27507c478bd9Sstevel@tonic-gate 	if (cur == NULL) {
27517c478bd9Sstevel@tonic-gate 		handle->zone_dh_cur = handle->zone_dh_top;
27527c478bd9Sstevel@tonic-gate 		return (Z_NO_ENTRY);
27537c478bd9Sstevel@tonic-gate 	}
27547c478bd9Sstevel@tonic-gate 
27557c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_DIR, tabptr->zone_fs_dir,
27567c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_fs_dir))) != Z_OK) {
27577c478bd9Sstevel@tonic-gate 		handle->zone_dh_cur = handle->zone_dh_top;
27587c478bd9Sstevel@tonic-gate 		return (err);
27597c478bd9Sstevel@tonic-gate 	}
27607c478bd9Sstevel@tonic-gate 
27617c478bd9Sstevel@tonic-gate 	handle->zone_dh_cur = cur->next;
27627c478bd9Sstevel@tonic-gate 	return (Z_OK);
27637c478bd9Sstevel@tonic-gate }
27647c478bd9Sstevel@tonic-gate 
27657c478bd9Sstevel@tonic-gate int
27667c478bd9Sstevel@tonic-gate zonecfg_endipdent(zone_dochandle_t handle)
27677c478bd9Sstevel@tonic-gate {
27687c478bd9Sstevel@tonic-gate 	return (zonecfg_endent(handle));
27697c478bd9Sstevel@tonic-gate }
27707c478bd9Sstevel@tonic-gate 
27717c478bd9Sstevel@tonic-gate int
27727c478bd9Sstevel@tonic-gate zonecfg_setnwifent(zone_dochandle_t handle)
27737c478bd9Sstevel@tonic-gate {
27747c478bd9Sstevel@tonic-gate 	return (zonecfg_setent(handle));
27757c478bd9Sstevel@tonic-gate }
27767c478bd9Sstevel@tonic-gate 
27777c478bd9Sstevel@tonic-gate int
27787c478bd9Sstevel@tonic-gate zonecfg_getnwifent(zone_dochandle_t handle, struct zone_nwiftab *tabptr)
27797c478bd9Sstevel@tonic-gate {
27807c478bd9Sstevel@tonic-gate 	xmlNodePtr cur;
27817c478bd9Sstevel@tonic-gate 	int err;
27827c478bd9Sstevel@tonic-gate 
27837c478bd9Sstevel@tonic-gate 	if (handle == NULL)
27847c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
27857c478bd9Sstevel@tonic-gate 
27867c478bd9Sstevel@tonic-gate 	if ((cur = handle->zone_dh_cur) == NULL)
27877c478bd9Sstevel@tonic-gate 		return (Z_NO_ENTRY);
27887c478bd9Sstevel@tonic-gate 
27897c478bd9Sstevel@tonic-gate 	for (; cur != NULL; cur = cur->next)
27907c478bd9Sstevel@tonic-gate 		if (!xmlStrcmp(cur->name, DTD_ELEM_NET))
27917c478bd9Sstevel@tonic-gate 			break;
27927c478bd9Sstevel@tonic-gate 	if (cur == NULL) {
27937c478bd9Sstevel@tonic-gate 		handle->zone_dh_cur = handle->zone_dh_top;
27947c478bd9Sstevel@tonic-gate 		return (Z_NO_ENTRY);
27957c478bd9Sstevel@tonic-gate 	}
27967c478bd9Sstevel@tonic-gate 
27977c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_ADDRESS, tabptr->zone_nwif_address,
27987c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_nwif_address))) != Z_OK) {
27997c478bd9Sstevel@tonic-gate 		handle->zone_dh_cur = handle->zone_dh_top;
28007c478bd9Sstevel@tonic-gate 		return (err);
28017c478bd9Sstevel@tonic-gate 	}
28027c478bd9Sstevel@tonic-gate 
28037c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_PHYSICAL, tabptr->zone_nwif_physical,
28047c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_nwif_physical))) != Z_OK) {
28057c478bd9Sstevel@tonic-gate 		handle->zone_dh_cur = handle->zone_dh_top;
28067c478bd9Sstevel@tonic-gate 		return (err);
28077c478bd9Sstevel@tonic-gate 	}
28087c478bd9Sstevel@tonic-gate 
28097c478bd9Sstevel@tonic-gate 	handle->zone_dh_cur = cur->next;
28107c478bd9Sstevel@tonic-gate 	return (Z_OK);
28117c478bd9Sstevel@tonic-gate }
28127c478bd9Sstevel@tonic-gate 
28137c478bd9Sstevel@tonic-gate int
28147c478bd9Sstevel@tonic-gate zonecfg_endnwifent(zone_dochandle_t handle)
28157c478bd9Sstevel@tonic-gate {
28167c478bd9Sstevel@tonic-gate 	return (zonecfg_endent(handle));
28177c478bd9Sstevel@tonic-gate }
28187c478bd9Sstevel@tonic-gate 
28197c478bd9Sstevel@tonic-gate int
28207c478bd9Sstevel@tonic-gate zonecfg_setdevent(zone_dochandle_t handle)
28217c478bd9Sstevel@tonic-gate {
28227c478bd9Sstevel@tonic-gate 	return (zonecfg_setent(handle));
28237c478bd9Sstevel@tonic-gate }
28247c478bd9Sstevel@tonic-gate 
28257c478bd9Sstevel@tonic-gate int
28267c478bd9Sstevel@tonic-gate zonecfg_getdevent(zone_dochandle_t handle, struct zone_devtab *tabptr)
28277c478bd9Sstevel@tonic-gate {
28287c478bd9Sstevel@tonic-gate 	xmlNodePtr cur;
28297c478bd9Sstevel@tonic-gate 	int err;
28307c478bd9Sstevel@tonic-gate 
28317c478bd9Sstevel@tonic-gate 	if (handle == NULL)
28327c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
28337c478bd9Sstevel@tonic-gate 
28347c478bd9Sstevel@tonic-gate 	if ((cur = handle->zone_dh_cur) == NULL)
28357c478bd9Sstevel@tonic-gate 		return (Z_NO_ENTRY);
28367c478bd9Sstevel@tonic-gate 
28377c478bd9Sstevel@tonic-gate 	for (; cur != NULL; cur = cur->next)
28387c478bd9Sstevel@tonic-gate 		if (!xmlStrcmp(cur->name, DTD_ELEM_DEVICE))
28397c478bd9Sstevel@tonic-gate 			break;
28407c478bd9Sstevel@tonic-gate 	if (cur == NULL) {
28417c478bd9Sstevel@tonic-gate 		handle->zone_dh_cur = handle->zone_dh_top;
28427c478bd9Sstevel@tonic-gate 		return (Z_NO_ENTRY);
28437c478bd9Sstevel@tonic-gate 	}
28447c478bd9Sstevel@tonic-gate 
28457c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_MATCH, tabptr->zone_dev_match,
28467c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_dev_match))) != Z_OK) {
28477c478bd9Sstevel@tonic-gate 		handle->zone_dh_cur = handle->zone_dh_top;
28487c478bd9Sstevel@tonic-gate 		return (err);
28497c478bd9Sstevel@tonic-gate 	}
28507c478bd9Sstevel@tonic-gate 
28517c478bd9Sstevel@tonic-gate 	handle->zone_dh_cur = cur->next;
28527c478bd9Sstevel@tonic-gate 	return (Z_OK);
28537c478bd9Sstevel@tonic-gate }
28547c478bd9Sstevel@tonic-gate 
28557c478bd9Sstevel@tonic-gate int
28567c478bd9Sstevel@tonic-gate zonecfg_enddevent(zone_dochandle_t handle)
28577c478bd9Sstevel@tonic-gate {
28587c478bd9Sstevel@tonic-gate 	return (zonecfg_endent(handle));
28597c478bd9Sstevel@tonic-gate }
28607c478bd9Sstevel@tonic-gate 
28617c478bd9Sstevel@tonic-gate int
28627c478bd9Sstevel@tonic-gate zonecfg_setrctlent(zone_dochandle_t handle)
28637c478bd9Sstevel@tonic-gate {
28647c478bd9Sstevel@tonic-gate 	return (zonecfg_setent(handle));
28657c478bd9Sstevel@tonic-gate }
28667c478bd9Sstevel@tonic-gate 
28677c478bd9Sstevel@tonic-gate int
28687c478bd9Sstevel@tonic-gate zonecfg_getrctlent(zone_dochandle_t handle, struct zone_rctltab *tabptr)
28697c478bd9Sstevel@tonic-gate {
28707c478bd9Sstevel@tonic-gate 	xmlNodePtr cur, val;
28717c478bd9Sstevel@tonic-gate 	struct zone_rctlvaltab *valptr;
28727c478bd9Sstevel@tonic-gate 	int err;
28737c478bd9Sstevel@tonic-gate 
28747c478bd9Sstevel@tonic-gate 	if (handle == NULL)
28757c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
28767c478bd9Sstevel@tonic-gate 
28777c478bd9Sstevel@tonic-gate 	if ((cur = handle->zone_dh_cur) == NULL)
28787c478bd9Sstevel@tonic-gate 		return (Z_NO_ENTRY);
28797c478bd9Sstevel@tonic-gate 
28807c478bd9Sstevel@tonic-gate 	for (; cur != NULL; cur = cur->next)
28817c478bd9Sstevel@tonic-gate 		if (!xmlStrcmp(cur->name, DTD_ELEM_RCTL))
28827c478bd9Sstevel@tonic-gate 			break;
28837c478bd9Sstevel@tonic-gate 	if (cur == NULL) {
28847c478bd9Sstevel@tonic-gate 		handle->zone_dh_cur = handle->zone_dh_top;
28857c478bd9Sstevel@tonic-gate 		return (Z_NO_ENTRY);
28867c478bd9Sstevel@tonic-gate 	}
28877c478bd9Sstevel@tonic-gate 
28887c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_NAME, tabptr->zone_rctl_name,
28897c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_rctl_name))) != Z_OK) {
28907c478bd9Sstevel@tonic-gate 		handle->zone_dh_cur = handle->zone_dh_top;
28917c478bd9Sstevel@tonic-gate 		return (err);
28927c478bd9Sstevel@tonic-gate 	}
28937c478bd9Sstevel@tonic-gate 
28947c478bd9Sstevel@tonic-gate 	tabptr->zone_rctl_valptr = NULL;
28957c478bd9Sstevel@tonic-gate 	for (val = cur->xmlChildrenNode; val != NULL; val = val->next) {
28967c478bd9Sstevel@tonic-gate 		valptr = (struct zone_rctlvaltab *)malloc(
28977c478bd9Sstevel@tonic-gate 		    sizeof (struct zone_rctlvaltab));
28987c478bd9Sstevel@tonic-gate 		if (valptr == NULL)
28997c478bd9Sstevel@tonic-gate 			return (Z_NOMEM);
29007c478bd9Sstevel@tonic-gate 		if (fetchprop(val, DTD_ATTR_PRIV, valptr->zone_rctlval_priv,
29017c478bd9Sstevel@tonic-gate 		    sizeof (valptr->zone_rctlval_priv)) != Z_OK)
29027c478bd9Sstevel@tonic-gate 			break;
29037c478bd9Sstevel@tonic-gate 		if (fetchprop(val, DTD_ATTR_LIMIT, valptr->zone_rctlval_limit,
29047c478bd9Sstevel@tonic-gate 		    sizeof (valptr->zone_rctlval_limit)) != Z_OK)
29057c478bd9Sstevel@tonic-gate 			break;
29067c478bd9Sstevel@tonic-gate 		if (fetchprop(val, DTD_ATTR_ACTION, valptr->zone_rctlval_action,
29077c478bd9Sstevel@tonic-gate 		    sizeof (valptr->zone_rctlval_action)) != Z_OK)
29087c478bd9Sstevel@tonic-gate 			break;
29097c478bd9Sstevel@tonic-gate 		if (zonecfg_add_rctl_value(tabptr, valptr) != Z_OK)
29107c478bd9Sstevel@tonic-gate 			break;
29117c478bd9Sstevel@tonic-gate 	}
29127c478bd9Sstevel@tonic-gate 
29137c478bd9Sstevel@tonic-gate 	handle->zone_dh_cur = cur->next;
29147c478bd9Sstevel@tonic-gate 	return (Z_OK);
29157c478bd9Sstevel@tonic-gate }
29167c478bd9Sstevel@tonic-gate 
29177c478bd9Sstevel@tonic-gate int
29187c478bd9Sstevel@tonic-gate zonecfg_endrctlent(zone_dochandle_t handle)
29197c478bd9Sstevel@tonic-gate {
29207c478bd9Sstevel@tonic-gate 	return (zonecfg_endent(handle));
29217c478bd9Sstevel@tonic-gate }
29227c478bd9Sstevel@tonic-gate 
29237c478bd9Sstevel@tonic-gate int
29247c478bd9Sstevel@tonic-gate zonecfg_setattrent(zone_dochandle_t handle)
29257c478bd9Sstevel@tonic-gate {
29267c478bd9Sstevel@tonic-gate 	return (zonecfg_setent(handle));
29277c478bd9Sstevel@tonic-gate }
29287c478bd9Sstevel@tonic-gate 
29297c478bd9Sstevel@tonic-gate int
29307c478bd9Sstevel@tonic-gate zonecfg_getattrent(zone_dochandle_t handle, struct zone_attrtab *tabptr)
29317c478bd9Sstevel@tonic-gate {
29327c478bd9Sstevel@tonic-gate 	xmlNodePtr cur;
29337c478bd9Sstevel@tonic-gate 	int err;
29347c478bd9Sstevel@tonic-gate 
29357c478bd9Sstevel@tonic-gate 	if (handle == NULL)
29367c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
29377c478bd9Sstevel@tonic-gate 
29387c478bd9Sstevel@tonic-gate 	if ((cur = handle->zone_dh_cur) == NULL)
29397c478bd9Sstevel@tonic-gate 		return (Z_NO_ENTRY);
29407c478bd9Sstevel@tonic-gate 
29417c478bd9Sstevel@tonic-gate 	for (; cur != NULL; cur = cur->next)
29427c478bd9Sstevel@tonic-gate 		if (!xmlStrcmp(cur->name, DTD_ELEM_ATTR))
29437c478bd9Sstevel@tonic-gate 			break;
29447c478bd9Sstevel@tonic-gate 	if (cur == NULL) {
29457c478bd9Sstevel@tonic-gate 		handle->zone_dh_cur = handle->zone_dh_top;
29467c478bd9Sstevel@tonic-gate 		return (Z_NO_ENTRY);
29477c478bd9Sstevel@tonic-gate 	}
29487c478bd9Sstevel@tonic-gate 
29497c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_NAME, tabptr->zone_attr_name,
29507c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_attr_name))) != Z_OK) {
29517c478bd9Sstevel@tonic-gate 		handle->zone_dh_cur = handle->zone_dh_top;
29527c478bd9Sstevel@tonic-gate 		return (err);
29537c478bd9Sstevel@tonic-gate 	}
29547c478bd9Sstevel@tonic-gate 
29557c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_TYPE, tabptr->zone_attr_type,
29567c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_attr_type))) != Z_OK) {
29577c478bd9Sstevel@tonic-gate 		handle->zone_dh_cur = handle->zone_dh_top;
29587c478bd9Sstevel@tonic-gate 		return (err);
29597c478bd9Sstevel@tonic-gate 	}
29607c478bd9Sstevel@tonic-gate 
29617c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_VALUE, tabptr->zone_attr_value,
29627c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_attr_value))) != Z_OK) {
29637c478bd9Sstevel@tonic-gate 		handle->zone_dh_cur = handle->zone_dh_top;
29647c478bd9Sstevel@tonic-gate 		return (err);
29657c478bd9Sstevel@tonic-gate 	}
29667c478bd9Sstevel@tonic-gate 
29677c478bd9Sstevel@tonic-gate 	handle->zone_dh_cur = cur->next;
29687c478bd9Sstevel@tonic-gate 	return (Z_OK);
29697c478bd9Sstevel@tonic-gate }
29707c478bd9Sstevel@tonic-gate 
29717c478bd9Sstevel@tonic-gate int
29727c478bd9Sstevel@tonic-gate zonecfg_endattrent(zone_dochandle_t handle)
29737c478bd9Sstevel@tonic-gate {
29747c478bd9Sstevel@tonic-gate 	return (zonecfg_endent(handle));
29757c478bd9Sstevel@tonic-gate }
29767c478bd9Sstevel@tonic-gate 
29777c478bd9Sstevel@tonic-gate /* This will ultimately be configurable. */
29787c478bd9Sstevel@tonic-gate static const char *priv_list[] = {
29797c478bd9Sstevel@tonic-gate 	PRIV_FILE_CHOWN,
29807c478bd9Sstevel@tonic-gate 	PRIV_FILE_CHOWN_SELF,
29817c478bd9Sstevel@tonic-gate 	PRIV_FILE_DAC_EXECUTE,
29827c478bd9Sstevel@tonic-gate 	PRIV_FILE_DAC_READ,
29837c478bd9Sstevel@tonic-gate 	PRIV_FILE_DAC_SEARCH,
29847c478bd9Sstevel@tonic-gate 	PRIV_FILE_DAC_WRITE,
29857c478bd9Sstevel@tonic-gate 	PRIV_FILE_OWNER,
29867c478bd9Sstevel@tonic-gate 	PRIV_FILE_SETID,
29877c478bd9Sstevel@tonic-gate 	PRIV_IPC_DAC_READ,
29887c478bd9Sstevel@tonic-gate 	PRIV_IPC_DAC_WRITE,
29897c478bd9Sstevel@tonic-gate 	PRIV_IPC_OWNER,
29907c478bd9Sstevel@tonic-gate 	PRIV_NET_ICMPACCESS,
29917c478bd9Sstevel@tonic-gate 	PRIV_NET_PRIVADDR,
29927c478bd9Sstevel@tonic-gate 	PRIV_PROC_CHROOT,
29937c478bd9Sstevel@tonic-gate 	PRIV_SYS_AUDIT,
29947c478bd9Sstevel@tonic-gate 	PRIV_PROC_AUDIT,
29957c478bd9Sstevel@tonic-gate 	PRIV_PROC_OWNER,
29967c478bd9Sstevel@tonic-gate 	PRIV_PROC_SETID,
29977c478bd9Sstevel@tonic-gate 	PRIV_PROC_TASKID,
29987c478bd9Sstevel@tonic-gate 	PRIV_SYS_ACCT,
29997c478bd9Sstevel@tonic-gate 	PRIV_SYS_ADMIN,
30007c478bd9Sstevel@tonic-gate 	PRIV_SYS_MOUNT,
30017c478bd9Sstevel@tonic-gate 	PRIV_SYS_NFS,
30027c478bd9Sstevel@tonic-gate 	PRIV_SYS_RESOURCE,
30037c478bd9Sstevel@tonic-gate 	PRIV_CONTRACT_EVENT,
30047c478bd9Sstevel@tonic-gate 	PRIV_CONTRACT_OBSERVER,
30057c478bd9Sstevel@tonic-gate 	NULL
30067c478bd9Sstevel@tonic-gate };
30077c478bd9Sstevel@tonic-gate 
30087c478bd9Sstevel@tonic-gate int
30097c478bd9Sstevel@tonic-gate zonecfg_get_privset(priv_set_t *privs)
30107c478bd9Sstevel@tonic-gate {
30117c478bd9Sstevel@tonic-gate 	const char **strp;
30127c478bd9Sstevel@tonic-gate 	priv_set_t *basic = priv_str_to_set("basic", ",", NULL);
30137c478bd9Sstevel@tonic-gate 
30147c478bd9Sstevel@tonic-gate 	if (basic == NULL)
30157c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
30167c478bd9Sstevel@tonic-gate 
30177c478bd9Sstevel@tonic-gate 	priv_union(basic, privs);
30187c478bd9Sstevel@tonic-gate 	priv_freeset(basic);
30197c478bd9Sstevel@tonic-gate 
30207c478bd9Sstevel@tonic-gate 	for (strp = priv_list; *strp != NULL; strp++) {
30217c478bd9Sstevel@tonic-gate 		if (priv_addset(privs, *strp) != 0) {
30227c478bd9Sstevel@tonic-gate 			return (Z_INVAL);
30237c478bd9Sstevel@tonic-gate 		}
30247c478bd9Sstevel@tonic-gate 	}
30257c478bd9Sstevel@tonic-gate 	return (Z_OK);
30267c478bd9Sstevel@tonic-gate }
30277c478bd9Sstevel@tonic-gate 
30287c478bd9Sstevel@tonic-gate int
30297c478bd9Sstevel@tonic-gate zone_get_zonepath(char *zone_name, char *zonepath, size_t rp_sz)
30307c478bd9Sstevel@tonic-gate {
30317c478bd9Sstevel@tonic-gate 	zone_dochandle_t handle;
30327c478bd9Sstevel@tonic-gate 	boolean_t found = B_FALSE;
30337c478bd9Sstevel@tonic-gate 	struct zoneent *ze;
30347c478bd9Sstevel@tonic-gate 	FILE *cookie;
30357c478bd9Sstevel@tonic-gate 	int err;
3036108322fbScarlsonj 	char *cp;
30377c478bd9Sstevel@tonic-gate 
30387c478bd9Sstevel@tonic-gate 	if (zone_name == NULL)
30397c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
30407c478bd9Sstevel@tonic-gate 
3041108322fbScarlsonj 	(void) strlcpy(zonepath, zonecfg_root, rp_sz);
3042108322fbScarlsonj 	cp = zonepath + strlen(zonepath);
3043108322fbScarlsonj 	while (cp > zonepath && cp[-1] == '/')
3044108322fbScarlsonj 		*--cp = '\0';
3045108322fbScarlsonj 
30467c478bd9Sstevel@tonic-gate 	if (strcmp(zone_name, GLOBAL_ZONENAME) == 0) {
3047108322fbScarlsonj 		if (zonepath[0] == '\0')
30487c478bd9Sstevel@tonic-gate 			(void) strlcpy(zonepath, "/", rp_sz);
30497c478bd9Sstevel@tonic-gate 		return (Z_OK);
30507c478bd9Sstevel@tonic-gate 	}
30517c478bd9Sstevel@tonic-gate 
30527c478bd9Sstevel@tonic-gate 	/*
30537c478bd9Sstevel@tonic-gate 	 * First check the index file.  Because older versions did not have
30547c478bd9Sstevel@tonic-gate 	 * a copy of the zone path, allow for it to be zero length, in which
30557c478bd9Sstevel@tonic-gate 	 * case we ignore this result and fall back to the XML files.
30567c478bd9Sstevel@tonic-gate 	 */
30577c478bd9Sstevel@tonic-gate 	cookie = setzoneent();
30587c478bd9Sstevel@tonic-gate 	while ((ze = getzoneent_private(cookie)) != NULL) {
30597c478bd9Sstevel@tonic-gate 		if (strcmp(ze->zone_name, zone_name) == 0) {
30607c478bd9Sstevel@tonic-gate 			found = B_TRUE;
3061108322fbScarlsonj 			if (ze->zone_path[0] != '\0')
3062108322fbScarlsonj 				(void) strlcpy(cp, ze->zone_path,
3063108322fbScarlsonj 				    rp_sz - (cp - zonepath));
30647c478bd9Sstevel@tonic-gate 		}
30657c478bd9Sstevel@tonic-gate 		free(ze);
30667c478bd9Sstevel@tonic-gate 		if (found)
30677c478bd9Sstevel@tonic-gate 			break;
30687c478bd9Sstevel@tonic-gate 	}
30697c478bd9Sstevel@tonic-gate 	endzoneent(cookie);
3070108322fbScarlsonj 	if (found && *cp != '\0')
30717c478bd9Sstevel@tonic-gate 		return (Z_OK);
30727c478bd9Sstevel@tonic-gate 
30737c478bd9Sstevel@tonic-gate 	/* Fall back to the XML files. */
30747c478bd9Sstevel@tonic-gate 	if ((handle = zonecfg_init_handle()) == NULL)
30757c478bd9Sstevel@tonic-gate 		return (Z_NOMEM);
30767c478bd9Sstevel@tonic-gate 
30777c478bd9Sstevel@tonic-gate 	/*
30787c478bd9Sstevel@tonic-gate 	 * Check the snapshot first: if a zone is running, its zonepath
30797c478bd9Sstevel@tonic-gate 	 * may have changed.
30807c478bd9Sstevel@tonic-gate 	 */
30817c478bd9Sstevel@tonic-gate 	if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) {
30827c478bd9Sstevel@tonic-gate 		if ((err = zonecfg_get_handle(zone_name, handle)) != Z_OK)
30837c478bd9Sstevel@tonic-gate 			return (err);
30847c478bd9Sstevel@tonic-gate 	}
30857c478bd9Sstevel@tonic-gate 	err = zonecfg_get_zonepath(handle, zonepath, rp_sz);
30867c478bd9Sstevel@tonic-gate 	zonecfg_fini_handle(handle);
30877c478bd9Sstevel@tonic-gate 	return (err);
30887c478bd9Sstevel@tonic-gate }
30897c478bd9Sstevel@tonic-gate 
30907c478bd9Sstevel@tonic-gate int
30917c478bd9Sstevel@tonic-gate zone_get_rootpath(char *zone_name, char *rootpath, size_t rp_sz)
30927c478bd9Sstevel@tonic-gate {
30937c478bd9Sstevel@tonic-gate 	int err;
30947c478bd9Sstevel@tonic-gate 
30957c478bd9Sstevel@tonic-gate 	/* This function makes sense for non-global zones only. */
30967c478bd9Sstevel@tonic-gate 	if (strcmp(zone_name, GLOBAL_ZONENAME) == 0)
30977c478bd9Sstevel@tonic-gate 		return (Z_BOGUS_ZONE_NAME);
30987c478bd9Sstevel@tonic-gate 	if ((err = zone_get_zonepath(zone_name, rootpath, rp_sz)) != Z_OK)
30997c478bd9Sstevel@tonic-gate 		return (err);
31007c478bd9Sstevel@tonic-gate 	if (strlcat(rootpath, "/root", rp_sz) >= rp_sz)
31017c478bd9Sstevel@tonic-gate 		return (Z_TOO_BIG);
31027c478bd9Sstevel@tonic-gate 	return (Z_OK);
31037c478bd9Sstevel@tonic-gate }
31047c478bd9Sstevel@tonic-gate 
31057c478bd9Sstevel@tonic-gate static zone_state_t
3106108322fbScarlsonj kernel_state_to_user_state(zoneid_t zoneid, zone_status_t kernel_state)
31077c478bd9Sstevel@tonic-gate {
3108108322fbScarlsonj 	char zoneroot[MAXPATHLEN];
3109108322fbScarlsonj 	size_t zlen;
3110108322fbScarlsonj 
31117c478bd9Sstevel@tonic-gate 	assert(kernel_state <= ZONE_MAX_STATE);
31127c478bd9Sstevel@tonic-gate 	switch (kernel_state) {
31137c478bd9Sstevel@tonic-gate 		case ZONE_IS_UNINITIALIZED:
3114108322fbScarlsonj 			return (ZONE_STATE_READY);
31157c478bd9Sstevel@tonic-gate 		case ZONE_IS_READY:
3116108322fbScarlsonj 			/*
3117108322fbScarlsonj 			 * If the zone's root is mounted on $ZONEPATH/lu, then
3118108322fbScarlsonj 			 * it's a mounted scratch zone.
3119108322fbScarlsonj 			 */
3120108322fbScarlsonj 			if (zone_getattr(zoneid, ZONE_ATTR_ROOT, zoneroot,
3121108322fbScarlsonj 			    sizeof (zoneroot)) >= 0) {
3122108322fbScarlsonj 				zlen = strlen(zoneroot);
3123108322fbScarlsonj 				if (zlen > 3 &&
3124108322fbScarlsonj 				    strcmp(zoneroot + zlen - 3, "/lu") == 0)
3125108322fbScarlsonj 					return (ZONE_STATE_MOUNTED);
3126108322fbScarlsonj 			}
31277c478bd9Sstevel@tonic-gate 			return (ZONE_STATE_READY);
31287c478bd9Sstevel@tonic-gate 		case ZONE_IS_BOOTING:
31297c478bd9Sstevel@tonic-gate 		case ZONE_IS_RUNNING:
31307c478bd9Sstevel@tonic-gate 			return (ZONE_STATE_RUNNING);
31317c478bd9Sstevel@tonic-gate 		case ZONE_IS_SHUTTING_DOWN:
31327c478bd9Sstevel@tonic-gate 		case ZONE_IS_EMPTY:
31337c478bd9Sstevel@tonic-gate 			return (ZONE_STATE_SHUTTING_DOWN);
31347c478bd9Sstevel@tonic-gate 		case ZONE_IS_DOWN:
31357c478bd9Sstevel@tonic-gate 		case ZONE_IS_DYING:
31367c478bd9Sstevel@tonic-gate 		case ZONE_IS_DEAD:
31377c478bd9Sstevel@tonic-gate 		default:
31387c478bd9Sstevel@tonic-gate 			return (ZONE_STATE_DOWN);
31397c478bd9Sstevel@tonic-gate 	}
31407c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
31417c478bd9Sstevel@tonic-gate }
31427c478bd9Sstevel@tonic-gate 
31437c478bd9Sstevel@tonic-gate int
31447c478bd9Sstevel@tonic-gate zone_get_state(char *zone_name, zone_state_t *state_num)
31457c478bd9Sstevel@tonic-gate {
31467c478bd9Sstevel@tonic-gate 	zone_status_t status;
31477c478bd9Sstevel@tonic-gate 	zoneid_t zone_id;
31487c478bd9Sstevel@tonic-gate 	struct zoneent *ze;
31497c478bd9Sstevel@tonic-gate 	boolean_t found = B_FALSE;
31507c478bd9Sstevel@tonic-gate 	FILE *cookie;
3151108322fbScarlsonj 	char kernzone[ZONENAME_MAX];
3152108322fbScarlsonj 	FILE *fp;
31537c478bd9Sstevel@tonic-gate 
31547c478bd9Sstevel@tonic-gate 	if (zone_name == NULL)
31557c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
31567c478bd9Sstevel@tonic-gate 
3157108322fbScarlsonj 	/*
3158108322fbScarlsonj 	 * If we're looking at an alternate root, then we need to query the
3159108322fbScarlsonj 	 * kernel using the scratch zone name.
3160108322fbScarlsonj 	 */
3161108322fbScarlsonj 	zone_id = -1;
3162108322fbScarlsonj 	if (*zonecfg_root != '\0' && !zonecfg_is_scratch(zone_name)) {
3163108322fbScarlsonj 		if ((fp = zonecfg_open_scratch("", B_FALSE)) != NULL) {
3164108322fbScarlsonj 			if (zonecfg_find_scratch(fp, zone_name, zonecfg_root,
3165108322fbScarlsonj 			    kernzone, sizeof (kernzone)) == 0)
3166108322fbScarlsonj 				zone_id = getzoneidbyname(kernzone);
3167108322fbScarlsonj 			zonecfg_close_scratch(fp);
3168108322fbScarlsonj 		}
3169108322fbScarlsonj 	} else {
3170108322fbScarlsonj 		zone_id = getzoneidbyname(zone_name);
3171108322fbScarlsonj 	}
3172108322fbScarlsonj 
31737c478bd9Sstevel@tonic-gate 	/* check to see if zone is running */
3174108322fbScarlsonj 	if (zone_id != -1 &&
31757c478bd9Sstevel@tonic-gate 	    zone_getattr(zone_id, ZONE_ATTR_STATUS, &status,
31767c478bd9Sstevel@tonic-gate 	    sizeof (status)) >= 0) {
3177108322fbScarlsonj 		*state_num = kernel_state_to_user_state(zone_id, status);
31787c478bd9Sstevel@tonic-gate 		return (Z_OK);
31797c478bd9Sstevel@tonic-gate 	}
31807c478bd9Sstevel@tonic-gate 
31817c478bd9Sstevel@tonic-gate 	cookie = setzoneent();
31827c478bd9Sstevel@tonic-gate 	while ((ze = getzoneent_private(cookie)) != NULL) {
31837c478bd9Sstevel@tonic-gate 		if (strcmp(ze->zone_name, zone_name) == 0) {
31847c478bd9Sstevel@tonic-gate 			found = B_TRUE;
31857c478bd9Sstevel@tonic-gate 			*state_num = ze->zone_state;
31867c478bd9Sstevel@tonic-gate 		}
31877c478bd9Sstevel@tonic-gate 		free(ze);
31887c478bd9Sstevel@tonic-gate 		if (found)
31897c478bd9Sstevel@tonic-gate 			break;
31907c478bd9Sstevel@tonic-gate 	}
31917c478bd9Sstevel@tonic-gate 	endzoneent(cookie);
31927c478bd9Sstevel@tonic-gate 	return ((found) ? Z_OK : Z_NO_ZONE);
31937c478bd9Sstevel@tonic-gate }
31947c478bd9Sstevel@tonic-gate 
31957c478bd9Sstevel@tonic-gate int
31967c478bd9Sstevel@tonic-gate zone_set_state(char *zone, zone_state_t state)
31977c478bd9Sstevel@tonic-gate {
31987c478bd9Sstevel@tonic-gate 	struct zoneent ze;
31997c478bd9Sstevel@tonic-gate 
32007c478bd9Sstevel@tonic-gate 	if (state != ZONE_STATE_CONFIGURED && state != ZONE_STATE_INSTALLED &&
32017c478bd9Sstevel@tonic-gate 	    state != ZONE_STATE_INCOMPLETE)
32027c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
32037c478bd9Sstevel@tonic-gate 
3204087719fdSdp 	bzero(&ze, sizeof (ze));
32057c478bd9Sstevel@tonic-gate 	(void) strlcpy(ze.zone_name, zone, sizeof (ze.zone_name));
32067c478bd9Sstevel@tonic-gate 	ze.zone_state = state;
32077c478bd9Sstevel@tonic-gate 	(void) strlcpy(ze.zone_path, "", sizeof (ze.zone_path));
32087c478bd9Sstevel@tonic-gate 	return (putzoneent(&ze, PZE_MODIFY));
32097c478bd9Sstevel@tonic-gate }
32107c478bd9Sstevel@tonic-gate 
32117c478bd9Sstevel@tonic-gate /*
32127c478bd9Sstevel@tonic-gate  * Get id (if any) for specified zone.  There are four possible outcomes:
32137c478bd9Sstevel@tonic-gate  * - If the string corresponds to the numeric id of an active (booted)
32147c478bd9Sstevel@tonic-gate  *   zone, sets *zip to the zone id and returns 0.
32157c478bd9Sstevel@tonic-gate  * - If the string corresponds to the name of an active (booted) zone,
32167c478bd9Sstevel@tonic-gate  *   sets *zip to the zone id and returns 0.
32177c478bd9Sstevel@tonic-gate  * - If the string is a name in the configuration but is not booted,
32187c478bd9Sstevel@tonic-gate  *   sets *zip to ZONE_ID_UNDEFINED and returns 0.
32197c478bd9Sstevel@tonic-gate  * - Otherwise, leaves *zip unchanged and returns -1.
32207c478bd9Sstevel@tonic-gate  *
32217c478bd9Sstevel@tonic-gate  * This function acts as an auxiliary filter on the function of the same
32227c478bd9Sstevel@tonic-gate  * name in libc; the linker binds to this version if libzonecfg exists,
32237c478bd9Sstevel@tonic-gate  * and the libc version if it doesn't.  Any changes to this version of
32247c478bd9Sstevel@tonic-gate  * the function should probably be reflected in the libc version as well.
32257c478bd9Sstevel@tonic-gate  */
32267c478bd9Sstevel@tonic-gate int
32277c478bd9Sstevel@tonic-gate zone_get_id(const char *str, zoneid_t *zip)
32287c478bd9Sstevel@tonic-gate {
32297c478bd9Sstevel@tonic-gate 	zone_dochandle_t hdl;
32307c478bd9Sstevel@tonic-gate 	zoneid_t zoneid;
32317c478bd9Sstevel@tonic-gate 	char *cp;
32327c478bd9Sstevel@tonic-gate 	int err;
32337c478bd9Sstevel@tonic-gate 
32347c478bd9Sstevel@tonic-gate 	/* first try looking for active zone by id */
32357c478bd9Sstevel@tonic-gate 	errno = 0;
32367c478bd9Sstevel@tonic-gate 	zoneid = (zoneid_t)strtol(str, &cp, 0);
32377c478bd9Sstevel@tonic-gate 	if (errno == 0 && cp != str && *cp == '\0' &&
32387c478bd9Sstevel@tonic-gate 	    getzonenamebyid(zoneid, NULL, 0) != -1) {
32397c478bd9Sstevel@tonic-gate 		*zip = zoneid;
32407c478bd9Sstevel@tonic-gate 		return (0);
32417c478bd9Sstevel@tonic-gate 	}
32427c478bd9Sstevel@tonic-gate 
32437c478bd9Sstevel@tonic-gate 	/* then look for active zone by name */
32447c478bd9Sstevel@tonic-gate 	if ((zoneid = getzoneidbyname(str)) != -1) {
32457c478bd9Sstevel@tonic-gate 		*zip = zoneid;
32467c478bd9Sstevel@tonic-gate 		return (0);
32477c478bd9Sstevel@tonic-gate 	}
32487c478bd9Sstevel@tonic-gate 
32497c478bd9Sstevel@tonic-gate 	/* if in global zone, try looking up name in configuration database */
32507c478bd9Sstevel@tonic-gate 	if (getzoneid() != GLOBAL_ZONEID ||
32517c478bd9Sstevel@tonic-gate 	    (hdl = zonecfg_init_handle()) == NULL)
32527c478bd9Sstevel@tonic-gate 		return (-1);
32537c478bd9Sstevel@tonic-gate 
3254108322fbScarlsonj 	if (zonecfg_get_handle(str, hdl) == Z_OK) {
32557c478bd9Sstevel@tonic-gate 		/* zone exists but isn't active */
32567c478bd9Sstevel@tonic-gate 		*zip = ZONE_ID_UNDEFINED;
32577c478bd9Sstevel@tonic-gate 		err = 0;
32587c478bd9Sstevel@tonic-gate 	} else {
32597c478bd9Sstevel@tonic-gate 		err = -1;
32607c478bd9Sstevel@tonic-gate 	}
32617c478bd9Sstevel@tonic-gate 
32627c478bd9Sstevel@tonic-gate 	zonecfg_fini_handle(hdl);
32637c478bd9Sstevel@tonic-gate 	return (err);
32647c478bd9Sstevel@tonic-gate }
32657c478bd9Sstevel@tonic-gate 
32667c478bd9Sstevel@tonic-gate char *
32677c478bd9Sstevel@tonic-gate zone_state_str(zone_state_t state_num)
32687c478bd9Sstevel@tonic-gate {
32697c478bd9Sstevel@tonic-gate 	switch (state_num) {
32707c478bd9Sstevel@tonic-gate 	case ZONE_STATE_CONFIGURED:
32717c478bd9Sstevel@tonic-gate 		return (ZONE_STATE_STR_CONFIGURED);
32727c478bd9Sstevel@tonic-gate 	case ZONE_STATE_INCOMPLETE:
32737c478bd9Sstevel@tonic-gate 		return (ZONE_STATE_STR_INCOMPLETE);
32747c478bd9Sstevel@tonic-gate 	case ZONE_STATE_INSTALLED:
32757c478bd9Sstevel@tonic-gate 		return (ZONE_STATE_STR_INSTALLED);
32767c478bd9Sstevel@tonic-gate 	case ZONE_STATE_READY:
32777c478bd9Sstevel@tonic-gate 		return (ZONE_STATE_STR_READY);
3278108322fbScarlsonj 	case ZONE_STATE_MOUNTED:
3279108322fbScarlsonj 		return (ZONE_STATE_STR_MOUNTED);
32807c478bd9Sstevel@tonic-gate 	case ZONE_STATE_RUNNING:
32817c478bd9Sstevel@tonic-gate 		return (ZONE_STATE_STR_RUNNING);
32827c478bd9Sstevel@tonic-gate 	case ZONE_STATE_SHUTTING_DOWN:
32837c478bd9Sstevel@tonic-gate 		return (ZONE_STATE_STR_SHUTTING_DOWN);
32847c478bd9Sstevel@tonic-gate 	case ZONE_STATE_DOWN:
32857c478bd9Sstevel@tonic-gate 		return (ZONE_STATE_STR_DOWN);
32867c478bd9Sstevel@tonic-gate 	default:
32877c478bd9Sstevel@tonic-gate 		return ("unknown");
32887c478bd9Sstevel@tonic-gate 	}
32897c478bd9Sstevel@tonic-gate }
32907c478bd9Sstevel@tonic-gate 
32917c478bd9Sstevel@tonic-gate /*
3292108322fbScarlsonj  * Given a UUID value, find an associated zone name.  This is intended to be
3293108322fbScarlsonj  * used by callers who set up some 'default' name (corresponding to the
3294108322fbScarlsonj  * expected name for the zone) in the zonename buffer, and thus the function
3295108322fbScarlsonj  * doesn't touch this buffer on failure.
3296108322fbScarlsonj  */
3297108322fbScarlsonj int
3298108322fbScarlsonj zonecfg_get_name_by_uuid(const uuid_t uuid, char *zonename, size_t namelen)
3299108322fbScarlsonj {
3300108322fbScarlsonj 	FILE *fp;
3301108322fbScarlsonj 	struct zoneent *ze;
3302108322fbScarlsonj 
3303108322fbScarlsonj 	/*
3304108322fbScarlsonj 	 * A small amount of subterfuge via casts is necessary here because
3305108322fbScarlsonj 	 * libuuid doesn't use const correctly, but we don't want to export
3306108322fbScarlsonj 	 * this brokenness to our clients.
3307108322fbScarlsonj 	 */
3308108322fbScarlsonj 	if (uuid_is_null(*(uuid_t *)&uuid))
3309108322fbScarlsonj 		return (Z_NO_ZONE);
3310108322fbScarlsonj 	if ((fp = setzoneent()) == NULL)
3311108322fbScarlsonj 		return (Z_NO_ZONE);
3312108322fbScarlsonj 	while ((ze = getzoneent_private(fp)) != NULL) {
3313108322fbScarlsonj 		if (uuid_compare(*(uuid_t *)&uuid, ze->zone_uuid) == 0)
3314108322fbScarlsonj 			break;
3315108322fbScarlsonj 		free(ze);
3316108322fbScarlsonj 	}
3317108322fbScarlsonj 	endzoneent(fp);
3318108322fbScarlsonj 	if (ze != NULL) {
3319108322fbScarlsonj 		(void) strlcpy(zonename, ze->zone_name, namelen);
3320108322fbScarlsonj 		free(ze);
3321108322fbScarlsonj 		return (Z_OK);
3322108322fbScarlsonj 	} else {
3323108322fbScarlsonj 		return (Z_NO_ZONE);
3324108322fbScarlsonj 	}
3325108322fbScarlsonj }
3326108322fbScarlsonj 
3327108322fbScarlsonj /*
3328108322fbScarlsonj  * Given a zone name, get its UUID.  Returns a "NULL" UUID value if the zone
3329108322fbScarlsonj  * exists but the file doesn't have a value set yet.  Returns an error if the
3330108322fbScarlsonj  * zone cannot be located.
3331108322fbScarlsonj  */
3332108322fbScarlsonj int
3333108322fbScarlsonj zonecfg_get_uuid(const char *zonename, uuid_t uuid)
3334108322fbScarlsonj {
3335108322fbScarlsonj 	FILE *fp;
3336108322fbScarlsonj 	struct zoneent *ze;
3337108322fbScarlsonj 
3338108322fbScarlsonj 	if ((fp = setzoneent()) == NULL)
3339108322fbScarlsonj 		return (Z_NO_ZONE);
3340108322fbScarlsonj 	while ((ze = getzoneent_private(fp)) != NULL) {
3341108322fbScarlsonj 		if (strcmp(ze->zone_name, zonename) == 0)
3342108322fbScarlsonj 			break;
3343108322fbScarlsonj 		free(ze);
3344108322fbScarlsonj 	}
3345108322fbScarlsonj 	endzoneent(fp);
3346108322fbScarlsonj 	if (ze != NULL) {
3347108322fbScarlsonj 		uuid_copy(uuid, ze->zone_uuid);
3348108322fbScarlsonj 		free(ze);
3349108322fbScarlsonj 		return (Z_OK);
3350108322fbScarlsonj 	} else {
3351108322fbScarlsonj 		return (Z_NO_ZONE);
3352108322fbScarlsonj 	}
3353108322fbScarlsonj }
3354108322fbScarlsonj 
3355108322fbScarlsonj /*
33567c478bd9Sstevel@tonic-gate  * File-system convenience functions.
33577c478bd9Sstevel@tonic-gate  */
33587c478bd9Sstevel@tonic-gate boolean_t
33597c478bd9Sstevel@tonic-gate zonecfg_valid_fs_type(const char *type)
33607c478bd9Sstevel@tonic-gate {
33617c478bd9Sstevel@tonic-gate 	/*
33627c478bd9Sstevel@tonic-gate 	 * We already know which FS types don't work.
33637c478bd9Sstevel@tonic-gate 	 */
33647c478bd9Sstevel@tonic-gate 	if (strcmp(type, "proc") == 0 ||
33657c478bd9Sstevel@tonic-gate 	    strcmp(type, "mntfs") == 0 ||
33667c478bd9Sstevel@tonic-gate 	    strcmp(type, "autofs") == 0 ||
33677c478bd9Sstevel@tonic-gate 	    strncmp(type, "nfs", sizeof ("nfs") - 1) == 0 ||
33687c478bd9Sstevel@tonic-gate 	    strcmp(type, "cachefs") == 0)
33697c478bd9Sstevel@tonic-gate 		return (B_FALSE);
33707c478bd9Sstevel@tonic-gate 	/*
33717c478bd9Sstevel@tonic-gate 	 * The caller may do more detailed verification to make sure other
33727c478bd9Sstevel@tonic-gate 	 * aspects of this filesystem type make sense.
33737c478bd9Sstevel@tonic-gate 	 */
33747c478bd9Sstevel@tonic-gate 	return (B_TRUE);
33757c478bd9Sstevel@tonic-gate }
33767c478bd9Sstevel@tonic-gate 
33777c478bd9Sstevel@tonic-gate /*
33787c478bd9Sstevel@tonic-gate  * Generally uninteresting rctl convenience functions.
33797c478bd9Sstevel@tonic-gate  */
33807c478bd9Sstevel@tonic-gate 
33817c478bd9Sstevel@tonic-gate int
33827c478bd9Sstevel@tonic-gate zonecfg_construct_rctlblk(const struct zone_rctlvaltab *rctlval,
33837c478bd9Sstevel@tonic-gate     rctlblk_t *rctlblk)
33847c478bd9Sstevel@tonic-gate {
33857c478bd9Sstevel@tonic-gate 	unsigned long long ull;
33867c478bd9Sstevel@tonic-gate 	char *endp;
33877c478bd9Sstevel@tonic-gate 	rctl_priv_t priv;
33887c478bd9Sstevel@tonic-gate 	rctl_qty_t limit;
33897c478bd9Sstevel@tonic-gate 	uint_t action;
33907c478bd9Sstevel@tonic-gate 
33917c478bd9Sstevel@tonic-gate 	/* Get the privilege */
33927c478bd9Sstevel@tonic-gate 	if (strcmp(rctlval->zone_rctlval_priv, "basic") == 0) {
33937c478bd9Sstevel@tonic-gate 		priv = RCPRIV_BASIC;
33947c478bd9Sstevel@tonic-gate 	} else if (strcmp(rctlval->zone_rctlval_priv, "privileged") == 0) {
33957c478bd9Sstevel@tonic-gate 		priv = RCPRIV_PRIVILEGED;
33967c478bd9Sstevel@tonic-gate 	} else {
33977c478bd9Sstevel@tonic-gate 		/* Invalid privilege */
33987c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
33997c478bd9Sstevel@tonic-gate 	}
34007c478bd9Sstevel@tonic-gate 
34017c478bd9Sstevel@tonic-gate 	/* deal with negative input; strtoull(3c) doesn't do what we want */
34027c478bd9Sstevel@tonic-gate 	if (rctlval->zone_rctlval_limit[0] == '-')
34037c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
34047c478bd9Sstevel@tonic-gate 	/* Get the limit */
34057c478bd9Sstevel@tonic-gate 	errno = 0;
34067c478bd9Sstevel@tonic-gate 	ull = strtoull(rctlval->zone_rctlval_limit, &endp, 0);
34077c478bd9Sstevel@tonic-gate 	if (errno != 0 || *endp != '\0') {
34087c478bd9Sstevel@tonic-gate 		/* parse failed */
34097c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
34107c478bd9Sstevel@tonic-gate 	}
34117c478bd9Sstevel@tonic-gate 	limit = (rctl_qty_t)ull;
34127c478bd9Sstevel@tonic-gate 
34137c478bd9Sstevel@tonic-gate 	/* Get the action */
34147c478bd9Sstevel@tonic-gate 	if (strcmp(rctlval->zone_rctlval_action, "none") == 0) {
34157c478bd9Sstevel@tonic-gate 		action = RCTL_LOCAL_NOACTION;
34167c478bd9Sstevel@tonic-gate 	} else if (strcmp(rctlval->zone_rctlval_action, "signal") == 0) {
34177c478bd9Sstevel@tonic-gate 		action = RCTL_LOCAL_SIGNAL;
34187c478bd9Sstevel@tonic-gate 	} else if (strcmp(rctlval->zone_rctlval_action, "deny") == 0) {
34197c478bd9Sstevel@tonic-gate 		action = RCTL_LOCAL_DENY;
34207c478bd9Sstevel@tonic-gate 	} else {
34217c478bd9Sstevel@tonic-gate 		/* Invalid Action */
34227c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
34237c478bd9Sstevel@tonic-gate 	}
34247c478bd9Sstevel@tonic-gate 	rctlblk_set_local_action(rctlblk, action, 0);
34257c478bd9Sstevel@tonic-gate 	rctlblk_set_privilege(rctlblk, priv);
34267c478bd9Sstevel@tonic-gate 	rctlblk_set_value(rctlblk, limit);
34277c478bd9Sstevel@tonic-gate 	return (Z_OK);
34287c478bd9Sstevel@tonic-gate }
34297c478bd9Sstevel@tonic-gate 
34307c478bd9Sstevel@tonic-gate static int
34317c478bd9Sstevel@tonic-gate rctl_check(const char *rctlname, void *arg)
34327c478bd9Sstevel@tonic-gate {
34337c478bd9Sstevel@tonic-gate 	const char *attrname = arg;
34347c478bd9Sstevel@tonic-gate 
34357c478bd9Sstevel@tonic-gate 	/*
34367c478bd9Sstevel@tonic-gate 	 * Returning 1 here is our signal to zonecfg_is_rctl() that it is
34377c478bd9Sstevel@tonic-gate 	 * indeed an rctl name recognized by the system.
34387c478bd9Sstevel@tonic-gate 	 */
34397c478bd9Sstevel@tonic-gate 	return (strcmp(rctlname, attrname) == 0 ? 1 : 0);
34407c478bd9Sstevel@tonic-gate }
34417c478bd9Sstevel@tonic-gate 
34427c478bd9Sstevel@tonic-gate boolean_t
34437c478bd9Sstevel@tonic-gate zonecfg_is_rctl(const char *name)
34447c478bd9Sstevel@tonic-gate {
34457c478bd9Sstevel@tonic-gate 	return (rctl_walk(rctl_check, (void *)name) == 1);
34467c478bd9Sstevel@tonic-gate }
34477c478bd9Sstevel@tonic-gate 
34487c478bd9Sstevel@tonic-gate boolean_t
34497c478bd9Sstevel@tonic-gate zonecfg_valid_rctlname(const char *name)
34507c478bd9Sstevel@tonic-gate {
34517c478bd9Sstevel@tonic-gate 	const char *c;
34527c478bd9Sstevel@tonic-gate 
34537c478bd9Sstevel@tonic-gate 	if (strncmp(name, "zone.", sizeof ("zone.") - 1) != 0)
34547c478bd9Sstevel@tonic-gate 		return (B_FALSE);
34557c478bd9Sstevel@tonic-gate 	if (strlen(name) == sizeof ("zone.") - 1)
34567c478bd9Sstevel@tonic-gate 		return (B_FALSE);
34577c478bd9Sstevel@tonic-gate 	for (c = name + sizeof ("zone.") - 1; *c != '\0'; c++) {
34587c478bd9Sstevel@tonic-gate 		if (!isalpha(*c) && *c != '-')
34597c478bd9Sstevel@tonic-gate 			return (B_FALSE);
34607c478bd9Sstevel@tonic-gate 	}
34617c478bd9Sstevel@tonic-gate 	return (B_TRUE);
34627c478bd9Sstevel@tonic-gate }
34637c478bd9Sstevel@tonic-gate 
34647c478bd9Sstevel@tonic-gate boolean_t
34657c478bd9Sstevel@tonic-gate zonecfg_valid_rctlblk(const rctlblk_t *rctlblk)
34667c478bd9Sstevel@tonic-gate {
34677c478bd9Sstevel@tonic-gate 	rctl_priv_t priv = rctlblk_get_privilege((rctlblk_t *)rctlblk);
34687c478bd9Sstevel@tonic-gate 	uint_t action = rctlblk_get_local_action((rctlblk_t *)rctlblk, NULL);
34697c478bd9Sstevel@tonic-gate 
34707c478bd9Sstevel@tonic-gate 	if (priv != RCPRIV_PRIVILEGED)
34717c478bd9Sstevel@tonic-gate 		return (B_FALSE);
34727c478bd9Sstevel@tonic-gate 	if (action != RCTL_LOCAL_NOACTION && action != RCTL_LOCAL_DENY)
34737c478bd9Sstevel@tonic-gate 		return (B_FALSE);
34747c478bd9Sstevel@tonic-gate 	return (B_TRUE);
34757c478bd9Sstevel@tonic-gate }
34767c478bd9Sstevel@tonic-gate 
34777c478bd9Sstevel@tonic-gate boolean_t
34787c478bd9Sstevel@tonic-gate zonecfg_valid_rctl(const char *name, const rctlblk_t *rctlblk)
34797c478bd9Sstevel@tonic-gate {
34807c478bd9Sstevel@tonic-gate 	rctlblk_t *current, *next;
34817c478bd9Sstevel@tonic-gate 	rctl_qty_t limit = rctlblk_get_value((rctlblk_t *)rctlblk);
34827c478bd9Sstevel@tonic-gate 	uint_t action = rctlblk_get_local_action((rctlblk_t *)rctlblk, NULL);
34837c478bd9Sstevel@tonic-gate 	uint_t global_flags;
34847c478bd9Sstevel@tonic-gate 
34857c478bd9Sstevel@tonic-gate 	if (!zonecfg_valid_rctlblk(rctlblk))
34867c478bd9Sstevel@tonic-gate 		return (B_FALSE);
34877c478bd9Sstevel@tonic-gate 	if (!zonecfg_valid_rctlname(name))
34887c478bd9Sstevel@tonic-gate 		return (B_FALSE);
34897c478bd9Sstevel@tonic-gate 
34907c478bd9Sstevel@tonic-gate 	current = alloca(rctlblk_size());
34917c478bd9Sstevel@tonic-gate 	if (getrctl(name, NULL, current, RCTL_FIRST) != 0)
34927c478bd9Sstevel@tonic-gate 		return (B_TRUE);	/* not an rctl on this system */
34937c478bd9Sstevel@tonic-gate 	/*
34947c478bd9Sstevel@tonic-gate 	 * Make sure the proposed value isn't greater than the current system
34957c478bd9Sstevel@tonic-gate 	 * value.
34967c478bd9Sstevel@tonic-gate 	 */
34977c478bd9Sstevel@tonic-gate 	next = alloca(rctlblk_size());
34987c478bd9Sstevel@tonic-gate 	while (rctlblk_get_privilege(current) != RCPRIV_SYSTEM) {
34997c478bd9Sstevel@tonic-gate 		rctlblk_t *tmp;
35007c478bd9Sstevel@tonic-gate 
35017c478bd9Sstevel@tonic-gate 		if (getrctl(name, current, next, RCTL_NEXT) != 0)
35027c478bd9Sstevel@tonic-gate 			return (B_FALSE);	/* shouldn't happen */
35037c478bd9Sstevel@tonic-gate 		tmp = current;
35047c478bd9Sstevel@tonic-gate 		current = next;
35057c478bd9Sstevel@tonic-gate 		next = tmp;
35067c478bd9Sstevel@tonic-gate 	}
35077c478bd9Sstevel@tonic-gate 	if (limit > rctlblk_get_value(current))
35087c478bd9Sstevel@tonic-gate 		return (B_FALSE);
35097c478bd9Sstevel@tonic-gate 
35107c478bd9Sstevel@tonic-gate 	/*
35117c478bd9Sstevel@tonic-gate 	 * Make sure the proposed action is allowed.
35127c478bd9Sstevel@tonic-gate 	 */
35137c478bd9Sstevel@tonic-gate 	global_flags = rctlblk_get_global_flags(current);
35147c478bd9Sstevel@tonic-gate 	if ((global_flags & RCTL_GLOBAL_DENY_NEVER) &&
35157c478bd9Sstevel@tonic-gate 	    action == RCTL_LOCAL_DENY)
35167c478bd9Sstevel@tonic-gate 		return (B_FALSE);
35177c478bd9Sstevel@tonic-gate 	if ((global_flags & RCTL_GLOBAL_DENY_ALWAYS) &&
35187c478bd9Sstevel@tonic-gate 	    action == RCTL_LOCAL_NOACTION)
35197c478bd9Sstevel@tonic-gate 		return (B_FALSE);
35207c478bd9Sstevel@tonic-gate 
35217c478bd9Sstevel@tonic-gate 	return (B_TRUE);
35227c478bd9Sstevel@tonic-gate }
3523*fa9e4066Sahrens 
3524*fa9e4066Sahrens static int
3525*fa9e4066Sahrens zonecfg_add_ds_core(zone_dochandle_t handle, struct zone_dstab *tabptr)
3526*fa9e4066Sahrens {
3527*fa9e4066Sahrens 	xmlNodePtr newnode, cur = handle->zone_dh_cur;
3528*fa9e4066Sahrens 	int err;
3529*fa9e4066Sahrens 
3530*fa9e4066Sahrens 	newnode = xmlNewTextChild(cur, NULL, DTD_ELEM_DATASET, NULL);
3531*fa9e4066Sahrens 	if ((err = newprop(newnode, DTD_ATTR_NAME,
3532*fa9e4066Sahrens 	    tabptr->zone_dataset_name)) != Z_OK)
3533*fa9e4066Sahrens 		return (err);
3534*fa9e4066Sahrens 	return (Z_OK);
3535*fa9e4066Sahrens }
3536*fa9e4066Sahrens 
3537*fa9e4066Sahrens int
3538*fa9e4066Sahrens zonecfg_add_ds(zone_dochandle_t handle, struct zone_dstab *tabptr)
3539*fa9e4066Sahrens {
3540*fa9e4066Sahrens 	int err;
3541*fa9e4066Sahrens 
3542*fa9e4066Sahrens 	if (tabptr == NULL)
3543*fa9e4066Sahrens 		return (Z_INVAL);
3544*fa9e4066Sahrens 
3545*fa9e4066Sahrens 	if ((err = operation_prep(handle)) != Z_OK)
3546*fa9e4066Sahrens 		return (err);
3547*fa9e4066Sahrens 
3548*fa9e4066Sahrens 	if ((err = zonecfg_add_ds_core(handle, tabptr)) != Z_OK)
3549*fa9e4066Sahrens 		return (err);
3550*fa9e4066Sahrens 
3551*fa9e4066Sahrens 	return (Z_OK);
3552*fa9e4066Sahrens }
3553*fa9e4066Sahrens 
3554*fa9e4066Sahrens static int
3555*fa9e4066Sahrens zonecfg_delete_ds_core(zone_dochandle_t handle, struct zone_dstab *tabptr)
3556*fa9e4066Sahrens {
3557*fa9e4066Sahrens 	xmlNodePtr cur = handle->zone_dh_cur;
3558*fa9e4066Sahrens 
3559*fa9e4066Sahrens 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
3560*fa9e4066Sahrens 		if (xmlStrcmp(cur->name, DTD_ELEM_DATASET))
3561*fa9e4066Sahrens 			continue;
3562*fa9e4066Sahrens 
3563*fa9e4066Sahrens 		if (match_prop(cur, DTD_ATTR_NAME,
3564*fa9e4066Sahrens 		    tabptr->zone_dataset_name)) {
3565*fa9e4066Sahrens 			xmlUnlinkNode(cur);
3566*fa9e4066Sahrens 			xmlFreeNode(cur);
3567*fa9e4066Sahrens 			return (Z_OK);
3568*fa9e4066Sahrens 		}
3569*fa9e4066Sahrens 	}
3570*fa9e4066Sahrens 	return (Z_NO_RESOURCE_ID);
3571*fa9e4066Sahrens }
3572*fa9e4066Sahrens 
3573*fa9e4066Sahrens int
3574*fa9e4066Sahrens zonecfg_delete_ds(zone_dochandle_t handle, struct zone_dstab *tabptr)
3575*fa9e4066Sahrens {
3576*fa9e4066Sahrens 	int err;
3577*fa9e4066Sahrens 
3578*fa9e4066Sahrens 	if (tabptr == NULL)
3579*fa9e4066Sahrens 		return (Z_INVAL);
3580*fa9e4066Sahrens 
3581*fa9e4066Sahrens 	if ((err = operation_prep(handle)) != Z_OK)
3582*fa9e4066Sahrens 		return (err);
3583*fa9e4066Sahrens 
3584*fa9e4066Sahrens 	if ((err = zonecfg_delete_ds_core(handle, tabptr)) != Z_OK)
3585*fa9e4066Sahrens 		return (err);
3586*fa9e4066Sahrens 
3587*fa9e4066Sahrens 	return (Z_OK);
3588*fa9e4066Sahrens }
3589*fa9e4066Sahrens 
3590*fa9e4066Sahrens int
3591*fa9e4066Sahrens zonecfg_modify_ds(
3592*fa9e4066Sahrens 	zone_dochandle_t handle,
3593*fa9e4066Sahrens 	struct zone_dstab *oldtabptr,
3594*fa9e4066Sahrens 	struct zone_dstab *newtabptr)
3595*fa9e4066Sahrens {
3596*fa9e4066Sahrens 	int err;
3597*fa9e4066Sahrens 
3598*fa9e4066Sahrens 	if (oldtabptr == NULL || newtabptr == NULL)
3599*fa9e4066Sahrens 		return (Z_INVAL);
3600*fa9e4066Sahrens 
3601*fa9e4066Sahrens 	if ((err = operation_prep(handle)) != Z_OK)
3602*fa9e4066Sahrens 		return (err);
3603*fa9e4066Sahrens 
3604*fa9e4066Sahrens 	if ((err = zonecfg_delete_ds_core(handle, oldtabptr)) != Z_OK)
3605*fa9e4066Sahrens 		return (err);
3606*fa9e4066Sahrens 
3607*fa9e4066Sahrens 	if ((err = zonecfg_add_ds_core(handle, newtabptr)) != Z_OK)
3608*fa9e4066Sahrens 		return (err);
3609*fa9e4066Sahrens 
3610*fa9e4066Sahrens 	return (Z_OK);
3611*fa9e4066Sahrens }
3612*fa9e4066Sahrens 
3613*fa9e4066Sahrens int
3614*fa9e4066Sahrens zonecfg_lookup_ds(zone_dochandle_t handle, struct zone_dstab *tabptr)
3615*fa9e4066Sahrens {
3616*fa9e4066Sahrens 	xmlNodePtr cur, firstmatch;
3617*fa9e4066Sahrens 	int err;
3618*fa9e4066Sahrens 	char dataset[MAXNAMELEN];
3619*fa9e4066Sahrens 
3620*fa9e4066Sahrens 	if (tabptr == NULL)
3621*fa9e4066Sahrens 		return (Z_INVAL);
3622*fa9e4066Sahrens 
3623*fa9e4066Sahrens 	if ((err = operation_prep(handle)) != Z_OK)
3624*fa9e4066Sahrens 		return (err);
3625*fa9e4066Sahrens 
3626*fa9e4066Sahrens 	cur = handle->zone_dh_cur;
3627*fa9e4066Sahrens 	firstmatch = NULL;
3628*fa9e4066Sahrens 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
3629*fa9e4066Sahrens 		if (xmlStrcmp(cur->name, DTD_ELEM_DATASET))
3630*fa9e4066Sahrens 			continue;
3631*fa9e4066Sahrens 		if (strlen(tabptr->zone_dataset_name) > 0) {
3632*fa9e4066Sahrens 			if ((fetchprop(cur, DTD_ATTR_NAME, dataset,
3633*fa9e4066Sahrens 			    sizeof (dataset)) == Z_OK) &&
3634*fa9e4066Sahrens 			    (strcmp(tabptr->zone_dataset_name,
3635*fa9e4066Sahrens 			    dataset) == 0)) {
3636*fa9e4066Sahrens 				if (firstmatch == NULL)
3637*fa9e4066Sahrens 					firstmatch = cur;
3638*fa9e4066Sahrens 				else
3639*fa9e4066Sahrens 					return (Z_INSUFFICIENT_SPEC);
3640*fa9e4066Sahrens 			}
3641*fa9e4066Sahrens 		}
3642*fa9e4066Sahrens 	}
3643*fa9e4066Sahrens 	if (firstmatch == NULL)
3644*fa9e4066Sahrens 		return (Z_NO_RESOURCE_ID);
3645*fa9e4066Sahrens 
3646*fa9e4066Sahrens 	cur = firstmatch;
3647*fa9e4066Sahrens 
3648*fa9e4066Sahrens 	if ((err = fetchprop(cur, DTD_ATTR_NAME, tabptr->zone_dataset_name,
3649*fa9e4066Sahrens 	    sizeof (tabptr->zone_dataset_name))) != Z_OK)
3650*fa9e4066Sahrens 		return (err);
3651*fa9e4066Sahrens 
3652*fa9e4066Sahrens 	return (Z_OK);
3653*fa9e4066Sahrens }
3654*fa9e4066Sahrens 
3655*fa9e4066Sahrens int
3656*fa9e4066Sahrens zonecfg_setdsent(zone_dochandle_t handle)
3657*fa9e4066Sahrens {
3658*fa9e4066Sahrens 	return (zonecfg_setent(handle));
3659*fa9e4066Sahrens }
3660*fa9e4066Sahrens 
3661*fa9e4066Sahrens int
3662*fa9e4066Sahrens zonecfg_getdsent(zone_dochandle_t handle, struct zone_dstab *tabptr)
3663*fa9e4066Sahrens {
3664*fa9e4066Sahrens 	xmlNodePtr cur;
3665*fa9e4066Sahrens 	int err;
3666*fa9e4066Sahrens 
3667*fa9e4066Sahrens 	if (handle == NULL)
3668*fa9e4066Sahrens 		return (Z_INVAL);
3669*fa9e4066Sahrens 
3670*fa9e4066Sahrens 	if ((cur = handle->zone_dh_cur) == NULL)
3671*fa9e4066Sahrens 		return (Z_NO_ENTRY);
3672*fa9e4066Sahrens 
3673*fa9e4066Sahrens 	for (; cur != NULL; cur = cur->next)
3674*fa9e4066Sahrens 		if (!xmlStrcmp(cur->name, DTD_ELEM_DATASET))
3675*fa9e4066Sahrens 			break;
3676*fa9e4066Sahrens 	if (cur == NULL) {
3677*fa9e4066Sahrens 		handle->zone_dh_cur = handle->zone_dh_top;
3678*fa9e4066Sahrens 		return (Z_NO_ENTRY);
3679*fa9e4066Sahrens 	}
3680*fa9e4066Sahrens 
3681*fa9e4066Sahrens 	if ((err = fetchprop(cur, DTD_ATTR_NAME, tabptr->zone_dataset_name,
3682*fa9e4066Sahrens 	    sizeof (tabptr->zone_dataset_name))) != Z_OK) {
3683*fa9e4066Sahrens 		handle->zone_dh_cur = handle->zone_dh_top;
3684*fa9e4066Sahrens 		return (err);
3685*fa9e4066Sahrens 	}
3686*fa9e4066Sahrens 
3687*fa9e4066Sahrens 	handle->zone_dh_cur = cur->next;
3688*fa9e4066Sahrens 	return (Z_OK);
3689*fa9e4066Sahrens }
3690*fa9e4066Sahrens 
3691*fa9e4066Sahrens int
3692*fa9e4066Sahrens zonecfg_enddsent(zone_dochandle_t handle)
3693*fa9e4066Sahrens {
3694*fa9e4066Sahrens 	return (zonecfg_endent(handle));
3695*fa9e4066Sahrens }
3696