15331Samw /*
25331Samw  * CDDL HEADER START
35331Samw  *
45331Samw  * The contents of this file are subject to the terms of the
55331Samw  * Common Development and Distribution License (the "License").
65331Samw  * You may not use this file except in compliance with the License.
75331Samw  *
85331Samw  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
95331Samw  * or http://www.opensolaris.org/os/licensing.
105331Samw  * See the License for the specific language governing permissions
115331Samw  * and limitations under the License.
125331Samw  *
135331Samw  * When distributing Covered Code, include this CDDL HEADER in each
145331Samw  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
155331Samw  * If applicable, add the following below this CDDL HEADER, with the
165331Samw  * fields enclosed by brackets "[]" replaced with your own identifying
175331Samw  * information: Portions Copyright [yyyy] [name of copyright owner]
185331Samw  *
195331Samw  * CDDL HEADER END
205331Samw  */
215331Samw 
225331Samw /*
235772Sas200622  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
245331Samw  * Use is subject to license terms.
255331Samw  */
265331Samw 
275331Samw #pragma ident	"%Z%%M%	%I%	%E% SMI"
285331Samw 
295331Samw /*
305331Samw  * SMB specific functions
315331Samw  */
325331Samw #include <stdio.h>
335331Samw #include <string.h>
345331Samw #include <ctype.h>
355331Samw #include <stdlib.h>
365331Samw #include <unistd.h>
375331Samw #include <zone.h>
385331Samw #include <errno.h>
395331Samw #include <locale.h>
405331Samw #include <fcntl.h>
415331Samw #include <sys/types.h>
425331Samw #include <sys/stat.h>
435331Samw #include <syslog.h>
445331Samw #include "libshare.h"
455331Samw #include "libshare_impl.h"
465331Samw #include <pwd.h>
475331Samw #include <limits.h>
485331Samw #include <libscf.h>
495331Samw #include <strings.h>
505331Samw #include "libshare_smb.h"
515331Samw #include <rpcsvc/daemon_utils.h>
525331Samw #include <smbsrv/lmshare.h>
535331Samw #include <smbsrv/lmshare_door.h>
545331Samw #include <smbsrv/smbinfo.h>
555331Samw #include <smbsrv/libsmb.h>
565331Samw 
575331Samw /* internal functions */
585331Samw static int smb_share_init(void);
595331Samw static void smb_share_fini(void);
605331Samw static int smb_enable_share(sa_share_t);
615331Samw static int smb_share_changed(sa_share_t);
625331Samw static int smb_resource_changed(sa_resource_t);
635331Samw static int smb_rename_resource(sa_handle_t, sa_resource_t, char *);
645331Samw static int smb_disable_share(sa_share_t share, char *);
655331Samw static int smb_validate_property(sa_property_t, sa_optionset_t);
665331Samw static int smb_set_proto_prop(sa_property_t);
675331Samw static sa_protocol_properties_t smb_get_proto_set(void);
685331Samw static char *smb_get_status(void);
695331Samw static int smb_parse_optstring(sa_group_t, char *);
705331Samw static char *smb_format_options(sa_group_t, int);
715331Samw 
725331Samw static int smb_enable_service(void);
735331Samw 
745331Samw static int range_check_validator(int, char *);
755331Samw static int range_check_validator_zero_ok(int, char *);
765331Samw static int string_length_check_validator(int, char *);
775331Samw static int true_false_validator(int, char *);
785331Samw static int ip_address_validator_empty_ok(int, char *);
795331Samw static int ip_address_csv_list_validator_empty_ok(int, char *);
805331Samw static int path_validator(int, char *);
815331Samw 
825331Samw static int smb_enable_resource(sa_resource_t);
835331Samw static int smb_disable_resource(sa_resource_t);
845331Samw static uint64_t smb_share_features(void);
855331Samw static int smb_list_transient(sa_handle_t);
865772Sas200622 
875772Sas200622 extern void lmshrd_door_close(void);
885331Samw 
895331Samw /* size of basic format allocation */
905331Samw #define	OPT_CHUNK	1024
915331Samw 
925772Sas200622 /* size of string for types - big enough to hold "dependency" */
935772Sas200622 #define	SCFTYPE_LEN	32
945772Sas200622 
955331Samw /*
965331Samw  * Indexes of entries in smb_proto_options table.
975331Samw  * Changes to smb_proto_options table may require
985331Samw  * an update to these values.
995331Samw  */
1005331Samw #define	PROTO_OPT_WINS1			6
1015331Samw #define	PROTO_OPT_WINS_EXCLUDE		8
1025331Samw 
1035331Samw 
1045331Samw /*
1055331Samw  * ops vector that provides the protocol specific info and operations
1065331Samw  * for share management.
1075331Samw  */
1085331Samw 
1095331Samw struct sa_plugin_ops sa_plugin_ops = {
1105331Samw 	SA_PLUGIN_VERSION,
1115331Samw 	SMB_PROTOCOL_NAME,
1125331Samw 	smb_share_init,
1135331Samw 	smb_share_fini,
1145331Samw 	smb_enable_share,
1155331Samw 	smb_disable_share,
1165331Samw 	smb_validate_property,
1176007Sthurlow 	NULL,	/* valid_space */
1186007Sthurlow 	NULL,	/* security_prop */
1195331Samw 	smb_parse_optstring,
1205331Samw 	smb_format_options,
1215331Samw 	smb_set_proto_prop,
1225331Samw 	smb_get_proto_set,
1235331Samw 	smb_get_status,
1246007Sthurlow 	NULL,	/* space_alias */
1256007Sthurlow 	NULL,	/* update_legacy */
1266007Sthurlow 	NULL,	/* delete_legacy */
1275331Samw 	smb_share_changed,
1285331Samw 	smb_enable_resource,
1295331Samw 	smb_disable_resource,
1305331Samw 	smb_share_features,
1315331Samw 	smb_list_transient,
1325331Samw 	smb_resource_changed,
1335331Samw 	smb_rename_resource,
1346007Sthurlow 	NULL,	/* run_command */
1356007Sthurlow 	NULL,	/* command_help */
1366007Sthurlow 	NULL	/* delete_proto_section */
1375331Samw };
1385331Samw 
1395331Samw /*
1405331Samw  * option definitions.  Make sure to keep the #define for the option
1415331Samw  * index just before the entry it is the index for. Changing the order
1425331Samw  * can cause breakage.
1435331Samw  */
1445331Samw 
1455331Samw struct option_defs optdefs[] = {
1465331Samw 	{SHOPT_AD_CONTAINER, OPT_TYPE_STRING},
1475331Samw 	{SHOPT_NAME, OPT_TYPE_NAME},
1485331Samw 	{NULL, NULL},
1495331Samw };
1505331Samw 
1515331Samw /*
1525331Samw  * findopt(name)
1535331Samw  *
1545331Samw  * Lookup option "name" in the option table and return the table
1555331Samw  * index.
1565331Samw  */
1575331Samw 
1585331Samw static int
1595331Samw findopt(char *name)
1605331Samw {
1615331Samw 	int i;
1625331Samw 	if (name != NULL) {
1635331Samw 		for (i = 0; optdefs[i].tag != NULL; i++) {
1645331Samw 			if (strcmp(optdefs[i].tag, name) == 0)
1655331Samw 				return (i);
1665331Samw 		}
1675331Samw 	}
1685331Samw 	return (-1);
1695331Samw }
1705331Samw 
1715331Samw /*
1725331Samw  * is_a_number(number)
1735331Samw  *
1745331Samw  * is the string a number in one of the forms we want to use?
1755331Samw  */
1765331Samw 
1775331Samw static int
1785331Samw is_a_number(char *number)
1795331Samw {
1805331Samw 	int ret = 1;
1815331Samw 	int hex = 0;
1825331Samw 
1835331Samw 	if (strncmp(number, "0x", 2) == 0) {
1845331Samw 		number += 2;
1855331Samw 		hex = 1;
1865331Samw 	} else if (*number == '-') {
1875331Samw 		number++; /* skip the minus */
1885331Samw 	}
1895331Samw 
1905331Samw 	while (ret == 1 && *number != '\0') {
1915331Samw 		if (hex) {
1925331Samw 			ret = isxdigit(*number++);
1935331Samw 		} else {
1945331Samw 			ret = isdigit(*number++);
1955331Samw 		}
1965331Samw 	}
1975331Samw 	return (ret);
1985331Samw }
1995331Samw 
2005331Samw /*
2015331Samw  * validresource(name)
2025331Samw  *
2035331Samw  * Check that name only has valid characters in it. The current valid
2045331Samw  * set are the printable characters but not including:
2055331Samw  *	" / \ [ ] : | < > + ; , ? * = \t
2065331Samw  * Note that space is included and there is a maximum length.
2075331Samw  */
2085331Samw static int
2095331Samw validresource(const char *name)
2105331Samw {
2115331Samw 	const char *cp;
2125331Samw 	size_t len;
2135331Samw 
2145331Samw 	if (name == NULL)
2155331Samw 		return (B_FALSE);
2165331Samw 
2175331Samw 	len = strlen(name);
2185331Samw 	if (len == 0 || len > SA_MAX_RESOURCE_NAME)
2195331Samw 		return (B_FALSE);
2205331Samw 
2215331Samw 	if (strpbrk(name, "\"/\\[]:|<>+;,?*=\t") != NULL) {
2225331Samw 		return (B_FALSE);
2235331Samw 	}
2245331Samw 
2255331Samw 	for (cp = name; *cp != '\0'; cp++)
2265331Samw 		if (iscntrl(*cp))
2275331Samw 			return (B_FALSE);
2285331Samw 
2295331Samw 	return (B_TRUE);
2305331Samw }
2315331Samw 
2325331Samw /*
2335331Samw  * smb_isonline()
2345331Samw  *
2355331Samw  * Determine if the SMF service instance is in the online state or
2365331Samw  * not. A number of operations depend on this state.
2375331Samw  */
2385331Samw static boolean_t
2395331Samw smb_isonline(void)
2405331Samw {
2415331Samw 	char *str;
2425331Samw 	boolean_t ret = B_FALSE;
2435331Samw 
2445331Samw 	if ((str = smf_get_state(SMBD_DEFAULT_INSTANCE_FMRI)) != NULL) {
2455331Samw 		ret = (strcmp(str, SCF_STATE_STRING_ONLINE) == 0);
2465331Samw 		free(str);
2475331Samw 	}
2485331Samw 	return (ret);
2495331Samw }
2505331Samw 
2515331Samw /*
2525772Sas200622  * smb_isdisabled()
2535772Sas200622  *
2545772Sas200622  * Determine if the SMF service instance is in the disabled state or
2555772Sas200622  * not. A number of operations depend on this state.
2565772Sas200622  */
2575772Sas200622 static boolean_t
2585772Sas200622 smb_isdisabled(void)
2595772Sas200622 {
2605772Sas200622 	char *str;
2615772Sas200622 	boolean_t ret = B_FALSE;
2625772Sas200622 
2635772Sas200622 	if ((str = smf_get_state(SMBD_DEFAULT_INSTANCE_FMRI)) != NULL) {
2645772Sas200622 		ret = (strcmp(str, SCF_STATE_STRING_DISABLED) == 0);
2655772Sas200622 		free(str);
2665772Sas200622 	}
2675772Sas200622 	return (ret);
2685772Sas200622 }
2695772Sas200622 
2705772Sas200622 /*
2715772Sas200622  * smb_isautoenable()
2725772Sas200622  *
2735772Sas200622  * Determine if the SMF service instance auto_enabled set or not. A
2745772Sas200622  * number of operations depend on this state.  The property not being
2755772Sas200622  * set or being set to true means autoenable.  Only being set to false
2765772Sas200622  * is not autoenabled.
2775772Sas200622  */
2785772Sas200622 static boolean_t
2795772Sas200622 smb_isautoenable(void)
2805772Sas200622 {
2815772Sas200622 	boolean_t ret = B_TRUE;
2825772Sas200622 	scf_simple_prop_t *prop;
2835772Sas200622 	uint8_t *retstr;
2845772Sas200622 
2855772Sas200622 	prop = scf_simple_prop_get(NULL, SMBD_DEFAULT_INSTANCE_FMRI,
2865772Sas200622 	    "application", "auto_enable");
2875772Sas200622 	if (prop != NULL) {
2885772Sas200622 		retstr = scf_simple_prop_next_boolean(prop);
2895772Sas200622 		ret = *retstr != 0;
2905772Sas200622 		scf_simple_prop_free(prop);
2915772Sas200622 	}
2925772Sas200622 	return (ret);
2935772Sas200622 }
2945772Sas200622 
2955772Sas200622 /*
2965331Samw  * smb_enable_share tells the implementation that it is to enable the share.
2975331Samw  * This entails converting the path and options into the appropriate ioctl
2985331Samw  * calls. It is assumed that all error checking of paths, etc. were
2995331Samw  * done earlier.
3005331Samw  */
3015331Samw static int
3025331Samw smb_enable_share(sa_share_t share)
3035331Samw {
3045331Samw 	char *path;
3055331Samw 	char *rname;
3065331Samw 	lmshare_info_t si;
3075331Samw 	sa_resource_t resource;
3085331Samw 	boolean_t iszfs;
3095331Samw 	boolean_t privileged;
3105331Samw 	int err = SA_OK;
3115331Samw 	priv_set_t *priv_effective;
3125331Samw 	boolean_t online;
3135331Samw 
3145331Samw 	priv_effective = priv_allocset();
3155331Samw 	(void) getppriv(PRIV_EFFECTIVE, priv_effective);
3165331Samw 	privileged = (priv_isfullset(priv_effective) == B_TRUE);
3175331Samw 	priv_freeset(priv_effective);
3185331Samw 
3195331Samw 	/* get the path since it is important in several places */
3205331Samw 	path = sa_get_share_attr(share, "path");
3215331Samw 	if (path == NULL)
3225331Samw 		return (SA_NO_SUCH_PATH);
3235331Samw 
3245772Sas200622 	/*
3255772Sas200622 	 * If administratively disabled, don't try to start anything.
3265772Sas200622 	 */
3275331Samw 	online = smb_isonline();
3285772Sas200622 	if (!online && !smb_isautoenable() && smb_isdisabled())
3295772Sas200622 		goto done;
3305331Samw 
3315331Samw 	iszfs = sa_path_is_zfs(path);
3325331Samw 
3335331Samw 	if (iszfs) {
3345331Samw 
3355331Samw 		if (privileged == B_FALSE && !online) {
3365331Samw 
3375331Samw 			if (!online) {
3385331Samw 				(void) printf(dgettext(TEXT_DOMAIN,
3395331Samw 				    "SMB: Cannot share remove "
3405331Samw 				    "file system: %s\n"), path);
3415331Samw 				(void) printf(dgettext(TEXT_DOMAIN,
3425331Samw 				    "SMB: Service needs to be enabled "
3435331Samw 				    "by a privileged user\n"));
3445331Samw 				err = SA_NO_PERMISSION;
3455331Samw 				errno = EPERM;
3465331Samw 			}
3475331Samw 			if (err) {
3485331Samw 				sa_free_attr_string(path);
3495331Samw 				return (err);
3505331Samw 			}
3515331Samw 
3525331Samw 		}
3535331Samw 	}
3545331Samw 
3555331Samw 	if (privileged == B_TRUE && !online) {
3565331Samw 		err = smb_enable_service();
3575331Samw 		if (err != SA_OK) {
3585331Samw 			(void) printf(dgettext(TEXT_DOMAIN,
3595331Samw 			    "SMB: Unable to enable service\n"));
3605331Samw 			/*
3615331Samw 			 * For now, it is OK to not be able to enable
3625331Samw 			 * the service.
3635331Samw 			 */
3645331Samw 			if (err == SA_BUSY)
3655331Samw 				err = SA_OK;
3665331Samw 		} else {
3675331Samw 			online = B_TRUE;
3685331Samw 		}
3695331Samw 	}
3705331Samw 
3715331Samw 	/*
3725331Samw 	 * Don't bother trying to start shares if the service isn't
3735331Samw 	 * running.
3745331Samw 	 */
3755331Samw 	if (!online)
3765331Samw 		goto done;
3775331Samw 
3785331Samw 	/* Each share can have multiple resources */
3795331Samw 	for (resource = sa_get_share_resource(share, NULL);
3805331Samw 	    resource != NULL;
3815331Samw 	    resource = sa_get_next_resource(resource)) {
3825331Samw 		sa_optionset_t opts;
3835331Samw 		bzero(&si, sizeof (lmshare_info_t));
3845331Samw 		rname = sa_get_resource_attr(resource, "name");
3855331Samw 		if (rname == NULL) {
3865331Samw 			sa_free_attr_string(path);
3875331Samw 			return (SA_NO_SUCH_RESOURCE);
3885331Samw 		}
3895331Samw 
3905331Samw 		opts = sa_get_derived_optionset(resource, SMB_PROTOCOL_NAME, 1);
3915331Samw 		smb_build_lmshare_info(rname, path, opts, &si);
3925331Samw 		sa_free_attr_string(rname);
3935331Samw 
3945331Samw 		sa_free_derived_optionset(opts);
3955331Samw 		if (!iszfs) {
3965331Samw 			err = lmshrd_add(&si);
3975331Samw 		} else {
3985331Samw 			share_t sh;
3995331Samw 
4005331Samw 			sa_sharetab_fill_zfs(share, &sh, "smb");
4015331Samw 			err = sa_share_zfs(share, (char *)path, &sh,
4025331Samw 			    &si, ZFS_SHARE_SMB);
4035331Samw 
4045331Samw 			sa_emptyshare(&sh);
4055331Samw 		}
4065331Samw 	}
4075331Samw 	if (!iszfs)
4085331Samw 		(void) sa_update_sharetab(share, "smb");
4095331Samw done:
4105331Samw 	sa_free_attr_string(path);
4115331Samw 
4125331Samw 	return (err == NERR_DuplicateShare ? 0 : err);
4135331Samw }
4145331Samw 
4155331Samw /*
4165331Samw  * This is the share for CIFS all shares have resource names.
4175331Samw  * Enable tells the smb server to update its hash. If it fails
4185331Samw  * because smb server is down, we just ignore as smb server loads
4195331Samw  * the resources from sharemanager at startup.
4205331Samw  */
4215331Samw 
4225331Samw static int
4235331Samw smb_enable_resource(sa_resource_t resource)
4245331Samw {
4255331Samw 	char *path;
4265331Samw 	char *rname;
4275331Samw 	sa_optionset_t opts;
4285331Samw 	sa_share_t share;
4295331Samw 	lmshare_info_t si;
4305772Sas200622 	int ret = SA_OK;
4315772Sas200622 	int err;
4325772Sas200622 	boolean_t isonline;
4335331Samw 
4345331Samw 	share = sa_get_resource_parent(resource);
4355331Samw 	if (share == NULL)
4365331Samw 		return (SA_NO_SUCH_PATH);
4375772Sas200622 
4385772Sas200622 	/*
4395772Sas200622 	 * If administratively disabled, don't try to start anything.
4405772Sas200622 	 */
4415772Sas200622 	isonline = smb_isonline();
4425772Sas200622 	if (!isonline && !smb_isautoenable() && smb_isdisabled())
4435772Sas200622 		goto done;
4445772Sas200622 
4455772Sas200622 	if (!isonline)
4465772Sas200622 		ret = smb_enable_service();
4475772Sas200622 	if (!smb_isonline()) {
4485772Sas200622 		ret = SA_OK;
4495772Sas200622 		goto done;
4505772Sas200622 	}
4515772Sas200622 
4525331Samw 	path = sa_get_share_attr(share, "path");
4535331Samw 	if (path == NULL)
4545331Samw 		return (SA_SYSTEM_ERR);
4555331Samw 	rname = sa_get_resource_attr(resource, "name");
4565331Samw 	if (rname == NULL) {
4575331Samw 		sa_free_attr_string(path);
4585331Samw 		return (SA_NO_SUCH_RESOURCE);
4595331Samw 	}
4605331Samw 
4615331Samw 	opts = sa_get_derived_optionset(resource, SMB_PROTOCOL_NAME, 1);
4625331Samw 	smb_build_lmshare_info(rname, path, opts, &si);
4635331Samw 	sa_free_attr_string(path);
4645331Samw 	sa_free_attr_string(rname);
4655331Samw 	sa_free_derived_optionset(opts);
4665772Sas200622 
4675772Sas200622 	/*
4685772Sas200622 	 * Attempt to add the share. Any error that occurs if it was
4695772Sas200622 	 * online is an error but don't count NERR_DuplicateName if
4705772Sas200622 	 * smb/server had to be brought online since bringing the
4715772Sas200622 	 * service up will enable the share that was just added prior
4725772Sas200622 	 * to the attempt to enable.
4735772Sas200622 	 */
4745772Sas200622 
4755772Sas200622 	err = lmshrd_add(&si);
4765772Sas200622 	if (err == NERR_Success || !(!isonline && err == NERR_DuplicateName))
4775772Sas200622 		(void) sa_update_sharetab(share, "smb");
4785772Sas200622 	else
4795331Samw 		return (SA_NOT_SHARED);
4805331Samw 
4815331Samw done:
4825331Samw 	return (ret);
4835331Samw }
4845331Samw 
4855331Samw /*
4865331Samw  * Remove it from smb server hash.
4875331Samw  */
4885331Samw static int
4895331Samw smb_disable_resource(sa_resource_t resource)
4905331Samw {
4915331Samw 	char *rname;
4925331Samw 	DWORD res;
4935331Samw 	sa_share_t share;
4945331Samw 
4955331Samw 	rname = sa_get_resource_attr(resource, "name");
4965331Samw 	if (rname == NULL)
4975331Samw 		return (SA_NO_SUCH_RESOURCE);
4985331Samw 
4995331Samw 	if (smb_isonline()) {
5005331Samw 		res = lmshrd_delete(rname);
5015331Samw 		if (res != NERR_Success) {
5025331Samw 			sa_free_attr_string(rname);
5035331Samw 			return (SA_CONFIG_ERR);
5045331Samw 		}
5055331Samw 	}
5065951Sdougm 
5075951Sdougm 	sa_free_attr_string(rname);
5085951Sdougm 
5095331Samw 	share = sa_get_resource_parent(resource);
5105331Samw 	if (share != NULL) {
5115331Samw 		rname = sa_get_share_attr(share, "path");
5125331Samw 		if (rname != NULL) {
5135951Sdougm 			sa_handle_t handle;
5145951Sdougm 
5155951Sdougm 			handle = sa_find_group_handle((sa_group_t)resource);
5165951Sdougm 			(void) sa_delete_sharetab(handle, rname, "smb");
5175331Samw 			sa_free_attr_string(rname);
5185331Samw 		}
5195331Samw 	}
5205331Samw 	/*
5215331Samw 	 * Always return OK as smb/server may be down and
5225331Samw 	 * Shares will be picked up when loaded.
5235331Samw 	 */
5245331Samw 	return (SA_OK);
5255331Samw }
5265331Samw 
5275331Samw /*
5285331Samw  * smb_share_changed(sa_share_t share)
5295331Samw  *
5305331Samw  * The specified share has changed.
5315331Samw  */
5325331Samw static int
5335331Samw smb_share_changed(sa_share_t share)
5345331Samw {
5355331Samw 	char *path;
5365331Samw 	sa_resource_t resource;
5375331Samw 
5385331Samw 	/* get the path since it is important in several places */
5395331Samw 	path = sa_get_share_attr(share, "path");
5405331Samw 	if (path == NULL)
5415331Samw 		return (SA_NO_SUCH_PATH);
5425331Samw 	for (resource = sa_get_share_resource(share, NULL);
5435331Samw 	    resource != NULL;
5445331Samw 	    resource = sa_get_next_resource(resource))
5455331Samw 		(void) smb_resource_changed(resource);
5465331Samw 
5475331Samw 	sa_free_attr_string(path);
5485331Samw 
5495331Samw 	return (SA_OK);
5505331Samw }
5515331Samw 
5525331Samw /*
5535331Samw  * smb_resource_changed(sa_resource_t resource)
5545331Samw  *
5555331Samw  * The specified resource has changed.
5565331Samw  */
5575331Samw static int
5585331Samw smb_resource_changed(sa_resource_t resource)
5595331Samw {
5605331Samw 	DWORD res;
5615331Samw 	lmshare_info_t si;
5625331Samw 	lmshare_info_t new_si;
5635331Samw 	char *rname, *path;
5645331Samw 	sa_optionset_t opts;
5655331Samw 	sa_share_t share;
5665331Samw 
5675331Samw 	rname = sa_get_resource_attr(resource, "name");
5685331Samw 	if (rname == NULL)
5695331Samw 		return (SA_NO_SUCH_RESOURCE);
5705331Samw 
5715331Samw 	share = sa_get_resource_parent(resource);
5725331Samw 	if (share == NULL) {
5735331Samw 		sa_free_attr_string(rname);
5745331Samw 		return (SA_CONFIG_ERR);
5755331Samw 	}
5765331Samw 
5775331Samw 	path = sa_get_share_attr(share, "path");
5785331Samw 	if (path == NULL) {
5795331Samw 		sa_free_attr_string(rname);
5805331Samw 		return (SA_NO_SUCH_PATH);
5815331Samw 	}
5825331Samw 
5835331Samw 	if (!smb_isonline()) {
5845331Samw 		sa_free_attr_string(rname);
5855331Samw 		return (SA_OK);
5865331Samw 	}
5875331Samw 
5885331Samw 	/* Update the share cache in smb/server */
5895331Samw 	res = lmshrd_getinfo(rname, &si);
5905331Samw 	if (res != NERR_Success) {
5915331Samw 		sa_free_attr_string(path);
5925331Samw 		sa_free_attr_string(rname);
5935331Samw 		return (SA_CONFIG_ERR);
5945331Samw 	}
5955331Samw 
5965331Samw 	opts = sa_get_derived_optionset(resource, SMB_PROTOCOL_NAME, 1);
5975331Samw 	smb_build_lmshare_info(rname, path, opts, &new_si);
5985331Samw 	sa_free_derived_optionset(opts);
5995331Samw 	sa_free_attr_string(path);
6005331Samw 	sa_free_attr_string(rname);
6015331Samw 
6025331Samw 	/*
6035331Samw 	 * Update all fields from sa_share_t
6045331Samw 	 * Get derived values.
6055331Samw 	 */
6065331Samw 	if (lmshrd_setinfo(&new_si) != LMSHR_DOOR_SRV_SUCCESS)
6075331Samw 		return (SA_CONFIG_ERR);
6085331Samw 	return (smb_enable_service());
6095331Samw }
6105331Samw 
6115331Samw /*
6125800Sdougm  * smb_disable_share(sa_share_t share, char *path)
6135331Samw  *
6145800Sdougm  * Unshare the specified share. Note that "path" is the same
6155800Sdougm  * path as what is in the "share" object. It is passed in to avoid an
6165800Sdougm  * additional lookup. A missing "path" value makes this a no-op
6175800Sdougm  * function.
6185331Samw  */
6195331Samw static int
6205331Samw smb_disable_share(sa_share_t share, char *path)
6215331Samw {
6225331Samw 	char *rname;
6235331Samw 	sa_resource_t resource;
6245800Sdougm 	sa_group_t parent;
6255331Samw 	boolean_t iszfs;
6265331Samw 	int err = SA_OK;
6275951Sdougm 	sa_handle_t handle;
6285331Samw 
6295800Sdougm 	if (path == NULL)
6305800Sdougm 		return (err);
6315800Sdougm 
6325800Sdougm 	/*
6335800Sdougm 	 * If the share is in a ZFS group we need to handle it
6345800Sdougm 	 * differently.  Just being on a ZFS file system isn't
6355800Sdougm 	 * enough since we may be in a legacy share case.
6365800Sdougm 	 */
6375800Sdougm 	parent = sa_get_parent_group(share);
6385800Sdougm 	iszfs = sa_group_is_zfs(parent);
6395800Sdougm 
6405331Samw 	if (!smb_isonline())
6415331Samw 		goto done;
6425331Samw 
6435331Samw 	for (resource = sa_get_share_resource(share, NULL);
6445331Samw 	    resource != NULL;
6455331Samw 	    resource = sa_get_next_resource(resource)) {
6465331Samw 		rname = sa_get_resource_attr(resource, "name");
6475331Samw 		if (rname == NULL) {
6485331Samw 			continue;
6495331Samw 		}
6505331Samw 		if (!iszfs) {
6515331Samw 			err = lmshrd_delete(rname);
6525331Samw 			switch (err) {
6535331Samw 			case NERR_NetNameNotFound:
6545331Samw 			case NERR_Success:
6555331Samw 				err = SA_OK;
6565331Samw 				break;
6575331Samw 			default:
6585331Samw 				err = SA_CONFIG_ERR;
6595331Samw 				break;
6605331Samw 			}
6615331Samw 		} else {
6625331Samw 			share_t sh;
6635331Samw 
6645331Samw 			sa_sharetab_fill_zfs(share, &sh, "smb");
6655331Samw 			err = sa_share_zfs(share, (char *)path, &sh,
6665331Samw 			    rname, ZFS_UNSHARE_SMB);
6675331Samw 			sa_emptyshare(&sh);
6685331Samw 		}
6695331Samw 		sa_free_attr_string(rname);
6705331Samw 	}
6715331Samw done:
6725951Sdougm 	if (!iszfs) {
6735951Sdougm 		handle = sa_find_group_handle((sa_group_t)share);
6745951Sdougm 		if (handle != NULL)
6755951Sdougm 			(void) sa_delete_sharetab(handle, path, "smb");
6765951Sdougm 		else
6775951Sdougm 			err = SA_SYSTEM_ERR;
6785951Sdougm 	}
6795331Samw 	return (err);
6805331Samw }
6815331Samw 
6825331Samw /*
6835331Samw  * smb_validate_property(property, parent)
6845331Samw  *
6855331Samw  * Check that the property has a legitimate value for its type.
6865331Samw  */
6875331Samw 
6885331Samw static int
6895331Samw smb_validate_property(sa_property_t property, sa_optionset_t parent)
6905331Samw {
6915331Samw 	int ret = SA_OK;
6925331Samw 	char *propname;
6935331Samw 	int optindex;
6945331Samw 	sa_group_t parent_group;
6955331Samw 	char *value;
6965331Samw 
6975331Samw 	propname = sa_get_property_attr(property, "type");
6985331Samw 
6995331Samw 	if ((optindex = findopt(propname)) < 0)
7005331Samw 		ret = SA_NO_SUCH_PROP;
7015331Samw 
7025331Samw 	/* need to validate value range here as well */
7035331Samw 	if (ret == SA_OK) {
7045331Samw 		parent_group = sa_get_parent_group((sa_share_t)parent);
7055331Samw 		if (optdefs[optindex].share && !sa_is_share(parent_group))
7065331Samw 			ret = SA_PROP_SHARE_ONLY;
7075331Samw 	}
7085331Samw 	if (ret != SA_OK) {
7095331Samw 		if (propname != NULL)
7105331Samw 			sa_free_attr_string(propname);
7115331Samw 		return (ret);
7125331Samw 	}
7135331Samw 
7145331Samw 	value = sa_get_property_attr(property, "value");
7155331Samw 	if (value != NULL) {
7165331Samw 		/* first basic type checking */
7175331Samw 		switch (optdefs[optindex].type) {
7185331Samw 		case OPT_TYPE_NUMBER:
7195331Samw 			/* check that the value is all digits */
7205331Samw 			if (!is_a_number(value))
7215331Samw 				ret = SA_BAD_VALUE;
7225331Samw 			break;
7235331Samw 		case OPT_TYPE_BOOLEAN:
7245331Samw 			if (strlen(value) == 0 ||
7255331Samw 			    strcasecmp(value, "true") == 0 ||
7265331Samw 			    strcmp(value, "1") == 0 ||
7275331Samw 			    strcasecmp(value, "false") == 0 ||
7285331Samw 			    strcmp(value, "0") == 0) {
7295331Samw 				ret = SA_OK;
7305331Samw 			} else {
7315331Samw 				ret = SA_BAD_VALUE;
7325331Samw 			}
7335331Samw 			break;
7345331Samw 		case OPT_TYPE_NAME:
7355331Samw 			/*
7365331Samw 			 * Make sure no invalid characters
7375331Samw 			 */
7385331Samw 			if (validresource(value) == B_FALSE)
7395331Samw 				ret = SA_BAD_VALUE;
7405331Samw 			break;
7415331Samw 		case OPT_TYPE_STRING:
7425331Samw 			/* whatever is here should be ok */
7435331Samw 			break;
7445331Samw 		default:
7455331Samw 			break;
7465331Samw 		}
7475331Samw 	}
7485331Samw 
7495331Samw 	if (value != NULL)
7505331Samw 		sa_free_attr_string(value);
7515331Samw 	if (ret == SA_OK && optdefs[optindex].check != NULL)
7525331Samw 		/* do the property specific check */
7535331Samw 		ret = optdefs[optindex].check(property);
7545331Samw 
7555331Samw 	if (propname != NULL)
7565331Samw 		sa_free_attr_string(propname);
7575331Samw 	return (ret);
7585331Samw }
7595331Samw 
7605331Samw /*
7615331Samw  * Protocol management functions
7625331Samw  *
7635331Samw  * properties defined in the default files are defined in
7645331Samw  * proto_option_defs for parsing and validation.
7655331Samw  */
7665331Samw 
7675331Samw struct smb_proto_option_defs {
7685331Samw 	int smb_index;
7695331Samw 	int32_t minval;
7705331Samw 	int32_t maxval; /* In case of length of string this should be max */
7715331Samw 	int (*validator)(int, char *);
7725331Samw 	int32_t	refresh;
7735331Samw } smb_proto_options[] = {
7745772Sas200622 	{ SMB_CI_SYS_CMNT, 0, MAX_VALUE_BUFLEN,
7755772Sas200622 	    string_length_check_validator, SMB_REFRESH_REFRESH },
7765772Sas200622 	{ SMB_CI_MAX_WORKERS, 64, 1024, range_check_validator,
7775772Sas200622 	    SMB_REFRESH_REFRESH },
7785772Sas200622 	{ SMB_CI_NBSCOPE, 0, MAX_VALUE_BUFLEN,
7795772Sas200622 	    string_length_check_validator, 0 },
7805772Sas200622 	{ SMB_CI_LM_LEVEL, 2, 5, range_check_validator, 0 },
7815772Sas200622 	{ SMB_CI_KEEPALIVE, 20, 5400, range_check_validator_zero_ok,
7825772Sas200622 	    SMB_REFRESH_REFRESH },
7835772Sas200622 	{ SMB_CI_WINS_SRV1, 0, MAX_VALUE_BUFLEN,
7845772Sas200622 	    ip_address_validator_empty_ok, SMB_REFRESH_REFRESH },
7855772Sas200622 	{ SMB_CI_WINS_SRV2, 0, MAX_VALUE_BUFLEN,
7865772Sas200622 	    ip_address_validator_empty_ok, SMB_REFRESH_REFRESH },
7875772Sas200622 	{ SMB_CI_WINS_EXCL, 0, MAX_VALUE_BUFLEN,
7885772Sas200622 	    ip_address_csv_list_validator_empty_ok, SMB_REFRESH_REFRESH },
7895772Sas200622 	{ SMB_CI_SIGNING_ENABLE, 0, 0, true_false_validator,
7905772Sas200622 	    SMB_REFRESH_REFRESH },
7915772Sas200622 	{ SMB_CI_SIGNING_REQD, 0, 0, true_false_validator,
7925772Sas200622 	    SMB_REFRESH_REFRESH },
7935772Sas200622 	{ SMB_CI_RESTRICT_ANON, 0, 0, true_false_validator,
7945772Sas200622 	    SMB_REFRESH_REFRESH },
7955772Sas200622 	{ SMB_CI_DOMAIN_SRV, 0, MAX_VALUE_BUFLEN,
7965772Sas200622 	    ip_address_validator_empty_ok, 0 },
7975772Sas200622 	{ SMB_CI_ADS_SITE, 0, MAX_VALUE_BUFLEN,
7985772Sas200622 	    string_length_check_validator, SMB_REFRESH_REFRESH },
7995772Sas200622 	{ SMB_CI_DYNDNS_ENABLE, 0, 0, true_false_validator, 0 },
8005772Sas200622 	{ SMB_CI_AUTOHOME_MAP, 0, MAX_VALUE_BUFLEN, path_validator, 0 },
8015331Samw };
8025331Samw 
8035772Sas200622 #define	SMB_OPT_NUM \
8045772Sas200622 	(sizeof (smb_proto_options) / sizeof (smb_proto_options[0]))
8055772Sas200622 
8065331Samw /*
8075331Samw  * Check the range of value as int range.
8085331Samw  */
8095331Samw static int
8105331Samw range_check_validator(int index, char *value)
8115331Samw {
8125331Samw 	int ret = SA_OK;
8135331Samw 
8145331Samw 	if (!is_a_number(value)) {
8155331Samw 		ret = SA_BAD_VALUE;
8165331Samw 	} else {
8175331Samw 		int val;
8185331Samw 		val = strtoul(value, NULL, 0);
8195331Samw 		if (val < smb_proto_options[index].minval ||
8205331Samw 		    val > smb_proto_options[index].maxval)
8215331Samw 			ret = SA_BAD_VALUE;
8225331Samw 	}
8235331Samw 	return (ret);
8245331Samw }
8255331Samw 
8265331Samw /*
8275331Samw  * Check the range of value as int range.
8285331Samw  */
8295331Samw static int
8305331Samw range_check_validator_zero_ok(int index, char *value)
8315331Samw {
8325331Samw 	int ret = SA_OK;
8335331Samw 
8345331Samw 	if (!is_a_number(value)) {
8355331Samw 		ret = SA_BAD_VALUE;
8365331Samw 	} else {
8375331Samw 		int val;
8385331Samw 		val = strtoul(value, NULL, 0);
8395331Samw 		if (val == 0)
8405331Samw 			ret = SA_OK;
8415331Samw 		else {
8425331Samw 			if (val < smb_proto_options[index].minval ||
8435331Samw 			    val > smb_proto_options[index].maxval)
8445331Samw 			ret = SA_BAD_VALUE;
8455331Samw 		}
8465331Samw 	}
8475331Samw 	return (ret);
8485331Samw }
8495331Samw 
8505331Samw /*
8515331Samw  * Check the length of the string
8525331Samw  */
8535331Samw static int
8545331Samw string_length_check_validator(int index, char *value)
8555331Samw {
8565331Samw 	int ret = SA_OK;
8575331Samw 
8585331Samw 	if (value == NULL)
8595331Samw 		return (SA_BAD_VALUE);
8605331Samw 	if (strlen(value) > smb_proto_options[index].maxval)
8615331Samw 		ret = SA_BAD_VALUE;
8625331Samw 	return (ret);
8635331Samw }
8645331Samw 
8655331Samw /*
8665331Samw  * Check yes/no
8675331Samw  */
8685331Samw /*ARGSUSED*/
8695331Samw static int
8705331Samw true_false_validator(int index, char *value)
8715331Samw {
8725331Samw 	if (value == NULL)
8735331Samw 		return (SA_BAD_VALUE);
8745331Samw 	if ((strcasecmp(value, "true") == 0) ||
8755331Samw 	    (strcasecmp(value, "false") == 0))
8765331Samw 		return (SA_OK);
8775331Samw 	return (SA_BAD_VALUE);
8785331Samw }
8795331Samw 
8805331Samw /*
8815331Samw  * Check IP address.
8825331Samw  */
8835331Samw /*ARGSUSED*/
8845331Samw static int
8855331Samw ip_address_validator_empty_ok(int index, char *value)
8865331Samw {
8875331Samw 	char sbytes[16];
8885331Samw 	int len;
8895331Samw 
8905331Samw 	if (value == NULL)
8915331Samw 		return (SA_OK);
8925331Samw 	len = strlen(value);
8935331Samw 	if (len == 0)
8945331Samw 		return (SA_OK);
8955331Samw 	if (inet_pton(AF_INET, value, (void *)sbytes) != 1)
8965331Samw 		return (SA_BAD_VALUE);
8975331Samw 
8985331Samw 	return (SA_OK);
8995331Samw }
9005331Samw 
9015331Samw /*
9025331Samw  * Check IP address list
9035331Samw  */
9045331Samw /*ARGSUSED*/
9055331Samw static int
9065331Samw ip_address_csv_list_validator_empty_ok(int index, char *value)
9075331Samw {
9085331Samw 	char sbytes[16];
9095331Samw 	char *ip, *tmp, *ctx;
9105331Samw 
9115331Samw 	if (value == NULL || *value == '\0')
9125331Samw 		return (SA_OK);
9135331Samw 
9145331Samw 	if (strlen(value) > MAX_VALUE_BUFLEN)
9155331Samw 		return (SA_BAD_VALUE);
9165331Samw 
9175331Samw 	if ((tmp = strdup(value)) == NULL)
9185331Samw 		return (SA_NO_MEMORY);
9195331Samw 
9205331Samw 	ip = strtok_r(tmp, ",", &ctx);
9215331Samw 	while (ip) {
9225331Samw 		if (strlen(ip) == 0) {
9235331Samw 			free(tmp);
9245331Samw 			return (SA_BAD_VALUE);
9255331Samw 		}
9265331Samw 		if (*ip != 0) {
9275331Samw 			if (inet_pton(AF_INET, ip,
9285331Samw 			    (void *)sbytes) != 1) {
9295331Samw 				free(tmp);
9305331Samw 				return (SA_BAD_VALUE);
9315331Samw 			}
9325331Samw 		}
9335331Samw 		ip = strtok_r(0, ",", &ctx);
9345331Samw 	}
9355331Samw 
9365331Samw 	free(tmp);
9375331Samw 	return (SA_OK);
9385331Samw }
9395331Samw 
9405331Samw /*
9415331Samw  * Check path
9425331Samw  */
9435331Samw /*ARGSUSED*/
9445331Samw static int
9455331Samw path_validator(int index, char *value)
9465331Samw {
9475331Samw 	struct stat buffer;
9485331Samw 	int fd, status;
9495331Samw 
9505331Samw 	if (value == NULL)
9515331Samw 		return (SA_BAD_VALUE);
9525331Samw 
9535331Samw 	fd = open(value, O_RDONLY);
9545331Samw 	if (fd < 0)
9555331Samw 		return (SA_BAD_VALUE);
9565331Samw 
9575331Samw 	status = fstat(fd, &buffer);
9585331Samw 	(void) close(fd);
9595331Samw 
9605331Samw 	if (status < 0)
9615331Samw 		return (SA_BAD_VALUE);
9625331Samw 
9635331Samw 	if (buffer.st_mode & S_IFDIR)
9645331Samw 		return (SA_OK);
9655331Samw 	return (SA_BAD_VALUE);
9665331Samw }
9675331Samw 
9685331Samw /*
9695331Samw  * the protoset holds the defined options so we don't have to read
9705331Samw  * them multiple times
9715331Samw  */
9725331Samw static sa_protocol_properties_t protoset;
9735331Samw 
9745331Samw static int
9755331Samw findprotoopt(char *name)
9765331Samw {
9775331Samw 	int i;
9785772Sas200622 	char *sc_name;
9795772Sas200622 
9805772Sas200622 	for (i = 0; i < SMB_OPT_NUM; i++) {
9815772Sas200622 		sc_name = smb_config_getname(smb_proto_options[i].smb_index);
9825772Sas200622 		if (strcasecmp(sc_name, name) == 0)
9835331Samw 			return (i);
9845331Samw 	}
9855772Sas200622 
9865331Samw 	return (-1);
9875331Samw }
9885331Samw 
9895331Samw /*
9905331Samw  * smb_load_proto_properties()
9915331Samw  *
9925331Samw  * read the smb config values from SMF.
9935331Samw  */
9945331Samw 
9955331Samw static int
9965331Samw smb_load_proto_properties()
9975331Samw {
9985331Samw 	sa_property_t prop;
9995772Sas200622 	char value[MAX_VALUE_BUFLEN];
10005772Sas200622 	char *name;
10015331Samw 	int index;
1002*6019Sdougm 	int ret = SA_OK;
10035772Sas200622 	int rc;
10045331Samw 
10055331Samw 	protoset = sa_create_protocol_properties(SMB_PROTOCOL_NAME);
10065331Samw 	if (protoset == NULL)
10075331Samw 		return (SA_NO_MEMORY);
10085331Samw 
1009*6019Sdougm 	for (index = 0; index < SMB_OPT_NUM && ret == SA_OK; index++) {
10105772Sas200622 		rc = smb_config_get(smb_proto_options[index].smb_index,
10115772Sas200622 		    value, sizeof (value));
10125772Sas200622 		if (rc != SMBD_SMF_OK)
10135772Sas200622 			continue;
10145772Sas200622 		name = smb_config_getname(smb_proto_options[index].smb_index);
10155772Sas200622 		prop = sa_create_property(name, value);
10165454Sdougm 		if (prop != NULL)
1017*6019Sdougm 			ret = sa_add_protocol_property(protoset, prop);
1018*6019Sdougm 		else
1019*6019Sdougm 			ret = SA_NO_MEMORY;
10205331Samw 	}
1021*6019Sdougm 	return (ret);
10225331Samw }
10235331Samw 
10245331Samw /*
10255331Samw  * smb_share_init()
10265331Samw  *
10275331Samw  * Initialize the smb plugin.
10285331Samw  */
10295331Samw 
10305331Samw static int
10315331Samw smb_share_init(void)
10325331Samw {
10335331Samw 	int ret = SA_OK;
10345331Samw 
10355331Samw 	if (sa_plugin_ops.sa_init != smb_share_init)
10365331Samw 		return (SA_SYSTEM_ERR);
10375331Samw 
1038*6019Sdougm 	ret = smb_load_proto_properties();
10395331Samw 
10405331Samw 	return (ret);
10415331Samw }
10425331Samw 
10435331Samw /*
10445331Samw  * smb_share_fini()
10455331Samw  *
10465331Samw  */
10475331Samw static void
10485331Samw smb_share_fini(void)
10495331Samw {
10505331Samw 	xmlFreeNode(protoset);
10515331Samw 	protoset = NULL;
10525772Sas200622 
10535772Sas200622 	(void) lmshrd_door_close();
10545331Samw }
10555331Samw 
10565331Samw /*
10575331Samw  * smb_get_proto_set()
10585331Samw  *
10595331Samw  * Return an optionset with all the protocol specific properties in
10605331Samw  * it.
10615331Samw  */
10625331Samw static sa_protocol_properties_t
10635331Samw smb_get_proto_set(void)
10645331Samw {
10655331Samw 	return (protoset);
10665331Samw }
10675331Samw 
10685331Samw /*
10695772Sas200622  * smb_enable_dependencies()
10705772Sas200622  *
10715772Sas200622  * SMBD_DEFAULT_INSTANCE_FMRI may have some dependencies that aren't
10725772Sas200622  * enabled. This will attempt to enable all of them.
10735772Sas200622  */
10745772Sas200622 static void
10755772Sas200622 smb_enable_dependencies(const char *fmri)
10765772Sas200622 {
10775772Sas200622 	scf_handle_t *handle;
10785772Sas200622 	scf_service_t *service;
10795772Sas200622 	scf_instance_t *inst = NULL;
10805772Sas200622 	scf_iter_t *iter;
10815772Sas200622 	scf_property_t *prop;
10825772Sas200622 	scf_value_t *value;
10835772Sas200622 	scf_propertygroup_t *pg;
10845772Sas200622 	scf_scope_t *scope;
10855772Sas200622 	char type[SCFTYPE_LEN];
10865772Sas200622 	char *dependency;
10875772Sas200622 	char *servname;
10885772Sas200622 	int maxlen;
10895772Sas200622 
10905772Sas200622 	/*
10915772Sas200622 	 * Get all required handles and storage.
10925772Sas200622 	 */
10935772Sas200622 	handle = scf_handle_create(SCF_VERSION);
10945772Sas200622 	if (handle == NULL)
10955772Sas200622 		return;
10965772Sas200622 
10975772Sas200622 	if (scf_handle_bind(handle) != 0) {
10985772Sas200622 		scf_handle_destroy(handle);
10995772Sas200622 		return;
11005772Sas200622 	}
11015772Sas200622 
11025772Sas200622 	maxlen = scf_limit(SCF_LIMIT_MAX_VALUE_LENGTH);
11035772Sas200622 	if (maxlen == (ssize_t)-1)
11045772Sas200622 		maxlen = MAXPATHLEN;
11055772Sas200622 
11065772Sas200622 	dependency = malloc(maxlen);
11075772Sas200622 
11085772Sas200622 	service = scf_service_create(handle);
11095772Sas200622 
11105772Sas200622 	iter = scf_iter_create(handle);
11115772Sas200622 
11125772Sas200622 	pg = scf_pg_create(handle);
11135772Sas200622 
11145772Sas200622 	prop = scf_property_create(handle);
11155772Sas200622 
11165772Sas200622 	value = scf_value_create(handle);
11175772Sas200622 
11185772Sas200622 	scope = scf_scope_create(handle);
11195772Sas200622 
11205772Sas200622 	if (service == NULL || iter == NULL || pg == NULL || prop == NULL ||
11215772Sas200622 	    value == NULL || scope == NULL || dependency == NULL)
11225772Sas200622 		goto done;
11235772Sas200622 
11245772Sas200622 	/*
11255772Sas200622 	 *  We passed in the FMRI for the default instance but for
11265772Sas200622 	 *  some things we need the simple form so construct it. Since
11275772Sas200622 	 *  we reuse the storage that dependency points to, we need to
11285772Sas200622 	 *  use the servname early.
11295772Sas200622 	 */
11305772Sas200622 	(void) snprintf(dependency, maxlen, "%s", fmri + sizeof ("svc:"));
11315772Sas200622 	servname = strrchr(dependency, ':');
11325772Sas200622 	if (servname == NULL)
11335772Sas200622 		goto done;
11345772Sas200622 	*servname = '\0';
11355772Sas200622 	servname = dependency;
11365772Sas200622 
11375772Sas200622 	/*
11385772Sas200622 	 * Setup to iterate over the service property groups, only
11395772Sas200622 	 * looking at those that are "dependency" types. The "entity"
11405772Sas200622 	 * property will have the FMRI of the service we are dependent
11415772Sas200622 	 * on.
11425772Sas200622 	 */
11435772Sas200622 	if (scf_handle_get_scope(handle, SCF_SCOPE_LOCAL, scope) != 0)
11445772Sas200622 		goto done;
11455772Sas200622 
11465772Sas200622 	if (scf_scope_get_service(scope, servname, service) != 0)
11475772Sas200622 		goto done;
11485772Sas200622 
11495772Sas200622 	if (scf_iter_service_pgs(iter, service) != 0)
11505772Sas200622 		goto done;
11515772Sas200622 
11525772Sas200622 	while (scf_iter_next_pg(iter, pg) > 0) {
11535772Sas200622 		char *services[2];
11545772Sas200622 		/*
11555772Sas200622 		 * Have a property group for the service. See if it is
11565772Sas200622 		 * a dependency pg and only do operations on those.
11575772Sas200622 		 */
11585772Sas200622 		if (scf_pg_get_type(pg, type, SCFTYPE_LEN) <= 0)
11595772Sas200622 			continue;
11605772Sas200622 
11615772Sas200622 		if (strncmp(type, SCF_GROUP_DEPENDENCY, SCFTYPE_LEN) != 0)
11625772Sas200622 			continue;
11635772Sas200622 		/*
11645772Sas200622 		 * Have a dependency.  Attempt to enable it.
11655772Sas200622 		 */
11665772Sas200622 		if (scf_pg_get_property(pg, SCF_PROPERTY_ENTITIES, prop) != 0)
11675772Sas200622 			continue;
11685772Sas200622 
11695772Sas200622 		if (scf_property_get_value(prop, value) != 0)
11705772Sas200622 			continue;
11715772Sas200622 
11725772Sas200622 		services[1] = NULL;
11735772Sas200622 
11745772Sas200622 		if (scf_value_get_as_string(value, dependency, maxlen) > 0) {
11755772Sas200622 			services[0] = dependency;
11765772Sas200622 			_check_services(services);
11775772Sas200622 		}
11785772Sas200622 	}
11795772Sas200622 
11805772Sas200622 done:
11815772Sas200622 	if (dependency != NULL)
11825772Sas200622 		free(dependency);
11835772Sas200622 	if (value != NULL)
11845772Sas200622 		scf_value_destroy(value);
11855772Sas200622 	if (prop != NULL)
11865772Sas200622 		scf_property_destroy(prop);
11875772Sas200622 	if (pg != NULL)
11885772Sas200622 		scf_pg_destroy(pg);
11895772Sas200622 	if (iter != NULL)
11905772Sas200622 		scf_iter_destroy(iter);
11915772Sas200622 	if (scope != NULL)
11925772Sas200622 		scf_scope_destroy(scope);
11935772Sas200622 	if (inst != NULL)
11945772Sas200622 		scf_instance_destroy(inst);
11955772Sas200622 	if (service != NULL)
11965772Sas200622 		scf_service_destroy(service);
11975772Sas200622 
11985772Sas200622 	(void) scf_handle_unbind(handle);
11995772Sas200622 	scf_handle_destroy(handle);
12005772Sas200622 }
12015772Sas200622 
12025772Sas200622 /*
12035331Samw  * How long to wait for service to come online
12045331Samw  */
12055331Samw #define	WAIT_FOR_SERVICE	15
12065331Samw 
12075331Samw /*
12085331Samw  * smb_enable_service()
12095331Samw  *
12105331Samw  */
12115331Samw static int
12125331Samw smb_enable_service(void)
12135331Samw {
12145331Samw 	int i;
12155331Samw 	int ret = SA_OK;
12165772Sas200622 	char *service[] = { SMBD_DEFAULT_INSTANCE_FMRI, NULL };
12175331Samw 
12185331Samw 	if (!smb_isonline()) {
12195772Sas200622 		/*
12205772Sas200622 		 * Attempt to start the idmap, and other dependent
12215772Sas200622 		 * services, first.  If it fails, the SMB service will
12225772Sas200622 		 * ultimately fail so we use that as the error.  If we
12235772Sas200622 		 * don't try to enable idmap, smb won't start the
12245772Sas200622 		 * first time unless the admin has done it
12255772Sas200622 		 * manually. The service could be administratively
12265772Sas200622 		 * disabled so we won't always get started.
12275772Sas200622 		 */
12285772Sas200622 		smb_enable_dependencies(SMBD_DEFAULT_INSTANCE_FMRI);
12295772Sas200622 		_check_services(service);
12305331Samw 
12315331Samw 		/* Wait for service to come online */
12325331Samw 		for (i = 0; i < WAIT_FOR_SERVICE; i++) {
12335331Samw 			if (smb_isonline()) {
12345772Sas200622 				ret =  SA_OK;
12355331Samw 				break;
12365331Samw 			} else {
12375331Samw 				ret = SA_BUSY;
12385331Samw 				(void) sleep(1);
12395331Samw 			}
12405331Samw 		}
12415331Samw 	}
12425331Samw 	return (ret);
12435331Samw }
12445331Samw 
12455331Samw /*
12465331Samw  * smb_validate_proto_prop(index, name, value)
12475331Samw  *
12485331Samw  * Verify that the property specified by name can take the new
12495331Samw  * value. This is a sanity check to prevent bad values getting into
12505331Samw  * the default files.
12515331Samw  */
12525331Samw static int
12535331Samw smb_validate_proto_prop(int index, char *name, char *value)
12545331Samw {
12555331Samw 	if ((name == NULL) || (index < 0))
12565331Samw 		return (SA_BAD_VALUE);
12575331Samw 
12585331Samw 	if (smb_proto_options[index].validator == NULL)
12595331Samw 		return (SA_OK);
12605331Samw 
12615331Samw 	if (smb_proto_options[index].validator(index, value) == SA_OK)
12625331Samw 		return (SA_OK);
12635331Samw 	return (SA_BAD_VALUE);
12645331Samw }
12655331Samw 
12665331Samw /*
12675331Samw  * smb_set_proto_prop(prop)
12685331Samw  *
12695331Samw  * check that prop is valid.
12705331Samw  */
12715331Samw /*ARGSUSED*/
12725331Samw static int
12735331Samw smb_set_proto_prop(sa_property_t prop)
12745331Samw {
12755331Samw 	int ret = SA_OK;
12765331Samw 	char *name;
12775331Samw 	char *value;
12785331Samw 	int index = -1;
12795521Sas200622 	struct smb_proto_option_defs *opt;
12805331Samw 
12815331Samw 	name = sa_get_property_attr(prop, "type");
12825331Samw 	value = sa_get_property_attr(prop, "value");
12835331Samw 	if (name != NULL && value != NULL) {
12845331Samw 		index = findprotoopt(name);
12855331Samw 		if (index >= 0) {
12865331Samw 			/* should test for valid value */
12875331Samw 			ret = smb_validate_proto_prop(index, name, value);
12885331Samw 			if (ret == SA_OK) {
12895521Sas200622 				opt = &smb_proto_options[index];
12905521Sas200622 
12915331Samw 				/* Save to SMF */
12925772Sas200622 				(void) smb_config_set(opt->smb_index, value);
12935331Samw 				/*
12945331Samw 				 * Specialized refresh mechanisms can
12955331Samw 				 * be flagged in the proto_options and
12965331Samw 				 * processed here.
12975331Samw 				 */
12985521Sas200622 				if (opt->refresh & SMB_REFRESH_REFRESH)
12995521Sas200622 					(void) smb_config_refresh();
13005521Sas200622 				else if (opt->refresh & SMB_REFRESH_RESTART)
13015331Samw 					(void) smf_restart_instance(
13025331Samw 					    SMBD_DEFAULT_INSTANCE_FMRI);
13035331Samw 			}
13045331Samw 		}
13055331Samw 	}
13065521Sas200622 
13075331Samw 	if (name != NULL)
13085331Samw 		sa_free_attr_string(name);
13095331Samw 	if (value != NULL)
13105331Samw 		sa_free_attr_string(value);
13115331Samw 
13125331Samw 	return (ret);
13135331Samw }
13145331Samw 
13155331Samw /*
13165331Samw  * smb_get_status()
13175331Samw  *
13185331Samw  * What is the current status of the smbd? We use the SMF state here.
13195331Samw  * Caller must free the returned value.
13205331Samw  */
13215331Samw 
13225331Samw static char *
13235331Samw smb_get_status(void)
13245331Samw {
13255331Samw 	char *state = NULL;
13265331Samw 	state = smf_get_state(SMBD_DEFAULT_INSTANCE_FMRI);
13275331Samw 	return (state != NULL ? state : "-");
13285331Samw }
13295331Samw 
13305331Samw /*
13315331Samw  * This protocol plugin require resource names
13325331Samw  */
13335331Samw static uint64_t
13345331Samw smb_share_features(void)
13355331Samw {
13365331Samw 	return (SA_FEATURE_RESOURCE | SA_FEATURE_ALLOWSUBDIRS |
13375331Samw 	    SA_FEATURE_ALLOWPARDIRS);
13385331Samw }
13395331Samw 
13405331Samw /*
13415331Samw  * This should be used to convert lmshare_info to sa_resource_t
13425331Samw  * Should only be needed to build temp shares/resources to be
13435331Samw  * supplied to sharemanager to display temp shares.
13445331Samw  */
13455331Samw static int
13465331Samw smb_build_tmp_sa_resource(sa_handle_t handle, lmshare_info_t *si)
13475331Samw {
13485331Samw 	int err;
13495331Samw 	sa_share_t share;
13505331Samw 	sa_group_t group;
13515331Samw 	sa_resource_t resource;
13525331Samw 
13535331Samw 	if (si == NULL)
13545331Samw 		return (SA_INVALID_NAME);
13555331Samw 
13565331Samw 	/*
13575331Samw 	 * First determine if the "share path" is already shared
13585331Samw 	 * somewhere. If it is, we have to use it as the authority on
13595331Samw 	 * where the transient share lives so will use it's parent
13605331Samw 	 * group. If it doesn't exist, it needs to land in "smb".
13615331Samw 	 */
13625331Samw 
13635331Samw 	share = sa_find_share(handle, si->directory);
13645331Samw 	if (share != NULL) {
13655331Samw 		group = sa_get_parent_group(share);
13665331Samw 	} else {
13675331Samw 		group = smb_get_smb_share_group(handle);
13685331Samw 		if (group == NULL)
13695331Samw 			return (SA_NO_SUCH_GROUP);
13705331Samw 		share = sa_get_share(group, si->directory);
13715331Samw 		if (share == NULL) {
13725331Samw 			share = sa_add_share(group, si->directory,
13735331Samw 			    SA_SHARE_TRANSIENT, &err);
13745331Samw 			if (share == NULL)
13755331Samw 				return (SA_NO_SUCH_PATH);
13765331Samw 		}
13775331Samw 	}
13785331Samw 
13795331Samw 	/*
13805331Samw 	 * Now handle the resource. Make sure that the resource is
13815331Samw 	 * transient and added to the share.
13825331Samw 	 */
13835331Samw 	resource = sa_get_share_resource(share, si->share_name);
13845331Samw 	if (resource == NULL) {
13855331Samw 		resource = sa_add_resource(share,
13865331Samw 		    si->share_name, SA_SHARE_TRANSIENT, &err);
13875331Samw 		if (resource == NULL)
13885331Samw 			return (SA_NO_SUCH_RESOURCE);
13895331Samw 	}
13905331Samw 
13915331Samw 	/* set resource attributes now */
13925331Samw 	(void) sa_set_resource_attr(resource, "description", si->comment);
13935331Samw 	(void) sa_set_resource_attr(resource, SHOPT_AD_CONTAINER,
13945331Samw 	    si->container);
13955331Samw 
13965331Samw 	return (SA_OK);
13975331Samw }
13985331Samw 
13995331Samw /*
14005331Samw  * Return smb transient shares.  Note that we really want to look at
14015331Samw  * all current shares from SMB in order to determine this. Transient
14025331Samw  * shares should be those that don't appear in either the SMF or ZFS
14035331Samw  * configurations.  Those that are in the repositories will be
14045331Samw  * filtered out by smb_build_tmp_sa_resource.
14055331Samw  */
14065331Samw static int
14075331Samw smb_list_transient(sa_handle_t handle)
14085331Samw {
14095331Samw 	int i, offset, num;
14105331Samw 	lmshare_list_t list;
14115331Samw 	int res;
14125331Samw 
14135331Samw 	num = lmshrd_num_shares();
14145331Samw 	if (num <= 0)
14155331Samw 		return (SA_OK);
14165331Samw 	offset = 0;
14175331Samw 	while (lmshrd_list(offset, &list) != NERR_InternalError) {
14185331Samw 		if (list.no == 0)
14195331Samw 			break;
14205331Samw 		for (i = 0; i < list.no; i++) {
14215331Samw 			res = smb_build_tmp_sa_resource(handle,
14225331Samw 			    &(list.smbshr[i]));
14235331Samw 			if (res != SA_OK)
14245331Samw 				return (res);
14255331Samw 		}
14265331Samw 		offset += list.no;
14275331Samw 	}
14285331Samw 
14295331Samw 	return (SA_OK);
14305331Samw }
14315331Samw 
14325331Samw /*
14335331Samw  * fix_resource_name(share, name,  prefix)
14345331Samw  *
14355331Samw  * Construct a name where the ZFS dataset has the prefix replaced with "name".
14365331Samw  */
14375331Samw static char *
14385331Samw fix_resource_name(sa_share_t share, char *name, char *prefix)
14395331Samw {
14405331Samw 	char *dataset = NULL;
14415331Samw 	char *newname = NULL;
14425331Samw 	size_t psize;
14435331Samw 	size_t nsize;
14445331Samw 
14455331Samw 	dataset = sa_get_share_attr(share, "dataset");
14465331Samw 
14475331Samw 	if (dataset != NULL && strcmp(dataset, prefix) != 0) {
14485331Samw 		psize = strlen(prefix);
14495331Samw 		if (strncmp(dataset, prefix, psize) == 0) {
14505331Samw 			/* need string plus ',' and NULL */
14515331Samw 			nsize = (strlen(dataset) - psize) + strlen(name) + 2;
14525331Samw 			newname = calloc(nsize, 1);
14535331Samw 			if (newname != NULL) {
14545331Samw 				(void) snprintf(newname, nsize, "%s%s", name,
14555331Samw 				    dataset + psize);
14565331Samw 				sa_fix_resource_name(newname);
14575331Samw 			}
14585331Samw 			sa_free_attr_string(dataset);
14595331Samw 			return (newname);
14605331Samw 		}
14615331Samw 	}
14625331Samw 	if (dataset != NULL)
14635331Samw 		sa_free_attr_string(dataset);
14645331Samw 	return (strdup(name));
14655331Samw }
14665331Samw 
14675331Samw /*
14685331Samw  * smb_parse_optstring(group, options)
14695331Samw  *
14705331Samw  * parse a compact option string into individual options. This allows
14715331Samw  * ZFS sharesmb and sharemgr "share" command to work.  group can be a
14725331Samw  * group, a share or a resource.
14735331Samw  */
14745331Samw static int
14755331Samw smb_parse_optstring(sa_group_t group, char *options)
14765331Samw {
14775331Samw 	char *dup;
14785331Samw 	char *base;
14795331Samw 	char *lasts;
14805331Samw 	char *token;
14815331Samw 	sa_optionset_t optionset;
14825331Samw 	sa_group_t parent = NULL;
14835331Samw 	sa_resource_t resource = NULL;
14845331Samw 	int iszfs = 0;
14855331Samw 	int persist = 0;
14865331Samw 	int need_optionset = 0;
14875331Samw 	int ret = SA_OK;
14885331Samw 	sa_property_t prop;
14895331Samw 
14905331Samw 	/*
14915331Samw 	 * In order to not attempt to change ZFS properties unless
14925331Samw 	 * absolutely necessary, we never do it in the legacy parsing
14935331Samw 	 * so we need to keep track of this.
14945331Samw 	 */
14955331Samw 	if (sa_is_share(group)) {
14965331Samw 		char *zfs;
14975331Samw 
14985331Samw 		parent = sa_get_parent_group(group);
14995331Samw 		if (parent != NULL) {
15005331Samw 			zfs = sa_get_group_attr(parent, "zfs");
15015331Samw 			if (zfs != NULL) {
15025331Samw 				sa_free_attr_string(zfs);
15035331Samw 				iszfs = 1;
15045331Samw 			}
15055331Samw 		}
15065331Samw 	} else {
15075331Samw 		iszfs = sa_group_is_zfs(group);
15085331Samw 		/*
15095331Samw 		 * If a ZFS group, then we need to see if a resource
15105331Samw 		 * name is being set. If so, bail with
15115331Samw 		 * SA_PROP_SHARE_ONLY, so we come back in with a share
15125331Samw 		 * instead of a group.
15135331Samw 		 */
15145331Samw 		if (strncmp(options, "name=", sizeof ("name=") - 1) == 0 ||
15155331Samw 		    strstr(options, ",name=") != NULL) {
15165331Samw 			return (SA_PROP_SHARE_ONLY);
15175331Samw 		}
15185331Samw 	}
15195331Samw 
15205331Samw 	/* do we have an existing optionset? */
15215331Samw 	optionset = sa_get_optionset(group, "smb");
15225331Samw 	if (optionset == NULL) {
15235331Samw 		/* didn't find existing optionset so create one */
15245331Samw 		optionset = sa_create_optionset(group, "smb");
15255331Samw 		if (optionset == NULL)
15265331Samw 			return (SA_NO_MEMORY);
15275331Samw 	} else {
15285331Samw 		/*
15295331Samw 		 * If an optionset already exists, we've come through
15305331Samw 		 * twice so ignore the second time.
15315331Samw 		 */
15325331Samw 		return (ret);
15335331Samw 	}
15345331Samw 
15355331Samw 	/* We need a copy of options for the next part. */
15365331Samw 	dup = strdup(options);
15375331Samw 	if (dup == NULL)
15385331Samw 		return (SA_NO_MEMORY);
15395331Samw 
15405331Samw 	/*
15415331Samw 	 * SMB properties are straightforward and are strings,
15425331Samw 	 * integers or booleans.  Properties are separated by
15435331Samw 	 * commas. It will be necessary to parse quotes due to some
15445331Samw 	 * strings not having a restricted characters set.
15455331Samw 	 *
15465331Samw 	 * Note that names will create a resource. For now, if there
15475331Samw 	 * is a set of properties "before" the first name="", those
15485331Samw 	 * properties will be placed on the group.
15495331Samw 	 */
15505331Samw 	persist = sa_is_persistent(group);
15515331Samw 	base = dup;
15525331Samw 	token = dup;
15535331Samw 	lasts = NULL;
15545331Samw 	while (token != NULL && ret == SA_OK) {
15555331Samw 		ret = SA_OK;
15565331Samw 		token = strtok_r(base, ",", &lasts);
15575331Samw 		base = NULL;
15585331Samw 		if (token != NULL) {
15595331Samw 			char *value;
15605331Samw 			/*
15615331Samw 			 * All SMB properties have values so there
15625331Samw 			 * MUST be an '=' character.  If it doesn't,
15635331Samw 			 * it is a syntax error.
15645331Samw 			 */
15655331Samw 			value = strchr(token, '=');
15665331Samw 			if (value != NULL) {
15675331Samw 				*value++ = '\0';
15685331Samw 			} else {
15695331Samw 				ret = SA_SYNTAX_ERR;
15705331Samw 				break;
15715331Samw 			}
15725331Samw 			/*
15735331Samw 			 * We may need to handle a "name" property
15745331Samw 			 * that is a ZFS imposed resource name. Each
15755331Samw 			 * name would trigger getting a new "resource"
15765331Samw 			 * to put properties on. For now, assume no
15775331Samw 			 * "name" property for special handling.
15785331Samw 			 */
15795331Samw 
15805331Samw 			if (strcmp(token, "name") == 0) {
15815331Samw 				char *prefix;
15825331Samw 				char *name = NULL;
15835331Samw 				/*
15845331Samw 				 * We have a name, so now work on the
15855331Samw 				 * resource level. We have a "share"
15865331Samw 				 * in "group" due to the caller having
15875331Samw 				 * added it. If we are called with a
15885331Samw 				 * group, the check for group/share
15895331Samw 				 * at the beginning of this function
15905331Samw 				 * will bail out the parse if there is a
15915331Samw 				 * "name" but no share.
15925331Samw 				 */
15935331Samw 				if (!iszfs) {
15945331Samw 					ret = SA_SYNTAX_ERR;
15955331Samw 					break;
15965331Samw 				}
15975331Samw 				/*
15985331Samw 				 * Make sure the parent group has the
15995331Samw 				 * "prefix" property since we will
16005331Samw 				 * need to use this for constructing
16015331Samw 				 * inherited name= values.
16025331Samw 				 */
16035331Samw 				prefix = sa_get_group_attr(parent, "prefix");
16045331Samw 				if (prefix == NULL) {
16055331Samw 					prefix = sa_get_group_attr(parent,
16065331Samw 					    "name");
16075331Samw 					if (prefix != NULL) {
16085331Samw 						(void) sa_set_group_attr(parent,
16095331Samw 						    "prefix", prefix);
16105331Samw 					}
16115331Samw 				}
16125331Samw 				name = fix_resource_name((sa_share_t)group,
16135331Samw 				    value, prefix);
16145331Samw 				if (name != NULL) {
16155331Samw 					resource = sa_add_resource(
16165331Samw 					    (sa_share_t)group, name,
16175331Samw 					    SA_SHARE_TRANSIENT, &ret);
16185331Samw 					sa_free_attr_string(name);
16195331Samw 				} else {
16205331Samw 					ret = SA_NO_MEMORY;
16215331Samw 				}
16225331Samw 				if (prefix != NULL)
16235331Samw 					sa_free_attr_string(prefix);
16245331Samw 
16255331Samw 				/* A resource level optionset is needed */
16265331Samw 
16275331Samw 				need_optionset = 1;
16285331Samw 				if (resource == NULL) {
16295331Samw 					ret = SA_NO_MEMORY;
16305331Samw 					break;
16315331Samw 				}
16325331Samw 				continue;
16335331Samw 			}
16345331Samw 
16355331Samw 			if (need_optionset) {
16365331Samw 				optionset = sa_create_optionset(resource,
16375331Samw 				    "smb");
16385331Samw 				need_optionset = 0;
16395331Samw 			}
16405331Samw 
16415331Samw 			prop = sa_create_property(token, value);
16425331Samw 			if (prop == NULL)
16435331Samw 				ret = SA_NO_MEMORY;
16445331Samw 			else
16455331Samw 				ret = sa_add_property(optionset, prop);
16465331Samw 			if (ret != SA_OK)
16475331Samw 				break;
16485331Samw 			if (!iszfs)
16495331Samw 				ret = sa_commit_properties(optionset, !persist);
16505331Samw 		}
16515331Samw 	}
16525331Samw 	free(dup);
16535331Samw 	return (ret);
16545331Samw }
16555331Samw 
16565331Samw /*
16575331Samw  * smb_sprint_option(rbuff, rbuffsize, incr, prop, sep)
16585331Samw  *
16595331Samw  * provides a mechanism to format SMB properties into legacy output
16605331Samw  * format. If the buffer would overflow, it is reallocated and grown
16615331Samw  * as appropriate. Special cases of converting internal form of values
16625331Samw  * to those used by "share" are done. this function does one property
16635331Samw  * at a time.
16645331Samw  */
16655331Samw 
16665331Samw static void
16675331Samw smb_sprint_option(char **rbuff, size_t *rbuffsize, size_t incr,
16685331Samw 			sa_property_t prop, int sep)
16695331Samw {
16705331Samw 	char *name;
16715331Samw 	char *value;
16725331Samw 	int curlen;
16735331Samw 	char *buff = *rbuff;
16745331Samw 	size_t buffsize = *rbuffsize;
16755331Samw 
16765331Samw 	name = sa_get_property_attr(prop, "type");
16775331Samw 	value = sa_get_property_attr(prop, "value");
16785331Samw 	if (buff != NULL)
16795331Samw 		curlen = strlen(buff);
16805331Samw 	else
16815331Samw 		curlen = 0;
16825331Samw 	if (name != NULL) {
16835331Samw 		int len;
16845331Samw 		len = strlen(name) + sep;
16855331Samw 
16865331Samw 		/*
16875331Samw 		 * A future RFE would be to replace this with more
16885331Samw 		 * generic code and to possibly handle more types.
16895331Samw 		 *
16905331Samw 		 * For now, everything else is treated as a string. If
16915331Samw 		 * we get any properties that aren't exactly
16925331Samw 		 * name/value pairs, we may need to
16935331Samw 		 * interpret/transform.
16945331Samw 		 */
16955331Samw 		if (value != NULL)
16965331Samw 			len += 1 + strlen(value);
16975331Samw 
16985331Samw 		while (buffsize <= (curlen + len)) {
16995331Samw 			/* need more room */
17005331Samw 			buffsize += incr;
17015331Samw 			buff = realloc(buff, buffsize);
17025331Samw 			*rbuff = buff;
17035331Samw 			*rbuffsize = buffsize;
17045331Samw 			if (buff == NULL) {
17055331Samw 				/* realloc failed so free everything */
17065331Samw 				if (*rbuff != NULL)
17075331Samw 					free(*rbuff);
17085331Samw 				goto err;
17095331Samw 			}
17105331Samw 		}
17115331Samw 		if (buff == NULL)
17125331Samw 			goto err;
17135331Samw 		(void) snprintf(buff + curlen, buffsize - curlen,
17145331Samw 		    "%s%s=%s", sep ? "," : "",
17155331Samw 		    name, value != NULL ? value : "\"\"");
17165331Samw 
17175331Samw 	}
17185331Samw err:
17195331Samw 	if (name != NULL)
17205331Samw 		sa_free_attr_string(name);
17215331Samw 	if (value != NULL)
17225331Samw 		sa_free_attr_string(value);
17235331Samw }
17245331Samw 
17255331Samw /*
17265331Samw  * smb_format_resource_options(resource, hier)
17275331Samw  *
17285331Samw  * format all the options on the group into a flattened option
17295331Samw  * string. If hier is non-zero, walk up the tree to get inherited
17305331Samw  * options.
17315331Samw  */
17325331Samw 
17335331Samw static char *
17345331Samw smb_format_options(sa_group_t group, int hier)
17355331Samw {
17365331Samw 	sa_optionset_t options = NULL;
17375331Samw 	sa_property_t prop;
17385331Samw 	int sep = 0;
17395331Samw 	char *buff;
17405331Samw 	size_t buffsize;
17415331Samw 
17425331Samw 
17435331Samw 	buff = malloc(OPT_CHUNK);
17445331Samw 	if (buff == NULL)
17455331Samw 		return (NULL);
17465331Samw 
17475331Samw 	buff[0] = '\0';
17485331Samw 	buffsize = OPT_CHUNK;
17495331Samw 
17505331Samw 	/*
17515331Samw 	 * We may have a an optionset relative to this item. format
17525331Samw 	 * these if we find them and then add any security definitions.
17535331Samw 	 */
17545331Samw 
17555331Samw 	options = sa_get_derived_optionset(group, "smb", hier);
17565331Samw 
17575331Samw 	/*
17585331Samw 	 * do the default set first but skip any option that is also
17595331Samw 	 * in the protocol specific optionset.
17605331Samw 	 */
17615331Samw 	if (options != NULL) {
17625331Samw 		for (prop = sa_get_property(options, NULL);
17635331Samw 		    prop != NULL; prop = sa_get_next_property(prop)) {
17645331Samw 			/*
17655331Samw 			 * use this one since we skipped any
17665331Samw 			 * of these that were also in
17675331Samw 			 * optdefault
17685331Samw 			 */
17695331Samw 			smb_sprint_option(&buff, &buffsize, OPT_CHUNK,
17705331Samw 			    prop, sep);
17715331Samw 			if (buff == NULL) {
17725331Samw 				/*
17735331Samw 				 * buff could become NULL if there
17745331Samw 				 * isn't enough memory for
17755331Samw 				 * smb_sprint_option to realloc()
17765331Samw 				 * as necessary. We can't really
17775331Samw 				 * do anything about it at this
17785331Samw 				 * point so we return NULL.  The
17795331Samw 				 * caller should handle the
17805331Samw 				 * failure.
17815331Samw 				 */
17825331Samw 				if (options != NULL)
17835331Samw 					sa_free_derived_optionset(
17845331Samw 					    options);
17855331Samw 				return (buff);
17865331Samw 			}
17875331Samw 			sep = 1;
17885331Samw 		}
17895331Samw 	}
17905331Samw 
17915331Samw 	if (options != NULL)
17925331Samw 		sa_free_derived_optionset(options);
17935331Samw 	return (buff);
17945331Samw }
17955331Samw 
17965331Samw /*
17975331Samw  * smb_rename_resource(resource, newname)
17985331Samw  *
17995331Samw  * Change the current exported name of the resource to newname.
18005331Samw  */
18015331Samw /*ARGSUSED*/
18025331Samw int
18035331Samw smb_rename_resource(sa_handle_t handle, sa_resource_t resource, char *newname)
18045331Samw {
18055331Samw 	int ret = SA_OK;
18065331Samw 	int err;
18075331Samw 	char *oldname;
18085331Samw 
18095331Samw 	oldname = sa_get_resource_attr(resource, "name");
18105331Samw 	if (oldname == NULL)
18115331Samw 		return (SA_NO_SUCH_RESOURCE);
18125331Samw 
18135331Samw 	err = lmshrd_rename(oldname, newname);
18145331Samw 
18155331Samw 	/* improve error values somewhat */
18165331Samw 	switch (err) {
18175331Samw 	case NERR_Success:
18185331Samw 		break;
18195331Samw 	case NERR_InternalError:
18205331Samw 		ret = SA_SYSTEM_ERR;
18215331Samw 		break;
18225331Samw 	case NERR_DuplicateShare:
18235331Samw 		ret = SA_DUPLICATE_NAME;
18245331Samw 		break;
18255331Samw 	default:
18265331Samw 		ret = SA_CONFIG_ERR;
18275331Samw 		break;
18285331Samw 	}
18295331Samw 
18305331Samw 	return (ret);
18315331Samw }
1832