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