xref: /titanic_41/usr/src/lib/libshare/common/libshare.c (revision 549ec3fff108310966327d1dc9004551b63210b7)
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 /*
23f345c0beSdougm  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
246185db85Sdougm  * Use is subject to license terms.
256185db85Sdougm  */
266185db85Sdougm 
276185db85Sdougm #pragma ident	"%Z%%M%	%I%	%E% SMI"
286185db85Sdougm 
296185db85Sdougm /*
306185db85Sdougm  * Share control API
316185db85Sdougm  */
326185db85Sdougm #include <stdio.h>
336185db85Sdougm #include <string.h>
346185db85Sdougm #include <ctype.h>
356185db85Sdougm #include <sys/types.h>
366185db85Sdougm #include <sys/stat.h>
37a99982a7Sdougm #include <fcntl.h>
386185db85Sdougm #include <unistd.h>
396185db85Sdougm #include <libxml/parser.h>
406185db85Sdougm #include <libxml/tree.h>
416185db85Sdougm #include "libshare.h"
426185db85Sdougm #include "libshare_impl.h"
436185db85Sdougm #include <libscf.h>
446185db85Sdougm #include "scfutil.h"
456185db85Sdougm #include <ctype.h>
466185db85Sdougm #include <libintl.h>
47*549ec3ffSdougm #include <thread.h>
48*549ec3ffSdougm #include <synch.h>
496185db85Sdougm 
506185db85Sdougm #if _NOT_SMF
516185db85Sdougm #define	CONFIG_FILE	"/var/tmp/share.cfg"
526185db85Sdougm #define	CONFIG_FILE_TMP	"/var/tmp/share.cfg.tmp"
536185db85Sdougm #endif
546185db85Sdougm #define	TSTAMP(tm)	(uint64_t)(((uint64_t)tm.tv_sec << 32) | \
556185db85Sdougm 					(tm.tv_nsec & 0xffffffff))
566185db85Sdougm 
57a99982a7Sdougm #define	DFS_LOCK_FILE	"/etc/dfs/fstypes"
58a99982a7Sdougm 
596185db85Sdougm /*
606185db85Sdougm  * internal data structures
616185db85Sdougm  */
626185db85Sdougm 
636185db85Sdougm extern struct sa_proto_plugin *sap_proto_list;
646185db85Sdougm 
656185db85Sdougm /* current SMF/SVC repository handle */
66*549ec3ffSdougm extern void getlegacyconfig(sa_handle_t, char *, xmlNodePtr *);
67*549ec3ffSdougm extern int gettransients(sa_handle_impl_t, xmlNodePtr *);
686185db85Sdougm extern int sa_valid_property(void *, char *, sa_property_t);
696185db85Sdougm extern char *sa_fstype(char *);
706185db85Sdougm extern int sa_is_share(void *);
716185db85Sdougm extern ssize_t scf_max_name_len; /* defined in scfutil during initialization */
726185db85Sdougm extern int sa_group_is_zfs(sa_group_t);
736185db85Sdougm extern int sa_path_is_zfs(char *);
746185db85Sdougm extern int sa_zfs_set_sharenfs(sa_group_t, char *, int);
75*549ec3ffSdougm extern void update_legacy_config(sa_handle_t);
766185db85Sdougm extern int issubdir(char *, char *);
77*549ec3ffSdougm extern void sa_zfs_init(sa_handle_impl_t);
78*549ec3ffSdougm extern void sa_zfs_fini(sa_handle_impl_t);
79a99982a7Sdougm extern void sablocksigs(sigset_t *);
80a99982a7Sdougm extern void saunblocksigs(sigset_t *);
816185db85Sdougm 
82*549ec3ffSdougm /*
83*549ec3ffSdougm  * Data structures for finding/managing the document root to access
84*549ec3ffSdougm  * handle mapping. The list isn't expected to grow very large so a
85*549ec3ffSdougm  * simple list is acceptable. The purpose is to provide a way to start
86*549ec3ffSdougm  * with a group or share and find the library handle needed for
87*549ec3ffSdougm  * various operations.
88*549ec3ffSdougm  */
89*549ec3ffSdougm mutex_t sa_global_lock;
90*549ec3ffSdougm struct doc2handle {
91*549ec3ffSdougm 	struct doc2handle	*next;
92*549ec3ffSdougm 	xmlNodePtr		root;
93*549ec3ffSdougm 	sa_handle_impl_t	handle;
94*549ec3ffSdougm };
95*549ec3ffSdougm 
96*549ec3ffSdougm static struct doc2handle *sa_global_handles = NULL;
976185db85Sdougm 
986185db85Sdougm /* helper functions */
996185db85Sdougm 
100*549ec3ffSdougm /*
101*549ec3ffSdougm  * sa_errorstr(err)
102*549ec3ffSdougm  *
103*549ec3ffSdougm  * convert an error value to an error string
104*549ec3ffSdougm  */
105*549ec3ffSdougm 
1066185db85Sdougm char *
1076185db85Sdougm sa_errorstr(int err)
1086185db85Sdougm {
1096185db85Sdougm 	static char errstr[32];
1106185db85Sdougm 	char *ret = NULL;
1116185db85Sdougm 
1126185db85Sdougm 	switch (err) {
1136185db85Sdougm 	case SA_OK:
11424424a35Sdougm 	    ret = dgettext(TEXT_DOMAIN, "ok");
1156185db85Sdougm 	    break;
1166185db85Sdougm 	case SA_NO_SUCH_PATH:
11724424a35Sdougm 	    ret = dgettext(TEXT_DOMAIN, "path doesn't exist");
1186185db85Sdougm 	    break;
1196185db85Sdougm 	case SA_NO_MEMORY:
12024424a35Sdougm 	    ret = dgettext(TEXT_DOMAIN, "no memory");
1216185db85Sdougm 	    break;
1226185db85Sdougm 	case SA_DUPLICATE_NAME:
12324424a35Sdougm 	    ret = dgettext(TEXT_DOMAIN, "name in use");
1246185db85Sdougm 	    break;
1256185db85Sdougm 	case SA_BAD_PATH:
12624424a35Sdougm 	    ret = dgettext(TEXT_DOMAIN, "bad path");
1276185db85Sdougm 	    break;
1286185db85Sdougm 	case SA_NO_SUCH_GROUP:
12924424a35Sdougm 	    ret = dgettext(TEXT_DOMAIN, "no such group");
1306185db85Sdougm 	    break;
1316185db85Sdougm 	case SA_CONFIG_ERR:
13224424a35Sdougm 	    ret = dgettext(TEXT_DOMAIN, "configuration error");
1336185db85Sdougm 	    break;
1346185db85Sdougm 	case SA_SYSTEM_ERR:
13524424a35Sdougm 	    ret = dgettext(TEXT_DOMAIN, "system error");
1366185db85Sdougm 	    break;
1376185db85Sdougm 	case SA_SYNTAX_ERR:
13824424a35Sdougm 	    ret = dgettext(TEXT_DOMAIN, "syntax error");
1396185db85Sdougm 	    break;
1406185db85Sdougm 	case SA_NO_PERMISSION:
14124424a35Sdougm 	    ret = dgettext(TEXT_DOMAIN, "no permission");
1426185db85Sdougm 	    break;
1436185db85Sdougm 	case SA_BUSY:
14424424a35Sdougm 	    ret = dgettext(TEXT_DOMAIN, "busy");
1456185db85Sdougm 	    break;
1466185db85Sdougm 	case SA_NO_SUCH_PROP:
14724424a35Sdougm 	    ret = dgettext(TEXT_DOMAIN, "no such property");
1486185db85Sdougm 	    break;
1496185db85Sdougm 	case SA_INVALID_NAME:
15024424a35Sdougm 	    ret = dgettext(TEXT_DOMAIN, "invalid name");
1516185db85Sdougm 	    break;
1526185db85Sdougm 	case SA_INVALID_PROTOCOL:
15324424a35Sdougm 	    ret = dgettext(TEXT_DOMAIN, "invalid protocol");
1546185db85Sdougm 	    break;
1556185db85Sdougm 	case SA_NOT_ALLOWED:
15624424a35Sdougm 	    ret = dgettext(TEXT_DOMAIN, "operation not allowed");
1576185db85Sdougm 	    break;
1586185db85Sdougm 	case SA_BAD_VALUE:
15924424a35Sdougm 	    ret = dgettext(TEXT_DOMAIN, "bad property value");
1606185db85Sdougm 	    break;
1616185db85Sdougm 	case SA_INVALID_SECURITY:
16224424a35Sdougm 	    ret = dgettext(TEXT_DOMAIN, "invalid security type");
1636185db85Sdougm 	    break;
1646185db85Sdougm 	case SA_NO_SUCH_SECURITY:
16524424a35Sdougm 	    ret = dgettext(TEXT_DOMAIN, "security type not found");
1666185db85Sdougm 	    break;
1676185db85Sdougm 	case SA_VALUE_CONFLICT:
16824424a35Sdougm 	    ret = dgettext(TEXT_DOMAIN, "property value conflict");
1696185db85Sdougm 	    break;
1706185db85Sdougm 	case SA_NOT_IMPLEMENTED:
17124424a35Sdougm 	    ret = dgettext(TEXT_DOMAIN, "not implemented");
1726185db85Sdougm 	    break;
1736185db85Sdougm 	case SA_INVALID_PATH:
17424424a35Sdougm 	    ret = dgettext(TEXT_DOMAIN, "invalid path");
1756185db85Sdougm 	    break;
1766185db85Sdougm 	case SA_NOT_SUPPORTED:
17724424a35Sdougm 	    ret = dgettext(TEXT_DOMAIN, "operation not supported");
1786185db85Sdougm 	    break;
1796185db85Sdougm 	case SA_PROP_SHARE_ONLY:
18024424a35Sdougm 	    ret = dgettext(TEXT_DOMAIN, "property not valid for group");
1816185db85Sdougm 	    break;
1826185db85Sdougm 	case SA_NOT_SHARED:
18324424a35Sdougm 	    ret = dgettext(TEXT_DOMAIN, "not shared");
1846185db85Sdougm 	    break;
1856185db85Sdougm 	default:
1866185db85Sdougm 	    (void) snprintf(errstr, sizeof (errstr),
18724424a35Sdougm 				dgettext(TEXT_DOMAIN, "unknown %d"), err);
1886185db85Sdougm 	    ret = errstr;
1896185db85Sdougm 	}
1906185db85Sdougm 	return (ret);
1916185db85Sdougm }
1926185db85Sdougm 
1936185db85Sdougm /*
194*549ec3ffSdougm  * Document root to active handle mapping functions.  These are only
195*549ec3ffSdougm  * used internally. A mutex is used to prevent access while the list
196*549ec3ffSdougm  * is changing. In general, the list will be relatively short - one
197*549ec3ffSdougm  * item per thread that has called sa_init().
198*549ec3ffSdougm  */
199*549ec3ffSdougm 
200*549ec3ffSdougm sa_handle_impl_t
201*549ec3ffSdougm get_handle_for_root(xmlNodePtr root)
202*549ec3ffSdougm {
203*549ec3ffSdougm 	struct doc2handle *item;
204*549ec3ffSdougm 
205*549ec3ffSdougm 	(void) mutex_lock(&sa_global_lock);
206*549ec3ffSdougm 	for (item = sa_global_handles; item != NULL; item = item->next) {
207*549ec3ffSdougm 	    if (item->root == root)
208*549ec3ffSdougm 		break;
209*549ec3ffSdougm 	}
210*549ec3ffSdougm 	(void) mutex_unlock(&sa_global_lock);
211*549ec3ffSdougm 	if (item != NULL)
212*549ec3ffSdougm 	    return (item->handle);
213*549ec3ffSdougm 	return (NULL);
214*549ec3ffSdougm }
215*549ec3ffSdougm 
216*549ec3ffSdougm static int
217*549ec3ffSdougm add_handle_for_root(xmlNodePtr root, sa_handle_impl_t handle)
218*549ec3ffSdougm {
219*549ec3ffSdougm 	struct doc2handle *item;
220*549ec3ffSdougm 	int ret = SA_NO_MEMORY;
221*549ec3ffSdougm 
222*549ec3ffSdougm 	item = (struct doc2handle *)calloc(sizeof (struct doc2handle), 1);
223*549ec3ffSdougm 	if (item != NULL) {
224*549ec3ffSdougm 	    item->root = root;
225*549ec3ffSdougm 	    item->handle = handle;
226*549ec3ffSdougm 	    (void) mutex_lock(&sa_global_lock);
227*549ec3ffSdougm 	    item->next = sa_global_handles;
228*549ec3ffSdougm 	    sa_global_handles = item;
229*549ec3ffSdougm 	    (void) mutex_unlock(&sa_global_lock);
230*549ec3ffSdougm 	    ret = SA_OK;
231*549ec3ffSdougm 	}
232*549ec3ffSdougm 	return (ret);
233*549ec3ffSdougm }
234*549ec3ffSdougm 
235*549ec3ffSdougm /*
236*549ec3ffSdougm  * remove_handle_for_root(root)
237*549ec3ffSdougm  *
238*549ec3ffSdougm  * Walks the list of handles and removes the one for this "root" from
239*549ec3ffSdougm  * the list. It is up to the caller to free the data.
240*549ec3ffSdougm  */
241*549ec3ffSdougm 
242*549ec3ffSdougm static void
243*549ec3ffSdougm remove_handle_for_root(xmlNodePtr root)
244*549ec3ffSdougm {
245*549ec3ffSdougm 	struct doc2handle *item, *prev;
246*549ec3ffSdougm 
247*549ec3ffSdougm 	(void) mutex_lock(&sa_global_lock);
248*549ec3ffSdougm 	for (prev = NULL, item = sa_global_handles; item != NULL;
249*549ec3ffSdougm 		item = item->next) {
250*549ec3ffSdougm 	    if (item->root == root) {
251*549ec3ffSdougm 		if (prev == NULL) {
252*549ec3ffSdougm 		    /* first in the list */
253*549ec3ffSdougm 		    sa_global_handles = sa_global_handles->next;
254*549ec3ffSdougm 		} else {
255*549ec3ffSdougm 		    prev->next = item->next;
256*549ec3ffSdougm 		}
257*549ec3ffSdougm 		/* Item is out of the list so free the list structure */
258*549ec3ffSdougm 		free(item);
259*549ec3ffSdougm 		break;
260*549ec3ffSdougm 	    }
261*549ec3ffSdougm 	    prev = item;
262*549ec3ffSdougm 	}
263*549ec3ffSdougm 	(void) mutex_unlock(&sa_global_lock);
264*549ec3ffSdougm }
265*549ec3ffSdougm 
266*549ec3ffSdougm /*
267*549ec3ffSdougm  * sa_find_group_handle(sa_group_t group)
268*549ec3ffSdougm  *
269*549ec3ffSdougm  * Find the sa_handle_t for the configuration associated with this
270*549ec3ffSdougm  * group.
271*549ec3ffSdougm  */
272*549ec3ffSdougm sa_handle_t
273*549ec3ffSdougm sa_find_group_handle(sa_group_t group)
274*549ec3ffSdougm {
275*549ec3ffSdougm 	xmlNodePtr node = (xmlNodePtr)group;
276*549ec3ffSdougm 	sa_handle_t handle;
277*549ec3ffSdougm 
278*549ec3ffSdougm 	while (node != NULL) {
279*549ec3ffSdougm 	    if (strcmp((char *)(node->name), "sharecfg") == 0) {
280*549ec3ffSdougm 		/* have the root so get the handle */
281*549ec3ffSdougm 		handle = (sa_handle_t)get_handle_for_root(node);
282*549ec3ffSdougm 		return (handle);
283*549ec3ffSdougm 	    }
284*549ec3ffSdougm 	    node = node->parent;
285*549ec3ffSdougm 	}
286*549ec3ffSdougm 	return (NULL);
287*549ec3ffSdougm }
288*549ec3ffSdougm 
289*549ec3ffSdougm /*
2906185db85Sdougm  * set_legacy_timestamp(root, path, timevalue)
2916185db85Sdougm  *
2926185db85Sdougm  * add the current timestamp value to the configuration for use in
2936185db85Sdougm  * determining when to update the legacy files.  For SMF, this
2946185db85Sdougm  * property is kept in default/operation/legacy_timestamp
2956185db85Sdougm  */
2966185db85Sdougm 
2976185db85Sdougm static void
2986185db85Sdougm set_legacy_timestamp(xmlNodePtr root, char *path, uint64_t tval)
2996185db85Sdougm {
3006185db85Sdougm 	xmlNodePtr node;
3016185db85Sdougm 	xmlChar *lpath = NULL;
302*549ec3ffSdougm 	sa_handle_impl_t handle;
303*549ec3ffSdougm 
304*549ec3ffSdougm 	/* Have to have a handle or else we weren't initialized. */
305*549ec3ffSdougm 	handle = get_handle_for_root(root);
306*549ec3ffSdougm 	if (handle == NULL)
307*549ec3ffSdougm 	    return;
3086185db85Sdougm 
3096185db85Sdougm 	for (node = root->xmlChildrenNode; node != NULL;
3106185db85Sdougm 		node = node->next) {
3116185db85Sdougm 	    if (xmlStrcmp(node->name, (xmlChar *)"legacy") == 0) {
3126185db85Sdougm 		/* a possible legacy node for this path */
3136185db85Sdougm 		lpath = xmlGetProp(node, (xmlChar *)"path");
3146185db85Sdougm 		if (lpath != NULL && xmlStrcmp(lpath, (xmlChar *)path) == 0) {
3156185db85Sdougm 		    xmlFree(lpath);
3166185db85Sdougm 		    break;
3176185db85Sdougm 		}
3186185db85Sdougm 		if (lpath != NULL)
3196185db85Sdougm 		    xmlFree(lpath);
3206185db85Sdougm 	    }
3216185db85Sdougm 	}
3226185db85Sdougm 	if (node == NULL) {
3236185db85Sdougm 	    /* need to create the first legacy timestamp node */
3246185db85Sdougm 	    node = xmlNewChild(root, NULL, (xmlChar *)"legacy", NULL);
3256185db85Sdougm 	}
3266185db85Sdougm 	if (node != NULL) {
3276185db85Sdougm 	    char tstring[32];
3286185db85Sdougm 	    int ret;
3296185db85Sdougm 
3306185db85Sdougm 	    (void) snprintf(tstring, sizeof (tstring), "%lld", tval);
3316185db85Sdougm 	    xmlSetProp(node, (xmlChar *)"timestamp", (xmlChar *)tstring);
3326185db85Sdougm 	    xmlSetProp(node, (xmlChar *)"path", (xmlChar *)path);
3336185db85Sdougm 	    /* now commit to SMF */
334*549ec3ffSdougm 	    ret = sa_get_instance(handle->scfhandle, "default");
3356185db85Sdougm 	    if (ret == SA_OK) {
336*549ec3ffSdougm 		ret = sa_start_transaction(handle->scfhandle, "operation");
3376185db85Sdougm 		if (ret == SA_OK) {
338*549ec3ffSdougm 		    ret = sa_set_property(handle->scfhandle, "legacy-timestamp",
3396185db85Sdougm 					    tstring);
3406185db85Sdougm 		    if (ret == SA_OK) {
341*549ec3ffSdougm 			(void) sa_end_transaction(handle->scfhandle);
3426185db85Sdougm 		    } else {
343*549ec3ffSdougm 			sa_abort_transaction(handle->scfhandle);
3446185db85Sdougm 		    }
3456185db85Sdougm 		}
3466185db85Sdougm 	    }
3476185db85Sdougm 	}
3486185db85Sdougm }
3496185db85Sdougm 
3506185db85Sdougm /*
3516185db85Sdougm  * is_shared(share)
3526185db85Sdougm  *
3536185db85Sdougm  * determine if the specified share is currently shared or not.
3546185db85Sdougm  */
3556185db85Sdougm static int
3566185db85Sdougm is_shared(sa_share_t share)
3576185db85Sdougm {
3586185db85Sdougm 	char *shared;
3596185db85Sdougm 	int result = 0; /* assume not */
3606185db85Sdougm 
3616185db85Sdougm 	shared = sa_get_share_attr(share, "shared");
3626185db85Sdougm 	if (shared != NULL) {
3636185db85Sdougm 	    if (strcmp(shared, "true") == 0)
3646185db85Sdougm 		result = 1;
3656185db85Sdougm 	    sa_free_attr_string(shared);
3666185db85Sdougm 	}
3676185db85Sdougm 	return (result);
3686185db85Sdougm }
3696185db85Sdougm 
3706185db85Sdougm /*
371a99982a7Sdougm  * checksubdirgroup(group, newpath, strictness)
372f345c0beSdougm  *
373a99982a7Sdougm  * check all the specified newpath against all the paths in the
374a99982a7Sdougm  * group. This is a helper function for checksubdir to make it easier
375a99982a7Sdougm  * to also check ZFS subgroups.
376a99982a7Sdougm  * The strictness values mean:
377f345c0beSdougm  * SA_CHECK_NORMAL == only check newpath against shares that are active
378f345c0beSdougm  * SA_CHECK_STRICT == check newpath against both active shares and those
379f345c0beSdougm  *		      stored in the repository
3806185db85Sdougm  */
3816185db85Sdougm static int
382a99982a7Sdougm checksubdirgroup(sa_group_t group, char *newpath, int strictness)
3836185db85Sdougm {
3846185db85Sdougm 	sa_share_t share;
385a99982a7Sdougm 	char *path;
386a99982a7Sdougm 	int issub = SA_OK;
3876185db85Sdougm 
3886185db85Sdougm 	for (share = sa_get_share(group, NULL); share != NULL;
3896185db85Sdougm 	    share = sa_get_next_share(share)) {
3906185db85Sdougm 		/*
3916185db85Sdougm 		 * The original behavior of share never checked
3926185db85Sdougm 		 * against the permanent configuration
3936185db85Sdougm 		 * (/etc/dfs/dfstab).  PIT has a number of cases where
3946185db85Sdougm 		 * it depends on this older behavior even though it
3956185db85Sdougm 		 * could be considered incorrect.  We may tighten this
3966185db85Sdougm 		 * up in the future.
3976185db85Sdougm 		 */
398f345c0beSdougm 	    if (strictness == SA_CHECK_NORMAL && !is_shared(share))
3996185db85Sdougm 		continue;
4006185db85Sdougm 
4016185db85Sdougm 	    path = sa_get_share_attr(share, "path");
402f345c0beSdougm 		/*
403f345c0beSdougm 		 * If path is NULL, then a share is in the process of
404f345c0beSdougm 		 * construction or someone has modified the property
405a99982a7Sdougm 		 * group inappropriately. It should be
406a99982a7Sdougm 		 * ignored. issubdir() comes from the original share
407a99982a7Sdougm 		 * implementation and does the difficult part of
408a99982a7Sdougm 		 * checking subdirectories.
409f345c0beSdougm 		 */
410f345c0beSdougm 	    if (path == NULL)
411f345c0beSdougm 		continue;
4126185db85Sdougm 	    if (newpath != NULL &&
4136185db85Sdougm 		(strcmp(path, newpath) == 0 || issubdir(newpath, path) ||
4146185db85Sdougm 		issubdir(path, newpath))) {
4156185db85Sdougm 		sa_free_attr_string(path);
4166185db85Sdougm 		path = NULL;
4176185db85Sdougm 		issub = SA_INVALID_PATH;
4186185db85Sdougm 		break;
4196185db85Sdougm 	    }
4206185db85Sdougm 	    sa_free_attr_string(path);
4216185db85Sdougm 	    path = NULL;
4226185db85Sdougm 	}
423a99982a7Sdougm 	return (issub);
424a99982a7Sdougm }
425a99982a7Sdougm 
426a99982a7Sdougm /*
427a99982a7Sdougm  * checksubdir(newpath, strictness)
428a99982a7Sdougm  *
429a99982a7Sdougm  * checksubdir determines if the specified path (newpath) is a
430a99982a7Sdougm  * subdirectory of another share. It calls checksubdirgroup() to do
431a99982a7Sdougm  * the complicated work. The strictness parameter determines how
432a99982a7Sdougm  * strict a check to make against the path. The strictness values
433a99982a7Sdougm  * mean: SA_CHECK_NORMAL == only check newpath against shares that are
434a99982a7Sdougm  * active SA_CHECK_STRICT == check newpath against both active shares
435a99982a7Sdougm  * and those * stored in the repository
436a99982a7Sdougm  */
437a99982a7Sdougm static int
438*549ec3ffSdougm checksubdir(sa_handle_t handle, char *newpath, int strictness)
439a99982a7Sdougm {
440a99982a7Sdougm 	sa_group_t group;
441a99982a7Sdougm 	int issub;
442a99982a7Sdougm 	char *path = NULL;
443a99982a7Sdougm 
444*549ec3ffSdougm 	for (issub = 0, group = sa_get_group(handle, NULL);
445a99982a7Sdougm 		group != NULL && !issub;
446a99982a7Sdougm 		group = sa_get_next_group(group)) {
447a99982a7Sdougm 	    if (sa_group_is_zfs(group)) {
448a99982a7Sdougm 		sa_group_t subgroup;
449a99982a7Sdougm 		for (subgroup = sa_get_sub_group(group);
450a99982a7Sdougm 		    subgroup != NULL && !issub;
451a99982a7Sdougm 		    subgroup = sa_get_next_group(subgroup))
452a99982a7Sdougm 		    issub = checksubdirgroup(subgroup, newpath, strictness);
453a99982a7Sdougm 	    } else {
454a99982a7Sdougm 		issub = checksubdirgroup(group, newpath, strictness);
455a99982a7Sdougm 	    }
4566185db85Sdougm 	}
4576185db85Sdougm 	if (path != NULL)
4586185db85Sdougm 	    sa_free_attr_string(path);
4596185db85Sdougm 	return (issub);
4606185db85Sdougm }
4616185db85Sdougm 
4626185db85Sdougm /*
463f345c0beSdougm  * validpath(path, strictness)
4646185db85Sdougm  * determine if the provided path is valid for a share. It shouldn't
4656185db85Sdougm  * be a sub-dir of an already shared path or the parent directory of a
4666185db85Sdougm  * share path.
4676185db85Sdougm  */
4686185db85Sdougm static int
469*549ec3ffSdougm validpath(sa_handle_t handle, char *path, int strictness)
4706185db85Sdougm {
4716185db85Sdougm 	int error = SA_OK;
4726185db85Sdougm 	struct stat st;
4736185db85Sdougm 	sa_share_t share;
4746185db85Sdougm 	char *fstype;
4756185db85Sdougm 
4766185db85Sdougm 	if (*path != '/') {
4776185db85Sdougm 	    return (SA_BAD_PATH);
4786185db85Sdougm 	}
4796185db85Sdougm 	if (stat(path, &st) < 0) {
4806185db85Sdougm 	    error = SA_NO_SUCH_PATH;
4816185db85Sdougm 	} else {
482*549ec3ffSdougm 	    share = sa_find_share(handle, path);
4836185db85Sdougm 	    if (share != NULL) {
4846185db85Sdougm 		error = SA_DUPLICATE_NAME;
4856185db85Sdougm 	    }
4866185db85Sdougm 	    if (error == SA_OK) {
4876185db85Sdougm 		/*
4886185db85Sdougm 		 * check for special case with file system that might
4896185db85Sdougm 		 * have restrictions.  For now, ZFS is the only case
4906185db85Sdougm 		 * since it has its own idea of how to configure
4916185db85Sdougm 		 * shares. We do this before subdir checking since
4926185db85Sdougm 		 * things like ZFS will do that for us. This should
4936185db85Sdougm 		 * also be done via plugin interface.
4946185db85Sdougm 		 */
4956185db85Sdougm 		fstype = sa_fstype(path);
4966185db85Sdougm 		if (fstype != NULL && strcmp(fstype, "zfs") == 0) {
497*549ec3ffSdougm 		    if (sa_zfs_is_shared(handle, path))
498a99982a7Sdougm 			error = SA_INVALID_NAME;
4996185db85Sdougm 		}
5006185db85Sdougm 		if (fstype != NULL)
5016185db85Sdougm 		    sa_free_fstype(fstype);
5026185db85Sdougm 	    }
5036185db85Sdougm 	    if (error == SA_OK) {
504*549ec3ffSdougm 		error = checksubdir(handle, path, strictness);
5056185db85Sdougm 	    }
5066185db85Sdougm 	}
5076185db85Sdougm 	return (error);
5086185db85Sdougm }
5096185db85Sdougm 
5106185db85Sdougm /*
5116185db85Sdougm  * check to see if group/share is persistent.
5126185db85Sdougm  */
5136185db85Sdougm static int
5146185db85Sdougm is_persistent(sa_group_t group)
5156185db85Sdougm {
5166185db85Sdougm 	char *type;
5176185db85Sdougm 	int persist = 1;
5186185db85Sdougm 
5196185db85Sdougm 	type = sa_get_group_attr(group, "type");
5206185db85Sdougm 	if (type != NULL && strcmp(type, "transient") == 0)
5216185db85Sdougm 	    persist = 0;
5226185db85Sdougm 	if (type != NULL)
5236185db85Sdougm 	    sa_free_attr_string(type);
5246185db85Sdougm 	return (persist);
5256185db85Sdougm }
5266185db85Sdougm 
5276185db85Sdougm /*
5286185db85Sdougm  * sa_valid_group_name(name)
5296185db85Sdougm  *
5306185db85Sdougm  * check that the "name" contains only valid characters and otherwise
5316185db85Sdougm  * fits the required naming conventions. Valid names must start with
5326185db85Sdougm  * an alphabetic and the remainder may consist of only alphanumeric
5336185db85Sdougm  * plus the '-' and '_' characters. This name limitation comes from
5346185db85Sdougm  * inherent limitations in SMF.
5356185db85Sdougm  */
5366185db85Sdougm 
5376185db85Sdougm int
5386185db85Sdougm sa_valid_group_name(char *name)
5396185db85Sdougm {
5406185db85Sdougm 	int ret = 1;
5416185db85Sdougm 	ssize_t len;
5426185db85Sdougm 
5436185db85Sdougm 	if (name != NULL && isalpha(*name)) {
5446185db85Sdougm 	    char c;
5456185db85Sdougm 	    len = strlen(name);
5466185db85Sdougm 	    if (len < (scf_max_name_len - sizeof ("group:"))) {
5476185db85Sdougm 		for (c = *name++; c != '\0' && ret != 0; c = *name++) {
5486185db85Sdougm 		    if (!isalnum(c) && c != '-' && c != '_')
5496185db85Sdougm 			ret = 0;
5506185db85Sdougm 		}
5516185db85Sdougm 	    } else {
5526185db85Sdougm 		ret = 0;
5536185db85Sdougm 	    }
5546185db85Sdougm 	} else {
5556185db85Sdougm 	    ret = 0;
5566185db85Sdougm 	}
5576185db85Sdougm 	return (ret);
5586185db85Sdougm }
5596185db85Sdougm 
5606185db85Sdougm 
5616185db85Sdougm /*
5626185db85Sdougm  * is_zfs_group(group)
5636185db85Sdougm  *	Determine if the specified group is a ZFS sharenfs group
5646185db85Sdougm  */
5656185db85Sdougm static int
5666185db85Sdougm is_zfs_group(sa_group_t group)
5676185db85Sdougm {
5686185db85Sdougm 	int ret = 0;
5696185db85Sdougm 	xmlNodePtr parent;
5706185db85Sdougm 	xmlChar *zfs;
5716185db85Sdougm 
5726185db85Sdougm 	if (strcmp((char *)((xmlNodePtr)group)->name, "share") == 0) {
5736185db85Sdougm 	    parent = (xmlNodePtr)sa_get_parent_group(group);
5746185db85Sdougm 	} else {
5756185db85Sdougm 	    parent = (xmlNodePtr)group;
5766185db85Sdougm 	}
5776185db85Sdougm 	zfs = xmlGetProp(parent, (xmlChar *)"zfs");
5786185db85Sdougm 	if (zfs != NULL) {
5796185db85Sdougm 	    xmlFree(zfs);
5806185db85Sdougm 	    ret = 1;
5816185db85Sdougm 	}
5826185db85Sdougm 	return (ret);
5836185db85Sdougm }
5846185db85Sdougm 
5856185db85Sdougm /*
5866185db85Sdougm  * sa_optionset_name(optionset, oname, len, id)
5876185db85Sdougm  *	return the SMF name for the optionset. If id is not NULL, it
5886185db85Sdougm  *	will have the GUID value for a share and should be used
5896185db85Sdougm  *	instead of the keyword "optionset" which is used for
5906185db85Sdougm  *	groups. If the optionset doesn't have a protocol type
5916185db85Sdougm  *	associated with it, "default" is used. This shouldn't happen
5926185db85Sdougm  *	at this point but may be desirable in the future if there are
5936185db85Sdougm  *	protocol independent properties added. The name is returned in
5946185db85Sdougm  *	oname.
5956185db85Sdougm  */
5966185db85Sdougm 
5976185db85Sdougm static int
5986185db85Sdougm sa_optionset_name(sa_optionset_t optionset, char *oname, size_t len, char *id)
5996185db85Sdougm {
6006185db85Sdougm 	char *proto;
6016185db85Sdougm 
6026185db85Sdougm 	if (id == NULL)
6036185db85Sdougm 	    id = "optionset";
6046185db85Sdougm 
6056185db85Sdougm 	proto = sa_get_optionset_attr(optionset, "type");
6066185db85Sdougm 	len = snprintf(oname, len, "%s_%s", id, proto ? proto : "default");
6076185db85Sdougm 
6086185db85Sdougm 	if (proto != NULL)
6096185db85Sdougm 	    sa_free_attr_string(proto);
6106185db85Sdougm 	return (len);
6116185db85Sdougm }
6126185db85Sdougm 
6136185db85Sdougm /*
6146185db85Sdougm  * sa_security_name(optionset, oname, len, id)
6156185db85Sdougm  *
6166185db85Sdougm  * return the SMF name for the security. If id is not NULL, it will
6176185db85Sdougm  * have the GUID value for a share and should be used instead of the
6186185db85Sdougm  * keyword "optionset" which is used for groups. If the optionset
6196185db85Sdougm  * doesn't have a protocol type associated with it, "default" is
6206185db85Sdougm  * used. This shouldn't happen at this point but may be desirable in
6216185db85Sdougm  * the future if there are protocol independent properties added. The
6226185db85Sdougm  * name is returned in oname. The security type is also encoded into
6236185db85Sdougm  * the name. In the future, this wil *be handled a bit differently.
6246185db85Sdougm  */
6256185db85Sdougm 
6266185db85Sdougm static int
6276185db85Sdougm sa_security_name(sa_security_t security, char *oname, size_t len, char *id)
6286185db85Sdougm {
6296185db85Sdougm 	char *proto;
6306185db85Sdougm 	char *sectype;
6316185db85Sdougm 
6326185db85Sdougm 	if (id == NULL)
6336185db85Sdougm 	    id = "optionset";
6346185db85Sdougm 
6356185db85Sdougm 	proto = sa_get_security_attr(security, "type");
6366185db85Sdougm 	sectype = sa_get_security_attr(security, "sectype");
6376185db85Sdougm 	len = snprintf(oname, len, "%s_%s_%s", id,
6386185db85Sdougm 			    proto ? proto : "default",
6396185db85Sdougm 			    sectype ? sectype : "default");
6406185db85Sdougm 	if (proto != NULL)
6416185db85Sdougm 	    sa_free_attr_string(proto);
6426185db85Sdougm 	if (sectype != NULL)
6436185db85Sdougm 	    sa_free_attr_string(sectype);
6446185db85Sdougm 	return (len);
6456185db85Sdougm }
6466185db85Sdougm 
6476185db85Sdougm /*
648f345c0beSdougm  * sa_init(init_service)
6496185db85Sdougm  *	Initialize the API
6506185db85Sdougm  *	find all the shared objects
6516185db85Sdougm  *	init the tables with all objects
6526185db85Sdougm  *	read in the current configuration
6536185db85Sdougm  */
6546185db85Sdougm 
655*549ec3ffSdougm sa_handle_t
6566185db85Sdougm sa_init(int init_service)
6576185db85Sdougm {
6586185db85Sdougm 	struct stat st;
6596185db85Sdougm 	int legacy = 0;
6606185db85Sdougm 	uint64_t tval = 0;
661a99982a7Sdougm 	int lockfd;
662a99982a7Sdougm 	sigset_t old;
663a99982a7Sdougm 	int updatelegacy = B_FALSE;
664a99982a7Sdougm 	scf_simple_prop_t *prop;
665*549ec3ffSdougm 	sa_handle_impl_t handle;
666*549ec3ffSdougm 	int err;
6676185db85Sdougm 
668*549ec3ffSdougm 	handle = calloc(sizeof (struct sa_handle_impl), 1);
669*549ec3ffSdougm 
670*549ec3ffSdougm 	if (handle != NULL) {
6716185db85Sdougm 	    /* get protocol specific structures */
6726185db85Sdougm 	    (void) proto_plugin_init();
6736185db85Sdougm 	    if (init_service & SA_INIT_SHARE_API) {
6746185db85Sdougm 		/*
6751cea05afSdougm 		 * initialize access into libzfs. We use this when
6761cea05afSdougm 		 * collecting info about ZFS datasets and shares.
6771cea05afSdougm 		 */
678*549ec3ffSdougm 		sa_zfs_init(handle);
6791cea05afSdougm 		/*
6806185db85Sdougm 		 * since we want to use SMF, initialize an svc handle
6816185db85Sdougm 		 * and find out what is there.
6826185db85Sdougm 		 */
683*549ec3ffSdougm 		handle->scfhandle = sa_scf_init(handle);
684*549ec3ffSdougm 		if (handle->scfhandle != NULL) {
685a99982a7Sdougm 			/*
686a99982a7Sdougm 			 * Need to lock the extraction of the
687a99982a7Sdougm 			 * configuration if the dfstab file has
688a99982a7Sdougm 			 * changed. Lock everything now and release if
689a99982a7Sdougm 			 * not needed.  Use a file that isn't being
690a99982a7Sdougm 			 * manipulated by other parts of the system in
691a99982a7Sdougm 			 * order to not interfere with locking. Using
692a99982a7Sdougm 			 * dfstab doesn't work.
693a99982a7Sdougm 			 */
694a99982a7Sdougm 		    sablocksigs(&old);
695a99982a7Sdougm 		    lockfd = open(DFS_LOCK_FILE, O_RDWR);
696a99982a7Sdougm 		    if (lockfd >= 0) {
697a99982a7Sdougm 			extern int errno;
698a99982a7Sdougm 			errno = 0;
699a99982a7Sdougm 			(void) lockf(lockfd, F_LOCK, 0);
700a99982a7Sdougm 			/*
701a99982a7Sdougm 			 * Check whether we are going to need to merge
702a99982a7Sdougm 			 * any dfstab changes. This is done by
703a99982a7Sdougm 			 * comparing the value of legacy-timestamp
704a99982a7Sdougm 			 * with the current st_ctim of the file. If
705a99982a7Sdougm 			 * they are different, an update is needed and
706a99982a7Sdougm 			 * the file must remain locked until the merge
707a99982a7Sdougm 			 * is done in order to prevent multiple
708a99982a7Sdougm 			 * startups from changing the SMF repository
709a99982a7Sdougm 			 * at the same time.  The first to get the
710a99982a7Sdougm 			 * lock will make any changes before the
711a99982a7Sdougm 			 * others can read the repository.
712a99982a7Sdougm 			 */
713*549ec3ffSdougm 			prop = scf_simple_prop_get(handle->scfhandle->handle,
714a99982a7Sdougm 						(const char *)
715a99982a7Sdougm 						    SA_SVC_FMRI_BASE ":default",
716a99982a7Sdougm 						"operation",
717a99982a7Sdougm 						"legacy-timestamp");
718a99982a7Sdougm 			if (prop != NULL) {
719a99982a7Sdougm 			    char *i64;
720a99982a7Sdougm 			    i64 = scf_simple_prop_next_astring(prop);
721a99982a7Sdougm 			    if (i64 != NULL) {
722a99982a7Sdougm 				tval = strtoull(i64, NULL, 0);
723a99982a7Sdougm 			    }
724a99982a7Sdougm 			    if (stat(SA_LEGACY_DFSTAB, &st) >= 0 &&
725a99982a7Sdougm 				tval != TSTAMP(st.st_ctim)) {
726a99982a7Sdougm 				updatelegacy = B_TRUE;
727a99982a7Sdougm 			    }
728a99982a7Sdougm 			} else {
729a99982a7Sdougm 			    /* We haven't set the timestamp before so do it. */
730a99982a7Sdougm 			    updatelegacy = B_TRUE;
731a99982a7Sdougm 			}
732a99982a7Sdougm 		    }
733a99982a7Sdougm 		    if (updatelegacy == B_FALSE) {
734a99982a7Sdougm 			/* Don't need the lock anymore */
735a99982a7Sdougm 			(void) lockf(lockfd, F_ULOCK, 0);
736a99982a7Sdougm 			(void) close(lockfd);
737a99982a7Sdougm 		    }
738*549ec3ffSdougm 		    (void) sa_get_config(handle->scfhandle, &handle->tree,
739*549ec3ffSdougm 				    &handle->doc, handle);
740*549ec3ffSdougm 		    /* need to setup for later searchs when using group/share */
741*549ec3ffSdougm 		    err = add_handle_for_root(handle->tree, handle);
742a99982a7Sdougm 		    saunblocksigs(&old);
743*549ec3ffSdougm 
744*549ec3ffSdougm 		    if (err != SA_OK) {
745*549ec3ffSdougm 			/*
746*549ec3ffSdougm 			 * If we couldn't add the tree handle to the
747*549ec3ffSdougm 			 * list, then things are going to fail
748*549ec3ffSdougm 			 * badly. Might as well undo everything now
749*549ec3ffSdougm 			 * and fail the sa_init().
750*549ec3ffSdougm 			 */
751*549ec3ffSdougm 			sa_fini(handle);
752*549ec3ffSdougm 			return (NULL);
753*549ec3ffSdougm 		    }
754*549ec3ffSdougm 
7556185db85Sdougm 		    if (tval == 0) {
7566185db85Sdougm 			/* first time so make sure default is setup */
7576185db85Sdougm 			sa_group_t defgrp;
7586185db85Sdougm 			sa_optionset_t opt;
759*549ec3ffSdougm 			defgrp = sa_get_group(handle, "default");
7606185db85Sdougm 			if (defgrp != NULL) {
7616185db85Sdougm 			    opt = sa_get_optionset(defgrp, NULL);
7626185db85Sdougm 			    if (opt == NULL)
7636185db85Sdougm 				/* NFS is the default for default */
7646185db85Sdougm 				opt = sa_create_optionset(defgrp, "nfs");
7656185db85Sdougm 			}
7666185db85Sdougm 		    }
767a99982a7Sdougm 		    if (updatelegacy == B_TRUE) {
768a99982a7Sdougm 			sablocksigs(&old);
769*549ec3ffSdougm 			getlegacyconfig((sa_handle_t)handle,
770*549ec3ffSdougm 					    SA_LEGACY_DFSTAB, &handle->tree);
7716185db85Sdougm 			if (stat(SA_LEGACY_DFSTAB, &st) >= 0)
772*549ec3ffSdougm 			    set_legacy_timestamp(handle->tree,
7736185db85Sdougm 						SA_LEGACY_DFSTAB,
7746185db85Sdougm 						TSTAMP(st.st_ctim));
775a99982a7Sdougm 			saunblocksigs(&old);
776a99982a7Sdougm 			/* Safe to unlock now to allow others to run */
777a99982a7Sdougm 			(void) lockf(lockfd, F_ULOCK, 0);
778a99982a7Sdougm 			(void) close(lockfd);
7796185db85Sdougm 		    }
780*549ec3ffSdougm 		    legacy |= sa_get_zfs_shares(handle, "zfs");
781*549ec3ffSdougm 		    legacy |= gettransients(handle, &handle->tree);
7826185db85Sdougm 		}
7836185db85Sdougm 	    }
7846185db85Sdougm 	}
785*549ec3ffSdougm 	return ((sa_handle_t)handle);
7866185db85Sdougm }
7876185db85Sdougm 
7886185db85Sdougm /*
789*549ec3ffSdougm  * sa_fini(handle)
7906185db85Sdougm  *	Uninitialize the API structures including the configuration
7911cea05afSdougm  *	data structures and ZFS related data.
7926185db85Sdougm  */
7936185db85Sdougm 
7946185db85Sdougm void
795*549ec3ffSdougm sa_fini(sa_handle_t handle)
7966185db85Sdougm {
797*549ec3ffSdougm 	sa_handle_impl_t impl_handle = (sa_handle_impl_t)handle;
798*549ec3ffSdougm 
799*549ec3ffSdougm 	if (impl_handle != NULL) {
800*549ec3ffSdougm 		/*
801*549ec3ffSdougm 		 * Free the config trees and any other data structures
802*549ec3ffSdougm 		 * used in the handle.
803*549ec3ffSdougm 		 */
804*549ec3ffSdougm 		if (impl_handle->doc != NULL)
805*549ec3ffSdougm 			xmlFreeDoc(impl_handle->doc);
806*549ec3ffSdougm 		sa_scf_fini(impl_handle->scfhandle);
807*549ec3ffSdougm 		sa_zfs_fini(impl_handle);
808*549ec3ffSdougm 
809*549ec3ffSdougm 		/* Remove and free the entry in the global list. */
810*549ec3ffSdougm 		remove_handle_for_root(impl_handle->tree);
811*549ec3ffSdougm 
812*549ec3ffSdougm 		/* Make sure we free the handle */
813*549ec3ffSdougm 		free(impl_handle);
814*549ec3ffSdougm 
815*549ec3ffSdougm 		/*
816*549ec3ffSdougm 		 * If this was the last handle to release, unload the
817*549ec3ffSdougm 		 * plugins that were loaded.
818*549ec3ffSdougm 		 */
819*549ec3ffSdougm 		if (sa_global_handles == NULL)
820*549ec3ffSdougm 		    (void) proto_plugin_fini();
821*549ec3ffSdougm 
8226185db85Sdougm 	}
8236185db85Sdougm }
8246185db85Sdougm 
8256185db85Sdougm /*
8266185db85Sdougm  * sa_get_protocols(char **protocol)
8276185db85Sdougm  *	Get array of protocols that are supported
8286185db85Sdougm  *	Returns pointer to an allocated and NULL terminated
8296185db85Sdougm  *	array of strings.  Caller must free.
8306185db85Sdougm  *	This really should be determined dynamically.
8316185db85Sdougm  *	If there aren't any defined, return -1.
8326185db85Sdougm  *	Use free() to return memory.
8336185db85Sdougm  */
8346185db85Sdougm 
8356185db85Sdougm int
8366185db85Sdougm sa_get_protocols(char ***protocols)
8376185db85Sdougm {
8386185db85Sdougm 	int numproto = -1;
8396185db85Sdougm 
8406185db85Sdougm 	if (protocols != NULL) {
8416185db85Sdougm 	    struct sa_proto_plugin *plug;
8426185db85Sdougm 	    for (numproto = 0, plug = sap_proto_list; plug != NULL;
8436185db85Sdougm 		plug = plug->plugin_next) {
8446185db85Sdougm 		numproto++;
8456185db85Sdougm 	    }
8466185db85Sdougm 
8476185db85Sdougm 	    *protocols = calloc(numproto + 1,  sizeof (char *));
8486185db85Sdougm 	    if (*protocols != NULL) {
8496185db85Sdougm 		int ret = 0;
8506185db85Sdougm 		for (plug = sap_proto_list; plug != NULL;
8516185db85Sdougm 		    plug = plug->plugin_next) {
8526185db85Sdougm 		    /* faking for now */
8536185db85Sdougm 		    (*protocols)[ret++] = plug->plugin_ops->sa_protocol;
8546185db85Sdougm 		}
8556185db85Sdougm 	    } else {
8566185db85Sdougm 		numproto = -1;
8576185db85Sdougm 	    }
8586185db85Sdougm 	}
8596185db85Sdougm 	return (numproto);
8606185db85Sdougm }
8616185db85Sdougm 
8626185db85Sdougm /*
8636185db85Sdougm  * find_group_by_name(node, group)
8646185db85Sdougm  *
8656185db85Sdougm  * search the XML document subtree specified by node to find the group
8666185db85Sdougm  * specified by group. Searching subtree allows subgroups to be
8676185db85Sdougm  * searched for.
8686185db85Sdougm  */
8696185db85Sdougm 
8706185db85Sdougm static xmlNodePtr
8716185db85Sdougm find_group_by_name(xmlNodePtr node, xmlChar *group)
8726185db85Sdougm {
8736185db85Sdougm 	xmlChar *name = NULL;
8746185db85Sdougm 
8756185db85Sdougm 	for (node = node->xmlChildrenNode; node != NULL;
8766185db85Sdougm 	    node = node->next) {
8776185db85Sdougm 	    if (xmlStrcmp(node->name, (xmlChar *)"group") == 0) {
8786185db85Sdougm 		/* if no groupname, return the first found */
8796185db85Sdougm 		if (group == NULL)
8806185db85Sdougm 		    break;
8816185db85Sdougm 		name = xmlGetProp(node, (xmlChar *)"name");
8826185db85Sdougm 		if (name != NULL &&
8836185db85Sdougm 		    xmlStrcmp(name, group) == 0) {
8846185db85Sdougm 		    break;
8856185db85Sdougm 		}
8866185db85Sdougm 		if (name != NULL) {
8876185db85Sdougm 		    xmlFree(name);
8886185db85Sdougm 		    name = NULL;
8896185db85Sdougm 		}
8906185db85Sdougm 	    }
8916185db85Sdougm 	}
8926185db85Sdougm 	if (name != NULL)
8936185db85Sdougm 	    xmlFree(name);
8946185db85Sdougm 	return (node);
8956185db85Sdougm }
8966185db85Sdougm 
8976185db85Sdougm /*
8986185db85Sdougm  * sa_get_group(groupname)
8996185db85Sdougm  *	Return the "group" specified.  If groupname is NULL,
9006185db85Sdougm  *	return the first group of the list of groups.
9016185db85Sdougm  */
9026185db85Sdougm sa_group_t
903*549ec3ffSdougm sa_get_group(sa_handle_t handle, char *groupname)
9046185db85Sdougm {
9056185db85Sdougm 	xmlNodePtr node = NULL;
9066185db85Sdougm 	char *subgroup = NULL;
9076185db85Sdougm 	char *group = NULL;
908*549ec3ffSdougm 	sa_handle_impl_t impl_handle = (sa_handle_impl_t)handle;
9096185db85Sdougm 
910*549ec3ffSdougm 	if (impl_handle != NULL && impl_handle->tree != NULL) {
9116185db85Sdougm 	    if (groupname != NULL) {
9126185db85Sdougm 		group = strdup(groupname);
9136185db85Sdougm 		subgroup = strchr(group, '/');
9146185db85Sdougm 		if (subgroup != NULL)
9156185db85Sdougm 		    *subgroup++ = '\0';
9166185db85Sdougm 	    }
917*549ec3ffSdougm 	    node = find_group_by_name(impl_handle->tree, (xmlChar *)group);
9186185db85Sdougm 	    /* if a subgroup, find it before returning */
9196185db85Sdougm 	    if (subgroup != NULL && node != NULL) {
9206185db85Sdougm 		node = find_group_by_name(node, (xmlChar *)subgroup);
9216185db85Sdougm 	    }
9226185db85Sdougm 	}
9236185db85Sdougm 	if (node != NULL && (char *)group != NULL)
924*549ec3ffSdougm 	    (void) sa_get_instance(impl_handle->scfhandle, (char *)group);
9256185db85Sdougm 	if (group != NULL)
9266185db85Sdougm 	    free(group);
9276185db85Sdougm 	return ((sa_group_t)(node));
9286185db85Sdougm }
9296185db85Sdougm 
9306185db85Sdougm /*
9316185db85Sdougm  * sa_get_next_group(group)
9326185db85Sdougm  *	Return the "next" group after the specified group from
9336185db85Sdougm  *	the internal group list.  NULL if there are no more.
9346185db85Sdougm  */
9356185db85Sdougm sa_group_t
9366185db85Sdougm sa_get_next_group(sa_group_t group)
9376185db85Sdougm {
9386185db85Sdougm 	xmlNodePtr ngroup = NULL;
9396185db85Sdougm 	if (group != NULL) {
9406185db85Sdougm 	    for (ngroup = ((xmlNodePtr)group)->next; ngroup != NULL;
9416185db85Sdougm 		    ngroup = ngroup->next) {
9426185db85Sdougm 		if (xmlStrcmp(ngroup->name, (xmlChar *)"group") == 0)
9436185db85Sdougm 		    break;
9446185db85Sdougm 	    }
9456185db85Sdougm 	}
9466185db85Sdougm 	return ((sa_group_t)ngroup);
9476185db85Sdougm }
9486185db85Sdougm 
9496185db85Sdougm /*
9506185db85Sdougm  * sa_get_share(group, sharepath)
9516185db85Sdougm  *	Return the share object for the share specified. The share
9526185db85Sdougm  *	must be in the specified group.  Return NULL if not found.
9536185db85Sdougm  */
9546185db85Sdougm sa_share_t
9556185db85Sdougm sa_get_share(sa_group_t group, char *sharepath)
9566185db85Sdougm {
9576185db85Sdougm 	xmlNodePtr node = NULL;
9586185db85Sdougm 	xmlChar *path;
9596185db85Sdougm 
9606185db85Sdougm 	/*
9616185db85Sdougm 	 * For future scalability, this should end up building a cache
9626185db85Sdougm 	 * since it will get called regularly by the mountd and info
9636185db85Sdougm 	 * services.
9646185db85Sdougm 	 */
9656185db85Sdougm 	if (group != NULL) {
9666185db85Sdougm 	    for (node = ((xmlNodePtr)group)->children; node != NULL;
9676185db85Sdougm 		    node = node->next) {
9686185db85Sdougm 		if (xmlStrcmp(node->name, (xmlChar *)"share") == 0) {
9696185db85Sdougm 			if (sharepath == NULL) {
9706185db85Sdougm 				break;
9716185db85Sdougm 			} else {
9726185db85Sdougm 				/* is it the correct share? */
9736185db85Sdougm 			    path = xmlGetProp(node, (xmlChar *)"path");
9746185db85Sdougm 			    if (path != NULL &&
9756185db85Sdougm 				xmlStrcmp(path, (xmlChar *)sharepath) == 0) {
9766185db85Sdougm 				xmlFree(path);
9776185db85Sdougm 				break;
9786185db85Sdougm 			    }
9796185db85Sdougm 			    xmlFree(path);
9806185db85Sdougm 			}
9816185db85Sdougm 		}
9826185db85Sdougm 	    }
9836185db85Sdougm 	}
9846185db85Sdougm 	return ((sa_share_t)node);
9856185db85Sdougm }
9866185db85Sdougm 
9876185db85Sdougm /*
9886185db85Sdougm  * sa_get_next_share(share)
9896185db85Sdougm  *	Return the next share following the specified share
9906185db85Sdougm  *	from the internal list of shares. Returns NULL if there
9916185db85Sdougm  *	are no more shares.  The list is relative to the same
9926185db85Sdougm  *	group.
9936185db85Sdougm  */
9946185db85Sdougm sa_share_t
9956185db85Sdougm sa_get_next_share(sa_share_t share)
9966185db85Sdougm {
9976185db85Sdougm 	xmlNodePtr node = NULL;
9986185db85Sdougm 
9996185db85Sdougm 	if (share != NULL) {
10006185db85Sdougm 	    for (node = ((xmlNodePtr)share)->next; node != NULL;
10016185db85Sdougm 		    node = node->next) {
10026185db85Sdougm 		if (xmlStrcmp(node->name, (xmlChar *)"share") == 0) {
10036185db85Sdougm 			break;
10046185db85Sdougm 		}
10056185db85Sdougm 	    }
10066185db85Sdougm 	}
10076185db85Sdougm 	return ((sa_share_t)node);
10086185db85Sdougm }
10096185db85Sdougm 
10106185db85Sdougm /*
10116185db85Sdougm  * _sa_get_child_node(node, type)
10126185db85Sdougm  *
10136185db85Sdougm  * find the child node of the specified node that has "type". This is
10146185db85Sdougm  * used to implement several internal functions.
10156185db85Sdougm  */
10166185db85Sdougm 
10176185db85Sdougm static xmlNodePtr
10186185db85Sdougm _sa_get_child_node(xmlNodePtr node, xmlChar *type)
10196185db85Sdougm {
10206185db85Sdougm 	xmlNodePtr child;
10216185db85Sdougm 	for (child = node->xmlChildrenNode; child != NULL;
10226185db85Sdougm 	    child = child->next)
10236185db85Sdougm 	    if (xmlStrcmp(child->name, type) == 0)
10246185db85Sdougm 		return (child);
10256185db85Sdougm 	return ((xmlNodePtr)NULL);
10266185db85Sdougm }
10276185db85Sdougm 
10286185db85Sdougm /*
10296185db85Sdougm  *  find_share(group, path)
10306185db85Sdougm  *
10316185db85Sdougm  * Search all the shares in the specified group for one that has the
10326185db85Sdougm  * specified path.
10336185db85Sdougm  */
10346185db85Sdougm 
10356185db85Sdougm static sa_share_t
10366185db85Sdougm find_share(sa_group_t group, char *sharepath)
10376185db85Sdougm {
10386185db85Sdougm 	sa_share_t share;
10396185db85Sdougm 	char *path;
10406185db85Sdougm 
10416185db85Sdougm 	for (share = sa_get_share(group, NULL); share != NULL;
10426185db85Sdougm 	    share = sa_get_next_share(share)) {
10436185db85Sdougm 	    path = sa_get_share_attr(share, "path");
10446185db85Sdougm 	    if (path != NULL && strcmp(path, sharepath) == 0) {
10456185db85Sdougm 		sa_free_attr_string(path);
10466185db85Sdougm 		break;
10476185db85Sdougm 	    }
10486185db85Sdougm 	    if (path != NULL)
10496185db85Sdougm 		sa_free_attr_string(path);
10506185db85Sdougm 	}
10516185db85Sdougm 	return (share);
10526185db85Sdougm }
10536185db85Sdougm 
10546185db85Sdougm /*
10556185db85Sdougm  * sa_get_sub_group(group)
10566185db85Sdougm  *
10576185db85Sdougm  * Get the first sub-group of group. The sa_get_next_group() function
10586185db85Sdougm  * can be used to get the rest. This is currently only used for ZFS
10596185db85Sdougm  * sub-groups but could be used to implement a more general mechanism.
10606185db85Sdougm  */
10616185db85Sdougm 
10626185db85Sdougm sa_group_t
10636185db85Sdougm sa_get_sub_group(sa_group_t group)
10646185db85Sdougm {
10656185db85Sdougm 	return ((sa_group_t)_sa_get_child_node((xmlNodePtr)group,
10666185db85Sdougm 					    (xmlChar *)"group"));
10676185db85Sdougm }
10686185db85Sdougm 
10696185db85Sdougm /*
10706185db85Sdougm  * sa_find_share(sharepath)
10716185db85Sdougm  *	Finds a share regardless of group.  In the future, this
10726185db85Sdougm  *	function should utilize a cache and hash table of some kind.
10736185db85Sdougm  *	The current assumption is that a path will only be shared
10746185db85Sdougm  *	once.  In the future, this may change as implementation of
10756185db85Sdougm  *	resource names comes into being.
10766185db85Sdougm  */
10776185db85Sdougm sa_share_t
1078*549ec3ffSdougm sa_find_share(sa_handle_t handle, char *sharepath)
10796185db85Sdougm {
10806185db85Sdougm 	sa_group_t group;
10816185db85Sdougm 	sa_group_t zgroup;
10826185db85Sdougm 	sa_share_t share = NULL;
10836185db85Sdougm 	int done = 0;
10846185db85Sdougm 
1085*549ec3ffSdougm 	for (group = sa_get_group(handle, NULL); group != NULL && !done;
10866185db85Sdougm 		group = sa_get_next_group(group)) {
10876185db85Sdougm 	    if (is_zfs_group(group)) {
10886185db85Sdougm 		for (zgroup = (sa_group_t)_sa_get_child_node((xmlNodePtr)group,
10896185db85Sdougm 							(xmlChar *)"group");
10906185db85Sdougm 		    zgroup != NULL; zgroup = sa_get_next_group(zgroup)) {
10916185db85Sdougm 		    share = find_share(zgroup, sharepath);
10926185db85Sdougm 		    if (share != NULL)
10936185db85Sdougm 			break;
10946185db85Sdougm 		}
10956185db85Sdougm 	    } else {
10966185db85Sdougm 		share = find_share(group, sharepath);
10976185db85Sdougm 	    }
10986185db85Sdougm 	    if (share != NULL)
10996185db85Sdougm 		break;
11006185db85Sdougm 	}
11016185db85Sdougm 	return (share);
11026185db85Sdougm }
11036185db85Sdougm 
11046185db85Sdougm /*
1105f345c0beSdougm  *  sa_check_path(group, path, strictness)
11066185db85Sdougm  *
11076185db85Sdougm  * check that path is a valid path relative to the group.  Currently,
11086185db85Sdougm  * we are ignoring the group and checking only the NFS rules. Later,
11096185db85Sdougm  * we may want to use the group to then check against the protocols
1110f345c0beSdougm  * enabled on the group. The strictness values mean:
1111f345c0beSdougm  * SA_CHECK_NORMAL == only check newpath against shares that are active
1112f345c0beSdougm  * SA_CHECK_STRICT == check newpath against both active shares and those
1113f345c0beSdougm  *		      stored in the repository
11146185db85Sdougm  */
11156185db85Sdougm 
11166185db85Sdougm int
1117f345c0beSdougm sa_check_path(sa_group_t group, char *path, int strictness)
11186185db85Sdougm {
1119*549ec3ffSdougm 	sa_handle_t handle;
1120*549ec3ffSdougm 
1121*549ec3ffSdougm 	handle = sa_find_group_handle(group);
1122*549ec3ffSdougm 	return (validpath(handle, path, strictness));
11236185db85Sdougm }
11246185db85Sdougm 
11256185db85Sdougm /*
11266185db85Sdougm  * _sa_add_share(group, sharepath, persist, *error)
11276185db85Sdougm  *
11286185db85Sdougm  * common code for all types of add_share. sa_add_share() is the
11296185db85Sdougm  * public API, we also need to be able to do this when parsing legacy
11306185db85Sdougm  * files and construction of the internal configuration while
11316185db85Sdougm  * extracting config info from SMF.
11326185db85Sdougm  */
11336185db85Sdougm 
11346185db85Sdougm sa_share_t
11356185db85Sdougm _sa_add_share(sa_group_t group, char *sharepath, int persist, int *error)
11366185db85Sdougm {
11376185db85Sdougm 	xmlNodePtr node = NULL;
11386185db85Sdougm 	int err;
11396185db85Sdougm 
11406185db85Sdougm 	err  = SA_OK; /* assume success */
11416185db85Sdougm 
11426185db85Sdougm 	node = xmlNewChild((xmlNodePtr)group, NULL,
11436185db85Sdougm 				(xmlChar *)"share", NULL);
11446185db85Sdougm 	if (node != NULL) {
11456185db85Sdougm 	    xmlSetProp(node, (xmlChar *)"path", (xmlChar *)sharepath);
11466185db85Sdougm 	    xmlSetProp(node, (xmlChar *)"type", persist ?
11476185db85Sdougm 			(xmlChar *)"persist" : (xmlChar *)"transient");
11486185db85Sdougm 	    if (persist != SA_SHARE_TRANSIENT) {
11496185db85Sdougm 		/*
11506185db85Sdougm 		 * persistent shares come in two flavors: SMF and
11516185db85Sdougm 		 * ZFS. Sort this one out based on target group and
11526185db85Sdougm 		 * path type. Currently, only NFS is supported in the
11536185db85Sdougm 		 * ZFS group and it is always on.
11546185db85Sdougm 		 */
11556185db85Sdougm 		if (sa_group_is_zfs(group) && sa_path_is_zfs(sharepath)) {
11566185db85Sdougm 		    err = sa_zfs_set_sharenfs(group, sharepath, 1);
11576185db85Sdougm 		} else {
1158*549ec3ffSdougm 		    sa_handle_impl_t impl_handle;
1159*549ec3ffSdougm 		    impl_handle = (sa_handle_impl_t)sa_find_group_handle(group);
1160*549ec3ffSdougm 		    if (impl_handle != NULL)
1161*549ec3ffSdougm 			err = sa_commit_share(impl_handle->scfhandle, group,
11626185db85Sdougm 						(sa_share_t)node);
1163*549ec3ffSdougm 		    else
1164*549ec3ffSdougm 			err = SA_SYSTEM_ERR;
11656185db85Sdougm 		}
11666185db85Sdougm 	    }
11676185db85Sdougm 	    if (err == SA_NO_PERMISSION && persist & SA_SHARE_PARSER) {
11686185db85Sdougm 		/* called by the dfstab parser so could be a show */
11696185db85Sdougm 		err = SA_OK;
11706185db85Sdougm 	    }
11716185db85Sdougm 	    if (err != SA_OK) {
11726185db85Sdougm 		/*
11736185db85Sdougm 		 * we couldn't commit to the repository so undo
11746185db85Sdougm 		 * our internal state to reflect reality.
11756185db85Sdougm 		 */
11766185db85Sdougm 		xmlUnlinkNode(node);
11776185db85Sdougm 		xmlFreeNode(node);
11786185db85Sdougm 		node = NULL;
11796185db85Sdougm 	    }
11806185db85Sdougm 	} else {
11816185db85Sdougm 	    err = SA_NO_MEMORY;
11826185db85Sdougm 	}
11836185db85Sdougm 	if (error != NULL)
11846185db85Sdougm 	    *error = err;
11856185db85Sdougm 	return (node);
11866185db85Sdougm }
11876185db85Sdougm 
11886185db85Sdougm /*
11896185db85Sdougm  * sa_add_share(group, sharepath, persist, *error)
11906185db85Sdougm  *
11916185db85Sdougm  *	Add a new share object to the specified group.  The share will
11926185db85Sdougm  *	have the specified sharepath and will only be constructed if
11936185db85Sdougm  *	it is a valid path to be shared.  NULL is returned on error
11946185db85Sdougm  *	and a detailed error value will be returned via the error
11956185db85Sdougm  *	pointer.
11966185db85Sdougm  */
11976185db85Sdougm sa_share_t
11986185db85Sdougm sa_add_share(sa_group_t group, char *sharepath, int persist, int *error)
11996185db85Sdougm {
12006185db85Sdougm 	xmlNodePtr node = NULL;
12016185db85Sdougm 	sa_share_t dup;
1202f345c0beSdougm 	int strictness = SA_CHECK_NORMAL;
1203*549ec3ffSdougm 	sa_handle_t handle;
1204f345c0beSdougm 
1205f345c0beSdougm 	/*
1206f345c0beSdougm 	 * If the share is to be permanent, use strict checking so a
1207f345c0beSdougm 	 * bad config doesn't get created. Transient shares only need
1208f345c0beSdougm 	 * to check against the currently active
1209f345c0beSdougm 	 * shares. SA_SHARE_PARSER is a modifier used internally to
1210f345c0beSdougm 	 * indicate that we are being called by the dfstab parser and
1211f345c0beSdougm 	 * that we need strict checking in all cases. Normally persist
1212f345c0beSdougm 	 * is in integer value but SA_SHARE_PARSER may be or'd into
1213f345c0beSdougm 	 * it as an override.
1214f345c0beSdougm 	 */
1215f345c0beSdougm 	if (persist & SA_SHARE_PARSER || persist == SA_SHARE_PERMANENT)
1216f345c0beSdougm 	    strictness = SA_CHECK_STRICT;
12176185db85Sdougm 
1218*549ec3ffSdougm 	handle = sa_find_group_handle(group);
1219*549ec3ffSdougm 
1220*549ec3ffSdougm 	if ((dup = sa_find_share(handle, sharepath)) == NULL &&
1221f345c0beSdougm 		(*error = sa_check_path(group, sharepath, strictness)) ==
1222f345c0beSdougm 			SA_OK) {
12236185db85Sdougm 	    node = _sa_add_share(group, sharepath, persist, error);
12246185db85Sdougm 	}
12256185db85Sdougm 	if (dup != NULL)
12266185db85Sdougm 	    *error = SA_DUPLICATE_NAME;
12276185db85Sdougm 
12286185db85Sdougm 	return ((sa_share_t)node);
12296185db85Sdougm }
12306185db85Sdougm 
12316185db85Sdougm /*
12326185db85Sdougm  * sa_enable_share(share, protocol)
12336185db85Sdougm  *	Enable the specified share to the specified protocol.
12346185db85Sdougm  *	If protocol is NULL, then all protocols.
12356185db85Sdougm  */
12366185db85Sdougm int
12376185db85Sdougm sa_enable_share(sa_share_t share, char *protocol)
12386185db85Sdougm {
12396185db85Sdougm 	char *sharepath;
12406185db85Sdougm 	struct stat st;
12416185db85Sdougm 	int err = 0;
12426185db85Sdougm 
12436185db85Sdougm 	sharepath = sa_get_share_attr(share, "path");
12446185db85Sdougm 	if (stat(sharepath, &st) < 0) {
12456185db85Sdougm 	    err = SA_NO_SUCH_PATH;
12466185db85Sdougm 	} else {
12476185db85Sdougm 	    /* tell the server about the share */
12486185db85Sdougm 	    if (protocol != NULL) {
12496185db85Sdougm 		/* lookup protocol specific handler */
12506185db85Sdougm 		err = sa_proto_share(protocol, share);
12516185db85Sdougm 		if (err == SA_OK)
12526185db85Sdougm 		    (void) sa_set_share_attr(share, "shared", "true");
12536185db85Sdougm 	    } else {
12546185db85Sdougm 		/* tell all protocols */
12556185db85Sdougm 		err = sa_proto_share("nfs", share); /* only NFS for now */
12566185db85Sdougm 		(void) sa_set_share_attr(share, "shared", "true");
12576185db85Sdougm 	    }
12586185db85Sdougm 	}
12596185db85Sdougm 	if (sharepath != NULL)
12606185db85Sdougm 	    sa_free_attr_string(sharepath);
12616185db85Sdougm 	return (err);
12626185db85Sdougm }
12636185db85Sdougm 
12646185db85Sdougm /*
12656185db85Sdougm  * sa_disable_share(share, protocol)
12666185db85Sdougm  *	Disable the specified share to the specified protocol.
12676185db85Sdougm  *	If protocol is NULL, then all protocols.
12686185db85Sdougm  */
12696185db85Sdougm int
12706185db85Sdougm sa_disable_share(sa_share_t share, char *protocol)
12716185db85Sdougm {
12726185db85Sdougm 	char *path;
12736185db85Sdougm 	char *shared;
12746185db85Sdougm 	int ret = SA_OK;
12756185db85Sdougm 
12766185db85Sdougm 	path = sa_get_share_attr(share, "path");
12776185db85Sdougm 	shared = sa_get_share_attr(share, "shared");
12786185db85Sdougm 
12796185db85Sdougm 	if (protocol != NULL) {
12806185db85Sdougm 	    ret = sa_proto_unshare(protocol, path);
12816185db85Sdougm 	} else {
12826185db85Sdougm 	    /* need to do all protocols */
12836185db85Sdougm 	    ret = sa_proto_unshare("nfs", path);
12846185db85Sdougm 	}
12856185db85Sdougm 	if (ret == SA_OK)
12866185db85Sdougm 		(void) sa_set_share_attr(share, "shared", NULL);
12876185db85Sdougm 	if (path != NULL)
12886185db85Sdougm 	    sa_free_attr_string(path);
12896185db85Sdougm 	if (shared != NULL)
12906185db85Sdougm 	    sa_free_attr_string(shared);
12916185db85Sdougm 	return (ret);
12926185db85Sdougm }
12936185db85Sdougm 
12946185db85Sdougm /*
12956185db85Sdougm  * sa_remove_share(share)
12966185db85Sdougm  *
12976185db85Sdougm  * remove the specified share from its containing group.
12986185db85Sdougm  * Remove from the SMF or ZFS configuration space.
12996185db85Sdougm  */
13006185db85Sdougm 
13016185db85Sdougm int
13026185db85Sdougm sa_remove_share(sa_share_t share)
13036185db85Sdougm {
13046185db85Sdougm 	sa_group_t group;
13056185db85Sdougm 	int ret = SA_OK;
13066185db85Sdougm 	char *type;
13076185db85Sdougm 	int transient = 0;
13086185db85Sdougm 	char *groupname;
13096185db85Sdougm 	char *zfs;
13106185db85Sdougm 
13116185db85Sdougm 	type = sa_get_share_attr(share, "type");
13126185db85Sdougm 	group = sa_get_parent_group(share);
13136185db85Sdougm 	zfs = sa_get_group_attr(group, "zfs");
13146185db85Sdougm 	groupname = sa_get_group_attr(group, "name");
13156185db85Sdougm 	if (type != NULL && strcmp(type, "persist") != 0)
13166185db85Sdougm 	    transient = 1;
13176185db85Sdougm 	if (type != NULL)
13186185db85Sdougm 	    sa_free_attr_string(type);
13196185db85Sdougm 
13206185db85Sdougm 	/* remove the node from its group then free the memory */
13216185db85Sdougm 
13226185db85Sdougm 	/*
13236185db85Sdougm 	 * need to test if "busy"
13246185db85Sdougm 	 */
13256185db85Sdougm 	/* only do SMF action if permanent */
13266185db85Sdougm 	if (!transient || zfs != NULL) {
13276185db85Sdougm 	    /* remove from legacy dfstab as well as possible SMF */
13286185db85Sdougm 	    ret = sa_delete_legacy(share);
13296185db85Sdougm 	    if (ret == SA_OK) {
13306185db85Sdougm 		if (!sa_group_is_zfs(group)) {
1331*549ec3ffSdougm 		    sa_handle_impl_t impl_handle;
1332*549ec3ffSdougm 		    impl_handle = (sa_handle_impl_t)sa_find_group_handle(group);
1333*549ec3ffSdougm 		    if (impl_handle != NULL)
1334*549ec3ffSdougm 			ret = sa_delete_share(impl_handle->scfhandle,
1335*549ec3ffSdougm 						group, share);
1336*549ec3ffSdougm 		    else
1337*549ec3ffSdougm 			ret = SA_SYSTEM_ERR;
13386185db85Sdougm 		} else {
13396185db85Sdougm 		    char *sharepath = sa_get_share_attr(share, "path");
13406185db85Sdougm 		    if (sharepath != NULL) {
13416185db85Sdougm 			ret = sa_zfs_set_sharenfs(group, sharepath, 0);
13426185db85Sdougm 			sa_free_attr_string(sharepath);
13436185db85Sdougm 		    }
13446185db85Sdougm 		}
13456185db85Sdougm 	    }
13466185db85Sdougm 	}
13476185db85Sdougm 	if (groupname != NULL)
13486185db85Sdougm 	    sa_free_attr_string(groupname);
13496185db85Sdougm 	if (zfs != NULL)
13506185db85Sdougm 	    sa_free_attr_string(zfs);
13516185db85Sdougm 
13526185db85Sdougm 	xmlUnlinkNode((xmlNodePtr)share);
13536185db85Sdougm 	xmlFreeNode((xmlNodePtr)share);
13546185db85Sdougm 	return (ret);
13556185db85Sdougm }
13566185db85Sdougm 
13576185db85Sdougm /*
13586185db85Sdougm  * sa_move_share(group, share)
13596185db85Sdougm  *
13606185db85Sdougm  * move the specified share to the specified group.  Update SMF
13616185db85Sdougm  * appropriately.
13626185db85Sdougm  */
13636185db85Sdougm 
13646185db85Sdougm int
13656185db85Sdougm sa_move_share(sa_group_t group, sa_share_t share)
13666185db85Sdougm {
13676185db85Sdougm 	sa_group_t oldgroup;
13686185db85Sdougm 	int ret = SA_OK;
13696185db85Sdougm 
13706185db85Sdougm 	/* remove the node from its group then free the memory */
13716185db85Sdougm 
13726185db85Sdougm 	oldgroup = sa_get_parent_group(share);
13736185db85Sdougm 	if (oldgroup != group) {
1374*549ec3ffSdougm 	    sa_handle_impl_t impl_handle;
13756185db85Sdougm 	    xmlUnlinkNode((xmlNodePtr)share);
13766185db85Sdougm 	    /* now that the share isn't in its old group, add to the new one */
13776185db85Sdougm 	    xmlAddChild((xmlNodePtr)group, (xmlNodePtr)share);
13786185db85Sdougm 	    /* need to deal with SMF */
1379*549ec3ffSdougm 	    impl_handle = (sa_handle_impl_t)sa_find_group_handle(group);
1380*549ec3ffSdougm 	    if (impl_handle != NULL) {
13816185db85Sdougm 		/*
13826185db85Sdougm 		 * need to remove from old group first and then add to
13836185db85Sdougm 		 * new group. Ideally, we would do the other order but
13846185db85Sdougm 		 * need to avoid having the share in two groups at the
13856185db85Sdougm 		 * same time.
13866185db85Sdougm 		 */
1387*549ec3ffSdougm 		ret = sa_delete_share(impl_handle->scfhandle, oldgroup, share);
1388*549ec3ffSdougm 		if (ret == SA_OK)
1389*549ec3ffSdougm 		    ret = sa_commit_share(impl_handle->scfhandle, group, share);
1390*549ec3ffSdougm 	    } else {
1391*549ec3ffSdougm 		ret = SA_SYSTEM_ERR;
13926185db85Sdougm 	    }
13936185db85Sdougm 	}
13946185db85Sdougm 	return (ret);
13956185db85Sdougm }
13966185db85Sdougm 
13976185db85Sdougm /*
13986185db85Sdougm  * sa_get_parent_group(share)
13996185db85Sdougm  *
14006185db85Sdougm  * Return the containg group for the share. If a group was actually
14016185db85Sdougm  * passed in, we don't want a parent so return NULL.
14026185db85Sdougm  */
14036185db85Sdougm 
14046185db85Sdougm sa_group_t
14056185db85Sdougm sa_get_parent_group(sa_share_t share)
14066185db85Sdougm {
14076185db85Sdougm 	xmlNodePtr node = NULL;
14086185db85Sdougm 	if (share != NULL) {
14096185db85Sdougm 	    node = ((xmlNodePtr)share)->parent;
14106185db85Sdougm 		/*
14116185db85Sdougm 		 * make sure parent is a group and not sharecfg since
14126185db85Sdougm 		 * we may be cheating and passing in a group.
14136185db85Sdougm 		 * Eventually, groups of groups might come into being.
14146185db85Sdougm 		 */
14156185db85Sdougm 	    if (node == NULL ||
14166185db85Sdougm 		xmlStrcmp(node->name, (xmlChar *)"sharecfg") == 0)
14176185db85Sdougm 		node = NULL;
14186185db85Sdougm 	}
14196185db85Sdougm 	return ((sa_group_t)node);
14206185db85Sdougm }
14216185db85Sdougm 
14226185db85Sdougm /*
1423*549ec3ffSdougm  * _sa_create_group(impl_handle, groupname)
14246185db85Sdougm  *
14256185db85Sdougm  * Create a group in the document. The caller will need to deal with
14266185db85Sdougm  * configuration store and activation.
14276185db85Sdougm  */
14286185db85Sdougm 
14296185db85Sdougm sa_group_t
1430*549ec3ffSdougm _sa_create_group(sa_handle_impl_t impl_handle, char *groupname)
14316185db85Sdougm {
14326185db85Sdougm 	xmlNodePtr node = NULL;
14336185db85Sdougm 
14346185db85Sdougm 	if (sa_valid_group_name(groupname)) {
1435*549ec3ffSdougm 	    node = xmlNewChild(impl_handle->tree, NULL,
14366185db85Sdougm 				(xmlChar *)"group", NULL);
14376185db85Sdougm 	    if (node != NULL) {
14386185db85Sdougm 		xmlSetProp(node, (xmlChar *)"name", (xmlChar *)groupname);
14396185db85Sdougm 		xmlSetProp(node, (xmlChar *)"state", (xmlChar *)"enabled");
14406185db85Sdougm 	    }
14416185db85Sdougm 	}
14426185db85Sdougm 	return ((sa_group_t)node);
14436185db85Sdougm }
14446185db85Sdougm 
14456185db85Sdougm /*
14466185db85Sdougm  * _sa_create_zfs_group(group, groupname)
14476185db85Sdougm  *
14486185db85Sdougm  * Create a ZFS subgroup under the specified group. This may
14496185db85Sdougm  * eventually form the basis of general sub-groups, but is currently
14506185db85Sdougm  * restricted to ZFS.
14516185db85Sdougm  */
14526185db85Sdougm sa_group_t
14536185db85Sdougm _sa_create_zfs_group(sa_group_t group, char *groupname)
14546185db85Sdougm {
14556185db85Sdougm 	xmlNodePtr node = NULL;
14566185db85Sdougm 
14576185db85Sdougm 	node = xmlNewChild((xmlNodePtr)group, NULL,
14586185db85Sdougm 				(xmlChar *)"group", NULL);
14596185db85Sdougm 	if (node != NULL) {
14606185db85Sdougm 		xmlSetProp(node, (xmlChar *)"name", (xmlChar *)groupname);
14616185db85Sdougm 		xmlSetProp(node, (xmlChar *)"state", (xmlChar *)"enabled");
14626185db85Sdougm 	}
14636185db85Sdougm 
14646185db85Sdougm 	return ((sa_group_t)node);
14656185db85Sdougm }
14666185db85Sdougm 
14676185db85Sdougm /*
14686185db85Sdougm  * sa_create_group(groupname, *error)
14696185db85Sdougm  *
14706185db85Sdougm  * Create a new group with groupname.  Need to validate that it is a
14716185db85Sdougm  * legal name for SMF and the construct the SMF service instance of
14726185db85Sdougm  * svc:/network/shares/group to implement the group. All necessary
14736185db85Sdougm  * operational properties must be added to the group at this point
14746185db85Sdougm  * (via the SMF transaction model).
14756185db85Sdougm  */
14766185db85Sdougm sa_group_t
1477*549ec3ffSdougm sa_create_group(sa_handle_t handle, char *groupname, int *error)
14786185db85Sdougm {
14796185db85Sdougm 	xmlNodePtr node = NULL;
14806185db85Sdougm 	sa_group_t group;
14816185db85Sdougm 	int ret;
14826185db85Sdougm 	char rbacstr[256];
1483*549ec3ffSdougm 	sa_handle_impl_t impl_handle = (sa_handle_impl_t)handle;
14846185db85Sdougm 
14856185db85Sdougm 	ret = SA_OK;
14866185db85Sdougm 
1487*549ec3ffSdougm 	if (impl_handle == NULL || impl_handle->scfhandle == NULL) {
14886185db85Sdougm 	    ret = SA_SYSTEM_ERR;
14896185db85Sdougm 	    goto err;
14906185db85Sdougm 	}
14916185db85Sdougm 
1492*549ec3ffSdougm 	group = sa_get_group(handle, groupname);
14936185db85Sdougm 	if (group != NULL) {
14946185db85Sdougm 	    ret = SA_DUPLICATE_NAME;
14956185db85Sdougm 	} else {
14966185db85Sdougm 	    if (sa_valid_group_name(groupname)) {
1497*549ec3ffSdougm 		node = xmlNewChild(impl_handle->tree, NULL,
14986185db85Sdougm 				    (xmlChar *)"group", NULL);
14996185db85Sdougm 		if (node != NULL) {
15006185db85Sdougm 		    xmlSetProp(node, (xmlChar *)"name", (xmlChar *)groupname);
15016185db85Sdougm 		    /* default to the group being enabled */
15026185db85Sdougm 		    xmlSetProp(node, (xmlChar *)"state", (xmlChar *)"enabled");
1503*549ec3ffSdougm 		    ret = sa_create_instance(impl_handle->scfhandle, groupname);
15046185db85Sdougm 		    if (ret == SA_OK) {
1505*549ec3ffSdougm 			ret = sa_start_transaction(impl_handle->scfhandle,
1506*549ec3ffSdougm 							"operation");
15076185db85Sdougm 		    }
15086185db85Sdougm 		    if (ret == SA_OK) {
1509*549ec3ffSdougm 			ret = sa_set_property(impl_handle->scfhandle,
1510*549ec3ffSdougm 						"state", "enabled");
15116185db85Sdougm 			if (ret == SA_OK) {
1512*549ec3ffSdougm 			    ret = sa_end_transaction(impl_handle->scfhandle);
15136185db85Sdougm 			} else {
1514*549ec3ffSdougm 			    sa_abort_transaction(impl_handle->scfhandle);
15156185db85Sdougm 			}
15166185db85Sdougm 		    }
15176185db85Sdougm 		    if (ret == SA_OK) {
15186185db85Sdougm 			/* initialize the RBAC strings */
1519*549ec3ffSdougm 			ret = sa_start_transaction(impl_handle->scfhandle,
1520*549ec3ffSdougm 							"general");
15216185db85Sdougm 			if (ret == SA_OK) {
15226185db85Sdougm 			    (void) snprintf(rbacstr, sizeof (rbacstr), "%s.%s",
15236185db85Sdougm 					SA_RBAC_MANAGE, groupname);
1524*549ec3ffSdougm 			    ret = sa_set_property(impl_handle->scfhandle,
15256185db85Sdougm 						    "action_authorization",
15266185db85Sdougm 						    rbacstr);
15276185db85Sdougm 			}
15286185db85Sdougm 			if (ret == SA_OK) {
15296185db85Sdougm 			    (void) snprintf(rbacstr, sizeof (rbacstr), "%s.%s",
15306185db85Sdougm 					SA_RBAC_VALUE, groupname);
1531*549ec3ffSdougm 			    ret = sa_set_property(impl_handle->scfhandle,
15326185db85Sdougm 						    "value_authorization",
15336185db85Sdougm 						    rbacstr);
15346185db85Sdougm 			}
15356185db85Sdougm 			if (ret == SA_OK) {
1536*549ec3ffSdougm 			    ret = sa_end_transaction(impl_handle->scfhandle);
15376185db85Sdougm 			} else {
1538*549ec3ffSdougm 			    sa_abort_transaction(impl_handle->scfhandle);
15396185db85Sdougm 			}
15406185db85Sdougm 		    }
15416185db85Sdougm 		    if (ret != SA_OK) {
15426185db85Sdougm 			/*
15436185db85Sdougm 			 * Couldn't commit the group so we need to
15446185db85Sdougm 			 * undo internally.
15456185db85Sdougm 			 */
15466185db85Sdougm 			xmlUnlinkNode(node);
15476185db85Sdougm 			xmlFreeNode(node);
15486185db85Sdougm 			node = NULL;
15496185db85Sdougm 		    }
15506185db85Sdougm 		} else {
15516185db85Sdougm 		    ret = SA_NO_MEMORY;
15526185db85Sdougm 		}
15536185db85Sdougm 	    } else {
15546185db85Sdougm 		ret = SA_INVALID_NAME;
15556185db85Sdougm 	    }
15566185db85Sdougm 	}
15576185db85Sdougm err:
15586185db85Sdougm 	if (error != NULL)
15596185db85Sdougm 	    *error = ret;
15606185db85Sdougm 	return ((sa_group_t)node);
15616185db85Sdougm }
15626185db85Sdougm 
15636185db85Sdougm /*
15646185db85Sdougm  * sa_remove_group(group)
15656185db85Sdougm  *
15666185db85Sdougm  * Remove the specified group. This deletes from the SMF repository.
15676185db85Sdougm  * All property groups and properties are removed.
15686185db85Sdougm  */
15696185db85Sdougm 
15706185db85Sdougm int
15716185db85Sdougm sa_remove_group(sa_group_t group)
15726185db85Sdougm {
15736185db85Sdougm 	char *name;
15746185db85Sdougm 	int ret = SA_OK;
1575*549ec3ffSdougm 	sa_handle_impl_t impl_handle;
15766185db85Sdougm 
1577*549ec3ffSdougm 	impl_handle = (sa_handle_impl_t)sa_find_group_handle(group);
1578*549ec3ffSdougm 	if (impl_handle != NULL) {
15796185db85Sdougm 	    name = sa_get_group_attr(group, "name");
15806185db85Sdougm 	    if (name != NULL) {
1581*549ec3ffSdougm 		ret = sa_delete_instance(impl_handle->scfhandle, name);
15826185db85Sdougm 		sa_free_attr_string(name);
15836185db85Sdougm 	    }
15846185db85Sdougm 	    xmlUnlinkNode((xmlNodePtr)group); /* make sure unlinked */
15856185db85Sdougm 	    xmlFreeNode((xmlNodePtr)group);   /* now it is gone */
1586*549ec3ffSdougm 	} else {
1587*549ec3ffSdougm 	    ret = SA_SYSTEM_ERR;
1588*549ec3ffSdougm 	}
15896185db85Sdougm 	return (ret);
15906185db85Sdougm }
15916185db85Sdougm 
15926185db85Sdougm /*
15936185db85Sdougm  * sa_update_config()
15946185db85Sdougm  *
15956185db85Sdougm  * Used to update legacy files that need to be updated in bulk
15966185db85Sdougm  * Currently, this is a placeholder and will go away in a future
15976185db85Sdougm  * release.
15986185db85Sdougm  */
15996185db85Sdougm 
16006185db85Sdougm int
1601*549ec3ffSdougm sa_update_config(sa_handle_t handle)
16026185db85Sdougm {
16036185db85Sdougm 	/*
16046185db85Sdougm 	 * do legacy files first so we can tell when they change.
16056185db85Sdougm 	 * This will go away when we start updating individual records
16066185db85Sdougm 	 * rather than the whole file.
16076185db85Sdougm 	 */
1608*549ec3ffSdougm 	update_legacy_config(handle);
16096185db85Sdougm 	return (SA_OK);
16106185db85Sdougm }
16116185db85Sdougm 
16126185db85Sdougm /*
16136185db85Sdougm  * get_node_attr(node, tag)
16146185db85Sdougm  *
16156185db85Sdougm  * Get the speficied tag(attribute) if it exists on the node.  This is
16166185db85Sdougm  * used internally by a number of attribute oriented functions.
16176185db85Sdougm  */
16186185db85Sdougm 
16196185db85Sdougm static char *
16206185db85Sdougm get_node_attr(void *nodehdl, char *tag)
16216185db85Sdougm {
16226185db85Sdougm 	xmlNodePtr node = (xmlNodePtr)nodehdl;
16236185db85Sdougm 	xmlChar *name = NULL;
16246185db85Sdougm 
16256185db85Sdougm 	if (node != NULL) {
16266185db85Sdougm 		name = xmlGetProp(node, (xmlChar *)tag);
16276185db85Sdougm 	}
16286185db85Sdougm 	return ((char *)name);
16296185db85Sdougm }
16306185db85Sdougm 
16316185db85Sdougm /*
16326185db85Sdougm  * get_node_attr(node, tag)
16336185db85Sdougm  *
16346185db85Sdougm  * Set the speficied tag(attribute) to the specified value This is
16356185db85Sdougm  * used internally by a number of attribute oriented functions. It
16366185db85Sdougm  * doesn't update the repository, only the internal document state.
16376185db85Sdougm  */
16386185db85Sdougm 
16396185db85Sdougm void
16406185db85Sdougm set_node_attr(void *nodehdl, char *tag, char *value)
16416185db85Sdougm {
16426185db85Sdougm 	xmlNodePtr node = (xmlNodePtr)nodehdl;
16436185db85Sdougm 	if (node != NULL && tag != NULL) {
16446185db85Sdougm 		if (value != NULL) {
16456185db85Sdougm 			xmlSetProp(node, (xmlChar *)tag, (xmlChar *)value);
16466185db85Sdougm 		} else {
16476185db85Sdougm 			xmlUnsetProp(node, (xmlChar *)tag);
16486185db85Sdougm 		}
16496185db85Sdougm 	}
16506185db85Sdougm }
16516185db85Sdougm 
16526185db85Sdougm /*
16536185db85Sdougm  * sa_get_group_attr(group, tag)
16546185db85Sdougm  *
16556185db85Sdougm  * Get the specied attribute, if defined, for the group.
16566185db85Sdougm  */
16576185db85Sdougm 
16586185db85Sdougm char *
16596185db85Sdougm sa_get_group_attr(sa_group_t group, char *tag)
16606185db85Sdougm {
16616185db85Sdougm 	return (get_node_attr((void *)group, tag));
16626185db85Sdougm }
16636185db85Sdougm 
16646185db85Sdougm /*
16656185db85Sdougm  * sa_set_group_attr(group, tag, value)
16666185db85Sdougm  *
16676185db85Sdougm  * set the specified tag/attribute on the group using value as its
16686185db85Sdougm  * value.
16696185db85Sdougm  *
16706185db85Sdougm  * This will result in setting the property in the SMF repository as
16716185db85Sdougm  * well as in the internal document.
16726185db85Sdougm  */
16736185db85Sdougm 
16746185db85Sdougm int
16756185db85Sdougm sa_set_group_attr(sa_group_t group, char *tag, char *value)
16766185db85Sdougm {
16776185db85Sdougm 	int ret;
16786185db85Sdougm 	char *groupname;
1679*549ec3ffSdougm 	sa_handle_impl_t impl_handle;
16806185db85Sdougm 
1681*549ec3ffSdougm 	impl_handle = (sa_handle_impl_t)sa_find_group_handle(group);
1682*549ec3ffSdougm 	if (impl_handle != NULL) {
16836185db85Sdougm 	    groupname = sa_get_group_attr(group, "name");
1684*549ec3ffSdougm 	    ret = sa_get_instance(impl_handle->scfhandle, groupname);
16856185db85Sdougm 	    if (ret == SA_OK) {
16866185db85Sdougm 		set_node_attr((void *)group, tag, value);
1687*549ec3ffSdougm 		ret = sa_start_transaction(impl_handle->scfhandle, "operation");
16886185db85Sdougm 		if (ret == SA_OK) {
1689*549ec3ffSdougm 		    ret = sa_set_property(impl_handle->scfhandle, tag, value);
16906185db85Sdougm 		    if (ret == SA_OK)
1691*549ec3ffSdougm 			(void) sa_end_transaction(impl_handle->scfhandle);
16926185db85Sdougm 		    else {
1693*549ec3ffSdougm 			sa_abort_transaction(impl_handle->scfhandle);
16946185db85Sdougm 		    }
16956185db85Sdougm 		}
16966185db85Sdougm 	    }
16976185db85Sdougm 	    if (groupname != NULL)
16986185db85Sdougm 		sa_free_attr_string(groupname);
1699*549ec3ffSdougm 	} else {
1700*549ec3ffSdougm 	    ret = SA_SYSTEM_ERR;
1701*549ec3ffSdougm 	}
17026185db85Sdougm 	return (ret);
17036185db85Sdougm }
17046185db85Sdougm 
17056185db85Sdougm /*
17066185db85Sdougm  * sa_get_share_attr(share, tag)
17076185db85Sdougm  *
17086185db85Sdougm  * Return the value of the tag/attribute set on the specified
17096185db85Sdougm  * share. Returns NULL if the tag doesn't exist.
17106185db85Sdougm  */
17116185db85Sdougm 
17126185db85Sdougm char *
17136185db85Sdougm sa_get_share_attr(sa_share_t share, char *tag)
17146185db85Sdougm {
17156185db85Sdougm 	return (get_node_attr((void *)share, tag));
17166185db85Sdougm }
17176185db85Sdougm 
17186185db85Sdougm /*
17196185db85Sdougm  * sa_get_resource(group, resource)
17206185db85Sdougm  *
17216185db85Sdougm  * Search all the shares in the speified group for a share with a
17226185db85Sdougm  * resource name matching the one specified.
17236185db85Sdougm  *
17246185db85Sdougm  * In the future, it may be advantageous to allow group to be NULL and
17256185db85Sdougm  * search all groups but that isn't needed at present.
17266185db85Sdougm  */
17276185db85Sdougm 
17286185db85Sdougm sa_share_t
17296185db85Sdougm sa_get_resource(sa_group_t group, char *resource)
17306185db85Sdougm {
17316185db85Sdougm 	sa_share_t share = NULL;
17326185db85Sdougm 	char *name = NULL;
17336185db85Sdougm 
17346185db85Sdougm 	if (resource != NULL) {
17356185db85Sdougm 	    for (share = sa_get_share(group, NULL); share != NULL;
17366185db85Sdougm 		share = sa_get_next_share(share)) {
17376185db85Sdougm 		name = sa_get_share_attr(share, "resource");
17386185db85Sdougm 		if (name != NULL) {
17396185db85Sdougm 		    if (strcmp(name, resource) == 0)
17406185db85Sdougm 			break;
17416185db85Sdougm 		    sa_free_attr_string(name);
17426185db85Sdougm 		    name = NULL;
17436185db85Sdougm 		}
17446185db85Sdougm 	    }
17456185db85Sdougm 	    if (name != NULL)
17466185db85Sdougm 		sa_free_attr_string(name);
17476185db85Sdougm 	}
17486185db85Sdougm 	return ((sa_share_t)share);
17496185db85Sdougm }
17506185db85Sdougm 
17516185db85Sdougm /*
17526185db85Sdougm  * _sa_set_share_description(share, description)
17536185db85Sdougm  *
17546185db85Sdougm  * Add a description tag with text contents to the specified share.
17556185db85Sdougm  * A separate XML tag is used rather than a property.
17566185db85Sdougm  */
17576185db85Sdougm 
17586185db85Sdougm xmlNodePtr
17596185db85Sdougm _sa_set_share_description(sa_share_t share, char *content)
17606185db85Sdougm {
17616185db85Sdougm 	xmlNodePtr node;
17626185db85Sdougm 	node = xmlNewChild((xmlNodePtr)share,
17636185db85Sdougm 			    NULL, (xmlChar *)"description", NULL);
17646185db85Sdougm 	xmlNodeSetContent(node, (xmlChar *)content);
17656185db85Sdougm 	return (node);
17666185db85Sdougm }
17676185db85Sdougm 
17686185db85Sdougm /*
17696185db85Sdougm  * sa_set_share_attr(share, tag, value)
17706185db85Sdougm  *
17716185db85Sdougm  * Set the share attribute specified by tag to the specified value. In
17726185db85Sdougm  * the case of "resource", enforce a no duplicates in a group rule. If
17736185db85Sdougm  * the share is not transient, commit the changes to the repository
17746185db85Sdougm  * else just update the share internally.
17756185db85Sdougm  */
17766185db85Sdougm 
17776185db85Sdougm int
17786185db85Sdougm sa_set_share_attr(sa_share_t share, char *tag, char *value)
17796185db85Sdougm {
17806185db85Sdougm 	sa_group_t group;
17816185db85Sdougm 	sa_share_t resource;
17826185db85Sdougm 	int ret = SA_OK;
17836185db85Sdougm 
17846185db85Sdougm 	group = sa_get_parent_group(share);
17856185db85Sdougm 
17866185db85Sdougm 	/*
17876185db85Sdougm 	 * There are some attributes that may have specific
17886185db85Sdougm 	 * restrictions on them. Initially, only "resource" has
17896185db85Sdougm 	 * special meaning that needs to be checked. Only one instance
17906185db85Sdougm 	 * of a resource name may exist within a group.
17916185db85Sdougm 	 */
17926185db85Sdougm 
17936185db85Sdougm 	if (strcmp(tag, "resource") == 0) {
17946185db85Sdougm 	    resource = sa_get_resource(group, value);
17956185db85Sdougm 	    if (resource != share && resource != NULL)
17966185db85Sdougm 		ret = SA_DUPLICATE_NAME;
17976185db85Sdougm 	}
17986185db85Sdougm 	if (ret == SA_OK) {
17996185db85Sdougm 	    set_node_attr((void *)share, tag, value);
18006185db85Sdougm 	    if (group != NULL) {
18016185db85Sdougm 		char *type;
18026185db85Sdougm 		/* we can probably optimize this some */
18036185db85Sdougm 		type = sa_get_share_attr(share, "type");
1804*549ec3ffSdougm 		if (type == NULL || strcmp(type, "transient") != 0) {
1805*549ec3ffSdougm 		    sa_handle_impl_t impl_handle;
1806*549ec3ffSdougm 		    impl_handle = (sa_handle_impl_t)sa_find_group_handle(group);
1807*549ec3ffSdougm 		    if (impl_handle != NULL)
1808*549ec3ffSdougm 			ret = sa_commit_share(impl_handle->scfhandle,
1809*549ec3ffSdougm 						group, share);
1810*549ec3ffSdougm 		    else
1811*549ec3ffSdougm 			ret = SA_SYSTEM_ERR;
1812*549ec3ffSdougm 		}
18136185db85Sdougm 		if (type != NULL)
18146185db85Sdougm 		    sa_free_attr_string(type);
18156185db85Sdougm 	    }
18166185db85Sdougm 	}
18176185db85Sdougm 	return (ret);
18186185db85Sdougm }
18196185db85Sdougm 
18206185db85Sdougm /*
18216185db85Sdougm  * sa_get_property_attr(prop, tag)
18226185db85Sdougm  *
18236185db85Sdougm  * Get the value of the specified property attribute. Standard
18246185db85Sdougm  * attributes are "type" and "value".
18256185db85Sdougm  */
18266185db85Sdougm 
18276185db85Sdougm char *
18286185db85Sdougm sa_get_property_attr(sa_property_t prop, char *tag)
18296185db85Sdougm {
18306185db85Sdougm 	return (get_node_attr((void *)prop, tag));
18316185db85Sdougm }
18326185db85Sdougm 
18336185db85Sdougm /*
18346185db85Sdougm  * sa_get_optionset_attr(prop, tag)
18356185db85Sdougm  *
18366185db85Sdougm  * Get the value of the specified property attribute. Standard
18376185db85Sdougm  * attribute is "type".
18386185db85Sdougm  */
18396185db85Sdougm 
18406185db85Sdougm char *
18416185db85Sdougm sa_get_optionset_attr(sa_property_t optionset, char *tag)
18426185db85Sdougm {
18436185db85Sdougm 	return (get_node_attr((void *)optionset, tag));
18446185db85Sdougm 
18456185db85Sdougm }
18466185db85Sdougm 
18476185db85Sdougm /*
18486185db85Sdougm  * sa_set_optionset_attr(optionset, tag, value)
18496185db85Sdougm  *
18506185db85Sdougm  * Set the specified attribute(tag) to the specified value on the
18516185db85Sdougm  * optionset.
18526185db85Sdougm  */
18536185db85Sdougm 
18546185db85Sdougm void
18556185db85Sdougm sa_set_optionset_attr(sa_group_t optionset, char *tag, char *value)
18566185db85Sdougm {
18576185db85Sdougm 	set_node_attr((void *)optionset, tag, value);
18586185db85Sdougm }
18596185db85Sdougm 
18606185db85Sdougm /*
18616185db85Sdougm  * sa_free_attr_string(string)
18626185db85Sdougm  *
18636185db85Sdougm  * Free the string that was returned in one of the sa_get_*_attr()
18646185db85Sdougm  * functions.
18656185db85Sdougm  */
18666185db85Sdougm 
18676185db85Sdougm void
18686185db85Sdougm sa_free_attr_string(char *string)
18696185db85Sdougm {
18706185db85Sdougm 	xmlFree((xmlChar *)string);
18716185db85Sdougm }
18726185db85Sdougm 
18736185db85Sdougm /*
18746185db85Sdougm  * sa_get_optionset(group, proto)
18756185db85Sdougm  *
18766185db85Sdougm  * Return the optionset, if it exists, that is associated with the
18776185db85Sdougm  * specified protocol.
18786185db85Sdougm  */
18796185db85Sdougm 
18806185db85Sdougm sa_optionset_t
18816185db85Sdougm sa_get_optionset(void *group, char *proto)
18826185db85Sdougm {
18836185db85Sdougm 	xmlNodePtr node;
18846185db85Sdougm 	xmlChar *value = NULL;
18856185db85Sdougm 
18866185db85Sdougm 	for (node = ((xmlNodePtr)group)->children; node != NULL;
18876185db85Sdougm 		node = node->next) {
18886185db85Sdougm 		if (xmlStrcmp(node->name, (xmlChar *)"optionset") == 0) {
18896185db85Sdougm 		    value = xmlGetProp(node, (xmlChar *)"type");
18906185db85Sdougm 		    if (proto != NULL) {
18916185db85Sdougm 			if (value != NULL &&
18926185db85Sdougm 			    xmlStrcmp(value, (xmlChar *)proto) == 0) {
18936185db85Sdougm 			    break;
18946185db85Sdougm 			}
18956185db85Sdougm 			if (value != NULL) {
18966185db85Sdougm 			    xmlFree(value);
18976185db85Sdougm 			    value = NULL;
18986185db85Sdougm 			}
18996185db85Sdougm 		    } else {
19006185db85Sdougm 			break;
19016185db85Sdougm 		    }
19026185db85Sdougm 		}
19036185db85Sdougm 	}
19046185db85Sdougm 	if (value != NULL)
19056185db85Sdougm 	    xmlFree(value);
19066185db85Sdougm 	return ((sa_optionset_t)node);
19076185db85Sdougm }
19086185db85Sdougm 
19096185db85Sdougm /*
19106185db85Sdougm  * sa_get_next_optionset(optionset)
19116185db85Sdougm  *
19126185db85Sdougm  * Return the next optionset in the group. NULL if this was the last.
19136185db85Sdougm  */
19146185db85Sdougm 
19156185db85Sdougm sa_optionset_t
19166185db85Sdougm sa_get_next_optionset(sa_optionset_t optionset)
19176185db85Sdougm {
19186185db85Sdougm 	xmlNodePtr node;
19196185db85Sdougm 
19206185db85Sdougm 	for (node = ((xmlNodePtr)optionset)->next; node != NULL;
19216185db85Sdougm 		node = node->next) {
19226185db85Sdougm 		if (xmlStrcmp(node->name, (xmlChar *)"optionset") == 0) {
19236185db85Sdougm 			break;
19246185db85Sdougm 		}
19256185db85Sdougm 	}
19266185db85Sdougm 	return ((sa_optionset_t)node);
19276185db85Sdougm }
19286185db85Sdougm 
19296185db85Sdougm /*
19306185db85Sdougm  * sa_get_security(group, sectype, proto)
19316185db85Sdougm  *
19326185db85Sdougm  * Return the security optionset. The internal name is a hold over
19336185db85Sdougm  * from the implementation and will be changed before the API is
19346185db85Sdougm  * finalized. This is really a named optionset that can be negotiated
19356185db85Sdougm  * as a group of properties (like NFS security options).
19366185db85Sdougm  */
19376185db85Sdougm 
19386185db85Sdougm sa_security_t
19396185db85Sdougm sa_get_security(sa_group_t group, char *sectype, char *proto)
19406185db85Sdougm {
19416185db85Sdougm 	xmlNodePtr node;
19426185db85Sdougm 	xmlChar *value = NULL;
19436185db85Sdougm 
19446185db85Sdougm 	for (node = ((xmlNodePtr)group)->children; node != NULL;
19456185db85Sdougm 		node = node->next) {
19466185db85Sdougm 	    if (xmlStrcmp(node->name, (xmlChar *)"security") == 0) {
19476185db85Sdougm 		if (proto != NULL) {
19486185db85Sdougm 		    value = xmlGetProp(node, (xmlChar *)"type");
19496185db85Sdougm 		    if (value == NULL ||
19506185db85Sdougm 			(value != NULL &&
19516185db85Sdougm 			xmlStrcmp(value, (xmlChar *)proto) != 0)) {
19526185db85Sdougm 			/* it doesn't match so continue */
19536185db85Sdougm 			xmlFree(value);
19546185db85Sdougm 			value = NULL;
19556185db85Sdougm 			continue;
19566185db85Sdougm 		    }
19576185db85Sdougm 		}
19586185db85Sdougm 		if (value != NULL) {
19596185db85Sdougm 		    xmlFree(value);
19606185db85Sdougm 		    value = NULL;
19616185db85Sdougm 		}
19626185db85Sdougm 		/* potential match */
19636185db85Sdougm 		if (sectype != NULL) {
19646185db85Sdougm 		    value = xmlGetProp(node, (xmlChar *)"sectype");
19656185db85Sdougm 		    if (value != NULL &&
19666185db85Sdougm 			xmlStrcmp(value, (xmlChar *)sectype) == 0) {
19676185db85Sdougm 			break;
19686185db85Sdougm 		    }
19696185db85Sdougm 		} else {
19706185db85Sdougm 		    break;
19716185db85Sdougm 		}
19726185db85Sdougm 	    }
19736185db85Sdougm 	    if (value != NULL) {
19746185db85Sdougm 		xmlFree(value);
19756185db85Sdougm 		value = NULL;
19766185db85Sdougm 	    }
19776185db85Sdougm 	}
19786185db85Sdougm 	if (value != NULL)
19796185db85Sdougm 	    xmlFree(value);
19806185db85Sdougm 	return ((sa_security_t)node);
19816185db85Sdougm }
19826185db85Sdougm 
19836185db85Sdougm /*
19846185db85Sdougm  * sa_get_next_security(security)
19856185db85Sdougm  *
19866185db85Sdougm  * Get the next security optionset if one exists.
19876185db85Sdougm  */
19886185db85Sdougm 
19896185db85Sdougm sa_security_t
19906185db85Sdougm sa_get_next_security(sa_security_t security)
19916185db85Sdougm {
19926185db85Sdougm 	xmlNodePtr node;
19936185db85Sdougm 
19946185db85Sdougm 	for (node = ((xmlNodePtr)security)->next; node != NULL;
19956185db85Sdougm 		node = node->next) {
19966185db85Sdougm 		if (xmlStrcmp(node->name, (xmlChar *)"security") == 0) {
19976185db85Sdougm 			break;
19986185db85Sdougm 		}
19996185db85Sdougm 	}
20006185db85Sdougm 	return ((sa_security_t)node);
20016185db85Sdougm }
20026185db85Sdougm 
20036185db85Sdougm /*
20046185db85Sdougm  * sa_get_property(optionset, prop)
20056185db85Sdougm  *
20066185db85Sdougm  * Get the property object with the name specified in prop from the
20076185db85Sdougm  * optionset.
20086185db85Sdougm  */
20096185db85Sdougm 
20106185db85Sdougm sa_property_t
20116185db85Sdougm sa_get_property(sa_optionset_t optionset, char *prop)
20126185db85Sdougm {
20136185db85Sdougm 	xmlNodePtr node = (xmlNodePtr)optionset;
20146185db85Sdougm 	xmlChar *value = NULL;
20156185db85Sdougm 
20166185db85Sdougm 	if (optionset == NULL)
20176185db85Sdougm 	    return (NULL);
20186185db85Sdougm 
20196185db85Sdougm 	for (node = node->children; node != NULL;
20206185db85Sdougm 		node = node->next) {
20216185db85Sdougm 	    if (xmlStrcmp(node->name, (xmlChar *)"option") == 0) {
20226185db85Sdougm 		if (prop == NULL)
20236185db85Sdougm 		    break;
20246185db85Sdougm 		value = xmlGetProp(node, (xmlChar *)"type");
20256185db85Sdougm 		if (value != NULL && xmlStrcmp(value, (xmlChar *)prop) == 0) {
20266185db85Sdougm 		    break;
20276185db85Sdougm 		}
20286185db85Sdougm 		if (value != NULL) {
20296185db85Sdougm 		    xmlFree(value);
20306185db85Sdougm 		    value = NULL;
20316185db85Sdougm 		}
20326185db85Sdougm 	    }
20336185db85Sdougm 	}
20346185db85Sdougm 	if (value != NULL)
20356185db85Sdougm 		xmlFree(value);
20366185db85Sdougm 	if (node != NULL && xmlStrcmp(node->name, (xmlChar *)"option") != 0) {
20376185db85Sdougm 	    /* avoid a non option node -- it is possible to be a text node */
20386185db85Sdougm 	    node = NULL;
20396185db85Sdougm 	}
20406185db85Sdougm 	return ((sa_property_t)node);
20416185db85Sdougm }
20426185db85Sdougm 
20436185db85Sdougm /*
20446185db85Sdougm  * sa_get_next_property(property)
20456185db85Sdougm  *
20466185db85Sdougm  * Get the next property following the specified property. NULL if
20476185db85Sdougm  * this was the last.
20486185db85Sdougm  */
20496185db85Sdougm 
20506185db85Sdougm sa_property_t
20516185db85Sdougm sa_get_next_property(sa_property_t property)
20526185db85Sdougm {
20536185db85Sdougm 	xmlNodePtr node;
20546185db85Sdougm 
20556185db85Sdougm 	for (node = ((xmlNodePtr)property)->next; node != NULL;
20566185db85Sdougm 		node = node->next) {
20576185db85Sdougm 		if (xmlStrcmp(node->name, (xmlChar *)"option") == 0) {
20586185db85Sdougm 			break;
20596185db85Sdougm 		}
20606185db85Sdougm 	}
20616185db85Sdougm 	return ((sa_property_t)node);
20626185db85Sdougm }
20636185db85Sdougm 
20646185db85Sdougm /*
20656185db85Sdougm  * sa_set_share_description(share, content)
20666185db85Sdougm  *
20676185db85Sdougm  * Set the description of share to content.
20686185db85Sdougm  */
20696185db85Sdougm 
20706185db85Sdougm int
20716185db85Sdougm sa_set_share_description(sa_share_t share, char *content)
20726185db85Sdougm {
20736185db85Sdougm 	xmlNodePtr node;
20746185db85Sdougm 	sa_group_t group;
20756185db85Sdougm 	int ret = SA_OK;
20766185db85Sdougm 
20776185db85Sdougm 	for (node = ((xmlNodePtr)share)->children; node != NULL;
20786185db85Sdougm 		node = node->next) {
20796185db85Sdougm 		if (xmlStrcmp(node->name, (xmlChar *)"description") == 0) {
20806185db85Sdougm 			break;
20816185db85Sdougm 		}
20826185db85Sdougm 	}
20836185db85Sdougm 	group = sa_get_parent_group(share);
20846185db85Sdougm 	/* no existing description but want to add */
20856185db85Sdougm 	if (node == NULL && content != NULL) {
20866185db85Sdougm 		/* add a description */
20876185db85Sdougm 	    node = _sa_set_share_description(share, content);
20886185db85Sdougm 	} else if (node != NULL && content != NULL) {
20896185db85Sdougm 		/* update a description */
20906185db85Sdougm 		xmlNodeSetContent(node, (xmlChar *)content);
20916185db85Sdougm 	} else if (node != NULL && content == NULL) {
20926185db85Sdougm 		/* remove an existing description */
20936185db85Sdougm 		xmlUnlinkNode(node);
20946185db85Sdougm 		xmlFreeNode(node);
20956185db85Sdougm 	}
2096*549ec3ffSdougm 	if (group != NULL && is_persistent((sa_group_t)share)) {
2097*549ec3ffSdougm 	    sa_handle_impl_t impl_handle;
2098*549ec3ffSdougm 	    impl_handle = (sa_handle_impl_t)sa_find_group_handle(group);
2099*549ec3ffSdougm 	    if (impl_handle != NULL)
2100*549ec3ffSdougm 		ret = sa_commit_share(impl_handle->scfhandle, group, share);
2101*549ec3ffSdougm 	    else
2102*549ec3ffSdougm 		ret = SA_SYSTEM_ERR;
2103*549ec3ffSdougm 	}
21046185db85Sdougm 	return (ret);
21056185db85Sdougm }
21066185db85Sdougm 
21076185db85Sdougm /*
21086185db85Sdougm  * fixproblemchars(string)
21096185db85Sdougm  *
21106185db85Sdougm  * don't want any newline or tab characters in the text since these
21116185db85Sdougm  * could break display of data and legacy file formats.
21126185db85Sdougm  */
21136185db85Sdougm static void
21146185db85Sdougm fixproblemchars(char *str)
21156185db85Sdougm {
21166185db85Sdougm 	int c;
21176185db85Sdougm 	for (c = *str; c != '\0'; c = *++str) {
21186185db85Sdougm 	    if (c == '\t' || c == '\n')
21196185db85Sdougm 		*str = ' ';
21206185db85Sdougm 	    else if (c == '"')
21216185db85Sdougm 		*str = '\'';
21226185db85Sdougm 	}
21236185db85Sdougm }
21246185db85Sdougm 
21256185db85Sdougm /*
21266185db85Sdougm  * sa_get_share_description(share)
21276185db85Sdougm  *
21286185db85Sdougm  * Return the description text for the specified share if it
21296185db85Sdougm  * exists. NULL if no description exists.
21306185db85Sdougm  */
21316185db85Sdougm 
21326185db85Sdougm char *
21336185db85Sdougm sa_get_share_description(sa_share_t share)
21346185db85Sdougm {
21356185db85Sdougm 	xmlChar *description = NULL;
21366185db85Sdougm 	xmlNodePtr node;
21376185db85Sdougm 
21386185db85Sdougm 	for (node = ((xmlNodePtr)share)->children; node != NULL;
21396185db85Sdougm 		node = node->next) {
21406185db85Sdougm 	    if (xmlStrcmp(node->name, (xmlChar *)"description") == 0) {
21416185db85Sdougm 		break;
21426185db85Sdougm 	    }
21436185db85Sdougm 	}
21446185db85Sdougm 	if (node != NULL) {
21456185db85Sdougm 	    description = xmlNodeGetContent((xmlNodePtr)share);
21466185db85Sdougm 	    fixproblemchars((char *)description);
21476185db85Sdougm 	}
21486185db85Sdougm 	return ((char *)description);
21496185db85Sdougm }
21506185db85Sdougm 
21516185db85Sdougm /*
21526185db85Sdougm  * sa_free(share_description(description)
21536185db85Sdougm  *
21546185db85Sdougm  * Free the description string.
21556185db85Sdougm  */
21566185db85Sdougm 
21576185db85Sdougm void
21586185db85Sdougm sa_free_share_description(char *description)
21596185db85Sdougm {
21606185db85Sdougm 	xmlFree((xmlChar *)description);
21616185db85Sdougm }
21626185db85Sdougm 
21636185db85Sdougm /*
21646185db85Sdougm  * sa_create_optionset(group, proto)
21656185db85Sdougm  *
21666185db85Sdougm  * Create an optionset for the specified protocol in the specied
21676185db85Sdougm  * group. This is manifested as a property group within SMF.
21686185db85Sdougm  */
21696185db85Sdougm 
21706185db85Sdougm sa_optionset_t
21716185db85Sdougm sa_create_optionset(sa_group_t group, char *proto)
21726185db85Sdougm {
21736185db85Sdougm 	sa_optionset_t optionset;
21746185db85Sdougm 	sa_group_t parent = group;
21756185db85Sdougm 
21766185db85Sdougm 	optionset = sa_get_optionset(group, proto);
21776185db85Sdougm 	if (optionset != NULL) {
21786185db85Sdougm 		/* can't have a duplicate protocol */
21796185db85Sdougm 	    optionset = NULL;
21806185db85Sdougm 	} else {
21816185db85Sdougm 	    optionset = (sa_optionset_t)xmlNewChild((xmlNodePtr)group,
21826185db85Sdougm 						    NULL,
21836185db85Sdougm 						    (xmlChar *)"optionset",
21846185db85Sdougm 						    NULL);
21856185db85Sdougm 		/*
21866185db85Sdougm 		 * only put to repository if on a group and we were
21876185db85Sdougm 		 * able to create an optionset.
21886185db85Sdougm 		 */
21896185db85Sdougm 	    if (optionset != NULL) {
21906185db85Sdougm 		char oname[256];
21916185db85Sdougm 		char *groupname;
21926185db85Sdougm 		char *id = NULL;
21936185db85Sdougm 
21946185db85Sdougm 		if (sa_is_share(group))
21956185db85Sdougm 		    parent = sa_get_parent_group((sa_share_t)group);
21966185db85Sdougm 
21976185db85Sdougm 		sa_set_optionset_attr(optionset, "type", proto);
21986185db85Sdougm 
21996185db85Sdougm 		if (sa_is_share(group)) {
22006185db85Sdougm 			id = sa_get_share_attr((sa_share_t)group, "id");
22016185db85Sdougm 		}
22026185db85Sdougm 		(void) sa_optionset_name(optionset, oname,
22036185db85Sdougm 					sizeof (oname), id);
22046185db85Sdougm 		groupname = sa_get_group_attr(parent, "name");
22056185db85Sdougm 		if (groupname != NULL && is_persistent(group)) {
2206*549ec3ffSdougm 		    sa_handle_impl_t impl_handle;
2207*549ec3ffSdougm 		    impl_handle = (sa_handle_impl_t)sa_find_group_handle(group);
2208*549ec3ffSdougm 		    assert(impl_handle != NULL);
2209*549ec3ffSdougm 		    if (impl_handle != NULL) {
2210*549ec3ffSdougm 			(void) sa_get_instance(impl_handle->scfhandle,
2211*549ec3ffSdougm 						groupname);
2212*549ec3ffSdougm 			(void) sa_create_pgroup(impl_handle->scfhandle, oname);
22136185db85Sdougm 		    }
2214*549ec3ffSdougm 		}
2215*549ec3ffSdougm 		if (groupname != NULL)
2216*549ec3ffSdougm 		    sa_free_attr_string(groupname);
22176185db85Sdougm 		if (id != NULL)
22186185db85Sdougm 		    sa_free_attr_string(id);
22196185db85Sdougm 	    }
22206185db85Sdougm 	}
22216185db85Sdougm 	return (optionset);
22226185db85Sdougm }
22236185db85Sdougm 
22246185db85Sdougm /*
22256185db85Sdougm  * sa_get_property_parent(property)
22266185db85Sdougm  *
22276185db85Sdougm  * Given a property, return the object it is a property of. This will
22286185db85Sdougm  * be an optionset of some type.
22296185db85Sdougm  */
22306185db85Sdougm 
22316185db85Sdougm static sa_optionset_t
22326185db85Sdougm sa_get_property_parent(sa_property_t property)
22336185db85Sdougm {
22346185db85Sdougm 	xmlNodePtr node = NULL;
22356185db85Sdougm 
22366185db85Sdougm 	if (property != NULL) {
22376185db85Sdougm 	    node = ((xmlNodePtr)property)->parent;
22386185db85Sdougm 	}
22396185db85Sdougm 	return ((sa_optionset_t)node);
22406185db85Sdougm }
22416185db85Sdougm 
22426185db85Sdougm /*
22436185db85Sdougm  * sa_get_optionset_parent(optionset)
22446185db85Sdougm  *
22456185db85Sdougm  * Return the parent of the specified optionset. This could be a group
22466185db85Sdougm  * or a share.
22476185db85Sdougm  */
22486185db85Sdougm 
22496185db85Sdougm static sa_group_t
22506185db85Sdougm sa_get_optionset_parent(sa_optionset_t optionset)
22516185db85Sdougm {
22526185db85Sdougm 	xmlNodePtr node = NULL;
22536185db85Sdougm 
22546185db85Sdougm 	if (optionset != NULL) {
22556185db85Sdougm 	    node = ((xmlNodePtr)optionset)->parent;
22566185db85Sdougm 	}
22576185db85Sdougm 	return ((sa_group_t)node);
22586185db85Sdougm }
22596185db85Sdougm 
22606185db85Sdougm /*
22616185db85Sdougm  * zfs_needs_update(share)
22626185db85Sdougm  *
22636185db85Sdougm  * In order to avoid making multiple updates to a ZFS share when
22646185db85Sdougm  * setting properties, the share attribute "changed" will be set to
22656185db85Sdougm  * true when a property is added or modifed.  When done adding
22666185db85Sdougm  * properties, we can then detect that an update is needed.  We then
22676185db85Sdougm  * clear the state here to detect additional changes.
22686185db85Sdougm  */
22696185db85Sdougm 
22706185db85Sdougm static int
22716185db85Sdougm zfs_needs_update(sa_share_t share)
22726185db85Sdougm {
22736185db85Sdougm 	char *attr;
22746185db85Sdougm 	int result = 0;
22756185db85Sdougm 
22766185db85Sdougm 	attr = sa_get_share_attr(share, "changed");
22776185db85Sdougm 	if (attr != NULL) {
22786185db85Sdougm 	    sa_free_attr_string(attr);
22796185db85Sdougm 		result = 1;
22806185db85Sdougm 	}
22816185db85Sdougm 	set_node_attr((void *)share, "changed", NULL);
22826185db85Sdougm 	return (result);
22836185db85Sdougm }
22846185db85Sdougm 
22856185db85Sdougm /*
22866185db85Sdougm  * zfs_set_update(share)
22876185db85Sdougm  *
22886185db85Sdougm  * Set the changed attribute of the share to true.
22896185db85Sdougm  */
22906185db85Sdougm 
22916185db85Sdougm static void
22926185db85Sdougm zfs_set_update(sa_share_t share)
22936185db85Sdougm {
22946185db85Sdougm 	set_node_attr((void *)share, "changed", "true");
22956185db85Sdougm }
22966185db85Sdougm 
22976185db85Sdougm /*
22986185db85Sdougm  * sa_commit_properties(optionset, clear)
22996185db85Sdougm  *
23006185db85Sdougm  * Check if SMF or ZFS config and either update or abort the pending
23016185db85Sdougm  * changes.
23026185db85Sdougm  */
23036185db85Sdougm 
23046185db85Sdougm int
23056185db85Sdougm sa_commit_properties(sa_optionset_t optionset, int clear)
23066185db85Sdougm {
23076185db85Sdougm 	sa_group_t group;
23086185db85Sdougm 	sa_group_t parent;
23096185db85Sdougm 	int zfs = 0;
23106185db85Sdougm 	int needsupdate = 0;
23116185db85Sdougm 	int ret = SA_OK;
2312*549ec3ffSdougm 	sa_handle_impl_t impl_handle;
23136185db85Sdougm 
23146185db85Sdougm 	group = sa_get_optionset_parent(optionset);
23156185db85Sdougm 	if (group != NULL && (sa_is_share(group) || is_zfs_group(group))) {
23166185db85Sdougm 	    /* only update ZFS if on a share */
23176185db85Sdougm 	    parent = sa_get_parent_group(group);
23186185db85Sdougm 	    zfs++;
23196185db85Sdougm 	    if (parent != NULL && is_zfs_group(parent)) {
23206185db85Sdougm 		needsupdate = zfs_needs_update(group);
23216185db85Sdougm 	    } else {
23226185db85Sdougm 		zfs = 0;
23236185db85Sdougm 	    }
23246185db85Sdougm 	}
23256185db85Sdougm 	if (zfs) {
23266185db85Sdougm 	    if (!clear && needsupdate)
23276185db85Sdougm 		ret = sa_zfs_update((sa_share_t)group);
23286185db85Sdougm 	} else {
2329*549ec3ffSdougm 	    impl_handle = (sa_handle_impl_t)sa_find_group_handle(group);
2330*549ec3ffSdougm 	    if (impl_handle != NULL) {
23316185db85Sdougm 		if (clear)
2332*549ec3ffSdougm 		    (void) sa_abort_transaction(impl_handle->scfhandle);
23336185db85Sdougm 		else
2334*549ec3ffSdougm 		    ret = sa_end_transaction(impl_handle->scfhandle);
2335*549ec3ffSdougm 	    } else {
2336*549ec3ffSdougm 		ret = SA_SYSTEM_ERR;
2337*549ec3ffSdougm 	    }
23386185db85Sdougm 	}
23396185db85Sdougm 	return (ret);
23406185db85Sdougm }
23416185db85Sdougm 
23426185db85Sdougm /*
23436185db85Sdougm  * sa_destroy_optionset(optionset)
23446185db85Sdougm  *
23456185db85Sdougm  * Remove the optionset from its group. Update the repostory to
23466185db85Sdougm  * reflect this change.
23476185db85Sdougm  */
23486185db85Sdougm 
23496185db85Sdougm int
23506185db85Sdougm sa_destroy_optionset(sa_optionset_t optionset)
23516185db85Sdougm {
23526185db85Sdougm 	char name[256];
23536185db85Sdougm 	int len;
23546185db85Sdougm 	int ret;
23556185db85Sdougm 	char *id = NULL;
23566185db85Sdougm 	sa_group_t group;
23576185db85Sdougm 	int ispersist = 1;
23586185db85Sdougm 
23596185db85Sdougm 	/* now delete the prop group */
23606185db85Sdougm 	group = sa_get_optionset_parent(optionset);
23616185db85Sdougm 	if (group != NULL && sa_is_share(group)) {
23626185db85Sdougm 	    ispersist = is_persistent(group);
23636185db85Sdougm 	    id = sa_get_share_attr((sa_share_t)group, "id");
23646185db85Sdougm 	}
23656185db85Sdougm 	if (ispersist) {
2366*549ec3ffSdougm 	    sa_handle_impl_t impl_handle;
23676185db85Sdougm 	    len = sa_optionset_name(optionset, name, sizeof (name), id);
2368*549ec3ffSdougm 	    impl_handle = (sa_handle_impl_t)sa_find_group_handle(group);
2369*549ec3ffSdougm 	    if (impl_handle != NULL) {
23706185db85Sdougm 		if (len > 0) {
2371*549ec3ffSdougm 		    ret = sa_delete_pgroup(impl_handle->scfhandle, name);
2372*549ec3ffSdougm 		}
2373*549ec3ffSdougm 	    } else {
2374*549ec3ffSdougm 		ret = SA_SYSTEM_ERR;
23756185db85Sdougm 	    }
23766185db85Sdougm 	}
23776185db85Sdougm 	xmlUnlinkNode((xmlNodePtr)optionset);
23786185db85Sdougm 	xmlFreeNode((xmlNodePtr)optionset);
23796185db85Sdougm 	if (id != NULL)
23806185db85Sdougm 	    sa_free_attr_string(id);
23816185db85Sdougm 	return (ret);
23826185db85Sdougm }
23836185db85Sdougm 
23846185db85Sdougm /* private to the implementation */
23856185db85Sdougm int
23866185db85Sdougm _sa_remove_optionset(sa_optionset_t optionset)
23876185db85Sdougm {
23886185db85Sdougm 	int ret = SA_OK;
23896185db85Sdougm 
23906185db85Sdougm 	xmlUnlinkNode((xmlNodePtr)optionset);
23916185db85Sdougm 	xmlFreeNode((xmlNodePtr)optionset);
23926185db85Sdougm 	return (ret);
23936185db85Sdougm }
23946185db85Sdougm 
23956185db85Sdougm /*
23966185db85Sdougm  * sa_create_security(group, sectype, proto)
23976185db85Sdougm  *
23986185db85Sdougm  * Create a security optionset (one that has a type name and a
23996185db85Sdougm  * proto). Security is left over from a pure NFS implementation. The
24006185db85Sdougm  * naming will change in the future when the API is released.
24016185db85Sdougm  */
24026185db85Sdougm sa_security_t
24036185db85Sdougm sa_create_security(sa_group_t group, char *sectype, char *proto)
24046185db85Sdougm {
24056185db85Sdougm 	sa_security_t security;
24066185db85Sdougm 	char *id = NULL;
24076185db85Sdougm 	sa_group_t parent;
24086185db85Sdougm 	char *groupname = NULL;
24096185db85Sdougm 
24106185db85Sdougm 	if (group != NULL && sa_is_share(group)) {
24116185db85Sdougm 	    id = sa_get_share_attr((sa_share_t)group, "id");
24126185db85Sdougm 	    parent = sa_get_parent_group(group);
24136185db85Sdougm 	    if (parent != NULL)
24146185db85Sdougm 		groupname = sa_get_group_attr(parent, "name");
24156185db85Sdougm 	} else if (group != NULL) {
24166185db85Sdougm 	    groupname = sa_get_group_attr(group, "name");
24176185db85Sdougm 	}
24186185db85Sdougm 
24196185db85Sdougm 	security = sa_get_security(group, sectype, proto);
24206185db85Sdougm 	if (security != NULL) {
24216185db85Sdougm 		/* can't have a duplicate security option */
24226185db85Sdougm 		security = NULL;
24236185db85Sdougm 	} else {
24246185db85Sdougm 		security = (sa_security_t)xmlNewChild((xmlNodePtr)group,
24256185db85Sdougm 							NULL,
24266185db85Sdougm 							(xmlChar *)"security",
24276185db85Sdougm 							NULL);
24286185db85Sdougm 		if (security != NULL) {
24296185db85Sdougm 			char oname[256];
24306185db85Sdougm 			sa_set_security_attr(security, "type", proto);
24316185db85Sdougm 
24326185db85Sdougm 			sa_set_security_attr(security, "sectype", sectype);
24336185db85Sdougm 			(void) sa_security_name(security, oname,
24346185db85Sdougm 						sizeof (oname), id);
24356185db85Sdougm 			if (groupname != NULL && is_persistent(group)) {
2436*549ec3ffSdougm 			    sa_handle_impl_t impl_handle;
2437*549ec3ffSdougm 			    impl_handle =
2438*549ec3ffSdougm 				(sa_handle_impl_t)sa_find_group_handle(group);
2439*549ec3ffSdougm 			    if (impl_handle != NULL) {
2440*549ec3ffSdougm 				(void) sa_get_instance(impl_handle->scfhandle,
2441*549ec3ffSdougm 							groupname);
2442*549ec3ffSdougm 				(void) sa_create_pgroup(impl_handle->scfhandle,
2443*549ec3ffSdougm 							oname);
2444*549ec3ffSdougm 			    }
24456185db85Sdougm 			}
24466185db85Sdougm 		}
24476185db85Sdougm 	}
24486185db85Sdougm 	if (groupname != NULL)
24496185db85Sdougm 	    sa_free_attr_string(groupname);
24506185db85Sdougm 	return (security);
24516185db85Sdougm }
24526185db85Sdougm 
24536185db85Sdougm /*
24546185db85Sdougm  * sa_destroy_security(security)
24556185db85Sdougm  *
24566185db85Sdougm  * Remove the specified optionset from the document and the
24576185db85Sdougm  * configuration.
24586185db85Sdougm  */
24596185db85Sdougm 
24606185db85Sdougm int
24616185db85Sdougm sa_destroy_security(sa_security_t security)
24626185db85Sdougm {
24636185db85Sdougm 	char name[256];
24646185db85Sdougm 	int len;
24656185db85Sdougm 	int ret = SA_OK;
24666185db85Sdougm 	char *id = NULL;
24676185db85Sdougm 	sa_group_t group;
24686185db85Sdougm 	int iszfs = 0;
24696185db85Sdougm 	int ispersist = 1;
24706185db85Sdougm 
24716185db85Sdougm 	group = sa_get_optionset_parent(security);
24726185db85Sdougm 
24736185db85Sdougm 	if (group != NULL)
24746185db85Sdougm 	    iszfs = sa_group_is_zfs(group);
24756185db85Sdougm 
24766185db85Sdougm 	if (group != NULL && !iszfs) {
24776185db85Sdougm 	    if (sa_is_share(group))
24786185db85Sdougm 		ispersist = is_persistent(group);
24796185db85Sdougm 	    id = sa_get_share_attr((sa_share_t)group, "id");
24806185db85Sdougm 	}
24816185db85Sdougm 	if (ispersist) {
24826185db85Sdougm 	    len = sa_security_name(security, name, sizeof (name), id);
24836185db85Sdougm 	    if (!iszfs && len > 0) {
2484*549ec3ffSdougm 		sa_handle_impl_t impl_handle;
2485*549ec3ffSdougm 		impl_handle = (sa_handle_impl_t)sa_find_group_handle(group);
2486*549ec3ffSdougm 		if (impl_handle != NULL) {
2487*549ec3ffSdougm 		    ret = sa_delete_pgroup(impl_handle->scfhandle, name);
2488*549ec3ffSdougm 		} else {
2489*549ec3ffSdougm 		    ret = SA_SYSTEM_ERR;
2490*549ec3ffSdougm 		}
24916185db85Sdougm 	    }
24926185db85Sdougm 	}
24936185db85Sdougm 	xmlUnlinkNode((xmlNodePtr)security);
24946185db85Sdougm 	xmlFreeNode((xmlNodePtr)security);
24956185db85Sdougm 	if (iszfs) {
24966185db85Sdougm 	    ret = sa_zfs_update(group);
24976185db85Sdougm 	}
24986185db85Sdougm 	if (id != NULL)
24996185db85Sdougm 	    sa_free_attr_string(id);
25006185db85Sdougm 	return (ret);
25016185db85Sdougm }
25026185db85Sdougm 
25036185db85Sdougm /*
25046185db85Sdougm  * sa_get_security_attr(optionset, tag)
25056185db85Sdougm  *
25066185db85Sdougm  * Return the specified attribute value from the optionset.
25076185db85Sdougm  */
25086185db85Sdougm 
25096185db85Sdougm char *
25106185db85Sdougm sa_get_security_attr(sa_property_t optionset, char *tag)
25116185db85Sdougm {
25126185db85Sdougm 	return (get_node_attr((void *)optionset, tag));
25136185db85Sdougm 
25146185db85Sdougm }
25156185db85Sdougm 
25166185db85Sdougm /*
25176185db85Sdougm  * sa_set_security_attr(optionset, tag, value)
25186185db85Sdougm  *
25196185db85Sdougm  * Set the optioset attribute specied by tag to the specified value.
25206185db85Sdougm  */
25216185db85Sdougm 
25226185db85Sdougm void
25236185db85Sdougm sa_set_security_attr(sa_group_t optionset, char *tag, char *value)
25246185db85Sdougm {
25256185db85Sdougm 	set_node_attr((void *)optionset, tag, value);
25266185db85Sdougm }
25276185db85Sdougm 
25286185db85Sdougm /*
25296185db85Sdougm  * is_nodetype(node, type)
25306185db85Sdougm  *
25316185db85Sdougm  * Check to see if node is of the type specified.
25326185db85Sdougm  */
25336185db85Sdougm 
25346185db85Sdougm static int
25356185db85Sdougm is_nodetype(void *node, char *type)
25366185db85Sdougm {
25376185db85Sdougm 	return (strcmp((char *)((xmlNodePtr)node)->name, type) == 0);
25386185db85Sdougm }
25396185db85Sdougm 
25406185db85Sdougm /*
25416185db85Sdougm  * sa_set_prop_by_prop(optionset, group, prop, type)
25426185db85Sdougm  *
25436185db85Sdougm  * Add/remove/update the specified property prop into the optionset or
25446185db85Sdougm  * share. If a share, sort out which property group based on GUID. In
25456185db85Sdougm  * all cases, the appropriate transaction is set (or ZFS share is
25466185db85Sdougm  * marked as needing an update)
25476185db85Sdougm  */
25486185db85Sdougm 
25496185db85Sdougm #define	SA_PROP_OP_REMOVE	1
25506185db85Sdougm #define	SA_PROP_OP_ADD		2
25516185db85Sdougm #define	SA_PROP_OP_UPDATE	3
25526185db85Sdougm static int
25536185db85Sdougm sa_set_prop_by_prop(sa_optionset_t optionset, sa_group_t group,
25546185db85Sdougm 			sa_property_t prop, int type)
25556185db85Sdougm {
25566185db85Sdougm 	char *name;
25576185db85Sdougm 	char *valstr;
25586185db85Sdougm 	int ret = SA_OK;
25596185db85Sdougm 	scf_transaction_entry_t *entry;
25606185db85Sdougm 	scf_value_t *value;
25616185db85Sdougm 	int opttype; /* 1 == optionset, 0 == security */
25626185db85Sdougm 	char *id = NULL;
25636185db85Sdougm 	int iszfs = 0;
25646185db85Sdougm 	int isshare = 0;
25656185db85Sdougm 	sa_group_t parent = NULL;
2566*549ec3ffSdougm 	sa_handle_impl_t impl_handle;
2567*549ec3ffSdougm 	scfutilhandle_t  *scf_handle;
25686185db85Sdougm 
25696185db85Sdougm 	if (!is_persistent(group)) {
25706185db85Sdougm 		/*
25716185db85Sdougm 		 * if the group/share is not persistent we don't need
25726185db85Sdougm 		 * to do anything here
25736185db85Sdougm 		 */
25746185db85Sdougm 	    return (SA_OK);
25756185db85Sdougm 	}
2576*549ec3ffSdougm 	impl_handle = (sa_handle_impl_t)sa_find_group_handle(group);
2577*549ec3ffSdougm 	if (impl_handle == NULL || impl_handle->scfhandle == NULL) {
2578*549ec3ffSdougm 	    return (SA_SYSTEM_ERR);
2579*549ec3ffSdougm 	}
2580*549ec3ffSdougm 	scf_handle = impl_handle->scfhandle;
25816185db85Sdougm 	name = sa_get_property_attr(prop, "type");
25826185db85Sdougm 	valstr = sa_get_property_attr(prop, "value");
25836185db85Sdougm 	entry = scf_entry_create(scf_handle->handle);
25846185db85Sdougm 	opttype = is_nodetype((void *)optionset, "optionset");
25856185db85Sdougm 
25866185db85Sdougm 	if (valstr != NULL && entry != NULL) {
25876185db85Sdougm 	    if (sa_is_share(group)) {
25886185db85Sdougm 		isshare = 1;
25896185db85Sdougm 		parent = sa_get_parent_group(group);
25906185db85Sdougm 		if (parent != NULL) {
25916185db85Sdougm 		    iszfs = is_zfs_group(parent);
25926185db85Sdougm 		}
25936185db85Sdougm 	    } else {
25946185db85Sdougm 		iszfs = is_zfs_group(group);
25956185db85Sdougm 	    }
25966185db85Sdougm 	    if (!iszfs) {
25976185db85Sdougm 		    if (scf_handle->trans == NULL) {
25986185db85Sdougm 			char oname[256];
25996185db85Sdougm 			char *groupname = NULL;
26006185db85Sdougm 			if (isshare) {
26016185db85Sdougm 			    if (parent != NULL) {
26026185db85Sdougm 				groupname = sa_get_group_attr(parent, "name");
26036185db85Sdougm 			    }
26046185db85Sdougm 			    id = sa_get_share_attr((sa_share_t)group, "id");
26056185db85Sdougm 			} else {
26066185db85Sdougm 			    groupname = sa_get_group_attr(group, "name");
26076185db85Sdougm 			}
26086185db85Sdougm 			if (groupname != NULL) {
26096185db85Sdougm 			    ret = sa_get_instance(scf_handle, groupname);
26106185db85Sdougm 			    sa_free_attr_string(groupname);
26116185db85Sdougm 			}
26126185db85Sdougm 			if (opttype)
26136185db85Sdougm 			    (void) sa_optionset_name(optionset, oname,
26146185db85Sdougm 							sizeof (oname), id);
26156185db85Sdougm 			else
26166185db85Sdougm 			    (void) sa_security_name(optionset, oname,
26176185db85Sdougm 							sizeof (oname), id);
26186185db85Sdougm 			ret = sa_start_transaction(scf_handle, oname);
26196185db85Sdougm 		    }
26206185db85Sdougm 		    if (ret == SA_OK) {
26216185db85Sdougm 			switch (type) {
26226185db85Sdougm 			case SA_PROP_OP_REMOVE:
2623*549ec3ffSdougm 			    ret = scf_transaction_property_delete(
2624*549ec3ffSdougm 							scf_handle->trans,
2625*549ec3ffSdougm 							entry, name);
26266185db85Sdougm 			    break;
26276185db85Sdougm 			case SA_PROP_OP_ADD:
26286185db85Sdougm 			case SA_PROP_OP_UPDATE:
26296185db85Sdougm 			    value = scf_value_create(scf_handle->handle);
26306185db85Sdougm 			    if (value != NULL) {
26316185db85Sdougm 				if (type == SA_PROP_OP_ADD)
26326185db85Sdougm 				    ret = scf_transaction_property_new(
26336185db85Sdougm 							scf_handle->trans,
26346185db85Sdougm 							entry,
26356185db85Sdougm 							name,
26366185db85Sdougm 							SCF_TYPE_ASTRING);
26376185db85Sdougm 				else
26386185db85Sdougm 				    ret = scf_transaction_property_change(
26396185db85Sdougm 							scf_handle->trans,
26406185db85Sdougm 							entry,
26416185db85Sdougm 							name,
26426185db85Sdougm 							SCF_TYPE_ASTRING);
26436185db85Sdougm 				if (ret == 0) {
26446185db85Sdougm 				    ret = scf_value_set_astring(value, valstr);
26456185db85Sdougm 				    if (ret == 0)
26466185db85Sdougm 					ret = scf_entry_add_value(entry, value);
26476185db85Sdougm 				    if (ret != 0) {
26486185db85Sdougm 					scf_value_destroy(value);
26496185db85Sdougm 					ret = SA_SYSTEM_ERR;
26506185db85Sdougm 				    }
26516185db85Sdougm 				} else {
26526185db85Sdougm 				    scf_entry_destroy(entry);
26536185db85Sdougm 				    ret = SA_SYSTEM_ERR;
26546185db85Sdougm 				}
26556185db85Sdougm 				break;
26566185db85Sdougm 			    }
26576185db85Sdougm 			}
26586185db85Sdougm 		    }
26596185db85Sdougm 	    } else {
26606185db85Sdougm 		/*
26616185db85Sdougm 		 * ZFS update. The calling function would have updated
26626185db85Sdougm 		 * the internal XML structure. Just need to flag it as
26636185db85Sdougm 		 * changed for ZFS.
26646185db85Sdougm 		 */
26656185db85Sdougm 		zfs_set_update((sa_share_t)group);
26666185db85Sdougm 	    }
26676185db85Sdougm 	}
26686185db85Sdougm 
26696185db85Sdougm 	if (name != NULL)
26706185db85Sdougm 	    sa_free_attr_string(name);
26716185db85Sdougm 	if (valstr != NULL)
26726185db85Sdougm 	    sa_free_attr_string(valstr);
26736185db85Sdougm 	else if (entry != NULL)
26746185db85Sdougm 	    scf_entry_destroy(entry);
26756185db85Sdougm 
26766185db85Sdougm 	if (ret == -1)
26776185db85Sdougm 	    ret = SA_SYSTEM_ERR;
26786185db85Sdougm 
26796185db85Sdougm 	return (ret);
26806185db85Sdougm }
26816185db85Sdougm 
26826185db85Sdougm /*
26836185db85Sdougm  * sa_create_property(name, value)
26846185db85Sdougm  *
26856185db85Sdougm  * Create a new property with the specified name and value.
26866185db85Sdougm  */
26876185db85Sdougm 
26886185db85Sdougm sa_property_t
26896185db85Sdougm sa_create_property(char *name, char *value)
26906185db85Sdougm {
26916185db85Sdougm 	xmlNodePtr node;
26926185db85Sdougm 
26936185db85Sdougm 	node = xmlNewNode(NULL, (xmlChar *)"option");
26946185db85Sdougm 	if (node != NULL) {
26956185db85Sdougm 		xmlSetProp(node, (xmlChar *)"type", (xmlChar *)name);
26966185db85Sdougm 		xmlSetProp(node, (xmlChar *)"value", (xmlChar *)value);
26976185db85Sdougm 	}
26986185db85Sdougm 	return ((sa_property_t)node);
26996185db85Sdougm }
27006185db85Sdougm 
27016185db85Sdougm /*
27026185db85Sdougm  * sa_add_property(object, property)
27036185db85Sdougm  *
27046185db85Sdougm  * Add the specified property to the object. Issue the appropriate
27056185db85Sdougm  * transaction or mark a ZFS object as needing an update.
27066185db85Sdougm  */
27076185db85Sdougm 
27086185db85Sdougm int
27096185db85Sdougm sa_add_property(void *object, sa_property_t property)
27106185db85Sdougm {
27116185db85Sdougm 	int ret = SA_OK;
27126185db85Sdougm 	sa_group_t parent;
27136185db85Sdougm 	sa_group_t group;
27146185db85Sdougm 	char *proto;
27156185db85Sdougm 
27166185db85Sdougm 	proto = sa_get_optionset_attr(object, "type");
27176185db85Sdougm 	if (property != NULL) {
27186185db85Sdougm 	    if ((ret = sa_valid_property(object, proto, property)) == SA_OK) {
27196185db85Sdougm 		property = (sa_property_t)xmlAddChild((xmlNodePtr)object,
27206185db85Sdougm 							(xmlNodePtr)property);
27216185db85Sdougm 	    } else {
27226185db85Sdougm 		if (proto != NULL)
27236185db85Sdougm 		    sa_free_attr_string(proto);
27246185db85Sdougm 		return (ret);
27256185db85Sdougm 	    }
27266185db85Sdougm 	}
27276185db85Sdougm 
27286185db85Sdougm 	if (proto != NULL)
27296185db85Sdougm 	    sa_free_attr_string(proto);
27306185db85Sdougm 
27316185db85Sdougm 	parent = sa_get_parent_group(object);
27326185db85Sdougm 	if (!is_persistent(parent)) {
27336185db85Sdougm 	    return (ret);
27346185db85Sdougm 	}
27356185db85Sdougm 
27366185db85Sdougm 	if (sa_is_share(parent))
27376185db85Sdougm 	    group = sa_get_parent_group(parent);
27386185db85Sdougm 	else
27396185db85Sdougm 	    group = parent;
27406185db85Sdougm 
27416185db85Sdougm 	if (property == NULL)
27426185db85Sdougm 	    ret = SA_NO_MEMORY;
27436185db85Sdougm 	else {
27446185db85Sdougm 	    char oname[256];
27456185db85Sdougm 
27466185db85Sdougm 	    if (!is_zfs_group(group)) {
27476185db85Sdougm 		char *id = NULL;
2748*549ec3ffSdougm 		sa_handle_impl_t impl_handle;
2749*549ec3ffSdougm 		scfutilhandle_t  *scf_handle;
2750*549ec3ffSdougm 
2751*549ec3ffSdougm 		impl_handle = (sa_handle_impl_t)sa_find_group_handle(group);
2752*549ec3ffSdougm 		if (impl_handle == NULL || impl_handle->scfhandle == NULL)
2753*549ec3ffSdougm 		    ret = SA_SYSTEM_ERR;
2754*549ec3ffSdougm 		if (ret == SA_OK) {
2755*549ec3ffSdougm 		    scf_handle = impl_handle->scfhandle;
27566185db85Sdougm 		    if (sa_is_share((sa_group_t)parent)) {
27576185db85Sdougm 			id = sa_get_share_attr((sa_share_t)parent, "id");
27586185db85Sdougm 		    }
27596185db85Sdougm 		    if (scf_handle->trans == NULL) {
27606185db85Sdougm 			if (is_nodetype(object, "optionset"))
27616185db85Sdougm 			    (void) sa_optionset_name((sa_optionset_t)object,
27626185db85Sdougm 					    oname, sizeof (oname), id);
27636185db85Sdougm 			else
27646185db85Sdougm 			    (void) sa_security_name((sa_optionset_t)object,
27656185db85Sdougm 					    oname, sizeof (oname), id);
27666185db85Sdougm 			ret = sa_start_transaction(scf_handle, oname);
27676185db85Sdougm 		    }
27686185db85Sdougm 		    if (ret == SA_OK) {
27696185db85Sdougm 			char *name;
27706185db85Sdougm 			char *value;
27716185db85Sdougm 			name = sa_get_property_attr(property, "type");
27726185db85Sdougm 			value = sa_get_property_attr(property, "value");
27736185db85Sdougm 			if (name != NULL && value != NULL) {
27746185db85Sdougm 			    if (scf_handle->scf_state == SCH_STATE_INIT)
27756185db85Sdougm 				ret = sa_set_property(scf_handle, name, value);
27766185db85Sdougm 			} else
27776185db85Sdougm 			    ret = SA_CONFIG_ERR;
27786185db85Sdougm 			if (name != NULL)
27796185db85Sdougm 			    sa_free_attr_string(name);
27806185db85Sdougm 			if (value != NULL)
27816185db85Sdougm 			    sa_free_attr_string(value);
27826185db85Sdougm 		    }
27836185db85Sdougm 		    if (id != NULL)
27846185db85Sdougm 			sa_free_attr_string(id);
2785*549ec3ffSdougm 		}
27866185db85Sdougm 	    } else {
27876185db85Sdougm 		/*
27886185db85Sdougm 		 * ZFS is a special case. We do want to allow editing
27896185db85Sdougm 		 * property/security lists since we can have a better
27906185db85Sdougm 		 * syntax and we also want to keep things consistent
27916185db85Sdougm 		 * when possible.
27926185db85Sdougm 		 *
27936185db85Sdougm 		 * Right now, we defer until the sa_commit_properties
27946185db85Sdougm 		 * so we can get them all at once. We do need to mark
27956185db85Sdougm 		 * the share as "changed"
27966185db85Sdougm 		 */
27976185db85Sdougm 		zfs_set_update((sa_share_t)parent);
27986185db85Sdougm 	    }
27996185db85Sdougm 	}
28006185db85Sdougm 	return (ret);
28016185db85Sdougm }
28026185db85Sdougm 
28036185db85Sdougm /*
28046185db85Sdougm  * sa_remove_property(property)
28056185db85Sdougm  *
28066185db85Sdougm  * Remove the specied property from its containing object. Update the
28076185db85Sdougm  * repository as appropriate.
28086185db85Sdougm  */
28096185db85Sdougm 
28106185db85Sdougm int
28116185db85Sdougm sa_remove_property(sa_property_t property)
28126185db85Sdougm {
28136185db85Sdougm 	int ret = SA_OK;
28146185db85Sdougm 
28156185db85Sdougm 	if (property != NULL) {
28166185db85Sdougm 		sa_optionset_t optionset;
28176185db85Sdougm 		sa_group_t group;
28186185db85Sdougm 		optionset = sa_get_property_parent(property);
28196185db85Sdougm 		if (optionset != NULL) {
28206185db85Sdougm 		    group = sa_get_optionset_parent(optionset);
28216185db85Sdougm 		    if (group != NULL) {
28226185db85Sdougm 			ret = sa_set_prop_by_prop(optionset, group, property,
28236185db85Sdougm 					    SA_PROP_OP_REMOVE);
28246185db85Sdougm 		    }
28256185db85Sdougm 		}
28266185db85Sdougm 		xmlUnlinkNode((xmlNodePtr)property);
28276185db85Sdougm 		xmlFreeNode((xmlNodePtr)property);
28286185db85Sdougm 	} else {
28296185db85Sdougm 	    ret = SA_NO_SUCH_PROP;
28306185db85Sdougm 	}
28316185db85Sdougm 	return (ret);
28326185db85Sdougm }
28336185db85Sdougm 
28346185db85Sdougm /*
28356185db85Sdougm  * sa_update_property(property, value)
28366185db85Sdougm  *
28376185db85Sdougm  * Update the specified property to the new value.  If value is NULL,
28386185db85Sdougm  * we currently treat this as a remove.
28396185db85Sdougm  */
28406185db85Sdougm 
28416185db85Sdougm int
28426185db85Sdougm sa_update_property(sa_property_t property, char *value)
28436185db85Sdougm {
28446185db85Sdougm 	int ret = SA_OK;
28456185db85Sdougm 	if (value == NULL) {
28466185db85Sdougm 		return (sa_remove_property(property));
28476185db85Sdougm 	} else {
28486185db85Sdougm 		sa_optionset_t optionset;
28496185db85Sdougm 		sa_group_t group;
28506185db85Sdougm 		set_node_attr((void *)property, "value", value);
28516185db85Sdougm 		optionset = sa_get_property_parent(property);
28526185db85Sdougm 		if (optionset != NULL) {
28536185db85Sdougm 		    group = sa_get_optionset_parent(optionset);
28546185db85Sdougm 		    if (group != NULL) {
28556185db85Sdougm 			ret = sa_set_prop_by_prop(optionset, group, property,
28566185db85Sdougm 					    SA_PROP_OP_UPDATE);
28576185db85Sdougm 		    }
28586185db85Sdougm 		} else {
28596185db85Sdougm 		    ret = SA_NO_SUCH_PROP;
28606185db85Sdougm 		}
28616185db85Sdougm 	}
28626185db85Sdougm 	return (ret);
28636185db85Sdougm }
28646185db85Sdougm 
28656185db85Sdougm /*
28666185db85Sdougm  * sa_get_protocol_property(propset, prop)
28676185db85Sdougm  *
28686185db85Sdougm  * Get the specified protocol specific property. These are global to
28696185db85Sdougm  * the protocol and not specific to a group or share.
28706185db85Sdougm  */
28716185db85Sdougm 
28726185db85Sdougm sa_property_t
28736185db85Sdougm sa_get_protocol_property(sa_protocol_properties_t propset, char *prop)
28746185db85Sdougm {
28756185db85Sdougm 	xmlNodePtr node = (xmlNodePtr)propset;
28766185db85Sdougm 	xmlChar *value = NULL;
28776185db85Sdougm 
28786185db85Sdougm 	for (node = node->children; node != NULL;
28796185db85Sdougm 		node = node->next) {
28806185db85Sdougm 	    if (xmlStrcmp(node->name, (xmlChar *)"option") == 0) {
28816185db85Sdougm 		if (prop == NULL)
28826185db85Sdougm 		    break;
28836185db85Sdougm 		value = xmlGetProp(node, (xmlChar *)"type");
28846185db85Sdougm 		if (value != NULL &&
28856185db85Sdougm 		    xmlStrcasecmp(value, (xmlChar *)prop) == 0) {
28866185db85Sdougm 		    break;
28876185db85Sdougm 		}
28886185db85Sdougm 		if (value != NULL) {
28896185db85Sdougm 		    xmlFree(value);
28906185db85Sdougm 		    value = NULL;
28916185db85Sdougm 		}
28926185db85Sdougm 	    }
28936185db85Sdougm 	}
28946185db85Sdougm 	if (value != NULL)
28956185db85Sdougm 		xmlFree(value);
28966185db85Sdougm 	if (node != NULL && xmlStrcmp(node->name, (xmlChar *)"option") != 0) {
28976185db85Sdougm 	    /* avoid a non option node -- it is possible to be a text node */
28986185db85Sdougm 	    node = NULL;
28996185db85Sdougm 	}
29006185db85Sdougm 	return ((sa_property_t)node);
29016185db85Sdougm }
29026185db85Sdougm 
29036185db85Sdougm /*
29046185db85Sdougm  * sa_get_next_protocol_property(prop)
29056185db85Sdougm  *
29066185db85Sdougm  * Get the next protocol specific property in the list.
29076185db85Sdougm  */
29086185db85Sdougm 
29096185db85Sdougm sa_property_t
29106185db85Sdougm sa_get_next_protocol_property(sa_property_t prop)
29116185db85Sdougm {
29126185db85Sdougm 	xmlNodePtr node;
29136185db85Sdougm 
29146185db85Sdougm 	for (node = ((xmlNodePtr)prop)->next; node != NULL;
29156185db85Sdougm 		node = node->next) {
29166185db85Sdougm 		if (xmlStrcmp(node->name, (xmlChar *)"option") == 0) {
29176185db85Sdougm 			break;
29186185db85Sdougm 		}
29196185db85Sdougm 	}
29206185db85Sdougm 	return ((sa_property_t)node);
29216185db85Sdougm }
29226185db85Sdougm 
29236185db85Sdougm /*
29246185db85Sdougm  * sa_set_protocol_property(prop, value)
29256185db85Sdougm  *
29266185db85Sdougm  * Set the specified property to have the new value.  The protocol
29276185db85Sdougm  * specific plugin will then be called to update the property.
29286185db85Sdougm  */
29296185db85Sdougm 
29306185db85Sdougm int
29316185db85Sdougm sa_set_protocol_property(sa_property_t prop, char *value)
29326185db85Sdougm {
29336185db85Sdougm 	sa_protocol_properties_t propset;
29346185db85Sdougm 	char *proto;
29356185db85Sdougm 	int ret = SA_INVALID_PROTOCOL;
29366185db85Sdougm 
29376185db85Sdougm 	propset = ((xmlNodePtr)prop)->parent;
29386185db85Sdougm 	if (propset != NULL) {
29396185db85Sdougm 	    proto = sa_get_optionset_attr(propset, "type");
29406185db85Sdougm 	    if (proto != NULL) {
29416185db85Sdougm 		set_node_attr((xmlNodePtr)prop, "value", value);
29426185db85Sdougm 		ret = sa_proto_set_property(proto, prop);
29433472f5dcSdougm 		sa_free_attr_string(proto);
29446185db85Sdougm 	    }
29456185db85Sdougm 	}
29466185db85Sdougm 	return (ret);
29476185db85Sdougm }
29486185db85Sdougm 
29496185db85Sdougm /*
29506185db85Sdougm  * sa_add_protocol_property(propset, prop)
29516185db85Sdougm  *
29526185db85Sdougm  * Add a new property to the protocol sepcific property set.
29536185db85Sdougm  */
29546185db85Sdougm 
29556185db85Sdougm int
29566185db85Sdougm sa_add_protocol_property(sa_protocol_properties_t propset, sa_property_t prop)
29576185db85Sdougm {
29586185db85Sdougm 	xmlNodePtr node;
29596185db85Sdougm 
29606185db85Sdougm 	/* should check for legitimacy */
29616185db85Sdougm 	node = xmlAddChild((xmlNodePtr)propset, (xmlNodePtr)prop);
29626185db85Sdougm 	if (node != NULL)
29636185db85Sdougm 	    return (SA_OK);
29646185db85Sdougm 	return (SA_NO_MEMORY);
29656185db85Sdougm }
29666185db85Sdougm 
29676185db85Sdougm /*
29686185db85Sdougm  * sa_create_protocol_properties(proto)
29696185db85Sdougm  *
29706185db85Sdougm  * Create a protocol specifity property set.
29716185db85Sdougm  */
29726185db85Sdougm 
29736185db85Sdougm sa_protocol_properties_t
29746185db85Sdougm sa_create_protocol_properties(char *proto)
29756185db85Sdougm {
29766185db85Sdougm 	xmlNodePtr node;
29776185db85Sdougm 	node = xmlNewNode(NULL, (xmlChar *)"propertyset");
29786185db85Sdougm 	if (node != NULL) {
29796185db85Sdougm 	    xmlSetProp(node, (xmlChar *)"type", (xmlChar *)proto);
29806185db85Sdougm 	}
29816185db85Sdougm 	return (node);
29826185db85Sdougm }
2983