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 *);
656214Sdougm static int smb_validate_property(sa_handle_t, 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 
333*6270Sdougm 	/*
334*6270Sdougm 	 * We only start in the global zone and only run if we aren't
335*6270Sdougm 	 * running Trusted Extensions.
336*6270Sdougm 	 */
337*6270Sdougm 	if (getzoneid() != GLOBAL_ZONEID) {
338*6270Sdougm 		(void) printf(dgettext(TEXT_DOMAIN,
339*6270Sdougm 		    "SMB: service not supported in local zone\n"));
340*6270Sdougm 		return (SA_NOT_SUPPORTED);
341*6270Sdougm 	}
342*6270Sdougm 	if (is_system_labeled()) {
343*6270Sdougm 		(void) printf(dgettext(TEXT_DOMAIN,
344*6270Sdougm 		    "SMB: service not supported with Trusted Extensions\n"));
345*6270Sdougm 		return (SA_NOT_SUPPORTED);
346*6270Sdougm 	}
347*6270Sdougm 
3485331Samw 	priv_effective = priv_allocset();
3495331Samw 	(void) getppriv(PRIV_EFFECTIVE, priv_effective);
3505331Samw 	privileged = (priv_isfullset(priv_effective) == B_TRUE);
3515331Samw 	priv_freeset(priv_effective);
3525331Samw 
3535331Samw 	/* get the path since it is important in several places */
3545331Samw 	path = sa_get_share_attr(share, "path");
3555331Samw 	if (path == NULL)
3565331Samw 		return (SA_NO_SUCH_PATH);
3575331Samw 
3585772Sas200622 	/*
3595772Sas200622 	 * If administratively disabled, don't try to start anything.
3605772Sas200622 	 */
3615331Samw 	online = smb_isonline();
3625772Sas200622 	if (!online && !smb_isautoenable() && smb_isdisabled())
3635772Sas200622 		goto done;
3645331Samw 
3655331Samw 	iszfs = sa_path_is_zfs(path);
3665331Samw 
3675331Samw 	if (iszfs) {
3685331Samw 
3695331Samw 		if (privileged == B_FALSE && !online) {
3705331Samw 
3715331Samw 			if (!online) {
3725331Samw 				(void) printf(dgettext(TEXT_DOMAIN,
3735331Samw 				    "SMB: Cannot share remove "
3745331Samw 				    "file system: %s\n"), path);
3755331Samw 				(void) printf(dgettext(TEXT_DOMAIN,
3765331Samw 				    "SMB: Service needs to be enabled "
3775331Samw 				    "by a privileged user\n"));
3785331Samw 				err = SA_NO_PERMISSION;
3795331Samw 				errno = EPERM;
3805331Samw 			}
3815331Samw 			if (err) {
3825331Samw 				sa_free_attr_string(path);
3835331Samw 				return (err);
3845331Samw 			}
3855331Samw 
3865331Samw 		}
3875331Samw 	}
3885331Samw 
3895331Samw 	if (privileged == B_TRUE && !online) {
3905331Samw 		err = smb_enable_service();
3915331Samw 		if (err != SA_OK) {
3925331Samw 			(void) printf(dgettext(TEXT_DOMAIN,
3935331Samw 			    "SMB: Unable to enable service\n"));
3945331Samw 			/*
3955331Samw 			 * For now, it is OK to not be able to enable
3965331Samw 			 * the service.
3975331Samw 			 */
3986030Sjb150015 			if (err == SA_BUSY || err == SA_SYSTEM_ERR)
3995331Samw 				err = SA_OK;
4005331Samw 		} else {
4015331Samw 			online = B_TRUE;
4025331Samw 		}
4035331Samw 	}
4045331Samw 
4055331Samw 	/*
4065331Samw 	 * Don't bother trying to start shares if the service isn't
4075331Samw 	 * running.
4085331Samw 	 */
4095331Samw 	if (!online)
4105331Samw 		goto done;
4115331Samw 
4125331Samw 	/* Each share can have multiple resources */
4135331Samw 	for (resource = sa_get_share_resource(share, NULL);
4145331Samw 	    resource != NULL;
4155331Samw 	    resource = sa_get_next_resource(resource)) {
4165331Samw 		sa_optionset_t opts;
4175331Samw 		bzero(&si, sizeof (lmshare_info_t));
4185331Samw 		rname = sa_get_resource_attr(resource, "name");
4195331Samw 		if (rname == NULL) {
4205331Samw 			sa_free_attr_string(path);
4215331Samw 			return (SA_NO_SUCH_RESOURCE);
4225331Samw 		}
4235331Samw 
4245331Samw 		opts = sa_get_derived_optionset(resource, SMB_PROTOCOL_NAME, 1);
4255331Samw 		smb_build_lmshare_info(rname, path, opts, &si);
4265331Samw 		sa_free_attr_string(rname);
4275331Samw 
4285331Samw 		sa_free_derived_optionset(opts);
4295331Samw 		if (!iszfs) {
4305331Samw 			err = lmshrd_add(&si);
4315331Samw 		} else {
4325331Samw 			share_t sh;
4335331Samw 
4345331Samw 			sa_sharetab_fill_zfs(share, &sh, "smb");
4355331Samw 			err = sa_share_zfs(share, (char *)path, &sh,
4365331Samw 			    &si, ZFS_SHARE_SMB);
4375331Samw 
4385331Samw 			sa_emptyshare(&sh);
4395331Samw 		}
4405331Samw 	}
4415331Samw 	if (!iszfs)
4425331Samw 		(void) sa_update_sharetab(share, "smb");
4435331Samw done:
4445331Samw 	sa_free_attr_string(path);
4455331Samw 
4465331Samw 	return (err == NERR_DuplicateShare ? 0 : err);
4475331Samw }
4485331Samw 
4495331Samw /*
4505331Samw  * This is the share for CIFS all shares have resource names.
4515331Samw  * Enable tells the smb server to update its hash. If it fails
4525331Samw  * because smb server is down, we just ignore as smb server loads
4535331Samw  * the resources from sharemanager at startup.
4545331Samw  */
4555331Samw 
4565331Samw static int
4575331Samw smb_enable_resource(sa_resource_t resource)
4585331Samw {
4595331Samw 	char *path;
4605331Samw 	char *rname;
4615331Samw 	sa_optionset_t opts;
4625331Samw 	sa_share_t share;
4635331Samw 	lmshare_info_t si;
4645772Sas200622 	int ret = SA_OK;
4655772Sas200622 	int err;
4665772Sas200622 	boolean_t isonline;
4675331Samw 
4685331Samw 	share = sa_get_resource_parent(resource);
4695331Samw 	if (share == NULL)
4705331Samw 		return (SA_NO_SUCH_PATH);
4715772Sas200622 
4725772Sas200622 	/*
4735772Sas200622 	 * If administratively disabled, don't try to start anything.
4745772Sas200622 	 */
4755772Sas200622 	isonline = smb_isonline();
4765772Sas200622 	if (!isonline && !smb_isautoenable() && smb_isdisabled())
4775772Sas200622 		goto done;
4785772Sas200622 
4795772Sas200622 	if (!isonline)
4805772Sas200622 		ret = smb_enable_service();
4815772Sas200622 	if (!smb_isonline()) {
4825772Sas200622 		ret = SA_OK;
4835772Sas200622 		goto done;
4845772Sas200622 	}
4855772Sas200622 
4865331Samw 	path = sa_get_share_attr(share, "path");
4875331Samw 	if (path == NULL)
4885331Samw 		return (SA_SYSTEM_ERR);
4895331Samw 	rname = sa_get_resource_attr(resource, "name");
4905331Samw 	if (rname == NULL) {
4915331Samw 		sa_free_attr_string(path);
4925331Samw 		return (SA_NO_SUCH_RESOURCE);
4935331Samw 	}
4945331Samw 
4955331Samw 	opts = sa_get_derived_optionset(resource, SMB_PROTOCOL_NAME, 1);
4965331Samw 	smb_build_lmshare_info(rname, path, opts, &si);
4975331Samw 	sa_free_attr_string(path);
4985331Samw 	sa_free_attr_string(rname);
4995331Samw 	sa_free_derived_optionset(opts);
5005772Sas200622 
5015772Sas200622 	/*
5025772Sas200622 	 * Attempt to add the share. Any error that occurs if it was
5035772Sas200622 	 * online is an error but don't count NERR_DuplicateName if
5045772Sas200622 	 * smb/server had to be brought online since bringing the
5055772Sas200622 	 * service up will enable the share that was just added prior
5065772Sas200622 	 * to the attempt to enable.
5075772Sas200622 	 */
5085772Sas200622 
5095772Sas200622 	err = lmshrd_add(&si);
5105772Sas200622 	if (err == NERR_Success || !(!isonline && err == NERR_DuplicateName))
5115772Sas200622 		(void) sa_update_sharetab(share, "smb");
5125772Sas200622 	else
5135331Samw 		return (SA_NOT_SHARED);
5145331Samw 
5155331Samw done:
5165331Samw 	return (ret);
5175331Samw }
5185331Samw 
5195331Samw /*
5205331Samw  * Remove it from smb server hash.
5215331Samw  */
5225331Samw static int
5235331Samw smb_disable_resource(sa_resource_t resource)
5245331Samw {
5255331Samw 	char *rname;
5265331Samw 	DWORD res;
5275331Samw 	sa_share_t share;
5285331Samw 
5295331Samw 	rname = sa_get_resource_attr(resource, "name");
5305331Samw 	if (rname == NULL)
5315331Samw 		return (SA_NO_SUCH_RESOURCE);
5325331Samw 
5335331Samw 	if (smb_isonline()) {
5345331Samw 		res = lmshrd_delete(rname);
5355331Samw 		if (res != NERR_Success) {
5365331Samw 			sa_free_attr_string(rname);
5375331Samw 			return (SA_CONFIG_ERR);
5385331Samw 		}
5395331Samw 	}
5405951Sdougm 
5415951Sdougm 	sa_free_attr_string(rname);
5425951Sdougm 
5435331Samw 	share = sa_get_resource_parent(resource);
5445331Samw 	if (share != NULL) {
5455331Samw 		rname = sa_get_share_attr(share, "path");
5465331Samw 		if (rname != NULL) {
5475951Sdougm 			sa_handle_t handle;
5485951Sdougm 
5495951Sdougm 			handle = sa_find_group_handle((sa_group_t)resource);
5505951Sdougm 			(void) sa_delete_sharetab(handle, rname, "smb");
5515331Samw 			sa_free_attr_string(rname);
5525331Samw 		}
5535331Samw 	}
5545331Samw 	/*
5555331Samw 	 * Always return OK as smb/server may be down and
5565331Samw 	 * Shares will be picked up when loaded.
5575331Samw 	 */
5585331Samw 	return (SA_OK);
5595331Samw }
5605331Samw 
5615331Samw /*
5625331Samw  * smb_share_changed(sa_share_t share)
5635331Samw  *
5645331Samw  * The specified share has changed.
5655331Samw  */
5665331Samw static int
5675331Samw smb_share_changed(sa_share_t share)
5685331Samw {
5695331Samw 	char *path;
5705331Samw 	sa_resource_t resource;
5715331Samw 
5725331Samw 	/* get the path since it is important in several places */
5735331Samw 	path = sa_get_share_attr(share, "path");
5745331Samw 	if (path == NULL)
5755331Samw 		return (SA_NO_SUCH_PATH);
5765331Samw 	for (resource = sa_get_share_resource(share, NULL);
5775331Samw 	    resource != NULL;
5785331Samw 	    resource = sa_get_next_resource(resource))
5795331Samw 		(void) smb_resource_changed(resource);
5805331Samw 
5815331Samw 	sa_free_attr_string(path);
5825331Samw 
5835331Samw 	return (SA_OK);
5845331Samw }
5855331Samw 
5865331Samw /*
5875331Samw  * smb_resource_changed(sa_resource_t resource)
5885331Samw  *
5895331Samw  * The specified resource has changed.
5905331Samw  */
5915331Samw static int
5925331Samw smb_resource_changed(sa_resource_t resource)
5935331Samw {
5945331Samw 	DWORD res;
5955331Samw 	lmshare_info_t si;
5965331Samw 	lmshare_info_t new_si;
5975331Samw 	char *rname, *path;
5985331Samw 	sa_optionset_t opts;
5995331Samw 	sa_share_t share;
6005331Samw 
6015331Samw 	rname = sa_get_resource_attr(resource, "name");
6025331Samw 	if (rname == NULL)
6035331Samw 		return (SA_NO_SUCH_RESOURCE);
6045331Samw 
6055331Samw 	share = sa_get_resource_parent(resource);
6065331Samw 	if (share == NULL) {
6075331Samw 		sa_free_attr_string(rname);
6085331Samw 		return (SA_CONFIG_ERR);
6095331Samw 	}
6105331Samw 
6115331Samw 	path = sa_get_share_attr(share, "path");
6125331Samw 	if (path == NULL) {
6135331Samw 		sa_free_attr_string(rname);
6145331Samw 		return (SA_NO_SUCH_PATH);
6155331Samw 	}
6165331Samw 
6175331Samw 	if (!smb_isonline()) {
6185331Samw 		sa_free_attr_string(rname);
6195331Samw 		return (SA_OK);
6205331Samw 	}
6215331Samw 
6225331Samw 	/* Update the share cache in smb/server */
6235331Samw 	res = lmshrd_getinfo(rname, &si);
6245331Samw 	if (res != NERR_Success) {
6255331Samw 		sa_free_attr_string(path);
6265331Samw 		sa_free_attr_string(rname);
6275331Samw 		return (SA_CONFIG_ERR);
6285331Samw 	}
6295331Samw 
6305331Samw 	opts = sa_get_derived_optionset(resource, SMB_PROTOCOL_NAME, 1);
6315331Samw 	smb_build_lmshare_info(rname, path, opts, &new_si);
6325331Samw 	sa_free_derived_optionset(opts);
6335331Samw 	sa_free_attr_string(path);
6345331Samw 	sa_free_attr_string(rname);
6355331Samw 
6365331Samw 	/*
6375331Samw 	 * Update all fields from sa_share_t
6385331Samw 	 * Get derived values.
6395331Samw 	 */
6405331Samw 	if (lmshrd_setinfo(&new_si) != LMSHR_DOOR_SRV_SUCCESS)
6415331Samw 		return (SA_CONFIG_ERR);
6425331Samw 	return (smb_enable_service());
6435331Samw }
6445331Samw 
6455331Samw /*
6465800Sdougm  * smb_disable_share(sa_share_t share, char *path)
6475331Samw  *
6485800Sdougm  * Unshare the specified share. Note that "path" is the same
6495800Sdougm  * path as what is in the "share" object. It is passed in to avoid an
6505800Sdougm  * additional lookup. A missing "path" value makes this a no-op
6515800Sdougm  * function.
6525331Samw  */
6535331Samw static int
6545331Samw smb_disable_share(sa_share_t share, char *path)
6555331Samw {
6565331Samw 	char *rname;
6575331Samw 	sa_resource_t resource;
6585800Sdougm 	sa_group_t parent;
6595331Samw 	boolean_t iszfs;
6605331Samw 	int err = SA_OK;
6615951Sdougm 	sa_handle_t handle;
6625331Samw 
6635800Sdougm 	if (path == NULL)
6645800Sdougm 		return (err);
6655800Sdougm 
6665800Sdougm 	/*
6675800Sdougm 	 * If the share is in a ZFS group we need to handle it
6685800Sdougm 	 * differently.  Just being on a ZFS file system isn't
6695800Sdougm 	 * enough since we may be in a legacy share case.
6705800Sdougm 	 */
6715800Sdougm 	parent = sa_get_parent_group(share);
6725800Sdougm 	iszfs = sa_group_is_zfs(parent);
6735800Sdougm 
6745331Samw 	if (!smb_isonline())
6755331Samw 		goto done;
6765331Samw 
6775331Samw 	for (resource = sa_get_share_resource(share, NULL);
6785331Samw 	    resource != NULL;
6795331Samw 	    resource = sa_get_next_resource(resource)) {
6805331Samw 		rname = sa_get_resource_attr(resource, "name");
6815331Samw 		if (rname == NULL) {
6825331Samw 			continue;
6835331Samw 		}
6845331Samw 		if (!iszfs) {
6855331Samw 			err = lmshrd_delete(rname);
6865331Samw 			switch (err) {
6875331Samw 			case NERR_NetNameNotFound:
6885331Samw 			case NERR_Success:
6895331Samw 				err = SA_OK;
6905331Samw 				break;
6915331Samw 			default:
6925331Samw 				err = SA_CONFIG_ERR;
6935331Samw 				break;
6945331Samw 			}
6955331Samw 		} else {
6965331Samw 			share_t sh;
6975331Samw 
6985331Samw 			sa_sharetab_fill_zfs(share, &sh, "smb");
6995331Samw 			err = sa_share_zfs(share, (char *)path, &sh,
7005331Samw 			    rname, ZFS_UNSHARE_SMB);
7015331Samw 			sa_emptyshare(&sh);
7025331Samw 		}
7035331Samw 		sa_free_attr_string(rname);
7045331Samw 	}
7055331Samw done:
7065951Sdougm 	if (!iszfs) {
7075951Sdougm 		handle = sa_find_group_handle((sa_group_t)share);
7085951Sdougm 		if (handle != NULL)
7095951Sdougm 			(void) sa_delete_sharetab(handle, path, "smb");
7105951Sdougm 		else
7115951Sdougm 			err = SA_SYSTEM_ERR;
7125951Sdougm 	}
7135331Samw 	return (err);
7145331Samw }
7155331Samw 
7165331Samw /*
7176214Sdougm  * smb_validate_property(handle, property, parent)
7185331Samw  *
7195331Samw  * Check that the property has a legitimate value for its type.
7206214Sdougm  * Handle isn't currently used but may need to be in the future.
7215331Samw  */
7225331Samw 
7236214Sdougm /*ARGSUSED*/
7245331Samw static int
7256214Sdougm smb_validate_property(sa_handle_t handle, sa_property_t property,
7266214Sdougm     sa_optionset_t parent)
7275331Samw {
7285331Samw 	int ret = SA_OK;
7295331Samw 	char *propname;
7305331Samw 	int optindex;
7315331Samw 	sa_group_t parent_group;
7325331Samw 	char *value;
7335331Samw 
7345331Samw 	propname = sa_get_property_attr(property, "type");
7355331Samw 
7365331Samw 	if ((optindex = findopt(propname)) < 0)
7375331Samw 		ret = SA_NO_SUCH_PROP;
7385331Samw 
7395331Samw 	/* need to validate value range here as well */
7405331Samw 	if (ret == SA_OK) {
7415331Samw 		parent_group = sa_get_parent_group((sa_share_t)parent);
7425331Samw 		if (optdefs[optindex].share && !sa_is_share(parent_group))
7435331Samw 			ret = SA_PROP_SHARE_ONLY;
7445331Samw 	}
7455331Samw 	if (ret != SA_OK) {
7465331Samw 		if (propname != NULL)
7475331Samw 			sa_free_attr_string(propname);
7485331Samw 		return (ret);
7495331Samw 	}
7505331Samw 
7515331Samw 	value = sa_get_property_attr(property, "value");
7525331Samw 	if (value != NULL) {
7535331Samw 		/* first basic type checking */
7545331Samw 		switch (optdefs[optindex].type) {
7555331Samw 		case OPT_TYPE_NUMBER:
7565331Samw 			/* check that the value is all digits */
7575331Samw 			if (!is_a_number(value))
7585331Samw 				ret = SA_BAD_VALUE;
7595331Samw 			break;
7605331Samw 		case OPT_TYPE_BOOLEAN:
7615331Samw 			if (strlen(value) == 0 ||
7625331Samw 			    strcasecmp(value, "true") == 0 ||
7635331Samw 			    strcmp(value, "1") == 0 ||
7645331Samw 			    strcasecmp(value, "false") == 0 ||
7655331Samw 			    strcmp(value, "0") == 0) {
7665331Samw 				ret = SA_OK;
7675331Samw 			} else {
7685331Samw 				ret = SA_BAD_VALUE;
7695331Samw 			}
7705331Samw 			break;
7715331Samw 		case OPT_TYPE_NAME:
7725331Samw 			/*
7735331Samw 			 * Make sure no invalid characters
7745331Samw 			 */
7755331Samw 			if (validresource(value) == B_FALSE)
7765331Samw 				ret = SA_BAD_VALUE;
7775331Samw 			break;
7785331Samw 		case OPT_TYPE_STRING:
7795331Samw 			/* whatever is here should be ok */
7805331Samw 			break;
7815331Samw 		default:
7825331Samw 			break;
7835331Samw 		}
7845331Samw 	}
7855331Samw 
7865331Samw 	if (value != NULL)
7875331Samw 		sa_free_attr_string(value);
7885331Samw 	if (ret == SA_OK && optdefs[optindex].check != NULL)
7895331Samw 		/* do the property specific check */
7905331Samw 		ret = optdefs[optindex].check(property);
7915331Samw 
7925331Samw 	if (propname != NULL)
7935331Samw 		sa_free_attr_string(propname);
7945331Samw 	return (ret);
7955331Samw }
7965331Samw 
7975331Samw /*
7985331Samw  * Protocol management functions
7995331Samw  *
8005331Samw  * properties defined in the default files are defined in
8015331Samw  * proto_option_defs for parsing and validation.
8025331Samw  */
8035331Samw 
8045331Samw struct smb_proto_option_defs {
8055331Samw 	int smb_index;
8065331Samw 	int32_t minval;
8075331Samw 	int32_t maxval; /* In case of length of string this should be max */
8085331Samw 	int (*validator)(int, char *);
8095331Samw 	int32_t	refresh;
8105331Samw } smb_proto_options[] = {
8115772Sas200622 	{ SMB_CI_SYS_CMNT, 0, MAX_VALUE_BUFLEN,
8125772Sas200622 	    string_length_check_validator, SMB_REFRESH_REFRESH },
8135772Sas200622 	{ SMB_CI_MAX_WORKERS, 64, 1024, range_check_validator,
8145772Sas200622 	    SMB_REFRESH_REFRESH },
8155772Sas200622 	{ SMB_CI_NBSCOPE, 0, MAX_VALUE_BUFLEN,
8165772Sas200622 	    string_length_check_validator, 0 },
8175772Sas200622 	{ SMB_CI_LM_LEVEL, 2, 5, range_check_validator, 0 },
8185772Sas200622 	{ SMB_CI_KEEPALIVE, 20, 5400, range_check_validator_zero_ok,
8195772Sas200622 	    SMB_REFRESH_REFRESH },
8205772Sas200622 	{ SMB_CI_WINS_SRV1, 0, MAX_VALUE_BUFLEN,
8215772Sas200622 	    ip_address_validator_empty_ok, SMB_REFRESH_REFRESH },
8225772Sas200622 	{ SMB_CI_WINS_SRV2, 0, MAX_VALUE_BUFLEN,
8235772Sas200622 	    ip_address_validator_empty_ok, SMB_REFRESH_REFRESH },
8245772Sas200622 	{ SMB_CI_WINS_EXCL, 0, MAX_VALUE_BUFLEN,
8255772Sas200622 	    ip_address_csv_list_validator_empty_ok, SMB_REFRESH_REFRESH },
8265772Sas200622 	{ SMB_CI_SIGNING_ENABLE, 0, 0, true_false_validator,
8275772Sas200622 	    SMB_REFRESH_REFRESH },
8285772Sas200622 	{ SMB_CI_SIGNING_REQD, 0, 0, true_false_validator,
8295772Sas200622 	    SMB_REFRESH_REFRESH },
8305772Sas200622 	{ SMB_CI_RESTRICT_ANON, 0, 0, true_false_validator,
8315772Sas200622 	    SMB_REFRESH_REFRESH },
8325772Sas200622 	{ SMB_CI_DOMAIN_SRV, 0, MAX_VALUE_BUFLEN,
8335772Sas200622 	    ip_address_validator_empty_ok, 0 },
8345772Sas200622 	{ SMB_CI_ADS_SITE, 0, MAX_VALUE_BUFLEN,
8355772Sas200622 	    string_length_check_validator, SMB_REFRESH_REFRESH },
8365772Sas200622 	{ SMB_CI_DYNDNS_ENABLE, 0, 0, true_false_validator, 0 },
8375772Sas200622 	{ SMB_CI_AUTOHOME_MAP, 0, MAX_VALUE_BUFLEN, path_validator, 0 },
8385331Samw };
8395331Samw 
8405772Sas200622 #define	SMB_OPT_NUM \
8415772Sas200622 	(sizeof (smb_proto_options) / sizeof (smb_proto_options[0]))
8425772Sas200622 
8435331Samw /*
8445331Samw  * Check the range of value as int range.
8455331Samw  */
8465331Samw static int
8475331Samw range_check_validator(int index, char *value)
8485331Samw {
8495331Samw 	int ret = SA_OK;
8505331Samw 
8515331Samw 	if (!is_a_number(value)) {
8525331Samw 		ret = SA_BAD_VALUE;
8535331Samw 	} else {
8545331Samw 		int val;
8555331Samw 		val = strtoul(value, NULL, 0);
8565331Samw 		if (val < smb_proto_options[index].minval ||
8575331Samw 		    val > smb_proto_options[index].maxval)
8585331Samw 			ret = SA_BAD_VALUE;
8595331Samw 	}
8605331Samw 	return (ret);
8615331Samw }
8625331Samw 
8635331Samw /*
8645331Samw  * Check the range of value as int range.
8655331Samw  */
8665331Samw static int
8675331Samw range_check_validator_zero_ok(int index, char *value)
8685331Samw {
8695331Samw 	int ret = SA_OK;
8705331Samw 
8715331Samw 	if (!is_a_number(value)) {
8725331Samw 		ret = SA_BAD_VALUE;
8735331Samw 	} else {
8745331Samw 		int val;
8755331Samw 		val = strtoul(value, NULL, 0);
8765331Samw 		if (val == 0)
8775331Samw 			ret = SA_OK;
8785331Samw 		else {
8795331Samw 			if (val < smb_proto_options[index].minval ||
8805331Samw 			    val > smb_proto_options[index].maxval)
8815331Samw 			ret = SA_BAD_VALUE;
8825331Samw 		}
8835331Samw 	}
8845331Samw 	return (ret);
8855331Samw }
8865331Samw 
8875331Samw /*
8885331Samw  * Check the length of the string
8895331Samw  */
8905331Samw static int
8915331Samw string_length_check_validator(int index, char *value)
8925331Samw {
8935331Samw 	int ret = SA_OK;
8945331Samw 
8955331Samw 	if (value == NULL)
8965331Samw 		return (SA_BAD_VALUE);
8975331Samw 	if (strlen(value) > smb_proto_options[index].maxval)
8985331Samw 		ret = SA_BAD_VALUE;
8995331Samw 	return (ret);
9005331Samw }
9015331Samw 
9025331Samw /*
9035331Samw  * Check yes/no
9045331Samw  */
9055331Samw /*ARGSUSED*/
9065331Samw static int
9075331Samw true_false_validator(int index, char *value)
9085331Samw {
9095331Samw 	if (value == NULL)
9105331Samw 		return (SA_BAD_VALUE);
9115331Samw 	if ((strcasecmp(value, "true") == 0) ||
9125331Samw 	    (strcasecmp(value, "false") == 0))
9135331Samw 		return (SA_OK);
9145331Samw 	return (SA_BAD_VALUE);
9155331Samw }
9165331Samw 
9175331Samw /*
9185331Samw  * Check IP address.
9195331Samw  */
9205331Samw /*ARGSUSED*/
9215331Samw static int
9225331Samw ip_address_validator_empty_ok(int index, char *value)
9235331Samw {
9245331Samw 	char sbytes[16];
9255331Samw 	int len;
9265331Samw 
9275331Samw 	if (value == NULL)
9285331Samw 		return (SA_OK);
9295331Samw 	len = strlen(value);
9305331Samw 	if (len == 0)
9315331Samw 		return (SA_OK);
9325331Samw 	if (inet_pton(AF_INET, value, (void *)sbytes) != 1)
9335331Samw 		return (SA_BAD_VALUE);
9345331Samw 
9355331Samw 	return (SA_OK);
9365331Samw }
9375331Samw 
9385331Samw /*
9395331Samw  * Check IP address list
9405331Samw  */
9415331Samw /*ARGSUSED*/
9425331Samw static int
9435331Samw ip_address_csv_list_validator_empty_ok(int index, char *value)
9445331Samw {
9455331Samw 	char sbytes[16];
9465331Samw 	char *ip, *tmp, *ctx;
9475331Samw 
9485331Samw 	if (value == NULL || *value == '\0')
9495331Samw 		return (SA_OK);
9505331Samw 
9515331Samw 	if (strlen(value) > MAX_VALUE_BUFLEN)
9525331Samw 		return (SA_BAD_VALUE);
9535331Samw 
9545331Samw 	if ((tmp = strdup(value)) == NULL)
9555331Samw 		return (SA_NO_MEMORY);
9565331Samw 
9575331Samw 	ip = strtok_r(tmp, ",", &ctx);
9585331Samw 	while (ip) {
9595331Samw 		if (strlen(ip) == 0) {
9605331Samw 			free(tmp);
9615331Samw 			return (SA_BAD_VALUE);
9625331Samw 		}
9635331Samw 		if (*ip != 0) {
9645331Samw 			if (inet_pton(AF_INET, ip,
9655331Samw 			    (void *)sbytes) != 1) {
9665331Samw 				free(tmp);
9675331Samw 				return (SA_BAD_VALUE);
9685331Samw 			}
9695331Samw 		}
9705331Samw 		ip = strtok_r(0, ",", &ctx);
9715331Samw 	}
9725331Samw 
9735331Samw 	free(tmp);
9745331Samw 	return (SA_OK);
9755331Samw }
9765331Samw 
9775331Samw /*
9785331Samw  * Check path
9795331Samw  */
9805331Samw /*ARGSUSED*/
9815331Samw static int
9825331Samw path_validator(int index, char *value)
9835331Samw {
9845331Samw 	struct stat buffer;
9855331Samw 	int fd, status;
9865331Samw 
9875331Samw 	if (value == NULL)
9885331Samw 		return (SA_BAD_VALUE);
9895331Samw 
9905331Samw 	fd = open(value, O_RDONLY);
9915331Samw 	if (fd < 0)
9925331Samw 		return (SA_BAD_VALUE);
9935331Samw 
9945331Samw 	status = fstat(fd, &buffer);
9955331Samw 	(void) close(fd);
9965331Samw 
9975331Samw 	if (status < 0)
9985331Samw 		return (SA_BAD_VALUE);
9995331Samw 
10005331Samw 	if (buffer.st_mode & S_IFDIR)
10015331Samw 		return (SA_OK);
10025331Samw 	return (SA_BAD_VALUE);
10035331Samw }
10045331Samw 
10055331Samw /*
10065331Samw  * the protoset holds the defined options so we don't have to read
10075331Samw  * them multiple times
10085331Samw  */
10095331Samw static sa_protocol_properties_t protoset;
10105331Samw 
10115331Samw static int
10125331Samw findprotoopt(char *name)
10135331Samw {
10145331Samw 	int i;
10155772Sas200622 	char *sc_name;
10165772Sas200622 
10175772Sas200622 	for (i = 0; i < SMB_OPT_NUM; i++) {
10185772Sas200622 		sc_name = smb_config_getname(smb_proto_options[i].smb_index);
10195772Sas200622 		if (strcasecmp(sc_name, name) == 0)
10205331Samw 			return (i);
10215331Samw 	}
10225772Sas200622 
10235331Samw 	return (-1);
10245331Samw }
10255331Samw 
10265331Samw /*
10275331Samw  * smb_load_proto_properties()
10285331Samw  *
10295331Samw  * read the smb config values from SMF.
10305331Samw  */
10315331Samw 
10325331Samw static int
10335331Samw smb_load_proto_properties()
10345331Samw {
10355331Samw 	sa_property_t prop;
10365772Sas200622 	char value[MAX_VALUE_BUFLEN];
10375772Sas200622 	char *name;
10385331Samw 	int index;
10396019Sdougm 	int ret = SA_OK;
10405772Sas200622 	int rc;
10415331Samw 
10425331Samw 	protoset = sa_create_protocol_properties(SMB_PROTOCOL_NAME);
10435331Samw 	if (protoset == NULL)
10445331Samw 		return (SA_NO_MEMORY);
10455331Samw 
10466019Sdougm 	for (index = 0; index < SMB_OPT_NUM && ret == SA_OK; index++) {
10475772Sas200622 		rc = smb_config_get(smb_proto_options[index].smb_index,
10485772Sas200622 		    value, sizeof (value));
10495772Sas200622 		if (rc != SMBD_SMF_OK)
10505772Sas200622 			continue;
10515772Sas200622 		name = smb_config_getname(smb_proto_options[index].smb_index);
10525772Sas200622 		prop = sa_create_property(name, value);
10535454Sdougm 		if (prop != NULL)
10546019Sdougm 			ret = sa_add_protocol_property(protoset, prop);
10556019Sdougm 		else
10566019Sdougm 			ret = SA_NO_MEMORY;
10575331Samw 	}
10586019Sdougm 	return (ret);
10595331Samw }
10605331Samw 
10615331Samw /*
10625331Samw  * smb_share_init()
10635331Samw  *
10645331Samw  * Initialize the smb plugin.
10655331Samw  */
10665331Samw 
10675331Samw static int
10685331Samw smb_share_init(void)
10695331Samw {
10705331Samw 	int ret = SA_OK;
10715331Samw 
10725331Samw 	if (sa_plugin_ops.sa_init != smb_share_init)
10735331Samw 		return (SA_SYSTEM_ERR);
10745331Samw 
10756019Sdougm 	ret = smb_load_proto_properties();
10765331Samw 
10775331Samw 	return (ret);
10785331Samw }
10795331Samw 
10805331Samw /*
10815331Samw  * smb_share_fini()
10825331Samw  *
10835331Samw  */
10845331Samw static void
10855331Samw smb_share_fini(void)
10865331Samw {
10875331Samw 	xmlFreeNode(protoset);
10885331Samw 	protoset = NULL;
10895772Sas200622 
10905772Sas200622 	(void) lmshrd_door_close();
10915331Samw }
10925331Samw 
10935331Samw /*
10945331Samw  * smb_get_proto_set()
10955331Samw  *
10965331Samw  * Return an optionset with all the protocol specific properties in
10975331Samw  * it.
10985331Samw  */
10995331Samw static sa_protocol_properties_t
11005331Samw smb_get_proto_set(void)
11015331Samw {
11025331Samw 	return (protoset);
11035331Samw }
11045331Samw 
11055331Samw /*
11065772Sas200622  * smb_enable_dependencies()
11075772Sas200622  *
11085772Sas200622  * SMBD_DEFAULT_INSTANCE_FMRI may have some dependencies that aren't
11095772Sas200622  * enabled. This will attempt to enable all of them.
11105772Sas200622  */
11115772Sas200622 static void
11125772Sas200622 smb_enable_dependencies(const char *fmri)
11135772Sas200622 {
11145772Sas200622 	scf_handle_t *handle;
11155772Sas200622 	scf_service_t *service;
11165772Sas200622 	scf_instance_t *inst = NULL;
11175772Sas200622 	scf_iter_t *iter;
11185772Sas200622 	scf_property_t *prop;
11195772Sas200622 	scf_value_t *value;
11205772Sas200622 	scf_propertygroup_t *pg;
11215772Sas200622 	scf_scope_t *scope;
11225772Sas200622 	char type[SCFTYPE_LEN];
11235772Sas200622 	char *dependency;
11245772Sas200622 	char *servname;
11255772Sas200622 	int maxlen;
11265772Sas200622 
11275772Sas200622 	/*
11285772Sas200622 	 * Get all required handles and storage.
11295772Sas200622 	 */
11305772Sas200622 	handle = scf_handle_create(SCF_VERSION);
11315772Sas200622 	if (handle == NULL)
11325772Sas200622 		return;
11335772Sas200622 
11345772Sas200622 	if (scf_handle_bind(handle) != 0) {
11355772Sas200622 		scf_handle_destroy(handle);
11365772Sas200622 		return;
11375772Sas200622 	}
11385772Sas200622 
11395772Sas200622 	maxlen = scf_limit(SCF_LIMIT_MAX_VALUE_LENGTH);
11405772Sas200622 	if (maxlen == (ssize_t)-1)
11415772Sas200622 		maxlen = MAXPATHLEN;
11425772Sas200622 
11435772Sas200622 	dependency = malloc(maxlen);
11445772Sas200622 
11455772Sas200622 	service = scf_service_create(handle);
11465772Sas200622 
11475772Sas200622 	iter = scf_iter_create(handle);
11485772Sas200622 
11495772Sas200622 	pg = scf_pg_create(handle);
11505772Sas200622 
11515772Sas200622 	prop = scf_property_create(handle);
11525772Sas200622 
11535772Sas200622 	value = scf_value_create(handle);
11545772Sas200622 
11555772Sas200622 	scope = scf_scope_create(handle);
11565772Sas200622 
11575772Sas200622 	if (service == NULL || iter == NULL || pg == NULL || prop == NULL ||
11585772Sas200622 	    value == NULL || scope == NULL || dependency == NULL)
11595772Sas200622 		goto done;
11605772Sas200622 
11615772Sas200622 	/*
11625772Sas200622 	 *  We passed in the FMRI for the default instance but for
11635772Sas200622 	 *  some things we need the simple form so construct it. Since
11645772Sas200622 	 *  we reuse the storage that dependency points to, we need to
11655772Sas200622 	 *  use the servname early.
11665772Sas200622 	 */
11675772Sas200622 	(void) snprintf(dependency, maxlen, "%s", fmri + sizeof ("svc:"));
11685772Sas200622 	servname = strrchr(dependency, ':');
11695772Sas200622 	if (servname == NULL)
11705772Sas200622 		goto done;
11715772Sas200622 	*servname = '\0';
11725772Sas200622 	servname = dependency;
11735772Sas200622 
11745772Sas200622 	/*
11755772Sas200622 	 * Setup to iterate over the service property groups, only
11765772Sas200622 	 * looking at those that are "dependency" types. The "entity"
11775772Sas200622 	 * property will have the FMRI of the service we are dependent
11785772Sas200622 	 * on.
11795772Sas200622 	 */
11805772Sas200622 	if (scf_handle_get_scope(handle, SCF_SCOPE_LOCAL, scope) != 0)
11815772Sas200622 		goto done;
11825772Sas200622 
11835772Sas200622 	if (scf_scope_get_service(scope, servname, service) != 0)
11845772Sas200622 		goto done;
11855772Sas200622 
11865772Sas200622 	if (scf_iter_service_pgs(iter, service) != 0)
11875772Sas200622 		goto done;
11885772Sas200622 
11895772Sas200622 	while (scf_iter_next_pg(iter, pg) > 0) {
11905772Sas200622 		char *services[2];
11915772Sas200622 		/*
11925772Sas200622 		 * Have a property group for the service. See if it is
11935772Sas200622 		 * a dependency pg and only do operations on those.
11945772Sas200622 		 */
11955772Sas200622 		if (scf_pg_get_type(pg, type, SCFTYPE_LEN) <= 0)
11965772Sas200622 			continue;
11975772Sas200622 
11985772Sas200622 		if (strncmp(type, SCF_GROUP_DEPENDENCY, SCFTYPE_LEN) != 0)
11995772Sas200622 			continue;
12005772Sas200622 		/*
12015772Sas200622 		 * Have a dependency.  Attempt to enable it.
12025772Sas200622 		 */
12035772Sas200622 		if (scf_pg_get_property(pg, SCF_PROPERTY_ENTITIES, prop) != 0)
12045772Sas200622 			continue;
12055772Sas200622 
12065772Sas200622 		if (scf_property_get_value(prop, value) != 0)
12075772Sas200622 			continue;
12085772Sas200622 
12095772Sas200622 		services[1] = NULL;
12105772Sas200622 
12115772Sas200622 		if (scf_value_get_as_string(value, dependency, maxlen) > 0) {
12125772Sas200622 			services[0] = dependency;
12135772Sas200622 			_check_services(services);
12145772Sas200622 		}
12155772Sas200622 	}
12165772Sas200622 
12175772Sas200622 done:
12185772Sas200622 	if (dependency != NULL)
12195772Sas200622 		free(dependency);
12205772Sas200622 	if (value != NULL)
12215772Sas200622 		scf_value_destroy(value);
12225772Sas200622 	if (prop != NULL)
12235772Sas200622 		scf_property_destroy(prop);
12245772Sas200622 	if (pg != NULL)
12255772Sas200622 		scf_pg_destroy(pg);
12265772Sas200622 	if (iter != NULL)
12275772Sas200622 		scf_iter_destroy(iter);
12285772Sas200622 	if (scope != NULL)
12295772Sas200622 		scf_scope_destroy(scope);
12305772Sas200622 	if (inst != NULL)
12315772Sas200622 		scf_instance_destroy(inst);
12325772Sas200622 	if (service != NULL)
12335772Sas200622 		scf_service_destroy(service);
12345772Sas200622 
12355772Sas200622 	(void) scf_handle_unbind(handle);
12365772Sas200622 	scf_handle_destroy(handle);
12375772Sas200622 }
12385772Sas200622 
12395772Sas200622 /*
12405331Samw  * How long to wait for service to come online
12415331Samw  */
12425331Samw #define	WAIT_FOR_SERVICE	15
12435331Samw 
12445331Samw /*
12455331Samw  * smb_enable_service()
12465331Samw  *
12475331Samw  */
12485331Samw static int
12495331Samw smb_enable_service(void)
12505331Samw {
12515331Samw 	int i;
12525331Samw 	int ret = SA_OK;
12535772Sas200622 	char *service[] = { SMBD_DEFAULT_INSTANCE_FMRI, NULL };
12545331Samw 
12555331Samw 	if (!smb_isonline()) {
12565772Sas200622 		/*
12575772Sas200622 		 * Attempt to start the idmap, and other dependent
12585772Sas200622 		 * services, first.  If it fails, the SMB service will
12595772Sas200622 		 * ultimately fail so we use that as the error.  If we
12605772Sas200622 		 * don't try to enable idmap, smb won't start the
12615772Sas200622 		 * first time unless the admin has done it
12625772Sas200622 		 * manually. The service could be administratively
12635772Sas200622 		 * disabled so we won't always get started.
12645772Sas200622 		 */
12655772Sas200622 		smb_enable_dependencies(SMBD_DEFAULT_INSTANCE_FMRI);
12665772Sas200622 		_check_services(service);
12675331Samw 
12685331Samw 		/* Wait for service to come online */
12695331Samw 		for (i = 0; i < WAIT_FOR_SERVICE; i++) {
12705331Samw 			if (smb_isonline()) {
12715772Sas200622 				ret =  SA_OK;
12725331Samw 				break;
12736030Sjb150015 			} else if (smb_ismaint()) {
12746030Sjb150015 				/* maintenance requires help */
12756030Sjb150015 				ret = SA_SYSTEM_ERR;
12766030Sjb150015 				break;
12776030Sjb150015 			} else if (smb_isdisabled()) {
12786030Sjb150015 				/* disabled is ok */
12796030Sjb150015 				ret = SA_OK;
12806030Sjb150015 				break;
12815331Samw 			} else {
12826030Sjb150015 				/* try another time */
12835331Samw 				ret = SA_BUSY;
12845331Samw 				(void) sleep(1);
12855331Samw 			}
12865331Samw 		}
12875331Samw 	}
12885331Samw 	return (ret);
12895331Samw }
12905331Samw 
12915331Samw /*
12925331Samw  * smb_validate_proto_prop(index, name, value)
12935331Samw  *
12945331Samw  * Verify that the property specified by name can take the new
12955331Samw  * value. This is a sanity check to prevent bad values getting into
12965331Samw  * the default files.
12975331Samw  */
12985331Samw static int
12995331Samw smb_validate_proto_prop(int index, char *name, char *value)
13005331Samw {
13015331Samw 	if ((name == NULL) || (index < 0))
13025331Samw 		return (SA_BAD_VALUE);
13035331Samw 
13045331Samw 	if (smb_proto_options[index].validator == NULL)
13055331Samw 		return (SA_OK);
13065331Samw 
13075331Samw 	if (smb_proto_options[index].validator(index, value) == SA_OK)
13085331Samw 		return (SA_OK);
13095331Samw 	return (SA_BAD_VALUE);
13105331Samw }
13115331Samw 
13125331Samw /*
13135331Samw  * smb_set_proto_prop(prop)
13145331Samw  *
13155331Samw  * check that prop is valid.
13165331Samw  */
13175331Samw /*ARGSUSED*/
13185331Samw static int
13195331Samw smb_set_proto_prop(sa_property_t prop)
13205331Samw {
13215331Samw 	int ret = SA_OK;
13225331Samw 	char *name;
13235331Samw 	char *value;
13245331Samw 	int index = -1;
13255521Sas200622 	struct smb_proto_option_defs *opt;
13265331Samw 
13275331Samw 	name = sa_get_property_attr(prop, "type");
13285331Samw 	value = sa_get_property_attr(prop, "value");
13295331Samw 	if (name != NULL && value != NULL) {
13305331Samw 		index = findprotoopt(name);
13315331Samw 		if (index >= 0) {
13325331Samw 			/* should test for valid value */
13335331Samw 			ret = smb_validate_proto_prop(index, name, value);
13345331Samw 			if (ret == SA_OK) {
13355521Sas200622 				opt = &smb_proto_options[index];
13365521Sas200622 
13375331Samw 				/* Save to SMF */
13385772Sas200622 				(void) smb_config_set(opt->smb_index, value);
13395331Samw 				/*
13405331Samw 				 * Specialized refresh mechanisms can
13415331Samw 				 * be flagged in the proto_options and
13425331Samw 				 * processed here.
13435331Samw 				 */
13445521Sas200622 				if (opt->refresh & SMB_REFRESH_REFRESH)
13456139Sjb150015 					(void) smf_refresh_instance(
13466139Sjb150015 					    SMBD_DEFAULT_INSTANCE_FMRI);
13475521Sas200622 				else if (opt->refresh & SMB_REFRESH_RESTART)
13485331Samw 					(void) smf_restart_instance(
13495331Samw 					    SMBD_DEFAULT_INSTANCE_FMRI);
13505331Samw 			}
13515331Samw 		}
13525331Samw 	}
13535521Sas200622 
13545331Samw 	if (name != NULL)
13555331Samw 		sa_free_attr_string(name);
13565331Samw 	if (value != NULL)
13575331Samw 		sa_free_attr_string(value);
13585331Samw 
13595331Samw 	return (ret);
13605331Samw }
13615331Samw 
13625331Samw /*
13635331Samw  * smb_get_status()
13645331Samw  *
13655331Samw  * What is the current status of the smbd? We use the SMF state here.
13665331Samw  * Caller must free the returned value.
13675331Samw  */
13685331Samw 
13695331Samw static char *
13705331Samw smb_get_status(void)
13715331Samw {
13725331Samw 	char *state = NULL;
13735331Samw 	state = smf_get_state(SMBD_DEFAULT_INSTANCE_FMRI);
13745331Samw 	return (state != NULL ? state : "-");
13755331Samw }
13765331Samw 
13775331Samw /*
13785331Samw  * This protocol plugin require resource names
13795331Samw  */
13805331Samw static uint64_t
13815331Samw smb_share_features(void)
13825331Samw {
13835331Samw 	return (SA_FEATURE_RESOURCE | SA_FEATURE_ALLOWSUBDIRS |
13846088Sdougm 	    SA_FEATURE_ALLOWPARDIRS | SA_FEATURE_SERVER);
13855331Samw }
13865331Samw 
13875331Samw /*
13885331Samw  * This should be used to convert lmshare_info to sa_resource_t
13895331Samw  * Should only be needed to build temp shares/resources to be
13905331Samw  * supplied to sharemanager to display temp shares.
13915331Samw  */
13925331Samw static int
13935331Samw smb_build_tmp_sa_resource(sa_handle_t handle, lmshare_info_t *si)
13945331Samw {
13955331Samw 	int err;
13965331Samw 	sa_share_t share;
13975331Samw 	sa_group_t group;
13985331Samw 	sa_resource_t resource;
13995331Samw 
14005331Samw 	if (si == NULL)
14015331Samw 		return (SA_INVALID_NAME);
14025331Samw 
14035331Samw 	/*
14045331Samw 	 * First determine if the "share path" is already shared
14055331Samw 	 * somewhere. If it is, we have to use it as the authority on
14065331Samw 	 * where the transient share lives so will use it's parent
14075331Samw 	 * group. If it doesn't exist, it needs to land in "smb".
14085331Samw 	 */
14095331Samw 
14105331Samw 	share = sa_find_share(handle, si->directory);
14115331Samw 	if (share != NULL) {
14125331Samw 		group = sa_get_parent_group(share);
14135331Samw 	} else {
14145331Samw 		group = smb_get_smb_share_group(handle);
14155331Samw 		if (group == NULL)
14165331Samw 			return (SA_NO_SUCH_GROUP);
14175331Samw 		share = sa_get_share(group, si->directory);
14185331Samw 		if (share == NULL) {
14195331Samw 			share = sa_add_share(group, si->directory,
14205331Samw 			    SA_SHARE_TRANSIENT, &err);
14215331Samw 			if (share == NULL)
14225331Samw 				return (SA_NO_SUCH_PATH);
14235331Samw 		}
14245331Samw 	}
14255331Samw 
14265331Samw 	/*
14275331Samw 	 * Now handle the resource. Make sure that the resource is
14285331Samw 	 * transient and added to the share.
14295331Samw 	 */
14305331Samw 	resource = sa_get_share_resource(share, si->share_name);
14315331Samw 	if (resource == NULL) {
14325331Samw 		resource = sa_add_resource(share,
14335331Samw 		    si->share_name, SA_SHARE_TRANSIENT, &err);
14345331Samw 		if (resource == NULL)
14355331Samw 			return (SA_NO_SUCH_RESOURCE);
14365331Samw 	}
14375331Samw 
14385331Samw 	/* set resource attributes now */
14395331Samw 	(void) sa_set_resource_attr(resource, "description", si->comment);
14405331Samw 	(void) sa_set_resource_attr(resource, SHOPT_AD_CONTAINER,
14415331Samw 	    si->container);
14425331Samw 
14435331Samw 	return (SA_OK);
14445331Samw }
14455331Samw 
14465331Samw /*
14475331Samw  * Return smb transient shares.  Note that we really want to look at
14485331Samw  * all current shares from SMB in order to determine this. Transient
14495331Samw  * shares should be those that don't appear in either the SMF or ZFS
14505331Samw  * configurations.  Those that are in the repositories will be
14515331Samw  * filtered out by smb_build_tmp_sa_resource.
14525331Samw  */
14535331Samw static int
14545331Samw smb_list_transient(sa_handle_t handle)
14555331Samw {
14565331Samw 	int i, offset, num;
14575331Samw 	lmshare_list_t list;
14585331Samw 	int res;
14595331Samw 
14605331Samw 	num = lmshrd_num_shares();
14615331Samw 	if (num <= 0)
14625331Samw 		return (SA_OK);
14635331Samw 	offset = 0;
14645331Samw 	while (lmshrd_list(offset, &list) != NERR_InternalError) {
14655331Samw 		if (list.no == 0)
14665331Samw 			break;
14675331Samw 		for (i = 0; i < list.no; i++) {
14685331Samw 			res = smb_build_tmp_sa_resource(handle,
14695331Samw 			    &(list.smbshr[i]));
14705331Samw 			if (res != SA_OK)
14715331Samw 				return (res);
14725331Samw 		}
14735331Samw 		offset += list.no;
14745331Samw 	}
14755331Samw 
14765331Samw 	return (SA_OK);
14775331Samw }
14785331Samw 
14795331Samw /*
14805331Samw  * fix_resource_name(share, name,  prefix)
14815331Samw  *
14825331Samw  * Construct a name where the ZFS dataset has the prefix replaced with "name".
14835331Samw  */
14845331Samw static char *
14855331Samw fix_resource_name(sa_share_t share, char *name, char *prefix)
14865331Samw {
14875331Samw 	char *dataset = NULL;
14885331Samw 	char *newname = NULL;
14895331Samw 	size_t psize;
14905331Samw 	size_t nsize;
14915331Samw 
14925331Samw 	dataset = sa_get_share_attr(share, "dataset");
14935331Samw 
14945331Samw 	if (dataset != NULL && strcmp(dataset, prefix) != 0) {
14955331Samw 		psize = strlen(prefix);
14965331Samw 		if (strncmp(dataset, prefix, psize) == 0) {
14975331Samw 			/* need string plus ',' and NULL */
14985331Samw 			nsize = (strlen(dataset) - psize) + strlen(name) + 2;
14995331Samw 			newname = calloc(nsize, 1);
15005331Samw 			if (newname != NULL) {
15015331Samw 				(void) snprintf(newname, nsize, "%s%s", name,
15025331Samw 				    dataset + psize);
15035331Samw 				sa_fix_resource_name(newname);
15045331Samw 			}
15055331Samw 			sa_free_attr_string(dataset);
15065331Samw 			return (newname);
15075331Samw 		}
15085331Samw 	}
15095331Samw 	if (dataset != NULL)
15105331Samw 		sa_free_attr_string(dataset);
15115331Samw 	return (strdup(name));
15125331Samw }
15135331Samw 
15145331Samw /*
15155331Samw  * smb_parse_optstring(group, options)
15165331Samw  *
15175331Samw  * parse a compact option string into individual options. This allows
15185331Samw  * ZFS sharesmb and sharemgr "share" command to work.  group can be a
15195331Samw  * group, a share or a resource.
15205331Samw  */
15215331Samw static int
15225331Samw smb_parse_optstring(sa_group_t group, char *options)
15235331Samw {
15245331Samw 	char *dup;
15255331Samw 	char *base;
15265331Samw 	char *lasts;
15275331Samw 	char *token;
15285331Samw 	sa_optionset_t optionset;
15295331Samw 	sa_group_t parent = NULL;
15305331Samw 	sa_resource_t resource = NULL;
15315331Samw 	int iszfs = 0;
15325331Samw 	int persist = 0;
15335331Samw 	int need_optionset = 0;
15345331Samw 	int ret = SA_OK;
15355331Samw 	sa_property_t prop;
15365331Samw 
15375331Samw 	/*
15385331Samw 	 * In order to not attempt to change ZFS properties unless
15395331Samw 	 * absolutely necessary, we never do it in the legacy parsing
15405331Samw 	 * so we need to keep track of this.
15415331Samw 	 */
15425331Samw 	if (sa_is_share(group)) {
15435331Samw 		char *zfs;
15445331Samw 
15455331Samw 		parent = sa_get_parent_group(group);
15465331Samw 		if (parent != NULL) {
15475331Samw 			zfs = sa_get_group_attr(parent, "zfs");
15485331Samw 			if (zfs != NULL) {
15495331Samw 				sa_free_attr_string(zfs);
15505331Samw 				iszfs = 1;
15515331Samw 			}
15525331Samw 		}
15535331Samw 	} else {
15545331Samw 		iszfs = sa_group_is_zfs(group);
15555331Samw 		/*
15565331Samw 		 * If a ZFS group, then we need to see if a resource
15575331Samw 		 * name is being set. If so, bail with
15585331Samw 		 * SA_PROP_SHARE_ONLY, so we come back in with a share
15595331Samw 		 * instead of a group.
15605331Samw 		 */
15615331Samw 		if (strncmp(options, "name=", sizeof ("name=") - 1) == 0 ||
15625331Samw 		    strstr(options, ",name=") != NULL) {
15635331Samw 			return (SA_PROP_SHARE_ONLY);
15645331Samw 		}
15655331Samw 	}
15665331Samw 
15675331Samw 	/* do we have an existing optionset? */
15685331Samw 	optionset = sa_get_optionset(group, "smb");
15695331Samw 	if (optionset == NULL) {
15705331Samw 		/* didn't find existing optionset so create one */
15715331Samw 		optionset = sa_create_optionset(group, "smb");
15725331Samw 		if (optionset == NULL)
15735331Samw 			return (SA_NO_MEMORY);
15745331Samw 	} else {
15755331Samw 		/*
15765331Samw 		 * If an optionset already exists, we've come through
15775331Samw 		 * twice so ignore the second time.
15785331Samw 		 */
15795331Samw 		return (ret);
15805331Samw 	}
15815331Samw 
15825331Samw 	/* We need a copy of options for the next part. */
15835331Samw 	dup = strdup(options);
15845331Samw 	if (dup == NULL)
15855331Samw 		return (SA_NO_MEMORY);
15865331Samw 
15875331Samw 	/*
15885331Samw 	 * SMB properties are straightforward and are strings,
15895331Samw 	 * integers or booleans.  Properties are separated by
15905331Samw 	 * commas. It will be necessary to parse quotes due to some
15915331Samw 	 * strings not having a restricted characters set.
15925331Samw 	 *
15935331Samw 	 * Note that names will create a resource. For now, if there
15945331Samw 	 * is a set of properties "before" the first name="", those
15955331Samw 	 * properties will be placed on the group.
15965331Samw 	 */
15975331Samw 	persist = sa_is_persistent(group);
15985331Samw 	base = dup;
15995331Samw 	token = dup;
16005331Samw 	lasts = NULL;
16015331Samw 	while (token != NULL && ret == SA_OK) {
16025331Samw 		ret = SA_OK;
16035331Samw 		token = strtok_r(base, ",", &lasts);
16045331Samw 		base = NULL;
16055331Samw 		if (token != NULL) {
16065331Samw 			char *value;
16075331Samw 			/*
16085331Samw 			 * All SMB properties have values so there
16095331Samw 			 * MUST be an '=' character.  If it doesn't,
16105331Samw 			 * it is a syntax error.
16115331Samw 			 */
16125331Samw 			value = strchr(token, '=');
16135331Samw 			if (value != NULL) {
16145331Samw 				*value++ = '\0';
16155331Samw 			} else {
16165331Samw 				ret = SA_SYNTAX_ERR;
16175331Samw 				break;
16185331Samw 			}
16195331Samw 			/*
16205331Samw 			 * We may need to handle a "name" property
16215331Samw 			 * that is a ZFS imposed resource name. Each
16225331Samw 			 * name would trigger getting a new "resource"
16235331Samw 			 * to put properties on. For now, assume no
16245331Samw 			 * "name" property for special handling.
16255331Samw 			 */
16265331Samw 
16275331Samw 			if (strcmp(token, "name") == 0) {
16285331Samw 				char *prefix;
16295331Samw 				char *name = NULL;
16305331Samw 				/*
16315331Samw 				 * We have a name, so now work on the
16325331Samw 				 * resource level. We have a "share"
16335331Samw 				 * in "group" due to the caller having
16345331Samw 				 * added it. If we are called with a
16355331Samw 				 * group, the check for group/share
16365331Samw 				 * at the beginning of this function
16375331Samw 				 * will bail out the parse if there is a
16385331Samw 				 * "name" but no share.
16395331Samw 				 */
16405331Samw 				if (!iszfs) {
16415331Samw 					ret = SA_SYNTAX_ERR;
16425331Samw 					break;
16435331Samw 				}
16445331Samw 				/*
16455331Samw 				 * Make sure the parent group has the
16465331Samw 				 * "prefix" property since we will
16475331Samw 				 * need to use this for constructing
16485331Samw 				 * inherited name= values.
16495331Samw 				 */
16505331Samw 				prefix = sa_get_group_attr(parent, "prefix");
16515331Samw 				if (prefix == NULL) {
16525331Samw 					prefix = sa_get_group_attr(parent,
16535331Samw 					    "name");
16545331Samw 					if (prefix != NULL) {
16555331Samw 						(void) sa_set_group_attr(parent,
16565331Samw 						    "prefix", prefix);
16575331Samw 					}
16585331Samw 				}
16595331Samw 				name = fix_resource_name((sa_share_t)group,
16605331Samw 				    value, prefix);
16615331Samw 				if (name != NULL) {
16625331Samw 					resource = sa_add_resource(
16635331Samw 					    (sa_share_t)group, name,
16645331Samw 					    SA_SHARE_TRANSIENT, &ret);
16655331Samw 					sa_free_attr_string(name);
16665331Samw 				} else {
16675331Samw 					ret = SA_NO_MEMORY;
16685331Samw 				}
16695331Samw 				if (prefix != NULL)
16705331Samw 					sa_free_attr_string(prefix);
16715331Samw 
16725331Samw 				/* A resource level optionset is needed */
16735331Samw 
16745331Samw 				need_optionset = 1;
16755331Samw 				if (resource == NULL) {
16765331Samw 					ret = SA_NO_MEMORY;
16775331Samw 					break;
16785331Samw 				}
16795331Samw 				continue;
16805331Samw 			}
16815331Samw 
16825331Samw 			if (need_optionset) {
16835331Samw 				optionset = sa_create_optionset(resource,
16845331Samw 				    "smb");
16855331Samw 				need_optionset = 0;
16865331Samw 			}
16875331Samw 
16885331Samw 			prop = sa_create_property(token, value);
16895331Samw 			if (prop == NULL)
16905331Samw 				ret = SA_NO_MEMORY;
16915331Samw 			else
16925331Samw 				ret = sa_add_property(optionset, prop);
16935331Samw 			if (ret != SA_OK)
16945331Samw 				break;
16955331Samw 			if (!iszfs)
16965331Samw 				ret = sa_commit_properties(optionset, !persist);
16975331Samw 		}
16985331Samw 	}
16995331Samw 	free(dup);
17005331Samw 	return (ret);
17015331Samw }
17025331Samw 
17035331Samw /*
17045331Samw  * smb_sprint_option(rbuff, rbuffsize, incr, prop, sep)
17055331Samw  *
17065331Samw  * provides a mechanism to format SMB properties into legacy output
17075331Samw  * format. If the buffer would overflow, it is reallocated and grown
17085331Samw  * as appropriate. Special cases of converting internal form of values
17095331Samw  * to those used by "share" are done. this function does one property
17105331Samw  * at a time.
17115331Samw  */
17125331Samw 
17135331Samw static void
17145331Samw smb_sprint_option(char **rbuff, size_t *rbuffsize, size_t incr,
17155331Samw 			sa_property_t prop, int sep)
17165331Samw {
17175331Samw 	char *name;
17185331Samw 	char *value;
17195331Samw 	int curlen;
17205331Samw 	char *buff = *rbuff;
17215331Samw 	size_t buffsize = *rbuffsize;
17225331Samw 
17235331Samw 	name = sa_get_property_attr(prop, "type");
17245331Samw 	value = sa_get_property_attr(prop, "value");
17255331Samw 	if (buff != NULL)
17265331Samw 		curlen = strlen(buff);
17275331Samw 	else
17285331Samw 		curlen = 0;
17295331Samw 	if (name != NULL) {
17305331Samw 		int len;
17315331Samw 		len = strlen(name) + sep;
17325331Samw 
17335331Samw 		/*
17345331Samw 		 * A future RFE would be to replace this with more
17355331Samw 		 * generic code and to possibly handle more types.
17365331Samw 		 *
17375331Samw 		 * For now, everything else is treated as a string. If
17385331Samw 		 * we get any properties that aren't exactly
17395331Samw 		 * name/value pairs, we may need to
17405331Samw 		 * interpret/transform.
17415331Samw 		 */
17425331Samw 		if (value != NULL)
17435331Samw 			len += 1 + strlen(value);
17445331Samw 
17455331Samw 		while (buffsize <= (curlen + len)) {
17465331Samw 			/* need more room */
17475331Samw 			buffsize += incr;
17485331Samw 			buff = realloc(buff, buffsize);
17495331Samw 			*rbuff = buff;
17505331Samw 			*rbuffsize = buffsize;
17515331Samw 			if (buff == NULL) {
17525331Samw 				/* realloc failed so free everything */
17535331Samw 				if (*rbuff != NULL)
17545331Samw 					free(*rbuff);
17555331Samw 				goto err;
17565331Samw 			}
17575331Samw 		}
17585331Samw 		if (buff == NULL)
17595331Samw 			goto err;
17605331Samw 		(void) snprintf(buff + curlen, buffsize - curlen,
17615331Samw 		    "%s%s=%s", sep ? "," : "",
17625331Samw 		    name, value != NULL ? value : "\"\"");
17635331Samw 
17645331Samw 	}
17655331Samw err:
17665331Samw 	if (name != NULL)
17675331Samw 		sa_free_attr_string(name);
17685331Samw 	if (value != NULL)
17695331Samw 		sa_free_attr_string(value);
17705331Samw }
17715331Samw 
17725331Samw /*
17735331Samw  * smb_format_resource_options(resource, hier)
17745331Samw  *
17755331Samw  * format all the options on the group into a flattened option
17765331Samw  * string. If hier is non-zero, walk up the tree to get inherited
17775331Samw  * options.
17785331Samw  */
17795331Samw 
17805331Samw static char *
17815331Samw smb_format_options(sa_group_t group, int hier)
17825331Samw {
17835331Samw 	sa_optionset_t options = NULL;
17845331Samw 	sa_property_t prop;
17855331Samw 	int sep = 0;
17865331Samw 	char *buff;
17875331Samw 	size_t buffsize;
17885331Samw 
17895331Samw 
17905331Samw 	buff = malloc(OPT_CHUNK);
17915331Samw 	if (buff == NULL)
17925331Samw 		return (NULL);
17935331Samw 
17945331Samw 	buff[0] = '\0';
17955331Samw 	buffsize = OPT_CHUNK;
17965331Samw 
17975331Samw 	/*
17985331Samw 	 * We may have a an optionset relative to this item. format
17995331Samw 	 * these if we find them and then add any security definitions.
18005331Samw 	 */
18015331Samw 
18025331Samw 	options = sa_get_derived_optionset(group, "smb", hier);
18035331Samw 
18045331Samw 	/*
18055331Samw 	 * do the default set first but skip any option that is also
18065331Samw 	 * in the protocol specific optionset.
18075331Samw 	 */
18085331Samw 	if (options != NULL) {
18095331Samw 		for (prop = sa_get_property(options, NULL);
18105331Samw 		    prop != NULL; prop = sa_get_next_property(prop)) {
18115331Samw 			/*
18125331Samw 			 * use this one since we skipped any
18135331Samw 			 * of these that were also in
18145331Samw 			 * optdefault
18155331Samw 			 */
18165331Samw 			smb_sprint_option(&buff, &buffsize, OPT_CHUNK,
18175331Samw 			    prop, sep);
18185331Samw 			if (buff == NULL) {
18195331Samw 				/*
18205331Samw 				 * buff could become NULL if there
18215331Samw 				 * isn't enough memory for
18225331Samw 				 * smb_sprint_option to realloc()
18235331Samw 				 * as necessary. We can't really
18245331Samw 				 * do anything about it at this
18255331Samw 				 * point so we return NULL.  The
18265331Samw 				 * caller should handle the
18275331Samw 				 * failure.
18285331Samw 				 */
18295331Samw 				if (options != NULL)
18305331Samw 					sa_free_derived_optionset(
18315331Samw 					    options);
18325331Samw 				return (buff);
18335331Samw 			}
18345331Samw 			sep = 1;
18355331Samw 		}
18365331Samw 	}
18375331Samw 
18385331Samw 	if (options != NULL)
18395331Samw 		sa_free_derived_optionset(options);
18405331Samw 	return (buff);
18415331Samw }
18425331Samw 
18435331Samw /*
18445331Samw  * smb_rename_resource(resource, newname)
18455331Samw  *
18465331Samw  * Change the current exported name of the resource to newname.
18475331Samw  */
18485331Samw /*ARGSUSED*/
18495331Samw int
18505331Samw smb_rename_resource(sa_handle_t handle, sa_resource_t resource, char *newname)
18515331Samw {
18525331Samw 	int ret = SA_OK;
18535331Samw 	int err;
18545331Samw 	char *oldname;
18555331Samw 
18565331Samw 	oldname = sa_get_resource_attr(resource, "name");
18575331Samw 	if (oldname == NULL)
18585331Samw 		return (SA_NO_SUCH_RESOURCE);
18595331Samw 
18605331Samw 	err = lmshrd_rename(oldname, newname);
18615331Samw 
18625331Samw 	/* improve error values somewhat */
18635331Samw 	switch (err) {
18645331Samw 	case NERR_Success:
18655331Samw 		break;
18665331Samw 	case NERR_InternalError:
18675331Samw 		ret = SA_SYSTEM_ERR;
18685331Samw 		break;
18695331Samw 	case NERR_DuplicateShare:
18705331Samw 		ret = SA_DUPLICATE_NAME;
18715331Samw 		break;
18725331Samw 	default:
18735331Samw 		ret = SA_CONFIG_ERR;
18745331Samw 		break;
18755331Samw 	}
18765331Samw 
18775331Samw 	return (ret);
18785331Samw }
1879