13034Sdougm /*
23034Sdougm  * CDDL HEADER START
33034Sdougm  *
43034Sdougm  * The contents of this file are subject to the terms of the
53034Sdougm  * Common Development and Distribution License (the "License").
63034Sdougm  * You may not use this file except in compliance with the License.
73034Sdougm  *
83034Sdougm  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
93034Sdougm  * or http://www.opensolaris.org/os/licensing.
103034Sdougm  * See the License for the specific language governing permissions
113034Sdougm  * and limitations under the License.
123034Sdougm  *
133034Sdougm  * When distributing Covered Code, include this CDDL HEADER in each
143034Sdougm  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
153034Sdougm  * If applicable, add the following below this CDDL HEADER, with the
163034Sdougm  * fields enclosed by brackets "[]" replaced with your own identifying
173034Sdougm  * information: Portions Copyright [yyyy] [name of copyright owner]
183034Sdougm  *
193034Sdougm  * CDDL HEADER END
203034Sdougm  */
213034Sdougm 
223034Sdougm /*
233348Sdougm  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
243034Sdougm  * Use is subject to license terms.
253034Sdougm  */
263034Sdougm 
273034Sdougm #pragma ident	"%Z%%M%	%I%	%E% SMI"
283034Sdougm 
293034Sdougm /*
303034Sdougm  * Share control API
313034Sdougm  */
323034Sdougm #include <stdio.h>
333034Sdougm #include <string.h>
343034Sdougm #include <ctype.h>
353034Sdougm #include <sys/types.h>
363034Sdougm #include <sys/stat.h>
373663Sdougm #include <fcntl.h>
383034Sdougm #include <unistd.h>
393034Sdougm #include <libxml/parser.h>
403034Sdougm #include <libxml/tree.h>
413034Sdougm #include "libshare.h"
423034Sdougm #include "libshare_impl.h"
433034Sdougm #include <libscf.h>
443034Sdougm #include "scfutil.h"
453034Sdougm #include <ctype.h>
463034Sdougm #include <libintl.h>
473910Sdougm #include <thread.h>
483910Sdougm #include <synch.h>
493034Sdougm 
503034Sdougm #if _NOT_SMF
513034Sdougm #define	CONFIG_FILE	"/var/tmp/share.cfg"
523034Sdougm #define	CONFIG_FILE_TMP	"/var/tmp/share.cfg.tmp"
533034Sdougm #endif
543034Sdougm #define	TSTAMP(tm)	(uint64_t)(((uint64_t)tm.tv_sec << 32) | \
553034Sdougm 					(tm.tv_nsec & 0xffffffff))
563034Sdougm 
573663Sdougm #define	DFS_LOCK_FILE	"/etc/dfs/fstypes"
584327Sdougm #define	SA_STRSIZE	256	/* max string size for names */
593663Sdougm 
603034Sdougm /*
61*5331Samw  * internal object type values returned by sa_get_object_type()
62*5331Samw  */
63*5331Samw #define	SA_TYPE_UNKNOWN		0
64*5331Samw #define	SA_TYPE_GROUP		1
65*5331Samw #define	SA_TYPE_SHARE		2
66*5331Samw #define	SA_TYPE_RESOURCE	3
67*5331Samw #define	SA_TYPE_OPTIONSET	4
68*5331Samw #define	SA_TYPE_ALTSPACE	5
69*5331Samw 
70*5331Samw /*
713034Sdougm  * internal data structures
723034Sdougm  */
733034Sdougm 
743034Sdougm extern struct sa_proto_plugin *sap_proto_list;
753034Sdougm 
763034Sdougm /* current SMF/SVC repository handle */
773910Sdougm extern void getlegacyconfig(sa_handle_t, char *, xmlNodePtr *);
783910Sdougm extern int gettransients(sa_handle_impl_t, xmlNodePtr *);
793034Sdougm extern int sa_valid_property(void *, char *, sa_property_t);
803034Sdougm extern char *sa_fstype(char *);
813034Sdougm extern int sa_is_share(void *);
82*5331Samw extern int sa_is_resource(void *);
833034Sdougm extern ssize_t scf_max_name_len; /* defined in scfutil during initialization */
843034Sdougm extern int sa_group_is_zfs(sa_group_t);
853034Sdougm extern int sa_path_is_zfs(char *);
863034Sdougm extern int sa_zfs_set_sharenfs(sa_group_t, char *, int);
87*5331Samw extern int sa_zfs_set_sharesmb(sa_group_t, char *, int);
883910Sdougm extern void update_legacy_config(sa_handle_t);
893034Sdougm extern int issubdir(char *, char *);
904327Sdougm extern int sa_zfs_init(sa_handle_impl_t);
913910Sdougm extern void sa_zfs_fini(sa_handle_impl_t);
923663Sdougm extern void sablocksigs(sigset_t *);
933663Sdougm extern void saunblocksigs(sigset_t *);
94*5331Samw static sa_group_t sa_get_optionset_parent(sa_optionset_t);
95*5331Samw static char *get_node_attr(void *, char *);
963034Sdougm 
973910Sdougm /*
983910Sdougm  * Data structures for finding/managing the document root to access
993910Sdougm  * handle mapping. The list isn't expected to grow very large so a
1003910Sdougm  * simple list is acceptable. The purpose is to provide a way to start
1013910Sdougm  * with a group or share and find the library handle needed for
1023910Sdougm  * various operations.
1033910Sdougm  */
1043910Sdougm mutex_t sa_global_lock;
1053910Sdougm struct doc2handle {
1063910Sdougm 	struct doc2handle	*next;
1073910Sdougm 	xmlNodePtr		root;
1083910Sdougm 	sa_handle_impl_t	handle;
1093910Sdougm };
1103910Sdougm 
1114327Sdougm /* definitions used in a couple of property functions */
1124327Sdougm #define	SA_PROP_OP_REMOVE	1
1134327Sdougm #define	SA_PROP_OP_ADD		2
1144327Sdougm #define	SA_PROP_OP_UPDATE	3
1154327Sdougm 
1163910Sdougm static struct doc2handle *sa_global_handles = NULL;
1173034Sdougm 
1183034Sdougm /* helper functions */
1193034Sdougm 
1203910Sdougm /*
1213910Sdougm  * sa_errorstr(err)
1223910Sdougm  *
1233910Sdougm  * convert an error value to an error string
1243910Sdougm  */
1253910Sdougm 
1263034Sdougm char *
1273034Sdougm sa_errorstr(int err)
1283034Sdougm {
1293034Sdougm 	static char errstr[32];
1303034Sdougm 	char *ret = NULL;
1313034Sdougm 
1323034Sdougm 	switch (err) {
1333034Sdougm 	case SA_OK:
1344327Sdougm 		ret = dgettext(TEXT_DOMAIN, "ok");
1354327Sdougm 		break;
1363034Sdougm 	case SA_NO_SUCH_PATH:
1374327Sdougm 		ret = dgettext(TEXT_DOMAIN, "path doesn't exist");
1384327Sdougm 		break;
1393034Sdougm 	case SA_NO_MEMORY:
1404327Sdougm 		ret = dgettext(TEXT_DOMAIN, "no memory");
1414327Sdougm 		break;
1423034Sdougm 	case SA_DUPLICATE_NAME:
1434327Sdougm 		ret = dgettext(TEXT_DOMAIN, "name in use");
1444327Sdougm 		break;
1453034Sdougm 	case SA_BAD_PATH:
1464327Sdougm 		ret = dgettext(TEXT_DOMAIN, "bad path");
1474327Sdougm 		break;
1483034Sdougm 	case SA_NO_SUCH_GROUP:
1494327Sdougm 		ret = dgettext(TEXT_DOMAIN, "no such group");
1504327Sdougm 		break;
1513034Sdougm 	case SA_CONFIG_ERR:
1524327Sdougm 		ret = dgettext(TEXT_DOMAIN, "configuration error");
1534327Sdougm 		break;
1543034Sdougm 	case SA_SYSTEM_ERR:
1554327Sdougm 		ret = dgettext(TEXT_DOMAIN, "system error");
1564327Sdougm 		break;
1573034Sdougm 	case SA_SYNTAX_ERR:
1584327Sdougm 		ret = dgettext(TEXT_DOMAIN, "syntax error");
1594327Sdougm 		break;
1603034Sdougm 	case SA_NO_PERMISSION:
1614327Sdougm 		ret = dgettext(TEXT_DOMAIN, "no permission");
1624327Sdougm 		break;
1633034Sdougm 	case SA_BUSY:
1644327Sdougm 		ret = dgettext(TEXT_DOMAIN, "busy");
1654327Sdougm 		break;
1663034Sdougm 	case SA_NO_SUCH_PROP:
1674327Sdougm 		ret = dgettext(TEXT_DOMAIN, "no such property");
1684327Sdougm 		break;
1693034Sdougm 	case SA_INVALID_NAME:
1704327Sdougm 		ret = dgettext(TEXT_DOMAIN, "invalid name");
1714327Sdougm 		break;
1723034Sdougm 	case SA_INVALID_PROTOCOL:
1734327Sdougm 		ret = dgettext(TEXT_DOMAIN, "invalid protocol");
1744327Sdougm 		break;
1753034Sdougm 	case SA_NOT_ALLOWED:
1764327Sdougm 		ret = dgettext(TEXT_DOMAIN, "operation not allowed");
1774327Sdougm 		break;
1783034Sdougm 	case SA_BAD_VALUE:
1794327Sdougm 		ret = dgettext(TEXT_DOMAIN, "bad property value");
1804327Sdougm 		break;
1813034Sdougm 	case SA_INVALID_SECURITY:
1824327Sdougm 		ret = dgettext(TEXT_DOMAIN, "invalid security type");
1834327Sdougm 		break;
1843034Sdougm 	case SA_NO_SUCH_SECURITY:
1854327Sdougm 		ret = dgettext(TEXT_DOMAIN, "security type not found");
1864327Sdougm 		break;
1873034Sdougm 	case SA_VALUE_CONFLICT:
1884327Sdougm 		ret = dgettext(TEXT_DOMAIN, "property value conflict");
1894327Sdougm 		break;
1903034Sdougm 	case SA_NOT_IMPLEMENTED:
1914327Sdougm 		ret = dgettext(TEXT_DOMAIN, "not implemented");
1924327Sdougm 		break;
1933034Sdougm 	case SA_INVALID_PATH:
1944327Sdougm 		ret = dgettext(TEXT_DOMAIN, "invalid path");
1954327Sdougm 		break;
1963034Sdougm 	case SA_NOT_SUPPORTED:
1974327Sdougm 		ret = dgettext(TEXT_DOMAIN, "operation not supported");
1984327Sdougm 		break;
1993034Sdougm 	case SA_PROP_SHARE_ONLY:
2004327Sdougm 		ret = dgettext(TEXT_DOMAIN, "property not valid for group");
2014327Sdougm 		break;
2023034Sdougm 	case SA_NOT_SHARED:
2034327Sdougm 		ret = dgettext(TEXT_DOMAIN, "not shared");
2044327Sdougm 		break;
205*5331Samw 	case SA_NO_SUCH_RESOURCE:
206*5331Samw 		ret = dgettext(TEXT_DOMAIN, "no such resource");
207*5331Samw 		break;
208*5331Samw 	case SA_RESOURCE_REQUIRED:
209*5331Samw 		ret = dgettext(TEXT_DOMAIN, "resource name required");
210*5331Samw 		break;
211*5331Samw 	case SA_MULTIPLE_ERROR:
212*5331Samw 		ret = dgettext(TEXT_DOMAIN, "errors from multiple protocols");
213*5331Samw 		break;
214*5331Samw 	case SA_PATH_IS_SUBDIR:
215*5331Samw 		ret = dgettext(TEXT_DOMAIN, "path is a subpath of share");
216*5331Samw 		break;
217*5331Samw 	case SA_PATH_IS_PARENTDIR:
218*5331Samw 		ret = dgettext(TEXT_DOMAIN, "path is parent of a share");
219*5331Samw 		break;
2203034Sdougm 	default:
2214327Sdougm 		(void) snprintf(errstr, sizeof (errstr),
2224327Sdougm 		    dgettext(TEXT_DOMAIN, "unknown %d"), err);
2234327Sdougm 		ret = errstr;
2243034Sdougm 	}
2253034Sdougm 	return (ret);
2263034Sdougm }
2273034Sdougm 
2283034Sdougm /*
2293910Sdougm  * Document root to active handle mapping functions.  These are only
2303910Sdougm  * used internally. A mutex is used to prevent access while the list
2313910Sdougm  * is changing. In general, the list will be relatively short - one
2323910Sdougm  * item per thread that has called sa_init().
2333910Sdougm  */
2343910Sdougm 
2353910Sdougm sa_handle_impl_t
2363910Sdougm get_handle_for_root(xmlNodePtr root)
2373910Sdougm {
2383910Sdougm 	struct doc2handle *item;
2393910Sdougm 
2403910Sdougm 	(void) mutex_lock(&sa_global_lock);
2413910Sdougm 	for (item = sa_global_handles; item != NULL; item = item->next) {
2424327Sdougm 		if (item->root == root)
2434327Sdougm 			break;
2443910Sdougm 	}
2453910Sdougm 	(void) mutex_unlock(&sa_global_lock);
2463910Sdougm 	if (item != NULL)
2474327Sdougm 		return (item->handle);
2483910Sdougm 	return (NULL);
2493910Sdougm }
2503910Sdougm 
2513910Sdougm static int
2523910Sdougm add_handle_for_root(xmlNodePtr root, sa_handle_impl_t handle)
2533910Sdougm {
2543910Sdougm 	struct doc2handle *item;
2553910Sdougm 	int ret = SA_NO_MEMORY;
2563910Sdougm 
2573910Sdougm 	item = (struct doc2handle *)calloc(sizeof (struct doc2handle), 1);
2583910Sdougm 	if (item != NULL) {
2594327Sdougm 		item->root = root;
2604327Sdougm 		item->handle = handle;
2614327Sdougm 		(void) mutex_lock(&sa_global_lock);
2624327Sdougm 		item->next = sa_global_handles;
2634327Sdougm 		sa_global_handles = item;
2644327Sdougm 		(void) mutex_unlock(&sa_global_lock);
2654327Sdougm 		ret = SA_OK;
2663910Sdougm 	}
2673910Sdougm 	return (ret);
2683910Sdougm }
2693910Sdougm 
2703910Sdougm /*
2713910Sdougm  * remove_handle_for_root(root)
2723910Sdougm  *
2733910Sdougm  * Walks the list of handles and removes the one for this "root" from
2743910Sdougm  * the list. It is up to the caller to free the data.
2753910Sdougm  */
2763910Sdougm 
2773910Sdougm static void
2783910Sdougm remove_handle_for_root(xmlNodePtr root)
2793910Sdougm {
2803910Sdougm 	struct doc2handle *item, *prev;
2813910Sdougm 
2823910Sdougm 	(void) mutex_lock(&sa_global_lock);
2833910Sdougm 	for (prev = NULL, item = sa_global_handles; item != NULL;
2844327Sdougm 	    item = item->next) {
2854327Sdougm 		if (item->root == root) {
2864327Sdougm 			/* first in the list */
2874327Sdougm 			if (prev == NULL)
2884327Sdougm 				sa_global_handles = sa_global_handles->next;
2894327Sdougm 			else
2904327Sdougm 				prev->next = item->next;
2914327Sdougm 			/* Item is out of the list so free the list structure */
2924327Sdougm 			free(item);
2934327Sdougm 			break;
2943910Sdougm 		}
2954327Sdougm 		prev = item;
2963910Sdougm 	}
2973910Sdougm 	(void) mutex_unlock(&sa_global_lock);
2983910Sdougm }
2993910Sdougm 
3003910Sdougm /*
3013910Sdougm  * sa_find_group_handle(sa_group_t group)
3023910Sdougm  *
3033910Sdougm  * Find the sa_handle_t for the configuration associated with this
3043910Sdougm  * group.
3053910Sdougm  */
3063910Sdougm sa_handle_t
3073910Sdougm sa_find_group_handle(sa_group_t group)
3083910Sdougm {
3093910Sdougm 	xmlNodePtr node = (xmlNodePtr)group;
3103910Sdougm 	sa_handle_t handle;
3113910Sdougm 
3123910Sdougm 	while (node != NULL) {
3134327Sdougm 		if (strcmp((char *)(node->name), "sharecfg") == 0) {
3144327Sdougm 			/* have the root so get the handle */
3154327Sdougm 			handle = (sa_handle_t)get_handle_for_root(node);
3164327Sdougm 			return (handle);
3174327Sdougm 		}
3184327Sdougm 		node = node->parent;
3193910Sdougm 	}
3203910Sdougm 	return (NULL);
3213910Sdougm }
3223910Sdougm 
3233910Sdougm /*
3243034Sdougm  * set_legacy_timestamp(root, path, timevalue)
3253034Sdougm  *
3263034Sdougm  * add the current timestamp value to the configuration for use in
3273034Sdougm  * determining when to update the legacy files.  For SMF, this
3283034Sdougm  * property is kept in default/operation/legacy_timestamp
3293034Sdougm  */
3303034Sdougm 
3313034Sdougm static void
3323034Sdougm set_legacy_timestamp(xmlNodePtr root, char *path, uint64_t tval)
3333034Sdougm {
3343034Sdougm 	xmlNodePtr node;
3353034Sdougm 	xmlChar *lpath = NULL;
3363910Sdougm 	sa_handle_impl_t handle;
3373910Sdougm 
3383910Sdougm 	/* Have to have a handle or else we weren't initialized. */
3393910Sdougm 	handle = get_handle_for_root(root);
3403910Sdougm 	if (handle == NULL)
3414327Sdougm 		return;
3423034Sdougm 
3433034Sdougm 	for (node = root->xmlChildrenNode; node != NULL;
3444327Sdougm 	    node = node->next) {
3454327Sdougm 		if (xmlStrcmp(node->name, (xmlChar *)"legacy") == 0) {
3464327Sdougm 			/* a possible legacy node for this path */
3474327Sdougm 			lpath = xmlGetProp(node, (xmlChar *)"path");
3484327Sdougm 			if (lpath != NULL &&
3494327Sdougm 			    xmlStrcmp(lpath, (xmlChar *)path) == 0) {
3504327Sdougm 				xmlFree(lpath);
3514327Sdougm 				break;
3524327Sdougm 			}
3534327Sdougm 			if (lpath != NULL)
3544327Sdougm 				xmlFree(lpath);
3553034Sdougm 		}
3563034Sdougm 	}
3573034Sdougm 	if (node == NULL) {
3584327Sdougm 		/* need to create the first legacy timestamp node */
3594327Sdougm 		node = xmlNewChild(root, NULL, (xmlChar *)"legacy", NULL);
3603034Sdougm 	}
3613034Sdougm 	if (node != NULL) {
3624327Sdougm 		char tstring[32];
3634327Sdougm 		int ret;
3643034Sdougm 
3654327Sdougm 		(void) snprintf(tstring, sizeof (tstring), "%lld", tval);
3664327Sdougm 		xmlSetProp(node, (xmlChar *)"timestamp", (xmlChar *)tstring);
3674327Sdougm 		xmlSetProp(node, (xmlChar *)"path", (xmlChar *)path);
3684327Sdougm 		/* now commit to SMF */
3694327Sdougm 		ret = sa_get_instance(handle->scfhandle, "default");
3703034Sdougm 		if (ret == SA_OK) {
3714327Sdougm 			ret = sa_start_transaction(handle->scfhandle,
3724327Sdougm 			    "operation");
3734327Sdougm 			if (ret == SA_OK) {
3744327Sdougm 				ret = sa_set_property(handle->scfhandle,
3754327Sdougm 				    "legacy-timestamp", tstring);
3764327Sdougm 				if (ret == SA_OK) {
3774327Sdougm 					(void) sa_end_transaction(
3784327Sdougm 					    handle->scfhandle);
3794327Sdougm 				} else {
3804327Sdougm 					sa_abort_transaction(handle->scfhandle);
3814327Sdougm 				}
3824327Sdougm 			}
3833034Sdougm 		}
3843034Sdougm 	}
3853034Sdougm }
3863034Sdougm 
3873034Sdougm /*
3883034Sdougm  * is_shared(share)
3893034Sdougm  *
3903034Sdougm  * determine if the specified share is currently shared or not.
3913034Sdougm  */
3923034Sdougm static int
3933034Sdougm is_shared(sa_share_t share)
3943034Sdougm {
3953034Sdougm 	char *shared;
3963034Sdougm 	int result = 0; /* assume not */
3973034Sdougm 
3983034Sdougm 	shared = sa_get_share_attr(share, "shared");
3993034Sdougm 	if (shared != NULL) {
4004327Sdougm 		if (strcmp(shared, "true") == 0)
4014327Sdougm 			result = 1;
4024327Sdougm 		sa_free_attr_string(shared);
4033034Sdougm 	}
4043034Sdougm 	return (result);
4053034Sdougm }
4063034Sdougm 
4073034Sdougm /*
408*5331Samw  * excluded_protocol(share, proto)
409*5331Samw  *
410*5331Samw  * Returns B_TRUE if the specified protocol appears in the "exclude"
411*5331Samw  * property. This is used to prevent sharing special case shares
412*5331Samw  * (e.g. subdirs when SMB wants a subdir and NFS doesn't. B_FALSE is
413*5331Samw  * returned if the protocol isn't in the list.
414*5331Samw  */
415*5331Samw static boolean_t
416*5331Samw excluded_protocol(sa_share_t share, char *proto)
417*5331Samw {
418*5331Samw 	char *protolist;
419*5331Samw 	char *str;
420*5331Samw 	char *token;
421*5331Samw 
422*5331Samw 	protolist = sa_get_share_attr(share, "exclude");
423*5331Samw 	if (protolist != NULL) {
424*5331Samw 		str = protolist;
425*5331Samw 		while ((token = strtok(str, ",")) != NULL) {
426*5331Samw 			if (strcmp(token, proto) == 0) {
427*5331Samw 				sa_free_attr_string(protolist);
428*5331Samw 				return (B_TRUE);
429*5331Samw 			}
430*5331Samw 			str = NULL;
431*5331Samw 		}
432*5331Samw 		sa_free_attr_string(protolist);
433*5331Samw 	}
434*5331Samw 	return (B_FALSE);
435*5331Samw }
436*5331Samw 
437*5331Samw /*
4383663Sdougm  * checksubdirgroup(group, newpath, strictness)
4393348Sdougm  *
4403663Sdougm  * check all the specified newpath against all the paths in the
4413663Sdougm  * group. This is a helper function for checksubdir to make it easier
4423663Sdougm  * to also check ZFS subgroups.
4433663Sdougm  * The strictness values mean:
4443348Sdougm  * SA_CHECK_NORMAL == only check newpath against shares that are active
4453348Sdougm  * SA_CHECK_STRICT == check newpath against both active shares and those
4463348Sdougm  *		      stored in the repository
4473034Sdougm  */
4483034Sdougm static int
4493663Sdougm checksubdirgroup(sa_group_t group, char *newpath, int strictness)
4503034Sdougm {
4513034Sdougm 	sa_share_t share;
4523663Sdougm 	char *path;
4533663Sdougm 	int issub = SA_OK;
454*5331Samw 	int subdir;
455*5331Samw 	int parent;
456*5331Samw 
457*5331Samw 	if (newpath == NULL)
458*5331Samw 		return (SA_INVALID_PATH);
4593034Sdougm 
4603663Sdougm 	for (share = sa_get_share(group, NULL); share != NULL;
4613663Sdougm 	    share = sa_get_next_share(share)) {
4623034Sdougm 		/*
4633034Sdougm 		 * The original behavior of share never checked
4643034Sdougm 		 * against the permanent configuration
4653034Sdougm 		 * (/etc/dfs/dfstab).  PIT has a number of cases where
4663034Sdougm 		 * it depends on this older behavior even though it
4673034Sdougm 		 * could be considered incorrect.  We may tighten this
4683034Sdougm 		 * up in the future.
4693034Sdougm 		 */
4704327Sdougm 		if (strictness == SA_CHECK_NORMAL && !is_shared(share))
4714327Sdougm 			continue;
4723034Sdougm 
4734327Sdougm 		path = sa_get_share_attr(share, "path");
4743348Sdougm 		/*
4753348Sdougm 		 * If path is NULL, then a share is in the process of
4763348Sdougm 		 * construction or someone has modified the property
4773663Sdougm 		 * group inappropriately. It should be
4783663Sdougm 		 * ignored. issubdir() comes from the original share
4793663Sdougm 		 * implementation and does the difficult part of
4803663Sdougm 		 * checking subdirectories.
4813348Sdougm 		 */
4824327Sdougm 		if (path == NULL)
4834327Sdougm 			continue;
484*5331Samw 
485*5331Samw 		if (strcmp(path, newpath) == 0) {
4864327Sdougm 			issub = SA_INVALID_PATH;
487*5331Samw 		} else {
488*5331Samw 			subdir = issubdir(newpath, path);
489*5331Samw 			parent = issubdir(path, newpath);
490*5331Samw 			if (subdir || parent) {
491*5331Samw 				sa_free_attr_string(path);
492*5331Samw 				path = NULL;
493*5331Samw 				return (subdir ?
494*5331Samw 				    SA_PATH_IS_SUBDIR : SA_PATH_IS_PARENTDIR);
495*5331Samw 			}
4964327Sdougm 		}
4973034Sdougm 		sa_free_attr_string(path);
4983034Sdougm 		path = NULL;
4993663Sdougm 	}
5003663Sdougm 	return (issub);
5013663Sdougm }
5023663Sdougm 
5033663Sdougm /*
5043663Sdougm  * checksubdir(newpath, strictness)
5053663Sdougm  *
5063663Sdougm  * checksubdir determines if the specified path (newpath) is a
5073663Sdougm  * subdirectory of another share. It calls checksubdirgroup() to do
5083663Sdougm  * the complicated work. The strictness parameter determines how
5093663Sdougm  * strict a check to make against the path. The strictness values
5103663Sdougm  * mean: SA_CHECK_NORMAL == only check newpath against shares that are
5113663Sdougm  * active SA_CHECK_STRICT == check newpath against both active shares
5123663Sdougm  * and those * stored in the repository
5133663Sdougm  */
5143663Sdougm static int
5153910Sdougm checksubdir(sa_handle_t handle, char *newpath, int strictness)
5163663Sdougm {
5173663Sdougm 	sa_group_t group;
518*5331Samw 	int issub = SA_OK;
5193663Sdougm 	char *path = NULL;
5203663Sdougm 
521*5331Samw 	for (group = sa_get_group(handle, NULL);
522*5331Samw 	    group != NULL && issub == SA_OK;
523*5331Samw 	    group = sa_get_next_group(group)) {
5244327Sdougm 		if (sa_group_is_zfs(group)) {
5254327Sdougm 			sa_group_t subgroup;
5264327Sdougm 			for (subgroup = sa_get_sub_group(group);
527*5331Samw 			    subgroup != NULL && issub == SA_OK;
5284327Sdougm 			    subgroup = sa_get_next_group(subgroup))
5294327Sdougm 				issub = checksubdirgroup(subgroup, newpath,
5304327Sdougm 				    strictness);
5314327Sdougm 		} else {
5324327Sdougm 			issub = checksubdirgroup(group, newpath, strictness);
5334327Sdougm 		}
5343034Sdougm 	}
5353034Sdougm 	if (path != NULL)
5364327Sdougm 		sa_free_attr_string(path);
5373034Sdougm 	return (issub);
5383034Sdougm }
5393034Sdougm 
5403034Sdougm /*
5413348Sdougm  * validpath(path, strictness)
5423034Sdougm  * determine if the provided path is valid for a share. It shouldn't
5433034Sdougm  * be a sub-dir of an already shared path or the parent directory of a
5443034Sdougm  * share path.
5453034Sdougm  */
5463034Sdougm static int
5473910Sdougm validpath(sa_handle_t handle, char *path, int strictness)
5483034Sdougm {
5493034Sdougm 	int error = SA_OK;
5503034Sdougm 	struct stat st;
5513034Sdougm 	sa_share_t share;
5523034Sdougm 	char *fstype;
5533034Sdougm 
5544327Sdougm 	if (*path != '/')
5554327Sdougm 		return (SA_BAD_PATH);
5564327Sdougm 
5573034Sdougm 	if (stat(path, &st) < 0) {
5584327Sdougm 		error = SA_NO_SUCH_PATH;
5593034Sdougm 	} else {
5604327Sdougm 		share = sa_find_share(handle, path);
5614327Sdougm 		if (share != NULL)
5624327Sdougm 			error = SA_DUPLICATE_NAME;
5634327Sdougm 
5644327Sdougm 		if (error == SA_OK) {
5654327Sdougm 			/*
5664327Sdougm 			 * check for special case with file system
5674327Sdougm 			 * that might have restrictions.  For now, ZFS
5684327Sdougm 			 * is the only case since it has its own idea
5694327Sdougm 			 * of how to configure shares. We do this
5704327Sdougm 			 * before subdir checking since things like
5714327Sdougm 			 * ZFS will do that for us. This should also
5724327Sdougm 			 * be done via plugin interface.
5734327Sdougm 			 */
5744327Sdougm 			fstype = sa_fstype(path);
5754327Sdougm 			if (fstype != NULL && strcmp(fstype, "zfs") == 0) {
5764327Sdougm 				if (sa_zfs_is_shared(handle, path))
5774327Sdougm 					error = SA_INVALID_NAME;
5784327Sdougm 			}
5794327Sdougm 			if (fstype != NULL)
5804327Sdougm 				sa_free_fstype(fstype);
5813034Sdougm 		}
5824327Sdougm 		if (error == SA_OK)
5834327Sdougm 			error = checksubdir(handle, path, strictness);
5843034Sdougm 	}
5853034Sdougm 	return (error);
5863034Sdougm }
5873034Sdougm 
5883034Sdougm /*
5893034Sdougm  * check to see if group/share is persistent.
590*5331Samw  *
591*5331Samw  * "group" can be either an sa_group_t or an sa_share_t. (void *)
592*5331Samw  * works since both thse types are also void *.
5933034Sdougm  */
594*5331Samw int
595*5331Samw sa_is_persistent(void *group)
5963034Sdougm {
5973034Sdougm 	char *type;
5983034Sdougm 	int persist = 1;
5993034Sdougm 
600*5331Samw 	type = sa_get_group_attr((sa_group_t)group, "type");
6013034Sdougm 	if (type != NULL && strcmp(type, "transient") == 0)
6024327Sdougm 		persist = 0;
6033034Sdougm 	if (type != NULL)
6044327Sdougm 		sa_free_attr_string(type);
6053034Sdougm 	return (persist);
6063034Sdougm }
6073034Sdougm 
6083034Sdougm /*
6093034Sdougm  * sa_valid_group_name(name)
6103034Sdougm  *
6113034Sdougm  * check that the "name" contains only valid characters and otherwise
6123034Sdougm  * fits the required naming conventions. Valid names must start with
6133034Sdougm  * an alphabetic and the remainder may consist of only alphanumeric
6143034Sdougm  * plus the '-' and '_' characters. This name limitation comes from
6153034Sdougm  * inherent limitations in SMF.
6163034Sdougm  */
6173034Sdougm 
6183034Sdougm int
6193034Sdougm sa_valid_group_name(char *name)
6203034Sdougm {
6213034Sdougm 	int ret = 1;
6223034Sdougm 	ssize_t len;
6233034Sdougm 
6243034Sdougm 	if (name != NULL && isalpha(*name)) {
6254327Sdougm 		char c;
6264327Sdougm 		len = strlen(name);
6274327Sdougm 		if (len < (scf_max_name_len - sizeof ("group:"))) {
6284327Sdougm 			for (c = *name++; c != '\0' && ret != 0; c = *name++) {
6294327Sdougm 				if (!isalnum(c) && c != '-' && c != '_')
6304327Sdougm 					ret = 0;
6314327Sdougm 			}
6324327Sdougm 		} else {
6333034Sdougm 			ret = 0;
6343034Sdougm 		}
6354327Sdougm 	} else {
6363034Sdougm 		ret = 0;
6373034Sdougm 	}
6383034Sdougm 	return (ret);
6393034Sdougm }
6403034Sdougm 
6413034Sdougm 
6423034Sdougm /*
6433034Sdougm  * is_zfs_group(group)
6443034Sdougm  *	Determine if the specified group is a ZFS sharenfs group
6453034Sdougm  */
6463034Sdougm static int
6473034Sdougm is_zfs_group(sa_group_t group)
6483034Sdougm {
6493034Sdougm 	int ret = 0;
6503034Sdougm 	xmlNodePtr parent;
6513034Sdougm 	xmlChar *zfs;
6523034Sdougm 
6534327Sdougm 	if (strcmp((char *)((xmlNodePtr)group)->name, "share") == 0)
6544327Sdougm 		parent = (xmlNodePtr)sa_get_parent_group(group);
6554327Sdougm 	else
6564327Sdougm 		parent = (xmlNodePtr)group;
6573034Sdougm 	zfs = xmlGetProp(parent, (xmlChar *)"zfs");
6583034Sdougm 	if (zfs != NULL) {
6594327Sdougm 		xmlFree(zfs);
6604327Sdougm 		ret = 1;
6613034Sdougm 	}
6623034Sdougm 	return (ret);
6633034Sdougm }
6643034Sdougm 
6653034Sdougm /*
666*5331Samw  * sa_get_object_type(object)
667*5331Samw  *
668*5331Samw  * This function returns a numeric value representing the object
669*5331Samw  * type. This allows using simpler checks when doing type specific
670*5331Samw  * operations.
671*5331Samw  */
672*5331Samw 
673*5331Samw static int
674*5331Samw sa_get_object_type(void *object)
675*5331Samw {
676*5331Samw 	xmlNodePtr node = (xmlNodePtr)object;
677*5331Samw 	int type;
678*5331Samw 
679*5331Samw 	if (xmlStrcmp(node->name, (xmlChar *)"group") == 0)
680*5331Samw 		type = SA_TYPE_GROUP;
681*5331Samw 	else if (xmlStrcmp(node->name, (xmlChar *)"share") == 0)
682*5331Samw 		type = SA_TYPE_SHARE;
683*5331Samw 	else if (xmlStrcmp(node->name, (xmlChar *)"resource") == 0)
684*5331Samw 		type = SA_TYPE_RESOURCE;
685*5331Samw 	else if (xmlStrcmp(node->name, (xmlChar *)"optionset") == 0)
686*5331Samw 		type = SA_TYPE_OPTIONSET;
687*5331Samw 	else if (xmlStrcmp(node->name, (xmlChar *)"security") == 0)
688*5331Samw 		type = SA_TYPE_ALTSPACE;
689*5331Samw 	else
690*5331Samw 		assert(0);
691*5331Samw 	return (type);
692*5331Samw }
693*5331Samw 
694*5331Samw /*
6953034Sdougm  * sa_optionset_name(optionset, oname, len, id)
6963034Sdougm  *	return the SMF name for the optionset. If id is not NULL, it
6973034Sdougm  *	will have the GUID value for a share and should be used
6983034Sdougm  *	instead of the keyword "optionset" which is used for
6993034Sdougm  *	groups. If the optionset doesn't have a protocol type
7003034Sdougm  *	associated with it, "default" is used. This shouldn't happen
7013034Sdougm  *	at this point but may be desirable in the future if there are
7023034Sdougm  *	protocol independent properties added. The name is returned in
7033034Sdougm  *	oname.
7043034Sdougm  */
7053034Sdougm 
7063034Sdougm static int
7073034Sdougm sa_optionset_name(sa_optionset_t optionset, char *oname, size_t len, char *id)
7083034Sdougm {
7093034Sdougm 	char *proto;
710*5331Samw 	void *parent;
711*5331Samw 	int ptype;
7123034Sdougm 
7133034Sdougm 	if (id == NULL)
7144327Sdougm 		id = "optionset";
7153034Sdougm 
716*5331Samw 	parent = sa_get_optionset_parent(optionset);
717*5331Samw 	if (parent != NULL) {
718*5331Samw 		ptype = sa_get_object_type(parent);
719*5331Samw 		proto = sa_get_optionset_attr(optionset, "type");
720*5331Samw 		if (ptype != SA_TYPE_RESOURCE) {
721*5331Samw 			len = snprintf(oname, len, "%s_%s", id,
722*5331Samw 			    proto ? proto : "default");
723*5331Samw 		} else {
724*5331Samw 			char *index;
725*5331Samw 			index = get_node_attr((void *)parent, "id");
726*5331Samw 			if (index != NULL)
727*5331Samw 				len = snprintf(oname, len, "%s_%s_%s", id,
728*5331Samw 				    proto ? proto : "default", index);
729*5331Samw 			else
730*5331Samw 				len = 0;
731*5331Samw 		}
732*5331Samw 
733*5331Samw 		if (proto != NULL)
734*5331Samw 			sa_free_attr_string(proto);
735*5331Samw 	} else {
736*5331Samw 		len = 0;
737*5331Samw 	}
7383034Sdougm 	return (len);
7393034Sdougm }
7403034Sdougm 
7413034Sdougm /*
7423034Sdougm  * sa_security_name(optionset, oname, len, id)
7433034Sdougm  *
7443034Sdougm  * return the SMF name for the security. If id is not NULL, it will
7453034Sdougm  * have the GUID value for a share and should be used instead of the
7463034Sdougm  * keyword "optionset" which is used for groups. If the optionset
7473034Sdougm  * doesn't have a protocol type associated with it, "default" is
7483034Sdougm  * used. This shouldn't happen at this point but may be desirable in
7493034Sdougm  * the future if there are protocol independent properties added. The
7503034Sdougm  * name is returned in oname. The security type is also encoded into
7513034Sdougm  * the name. In the future, this wil *be handled a bit differently.
7523034Sdougm  */
7533034Sdougm 
7543034Sdougm static int
7553034Sdougm sa_security_name(sa_security_t security, char *oname, size_t len, char *id)
7563034Sdougm {
7573034Sdougm 	char *proto;
7583034Sdougm 	char *sectype;
7593034Sdougm 
7603034Sdougm 	if (id == NULL)
7614327Sdougm 		id = "optionset";
7623034Sdougm 
7633034Sdougm 	proto = sa_get_security_attr(security, "type");
7643034Sdougm 	sectype = sa_get_security_attr(security, "sectype");
7654327Sdougm 	len = snprintf(oname, len, "%s_%s_%s", id, proto ? proto : "default",
7664327Sdougm 	    sectype ? sectype : "default");
7673034Sdougm 	if (proto != NULL)
7684327Sdougm 		sa_free_attr_string(proto);
7693034Sdougm 	if (sectype != NULL)
7704327Sdougm 		sa_free_attr_string(sectype);
7713034Sdougm 	return (len);
7723034Sdougm }
7733034Sdougm 
7743034Sdougm /*
7754327Sdougm  * verifydefgroupopts(handle)
7764327Sdougm  *
7774327Sdougm  * Make sure a "default" group exists and has default protocols enabled.
7784327Sdougm  */
7794327Sdougm static void
7804327Sdougm verifydefgroupopts(sa_handle_t handle)
7814327Sdougm {
7824327Sdougm 	sa_group_t defgrp;
7834327Sdougm 	sa_optionset_t opt;
784*5331Samw 
7854327Sdougm 	defgrp = sa_get_group(handle, "default");
7864327Sdougm 	if (defgrp != NULL) {
7874327Sdougm 		opt = sa_get_optionset(defgrp, NULL);
7884327Sdougm 		/*
7894327Sdougm 		 * NFS is the default for default group
7904327Sdougm 		 */
7914327Sdougm 		if (opt == NULL)
7924327Sdougm 			opt = sa_create_optionset(defgrp, "nfs");
7934327Sdougm 	}
7944327Sdougm }
7954327Sdougm 
7964327Sdougm /*
7973348Sdougm  * sa_init(init_service)
7983034Sdougm  *	Initialize the API
7993034Sdougm  *	find all the shared objects
8003034Sdougm  *	init the tables with all objects
8013034Sdougm  *	read in the current configuration
8023034Sdougm  */
8033034Sdougm 
8044327Sdougm #define	GETPROP(prop)	scf_simple_prop_next_astring(prop)
8054327Sdougm #define	CHECKTSTAMP(st, tval)	stat(SA_LEGACY_DFSTAB, &st) >= 0 && \
8064327Sdougm 	tval != TSTAMP(st.st_ctim)
8074327Sdougm 
8083910Sdougm sa_handle_t
8093034Sdougm sa_init(int init_service)
8103034Sdougm {
8113034Sdougm 	struct stat st;
8123034Sdougm 	int legacy = 0;
8133034Sdougm 	uint64_t tval = 0;
8143663Sdougm 	int lockfd;
8153663Sdougm 	sigset_t old;
8163663Sdougm 	int updatelegacy = B_FALSE;
8173663Sdougm 	scf_simple_prop_t *prop;
8183910Sdougm 	sa_handle_impl_t handle;
8193910Sdougm 	int err;
8203034Sdougm 
8213910Sdougm 	handle = calloc(sizeof (struct sa_handle_impl), 1);
8223910Sdougm 
8233910Sdougm 	if (handle != NULL) {
8244327Sdougm 		/* get protocol specific structures */
8254327Sdougm 		(void) proto_plugin_init();
8264327Sdougm 		if (init_service & SA_INIT_SHARE_API) {
8273663Sdougm 			/*
8284327Sdougm 			 * initialize access into libzfs. We use this
8294327Sdougm 			 * when collecting info about ZFS datasets and
8304327Sdougm 			 * shares.
8313663Sdougm 			 */
8324327Sdougm 			if (sa_zfs_init(handle) == B_FALSE) {
8334327Sdougm 				free(handle);
8344327Sdougm 				(void) proto_plugin_fini();
8354327Sdougm 				return (NULL);
8364327Sdougm 			}
8373663Sdougm 			/*
8384327Sdougm 			 * since we want to use SMF, initialize an svc handle
8394327Sdougm 			 * and find out what is there.
8403663Sdougm 			 */
8414327Sdougm 			handle->scfhandle = sa_scf_init(handle);
8424327Sdougm 			if (handle->scfhandle != NULL) {
8434327Sdougm 				/*
8444327Sdougm 				 * Need to lock the extraction of the
8454327Sdougm 				 * configuration if the dfstab file has
8464327Sdougm 				 * changed. Lock everything now and release if
8474327Sdougm 				 * not needed.  Use a file that isn't being
8484327Sdougm 				 * manipulated by other parts of the system in
8494327Sdougm 				 * order to not interfere with locking. Using
8504327Sdougm 				 * dfstab doesn't work.
8514327Sdougm 				 */
8524327Sdougm 				sablocksigs(&old);
8534327Sdougm 				lockfd = open(DFS_LOCK_FILE, O_RDWR);
8544327Sdougm 				if (lockfd >= 0) {
8554327Sdougm 					extern int errno;
8564327Sdougm 					errno = 0;
8574327Sdougm 					(void) lockf(lockfd, F_LOCK, 0);
8584327Sdougm 					/*
8594327Sdougm 					 * Check whether we are going to need
8604327Sdougm 					 * to merge any dfstab changes. This
8614327Sdougm 					 * is done by comparing the value of
8624327Sdougm 					 * legacy-timestamp with the current
8634327Sdougm 					 * st_ctim of the file. If they are
8644327Sdougm 					 * different, an update is needed and
8654327Sdougm 					 * the file must remain locked until
8664327Sdougm 					 * the merge is done in order to
8674327Sdougm 					 * prevent multiple startups from
8684327Sdougm 					 * changing the SMF repository at the
8694327Sdougm 					 * same time.  The first to get the
8704327Sdougm 					 * lock will make any changes before
8714327Sdougm 					 * the others can read the repository.
8724327Sdougm 					 */
8734327Sdougm 					prop = scf_simple_prop_get
8744327Sdougm 					    (handle->scfhandle->handle,
8754327Sdougm 					    (const char *)SA_SVC_FMRI_BASE
8764327Sdougm 					    ":default", "operation",
8774327Sdougm 					    "legacy-timestamp");
8784327Sdougm 					if (prop != NULL) {
8794327Sdougm 						char *i64;
8804327Sdougm 						i64 = GETPROP(prop);
8814327Sdougm 						if (i64 != NULL)
8824327Sdougm 							tval = strtoull(i64,
8834327Sdougm 							    NULL, 0);
8844327Sdougm 						if (CHECKTSTAMP(st, tval))
8854327Sdougm 							updatelegacy = B_TRUE;
8864327Sdougm 						scf_simple_prop_free(prop);
8874327Sdougm 					} else {
8884327Sdougm 						/*
8894327Sdougm 						 * We haven't set the
8904327Sdougm 						 * timestamp before so do it.
8914327Sdougm 						 */
8924327Sdougm 						updatelegacy = B_TRUE;
8934327Sdougm 					}
8944327Sdougm 				}
8954327Sdougm 				if (updatelegacy == B_FALSE) {
8964327Sdougm 					/* Don't need the lock anymore */
8974327Sdougm 					(void) lockf(lockfd, F_ULOCK, 0);
8984327Sdougm 					(void) close(lockfd);
8994327Sdougm 				}
9003973Sdougm 
9014327Sdougm 				/*
9024327Sdougm 				 * It is essential that the document tree and
9034327Sdougm 				 * the internal list of roots to handles be
9044327Sdougm 				 * setup before anything that might try to
9054327Sdougm 				 * create a new object is called. The document
9064327Sdougm 				 * tree is the combination of handle->doc and
9074327Sdougm 				 * handle->tree. This allows searches,
9084327Sdougm 				 * etc. when all you have is an object in the
9094327Sdougm 				 * tree.
9104327Sdougm 				 */
9114327Sdougm 				handle->doc = xmlNewDoc((xmlChar *)"1.0");
9124327Sdougm 				handle->tree = xmlNewNode(NULL,
9134327Sdougm 				    (xmlChar *)"sharecfg");
9144327Sdougm 				if (handle->doc != NULL &&
9154327Sdougm 				    handle->tree != NULL) {
9164327Sdougm 					xmlDocSetRootElement(handle->doc,
9174327Sdougm 					    handle->tree);
9184327Sdougm 					err = add_handle_for_root(handle->tree,
9194327Sdougm 					    handle);
9204327Sdougm 					if (err == SA_OK)
9214327Sdougm 						err = sa_get_config(
9224327Sdougm 						    handle->scfhandle,
9233973Sdougm 						    handle->tree, handle);
9244327Sdougm 				} else {
9254327Sdougm 					if (handle->doc != NULL)
9264327Sdougm 						xmlFreeDoc(handle->doc);
9274327Sdougm 					if (handle->tree != NULL)
9284327Sdougm 						xmlFreeNode(handle->tree);
9294327Sdougm 					err = SA_NO_MEMORY;
9304327Sdougm 				}
9313973Sdougm 
9324327Sdougm 				saunblocksigs(&old);
9333910Sdougm 
9344327Sdougm 				if (err != SA_OK) {
9354327Sdougm 					/*
9364327Sdougm 					 * If we couldn't add the tree handle
9374327Sdougm 					 * to the list, then things are going
9384327Sdougm 					 * to fail badly. Might as well undo
9394327Sdougm 					 * everything now and fail the
9404327Sdougm 					 * sa_init().
9414327Sdougm 					 */
9424327Sdougm 					sa_fini(handle);
9434327Sdougm 					return (NULL);
9444327Sdougm 				}
9453910Sdougm 
9464327Sdougm 				if (tval == 0) {
9474327Sdougm 					/*
9484327Sdougm 					 * first time so make sure
9494327Sdougm 					 * default is setup
9504327Sdougm 					 */
9514327Sdougm 					verifydefgroupopts(handle);
9524327Sdougm 				}
9533973Sdougm 
9544524Sdougm 				if (updatelegacy == B_TRUE) {
9554524Sdougm 					sablocksigs(&old);
9564524Sdougm 					getlegacyconfig((sa_handle_t)handle,
9574524Sdougm 					    SA_LEGACY_DFSTAB, &handle->tree);
9584524Sdougm 					if (stat(SA_LEGACY_DFSTAB, &st) >= 0)
9594524Sdougm 						set_legacy_timestamp(
9604524Sdougm 						    handle->tree,
9614524Sdougm 						    SA_LEGACY_DFSTAB,
9624524Sdougm 						    TSTAMP(st.st_ctim));
9634524Sdougm 					saunblocksigs(&old);
9644524Sdougm 					/*
9654524Sdougm 					 * Safe to unlock now to allow
9664524Sdougm 					 * others to run
9674524Sdougm 					 */
9684524Sdougm 					(void) lockf(lockfd, F_ULOCK, 0);
9694524Sdougm 					(void) close(lockfd);
9704524Sdougm 				}
9714524Sdougm 				legacy |= sa_get_zfs_shares(handle, "zfs");
9724524Sdougm 				legacy |= gettransients(handle, &handle->tree);
9734327Sdougm 			}
9744327Sdougm 		}
9753034Sdougm 	}
9763910Sdougm 	return ((sa_handle_t)handle);
9773034Sdougm }
9783034Sdougm 
9793034Sdougm /*
9803910Sdougm  * sa_fini(handle)
9813034Sdougm  *	Uninitialize the API structures including the configuration
9823218Sdougm  *	data structures and ZFS related data.
9833034Sdougm  */
9843034Sdougm 
9853034Sdougm void
9863910Sdougm sa_fini(sa_handle_t handle)
9873034Sdougm {
9883910Sdougm 	sa_handle_impl_t impl_handle = (sa_handle_impl_t)handle;
9893910Sdougm 
9903910Sdougm 	if (impl_handle != NULL) {
9913910Sdougm 		/*
9923910Sdougm 		 * Free the config trees and any other data structures
9933910Sdougm 		 * used in the handle.
9943910Sdougm 		 */
9953910Sdougm 		if (impl_handle->doc != NULL)
9963910Sdougm 			xmlFreeDoc(impl_handle->doc);
9973910Sdougm 		sa_scf_fini(impl_handle->scfhandle);
9983910Sdougm 		sa_zfs_fini(impl_handle);
9993910Sdougm 
10003910Sdougm 		/* Remove and free the entry in the global list. */
10013910Sdougm 		remove_handle_for_root(impl_handle->tree);
10023910Sdougm 
10033910Sdougm 		/* Make sure we free the handle */
10043910Sdougm 		free(impl_handle);
10053910Sdougm 
10063910Sdougm 		/*
10073910Sdougm 		 * If this was the last handle to release, unload the
10083910Sdougm 		 * plugins that were loaded.
10093910Sdougm 		 */
10103910Sdougm 		if (sa_global_handles == NULL)
10114327Sdougm 			(void) proto_plugin_fini();
10123910Sdougm 
10133034Sdougm 	}
10143034Sdougm }
10153034Sdougm 
10163034Sdougm /*
10173034Sdougm  * sa_get_protocols(char **protocol)
10183034Sdougm  *	Get array of protocols that are supported
10193034Sdougm  *	Returns pointer to an allocated and NULL terminated
10203034Sdougm  *	array of strings.  Caller must free.
10213034Sdougm  *	This really should be determined dynamically.
10223034Sdougm  *	If there aren't any defined, return -1.
10233034Sdougm  *	Use free() to return memory.
10243034Sdougm  */
10253034Sdougm 
10263034Sdougm int
10273034Sdougm sa_get_protocols(char ***protocols)
10283034Sdougm {
10293034Sdougm 	int numproto = -1;
10303034Sdougm 
10313034Sdougm 	if (protocols != NULL) {
10324327Sdougm 		struct sa_proto_plugin *plug;
10334327Sdougm 		for (numproto = 0, plug = sap_proto_list; plug != NULL;
10344327Sdougm 		    plug = plug->plugin_next) {
10354327Sdougm 			numproto++;
10364327Sdougm 		}
10373034Sdougm 
10384327Sdougm 		*protocols = calloc(numproto + 1,  sizeof (char *));
10394327Sdougm 		if (*protocols != NULL) {
10404327Sdougm 			int ret = 0;
10414327Sdougm 			for (plug = sap_proto_list; plug != NULL;
10424327Sdougm 			    plug = plug->plugin_next) {
10434327Sdougm 				/* faking for now */
10444327Sdougm 				(*protocols)[ret++] =
10454327Sdougm 				    plug->plugin_ops->sa_protocol;
10464327Sdougm 			}
10474327Sdougm 		} else {
10484327Sdougm 			numproto = -1;
10493034Sdougm 		}
10503034Sdougm 	}
10513034Sdougm 	return (numproto);
10523034Sdougm }
10533034Sdougm 
10543034Sdougm /*
10553034Sdougm  * find_group_by_name(node, group)
10563034Sdougm  *
10573034Sdougm  * search the XML document subtree specified by node to find the group
10583034Sdougm  * specified by group. Searching subtree allows subgroups to be
10593034Sdougm  * searched for.
10603034Sdougm  */
10613034Sdougm 
10623034Sdougm static xmlNodePtr
10633034Sdougm find_group_by_name(xmlNodePtr node, xmlChar *group)
10643034Sdougm {
10653034Sdougm 	xmlChar *name = NULL;
10663034Sdougm 
10673034Sdougm 	for (node = node->xmlChildrenNode; node != NULL;
10683034Sdougm 	    node = node->next) {
10694327Sdougm 		if (xmlStrcmp(node->name, (xmlChar *)"group") == 0) {
10704327Sdougm 			/* if no groupname, return the first found */
10714327Sdougm 			if (group == NULL)
10724327Sdougm 				break;
10734327Sdougm 			name = xmlGetProp(node, (xmlChar *)"name");
10744327Sdougm 			if (name != NULL && xmlStrcmp(name, group) == 0)
10754327Sdougm 				break;
10764327Sdougm 			if (name != NULL) {
10774327Sdougm 				xmlFree(name);
10784327Sdougm 				name = NULL;
10794327Sdougm 			}
10803034Sdougm 		}
10813034Sdougm 	}
10823034Sdougm 	if (name != NULL)
10834327Sdougm 		xmlFree(name);
10843034Sdougm 	return (node);
10853034Sdougm }
10863034Sdougm 
10873034Sdougm /*
10883034Sdougm  * sa_get_group(groupname)
10893034Sdougm  *	Return the "group" specified.  If groupname is NULL,
10903034Sdougm  *	return the first group of the list of groups.
10913034Sdougm  */
10923034Sdougm sa_group_t
10933910Sdougm sa_get_group(sa_handle_t handle, char *groupname)
10943034Sdougm {
10953034Sdougm 	xmlNodePtr node = NULL;
10963034Sdougm 	char *subgroup = NULL;
10973034Sdougm 	char *group = NULL;
10983910Sdougm 	sa_handle_impl_t impl_handle = (sa_handle_impl_t)handle;
10993034Sdougm 
11003910Sdougm 	if (impl_handle != NULL && impl_handle->tree != NULL) {
11014327Sdougm 		if (groupname != NULL) {
11024327Sdougm 			group = strdup(groupname);
11034345Sdougm 			if (group != NULL) {
11044345Sdougm 				subgroup = strchr(group, '/');
11054345Sdougm 				if (subgroup != NULL)
11064345Sdougm 					*subgroup++ = '\0';
11074345Sdougm 			}
11084327Sdougm 		}
11094345Sdougm 		/*
11104345Sdougm 		 * We want to find the, possibly, named group. If
11114345Sdougm 		 * group is not NULL, then lookup the name. If it is
11124345Sdougm 		 * NULL, we only do the find if groupname is also
11134345Sdougm 		 * NULL. This allows lookup of the "first" group in
11144345Sdougm 		 * the internal list.
11154345Sdougm 		 */
11164345Sdougm 		if (group != NULL || groupname == NULL)
11174345Sdougm 			node = find_group_by_name(impl_handle->tree,
11184345Sdougm 			    (xmlChar *)group);
11194345Sdougm 
11204327Sdougm 		/* if a subgroup, find it before returning */
11214327Sdougm 		if (subgroup != NULL && node != NULL)
11224327Sdougm 			node = find_group_by_name(node, (xmlChar *)subgroup);
11233034Sdougm 	}
11243034Sdougm 	if (node != NULL && (char *)group != NULL)
11254327Sdougm 		(void) sa_get_instance(impl_handle->scfhandle, (char *)group);
11263034Sdougm 	if (group != NULL)
11274327Sdougm 		free(group);
11283034Sdougm 	return ((sa_group_t)(node));
11293034Sdougm }
11303034Sdougm 
11313034Sdougm /*
11323034Sdougm  * sa_get_next_group(group)
11333034Sdougm  *	Return the "next" group after the specified group from
11343034Sdougm  *	the internal group list.  NULL if there are no more.
11353034Sdougm  */
11363034Sdougm sa_group_t
11373034Sdougm sa_get_next_group(sa_group_t group)
11383034Sdougm {
11393034Sdougm 	xmlNodePtr ngroup = NULL;
11403034Sdougm 	if (group != NULL) {
11414327Sdougm 		for (ngroup = ((xmlNodePtr)group)->next; ngroup != NULL;
11423034Sdougm 		    ngroup = ngroup->next) {
11434327Sdougm 			if (xmlStrcmp(ngroup->name, (xmlChar *)"group") == 0)
11444327Sdougm 				break;
11454327Sdougm 		}
11463034Sdougm 	}
11473034Sdougm 	return ((sa_group_t)ngroup);
11483034Sdougm }
11493034Sdougm 
11503034Sdougm /*
11513034Sdougm  * sa_get_share(group, sharepath)
11523034Sdougm  *	Return the share object for the share specified. The share
11533034Sdougm  *	must be in the specified group.  Return NULL if not found.
11543034Sdougm  */
11553034Sdougm sa_share_t
11563034Sdougm sa_get_share(sa_group_t group, char *sharepath)
11573034Sdougm {
11583034Sdougm 	xmlNodePtr node = NULL;
11593034Sdougm 	xmlChar *path;
11603034Sdougm 
11613034Sdougm 	/*
11623034Sdougm 	 * For future scalability, this should end up building a cache
11633034Sdougm 	 * since it will get called regularly by the mountd and info
11643034Sdougm 	 * services.
11653034Sdougm 	 */
11663034Sdougm 	if (group != NULL) {
11674327Sdougm 		for (node = ((xmlNodePtr)group)->children; node != NULL;
11683034Sdougm 		    node = node->next) {
11694327Sdougm 			if (xmlStrcmp(node->name, (xmlChar *)"share") == 0) {
11704327Sdougm 				if (sharepath == NULL) {
11714327Sdougm 					break;
11724327Sdougm 				} else {
11734327Sdougm 					/* is it the correct share? */
11744327Sdougm 					path = xmlGetProp(node,
11754327Sdougm 					    (xmlChar *)"path");
11764327Sdougm 					if (path != NULL &&
11774327Sdougm 					    xmlStrcmp(path,
11784327Sdougm 					    (xmlChar *)sharepath) == 0) {
11794327Sdougm 						xmlFree(path);
11804327Sdougm 						break;
11814327Sdougm 					}
11824327Sdougm 					xmlFree(path);
11834327Sdougm 				}
11843034Sdougm 			}
11853034Sdougm 		}
11863034Sdougm 	}
11873034Sdougm 	return ((sa_share_t)node);
11883034Sdougm }
11893034Sdougm 
11903034Sdougm /*
11913034Sdougm  * sa_get_next_share(share)
11923034Sdougm  *	Return the next share following the specified share
11933034Sdougm  *	from the internal list of shares. Returns NULL if there
11943034Sdougm  *	are no more shares.  The list is relative to the same
11953034Sdougm  *	group.
11963034Sdougm  */
11973034Sdougm sa_share_t
11983034Sdougm sa_get_next_share(sa_share_t share)
11993034Sdougm {
12003034Sdougm 	xmlNodePtr node = NULL;
12013034Sdougm 
12023034Sdougm 	if (share != NULL) {
12034327Sdougm 		for (node = ((xmlNodePtr)share)->next; node != NULL;
12043034Sdougm 		    node = node->next) {
12054327Sdougm 			if (xmlStrcmp(node->name, (xmlChar *)"share") == 0) {
12064327Sdougm 				break;
12074327Sdougm 			}
12083034Sdougm 		}
12093034Sdougm 	}
12103034Sdougm 	return ((sa_share_t)node);
12113034Sdougm }
12123034Sdougm 
12133034Sdougm /*
12143034Sdougm  * _sa_get_child_node(node, type)
12153034Sdougm  *
12163034Sdougm  * find the child node of the specified node that has "type". This is
12173034Sdougm  * used to implement several internal functions.
12183034Sdougm  */
12193034Sdougm 
12203034Sdougm static xmlNodePtr
12213034Sdougm _sa_get_child_node(xmlNodePtr node, xmlChar *type)
12223034Sdougm {
12233034Sdougm 	xmlNodePtr child;
12243034Sdougm 	for (child = node->xmlChildrenNode; child != NULL;
12253034Sdougm 	    child = child->next)
12264327Sdougm 		if (xmlStrcmp(child->name, type) == 0)
12274327Sdougm 			return (child);
12283034Sdougm 	return ((xmlNodePtr)NULL);
12293034Sdougm }
12303034Sdougm 
12313034Sdougm /*
12323034Sdougm  *  find_share(group, path)
12333034Sdougm  *
12343034Sdougm  * Search all the shares in the specified group for one that has the
12353034Sdougm  * specified path.
12363034Sdougm  */
12373034Sdougm 
12383034Sdougm static sa_share_t
12393034Sdougm find_share(sa_group_t group, char *sharepath)
12403034Sdougm {
12413034Sdougm 	sa_share_t share;
12423034Sdougm 	char *path;
12433034Sdougm 
12443034Sdougm 	for (share = sa_get_share(group, NULL); share != NULL;
12453034Sdougm 	    share = sa_get_next_share(share)) {
12464327Sdougm 		path = sa_get_share_attr(share, "path");
12474327Sdougm 		if (path != NULL && strcmp(path, sharepath) == 0) {
12484327Sdougm 			sa_free_attr_string(path);
12494327Sdougm 			break;
12504327Sdougm 		}
12514327Sdougm 		if (path != NULL)
12524327Sdougm 			sa_free_attr_string(path);
12533034Sdougm 	}
12543034Sdougm 	return (share);
12553034Sdougm }
12563034Sdougm 
12573034Sdougm /*
12583034Sdougm  * sa_get_sub_group(group)
12593034Sdougm  *
12603034Sdougm  * Get the first sub-group of group. The sa_get_next_group() function
12613034Sdougm  * can be used to get the rest. This is currently only used for ZFS
12623034Sdougm  * sub-groups but could be used to implement a more general mechanism.
12633034Sdougm  */
12643034Sdougm 
12653034Sdougm sa_group_t
12663034Sdougm sa_get_sub_group(sa_group_t group)
12673034Sdougm {
12683034Sdougm 	return ((sa_group_t)_sa_get_child_node((xmlNodePtr)group,
12694327Sdougm 	    (xmlChar *)"group"));
12703034Sdougm }
12713034Sdougm 
12723034Sdougm /*
12733034Sdougm  * sa_find_share(sharepath)
12743034Sdougm  *	Finds a share regardless of group.  In the future, this
12753034Sdougm  *	function should utilize a cache and hash table of some kind.
12763034Sdougm  *	The current assumption is that a path will only be shared
12773034Sdougm  *	once.  In the future, this may change as implementation of
12783034Sdougm  *	resource names comes into being.
12793034Sdougm  */
12803034Sdougm sa_share_t
12813910Sdougm sa_find_share(sa_handle_t handle, char *sharepath)
12823034Sdougm {
12833034Sdougm 	sa_group_t group;
12843034Sdougm 	sa_group_t zgroup;
12853034Sdougm 	sa_share_t share = NULL;
12863034Sdougm 	int done = 0;
12873034Sdougm 
12883910Sdougm 	for (group = sa_get_group(handle, NULL); group != NULL && !done;
12894327Sdougm 	    group = sa_get_next_group(group)) {
12904327Sdougm 		if (is_zfs_group(group)) {
12914327Sdougm 			for (zgroup =
12924327Sdougm 			    (sa_group_t)_sa_get_child_node((xmlNodePtr)group,
12934327Sdougm 			    (xmlChar *)"group");
12944327Sdougm 			    zgroup != NULL;
12954327Sdougm 			    zgroup = sa_get_next_group(zgroup)) {
12964327Sdougm 				share = find_share(zgroup, sharepath);
12974327Sdougm 				if (share != NULL)
12984327Sdougm 					break;
12994327Sdougm 			}
13004327Sdougm 		} else {
13014327Sdougm 			share = find_share(group, sharepath);
13024327Sdougm 		}
13034327Sdougm 		if (share != NULL)
13043034Sdougm 			break;
13053034Sdougm 	}
13063034Sdougm 	return (share);
13073034Sdougm }
13083034Sdougm 
13093034Sdougm /*
13103348Sdougm  *  sa_check_path(group, path, strictness)
13113034Sdougm  *
1312*5331Samw  * Check that path is a valid path relative to the group.  Currently,
13133034Sdougm  * we are ignoring the group and checking only the NFS rules. Later,
13143034Sdougm  * we may want to use the group to then check against the protocols
13153348Sdougm  * enabled on the group. The strictness values mean:
13163348Sdougm  * SA_CHECK_NORMAL == only check newpath against shares that are active
13173348Sdougm  * SA_CHECK_STRICT == check newpath against both active shares and those
13183348Sdougm  *		      stored in the repository
13193034Sdougm  */
13203034Sdougm 
13213034Sdougm int
13223348Sdougm sa_check_path(sa_group_t group, char *path, int strictness)
13233034Sdougm {
13243910Sdougm 	sa_handle_t handle;
13253910Sdougm 
13263910Sdougm 	handle = sa_find_group_handle(group);
13273910Sdougm 	return (validpath(handle, path, strictness));
13283034Sdougm }
13293034Sdougm 
13303034Sdougm /*
1331*5331Samw  * mark_excluded_protos(group, share, flags)
13323034Sdougm  *
1333*5331Samw  * Walk through all the protocols enabled for the group and check to
1334*5331Samw  * see if the share has any of them should be in the exclude list
1335*5331Samw  * based on the featureset of the protocol. If there are any, add the
1336*5331Samw  * "exclude" property to the share.
1337*5331Samw  */
1338*5331Samw static void
1339*5331Samw mark_excluded_protos(sa_group_t group, xmlNodePtr share, uint64_t flags)
1340*5331Samw {
1341*5331Samw 	sa_optionset_t optionset;
1342*5331Samw 	char exclude_list[SA_STRSIZE];
1343*5331Samw 	char *sep = "";
1344*5331Samw 
1345*5331Samw 	exclude_list[0] = '\0';
1346*5331Samw 	for (optionset = sa_get_optionset(group, NULL);
1347*5331Samw 	    optionset != NULL;
1348*5331Samw 	    optionset = sa_get_next_optionset(optionset)) {
1349*5331Samw 		char *value;
1350*5331Samw 		uint64_t features;
1351*5331Samw 		value = sa_get_optionset_attr(optionset, "type");
1352*5331Samw 		if (value == NULL)
1353*5331Samw 			continue;
1354*5331Samw 		features = sa_proto_get_featureset(value);
1355*5331Samw 		sa_free_attr_string(value);
1356*5331Samw 		if (!(features & flags)) {
1357*5331Samw 			(void) strlcat(exclude_list, sep,
1358*5331Samw 			    sizeof (exclude_list));
1359*5331Samw 			(void) strlcat(exclude_list, value,
1360*5331Samw 			    sizeof (exclude_list));
1361*5331Samw 			sep = ",";
1362*5331Samw 		}
1363*5331Samw 	}
1364*5331Samw 	if (exclude_list[0] != '\0')
1365*5331Samw 		xmlSetProp(share, (xmlChar *)"exclude",
1366*5331Samw 		    (xmlChar *)exclude_list);
1367*5331Samw }
1368*5331Samw 
1369*5331Samw /*
1370*5331Samw  * get_all_features(group)
1371*5331Samw  *
1372*5331Samw  * Walk through all the protocols on the group and collect all
1373*5331Samw  * possible enabled features. This is the OR of all the featuresets.
1374*5331Samw  */
1375*5331Samw static uint64_t
1376*5331Samw get_all_features(sa_group_t group)
1377*5331Samw {
1378*5331Samw 	sa_optionset_t optionset;
1379*5331Samw 	uint64_t features = 0;
1380*5331Samw 
1381*5331Samw 	for (optionset = sa_get_optionset(group, NULL);
1382*5331Samw 	    optionset != NULL;
1383*5331Samw 	    optionset = sa_get_next_optionset(optionset)) {
1384*5331Samw 		char *value;
1385*5331Samw 		value = sa_get_optionset_attr(optionset, "type");
1386*5331Samw 		if (value == NULL)
1387*5331Samw 			continue;
1388*5331Samw 		features |= sa_proto_get_featureset(value);
1389*5331Samw 		sa_free_attr_string(value);
1390*5331Samw 	}
1391*5331Samw 	return (features);
1392*5331Samw }
1393*5331Samw 
1394*5331Samw 
1395*5331Samw /*
1396*5331Samw  * _sa_add_share(group, sharepath, persist, *error, flags)
1397*5331Samw  *
1398*5331Samw  * Common code for all types of add_share. sa_add_share() is the
13993034Sdougm  * public API, we also need to be able to do this when parsing legacy
14003034Sdougm  * files and construction of the internal configuration while
1401*5331Samw  * extracting config info from SMF. "flags" indicates if some
1402*5331Samw  * protocols need relaxed rules while other don't. These values are
1403*5331Samw  * the featureset values defined in libshare.h.
14043034Sdougm  */
14053034Sdougm 
14063034Sdougm sa_share_t
1407*5331Samw _sa_add_share(sa_group_t group, char *sharepath, int persist, int *error,
1408*5331Samw     uint64_t flags)
14093034Sdougm {
14103034Sdougm 	xmlNodePtr node = NULL;
14113034Sdougm 	int err;
14123034Sdougm 
14133034Sdougm 	err  = SA_OK; /* assume success */
14143034Sdougm 
14154327Sdougm 	node = xmlNewChild((xmlNodePtr)group, NULL, (xmlChar *)"share", NULL);
1416*5331Samw 	if (node == NULL) {
1417*5331Samw 		if (error != NULL)
1418*5331Samw 			*error = SA_NO_MEMORY;
1419*5331Samw 		return (node);
1420*5331Samw 	}
1421*5331Samw 
1422*5331Samw 	xmlSetProp(node, (xmlChar *)"path", (xmlChar *)sharepath);
1423*5331Samw 	xmlSetProp(node, (xmlChar *)"type",
1424*5331Samw 	    persist ? (xmlChar *)"persist" : (xmlChar *)"transient");
1425*5331Samw 	if (flags != 0)
1426*5331Samw 		mark_excluded_protos(group, node, flags);
1427*5331Samw 	if (persist != SA_SHARE_TRANSIENT) {
1428*5331Samw 		/*
1429*5331Samw 		 * persistent shares come in two flavors: SMF and
1430*5331Samw 		 * ZFS. Sort this one out based on target group and
1431*5331Samw 		 * path type. Both NFS and SMB are supported. First,
1432*5331Samw 		 * check to see if the protocol is enabled on the
1433*5331Samw 		 * subgroup and then setup the share appropriately.
1434*5331Samw 		 */
1435*5331Samw 		if (sa_group_is_zfs(group) &&
1436*5331Samw 		    sa_path_is_zfs(sharepath)) {
1437*5331Samw 			if (sa_get_optionset(group, "nfs") != NULL)
14384327Sdougm 				err = sa_zfs_set_sharenfs(group, sharepath, 1);
1439*5331Samw 			else if (sa_get_optionset(group, "smb") != NULL)
1440*5331Samw 				err = sa_zfs_set_sharesmb(group, sharepath, 1);
1441*5331Samw 		} else {
1442*5331Samw 			sa_handle_impl_t impl_handle;
1443*5331Samw 			impl_handle =
1444*5331Samw 			    (sa_handle_impl_t)sa_find_group_handle(group);
1445*5331Samw 			if (impl_handle != NULL) {
1446*5331Samw 				err = sa_commit_share(impl_handle->scfhandle,
1447*5331Samw 				    group, (sa_share_t)node);
14484327Sdougm 			} else {
1449*5331Samw 				err = SA_SYSTEM_ERR;
14504327Sdougm 			}
14513034Sdougm 		}
14523034Sdougm 	}
1453*5331Samw 	if (err == SA_NO_PERMISSION && persist & SA_SHARE_PARSER)
1454*5331Samw 		/* called by the dfstab parser so could be a show */
1455*5331Samw 		err = SA_OK;
1456*5331Samw 
1457*5331Samw 	if (err != SA_OK) {
1458*5331Samw 		/*
1459*5331Samw 		 * we couldn't commit to the repository so undo
1460*5331Samw 		 * our internal state to reflect reality.
1461*5331Samw 		 */
1462*5331Samw 		xmlUnlinkNode(node);
1463*5331Samw 		xmlFreeNode(node);
1464*5331Samw 		node = NULL;
1465*5331Samw 	}
1466*5331Samw 
14673034Sdougm 	if (error != NULL)
14684327Sdougm 		*error = err;
1469*5331Samw 
14703034Sdougm 	return (node);
14713034Sdougm }
14723034Sdougm 
14733034Sdougm /*
14743034Sdougm  * sa_add_share(group, sharepath, persist, *error)
14753034Sdougm  *
14763034Sdougm  *	Add a new share object to the specified group.  The share will
14773034Sdougm  *	have the specified sharepath and will only be constructed if
14783034Sdougm  *	it is a valid path to be shared.  NULL is returned on error
14793034Sdougm  *	and a detailed error value will be returned via the error
14803034Sdougm  *	pointer.
14813034Sdougm  */
14823034Sdougm sa_share_t
14833034Sdougm sa_add_share(sa_group_t group, char *sharepath, int persist, int *error)
14843034Sdougm {
14853034Sdougm 	xmlNodePtr node = NULL;
14863348Sdougm 	int strictness = SA_CHECK_NORMAL;
14873910Sdougm 	sa_handle_t handle;
1488*5331Samw 	uint64_t special = 0;
1489*5331Samw 	uint64_t features;
14903348Sdougm 
14913348Sdougm 	/*
14923348Sdougm 	 * If the share is to be permanent, use strict checking so a
14933348Sdougm 	 * bad config doesn't get created. Transient shares only need
14943348Sdougm 	 * to check against the currently active
14953348Sdougm 	 * shares. SA_SHARE_PARSER is a modifier used internally to
14963348Sdougm 	 * indicate that we are being called by the dfstab parser and
14973348Sdougm 	 * that we need strict checking in all cases. Normally persist
14983348Sdougm 	 * is in integer value but SA_SHARE_PARSER may be or'd into
14993348Sdougm 	 * it as an override.
15003348Sdougm 	 */
15013348Sdougm 	if (persist & SA_SHARE_PARSER || persist == SA_SHARE_PERMANENT)
15024327Sdougm 		strictness = SA_CHECK_STRICT;
15033034Sdougm 
15043910Sdougm 	handle = sa_find_group_handle(group);
15053910Sdougm 
1506*5331Samw 	/*
1507*5331Samw 	 * need to determine if the share is valid. The rules are:
1508*5331Samw 	 *	- The path must not already exist
1509*5331Samw 	 *	- The path must not be a subdir or parent dir of an
1510*5331Samw 	 *	  existing path unless at least one protocol allows it.
1511*5331Samw 	 * The sub/parent check is done in sa_check_path().
1512*5331Samw 	 */
1513*5331Samw 
1514*5331Samw 	if (sa_find_share(handle, sharepath) == NULL) {
1515*5331Samw 		*error = sa_check_path(group, sharepath, strictness);
1516*5331Samw 		features = get_all_features(group);
1517*5331Samw 		switch (*error) {
1518*5331Samw 		case SA_PATH_IS_SUBDIR:
1519*5331Samw 			if (features & SA_FEATURE_ALLOWSUBDIRS)
1520*5331Samw 				special |= SA_FEATURE_ALLOWSUBDIRS;
1521*5331Samw 			break;
1522*5331Samw 		case SA_PATH_IS_PARENTDIR:
1523*5331Samw 			if (features & SA_FEATURE_ALLOWPARDIRS)
1524*5331Samw 				special |= SA_FEATURE_ALLOWPARDIRS;
1525*5331Samw 			break;
1526*5331Samw 		}
1527*5331Samw 		if (*error == SA_OK || special != SA_FEATURE_NONE)
1528*5331Samw 			node = _sa_add_share(group, sharepath, persist,
1529*5331Samw 			    error, special);
1530*5331Samw 	} else {
1531*5331Samw 		*error = SA_DUPLICATE_NAME;
15323034Sdougm 	}
15333034Sdougm 
15343034Sdougm 	return ((sa_share_t)node);
15353034Sdougm }
15363034Sdougm 
15373034Sdougm /*
15383034Sdougm  * sa_enable_share(share, protocol)
15393034Sdougm  *	Enable the specified share to the specified protocol.
15403034Sdougm  *	If protocol is NULL, then all protocols.
15413034Sdougm  */
15423034Sdougm int
15433034Sdougm sa_enable_share(sa_share_t share, char *protocol)
15443034Sdougm {
15453034Sdougm 	char *sharepath;
15463034Sdougm 	struct stat st;
1547*5331Samw 	int err = SA_OK;
1548*5331Samw 	int ret;
15493034Sdougm 
15503034Sdougm 	sharepath = sa_get_share_attr(share, "path");
1551*5331Samw 	if (sharepath == NULL)
1552*5331Samw 		return (SA_NO_MEMORY);
15533034Sdougm 	if (stat(sharepath, &st) < 0) {
15544327Sdougm 		err = SA_NO_SUCH_PATH;
15553034Sdougm 	} else {
15564327Sdougm 		/* tell the server about the share */
15574327Sdougm 		if (protocol != NULL) {
1558*5331Samw 			if (excluded_protocol(share, protocol))
1559*5331Samw 				goto done;
1560*5331Samw 
15614327Sdougm 			/* lookup protocol specific handler */
15624327Sdougm 			err = sa_proto_share(protocol, share);
15634327Sdougm 			if (err == SA_OK)
1564*5331Samw 				(void) sa_set_share_attr(share,
1565*5331Samw 				    "shared", "true");
15664327Sdougm 		} else {
1567*5331Samw 			/* Tell all protocols about the share */
1568*5331Samw 			sa_group_t group;
1569*5331Samw 			sa_optionset_t optionset;
1570*5331Samw 
1571*5331Samw 			group = sa_get_parent_group(share);
1572*5331Samw 
1573*5331Samw 			for (optionset = sa_get_optionset(group, NULL);
1574*5331Samw 			    optionset != NULL;
1575*5331Samw 			    optionset = sa_get_next_optionset(optionset)) {
1576*5331Samw 				char *proto;
1577*5331Samw 				proto = sa_get_optionset_attr(optionset,
1578*5331Samw 				    "type");
1579*5331Samw 				if (proto != NULL) {
1580*5331Samw 					if (!excluded_protocol(share, proto)) {
1581*5331Samw 						ret = sa_proto_share(proto,
1582*5331Samw 						    share);
1583*5331Samw 						if (ret != SA_OK)
1584*5331Samw 							err = ret;
1585*5331Samw 					}
1586*5331Samw 					sa_free_attr_string(proto);
1587*5331Samw 				}
1588*5331Samw 			}
15894327Sdougm 			(void) sa_set_share_attr(share, "shared", "true");
15904327Sdougm 		}
15913034Sdougm 	}
1592*5331Samw done:
15933034Sdougm 	if (sharepath != NULL)
15944327Sdougm 		sa_free_attr_string(sharepath);
15953034Sdougm 	return (err);
15963034Sdougm }
15973034Sdougm 
15983034Sdougm /*
15993034Sdougm  * sa_disable_share(share, protocol)
1600*5331Samw  *	Disable the specified share to the specified protocol.  If
1601*5331Samw  *	protocol is NULL, then all protocols that are enabled for the
1602*5331Samw  *	share should be disabled.
16033034Sdougm  */
16043034Sdougm int
16053034Sdougm sa_disable_share(sa_share_t share, char *protocol)
16063034Sdougm {
16073034Sdougm 	char *path;
1608*5331Samw 	int err = SA_OK;
16093034Sdougm 	int ret = SA_OK;
16103034Sdougm 
16113034Sdougm 	path = sa_get_share_attr(share, "path");
16123034Sdougm 
16133034Sdougm 	if (protocol != NULL) {
16144543Smarks 		ret = sa_proto_unshare(share, protocol, path);
16153034Sdougm 	} else {
16164327Sdougm 		/* need to do all protocols */
1617*5331Samw 		sa_group_t group;
1618*5331Samw 		sa_optionset_t optionset;
1619*5331Samw 
1620*5331Samw 		group = sa_get_parent_group(share);
1621*5331Samw 
1622*5331Samw 		/* Tell all protocols about the share */
1623*5331Samw 		for (optionset = sa_get_optionset(group, NULL);
1624*5331Samw 		    optionset != NULL;
1625*5331Samw 		    optionset = sa_get_next_optionset(optionset)) {
1626*5331Samw 			char *proto;
1627*5331Samw 
1628*5331Samw 			proto = sa_get_optionset_attr(optionset, "type");
1629*5331Samw 			if (proto != NULL) {
1630*5331Samw 				err = sa_proto_unshare(share, proto, path);
1631*5331Samw 				if (err != SA_OK)
1632*5331Samw 					ret = err;
1633*5331Samw 				sa_free_attr_string(proto);
1634*5331Samw 			}
1635*5331Samw 		}
16363034Sdougm 	}
16373034Sdougm 	if (ret == SA_OK)
16383034Sdougm 		(void) sa_set_share_attr(share, "shared", NULL);
16393034Sdougm 	if (path != NULL)
16404327Sdougm 		sa_free_attr_string(path);
16413034Sdougm 	return (ret);
16423034Sdougm }
16433034Sdougm 
16443034Sdougm /*
16453034Sdougm  * sa_remove_share(share)
16463034Sdougm  *
16473034Sdougm  * remove the specified share from its containing group.
16483034Sdougm  * Remove from the SMF or ZFS configuration space.
16493034Sdougm  */
16503034Sdougm 
16513034Sdougm int
16523034Sdougm sa_remove_share(sa_share_t share)
16533034Sdougm {
16543034Sdougm 	sa_group_t group;
16553034Sdougm 	int ret = SA_OK;
16563034Sdougm 	char *type;
16573034Sdougm 	int transient = 0;
16583034Sdougm 	char *groupname;
16593034Sdougm 	char *zfs;
16603034Sdougm 
16613034Sdougm 	type = sa_get_share_attr(share, "type");
16623034Sdougm 	group = sa_get_parent_group(share);
16633034Sdougm 	zfs = sa_get_group_attr(group, "zfs");
16643034Sdougm 	groupname = sa_get_group_attr(group, "name");
16653034Sdougm 	if (type != NULL && strcmp(type, "persist") != 0)
16664327Sdougm 		transient = 1;
16673034Sdougm 	if (type != NULL)
16684327Sdougm 		sa_free_attr_string(type);
16693034Sdougm 
16703034Sdougm 	/* remove the node from its group then free the memory */
16713034Sdougm 
16723034Sdougm 	/*
16733034Sdougm 	 * need to test if "busy"
16743034Sdougm 	 */
16753034Sdougm 	/* only do SMF action if permanent */
16763034Sdougm 	if (!transient || zfs != NULL) {
16774327Sdougm 		/* remove from legacy dfstab as well as possible SMF */
1678*5331Samw 		ret = sa_delete_legacy(share, NULL);
16794327Sdougm 		if (ret == SA_OK) {
16804327Sdougm 			if (!sa_group_is_zfs(group)) {
16814327Sdougm 				sa_handle_impl_t impl_handle;
16824327Sdougm 				impl_handle = (sa_handle_impl_t)
16834327Sdougm 				    sa_find_group_handle(group);
16844327Sdougm 				if (impl_handle != NULL) {
16854327Sdougm 					ret = sa_delete_share(
16864327Sdougm 					    impl_handle->scfhandle, group,
16874327Sdougm 					    share);
16884327Sdougm 				} else {
16894327Sdougm 					ret = SA_SYSTEM_ERR;
16904327Sdougm 				}
16914327Sdougm 			} else {
16924327Sdougm 				char *sharepath = sa_get_share_attr(share,
16934327Sdougm 				    "path");
16944327Sdougm 				if (sharepath != NULL) {
16954327Sdougm 					ret = sa_zfs_set_sharenfs(group,
16964327Sdougm 					    sharepath, 0);
16974327Sdougm 					sa_free_attr_string(sharepath);
16984327Sdougm 				}
16994327Sdougm 			}
17003034Sdougm 		}
17013034Sdougm 	}
17023034Sdougm 	if (groupname != NULL)
17034327Sdougm 		sa_free_attr_string(groupname);
17043034Sdougm 	if (zfs != NULL)
17054327Sdougm 		sa_free_attr_string(zfs);
17063034Sdougm 
17073034Sdougm 	xmlUnlinkNode((xmlNodePtr)share);
17083034Sdougm 	xmlFreeNode((xmlNodePtr)share);
17093034Sdougm 	return (ret);
17103034Sdougm }
17113034Sdougm 
17123034Sdougm /*
17133034Sdougm  * sa_move_share(group, share)
17143034Sdougm  *
17153034Sdougm  * move the specified share to the specified group.  Update SMF
17163034Sdougm  * appropriately.
17173034Sdougm  */
17183034Sdougm 
17193034Sdougm int
17203034Sdougm sa_move_share(sa_group_t group, sa_share_t share)
17213034Sdougm {
17223034Sdougm 	sa_group_t oldgroup;
17233034Sdougm 	int ret = SA_OK;
17243034Sdougm 
17253034Sdougm 	/* remove the node from its group then free the memory */
17263034Sdougm 
17273034Sdougm 	oldgroup = sa_get_parent_group(share);
17283034Sdougm 	if (oldgroup != group) {
17294327Sdougm 		sa_handle_impl_t impl_handle;
17304327Sdougm 		xmlUnlinkNode((xmlNodePtr)share);
17313034Sdougm 		/*
17324327Sdougm 		 * now that the share isn't in its old group, add to
17334327Sdougm 		 * the new one
17343034Sdougm 		 */
17354327Sdougm 		xmlAddChild((xmlNodePtr)group, (xmlNodePtr)share);
17364327Sdougm 		/* need to deal with SMF */
17374327Sdougm 		impl_handle = (sa_handle_impl_t)sa_find_group_handle(group);
17384327Sdougm 		if (impl_handle != NULL) {
17394327Sdougm 			/*
17404327Sdougm 			 * need to remove from old group first and then add to
17414327Sdougm 			 * new group. Ideally, we would do the other order but
17424327Sdougm 			 * need to avoid having the share in two groups at the
17434327Sdougm 			 * same time.
17444327Sdougm 			 */
17454327Sdougm 			ret = sa_delete_share(impl_handle->scfhandle, oldgroup,
17464327Sdougm 			    share);
17474327Sdougm 			if (ret == SA_OK)
17484327Sdougm 				ret = sa_commit_share(impl_handle->scfhandle,
17494327Sdougm 				    group, share);
17504327Sdougm 		} else {
17514327Sdougm 			ret = SA_SYSTEM_ERR;
17524327Sdougm 		}
17533034Sdougm 	}
17543034Sdougm 	return (ret);
17553034Sdougm }
17563034Sdougm 
17573034Sdougm /*
17583034Sdougm  * sa_get_parent_group(share)
17593034Sdougm  *
1760*5331Samw  * Return the containing group for the share. If a group was actually
17613034Sdougm  * passed in, we don't want a parent so return NULL.
17623034Sdougm  */
17633034Sdougm 
17643034Sdougm sa_group_t
17653034Sdougm sa_get_parent_group(sa_share_t share)
17663034Sdougm {
17673034Sdougm 	xmlNodePtr node = NULL;
17683034Sdougm 	if (share != NULL) {
17694327Sdougm 		node = ((xmlNodePtr)share)->parent;
17703034Sdougm 		/*
17713034Sdougm 		 * make sure parent is a group and not sharecfg since
17723034Sdougm 		 * we may be cheating and passing in a group.
17733034Sdougm 		 * Eventually, groups of groups might come into being.
17743034Sdougm 		 */
17754327Sdougm 		if (node == NULL ||
17764327Sdougm 		    xmlStrcmp(node->name, (xmlChar *)"sharecfg") == 0)
17774327Sdougm 			node = NULL;
17783034Sdougm 	}
17793034Sdougm 	return ((sa_group_t)node);
17803034Sdougm }
17813034Sdougm 
17823034Sdougm /*
17833910Sdougm  * _sa_create_group(impl_handle, groupname)
17843034Sdougm  *
17853034Sdougm  * Create a group in the document. The caller will need to deal with
17863034Sdougm  * configuration store and activation.
17873034Sdougm  */
17883034Sdougm 
17893034Sdougm sa_group_t
17903910Sdougm _sa_create_group(sa_handle_impl_t impl_handle, char *groupname)
17913034Sdougm {
17923034Sdougm 	xmlNodePtr node = NULL;
17933034Sdougm 
17943034Sdougm 	if (sa_valid_group_name(groupname)) {
17954327Sdougm 		node = xmlNewChild(impl_handle->tree, NULL, (xmlChar *)"group",
17964327Sdougm 		    NULL);
17974327Sdougm 		if (node != NULL) {
17984327Sdougm 			xmlSetProp(node, (xmlChar *)"name",
17994327Sdougm 			    (xmlChar *)groupname);
18004327Sdougm 			xmlSetProp(node, (xmlChar *)"state",
18014327Sdougm 			    (xmlChar *)"enabled");
18024327Sdougm 		}
18033034Sdougm 	}
18043034Sdougm 	return ((sa_group_t)node);
18053034Sdougm }
18063034Sdougm 
18073034Sdougm /*
18083034Sdougm  * _sa_create_zfs_group(group, groupname)
18093034Sdougm  *
18103034Sdougm  * Create a ZFS subgroup under the specified group. This may
18113034Sdougm  * eventually form the basis of general sub-groups, but is currently
18123034Sdougm  * restricted to ZFS.
18133034Sdougm  */
18143034Sdougm sa_group_t
18153034Sdougm _sa_create_zfs_group(sa_group_t group, char *groupname)
18163034Sdougm {
18173034Sdougm 	xmlNodePtr node = NULL;
18183034Sdougm 
18194327Sdougm 	node = xmlNewChild((xmlNodePtr)group, NULL, (xmlChar *)"group", NULL);
18203034Sdougm 	if (node != NULL) {
18213034Sdougm 		xmlSetProp(node, (xmlChar *)"name", (xmlChar *)groupname);
18223034Sdougm 		xmlSetProp(node, (xmlChar *)"state", (xmlChar *)"enabled");
18233034Sdougm 	}
18243034Sdougm 
18253034Sdougm 	return ((sa_group_t)node);
18263034Sdougm }
18273034Sdougm 
18283034Sdougm /*
18293034Sdougm  * sa_create_group(groupname, *error)
18303034Sdougm  *
18313034Sdougm  * Create a new group with groupname.  Need to validate that it is a
18323034Sdougm  * legal name for SMF and the construct the SMF service instance of
18333034Sdougm  * svc:/network/shares/group to implement the group. All necessary
18343034Sdougm  * operational properties must be added to the group at this point
18353034Sdougm  * (via the SMF transaction model).
18363034Sdougm  */
18373034Sdougm sa_group_t
18383910Sdougm sa_create_group(sa_handle_t handle, char *groupname, int *error)
18393034Sdougm {
18403034Sdougm 	xmlNodePtr node = NULL;
18413034Sdougm 	sa_group_t group;
18423034Sdougm 	int ret;
18434327Sdougm 	char rbacstr[SA_STRSIZE];
18443910Sdougm 	sa_handle_impl_t impl_handle = (sa_handle_impl_t)handle;
18453034Sdougm 
18463034Sdougm 	ret = SA_OK;
18473034Sdougm 
18483910Sdougm 	if (impl_handle == NULL || impl_handle->scfhandle == NULL) {
18494327Sdougm 		ret = SA_SYSTEM_ERR;
18504327Sdougm 		goto err;
18513034Sdougm 	}
18523034Sdougm 
18533910Sdougm 	group = sa_get_group(handle, groupname);
18543034Sdougm 	if (group != NULL) {
18554327Sdougm 		ret = SA_DUPLICATE_NAME;
18563034Sdougm 	} else {
18574327Sdougm 		if (sa_valid_group_name(groupname)) {
18584327Sdougm 			node = xmlNewChild(impl_handle->tree, NULL,
18594327Sdougm 			    (xmlChar *)"group", NULL);
18604327Sdougm 			if (node != NULL) {
18614327Sdougm 				xmlSetProp(node, (xmlChar *)"name",
18624327Sdougm 				    (xmlChar *)groupname);
18634327Sdougm 				/* default to the group being enabled */
18644327Sdougm 				xmlSetProp(node, (xmlChar *)"state",
18654327Sdougm 				    (xmlChar *)"enabled");
18664327Sdougm 				ret = sa_create_instance(impl_handle->scfhandle,
18674327Sdougm 				    groupname);
18684327Sdougm 				if (ret == SA_OK) {
18694327Sdougm 					ret = sa_start_transaction(
18704327Sdougm 					    impl_handle->scfhandle,
18714327Sdougm 					    "operation");
18724327Sdougm 				}
18734327Sdougm 				if (ret == SA_OK) {
18744327Sdougm 					ret = sa_set_property(
18754327Sdougm 					    impl_handle->scfhandle,
18764327Sdougm 					    "state", "enabled");
18774327Sdougm 					if (ret == SA_OK) {
18784327Sdougm 						ret = sa_end_transaction(
18794327Sdougm 						    impl_handle->scfhandle);
18804327Sdougm 					} else {
18814327Sdougm 						sa_abort_transaction(
18824327Sdougm 						    impl_handle->scfhandle);
18834327Sdougm 					}
18844327Sdougm 				}
18854327Sdougm 				if (ret == SA_OK) {
18864327Sdougm 					/* initialize the RBAC strings */
18874327Sdougm 					ret = sa_start_transaction(
18884327Sdougm 					    impl_handle->scfhandle,
18894327Sdougm 					    "general");
18904327Sdougm 					if (ret == SA_OK) {
18914327Sdougm 						(void) snprintf(rbacstr,
18924327Sdougm 						    sizeof (rbacstr), "%s.%s",
18934327Sdougm 						    SA_RBAC_MANAGE, groupname);
18944327Sdougm 						ret = sa_set_property(
18954327Sdougm 						    impl_handle->scfhandle,
18963034Sdougm 						    "action_authorization",
18973034Sdougm 						    rbacstr);
18984327Sdougm 					}
18994327Sdougm 					if (ret == SA_OK) {
19004327Sdougm 						(void) snprintf(rbacstr,
19014327Sdougm 						    sizeof (rbacstr), "%s.%s",
19024327Sdougm 						    SA_RBAC_VALUE, groupname);
19034327Sdougm 						ret = sa_set_property(
19044327Sdougm 						    impl_handle->scfhandle,
19053034Sdougm 						    "value_authorization",
19063034Sdougm 						    rbacstr);
19074327Sdougm 					}
19084327Sdougm 					if (ret == SA_OK) {
19094327Sdougm 						ret = sa_end_transaction(
19104327Sdougm 						    impl_handle->scfhandle);
19114327Sdougm 					} else {
19124327Sdougm 						sa_abort_transaction(
19134327Sdougm 						    impl_handle->scfhandle);
19144327Sdougm 					}
19154327Sdougm 				}
19164327Sdougm 				if (ret != SA_OK) {
19174327Sdougm 					/*
19184327Sdougm 					 * Couldn't commit the group
19194327Sdougm 					 * so we need to undo
19204327Sdougm 					 * internally.
19214327Sdougm 					 */
19224327Sdougm 					xmlUnlinkNode(node);
19234327Sdougm 					xmlFreeNode(node);
19244327Sdougm 					node = NULL;
19254327Sdougm 				}
19263034Sdougm 			} else {
19274327Sdougm 				ret = SA_NO_MEMORY;
19283034Sdougm 			}
19293034Sdougm 		} else {
19304327Sdougm 			ret = SA_INVALID_NAME;
19313034Sdougm 		}
19323034Sdougm 	}
19333034Sdougm err:
19343034Sdougm 	if (error != NULL)
19354327Sdougm 		*error = ret;
19363034Sdougm 	return ((sa_group_t)node);
19373034Sdougm }
19383034Sdougm 
19393034Sdougm /*
19403034Sdougm  * sa_remove_group(group)
19413034Sdougm  *
19423034Sdougm  * Remove the specified group. This deletes from the SMF repository.
19433034Sdougm  * All property groups and properties are removed.
19443034Sdougm  */
19453034Sdougm 
19463034Sdougm int
19473034Sdougm sa_remove_group(sa_group_t group)
19483034Sdougm {
19493034Sdougm 	char *name;
19503034Sdougm 	int ret = SA_OK;
19513910Sdougm 	sa_handle_impl_t impl_handle;
19523034Sdougm 
19533910Sdougm 	impl_handle = (sa_handle_impl_t)sa_find_group_handle(group);
19543910Sdougm 	if (impl_handle != NULL) {
19554327Sdougm 		name = sa_get_group_attr(group, "name");
19564327Sdougm 		if (name != NULL) {
19574327Sdougm 			ret = sa_delete_instance(impl_handle->scfhandle, name);
19584327Sdougm 			sa_free_attr_string(name);
19594327Sdougm 		}
19604327Sdougm 		xmlUnlinkNode((xmlNodePtr)group); /* make sure unlinked */
19614327Sdougm 		xmlFreeNode((xmlNodePtr)group);   /* now it is gone */
19623910Sdougm 	} else {
19634327Sdougm 		ret = SA_SYSTEM_ERR;
19643034Sdougm 	}
19653034Sdougm 	return (ret);
19663034Sdougm }
19673034Sdougm 
19683034Sdougm /*
19693034Sdougm  * sa_update_config()
19703034Sdougm  *
19713034Sdougm  * Used to update legacy files that need to be updated in bulk
19723034Sdougm  * Currently, this is a placeholder and will go away in a future
19733034Sdougm  * release.
19743034Sdougm  */
19753034Sdougm 
19763034Sdougm int
19773910Sdougm sa_update_config(sa_handle_t handle)
19783034Sdougm {
19793034Sdougm 	/*
19803034Sdougm 	 * do legacy files first so we can tell when they change.
19813034Sdougm 	 * This will go away when we start updating individual records
19823034Sdougm 	 * rather than the whole file.
19833034Sdougm 	 */
19843910Sdougm 	update_legacy_config(handle);
19853034Sdougm 	return (SA_OK);
19863034Sdougm }
19873034Sdougm 
19883034Sdougm /*
19893034Sdougm  * get_node_attr(node, tag)
19903034Sdougm  *
1991*5331Samw  * Get the specified tag(attribute) if it exists on the node.  This is
19923034Sdougm  * used internally by a number of attribute oriented functions.
19933034Sdougm  */
19943034Sdougm 
19953034Sdougm static char *
19963034Sdougm get_node_attr(void *nodehdl, char *tag)
19973034Sdougm {
19983034Sdougm 	xmlNodePtr node = (xmlNodePtr)nodehdl;
19993034Sdougm 	xmlChar *name = NULL;
20003034Sdougm 
20014327Sdougm 	if (node != NULL)
20023034Sdougm 		name = xmlGetProp(node, (xmlChar *)tag);
20033034Sdougm 	return ((char *)name);
20043034Sdougm }
20053034Sdougm 
20063034Sdougm /*
20073034Sdougm  * get_node_attr(node, tag)
20083034Sdougm  *
2009*5331Samw  * Set the specified tag(attribute) to the specified value This is
20103034Sdougm  * used internally by a number of attribute oriented functions. It
20113034Sdougm  * doesn't update the repository, only the internal document state.
20123034Sdougm  */
20133034Sdougm 
20143034Sdougm void
20153034Sdougm set_node_attr(void *nodehdl, char *tag, char *value)
20163034Sdougm {
20173034Sdougm 	xmlNodePtr node = (xmlNodePtr)nodehdl;
20183034Sdougm 	if (node != NULL && tag != NULL) {
20194327Sdougm 		if (value != NULL)
20203034Sdougm 			xmlSetProp(node, (xmlChar *)tag, (xmlChar *)value);
20214327Sdougm 		else
20223034Sdougm 			xmlUnsetProp(node, (xmlChar *)tag);
20233034Sdougm 	}
20243034Sdougm }
20253034Sdougm 
20263034Sdougm /*
20273034Sdougm  * sa_get_group_attr(group, tag)
20283034Sdougm  *
20293034Sdougm  * Get the specied attribute, if defined, for the group.
20303034Sdougm  */
20313034Sdougm 
20323034Sdougm char *
20333034Sdougm sa_get_group_attr(sa_group_t group, char *tag)
20343034Sdougm {
20353034Sdougm 	return (get_node_attr((void *)group, tag));
20363034Sdougm }
20373034Sdougm 
20383034Sdougm /*
20393034Sdougm  * sa_set_group_attr(group, tag, value)
20403034Sdougm  *
20413034Sdougm  * set the specified tag/attribute on the group using value as its
20423034Sdougm  * value.
20433034Sdougm  *
20443034Sdougm  * This will result in setting the property in the SMF repository as
20453034Sdougm  * well as in the internal document.
20463034Sdougm  */
20473034Sdougm 
20483034Sdougm int
20493034Sdougm sa_set_group_attr(sa_group_t group, char *tag, char *value)
20503034Sdougm {
20513034Sdougm 	int ret;
20523034Sdougm 	char *groupname;
20533910Sdougm 	sa_handle_impl_t impl_handle;
20543034Sdougm 
2055*5331Samw 	/*
2056*5331Samw 	 * ZFS group/subgroup doesn't need the handle so shortcut.
2057*5331Samw 	 */
2058*5331Samw 	if (sa_group_is_zfs(group)) {
2059*5331Samw 		set_node_attr((void *)group, tag, value);
2060*5331Samw 		return (SA_OK);
2061*5331Samw 	}
2062*5331Samw 
20633910Sdougm 	impl_handle = (sa_handle_impl_t)sa_find_group_handle(group);
20643910Sdougm 	if (impl_handle != NULL) {
20654327Sdougm 		groupname = sa_get_group_attr(group, "name");
20664327Sdougm 		ret = sa_get_instance(impl_handle->scfhandle, groupname);
20673910Sdougm 		if (ret == SA_OK) {
20684327Sdougm 			set_node_attr((void *)group, tag, value);
20694327Sdougm 			ret = sa_start_transaction(impl_handle->scfhandle,
20704327Sdougm 			    "operation");
20714327Sdougm 			if (ret == SA_OK) {
20724327Sdougm 				ret = sa_set_property(impl_handle->scfhandle,
20734327Sdougm 				    tag, value);
20744327Sdougm 				if (ret == SA_OK)
20754327Sdougm 					(void) sa_end_transaction(
20764327Sdougm 					    impl_handle->scfhandle);
20774327Sdougm 				else
20784327Sdougm 					sa_abort_transaction(
20794327Sdougm 					    impl_handle->scfhandle);
20804327Sdougm 			}
20813034Sdougm 		}
20824327Sdougm 		if (groupname != NULL)
20834327Sdougm 			sa_free_attr_string(groupname);
20843910Sdougm 	} else {
20854327Sdougm 		ret = SA_SYSTEM_ERR;
20863034Sdougm 	}
20873034Sdougm 	return (ret);
20883034Sdougm }
20893034Sdougm 
20903034Sdougm /*
20913034Sdougm  * sa_get_share_attr(share, tag)
20923034Sdougm  *
20933034Sdougm  * Return the value of the tag/attribute set on the specified
20943034Sdougm  * share. Returns NULL if the tag doesn't exist.
20953034Sdougm  */
20963034Sdougm 
20973034Sdougm char *
20983034Sdougm sa_get_share_attr(sa_share_t share, char *tag)
20993034Sdougm {
21003034Sdougm 	return (get_node_attr((void *)share, tag));
21013034Sdougm }
21023034Sdougm 
21033034Sdougm /*
21043034Sdougm  * _sa_set_share_description(share, description)
21053034Sdougm  *
2106*5331Samw  * Add a description tag with text contents to the specified share.  A
2107*5331Samw  * separate XML tag is used rather than a property. This can also be
2108*5331Samw  * used with resources.
21093034Sdougm  */
21103034Sdougm 
21113034Sdougm xmlNodePtr
2112*5331Samw _sa_set_share_description(void *share, char *content)
21133034Sdougm {
21143034Sdougm 	xmlNodePtr node;
21154327Sdougm 	node = xmlNewChild((xmlNodePtr)share, NULL, (xmlChar *)"description",
21164327Sdougm 	    NULL);
21173034Sdougm 	xmlNodeSetContent(node, (xmlChar *)content);
21183034Sdougm 	return (node);
21193034Sdougm }
21203034Sdougm 
21213034Sdougm /*
21223034Sdougm  * sa_set_share_attr(share, tag, value)
21233034Sdougm  *
21243034Sdougm  * Set the share attribute specified by tag to the specified value. In
21253034Sdougm  * the case of "resource", enforce a no duplicates in a group rule. If
21263034Sdougm  * the share is not transient, commit the changes to the repository
21273034Sdougm  * else just update the share internally.
21283034Sdougm  */
21293034Sdougm 
21303034Sdougm int
21313034Sdougm sa_set_share_attr(sa_share_t share, char *tag, char *value)
21323034Sdougm {
21333034Sdougm 	sa_group_t group;
21343034Sdougm 	sa_share_t resource;
21353034Sdougm 	int ret = SA_OK;
21363034Sdougm 
21373034Sdougm 	group = sa_get_parent_group(share);
21383034Sdougm 
21393034Sdougm 	/*
21403034Sdougm 	 * There are some attributes that may have specific
21413034Sdougm 	 * restrictions on them. Initially, only "resource" has
21423034Sdougm 	 * special meaning that needs to be checked. Only one instance
21433034Sdougm 	 * of a resource name may exist within a group.
21443034Sdougm 	 */
21453034Sdougm 
21463034Sdougm 	if (strcmp(tag, "resource") == 0) {
21474327Sdougm 		resource = sa_get_resource(group, value);
21484327Sdougm 		if (resource != share && resource != NULL)
21494327Sdougm 			ret = SA_DUPLICATE_NAME;
21503034Sdougm 	}
21513034Sdougm 	if (ret == SA_OK) {
21524327Sdougm 		set_node_attr((void *)share, tag, value);
21534327Sdougm 		if (group != NULL) {
21544327Sdougm 			char *type;
21554327Sdougm 			/* we can probably optimize this some */
21564327Sdougm 			type = sa_get_share_attr(share, "type");
21574327Sdougm 			if (type == NULL || strcmp(type, "transient") != 0) {
21584327Sdougm 				sa_handle_impl_t impl_handle;
21594327Sdougm 				impl_handle =
21604327Sdougm 				    (sa_handle_impl_t)sa_find_group_handle(
21614327Sdougm 				    group);
21624327Sdougm 				if (impl_handle != NULL) {
21634327Sdougm 					ret = sa_commit_share(
21644327Sdougm 					    impl_handle->scfhandle, group,
21654327Sdougm 					    share);
21664327Sdougm 				} else {
21674327Sdougm 					ret = SA_SYSTEM_ERR;
21684327Sdougm 				}
21694327Sdougm 			}
21704327Sdougm 			if (type != NULL)
21714327Sdougm 				sa_free_attr_string(type);
21723910Sdougm 		}
21733034Sdougm 	}
21743034Sdougm 	return (ret);
21753034Sdougm }
21763034Sdougm 
21773034Sdougm /*
21783034Sdougm  * sa_get_property_attr(prop, tag)
21793034Sdougm  *
21803034Sdougm  * Get the value of the specified property attribute. Standard
21813034Sdougm  * attributes are "type" and "value".
21823034Sdougm  */
21833034Sdougm 
21843034Sdougm char *
21853034Sdougm sa_get_property_attr(sa_property_t prop, char *tag)
21863034Sdougm {
21873034Sdougm 	return (get_node_attr((void *)prop, tag));
21883034Sdougm }
21893034Sdougm 
21903034Sdougm /*
21913034Sdougm  * sa_get_optionset_attr(prop, tag)
21923034Sdougm  *
21933034Sdougm  * Get the value of the specified property attribute. Standard
21943034Sdougm  * attribute is "type".
21953034Sdougm  */
21963034Sdougm 
21973034Sdougm char *
21983034Sdougm sa_get_optionset_attr(sa_property_t optionset, char *tag)
21993034Sdougm {
22003034Sdougm 	return (get_node_attr((void *)optionset, tag));
22013034Sdougm 
22023034Sdougm }
22033034Sdougm 
22043034Sdougm /*
22053034Sdougm  * sa_set_optionset_attr(optionset, tag, value)
22063034Sdougm  *
22073034Sdougm  * Set the specified attribute(tag) to the specified value on the
22083034Sdougm  * optionset.
22093034Sdougm  */
22103034Sdougm 
22113034Sdougm void
22123034Sdougm sa_set_optionset_attr(sa_group_t optionset, char *tag, char *value)
22133034Sdougm {
22143034Sdougm 	set_node_attr((void *)optionset, tag, value);
22153034Sdougm }
22163034Sdougm 
22173034Sdougm /*
22183034Sdougm  * sa_free_attr_string(string)
22193034Sdougm  *
22203034Sdougm  * Free the string that was returned in one of the sa_get_*_attr()
22213034Sdougm  * functions.
22223034Sdougm  */
22233034Sdougm 
22243034Sdougm void
22253034Sdougm sa_free_attr_string(char *string)
22263034Sdougm {
22273034Sdougm 	xmlFree((xmlChar *)string);
22283034Sdougm }
22293034Sdougm 
22303034Sdougm /*
22313034Sdougm  * sa_get_optionset(group, proto)
22323034Sdougm  *
22333034Sdougm  * Return the optionset, if it exists, that is associated with the
22343034Sdougm  * specified protocol.
22353034Sdougm  */
22363034Sdougm 
22373034Sdougm sa_optionset_t
22383034Sdougm sa_get_optionset(void *group, char *proto)
22393034Sdougm {
22403034Sdougm 	xmlNodePtr node;
22413034Sdougm 	xmlChar *value = NULL;
22423034Sdougm 
22433034Sdougm 	for (node = ((xmlNodePtr)group)->children; node != NULL;
22444327Sdougm 	    node = node->next) {
22453034Sdougm 		if (xmlStrcmp(node->name, (xmlChar *)"optionset") == 0) {
22464327Sdougm 			value = xmlGetProp(node, (xmlChar *)"type");
22474327Sdougm 			if (proto != NULL) {
22484327Sdougm 				if (value != NULL &&
22494327Sdougm 				    xmlStrcmp(value, (xmlChar *)proto) == 0) {
22504327Sdougm 					break;
22514327Sdougm 				}
22524327Sdougm 				if (value != NULL) {
22534327Sdougm 					xmlFree(value);
22544327Sdougm 					value = NULL;
22554327Sdougm 				}
22564327Sdougm 			} else {
22574327Sdougm 				break;
22583034Sdougm 			}
22593034Sdougm 		}
22603034Sdougm 	}
22613034Sdougm 	if (value != NULL)
22624327Sdougm 		xmlFree(value);
22633034Sdougm 	return ((sa_optionset_t)node);
22643034Sdougm }
22653034Sdougm 
22663034Sdougm /*
22673034Sdougm  * sa_get_next_optionset(optionset)
22683034Sdougm  *
22693034Sdougm  * Return the next optionset in the group. NULL if this was the last.
22703034Sdougm  */
22713034Sdougm 
22723034Sdougm sa_optionset_t
22733034Sdougm sa_get_next_optionset(sa_optionset_t optionset)
22743034Sdougm {
22753034Sdougm 	xmlNodePtr node;
22763034Sdougm 
22773034Sdougm 	for (node = ((xmlNodePtr)optionset)->next; node != NULL;
22784327Sdougm 	    node = node->next) {
22793034Sdougm 		if (xmlStrcmp(node->name, (xmlChar *)"optionset") == 0) {
22803034Sdougm 			break;
22813034Sdougm 		}
22823034Sdougm 	}
22833034Sdougm 	return ((sa_optionset_t)node);
22843034Sdougm }
22853034Sdougm 
22863034Sdougm /*
22873034Sdougm  * sa_get_security(group, sectype, proto)
22883034Sdougm  *
22893034Sdougm  * Return the security optionset. The internal name is a hold over
22903034Sdougm  * from the implementation and will be changed before the API is
22913034Sdougm  * finalized. This is really a named optionset that can be negotiated
22923034Sdougm  * as a group of properties (like NFS security options).
22933034Sdougm  */
22943034Sdougm 
22953034Sdougm sa_security_t
22963034Sdougm sa_get_security(sa_group_t group, char *sectype, char *proto)
22973034Sdougm {
22983034Sdougm 	xmlNodePtr node;
22993034Sdougm 	xmlChar *value = NULL;
23003034Sdougm 
23013034Sdougm 	for (node = ((xmlNodePtr)group)->children; node != NULL;
23024327Sdougm 	    node = node->next) {
23034327Sdougm 		if (xmlStrcmp(node->name, (xmlChar *)"security") == 0) {
23044327Sdougm 			if (proto != NULL) {
23054327Sdougm 				value = xmlGetProp(node, (xmlChar *)"type");
23064327Sdougm 				if (value == NULL ||
23074327Sdougm 				    (value != NULL &&
23084327Sdougm 				    xmlStrcmp(value, (xmlChar *)proto) != 0)) {
23094327Sdougm 					/* it doesn't match so continue */
23104327Sdougm 					xmlFree(value);
23114327Sdougm 					value = NULL;
23124327Sdougm 					continue;
23134327Sdougm 				}
23144327Sdougm 			}
23154327Sdougm 			if (value != NULL) {
23164327Sdougm 				xmlFree(value);
23174327Sdougm 				value = NULL;
23184327Sdougm 			}
23194327Sdougm 			/* potential match */
23204327Sdougm 			if (sectype != NULL) {
23214327Sdougm 				value = xmlGetProp(node, (xmlChar *)"sectype");
23224327Sdougm 				if (value != NULL &&
23234327Sdougm 				    xmlStrcmp(value, (xmlChar *)sectype) == 0) {
23244327Sdougm 					break;
23254327Sdougm 				}
23264327Sdougm 			} else {
23274327Sdougm 				break;
23284327Sdougm 			}
23293034Sdougm 		}
23303034Sdougm 		if (value != NULL) {
23314327Sdougm 			xmlFree(value);
23324327Sdougm 			value = NULL;
23333034Sdougm 		}
23343034Sdougm 	}
23353034Sdougm 	if (value != NULL)
23364327Sdougm 		xmlFree(value);
23373034Sdougm 	return ((sa_security_t)node);
23383034Sdougm }
23393034Sdougm 
23403034Sdougm /*
23413034Sdougm  * sa_get_next_security(security)
23423034Sdougm  *
23433034Sdougm  * Get the next security optionset if one exists.
23443034Sdougm  */
23453034Sdougm 
23463034Sdougm sa_security_t
23473034Sdougm sa_get_next_security(sa_security_t security)
23483034Sdougm {
23493034Sdougm 	xmlNodePtr node;
23503034Sdougm 
23513034Sdougm 	for (node = ((xmlNodePtr)security)->next; node != NULL;
23524327Sdougm 	    node = node->next) {
23533034Sdougm 		if (xmlStrcmp(node->name, (xmlChar *)"security") == 0) {
23543034Sdougm 			break;
23553034Sdougm 		}
23563034Sdougm 	}
23573034Sdougm 	return ((sa_security_t)node);
23583034Sdougm }
23593034Sdougm 
23603034Sdougm /*
23613034Sdougm  * sa_get_property(optionset, prop)
23623034Sdougm  *
23633034Sdougm  * Get the property object with the name specified in prop from the
23643034Sdougm  * optionset.
23653034Sdougm  */
23663034Sdougm 
23673034Sdougm sa_property_t
23683034Sdougm sa_get_property(sa_optionset_t optionset, char *prop)
23693034Sdougm {
23703034Sdougm 	xmlNodePtr node = (xmlNodePtr)optionset;
23713034Sdougm 	xmlChar *value = NULL;
23723034Sdougm 
23733034Sdougm 	if (optionset == NULL)
23744327Sdougm 		return (NULL);
23753034Sdougm 
23763034Sdougm 	for (node = node->children; node != NULL;
23774327Sdougm 	    node = node->next) {
23784327Sdougm 		if (xmlStrcmp(node->name, (xmlChar *)"option") == 0) {
23794327Sdougm 			if (prop == NULL)
23804327Sdougm 				break;
23814327Sdougm 			value = xmlGetProp(node, (xmlChar *)"type");
23824327Sdougm 			if (value != NULL &&
23834327Sdougm 			    xmlStrcmp(value, (xmlChar *)prop) == 0) {
23844327Sdougm 				break;
23854327Sdougm 			}
23864327Sdougm 			if (value != NULL) {
23874327Sdougm 				xmlFree(value);
23884327Sdougm 				value = NULL;
23894327Sdougm 			}
23903034Sdougm 		}
23913034Sdougm 	}
23923034Sdougm 	if (value != NULL)
23933034Sdougm 		xmlFree(value);
23943034Sdougm 	if (node != NULL && xmlStrcmp(node->name, (xmlChar *)"option") != 0) {
23954327Sdougm 		/*
23964327Sdougm 		 * avoid a non option node -- it is possible to be a
23974327Sdougm 		 * text node
23984327Sdougm 		 */
23994327Sdougm 		node = NULL;
24003034Sdougm 	}
24013034Sdougm 	return ((sa_property_t)node);
24023034Sdougm }
24033034Sdougm 
24043034Sdougm /*
24053034Sdougm  * sa_get_next_property(property)
24063034Sdougm  *
24073034Sdougm  * Get the next property following the specified property. NULL if
24083034Sdougm  * this was the last.
24093034Sdougm  */
24103034Sdougm 
24113034Sdougm sa_property_t
24123034Sdougm sa_get_next_property(sa_property_t property)
24133034Sdougm {
24143034Sdougm 	xmlNodePtr node;
24153034Sdougm 
24163034Sdougm 	for (node = ((xmlNodePtr)property)->next; node != NULL;
24174327Sdougm 	    node = node->next) {
24183034Sdougm 		if (xmlStrcmp(node->name, (xmlChar *)"option") == 0) {
24193034Sdougm 			break;
24203034Sdougm 		}
24213034Sdougm 	}
24223034Sdougm 	return ((sa_property_t)node);
24233034Sdougm }
24243034Sdougm 
24253034Sdougm /*
24263034Sdougm  * sa_set_share_description(share, content)
24273034Sdougm  *
24283034Sdougm  * Set the description of share to content.
24293034Sdougm  */
24303034Sdougm 
24313034Sdougm int
24323034Sdougm sa_set_share_description(sa_share_t share, char *content)
24333034Sdougm {
24343034Sdougm 	xmlNodePtr node;
24353034Sdougm 	sa_group_t group;
24363034Sdougm 	int ret = SA_OK;
24373034Sdougm 
24383034Sdougm 	for (node = ((xmlNodePtr)share)->children; node != NULL;
24394327Sdougm 	    node = node->next) {
24403034Sdougm 		if (xmlStrcmp(node->name, (xmlChar *)"description") == 0) {
24413034Sdougm 			break;
24423034Sdougm 		}
24433034Sdougm 	}
24443034Sdougm 	/* no existing description but want to add */
24453034Sdougm 	if (node == NULL && content != NULL) {
24463034Sdougm 		/* add a description */
24474327Sdougm 		node = _sa_set_share_description(share, content);
24483034Sdougm 	} else if (node != NULL && content != NULL) {
24493034Sdougm 		/* update a description */
24503034Sdougm 		xmlNodeSetContent(node, (xmlChar *)content);
24513034Sdougm 	} else if (node != NULL && content == NULL) {
24523034Sdougm 		/* remove an existing description */
24533034Sdougm 		xmlUnlinkNode(node);
24543034Sdougm 		xmlFreeNode(node);
24553034Sdougm 	}
2456*5331Samw 	group = sa_get_parent_group(share);
2457*5331Samw 	if (group != NULL && sa_is_persistent(share)) {
24584327Sdougm 		sa_handle_impl_t impl_handle;
24594327Sdougm 		impl_handle = (sa_handle_impl_t)sa_find_group_handle(group);
24604327Sdougm 		if (impl_handle != NULL) {
24614327Sdougm 			ret = sa_commit_share(impl_handle->scfhandle, group,
24624327Sdougm 			    share);
24634327Sdougm 		} else {
24644327Sdougm 			ret = SA_SYSTEM_ERR;
24654327Sdougm 		}
24663910Sdougm 	}
24673034Sdougm 	return (ret);
24683034Sdougm }
24693034Sdougm 
24703034Sdougm /*
24713034Sdougm  * fixproblemchars(string)
24723034Sdougm  *
24733034Sdougm  * don't want any newline or tab characters in the text since these
24743034Sdougm  * could break display of data and legacy file formats.
24753034Sdougm  */
24763034Sdougm static void
24773034Sdougm fixproblemchars(char *str)
24783034Sdougm {
24793034Sdougm 	int c;
24803034Sdougm 	for (c = *str; c != '\0'; c = *++str) {
24814327Sdougm 		if (c == '\t' || c == '\n')
24824327Sdougm 			*str = ' ';
24834327Sdougm 		else if (c == '"')
24844327Sdougm 			*str = '\'';
24853034Sdougm 	}
24863034Sdougm }
24873034Sdougm 
24883034Sdougm /*
24893034Sdougm  * sa_get_share_description(share)
24903034Sdougm  *
24913034Sdougm  * Return the description text for the specified share if it
24923034Sdougm  * exists. NULL if no description exists.
24933034Sdougm  */
24943034Sdougm 
24953034Sdougm char *
24963034Sdougm sa_get_share_description(sa_share_t share)
24973034Sdougm {
24983034Sdougm 	xmlChar *description = NULL;
24993034Sdougm 	xmlNodePtr node;
25003034Sdougm 
25013034Sdougm 	for (node = ((xmlNodePtr)share)->children; node != NULL;
25024327Sdougm 	    node = node->next) {
25034327Sdougm 		if (xmlStrcmp(node->name, (xmlChar *)"description") == 0) {
25044327Sdougm 			break;
25054327Sdougm 		}
25063034Sdougm 	}
25073034Sdougm 	if (node != NULL) {
2508*5331Samw 		description = xmlNodeGetContent(node);
25094327Sdougm 		fixproblemchars((char *)description);
25103034Sdougm 	}
25113034Sdougm 	return ((char *)description);
25123034Sdougm }
25133034Sdougm 
25143034Sdougm /*
25153034Sdougm  * sa_free(share_description(description)
25163034Sdougm  *
25173034Sdougm  * Free the description string.
25183034Sdougm  */
25193034Sdougm 
25203034Sdougm void
25213034Sdougm sa_free_share_description(char *description)
25223034Sdougm {
25233034Sdougm 	xmlFree((xmlChar *)description);
25243034Sdougm }
25253034Sdougm 
25263034Sdougm /*
25273034Sdougm  * sa_create_optionset(group, proto)
25283034Sdougm  *
25293034Sdougm  * Create an optionset for the specified protocol in the specied
25303034Sdougm  * group. This is manifested as a property group within SMF.
25313034Sdougm  */
25323034Sdougm 
25333034Sdougm sa_optionset_t
25343034Sdougm sa_create_optionset(sa_group_t group, char *proto)
25353034Sdougm {
25363034Sdougm 	sa_optionset_t optionset;
25373034Sdougm 	sa_group_t parent = group;
2538*5331Samw 	sa_share_t share = NULL;
2539*5331Samw 	int err = SA_OK;
2540*5331Samw 	char *id = NULL;
25413034Sdougm 
25423034Sdougm 	optionset = sa_get_optionset(group, proto);
25433034Sdougm 	if (optionset != NULL) {
25443034Sdougm 		/* can't have a duplicate protocol */
25454327Sdougm 		optionset = NULL;
25463034Sdougm 	} else {
2547*5331Samw 		/*
2548*5331Samw 		 * Account for resource names being slightly
2549*5331Samw 		 * different.
2550*5331Samw 		 */
2551*5331Samw 		if (sa_is_share(group)) {
2552*5331Samw 			/*
2553*5331Samw 			 * Transient shares do not have an "id" so not an
2554*5331Samw 			 * error to not find one.
2555*5331Samw 			 */
2556*5331Samw 			id = sa_get_share_attr((sa_share_t)group, "id");
2557*5331Samw 		} else if (sa_is_resource(group)) {
2558*5331Samw 			share = sa_get_resource_parent(
2559*5331Samw 			    (sa_resource_t)group);
2560*5331Samw 			id = sa_get_resource_attr(share, "id");
2561*5331Samw 
2562*5331Samw 			/* id can be NULL if the group is transient (ZFS) */
2563*5331Samw 			if (id == NULL && sa_is_persistent(group))
2564*5331Samw 				err = SA_NO_MEMORY;
2565*5331Samw 		}
2566*5331Samw 		if (err == SA_NO_MEMORY) {
2567*5331Samw 			/*
2568*5331Samw 			 * Couldn't get the id for the share or
2569*5331Samw 			 * resource. While this could be a
2570*5331Samw 			 * configuration issue, it is most likely an
2571*5331Samw 			 * out of memory. In any case, fail the create.
2572*5331Samw 			 */
2573*5331Samw 			return (NULL);
2574*5331Samw 		}
2575*5331Samw 
25764327Sdougm 		optionset = (sa_optionset_t)xmlNewChild((xmlNodePtr)group,
25774327Sdougm 		    NULL, (xmlChar *)"optionset", NULL);
25783034Sdougm 		/*
25793034Sdougm 		 * only put to repository if on a group and we were
25803034Sdougm 		 * able to create an optionset.
25813034Sdougm 		 */
25824327Sdougm 		if (optionset != NULL) {
25834327Sdougm 			char oname[SA_STRSIZE];
25844327Sdougm 			char *groupname;
2585*5331Samw 
2586*5331Samw 			/*
2587*5331Samw 			 * Need to get parent group in all cases, but also get
2588*5331Samw 			 * the share if this is a resource.
2589*5331Samw 			 */
2590*5331Samw 			if (sa_is_share(group)) {
25914327Sdougm 				parent = sa_get_parent_group((sa_share_t)group);
2592*5331Samw 			} else if (sa_is_resource(group)) {
2593*5331Samw 				share = sa_get_resource_parent(
2594*5331Samw 				    (sa_resource_t)group);
2595*5331Samw 				parent = sa_get_parent_group(share);
2596*5331Samw 			}
25974327Sdougm 
25984327Sdougm 			sa_set_optionset_attr(optionset, "type", proto);
25993034Sdougm 
26004327Sdougm 			(void) sa_optionset_name(optionset, oname,
26014327Sdougm 			    sizeof (oname), id);
26024327Sdougm 			groupname = sa_get_group_attr(parent, "name");
2603*5331Samw 			if (groupname != NULL && sa_is_persistent(group)) {
26044327Sdougm 				sa_handle_impl_t impl_handle;
2605*5331Samw 				impl_handle =
2606*5331Samw 				    (sa_handle_impl_t)sa_find_group_handle(
2607*5331Samw 				    group);
26084327Sdougm 				assert(impl_handle != NULL);
26094327Sdougm 				if (impl_handle != NULL) {
26104327Sdougm 					(void) sa_get_instance(
2611*5331Samw 					    impl_handle->scfhandle, groupname);
26124327Sdougm 					(void) sa_create_pgroup(
26134327Sdougm 					    impl_handle->scfhandle, oname);
26144327Sdougm 				}
26154327Sdougm 			}
26164327Sdougm 			if (groupname != NULL)
26174327Sdougm 				sa_free_attr_string(groupname);
26183034Sdougm 		}
26193034Sdougm 	}
2620*5331Samw 
2621*5331Samw 	if (id != NULL)
2622*5331Samw 		sa_free_attr_string(id);
26233034Sdougm 	return (optionset);
26243034Sdougm }
26253034Sdougm 
26263034Sdougm /*
26273034Sdougm  * sa_get_property_parent(property)
26283034Sdougm  *
26293034Sdougm  * Given a property, return the object it is a property of. This will
26303034Sdougm  * be an optionset of some type.
26313034Sdougm  */
26323034Sdougm 
26333034Sdougm static sa_optionset_t
26343034Sdougm sa_get_property_parent(sa_property_t property)
26353034Sdougm {
26363034Sdougm 	xmlNodePtr node = NULL;
26373034Sdougm 
26384327Sdougm 	if (property != NULL)
26394327Sdougm 		node = ((xmlNodePtr)property)->parent;
26403034Sdougm 	return ((sa_optionset_t)node);
26413034Sdougm }
26423034Sdougm 
26433034Sdougm /*
26443034Sdougm  * sa_get_optionset_parent(optionset)
26453034Sdougm  *
26463034Sdougm  * Return the parent of the specified optionset. This could be a group
26473034Sdougm  * or a share.
26483034Sdougm  */
26493034Sdougm 
26503034Sdougm static sa_group_t
26513034Sdougm sa_get_optionset_parent(sa_optionset_t optionset)
26523034Sdougm {
26533034Sdougm 	xmlNodePtr node = NULL;
26543034Sdougm 
26554327Sdougm 	if (optionset != NULL)
26564327Sdougm 		node = ((xmlNodePtr)optionset)->parent;
26573034Sdougm 	return ((sa_group_t)node);
26583034Sdougm }
26593034Sdougm 
26603034Sdougm /*
26613034Sdougm  * zfs_needs_update(share)
26623034Sdougm  *
26633034Sdougm  * In order to avoid making multiple updates to a ZFS share when
26643034Sdougm  * setting properties, the share attribute "changed" will be set to
2665*5331Samw  * true when a property is added or modified.  When done adding
26663034Sdougm  * properties, we can then detect that an update is needed.  We then
26673034Sdougm  * clear the state here to detect additional changes.
26683034Sdougm  */
26693034Sdougm 
26703034Sdougm static int
26713034Sdougm zfs_needs_update(sa_share_t share)
26723034Sdougm {
26733034Sdougm 	char *attr;
26743034Sdougm 	int result = 0;
26753034Sdougm 
26763034Sdougm 	attr = sa_get_share_attr(share, "changed");
26773034Sdougm 	if (attr != NULL) {
26784327Sdougm 		sa_free_attr_string(attr);
26793034Sdougm 		result = 1;
26803034Sdougm 	}
26813034Sdougm 	set_node_attr((void *)share, "changed", NULL);
26823034Sdougm 	return (result);
26833034Sdougm }
26843034Sdougm 
26853034Sdougm /*
26863034Sdougm  * zfs_set_update(share)
26873034Sdougm  *
26883034Sdougm  * Set the changed attribute of the share to true.
26893034Sdougm  */
26903034Sdougm 
26913034Sdougm static void
26923034Sdougm zfs_set_update(sa_share_t share)
26933034Sdougm {
26943034Sdougm 	set_node_attr((void *)share, "changed", "true");
26953034Sdougm }
26963034Sdougm 
26973034Sdougm /*
26983034Sdougm  * sa_commit_properties(optionset, clear)
26993034Sdougm  *
27003034Sdougm  * Check if SMF or ZFS config and either update or abort the pending
27013034Sdougm  * changes.
27023034Sdougm  */
27033034Sdougm 
27043034Sdougm int
27053034Sdougm sa_commit_properties(sa_optionset_t optionset, int clear)
27063034Sdougm {
27073034Sdougm 	sa_group_t group;
27083034Sdougm 	sa_group_t parent;
27093034Sdougm 	int zfs = 0;
27103034Sdougm 	int needsupdate = 0;
27113034Sdougm 	int ret = SA_OK;
27123910Sdougm 	sa_handle_impl_t impl_handle;
27133034Sdougm 
27143034Sdougm 	group = sa_get_optionset_parent(optionset);
27153034Sdougm 	if (group != NULL && (sa_is_share(group) || is_zfs_group(group))) {
27164327Sdougm 		/* only update ZFS if on a share */
27174327Sdougm 		parent = sa_get_parent_group(group);
27184327Sdougm 		zfs++;
27194327Sdougm 		if (parent != NULL && is_zfs_group(parent))
27204327Sdougm 			needsupdate = zfs_needs_update(group);
27214327Sdougm 		else
27224327Sdougm 			zfs = 0;
27233034Sdougm 	}
27243034Sdougm 	if (zfs) {
27254327Sdougm 		if (!clear && needsupdate)
27264327Sdougm 			ret = sa_zfs_update((sa_share_t)group);
27273034Sdougm 	} else {
27284327Sdougm 		impl_handle = (sa_handle_impl_t)sa_find_group_handle(group);
27294327Sdougm 		if (impl_handle != NULL) {
27304327Sdougm 			if (clear) {
27314327Sdougm 				(void) sa_abort_transaction(
27324327Sdougm 				    impl_handle->scfhandle);
27334327Sdougm 			} else {
27344327Sdougm 				ret = sa_end_transaction(
27354327Sdougm 				    impl_handle->scfhandle);
27364327Sdougm 			}
27374327Sdougm 		} else {
27384327Sdougm 			ret = SA_SYSTEM_ERR;
27394327Sdougm 		}
27403034Sdougm 	}
27413034Sdougm 	return (ret);
27423034Sdougm }
27433034Sdougm 
27443034Sdougm /*
27453034Sdougm  * sa_destroy_optionset(optionset)
27463034Sdougm  *
2747*5331Samw  * Remove the optionset from its group. Update the repository to
27483034Sdougm  * reflect this change.
27493034Sdougm  */
27503034Sdougm 
27513034Sdougm int
27523034Sdougm sa_destroy_optionset(sa_optionset_t optionset)
27533034Sdougm {
27544327Sdougm 	char name[SA_STRSIZE];
27553034Sdougm 	int len;
27563034Sdougm 	int ret;
27573034Sdougm 	char *id = NULL;
27583034Sdougm 	sa_group_t group;
27593034Sdougm 	int ispersist = 1;
27603034Sdougm 
27613034Sdougm 	/* now delete the prop group */
27623034Sdougm 	group = sa_get_optionset_parent(optionset);
2763*5331Samw 	if (group != NULL) {
2764*5331Samw 		if (sa_is_resource(group)) {
2765*5331Samw 			sa_resource_t resource = group;
2766*5331Samw 			sa_share_t share = sa_get_resource_parent(resource);
2767*5331Samw 			group = sa_get_parent_group(share);
2768*5331Samw 			id = sa_get_share_attr(share, "id");
2769*5331Samw 		} else if (sa_is_share(group)) {
2770*5331Samw 			id = sa_get_share_attr((sa_share_t)group, "id");
2771*5331Samw 		}
2772*5331Samw 		ispersist = sa_is_persistent(group);
27733034Sdougm 	}
27743034Sdougm 	if (ispersist) {
27754327Sdougm 		sa_handle_impl_t impl_handle;
27764327Sdougm 		len = sa_optionset_name(optionset, name, sizeof (name), id);
27774327Sdougm 		impl_handle = (sa_handle_impl_t)sa_find_group_handle(group);
27784327Sdougm 		if (impl_handle != NULL) {
27794327Sdougm 			if (len > 0) {
27804327Sdougm 				ret = sa_delete_pgroup(impl_handle->scfhandle,
27814327Sdougm 				    name);
27824327Sdougm 			}
27834327Sdougm 		} else {
27844327Sdougm 			ret = SA_SYSTEM_ERR;
27853910Sdougm 		}
27863034Sdougm 	}
27873034Sdougm 	xmlUnlinkNode((xmlNodePtr)optionset);
27883034Sdougm 	xmlFreeNode((xmlNodePtr)optionset);
27893034Sdougm 	if (id != NULL)
27904327Sdougm 		sa_free_attr_string(id);
27913034Sdougm 	return (ret);
27923034Sdougm }
27933034Sdougm 
27943034Sdougm /* private to the implementation */
27953034Sdougm int
27963034Sdougm _sa_remove_optionset(sa_optionset_t optionset)
27973034Sdougm {
27983034Sdougm 	int ret = SA_OK;
27993034Sdougm 
28003034Sdougm 	xmlUnlinkNode((xmlNodePtr)optionset);
28013034Sdougm 	xmlFreeNode((xmlNodePtr)optionset);
28023034Sdougm 	return (ret);
28033034Sdougm }
28043034Sdougm 
28053034Sdougm /*
28063034Sdougm  * sa_create_security(group, sectype, proto)
28073034Sdougm  *
28083034Sdougm  * Create a security optionset (one that has a type name and a
28093034Sdougm  * proto). Security is left over from a pure NFS implementation. The
28103034Sdougm  * naming will change in the future when the API is released.
28113034Sdougm  */
28123034Sdougm sa_security_t
28133034Sdougm sa_create_security(sa_group_t group, char *sectype, char *proto)
28143034Sdougm {
28153034Sdougm 	sa_security_t security;
28163034Sdougm 	char *id = NULL;
28173034Sdougm 	sa_group_t parent;
28183034Sdougm 	char *groupname = NULL;
28193034Sdougm 
28203034Sdougm 	if (group != NULL && sa_is_share(group)) {
28214327Sdougm 		id = sa_get_share_attr((sa_share_t)group, "id");
28224327Sdougm 		parent = sa_get_parent_group(group);
28234327Sdougm 		if (parent != NULL)
28244327Sdougm 			groupname = sa_get_group_attr(parent, "name");
28253034Sdougm 	} else if (group != NULL) {
28264327Sdougm 		groupname = sa_get_group_attr(group, "name");
28273034Sdougm 	}
28283034Sdougm 
28293034Sdougm 	security = sa_get_security(group, sectype, proto);
28303034Sdougm 	if (security != NULL) {
28313034Sdougm 		/* can't have a duplicate security option */
28323034Sdougm 		security = NULL;
28333034Sdougm 	} else {
28343034Sdougm 		security = (sa_security_t)xmlNewChild((xmlNodePtr)group,
28354327Sdougm 		    NULL, (xmlChar *)"security", NULL);
28363034Sdougm 		if (security != NULL) {
28374327Sdougm 			char oname[SA_STRSIZE];
28383034Sdougm 			sa_set_security_attr(security, "type", proto);
28393034Sdougm 
28403034Sdougm 			sa_set_security_attr(security, "sectype", sectype);
28413034Sdougm 			(void) sa_security_name(security, oname,
28424327Sdougm 			    sizeof (oname), id);
2843*5331Samw 			if (groupname != NULL && sa_is_persistent(group)) {
28444327Sdougm 				sa_handle_impl_t impl_handle;
28454327Sdougm 				impl_handle =
28464327Sdougm 				    (sa_handle_impl_t)sa_find_group_handle(
28474327Sdougm 				    group);
28484327Sdougm 				if (impl_handle != NULL) {
28494327Sdougm 					(void) sa_get_instance(
28504327Sdougm 					    impl_handle->scfhandle, groupname);
28514327Sdougm 					(void) sa_create_pgroup(
28524327Sdougm 					    impl_handle->scfhandle, oname);
28534327Sdougm 				}
28543034Sdougm 			}
28553034Sdougm 		}
28563034Sdougm 	}
28573034Sdougm 	if (groupname != NULL)
28584327Sdougm 		sa_free_attr_string(groupname);
28593034Sdougm 	return (security);
28603034Sdougm }
28613034Sdougm 
28623034Sdougm /*
28633034Sdougm  * sa_destroy_security(security)
28643034Sdougm  *
28653034Sdougm  * Remove the specified optionset from the document and the
28663034Sdougm  * configuration.
28673034Sdougm  */
28683034Sdougm 
28693034Sdougm int
28703034Sdougm sa_destroy_security(sa_security_t security)
28713034Sdougm {
28724327Sdougm 	char name[SA_STRSIZE];
28733034Sdougm 	int len;
28743034Sdougm 	int ret = SA_OK;
28753034Sdougm 	char *id = NULL;
28763034Sdougm 	sa_group_t group;
28773034Sdougm 	int iszfs = 0;
28783034Sdougm 	int ispersist = 1;
28793034Sdougm 
28803034Sdougm 	group = sa_get_optionset_parent(security);
28813034Sdougm 
28823034Sdougm 	if (group != NULL)
28834327Sdougm 		iszfs = sa_group_is_zfs(group);
28843034Sdougm 
28853034Sdougm 	if (group != NULL && !iszfs) {
28864327Sdougm 		if (sa_is_share(group))
2887*5331Samw 			ispersist = sa_is_persistent(group);
28884327Sdougm 		id = sa_get_share_attr((sa_share_t)group, "id");
28893034Sdougm 	}
28903034Sdougm 	if (ispersist) {
28914327Sdougm 		len = sa_security_name(security, name, sizeof (name), id);
28924327Sdougm 		if (!iszfs && len > 0) {
28934327Sdougm 			sa_handle_impl_t impl_handle;
28944327Sdougm 			impl_handle =
28954327Sdougm 			    (sa_handle_impl_t)sa_find_group_handle(group);
28964327Sdougm 			if (impl_handle != NULL) {
28974327Sdougm 				ret = sa_delete_pgroup(impl_handle->scfhandle,
28984327Sdougm 				    name);
28994327Sdougm 			} else {
29004327Sdougm 				ret = SA_SYSTEM_ERR;
29014327Sdougm 			}
29023910Sdougm 		}
29033034Sdougm 	}
29043034Sdougm 	xmlUnlinkNode((xmlNodePtr)security);
29053034Sdougm 	xmlFreeNode((xmlNodePtr)security);
29064327Sdougm 	if (iszfs)
29074327Sdougm 		ret = sa_zfs_update(group);
29083034Sdougm 	if (id != NULL)
29094327Sdougm 		sa_free_attr_string(id);
29103034Sdougm 	return (ret);
29113034Sdougm }
29123034Sdougm 
29133034Sdougm /*
29143034Sdougm  * sa_get_security_attr(optionset, tag)
29153034Sdougm  *
29163034Sdougm  * Return the specified attribute value from the optionset.
29173034Sdougm  */
29183034Sdougm 
29193034Sdougm char *
29203034Sdougm sa_get_security_attr(sa_property_t optionset, char *tag)
29213034Sdougm {
29223034Sdougm 	return (get_node_attr((void *)optionset, tag));
29233034Sdougm 
29243034Sdougm }
29253034Sdougm 
29263034Sdougm /*
29273034Sdougm  * sa_set_security_attr(optionset, tag, value)
29283034Sdougm  *
29293034Sdougm  * Set the optioset attribute specied by tag to the specified value.
29303034Sdougm  */
29313034Sdougm 
29323034Sdougm void
29333034Sdougm sa_set_security_attr(sa_group_t optionset, char *tag, char *value)
29343034Sdougm {
29353034Sdougm 	set_node_attr((void *)optionset, tag, value);
29363034Sdougm }
29373034Sdougm 
29383034Sdougm /*
29393034Sdougm  * is_nodetype(node, type)
29403034Sdougm  *
29413034Sdougm  * Check to see if node is of the type specified.
29423034Sdougm  */
29433034Sdougm 
29443034Sdougm static int
29453034Sdougm is_nodetype(void *node, char *type)
29463034Sdougm {
29473034Sdougm 	return (strcmp((char *)((xmlNodePtr)node)->name, type) == 0);
29483034Sdougm }
29493034Sdougm 
29504327Sdougm /*
29514327Sdougm  * add_or_update()
29524327Sdougm  *
29534327Sdougm  * Add or update a property. Pulled out of sa_set_prop_by_prop for
29544327Sdougm  * readability.
29554327Sdougm  */
29564327Sdougm static int
29574327Sdougm add_or_update(scfutilhandle_t *scf_handle, int type, scf_value_t *value,
29584327Sdougm     scf_transaction_entry_t *entry, char *name, char *valstr)
29594327Sdougm {
29604327Sdougm 	int ret = SA_SYSTEM_ERR;
29614327Sdougm 
29624327Sdougm 	if (value != NULL) {
29634327Sdougm 		if (type == SA_PROP_OP_ADD)
29644327Sdougm 			ret = scf_transaction_property_new(scf_handle->trans,
29654327Sdougm 			    entry, name, SCF_TYPE_ASTRING);
29664327Sdougm 		else
29674327Sdougm 			ret = scf_transaction_property_change(scf_handle->trans,
29684327Sdougm 			    entry, name, SCF_TYPE_ASTRING);
29694327Sdougm 		if (ret == 0) {
29704327Sdougm 			ret = scf_value_set_astring(value, valstr);
29714327Sdougm 			if (ret == 0)
29724327Sdougm 				ret = scf_entry_add_value(entry, value);
29734327Sdougm 			if (ret == 0)
29744327Sdougm 				return (ret);
29754327Sdougm 			scf_value_destroy(value);
29764327Sdougm 		} else {
29774327Sdougm 			scf_entry_destroy(entry);
29784327Sdougm 		}
29794327Sdougm 	}
29804327Sdougm 	return (SA_SYSTEM_ERR);
29814327Sdougm }
29824327Sdougm 
29833034Sdougm /*
29843034Sdougm  * sa_set_prop_by_prop(optionset, group, prop, type)
29853034Sdougm  *
29863034Sdougm  * Add/remove/update the specified property prop into the optionset or
29873034Sdougm  * share. If a share, sort out which property group based on GUID. In
29883034Sdougm  * all cases, the appropriate transaction is set (or ZFS share is
29893034Sdougm  * marked as needing an update)
29903034Sdougm  */
29913034Sdougm 
29923034Sdougm static int
29933034Sdougm sa_set_prop_by_prop(sa_optionset_t optionset, sa_group_t group,
29943034Sdougm 			sa_property_t prop, int type)
29953034Sdougm {
29963034Sdougm 	char *name;
29973034Sdougm 	char *valstr;
29983034Sdougm 	int ret = SA_OK;
29993034Sdougm 	scf_transaction_entry_t *entry;
30003034Sdougm 	scf_value_t *value;
30013034Sdougm 	int opttype; /* 1 == optionset, 0 == security */
30023034Sdougm 	char *id = NULL;
30033034Sdougm 	int iszfs = 0;
30043034Sdougm 	sa_group_t parent = NULL;
3005*5331Samw 	sa_share_t share = NULL;
30063910Sdougm 	sa_handle_impl_t impl_handle;
30073910Sdougm 	scfutilhandle_t  *scf_handle;
30083034Sdougm 
3009*5331Samw 	if (!sa_is_persistent(group)) {
30103034Sdougm 		/*
30113034Sdougm 		 * if the group/share is not persistent we don't need
30123034Sdougm 		 * to do anything here
30133034Sdougm 		 */
30144327Sdougm 		return (SA_OK);
30153034Sdougm 	}
30163910Sdougm 	impl_handle = (sa_handle_impl_t)sa_find_group_handle(group);
30174327Sdougm 	if (impl_handle == NULL || impl_handle->scfhandle == NULL)
30184327Sdougm 		return (SA_SYSTEM_ERR);
30193910Sdougm 	scf_handle = impl_handle->scfhandle;
30203034Sdougm 	name = sa_get_property_attr(prop, "type");
30213034Sdougm 	valstr = sa_get_property_attr(prop, "value");
30223034Sdougm 	entry = scf_entry_create(scf_handle->handle);
30233034Sdougm 	opttype = is_nodetype((void *)optionset, "optionset");
30243034Sdougm 
3025*5331Samw 	/*
3026*5331Samw 	 * Check for share vs. resource since they need slightly
3027*5331Samw 	 * different treatment given the hierarchy.
3028*5331Samw 	 */
30293034Sdougm 	if (valstr != NULL && entry != NULL) {
30304327Sdougm 		if (sa_is_share(group)) {
30314327Sdougm 			parent = sa_get_parent_group(group);
3032*5331Samw 			share = (sa_share_t)group;
30334327Sdougm 			if (parent != NULL)
30344327Sdougm 				iszfs = is_zfs_group(parent);
3035*5331Samw 		} else if (sa_is_resource(group)) {
3036*5331Samw 			share = sa_get_parent_group(group);
3037*5331Samw 			if (share != NULL)
3038*5331Samw 				parent = sa_get_parent_group(share);
30394327Sdougm 		} else {
30404327Sdougm 			iszfs = is_zfs_group(group);
30413034Sdougm 		}
30424327Sdougm 		if (!iszfs) {
30434327Sdougm 			if (scf_handle->trans == NULL) {
30444327Sdougm 				char oname[SA_STRSIZE];
30454327Sdougm 				char *groupname = NULL;
3046*5331Samw 				if (share != NULL) {
3047*5331Samw 					if (parent != NULL)
30484327Sdougm 						groupname =
30494327Sdougm 						    sa_get_group_attr(parent,
30504327Sdougm 						    "name");
3051*5331Samw 					id = sa_get_share_attr(
3052*5331Samw 					    (sa_share_t)share, "id");
30534327Sdougm 				} else {
30544327Sdougm 					groupname = sa_get_group_attr(group,
30554327Sdougm 					    "name");
30564327Sdougm 				}
30574327Sdougm 				if (groupname != NULL) {
30584327Sdougm 					ret = sa_get_instance(scf_handle,
30594327Sdougm 					    groupname);
30604327Sdougm 					sa_free_attr_string(groupname);
30614327Sdougm 				}
30624327Sdougm 				if (opttype)
30634327Sdougm 					(void) sa_optionset_name(optionset,
30644327Sdougm 					    oname, sizeof (oname), id);
30654327Sdougm 				else
30664327Sdougm 					(void) sa_security_name(optionset,
30674327Sdougm 					    oname, sizeof (oname), id);
30684327Sdougm 				ret = sa_start_transaction(scf_handle, oname);
30693910Sdougm 			}
30704327Sdougm 			if (ret == SA_OK) {
30714327Sdougm 				switch (type) {
30724327Sdougm 				case SA_PROP_OP_REMOVE:
30734327Sdougm 					ret = scf_transaction_property_delete(
30744327Sdougm 					    scf_handle->trans, entry, name);
30754327Sdougm 					break;
30764327Sdougm 				case SA_PROP_OP_ADD:
30774327Sdougm 				case SA_PROP_OP_UPDATE:
30784327Sdougm 					value = scf_value_create(
30794327Sdougm 					    scf_handle->handle);
30804327Sdougm 					ret = add_or_update(scf_handle, type,
30814327Sdougm 					    value, entry, name, valstr);
30824327Sdougm 					break;
30833034Sdougm 				}
30843034Sdougm 			}
30854327Sdougm 		} else {
30864327Sdougm 			/*
30874327Sdougm 			 * ZFS update. The calling function would have updated
30884327Sdougm 			 * the internal XML structure. Just need to flag it as
30894327Sdougm 			 * changed for ZFS.
30904327Sdougm 			 */
30914327Sdougm 			zfs_set_update((sa_share_t)group);
30924327Sdougm 		}
30933034Sdougm 	}
30943034Sdougm 
30953034Sdougm 	if (name != NULL)
30964327Sdougm 		sa_free_attr_string(name);
30973034Sdougm 	if (valstr != NULL)
30984327Sdougm 		sa_free_attr_string(valstr);
30993034Sdougm 	else if (entry != NULL)
31004327Sdougm 		scf_entry_destroy(entry);
31013034Sdougm 
31023034Sdougm 	if (ret == -1)
31034327Sdougm 		ret = SA_SYSTEM_ERR;
31043034Sdougm 
31053034Sdougm 	return (ret);
31063034Sdougm }
31073034Sdougm 
31083034Sdougm /*
31093034Sdougm  * sa_create_property(name, value)
31103034Sdougm  *
31113034Sdougm  * Create a new property with the specified name and value.
31123034Sdougm  */
31133034Sdougm 
31143034Sdougm sa_property_t
31153034Sdougm sa_create_property(char *name, char *value)
31163034Sdougm {
31173034Sdougm 	xmlNodePtr node;
31183034Sdougm 
31193034Sdougm 	node = xmlNewNode(NULL, (xmlChar *)"option");
31203034Sdougm 	if (node != NULL) {
31213034Sdougm 		xmlSetProp(node, (xmlChar *)"type", (xmlChar *)name);
31223034Sdougm 		xmlSetProp(node, (xmlChar *)"value", (xmlChar *)value);
31233034Sdougm 	}
31243034Sdougm 	return ((sa_property_t)node);
31253034Sdougm }
31263034Sdougm 
31273034Sdougm /*
31283034Sdougm  * sa_add_property(object, property)
31293034Sdougm  *
31303034Sdougm  * Add the specified property to the object. Issue the appropriate
31313034Sdougm  * transaction or mark a ZFS object as needing an update.
31323034Sdougm  */
31333034Sdougm 
31343034Sdougm int
31353034Sdougm sa_add_property(void *object, sa_property_t property)
31363034Sdougm {
31373034Sdougm 	int ret = SA_OK;
31383034Sdougm 	sa_group_t parent;
31393034Sdougm 	sa_group_t group;
31403034Sdougm 	char *proto;
31413034Sdougm 
31423034Sdougm 	proto = sa_get_optionset_attr(object, "type");
31433034Sdougm 	if (property != NULL) {
31444327Sdougm 		if ((ret = sa_valid_property(object, proto, property)) ==
31454327Sdougm 		    SA_OK) {
31464327Sdougm 			property = (sa_property_t)xmlAddChild(
31474327Sdougm 			    (xmlNodePtr)object, (xmlNodePtr)property);
31484327Sdougm 		} else {
31494327Sdougm 			if (proto != NULL)
31504327Sdougm 				sa_free_attr_string(proto);
31514327Sdougm 			return (ret);
31524327Sdougm 		}
31533034Sdougm 	}
31543034Sdougm 
31553034Sdougm 	if (proto != NULL)
31564327Sdougm 		sa_free_attr_string(proto);
31573034Sdougm 
31583034Sdougm 	parent = sa_get_parent_group(object);
3159*5331Samw 	if (!sa_is_persistent(parent))
31604327Sdougm 		return (ret);
3161*5331Samw 
3162*5331Samw 	if (sa_is_resource(parent)) {
3163*5331Samw 		/*
3164*5331Samw 		 * Resources are children of share.  Need to go up two
3165*5331Samw 		 * levels to find the group but the parent needs to be
3166*5331Samw 		 * the share at this point in order to get the "id".
3167*5331Samw 		 */
3168*5331Samw 		parent = sa_get_parent_group(parent);
3169*5331Samw 		group = sa_get_parent_group(parent);
3170*5331Samw 	} else if (sa_is_share(parent)) {
3171*5331Samw 		group = sa_get_parent_group(parent);
3172*5331Samw 	} else {
3173*5331Samw 		group = parent;
31743034Sdougm 	}
31753034Sdougm 
31764327Sdougm 	if (property == NULL) {
31774327Sdougm 		ret = SA_NO_MEMORY;
31784327Sdougm 	} else {
31794327Sdougm 		char oname[SA_STRSIZE];
31803034Sdougm 
31814327Sdougm 		if (!is_zfs_group(group)) {
31824327Sdougm 			char *id = NULL;
31834327Sdougm 			sa_handle_impl_t impl_handle;
31844327Sdougm 			scfutilhandle_t  *scf_handle;
31853910Sdougm 
31864327Sdougm 			impl_handle = (sa_handle_impl_t)sa_find_group_handle(
31874327Sdougm 			    group);
31884327Sdougm 			if (impl_handle == NULL ||
31894327Sdougm 			    impl_handle->scfhandle == NULL)
31904327Sdougm 				ret = SA_SYSTEM_ERR;
31914327Sdougm 			if (ret == SA_OK) {
31924327Sdougm 				scf_handle = impl_handle->scfhandle;
31934327Sdougm 				if (sa_is_share((sa_group_t)parent)) {
31944327Sdougm 					id = sa_get_share_attr(
31954327Sdougm 					    (sa_share_t)parent, "id");
31964327Sdougm 				}
31974327Sdougm 				if (scf_handle->trans == NULL) {
31984327Sdougm 					if (is_nodetype(object, "optionset")) {
31994327Sdougm 						(void) sa_optionset_name(
32004327Sdougm 						    (sa_optionset_t)object,
32014327Sdougm 						    oname, sizeof (oname), id);
32024327Sdougm 					} else {
32034327Sdougm 						(void) sa_security_name(
32044327Sdougm 						    (sa_optionset_t)object,
32054327Sdougm 						    oname, sizeof (oname), id);
32064327Sdougm 					}
32074327Sdougm 					ret = sa_start_transaction(scf_handle,
32084327Sdougm 					    oname);
32094327Sdougm 				}
32104327Sdougm 				if (ret == SA_OK) {
32114327Sdougm 					char *name;
32124327Sdougm 					char *value;
32134327Sdougm 					name = sa_get_property_attr(property,
32144327Sdougm 					    "type");
32154327Sdougm 					value = sa_get_property_attr(property,
32164327Sdougm 					    "value");
32174327Sdougm 					if (name != NULL && value != NULL) {
32184327Sdougm 						if (scf_handle->scf_state ==
32194327Sdougm 						    SCH_STATE_INIT) {
32204327Sdougm 							ret = sa_set_property(
32214327Sdougm 							    scf_handle, name,
32224327Sdougm 							    value);
32234327Sdougm 						}
32244327Sdougm 					} else {
32254327Sdougm 						ret = SA_CONFIG_ERR;
32264327Sdougm 					}
32274327Sdougm 					if (name != NULL)
32284327Sdougm 						sa_free_attr_string(
32294327Sdougm 						    name);
32304327Sdougm 					if (value != NULL)
32314327Sdougm 						sa_free_attr_string(value);
32324327Sdougm 				}
32334327Sdougm 				if (id != NULL)
32344327Sdougm 					sa_free_attr_string(id);
32354327Sdougm 			}
32364327Sdougm 		} else {
32374327Sdougm 			/*
32384327Sdougm 			 * ZFS is a special case. We do want
32394327Sdougm 			 * to allow editing property/security
32404327Sdougm 			 * lists since we can have a better
32414327Sdougm 			 * syntax and we also want to keep
32424327Sdougm 			 * things consistent when possible.
32434327Sdougm 			 *
32444327Sdougm 			 * Right now, we defer until the
32454327Sdougm 			 * sa_commit_properties so we can get
32464327Sdougm 			 * them all at once. We do need to
32474327Sdougm 			 * mark the share as "changed"
32484327Sdougm 			 */
32494327Sdougm 			zfs_set_update((sa_share_t)parent);
32503034Sdougm 		}
32513034Sdougm 	}
32523034Sdougm 	return (ret);
32533034Sdougm }
32543034Sdougm 
32553034Sdougm /*
32563034Sdougm  * sa_remove_property(property)
32573034Sdougm  *
32583034Sdougm  * Remove the specied property from its containing object. Update the
32593034Sdougm  * repository as appropriate.
32603034Sdougm  */
32613034Sdougm 
32623034Sdougm int
32633034Sdougm sa_remove_property(sa_property_t property)
32643034Sdougm {
32653034Sdougm 	int ret = SA_OK;
32663034Sdougm 
32673034Sdougm 	if (property != NULL) {
32683034Sdougm 		sa_optionset_t optionset;
32693034Sdougm 		sa_group_t group;
32703034Sdougm 		optionset = sa_get_property_parent(property);
32713034Sdougm 		if (optionset != NULL) {
32724327Sdougm 			group = sa_get_optionset_parent(optionset);
32734327Sdougm 			if (group != NULL) {
32744327Sdougm 				ret = sa_set_prop_by_prop(optionset, group,
32754327Sdougm 				    property, SA_PROP_OP_REMOVE);
32764327Sdougm 			}
32773034Sdougm 		}
32783034Sdougm 		xmlUnlinkNode((xmlNodePtr)property);
32793034Sdougm 		xmlFreeNode((xmlNodePtr)property);
32803034Sdougm 	} else {
32814327Sdougm 		ret = SA_NO_SUCH_PROP;
32823034Sdougm 	}
32833034Sdougm 	return (ret);
32843034Sdougm }
32853034Sdougm 
32863034Sdougm /*
32873034Sdougm  * sa_update_property(property, value)
32883034Sdougm  *
32893034Sdougm  * Update the specified property to the new value.  If value is NULL,
32903034Sdougm  * we currently treat this as a remove.
32913034Sdougm  */
32923034Sdougm 
32933034Sdougm int
32943034Sdougm sa_update_property(sa_property_t property, char *value)
32953034Sdougm {
32963034Sdougm 	int ret = SA_OK;
32973034Sdougm 	if (value == NULL) {
32983034Sdougm 		return (sa_remove_property(property));
32993034Sdougm 	} else {
33003034Sdougm 		sa_optionset_t optionset;
33013034Sdougm 		sa_group_t group;
33023034Sdougm 		set_node_attr((void *)property, "value", value);
33033034Sdougm 		optionset = sa_get_property_parent(property);
33043034Sdougm 		if (optionset != NULL) {
33054327Sdougm 			group = sa_get_optionset_parent(optionset);
33064327Sdougm 			if (group != NULL) {
33074327Sdougm 				ret = sa_set_prop_by_prop(optionset, group,
33084327Sdougm 				    property, SA_PROP_OP_UPDATE);
33094327Sdougm 			}
33103034Sdougm 		} else {
33114327Sdougm 			ret = SA_NO_SUCH_PROP;
33123034Sdougm 		}
33133034Sdougm 	}
33143034Sdougm 	return (ret);
33153034Sdougm }
33163034Sdougm 
33173034Sdougm /*
33183034Sdougm  * sa_get_protocol_property(propset, prop)
33193034Sdougm  *
33203034Sdougm  * Get the specified protocol specific property. These are global to
33213034Sdougm  * the protocol and not specific to a group or share.
33223034Sdougm  */
33233034Sdougm 
33243034Sdougm sa_property_t
33253034Sdougm sa_get_protocol_property(sa_protocol_properties_t propset, char *prop)
33263034Sdougm {
33273034Sdougm 	xmlNodePtr node = (xmlNodePtr)propset;
33283034Sdougm 	xmlChar *value = NULL;
33293034Sdougm 
33303034Sdougm 	for (node = node->children; node != NULL;
33314327Sdougm 	    node = node->next) {
33324327Sdougm 		if (xmlStrcmp(node->name, (xmlChar *)"option") == 0) {
33334327Sdougm 			if (prop == NULL)
33344327Sdougm 				break;
33354327Sdougm 			value = xmlGetProp(node, (xmlChar *)"type");
33364327Sdougm 			if (value != NULL &&
33374327Sdougm 			    xmlStrcasecmp(value, (xmlChar *)prop) == 0) {
33384327Sdougm 				break;
33394327Sdougm 			}
33404327Sdougm 			if (value != NULL) {
33414327Sdougm 				xmlFree(value);
33424327Sdougm 				value = NULL;
33434327Sdougm 			}
33443034Sdougm 		}
33453034Sdougm 	}
33463034Sdougm 	if (value != NULL)
33473034Sdougm 		xmlFree(value);
33483034Sdougm 	if (node != NULL && xmlStrcmp(node->name, (xmlChar *)"option") != 0) {
33494327Sdougm 		/*
33504327Sdougm 		 * avoid a non option node -- it is possible to be a
33514327Sdougm 		 * text node
33524327Sdougm 		 */
33534327Sdougm 		node = NULL;
33543034Sdougm 	}
33553034Sdougm 	return ((sa_property_t)node);
33563034Sdougm }
33573034Sdougm 
33583034Sdougm /*
33593034Sdougm  * sa_get_next_protocol_property(prop)
33603034Sdougm  *
33613034Sdougm  * Get the next protocol specific property in the list.
33623034Sdougm  */
33633034Sdougm 
33643034Sdougm sa_property_t
33653034Sdougm sa_get_next_protocol_property(sa_property_t prop)
33663034Sdougm {
33673034Sdougm 	xmlNodePtr node;
33683034Sdougm 
33693034Sdougm 	for (node = ((xmlNodePtr)prop)->next; node != NULL;
33704327Sdougm 	    node = node->next) {
33713034Sdougm 		if (xmlStrcmp(node->name, (xmlChar *)"option") == 0) {
33723034Sdougm 			break;
33733034Sdougm 		}
33743034Sdougm 	}
33753034Sdougm 	return ((sa_property_t)node);
33763034Sdougm }
33773034Sdougm 
33783034Sdougm /*
33793034Sdougm  * sa_set_protocol_property(prop, value)
33803034Sdougm  *
33813034Sdougm  * Set the specified property to have the new value.  The protocol
33823034Sdougm  * specific plugin will then be called to update the property.
33833034Sdougm  */
33843034Sdougm 
33853034Sdougm int
33863034Sdougm sa_set_protocol_property(sa_property_t prop, char *value)
33873034Sdougm {
33883034Sdougm 	sa_protocol_properties_t propset;
33893034Sdougm 	char *proto;
33903034Sdougm 	int ret = SA_INVALID_PROTOCOL;
33913034Sdougm 
33923034Sdougm 	propset = ((xmlNodePtr)prop)->parent;
33933034Sdougm 	if (propset != NULL) {
33944327Sdougm 		proto = sa_get_optionset_attr(propset, "type");
33954327Sdougm 		if (proto != NULL) {
33964327Sdougm 			set_node_attr((xmlNodePtr)prop, "value", value);
33974327Sdougm 			ret = sa_proto_set_property(proto, prop);
33984327Sdougm 			sa_free_attr_string(proto);
33994327Sdougm 		}
34003034Sdougm 	}
34013034Sdougm 	return (ret);
34023034Sdougm }
34033034Sdougm 
34043034Sdougm /*
34053034Sdougm  * sa_add_protocol_property(propset, prop)
34063034Sdougm  *
3407*5331Samw  * Add a new property to the protocol specific property set.
34083034Sdougm  */
34093034Sdougm 
34103034Sdougm int
34113034Sdougm sa_add_protocol_property(sa_protocol_properties_t propset, sa_property_t prop)
34123034Sdougm {
34133034Sdougm 	xmlNodePtr node;
34143034Sdougm 
34153034Sdougm 	/* should check for legitimacy */
34163034Sdougm 	node = xmlAddChild((xmlNodePtr)propset, (xmlNodePtr)prop);
34173034Sdougm 	if (node != NULL)
34184327Sdougm 		return (SA_OK);
34193034Sdougm 	return (SA_NO_MEMORY);
34203034Sdougm }
34213034Sdougm 
34223034Sdougm /*
34233034Sdougm  * sa_create_protocol_properties(proto)
34243034Sdougm  *
3425*5331Samw  * Create a protocol specific property set.
34263034Sdougm  */
34273034Sdougm 
34283034Sdougm sa_protocol_properties_t
34293034Sdougm sa_create_protocol_properties(char *proto)
34303034Sdougm {
34313034Sdougm 	xmlNodePtr node;
34324327Sdougm 
34333034Sdougm 	node = xmlNewNode(NULL, (xmlChar *)"propertyset");
34344327Sdougm 	if (node != NULL)
34354327Sdougm 		xmlSetProp(node, (xmlChar *)"type", (xmlChar *)proto);
34363034Sdougm 	return (node);
34373034Sdougm }
3438*5331Samw 
3439*5331Samw /*
3440*5331Samw  * sa_get_share_resource(share, resource)
3441*5331Samw  *
3442*5331Samw  * Get the named resource from the share, if it exists. If resource is
3443*5331Samw  * NULL, get the first resource.
3444*5331Samw  */
3445*5331Samw 
3446*5331Samw sa_resource_t
3447*5331Samw sa_get_share_resource(sa_share_t share, char *resource)
3448*5331Samw {
3449*5331Samw 	xmlNodePtr node = NULL;
3450*5331Samw 	xmlChar *name;
3451*5331Samw 
3452*5331Samw 	if (share != NULL) {
3453*5331Samw 		for (node = ((xmlNodePtr)share)->children; node != NULL;
3454*5331Samw 		    node = node->next) {
3455*5331Samw 			if (xmlStrcmp(node->name, (xmlChar *)"resource") == 0) {
3456*5331Samw 				if (resource == NULL) {
3457*5331Samw 					/*
3458*5331Samw 					 * We are looking for the first
3459*5331Samw 					 * resource node and not a names
3460*5331Samw 					 * resource.
3461*5331Samw 					 */
3462*5331Samw 					break;
3463*5331Samw 				} else {
3464*5331Samw 					/* is it the correct share? */
3465*5331Samw 					name = xmlGetProp(node,
3466*5331Samw 					    (xmlChar *)"name");
3467*5331Samw 					if (name != NULL &&
3468*5331Samw 					    xmlStrcasecmp(name,
3469*5331Samw 					    (xmlChar *)resource) == 0) {
3470*5331Samw 						xmlFree(name);
3471*5331Samw 						break;
3472*5331Samw 					}
3473*5331Samw 					xmlFree(name);
3474*5331Samw 				}
3475*5331Samw 			}
3476*5331Samw 		}
3477*5331Samw 	}
3478*5331Samw 	return ((sa_resource_t)node);
3479*5331Samw }
3480*5331Samw 
3481*5331Samw /*
3482*5331Samw  * sa_get_next_resource(resource)
3483*5331Samw  *	Return the next share following the specified share
3484*5331Samw  *	from the internal list of shares. Returns NULL if there
3485*5331Samw  *	are no more shares.  The list is relative to the same
3486*5331Samw  *	group.
3487*5331Samw  */
3488*5331Samw sa_share_t
3489*5331Samw sa_get_next_resource(sa_resource_t resource)
3490*5331Samw {
3491*5331Samw 	xmlNodePtr node = NULL;
3492*5331Samw 
3493*5331Samw 	if (resource != NULL) {
3494*5331Samw 		for (node = ((xmlNodePtr)resource)->next; node != NULL;
3495*5331Samw 		    node = node->next) {
3496*5331Samw 			if (xmlStrcmp(node->name, (xmlChar *)"resource") == 0)
3497*5331Samw 				break;
3498*5331Samw 		}
3499*5331Samw 	}
3500*5331Samw 	return ((sa_share_t)node);
3501*5331Samw }
3502*5331Samw 
3503*5331Samw /*
3504*5331Samw  * _sa_get_next_resource_index(share)
3505*5331Samw  *
3506*5331Samw  * get the next resource index number (one greater then current largest)
3507*5331Samw  */
3508*5331Samw 
3509*5331Samw static int
3510*5331Samw _sa_get_next_resource_index(sa_share_t share)
3511*5331Samw {
3512*5331Samw 	sa_resource_t resource;
3513*5331Samw 	int index = 0;
3514*5331Samw 	char *id;
3515*5331Samw 
3516*5331Samw 	for (resource = sa_get_share_resource(share, NULL);
3517*5331Samw 	    resource != NULL;
3518*5331Samw 	    resource = sa_get_next_resource(resource)) {
3519*5331Samw 		id = get_node_attr((void *)resource, "id");
3520*5331Samw 		if (id != NULL) {
3521*5331Samw 			int val;
3522*5331Samw 			val = atoi(id);
3523*5331Samw 			if (val > index)
3524*5331Samw 				index = val;
3525*5331Samw 			sa_free_attr_string(id);
3526*5331Samw 		}
3527*5331Samw 	}
3528*5331Samw 	return (index + 1);
3529*5331Samw }
3530*5331Samw 
3531*5331Samw 
3532*5331Samw /*
3533*5331Samw  * sa_add_resource(share, resource, persist, &err)
3534*5331Samw  *
3535*5331Samw  * Adds a new resource name associated with share. The resource name
3536*5331Samw  * must be unique in the system and will be case insensitive (eventually).
3537*5331Samw  */
3538*5331Samw 
3539*5331Samw sa_resource_t
3540*5331Samw sa_add_resource(sa_share_t share, char *resource, int persist, int *error)
3541*5331Samw {
3542*5331Samw 	xmlNodePtr node;
3543*5331Samw 	int err = SA_OK;
3544*5331Samw 	sa_resource_t res;
3545*5331Samw 	sa_group_t group;
3546*5331Samw 	sa_handle_t handle;
3547*5331Samw 	char istring[8]; /* just big enough for an integer value */
3548*5331Samw 	int index;
3549*5331Samw 
3550*5331Samw 	group = sa_get_parent_group(share);
3551*5331Samw 	handle = sa_find_group_handle(group);
3552*5331Samw 	res = sa_find_resource(handle, resource);
3553*5331Samw 	if (res != NULL) {
3554*5331Samw 		err = SA_DUPLICATE_NAME;
3555*5331Samw 		res = NULL;
3556*5331Samw 	} else {
3557*5331Samw 		node = xmlNewChild((xmlNodePtr)share, NULL,
3558*5331Samw 		    (xmlChar *)"resource", NULL);
3559*5331Samw 		if (node != NULL) {
3560*5331Samw 			xmlSetProp(node, (xmlChar *)"name",
3561*5331Samw 			    (xmlChar *)resource);
3562*5331Samw 			xmlSetProp(node, (xmlChar *)"type", persist ?
3563*5331Samw 			    (xmlChar *)"persist" : (xmlChar *)"transient");
3564*5331Samw 			if (persist != SA_SHARE_TRANSIENT) {
3565*5331Samw 				index = _sa_get_next_resource_index(share);
3566*5331Samw 				(void) snprintf(istring, sizeof (istring), "%d",
3567*5331Samw 				    index);
3568*5331Samw 				xmlSetProp(node, (xmlChar *)"id",
3569*5331Samw 				    (xmlChar *)istring);
3570*5331Samw 				if (!sa_group_is_zfs(group) &&
3571*5331Samw 				    sa_is_persistent((sa_group_t)share)) {
3572*5331Samw 					/* ZFS doesn't use resource names */
3573*5331Samw 					sa_handle_impl_t ihandle;
3574*5331Samw 					ihandle = (sa_handle_impl_t)
3575*5331Samw 					    sa_find_group_handle(
3576*5331Samw 					    group);
3577*5331Samw 					if (ihandle != NULL)
3578*5331Samw 						err = sa_commit_share(
3579*5331Samw 						    ihandle->scfhandle, group,
3580*5331Samw 						    share);
3581*5331Samw 					else
3582*5331Samw 						err = SA_SYSTEM_ERR;
3583*5331Samw 				}
3584*5331Samw 			}
3585*5331Samw 		}
3586*5331Samw 	}
3587*5331Samw 	if (error != NULL)
3588*5331Samw 		*error = err;
3589*5331Samw 	return ((sa_resource_t)node);
3590*5331Samw }
3591*5331Samw 
3592*5331Samw /*
3593*5331Samw  * sa_remove_resource(resource)
3594*5331Samw  *
3595*5331Samw  * Remove the resource name from the share (and the system)
3596*5331Samw  */
3597*5331Samw 
3598*5331Samw int
3599*5331Samw sa_remove_resource(sa_resource_t resource)
3600*5331Samw {
3601*5331Samw 	sa_share_t share;
3602*5331Samw 	sa_group_t group;
3603*5331Samw 	char *type;
3604*5331Samw 	int ret = SA_OK;
3605*5331Samw 	int transient = 0;
3606*5331Samw 
3607*5331Samw 	share = sa_get_resource_parent(resource);
3608*5331Samw 	type = sa_get_share_attr(share, "type");
3609*5331Samw 	group = sa_get_parent_group(share);
3610*5331Samw 
3611*5331Samw 
3612*5331Samw 	if (type != NULL) {
3613*5331Samw 		if (strcmp(type, "persist") != 0)
3614*5331Samw 			transient = 1;
3615*5331Samw 		sa_free_attr_string(type);
3616*5331Samw 	}
3617*5331Samw 
3618*5331Samw 	/* Remove from the share */
3619*5331Samw 	xmlUnlinkNode((xmlNode *)resource);
3620*5331Samw 	xmlFreeNode((xmlNode *)resource);
3621*5331Samw 
3622*5331Samw 	/* only do SMF action if permanent and not ZFS */
3623*5331Samw 	if (!transient && !sa_group_is_zfs(group)) {
3624*5331Samw 		sa_handle_impl_t ihandle;
3625*5331Samw 		ihandle = (sa_handle_impl_t)sa_find_group_handle(group);
3626*5331Samw 		if (ihandle != NULL)
3627*5331Samw 			ret = sa_commit_share(ihandle->scfhandle, group, share);
3628*5331Samw 		else
3629*5331Samw 			ret = SA_SYSTEM_ERR;
3630*5331Samw 	}
3631*5331Samw 	return (ret);
3632*5331Samw }
3633*5331Samw 
3634*5331Samw /*
3635*5331Samw  * proto_resource_rename(handle, group, resource, newname)
3636*5331Samw  *
3637*5331Samw  * Helper function for sa_rename_resource that notifies the protocol
3638*5331Samw  * of a resource name change prior to a config repository update.
3639*5331Samw  */
3640*5331Samw static int
3641*5331Samw proto_rename_resource(sa_handle_t handle, sa_group_t group,
3642*5331Samw     sa_resource_t resource, char *newname)
3643*5331Samw {
3644*5331Samw 	sa_optionset_t optionset;
3645*5331Samw 	int ret = SA_OK;
3646*5331Samw 	int err;
3647*5331Samw 
3648*5331Samw 	for (optionset = sa_get_optionset(group, NULL);
3649*5331Samw 	    optionset != NULL;
3650*5331Samw 	    optionset = sa_get_next_optionset(optionset)) {
3651*5331Samw 		char *type;
3652*5331Samw 		type = sa_get_optionset_attr(optionset, "type");
3653*5331Samw 		if (type != NULL) {
3654*5331Samw 			err = sa_proto_rename_resource(handle, type, resource,
3655*5331Samw 			    newname);
3656*5331Samw 			if (err != SA_OK)
3657*5331Samw 				ret = err;
3658*5331Samw 			sa_free_attr_string(type);
3659*5331Samw 		}
3660*5331Samw 	}
3661*5331Samw 	return (ret);
3662*5331Samw }
3663*5331Samw 
3664*5331Samw /*
3665*5331Samw  * sa_rename_resource(resource, newname)
3666*5331Samw  *
3667*5331Samw  * Rename the resource to the new name, if it is unique.
3668*5331Samw  */
3669*5331Samw 
3670*5331Samw int
3671*5331Samw sa_rename_resource(sa_resource_t resource, char *newname)
3672*5331Samw {
3673*5331Samw 	sa_share_t share;
3674*5331Samw 	sa_group_t group = NULL;
3675*5331Samw 	sa_resource_t target;
3676*5331Samw 	int ret = SA_CONFIG_ERR;
3677*5331Samw 	sa_handle_t handle = NULL;
3678*5331Samw 
3679*5331Samw 	share = sa_get_resource_parent(resource);
3680*5331Samw 	if (share == NULL)
3681*5331Samw 		return (ret);
3682*5331Samw 
3683*5331Samw 	group = sa_get_parent_group(share);
3684*5331Samw 	if (group == NULL)
3685*5331Samw 		return (ret);
3686*5331Samw 
3687*5331Samw 	handle = (sa_handle_impl_t)sa_find_group_handle(group);
3688*5331Samw 	if (handle == NULL)
3689*5331Samw 		return (ret);
3690*5331Samw 
3691*5331Samw 	target = sa_find_resource(handle, newname);
3692*5331Samw 	if (target != NULL) {
3693*5331Samw 		ret = SA_DUPLICATE_NAME;
3694*5331Samw 	} else {
3695*5331Samw 		/*
3696*5331Samw 		 * Everything appears to be valid at this
3697*5331Samw 		 * point. Change the name of the active share and then
3698*5331Samw 		 * update the share in the appropriate repository.
3699*5331Samw 		 */
3700*5331Samw 		ret = proto_rename_resource(handle, group, resource, newname);
3701*5331Samw 		set_node_attr(resource, "name", newname);
3702*5331Samw 		if (!sa_group_is_zfs(group) &&
3703*5331Samw 		    sa_is_persistent((sa_group_t)share)) {
3704*5331Samw 			sa_handle_impl_t ihandle = (sa_handle_impl_t)handle;
3705*5331Samw 			ret = sa_commit_share(ihandle->scfhandle, group,
3706*5331Samw 			    share);
3707*5331Samw 		}
3708*5331Samw 	}
3709*5331Samw 	return (ret);
3710*5331Samw }
3711*5331Samw 
3712*5331Samw /*
3713*5331Samw  * sa_get_resource_attr(resource, tag)
3714*5331Samw  *
3715*5331Samw  * Get the named attribute of the resource. "name" and "id" are
3716*5331Samw  * currently defined.  NULL if tag not defined.
3717*5331Samw  */
3718*5331Samw 
3719*5331Samw char *
3720*5331Samw sa_get_resource_attr(sa_resource_t resource, char *tag)
3721*5331Samw {
3722*5331Samw 	return (get_node_attr((void *)resource, tag));
3723*5331Samw }
3724*5331Samw 
3725*5331Samw /*
3726*5331Samw  * sa_set_resource_attr(resource, tag, value)
3727*5331Samw  *
3728*5331Samw  * Get the named attribute of the resource. "name" and "id" are
3729*5331Samw  * currently defined.  NULL if tag not defined. Currently we don't do
3730*5331Samw  * much, but additional checking may be needed in the future.
3731*5331Samw  */
3732*5331Samw 
3733*5331Samw int
3734*5331Samw sa_set_resource_attr(sa_resource_t resource, char *tag, char *value)
3735*5331Samw {
3736*5331Samw 	set_node_attr((void *)resource, tag, value);
3737*5331Samw 	return (SA_OK);
3738*5331Samw }
3739*5331Samw 
3740*5331Samw /*
3741*5331Samw  * sa_get_resource_parent(resource_t)
3742*5331Samw  *
3743*5331Samw  * Returns the share associated with the resource.
3744*5331Samw  */
3745*5331Samw 
3746*5331Samw sa_share_t
3747*5331Samw sa_get_resource_parent(sa_resource_t resource)
3748*5331Samw {
3749*5331Samw 	sa_share_t share = NULL;
3750*5331Samw 
3751*5331Samw 	if (resource != NULL)
3752*5331Samw 		share = (sa_share_t)((xmlNodePtr)resource)->parent;
3753*5331Samw 	return (share);
3754*5331Samw }
3755*5331Samw 
3756*5331Samw /*
3757*5331Samw  * find_resource(group, name)
3758*5331Samw  *
3759*5331Samw  * Find the resource within the group.
3760*5331Samw  */
3761*5331Samw 
3762*5331Samw static sa_resource_t
3763*5331Samw find_resource(sa_group_t group, char *resname)
3764*5331Samw {
3765*5331Samw 	sa_share_t share;
3766*5331Samw 	sa_resource_t resource = NULL;
3767*5331Samw 	char *name;
3768*5331Samw 
3769*5331Samw 	/* Iterate over all the shares and resources in the group. */
3770*5331Samw 	for (share = sa_get_share(group, NULL);
3771*5331Samw 	    share != NULL && resource == NULL;
3772*5331Samw 	    share = sa_get_next_share(share)) {
3773*5331Samw 		for (resource = sa_get_share_resource(share, NULL);
3774*5331Samw 		    resource != NULL;
3775*5331Samw 		    resource = sa_get_next_resource(resource)) {
3776*5331Samw 			name = sa_get_resource_attr(resource, "name");
3777*5331Samw 			if (name != NULL && xmlStrcasecmp((xmlChar*)name,
3778*5331Samw 			    (xmlChar*)resname) == 0) {
3779*5331Samw 				sa_free_attr_string(name);
3780*5331Samw 				break;
3781*5331Samw 			}
3782*5331Samw 			if (name != NULL) {
3783*5331Samw 				sa_free_attr_string(name);
3784*5331Samw 			}
3785*5331Samw 		}
3786*5331Samw 	}
3787*5331Samw 	return (resource);
3788*5331Samw }
3789*5331Samw 
3790*5331Samw /*
3791*5331Samw  * sa_find_resource(name)
3792*5331Samw  *
3793*5331Samw  * Find the named resource in the system.
3794*5331Samw  */
3795*5331Samw 
3796*5331Samw sa_resource_t
3797*5331Samw sa_find_resource(sa_handle_t handle, char *name)
3798*5331Samw {
3799*5331Samw 	sa_group_t group;
3800*5331Samw 	sa_group_t zgroup;
3801*5331Samw 	sa_resource_t resource = NULL;
3802*5331Samw 
3803*5331Samw 	/*
3804*5331Samw 	 * Iterate over all groups and zfs subgroups and check for
3805*5331Samw 	 * resource name in them.
3806*5331Samw 	 */
3807*5331Samw 	for (group = sa_get_group(handle, NULL); group != NULL;
3808*5331Samw 	    group = sa_get_next_group(group)) {
3809*5331Samw 
3810*5331Samw 		if (is_zfs_group(group)) {
3811*5331Samw 			for (zgroup =
3812*5331Samw 			    (sa_group_t)_sa_get_child_node((xmlNodePtr)group,
3813*5331Samw 			    (xmlChar *)"group");
3814*5331Samw 			    zgroup != NULL && resource == NULL;
3815*5331Samw 			    zgroup = sa_get_next_group(zgroup)) {
3816*5331Samw 				resource = find_resource(zgroup, name);
3817*5331Samw 			}
3818*5331Samw 		} else {
3819*5331Samw 			resource = find_resource(group, name);
3820*5331Samw 		}
3821*5331Samw 		if (resource != NULL)
3822*5331Samw 			break;
3823*5331Samw 	}
3824*5331Samw 	return (resource);
3825*5331Samw }
3826*5331Samw 
3827*5331Samw /*
3828*5331Samw  * sa_get_resource(group, resource)
3829*5331Samw  *
3830*5331Samw  * Search all the shares in the specified group for a share with a
3831*5331Samw  * resource name matching the one specified.
3832*5331Samw  *
3833*5331Samw  * In the future, it may be advantageous to allow group to be NULL and
3834*5331Samw  * search all groups but that isn't needed at present.
3835*5331Samw  */
3836*5331Samw 
3837*5331Samw sa_resource_t
3838*5331Samw sa_get_resource(sa_group_t group, char *resource)
3839*5331Samw {
3840*5331Samw 	sa_share_t share = NULL;
3841*5331Samw 	sa_resource_t res = NULL;
3842*5331Samw 
3843*5331Samw 	if (resource != NULL) {
3844*5331Samw 		for (share = sa_get_share(group, NULL);
3845*5331Samw 		    share != NULL && res == NULL;
3846*5331Samw 		    share = sa_get_next_share(share)) {
3847*5331Samw 			res = sa_get_share_resource(share, resource);
3848*5331Samw 		}
3849*5331Samw 	}
3850*5331Samw 	return (res);
3851*5331Samw }
3852*5331Samw 
3853*5331Samw /*
3854*5331Samw  * sa_enable_resource, protocol)
3855*5331Samw  *	Disable the specified share to the specified protocol.
3856*5331Samw  *	If protocol is NULL, then all protocols.
3857*5331Samw  */
3858*5331Samw int
3859*5331Samw sa_enable_resource(sa_resource_t resource, char *protocol)
3860*5331Samw {
3861*5331Samw 	int ret = SA_OK;
3862*5331Samw 	char **protocols;
3863*5331Samw 	int numproto;
3864*5331Samw 
3865*5331Samw 	if (protocol != NULL) {
3866*5331Samw 		ret = sa_proto_share_resource(protocol, resource);
3867*5331Samw 	} else {
3868*5331Samw 		/* need to do all protocols */
3869*5331Samw 		if ((numproto = sa_get_protocols(&protocols)) >= 0) {
3870*5331Samw 			int i, err;
3871*5331Samw 			for (i = 0; i < numproto; i++) {
3872*5331Samw 				err = sa_proto_share_resource(
3873*5331Samw 				    protocols[i], resource);
3874*5331Samw 				if (err != SA_OK)
3875*5331Samw 					ret = err;
3876*5331Samw 			}
3877*5331Samw 			free(protocols);
3878*5331Samw 		}
3879*5331Samw 	}
3880*5331Samw 	if (ret == SA_OK)
3881*5331Samw 		(void) sa_set_resource_attr(resource, "shared", NULL);
3882*5331Samw 
3883*5331Samw 	return (ret);
3884*5331Samw }
3885*5331Samw 
3886*5331Samw /*
3887*5331Samw  * sa_disable_resource(resource, protocol)
3888*5331Samw  *
3889*5331Samw  *	Disable the specified share for the specified protocol.  If
3890*5331Samw  *	protocol is NULL, then all protocols.  If the underlying
3891*5331Samw  *	protocol doesn't implement disable at the resource level, we
3892*5331Samw  *	disable at the share level.
3893*5331Samw  */
3894*5331Samw int
3895*5331Samw sa_disable_resource(sa_resource_t resource, char *protocol)
3896*5331Samw {
3897*5331Samw 	int ret = SA_OK;
3898*5331Samw 	char **protocols;
3899*5331Samw 	int numproto;
3900*5331Samw 
3901*5331Samw 	if (protocol != NULL) {
3902*5331Samw 		ret = sa_proto_unshare_resource(protocol, resource);
3903*5331Samw 		if (ret == SA_NOT_IMPLEMENTED) {
3904*5331Samw 			sa_share_t parent;
3905*5331Samw 			/*
3906*5331Samw 			 * The protocol doesn't implement unshare
3907*5331Samw 			 * resource. That implies that resource names are
3908*5331Samw 			 * simple aliases for this protocol so we need to
3909*5331Samw 			 * unshare the share.
3910*5331Samw 			 */
3911*5331Samw 			parent = sa_get_resource_parent(resource);
3912*5331Samw 			if (parent != NULL)
3913*5331Samw 				ret = sa_disable_share(parent, protocol);
3914*5331Samw 			else
3915*5331Samw 				ret = SA_CONFIG_ERR;
3916*5331Samw 		}
3917*5331Samw 	} else {
3918*5331Samw 		/* need to do all protocols */
3919*5331Samw 		if ((numproto = sa_get_protocols(&protocols)) >= 0) {
3920*5331Samw 			int i, err;
3921*5331Samw 			for (i = 0; i < numproto; i++) {
3922*5331Samw 				err = sa_proto_unshare_resource(protocols[i],
3923*5331Samw 				    resource);
3924*5331Samw 				if (err == SA_NOT_SUPPORTED) {
3925*5331Samw 					sa_share_t parent;
3926*5331Samw 					parent = sa_get_resource_parent(
3927*5331Samw 					    resource);
3928*5331Samw 					if (parent != NULL)
3929*5331Samw 						err = sa_disable_share(parent,
3930*5331Samw 						    protocols[i]);
3931*5331Samw 					else
3932*5331Samw 						err = SA_CONFIG_ERR;
3933*5331Samw 				}
3934*5331Samw 				if (err != SA_OK)
3935*5331Samw 					ret = err;
3936*5331Samw 			}
3937*5331Samw 			free(protocols);
3938*5331Samw 		}
3939*5331Samw 	}
3940*5331Samw 	if (ret == SA_OK)
3941*5331Samw 		(void) sa_set_resource_attr(resource, "shared", NULL);
3942*5331Samw 
3943*5331Samw 	return (ret);
3944*5331Samw }
3945*5331Samw 
3946*5331Samw /*
3947*5331Samw  * sa_set_resource_description(resource, content)
3948*5331Samw  *
3949*5331Samw  * Set the description of share to content.
3950*5331Samw  */
3951*5331Samw 
3952*5331Samw int
3953*5331Samw sa_set_resource_description(sa_resource_t resource, char *content)
3954*5331Samw {
3955*5331Samw 	xmlNodePtr node;
3956*5331Samw 	sa_group_t group;
3957*5331Samw 	sa_share_t share;
3958*5331Samw 	int ret = SA_OK;
3959*5331Samw 
3960*5331Samw 	for (node = ((xmlNodePtr)resource)->children;
3961*5331Samw 	    node != NULL;
3962*5331Samw 	    node = node->next) {
3963*5331Samw 		if (xmlStrcmp(node->name, (xmlChar *)"description") == 0) {
3964*5331Samw 			break;
3965*5331Samw 		}
3966*5331Samw 	}
3967*5331Samw 
3968*5331Samw 	/* no existing description but want to add */
3969*5331Samw 	if (node == NULL && content != NULL) {
3970*5331Samw 		/* add a description */
3971*5331Samw 		node = _sa_set_share_description(resource, content);
3972*5331Samw 	} else if (node != NULL && content != NULL) {
3973*5331Samw 		/* update a description */
3974*5331Samw 		xmlNodeSetContent(node, (xmlChar *)content);
3975*5331Samw 	} else if (node != NULL && content == NULL) {
3976*5331Samw 		/* remove an existing description */
3977*5331Samw 		xmlUnlinkNode(node);
3978*5331Samw 		xmlFreeNode(node);
3979*5331Samw 	}
3980*5331Samw 	share = sa_get_resource_parent(resource);
3981*5331Samw 	group = sa_get_parent_group(share);
3982*5331Samw 	if (group != NULL && sa_is_persistent(share)) {
3983*5331Samw 		sa_handle_impl_t impl_handle;
3984*5331Samw 		impl_handle = (sa_handle_impl_t)sa_find_group_handle(group);
3985*5331Samw 		if (impl_handle != NULL)
3986*5331Samw 			ret = sa_commit_share(impl_handle->scfhandle,
3987*5331Samw 			    group, share);
3988*5331Samw 		else
3989*5331Samw 			ret = SA_SYSTEM_ERR;
3990*5331Samw 	}
3991*5331Samw 	return (ret);
3992*5331Samw }
3993*5331Samw 
3994*5331Samw /*
3995*5331Samw  * sa_get_resource_description(share)
3996*5331Samw  *
3997*5331Samw  * Return the description text for the specified share if it
3998*5331Samw  * exists. NULL if no description exists.
3999*5331Samw  */
4000*5331Samw 
4001*5331Samw char *
4002*5331Samw sa_get_resource_description(sa_resource_t resource)
4003*5331Samw {
4004*5331Samw 	xmlChar *description = NULL;
4005*5331Samw 	xmlNodePtr node;
4006*5331Samw 
4007*5331Samw 	for (node = ((xmlNodePtr)resource)->children; node != NULL;
4008*5331Samw 	    node = node->next) {
4009*5331Samw 		if (xmlStrcmp(node->name, (xmlChar *)"description") == 0)
4010*5331Samw 			break;
4011*5331Samw 	}
4012*5331Samw 	if (node != NULL) {
4013*5331Samw 		description = xmlNodeGetContent(node);
4014*5331Samw 		fixproblemchars((char *)description);
4015*5331Samw 	}
4016*5331Samw 	return ((char *)description);
4017*5331Samw }
4018