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 /*
2966030Sjb150015  * smb_ismaint()
2976030Sjb150015  *
2986030Sjb150015  * Determine if the SMF service instance is in the disabled state or
2996030Sjb150015  * not. A number of operations depend on this state.
3006030Sjb150015  */
3016030Sjb150015 static boolean_t
3026030Sjb150015 smb_ismaint(void)
3036030Sjb150015 {
3046030Sjb150015 	char *str;
3056030Sjb150015 	boolean_t ret = B_FALSE;
3066030Sjb150015 
3076030Sjb150015 	if ((str = smf_get_state(SMBD_DEFAULT_INSTANCE_FMRI)) != NULL) {
3086030Sjb150015 		ret = (strcmp(str, SCF_STATE_STRING_MAINT) == 0);
3096030Sjb150015 		free(str);
3106030Sjb150015 	}
3116030Sjb150015 	return (ret);
3126030Sjb150015 }
3136030Sjb150015 
3146030Sjb150015 /*
3155331Samw  * smb_enable_share tells the implementation that it is to enable the share.
3165331Samw  * This entails converting the path and options into the appropriate ioctl
3175331Samw  * calls. It is assumed that all error checking of paths, etc. were
3185331Samw  * done earlier.
3195331Samw  */
3205331Samw static int
3215331Samw smb_enable_share(sa_share_t share)
3225331Samw {
3235331Samw 	char *path;
3245331Samw 	char *rname;
3255331Samw 	lmshare_info_t si;
3265331Samw 	sa_resource_t resource;
3275331Samw 	boolean_t iszfs;
3285331Samw 	boolean_t privileged;
3295331Samw 	int err = SA_OK;
3305331Samw 	priv_set_t *priv_effective;
3315331Samw 	boolean_t online;
3325331Samw 
3335331Samw 	priv_effective = priv_allocset();
3345331Samw 	(void) getppriv(PRIV_EFFECTIVE, priv_effective);
3355331Samw 	privileged = (priv_isfullset(priv_effective) == B_TRUE);
3365331Samw 	priv_freeset(priv_effective);
3375331Samw 
3385331Samw 	/* get the path since it is important in several places */
3395331Samw 	path = sa_get_share_attr(share, "path");
3405331Samw 	if (path == NULL)
3415331Samw 		return (SA_NO_SUCH_PATH);
3425331Samw 
3435772Sas200622 	/*
3445772Sas200622 	 * If administratively disabled, don't try to start anything.
3455772Sas200622 	 */
3465331Samw 	online = smb_isonline();
3475772Sas200622 	if (!online && !smb_isautoenable() && smb_isdisabled())
3485772Sas200622 		goto done;
3495331Samw 
3505331Samw 	iszfs = sa_path_is_zfs(path);
3515331Samw 
3525331Samw 	if (iszfs) {
3535331Samw 
3545331Samw 		if (privileged == B_FALSE && !online) {
3555331Samw 
3565331Samw 			if (!online) {
3575331Samw 				(void) printf(dgettext(TEXT_DOMAIN,
3585331Samw 				    "SMB: Cannot share remove "
3595331Samw 				    "file system: %s\n"), path);
3605331Samw 				(void) printf(dgettext(TEXT_DOMAIN,
3615331Samw 				    "SMB: Service needs to be enabled "
3625331Samw 				    "by a privileged user\n"));
3635331Samw 				err = SA_NO_PERMISSION;
3645331Samw 				errno = EPERM;
3655331Samw 			}
3665331Samw 			if (err) {
3675331Samw 				sa_free_attr_string(path);
3685331Samw 				return (err);
3695331Samw 			}
3705331Samw 
3715331Samw 		}
3725331Samw 	}
3735331Samw 
3745331Samw 	if (privileged == B_TRUE && !online) {
3755331Samw 		err = smb_enable_service();
3765331Samw 		if (err != SA_OK) {
3775331Samw 			(void) printf(dgettext(TEXT_DOMAIN,
3785331Samw 			    "SMB: Unable to enable service\n"));
3795331Samw 			/*
3805331Samw 			 * For now, it is OK to not be able to enable
3815331Samw 			 * the service.
3825331Samw 			 */
3836030Sjb150015 			if (err == SA_BUSY || err == SA_SYSTEM_ERR)
3845331Samw 				err = SA_OK;
3855331Samw 		} else {
3865331Samw 			online = B_TRUE;
3875331Samw 		}
3885331Samw 	}
3895331Samw 
3905331Samw 	/*
3915331Samw 	 * Don't bother trying to start shares if the service isn't
3925331Samw 	 * running.
3935331Samw 	 */
3945331Samw 	if (!online)
3955331Samw 		goto done;
3965331Samw 
3975331Samw 	/* Each share can have multiple resources */
3985331Samw 	for (resource = sa_get_share_resource(share, NULL);
3995331Samw 	    resource != NULL;
4005331Samw 	    resource = sa_get_next_resource(resource)) {
4015331Samw 		sa_optionset_t opts;
4025331Samw 		bzero(&si, sizeof (lmshare_info_t));
4035331Samw 		rname = sa_get_resource_attr(resource, "name");
4045331Samw 		if (rname == NULL) {
4055331Samw 			sa_free_attr_string(path);
4065331Samw 			return (SA_NO_SUCH_RESOURCE);
4075331Samw 		}
4085331Samw 
4095331Samw 		opts = sa_get_derived_optionset(resource, SMB_PROTOCOL_NAME, 1);
4105331Samw 		smb_build_lmshare_info(rname, path, opts, &si);
4115331Samw 		sa_free_attr_string(rname);
4125331Samw 
4135331Samw 		sa_free_derived_optionset(opts);
4145331Samw 		if (!iszfs) {
4155331Samw 			err = lmshrd_add(&si);
4165331Samw 		} else {
4175331Samw 			share_t sh;
4185331Samw 
4195331Samw 			sa_sharetab_fill_zfs(share, &sh, "smb");
4205331Samw 			err = sa_share_zfs(share, (char *)path, &sh,
4215331Samw 			    &si, ZFS_SHARE_SMB);
4225331Samw 
4235331Samw 			sa_emptyshare(&sh);
4245331Samw 		}
4255331Samw 	}
4265331Samw 	if (!iszfs)
4275331Samw 		(void) sa_update_sharetab(share, "smb");
4285331Samw done:
4295331Samw 	sa_free_attr_string(path);
4305331Samw 
4315331Samw 	return (err == NERR_DuplicateShare ? 0 : err);
4325331Samw }
4335331Samw 
4345331Samw /*
4355331Samw  * This is the share for CIFS all shares have resource names.
4365331Samw  * Enable tells the smb server to update its hash. If it fails
4375331Samw  * because smb server is down, we just ignore as smb server loads
4385331Samw  * the resources from sharemanager at startup.
4395331Samw  */
4405331Samw 
4415331Samw static int
4425331Samw smb_enable_resource(sa_resource_t resource)
4435331Samw {
4445331Samw 	char *path;
4455331Samw 	char *rname;
4465331Samw 	sa_optionset_t opts;
4475331Samw 	sa_share_t share;
4485331Samw 	lmshare_info_t si;
4495772Sas200622 	int ret = SA_OK;
4505772Sas200622 	int err;
4515772Sas200622 	boolean_t isonline;
4525331Samw 
4535331Samw 	share = sa_get_resource_parent(resource);
4545331Samw 	if (share == NULL)
4555331Samw 		return (SA_NO_SUCH_PATH);
4565772Sas200622 
4575772Sas200622 	/*
4585772Sas200622 	 * If administratively disabled, don't try to start anything.
4595772Sas200622 	 */
4605772Sas200622 	isonline = smb_isonline();
4615772Sas200622 	if (!isonline && !smb_isautoenable() && smb_isdisabled())
4625772Sas200622 		goto done;
4635772Sas200622 
4645772Sas200622 	if (!isonline)
4655772Sas200622 		ret = smb_enable_service();
4665772Sas200622 	if (!smb_isonline()) {
4675772Sas200622 		ret = SA_OK;
4685772Sas200622 		goto done;
4695772Sas200622 	}
4705772Sas200622 
4715331Samw 	path = sa_get_share_attr(share, "path");
4725331Samw 	if (path == NULL)
4735331Samw 		return (SA_SYSTEM_ERR);
4745331Samw 	rname = sa_get_resource_attr(resource, "name");
4755331Samw 	if (rname == NULL) {
4765331Samw 		sa_free_attr_string(path);
4775331Samw 		return (SA_NO_SUCH_RESOURCE);
4785331Samw 	}
4795331Samw 
4805331Samw 	opts = sa_get_derived_optionset(resource, SMB_PROTOCOL_NAME, 1);
4815331Samw 	smb_build_lmshare_info(rname, path, opts, &si);
4825331Samw 	sa_free_attr_string(path);
4835331Samw 	sa_free_attr_string(rname);
4845331Samw 	sa_free_derived_optionset(opts);
4855772Sas200622 
4865772Sas200622 	/*
4875772Sas200622 	 * Attempt to add the share. Any error that occurs if it was
4885772Sas200622 	 * online is an error but don't count NERR_DuplicateName if
4895772Sas200622 	 * smb/server had to be brought online since bringing the
4905772Sas200622 	 * service up will enable the share that was just added prior
4915772Sas200622 	 * to the attempt to enable.
4925772Sas200622 	 */
4935772Sas200622 
4945772Sas200622 	err = lmshrd_add(&si);
4955772Sas200622 	if (err == NERR_Success || !(!isonline && err == NERR_DuplicateName))
4965772Sas200622 		(void) sa_update_sharetab(share, "smb");
4975772Sas200622 	else
4985331Samw 		return (SA_NOT_SHARED);
4995331Samw 
5005331Samw done:
5015331Samw 	return (ret);
5025331Samw }
5035331Samw 
5045331Samw /*
5055331Samw  * Remove it from smb server hash.
5065331Samw  */
5075331Samw static int
5085331Samw smb_disable_resource(sa_resource_t resource)
5095331Samw {
5105331Samw 	char *rname;
5115331Samw 	DWORD res;
5125331Samw 	sa_share_t share;
5135331Samw 
5145331Samw 	rname = sa_get_resource_attr(resource, "name");
5155331Samw 	if (rname == NULL)
5165331Samw 		return (SA_NO_SUCH_RESOURCE);
5175331Samw 
5185331Samw 	if (smb_isonline()) {
5195331Samw 		res = lmshrd_delete(rname);
5205331Samw 		if (res != NERR_Success) {
5215331Samw 			sa_free_attr_string(rname);
5225331Samw 			return (SA_CONFIG_ERR);
5235331Samw 		}
5245331Samw 	}
5255951Sdougm 
5265951Sdougm 	sa_free_attr_string(rname);
5275951Sdougm 
5285331Samw 	share = sa_get_resource_parent(resource);
5295331Samw 	if (share != NULL) {
5305331Samw 		rname = sa_get_share_attr(share, "path");
5315331Samw 		if (rname != NULL) {
5325951Sdougm 			sa_handle_t handle;
5335951Sdougm 
5345951Sdougm 			handle = sa_find_group_handle((sa_group_t)resource);
5355951Sdougm 			(void) sa_delete_sharetab(handle, rname, "smb");
5365331Samw 			sa_free_attr_string(rname);
5375331Samw 		}
5385331Samw 	}
5395331Samw 	/*
5405331Samw 	 * Always return OK as smb/server may be down and
5415331Samw 	 * Shares will be picked up when loaded.
5425331Samw 	 */
5435331Samw 	return (SA_OK);
5445331Samw }
5455331Samw 
5465331Samw /*
5475331Samw  * smb_share_changed(sa_share_t share)
5485331Samw  *
5495331Samw  * The specified share has changed.
5505331Samw  */
5515331Samw static int
5525331Samw smb_share_changed(sa_share_t share)
5535331Samw {
5545331Samw 	char *path;
5555331Samw 	sa_resource_t resource;
5565331Samw 
5575331Samw 	/* get the path since it is important in several places */
5585331Samw 	path = sa_get_share_attr(share, "path");
5595331Samw 	if (path == NULL)
5605331Samw 		return (SA_NO_SUCH_PATH);
5615331Samw 	for (resource = sa_get_share_resource(share, NULL);
5625331Samw 	    resource != NULL;
5635331Samw 	    resource = sa_get_next_resource(resource))
5645331Samw 		(void) smb_resource_changed(resource);
5655331Samw 
5665331Samw 	sa_free_attr_string(path);
5675331Samw 
5685331Samw 	return (SA_OK);
5695331Samw }
5705331Samw 
5715331Samw /*
5725331Samw  * smb_resource_changed(sa_resource_t resource)
5735331Samw  *
5745331Samw  * The specified resource has changed.
5755331Samw  */
5765331Samw static int
5775331Samw smb_resource_changed(sa_resource_t resource)
5785331Samw {
5795331Samw 	DWORD res;
5805331Samw 	lmshare_info_t si;
5815331Samw 	lmshare_info_t new_si;
5825331Samw 	char *rname, *path;
5835331Samw 	sa_optionset_t opts;
5845331Samw 	sa_share_t share;
5855331Samw 
5865331Samw 	rname = sa_get_resource_attr(resource, "name");
5875331Samw 	if (rname == NULL)
5885331Samw 		return (SA_NO_SUCH_RESOURCE);
5895331Samw 
5905331Samw 	share = sa_get_resource_parent(resource);
5915331Samw 	if (share == NULL) {
5925331Samw 		sa_free_attr_string(rname);
5935331Samw 		return (SA_CONFIG_ERR);
5945331Samw 	}
5955331Samw 
5965331Samw 	path = sa_get_share_attr(share, "path");
5975331Samw 	if (path == NULL) {
5985331Samw 		sa_free_attr_string(rname);
5995331Samw 		return (SA_NO_SUCH_PATH);
6005331Samw 	}
6015331Samw 
6025331Samw 	if (!smb_isonline()) {
6035331Samw 		sa_free_attr_string(rname);
6045331Samw 		return (SA_OK);
6055331Samw 	}
6065331Samw 
6075331Samw 	/* Update the share cache in smb/server */
6085331Samw 	res = lmshrd_getinfo(rname, &si);
6095331Samw 	if (res != NERR_Success) {
6105331Samw 		sa_free_attr_string(path);
6115331Samw 		sa_free_attr_string(rname);
6125331Samw 		return (SA_CONFIG_ERR);
6135331Samw 	}
6145331Samw 
6155331Samw 	opts = sa_get_derived_optionset(resource, SMB_PROTOCOL_NAME, 1);
6165331Samw 	smb_build_lmshare_info(rname, path, opts, &new_si);
6175331Samw 	sa_free_derived_optionset(opts);
6185331Samw 	sa_free_attr_string(path);
6195331Samw 	sa_free_attr_string(rname);
6205331Samw 
6215331Samw 	/*
6225331Samw 	 * Update all fields from sa_share_t
6235331Samw 	 * Get derived values.
6245331Samw 	 */
6255331Samw 	if (lmshrd_setinfo(&new_si) != LMSHR_DOOR_SRV_SUCCESS)
6265331Samw 		return (SA_CONFIG_ERR);
6275331Samw 	return (smb_enable_service());
6285331Samw }
6295331Samw 
6305331Samw /*
6315800Sdougm  * smb_disable_share(sa_share_t share, char *path)
6325331Samw  *
6335800Sdougm  * Unshare the specified share. Note that "path" is the same
6345800Sdougm  * path as what is in the "share" object. It is passed in to avoid an
6355800Sdougm  * additional lookup. A missing "path" value makes this a no-op
6365800Sdougm  * function.
6375331Samw  */
6385331Samw static int
6395331Samw smb_disable_share(sa_share_t share, char *path)
6405331Samw {
6415331Samw 	char *rname;
6425331Samw 	sa_resource_t resource;
6435800Sdougm 	sa_group_t parent;
6445331Samw 	boolean_t iszfs;
6455331Samw 	int err = SA_OK;
6465951Sdougm 	sa_handle_t handle;
6475331Samw 
6485800Sdougm 	if (path == NULL)
6495800Sdougm 		return (err);
6505800Sdougm 
6515800Sdougm 	/*
6525800Sdougm 	 * If the share is in a ZFS group we need to handle it
6535800Sdougm 	 * differently.  Just being on a ZFS file system isn't
6545800Sdougm 	 * enough since we may be in a legacy share case.
6555800Sdougm 	 */
6565800Sdougm 	parent = sa_get_parent_group(share);
6575800Sdougm 	iszfs = sa_group_is_zfs(parent);
6585800Sdougm 
6595331Samw 	if (!smb_isonline())
6605331Samw 		goto done;
6615331Samw 
6625331Samw 	for (resource = sa_get_share_resource(share, NULL);
6635331Samw 	    resource != NULL;
6645331Samw 	    resource = sa_get_next_resource(resource)) {
6655331Samw 		rname = sa_get_resource_attr(resource, "name");
6665331Samw 		if (rname == NULL) {
6675331Samw 			continue;
6685331Samw 		}
6695331Samw 		if (!iszfs) {
6705331Samw 			err = lmshrd_delete(rname);
6715331Samw 			switch (err) {
6725331Samw 			case NERR_NetNameNotFound:
6735331Samw 			case NERR_Success:
6745331Samw 				err = SA_OK;
6755331Samw 				break;
6765331Samw 			default:
6775331Samw 				err = SA_CONFIG_ERR;
6785331Samw 				break;
6795331Samw 			}
6805331Samw 		} else {
6815331Samw 			share_t sh;
6825331Samw 
6835331Samw 			sa_sharetab_fill_zfs(share, &sh, "smb");
6845331Samw 			err = sa_share_zfs(share, (char *)path, &sh,
6855331Samw 			    rname, ZFS_UNSHARE_SMB);
6865331Samw 			sa_emptyshare(&sh);
6875331Samw 		}
6885331Samw 		sa_free_attr_string(rname);
6895331Samw 	}
6905331Samw done:
6915951Sdougm 	if (!iszfs) {
6925951Sdougm 		handle = sa_find_group_handle((sa_group_t)share);
6935951Sdougm 		if (handle != NULL)
6945951Sdougm 			(void) sa_delete_sharetab(handle, path, "smb");
6955951Sdougm 		else
6965951Sdougm 			err = SA_SYSTEM_ERR;
6975951Sdougm 	}
6985331Samw 	return (err);
6995331Samw }
7005331Samw 
7015331Samw /*
7025331Samw  * smb_validate_property(property, parent)
7035331Samw  *
7045331Samw  * Check that the property has a legitimate value for its type.
7055331Samw  */
7065331Samw 
7075331Samw static int
7085331Samw smb_validate_property(sa_property_t property, sa_optionset_t parent)
7095331Samw {
7105331Samw 	int ret = SA_OK;
7115331Samw 	char *propname;
7125331Samw 	int optindex;
7135331Samw 	sa_group_t parent_group;
7145331Samw 	char *value;
7155331Samw 
7165331Samw 	propname = sa_get_property_attr(property, "type");
7175331Samw 
7185331Samw 	if ((optindex = findopt(propname)) < 0)
7195331Samw 		ret = SA_NO_SUCH_PROP;
7205331Samw 
7215331Samw 	/* need to validate value range here as well */
7225331Samw 	if (ret == SA_OK) {
7235331Samw 		parent_group = sa_get_parent_group((sa_share_t)parent);
7245331Samw 		if (optdefs[optindex].share && !sa_is_share(parent_group))
7255331Samw 			ret = SA_PROP_SHARE_ONLY;
7265331Samw 	}
7275331Samw 	if (ret != SA_OK) {
7285331Samw 		if (propname != NULL)
7295331Samw 			sa_free_attr_string(propname);
7305331Samw 		return (ret);
7315331Samw 	}
7325331Samw 
7335331Samw 	value = sa_get_property_attr(property, "value");
7345331Samw 	if (value != NULL) {
7355331Samw 		/* first basic type checking */
7365331Samw 		switch (optdefs[optindex].type) {
7375331Samw 		case OPT_TYPE_NUMBER:
7385331Samw 			/* check that the value is all digits */
7395331Samw 			if (!is_a_number(value))
7405331Samw 				ret = SA_BAD_VALUE;
7415331Samw 			break;
7425331Samw 		case OPT_TYPE_BOOLEAN:
7435331Samw 			if (strlen(value) == 0 ||
7445331Samw 			    strcasecmp(value, "true") == 0 ||
7455331Samw 			    strcmp(value, "1") == 0 ||
7465331Samw 			    strcasecmp(value, "false") == 0 ||
7475331Samw 			    strcmp(value, "0") == 0) {
7485331Samw 				ret = SA_OK;
7495331Samw 			} else {
7505331Samw 				ret = SA_BAD_VALUE;
7515331Samw 			}
7525331Samw 			break;
7535331Samw 		case OPT_TYPE_NAME:
7545331Samw 			/*
7555331Samw 			 * Make sure no invalid characters
7565331Samw 			 */
7575331Samw 			if (validresource(value) == B_FALSE)
7585331Samw 				ret = SA_BAD_VALUE;
7595331Samw 			break;
7605331Samw 		case OPT_TYPE_STRING:
7615331Samw 			/* whatever is here should be ok */
7625331Samw 			break;
7635331Samw 		default:
7645331Samw 			break;
7655331Samw 		}
7665331Samw 	}
7675331Samw 
7685331Samw 	if (value != NULL)
7695331Samw 		sa_free_attr_string(value);
7705331Samw 	if (ret == SA_OK && optdefs[optindex].check != NULL)
7715331Samw 		/* do the property specific check */
7725331Samw 		ret = optdefs[optindex].check(property);
7735331Samw 
7745331Samw 	if (propname != NULL)
7755331Samw 		sa_free_attr_string(propname);
7765331Samw 	return (ret);
7775331Samw }
7785331Samw 
7795331Samw /*
7805331Samw  * Protocol management functions
7815331Samw  *
7825331Samw  * properties defined in the default files are defined in
7835331Samw  * proto_option_defs for parsing and validation.
7845331Samw  */
7855331Samw 
7865331Samw struct smb_proto_option_defs {
7875331Samw 	int smb_index;
7885331Samw 	int32_t minval;
7895331Samw 	int32_t maxval; /* In case of length of string this should be max */
7905331Samw 	int (*validator)(int, char *);
7915331Samw 	int32_t	refresh;
7925331Samw } smb_proto_options[] = {
7935772Sas200622 	{ SMB_CI_SYS_CMNT, 0, MAX_VALUE_BUFLEN,
7945772Sas200622 	    string_length_check_validator, SMB_REFRESH_REFRESH },
7955772Sas200622 	{ SMB_CI_MAX_WORKERS, 64, 1024, range_check_validator,
7965772Sas200622 	    SMB_REFRESH_REFRESH },
7975772Sas200622 	{ SMB_CI_NBSCOPE, 0, MAX_VALUE_BUFLEN,
7985772Sas200622 	    string_length_check_validator, 0 },
7995772Sas200622 	{ SMB_CI_LM_LEVEL, 2, 5, range_check_validator, 0 },
8005772Sas200622 	{ SMB_CI_KEEPALIVE, 20, 5400, range_check_validator_zero_ok,
8015772Sas200622 	    SMB_REFRESH_REFRESH },
8025772Sas200622 	{ SMB_CI_WINS_SRV1, 0, MAX_VALUE_BUFLEN,
8035772Sas200622 	    ip_address_validator_empty_ok, SMB_REFRESH_REFRESH },
8045772Sas200622 	{ SMB_CI_WINS_SRV2, 0, MAX_VALUE_BUFLEN,
8055772Sas200622 	    ip_address_validator_empty_ok, SMB_REFRESH_REFRESH },
8065772Sas200622 	{ SMB_CI_WINS_EXCL, 0, MAX_VALUE_BUFLEN,
8075772Sas200622 	    ip_address_csv_list_validator_empty_ok, SMB_REFRESH_REFRESH },
8085772Sas200622 	{ SMB_CI_SIGNING_ENABLE, 0, 0, true_false_validator,
8095772Sas200622 	    SMB_REFRESH_REFRESH },
8105772Sas200622 	{ SMB_CI_SIGNING_REQD, 0, 0, true_false_validator,
8115772Sas200622 	    SMB_REFRESH_REFRESH },
8125772Sas200622 	{ SMB_CI_RESTRICT_ANON, 0, 0, true_false_validator,
8135772Sas200622 	    SMB_REFRESH_REFRESH },
8145772Sas200622 	{ SMB_CI_DOMAIN_SRV, 0, MAX_VALUE_BUFLEN,
8155772Sas200622 	    ip_address_validator_empty_ok, 0 },
8165772Sas200622 	{ SMB_CI_ADS_SITE, 0, MAX_VALUE_BUFLEN,
8175772Sas200622 	    string_length_check_validator, SMB_REFRESH_REFRESH },
8185772Sas200622 	{ SMB_CI_DYNDNS_ENABLE, 0, 0, true_false_validator, 0 },
8195772Sas200622 	{ SMB_CI_AUTOHOME_MAP, 0, MAX_VALUE_BUFLEN, path_validator, 0 },
8205331Samw };
8215331Samw 
8225772Sas200622 #define	SMB_OPT_NUM \
8235772Sas200622 	(sizeof (smb_proto_options) / sizeof (smb_proto_options[0]))
8245772Sas200622 
8255331Samw /*
8265331Samw  * Check the range of value as int range.
8275331Samw  */
8285331Samw static int
8295331Samw range_check_validator(int index, char *value)
8305331Samw {
8315331Samw 	int ret = SA_OK;
8325331Samw 
8335331Samw 	if (!is_a_number(value)) {
8345331Samw 		ret = SA_BAD_VALUE;
8355331Samw 	} else {
8365331Samw 		int val;
8375331Samw 		val = strtoul(value, NULL, 0);
8385331Samw 		if (val < smb_proto_options[index].minval ||
8395331Samw 		    val > smb_proto_options[index].maxval)
8405331Samw 			ret = SA_BAD_VALUE;
8415331Samw 	}
8425331Samw 	return (ret);
8435331Samw }
8445331Samw 
8455331Samw /*
8465331Samw  * Check the range of value as int range.
8475331Samw  */
8485331Samw static int
8495331Samw range_check_validator_zero_ok(int index, char *value)
8505331Samw {
8515331Samw 	int ret = SA_OK;
8525331Samw 
8535331Samw 	if (!is_a_number(value)) {
8545331Samw 		ret = SA_BAD_VALUE;
8555331Samw 	} else {
8565331Samw 		int val;
8575331Samw 		val = strtoul(value, NULL, 0);
8585331Samw 		if (val == 0)
8595331Samw 			ret = SA_OK;
8605331Samw 		else {
8615331Samw 			if (val < smb_proto_options[index].minval ||
8625331Samw 			    val > smb_proto_options[index].maxval)
8635331Samw 			ret = SA_BAD_VALUE;
8645331Samw 		}
8655331Samw 	}
8665331Samw 	return (ret);
8675331Samw }
8685331Samw 
8695331Samw /*
8705331Samw  * Check the length of the string
8715331Samw  */
8725331Samw static int
8735331Samw string_length_check_validator(int index, char *value)
8745331Samw {
8755331Samw 	int ret = SA_OK;
8765331Samw 
8775331Samw 	if (value == NULL)
8785331Samw 		return (SA_BAD_VALUE);
8795331Samw 	if (strlen(value) > smb_proto_options[index].maxval)
8805331Samw 		ret = SA_BAD_VALUE;
8815331Samw 	return (ret);
8825331Samw }
8835331Samw 
8845331Samw /*
8855331Samw  * Check yes/no
8865331Samw  */
8875331Samw /*ARGSUSED*/
8885331Samw static int
8895331Samw true_false_validator(int index, char *value)
8905331Samw {
8915331Samw 	if (value == NULL)
8925331Samw 		return (SA_BAD_VALUE);
8935331Samw 	if ((strcasecmp(value, "true") == 0) ||
8945331Samw 	    (strcasecmp(value, "false") == 0))
8955331Samw 		return (SA_OK);
8965331Samw 	return (SA_BAD_VALUE);
8975331Samw }
8985331Samw 
8995331Samw /*
9005331Samw  * Check IP address.
9015331Samw  */
9025331Samw /*ARGSUSED*/
9035331Samw static int
9045331Samw ip_address_validator_empty_ok(int index, char *value)
9055331Samw {
9065331Samw 	char sbytes[16];
9075331Samw 	int len;
9085331Samw 
9095331Samw 	if (value == NULL)
9105331Samw 		return (SA_OK);
9115331Samw 	len = strlen(value);
9125331Samw 	if (len == 0)
9135331Samw 		return (SA_OK);
9145331Samw 	if (inet_pton(AF_INET, value, (void *)sbytes) != 1)
9155331Samw 		return (SA_BAD_VALUE);
9165331Samw 
9175331Samw 	return (SA_OK);
9185331Samw }
9195331Samw 
9205331Samw /*
9215331Samw  * Check IP address list
9225331Samw  */
9235331Samw /*ARGSUSED*/
9245331Samw static int
9255331Samw ip_address_csv_list_validator_empty_ok(int index, char *value)
9265331Samw {
9275331Samw 	char sbytes[16];
9285331Samw 	char *ip, *tmp, *ctx;
9295331Samw 
9305331Samw 	if (value == NULL || *value == '\0')
9315331Samw 		return (SA_OK);
9325331Samw 
9335331Samw 	if (strlen(value) > MAX_VALUE_BUFLEN)
9345331Samw 		return (SA_BAD_VALUE);
9355331Samw 
9365331Samw 	if ((tmp = strdup(value)) == NULL)
9375331Samw 		return (SA_NO_MEMORY);
9385331Samw 
9395331Samw 	ip = strtok_r(tmp, ",", &ctx);
9405331Samw 	while (ip) {
9415331Samw 		if (strlen(ip) == 0) {
9425331Samw 			free(tmp);
9435331Samw 			return (SA_BAD_VALUE);
9445331Samw 		}
9455331Samw 		if (*ip != 0) {
9465331Samw 			if (inet_pton(AF_INET, ip,
9475331Samw 			    (void *)sbytes) != 1) {
9485331Samw 				free(tmp);
9495331Samw 				return (SA_BAD_VALUE);
9505331Samw 			}
9515331Samw 		}
9525331Samw 		ip = strtok_r(0, ",", &ctx);
9535331Samw 	}
9545331Samw 
9555331Samw 	free(tmp);
9565331Samw 	return (SA_OK);
9575331Samw }
9585331Samw 
9595331Samw /*
9605331Samw  * Check path
9615331Samw  */
9625331Samw /*ARGSUSED*/
9635331Samw static int
9645331Samw path_validator(int index, char *value)
9655331Samw {
9665331Samw 	struct stat buffer;
9675331Samw 	int fd, status;
9685331Samw 
9695331Samw 	if (value == NULL)
9705331Samw 		return (SA_BAD_VALUE);
9715331Samw 
9725331Samw 	fd = open(value, O_RDONLY);
9735331Samw 	if (fd < 0)
9745331Samw 		return (SA_BAD_VALUE);
9755331Samw 
9765331Samw 	status = fstat(fd, &buffer);
9775331Samw 	(void) close(fd);
9785331Samw 
9795331Samw 	if (status < 0)
9805331Samw 		return (SA_BAD_VALUE);
9815331Samw 
9825331Samw 	if (buffer.st_mode & S_IFDIR)
9835331Samw 		return (SA_OK);
9845331Samw 	return (SA_BAD_VALUE);
9855331Samw }
9865331Samw 
9875331Samw /*
9885331Samw  * the protoset holds the defined options so we don't have to read
9895331Samw  * them multiple times
9905331Samw  */
9915331Samw static sa_protocol_properties_t protoset;
9925331Samw 
9935331Samw static int
9945331Samw findprotoopt(char *name)
9955331Samw {
9965331Samw 	int i;
9975772Sas200622 	char *sc_name;
9985772Sas200622 
9995772Sas200622 	for (i = 0; i < SMB_OPT_NUM; i++) {
10005772Sas200622 		sc_name = smb_config_getname(smb_proto_options[i].smb_index);
10015772Sas200622 		if (strcasecmp(sc_name, name) == 0)
10025331Samw 			return (i);
10035331Samw 	}
10045772Sas200622 
10055331Samw 	return (-1);
10065331Samw }
10075331Samw 
10085331Samw /*
10095331Samw  * smb_load_proto_properties()
10105331Samw  *
10115331Samw  * read the smb config values from SMF.
10125331Samw  */
10135331Samw 
10145331Samw static int
10155331Samw smb_load_proto_properties()
10165331Samw {
10175331Samw 	sa_property_t prop;
10185772Sas200622 	char value[MAX_VALUE_BUFLEN];
10195772Sas200622 	char *name;
10205331Samw 	int index;
10216019Sdougm 	int ret = SA_OK;
10225772Sas200622 	int rc;
10235331Samw 
10245331Samw 	protoset = sa_create_protocol_properties(SMB_PROTOCOL_NAME);
10255331Samw 	if (protoset == NULL)
10265331Samw 		return (SA_NO_MEMORY);
10275331Samw 
10286019Sdougm 	for (index = 0; index < SMB_OPT_NUM && ret == SA_OK; index++) {
10295772Sas200622 		rc = smb_config_get(smb_proto_options[index].smb_index,
10305772Sas200622 		    value, sizeof (value));
10315772Sas200622 		if (rc != SMBD_SMF_OK)
10325772Sas200622 			continue;
10335772Sas200622 		name = smb_config_getname(smb_proto_options[index].smb_index);
10345772Sas200622 		prop = sa_create_property(name, value);
10355454Sdougm 		if (prop != NULL)
10366019Sdougm 			ret = sa_add_protocol_property(protoset, prop);
10376019Sdougm 		else
10386019Sdougm 			ret = SA_NO_MEMORY;
10395331Samw 	}
10406019Sdougm 	return (ret);
10415331Samw }
10425331Samw 
10435331Samw /*
10445331Samw  * smb_share_init()
10455331Samw  *
10465331Samw  * Initialize the smb plugin.
10475331Samw  */
10485331Samw 
10495331Samw static int
10505331Samw smb_share_init(void)
10515331Samw {
10525331Samw 	int ret = SA_OK;
10535331Samw 
10545331Samw 	if (sa_plugin_ops.sa_init != smb_share_init)
10555331Samw 		return (SA_SYSTEM_ERR);
10565331Samw 
10576019Sdougm 	ret = smb_load_proto_properties();
10585331Samw 
10595331Samw 	return (ret);
10605331Samw }
10615331Samw 
10625331Samw /*
10635331Samw  * smb_share_fini()
10645331Samw  *
10655331Samw  */
10665331Samw static void
10675331Samw smb_share_fini(void)
10685331Samw {
10695331Samw 	xmlFreeNode(protoset);
10705331Samw 	protoset = NULL;
10715772Sas200622 
10725772Sas200622 	(void) lmshrd_door_close();
10735331Samw }
10745331Samw 
10755331Samw /*
10765331Samw  * smb_get_proto_set()
10775331Samw  *
10785331Samw  * Return an optionset with all the protocol specific properties in
10795331Samw  * it.
10805331Samw  */
10815331Samw static sa_protocol_properties_t
10825331Samw smb_get_proto_set(void)
10835331Samw {
10845331Samw 	return (protoset);
10855331Samw }
10865331Samw 
10875331Samw /*
10885772Sas200622  * smb_enable_dependencies()
10895772Sas200622  *
10905772Sas200622  * SMBD_DEFAULT_INSTANCE_FMRI may have some dependencies that aren't
10915772Sas200622  * enabled. This will attempt to enable all of them.
10925772Sas200622  */
10935772Sas200622 static void
10945772Sas200622 smb_enable_dependencies(const char *fmri)
10955772Sas200622 {
10965772Sas200622 	scf_handle_t *handle;
10975772Sas200622 	scf_service_t *service;
10985772Sas200622 	scf_instance_t *inst = NULL;
10995772Sas200622 	scf_iter_t *iter;
11005772Sas200622 	scf_property_t *prop;
11015772Sas200622 	scf_value_t *value;
11025772Sas200622 	scf_propertygroup_t *pg;
11035772Sas200622 	scf_scope_t *scope;
11045772Sas200622 	char type[SCFTYPE_LEN];
11055772Sas200622 	char *dependency;
11065772Sas200622 	char *servname;
11075772Sas200622 	int maxlen;
11085772Sas200622 
11095772Sas200622 	/*
11105772Sas200622 	 * Get all required handles and storage.
11115772Sas200622 	 */
11125772Sas200622 	handle = scf_handle_create(SCF_VERSION);
11135772Sas200622 	if (handle == NULL)
11145772Sas200622 		return;
11155772Sas200622 
11165772Sas200622 	if (scf_handle_bind(handle) != 0) {
11175772Sas200622 		scf_handle_destroy(handle);
11185772Sas200622 		return;
11195772Sas200622 	}
11205772Sas200622 
11215772Sas200622 	maxlen = scf_limit(SCF_LIMIT_MAX_VALUE_LENGTH);
11225772Sas200622 	if (maxlen == (ssize_t)-1)
11235772Sas200622 		maxlen = MAXPATHLEN;
11245772Sas200622 
11255772Sas200622 	dependency = malloc(maxlen);
11265772Sas200622 
11275772Sas200622 	service = scf_service_create(handle);
11285772Sas200622 
11295772Sas200622 	iter = scf_iter_create(handle);
11305772Sas200622 
11315772Sas200622 	pg = scf_pg_create(handle);
11325772Sas200622 
11335772Sas200622 	prop = scf_property_create(handle);
11345772Sas200622 
11355772Sas200622 	value = scf_value_create(handle);
11365772Sas200622 
11375772Sas200622 	scope = scf_scope_create(handle);
11385772Sas200622 
11395772Sas200622 	if (service == NULL || iter == NULL || pg == NULL || prop == NULL ||
11405772Sas200622 	    value == NULL || scope == NULL || dependency == NULL)
11415772Sas200622 		goto done;
11425772Sas200622 
11435772Sas200622 	/*
11445772Sas200622 	 *  We passed in the FMRI for the default instance but for
11455772Sas200622 	 *  some things we need the simple form so construct it. Since
11465772Sas200622 	 *  we reuse the storage that dependency points to, we need to
11475772Sas200622 	 *  use the servname early.
11485772Sas200622 	 */
11495772Sas200622 	(void) snprintf(dependency, maxlen, "%s", fmri + sizeof ("svc:"));
11505772Sas200622 	servname = strrchr(dependency, ':');
11515772Sas200622 	if (servname == NULL)
11525772Sas200622 		goto done;
11535772Sas200622 	*servname = '\0';
11545772Sas200622 	servname = dependency;
11555772Sas200622 
11565772Sas200622 	/*
11575772Sas200622 	 * Setup to iterate over the service property groups, only
11585772Sas200622 	 * looking at those that are "dependency" types. The "entity"
11595772Sas200622 	 * property will have the FMRI of the service we are dependent
11605772Sas200622 	 * on.
11615772Sas200622 	 */
11625772Sas200622 	if (scf_handle_get_scope(handle, SCF_SCOPE_LOCAL, scope) != 0)
11635772Sas200622 		goto done;
11645772Sas200622 
11655772Sas200622 	if (scf_scope_get_service(scope, servname, service) != 0)
11665772Sas200622 		goto done;
11675772Sas200622 
11685772Sas200622 	if (scf_iter_service_pgs(iter, service) != 0)
11695772Sas200622 		goto done;
11705772Sas200622 
11715772Sas200622 	while (scf_iter_next_pg(iter, pg) > 0) {
11725772Sas200622 		char *services[2];
11735772Sas200622 		/*
11745772Sas200622 		 * Have a property group for the service. See if it is
11755772Sas200622 		 * a dependency pg and only do operations on those.
11765772Sas200622 		 */
11775772Sas200622 		if (scf_pg_get_type(pg, type, SCFTYPE_LEN) <= 0)
11785772Sas200622 			continue;
11795772Sas200622 
11805772Sas200622 		if (strncmp(type, SCF_GROUP_DEPENDENCY, SCFTYPE_LEN) != 0)
11815772Sas200622 			continue;
11825772Sas200622 		/*
11835772Sas200622 		 * Have a dependency.  Attempt to enable it.
11845772Sas200622 		 */
11855772Sas200622 		if (scf_pg_get_property(pg, SCF_PROPERTY_ENTITIES, prop) != 0)
11865772Sas200622 			continue;
11875772Sas200622 
11885772Sas200622 		if (scf_property_get_value(prop, value) != 0)
11895772Sas200622 			continue;
11905772Sas200622 
11915772Sas200622 		services[1] = NULL;
11925772Sas200622 
11935772Sas200622 		if (scf_value_get_as_string(value, dependency, maxlen) > 0) {
11945772Sas200622 			services[0] = dependency;
11955772Sas200622 			_check_services(services);
11965772Sas200622 		}
11975772Sas200622 	}
11985772Sas200622 
11995772Sas200622 done:
12005772Sas200622 	if (dependency != NULL)
12015772Sas200622 		free(dependency);
12025772Sas200622 	if (value != NULL)
12035772Sas200622 		scf_value_destroy(value);
12045772Sas200622 	if (prop != NULL)
12055772Sas200622 		scf_property_destroy(prop);
12065772Sas200622 	if (pg != NULL)
12075772Sas200622 		scf_pg_destroy(pg);
12085772Sas200622 	if (iter != NULL)
12095772Sas200622 		scf_iter_destroy(iter);
12105772Sas200622 	if (scope != NULL)
12115772Sas200622 		scf_scope_destroy(scope);
12125772Sas200622 	if (inst != NULL)
12135772Sas200622 		scf_instance_destroy(inst);
12145772Sas200622 	if (service != NULL)
12155772Sas200622 		scf_service_destroy(service);
12165772Sas200622 
12175772Sas200622 	(void) scf_handle_unbind(handle);
12185772Sas200622 	scf_handle_destroy(handle);
12195772Sas200622 }
12205772Sas200622 
12215772Sas200622 /*
12225331Samw  * How long to wait for service to come online
12235331Samw  */
12245331Samw #define	WAIT_FOR_SERVICE	15
12255331Samw 
12265331Samw /*
12275331Samw  * smb_enable_service()
12285331Samw  *
12295331Samw  */
12305331Samw static int
12315331Samw smb_enable_service(void)
12325331Samw {
12335331Samw 	int i;
12345331Samw 	int ret = SA_OK;
12355772Sas200622 	char *service[] = { SMBD_DEFAULT_INSTANCE_FMRI, NULL };
12365331Samw 
12375331Samw 	if (!smb_isonline()) {
12385772Sas200622 		/*
12395772Sas200622 		 * Attempt to start the idmap, and other dependent
12405772Sas200622 		 * services, first.  If it fails, the SMB service will
12415772Sas200622 		 * ultimately fail so we use that as the error.  If we
12425772Sas200622 		 * don't try to enable idmap, smb won't start the
12435772Sas200622 		 * first time unless the admin has done it
12445772Sas200622 		 * manually. The service could be administratively
12455772Sas200622 		 * disabled so we won't always get started.
12465772Sas200622 		 */
12475772Sas200622 		smb_enable_dependencies(SMBD_DEFAULT_INSTANCE_FMRI);
12485772Sas200622 		_check_services(service);
12495331Samw 
12505331Samw 		/* Wait for service to come online */
12515331Samw 		for (i = 0; i < WAIT_FOR_SERVICE; i++) {
12525331Samw 			if (smb_isonline()) {
12535772Sas200622 				ret =  SA_OK;
12545331Samw 				break;
12556030Sjb150015 			} else if (smb_ismaint()) {
12566030Sjb150015 				/* maintenance requires help */
12576030Sjb150015 				ret = SA_SYSTEM_ERR;
12586030Sjb150015 				break;
12596030Sjb150015 			} else if (smb_isdisabled()) {
12606030Sjb150015 				/* disabled is ok */
12616030Sjb150015 				ret = SA_OK;
12626030Sjb150015 				break;
12635331Samw 			} else {
12646030Sjb150015 				/* try another time */
12655331Samw 				ret = SA_BUSY;
12665331Samw 				(void) sleep(1);
12675331Samw 			}
12685331Samw 		}
12695331Samw 	}
12705331Samw 	return (ret);
12715331Samw }
12725331Samw 
12735331Samw /*
12745331Samw  * smb_validate_proto_prop(index, name, value)
12755331Samw  *
12765331Samw  * Verify that the property specified by name can take the new
12775331Samw  * value. This is a sanity check to prevent bad values getting into
12785331Samw  * the default files.
12795331Samw  */
12805331Samw static int
12815331Samw smb_validate_proto_prop(int index, char *name, char *value)
12825331Samw {
12835331Samw 	if ((name == NULL) || (index < 0))
12845331Samw 		return (SA_BAD_VALUE);
12855331Samw 
12865331Samw 	if (smb_proto_options[index].validator == NULL)
12875331Samw 		return (SA_OK);
12885331Samw 
12895331Samw 	if (smb_proto_options[index].validator(index, value) == SA_OK)
12905331Samw 		return (SA_OK);
12915331Samw 	return (SA_BAD_VALUE);
12925331Samw }
12935331Samw 
12945331Samw /*
12955331Samw  * smb_set_proto_prop(prop)
12965331Samw  *
12975331Samw  * check that prop is valid.
12985331Samw  */
12995331Samw /*ARGSUSED*/
13005331Samw static int
13015331Samw smb_set_proto_prop(sa_property_t prop)
13025331Samw {
13035331Samw 	int ret = SA_OK;
13045331Samw 	char *name;
13055331Samw 	char *value;
13065331Samw 	int index = -1;
13075521Sas200622 	struct smb_proto_option_defs *opt;
13085331Samw 
13095331Samw 	name = sa_get_property_attr(prop, "type");
13105331Samw 	value = sa_get_property_attr(prop, "value");
13115331Samw 	if (name != NULL && value != NULL) {
13125331Samw 		index = findprotoopt(name);
13135331Samw 		if (index >= 0) {
13145331Samw 			/* should test for valid value */
13155331Samw 			ret = smb_validate_proto_prop(index, name, value);
13165331Samw 			if (ret == SA_OK) {
13175521Sas200622 				opt = &smb_proto_options[index];
13185521Sas200622 
13195331Samw 				/* Save to SMF */
13205772Sas200622 				(void) smb_config_set(opt->smb_index, value);
13215331Samw 				/*
13225331Samw 				 * Specialized refresh mechanisms can
13235331Samw 				 * be flagged in the proto_options and
13245331Samw 				 * processed here.
13255331Samw 				 */
13265521Sas200622 				if (opt->refresh & SMB_REFRESH_REFRESH)
1327*6139Sjb150015 					(void) smf_refresh_instance(
1328*6139Sjb150015 					    SMBD_DEFAULT_INSTANCE_FMRI);
13295521Sas200622 				else if (opt->refresh & SMB_REFRESH_RESTART)
13305331Samw 					(void) smf_restart_instance(
13315331Samw 					    SMBD_DEFAULT_INSTANCE_FMRI);
13325331Samw 			}
13335331Samw 		}
13345331Samw 	}
13355521Sas200622 
13365331Samw 	if (name != NULL)
13375331Samw 		sa_free_attr_string(name);
13385331Samw 	if (value != NULL)
13395331Samw 		sa_free_attr_string(value);
13405331Samw 
13415331Samw 	return (ret);
13425331Samw }
13435331Samw 
13445331Samw /*
13455331Samw  * smb_get_status()
13465331Samw  *
13475331Samw  * What is the current status of the smbd? We use the SMF state here.
13485331Samw  * Caller must free the returned value.
13495331Samw  */
13505331Samw 
13515331Samw static char *
13525331Samw smb_get_status(void)
13535331Samw {
13545331Samw 	char *state = NULL;
13555331Samw 	state = smf_get_state(SMBD_DEFAULT_INSTANCE_FMRI);
13565331Samw 	return (state != NULL ? state : "-");
13575331Samw }
13585331Samw 
13595331Samw /*
13605331Samw  * This protocol plugin require resource names
13615331Samw  */
13625331Samw static uint64_t
13635331Samw smb_share_features(void)
13645331Samw {
13655331Samw 	return (SA_FEATURE_RESOURCE | SA_FEATURE_ALLOWSUBDIRS |
13666088Sdougm 	    SA_FEATURE_ALLOWPARDIRS | SA_FEATURE_SERVER);
13675331Samw }
13685331Samw 
13695331Samw /*
13705331Samw  * This should be used to convert lmshare_info to sa_resource_t
13715331Samw  * Should only be needed to build temp shares/resources to be
13725331Samw  * supplied to sharemanager to display temp shares.
13735331Samw  */
13745331Samw static int
13755331Samw smb_build_tmp_sa_resource(sa_handle_t handle, lmshare_info_t *si)
13765331Samw {
13775331Samw 	int err;
13785331Samw 	sa_share_t share;
13795331Samw 	sa_group_t group;
13805331Samw 	sa_resource_t resource;
13815331Samw 
13825331Samw 	if (si == NULL)
13835331Samw 		return (SA_INVALID_NAME);
13845331Samw 
13855331Samw 	/*
13865331Samw 	 * First determine if the "share path" is already shared
13875331Samw 	 * somewhere. If it is, we have to use it as the authority on
13885331Samw 	 * where the transient share lives so will use it's parent
13895331Samw 	 * group. If it doesn't exist, it needs to land in "smb".
13905331Samw 	 */
13915331Samw 
13925331Samw 	share = sa_find_share(handle, si->directory);
13935331Samw 	if (share != NULL) {
13945331Samw 		group = sa_get_parent_group(share);
13955331Samw 	} else {
13965331Samw 		group = smb_get_smb_share_group(handle);
13975331Samw 		if (group == NULL)
13985331Samw 			return (SA_NO_SUCH_GROUP);
13995331Samw 		share = sa_get_share(group, si->directory);
14005331Samw 		if (share == NULL) {
14015331Samw 			share = sa_add_share(group, si->directory,
14025331Samw 			    SA_SHARE_TRANSIENT, &err);
14035331Samw 			if (share == NULL)
14045331Samw 				return (SA_NO_SUCH_PATH);
14055331Samw 		}
14065331Samw 	}
14075331Samw 
14085331Samw 	/*
14095331Samw 	 * Now handle the resource. Make sure that the resource is
14105331Samw 	 * transient and added to the share.
14115331Samw 	 */
14125331Samw 	resource = sa_get_share_resource(share, si->share_name);
14135331Samw 	if (resource == NULL) {
14145331Samw 		resource = sa_add_resource(share,
14155331Samw 		    si->share_name, SA_SHARE_TRANSIENT, &err);
14165331Samw 		if (resource == NULL)
14175331Samw 			return (SA_NO_SUCH_RESOURCE);
14185331Samw 	}
14195331Samw 
14205331Samw 	/* set resource attributes now */
14215331Samw 	(void) sa_set_resource_attr(resource, "description", si->comment);
14225331Samw 	(void) sa_set_resource_attr(resource, SHOPT_AD_CONTAINER,
14235331Samw 	    si->container);
14245331Samw 
14255331Samw 	return (SA_OK);
14265331Samw }
14275331Samw 
14285331Samw /*
14295331Samw  * Return smb transient shares.  Note that we really want to look at
14305331Samw  * all current shares from SMB in order to determine this. Transient
14315331Samw  * shares should be those that don't appear in either the SMF or ZFS
14325331Samw  * configurations.  Those that are in the repositories will be
14335331Samw  * filtered out by smb_build_tmp_sa_resource.
14345331Samw  */
14355331Samw static int
14365331Samw smb_list_transient(sa_handle_t handle)
14375331Samw {
14385331Samw 	int i, offset, num;
14395331Samw 	lmshare_list_t list;
14405331Samw 	int res;
14415331Samw 
14425331Samw 	num = lmshrd_num_shares();
14435331Samw 	if (num <= 0)
14445331Samw 		return (SA_OK);
14455331Samw 	offset = 0;
14465331Samw 	while (lmshrd_list(offset, &list) != NERR_InternalError) {
14475331Samw 		if (list.no == 0)
14485331Samw 			break;
14495331Samw 		for (i = 0; i < list.no; i++) {
14505331Samw 			res = smb_build_tmp_sa_resource(handle,
14515331Samw 			    &(list.smbshr[i]));
14525331Samw 			if (res != SA_OK)
14535331Samw 				return (res);
14545331Samw 		}
14555331Samw 		offset += list.no;
14565331Samw 	}
14575331Samw 
14585331Samw 	return (SA_OK);
14595331Samw }
14605331Samw 
14615331Samw /*
14625331Samw  * fix_resource_name(share, name,  prefix)
14635331Samw  *
14645331Samw  * Construct a name where the ZFS dataset has the prefix replaced with "name".
14655331Samw  */
14665331Samw static char *
14675331Samw fix_resource_name(sa_share_t share, char *name, char *prefix)
14685331Samw {
14695331Samw 	char *dataset = NULL;
14705331Samw 	char *newname = NULL;
14715331Samw 	size_t psize;
14725331Samw 	size_t nsize;
14735331Samw 
14745331Samw 	dataset = sa_get_share_attr(share, "dataset");
14755331Samw 
14765331Samw 	if (dataset != NULL && strcmp(dataset, prefix) != 0) {
14775331Samw 		psize = strlen(prefix);
14785331Samw 		if (strncmp(dataset, prefix, psize) == 0) {
14795331Samw 			/* need string plus ',' and NULL */
14805331Samw 			nsize = (strlen(dataset) - psize) + strlen(name) + 2;
14815331Samw 			newname = calloc(nsize, 1);
14825331Samw 			if (newname != NULL) {
14835331Samw 				(void) snprintf(newname, nsize, "%s%s", name,
14845331Samw 				    dataset + psize);
14855331Samw 				sa_fix_resource_name(newname);
14865331Samw 			}
14875331Samw 			sa_free_attr_string(dataset);
14885331Samw 			return (newname);
14895331Samw 		}
14905331Samw 	}
14915331Samw 	if (dataset != NULL)
14925331Samw 		sa_free_attr_string(dataset);
14935331Samw 	return (strdup(name));
14945331Samw }
14955331Samw 
14965331Samw /*
14975331Samw  * smb_parse_optstring(group, options)
14985331Samw  *
14995331Samw  * parse a compact option string into individual options. This allows
15005331Samw  * ZFS sharesmb and sharemgr "share" command to work.  group can be a
15015331Samw  * group, a share or a resource.
15025331Samw  */
15035331Samw static int
15045331Samw smb_parse_optstring(sa_group_t group, char *options)
15055331Samw {
15065331Samw 	char *dup;
15075331Samw 	char *base;
15085331Samw 	char *lasts;
15095331Samw 	char *token;
15105331Samw 	sa_optionset_t optionset;
15115331Samw 	sa_group_t parent = NULL;
15125331Samw 	sa_resource_t resource = NULL;
15135331Samw 	int iszfs = 0;
15145331Samw 	int persist = 0;
15155331Samw 	int need_optionset = 0;
15165331Samw 	int ret = SA_OK;
15175331Samw 	sa_property_t prop;
15185331Samw 
15195331Samw 	/*
15205331Samw 	 * In order to not attempt to change ZFS properties unless
15215331Samw 	 * absolutely necessary, we never do it in the legacy parsing
15225331Samw 	 * so we need to keep track of this.
15235331Samw 	 */
15245331Samw 	if (sa_is_share(group)) {
15255331Samw 		char *zfs;
15265331Samw 
15275331Samw 		parent = sa_get_parent_group(group);
15285331Samw 		if (parent != NULL) {
15295331Samw 			zfs = sa_get_group_attr(parent, "zfs");
15305331Samw 			if (zfs != NULL) {
15315331Samw 				sa_free_attr_string(zfs);
15325331Samw 				iszfs = 1;
15335331Samw 			}
15345331Samw 		}
15355331Samw 	} else {
15365331Samw 		iszfs = sa_group_is_zfs(group);
15375331Samw 		/*
15385331Samw 		 * If a ZFS group, then we need to see if a resource
15395331Samw 		 * name is being set. If so, bail with
15405331Samw 		 * SA_PROP_SHARE_ONLY, so we come back in with a share
15415331Samw 		 * instead of a group.
15425331Samw 		 */
15435331Samw 		if (strncmp(options, "name=", sizeof ("name=") - 1) == 0 ||
15445331Samw 		    strstr(options, ",name=") != NULL) {
15455331Samw 			return (SA_PROP_SHARE_ONLY);
15465331Samw 		}
15475331Samw 	}
15485331Samw 
15495331Samw 	/* do we have an existing optionset? */
15505331Samw 	optionset = sa_get_optionset(group, "smb");
15515331Samw 	if (optionset == NULL) {
15525331Samw 		/* didn't find existing optionset so create one */
15535331Samw 		optionset = sa_create_optionset(group, "smb");
15545331Samw 		if (optionset == NULL)
15555331Samw 			return (SA_NO_MEMORY);
15565331Samw 	} else {
15575331Samw 		/*
15585331Samw 		 * If an optionset already exists, we've come through
15595331Samw 		 * twice so ignore the second time.
15605331Samw 		 */
15615331Samw 		return (ret);
15625331Samw 	}
15635331Samw 
15645331Samw 	/* We need a copy of options for the next part. */
15655331Samw 	dup = strdup(options);
15665331Samw 	if (dup == NULL)
15675331Samw 		return (SA_NO_MEMORY);
15685331Samw 
15695331Samw 	/*
15705331Samw 	 * SMB properties are straightforward and are strings,
15715331Samw 	 * integers or booleans.  Properties are separated by
15725331Samw 	 * commas. It will be necessary to parse quotes due to some
15735331Samw 	 * strings not having a restricted characters set.
15745331Samw 	 *
15755331Samw 	 * Note that names will create a resource. For now, if there
15765331Samw 	 * is a set of properties "before" the first name="", those
15775331Samw 	 * properties will be placed on the group.
15785331Samw 	 */
15795331Samw 	persist = sa_is_persistent(group);
15805331Samw 	base = dup;
15815331Samw 	token = dup;
15825331Samw 	lasts = NULL;
15835331Samw 	while (token != NULL && ret == SA_OK) {
15845331Samw 		ret = SA_OK;
15855331Samw 		token = strtok_r(base, ",", &lasts);
15865331Samw 		base = NULL;
15875331Samw 		if (token != NULL) {
15885331Samw 			char *value;
15895331Samw 			/*
15905331Samw 			 * All SMB properties have values so there
15915331Samw 			 * MUST be an '=' character.  If it doesn't,
15925331Samw 			 * it is a syntax error.
15935331Samw 			 */
15945331Samw 			value = strchr(token, '=');
15955331Samw 			if (value != NULL) {
15965331Samw 				*value++ = '\0';
15975331Samw 			} else {
15985331Samw 				ret = SA_SYNTAX_ERR;
15995331Samw 				break;
16005331Samw 			}
16015331Samw 			/*
16025331Samw 			 * We may need to handle a "name" property
16035331Samw 			 * that is a ZFS imposed resource name. Each
16045331Samw 			 * name would trigger getting a new "resource"
16055331Samw 			 * to put properties on. For now, assume no
16065331Samw 			 * "name" property for special handling.
16075331Samw 			 */
16085331Samw 
16095331Samw 			if (strcmp(token, "name") == 0) {
16105331Samw 				char *prefix;
16115331Samw 				char *name = NULL;
16125331Samw 				/*
16135331Samw 				 * We have a name, so now work on the
16145331Samw 				 * resource level. We have a "share"
16155331Samw 				 * in "group" due to the caller having
16165331Samw 				 * added it. If we are called with a
16175331Samw 				 * group, the check for group/share
16185331Samw 				 * at the beginning of this function
16195331Samw 				 * will bail out the parse if there is a
16205331Samw 				 * "name" but no share.
16215331Samw 				 */
16225331Samw 				if (!iszfs) {
16235331Samw 					ret = SA_SYNTAX_ERR;
16245331Samw 					break;
16255331Samw 				}
16265331Samw 				/*
16275331Samw 				 * Make sure the parent group has the
16285331Samw 				 * "prefix" property since we will
16295331Samw 				 * need to use this for constructing
16305331Samw 				 * inherited name= values.
16315331Samw 				 */
16325331Samw 				prefix = sa_get_group_attr(parent, "prefix");
16335331Samw 				if (prefix == NULL) {
16345331Samw 					prefix = sa_get_group_attr(parent,
16355331Samw 					    "name");
16365331Samw 					if (prefix != NULL) {
16375331Samw 						(void) sa_set_group_attr(parent,
16385331Samw 						    "prefix", prefix);
16395331Samw 					}
16405331Samw 				}
16415331Samw 				name = fix_resource_name((sa_share_t)group,
16425331Samw 				    value, prefix);
16435331Samw 				if (name != NULL) {
16445331Samw 					resource = sa_add_resource(
16455331Samw 					    (sa_share_t)group, name,
16465331Samw 					    SA_SHARE_TRANSIENT, &ret);
16475331Samw 					sa_free_attr_string(name);
16485331Samw 				} else {
16495331Samw 					ret = SA_NO_MEMORY;
16505331Samw 				}
16515331Samw 				if (prefix != NULL)
16525331Samw 					sa_free_attr_string(prefix);
16535331Samw 
16545331Samw 				/* A resource level optionset is needed */
16555331Samw 
16565331Samw 				need_optionset = 1;
16575331Samw 				if (resource == NULL) {
16585331Samw 					ret = SA_NO_MEMORY;
16595331Samw 					break;
16605331Samw 				}
16615331Samw 				continue;
16625331Samw 			}
16635331Samw 
16645331Samw 			if (need_optionset) {
16655331Samw 				optionset = sa_create_optionset(resource,
16665331Samw 				    "smb");
16675331Samw 				need_optionset = 0;
16685331Samw 			}
16695331Samw 
16705331Samw 			prop = sa_create_property(token, value);
16715331Samw 			if (prop == NULL)
16725331Samw 				ret = SA_NO_MEMORY;
16735331Samw 			else
16745331Samw 				ret = sa_add_property(optionset, prop);
16755331Samw 			if (ret != SA_OK)
16765331Samw 				break;
16775331Samw 			if (!iszfs)
16785331Samw 				ret = sa_commit_properties(optionset, !persist);
16795331Samw 		}
16805331Samw 	}
16815331Samw 	free(dup);
16825331Samw 	return (ret);
16835331Samw }
16845331Samw 
16855331Samw /*
16865331Samw  * smb_sprint_option(rbuff, rbuffsize, incr, prop, sep)
16875331Samw  *
16885331Samw  * provides a mechanism to format SMB properties into legacy output
16895331Samw  * format. If the buffer would overflow, it is reallocated and grown
16905331Samw  * as appropriate. Special cases of converting internal form of values
16915331Samw  * to those used by "share" are done. this function does one property
16925331Samw  * at a time.
16935331Samw  */
16945331Samw 
16955331Samw static void
16965331Samw smb_sprint_option(char **rbuff, size_t *rbuffsize, size_t incr,
16975331Samw 			sa_property_t prop, int sep)
16985331Samw {
16995331Samw 	char *name;
17005331Samw 	char *value;
17015331Samw 	int curlen;
17025331Samw 	char *buff = *rbuff;
17035331Samw 	size_t buffsize = *rbuffsize;
17045331Samw 
17055331Samw 	name = sa_get_property_attr(prop, "type");
17065331Samw 	value = sa_get_property_attr(prop, "value");
17075331Samw 	if (buff != NULL)
17085331Samw 		curlen = strlen(buff);
17095331Samw 	else
17105331Samw 		curlen = 0;
17115331Samw 	if (name != NULL) {
17125331Samw 		int len;
17135331Samw 		len = strlen(name) + sep;
17145331Samw 
17155331Samw 		/*
17165331Samw 		 * A future RFE would be to replace this with more
17175331Samw 		 * generic code and to possibly handle more types.
17185331Samw 		 *
17195331Samw 		 * For now, everything else is treated as a string. If
17205331Samw 		 * we get any properties that aren't exactly
17215331Samw 		 * name/value pairs, we may need to
17225331Samw 		 * interpret/transform.
17235331Samw 		 */
17245331Samw 		if (value != NULL)
17255331Samw 			len += 1 + strlen(value);
17265331Samw 
17275331Samw 		while (buffsize <= (curlen + len)) {
17285331Samw 			/* need more room */
17295331Samw 			buffsize += incr;
17305331Samw 			buff = realloc(buff, buffsize);
17315331Samw 			*rbuff = buff;
17325331Samw 			*rbuffsize = buffsize;
17335331Samw 			if (buff == NULL) {
17345331Samw 				/* realloc failed so free everything */
17355331Samw 				if (*rbuff != NULL)
17365331Samw 					free(*rbuff);
17375331Samw 				goto err;
17385331Samw 			}
17395331Samw 		}
17405331Samw 		if (buff == NULL)
17415331Samw 			goto err;
17425331Samw 		(void) snprintf(buff + curlen, buffsize - curlen,
17435331Samw 		    "%s%s=%s", sep ? "," : "",
17445331Samw 		    name, value != NULL ? value : "\"\"");
17455331Samw 
17465331Samw 	}
17475331Samw err:
17485331Samw 	if (name != NULL)
17495331Samw 		sa_free_attr_string(name);
17505331Samw 	if (value != NULL)
17515331Samw 		sa_free_attr_string(value);
17525331Samw }
17535331Samw 
17545331Samw /*
17555331Samw  * smb_format_resource_options(resource, hier)
17565331Samw  *
17575331Samw  * format all the options on the group into a flattened option
17585331Samw  * string. If hier is non-zero, walk up the tree to get inherited
17595331Samw  * options.
17605331Samw  */
17615331Samw 
17625331Samw static char *
17635331Samw smb_format_options(sa_group_t group, int hier)
17645331Samw {
17655331Samw 	sa_optionset_t options = NULL;
17665331Samw 	sa_property_t prop;
17675331Samw 	int sep = 0;
17685331Samw 	char *buff;
17695331Samw 	size_t buffsize;
17705331Samw 
17715331Samw 
17725331Samw 	buff = malloc(OPT_CHUNK);
17735331Samw 	if (buff == NULL)
17745331Samw 		return (NULL);
17755331Samw 
17765331Samw 	buff[0] = '\0';
17775331Samw 	buffsize = OPT_CHUNK;
17785331Samw 
17795331Samw 	/*
17805331Samw 	 * We may have a an optionset relative to this item. format
17815331Samw 	 * these if we find them and then add any security definitions.
17825331Samw 	 */
17835331Samw 
17845331Samw 	options = sa_get_derived_optionset(group, "smb", hier);
17855331Samw 
17865331Samw 	/*
17875331Samw 	 * do the default set first but skip any option that is also
17885331Samw 	 * in the protocol specific optionset.
17895331Samw 	 */
17905331Samw 	if (options != NULL) {
17915331Samw 		for (prop = sa_get_property(options, NULL);
17925331Samw 		    prop != NULL; prop = sa_get_next_property(prop)) {
17935331Samw 			/*
17945331Samw 			 * use this one since we skipped any
17955331Samw 			 * of these that were also in
17965331Samw 			 * optdefault
17975331Samw 			 */
17985331Samw 			smb_sprint_option(&buff, &buffsize, OPT_CHUNK,
17995331Samw 			    prop, sep);
18005331Samw 			if (buff == NULL) {
18015331Samw 				/*
18025331Samw 				 * buff could become NULL if there
18035331Samw 				 * isn't enough memory for
18045331Samw 				 * smb_sprint_option to realloc()
18055331Samw 				 * as necessary. We can't really
18065331Samw 				 * do anything about it at this
18075331Samw 				 * point so we return NULL.  The
18085331Samw 				 * caller should handle the
18095331Samw 				 * failure.
18105331Samw 				 */
18115331Samw 				if (options != NULL)
18125331Samw 					sa_free_derived_optionset(
18135331Samw 					    options);
18145331Samw 				return (buff);
18155331Samw 			}
18165331Samw 			sep = 1;
18175331Samw 		}
18185331Samw 	}
18195331Samw 
18205331Samw 	if (options != NULL)
18215331Samw 		sa_free_derived_optionset(options);
18225331Samw 	return (buff);
18235331Samw }
18245331Samw 
18255331Samw /*
18265331Samw  * smb_rename_resource(resource, newname)
18275331Samw  *
18285331Samw  * Change the current exported name of the resource to newname.
18295331Samw  */
18305331Samw /*ARGSUSED*/
18315331Samw int
18325331Samw smb_rename_resource(sa_handle_t handle, sa_resource_t resource, char *newname)
18335331Samw {
18345331Samw 	int ret = SA_OK;
18355331Samw 	int err;
18365331Samw 	char *oldname;
18375331Samw 
18385331Samw 	oldname = sa_get_resource_attr(resource, "name");
18395331Samw 	if (oldname == NULL)
18405331Samw 		return (SA_NO_SUCH_RESOURCE);
18415331Samw 
18425331Samw 	err = lmshrd_rename(oldname, newname);
18435331Samw 
18445331Samw 	/* improve error values somewhat */
18455331Samw 	switch (err) {
18465331Samw 	case NERR_Success:
18475331Samw 		break;
18485331Samw 	case NERR_InternalError:
18495331Samw 		ret = SA_SYSTEM_ERR;
18505331Samw 		break;
18515331Samw 	case NERR_DuplicateShare:
18525331Samw 		ret = SA_DUPLICATE_NAME;
18535331Samw 		break;
18545331Samw 	default:
18555331Samw 		ret = SA_CONFIG_ERR;
18565331Samw 		break;
18575331Samw 	}
18585331Samw 
18595331Samw 	return (ret);
18605331Samw }
1861