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