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 /*
235951Sdougm  * Copyright 2008 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>
435951Sdougm #include <sys/time.h>
44*5997Sdougm #include <libintl.h>
453034Sdougm 
463034Sdougm ssize_t scf_max_name_len;
473034Sdougm extern struct sa_proto_plugin *sap_proto_list;
483910Sdougm extern sa_handle_impl_t get_handle_for_root(xmlNodePtr);
495951Sdougm static void set_transaction_tstamp(sa_handle_impl_t);
503034Sdougm /*
513034Sdougm  * The SMF facility uses some properties that must exist. We want to
523034Sdougm  * skip over these when processing protocol options.
533034Sdougm  */
543034Sdougm static char *skip_props[] = {
553034Sdougm 	"modify_authorization",
563034Sdougm 	"action_authorization",
573034Sdougm 	"value_authorization",
583034Sdougm 	NULL
593034Sdougm };
603034Sdougm 
613034Sdougm /*
623034Sdougm  * sa_scf_fini(handle)
633034Sdougm  *
644653Sdougm  * Must be called when done. Called with the handle allocated in
653034Sdougm  * sa_scf_init(), it cleans up the state and frees any SCF resources
663034Sdougm  * still in use. Called by sa_fini().
673034Sdougm  */
683034Sdougm 
693034Sdougm void
703034Sdougm sa_scf_fini(scfutilhandle_t *handle)
713034Sdougm {
723034Sdougm 	if (handle != NULL) {
734653Sdougm 		int unbind = 0;
744653Sdougm 		if (handle->scope != NULL) {
754653Sdougm 			unbind = 1;
764653Sdougm 			scf_scope_destroy(handle->scope);
774653Sdougm 		}
784653Sdougm 		if (handle->instance != NULL)
794653Sdougm 			scf_instance_destroy(handle->instance);
804653Sdougm 		if (handle->service != NULL)
814653Sdougm 			scf_service_destroy(handle->service);
824653Sdougm 		if (handle->pg != NULL)
834653Sdougm 			scf_pg_destroy(handle->pg);
844653Sdougm 		if (handle->handle != NULL) {
854653Sdougm 			handle->scf_state = SCH_STATE_UNINIT;
864653Sdougm 			if (unbind)
874653Sdougm 				(void) scf_handle_unbind(handle->handle);
884653Sdougm 			scf_handle_destroy(handle->handle);
894653Sdougm 		}
904653Sdougm 		free(handle);
913034Sdougm 	}
923034Sdougm }
933034Sdougm 
943034Sdougm /*
953034Sdougm  * sa_scf_init()
963034Sdougm  *
974653Sdougm  * Must be called before using any of the SCF functions. Called by
983034Sdougm  * sa_init() during the API setup.
993034Sdougm  */
1003034Sdougm 
1013034Sdougm scfutilhandle_t *
1023910Sdougm sa_scf_init(sa_handle_impl_t ihandle)
1033034Sdougm {
1043034Sdougm 	scfutilhandle_t *handle;
1053034Sdougm 
1063034Sdougm 	scf_max_name_len = scf_limit(SCF_LIMIT_MAX_NAME_LENGTH);
1073034Sdougm 	if (scf_max_name_len <= 0)
1084653Sdougm 		scf_max_name_len = SA_MAX_NAME_LEN + 1;
1093034Sdougm 
1103034Sdougm 	handle = calloc(1, sizeof (scfutilhandle_t));
1114653Sdougm 	if (handle == NULL)
1124653Sdougm 		return (handle);
1134345Sdougm 
1144653Sdougm 	ihandle->scfhandle = handle;
1154653Sdougm 	handle->scf_state = SCH_STATE_INITIALIZING;
1164653Sdougm 	handle->handle = scf_handle_create(SCF_VERSION);
1174653Sdougm 	if (handle->handle == NULL) {
1183034Sdougm 		free(handle);
1193034Sdougm 		handle = NULL;
1203034Sdougm 		(void) printf("libshare could not access SMF repository: %s\n",
1214653Sdougm 		    scf_strerror(scf_error()));
1224653Sdougm 		return (handle);
1233034Sdougm 	}
1244653Sdougm 	if (scf_handle_bind(handle->handle) != 0)
1254653Sdougm 		goto err;
1264653Sdougm 
1274653Sdougm 	handle->scope = scf_scope_create(handle->handle);
1284653Sdougm 	handle->service = scf_service_create(handle->handle);
1294653Sdougm 	handle->pg = scf_pg_create(handle->handle);
1304653Sdougm 
1314653Sdougm 	/* Make sure we have sufficient SMF running */
1324653Sdougm 	handle->instance = scf_instance_create(handle->handle);
1334653Sdougm 	if (handle->scope == NULL || handle->service == NULL ||
1344653Sdougm 	    handle->pg == NULL || handle->instance == NULL)
1354653Sdougm 		goto err;
1364653Sdougm 	if (scf_handle_get_scope(handle->handle,
1374653Sdougm 	    SCF_SCOPE_LOCAL, handle->scope) != 0)
1384653Sdougm 		goto err;
1394653Sdougm 	if (scf_scope_get_service(handle->scope,
1404653Sdougm 	    SA_GROUP_SVC_NAME, handle->service) != 0)
1414653Sdougm 		goto err;
1424653Sdougm 
1434653Sdougm 	handle->scf_state = SCH_STATE_INIT;
1444653Sdougm 	if (sa_get_instance(handle, "default") != SA_OK) {
1454653Sdougm 		sa_group_t defgrp;
1464653Sdougm 		defgrp = sa_create_group((sa_handle_t)ihandle, "default", NULL);
147*5997Sdougm 		/* Only NFS enabled for "default" group. */
148*5997Sdougm 		if (defgrp != NULL)
149*5997Sdougm 			(void) sa_create_optionset(defgrp, "nfs");
1504653Sdougm 	}
1514653Sdougm 
1523034Sdougm 	return (handle);
1533034Sdougm 
1544653Sdougm 	/* Error handling/unwinding */
1553034Sdougm err:
1563034Sdougm 	(void) sa_scf_fini(handle);
1573034Sdougm 	(void) printf("libshare SMF initialization problem: %s\n",
1584653Sdougm 	    scf_strerror(scf_error()));
1593034Sdougm 	return (NULL);
1603034Sdougm }
1613034Sdougm 
1623034Sdougm /*
1633034Sdougm  * get_scf_limit(name)
1643034Sdougm  *
1653034Sdougm  * Since we use  scf_limit a lot and do the same  check and return the
1663034Sdougm  * same  value  if  it  fails,   implement  as  a  function  for  code
1673034Sdougm  * simplification.  Basically, if  name isn't found, return MAXPATHLEN
1683034Sdougm  * (1024) so we have a reasonable default buffer size.
1693034Sdougm  */
1703034Sdougm static ssize_t
1713034Sdougm get_scf_limit(uint32_t name)
1723034Sdougm {
1733034Sdougm 	ssize_t vallen;
1743034Sdougm 
1753034Sdougm 	vallen = scf_limit(name);
1763034Sdougm 	if (vallen == (ssize_t)-1)
1774653Sdougm 		vallen = MAXPATHLEN;
1783034Sdougm 	return (vallen);
1793034Sdougm }
1803034Sdougm 
1813034Sdougm /*
1823034Sdougm  * skip_property(name)
1833034Sdougm  *
1844653Sdougm  * Internal function to check to see if a property is an SMF magic
1853034Sdougm  * property that needs to be skipped.
1863034Sdougm  */
1873034Sdougm static int
1883034Sdougm skip_property(char *name)
1893034Sdougm {
1903034Sdougm 	int i;
1913034Sdougm 
1923034Sdougm 	for (i = 0; skip_props[i] != NULL; i++)
1934653Sdougm 		if (strcmp(name, skip_props[i]) == 0)
1943034Sdougm 		return (1);
1953034Sdougm 	return (0);
1963034Sdougm }
1973034Sdougm 
1983034Sdougm /*
1993034Sdougm  * generate_unique_sharename(sharename)
2003034Sdougm  *
2013034Sdougm  * Shares are represented in SMF as property groups. Due to share
2023034Sdougm  * paths containing characters that are not allowed in SMF names and
2033034Sdougm  * the need to be unique, we use UUIDs to construct a unique name.
2043034Sdougm  */
2053034Sdougm 
2063034Sdougm static void
2073034Sdougm generate_unique_sharename(char *sharename)
2083034Sdougm {
2093034Sdougm 	uuid_t uuid;
2103034Sdougm 
2113034Sdougm 	uuid_generate(uuid);
2123034Sdougm 	(void) strcpy(sharename, "S-");
2133034Sdougm 	uuid_unparse(uuid, sharename + 2);
2143034Sdougm }
2153034Sdougm 
2163034Sdougm /*
2173034Sdougm  * valid_protocol(proto)
2183034Sdougm  *
2194653Sdougm  * Check to see if the specified protocol is a valid one for the
2203034Sdougm  * general sharemgr facility. We determine this by checking which
2213034Sdougm  * plugin protocols were found.
2223034Sdougm  */
2233034Sdougm 
2243034Sdougm static int
2253034Sdougm valid_protocol(char *proto)
2263034Sdougm {
2273034Sdougm 	struct sa_proto_plugin *plugin;
2283034Sdougm 	for (plugin = sap_proto_list; plugin != NULL;
2293034Sdougm 	    plugin = plugin->plugin_next)
2304653Sdougm 		if (strcmp(proto, plugin->plugin_ops->sa_protocol) == 0)
2314653Sdougm 			return (1);
2323034Sdougm 	return (0);
2333034Sdougm }
2343034Sdougm 
2353034Sdougm /*
2363034Sdougm  * sa_extract_pgroup(root, handle, pg, nodetype, proto, sectype)
2373034Sdougm  *
2384653Sdougm  * Extract the name property group and create the specified type of
2393034Sdougm  * node on the provided group.  type will be optionset or security.
2403034Sdougm  */
2413034Sdougm 
2423034Sdougm static int
2433034Sdougm sa_extract_pgroup(xmlNodePtr root, scfutilhandle_t *handle,
2443034Sdougm 			scf_propertygroup_t *pg,
2453034Sdougm 			char *nodetype, char *proto, char *sectype)
2463034Sdougm {
2473034Sdougm 	xmlNodePtr node;
2483034Sdougm 	scf_iter_t *iter;
2493034Sdougm 	scf_property_t *prop;
2503034Sdougm 	scf_value_t *value;
2513034Sdougm 	char *name;
2523034Sdougm 	char *valuestr;
2533034Sdougm 	ssize_t vallen;
2543034Sdougm 	int ret = SA_OK;
2553034Sdougm 
2563034Sdougm 	vallen = get_scf_limit(SCF_LIMIT_MAX_VALUE_LENGTH);
2573034Sdougm 
2583034Sdougm 	node = xmlNewChild(root, NULL, (xmlChar *)nodetype, NULL);
2594653Sdougm 	if (node == NULL)
2604653Sdougm 		return (ret);
2614653Sdougm 
2624653Sdougm 	if (proto != NULL)
2633034Sdougm 		xmlSetProp(node, (xmlChar *)"type", (xmlChar *)proto);
2644653Sdougm 	if (sectype != NULL)
2653034Sdougm 		xmlSetProp(node, (xmlChar *)"sectype", (xmlChar *)sectype);
2664653Sdougm 	/*
2674653Sdougm 	 * Have node to work with so iterate over the properties
2684653Sdougm 	 * in the pg and create option sub nodes.
2694653Sdougm 	 */
2704653Sdougm 	iter = scf_iter_create(handle->handle);
2714653Sdougm 	value = scf_value_create(handle->handle);
2724653Sdougm 	prop = scf_property_create(handle->handle);
2734653Sdougm 	name = malloc(scf_max_name_len);
2744653Sdougm 	valuestr = malloc(vallen);
2754653Sdougm 	/*
2764653Sdougm 	 * Want to iterate through the properties and add them
2774653Sdougm 	 * to the base optionset.
2784653Sdougm 	 */
2794653Sdougm 	if (iter == NULL || value == NULL || prop == NULL ||
2804653Sdougm 	    valuestr == NULL || name == NULL) {
2814653Sdougm 		ret = SA_NO_MEMORY;
2824653Sdougm 		goto out;
2834653Sdougm 	}
2844653Sdougm 	if (scf_iter_pg_properties(iter, pg) == 0) {
2854653Sdougm 		/* Now iterate the properties in the group */
2864653Sdougm 		while (scf_iter_next_property(iter, prop) > 0) {
2874653Sdougm 			/* have a property */
2884653Sdougm 			if (scf_property_get_name(prop, name,
2894653Sdougm 			    scf_max_name_len) > 0) {
2904653Sdougm 				sa_property_t saprop;
2914653Sdougm 				/* Some properties are part of the framework */
2923034Sdougm 				if (skip_property(name))
2934653Sdougm 					continue;
2944653Sdougm 				if (scf_property_get_value(prop, value) != 0)
2954653Sdougm 					continue;
2964653Sdougm 				if (scf_value_get_astring(value, valuestr,
2974653Sdougm 				    vallen) < 0)
2984653Sdougm 					continue;
2994653Sdougm 				saprop = sa_create_property(name, valuestr);
3004653Sdougm 				if (saprop != NULL) {
3013034Sdougm 					/*
3024653Sdougm 					 * Since in SMF, don't
3033034Sdougm 					 * recurse. Use xmlAddChild
3043034Sdougm 					 * directly, instead.
3053034Sdougm 					 */
3064653Sdougm 					xmlAddChild(node,
3074653Sdougm 					    (xmlNodePtr) saprop);
3083034Sdougm 				}
3093034Sdougm 			}
3103034Sdougm 		}
3113034Sdougm 	}
3124653Sdougm out:
3134653Sdougm 	/* cleanup to avoid memory leaks */
3144653Sdougm 	if (value != NULL)
3154653Sdougm 		scf_value_destroy(value);
3164653Sdougm 	if (iter != NULL)
3174653Sdougm 		scf_iter_destroy(iter);
3184653Sdougm 	if (prop != NULL)
3194653Sdougm 		scf_property_destroy(prop);
3204653Sdougm 	if (name != NULL)
3214653Sdougm 		free(name);
3224653Sdougm 	if (valuestr != NULL)
3234653Sdougm 		free(valuestr);
3244653Sdougm 
3253034Sdougm 	return (ret);
3263034Sdougm }
3273034Sdougm 
3283034Sdougm /*
3293034Sdougm  * sa_extract_attrs(root, handle, instance)
3303034Sdougm  *
3314653Sdougm  * Local function to extract the actual attributes/properties from the
3323034Sdougm  * property group of the service instance. These are the well known
3333034Sdougm  * attributes of "state" and "zfs". If additional attributes are
3343034Sdougm  * added, they should be added here.
3353034Sdougm  */
3363034Sdougm 
3373034Sdougm static void
3383034Sdougm sa_extract_attrs(xmlNodePtr root, scfutilhandle_t *handle,
3393034Sdougm 		    scf_instance_t *instance)
3403034Sdougm {
3413034Sdougm 	scf_property_t *prop;
3423034Sdougm 	scf_value_t *value;
3433034Sdougm 	char *valuestr;
3443034Sdougm 	ssize_t vallen;
3453034Sdougm 
3463034Sdougm 	vallen = get_scf_limit(SCF_LIMIT_MAX_VALUE_LENGTH);
3473034Sdougm 	prop = scf_property_create(handle->handle);
3483034Sdougm 	value = scf_value_create(handle->handle);
3493034Sdougm 	valuestr = malloc(vallen);
3504653Sdougm 	if (prop == NULL || value == NULL || valuestr == NULL ||
3514653Sdougm 	    scf_instance_get_pg(instance, "operation", handle->pg) != 0) {
3524653Sdougm 		goto out;
3534653Sdougm 	}
3544653Sdougm 	/*
3554653Sdougm 	 * Have a property group with desired name so now get
3564653Sdougm 	 * the known attributes.
3574653Sdougm 	 */
3584653Sdougm 	if (scf_pg_get_property(handle->pg, "state", prop) == 0) {
3594653Sdougm 		/* Found the property so get the value */
3603034Sdougm 		if (scf_property_get_value(prop, value) == 0) {
3614653Sdougm 			if (scf_value_get_astring(value, valuestr,
3624653Sdougm 			    vallen) >= 0) {
3634653Sdougm 				xmlSetProp(root, (xmlChar *)"state",
3643034Sdougm 				    (xmlChar *)valuestr);
3654653Sdougm 			}
3663034Sdougm 		}
3674653Sdougm 	}
3684653Sdougm 	if (scf_pg_get_property(handle->pg, "zfs", prop) == 0) {
3694653Sdougm 		/* Found the property so get the value */
3703034Sdougm 		if (scf_property_get_value(prop, value) == 0) {
3714653Sdougm 			if (scf_value_get_astring(value, valuestr,
3724653Sdougm 			    vallen) > 0) {
3734653Sdougm 				xmlSetProp(root, (xmlChar *)"zfs",
3743034Sdougm 				    (xmlChar *)valuestr);
3754653Sdougm 			}
3763034Sdougm 		}
3773034Sdougm 	}
3784653Sdougm out:
3793034Sdougm 	if (valuestr != NULL)
3804653Sdougm 		free(valuestr);
3813034Sdougm 	if (value != NULL)
3824653Sdougm 		scf_value_destroy(value);
3833034Sdougm 	if (prop != NULL)
3844653Sdougm 		scf_property_destroy(prop);
3853034Sdougm }
3863034Sdougm 
3873034Sdougm /*
3884653Sdougm  * List of known share attributes.
3893034Sdougm  */
3903034Sdougm 
3913034Sdougm static char *share_attr[] = {
3923034Sdougm 	"path",
3933034Sdougm 	"id",
3945331Samw 	"drive-letter",
3955331Samw 	"exclude",
3963034Sdougm 	NULL,
3973034Sdougm };
3983034Sdougm 
3993034Sdougm static int
4003034Sdougm is_share_attr(char *name)
4013034Sdougm {
4023034Sdougm 	int i;
4033034Sdougm 	for (i = 0; share_attr[i] != NULL; i++)
4044653Sdougm 		if (strcmp(name, share_attr[i]) == 0)
4054653Sdougm 			return (1);
4063034Sdougm 	return (0);
4073034Sdougm }
4083034Sdougm 
4093034Sdougm /*
4105331Samw  * _sa_make_resource(node, valuestr)
4115331Samw  *
4125331Samw  * Make a resource node on the share node. The valusestr will either
4135331Samw  * be old format (SMF acceptable string) or new format (pretty much an
4145331Samw  * arbitrary string with "nnn:" prefixing in order to persist
4155331Samw  * mapping). The input valuestr will get modified in place. This is
4165331Samw  * only used in SMF repository parsing. A possible third field will be
4175331Samw  * a "description" string.
4185331Samw  */
4195331Samw 
4205331Samw static void
4215331Samw _sa_make_resource(xmlNodePtr node, char *valuestr)
4225331Samw {
4235331Samw 	char *idx;
4245331Samw 	char *name;
4255331Samw 	char *description = NULL;
4265331Samw 
4275331Samw 	idx = valuestr;
4285331Samw 	name = strchr(valuestr, ':');
4295331Samw 	if (name == NULL) {
4305331Samw 		/* this is old form so give an index of "0" */
4315331Samw 		idx = "0";
4325331Samw 		name = valuestr;
4335331Samw 	} else {
4345331Samw 		/* NUL the ':' and move past it */
4355331Samw 		*name++ = '\0';
4365331Samw 		/* There could also be a description string */
4375331Samw 		description = strchr(name, ':');
4385331Samw 		if (description != NULL)
4395331Samw 			*description++ = '\0';
4405331Samw 	}
4415331Samw 	node = xmlNewChild(node, NULL, (xmlChar *)"resource", NULL);
4425331Samw 	if (node != NULL) {
4435331Samw 		xmlSetProp(node, (xmlChar *)"name", (xmlChar *)name);
4445331Samw 		xmlSetProp(node, (xmlChar *)"id", (xmlChar *)idx);
4455331Samw 		/* SMF values are always persistent */
4465331Samw 		xmlSetProp(node, (xmlChar *)"type", (xmlChar *)"persist");
4475331Samw 		if (description != NULL && strlen(description) > 0) {
4485331Samw 			(void) xmlNewChild(node, NULL, (xmlChar *)"description",
4495331Samw 			    (xmlChar *)description);
4505331Samw 		}
4515331Samw 	}
4525331Samw }
4535331Samw 
4545331Samw 
4555331Samw /*
4563034Sdougm  * sa_share_from_pgroup
4573034Sdougm  *
4584653Sdougm  * Extract the share definition from the share property group. We do
4593034Sdougm  * some sanity checking to avoid bad data.
4603034Sdougm  *
4613034Sdougm  * Since this is only constructing the internal data structures, we
4623034Sdougm  * don't use the sa_* functions most of the time.
4633034Sdougm  */
4643034Sdougm void
4653034Sdougm sa_share_from_pgroup(xmlNodePtr root, scfutilhandle_t *handle,
4663034Sdougm 			scf_propertygroup_t *pg, char *id)
4673034Sdougm {
4683034Sdougm 	xmlNodePtr node;
4693034Sdougm 	char *name;
4703034Sdougm 	scf_iter_t *iter;
4713034Sdougm 	scf_property_t *prop;
4723034Sdougm 	scf_value_t *value;
4733034Sdougm 	ssize_t vallen;
4743034Sdougm 	char *valuestr;
4753034Sdougm 	int ret = SA_OK;
4763348Sdougm 	int have_path = 0;
4773034Sdougm 
4783034Sdougm 	/*
4793034Sdougm 	 * While preliminary check (starts with 'S') passed before
4803034Sdougm 	 * getting here. Need to make sure it is in ID syntax
4813034Sdougm 	 * (Snnnnnn). Note that shares with properties have similar
4823034Sdougm 	 * pgroups.
4833034Sdougm 	 */
4843034Sdougm 	vallen = strlen(id);
4853034Sdougm 	if (*id == SA_SHARE_PG_PREFIX[0] && vallen == SA_SHARE_PG_LEN) {
4864653Sdougm 		uuid_t uuid;
4874653Sdougm 		if (strncmp(id, SA_SHARE_PG_PREFIX,
4884653Sdougm 		    SA_SHARE_PG_PREFIXLEN) != 0 ||
4894653Sdougm 		    uuid_parse(id + 2, uuid) < 0)
4904653Sdougm 			return;
4914653Sdougm 	} else {
4923034Sdougm 		return;
4933034Sdougm 	}
4943034Sdougm 
4953034Sdougm 	vallen = get_scf_limit(SCF_LIMIT_MAX_VALUE_LENGTH);
4963034Sdougm 
4973034Sdougm 	iter = scf_iter_create(handle->handle);
4983034Sdougm 	value = scf_value_create(handle->handle);
4993034Sdougm 	prop = scf_property_create(handle->handle);
5003034Sdougm 	name = malloc(scf_max_name_len);
5013034Sdougm 	valuestr = malloc(vallen);
5023034Sdougm 
5033034Sdougm 	/*
5044653Sdougm 	 * Construct the share XML node. It is similar to sa_add_share
5053034Sdougm 	 * but never changes the repository. Also, there won't be any
5063034Sdougm 	 * ZFS or transient shares.  Root will be the group it is
5073034Sdougm 	 * associated with.
5083034Sdougm 	 */
5093034Sdougm 	node = xmlNewChild(root, NULL, (xmlChar *)"share", NULL);
5103034Sdougm 	if (node != NULL) {
5113034Sdougm 		/*
5124653Sdougm 		 * Make sure the UUID part of the property group is
5133034Sdougm 		 * stored in the share "id" property. We use this
5143034Sdougm 		 * later.
5153034Sdougm 		 */
5164653Sdougm 		xmlSetProp(node, (xmlChar *)"id", (xmlChar *)id);
5174653Sdougm 		xmlSetProp(node, (xmlChar *)"type", (xmlChar *)"persist");
5183034Sdougm 	}
5193034Sdougm 
5204653Sdougm 	if (iter == NULL || value == NULL || prop == NULL || name == NULL)
5214653Sdougm 		goto out;
5224653Sdougm 
5234653Sdougm 	/* Iterate over the share pg properties */
5244653Sdougm 	if (scf_iter_pg_properties(iter, pg) != 0)
5254653Sdougm 		goto out;
5264653Sdougm 
5274653Sdougm 	while (scf_iter_next_property(iter, prop) > 0) {
5284653Sdougm 		ret = SA_SYSTEM_ERR; /* assume the worst */
5294653Sdougm 		if (scf_property_get_name(prop, name, scf_max_name_len) > 0) {
5303034Sdougm 			if (scf_property_get_value(prop, value) == 0) {
5314653Sdougm 				if (scf_value_get_astring(value, valuestr,
5324653Sdougm 				    vallen) >= 0) {
5334653Sdougm 					ret = SA_OK;
5344653Sdougm 				}
5355331Samw 			} else if (strcmp(name, "resource") == 0) {
5365331Samw 				ret = SA_OK;
5373034Sdougm 			}
5384653Sdougm 		}
5395331Samw 		if (ret != SA_OK)
5405331Samw 			continue;
5415331Samw 		/*
5425331Samw 		 * Check that we have the "path" property in
5435331Samw 		 * name. The string in name will always be nul
5445331Samw 		 * terminated if scf_property_get_name()
5455331Samw 		 * succeeded.
5465331Samw 		 */
5475331Samw 		if (strcmp(name, "path") == 0)
5485331Samw 			have_path = 1;
5495331Samw 		if (is_share_attr(name)) {
5503348Sdougm 			/*
5515331Samw 			 * If a share attr, then simple -
5525331Samw 			 * usually path and id name
5533348Sdougm 			 */
5545331Samw 			xmlSetProp(node, (xmlChar *)name,
5555331Samw 			    (xmlChar *)valuestr);
5565331Samw 		} else if (strcmp(name, "resource") == 0) {
5575331Samw 			/*
5585331Samw 			 * Resource names handled differently since
5595331Samw 			 * there can be multiple on each share. The
5605331Samw 			 * "resource" id must be preserved since this
5615331Samw 			 * will be used by some protocols in mapping
5625331Samw 			 * "property spaces" to names and is always
5635331Samw 			 * used to create SMF property groups specific
5645331Samw 			 * to resources.  CIFS needs this.  The first
5655331Samw 			 * value is present so add and then loop for
5665331Samw 			 * any additional. Since this is new and
5675331Samw 			 * previous values may exist, handle
5685331Samw 			 * conversions.
5695331Samw 			 */
5705331Samw 			scf_iter_t *viter;
5715331Samw 			viter = scf_iter_create(handle->handle);
5725331Samw 			if (viter != NULL &&
5735331Samw 			    scf_iter_property_values(viter, prop) == 0) {
5745331Samw 				while (scf_iter_next_value(viter, value) > 0) {
5755331Samw 					/* Have a value so process it */
5765331Samw 					if (scf_value_get_ustring(value,
5775331Samw 					    valuestr, vallen) >= 0) {
5785331Samw 						/* have a ustring */
5795331Samw 						_sa_make_resource(node,
5805331Samw 						    valuestr);
5815331Samw 					} else if (scf_value_get_astring(value,
5825331Samw 					    valuestr, vallen) >= 0) {
5835331Samw 						/* have an astring */
5845331Samw 						_sa_make_resource(node,
5855331Samw 						    valuestr);
5865331Samw 					}
5874653Sdougm 				}
5885331Samw 				scf_iter_destroy(viter);
5895331Samw 			}
5905331Samw 		} else {
5915331Samw 			if (strcmp(name, "description") == 0) {
5925331Samw 				/* We have a description node */
5935331Samw 				xmlNodePtr desc;
5945331Samw 				desc = xmlNewChild(node, NULL,
5955331Samw 				    (xmlChar *)"description", NULL);
5965331Samw 				if (desc != NULL)
5975331Samw 					xmlNodeSetContent(desc,
5985331Samw 					    (xmlChar *)valuestr);
5993034Sdougm 			}
6003034Sdougm 		}
6013034Sdougm 	}
6024653Sdougm out:
6033348Sdougm 	/*
6044653Sdougm 	 * A share without a path is broken so we want to not include
6053348Sdougm 	 * these.  They shouldn't happen but if you kill a sharemgr in
6063348Sdougm 	 * the process of creating a share, it could happen.  They
6073348Sdougm 	 * should be harmless.  It is also possible that another
6083348Sdougm 	 * sharemgr is running and in the process of creating a share.
6093348Sdougm 	 */
6103348Sdougm 	if (have_path == 0 && node != NULL) {
6114653Sdougm 		xmlUnlinkNode(node);
6124653Sdougm 		xmlFreeNode(node);
6133348Sdougm 	}
6143034Sdougm 	if (name != NULL)
6154653Sdougm 		free(name);
6163034Sdougm 	if (valuestr != NULL)
6174653Sdougm 		free(valuestr);
6183034Sdougm 	if (value != NULL)
6194653Sdougm 		scf_value_destroy(value);
6203034Sdougm 	if (iter != NULL)
6214653Sdougm 		scf_iter_destroy(iter);
6223034Sdougm 	if (prop != NULL)
6234653Sdougm 		scf_property_destroy(prop);
6243034Sdougm }
6253034Sdougm 
6263034Sdougm /*
6273034Sdougm  * find_share_by_id(shareid)
6283034Sdougm  *
6293034Sdougm  * Search all shares in all groups until we find the share represented
6303034Sdougm  * by "id".
6313034Sdougm  */
6323034Sdougm 
6333034Sdougm static sa_share_t
6343910Sdougm find_share_by_id(sa_handle_t handle, char *shareid)
6353034Sdougm {
6363034Sdougm 	sa_group_t group;
6373034Sdougm 	sa_share_t share = NULL;
6383034Sdougm 	char *id = NULL;
6393034Sdougm 	int done = 0;
6403034Sdougm 
6414653Sdougm 	for (group = sa_get_group(handle, NULL);
6424653Sdougm 	    group != NULL && !done;
6434653Sdougm 	    group = sa_get_next_group(group)) {
6444653Sdougm 		for (share = sa_get_share(group, NULL);
6454653Sdougm 		    share != NULL;
6464653Sdougm 		    share = sa_get_next_share(share)) {
6473034Sdougm 			id = sa_get_share_attr(share, "id");
6483034Sdougm 			if (id != NULL && strcmp(id, shareid) == 0) {
6493034Sdougm 				sa_free_attr_string(id);
6503034Sdougm 				id = NULL;
6513034Sdougm 				done++;
6523034Sdougm 				break;
6533034Sdougm 			}
6543034Sdougm 			if (id != NULL) {
6554653Sdougm 				sa_free_attr_string(id);
6564653Sdougm 				id = NULL;
6573034Sdougm 			}
6583034Sdougm 		}
6593034Sdougm 	}
6603034Sdougm 	return (share);
6613034Sdougm }
6623034Sdougm 
6633034Sdougm /*
6645331Samw  * find_resource_by_index(share, index)
6655331Samw  *
6665331Samw  * Search the resource records on the share for the id index.
6675331Samw  */
6685331Samw static sa_resource_t
6695331Samw find_resource_by_index(sa_share_t share, char *index)
6705331Samw {
6715331Samw 	sa_resource_t resource;
6725331Samw 	sa_resource_t found = NULL;
6735331Samw 	char *id;
6745331Samw 
6755331Samw 	for (resource = sa_get_share_resource(share, NULL);
6765331Samw 	    resource != NULL && found == NULL;
6775331Samw 	    resource = sa_get_next_resource(resource)) {
6785331Samw 		id = (char *)xmlGetProp((xmlNodePtr)resource, (xmlChar *)"id");
6795331Samw 		if (id != NULL) {
6805331Samw 			if (strcmp(id, index) == 0) {
6815331Samw 				/* found it so save in "found" */
6825331Samw 				found = resource;
6835331Samw 			}
6845331Samw 			sa_free_attr_string(id);
6855331Samw 		}
6865331Samw 	}
6875331Samw 	return (found);
6885331Samw }
6895331Samw 
6905331Samw /*
6915331Samw  * sa_share_props_from_pgroup(root, handle, pg, id, sahandle)
6923034Sdougm  *
6934653Sdougm  * Extract share properties from the SMF property group. More sanity
6943034Sdougm  * checks are done and the share object is created. We ignore some
6953034Sdougm  * errors that could exist in the repository and only worry about
6963034Sdougm  * property groups that validate in naming.
6973034Sdougm  */
6983034Sdougm 
6993034Sdougm static int
7003034Sdougm sa_share_props_from_pgroup(xmlNodePtr root, scfutilhandle_t *handle,
7013910Sdougm 			scf_propertygroup_t *pg, char *id, sa_handle_t sahandle)
7023034Sdougm {
7033034Sdougm 	xmlNodePtr node;
7044653Sdougm 	char *name = NULL;
7054653Sdougm 	scf_iter_t *iter = NULL;
7064653Sdougm 	scf_property_t *prop = NULL;
7074653Sdougm 	scf_value_t *value = NULL;
7083034Sdougm 	ssize_t vallen;
7094653Sdougm 	char *valuestr = NULL;
7103034Sdougm 	int ret = SA_OK;
7113034Sdougm 	char *sectype = NULL;
7123034Sdougm 	char *proto;
7133034Sdougm 	sa_share_t share;
7144653Sdougm 	uuid_t uuid;
7153034Sdougm 
7163034Sdougm 	/*
7173034Sdougm 	 * While preliminary check (starts with 'S') passed before
7183034Sdougm 	 * getting here. Need to make sure it is in ID syntax
7193034Sdougm 	 * (Snnnnnn). Note that shares with properties have similar
7203034Sdougm 	 * pgroups. If the pg name is more than SA_SHARE_PG_LEN
7213034Sdougm 	 * characters, it is likely one of the protocol/security
7223034Sdougm 	 * versions.
7233034Sdougm 	 */
7243034Sdougm 	vallen = strlen(id);
7254653Sdougm 	if (*id != SA_SHARE_PG_PREFIX[0] || vallen <= SA_SHARE_PG_LEN) {
7264653Sdougm 		/*
7274653Sdougm 		 * It is ok to not have what we thought since someone might
7284653Sdougm 		 * have added a name via SMF.
7294653Sdougm 		 */
7304653Sdougm 		return (ret);
7314653Sdougm 	}
7324653Sdougm 	if (strncmp(id, SA_SHARE_PG_PREFIX, SA_SHARE_PG_PREFIXLEN) == 0) {
7333034Sdougm 		proto = strchr(id, '_');
7343034Sdougm 		if (proto == NULL)
7354653Sdougm 			return (ret);
7363034Sdougm 		*proto++ = '\0';
7373034Sdougm 		if (uuid_parse(id + SA_SHARE_PG_PREFIXLEN, uuid) < 0)
7384653Sdougm 			return (ret);
7393034Sdougm 		/*
7403034Sdougm 		 * probably a legal optionset so check a few more
7413034Sdougm 		 * syntax points below.
7423034Sdougm 		 */
7433034Sdougm 		if (*proto == '\0') {
7444653Sdougm 			/* not a valid proto (null) */
7454653Sdougm 			return (ret);
7463034Sdougm 		}
7475331Samw 
7483034Sdougm 		sectype = strchr(proto, '_');
7493034Sdougm 		if (sectype != NULL)
7504653Sdougm 			*sectype++ = '\0';
7513034Sdougm 		if (!valid_protocol(proto))
7524653Sdougm 			return (ret);
7533034Sdougm 	}
7543034Sdougm 
7553034Sdougm 	/*
7564653Sdougm 	 * To get here, we have a valid protocol and possibly a
7573034Sdougm 	 * security. We now have to find the share that it is really
7583034Sdougm 	 * associated with. The "id" portion of the pgroup name will
7593034Sdougm 	 * match.
7603034Sdougm 	 */
7613034Sdougm 
7623910Sdougm 	share = find_share_by_id(sahandle, id);
7633034Sdougm 	if (share == NULL)
7644653Sdougm 		return (SA_BAD_PATH);
7653034Sdougm 
7663034Sdougm 	root = (xmlNodePtr)share;
7673034Sdougm 
7683034Sdougm 	vallen = get_scf_limit(SCF_LIMIT_MAX_VALUE_LENGTH);
7693034Sdougm 
7704653Sdougm 	if (sectype == NULL)
7714653Sdougm 		node = xmlNewChild(root, NULL, (xmlChar *)"optionset", NULL);
7724653Sdougm 	else {
7735331Samw 		if (isdigit((int)*sectype)) {
7745331Samw 			sa_resource_t resource;
7755331Samw 			/*
7765331Samw 			 * If sectype[0] is a digit, then it is an index into
7775331Samw 			 * the resource names. We need to find a resource
7785331Samw 			 * record and then get the properties into an
7795331Samw 			 * optionset. The optionset becomes the "node" and the
7805331Samw 			 * rest is hung off of the share.
7815331Samw 			 */
7825331Samw 			resource = find_resource_by_index(share, sectype);
7835331Samw 			if (resource != NULL) {
7845331Samw 				node = xmlNewChild(resource, NULL,
7855331Samw 				    (xmlChar *)"optionset", NULL);
7865331Samw 			} else {
7875521Sas200622 				/* This shouldn't happen. */
7885331Samw 				ret = SA_SYSTEM_ERR;
7895521Sas200622 				goto out;
7905331Samw 			}
7915331Samw 		} else {
7925331Samw 			/*
7935331Samw 			 * If not a digit, then it is a security type
7945331Samw 			 * (alternate option space). Security types start with
7955331Samw 			 * an alphabetic.
7965331Samw 			 */
7975331Samw 			node = xmlNewChild(root, NULL, (xmlChar *)"security",
7985331Samw 			    NULL);
7995331Samw 			if (node != NULL)
8005331Samw 				xmlSetProp(node, (xmlChar *)"sectype",
8015331Samw 				    (xmlChar *)sectype);
8025331Samw 		}
8034653Sdougm 	}
8044653Sdougm 	if (node == NULL) {
8054653Sdougm 		ret = SA_NO_MEMORY;
8064653Sdougm 		goto out;
8074653Sdougm 	}
8084653Sdougm 
8094653Sdougm 	xmlSetProp(node, (xmlChar *)"type", (xmlChar *)proto);
8104653Sdougm 	/* now find the properties */
8113034Sdougm 	iter = scf_iter_create(handle->handle);
8123034Sdougm 	value = scf_value_create(handle->handle);
8133034Sdougm 	prop = scf_property_create(handle->handle);
8143034Sdougm 	name = malloc(scf_max_name_len);
8153034Sdougm 	valuestr = malloc(vallen);
8163034Sdougm 
8174653Sdougm 	if (iter == NULL || value == NULL || prop == NULL || name == NULL)
8184653Sdougm 		goto out;
8194653Sdougm 
8205331Samw 	/* iterate over the share pg properties */
8214653Sdougm 	if (scf_iter_pg_properties(iter, pg) == 0) {
8224653Sdougm 		while (scf_iter_next_property(iter, prop) > 0) {
8233034Sdougm 			ret = SA_SYSTEM_ERR; /* assume the worst */
8243034Sdougm 			if (scf_property_get_name(prop, name,
8254653Sdougm 			    scf_max_name_len) > 0) {
8264653Sdougm 				if (scf_property_get_value(prop, value) == 0) {
8274653Sdougm 					if (scf_value_get_astring(value,
8284653Sdougm 					    valuestr, vallen) >= 0) {
8294653Sdougm 						ret = SA_OK;
8304653Sdougm 					}
8313034Sdougm 				}
8323034Sdougm 			} else {
8334653Sdougm 				ret = SA_SYSTEM_ERR;
8343034Sdougm 			}
8353034Sdougm 			if (ret == SA_OK) {
8364653Sdougm 				sa_property_t prop;
8374653Sdougm 				prop = sa_create_property(name, valuestr);
8384653Sdougm 				if (prop != NULL)
8394653Sdougm 					prop = (sa_property_t)xmlAddChild(node,
8404653Sdougm 					    (xmlNodePtr)prop);
8414653Sdougm 				else
8424653Sdougm 					ret = SA_NO_MEMORY;
8433034Sdougm 			}
8443034Sdougm 		}
8453034Sdougm 	} else {
8464653Sdougm 		ret = SA_SYSTEM_ERR;
8473034Sdougm 	}
8484653Sdougm out:
8493034Sdougm 	if (iter != NULL)
8504653Sdougm 		scf_iter_destroy(iter);
8513034Sdougm 	if (value != NULL)
8524653Sdougm 		scf_value_destroy(value);
8533034Sdougm 	if (prop != NULL)
8544653Sdougm 		scf_property_destroy(prop);
8553034Sdougm 	if (name != NULL)
8564653Sdougm 		free(name);
8573034Sdougm 	if (valuestr != NULL)
8584653Sdougm 		free(valuestr);
8593034Sdougm 	return (ret);
8603034Sdougm }
8613034Sdougm 
8623034Sdougm /*
8633034Sdougm  * sa_extract_group(root, handle, instance)
8643034Sdougm  *
8654653Sdougm  * Get the config info for this instance of a group and create the XML
8663034Sdougm  * subtree from it.
8673034Sdougm  */
8683034Sdougm 
8693034Sdougm static int
8703034Sdougm sa_extract_group(xmlNodePtr root, scfutilhandle_t *handle,
8715951Sdougm     scf_instance_t *instance, sa_handle_t sahandle)
8723034Sdougm {
8733034Sdougm 	char *buff;
8743034Sdougm 	xmlNodePtr node;
8753034Sdougm 	scf_iter_t *iter;
8763034Sdougm 	char *proto;
8773034Sdougm 	char *sectype;
878*5997Sdougm 	boolean_t have_shares = B_FALSE;
879*5997Sdougm 	boolean_t is_default = B_FALSE;
880*5997Sdougm 	boolean_t is_nfs = B_FALSE;
8813034Sdougm 	int ret = SA_OK;
8823034Sdougm 	int err;
8833034Sdougm 
8843034Sdougm 	buff = malloc(scf_max_name_len);
8854653Sdougm 	if (buff == NULL)
8864653Sdougm 		return (SA_NO_MEMORY);
8874653Sdougm 
8883034Sdougm 	iter = scf_iter_create(handle->handle);
8894653Sdougm 	if (iter == NULL) {
8904653Sdougm 		ret = SA_NO_MEMORY;
8914653Sdougm 		goto out;
8924653Sdougm 	}
8934653Sdougm 
8944653Sdougm 	if (scf_instance_get_name(instance, buff, scf_max_name_len) > 0) {
8953034Sdougm 		node = xmlNewChild(root, NULL, (xmlChar *)"group", NULL);
8964653Sdougm 		if (node == NULL) {
8974653Sdougm 			ret = SA_NO_MEMORY;
8984653Sdougm 			goto out;
8994653Sdougm 		}
9004653Sdougm 		xmlSetProp(node, (xmlChar *)"name", (xmlChar *)buff);
9014653Sdougm 		if (strcmp(buff, "default") == 0)
902*5997Sdougm 			is_default = B_TRUE;
9034653Sdougm 
9044653Sdougm 		sa_extract_attrs(node, handle, instance);
9054653Sdougm 		/*
9064653Sdougm 		 * Iterate through all the property groups
9074653Sdougm 		 * looking for those with security or
9084653Sdougm 		 * optionset prefixes. The names of the
9094653Sdougm 		 * matching pgroups are parsed to get the
9104653Sdougm 		 * protocol, and for security, the sectype.
9114653Sdougm 		 * Syntax is as follows:
9124653Sdougm 		 *    optionset | optionset_<proto>
9134653Sdougm 		 *    security_default | security_<proto>_<sectype>
9144653Sdougm 		 * "operation" is handled by
9154653Sdougm 		 * sa_extract_attrs().
9164653Sdougm 		 */
9174653Sdougm 		if (scf_iter_instance_pgs(iter, instance) != 0) {
9184653Sdougm 			ret = SA_NO_MEMORY;
9194653Sdougm 			goto out;
9204653Sdougm 		}
9214653Sdougm 		while (scf_iter_next_pg(iter, handle->pg) > 0) {
9224653Sdougm 			/* Have a pgroup so sort it out */
9234653Sdougm 			ret = scf_pg_get_name(handle->pg, buff,
9244653Sdougm 			    scf_max_name_len);
925*5997Sdougm 			if (ret <= 0)
926*5997Sdougm 				continue;
927*5997Sdougm 			is_nfs = B_FALSE;
928*5997Sdougm 
929*5997Sdougm 			if (buff[0] == SA_SHARE_PG_PREFIX[0]) {
930*5997Sdougm 				sa_share_from_pgroup(node, handle,
931*5997Sdougm 				    handle->pg, buff);
932*5997Sdougm 				have_shares = B_TRUE;
933*5997Sdougm 			} else if (strncmp(buff, "optionset", 9) == 0) {
934*5997Sdougm 				char *nodetype = "optionset";
935*5997Sdougm 				/* Have an optionset */
936*5997Sdougm 				sectype = NULL;
937*5997Sdougm 				proto = strchr(buff, '_');
938*5997Sdougm 				if (proto != NULL) {
939*5997Sdougm 					*proto++ = '\0';
940*5997Sdougm 					sectype = strchr(proto, '_');
941*5997Sdougm 					if (sectype != NULL) {
942*5997Sdougm 						*sectype++ = '\0';
943*5997Sdougm 						nodetype = "security";
9443034Sdougm 					}
945*5997Sdougm 					is_nfs = strcmp(proto, "nfs") == 0;
946*5997Sdougm 				} else if (strlen(buff) > 9) {
947*5997Sdougm 					/*
948*5997Sdougm 					 * This can only occur if
949*5997Sdougm 					 * someone has made changes
950*5997Sdougm 					 * via an SMF command. Since
951*5997Sdougm 					 * this would be an unknown
952*5997Sdougm 					 * syntax, we just ignore it.
953*5997Sdougm 					 */
954*5997Sdougm 					continue;
955*5997Sdougm 				}
956*5997Sdougm 				/*
957*5997Sdougm 				 * If the group is not "default" or is
958*5997Sdougm 				 * "default" and is_nfs, then extract the
959*5997Sdougm 				 * pgroup.  If it is_default and !is_nfs,
960*5997Sdougm 				 * then we have an error and should remove
961*5997Sdougm 				 * the extraneous protocols.  We don't care
962*5997Sdougm 				 * about errors on scf_pg_delete since we
963*5997Sdougm 				 * might not have permission during an
964*5997Sdougm 				 * extract only.
965*5997Sdougm 				 */
966*5997Sdougm 				if (!is_default || is_nfs) {
9673034Sdougm 					ret = sa_extract_pgroup(node, handle,
9684653Sdougm 					    handle->pg, nodetype, proto,
9694653Sdougm 					    sectype);
970*5997Sdougm 				} else {
971*5997Sdougm 					err = scf_pg_delete(handle->pg);
972*5997Sdougm 					if (err == 0)
973*5997Sdougm 						(void) fprintf(stderr,
974*5997Sdougm 						    dgettext(TEXT_DOMAIN,
975*5997Sdougm 						    "Removed protocol \"%s\" "
976*5997Sdougm 						    "from group \"default\"\n"),
977*5997Sdougm 						    proto);
9783034Sdougm 				}
979*5997Sdougm 			} else if (strncmp(buff, "security", 8) == 0) {
980*5997Sdougm 				/*
981*5997Sdougm 				 * Have a security (note that
982*5997Sdougm 				 * this should change in the
983*5997Sdougm 				 * future)
984*5997Sdougm 				 */
985*5997Sdougm 				proto = strchr(buff, '_');
986*5997Sdougm 				sectype = NULL;
987*5997Sdougm 				if (proto != NULL) {
988*5997Sdougm 					*proto++ = '\0';
989*5997Sdougm 					sectype = strchr(proto, '_');
990*5997Sdougm 					if (sectype != NULL)
991*5997Sdougm 						*sectype++ = '\0';
992*5997Sdougm 					if (strcmp(proto, "default") == 0)
993*5997Sdougm 						proto = NULL;
994*5997Sdougm 				}
995*5997Sdougm 				ret = sa_extract_pgroup(node, handle,
996*5997Sdougm 				    handle->pg, "security", proto, sectype);
9973034Sdougm 			}
998*5997Sdougm 			/* Ignore everything else */
9994653Sdougm 		}
10004653Sdougm 		/*
10014653Sdougm 		 * Make sure we have a valid default group.
10024653Sdougm 		 * On first boot, default won't have any
10034653Sdougm 		 * protocols defined and won't be enabled (but
1004*5997Sdougm 		 * should be).  "default" only has NFS enabled on it.
10054653Sdougm 		 */
10064653Sdougm 		if (is_default) {
10074653Sdougm 			char *state = sa_get_group_attr((sa_group_t)node,
10084653Sdougm 			    "state");
10093034Sdougm 
10104653Sdougm 			if (state == NULL) {
10113034Sdougm 				/* set attribute to enabled */
10123034Sdougm 				(void) sa_set_group_attr((sa_group_t)node,
10134653Sdougm 				    "state", "enabled");
1014*5997Sdougm 				(void) sa_create_optionset((sa_group_t)node,
1015*5997Sdougm 				    "nfs");
10164653Sdougm 			} else {
10173034Sdougm 				sa_free_attr_string(state);
10183034Sdougm 			}
10194653Sdougm 		}
10204653Sdougm 		/* Do a second pass if shares were found */
10214653Sdougm 		if (have_shares && scf_iter_instance_pgs(iter, instance) == 0) {
10224653Sdougm 			while (scf_iter_next_pg(iter, handle->pg) > 0) {
10233034Sdougm 				/*
10244653Sdougm 				 * Have a pgroup so see if it is a
10253034Sdougm 				 * share optionset
10263034Sdougm 				 */
10273034Sdougm 				err = scf_pg_get_name(handle->pg, buff,
10284653Sdougm 				    scf_max_name_len);
10294653Sdougm 				if (err  <= 0)
10304653Sdougm 					continue;
10314653Sdougm 				if (buff[0] == SA_SHARE_PG_PREFIX[0]) {
10323034Sdougm 					ret = sa_share_props_from_pgroup(node,
10334653Sdougm 					    handle, handle->pg, buff,
10344653Sdougm 					    sahandle);
10353034Sdougm 				}
10363034Sdougm 			}
10373034Sdougm 		}
10383034Sdougm 	}
10394653Sdougm out:
10403034Sdougm 	if (iter != NULL)
10414653Sdougm 		scf_iter_destroy(iter);
10423034Sdougm 	if (buff != NULL)
10434653Sdougm 		free(buff);
10443034Sdougm 	return (ret);
10453034Sdougm }
10463034Sdougm 
10473034Sdougm /*
10483034Sdougm  * sa_extract_defaults(root, handle, instance)
10493034Sdougm  *
10504653Sdougm  * Local function to find the default properties that live in the
10515331Samw  * default instance's "operation" property group.
10523034Sdougm  */
10533034Sdougm 
10543034Sdougm static void
10553034Sdougm sa_extract_defaults(xmlNodePtr root, scfutilhandle_t *handle,
10563034Sdougm 		    scf_instance_t *instance)
10573034Sdougm {
10583034Sdougm 	xmlNodePtr node;
10593034Sdougm 	scf_property_t *prop;
10603034Sdougm 	scf_value_t *value;
10613034Sdougm 	char *valuestr;
10623034Sdougm 	ssize_t vallen;
10633034Sdougm 
10643034Sdougm 	vallen = get_scf_limit(SCF_LIMIT_MAX_VALUE_LENGTH);
10653034Sdougm 	prop = scf_property_create(handle->handle);
10663034Sdougm 	value = scf_value_create(handle->handle);
10673034Sdougm 	valuestr = malloc(vallen);
10684653Sdougm 
10694653Sdougm 	if (prop == NULL || value == NULL || vallen == 0 ||
10704653Sdougm 	    scf_instance_get_pg(instance, "operation", handle->pg) != 0)
10714653Sdougm 		goto out;
10724653Sdougm 
10734653Sdougm 	if (scf_pg_get_property(handle->pg, "legacy-timestamp", prop) != 0)
10744653Sdougm 		goto out;
10754653Sdougm 
10764653Sdougm 	/* Found the property so get the value */
10774653Sdougm 	if (scf_property_get_value(prop, value) == 0) {
10784653Sdougm 		if (scf_value_get_astring(value, valuestr, vallen) > 0) {
10793034Sdougm 			node = xmlNewChild(root, NULL, (xmlChar *)"legacy",
10804653Sdougm 			    NULL);
10813034Sdougm 			if (node != NULL) {
10824653Sdougm 				xmlSetProp(node, (xmlChar *)"timestamp",
10834653Sdougm 				    (xmlChar *)valuestr);
10844653Sdougm 				xmlSetProp(node, (xmlChar *)"path",
10854653Sdougm 				    (xmlChar *)SA_LEGACY_DFSTAB);
10863034Sdougm 			}
10873034Sdougm 		}
10883034Sdougm 	}
10894653Sdougm out:
10903034Sdougm 	if (valuestr != NULL)
10914653Sdougm 		free(valuestr);
10923034Sdougm 	if (value != NULL)
10934653Sdougm 		scf_value_destroy(value);
10943034Sdougm 	if (prop != NULL)
10954653Sdougm 		scf_property_destroy(prop);
10963034Sdougm }
10973034Sdougm 
10983034Sdougm 
10993034Sdougm /*
11005331Samw  * sa_get_config(handle, root, doc, sahandle)
11013034Sdougm  *
11024653Sdougm  * Walk the SMF repository for /network/shares/group and find all the
11033034Sdougm  * instances. These become group names.  Then add the XML structure
11043034Sdougm  * below the groups based on property groups and properties.
11053034Sdougm  */
11063034Sdougm int
11073973Sdougm sa_get_config(scfutilhandle_t *handle, xmlNodePtr root, sa_handle_t sahandle)
11083034Sdougm {
11093034Sdougm 	int ret = SA_OK;
11103034Sdougm 	scf_instance_t *instance;
11113034Sdougm 	scf_iter_t *iter;
11123034Sdougm 	char buff[BUFSIZ * 2];
11133034Sdougm 
11143034Sdougm 	instance = scf_instance_create(handle->handle);
11153034Sdougm 	iter = scf_iter_create(handle->handle);
11163973Sdougm 	if (instance != NULL && iter != NULL) {
11174653Sdougm 		if ((ret = scf_iter_service_instances(iter,
11184653Sdougm 		    handle->service)) == 0) {
11194653Sdougm 			while ((ret = scf_iter_next_instance(iter,
11204653Sdougm 			    instance)) > 0) {
11214653Sdougm 				if (scf_instance_get_name(instance, buff,
11224653Sdougm 				    sizeof (buff)) > 0) {
11234653Sdougm 					if (strcmp(buff, "default") == 0)
11244653Sdougm 						sa_extract_defaults(root,
11254653Sdougm 						    handle, instance);
11264653Sdougm 					ret = sa_extract_group(root, handle,
11274653Sdougm 					    instance, sahandle);
11284653Sdougm 				}
11294653Sdougm 			}
11303034Sdougm 		}
11313034Sdougm 	}
11323973Sdougm 
11334653Sdougm 	/* Always cleanup these */
11343034Sdougm 	if (instance != NULL)
11354653Sdougm 		scf_instance_destroy(instance);
11363034Sdougm 	if (iter != NULL)
11374653Sdougm 		scf_iter_destroy(iter);
11383034Sdougm 	return (ret);
11393034Sdougm }
11403034Sdougm 
11413034Sdougm /*
11423034Sdougm  * sa_get_instance(handle, instance)
11433034Sdougm  *
11444653Sdougm  * Get the instance of the group service. This is actually the
11453034Sdougm  * specific group name. The instance is needed for all property and
11463034Sdougm  * control operations.
11473034Sdougm  */
11483034Sdougm 
11493034Sdougm int
11503034Sdougm sa_get_instance(scfutilhandle_t *handle, char *instname)
11513034Sdougm {
11523034Sdougm 	if (scf_service_get_instance(handle->service, instname,
11534653Sdougm 	    handle->instance) != 0) {
11544653Sdougm 		return (SA_NO_SUCH_GROUP);
11553034Sdougm 	}
11563034Sdougm 	return (SA_OK);
11573034Sdougm }
11583034Sdougm 
11593034Sdougm /*
11603034Sdougm  * sa_create_instance(handle, instname)
11613034Sdougm  *
11623034Sdougm  * Create a new SMF service instance. There can only be one with a
11633034Sdougm  * given name.
11643034Sdougm  */
11653034Sdougm 
11663034Sdougm int
11673034Sdougm sa_create_instance(scfutilhandle_t *handle, char *instname)
11683034Sdougm {
11693034Sdougm 	int ret = SA_OK;
11703034Sdougm 	char instance[SA_GROUP_INST_LEN];
11713034Sdougm 	if (scf_service_add_instance(handle->service, instname,
11724653Sdougm 	    handle->instance) != 0) {
11733034Sdougm 	/* better error returns need to be added based on real error */
11744653Sdougm 		if (scf_error() == SCF_ERROR_PERMISSION_DENIED)
11754653Sdougm 			ret = SA_NO_PERMISSION;
11764653Sdougm 		else
11774653Sdougm 			ret = SA_DUPLICATE_NAME;
11783034Sdougm 	} else {
11794653Sdougm 		/* have the service created, so enable it */
11804653Sdougm 		(void) snprintf(instance, sizeof (instance), "%s:%s",
11814653Sdougm 		    SA_SVC_FMRI_BASE, instname);
11824653Sdougm 		(void) smf_enable_instance(instance, 0);
11833034Sdougm 	}
11843034Sdougm 	return (ret);
11853034Sdougm }
11863034Sdougm 
11873034Sdougm /*
11883034Sdougm  * sa_delete_instance(handle, instname)
11893034Sdougm  *
11903034Sdougm  * When a group goes away, we also remove the service instance.
11913034Sdougm  */
11923034Sdougm 
11933034Sdougm int
11943034Sdougm sa_delete_instance(scfutilhandle_t *handle, char *instname)
11953034Sdougm {
11963034Sdougm 	int ret;
11973034Sdougm 
11983034Sdougm 	if (strcmp(instname, "default") == 0) {
11994653Sdougm 		ret = SA_NO_PERMISSION;
12003034Sdougm 	} else {
12014653Sdougm 		if ((ret = sa_get_instance(handle, instname)) == SA_OK) {
12024653Sdougm 			if (scf_instance_delete(handle->instance) != 0)
12034653Sdougm 				/* need better analysis */
12044653Sdougm 				ret = SA_NO_PERMISSION;
12054653Sdougm 		}
12063034Sdougm 	}
12073034Sdougm 	return (ret);
12083034Sdougm }
12093034Sdougm 
12103034Sdougm /*
12113034Sdougm  * sa_create_pgroup(handle, pgroup)
12123034Sdougm  *
12133034Sdougm  * create a new property group
12143034Sdougm  */
12153034Sdougm 
12163034Sdougm int
12173034Sdougm sa_create_pgroup(scfutilhandle_t *handle, char *pgroup)
12183034Sdougm {
12193034Sdougm 	int ret = SA_OK;
12205951Sdougm 	int persist = 0;
12215951Sdougm 
12223034Sdougm 	/*
12234653Sdougm 	 * Only create a handle if it doesn't exist. It is ok to exist
12243034Sdougm 	 * since the pg handle will be set as a side effect.
12253034Sdougm 	 */
12264653Sdougm 	if (handle->pg == NULL)
12274653Sdougm 		handle->pg = scf_pg_create(handle->handle);
12284653Sdougm 
12293034Sdougm 	/*
12305951Sdougm 	 * Special case for a non-persistent property group. This is
12315951Sdougm 	 * internal use only.
12325951Sdougm 	 */
12335951Sdougm 	if (*pgroup == '*') {
12345951Sdougm 		persist = SCF_PG_FLAG_NONPERSISTENT;
12355951Sdougm 		pgroup++;
12365951Sdougm 	}
12375951Sdougm 
12385951Sdougm 	/*
12394653Sdougm 	 * If the pgroup exists, we are done. If it doesn't, then we
12403034Sdougm 	 * need to actually add one to the service instance.
12413034Sdougm 	 */
12423034Sdougm 	if (scf_instance_get_pg(handle->instance,
12434653Sdougm 	    pgroup, handle->pg) != 0) {
12445951Sdougm 
12454653Sdougm 		/* Doesn't exist so create one */
12464653Sdougm 		if (scf_instance_add_pg(handle->instance, pgroup,
12475951Sdougm 		    SCF_GROUP_APPLICATION, persist, handle->pg) != 0) {
12484653Sdougm 			switch (scf_error()) {
12494653Sdougm 			case SCF_ERROR_PERMISSION_DENIED:
12504653Sdougm 				ret = SA_NO_PERMISSION;
12514653Sdougm 				break;
12524653Sdougm 			default:
12534653Sdougm 				ret = SA_SYSTEM_ERR;
12544653Sdougm 				break;
12554653Sdougm 			}
12563034Sdougm 		}
12573034Sdougm 	}
12583034Sdougm 	return (ret);
12593034Sdougm }
12603034Sdougm 
12613034Sdougm /*
12623034Sdougm  * sa_delete_pgroup(handle, pgroup)
12633034Sdougm  *
12644653Sdougm  * Remove the property group from the current instance of the service,
12653034Sdougm  * but only if it actually exists.
12663034Sdougm  */
12673034Sdougm 
12683034Sdougm int
12693034Sdougm sa_delete_pgroup(scfutilhandle_t *handle, char *pgroup)
12703034Sdougm {
12713034Sdougm 	int ret = SA_OK;
12723034Sdougm 	/*
12734653Sdougm 	 * Only delete if it does exist.
12743034Sdougm 	 */
12754653Sdougm 	if (scf_instance_get_pg(handle->instance, pgroup, handle->pg) == 0) {
12764653Sdougm 		/* does exist so delete it */
12774653Sdougm 		if (scf_pg_delete(handle->pg) != 0)
12784653Sdougm 			ret = SA_SYSTEM_ERR;
12794653Sdougm 	} else {
12803034Sdougm 		ret = SA_SYSTEM_ERR;
12813034Sdougm 	}
12823034Sdougm 	if (ret == SA_SYSTEM_ERR &&
12833034Sdougm 	    scf_error() == SCF_ERROR_PERMISSION_DENIED) {
12843034Sdougm 		ret = SA_NO_PERMISSION;
12853034Sdougm 	}
12863034Sdougm 	return (ret);
12873034Sdougm }
12883034Sdougm 
12893034Sdougm /*
12903034Sdougm  * sa_start_transaction(handle, pgroup)
12913034Sdougm  *
12923034Sdougm  * Start an SMF transaction so we can deal with properties. it would
12933034Sdougm  * be nice to not have to expose this, but we have to in order to
12943034Sdougm  * optimize.
12953034Sdougm  *
12963034Sdougm  * Basic model is to hold the transaction in the handle and allow
12973034Sdougm  * property adds/deletes/updates to be added then close the
12983034Sdougm  * transaction (or abort).  There may eventually be a need to handle
12993034Sdougm  * other types of transaction mechanisms but we don't do that now.
13003034Sdougm  *
13013034Sdougm  * An sa_start_transaction must be followed by either an
13023034Sdougm  * sa_end_transaction or sa_abort_transaction before another
13033034Sdougm  * sa_start_transaction can be done.
13043034Sdougm  */
13053034Sdougm 
13063034Sdougm int
13073034Sdougm sa_start_transaction(scfutilhandle_t *handle, char *propgroup)
13083034Sdougm {
13093034Sdougm 	int ret = SA_OK;
13103034Sdougm 	/*
13114653Sdougm 	 * Lookup the property group and create it if it doesn't already
13123034Sdougm 	 * exist.
13133034Sdougm 	 */
13145951Sdougm 	if (handle == NULL)
13155951Sdougm 		return (SA_CONFIG_ERR);
13165951Sdougm 
13173034Sdougm 	if (handle->scf_state == SCH_STATE_INIT) {
13184653Sdougm 		ret = sa_create_pgroup(handle, propgroup);
13194653Sdougm 		if (ret == SA_OK) {
13204653Sdougm 			handle->trans = scf_transaction_create(handle->handle);
13214653Sdougm 			if (handle->trans != NULL) {
13224653Sdougm 				if (scf_transaction_start(handle->trans,
13234653Sdougm 				    handle->pg) != 0) {
13244653Sdougm 					ret = SA_SYSTEM_ERR;
13254653Sdougm 				}
13264653Sdougm 				if (ret != SA_OK) {
13274653Sdougm 					scf_transaction_destroy(handle->trans);
13284653Sdougm 					handle->trans = NULL;
13294653Sdougm 				}
13304653Sdougm 			} else {
13314653Sdougm 				ret = SA_SYSTEM_ERR;
13324653Sdougm 			}
13333034Sdougm 		}
13343034Sdougm 	}
13353034Sdougm 	if (ret == SA_SYSTEM_ERR &&
13363034Sdougm 	    scf_error() == SCF_ERROR_PERMISSION_DENIED) {
13373034Sdougm 		ret = SA_NO_PERMISSION;
13383034Sdougm 	}
13393034Sdougm 	return (ret);
13403034Sdougm }
13413034Sdougm 
13425951Sdougm 
13433034Sdougm /*
13445951Sdougm  * sa_end_transaction(scfhandle, sahandle)
13453034Sdougm  *
13463034Sdougm  * Commit the changes that were added to the transaction in the
13473034Sdougm  * handle. Do all necessary cleanup.
13483034Sdougm  */
13493034Sdougm 
13503034Sdougm int
13515951Sdougm sa_end_transaction(scfutilhandle_t *handle, sa_handle_impl_t sahandle)
13523034Sdougm {
13533034Sdougm 	int ret = SA_OK;
13543034Sdougm 
13555951Sdougm 	if (handle == NULL || handle->trans == NULL || sahandle == NULL) {
13564653Sdougm 		ret = SA_SYSTEM_ERR;
13573034Sdougm 	} else {
13584653Sdougm 		if (scf_transaction_commit(handle->trans) < 0)
13594653Sdougm 			ret = SA_SYSTEM_ERR;
13604653Sdougm 		scf_transaction_destroy_children(handle->trans);
13614653Sdougm 		scf_transaction_destroy(handle->trans);
13625951Sdougm 		if (ret == SA_OK)
13635951Sdougm 			set_transaction_tstamp(sahandle);
13644653Sdougm 		handle->trans = NULL;
13653034Sdougm 	}
13663034Sdougm 	return (ret);
13673034Sdougm }
13683034Sdougm 
13693034Sdougm /*
13703034Sdougm  * sa_abort_transaction(handle)
13713034Sdougm  *
13723034Sdougm  * Abort the changes that were added to the transaction in the
13733034Sdougm  * handle. Do all necessary cleanup.
13743034Sdougm  */
13753034Sdougm 
13763034Sdougm void
13773034Sdougm sa_abort_transaction(scfutilhandle_t *handle)
13783034Sdougm {
13793034Sdougm 	if (handle->trans != NULL) {
13804653Sdougm 		scf_transaction_reset_all(handle->trans);
13814653Sdougm 		scf_transaction_destroy_children(handle->trans);
13824653Sdougm 		scf_transaction_destroy(handle->trans);
13834653Sdougm 		handle->trans = NULL;
13843034Sdougm 	}
13853034Sdougm }
13863034Sdougm 
13873034Sdougm /*
13885951Sdougm  * set_transaction_tstamp(sahandle)
13895951Sdougm  *
13905951Sdougm  * After a successful transaction commit, update the timestamp of the
13915951Sdougm  * last transaction. This lets us detect changes from other processes.
13925951Sdougm  */
13935951Sdougm static void
13945951Sdougm set_transaction_tstamp(sa_handle_impl_t sahandle)
13955951Sdougm {
13965951Sdougm 	char tstring[32];
13975951Sdougm 	struct timeval tv;
13985951Sdougm 	scfutilhandle_t *scfhandle;
13995951Sdougm 
14005951Sdougm 	if (sahandle == NULL || sahandle->scfhandle == NULL)
14015951Sdougm 		return;
14025951Sdougm 
14035951Sdougm 	scfhandle = sahandle->scfhandle;
14045951Sdougm 
14055951Sdougm 	if (sa_get_instance(scfhandle, "default") != SA_OK)
14065951Sdougm 		return;
14075951Sdougm 
14085951Sdougm 	if (gettimeofday(&tv, NULL) != 0)
14095951Sdougm 		return;
14105951Sdougm 
14115951Sdougm 	if (sa_start_transaction(scfhandle, "*state") != SA_OK)
14125951Sdougm 		return;
14135951Sdougm 
14145951Sdougm 	sahandle->tstrans = TSTAMP((*(timestruc_t *)&tv));
14155951Sdougm 	(void) snprintf(tstring, sizeof (tstring), "%lld", sahandle->tstrans);
14165951Sdougm 	if (sa_set_property(sahandle->scfhandle, "lastupdate", tstring) ==
14175951Sdougm 	    SA_OK) {
14185951Sdougm 		/*
14195951Sdougm 		 * While best if it succeeds, a failure doesn't cause
14205951Sdougm 		 * problems and we will ignore it anyway.
14215951Sdougm 		 */
14225951Sdougm 		(void) scf_transaction_commit(scfhandle->trans);
14235951Sdougm 		scf_transaction_destroy_children(scfhandle->trans);
14245951Sdougm 		scf_transaction_destroy(scfhandle->trans);
14255951Sdougm 	} else {
14265951Sdougm 		sa_abort_transaction(scfhandle);
14275951Sdougm 	}
14285951Sdougm }
14295951Sdougm 
14305951Sdougm /*
14313034Sdougm  * sa_set_property(handle, prop, value)
14323034Sdougm  *
14334653Sdougm  * Set a property transaction entry into the pending SMF transaction.
14343034Sdougm  */
14353034Sdougm 
14363034Sdougm int
14373034Sdougm sa_set_property(scfutilhandle_t *handle, char *propname, char *valstr)
14383034Sdougm {
14393034Sdougm 	int ret = SA_OK;
14403034Sdougm 	scf_value_t *value;
14413034Sdougm 	scf_transaction_entry_t *entry;
14423034Sdougm 	/*
14434653Sdougm 	 * Properties must be set in transactions and don't take
14443034Sdougm 	 * effect until the transaction has been ended/committed.
14453034Sdougm 	 */
14463034Sdougm 	value = scf_value_create(handle->handle);
14473034Sdougm 	entry = scf_entry_create(handle->handle);
14483034Sdougm 	if (value != NULL && entry != NULL) {
14494653Sdougm 		if (scf_transaction_property_change(handle->trans, entry,
14504653Sdougm 		    propname, SCF_TYPE_ASTRING) == 0 ||
14514653Sdougm 		    scf_transaction_property_new(handle->trans, entry,
14524653Sdougm 		    propname, SCF_TYPE_ASTRING) == 0) {
14534653Sdougm 			if (scf_value_set_astring(value, valstr) == 0) {
14544653Sdougm 				if (scf_entry_add_value(entry, value) != 0) {
14554653Sdougm 					ret = SA_SYSTEM_ERR;
14564653Sdougm 					scf_value_destroy(value);
14574653Sdougm 				}
14584653Sdougm 				/* The value is in the transaction */
14594653Sdougm 				value = NULL;
14604653Sdougm 			} else {
14614653Sdougm 				/* Value couldn't be constructed */
14624653Sdougm 				ret = SA_SYSTEM_ERR;
14634653Sdougm 			}
14644653Sdougm 			/* The entry is in the transaction */
14654653Sdougm 			entry = NULL;
14664653Sdougm 		} else {
14673034Sdougm 			ret = SA_SYSTEM_ERR;
14683034Sdougm 		}
14694653Sdougm 	} else {
14703034Sdougm 		ret = SA_SYSTEM_ERR;
14713034Sdougm 	}
14723034Sdougm 	if (ret == SA_SYSTEM_ERR) {
14734653Sdougm 		switch (scf_error()) {
14744653Sdougm 		case SCF_ERROR_PERMISSION_DENIED:
14754653Sdougm 			ret = SA_NO_PERMISSION;
14764653Sdougm 			break;
14774653Sdougm 		}
14783034Sdougm 	}
14793034Sdougm 	/*
14804653Sdougm 	 * Cleanup if there were any errors that didn't leave these
14813034Sdougm 	 * values where they would be cleaned up later.
14823034Sdougm 	 */
14833034Sdougm 	if (value != NULL)
14844653Sdougm 		scf_value_destroy(value);
14853034Sdougm 	if (entry != NULL)
14864653Sdougm 		scf_entry_destroy(entry);
14873034Sdougm 	return (ret);
14883034Sdougm }
14893034Sdougm 
14903034Sdougm /*
14915331Samw  * check_resource(share)
14925331Samw  *
14935331Samw  * Check to see if share has any persistent resources. We don't want
14945331Samw  * to save if they are all transient.
14955331Samw  */
14965331Samw static int
14975331Samw check_resource(sa_share_t share)
14985331Samw {
14995331Samw 	sa_resource_t resource;
15005331Samw 	int ret = B_FALSE;
15015331Samw 
15025331Samw 	for (resource = sa_get_share_resource(share, NULL);
15035331Samw 	    resource != NULL && ret == B_FALSE;
15045331Samw 	    resource = sa_get_next_resource(resource)) {
15055331Samw 		char *type;
15065331Samw 		type = sa_get_resource_attr(resource, "type");
15075331Samw 		if (type != NULL) {
15085331Samw 			if (strcmp(type, "transient") != 0) {
15095331Samw 				ret = B_TRUE;
15105331Samw 			}
15115331Samw 			sa_free_attr_string(type);
15125331Samw 		}
15135331Samw 	}
15145331Samw 	return (ret);
15155331Samw }
15165331Samw 
15175331Samw /*
15185331Samw  * sa_set_resource_property(handle, prop, value)
15195331Samw  *
15205331Samw  * set a property transaction entry into the pending SMF
15215331Samw  * transaction. We don't want to include any transient resources
15225331Samw  */
15235331Samw 
15245331Samw static int
15255331Samw sa_set_resource_property(scfutilhandle_t *handle, sa_share_t share)
15265331Samw {
15275331Samw 	int ret = SA_OK;
15285331Samw 	scf_value_t *value;
15295331Samw 	scf_transaction_entry_t *entry;
15305331Samw 	sa_resource_t resource;
15315331Samw 	char *valstr;
15325331Samw 	char *idstr;
15335331Samw 	char *description;
15345331Samw 	char *propstr = NULL;
15355331Samw 	size_t strsize;
15365331Samw 
15375331Samw 	/* don't bother if no persistent resources */
15385331Samw 	if (check_resource(share) == B_FALSE)
15395331Samw 		return (ret);
15405331Samw 
15415331Samw 	/*
15425331Samw 	 * properties must be set in transactions and don't take
15435331Samw 	 * effect until the transaction has been ended/committed.
15445331Samw 	 */
15455331Samw 	entry = scf_entry_create(handle->handle);
15465331Samw 	if (entry == NULL)
15475331Samw 		return (SA_SYSTEM_ERR);
15485331Samw 
15495331Samw 	if (scf_transaction_property_change(handle->trans, entry,
15505331Samw 	    "resource",	SCF_TYPE_ASTRING) != 0 &&
15515331Samw 	    scf_transaction_property_new(handle->trans, entry,
15525331Samw 	    "resource", SCF_TYPE_ASTRING) != 0) {
15535331Samw 		scf_entry_destroy(entry);
15545331Samw 		return (SA_SYSTEM_ERR);
15555331Samw 
15565331Samw 	}
15575331Samw 	for (resource = sa_get_share_resource(share, NULL);
15585331Samw 	    resource != NULL;
15595331Samw 	    resource = sa_get_next_resource(resource)) {
15605331Samw 		value = scf_value_create(handle->handle);
15615331Samw 		if (value == NULL) {
15625331Samw 			ret = SA_NO_MEMORY;
15635331Samw 			break;
15645331Samw 		}
15655331Samw 			/* Get size of complete string */
15665331Samw 		valstr = sa_get_resource_attr(resource, "name");
15675331Samw 		idstr = sa_get_resource_attr(resource, "id");
15685331Samw 		description = sa_get_resource_description(resource);
15695331Samw 		strsize = (valstr != NULL) ? strlen(valstr) : 0;
15705331Samw 		strsize += (idstr != NULL) ? strlen(idstr) : 0;
15715331Samw 		strsize += (description != NULL) ? strlen(description) : 0;
15725331Samw 		if (strsize > 0) {
15735331Samw 			strsize += 3; /* add nul and ':' */
15745331Samw 			propstr = (char *)malloc(strsize);
15755331Samw 			if (propstr == NULL) {
15765331Samw 				scf_value_destroy(value);
15775331Samw 				ret = SA_NO_MEMORY;
15785331Samw 				goto err;
15795331Samw 			}
15805331Samw 			if (idstr == NULL)
15815331Samw 				(void) snprintf(propstr, strsize, "%s",
15825331Samw 				    valstr ? valstr : "");
15835331Samw 			else
15845331Samw 				(void) snprintf(propstr, strsize, "%s:%s:%s",
15855331Samw 				    idstr ? idstr : "", valstr ? valstr : "",
15865331Samw 				    description ? description : "");
15875331Samw 			if (scf_value_set_astring(value, propstr) != 0) {
15885331Samw 				ret = SA_SYSTEM_ERR;
15895331Samw 				free(propstr);
15905331Samw 				scf_value_destroy(value);
15915331Samw 				break;
15925331Samw 			}
15935331Samw 			if (scf_entry_add_value(entry, value) != 0) {
15945331Samw 				ret = SA_SYSTEM_ERR;
15955331Samw 				free(propstr);
15965331Samw 				scf_value_destroy(value);
15975331Samw 				break;
15985331Samw 			}
15995331Samw 			/* the value is in the transaction */
16005331Samw 			value = NULL;
16015331Samw 			free(propstr);
16025331Samw 		}
16035331Samw err:
16045331Samw 		if (valstr != NULL)
16055331Samw 			sa_free_attr_string(valstr);
16065331Samw 		if (idstr != NULL)
16075331Samw 			sa_free_attr_string(idstr);
16085331Samw 		if (description != NULL)
16095331Samw 			sa_free_share_description(description);
16105331Samw 	}
16115331Samw 	/* the entry is in the transaction */
16125331Samw 	entry = NULL;
16135331Samw 
16145331Samw 	if (ret == SA_SYSTEM_ERR) {
16155331Samw 		switch (scf_error()) {
16165331Samw 		case SCF_ERROR_PERMISSION_DENIED:
16175331Samw 			ret = SA_NO_PERMISSION;
16185331Samw 			break;
16195331Samw 		}
16205331Samw 	}
16215331Samw 	/*
16225331Samw 	 * cleanup if there were any errors that didn't leave
16235331Samw 	 * these values where they would be cleaned up later.
16245331Samw 	 */
16255331Samw 	if (entry != NULL)
16265331Samw 		scf_entry_destroy(entry);
16275331Samw 
16285331Samw 	return (ret);
16295331Samw }
16305331Samw 
16315331Samw /*
16323034Sdougm  * sa_commit_share(handle, group, share)
16333034Sdougm  *
16344653Sdougm  *	Commit this share to the repository.
16353034Sdougm  *	properties are added if they exist but can be added later.
16363034Sdougm  *	Need to add to dfstab and sharetab, if appropriate.
16373034Sdougm  */
16383034Sdougm int
16393034Sdougm sa_commit_share(scfutilhandle_t *handle, sa_group_t group, sa_share_t share)
16403034Sdougm {
16413034Sdougm 	int ret = SA_OK;
16423034Sdougm 	char *groupname;
16433034Sdougm 	char *name;
16443034Sdougm 	char *description;
16453034Sdougm 	char *sharename;
16463034Sdougm 	ssize_t proplen;
16473034Sdougm 	char *propstring;
16483034Sdougm 
16493034Sdougm 	/*
16504653Sdougm 	 * Don't commit in the zfs group. We do commit legacy
16513034Sdougm 	 * (default) and all other groups/shares. ZFS is handled
16523034Sdougm 	 * through the ZFS configuration rather than SMF.
16533034Sdougm 	 */
16543034Sdougm 
16553034Sdougm 	groupname = sa_get_group_attr(group, "name");
16563034Sdougm 	if (groupname != NULL) {
16574653Sdougm 		if (strcmp(groupname, "zfs") == 0) {
16584653Sdougm 			/*
16594653Sdougm 			 * Adding to the ZFS group will result in the sharenfs
16604653Sdougm 			 * property being set but we don't want to do anything
16614653Sdougm 			 * SMF related at this point.
16624653Sdougm 			 */
16634653Sdougm 			sa_free_attr_string(groupname);
16644653Sdougm 			return (ret);
16654653Sdougm 		}
16663034Sdougm 	}
16673034Sdougm 
16683034Sdougm 	proplen = get_scf_limit(SCF_LIMIT_MAX_VALUE_LENGTH);
16693034Sdougm 	propstring = malloc(proplen);
16703034Sdougm 	if (propstring == NULL)
16714653Sdougm 		ret = SA_NO_MEMORY;
16723034Sdougm 
16733034Sdougm 	if (groupname != NULL && ret == SA_OK) {
16744653Sdougm 		ret = sa_get_instance(handle, groupname);
16754653Sdougm 		sa_free_attr_string(groupname);
16764653Sdougm 		groupname = NULL;
16774653Sdougm 		sharename = sa_get_share_attr(share, "id");
16784653Sdougm 		if (sharename == NULL) {
16794653Sdougm 			/* slipped by */
16804653Sdougm 			char shname[SA_SHARE_UUID_BUFLEN];
16814653Sdougm 			generate_unique_sharename(shname);
16824653Sdougm 			xmlSetProp((xmlNodePtr)share, (xmlChar *)"id",
16833034Sdougm 			    (xmlChar *)shname);
16844653Sdougm 			sharename = strdup(shname);
16853034Sdougm 		}
16864653Sdougm 		if (sharename != NULL) {
16874653Sdougm 			sigset_t old, new;
16884653Sdougm 			/*
16894653Sdougm 			 * Have a share name allocated so create a pgroup for
16904653Sdougm 			 * it. It may already exist, but that is OK.  In order
16914653Sdougm 			 * to avoid creating a share pgroup that doesn't have
16924653Sdougm 			 * a path property, block signals around the critical
16934653Sdougm 			 * region of creating the share pgroup and props.
16944653Sdougm 			 */
16954653Sdougm 			(void) sigprocmask(SIG_BLOCK, NULL, &new);
16964653Sdougm 			(void) sigaddset(&new, SIGHUP);
16974653Sdougm 			(void) sigaddset(&new, SIGINT);
16984653Sdougm 			(void) sigaddset(&new, SIGQUIT);
16994653Sdougm 			(void) sigaddset(&new, SIGTSTP);
17004653Sdougm 			(void) sigprocmask(SIG_SETMASK, &new, &old);
17014653Sdougm 
17024653Sdougm 			ret = sa_create_pgroup(handle, sharename);
17034653Sdougm 			if (ret == SA_OK) {
17044653Sdougm 				/*
17054653Sdougm 				 * Now start the transaction for the
17064653Sdougm 				 * properties that define this share. They may
17074653Sdougm 				 * exist so attempt to update before create.
17084653Sdougm 				 */
17094653Sdougm 				ret = sa_start_transaction(handle, sharename);
17104653Sdougm 			}
17114653Sdougm 			if (ret == SA_OK) {
17124653Sdougm 				name = sa_get_share_attr(share, "path");
17134653Sdougm 				if (name != NULL) {
17144653Sdougm 					/*
17154653Sdougm 					 * There needs to be a path
17164653Sdougm 					 * for a share to exist.
17174653Sdougm 					 */
17184653Sdougm 					ret = sa_set_property(handle, "path",
17194653Sdougm 					    name);
17204653Sdougm 					sa_free_attr_string(name);
17214653Sdougm 				} else {
17224653Sdougm 					ret = SA_NO_MEMORY;
17234653Sdougm 				}
17244653Sdougm 			}
17254653Sdougm 			if (ret == SA_OK) {
17265331Samw 				name = sa_get_share_attr(share, "drive-letter");
17275331Samw 				if (name != NULL) {
17285331Samw 					/* A drive letter may exist for SMB */
17294653Sdougm 					ret = sa_set_property(handle,
17305331Samw 					    "drive-letter", name);
17315331Samw 					sa_free_attr_string(name);
17324653Sdougm 				}
17334653Sdougm 			}
17344653Sdougm 			if (ret == SA_OK) {
17355331Samw 				name = sa_get_share_attr(share, "exclude");
17365331Samw 				if (name != NULL) {
17375331Samw 					/*
17385331Samw 					 * In special cases need to
17395331Samw 					 * exclude proto enable.
17405331Samw 					 */
17415331Samw 					ret = sa_set_property(handle,
17425331Samw 					    "exclude", name);
17435331Samw 					sa_free_attr_string(name);
17445331Samw 				}
17455331Samw 			}
17465331Samw 			if (ret == SA_OK) {
17475331Samw 				/*
17485331Samw 				 * If there are resource names, bundle them up
17495331Samw 				 * and save appropriately.
17505331Samw 				 */
17515331Samw 				ret = sa_set_resource_property(handle, share);
17525331Samw 			}
17535331Samw 
17545331Samw 			if (ret == SA_OK) {
17554653Sdougm 				description = sa_get_share_description(share);
17564653Sdougm 				if (description != NULL) {
17574653Sdougm 					ret = sa_set_property(handle,
17584653Sdougm 					    "description",
17594653Sdougm 					    description);
17604653Sdougm 					sa_free_share_description(description);
17614653Sdougm 				}
17624653Sdougm 			}
17634653Sdougm 			/* Make sure we cleanup the transaction */
17644653Sdougm 			if (ret == SA_OK) {
17655951Sdougm 				sa_handle_impl_t sahandle;
17665951Sdougm 				sahandle = (sa_handle_impl_t)
17675951Sdougm 				    sa_find_group_handle(group);
17685951Sdougm 				if (sahandle != NULL)
17695951Sdougm 					ret = sa_end_transaction(handle,
17705951Sdougm 					    sahandle);
17715951Sdougm 				else
17725951Sdougm 					ret = SA_SYSTEM_ERR;
17734653Sdougm 			} else {
17744653Sdougm 				sa_abort_transaction(handle);
17754653Sdougm 			}
17764653Sdougm 
17774653Sdougm 			(void) sigprocmask(SIG_SETMASK, &old, NULL);
17784653Sdougm 
17794653Sdougm 			free(sharename);
17803034Sdougm 		}
17813034Sdougm 	}
17823034Sdougm 	if (ret == SA_SYSTEM_ERR) {
17834653Sdougm 		int err = scf_error();
17844653Sdougm 		if (err == SCF_ERROR_PERMISSION_DENIED)
17854653Sdougm 			ret = SA_NO_PERMISSION;
17863034Sdougm 	}
17873034Sdougm 	if (propstring != NULL)
17884653Sdougm 		free(propstring);
17893034Sdougm 	if (groupname != NULL)
17904653Sdougm 		sa_free_attr_string(groupname);
17913034Sdougm 
17923034Sdougm 	return (ret);
17933034Sdougm }
17943034Sdougm 
17953034Sdougm /*
17965331Samw  * remove_resources(handle, share, shareid)
17975331Samw  *
17985331Samw  * If the share has resources, remove all of them and their
17995331Samw  * optionsets.
18005331Samw  */
18015331Samw static int
18025331Samw remove_resources(scfutilhandle_t *handle, sa_share_t share, char *shareid)
18035331Samw {
18045331Samw 	sa_resource_t resource;
18055331Samw 	sa_optionset_t opt;
18065331Samw 	char *proto;
18075331Samw 	char *id;
18085331Samw 	ssize_t proplen;
18095331Samw 	char *propstring;
18105331Samw 	int ret = SA_OK;
18115331Samw 
18125331Samw 	proplen = get_scf_limit(SCF_LIMIT_MAX_VALUE_LENGTH);
18135331Samw 	propstring = malloc(proplen);
18145331Samw 	if (propstring == NULL)
18155331Samw 		return (SA_NO_MEMORY);
18165331Samw 
18175331Samw 	for (resource = sa_get_share_resource(share, NULL);
18185331Samw 	    resource != NULL; resource = sa_get_next_resource(resource)) {
18195331Samw 		id = sa_get_resource_attr(resource, "id");
18205331Samw 		if (id == NULL)
18215331Samw 			continue;
18225331Samw 		for (opt = sa_get_optionset(resource, NULL);
18235331Samw 		    opt != NULL; opt = sa_get_next_optionset(resource)) {
18245331Samw 			proto = sa_get_optionset_attr(opt, "type");
18255331Samw 			if (proto != NULL) {
18265331Samw 				(void) snprintf(propstring, proplen,
18275331Samw 				    "%s_%s_%s", shareid, proto, id);
18285331Samw 				ret = sa_delete_pgroup(handle, propstring);
18295331Samw 				sa_free_attr_string(proto);
18305331Samw 			}
18315331Samw 		}
18325331Samw 		sa_free_attr_string(id);
18335331Samw 	}
18345331Samw 	free(propstring);
18355331Samw 	return (ret);
18365331Samw }
18375331Samw 
18385331Samw /*
18393034Sdougm  * sa_delete_share(handle, group, share)
18403034Sdougm  *
18414653Sdougm  * Remove the specified share from the group (and service instance).
18423034Sdougm  */
18433034Sdougm 
18443034Sdougm int
18453034Sdougm sa_delete_share(scfutilhandle_t *handle, sa_group_t group, sa_share_t share)
18463034Sdougm {
18473034Sdougm 	int ret = SA_OK;
18483034Sdougm 	char *groupname = NULL;
18493034Sdougm 	char *shareid = NULL;
18503034Sdougm 	sa_optionset_t opt;
18513034Sdougm 	sa_security_t sec;
18523034Sdougm 	ssize_t proplen;
18533034Sdougm 	char *propstring;
18543034Sdougm 
18553034Sdougm 	proplen = get_scf_limit(SCF_LIMIT_MAX_VALUE_LENGTH);
18563034Sdougm 	propstring = malloc(proplen);
18573034Sdougm 	if (propstring == NULL)
18584653Sdougm 		ret = SA_NO_MEMORY;
18593034Sdougm 
18603034Sdougm 	if (ret == SA_OK) {
18614653Sdougm 		groupname = sa_get_group_attr(group, "name");
18624653Sdougm 		shareid = sa_get_share_attr(share, "id");
18634653Sdougm 		if (groupname == NULL || shareid == NULL) {
18644653Sdougm 			ret = SA_CONFIG_ERR;
18654653Sdougm 			goto out;
18664653Sdougm 		}
18673034Sdougm 		ret = sa_get_instance(handle, groupname);
18683034Sdougm 		if (ret == SA_OK) {
18695331Samw 			/* If a share has resources, remove them */
18705331Samw 			ret = remove_resources(handle, share, shareid);
18714653Sdougm 			/* If a share has properties, remove them */
18724653Sdougm 			ret = sa_delete_pgroup(handle, shareid);
18734653Sdougm 			for (opt = sa_get_optionset(share, NULL);
18744653Sdougm 			    opt != NULL;
18754653Sdougm 			    opt = sa_get_next_optionset(opt)) {
18764653Sdougm 				char *proto;
18774653Sdougm 				proto = sa_get_optionset_attr(opt, "type");
18784653Sdougm 				if (proto != NULL) {
18794653Sdougm 					(void) snprintf(propstring,
18804653Sdougm 					    proplen, "%s_%s", shareid,
18814653Sdougm 					    proto);
18824653Sdougm 					ret = sa_delete_pgroup(handle,
18834653Sdougm 					    propstring);
18844653Sdougm 					sa_free_attr_string(proto);
18854653Sdougm 				} else {
18864653Sdougm 					ret = SA_NO_MEMORY;
18874653Sdougm 				}
18883034Sdougm 			}
18893034Sdougm 			/*
18904653Sdougm 			 * If a share has security/negotiable
18913034Sdougm 			 * properties, remove them.
18923034Sdougm 			 */
18934653Sdougm 			for (sec = sa_get_security(share, NULL, NULL);
18944653Sdougm 			    sec != NULL;
18954653Sdougm 			    sec = sa_get_next_security(sec)) {
18964653Sdougm 				char *proto;
18974653Sdougm 				char *sectype;
18984653Sdougm 				proto = sa_get_security_attr(sec, "type");
18994653Sdougm 				sectype = sa_get_security_attr(sec, "sectype");
19004653Sdougm 				if (proto != NULL && sectype != NULL) {
19014653Sdougm 					(void) snprintf(propstring, proplen,
19024653Sdougm 					    "%s_%s_%s", shareid,  proto,
19034653Sdougm 					    sectype);
19044653Sdougm 					ret = sa_delete_pgroup(handle,
19054653Sdougm 					    propstring);
19064653Sdougm 				} else {
19074653Sdougm 					ret = SA_NO_MEMORY;
19084653Sdougm 				}
19094653Sdougm 				if (proto != NULL)
19104653Sdougm 					sa_free_attr_string(proto);
19114653Sdougm 				if (sectype != NULL)
19124653Sdougm 					sa_free_attr_string(sectype);
19133034Sdougm 			}
19143034Sdougm 		}
19153034Sdougm 	}
19164653Sdougm out:
19173034Sdougm 	if (groupname != NULL)
19184653Sdougm 		sa_free_attr_string(groupname);
19193034Sdougm 	if (shareid != NULL)
19204653Sdougm 		sa_free_attr_string(shareid);
19213034Sdougm 	if (propstring != NULL)
19224653Sdougm 		free(propstring);
19233034Sdougm 
19243034Sdougm 	return (ret);
19253034Sdougm }
1926