13034Sdougm /*
23034Sdougm  * CDDL HEADER START
33034Sdougm  *
43034Sdougm  * The contents of this file are subject to the terms of the
53034Sdougm  * Common Development and Distribution License (the "License").
63034Sdougm  * You may not use this file except in compliance with the License.
73034Sdougm  *
83034Sdougm  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
93034Sdougm  * or http://www.opensolaris.org/os/licensing.
103034Sdougm  * See the License for the specific language governing permissions
113034Sdougm  * and limitations under the License.
123034Sdougm  *
133034Sdougm  * When distributing Covered Code, include this CDDL HEADER in each
143034Sdougm  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
153034Sdougm  * If applicable, add the following below this CDDL HEADER, with the
163034Sdougm  * fields enclosed by brackets "[]" replaced with your own identifying
173034Sdougm  * information: Portions Copyright [yyyy] [name of copyright owner]
183034Sdougm  *
193034Sdougm  * CDDL HEADER END
203034Sdougm  */
213034Sdougm 
223034Sdougm /*
233348Sdougm  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
243034Sdougm  * Use is subject to license terms.
253034Sdougm  */
263034Sdougm 
273034Sdougm #pragma ident	"%Z%%M%	%I%	%E% SMI"
283034Sdougm 
293034Sdougm /* helper functions for using libscf with sharemgr */
303034Sdougm 
313034Sdougm #include <libscf.h>
323034Sdougm #include <libxml/parser.h>
333034Sdougm #include <libxml/tree.h>
343034Sdougm #include "libshare.h"
353034Sdougm #include "libshare_impl.h"
363034Sdougm #include "scfutil.h"
373034Sdougm #include <string.h>
385331Samw #include <ctype.h>
393034Sdougm #include <errno.h>
403034Sdougm #include <uuid/uuid.h>
413034Sdougm #include <sys/param.h>
423348Sdougm #include <signal.h>
433034Sdougm 
443034Sdougm ssize_t scf_max_name_len;
453034Sdougm extern struct sa_proto_plugin *sap_proto_list;
463910Sdougm extern sa_handle_impl_t get_handle_for_root(xmlNodePtr);
473034Sdougm 
483034Sdougm /*
493034Sdougm  * The SMF facility uses some properties that must exist. We want to
503034Sdougm  * skip over these when processing protocol options.
513034Sdougm  */
523034Sdougm static char *skip_props[] = {
533034Sdougm 	"modify_authorization",
543034Sdougm 	"action_authorization",
553034Sdougm 	"value_authorization",
563034Sdougm 	NULL
573034Sdougm };
583034Sdougm 
593034Sdougm /*
603034Sdougm  * sa_scf_fini(handle)
613034Sdougm  *
624653Sdougm  * Must be called when done. Called with the handle allocated in
633034Sdougm  * sa_scf_init(), it cleans up the state and frees any SCF resources
643034Sdougm  * still in use. Called by sa_fini().
653034Sdougm  */
663034Sdougm 
673034Sdougm void
683034Sdougm sa_scf_fini(scfutilhandle_t *handle)
693034Sdougm {
703034Sdougm 	if (handle != NULL) {
714653Sdougm 		int unbind = 0;
724653Sdougm 		if (handle->scope != NULL) {
734653Sdougm 			unbind = 1;
744653Sdougm 			scf_scope_destroy(handle->scope);
754653Sdougm 		}
764653Sdougm 		if (handle->instance != NULL)
774653Sdougm 			scf_instance_destroy(handle->instance);
784653Sdougm 		if (handle->service != NULL)
794653Sdougm 			scf_service_destroy(handle->service);
804653Sdougm 		if (handle->pg != NULL)
814653Sdougm 			scf_pg_destroy(handle->pg);
824653Sdougm 		if (handle->handle != NULL) {
834653Sdougm 			handle->scf_state = SCH_STATE_UNINIT;
844653Sdougm 			if (unbind)
854653Sdougm 				(void) scf_handle_unbind(handle->handle);
864653Sdougm 			scf_handle_destroy(handle->handle);
874653Sdougm 		}
884653Sdougm 		free(handle);
893034Sdougm 	}
903034Sdougm }
913034Sdougm 
923034Sdougm /*
933034Sdougm  * sa_scf_init()
943034Sdougm  *
954653Sdougm  * Must be called before using any of the SCF functions. Called by
963034Sdougm  * sa_init() during the API setup.
973034Sdougm  */
983034Sdougm 
993034Sdougm scfutilhandle_t *
1003910Sdougm sa_scf_init(sa_handle_impl_t ihandle)
1013034Sdougm {
1023034Sdougm 	scfutilhandle_t *handle;
1033034Sdougm 
1043034Sdougm 	scf_max_name_len = scf_limit(SCF_LIMIT_MAX_NAME_LENGTH);
1053034Sdougm 	if (scf_max_name_len <= 0)
1064653Sdougm 		scf_max_name_len = SA_MAX_NAME_LEN + 1;
1073034Sdougm 
1083034Sdougm 	handle = calloc(1, sizeof (scfutilhandle_t));
1094653Sdougm 	if (handle == NULL)
1104653Sdougm 		return (handle);
1114345Sdougm 
1124653Sdougm 	ihandle->scfhandle = handle;
1134653Sdougm 	handle->scf_state = SCH_STATE_INITIALIZING;
1144653Sdougm 	handle->handle = scf_handle_create(SCF_VERSION);
1154653Sdougm 	if (handle->handle == NULL) {
1163034Sdougm 		free(handle);
1173034Sdougm 		handle = NULL;
1183034Sdougm 		(void) printf("libshare could not access SMF repository: %s\n",
1194653Sdougm 		    scf_strerror(scf_error()));
1204653Sdougm 		return (handle);
1213034Sdougm 	}
1224653Sdougm 	if (scf_handle_bind(handle->handle) != 0)
1234653Sdougm 		goto err;
1244653Sdougm 
1254653Sdougm 	handle->scope = scf_scope_create(handle->handle);
1264653Sdougm 	handle->service = scf_service_create(handle->handle);
1274653Sdougm 	handle->pg = scf_pg_create(handle->handle);
1284653Sdougm 
1294653Sdougm 	/* Make sure we have sufficient SMF running */
1304653Sdougm 	handle->instance = scf_instance_create(handle->handle);
1314653Sdougm 	if (handle->scope == NULL || handle->service == NULL ||
1324653Sdougm 	    handle->pg == NULL || handle->instance == NULL)
1334653Sdougm 		goto err;
1344653Sdougm 	if (scf_handle_get_scope(handle->handle,
1354653Sdougm 	    SCF_SCOPE_LOCAL, handle->scope) != 0)
1364653Sdougm 		goto err;
1374653Sdougm 	if (scf_scope_get_service(handle->scope,
1384653Sdougm 	    SA_GROUP_SVC_NAME, handle->service) != 0)
1394653Sdougm 		goto err;
1404653Sdougm 
1414653Sdougm 	handle->scf_state = SCH_STATE_INIT;
1424653Sdougm 	if (sa_get_instance(handle, "default") != SA_OK) {
1434653Sdougm 		char **protolist;
1444653Sdougm 		int numprotos, i;
1454653Sdougm 		sa_group_t defgrp;
1464653Sdougm 		defgrp = sa_create_group((sa_handle_t)ihandle, "default", NULL);
1474653Sdougm 		if (defgrp != NULL) {
1484653Sdougm 			numprotos = sa_get_protocols(
1494653Sdougm 			    &protolist);
1504653Sdougm 			for (i = 0; i < numprotos; i++)
1514653Sdougm 				(void) sa_create_optionset(defgrp,
1524653Sdougm 				    protolist[i]);
1534653Sdougm 			if (protolist != NULL)
1544653Sdougm 				free(protolist);
1554653Sdougm 		}
1564653Sdougm 	}
1574653Sdougm 
1583034Sdougm 	return (handle);
1593034Sdougm 
1604653Sdougm 	/* Error handling/unwinding */
1613034Sdougm err:
1623034Sdougm 	(void) sa_scf_fini(handle);
1633034Sdougm 	(void) printf("libshare SMF initialization problem: %s\n",
1644653Sdougm 	    scf_strerror(scf_error()));
1653034Sdougm 	return (NULL);
1663034Sdougm }
1673034Sdougm 
1683034Sdougm /*
1693034Sdougm  * get_scf_limit(name)
1703034Sdougm  *
1713034Sdougm  * Since we use  scf_limit a lot and do the same  check and return the
1723034Sdougm  * same  value  if  it  fails,   implement  as  a  function  for  code
1733034Sdougm  * simplification.  Basically, if  name isn't found, return MAXPATHLEN
1743034Sdougm  * (1024) so we have a reasonable default buffer size.
1753034Sdougm  */
1763034Sdougm static ssize_t
1773034Sdougm get_scf_limit(uint32_t name)
1783034Sdougm {
1793034Sdougm 	ssize_t vallen;
1803034Sdougm 
1813034Sdougm 	vallen = scf_limit(name);
1823034Sdougm 	if (vallen == (ssize_t)-1)
1834653Sdougm 		vallen = MAXPATHLEN;
1843034Sdougm 	return (vallen);
1853034Sdougm }
1863034Sdougm 
1873034Sdougm /*
1883034Sdougm  * skip_property(name)
1893034Sdougm  *
1904653Sdougm  * Internal function to check to see if a property is an SMF magic
1913034Sdougm  * property that needs to be skipped.
1923034Sdougm  */
1933034Sdougm static int
1943034Sdougm skip_property(char *name)
1953034Sdougm {
1963034Sdougm 	int i;
1973034Sdougm 
1983034Sdougm 	for (i = 0; skip_props[i] != NULL; i++)
1994653Sdougm 		if (strcmp(name, skip_props[i]) == 0)
2003034Sdougm 		return (1);
2013034Sdougm 	return (0);
2023034Sdougm }
2033034Sdougm 
2043034Sdougm /*
2053034Sdougm  * generate_unique_sharename(sharename)
2063034Sdougm  *
2073034Sdougm  * Shares are represented in SMF as property groups. Due to share
2083034Sdougm  * paths containing characters that are not allowed in SMF names and
2093034Sdougm  * the need to be unique, we use UUIDs to construct a unique name.
2103034Sdougm  */
2113034Sdougm 
2123034Sdougm static void
2133034Sdougm generate_unique_sharename(char *sharename)
2143034Sdougm {
2153034Sdougm 	uuid_t uuid;
2163034Sdougm 
2173034Sdougm 	uuid_generate(uuid);
2183034Sdougm 	(void) strcpy(sharename, "S-");
2193034Sdougm 	uuid_unparse(uuid, sharename + 2);
2203034Sdougm }
2213034Sdougm 
2223034Sdougm /*
2233034Sdougm  * valid_protocol(proto)
2243034Sdougm  *
2254653Sdougm  * Check to see if the specified protocol is a valid one for the
2263034Sdougm  * general sharemgr facility. We determine this by checking which
2273034Sdougm  * plugin protocols were found.
2283034Sdougm  */
2293034Sdougm 
2303034Sdougm static int
2313034Sdougm valid_protocol(char *proto)
2323034Sdougm {
2333034Sdougm 	struct sa_proto_plugin *plugin;
2343034Sdougm 	for (plugin = sap_proto_list; plugin != NULL;
2353034Sdougm 	    plugin = plugin->plugin_next)
2364653Sdougm 		if (strcmp(proto, plugin->plugin_ops->sa_protocol) == 0)
2374653Sdougm 			return (1);
2383034Sdougm 	return (0);
2393034Sdougm }
2403034Sdougm 
2413034Sdougm /*
2423034Sdougm  * sa_extract_pgroup(root, handle, pg, nodetype, proto, sectype)
2433034Sdougm  *
2444653Sdougm  * Extract the name property group and create the specified type of
2453034Sdougm  * node on the provided group.  type will be optionset or security.
2463034Sdougm  */
2473034Sdougm 
2483034Sdougm static int
2493034Sdougm sa_extract_pgroup(xmlNodePtr root, scfutilhandle_t *handle,
2503034Sdougm 			scf_propertygroup_t *pg,
2513034Sdougm 			char *nodetype, char *proto, char *sectype)
2523034Sdougm {
2533034Sdougm 	xmlNodePtr node;
2543034Sdougm 	scf_iter_t *iter;
2553034Sdougm 	scf_property_t *prop;
2563034Sdougm 	scf_value_t *value;
2573034Sdougm 	char *name;
2583034Sdougm 	char *valuestr;
2593034Sdougm 	ssize_t vallen;
2603034Sdougm 	int ret = SA_OK;
2613034Sdougm 
2623034Sdougm 	vallen = get_scf_limit(SCF_LIMIT_MAX_VALUE_LENGTH);
2633034Sdougm 
2643034Sdougm 	node = xmlNewChild(root, NULL, (xmlChar *)nodetype, NULL);
2654653Sdougm 	if (node == NULL)
2664653Sdougm 		return (ret);
2674653Sdougm 
2684653Sdougm 	if (proto != NULL)
2693034Sdougm 		xmlSetProp(node, (xmlChar *)"type", (xmlChar *)proto);
2704653Sdougm 	if (sectype != NULL)
2713034Sdougm 		xmlSetProp(node, (xmlChar *)"sectype", (xmlChar *)sectype);
2724653Sdougm 	/*
2734653Sdougm 	 * Have node to work with so iterate over the properties
2744653Sdougm 	 * in the pg and create option sub nodes.
2754653Sdougm 	 */
2764653Sdougm 	iter = scf_iter_create(handle->handle);
2774653Sdougm 	value = scf_value_create(handle->handle);
2784653Sdougm 	prop = scf_property_create(handle->handle);
2794653Sdougm 	name = malloc(scf_max_name_len);
2804653Sdougm 	valuestr = malloc(vallen);
2814653Sdougm 	/*
2824653Sdougm 	 * Want to iterate through the properties and add them
2834653Sdougm 	 * to the base optionset.
2844653Sdougm 	 */
2854653Sdougm 	if (iter == NULL || value == NULL || prop == NULL ||
2864653Sdougm 	    valuestr == NULL || name == NULL) {
2874653Sdougm 		ret = SA_NO_MEMORY;
2884653Sdougm 		goto out;
2894653Sdougm 	}
2904653Sdougm 	if (scf_iter_pg_properties(iter, pg) == 0) {
2914653Sdougm 		/* Now iterate the properties in the group */
2924653Sdougm 		while (scf_iter_next_property(iter, prop) > 0) {
2934653Sdougm 			/* have a property */
2944653Sdougm 			if (scf_property_get_name(prop, name,
2954653Sdougm 			    scf_max_name_len) > 0) {
2964653Sdougm 				sa_property_t saprop;
2974653Sdougm 				/* Some properties are part of the framework */
2983034Sdougm 				if (skip_property(name))
2994653Sdougm 					continue;
3004653Sdougm 				if (scf_property_get_value(prop, value) != 0)
3014653Sdougm 					continue;
3024653Sdougm 				if (scf_value_get_astring(value, valuestr,
3034653Sdougm 				    vallen) < 0)
3044653Sdougm 					continue;
3054653Sdougm 				saprop = sa_create_property(name, valuestr);
3064653Sdougm 				if (saprop != NULL) {
3073034Sdougm 					/*
3084653Sdougm 					 * Since in SMF, don't
3093034Sdougm 					 * recurse. Use xmlAddChild
3103034Sdougm 					 * directly, instead.
3113034Sdougm 					 */
3124653Sdougm 					xmlAddChild(node,
3134653Sdougm 					    (xmlNodePtr) saprop);
3143034Sdougm 				}
3153034Sdougm 			}
3163034Sdougm 		}
3173034Sdougm 	}
3184653Sdougm out:
3194653Sdougm 	/* cleanup to avoid memory leaks */
3204653Sdougm 	if (value != NULL)
3214653Sdougm 		scf_value_destroy(value);
3224653Sdougm 	if (iter != NULL)
3234653Sdougm 		scf_iter_destroy(iter);
3244653Sdougm 	if (prop != NULL)
3254653Sdougm 		scf_property_destroy(prop);
3264653Sdougm 	if (name != NULL)
3274653Sdougm 		free(name);
3284653Sdougm 	if (valuestr != NULL)
3294653Sdougm 		free(valuestr);
3304653Sdougm 
3313034Sdougm 	return (ret);
3323034Sdougm }
3333034Sdougm 
3343034Sdougm /*
3353034Sdougm  * sa_extract_attrs(root, handle, instance)
3363034Sdougm  *
3374653Sdougm  * Local function to extract the actual attributes/properties from the
3383034Sdougm  * property group of the service instance. These are the well known
3393034Sdougm  * attributes of "state" and "zfs". If additional attributes are
3403034Sdougm  * added, they should be added here.
3413034Sdougm  */
3423034Sdougm 
3433034Sdougm static void
3443034Sdougm sa_extract_attrs(xmlNodePtr root, scfutilhandle_t *handle,
3453034Sdougm 		    scf_instance_t *instance)
3463034Sdougm {
3473034Sdougm 	scf_property_t *prop;
3483034Sdougm 	scf_value_t *value;
3493034Sdougm 	char *valuestr;
3503034Sdougm 	ssize_t vallen;
3513034Sdougm 
3523034Sdougm 	vallen = get_scf_limit(SCF_LIMIT_MAX_VALUE_LENGTH);
3533034Sdougm 	prop = scf_property_create(handle->handle);
3543034Sdougm 	value = scf_value_create(handle->handle);
3553034Sdougm 	valuestr = malloc(vallen);
3564653Sdougm 	if (prop == NULL || value == NULL || valuestr == NULL ||
3574653Sdougm 	    scf_instance_get_pg(instance, "operation", handle->pg) != 0) {
3584653Sdougm 		goto out;
3594653Sdougm 	}
3604653Sdougm 	/*
3614653Sdougm 	 * Have a property group with desired name so now get
3624653Sdougm 	 * the known attributes.
3634653Sdougm 	 */
3644653Sdougm 	if (scf_pg_get_property(handle->pg, "state", prop) == 0) {
3654653Sdougm 		/* Found the property so get the value */
3663034Sdougm 		if (scf_property_get_value(prop, value) == 0) {
3674653Sdougm 			if (scf_value_get_astring(value, valuestr,
3684653Sdougm 			    vallen) >= 0) {
3694653Sdougm 				xmlSetProp(root, (xmlChar *)"state",
3703034Sdougm 				    (xmlChar *)valuestr);
3714653Sdougm 			}
3723034Sdougm 		}
3734653Sdougm 	}
3744653Sdougm 	if (scf_pg_get_property(handle->pg, "zfs", prop) == 0) {
3754653Sdougm 		/* Found the property so get the value */
3763034Sdougm 		if (scf_property_get_value(prop, value) == 0) {
3774653Sdougm 			if (scf_value_get_astring(value, valuestr,
3784653Sdougm 			    vallen) > 0) {
3794653Sdougm 				xmlSetProp(root, (xmlChar *)"zfs",
3803034Sdougm 				    (xmlChar *)valuestr);
3814653Sdougm 			}
3823034Sdougm 		}
3833034Sdougm 	}
3844653Sdougm out:
3853034Sdougm 	if (valuestr != NULL)
3864653Sdougm 		free(valuestr);
3873034Sdougm 	if (value != NULL)
3884653Sdougm 		scf_value_destroy(value);
3893034Sdougm 	if (prop != NULL)
3904653Sdougm 		scf_property_destroy(prop);
3913034Sdougm }
3923034Sdougm 
3933034Sdougm /*
3944653Sdougm  * List of known share attributes.
3953034Sdougm  */
3963034Sdougm 
3973034Sdougm static char *share_attr[] = {
3983034Sdougm 	"path",
3993034Sdougm 	"id",
4005331Samw 	"drive-letter",
4015331Samw 	"exclude",
4023034Sdougm 	NULL,
4033034Sdougm };
4043034Sdougm 
4053034Sdougm static int
4063034Sdougm is_share_attr(char *name)
4073034Sdougm {
4083034Sdougm 	int i;
4093034Sdougm 	for (i = 0; share_attr[i] != NULL; i++)
4104653Sdougm 		if (strcmp(name, share_attr[i]) == 0)
4114653Sdougm 			return (1);
4123034Sdougm 	return (0);
4133034Sdougm }
4143034Sdougm 
4153034Sdougm /*
4165331Samw  * _sa_make_resource(node, valuestr)
4175331Samw  *
4185331Samw  * Make a resource node on the share node. The valusestr will either
4195331Samw  * be old format (SMF acceptable string) or new format (pretty much an
4205331Samw  * arbitrary string with "nnn:" prefixing in order to persist
4215331Samw  * mapping). The input valuestr will get modified in place. This is
4225331Samw  * only used in SMF repository parsing. A possible third field will be
4235331Samw  * a "description" string.
4245331Samw  */
4255331Samw 
4265331Samw static void
4275331Samw _sa_make_resource(xmlNodePtr node, char *valuestr)
4285331Samw {
4295331Samw 	char *idx;
4305331Samw 	char *name;
4315331Samw 	char *description = NULL;
4325331Samw 
4335331Samw 	idx = valuestr;
4345331Samw 	name = strchr(valuestr, ':');
4355331Samw 	if (name == NULL) {
4365331Samw 		/* this is old form so give an index of "0" */
4375331Samw 		idx = "0";
4385331Samw 		name = valuestr;
4395331Samw 	} else {
4405331Samw 		/* NUL the ':' and move past it */
4415331Samw 		*name++ = '\0';
4425331Samw 		/* There could also be a description string */
4435331Samw 		description = strchr(name, ':');
4445331Samw 		if (description != NULL)
4455331Samw 			*description++ = '\0';
4465331Samw 	}
4475331Samw 	node = xmlNewChild(node, NULL, (xmlChar *)"resource", NULL);
4485331Samw 	if (node != NULL) {
4495331Samw 		xmlSetProp(node, (xmlChar *)"name", (xmlChar *)name);
4505331Samw 		xmlSetProp(node, (xmlChar *)"id", (xmlChar *)idx);
4515331Samw 		/* SMF values are always persistent */
4525331Samw 		xmlSetProp(node, (xmlChar *)"type", (xmlChar *)"persist");
4535331Samw 		if (description != NULL && strlen(description) > 0) {
4545331Samw 			(void) xmlNewChild(node, NULL, (xmlChar *)"description",
4555331Samw 			    (xmlChar *)description);
4565331Samw 		}
4575331Samw 	}
4585331Samw }
4595331Samw 
4605331Samw 
4615331Samw /*
4623034Sdougm  * sa_share_from_pgroup
4633034Sdougm  *
4644653Sdougm  * Extract the share definition from the share property group. We do
4653034Sdougm  * some sanity checking to avoid bad data.
4663034Sdougm  *
4673034Sdougm  * Since this is only constructing the internal data structures, we
4683034Sdougm  * don't use the sa_* functions most of the time.
4693034Sdougm  */
4703034Sdougm void
4713034Sdougm sa_share_from_pgroup(xmlNodePtr root, scfutilhandle_t *handle,
4723034Sdougm 			scf_propertygroup_t *pg, char *id)
4733034Sdougm {
4743034Sdougm 	xmlNodePtr node;
4753034Sdougm 	char *name;
4763034Sdougm 	scf_iter_t *iter;
4773034Sdougm 	scf_property_t *prop;
4783034Sdougm 	scf_value_t *value;
4793034Sdougm 	ssize_t vallen;
4803034Sdougm 	char *valuestr;
4813034Sdougm 	int ret = SA_OK;
4823348Sdougm 	int have_path = 0;
4833034Sdougm 
4843034Sdougm 	/*
4853034Sdougm 	 * While preliminary check (starts with 'S') passed before
4863034Sdougm 	 * getting here. Need to make sure it is in ID syntax
4873034Sdougm 	 * (Snnnnnn). Note that shares with properties have similar
4883034Sdougm 	 * pgroups.
4893034Sdougm 	 */
4903034Sdougm 	vallen = strlen(id);
4913034Sdougm 	if (*id == SA_SHARE_PG_PREFIX[0] && vallen == SA_SHARE_PG_LEN) {
4924653Sdougm 		uuid_t uuid;
4934653Sdougm 		if (strncmp(id, SA_SHARE_PG_PREFIX,
4944653Sdougm 		    SA_SHARE_PG_PREFIXLEN) != 0 ||
4954653Sdougm 		    uuid_parse(id + 2, uuid) < 0)
4964653Sdougm 			return;
4974653Sdougm 	} else {
4983034Sdougm 		return;
4993034Sdougm 	}
5003034Sdougm 
5013034Sdougm 	vallen = get_scf_limit(SCF_LIMIT_MAX_VALUE_LENGTH);
5023034Sdougm 
5033034Sdougm 	iter = scf_iter_create(handle->handle);
5043034Sdougm 	value = scf_value_create(handle->handle);
5053034Sdougm 	prop = scf_property_create(handle->handle);
5063034Sdougm 	name = malloc(scf_max_name_len);
5073034Sdougm 	valuestr = malloc(vallen);
5083034Sdougm 
5093034Sdougm 	/*
5104653Sdougm 	 * Construct the share XML node. It is similar to sa_add_share
5113034Sdougm 	 * but never changes the repository. Also, there won't be any
5123034Sdougm 	 * ZFS or transient shares.  Root will be the group it is
5133034Sdougm 	 * associated with.
5143034Sdougm 	 */
5153034Sdougm 	node = xmlNewChild(root, NULL, (xmlChar *)"share", NULL);
5163034Sdougm 	if (node != NULL) {
5173034Sdougm 		/*
5184653Sdougm 		 * Make sure the UUID part of the property group is
5193034Sdougm 		 * stored in the share "id" property. We use this
5203034Sdougm 		 * later.
5213034Sdougm 		 */
5224653Sdougm 		xmlSetProp(node, (xmlChar *)"id", (xmlChar *)id);
5234653Sdougm 		xmlSetProp(node, (xmlChar *)"type", (xmlChar *)"persist");
5243034Sdougm 	}
5253034Sdougm 
5264653Sdougm 	if (iter == NULL || value == NULL || prop == NULL || name == NULL)
5274653Sdougm 		goto out;
5284653Sdougm 
5294653Sdougm 	/* Iterate over the share pg properties */
5304653Sdougm 	if (scf_iter_pg_properties(iter, pg) != 0)
5314653Sdougm 		goto out;
5324653Sdougm 
5334653Sdougm 	while (scf_iter_next_property(iter, prop) > 0) {
5344653Sdougm 		ret = SA_SYSTEM_ERR; /* assume the worst */
5354653Sdougm 		if (scf_property_get_name(prop, name, scf_max_name_len) > 0) {
5363034Sdougm 			if (scf_property_get_value(prop, value) == 0) {
5374653Sdougm 				if (scf_value_get_astring(value, valuestr,
5384653Sdougm 				    vallen) >= 0) {
5394653Sdougm 					ret = SA_OK;
5404653Sdougm 				}
5415331Samw 			} else if (strcmp(name, "resource") == 0) {
5425331Samw 				ret = SA_OK;
5433034Sdougm 			}
5444653Sdougm 		}
5455331Samw 		if (ret != SA_OK)
5465331Samw 			continue;
5475331Samw 		/*
5485331Samw 		 * Check that we have the "path" property in
5495331Samw 		 * name. The string in name will always be nul
5505331Samw 		 * terminated if scf_property_get_name()
5515331Samw 		 * succeeded.
5525331Samw 		 */
5535331Samw 		if (strcmp(name, "path") == 0)
5545331Samw 			have_path = 1;
5555331Samw 		if (is_share_attr(name)) {
5563348Sdougm 			/*
5575331Samw 			 * If a share attr, then simple -
5585331Samw 			 * usually path and id name
5593348Sdougm 			 */
5605331Samw 			xmlSetProp(node, (xmlChar *)name,
5615331Samw 			    (xmlChar *)valuestr);
5625331Samw 		} else if (strcmp(name, "resource") == 0) {
5635331Samw 			/*
5645331Samw 			 * Resource names handled differently since
5655331Samw 			 * there can be multiple on each share. The
5665331Samw 			 * "resource" id must be preserved since this
5675331Samw 			 * will be used by some protocols in mapping
5685331Samw 			 * "property spaces" to names and is always
5695331Samw 			 * used to create SMF property groups specific
5705331Samw 			 * to resources.  CIFS needs this.  The first
5715331Samw 			 * value is present so add and then loop for
5725331Samw 			 * any additional. Since this is new and
5735331Samw 			 * previous values may exist, handle
5745331Samw 			 * conversions.
5755331Samw 			 */
5765331Samw 			scf_iter_t *viter;
5775331Samw 			viter = scf_iter_create(handle->handle);
5785331Samw 			if (viter != NULL &&
5795331Samw 			    scf_iter_property_values(viter, prop) == 0) {
5805331Samw 				while (scf_iter_next_value(viter, value) > 0) {
5815331Samw 					/* Have a value so process it */
5825331Samw 					if (scf_value_get_ustring(value,
5835331Samw 					    valuestr, vallen) >= 0) {
5845331Samw 						/* have a ustring */
5855331Samw 						_sa_make_resource(node,
5865331Samw 						    valuestr);
5875331Samw 					} else if (scf_value_get_astring(value,
5885331Samw 					    valuestr, vallen) >= 0) {
5895331Samw 						/* have an astring */
5905331Samw 						_sa_make_resource(node,
5915331Samw 						    valuestr);
5925331Samw 					}
5934653Sdougm 				}
5945331Samw 				scf_iter_destroy(viter);
5955331Samw 			}
5965331Samw 		} else {
5975331Samw 			if (strcmp(name, "description") == 0) {
5985331Samw 				/* We have a description node */
5995331Samw 				xmlNodePtr desc;
6005331Samw 				desc = xmlNewChild(node, NULL,
6015331Samw 				    (xmlChar *)"description", NULL);
6025331Samw 				if (desc != NULL)
6035331Samw 					xmlNodeSetContent(desc,
6045331Samw 					    (xmlChar *)valuestr);
6053034Sdougm 			}
6063034Sdougm 		}
6073034Sdougm 	}
6084653Sdougm out:
6093348Sdougm 	/*
6104653Sdougm 	 * A share without a path is broken so we want to not include
6113348Sdougm 	 * these.  They shouldn't happen but if you kill a sharemgr in
6123348Sdougm 	 * the process of creating a share, it could happen.  They
6133348Sdougm 	 * should be harmless.  It is also possible that another
6143348Sdougm 	 * sharemgr is running and in the process of creating a share.
6153348Sdougm 	 */
6163348Sdougm 	if (have_path == 0 && node != NULL) {
6174653Sdougm 		xmlUnlinkNode(node);
6184653Sdougm 		xmlFreeNode(node);
6193348Sdougm 	}
6203034Sdougm 	if (name != NULL)
6214653Sdougm 		free(name);
6223034Sdougm 	if (valuestr != NULL)
6234653Sdougm 		free(valuestr);
6243034Sdougm 	if (value != NULL)
6254653Sdougm 		scf_value_destroy(value);
6263034Sdougm 	if (iter != NULL)
6274653Sdougm 		scf_iter_destroy(iter);
6283034Sdougm 	if (prop != NULL)
6294653Sdougm 		scf_property_destroy(prop);
6303034Sdougm }
6313034Sdougm 
6323034Sdougm /*
6333034Sdougm  * find_share_by_id(shareid)
6343034Sdougm  *
6353034Sdougm  * Search all shares in all groups until we find the share represented
6363034Sdougm  * by "id".
6373034Sdougm  */
6383034Sdougm 
6393034Sdougm static sa_share_t
6403910Sdougm find_share_by_id(sa_handle_t handle, char *shareid)
6413034Sdougm {
6423034Sdougm 	sa_group_t group;
6433034Sdougm 	sa_share_t share = NULL;
6443034Sdougm 	char *id = NULL;
6453034Sdougm 	int done = 0;
6463034Sdougm 
6474653Sdougm 	for (group = sa_get_group(handle, NULL);
6484653Sdougm 	    group != NULL && !done;
6494653Sdougm 	    group = sa_get_next_group(group)) {
6504653Sdougm 		for (share = sa_get_share(group, NULL);
6514653Sdougm 		    share != NULL;
6524653Sdougm 		    share = sa_get_next_share(share)) {
6533034Sdougm 			id = sa_get_share_attr(share, "id");
6543034Sdougm 			if (id != NULL && strcmp(id, shareid) == 0) {
6553034Sdougm 				sa_free_attr_string(id);
6563034Sdougm 				id = NULL;
6573034Sdougm 				done++;
6583034Sdougm 				break;
6593034Sdougm 			}
6603034Sdougm 			if (id != NULL) {
6614653Sdougm 				sa_free_attr_string(id);
6624653Sdougm 				id = NULL;
6633034Sdougm 			}
6643034Sdougm 		}
6653034Sdougm 	}
6663034Sdougm 	return (share);
6673034Sdougm }
6683034Sdougm 
6693034Sdougm /*
6705331Samw  * find_resource_by_index(share, index)
6715331Samw  *
6725331Samw  * Search the resource records on the share for the id index.
6735331Samw  */
6745331Samw static sa_resource_t
6755331Samw find_resource_by_index(sa_share_t share, char *index)
6765331Samw {
6775331Samw 	sa_resource_t resource;
6785331Samw 	sa_resource_t found = NULL;
6795331Samw 	char *id;
6805331Samw 
6815331Samw 	for (resource = sa_get_share_resource(share, NULL);
6825331Samw 	    resource != NULL && found == NULL;
6835331Samw 	    resource = sa_get_next_resource(resource)) {
6845331Samw 		id = (char *)xmlGetProp((xmlNodePtr)resource, (xmlChar *)"id");
6855331Samw 		if (id != NULL) {
6865331Samw 			if (strcmp(id, index) == 0) {
6875331Samw 				/* found it so save in "found" */
6885331Samw 				found = resource;
6895331Samw 			}
6905331Samw 			sa_free_attr_string(id);
6915331Samw 		}
6925331Samw 	}
6935331Samw 	return (found);
6945331Samw }
6955331Samw 
6965331Samw /*
6975331Samw  * sa_share_props_from_pgroup(root, handle, pg, id, sahandle)
6983034Sdougm  *
6994653Sdougm  * Extract share properties from the SMF property group. More sanity
7003034Sdougm  * checks are done and the share object is created. We ignore some
7013034Sdougm  * errors that could exist in the repository and only worry about
7023034Sdougm  * property groups that validate in naming.
7033034Sdougm  */
7043034Sdougm 
7053034Sdougm static int
7063034Sdougm sa_share_props_from_pgroup(xmlNodePtr root, scfutilhandle_t *handle,
7073910Sdougm 			scf_propertygroup_t *pg, char *id, sa_handle_t sahandle)
7083034Sdougm {
7093034Sdougm 	xmlNodePtr node;
7104653Sdougm 	char *name = NULL;
7114653Sdougm 	scf_iter_t *iter = NULL;
7124653Sdougm 	scf_property_t *prop = NULL;
7134653Sdougm 	scf_value_t *value = NULL;
7143034Sdougm 	ssize_t vallen;
7154653Sdougm 	char *valuestr = NULL;
7163034Sdougm 	int ret = SA_OK;
7173034Sdougm 	char *sectype = NULL;
7183034Sdougm 	char *proto;
7193034Sdougm 	sa_share_t share;
7204653Sdougm 	uuid_t uuid;
7213034Sdougm 
7223034Sdougm 	/*
7233034Sdougm 	 * While preliminary check (starts with 'S') passed before
7243034Sdougm 	 * getting here. Need to make sure it is in ID syntax
7253034Sdougm 	 * (Snnnnnn). Note that shares with properties have similar
7263034Sdougm 	 * pgroups. If the pg name is more than SA_SHARE_PG_LEN
7273034Sdougm 	 * characters, it is likely one of the protocol/security
7283034Sdougm 	 * versions.
7293034Sdougm 	 */
7303034Sdougm 	vallen = strlen(id);
7314653Sdougm 	if (*id != SA_SHARE_PG_PREFIX[0] || vallen <= SA_SHARE_PG_LEN) {
7324653Sdougm 		/*
7334653Sdougm 		 * It is ok to not have what we thought since someone might
7344653Sdougm 		 * have added a name via SMF.
7354653Sdougm 		 */
7364653Sdougm 		return (ret);
7374653Sdougm 	}
7384653Sdougm 	if (strncmp(id, SA_SHARE_PG_PREFIX, SA_SHARE_PG_PREFIXLEN) == 0) {
7393034Sdougm 		proto = strchr(id, '_');
7403034Sdougm 		if (proto == NULL)
7414653Sdougm 			return (ret);
7423034Sdougm 		*proto++ = '\0';
7433034Sdougm 		if (uuid_parse(id + SA_SHARE_PG_PREFIXLEN, uuid) < 0)
7444653Sdougm 			return (ret);
7453034Sdougm 		/*
7463034Sdougm 		 * probably a legal optionset so check a few more
7473034Sdougm 		 * syntax points below.
7483034Sdougm 		 */
7493034Sdougm 		if (*proto == '\0') {
7504653Sdougm 			/* not a valid proto (null) */
7514653Sdougm 			return (ret);
7523034Sdougm 		}
7535331Samw 
7543034Sdougm 		sectype = strchr(proto, '_');
7553034Sdougm 		if (sectype != NULL)
7564653Sdougm 			*sectype++ = '\0';
7573034Sdougm 		if (!valid_protocol(proto))
7584653Sdougm 			return (ret);
7593034Sdougm 	}
7603034Sdougm 
7613034Sdougm 	/*
7624653Sdougm 	 * To get here, we have a valid protocol and possibly a
7633034Sdougm 	 * security. We now have to find the share that it is really
7643034Sdougm 	 * associated with. The "id" portion of the pgroup name will
7653034Sdougm 	 * match.
7663034Sdougm 	 */
7673034Sdougm 
7683910Sdougm 	share = find_share_by_id(sahandle, id);
7693034Sdougm 	if (share == NULL)
7704653Sdougm 		return (SA_BAD_PATH);
7713034Sdougm 
7723034Sdougm 	root = (xmlNodePtr)share;
7733034Sdougm 
7743034Sdougm 	vallen = get_scf_limit(SCF_LIMIT_MAX_VALUE_LENGTH);
7753034Sdougm 
7764653Sdougm 	if (sectype == NULL)
7774653Sdougm 		node = xmlNewChild(root, NULL, (xmlChar *)"optionset", NULL);
7784653Sdougm 	else {
7795331Samw 		if (isdigit((int)*sectype)) {
7805331Samw 			sa_resource_t resource;
7815331Samw 			/*
7825331Samw 			 * If sectype[0] is a digit, then it is an index into
7835331Samw 			 * the resource names. We need to find a resource
7845331Samw 			 * record and then get the properties into an
7855331Samw 			 * optionset. The optionset becomes the "node" and the
7865331Samw 			 * rest is hung off of the share.
7875331Samw 			 */
7885331Samw 			resource = find_resource_by_index(share, sectype);
7895331Samw 			if (resource != NULL) {
7905331Samw 				node = xmlNewChild(resource, NULL,
7915331Samw 				    (xmlChar *)"optionset", NULL);
7925331Samw 			} else {
793*5521Sas200622 				/* This shouldn't happen. */
7945331Samw 				ret = SA_SYSTEM_ERR;
795*5521Sas200622 				goto out;
7965331Samw 			}
7975331Samw 		} else {
7985331Samw 			/*
7995331Samw 			 * If not a digit, then it is a security type
8005331Samw 			 * (alternate option space). Security types start with
8015331Samw 			 * an alphabetic.
8025331Samw 			 */
8035331Samw 			node = xmlNewChild(root, NULL, (xmlChar *)"security",
8045331Samw 			    NULL);
8055331Samw 			if (node != NULL)
8065331Samw 				xmlSetProp(node, (xmlChar *)"sectype",
8075331Samw 				    (xmlChar *)sectype);
8085331Samw 		}
8094653Sdougm 	}
8104653Sdougm 	if (node == NULL) {
8114653Sdougm 		ret = SA_NO_MEMORY;
8124653Sdougm 		goto out;
8134653Sdougm 	}
8144653Sdougm 
8154653Sdougm 	xmlSetProp(node, (xmlChar *)"type", (xmlChar *)proto);
8164653Sdougm 	/* now find the properties */
8173034Sdougm 	iter = scf_iter_create(handle->handle);
8183034Sdougm 	value = scf_value_create(handle->handle);
8193034Sdougm 	prop = scf_property_create(handle->handle);
8203034Sdougm 	name = malloc(scf_max_name_len);
8213034Sdougm 	valuestr = malloc(vallen);
8223034Sdougm 
8234653Sdougm 	if (iter == NULL || value == NULL || prop == NULL || name == NULL)
8244653Sdougm 		goto out;
8254653Sdougm 
8265331Samw 	/* iterate over the share pg properties */
8274653Sdougm 	if (scf_iter_pg_properties(iter, pg) == 0) {
8284653Sdougm 		while (scf_iter_next_property(iter, prop) > 0) {
8293034Sdougm 			ret = SA_SYSTEM_ERR; /* assume the worst */
8303034Sdougm 			if (scf_property_get_name(prop, name,
8314653Sdougm 			    scf_max_name_len) > 0) {
8324653Sdougm 				if (scf_property_get_value(prop, value) == 0) {
8334653Sdougm 					if (scf_value_get_astring(value,
8344653Sdougm 					    valuestr, vallen) >= 0) {
8354653Sdougm 						ret = SA_OK;
8364653Sdougm 					}
8373034Sdougm 				}
8383034Sdougm 			} else {
8394653Sdougm 				ret = SA_SYSTEM_ERR;
8403034Sdougm 			}
8413034Sdougm 			if (ret == SA_OK) {
8424653Sdougm 				sa_property_t prop;
8434653Sdougm 				prop = sa_create_property(name, valuestr);
8444653Sdougm 				if (prop != NULL)
8454653Sdougm 					prop = (sa_property_t)xmlAddChild(node,
8464653Sdougm 					    (xmlNodePtr)prop);
8474653Sdougm 				else
8484653Sdougm 					ret = SA_NO_MEMORY;
8493034Sdougm 			}
8503034Sdougm 		}
8513034Sdougm 	} else {
8524653Sdougm 		ret = SA_SYSTEM_ERR;
8533034Sdougm 	}
8544653Sdougm out:
8553034Sdougm 	if (iter != NULL)
8564653Sdougm 		scf_iter_destroy(iter);
8573034Sdougm 	if (value != NULL)
8584653Sdougm 		scf_value_destroy(value);
8593034Sdougm 	if (prop != NULL)
8604653Sdougm 		scf_property_destroy(prop);
8613034Sdougm 	if (name != NULL)
8624653Sdougm 		free(name);
8633034Sdougm 	if (valuestr != NULL)
8644653Sdougm 		free(valuestr);
8653034Sdougm 	return (ret);
8663034Sdougm }
8673034Sdougm 
8683034Sdougm /*
8693034Sdougm  * sa_extract_group(root, handle, instance)
8703034Sdougm  *
8714653Sdougm  * Get the config info for this instance of a group and create the XML
8723034Sdougm  * subtree from it.
8733034Sdougm  */
8743034Sdougm 
8753034Sdougm static int
8763034Sdougm sa_extract_group(xmlNodePtr root, scfutilhandle_t *handle,
8773910Sdougm 			scf_instance_t *instance, sa_handle_t sahandle)
8783034Sdougm {
8793034Sdougm 	char *buff;
8803034Sdougm 	xmlNodePtr node;
8813034Sdougm 	scf_iter_t *iter;
8823034Sdougm 	char *proto;
8833034Sdougm 	char *sectype;
8843034Sdougm 	int have_shares = 0;
8853034Sdougm 	int has_proto = 0;
8863034Sdougm 	int is_default = 0;
8873034Sdougm 	int ret = SA_OK;
8883034Sdougm 	int err;
8893034Sdougm 
8903034Sdougm 	buff = malloc(scf_max_name_len);
8914653Sdougm 	if (buff == NULL)
8924653Sdougm 		return (SA_NO_MEMORY);
8934653Sdougm 
8943034Sdougm 	iter = scf_iter_create(handle->handle);
8954653Sdougm 	if (iter == NULL) {
8964653Sdougm 		ret = SA_NO_MEMORY;
8974653Sdougm 		goto out;
8984653Sdougm 	}
8994653Sdougm 
9004653Sdougm 	if (scf_instance_get_name(instance, buff, scf_max_name_len) > 0) {
9013034Sdougm 		node = xmlNewChild(root, NULL, (xmlChar *)"group", NULL);
9024653Sdougm 		if (node == NULL) {
9034653Sdougm 			ret = SA_NO_MEMORY;
9044653Sdougm 			goto out;
9054653Sdougm 		}
9064653Sdougm 		xmlSetProp(node, (xmlChar *)"name", (xmlChar *)buff);
9074653Sdougm 		if (strcmp(buff, "default") == 0)
9083034Sdougm 			is_default++;
9094653Sdougm 
9104653Sdougm 		sa_extract_attrs(node, handle, instance);
9114653Sdougm 		/*
9124653Sdougm 		 * Iterate through all the property groups
9134653Sdougm 		 * looking for those with security or
9144653Sdougm 		 * optionset prefixes. The names of the
9154653Sdougm 		 * matching pgroups are parsed to get the
9164653Sdougm 		 * protocol, and for security, the sectype.
9174653Sdougm 		 * Syntax is as follows:
9184653Sdougm 		 *    optionset | optionset_<proto>
9194653Sdougm 		 *    security_default | security_<proto>_<sectype>
9204653Sdougm 		 * "operation" is handled by
9214653Sdougm 		 * sa_extract_attrs().
9224653Sdougm 		 */
9234653Sdougm 		if (scf_iter_instance_pgs(iter, instance) != 0) {
9244653Sdougm 			ret = SA_NO_MEMORY;
9254653Sdougm 			goto out;
9264653Sdougm 		}
9274653Sdougm 		while (scf_iter_next_pg(iter, handle->pg) > 0) {
9284653Sdougm 			/* Have a pgroup so sort it out */
9294653Sdougm 			ret = scf_pg_get_name(handle->pg, buff,
9304653Sdougm 			    scf_max_name_len);
9314653Sdougm 			if (ret  > 0) {
9324653Sdougm 				if (buff[0] == SA_SHARE_PG_PREFIX[0]) {
9333034Sdougm 					sa_share_from_pgroup(node, handle,
9344653Sdougm 					    handle->pg, buff);
9353034Sdougm 					have_shares++;
9364653Sdougm 				} else if (strncmp(buff, "optionset", 9) ==
9374653Sdougm 				    0) {
9383034Sdougm 					char *nodetype = "optionset";
9394653Sdougm 					/* Have an optionset */
9403034Sdougm 					sectype = NULL;
9413034Sdougm 					proto = strchr(buff, '_');
9423034Sdougm 					if (proto != NULL) {
9434653Sdougm 						*proto++ = '\0';
9444653Sdougm 						sectype = strchr(proto, '_');
9454653Sdougm 						if (sectype != NULL) {
9464653Sdougm 							*sectype++ = '\0';
9474653Sdougm 							nodetype = "security";
9484653Sdougm 						}
9493034Sdougm 					}
9503034Sdougm 					ret = sa_extract_pgroup(node, handle,
9514653Sdougm 					    handle->pg, nodetype, proto,
9524653Sdougm 					    sectype);
9533034Sdougm 					has_proto++;
9544653Sdougm 				} else if (strncmp(buff, "security", 8) == 0) {
9553034Sdougm 					/*
9564653Sdougm 					 * Have a security (note that
9573034Sdougm 					 * this should change in the
9583034Sdougm 					 * future)
9593034Sdougm 					 */
9603034Sdougm 					proto = strchr(buff, '_');
9613034Sdougm 					sectype = NULL;
9623034Sdougm 					if (proto != NULL) {
9634653Sdougm 						*proto++ = '\0';
9644653Sdougm 						sectype = strchr(proto, '_');
9654653Sdougm 						if (sectype != NULL)
9664653Sdougm 							*sectype++ = '\0';
9674653Sdougm 						if (strcmp(proto, "default") ==
9684653Sdougm 						    0)
9694653Sdougm 							proto = NULL;
9703034Sdougm 					}
9713034Sdougm 					ret = sa_extract_pgroup(node, handle,
9724653Sdougm 					    handle->pg, "security", proto,
9734653Sdougm 					    sectype);
9743034Sdougm 					has_proto++;
9753034Sdougm 				}
9764653Sdougm 				/* Ignore everything else */
9773034Sdougm 			}
9784653Sdougm 		}
9794653Sdougm 		/*
9804653Sdougm 		 * Make sure we have a valid default group.
9814653Sdougm 		 * On first boot, default won't have any
9824653Sdougm 		 * protocols defined and won't be enabled (but
9834653Sdougm 		 * should be).
9844653Sdougm 		 */
9854653Sdougm 		if (is_default) {
9864653Sdougm 			char *state = sa_get_group_attr((sa_group_t)node,
9874653Sdougm 			    "state");
9884653Sdougm 			char **protos;
9894653Sdougm 			int numprotos;
9904653Sdougm 			int i;
9913034Sdougm 
9924653Sdougm 			if (state == NULL) {
9933034Sdougm 				/* set attribute to enabled */
9943034Sdougm 				(void) sa_set_group_attr((sa_group_t)node,
9954653Sdougm 				    "state", "enabled");
9964653Sdougm 				/* We can assume no protocols */
9973034Sdougm 				numprotos = sa_get_protocols(&protos);
9983034Sdougm 				for (i = 0; i < numprotos; i++)
9994653Sdougm 					(void) sa_create_optionset(
10004653Sdougm 					    (sa_group_t)node, protos[i]);
10013034Sdougm 				if (numprotos > 0)
10024653Sdougm 					free(protos);
10034653Sdougm 			} else {
10043034Sdougm 				sa_free_attr_string(state);
10053034Sdougm 			}
10064653Sdougm 		}
10074653Sdougm 		/* Do a second pass if shares were found */
10084653Sdougm 		if (have_shares && scf_iter_instance_pgs(iter, instance) == 0) {
10094653Sdougm 			while (scf_iter_next_pg(iter, handle->pg) > 0) {
10103034Sdougm 				/*
10114653Sdougm 				 * Have a pgroup so see if it is a
10123034Sdougm 				 * share optionset
10133034Sdougm 				 */
10143034Sdougm 				err = scf_pg_get_name(handle->pg, buff,
10154653Sdougm 				    scf_max_name_len);
10164653Sdougm 				if (err  <= 0)
10174653Sdougm 					continue;
10184653Sdougm 				if (buff[0] == SA_SHARE_PG_PREFIX[0]) {
10193034Sdougm 					ret = sa_share_props_from_pgroup(node,
10204653Sdougm 					    handle, handle->pg, buff,
10214653Sdougm 					    sahandle);
10223034Sdougm 				}
10233034Sdougm 			}
10243034Sdougm 		}
10253034Sdougm 	}
10264653Sdougm out:
10273034Sdougm 	if (iter != NULL)
10284653Sdougm 		scf_iter_destroy(iter);
10293034Sdougm 	if (buff != NULL)
10304653Sdougm 		free(buff);
10313034Sdougm 	return (ret);
10323034Sdougm }
10333034Sdougm 
10343034Sdougm /*
10353034Sdougm  * sa_extract_defaults(root, handle, instance)
10363034Sdougm  *
10374653Sdougm  * Local function to find the default properties that live in the
10385331Samw  * default instance's "operation" property group.
10393034Sdougm  */
10403034Sdougm 
10413034Sdougm static void
10423034Sdougm sa_extract_defaults(xmlNodePtr root, scfutilhandle_t *handle,
10433034Sdougm 		    scf_instance_t *instance)
10443034Sdougm {
10453034Sdougm 	xmlNodePtr node;
10463034Sdougm 	scf_property_t *prop;
10473034Sdougm 	scf_value_t *value;
10483034Sdougm 	char *valuestr;
10493034Sdougm 	ssize_t vallen;
10503034Sdougm 
10513034Sdougm 	vallen = get_scf_limit(SCF_LIMIT_MAX_VALUE_LENGTH);
10523034Sdougm 	prop = scf_property_create(handle->handle);
10533034Sdougm 	value = scf_value_create(handle->handle);
10543034Sdougm 	valuestr = malloc(vallen);
10554653Sdougm 
10564653Sdougm 	if (prop == NULL || value == NULL || vallen == 0 ||
10574653Sdougm 	    scf_instance_get_pg(instance, "operation", handle->pg) != 0)
10584653Sdougm 		goto out;
10594653Sdougm 
10604653Sdougm 	if (scf_pg_get_property(handle->pg, "legacy-timestamp", prop) != 0)
10614653Sdougm 		goto out;
10624653Sdougm 
10634653Sdougm 	/* Found the property so get the value */
10644653Sdougm 	if (scf_property_get_value(prop, value) == 0) {
10654653Sdougm 		if (scf_value_get_astring(value, valuestr, vallen) > 0) {
10663034Sdougm 			node = xmlNewChild(root, NULL, (xmlChar *)"legacy",
10674653Sdougm 			    NULL);
10683034Sdougm 			if (node != NULL) {
10694653Sdougm 				xmlSetProp(node, (xmlChar *)"timestamp",
10704653Sdougm 				    (xmlChar *)valuestr);
10714653Sdougm 				xmlSetProp(node, (xmlChar *)"path",
10724653Sdougm 				    (xmlChar *)SA_LEGACY_DFSTAB);
10733034Sdougm 			}
10743034Sdougm 		}
10753034Sdougm 	}
10764653Sdougm out:
10773034Sdougm 	if (valuestr != NULL)
10784653Sdougm 		free(valuestr);
10793034Sdougm 	if (value != NULL)
10804653Sdougm 		scf_value_destroy(value);
10813034Sdougm 	if (prop != NULL)
10824653Sdougm 		scf_property_destroy(prop);
10833034Sdougm }
10843034Sdougm 
10853034Sdougm 
10863034Sdougm /*
10875331Samw  * sa_get_config(handle, root, doc, sahandle)
10883034Sdougm  *
10894653Sdougm  * Walk the SMF repository for /network/shares/group and find all the
10903034Sdougm  * instances. These become group names.  Then add the XML structure
10913034Sdougm  * below the groups based on property groups and properties.
10923034Sdougm  */
10933034Sdougm int
10943973Sdougm sa_get_config(scfutilhandle_t *handle, xmlNodePtr root, sa_handle_t sahandle)
10953034Sdougm {
10963034Sdougm 	int ret = SA_OK;
10973034Sdougm 	scf_instance_t *instance;
10983034Sdougm 	scf_iter_t *iter;
10993034Sdougm 	char buff[BUFSIZ * 2];
11003034Sdougm 
11013034Sdougm 	instance = scf_instance_create(handle->handle);
11023034Sdougm 	iter = scf_iter_create(handle->handle);
11033973Sdougm 	if (instance != NULL && iter != NULL) {
11044653Sdougm 		if ((ret = scf_iter_service_instances(iter,
11054653Sdougm 		    handle->service)) == 0) {
11064653Sdougm 			while ((ret = scf_iter_next_instance(iter,
11074653Sdougm 			    instance)) > 0) {
11084653Sdougm 				if (scf_instance_get_name(instance, buff,
11094653Sdougm 				    sizeof (buff)) > 0) {
11104653Sdougm 					if (strcmp(buff, "default") == 0)
11114653Sdougm 						sa_extract_defaults(root,
11124653Sdougm 						    handle, instance);
11134653Sdougm 					ret = sa_extract_group(root, handle,
11144653Sdougm 					    instance, sahandle);
11154653Sdougm 				}
11164653Sdougm 			}
11173034Sdougm 		}
11183034Sdougm 	}
11193973Sdougm 
11204653Sdougm 	/* Always cleanup these */
11213034Sdougm 	if (instance != NULL)
11224653Sdougm 		scf_instance_destroy(instance);
11233034Sdougm 	if (iter != NULL)
11244653Sdougm 		scf_iter_destroy(iter);
11253034Sdougm 	return (ret);
11263034Sdougm }
11273034Sdougm 
11283034Sdougm /*
11293034Sdougm  * sa_get_instance(handle, instance)
11303034Sdougm  *
11314653Sdougm  * Get the instance of the group service. This is actually the
11323034Sdougm  * specific group name. The instance is needed for all property and
11333034Sdougm  * control operations.
11343034Sdougm  */
11353034Sdougm 
11363034Sdougm int
11373034Sdougm sa_get_instance(scfutilhandle_t *handle, char *instname)
11383034Sdougm {
11393034Sdougm 	if (scf_service_get_instance(handle->service, instname,
11404653Sdougm 	    handle->instance) != 0) {
11414653Sdougm 		return (SA_NO_SUCH_GROUP);
11423034Sdougm 	}
11433034Sdougm 	return (SA_OK);
11443034Sdougm }
11453034Sdougm 
11463034Sdougm /*
11473034Sdougm  * sa_create_instance(handle, instname)
11483034Sdougm  *
11493034Sdougm  * Create a new SMF service instance. There can only be one with a
11503034Sdougm  * given name.
11513034Sdougm  */
11523034Sdougm 
11533034Sdougm int
11543034Sdougm sa_create_instance(scfutilhandle_t *handle, char *instname)
11553034Sdougm {
11563034Sdougm 	int ret = SA_OK;
11573034Sdougm 	char instance[SA_GROUP_INST_LEN];
11583034Sdougm 	if (scf_service_add_instance(handle->service, instname,
11594653Sdougm 	    handle->instance) != 0) {
11603034Sdougm 	/* better error returns need to be added based on real error */
11614653Sdougm 		if (scf_error() == SCF_ERROR_PERMISSION_DENIED)
11624653Sdougm 			ret = SA_NO_PERMISSION;
11634653Sdougm 		else
11644653Sdougm 			ret = SA_DUPLICATE_NAME;
11653034Sdougm 	} else {
11664653Sdougm 		/* have the service created, so enable it */
11674653Sdougm 		(void) snprintf(instance, sizeof (instance), "%s:%s",
11684653Sdougm 		    SA_SVC_FMRI_BASE, instname);
11694653Sdougm 		(void) smf_enable_instance(instance, 0);
11703034Sdougm 	}
11713034Sdougm 	return (ret);
11723034Sdougm }
11733034Sdougm 
11743034Sdougm /*
11753034Sdougm  * sa_delete_instance(handle, instname)
11763034Sdougm  *
11773034Sdougm  * When a group goes away, we also remove the service instance.
11783034Sdougm  */
11793034Sdougm 
11803034Sdougm int
11813034Sdougm sa_delete_instance(scfutilhandle_t *handle, char *instname)
11823034Sdougm {
11833034Sdougm 	int ret;
11843034Sdougm 
11853034Sdougm 	if (strcmp(instname, "default") == 0) {
11864653Sdougm 		ret = SA_NO_PERMISSION;
11873034Sdougm 	} else {
11884653Sdougm 		if ((ret = sa_get_instance(handle, instname)) == SA_OK) {
11894653Sdougm 			if (scf_instance_delete(handle->instance) != 0)
11904653Sdougm 				/* need better analysis */
11914653Sdougm 				ret = SA_NO_PERMISSION;
11924653Sdougm 		}
11933034Sdougm 	}
11943034Sdougm 	return (ret);
11953034Sdougm }
11963034Sdougm 
11973034Sdougm /*
11983034Sdougm  * sa_create_pgroup(handle, pgroup)
11993034Sdougm  *
12003034Sdougm  * create a new property group
12013034Sdougm  */
12023034Sdougm 
12033034Sdougm int
12043034Sdougm sa_create_pgroup(scfutilhandle_t *handle, char *pgroup)
12053034Sdougm {
12063034Sdougm 	int ret = SA_OK;
12073034Sdougm 	/*
12084653Sdougm 	 * Only create a handle if it doesn't exist. It is ok to exist
12093034Sdougm 	 * since the pg handle will be set as a side effect.
12103034Sdougm 	 */
12114653Sdougm 	if (handle->pg == NULL)
12124653Sdougm 		handle->pg = scf_pg_create(handle->handle);
12134653Sdougm 
12143034Sdougm 	/*
12154653Sdougm 	 * If the pgroup exists, we are done. If it doesn't, then we
12163034Sdougm 	 * need to actually add one to the service instance.
12173034Sdougm 	 */
12183034Sdougm 	if (scf_instance_get_pg(handle->instance,
12194653Sdougm 	    pgroup, handle->pg) != 0) {
12204653Sdougm 		/* Doesn't exist so create one */
12214653Sdougm 		if (scf_instance_add_pg(handle->instance, pgroup,
12224653Sdougm 		    SCF_GROUP_APPLICATION, 0, handle->pg) != 0) {
12234653Sdougm 			switch (scf_error()) {
12244653Sdougm 			case SCF_ERROR_PERMISSION_DENIED:
12254653Sdougm 				ret = SA_NO_PERMISSION;
12264653Sdougm 				break;
12274653Sdougm 			default:
12284653Sdougm 				ret = SA_SYSTEM_ERR;
12294653Sdougm 				break;
12304653Sdougm 			}
12313034Sdougm 		}
12323034Sdougm 	}
12333034Sdougm 	return (ret);
12343034Sdougm }
12353034Sdougm 
12363034Sdougm /*
12373034Sdougm  * sa_delete_pgroup(handle, pgroup)
12383034Sdougm  *
12394653Sdougm  * Remove the property group from the current instance of the service,
12403034Sdougm  * but only if it actually exists.
12413034Sdougm  */
12423034Sdougm 
12433034Sdougm int
12443034Sdougm sa_delete_pgroup(scfutilhandle_t *handle, char *pgroup)
12453034Sdougm {
12463034Sdougm 	int ret = SA_OK;
12473034Sdougm 	/*
12484653Sdougm 	 * Only delete if it does exist.
12493034Sdougm 	 */
12504653Sdougm 	if (scf_instance_get_pg(handle->instance, pgroup, handle->pg) == 0) {
12514653Sdougm 		/* does exist so delete it */
12524653Sdougm 		if (scf_pg_delete(handle->pg) != 0)
12534653Sdougm 			ret = SA_SYSTEM_ERR;
12544653Sdougm 	} else {
12553034Sdougm 		ret = SA_SYSTEM_ERR;
12563034Sdougm 	}
12573034Sdougm 	if (ret == SA_SYSTEM_ERR &&
12583034Sdougm 	    scf_error() == SCF_ERROR_PERMISSION_DENIED) {
12593034Sdougm 		ret = SA_NO_PERMISSION;
12603034Sdougm 	}
12613034Sdougm 	return (ret);
12623034Sdougm }
12633034Sdougm 
12643034Sdougm /*
12653034Sdougm  * sa_start_transaction(handle, pgroup)
12663034Sdougm  *
12673034Sdougm  * Start an SMF transaction so we can deal with properties. it would
12683034Sdougm  * be nice to not have to expose this, but we have to in order to
12693034Sdougm  * optimize.
12703034Sdougm  *
12713034Sdougm  * Basic model is to hold the transaction in the handle and allow
12723034Sdougm  * property adds/deletes/updates to be added then close the
12733034Sdougm  * transaction (or abort).  There may eventually be a need to handle
12743034Sdougm  * other types of transaction mechanisms but we don't do that now.
12753034Sdougm  *
12763034Sdougm  * An sa_start_transaction must be followed by either an
12773034Sdougm  * sa_end_transaction or sa_abort_transaction before another
12783034Sdougm  * sa_start_transaction can be done.
12793034Sdougm  */
12803034Sdougm 
12813034Sdougm int
12823034Sdougm sa_start_transaction(scfutilhandle_t *handle, char *propgroup)
12833034Sdougm {
12843034Sdougm 	int ret = SA_OK;
12853034Sdougm 	/*
12864653Sdougm 	 * Lookup the property group and create it if it doesn't already
12873034Sdougm 	 * exist.
12883034Sdougm 	 */
12893034Sdougm 	if (handle->scf_state == SCH_STATE_INIT) {
12904653Sdougm 		ret = sa_create_pgroup(handle, propgroup);
12914653Sdougm 		if (ret == SA_OK) {
12924653Sdougm 			handle->trans = scf_transaction_create(handle->handle);
12934653Sdougm 			if (handle->trans != NULL) {
12944653Sdougm 				if (scf_transaction_start(handle->trans,
12954653Sdougm 				    handle->pg) != 0) {
12964653Sdougm 					ret = SA_SYSTEM_ERR;
12974653Sdougm 				}
12984653Sdougm 				if (ret != SA_OK) {
12994653Sdougm 					scf_transaction_destroy(handle->trans);
13004653Sdougm 					handle->trans = NULL;
13014653Sdougm 				}
13024653Sdougm 			} else {
13034653Sdougm 				ret = SA_SYSTEM_ERR;
13044653Sdougm 			}
13053034Sdougm 		}
13063034Sdougm 	}
13073034Sdougm 	if (ret == SA_SYSTEM_ERR &&
13083034Sdougm 	    scf_error() == SCF_ERROR_PERMISSION_DENIED) {
13093034Sdougm 		ret = SA_NO_PERMISSION;
13103034Sdougm 	}
13113034Sdougm 	return (ret);
13123034Sdougm }
13133034Sdougm 
13143034Sdougm /*
13153034Sdougm  * sa_end_transaction(handle)
13163034Sdougm  *
13173034Sdougm  * Commit the changes that were added to the transaction in the
13183034Sdougm  * handle. Do all necessary cleanup.
13193034Sdougm  */
13203034Sdougm 
13213034Sdougm int
13223034Sdougm sa_end_transaction(scfutilhandle_t *handle)
13233034Sdougm {
13243034Sdougm 	int ret = SA_OK;
13253034Sdougm 
13263034Sdougm 	if (handle->trans == NULL) {
13274653Sdougm 		ret = SA_SYSTEM_ERR;
13283034Sdougm 	} else {
13294653Sdougm 		if (scf_transaction_commit(handle->trans) < 0)
13304653Sdougm 			ret = SA_SYSTEM_ERR;
13314653Sdougm 		scf_transaction_destroy_children(handle->trans);
13324653Sdougm 		scf_transaction_destroy(handle->trans);
13334653Sdougm 		handle->trans = NULL;
13343034Sdougm 	}
13353034Sdougm 	return (ret);
13363034Sdougm }
13373034Sdougm 
13383034Sdougm /*
13393034Sdougm  * sa_abort_transaction(handle)
13403034Sdougm  *
13413034Sdougm  * Abort the changes that were added to the transaction in the
13423034Sdougm  * handle. Do all necessary cleanup.
13433034Sdougm  */
13443034Sdougm 
13453034Sdougm void
13463034Sdougm sa_abort_transaction(scfutilhandle_t *handle)
13473034Sdougm {
13483034Sdougm 	if (handle->trans != NULL) {
13494653Sdougm 		scf_transaction_reset_all(handle->trans);
13504653Sdougm 		scf_transaction_destroy_children(handle->trans);
13514653Sdougm 		scf_transaction_destroy(handle->trans);
13524653Sdougm 		handle->trans = NULL;
13533034Sdougm 	}
13543034Sdougm }
13553034Sdougm 
13563034Sdougm /*
13573034Sdougm  * sa_set_property(handle, prop, value)
13583034Sdougm  *
13594653Sdougm  * Set a property transaction entry into the pending SMF transaction.
13603034Sdougm  */
13613034Sdougm 
13623034Sdougm int
13633034Sdougm sa_set_property(scfutilhandle_t *handle, char *propname, char *valstr)
13643034Sdougm {
13653034Sdougm 	int ret = SA_OK;
13663034Sdougm 	scf_value_t *value;
13673034Sdougm 	scf_transaction_entry_t *entry;
13683034Sdougm 	/*
13694653Sdougm 	 * Properties must be set in transactions and don't take
13703034Sdougm 	 * effect until the transaction has been ended/committed.
13713034Sdougm 	 */
13723034Sdougm 	value = scf_value_create(handle->handle);
13733034Sdougm 	entry = scf_entry_create(handle->handle);
13743034Sdougm 	if (value != NULL && entry != NULL) {
13754653Sdougm 		if (scf_transaction_property_change(handle->trans, entry,
13764653Sdougm 		    propname, SCF_TYPE_ASTRING) == 0 ||
13774653Sdougm 		    scf_transaction_property_new(handle->trans, entry,
13784653Sdougm 		    propname, SCF_TYPE_ASTRING) == 0) {
13794653Sdougm 			if (scf_value_set_astring(value, valstr) == 0) {
13804653Sdougm 				if (scf_entry_add_value(entry, value) != 0) {
13814653Sdougm 					ret = SA_SYSTEM_ERR;
13824653Sdougm 					scf_value_destroy(value);
13834653Sdougm 				}
13844653Sdougm 				/* The value is in the transaction */
13854653Sdougm 				value = NULL;
13864653Sdougm 			} else {
13874653Sdougm 				/* Value couldn't be constructed */
13884653Sdougm 				ret = SA_SYSTEM_ERR;
13894653Sdougm 			}
13904653Sdougm 			/* The entry is in the transaction */
13914653Sdougm 			entry = NULL;
13924653Sdougm 		} else {
13933034Sdougm 			ret = SA_SYSTEM_ERR;
13943034Sdougm 		}
13954653Sdougm 	} else {
13963034Sdougm 		ret = SA_SYSTEM_ERR;
13973034Sdougm 	}
13983034Sdougm 	if (ret == SA_SYSTEM_ERR) {
13994653Sdougm 		switch (scf_error()) {
14004653Sdougm 		case SCF_ERROR_PERMISSION_DENIED:
14014653Sdougm 			ret = SA_NO_PERMISSION;
14024653Sdougm 			break;
14034653Sdougm 		}
14043034Sdougm 	}
14053034Sdougm 	/*
14064653Sdougm 	 * Cleanup if there were any errors that didn't leave these
14073034Sdougm 	 * values where they would be cleaned up later.
14083034Sdougm 	 */
14093034Sdougm 	if (value != NULL)
14104653Sdougm 		scf_value_destroy(value);
14113034Sdougm 	if (entry != NULL)
14124653Sdougm 		scf_entry_destroy(entry);
14133034Sdougm 	return (ret);
14143034Sdougm }
14153034Sdougm 
14163034Sdougm /*
14175331Samw  * check_resource(share)
14185331Samw  *
14195331Samw  * Check to see if share has any persistent resources. We don't want
14205331Samw  * to save if they are all transient.
14215331Samw  */
14225331Samw static int
14235331Samw check_resource(sa_share_t share)
14245331Samw {
14255331Samw 	sa_resource_t resource;
14265331Samw 	int ret = B_FALSE;
14275331Samw 
14285331Samw 	for (resource = sa_get_share_resource(share, NULL);
14295331Samw 	    resource != NULL && ret == B_FALSE;
14305331Samw 	    resource = sa_get_next_resource(resource)) {
14315331Samw 		char *type;
14325331Samw 		type = sa_get_resource_attr(resource, "type");
14335331Samw 		if (type != NULL) {
14345331Samw 			if (strcmp(type, "transient") != 0) {
14355331Samw 				ret = B_TRUE;
14365331Samw 			}
14375331Samw 			sa_free_attr_string(type);
14385331Samw 		}
14395331Samw 	}
14405331Samw 	return (ret);
14415331Samw }
14425331Samw 
14435331Samw /*
14445331Samw  * sa_set_resource_property(handle, prop, value)
14455331Samw  *
14465331Samw  * set a property transaction entry into the pending SMF
14475331Samw  * transaction. We don't want to include any transient resources
14485331Samw  */
14495331Samw 
14505331Samw static int
14515331Samw sa_set_resource_property(scfutilhandle_t *handle, sa_share_t share)
14525331Samw {
14535331Samw 	int ret = SA_OK;
14545331Samw 	scf_value_t *value;
14555331Samw 	scf_transaction_entry_t *entry;
14565331Samw 	sa_resource_t resource;
14575331Samw 	char *valstr;
14585331Samw 	char *idstr;
14595331Samw 	char *description;
14605331Samw 	char *propstr = NULL;
14615331Samw 	size_t strsize;
14625331Samw 
14635331Samw 	/* don't bother if no persistent resources */
14645331Samw 	if (check_resource(share) == B_FALSE)
14655331Samw 		return (ret);
14665331Samw 
14675331Samw 	/*
14685331Samw 	 * properties must be set in transactions and don't take
14695331Samw 	 * effect until the transaction has been ended/committed.
14705331Samw 	 */
14715331Samw 	entry = scf_entry_create(handle->handle);
14725331Samw 	if (entry == NULL)
14735331Samw 		return (SA_SYSTEM_ERR);
14745331Samw 
14755331Samw 	if (scf_transaction_property_change(handle->trans, entry,
14765331Samw 	    "resource",	SCF_TYPE_ASTRING) != 0 &&
14775331Samw 	    scf_transaction_property_new(handle->trans, entry,
14785331Samw 	    "resource", SCF_TYPE_ASTRING) != 0) {
14795331Samw 		scf_entry_destroy(entry);
14805331Samw 		return (SA_SYSTEM_ERR);
14815331Samw 
14825331Samw 	}
14835331Samw 	for (resource = sa_get_share_resource(share, NULL);
14845331Samw 	    resource != NULL;
14855331Samw 	    resource = sa_get_next_resource(resource)) {
14865331Samw 		value = scf_value_create(handle->handle);
14875331Samw 		if (value == NULL) {
14885331Samw 			ret = SA_NO_MEMORY;
14895331Samw 			break;
14905331Samw 		}
14915331Samw 			/* Get size of complete string */
14925331Samw 		valstr = sa_get_resource_attr(resource, "name");
14935331Samw 		idstr = sa_get_resource_attr(resource, "id");
14945331Samw 		description = sa_get_resource_description(resource);
14955331Samw 		strsize = (valstr != NULL) ? strlen(valstr) : 0;
14965331Samw 		strsize += (idstr != NULL) ? strlen(idstr) : 0;
14975331Samw 		strsize += (description != NULL) ? strlen(description) : 0;
14985331Samw 		if (strsize > 0) {
14995331Samw 			strsize += 3; /* add nul and ':' */
15005331Samw 			propstr = (char *)malloc(strsize);
15015331Samw 			if (propstr == NULL) {
15025331Samw 				scf_value_destroy(value);
15035331Samw 				ret = SA_NO_MEMORY;
15045331Samw 				goto err;
15055331Samw 			}
15065331Samw 			if (idstr == NULL)
15075331Samw 				(void) snprintf(propstr, strsize, "%s",
15085331Samw 				    valstr ? valstr : "");
15095331Samw 			else
15105331Samw 				(void) snprintf(propstr, strsize, "%s:%s:%s",
15115331Samw 				    idstr ? idstr : "", valstr ? valstr : "",
15125331Samw 				    description ? description : "");
15135331Samw 			if (scf_value_set_astring(value, propstr) != 0) {
15145331Samw 				ret = SA_SYSTEM_ERR;
15155331Samw 				free(propstr);
15165331Samw 				scf_value_destroy(value);
15175331Samw 				break;
15185331Samw 			}
15195331Samw 			if (scf_entry_add_value(entry, value) != 0) {
15205331Samw 				ret = SA_SYSTEM_ERR;
15215331Samw 				free(propstr);
15225331Samw 				scf_value_destroy(value);
15235331Samw 				break;
15245331Samw 			}
15255331Samw 			/* the value is in the transaction */
15265331Samw 			value = NULL;
15275331Samw 			free(propstr);
15285331Samw 		}
15295331Samw err:
15305331Samw 		if (valstr != NULL)
15315331Samw 			sa_free_attr_string(valstr);
15325331Samw 		if (idstr != NULL)
15335331Samw 			sa_free_attr_string(idstr);
15345331Samw 		if (description != NULL)
15355331Samw 			sa_free_share_description(description);
15365331Samw 	}
15375331Samw 	/* the entry is in the transaction */
15385331Samw 	entry = NULL;
15395331Samw 
15405331Samw 	if (ret == SA_SYSTEM_ERR) {
15415331Samw 		switch (scf_error()) {
15425331Samw 		case SCF_ERROR_PERMISSION_DENIED:
15435331Samw 			ret = SA_NO_PERMISSION;
15445331Samw 			break;
15455331Samw 		}
15465331Samw 	}
15475331Samw 	/*
15485331Samw 	 * cleanup if there were any errors that didn't leave
15495331Samw 	 * these values where they would be cleaned up later.
15505331Samw 	 */
15515331Samw 	if (entry != NULL)
15525331Samw 		scf_entry_destroy(entry);
15535331Samw 
15545331Samw 	return (ret);
15555331Samw }
15565331Samw 
15575331Samw /*
15583034Sdougm  * sa_commit_share(handle, group, share)
15593034Sdougm  *
15604653Sdougm  *	Commit this share to the repository.
15613034Sdougm  *	properties are added if they exist but can be added later.
15623034Sdougm  *	Need to add to dfstab and sharetab, if appropriate.
15633034Sdougm  */
15643034Sdougm int
15653034Sdougm sa_commit_share(scfutilhandle_t *handle, sa_group_t group, sa_share_t share)
15663034Sdougm {
15673034Sdougm 	int ret = SA_OK;
15683034Sdougm 	char *groupname;
15693034Sdougm 	char *name;
15703034Sdougm 	char *description;
15713034Sdougm 	char *sharename;
15723034Sdougm 	ssize_t proplen;
15733034Sdougm 	char *propstring;
15743034Sdougm 
15753034Sdougm 	/*
15764653Sdougm 	 * Don't commit in the zfs group. We do commit legacy
15773034Sdougm 	 * (default) and all other groups/shares. ZFS is handled
15783034Sdougm 	 * through the ZFS configuration rather than SMF.
15793034Sdougm 	 */
15803034Sdougm 
15813034Sdougm 	groupname = sa_get_group_attr(group, "name");
15823034Sdougm 	if (groupname != NULL) {
15834653Sdougm 		if (strcmp(groupname, "zfs") == 0) {
15844653Sdougm 			/*
15854653Sdougm 			 * Adding to the ZFS group will result in the sharenfs
15864653Sdougm 			 * property being set but we don't want to do anything
15874653Sdougm 			 * SMF related at this point.
15884653Sdougm 			 */
15894653Sdougm 			sa_free_attr_string(groupname);
15904653Sdougm 			return (ret);
15914653Sdougm 		}
15923034Sdougm 	}
15933034Sdougm 
15943034Sdougm 	proplen = get_scf_limit(SCF_LIMIT_MAX_VALUE_LENGTH);
15953034Sdougm 	propstring = malloc(proplen);
15963034Sdougm 	if (propstring == NULL)
15974653Sdougm 		ret = SA_NO_MEMORY;
15983034Sdougm 
15993034Sdougm 	if (groupname != NULL && ret == SA_OK) {
16004653Sdougm 		ret = sa_get_instance(handle, groupname);
16014653Sdougm 		sa_free_attr_string(groupname);
16024653Sdougm 		groupname = NULL;
16034653Sdougm 		sharename = sa_get_share_attr(share, "id");
16044653Sdougm 		if (sharename == NULL) {
16054653Sdougm 			/* slipped by */
16064653Sdougm 			char shname[SA_SHARE_UUID_BUFLEN];
16074653Sdougm 			generate_unique_sharename(shname);
16084653Sdougm 			xmlSetProp((xmlNodePtr)share, (xmlChar *)"id",
16093034Sdougm 			    (xmlChar *)shname);
16104653Sdougm 			sharename = strdup(shname);
16113034Sdougm 		}
16124653Sdougm 		if (sharename != NULL) {
16134653Sdougm 			sigset_t old, new;
16144653Sdougm 			/*
16154653Sdougm 			 * Have a share name allocated so create a pgroup for
16164653Sdougm 			 * it. It may already exist, but that is OK.  In order
16174653Sdougm 			 * to avoid creating a share pgroup that doesn't have
16184653Sdougm 			 * a path property, block signals around the critical
16194653Sdougm 			 * region of creating the share pgroup and props.
16204653Sdougm 			 */
16214653Sdougm 			(void) sigprocmask(SIG_BLOCK, NULL, &new);
16224653Sdougm 			(void) sigaddset(&new, SIGHUP);
16234653Sdougm 			(void) sigaddset(&new, SIGINT);
16244653Sdougm 			(void) sigaddset(&new, SIGQUIT);
16254653Sdougm 			(void) sigaddset(&new, SIGTSTP);
16264653Sdougm 			(void) sigprocmask(SIG_SETMASK, &new, &old);
16274653Sdougm 
16284653Sdougm 			ret = sa_create_pgroup(handle, sharename);
16294653Sdougm 			if (ret == SA_OK) {
16304653Sdougm 				/*
16314653Sdougm 				 * Now start the transaction for the
16324653Sdougm 				 * properties that define this share. They may
16334653Sdougm 				 * exist so attempt to update before create.
16344653Sdougm 				 */
16354653Sdougm 				ret = sa_start_transaction(handle, sharename);
16364653Sdougm 			}
16374653Sdougm 			if (ret == SA_OK) {
16384653Sdougm 				name = sa_get_share_attr(share, "path");
16394653Sdougm 				if (name != NULL) {
16404653Sdougm 					/*
16414653Sdougm 					 * There needs to be a path
16424653Sdougm 					 * for a share to exist.
16434653Sdougm 					 */
16444653Sdougm 					ret = sa_set_property(handle, "path",
16454653Sdougm 					    name);
16464653Sdougm 					sa_free_attr_string(name);
16474653Sdougm 				} else {
16484653Sdougm 					ret = SA_NO_MEMORY;
16494653Sdougm 				}
16504653Sdougm 			}
16514653Sdougm 			if (ret == SA_OK) {
16525331Samw 				name = sa_get_share_attr(share, "drive-letter");
16535331Samw 				if (name != NULL) {
16545331Samw 					/* A drive letter may exist for SMB */
16554653Sdougm 					ret = sa_set_property(handle,
16565331Samw 					    "drive-letter", name);
16575331Samw 					sa_free_attr_string(name);
16584653Sdougm 				}
16594653Sdougm 			}
16604653Sdougm 			if (ret == SA_OK) {
16615331Samw 				name = sa_get_share_attr(share, "exclude");
16625331Samw 				if (name != NULL) {
16635331Samw 					/*
16645331Samw 					 * In special cases need to
16655331Samw 					 * exclude proto enable.
16665331Samw 					 */
16675331Samw 					ret = sa_set_property(handle,
16685331Samw 					    "exclude", name);
16695331Samw 					sa_free_attr_string(name);
16705331Samw 				}
16715331Samw 			}
16725331Samw 			if (ret == SA_OK) {
16735331Samw 				/*
16745331Samw 				 * If there are resource names, bundle them up
16755331Samw 				 * and save appropriately.
16765331Samw 				 */
16775331Samw 				ret = sa_set_resource_property(handle, share);
16785331Samw 			}
16795331Samw 
16805331Samw 			if (ret == SA_OK) {
16814653Sdougm 				description = sa_get_share_description(share);
16824653Sdougm 				if (description != NULL) {
16834653Sdougm 					ret = sa_set_property(handle,
16844653Sdougm 					    "description",
16854653Sdougm 					    description);
16864653Sdougm 					sa_free_share_description(description);
16874653Sdougm 				}
16884653Sdougm 			}
16894653Sdougm 			/* Make sure we cleanup the transaction */
16904653Sdougm 			if (ret == SA_OK) {
16914653Sdougm 				ret = sa_end_transaction(handle);
16924653Sdougm 			} else {
16934653Sdougm 				sa_abort_transaction(handle);
16944653Sdougm 			}
16954653Sdougm 
16964653Sdougm 			(void) sigprocmask(SIG_SETMASK, &old, NULL);
16974653Sdougm 
16984653Sdougm 			free(sharename);
16993034Sdougm 		}
17003034Sdougm 	}
17013034Sdougm 	if (ret == SA_SYSTEM_ERR) {
17024653Sdougm 		int err = scf_error();
17034653Sdougm 		if (err == SCF_ERROR_PERMISSION_DENIED)
17044653Sdougm 			ret = SA_NO_PERMISSION;
17053034Sdougm 	}
17063034Sdougm 	if (propstring != NULL)
17074653Sdougm 		free(propstring);
17083034Sdougm 	if (groupname != NULL)
17094653Sdougm 		sa_free_attr_string(groupname);
17103034Sdougm 
17113034Sdougm 	return (ret);
17123034Sdougm }
17133034Sdougm 
17143034Sdougm /*
17155331Samw  * remove_resources(handle, share, shareid)
17165331Samw  *
17175331Samw  * If the share has resources, remove all of them and their
17185331Samw  * optionsets.
17195331Samw  */
17205331Samw static int
17215331Samw remove_resources(scfutilhandle_t *handle, sa_share_t share, char *shareid)
17225331Samw {
17235331Samw 	sa_resource_t resource;
17245331Samw 	sa_optionset_t opt;
17255331Samw 	char *proto;
17265331Samw 	char *id;
17275331Samw 	ssize_t proplen;
17285331Samw 	char *propstring;
17295331Samw 	int ret = SA_OK;
17305331Samw 
17315331Samw 	proplen = get_scf_limit(SCF_LIMIT_MAX_VALUE_LENGTH);
17325331Samw 	propstring = malloc(proplen);
17335331Samw 	if (propstring == NULL)
17345331Samw 		return (SA_NO_MEMORY);
17355331Samw 
17365331Samw 	for (resource = sa_get_share_resource(share, NULL);
17375331Samw 	    resource != NULL; resource = sa_get_next_resource(resource)) {
17385331Samw 		id = sa_get_resource_attr(resource, "id");
17395331Samw 		if (id == NULL)
17405331Samw 			continue;
17415331Samw 		for (opt = sa_get_optionset(resource, NULL);
17425331Samw 		    opt != NULL; opt = sa_get_next_optionset(resource)) {
17435331Samw 			proto = sa_get_optionset_attr(opt, "type");
17445331Samw 			if (proto != NULL) {
17455331Samw 				(void) snprintf(propstring, proplen,
17465331Samw 				    "%s_%s_%s", shareid, proto, id);
17475331Samw 				ret = sa_delete_pgroup(handle, propstring);
17485331Samw 				sa_free_attr_string(proto);
17495331Samw 			}
17505331Samw 		}
17515331Samw 		sa_free_attr_string(id);
17525331Samw 	}
17535331Samw 	free(propstring);
17545331Samw 	return (ret);
17555331Samw }
17565331Samw 
17575331Samw /*
17583034Sdougm  * sa_delete_share(handle, group, share)
17593034Sdougm  *
17604653Sdougm  * Remove the specified share from the group (and service instance).
17613034Sdougm  */
17623034Sdougm 
17633034Sdougm int
17643034Sdougm sa_delete_share(scfutilhandle_t *handle, sa_group_t group, sa_share_t share)
17653034Sdougm {
17663034Sdougm 	int ret = SA_OK;
17673034Sdougm 	char *groupname = NULL;
17683034Sdougm 	char *shareid = NULL;
17693034Sdougm 	sa_optionset_t opt;
17703034Sdougm 	sa_security_t sec;
17713034Sdougm 	ssize_t proplen;
17723034Sdougm 	char *propstring;
17733034Sdougm 
17743034Sdougm 	proplen = get_scf_limit(SCF_LIMIT_MAX_VALUE_LENGTH);
17753034Sdougm 	propstring = malloc(proplen);
17763034Sdougm 	if (propstring == NULL)
17774653Sdougm 		ret = SA_NO_MEMORY;
17783034Sdougm 
17793034Sdougm 	if (ret == SA_OK) {
17804653Sdougm 		groupname = sa_get_group_attr(group, "name");
17814653Sdougm 		shareid = sa_get_share_attr(share, "id");
17824653Sdougm 		if (groupname == NULL || shareid == NULL) {
17834653Sdougm 			ret = SA_CONFIG_ERR;
17844653Sdougm 			goto out;
17854653Sdougm 		}
17863034Sdougm 		ret = sa_get_instance(handle, groupname);
17873034Sdougm 		if (ret == SA_OK) {
17885331Samw 			/* If a share has resources, remove them */
17895331Samw 			ret = remove_resources(handle, share, shareid);
17904653Sdougm 			/* If a share has properties, remove them */
17914653Sdougm 			ret = sa_delete_pgroup(handle, shareid);
17924653Sdougm 			for (opt = sa_get_optionset(share, NULL);
17934653Sdougm 			    opt != NULL;
17944653Sdougm 			    opt = sa_get_next_optionset(opt)) {
17954653Sdougm 				char *proto;
17964653Sdougm 				proto = sa_get_optionset_attr(opt, "type");
17974653Sdougm 				if (proto != NULL) {
17984653Sdougm 					(void) snprintf(propstring,
17994653Sdougm 					    proplen, "%s_%s", shareid,
18004653Sdougm 					    proto);
18014653Sdougm 					ret = sa_delete_pgroup(handle,
18024653Sdougm 					    propstring);
18034653Sdougm 					sa_free_attr_string(proto);
18044653Sdougm 				} else {
18054653Sdougm 					ret = SA_NO_MEMORY;
18064653Sdougm 				}
18073034Sdougm 			}
18083034Sdougm 			/*
18094653Sdougm 			 * If a share has security/negotiable
18103034Sdougm 			 * properties, remove them.
18113034Sdougm 			 */
18124653Sdougm 			for (sec = sa_get_security(share, NULL, NULL);
18134653Sdougm 			    sec != NULL;
18144653Sdougm 			    sec = sa_get_next_security(sec)) {
18154653Sdougm 				char *proto;
18164653Sdougm 				char *sectype;
18174653Sdougm 				proto = sa_get_security_attr(sec, "type");
18184653Sdougm 				sectype = sa_get_security_attr(sec, "sectype");
18194653Sdougm 				if (proto != NULL && sectype != NULL) {
18204653Sdougm 					(void) snprintf(propstring, proplen,
18214653Sdougm 					    "%s_%s_%s", shareid,  proto,
18224653Sdougm 					    sectype);
18234653Sdougm 					ret = sa_delete_pgroup(handle,
18244653Sdougm 					    propstring);
18254653Sdougm 				} else {
18264653Sdougm 					ret = SA_NO_MEMORY;
18274653Sdougm 				}
18284653Sdougm 				if (proto != NULL)
18294653Sdougm 					sa_free_attr_string(proto);
18304653Sdougm 				if (sectype != NULL)
18314653Sdougm 					sa_free_attr_string(sectype);
18323034Sdougm 			}
18333034Sdougm 		}
18343034Sdougm 	}
18354653Sdougm out:
18363034Sdougm 	if (groupname != NULL)
18374653Sdougm 		sa_free_attr_string(groupname);
18383034Sdougm 	if (shareid != NULL)
18394653Sdougm 		sa_free_attr_string(shareid);
18403034Sdougm 	if (propstring != NULL)
18414653Sdougm 		free(propstring);
18423034Sdougm 
18433034Sdougm 	return (ret);
18443034Sdougm }
1845