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>
52*7052Samw #include <smbsrv/smb_share.h>
535331Samw #include <smbsrv/smbinfo.h>
545331Samw #include <smbsrv/libsmb.h>
555331Samw 
565331Samw /* internal functions */
575331Samw static int smb_share_init(void);
585331Samw static void smb_share_fini(void);
595331Samw static int smb_enable_share(sa_share_t);
605331Samw static int smb_share_changed(sa_share_t);
615331Samw static int smb_resource_changed(sa_resource_t);
625331Samw static int smb_rename_resource(sa_handle_t, sa_resource_t, char *);
635331Samw static int smb_disable_share(sa_share_t share, char *);
646214Sdougm static int smb_validate_property(sa_handle_t, sa_property_t, sa_optionset_t);
655331Samw static int smb_set_proto_prop(sa_property_t);
665331Samw static sa_protocol_properties_t smb_get_proto_set(void);
675331Samw static char *smb_get_status(void);
685331Samw static int smb_parse_optstring(sa_group_t, char *);
695331Samw static char *smb_format_options(sa_group_t, int);
705331Samw 
715331Samw static int smb_enable_service(void);
725331Samw 
735331Samw static int range_check_validator(int, char *);
745331Samw static int range_check_validator_zero_ok(int, char *);
755331Samw static int string_length_check_validator(int, char *);
765331Samw static int true_false_validator(int, char *);
775331Samw static int ip_address_validator_empty_ok(int, char *);
785331Samw static int ip_address_csv_list_validator_empty_ok(int, char *);
795331Samw static int path_validator(int, char *);
805331Samw 
815331Samw static int smb_enable_resource(sa_resource_t);
825331Samw static int smb_disable_resource(sa_resource_t);
835331Samw static uint64_t smb_share_features(void);
845331Samw static int smb_list_transient(sa_handle_t);
855772Sas200622 
865331Samw /* size of basic format allocation */
875331Samw #define	OPT_CHUNK	1024
885331Samw 
895772Sas200622 /* size of string for types - big enough to hold "dependency" */
905772Sas200622 #define	SCFTYPE_LEN	32
915772Sas200622 
925331Samw /*
935331Samw  * Indexes of entries in smb_proto_options table.
945331Samw  * Changes to smb_proto_options table may require
955331Samw  * an update to these values.
965331Samw  */
975331Samw #define	PROTO_OPT_WINS1			6
985331Samw #define	PROTO_OPT_WINS_EXCLUDE		8
995331Samw 
1005331Samw 
1015331Samw /*
1025331Samw  * ops vector that provides the protocol specific info and operations
1035331Samw  * for share management.
1045331Samw  */
1055331Samw 
1065331Samw struct sa_plugin_ops sa_plugin_ops = {
1075331Samw 	SA_PLUGIN_VERSION,
1085331Samw 	SMB_PROTOCOL_NAME,
1095331Samw 	smb_share_init,
1105331Samw 	smb_share_fini,
1115331Samw 	smb_enable_share,
1125331Samw 	smb_disable_share,
1135331Samw 	smb_validate_property,
1146007Sthurlow 	NULL,	/* valid_space */
1156007Sthurlow 	NULL,	/* security_prop */
1165331Samw 	smb_parse_optstring,
1175331Samw 	smb_format_options,
1185331Samw 	smb_set_proto_prop,
1195331Samw 	smb_get_proto_set,
1205331Samw 	smb_get_status,
1216007Sthurlow 	NULL,	/* space_alias */
1226007Sthurlow 	NULL,	/* update_legacy */
1236007Sthurlow 	NULL,	/* delete_legacy */
1245331Samw 	smb_share_changed,
1255331Samw 	smb_enable_resource,
1265331Samw 	smb_disable_resource,
1275331Samw 	smb_share_features,
1285331Samw 	smb_list_transient,
1295331Samw 	smb_resource_changed,
1305331Samw 	smb_rename_resource,
1316007Sthurlow 	NULL,	/* run_command */
1326007Sthurlow 	NULL,	/* command_help */
1336007Sthurlow 	NULL	/* delete_proto_section */
1345331Samw };
1355331Samw 
1365331Samw /*
1375331Samw  * option definitions.  Make sure to keep the #define for the option
1385331Samw  * index just before the entry it is the index for. Changing the order
1395331Samw  * can cause breakage.
1405331Samw  */
1415331Samw 
1425331Samw struct option_defs optdefs[] = {
143*7052Samw 	{SMB_SHROPT_AD_CONTAINER, OPT_TYPE_STRING},
144*7052Samw 	{SMB_SHROPT_NAME, OPT_TYPE_NAME},
1455331Samw 	{NULL, NULL},
1465331Samw };
1475331Samw 
1485331Samw /*
1495331Samw  * findopt(name)
1505331Samw  *
1515331Samw  * Lookup option "name" in the option table and return the table
1525331Samw  * index.
1535331Samw  */
1545331Samw 
1555331Samw static int
1565331Samw findopt(char *name)
1575331Samw {
1585331Samw 	int i;
1595331Samw 	if (name != NULL) {
1605331Samw 		for (i = 0; optdefs[i].tag != NULL; i++) {
1615331Samw 			if (strcmp(optdefs[i].tag, name) == 0)
1625331Samw 				return (i);
1635331Samw 		}
1645331Samw 	}
1655331Samw 	return (-1);
1665331Samw }
1675331Samw 
1685331Samw /*
1695331Samw  * is_a_number(number)
1705331Samw  *
1715331Samw  * is the string a number in one of the forms we want to use?
1725331Samw  */
1735331Samw 
1745331Samw static int
1755331Samw is_a_number(char *number)
1765331Samw {
1775331Samw 	int ret = 1;
1785331Samw 	int hex = 0;
1795331Samw 
1805331Samw 	if (strncmp(number, "0x", 2) == 0) {
1815331Samw 		number += 2;
1825331Samw 		hex = 1;
1835331Samw 	} else if (*number == '-') {
1845331Samw 		number++; /* skip the minus */
1855331Samw 	}
1865331Samw 
1875331Samw 	while (ret == 1 && *number != '\0') {
1885331Samw 		if (hex) {
1895331Samw 			ret = isxdigit(*number++);
1905331Samw 		} else {
1915331Samw 			ret = isdigit(*number++);
1925331Samw 		}
1935331Samw 	}
1945331Samw 	return (ret);
1955331Samw }
1965331Samw 
1975331Samw /*
1985331Samw  * validresource(name)
1995331Samw  *
2005331Samw  * Check that name only has valid characters in it. The current valid
2015331Samw  * set are the printable characters but not including:
2025331Samw  *	" / \ [ ] : | < > + ; , ? * = \t
2035331Samw  * Note that space is included and there is a maximum length.
2045331Samw  */
2055331Samw static int
2065331Samw validresource(const char *name)
2075331Samw {
2085331Samw 	const char *cp;
2095331Samw 	size_t len;
2105331Samw 
2115331Samw 	if (name == NULL)
2125331Samw 		return (B_FALSE);
2135331Samw 
2145331Samw 	len = strlen(name);
2155331Samw 	if (len == 0 || len > SA_MAX_RESOURCE_NAME)
2165331Samw 		return (B_FALSE);
2175331Samw 
2185331Samw 	if (strpbrk(name, "\"/\\[]:|<>+;,?*=\t") != NULL) {
2195331Samw 		return (B_FALSE);
2205331Samw 	}
2215331Samw 
2225331Samw 	for (cp = name; *cp != '\0'; cp++)
2235331Samw 		if (iscntrl(*cp))
2245331Samw 			return (B_FALSE);
2255331Samw 
2265331Samw 	return (B_TRUE);
2275331Samw }
2285331Samw 
2295331Samw /*
2305331Samw  * smb_isonline()
2315331Samw  *
2325331Samw  * Determine if the SMF service instance is in the online state or
2335331Samw  * not. A number of operations depend on this state.
2345331Samw  */
2355331Samw static boolean_t
2365331Samw smb_isonline(void)
2375331Samw {
2385331Samw 	char *str;
2395331Samw 	boolean_t ret = B_FALSE;
2405331Samw 
2415331Samw 	if ((str = smf_get_state(SMBD_DEFAULT_INSTANCE_FMRI)) != NULL) {
2425331Samw 		ret = (strcmp(str, SCF_STATE_STRING_ONLINE) == 0);
2435331Samw 		free(str);
2445331Samw 	}
2455331Samw 	return (ret);
2465331Samw }
2475331Samw 
2485331Samw /*
2495772Sas200622  * smb_isdisabled()
2505772Sas200622  *
2515772Sas200622  * Determine if the SMF service instance is in the disabled state or
2525772Sas200622  * not. A number of operations depend on this state.
2535772Sas200622  */
2545772Sas200622 static boolean_t
2555772Sas200622 smb_isdisabled(void)
2565772Sas200622 {
2575772Sas200622 	char *str;
2585772Sas200622 	boolean_t ret = B_FALSE;
2595772Sas200622 
2605772Sas200622 	if ((str = smf_get_state(SMBD_DEFAULT_INSTANCE_FMRI)) != NULL) {
2615772Sas200622 		ret = (strcmp(str, SCF_STATE_STRING_DISABLED) == 0);
2625772Sas200622 		free(str);
2635772Sas200622 	}
2645772Sas200622 	return (ret);
2655772Sas200622 }
2665772Sas200622 
2675772Sas200622 /*
2685772Sas200622  * smb_isautoenable()
2695772Sas200622  *
2705772Sas200622  * Determine if the SMF service instance auto_enabled set or not. A
2715772Sas200622  * number of operations depend on this state.  The property not being
2725772Sas200622  * set or being set to true means autoenable.  Only being set to false
2735772Sas200622  * is not autoenabled.
2745772Sas200622  */
2755772Sas200622 static boolean_t
2765772Sas200622 smb_isautoenable(void)
2775772Sas200622 {
2785772Sas200622 	boolean_t ret = B_TRUE;
2795772Sas200622 	scf_simple_prop_t *prop;
2805772Sas200622 	uint8_t *retstr;
2815772Sas200622 
2825772Sas200622 	prop = scf_simple_prop_get(NULL, SMBD_DEFAULT_INSTANCE_FMRI,
2835772Sas200622 	    "application", "auto_enable");
2845772Sas200622 	if (prop != NULL) {
2855772Sas200622 		retstr = scf_simple_prop_next_boolean(prop);
2865772Sas200622 		ret = *retstr != 0;
2875772Sas200622 		scf_simple_prop_free(prop);
2885772Sas200622 	}
2895772Sas200622 	return (ret);
2905772Sas200622 }
2915772Sas200622 
2925772Sas200622 /*
2936030Sjb150015  * smb_ismaint()
2946030Sjb150015  *
2956030Sjb150015  * Determine if the SMF service instance is in the disabled state or
2966030Sjb150015  * not. A number of operations depend on this state.
2976030Sjb150015  */
2986030Sjb150015 static boolean_t
2996030Sjb150015 smb_ismaint(void)
3006030Sjb150015 {
3016030Sjb150015 	char *str;
3026030Sjb150015 	boolean_t ret = B_FALSE;
3036030Sjb150015 
3046030Sjb150015 	if ((str = smf_get_state(SMBD_DEFAULT_INSTANCE_FMRI)) != NULL) {
3056030Sjb150015 		ret = (strcmp(str, SCF_STATE_STRING_MAINT) == 0);
3066030Sjb150015 		free(str);
3076030Sjb150015 	}
3086030Sjb150015 	return (ret);
3096030Sjb150015 }
3106030Sjb150015 
3116030Sjb150015 /*
3125331Samw  * smb_enable_share tells the implementation that it is to enable the share.
3135331Samw  * This entails converting the path and options into the appropriate ioctl
3145331Samw  * calls. It is assumed that all error checking of paths, etc. were
3155331Samw  * done earlier.
3165331Samw  */
3175331Samw static int
3185331Samw smb_enable_share(sa_share_t share)
3195331Samw {
3205331Samw 	char *path;
3215331Samw 	char *rname;
322*7052Samw 	smb_share_t si;
3235331Samw 	sa_resource_t resource;
3245331Samw 	boolean_t iszfs;
3255331Samw 	boolean_t privileged;
3265331Samw 	int err = SA_OK;
3275331Samw 	priv_set_t *priv_effective;
3285331Samw 	boolean_t online;
3295331Samw 
3306270Sdougm 	/*
3316270Sdougm 	 * We only start in the global zone and only run if we aren't
3326270Sdougm 	 * running Trusted Extensions.
3336270Sdougm 	 */
3346270Sdougm 	if (getzoneid() != GLOBAL_ZONEID) {
3356270Sdougm 		(void) printf(dgettext(TEXT_DOMAIN,
3366270Sdougm 		    "SMB: service not supported in local zone\n"));
3376270Sdougm 		return (SA_NOT_SUPPORTED);
3386270Sdougm 	}
3396270Sdougm 	if (is_system_labeled()) {
3406270Sdougm 		(void) printf(dgettext(TEXT_DOMAIN,
3416270Sdougm 		    "SMB: service not supported with Trusted Extensions\n"));
3426270Sdougm 		return (SA_NOT_SUPPORTED);
3436270Sdougm 	}
3446270Sdougm 
3455331Samw 	priv_effective = priv_allocset();
3465331Samw 	(void) getppriv(PRIV_EFFECTIVE, priv_effective);
3475331Samw 	privileged = (priv_isfullset(priv_effective) == B_TRUE);
3485331Samw 	priv_freeset(priv_effective);
3495331Samw 
3505331Samw 	/* get the path since it is important in several places */
3515331Samw 	path = sa_get_share_attr(share, "path");
3525331Samw 	if (path == NULL)
3535331Samw 		return (SA_NO_SUCH_PATH);
3545331Samw 
3555772Sas200622 	/*
3565772Sas200622 	 * If administratively disabled, don't try to start anything.
3575772Sas200622 	 */
3585331Samw 	online = smb_isonline();
3595772Sas200622 	if (!online && !smb_isautoenable() && smb_isdisabled())
3605772Sas200622 		goto done;
3615331Samw 
3625331Samw 	iszfs = sa_path_is_zfs(path);
3635331Samw 
3645331Samw 	if (iszfs) {
3655331Samw 
3665331Samw 		if (privileged == B_FALSE && !online) {
3675331Samw 
3685331Samw 			if (!online) {
3695331Samw 				(void) printf(dgettext(TEXT_DOMAIN,
3705331Samw 				    "SMB: Cannot share remove "
3715331Samw 				    "file system: %s\n"), path);
3725331Samw 				(void) printf(dgettext(TEXT_DOMAIN,
3735331Samw 				    "SMB: Service needs to be enabled "
3745331Samw 				    "by a privileged user\n"));
3755331Samw 				err = SA_NO_PERMISSION;
3765331Samw 				errno = EPERM;
3775331Samw 			}
3785331Samw 			if (err) {
3795331Samw 				sa_free_attr_string(path);
3805331Samw 				return (err);
3815331Samw 			}
3825331Samw 
3835331Samw 		}
3845331Samw 	}
3855331Samw 
3865331Samw 	if (privileged == B_TRUE && !online) {
3875331Samw 		err = smb_enable_service();
3885331Samw 		if (err != SA_OK) {
3895331Samw 			(void) printf(dgettext(TEXT_DOMAIN,
3905331Samw 			    "SMB: Unable to enable service\n"));
3915331Samw 			/*
3925331Samw 			 * For now, it is OK to not be able to enable
3935331Samw 			 * the service.
3945331Samw 			 */
3956030Sjb150015 			if (err == SA_BUSY || err == SA_SYSTEM_ERR)
3965331Samw 				err = SA_OK;
3975331Samw 		} else {
3985331Samw 			online = B_TRUE;
3995331Samw 		}
4005331Samw 	}
4015331Samw 
4025331Samw 	/*
4035331Samw 	 * Don't bother trying to start shares if the service isn't
4045331Samw 	 * running.
4055331Samw 	 */
4065331Samw 	if (!online)
4075331Samw 		goto done;
4085331Samw 
4095331Samw 	/* Each share can have multiple resources */
4105331Samw 	for (resource = sa_get_share_resource(share, NULL);
4115331Samw 	    resource != NULL;
4125331Samw 	    resource = sa_get_next_resource(resource)) {
413*7052Samw 		bzero(&si, sizeof (smb_share_t));
4145331Samw 		rname = sa_get_resource_attr(resource, "name");
4155331Samw 		if (rname == NULL) {
4165331Samw 			sa_free_attr_string(path);
4175331Samw 			return (SA_NO_SUCH_RESOURCE);
4185331Samw 		}
4195331Samw 
4206771Sjb150015 		smb_build_lmshare_info(rname, path, resource, &si);
4215331Samw 		sa_free_attr_string(rname);
4225331Samw 
4235331Samw 		if (!iszfs) {
424*7052Samw 			err = smb_share_add(&si);
4255331Samw 		} else {
4265331Samw 			share_t sh;
4275331Samw 
4285331Samw 			sa_sharetab_fill_zfs(share, &sh, "smb");
4295331Samw 			err = sa_share_zfs(share, (char *)path, &sh,
4305331Samw 			    &si, ZFS_SHARE_SMB);
4315331Samw 
4325331Samw 			sa_emptyshare(&sh);
4335331Samw 		}
4345331Samw 	}
4355331Samw 	if (!iszfs)
4365331Samw 		(void) sa_update_sharetab(share, "smb");
4375331Samw done:
4385331Samw 	sa_free_attr_string(path);
4395331Samw 
4405331Samw 	return (err == NERR_DuplicateShare ? 0 : err);
4415331Samw }
4425331Samw 
4435331Samw /*
4445331Samw  * This is the share for CIFS all shares have resource names.
4455331Samw  * Enable tells the smb server to update its hash. If it fails
4465331Samw  * because smb server is down, we just ignore as smb server loads
4475331Samw  * the resources from sharemanager at startup.
4485331Samw  */
4495331Samw 
4505331Samw static int
4515331Samw smb_enable_resource(sa_resource_t resource)
4525331Samw {
4535331Samw 	char *path;
4545331Samw 	char *rname;
4555331Samw 	sa_share_t share;
456*7052Samw 	smb_share_t si;
4575772Sas200622 	int ret = SA_OK;
4585772Sas200622 	int err;
4595772Sas200622 	boolean_t isonline;
4605331Samw 
4615331Samw 	share = sa_get_resource_parent(resource);
4625331Samw 	if (share == NULL)
4635331Samw 		return (SA_NO_SUCH_PATH);
4645772Sas200622 
4655772Sas200622 	/*
4665772Sas200622 	 * If administratively disabled, don't try to start anything.
4675772Sas200622 	 */
4685772Sas200622 	isonline = smb_isonline();
4695772Sas200622 	if (!isonline && !smb_isautoenable() && smb_isdisabled())
4705772Sas200622 		goto done;
4715772Sas200622 
4725772Sas200622 	if (!isonline)
4735772Sas200622 		ret = smb_enable_service();
4745772Sas200622 	if (!smb_isonline()) {
4755772Sas200622 		ret = SA_OK;
4765772Sas200622 		goto done;
4775772Sas200622 	}
4785772Sas200622 
4795331Samw 	path = sa_get_share_attr(share, "path");
4805331Samw 	if (path == NULL)
4815331Samw 		return (SA_SYSTEM_ERR);
4825331Samw 	rname = sa_get_resource_attr(resource, "name");
4835331Samw 	if (rname == NULL) {
4845331Samw 		sa_free_attr_string(path);
4855331Samw 		return (SA_NO_SUCH_RESOURCE);
4865331Samw 	}
4875331Samw 
4886771Sjb150015 	smb_build_lmshare_info(rname, path, resource, &si);
4895331Samw 	sa_free_attr_string(path);
4905331Samw 	sa_free_attr_string(rname);
4915772Sas200622 
4925772Sas200622 	/*
4935772Sas200622 	 * Attempt to add the share. Any error that occurs if it was
4945772Sas200622 	 * online is an error but don't count NERR_DuplicateName if
4955772Sas200622 	 * smb/server had to be brought online since bringing the
4965772Sas200622 	 * service up will enable the share that was just added prior
4975772Sas200622 	 * to the attempt to enable.
4985772Sas200622 	 */
4995772Sas200622 
500*7052Samw 	err = smb_share_add(&si);
5015772Sas200622 	if (err == NERR_Success || !(!isonline && err == NERR_DuplicateName))
5025772Sas200622 		(void) sa_update_sharetab(share, "smb");
5035772Sas200622 	else
5045331Samw 		return (SA_NOT_SHARED);
5055331Samw 
5065331Samw done:
5075331Samw 	return (ret);
5085331Samw }
5095331Samw 
5105331Samw /*
5115331Samw  * Remove it from smb server hash.
5125331Samw  */
5135331Samw static int
5145331Samw smb_disable_resource(sa_resource_t resource)
5155331Samw {
5165331Samw 	char *rname;
517*7052Samw 	uint32_t res;
5185331Samw 	sa_share_t share;
5195331Samw 
5205331Samw 	rname = sa_get_resource_attr(resource, "name");
5215331Samw 	if (rname == NULL)
5225331Samw 		return (SA_NO_SUCH_RESOURCE);
5235331Samw 
5245331Samw 	if (smb_isonline()) {
525*7052Samw 		res = smb_share_del(rname);
5265331Samw 		if (res != NERR_Success) {
5275331Samw 			sa_free_attr_string(rname);
5285331Samw 			return (SA_CONFIG_ERR);
5295331Samw 		}
5305331Samw 	}
5315951Sdougm 
5325951Sdougm 	sa_free_attr_string(rname);
5335951Sdougm 
5345331Samw 	share = sa_get_resource_parent(resource);
5355331Samw 	if (share != NULL) {
5365331Samw 		rname = sa_get_share_attr(share, "path");
5375331Samw 		if (rname != NULL) {
5385951Sdougm 			sa_handle_t handle;
5395951Sdougm 
5405951Sdougm 			handle = sa_find_group_handle((sa_group_t)resource);
5415951Sdougm 			(void) sa_delete_sharetab(handle, rname, "smb");
5425331Samw 			sa_free_attr_string(rname);
5435331Samw 		}
5445331Samw 	}
5455331Samw 	/*
5465331Samw 	 * Always return OK as smb/server may be down and
5475331Samw 	 * Shares will be picked up when loaded.
5485331Samw 	 */
5495331Samw 	return (SA_OK);
5505331Samw }
5515331Samw 
5525331Samw /*
5535331Samw  * smb_share_changed(sa_share_t share)
5545331Samw  *
5555331Samw  * The specified share has changed.
5565331Samw  */
5575331Samw static int
5585331Samw smb_share_changed(sa_share_t share)
5595331Samw {
5605331Samw 	char *path;
5615331Samw 	sa_resource_t resource;
5625331Samw 
5635331Samw 	/* get the path since it is important in several places */
5645331Samw 	path = sa_get_share_attr(share, "path");
5655331Samw 	if (path == NULL)
5665331Samw 		return (SA_NO_SUCH_PATH);
5675331Samw 	for (resource = sa_get_share_resource(share, NULL);
5685331Samw 	    resource != NULL;
5695331Samw 	    resource = sa_get_next_resource(resource))
5705331Samw 		(void) smb_resource_changed(resource);
5715331Samw 
5725331Samw 	sa_free_attr_string(path);
5735331Samw 
5745331Samw 	return (SA_OK);
5755331Samw }
5765331Samw 
5775331Samw /*
5785331Samw  * smb_resource_changed(sa_resource_t resource)
5795331Samw  *
5805331Samw  * The specified resource has changed.
5815331Samw  */
5825331Samw static int
5835331Samw smb_resource_changed(sa_resource_t resource)
5845331Samw {
585*7052Samw 	uint32_t res;
586*7052Samw 	smb_share_t si;
587*7052Samw 	smb_share_t new_si;
5885331Samw 	char *rname, *path;
5895331Samw 	sa_share_t share;
5905331Samw 
5915331Samw 	rname = sa_get_resource_attr(resource, "name");
5925331Samw 	if (rname == NULL)
5935331Samw 		return (SA_NO_SUCH_RESOURCE);
5945331Samw 
5955331Samw 	share = sa_get_resource_parent(resource);
5965331Samw 	if (share == NULL) {
5975331Samw 		sa_free_attr_string(rname);
5985331Samw 		return (SA_CONFIG_ERR);
5995331Samw 	}
6005331Samw 
6015331Samw 	path = sa_get_share_attr(share, "path");
6025331Samw 	if (path == NULL) {
6035331Samw 		sa_free_attr_string(rname);
6045331Samw 		return (SA_NO_SUCH_PATH);
6055331Samw 	}
6065331Samw 
6075331Samw 	if (!smb_isonline()) {
6085331Samw 		sa_free_attr_string(rname);
6095331Samw 		return (SA_OK);
6105331Samw 	}
6115331Samw 
6125331Samw 	/* Update the share cache in smb/server */
613*7052Samw 	res = smb_share_get(rname, &si);
6145331Samw 	if (res != NERR_Success) {
6155331Samw 		sa_free_attr_string(path);
6165331Samw 		sa_free_attr_string(rname);
6175331Samw 		return (SA_CONFIG_ERR);
6185331Samw 	}
6195331Samw 
6206771Sjb150015 	smb_build_lmshare_info(rname, path, resource, &new_si);
6215331Samw 	sa_free_attr_string(path);
6225331Samw 	sa_free_attr_string(rname);
6235331Samw 
6245331Samw 	/*
6255331Samw 	 * Update all fields from sa_share_t
6265331Samw 	 * Get derived values.
6275331Samw 	 */
628*7052Samw 	if (smb_share_set(&new_si) != SMB_SHARE_DSUCCESS)
6295331Samw 		return (SA_CONFIG_ERR);
6305331Samw 	return (smb_enable_service());
6315331Samw }
6325331Samw 
6335331Samw /*
6345800Sdougm  * smb_disable_share(sa_share_t share, char *path)
6355331Samw  *
6365800Sdougm  * Unshare the specified share. Note that "path" is the same
6375800Sdougm  * path as what is in the "share" object. It is passed in to avoid an
6385800Sdougm  * additional lookup. A missing "path" value makes this a no-op
6395800Sdougm  * function.
6405331Samw  */
6415331Samw static int
6425331Samw smb_disable_share(sa_share_t share, char *path)
6435331Samw {
6445331Samw 	char *rname;
6455331Samw 	sa_resource_t resource;
6465800Sdougm 	sa_group_t parent;
6475331Samw 	boolean_t iszfs;
6485331Samw 	int err = SA_OK;
6495951Sdougm 	sa_handle_t handle;
6505331Samw 
6515800Sdougm 	if (path == NULL)
6525800Sdougm 		return (err);
6535800Sdougm 
6545800Sdougm 	/*
6555800Sdougm 	 * If the share is in a ZFS group we need to handle it
6565800Sdougm 	 * differently.  Just being on a ZFS file system isn't
6575800Sdougm 	 * enough since we may be in a legacy share case.
6585800Sdougm 	 */
6595800Sdougm 	parent = sa_get_parent_group(share);
6605800Sdougm 	iszfs = sa_group_is_zfs(parent);
6615800Sdougm 
6625331Samw 	if (!smb_isonline())
6635331Samw 		goto done;
6645331Samw 
6655331Samw 	for (resource = sa_get_share_resource(share, NULL);
6665331Samw 	    resource != NULL;
6675331Samw 	    resource = sa_get_next_resource(resource)) {
6685331Samw 		rname = sa_get_resource_attr(resource, "name");
6695331Samw 		if (rname == NULL) {
6705331Samw 			continue;
6715331Samw 		}
6725331Samw 		if (!iszfs) {
673*7052Samw 			err = smb_share_del(rname);
6745331Samw 			switch (err) {
6755331Samw 			case NERR_NetNameNotFound:
6765331Samw 			case NERR_Success:
6775331Samw 				err = SA_OK;
6785331Samw 				break;
6795331Samw 			default:
6805331Samw 				err = SA_CONFIG_ERR;
6815331Samw 				break;
6825331Samw 			}
6835331Samw 		} else {
6845331Samw 			share_t sh;
6855331Samw 
6865331Samw 			sa_sharetab_fill_zfs(share, &sh, "smb");
6875331Samw 			err = sa_share_zfs(share, (char *)path, &sh,
6885331Samw 			    rname, ZFS_UNSHARE_SMB);
6895331Samw 			sa_emptyshare(&sh);
6905331Samw 		}
6915331Samw 		sa_free_attr_string(rname);
6925331Samw 	}
6935331Samw done:
6945951Sdougm 	if (!iszfs) {
6955951Sdougm 		handle = sa_find_group_handle((sa_group_t)share);
6965951Sdougm 		if (handle != NULL)
6975951Sdougm 			(void) sa_delete_sharetab(handle, path, "smb");
6985951Sdougm 		else
6995951Sdougm 			err = SA_SYSTEM_ERR;
7005951Sdougm 	}
7015331Samw 	return (err);
7025331Samw }
7035331Samw 
7045331Samw /*
7056214Sdougm  * smb_validate_property(handle, property, parent)
7065331Samw  *
7075331Samw  * Check that the property has a legitimate value for its type.
7086214Sdougm  * Handle isn't currently used but may need to be in the future.
7095331Samw  */
7105331Samw 
7116214Sdougm /*ARGSUSED*/
7125331Samw static int
7136214Sdougm smb_validate_property(sa_handle_t handle, sa_property_t property,
7146214Sdougm     sa_optionset_t parent)
7155331Samw {
7165331Samw 	int ret = SA_OK;
7175331Samw 	char *propname;
7185331Samw 	int optindex;
7195331Samw 	sa_group_t parent_group;
7205331Samw 	char *value;
7215331Samw 
7225331Samw 	propname = sa_get_property_attr(property, "type");
7235331Samw 
7245331Samw 	if ((optindex = findopt(propname)) < 0)
7255331Samw 		ret = SA_NO_SUCH_PROP;
7265331Samw 
7275331Samw 	/* need to validate value range here as well */
7285331Samw 	if (ret == SA_OK) {
7295331Samw 		parent_group = sa_get_parent_group((sa_share_t)parent);
7305331Samw 		if (optdefs[optindex].share && !sa_is_share(parent_group))
7315331Samw 			ret = SA_PROP_SHARE_ONLY;
7325331Samw 	}
7335331Samw 	if (ret != SA_OK) {
7345331Samw 		if (propname != NULL)
7355331Samw 			sa_free_attr_string(propname);
7365331Samw 		return (ret);
7375331Samw 	}
7385331Samw 
7395331Samw 	value = sa_get_property_attr(property, "value");
7405331Samw 	if (value != NULL) {
7415331Samw 		/* first basic type checking */
7425331Samw 		switch (optdefs[optindex].type) {
7435331Samw 		case OPT_TYPE_NUMBER:
7445331Samw 			/* check that the value is all digits */
7455331Samw 			if (!is_a_number(value))
7465331Samw 				ret = SA_BAD_VALUE;
7475331Samw 			break;
7485331Samw 		case OPT_TYPE_BOOLEAN:
7495331Samw 			if (strlen(value) == 0 ||
7505331Samw 			    strcasecmp(value, "true") == 0 ||
7515331Samw 			    strcmp(value, "1") == 0 ||
7525331Samw 			    strcasecmp(value, "false") == 0 ||
7535331Samw 			    strcmp(value, "0") == 0) {
7545331Samw 				ret = SA_OK;
7555331Samw 			} else {
7565331Samw 				ret = SA_BAD_VALUE;
7575331Samw 			}
7585331Samw 			break;
7595331Samw 		case OPT_TYPE_NAME:
7605331Samw 			/*
7615331Samw 			 * Make sure no invalid characters
7625331Samw 			 */
7635331Samw 			if (validresource(value) == B_FALSE)
7645331Samw 				ret = SA_BAD_VALUE;
7655331Samw 			break;
7665331Samw 		case OPT_TYPE_STRING:
7675331Samw 			/* whatever is here should be ok */
7685331Samw 			break;
7695331Samw 		default:
7705331Samw 			break;
7715331Samw 		}
7725331Samw 	}
7735331Samw 
7745331Samw 	if (value != NULL)
7755331Samw 		sa_free_attr_string(value);
7765331Samw 	if (ret == SA_OK && optdefs[optindex].check != NULL)
7775331Samw 		/* do the property specific check */
7785331Samw 		ret = optdefs[optindex].check(property);
7795331Samw 
7805331Samw 	if (propname != NULL)
7815331Samw 		sa_free_attr_string(propname);
7825331Samw 	return (ret);
7835331Samw }
7845331Samw 
7855331Samw /*
7865331Samw  * Protocol management functions
7875331Samw  *
7885331Samw  * properties defined in the default files are defined in
7895331Samw  * proto_option_defs for parsing and validation.
7905331Samw  */
7915331Samw 
7925331Samw struct smb_proto_option_defs {
7935331Samw 	int smb_index;
7945331Samw 	int32_t minval;
7955331Samw 	int32_t maxval; /* In case of length of string this should be max */
7965331Samw 	int (*validator)(int, char *);
7975331Samw 	int32_t	refresh;
7985331Samw } smb_proto_options[] = {
7995772Sas200622 	{ SMB_CI_SYS_CMNT, 0, MAX_VALUE_BUFLEN,
8005772Sas200622 	    string_length_check_validator, SMB_REFRESH_REFRESH },
8015772Sas200622 	{ SMB_CI_MAX_WORKERS, 64, 1024, range_check_validator,
8025772Sas200622 	    SMB_REFRESH_REFRESH },
8035772Sas200622 	{ SMB_CI_NBSCOPE, 0, MAX_VALUE_BUFLEN,
8045772Sas200622 	    string_length_check_validator, 0 },
8055772Sas200622 	{ SMB_CI_LM_LEVEL, 2, 5, range_check_validator, 0 },
8065772Sas200622 	{ SMB_CI_KEEPALIVE, 20, 5400, range_check_validator_zero_ok,
8075772Sas200622 	    SMB_REFRESH_REFRESH },
8085772Sas200622 	{ SMB_CI_WINS_SRV1, 0, MAX_VALUE_BUFLEN,
8095772Sas200622 	    ip_address_validator_empty_ok, SMB_REFRESH_REFRESH },
8105772Sas200622 	{ SMB_CI_WINS_SRV2, 0, MAX_VALUE_BUFLEN,
8115772Sas200622 	    ip_address_validator_empty_ok, SMB_REFRESH_REFRESH },
8125772Sas200622 	{ SMB_CI_WINS_EXCL, 0, MAX_VALUE_BUFLEN,
8135772Sas200622 	    ip_address_csv_list_validator_empty_ok, SMB_REFRESH_REFRESH },
8145772Sas200622 	{ SMB_CI_SIGNING_ENABLE, 0, 0, true_false_validator,
8155772Sas200622 	    SMB_REFRESH_REFRESH },
8165772Sas200622 	{ SMB_CI_SIGNING_REQD, 0, 0, true_false_validator,
8175772Sas200622 	    SMB_REFRESH_REFRESH },
8185772Sas200622 	{ SMB_CI_RESTRICT_ANON, 0, 0, true_false_validator,
8195772Sas200622 	    SMB_REFRESH_REFRESH },
8205772Sas200622 	{ SMB_CI_DOMAIN_SRV, 0, MAX_VALUE_BUFLEN,
8215772Sas200622 	    ip_address_validator_empty_ok, 0 },
8225772Sas200622 	{ SMB_CI_ADS_SITE, 0, MAX_VALUE_BUFLEN,
8235772Sas200622 	    string_length_check_validator, SMB_REFRESH_REFRESH },
8245772Sas200622 	{ SMB_CI_DYNDNS_ENABLE, 0, 0, true_false_validator, 0 },
8255772Sas200622 	{ SMB_CI_AUTOHOME_MAP, 0, MAX_VALUE_BUFLEN, path_validator, 0 },
8265331Samw };
8275331Samw 
8285772Sas200622 #define	SMB_OPT_NUM \
8295772Sas200622 	(sizeof (smb_proto_options) / sizeof (smb_proto_options[0]))
8305772Sas200622 
8315331Samw /*
8325331Samw  * Check the range of value as int range.
8335331Samw  */
8345331Samw static int
8355331Samw range_check_validator(int index, char *value)
8365331Samw {
8375331Samw 	int ret = SA_OK;
8385331Samw 
8395331Samw 	if (!is_a_number(value)) {
8405331Samw 		ret = SA_BAD_VALUE;
8415331Samw 	} else {
8425331Samw 		int val;
8435331Samw 		val = strtoul(value, NULL, 0);
8445331Samw 		if (val < smb_proto_options[index].minval ||
8455331Samw 		    val > smb_proto_options[index].maxval)
8465331Samw 			ret = SA_BAD_VALUE;
8475331Samw 	}
8485331Samw 	return (ret);
8495331Samw }
8505331Samw 
8515331Samw /*
8525331Samw  * Check the range of value as int range.
8535331Samw  */
8545331Samw static int
8555331Samw range_check_validator_zero_ok(int index, char *value)
8565331Samw {
8575331Samw 	int ret = SA_OK;
8585331Samw 
8595331Samw 	if (!is_a_number(value)) {
8605331Samw 		ret = SA_BAD_VALUE;
8615331Samw 	} else {
8625331Samw 		int val;
8635331Samw 		val = strtoul(value, NULL, 0);
8645331Samw 		if (val == 0)
8655331Samw 			ret = SA_OK;
8665331Samw 		else {
8675331Samw 			if (val < smb_proto_options[index].minval ||
8685331Samw 			    val > smb_proto_options[index].maxval)
8695331Samw 			ret = SA_BAD_VALUE;
8705331Samw 		}
8715331Samw 	}
8725331Samw 	return (ret);
8735331Samw }
8745331Samw 
8755331Samw /*
8765331Samw  * Check the length of the string
8775331Samw  */
8785331Samw static int
8795331Samw string_length_check_validator(int index, char *value)
8805331Samw {
8815331Samw 	int ret = SA_OK;
8825331Samw 
8835331Samw 	if (value == NULL)
8845331Samw 		return (SA_BAD_VALUE);
8855331Samw 	if (strlen(value) > smb_proto_options[index].maxval)
8865331Samw 		ret = SA_BAD_VALUE;
8875331Samw 	return (ret);
8885331Samw }
8895331Samw 
8905331Samw /*
8915331Samw  * Check yes/no
8925331Samw  */
8935331Samw /*ARGSUSED*/
8945331Samw static int
8955331Samw true_false_validator(int index, char *value)
8965331Samw {
8975331Samw 	if (value == NULL)
8985331Samw 		return (SA_BAD_VALUE);
8995331Samw 	if ((strcasecmp(value, "true") == 0) ||
9005331Samw 	    (strcasecmp(value, "false") == 0))
9015331Samw 		return (SA_OK);
9025331Samw 	return (SA_BAD_VALUE);
9035331Samw }
9045331Samw 
9055331Samw /*
9065331Samw  * Check IP address.
9075331Samw  */
9085331Samw /*ARGSUSED*/
9095331Samw static int
9105331Samw ip_address_validator_empty_ok(int index, char *value)
9115331Samw {
9125331Samw 	char sbytes[16];
9135331Samw 	int len;
9145331Samw 
9155331Samw 	if (value == NULL)
9165331Samw 		return (SA_OK);
9175331Samw 	len = strlen(value);
9185331Samw 	if (len == 0)
9195331Samw 		return (SA_OK);
9205331Samw 	if (inet_pton(AF_INET, value, (void *)sbytes) != 1)
9215331Samw 		return (SA_BAD_VALUE);
9225331Samw 
9235331Samw 	return (SA_OK);
9245331Samw }
9255331Samw 
9265331Samw /*
9275331Samw  * Check IP address list
9285331Samw  */
9295331Samw /*ARGSUSED*/
9305331Samw static int
9315331Samw ip_address_csv_list_validator_empty_ok(int index, char *value)
9325331Samw {
9335331Samw 	char sbytes[16];
9345331Samw 	char *ip, *tmp, *ctx;
9355331Samw 
9365331Samw 	if (value == NULL || *value == '\0')
9375331Samw 		return (SA_OK);
9385331Samw 
9395331Samw 	if (strlen(value) > MAX_VALUE_BUFLEN)
9405331Samw 		return (SA_BAD_VALUE);
9415331Samw 
9425331Samw 	if ((tmp = strdup(value)) == NULL)
9435331Samw 		return (SA_NO_MEMORY);
9445331Samw 
9455331Samw 	ip = strtok_r(tmp, ",", &ctx);
9465331Samw 	while (ip) {
9475331Samw 		if (strlen(ip) == 0) {
9485331Samw 			free(tmp);
9495331Samw 			return (SA_BAD_VALUE);
9505331Samw 		}
9515331Samw 		if (*ip != 0) {
9525331Samw 			if (inet_pton(AF_INET, ip,
9535331Samw 			    (void *)sbytes) != 1) {
9545331Samw 				free(tmp);
9555331Samw 				return (SA_BAD_VALUE);
9565331Samw 			}
9575331Samw 		}
9585331Samw 		ip = strtok_r(0, ",", &ctx);
9595331Samw 	}
9605331Samw 
9615331Samw 	free(tmp);
9625331Samw 	return (SA_OK);
9635331Samw }
9645331Samw 
9655331Samw /*
9665331Samw  * Check path
9675331Samw  */
9685331Samw /*ARGSUSED*/
9695331Samw static int
9705331Samw path_validator(int index, char *value)
9715331Samw {
9725331Samw 	struct stat buffer;
9735331Samw 	int fd, status;
9745331Samw 
9755331Samw 	if (value == NULL)
9765331Samw 		return (SA_BAD_VALUE);
9775331Samw 
9785331Samw 	fd = open(value, O_RDONLY);
9795331Samw 	if (fd < 0)
9805331Samw 		return (SA_BAD_VALUE);
9815331Samw 
9825331Samw 	status = fstat(fd, &buffer);
9835331Samw 	(void) close(fd);
9845331Samw 
9855331Samw 	if (status < 0)
9865331Samw 		return (SA_BAD_VALUE);
9875331Samw 
9885331Samw 	if (buffer.st_mode & S_IFDIR)
9895331Samw 		return (SA_OK);
9905331Samw 	return (SA_BAD_VALUE);
9915331Samw }
9925331Samw 
9935331Samw /*
9945331Samw  * the protoset holds the defined options so we don't have to read
9955331Samw  * them multiple times
9965331Samw  */
9975331Samw static sa_protocol_properties_t protoset;
9985331Samw 
9995331Samw static int
10005331Samw findprotoopt(char *name)
10015331Samw {
10025331Samw 	int i;
10035772Sas200622 	char *sc_name;
10045772Sas200622 
10055772Sas200622 	for (i = 0; i < SMB_OPT_NUM; i++) {
10065772Sas200622 		sc_name = smb_config_getname(smb_proto_options[i].smb_index);
10075772Sas200622 		if (strcasecmp(sc_name, name) == 0)
10085331Samw 			return (i);
10095331Samw 	}
10105772Sas200622 
10115331Samw 	return (-1);
10125331Samw }
10135331Samw 
10145331Samw /*
10155331Samw  * smb_load_proto_properties()
10165331Samw  *
10175331Samw  * read the smb config values from SMF.
10185331Samw  */
10195331Samw 
10205331Samw static int
10215331Samw smb_load_proto_properties()
10225331Samw {
10235331Samw 	sa_property_t prop;
10245772Sas200622 	char value[MAX_VALUE_BUFLEN];
10255772Sas200622 	char *name;
10265331Samw 	int index;
10276019Sdougm 	int ret = SA_OK;
10285772Sas200622 	int rc;
10295331Samw 
10305331Samw 	protoset = sa_create_protocol_properties(SMB_PROTOCOL_NAME);
10315331Samw 	if (protoset == NULL)
10325331Samw 		return (SA_NO_MEMORY);
10335331Samw 
10346019Sdougm 	for (index = 0; index < SMB_OPT_NUM && ret == SA_OK; index++) {
10355772Sas200622 		rc = smb_config_get(smb_proto_options[index].smb_index,
10365772Sas200622 		    value, sizeof (value));
10375772Sas200622 		if (rc != SMBD_SMF_OK)
10385772Sas200622 			continue;
10395772Sas200622 		name = smb_config_getname(smb_proto_options[index].smb_index);
10405772Sas200622 		prop = sa_create_property(name, value);
10415454Sdougm 		if (prop != NULL)
10426019Sdougm 			ret = sa_add_protocol_property(protoset, prop);
10436019Sdougm 		else
10446019Sdougm 			ret = SA_NO_MEMORY;
10455331Samw 	}
10466019Sdougm 	return (ret);
10475331Samw }
10485331Samw 
10495331Samw /*
10505331Samw  * smb_share_init()
10515331Samw  *
10525331Samw  * Initialize the smb plugin.
10535331Samw  */
10545331Samw 
10555331Samw static int
10565331Samw smb_share_init(void)
10575331Samw {
10585331Samw 	int ret = SA_OK;
10595331Samw 
10605331Samw 	if (sa_plugin_ops.sa_init != smb_share_init)
10615331Samw 		return (SA_SYSTEM_ERR);
10625331Samw 
10636019Sdougm 	ret = smb_load_proto_properties();
10645331Samw 
10655331Samw 	return (ret);
10665331Samw }
10675331Samw 
10685331Samw /*
10695331Samw  * smb_share_fini()
10705331Samw  *
10715331Samw  */
10725331Samw static void
10735331Samw smb_share_fini(void)
10745331Samw {
10755331Samw 	xmlFreeNode(protoset);
10765331Samw 	protoset = NULL;
10775772Sas200622 
1078*7052Samw 	(void) smb_share_dclose();
10795331Samw }
10805331Samw 
10815331Samw /*
10825331Samw  * smb_get_proto_set()
10835331Samw  *
10845331Samw  * Return an optionset with all the protocol specific properties in
10855331Samw  * it.
10865331Samw  */
10875331Samw static sa_protocol_properties_t
10885331Samw smb_get_proto_set(void)
10895331Samw {
10905331Samw 	return (protoset);
10915331Samw }
10925331Samw 
10935331Samw /*
10945772Sas200622  * smb_enable_dependencies()
10955772Sas200622  *
10965772Sas200622  * SMBD_DEFAULT_INSTANCE_FMRI may have some dependencies that aren't
10975772Sas200622  * enabled. This will attempt to enable all of them.
10985772Sas200622  */
10995772Sas200622 static void
11005772Sas200622 smb_enable_dependencies(const char *fmri)
11015772Sas200622 {
11025772Sas200622 	scf_handle_t *handle;
11035772Sas200622 	scf_service_t *service;
11045772Sas200622 	scf_instance_t *inst = NULL;
11055772Sas200622 	scf_iter_t *iter;
11065772Sas200622 	scf_property_t *prop;
11075772Sas200622 	scf_value_t *value;
11085772Sas200622 	scf_propertygroup_t *pg;
11095772Sas200622 	scf_scope_t *scope;
11105772Sas200622 	char type[SCFTYPE_LEN];
11115772Sas200622 	char *dependency;
11125772Sas200622 	char *servname;
11135772Sas200622 	int maxlen;
11145772Sas200622 
11155772Sas200622 	/*
11165772Sas200622 	 * Get all required handles and storage.
11175772Sas200622 	 */
11185772Sas200622 	handle = scf_handle_create(SCF_VERSION);
11195772Sas200622 	if (handle == NULL)
11205772Sas200622 		return;
11215772Sas200622 
11225772Sas200622 	if (scf_handle_bind(handle) != 0) {
11235772Sas200622 		scf_handle_destroy(handle);
11245772Sas200622 		return;
11255772Sas200622 	}
11265772Sas200622 
11275772Sas200622 	maxlen = scf_limit(SCF_LIMIT_MAX_VALUE_LENGTH);
11285772Sas200622 	if (maxlen == (ssize_t)-1)
11295772Sas200622 		maxlen = MAXPATHLEN;
11305772Sas200622 
11315772Sas200622 	dependency = malloc(maxlen);
11325772Sas200622 
11335772Sas200622 	service = scf_service_create(handle);
11345772Sas200622 
11355772Sas200622 	iter = scf_iter_create(handle);
11365772Sas200622 
11375772Sas200622 	pg = scf_pg_create(handle);
11385772Sas200622 
11395772Sas200622 	prop = scf_property_create(handle);
11405772Sas200622 
11415772Sas200622 	value = scf_value_create(handle);
11425772Sas200622 
11435772Sas200622 	scope = scf_scope_create(handle);
11445772Sas200622 
11455772Sas200622 	if (service == NULL || iter == NULL || pg == NULL || prop == NULL ||
11465772Sas200622 	    value == NULL || scope == NULL || dependency == NULL)
11475772Sas200622 		goto done;
11485772Sas200622 
11495772Sas200622 	/*
11505772Sas200622 	 *  We passed in the FMRI for the default instance but for
11515772Sas200622 	 *  some things we need the simple form so construct it. Since
11525772Sas200622 	 *  we reuse the storage that dependency points to, we need to
11535772Sas200622 	 *  use the servname early.
11545772Sas200622 	 */
11555772Sas200622 	(void) snprintf(dependency, maxlen, "%s", fmri + sizeof ("svc:"));
11565772Sas200622 	servname = strrchr(dependency, ':');
11575772Sas200622 	if (servname == NULL)
11585772Sas200622 		goto done;
11595772Sas200622 	*servname = '\0';
11605772Sas200622 	servname = dependency;
11615772Sas200622 
11625772Sas200622 	/*
11635772Sas200622 	 * Setup to iterate over the service property groups, only
11645772Sas200622 	 * looking at those that are "dependency" types. The "entity"
11655772Sas200622 	 * property will have the FMRI of the service we are dependent
11665772Sas200622 	 * on.
11675772Sas200622 	 */
11685772Sas200622 	if (scf_handle_get_scope(handle, SCF_SCOPE_LOCAL, scope) != 0)
11695772Sas200622 		goto done;
11705772Sas200622 
11715772Sas200622 	if (scf_scope_get_service(scope, servname, service) != 0)
11725772Sas200622 		goto done;
11735772Sas200622 
11745772Sas200622 	if (scf_iter_service_pgs(iter, service) != 0)
11755772Sas200622 		goto done;
11765772Sas200622 
11775772Sas200622 	while (scf_iter_next_pg(iter, pg) > 0) {
11785772Sas200622 		char *services[2];
11795772Sas200622 		/*
11805772Sas200622 		 * Have a property group for the service. See if it is
11815772Sas200622 		 * a dependency pg and only do operations on those.
11825772Sas200622 		 */
11835772Sas200622 		if (scf_pg_get_type(pg, type, SCFTYPE_LEN) <= 0)
11845772Sas200622 			continue;
11855772Sas200622 
11865772Sas200622 		if (strncmp(type, SCF_GROUP_DEPENDENCY, SCFTYPE_LEN) != 0)
11875772Sas200622 			continue;
11885772Sas200622 		/*
11895772Sas200622 		 * Have a dependency.  Attempt to enable it.
11905772Sas200622 		 */
11915772Sas200622 		if (scf_pg_get_property(pg, SCF_PROPERTY_ENTITIES, prop) != 0)
11925772Sas200622 			continue;
11935772Sas200622 
11945772Sas200622 		if (scf_property_get_value(prop, value) != 0)
11955772Sas200622 			continue;
11965772Sas200622 
11975772Sas200622 		services[1] = NULL;
11985772Sas200622 
11995772Sas200622 		if (scf_value_get_as_string(value, dependency, maxlen) > 0) {
12005772Sas200622 			services[0] = dependency;
12015772Sas200622 			_check_services(services);
12025772Sas200622 		}
12035772Sas200622 	}
12045772Sas200622 
12055772Sas200622 done:
12065772Sas200622 	if (dependency != NULL)
12075772Sas200622 		free(dependency);
12085772Sas200622 	if (value != NULL)
12095772Sas200622 		scf_value_destroy(value);
12105772Sas200622 	if (prop != NULL)
12115772Sas200622 		scf_property_destroy(prop);
12125772Sas200622 	if (pg != NULL)
12135772Sas200622 		scf_pg_destroy(pg);
12145772Sas200622 	if (iter != NULL)
12155772Sas200622 		scf_iter_destroy(iter);
12165772Sas200622 	if (scope != NULL)
12175772Sas200622 		scf_scope_destroy(scope);
12185772Sas200622 	if (inst != NULL)
12195772Sas200622 		scf_instance_destroy(inst);
12205772Sas200622 	if (service != NULL)
12215772Sas200622 		scf_service_destroy(service);
12225772Sas200622 
12235772Sas200622 	(void) scf_handle_unbind(handle);
12245772Sas200622 	scf_handle_destroy(handle);
12255772Sas200622 }
12265772Sas200622 
12275772Sas200622 /*
12285331Samw  * How long to wait for service to come online
12295331Samw  */
12305331Samw #define	WAIT_FOR_SERVICE	15
12315331Samw 
12325331Samw /*
12335331Samw  * smb_enable_service()
12345331Samw  *
12355331Samw  */
12365331Samw static int
12375331Samw smb_enable_service(void)
12385331Samw {
12395331Samw 	int i;
12405331Samw 	int ret = SA_OK;
12415772Sas200622 	char *service[] = { SMBD_DEFAULT_INSTANCE_FMRI, NULL };
12425331Samw 
12435331Samw 	if (!smb_isonline()) {
12445772Sas200622 		/*
12455772Sas200622 		 * Attempt to start the idmap, and other dependent
12465772Sas200622 		 * services, first.  If it fails, the SMB service will
12475772Sas200622 		 * ultimately fail so we use that as the error.  If we
12485772Sas200622 		 * don't try to enable idmap, smb won't start the
12495772Sas200622 		 * first time unless the admin has done it
12505772Sas200622 		 * manually. The service could be administratively
12515772Sas200622 		 * disabled so we won't always get started.
12525772Sas200622 		 */
12535772Sas200622 		smb_enable_dependencies(SMBD_DEFAULT_INSTANCE_FMRI);
12545772Sas200622 		_check_services(service);
12555331Samw 
12565331Samw 		/* Wait for service to come online */
12575331Samw 		for (i = 0; i < WAIT_FOR_SERVICE; i++) {
12585331Samw 			if (smb_isonline()) {
12595772Sas200622 				ret =  SA_OK;
12605331Samw 				break;
12616030Sjb150015 			} else if (smb_ismaint()) {
12626030Sjb150015 				/* maintenance requires help */
12636030Sjb150015 				ret = SA_SYSTEM_ERR;
12646030Sjb150015 				break;
12656030Sjb150015 			} else if (smb_isdisabled()) {
12666030Sjb150015 				/* disabled is ok */
12676030Sjb150015 				ret = SA_OK;
12686030Sjb150015 				break;
12695331Samw 			} else {
12706030Sjb150015 				/* try another time */
12715331Samw 				ret = SA_BUSY;
12725331Samw 				(void) sleep(1);
12735331Samw 			}
12745331Samw 		}
12755331Samw 	}
12765331Samw 	return (ret);
12775331Samw }
12785331Samw 
12795331Samw /*
12805331Samw  * smb_validate_proto_prop(index, name, value)
12815331Samw  *
12825331Samw  * Verify that the property specified by name can take the new
12835331Samw  * value. This is a sanity check to prevent bad values getting into
12845331Samw  * the default files.
12855331Samw  */
12865331Samw static int
12875331Samw smb_validate_proto_prop(int index, char *name, char *value)
12885331Samw {
12895331Samw 	if ((name == NULL) || (index < 0))
12905331Samw 		return (SA_BAD_VALUE);
12915331Samw 
12925331Samw 	if (smb_proto_options[index].validator == NULL)
12935331Samw 		return (SA_OK);
12945331Samw 
12955331Samw 	if (smb_proto_options[index].validator(index, value) == SA_OK)
12965331Samw 		return (SA_OK);
12975331Samw 	return (SA_BAD_VALUE);
12985331Samw }
12995331Samw 
13005331Samw /*
13015331Samw  * smb_set_proto_prop(prop)
13025331Samw  *
13035331Samw  * check that prop is valid.
13045331Samw  */
13055331Samw /*ARGSUSED*/
13065331Samw static int
13075331Samw smb_set_proto_prop(sa_property_t prop)
13085331Samw {
13095331Samw 	int ret = SA_OK;
13105331Samw 	char *name;
13115331Samw 	char *value;
13125331Samw 	int index = -1;
13135521Sas200622 	struct smb_proto_option_defs *opt;
13145331Samw 
13155331Samw 	name = sa_get_property_attr(prop, "type");
13165331Samw 	value = sa_get_property_attr(prop, "value");
13175331Samw 	if (name != NULL && value != NULL) {
13185331Samw 		index = findprotoopt(name);
13195331Samw 		if (index >= 0) {
13205331Samw 			/* should test for valid value */
13215331Samw 			ret = smb_validate_proto_prop(index, name, value);
13225331Samw 			if (ret == SA_OK) {
13235521Sas200622 				opt = &smb_proto_options[index];
13245521Sas200622 
13255331Samw 				/* Save to SMF */
13265772Sas200622 				(void) smb_config_set(opt->smb_index, value);
13275331Samw 				/*
13285331Samw 				 * Specialized refresh mechanisms can
13295331Samw 				 * be flagged in the proto_options and
13305331Samw 				 * processed here.
13315331Samw 				 */
13325521Sas200622 				if (opt->refresh & SMB_REFRESH_REFRESH)
13336139Sjb150015 					(void) smf_refresh_instance(
13346139Sjb150015 					    SMBD_DEFAULT_INSTANCE_FMRI);
13355521Sas200622 				else if (opt->refresh & SMB_REFRESH_RESTART)
13365331Samw 					(void) smf_restart_instance(
13375331Samw 					    SMBD_DEFAULT_INSTANCE_FMRI);
13385331Samw 			}
13395331Samw 		}
13405331Samw 	}
13415521Sas200622 
13425331Samw 	if (name != NULL)
13435331Samw 		sa_free_attr_string(name);
13445331Samw 	if (value != NULL)
13455331Samw 		sa_free_attr_string(value);
13465331Samw 
13475331Samw 	return (ret);
13485331Samw }
13495331Samw 
13505331Samw /*
13515331Samw  * smb_get_status()
13525331Samw  *
13535331Samw  * What is the current status of the smbd? We use the SMF state here.
13545331Samw  * Caller must free the returned value.
13555331Samw  */
13565331Samw 
13575331Samw static char *
13585331Samw smb_get_status(void)
13595331Samw {
13605331Samw 	char *state = NULL;
13615331Samw 	state = smf_get_state(SMBD_DEFAULT_INSTANCE_FMRI);
13625331Samw 	return (state != NULL ? state : "-");
13635331Samw }
13645331Samw 
13655331Samw /*
13665331Samw  * This protocol plugin require resource names
13675331Samw  */
13685331Samw static uint64_t
13695331Samw smb_share_features(void)
13705331Samw {
13715331Samw 	return (SA_FEATURE_RESOURCE | SA_FEATURE_ALLOWSUBDIRS |
13726088Sdougm 	    SA_FEATURE_ALLOWPARDIRS | SA_FEATURE_SERVER);
13735331Samw }
13745331Samw 
13755331Samw /*
13765331Samw  * This should be used to convert lmshare_info to sa_resource_t
13775331Samw  * Should only be needed to build temp shares/resources to be
13785331Samw  * supplied to sharemanager to display temp shares.
13795331Samw  */
13805331Samw static int
1381*7052Samw smb_build_tmp_sa_resource(sa_handle_t handle, smb_share_t *si)
13825331Samw {
13835331Samw 	int err;
13845331Samw 	sa_share_t share;
13855331Samw 	sa_group_t group;
13865331Samw 	sa_resource_t resource;
13875331Samw 
13885331Samw 	if (si == NULL)
13895331Samw 		return (SA_INVALID_NAME);
13905331Samw 
13915331Samw 	/*
13925331Samw 	 * First determine if the "share path" is already shared
13935331Samw 	 * somewhere. If it is, we have to use it as the authority on
13945331Samw 	 * where the transient share lives so will use it's parent
13955331Samw 	 * group. If it doesn't exist, it needs to land in "smb".
13965331Samw 	 */
13975331Samw 
1398*7052Samw 	share = sa_find_share(handle, si->shr_path);
13995331Samw 	if (share != NULL) {
14005331Samw 		group = sa_get_parent_group(share);
14015331Samw 	} else {
14025331Samw 		group = smb_get_smb_share_group(handle);
14035331Samw 		if (group == NULL)
14045331Samw 			return (SA_NO_SUCH_GROUP);
1405*7052Samw 		share = sa_get_share(group, si->shr_path);
14065331Samw 		if (share == NULL) {
1407*7052Samw 			share = sa_add_share(group, si->shr_path,
14085331Samw 			    SA_SHARE_TRANSIENT, &err);
14095331Samw 			if (share == NULL)
14105331Samw 				return (SA_NO_SUCH_PATH);
14115331Samw 		}
14125331Samw 	}
14135331Samw 
14145331Samw 	/*
14155331Samw 	 * Now handle the resource. Make sure that the resource is
14165331Samw 	 * transient and added to the share.
14175331Samw 	 */
1418*7052Samw 	resource = sa_get_share_resource(share, si->shr_name);
14195331Samw 	if (resource == NULL) {
14205331Samw 		resource = sa_add_resource(share,
1421*7052Samw 		    si->shr_name, SA_SHARE_TRANSIENT, &err);
14225331Samw 		if (resource == NULL)
14235331Samw 			return (SA_NO_SUCH_RESOURCE);
14245331Samw 	}
14255331Samw 
14265331Samw 	/* set resource attributes now */
1427*7052Samw 	(void) sa_set_resource_attr(resource, "description", si->shr_cmnt);
1428*7052Samw 	(void) sa_set_resource_attr(resource, SMB_SHROPT_AD_CONTAINER,
1429*7052Samw 	    si->shr_container);
14305331Samw 
14315331Samw 	return (SA_OK);
14325331Samw }
14335331Samw 
14345331Samw /*
14355331Samw  * Return smb transient shares.  Note that we really want to look at
14365331Samw  * all current shares from SMB in order to determine this. Transient
14375331Samw  * shares should be those that don't appear in either the SMF or ZFS
14385331Samw  * configurations.  Those that are in the repositories will be
14395331Samw  * filtered out by smb_build_tmp_sa_resource.
14405331Samw  */
14415331Samw static int
14425331Samw smb_list_transient(sa_handle_t handle)
14435331Samw {
14445331Samw 	int i, offset, num;
1445*7052Samw 	smb_shrlist_t list;
14465331Samw 	int res;
14475331Samw 
1448*7052Samw 	num = smb_share_count();
14495331Samw 	if (num <= 0)
14505331Samw 		return (SA_OK);
14515331Samw 	offset = 0;
1452*7052Samw 	while (smb_share_list(offset, &list) != NERR_InternalError) {
14535331Samw 		if (list.no == 0)
14545331Samw 			break;
14555331Samw 		for (i = 0; i < list.no; i++) {
14565331Samw 			res = smb_build_tmp_sa_resource(handle,
14575331Samw 			    &(list.smbshr[i]));
14585331Samw 			if (res != SA_OK)
14595331Samw 				return (res);
14605331Samw 		}
14615331Samw 		offset += list.no;
14625331Samw 	}
14635331Samw 
14645331Samw 	return (SA_OK);
14655331Samw }
14665331Samw 
14675331Samw /*
14685331Samw  * fix_resource_name(share, name,  prefix)
14695331Samw  *
14705331Samw  * Construct a name where the ZFS dataset has the prefix replaced with "name".
14715331Samw  */
14725331Samw static char *
14735331Samw fix_resource_name(sa_share_t share, char *name, char *prefix)
14745331Samw {
14755331Samw 	char *dataset = NULL;
14765331Samw 	char *newname = NULL;
14775331Samw 	size_t psize;
14785331Samw 	size_t nsize;
14795331Samw 
14805331Samw 	dataset = sa_get_share_attr(share, "dataset");
14815331Samw 
14825331Samw 	if (dataset != NULL && strcmp(dataset, prefix) != 0) {
14835331Samw 		psize = strlen(prefix);
14845331Samw 		if (strncmp(dataset, prefix, psize) == 0) {
14855331Samw 			/* need string plus ',' and NULL */
14865331Samw 			nsize = (strlen(dataset) - psize) + strlen(name) + 2;
14875331Samw 			newname = calloc(nsize, 1);
14885331Samw 			if (newname != NULL) {
14895331Samw 				(void) snprintf(newname, nsize, "%s%s", name,
14905331Samw 				    dataset + psize);
14915331Samw 				sa_fix_resource_name(newname);
14925331Samw 			}
14935331Samw 			sa_free_attr_string(dataset);
14945331Samw 			return (newname);
14955331Samw 		}
14965331Samw 	}
14975331Samw 	if (dataset != NULL)
14985331Samw 		sa_free_attr_string(dataset);
14995331Samw 	return (strdup(name));
15005331Samw }
15015331Samw 
15025331Samw /*
15035331Samw  * smb_parse_optstring(group, options)
15045331Samw  *
15055331Samw  * parse a compact option string into individual options. This allows
15065331Samw  * ZFS sharesmb and sharemgr "share" command to work.  group can be a
15075331Samw  * group, a share or a resource.
15085331Samw  */
15095331Samw static int
15105331Samw smb_parse_optstring(sa_group_t group, char *options)
15115331Samw {
15125331Samw 	char *dup;
15135331Samw 	char *base;
15145331Samw 	char *lasts;
15155331Samw 	char *token;
15165331Samw 	sa_optionset_t optionset;
15175331Samw 	sa_group_t parent = NULL;
15185331Samw 	sa_resource_t resource = NULL;
15195331Samw 	int iszfs = 0;
15205331Samw 	int persist = 0;
15215331Samw 	int need_optionset = 0;
15225331Samw 	int ret = SA_OK;
15235331Samw 	sa_property_t prop;
15245331Samw 
15255331Samw 	/*
15265331Samw 	 * In order to not attempt to change ZFS properties unless
15275331Samw 	 * absolutely necessary, we never do it in the legacy parsing
15285331Samw 	 * so we need to keep track of this.
15295331Samw 	 */
15305331Samw 	if (sa_is_share(group)) {
15315331Samw 		char *zfs;
15325331Samw 
15335331Samw 		parent = sa_get_parent_group(group);
15345331Samw 		if (parent != NULL) {
15355331Samw 			zfs = sa_get_group_attr(parent, "zfs");
15365331Samw 			if (zfs != NULL) {
15375331Samw 				sa_free_attr_string(zfs);
15385331Samw 				iszfs = 1;
15395331Samw 			}
15405331Samw 		}
15415331Samw 	} else {
15425331Samw 		iszfs = sa_group_is_zfs(group);
15435331Samw 		/*
15445331Samw 		 * If a ZFS group, then we need to see if a resource
15455331Samw 		 * name is being set. If so, bail with
15465331Samw 		 * SA_PROP_SHARE_ONLY, so we come back in with a share
15475331Samw 		 * instead of a group.
15485331Samw 		 */
15495331Samw 		if (strncmp(options, "name=", sizeof ("name=") - 1) == 0 ||
15505331Samw 		    strstr(options, ",name=") != NULL) {
15515331Samw 			return (SA_PROP_SHARE_ONLY);
15525331Samw 		}
15535331Samw 	}
15545331Samw 
15555331Samw 	/* do we have an existing optionset? */
15565331Samw 	optionset = sa_get_optionset(group, "smb");
15575331Samw 	if (optionset == NULL) {
15585331Samw 		/* didn't find existing optionset so create one */
15595331Samw 		optionset = sa_create_optionset(group, "smb");
15605331Samw 		if (optionset == NULL)
15615331Samw 			return (SA_NO_MEMORY);
15625331Samw 	} else {
15635331Samw 		/*
15645331Samw 		 * If an optionset already exists, we've come through
15655331Samw 		 * twice so ignore the second time.
15665331Samw 		 */
15675331Samw 		return (ret);
15685331Samw 	}
15695331Samw 
15705331Samw 	/* We need a copy of options for the next part. */
15715331Samw 	dup = strdup(options);
15725331Samw 	if (dup == NULL)
15735331Samw 		return (SA_NO_MEMORY);
15745331Samw 
15755331Samw 	/*
15765331Samw 	 * SMB properties are straightforward and are strings,
15775331Samw 	 * integers or booleans.  Properties are separated by
15785331Samw 	 * commas. It will be necessary to parse quotes due to some
15795331Samw 	 * strings not having a restricted characters set.
15805331Samw 	 *
15815331Samw 	 * Note that names will create a resource. For now, if there
15825331Samw 	 * is a set of properties "before" the first name="", those
15835331Samw 	 * properties will be placed on the group.
15845331Samw 	 */
15855331Samw 	persist = sa_is_persistent(group);
15865331Samw 	base = dup;
15875331Samw 	token = dup;
15885331Samw 	lasts = NULL;
15895331Samw 	while (token != NULL && ret == SA_OK) {
15905331Samw 		ret = SA_OK;
15915331Samw 		token = strtok_r(base, ",", &lasts);
15925331Samw 		base = NULL;
15935331Samw 		if (token != NULL) {
15945331Samw 			char *value;
15955331Samw 			/*
15965331Samw 			 * All SMB properties have values so there
15975331Samw 			 * MUST be an '=' character.  If it doesn't,
15985331Samw 			 * it is a syntax error.
15995331Samw 			 */
16005331Samw 			value = strchr(token, '=');
16015331Samw 			if (value != NULL) {
16025331Samw 				*value++ = '\0';
16035331Samw 			} else {
16045331Samw 				ret = SA_SYNTAX_ERR;
16055331Samw 				break;
16065331Samw 			}
16075331Samw 			/*
16085331Samw 			 * We may need to handle a "name" property
16095331Samw 			 * that is a ZFS imposed resource name. Each
16105331Samw 			 * name would trigger getting a new "resource"
16115331Samw 			 * to put properties on. For now, assume no
16125331Samw 			 * "name" property for special handling.
16135331Samw 			 */
16145331Samw 
16155331Samw 			if (strcmp(token, "name") == 0) {
16165331Samw 				char *prefix;
16175331Samw 				char *name = NULL;
16185331Samw 				/*
16195331Samw 				 * We have a name, so now work on the
16205331Samw 				 * resource level. We have a "share"
16215331Samw 				 * in "group" due to the caller having
16225331Samw 				 * added it. If we are called with a
16235331Samw 				 * group, the check for group/share
16245331Samw 				 * at the beginning of this function
16255331Samw 				 * will bail out the parse if there is a
16265331Samw 				 * "name" but no share.
16275331Samw 				 */
16285331Samw 				if (!iszfs) {
16295331Samw 					ret = SA_SYNTAX_ERR;
16305331Samw 					break;
16315331Samw 				}
16325331Samw 				/*
16335331Samw 				 * Make sure the parent group has the
16345331Samw 				 * "prefix" property since we will
16355331Samw 				 * need to use this for constructing
16365331Samw 				 * inherited name= values.
16375331Samw 				 */
16385331Samw 				prefix = sa_get_group_attr(parent, "prefix");
16395331Samw 				if (prefix == NULL) {
16405331Samw 					prefix = sa_get_group_attr(parent,
16415331Samw 					    "name");
16425331Samw 					if (prefix != NULL) {
16435331Samw 						(void) sa_set_group_attr(parent,
16445331Samw 						    "prefix", prefix);
16455331Samw 					}
16465331Samw 				}
16475331Samw 				name = fix_resource_name((sa_share_t)group,
16485331Samw 				    value, prefix);
16495331Samw 				if (name != NULL) {
16505331Samw 					resource = sa_add_resource(
16515331Samw 					    (sa_share_t)group, name,
16525331Samw 					    SA_SHARE_TRANSIENT, &ret);
16535331Samw 					sa_free_attr_string(name);
16545331Samw 				} else {
16555331Samw 					ret = SA_NO_MEMORY;
16565331Samw 				}
16575331Samw 				if (prefix != NULL)
16585331Samw 					sa_free_attr_string(prefix);
16595331Samw 
16605331Samw 				/* A resource level optionset is needed */
16615331Samw 
16625331Samw 				need_optionset = 1;
16635331Samw 				if (resource == NULL) {
16645331Samw 					ret = SA_NO_MEMORY;
16655331Samw 					break;
16665331Samw 				}
16675331Samw 				continue;
16685331Samw 			}
16695331Samw 
16705331Samw 			if (need_optionset) {
16715331Samw 				optionset = sa_create_optionset(resource,
16725331Samw 				    "smb");
16735331Samw 				need_optionset = 0;
16745331Samw 			}
16755331Samw 
16765331Samw 			prop = sa_create_property(token, value);
16775331Samw 			if (prop == NULL)
16785331Samw 				ret = SA_NO_MEMORY;
16795331Samw 			else
16805331Samw 				ret = sa_add_property(optionset, prop);
16815331Samw 			if (ret != SA_OK)
16825331Samw 				break;
16835331Samw 			if (!iszfs)
16845331Samw 				ret = sa_commit_properties(optionset, !persist);
16855331Samw 		}
16865331Samw 	}
16875331Samw 	free(dup);
16885331Samw 	return (ret);
16895331Samw }
16905331Samw 
16915331Samw /*
16925331Samw  * smb_sprint_option(rbuff, rbuffsize, incr, prop, sep)
16935331Samw  *
16945331Samw  * provides a mechanism to format SMB properties into legacy output
16955331Samw  * format. If the buffer would overflow, it is reallocated and grown
16965331Samw  * as appropriate. Special cases of converting internal form of values
16975331Samw  * to those used by "share" are done. this function does one property
16985331Samw  * at a time.
16995331Samw  */
17005331Samw 
17015331Samw static void
17025331Samw smb_sprint_option(char **rbuff, size_t *rbuffsize, size_t incr,
17035331Samw 			sa_property_t prop, int sep)
17045331Samw {
17055331Samw 	char *name;
17065331Samw 	char *value;
17075331Samw 	int curlen;
17085331Samw 	char *buff = *rbuff;
17095331Samw 	size_t buffsize = *rbuffsize;
17105331Samw 
17115331Samw 	name = sa_get_property_attr(prop, "type");
17125331Samw 	value = sa_get_property_attr(prop, "value");
17135331Samw 	if (buff != NULL)
17145331Samw 		curlen = strlen(buff);
17155331Samw 	else
17165331Samw 		curlen = 0;
17175331Samw 	if (name != NULL) {
17185331Samw 		int len;
17195331Samw 		len = strlen(name) + sep;
17205331Samw 
17215331Samw 		/*
17225331Samw 		 * A future RFE would be to replace this with more
17235331Samw 		 * generic code and to possibly handle more types.
17245331Samw 		 *
17255331Samw 		 * For now, everything else is treated as a string. If
17265331Samw 		 * we get any properties that aren't exactly
17275331Samw 		 * name/value pairs, we may need to
17285331Samw 		 * interpret/transform.
17295331Samw 		 */
17305331Samw 		if (value != NULL)
17315331Samw 			len += 1 + strlen(value);
17325331Samw 
17335331Samw 		while (buffsize <= (curlen + len)) {
17345331Samw 			/* need more room */
17355331Samw 			buffsize += incr;
17365331Samw 			buff = realloc(buff, buffsize);
17375331Samw 			*rbuff = buff;
17385331Samw 			*rbuffsize = buffsize;
17395331Samw 			if (buff == NULL) {
17405331Samw 				/* realloc failed so free everything */
17415331Samw 				if (*rbuff != NULL)
17425331Samw 					free(*rbuff);
17435331Samw 				goto err;
17445331Samw 			}
17455331Samw 		}
17465331Samw 		if (buff == NULL)
17475331Samw 			goto err;
17485331Samw 		(void) snprintf(buff + curlen, buffsize - curlen,
17495331Samw 		    "%s%s=%s", sep ? "," : "",
17505331Samw 		    name, value != NULL ? value : "\"\"");
17515331Samw 
17525331Samw 	}
17535331Samw err:
17545331Samw 	if (name != NULL)
17555331Samw 		sa_free_attr_string(name);
17565331Samw 	if (value != NULL)
17575331Samw 		sa_free_attr_string(value);
17585331Samw }
17595331Samw 
17605331Samw /*
17615331Samw  * smb_format_resource_options(resource, hier)
17625331Samw  *
17635331Samw  * format all the options on the group into a flattened option
17645331Samw  * string. If hier is non-zero, walk up the tree to get inherited
17655331Samw  * options.
17665331Samw  */
17675331Samw 
17685331Samw static char *
17695331Samw smb_format_options(sa_group_t group, int hier)
17705331Samw {
17715331Samw 	sa_optionset_t options = NULL;
17725331Samw 	sa_property_t prop;
17735331Samw 	int sep = 0;
17745331Samw 	char *buff;
17755331Samw 	size_t buffsize;
17765331Samw 
17775331Samw 
17785331Samw 	buff = malloc(OPT_CHUNK);
17795331Samw 	if (buff == NULL)
17805331Samw 		return (NULL);
17815331Samw 
17825331Samw 	buff[0] = '\0';
17835331Samw 	buffsize = OPT_CHUNK;
17845331Samw 
17855331Samw 	/*
17865331Samw 	 * We may have a an optionset relative to this item. format
17875331Samw 	 * these if we find them and then add any security definitions.
17885331Samw 	 */
17895331Samw 
17905331Samw 	options = sa_get_derived_optionset(group, "smb", hier);
17915331Samw 
17925331Samw 	/*
17935331Samw 	 * do the default set first but skip any option that is also
17945331Samw 	 * in the protocol specific optionset.
17955331Samw 	 */
17965331Samw 	if (options != NULL) {
17975331Samw 		for (prop = sa_get_property(options, NULL);
17985331Samw 		    prop != NULL; prop = sa_get_next_property(prop)) {
17995331Samw 			/*
18005331Samw 			 * use this one since we skipped any
18015331Samw 			 * of these that were also in
18025331Samw 			 * optdefault
18035331Samw 			 */
18045331Samw 			smb_sprint_option(&buff, &buffsize, OPT_CHUNK,
18055331Samw 			    prop, sep);
18065331Samw 			if (buff == NULL) {
18075331Samw 				/*
18085331Samw 				 * buff could become NULL if there
18095331Samw 				 * isn't enough memory for
18105331Samw 				 * smb_sprint_option to realloc()
18115331Samw 				 * as necessary. We can't really
18125331Samw 				 * do anything about it at this
18135331Samw 				 * point so we return NULL.  The
18145331Samw 				 * caller should handle the
18155331Samw 				 * failure.
18165331Samw 				 */
18175331Samw 				if (options != NULL)
18185331Samw 					sa_free_derived_optionset(
18195331Samw 					    options);
18205331Samw 				return (buff);
18215331Samw 			}
18225331Samw 			sep = 1;
18235331Samw 		}
18245331Samw 	}
18255331Samw 
18265331Samw 	if (options != NULL)
18275331Samw 		sa_free_derived_optionset(options);
18285331Samw 	return (buff);
18295331Samw }
18305331Samw 
18315331Samw /*
18325331Samw  * smb_rename_resource(resource, newname)
18335331Samw  *
18345331Samw  * Change the current exported name of the resource to newname.
18355331Samw  */
18365331Samw /*ARGSUSED*/
18375331Samw int
18385331Samw smb_rename_resource(sa_handle_t handle, sa_resource_t resource, char *newname)
18395331Samw {
18405331Samw 	int ret = SA_OK;
18415331Samw 	int err;
18425331Samw 	char *oldname;
18435331Samw 
18445331Samw 	oldname = sa_get_resource_attr(resource, "name");
18455331Samw 	if (oldname == NULL)
18465331Samw 		return (SA_NO_SUCH_RESOURCE);
18475331Samw 
1848*7052Samw 	err = smb_share_ren(oldname, newname);
18495331Samw 
18505331Samw 	/* improve error values somewhat */
18515331Samw 	switch (err) {
18525331Samw 	case NERR_Success:
18535331Samw 		break;
18545331Samw 	case NERR_InternalError:
18555331Samw 		ret = SA_SYSTEM_ERR;
18565331Samw 		break;
18575331Samw 	case NERR_DuplicateShare:
18585331Samw 		ret = SA_DUPLICATE_NAME;
18595331Samw 		break;
18605331Samw 	default:
18615331Samw 		ret = SA_CONFIG_ERR;
18625331Samw 		break;
18635331Samw 	}
18645331Samw 
18655331Samw 	return (ret);
18665331Samw }
1867