xref: /titanic_50/usr/src/lib/libshare/common/libshare.c (revision 5cb0d67909d9970a3e7adbea9422ca3fc88000bf)
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.
24*5cb0d679SMarcel Telka  * Copyright 2014 Nexenta Systems, Inc.  All rights reserved.
256185db85Sdougm  */
266185db85Sdougm 
276185db85Sdougm /*
286185db85Sdougm  * Share control API
296185db85Sdougm  */
306185db85Sdougm #include <stdio.h>
316185db85Sdougm #include <string.h>
326185db85Sdougm #include <ctype.h>
336185db85Sdougm #include <sys/types.h>
346185db85Sdougm #include <sys/stat.h>
35a99982a7Sdougm #include <fcntl.h>
366185db85Sdougm #include <unistd.h>
376185db85Sdougm #include <libxml/parser.h>
386185db85Sdougm #include <libxml/tree.h>
396185db85Sdougm #include "libshare.h"
406185db85Sdougm #include "libshare_impl.h"
416185db85Sdougm #include <libscf.h>
426185db85Sdougm #include "scfutil.h"
436185db85Sdougm #include <ctype.h>
446185db85Sdougm #include <libintl.h>
45549ec3ffSdougm #include <thread.h>
46549ec3ffSdougm #include <synch.h>
476185db85Sdougm 
48a99982a7Sdougm #define	DFS_LOCK_FILE	"/etc/dfs/fstypes"
4957b448deSdougm #define	SA_STRSIZE	256	/* max string size for names */
50a99982a7Sdougm 
516185db85Sdougm /*
52da6c28aaSamw  * internal object type values returned by sa_get_object_type()
53da6c28aaSamw  */
54da6c28aaSamw #define	SA_TYPE_UNKNOWN		0
55da6c28aaSamw #define	SA_TYPE_GROUP		1
56da6c28aaSamw #define	SA_TYPE_SHARE		2
57da6c28aaSamw #define	SA_TYPE_RESOURCE	3
58da6c28aaSamw #define	SA_TYPE_OPTIONSET	4
59da6c28aaSamw #define	SA_TYPE_ALTSPACE	5
60da6c28aaSamw 
61da6c28aaSamw /*
626185db85Sdougm  * internal data structures
636185db85Sdougm  */
646185db85Sdougm 
656185db85Sdougm extern struct sa_proto_plugin *sap_proto_list;
666185db85Sdougm 
676185db85Sdougm /* current SMF/SVC repository handle */
68549ec3ffSdougm extern void getlegacyconfig(sa_handle_t, char *, xmlNodePtr *);
69549ec3ffSdougm extern int gettransients(sa_handle_impl_t, xmlNodePtr *);
706185db85Sdougm extern char *sa_fstype(char *);
716185db85Sdougm extern int sa_is_share(void *);
72da6c28aaSamw extern int sa_is_resource(void *);
736185db85Sdougm extern ssize_t scf_max_name_len; /* defined in scfutil during initialization */
746185db85Sdougm extern int sa_group_is_zfs(sa_group_t);
756185db85Sdougm extern int sa_path_is_zfs(char *);
766185db85Sdougm extern int sa_zfs_set_sharenfs(sa_group_t, char *, int);
77da6c28aaSamw extern int sa_zfs_set_sharesmb(sa_group_t, char *, int);
78549ec3ffSdougm extern void update_legacy_config(sa_handle_t);
796185db85Sdougm extern int issubdir(char *, char *);
8057b448deSdougm extern int sa_zfs_init(sa_handle_impl_t);
81549ec3ffSdougm extern void sa_zfs_fini(sa_handle_impl_t);
82a99982a7Sdougm extern void sablocksigs(sigset_t *);
83a99982a7Sdougm extern void saunblocksigs(sigset_t *);
84da6c28aaSamw static sa_group_t sa_get_optionset_parent(sa_optionset_t);
85da6c28aaSamw static char *get_node_attr(void *, char *);
865b6e0c46Sdougm extern void sa_update_sharetab_ts(sa_handle_t);
876185db85Sdougm 
88549ec3ffSdougm /*
89549ec3ffSdougm  * Data structures for finding/managing the document root to access
90549ec3ffSdougm  * handle mapping. The list isn't expected to grow very large so a
91549ec3ffSdougm  * simple list is acceptable. The purpose is to provide a way to start
92549ec3ffSdougm  * with a group or share and find the library handle needed for
93549ec3ffSdougm  * various operations.
94549ec3ffSdougm  */
95549ec3ffSdougm mutex_t sa_global_lock;
96549ec3ffSdougm struct doc2handle {
97549ec3ffSdougm 	struct doc2handle	*next;
98549ec3ffSdougm 	xmlNodePtr		root;
99549ec3ffSdougm 	sa_handle_impl_t	handle;
100549ec3ffSdougm };
101549ec3ffSdougm 
102148c5f43SAlan Wright mutex_t sa_dfstab_lock;
103148c5f43SAlan Wright 
10457b448deSdougm /* definitions used in a couple of property functions */
10557b448deSdougm #define	SA_PROP_OP_REMOVE	1
10657b448deSdougm #define	SA_PROP_OP_ADD		2
10757b448deSdougm #define	SA_PROP_OP_UPDATE	3
10857b448deSdougm 
109549ec3ffSdougm static struct doc2handle *sa_global_handles = NULL;
1106185db85Sdougm 
1116185db85Sdougm /* helper functions */
1126185db85Sdougm 
113549ec3ffSdougm /*
114549ec3ffSdougm  * sa_errorstr(err)
115549ec3ffSdougm  *
116549ec3ffSdougm  * convert an error value to an error string
117549ec3ffSdougm  */
118549ec3ffSdougm 
1196185db85Sdougm char *
sa_errorstr(int err)1206185db85Sdougm sa_errorstr(int err)
1216185db85Sdougm {
1226185db85Sdougm 	static char errstr[32];
1236185db85Sdougm 	char *ret = NULL;
1246185db85Sdougm 
1256185db85Sdougm 	switch (err) {
1266185db85Sdougm 	case SA_OK:
12724424a35Sdougm 		ret = dgettext(TEXT_DOMAIN, "ok");
1286185db85Sdougm 		break;
1296185db85Sdougm 	case SA_NO_SUCH_PATH:
13024424a35Sdougm 		ret = dgettext(TEXT_DOMAIN, "path doesn't exist");
1316185db85Sdougm 		break;
1326185db85Sdougm 	case SA_NO_MEMORY:
13324424a35Sdougm 		ret = dgettext(TEXT_DOMAIN, "no memory");
1346185db85Sdougm 		break;
1356185db85Sdougm 	case SA_DUPLICATE_NAME:
13624424a35Sdougm 		ret = dgettext(TEXT_DOMAIN, "name in use");
1376185db85Sdougm 		break;
1386185db85Sdougm 	case SA_BAD_PATH:
13924424a35Sdougm 		ret = dgettext(TEXT_DOMAIN, "bad path");
1406185db85Sdougm 		break;
1416185db85Sdougm 	case SA_NO_SUCH_GROUP:
14224424a35Sdougm 		ret = dgettext(TEXT_DOMAIN, "no such group");
1436185db85Sdougm 		break;
1446185db85Sdougm 	case SA_CONFIG_ERR:
14524424a35Sdougm 		ret = dgettext(TEXT_DOMAIN, "configuration error");
1466185db85Sdougm 		break;
1476185db85Sdougm 	case SA_SYSTEM_ERR:
14824424a35Sdougm 		ret = dgettext(TEXT_DOMAIN, "system error");
1496185db85Sdougm 		break;
1506185db85Sdougm 	case SA_SYNTAX_ERR:
15124424a35Sdougm 		ret = dgettext(TEXT_DOMAIN, "syntax error");
1526185db85Sdougm 		break;
1536185db85Sdougm 	case SA_NO_PERMISSION:
15424424a35Sdougm 		ret = dgettext(TEXT_DOMAIN, "no permission");
1556185db85Sdougm 		break;
1566185db85Sdougm 	case SA_BUSY:
15724424a35Sdougm 		ret = dgettext(TEXT_DOMAIN, "busy");
1586185db85Sdougm 		break;
1596185db85Sdougm 	case SA_NO_SUCH_PROP:
16024424a35Sdougm 		ret = dgettext(TEXT_DOMAIN, "no such property");
1616185db85Sdougm 		break;
1626185db85Sdougm 	case SA_INVALID_NAME:
16324424a35Sdougm 		ret = dgettext(TEXT_DOMAIN, "invalid name");
1646185db85Sdougm 		break;
1656185db85Sdougm 	case SA_INVALID_PROTOCOL:
16624424a35Sdougm 		ret = dgettext(TEXT_DOMAIN, "invalid protocol");
1676185db85Sdougm 		break;
1686185db85Sdougm 	case SA_NOT_ALLOWED:
16924424a35Sdougm 		ret = dgettext(TEXT_DOMAIN, "operation not allowed");
1706185db85Sdougm 		break;
1716185db85Sdougm 	case SA_BAD_VALUE:
17224424a35Sdougm 		ret = dgettext(TEXT_DOMAIN, "bad property value");
1736185db85Sdougm 		break;
1746185db85Sdougm 	case SA_INVALID_SECURITY:
17524424a35Sdougm 		ret = dgettext(TEXT_DOMAIN, "invalid security type");
1766185db85Sdougm 		break;
1776185db85Sdougm 	case SA_NO_SUCH_SECURITY:
17824424a35Sdougm 		ret = dgettext(TEXT_DOMAIN, "security type not found");
1796185db85Sdougm 		break;
1806185db85Sdougm 	case SA_VALUE_CONFLICT:
18124424a35Sdougm 		ret = dgettext(TEXT_DOMAIN, "property value conflict");
1826185db85Sdougm 		break;
1836185db85Sdougm 	case SA_NOT_IMPLEMENTED:
18424424a35Sdougm 		ret = dgettext(TEXT_DOMAIN, "not implemented");
1856185db85Sdougm 		break;
1866185db85Sdougm 	case SA_INVALID_PATH:
18724424a35Sdougm 		ret = dgettext(TEXT_DOMAIN, "invalid path");
1886185db85Sdougm 		break;
1896185db85Sdougm 	case SA_NOT_SUPPORTED:
19024424a35Sdougm 		ret = dgettext(TEXT_DOMAIN, "operation not supported");
1916185db85Sdougm 		break;
1926185db85Sdougm 	case SA_PROP_SHARE_ONLY:
19324424a35Sdougm 		ret = dgettext(TEXT_DOMAIN, "property not valid for group");
1946185db85Sdougm 		break;
1956185db85Sdougm 	case SA_NOT_SHARED:
19624424a35Sdougm 		ret = dgettext(TEXT_DOMAIN, "not shared");
1976185db85Sdougm 		break;
198da6c28aaSamw 	case SA_NO_SUCH_RESOURCE:
199da6c28aaSamw 		ret = dgettext(TEXT_DOMAIN, "no such resource");
200da6c28aaSamw 		break;
201da6c28aaSamw 	case SA_RESOURCE_REQUIRED:
202da6c28aaSamw 		ret = dgettext(TEXT_DOMAIN, "resource name required");
203da6c28aaSamw 		break;
204da6c28aaSamw 	case SA_MULTIPLE_ERROR:
205da6c28aaSamw 		ret = dgettext(TEXT_DOMAIN, "errors from multiple protocols");
206da6c28aaSamw 		break;
207da6c28aaSamw 	case SA_PATH_IS_SUBDIR:
208da6c28aaSamw 		ret = dgettext(TEXT_DOMAIN, "path is a subpath of share");
209da6c28aaSamw 		break;
210da6c28aaSamw 	case SA_PATH_IS_PARENTDIR:
211da6c28aaSamw 		ret = dgettext(TEXT_DOMAIN, "path is parent of a share");
212da6c28aaSamw 		break;
2134bff34e3Sthurlow 	case SA_NO_SECTION:
2144bff34e3Sthurlow 		ret = dgettext(TEXT_DOMAIN, "protocol requires a section");
2154bff34e3Sthurlow 		break;
2164bff34e3Sthurlow 	case SA_NO_PROPERTIES:
2174bff34e3Sthurlow 		ret = dgettext(TEXT_DOMAIN, "properties not found");
2184bff34e3Sthurlow 		break;
2194bff34e3Sthurlow 	case SA_NO_SUCH_SECTION:
2204bff34e3Sthurlow 		ret = dgettext(TEXT_DOMAIN, "section not found");
2214bff34e3Sthurlow 		break;
2224bff34e3Sthurlow 	case SA_PASSWORD_ENC:
2234bff34e3Sthurlow 		ret = dgettext(TEXT_DOMAIN, "passwords must be encrypted");
2244bff34e3Sthurlow 		break;
2250616fd7fSPavel Filipensky 	case SA_SHARE_EXISTS:
2260616fd7fSPavel Filipensky 		ret = dgettext(TEXT_DOMAIN, "path or file is already shared");
2270616fd7fSPavel Filipensky 		break;
2286185db85Sdougm 	default:
2296185db85Sdougm 		(void) snprintf(errstr, sizeof (errstr),
23024424a35Sdougm 		    dgettext(TEXT_DOMAIN, "unknown %d"), err);
2316185db85Sdougm 		ret = errstr;
2326185db85Sdougm 	}
2336185db85Sdougm 	return (ret);
2346185db85Sdougm }
2356185db85Sdougm 
2366185db85Sdougm /*
237549ec3ffSdougm  * Document root to active handle mapping functions.  These are only
238549ec3ffSdougm  * used internally. A mutex is used to prevent access while the list
239549ec3ffSdougm  * is changing. In general, the list will be relatively short - one
240549ec3ffSdougm  * item per thread that has called sa_init().
241549ec3ffSdougm  */
242549ec3ffSdougm 
243549ec3ffSdougm sa_handle_impl_t
get_handle_for_root(xmlNodePtr root)244549ec3ffSdougm get_handle_for_root(xmlNodePtr root)
245549ec3ffSdougm {
246549ec3ffSdougm 	struct doc2handle *item;
247549ec3ffSdougm 
248549ec3ffSdougm 	(void) mutex_lock(&sa_global_lock);
249549ec3ffSdougm 	for (item = sa_global_handles; item != NULL; item = item->next) {
250549ec3ffSdougm 		if (item->root == root)
251549ec3ffSdougm 			break;
252549ec3ffSdougm 	}
253549ec3ffSdougm 	(void) mutex_unlock(&sa_global_lock);
254549ec3ffSdougm 	if (item != NULL)
255549ec3ffSdougm 		return (item->handle);
256549ec3ffSdougm 	return (NULL);
257549ec3ffSdougm }
258549ec3ffSdougm 
259549ec3ffSdougm static int
add_handle_for_root(xmlNodePtr root,sa_handle_impl_t handle)260549ec3ffSdougm add_handle_for_root(xmlNodePtr root, sa_handle_impl_t handle)
261549ec3ffSdougm {
262549ec3ffSdougm 	struct doc2handle *item;
263549ec3ffSdougm 	int ret = SA_NO_MEMORY;
264549ec3ffSdougm 
265549ec3ffSdougm 	item = (struct doc2handle *)calloc(sizeof (struct doc2handle), 1);
266549ec3ffSdougm 	if (item != NULL) {
267549ec3ffSdougm 		item->root = root;
268549ec3ffSdougm 		item->handle = handle;
269549ec3ffSdougm 		(void) mutex_lock(&sa_global_lock);
270549ec3ffSdougm 		item->next = sa_global_handles;
271549ec3ffSdougm 		sa_global_handles = item;
272549ec3ffSdougm 		(void) mutex_unlock(&sa_global_lock);
273549ec3ffSdougm 		ret = SA_OK;
274549ec3ffSdougm 	}
275549ec3ffSdougm 	return (ret);
276549ec3ffSdougm }
277549ec3ffSdougm 
278549ec3ffSdougm /*
279549ec3ffSdougm  * remove_handle_for_root(root)
280549ec3ffSdougm  *
281549ec3ffSdougm  * Walks the list of handles and removes the one for this "root" from
282549ec3ffSdougm  * the list. It is up to the caller to free the data.
283549ec3ffSdougm  */
284549ec3ffSdougm 
285549ec3ffSdougm static void
remove_handle_for_root(xmlNodePtr root)286549ec3ffSdougm remove_handle_for_root(xmlNodePtr root)
287549ec3ffSdougm {
288549ec3ffSdougm 	struct doc2handle *item, *prev;
289549ec3ffSdougm 
290549ec3ffSdougm 	(void) mutex_lock(&sa_global_lock);
291549ec3ffSdougm 	for (prev = NULL, item = sa_global_handles; item != NULL;
292549ec3ffSdougm 	    item = item->next) {
293549ec3ffSdougm 		if (item->root == root) {
294549ec3ffSdougm 			/* first in the list */
29557b448deSdougm 			if (prev == NULL)
296549ec3ffSdougm 				sa_global_handles = sa_global_handles->next;
29757b448deSdougm 			else
298549ec3ffSdougm 				prev->next = item->next;
299549ec3ffSdougm 			/* Item is out of the list so free the list structure */
300549ec3ffSdougm 			free(item);
301549ec3ffSdougm 			break;
302549ec3ffSdougm 		}
303549ec3ffSdougm 		prev = item;
304549ec3ffSdougm 	}
305549ec3ffSdougm 	(void) mutex_unlock(&sa_global_lock);
306549ec3ffSdougm }
307549ec3ffSdougm 
308549ec3ffSdougm /*
309549ec3ffSdougm  * sa_find_group_handle(sa_group_t group)
310549ec3ffSdougm  *
311549ec3ffSdougm  * Find the sa_handle_t for the configuration associated with this
312549ec3ffSdougm  * group.
313549ec3ffSdougm  */
314549ec3ffSdougm sa_handle_t
sa_find_group_handle(sa_group_t group)315549ec3ffSdougm sa_find_group_handle(sa_group_t group)
316549ec3ffSdougm {
317549ec3ffSdougm 	xmlNodePtr node = (xmlNodePtr)group;
318549ec3ffSdougm 	sa_handle_t handle;
319549ec3ffSdougm 
320549ec3ffSdougm 	while (node != NULL) {
321549ec3ffSdougm 		if (strcmp((char *)(node->name), "sharecfg") == 0) {
322549ec3ffSdougm 			/* have the root so get the handle */
323549ec3ffSdougm 			handle = (sa_handle_t)get_handle_for_root(node);
324549ec3ffSdougm 			return (handle);
325549ec3ffSdougm 		}
326549ec3ffSdougm 		node = node->parent;
327549ec3ffSdougm 	}
328549ec3ffSdougm 	return (NULL);
329549ec3ffSdougm }
330549ec3ffSdougm 
331549ec3ffSdougm /*
3326185db85Sdougm  * set_legacy_timestamp(root, path, timevalue)
3336185db85Sdougm  *
3346185db85Sdougm  * add the current timestamp value to the configuration for use in
3356185db85Sdougm  * determining when to update the legacy files.  For SMF, this
3366185db85Sdougm  * property is kept in default/operation/legacy_timestamp
3376185db85Sdougm  */
3386185db85Sdougm 
3396185db85Sdougm static void
set_legacy_timestamp(xmlNodePtr root,char * path,uint64_t tval)3406185db85Sdougm set_legacy_timestamp(xmlNodePtr root, char *path, uint64_t tval)
3416185db85Sdougm {
3426185db85Sdougm 	xmlNodePtr node;
3436185db85Sdougm 	xmlChar *lpath = NULL;
344549ec3ffSdougm 	sa_handle_impl_t handle;
345549ec3ffSdougm 
346549ec3ffSdougm 	/* Have to have a handle or else we weren't initialized. */
347549ec3ffSdougm 	handle = get_handle_for_root(root);
348549ec3ffSdougm 	if (handle == NULL)
349549ec3ffSdougm 		return;
3506185db85Sdougm 
3516185db85Sdougm 	for (node = root->xmlChildrenNode; node != NULL;
3526185db85Sdougm 	    node = node->next) {
3536185db85Sdougm 		if (xmlStrcmp(node->name, (xmlChar *)"legacy") == 0) {
3546185db85Sdougm 			/* a possible legacy node for this path */
3556185db85Sdougm 			lpath = xmlGetProp(node, (xmlChar *)"path");
35657b448deSdougm 			if (lpath != NULL &&
35757b448deSdougm 			    xmlStrcmp(lpath, (xmlChar *)path) == 0) {
3586185db85Sdougm 				xmlFree(lpath);
3596185db85Sdougm 				break;
3606185db85Sdougm 			}
3616185db85Sdougm 			if (lpath != NULL)
3626185db85Sdougm 				xmlFree(lpath);
3636185db85Sdougm 		}
3646185db85Sdougm 	}
3656185db85Sdougm 	if (node == NULL) {
3666185db85Sdougm 		/* need to create the first legacy timestamp node */
3676185db85Sdougm 		node = xmlNewChild(root, NULL, (xmlChar *)"legacy", NULL);
3686185db85Sdougm 	}
3696185db85Sdougm 	if (node != NULL) {
3706185db85Sdougm 		char tstring[32];
3716185db85Sdougm 		int ret;
3726185db85Sdougm 
3736185db85Sdougm 		(void) snprintf(tstring, sizeof (tstring), "%lld", tval);
3744bff34e3Sthurlow 		(void) xmlSetProp(node, (xmlChar *)"timestamp",
3754bff34e3Sthurlow 		    (xmlChar *)tstring);
3764bff34e3Sthurlow 		(void) xmlSetProp(node, (xmlChar *)"path", (xmlChar *)path);
3776185db85Sdougm 		/* now commit to SMF */
378549ec3ffSdougm 		ret = sa_get_instance(handle->scfhandle, "default");
3796185db85Sdougm 		if (ret == SA_OK) {
38057b448deSdougm 			ret = sa_start_transaction(handle->scfhandle,
38157b448deSdougm 			    "operation");
3826185db85Sdougm 			if (ret == SA_OK) {
38357b448deSdougm 				ret = sa_set_property(handle->scfhandle,
38457b448deSdougm 				    "legacy-timestamp", tstring);
3856185db85Sdougm 				if (ret == SA_OK) {
38657b448deSdougm 					(void) sa_end_transaction(
3875b6e0c46Sdougm 					    handle->scfhandle, handle);
3886185db85Sdougm 				} else {
389549ec3ffSdougm 					sa_abort_transaction(handle->scfhandle);
3906185db85Sdougm 				}
3916185db85Sdougm 			}
3926185db85Sdougm 		}
3936185db85Sdougm 	}
3946185db85Sdougm }
3956185db85Sdougm 
3966185db85Sdougm /*
3976185db85Sdougm  * is_shared(share)
3986185db85Sdougm  *
3996185db85Sdougm  * determine if the specified share is currently shared or not.
4006185db85Sdougm  */
4016185db85Sdougm static int
is_shared(sa_share_t share)4026185db85Sdougm is_shared(sa_share_t share)
4036185db85Sdougm {
4046185db85Sdougm 	char *shared;
4056185db85Sdougm 	int result = 0; /* assume not */
4066185db85Sdougm 
4076185db85Sdougm 	shared = sa_get_share_attr(share, "shared");
4086185db85Sdougm 	if (shared != NULL) {
4096185db85Sdougm 		if (strcmp(shared, "true") == 0)
4106185db85Sdougm 			result = 1;
4116185db85Sdougm 		sa_free_attr_string(shared);
4126185db85Sdougm 	}
4136185db85Sdougm 	return (result);
4146185db85Sdougm }
4156185db85Sdougm 
4166185db85Sdougm /*
417da6c28aaSamw  * excluded_protocol(share, proto)
418da6c28aaSamw  *
419da6c28aaSamw  * Returns B_TRUE if the specified protocol appears in the "exclude"
420da6c28aaSamw  * property. This is used to prevent sharing special case shares
421da6c28aaSamw  * (e.g. subdirs when SMB wants a subdir and NFS doesn't. B_FALSE is
422da6c28aaSamw  * returned if the protocol isn't in the list.
423da6c28aaSamw  */
424da6c28aaSamw static boolean_t
excluded_protocol(sa_share_t share,char * proto)425da6c28aaSamw excluded_protocol(sa_share_t share, char *proto)
426da6c28aaSamw {
427da6c28aaSamw 	char *protolist;
428da6c28aaSamw 	char *str;
429da6c28aaSamw 	char *token;
430da6c28aaSamw 
431da6c28aaSamw 	protolist = sa_get_share_attr(share, "exclude");
432da6c28aaSamw 	if (protolist != NULL) {
433da6c28aaSamw 		str = protolist;
434da6c28aaSamw 		while ((token = strtok(str, ",")) != NULL) {
435da6c28aaSamw 			if (strcmp(token, proto) == 0) {
436da6c28aaSamw 				sa_free_attr_string(protolist);
437da6c28aaSamw 				return (B_TRUE);
438da6c28aaSamw 			}
439da6c28aaSamw 			str = NULL;
440da6c28aaSamw 		}
441da6c28aaSamw 		sa_free_attr_string(protolist);
442da6c28aaSamw 	}
443da6c28aaSamw 	return (B_FALSE);
444da6c28aaSamw }
445da6c28aaSamw 
446da6c28aaSamw /*
447a99982a7Sdougm  * checksubdirgroup(group, newpath, strictness)
448f345c0beSdougm  *
449a99982a7Sdougm  * check all the specified newpath against all the paths in the
450a99982a7Sdougm  * group. This is a helper function for checksubdir to make it easier
451a99982a7Sdougm  * to also check ZFS subgroups.
452a99982a7Sdougm  * The strictness values mean:
453f345c0beSdougm  * SA_CHECK_NORMAL == only check newpath against shares that are active
454f345c0beSdougm  * SA_CHECK_STRICT == check newpath against both active shares and those
455f345c0beSdougm  *		      stored in the repository
4566185db85Sdougm  */
4576185db85Sdougm static int
checksubdirgroup(sa_group_t group,char * newpath,int strictness)458a99982a7Sdougm checksubdirgroup(sa_group_t group, char *newpath, int strictness)
4596185db85Sdougm {
4606185db85Sdougm 	sa_share_t share;
461a99982a7Sdougm 	char *path;
462a99982a7Sdougm 	int issub = SA_OK;
463da6c28aaSamw 	int subdir;
464da6c28aaSamw 	int parent;
465da6c28aaSamw 
466da6c28aaSamw 	if (newpath == NULL)
467da6c28aaSamw 		return (SA_INVALID_PATH);
4686185db85Sdougm 
4696185db85Sdougm 	for (share = sa_get_share(group, NULL); share != NULL;
4706185db85Sdougm 	    share = sa_get_next_share(share)) {
4716185db85Sdougm 		/*
4726185db85Sdougm 		 * The original behavior of share never checked
4736185db85Sdougm 		 * against the permanent configuration
4746185db85Sdougm 		 * (/etc/dfs/dfstab).  PIT has a number of cases where
4756185db85Sdougm 		 * it depends on this older behavior even though it
4766185db85Sdougm 		 * could be considered incorrect.  We may tighten this
4776185db85Sdougm 		 * up in the future.
4786185db85Sdougm 		 */
479f345c0beSdougm 		if (strictness == SA_CHECK_NORMAL && !is_shared(share))
4806185db85Sdougm 			continue;
4816185db85Sdougm 
4826185db85Sdougm 		path = sa_get_share_attr(share, "path");
483f345c0beSdougm 		/*
484f345c0beSdougm 		 * If path is NULL, then a share is in the process of
485f345c0beSdougm 		 * construction or someone has modified the property
486a99982a7Sdougm 		 * group inappropriately. It should be
487a99982a7Sdougm 		 * ignored. issubdir() comes from the original share
488a99982a7Sdougm 		 * implementation and does the difficult part of
489a99982a7Sdougm 		 * checking subdirectories.
490f345c0beSdougm 		 */
491f345c0beSdougm 		if (path == NULL)
492f345c0beSdougm 			continue;
493da6c28aaSamw 
494da6c28aaSamw 		if (strcmp(path, newpath) == 0) {
495da6c28aaSamw 			issub = SA_INVALID_PATH;
496da6c28aaSamw 		} else {
497da6c28aaSamw 			subdir = issubdir(newpath, path);
498da6c28aaSamw 			parent = issubdir(path, newpath);
499da6c28aaSamw 			if (subdir || parent) {
5006185db85Sdougm 				sa_free_attr_string(path);
5016185db85Sdougm 				path = NULL;
502da6c28aaSamw 				return (subdir ?
503da6c28aaSamw 				    SA_PATH_IS_SUBDIR : SA_PATH_IS_PARENTDIR);
504da6c28aaSamw 			}
5056185db85Sdougm 		}
5066185db85Sdougm 		sa_free_attr_string(path);
5076185db85Sdougm 		path = NULL;
5086185db85Sdougm 	}
509a99982a7Sdougm 	return (issub);
510a99982a7Sdougm }
511a99982a7Sdougm 
512a99982a7Sdougm /*
513a99982a7Sdougm  * checksubdir(newpath, strictness)
514a99982a7Sdougm  *
515a99982a7Sdougm  * checksubdir determines if the specified path (newpath) is a
516a99982a7Sdougm  * subdirectory of another share. It calls checksubdirgroup() to do
517a99982a7Sdougm  * the complicated work. The strictness parameter determines how
518a99982a7Sdougm  * strict a check to make against the path. The strictness values
519a99982a7Sdougm  * mean: SA_CHECK_NORMAL == only check newpath against shares that are
520a99982a7Sdougm  * active SA_CHECK_STRICT == check newpath against both active shares
521a99982a7Sdougm  * and those * stored in the repository
522a99982a7Sdougm  */
523a99982a7Sdougm static int
checksubdir(sa_handle_t handle,char * newpath,int strictness)524549ec3ffSdougm checksubdir(sa_handle_t handle, char *newpath, int strictness)
525a99982a7Sdougm {
526a99982a7Sdougm 	sa_group_t group;
527da6c28aaSamw 	int issub = SA_OK;
528a99982a7Sdougm 	char *path = NULL;
529a99982a7Sdougm 
530da6c28aaSamw 	for (group = sa_get_group(handle, NULL);
531da6c28aaSamw 	    group != NULL && issub == SA_OK;
532da6c28aaSamw 	    group = sa_get_next_group(group)) {
533a99982a7Sdougm 		if (sa_group_is_zfs(group)) {
534a99982a7Sdougm 			sa_group_t subgroup;
535a99982a7Sdougm 			for (subgroup = sa_get_sub_group(group);
536da6c28aaSamw 			    subgroup != NULL && issub == SA_OK;
537a99982a7Sdougm 			    subgroup = sa_get_next_group(subgroup))
53857b448deSdougm 				issub = checksubdirgroup(subgroup, newpath,
53957b448deSdougm 				    strictness);
540a99982a7Sdougm 		} else {
541a99982a7Sdougm 			issub = checksubdirgroup(group, newpath, strictness);
542a99982a7Sdougm 		}
5436185db85Sdougm 	}
5446185db85Sdougm 	if (path != NULL)
5456185db85Sdougm 		sa_free_attr_string(path);
5466185db85Sdougm 	return (issub);
5476185db85Sdougm }
5486185db85Sdougm 
5496185db85Sdougm /*
550f345c0beSdougm  * validpath(path, strictness)
5516185db85Sdougm  * determine if the provided path is valid for a share. It shouldn't
5526185db85Sdougm  * be a sub-dir of an already shared path or the parent directory of a
5536185db85Sdougm  * share path.
5546185db85Sdougm  */
5556185db85Sdougm static int
validpath(sa_handle_t handle,char * path,int strictness)556549ec3ffSdougm validpath(sa_handle_t handle, char *path, int strictness)
5576185db85Sdougm {
5586185db85Sdougm 	int error = SA_OK;
5596185db85Sdougm 	struct stat st;
5606185db85Sdougm 	sa_share_t share;
5616185db85Sdougm 	char *fstype;
5626185db85Sdougm 
56357b448deSdougm 	if (*path != '/')
5646185db85Sdougm 		return (SA_BAD_PATH);
56557b448deSdougm 
5666185db85Sdougm 	if (stat(path, &st) < 0) {
5676185db85Sdougm 		error = SA_NO_SUCH_PATH;
5686185db85Sdougm 	} else {
569549ec3ffSdougm 		share = sa_find_share(handle, path);
57057b448deSdougm 		if (share != NULL)
5716185db85Sdougm 			error = SA_DUPLICATE_NAME;
57257b448deSdougm 
5736185db85Sdougm 		if (error == SA_OK) {
5746185db85Sdougm 			/*
57557b448deSdougm 			 * check for special case with file system
57657b448deSdougm 			 * that might have restrictions.  For now, ZFS
57757b448deSdougm 			 * is the only case since it has its own idea
57857b448deSdougm 			 * of how to configure shares. We do this
57957b448deSdougm 			 * before subdir checking since things like
58057b448deSdougm 			 * ZFS will do that for us. This should also
58157b448deSdougm 			 * be done via plugin interface.
5826185db85Sdougm 			 */
5836185db85Sdougm 			fstype = sa_fstype(path);
5846185db85Sdougm 			if (fstype != NULL && strcmp(fstype, "zfs") == 0) {
585549ec3ffSdougm 				if (sa_zfs_is_shared(handle, path))
586a99982a7Sdougm 					error = SA_INVALID_NAME;
5876185db85Sdougm 			}
5886185db85Sdougm 			if (fstype != NULL)
5896185db85Sdougm 				sa_free_fstype(fstype);
5906185db85Sdougm 		}
59157b448deSdougm 		if (error == SA_OK)
592549ec3ffSdougm 			error = checksubdir(handle, path, strictness);
5936185db85Sdougm 	}
5946185db85Sdougm 	return (error);
5956185db85Sdougm }
5966185db85Sdougm 
5976185db85Sdougm /*
5986185db85Sdougm  * check to see if group/share is persistent.
599da6c28aaSamw  *
600da6c28aaSamw  * "group" can be either an sa_group_t or an sa_share_t. (void *)
601*5cb0d679SMarcel Telka  * works since both these types are also void *.
602148c5f43SAlan Wright  * If the share is a ZFS share, mark it as persistent.
6036185db85Sdougm  */
604da6c28aaSamw int
sa_is_persistent(void * group)605da6c28aaSamw sa_is_persistent(void *group)
6066185db85Sdougm {
6076185db85Sdougm 	char *type;
6086185db85Sdougm 	int persist = 1;
609148c5f43SAlan Wright 	sa_group_t grp;
6106185db85Sdougm 
611da6c28aaSamw 	type = sa_get_group_attr((sa_group_t)group, "type");
612148c5f43SAlan Wright 	if (type != NULL) {
613148c5f43SAlan Wright 		if (strcmp(type, "transient") == 0)
6146185db85Sdougm 			persist = 0;
6156185db85Sdougm 		sa_free_attr_string(type);
616148c5f43SAlan Wright 	}
617148c5f43SAlan Wright 
618148c5f43SAlan Wright 	grp = (sa_is_share(group)) ? sa_get_parent_group(group) : group;
619148c5f43SAlan Wright 	if (sa_group_is_zfs(grp))
620148c5f43SAlan Wright 		persist = 1;
621148c5f43SAlan Wright 
6226185db85Sdougm 	return (persist);
6236185db85Sdougm }
6246185db85Sdougm 
6256185db85Sdougm /*
6266185db85Sdougm  * sa_valid_group_name(name)
6276185db85Sdougm  *
6286185db85Sdougm  * check that the "name" contains only valid characters and otherwise
6296185db85Sdougm  * fits the required naming conventions. Valid names must start with
6306185db85Sdougm  * an alphabetic and the remainder may consist of only alphanumeric
6316185db85Sdougm  * plus the '-' and '_' characters. This name limitation comes from
6326185db85Sdougm  * inherent limitations in SMF.
6336185db85Sdougm  */
6346185db85Sdougm 
6356185db85Sdougm int
sa_valid_group_name(char * name)6366185db85Sdougm sa_valid_group_name(char *name)
6376185db85Sdougm {
6386185db85Sdougm 	int ret = 1;
6396185db85Sdougm 	ssize_t len;
6406185db85Sdougm 
6416185db85Sdougm 	if (name != NULL && isalpha(*name)) {
6426185db85Sdougm 		char c;
6436185db85Sdougm 		len = strlen(name);
6446185db85Sdougm 		if (len < (scf_max_name_len - sizeof ("group:"))) {
6456185db85Sdougm 			for (c = *name++; c != '\0' && ret != 0; c = *name++) {
6466185db85Sdougm 				if (!isalnum(c) && c != '-' && c != '_')
6476185db85Sdougm 					ret = 0;
6486185db85Sdougm 			}
6496185db85Sdougm 		} else {
6506185db85Sdougm 			ret = 0;
6516185db85Sdougm 		}
6526185db85Sdougm 	} else {
6536185db85Sdougm 		ret = 0;
6546185db85Sdougm 	}
6556185db85Sdougm 	return (ret);
6566185db85Sdougm }
6576185db85Sdougm 
6586185db85Sdougm 
6596185db85Sdougm /*
6606185db85Sdougm  * is_zfs_group(group)
6616185db85Sdougm  *	Determine if the specified group is a ZFS sharenfs group
6626185db85Sdougm  */
6636185db85Sdougm static int
is_zfs_group(sa_group_t group)6646185db85Sdougm is_zfs_group(sa_group_t group)
6656185db85Sdougm {
6666185db85Sdougm 	int ret = 0;
6676185db85Sdougm 	xmlNodePtr parent;
6686185db85Sdougm 	xmlChar *zfs;
6696185db85Sdougm 
67057b448deSdougm 	if (strcmp((char *)((xmlNodePtr)group)->name, "share") == 0)
6716185db85Sdougm 		parent = (xmlNodePtr)sa_get_parent_group(group);
67257b448deSdougm 	else
6736185db85Sdougm 		parent = (xmlNodePtr)group;
6746185db85Sdougm 	zfs = xmlGetProp(parent, (xmlChar *)"zfs");
6756185db85Sdougm 	if (zfs != NULL) {
6766185db85Sdougm 		xmlFree(zfs);
6776185db85Sdougm 		ret = 1;
6786185db85Sdougm 	}
6796185db85Sdougm 	return (ret);
6806185db85Sdougm }
6816185db85Sdougm 
6826185db85Sdougm /*
683da6c28aaSamw  * sa_get_object_type(object)
684da6c28aaSamw  *
685da6c28aaSamw  * This function returns a numeric value representing the object
686da6c28aaSamw  * type. This allows using simpler checks when doing type specific
687da6c28aaSamw  * operations.
688da6c28aaSamw  */
689da6c28aaSamw 
690da6c28aaSamw static int
sa_get_object_type(void * object)691da6c28aaSamw sa_get_object_type(void *object)
692da6c28aaSamw {
693da6c28aaSamw 	xmlNodePtr node = (xmlNodePtr)object;
694da6c28aaSamw 	int type;
695da6c28aaSamw 
696da6c28aaSamw 	if (xmlStrcmp(node->name, (xmlChar *)"group") == 0)
697da6c28aaSamw 		type = SA_TYPE_GROUP;
698da6c28aaSamw 	else if (xmlStrcmp(node->name, (xmlChar *)"share") == 0)
699da6c28aaSamw 		type = SA_TYPE_SHARE;
700da6c28aaSamw 	else if (xmlStrcmp(node->name, (xmlChar *)"resource") == 0)
701da6c28aaSamw 		type = SA_TYPE_RESOURCE;
702da6c28aaSamw 	else if (xmlStrcmp(node->name, (xmlChar *)"optionset") == 0)
703da6c28aaSamw 		type = SA_TYPE_OPTIONSET;
704da6c28aaSamw 	else if (xmlStrcmp(node->name, (xmlChar *)"security") == 0)
705da6c28aaSamw 		type = SA_TYPE_ALTSPACE;
706da6c28aaSamw 	else
707da6c28aaSamw 		assert(0);
708da6c28aaSamw 	return (type);
709da6c28aaSamw }
710da6c28aaSamw 
711da6c28aaSamw /*
7126185db85Sdougm  * sa_optionset_name(optionset, oname, len, id)
7136185db85Sdougm  *	return the SMF name for the optionset. If id is not NULL, it
7146185db85Sdougm  *	will have the GUID value for a share and should be used
7156185db85Sdougm  *	instead of the keyword "optionset" which is used for
7166185db85Sdougm  *	groups. If the optionset doesn't have a protocol type
7176185db85Sdougm  *	associated with it, "default" is used. This shouldn't happen
7186185db85Sdougm  *	at this point but may be desirable in the future if there are
7196185db85Sdougm  *	protocol independent properties added. The name is returned in
7206185db85Sdougm  *	oname.
7216185db85Sdougm  */
7226185db85Sdougm 
7236185db85Sdougm static int
sa_optionset_name(sa_optionset_t optionset,char * oname,size_t len,char * id)7246185db85Sdougm sa_optionset_name(sa_optionset_t optionset, char *oname, size_t len, char *id)
7256185db85Sdougm {
7266185db85Sdougm 	char *proto;
727da6c28aaSamw 	void *parent;
728da6c28aaSamw 	int ptype;
7296185db85Sdougm 
7306185db85Sdougm 	if (id == NULL)
7316185db85Sdougm 		id = "optionset";
7326185db85Sdougm 
733da6c28aaSamw 	parent = sa_get_optionset_parent(optionset);
734da6c28aaSamw 	if (parent != NULL) {
735da6c28aaSamw 		ptype = sa_get_object_type(parent);
7366185db85Sdougm 		proto = sa_get_optionset_attr(optionset, "type");
737da6c28aaSamw 		if (ptype != SA_TYPE_RESOURCE) {
738da6c28aaSamw 			len = snprintf(oname, len, "%s_%s", id,
739da6c28aaSamw 			    proto ? proto : "default");
740da6c28aaSamw 		} else {
741da6c28aaSamw 			char *index;
742da6c28aaSamw 			index = get_node_attr((void *)parent, "id");
743fe1c642dSBill Krier 			if (index != NULL) {
744da6c28aaSamw 				len = snprintf(oname, len, "%s_%s_%s", id,
745da6c28aaSamw 				    proto ? proto : "default", index);
746fe1c642dSBill Krier 				sa_free_attr_string(index);
747fe1c642dSBill Krier 			} else {
748da6c28aaSamw 				len = 0;
749da6c28aaSamw 			}
750fe1c642dSBill Krier 		}
7516185db85Sdougm 
7526185db85Sdougm 		if (proto != NULL)
7536185db85Sdougm 			sa_free_attr_string(proto);
754da6c28aaSamw 	} else {
755da6c28aaSamw 		len = 0;
756da6c28aaSamw 	}
7576185db85Sdougm 	return (len);
7586185db85Sdougm }
7596185db85Sdougm 
7606185db85Sdougm /*
7616185db85Sdougm  * sa_security_name(optionset, oname, len, id)
7626185db85Sdougm  *
7636185db85Sdougm  * return the SMF name for the security. If id is not NULL, it will
7646185db85Sdougm  * have the GUID value for a share and should be used instead of the
7656185db85Sdougm  * keyword "optionset" which is used for groups. If the optionset
7666185db85Sdougm  * doesn't have a protocol type associated with it, "default" is
7676185db85Sdougm  * used. This shouldn't happen at this point but may be desirable in
7686185db85Sdougm  * the future if there are protocol independent properties added. The
7696185db85Sdougm  * name is returned in oname. The security type is also encoded into
7706185db85Sdougm  * the name. In the future, this wil *be handled a bit differently.
7716185db85Sdougm  */
7726185db85Sdougm 
7736185db85Sdougm static int
sa_security_name(sa_security_t security,char * oname,size_t len,char * id)7746185db85Sdougm sa_security_name(sa_security_t security, char *oname, size_t len, char *id)
7756185db85Sdougm {
7766185db85Sdougm 	char *proto;
7776185db85Sdougm 	char *sectype;
7786185db85Sdougm 
7796185db85Sdougm 	if (id == NULL)
7806185db85Sdougm 		id = "optionset";
7816185db85Sdougm 
7826185db85Sdougm 	proto = sa_get_security_attr(security, "type");
7836185db85Sdougm 	sectype = sa_get_security_attr(security, "sectype");
78457b448deSdougm 	len = snprintf(oname, len, "%s_%s_%s", id, proto ? proto : "default",
7856185db85Sdougm 	    sectype ? sectype : "default");
7866185db85Sdougm 	if (proto != NULL)
7876185db85Sdougm 		sa_free_attr_string(proto);
7886185db85Sdougm 	if (sectype != NULL)
7896185db85Sdougm 		sa_free_attr_string(sectype);
7906185db85Sdougm 	return (len);
7916185db85Sdougm }
7926185db85Sdougm 
7936185db85Sdougm /*
79457b448deSdougm  * verifydefgroupopts(handle)
79557b448deSdougm  *
79657b448deSdougm  * Make sure a "default" group exists and has default protocols enabled.
79757b448deSdougm  */
79857b448deSdougm static void
verifydefgroupopts(sa_handle_t handle)79957b448deSdougm verifydefgroupopts(sa_handle_t handle)
80057b448deSdougm {
80157b448deSdougm 	sa_group_t defgrp;
80257b448deSdougm 	sa_optionset_t opt;
803da6c28aaSamw 
80457b448deSdougm 	defgrp = sa_get_group(handle, "default");
80557b448deSdougm 	if (defgrp != NULL) {
80657b448deSdougm 		opt = sa_get_optionset(defgrp, NULL);
80757b448deSdougm 		/*
80857b448deSdougm 		 * NFS is the default for default group
80957b448deSdougm 		 */
81057b448deSdougm 		if (opt == NULL)
81157b448deSdougm 			opt = sa_create_optionset(defgrp, "nfs");
81257b448deSdougm 	}
81357b448deSdougm }
81457b448deSdougm 
81557b448deSdougm /*
816f345c0beSdougm  * sa_init(init_service)
8176185db85Sdougm  *	Initialize the API
8186185db85Sdougm  *	find all the shared objects
8196185db85Sdougm  *	init the tables with all objects
8206185db85Sdougm  *	read in the current configuration
8216185db85Sdougm  */
8226185db85Sdougm 
82357b448deSdougm #define	GETPROP(prop)	scf_simple_prop_next_astring(prop)
82457b448deSdougm #define	CHECKTSTAMP(st, tval)	stat(SA_LEGACY_DFSTAB, &st) >= 0 && \
82557b448deSdougm 	tval != TSTAMP(st.st_ctim)
82657b448deSdougm 
827549ec3ffSdougm sa_handle_t
sa_init(int init_service)8286185db85Sdougm sa_init(int init_service)
8296185db85Sdougm {
8306185db85Sdougm 	struct stat st;
8316185db85Sdougm 	int legacy = 0;
8326185db85Sdougm 	uint64_t tval = 0;
833a99982a7Sdougm 	int lockfd;
834a99982a7Sdougm 	sigset_t old;
835a99982a7Sdougm 	int updatelegacy = B_FALSE;
836a99982a7Sdougm 	scf_simple_prop_t *prop;
837549ec3ffSdougm 	sa_handle_impl_t handle;
838549ec3ffSdougm 	int err;
8396185db85Sdougm 
840549ec3ffSdougm 	handle = calloc(sizeof (struct sa_handle_impl), 1);
841549ec3ffSdougm 
842549ec3ffSdougm 	if (handle != NULL) {
843c5f58477Sdougm 		/*
844c5f58477Sdougm 		 * Get protocol specific structures, but only if this
845c5f58477Sdougm 		 * is the only handle.
846c5f58477Sdougm 		 */
847c5f58477Sdougm 		(void) mutex_lock(&sa_global_lock);
848c5f58477Sdougm 		if (sa_global_handles == NULL)
8496185db85Sdougm 			(void) proto_plugin_init();
850c5f58477Sdougm 		(void) mutex_unlock(&sa_global_lock);
8516185db85Sdougm 		if (init_service & SA_INIT_SHARE_API) {
8526185db85Sdougm 			/*
85357b448deSdougm 			 * initialize access into libzfs. We use this
85457b448deSdougm 			 * when collecting info about ZFS datasets and
85557b448deSdougm 			 * shares.
8561cea05afSdougm 			 */
85757b448deSdougm 			if (sa_zfs_init(handle) == B_FALSE) {
85857b448deSdougm 				free(handle);
85989dc44ceSjose borrego 				(void) mutex_lock(&sa_global_lock);
86057b448deSdougm 				(void) proto_plugin_fini();
86189dc44ceSjose borrego 				(void) mutex_unlock(&sa_global_lock);
86257b448deSdougm 				return (NULL);
86357b448deSdougm 			}
8641cea05afSdougm 			/*
8656185db85Sdougm 			 * since we want to use SMF, initialize an svc handle
8666185db85Sdougm 			 * and find out what is there.
8676185db85Sdougm 			 */
868549ec3ffSdougm 			handle->scfhandle = sa_scf_init(handle);
869549ec3ffSdougm 			if (handle->scfhandle != NULL) {
870a99982a7Sdougm 				/*
871a99982a7Sdougm 				 * Need to lock the extraction of the
872a99982a7Sdougm 				 * configuration if the dfstab file has
873a99982a7Sdougm 				 * changed. Lock everything now and release if
874a99982a7Sdougm 				 * not needed.  Use a file that isn't being
875a99982a7Sdougm 				 * manipulated by other parts of the system in
876a99982a7Sdougm 				 * order to not interfere with locking. Using
877a99982a7Sdougm 				 * dfstab doesn't work.
878a99982a7Sdougm 				 */
879a99982a7Sdougm 				sablocksigs(&old);
880a99982a7Sdougm 				lockfd = open(DFS_LOCK_FILE, O_RDWR);
881a99982a7Sdougm 				if (lockfd >= 0) {
882a99982a7Sdougm 					extern int errno;
883a99982a7Sdougm 					errno = 0;
884a99982a7Sdougm 					(void) lockf(lockfd, F_LOCK, 0);
885148c5f43SAlan Wright 					(void) mutex_lock(&sa_dfstab_lock);
886a99982a7Sdougm 					/*
88757b448deSdougm 					 * Check whether we are going to need
88857b448deSdougm 					 * to merge any dfstab changes. This
88957b448deSdougm 					 * is done by comparing the value of
89057b448deSdougm 					 * legacy-timestamp with the current
89157b448deSdougm 					 * st_ctim of the file. If they are
89257b448deSdougm 					 * different, an update is needed and
89357b448deSdougm 					 * the file must remain locked until
89457b448deSdougm 					 * the merge is done in order to
89557b448deSdougm 					 * prevent multiple startups from
89657b448deSdougm 					 * changing the SMF repository at the
89757b448deSdougm 					 * same time.  The first to get the
89857b448deSdougm 					 * lock will make any changes before
89957b448deSdougm 					 * the others can read the repository.
900a99982a7Sdougm 					 */
90157b448deSdougm 					prop = scf_simple_prop_get
90257b448deSdougm 					    (handle->scfhandle->handle,
90357b448deSdougm 					    (const char *)SA_SVC_FMRI_BASE
90457b448deSdougm 					    ":default", "operation",
905a99982a7Sdougm 					    "legacy-timestamp");
906a99982a7Sdougm 					if (prop != NULL) {
907a99982a7Sdougm 						char *i64;
90857b448deSdougm 						i64 = GETPROP(prop);
90957b448deSdougm 						if (i64 != NULL)
91057b448deSdougm 							tval = strtoull(i64,
91157b448deSdougm 							    NULL, 0);
91257b448deSdougm 						if (CHECKTSTAMP(st, tval))
913a99982a7Sdougm 							updatelegacy = B_TRUE;
91457b448deSdougm 						scf_simple_prop_free(prop);
915a99982a7Sdougm 					} else {
91657b448deSdougm 						/*
91757b448deSdougm 						 * We haven't set the
91857b448deSdougm 						 * timestamp before so do it.
91957b448deSdougm 						 */
920a99982a7Sdougm 						updatelegacy = B_TRUE;
921a99982a7Sdougm 					}
922a99982a7Sdougm 					if (updatelegacy == B_FALSE) {
923148c5f43SAlan Wright 						(void) mutex_unlock(
924148c5f43SAlan Wright 						    &sa_dfstab_lock);
925148c5f43SAlan Wright 						(void) lockf(lockfd, F_ULOCK,
926148c5f43SAlan Wright 						    0);
927a99982a7Sdougm 						(void) close(lockfd);
928a99982a7Sdougm 					}
9291d1813a7Sdougm 
930148c5f43SAlan Wright 				}
9311d1813a7Sdougm 				/*
9321d1813a7Sdougm 				 * It is essential that the document tree and
9331d1813a7Sdougm 				 * the internal list of roots to handles be
9341d1813a7Sdougm 				 * setup before anything that might try to
9351d1813a7Sdougm 				 * create a new object is called. The document
9361d1813a7Sdougm 				 * tree is the combination of handle->doc and
9371d1813a7Sdougm 				 * handle->tree. This allows searches,
9381d1813a7Sdougm 				 * etc. when all you have is an object in the
9391d1813a7Sdougm 				 * tree.
9401d1813a7Sdougm 				 */
9411d1813a7Sdougm 				handle->doc = xmlNewDoc((xmlChar *)"1.0");
94257b448deSdougm 				handle->tree = xmlNewNode(NULL,
94357b448deSdougm 				    (xmlChar *)"sharecfg");
94457b448deSdougm 				if (handle->doc != NULL &&
94557b448deSdougm 				    handle->tree != NULL) {
9464bff34e3Sthurlow 					(void) xmlDocSetRootElement(handle->doc,
94757b448deSdougm 					    handle->tree);
94857b448deSdougm 					err = add_handle_for_root(handle->tree,
94957b448deSdougm 					    handle);
9501d1813a7Sdougm 					if (err == SA_OK)
95157b448deSdougm 						err = sa_get_config(
95257b448deSdougm 						    handle->scfhandle,
9531d1813a7Sdougm 						    handle->tree, handle);
9541d1813a7Sdougm 				} else {
9551d1813a7Sdougm 					if (handle->doc != NULL)
9561d1813a7Sdougm 						xmlFreeDoc(handle->doc);
9571d1813a7Sdougm 					if (handle->tree != NULL)
9581d1813a7Sdougm 						xmlFreeNode(handle->tree);
9591d1813a7Sdougm 					err = SA_NO_MEMORY;
9601d1813a7Sdougm 				}
9611d1813a7Sdougm 
962a99982a7Sdougm 				saunblocksigs(&old);
963549ec3ffSdougm 
964549ec3ffSdougm 				if (err != SA_OK) {
965549ec3ffSdougm 					/*
9661d1813a7Sdougm 					 * If we couldn't add the tree handle
9671d1813a7Sdougm 					 * to the list, then things are going
9681d1813a7Sdougm 					 * to fail badly. Might as well undo
9691d1813a7Sdougm 					 * everything now and fail the
9701d1813a7Sdougm 					 * sa_init().
971549ec3ffSdougm 					 */
972549ec3ffSdougm 					sa_fini(handle);
973148c5f43SAlan Wright 					if (updatelegacy == B_TRUE) {
974148c5f43SAlan Wright 						(void) mutex_unlock(
975148c5f43SAlan Wright 						    &sa_dfstab_lock);
976148c5f43SAlan Wright 						(void) lockf(lockfd,
977148c5f43SAlan Wright 						    F_ULOCK, 0);
978148c5f43SAlan Wright 						(void) close(lockfd);
979148c5f43SAlan Wright 					}
980549ec3ffSdougm 					return (NULL);
981549ec3ffSdougm 				}
982549ec3ffSdougm 
9836185db85Sdougm 				if (tval == 0) {
98457b448deSdougm 					/*
98557b448deSdougm 					 * first time so make sure
98657b448deSdougm 					 * default is setup
98757b448deSdougm 					 */
98857b448deSdougm 					verifydefgroupopts(handle);
9896185db85Sdougm 				}
9901d1813a7Sdougm 
991a99982a7Sdougm 				if (updatelegacy == B_TRUE) {
992a99982a7Sdougm 					sablocksigs(&old);
993549ec3ffSdougm 					getlegacyconfig((sa_handle_t)handle,
994549ec3ffSdougm 					    SA_LEGACY_DFSTAB, &handle->tree);
9956185db85Sdougm 					if (stat(SA_LEGACY_DFSTAB, &st) >= 0)
996546405c3Sdougm 						set_legacy_timestamp(
997546405c3Sdougm 						    handle->tree,
9986185db85Sdougm 						    SA_LEGACY_DFSTAB,
9996185db85Sdougm 						    TSTAMP(st.st_ctim));
1000a99982a7Sdougm 					saunblocksigs(&old);
1001546405c3Sdougm 					/*
1002546405c3Sdougm 					 * Safe to unlock now to allow
1003546405c3Sdougm 					 * others to run
1004546405c3Sdougm 					 */
1005148c5f43SAlan Wright 					(void) mutex_unlock(&sa_dfstab_lock);
1006a99982a7Sdougm 					(void) lockf(lockfd, F_ULOCK, 0);
1007a99982a7Sdougm 					(void) close(lockfd);
10086185db85Sdougm 				}
10095b6e0c46Sdougm 				/* Get sharetab timestamp */
10105b6e0c46Sdougm 				sa_update_sharetab_ts((sa_handle_t)handle);
10115b6e0c46Sdougm 
10125b6e0c46Sdougm 				/* Get lastupdate (transaction) timestamp */
10135b6e0c46Sdougm 				prop = scf_simple_prop_get(
10145b6e0c46Sdougm 				    handle->scfhandle->handle,
10155b6e0c46Sdougm 				    (const char *)SA_SVC_FMRI_BASE ":default",
10165b6e0c46Sdougm 				    "state", "lastupdate");
10175b6e0c46Sdougm 				if (prop != NULL) {
10185b6e0c46Sdougm 					char *str;
10195b6e0c46Sdougm 					str =
10205b6e0c46Sdougm 					    scf_simple_prop_next_astring(prop);
10215b6e0c46Sdougm 					if (str != NULL)
10225b6e0c46Sdougm 						handle->tstrans =
10235b6e0c46Sdougm 						    strtoull(str, NULL, 0);
10245b6e0c46Sdougm 					else
10255b6e0c46Sdougm 						handle->tstrans = 0;
10265b6e0c46Sdougm 					scf_simple_prop_free(prop);
10275b6e0c46Sdougm 				}
1028549ec3ffSdougm 				legacy |= sa_get_zfs_shares(handle, "zfs");
1029549ec3ffSdougm 				legacy |= gettransients(handle, &handle->tree);
10306185db85Sdougm 			}
10316185db85Sdougm 		}
10326185db85Sdougm 	}
1033549ec3ffSdougm 	return ((sa_handle_t)handle);
10346185db85Sdougm }
10356185db85Sdougm 
10366185db85Sdougm /*
1037549ec3ffSdougm  * sa_fini(handle)
10386185db85Sdougm  *	Uninitialize the API structures including the configuration
10391cea05afSdougm  *	data structures and ZFS related data.
10406185db85Sdougm  */
10416185db85Sdougm 
10426185db85Sdougm void
sa_fini(sa_handle_t handle)1043549ec3ffSdougm sa_fini(sa_handle_t handle)
10446185db85Sdougm {
1045549ec3ffSdougm 	sa_handle_impl_t impl_handle = (sa_handle_impl_t)handle;
1046549ec3ffSdougm 
1047549ec3ffSdougm 	if (impl_handle != NULL) {
1048549ec3ffSdougm 		/*
1049549ec3ffSdougm 		 * Free the config trees and any other data structures
1050549ec3ffSdougm 		 * used in the handle.
1051549ec3ffSdougm 		 */
1052549ec3ffSdougm 		if (impl_handle->doc != NULL)
1053549ec3ffSdougm 			xmlFreeDoc(impl_handle->doc);
1054549ec3ffSdougm 
1055549ec3ffSdougm 		/* Remove and free the entry in the global list. */
1056549ec3ffSdougm 		remove_handle_for_root(impl_handle->tree);
1057549ec3ffSdougm 
1058549ec3ffSdougm 		/*
1059549ec3ffSdougm 		 * If this was the last handle to release, unload the
1060c5f58477Sdougm 		 * plugins that were loaded. Use a mutex in case
1061c5f58477Sdougm 		 * another thread is reinitializing.
1062549ec3ffSdougm 		 */
1063c5f58477Sdougm 		(void) mutex_lock(&sa_global_lock);
1064549ec3ffSdougm 		if (sa_global_handles == NULL)
1065549ec3ffSdougm 			(void) proto_plugin_fini();
1066c5f58477Sdougm 		(void) mutex_unlock(&sa_global_lock);
1067549ec3ffSdougm 
10680fd77660Sgwr 		sa_scf_fini(impl_handle->scfhandle);
10690fd77660Sgwr 		sa_zfs_fini(impl_handle);
10700fd77660Sgwr 
10710fd77660Sgwr 		/* Make sure we free the handle */
10720fd77660Sgwr 		free(impl_handle);
10730fd77660Sgwr 
10746185db85Sdougm 	}
10756185db85Sdougm }
10766185db85Sdougm 
10776185db85Sdougm /*
10786185db85Sdougm  * sa_get_protocols(char **protocol)
10796185db85Sdougm  *	Get array of protocols that are supported
10806185db85Sdougm  *	Returns pointer to an allocated and NULL terminated
10816185db85Sdougm  *	array of strings.  Caller must free.
10826185db85Sdougm  *	This really should be determined dynamically.
10836185db85Sdougm  *	If there aren't any defined, return -1.
10846185db85Sdougm  *	Use free() to return memory.
10856185db85Sdougm  */
10866185db85Sdougm 
10876185db85Sdougm int
sa_get_protocols(char *** protocols)10886185db85Sdougm sa_get_protocols(char ***protocols)
10896185db85Sdougm {
10906185db85Sdougm 	int numproto = -1;
10916185db85Sdougm 
10926185db85Sdougm 	if (protocols != NULL) {
10936185db85Sdougm 		struct sa_proto_plugin *plug;
10946185db85Sdougm 		for (numproto = 0, plug = sap_proto_list; plug != NULL;
10956185db85Sdougm 		    plug = plug->plugin_next) {
10966185db85Sdougm 			numproto++;
10976185db85Sdougm 		}
10986185db85Sdougm 
10996185db85Sdougm 		*protocols = calloc(numproto + 1,  sizeof (char *));
11006185db85Sdougm 		if (*protocols != NULL) {
11016185db85Sdougm 			int ret = 0;
11026185db85Sdougm 			for (plug = sap_proto_list; plug != NULL;
11036185db85Sdougm 			    plug = plug->plugin_next) {
11046185db85Sdougm 				/* faking for now */
110557b448deSdougm 				(*protocols)[ret++] =
110657b448deSdougm 				    plug->plugin_ops->sa_protocol;
11076185db85Sdougm 			}
11086185db85Sdougm 		} else {
11096185db85Sdougm 			numproto = -1;
11106185db85Sdougm 		}
11116185db85Sdougm 	}
11126185db85Sdougm 	return (numproto);
11136185db85Sdougm }
11146185db85Sdougm 
11156185db85Sdougm /*
11166185db85Sdougm  * find_group_by_name(node, group)
11176185db85Sdougm  *
11186185db85Sdougm  * search the XML document subtree specified by node to find the group
11196185db85Sdougm  * specified by group. Searching subtree allows subgroups to be
11206185db85Sdougm  * searched for.
11216185db85Sdougm  */
11226185db85Sdougm 
11236185db85Sdougm static xmlNodePtr
find_group_by_name(xmlNodePtr node,xmlChar * group)11246185db85Sdougm find_group_by_name(xmlNodePtr node, xmlChar *group)
11256185db85Sdougm {
11266185db85Sdougm 	xmlChar *name = NULL;
11276185db85Sdougm 
11286185db85Sdougm 	for (node = node->xmlChildrenNode; node != NULL;
11296185db85Sdougm 	    node = node->next) {
11306185db85Sdougm 		if (xmlStrcmp(node->name, (xmlChar *)"group") == 0) {
11316185db85Sdougm 			/* if no groupname, return the first found */
11326185db85Sdougm 			if (group == NULL)
11336185db85Sdougm 				break;
11346185db85Sdougm 			name = xmlGetProp(node, (xmlChar *)"name");
113557b448deSdougm 			if (name != NULL && xmlStrcmp(name, group) == 0)
11366185db85Sdougm 				break;
11376185db85Sdougm 			if (name != NULL) {
11386185db85Sdougm 				xmlFree(name);
11396185db85Sdougm 				name = NULL;
11406185db85Sdougm 			}
11416185db85Sdougm 		}
11426185db85Sdougm 	}
11436185db85Sdougm 	if (name != NULL)
11446185db85Sdougm 		xmlFree(name);
11456185db85Sdougm 	return (node);
11466185db85Sdougm }
11476185db85Sdougm 
11486185db85Sdougm /*
11496185db85Sdougm  * sa_get_group(groupname)
11506185db85Sdougm  *	Return the "group" specified.  If groupname is NULL,
11516185db85Sdougm  *	return the first group of the list of groups.
11526185db85Sdougm  */
11536185db85Sdougm sa_group_t
sa_get_group(sa_handle_t handle,char * groupname)1154549ec3ffSdougm sa_get_group(sa_handle_t handle, char *groupname)
11556185db85Sdougm {
11566185db85Sdougm 	xmlNodePtr node = NULL;
11576185db85Sdougm 	char *subgroup = NULL;
11586185db85Sdougm 	char *group = NULL;
1159549ec3ffSdougm 	sa_handle_impl_t impl_handle = (sa_handle_impl_t)handle;
11606185db85Sdougm 
1161549ec3ffSdougm 	if (impl_handle != NULL && impl_handle->tree != NULL) {
11626185db85Sdougm 		if (groupname != NULL) {
11636185db85Sdougm 			group = strdup(groupname);
1164a3351425Sdougm 			if (group != NULL) {
11656185db85Sdougm 				subgroup = strchr(group, '/');
11666185db85Sdougm 				if (subgroup != NULL)
11676185db85Sdougm 					*subgroup++ = '\0';
11686185db85Sdougm 			}
1169a3351425Sdougm 		}
1170a3351425Sdougm 		/*
1171a3351425Sdougm 		 * We want to find the, possibly, named group. If
1172a3351425Sdougm 		 * group is not NULL, then lookup the name. If it is
1173a3351425Sdougm 		 * NULL, we only do the find if groupname is also
1174a3351425Sdougm 		 * NULL. This allows lookup of the "first" group in
1175a3351425Sdougm 		 * the internal list.
1176a3351425Sdougm 		 */
1177a3351425Sdougm 		if (group != NULL || groupname == NULL)
1178a3351425Sdougm 			node = find_group_by_name(impl_handle->tree,
1179a3351425Sdougm 			    (xmlChar *)group);
1180a3351425Sdougm 
11816185db85Sdougm 		/* if a subgroup, find it before returning */
118257b448deSdougm 		if (subgroup != NULL && node != NULL)
11836185db85Sdougm 			node = find_group_by_name(node, (xmlChar *)subgroup);
11846185db85Sdougm 	}
11856185db85Sdougm 	if (node != NULL && (char *)group != NULL)
1186549ec3ffSdougm 		(void) sa_get_instance(impl_handle->scfhandle, (char *)group);
11876185db85Sdougm 	if (group != NULL)
11886185db85Sdougm 		free(group);
11896185db85Sdougm 	return ((sa_group_t)(node));
11906185db85Sdougm }
11916185db85Sdougm 
11926185db85Sdougm /*
11936185db85Sdougm  * sa_get_next_group(group)
11946185db85Sdougm  *	Return the "next" group after the specified group from
11956185db85Sdougm  *	the internal group list.  NULL if there are no more.
11966185db85Sdougm  */
11976185db85Sdougm sa_group_t
sa_get_next_group(sa_group_t group)11986185db85Sdougm sa_get_next_group(sa_group_t group)
11996185db85Sdougm {
12006185db85Sdougm 	xmlNodePtr ngroup = NULL;
12016185db85Sdougm 	if (group != NULL) {
12026185db85Sdougm 		for (ngroup = ((xmlNodePtr)group)->next; ngroup != NULL;
12036185db85Sdougm 		    ngroup = ngroup->next) {
12046185db85Sdougm 			if (xmlStrcmp(ngroup->name, (xmlChar *)"group") == 0)
12056185db85Sdougm 				break;
12066185db85Sdougm 		}
12076185db85Sdougm 	}
12086185db85Sdougm 	return ((sa_group_t)ngroup);
12096185db85Sdougm }
12106185db85Sdougm 
12116185db85Sdougm /*
12126185db85Sdougm  * sa_get_share(group, sharepath)
12136185db85Sdougm  *	Return the share object for the share specified. The share
12146185db85Sdougm  *	must be in the specified group.  Return NULL if not found.
12156185db85Sdougm  */
12166185db85Sdougm sa_share_t
sa_get_share(sa_group_t group,char * sharepath)12176185db85Sdougm sa_get_share(sa_group_t group, char *sharepath)
12186185db85Sdougm {
12196185db85Sdougm 	xmlNodePtr node = NULL;
12206185db85Sdougm 	xmlChar *path;
12216185db85Sdougm 
12226185db85Sdougm 	/*
12236185db85Sdougm 	 * For future scalability, this should end up building a cache
12246185db85Sdougm 	 * since it will get called regularly by the mountd and info
12256185db85Sdougm 	 * services.
12266185db85Sdougm 	 */
12276185db85Sdougm 	if (group != NULL) {
12286185db85Sdougm 		for (node = ((xmlNodePtr)group)->children; node != NULL;
12296185db85Sdougm 		    node = node->next) {
12306185db85Sdougm 			if (xmlStrcmp(node->name, (xmlChar *)"share") == 0) {
12316185db85Sdougm 				if (sharepath == NULL) {
12326185db85Sdougm 					break;
12336185db85Sdougm 				} else {
12346185db85Sdougm 					/* is it the correct share? */
123557b448deSdougm 					path = xmlGetProp(node,
123657b448deSdougm 					    (xmlChar *)"path");
12376185db85Sdougm 					if (path != NULL &&
123857b448deSdougm 					    xmlStrcmp(path,
123957b448deSdougm 					    (xmlChar *)sharepath) == 0) {
12406185db85Sdougm 						xmlFree(path);
12416185db85Sdougm 						break;
12426185db85Sdougm 					}
12436185db85Sdougm 					xmlFree(path);
12446185db85Sdougm 				}
12456185db85Sdougm 			}
12466185db85Sdougm 		}
12476185db85Sdougm 	}
12486185db85Sdougm 	return ((sa_share_t)node);
12496185db85Sdougm }
12506185db85Sdougm 
12516185db85Sdougm /*
12526185db85Sdougm  * sa_get_next_share(share)
12536185db85Sdougm  *	Return the next share following the specified share
12546185db85Sdougm  *	from the internal list of shares. Returns NULL if there
12556185db85Sdougm  *	are no more shares.  The list is relative to the same
12566185db85Sdougm  *	group.
12576185db85Sdougm  */
12586185db85Sdougm sa_share_t
sa_get_next_share(sa_share_t share)12596185db85Sdougm sa_get_next_share(sa_share_t share)
12606185db85Sdougm {
12616185db85Sdougm 	xmlNodePtr node = NULL;
12626185db85Sdougm 
12636185db85Sdougm 	if (share != NULL) {
12646185db85Sdougm 		for (node = ((xmlNodePtr)share)->next; node != NULL;
12656185db85Sdougm 		    node = node->next) {
12666185db85Sdougm 			if (xmlStrcmp(node->name, (xmlChar *)"share") == 0) {
12676185db85Sdougm 				break;
12686185db85Sdougm 			}
12696185db85Sdougm 		}
12706185db85Sdougm 	}
12716185db85Sdougm 	return ((sa_share_t)node);
12726185db85Sdougm }
12736185db85Sdougm 
12746185db85Sdougm /*
12756185db85Sdougm  * _sa_get_child_node(node, type)
12766185db85Sdougm  *
12776185db85Sdougm  * find the child node of the specified node that has "type". This is
12786185db85Sdougm  * used to implement several internal functions.
12796185db85Sdougm  */
12806185db85Sdougm 
12816185db85Sdougm static xmlNodePtr
_sa_get_child_node(xmlNodePtr node,xmlChar * type)12826185db85Sdougm _sa_get_child_node(xmlNodePtr node, xmlChar *type)
12836185db85Sdougm {
12846185db85Sdougm 	xmlNodePtr child;
12856185db85Sdougm 	for (child = node->xmlChildrenNode; child != NULL;
12866185db85Sdougm 	    child = child->next)
12876185db85Sdougm 		if (xmlStrcmp(child->name, type) == 0)
12886185db85Sdougm 			return (child);
12896185db85Sdougm 	return ((xmlNodePtr)NULL);
12906185db85Sdougm }
12916185db85Sdougm 
12926185db85Sdougm /*
12936185db85Sdougm  *  find_share(group, path)
12946185db85Sdougm  *
12956185db85Sdougm  * Search all the shares in the specified group for one that has the
12966185db85Sdougm  * specified path.
12976185db85Sdougm  */
12986185db85Sdougm 
12996185db85Sdougm static sa_share_t
find_share(sa_group_t group,char * sharepath)13006185db85Sdougm find_share(sa_group_t group, char *sharepath)
13016185db85Sdougm {
13026185db85Sdougm 	sa_share_t share;
13036185db85Sdougm 	char *path;
13046185db85Sdougm 
13056185db85Sdougm 	for (share = sa_get_share(group, NULL); share != NULL;
13066185db85Sdougm 	    share = sa_get_next_share(share)) {
13076185db85Sdougm 		path = sa_get_share_attr(share, "path");
13086185db85Sdougm 		if (path != NULL && strcmp(path, sharepath) == 0) {
13096185db85Sdougm 			sa_free_attr_string(path);
13106185db85Sdougm 			break;
13116185db85Sdougm 		}
13126185db85Sdougm 		if (path != NULL)
13136185db85Sdougm 			sa_free_attr_string(path);
13146185db85Sdougm 	}
13156185db85Sdougm 	return (share);
13166185db85Sdougm }
13176185db85Sdougm 
13186185db85Sdougm /*
13196185db85Sdougm  * sa_get_sub_group(group)
13206185db85Sdougm  *
13216185db85Sdougm  * Get the first sub-group of group. The sa_get_next_group() function
13226185db85Sdougm  * can be used to get the rest. This is currently only used for ZFS
13236185db85Sdougm  * sub-groups but could be used to implement a more general mechanism.
13246185db85Sdougm  */
13256185db85Sdougm 
13266185db85Sdougm sa_group_t
sa_get_sub_group(sa_group_t group)13276185db85Sdougm sa_get_sub_group(sa_group_t group)
13286185db85Sdougm {
13296185db85Sdougm 	return ((sa_group_t)_sa_get_child_node((xmlNodePtr)group,
13306185db85Sdougm 	    (xmlChar *)"group"));
13316185db85Sdougm }
13326185db85Sdougm 
13336185db85Sdougm /*
13346185db85Sdougm  * sa_find_share(sharepath)
13356185db85Sdougm  *	Finds a share regardless of group.  In the future, this
13366185db85Sdougm  *	function should utilize a cache and hash table of some kind.
13376185db85Sdougm  *	The current assumption is that a path will only be shared
13386185db85Sdougm  *	once.  In the future, this may change as implementation of
13396185db85Sdougm  *	resource names comes into being.
13406185db85Sdougm  */
13416185db85Sdougm sa_share_t
sa_find_share(sa_handle_t handle,char * sharepath)1342549ec3ffSdougm sa_find_share(sa_handle_t handle, char *sharepath)
13436185db85Sdougm {
13446185db85Sdougm 	sa_group_t group;
13456185db85Sdougm 	sa_group_t zgroup;
13466185db85Sdougm 	sa_share_t share = NULL;
13476185db85Sdougm 	int done = 0;
13486185db85Sdougm 
1349549ec3ffSdougm 	for (group = sa_get_group(handle, NULL); group != NULL && !done;
13506185db85Sdougm 	    group = sa_get_next_group(group)) {
13516185db85Sdougm 		if (is_zfs_group(group)) {
135257b448deSdougm 			for (zgroup =
135357b448deSdougm 			    (sa_group_t)_sa_get_child_node((xmlNodePtr)group,
13546185db85Sdougm 			    (xmlChar *)"group");
135557b448deSdougm 			    zgroup != NULL;
135657b448deSdougm 			    zgroup = sa_get_next_group(zgroup)) {
13576185db85Sdougm 				share = find_share(zgroup, sharepath);
13586185db85Sdougm 				if (share != NULL)
13596185db85Sdougm 					break;
13606185db85Sdougm 			}
13616185db85Sdougm 		} else {
13626185db85Sdougm 			share = find_share(group, sharepath);
13636185db85Sdougm 		}
13646185db85Sdougm 		if (share != NULL)
13656185db85Sdougm 			break;
13666185db85Sdougm 	}
13676185db85Sdougm 	return (share);
13686185db85Sdougm }
13696185db85Sdougm 
13706185db85Sdougm /*
1371f345c0beSdougm  *  sa_check_path(group, path, strictness)
13726185db85Sdougm  *
1373da6c28aaSamw  * Check that path is a valid path relative to the group.  Currently,
13746185db85Sdougm  * we are ignoring the group and checking only the NFS rules. Later,
13756185db85Sdougm  * we may want to use the group to then check against the protocols
1376f345c0beSdougm  * enabled on the group. The strictness values mean:
1377f345c0beSdougm  * SA_CHECK_NORMAL == only check newpath against shares that are active
1378f345c0beSdougm  * SA_CHECK_STRICT == check newpath against both active shares and those
1379f345c0beSdougm  *		      stored in the repository
13806185db85Sdougm  */
13816185db85Sdougm 
13826185db85Sdougm int
sa_check_path(sa_group_t group,char * path,int strictness)1383f345c0beSdougm sa_check_path(sa_group_t group, char *path, int strictness)
13846185db85Sdougm {
1385549ec3ffSdougm 	sa_handle_t handle;
1386549ec3ffSdougm 
1387549ec3ffSdougm 	handle = sa_find_group_handle(group);
13889fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	if (handle == NULL)
13899fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 		return (SA_BAD_PATH);
13909fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 
1391549ec3ffSdougm 	return (validpath(handle, path, strictness));
13926185db85Sdougm }
13936185db85Sdougm 
13946185db85Sdougm /*
1395da6c28aaSamw  * mark_excluded_protos(group, share, flags)
13966185db85Sdougm  *
1397da6c28aaSamw  * Walk through all the protocols enabled for the group and check to
1398da6c28aaSamw  * see if the share has any of them should be in the exclude list
1399da6c28aaSamw  * based on the featureset of the protocol. If there are any, add the
1400da6c28aaSamw  * "exclude" property to the share.
1401da6c28aaSamw  */
1402da6c28aaSamw static void
mark_excluded_protos(sa_group_t group,xmlNodePtr share,uint64_t flags)1403da6c28aaSamw mark_excluded_protos(sa_group_t group, xmlNodePtr share, uint64_t flags)
1404da6c28aaSamw {
1405da6c28aaSamw 	sa_optionset_t optionset;
1406da6c28aaSamw 	char exclude_list[SA_STRSIZE];
1407da6c28aaSamw 	char *sep = "";
1408da6c28aaSamw 
1409da6c28aaSamw 	exclude_list[0] = '\0';
1410da6c28aaSamw 	for (optionset = sa_get_optionset(group, NULL);
1411da6c28aaSamw 	    optionset != NULL;
1412da6c28aaSamw 	    optionset = sa_get_next_optionset(optionset)) {
1413da6c28aaSamw 		char *value;
1414da6c28aaSamw 		uint64_t features;
1415da6c28aaSamw 		value = sa_get_optionset_attr(optionset, "type");
1416da6c28aaSamw 		if (value == NULL)
1417da6c28aaSamw 			continue;
1418da6c28aaSamw 		features = sa_proto_get_featureset(value);
1419da6c28aaSamw 		if (!(features & flags)) {
1420da6c28aaSamw 			(void) strlcat(exclude_list, sep,
1421da6c28aaSamw 			    sizeof (exclude_list));
1422da6c28aaSamw 			(void) strlcat(exclude_list, value,
1423da6c28aaSamw 			    sizeof (exclude_list));
1424da6c28aaSamw 			sep = ",";
1425da6c28aaSamw 		}
1426fe1c642dSBill Krier 		sa_free_attr_string(value);
1427da6c28aaSamw 	}
1428da6c28aaSamw 	if (exclude_list[0] != '\0')
14294bff34e3Sthurlow 		(void) xmlSetProp(share, (xmlChar *)"exclude",
1430da6c28aaSamw 		    (xmlChar *)exclude_list);
1431da6c28aaSamw }
1432da6c28aaSamw 
1433da6c28aaSamw /*
1434da6c28aaSamw  * get_all_features(group)
1435da6c28aaSamw  *
1436da6c28aaSamw  * Walk through all the protocols on the group and collect all
1437da6c28aaSamw  * possible enabled features. This is the OR of all the featuresets.
1438da6c28aaSamw  */
1439da6c28aaSamw static uint64_t
get_all_features(sa_group_t group)1440da6c28aaSamw get_all_features(sa_group_t group)
1441da6c28aaSamw {
1442da6c28aaSamw 	sa_optionset_t optionset;
1443da6c28aaSamw 	uint64_t features = 0;
1444da6c28aaSamw 
1445da6c28aaSamw 	for (optionset = sa_get_optionset(group, NULL);
1446da6c28aaSamw 	    optionset != NULL;
1447da6c28aaSamw 	    optionset = sa_get_next_optionset(optionset)) {
1448da6c28aaSamw 		char *value;
1449da6c28aaSamw 		value = sa_get_optionset_attr(optionset, "type");
1450da6c28aaSamw 		if (value == NULL)
1451da6c28aaSamw 			continue;
1452da6c28aaSamw 		features |= sa_proto_get_featureset(value);
1453da6c28aaSamw 		sa_free_attr_string(value);
1454da6c28aaSamw 	}
1455da6c28aaSamw 	return (features);
1456da6c28aaSamw }
1457da6c28aaSamw 
1458da6c28aaSamw 
1459da6c28aaSamw /*
1460da6c28aaSamw  * _sa_add_share(group, sharepath, persist, *error, flags)
1461da6c28aaSamw  *
1462da6c28aaSamw  * Common code for all types of add_share. sa_add_share() is the
14636185db85Sdougm  * public API, we also need to be able to do this when parsing legacy
14646185db85Sdougm  * files and construction of the internal configuration while
1465da6c28aaSamw  * extracting config info from SMF. "flags" indicates if some
1466da6c28aaSamw  * protocols need relaxed rules while other don't. These values are
1467da6c28aaSamw  * the featureset values defined in libshare.h.
14686185db85Sdougm  */
14696185db85Sdougm 
14706185db85Sdougm sa_share_t
_sa_add_share(sa_group_t group,char * sharepath,int persist,int * error,uint64_t flags)1471da6c28aaSamw _sa_add_share(sa_group_t group, char *sharepath, int persist, int *error,
1472da6c28aaSamw     uint64_t flags)
14736185db85Sdougm {
14746185db85Sdougm 	xmlNodePtr node = NULL;
14756185db85Sdougm 	int err;
14766185db85Sdougm 
14776185db85Sdougm 	err  = SA_OK; /* assume success */
14786185db85Sdougm 
147957b448deSdougm 	node = xmlNewChild((xmlNodePtr)group, NULL, (xmlChar *)"share", NULL);
1480da6c28aaSamw 	if (node == NULL) {
1481da6c28aaSamw 		if (error != NULL)
1482da6c28aaSamw 			*error = SA_NO_MEMORY;
1483da6c28aaSamw 		return (node);
1484da6c28aaSamw 	}
1485da6c28aaSamw 
14864bff34e3Sthurlow 	(void) xmlSetProp(node, (xmlChar *)"path", (xmlChar *)sharepath);
14874bff34e3Sthurlow 	(void) xmlSetProp(node, (xmlChar *)"type",
148857b448deSdougm 	    persist ? (xmlChar *)"persist" : (xmlChar *)"transient");
1489da6c28aaSamw 	if (flags != 0)
1490da6c28aaSamw 		mark_excluded_protos(group, node, flags);
14916185db85Sdougm 	if (persist != SA_SHARE_TRANSIENT) {
14926185db85Sdougm 		/*
14936185db85Sdougm 		 * persistent shares come in two flavors: SMF and
14946185db85Sdougm 		 * ZFS. Sort this one out based on target group and
1495da6c28aaSamw 		 * path type. Both NFS and SMB are supported. First,
1496da6c28aaSamw 		 * check to see if the protocol is enabled on the
1497da6c28aaSamw 		 * subgroup and then setup the share appropriately.
14986185db85Sdougm 		 */
149957b448deSdougm 		if (sa_group_is_zfs(group) &&
150057b448deSdougm 		    sa_path_is_zfs(sharepath)) {
1501da6c28aaSamw 			if (sa_get_optionset(group, "nfs") != NULL)
15026185db85Sdougm 				err = sa_zfs_set_sharenfs(group, sharepath, 1);
1503da6c28aaSamw 			else if (sa_get_optionset(group, "smb") != NULL)
1504da6c28aaSamw 				err = sa_zfs_set_sharesmb(group, sharepath, 1);
15056185db85Sdougm 		} else {
1506549ec3ffSdougm 			sa_handle_impl_t impl_handle;
150757b448deSdougm 			impl_handle =
1508da6c28aaSamw 			    (sa_handle_impl_t)sa_find_group_handle(group);
150957b448deSdougm 			if (impl_handle != NULL) {
1510da6c28aaSamw 				err = sa_commit_share(impl_handle->scfhandle,
1511da6c28aaSamw 				    group, (sa_share_t)node);
151257b448deSdougm 			} else {
1513549ec3ffSdougm 				err = SA_SYSTEM_ERR;
15146185db85Sdougm 			}
15156185db85Sdougm 		}
151657b448deSdougm 	}
1517da6c28aaSamw 	if (err == SA_NO_PERMISSION && persist & SA_SHARE_PARSER)
15186185db85Sdougm 		/* called by the dfstab parser so could be a show */
15196185db85Sdougm 		err = SA_OK;
1520da6c28aaSamw 
15216185db85Sdougm 	if (err != SA_OK) {
15226185db85Sdougm 		/*
15236185db85Sdougm 		 * we couldn't commit to the repository so undo
15246185db85Sdougm 		 * our internal state to reflect reality.
15256185db85Sdougm 		 */
15266185db85Sdougm 		xmlUnlinkNode(node);
15276185db85Sdougm 		xmlFreeNode(node);
15286185db85Sdougm 		node = NULL;
15296185db85Sdougm 	}
1530da6c28aaSamw 
15316185db85Sdougm 	if (error != NULL)
15326185db85Sdougm 		*error = err;
1533da6c28aaSamw 
15346185db85Sdougm 	return (node);
15356185db85Sdougm }
15366185db85Sdougm 
15376185db85Sdougm /*
15386185db85Sdougm  * sa_add_share(group, sharepath, persist, *error)
15396185db85Sdougm  *
15406185db85Sdougm  *	Add a new share object to the specified group.  The share will
15416185db85Sdougm  *	have the specified sharepath and will only be constructed if
15426185db85Sdougm  *	it is a valid path to be shared.  NULL is returned on error
15436185db85Sdougm  *	and a detailed error value will be returned via the error
15446185db85Sdougm  *	pointer.
15456185db85Sdougm  */
15466185db85Sdougm sa_share_t
sa_add_share(sa_group_t group,char * sharepath,int persist,int * error)15476185db85Sdougm sa_add_share(sa_group_t group, char *sharepath, int persist, int *error)
15486185db85Sdougm {
15496185db85Sdougm 	xmlNodePtr node = NULL;
1550f345c0beSdougm 	int strictness = SA_CHECK_NORMAL;
1551549ec3ffSdougm 	sa_handle_t handle;
1552da6c28aaSamw 	uint64_t special = 0;
1553da6c28aaSamw 	uint64_t features;
1554f345c0beSdougm 
1555f345c0beSdougm 	/*
1556f345c0beSdougm 	 * If the share is to be permanent, use strict checking so a
1557f345c0beSdougm 	 * bad config doesn't get created. Transient shares only need
1558f345c0beSdougm 	 * to check against the currently active
1559f345c0beSdougm 	 * shares. SA_SHARE_PARSER is a modifier used internally to
1560f345c0beSdougm 	 * indicate that we are being called by the dfstab parser and
1561f345c0beSdougm 	 * that we need strict checking in all cases. Normally persist
1562f345c0beSdougm 	 * is in integer value but SA_SHARE_PARSER may be or'd into
1563f345c0beSdougm 	 * it as an override.
1564f345c0beSdougm 	 */
1565f345c0beSdougm 	if (persist & SA_SHARE_PARSER || persist == SA_SHARE_PERMANENT)
1566f345c0beSdougm 		strictness = SA_CHECK_STRICT;
15676185db85Sdougm 
1568549ec3ffSdougm 	handle = sa_find_group_handle(group);
1569549ec3ffSdougm 
1570da6c28aaSamw 	/*
1571da6c28aaSamw 	 * need to determine if the share is valid. The rules are:
1572da6c28aaSamw 	 *	- The path must not already exist
1573da6c28aaSamw 	 *	- The path must not be a subdir or parent dir of an
1574da6c28aaSamw 	 *	  existing path unless at least one protocol allows it.
1575da6c28aaSamw 	 * The sub/parent check is done in sa_check_path().
1576da6c28aaSamw 	 */
1577da6c28aaSamw 
1578da6c28aaSamw 	if (sa_find_share(handle, sharepath) == NULL) {
1579da6c28aaSamw 		*error = sa_check_path(group, sharepath, strictness);
1580da6c28aaSamw 		features = get_all_features(group);
1581da6c28aaSamw 		switch (*error) {
1582da6c28aaSamw 		case SA_PATH_IS_SUBDIR:
1583da6c28aaSamw 			if (features & SA_FEATURE_ALLOWSUBDIRS)
1584da6c28aaSamw 				special |= SA_FEATURE_ALLOWSUBDIRS;
1585da6c28aaSamw 			break;
1586da6c28aaSamw 		case SA_PATH_IS_PARENTDIR:
1587da6c28aaSamw 			if (features & SA_FEATURE_ALLOWPARDIRS)
1588da6c28aaSamw 				special |= SA_FEATURE_ALLOWPARDIRS;
1589da6c28aaSamw 			break;
15906185db85Sdougm 		}
1591da6c28aaSamw 		if (*error == SA_OK || special != SA_FEATURE_NONE)
1592da6c28aaSamw 			node = _sa_add_share(group, sharepath, persist,
1593da6c28aaSamw 			    error, special);
1594da6c28aaSamw 	} else {
15956185db85Sdougm 		*error = SA_DUPLICATE_NAME;
1596da6c28aaSamw 	}
15976185db85Sdougm 
15986185db85Sdougm 	return ((sa_share_t)node);
15996185db85Sdougm }
16006185db85Sdougm 
16016185db85Sdougm /*
16026185db85Sdougm  * sa_enable_share(share, protocol)
16036185db85Sdougm  *	Enable the specified share to the specified protocol.
16046185db85Sdougm  *	If protocol is NULL, then all protocols.
16056185db85Sdougm  */
16066185db85Sdougm int
sa_enable_share(sa_share_t share,char * protocol)16076185db85Sdougm sa_enable_share(sa_share_t share, char *protocol)
16086185db85Sdougm {
16096185db85Sdougm 	char *sharepath;
16106185db85Sdougm 	struct stat st;
1611da6c28aaSamw 	int err = SA_OK;
1612da6c28aaSamw 	int ret;
16136185db85Sdougm 
16146185db85Sdougm 	sharepath = sa_get_share_attr(share, "path");
1615da6c28aaSamw 	if (sharepath == NULL)
1616da6c28aaSamw 		return (SA_NO_MEMORY);
16176185db85Sdougm 	if (stat(sharepath, &st) < 0) {
16186185db85Sdougm 		err = SA_NO_SUCH_PATH;
16196185db85Sdougm 	} else {
16206185db85Sdougm 		/* tell the server about the share */
16216185db85Sdougm 		if (protocol != NULL) {
1622da6c28aaSamw 			if (excluded_protocol(share, protocol))
1623da6c28aaSamw 				goto done;
1624da6c28aaSamw 
16256185db85Sdougm 			/* lookup protocol specific handler */
16266185db85Sdougm 			err = sa_proto_share(protocol, share);
16276185db85Sdougm 			if (err == SA_OK)
1628da6c28aaSamw 				(void) sa_set_share_attr(share,
1629da6c28aaSamw 				    "shared", "true");
16306185db85Sdougm 		} else {
1631da6c28aaSamw 			/* Tell all protocols about the share */
1632da6c28aaSamw 			sa_group_t group;
1633da6c28aaSamw 			sa_optionset_t optionset;
1634da6c28aaSamw 
1635da6c28aaSamw 			group = sa_get_parent_group(share);
1636da6c28aaSamw 
1637da6c28aaSamw 			for (optionset = sa_get_optionset(group, NULL);
1638da6c28aaSamw 			    optionset != NULL;
1639da6c28aaSamw 			    optionset = sa_get_next_optionset(optionset)) {
1640da6c28aaSamw 				char *proto;
1641da6c28aaSamw 				proto = sa_get_optionset_attr(optionset,
1642da6c28aaSamw 				    "type");
1643da6c28aaSamw 				if (proto != NULL) {
1644da6c28aaSamw 					if (!excluded_protocol(share, proto)) {
1645da6c28aaSamw 						ret = sa_proto_share(proto,
1646da6c28aaSamw 						    share);
1647da6c28aaSamw 						if (ret != SA_OK)
1648da6c28aaSamw 							err = ret;
1649da6c28aaSamw 					}
1650da6c28aaSamw 					sa_free_attr_string(proto);
1651da6c28aaSamw 				}
1652da6c28aaSamw 			}
16536185db85Sdougm 			(void) sa_set_share_attr(share, "shared", "true");
16546185db85Sdougm 		}
16556185db85Sdougm 	}
1656da6c28aaSamw done:
16576185db85Sdougm 	if (sharepath != NULL)
16586185db85Sdougm 		sa_free_attr_string(sharepath);
16596185db85Sdougm 	return (err);
16606185db85Sdougm }
16616185db85Sdougm 
16626185db85Sdougm /*
16636185db85Sdougm  * sa_disable_share(share, protocol)
1664da6c28aaSamw  *	Disable the specified share to the specified protocol.  If
1665da6c28aaSamw  *	protocol is NULL, then all protocols that are enabled for the
1666da6c28aaSamw  *	share should be disabled.
16676185db85Sdougm  */
16686185db85Sdougm int
sa_disable_share(sa_share_t share,char * protocol)16696185db85Sdougm sa_disable_share(sa_share_t share, char *protocol)
16706185db85Sdougm {
16716185db85Sdougm 	char *path;
1672da6c28aaSamw 	int err = SA_OK;
16736185db85Sdougm 	int ret = SA_OK;
16746185db85Sdougm 
16756185db85Sdougm 	path = sa_get_share_attr(share, "path");
16766185db85Sdougm 
16776185db85Sdougm 	if (protocol != NULL) {
1678ecd6cf80Smarks 		ret = sa_proto_unshare(share, protocol, path);
16796185db85Sdougm 	} else {
16806185db85Sdougm 		/* need to do all protocols */
1681da6c28aaSamw 		sa_group_t group;
1682da6c28aaSamw 		sa_optionset_t optionset;
1683da6c28aaSamw 
1684da6c28aaSamw 		group = sa_get_parent_group(share);
1685da6c28aaSamw 
1686da6c28aaSamw 		/* Tell all protocols about the share */
1687da6c28aaSamw 		for (optionset = sa_get_optionset(group, NULL);
1688da6c28aaSamw 		    optionset != NULL;
1689da6c28aaSamw 		    optionset = sa_get_next_optionset(optionset)) {
1690da6c28aaSamw 			char *proto;
1691da6c28aaSamw 
1692da6c28aaSamw 			proto = sa_get_optionset_attr(optionset, "type");
1693da6c28aaSamw 			if (proto != NULL) {
1694da6c28aaSamw 				err = sa_proto_unshare(share, proto, path);
1695da6c28aaSamw 				if (err != SA_OK)
1696da6c28aaSamw 					ret = err;
1697da6c28aaSamw 				sa_free_attr_string(proto);
1698da6c28aaSamw 			}
1699da6c28aaSamw 		}
17006185db85Sdougm 	}
17016185db85Sdougm 	if (ret == SA_OK)
17026185db85Sdougm 		(void) sa_set_share_attr(share, "shared", NULL);
17036185db85Sdougm 	if (path != NULL)
17046185db85Sdougm 		sa_free_attr_string(path);
17056185db85Sdougm 	return (ret);
17066185db85Sdougm }
17076185db85Sdougm 
17086185db85Sdougm /*
17096185db85Sdougm  * sa_remove_share(share)
17106185db85Sdougm  *
17116185db85Sdougm  * remove the specified share from its containing group.
17126185db85Sdougm  * Remove from the SMF or ZFS configuration space.
17136185db85Sdougm  */
17146185db85Sdougm 
17156185db85Sdougm int
sa_remove_share(sa_share_t share)17166185db85Sdougm sa_remove_share(sa_share_t share)
17176185db85Sdougm {
17186185db85Sdougm 	sa_group_t group;
17196185db85Sdougm 	int ret = SA_OK;
17206185db85Sdougm 	char *type;
17216185db85Sdougm 	int transient = 0;
17226185db85Sdougm 	char *groupname;
17236185db85Sdougm 	char *zfs;
17246185db85Sdougm 
17256185db85Sdougm 	type = sa_get_share_attr(share, "type");
17266185db85Sdougm 	group = sa_get_parent_group(share);
17276185db85Sdougm 	zfs = sa_get_group_attr(group, "zfs");
17286185db85Sdougm 	groupname = sa_get_group_attr(group, "name");
17296185db85Sdougm 	if (type != NULL && strcmp(type, "persist") != 0)
17306185db85Sdougm 		transient = 1;
17316185db85Sdougm 	if (type != NULL)
17326185db85Sdougm 		sa_free_attr_string(type);
17336185db85Sdougm 
17346185db85Sdougm 	/* remove the node from its group then free the memory */
17356185db85Sdougm 
17366185db85Sdougm 	/*
17376185db85Sdougm 	 * need to test if "busy"
17386185db85Sdougm 	 */
17396185db85Sdougm 	/* only do SMF action if permanent */
17406185db85Sdougm 	if (!transient || zfs != NULL) {
17416185db85Sdougm 		/* remove from legacy dfstab as well as possible SMF */
1742da6c28aaSamw 		ret = sa_delete_legacy(share, NULL);
17436185db85Sdougm 		if (ret == SA_OK) {
17446185db85Sdougm 			if (!sa_group_is_zfs(group)) {
1745549ec3ffSdougm 				sa_handle_impl_t impl_handle;
174657b448deSdougm 				impl_handle = (sa_handle_impl_t)
174757b448deSdougm 				    sa_find_group_handle(group);
174857b448deSdougm 				if (impl_handle != NULL) {
174957b448deSdougm 					ret = sa_delete_share(
175057b448deSdougm 					    impl_handle->scfhandle, group,
175157b448deSdougm 					    share);
17526185db85Sdougm 				} else {
175357b448deSdougm 					ret = SA_SYSTEM_ERR;
175457b448deSdougm 				}
175557b448deSdougm 			} else {
175657b448deSdougm 				char *sharepath = sa_get_share_attr(share,
175757b448deSdougm 				    "path");
17586185db85Sdougm 				if (sharepath != NULL) {
175957b448deSdougm 					ret = sa_zfs_set_sharenfs(group,
176057b448deSdougm 					    sharepath, 0);
17616185db85Sdougm 					sa_free_attr_string(sharepath);
17626185db85Sdougm 				}
17636185db85Sdougm 			}
17646185db85Sdougm 		}
17656185db85Sdougm 	}
17666185db85Sdougm 	if (groupname != NULL)
17676185db85Sdougm 		sa_free_attr_string(groupname);
17686185db85Sdougm 	if (zfs != NULL)
17696185db85Sdougm 		sa_free_attr_string(zfs);
17706185db85Sdougm 
17716185db85Sdougm 	xmlUnlinkNode((xmlNodePtr)share);
17726185db85Sdougm 	xmlFreeNode((xmlNodePtr)share);
17736185db85Sdougm 	return (ret);
17746185db85Sdougm }
17756185db85Sdougm 
17766185db85Sdougm /*
17776185db85Sdougm  * sa_move_share(group, share)
17786185db85Sdougm  *
17796185db85Sdougm  * move the specified share to the specified group.  Update SMF
17806185db85Sdougm  * appropriately.
17816185db85Sdougm  */
17826185db85Sdougm 
17836185db85Sdougm int
sa_move_share(sa_group_t group,sa_share_t share)17846185db85Sdougm sa_move_share(sa_group_t group, sa_share_t share)
17856185db85Sdougm {
17866185db85Sdougm 	sa_group_t oldgroup;
17876185db85Sdougm 	int ret = SA_OK;
17886185db85Sdougm 
17896185db85Sdougm 	/* remove the node from its group then free the memory */
17906185db85Sdougm 
17916185db85Sdougm 	oldgroup = sa_get_parent_group(share);
17926185db85Sdougm 	if (oldgroup != group) {
1793549ec3ffSdougm 		sa_handle_impl_t impl_handle;
17946185db85Sdougm 		xmlUnlinkNode((xmlNodePtr)share);
179557b448deSdougm 		/*
179657b448deSdougm 		 * now that the share isn't in its old group, add to
179757b448deSdougm 		 * the new one
179857b448deSdougm 		 */
17994bff34e3Sthurlow 		(void) xmlAddChild((xmlNodePtr)group, (xmlNodePtr)share);
18006185db85Sdougm 		/* need to deal with SMF */
1801549ec3ffSdougm 		impl_handle = (sa_handle_impl_t)sa_find_group_handle(group);
1802549ec3ffSdougm 		if (impl_handle != NULL) {
18036185db85Sdougm 			/*
18046185db85Sdougm 			 * need to remove from old group first and then add to
18056185db85Sdougm 			 * new group. Ideally, we would do the other order but
18066185db85Sdougm 			 * need to avoid having the share in two groups at the
18076185db85Sdougm 			 * same time.
18086185db85Sdougm 			 */
180957b448deSdougm 			ret = sa_delete_share(impl_handle->scfhandle, oldgroup,
181057b448deSdougm 			    share);
1811549ec3ffSdougm 			if (ret == SA_OK)
181257b448deSdougm 				ret = sa_commit_share(impl_handle->scfhandle,
181357b448deSdougm 				    group, share);
1814549ec3ffSdougm 		} else {
1815549ec3ffSdougm 			ret = SA_SYSTEM_ERR;
18166185db85Sdougm 		}
18176185db85Sdougm 	}
18186185db85Sdougm 	return (ret);
18196185db85Sdougm }
18206185db85Sdougm 
18216185db85Sdougm /*
18226185db85Sdougm  * sa_get_parent_group(share)
18236185db85Sdougm  *
1824da6c28aaSamw  * Return the containing group for the share. If a group was actually
18256185db85Sdougm  * passed in, we don't want a parent so return NULL.
18266185db85Sdougm  */
18276185db85Sdougm 
18286185db85Sdougm sa_group_t
sa_get_parent_group(sa_share_t share)18296185db85Sdougm sa_get_parent_group(sa_share_t share)
18306185db85Sdougm {
18316185db85Sdougm 	xmlNodePtr node = NULL;
18326185db85Sdougm 	if (share != NULL) {
18336185db85Sdougm 		node = ((xmlNodePtr)share)->parent;
18346185db85Sdougm 		/*
18356185db85Sdougm 		 * make sure parent is a group and not sharecfg since
18366185db85Sdougm 		 * we may be cheating and passing in a group.
18376185db85Sdougm 		 * Eventually, groups of groups might come into being.
18386185db85Sdougm 		 */
18396185db85Sdougm 		if (node == NULL ||
18406185db85Sdougm 		    xmlStrcmp(node->name, (xmlChar *)"sharecfg") == 0)
18416185db85Sdougm 			node = NULL;
18426185db85Sdougm 	}
18436185db85Sdougm 	return ((sa_group_t)node);
18446185db85Sdougm }
18456185db85Sdougm 
18466185db85Sdougm /*
1847549ec3ffSdougm  * _sa_create_group(impl_handle, groupname)
18486185db85Sdougm  *
18496185db85Sdougm  * Create a group in the document. The caller will need to deal with
18506185db85Sdougm  * configuration store and activation.
18516185db85Sdougm  */
18526185db85Sdougm 
18536185db85Sdougm sa_group_t
_sa_create_group(sa_handle_impl_t impl_handle,char * groupname)1854549ec3ffSdougm _sa_create_group(sa_handle_impl_t impl_handle, char *groupname)
18556185db85Sdougm {
18566185db85Sdougm 	xmlNodePtr node = NULL;
18576185db85Sdougm 
18586185db85Sdougm 	if (sa_valid_group_name(groupname)) {
185957b448deSdougm 		node = xmlNewChild(impl_handle->tree, NULL, (xmlChar *)"group",
186057b448deSdougm 		    NULL);
18616185db85Sdougm 		if (node != NULL) {
18624bff34e3Sthurlow 			(void) xmlSetProp(node, (xmlChar *)"name",
186357b448deSdougm 			    (xmlChar *)groupname);
18644bff34e3Sthurlow 			(void) xmlSetProp(node, (xmlChar *)"state",
186557b448deSdougm 			    (xmlChar *)"enabled");
18666185db85Sdougm 		}
18676185db85Sdougm 	}
18686185db85Sdougm 	return ((sa_group_t)node);
18696185db85Sdougm }
18706185db85Sdougm 
18716185db85Sdougm /*
18726185db85Sdougm  * _sa_create_zfs_group(group, groupname)
18736185db85Sdougm  *
18746185db85Sdougm  * Create a ZFS subgroup under the specified group. This may
18756185db85Sdougm  * eventually form the basis of general sub-groups, but is currently
18766185db85Sdougm  * restricted to ZFS.
18776185db85Sdougm  */
18786185db85Sdougm sa_group_t
_sa_create_zfs_group(sa_group_t group,char * groupname)18796185db85Sdougm _sa_create_zfs_group(sa_group_t group, char *groupname)
18806185db85Sdougm {
18816185db85Sdougm 	xmlNodePtr node = NULL;
18826185db85Sdougm 
188357b448deSdougm 	node = xmlNewChild((xmlNodePtr)group, NULL, (xmlChar *)"group", NULL);
18846185db85Sdougm 	if (node != NULL) {
18854bff34e3Sthurlow 		(void) xmlSetProp(node, (xmlChar *)"name",
18864bff34e3Sthurlow 		    (xmlChar *)groupname);
18874bff34e3Sthurlow 		(void) xmlSetProp(node, (xmlChar *)"state",
18884bff34e3Sthurlow 		    (xmlChar *)"enabled");
18896185db85Sdougm 	}
18906185db85Sdougm 
18916185db85Sdougm 	return ((sa_group_t)node);
18926185db85Sdougm }
18936185db85Sdougm 
18946185db85Sdougm /*
18956185db85Sdougm  * sa_create_group(groupname, *error)
18966185db85Sdougm  *
18976185db85Sdougm  * Create a new group with groupname.  Need to validate that it is a
18986185db85Sdougm  * legal name for SMF and the construct the SMF service instance of
18996185db85Sdougm  * svc:/network/shares/group to implement the group. All necessary
19006185db85Sdougm  * operational properties must be added to the group at this point
19016185db85Sdougm  * (via the SMF transaction model).
19026185db85Sdougm  */
19036185db85Sdougm sa_group_t
sa_create_group(sa_handle_t handle,char * groupname,int * error)1904549ec3ffSdougm sa_create_group(sa_handle_t handle, char *groupname, int *error)
19056185db85Sdougm {
19066185db85Sdougm 	xmlNodePtr node = NULL;
19076185db85Sdougm 	sa_group_t group;
19086185db85Sdougm 	int ret;
190957b448deSdougm 	char rbacstr[SA_STRSIZE];
1910549ec3ffSdougm 	sa_handle_impl_t impl_handle = (sa_handle_impl_t)handle;
19116185db85Sdougm 
19126185db85Sdougm 	ret = SA_OK;
19136185db85Sdougm 
1914549ec3ffSdougm 	if (impl_handle == NULL || impl_handle->scfhandle == NULL) {
19156185db85Sdougm 		ret = SA_SYSTEM_ERR;
19166185db85Sdougm 		goto err;
19176185db85Sdougm 	}
19186185db85Sdougm 
1919549ec3ffSdougm 	group = sa_get_group(handle, groupname);
19206185db85Sdougm 	if (group != NULL) {
19216185db85Sdougm 		ret = SA_DUPLICATE_NAME;
19226185db85Sdougm 	} else {
19236185db85Sdougm 		if (sa_valid_group_name(groupname)) {
1924549ec3ffSdougm 			node = xmlNewChild(impl_handle->tree, NULL,
19256185db85Sdougm 			    (xmlChar *)"group", NULL);
19266185db85Sdougm 			if (node != NULL) {
19274bff34e3Sthurlow 				(void) xmlSetProp(node, (xmlChar *)"name",
192857b448deSdougm 				    (xmlChar *)groupname);
19296185db85Sdougm 				/* default to the group being enabled */
19304bff34e3Sthurlow 				(void) xmlSetProp(node, (xmlChar *)"state",
193157b448deSdougm 				    (xmlChar *)"enabled");
193257b448deSdougm 				ret = sa_create_instance(impl_handle->scfhandle,
193357b448deSdougm 				    groupname);
19346185db85Sdougm 				if (ret == SA_OK) {
193557b448deSdougm 					ret = sa_start_transaction(
193657b448deSdougm 					    impl_handle->scfhandle,
1937549ec3ffSdougm 					    "operation");
19386185db85Sdougm 				}
19396185db85Sdougm 				if (ret == SA_OK) {
194057b448deSdougm 					ret = sa_set_property(
194157b448deSdougm 					    impl_handle->scfhandle,
1942549ec3ffSdougm 					    "state", "enabled");
19436185db85Sdougm 					if (ret == SA_OK) {
194457b448deSdougm 						ret = sa_end_transaction(
19455b6e0c46Sdougm 						    impl_handle->scfhandle,
19465b6e0c46Sdougm 						    impl_handle);
19476185db85Sdougm 					} else {
194857b448deSdougm 						sa_abort_transaction(
194957b448deSdougm 						    impl_handle->scfhandle);
19506185db85Sdougm 					}
19516185db85Sdougm 				}
19526185db85Sdougm 				if (ret == SA_OK) {
19536185db85Sdougm 					/* initialize the RBAC strings */
195457b448deSdougm 					ret = sa_start_transaction(
195557b448deSdougm 					    impl_handle->scfhandle,
1956549ec3ffSdougm 					    "general");
19576185db85Sdougm 					if (ret == SA_OK) {
195857b448deSdougm 						(void) snprintf(rbacstr,
195957b448deSdougm 						    sizeof (rbacstr), "%s.%s",
19606185db85Sdougm 						    SA_RBAC_MANAGE, groupname);
196157b448deSdougm 						ret = sa_set_property(
196257b448deSdougm 						    impl_handle->scfhandle,
19636185db85Sdougm 						    "action_authorization",
19646185db85Sdougm 						    rbacstr);
19656185db85Sdougm 					}
19666185db85Sdougm 					if (ret == SA_OK) {
196757b448deSdougm 						(void) snprintf(rbacstr,
196857b448deSdougm 						    sizeof (rbacstr), "%s.%s",
19696185db85Sdougm 						    SA_RBAC_VALUE, groupname);
197057b448deSdougm 						ret = sa_set_property(
197157b448deSdougm 						    impl_handle->scfhandle,
19726185db85Sdougm 						    "value_authorization",
19736185db85Sdougm 						    rbacstr);
19746185db85Sdougm 					}
19756185db85Sdougm 					if (ret == SA_OK) {
197657b448deSdougm 						ret = sa_end_transaction(
19775b6e0c46Sdougm 						    impl_handle->scfhandle,
19785b6e0c46Sdougm 						    impl_handle);
19796185db85Sdougm 					} else {
198057b448deSdougm 						sa_abort_transaction(
198157b448deSdougm 						    impl_handle->scfhandle);
19826185db85Sdougm 					}
19836185db85Sdougm 				}
19846185db85Sdougm 				if (ret != SA_OK) {
19856185db85Sdougm 					/*
198657b448deSdougm 					 * Couldn't commit the group
198757b448deSdougm 					 * so we need to undo
198857b448deSdougm 					 * internally.
19896185db85Sdougm 					 */
19906185db85Sdougm 					xmlUnlinkNode(node);
19916185db85Sdougm 					xmlFreeNode(node);
19926185db85Sdougm 					node = NULL;
19936185db85Sdougm 				}
19946185db85Sdougm 			} else {
19956185db85Sdougm 				ret = SA_NO_MEMORY;
19966185db85Sdougm 			}
19976185db85Sdougm 		} else {
19986185db85Sdougm 			ret = SA_INVALID_NAME;
19996185db85Sdougm 		}
20006185db85Sdougm 	}
20016185db85Sdougm err:
20026185db85Sdougm 	if (error != NULL)
20036185db85Sdougm 		*error = ret;
20046185db85Sdougm 	return ((sa_group_t)node);
20056185db85Sdougm }
20066185db85Sdougm 
20076185db85Sdougm /*
20086185db85Sdougm  * sa_remove_group(group)
20096185db85Sdougm  *
20106185db85Sdougm  * Remove the specified group. This deletes from the SMF repository.
20116185db85Sdougm  * All property groups and properties are removed.
20126185db85Sdougm  */
20136185db85Sdougm 
20146185db85Sdougm int
sa_remove_group(sa_group_t group)20156185db85Sdougm sa_remove_group(sa_group_t group)
20166185db85Sdougm {
20176185db85Sdougm 	char *name;
20186185db85Sdougm 	int ret = SA_OK;
2019549ec3ffSdougm 	sa_handle_impl_t impl_handle;
20206185db85Sdougm 
2021549ec3ffSdougm 	impl_handle = (sa_handle_impl_t)sa_find_group_handle(group);
2022549ec3ffSdougm 	if (impl_handle != NULL) {
20236185db85Sdougm 		name = sa_get_group_attr(group, "name");
20246185db85Sdougm 		if (name != NULL) {
2025549ec3ffSdougm 			ret = sa_delete_instance(impl_handle->scfhandle, name);
20266185db85Sdougm 			sa_free_attr_string(name);
20276185db85Sdougm 		}
20286185db85Sdougm 		xmlUnlinkNode((xmlNodePtr)group); /* make sure unlinked */
20296185db85Sdougm 		xmlFreeNode((xmlNodePtr)group);   /* now it is gone */
2030549ec3ffSdougm 	} else {
2031549ec3ffSdougm 		ret = SA_SYSTEM_ERR;
2032549ec3ffSdougm 	}
20336185db85Sdougm 	return (ret);
20346185db85Sdougm }
20356185db85Sdougm 
20366185db85Sdougm /*
20376185db85Sdougm  * sa_update_config()
20386185db85Sdougm  *
20396185db85Sdougm  * Used to update legacy files that need to be updated in bulk
20406185db85Sdougm  * Currently, this is a placeholder and will go away in a future
20416185db85Sdougm  * release.
20426185db85Sdougm  */
20436185db85Sdougm 
20446185db85Sdougm int
sa_update_config(sa_handle_t handle)2045549ec3ffSdougm sa_update_config(sa_handle_t handle)
20466185db85Sdougm {
20476185db85Sdougm 	/*
20486185db85Sdougm 	 * do legacy files first so we can tell when they change.
20496185db85Sdougm 	 * This will go away when we start updating individual records
20506185db85Sdougm 	 * rather than the whole file.
20516185db85Sdougm 	 */
2052549ec3ffSdougm 	update_legacy_config(handle);
20536185db85Sdougm 	return (SA_OK);
20546185db85Sdougm }
20556185db85Sdougm 
20566185db85Sdougm /*
20576185db85Sdougm  * get_node_attr(node, tag)
20586185db85Sdougm  *
2059da6c28aaSamw  * Get the specified tag(attribute) if it exists on the node.  This is
20606185db85Sdougm  * used internally by a number of attribute oriented functions.
20616185db85Sdougm  */
20626185db85Sdougm 
20636185db85Sdougm static char *
get_node_attr(void * nodehdl,char * tag)20646185db85Sdougm get_node_attr(void *nodehdl, char *tag)
20656185db85Sdougm {
20666185db85Sdougm 	xmlNodePtr node = (xmlNodePtr)nodehdl;
20676185db85Sdougm 	xmlChar *name = NULL;
20686185db85Sdougm 
206957b448deSdougm 	if (node != NULL)
20706185db85Sdougm 		name = xmlGetProp(node, (xmlChar *)tag);
20716185db85Sdougm 	return ((char *)name);
20726185db85Sdougm }
20736185db85Sdougm 
20746185db85Sdougm /*
2075fe1c642dSBill Krier  * set_node_attr(node, tag)
20766185db85Sdougm  *
2077da6c28aaSamw  * Set the specified tag(attribute) to the specified value This is
20786185db85Sdougm  * used internally by a number of attribute oriented functions. It
20796185db85Sdougm  * doesn't update the repository, only the internal document state.
20806185db85Sdougm  */
20816185db85Sdougm 
20826185db85Sdougm void
set_node_attr(void * nodehdl,char * tag,char * value)20836185db85Sdougm set_node_attr(void *nodehdl, char *tag, char *value)
20846185db85Sdougm {
20856185db85Sdougm 	xmlNodePtr node = (xmlNodePtr)nodehdl;
20866185db85Sdougm 	if (node != NULL && tag != NULL) {
208757b448deSdougm 		if (value != NULL)
20884bff34e3Sthurlow 			(void) xmlSetProp(node, (xmlChar *)tag,
20894bff34e3Sthurlow 			    (xmlChar *)value);
209057b448deSdougm 		else
20914bff34e3Sthurlow 			(void) xmlUnsetProp(node, (xmlChar *)tag);
20926185db85Sdougm 	}
20936185db85Sdougm }
20946185db85Sdougm 
20956185db85Sdougm /*
20966185db85Sdougm  * sa_get_group_attr(group, tag)
20976185db85Sdougm  *
20986185db85Sdougm  * Get the specied attribute, if defined, for the group.
20996185db85Sdougm  */
21006185db85Sdougm 
21016185db85Sdougm char *
sa_get_group_attr(sa_group_t group,char * tag)21026185db85Sdougm sa_get_group_attr(sa_group_t group, char *tag)
21036185db85Sdougm {
21046185db85Sdougm 	return (get_node_attr((void *)group, tag));
21056185db85Sdougm }
21066185db85Sdougm 
21076185db85Sdougm /*
21086185db85Sdougm  * sa_set_group_attr(group, tag, value)
21096185db85Sdougm  *
21106185db85Sdougm  * set the specified tag/attribute on the group using value as its
21116185db85Sdougm  * value.
21126185db85Sdougm  *
21136185db85Sdougm  * This will result in setting the property in the SMF repository as
21146185db85Sdougm  * well as in the internal document.
21156185db85Sdougm  */
21166185db85Sdougm 
21176185db85Sdougm int
sa_set_group_attr(sa_group_t group,char * tag,char * value)21186185db85Sdougm sa_set_group_attr(sa_group_t group, char *tag, char *value)
21196185db85Sdougm {
21206185db85Sdougm 	int ret;
21216185db85Sdougm 	char *groupname;
2122549ec3ffSdougm 	sa_handle_impl_t impl_handle;
21236185db85Sdougm 
2124da6c28aaSamw 	/*
2125da6c28aaSamw 	 * ZFS group/subgroup doesn't need the handle so shortcut.
2126da6c28aaSamw 	 */
2127da6c28aaSamw 	if (sa_group_is_zfs(group)) {
2128da6c28aaSamw 		set_node_attr((void *)group, tag, value);
2129da6c28aaSamw 		return (SA_OK);
2130da6c28aaSamw 	}
2131da6c28aaSamw 
2132549ec3ffSdougm 	impl_handle = (sa_handle_impl_t)sa_find_group_handle(group);
2133549ec3ffSdougm 	if (impl_handle != NULL) {
21346185db85Sdougm 		groupname = sa_get_group_attr(group, "name");
2135549ec3ffSdougm 		ret = sa_get_instance(impl_handle->scfhandle, groupname);
21366185db85Sdougm 		if (ret == SA_OK) {
21376185db85Sdougm 			set_node_attr((void *)group, tag, value);
213857b448deSdougm 			ret = sa_start_transaction(impl_handle->scfhandle,
213957b448deSdougm 			    "operation");
21406185db85Sdougm 			if (ret == SA_OK) {
214157b448deSdougm 				ret = sa_set_property(impl_handle->scfhandle,
214257b448deSdougm 				    tag, value);
21436185db85Sdougm 				if (ret == SA_OK)
2144573b0c00Sdougm 					ret = sa_end_transaction(
21455b6e0c46Sdougm 					    impl_handle->scfhandle,
21465b6e0c46Sdougm 					    impl_handle);
214757b448deSdougm 				else
214857b448deSdougm 					sa_abort_transaction(
214957b448deSdougm 					    impl_handle->scfhandle);
21506185db85Sdougm 			}
2151573b0c00Sdougm 			if (ret == SA_SYSTEM_ERR)
2152573b0c00Sdougm 				ret = SA_NO_PERMISSION;
21536185db85Sdougm 		}
21546185db85Sdougm 		if (groupname != NULL)
21556185db85Sdougm 			sa_free_attr_string(groupname);
2156549ec3ffSdougm 	} else {
2157549ec3ffSdougm 		ret = SA_SYSTEM_ERR;
2158549ec3ffSdougm 	}
21596185db85Sdougm 	return (ret);
21606185db85Sdougm }
21616185db85Sdougm 
21626185db85Sdougm /*
21636185db85Sdougm  * sa_get_share_attr(share, tag)
21646185db85Sdougm  *
21656185db85Sdougm  * Return the value of the tag/attribute set on the specified
21666185db85Sdougm  * share. Returns NULL if the tag doesn't exist.
21676185db85Sdougm  */
21686185db85Sdougm 
21696185db85Sdougm char *
sa_get_share_attr(sa_share_t share,char * tag)21706185db85Sdougm sa_get_share_attr(sa_share_t share, char *tag)
21716185db85Sdougm {
21726185db85Sdougm 	return (get_node_attr((void *)share, tag));
21736185db85Sdougm }
21746185db85Sdougm 
21756185db85Sdougm /*
21766185db85Sdougm  * _sa_set_share_description(share, description)
21776185db85Sdougm  *
2178da6c28aaSamw  * Add a description tag with text contents to the specified share.  A
2179da6c28aaSamw  * separate XML tag is used rather than a property. This can also be
2180da6c28aaSamw  * used with resources.
21816185db85Sdougm  */
21826185db85Sdougm 
21836185db85Sdougm xmlNodePtr
_sa_set_share_description(void * share,char * content)2184da6c28aaSamw _sa_set_share_description(void *share, char *content)
21856185db85Sdougm {
21866185db85Sdougm 	xmlNodePtr node;
218757b448deSdougm 	node = xmlNewChild((xmlNodePtr)share, NULL, (xmlChar *)"description",
218857b448deSdougm 	    NULL);
21896185db85Sdougm 	xmlNodeSetContent(node, (xmlChar *)content);
21906185db85Sdougm 	return (node);
21916185db85Sdougm }
21926185db85Sdougm 
21936185db85Sdougm /*
21946185db85Sdougm  * sa_set_share_attr(share, tag, value)
21956185db85Sdougm  *
21966185db85Sdougm  * Set the share attribute specified by tag to the specified value. In
21976185db85Sdougm  * the case of "resource", enforce a no duplicates in a group rule. If
21986185db85Sdougm  * the share is not transient, commit the changes to the repository
21996185db85Sdougm  * else just update the share internally.
22006185db85Sdougm  */
22016185db85Sdougm 
22026185db85Sdougm int
sa_set_share_attr(sa_share_t share,char * tag,char * value)22036185db85Sdougm sa_set_share_attr(sa_share_t share, char *tag, char *value)
22046185db85Sdougm {
22056185db85Sdougm 	sa_group_t group;
22066185db85Sdougm 	sa_share_t resource;
22076185db85Sdougm 	int ret = SA_OK;
22086185db85Sdougm 
22096185db85Sdougm 	group = sa_get_parent_group(share);
22106185db85Sdougm 
22116185db85Sdougm 	/*
22126185db85Sdougm 	 * There are some attributes that may have specific
22136185db85Sdougm 	 * restrictions on them. Initially, only "resource" has
22146185db85Sdougm 	 * special meaning that needs to be checked. Only one instance
22156185db85Sdougm 	 * of a resource name may exist within a group.
22166185db85Sdougm 	 */
22176185db85Sdougm 
22186185db85Sdougm 	if (strcmp(tag, "resource") == 0) {
22196185db85Sdougm 		resource = sa_get_resource(group, value);
22206185db85Sdougm 		if (resource != share && resource != NULL)
22216185db85Sdougm 			ret = SA_DUPLICATE_NAME;
22226185db85Sdougm 	}
22236185db85Sdougm 	if (ret == SA_OK) {
22246185db85Sdougm 		set_node_attr((void *)share, tag, value);
22256185db85Sdougm 		if (group != NULL) {
22266185db85Sdougm 			char *type;
22276185db85Sdougm 			/* we can probably optimize this some */
22286185db85Sdougm 			type = sa_get_share_attr(share, "type");
2229549ec3ffSdougm 			if (type == NULL || strcmp(type, "transient") != 0) {
2230549ec3ffSdougm 				sa_handle_impl_t impl_handle;
223157b448deSdougm 				impl_handle =
223257b448deSdougm 				    (sa_handle_impl_t)sa_find_group_handle(
223357b448deSdougm 				    group);
223457b448deSdougm 				if (impl_handle != NULL) {
223557b448deSdougm 					ret = sa_commit_share(
223657b448deSdougm 					    impl_handle->scfhandle, group,
223757b448deSdougm 					    share);
223857b448deSdougm 				} else {
2239549ec3ffSdougm 					ret = SA_SYSTEM_ERR;
2240549ec3ffSdougm 				}
224157b448deSdougm 			}
22426185db85Sdougm 			if (type != NULL)
22436185db85Sdougm 				sa_free_attr_string(type);
22446185db85Sdougm 		}
22456185db85Sdougm 	}
22466185db85Sdougm 	return (ret);
22476185db85Sdougm }
22486185db85Sdougm 
22496185db85Sdougm /*
22506185db85Sdougm  * sa_get_property_attr(prop, tag)
22516185db85Sdougm  *
22526185db85Sdougm  * Get the value of the specified property attribute. Standard
22536185db85Sdougm  * attributes are "type" and "value".
22546185db85Sdougm  */
22556185db85Sdougm 
22566185db85Sdougm char *
sa_get_property_attr(sa_property_t prop,char * tag)22576185db85Sdougm sa_get_property_attr(sa_property_t prop, char *tag)
22586185db85Sdougm {
22596185db85Sdougm 	return (get_node_attr((void *)prop, tag));
22606185db85Sdougm }
22616185db85Sdougm 
22626185db85Sdougm /*
22636185db85Sdougm  * sa_get_optionset_attr(prop, tag)
22646185db85Sdougm  *
22656185db85Sdougm  * Get the value of the specified property attribute. Standard
22666185db85Sdougm  * attribute is "type".
22676185db85Sdougm  */
22686185db85Sdougm 
22696185db85Sdougm char *
sa_get_optionset_attr(sa_property_t optionset,char * tag)22706185db85Sdougm sa_get_optionset_attr(sa_property_t optionset, char *tag)
22716185db85Sdougm {
22726185db85Sdougm 	return (get_node_attr((void *)optionset, tag));
22736185db85Sdougm 
22746185db85Sdougm }
22756185db85Sdougm 
22766185db85Sdougm /*
22776185db85Sdougm  * sa_set_optionset_attr(optionset, tag, value)
22786185db85Sdougm  *
22796185db85Sdougm  * Set the specified attribute(tag) to the specified value on the
22806185db85Sdougm  * optionset.
22816185db85Sdougm  */
22826185db85Sdougm 
22836185db85Sdougm void
sa_set_optionset_attr(sa_group_t optionset,char * tag,char * value)22846185db85Sdougm sa_set_optionset_attr(sa_group_t optionset, char *tag, char *value)
22856185db85Sdougm {
22866185db85Sdougm 	set_node_attr((void *)optionset, tag, value);
22876185db85Sdougm }
22886185db85Sdougm 
22896185db85Sdougm /*
22906185db85Sdougm  * sa_free_attr_string(string)
22916185db85Sdougm  *
22926185db85Sdougm  * Free the string that was returned in one of the sa_get_*_attr()
22936185db85Sdougm  * functions.
22946185db85Sdougm  */
22956185db85Sdougm 
22966185db85Sdougm void
sa_free_attr_string(char * string)22976185db85Sdougm sa_free_attr_string(char *string)
22986185db85Sdougm {
22996185db85Sdougm 	xmlFree((xmlChar *)string);
23006185db85Sdougm }
23016185db85Sdougm 
23026185db85Sdougm /*
23036185db85Sdougm  * sa_get_optionset(group, proto)
23046185db85Sdougm  *
23056185db85Sdougm  * Return the optionset, if it exists, that is associated with the
23066185db85Sdougm  * specified protocol.
23076185db85Sdougm  */
23086185db85Sdougm 
23096185db85Sdougm sa_optionset_t
sa_get_optionset(void * group,char * proto)23106185db85Sdougm sa_get_optionset(void *group, char *proto)
23116185db85Sdougm {
23126185db85Sdougm 	xmlNodePtr node;
23136185db85Sdougm 	xmlChar *value = NULL;
23146185db85Sdougm 
23156185db85Sdougm 	for (node = ((xmlNodePtr)group)->children; node != NULL;
23166185db85Sdougm 	    node = node->next) {
23176185db85Sdougm 		if (xmlStrcmp(node->name, (xmlChar *)"optionset") == 0) {
23186185db85Sdougm 			value = xmlGetProp(node, (xmlChar *)"type");
23196185db85Sdougm 			if (proto != NULL) {
23206185db85Sdougm 				if (value != NULL &&
23216185db85Sdougm 				    xmlStrcmp(value, (xmlChar *)proto) == 0) {
23226185db85Sdougm 					break;
23236185db85Sdougm 				}
23246185db85Sdougm 				if (value != NULL) {
23256185db85Sdougm 					xmlFree(value);
23266185db85Sdougm 					value = NULL;
23276185db85Sdougm 				}
23286185db85Sdougm 			} else {
23296185db85Sdougm 				break;
23306185db85Sdougm 			}
23316185db85Sdougm 		}
23326185db85Sdougm 	}
23336185db85Sdougm 	if (value != NULL)
23346185db85Sdougm 		xmlFree(value);
23356185db85Sdougm 	return ((sa_optionset_t)node);
23366185db85Sdougm }
23376185db85Sdougm 
23386185db85Sdougm /*
23396185db85Sdougm  * sa_get_next_optionset(optionset)
23406185db85Sdougm  *
23416185db85Sdougm  * Return the next optionset in the group. NULL if this was the last.
23426185db85Sdougm  */
23436185db85Sdougm 
23446185db85Sdougm sa_optionset_t
sa_get_next_optionset(sa_optionset_t optionset)23456185db85Sdougm sa_get_next_optionset(sa_optionset_t optionset)
23466185db85Sdougm {
23476185db85Sdougm 	xmlNodePtr node;
23486185db85Sdougm 
23496185db85Sdougm 	for (node = ((xmlNodePtr)optionset)->next; node != NULL;
23506185db85Sdougm 	    node = node->next) {
23516185db85Sdougm 		if (xmlStrcmp(node->name, (xmlChar *)"optionset") == 0) {
23526185db85Sdougm 			break;
23536185db85Sdougm 		}
23546185db85Sdougm 	}
23556185db85Sdougm 	return ((sa_optionset_t)node);
23566185db85Sdougm }
23576185db85Sdougm 
23586185db85Sdougm /*
23596185db85Sdougm  * sa_get_security(group, sectype, proto)
23606185db85Sdougm  *
23616185db85Sdougm  * Return the security optionset. The internal name is a hold over
23626185db85Sdougm  * from the implementation and will be changed before the API is
23636185db85Sdougm  * finalized. This is really a named optionset that can be negotiated
23646185db85Sdougm  * as a group of properties (like NFS security options).
23656185db85Sdougm  */
23666185db85Sdougm 
23676185db85Sdougm sa_security_t
sa_get_security(sa_group_t group,char * sectype,char * proto)23686185db85Sdougm sa_get_security(sa_group_t group, char *sectype, char *proto)
23696185db85Sdougm {
23706185db85Sdougm 	xmlNodePtr node;
23716185db85Sdougm 	xmlChar *value = NULL;
23726185db85Sdougm 
23736185db85Sdougm 	for (node = ((xmlNodePtr)group)->children; node != NULL;
23746185db85Sdougm 	    node = node->next) {
23756185db85Sdougm 		if (xmlStrcmp(node->name, (xmlChar *)"security") == 0) {
23766185db85Sdougm 			if (proto != NULL) {
23776185db85Sdougm 				value = xmlGetProp(node, (xmlChar *)"type");
23786185db85Sdougm 				if (value == NULL ||
23796185db85Sdougm 				    (value != NULL &&
23806185db85Sdougm 				    xmlStrcmp(value, (xmlChar *)proto) != 0)) {
23816185db85Sdougm 					/* it doesn't match so continue */
23826185db85Sdougm 					xmlFree(value);
23836185db85Sdougm 					value = NULL;
23846185db85Sdougm 					continue;
23856185db85Sdougm 				}
23866185db85Sdougm 			}
23876185db85Sdougm 			if (value != NULL) {
23886185db85Sdougm 				xmlFree(value);
23896185db85Sdougm 				value = NULL;
23906185db85Sdougm 			}
23916185db85Sdougm 			/* potential match */
23926185db85Sdougm 			if (sectype != NULL) {
23936185db85Sdougm 				value = xmlGetProp(node, (xmlChar *)"sectype");
23946185db85Sdougm 				if (value != NULL &&
23956185db85Sdougm 				    xmlStrcmp(value, (xmlChar *)sectype) == 0) {
23966185db85Sdougm 					break;
23976185db85Sdougm 				}
23986185db85Sdougm 			} else {
23996185db85Sdougm 				break;
24006185db85Sdougm 			}
24016185db85Sdougm 		}
24026185db85Sdougm 		if (value != NULL) {
24036185db85Sdougm 			xmlFree(value);
24046185db85Sdougm 			value = NULL;
24056185db85Sdougm 		}
24066185db85Sdougm 	}
24076185db85Sdougm 	if (value != NULL)
24086185db85Sdougm 		xmlFree(value);
24096185db85Sdougm 	return ((sa_security_t)node);
24106185db85Sdougm }
24116185db85Sdougm 
24126185db85Sdougm /*
24136185db85Sdougm  * sa_get_next_security(security)
24146185db85Sdougm  *
24156185db85Sdougm  * Get the next security optionset if one exists.
24166185db85Sdougm  */
24176185db85Sdougm 
24186185db85Sdougm sa_security_t
sa_get_next_security(sa_security_t security)24196185db85Sdougm sa_get_next_security(sa_security_t security)
24206185db85Sdougm {
24216185db85Sdougm 	xmlNodePtr node;
24226185db85Sdougm 
24236185db85Sdougm 	for (node = ((xmlNodePtr)security)->next; node != NULL;
24246185db85Sdougm 	    node = node->next) {
24256185db85Sdougm 		if (xmlStrcmp(node->name, (xmlChar *)"security") == 0) {
24266185db85Sdougm 			break;
24276185db85Sdougm 		}
24286185db85Sdougm 	}
24296185db85Sdougm 	return ((sa_security_t)node);
24306185db85Sdougm }
24316185db85Sdougm 
24326185db85Sdougm /*
24336185db85Sdougm  * sa_get_property(optionset, prop)
24346185db85Sdougm  *
24356185db85Sdougm  * Get the property object with the name specified in prop from the
24366185db85Sdougm  * optionset.
24376185db85Sdougm  */
24386185db85Sdougm 
24396185db85Sdougm sa_property_t
sa_get_property(sa_optionset_t optionset,char * prop)24406185db85Sdougm sa_get_property(sa_optionset_t optionset, char *prop)
24416185db85Sdougm {
24426185db85Sdougm 	xmlNodePtr node = (xmlNodePtr)optionset;
24436185db85Sdougm 	xmlChar *value = NULL;
24446185db85Sdougm 
24456185db85Sdougm 	if (optionset == NULL)
24466185db85Sdougm 		return (NULL);
24476185db85Sdougm 
24486185db85Sdougm 	for (node = node->children; node != NULL;
24496185db85Sdougm 	    node = node->next) {
24506185db85Sdougm 		if (xmlStrcmp(node->name, (xmlChar *)"option") == 0) {
24516185db85Sdougm 			if (prop == NULL)
24526185db85Sdougm 				break;
24536185db85Sdougm 			value = xmlGetProp(node, (xmlChar *)"type");
245457b448deSdougm 			if (value != NULL &&
245557b448deSdougm 			    xmlStrcmp(value, (xmlChar *)prop) == 0) {
24566185db85Sdougm 				break;
24576185db85Sdougm 			}
24586185db85Sdougm 			if (value != NULL) {
24596185db85Sdougm 				xmlFree(value);
24606185db85Sdougm 				value = NULL;
24616185db85Sdougm 			}
24626185db85Sdougm 		}
24636185db85Sdougm 	}
24646185db85Sdougm 	if (value != NULL)
24656185db85Sdougm 		xmlFree(value);
24666185db85Sdougm 	if (node != NULL && xmlStrcmp(node->name, (xmlChar *)"option") != 0) {
246757b448deSdougm 		/*
246857b448deSdougm 		 * avoid a non option node -- it is possible to be a
246957b448deSdougm 		 * text node
247057b448deSdougm 		 */
24716185db85Sdougm 		node = NULL;
24726185db85Sdougm 	}
24736185db85Sdougm 	return ((sa_property_t)node);
24746185db85Sdougm }
24756185db85Sdougm 
24766185db85Sdougm /*
24776185db85Sdougm  * sa_get_next_property(property)
24786185db85Sdougm  *
24796185db85Sdougm  * Get the next property following the specified property. NULL if
24806185db85Sdougm  * this was the last.
24816185db85Sdougm  */
24826185db85Sdougm 
24836185db85Sdougm sa_property_t
sa_get_next_property(sa_property_t property)24846185db85Sdougm sa_get_next_property(sa_property_t property)
24856185db85Sdougm {
24866185db85Sdougm 	xmlNodePtr node;
24876185db85Sdougm 
24886185db85Sdougm 	for (node = ((xmlNodePtr)property)->next; node != NULL;
24896185db85Sdougm 	    node = node->next) {
24906185db85Sdougm 		if (xmlStrcmp(node->name, (xmlChar *)"option") == 0) {
24916185db85Sdougm 			break;
24926185db85Sdougm 		}
24936185db85Sdougm 	}
24946185db85Sdougm 	return ((sa_property_t)node);
24956185db85Sdougm }
24966185db85Sdougm 
24976185db85Sdougm /*
24986185db85Sdougm  * sa_set_share_description(share, content)
24996185db85Sdougm  *
25006185db85Sdougm  * Set the description of share to content.
25016185db85Sdougm  */
25026185db85Sdougm 
25036185db85Sdougm int
sa_set_share_description(sa_share_t share,char * content)25046185db85Sdougm sa_set_share_description(sa_share_t share, char *content)
25056185db85Sdougm {
25066185db85Sdougm 	xmlNodePtr node;
25076185db85Sdougm 	sa_group_t group;
25086185db85Sdougm 	int ret = SA_OK;
25096185db85Sdougm 
25106185db85Sdougm 	for (node = ((xmlNodePtr)share)->children; node != NULL;
25116185db85Sdougm 	    node = node->next) {
25126185db85Sdougm 		if (xmlStrcmp(node->name, (xmlChar *)"description") == 0) {
25136185db85Sdougm 			break;
25146185db85Sdougm 		}
25156185db85Sdougm 	}
25166185db85Sdougm 	/* no existing description but want to add */
25176185db85Sdougm 	if (node == NULL && content != NULL) {
25186185db85Sdougm 		/* add a description */
25196185db85Sdougm 		node = _sa_set_share_description(share, content);
25206185db85Sdougm 	} else if (node != NULL && content != NULL) {
25216185db85Sdougm 		/* update a description */
25226185db85Sdougm 		xmlNodeSetContent(node, (xmlChar *)content);
25236185db85Sdougm 	} else if (node != NULL && content == NULL) {
25246185db85Sdougm 		/* remove an existing description */
25256185db85Sdougm 		xmlUnlinkNode(node);
25266185db85Sdougm 		xmlFreeNode(node);
25276185db85Sdougm 	}
2528da6c28aaSamw 	group = sa_get_parent_group(share);
2529148c5f43SAlan Wright 	if (group != NULL &&
2530148c5f43SAlan Wright 	    sa_is_persistent(share) && (!sa_group_is_zfs(group))) {
2531549ec3ffSdougm 		sa_handle_impl_t impl_handle;
2532549ec3ffSdougm 		impl_handle = (sa_handle_impl_t)sa_find_group_handle(group);
253357b448deSdougm 		if (impl_handle != NULL) {
253457b448deSdougm 			ret = sa_commit_share(impl_handle->scfhandle, group,
253557b448deSdougm 			    share);
253657b448deSdougm 		} else {
2537549ec3ffSdougm 			ret = SA_SYSTEM_ERR;
2538549ec3ffSdougm 		}
253957b448deSdougm 	}
25406185db85Sdougm 	return (ret);
25416185db85Sdougm }
25426185db85Sdougm 
25436185db85Sdougm /*
25446185db85Sdougm  * fixproblemchars(string)
25456185db85Sdougm  *
25466185db85Sdougm  * don't want any newline or tab characters in the text since these
25476185db85Sdougm  * could break display of data and legacy file formats.
25486185db85Sdougm  */
25496185db85Sdougm static void
fixproblemchars(char * str)25506185db85Sdougm fixproblemchars(char *str)
25516185db85Sdougm {
25526185db85Sdougm 	int c;
25536185db85Sdougm 	for (c = *str; c != '\0'; c = *++str) {
25546185db85Sdougm 		if (c == '\t' || c == '\n')
25556185db85Sdougm 			*str = ' ';
25566185db85Sdougm 		else if (c == '"')
25576185db85Sdougm 			*str = '\'';
25586185db85Sdougm 	}
25596185db85Sdougm }
25606185db85Sdougm 
25616185db85Sdougm /*
25626185db85Sdougm  * sa_get_share_description(share)
25636185db85Sdougm  *
25646185db85Sdougm  * Return the description text for the specified share if it
25656185db85Sdougm  * exists. NULL if no description exists.
25666185db85Sdougm  */
25676185db85Sdougm 
25686185db85Sdougm char *
sa_get_share_description(sa_share_t share)25696185db85Sdougm sa_get_share_description(sa_share_t share)
25706185db85Sdougm {
25716185db85Sdougm 	xmlChar *description = NULL;
25726185db85Sdougm 	xmlNodePtr node;
25736185db85Sdougm 
25746185db85Sdougm 	for (node = ((xmlNodePtr)share)->children; node != NULL;
25756185db85Sdougm 	    node = node->next) {
25766185db85Sdougm 		if (xmlStrcmp(node->name, (xmlChar *)"description") == 0) {
25776185db85Sdougm 			break;
25786185db85Sdougm 		}
25796185db85Sdougm 	}
25806185db85Sdougm 	if (node != NULL) {
2581da6c28aaSamw 		description = xmlNodeGetContent(node);
25826185db85Sdougm 		fixproblemchars((char *)description);
25836185db85Sdougm 	}
25846185db85Sdougm 	return ((char *)description);
25856185db85Sdougm }
25866185db85Sdougm 
25876185db85Sdougm /*
25886185db85Sdougm  * sa_free(share_description(description)
25896185db85Sdougm  *
25906185db85Sdougm  * Free the description string.
25916185db85Sdougm  */
25926185db85Sdougm 
25936185db85Sdougm void
sa_free_share_description(char * description)25946185db85Sdougm sa_free_share_description(char *description)
25956185db85Sdougm {
25966185db85Sdougm 	xmlFree((xmlChar *)description);
25976185db85Sdougm }
25986185db85Sdougm 
25996185db85Sdougm /*
26006185db85Sdougm  * sa_create_optionset(group, proto)
26016185db85Sdougm  *
26026185db85Sdougm  * Create an optionset for the specified protocol in the specied
26036185db85Sdougm  * group. This is manifested as a property group within SMF.
26046185db85Sdougm  */
26056185db85Sdougm 
26066185db85Sdougm sa_optionset_t
sa_create_optionset(sa_group_t group,char * proto)26076185db85Sdougm sa_create_optionset(sa_group_t group, char *proto)
26086185db85Sdougm {
26096185db85Sdougm 	sa_optionset_t optionset;
26106185db85Sdougm 	sa_group_t parent = group;
2611da6c28aaSamw 	sa_share_t share = NULL;
2612da6c28aaSamw 	int err = SA_OK;
2613da6c28aaSamw 	char *id = NULL;
26146185db85Sdougm 
26156185db85Sdougm 	optionset = sa_get_optionset(group, proto);
26166185db85Sdougm 	if (optionset != NULL) {
26176185db85Sdougm 		/* can't have a duplicate protocol */
26186185db85Sdougm 		optionset = NULL;
26196185db85Sdougm 	} else {
2620da6c28aaSamw 		/*
2621da6c28aaSamw 		 * Account for resource names being slightly
2622da6c28aaSamw 		 * different.
2623da6c28aaSamw 		 */
2624da6c28aaSamw 		if (sa_is_share(group)) {
2625da6c28aaSamw 			/*
2626da6c28aaSamw 			 * Transient shares do not have an "id" so not an
2627da6c28aaSamw 			 * error to not find one.
2628da6c28aaSamw 			 */
2629da6c28aaSamw 			id = sa_get_share_attr((sa_share_t)group, "id");
2630da6c28aaSamw 		} else if (sa_is_resource(group)) {
2631da6c28aaSamw 			share = sa_get_resource_parent(
2632da6c28aaSamw 			    (sa_resource_t)group);
2633da6c28aaSamw 			id = sa_get_resource_attr(share, "id");
2634da6c28aaSamw 
2635da6c28aaSamw 			/* id can be NULL if the group is transient (ZFS) */
2636da6c28aaSamw 			if (id == NULL && sa_is_persistent(group))
2637da6c28aaSamw 				err = SA_NO_MEMORY;
2638da6c28aaSamw 		}
2639da6c28aaSamw 		if (err == SA_NO_MEMORY) {
2640da6c28aaSamw 			/*
2641da6c28aaSamw 			 * Couldn't get the id for the share or
2642da6c28aaSamw 			 * resource. While this could be a
2643da6c28aaSamw 			 * configuration issue, it is most likely an
2644da6c28aaSamw 			 * out of memory. In any case, fail the create.
2645da6c28aaSamw 			 */
2646da6c28aaSamw 			return (NULL);
2647da6c28aaSamw 		}
2648da6c28aaSamw 
26496185db85Sdougm 		optionset = (sa_optionset_t)xmlNewChild((xmlNodePtr)group,
265057b448deSdougm 		    NULL, (xmlChar *)"optionset", NULL);
26516185db85Sdougm 		/*
26526185db85Sdougm 		 * only put to repository if on a group and we were
26536185db85Sdougm 		 * able to create an optionset.
26546185db85Sdougm 		 */
26556185db85Sdougm 		if (optionset != NULL) {
265657b448deSdougm 			char oname[SA_STRSIZE];
26576185db85Sdougm 			char *groupname;
26586185db85Sdougm 
2659da6c28aaSamw 			/*
2660da6c28aaSamw 			 * Need to get parent group in all cases, but also get
2661da6c28aaSamw 			 * the share if this is a resource.
2662da6c28aaSamw 			 */
2663da6c28aaSamw 			if (sa_is_share(group)) {
26646185db85Sdougm 				parent = sa_get_parent_group((sa_share_t)group);
2665da6c28aaSamw 			} else if (sa_is_resource(group)) {
2666da6c28aaSamw 				share = sa_get_resource_parent(
2667da6c28aaSamw 				    (sa_resource_t)group);
2668da6c28aaSamw 				parent = sa_get_parent_group(share);
2669da6c28aaSamw 			}
26706185db85Sdougm 
26716185db85Sdougm 			sa_set_optionset_attr(optionset, "type", proto);
26726185db85Sdougm 
26736185db85Sdougm 			(void) sa_optionset_name(optionset, oname,
26746185db85Sdougm 			    sizeof (oname), id);
26756185db85Sdougm 			groupname = sa_get_group_attr(parent, "name");
2676da6c28aaSamw 			if (groupname != NULL && sa_is_persistent(group)) {
2677549ec3ffSdougm 				sa_handle_impl_t impl_handle;
2678da6c28aaSamw 				impl_handle =
2679da6c28aaSamw 				    (sa_handle_impl_t)sa_find_group_handle(
2680da6c28aaSamw 				    group);
2681549ec3ffSdougm 				assert(impl_handle != NULL);
2682549ec3ffSdougm 				if (impl_handle != NULL) {
268357b448deSdougm 					(void) sa_get_instance(
2684da6c28aaSamw 					    impl_handle->scfhandle, groupname);
268557b448deSdougm 					(void) sa_create_pgroup(
268657b448deSdougm 					    impl_handle->scfhandle, oname);
26876185db85Sdougm 				}
2688549ec3ffSdougm 			}
2689549ec3ffSdougm 			if (groupname != NULL)
2690549ec3ffSdougm 				sa_free_attr_string(groupname);
2691da6c28aaSamw 		}
2692da6c28aaSamw 	}
2693da6c28aaSamw 
26946185db85Sdougm 	if (id != NULL)
26956185db85Sdougm 		sa_free_attr_string(id);
26966185db85Sdougm 	return (optionset);
26976185db85Sdougm }
26986185db85Sdougm 
26996185db85Sdougm /*
27006185db85Sdougm  * sa_get_property_parent(property)
27016185db85Sdougm  *
27026185db85Sdougm  * Given a property, return the object it is a property of. This will
27036185db85Sdougm  * be an optionset of some type.
27046185db85Sdougm  */
27056185db85Sdougm 
27066185db85Sdougm static sa_optionset_t
sa_get_property_parent(sa_property_t property)27076185db85Sdougm sa_get_property_parent(sa_property_t property)
27086185db85Sdougm {
27096185db85Sdougm 	xmlNodePtr node = NULL;
27106185db85Sdougm 
271157b448deSdougm 	if (property != NULL)
27126185db85Sdougm 		node = ((xmlNodePtr)property)->parent;
27136185db85Sdougm 	return ((sa_optionset_t)node);
27146185db85Sdougm }
27156185db85Sdougm 
27166185db85Sdougm /*
27176185db85Sdougm  * sa_get_optionset_parent(optionset)
27186185db85Sdougm  *
27196185db85Sdougm  * Return the parent of the specified optionset. This could be a group
27206185db85Sdougm  * or a share.
27216185db85Sdougm  */
27226185db85Sdougm 
27236185db85Sdougm static sa_group_t
sa_get_optionset_parent(sa_optionset_t optionset)27246185db85Sdougm sa_get_optionset_parent(sa_optionset_t optionset)
27256185db85Sdougm {
27266185db85Sdougm 	xmlNodePtr node = NULL;
27276185db85Sdougm 
272857b448deSdougm 	if (optionset != NULL)
27296185db85Sdougm 		node = ((xmlNodePtr)optionset)->parent;
27306185db85Sdougm 	return ((sa_group_t)node);
27316185db85Sdougm }
27326185db85Sdougm 
27336185db85Sdougm /*
27346185db85Sdougm  * zfs_needs_update(share)
27356185db85Sdougm  *
27366185db85Sdougm  * In order to avoid making multiple updates to a ZFS share when
27376185db85Sdougm  * setting properties, the share attribute "changed" will be set to
2738da6c28aaSamw  * true when a property is added or modified.  When done adding
27396185db85Sdougm  * properties, we can then detect that an update is needed.  We then
27406185db85Sdougm  * clear the state here to detect additional changes.
27416185db85Sdougm  */
27426185db85Sdougm 
27436185db85Sdougm static int
zfs_needs_update(sa_share_t share)27446185db85Sdougm zfs_needs_update(sa_share_t share)
27456185db85Sdougm {
27466185db85Sdougm 	char *attr;
27476185db85Sdougm 	int result = 0;
27486185db85Sdougm 
27496185db85Sdougm 	attr = sa_get_share_attr(share, "changed");
27506185db85Sdougm 	if (attr != NULL) {
27516185db85Sdougm 		sa_free_attr_string(attr);
27526185db85Sdougm 		result = 1;
27536185db85Sdougm 	}
27546185db85Sdougm 	set_node_attr((void *)share, "changed", NULL);
27556185db85Sdougm 	return (result);
27566185db85Sdougm }
27576185db85Sdougm 
27586185db85Sdougm /*
27596185db85Sdougm  * zfs_set_update(share)
27606185db85Sdougm  *
27616185db85Sdougm  * Set the changed attribute of the share to true.
27626185db85Sdougm  */
27636185db85Sdougm 
27646185db85Sdougm static void
zfs_set_update(sa_share_t share)27656185db85Sdougm zfs_set_update(sa_share_t share)
27666185db85Sdougm {
27676185db85Sdougm 	set_node_attr((void *)share, "changed", "true");
27686185db85Sdougm }
27696185db85Sdougm 
27706185db85Sdougm /*
27716185db85Sdougm  * sa_commit_properties(optionset, clear)
27726185db85Sdougm  *
27736185db85Sdougm  * Check if SMF or ZFS config and either update or abort the pending
27746185db85Sdougm  * changes.
27756185db85Sdougm  */
27766185db85Sdougm 
27776185db85Sdougm int
sa_commit_properties(sa_optionset_t optionset,int clear)27786185db85Sdougm sa_commit_properties(sa_optionset_t optionset, int clear)
27796185db85Sdougm {
27806185db85Sdougm 	sa_group_t group;
27816185db85Sdougm 	sa_group_t parent;
27826185db85Sdougm 	int zfs = 0;
27836185db85Sdougm 	int needsupdate = 0;
27846185db85Sdougm 	int ret = SA_OK;
2785549ec3ffSdougm 	sa_handle_impl_t impl_handle;
27866185db85Sdougm 
27876185db85Sdougm 	group = sa_get_optionset_parent(optionset);
27886185db85Sdougm 	if (group != NULL && (sa_is_share(group) || is_zfs_group(group))) {
27896185db85Sdougm 		/* only update ZFS if on a share */
27906185db85Sdougm 		parent = sa_get_parent_group(group);
27916185db85Sdougm 		zfs++;
279257b448deSdougm 		if (parent != NULL && is_zfs_group(parent))
27936185db85Sdougm 			needsupdate = zfs_needs_update(group);
279457b448deSdougm 		else
27956185db85Sdougm 			zfs = 0;
27966185db85Sdougm 	}
27976185db85Sdougm 	if (zfs) {
27986185db85Sdougm 		if (!clear && needsupdate)
27996185db85Sdougm 			ret = sa_zfs_update((sa_share_t)group);
28006185db85Sdougm 	} else {
2801549ec3ffSdougm 		impl_handle = (sa_handle_impl_t)sa_find_group_handle(group);
2802549ec3ffSdougm 		if (impl_handle != NULL) {
280357b448deSdougm 			if (clear) {
280457b448deSdougm 				(void) sa_abort_transaction(
280557b448deSdougm 				    impl_handle->scfhandle);
280657b448deSdougm 			} else {
280757b448deSdougm 				ret = sa_end_transaction(
28085b6e0c46Sdougm 				    impl_handle->scfhandle, impl_handle);
280957b448deSdougm 			}
2810549ec3ffSdougm 		} else {
2811549ec3ffSdougm 			ret = SA_SYSTEM_ERR;
2812549ec3ffSdougm 		}
28136185db85Sdougm 	}
28146185db85Sdougm 	return (ret);
28156185db85Sdougm }
28166185db85Sdougm 
28176185db85Sdougm /*
28186185db85Sdougm  * sa_destroy_optionset(optionset)
28196185db85Sdougm  *
2820da6c28aaSamw  * Remove the optionset from its group. Update the repository to
28216185db85Sdougm  * reflect this change.
28226185db85Sdougm  */
28236185db85Sdougm 
28246185db85Sdougm int
sa_destroy_optionset(sa_optionset_t optionset)28256185db85Sdougm sa_destroy_optionset(sa_optionset_t optionset)
28266185db85Sdougm {
282757b448deSdougm 	char name[SA_STRSIZE];
28286185db85Sdougm 	int len;
28296185db85Sdougm 	int ret;
28306185db85Sdougm 	char *id = NULL;
28316185db85Sdougm 	sa_group_t group;
28326185db85Sdougm 	int ispersist = 1;
28336185db85Sdougm 
28346185db85Sdougm 	/* now delete the prop group */
28356185db85Sdougm 	group = sa_get_optionset_parent(optionset);
2836da6c28aaSamw 	if (group != NULL) {
2837da6c28aaSamw 		if (sa_is_resource(group)) {
2838da6c28aaSamw 			sa_resource_t resource = group;
2839da6c28aaSamw 			sa_share_t share = sa_get_resource_parent(resource);
2840da6c28aaSamw 			group = sa_get_parent_group(share);
2841da6c28aaSamw 			id = sa_get_share_attr(share, "id");
2842da6c28aaSamw 		} else if (sa_is_share(group)) {
28436185db85Sdougm 			id = sa_get_share_attr((sa_share_t)group, "id");
28446185db85Sdougm 		}
2845da6c28aaSamw 		ispersist = sa_is_persistent(group);
2846da6c28aaSamw 	}
28476185db85Sdougm 	if (ispersist) {
2848549ec3ffSdougm 		sa_handle_impl_t impl_handle;
28496185db85Sdougm 		len = sa_optionset_name(optionset, name, sizeof (name), id);
2850549ec3ffSdougm 		impl_handle = (sa_handle_impl_t)sa_find_group_handle(group);
2851549ec3ffSdougm 		if (impl_handle != NULL) {
28526185db85Sdougm 			if (len > 0) {
285357b448deSdougm 				ret = sa_delete_pgroup(impl_handle->scfhandle,
285457b448deSdougm 				    name);
2855549ec3ffSdougm 			}
2856549ec3ffSdougm 		} else {
2857549ec3ffSdougm 			ret = SA_SYSTEM_ERR;
28586185db85Sdougm 		}
28596185db85Sdougm 	}
28606185db85Sdougm 	xmlUnlinkNode((xmlNodePtr)optionset);
28616185db85Sdougm 	xmlFreeNode((xmlNodePtr)optionset);
28626185db85Sdougm 	if (id != NULL)
28636185db85Sdougm 		sa_free_attr_string(id);
28646185db85Sdougm 	return (ret);
28656185db85Sdougm }
28666185db85Sdougm 
28676185db85Sdougm /* private to the implementation */
28686185db85Sdougm int
_sa_remove_optionset(sa_optionset_t optionset)28696185db85Sdougm _sa_remove_optionset(sa_optionset_t optionset)
28706185db85Sdougm {
28716185db85Sdougm 	int ret = SA_OK;
28726185db85Sdougm 
28736185db85Sdougm 	xmlUnlinkNode((xmlNodePtr)optionset);
28746185db85Sdougm 	xmlFreeNode((xmlNodePtr)optionset);
28756185db85Sdougm 	return (ret);
28766185db85Sdougm }
28776185db85Sdougm 
28786185db85Sdougm /*
28796185db85Sdougm  * sa_create_security(group, sectype, proto)
28806185db85Sdougm  *
28816185db85Sdougm  * Create a security optionset (one that has a type name and a
28826185db85Sdougm  * proto). Security is left over from a pure NFS implementation. The
28836185db85Sdougm  * naming will change in the future when the API is released.
28846185db85Sdougm  */
28856185db85Sdougm sa_security_t
sa_create_security(sa_group_t group,char * sectype,char * proto)28866185db85Sdougm sa_create_security(sa_group_t group, char *sectype, char *proto)
28876185db85Sdougm {
28886185db85Sdougm 	sa_security_t security;
28896185db85Sdougm 	char *id = NULL;
28906185db85Sdougm 	sa_group_t parent;
28916185db85Sdougm 	char *groupname = NULL;
28926185db85Sdougm 
28936185db85Sdougm 	if (group != NULL && sa_is_share(group)) {
28946185db85Sdougm 		id = sa_get_share_attr((sa_share_t)group, "id");
28956185db85Sdougm 		parent = sa_get_parent_group(group);
28966185db85Sdougm 		if (parent != NULL)
28976185db85Sdougm 			groupname = sa_get_group_attr(parent, "name");
28986185db85Sdougm 	} else if (group != NULL) {
28996185db85Sdougm 		groupname = sa_get_group_attr(group, "name");
29006185db85Sdougm 	}
29016185db85Sdougm 
29026185db85Sdougm 	security = sa_get_security(group, sectype, proto);
29036185db85Sdougm 	if (security != NULL) {
29046185db85Sdougm 		/* can't have a duplicate security option */
29056185db85Sdougm 		security = NULL;
29066185db85Sdougm 	} else {
29076185db85Sdougm 		security = (sa_security_t)xmlNewChild((xmlNodePtr)group,
290857b448deSdougm 		    NULL, (xmlChar *)"security", NULL);
29096185db85Sdougm 		if (security != NULL) {
291057b448deSdougm 			char oname[SA_STRSIZE];
29116185db85Sdougm 			sa_set_security_attr(security, "type", proto);
29126185db85Sdougm 
29136185db85Sdougm 			sa_set_security_attr(security, "sectype", sectype);
29146185db85Sdougm 			(void) sa_security_name(security, oname,
29156185db85Sdougm 			    sizeof (oname), id);
2916da6c28aaSamw 			if (groupname != NULL && sa_is_persistent(group)) {
2917549ec3ffSdougm 				sa_handle_impl_t impl_handle;
2918549ec3ffSdougm 				impl_handle =
291957b448deSdougm 				    (sa_handle_impl_t)sa_find_group_handle(
292057b448deSdougm 				    group);
2921549ec3ffSdougm 				if (impl_handle != NULL) {
292257b448deSdougm 					(void) sa_get_instance(
292357b448deSdougm 					    impl_handle->scfhandle, groupname);
292457b448deSdougm 					(void) sa_create_pgroup(
292557b448deSdougm 					    impl_handle->scfhandle, oname);
2926549ec3ffSdougm 				}
29276185db85Sdougm 			}
29286185db85Sdougm 		}
29296185db85Sdougm 	}
2930fe1c642dSBill Krier 	if (id != NULL)
2931fe1c642dSBill Krier 		sa_free_attr_string(id);
29326185db85Sdougm 	if (groupname != NULL)
29336185db85Sdougm 		sa_free_attr_string(groupname);
29346185db85Sdougm 	return (security);
29356185db85Sdougm }
29366185db85Sdougm 
29376185db85Sdougm /*
29386185db85Sdougm  * sa_destroy_security(security)
29396185db85Sdougm  *
29406185db85Sdougm  * Remove the specified optionset from the document and the
29416185db85Sdougm  * configuration.
29426185db85Sdougm  */
29436185db85Sdougm 
29446185db85Sdougm int
sa_destroy_security(sa_security_t security)29456185db85Sdougm sa_destroy_security(sa_security_t security)
29466185db85Sdougm {
294757b448deSdougm 	char name[SA_STRSIZE];
29486185db85Sdougm 	int len;
29496185db85Sdougm 	int ret = SA_OK;
29506185db85Sdougm 	char *id = NULL;
29516185db85Sdougm 	sa_group_t group;
29526185db85Sdougm 	int iszfs = 0;
29536185db85Sdougm 	int ispersist = 1;
29546185db85Sdougm 
29556185db85Sdougm 	group = sa_get_optionset_parent(security);
29566185db85Sdougm 
29576185db85Sdougm 	if (group != NULL)
29586185db85Sdougm 		iszfs = sa_group_is_zfs(group);
29596185db85Sdougm 
29606185db85Sdougm 	if (group != NULL && !iszfs) {
29616185db85Sdougm 		if (sa_is_share(group))
2962da6c28aaSamw 			ispersist = sa_is_persistent(group);
29636185db85Sdougm 		id = sa_get_share_attr((sa_share_t)group, "id");
29646185db85Sdougm 	}
29656185db85Sdougm 	if (ispersist) {
29666185db85Sdougm 		len = sa_security_name(security, name, sizeof (name), id);
29676185db85Sdougm 		if (!iszfs && len > 0) {
2968549ec3ffSdougm 			sa_handle_impl_t impl_handle;
296957b448deSdougm 			impl_handle =
297057b448deSdougm 			    (sa_handle_impl_t)sa_find_group_handle(group);
2971549ec3ffSdougm 			if (impl_handle != NULL) {
297257b448deSdougm 				ret = sa_delete_pgroup(impl_handle->scfhandle,
297357b448deSdougm 				    name);
2974549ec3ffSdougm 			} else {
2975549ec3ffSdougm 				ret = SA_SYSTEM_ERR;
2976549ec3ffSdougm 			}
29776185db85Sdougm 		}
29786185db85Sdougm 	}
29796185db85Sdougm 	xmlUnlinkNode((xmlNodePtr)security);
29806185db85Sdougm 	xmlFreeNode((xmlNodePtr)security);
298157b448deSdougm 	if (iszfs)
29826185db85Sdougm 		ret = sa_zfs_update(group);
29836185db85Sdougm 	if (id != NULL)
29846185db85Sdougm 		sa_free_attr_string(id);
29856185db85Sdougm 	return (ret);
29866185db85Sdougm }
29876185db85Sdougm 
29886185db85Sdougm /*
29896185db85Sdougm  * sa_get_security_attr(optionset, tag)
29906185db85Sdougm  *
29916185db85Sdougm  * Return the specified attribute value from the optionset.
29926185db85Sdougm  */
29936185db85Sdougm 
29946185db85Sdougm char *
sa_get_security_attr(sa_property_t optionset,char * tag)29956185db85Sdougm sa_get_security_attr(sa_property_t optionset, char *tag)
29966185db85Sdougm {
29976185db85Sdougm 	return (get_node_attr((void *)optionset, tag));
29986185db85Sdougm 
29996185db85Sdougm }
30006185db85Sdougm 
30016185db85Sdougm /*
30026185db85Sdougm  * sa_set_security_attr(optionset, tag, value)
30036185db85Sdougm  *
30046185db85Sdougm  * Set the optioset attribute specied by tag to the specified value.
30056185db85Sdougm  */
30066185db85Sdougm 
30076185db85Sdougm void
sa_set_security_attr(sa_group_t optionset,char * tag,char * value)30086185db85Sdougm sa_set_security_attr(sa_group_t optionset, char *tag, char *value)
30096185db85Sdougm {
30106185db85Sdougm 	set_node_attr((void *)optionset, tag, value);
30116185db85Sdougm }
30126185db85Sdougm 
30136185db85Sdougm /*
30146185db85Sdougm  * is_nodetype(node, type)
30156185db85Sdougm  *
30166185db85Sdougm  * Check to see if node is of the type specified.
30176185db85Sdougm  */
30186185db85Sdougm 
30196185db85Sdougm static int
is_nodetype(void * node,char * type)30206185db85Sdougm is_nodetype(void *node, char *type)
30216185db85Sdougm {
30226185db85Sdougm 	return (strcmp((char *)((xmlNodePtr)node)->name, type) == 0);
30236185db85Sdougm }
30246185db85Sdougm 
302557b448deSdougm /*
302657b448deSdougm  * add_or_update()
302757b448deSdougm  *
302857b448deSdougm  * Add or update a property. Pulled out of sa_set_prop_by_prop for
302957b448deSdougm  * readability.
303057b448deSdougm  */
303157b448deSdougm static int
add_or_update(scfutilhandle_t * scf_handle,int type,scf_value_t * value,scf_transaction_entry_t * entry,char * name,char * valstr)303257b448deSdougm add_or_update(scfutilhandle_t *scf_handle, int type, scf_value_t *value,
303357b448deSdougm     scf_transaction_entry_t *entry, char *name, char *valstr)
303457b448deSdougm {
303557b448deSdougm 	int ret = SA_SYSTEM_ERR;
303657b448deSdougm 
303757b448deSdougm 	if (value != NULL) {
303857b448deSdougm 		if (type == SA_PROP_OP_ADD)
303957b448deSdougm 			ret = scf_transaction_property_new(scf_handle->trans,
304057b448deSdougm 			    entry, name, SCF_TYPE_ASTRING);
304157b448deSdougm 		else
304257b448deSdougm 			ret = scf_transaction_property_change(scf_handle->trans,
304357b448deSdougm 			    entry, name, SCF_TYPE_ASTRING);
304457b448deSdougm 		if (ret == 0) {
304557b448deSdougm 			ret = scf_value_set_astring(value, valstr);
304657b448deSdougm 			if (ret == 0)
304757b448deSdougm 				ret = scf_entry_add_value(entry, value);
304857b448deSdougm 			if (ret == 0)
304957b448deSdougm 				return (ret);
305057b448deSdougm 			scf_value_destroy(value);
305157b448deSdougm 		} else {
305257b448deSdougm 			scf_entry_destroy(entry);
305357b448deSdougm 		}
305457b448deSdougm 	}
305557b448deSdougm 	return (SA_SYSTEM_ERR);
305657b448deSdougm }
305757b448deSdougm 
30586185db85Sdougm /*
30596185db85Sdougm  * sa_set_prop_by_prop(optionset, group, prop, type)
30606185db85Sdougm  *
30616185db85Sdougm  * Add/remove/update the specified property prop into the optionset or
30626185db85Sdougm  * share. If a share, sort out which property group based on GUID. In
30636185db85Sdougm  * all cases, the appropriate transaction is set (or ZFS share is
30646185db85Sdougm  * marked as needing an update)
30656185db85Sdougm  */
30666185db85Sdougm 
30676185db85Sdougm static int
sa_set_prop_by_prop(sa_optionset_t optionset,sa_group_t group,sa_property_t prop,int type)30686185db85Sdougm sa_set_prop_by_prop(sa_optionset_t optionset, sa_group_t group,
30696185db85Sdougm 			sa_property_t prop, int type)
30706185db85Sdougm {
30716185db85Sdougm 	char *name;
30726185db85Sdougm 	char *valstr;
30736185db85Sdougm 	int ret = SA_OK;
30746185db85Sdougm 	scf_transaction_entry_t *entry;
30756185db85Sdougm 	scf_value_t *value;
30766185db85Sdougm 	int opttype; /* 1 == optionset, 0 == security */
30776185db85Sdougm 	char *id = NULL;
30786185db85Sdougm 	int iszfs = 0;
30796185db85Sdougm 	sa_group_t parent = NULL;
3080da6c28aaSamw 	sa_share_t share = NULL;
3081549ec3ffSdougm 	sa_handle_impl_t impl_handle;
3082549ec3ffSdougm 	scfutilhandle_t  *scf_handle;
30836185db85Sdougm 
3084da6c28aaSamw 	if (!sa_is_persistent(group)) {
30856185db85Sdougm 		/*
30866185db85Sdougm 		 * if the group/share is not persistent we don't need
30876185db85Sdougm 		 * to do anything here
30886185db85Sdougm 		 */
30896185db85Sdougm 		return (SA_OK);
30906185db85Sdougm 	}
3091549ec3ffSdougm 	impl_handle = (sa_handle_impl_t)sa_find_group_handle(group);
309257b448deSdougm 	if (impl_handle == NULL || impl_handle->scfhandle == NULL)
3093549ec3ffSdougm 		return (SA_SYSTEM_ERR);
3094549ec3ffSdougm 	scf_handle = impl_handle->scfhandle;
30956185db85Sdougm 	name = sa_get_property_attr(prop, "type");
30966185db85Sdougm 	valstr = sa_get_property_attr(prop, "value");
30976185db85Sdougm 	entry = scf_entry_create(scf_handle->handle);
30986185db85Sdougm 	opttype = is_nodetype((void *)optionset, "optionset");
30996185db85Sdougm 
3100da6c28aaSamw 	/*
3101da6c28aaSamw 	 * Check for share vs. resource since they need slightly
3102da6c28aaSamw 	 * different treatment given the hierarchy.
3103da6c28aaSamw 	 */
31046185db85Sdougm 	if (valstr != NULL && entry != NULL) {
31056185db85Sdougm 		if (sa_is_share(group)) {
31066185db85Sdougm 			parent = sa_get_parent_group(group);
3107da6c28aaSamw 			share = (sa_share_t)group;
310857b448deSdougm 			if (parent != NULL)
31096185db85Sdougm 				iszfs = is_zfs_group(parent);
3110da6c28aaSamw 		} else if (sa_is_resource(group)) {
3111da6c28aaSamw 			share = sa_get_parent_group(group);
3112da6c28aaSamw 			if (share != NULL)
3113da6c28aaSamw 				parent = sa_get_parent_group(share);
31146185db85Sdougm 		} else {
31156185db85Sdougm 			iszfs = is_zfs_group(group);
31166185db85Sdougm 		}
31176185db85Sdougm 		if (!iszfs) {
31186185db85Sdougm 			if (scf_handle->trans == NULL) {
311957b448deSdougm 				char oname[SA_STRSIZE];
31206185db85Sdougm 				char *groupname = NULL;
3121da6c28aaSamw 				if (share != NULL) {
3122da6c28aaSamw 					if (parent != NULL)
312357b448deSdougm 						groupname =
312457b448deSdougm 						    sa_get_group_attr(parent,
312557b448deSdougm 						    "name");
3126da6c28aaSamw 					id = sa_get_share_attr(
3127da6c28aaSamw 					    (sa_share_t)share, "id");
31286185db85Sdougm 				} else {
312957b448deSdougm 					groupname = sa_get_group_attr(group,
313057b448deSdougm 					    "name");
31316185db85Sdougm 				}
31326185db85Sdougm 				if (groupname != NULL) {
313357b448deSdougm 					ret = sa_get_instance(scf_handle,
313457b448deSdougm 					    groupname);
31356185db85Sdougm 					sa_free_attr_string(groupname);
31366185db85Sdougm 				}
31376185db85Sdougm 				if (opttype)
313857b448deSdougm 					(void) sa_optionset_name(optionset,
313957b448deSdougm 					    oname, sizeof (oname), id);
31406185db85Sdougm 				else
314157b448deSdougm 					(void) sa_security_name(optionset,
314257b448deSdougm 					    oname, sizeof (oname), id);
31436185db85Sdougm 				ret = sa_start_transaction(scf_handle, oname);
3144fe1c642dSBill Krier 				if (id != NULL)
3145fe1c642dSBill Krier 					sa_free_attr_string(id);
31466185db85Sdougm 			}
31476185db85Sdougm 			if (ret == SA_OK) {
31486185db85Sdougm 				switch (type) {
31496185db85Sdougm 				case SA_PROP_OP_REMOVE:
3150549ec3ffSdougm 					ret = scf_transaction_property_delete(
315157b448deSdougm 					    scf_handle->trans, entry, name);
31526185db85Sdougm 					break;
31536185db85Sdougm 				case SA_PROP_OP_ADD:
31546185db85Sdougm 				case SA_PROP_OP_UPDATE:
315557b448deSdougm 					value = scf_value_create(
315657b448deSdougm 					    scf_handle->handle);
315757b448deSdougm 					ret = add_or_update(scf_handle, type,
315857b448deSdougm 					    value, entry, name, valstr);
31596185db85Sdougm 					break;
31606185db85Sdougm 				}
31616185db85Sdougm 			}
31626185db85Sdougm 		} else {
31636185db85Sdougm 			/*
31646185db85Sdougm 			 * ZFS update. The calling function would have updated
31656185db85Sdougm 			 * the internal XML structure. Just need to flag it as
31666185db85Sdougm 			 * changed for ZFS.
31676185db85Sdougm 			 */
31686185db85Sdougm 			zfs_set_update((sa_share_t)group);
31696185db85Sdougm 		}
31706185db85Sdougm 	}
31716185db85Sdougm 
31726185db85Sdougm 	if (name != NULL)
31736185db85Sdougm 		sa_free_attr_string(name);
31746185db85Sdougm 	if (valstr != NULL)
31756185db85Sdougm 		sa_free_attr_string(valstr);
31766185db85Sdougm 	else if (entry != NULL)
31776185db85Sdougm 		scf_entry_destroy(entry);
31786185db85Sdougm 
31796185db85Sdougm 	if (ret == -1)
31806185db85Sdougm 		ret = SA_SYSTEM_ERR;
31816185db85Sdougm 
31826185db85Sdougm 	return (ret);
31836185db85Sdougm }
31846185db85Sdougm 
31856185db85Sdougm /*
31864bff34e3Sthurlow  * sa_create_section(name, value)
31874bff34e3Sthurlow  *
31884bff34e3Sthurlow  * Create a new section with the specified name and extra data.
31894bff34e3Sthurlow  */
31904bff34e3Sthurlow 
31914bff34e3Sthurlow sa_property_t
sa_create_section(char * name,char * extra)31924bff34e3Sthurlow sa_create_section(char *name, char *extra)
31934bff34e3Sthurlow {
31944bff34e3Sthurlow 	xmlNodePtr node;
31954bff34e3Sthurlow 
31964bff34e3Sthurlow 	node = xmlNewNode(NULL, (xmlChar *)"section");
31974bff34e3Sthurlow 	if (node != NULL) {
31984bff34e3Sthurlow 		if (name != NULL)
31994bff34e3Sthurlow 			(void) xmlSetProp(node, (xmlChar *)"name",
32004bff34e3Sthurlow 			    (xmlChar *)name);
32014bff34e3Sthurlow 		if (extra != NULL)
32024bff34e3Sthurlow 			(void) xmlSetProp(node, (xmlChar *)"extra",
32034bff34e3Sthurlow 			    (xmlChar *)extra);
32044bff34e3Sthurlow 	}
32054bff34e3Sthurlow 	return ((sa_property_t)node);
32064bff34e3Sthurlow }
32074bff34e3Sthurlow 
32084bff34e3Sthurlow void
sa_set_section_attr(sa_property_t sect,char * name,char * value)32094bff34e3Sthurlow sa_set_section_attr(sa_property_t sect, char *name, char *value)
32104bff34e3Sthurlow {
32114bff34e3Sthurlow 	(void) xmlSetProp(sect, (xmlChar *)name, (xmlChar *)value);
32124bff34e3Sthurlow }
32134bff34e3Sthurlow 
32144bff34e3Sthurlow /*
32154bff34e3Sthurlow  * sa_create_property(section, name, value)
32166185db85Sdougm  *
32176185db85Sdougm  * Create a new property with the specified name and value.
32186185db85Sdougm  */
32196185db85Sdougm 
32206185db85Sdougm sa_property_t
sa_create_property(char * name,char * value)32216185db85Sdougm sa_create_property(char *name, char *value)
32226185db85Sdougm {
32236185db85Sdougm 	xmlNodePtr node;
32246185db85Sdougm 
32256185db85Sdougm 	node = xmlNewNode(NULL, (xmlChar *)"option");
32266185db85Sdougm 	if (node != NULL) {
32274bff34e3Sthurlow 		(void) xmlSetProp(node, (xmlChar *)"type", (xmlChar *)name);
32284bff34e3Sthurlow 		(void) xmlSetProp(node, (xmlChar *)"value", (xmlChar *)value);
32296185db85Sdougm 	}
32306185db85Sdougm 	return ((sa_property_t)node);
32316185db85Sdougm }
32326185db85Sdougm 
32336185db85Sdougm /*
32346185db85Sdougm  * sa_add_property(object, property)
32356185db85Sdougm  *
32366185db85Sdougm  * Add the specified property to the object. Issue the appropriate
32376185db85Sdougm  * transaction or mark a ZFS object as needing an update.
32386185db85Sdougm  */
32396185db85Sdougm 
32406185db85Sdougm int
sa_add_property(void * object,sa_property_t property)32416185db85Sdougm sa_add_property(void *object, sa_property_t property)
32426185db85Sdougm {
32436185db85Sdougm 	int ret = SA_OK;
32446185db85Sdougm 	sa_group_t parent;
32456185db85Sdougm 	sa_group_t group;
32466185db85Sdougm 	char *proto;
32476185db85Sdougm 
32486185db85Sdougm 	if (property != NULL) {
32493b61b335Sdougm 		sa_handle_t handle;
3250687915e9Sdougm 		handle = sa_find_group_handle((sa_group_t)object);
32513b61b335Sdougm 		/* It is legitimate to not find a handle */
3252687915e9Sdougm 		proto = sa_get_optionset_attr(object, "type");
3253687915e9Sdougm 		if ((ret = sa_valid_property(handle, object, proto,
3254687915e9Sdougm 		    property)) == SA_OK) {
325557b448deSdougm 			property = (sa_property_t)xmlAddChild(
325657b448deSdougm 			    (xmlNodePtr)object, (xmlNodePtr)property);
32576185db85Sdougm 		} else {
32586185db85Sdougm 			if (proto != NULL)
32596185db85Sdougm 				sa_free_attr_string(proto);
32606185db85Sdougm 			return (ret);
32616185db85Sdougm 		}
32626185db85Sdougm 		if (proto != NULL)
32636185db85Sdougm 			sa_free_attr_string(proto);
3264687915e9Sdougm 	}
3265687915e9Sdougm 
32666185db85Sdougm 
32676185db85Sdougm 	parent = sa_get_parent_group(object);
3268da6c28aaSamw 	if (!sa_is_persistent(parent))
32696185db85Sdougm 		return (ret);
32706185db85Sdougm 
3271da6c28aaSamw 	if (sa_is_resource(parent)) {
3272da6c28aaSamw 		/*
3273da6c28aaSamw 		 * Resources are children of share.  Need to go up two
3274da6c28aaSamw 		 * levels to find the group but the parent needs to be
3275da6c28aaSamw 		 * the share at this point in order to get the "id".
3276da6c28aaSamw 		 */
3277da6c28aaSamw 		parent = sa_get_parent_group(parent);
32786185db85Sdougm 		group = sa_get_parent_group(parent);
3279da6c28aaSamw 	} else if (sa_is_share(parent)) {
3280da6c28aaSamw 		group = sa_get_parent_group(parent);
3281da6c28aaSamw 	} else {
32826185db85Sdougm 		group = parent;
3283da6c28aaSamw 	}
32846185db85Sdougm 
328557b448deSdougm 	if (property == NULL) {
32866185db85Sdougm 		ret = SA_NO_MEMORY;
328757b448deSdougm 	} else {
328857b448deSdougm 		char oname[SA_STRSIZE];
32896185db85Sdougm 
32906185db85Sdougm 		if (!is_zfs_group(group)) {
32916185db85Sdougm 			char *id = NULL;
3292549ec3ffSdougm 			sa_handle_impl_t impl_handle;
3293549ec3ffSdougm 			scfutilhandle_t  *scf_handle;
3294549ec3ffSdougm 
329557b448deSdougm 			impl_handle = (sa_handle_impl_t)sa_find_group_handle(
329657b448deSdougm 			    group);
329757b448deSdougm 			if (impl_handle == NULL ||
329857b448deSdougm 			    impl_handle->scfhandle == NULL)
3299549ec3ffSdougm 				ret = SA_SYSTEM_ERR;
3300549ec3ffSdougm 			if (ret == SA_OK) {
3301549ec3ffSdougm 				scf_handle = impl_handle->scfhandle;
33026185db85Sdougm 				if (sa_is_share((sa_group_t)parent)) {
330357b448deSdougm 					id = sa_get_share_attr(
330457b448deSdougm 					    (sa_share_t)parent, "id");
33056185db85Sdougm 				}
33066185db85Sdougm 				if (scf_handle->trans == NULL) {
330757b448deSdougm 					if (is_nodetype(object, "optionset")) {
330857b448deSdougm 						(void) sa_optionset_name(
330957b448deSdougm 						    (sa_optionset_t)object,
33106185db85Sdougm 						    oname, sizeof (oname), id);
331157b448deSdougm 					} else {
331257b448deSdougm 						(void) sa_security_name(
331357b448deSdougm 						    (sa_optionset_t)object,
33146185db85Sdougm 						    oname, sizeof (oname), id);
331557b448deSdougm 					}
331657b448deSdougm 					ret = sa_start_transaction(scf_handle,
331757b448deSdougm 					    oname);
33186185db85Sdougm 				}
33196185db85Sdougm 				if (ret == SA_OK) {
33206185db85Sdougm 					char *name;
33216185db85Sdougm 					char *value;
332257b448deSdougm 					name = sa_get_property_attr(property,
332357b448deSdougm 					    "type");
332457b448deSdougm 					value = sa_get_property_attr(property,
332557b448deSdougm 					    "value");
33266185db85Sdougm 					if (name != NULL && value != NULL) {
332757b448deSdougm 						if (scf_handle->scf_state ==
332857b448deSdougm 						    SCH_STATE_INIT) {
332957b448deSdougm 							ret = sa_set_property(
333057b448deSdougm 							    scf_handle, name,
333157b448deSdougm 							    value);
333257b448deSdougm 						}
333357b448deSdougm 					} else {
33346185db85Sdougm 						ret = SA_CONFIG_ERR;
333557b448deSdougm 					}
33366185db85Sdougm 					if (name != NULL)
333757b448deSdougm 						sa_free_attr_string(
333857b448deSdougm 						    name);
33396185db85Sdougm 					if (value != NULL)
33406185db85Sdougm 						sa_free_attr_string(value);
33416185db85Sdougm 				}
33426185db85Sdougm 				if (id != NULL)
33436185db85Sdougm 					sa_free_attr_string(id);
3344549ec3ffSdougm 			}
33456185db85Sdougm 		} else {
33466185db85Sdougm 			/*
334757b448deSdougm 			 * ZFS is a special case. We do want
334857b448deSdougm 			 * to allow editing property/security
334957b448deSdougm 			 * lists since we can have a better
335057b448deSdougm 			 * syntax and we also want to keep
335157b448deSdougm 			 * things consistent when possible.
33526185db85Sdougm 			 *
335357b448deSdougm 			 * Right now, we defer until the
335457b448deSdougm 			 * sa_commit_properties so we can get
335557b448deSdougm 			 * them all at once. We do need to
335657b448deSdougm 			 * mark the share as "changed"
33576185db85Sdougm 			 */
33586185db85Sdougm 			zfs_set_update((sa_share_t)parent);
33596185db85Sdougm 		}
33606185db85Sdougm 	}
33616185db85Sdougm 	return (ret);
33626185db85Sdougm }
33636185db85Sdougm 
33646185db85Sdougm /*
33656185db85Sdougm  * sa_remove_property(property)
33666185db85Sdougm  *
33676185db85Sdougm  * Remove the specied property from its containing object. Update the
33686185db85Sdougm  * repository as appropriate.
33696185db85Sdougm  */
33706185db85Sdougm 
33716185db85Sdougm int
sa_remove_property(sa_property_t property)33726185db85Sdougm sa_remove_property(sa_property_t property)
33736185db85Sdougm {
33746185db85Sdougm 	int ret = SA_OK;
33756185db85Sdougm 
33766185db85Sdougm 	if (property != NULL) {
33776185db85Sdougm 		sa_optionset_t optionset;
33786185db85Sdougm 		sa_group_t group;
33796185db85Sdougm 		optionset = sa_get_property_parent(property);
33806185db85Sdougm 		if (optionset != NULL) {
33816185db85Sdougm 			group = sa_get_optionset_parent(optionset);
33826185db85Sdougm 			if (group != NULL) {
338357b448deSdougm 				ret = sa_set_prop_by_prop(optionset, group,
338457b448deSdougm 				    property, SA_PROP_OP_REMOVE);
33856185db85Sdougm 			}
33866185db85Sdougm 		}
33876185db85Sdougm 		xmlUnlinkNode((xmlNodePtr)property);
33886185db85Sdougm 		xmlFreeNode((xmlNodePtr)property);
33896185db85Sdougm 	} else {
33906185db85Sdougm 		ret = SA_NO_SUCH_PROP;
33916185db85Sdougm 	}
33926185db85Sdougm 	return (ret);
33936185db85Sdougm }
33946185db85Sdougm 
33956185db85Sdougm /*
33966185db85Sdougm  * sa_update_property(property, value)
33976185db85Sdougm  *
33986185db85Sdougm  * Update the specified property to the new value.  If value is NULL,
33996185db85Sdougm  * we currently treat this as a remove.
34006185db85Sdougm  */
34016185db85Sdougm 
34026185db85Sdougm int
sa_update_property(sa_property_t property,char * value)34036185db85Sdougm sa_update_property(sa_property_t property, char *value)
34046185db85Sdougm {
34056185db85Sdougm 	int ret = SA_OK;
34066185db85Sdougm 	if (value == NULL) {
34076185db85Sdougm 		return (sa_remove_property(property));
34086185db85Sdougm 	} else {
34096185db85Sdougm 		sa_optionset_t optionset;
34106185db85Sdougm 		sa_group_t group;
34116185db85Sdougm 		set_node_attr((void *)property, "value", value);
34126185db85Sdougm 		optionset = sa_get_property_parent(property);
34136185db85Sdougm 		if (optionset != NULL) {
34146185db85Sdougm 			group = sa_get_optionset_parent(optionset);
34156185db85Sdougm 			if (group != NULL) {
341657b448deSdougm 				ret = sa_set_prop_by_prop(optionset, group,
341757b448deSdougm 				    property, SA_PROP_OP_UPDATE);
34186185db85Sdougm 			}
34196185db85Sdougm 		} else {
34206185db85Sdougm 			ret = SA_NO_SUCH_PROP;
34216185db85Sdougm 		}
34226185db85Sdougm 	}
34236185db85Sdougm 	return (ret);
34246185db85Sdougm }
34256185db85Sdougm 
34266185db85Sdougm /*
34274bff34e3Sthurlow  * sa_get_protocol_section(propset, prop)
34284bff34e3Sthurlow  *
34294bff34e3Sthurlow  * Get the specified protocol specific section. These are global to
34304bff34e3Sthurlow  * the protocol and not specific to a group or share.
34314bff34e3Sthurlow  */
34324bff34e3Sthurlow 
34334bff34e3Sthurlow sa_protocol_properties_t
sa_get_protocol_section(sa_protocol_properties_t propset,char * section)34344bff34e3Sthurlow sa_get_protocol_section(sa_protocol_properties_t propset, char *section)
34354bff34e3Sthurlow {
34364bff34e3Sthurlow 	xmlNodePtr node = (xmlNodePtr)propset;
34374bff34e3Sthurlow 	xmlChar *value = NULL;
34384bff34e3Sthurlow 	char *proto;
34394bff34e3Sthurlow 
34404bff34e3Sthurlow 	proto = sa_get_optionset_attr(propset, "type");
34419c9af259SGordon Ross 	if ((sa_proto_get_featureset(proto) & SA_FEATURE_HAS_SECTIONS) == 0) {
34429c9af259SGordon Ross 		if (proto != NULL)
34439c9af259SGordon Ross 			sa_free_attr_string(proto);
34444bff34e3Sthurlow 		return (propset);
34459c9af259SGordon Ross 	}
34464bff34e3Sthurlow 
34474bff34e3Sthurlow 	for (node = node->children; node != NULL;
34484bff34e3Sthurlow 	    node = node->next) {
34494bff34e3Sthurlow 		if (xmlStrcmp(node->name, (xmlChar *)"section") == 0) {
34504bff34e3Sthurlow 			if (section == NULL)
34514bff34e3Sthurlow 				break;
34524bff34e3Sthurlow 			value = xmlGetProp(node, (xmlChar *)"name");
34534bff34e3Sthurlow 			if (value != NULL &&
34544bff34e3Sthurlow 			    xmlStrcasecmp(value, (xmlChar *)section) == 0) {
34554bff34e3Sthurlow 				break;
34564bff34e3Sthurlow 			}
34574bff34e3Sthurlow 			if (value != NULL) {
34584bff34e3Sthurlow 				xmlFree(value);
34594bff34e3Sthurlow 				value = NULL;
34604bff34e3Sthurlow 			}
34614bff34e3Sthurlow 		}
34624bff34e3Sthurlow 	}
34634bff34e3Sthurlow 	if (value != NULL)
34644bff34e3Sthurlow 		xmlFree(value);
34659c9af259SGordon Ross 	if (proto != NULL)
34669c9af259SGordon Ross 		sa_free_attr_string(proto);
34674bff34e3Sthurlow 	if (node != NULL && xmlStrcmp(node->name, (xmlChar *)"section") != 0) {
34684bff34e3Sthurlow 		/*
34694bff34e3Sthurlow 		 * avoid a non option node -- it is possible to be a
34704bff34e3Sthurlow 		 * text node
34714bff34e3Sthurlow 		 */
34724bff34e3Sthurlow 		node = NULL;
34734bff34e3Sthurlow 	}
34744bff34e3Sthurlow 	return ((sa_protocol_properties_t)node);
34754bff34e3Sthurlow }
34764bff34e3Sthurlow 
34774bff34e3Sthurlow /*
34784bff34e3Sthurlow  * sa_get_next_protocol_section(prop, find)
34794bff34e3Sthurlow  *
34804bff34e3Sthurlow  * Get the next protocol specific section in the list.
34814bff34e3Sthurlow  */
34824bff34e3Sthurlow 
34834bff34e3Sthurlow sa_property_t
sa_get_next_protocol_section(sa_property_t prop,char * find)34844bff34e3Sthurlow sa_get_next_protocol_section(sa_property_t prop, char *find)
34854bff34e3Sthurlow {
34864bff34e3Sthurlow 	xmlNodePtr node;
34874bff34e3Sthurlow 	xmlChar *value = NULL;
34884bff34e3Sthurlow 	char *proto;
34894bff34e3Sthurlow 
34904bff34e3Sthurlow 	proto = sa_get_optionset_attr(prop, "type");
34919c9af259SGordon Ross 	if ((sa_proto_get_featureset(proto) & SA_FEATURE_HAS_SECTIONS) == 0) {
34929c9af259SGordon Ross 		if (proto != NULL)
34939c9af259SGordon Ross 			sa_free_attr_string(proto);
34944bff34e3Sthurlow 		return ((sa_property_t)NULL);
34959c9af259SGordon Ross 	}
34964bff34e3Sthurlow 
34974bff34e3Sthurlow 	for (node = ((xmlNodePtr)prop)->next; node != NULL;
34984bff34e3Sthurlow 	    node = node->next) {
34994bff34e3Sthurlow 		if (xmlStrcmp(node->name, (xmlChar *)"section") == 0) {
35004bff34e3Sthurlow 			if (find == NULL)
35014bff34e3Sthurlow 				break;
35024bff34e3Sthurlow 			value = xmlGetProp(node, (xmlChar *)"name");
35034bff34e3Sthurlow 			if (value != NULL &&
35044bff34e3Sthurlow 			    xmlStrcasecmp(value, (xmlChar *)find) == 0) {
35054bff34e3Sthurlow 				break;
35064bff34e3Sthurlow 			}
35074bff34e3Sthurlow 			if (value != NULL) {
35084bff34e3Sthurlow 				xmlFree(value);
35094bff34e3Sthurlow 				value = NULL;
35104bff34e3Sthurlow 			}
35114bff34e3Sthurlow 
35124bff34e3Sthurlow 		}
35134bff34e3Sthurlow 	}
35144bff34e3Sthurlow 	if (value != NULL)
35154bff34e3Sthurlow 		xmlFree(value);
35169c9af259SGordon Ross 	if (proto != NULL)
35179c9af259SGordon Ross 		sa_free_attr_string(proto);
35184bff34e3Sthurlow 	return ((sa_property_t)node);
35194bff34e3Sthurlow }
35204bff34e3Sthurlow 
35214bff34e3Sthurlow /*
35226185db85Sdougm  * sa_get_protocol_property(propset, prop)
35236185db85Sdougm  *
35246185db85Sdougm  * Get the specified protocol specific property. These are global to
35256185db85Sdougm  * the protocol and not specific to a group or share.
35266185db85Sdougm  */
35276185db85Sdougm 
35286185db85Sdougm sa_property_t
sa_get_protocol_property(sa_protocol_properties_t propset,char * prop)35296185db85Sdougm sa_get_protocol_property(sa_protocol_properties_t propset, char *prop)
35306185db85Sdougm {
35316185db85Sdougm 	xmlNodePtr node = (xmlNodePtr)propset;
35326185db85Sdougm 	xmlChar *value = NULL;
35336185db85Sdougm 
35344bff34e3Sthurlow 	if (propset == NULL)
35354bff34e3Sthurlow 		return (NULL);
35364bff34e3Sthurlow 
35376185db85Sdougm 	for (node = node->children; node != NULL;
35386185db85Sdougm 	    node = node->next) {
35396185db85Sdougm 		if (xmlStrcmp(node->name, (xmlChar *)"option") == 0) {
35406185db85Sdougm 			if (prop == NULL)
35416185db85Sdougm 				break;
35426185db85Sdougm 			value = xmlGetProp(node, (xmlChar *)"type");
35436185db85Sdougm 			if (value != NULL &&
35446185db85Sdougm 			    xmlStrcasecmp(value, (xmlChar *)prop) == 0) {
35456185db85Sdougm 				break;
35466185db85Sdougm 			}
35476185db85Sdougm 			if (value != NULL) {
35486185db85Sdougm 				xmlFree(value);
35496185db85Sdougm 				value = NULL;
35506185db85Sdougm 			}
35516185db85Sdougm 		}
35526185db85Sdougm 	}
35536185db85Sdougm 	if (value != NULL)
35546185db85Sdougm 		xmlFree(value);
35556185db85Sdougm 	if (node != NULL && xmlStrcmp(node->name, (xmlChar *)"option") != 0) {
355657b448deSdougm 		/*
355757b448deSdougm 		 * avoid a non option node -- it is possible to be a
355857b448deSdougm 		 * text node
355957b448deSdougm 		 */
35606185db85Sdougm 		node = NULL;
35616185db85Sdougm 	}
35626185db85Sdougm 	return ((sa_property_t)node);
35636185db85Sdougm }
35646185db85Sdougm 
35656185db85Sdougm /*
35666185db85Sdougm  * sa_get_next_protocol_property(prop)
35676185db85Sdougm  *
35686185db85Sdougm  * Get the next protocol specific property in the list.
35696185db85Sdougm  */
35706185db85Sdougm 
35716185db85Sdougm sa_property_t
sa_get_next_protocol_property(sa_property_t prop,char * find)35724bff34e3Sthurlow sa_get_next_protocol_property(sa_property_t prop, char *find)
35736185db85Sdougm {
35746185db85Sdougm 	xmlNodePtr node;
35754bff34e3Sthurlow 	xmlChar *value = NULL;
35766185db85Sdougm 
35776185db85Sdougm 	for (node = ((xmlNodePtr)prop)->next; node != NULL;
35786185db85Sdougm 	    node = node->next) {
35796185db85Sdougm 		if (xmlStrcmp(node->name, (xmlChar *)"option") == 0) {
35804bff34e3Sthurlow 			if (find == NULL)
35814bff34e3Sthurlow 				break;
35824bff34e3Sthurlow 			value = xmlGetProp(node, (xmlChar *)"type");
35834bff34e3Sthurlow 			if (value != NULL &&
35844bff34e3Sthurlow 			    xmlStrcasecmp(value, (xmlChar *)find) == 0) {
35856185db85Sdougm 				break;
35866185db85Sdougm 			}
35874bff34e3Sthurlow 			if (value != NULL) {
35884bff34e3Sthurlow 				xmlFree(value);
35894bff34e3Sthurlow 				value = NULL;
35906185db85Sdougm 			}
35914bff34e3Sthurlow 
35924bff34e3Sthurlow 		}
35934bff34e3Sthurlow 	}
35944bff34e3Sthurlow 	if (value != NULL)
35954bff34e3Sthurlow 		xmlFree(value);
35966185db85Sdougm 	return ((sa_property_t)node);
35976185db85Sdougm }
35986185db85Sdougm 
35996185db85Sdougm /*
36006185db85Sdougm  * sa_set_protocol_property(prop, value)
36016185db85Sdougm  *
36026185db85Sdougm  * Set the specified property to have the new value.  The protocol
36036185db85Sdougm  * specific plugin will then be called to update the property.
36046185db85Sdougm  */
36056185db85Sdougm 
36066185db85Sdougm int
sa_set_protocol_property(sa_property_t prop,char * section,char * value)36074bff34e3Sthurlow sa_set_protocol_property(sa_property_t prop, char *section, char *value)
36086185db85Sdougm {
36096185db85Sdougm 	sa_protocol_properties_t propset;
36106185db85Sdougm 	char *proto;
36116185db85Sdougm 	int ret = SA_INVALID_PROTOCOL;
36126185db85Sdougm 
36136185db85Sdougm 	propset = ((xmlNodePtr)prop)->parent;
36146185db85Sdougm 	if (propset != NULL) {
36156185db85Sdougm 		proto = sa_get_optionset_attr(propset, "type");
36166185db85Sdougm 		if (proto != NULL) {
36174bff34e3Sthurlow 			if (section != NULL)
36184bff34e3Sthurlow 				set_node_attr((xmlNodePtr)prop, "section",
36194bff34e3Sthurlow 				    section);
36206185db85Sdougm 			set_node_attr((xmlNodePtr)prop, "value", value);
36216185db85Sdougm 			ret = sa_proto_set_property(proto, prop);
36223472f5dcSdougm 			sa_free_attr_string(proto);
36236185db85Sdougm 		}
36246185db85Sdougm 	}
36256185db85Sdougm 	return (ret);
36266185db85Sdougm }
36276185db85Sdougm 
36286185db85Sdougm /*
36296185db85Sdougm  * sa_add_protocol_property(propset, prop)
36306185db85Sdougm  *
3631da6c28aaSamw  * Add a new property to the protocol specific property set.
36326185db85Sdougm  */
36336185db85Sdougm 
36346185db85Sdougm int
sa_add_protocol_property(sa_protocol_properties_t propset,sa_property_t prop)36356185db85Sdougm sa_add_protocol_property(sa_protocol_properties_t propset, sa_property_t prop)
36366185db85Sdougm {
36376185db85Sdougm 	xmlNodePtr node;
36386185db85Sdougm 
36396185db85Sdougm 	/* should check for legitimacy */
36406185db85Sdougm 	node = xmlAddChild((xmlNodePtr)propset, (xmlNodePtr)prop);
36416185db85Sdougm 	if (node != NULL)
36426185db85Sdougm 		return (SA_OK);
36436185db85Sdougm 	return (SA_NO_MEMORY);
36446185db85Sdougm }
36456185db85Sdougm 
36466185db85Sdougm /*
36476185db85Sdougm  * sa_create_protocol_properties(proto)
36486185db85Sdougm  *
3649da6c28aaSamw  * Create a protocol specific property set.
36506185db85Sdougm  */
36516185db85Sdougm 
36526185db85Sdougm sa_protocol_properties_t
sa_create_protocol_properties(char * proto)36536185db85Sdougm sa_create_protocol_properties(char *proto)
36546185db85Sdougm {
36556185db85Sdougm 	xmlNodePtr node;
365657b448deSdougm 
36576185db85Sdougm 	node = xmlNewNode(NULL, (xmlChar *)"propertyset");
365857b448deSdougm 	if (node != NULL)
36594bff34e3Sthurlow 		(void) xmlSetProp(node, (xmlChar *)"type", (xmlChar *)proto);
36606185db85Sdougm 	return (node);
36616185db85Sdougm }
3662da6c28aaSamw 
3663da6c28aaSamw /*
3664da6c28aaSamw  * sa_get_share_resource(share, resource)
3665da6c28aaSamw  *
3666da6c28aaSamw  * Get the named resource from the share, if it exists. If resource is
3667da6c28aaSamw  * NULL, get the first resource.
3668da6c28aaSamw  */
3669da6c28aaSamw 
3670da6c28aaSamw sa_resource_t
sa_get_share_resource(sa_share_t share,char * resource)3671da6c28aaSamw sa_get_share_resource(sa_share_t share, char *resource)
3672da6c28aaSamw {
3673da6c28aaSamw 	xmlNodePtr node = NULL;
3674da6c28aaSamw 	xmlChar *name;
3675da6c28aaSamw 
3676da6c28aaSamw 	if (share != NULL) {
3677da6c28aaSamw 		for (node = ((xmlNodePtr)share)->children; node != NULL;
3678da6c28aaSamw 		    node = node->next) {
3679da6c28aaSamw 			if (xmlStrcmp(node->name, (xmlChar *)"resource") == 0) {
3680da6c28aaSamw 				if (resource == NULL) {
3681da6c28aaSamw 					/*
3682da6c28aaSamw 					 * We are looking for the first
3683da6c28aaSamw 					 * resource node and not a names
3684da6c28aaSamw 					 * resource.
3685da6c28aaSamw 					 */
3686da6c28aaSamw 					break;
3687da6c28aaSamw 				} else {
3688da6c28aaSamw 					/* is it the correct share? */
3689da6c28aaSamw 					name = xmlGetProp(node,
3690da6c28aaSamw 					    (xmlChar *)"name");
3691da6c28aaSamw 					if (name != NULL &&
3692da6c28aaSamw 					    xmlStrcasecmp(name,
3693da6c28aaSamw 					    (xmlChar *)resource) == 0) {
3694da6c28aaSamw 						xmlFree(name);
3695da6c28aaSamw 						break;
3696da6c28aaSamw 					}
3697da6c28aaSamw 					xmlFree(name);
3698da6c28aaSamw 				}
3699da6c28aaSamw 			}
3700da6c28aaSamw 		}
3701da6c28aaSamw 	}
3702da6c28aaSamw 	return ((sa_resource_t)node);
3703da6c28aaSamw }
3704da6c28aaSamw 
3705da6c28aaSamw /*
3706da6c28aaSamw  * sa_get_next_resource(resource)
3707da6c28aaSamw  *	Return the next share following the specified share
3708da6c28aaSamw  *	from the internal list of shares. Returns NULL if there
3709da6c28aaSamw  *	are no more shares.  The list is relative to the same
3710da6c28aaSamw  *	group.
3711da6c28aaSamw  */
3712da6c28aaSamw sa_share_t
sa_get_next_resource(sa_resource_t resource)3713da6c28aaSamw sa_get_next_resource(sa_resource_t resource)
3714da6c28aaSamw {
3715da6c28aaSamw 	xmlNodePtr node = NULL;
3716da6c28aaSamw 
3717da6c28aaSamw 	if (resource != NULL) {
3718da6c28aaSamw 		for (node = ((xmlNodePtr)resource)->next; node != NULL;
3719da6c28aaSamw 		    node = node->next) {
3720da6c28aaSamw 			if (xmlStrcmp(node->name, (xmlChar *)"resource") == 0)
3721da6c28aaSamw 				break;
3722da6c28aaSamw 		}
3723da6c28aaSamw 	}
3724da6c28aaSamw 	return ((sa_share_t)node);
3725da6c28aaSamw }
3726da6c28aaSamw 
3727da6c28aaSamw /*
3728da6c28aaSamw  * _sa_get_next_resource_index(share)
3729da6c28aaSamw  *
3730da6c28aaSamw  * get the next resource index number (one greater then current largest)
3731da6c28aaSamw  */
3732da6c28aaSamw 
3733da6c28aaSamw static int
_sa_get_next_resource_index(sa_share_t share)3734da6c28aaSamw _sa_get_next_resource_index(sa_share_t share)
3735da6c28aaSamw {
3736da6c28aaSamw 	sa_resource_t resource;
3737da6c28aaSamw 	int index = 0;
3738da6c28aaSamw 	char *id;
3739da6c28aaSamw 
3740da6c28aaSamw 	for (resource = sa_get_share_resource(share, NULL);
3741da6c28aaSamw 	    resource != NULL;
3742da6c28aaSamw 	    resource = sa_get_next_resource(resource)) {
3743da6c28aaSamw 		id = get_node_attr((void *)resource, "id");
3744da6c28aaSamw 		if (id != NULL) {
3745da6c28aaSamw 			int val;
3746da6c28aaSamw 			val = atoi(id);
3747da6c28aaSamw 			if (val > index)
3748da6c28aaSamw 				index = val;
3749da6c28aaSamw 			sa_free_attr_string(id);
3750da6c28aaSamw 		}
3751da6c28aaSamw 	}
3752da6c28aaSamw 	return (index + 1);
3753da6c28aaSamw }
3754da6c28aaSamw 
3755da6c28aaSamw 
3756da6c28aaSamw /*
3757da6c28aaSamw  * sa_add_resource(share, resource, persist, &err)
3758da6c28aaSamw  *
3759da6c28aaSamw  * Adds a new resource name associated with share. The resource name
3760da6c28aaSamw  * must be unique in the system and will be case insensitive (eventually).
3761da6c28aaSamw  */
3762da6c28aaSamw 
3763da6c28aaSamw sa_resource_t
sa_add_resource(sa_share_t share,char * resource,int persist,int * error)3764da6c28aaSamw sa_add_resource(sa_share_t share, char *resource, int persist, int *error)
3765da6c28aaSamw {
3766da6c28aaSamw 	xmlNodePtr node;
3767da6c28aaSamw 	int err = SA_OK;
3768da6c28aaSamw 	sa_resource_t res;
3769da6c28aaSamw 	sa_group_t group;
3770da6c28aaSamw 	sa_handle_t handle;
3771da6c28aaSamw 	char istring[8]; /* just big enough for an integer value */
3772da6c28aaSamw 	int index;
3773da6c28aaSamw 
3774da6c28aaSamw 	group = sa_get_parent_group(share);
3775da6c28aaSamw 	handle = sa_find_group_handle(group);
3776da6c28aaSamw 	res = sa_find_resource(handle, resource);
3777da6c28aaSamw 	if (res != NULL) {
3778da6c28aaSamw 		err = SA_DUPLICATE_NAME;
3779da6c28aaSamw 		res = NULL;
3780da6c28aaSamw 	} else {
3781da6c28aaSamw 		node = xmlNewChild((xmlNodePtr)share, NULL,
3782da6c28aaSamw 		    (xmlChar *)"resource", NULL);
3783da6c28aaSamw 		if (node != NULL) {
37844bff34e3Sthurlow 			(void) xmlSetProp(node, (xmlChar *)"name",
3785da6c28aaSamw 			    (xmlChar *)resource);
37864bff34e3Sthurlow 			(void) xmlSetProp(node, (xmlChar *)"type", persist ?
3787da6c28aaSamw 			    (xmlChar *)"persist" : (xmlChar *)"transient");
3788da6c28aaSamw 			if (persist != SA_SHARE_TRANSIENT) {
3789da6c28aaSamw 				index = _sa_get_next_resource_index(share);
3790da6c28aaSamw 				(void) snprintf(istring, sizeof (istring), "%d",
3791da6c28aaSamw 				    index);
37924bff34e3Sthurlow 				(void) xmlSetProp(node, (xmlChar *)"id",
3793da6c28aaSamw 				    (xmlChar *)istring);
37941f713840SDoug McCallum 
37951f713840SDoug McCallum 				if (!sa_is_persistent((sa_group_t)share))
37961f713840SDoug McCallum 					goto done;
37971f713840SDoug McCallum 
37981f713840SDoug McCallum 				if (!sa_group_is_zfs(group)) {
3799da6c28aaSamw 					/* ZFS doesn't use resource names */
3800da6c28aaSamw 					sa_handle_impl_t ihandle;
38011f713840SDoug McCallum 
3802da6c28aaSamw 					ihandle = (sa_handle_impl_t)
3803da6c28aaSamw 					    sa_find_group_handle(
3804da6c28aaSamw 					    group);
3805da6c28aaSamw 					if (ihandle != NULL)
3806da6c28aaSamw 						err = sa_commit_share(
3807da6c28aaSamw 						    ihandle->scfhandle, group,
3808da6c28aaSamw 						    share);
3809da6c28aaSamw 					else
3810da6c28aaSamw 						err = SA_SYSTEM_ERR;
38111f713840SDoug McCallum 				} else {
38121f713840SDoug McCallum 					err = sa_zfs_update((sa_share_t)group);
3813da6c28aaSamw 				}
3814da6c28aaSamw 			}
3815da6c28aaSamw 		}
3816da6c28aaSamw 	}
38171f713840SDoug McCallum done:
3818da6c28aaSamw 	if (error != NULL)
3819da6c28aaSamw 		*error = err;
3820da6c28aaSamw 	return ((sa_resource_t)node);
3821da6c28aaSamw }
3822da6c28aaSamw 
3823da6c28aaSamw /*
3824da6c28aaSamw  * sa_remove_resource(resource)
3825da6c28aaSamw  *
3826da6c28aaSamw  * Remove the resource name from the share (and the system)
3827da6c28aaSamw  */
3828da6c28aaSamw 
3829da6c28aaSamw int
sa_remove_resource(sa_resource_t resource)3830da6c28aaSamw sa_remove_resource(sa_resource_t resource)
3831da6c28aaSamw {
3832da6c28aaSamw 	sa_share_t share;
3833da6c28aaSamw 	sa_group_t group;
3834da6c28aaSamw 	char *type;
3835da6c28aaSamw 	int ret = SA_OK;
38361f713840SDoug McCallum 	boolean_t transient = B_FALSE;
383755bf511dSas200622 	sa_optionset_t opt;
3838da6c28aaSamw 
3839da6c28aaSamw 	share = sa_get_resource_parent(resource);
3840da6c28aaSamw 	type = sa_get_share_attr(share, "type");
3841da6c28aaSamw 	group = sa_get_parent_group(share);
3842da6c28aaSamw 
3843da6c28aaSamw 
3844da6c28aaSamw 	if (type != NULL) {
3845da6c28aaSamw 		if (strcmp(type, "persist") != 0)
38461f713840SDoug McCallum 			transient = B_TRUE;
3847da6c28aaSamw 		sa_free_attr_string(type);
3848da6c28aaSamw 	}
3849da6c28aaSamw 
385055bf511dSas200622 	/* Disable the resource for all protocols. */
385155bf511dSas200622 	(void) sa_disable_resource(resource, NULL);
385255bf511dSas200622 
385355bf511dSas200622 	/* Remove any optionsets from the resource. */
385455bf511dSas200622 	for (opt = sa_get_optionset(resource, NULL);
385555bf511dSas200622 	    opt != NULL;
385655bf511dSas200622 	    opt = sa_get_next_optionset(opt))
385755bf511dSas200622 		(void) sa_destroy_optionset(opt);
385855bf511dSas200622 
3859da6c28aaSamw 	/* Remove from the share */
3860da6c28aaSamw 	xmlUnlinkNode((xmlNode *)resource);
3861da6c28aaSamw 	xmlFreeNode((xmlNode *)resource);
3862da6c28aaSamw 
3863da6c28aaSamw 	/* only do SMF action if permanent and not ZFS */
38641f713840SDoug McCallum 	if (transient)
38651f713840SDoug McCallum 		return (ret);
38661f713840SDoug McCallum 
38671f713840SDoug McCallum 	if (!sa_group_is_zfs(group)) {
3868da6c28aaSamw 		sa_handle_impl_t ihandle;
3869da6c28aaSamw 		ihandle = (sa_handle_impl_t)sa_find_group_handle(group);
3870da6c28aaSamw 		if (ihandle != NULL)
3871da6c28aaSamw 			ret = sa_commit_share(ihandle->scfhandle, group, share);
3872da6c28aaSamw 		else
3873da6c28aaSamw 			ret = SA_SYSTEM_ERR;
38741f713840SDoug McCallum 	} else {
38751f713840SDoug McCallum 		ret = sa_zfs_update((sa_share_t)group);
3876da6c28aaSamw 	}
3877743a77edSAlan Wright 
3878da6c28aaSamw 	return (ret);
3879da6c28aaSamw }
3880da6c28aaSamw 
3881da6c28aaSamw /*
3882743a77edSAlan Wright  * proto_rename_resource(handle, group, resource, newname)
3883da6c28aaSamw  *
3884da6c28aaSamw  * Helper function for sa_rename_resource that notifies the protocol
3885da6c28aaSamw  * of a resource name change prior to a config repository update.
3886da6c28aaSamw  */
3887da6c28aaSamw static int
proto_rename_resource(sa_handle_t handle,sa_group_t group,sa_resource_t resource,char * newname)3888da6c28aaSamw proto_rename_resource(sa_handle_t handle, sa_group_t group,
3889da6c28aaSamw     sa_resource_t resource, char *newname)
3890da6c28aaSamw {
3891da6c28aaSamw 	sa_optionset_t optionset;
3892da6c28aaSamw 	int ret = SA_OK;
3893da6c28aaSamw 	int err;
3894da6c28aaSamw 
3895da6c28aaSamw 	for (optionset = sa_get_optionset(group, NULL);
3896da6c28aaSamw 	    optionset != NULL;
3897da6c28aaSamw 	    optionset = sa_get_next_optionset(optionset)) {
3898da6c28aaSamw 		char *type;
3899da6c28aaSamw 		type = sa_get_optionset_attr(optionset, "type");
3900da6c28aaSamw 		if (type != NULL) {
3901da6c28aaSamw 			err = sa_proto_rename_resource(handle, type, resource,
3902da6c28aaSamw 			    newname);
3903da6c28aaSamw 			if (err != SA_OK)
3904da6c28aaSamw 				ret = err;
3905da6c28aaSamw 			sa_free_attr_string(type);
3906da6c28aaSamw 		}
3907da6c28aaSamw 	}
3908da6c28aaSamw 	return (ret);
3909da6c28aaSamw }
3910da6c28aaSamw 
3911da6c28aaSamw /*
3912da6c28aaSamw  * sa_rename_resource(resource, newname)
3913da6c28aaSamw  *
3914da6c28aaSamw  * Rename the resource to the new name, if it is unique.
3915da6c28aaSamw  */
3916da6c28aaSamw 
3917da6c28aaSamw int
sa_rename_resource(sa_resource_t resource,char * newname)3918da6c28aaSamw sa_rename_resource(sa_resource_t resource, char *newname)
3919da6c28aaSamw {
3920da6c28aaSamw 	sa_share_t share;
3921da6c28aaSamw 	sa_group_t group = NULL;
3922da6c28aaSamw 	sa_resource_t target;
3923da6c28aaSamw 	int ret = SA_CONFIG_ERR;
3924da6c28aaSamw 	sa_handle_t handle = NULL;
3925da6c28aaSamw 
3926da6c28aaSamw 	share = sa_get_resource_parent(resource);
3927da6c28aaSamw 	if (share == NULL)
3928da6c28aaSamw 		return (ret);
3929da6c28aaSamw 
3930da6c28aaSamw 	group = sa_get_parent_group(share);
3931da6c28aaSamw 	if (group == NULL)
3932da6c28aaSamw 		return (ret);
3933da6c28aaSamw 
3934da6c28aaSamw 	handle = (sa_handle_impl_t)sa_find_group_handle(group);
3935da6c28aaSamw 	if (handle == NULL)
3936da6c28aaSamw 		return (ret);
3937da6c28aaSamw 
3938da6c28aaSamw 	target = sa_find_resource(handle, newname);
3939da6c28aaSamw 	if (target != NULL) {
3940da6c28aaSamw 		ret = SA_DUPLICATE_NAME;
3941da6c28aaSamw 	} else {
3942da6c28aaSamw 		/*
3943da6c28aaSamw 		 * Everything appears to be valid at this
3944da6c28aaSamw 		 * point. Change the name of the active share and then
3945da6c28aaSamw 		 * update the share in the appropriate repository.
3946da6c28aaSamw 		 */
3947da6c28aaSamw 		ret = proto_rename_resource(handle, group, resource, newname);
3948da6c28aaSamw 		set_node_attr(resource, "name", newname);
39491f713840SDoug McCallum 
39501f713840SDoug McCallum 		if (!sa_is_persistent((sa_group_t)share))
39511f713840SDoug McCallum 			return (ret);
39521f713840SDoug McCallum 
39531f713840SDoug McCallum 		if (!sa_group_is_zfs(group)) {
3954da6c28aaSamw 			sa_handle_impl_t ihandle = (sa_handle_impl_t)handle;
3955da6c28aaSamw 			ret = sa_commit_share(ihandle->scfhandle, group,
3956da6c28aaSamw 			    share);
39571f713840SDoug McCallum 		} else {
39581f713840SDoug McCallum 			ret = sa_zfs_update((sa_share_t)group);
3959da6c28aaSamw 		}
3960da6c28aaSamw 	}
3961da6c28aaSamw 	return (ret);
3962da6c28aaSamw }
3963da6c28aaSamw 
3964da6c28aaSamw /*
3965da6c28aaSamw  * sa_get_resource_attr(resource, tag)
3966da6c28aaSamw  *
3967da6c28aaSamw  * Get the named attribute of the resource. "name" and "id" are
3968da6c28aaSamw  * currently defined.  NULL if tag not defined.
3969da6c28aaSamw  */
3970da6c28aaSamw 
3971da6c28aaSamw char *
sa_get_resource_attr(sa_resource_t resource,char * tag)3972da6c28aaSamw sa_get_resource_attr(sa_resource_t resource, char *tag)
3973da6c28aaSamw {
3974da6c28aaSamw 	return (get_node_attr((void *)resource, tag));
3975da6c28aaSamw }
3976da6c28aaSamw 
3977da6c28aaSamw /*
3978da6c28aaSamw  * sa_set_resource_attr(resource, tag, value)
3979da6c28aaSamw  *
3980da6c28aaSamw  * Get the named attribute of the resource. "name" and "id" are
3981da6c28aaSamw  * currently defined.  NULL if tag not defined. Currently we don't do
3982da6c28aaSamw  * much, but additional checking may be needed in the future.
3983da6c28aaSamw  */
3984da6c28aaSamw 
3985da6c28aaSamw int
sa_set_resource_attr(sa_resource_t resource,char * tag,char * value)3986da6c28aaSamw sa_set_resource_attr(sa_resource_t resource, char *tag, char *value)
3987da6c28aaSamw {
3988da6c28aaSamw 	set_node_attr((void *)resource, tag, value);
3989da6c28aaSamw 	return (SA_OK);
3990da6c28aaSamw }
3991da6c28aaSamw 
3992da6c28aaSamw /*
3993da6c28aaSamw  * sa_get_resource_parent(resource_t)
3994da6c28aaSamw  *
3995da6c28aaSamw  * Returns the share associated with the resource.
3996da6c28aaSamw  */
3997da6c28aaSamw 
3998da6c28aaSamw sa_share_t
sa_get_resource_parent(sa_resource_t resource)3999da6c28aaSamw sa_get_resource_parent(sa_resource_t resource)
4000da6c28aaSamw {
4001da6c28aaSamw 	sa_share_t share = NULL;
4002da6c28aaSamw 
4003da6c28aaSamw 	if (resource != NULL)
4004da6c28aaSamw 		share = (sa_share_t)((xmlNodePtr)resource)->parent;
4005da6c28aaSamw 	return (share);
4006da6c28aaSamw }
4007da6c28aaSamw 
4008da6c28aaSamw /*
4009da6c28aaSamw  * find_resource(group, name)
4010da6c28aaSamw  *
4011da6c28aaSamw  * Find the resource within the group.
4012da6c28aaSamw  */
4013da6c28aaSamw 
4014da6c28aaSamw static sa_resource_t
find_resource(sa_group_t group,char * resname)4015da6c28aaSamw find_resource(sa_group_t group, char *resname)
4016da6c28aaSamw {
4017da6c28aaSamw 	sa_share_t share;
4018da6c28aaSamw 	sa_resource_t resource = NULL;
4019da6c28aaSamw 	char *name;
4020da6c28aaSamw 
4021da6c28aaSamw 	/* Iterate over all the shares and resources in the group. */
4022da6c28aaSamw 	for (share = sa_get_share(group, NULL);
4023da6c28aaSamw 	    share != NULL && resource == NULL;
4024da6c28aaSamw 	    share = sa_get_next_share(share)) {
4025da6c28aaSamw 		for (resource = sa_get_share_resource(share, NULL);
4026da6c28aaSamw 		    resource != NULL;
4027da6c28aaSamw 		    resource = sa_get_next_resource(resource)) {
4028da6c28aaSamw 			name = sa_get_resource_attr(resource, "name");
4029da6c28aaSamw 			if (name != NULL && xmlStrcasecmp((xmlChar*)name,
4030da6c28aaSamw 			    (xmlChar*)resname) == 0) {
4031da6c28aaSamw 				sa_free_attr_string(name);
4032da6c28aaSamw 				break;
4033da6c28aaSamw 			}
4034da6c28aaSamw 			if (name != NULL) {
4035da6c28aaSamw 				sa_free_attr_string(name);
4036da6c28aaSamw 			}
4037da6c28aaSamw 		}
4038da6c28aaSamw 	}
4039da6c28aaSamw 	return (resource);
4040da6c28aaSamw }
4041da6c28aaSamw 
4042da6c28aaSamw /*
4043da6c28aaSamw  * sa_find_resource(name)
4044da6c28aaSamw  *
4045da6c28aaSamw  * Find the named resource in the system.
4046da6c28aaSamw  */
4047da6c28aaSamw 
4048da6c28aaSamw sa_resource_t
sa_find_resource(sa_handle_t handle,char * name)4049da6c28aaSamw sa_find_resource(sa_handle_t handle, char *name)
4050da6c28aaSamw {
4051da6c28aaSamw 	sa_group_t group;
4052da6c28aaSamw 	sa_group_t zgroup;
4053da6c28aaSamw 	sa_resource_t resource = NULL;
4054da6c28aaSamw 
4055da6c28aaSamw 	/*
4056da6c28aaSamw 	 * Iterate over all groups and zfs subgroups and check for
4057da6c28aaSamw 	 * resource name in them.
4058da6c28aaSamw 	 */
4059da6c28aaSamw 	for (group = sa_get_group(handle, NULL); group != NULL;
4060da6c28aaSamw 	    group = sa_get_next_group(group)) {
4061da6c28aaSamw 
4062da6c28aaSamw 		if (is_zfs_group(group)) {
4063da6c28aaSamw 			for (zgroup =
4064da6c28aaSamw 			    (sa_group_t)_sa_get_child_node((xmlNodePtr)group,
4065da6c28aaSamw 			    (xmlChar *)"group");
4066da6c28aaSamw 			    zgroup != NULL && resource == NULL;
4067da6c28aaSamw 			    zgroup = sa_get_next_group(zgroup)) {
4068da6c28aaSamw 				resource = find_resource(zgroup, name);
4069da6c28aaSamw 			}
4070da6c28aaSamw 		} else {
4071da6c28aaSamw 			resource = find_resource(group, name);
4072da6c28aaSamw 		}
4073da6c28aaSamw 		if (resource != NULL)
4074da6c28aaSamw 			break;
4075da6c28aaSamw 	}
4076da6c28aaSamw 	return (resource);
4077da6c28aaSamw }
4078da6c28aaSamw 
4079da6c28aaSamw /*
4080da6c28aaSamw  * sa_get_resource(group, resource)
4081da6c28aaSamw  *
4082da6c28aaSamw  * Search all the shares in the specified group for a share with a
4083da6c28aaSamw  * resource name matching the one specified.
4084da6c28aaSamw  *
4085da6c28aaSamw  * In the future, it may be advantageous to allow group to be NULL and
4086da6c28aaSamw  * search all groups but that isn't needed at present.
4087da6c28aaSamw  */
4088da6c28aaSamw 
4089da6c28aaSamw sa_resource_t
sa_get_resource(sa_group_t group,char * resource)4090da6c28aaSamw sa_get_resource(sa_group_t group, char *resource)
4091da6c28aaSamw {
4092da6c28aaSamw 	sa_share_t share = NULL;
4093da6c28aaSamw 	sa_resource_t res = NULL;
4094da6c28aaSamw 
4095da6c28aaSamw 	if (resource != NULL) {
4096da6c28aaSamw 		for (share = sa_get_share(group, NULL);
4097da6c28aaSamw 		    share != NULL && res == NULL;
4098da6c28aaSamw 		    share = sa_get_next_share(share)) {
4099da6c28aaSamw 			res = sa_get_share_resource(share, resource);
4100da6c28aaSamw 		}
4101da6c28aaSamw 	}
4102da6c28aaSamw 	return (res);
4103da6c28aaSamw }
4104da6c28aaSamw 
4105da6c28aaSamw /*
4106f85463f2Sdougm  * get_protocol_list(optionset, object)
4107f85463f2Sdougm  *
4108f85463f2Sdougm  * Get the protocol optionset list for the object and add them as
4109f85463f2Sdougm  * properties to optionset.
4110f85463f2Sdougm  */
4111f85463f2Sdougm static int
get_protocol_list(sa_optionset_t optionset,void * object)4112f85463f2Sdougm get_protocol_list(sa_optionset_t optionset, void *object)
4113f85463f2Sdougm {
4114f85463f2Sdougm 	sa_property_t prop;
4115f85463f2Sdougm 	sa_optionset_t opts;
4116f85463f2Sdougm 	int ret = SA_OK;
4117f85463f2Sdougm 
4118f85463f2Sdougm 	for (opts = sa_get_optionset(object, NULL);
4119f85463f2Sdougm 	    opts != NULL;
4120f85463f2Sdougm 	    opts = sa_get_next_optionset(opts)) {
4121f85463f2Sdougm 		char *type;
4122f85463f2Sdougm 		type = sa_get_optionset_attr(opts, "type");
4123f85463f2Sdougm 		/*
4124f85463f2Sdougm 		 * It is possible to have a non-protocol optionset. We
4125f85463f2Sdougm 		 * skip any of those found.
4126f85463f2Sdougm 		 */
4127f85463f2Sdougm 		if (type == NULL)
4128f85463f2Sdougm 			continue;
4129f85463f2Sdougm 		prop = sa_create_property(type, "true");
4130f85463f2Sdougm 		sa_free_attr_string(type);
4131f85463f2Sdougm 		if (prop != NULL)
4132f85463f2Sdougm 			prop = (sa_property_t)xmlAddChild((xmlNodePtr)optionset,
4133f85463f2Sdougm 			    (xmlNodePtr)prop);
4134f85463f2Sdougm 		/* If prop is NULL, don't bother continuing */
4135f85463f2Sdougm 		if (prop == NULL) {
4136f85463f2Sdougm 			ret = SA_NO_MEMORY;
4137f85463f2Sdougm 			break;
4138f85463f2Sdougm 		}
4139f85463f2Sdougm 	}
4140f85463f2Sdougm 	return (ret);
4141f85463f2Sdougm }
4142f85463f2Sdougm 
4143f85463f2Sdougm /*
4144f85463f2Sdougm  * sa_free_protoset(optionset)
4145f85463f2Sdougm  *
4146f85463f2Sdougm  * Free the protocol property optionset.
4147f85463f2Sdougm  */
4148f85463f2Sdougm static void
sa_free_protoset(sa_optionset_t optionset)4149f85463f2Sdougm sa_free_protoset(sa_optionset_t optionset)
4150f85463f2Sdougm {
4151f85463f2Sdougm 	if (optionset != NULL) {
4152f85463f2Sdougm 		xmlUnlinkNode((xmlNodePtr) optionset);
4153f85463f2Sdougm 		xmlFreeNode((xmlNodePtr) optionset);
4154f85463f2Sdougm 	}
4155f85463f2Sdougm }
4156f85463f2Sdougm 
4157f85463f2Sdougm /*
4158f85463f2Sdougm  * sa_optionset_t sa_get_active_protocols(object)
4159f85463f2Sdougm  *
4160f85463f2Sdougm  * Return a list of the protocols that are active for the object.
4161f85463f2Sdougm  * This is currently an internal helper function, but could be
4162f85463f2Sdougm  * made visible if there is enough demand for it.
4163f85463f2Sdougm  *
4164f85463f2Sdougm  * The function finds the parent group and extracts the protocol
4165f85463f2Sdougm  * optionsets creating a new optionset with the protocols as properties.
4166f85463f2Sdougm  *
4167f85463f2Sdougm  * The caller must free the returned optionset.
4168f85463f2Sdougm  */
4169f85463f2Sdougm 
4170f85463f2Sdougm static sa_optionset_t
sa_get_active_protocols(void * object)4171f85463f2Sdougm sa_get_active_protocols(void *object)
4172f85463f2Sdougm {
4173f85463f2Sdougm 	sa_optionset_t options;
4174f85463f2Sdougm 	sa_share_t share = NULL;
4175f85463f2Sdougm 	sa_group_t group = NULL;
4176f85463f2Sdougm 	sa_resource_t resource = NULL;
4177f85463f2Sdougm 	int ret = SA_OK;
4178f85463f2Sdougm 
4179f85463f2Sdougm 	if (object == NULL)
4180f85463f2Sdougm 		return (NULL);
4181f85463f2Sdougm 	options = (sa_optionset_t)xmlNewNode(NULL, (xmlChar *)"optionset");
4182f85463f2Sdougm 	if (options == NULL)
4183f85463f2Sdougm 		return (NULL);
4184f85463f2Sdougm 
4185f85463f2Sdougm 	/*
4186f85463f2Sdougm 	 * Find the objects up the tree that might have protocols
4187f85463f2Sdougm 	 * enabled on them.
4188f85463f2Sdougm 	 */
4189f85463f2Sdougm 	if (sa_is_resource(object)) {
4190f85463f2Sdougm 		resource = (sa_resource_t)object;
4191f85463f2Sdougm 		share = sa_get_resource_parent(resource);
4192f85463f2Sdougm 		group = sa_get_parent_group(share);
4193f85463f2Sdougm 	} else if (sa_is_share(object)) {
4194f85463f2Sdougm 		share = (sa_share_t)object;
4195f85463f2Sdougm 		group = sa_get_parent_group(share);
4196f85463f2Sdougm 	} else {
4197f85463f2Sdougm 		group = (sa_group_t)group;
4198f85463f2Sdougm 	}
4199f85463f2Sdougm 	if (resource != NULL)
4200f85463f2Sdougm 		ret = get_protocol_list(options, resource);
4201f85463f2Sdougm 	if (ret == SA_OK && share != NULL)
4202f85463f2Sdougm 		ret = get_protocol_list(options, share);
4203f85463f2Sdougm 	if (ret == SA_OK && group != NULL)
4204f85463f2Sdougm 		ret = get_protocol_list(options, group);
4205f85463f2Sdougm 
4206f85463f2Sdougm 	/*
4207f85463f2Sdougm 	 * If there was an error, we won't have a complete list so
4208f85463f2Sdougm 	 * abandon everything.  The caller will have to deal with the
4209f85463f2Sdougm 	 * issue.
4210f85463f2Sdougm 	 */
4211f85463f2Sdougm 	if (ret != SA_OK) {
4212f85463f2Sdougm 		sa_free_protoset(options);
4213f85463f2Sdougm 		options = NULL;
4214f85463f2Sdougm 	}
4215f85463f2Sdougm 	return (options);
4216f85463f2Sdougm }
4217f85463f2Sdougm 
4218f85463f2Sdougm /*
4219da6c28aaSamw  * sa_enable_resource, protocol)
4220da6c28aaSamw  *	Disable the specified share to the specified protocol.
4221da6c28aaSamw  *	If protocol is NULL, then all protocols.
4222da6c28aaSamw  */
4223da6c28aaSamw int
sa_enable_resource(sa_resource_t resource,char * protocol)4224da6c28aaSamw sa_enable_resource(sa_resource_t resource, char *protocol)
4225da6c28aaSamw {
4226da6c28aaSamw 	int ret = SA_OK;
4227da6c28aaSamw 
4228da6c28aaSamw 	if (protocol != NULL) {
4229da6c28aaSamw 		ret = sa_proto_share_resource(protocol, resource);
4230da6c28aaSamw 	} else {
4231f85463f2Sdougm 		sa_optionset_t protoset;
4232f85463f2Sdougm 		sa_property_t prop;
4233f85463f2Sdougm 		char *proto;
4234f85463f2Sdougm 		int err;
4235f85463f2Sdougm 
4236da6c28aaSamw 		/* need to do all protocols */
4237f85463f2Sdougm 		protoset = sa_get_active_protocols(resource);
4238f85463f2Sdougm 		if (protoset == NULL)
4239f85463f2Sdougm 			return (SA_NO_MEMORY);
4240f85463f2Sdougm 		for (prop = sa_get_property(protoset, NULL);
4241f85463f2Sdougm 		    prop != NULL;
4242f85463f2Sdougm 		    prop = sa_get_next_property(prop)) {
4243f85463f2Sdougm 			proto = sa_get_property_attr(prop, "type");
4244f85463f2Sdougm 			if (proto == NULL) {
4245f85463f2Sdougm 				ret = SA_NO_MEMORY;
4246f85463f2Sdougm 				continue;
4247f85463f2Sdougm 			}
4248f85463f2Sdougm 			err = sa_proto_share_resource(proto, resource);
4249da6c28aaSamw 			if (err != SA_OK)
4250da6c28aaSamw 				ret = err;
4251f85463f2Sdougm 			sa_free_attr_string(proto);
4252da6c28aaSamw 		}
4253f85463f2Sdougm 		sa_free_protoset(protoset);
4254da6c28aaSamw 	}
4255da6c28aaSamw 	if (ret == SA_OK)
4256da6c28aaSamw 		(void) sa_set_resource_attr(resource, "shared", NULL);
4257da6c28aaSamw 
4258da6c28aaSamw 	return (ret);
4259da6c28aaSamw }
4260da6c28aaSamw 
4261da6c28aaSamw /*
4262da6c28aaSamw  * sa_disable_resource(resource, protocol)
4263da6c28aaSamw  *
4264da6c28aaSamw  *	Disable the specified share for the specified protocol.  If
4265da6c28aaSamw  *	protocol is NULL, then all protocols.  If the underlying
4266da6c28aaSamw  *	protocol doesn't implement disable at the resource level, we
4267da6c28aaSamw  *	disable at the share level.
4268da6c28aaSamw  */
4269da6c28aaSamw int
sa_disable_resource(sa_resource_t resource,char * protocol)4270da6c28aaSamw sa_disable_resource(sa_resource_t resource, char *protocol)
4271da6c28aaSamw {
4272da6c28aaSamw 	int ret = SA_OK;
4273da6c28aaSamw 
4274da6c28aaSamw 	if (protocol != NULL) {
4275da6c28aaSamw 		ret = sa_proto_unshare_resource(protocol, resource);
4276da6c28aaSamw 		if (ret == SA_NOT_IMPLEMENTED) {
4277da6c28aaSamw 			sa_share_t parent;
4278da6c28aaSamw 			/*
4279da6c28aaSamw 			 * The protocol doesn't implement unshare
4280da6c28aaSamw 			 * resource. That implies that resource names are
4281da6c28aaSamw 			 * simple aliases for this protocol so we need to
4282da6c28aaSamw 			 * unshare the share.
4283da6c28aaSamw 			 */
4284da6c28aaSamw 			parent = sa_get_resource_parent(resource);
4285da6c28aaSamw 			if (parent != NULL)
4286da6c28aaSamw 				ret = sa_disable_share(parent, protocol);
4287da6c28aaSamw 			else
4288da6c28aaSamw 				ret = SA_CONFIG_ERR;
4289da6c28aaSamw 		}
4290da6c28aaSamw 	} else {
4291f85463f2Sdougm 		sa_optionset_t protoset;
4292f85463f2Sdougm 		sa_property_t prop;
4293f85463f2Sdougm 		char *proto;
4294f85463f2Sdougm 		int err;
4295f85463f2Sdougm 
4296da6c28aaSamw 		/* need to do all protocols */
4297f85463f2Sdougm 		protoset = sa_get_active_protocols(resource);
4298f85463f2Sdougm 		if (protoset == NULL)
4299f85463f2Sdougm 			return (SA_NO_MEMORY);
4300f85463f2Sdougm 		for (prop = sa_get_property(protoset, NULL);
4301f85463f2Sdougm 		    prop != NULL;
4302f85463f2Sdougm 		    prop = sa_get_next_property(prop)) {
4303f85463f2Sdougm 			proto = sa_get_property_attr(prop, "type");
4304f85463f2Sdougm 			if (proto == NULL) {
4305f85463f2Sdougm 				ret = SA_NO_MEMORY;
4306f85463f2Sdougm 				continue;
4307f85463f2Sdougm 			}
4308f85463f2Sdougm 			err = sa_proto_unshare_resource(proto, resource);
4309da6c28aaSamw 			if (err == SA_NOT_SUPPORTED) {
4310da6c28aaSamw 				sa_share_t parent;
4311f85463f2Sdougm 				parent = sa_get_resource_parent(resource);
4312da6c28aaSamw 				if (parent != NULL)
4313f85463f2Sdougm 					err = sa_disable_share(parent, proto);
4314da6c28aaSamw 				else
4315da6c28aaSamw 					err = SA_CONFIG_ERR;
4316da6c28aaSamw 			}
4317da6c28aaSamw 			if (err != SA_OK)
4318da6c28aaSamw 				ret = err;
4319f85463f2Sdougm 			sa_free_attr_string(proto);
4320da6c28aaSamw 		}
4321f85463f2Sdougm 		sa_free_protoset(protoset);
4322da6c28aaSamw 	}
4323da6c28aaSamw 	if (ret == SA_OK)
4324da6c28aaSamw 		(void) sa_set_resource_attr(resource, "shared", NULL);
4325da6c28aaSamw 
4326da6c28aaSamw 	return (ret);
4327da6c28aaSamw }
4328da6c28aaSamw 
4329da6c28aaSamw /*
4330da6c28aaSamw  * sa_set_resource_description(resource, content)
4331da6c28aaSamw  *
4332da6c28aaSamw  * Set the description of share to content.
4333da6c28aaSamw  */
4334da6c28aaSamw 
4335da6c28aaSamw int
sa_set_resource_description(sa_resource_t resource,char * content)4336da6c28aaSamw sa_set_resource_description(sa_resource_t resource, char *content)
4337da6c28aaSamw {
4338da6c28aaSamw 	xmlNodePtr node;
4339da6c28aaSamw 	sa_group_t group;
4340da6c28aaSamw 	sa_share_t share;
4341da6c28aaSamw 	int ret = SA_OK;
4342da6c28aaSamw 
4343da6c28aaSamw 	for (node = ((xmlNodePtr)resource)->children;
4344da6c28aaSamw 	    node != NULL;
4345da6c28aaSamw 	    node = node->next) {
4346da6c28aaSamw 		if (xmlStrcmp(node->name, (xmlChar *)"description") == 0) {
4347da6c28aaSamw 			break;
4348da6c28aaSamw 		}
4349da6c28aaSamw 	}
4350da6c28aaSamw 
4351da6c28aaSamw 	/* no existing description but want to add */
4352da6c28aaSamw 	if (node == NULL && content != NULL) {
4353da6c28aaSamw 		/* add a description */
4354da6c28aaSamw 		node = _sa_set_share_description(resource, content);
4355da6c28aaSamw 	} else if (node != NULL && content != NULL) {
4356da6c28aaSamw 		/* update a description */
4357da6c28aaSamw 		xmlNodeSetContent(node, (xmlChar *)content);
4358da6c28aaSamw 	} else if (node != NULL && content == NULL) {
4359da6c28aaSamw 		/* remove an existing description */
4360da6c28aaSamw 		xmlUnlinkNode(node);
4361da6c28aaSamw 		xmlFreeNode(node);
4362da6c28aaSamw 	}
4363148c5f43SAlan Wright 
4364da6c28aaSamw 	share = sa_get_resource_parent(resource);
4365da6c28aaSamw 	group = sa_get_parent_group(share);
4366148c5f43SAlan Wright 	if (group != NULL &&
4367148c5f43SAlan Wright 	    sa_is_persistent(share) && (!sa_group_is_zfs(group))) {
4368da6c28aaSamw 		sa_handle_impl_t impl_handle;
4369da6c28aaSamw 		impl_handle = (sa_handle_impl_t)sa_find_group_handle(group);
4370da6c28aaSamw 		if (impl_handle != NULL)
4371da6c28aaSamw 			ret = sa_commit_share(impl_handle->scfhandle,
4372da6c28aaSamw 			    group, share);
4373da6c28aaSamw 		else
4374da6c28aaSamw 			ret = SA_SYSTEM_ERR;
4375da6c28aaSamw 	}
4376da6c28aaSamw 	return (ret);
4377da6c28aaSamw }
4378da6c28aaSamw 
4379da6c28aaSamw /*
4380da6c28aaSamw  * sa_get_resource_description(share)
4381da6c28aaSamw  *
4382da6c28aaSamw  * Return the description text for the specified share if it
4383da6c28aaSamw  * exists. NULL if no description exists.
4384da6c28aaSamw  */
4385da6c28aaSamw 
4386da6c28aaSamw char *
sa_get_resource_description(sa_resource_t resource)4387da6c28aaSamw sa_get_resource_description(sa_resource_t resource)
4388da6c28aaSamw {
4389da6c28aaSamw 	xmlChar *description = NULL;
4390da6c28aaSamw 	xmlNodePtr node;
4391da6c28aaSamw 
4392da6c28aaSamw 	for (node = ((xmlNodePtr)resource)->children; node != NULL;
4393da6c28aaSamw 	    node = node->next) {
4394da6c28aaSamw 		if (xmlStrcmp(node->name, (xmlChar *)"description") == 0)
4395da6c28aaSamw 			break;
4396da6c28aaSamw 	}
4397da6c28aaSamw 	if (node != NULL) {
4398da6c28aaSamw 		description = xmlNodeGetContent(node);
4399da6c28aaSamw 		fixproblemchars((char *)description);
4400da6c28aaSamw 	}
4401da6c28aaSamw 	return ((char *)description);
4402da6c28aaSamw }
4403