16185db85Sdougm /*
26185db85Sdougm  * CDDL HEADER START
36185db85Sdougm  *
46185db85Sdougm  * The contents of this file are subject to the terms of the
56185db85Sdougm  * Common Development and Distribution License (the "License").
66185db85Sdougm  * You may not use this file except in compliance with the License.
76185db85Sdougm  *
86185db85Sdougm  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
96185db85Sdougm  * or http://www.opensolaris.org/os/licensing.
106185db85Sdougm  * See the License for the specific language governing permissions
116185db85Sdougm  * and limitations under the License.
126185db85Sdougm  *
136185db85Sdougm  * When distributing Covered Code, include this CDDL HEADER in each
146185db85Sdougm  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
156185db85Sdougm  * If applicable, add the following below this CDDL HEADER, with the
166185db85Sdougm  * fields enclosed by brackets "[]" replaced with your own identifying
176185db85Sdougm  * information: Portions Copyright [yyyy] [name of copyright owner]
186185db85Sdougm  *
196185db85Sdougm  * CDDL HEADER END
206185db85Sdougm  */
216185db85Sdougm 
226185db85Sdougm /*
23148c5f43SAlan Wright  * Copyright (c) 2006, 2010, Oracle and/or its affiliates. All rights reserved.
24b8dc1b43SJoyce McIntosh  * Copyright 2018 Nexenta Systems, Inc.  All rights reserved.
258a981c33SDaniel Hoffman  * Copyright (c) 2016 by Delphix. All rights reserved.
266185db85Sdougm  */
276185db85Sdougm 
286185db85Sdougm /*
296185db85Sdougm  * Share control API
306185db85Sdougm  */
316185db85Sdougm #include <stdio.h>
326185db85Sdougm #include <string.h>
336185db85Sdougm #include <ctype.h>
346185db85Sdougm #include <sys/types.h>
356185db85Sdougm #include <sys/stat.h>
36a99982a7Sdougm #include <fcntl.h>
376185db85Sdougm #include <unistd.h>
386185db85Sdougm #include <libxml/parser.h>
396185db85Sdougm #include <libxml/tree.h>
406185db85Sdougm #include "libshare.h"
416185db85Sdougm #include "libshare_impl.h"
426185db85Sdougm #include <libscf.h>
436185db85Sdougm #include "scfutil.h"
446185db85Sdougm #include <ctype.h>
456185db85Sdougm #include <libintl.h>
46549ec3ffSdougm #include <thread.h>
47549ec3ffSdougm #include <synch.h>
48015408caSJean McCormack #include <errno.h>
496185db85Sdougm 
50a99982a7Sdougm #define	DFS_LOCK_FILE	"/etc/dfs/fstypes"
5157b448deSdougm #define	SA_STRSIZE	256	/* max string size for names */
52a99982a7Sdougm 
53da6c28aaSamw /*
54da6c28aaSamw  * internal object type values returned by sa_get_object_type()
55da6c28aaSamw  */
56da6c28aaSamw #define	SA_TYPE_UNKNOWN		0
57da6c28aaSamw #define	SA_TYPE_GROUP		1
58da6c28aaSamw #define	SA_TYPE_SHARE		2
59da6c28aaSamw #define	SA_TYPE_RESOURCE	3
60da6c28aaSamw #define	SA_TYPE_OPTIONSET	4
61da6c28aaSamw #define	SA_TYPE_ALTSPACE	5
62da6c28aaSamw 
636185db85Sdougm /*
646185db85Sdougm  * internal data structures
656185db85Sdougm  */
666185db85Sdougm 
676185db85Sdougm extern struct sa_proto_plugin *sap_proto_list;
686185db85Sdougm 
696185db85Sdougm /* current SMF/SVC repository handle */
70549ec3ffSdougm extern void getlegacyconfig(sa_handle_t, char *, xmlNodePtr *);
71549ec3ffSdougm extern int gettransients(sa_handle_impl_t, xmlNodePtr *);
728a981c33SDaniel Hoffman extern int get_one_transient(sa_handle_impl_t, xmlNodePtr *, char **, size_t);
736185db85Sdougm extern char *sa_fstype(char *);
746185db85Sdougm extern int sa_is_share(void *);
75da6c28aaSamw extern int sa_is_resource(void *);
766185db85Sdougm extern ssize_t scf_max_name_len; /* defined in scfutil during initialization */
776185db85Sdougm extern int sa_group_is_zfs(sa_group_t);
786185db85Sdougm extern int sa_path_is_zfs(char *);
796185db85Sdougm extern int sa_zfs_set_sharenfs(sa_group_t, char *, int);
80da6c28aaSamw extern int sa_zfs_set_sharesmb(sa_group_t, char *, int);
81549ec3ffSdougm extern void update_legacy_config(sa_handle_t);
826185db85Sdougm extern int issubdir(char *, char *);
8357b448deSdougm extern int sa_zfs_init(sa_handle_impl_t);
84549ec3ffSdougm extern void sa_zfs_fini(sa_handle_impl_t);
85a99982a7Sdougm extern void sablocksigs(sigset_t *);
86a99982a7Sdougm extern void saunblocksigs(sigset_t *);
87da6c28aaSamw static sa_group_t sa_get_optionset_parent(sa_optionset_t);
88da6c28aaSamw static char *get_node_attr(void *, char *);
895b6e0c46Sdougm extern void sa_update_sharetab_ts(sa_handle_t);
906185db85Sdougm 
91549ec3ffSdougm /*
92549ec3ffSdougm  * Data structures for finding/managing the document root to access
93549ec3ffSdougm  * handle mapping. The list isn't expected to grow very large so a
94549ec3ffSdougm  * simple list is acceptable. The purpose is to provide a way to start
95549ec3ffSdougm  * with a group or share and find the library handle needed for
96549ec3ffSdougm  * various operations.
97549ec3ffSdougm  */
98549ec3ffSdougm mutex_t sa_global_lock;
99549ec3ffSdougm struct doc2handle {
100549ec3ffSdougm 	struct doc2handle	*next;
101549ec3ffSdougm 	xmlNodePtr		root;
102549ec3ffSdougm 	sa_handle_impl_t	handle;
103549ec3ffSdougm };
104549ec3ffSdougm 
105148c5f43SAlan Wright mutex_t sa_dfstab_lock;
106148c5f43SAlan Wright 
10757b448deSdougm /* definitions used in a couple of property functions */
10857b448deSdougm #define	SA_PROP_OP_REMOVE	1
10957b448deSdougm #define	SA_PROP_OP_ADD		2
11057b448deSdougm #define	SA_PROP_OP_UPDATE	3
11157b448deSdougm 
112549ec3ffSdougm static struct doc2handle *sa_global_handles = NULL;
1136185db85Sdougm 
1146185db85Sdougm /* helper functions */
1156185db85Sdougm 
116549ec3ffSdougm /*
117549ec3ffSdougm  * sa_errorstr(err)
118549ec3ffSdougm  *
119549ec3ffSdougm  * convert an error value to an error string
120549ec3ffSdougm  */
121549ec3ffSdougm 
1226185db85Sdougm char *
sa_errorstr(int err)1236185db85Sdougm sa_errorstr(int err)
1246185db85Sdougm {
1256185db85Sdougm 	static char errstr[32];
1266185db85Sdougm 	char *ret = NULL;
1276185db85Sdougm 
1286185db85Sdougm 	switch (err) {
1296185db85Sdougm 	case SA_OK:
13057b448deSdougm 		ret = dgettext(TEXT_DOMAIN, "ok");
13157b448deSdougm 		break;
1326185db85Sdougm 	case SA_NO_SUCH_PATH:
13357b448deSdougm 		ret = dgettext(TEXT_DOMAIN, "path doesn't exist");
13457b448deSdougm 		break;
1356185db85Sdougm 	case SA_NO_MEMORY:
13657b448deSdougm 		ret = dgettext(TEXT_DOMAIN, "no memory");
13757b448deSdougm 		break;
1386185db85Sdougm 	case SA_DUPLICATE_NAME:
13957b448deSdougm 		ret = dgettext(TEXT_DOMAIN, "name in use");
14057b448deSdougm 		break;
1416185db85Sdougm 	case SA_BAD_PATH:
14257b448deSdougm 		ret = dgettext(TEXT_DOMAIN, "bad path");
14357b448deSdougm 		break;
1446185db85Sdougm 	case SA_NO_SUCH_GROUP:
14557b448deSdougm 		ret = dgettext(TEXT_DOMAIN, "no such group");
14657b448deSdougm 		break;
1476185db85Sdougm 	case SA_CONFIG_ERR:
14857b448deSdougm 		ret = dgettext(TEXT_DOMAIN, "configuration error");
14957b448deSdougm 		break;
1506185db85Sdougm 	case SA_SYSTEM_ERR:
15157b448deSdougm 		ret = dgettext(TEXT_DOMAIN, "system error");
15257b448deSdougm 		break;
1536185db85Sdougm 	case SA_SYNTAX_ERR:
15457b448deSdougm 		ret = dgettext(TEXT_DOMAIN, "syntax error");
15557b448deSdougm 		break;
1566185db85Sdougm 	case SA_NO_PERMISSION:
15757b448deSdougm 		ret = dgettext(TEXT_DOMAIN, "no permission");
15857b448deSdougm 		break;
1596185db85Sdougm 	case SA_BUSY:
16057b448deSdougm 		ret = dgettext(TEXT_DOMAIN, "busy");
16157b448deSdougm 		break;
1626185db85Sdougm 	case SA_NO_SUCH_PROP:
16357b448deSdougm 		ret = dgettext(TEXT_DOMAIN, "no such property");
16457b448deSdougm 		break;
1656185db85Sdougm 	case SA_INVALID_NAME:
16657b448deSdougm 		ret = dgettext(TEXT_DOMAIN, "invalid name");
16757b448deSdougm 		break;
1686185db85Sdougm 	case SA_INVALID_PROTOCOL:
16957b448deSdougm 		ret = dgettext(TEXT_DOMAIN, "invalid protocol");
17057b448deSdougm 		break;
1716185db85Sdougm 	case SA_NOT_ALLOWED:
17257b448deSdougm 		ret = dgettext(TEXT_DOMAIN, "operation not allowed");
17357b448deSdougm 		break;
1746185db85Sdougm 	case SA_BAD_VALUE:
17557b448deSdougm 		ret = dgettext(TEXT_DOMAIN, "bad property value");
17657b448deSdougm 		break;
1776185db85Sdougm 	case SA_INVALID_SECURITY:
17857b448deSdougm 		ret = dgettext(TEXT_DOMAIN, "invalid security type");
17957b448deSdougm 		break;
1806185db85Sdougm 	case SA_NO_SUCH_SECURITY:
18157b448deSdougm 		ret = dgettext(TEXT_DOMAIN, "security type not found");
18257b448deSdougm 		break;
1836185db85Sdougm 	case SA_VALUE_CONFLICT:
18457b448deSdougm 		ret = dgettext(TEXT_DOMAIN, "property value conflict");
18557b448deSdougm 		break;
1866185db85Sdougm 	case SA_NOT_IMPLEMENTED:
18757b448deSdougm 		ret = dgettext(TEXT_DOMAIN, "not implemented");
18857b448deSdougm 		break;
1896185db85Sdougm 	case SA_INVALID_PATH:
19057b448deSdougm 		ret = dgettext(TEXT_DOMAIN, "invalid path");
19157b448deSdougm 		break;
1926185db85Sdougm 	case SA_NOT_SUPPORTED:
19357b448deSdougm 		ret = dgettext(TEXT_DOMAIN, "operation not supported");
19457b448deSdougm 		break;
1956185db85Sdougm 	case SA_PROP_SHARE_ONLY:
19657b448deSdougm 		ret = dgettext(TEXT_DOMAIN, "property not valid for group");
19757b448deSdougm 		break;
1986185db85Sdougm 	case SA_NOT_SHARED:
19957b448deSdougm 		ret = dgettext(TEXT_DOMAIN, "not shared");
20057b448deSdougm 		break;
201da6c28aaSamw 	case SA_NO_SUCH_RESOURCE:
202da6c28aaSamw 		ret = dgettext(TEXT_DOMAIN, "no such resource");
203da6c28aaSamw 		break;
204da6c28aaSamw 	case SA_RESOURCE_REQUIRED:
205da6c28aaSamw 		ret = dgettext(TEXT_DOMAIN, "resource name required");
206da6c28aaSamw 		break;
207da6c28aaSamw 	case SA_MULTIPLE_ERROR:
208da6c28aaSamw 		ret = dgettext(TEXT_DOMAIN, "errors from multiple protocols");
209da6c28aaSamw 		break;
210da6c28aaSamw 	case SA_PATH_IS_SUBDIR:
211da6c28aaSamw 		ret = dgettext(TEXT_DOMAIN, "path is a subpath of share");
212da6c28aaSamw 		break;
213da6c28aaSamw 	case SA_PATH_IS_PARENTDIR:
214da6c28aaSamw 		ret = dgettext(TEXT_DOMAIN, "path is parent of a share");
215da6c28aaSamw 		break;
2164bff34e3Sthurlow 	case SA_NO_SECTION:
2174bff34e3Sthurlow 		ret = dgettext(TEXT_DOMAIN, "protocol requires a section");
2184bff34e3Sthurlow 		break;
2194bff34e3Sthurlow 	case SA_NO_PROPERTIES:
2204bff34e3Sthurlow 		ret = dgettext(TEXT_DOMAIN, "properties not found");
2214bff34e3Sthurlow 		break;
2224bff34e3Sthurlow 	case SA_NO_SUCH_SECTION:
2234bff34e3Sthurlow 		ret = dgettext(TEXT_DOMAIN, "section not found");
2244bff34e3Sthurlow 		break;
2254bff34e3Sthurlow 	case SA_PASSWORD_ENC:
2264bff34e3Sthurlow 		ret = dgettext(TEXT_DOMAIN, "passwords must be encrypted");
2274bff34e3Sthurlow 		break;
2280616fd7fSPavel Filipensky 	case SA_SHARE_EXISTS:
2290616fd7fSPavel Filipensky 		ret = dgettext(TEXT_DOMAIN, "path or file is already shared");
2300616fd7fSPavel Filipensky 		break;
2316185db85Sdougm 	default:
23257b448deSdougm 		(void) snprintf(errstr, sizeof (errstr),
23357b448deSdougm 		    dgettext(TEXT_DOMAIN, "unknown %d"), err);
23457b448deSdougm 		ret = errstr;
2356185db85Sdougm 	}
2366185db85Sdougm 	return (ret);
2376185db85Sdougm }
2386185db85Sdougm 
239549ec3ffSdougm /*
240549ec3ffSdougm  * Document root to active handle mapping functions.  These are only
241549ec3ffSdougm  * used internally. A mutex is used to prevent access while the list
242549ec3ffSdougm  * is changing. In general, the list will be relatively short - one
243549ec3ffSdougm  * item per thread that has called sa_init().
244549ec3ffSdougm  */
245549ec3ffSdougm 
246549ec3ffSdougm sa_handle_impl_t
get_handle_for_root(xmlNodePtr root)247549ec3ffSdougm get_handle_for_root(xmlNodePtr root)
248549ec3ffSdougm {
249549ec3ffSdougm 	struct doc2handle *item;
250549ec3ffSdougm 
251549ec3ffSdougm 	(void) mutex_lock(&sa_global_lock);
252549ec3ffSdougm 	for (item = sa_global_handles; item != NULL; item = item->next) {
25357b448deSdougm 		if (item->root == root)
25457b448deSdougm 			break;
255549ec3ffSdougm 	}
256549ec3ffSdougm 	(void) mutex_unlock(&sa_global_lock);
257549ec3ffSdougm 	if (item != NULL)
25857b448deSdougm 		return (item->handle);
259549ec3ffSdougm 	return (NULL);
260549ec3ffSdougm }
261549ec3ffSdougm 
262549ec3ffSdougm static int
add_handle_for_root(xmlNodePtr root,sa_handle_impl_t handle)263549ec3ffSdougm add_handle_for_root(xmlNodePtr root, sa_handle_impl_t handle)
264549ec3ffSdougm {
265549ec3ffSdougm 	struct doc2handle *item;
266549ec3ffSdougm 	int ret = SA_NO_MEMORY;
267549ec3ffSdougm 
268*2bc647a2SToomas Soome 	item = calloc(1, sizeof (struct doc2handle));
269549ec3ffSdougm 	if (item != NULL) {
27057b448deSdougm 		item->root = root;
27157b448deSdougm 		item->handle = handle;
27257b448deSdougm 		(void) mutex_lock(&sa_global_lock);
27357b448deSdougm 		item->next = sa_global_handles;
27457b448deSdougm 		sa_global_handles = item;
27557b448deSdougm 		(void) mutex_unlock(&sa_global_lock);
27657b448deSdougm 		ret = SA_OK;
277549ec3ffSdougm 	}
278549ec3ffSdougm 	return (ret);
279549ec3ffSdougm }
280549ec3ffSdougm 
281549ec3ffSdougm /*
282549ec3ffSdougm  * remove_handle_for_root(root)
283549ec3ffSdougm  *
284549ec3ffSdougm  * Walks the list of handles and removes the one for this "root" from
285549ec3ffSdougm  * the list. It is up to the caller to free the data.
286549ec3ffSdougm  */
287549ec3ffSdougm 
288549ec3ffSdougm static void
remove_handle_for_root(xmlNodePtr root)289549ec3ffSdougm remove_handle_for_root(xmlNodePtr root)
290549ec3ffSdougm {
291549ec3ffSdougm 	struct doc2handle *item, *prev;
292549ec3ffSdougm 
293549ec3ffSdougm 	(void) mutex_lock(&sa_global_lock);
294549ec3ffSdougm 	for (prev = NULL, item = sa_global_handles; item != NULL;
29557b448deSdougm 	    item = item->next) {
29657b448deSdougm 		if (item->root == root) {
29757b448deSdougm 			/* first in the list */
29857b448deSdougm 			if (prev == NULL)
29957b448deSdougm 				sa_global_handles = sa_global_handles->next;
30057b448deSdougm 			else
30157b448deSdougm 				prev->next = item->next;
30257b448deSdougm 			/* Item is out of the list so free the list structure */
30357b448deSdougm 			free(item);
30457b448deSdougm 			break;
305549ec3ffSdougm 		}
30657b448deSdougm 		prev = item;
307549ec3ffSdougm 	}
308549ec3ffSdougm 	(void) mutex_unlock(&sa_global_lock);
309549ec3ffSdougm }
310549ec3ffSdougm 
311549ec3ffSdougm /*
312549ec3ffSdougm  * sa_find_group_handle(sa_group_t group)
313549ec3ffSdougm  *
314549ec3ffSdougm  * Find the sa_handle_t for the configuration associated with this
315549ec3ffSdougm  * group.
316549ec3ffSdougm  */
317549ec3ffSdougm sa_handle_t
sa_find_group_handle(sa_group_t group)318549ec3ffSdougm sa_find_group_handle(sa_group_t group)
319549ec3ffSdougm {
320549ec3ffSdougm 	xmlNodePtr node = (xmlNodePtr)group;
321549ec3ffSdougm 	sa_handle_t handle;
322549ec3ffSdougm 
323549ec3ffSdougm 	while (node != NULL) {
32457b448deSdougm 		if (strcmp((char *)(node->name), "sharecfg") == 0) {
32557b448deSdougm 			/* have the root so get the handle */
32657b448deSdougm 			handle = (sa_handle_t)get_handle_for_root(node);
32757b448deSdougm 			return (handle);
32857b448deSdougm 		}
32957b448deSdougm 		node = node->parent;
330549ec3ffSdougm 	}
331549ec3ffSdougm 	return (NULL);
332549ec3ffSdougm }
333549ec3ffSdougm 
3346185db85Sdougm /*
3356185db85Sdougm  * set_legacy_timestamp(root, path, timevalue)
3366185db85Sdougm  *
3376185db85Sdougm  * add the current timestamp value to the configuration for use in
3386185db85Sdougm  * determining when to update the legacy files.  For SMF, this
3396185db85Sdougm  * property is kept in default/operation/legacy_timestamp
3406185db85Sdougm  */
3416185db85Sdougm 
3426185db85Sdougm static void
set_legacy_timestamp(xmlNodePtr root,char * path,uint64_t tval)3436185db85Sdougm set_legacy_timestamp(xmlNodePtr root, char *path, uint64_t tval)
3446185db85Sdougm {
3456185db85Sdougm 	xmlNodePtr node;
3466185db85Sdougm 	xmlChar *lpath = NULL;
347549ec3ffSdougm 	sa_handle_impl_t handle;
348549ec3ffSdougm 
349549ec3ffSdougm 	/* Have to have a handle or else we weren't initialized. */
350549ec3ffSdougm 	handle = get_handle_for_root(root);
351549ec3ffSdougm 	if (handle == NULL)
35257b448deSdougm 		return;
3536185db85Sdougm 
3546185db85Sdougm 	for (node = root->xmlChildrenNode; node != NULL;
35557b448deSdougm 	    node = node->next) {
35657b448deSdougm 		if (xmlStrcmp(node->name, (xmlChar *)"legacy") == 0) {
35757b448deSdougm 			/* a possible legacy node for this path */
35857b448deSdougm 			lpath = xmlGetProp(node, (xmlChar *)"path");
35957b448deSdougm 			if (lpath != NULL &&
36057b448deSdougm 			    xmlStrcmp(lpath, (xmlChar *)path) == 0) {
36157b448deSdougm 				xmlFree(lpath);
36257b448deSdougm 				break;
36357b448deSdougm 			}
36457b448deSdougm 			if (lpath != NULL)
36557b448deSdougm 				xmlFree(lpath);
3666185db85Sdougm 		}
3676185db85Sdougm 	}
3686185db85Sdougm 	if (node == NULL) {
36957b448deSdougm 		/* need to create the first legacy timestamp node */
37057b448deSdougm 		node = xmlNewChild(root, NULL, (xmlChar *)"legacy", NULL);
3716185db85Sdougm 	}
3726185db85Sdougm 	if (node != NULL) {
37357b448deSdougm 		char tstring[32];
37457b448deSdougm 		int ret;
37557b448deSdougm 
37657b448deSdougm 		(void) snprintf(tstring, sizeof (tstring), "%lld", tval);
3774bff34e3Sthurlow 		(void) xmlSetProp(node, (xmlChar *)"timestamp",
3784bff34e3Sthurlow 		    (xmlChar *)tstring);
3794bff34e3Sthurlow 		(void) xmlSetProp(node, (xmlChar *)"path", (xmlChar *)path);
38057b448deSdougm 		/* now commit to SMF */
38157b448deSdougm 		ret = sa_get_instance(handle->scfhandle, "default");
3826185db85Sdougm 		if (ret == SA_OK) {
38357b448deSdougm 			ret = sa_start_transaction(handle->scfhandle,
38457b448deSdougm 			    "operation");
38557b448deSdougm 			if (ret == SA_OK) {
38657b448deSdougm 				ret = sa_set_property(handle->scfhandle,
38757b448deSdougm 				    "legacy-timestamp", tstring);
38857b448deSdougm 				if (ret == SA_OK) {
38957b448deSdougm 					(void) sa_end_transaction(
3905b6e0c46Sdougm 					    handle->scfhandle, handle);
39157b448deSdougm 				} else {
39257b448deSdougm 					sa_abort_transaction(handle->scfhandle);
39357b448deSdougm 				}
39457b448deSdougm 			}
3956185db85Sdougm 		}
3966185db85Sdougm 	}
3976185db85Sdougm }
3986185db85Sdougm 
3996185db85Sdougm /*
4006185db85Sdougm  * is_shared(share)
4016185db85Sdougm  *
4026185db85Sdougm  * determine if the specified share is currently shared or not.
4036185db85Sdougm  */
4046185db85Sdougm static int
is_shared(sa_share_t share)4056185db85Sdougm is_shared(sa_share_t share)
4066185db85Sdougm {
4076185db85Sdougm 	char *shared;
4086185db85Sdougm 	int result = 0; /* assume not */
4096185db85Sdougm 
4106185db85Sdougm 	shared = sa_get_share_attr(share, "shared");
4116185db85Sdougm 	if (shared != NULL) {
41257b448deSdougm 		if (strcmp(shared, "true") == 0)
41357b448deSdougm 			result = 1;
41457b448deSdougm 		sa_free_attr_string(shared);
4156185db85Sdougm 	}
4166185db85Sdougm 	return (result);
4176185db85Sdougm }
4186185db85Sdougm 
419da6c28aaSamw /*
420da6c28aaSamw  * excluded_protocol(share, proto)
421da6c28aaSamw  *
422da6c28aaSamw  * Returns B_TRUE if the specified protocol appears in the "exclude"
423da6c28aaSamw  * property. This is used to prevent sharing special case shares
424da6c28aaSamw  * (e.g. subdirs when SMB wants a subdir and NFS doesn't. B_FALSE is
425da6c28aaSamw  * returned if the protocol isn't in the list.
426da6c28aaSamw  */
427da6c28aaSamw static boolean_t
excluded_protocol(sa_share_t share,char * proto)428da6c28aaSamw excluded_protocol(sa_share_t share, char *proto)
429da6c28aaSamw {
430da6c28aaSamw 	char *protolist;
431da6c28aaSamw 	char *str;
432da6c28aaSamw 	char *token;
433da6c28aaSamw 
434da6c28aaSamw 	protolist = sa_get_share_attr(share, "exclude");
435da6c28aaSamw 	if (protolist != NULL) {
436da6c28aaSamw 		str = protolist;
437da6c28aaSamw 		while ((token = strtok(str, ",")) != NULL) {
438da6c28aaSamw 			if (strcmp(token, proto) == 0) {
439da6c28aaSamw 				sa_free_attr_string(protolist);
440da6c28aaSamw 				return (B_TRUE);
441da6c28aaSamw 			}
442da6c28aaSamw 			str = NULL;
443da6c28aaSamw 		}
444da6c28aaSamw 		sa_free_attr_string(protolist);
445da6c28aaSamw 	}
446da6c28aaSamw 	return (B_FALSE);
447da6c28aaSamw }
448da6c28aaSamw 
4496185db85Sdougm /*
450a99982a7Sdougm  * checksubdirgroup(group, newpath, strictness)
451f345c0beSdougm  *
452a99982a7Sdougm  * check all the specified newpath against all the paths in the
453a99982a7Sdougm  * group. This is a helper function for checksubdir to make it easier
454a99982a7Sdougm  * to also check ZFS subgroups.
455a99982a7Sdougm  * The strictness values mean:
456f345c0beSdougm  * SA_CHECK_NORMAL == only check newpath against shares that are active
457f345c0beSdougm  * SA_CHECK_STRICT == check newpath against both active shares and those
458f345c0beSdougm  *		      stored in the repository
4596185db85Sdougm  */
4606185db85Sdougm static int
checksubdirgroup(sa_group_t group,char * newpath,int strictness)461a99982a7Sdougm checksubdirgroup(sa_group_t group, char *newpath, int strictness)
4626185db85Sdougm {
4636185db85Sdougm 	sa_share_t share;
464a99982a7Sdougm 	char *path;
465a99982a7Sdougm 	int issub = SA_OK;
466da6c28aaSamw 	int subdir;
467da6c28aaSamw 	int parent;
468da6c28aaSamw 
469da6c28aaSamw 	if (newpath == NULL)
470da6c28aaSamw 		return (SA_INVALID_PATH);
4716185db85Sdougm 
472a99982a7Sdougm 	for (share = sa_get_share(group, NULL); share != NULL;
473a99982a7Sdougm 	    share = sa_get_next_share(share)) {
4746185db85Sdougm 		/*
4756185db85Sdougm 		 * The original behavior of share never checked
4766185db85Sdougm 		 * against the permanent configuration
4776185db85Sdougm 		 * (/etc/dfs/dfstab).  PIT has a number of cases where
4786185db85Sdougm 		 * it depends on this older behavior even though it
4796185db85Sdougm 		 * could be considered incorrect.  We may tighten this
4806185db85Sdougm 		 * up in the future.
4816185db85Sdougm 		 */
48257b448deSdougm 		if (strictness == SA_CHECK_NORMAL && !is_shared(share))
48357b448deSdougm 			continue;
4846185db85Sdougm 
48557b448deSdougm 		path = sa_get_share_attr(share, "path");
486f345c0beSdougm 		/*
487f345c0beSdougm 		 * If path is NULL, then a share is in the process of
488f345c0beSdougm 		 * construction or someone has modified the property
489a99982a7Sdougm 		 * group inappropriately. It should be
490a99982a7Sdougm 		 * ignored. issubdir() comes from the original share
491a99982a7Sdougm 		 * implementation and does the difficult part of
492a99982a7Sdougm 		 * checking subdirectories.
493f345c0beSdougm 		 */
49457b448deSdougm 		if (path == NULL)
49557b448deSdougm 			continue;
496da6c28aaSamw 
497da6c28aaSamw 		if (strcmp(path, newpath) == 0) {
49857b448deSdougm 			issub = SA_INVALID_PATH;
499da6c28aaSamw 		} else {
500da6c28aaSamw 			subdir = issubdir(newpath, path);
501da6c28aaSamw 			parent = issubdir(path, newpath);
502da6c28aaSamw 			if (subdir || parent) {
503da6c28aaSamw 				sa_free_attr_string(path);
504da6c28aaSamw 				path = NULL;
505da6c28aaSamw 				return (subdir ?
506da6c28aaSamw 				    SA_PATH_IS_SUBDIR : SA_PATH_IS_PARENTDIR);
507da6c28aaSamw 			}
50857b448deSdougm 		}
5096185db85Sdougm 		sa_free_attr_string(path);
5106185db85Sdougm 		path = NULL;
511a99982a7Sdougm 	}
512a99982a7Sdougm 	return (issub);
513a99982a7Sdougm }
514a99982a7Sdougm 
515a99982a7Sdougm /*
516a99982a7Sdougm  * checksubdir(newpath, strictness)
517a99982a7Sdougm  *
518a99982a7Sdougm  * checksubdir determines if the specified path (newpath) is a
519a99982a7Sdougm  * subdirectory of another share. It calls checksubdirgroup() to do
520a99982a7Sdougm  * the complicated work. The strictness parameter determines how
521a99982a7Sdougm  * strict a check to make against the path. The strictness values
522a99982a7Sdougm  * mean: SA_CHECK_NORMAL == only check newpath against shares that are
523a99982a7Sdougm  * active SA_CHECK_STRICT == check newpath against both active shares
524a99982a7Sdougm  * and those * stored in the repository
525a99982a7Sdougm  */
526a99982a7Sdougm static int
checksubdir(sa_handle_t handle,char * newpath,int strictness)527549ec3ffSdougm checksubdir(sa_handle_t handle, char *newpath, int strictness)
528a99982a7Sdougm {
529a99982a7Sdougm 	sa_group_t group;
530da6c28aaSamw 	int issub = SA_OK;
531a99982a7Sdougm 	char *path = NULL;
532a99982a7Sdougm 
533da6c28aaSamw 	for (group = sa_get_group(handle, NULL);
534da6c28aaSamw 	    group != NULL && issub == SA_OK;
535da6c28aaSamw 	    group = sa_get_next_group(group)) {
53657b448deSdougm 		if (sa_group_is_zfs(group)) {
53757b448deSdougm 			sa_group_t subgroup;
53857b448deSdougm 			for (subgroup = sa_get_sub_group(group);
539da6c28aaSamw 			    subgroup != NULL && issub == SA_OK;
54057b448deSdougm 			    subgroup = sa_get_next_group(subgroup))
54157b448deSdougm 				issub = checksubdirgroup(subgroup, newpath,
54257b448deSdougm 				    strictness);
54357b448deSdougm 		} else {
54457b448deSdougm 			issub = checksubdirgroup(group, newpath, strictness);
54557b448deSdougm 		}
5466185db85Sdougm 	}
5476185db85Sdougm 	if (path != NULL)
54857b448deSdougm 		sa_free_attr_string(path);
5496185db85Sdougm 	return (issub);
5506185db85Sdougm }
5516185db85Sdougm 
5526185db85Sdougm /*
553f345c0beSdougm  * validpath(path, strictness)
5546185db85Sdougm  * determine if the provided path is valid for a share. It shouldn't
5556185db85Sdougm  * be a sub-dir of an already shared path or the parent directory of a
5566185db85Sdougm  * share path.
5576185db85Sdougm  */
5586185db85Sdougm static int
validpath(sa_handle_t handle,char * path,int strictness)559549ec3ffSdougm validpath(sa_handle_t handle, char *path, int strictness)
5606185db85Sdougm {
5616185db85Sdougm 	int error = SA_OK;
5626185db85Sdougm 	struct stat st;
5636185db85Sdougm 	sa_share_t share;
5646185db85Sdougm 	char *fstype;
5656185db85Sdougm 
56657b448deSdougm 	if (*path != '/')
56757b448deSdougm 		return (SA_BAD_PATH);
56857b448deSdougm 
5696185db85Sdougm 	if (stat(path, &st) < 0) {
57057b448deSdougm 		error = SA_NO_SUCH_PATH;
5716185db85Sdougm 	} else {
57257b448deSdougm 		share = sa_find_share(handle, path);
57357b448deSdougm 		if (share != NULL)
57457b448deSdougm 			error = SA_DUPLICATE_NAME;
57557b448deSdougm 
57657b448deSdougm 		if (error == SA_OK) {
57757b448deSdougm 			/*
57857b448deSdougm 			 * check for special case with file system
57957b448deSdougm 			 * that might have restrictions.  For now, ZFS
58057b448deSdougm 			 * is the only case since it has its own idea
58157b448deSdougm 			 * of how to configure shares. We do this
58257b448deSdougm 			 * before subdir checking since things like
58357b448deSdougm 			 * ZFS will do that for us. This should also
58457b448deSdougm 			 * be done via plugin interface.
58557b448deSdougm 			 */
58657b448deSdougm 			fstype = sa_fstype(path);
58757b448deSdougm 			if (fstype != NULL && strcmp(fstype, "zfs") == 0) {
58857b448deSdougm 				if (sa_zfs_is_shared(handle, path))
58957b448deSdougm 					error = SA_INVALID_NAME;
59057b448deSdougm 			}
59157b448deSdougm 			if (fstype != NULL)
59257b448deSdougm 				sa_free_fstype(fstype);
5936185db85Sdougm 		}
59457b448deSdougm 		if (error == SA_OK)
59557b448deSdougm 			error = checksubdir(handle, path, strictness);
5966185db85Sdougm 	}
5976185db85Sdougm 	return (error);
5986185db85Sdougm }
5996185db85Sdougm 
6006185db85Sdougm /*
6016185db85Sdougm  * check to see if group/share is persistent.
602da6c28aaSamw  *
603da6c28aaSamw  * "group" can be either an sa_group_t or an sa_share_t. (void *)
6045cb0d679SMarcel Telka  * works since both these types are also void *.
605148c5f43SAlan Wright  * If the share is a ZFS share, mark it as persistent.
6066185db85Sdougm  */
607da6c28aaSamw int
sa_is_persistent(void * group)608da6c28aaSamw sa_is_persistent(void *group)
6096185db85Sdougm {
6106185db85Sdougm 	char *type;
6116185db85Sdougm 	int persist = 1;
612148c5f43SAlan Wright 	sa_group_t grp;
6136185db85Sdougm 
614da6c28aaSamw 	type = sa_get_group_attr((sa_group_t)group, "type");
615148c5f43SAlan Wright 	if (type != NULL) {
616148c5f43SAlan Wright 		if (strcmp(type, "transient") == 0)
617148c5f43SAlan Wright 			persist = 0;
61857b448deSdougm 		sa_free_attr_string(type);
619148c5f43SAlan Wright 	}
620148c5f43SAlan Wright 
621148c5f43SAlan Wright 	grp = (sa_is_share(group)) ? sa_get_parent_group(group) : group;
622148c5f43SAlan Wright 	if (sa_group_is_zfs(grp))
623148c5f43SAlan Wright 		persist = 1;
624148c5f43SAlan Wright 
6256185db85Sdougm 	return (persist);
6266185db85Sdougm }
6276185db85Sdougm 
6286185db85Sdougm /*
6296185db85Sdougm  * sa_valid_group_name(name)
6306185db85Sdougm  *
6316185db85Sdougm  * check that the "name" contains only valid characters and otherwise
6326185db85Sdougm  * fits the required naming conventions. Valid names must start with
6336185db85Sdougm  * an alphabetic and the remainder may consist of only alphanumeric
6346185db85Sdougm  * plus the '-' and '_' characters. This name limitation comes from
6356185db85Sdougm  * inherent limitations in SMF.
6366185db85Sdougm  */
6376185db85Sdougm 
6386185db85Sdougm int
sa_valid_group_name(char * name)6396185db85Sdougm sa_valid_group_name(char *name)
6406185db85Sdougm {
6416185db85Sdougm 	int ret = 1;
6426185db85Sdougm 	ssize_t len;
6436185db85Sdougm 
6446185db85Sdougm 	if (name != NULL && isalpha(*name)) {
64557b448deSdougm 		char c;
64657b448deSdougm 		len = strlen(name);
64757b448deSdougm 		if (len < (scf_max_name_len - sizeof ("group:"))) {
64857b448deSdougm 			for (c = *name++; c != '\0' && ret != 0; c = *name++) {
64957b448deSdougm 				if (!isalnum(c) && c != '-' && c != '_')
65057b448deSdougm 					ret = 0;
65157b448deSdougm 			}
65257b448deSdougm 		} else {
6536185db85Sdougm 			ret = 0;
6546185db85Sdougm 		}
6556185db85Sdougm 	} else {
65657b448deSdougm 		ret = 0;
6576185db85Sdougm 	}
6586185db85Sdougm 	return (ret);
6596185db85Sdougm }
6606185db85Sdougm 
6616185db85Sdougm 
6626185db85Sdougm /*
6636185db85Sdougm  * is_zfs_group(group)
6646185db85Sdougm  *	Determine if the specified group is a ZFS sharenfs group
6656185db85Sdougm  */
6666185db85Sdougm static int
is_zfs_group(sa_group_t group)6676185db85Sdougm is_zfs_group(sa_group_t group)
6686185db85Sdougm {
6696185db85Sdougm 	int ret = 0;
6706185db85Sdougm 	xmlNodePtr parent;
6716185db85Sdougm 	xmlChar *zfs;
6726185db85Sdougm 
67357b448deSdougm 	if (strcmp((char *)((xmlNodePtr)group)->name, "share") == 0)
67457b448deSdougm 		parent = (xmlNodePtr)sa_get_parent_group(group);
67557b448deSdougm 	else
67657b448deSdougm 		parent = (xmlNodePtr)group;
6776185db85Sdougm 	zfs = xmlGetProp(parent, (xmlChar *)"zfs");
6786185db85Sdougm 	if (zfs != NULL) {
67957b448deSdougm 		xmlFree(zfs);
68057b448deSdougm 		ret = 1;
6816185db85Sdougm 	}
6826185db85Sdougm 	return (ret);
6836185db85Sdougm }
6846185db85Sdougm 
685da6c28aaSamw /*
686da6c28aaSamw  * sa_get_object_type(object)
687da6c28aaSamw  *
688da6c28aaSamw  * This function returns a numeric value representing the object
689da6c28aaSamw  * type. This allows using simpler checks when doing type specific
690da6c28aaSamw  * operations.
691da6c28aaSamw  */
692da6c28aaSamw 
693da6c28aaSamw static int
sa_get_object_type(void * object)694da6c28aaSamw sa_get_object_type(void *object)
695da6c28aaSamw {
696da6c28aaSamw 	xmlNodePtr node = (xmlNodePtr)object;
697da6c28aaSamw 	int type;
698da6c28aaSamw 
699da6c28aaSamw 	if (xmlStrcmp(node->name, (xmlChar *)"group") == 0)
700da6c28aaSamw 		type = SA_TYPE_GROUP;
701da6c28aaSamw 	else if (xmlStrcmp(node->name, (xmlChar *)"share") == 0)
702da6c28aaSamw 		type = SA_TYPE_SHARE;
703da6c28aaSamw 	else if (xmlStrcmp(node->name, (xmlChar *)"resource") == 0)
704da6c28aaSamw 		type = SA_TYPE_RESOURCE;
705da6c28aaSamw 	else if (xmlStrcmp(node->name, (xmlChar *)"optionset") == 0)
706da6c28aaSamw 		type = SA_TYPE_OPTIONSET;
707da6c28aaSamw 	else if (xmlStrcmp(node->name, (xmlChar *)"security") == 0)
708da6c28aaSamw 		type = SA_TYPE_ALTSPACE;
709da6c28aaSamw 	else
710da6c28aaSamw 		assert(0);
711da6c28aaSamw 	return (type);
712da6c28aaSamw }
713da6c28aaSamw 
7146185db85Sdougm /*
7156185db85Sdougm  * sa_optionset_name(optionset, oname, len, id)
7166185db85Sdougm  *	return the SMF name for the optionset. If id is not NULL, it
7176185db85Sdougm  *	will have the GUID value for a share and should be used
7186185db85Sdougm  *	instead of the keyword "optionset" which is used for
7196185db85Sdougm  *	groups. If the optionset doesn't have a protocol type
7206185db85Sdougm  *	associated with it, "default" is used. This shouldn't happen
7216185db85Sdougm  *	at this point but may be desirable in the future if there are
7226185db85Sdougm  *	protocol independent properties added. The name is returned in
7236185db85Sdougm  *	oname.
7246185db85Sdougm  */
7256185db85Sdougm 
7266185db85Sdougm static int
sa_optionset_name(sa_optionset_t optionset,char * oname,size_t len,char * id)7276185db85Sdougm sa_optionset_name(sa_optionset_t optionset, char *oname, size_t len, char *id)
7286185db85Sdougm {
7296185db85Sdougm 	char *proto;
730da6c28aaSamw 	void *parent;
731da6c28aaSamw 	int ptype;
7326185db85Sdougm 
7336185db85Sdougm 	if (id == NULL)
73457b448deSdougm 		id = "optionset";
7356185db85Sdougm 
736da6c28aaSamw 	parent = sa_get_optionset_parent(optionset);
737da6c28aaSamw 	if (parent != NULL) {
738da6c28aaSamw 		ptype = sa_get_object_type(parent);
739da6c28aaSamw 		proto = sa_get_optionset_attr(optionset, "type");
740da6c28aaSamw 		if (ptype != SA_TYPE_RESOURCE) {
741da6c28aaSamw 			len = snprintf(oname, len, "%s_%s", id,
742da6c28aaSamw 			    proto ? proto : "default");
743da6c28aaSamw 		} else {
744da6c28aaSamw 			char *index;
745da6c28aaSamw 			index = get_node_attr((void *)parent, "id");
746fe1c642dSBill Krier 			if (index != NULL) {
747da6c28aaSamw 				len = snprintf(oname, len, "%s_%s_%s", id,
748da6c28aaSamw 				    proto ? proto : "default", index);
749fe1c642dSBill Krier 				sa_free_attr_string(index);
750fe1c642dSBill Krier 			} else {
751da6c28aaSamw 				len = 0;
752fe1c642dSBill Krier 			}
753da6c28aaSamw 		}
7546185db85Sdougm 
755da6c28aaSamw 		if (proto != NULL)
756da6c28aaSamw 			sa_free_attr_string(proto);
757da6c28aaSamw 	} else {
758da6c28aaSamw 		len = 0;
759da6c28aaSamw 	}
7606185db85Sdougm 	return (len);
7616185db85Sdougm }
7626185db85Sdougm 
7636185db85Sdougm /*
7646185db85Sdougm  * sa_security_name(optionset, oname, len, id)
7656185db85Sdougm  *
7666185db85Sdougm  * return the SMF name for the security. If id is not NULL, it will
7676185db85Sdougm  * have the GUID value for a share and should be used instead of the
7686185db85Sdougm  * keyword "optionset" which is used for groups. If the optionset
7696185db85Sdougm  * doesn't have a protocol type associated with it, "default" is
7706185db85Sdougm  * used. This shouldn't happen at this point but may be desirable in
7716185db85Sdougm  * the future if there are protocol independent properties added. The
7726185db85Sdougm  * name is returned in oname. The security type is also encoded into
7736185db85Sdougm  * the name. In the future, this wil *be handled a bit differently.
7746185db85Sdougm  */
7756185db85Sdougm 
7766185db85Sdougm static int
sa_security_name(sa_security_t security,char * oname,size_t len,char * id)7776185db85Sdougm sa_security_name(sa_security_t security, char *oname, size_t len, char *id)
7786185db85Sdougm {
7796185db85Sdougm 	char *proto;
7806185db85Sdougm 	char *sectype;
7816185db85Sdougm 
7826185db85Sdougm 	if (id == NULL)
78357b448deSdougm 		id = "optionset";
7846185db85Sdougm 
7856185db85Sdougm 	proto = sa_get_security_attr(security, "type");
7866185db85Sdougm 	sectype = sa_get_security_attr(security, "sectype");
78757b448deSdougm 	len = snprintf(oname, len, "%s_%s_%s", id, proto ? proto : "default",
78857b448deSdougm 	    sectype ? sectype : "default");
7896185db85Sdougm 	if (proto != NULL)
79057b448deSdougm 		sa_free_attr_string(proto);
7916185db85Sdougm 	if (sectype != NULL)
79257b448deSdougm 		sa_free_attr_string(sectype);
7936185db85Sdougm 	return (len);
7946185db85Sdougm }
7956185db85Sdougm 
79657b448deSdougm /*
79757b448deSdougm  * verifydefgroupopts(handle)
79857b448deSdougm  *
79957b448deSdougm  * Make sure a "default" group exists and has default protocols enabled.
80057b448deSdougm  */
80157b448deSdougm static void
verifydefgroupopts(sa_handle_t handle)80257b448deSdougm verifydefgroupopts(sa_handle_t handle)
80357b448deSdougm {
80457b448deSdougm 	sa_group_t defgrp;
80557b448deSdougm 	sa_optionset_t opt;
806da6c28aaSamw 
80757b448deSdougm 	defgrp = sa_get_group(handle, "default");
80857b448deSdougm 	if (defgrp != NULL) {
80957b448deSdougm 		opt = sa_get_optionset(defgrp, NULL);
81057b448deSdougm 		/*
81157b448deSdougm 		 * NFS is the default for default group
81257b448deSdougm 		 */
81357b448deSdougm 		if (opt == NULL)
81457b448deSdougm 			opt = sa_create_optionset(defgrp, "nfs");
81557b448deSdougm 	}
81657b448deSdougm }
81757b448deSdougm 
8186185db85Sdougm /*
8198a981c33SDaniel Hoffman  * sa_init_impl(init_service, arg)
8206185db85Sdougm  *	Initialize the API
8216185db85Sdougm  *	find all the shared objects
8226185db85Sdougm  *	init the tables with all objects
8236185db85Sdougm  *	read in the current configuration
8248a981c33SDaniel Hoffman  *
8258a981c33SDaniel Hoffman  *	arg is a parameter passed in whose meaning is based on the init_service.
8268a981c33SDaniel Hoffman  *	See libshare.h under API initialization.
8276185db85Sdougm  */
82857b448deSdougm #define	GETPROP(prop)	scf_simple_prop_next_astring(prop)
82957b448deSdougm #define	CHECKTSTAMP(st, tval)	stat(SA_LEGACY_DFSTAB, &st) >= 0 && \
83057b448deSdougm 	tval != TSTAMP(st.st_ctim)
8318a981c33SDaniel Hoffman static sa_handle_t
sa_init_impl(int init_service,void * arg)8328a981c33SDaniel Hoffman sa_init_impl(int init_service, void *arg)
8336185db85Sdougm {
8346185db85Sdougm 	struct stat st;
8358a981c33SDaniel Hoffman 	/* legacy is used for debugging only as far as I can tell */
8366185db85Sdougm 	int legacy = 0;
8376185db85Sdougm 	uint64_t tval = 0;
838a99982a7Sdougm 	int lockfd;
839a99982a7Sdougm 	sigset_t old;
840a99982a7Sdougm 	int updatelegacy = B_FALSE;
841a99982a7Sdougm 	scf_simple_prop_t *prop;
842549ec3ffSdougm 	sa_handle_impl_t handle;
843549ec3ffSdougm 	int err;
844549ec3ffSdougm 
845*2bc647a2SToomas Soome 	handle = calloc(1, sizeof (struct sa_handle_impl));
8466185db85Sdougm 
847549ec3ffSdougm 	if (handle != NULL) {
8488a981c33SDaniel Hoffman 		handle->sa_service = init_service;
849c5f58477Sdougm 		/*
850c5f58477Sdougm 		 * Get protocol specific structures, but only if this
851c5f58477Sdougm 		 * is the only handle.
852c5f58477Sdougm 		 */
853c5f58477Sdougm 		(void) mutex_lock(&sa_global_lock);
854c5f58477Sdougm 		if (sa_global_handles == NULL)
855c5f58477Sdougm 			(void) proto_plugin_init();
856c5f58477Sdougm 		(void) mutex_unlock(&sa_global_lock);
8578a981c33SDaniel Hoffman 		if (init_service & (SA_INIT_SHARE_API |
8588a981c33SDaniel Hoffman 		    SA_INIT_SHARE_API_SELECTIVE | SA_INIT_ONE_SHARE_FROM_NAME |
8598a981c33SDaniel Hoffman 		    SA_INIT_ONE_SHARE_FROM_HANDLE)) {
860a99982a7Sdougm 			/*
86157b448deSdougm 			 * initialize access into libzfs. We use this
86257b448deSdougm 			 * when collecting info about ZFS datasets and
86357b448deSdougm 			 * shares.
864a99982a7Sdougm 			 */
86557b448deSdougm 			if (sa_zfs_init(handle) == B_FALSE) {
86657b448deSdougm 				free(handle);
86789dc44ceSjose borrego 				(void) mutex_lock(&sa_global_lock);
86857b448deSdougm 				(void) proto_plugin_fini();
86989dc44ceSjose borrego 				(void) mutex_unlock(&sa_global_lock);
87057b448deSdougm 				return (NULL);
87157b448deSdougm 			}
872a99982a7Sdougm 			/*
87357b448deSdougm 			 * since we want to use SMF, initialize an svc handle
87457b448deSdougm 			 * and find out what is there.
875a99982a7Sdougm 			 */
87657b448deSdougm 			handle->scfhandle = sa_scf_init(handle);
87757b448deSdougm 			if (handle->scfhandle != NULL) {
87857b448deSdougm 				/*
87957b448deSdougm 				 * Need to lock the extraction of the
88057b448deSdougm 				 * configuration if the dfstab file has
88157b448deSdougm 				 * changed. Lock everything now and release if
88257b448deSdougm 				 * not needed.  Use a file that isn't being
88357b448deSdougm 				 * manipulated by other parts of the system in
88457b448deSdougm 				 * order to not interfere with locking. Using
88557b448deSdougm 				 * dfstab doesn't work.
88657b448deSdougm 				 */
88757b448deSdougm 				sablocksigs(&old);
88857b448deSdougm 				lockfd = open(DFS_LOCK_FILE, O_RDWR);
88957b448deSdougm 				if (lockfd >= 0) {
89057b448deSdougm 					errno = 0;
89157b448deSdougm 					(void) lockf(lockfd, F_LOCK, 0);
892148c5f43SAlan Wright 					(void) mutex_lock(&sa_dfstab_lock);
89357b448deSdougm 					/*
89457b448deSdougm 					 * Check whether we are going to need
89557b448deSdougm 					 * to merge any dfstab changes. This
89657b448deSdougm 					 * is done by comparing the value of
89757b448deSdougm 					 * legacy-timestamp with the current
89857b448deSdougm 					 * st_ctim of the file. If they are
89957b448deSdougm 					 * different, an update is needed and
90057b448deSdougm 					 * the file must remain locked until
90157b448deSdougm 					 * the merge is done in order to
90257b448deSdougm 					 * prevent multiple startups from
90357b448deSdougm 					 * changing the SMF repository at the
90457b448deSdougm 					 * same time.  The first to get the
90557b448deSdougm 					 * lock will make any changes before
90657b448deSdougm 					 * the others can read the repository.
90757b448deSdougm 					 */
90857b448deSdougm 					prop = scf_simple_prop_get
90957b448deSdougm 					    (handle->scfhandle->handle,
91057b448deSdougm 					    (const char *)SA_SVC_FMRI_BASE
91157b448deSdougm 					    ":default", "operation",
91257b448deSdougm 					    "legacy-timestamp");
91357b448deSdougm 					if (prop != NULL) {
91457b448deSdougm 						char *i64;
91557b448deSdougm 						i64 = GETPROP(prop);
91657b448deSdougm 						if (i64 != NULL)
91757b448deSdougm 							tval = strtoull(i64,
91857b448deSdougm 							    NULL, 0);
91957b448deSdougm 						if (CHECKTSTAMP(st, tval))
92057b448deSdougm 							updatelegacy = B_TRUE;
92157b448deSdougm 						scf_simple_prop_free(prop);
92257b448deSdougm 					} else {
92357b448deSdougm 						/*
92457b448deSdougm 						 * We haven't set the
92557b448deSdougm 						 * timestamp before so do it.
92657b448deSdougm 						 */
92757b448deSdougm 						updatelegacy = B_TRUE;
92857b448deSdougm 					}
929148c5f43SAlan Wright 					if (updatelegacy == B_FALSE) {
930148c5f43SAlan Wright 						(void) mutex_unlock(
931148c5f43SAlan Wright 						    &sa_dfstab_lock);
932148c5f43SAlan Wright 						(void) lockf(lockfd, F_ULOCK,
933148c5f43SAlan Wright 						    0);
934148c5f43SAlan Wright 						(void) close(lockfd);
935148c5f43SAlan Wright 					}
9361d1813a7Sdougm 
937148c5f43SAlan Wright 				}
93857b448deSdougm 				/*
93957b448deSdougm 				 * It is essential that the document tree and
94057b448deSdougm 				 * the internal list of roots to handles be
94157b448deSdougm 				 * setup before anything that might try to
94257b448deSdougm 				 * create a new object is called. The document
94357b448deSdougm 				 * tree is the combination of handle->doc and
94457b448deSdougm 				 * handle->tree. This allows searches,
94557b448deSdougm 				 * etc. when all you have is an object in the
94657b448deSdougm 				 * tree.
94757b448deSdougm 				 */
94857b448deSdougm 				handle->doc = xmlNewDoc((xmlChar *)"1.0");
94957b448deSdougm 				handle->tree = xmlNewNode(NULL,
95057b448deSdougm 				    (xmlChar *)"sharecfg");
95157b448deSdougm 				if (handle->doc != NULL &&
95257b448deSdougm 				    handle->tree != NULL) {
9534bff34e3Sthurlow 					(void) xmlDocSetRootElement(handle->doc,
95457b448deSdougm 					    handle->tree);
95557b448deSdougm 					err = add_handle_for_root(handle->tree,
95657b448deSdougm 					    handle);
95757b448deSdougm 					if (err == SA_OK)
95857b448deSdougm 						err = sa_get_config(
95957b448deSdougm 						    handle->scfhandle,
9601d1813a7Sdougm 						    handle->tree, handle);
96157b448deSdougm 				} else {
96257b448deSdougm 					if (handle->doc != NULL)
96357b448deSdougm 						xmlFreeDoc(handle->doc);
96457b448deSdougm 					if (handle->tree != NULL)
96557b448deSdougm 						xmlFreeNode(handle->tree);
96657b448deSdougm 					err = SA_NO_MEMORY;
96757b448deSdougm 				}
96857b448deSdougm 
96957b448deSdougm 				saunblocksigs(&old);
97057b448deSdougm 
97157b448deSdougm 				if (err != SA_OK) {
97257b448deSdougm 					/*
97357b448deSdougm 					 * If we couldn't add the tree handle
97457b448deSdougm 					 * to the list, then things are going
97557b448deSdougm 					 * to fail badly. Might as well undo
97657b448deSdougm 					 * everything now and fail the
97757b448deSdougm 					 * sa_init().
97857b448deSdougm 					 */
97957b448deSdougm 					sa_fini(handle);
980148c5f43SAlan Wright 					if (updatelegacy == B_TRUE) {
981148c5f43SAlan Wright 						(void) mutex_unlock(
982148c5f43SAlan Wright 						    &sa_dfstab_lock);
983148c5f43SAlan Wright 						(void) lockf(lockfd,
984148c5f43SAlan Wright 						    F_ULOCK, 0);
985148c5f43SAlan Wright 						(void) close(lockfd);
986148c5f43SAlan Wright 					}
98757b448deSdougm 					return (NULL);
98857b448deSdougm 				}
9891d1813a7Sdougm 
99057b448deSdougm 				if (tval == 0) {
99157b448deSdougm 					/*
99257b448deSdougm 					 * first time so make sure
99357b448deSdougm 					 * default is setup
99457b448deSdougm 					 */
99557b448deSdougm 					verifydefgroupopts(handle);
99657b448deSdougm 				}
997549ec3ffSdougm 
998546405c3Sdougm 				if (updatelegacy == B_TRUE) {
999546405c3Sdougm 					sablocksigs(&old);
1000546405c3Sdougm 					getlegacyconfig((sa_handle_t)handle,
1001546405c3Sdougm 					    SA_LEGACY_DFSTAB, &handle->tree);
1002546405c3Sdougm 					if (stat(SA_LEGACY_DFSTAB, &st) >= 0)
1003546405c3Sdougm 						set_legacy_timestamp(
1004546405c3Sdougm 						    handle->tree,
1005546405c3Sdougm 						    SA_LEGACY_DFSTAB,
1006546405c3Sdougm 						    TSTAMP(st.st_ctim));
1007546405c3Sdougm 					saunblocksigs(&old);
1008546405c3Sdougm 					/*
1009546405c3Sdougm 					 * Safe to unlock now to allow
1010546405c3Sdougm 					 * others to run
1011546405c3Sdougm 					 */
1012148c5f43SAlan Wright 					(void) mutex_unlock(&sa_dfstab_lock);
1013546405c3Sdougm 					(void) lockf(lockfd, F_ULOCK, 0);
1014546405c3Sdougm 					(void) close(lockfd);
1015546405c3Sdougm 				}
10165b6e0c46Sdougm 				/* Get sharetab timestamp */
10175b6e0c46Sdougm 				sa_update_sharetab_ts((sa_handle_t)handle);
10185b6e0c46Sdougm 
10195b6e0c46Sdougm 				/* Get lastupdate (transaction) timestamp */
10205b6e0c46Sdougm 				prop = scf_simple_prop_get(
10215b6e0c46Sdougm 				    handle->scfhandle->handle,
10225b6e0c46Sdougm 				    (const char *)SA_SVC_FMRI_BASE ":default",
10235b6e0c46Sdougm 				    "state", "lastupdate");
10245b6e0c46Sdougm 				if (prop != NULL) {
10255b6e0c46Sdougm 					char *str;
10265b6e0c46Sdougm 					str =
10275b6e0c46Sdougm 					    scf_simple_prop_next_astring(prop);
10285b6e0c46Sdougm 					if (str != NULL)
10295b6e0c46Sdougm 						handle->tstrans =
10305b6e0c46Sdougm 						    strtoull(str, NULL, 0);
10315b6e0c46Sdougm 					else
10325b6e0c46Sdougm 						handle->tstrans = 0;
10335b6e0c46Sdougm 					scf_simple_prop_free(prop);
10345b6e0c46Sdougm 				}
10358a981c33SDaniel Hoffman 				/*
10368a981c33SDaniel Hoffman 				 * In this conditional the library reads from
10378a981c33SDaniel Hoffman 				 * zfs and /etc/dfs/sharetab to find datasets
10388a981c33SDaniel Hoffman 				 * that must be shared. The result is a tree of
10398a981c33SDaniel Hoffman 				 * groups that are stored in the handle for
10408a981c33SDaniel Hoffman 				 * libshare to utilize later when asked to share
10418a981c33SDaniel Hoffman 				 * or unshare datasets.
10428a981c33SDaniel Hoffman 				 */
10438a981c33SDaniel Hoffman 				if (init_service &
10448a981c33SDaniel Hoffman 				    SA_INIT_SHARE_API_SELECTIVE) {
10458a981c33SDaniel Hoffman 					char **paths;
10468a981c33SDaniel Hoffman 					size_t paths_len, i;
10478a981c33SDaniel Hoffman 
10488a981c33SDaniel Hoffman 					legacy |= sa_get_one_zfs_share(handle,
10498a981c33SDaniel Hoffman 					    "zfs",
10508a981c33SDaniel Hoffman 					    (sa_init_selective_arg_t *)arg,
10518a981c33SDaniel Hoffman 					    &paths, &paths_len);
10528a981c33SDaniel Hoffman 					legacy |= get_one_transient(handle,
10538a981c33SDaniel Hoffman 					    &handle->tree, paths, paths_len);
10548a981c33SDaniel Hoffman 					for (i = 0; i < paths_len; ++i) {
10558a981c33SDaniel Hoffman 						free(paths[i]);
10568a981c33SDaniel Hoffman 					}
10578a981c33SDaniel Hoffman 					free(paths);
10588a981c33SDaniel Hoffman 				} else if (init_service &
10598a981c33SDaniel Hoffman 				    SA_INIT_ONE_SHARE_FROM_NAME) {
10608a981c33SDaniel Hoffman 					char path[ZFS_MAXPROPLEN];
10618a981c33SDaniel Hoffman 					char *ptr = path;
10628a981c33SDaniel Hoffman 					char **ptr_to_path = &ptr;
10638a981c33SDaniel Hoffman 
10648a981c33SDaniel Hoffman 					legacy |=
10658a981c33SDaniel Hoffman 					    sa_get_zfs_share_for_name(handle,
10668a981c33SDaniel Hoffman 					    "zfs", (char *)arg, path);
10678a981c33SDaniel Hoffman 					legacy |= get_one_transient(handle,
10688a981c33SDaniel Hoffman 					    &handle->tree, ptr_to_path, 1);
10698a981c33SDaniel Hoffman 				} else if (init_service &
10708a981c33SDaniel Hoffman 				    SA_INIT_ONE_SHARE_FROM_HANDLE) {
10718a981c33SDaniel Hoffman 					char path[ZFS_MAXPROPLEN];
10728a981c33SDaniel Hoffman 					char *ptr = path;
10738a981c33SDaniel Hoffman 					char **ptr_to_path = &ptr;
10748a981c33SDaniel Hoffman 
10758a981c33SDaniel Hoffman 					legacy |=
10768a981c33SDaniel Hoffman 					    sa_get_zfs_share_for_name(handle,
10778a981c33SDaniel Hoffman 					    "zfs",
10788a981c33SDaniel Hoffman 					    zfs_get_name(
10798a981c33SDaniel Hoffman 					    (zfs_handle_t *)arg),
10808a981c33SDaniel Hoffman 					    path);
10818a981c33SDaniel Hoffman 					legacy |= get_one_transient(handle,
10828a981c33SDaniel Hoffman 					    &handle->tree, ptr_to_path, 1);
10838a981c33SDaniel Hoffman 				} else {
10848a981c33SDaniel Hoffman 					legacy |= sa_get_zfs_shares(handle,
10858a981c33SDaniel Hoffman 					    "zfs");
10868a981c33SDaniel Hoffman 					legacy |= gettransients(handle,
10878a981c33SDaniel Hoffman 					    &handle->tree);
10888a981c33SDaniel Hoffman 				}
10896185db85Sdougm 			}
10906185db85Sdougm 		}
10916185db85Sdougm 	}
1092549ec3ffSdougm 	return ((sa_handle_t)handle);
10936185db85Sdougm }
10946185db85Sdougm 
10958a981c33SDaniel Hoffman /*
10968a981c33SDaniel Hoffman  * sa_init exists as a legacy interface, new consumers should use sa_init_arg.
10978a981c33SDaniel Hoffman  */
10988a981c33SDaniel Hoffman sa_handle_t
sa_init(int init_service)10998a981c33SDaniel Hoffman sa_init(int init_service)
11008a981c33SDaniel Hoffman {
11018a981c33SDaniel Hoffman 	return (sa_init_impl(init_service, NULL));
11028a981c33SDaniel Hoffman }
11038a981c33SDaniel Hoffman 
11048a981c33SDaniel Hoffman /*
11058a981c33SDaniel Hoffman  * See libshare.h "API Initialization" section for valid values of init_service
11068a981c33SDaniel Hoffman  * as well as the appropriate argument type for a given init_service.
11078a981c33SDaniel Hoffman  */
11088a981c33SDaniel Hoffman sa_handle_t
sa_init_arg(int init_service,void * arg)11098a981c33SDaniel Hoffman sa_init_arg(int init_service, void *arg)
11108a981c33SDaniel Hoffman {
11118a981c33SDaniel Hoffman 	return (sa_init_impl(init_service, arg));
11128a981c33SDaniel Hoffman }
11138a981c33SDaniel Hoffman 
11146185db85Sdougm /*
1115549ec3ffSdougm  * sa_fini(handle)
11166185db85Sdougm  *	Uninitialize the API structures including the configuration
11171cea05afSdougm  *	data structures and ZFS related data.
11186185db85Sdougm  */
11196185db85Sdougm 
11206185db85Sdougm void
sa_fini(sa_handle_t handle)1121549ec3ffSdougm sa_fini(sa_handle_t handle)
11226185db85Sdougm {
1123549ec3ffSdougm 	sa_handle_impl_t impl_handle = (sa_handle_impl_t)handle;
1124549ec3ffSdougm 
1125549ec3ffSdougm 	if (impl_handle != NULL) {
1126549ec3ffSdougm 		/*
1127549ec3ffSdougm 		 * Free the config trees and any other data structures
1128549ec3ffSdougm 		 * used in the handle.
1129549ec3ffSdougm 		 */
1130549ec3ffSdougm 		if (impl_handle->doc != NULL)
1131549ec3ffSdougm 			xmlFreeDoc(impl_handle->doc);
1132549ec3ffSdougm 
1133549ec3ffSdougm 		/* Remove and free the entry in the global list. */
1134549ec3ffSdougm 		remove_handle_for_root(impl_handle->tree);
1135549ec3ffSdougm 
1136549ec3ffSdougm 		/*
1137549ec3ffSdougm 		 * If this was the last handle to release, unload the
1138c5f58477Sdougm 		 * plugins that were loaded. Use a mutex in case
1139c5f58477Sdougm 		 * another thread is reinitializing.
1140549ec3ffSdougm 		 */
1141c5f58477Sdougm 		(void) mutex_lock(&sa_global_lock);
1142549ec3ffSdougm 		if (sa_global_handles == NULL)
114357b448deSdougm 			(void) proto_plugin_fini();
1144c5f58477Sdougm 		(void) mutex_unlock(&sa_global_lock);
1145549ec3ffSdougm 
11460fd77660Sgwr 		sa_scf_fini(impl_handle->scfhandle);
11470fd77660Sgwr 		sa_zfs_fini(impl_handle);
11480fd77660Sgwr 
11490fd77660Sgwr 		/* Make sure we free the handle */
11500fd77660Sgwr 		free(impl_handle);
11510fd77660Sgwr 
11526185db85Sdougm 	}
11536185db85Sdougm }
11546185db85Sdougm 
1155b8dc1b43SJoyce McIntosh /*
1156b8dc1b43SJoyce McIntosh  * sa_service(sa_handle_t handle)
1157b8dc1b43SJoyce McIntosh  *
1158b8dc1b43SJoyce McIntosh  * Returns the service for which the handle is currently initialized.
1159b8dc1b43SJoyce McIntosh  */
1160b8dc1b43SJoyce McIntosh int
sa_service(sa_handle_t handle)1161b8dc1b43SJoyce McIntosh sa_service(sa_handle_t handle)
1162b8dc1b43SJoyce McIntosh {
1163b8dc1b43SJoyce McIntosh 	if (handle == NULL)
1164b8dc1b43SJoyce McIntosh 		return (0);
1165b8dc1b43SJoyce McIntosh 
1166b8dc1b43SJoyce McIntosh 	return (((sa_handle_impl_t)handle)->sa_service);
1167b8dc1b43SJoyce McIntosh }
1168b8dc1b43SJoyce McIntosh 
11696185db85Sdougm /*
11706185db85Sdougm  * sa_get_protocols(char **protocol)
11716185db85Sdougm  *	Get array of protocols that are supported
11726185db85Sdougm  *	Returns pointer to an allocated and NULL terminated
11736185db85Sdougm  *	array of strings.  Caller must free.
11746185db85Sdougm  *	This really should be determined dynamically.
11756185db85Sdougm  *	If there aren't any defined, return -1.
11766185db85Sdougm  *	Use free() to return memory.
11776185db85Sdougm  */
11786185db85Sdougm 
11796185db85Sdougm int
sa_get_protocols(char *** protocols)11806185db85Sdougm sa_get_protocols(char ***protocols)
11816185db85Sdougm {
11826185db85Sdougm 	int numproto = -1;
11836185db85Sdougm 
11846185db85Sdougm 	if (protocols != NULL) {
118557b448deSdougm 		struct sa_proto_plugin *plug;
118657b448deSdougm 		for (numproto = 0, plug = sap_proto_list; plug != NULL;
11876185db85Sdougm 		    plug = plug->plugin_next) {
118857b448deSdougm 			numproto++;
118957b448deSdougm 		}
119057b448deSdougm 
119157b448deSdougm 		*protocols = calloc(numproto + 1,  sizeof (char *));
119257b448deSdougm 		if (*protocols != NULL) {
119357b448deSdougm 			int ret = 0;
119457b448deSdougm 			for (plug = sap_proto_list; plug != NULL;
119557b448deSdougm 			    plug = plug->plugin_next) {
119657b448deSdougm 				/* faking for now */
119757b448deSdougm 				(*protocols)[ret++] =
119857b448deSdougm 				    plug->plugin_ops->sa_protocol;
119957b448deSdougm 			}
120057b448deSdougm 		} else {
120157b448deSdougm 			numproto = -1;
12026185db85Sdougm 		}
12036185db85Sdougm 	}
12046185db85Sdougm 	return (numproto);
12056185db85Sdougm }
12066185db85Sdougm 
12076185db85Sdougm /*
12086185db85Sdougm  * find_group_by_name(node, group)
12096185db85Sdougm  *
12106185db85Sdougm  * search the XML document subtree specified by node to find the group
12116185db85Sdougm  * specified by group. Searching subtree allows subgroups to be
12126185db85Sdougm  * searched for.
12136185db85Sdougm  */
12146185db85Sdougm 
12156185db85Sdougm static xmlNodePtr
find_group_by_name(xmlNodePtr node,xmlChar * group)12166185db85Sdougm find_group_by_name(xmlNodePtr node, xmlChar *group)
12176185db85Sdougm {
12186185db85Sdougm 	xmlChar *name = NULL;
12196185db85Sdougm 
12206185db85Sdougm 	for (node = node->xmlChildrenNode; node != NULL;
12216185db85Sdougm 	    node = node->next) {
122257b448deSdougm 		if (xmlStrcmp(node->name, (xmlChar *)"group") == 0) {
122357b448deSdougm 			/* if no groupname, return the first found */
122457b448deSdougm 			if (group == NULL)
122557b448deSdougm 				break;
122657b448deSdougm 			name = xmlGetProp(node, (xmlChar *)"name");
122757b448deSdougm 			if (name != NULL && xmlStrcmp(name, group) == 0)
122857b448deSdougm 				break;
122957b448deSdougm 			if (name != NULL) {
123057b448deSdougm 				xmlFree(name);
123157b448deSdougm 				name = NULL;
123257b448deSdougm 			}
12336185db85Sdougm 		}
12346185db85Sdougm 	}
12356185db85Sdougm 	if (name != NULL)
123657b448deSdougm 		xmlFree(name);
12376185db85Sdougm 	return (node);
12386185db85Sdougm }
12396185db85Sdougm 
12406185db85Sdougm /*
12416185db85Sdougm  * sa_get_group(groupname)
12426185db85Sdougm  *	Return the "group" specified.  If groupname is NULL,
12436185db85Sdougm  *	return the first group of the list of groups.
12446185db85Sdougm  */
12456185db85Sdougm sa_group_t
sa_get_group(sa_handle_t handle,char * groupname)1246549ec3ffSdougm sa_get_group(sa_handle_t handle, char *groupname)
12476185db85Sdougm {
12486185db85Sdougm 	xmlNodePtr node = NULL;
12496185db85Sdougm 	char *subgroup = NULL;
12506185db85Sdougm 	char *group = NULL;
1251549ec3ffSdougm 	sa_handle_impl_t impl_handle = (sa_handle_impl_t)handle;
12526185db85Sdougm 
1253549ec3ffSdougm 	if (impl_handle != NULL && impl_handle->tree != NULL) {
125457b448deSdougm 		if (groupname != NULL) {
125557b448deSdougm 			group = strdup(groupname);
1256a3351425Sdougm 			if (group != NULL) {
1257a3351425Sdougm 				subgroup = strchr(group, '/');
1258a3351425Sdougm 				if (subgroup != NULL)
1259a3351425Sdougm 					*subgroup++ = '\0';
1260a3351425Sdougm 			}
126157b448deSdougm 		}
1262a3351425Sdougm 		/*
1263a3351425Sdougm 		 * We want to find the, possibly, named group. If
1264a3351425Sdougm 		 * group is not NULL, then lookup the name. If it is
1265a3351425Sdougm 		 * NULL, we only do the find if groupname is also
1266a3351425Sdougm 		 * NULL. This allows lookup of the "first" group in
1267a3351425Sdougm 		 * the internal list.
1268a3351425Sdougm 		 */
1269a3351425Sdougm 		if (group != NULL || groupname == NULL)
1270a3351425Sdougm 			node = find_group_by_name(impl_handle->tree,
1271a3351425Sdougm 			    (xmlChar *)group);
1272a3351425Sdougm 
127357b448deSdougm 		/* if a subgroup, find it before returning */
127457b448deSdougm 		if (subgroup != NULL && node != NULL)
127557b448deSdougm 			node = find_group_by_name(node, (xmlChar *)subgroup);
12766185db85Sdougm 	}
12776185db85Sdougm 	if (node != NULL && (char *)group != NULL)
127857b448deSdougm 		(void) sa_get_instance(impl_handle->scfhandle, (char *)group);
12796185db85Sdougm 	if (group != NULL)
128057b448deSdougm 		free(group);
12816185db85Sdougm 	return ((sa_group_t)(node));
12826185db85Sdougm }
12836185db85Sdougm 
12846185db85Sdougm /*
12856185db85Sdougm  * sa_get_next_group(group)
12866185db85Sdougm  *	Return the "next" group after the specified group from
12876185db85Sdougm  *	the internal group list.  NULL if there are no more.
12886185db85Sdougm  */
12896185db85Sdougm sa_group_t
sa_get_next_group(sa_group_t group)12906185db85Sdougm sa_get_next_group(sa_group_t group)
12916185db85Sdougm {
12926185db85Sdougm 	xmlNodePtr ngroup = NULL;
12936185db85Sdougm 	if (group != NULL) {
129457b448deSdougm 		for (ngroup = ((xmlNodePtr)group)->next; ngroup != NULL;
12956185db85Sdougm 		    ngroup = ngroup->next) {
129657b448deSdougm 			if (xmlStrcmp(ngroup->name, (xmlChar *)"group") == 0)
129757b448deSdougm 				break;
129857b448deSdougm 		}
12996185db85Sdougm 	}
13006185db85Sdougm 	return ((sa_group_t)ngroup);
13016185db85Sdougm }
13026185db85Sdougm 
13036185db85Sdougm /*
13046185db85Sdougm  * sa_get_share(group, sharepath)
13056185db85Sdougm  *	Return the share object for the share specified. The share
13066185db85Sdougm  *	must be in the specified group.  Return NULL if not found.
13076185db85Sdougm  */
13086185db85Sdougm sa_share_t
sa_get_share(sa_group_t group,char * sharepath)13096185db85Sdougm sa_get_share(sa_group_t group, char *sharepath)
13106185db85Sdougm {
13116185db85Sdougm 	xmlNodePtr node = NULL;
13126185db85Sdougm 	xmlChar *path;
13136185db85Sdougm 
13146185db85Sdougm 	/*
13156185db85Sdougm 	 * For future scalability, this should end up building a cache
13166185db85Sdougm 	 * since it will get called regularly by the mountd and info
13176185db85Sdougm 	 * services.
13186185db85Sdougm 	 */
13196185db85Sdougm 	if (group != NULL) {
132057b448deSdougm 		for (node = ((xmlNodePtr)group)->children; node != NULL;
13216185db85Sdougm 		    node = node->next) {
132257b448deSdougm 			if (xmlStrcmp(node->name, (xmlChar *)"share") == 0) {
132357b448deSdougm 				if (sharepath == NULL) {
132457b448deSdougm 					break;
132557b448deSdougm 				} else {
132657b448deSdougm 					/* is it the correct share? */
132757b448deSdougm 					path = xmlGetProp(node,
132857b448deSdougm 					    (xmlChar *)"path");
132957b448deSdougm 					if (path != NULL &&
133057b448deSdougm 					    xmlStrcmp(path,
133157b448deSdougm 					    (xmlChar *)sharepath) == 0) {
133257b448deSdougm 						xmlFree(path);
133357b448deSdougm 						break;
133457b448deSdougm 					}
133557b448deSdougm 					xmlFree(path);
133657b448deSdougm 				}
13376185db85Sdougm 			}
13386185db85Sdougm 		}
13396185db85Sdougm 	}
13406185db85Sdougm 	return ((sa_share_t)node);
13416185db85Sdougm }
13426185db85Sdougm 
13436185db85Sdougm /*
13446185db85Sdougm  * sa_get_next_share(share)
13456185db85Sdougm  *	Return the next share following the specified share
13466185db85Sdougm  *	from the internal list of shares. Returns NULL if there
13476185db85Sdougm  *	are no more shares.  The list is relative to the same
13486185db85Sdougm  *	group.
13496185db85Sdougm  */
13506185db85Sdougm sa_share_t
sa_get_next_share(sa_share_t share)13516185db85Sdougm sa_get_next_share(sa_share_t share)
13526185db85Sdougm {
13536185db85Sdougm 	xmlNodePtr node = NULL;
13546185db85Sdougm 
13556185db85Sdougm 	if (share != NULL) {
135657b448deSdougm 		for (node = ((xmlNodePtr)share)->next; node != NULL;
13576185db85Sdougm 		    node = node->next) {
135857b448deSdougm 			if (xmlStrcmp(node->name, (xmlChar *)"share") == 0) {
135957b448deSdougm 				break;
136057b448deSdougm 			}
13616185db85Sdougm 		}
13626185db85Sdougm 	}
13636185db85Sdougm 	return ((sa_share_t)node);
13646185db85Sdougm }
13656185db85Sdougm 
13666185db85Sdougm /*
13676185db85Sdougm  * _sa_get_child_node(node, type)
13686185db85Sdougm  *
13696185db85Sdougm  * find the child node of the specified node that has "type". This is
13706185db85Sdougm  * used to implement several internal functions.
13716185db85Sdougm  */
13726185db85Sdougm 
13736185db85Sdougm static xmlNodePtr
_sa_get_child_node(xmlNodePtr node,xmlChar * type)13746185db85Sdougm _sa_get_child_node(xmlNodePtr node, xmlChar *type)
13756185db85Sdougm {
13766185db85Sdougm 	xmlNodePtr child;
13776185db85Sdougm 	for (child = node->xmlChildrenNode; child != NULL;
13786185db85Sdougm 	    child = child->next)
137957b448deSdougm 		if (xmlStrcmp(child->name, type) == 0)
138057b448deSdougm 			return (child);
13816185db85Sdougm 	return ((xmlNodePtr)NULL);
13826185db85Sdougm }
13836185db85Sdougm 
13846185db85Sdougm /*
13856185db85Sdougm  *  find_share(group, path)
13866185db85Sdougm  *
13876185db85Sdougm  * Search all the shares in the specified group for one that has the
13886185db85Sdougm  * specified path.
13896185db85Sdougm  */
13906185db85Sdougm 
13916185db85Sdougm static sa_share_t
find_share(sa_group_t group,char * sharepath)13926185db85Sdougm find_share(sa_group_t group, char *sharepath)
13936185db85Sdougm {
13946185db85Sdougm 	sa_share_t share;
13956185db85Sdougm 	char *path;
13966185db85Sdougm 
13976185db85Sdougm 	for (share = sa_get_share(group, NULL); share != NULL;
13986185db85Sdougm 	    share = sa_get_next_share(share)) {
139957b448deSdougm 		path = sa_get_share_attr(share, "path");
140057b448deSdougm 		if (path != NULL && strcmp(path, sharepath) == 0) {
140157b448deSdougm 			sa_free_attr_string(path);
140257b448deSdougm 			break;
140357b448deSdougm 		}
140457b448deSdougm 		if (path != NULL)
140557b448deSdougm 			sa_free_attr_string(path);
14066185db85Sdougm 	}
14076185db85Sdougm 	return (share);
14086185db85Sdougm }
14096185db85Sdougm 
14106185db85Sdougm /*
14116185db85Sdougm  * sa_get_sub_group(group)
14126185db85Sdougm  *
14136185db85Sdougm  * Get the first sub-group of group. The sa_get_next_group() function
14146185db85Sdougm  * can be used to get the rest. This is currently only used for ZFS
14156185db85Sdougm  * sub-groups but could be used to implement a more general mechanism.
14166185db85Sdougm  */
14176185db85Sdougm 
14186185db85Sdougm sa_group_t
sa_get_sub_group(sa_group_t group)14196185db85Sdougm sa_get_sub_group(sa_group_t group)
14206185db85Sdougm {
14216185db85Sdougm 	return ((sa_group_t)_sa_get_child_node((xmlNodePtr)group,
142257b448deSdougm 	    (xmlChar *)"group"));
14236185db85Sdougm }
14246185db85Sdougm 
14256185db85Sdougm /*
14266185db85Sdougm  * sa_find_share(sharepath)
14276185db85Sdougm  *	Finds a share regardless of group.  In the future, this
14286185db85Sdougm  *	function should utilize a cache and hash table of some kind.
14296185db85Sdougm  *	The current assumption is that a path will only be shared
14306185db85Sdougm  *	once.  In the future, this may change as implementation of
14316185db85Sdougm  *	resource names comes into being.
14326185db85Sdougm  */
14336185db85Sdougm sa_share_t
sa_find_share(sa_handle_t handle,char * sharepath)1434549ec3ffSdougm sa_find_share(sa_handle_t handle, char *sharepath)
14356185db85Sdougm {
14366185db85Sdougm 	sa_group_t group;
14376185db85Sdougm 	sa_group_t zgroup;
14386185db85Sdougm 	sa_share_t share = NULL;
14396185db85Sdougm 	int done = 0;
14406185db85Sdougm 
1441549ec3ffSdougm 	for (group = sa_get_group(handle, NULL); group != NULL && !done;
144257b448deSdougm 	    group = sa_get_next_group(group)) {
144357b448deSdougm 		if (is_zfs_group(group)) {
144457b448deSdougm 			for (zgroup =
144557b448deSdougm 			    (sa_group_t)_sa_get_child_node((xmlNodePtr)group,
144657b448deSdougm 			    (xmlChar *)"group");
144757b448deSdougm 			    zgroup != NULL;
144857b448deSdougm 			    zgroup = sa_get_next_group(zgroup)) {
144957b448deSdougm 				share = find_share(zgroup, sharepath);
145057b448deSdougm 				if (share != NULL)
145157b448deSdougm 					break;
145257b448deSdougm 			}
145357b448deSdougm 		} else {
145457b448deSdougm 			share = find_share(group, sharepath);
14556185db85Sdougm 		}
145657b448deSdougm 		if (share != NULL)
145757b448deSdougm 			break;
14586185db85Sdougm 	}
14596185db85Sdougm 	return (share);
14606185db85Sdougm }
14616185db85Sdougm 
14626185db85Sdougm /*
1463f345c0beSdougm  *  sa_check_path(group, path, strictness)
14646185db85Sdougm  *
1465da6c28aaSamw  * Check that path is a valid path relative to the group.  Currently,
14666185db85Sdougm  * we are ignoring the group and checking only the NFS rules. Later,
14676185db85Sdougm  * we may want to use the group to then check against the protocols
1468f345c0beSdougm  * enabled on the group. The strictness values mean:
1469f345c0beSdougm  * SA_CHECK_NORMAL == only check newpath against shares that are active
1470f345c0beSdougm  * SA_CHECK_STRICT == check newpath against both active shares and those
1471f345c0beSdougm  *		      stored in the repository
14726185db85Sdougm  */
14736185db85Sdougm 
14746185db85Sdougm int
sa_check_path(sa_group_t group,char * path,int strictness)1475f345c0beSdougm sa_check_path(sa_group_t group, char *path, int strictness)
14766185db85Sdougm {
1477549ec3ffSdougm 	sa_handle_t handle;
1478549ec3ffSdougm 
1479549ec3ffSdougm 	handle = sa_find_group_handle(group);
14809fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	if (handle == NULL)
14819fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 		return (SA_BAD_PATH);
14829fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 
1483549ec3ffSdougm 	return (validpath(handle, path, strictness));
14846185db85Sdougm }
14856185db85Sdougm 
14866185db85Sdougm /*
1487da6c28aaSamw  * mark_excluded_protos(group, share, flags)
1488da6c28aaSamw  *
1489da6c28aaSamw  * Walk through all the protocols enabled for the group and check to
1490da6c28aaSamw  * see if the share has any of them should be in the exclude list
1491da6c28aaSamw  * based on the featureset of the protocol. If there are any, add the
1492da6c28aaSamw  * "exclude" property to the share.
1493da6c28aaSamw  */
1494da6c28aaSamw static void
mark_excluded_protos(sa_group_t group,xmlNodePtr share,uint64_t flags)1495da6c28aaSamw mark_excluded_protos(sa_group_t group, xmlNodePtr share, uint64_t flags)
1496da6c28aaSamw {
1497da6c28aaSamw 	sa_optionset_t optionset;
1498da6c28aaSamw 	char exclude_list[SA_STRSIZE];
1499da6c28aaSamw 	char *sep = "";
1500da6c28aaSamw 
1501da6c28aaSamw 	exclude_list[0] = '\0';
1502da6c28aaSamw 	for (optionset = sa_get_optionset(group, NULL);
1503da6c28aaSamw 	    optionset != NULL;
1504da6c28aaSamw 	    optionset = sa_get_next_optionset(optionset)) {
1505da6c28aaSamw 		char *value;
1506da6c28aaSamw 		uint64_t features;
1507da6c28aaSamw 		value = sa_get_optionset_attr(optionset, "type");
1508da6c28aaSamw 		if (value == NULL)
1509da6c28aaSamw 			continue;
1510da6c28aaSamw 		features = sa_proto_get_featureset(value);
1511da6c28aaSamw 		if (!(features & flags)) {
1512da6c28aaSamw 			(void) strlcat(exclude_list, sep,
1513da6c28aaSamw 			    sizeof (exclude_list));
1514da6c28aaSamw 			(void) strlcat(exclude_list, value,
1515da6c28aaSamw 			    sizeof (exclude_list));
1516da6c28aaSamw 			sep = ",";
1517da6c28aaSamw 		}
1518fe1c642dSBill Krier 		sa_free_attr_string(value);
1519da6c28aaSamw 	}
1520da6c28aaSamw 	if (exclude_list[0] != '\0')
15214bff34e3Sthurlow 		(void) xmlSetProp(share, (xmlChar *)"exclude",
1522da6c28aaSamw 		    (xmlChar *)exclude_list);
1523da6c28aaSamw }
1524da6c28aaSamw 
1525da6c28aaSamw /*
1526da6c28aaSamw  * get_all_features(group)
1527da6c28aaSamw  *
1528da6c28aaSamw  * Walk through all the protocols on the group and collect all
1529da6c28aaSamw  * possible enabled features. This is the OR of all the featuresets.
1530da6c28aaSamw  */
1531da6c28aaSamw static uint64_t
get_all_features(sa_group_t group)1532da6c28aaSamw get_all_features(sa_group_t group)
1533da6c28aaSamw {
1534da6c28aaSamw 	sa_optionset_t optionset;
1535da6c28aaSamw 	uint64_t features = 0;
1536da6c28aaSamw 
1537da6c28aaSamw 	for (optionset = sa_get_optionset(group, NULL);
1538da6c28aaSamw 	    optionset != NULL;
1539da6c28aaSamw 	    optionset = sa_get_next_optionset(optionset)) {
1540da6c28aaSamw 		char *value;
1541da6c28aaSamw 		value = sa_get_optionset_attr(optionset, "type");
1542da6c28aaSamw 		if (value == NULL)
1543da6c28aaSamw 			continue;
1544da6c28aaSamw 		features |= sa_proto_get_featureset(value);
1545da6c28aaSamw 		sa_free_attr_string(value);
1546da6c28aaSamw 	}
1547da6c28aaSamw 	return (features);
1548da6c28aaSamw }
1549da6c28aaSamw 
1550da6c28aaSamw 
1551da6c28aaSamw /*
1552da6c28aaSamw  * _sa_add_share(group, sharepath, persist, *error, flags)
15536185db85Sdougm  *
1554da6c28aaSamw  * Common code for all types of add_share. sa_add_share() is the
15556185db85Sdougm  * public API, we also need to be able to do this when parsing legacy
15566185db85Sdougm  * files and construction of the internal configuration while
1557da6c28aaSamw  * extracting config info from SMF. "flags" indicates if some
1558da6c28aaSamw  * protocols need relaxed rules while other don't. These values are
1559da6c28aaSamw  * the featureset values defined in libshare.h.
15606185db85Sdougm  */
15616185db85Sdougm 
15626185db85Sdougm sa_share_t
_sa_add_share(sa_group_t group,char * sharepath,int persist,int * error,uint64_t flags)1563da6c28aaSamw _sa_add_share(sa_group_t group, char *sharepath, int persist, int *error,
1564da6c28aaSamw     uint64_t flags)
15656185db85Sdougm {
15666185db85Sdougm 	xmlNodePtr node = NULL;
15676185db85Sdougm 	int err;
15686185db85Sdougm 
15696185db85Sdougm 	err  = SA_OK; /* assume success */
15706185db85Sdougm 
157157b448deSdougm 	node = xmlNewChild((xmlNodePtr)group, NULL, (xmlChar *)"share", NULL);
1572da6c28aaSamw 	if (node == NULL) {
1573da6c28aaSamw 		if (error != NULL)
1574da6c28aaSamw 			*error = SA_NO_MEMORY;
1575da6c28aaSamw 		return (node);
1576da6c28aaSamw 	}
1577da6c28aaSamw 
15784bff34e3Sthurlow 	(void) xmlSetProp(node, (xmlChar *)"path", (xmlChar *)sharepath);
15794bff34e3Sthurlow 	(void) xmlSetProp(node, (xmlChar *)"type",
1580da6c28aaSamw 	    persist ? (xmlChar *)"persist" : (xmlChar *)"transient");
1581da6c28aaSamw 	if (flags != 0)
1582da6c28aaSamw 		mark_excluded_protos(group, node, flags);
1583da6c28aaSamw 	if (persist != SA_SHARE_TRANSIENT) {
1584da6c28aaSamw 		/*
1585da6c28aaSamw 		 * persistent shares come in two flavors: SMF and
1586da6c28aaSamw 		 * ZFS. Sort this one out based on target group and
1587da6c28aaSamw 		 * path type. Both NFS and SMB are supported. First,
1588da6c28aaSamw 		 * check to see if the protocol is enabled on the
1589da6c28aaSamw 		 * subgroup and then setup the share appropriately.
1590da6c28aaSamw 		 */
1591da6c28aaSamw 		if (sa_group_is_zfs(group) &&
1592da6c28aaSamw 		    sa_path_is_zfs(sharepath)) {
1593da6c28aaSamw 			if (sa_get_optionset(group, "nfs") != NULL)
159457b448deSdougm 				err = sa_zfs_set_sharenfs(group, sharepath, 1);
1595da6c28aaSamw 			else if (sa_get_optionset(group, "smb") != NULL)
1596da6c28aaSamw 				err = sa_zfs_set_sharesmb(group, sharepath, 1);
1597da6c28aaSamw 		} else {
1598da6c28aaSamw 			sa_handle_impl_t impl_handle;
1599da6c28aaSamw 			impl_handle =
1600da6c28aaSamw 			    (sa_handle_impl_t)sa_find_group_handle(group);
1601da6c28aaSamw 			if (impl_handle != NULL) {
1602da6c28aaSamw 				err = sa_commit_share(impl_handle->scfhandle,
1603da6c28aaSamw 				    group, (sa_share_t)node);
160457b448deSdougm 			} else {
1605da6c28aaSamw 				err = SA_SYSTEM_ERR;
160657b448deSdougm 			}
160757b448deSdougm 		}
16086185db85Sdougm 	}
1609da6c28aaSamw 	if (err == SA_NO_PERMISSION && persist & SA_SHARE_PARSER)
1610da6c28aaSamw 		/* called by the dfstab parser so could be a show */
1611da6c28aaSamw 		err = SA_OK;
1612da6c28aaSamw 
1613da6c28aaSamw 	if (err != SA_OK) {
1614da6c28aaSamw 		/*
1615da6c28aaSamw 		 * we couldn't commit to the repository so undo
1616da6c28aaSamw 		 * our internal state to reflect reality.
1617da6c28aaSamw 		 */
1618da6c28aaSamw 		xmlUnlinkNode(node);
1619da6c28aaSamw 		xmlFreeNode(node);
1620da6c28aaSamw 		node = NULL;
1621da6c28aaSamw 	}
1622da6c28aaSamw 
16236185db85Sdougm 	if (error != NULL)
162457b448deSdougm 		*error = err;
1625da6c28aaSamw 
16266185db85Sdougm 	return (node);
16276185db85Sdougm }
16286185db85Sdougm 
16296185db85Sdougm /*
16306185db85Sdougm  * sa_add_share(group, sharepath, persist, *error)
16316185db85Sdougm  *
16326185db85Sdougm  *	Add a new share object to the specified group.  The share will
16336185db85Sdougm  *	have the specified sharepath and will only be constructed if
16346185db85Sdougm  *	it is a valid path to be shared.  NULL is returned on error
16356185db85Sdougm  *	and a detailed error value will be returned via the error
16366185db85Sdougm  *	pointer.
16376185db85Sdougm  */
16386185db85Sdougm sa_share_t
sa_add_share(sa_group_t group,char * sharepath,int persist,int * error)16396185db85Sdougm sa_add_share(sa_group_t group, char *sharepath, int persist, int *error)
16406185db85Sdougm {
16416185db85Sdougm 	xmlNodePtr node = NULL;
1642f345c0beSdougm 	int strictness = SA_CHECK_NORMAL;
1643549ec3ffSdougm 	sa_handle_t handle;
1644da6c28aaSamw 	uint64_t special = 0;
1645da6c28aaSamw 	uint64_t features;
1646f345c0beSdougm 
1647f345c0beSdougm 	/*
1648f345c0beSdougm 	 * If the share is to be permanent, use strict checking so a
1649f345c0beSdougm 	 * bad config doesn't get created. Transient shares only need
1650f345c0beSdougm 	 * to check against the currently active
1651f345c0beSdougm 	 * shares. SA_SHARE_PARSER is a modifier used internally to
1652f345c0beSdougm 	 * indicate that we are being called by the dfstab parser and
1653f345c0beSdougm 	 * that we need strict checking in all cases. Normally persist
1654f345c0beSdougm 	 * is in integer value but SA_SHARE_PARSER may be or'd into
1655f345c0beSdougm 	 * it as an override.
1656f345c0beSdougm 	 */
1657f345c0beSdougm 	if (persist & SA_SHARE_PARSER || persist == SA_SHARE_PERMANENT)
165857b448deSdougm 		strictness = SA_CHECK_STRICT;
16596185db85Sdougm 
1660549ec3ffSdougm 	handle = sa_find_group_handle(group);
1661549ec3ffSdougm 
1662da6c28aaSamw 	/*
1663da6c28aaSamw 	 * need to determine if the share is valid. The rules are:
1664da6c28aaSamw 	 *	- The path must not already exist
1665da6c28aaSamw 	 *	- The path must not be a subdir or parent dir of an
1666da6c28aaSamw 	 *	  existing path unless at least one protocol allows it.
1667da6c28aaSamw 	 * The sub/parent check is done in sa_check_path().
1668da6c28aaSamw 	 */
1669da6c28aaSamw 
1670da6c28aaSamw 	if (sa_find_share(handle, sharepath) == NULL) {
1671da6c28aaSamw 		*error = sa_check_path(group, sharepath, strictness);
1672da6c28aaSamw 		features = get_all_features(group);
1673da6c28aaSamw 		switch (*error) {
1674da6c28aaSamw 		case SA_PATH_IS_SUBDIR:
1675da6c28aaSamw 			if (features & SA_FEATURE_ALLOWSUBDIRS)
1676da6c28aaSamw 				special |= SA_FEATURE_ALLOWSUBDIRS;
1677da6c28aaSamw 			break;
1678da6c28aaSamw 		case SA_PATH_IS_PARENTDIR:
1679da6c28aaSamw 			if (features & SA_FEATURE_ALLOWPARDIRS)
1680da6c28aaSamw 				special |= SA_FEATURE_ALLOWPARDIRS;
1681da6c28aaSamw 			break;
1682da6c28aaSamw 		}
1683da6c28aaSamw 		if (*error == SA_OK || special != SA_FEATURE_NONE)
1684da6c28aaSamw 			node = _sa_add_share(group, sharepath, persist,
1685da6c28aaSamw 			    error, special);
1686da6c28aaSamw 	} else {
168757b448deSdougm 		*error = SA_DUPLICATE_NAME;
1688da6c28aaSamw 	}
16896185db85Sdougm 
16906185db85Sdougm 	return ((sa_share_t)node);
16916185db85Sdougm }
16926185db85Sdougm 
16936185db85Sdougm /*
16946185db85Sdougm  * sa_enable_share(share, protocol)
16956185db85Sdougm  *	Enable the specified share to the specified protocol.
16966185db85Sdougm  *	If protocol is NULL, then all protocols.
16976185db85Sdougm  */
16986185db85Sdougm int
sa_enable_share(sa_share_t share,char * protocol)16996185db85Sdougm sa_enable_share(sa_share_t share, char *protocol)
17006185db85Sdougm {
17016185db85Sdougm 	char *sharepath;
17026185db85Sdougm 	struct stat st;
1703da6c28aaSamw 	int err = SA_OK;
1704da6c28aaSamw 	int ret;
17056185db85Sdougm 
17066185db85Sdougm 	sharepath = sa_get_share_attr(share, "path");
1707da6c28aaSamw 	if (sharepath == NULL)
1708da6c28aaSamw 		return (SA_NO_MEMORY);
17096185db85Sdougm 	if (stat(sharepath, &st) < 0) {
171057b448deSdougm 		err = SA_NO_SUCH_PATH;
17116185db85Sdougm 	} else {
171257b448deSdougm 		/* tell the server about the share */
171357b448deSdougm 		if (protocol != NULL) {
1714da6c28aaSamw 			if (excluded_protocol(share, protocol))
1715da6c28aaSamw 				goto done;
1716da6c28aaSamw 
171757b448deSdougm 			/* lookup protocol specific handler */
171857b448deSdougm 			err = sa_proto_share(protocol, share);
171957b448deSdougm 			if (err == SA_OK)
1720da6c28aaSamw 				(void) sa_set_share_attr(share,
1721da6c28aaSamw 				    "shared", "true");
172257b448deSdougm 		} else {
1723da6c28aaSamw 			/* Tell all protocols about the share */
1724da6c28aaSamw 			sa_group_t group;
1725da6c28aaSamw 			sa_optionset_t optionset;
1726da6c28aaSamw 
1727da6c28aaSamw 			group = sa_get_parent_group(share);
1728da6c28aaSamw 
1729da6c28aaSamw 			for (optionset = sa_get_optionset(group, NULL);
1730da6c28aaSamw 			    optionset != NULL;
1731da6c28aaSamw 			    optionset = sa_get_next_optionset(optionset)) {
1732da6c28aaSamw 				char *proto;
1733da6c28aaSamw 				proto = sa_get_optionset_attr(optionset,
1734da6c28aaSamw 				    "type");
1735da6c28aaSamw 				if (proto != NULL) {
1736da6c28aaSamw 					if (!excluded_protocol(share, proto)) {
1737da6c28aaSamw 						ret = sa_proto_share(proto,
1738da6c28aaSamw 						    share);
1739da6c28aaSamw 						if (ret != SA_OK)
1740da6c28aaSamw 							err = ret;
1741da6c28aaSamw 					}
1742da6c28aaSamw 					sa_free_attr_string(proto);
1743da6c28aaSamw 				}
1744da6c28aaSamw 			}
174557b448deSdougm 			(void) sa_set_share_attr(share, "shared", "true");
174657b448deSdougm 		}
17476185db85Sdougm 	}
1748da6c28aaSamw done:
17496185db85Sdougm 	if (sharepath != NULL)
175057b448deSdougm 		sa_free_attr_string(sharepath);
17516185db85Sdougm 	return (err);
17526185db85Sdougm }
17536185db85Sdougm 
17546185db85Sdougm /*
17556185db85Sdougm  * sa_disable_share(share, protocol)
1756da6c28aaSamw  *	Disable the specified share to the specified protocol.  If
1757da6c28aaSamw  *	protocol is NULL, then all protocols that are enabled for the
1758da6c28aaSamw  *	share should be disabled.
17596185db85Sdougm  */
17606185db85Sdougm int
sa_disable_share(sa_share_t share,char * protocol)17616185db85Sdougm sa_disable_share(sa_share_t share, char *protocol)
17626185db85Sdougm {
17636185db85Sdougm 	char *path;
1764da6c28aaSamw 	int err = SA_OK;
17656185db85Sdougm 	int ret = SA_OK;
17666185db85Sdougm 
17676185db85Sdougm 	path = sa_get_share_attr(share, "path");
17686185db85Sdougm 
17696185db85Sdougm 	if (protocol != NULL) {
1770ecd6cf80Smarks 		ret = sa_proto_unshare(share, protocol, path);
17716185db85Sdougm 	} else {
177257b448deSdougm 		/* need to do all protocols */
1773da6c28aaSamw 		sa_group_t group;
1774da6c28aaSamw 		sa_optionset_t optionset;
1775da6c28aaSamw 
1776da6c28aaSamw 		group = sa_get_parent_group(share);
1777da6c28aaSamw 
1778da6c28aaSamw 		/* Tell all protocols about the share */
1779da6c28aaSamw 		for (optionset = sa_get_optionset(group, NULL);
1780da6c28aaSamw 		    optionset != NULL;
1781da6c28aaSamw 		    optionset = sa_get_next_optionset(optionset)) {
1782da6c28aaSamw 			char *proto;
1783da6c28aaSamw 
1784da6c28aaSamw 			proto = sa_get_optionset_attr(optionset, "type");
1785da6c28aaSamw 			if (proto != NULL) {
1786da6c28aaSamw 				err = sa_proto_unshare(share, proto, path);
1787da6c28aaSamw 				if (err != SA_OK)
1788da6c28aaSamw 					ret = err;
1789da6c28aaSamw 				sa_free_attr_string(proto);
1790da6c28aaSamw 			}
1791da6c28aaSamw 		}
17926185db85Sdougm 	}
17936185db85Sdougm 	if (ret == SA_OK)
17946185db85Sdougm 		(void) sa_set_share_attr(share, "shared", NULL);
17956185db85Sdougm 	if (path != NULL)
179657b448deSdougm 		sa_free_attr_string(path);
17976185db85Sdougm 	return (ret);
17986185db85Sdougm }
17996185db85Sdougm 
18006185db85Sdougm /*
18016185db85Sdougm  * sa_remove_share(share)
18026185db85Sdougm  *
18036185db85Sdougm  * remove the specified share from its containing group.
18046185db85Sdougm  * Remove from the SMF or ZFS configuration space.
18056185db85Sdougm  */
18066185db85Sdougm 
18076185db85Sdougm int
sa_remove_share(sa_share_t share)18086185db85Sdougm sa_remove_share(sa_share_t share)
18096185db85Sdougm {
18106185db85Sdougm 	sa_group_t group;
18116185db85Sdougm 	int ret = SA_OK;
18126185db85Sdougm 	char *type;
18136185db85Sdougm 	int transient = 0;
18146185db85Sdougm 	char *groupname;
18156185db85Sdougm 	char *zfs;
18166185db85Sdougm 
18176185db85Sdougm 	type = sa_get_share_attr(share, "type");
18186185db85Sdougm 	group = sa_get_parent_group(share);
18196185db85Sdougm 	zfs = sa_get_group_attr(group, "zfs");
18206185db85Sdougm 	groupname = sa_get_group_attr(group, "name");
18216185db85Sdougm 	if (type != NULL && strcmp(type, "persist") != 0)
182257b448deSdougm 		transient = 1;
18236185db85Sdougm 	if (type != NULL)
182457b448deSdougm 		sa_free_attr_string(type);
18256185db85Sdougm 
18266185db85Sdougm 	/* remove the node from its group then free the memory */
18276185db85Sdougm 
18286185db85Sdougm 	/*
18296185db85Sdougm 	 * need to test if "busy"
18306185db85Sdougm 	 */
18316185db85Sdougm 	/* only do SMF action if permanent */
18326185db85Sdougm 	if (!transient || zfs != NULL) {
183357b448deSdougm 		/* remove from legacy dfstab as well as possible SMF */
1834da6c28aaSamw 		ret = sa_delete_legacy(share, NULL);
183557b448deSdougm 		if (ret == SA_OK) {
183657b448deSdougm 			if (!sa_group_is_zfs(group)) {
183757b448deSdougm 				sa_handle_impl_t impl_handle;
183857b448deSdougm 				impl_handle = (sa_handle_impl_t)
183957b448deSdougm 				    sa_find_group_handle(group);
184057b448deSdougm 				if (impl_handle != NULL) {
184157b448deSdougm 					ret = sa_delete_share(
184257b448deSdougm 					    impl_handle->scfhandle, group,
184357b448deSdougm 					    share);
184457b448deSdougm 				} else {
184557b448deSdougm 					ret = SA_SYSTEM_ERR;
184657b448deSdougm 				}
184757b448deSdougm 			} else {
184857b448deSdougm 				char *sharepath = sa_get_share_attr(share,
184957b448deSdougm 				    "path");
185057b448deSdougm 				if (sharepath != NULL) {
185157b448deSdougm 					ret = sa_zfs_set_sharenfs(group,
185257b448deSdougm 					    sharepath, 0);
185357b448deSdougm 					sa_free_attr_string(sharepath);
185457b448deSdougm 				}
185557b448deSdougm 			}
18566185db85Sdougm 		}
18576185db85Sdougm 	}
18586185db85Sdougm 	if (groupname != NULL)
185957b448deSdougm 		sa_free_attr_string(groupname);
18606185db85Sdougm 	if (zfs != NULL)
186157b448deSdougm 		sa_free_attr_string(zfs);
18626185db85Sdougm 
18636185db85Sdougm 	xmlUnlinkNode((xmlNodePtr)share);
18646185db85Sdougm 	xmlFreeNode((xmlNodePtr)share);
18656185db85Sdougm 	return (ret);
18666185db85Sdougm }
18676185db85Sdougm 
18686185db85Sdougm /*
18696185db85Sdougm  * sa_move_share(group, share)
18706185db85Sdougm  *
18716185db85Sdougm  * move the specified share to the specified group.  Update SMF
18726185db85Sdougm  * appropriately.
18736185db85Sdougm  */
18746185db85Sdougm 
18756185db85Sdougm int
sa_move_share(sa_group_t group,sa_share_t share)18766185db85Sdougm sa_move_share(sa_group_t group, sa_share_t share)
18776185db85Sdougm {
18786185db85Sdougm 	sa_group_t oldgroup;
18796185db85Sdougm 	int ret = SA_OK;
18806185db85Sdougm 
18816185db85Sdougm 	/* remove the node from its group then free the memory */
18826185db85Sdougm 
18836185db85Sdougm 	oldgroup = sa_get_parent_group(share);
18846185db85Sdougm 	if (oldgroup != group) {
188557b448deSdougm 		sa_handle_impl_t impl_handle;
188657b448deSdougm 		xmlUnlinkNode((xmlNodePtr)share);
18876185db85Sdougm 		/*
188857b448deSdougm 		 * now that the share isn't in its old group, add to
188957b448deSdougm 		 * the new one
18906185db85Sdougm 		 */
18914bff34e3Sthurlow 		(void) xmlAddChild((xmlNodePtr)group, (xmlNodePtr)share);
189257b448deSdougm 		/* need to deal with SMF */
189357b448deSdougm 		impl_handle = (sa_handle_impl_t)sa_find_group_handle(group);
189457b448deSdougm 		if (impl_handle != NULL) {
189557b448deSdougm 			/*
189657b448deSdougm 			 * need to remove from old group first and then add to
189757b448deSdougm 			 * new group. Ideally, we would do the other order but
189857b448deSdougm 			 * need to avoid having the share in two groups at the
189957b448deSdougm 			 * same time.
190057b448deSdougm 			 */
190157b448deSdougm 			ret = sa_delete_share(impl_handle->scfhandle, oldgroup,
190257b448deSdougm 			    share);
190357b448deSdougm 			if (ret == SA_OK)
190457b448deSdougm 				ret = sa_commit_share(impl_handle->scfhandle,
190557b448deSdougm 				    group, share);
190657b448deSdougm 		} else {
190757b448deSdougm 			ret = SA_SYSTEM_ERR;
190857b448deSdougm 		}
19096185db85Sdougm 	}
19106185db85Sdougm 	return (ret);
19116185db85Sdougm }
19126185db85Sdougm 
19136185db85Sdougm /*
19146185db85Sdougm  * sa_get_parent_group(share)
19156185db85Sdougm  *
1916da6c28aaSamw  * Return the containing group for the share. If a group was actually
19176185db85Sdougm  * passed in, we don't want a parent so return NULL.
19186185db85Sdougm  */
19196185db85Sdougm 
19206185db85Sdougm sa_group_t
sa_get_parent_group(sa_share_t share)19216185db85Sdougm sa_get_parent_group(sa_share_t share)
19226185db85Sdougm {
19236185db85Sdougm 	xmlNodePtr node = NULL;
19246185db85Sdougm 	if (share != NULL) {
192557b448deSdougm 		node = ((xmlNodePtr)share)->parent;
19266185db85Sdougm 		/*
19276185db85Sdougm 		 * make sure parent is a group and not sharecfg since
19286185db85Sdougm 		 * we may be cheating and passing in a group.
19296185db85Sdougm 		 * Eventually, groups of groups might come into being.
19306185db85Sdougm 		 */
193157b448deSdougm 		if (node == NULL ||
193257b448deSdougm 		    xmlStrcmp(node->name, (xmlChar *)"sharecfg") == 0)
193357b448deSdougm 			node = NULL;
19346185db85Sdougm 	}
19356185db85Sdougm 	return ((sa_group_t)node);
19366185db85Sdougm }
19376185db85Sdougm 
19386185db85Sdougm /*
1939549ec3ffSdougm  * _sa_create_group(impl_handle, groupname)
19406185db85Sdougm  *
19416185db85Sdougm  * Create a group in the document. The caller will need to deal with
19426185db85Sdougm  * configuration store and activation.
19436185db85Sdougm  */
19446185db85Sdougm 
19456185db85Sdougm sa_group_t
_sa_create_group(sa_handle_impl_t impl_handle,char * groupname)1946549ec3ffSdougm _sa_create_group(sa_handle_impl_t impl_handle, char *groupname)
19476185db85Sdougm {
19486185db85Sdougm 	xmlNodePtr node = NULL;
19496185db85Sdougm 
19506185db85Sdougm 	if (sa_valid_group_name(groupname)) {
195157b448deSdougm 		node = xmlNewChild(impl_handle->tree, NULL, (xmlChar *)"group",
195257b448deSdougm 		    NULL);
195357b448deSdougm 		if (node != NULL) {
19544bff34e3Sthurlow 			(void) xmlSetProp(node, (xmlChar *)"name",
195557b448deSdougm 			    (xmlChar *)groupname);
19564bff34e3Sthurlow 			(void) xmlSetProp(node, (xmlChar *)"state",
195757b448deSdougm 			    (xmlChar *)"enabled");
195857b448deSdougm 		}
19596185db85Sdougm 	}
19606185db85Sdougm 	return ((sa_group_t)node);
19616185db85Sdougm }
19626185db85Sdougm 
19636185db85Sdougm /*
19646185db85Sdougm  * _sa_create_zfs_group(group, groupname)
19656185db85Sdougm  *
19666185db85Sdougm  * Create a ZFS subgroup under the specified group. This may
19676185db85Sdougm  * eventually form the basis of general sub-groups, but is currently
19686185db85Sdougm  * restricted to ZFS.
19696185db85Sdougm  */
19706185db85Sdougm sa_group_t
_sa_create_zfs_group(sa_group_t group,char * groupname)19716185db85Sdougm _sa_create_zfs_group(sa_group_t group, char *groupname)
19726185db85Sdougm {
19736185db85Sdougm 	xmlNodePtr node = NULL;
19746185db85Sdougm 
197557b448deSdougm 	node = xmlNewChild((xmlNodePtr)group, NULL, (xmlChar *)"group", NULL);
19766185db85Sdougm 	if (node != NULL) {
19774bff34e3Sthurlow 		(void) xmlSetProp(node, (xmlChar *)"name",
19784bff34e3Sthurlow 		    (xmlChar *)groupname);
19794bff34e3Sthurlow 		(void) xmlSetProp(node, (xmlChar *)"state",
19804bff34e3Sthurlow 		    (xmlChar *)"enabled");
19816185db85Sdougm 	}
19826185db85Sdougm 
19836185db85Sdougm 	return ((sa_group_t)node);
19846185db85Sdougm }
19856185db85Sdougm 
19866185db85Sdougm /*
19876185db85Sdougm  * sa_create_group(groupname, *error)
19886185db85Sdougm  *
19896185db85Sdougm  * Create a new group with groupname.  Need to validate that it is a
19906185db85Sdougm  * legal name for SMF and the construct the SMF service instance of
19916185db85Sdougm  * svc:/network/shares/group to implement the group. All necessary
19926185db85Sdougm  * operational properties must be added to the group at this point
19936185db85Sdougm  * (via the SMF transaction model).
19946185db85Sdougm  */
19956185db85Sdougm sa_group_t
sa_create_group(sa_handle_t handle,char * groupname,int * error)1996549ec3ffSdougm sa_create_group(sa_handle_t handle, char *groupname, int *error)
19976185db85Sdougm {
19986185db85Sdougm 	xmlNodePtr node = NULL;
19996185db85Sdougm 	sa_group_t group;
20006185db85Sdougm 	int ret;
200157b448deSdougm 	char rbacstr[SA_STRSIZE];
2002549ec3ffSdougm 	sa_handle_impl_t impl_handle = (sa_handle_impl_t)handle;
20036185db85Sdougm 
20046185db85Sdougm 	ret = SA_OK;
20056185db85Sdougm 
2006549ec3ffSdougm 	if (impl_handle == NULL || impl_handle->scfhandle == NULL) {
200757b448deSdougm 		ret = SA_SYSTEM_ERR;
200857b448deSdougm 		goto err;
20096185db85Sdougm 	}
20106185db85Sdougm 
2011549ec3ffSdougm 	group = sa_get_group(handle, groupname);
20126185db85Sdougm 	if (group != NULL) {
201357b448deSdougm 		ret = SA_DUPLICATE_NAME;
20146185db85Sdougm 	} else {
201557b448deSdougm 		if (sa_valid_group_name(groupname)) {
201657b448deSdougm 			node = xmlNewChild(impl_handle->tree, NULL,
201757b448deSdougm 			    (xmlChar *)"group", NULL);
201857b448deSdougm 			if (node != NULL) {
20194bff34e3Sthurlow 				(void) xmlSetProp(node, (xmlChar *)"name",
202057b448deSdougm 				    (xmlChar *)groupname);
202157b448deSdougm 				/* default to the group being enabled */
20224bff34e3Sthurlow 				(void) xmlSetProp(node, (xmlChar *)"state",
202357b448deSdougm 				    (xmlChar *)"enabled");
202457b448deSdougm 				ret = sa_create_instance(impl_handle->scfhandle,
202557b448deSdougm 				    groupname);
202657b448deSdougm 				if (ret == SA_OK) {
202757b448deSdougm 					ret = sa_start_transaction(
202857b448deSdougm 					    impl_handle->scfhandle,
202957b448deSdougm 					    "operation");
203057b448deSdougm 				}
203157b448deSdougm 				if (ret == SA_OK) {
203257b448deSdougm 					ret = sa_set_property(
203357b448deSdougm 					    impl_handle->scfhandle,
203457b448deSdougm 					    "state", "enabled");
203557b448deSdougm 					if (ret == SA_OK) {
203657b448deSdougm 						ret = sa_end_transaction(
20375b6e0c46Sdougm 						    impl_handle->scfhandle,
20385b6e0c46Sdougm 						    impl_handle);
203957b448deSdougm 					} else {
204057b448deSdougm 						sa_abort_transaction(
204157b448deSdougm 						    impl_handle->scfhandle);
204257b448deSdougm 					}
204357b448deSdougm 				}
204457b448deSdougm 				if (ret == SA_OK) {
204557b448deSdougm 					/* initialize the RBAC strings */
204657b448deSdougm 					ret = sa_start_transaction(
204757b448deSdougm 					    impl_handle->scfhandle,
204857b448deSdougm 					    "general");
204957b448deSdougm 					if (ret == SA_OK) {
205057b448deSdougm 						(void) snprintf(rbacstr,
205157b448deSdougm 						    sizeof (rbacstr), "%s.%s",
205257b448deSdougm 						    SA_RBAC_MANAGE, groupname);
205357b448deSdougm 						ret = sa_set_property(
205457b448deSdougm 						    impl_handle->scfhandle,
20556185db85Sdougm 						    "action_authorization",
20566185db85Sdougm 						    rbacstr);
205757b448deSdougm 					}
205857b448deSdougm 					if (ret == SA_OK) {
205957b448deSdougm 						(void) snprintf(rbacstr,
206057b448deSdougm 						    sizeof (rbacstr), "%s.%s",
206157b448deSdougm 						    SA_RBAC_VALUE, groupname);
206257b448deSdougm 						ret = sa_set_property(
206357b448deSdougm 						    impl_handle->scfhandle,
20646185db85Sdougm 						    "value_authorization",
20656185db85Sdougm 						    rbacstr);
206657b448deSdougm 					}
206757b448deSdougm 					if (ret == SA_OK) {
206857b448deSdougm 						ret = sa_end_transaction(
20695b6e0c46Sdougm 						    impl_handle->scfhandle,
20705b6e0c46Sdougm 						    impl_handle);
207157b448deSdougm 					} else {
207257b448deSdougm 						sa_abort_transaction(
207357b448deSdougm 						    impl_handle->scfhandle);
207457b448deSdougm 					}
207557b448deSdougm 				}
207657b448deSdougm 				if (ret != SA_OK) {
207757b448deSdougm 					/*
207857b448deSdougm 					 * Couldn't commit the group
207957b448deSdougm 					 * so we need to undo
208057b448deSdougm 					 * internally.
208157b448deSdougm 					 */
208257b448deSdougm 					xmlUnlinkNode(node);
208357b448deSdougm 					xmlFreeNode(node);
208457b448deSdougm 					node = NULL;
208557b448deSdougm 				}
20866185db85Sdougm 			} else {
208757b448deSdougm 				ret = SA_NO_MEMORY;
20886185db85Sdougm 			}
20896185db85Sdougm 		} else {
209057b448deSdougm 			ret = SA_INVALID_NAME;
20916185db85Sdougm 		}
20926185db85Sdougm 	}
20936185db85Sdougm err:
20946185db85Sdougm 	if (error != NULL)
209557b448deSdougm 		*error = ret;
20966185db85Sdougm 	return ((sa_group_t)node);
20976185db85Sdougm }
20986185db85Sdougm 
20996185db85Sdougm /*
21006185db85Sdougm  * sa_remove_group(group)
21016185db85Sdougm  *
21026185db85Sdougm  * Remove the specified group. This deletes from the SMF repository.
21036185db85Sdougm  * All property groups and properties are removed.
21046185db85Sdougm  */
21056185db85Sdougm 
21066185db85Sdougm int
sa_remove_group(sa_group_t group)21076185db85Sdougm sa_remove_group(sa_group_t group)
21086185db85Sdougm {
21096185db85Sdougm 	char *name;
21106185db85Sdougm 	int ret = SA_OK;
2111549ec3ffSdougm 	sa_handle_impl_t impl_handle;
21126185db85Sdougm 
2113549ec3ffSdougm 	impl_handle = (sa_handle_impl_t)sa_find_group_handle(group);
2114549ec3ffSdougm 	if (impl_handle != NULL) {
211557b448deSdougm 		name = sa_get_group_attr(group, "name");
211657b448deSdougm 		if (name != NULL) {
211757b448deSdougm 			ret = sa_delete_instance(impl_handle->scfhandle, name);
211857b448deSdougm 			sa_free_attr_string(name);
211957b448deSdougm 		}
212057b448deSdougm 		xmlUnlinkNode((xmlNodePtr)group); /* make sure unlinked */
212157b448deSdougm 		xmlFreeNode((xmlNodePtr)group);   /* now it is gone */
2122549ec3ffSdougm 	} else {
212357b448deSdougm 		ret = SA_SYSTEM_ERR;
21246185db85Sdougm 	}
21256185db85Sdougm 	return (ret);
21266185db85Sdougm }
21276185db85Sdougm 
21286185db85Sdougm /*
21296185db85Sdougm  * sa_update_config()
21306185db85Sdougm  *
21316185db85Sdougm  * Used to update legacy files that need to be updated in bulk
21326185db85Sdougm  * Currently, this is a placeholder and will go away in a future
21336185db85Sdougm  * release.
21346185db85Sdougm  */
21356185db85Sdougm 
21366185db85Sdougm int
sa_update_config(sa_handle_t handle)2137549ec3ffSdougm sa_update_config(sa_handle_t handle)
21386185db85Sdougm {
21396185db85Sdougm 	/*
21406185db85Sdougm 	 * do legacy files first so we can tell when they change.
21416185db85Sdougm 	 * This will go away when we start updating individual records
21426185db85Sdougm 	 * rather than the whole file.
21436185db85Sdougm 	 */
2144549ec3ffSdougm 	update_legacy_config(handle);
21456185db85Sdougm 	return (SA_OK);
21466185db85Sdougm }
21476185db85Sdougm 
21486185db85Sdougm /*
21496185db85Sdougm  * get_node_attr(node, tag)
21506185db85Sdougm  *
2151da6c28aaSamw  * Get the specified tag(attribute) if it exists on the node.  This is
21526185db85Sdougm  * used internally by a number of attribute oriented functions.
21536185db85Sdougm  */
21546185db85Sdougm 
21556185db85Sdougm static char *
get_node_attr(void * nodehdl,char * tag)21566185db85Sdougm get_node_attr(void *nodehdl, char *tag)
21576185db85Sdougm {
21586185db85Sdougm 	xmlNodePtr node = (xmlNodePtr)nodehdl;
21596185db85Sdougm 	xmlChar *name = NULL;
21606185db85Sdougm 
216157b448deSdougm 	if (node != NULL)
21626185db85Sdougm 		name = xmlGetProp(node, (xmlChar *)tag);
21636185db85Sdougm 	return ((char *)name);
21646185db85Sdougm }
21656185db85Sdougm 
21666185db85Sdougm /*
2167fe1c642dSBill Krier  * set_node_attr(node, tag)
21686185db85Sdougm  *
2169da6c28aaSamw  * Set the specified tag(attribute) to the specified value This is
21706185db85Sdougm  * used internally by a number of attribute oriented functions. It
21716185db85Sdougm  * doesn't update the repository, only the internal document state.
21726185db85Sdougm  */
21736185db85Sdougm 
21746185db85Sdougm void
set_node_attr(void * nodehdl,char * tag,char * value)21756185db85Sdougm set_node_attr(void *nodehdl, char *tag, char *value)
21766185db85Sdougm {
21776185db85Sdougm 	xmlNodePtr node = (xmlNodePtr)nodehdl;
21786185db85Sdougm 	if (node != NULL && tag != NULL) {
217957b448deSdougm 		if (value != NULL)
21804bff34e3Sthurlow 			(void) xmlSetProp(node, (xmlChar *)tag,
21814bff34e3Sthurlow 			    (xmlChar *)value);
218257b448deSdougm 		else
21834bff34e3Sthurlow 			(void) xmlUnsetProp(node, (xmlChar *)tag);
21846185db85Sdougm 	}
21856185db85Sdougm }
21866185db85Sdougm 
21876185db85Sdougm /*
21886185db85Sdougm  * sa_get_group_attr(group, tag)
21896185db85Sdougm  *
21906185db85Sdougm  * Get the specied attribute, if defined, for the group.
21916185db85Sdougm  */
21926185db85Sdougm 
21936185db85Sdougm char *
sa_get_group_attr(sa_group_t group,char * tag)21946185db85Sdougm sa_get_group_attr(sa_group_t group, char *tag)
21956185db85Sdougm {
21966185db85Sdougm 	return (get_node_attr((void *)group, tag));
21976185db85Sdougm }
21986185db85Sdougm 
21996185db85Sdougm /*
22006185db85Sdougm  * sa_set_group_attr(group, tag, value)
22016185db85Sdougm  *
22026185db85Sdougm  * set the specified tag/attribute on the group using value as its
22036185db85Sdougm  * value.
22046185db85Sdougm  *
22056185db85Sdougm  * This will result in setting the property in the SMF repository as
22066185db85Sdougm  * well as in the internal document.
22076185db85Sdougm  */
22086185db85Sdougm 
22096185db85Sdougm int
sa_set_group_attr(sa_group_t group,char * tag,char * value)22106185db85Sdougm sa_set_group_attr(sa_group_t group, char *tag, char *value)
22116185db85Sdougm {
22126185db85Sdougm 	int ret;
22136185db85Sdougm 	char *groupname;
2214549ec3ffSdougm 	sa_handle_impl_t impl_handle;
22156185db85Sdougm 
2216da6c28aaSamw 	/*
2217da6c28aaSamw 	 * ZFS group/subgroup doesn't need the handle so shortcut.
2218da6c28aaSamw 	 */
2219da6c28aaSamw 	if (sa_group_is_zfs(group)) {
2220da6c28aaSamw 		set_node_attr((void *)group, tag, value);
2221da6c28aaSamw 		return (SA_OK);
2222da6c28aaSamw 	}
2223da6c28aaSamw 
2224549ec3ffSdougm 	impl_handle = (sa_handle_impl_t)sa_find_group_handle(group);
2225549ec3ffSdougm 	if (impl_handle != NULL) {
222657b448deSdougm 		groupname = sa_get_group_attr(group, "name");
222757b448deSdougm 		ret = sa_get_instance(impl_handle->scfhandle, groupname);
2228549ec3ffSdougm 		if (ret == SA_OK) {
222957b448deSdougm 			set_node_attr((void *)group, tag, value);
223057b448deSdougm 			ret = sa_start_transaction(impl_handle->scfhandle,
223157b448deSdougm 			    "operation");
223257b448deSdougm 			if (ret == SA_OK) {
223357b448deSdougm 				ret = sa_set_property(impl_handle->scfhandle,
223457b448deSdougm 				    tag, value);
223557b448deSdougm 				if (ret == SA_OK)
2236573b0c00Sdougm 					ret = sa_end_transaction(
22375b6e0c46Sdougm 					    impl_handle->scfhandle,
22385b6e0c46Sdougm 					    impl_handle);
223957b448deSdougm 				else
224057b448deSdougm 					sa_abort_transaction(
224157b448deSdougm 					    impl_handle->scfhandle);
224257b448deSdougm 			}
2243573b0c00Sdougm 			if (ret == SA_SYSTEM_ERR)
2244573b0c00Sdougm 				ret = SA_NO_PERMISSION;
22456185db85Sdougm 		}
224657b448deSdougm 		if (groupname != NULL)
224757b448deSdougm 			sa_free_attr_string(groupname);
2248549ec3ffSdougm 	} else {
224957b448deSdougm 		ret = SA_SYSTEM_ERR;
22506185db85Sdougm 	}
22516185db85Sdougm 	return (ret);
22526185db85Sdougm }
22536185db85Sdougm 
22546185db85Sdougm /*
22556185db85Sdougm  * sa_get_share_attr(share, tag)
22566185db85Sdougm  *
22576185db85Sdougm  * Return the value of the tag/attribute set on the specified
22586185db85Sdougm  * share. Returns NULL if the tag doesn't exist.
22596185db85Sdougm  */
22606185db85Sdougm 
22616185db85Sdougm char *
sa_get_share_attr(sa_share_t share,char * tag)22626185db85Sdougm sa_get_share_attr(sa_share_t share, char *tag)
22636185db85Sdougm {
22646185db85Sdougm 	return (get_node_attr((void *)share, tag));
22656185db85Sdougm }
22666185db85Sdougm 
22676185db85Sdougm /*
22686185db85Sdougm  * _sa_set_share_description(share, description)
22696185db85Sdougm  *
2270da6c28aaSamw  * Add a description tag with text contents to the specified share.  A
2271da6c28aaSamw  * separate XML tag is used rather than a property. This can also be
2272da6c28aaSamw  * used with resources.
22736185db85Sdougm  */
22746185db85Sdougm 
22756185db85Sdougm xmlNodePtr
_sa_set_share_description(void * share,char * content)2276da6c28aaSamw _sa_set_share_description(void *share, char *content)
22776185db85Sdougm {
22786185db85Sdougm 	xmlNodePtr node;
227957b448deSdougm 	node = xmlNewChild((xmlNodePtr)share, NULL, (xmlChar *)"description",
228057b448deSdougm 	    NULL);
22816185db85Sdougm 	xmlNodeSetContent(node, (xmlChar *)content);
22826185db85Sdougm 	return (node);
22836185db85Sdougm }
22846185db85Sdougm 
22856185db85Sdougm /*
22866185db85Sdougm  * sa_set_share_attr(share, tag, value)
22876185db85Sdougm  *
22886185db85Sdougm  * Set the share attribute specified by tag to the specified value. In
22896185db85Sdougm  * the case of "resource", enforce a no duplicates in a group rule. If
22906185db85Sdougm  * the share is not transient, commit the changes to the repository
22916185db85Sdougm  * else just update the share internally.
22926185db85Sdougm  */
22936185db85Sdougm 
22946185db85Sdougm int
sa_set_share_attr(sa_share_t share,char * tag,char * value)22956185db85Sdougm sa_set_share_attr(sa_share_t share, char *tag, char *value)
22966185db85Sdougm {
22976185db85Sdougm 	sa_group_t group;
22986185db85Sdougm 	sa_share_t resource;
22996185db85Sdougm 	int ret = SA_OK;
23006185db85Sdougm 
23016185db85Sdougm 	group = sa_get_parent_group(share);
23026185db85Sdougm 
23036185db85Sdougm 	/*
23046185db85Sdougm 	 * There are some attributes that may have specific
23056185db85Sdougm 	 * restrictions on them. Initially, only "resource" has
23066185db85Sdougm 	 * special meaning that needs to be checked. Only one instance
23076185db85Sdougm 	 * of a resource name may exist within a group.
23086185db85Sdougm 	 */
23096185db85Sdougm 
23106185db85Sdougm 	if (strcmp(tag, "resource") == 0) {
231157b448deSdougm 		resource = sa_get_resource(group, value);
231257b448deSdougm 		if (resource != share && resource != NULL)
231357b448deSdougm 			ret = SA_DUPLICATE_NAME;
23146185db85Sdougm 	}
23156185db85Sdougm 	if (ret == SA_OK) {
231657b448deSdougm 		set_node_attr((void *)share, tag, value);
231757b448deSdougm 		if (group != NULL) {
231857b448deSdougm 			char *type;
231957b448deSdougm 			/* we can probably optimize this some */
232057b448deSdougm 			type = sa_get_share_attr(share, "type");
232157b448deSdougm 			if (type == NULL || strcmp(type, "transient") != 0) {
232257b448deSdougm 				sa_handle_impl_t impl_handle;
232357b448deSdougm 				impl_handle =
232457b448deSdougm 				    (sa_handle_impl_t)sa_find_group_handle(
232557b448deSdougm 				    group);
232657b448deSdougm 				if (impl_handle != NULL) {
232757b448deSdougm 					ret = sa_commit_share(
232857b448deSdougm 					    impl_handle->scfhandle, group,
232957b448deSdougm 					    share);
233057b448deSdougm 				} else {
233157b448deSdougm 					ret = SA_SYSTEM_ERR;
233257b448deSdougm 				}
233357b448deSdougm 			}
233457b448deSdougm 			if (type != NULL)
233557b448deSdougm 				sa_free_attr_string(type);
2336549ec3ffSdougm 		}
23376185db85Sdougm 	}
23386185db85Sdougm 	return (ret);
23396185db85Sdougm }
23406185db85Sdougm 
23416185db85Sdougm /*
23426185db85Sdougm  * sa_get_property_attr(prop, tag)
23436185db85Sdougm  *
23446185db85Sdougm  * Get the value of the specified property attribute. Standard
23456185db85Sdougm  * attributes are "type" and "value".
23466185db85Sdougm  */
23476185db85Sdougm 
23486185db85Sdougm char *
sa_get_property_attr(sa_property_t prop,char * tag)23496185db85Sdougm sa_get_property_attr(sa_property_t prop, char *tag)
23506185db85Sdougm {
23516185db85Sdougm 	return (get_node_attr((void *)prop, tag));
23526185db85Sdougm }
23536185db85Sdougm 
23546185db85Sdougm /*
23556185db85Sdougm  * sa_get_optionset_attr(prop, tag)
23566185db85Sdougm  *
23576185db85Sdougm  * Get the value of the specified property attribute. Standard
23586185db85Sdougm  * attribute is "type".
23596185db85Sdougm  */
23606185db85Sdougm 
23616185db85Sdougm char *
sa_get_optionset_attr(sa_property_t optionset,char * tag)23626185db85Sdougm sa_get_optionset_attr(sa_property_t optionset, char *tag)
23636185db85Sdougm {
23646185db85Sdougm 	return (get_node_attr((void *)optionset, tag));
23656185db85Sdougm 
23666185db85Sdougm }
23676185db85Sdougm 
23686185db85Sdougm /*
23696185db85Sdougm  * sa_set_optionset_attr(optionset, tag, value)
23706185db85Sdougm  *
23716185db85Sdougm  * Set the specified attribute(tag) to the specified value on the
23726185db85Sdougm  * optionset.
23736185db85Sdougm  */
23746185db85Sdougm 
23756185db85Sdougm void
sa_set_optionset_attr(sa_group_t optionset,char * tag,char * value)23766185db85Sdougm sa_set_optionset_attr(sa_group_t optionset, char *tag, char *value)
23776185db85Sdougm {
23786185db85Sdougm 	set_node_attr((void *)optionset, tag, value);
23796185db85Sdougm }
23806185db85Sdougm 
23816185db85Sdougm /*
23826185db85Sdougm  * sa_free_attr_string(string)
23836185db85Sdougm  *
23846185db85Sdougm  * Free the string that was returned in one of the sa_get_*_attr()
23856185db85Sdougm  * functions.
23866185db85Sdougm  */
23876185db85Sdougm 
23886185db85Sdougm void
sa_free_attr_string(char * string)23896185db85Sdougm sa_free_attr_string(char *string)
23906185db85Sdougm {
23916185db85Sdougm 	xmlFree((xmlChar *)string);
23926185db85Sdougm }
23936185db85Sdougm 
23946185db85Sdougm /*
23956185db85Sdougm  * sa_get_optionset(group, proto)
23966185db85Sdougm  *
23976185db85Sdougm  * Return the optionset, if it exists, that is associated with the
23986185db85Sdougm  * specified protocol.
23996185db85Sdougm  */
24006185db85Sdougm 
24016185db85Sdougm sa_optionset_t
sa_get_optionset(void * group,char * proto)24026185db85Sdougm sa_get_optionset(void *group, char *proto)
24036185db85Sdougm {
24046185db85Sdougm 	xmlNodePtr node;
24056185db85Sdougm 	xmlChar *value = NULL;
24066185db85Sdougm 
24076185db85Sdougm 	for (node = ((xmlNodePtr)group)->children; node != NULL;
240857b448deSdougm 	    node = node->next) {
24096185db85Sdougm 		if (xmlStrcmp(node->name, (xmlChar *)"optionset") == 0) {
241057b448deSdougm 			value = xmlGetProp(node, (xmlChar *)"type");
241157b448deSdougm 			if (proto != NULL) {
241257b448deSdougm 				if (value != NULL &&
241357b448deSdougm 				    xmlStrcmp(value, (xmlChar *)proto) == 0) {
241457b448deSdougm 					break;
241557b448deSdougm 				}
241657b448deSdougm 				if (value != NULL) {
241757b448deSdougm 					xmlFree(value);
241857b448deSdougm 					value = NULL;
241957b448deSdougm 				}
242057b448deSdougm 			} else {
242157b448deSdougm 				break;
24226185db85Sdougm 			}
24236185db85Sdougm 		}
24246185db85Sdougm 	}
24256185db85Sdougm 	if (value != NULL)
242657b448deSdougm 		xmlFree(value);
24276185db85Sdougm 	return ((sa_optionset_t)node);
24286185db85Sdougm }
24296185db85Sdougm 
24306185db85Sdougm /*
24316185db85Sdougm  * sa_get_next_optionset(optionset)
24326185db85Sdougm  *
24336185db85Sdougm  * Return the next optionset in the group. NULL if this was the last.
24346185db85Sdougm  */
24356185db85Sdougm 
24366185db85Sdougm sa_optionset_t
sa_get_next_optionset(sa_optionset_t optionset)24376185db85Sdougm sa_get_next_optionset(sa_optionset_t optionset)
24386185db85Sdougm {
24396185db85Sdougm 	xmlNodePtr node;
24406185db85Sdougm 
24416185db85Sdougm 	for (node = ((xmlNodePtr)optionset)->next; node != NULL;
244257b448deSdougm 	    node = node->next) {
24436185db85Sdougm 		if (xmlStrcmp(node->name, (xmlChar *)"optionset") == 0) {
24446185db85Sdougm 			break;
24456185db85Sdougm 		}
24466185db85Sdougm 	}
24476185db85Sdougm 	return ((sa_optionset_t)node);
24486185db85Sdougm }
24496185db85Sdougm 
24506185db85Sdougm /*
24516185db85Sdougm  * sa_get_security(group, sectype, proto)
24526185db85Sdougm  *
24536185db85Sdougm  * Return the security optionset. The internal name is a hold over
24546185db85Sdougm  * from the implementation and will be changed before the API is
24556185db85Sdougm  * finalized. This is really a named optionset that can be negotiated
24566185db85Sdougm  * as a group of properties (like NFS security options).
24576185db85Sdougm  */
24586185db85Sdougm 
24596185db85Sdougm sa_security_t
sa_get_security(sa_group_t group,char * sectype,char * proto)24606185db85Sdougm sa_get_security(sa_group_t group, char *sectype, char *proto)
24616185db85Sdougm {
24626185db85Sdougm 	xmlNodePtr node;
24636185db85Sdougm 	xmlChar *value = NULL;
24646185db85Sdougm 
24656185db85Sdougm 	for (node = ((xmlNodePtr)group)->children; node != NULL;
246657b448deSdougm 	    node = node->next) {
246757b448deSdougm 		if (xmlStrcmp(node->name, (xmlChar *)"security") == 0) {
246857b448deSdougm 			if (proto != NULL) {
246957b448deSdougm 				value = xmlGetProp(node, (xmlChar *)"type");
247057b448deSdougm 				if (value == NULL ||
247157b448deSdougm 				    (value != NULL &&
247257b448deSdougm 				    xmlStrcmp(value, (xmlChar *)proto) != 0)) {
247357b448deSdougm 					/* it doesn't match so continue */
247457b448deSdougm 					xmlFree(value);
247557b448deSdougm 					value = NULL;
247657b448deSdougm 					continue;
247757b448deSdougm 				}
247857b448deSdougm 			}
247957b448deSdougm 			if (value != NULL) {
248057b448deSdougm 				xmlFree(value);
248157b448deSdougm 				value = NULL;
248257b448deSdougm 			}
248357b448deSdougm 			/* potential match */
248457b448deSdougm 			if (sectype != NULL) {
248557b448deSdougm 				value = xmlGetProp(node, (xmlChar *)"sectype");
248657b448deSdougm 				if (value != NULL &&
248757b448deSdougm 				    xmlStrcmp(value, (xmlChar *)sectype) == 0) {
248857b448deSdougm 					break;
248957b448deSdougm 				}
249057b448deSdougm 			} else {
249157b448deSdougm 				break;
249257b448deSdougm 			}
24936185db85Sdougm 		}
24946185db85Sdougm 		if (value != NULL) {
249557b448deSdougm 			xmlFree(value);
249657b448deSdougm 			value = NULL;
24976185db85Sdougm 		}
24986185db85Sdougm 	}
24996185db85Sdougm 	if (value != NULL)
250057b448deSdougm 		xmlFree(value);
25016185db85Sdougm 	return ((sa_security_t)node);
25026185db85Sdougm }
25036185db85Sdougm 
25046185db85Sdougm /*
25056185db85Sdougm  * sa_get_next_security(security)
25066185db85Sdougm  *
25076185db85Sdougm  * Get the next security optionset if one exists.
25086185db85Sdougm  */
25096185db85Sdougm 
25106185db85Sdougm sa_security_t
sa_get_next_security(sa_security_t security)25116185db85Sdougm sa_get_next_security(sa_security_t security)
25126185db85Sdougm {
25136185db85Sdougm 	xmlNodePtr node;
25146185db85Sdougm 
25156185db85Sdougm 	for (node = ((xmlNodePtr)security)->next; node != NULL;
251657b448deSdougm 	    node = node->next) {
25176185db85Sdougm 		if (xmlStrcmp(node->name, (xmlChar *)"security") == 0) {
25186185db85Sdougm 			break;
25196185db85Sdougm 		}
25206185db85Sdougm 	}
25216185db85Sdougm 	return ((sa_security_t)node);
25226185db85Sdougm }
25236185db85Sdougm 
25246185db85Sdougm /*
25256185db85Sdougm  * sa_get_property(optionset, prop)
25266185db85Sdougm  *
25276185db85Sdougm  * Get the property object with the name specified in prop from the
25286185db85Sdougm  * optionset.
25296185db85Sdougm  */
25306185db85Sdougm 
25316185db85Sdougm sa_property_t
sa_get_property(sa_optionset_t optionset,char * prop)25326185db85Sdougm sa_get_property(sa_optionset_t optionset, char *prop)
25336185db85Sdougm {
25346185db85Sdougm 	xmlNodePtr node = (xmlNodePtr)optionset;
25356185db85Sdougm 	xmlChar *value = NULL;
25366185db85Sdougm 
25376185db85Sdougm 	if (optionset == NULL)
253857b448deSdougm 		return (NULL);
25396185db85Sdougm 
25406185db85Sdougm 	for (node = node->children; node != NULL;
254157b448deSdougm 	    node = node->next) {
254257b448deSdougm 		if (xmlStrcmp(node->name, (xmlChar *)"option") == 0) {
254357b448deSdougm 			if (prop == NULL)
254457b448deSdougm 				break;
254557b448deSdougm 			value = xmlGetProp(node, (xmlChar *)"type");
254657b448deSdougm 			if (value != NULL &&
254757b448deSdougm 			    xmlStrcmp(value, (xmlChar *)prop) == 0) {
254857b448deSdougm 				break;
254957b448deSdougm 			}
255057b448deSdougm 			if (value != NULL) {
255157b448deSdougm 				xmlFree(value);
255257b448deSdougm 				value = NULL;
255357b448deSdougm 			}
25546185db85Sdougm 		}
25556185db85Sdougm 	}
25566185db85Sdougm 	if (value != NULL)
25576185db85Sdougm 		xmlFree(value);
25586185db85Sdougm 	if (node != NULL && xmlStrcmp(node->name, (xmlChar *)"option") != 0) {
255957b448deSdougm 		/*
256057b448deSdougm 		 * avoid a non option node -- it is possible to be a
256157b448deSdougm 		 * text node
256257b448deSdougm 		 */
256357b448deSdougm 		node = NULL;
25646185db85Sdougm 	}
25656185db85Sdougm 	return ((sa_property_t)node);
25666185db85Sdougm }
25676185db85Sdougm 
25686185db85Sdougm /*
25696185db85Sdougm  * sa_get_next_property(property)
25706185db85Sdougm  *
25716185db85Sdougm  * Get the next property following the specified property. NULL if
25726185db85Sdougm  * this was the last.
25736185db85Sdougm  */
25746185db85Sdougm 
25756185db85Sdougm sa_property_t
sa_get_next_property(sa_property_t property)25766185db85Sdougm sa_get_next_property(sa_property_t property)
25776185db85Sdougm {
25786185db85Sdougm 	xmlNodePtr node;
25796185db85Sdougm 
25806185db85Sdougm 	for (node = ((xmlNodePtr)property)->next; node != NULL;
258157b448deSdougm 	    node = node->next) {
25826185db85Sdougm 		if (xmlStrcmp(node->name, (xmlChar *)"option") == 0) {
25836185db85Sdougm 			break;
25846185db85Sdougm 		}
25856185db85Sdougm 	}
25866185db85Sdougm 	return ((sa_property_t)node);
25876185db85Sdougm }
25886185db85Sdougm 
25896185db85Sdougm /*
25906185db85Sdougm  * sa_set_share_description(share, content)
25916185db85Sdougm  *
25926185db85Sdougm  * Set the description of share to content.
25936185db85Sdougm  */
25946185db85Sdougm 
25956185db85Sdougm int
sa_set_share_description(sa_share_t share,char * content)25966185db85Sdougm sa_set_share_description(sa_share_t share, char *content)
25976185db85Sdougm {
25986185db85Sdougm 	xmlNodePtr node;
25996185db85Sdougm 	sa_group_t group;
26006185db85Sdougm 	int ret = SA_OK;
26016185db85Sdougm 
26026185db85Sdougm 	for (node = ((xmlNodePtr)share)->children; node != NULL;
260357b448deSdougm 	    node = node->next) {
26046185db85Sdougm 		if (xmlStrcmp(node->name, (xmlChar *)"description") == 0) {
26056185db85Sdougm 			break;
26066185db85Sdougm 		}
26076185db85Sdougm 	}
26086185db85Sdougm 	/* no existing description but want to add */
26096185db85Sdougm 	if (node == NULL && content != NULL) {
26106185db85Sdougm 		/* add a description */
261157b448deSdougm 		node = _sa_set_share_description(share, content);
26126185db85Sdougm 	} else if (node != NULL && content != NULL) {
26136185db85Sdougm 		/* update a description */
26146185db85Sdougm 		xmlNodeSetContent(node, (xmlChar *)content);
26156185db85Sdougm 	} else if (node != NULL && content == NULL) {
26166185db85Sdougm 		/* remove an existing description */
26176185db85Sdougm 		xmlUnlinkNode(node);
26186185db85Sdougm 		xmlFreeNode(node);
26196185db85Sdougm 	}
2620da6c28aaSamw 	group = sa_get_parent_group(share);
2621148c5f43SAlan Wright 	if (group != NULL &&
2622148c5f43SAlan Wright 	    sa_is_persistent(share) && (!sa_group_is_zfs(group))) {
262357b448deSdougm 		sa_handle_impl_t impl_handle;
262457b448deSdougm 		impl_handle = (sa_handle_impl_t)sa_find_group_handle(group);
262557b448deSdougm 		if (impl_handle != NULL) {
262657b448deSdougm 			ret = sa_commit_share(impl_handle->scfhandle, group,
262757b448deSdougm 			    share);
262857b448deSdougm 		} else {
262957b448deSdougm 			ret = SA_SYSTEM_ERR;
263057b448deSdougm 		}
2631549ec3ffSdougm 	}
26326185db85Sdougm 	return (ret);
26336185db85Sdougm }
26346185db85Sdougm 
26356185db85Sdougm /*
26366185db85Sdougm  * fixproblemchars(string)
26376185db85Sdougm  *
26386185db85Sdougm  * don't want any newline or tab characters in the text since these
26396185db85Sdougm  * could break display of data and legacy file formats.
26406185db85Sdougm  */
26416185db85Sdougm static void
fixproblemchars(char * str)26426185db85Sdougm fixproblemchars(char *str)
26436185db85Sdougm {
26446185db85Sdougm 	int c;
26456185db85Sdougm 	for (c = *str; c != '\0'; c = *++str) {
264657b448deSdougm 		if (c == '\t' || c == '\n')
264757b448deSdougm 			*str = ' ';
264857b448deSdougm 		else if (c == '"')
264957b448deSdougm 			*str = '\'';
26506185db85Sdougm 	}
26516185db85Sdougm }
26526185db85Sdougm 
26536185db85Sdougm /*
26546185db85Sdougm  * sa_get_share_description(share)
26556185db85Sdougm  *
26566185db85Sdougm  * Return the description text for the specified share if it
26576185db85Sdougm  * exists. NULL if no description exists.
26586185db85Sdougm  */
26596185db85Sdougm 
26606185db85Sdougm char *
sa_get_share_description(sa_share_t share)26616185db85Sdougm sa_get_share_description(sa_share_t share)
26626185db85Sdougm {
26636185db85Sdougm 	xmlChar *description = NULL;
26646185db85Sdougm 	xmlNodePtr node;
26656185db85Sdougm 
26666185db85Sdougm 	for (node = ((xmlNodePtr)share)->children; node != NULL;
266757b448deSdougm 	    node = node->next) {
266857b448deSdougm 		if (xmlStrcmp(node->name, (xmlChar *)"description") == 0) {
266957b448deSdougm 			break;
267057b448deSdougm 		}
26716185db85Sdougm 	}
26726185db85Sdougm 	if (node != NULL) {
2673da6c28aaSamw 		description = xmlNodeGetContent(node);
267457b448deSdougm 		fixproblemchars((char *)description);
26756185db85Sdougm 	}
26766185db85Sdougm 	return ((char *)description);
26776185db85Sdougm }
26786185db85Sdougm 
26796185db85Sdougm /*
26806185db85Sdougm  * sa_free(share_description(description)
26816185db85Sdougm  *
26826185db85Sdougm  * Free the description string.
26836185db85Sdougm  */
26846185db85Sdougm 
26856185db85Sdougm void
sa_free_share_description(char * description)26866185db85Sdougm sa_free_share_description(char *description)
26876185db85Sdougm {
26886185db85Sdougm 	xmlFree((xmlChar *)description);
26896185db85Sdougm }
26906185db85Sdougm 
26916185db85Sdougm /*
26926185db85Sdougm  * sa_create_optionset(group, proto)
26936185db85Sdougm  *
26946185db85Sdougm  * Create an optionset for the specified protocol in the specied
26956185db85Sdougm  * group. This is manifested as a property group within SMF.
26966185db85Sdougm  */
26976185db85Sdougm 
26986185db85Sdougm sa_optionset_t
sa_create_optionset(sa_group_t group,char * proto)26996185db85Sdougm sa_create_optionset(sa_group_t group, char *proto)
27006185db85Sdougm {
27016185db85Sdougm 	sa_optionset_t optionset;
27026185db85Sdougm 	sa_group_t parent = group;
2703da6c28aaSamw 	sa_share_t share = NULL;
2704da6c28aaSamw 	int err = SA_OK;
2705da6c28aaSamw 	char *id = NULL;
27066185db85Sdougm 
27076185db85Sdougm 	optionset = sa_get_optionset(group, proto);
27086185db85Sdougm 	if (optionset != NULL) {
27096185db85Sdougm 		/* can't have a duplicate protocol */
271057b448deSdougm 		optionset = NULL;
27116185db85Sdougm 	} else {
2712da6c28aaSamw 		/*
2713da6c28aaSamw 		 * Account for resource names being slightly
2714da6c28aaSamw 		 * different.
2715da6c28aaSamw 		 */
2716da6c28aaSamw 		if (sa_is_share(group)) {
2717da6c28aaSamw 			/*
2718da6c28aaSamw 			 * Transient shares do not have an "id" so not an
2719da6c28aaSamw 			 * error to not find one.
2720da6c28aaSamw 			 */
2721da6c28aaSamw 			id = sa_get_share_attr((sa_share_t)group, "id");
2722da6c28aaSamw 		} else if (sa_is_resource(group)) {
2723da6c28aaSamw 			share = sa_get_resource_parent(
2724da6c28aaSamw 			    (sa_resource_t)group);
2725da6c28aaSamw 			id = sa_get_resource_attr(share, "id");
2726da6c28aaSamw 
2727da6c28aaSamw 			/* id can be NULL if the group is transient (ZFS) */
2728da6c28aaSamw 			if (id == NULL && sa_is_persistent(group))
2729da6c28aaSamw 				err = SA_NO_MEMORY;
2730da6c28aaSamw 		}
2731da6c28aaSamw 		if (err == SA_NO_MEMORY) {
2732da6c28aaSamw 			/*
2733da6c28aaSamw 			 * Couldn't get the id for the share or
2734da6c28aaSamw 			 * resource. While this could be a
2735da6c28aaSamw 			 * configuration issue, it is most likely an
2736da6c28aaSamw 			 * out of memory. In any case, fail the create.
2737da6c28aaSamw 			 */
2738da6c28aaSamw 			return (NULL);
2739da6c28aaSamw 		}
2740da6c28aaSamw 
274157b448deSdougm 		optionset = (sa_optionset_t)xmlNewChild((xmlNodePtr)group,
274257b448deSdougm 		    NULL, (xmlChar *)"optionset", NULL);
27436185db85Sdougm 		/*
27446185db85Sdougm 		 * only put to repository if on a group and we were
27456185db85Sdougm 		 * able to create an optionset.
27466185db85Sdougm 		 */
274757b448deSdougm 		if (optionset != NULL) {
274857b448deSdougm 			char oname[SA_STRSIZE];
274957b448deSdougm 			char *groupname;
27506185db85Sdougm 
2751da6c28aaSamw 			/*
2752da6c28aaSamw 			 * Need to get parent group in all cases, but also get
2753da6c28aaSamw 			 * the share if this is a resource.
2754da6c28aaSamw 			 */
2755da6c28aaSamw 			if (sa_is_share(group)) {
275657b448deSdougm 				parent = sa_get_parent_group((sa_share_t)group);
2757da6c28aaSamw 			} else if (sa_is_resource(group)) {
2758da6c28aaSamw 				share = sa_get_resource_parent(
2759da6c28aaSamw 				    (sa_resource_t)group);
2760da6c28aaSamw 				parent = sa_get_parent_group(share);
2761da6c28aaSamw 			}
27626185db85Sdougm 
276357b448deSdougm 			sa_set_optionset_attr(optionset, "type", proto);
27646185db85Sdougm 
276557b448deSdougm 			(void) sa_optionset_name(optionset, oname,
276657b448deSdougm 			    sizeof (oname), id);
276757b448deSdougm 			groupname = sa_get_group_attr(parent, "name");
2768da6c28aaSamw 			if (groupname != NULL && sa_is_persistent(group)) {
276957b448deSdougm 				sa_handle_impl_t impl_handle;
2770da6c28aaSamw 				impl_handle =
2771da6c28aaSamw 				    (sa_handle_impl_t)sa_find_group_handle(
2772da6c28aaSamw 				    group);
277357b448deSdougm 				assert(impl_handle != NULL);
277457b448deSdougm 				if (impl_handle != NULL) {
277557b448deSdougm 					(void) sa_get_instance(
2776da6c28aaSamw 					    impl_handle->scfhandle, groupname);
277757b448deSdougm 					(void) sa_create_pgroup(
277857b448deSdougm 					    impl_handle->scfhandle, oname);
277957b448deSdougm 				}
278057b448deSdougm 			}
278157b448deSdougm 			if (groupname != NULL)
278257b448deSdougm 				sa_free_attr_string(groupname);
27836185db85Sdougm 		}
27846185db85Sdougm 	}
2785da6c28aaSamw 
2786da6c28aaSamw 	if (id != NULL)
2787da6c28aaSamw 		sa_free_attr_string(id);
27886185db85Sdougm 	return (optionset);
27896185db85Sdougm }
27906185db85Sdougm 
27916185db85Sdougm /*
27926185db85Sdougm  * sa_get_property_parent(property)
27936185db85Sdougm  *
27946185db85Sdougm  * Given a property, return the object it is a property of. This will
27956185db85Sdougm  * be an optionset of some type.
27966185db85Sdougm  */
27976185db85Sdougm 
27986185db85Sdougm static sa_optionset_t
sa_get_property_parent(sa_property_t property)27996185db85Sdougm sa_get_property_parent(sa_property_t property)
28006185db85Sdougm {
28016185db85Sdougm 	xmlNodePtr node = NULL;
28026185db85Sdougm 
280357b448deSdougm 	if (property != NULL)
280457b448deSdougm 		node = ((xmlNodePtr)property)->parent;
28056185db85Sdougm 	return ((sa_optionset_t)node);
28066185db85Sdougm }
28076185db85Sdougm 
28086185db85Sdougm /*
28096185db85Sdougm  * sa_get_optionset_parent(optionset)
28106185db85Sdougm  *
28116185db85Sdougm  * Return the parent of the specified optionset. This could be a group
28126185db85Sdougm  * or a share.
28136185db85Sdougm  */
28146185db85Sdougm 
28156185db85Sdougm static sa_group_t
sa_get_optionset_parent(sa_optionset_t optionset)28166185db85Sdougm sa_get_optionset_parent(sa_optionset_t optionset)
28176185db85Sdougm {
28186185db85Sdougm 	xmlNodePtr node = NULL;
28196185db85Sdougm 
282057b448deSdougm 	if (optionset != NULL)
282157b448deSdougm 		node = ((xmlNodePtr)optionset)->parent;
28226185db85Sdougm 	return ((sa_group_t)node);
28236185db85Sdougm }
28246185db85Sdougm 
28256185db85Sdougm /*
28266185db85Sdougm  * zfs_needs_update(share)
28276185db85Sdougm  *
28286185db85Sdougm  * In order to avoid making multiple updates to a ZFS share when
28296185db85Sdougm  * setting properties, the share attribute "changed" will be set to
2830da6c28aaSamw  * true when a property is added or modified.  When done adding
28316185db85Sdougm  * properties, we can then detect that an update is needed.  We then
28326185db85Sdougm  * clear the state here to detect additional changes.
28336185db85Sdougm  */
28346185db85Sdougm 
28356185db85Sdougm static int
zfs_needs_update(sa_share_t share)28366185db85Sdougm zfs_needs_update(sa_share_t share)
28376185db85Sdougm {
28386185db85Sdougm 	char *attr;
28396185db85Sdougm 	int result = 0;
28406185db85Sdougm 
28416185db85Sdougm 	attr = sa_get_share_attr(share, "changed");
28426185db85Sdougm 	if (attr != NULL) {
284357b448deSdougm 		sa_free_attr_string(attr);
28446185db85Sdougm 		result = 1;
28456185db85Sdougm 	}
28466185db85Sdougm 	set_node_attr((void *)share, "changed", NULL);
28476185db85Sdougm 	return (result);
28486185db85Sdougm }
28496185db85Sdougm 
28506185db85Sdougm /*
28516185db85Sdougm  * zfs_set_update(share)
28526185db85Sdougm  *
28536185db85Sdougm  * Set the changed attribute of the share to true.
28546185db85Sdougm  */
28556185db85Sdougm 
28566185db85Sdougm static void
zfs_set_update(sa_share_t share)28576185db85Sdougm zfs_set_update(sa_share_t share)
28586185db85Sdougm {
28596185db85Sdougm 	set_node_attr((void *)share, "changed", "true");
28606185db85Sdougm }
28616185db85Sdougm 
28626185db85Sdougm /*
28636185db85Sdougm  * sa_commit_properties(optionset, clear)
28646185db85Sdougm  *
28656185db85Sdougm  * Check if SMF or ZFS config and either update or abort the pending
28666185db85Sdougm  * changes.
28676185db85Sdougm  */
28686185db85Sdougm 
28696185db85Sdougm int
sa_commit_properties(sa_optionset_t optionset,int clear)28706185db85Sdougm sa_commit_properties(sa_optionset_t optionset, int clear)
28716185db85Sdougm {
28726185db85Sdougm 	sa_group_t group;
28736185db85Sdougm 	sa_group_t parent;
28746185db85Sdougm 	int zfs = 0;
28756185db85Sdougm 	int needsupdate = 0;
28766185db85Sdougm 	int ret = SA_OK;
2877549ec3ffSdougm 	sa_handle_impl_t impl_handle;
28786185db85Sdougm 
28796185db85Sdougm 	group = sa_get_optionset_parent(optionset);
28806185db85Sdougm 	if (group != NULL && (sa_is_share(group) || is_zfs_group(group))) {
288157b448deSdougm 		/* only update ZFS if on a share */
288257b448deSdougm 		parent = sa_get_parent_group(group);
288357b448deSdougm 		zfs++;
288457b448deSdougm 		if (parent != NULL && is_zfs_group(parent))
288557b448deSdougm 			needsupdate = zfs_needs_update(group);
288657b448deSdougm 		else
288757b448deSdougm 			zfs = 0;
28886185db85Sdougm 	}
28896185db85Sdougm 	if (zfs) {
289057b448deSdougm 		if (!clear && needsupdate)
289157b448deSdougm 			ret = sa_zfs_update((sa_share_t)group);
28926185db85Sdougm 	} else {
289357b448deSdougm 		impl_handle = (sa_handle_impl_t)sa_find_group_handle(group);
289457b448deSdougm 		if (impl_handle != NULL) {
289557b448deSdougm 			if (clear) {
289657b448deSdougm 				(void) sa_abort_transaction(
289757b448deSdougm 				    impl_handle->scfhandle);
289857b448deSdougm 			} else {
289957b448deSdougm 				ret = sa_end_transaction(
29005b6e0c46Sdougm 				    impl_handle->scfhandle, impl_handle);
290157b448deSdougm 			}
290257b448deSdougm 		} else {
290357b448deSdougm 			ret = SA_SYSTEM_ERR;
290457b448deSdougm 		}
29056185db85Sdougm 	}
29066185db85Sdougm 	return (ret);
29076185db85Sdougm }
29086185db85Sdougm 
29096185db85Sdougm /*
29106185db85Sdougm  * sa_destroy_optionset(optionset)
29116185db85Sdougm  *
2912da6c28aaSamw  * Remove the optionset from its group. Update the repository to
29136185db85Sdougm  * reflect this change.
29146185db85Sdougm  */
29156185db85Sdougm 
29166185db85Sdougm int
sa_destroy_optionset(sa_optionset_t optionset)29176185db85Sdougm sa_destroy_optionset(sa_optionset_t optionset)
29186185db85Sdougm {
291957b448deSdougm 	char name[SA_STRSIZE];
29206185db85Sdougm 	int len;
2921*2bc647a2SToomas Soome 	int ret = SA_OK;
29226185db85Sdougm 	char *id = NULL;
29236185db85Sdougm 	sa_group_t group;
29246185db85Sdougm 	int ispersist = 1;
29256185db85Sdougm 
29266185db85Sdougm 	/* now delete the prop group */
29276185db85Sdougm 	group = sa_get_optionset_parent(optionset);
2928da6c28aaSamw 	if (group != NULL) {
2929da6c28aaSamw 		if (sa_is_resource(group)) {
2930da6c28aaSamw 			sa_resource_t resource = group;
2931da6c28aaSamw 			sa_share_t share = sa_get_resource_parent(resource);
2932da6c28aaSamw 			group = sa_get_parent_group(share);
2933da6c28aaSamw 			id = sa_get_share_attr(share, "id");
2934da6c28aaSamw 		} else if (sa_is_share(group)) {
2935da6c28aaSamw 			id = sa_get_share_attr((sa_share_t)group, "id");
2936da6c28aaSamw 		}
2937da6c28aaSamw 		ispersist = sa_is_persistent(group);
29386185db85Sdougm 	}
29396185db85Sdougm 	if (ispersist) {
294057b448deSdougm 		sa_handle_impl_t impl_handle;
294157b448deSdougm 		len = sa_optionset_name(optionset, name, sizeof (name), id);
294257b448deSdougm 		impl_handle = (sa_handle_impl_t)sa_find_group_handle(group);
294357b448deSdougm 		if (impl_handle != NULL) {
294457b448deSdougm 			if (len > 0) {
294557b448deSdougm 				ret = sa_delete_pgroup(impl_handle->scfhandle,
294657b448deSdougm 				    name);
294757b448deSdougm 			}
294857b448deSdougm 		} else {
294957b448deSdougm 			ret = SA_SYSTEM_ERR;
2950549ec3ffSdougm 		}
29516185db85Sdougm 	}
29526185db85Sdougm 	xmlUnlinkNode((xmlNodePtr)optionset);
29536185db85Sdougm 	xmlFreeNode((xmlNodePtr)optionset);
29546185db85Sdougm 	if (id != NULL)
295557b448deSdougm 		sa_free_attr_string(id);
29566185db85Sdougm 	return (ret);
29576185db85Sdougm }
29586185db85Sdougm 
29596185db85Sdougm /* private to the implementation */
29606185db85Sdougm int
_sa_remove_optionset(sa_optionset_t optionset)29616185db85Sdougm _sa_remove_optionset(sa_optionset_t optionset)
29626185db85Sdougm {
29636185db85Sdougm 	int ret = SA_OK;
29646185db85Sdougm 
29656185db85Sdougm 	xmlUnlinkNode((xmlNodePtr)optionset);
29666185db85Sdougm 	xmlFreeNode((xmlNodePtr)optionset);
29676185db85Sdougm 	return (ret);
29686185db85Sdougm }
29696185db85Sdougm 
29706185db85Sdougm /*
29716185db85Sdougm  * sa_create_security(group, sectype, proto)
29726185db85Sdougm  *
29736185db85Sdougm  * Create a security optionset (one that has a type name and a
29746185db85Sdougm  * proto). Security is left over from a pure NFS implementation. The
29756185db85Sdougm  * naming will change in the future when the API is released.
29766185db85Sdougm  */
29776185db85Sdougm sa_security_t
sa_create_security(sa_group_t group,char * sectype,char * proto)29786185db85Sdougm sa_create_security(sa_group_t group, char *sectype, char *proto)
29796185db85Sdougm {
29806185db85Sdougm 	sa_security_t security;
29816185db85Sdougm 	char *id = NULL;
29826185db85Sdougm 	sa_group_t parent;
29836185db85Sdougm 	char *groupname = NULL;
29846185db85Sdougm 
29856185db85Sdougm 	if (group != NULL && sa_is_share(group)) {
298657b448deSdougm 		id = sa_get_share_attr((sa_share_t)group, "id");
298757b448deSdougm 		parent = sa_get_parent_group(group);
298857b448deSdougm 		if (parent != NULL)
298957b448deSdougm 			groupname = sa_get_group_attr(parent, "name");
29906185db85Sdougm 	} else if (group != NULL) {
299157b448deSdougm 		groupname = sa_get_group_attr(group, "name");
29926185db85Sdougm 	}
29936185db85Sdougm 
29946185db85Sdougm 	security = sa_get_security(group, sectype, proto);
29956185db85Sdougm 	if (security != NULL) {
29966185db85Sdougm 		/* can't have a duplicate security option */
29976185db85Sdougm 		security = NULL;
29986185db85Sdougm 	} else {
29996185db85Sdougm 		security = (sa_security_t)xmlNewChild((xmlNodePtr)group,
300057b448deSdougm 		    NULL, (xmlChar *)"security", NULL);
30016185db85Sdougm 		if (security != NULL) {
300257b448deSdougm 			char oname[SA_STRSIZE];
30036185db85Sdougm 			sa_set_security_attr(security, "type", proto);
30046185db85Sdougm 
30056185db85Sdougm 			sa_set_security_attr(security, "sectype", sectype);
30066185db85Sdougm 			(void) sa_security_name(security, oname,
300757b448deSdougm 			    sizeof (oname), id);
3008da6c28aaSamw 			if (groupname != NULL && sa_is_persistent(group)) {
300957b448deSdougm 				sa_handle_impl_t impl_handle;
301057b448deSdougm 				impl_handle =
301157b448deSdougm 				    (sa_handle_impl_t)sa_find_group_handle(
301257b448deSdougm 				    group);
301357b448deSdougm 				if (impl_handle != NULL) {
301457b448deSdougm 					(void) sa_get_instance(
301557b448deSdougm 					    impl_handle->scfhandle, groupname);
301657b448deSdougm 					(void) sa_create_pgroup(
301757b448deSdougm 					    impl_handle->scfhandle, oname);
301857b448deSdougm 				}
30196185db85Sdougm 			}
30206185db85Sdougm 		}
30216185db85Sdougm 	}
3022fe1c642dSBill Krier 	if (id != NULL)
3023fe1c642dSBill Krier 		sa_free_attr_string(id);
30246185db85Sdougm 	if (groupname != NULL)
302557b448deSdougm 		sa_free_attr_string(groupname);
30266185db85Sdougm 	return (security);
30276185db85Sdougm }
30286185db85Sdougm 
30296185db85Sdougm /*
30306185db85Sdougm  * sa_destroy_security(security)
30316185db85Sdougm  *
30326185db85Sdougm  * Remove the specified optionset from the document and the
30336185db85Sdougm  * configuration.
30346185db85Sdougm  */
30356185db85Sdougm 
30366185db85Sdougm int
sa_destroy_security(sa_security_t security)30376185db85Sdougm sa_destroy_security(sa_security_t security)
30386185db85Sdougm {
303957b448deSdougm 	char name[SA_STRSIZE];
30406185db85Sdougm 	int len;
30416185db85Sdougm 	int ret = SA_OK;
30426185db85Sdougm 	char *id = NULL;
30436185db85Sdougm 	sa_group_t group;
30446185db85Sdougm 	int iszfs = 0;
30456185db85Sdougm 	int ispersist = 1;
30466185db85Sdougm 
30476185db85Sdougm 	group = sa_get_optionset_parent(security);
30486185db85Sdougm 
30496185db85Sdougm 	if (group != NULL)
305057b448deSdougm 		iszfs = sa_group_is_zfs(group);
30516185db85Sdougm 
30526185db85Sdougm 	if (group != NULL && !iszfs) {
305357b448deSdougm 		if (sa_is_share(group))
3054da6c28aaSamw 			ispersist = sa_is_persistent(group);
305557b448deSdougm 		id = sa_get_share_attr((sa_share_t)group, "id");
30566185db85Sdougm 	}
30576185db85Sdougm 	if (ispersist) {
305857b448deSdougm 		len = sa_security_name(security, name, sizeof (name), id);
305957b448deSdougm 		if (!iszfs && len > 0) {
306057b448deSdougm 			sa_handle_impl_t impl_handle;
306157b448deSdougm 			impl_handle =
306257b448deSdougm 			    (sa_handle_impl_t)sa_find_group_handle(group);
306357b448deSdougm 			if (impl_handle != NULL) {
306457b448deSdougm 				ret = sa_delete_pgroup(impl_handle->scfhandle,
306557b448deSdougm 				    name);
306657b448deSdougm 			} else {
306757b448deSdougm 				ret = SA_SYSTEM_ERR;
306857b448deSdougm 			}
3069549ec3ffSdougm 		}
30706185db85Sdougm 	}
30716185db85Sdougm 	xmlUnlinkNode((xmlNodePtr)security);
30726185db85Sdougm 	xmlFreeNode((xmlNodePtr)security);
307357b448deSdougm 	if (iszfs)
307457b448deSdougm 		ret = sa_zfs_update(group);
30756185db85Sdougm 	if (id != NULL)
307657b448deSdougm 		sa_free_attr_string(id);
30776185db85Sdougm 	return (ret);
30786185db85Sdougm }
30796185db85Sdougm 
30806185db85Sdougm /*
30816185db85Sdougm  * sa_get_security_attr(optionset, tag)
30826185db85Sdougm  *
30836185db85Sdougm  * Return the specified attribute value from the optionset.
30846185db85Sdougm  */
30856185db85Sdougm 
30866185db85Sdougm char *
sa_get_security_attr(sa_property_t optionset,char * tag)30876185db85Sdougm sa_get_security_attr(sa_property_t optionset, char *tag)
30886185db85Sdougm {
30896185db85Sdougm 	return (get_node_attr((void *)optionset, tag));
30906185db85Sdougm 
30916185db85Sdougm }
30926185db85Sdougm 
30936185db85Sdougm /*
30946185db85Sdougm  * sa_set_security_attr(optionset, tag, value)
30956185db85Sdougm  *
30966185db85Sdougm  * Set the optioset attribute specied by tag to the specified value.
30976185db85Sdougm  */
30986185db85Sdougm 
30996185db85Sdougm void
sa_set_security_attr(sa_group_t optionset,char * tag,char * value)31006185db85Sdougm sa_set_security_attr(sa_group_t optionset, char *tag, char *value)
31016185db85Sdougm {
31026185db85Sdougm 	set_node_attr((void *)optionset, tag, value);
31036185db85Sdougm }
31046185db85Sdougm 
31056185db85Sdougm /*
31066185db85Sdougm  * is_nodetype(node, type)
31076185db85Sdougm  *
31086185db85Sdougm  * Check to see if node is of the type specified.
31096185db85Sdougm  */
31106185db85Sdougm 
31116185db85Sdougm static int
is_nodetype(void * node,char * type)31126185db85Sdougm is_nodetype(void *node, char *type)
31136185db85Sdougm {
31146185db85Sdougm 	return (strcmp((char *)((xmlNodePtr)node)->name, type) == 0);
31156185db85Sdougm }
31166185db85Sdougm 
311757b448deSdougm /*
311857b448deSdougm  * add_or_update()
311957b448deSdougm  *
312057b448deSdougm  * Add or update a property. Pulled out of sa_set_prop_by_prop for
312157b448deSdougm  * readability.
312257b448deSdougm  */
312357b448deSdougm static int
add_or_update(scfutilhandle_t * scf_handle,int type,scf_value_t * value,scf_transaction_entry_t * entry,char * name,char * valstr)312457b448deSdougm add_or_update(scfutilhandle_t *scf_handle, int type, scf_value_t *value,
312557b448deSdougm     scf_transaction_entry_t *entry, char *name, char *valstr)
312657b448deSdougm {
312757b448deSdougm 	int ret = SA_SYSTEM_ERR;
312857b448deSdougm 
312957b448deSdougm 	if (value != NULL) {
313057b448deSdougm 		if (type == SA_PROP_OP_ADD)
313157b448deSdougm 			ret = scf_transaction_property_new(scf_handle->trans,
313257b448deSdougm 			    entry, name, SCF_TYPE_ASTRING);
313357b448deSdougm 		else
313457b448deSdougm 			ret = scf_transaction_property_change(scf_handle->trans,
313557b448deSdougm 			    entry, name, SCF_TYPE_ASTRING);
313657b448deSdougm 		if (ret == 0) {
313757b448deSdougm 			ret = scf_value_set_astring(value, valstr);
313857b448deSdougm 			if (ret == 0)
313957b448deSdougm 				ret = scf_entry_add_value(entry, value);
314057b448deSdougm 			if (ret == 0)
314157b448deSdougm 				return (ret);
314257b448deSdougm 			scf_value_destroy(value);
314357b448deSdougm 		} else {
314457b448deSdougm 			scf_entry_destroy(entry);
314557b448deSdougm 		}
314657b448deSdougm 	}
314757b448deSdougm 	return (SA_SYSTEM_ERR);
314857b448deSdougm }
314957b448deSdougm 
31506185db85Sdougm /*
31516185db85Sdougm  * sa_set_prop_by_prop(optionset, group, prop, type)
31526185db85Sdougm  *
31536185db85Sdougm  * Add/remove/update the specified property prop into the optionset or
31546185db85Sdougm  * share. If a share, sort out which property group based on GUID. In
31556185db85Sdougm  * all cases, the appropriate transaction is set (or ZFS share is
31566185db85Sdougm  * marked as needing an update)
31576185db85Sdougm  */
31586185db85Sdougm 
31596185db85Sdougm static int
sa_set_prop_by_prop(sa_optionset_t optionset,sa_group_t group,sa_property_t prop,int type)31606185db85Sdougm sa_set_prop_by_prop(sa_optionset_t optionset, sa_group_t group,
31618a981c33SDaniel Hoffman     sa_property_t prop, int type)
31626185db85Sdougm {
31636185db85Sdougm 	char *name;
31646185db85Sdougm 	char *valstr;
31656185db85Sdougm 	int ret = SA_OK;
31666185db85Sdougm 	scf_transaction_entry_t *entry;
31676185db85Sdougm 	scf_value_t *value;
31686185db85Sdougm 	int opttype; /* 1 == optionset, 0 == security */
31696185db85Sdougm 	char *id = NULL;
31706185db85Sdougm 	int iszfs = 0;
31716185db85Sdougm 	sa_group_t parent = NULL;
3172da6c28aaSamw 	sa_share_t share = NULL;
3173549ec3ffSdougm 	sa_handle_impl_t impl_handle;
3174549ec3ffSdougm 	scfutilhandle_t  *scf_handle;
31756185db85Sdougm 
3176da6c28aaSamw 	if (!sa_is_persistent(group)) {
31776185db85Sdougm 		/*
31786185db85Sdougm 		 * if the group/share is not persistent we don't need
31796185db85Sdougm 		 * to do anything here
31806185db85Sdougm 		 */
318157b448deSdougm 		return (SA_OK);
31826185db85Sdougm 	}
3183549ec3ffSdougm 	impl_handle = (sa_handle_impl_t)sa_find_group_handle(group);
318457b448deSdougm 	if (impl_handle == NULL || impl_handle->scfhandle == NULL)
318557b448deSdougm 		return (SA_SYSTEM_ERR);
3186549ec3ffSdougm 	scf_handle = impl_handle->scfhandle;
31876185db85Sdougm 	name = sa_get_property_attr(prop, "type");
31886185db85Sdougm 	valstr = sa_get_property_attr(prop, "value");
31896185db85Sdougm 	entry = scf_entry_create(scf_handle->handle);
31906185db85Sdougm 	opttype = is_nodetype((void *)optionset, "optionset");
31916185db85Sdougm 
3192da6c28aaSamw 	/*
3193da6c28aaSamw 	 * Check for share vs. resource since they need slightly
3194da6c28aaSamw 	 * different treatment given the hierarchy.
3195da6c28aaSamw 	 */
31966185db85Sdougm 	if (valstr != NULL && entry != NULL) {
319757b448deSdougm 		if (sa_is_share(group)) {
319857b448deSdougm 			parent = sa_get_parent_group(group);
3199da6c28aaSamw 			share = (sa_share_t)group;
320057b448deSdougm 			if (parent != NULL)
320157b448deSdougm 				iszfs = is_zfs_group(parent);
3202da6c28aaSamw 		} else if (sa_is_resource(group)) {
3203da6c28aaSamw 			share = sa_get_parent_group(group);
3204da6c28aaSamw 			if (share != NULL)
3205da6c28aaSamw 				parent = sa_get_parent_group(share);
320657b448deSdougm 		} else {
320757b448deSdougm 			iszfs = is_zfs_group(group);
32086185db85Sdougm 		}
320957b448deSdougm 		if (!iszfs) {
321057b448deSdougm 			if (scf_handle->trans == NULL) {
321157b448deSdougm 				char oname[SA_STRSIZE];
321257b448deSdougm 				char *groupname = NULL;
3213da6c28aaSamw 				if (share != NULL) {
3214da6c28aaSamw 					if (parent != NULL)
321557b448deSdougm 						groupname =
321657b448deSdougm 						    sa_get_group_attr(parent,
321757b448deSdougm 						    "name");
3218da6c28aaSamw 					id = sa_get_share_attr(
3219da6c28aaSamw 					    (sa_share_t)share, "id");
3220549ec3ffSdougm 				} else {
322157b448deSdougm 					groupname = sa_get_group_attr(group,
322257b448deSdougm 					    "name");
32236185db85Sdougm 				}
322457b448deSdougm 				if (groupname != NULL) {
322557b448deSdougm 					ret = sa_get_instance(scf_handle,
322657b448deSdougm 					    groupname);
322757b448deSdougm 					sa_free_attr_string(groupname);
322857b448deSdougm 				}
322957b448deSdougm 				if (opttype)
323057b448deSdougm 					(void) sa_optionset_name(optionset,
323157b448deSdougm 					    oname, sizeof (oname), id);
323257b448deSdougm 				else
323357b448deSdougm 					(void) sa_security_name(optionset,
323457b448deSdougm 					    oname, sizeof (oname), id);
323557b448deSdougm 				ret = sa_start_transaction(scf_handle, oname);
3236fe1c642dSBill Krier 				if (id != NULL)
3237fe1c642dSBill Krier 					sa_free_attr_string(id);
32386185db85Sdougm 			}
323957b448deSdougm 			if (ret == SA_OK) {
324057b448deSdougm 				switch (type) {
324157b448deSdougm 				case SA_PROP_OP_REMOVE:
324257b448deSdougm 					ret = scf_transaction_property_delete(
324357b448deSdougm 					    scf_handle->trans, entry, name);
324457b448deSdougm 					break;
324557b448deSdougm 				case SA_PROP_OP_ADD:
324657b448deSdougm 				case SA_PROP_OP_UPDATE:
324757b448deSdougm 					value = scf_value_create(
324857b448deSdougm 					    scf_handle->handle);
324957b448deSdougm 					ret = add_or_update(scf_handle, type,
325057b448deSdougm 					    value, entry, name, valstr);
325157b448deSdougm 					break;
325257b448deSdougm 				}
325357b448deSdougm 			}
325457b448deSdougm 		} else {
325557b448deSdougm 			/*
325657b448deSdougm 			 * ZFS update. The calling function would have updated
325757b448deSdougm 			 * the internal XML structure. Just need to flag it as
325857b448deSdougm 			 * changed for ZFS.
325957b448deSdougm 			 */
326057b448deSdougm 			zfs_set_update((sa_share_t)group);
326157b448deSdougm 		}
32626185db85Sdougm 	}
32636185db85Sdougm 
32646185db85Sdougm 	if (name != NULL)
326557b448deSdougm 		sa_free_attr_string(name);
32666185db85Sdougm 	if (valstr != NULL)
326757b448deSdougm 		sa_free_attr_string(valstr);
32686185db85Sdougm 	else if (entry != NULL)
326957b448deSdougm 		scf_entry_destroy(entry);
32706185db85Sdougm 
32716185db85Sdougm 	if (ret == -1)
327257b448deSdougm 		ret = SA_SYSTEM_ERR;
32736185db85Sdougm 
32746185db85Sdougm 	return (ret);
32756185db85Sdougm }
32766185db85Sdougm 
32776185db85Sdougm /*
32784bff34e3Sthurlow  * sa_create_section(name, value)
32794bff34e3Sthurlow  *
32804bff34e3Sthurlow  * Create a new section with the specified name and extra data.
32814bff34e3Sthurlow  */
32824bff34e3Sthurlow 
32834bff34e3Sthurlow sa_property_t
sa_create_section(char * name,char * extra)32844bff34e3Sthurlow sa_create_section(char *name, char *extra)
32854bff34e3Sthurlow {
32864bff34e3Sthurlow 	xmlNodePtr node;
32874bff34e3Sthurlow 
32884bff34e3Sthurlow 	node = xmlNewNode(NULL, (xmlChar *)"section");
32894bff34e3Sthurlow 	if (node != NULL) {
32904bff34e3Sthurlow 		if (name != NULL)
32914bff34e3Sthurlow 			(void) xmlSetProp(node, (xmlChar *)"name",
32924bff34e3Sthurlow 			    (xmlChar *)name);
32934bff34e3Sthurlow 		if (extra != NULL)
32944bff34e3Sthurlow 			(void) xmlSetProp(node, (xmlChar *)"extra",
32954bff34e3Sthurlow 			    (xmlChar *)extra);
32964bff34e3Sthurlow 	}
32974bff34e3Sthurlow 	return ((sa_property_t)node);
32984bff34e3Sthurlow }
32994bff34e3Sthurlow 
33004bff34e3Sthurlow void
sa_set_section_attr(sa_property_t sect,char * name,char * value)33014bff34e3Sthurlow sa_set_section_attr(sa_property_t sect, char *name, char *value)
33024bff34e3Sthurlow {
33034bff34e3Sthurlow 	(void) xmlSetProp(sect, (xmlChar *)name, (xmlChar *)value);
33044bff34e3Sthurlow }
33054bff34e3Sthurlow 
33064bff34e3Sthurlow /*
33074bff34e3Sthurlow  * sa_create_property(section, name, value)
33086185db85Sdougm  *
33096185db85Sdougm  * Create a new property with the specified name and value.
33106185db85Sdougm  */
33116185db85Sdougm 
33126185db85Sdougm sa_property_t
sa_create_property(char * name,char * value)33136185db85Sdougm sa_create_property(char *name, char *value)
33146185db85Sdougm {
33156185db85Sdougm 	xmlNodePtr node;
33166185db85Sdougm 
33176185db85Sdougm 	node = xmlNewNode(NULL, (xmlChar *)"option");
33186185db85Sdougm 	if (node != NULL) {
33194bff34e3Sthurlow 		(void) xmlSetProp(node, (xmlChar *)"type", (xmlChar *)name);
33204bff34e3Sthurlow 		(void) xmlSetProp(node, (xmlChar *)"value", (xmlChar *)value);
33216185db85Sdougm 	}
33226185db85Sdougm 	return ((sa_property_t)node);
33236185db85Sdougm }
33246185db85Sdougm 
33256185db85Sdougm /*
33266185db85Sdougm  * sa_add_property(object, property)
33276185db85Sdougm  *
33286185db85Sdougm  * Add the specified property to the object. Issue the appropriate
33296185db85Sdougm  * transaction or mark a ZFS object as needing an update.
33306185db85Sdougm  */
33316185db85Sdougm 
33326185db85Sdougm int
sa_add_property(void * object,sa_property_t property)33336185db85Sdougm sa_add_property(void *object, sa_property_t property)
33346185db85Sdougm {
33356185db85Sdougm 	int ret = SA_OK;
33366185db85Sdougm 	sa_group_t parent;
33376185db85Sdougm 	sa_group_t group;
33386185db85Sdougm 	char *proto;
33396185db85Sdougm 
33406185db85Sdougm 	if (property != NULL) {
33413b61b335Sdougm 		sa_handle_t handle;
3342687915e9Sdougm 		handle = sa_find_group_handle((sa_group_t)object);
33433b61b335Sdougm 		/* It is legitimate to not find a handle */
3344687915e9Sdougm 		proto = sa_get_optionset_attr(object, "type");
3345687915e9Sdougm 		if ((ret = sa_valid_property(handle, object, proto,
3346687915e9Sdougm 		    property)) == SA_OK) {
334757b448deSdougm 			property = (sa_property_t)xmlAddChild(
334857b448deSdougm 			    (xmlNodePtr)object, (xmlNodePtr)property);
334957b448deSdougm 		} else {
335057b448deSdougm 			if (proto != NULL)
335157b448deSdougm 				sa_free_attr_string(proto);
335257b448deSdougm 			return (ret);
335357b448deSdougm 		}
3354687915e9Sdougm 		if (proto != NULL)
3355687915e9Sdougm 			sa_free_attr_string(proto);
33566185db85Sdougm 	}
33576185db85Sdougm 
33586185db85Sdougm 
33596185db85Sdougm 	parent = sa_get_parent_group(object);
3360da6c28aaSamw 	if (!sa_is_persistent(parent))
336157b448deSdougm 		return (ret);
33626185db85Sdougm 
3363da6c28aaSamw 	if (sa_is_resource(parent)) {
3364da6c28aaSamw 		/*
3365da6c28aaSamw 		 * Resources are children of share.  Need to go up two
3366da6c28aaSamw 		 * levels to find the group but the parent needs to be
3367da6c28aaSamw 		 * the share at this point in order to get the "id".
3368da6c28aaSamw 		 */
3369da6c28aaSamw 		parent = sa_get_parent_group(parent);
337057b448deSdougm 		group = sa_get_parent_group(parent);
3371da6c28aaSamw 	} else if (sa_is_share(parent)) {
3372da6c28aaSamw 		group = sa_get_parent_group(parent);
3373da6c28aaSamw 	} else {
337457b448deSdougm 		group = parent;
3375da6c28aaSamw 	}
3376549ec3ffSdougm 
337757b448deSdougm 	if (property == NULL) {
337857b448deSdougm 		ret = SA_NO_MEMORY;
337957b448deSdougm 	} else {
338057b448deSdougm 		char oname[SA_STRSIZE];
338157b448deSdougm 
338257b448deSdougm 		if (!is_zfs_group(group)) {
338357b448deSdougm 			char *id = NULL;
338457b448deSdougm 			sa_handle_impl_t impl_handle;
338557b448deSdougm 			scfutilhandle_t  *scf_handle;
338657b448deSdougm 
338757b448deSdougm 			impl_handle = (sa_handle_impl_t)sa_find_group_handle(
338857b448deSdougm 			    group);
338957b448deSdougm 			if (impl_handle == NULL ||
339057b448deSdougm 			    impl_handle->scfhandle == NULL)
339157b448deSdougm 				ret = SA_SYSTEM_ERR;
339257b448deSdougm 			if (ret == SA_OK) {
339357b448deSdougm 				scf_handle = impl_handle->scfhandle;
339457b448deSdougm 				if (sa_is_share((sa_group_t)parent)) {
339557b448deSdougm 					id = sa_get_share_attr(
339657b448deSdougm 					    (sa_share_t)parent, "id");
339757b448deSdougm 				}
339857b448deSdougm 				if (scf_handle->trans == NULL) {
339957b448deSdougm 					if (is_nodetype(object, "optionset")) {
340057b448deSdougm 						(void) sa_optionset_name(
340157b448deSdougm 						    (sa_optionset_t)object,
340257b448deSdougm 						    oname, sizeof (oname), id);
340357b448deSdougm 					} else {
340457b448deSdougm 						(void) sa_security_name(
340557b448deSdougm 						    (sa_optionset_t)object,
340657b448deSdougm 						    oname, sizeof (oname), id);
340757b448deSdougm 					}
340857b448deSdougm 					ret = sa_start_transaction(scf_handle,
340957b448deSdougm 					    oname);
341057b448deSdougm 				}
341157b448deSdougm 				if (ret == SA_OK) {
341257b448deSdougm 					char *name;
341357b448deSdougm 					char *value;
341457b448deSdougm 					name = sa_get_property_attr(property,
341557b448deSdougm 					    "type");
341657b448deSdougm 					value = sa_get_property_attr(property,
341757b448deSdougm 					    "value");
341857b448deSdougm 					if (name != NULL && value != NULL) {
341957b448deSdougm 						if (scf_handle->scf_state ==
342057b448deSdougm 						    SCH_STATE_INIT) {
342157b448deSdougm 							ret = sa_set_property(
342257b448deSdougm 							    scf_handle, name,
342357b448deSdougm 							    value);
342457b448deSdougm 						}
342557b448deSdougm 					} else {
342657b448deSdougm 						ret = SA_CONFIG_ERR;
342757b448deSdougm 					}
342857b448deSdougm 					if (name != NULL)
342957b448deSdougm 						sa_free_attr_string(
343057b448deSdougm 						    name);
343157b448deSdougm 					if (value != NULL)
343257b448deSdougm 						sa_free_attr_string(value);
343357b448deSdougm 				}
343457b448deSdougm 				if (id != NULL)
343557b448deSdougm 					sa_free_attr_string(id);
343657b448deSdougm 			}
343757b448deSdougm 		} else {
343857b448deSdougm 			/*
343957b448deSdougm 			 * ZFS is a special case. We do want
344057b448deSdougm 			 * to allow editing property/security
344157b448deSdougm 			 * lists since we can have a better
344257b448deSdougm 			 * syntax and we also want to keep
344357b448deSdougm 			 * things consistent when possible.
344457b448deSdougm 			 *
344557b448deSdougm 			 * Right now, we defer until the
344657b448deSdougm 			 * sa_commit_properties so we can get
344757b448deSdougm 			 * them all at once. We do need to
344857b448deSdougm 			 * mark the share as "changed"
344957b448deSdougm 			 */
345057b448deSdougm 			zfs_set_update((sa_share_t)parent);
34516185db85Sdougm 		}
34526185db85Sdougm 	}
34536185db85Sdougm 	return (ret);
34546185db85Sdougm }
34556185db85Sdougm 
34566185db85Sdougm /*
34576185db85Sdougm  * sa_remove_property(property)
34586185db85Sdougm  *
34596185db85Sdougm  * Remove the specied property from its containing object. Update the
34606185db85Sdougm  * repository as appropriate.
34616185db85Sdougm  */
34626185db85Sdougm 
34636185db85Sdougm int
sa_remove_property(sa_property_t property)34646185db85Sdougm sa_remove_property(sa_property_t property)
34656185db85Sdougm {
34666185db85Sdougm 	int ret = SA_OK;
34676185db85Sdougm 
34686185db85Sdougm 	if (property != NULL) {
34696185db85Sdougm 		sa_optionset_t optionset;
34706185db85Sdougm 		sa_group_t group;
34716185db85Sdougm 		optionset = sa_get_property_parent(property);
34726185db85Sdougm 		if (optionset != NULL) {
347357b448deSdougm 			group = sa_get_optionset_parent(optionset);
347457b448deSdougm 			if (group != NULL) {
347557b448deSdougm 				ret = sa_set_prop_by_prop(optionset, group,
347657b448deSdougm 				    property, SA_PROP_OP_REMOVE);
347757b448deSdougm 			}
34786185db85Sdougm 		}
34796185db85Sdougm 		xmlUnlinkNode((xmlNodePtr)property);
34806185db85Sdougm 		xmlFreeNode((xmlNodePtr)property);
34816185db85Sdougm 	} else {
348257b448deSdougm 		ret = SA_NO_SUCH_PROP;
34836185db85Sdougm 	}
34846185db85Sdougm 	return (ret);
34856185db85Sdougm }
34866185db85Sdougm 
34876185db85Sdougm /*
34886185db85Sdougm  * sa_update_property(property, value)
34896185db85Sdougm  *
34906185db85Sdougm  * Update the specified property to the new value.  If value is NULL,
34916185db85Sdougm  * we currently treat this as a remove.
34926185db85Sdougm  */
34936185db85Sdougm 
34946185db85Sdougm int
sa_update_property(sa_property_t property,char * value)34956185db85Sdougm sa_update_property(sa_property_t property, char *value)
34966185db85Sdougm {
34976185db85Sdougm 	int ret = SA_OK;
34986185db85Sdougm 	if (value == NULL) {
34996185db85Sdougm 		return (sa_remove_property(property));
35006185db85Sdougm 	} else {
35016185db85Sdougm 		sa_optionset_t optionset;
35026185db85Sdougm 		sa_group_t group;
35036185db85Sdougm 		set_node_attr((void *)property, "value", value);
35046185db85Sdougm 		optionset = sa_get_property_parent(property);
35056185db85Sdougm 		if (optionset != NULL) {
350657b448deSdougm 			group = sa_get_optionset_parent(optionset);
350757b448deSdougm 			if (group != NULL) {
350857b448deSdougm 				ret = sa_set_prop_by_prop(optionset, group,
350957b448deSdougm 				    property, SA_PROP_OP_UPDATE);
351057b448deSdougm 			}
35116185db85Sdougm 		} else {
351257b448deSdougm 			ret = SA_NO_SUCH_PROP;
35136185db85Sdougm 		}
35146185db85Sdougm 	}
35156185db85Sdougm 	return (ret);
35166185db85Sdougm }
35176185db85Sdougm 
35184bff34e3Sthurlow /*
35194bff34e3Sthurlow  * sa_get_protocol_section(propset, prop)
35204bff34e3Sthurlow  *
35214bff34e3Sthurlow  * Get the specified protocol specific section. These are global to
35224bff34e3Sthurlow  * the protocol and not specific to a group or share.
35234bff34e3Sthurlow  */
35244bff34e3Sthurlow 
35254bff34e3Sthurlow sa_protocol_properties_t
sa_get_protocol_section(sa_protocol_properties_t propset,char * section)35264bff34e3Sthurlow sa_get_protocol_section(sa_protocol_properties_t propset, char *section)
35274bff34e3Sthurlow {
35284bff34e3Sthurlow 	xmlNodePtr node = (xmlNodePtr)propset;
35294bff34e3Sthurlow 	xmlChar *value = NULL;
35304bff34e3Sthurlow 	char *proto;
35314bff34e3Sthurlow 
35324bff34e3Sthurlow 	proto = sa_get_optionset_attr(propset, "type");
35339c9af259SGordon Ross 	if ((sa_proto_get_featureset(proto) & SA_FEATURE_HAS_SECTIONS) == 0) {
35349c9af259SGordon Ross 		if (proto != NULL)
35359c9af259SGordon Ross 			sa_free_attr_string(proto);
35364bff34e3Sthurlow 		return (propset);
35379c9af259SGordon Ross 	}
35384bff34e3Sthurlow 
35394bff34e3Sthurlow 	for (node = node->children; node != NULL;
35404bff34e3Sthurlow 	    node = node->next) {
35414bff34e3Sthurlow 		if (xmlStrcmp(node->name, (xmlChar *)"section") == 0) {
35424bff34e3Sthurlow 			if (section == NULL)
35434bff34e3Sthurlow 				break;
35444bff34e3Sthurlow 			value = xmlGetProp(node, (xmlChar *)"name");
35454bff34e3Sthurlow 			if (value != NULL &&
35464bff34e3Sthurlow 			    xmlStrcasecmp(value, (xmlChar *)section) == 0) {
35474bff34e3Sthurlow 				break;
35484bff34e3Sthurlow 			}
35494bff34e3Sthurlow 			if (value != NULL) {
35504bff34e3Sthurlow 				xmlFree(value);
35514bff34e3Sthurlow 				value = NULL;
35524bff34e3Sthurlow 			}
35534bff34e3Sthurlow 		}
35544bff34e3Sthurlow 	}
35554bff34e3Sthurlow 	if (value != NULL)
35564bff34e3Sthurlow 		xmlFree(value);
35579c9af259SGordon Ross 	if (proto != NULL)
35589c9af259SGordon Ross 		sa_free_attr_string(proto);
35594bff34e3Sthurlow 	if (node != NULL && xmlStrcmp(node->name, (xmlChar *)"section") != 0) {
35604bff34e3Sthurlow 		/*
35614bff34e3Sthurlow 		 * avoid a non option node -- it is possible to be a
35624bff34e3Sthurlow 		 * text node
35634bff34e3Sthurlow 		 */
35644bff34e3Sthurlow 		node = NULL;
35654bff34e3Sthurlow 	}
35664bff34e3Sthurlow 	return ((sa_protocol_properties_t)node);
35674bff34e3Sthurlow }
35684bff34e3Sthurlow 
35694bff34e3Sthurlow /*
35704bff34e3Sthurlow  * sa_get_next_protocol_section(prop, find)
35714bff34e3Sthurlow  *
35724bff34e3Sthurlow  * Get the next protocol specific section in the list.
35734bff34e3Sthurlow  */
35744bff34e3Sthurlow 
35754bff34e3Sthurlow sa_property_t
sa_get_next_protocol_section(sa_property_t prop,char * find)35764bff34e3Sthurlow sa_get_next_protocol_section(sa_property_t prop, char *find)
35774bff34e3Sthurlow {
35784bff34e3Sthurlow 	xmlNodePtr node;
35794bff34e3Sthurlow 	xmlChar *value = NULL;
35804bff34e3Sthurlow 	char *proto;
35814bff34e3Sthurlow 
35824bff34e3Sthurlow 	proto = sa_get_optionset_attr(prop, "type");
35839c9af259SGordon Ross 	if ((sa_proto_get_featureset(proto) & SA_FEATURE_HAS_SECTIONS) == 0) {
35849c9af259SGordon Ross 		if (proto != NULL)
35859c9af259SGordon Ross 			sa_free_attr_string(proto);
35864bff34e3Sthurlow 		return ((sa_property_t)NULL);
35879c9af259SGordon Ross 	}
35884bff34e3Sthurlow 
35894bff34e3Sthurlow 	for (node = ((xmlNodePtr)prop)->next; node != NULL;
35904bff34e3Sthurlow 	    node = node->next) {
35914bff34e3Sthurlow 		if (xmlStrcmp(node->name, (xmlChar *)"section") == 0) {
35924bff34e3Sthurlow 			if (find == NULL)
35934bff34e3Sthurlow 				break;
35944bff34e3Sthurlow 			value = xmlGetProp(node, (xmlChar *)"name");
35954bff34e3Sthurlow 			if (value != NULL &&
35964bff34e3Sthurlow 			    xmlStrcasecmp(value, (xmlChar *)find) == 0) {
35974bff34e3Sthurlow 				break;
35984bff34e3Sthurlow 			}
35994bff34e3Sthurlow 			if (value != NULL) {
36004bff34e3Sthurlow 				xmlFree(value);
36014bff34e3Sthurlow 				value = NULL;
36024bff34e3Sthurlow 			}
36034bff34e3Sthurlow 
36044bff34e3Sthurlow 		}
36054bff34e3Sthurlow 	}
36064bff34e3Sthurlow 	if (value != NULL)
36074bff34e3Sthurlow 		xmlFree(value);
36089c9af259SGordon Ross 	if (proto != NULL)
36099c9af259SGordon Ross 		sa_free_attr_string(proto);
36104bff34e3Sthurlow 	return ((sa_property_t)node);
36114bff34e3Sthurlow }
36124bff34e3Sthurlow 
36136185db85Sdougm /*
36146185db85Sdougm  * sa_get_protocol_property(propset, prop)
36156185db85Sdougm  *
36166185db85Sdougm  * Get the specified protocol specific property. These are global to
36176185db85Sdougm  * the protocol and not specific to a group or share.
36186185db85Sdougm  */
36196185db85Sdougm 
36206185db85Sdougm sa_property_t
sa_get_protocol_property(sa_protocol_properties_t propset,char * prop)36216185db85Sdougm sa_get_protocol_property(sa_protocol_properties_t propset, char *prop)
36226185db85Sdougm {
36236185db85Sdougm 	xmlNodePtr node = (xmlNodePtr)propset;
36246185db85Sdougm 	xmlChar *value = NULL;
36256185db85Sdougm 
36264bff34e3Sthurlow 	if (propset == NULL)
36274bff34e3Sthurlow 		return (NULL);
36284bff34e3Sthurlow 
36296185db85Sdougm 	for (node = node->children; node != NULL;
363057b448deSdougm 	    node = node->next) {
363157b448deSdougm 		if (xmlStrcmp(node->name, (xmlChar *)"option") == 0) {
363257b448deSdougm 			if (prop == NULL)
363357b448deSdougm 				break;
363457b448deSdougm 			value = xmlGetProp(node, (xmlChar *)"type");
363557b448deSdougm 			if (value != NULL &&
363657b448deSdougm 			    xmlStrcasecmp(value, (xmlChar *)prop) == 0) {
363757b448deSdougm 				break;
363857b448deSdougm 			}
363957b448deSdougm 			if (value != NULL) {
364057b448deSdougm 				xmlFree(value);
364157b448deSdougm 				value = NULL;
364257b448deSdougm 			}
36436185db85Sdougm 		}
36446185db85Sdougm 	}
36456185db85Sdougm 	if (value != NULL)
36466185db85Sdougm 		xmlFree(value);
36476185db85Sdougm 	if (node != NULL && xmlStrcmp(node->name, (xmlChar *)"option") != 0) {
364857b448deSdougm 		/*
364957b448deSdougm 		 * avoid a non option node -- it is possible to be a
365057b448deSdougm 		 * text node
365157b448deSdougm 		 */
365257b448deSdougm 		node = NULL;
36536185db85Sdougm 	}
36546185db85Sdougm 	return ((sa_property_t)node);
36556185db85Sdougm }
36566185db85Sdougm 
36576185db85Sdougm /*
36586185db85Sdougm  * sa_get_next_protocol_property(prop)
36596185db85Sdougm  *
36606185db85Sdougm  * Get the next protocol specific property in the list.
36616185db85Sdougm  */
36626185db85Sdougm 
36636185db85Sdougm sa_property_t
sa_get_next_protocol_property(sa_property_t prop,char * find)36644bff34e3Sthurlow sa_get_next_protocol_property(sa_property_t prop, char *find)
36656185db85Sdougm {
36666185db85Sdougm 	xmlNodePtr node;
36674bff34e3Sthurlow 	xmlChar *value = NULL;
36686185db85Sdougm 
36696185db85Sdougm 	for (node = ((xmlNodePtr)prop)->next; node != NULL;
367057b448deSdougm 	    node = node->next) {
36716185db85Sdougm 		if (xmlStrcmp(node->name, (xmlChar *)"option") == 0) {
36724bff34e3Sthurlow 			if (find == NULL)
36734bff34e3Sthurlow 				break;
36744bff34e3Sthurlow 			value = xmlGetProp(node, (xmlChar *)"type");
36754bff34e3Sthurlow 			if (value != NULL &&
36764bff34e3Sthurlow 			    xmlStrcasecmp(value, (xmlChar *)find) == 0) {
36774bff34e3Sthurlow 				break;
36784bff34e3Sthurlow 			}
36794bff34e3Sthurlow 			if (value != NULL) {
36804bff34e3Sthurlow 				xmlFree(value);
36814bff34e3Sthurlow 				value = NULL;
36824bff34e3Sthurlow 			}
36834bff34e3Sthurlow 
36846185db85Sdougm 		}
36856185db85Sdougm 	}
36864bff34e3Sthurlow 	if (value != NULL)
36874bff34e3Sthurlow 		xmlFree(value);
36886185db85Sdougm 	return ((sa_property_t)node);
36896185db85Sdougm }
36906185db85Sdougm 
36916185db85Sdougm /*
36926185db85Sdougm  * sa_set_protocol_property(prop, value)
36936185db85Sdougm  *
36946185db85Sdougm  * Set the specified property to have the new value.  The protocol
36956185db85Sdougm  * specific plugin will then be called to update the property.
36966185db85Sdougm  */
36976185db85Sdougm 
36986185db85Sdougm int
sa_set_protocol_property(sa_property_t prop,char * section,char * value)36994bff34e3Sthurlow sa_set_protocol_property(sa_property_t prop, char *section, char *value)
37006185db85Sdougm {
37016185db85Sdougm 	sa_protocol_properties_t propset;
37026185db85Sdougm 	char *proto;
37036185db85Sdougm 	int ret = SA_INVALID_PROTOCOL;
37046185db85Sdougm 
37056185db85Sdougm 	propset = ((xmlNodePtr)prop)->parent;
37066185db85Sdougm 	if (propset != NULL) {
370757b448deSdougm 		proto = sa_get_optionset_attr(propset, "type");
370857b448deSdougm 		if (proto != NULL) {
37094bff34e3Sthurlow 			if (section != NULL)
37104bff34e3Sthurlow 				set_node_attr((xmlNodePtr)prop, "section",
37114bff34e3Sthurlow 				    section);
371257b448deSdougm 			set_node_attr((xmlNodePtr)prop, "value", value);
371357b448deSdougm 			ret = sa_proto_set_property(proto, prop);
371457b448deSdougm 			sa_free_attr_string(proto);
371557b448deSdougm 		}
37166185db85Sdougm 	}
37176185db85Sdougm 	return (ret);
37186185db85Sdougm }
37196185db85Sdougm 
37206185db85Sdougm /*
37216185db85Sdougm  * sa_add_protocol_property(propset, prop)
37226185db85Sdougm  *
3723da6c28aaSamw  * Add a new property to the protocol specific property set.
37246185db85Sdougm  */
37256185db85Sdougm 
37266185db85Sdougm int
sa_add_protocol_property(sa_protocol_properties_t propset,sa_property_t prop)37276185db85Sdougm sa_add_protocol_property(sa_protocol_properties_t propset, sa_property_t prop)
37286185db85Sdougm {
37296185db85Sdougm 	xmlNodePtr node;
37306185db85Sdougm 
37316185db85Sdougm 	/* should check for legitimacy */
37326185db85Sdougm 	node = xmlAddChild((xmlNodePtr)propset, (xmlNodePtr)prop);
37336185db85Sdougm 	if (node != NULL)
373457b448deSdougm 		return (SA_OK);
37356185db85Sdougm 	return (SA_NO_MEMORY);
37366185db85Sdougm }
37376185db85Sdougm 
37386185db85Sdougm /*
37396185db85Sdougm  * sa_create_protocol_properties(proto)
37406185db85Sdougm  *
3741da6c28aaSamw  * Create a protocol specific property set.
37426185db85Sdougm  */
37436185db85Sdougm 
37446185db85Sdougm sa_protocol_properties_t
sa_create_protocol_properties(char * proto)37456185db85Sdougm sa_create_protocol_properties(char *proto)
37466185db85Sdougm {
37476185db85Sdougm 	xmlNodePtr node;
374857b448deSdougm 
37496185db85Sdougm 	node = xmlNewNode(NULL, (xmlChar *)"propertyset");
375057b448deSdougm 	if (node != NULL)
37514bff34e3Sthurlow 		(void) xmlSetProp(node, (xmlChar *)"type", (xmlChar *)proto);
37526185db85Sdougm 	return (node);
37536185db85Sdougm }
3754da6c28aaSamw 
3755da6c28aaSamw /*
3756da6c28aaSamw  * sa_get_share_resource(share, resource)
3757da6c28aaSamw  *
3758da6c28aaSamw  * Get the named resource from the share, if it exists. If resource is
3759da6c28aaSamw  * NULL, get the first resource.
3760da6c28aaSamw  */
3761da6c28aaSamw 
3762da6c28aaSamw sa_resource_t
sa_get_share_resource(sa_share_t share,char * resource)3763da6c28aaSamw sa_get_share_resource(sa_share_t share, char *resource)
3764da6c28aaSamw {
3765da6c28aaSamw 	xmlNodePtr node = NULL;
3766da6c28aaSamw 	xmlChar *name;
3767da6c28aaSamw 
3768da6c28aaSamw 	if (share != NULL) {
3769da6c28aaSamw 		for (node = ((xmlNodePtr)share)->children; node != NULL;
3770da6c28aaSamw 		    node = node->next) {
3771da6c28aaSamw 			if (xmlStrcmp(node->name, (xmlChar *)"resource") == 0) {
3772da6c28aaSamw 				if (resource == NULL) {
3773da6c28aaSamw 					/*
3774da6c28aaSamw 					 * We are looking for the first
3775da6c28aaSamw 					 * resource node and not a names
3776da6c28aaSamw 					 * resource.
3777da6c28aaSamw 					 */
3778da6c28aaSamw 					break;
3779da6c28aaSamw 				} else {
3780da6c28aaSamw 					/* is it the correct share? */
3781da6c28aaSamw 					name = xmlGetProp(node,
3782da6c28aaSamw 					    (xmlChar *)"name");
3783da6c28aaSamw 					if (name != NULL &&
3784da6c28aaSamw 					    xmlStrcasecmp(name,
3785da6c28aaSamw 					    (xmlChar *)resource) == 0) {
3786da6c28aaSamw 						xmlFree(name);
3787da6c28aaSamw 						break;
3788da6c28aaSamw 					}
3789da6c28aaSamw 					xmlFree(name);
3790da6c28aaSamw 				}
3791da6c28aaSamw 			}
3792da6c28aaSamw 		}
3793da6c28aaSamw 	}
3794da6c28aaSamw 	return ((sa_resource_t)node);
3795da6c28aaSamw }
3796da6c28aaSamw 
3797da6c28aaSamw /*
3798da6c28aaSamw  * sa_get_next_resource(resource)
3799da6c28aaSamw  *	Return the next share following the specified share
3800da6c28aaSamw  *	from the internal list of shares. Returns NULL if there
3801da6c28aaSamw  *	are no more shares.  The list is relative to the same
3802da6c28aaSamw  *	group.
3803da6c28aaSamw  */
3804da6c28aaSamw sa_share_t
sa_get_next_resource(sa_resource_t resource)3805da6c28aaSamw sa_get_next_resource(sa_resource_t resource)
3806da6c28aaSamw {
3807da6c28aaSamw 	xmlNodePtr node = NULL;
3808da6c28aaSamw 
3809da6c28aaSamw 	if (resource != NULL) {
3810da6c28aaSamw 		for (node = ((xmlNodePtr)resource)->next; node != NULL;
3811da6c28aaSamw 		    node = node->next) {
3812da6c28aaSamw 			if (xmlStrcmp(node->name, (xmlChar *)"resource") == 0)
3813da6c28aaSamw 				break;
3814da6c28aaSamw 		}
3815da6c28aaSamw 	}
3816da6c28aaSamw 	return ((sa_share_t)node);
3817da6c28aaSamw }
3818da6c28aaSamw 
3819da6c28aaSamw /*
3820da6c28aaSamw  * _sa_get_next_resource_index(share)
3821da6c28aaSamw  *
3822da6c28aaSamw  * get the next resource index number (one greater then current largest)
3823da6c28aaSamw  */
3824da6c28aaSamw 
3825da6c28aaSamw static int
_sa_get_next_resource_index(sa_share_t share)3826da6c28aaSamw _sa_get_next_resource_index(sa_share_t share)
3827da6c28aaSamw {
3828da6c28aaSamw 	sa_resource_t resource;
3829da6c28aaSamw 	int index = 0;
3830da6c28aaSamw 	char *id;
3831da6c28aaSamw 
3832da6c28aaSamw 	for (resource = sa_get_share_resource(share, NULL);
3833da6c28aaSamw 	    resource != NULL;
3834da6c28aaSamw 	    resource = sa_get_next_resource(resource)) {
3835da6c28aaSamw 		id = get_node_attr((void *)resource, "id");
3836da6c28aaSamw 		if (id != NULL) {
3837da6c28aaSamw 			int val;
3838da6c28aaSamw 			val = atoi(id);
3839da6c28aaSamw 			if (val > index)
3840da6c28aaSamw 				index = val;
3841da6c28aaSamw 			sa_free_attr_string(id);
3842da6c28aaSamw 		}
3843da6c28aaSamw 	}
3844da6c28aaSamw 	return (index + 1);
3845da6c28aaSamw }
3846da6c28aaSamw 
3847da6c28aaSamw 
3848da6c28aaSamw /*
3849da6c28aaSamw  * sa_add_resource(share, resource, persist, &err)
3850da6c28aaSamw  *
3851da6c28aaSamw  * Adds a new resource name associated with share. The resource name
3852da6c28aaSamw  * must be unique in the system and will be case insensitive (eventually).
3853da6c28aaSamw  */
3854da6c28aaSamw 
3855da6c28aaSamw sa_resource_t
sa_add_resource(sa_share_t share,char * resource,int persist,int * error)3856da6c28aaSamw sa_add_resource(sa_share_t share, char *resource, int persist, int *error)
3857da6c28aaSamw {
3858*2bc647a2SToomas Soome 	xmlNodePtr node = NULL;
3859da6c28aaSamw 	int err = SA_OK;
3860da6c28aaSamw 	sa_resource_t res;
3861da6c28aaSamw 	sa_group_t group;
3862da6c28aaSamw 	sa_handle_t handle;
3863da6c28aaSamw 	char istring[8]; /* just big enough for an integer value */
3864da6c28aaSamw 	int index;
3865da6c28aaSamw 
3866da6c28aaSamw 	group = sa_get_parent_group(share);
3867da6c28aaSamw 	handle = sa_find_group_handle(group);
3868da6c28aaSamw 	res = sa_find_resource(handle, resource);
3869da6c28aaSamw 	if (res != NULL) {
3870da6c28aaSamw 		err = SA_DUPLICATE_NAME;
3871da6c28aaSamw 		res = NULL;
3872da6c28aaSamw 	} else {
3873da6c28aaSamw 		node = xmlNewChild((xmlNodePtr)share, NULL,
3874da6c28aaSamw 		    (xmlChar *)"resource", NULL);
3875da6c28aaSamw 		if (node != NULL) {
38764bff34e3Sthurlow 			(void) xmlSetProp(node, (xmlChar *)"name",
3877da6c28aaSamw 			    (xmlChar *)resource);
38784bff34e3Sthurlow 			(void) xmlSetProp(node, (xmlChar *)"type", persist ?
3879da6c28aaSamw 			    (xmlChar *)"persist" : (xmlChar *)"transient");
3880da6c28aaSamw 			if (persist != SA_SHARE_TRANSIENT) {
3881da6c28aaSamw 				index = _sa_get_next_resource_index(share);
3882da6c28aaSamw 				(void) snprintf(istring, sizeof (istring), "%d",
3883da6c28aaSamw 				    index);
38844bff34e3Sthurlow 				(void) xmlSetProp(node, (xmlChar *)"id",
3885da6c28aaSamw 				    (xmlChar *)istring);
38861f713840SDoug McCallum 
38871f713840SDoug McCallum 				if (!sa_is_persistent((sa_group_t)share))
38881f713840SDoug McCallum 					goto done;
38891f713840SDoug McCallum 
38901f713840SDoug McCallum 				if (!sa_group_is_zfs(group)) {
3891da6c28aaSamw 					/* ZFS doesn't use resource names */
3892da6c28aaSamw 					sa_handle_impl_t ihandle;
38931f713840SDoug McCallum 
3894da6c28aaSamw 					ihandle = (sa_handle_impl_t)
3895da6c28aaSamw 					    sa_find_group_handle(
3896da6c28aaSamw 					    group);
3897da6c28aaSamw 					if (ihandle != NULL)
3898da6c28aaSamw 						err = sa_commit_share(
3899da6c28aaSamw 						    ihandle->scfhandle, group,
3900da6c28aaSamw 						    share);
3901da6c28aaSamw 					else
3902da6c28aaSamw 						err = SA_SYSTEM_ERR;
39031f713840SDoug McCallum 				} else {
39041f713840SDoug McCallum 					err = sa_zfs_update((sa_share_t)group);
3905da6c28aaSamw 				}
3906da6c28aaSamw 			}
3907da6c28aaSamw 		}
3908da6c28aaSamw 	}
39091f713840SDoug McCallum done:
3910da6c28aaSamw 	if (error != NULL)
3911da6c28aaSamw 		*error = err;
3912da6c28aaSamw 	return ((sa_resource_t)node);
3913da6c28aaSamw }
3914da6c28aaSamw 
3915da6c28aaSamw /*
3916da6c28aaSamw  * sa_remove_resource(resource)
3917da6c28aaSamw  *
3918da6c28aaSamw  * Remove the resource name from the share (and the system)
3919da6c28aaSamw  */
3920da6c28aaSamw 
3921da6c28aaSamw int
sa_remove_resource(sa_resource_t resource)3922da6c28aaSamw sa_remove_resource(sa_resource_t resource)
3923da6c28aaSamw {
3924da6c28aaSamw 	sa_share_t share;
3925da6c28aaSamw 	sa_group_t group;
3926da6c28aaSamw 	char *type;
3927da6c28aaSamw 	int ret = SA_OK;
39281f713840SDoug McCallum 	boolean_t transient = B_FALSE;
392955bf511dSas 	sa_optionset_t opt;
3930da6c28aaSamw 
3931da6c28aaSamw 	share = sa_get_resource_parent(resource);
3932da6c28aaSamw 	type = sa_get_share_attr(share, "type");
3933da6c28aaSamw 	group = sa_get_parent_group(share);
3934da6c28aaSamw 
3935da6c28aaSamw 
3936da6c28aaSamw 	if (type != NULL) {
3937da6c28aaSamw 		if (strcmp(type, "persist") != 0)
39381f713840SDoug McCallum 			transient = B_TRUE;
3939da6c28aaSamw 		sa_free_attr_string(type);
3940da6c28aaSamw 	}
3941da6c28aaSamw 
394255bf511dSas 	/* Disable the resource for all protocols. */
394355bf511dSas 	(void) sa_disable_resource(resource, NULL);
394455bf511dSas 
394555bf511dSas 	/* Remove any optionsets from the resource. */
394655bf511dSas 	for (opt = sa_get_optionset(resource, NULL);
394755bf511dSas 	    opt != NULL;
394855bf511dSas 	    opt = sa_get_next_optionset(opt))
394955bf511dSas 		(void) sa_destroy_optionset(opt);
395055bf511dSas 
3951da6c28aaSamw 	/* Remove from the share */
3952da6c28aaSamw 	xmlUnlinkNode((xmlNode *)resource);
3953da6c28aaSamw 	xmlFreeNode((xmlNode *)resource);
3954da6c28aaSamw 
3955da6c28aaSamw 	/* only do SMF action if permanent and not ZFS */
39561f713840SDoug McCallum 	if (transient)
39571f713840SDoug McCallum 		return (ret);
39581f713840SDoug McCallum 
39591f713840SDoug McCallum 	if (!sa_group_is_zfs(group)) {
3960da6c28aaSamw 		sa_handle_impl_t ihandle;
3961da6c28aaSamw 		ihandle = (sa_handle_impl_t)sa_find_group_handle(group);
3962da6c28aaSamw 		if (ihandle != NULL)
3963da6c28aaSamw 			ret = sa_commit_share(ihandle->scfhandle, group, share);
3964da6c28aaSamw 		else
3965da6c28aaSamw 			ret = SA_SYSTEM_ERR;
39661f713840SDoug McCallum 	} else {
39671f713840SDoug McCallum 		ret = sa_zfs_update((sa_share_t)group);
3968da6c28aaSamw 	}
3969743a77edSAlan Wright 
3970da6c28aaSamw 	return (ret);
3971da6c28aaSamw }
3972da6c28aaSamw 
3973da6c28aaSamw /*
3974743a77edSAlan Wright  * proto_rename_resource(handle, group, resource, newname)
3975da6c28aaSamw  *
3976da6c28aaSamw  * Helper function for sa_rename_resource that notifies the protocol
3977da6c28aaSamw  * of a resource name change prior to a config repository update.
3978da6c28aaSamw  */
3979da6c28aaSamw static int
proto_rename_resource(sa_handle_t handle,sa_group_t group,sa_resource_t resource,char * newname)3980da6c28aaSamw proto_rename_resource(sa_handle_t handle, sa_group_t group,
3981da6c28aaSamw     sa_resource_t resource, char *newname)
3982da6c28aaSamw {
3983da6c28aaSamw 	sa_optionset_t optionset;
3984da6c28aaSamw 	int ret = SA_OK;
3985da6c28aaSamw 	int err;
3986da6c28aaSamw 
3987da6c28aaSamw 	for (optionset = sa_get_optionset(group, NULL);
3988da6c28aaSamw 	    optionset != NULL;
3989da6c28aaSamw 	    optionset = sa_get_next_optionset(optionset)) {
3990da6c28aaSamw 		char *type;
3991da6c28aaSamw 		type = sa_get_optionset_attr(optionset, "type");
3992da6c28aaSamw 		if (type != NULL) {
3993da6c28aaSamw 			err = sa_proto_rename_resource(handle, type, resource,
3994da6c28aaSamw 			    newname);
3995da6c28aaSamw 			if (err != SA_OK)
3996da6c28aaSamw 				ret = err;
3997da6c28aaSamw 			sa_free_attr_string(type);
3998da6c28aaSamw 		}
3999da6c28aaSamw 	}
4000da6c28aaSamw 	return (ret);
4001da6c28aaSamw }
4002da6c28aaSamw 
4003da6c28aaSamw /*
4004da6c28aaSamw  * sa_rename_resource(resource, newname)
4005da6c28aaSamw  *
4006da6c28aaSamw  * Rename the resource to the new name, if it is unique.
4007da6c28aaSamw  */
4008da6c28aaSamw 
4009da6c28aaSamw int
sa_rename_resource(sa_resource_t resource,char * newname)4010da6c28aaSamw sa_rename_resource(sa_resource_t resource, char *newname)
4011da6c28aaSamw {
4012da6c28aaSamw 	sa_share_t share;
4013da6c28aaSamw 	sa_group_t group = NULL;
4014da6c28aaSamw 	sa_resource_t target;
4015da6c28aaSamw 	int ret = SA_CONFIG_ERR;
4016da6c28aaSamw 	sa_handle_t handle = NULL;
4017da6c28aaSamw 
4018da6c28aaSamw 	share = sa_get_resource_parent(resource);
4019da6c28aaSamw 	if (share == NULL)
4020da6c28aaSamw 		return (ret);
4021da6c28aaSamw 
4022da6c28aaSamw 	group = sa_get_parent_group(share);
4023da6c28aaSamw 	if (group == NULL)
4024da6c28aaSamw 		return (ret);
4025da6c28aaSamw 
4026da6c28aaSamw 	handle = (sa_handle_impl_t)sa_find_group_handle(group);
4027da6c28aaSamw 	if (handle == NULL)
4028da6c28aaSamw 		return (ret);
4029da6c28aaSamw 
4030da6c28aaSamw 	target = sa_find_resource(handle, newname);
4031da6c28aaSamw 	if (target != NULL) {
4032da6c28aaSamw 		ret = SA_DUPLICATE_NAME;
4033da6c28aaSamw 	} else {
4034da6c28aaSamw 		/*
4035da6c28aaSamw 		 * Everything appears to be valid at this
4036da6c28aaSamw 		 * point. Change the name of the active share and then
4037da6c28aaSamw 		 * update the share in the appropriate repository.
4038da6c28aaSamw 		 */
4039da6c28aaSamw 		ret = proto_rename_resource(handle, group, resource, newname);
4040da6c28aaSamw 		set_node_attr(resource, "name", newname);
40411f713840SDoug McCallum 
40421f713840SDoug McCallum 		if (!sa_is_persistent((sa_group_t)share))
40431f713840SDoug McCallum 			return (ret);
40441f713840SDoug McCallum 
40451f713840SDoug McCallum 		if (!sa_group_is_zfs(group)) {
4046da6c28aaSamw 			sa_handle_impl_t ihandle = (sa_handle_impl_t)handle;
4047da6c28aaSamw 			ret = sa_commit_share(ihandle->scfhandle, group,
4048da6c28aaSamw 			    share);
40491f713840SDoug McCallum 		} else {
40501f713840SDoug McCallum 			ret = sa_zfs_update((sa_share_t)group);
4051da6c28aaSamw 		}
4052da6c28aaSamw 	}
4053da6c28aaSamw 	return (ret);
4054da6c28aaSamw }
4055da6c28aaSamw 
4056da6c28aaSamw /*
4057da6c28aaSamw  * sa_get_resource_attr(resource, tag)
4058da6c28aaSamw  *
4059da6c28aaSamw  * Get the named attribute of the resource. "name" and "id" are
4060da6c28aaSamw  * currently defined.  NULL if tag not defined.
4061da6c28aaSamw  */
4062da6c28aaSamw 
4063da6c28aaSamw char *
sa_get_resource_attr(sa_resource_t resource,char * tag)4064da6c28aaSamw sa_get_resource_attr(sa_resource_t resource, char *tag)
4065da6c28aaSamw {
4066da6c28aaSamw 	return (get_node_attr((void *)resource, tag));
4067da6c28aaSamw }
4068da6c28aaSamw 
4069da6c28aaSamw /*
4070da6c28aaSamw  * sa_set_resource_attr(resource, tag, value)
4071da6c28aaSamw  *
4072da6c28aaSamw  * Get the named attribute of the resource. "name" and "id" are
4073da6c28aaSamw  * currently defined.  NULL if tag not defined. Currently we don't do
4074da6c28aaSamw  * much, but additional checking may be needed in the future.
4075da6c28aaSamw  */
4076da6c28aaSamw 
4077da6c28aaSamw int
sa_set_resource_attr(sa_resource_t resource,char * tag,char * value)4078da6c28aaSamw sa_set_resource_attr(sa_resource_t resource, char *tag, char *value)
4079da6c28aaSamw {
4080da6c28aaSamw 	set_node_attr((void *)resource, tag, value);
4081da6c28aaSamw 	return (SA_OK);
4082da6c28aaSamw }
4083da6c28aaSamw 
4084da6c28aaSamw /*
4085da6c28aaSamw  * sa_get_resource_parent(resource_t)
4086da6c28aaSamw  *
4087da6c28aaSamw  * Returns the share associated with the resource.
4088da6c28aaSamw  */
4089da6c28aaSamw 
4090da6c28aaSamw sa_share_t
sa_get_resource_parent(sa_resource_t resource)4091da6c28aaSamw sa_get_resource_parent(sa_resource_t resource)
4092da6c28aaSamw {
4093da6c28aaSamw 	sa_share_t share = NULL;
4094da6c28aaSamw 
4095da6c28aaSamw 	if (resource != NULL)
4096da6c28aaSamw 		share = (sa_share_t)((xmlNodePtr)resource)->parent;
4097da6c28aaSamw 	return (share);
4098da6c28aaSamw }
4099da6c28aaSamw 
4100da6c28aaSamw /*
4101da6c28aaSamw  * find_resource(group, name)
4102da6c28aaSamw  *
4103da6c28aaSamw  * Find the resource within the group.
4104da6c28aaSamw  */
4105da6c28aaSamw 
4106da6c28aaSamw static sa_resource_t
find_resource(sa_group_t group,char * resname)4107da6c28aaSamw find_resource(sa_group_t group, char *resname)
4108da6c28aaSamw {
4109da6c28aaSamw 	sa_share_t share;
4110da6c28aaSamw 	sa_resource_t resource = NULL;
4111da6c28aaSamw 	char *name;
4112da6c28aaSamw 
4113da6c28aaSamw 	/* Iterate over all the shares and resources in the group. */
4114da6c28aaSamw 	for (share = sa_get_share(group, NULL);
4115da6c28aaSamw 	    share != NULL && resource == NULL;
4116da6c28aaSamw 	    share = sa_get_next_share(share)) {
4117da6c28aaSamw 		for (resource = sa_get_share_resource(share, NULL);
4118da6c28aaSamw 		    resource != NULL;
4119da6c28aaSamw 		    resource = sa_get_next_resource(resource)) {
4120da6c28aaSamw 			name = sa_get_resource_attr(resource, "name");
4121da6c28aaSamw 			if (name != NULL && xmlStrcasecmp((xmlChar*)name,
4122da6c28aaSamw 			    (xmlChar*)resname) == 0) {
4123da6c28aaSamw 				sa_free_attr_string(name);
4124da6c28aaSamw 				break;
4125da6c28aaSamw 			}
4126da6c28aaSamw 			if (name != NULL) {
4127da6c28aaSamw 				sa_free_attr_string(name);
4128da6c28aaSamw 			}
4129da6c28aaSamw 		}
4130da6c28aaSamw 	}
4131da6c28aaSamw 	return (resource);
4132da6c28aaSamw }
4133da6c28aaSamw 
4134da6c28aaSamw /*
4135da6c28aaSamw  * sa_find_resource(name)
4136da6c28aaSamw  *
4137da6c28aaSamw  * Find the named resource in the system.
4138da6c28aaSamw  */
4139da6c28aaSamw 
4140da6c28aaSamw sa_resource_t
sa_find_resource(sa_handle_t handle,char * name)4141da6c28aaSamw sa_find_resource(sa_handle_t handle, char *name)
4142da6c28aaSamw {
4143da6c28aaSamw 	sa_group_t group;
4144da6c28aaSamw 	sa_group_t zgroup;
4145da6c28aaSamw 	sa_resource_t resource = NULL;
4146da6c28aaSamw 
4147da6c28aaSamw 	/*
4148da6c28aaSamw 	 * Iterate over all groups and zfs subgroups and check for
4149da6c28aaSamw 	 * resource name in them.
4150da6c28aaSamw 	 */
4151da6c28aaSamw 	for (group = sa_get_group(handle, NULL); group != NULL;
4152da6c28aaSamw 	    group = sa_get_next_group(group)) {
4153da6c28aaSamw 
4154da6c28aaSamw 		if (is_zfs_group(group)) {
4155da6c28aaSamw 			for (zgroup =
4156da6c28aaSamw 			    (sa_group_t)_sa_get_child_node((xmlNodePtr)group,
4157da6c28aaSamw 			    (xmlChar *)"group");
4158da6c28aaSamw 			    zgroup != NULL && resource == NULL;
4159da6c28aaSamw 			    zgroup = sa_get_next_group(zgroup)) {
4160da6c28aaSamw 				resource = find_resource(zgroup, name);
4161da6c28aaSamw 			}
4162da6c28aaSamw 		} else {
4163da6c28aaSamw 			resource = find_resource(group, name);
4164da6c28aaSamw 		}
4165da6c28aaSamw 		if (resource != NULL)
4166da6c28aaSamw 			break;
4167da6c28aaSamw 	}
4168da6c28aaSamw 	return (resource);
4169da6c28aaSamw }
4170da6c28aaSamw 
4171da6c28aaSamw /*
4172da6c28aaSamw  * sa_get_resource(group, resource)
4173da6c28aaSamw  *
4174da6c28aaSamw  * Search all the shares in the specified group for a share with a
4175da6c28aaSamw  * resource name matching the one specified.
4176da6c28aaSamw  *
4177da6c28aaSamw  * In the future, it may be advantageous to allow group to be NULL and
4178da6c28aaSamw  * search all groups but that isn't needed at present.
4179da6c28aaSamw  */
4180da6c28aaSamw 
4181da6c28aaSamw sa_resource_t
sa_get_resource(sa_group_t group,char * resource)4182da6c28aaSamw sa_get_resource(sa_group_t group, char *resource)
4183da6c28aaSamw {
4184da6c28aaSamw 	sa_share_t share = NULL;
4185da6c28aaSamw 	sa_resource_t res = NULL;
4186da6c28aaSamw 
4187da6c28aaSamw 	if (resource != NULL) {
4188da6c28aaSamw 		for (share = sa_get_share(group, NULL);
4189da6c28aaSamw 		    share != NULL && res == NULL;
4190da6c28aaSamw 		    share = sa_get_next_share(share)) {
4191da6c28aaSamw 			res = sa_get_share_resource(share, resource);
4192da6c28aaSamw 		}
4193da6c28aaSamw 	}
4194da6c28aaSamw 	return (res);
4195da6c28aaSamw }
4196da6c28aaSamw 
4197f85463f2Sdougm /*
4198f85463f2Sdougm  * get_protocol_list(optionset, object)
4199f85463f2Sdougm  *
4200f85463f2Sdougm  * Get the protocol optionset list for the object and add them as
4201f85463f2Sdougm  * properties to optionset.
4202f85463f2Sdougm  */
4203f85463f2Sdougm static int
get_protocol_list(sa_optionset_t optionset,void * object)4204f85463f2Sdougm get_protocol_list(sa_optionset_t optionset, void *object)
4205f85463f2Sdougm {
4206f85463f2Sdougm 	sa_property_t prop;
4207f85463f2Sdougm 	sa_optionset_t opts;
4208f85463f2Sdougm 	int ret = SA_OK;
4209f85463f2Sdougm 
4210f85463f2Sdougm 	for (opts = sa_get_optionset(object, NULL);
4211f85463f2Sdougm 	    opts != NULL;
4212f85463f2Sdougm 	    opts = sa_get_next_optionset(opts)) {
4213f85463f2Sdougm 		char *type;
4214f85463f2Sdougm 		type = sa_get_optionset_attr(opts, "type");
4215f85463f2Sdougm 		/*
4216f85463f2Sdougm 		 * It is possible to have a non-protocol optionset. We
4217f85463f2Sdougm 		 * skip any of those found.
4218f85463f2Sdougm 		 */
4219f85463f2Sdougm 		if (type == NULL)
4220f85463f2Sdougm 			continue;
4221f85463f2Sdougm 		prop = sa_create_property(type, "true");
4222f85463f2Sdougm 		sa_free_attr_string(type);
4223f85463f2Sdougm 		if (prop != NULL)
4224f85463f2Sdougm 			prop = (sa_property_t)xmlAddChild((xmlNodePtr)optionset,
4225f85463f2Sdougm 			    (xmlNodePtr)prop);
4226f85463f2Sdougm 		/* If prop is NULL, don't bother continuing */
4227f85463f2Sdougm 		if (prop == NULL) {
4228f85463f2Sdougm 			ret = SA_NO_MEMORY;
4229f85463f2Sdougm 			break;
4230f85463f2Sdougm 		}
4231f85463f2Sdougm 	}
4232f85463f2Sdougm 	return (ret);
4233f85463f2Sdougm }
4234f85463f2Sdougm 
4235f85463f2Sdougm /*
4236f85463f2Sdougm  * sa_free_protoset(optionset)
4237f85463f2Sdougm  *
4238f85463f2Sdougm  * Free the protocol property optionset.
4239f85463f2Sdougm  */
4240f85463f2Sdougm static void
sa_free_protoset(sa_optionset_t optionset)4241f85463f2Sdougm sa_free_protoset(sa_optionset_t optionset)
4242f85463f2Sdougm {
4243f85463f2Sdougm 	if (optionset != NULL) {
4244f85463f2Sdougm 		xmlUnlinkNode((xmlNodePtr) optionset);
4245f85463f2Sdougm 		xmlFreeNode((xmlNodePtr) optionset);
4246f85463f2Sdougm 	}
4247f85463f2Sdougm }
4248f85463f2Sdougm 
4249f85463f2Sdougm /*
4250f85463f2Sdougm  * sa_optionset_t sa_get_active_protocols(object)
4251f85463f2Sdougm  *
4252f85463f2Sdougm  * Return a list of the protocols that are active for the object.
4253f85463f2Sdougm  * This is currently an internal helper function, but could be
4254f85463f2Sdougm  * made visible if there is enough demand for it.
4255f85463f2Sdougm  *
4256f85463f2Sdougm  * The function finds the parent group and extracts the protocol
4257f85463f2Sdougm  * optionsets creating a new optionset with the protocols as properties.
4258f85463f2Sdougm  *
4259f85463f2Sdougm  * The caller must free the returned optionset.
4260f85463f2Sdougm  */
4261f85463f2Sdougm 
4262f85463f2Sdougm static sa_optionset_t
sa_get_active_protocols(void * object)4263f85463f2Sdougm sa_get_active_protocols(void *object)
4264f85463f2Sdougm {
4265f85463f2Sdougm 	sa_optionset_t options;
4266f85463f2Sdougm 	sa_share_t share = NULL;
4267f85463f2Sdougm 	sa_group_t group = NULL;
4268f85463f2Sdougm 	sa_resource_t resource = NULL;
4269f85463f2Sdougm 	int ret = SA_OK;
4270f85463f2Sdougm 
4271f85463f2Sdougm 	if (object == NULL)
4272f85463f2Sdougm 		return (NULL);
4273f85463f2Sdougm 	options = (sa_optionset_t)xmlNewNode(NULL, (xmlChar *)"optionset");
4274f85463f2Sdougm 	if (options == NULL)
4275f85463f2Sdougm 		return (NULL);
4276f85463f2Sdougm 
4277f85463f2Sdougm 	/*
4278f85463f2Sdougm 	 * Find the objects up the tree that might have protocols
4279f85463f2Sdougm 	 * enabled on them.
4280f85463f2Sdougm 	 */
4281f85463f2Sdougm 	if (sa_is_resource(object)) {
4282f85463f2Sdougm 		resource = (sa_resource_t)object;
4283f85463f2Sdougm 		share = sa_get_resource_parent(resource);
4284f85463f2Sdougm 		group = sa_get_parent_group(share);
4285f85463f2Sdougm 	} else if (sa_is_share(object)) {
4286f85463f2Sdougm 		share = (sa_share_t)object;
4287f85463f2Sdougm 		group = sa_get_parent_group(share);
4288f85463f2Sdougm 	} else {
4289f85463f2Sdougm 		group = (sa_group_t)group;
4290f85463f2Sdougm 	}
4291f85463f2Sdougm 	if (resource != NULL)
4292f85463f2Sdougm 		ret = get_protocol_list(options, resource);
4293f85463f2Sdougm 	if (ret == SA_OK && share != NULL)
4294f85463f2Sdougm 		ret = get_protocol_list(options, share);
4295f85463f2Sdougm 	if (ret == SA_OK && group != NULL)
4296f85463f2Sdougm 		ret = get_protocol_list(options, group);
4297f85463f2Sdougm 
4298f85463f2Sdougm 	/*
4299f85463f2Sdougm 	 * If there was an error, we won't have a complete list so
4300f85463f2Sdougm 	 * abandon everything.  The caller will have to deal with the
4301f85463f2Sdougm 	 * issue.
4302f85463f2Sdougm 	 */
4303f85463f2Sdougm 	if (ret != SA_OK) {
4304f85463f2Sdougm 		sa_free_protoset(options);
4305f85463f2Sdougm 		options = NULL;
4306f85463f2Sdougm 	}
4307f85463f2Sdougm 	return (options);
4308f85463f2Sdougm }
4309f85463f2Sdougm 
4310da6c28aaSamw /*
4311da6c28aaSamw  * sa_enable_resource, protocol)
4312da6c28aaSamw  *	Disable the specified share to the specified protocol.
4313da6c28aaSamw  *	If protocol is NULL, then all protocols.
4314da6c28aaSamw  */
4315da6c28aaSamw int
sa_enable_resource(sa_resource_t resource,char * protocol)4316da6c28aaSamw sa_enable_resource(sa_resource_t resource, char *protocol)
4317da6c28aaSamw {
4318da6c28aaSamw 	int ret = SA_OK;
4319da6c28aaSamw 
4320da6c28aaSamw 	if (protocol != NULL) {
4321da6c28aaSamw 		ret = sa_proto_share_resource(protocol, resource);
4322da6c28aaSamw 	} else {
4323f85463f2Sdougm 		sa_optionset_t protoset;
4324f85463f2Sdougm 		sa_property_t prop;
4325f85463f2Sdougm 		char *proto;
4326f85463f2Sdougm 		int err;
4327f85463f2Sdougm 
4328da6c28aaSamw 		/* need to do all protocols */
4329f85463f2Sdougm 		protoset = sa_get_active_protocols(resource);
4330f85463f2Sdougm 		if (protoset == NULL)
4331f85463f2Sdougm 			return (SA_NO_MEMORY);
4332f85463f2Sdougm 		for (prop = sa_get_property(protoset, NULL);
4333f85463f2Sdougm 		    prop != NULL;
4334f85463f2Sdougm 		    prop = sa_get_next_property(prop)) {
4335f85463f2Sdougm 			proto = sa_get_property_attr(prop, "type");
4336f85463f2Sdougm 			if (proto == NULL) {
4337f85463f2Sdougm 				ret = SA_NO_MEMORY;
4338f85463f2Sdougm 				continue;
4339da6c28aaSamw 			}
4340f85463f2Sdougm 			err = sa_proto_share_resource(proto, resource);
4341f85463f2Sdougm 			if (err != SA_OK)
4342f85463f2Sdougm 				ret = err;
4343f85463f2Sdougm 			sa_free_attr_string(proto);
4344da6c28aaSamw 		}
4345f85463f2Sdougm 		sa_free_protoset(protoset);
4346da6c28aaSamw 	}
4347da6c28aaSamw 	if (ret == SA_OK)
4348da6c28aaSamw 		(void) sa_set_resource_attr(resource, "shared", NULL);
4349da6c28aaSamw 
4350da6c28aaSamw 	return (ret);
4351da6c28aaSamw }
4352da6c28aaSamw 
4353da6c28aaSamw /*
4354da6c28aaSamw  * sa_disable_resource(resource, protocol)
4355da6c28aaSamw  *
4356da6c28aaSamw  *	Disable the specified share for the specified protocol.  If
4357da6c28aaSamw  *	protocol is NULL, then all protocols.  If the underlying
4358da6c28aaSamw  *	protocol doesn't implement disable at the resource level, we
4359da6c28aaSamw  *	disable at the share level.
4360da6c28aaSamw  */
4361da6c28aaSamw int
sa_disable_resource(sa_resource_t resource,char * protocol)4362da6c28aaSamw sa_disable_resource(sa_resource_t resource, char *protocol)
4363da6c28aaSamw {
4364da6c28aaSamw 	int ret = SA_OK;
4365da6c28aaSamw 
4366da6c28aaSamw 	if (protocol != NULL) {
4367da6c28aaSamw 		ret = sa_proto_unshare_resource(protocol, resource);
4368da6c28aaSamw 		if (ret == SA_NOT_IMPLEMENTED) {
4369da6c28aaSamw 			sa_share_t parent;
4370da6c28aaSamw 			/*
4371da6c28aaSamw 			 * The protocol doesn't implement unshare
4372da6c28aaSamw 			 * resource. That implies that resource names are
4373da6c28aaSamw 			 * simple aliases for this protocol so we need to
4374da6c28aaSamw 			 * unshare the share.
4375da6c28aaSamw 			 */
4376da6c28aaSamw 			parent = sa_get_resource_parent(resource);
4377da6c28aaSamw 			if (parent != NULL)
4378da6c28aaSamw 				ret = sa_disable_share(parent, protocol);
4379da6c28aaSamw 			else
4380da6c28aaSamw 				ret = SA_CONFIG_ERR;
4381da6c28aaSamw 		}
4382da6c28aaSamw 	} else {
4383f85463f2Sdougm 		sa_optionset_t protoset;
4384f85463f2Sdougm 		sa_property_t prop;
4385f85463f2Sdougm 		char *proto;
4386f85463f2Sdougm 		int err;
4387f85463f2Sdougm 
4388da6c28aaSamw 		/* need to do all protocols */
4389f85463f2Sdougm 		protoset = sa_get_active_protocols(resource);
4390f85463f2Sdougm 		if (protoset == NULL)
4391f85463f2Sdougm 			return (SA_NO_MEMORY);
4392f85463f2Sdougm 		for (prop = sa_get_property(protoset, NULL);
4393f85463f2Sdougm 		    prop != NULL;
4394f85463f2Sdougm 		    prop = sa_get_next_property(prop)) {
4395f85463f2Sdougm 			proto = sa_get_property_attr(prop, "type");
4396f85463f2Sdougm 			if (proto == NULL) {
4397f85463f2Sdougm 				ret = SA_NO_MEMORY;
4398f85463f2Sdougm 				continue;
4399da6c28aaSamw 			}
4400f85463f2Sdougm 			err = sa_proto_unshare_resource(proto, resource);
4401f85463f2Sdougm 			if (err == SA_NOT_SUPPORTED) {
4402f85463f2Sdougm 				sa_share_t parent;
4403f85463f2Sdougm 				parent = sa_get_resource_parent(resource);
4404f85463f2Sdougm 				if (parent != NULL)
4405f85463f2Sdougm 					err = sa_disable_share(parent, proto);
4406f85463f2Sdougm 				else
4407f85463f2Sdougm 					err = SA_CONFIG_ERR;
4408f85463f2Sdougm 			}
4409f85463f2Sdougm 			if (err != SA_OK)
4410f85463f2Sdougm 				ret = err;
4411f85463f2Sdougm 			sa_free_attr_string(proto);
4412da6c28aaSamw 		}
4413f85463f2Sdougm 		sa_free_protoset(protoset);
4414da6c28aaSamw 	}
4415da6c28aaSamw 	if (ret == SA_OK)
4416da6c28aaSamw 		(void) sa_set_resource_attr(resource, "shared", NULL);
4417da6c28aaSamw 
4418da6c28aaSamw 	return (ret);
4419da6c28aaSamw }
4420da6c28aaSamw 
4421da6c28aaSamw /*
4422da6c28aaSamw  * sa_set_resource_description(resource, content)
4423da6c28aaSamw  *
4424da6c28aaSamw  * Set the description of share to content.
4425da6c28aaSamw  */
4426da6c28aaSamw 
4427da6c28aaSamw int
sa_set_resource_description(sa_resource_t resource,char * content)4428da6c28aaSamw sa_set_resource_description(sa_resource_t resource, char *content)
4429da6c28aaSamw {
4430da6c28aaSamw 	xmlNodePtr node;
4431da6c28aaSamw 	sa_group_t group;
4432da6c28aaSamw 	sa_share_t share;
4433da6c28aaSamw 	int ret = SA_OK;
4434da6c28aaSamw 
4435da6c28aaSamw 	for (node = ((xmlNodePtr)resource)->children;
4436da6c28aaSamw 	    node != NULL;
4437da6c28aaSamw 	    node = node->next) {
4438da6c28aaSamw 		if (xmlStrcmp(node->name, (xmlChar *)"description") == 0) {
4439da6c28aaSamw 			break;
4440da6c28aaSamw 		}
4441da6c28aaSamw 	}
4442da6c28aaSamw 
4443da6c28aaSamw 	/* no existing description but want to add */
4444da6c28aaSamw 	if (node == NULL && content != NULL) {
4445da6c28aaSamw 		/* add a description */
4446da6c28aaSamw 		node = _sa_set_share_description(resource, content);
4447da6c28aaSamw 	} else if (node != NULL && content != NULL) {
4448da6c28aaSamw 		/* update a description */
4449da6c28aaSamw 		xmlNodeSetContent(node, (xmlChar *)content);
4450da6c28aaSamw 	} else if (node != NULL && content == NULL) {
4451da6c28aaSamw 		/* remove an existing description */
4452da6c28aaSamw 		xmlUnlinkNode(node);
4453da6c28aaSamw 		xmlFreeNode(node);
4454da6c28aaSamw 	}
4455148c5f43SAlan Wright 
4456da6c28aaSamw 	share = sa_get_resource_parent(resource);
4457da6c28aaSamw 	group = sa_get_parent_group(share);
4458148c5f43SAlan Wright 	if (group != NULL &&
4459148c5f43SAlan Wright 	    sa_is_persistent(share) && (!sa_group_is_zfs(group))) {
4460da6c28aaSamw 		sa_handle_impl_t impl_handle;
4461da6c28aaSamw 		impl_handle = (sa_handle_impl_t)sa_find_group_handle(group);
4462da6c28aaSamw 		if (impl_handle != NULL)
4463da6c28aaSamw 			ret = sa_commit_share(impl_handle->scfhandle,
4464da6c28aaSamw 			    group, share);
4465da6c28aaSamw 		else
4466da6c28aaSamw 			ret = SA_SYSTEM_ERR;
4467da6c28aaSamw 	}
4468da6c28aaSamw 	return (ret);
4469da6c28aaSamw }
4470da6c28aaSamw 
4471da6c28aaSamw /*
4472da6c28aaSamw  * sa_get_resource_description(share)
4473da6c28aaSamw  *
4474da6c28aaSamw  * Return the description text for the specified share if it
4475da6c28aaSamw  * exists. NULL if no description exists.
4476da6c28aaSamw  */
4477da6c28aaSamw 
4478da6c28aaSamw char *
sa_get_resource_description(sa_resource_t resource)4479da6c28aaSamw sa_get_resource_description(sa_resource_t resource)
4480da6c28aaSamw {
4481da6c28aaSamw 	xmlChar *description = NULL;
4482da6c28aaSamw 	xmlNodePtr node;
4483da6c28aaSamw 
4484da6c28aaSamw 	for (node = ((xmlNodePtr)resource)->children; node != NULL;
4485da6c28aaSamw 	    node = node->next) {
4486da6c28aaSamw 		if (xmlStrcmp(node->name, (xmlChar *)"description") == 0)
4487da6c28aaSamw 			break;
4488da6c28aaSamw 	}
4489da6c28aaSamw 	if (node != NULL) {
4490da6c28aaSamw 		description = xmlNodeGetContent(node);
4491da6c28aaSamw 		fixproblemchars((char *)description);
4492da6c28aaSamw 	}
4493da6c28aaSamw 	return ((char *)description);
4494da6c28aaSamw }
4495