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 /*
285331Samw  * SMB specific functions
295331Samw  */
305331Samw #include <stdio.h>
315331Samw #include <string.h>
325331Samw #include <ctype.h>
335331Samw #include <stdlib.h>
345331Samw #include <unistd.h>
355331Samw #include <zone.h>
365331Samw #include <errno.h>
375331Samw #include <locale.h>
385331Samw #include <fcntl.h>
395331Samw #include <sys/types.h>
405331Samw #include <sys/stat.h>
415331Samw #include <syslog.h>
425331Samw #include "libshare.h"
435331Samw #include "libshare_impl.h"
445331Samw #include <pwd.h>
455331Samw #include <limits.h>
465331Samw #include <libscf.h>
475331Samw #include <strings.h>
485331Samw #include "libshare_smb.h"
495331Samw #include <rpcsvc/daemon_utils.h>
507052Samw #include <smbsrv/smb_share.h>
515331Samw #include <smbsrv/smbinfo.h>
525331Samw #include <smbsrv/libsmb.h>
535331Samw 
545331Samw /* internal functions */
555331Samw static int smb_share_init(void);
565331Samw static void smb_share_fini(void);
575331Samw static int smb_enable_share(sa_share_t);
585331Samw static int smb_share_changed(sa_share_t);
595331Samw static int smb_resource_changed(sa_resource_t);
605331Samw static int smb_rename_resource(sa_handle_t, sa_resource_t, char *);
615331Samw static int smb_disable_share(sa_share_t share, char *);
626214Sdougm static int smb_validate_property(sa_handle_t, sa_property_t, sa_optionset_t);
635331Samw static int smb_set_proto_prop(sa_property_t);
645331Samw static sa_protocol_properties_t smb_get_proto_set(void);
655331Samw static char *smb_get_status(void);
665331Samw static int smb_parse_optstring(sa_group_t, char *);
675331Samw static char *smb_format_options(sa_group_t, int);
685331Samw 
695331Samw static int smb_enable_service(void);
705331Samw 
715331Samw static int range_check_validator(int, char *);
725331Samw static int range_check_validator_zero_ok(int, char *);
735331Samw static int string_length_check_validator(int, char *);
745331Samw static int true_false_validator(int, char *);
755331Samw static int ip_address_validator_empty_ok(int, char *);
765331Samw static int ip_address_csv_list_validator_empty_ok(int, char *);
775331Samw static int path_validator(int, char *);
785331Samw 
795331Samw static int smb_enable_resource(sa_resource_t);
805331Samw static int smb_disable_resource(sa_resource_t);
815331Samw static uint64_t smb_share_features(void);
825331Samw static int smb_list_transient(sa_handle_t);
835772Sas200622 
847348SJose.Borrego@Sun.COM static int smb_build_shareinfo(sa_share_t, sa_resource_t, smb_share_t *);
857348SJose.Borrego@Sun.COM static sa_group_t smb_get_defaultgrp(sa_handle_t);
867348SJose.Borrego@Sun.COM 
875331Samw /* size of basic format allocation */
885331Samw #define	OPT_CHUNK	1024
895331Samw 
905772Sas200622 /* size of string for types - big enough to hold "dependency" */
915772Sas200622 #define	SCFTYPE_LEN	32
925772Sas200622 
935331Samw /*
945331Samw  * Indexes of entries in smb_proto_options table.
955331Samw  * Changes to smb_proto_options table may require
965331Samw  * an update to these values.
975331Samw  */
985331Samw #define	PROTO_OPT_WINS1			6
995331Samw #define	PROTO_OPT_WINS_EXCLUDE		8
1005331Samw 
1015331Samw 
1025331Samw /*
1035331Samw  * ops vector that provides the protocol specific info and operations
1045331Samw  * for share management.
1055331Samw  */
1065331Samw 
1075331Samw struct sa_plugin_ops sa_plugin_ops = {
1085331Samw 	SA_PLUGIN_VERSION,
1095331Samw 	SMB_PROTOCOL_NAME,
1105331Samw 	smb_share_init,
1115331Samw 	smb_share_fini,
1125331Samw 	smb_enable_share,
1135331Samw 	smb_disable_share,
1145331Samw 	smb_validate_property,
1156007Sthurlow 	NULL,	/* valid_space */
1166007Sthurlow 	NULL,	/* security_prop */
1175331Samw 	smb_parse_optstring,
1185331Samw 	smb_format_options,
1195331Samw 	smb_set_proto_prop,
1205331Samw 	smb_get_proto_set,
1215331Samw 	smb_get_status,
1226007Sthurlow 	NULL,	/* space_alias */
1236007Sthurlow 	NULL,	/* update_legacy */
1246007Sthurlow 	NULL,	/* delete_legacy */
1255331Samw 	smb_share_changed,
1265331Samw 	smb_enable_resource,
1275331Samw 	smb_disable_resource,
1285331Samw 	smb_share_features,
1295331Samw 	smb_list_transient,
1305331Samw 	smb_resource_changed,
1315331Samw 	smb_rename_resource,
1326007Sthurlow 	NULL,	/* run_command */
1336007Sthurlow 	NULL,	/* command_help */
1346007Sthurlow 	NULL	/* delete_proto_section */
1355331Samw };
1365331Samw 
1375331Samw /*
1385331Samw  * option definitions.  Make sure to keep the #define for the option
1395331Samw  * index just before the entry it is the index for. Changing the order
1405331Samw  * can cause breakage.
1415331Samw  */
1425331Samw 
1435331Samw struct option_defs optdefs[] = {
1447052Samw 	{SMB_SHROPT_AD_CONTAINER, OPT_TYPE_STRING},
1457052Samw 	{SMB_SHROPT_NAME, OPT_TYPE_NAME},
146*7961SNatalie.Li@Sun.COM 	{SHOPT_RO, OPT_TYPE_ACCLIST},
147*7961SNatalie.Li@Sun.COM 	{SHOPT_RW, OPT_TYPE_ACCLIST},
148*7961SNatalie.Li@Sun.COM 	{SHOPT_NONE, OPT_TYPE_ACCLIST},
1495331Samw 	{NULL, NULL},
1505331Samw };
1515331Samw 
1525331Samw /*
1535331Samw  * findopt(name)
1545331Samw  *
1555331Samw  * Lookup option "name" in the option table and return the table
1565331Samw  * index.
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  */
1767348SJose.Borrego@Sun.COM static boolean_t
1775331Samw is_a_number(char *number)
1785331Samw {
1797348SJose.Borrego@Sun.COM 	boolean_t isnum = B_TRUE;
1807348SJose.Borrego@Sun.COM 	boolean_t ishex = B_FALSE;
1817348SJose.Borrego@Sun.COM 
1827348SJose.Borrego@Sun.COM 	if (number == NULL || *number == '\0')
1837348SJose.Borrego@Sun.COM 		return (B_FALSE);
1845331Samw 
1857348SJose.Borrego@Sun.COM 	if (strncasecmp(number, "0x", 2) == 0) {
1865331Samw 		number += 2;
1877348SJose.Borrego@Sun.COM 		ishex = B_TRUE;
1885331Samw 	} else if (*number == '-') {
1897348SJose.Borrego@Sun.COM 		number++;
1905331Samw 	}
1915331Samw 
1927348SJose.Borrego@Sun.COM 	while (isnum && (*number != '\0')) {
1937348SJose.Borrego@Sun.COM 		isnum = (ishex) ? isxdigit(*number) : isdigit(*number);
1947348SJose.Borrego@Sun.COM 		number++;
1955331Samw 	}
1967348SJose.Borrego@Sun.COM 
1977348SJose.Borrego@Sun.COM 	return (isnum);
1985331Samw }
1995331Samw 
2005331Samw /*
201*7961SNatalie.Li@Sun.COM  * check ro vs rw values.  Over time this may get beefed up.
202*7961SNatalie.Li@Sun.COM  * for now it just does simple checks.
203*7961SNatalie.Li@Sun.COM  */
204*7961SNatalie.Li@Sun.COM 
205*7961SNatalie.Li@Sun.COM static int
206*7961SNatalie.Li@Sun.COM check_rorw(char *v1, char *v2)
207*7961SNatalie.Li@Sun.COM {
208*7961SNatalie.Li@Sun.COM 	int ret = SA_OK;
209*7961SNatalie.Li@Sun.COM 	if (strcmp(v1, v2) == 0)
210*7961SNatalie.Li@Sun.COM 		ret = SA_VALUE_CONFLICT;
211*7961SNatalie.Li@Sun.COM 	return (ret);
212*7961SNatalie.Li@Sun.COM }
213*7961SNatalie.Li@Sun.COM 
214*7961SNatalie.Li@Sun.COM /*
2155331Samw  * validresource(name)
2165331Samw  *
2175331Samw  * Check that name only has valid characters in it. The current valid
2185331Samw  * set are the printable characters but not including:
2195331Samw  *	" / \ [ ] : | < > + ; , ? * = \t
2205331Samw  * Note that space is included and there is a maximum length.
2215331Samw  */
2227348SJose.Borrego@Sun.COM static boolean_t
2235331Samw validresource(const char *name)
2245331Samw {
2255331Samw 	const char *cp;
2265331Samw 	size_t len;
2275331Samw 
2285331Samw 	if (name == NULL)
2295331Samw 		return (B_FALSE);
2305331Samw 
2315331Samw 	len = strlen(name);
2325331Samw 	if (len == 0 || len > SA_MAX_RESOURCE_NAME)
2335331Samw 		return (B_FALSE);
2345331Samw 
2355331Samw 	if (strpbrk(name, "\"/\\[]:|<>+;,?*=\t") != NULL) {
2365331Samw 		return (B_FALSE);
2375331Samw 	}
2385331Samw 
2395331Samw 	for (cp = name; *cp != '\0'; cp++)
2405331Samw 		if (iscntrl(*cp))
2415331Samw 			return (B_FALSE);
2425331Samw 
2435331Samw 	return (B_TRUE);
2445331Samw }
2455331Samw 
2465331Samw /*
2475331Samw  * smb_isonline()
2485331Samw  *
2495331Samw  * Determine if the SMF service instance is in the online state or
2505331Samw  * not. A number of operations depend on this state.
2515331Samw  */
2525331Samw static boolean_t
2535331Samw smb_isonline(void)
2545331Samw {
2555331Samw 	char *str;
2565331Samw 	boolean_t ret = B_FALSE;
2575331Samw 
2585331Samw 	if ((str = smf_get_state(SMBD_DEFAULT_INSTANCE_FMRI)) != NULL) {
2595331Samw 		ret = (strcmp(str, SCF_STATE_STRING_ONLINE) == 0);
2605331Samw 		free(str);
2615331Samw 	}
2625331Samw 	return (ret);
2635331Samw }
2645331Samw 
2655331Samw /*
2665772Sas200622  * smb_isdisabled()
2675772Sas200622  *
2685772Sas200622  * Determine if the SMF service instance is in the disabled state or
2695772Sas200622  * not. A number of operations depend on this state.
2705772Sas200622  */
2715772Sas200622 static boolean_t
2725772Sas200622 smb_isdisabled(void)
2735772Sas200622 {
2745772Sas200622 	char *str;
2755772Sas200622 	boolean_t ret = B_FALSE;
2765772Sas200622 
2775772Sas200622 	if ((str = smf_get_state(SMBD_DEFAULT_INSTANCE_FMRI)) != NULL) {
2785772Sas200622 		ret = (strcmp(str, SCF_STATE_STRING_DISABLED) == 0);
2795772Sas200622 		free(str);
2805772Sas200622 	}
2815772Sas200622 	return (ret);
2825772Sas200622 }
2835772Sas200622 
2845772Sas200622 /*
2855772Sas200622  * smb_isautoenable()
2865772Sas200622  *
2875772Sas200622  * Determine if the SMF service instance auto_enabled set or not. A
2885772Sas200622  * number of operations depend on this state.  The property not being
2895772Sas200622  * set or being set to true means autoenable.  Only being set to false
2905772Sas200622  * is not autoenabled.
2915772Sas200622  */
2925772Sas200622 static boolean_t
2935772Sas200622 smb_isautoenable(void)
2945772Sas200622 {
2955772Sas200622 	boolean_t ret = B_TRUE;
2965772Sas200622 	scf_simple_prop_t *prop;
2975772Sas200622 	uint8_t *retstr;
2985772Sas200622 
2995772Sas200622 	prop = scf_simple_prop_get(NULL, SMBD_DEFAULT_INSTANCE_FMRI,
3005772Sas200622 	    "application", "auto_enable");
3015772Sas200622 	if (prop != NULL) {
3025772Sas200622 		retstr = scf_simple_prop_next_boolean(prop);
3035772Sas200622 		ret = *retstr != 0;
3045772Sas200622 		scf_simple_prop_free(prop);
3055772Sas200622 	}
3065772Sas200622 	return (ret);
3075772Sas200622 }
3085772Sas200622 
3095772Sas200622 /*
3106030Sjb150015  * smb_ismaint()
3116030Sjb150015  *
3126030Sjb150015  * Determine if the SMF service instance is in the disabled state or
3136030Sjb150015  * not. A number of operations depend on this state.
3146030Sjb150015  */
3156030Sjb150015 static boolean_t
3166030Sjb150015 smb_ismaint(void)
3176030Sjb150015 {
3186030Sjb150015 	char *str;
3196030Sjb150015 	boolean_t ret = B_FALSE;
3206030Sjb150015 
3216030Sjb150015 	if ((str = smf_get_state(SMBD_DEFAULT_INSTANCE_FMRI)) != NULL) {
3226030Sjb150015 		ret = (strcmp(str, SCF_STATE_STRING_MAINT) == 0);
3236030Sjb150015 		free(str);
3246030Sjb150015 	}
3256030Sjb150015 	return (ret);
3266030Sjb150015 }
3276030Sjb150015 
3286030Sjb150015 /*
3295331Samw  * smb_enable_share tells the implementation that it is to enable the share.
3305331Samw  * This entails converting the path and options into the appropriate ioctl
3315331Samw  * calls. It is assumed that all error checking of paths, etc. were
3325331Samw  * done earlier.
3335331Samw  */
3345331Samw static int
3355331Samw smb_enable_share(sa_share_t share)
3365331Samw {
3375331Samw 	char *path;
3387052Samw 	smb_share_t si;
3395331Samw 	sa_resource_t resource;
3405331Samw 	boolean_t iszfs;
3415331Samw 	boolean_t privileged;
3425331Samw 	int err = SA_OK;
3435331Samw 	priv_set_t *priv_effective;
3445331Samw 	boolean_t online;
3455331Samw 
3466270Sdougm 	/*
3476270Sdougm 	 * We only start in the global zone and only run if we aren't
3486270Sdougm 	 * running Trusted Extensions.
3496270Sdougm 	 */
3506270Sdougm 	if (getzoneid() != GLOBAL_ZONEID) {
3516270Sdougm 		(void) printf(dgettext(TEXT_DOMAIN,
3526270Sdougm 		    "SMB: service not supported in local zone\n"));
3536270Sdougm 		return (SA_NOT_SUPPORTED);
3546270Sdougm 	}
3556270Sdougm 	if (is_system_labeled()) {
3566270Sdougm 		(void) printf(dgettext(TEXT_DOMAIN,
3576270Sdougm 		    "SMB: service not supported with Trusted Extensions\n"));
3586270Sdougm 		return (SA_NOT_SUPPORTED);
3596270Sdougm 	}
3606270Sdougm 
3615331Samw 	priv_effective = priv_allocset();
3625331Samw 	(void) getppriv(PRIV_EFFECTIVE, priv_effective);
3635331Samw 	privileged = (priv_isfullset(priv_effective) == B_TRUE);
3645331Samw 	priv_freeset(priv_effective);
3655331Samw 
3665331Samw 	/* get the path since it is important in several places */
3675331Samw 	path = sa_get_share_attr(share, "path");
3685331Samw 	if (path == NULL)
3695331Samw 		return (SA_NO_SUCH_PATH);
3705331Samw 
3715772Sas200622 	/*
3725772Sas200622 	 * If administratively disabled, don't try to start anything.
3735772Sas200622 	 */
3745331Samw 	online = smb_isonline();
3755772Sas200622 	if (!online && !smb_isautoenable() && smb_isdisabled())
3765772Sas200622 		goto done;
3775331Samw 
3785331Samw 	iszfs = sa_path_is_zfs(path);
3795331Samw 
3805331Samw 	if (iszfs) {
3815331Samw 
3825331Samw 		if (privileged == B_FALSE && !online) {
3835331Samw 
3845331Samw 			if (!online) {
3855331Samw 				(void) printf(dgettext(TEXT_DOMAIN,
3865331Samw 				    "SMB: Cannot share remove "
3875331Samw 				    "file system: %s\n"), path);
3885331Samw 				(void) printf(dgettext(TEXT_DOMAIN,
3895331Samw 				    "SMB: Service needs to be enabled "
3905331Samw 				    "by a privileged user\n"));
3915331Samw 				err = SA_NO_PERMISSION;
3925331Samw 				errno = EPERM;
3935331Samw 			}
3945331Samw 			if (err) {
3955331Samw 				sa_free_attr_string(path);
3965331Samw 				return (err);
3975331Samw 			}
3985331Samw 
3995331Samw 		}
4005331Samw 	}
4015331Samw 
4025331Samw 	if (privileged == B_TRUE && !online) {
4035331Samw 		err = smb_enable_service();
4045331Samw 		if (err != SA_OK) {
4055331Samw 			(void) printf(dgettext(TEXT_DOMAIN,
4065331Samw 			    "SMB: Unable to enable service\n"));
4075331Samw 			/*
4085331Samw 			 * For now, it is OK to not be able to enable
4095331Samw 			 * the service.
4105331Samw 			 */
4116030Sjb150015 			if (err == SA_BUSY || err == SA_SYSTEM_ERR)
4125331Samw 				err = SA_OK;
4135331Samw 		} else {
4145331Samw 			online = B_TRUE;
4155331Samw 		}
4165331Samw 	}
4175331Samw 
4185331Samw 	/*
4195331Samw 	 * Don't bother trying to start shares if the service isn't
4205331Samw 	 * running.
4215331Samw 	 */
4225331Samw 	if (!online)
4235331Samw 		goto done;
4245331Samw 
4255331Samw 	/* Each share can have multiple resources */
4265331Samw 	for (resource = sa_get_share_resource(share, NULL);
4275331Samw 	    resource != NULL;
4285331Samw 	    resource = sa_get_next_resource(resource)) {
4297348SJose.Borrego@Sun.COM 		err = smb_build_shareinfo(share, resource, &si);
4307348SJose.Borrego@Sun.COM 		if (err != SA_OK) {
4315331Samw 			sa_free_attr_string(path);
4327348SJose.Borrego@Sun.COM 			return (err);
4335331Samw 		}
4345331Samw 
4355331Samw 		if (!iszfs) {
4367348SJose.Borrego@Sun.COM 			err = smb_share_create(&si);
4375331Samw 		} else {
4385331Samw 			share_t sh;
4395331Samw 
4405331Samw 			sa_sharetab_fill_zfs(share, &sh, "smb");
4415331Samw 			err = sa_share_zfs(share, (char *)path, &sh,
4425331Samw 			    &si, ZFS_SHARE_SMB);
4435331Samw 
4445331Samw 			sa_emptyshare(&sh);
4455331Samw 		}
4465331Samw 	}
4475331Samw 	if (!iszfs)
4485331Samw 		(void) sa_update_sharetab(share, "smb");
4495331Samw done:
4505331Samw 	sa_free_attr_string(path);
4515331Samw 
4525331Samw 	return (err == NERR_DuplicateShare ? 0 : err);
4535331Samw }
4545331Samw 
4555331Samw /*
4565331Samw  * This is the share for CIFS all shares have resource names.
4575331Samw  * Enable tells the smb server to update its hash. If it fails
4585331Samw  * because smb server is down, we just ignore as smb server loads
4595331Samw  * the resources from sharemanager at startup.
4605331Samw  */
4615331Samw 
4625331Samw static int
4635331Samw smb_enable_resource(sa_resource_t resource)
4645331Samw {
4655331Samw 	sa_share_t share;
4667052Samw 	smb_share_t si;
4675772Sas200622 	int ret = SA_OK;
4685772Sas200622 	int err;
4695772Sas200622 	boolean_t isonline;
4705331Samw 
4715331Samw 	share = sa_get_resource_parent(resource);
4725331Samw 	if (share == NULL)
4735331Samw 		return (SA_NO_SUCH_PATH);
4745772Sas200622 
4755772Sas200622 	/*
4765772Sas200622 	 * If administratively disabled, don't try to start anything.
4775772Sas200622 	 */
4785772Sas200622 	isonline = smb_isonline();
4795772Sas200622 	if (!isonline && !smb_isautoenable() && smb_isdisabled())
4807348SJose.Borrego@Sun.COM 		return (SA_OK);
4815772Sas200622 
4827348SJose.Borrego@Sun.COM 	if (!isonline) {
4837348SJose.Borrego@Sun.COM 		(void) smb_enable_service();
4847348SJose.Borrego@Sun.COM 
4857348SJose.Borrego@Sun.COM 		if (!smb_isonline())
4867348SJose.Borrego@Sun.COM 			return (SA_OK);
4875772Sas200622 	}
4885772Sas200622 
4897348SJose.Borrego@Sun.COM 	if ((ret = smb_build_shareinfo(share, resource, &si)) != SA_OK)
4907348SJose.Borrego@Sun.COM 		return (ret);
4915772Sas200622 
4925772Sas200622 	/*
4935772Sas200622 	 * Attempt to add the share. Any error that occurs if it was
4945772Sas200622 	 * online is an error but don't count NERR_DuplicateName if
4955772Sas200622 	 * smb/server had to be brought online since bringing the
4965772Sas200622 	 * service up will enable the share that was just added prior
4975772Sas200622 	 * to the attempt to enable.
4985772Sas200622 	 */
4997348SJose.Borrego@Sun.COM 	err = smb_share_create(&si);
5005772Sas200622 	if (err == NERR_Success || !(!isonline && err == NERR_DuplicateName))
5015772Sas200622 		(void) sa_update_sharetab(share, "smb");
5025772Sas200622 	else
5035331Samw 		return (SA_NOT_SHARED);
5045331Samw 
5057348SJose.Borrego@Sun.COM 	return (SA_OK);
5065331Samw }
5075331Samw 
5085331Samw /*
5095331Samw  * Remove it from smb server hash.
5105331Samw  */
5115331Samw static int
5125331Samw smb_disable_resource(sa_resource_t resource)
5135331Samw {
5145331Samw 	char *rname;
5157052Samw 	uint32_t res;
5165331Samw 	sa_share_t share;
5175331Samw 
5185331Samw 	rname = sa_get_resource_attr(resource, "name");
5195331Samw 	if (rname == NULL)
5205331Samw 		return (SA_NO_SUCH_RESOURCE);
5215331Samw 
5225331Samw 	if (smb_isonline()) {
5237348SJose.Borrego@Sun.COM 		res = smb_share_delete(rname);
5245331Samw 		if (res != NERR_Success) {
5255331Samw 			sa_free_attr_string(rname);
5265331Samw 			return (SA_CONFIG_ERR);
5275331Samw 		}
5285331Samw 	}
5295951Sdougm 
5305951Sdougm 	sa_free_attr_string(rname);
5315951Sdougm 
5325331Samw 	share = sa_get_resource_parent(resource);
5335331Samw 	if (share != NULL) {
5345331Samw 		rname = sa_get_share_attr(share, "path");
5355331Samw 		if (rname != NULL) {
5365951Sdougm 			sa_handle_t handle;
5375951Sdougm 
5385951Sdougm 			handle = sa_find_group_handle((sa_group_t)resource);
5395951Sdougm 			(void) sa_delete_sharetab(handle, rname, "smb");
5405331Samw 			sa_free_attr_string(rname);
5415331Samw 		}
5425331Samw 	}
5435331Samw 	/*
5445331Samw 	 * Always return OK as smb/server may be down and
5455331Samw 	 * Shares will be picked up when loaded.
5465331Samw 	 */
5475331Samw 	return (SA_OK);
5485331Samw }
5495331Samw 
5505331Samw /*
5515331Samw  * smb_share_changed(sa_share_t share)
5525331Samw  *
5535331Samw  * The specified share has changed.
5545331Samw  */
5555331Samw static int
5565331Samw smb_share_changed(sa_share_t share)
5575331Samw {
5585331Samw 	char *path;
5595331Samw 	sa_resource_t resource;
5605331Samw 
5617348SJose.Borrego@Sun.COM 	if (!smb_isonline())
5627348SJose.Borrego@Sun.COM 		return (SA_OK);
5637348SJose.Borrego@Sun.COM 
5645331Samw 	/* get the path since it is important in several places */
5655331Samw 	path = sa_get_share_attr(share, "path");
5665331Samw 	if (path == NULL)
5675331Samw 		return (SA_NO_SUCH_PATH);
5687348SJose.Borrego@Sun.COM 
5695331Samw 	for (resource = sa_get_share_resource(share, NULL);
5705331Samw 	    resource != NULL;
5715331Samw 	    resource = sa_get_next_resource(resource))
5725331Samw 		(void) smb_resource_changed(resource);
5735331Samw 
5745331Samw 	sa_free_attr_string(path);
5755331Samw 
5765331Samw 	return (SA_OK);
5775331Samw }
5785331Samw 
5795331Samw /*
5805331Samw  * smb_resource_changed(sa_resource_t resource)
5815331Samw  *
5825331Samw  * The specified resource has changed.
5835331Samw  */
5845331Samw static int
5855331Samw smb_resource_changed(sa_resource_t resource)
5865331Samw {
5877052Samw 	uint32_t res;
5887348SJose.Borrego@Sun.COM 	sa_share_t share;
5897052Samw 	smb_share_t si;
5905331Samw 
5917348SJose.Borrego@Sun.COM 	if (!smb_isonline())
5927348SJose.Borrego@Sun.COM 		return (SA_OK);
5935331Samw 
5947348SJose.Borrego@Sun.COM 	if ((share = sa_get_resource_parent(resource)) == NULL)
5957348SJose.Borrego@Sun.COM 		return (SA_CONFIG_ERR);
5965331Samw 
5977348SJose.Borrego@Sun.COM 	if ((res = smb_build_shareinfo(share, resource, &si)) != SA_OK)
5987348SJose.Borrego@Sun.COM 		return (res);
5995331Samw 
600*7961SNatalie.Li@Sun.COM 	res = smb_share_modify(&si);
6017348SJose.Borrego@Sun.COM 
6027348SJose.Borrego@Sun.COM 	if (res != NERR_Success)
6035331Samw 		return (SA_CONFIG_ERR);
6045331Samw 
6055331Samw 	return (smb_enable_service());
6065331Samw }
6075331Samw 
6085331Samw /*
6095800Sdougm  * smb_disable_share(sa_share_t share, char *path)
6105331Samw  *
6115800Sdougm  * Unshare the specified share. Note that "path" is the same
6125800Sdougm  * path as what is in the "share" object. It is passed in to avoid an
6135800Sdougm  * additional lookup. A missing "path" value makes this a no-op
6145800Sdougm  * function.
6155331Samw  */
6165331Samw static int
6175331Samw smb_disable_share(sa_share_t share, char *path)
6185331Samw {
6195331Samw 	char *rname;
6205331Samw 	sa_resource_t resource;
6215800Sdougm 	sa_group_t parent;
6225331Samw 	boolean_t iszfs;
6235331Samw 	int err = SA_OK;
6245951Sdougm 	sa_handle_t handle;
6257483SDoug.McCallum@Sun.COM 	boolean_t first = B_TRUE; /* work around sharetab issue */
6265331Samw 
6275800Sdougm 	if (path == NULL)
6285800Sdougm 		return (err);
6295800Sdougm 
6305800Sdougm 	/*
6315800Sdougm 	 * If the share is in a ZFS group we need to handle it
6325800Sdougm 	 * differently.  Just being on a ZFS file system isn't
6335800Sdougm 	 * enough since we may be in a legacy share case.
6345800Sdougm 	 */
6355800Sdougm 	parent = sa_get_parent_group(share);
6365800Sdougm 	iszfs = sa_group_is_zfs(parent);
6375800Sdougm 
6385331Samw 	if (!smb_isonline())
6395331Samw 		goto done;
6405331Samw 
6415331Samw 	for (resource = sa_get_share_resource(share, NULL);
6427483SDoug.McCallum@Sun.COM 	    resource != NULL || err != SA_OK;
6435331Samw 	    resource = sa_get_next_resource(resource)) {
6445331Samw 		rname = sa_get_resource_attr(resource, "name");
6455331Samw 		if (rname == NULL) {
6465331Samw 			continue;
6475331Samw 		}
6485331Samw 		if (!iszfs) {
6497348SJose.Borrego@Sun.COM 			err = smb_share_delete(rname);
6505331Samw 			switch (err) {
6515331Samw 			case NERR_NetNameNotFound:
6525331Samw 			case NERR_Success:
6535331Samw 				err = SA_OK;
6545331Samw 				break;
6555331Samw 			default:
6565331Samw 				err = SA_CONFIG_ERR;
6575331Samw 				break;
6585331Samw 			}
6595331Samw 		} else {
6605331Samw 			share_t sh;
6615331Samw 
6625331Samw 			sa_sharetab_fill_zfs(share, &sh, "smb");
6635331Samw 			err = sa_share_zfs(share, (char *)path, &sh,
6645331Samw 			    rname, ZFS_UNSHARE_SMB);
6657483SDoug.McCallum@Sun.COM 			/*
6667483SDoug.McCallum@Sun.COM 			 * If we are no longer the first case, we
6677483SDoug.McCallum@Sun.COM 			 * don't care about the sa_share_zfs err if it
6687483SDoug.McCallum@Sun.COM 			 * is -1. This works around a problem in
6697483SDoug.McCallum@Sun.COM 			 * sharefs and should be removed when sharefs
6707483SDoug.McCallum@Sun.COM 			 * supports multiple entries per path.
6717483SDoug.McCallum@Sun.COM 			 */
6727483SDoug.McCallum@Sun.COM 			if (!first && err == -1)
6737483SDoug.McCallum@Sun.COM 				err = SA_OK;
6747483SDoug.McCallum@Sun.COM 			first = B_FALSE;
6757483SDoug.McCallum@Sun.COM 
6765331Samw 			sa_emptyshare(&sh);
6775331Samw 		}
6785331Samw 		sa_free_attr_string(rname);
6795331Samw 	}
6805331Samw done:
6815951Sdougm 	if (!iszfs) {
6825951Sdougm 		handle = sa_find_group_handle((sa_group_t)share);
6835951Sdougm 		if (handle != NULL)
6845951Sdougm 			(void) sa_delete_sharetab(handle, path, "smb");
6855951Sdougm 		else
6865951Sdougm 			err = SA_SYSTEM_ERR;
6875951Sdougm 	}
6885331Samw 	return (err);
6895331Samw }
6905331Samw 
6915331Samw /*
6926214Sdougm  * smb_validate_property(handle, property, parent)
6935331Samw  *
6945331Samw  * Check that the property has a legitimate value for its type.
6956214Sdougm  * Handle isn't currently used but may need to be in the future.
6965331Samw  */
6975331Samw 
6986214Sdougm /*ARGSUSED*/
6995331Samw static int
7006214Sdougm smb_validate_property(sa_handle_t handle, sa_property_t property,
7016214Sdougm     sa_optionset_t parent)
7025331Samw {
7035331Samw 	int ret = SA_OK;
7045331Samw 	char *propname;
7055331Samw 	int optindex;
7065331Samw 	sa_group_t parent_group;
7075331Samw 	char *value;
708*7961SNatalie.Li@Sun.COM 	char *other;
7095331Samw 
7105331Samw 	propname = sa_get_property_attr(property, "type");
7115331Samw 
7125331Samw 	if ((optindex = findopt(propname)) < 0)
7135331Samw 		ret = SA_NO_SUCH_PROP;
7145331Samw 
7155331Samw 	/* need to validate value range here as well */
7165331Samw 	if (ret == SA_OK) {
7175331Samw 		parent_group = sa_get_parent_group((sa_share_t)parent);
7185331Samw 		if (optdefs[optindex].share && !sa_is_share(parent_group))
7195331Samw 			ret = SA_PROP_SHARE_ONLY;
7205331Samw 	}
7215331Samw 	if (ret != SA_OK) {
7225331Samw 		if (propname != NULL)
7235331Samw 			sa_free_attr_string(propname);
7245331Samw 		return (ret);
7255331Samw 	}
7265331Samw 
7275331Samw 	value = sa_get_property_attr(property, "value");
7285331Samw 	if (value != NULL) {
7295331Samw 		/* first basic type checking */
7305331Samw 		switch (optdefs[optindex].type) {
7315331Samw 		case OPT_TYPE_NUMBER:
7325331Samw 			/* check that the value is all digits */
7335331Samw 			if (!is_a_number(value))
7345331Samw 				ret = SA_BAD_VALUE;
7355331Samw 			break;
7365331Samw 		case OPT_TYPE_BOOLEAN:
7375331Samw 			if (strlen(value) == 0 ||
7385331Samw 			    strcasecmp(value, "true") == 0 ||
7395331Samw 			    strcmp(value, "1") == 0 ||
7405331Samw 			    strcasecmp(value, "false") == 0 ||
7415331Samw 			    strcmp(value, "0") == 0) {
7425331Samw 				ret = SA_OK;
7435331Samw 			} else {
7445331Samw 				ret = SA_BAD_VALUE;
7455331Samw 			}
7465331Samw 			break;
7475331Samw 		case OPT_TYPE_NAME:
7485331Samw 			/*
7495331Samw 			 * Make sure no invalid characters
7505331Samw 			 */
7515331Samw 			if (validresource(value) == B_FALSE)
7525331Samw 				ret = SA_BAD_VALUE;
7535331Samw 			break;
7545331Samw 		case OPT_TYPE_STRING:
7555331Samw 			/* whatever is here should be ok */
7565331Samw 			break;
757*7961SNatalie.Li@Sun.COM 		case OPT_TYPE_ACCLIST: {
758*7961SNatalie.Li@Sun.COM 			sa_property_t oprop;
759*7961SNatalie.Li@Sun.COM 			char *ovalue;
760*7961SNatalie.Li@Sun.COM 			/*
761*7961SNatalie.Li@Sun.COM 			 * access list handling. Should eventually
762*7961SNatalie.Li@Sun.COM 			 * validate that all the values make sense.
763*7961SNatalie.Li@Sun.COM 			 * Also, ro and rw may have cross value
764*7961SNatalie.Li@Sun.COM 			 * conflicts.
765*7961SNatalie.Li@Sun.COM 			 */
766*7961SNatalie.Li@Sun.COM 			if (parent == NULL)
767*7961SNatalie.Li@Sun.COM 				break;
768*7961SNatalie.Li@Sun.COM 			if (strcmp(propname, SHOPT_RO) == 0)
769*7961SNatalie.Li@Sun.COM 				other = SHOPT_RW;
770*7961SNatalie.Li@Sun.COM 			else if (strcmp(propname, SHOPT_RW) == 0)
771*7961SNatalie.Li@Sun.COM 				other = SHOPT_RO;
772*7961SNatalie.Li@Sun.COM 			else
773*7961SNatalie.Li@Sun.COM 				other = NULL;
774*7961SNatalie.Li@Sun.COM 			if (other == NULL)
775*7961SNatalie.Li@Sun.COM 				break;
776*7961SNatalie.Li@Sun.COM 
777*7961SNatalie.Li@Sun.COM 			/* compare rw(ro) with ro(rw) */
778*7961SNatalie.Li@Sun.COM 			oprop = sa_get_property(parent, other);
779*7961SNatalie.Li@Sun.COM 			if (oprop == NULL)
780*7961SNatalie.Li@Sun.COM 				break;
781*7961SNatalie.Li@Sun.COM 			/*
782*7961SNatalie.Li@Sun.COM 			 * only potential
783*7961SNatalie.Li@Sun.COM 			 * confusion if other
784*7961SNatalie.Li@Sun.COM 			 * exists
785*7961SNatalie.Li@Sun.COM 			 */
786*7961SNatalie.Li@Sun.COM 			ovalue = sa_get_property_attr(oprop, "value");
787*7961SNatalie.Li@Sun.COM 			if (ovalue != NULL) {
788*7961SNatalie.Li@Sun.COM 				ret = check_rorw(value, ovalue);
789*7961SNatalie.Li@Sun.COM 				sa_free_attr_string(ovalue);
790*7961SNatalie.Li@Sun.COM 			}
791*7961SNatalie.Li@Sun.COM 			break;
792*7961SNatalie.Li@Sun.COM 		}
7935331Samw 		default:
7945331Samw 			break;
7955331Samw 		}
7965331Samw 	}
7975331Samw 
7985331Samw 	if (value != NULL)
7995331Samw 		sa_free_attr_string(value);
8005331Samw 	if (ret == SA_OK && optdefs[optindex].check != NULL)
8015331Samw 		/* do the property specific check */
8025331Samw 		ret = optdefs[optindex].check(property);
8035331Samw 
8045331Samw 	if (propname != NULL)
8055331Samw 		sa_free_attr_string(propname);
8065331Samw 	return (ret);
8075331Samw }
8085331Samw 
8095331Samw /*
8105331Samw  * Protocol management functions
8115331Samw  *
8125331Samw  * properties defined in the default files are defined in
8135331Samw  * proto_option_defs for parsing and validation.
8145331Samw  */
8155331Samw 
8165331Samw struct smb_proto_option_defs {
8175331Samw 	int smb_index;
8185331Samw 	int32_t minval;
8195331Samw 	int32_t maxval; /* In case of length of string this should be max */
8205331Samw 	int (*validator)(int, char *);
8215331Samw 	int32_t	refresh;
8225331Samw } smb_proto_options[] = {
8235772Sas200622 	{ SMB_CI_SYS_CMNT, 0, MAX_VALUE_BUFLEN,
8245772Sas200622 	    string_length_check_validator, SMB_REFRESH_REFRESH },
8255772Sas200622 	{ SMB_CI_MAX_WORKERS, 64, 1024, range_check_validator,
8265772Sas200622 	    SMB_REFRESH_REFRESH },
8275772Sas200622 	{ SMB_CI_NBSCOPE, 0, MAX_VALUE_BUFLEN,
8285772Sas200622 	    string_length_check_validator, 0 },
8295772Sas200622 	{ SMB_CI_LM_LEVEL, 2, 5, range_check_validator, 0 },
8305772Sas200622 	{ SMB_CI_KEEPALIVE, 20, 5400, range_check_validator_zero_ok,
8315772Sas200622 	    SMB_REFRESH_REFRESH },
8325772Sas200622 	{ SMB_CI_WINS_SRV1, 0, MAX_VALUE_BUFLEN,
8335772Sas200622 	    ip_address_validator_empty_ok, SMB_REFRESH_REFRESH },
8345772Sas200622 	{ SMB_CI_WINS_SRV2, 0, MAX_VALUE_BUFLEN,
8355772Sas200622 	    ip_address_validator_empty_ok, SMB_REFRESH_REFRESH },
8365772Sas200622 	{ SMB_CI_WINS_EXCL, 0, MAX_VALUE_BUFLEN,
8375772Sas200622 	    ip_address_csv_list_validator_empty_ok, SMB_REFRESH_REFRESH },
8385772Sas200622 	{ SMB_CI_SIGNING_ENABLE, 0, 0, true_false_validator,
8395772Sas200622 	    SMB_REFRESH_REFRESH },
8405772Sas200622 	{ SMB_CI_SIGNING_REQD, 0, 0, true_false_validator,
8415772Sas200622 	    SMB_REFRESH_REFRESH },
8425772Sas200622 	{ SMB_CI_RESTRICT_ANON, 0, 0, true_false_validator,
8435772Sas200622 	    SMB_REFRESH_REFRESH },
8445772Sas200622 	{ SMB_CI_DOMAIN_SRV, 0, MAX_VALUE_BUFLEN,
8455772Sas200622 	    ip_address_validator_empty_ok, 0 },
8465772Sas200622 	{ SMB_CI_ADS_SITE, 0, MAX_VALUE_BUFLEN,
8475772Sas200622 	    string_length_check_validator, SMB_REFRESH_REFRESH },
8485772Sas200622 	{ SMB_CI_DYNDNS_ENABLE, 0, 0, true_false_validator, 0 },
8495772Sas200622 	{ SMB_CI_AUTOHOME_MAP, 0, MAX_VALUE_BUFLEN, path_validator, 0 },
8505331Samw };
8515331Samw 
8525772Sas200622 #define	SMB_OPT_NUM \
8535772Sas200622 	(sizeof (smb_proto_options) / sizeof (smb_proto_options[0]))
8545772Sas200622 
8555331Samw /*
8565331Samw  * Check the range of value as int range.
8575331Samw  */
8585331Samw static int
8595331Samw range_check_validator(int index, char *value)
8605331Samw {
8615331Samw 	int ret = SA_OK;
8625331Samw 
8635331Samw 	if (!is_a_number(value)) {
8645331Samw 		ret = SA_BAD_VALUE;
8655331Samw 	} else {
8665331Samw 		int val;
8675331Samw 		val = strtoul(value, NULL, 0);
8685331Samw 		if (val < smb_proto_options[index].minval ||
8695331Samw 		    val > smb_proto_options[index].maxval)
8705331Samw 			ret = SA_BAD_VALUE;
8715331Samw 	}
8725331Samw 	return (ret);
8735331Samw }
8745331Samw 
8755331Samw /*
8765331Samw  * Check the range of value as int range.
8775331Samw  */
8785331Samw static int
8795331Samw range_check_validator_zero_ok(int index, char *value)
8805331Samw {
8815331Samw 	int ret = SA_OK;
8825331Samw 
8835331Samw 	if (!is_a_number(value)) {
8845331Samw 		ret = SA_BAD_VALUE;
8855331Samw 	} else {
8865331Samw 		int val;
8875331Samw 		val = strtoul(value, NULL, 0);
8885331Samw 		if (val == 0)
8895331Samw 			ret = SA_OK;
8905331Samw 		else {
8915331Samw 			if (val < smb_proto_options[index].minval ||
8925331Samw 			    val > smb_proto_options[index].maxval)
8935331Samw 			ret = SA_BAD_VALUE;
8945331Samw 		}
8955331Samw 	}
8965331Samw 	return (ret);
8975331Samw }
8985331Samw 
8995331Samw /*
9005331Samw  * Check the length of the string
9015331Samw  */
9025331Samw static int
9035331Samw string_length_check_validator(int index, char *value)
9045331Samw {
9055331Samw 	int ret = SA_OK;
9065331Samw 
9075331Samw 	if (value == NULL)
9085331Samw 		return (SA_BAD_VALUE);
9095331Samw 	if (strlen(value) > smb_proto_options[index].maxval)
9105331Samw 		ret = SA_BAD_VALUE;
9115331Samw 	return (ret);
9125331Samw }
9135331Samw 
9145331Samw /*
9155331Samw  * Check yes/no
9165331Samw  */
9175331Samw /*ARGSUSED*/
9185331Samw static int
9195331Samw true_false_validator(int index, char *value)
9205331Samw {
9215331Samw 	if (value == NULL)
9225331Samw 		return (SA_BAD_VALUE);
9235331Samw 	if ((strcasecmp(value, "true") == 0) ||
9245331Samw 	    (strcasecmp(value, "false") == 0))
9255331Samw 		return (SA_OK);
9265331Samw 	return (SA_BAD_VALUE);
9275331Samw }
9285331Samw 
9295331Samw /*
9305331Samw  * Check IP address.
9315331Samw  */
9325331Samw /*ARGSUSED*/
9335331Samw static int
9345331Samw ip_address_validator_empty_ok(int index, char *value)
9355331Samw {
9365331Samw 	char sbytes[16];
9375331Samw 	int len;
9385331Samw 
9395331Samw 	if (value == NULL)
9405331Samw 		return (SA_OK);
9415331Samw 	len = strlen(value);
9425331Samw 	if (len == 0)
9435331Samw 		return (SA_OK);
9445331Samw 	if (inet_pton(AF_INET, value, (void *)sbytes) != 1)
9455331Samw 		return (SA_BAD_VALUE);
9465331Samw 
9475331Samw 	return (SA_OK);
9485331Samw }
9495331Samw 
9505331Samw /*
9515331Samw  * Check IP address list
9525331Samw  */
9535331Samw /*ARGSUSED*/
9545331Samw static int
9555331Samw ip_address_csv_list_validator_empty_ok(int index, char *value)
9565331Samw {
9575331Samw 	char sbytes[16];
9585331Samw 	char *ip, *tmp, *ctx;
9595331Samw 
9605331Samw 	if (value == NULL || *value == '\0')
9615331Samw 		return (SA_OK);
9625331Samw 
9635331Samw 	if (strlen(value) > MAX_VALUE_BUFLEN)
9645331Samw 		return (SA_BAD_VALUE);
9655331Samw 
9665331Samw 	if ((tmp = strdup(value)) == NULL)
9675331Samw 		return (SA_NO_MEMORY);
9685331Samw 
9695331Samw 	ip = strtok_r(tmp, ",", &ctx);
9705331Samw 	while (ip) {
9715331Samw 		if (strlen(ip) == 0) {
9725331Samw 			free(tmp);
9735331Samw 			return (SA_BAD_VALUE);
9745331Samw 		}
9755331Samw 		if (*ip != 0) {
9765331Samw 			if (inet_pton(AF_INET, ip,
9775331Samw 			    (void *)sbytes) != 1) {
9785331Samw 				free(tmp);
9795331Samw 				return (SA_BAD_VALUE);
9805331Samw 			}
9815331Samw 		}
9825331Samw 		ip = strtok_r(0, ",", &ctx);
9835331Samw 	}
9845331Samw 
9855331Samw 	free(tmp);
9865331Samw 	return (SA_OK);
9875331Samw }
9885331Samw 
9895331Samw /*
9905331Samw  * Check path
9915331Samw  */
9925331Samw /*ARGSUSED*/
9935331Samw static int
9947588Samw@Sun.COM path_validator(int index, char *path)
9955331Samw {
9965331Samw 	struct stat buffer;
9975331Samw 	int fd, status;
9985331Samw 
9997588Samw@Sun.COM 	if (path == NULL)
10005331Samw 		return (SA_BAD_VALUE);
10015331Samw 
10027588Samw@Sun.COM 	fd = open(path, O_RDONLY);
10035331Samw 	if (fd < 0)
10045331Samw 		return (SA_BAD_VALUE);
10055331Samw 
10065331Samw 	status = fstat(fd, &buffer);
10075331Samw 	(void) close(fd);
10085331Samw 
10095331Samw 	if (status < 0)
10105331Samw 		return (SA_BAD_VALUE);
10115331Samw 
10125331Samw 	if (buffer.st_mode & S_IFDIR)
10135331Samw 		return (SA_OK);
10145331Samw 	return (SA_BAD_VALUE);
10155331Samw }
10165331Samw 
10175331Samw /*
10185331Samw  * the protoset holds the defined options so we don't have to read
10195331Samw  * them multiple times
10205331Samw  */
10215331Samw static sa_protocol_properties_t protoset;
10225331Samw 
10235331Samw static int
10245331Samw findprotoopt(char *name)
10255331Samw {
10265331Samw 	int i;
10275772Sas200622 	char *sc_name;
10285772Sas200622 
10295772Sas200622 	for (i = 0; i < SMB_OPT_NUM; i++) {
10305772Sas200622 		sc_name = smb_config_getname(smb_proto_options[i].smb_index);
10315772Sas200622 		if (strcasecmp(sc_name, name) == 0)
10325331Samw 			return (i);
10335331Samw 	}
10345772Sas200622 
10355331Samw 	return (-1);
10365331Samw }
10375331Samw 
10385331Samw /*
10395331Samw  * smb_load_proto_properties()
10405331Samw  *
10415331Samw  * read the smb config values from SMF.
10425331Samw  */
10435331Samw 
10445331Samw static int
10455331Samw smb_load_proto_properties()
10465331Samw {
10475331Samw 	sa_property_t prop;
10485772Sas200622 	char value[MAX_VALUE_BUFLEN];
10495772Sas200622 	char *name;
10505331Samw 	int index;
10516019Sdougm 	int ret = SA_OK;
10525772Sas200622 	int rc;
10535331Samw 
10545331Samw 	protoset = sa_create_protocol_properties(SMB_PROTOCOL_NAME);
10555331Samw 	if (protoset == NULL)
10565331Samw 		return (SA_NO_MEMORY);
10575331Samw 
10586019Sdougm 	for (index = 0; index < SMB_OPT_NUM && ret == SA_OK; index++) {
10595772Sas200622 		rc = smb_config_get(smb_proto_options[index].smb_index,
10605772Sas200622 		    value, sizeof (value));
10615772Sas200622 		if (rc != SMBD_SMF_OK)
10625772Sas200622 			continue;
10635772Sas200622 		name = smb_config_getname(smb_proto_options[index].smb_index);
10645772Sas200622 		prop = sa_create_property(name, value);
10655454Sdougm 		if (prop != NULL)
10666019Sdougm 			ret = sa_add_protocol_property(protoset, prop);
10676019Sdougm 		else
10686019Sdougm 			ret = SA_NO_MEMORY;
10695331Samw 	}
10706019Sdougm 	return (ret);
10715331Samw }
10725331Samw 
10735331Samw /*
10745331Samw  * smb_share_init()
10755331Samw  *
10765331Samw  * Initialize the smb plugin.
10775331Samw  */
10785331Samw 
10795331Samw static int
10805331Samw smb_share_init(void)
10815331Samw {
10825331Samw 	if (sa_plugin_ops.sa_init != smb_share_init)
10835331Samw 		return (SA_SYSTEM_ERR);
10845331Samw 
10857588Samw@Sun.COM 	smb_share_door_clnt_init();
10867348SJose.Borrego@Sun.COM 	return (smb_load_proto_properties());
10875331Samw }
10885331Samw 
10895331Samw /*
10905331Samw  * smb_share_fini()
10915331Samw  *
10925331Samw  */
10935331Samw static void
10945331Samw smb_share_fini(void)
10955331Samw {
10965331Samw 	xmlFreeNode(protoset);
10975331Samw 	protoset = NULL;
10985772Sas200622 
10997588Samw@Sun.COM 	smb_share_door_clnt_fini();
11005331Samw }
11015331Samw 
11025331Samw /*
11035331Samw  * smb_get_proto_set()
11045331Samw  *
11055331Samw  * Return an optionset with all the protocol specific properties in
11065331Samw  * it.
11075331Samw  */
11085331Samw static sa_protocol_properties_t
11095331Samw smb_get_proto_set(void)
11105331Samw {
11115331Samw 	return (protoset);
11125331Samw }
11135331Samw 
11145331Samw /*
11155772Sas200622  * smb_enable_dependencies()
11165772Sas200622  *
11175772Sas200622  * SMBD_DEFAULT_INSTANCE_FMRI may have some dependencies that aren't
11185772Sas200622  * enabled. This will attempt to enable all of them.
11195772Sas200622  */
11205772Sas200622 static void
11215772Sas200622 smb_enable_dependencies(const char *fmri)
11225772Sas200622 {
11235772Sas200622 	scf_handle_t *handle;
11245772Sas200622 	scf_service_t *service;
11255772Sas200622 	scf_instance_t *inst = NULL;
11265772Sas200622 	scf_iter_t *iter;
11275772Sas200622 	scf_property_t *prop;
11285772Sas200622 	scf_value_t *value;
11295772Sas200622 	scf_propertygroup_t *pg;
11305772Sas200622 	scf_scope_t *scope;
11315772Sas200622 	char type[SCFTYPE_LEN];
11325772Sas200622 	char *dependency;
11335772Sas200622 	char *servname;
11345772Sas200622 	int maxlen;
11355772Sas200622 
11365772Sas200622 	/*
11375772Sas200622 	 * Get all required handles and storage.
11385772Sas200622 	 */
11395772Sas200622 	handle = scf_handle_create(SCF_VERSION);
11405772Sas200622 	if (handle == NULL)
11415772Sas200622 		return;
11425772Sas200622 
11435772Sas200622 	if (scf_handle_bind(handle) != 0) {
11445772Sas200622 		scf_handle_destroy(handle);
11455772Sas200622 		return;
11465772Sas200622 	}
11475772Sas200622 
11485772Sas200622 	maxlen = scf_limit(SCF_LIMIT_MAX_VALUE_LENGTH);
11495772Sas200622 	if (maxlen == (ssize_t)-1)
11505772Sas200622 		maxlen = MAXPATHLEN;
11515772Sas200622 
11525772Sas200622 	dependency = malloc(maxlen);
11535772Sas200622 
11545772Sas200622 	service = scf_service_create(handle);
11555772Sas200622 
11565772Sas200622 	iter = scf_iter_create(handle);
11575772Sas200622 
11585772Sas200622 	pg = scf_pg_create(handle);
11595772Sas200622 
11605772Sas200622 	prop = scf_property_create(handle);
11615772Sas200622 
11625772Sas200622 	value = scf_value_create(handle);
11635772Sas200622 
11645772Sas200622 	scope = scf_scope_create(handle);
11655772Sas200622 
11665772Sas200622 	if (service == NULL || iter == NULL || pg == NULL || prop == NULL ||
11675772Sas200622 	    value == NULL || scope == NULL || dependency == NULL)
11685772Sas200622 		goto done;
11695772Sas200622 
11705772Sas200622 	/*
11715772Sas200622 	 *  We passed in the FMRI for the default instance but for
11725772Sas200622 	 *  some things we need the simple form so construct it. Since
11735772Sas200622 	 *  we reuse the storage that dependency points to, we need to
11745772Sas200622 	 *  use the servname early.
11755772Sas200622 	 */
11765772Sas200622 	(void) snprintf(dependency, maxlen, "%s", fmri + sizeof ("svc:"));
11775772Sas200622 	servname = strrchr(dependency, ':');
11785772Sas200622 	if (servname == NULL)
11795772Sas200622 		goto done;
11805772Sas200622 	*servname = '\0';
11815772Sas200622 	servname = dependency;
11825772Sas200622 
11835772Sas200622 	/*
11845772Sas200622 	 * Setup to iterate over the service property groups, only
11855772Sas200622 	 * looking at those that are "dependency" types. The "entity"
11865772Sas200622 	 * property will have the FMRI of the service we are dependent
11875772Sas200622 	 * on.
11885772Sas200622 	 */
11895772Sas200622 	if (scf_handle_get_scope(handle, SCF_SCOPE_LOCAL, scope) != 0)
11905772Sas200622 		goto done;
11915772Sas200622 
11925772Sas200622 	if (scf_scope_get_service(scope, servname, service) != 0)
11935772Sas200622 		goto done;
11945772Sas200622 
11955772Sas200622 	if (scf_iter_service_pgs(iter, service) != 0)
11965772Sas200622 		goto done;
11975772Sas200622 
11985772Sas200622 	while (scf_iter_next_pg(iter, pg) > 0) {
11995772Sas200622 		char *services[2];
12005772Sas200622 		/*
12015772Sas200622 		 * Have a property group for the service. See if it is
12025772Sas200622 		 * a dependency pg and only do operations on those.
12035772Sas200622 		 */
12045772Sas200622 		if (scf_pg_get_type(pg, type, SCFTYPE_LEN) <= 0)
12055772Sas200622 			continue;
12065772Sas200622 
12075772Sas200622 		if (strncmp(type, SCF_GROUP_DEPENDENCY, SCFTYPE_LEN) != 0)
12085772Sas200622 			continue;
12095772Sas200622 		/*
12105772Sas200622 		 * Have a dependency.  Attempt to enable it.
12115772Sas200622 		 */
12125772Sas200622 		if (scf_pg_get_property(pg, SCF_PROPERTY_ENTITIES, prop) != 0)
12135772Sas200622 			continue;
12145772Sas200622 
12155772Sas200622 		if (scf_property_get_value(prop, value) != 0)
12165772Sas200622 			continue;
12175772Sas200622 
12185772Sas200622 		services[1] = NULL;
12195772Sas200622 
12205772Sas200622 		if (scf_value_get_as_string(value, dependency, maxlen) > 0) {
12215772Sas200622 			services[0] = dependency;
12225772Sas200622 			_check_services(services);
12235772Sas200622 		}
12245772Sas200622 	}
12255772Sas200622 
12265772Sas200622 done:
12275772Sas200622 	if (dependency != NULL)
12285772Sas200622 		free(dependency);
12295772Sas200622 	if (value != NULL)
12305772Sas200622 		scf_value_destroy(value);
12315772Sas200622 	if (prop != NULL)
12325772Sas200622 		scf_property_destroy(prop);
12335772Sas200622 	if (pg != NULL)
12345772Sas200622 		scf_pg_destroy(pg);
12355772Sas200622 	if (iter != NULL)
12365772Sas200622 		scf_iter_destroy(iter);
12375772Sas200622 	if (scope != NULL)
12385772Sas200622 		scf_scope_destroy(scope);
12395772Sas200622 	if (inst != NULL)
12405772Sas200622 		scf_instance_destroy(inst);
12415772Sas200622 	if (service != NULL)
12425772Sas200622 		scf_service_destroy(service);
12435772Sas200622 
12445772Sas200622 	(void) scf_handle_unbind(handle);
12455772Sas200622 	scf_handle_destroy(handle);
12465772Sas200622 }
12475772Sas200622 
12485772Sas200622 /*
12495331Samw  * How long to wait for service to come online
12505331Samw  */
12515331Samw #define	WAIT_FOR_SERVICE	15
12525331Samw 
12535331Samw /*
12545331Samw  * smb_enable_service()
12555331Samw  *
12565331Samw  */
12575331Samw static int
12585331Samw smb_enable_service(void)
12595331Samw {
12605331Samw 	int i;
12615331Samw 	int ret = SA_OK;
12625772Sas200622 	char *service[] = { SMBD_DEFAULT_INSTANCE_FMRI, NULL };
12635331Samw 
12645331Samw 	if (!smb_isonline()) {
12655772Sas200622 		/*
12665772Sas200622 		 * Attempt to start the idmap, and other dependent
12675772Sas200622 		 * services, first.  If it fails, the SMB service will
12685772Sas200622 		 * ultimately fail so we use that as the error.  If we
12695772Sas200622 		 * don't try to enable idmap, smb won't start the
12705772Sas200622 		 * first time unless the admin has done it
12715772Sas200622 		 * manually. The service could be administratively
12725772Sas200622 		 * disabled so we won't always get started.
12735772Sas200622 		 */
12745772Sas200622 		smb_enable_dependencies(SMBD_DEFAULT_INSTANCE_FMRI);
12755772Sas200622 		_check_services(service);
12765331Samw 
12775331Samw 		/* Wait for service to come online */
12785331Samw 		for (i = 0; i < WAIT_FOR_SERVICE; i++) {
12795331Samw 			if (smb_isonline()) {
12805772Sas200622 				ret =  SA_OK;
12815331Samw 				break;
12826030Sjb150015 			} else if (smb_ismaint()) {
12836030Sjb150015 				/* maintenance requires help */
12846030Sjb150015 				ret = SA_SYSTEM_ERR;
12856030Sjb150015 				break;
12866030Sjb150015 			} else if (smb_isdisabled()) {
12876030Sjb150015 				/* disabled is ok */
12886030Sjb150015 				ret = SA_OK;
12896030Sjb150015 				break;
12905331Samw 			} else {
12916030Sjb150015 				/* try another time */
12925331Samw 				ret = SA_BUSY;
12935331Samw 				(void) sleep(1);
12945331Samw 			}
12955331Samw 		}
12965331Samw 	}
12975331Samw 	return (ret);
12985331Samw }
12995331Samw 
13005331Samw /*
13015331Samw  * smb_validate_proto_prop(index, name, value)
13025331Samw  *
13035331Samw  * Verify that the property specified by name can take the new
13045331Samw  * value. This is a sanity check to prevent bad values getting into
13055331Samw  * the default files.
13065331Samw  */
13075331Samw static int
13085331Samw smb_validate_proto_prop(int index, char *name, char *value)
13095331Samw {
13105331Samw 	if ((name == NULL) || (index < 0))
13115331Samw 		return (SA_BAD_VALUE);
13125331Samw 
13135331Samw 	if (smb_proto_options[index].validator == NULL)
13145331Samw 		return (SA_OK);
13155331Samw 
13165331Samw 	if (smb_proto_options[index].validator(index, value) == SA_OK)
13175331Samw 		return (SA_OK);
13185331Samw 	return (SA_BAD_VALUE);
13195331Samw }
13205331Samw 
13215331Samw /*
13225331Samw  * smb_set_proto_prop(prop)
13235331Samw  *
13245331Samw  * check that prop is valid.
13255331Samw  */
13265331Samw /*ARGSUSED*/
13275331Samw static int
13285331Samw smb_set_proto_prop(sa_property_t prop)
13295331Samw {
13305331Samw 	int ret = SA_OK;
13315331Samw 	char *name;
13325331Samw 	char *value;
13335331Samw 	int index = -1;
13345521Sas200622 	struct smb_proto_option_defs *opt;
13355331Samw 
13365331Samw 	name = sa_get_property_attr(prop, "type");
13375331Samw 	value = sa_get_property_attr(prop, "value");
13385331Samw 	if (name != NULL && value != NULL) {
13395331Samw 		index = findprotoopt(name);
13405331Samw 		if (index >= 0) {
13415331Samw 			/* should test for valid value */
13425331Samw 			ret = smb_validate_proto_prop(index, name, value);
13435331Samw 			if (ret == SA_OK) {
13445521Sas200622 				opt = &smb_proto_options[index];
13455521Sas200622 
13465331Samw 				/* Save to SMF */
13475772Sas200622 				(void) smb_config_set(opt->smb_index, value);
13485331Samw 				/*
13495331Samw 				 * Specialized refresh mechanisms can
13505331Samw 				 * be flagged in the proto_options and
13515331Samw 				 * processed here.
13525331Samw 				 */
13535521Sas200622 				if (opt->refresh & SMB_REFRESH_REFRESH)
13546139Sjb150015 					(void) smf_refresh_instance(
13556139Sjb150015 					    SMBD_DEFAULT_INSTANCE_FMRI);
13565521Sas200622 				else if (opt->refresh & SMB_REFRESH_RESTART)
13575331Samw 					(void) smf_restart_instance(
13585331Samw 					    SMBD_DEFAULT_INSTANCE_FMRI);
13595331Samw 			}
13605331Samw 		}
13615331Samw 	}
13625521Sas200622 
13635331Samw 	if (name != NULL)
13645331Samw 		sa_free_attr_string(name);
13655331Samw 	if (value != NULL)
13665331Samw 		sa_free_attr_string(value);
13675331Samw 
13685331Samw 	return (ret);
13695331Samw }
13705331Samw 
13715331Samw /*
13725331Samw  * smb_get_status()
13735331Samw  *
13745331Samw  * What is the current status of the smbd? We use the SMF state here.
13755331Samw  * Caller must free the returned value.
13765331Samw  */
13775331Samw 
13785331Samw static char *
13795331Samw smb_get_status(void)
13805331Samw {
13815331Samw 	char *state = NULL;
13825331Samw 	state = smf_get_state(SMBD_DEFAULT_INSTANCE_FMRI);
13835331Samw 	return (state != NULL ? state : "-");
13845331Samw }
13855331Samw 
13865331Samw /*
13875331Samw  * This protocol plugin require resource names
13885331Samw  */
13895331Samw static uint64_t
13905331Samw smb_share_features(void)
13915331Samw {
13925331Samw 	return (SA_FEATURE_RESOURCE | SA_FEATURE_ALLOWSUBDIRS |
13936088Sdougm 	    SA_FEATURE_ALLOWPARDIRS | SA_FEATURE_SERVER);
13945331Samw }
13955331Samw 
13965331Samw /*
13977348SJose.Borrego@Sun.COM  * This should be used to convert smb_share_t to sa_resource_t
13987348SJose.Borrego@Sun.COM  * Should only be needed to build transient shares/resources to be
13997348SJose.Borrego@Sun.COM  * supplied to sharemgr to display.
14005331Samw  */
14015331Samw static int
14027348SJose.Borrego@Sun.COM smb_add_transient(sa_handle_t handle, smb_share_t *si)
14035331Samw {
14045331Samw 	int err;
14055331Samw 	sa_share_t share;
14065331Samw 	sa_group_t group;
14075331Samw 	sa_resource_t resource;
14085331Samw 
14095331Samw 	if (si == NULL)
14105331Samw 		return (SA_INVALID_NAME);
14115331Samw 
14127348SJose.Borrego@Sun.COM 	if ((share = sa_find_share(handle, si->shr_path)) == NULL) {
14137348SJose.Borrego@Sun.COM 		if ((group = smb_get_defaultgrp(handle)) == NULL)
14147348SJose.Borrego@Sun.COM 			return (SA_NO_SUCH_GROUP);
14155331Samw 
14167052Samw 		share = sa_get_share(group, si->shr_path);
14175331Samw 		if (share == NULL) {
14187052Samw 			share = sa_add_share(group, si->shr_path,
14195331Samw 			    SA_SHARE_TRANSIENT, &err);
14205331Samw 			if (share == NULL)
14215331Samw 				return (SA_NO_SUCH_PATH);
14225331Samw 		}
14235331Samw 	}
14245331Samw 
14255331Samw 	/*
14265331Samw 	 * Now handle the resource. Make sure that the resource is
14275331Samw 	 * transient and added to the share.
14285331Samw 	 */
14297052Samw 	resource = sa_get_share_resource(share, si->shr_name);
14305331Samw 	if (resource == NULL) {
14315331Samw 		resource = sa_add_resource(share,
14327052Samw 		    si->shr_name, SA_SHARE_TRANSIENT, &err);
14335331Samw 		if (resource == NULL)
14345331Samw 			return (SA_NO_SUCH_RESOURCE);
14355331Samw 	}
14365331Samw 
14375331Samw 	/* set resource attributes now */
14387052Samw 	(void) sa_set_resource_attr(resource, "description", si->shr_cmnt);
14397052Samw 	(void) sa_set_resource_attr(resource, SMB_SHROPT_AD_CONTAINER,
14407052Samw 	    si->shr_container);
14415331Samw 
14425331Samw 	return (SA_OK);
14435331Samw }
14445331Samw 
14455331Samw /*
14467348SJose.Borrego@Sun.COM  * Return smb transient shares.
14475331Samw  */
14485331Samw static int
14495331Samw smb_list_transient(sa_handle_t handle)
14505331Samw {
14517348SJose.Borrego@Sun.COM 	int i, offset;
14527052Samw 	smb_shrlist_t list;
14535331Samw 	int res;
14545331Samw 
14557348SJose.Borrego@Sun.COM 	if (smb_share_count() <= 0)
14565331Samw 		return (SA_OK);
14577348SJose.Borrego@Sun.COM 
14585331Samw 	offset = 0;
14597348SJose.Borrego@Sun.COM 	while (smb_share_list(offset, &list) == NERR_Success) {
14607348SJose.Borrego@Sun.COM 		if (list.sl_cnt == 0)
14615331Samw 			break;
14627348SJose.Borrego@Sun.COM 
14637348SJose.Borrego@Sun.COM 		for (i = 0; i < list.sl_cnt; i++) {
14647348SJose.Borrego@Sun.COM 			res = smb_add_transient(handle, &(list.sl_shares[i]));
14655331Samw 			if (res != SA_OK)
14665331Samw 				return (res);
14675331Samw 		}
14687348SJose.Borrego@Sun.COM 		offset += list.sl_cnt;
14695331Samw 	}
14705331Samw 
14715331Samw 	return (SA_OK);
14725331Samw }
14735331Samw 
14745331Samw /*
14755331Samw  * fix_resource_name(share, name,  prefix)
14765331Samw  *
14775331Samw  * Construct a name where the ZFS dataset has the prefix replaced with "name".
14785331Samw  */
14795331Samw static char *
14805331Samw fix_resource_name(sa_share_t share, char *name, char *prefix)
14815331Samw {
14825331Samw 	char *dataset = NULL;
14835331Samw 	char *newname = NULL;
14845331Samw 	size_t psize;
14855331Samw 	size_t nsize;
14865331Samw 
14875331Samw 	dataset = sa_get_share_attr(share, "dataset");
14885331Samw 
14895331Samw 	if (dataset != NULL && strcmp(dataset, prefix) != 0) {
14905331Samw 		psize = strlen(prefix);
14915331Samw 		if (strncmp(dataset, prefix, psize) == 0) {
14925331Samw 			/* need string plus ',' and NULL */
14935331Samw 			nsize = (strlen(dataset) - psize) + strlen(name) + 2;
14945331Samw 			newname = calloc(nsize, 1);
14955331Samw 			if (newname != NULL) {
14965331Samw 				(void) snprintf(newname, nsize, "%s%s", name,
14975331Samw 				    dataset + psize);
14985331Samw 				sa_fix_resource_name(newname);
14995331Samw 			}
15005331Samw 			sa_free_attr_string(dataset);
15015331Samw 			return (newname);
15025331Samw 		}
15035331Samw 	}
15045331Samw 	if (dataset != NULL)
15055331Samw 		sa_free_attr_string(dataset);
15065331Samw 	return (strdup(name));
15075331Samw }
15085331Samw 
15095331Samw /*
15105331Samw  * smb_parse_optstring(group, options)
15115331Samw  *
15125331Samw  * parse a compact option string into individual options. This allows
15135331Samw  * ZFS sharesmb and sharemgr "share" command to work.  group can be a
15145331Samw  * group, a share or a resource.
15155331Samw  */
15165331Samw static int
15175331Samw smb_parse_optstring(sa_group_t group, char *options)
15185331Samw {
15195331Samw 	char *dup;
15205331Samw 	char *base;
15215331Samw 	char *lasts;
15225331Samw 	char *token;
15235331Samw 	sa_optionset_t optionset;
15245331Samw 	sa_group_t parent = NULL;
15255331Samw 	sa_resource_t resource = NULL;
15265331Samw 	int iszfs = 0;
15275331Samw 	int persist = 0;
15285331Samw 	int need_optionset = 0;
15295331Samw 	int ret = SA_OK;
15305331Samw 	sa_property_t prop;
15315331Samw 
15325331Samw 	/*
15335331Samw 	 * In order to not attempt to change ZFS properties unless
15345331Samw 	 * absolutely necessary, we never do it in the legacy parsing
15355331Samw 	 * so we need to keep track of this.
15365331Samw 	 */
15375331Samw 	if (sa_is_share(group)) {
15385331Samw 		char *zfs;
15395331Samw 
15405331Samw 		parent = sa_get_parent_group(group);
15415331Samw 		if (parent != NULL) {
15425331Samw 			zfs = sa_get_group_attr(parent, "zfs");
15435331Samw 			if (zfs != NULL) {
15445331Samw 				sa_free_attr_string(zfs);
15455331Samw 				iszfs = 1;
15465331Samw 			}
15475331Samw 		}
15485331Samw 	} else {
15495331Samw 		iszfs = sa_group_is_zfs(group);
15505331Samw 		/*
15515331Samw 		 * If a ZFS group, then we need to see if a resource
15525331Samw 		 * name is being set. If so, bail with
15535331Samw 		 * SA_PROP_SHARE_ONLY, so we come back in with a share
15545331Samw 		 * instead of a group.
15555331Samw 		 */
15565331Samw 		if (strncmp(options, "name=", sizeof ("name=") - 1) == 0 ||
15575331Samw 		    strstr(options, ",name=") != NULL) {
15585331Samw 			return (SA_PROP_SHARE_ONLY);
15595331Samw 		}
15605331Samw 	}
15615331Samw 
15625331Samw 	/* do we have an existing optionset? */
15635331Samw 	optionset = sa_get_optionset(group, "smb");
15645331Samw 	if (optionset == NULL) {
15655331Samw 		/* didn't find existing optionset so create one */
15665331Samw 		optionset = sa_create_optionset(group, "smb");
15675331Samw 		if (optionset == NULL)
15685331Samw 			return (SA_NO_MEMORY);
15695331Samw 	} else {
15705331Samw 		/*
15715331Samw 		 * If an optionset already exists, we've come through
15725331Samw 		 * twice so ignore the second time.
15735331Samw 		 */
15745331Samw 		return (ret);
15755331Samw 	}
15765331Samw 
15775331Samw 	/* We need a copy of options for the next part. */
15785331Samw 	dup = strdup(options);
15795331Samw 	if (dup == NULL)
15805331Samw 		return (SA_NO_MEMORY);
15815331Samw 
15825331Samw 	/*
15835331Samw 	 * SMB properties are straightforward and are strings,
15845331Samw 	 * integers or booleans.  Properties are separated by
15855331Samw 	 * commas. It will be necessary to parse quotes due to some
15865331Samw 	 * strings not having a restricted characters set.
15875331Samw 	 *
15885331Samw 	 * Note that names will create a resource. For now, if there
15895331Samw 	 * is a set of properties "before" the first name="", those
15905331Samw 	 * properties will be placed on the group.
15915331Samw 	 */
15925331Samw 	persist = sa_is_persistent(group);
15935331Samw 	base = dup;
15945331Samw 	token = dup;
15955331Samw 	lasts = NULL;
15965331Samw 	while (token != NULL && ret == SA_OK) {
15975331Samw 		ret = SA_OK;
15985331Samw 		token = strtok_r(base, ",", &lasts);
15995331Samw 		base = NULL;
16005331Samw 		if (token != NULL) {
16015331Samw 			char *value;
16025331Samw 			/*
16035331Samw 			 * All SMB properties have values so there
16045331Samw 			 * MUST be an '=' character.  If it doesn't,
16055331Samw 			 * it is a syntax error.
16065331Samw 			 */
16075331Samw 			value = strchr(token, '=');
16085331Samw 			if (value != NULL) {
16095331Samw 				*value++ = '\0';
16105331Samw 			} else {
16115331Samw 				ret = SA_SYNTAX_ERR;
16125331Samw 				break;
16135331Samw 			}
16145331Samw 			/*
16155331Samw 			 * We may need to handle a "name" property
16165331Samw 			 * that is a ZFS imposed resource name. Each
16175331Samw 			 * name would trigger getting a new "resource"
16185331Samw 			 * to put properties on. For now, assume no
16195331Samw 			 * "name" property for special handling.
16205331Samw 			 */
16215331Samw 
16225331Samw 			if (strcmp(token, "name") == 0) {
16235331Samw 				char *prefix;
16245331Samw 				char *name = NULL;
16255331Samw 				/*
16265331Samw 				 * We have a name, so now work on the
16275331Samw 				 * resource level. We have a "share"
16285331Samw 				 * in "group" due to the caller having
16295331Samw 				 * added it. If we are called with a
16305331Samw 				 * group, the check for group/share
16315331Samw 				 * at the beginning of this function
16325331Samw 				 * will bail out the parse if there is a
16335331Samw 				 * "name" but no share.
16345331Samw 				 */
16355331Samw 				if (!iszfs) {
16365331Samw 					ret = SA_SYNTAX_ERR;
16375331Samw 					break;
16385331Samw 				}
16395331Samw 				/*
16405331Samw 				 * Make sure the parent group has the
16415331Samw 				 * "prefix" property since we will
16425331Samw 				 * need to use this for constructing
16435331Samw 				 * inherited name= values.
16445331Samw 				 */
16455331Samw 				prefix = sa_get_group_attr(parent, "prefix");
16465331Samw 				if (prefix == NULL) {
16475331Samw 					prefix = sa_get_group_attr(parent,
16485331Samw 					    "name");
16495331Samw 					if (prefix != NULL) {
16505331Samw 						(void) sa_set_group_attr(parent,
16515331Samw 						    "prefix", prefix);
16525331Samw 					}
16535331Samw 				}
16545331Samw 				name = fix_resource_name((sa_share_t)group,
16555331Samw 				    value, prefix);
16565331Samw 				if (name != NULL) {
16575331Samw 					resource = sa_add_resource(
16585331Samw 					    (sa_share_t)group, name,
16595331Samw 					    SA_SHARE_TRANSIENT, &ret);
16605331Samw 					sa_free_attr_string(name);
16615331Samw 				} else {
16625331Samw 					ret = SA_NO_MEMORY;
16635331Samw 				}
16645331Samw 				if (prefix != NULL)
16655331Samw 					sa_free_attr_string(prefix);
16665331Samw 
16675331Samw 				/* A resource level optionset is needed */
16685331Samw 
16695331Samw 				need_optionset = 1;
16705331Samw 				if (resource == NULL) {
16715331Samw 					ret = SA_NO_MEMORY;
16725331Samw 					break;
16735331Samw 				}
16745331Samw 				continue;
16755331Samw 			}
16765331Samw 
16775331Samw 			if (need_optionset) {
16785331Samw 				optionset = sa_create_optionset(resource,
16795331Samw 				    "smb");
16805331Samw 				need_optionset = 0;
16815331Samw 			}
16825331Samw 
16835331Samw 			prop = sa_create_property(token, value);
16845331Samw 			if (prop == NULL)
16855331Samw 				ret = SA_NO_MEMORY;
16865331Samw 			else
16875331Samw 				ret = sa_add_property(optionset, prop);
16885331Samw 			if (ret != SA_OK)
16895331Samw 				break;
16905331Samw 			if (!iszfs)
16915331Samw 				ret = sa_commit_properties(optionset, !persist);
16925331Samw 		}
16935331Samw 	}
16945331Samw 	free(dup);
16955331Samw 	return (ret);
16965331Samw }
16975331Samw 
16985331Samw /*
16995331Samw  * smb_sprint_option(rbuff, rbuffsize, incr, prop, sep)
17005331Samw  *
17015331Samw  * provides a mechanism to format SMB properties into legacy output
17025331Samw  * format. If the buffer would overflow, it is reallocated and grown
17035331Samw  * as appropriate. Special cases of converting internal form of values
17045331Samw  * to those used by "share" are done. this function does one property
17055331Samw  * at a time.
17065331Samw  */
17075331Samw 
17085331Samw static void
17095331Samw smb_sprint_option(char **rbuff, size_t *rbuffsize, size_t incr,
17105331Samw 			sa_property_t prop, int sep)
17115331Samw {
17125331Samw 	char *name;
17135331Samw 	char *value;
17145331Samw 	int curlen;
17155331Samw 	char *buff = *rbuff;
17165331Samw 	size_t buffsize = *rbuffsize;
17175331Samw 
17185331Samw 	name = sa_get_property_attr(prop, "type");
17195331Samw 	value = sa_get_property_attr(prop, "value");
17205331Samw 	if (buff != NULL)
17215331Samw 		curlen = strlen(buff);
17225331Samw 	else
17235331Samw 		curlen = 0;
17245331Samw 	if (name != NULL) {
17255331Samw 		int len;
17265331Samw 		len = strlen(name) + sep;
17275331Samw 
17285331Samw 		/*
17295331Samw 		 * A future RFE would be to replace this with more
17305331Samw 		 * generic code and to possibly handle more types.
17315331Samw 		 *
17325331Samw 		 * For now, everything else is treated as a string. If
17335331Samw 		 * we get any properties that aren't exactly
17345331Samw 		 * name/value pairs, we may need to
17355331Samw 		 * interpret/transform.
17365331Samw 		 */
17375331Samw 		if (value != NULL)
17385331Samw 			len += 1 + strlen(value);
17395331Samw 
17405331Samw 		while (buffsize <= (curlen + len)) {
17415331Samw 			/* need more room */
17425331Samw 			buffsize += incr;
17435331Samw 			buff = realloc(buff, buffsize);
17445331Samw 			*rbuff = buff;
17455331Samw 			*rbuffsize = buffsize;
17465331Samw 			if (buff == NULL) {
17475331Samw 				/* realloc failed so free everything */
17485331Samw 				if (*rbuff != NULL)
17495331Samw 					free(*rbuff);
17505331Samw 				goto err;
17515331Samw 			}
17525331Samw 		}
17535331Samw 		if (buff == NULL)
17545331Samw 			goto err;
17555331Samw 		(void) snprintf(buff + curlen, buffsize - curlen,
17565331Samw 		    "%s%s=%s", sep ? "," : "",
17575331Samw 		    name, value != NULL ? value : "\"\"");
17585331Samw 
17595331Samw 	}
17605331Samw err:
17615331Samw 	if (name != NULL)
17625331Samw 		sa_free_attr_string(name);
17635331Samw 	if (value != NULL)
17645331Samw 		sa_free_attr_string(value);
17655331Samw }
17665331Samw 
17675331Samw /*
17685331Samw  * smb_format_resource_options(resource, hier)
17695331Samw  *
17705331Samw  * format all the options on the group into a flattened option
17715331Samw  * string. If hier is non-zero, walk up the tree to get inherited
17725331Samw  * options.
17735331Samw  */
17745331Samw 
17755331Samw static char *
17765331Samw smb_format_options(sa_group_t group, int hier)
17775331Samw {
17785331Samw 	sa_optionset_t options = NULL;
17795331Samw 	sa_property_t prop;
17805331Samw 	int sep = 0;
17815331Samw 	char *buff;
17825331Samw 	size_t buffsize;
17835331Samw 
17845331Samw 
17855331Samw 	buff = malloc(OPT_CHUNK);
17865331Samw 	if (buff == NULL)
17875331Samw 		return (NULL);
17885331Samw 
17895331Samw 	buff[0] = '\0';
17905331Samw 	buffsize = OPT_CHUNK;
17915331Samw 
17925331Samw 	/*
17935331Samw 	 * We may have a an optionset relative to this item. format
17945331Samw 	 * these if we find them and then add any security definitions.
17955331Samw 	 */
17965331Samw 
17975331Samw 	options = sa_get_derived_optionset(group, "smb", hier);
17985331Samw 
17995331Samw 	/*
18005331Samw 	 * do the default set first but skip any option that is also
18015331Samw 	 * in the protocol specific optionset.
18025331Samw 	 */
18035331Samw 	if (options != NULL) {
18045331Samw 		for (prop = sa_get_property(options, NULL);
18055331Samw 		    prop != NULL; prop = sa_get_next_property(prop)) {
18065331Samw 			/*
18075331Samw 			 * use this one since we skipped any
18085331Samw 			 * of these that were also in
18095331Samw 			 * optdefault
18105331Samw 			 */
18115331Samw 			smb_sprint_option(&buff, &buffsize, OPT_CHUNK,
18125331Samw 			    prop, sep);
18135331Samw 			if (buff == NULL) {
18145331Samw 				/*
18155331Samw 				 * buff could become NULL if there
18165331Samw 				 * isn't enough memory for
18175331Samw 				 * smb_sprint_option to realloc()
18185331Samw 				 * as necessary. We can't really
18195331Samw 				 * do anything about it at this
18205331Samw 				 * point so we return NULL.  The
18215331Samw 				 * caller should handle the
18225331Samw 				 * failure.
18235331Samw 				 */
18245331Samw 				if (options != NULL)
18255331Samw 					sa_free_derived_optionset(
18265331Samw 					    options);
18275331Samw 				return (buff);
18285331Samw 			}
18295331Samw 			sep = 1;
18305331Samw 		}
18315331Samw 	}
18325331Samw 
18335331Samw 	if (options != NULL)
18345331Samw 		sa_free_derived_optionset(options);
18355331Samw 	return (buff);
18365331Samw }
18375331Samw 
18385331Samw /*
18395331Samw  * smb_rename_resource(resource, newname)
18405331Samw  *
18415331Samw  * Change the current exported name of the resource to newname.
18425331Samw  */
18435331Samw /*ARGSUSED*/
18445331Samw int
18455331Samw smb_rename_resource(sa_handle_t handle, sa_resource_t resource, char *newname)
18465331Samw {
18475331Samw 	int ret = SA_OK;
18485331Samw 	int err;
18495331Samw 	char *oldname;
18505331Samw 
18517348SJose.Borrego@Sun.COM 	if (!smb_isonline())
18527348SJose.Borrego@Sun.COM 		return (SA_OK);
18537348SJose.Borrego@Sun.COM 
18545331Samw 	oldname = sa_get_resource_attr(resource, "name");
18555331Samw 	if (oldname == NULL)
18565331Samw 		return (SA_NO_SUCH_RESOURCE);
18575331Samw 
18587348SJose.Borrego@Sun.COM 	err = smb_share_rename(oldname, newname);
18595331Samw 
18605331Samw 	/* improve error values somewhat */
18615331Samw 	switch (err) {
18625331Samw 	case NERR_Success:
18635331Samw 		break;
18645331Samw 	case NERR_InternalError:
18655331Samw 		ret = SA_SYSTEM_ERR;
18665331Samw 		break;
18675331Samw 	case NERR_DuplicateShare:
18685331Samw 		ret = SA_DUPLICATE_NAME;
18695331Samw 		break;
18705331Samw 	default:
18715331Samw 		ret = SA_CONFIG_ERR;
18725331Samw 		break;
18735331Samw 	}
18745331Samw 
18755331Samw 	return (ret);
18765331Samw }
18777348SJose.Borrego@Sun.COM 
18787348SJose.Borrego@Sun.COM static int
18797348SJose.Borrego@Sun.COM smb_build_shareinfo(sa_share_t share, sa_resource_t resource, smb_share_t *si)
18807348SJose.Borrego@Sun.COM {
18817348SJose.Borrego@Sun.COM 	sa_property_t prop;
18827348SJose.Borrego@Sun.COM 	sa_optionset_t opts;
18837348SJose.Borrego@Sun.COM 	char *path;
18847348SJose.Borrego@Sun.COM 	char *rname;
18857348SJose.Borrego@Sun.COM 	char *val = NULL;
18867348SJose.Borrego@Sun.COM 
18877348SJose.Borrego@Sun.COM 	bzero(si, sizeof (smb_share_t));
18887348SJose.Borrego@Sun.COM 
18897348SJose.Borrego@Sun.COM 	if ((path = sa_get_share_attr(share, "path")) == NULL)
18907348SJose.Borrego@Sun.COM 		return (SA_NO_SUCH_PATH);
18917348SJose.Borrego@Sun.COM 
18927348SJose.Borrego@Sun.COM 	if ((rname = sa_get_resource_attr(resource, "name")) == NULL) {
18937348SJose.Borrego@Sun.COM 		sa_free_attr_string(path);
18947348SJose.Borrego@Sun.COM 		return (SA_NO_SUCH_RESOURCE);
18957348SJose.Borrego@Sun.COM 	}
18967348SJose.Borrego@Sun.COM 
18977348SJose.Borrego@Sun.COM 	si->shr_flags = (sa_is_persistent(share))
18987348SJose.Borrego@Sun.COM 	    ? SMB_SHRF_PERM : SMB_SHRF_TRANS;
18997348SJose.Borrego@Sun.COM 
19007348SJose.Borrego@Sun.COM 	(void) strlcpy(si->shr_path, path, sizeof (si->shr_path));
19017348SJose.Borrego@Sun.COM 	(void) strlcpy(si->shr_name, rname, sizeof (si->shr_name));
19027348SJose.Borrego@Sun.COM 	sa_free_attr_string(path);
19037348SJose.Borrego@Sun.COM 	sa_free_attr_string(rname);
19047348SJose.Borrego@Sun.COM 
19057348SJose.Borrego@Sun.COM 	val = sa_get_resource_description(resource);
19067348SJose.Borrego@Sun.COM 	if (val == NULL)
19077348SJose.Borrego@Sun.COM 		val = sa_get_share_description(share);
19087348SJose.Borrego@Sun.COM 
19097348SJose.Borrego@Sun.COM 	if (val != NULL) {
19107348SJose.Borrego@Sun.COM 		(void) strlcpy(si->shr_cmnt, val, sizeof (si->shr_cmnt));
19117348SJose.Borrego@Sun.COM 		sa_free_share_description(val);
19127348SJose.Borrego@Sun.COM 	}
19137348SJose.Borrego@Sun.COM 
19147348SJose.Borrego@Sun.COM 	opts = sa_get_derived_optionset(resource, SMB_PROTOCOL_NAME, 1);
19157348SJose.Borrego@Sun.COM 	if (opts == NULL)
19167348SJose.Borrego@Sun.COM 		return (SA_OK);
19177348SJose.Borrego@Sun.COM 
19187348SJose.Borrego@Sun.COM 	prop = sa_get_property(opts, SMB_SHROPT_AD_CONTAINER);
19197348SJose.Borrego@Sun.COM 	if (prop != NULL) {
19207348SJose.Borrego@Sun.COM 		if ((val = sa_get_property_attr(prop, "value")) != NULL) {
19217348SJose.Borrego@Sun.COM 			(void) strlcpy(si->shr_container, val,
19227348SJose.Borrego@Sun.COM 			    sizeof (si->shr_container));
19237348SJose.Borrego@Sun.COM 			free(val);
19247348SJose.Borrego@Sun.COM 		}
19257348SJose.Borrego@Sun.COM 	}
19267348SJose.Borrego@Sun.COM 
1927*7961SNatalie.Li@Sun.COM 	prop = sa_get_property(opts, SHOPT_RO);
1928*7961SNatalie.Li@Sun.COM 	if (prop != NULL) {
1929*7961SNatalie.Li@Sun.COM 		if ((val = sa_get_property_attr(prop, "value")) != NULL) {
1930*7961SNatalie.Li@Sun.COM 			(void) strlcpy(si->shr_access_ro, val,
1931*7961SNatalie.Li@Sun.COM 			    sizeof (si->shr_access_ro));
1932*7961SNatalie.Li@Sun.COM 			free(val);
1933*7961SNatalie.Li@Sun.COM 			si->shr_flags |= SMB_SHRF_ACC_RO;
1934*7961SNatalie.Li@Sun.COM 		}
1935*7961SNatalie.Li@Sun.COM 	}
1936*7961SNatalie.Li@Sun.COM 
1937*7961SNatalie.Li@Sun.COM 	prop = sa_get_property(opts, SHOPT_RW);
1938*7961SNatalie.Li@Sun.COM 	if (prop != NULL) {
1939*7961SNatalie.Li@Sun.COM 		if ((val = sa_get_property_attr(prop, "value")) != NULL) {
1940*7961SNatalie.Li@Sun.COM 			(void) strlcpy(si->shr_access_rw, val,
1941*7961SNatalie.Li@Sun.COM 			    sizeof (si->shr_access_rw));
1942*7961SNatalie.Li@Sun.COM 			free(val);
1943*7961SNatalie.Li@Sun.COM 			si->shr_flags |= SMB_SHRF_ACC_RW;
1944*7961SNatalie.Li@Sun.COM 		}
1945*7961SNatalie.Li@Sun.COM 	}
1946*7961SNatalie.Li@Sun.COM 
1947*7961SNatalie.Li@Sun.COM 	prop = sa_get_property(opts, SHOPT_NONE);
1948*7961SNatalie.Li@Sun.COM 	if (prop != NULL) {
1949*7961SNatalie.Li@Sun.COM 		if ((val = sa_get_property_attr(prop, "value")) != NULL) {
1950*7961SNatalie.Li@Sun.COM 			(void) strlcpy(si->shr_access_none, val,
1951*7961SNatalie.Li@Sun.COM 			    sizeof (si->shr_access_none));
1952*7961SNatalie.Li@Sun.COM 			free(val);
1953*7961SNatalie.Li@Sun.COM 			si->shr_flags |= SMB_SHRF_ACC_NONE;
1954*7961SNatalie.Li@Sun.COM 		}
1955*7961SNatalie.Li@Sun.COM 	}
1956*7961SNatalie.Li@Sun.COM 
19577348SJose.Borrego@Sun.COM 	sa_free_derived_optionset(opts);
19587348SJose.Borrego@Sun.COM 	return (SA_OK);
19597348SJose.Borrego@Sun.COM }
19607348SJose.Borrego@Sun.COM 
19617348SJose.Borrego@Sun.COM /*
19627348SJose.Borrego@Sun.COM  * smb_get_defaultgrp
19637348SJose.Borrego@Sun.COM  *
19647348SJose.Borrego@Sun.COM  * If default group for CIFS shares (i.e. "smb") exists
19657348SJose.Borrego@Sun.COM  * then it will return the group handle, otherwise it will
19667348SJose.Borrego@Sun.COM  * create the group and return the handle.
19677348SJose.Borrego@Sun.COM  *
19687348SJose.Borrego@Sun.COM  * All the shares created by CIFS clients (this is only possible
19697348SJose.Borrego@Sun.COM  * via RPC) will be added to "smb" groups.
19707348SJose.Borrego@Sun.COM  */
19717348SJose.Borrego@Sun.COM static sa_group_t
19727348SJose.Borrego@Sun.COM smb_get_defaultgrp(sa_handle_t handle)
19737348SJose.Borrego@Sun.COM {
19747348SJose.Borrego@Sun.COM 	sa_group_t group = NULL;
19757348SJose.Borrego@Sun.COM 	int err;
19767348SJose.Borrego@Sun.COM 
19777348SJose.Borrego@Sun.COM 	group = sa_get_group(handle, SMB_DEFAULT_SHARE_GROUP);
19787348SJose.Borrego@Sun.COM 	if (group != NULL)
19797348SJose.Borrego@Sun.COM 		return (group);
19807348SJose.Borrego@Sun.COM 
19817348SJose.Borrego@Sun.COM 	group = sa_create_group(handle, SMB_DEFAULT_SHARE_GROUP, &err);
19827348SJose.Borrego@Sun.COM 	if (group == NULL)
19837348SJose.Borrego@Sun.COM 		return (NULL);
19847348SJose.Borrego@Sun.COM 
19857348SJose.Borrego@Sun.COM 	if (sa_create_optionset(group, SMB_DEFAULT_SHARE_GROUP) == NULL) {
19867348SJose.Borrego@Sun.COM 		(void) sa_remove_group(group);
19877348SJose.Borrego@Sun.COM 		group = NULL;
19887348SJose.Borrego@Sun.COM 	}
19897348SJose.Borrego@Sun.COM 
19907348SJose.Borrego@Sun.COM 	return (group);
19917348SJose.Borrego@Sun.COM }
1992