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},
1465331Samw 	{NULL, NULL},
1475331Samw };
1485331Samw 
1495331Samw /*
1505331Samw  * findopt(name)
1515331Samw  *
1525331Samw  * Lookup option "name" in the option table and return the table
1535331Samw  * index.
1545331Samw  */
1555331Samw static int
1565331Samw findopt(char *name)
1575331Samw {
1585331Samw 	int i;
1595331Samw 	if (name != NULL) {
1605331Samw 		for (i = 0; optdefs[i].tag != NULL; i++) {
1615331Samw 			if (strcmp(optdefs[i].tag, name) == 0)
1625331Samw 				return (i);
1635331Samw 		}
1645331Samw 	}
1655331Samw 	return (-1);
1665331Samw }
1675331Samw 
1685331Samw /*
1695331Samw  * is_a_number(number)
1705331Samw  *
1715331Samw  * is the string a number in one of the forms we want to use?
1725331Samw  */
1737348SJose.Borrego@Sun.COM static boolean_t
1745331Samw is_a_number(char *number)
1755331Samw {
1767348SJose.Borrego@Sun.COM 	boolean_t isnum = B_TRUE;
1777348SJose.Borrego@Sun.COM 	boolean_t ishex = B_FALSE;
1787348SJose.Borrego@Sun.COM 
1797348SJose.Borrego@Sun.COM 	if (number == NULL || *number == '\0')
1807348SJose.Borrego@Sun.COM 		return (B_FALSE);
1815331Samw 
1827348SJose.Borrego@Sun.COM 	if (strncasecmp(number, "0x", 2) == 0) {
1835331Samw 		number += 2;
1847348SJose.Borrego@Sun.COM 		ishex = B_TRUE;
1855331Samw 	} else if (*number == '-') {
1867348SJose.Borrego@Sun.COM 		number++;
1875331Samw 	}
1885331Samw 
1897348SJose.Borrego@Sun.COM 	while (isnum && (*number != '\0')) {
1907348SJose.Borrego@Sun.COM 		isnum = (ishex) ? isxdigit(*number) : isdigit(*number);
1917348SJose.Borrego@Sun.COM 		number++;
1925331Samw 	}
1937348SJose.Borrego@Sun.COM 
1947348SJose.Borrego@Sun.COM 	return (isnum);
1955331Samw }
1965331Samw 
1975331Samw /*
1985331Samw  * validresource(name)
1995331Samw  *
2005331Samw  * Check that name only has valid characters in it. The current valid
2015331Samw  * set are the printable characters but not including:
2025331Samw  *	" / \ [ ] : | < > + ; , ? * = \t
2035331Samw  * Note that space is included and there is a maximum length.
2045331Samw  */
2057348SJose.Borrego@Sun.COM static boolean_t
2065331Samw validresource(const char *name)
2075331Samw {
2085331Samw 	const char *cp;
2095331Samw 	size_t len;
2105331Samw 
2115331Samw 	if (name == NULL)
2125331Samw 		return (B_FALSE);
2135331Samw 
2145331Samw 	len = strlen(name);
2155331Samw 	if (len == 0 || len > SA_MAX_RESOURCE_NAME)
2165331Samw 		return (B_FALSE);
2175331Samw 
2185331Samw 	if (strpbrk(name, "\"/\\[]:|<>+;,?*=\t") != NULL) {
2195331Samw 		return (B_FALSE);
2205331Samw 	}
2215331Samw 
2225331Samw 	for (cp = name; *cp != '\0'; cp++)
2235331Samw 		if (iscntrl(*cp))
2245331Samw 			return (B_FALSE);
2255331Samw 
2265331Samw 	return (B_TRUE);
2275331Samw }
2285331Samw 
2295331Samw /*
2305331Samw  * smb_isonline()
2315331Samw  *
2325331Samw  * Determine if the SMF service instance is in the online state or
2335331Samw  * not. A number of operations depend on this state.
2345331Samw  */
2355331Samw static boolean_t
2365331Samw smb_isonline(void)
2375331Samw {
2385331Samw 	char *str;
2395331Samw 	boolean_t ret = B_FALSE;
2405331Samw 
2415331Samw 	if ((str = smf_get_state(SMBD_DEFAULT_INSTANCE_FMRI)) != NULL) {
2425331Samw 		ret = (strcmp(str, SCF_STATE_STRING_ONLINE) == 0);
2435331Samw 		free(str);
2445331Samw 	}
2455331Samw 	return (ret);
2465331Samw }
2475331Samw 
2485331Samw /*
2495772Sas200622  * smb_isdisabled()
2505772Sas200622  *
2515772Sas200622  * Determine if the SMF service instance is in the disabled state or
2525772Sas200622  * not. A number of operations depend on this state.
2535772Sas200622  */
2545772Sas200622 static boolean_t
2555772Sas200622 smb_isdisabled(void)
2565772Sas200622 {
2575772Sas200622 	char *str;
2585772Sas200622 	boolean_t ret = B_FALSE;
2595772Sas200622 
2605772Sas200622 	if ((str = smf_get_state(SMBD_DEFAULT_INSTANCE_FMRI)) != NULL) {
2615772Sas200622 		ret = (strcmp(str, SCF_STATE_STRING_DISABLED) == 0);
2625772Sas200622 		free(str);
2635772Sas200622 	}
2645772Sas200622 	return (ret);
2655772Sas200622 }
2665772Sas200622 
2675772Sas200622 /*
2685772Sas200622  * smb_isautoenable()
2695772Sas200622  *
2705772Sas200622  * Determine if the SMF service instance auto_enabled set or not. A
2715772Sas200622  * number of operations depend on this state.  The property not being
2725772Sas200622  * set or being set to true means autoenable.  Only being set to false
2735772Sas200622  * is not autoenabled.
2745772Sas200622  */
2755772Sas200622 static boolean_t
2765772Sas200622 smb_isautoenable(void)
2775772Sas200622 {
2785772Sas200622 	boolean_t ret = B_TRUE;
2795772Sas200622 	scf_simple_prop_t *prop;
2805772Sas200622 	uint8_t *retstr;
2815772Sas200622 
2825772Sas200622 	prop = scf_simple_prop_get(NULL, SMBD_DEFAULT_INSTANCE_FMRI,
2835772Sas200622 	    "application", "auto_enable");
2845772Sas200622 	if (prop != NULL) {
2855772Sas200622 		retstr = scf_simple_prop_next_boolean(prop);
2865772Sas200622 		ret = *retstr != 0;
2875772Sas200622 		scf_simple_prop_free(prop);
2885772Sas200622 	}
2895772Sas200622 	return (ret);
2905772Sas200622 }
2915772Sas200622 
2925772Sas200622 /*
2936030Sjb150015  * smb_ismaint()
2946030Sjb150015  *
2956030Sjb150015  * Determine if the SMF service instance is in the disabled state or
2966030Sjb150015  * not. A number of operations depend on this state.
2976030Sjb150015  */
2986030Sjb150015 static boolean_t
2996030Sjb150015 smb_ismaint(void)
3006030Sjb150015 {
3016030Sjb150015 	char *str;
3026030Sjb150015 	boolean_t ret = B_FALSE;
3036030Sjb150015 
3046030Sjb150015 	if ((str = smf_get_state(SMBD_DEFAULT_INSTANCE_FMRI)) != NULL) {
3056030Sjb150015 		ret = (strcmp(str, SCF_STATE_STRING_MAINT) == 0);
3066030Sjb150015 		free(str);
3076030Sjb150015 	}
3086030Sjb150015 	return (ret);
3096030Sjb150015 }
3106030Sjb150015 
3116030Sjb150015 /*
3125331Samw  * smb_enable_share tells the implementation that it is to enable the share.
3135331Samw  * This entails converting the path and options into the appropriate ioctl
3145331Samw  * calls. It is assumed that all error checking of paths, etc. were
3155331Samw  * done earlier.
3165331Samw  */
3175331Samw static int
3185331Samw smb_enable_share(sa_share_t share)
3195331Samw {
3205331Samw 	char *path;
3217052Samw 	smb_share_t si;
3225331Samw 	sa_resource_t resource;
3235331Samw 	boolean_t iszfs;
3245331Samw 	boolean_t privileged;
3255331Samw 	int err = SA_OK;
3265331Samw 	priv_set_t *priv_effective;
3275331Samw 	boolean_t online;
3285331Samw 
3296270Sdougm 	/*
3306270Sdougm 	 * We only start in the global zone and only run if we aren't
3316270Sdougm 	 * running Trusted Extensions.
3326270Sdougm 	 */
3336270Sdougm 	if (getzoneid() != GLOBAL_ZONEID) {
3346270Sdougm 		(void) printf(dgettext(TEXT_DOMAIN,
3356270Sdougm 		    "SMB: service not supported in local zone\n"));
3366270Sdougm 		return (SA_NOT_SUPPORTED);
3376270Sdougm 	}
3386270Sdougm 	if (is_system_labeled()) {
3396270Sdougm 		(void) printf(dgettext(TEXT_DOMAIN,
3406270Sdougm 		    "SMB: service not supported with Trusted Extensions\n"));
3416270Sdougm 		return (SA_NOT_SUPPORTED);
3426270Sdougm 	}
3436270Sdougm 
3445331Samw 	priv_effective = priv_allocset();
3455331Samw 	(void) getppriv(PRIV_EFFECTIVE, priv_effective);
3465331Samw 	privileged = (priv_isfullset(priv_effective) == B_TRUE);
3475331Samw 	priv_freeset(priv_effective);
3485331Samw 
3495331Samw 	/* get the path since it is important in several places */
3505331Samw 	path = sa_get_share_attr(share, "path");
3515331Samw 	if (path == NULL)
3525331Samw 		return (SA_NO_SUCH_PATH);
3535331Samw 
3545772Sas200622 	/*
3555772Sas200622 	 * If administratively disabled, don't try to start anything.
3565772Sas200622 	 */
3575331Samw 	online = smb_isonline();
3585772Sas200622 	if (!online && !smb_isautoenable() && smb_isdisabled())
3595772Sas200622 		goto done;
3605331Samw 
3615331Samw 	iszfs = sa_path_is_zfs(path);
3625331Samw 
3635331Samw 	if (iszfs) {
3645331Samw 
3655331Samw 		if (privileged == B_FALSE && !online) {
3665331Samw 
3675331Samw 			if (!online) {
3685331Samw 				(void) printf(dgettext(TEXT_DOMAIN,
3695331Samw 				    "SMB: Cannot share remove "
3705331Samw 				    "file system: %s\n"), path);
3715331Samw 				(void) printf(dgettext(TEXT_DOMAIN,
3725331Samw 				    "SMB: Service needs to be enabled "
3735331Samw 				    "by a privileged user\n"));
3745331Samw 				err = SA_NO_PERMISSION;
3755331Samw 				errno = EPERM;
3765331Samw 			}
3775331Samw 			if (err) {
3785331Samw 				sa_free_attr_string(path);
3795331Samw 				return (err);
3805331Samw 			}
3815331Samw 
3825331Samw 		}
3835331Samw 	}
3845331Samw 
3855331Samw 	if (privileged == B_TRUE && !online) {
3865331Samw 		err = smb_enable_service();
3875331Samw 		if (err != SA_OK) {
3885331Samw 			(void) printf(dgettext(TEXT_DOMAIN,
3895331Samw 			    "SMB: Unable to enable service\n"));
3905331Samw 			/*
3915331Samw 			 * For now, it is OK to not be able to enable
3925331Samw 			 * the service.
3935331Samw 			 */
3946030Sjb150015 			if (err == SA_BUSY || err == SA_SYSTEM_ERR)
3955331Samw 				err = SA_OK;
3965331Samw 		} else {
3975331Samw 			online = B_TRUE;
3985331Samw 		}
3995331Samw 	}
4005331Samw 
4015331Samw 	/*
4025331Samw 	 * Don't bother trying to start shares if the service isn't
4035331Samw 	 * running.
4045331Samw 	 */
4055331Samw 	if (!online)
4065331Samw 		goto done;
4075331Samw 
4085331Samw 	/* Each share can have multiple resources */
4095331Samw 	for (resource = sa_get_share_resource(share, NULL);
4105331Samw 	    resource != NULL;
4115331Samw 	    resource = sa_get_next_resource(resource)) {
4127348SJose.Borrego@Sun.COM 		err = smb_build_shareinfo(share, resource, &si);
4137348SJose.Borrego@Sun.COM 		if (err != SA_OK) {
4145331Samw 			sa_free_attr_string(path);
4157348SJose.Borrego@Sun.COM 			return (err);
4165331Samw 		}
4175331Samw 
4185331Samw 		if (!iszfs) {
4197348SJose.Borrego@Sun.COM 			err = smb_share_create(&si);
4205331Samw 		} else {
4215331Samw 			share_t sh;
4225331Samw 
4235331Samw 			sa_sharetab_fill_zfs(share, &sh, "smb");
4245331Samw 			err = sa_share_zfs(share, (char *)path, &sh,
4255331Samw 			    &si, ZFS_SHARE_SMB);
4265331Samw 
4275331Samw 			sa_emptyshare(&sh);
4285331Samw 		}
4295331Samw 	}
4305331Samw 	if (!iszfs)
4315331Samw 		(void) sa_update_sharetab(share, "smb");
4325331Samw done:
4335331Samw 	sa_free_attr_string(path);
4345331Samw 
4355331Samw 	return (err == NERR_DuplicateShare ? 0 : err);
4365331Samw }
4375331Samw 
4385331Samw /*
4395331Samw  * This is the share for CIFS all shares have resource names.
4405331Samw  * Enable tells the smb server to update its hash. If it fails
4415331Samw  * because smb server is down, we just ignore as smb server loads
4425331Samw  * the resources from sharemanager at startup.
4435331Samw  */
4445331Samw 
4455331Samw static int
4465331Samw smb_enable_resource(sa_resource_t resource)
4475331Samw {
4485331Samw 	sa_share_t share;
4497052Samw 	smb_share_t si;
4505772Sas200622 	int ret = SA_OK;
4515772Sas200622 	int err;
4525772Sas200622 	boolean_t isonline;
4535331Samw 
4545331Samw 	share = sa_get_resource_parent(resource);
4555331Samw 	if (share == NULL)
4565331Samw 		return (SA_NO_SUCH_PATH);
4575772Sas200622 
4585772Sas200622 	/*
4595772Sas200622 	 * If administratively disabled, don't try to start anything.
4605772Sas200622 	 */
4615772Sas200622 	isonline = smb_isonline();
4625772Sas200622 	if (!isonline && !smb_isautoenable() && smb_isdisabled())
4637348SJose.Borrego@Sun.COM 		return (SA_OK);
4645772Sas200622 
4657348SJose.Borrego@Sun.COM 	if (!isonline) {
4667348SJose.Borrego@Sun.COM 		(void) smb_enable_service();
4677348SJose.Borrego@Sun.COM 
4687348SJose.Borrego@Sun.COM 		if (!smb_isonline())
4697348SJose.Borrego@Sun.COM 			return (SA_OK);
4705772Sas200622 	}
4715772Sas200622 
4727348SJose.Borrego@Sun.COM 	if ((ret = smb_build_shareinfo(share, resource, &si)) != SA_OK)
4737348SJose.Borrego@Sun.COM 		return (ret);
4745772Sas200622 
4755772Sas200622 	/*
4765772Sas200622 	 * Attempt to add the share. Any error that occurs if it was
4775772Sas200622 	 * online is an error but don't count NERR_DuplicateName if
4785772Sas200622 	 * smb/server had to be brought online since bringing the
4795772Sas200622 	 * service up will enable the share that was just added prior
4805772Sas200622 	 * to the attempt to enable.
4815772Sas200622 	 */
4827348SJose.Borrego@Sun.COM 	err = smb_share_create(&si);
4835772Sas200622 	if (err == NERR_Success || !(!isonline && err == NERR_DuplicateName))
4845772Sas200622 		(void) sa_update_sharetab(share, "smb");
4855772Sas200622 	else
4865331Samw 		return (SA_NOT_SHARED);
4875331Samw 
4887348SJose.Borrego@Sun.COM 	return (SA_OK);
4895331Samw }
4905331Samw 
4915331Samw /*
4925331Samw  * Remove it from smb server hash.
4935331Samw  */
4945331Samw static int
4955331Samw smb_disable_resource(sa_resource_t resource)
4965331Samw {
4975331Samw 	char *rname;
4987052Samw 	uint32_t res;
4995331Samw 	sa_share_t share;
5005331Samw 
5015331Samw 	rname = sa_get_resource_attr(resource, "name");
5025331Samw 	if (rname == NULL)
5035331Samw 		return (SA_NO_SUCH_RESOURCE);
5045331Samw 
5055331Samw 	if (smb_isonline()) {
5067348SJose.Borrego@Sun.COM 		res = smb_share_delete(rname);
5075331Samw 		if (res != NERR_Success) {
5085331Samw 			sa_free_attr_string(rname);
5095331Samw 			return (SA_CONFIG_ERR);
5105331Samw 		}
5115331Samw 	}
5125951Sdougm 
5135951Sdougm 	sa_free_attr_string(rname);
5145951Sdougm 
5155331Samw 	share = sa_get_resource_parent(resource);
5165331Samw 	if (share != NULL) {
5175331Samw 		rname = sa_get_share_attr(share, "path");
5185331Samw 		if (rname != NULL) {
5195951Sdougm 			sa_handle_t handle;
5205951Sdougm 
5215951Sdougm 			handle = sa_find_group_handle((sa_group_t)resource);
5225951Sdougm 			(void) sa_delete_sharetab(handle, rname, "smb");
5235331Samw 			sa_free_attr_string(rname);
5245331Samw 		}
5255331Samw 	}
5265331Samw 	/*
5275331Samw 	 * Always return OK as smb/server may be down and
5285331Samw 	 * Shares will be picked up when loaded.
5295331Samw 	 */
5305331Samw 	return (SA_OK);
5315331Samw }
5325331Samw 
5335331Samw /*
5345331Samw  * smb_share_changed(sa_share_t share)
5355331Samw  *
5365331Samw  * The specified share has changed.
5375331Samw  */
5385331Samw static int
5395331Samw smb_share_changed(sa_share_t share)
5405331Samw {
5415331Samw 	char *path;
5425331Samw 	sa_resource_t resource;
5435331Samw 
5447348SJose.Borrego@Sun.COM 	if (!smb_isonline())
5457348SJose.Borrego@Sun.COM 		return (SA_OK);
5467348SJose.Borrego@Sun.COM 
5475331Samw 	/* get the path since it is important in several places */
5485331Samw 	path = sa_get_share_attr(share, "path");
5495331Samw 	if (path == NULL)
5505331Samw 		return (SA_NO_SUCH_PATH);
5517348SJose.Borrego@Sun.COM 
5525331Samw 	for (resource = sa_get_share_resource(share, NULL);
5535331Samw 	    resource != NULL;
5545331Samw 	    resource = sa_get_next_resource(resource))
5555331Samw 		(void) smb_resource_changed(resource);
5565331Samw 
5575331Samw 	sa_free_attr_string(path);
5585331Samw 
5595331Samw 	return (SA_OK);
5605331Samw }
5615331Samw 
5625331Samw /*
5635331Samw  * smb_resource_changed(sa_resource_t resource)
5645331Samw  *
5655331Samw  * The specified resource has changed.
5665331Samw  */
5675331Samw static int
5685331Samw smb_resource_changed(sa_resource_t resource)
5695331Samw {
5707052Samw 	uint32_t res;
5717348SJose.Borrego@Sun.COM 	sa_share_t share;
5727052Samw 	smb_share_t si;
5735331Samw 
5747348SJose.Borrego@Sun.COM 	if (!smb_isonline())
5757348SJose.Borrego@Sun.COM 		return (SA_OK);
5765331Samw 
5777348SJose.Borrego@Sun.COM 	if ((share = sa_get_resource_parent(resource)) == NULL)
5787348SJose.Borrego@Sun.COM 		return (SA_CONFIG_ERR);
5795331Samw 
5807348SJose.Borrego@Sun.COM 	if ((res = smb_build_shareinfo(share, resource, &si)) != SA_OK)
5817348SJose.Borrego@Sun.COM 		return (res);
5825331Samw 
5837348SJose.Borrego@Sun.COM 	res = smb_share_modify(si.shr_name, si.shr_cmnt, si.shr_container);
5847348SJose.Borrego@Sun.COM 
5857348SJose.Borrego@Sun.COM 	if (res != NERR_Success)
5865331Samw 		return (SA_CONFIG_ERR);
5875331Samw 
5885331Samw 	return (smb_enable_service());
5895331Samw }
5905331Samw 
5915331Samw /*
5925800Sdougm  * smb_disable_share(sa_share_t share, char *path)
5935331Samw  *
5945800Sdougm  * Unshare the specified share. Note that "path" is the same
5955800Sdougm  * path as what is in the "share" object. It is passed in to avoid an
5965800Sdougm  * additional lookup. A missing "path" value makes this a no-op
5975800Sdougm  * function.
5985331Samw  */
5995331Samw static int
6005331Samw smb_disable_share(sa_share_t share, char *path)
6015331Samw {
6025331Samw 	char *rname;
6035331Samw 	sa_resource_t resource;
6045800Sdougm 	sa_group_t parent;
6055331Samw 	boolean_t iszfs;
6065331Samw 	int err = SA_OK;
6075951Sdougm 	sa_handle_t handle;
6087483SDoug.McCallum@Sun.COM 	boolean_t first = B_TRUE; /* work around sharetab issue */
6095331Samw 
6105800Sdougm 	if (path == NULL)
6115800Sdougm 		return (err);
6125800Sdougm 
6135800Sdougm 	/*
6145800Sdougm 	 * If the share is in a ZFS group we need to handle it
6155800Sdougm 	 * differently.  Just being on a ZFS file system isn't
6165800Sdougm 	 * enough since we may be in a legacy share case.
6175800Sdougm 	 */
6185800Sdougm 	parent = sa_get_parent_group(share);
6195800Sdougm 	iszfs = sa_group_is_zfs(parent);
6205800Sdougm 
6215331Samw 	if (!smb_isonline())
6225331Samw 		goto done;
6235331Samw 
6245331Samw 	for (resource = sa_get_share_resource(share, NULL);
6257483SDoug.McCallum@Sun.COM 	    resource != NULL || err != SA_OK;
6265331Samw 	    resource = sa_get_next_resource(resource)) {
6275331Samw 		rname = sa_get_resource_attr(resource, "name");
6285331Samw 		if (rname == NULL) {
6295331Samw 			continue;
6305331Samw 		}
6315331Samw 		if (!iszfs) {
6327348SJose.Borrego@Sun.COM 			err = smb_share_delete(rname);
6335331Samw 			switch (err) {
6345331Samw 			case NERR_NetNameNotFound:
6355331Samw 			case NERR_Success:
6365331Samw 				err = SA_OK;
6375331Samw 				break;
6385331Samw 			default:
6395331Samw 				err = SA_CONFIG_ERR;
6405331Samw 				break;
6415331Samw 			}
6425331Samw 		} else {
6435331Samw 			share_t sh;
6445331Samw 
6455331Samw 			sa_sharetab_fill_zfs(share, &sh, "smb");
6465331Samw 			err = sa_share_zfs(share, (char *)path, &sh,
6475331Samw 			    rname, ZFS_UNSHARE_SMB);
6487483SDoug.McCallum@Sun.COM 			/*
6497483SDoug.McCallum@Sun.COM 			 * If we are no longer the first case, we
6507483SDoug.McCallum@Sun.COM 			 * don't care about the sa_share_zfs err if it
6517483SDoug.McCallum@Sun.COM 			 * is -1. This works around a problem in
6527483SDoug.McCallum@Sun.COM 			 * sharefs and should be removed when sharefs
6537483SDoug.McCallum@Sun.COM 			 * supports multiple entries per path.
6547483SDoug.McCallum@Sun.COM 			 */
6557483SDoug.McCallum@Sun.COM 			if (!first && err == -1)
6567483SDoug.McCallum@Sun.COM 				err = SA_OK;
6577483SDoug.McCallum@Sun.COM 			first = B_FALSE;
6587483SDoug.McCallum@Sun.COM 
6595331Samw 			sa_emptyshare(&sh);
6605331Samw 		}
6615331Samw 		sa_free_attr_string(rname);
6625331Samw 	}
6635331Samw done:
6645951Sdougm 	if (!iszfs) {
6655951Sdougm 		handle = sa_find_group_handle((sa_group_t)share);
6665951Sdougm 		if (handle != NULL)
6675951Sdougm 			(void) sa_delete_sharetab(handle, path, "smb");
6685951Sdougm 		else
6695951Sdougm 			err = SA_SYSTEM_ERR;
6705951Sdougm 	}
6715331Samw 	return (err);
6725331Samw }
6735331Samw 
6745331Samw /*
6756214Sdougm  * smb_validate_property(handle, property, parent)
6765331Samw  *
6775331Samw  * Check that the property has a legitimate value for its type.
6786214Sdougm  * Handle isn't currently used but may need to be in the future.
6795331Samw  */
6805331Samw 
6816214Sdougm /*ARGSUSED*/
6825331Samw static int
6836214Sdougm smb_validate_property(sa_handle_t handle, sa_property_t property,
6846214Sdougm     sa_optionset_t parent)
6855331Samw {
6865331Samw 	int ret = SA_OK;
6875331Samw 	char *propname;
6885331Samw 	int optindex;
6895331Samw 	sa_group_t parent_group;
6905331Samw 	char *value;
6915331Samw 
6925331Samw 	propname = sa_get_property_attr(property, "type");
6935331Samw 
6945331Samw 	if ((optindex = findopt(propname)) < 0)
6955331Samw 		ret = SA_NO_SUCH_PROP;
6965331Samw 
6975331Samw 	/* need to validate value range here as well */
6985331Samw 	if (ret == SA_OK) {
6995331Samw 		parent_group = sa_get_parent_group((sa_share_t)parent);
7005331Samw 		if (optdefs[optindex].share && !sa_is_share(parent_group))
7015331Samw 			ret = SA_PROP_SHARE_ONLY;
7025331Samw 	}
7035331Samw 	if (ret != SA_OK) {
7045331Samw 		if (propname != NULL)
7055331Samw 			sa_free_attr_string(propname);
7065331Samw 		return (ret);
7075331Samw 	}
7085331Samw 
7095331Samw 	value = sa_get_property_attr(property, "value");
7105331Samw 	if (value != NULL) {
7115331Samw 		/* first basic type checking */
7125331Samw 		switch (optdefs[optindex].type) {
7135331Samw 		case OPT_TYPE_NUMBER:
7145331Samw 			/* check that the value is all digits */
7155331Samw 			if (!is_a_number(value))
7165331Samw 				ret = SA_BAD_VALUE;
7175331Samw 			break;
7185331Samw 		case OPT_TYPE_BOOLEAN:
7195331Samw 			if (strlen(value) == 0 ||
7205331Samw 			    strcasecmp(value, "true") == 0 ||
7215331Samw 			    strcmp(value, "1") == 0 ||
7225331Samw 			    strcasecmp(value, "false") == 0 ||
7235331Samw 			    strcmp(value, "0") == 0) {
7245331Samw 				ret = SA_OK;
7255331Samw 			} else {
7265331Samw 				ret = SA_BAD_VALUE;
7275331Samw 			}
7285331Samw 			break;
7295331Samw 		case OPT_TYPE_NAME:
7305331Samw 			/*
7315331Samw 			 * Make sure no invalid characters
7325331Samw 			 */
7335331Samw 			if (validresource(value) == B_FALSE)
7345331Samw 				ret = SA_BAD_VALUE;
7355331Samw 			break;
7365331Samw 		case OPT_TYPE_STRING:
7375331Samw 			/* whatever is here should be ok */
7385331Samw 			break;
7395331Samw 		default:
7405331Samw 			break;
7415331Samw 		}
7425331Samw 	}
7435331Samw 
7445331Samw 	if (value != NULL)
7455331Samw 		sa_free_attr_string(value);
7465331Samw 	if (ret == SA_OK && optdefs[optindex].check != NULL)
7475331Samw 		/* do the property specific check */
7485331Samw 		ret = optdefs[optindex].check(property);
7495331Samw 
7505331Samw 	if (propname != NULL)
7515331Samw 		sa_free_attr_string(propname);
7525331Samw 	return (ret);
7535331Samw }
7545331Samw 
7555331Samw /*
7565331Samw  * Protocol management functions
7575331Samw  *
7585331Samw  * properties defined in the default files are defined in
7595331Samw  * proto_option_defs for parsing and validation.
7605331Samw  */
7615331Samw 
7625331Samw struct smb_proto_option_defs {
7635331Samw 	int smb_index;
7645331Samw 	int32_t minval;
7655331Samw 	int32_t maxval; /* In case of length of string this should be max */
7665331Samw 	int (*validator)(int, char *);
7675331Samw 	int32_t	refresh;
7685331Samw } smb_proto_options[] = {
7695772Sas200622 	{ SMB_CI_SYS_CMNT, 0, MAX_VALUE_BUFLEN,
7705772Sas200622 	    string_length_check_validator, SMB_REFRESH_REFRESH },
7715772Sas200622 	{ SMB_CI_MAX_WORKERS, 64, 1024, range_check_validator,
7725772Sas200622 	    SMB_REFRESH_REFRESH },
7735772Sas200622 	{ SMB_CI_NBSCOPE, 0, MAX_VALUE_BUFLEN,
7745772Sas200622 	    string_length_check_validator, 0 },
7755772Sas200622 	{ SMB_CI_LM_LEVEL, 2, 5, range_check_validator, 0 },
7765772Sas200622 	{ SMB_CI_KEEPALIVE, 20, 5400, range_check_validator_zero_ok,
7775772Sas200622 	    SMB_REFRESH_REFRESH },
7785772Sas200622 	{ SMB_CI_WINS_SRV1, 0, MAX_VALUE_BUFLEN,
7795772Sas200622 	    ip_address_validator_empty_ok, SMB_REFRESH_REFRESH },
7805772Sas200622 	{ SMB_CI_WINS_SRV2, 0, MAX_VALUE_BUFLEN,
7815772Sas200622 	    ip_address_validator_empty_ok, SMB_REFRESH_REFRESH },
7825772Sas200622 	{ SMB_CI_WINS_EXCL, 0, MAX_VALUE_BUFLEN,
7835772Sas200622 	    ip_address_csv_list_validator_empty_ok, SMB_REFRESH_REFRESH },
7845772Sas200622 	{ SMB_CI_SIGNING_ENABLE, 0, 0, true_false_validator,
7855772Sas200622 	    SMB_REFRESH_REFRESH },
7865772Sas200622 	{ SMB_CI_SIGNING_REQD, 0, 0, true_false_validator,
7875772Sas200622 	    SMB_REFRESH_REFRESH },
7885772Sas200622 	{ SMB_CI_RESTRICT_ANON, 0, 0, true_false_validator,
7895772Sas200622 	    SMB_REFRESH_REFRESH },
7905772Sas200622 	{ SMB_CI_DOMAIN_SRV, 0, MAX_VALUE_BUFLEN,
7915772Sas200622 	    ip_address_validator_empty_ok, 0 },
7925772Sas200622 	{ SMB_CI_ADS_SITE, 0, MAX_VALUE_BUFLEN,
7935772Sas200622 	    string_length_check_validator, SMB_REFRESH_REFRESH },
7945772Sas200622 	{ SMB_CI_DYNDNS_ENABLE, 0, 0, true_false_validator, 0 },
7955772Sas200622 	{ SMB_CI_AUTOHOME_MAP, 0, MAX_VALUE_BUFLEN, path_validator, 0 },
7965331Samw };
7975331Samw 
7985772Sas200622 #define	SMB_OPT_NUM \
7995772Sas200622 	(sizeof (smb_proto_options) / sizeof (smb_proto_options[0]))
8005772Sas200622 
8015331Samw /*
8025331Samw  * Check the range of value as int range.
8035331Samw  */
8045331Samw static int
8055331Samw range_check_validator(int index, char *value)
8065331Samw {
8075331Samw 	int ret = SA_OK;
8085331Samw 
8095331Samw 	if (!is_a_number(value)) {
8105331Samw 		ret = SA_BAD_VALUE;
8115331Samw 	} else {
8125331Samw 		int val;
8135331Samw 		val = strtoul(value, NULL, 0);
8145331Samw 		if (val < smb_proto_options[index].minval ||
8155331Samw 		    val > smb_proto_options[index].maxval)
8165331Samw 			ret = SA_BAD_VALUE;
8175331Samw 	}
8185331Samw 	return (ret);
8195331Samw }
8205331Samw 
8215331Samw /*
8225331Samw  * Check the range of value as int range.
8235331Samw  */
8245331Samw static int
8255331Samw range_check_validator_zero_ok(int index, char *value)
8265331Samw {
8275331Samw 	int ret = SA_OK;
8285331Samw 
8295331Samw 	if (!is_a_number(value)) {
8305331Samw 		ret = SA_BAD_VALUE;
8315331Samw 	} else {
8325331Samw 		int val;
8335331Samw 		val = strtoul(value, NULL, 0);
8345331Samw 		if (val == 0)
8355331Samw 			ret = SA_OK;
8365331Samw 		else {
8375331Samw 			if (val < smb_proto_options[index].minval ||
8385331Samw 			    val > smb_proto_options[index].maxval)
8395331Samw 			ret = SA_BAD_VALUE;
8405331Samw 		}
8415331Samw 	}
8425331Samw 	return (ret);
8435331Samw }
8445331Samw 
8455331Samw /*
8465331Samw  * Check the length of the string
8475331Samw  */
8485331Samw static int
8495331Samw string_length_check_validator(int index, char *value)
8505331Samw {
8515331Samw 	int ret = SA_OK;
8525331Samw 
8535331Samw 	if (value == NULL)
8545331Samw 		return (SA_BAD_VALUE);
8555331Samw 	if (strlen(value) > smb_proto_options[index].maxval)
8565331Samw 		ret = SA_BAD_VALUE;
8575331Samw 	return (ret);
8585331Samw }
8595331Samw 
8605331Samw /*
8615331Samw  * Check yes/no
8625331Samw  */
8635331Samw /*ARGSUSED*/
8645331Samw static int
8655331Samw true_false_validator(int index, char *value)
8665331Samw {
8675331Samw 	if (value == NULL)
8685331Samw 		return (SA_BAD_VALUE);
8695331Samw 	if ((strcasecmp(value, "true") == 0) ||
8705331Samw 	    (strcasecmp(value, "false") == 0))
8715331Samw 		return (SA_OK);
8725331Samw 	return (SA_BAD_VALUE);
8735331Samw }
8745331Samw 
8755331Samw /*
8765331Samw  * Check IP address.
8775331Samw  */
8785331Samw /*ARGSUSED*/
8795331Samw static int
8805331Samw ip_address_validator_empty_ok(int index, char *value)
8815331Samw {
8825331Samw 	char sbytes[16];
8835331Samw 	int len;
8845331Samw 
8855331Samw 	if (value == NULL)
8865331Samw 		return (SA_OK);
8875331Samw 	len = strlen(value);
8885331Samw 	if (len == 0)
8895331Samw 		return (SA_OK);
8905331Samw 	if (inet_pton(AF_INET, value, (void *)sbytes) != 1)
8915331Samw 		return (SA_BAD_VALUE);
8925331Samw 
8935331Samw 	return (SA_OK);
8945331Samw }
8955331Samw 
8965331Samw /*
8975331Samw  * Check IP address list
8985331Samw  */
8995331Samw /*ARGSUSED*/
9005331Samw static int
9015331Samw ip_address_csv_list_validator_empty_ok(int index, char *value)
9025331Samw {
9035331Samw 	char sbytes[16];
9045331Samw 	char *ip, *tmp, *ctx;
9055331Samw 
9065331Samw 	if (value == NULL || *value == '\0')
9075331Samw 		return (SA_OK);
9085331Samw 
9095331Samw 	if (strlen(value) > MAX_VALUE_BUFLEN)
9105331Samw 		return (SA_BAD_VALUE);
9115331Samw 
9125331Samw 	if ((tmp = strdup(value)) == NULL)
9135331Samw 		return (SA_NO_MEMORY);
9145331Samw 
9155331Samw 	ip = strtok_r(tmp, ",", &ctx);
9165331Samw 	while (ip) {
9175331Samw 		if (strlen(ip) == 0) {
9185331Samw 			free(tmp);
9195331Samw 			return (SA_BAD_VALUE);
9205331Samw 		}
9215331Samw 		if (*ip != 0) {
9225331Samw 			if (inet_pton(AF_INET, ip,
9235331Samw 			    (void *)sbytes) != 1) {
9245331Samw 				free(tmp);
9255331Samw 				return (SA_BAD_VALUE);
9265331Samw 			}
9275331Samw 		}
9285331Samw 		ip = strtok_r(0, ",", &ctx);
9295331Samw 	}
9305331Samw 
9315331Samw 	free(tmp);
9325331Samw 	return (SA_OK);
9335331Samw }
9345331Samw 
9355331Samw /*
9365331Samw  * Check path
9375331Samw  */
9385331Samw /*ARGSUSED*/
9395331Samw static int
940*7588Samw@Sun.COM path_validator(int index, char *path)
9415331Samw {
9425331Samw 	struct stat buffer;
9435331Samw 	int fd, status;
9445331Samw 
945*7588Samw@Sun.COM 	if (path == NULL)
9465331Samw 		return (SA_BAD_VALUE);
9475331Samw 
948*7588Samw@Sun.COM 	fd = open(path, O_RDONLY);
9495331Samw 	if (fd < 0)
9505331Samw 		return (SA_BAD_VALUE);
9515331Samw 
9525331Samw 	status = fstat(fd, &buffer);
9535331Samw 	(void) close(fd);
9545331Samw 
9555331Samw 	if (status < 0)
9565331Samw 		return (SA_BAD_VALUE);
9575331Samw 
9585331Samw 	if (buffer.st_mode & S_IFDIR)
9595331Samw 		return (SA_OK);
9605331Samw 	return (SA_BAD_VALUE);
9615331Samw }
9625331Samw 
9635331Samw /*
9645331Samw  * the protoset holds the defined options so we don't have to read
9655331Samw  * them multiple times
9665331Samw  */
9675331Samw static sa_protocol_properties_t protoset;
9685331Samw 
9695331Samw static int
9705331Samw findprotoopt(char *name)
9715331Samw {
9725331Samw 	int i;
9735772Sas200622 	char *sc_name;
9745772Sas200622 
9755772Sas200622 	for (i = 0; i < SMB_OPT_NUM; i++) {
9765772Sas200622 		sc_name = smb_config_getname(smb_proto_options[i].smb_index);
9775772Sas200622 		if (strcasecmp(sc_name, name) == 0)
9785331Samw 			return (i);
9795331Samw 	}
9805772Sas200622 
9815331Samw 	return (-1);
9825331Samw }
9835331Samw 
9845331Samw /*
9855331Samw  * smb_load_proto_properties()
9865331Samw  *
9875331Samw  * read the smb config values from SMF.
9885331Samw  */
9895331Samw 
9905331Samw static int
9915331Samw smb_load_proto_properties()
9925331Samw {
9935331Samw 	sa_property_t prop;
9945772Sas200622 	char value[MAX_VALUE_BUFLEN];
9955772Sas200622 	char *name;
9965331Samw 	int index;
9976019Sdougm 	int ret = SA_OK;
9985772Sas200622 	int rc;
9995331Samw 
10005331Samw 	protoset = sa_create_protocol_properties(SMB_PROTOCOL_NAME);
10015331Samw 	if (protoset == NULL)
10025331Samw 		return (SA_NO_MEMORY);
10035331Samw 
10046019Sdougm 	for (index = 0; index < SMB_OPT_NUM && ret == SA_OK; index++) {
10055772Sas200622 		rc = smb_config_get(smb_proto_options[index].smb_index,
10065772Sas200622 		    value, sizeof (value));
10075772Sas200622 		if (rc != SMBD_SMF_OK)
10085772Sas200622 			continue;
10095772Sas200622 		name = smb_config_getname(smb_proto_options[index].smb_index);
10105772Sas200622 		prop = sa_create_property(name, value);
10115454Sdougm 		if (prop != NULL)
10126019Sdougm 			ret = sa_add_protocol_property(protoset, prop);
10136019Sdougm 		else
10146019Sdougm 			ret = SA_NO_MEMORY;
10155331Samw 	}
10166019Sdougm 	return (ret);
10175331Samw }
10185331Samw 
10195331Samw /*
10205331Samw  * smb_share_init()
10215331Samw  *
10225331Samw  * Initialize the smb plugin.
10235331Samw  */
10245331Samw 
10255331Samw static int
10265331Samw smb_share_init(void)
10275331Samw {
10285331Samw 	if (sa_plugin_ops.sa_init != smb_share_init)
10295331Samw 		return (SA_SYSTEM_ERR);
10305331Samw 
1031*7588Samw@Sun.COM 	smb_share_door_clnt_init();
10327348SJose.Borrego@Sun.COM 	return (smb_load_proto_properties());
10335331Samw }
10345331Samw 
10355331Samw /*
10365331Samw  * smb_share_fini()
10375331Samw  *
10385331Samw  */
10395331Samw static void
10405331Samw smb_share_fini(void)
10415331Samw {
10425331Samw 	xmlFreeNode(protoset);
10435331Samw 	protoset = NULL;
10445772Sas200622 
1045*7588Samw@Sun.COM 	smb_share_door_clnt_fini();
10465331Samw }
10475331Samw 
10485331Samw /*
10495331Samw  * smb_get_proto_set()
10505331Samw  *
10515331Samw  * Return an optionset with all the protocol specific properties in
10525331Samw  * it.
10535331Samw  */
10545331Samw static sa_protocol_properties_t
10555331Samw smb_get_proto_set(void)
10565331Samw {
10575331Samw 	return (protoset);
10585331Samw }
10595331Samw 
10605331Samw /*
10615772Sas200622  * smb_enable_dependencies()
10625772Sas200622  *
10635772Sas200622  * SMBD_DEFAULT_INSTANCE_FMRI may have some dependencies that aren't
10645772Sas200622  * enabled. This will attempt to enable all of them.
10655772Sas200622  */
10665772Sas200622 static void
10675772Sas200622 smb_enable_dependencies(const char *fmri)
10685772Sas200622 {
10695772Sas200622 	scf_handle_t *handle;
10705772Sas200622 	scf_service_t *service;
10715772Sas200622 	scf_instance_t *inst = NULL;
10725772Sas200622 	scf_iter_t *iter;
10735772Sas200622 	scf_property_t *prop;
10745772Sas200622 	scf_value_t *value;
10755772Sas200622 	scf_propertygroup_t *pg;
10765772Sas200622 	scf_scope_t *scope;
10775772Sas200622 	char type[SCFTYPE_LEN];
10785772Sas200622 	char *dependency;
10795772Sas200622 	char *servname;
10805772Sas200622 	int maxlen;
10815772Sas200622 
10825772Sas200622 	/*
10835772Sas200622 	 * Get all required handles and storage.
10845772Sas200622 	 */
10855772Sas200622 	handle = scf_handle_create(SCF_VERSION);
10865772Sas200622 	if (handle == NULL)
10875772Sas200622 		return;
10885772Sas200622 
10895772Sas200622 	if (scf_handle_bind(handle) != 0) {
10905772Sas200622 		scf_handle_destroy(handle);
10915772Sas200622 		return;
10925772Sas200622 	}
10935772Sas200622 
10945772Sas200622 	maxlen = scf_limit(SCF_LIMIT_MAX_VALUE_LENGTH);
10955772Sas200622 	if (maxlen == (ssize_t)-1)
10965772Sas200622 		maxlen = MAXPATHLEN;
10975772Sas200622 
10985772Sas200622 	dependency = malloc(maxlen);
10995772Sas200622 
11005772Sas200622 	service = scf_service_create(handle);
11015772Sas200622 
11025772Sas200622 	iter = scf_iter_create(handle);
11035772Sas200622 
11045772Sas200622 	pg = scf_pg_create(handle);
11055772Sas200622 
11065772Sas200622 	prop = scf_property_create(handle);
11075772Sas200622 
11085772Sas200622 	value = scf_value_create(handle);
11095772Sas200622 
11105772Sas200622 	scope = scf_scope_create(handle);
11115772Sas200622 
11125772Sas200622 	if (service == NULL || iter == NULL || pg == NULL || prop == NULL ||
11135772Sas200622 	    value == NULL || scope == NULL || dependency == NULL)
11145772Sas200622 		goto done;
11155772Sas200622 
11165772Sas200622 	/*
11175772Sas200622 	 *  We passed in the FMRI for the default instance but for
11185772Sas200622 	 *  some things we need the simple form so construct it. Since
11195772Sas200622 	 *  we reuse the storage that dependency points to, we need to
11205772Sas200622 	 *  use the servname early.
11215772Sas200622 	 */
11225772Sas200622 	(void) snprintf(dependency, maxlen, "%s", fmri + sizeof ("svc:"));
11235772Sas200622 	servname = strrchr(dependency, ':');
11245772Sas200622 	if (servname == NULL)
11255772Sas200622 		goto done;
11265772Sas200622 	*servname = '\0';
11275772Sas200622 	servname = dependency;
11285772Sas200622 
11295772Sas200622 	/*
11305772Sas200622 	 * Setup to iterate over the service property groups, only
11315772Sas200622 	 * looking at those that are "dependency" types. The "entity"
11325772Sas200622 	 * property will have the FMRI of the service we are dependent
11335772Sas200622 	 * on.
11345772Sas200622 	 */
11355772Sas200622 	if (scf_handle_get_scope(handle, SCF_SCOPE_LOCAL, scope) != 0)
11365772Sas200622 		goto done;
11375772Sas200622 
11385772Sas200622 	if (scf_scope_get_service(scope, servname, service) != 0)
11395772Sas200622 		goto done;
11405772Sas200622 
11415772Sas200622 	if (scf_iter_service_pgs(iter, service) != 0)
11425772Sas200622 		goto done;
11435772Sas200622 
11445772Sas200622 	while (scf_iter_next_pg(iter, pg) > 0) {
11455772Sas200622 		char *services[2];
11465772Sas200622 		/*
11475772Sas200622 		 * Have a property group for the service. See if it is
11485772Sas200622 		 * a dependency pg and only do operations on those.
11495772Sas200622 		 */
11505772Sas200622 		if (scf_pg_get_type(pg, type, SCFTYPE_LEN) <= 0)
11515772Sas200622 			continue;
11525772Sas200622 
11535772Sas200622 		if (strncmp(type, SCF_GROUP_DEPENDENCY, SCFTYPE_LEN) != 0)
11545772Sas200622 			continue;
11555772Sas200622 		/*
11565772Sas200622 		 * Have a dependency.  Attempt to enable it.
11575772Sas200622 		 */
11585772Sas200622 		if (scf_pg_get_property(pg, SCF_PROPERTY_ENTITIES, prop) != 0)
11595772Sas200622 			continue;
11605772Sas200622 
11615772Sas200622 		if (scf_property_get_value(prop, value) != 0)
11625772Sas200622 			continue;
11635772Sas200622 
11645772Sas200622 		services[1] = NULL;
11655772Sas200622 
11665772Sas200622 		if (scf_value_get_as_string(value, dependency, maxlen) > 0) {
11675772Sas200622 			services[0] = dependency;
11685772Sas200622 			_check_services(services);
11695772Sas200622 		}
11705772Sas200622 	}
11715772Sas200622 
11725772Sas200622 done:
11735772Sas200622 	if (dependency != NULL)
11745772Sas200622 		free(dependency);
11755772Sas200622 	if (value != NULL)
11765772Sas200622 		scf_value_destroy(value);
11775772Sas200622 	if (prop != NULL)
11785772Sas200622 		scf_property_destroy(prop);
11795772Sas200622 	if (pg != NULL)
11805772Sas200622 		scf_pg_destroy(pg);
11815772Sas200622 	if (iter != NULL)
11825772Sas200622 		scf_iter_destroy(iter);
11835772Sas200622 	if (scope != NULL)
11845772Sas200622 		scf_scope_destroy(scope);
11855772Sas200622 	if (inst != NULL)
11865772Sas200622 		scf_instance_destroy(inst);
11875772Sas200622 	if (service != NULL)
11885772Sas200622 		scf_service_destroy(service);
11895772Sas200622 
11905772Sas200622 	(void) scf_handle_unbind(handle);
11915772Sas200622 	scf_handle_destroy(handle);
11925772Sas200622 }
11935772Sas200622 
11945772Sas200622 /*
11955331Samw  * How long to wait for service to come online
11965331Samw  */
11975331Samw #define	WAIT_FOR_SERVICE	15
11985331Samw 
11995331Samw /*
12005331Samw  * smb_enable_service()
12015331Samw  *
12025331Samw  */
12035331Samw static int
12045331Samw smb_enable_service(void)
12055331Samw {
12065331Samw 	int i;
12075331Samw 	int ret = SA_OK;
12085772Sas200622 	char *service[] = { SMBD_DEFAULT_INSTANCE_FMRI, NULL };
12095331Samw 
12105331Samw 	if (!smb_isonline()) {
12115772Sas200622 		/*
12125772Sas200622 		 * Attempt to start the idmap, and other dependent
12135772Sas200622 		 * services, first.  If it fails, the SMB service will
12145772Sas200622 		 * ultimately fail so we use that as the error.  If we
12155772Sas200622 		 * don't try to enable idmap, smb won't start the
12165772Sas200622 		 * first time unless the admin has done it
12175772Sas200622 		 * manually. The service could be administratively
12185772Sas200622 		 * disabled so we won't always get started.
12195772Sas200622 		 */
12205772Sas200622 		smb_enable_dependencies(SMBD_DEFAULT_INSTANCE_FMRI);
12215772Sas200622 		_check_services(service);
12225331Samw 
12235331Samw 		/* Wait for service to come online */
12245331Samw 		for (i = 0; i < WAIT_FOR_SERVICE; i++) {
12255331Samw 			if (smb_isonline()) {
12265772Sas200622 				ret =  SA_OK;
12275331Samw 				break;
12286030Sjb150015 			} else if (smb_ismaint()) {
12296030Sjb150015 				/* maintenance requires help */
12306030Sjb150015 				ret = SA_SYSTEM_ERR;
12316030Sjb150015 				break;
12326030Sjb150015 			} else if (smb_isdisabled()) {
12336030Sjb150015 				/* disabled is ok */
12346030Sjb150015 				ret = SA_OK;
12356030Sjb150015 				break;
12365331Samw 			} else {
12376030Sjb150015 				/* try another time */
12385331Samw 				ret = SA_BUSY;
12395331Samw 				(void) sleep(1);
12405331Samw 			}
12415331Samw 		}
12425331Samw 	}
12435331Samw 	return (ret);
12445331Samw }
12455331Samw 
12465331Samw /*
12475331Samw  * smb_validate_proto_prop(index, name, value)
12485331Samw  *
12495331Samw  * Verify that the property specified by name can take the new
12505331Samw  * value. This is a sanity check to prevent bad values getting into
12515331Samw  * the default files.
12525331Samw  */
12535331Samw static int
12545331Samw smb_validate_proto_prop(int index, char *name, char *value)
12555331Samw {
12565331Samw 	if ((name == NULL) || (index < 0))
12575331Samw 		return (SA_BAD_VALUE);
12585331Samw 
12595331Samw 	if (smb_proto_options[index].validator == NULL)
12605331Samw 		return (SA_OK);
12615331Samw 
12625331Samw 	if (smb_proto_options[index].validator(index, value) == SA_OK)
12635331Samw 		return (SA_OK);
12645331Samw 	return (SA_BAD_VALUE);
12655331Samw }
12665331Samw 
12675331Samw /*
12685331Samw  * smb_set_proto_prop(prop)
12695331Samw  *
12705331Samw  * check that prop is valid.
12715331Samw  */
12725331Samw /*ARGSUSED*/
12735331Samw static int
12745331Samw smb_set_proto_prop(sa_property_t prop)
12755331Samw {
12765331Samw 	int ret = SA_OK;
12775331Samw 	char *name;
12785331Samw 	char *value;
12795331Samw 	int index = -1;
12805521Sas200622 	struct smb_proto_option_defs *opt;
12815331Samw 
12825331Samw 	name = sa_get_property_attr(prop, "type");
12835331Samw 	value = sa_get_property_attr(prop, "value");
12845331Samw 	if (name != NULL && value != NULL) {
12855331Samw 		index = findprotoopt(name);
12865331Samw 		if (index >= 0) {
12875331Samw 			/* should test for valid value */
12885331Samw 			ret = smb_validate_proto_prop(index, name, value);
12895331Samw 			if (ret == SA_OK) {
12905521Sas200622 				opt = &smb_proto_options[index];
12915521Sas200622 
12925331Samw 				/* Save to SMF */
12935772Sas200622 				(void) smb_config_set(opt->smb_index, value);
12945331Samw 				/*
12955331Samw 				 * Specialized refresh mechanisms can
12965331Samw 				 * be flagged in the proto_options and
12975331Samw 				 * processed here.
12985331Samw 				 */
12995521Sas200622 				if (opt->refresh & SMB_REFRESH_REFRESH)
13006139Sjb150015 					(void) smf_refresh_instance(
13016139Sjb150015 					    SMBD_DEFAULT_INSTANCE_FMRI);
13025521Sas200622 				else if (opt->refresh & SMB_REFRESH_RESTART)
13035331Samw 					(void) smf_restart_instance(
13045331Samw 					    SMBD_DEFAULT_INSTANCE_FMRI);
13055331Samw 			}
13065331Samw 		}
13075331Samw 	}
13085521Sas200622 
13095331Samw 	if (name != NULL)
13105331Samw 		sa_free_attr_string(name);
13115331Samw 	if (value != NULL)
13125331Samw 		sa_free_attr_string(value);
13135331Samw 
13145331Samw 	return (ret);
13155331Samw }
13165331Samw 
13175331Samw /*
13185331Samw  * smb_get_status()
13195331Samw  *
13205331Samw  * What is the current status of the smbd? We use the SMF state here.
13215331Samw  * Caller must free the returned value.
13225331Samw  */
13235331Samw 
13245331Samw static char *
13255331Samw smb_get_status(void)
13265331Samw {
13275331Samw 	char *state = NULL;
13285331Samw 	state = smf_get_state(SMBD_DEFAULT_INSTANCE_FMRI);
13295331Samw 	return (state != NULL ? state : "-");
13305331Samw }
13315331Samw 
13325331Samw /*
13335331Samw  * This protocol plugin require resource names
13345331Samw  */
13355331Samw static uint64_t
13365331Samw smb_share_features(void)
13375331Samw {
13385331Samw 	return (SA_FEATURE_RESOURCE | SA_FEATURE_ALLOWSUBDIRS |
13396088Sdougm 	    SA_FEATURE_ALLOWPARDIRS | SA_FEATURE_SERVER);
13405331Samw }
13415331Samw 
13425331Samw /*
13437348SJose.Borrego@Sun.COM  * This should be used to convert smb_share_t to sa_resource_t
13447348SJose.Borrego@Sun.COM  * Should only be needed to build transient shares/resources to be
13457348SJose.Borrego@Sun.COM  * supplied to sharemgr to display.
13465331Samw  */
13475331Samw static int
13487348SJose.Borrego@Sun.COM smb_add_transient(sa_handle_t handle, smb_share_t *si)
13495331Samw {
13505331Samw 	int err;
13515331Samw 	sa_share_t share;
13525331Samw 	sa_group_t group;
13535331Samw 	sa_resource_t resource;
13545331Samw 
13555331Samw 	if (si == NULL)
13565331Samw 		return (SA_INVALID_NAME);
13575331Samw 
13587348SJose.Borrego@Sun.COM 	if ((share = sa_find_share(handle, si->shr_path)) == NULL) {
13597348SJose.Borrego@Sun.COM 		if ((group = smb_get_defaultgrp(handle)) == NULL)
13607348SJose.Borrego@Sun.COM 			return (SA_NO_SUCH_GROUP);
13615331Samw 
13627052Samw 		share = sa_get_share(group, si->shr_path);
13635331Samw 		if (share == NULL) {
13647052Samw 			share = sa_add_share(group, si->shr_path,
13655331Samw 			    SA_SHARE_TRANSIENT, &err);
13665331Samw 			if (share == NULL)
13675331Samw 				return (SA_NO_SUCH_PATH);
13685331Samw 		}
13695331Samw 	}
13705331Samw 
13715331Samw 	/*
13725331Samw 	 * Now handle the resource. Make sure that the resource is
13735331Samw 	 * transient and added to the share.
13745331Samw 	 */
13757052Samw 	resource = sa_get_share_resource(share, si->shr_name);
13765331Samw 	if (resource == NULL) {
13775331Samw 		resource = sa_add_resource(share,
13787052Samw 		    si->shr_name, SA_SHARE_TRANSIENT, &err);
13795331Samw 		if (resource == NULL)
13805331Samw 			return (SA_NO_SUCH_RESOURCE);
13815331Samw 	}
13825331Samw 
13835331Samw 	/* set resource attributes now */
13847052Samw 	(void) sa_set_resource_attr(resource, "description", si->shr_cmnt);
13857052Samw 	(void) sa_set_resource_attr(resource, SMB_SHROPT_AD_CONTAINER,
13867052Samw 	    si->shr_container);
13875331Samw 
13885331Samw 	return (SA_OK);
13895331Samw }
13905331Samw 
13915331Samw /*
13927348SJose.Borrego@Sun.COM  * Return smb transient shares.
13935331Samw  */
13945331Samw static int
13955331Samw smb_list_transient(sa_handle_t handle)
13965331Samw {
13977348SJose.Borrego@Sun.COM 	int i, offset;
13987052Samw 	smb_shrlist_t list;
13995331Samw 	int res;
14005331Samw 
14017348SJose.Borrego@Sun.COM 	if (smb_share_count() <= 0)
14025331Samw 		return (SA_OK);
14037348SJose.Borrego@Sun.COM 
14045331Samw 	offset = 0;
14057348SJose.Borrego@Sun.COM 	while (smb_share_list(offset, &list) == NERR_Success) {
14067348SJose.Borrego@Sun.COM 		if (list.sl_cnt == 0)
14075331Samw 			break;
14087348SJose.Borrego@Sun.COM 
14097348SJose.Borrego@Sun.COM 		for (i = 0; i < list.sl_cnt; i++) {
14107348SJose.Borrego@Sun.COM 			res = smb_add_transient(handle, &(list.sl_shares[i]));
14115331Samw 			if (res != SA_OK)
14125331Samw 				return (res);
14135331Samw 		}
14147348SJose.Borrego@Sun.COM 		offset += list.sl_cnt;
14155331Samw 	}
14165331Samw 
14175331Samw 	return (SA_OK);
14185331Samw }
14195331Samw 
14205331Samw /*
14215331Samw  * fix_resource_name(share, name,  prefix)
14225331Samw  *
14235331Samw  * Construct a name where the ZFS dataset has the prefix replaced with "name".
14245331Samw  */
14255331Samw static char *
14265331Samw fix_resource_name(sa_share_t share, char *name, char *prefix)
14275331Samw {
14285331Samw 	char *dataset = NULL;
14295331Samw 	char *newname = NULL;
14305331Samw 	size_t psize;
14315331Samw 	size_t nsize;
14325331Samw 
14335331Samw 	dataset = sa_get_share_attr(share, "dataset");
14345331Samw 
14355331Samw 	if (dataset != NULL && strcmp(dataset, prefix) != 0) {
14365331Samw 		psize = strlen(prefix);
14375331Samw 		if (strncmp(dataset, prefix, psize) == 0) {
14385331Samw 			/* need string plus ',' and NULL */
14395331Samw 			nsize = (strlen(dataset) - psize) + strlen(name) + 2;
14405331Samw 			newname = calloc(nsize, 1);
14415331Samw 			if (newname != NULL) {
14425331Samw 				(void) snprintf(newname, nsize, "%s%s", name,
14435331Samw 				    dataset + psize);
14445331Samw 				sa_fix_resource_name(newname);
14455331Samw 			}
14465331Samw 			sa_free_attr_string(dataset);
14475331Samw 			return (newname);
14485331Samw 		}
14495331Samw 	}
14505331Samw 	if (dataset != NULL)
14515331Samw 		sa_free_attr_string(dataset);
14525331Samw 	return (strdup(name));
14535331Samw }
14545331Samw 
14555331Samw /*
14565331Samw  * smb_parse_optstring(group, options)
14575331Samw  *
14585331Samw  * parse a compact option string into individual options. This allows
14595331Samw  * ZFS sharesmb and sharemgr "share" command to work.  group can be a
14605331Samw  * group, a share or a resource.
14615331Samw  */
14625331Samw static int
14635331Samw smb_parse_optstring(sa_group_t group, char *options)
14645331Samw {
14655331Samw 	char *dup;
14665331Samw 	char *base;
14675331Samw 	char *lasts;
14685331Samw 	char *token;
14695331Samw 	sa_optionset_t optionset;
14705331Samw 	sa_group_t parent = NULL;
14715331Samw 	sa_resource_t resource = NULL;
14725331Samw 	int iszfs = 0;
14735331Samw 	int persist = 0;
14745331Samw 	int need_optionset = 0;
14755331Samw 	int ret = SA_OK;
14765331Samw 	sa_property_t prop;
14775331Samw 
14785331Samw 	/*
14795331Samw 	 * In order to not attempt to change ZFS properties unless
14805331Samw 	 * absolutely necessary, we never do it in the legacy parsing
14815331Samw 	 * so we need to keep track of this.
14825331Samw 	 */
14835331Samw 	if (sa_is_share(group)) {
14845331Samw 		char *zfs;
14855331Samw 
14865331Samw 		parent = sa_get_parent_group(group);
14875331Samw 		if (parent != NULL) {
14885331Samw 			zfs = sa_get_group_attr(parent, "zfs");
14895331Samw 			if (zfs != NULL) {
14905331Samw 				sa_free_attr_string(zfs);
14915331Samw 				iszfs = 1;
14925331Samw 			}
14935331Samw 		}
14945331Samw 	} else {
14955331Samw 		iszfs = sa_group_is_zfs(group);
14965331Samw 		/*
14975331Samw 		 * If a ZFS group, then we need to see if a resource
14985331Samw 		 * name is being set. If so, bail with
14995331Samw 		 * SA_PROP_SHARE_ONLY, so we come back in with a share
15005331Samw 		 * instead of a group.
15015331Samw 		 */
15025331Samw 		if (strncmp(options, "name=", sizeof ("name=") - 1) == 0 ||
15035331Samw 		    strstr(options, ",name=") != NULL) {
15045331Samw 			return (SA_PROP_SHARE_ONLY);
15055331Samw 		}
15065331Samw 	}
15075331Samw 
15085331Samw 	/* do we have an existing optionset? */
15095331Samw 	optionset = sa_get_optionset(group, "smb");
15105331Samw 	if (optionset == NULL) {
15115331Samw 		/* didn't find existing optionset so create one */
15125331Samw 		optionset = sa_create_optionset(group, "smb");
15135331Samw 		if (optionset == NULL)
15145331Samw 			return (SA_NO_MEMORY);
15155331Samw 	} else {
15165331Samw 		/*
15175331Samw 		 * If an optionset already exists, we've come through
15185331Samw 		 * twice so ignore the second time.
15195331Samw 		 */
15205331Samw 		return (ret);
15215331Samw 	}
15225331Samw 
15235331Samw 	/* We need a copy of options for the next part. */
15245331Samw 	dup = strdup(options);
15255331Samw 	if (dup == NULL)
15265331Samw 		return (SA_NO_MEMORY);
15275331Samw 
15285331Samw 	/*
15295331Samw 	 * SMB properties are straightforward and are strings,
15305331Samw 	 * integers or booleans.  Properties are separated by
15315331Samw 	 * commas. It will be necessary to parse quotes due to some
15325331Samw 	 * strings not having a restricted characters set.
15335331Samw 	 *
15345331Samw 	 * Note that names will create a resource. For now, if there
15355331Samw 	 * is a set of properties "before" the first name="", those
15365331Samw 	 * properties will be placed on the group.
15375331Samw 	 */
15385331Samw 	persist = sa_is_persistent(group);
15395331Samw 	base = dup;
15405331Samw 	token = dup;
15415331Samw 	lasts = NULL;
15425331Samw 	while (token != NULL && ret == SA_OK) {
15435331Samw 		ret = SA_OK;
15445331Samw 		token = strtok_r(base, ",", &lasts);
15455331Samw 		base = NULL;
15465331Samw 		if (token != NULL) {
15475331Samw 			char *value;
15485331Samw 			/*
15495331Samw 			 * All SMB properties have values so there
15505331Samw 			 * MUST be an '=' character.  If it doesn't,
15515331Samw 			 * it is a syntax error.
15525331Samw 			 */
15535331Samw 			value = strchr(token, '=');
15545331Samw 			if (value != NULL) {
15555331Samw 				*value++ = '\0';
15565331Samw 			} else {
15575331Samw 				ret = SA_SYNTAX_ERR;
15585331Samw 				break;
15595331Samw 			}
15605331Samw 			/*
15615331Samw 			 * We may need to handle a "name" property
15625331Samw 			 * that is a ZFS imposed resource name. Each
15635331Samw 			 * name would trigger getting a new "resource"
15645331Samw 			 * to put properties on. For now, assume no
15655331Samw 			 * "name" property for special handling.
15665331Samw 			 */
15675331Samw 
15685331Samw 			if (strcmp(token, "name") == 0) {
15695331Samw 				char *prefix;
15705331Samw 				char *name = NULL;
15715331Samw 				/*
15725331Samw 				 * We have a name, so now work on the
15735331Samw 				 * resource level. We have a "share"
15745331Samw 				 * in "group" due to the caller having
15755331Samw 				 * added it. If we are called with a
15765331Samw 				 * group, the check for group/share
15775331Samw 				 * at the beginning of this function
15785331Samw 				 * will bail out the parse if there is a
15795331Samw 				 * "name" but no share.
15805331Samw 				 */
15815331Samw 				if (!iszfs) {
15825331Samw 					ret = SA_SYNTAX_ERR;
15835331Samw 					break;
15845331Samw 				}
15855331Samw 				/*
15865331Samw 				 * Make sure the parent group has the
15875331Samw 				 * "prefix" property since we will
15885331Samw 				 * need to use this for constructing
15895331Samw 				 * inherited name= values.
15905331Samw 				 */
15915331Samw 				prefix = sa_get_group_attr(parent, "prefix");
15925331Samw 				if (prefix == NULL) {
15935331Samw 					prefix = sa_get_group_attr(parent,
15945331Samw 					    "name");
15955331Samw 					if (prefix != NULL) {
15965331Samw 						(void) sa_set_group_attr(parent,
15975331Samw 						    "prefix", prefix);
15985331Samw 					}
15995331Samw 				}
16005331Samw 				name = fix_resource_name((sa_share_t)group,
16015331Samw 				    value, prefix);
16025331Samw 				if (name != NULL) {
16035331Samw 					resource = sa_add_resource(
16045331Samw 					    (sa_share_t)group, name,
16055331Samw 					    SA_SHARE_TRANSIENT, &ret);
16065331Samw 					sa_free_attr_string(name);
16075331Samw 				} else {
16085331Samw 					ret = SA_NO_MEMORY;
16095331Samw 				}
16105331Samw 				if (prefix != NULL)
16115331Samw 					sa_free_attr_string(prefix);
16125331Samw 
16135331Samw 				/* A resource level optionset is needed */
16145331Samw 
16155331Samw 				need_optionset = 1;
16165331Samw 				if (resource == NULL) {
16175331Samw 					ret = SA_NO_MEMORY;
16185331Samw 					break;
16195331Samw 				}
16205331Samw 				continue;
16215331Samw 			}
16225331Samw 
16235331Samw 			if (need_optionset) {
16245331Samw 				optionset = sa_create_optionset(resource,
16255331Samw 				    "smb");
16265331Samw 				need_optionset = 0;
16275331Samw 			}
16285331Samw 
16295331Samw 			prop = sa_create_property(token, value);
16305331Samw 			if (prop == NULL)
16315331Samw 				ret = SA_NO_MEMORY;
16325331Samw 			else
16335331Samw 				ret = sa_add_property(optionset, prop);
16345331Samw 			if (ret != SA_OK)
16355331Samw 				break;
16365331Samw 			if (!iszfs)
16375331Samw 				ret = sa_commit_properties(optionset, !persist);
16385331Samw 		}
16395331Samw 	}
16405331Samw 	free(dup);
16415331Samw 	return (ret);
16425331Samw }
16435331Samw 
16445331Samw /*
16455331Samw  * smb_sprint_option(rbuff, rbuffsize, incr, prop, sep)
16465331Samw  *
16475331Samw  * provides a mechanism to format SMB properties into legacy output
16485331Samw  * format. If the buffer would overflow, it is reallocated and grown
16495331Samw  * as appropriate. Special cases of converting internal form of values
16505331Samw  * to those used by "share" are done. this function does one property
16515331Samw  * at a time.
16525331Samw  */
16535331Samw 
16545331Samw static void
16555331Samw smb_sprint_option(char **rbuff, size_t *rbuffsize, size_t incr,
16565331Samw 			sa_property_t prop, int sep)
16575331Samw {
16585331Samw 	char *name;
16595331Samw 	char *value;
16605331Samw 	int curlen;
16615331Samw 	char *buff = *rbuff;
16625331Samw 	size_t buffsize = *rbuffsize;
16635331Samw 
16645331Samw 	name = sa_get_property_attr(prop, "type");
16655331Samw 	value = sa_get_property_attr(prop, "value");
16665331Samw 	if (buff != NULL)
16675331Samw 		curlen = strlen(buff);
16685331Samw 	else
16695331Samw 		curlen = 0;
16705331Samw 	if (name != NULL) {
16715331Samw 		int len;
16725331Samw 		len = strlen(name) + sep;
16735331Samw 
16745331Samw 		/*
16755331Samw 		 * A future RFE would be to replace this with more
16765331Samw 		 * generic code and to possibly handle more types.
16775331Samw 		 *
16785331Samw 		 * For now, everything else is treated as a string. If
16795331Samw 		 * we get any properties that aren't exactly
16805331Samw 		 * name/value pairs, we may need to
16815331Samw 		 * interpret/transform.
16825331Samw 		 */
16835331Samw 		if (value != NULL)
16845331Samw 			len += 1 + strlen(value);
16855331Samw 
16865331Samw 		while (buffsize <= (curlen + len)) {
16875331Samw 			/* need more room */
16885331Samw 			buffsize += incr;
16895331Samw 			buff = realloc(buff, buffsize);
16905331Samw 			*rbuff = buff;
16915331Samw 			*rbuffsize = buffsize;
16925331Samw 			if (buff == NULL) {
16935331Samw 				/* realloc failed so free everything */
16945331Samw 				if (*rbuff != NULL)
16955331Samw 					free(*rbuff);
16965331Samw 				goto err;
16975331Samw 			}
16985331Samw 		}
16995331Samw 		if (buff == NULL)
17005331Samw 			goto err;
17015331Samw 		(void) snprintf(buff + curlen, buffsize - curlen,
17025331Samw 		    "%s%s=%s", sep ? "," : "",
17035331Samw 		    name, value != NULL ? value : "\"\"");
17045331Samw 
17055331Samw 	}
17065331Samw err:
17075331Samw 	if (name != NULL)
17085331Samw 		sa_free_attr_string(name);
17095331Samw 	if (value != NULL)
17105331Samw 		sa_free_attr_string(value);
17115331Samw }
17125331Samw 
17135331Samw /*
17145331Samw  * smb_format_resource_options(resource, hier)
17155331Samw  *
17165331Samw  * format all the options on the group into a flattened option
17175331Samw  * string. If hier is non-zero, walk up the tree to get inherited
17185331Samw  * options.
17195331Samw  */
17205331Samw 
17215331Samw static char *
17225331Samw smb_format_options(sa_group_t group, int hier)
17235331Samw {
17245331Samw 	sa_optionset_t options = NULL;
17255331Samw 	sa_property_t prop;
17265331Samw 	int sep = 0;
17275331Samw 	char *buff;
17285331Samw 	size_t buffsize;
17295331Samw 
17305331Samw 
17315331Samw 	buff = malloc(OPT_CHUNK);
17325331Samw 	if (buff == NULL)
17335331Samw 		return (NULL);
17345331Samw 
17355331Samw 	buff[0] = '\0';
17365331Samw 	buffsize = OPT_CHUNK;
17375331Samw 
17385331Samw 	/*
17395331Samw 	 * We may have a an optionset relative to this item. format
17405331Samw 	 * these if we find them and then add any security definitions.
17415331Samw 	 */
17425331Samw 
17435331Samw 	options = sa_get_derived_optionset(group, "smb", hier);
17445331Samw 
17455331Samw 	/*
17465331Samw 	 * do the default set first but skip any option that is also
17475331Samw 	 * in the protocol specific optionset.
17485331Samw 	 */
17495331Samw 	if (options != NULL) {
17505331Samw 		for (prop = sa_get_property(options, NULL);
17515331Samw 		    prop != NULL; prop = sa_get_next_property(prop)) {
17525331Samw 			/*
17535331Samw 			 * use this one since we skipped any
17545331Samw 			 * of these that were also in
17555331Samw 			 * optdefault
17565331Samw 			 */
17575331Samw 			smb_sprint_option(&buff, &buffsize, OPT_CHUNK,
17585331Samw 			    prop, sep);
17595331Samw 			if (buff == NULL) {
17605331Samw 				/*
17615331Samw 				 * buff could become NULL if there
17625331Samw 				 * isn't enough memory for
17635331Samw 				 * smb_sprint_option to realloc()
17645331Samw 				 * as necessary. We can't really
17655331Samw 				 * do anything about it at this
17665331Samw 				 * point so we return NULL.  The
17675331Samw 				 * caller should handle the
17685331Samw 				 * failure.
17695331Samw 				 */
17705331Samw 				if (options != NULL)
17715331Samw 					sa_free_derived_optionset(
17725331Samw 					    options);
17735331Samw 				return (buff);
17745331Samw 			}
17755331Samw 			sep = 1;
17765331Samw 		}
17775331Samw 	}
17785331Samw 
17795331Samw 	if (options != NULL)
17805331Samw 		sa_free_derived_optionset(options);
17815331Samw 	return (buff);
17825331Samw }
17835331Samw 
17845331Samw /*
17855331Samw  * smb_rename_resource(resource, newname)
17865331Samw  *
17875331Samw  * Change the current exported name of the resource to newname.
17885331Samw  */
17895331Samw /*ARGSUSED*/
17905331Samw int
17915331Samw smb_rename_resource(sa_handle_t handle, sa_resource_t resource, char *newname)
17925331Samw {
17935331Samw 	int ret = SA_OK;
17945331Samw 	int err;
17955331Samw 	char *oldname;
17965331Samw 
17977348SJose.Borrego@Sun.COM 	if (!smb_isonline())
17987348SJose.Borrego@Sun.COM 		return (SA_OK);
17997348SJose.Borrego@Sun.COM 
18005331Samw 	oldname = sa_get_resource_attr(resource, "name");
18015331Samw 	if (oldname == NULL)
18025331Samw 		return (SA_NO_SUCH_RESOURCE);
18035331Samw 
18047348SJose.Borrego@Sun.COM 	err = smb_share_rename(oldname, newname);
18055331Samw 
18065331Samw 	/* improve error values somewhat */
18075331Samw 	switch (err) {
18085331Samw 	case NERR_Success:
18095331Samw 		break;
18105331Samw 	case NERR_InternalError:
18115331Samw 		ret = SA_SYSTEM_ERR;
18125331Samw 		break;
18135331Samw 	case NERR_DuplicateShare:
18145331Samw 		ret = SA_DUPLICATE_NAME;
18155331Samw 		break;
18165331Samw 	default:
18175331Samw 		ret = SA_CONFIG_ERR;
18185331Samw 		break;
18195331Samw 	}
18205331Samw 
18215331Samw 	return (ret);
18225331Samw }
18237348SJose.Borrego@Sun.COM 
18247348SJose.Borrego@Sun.COM static int
18257348SJose.Borrego@Sun.COM smb_build_shareinfo(sa_share_t share, sa_resource_t resource, smb_share_t *si)
18267348SJose.Borrego@Sun.COM {
18277348SJose.Borrego@Sun.COM 	sa_property_t prop;
18287348SJose.Borrego@Sun.COM 	sa_optionset_t opts;
18297348SJose.Borrego@Sun.COM 	char *path;
18307348SJose.Borrego@Sun.COM 	char *rname;
18317348SJose.Borrego@Sun.COM 	char *val = NULL;
18327348SJose.Borrego@Sun.COM 
18337348SJose.Borrego@Sun.COM 	bzero(si, sizeof (smb_share_t));
18347348SJose.Borrego@Sun.COM 
18357348SJose.Borrego@Sun.COM 	if ((path = sa_get_share_attr(share, "path")) == NULL)
18367348SJose.Borrego@Sun.COM 		return (SA_NO_SUCH_PATH);
18377348SJose.Borrego@Sun.COM 
18387348SJose.Borrego@Sun.COM 	if ((rname = sa_get_resource_attr(resource, "name")) == NULL) {
18397348SJose.Borrego@Sun.COM 		sa_free_attr_string(path);
18407348SJose.Borrego@Sun.COM 		return (SA_NO_SUCH_RESOURCE);
18417348SJose.Borrego@Sun.COM 	}
18427348SJose.Borrego@Sun.COM 
18437348SJose.Borrego@Sun.COM 	si->shr_flags = (sa_is_persistent(share))
18447348SJose.Borrego@Sun.COM 	    ? SMB_SHRF_PERM : SMB_SHRF_TRANS;
18457348SJose.Borrego@Sun.COM 
18467348SJose.Borrego@Sun.COM 	(void) strlcpy(si->shr_path, path, sizeof (si->shr_path));
18477348SJose.Borrego@Sun.COM 	(void) strlcpy(si->shr_name, rname, sizeof (si->shr_name));
18487348SJose.Borrego@Sun.COM 	sa_free_attr_string(path);
18497348SJose.Borrego@Sun.COM 	sa_free_attr_string(rname);
18507348SJose.Borrego@Sun.COM 
18517348SJose.Borrego@Sun.COM 	val = sa_get_resource_description(resource);
18527348SJose.Borrego@Sun.COM 	if (val == NULL)
18537348SJose.Borrego@Sun.COM 		val = sa_get_share_description(share);
18547348SJose.Borrego@Sun.COM 
18557348SJose.Borrego@Sun.COM 	if (val != NULL) {
18567348SJose.Borrego@Sun.COM 		(void) strlcpy(si->shr_cmnt, val, sizeof (si->shr_cmnt));
18577348SJose.Borrego@Sun.COM 		sa_free_share_description(val);
18587348SJose.Borrego@Sun.COM 	}
18597348SJose.Borrego@Sun.COM 
18607348SJose.Borrego@Sun.COM 	opts = sa_get_derived_optionset(resource, SMB_PROTOCOL_NAME, 1);
18617348SJose.Borrego@Sun.COM 	if (opts == NULL)
18627348SJose.Borrego@Sun.COM 		return (SA_OK);
18637348SJose.Borrego@Sun.COM 
18647348SJose.Borrego@Sun.COM 	prop = sa_get_property(opts, SMB_SHROPT_AD_CONTAINER);
18657348SJose.Borrego@Sun.COM 	if (prop != NULL) {
18667348SJose.Borrego@Sun.COM 		if ((val = sa_get_property_attr(prop, "value")) != NULL) {
18677348SJose.Borrego@Sun.COM 			(void) strlcpy(si->shr_container, val,
18687348SJose.Borrego@Sun.COM 			    sizeof (si->shr_container));
18697348SJose.Borrego@Sun.COM 			free(val);
18707348SJose.Borrego@Sun.COM 		}
18717348SJose.Borrego@Sun.COM 	}
18727348SJose.Borrego@Sun.COM 
18737348SJose.Borrego@Sun.COM 	sa_free_derived_optionset(opts);
18747348SJose.Borrego@Sun.COM 	return (SA_OK);
18757348SJose.Borrego@Sun.COM }
18767348SJose.Borrego@Sun.COM 
18777348SJose.Borrego@Sun.COM /*
18787348SJose.Borrego@Sun.COM  * smb_get_defaultgrp
18797348SJose.Borrego@Sun.COM  *
18807348SJose.Borrego@Sun.COM  * If default group for CIFS shares (i.e. "smb") exists
18817348SJose.Borrego@Sun.COM  * then it will return the group handle, otherwise it will
18827348SJose.Borrego@Sun.COM  * create the group and return the handle.
18837348SJose.Borrego@Sun.COM  *
18847348SJose.Borrego@Sun.COM  * All the shares created by CIFS clients (this is only possible
18857348SJose.Borrego@Sun.COM  * via RPC) will be added to "smb" groups.
18867348SJose.Borrego@Sun.COM  */
18877348SJose.Borrego@Sun.COM static sa_group_t
18887348SJose.Borrego@Sun.COM smb_get_defaultgrp(sa_handle_t handle)
18897348SJose.Borrego@Sun.COM {
18907348SJose.Borrego@Sun.COM 	sa_group_t group = NULL;
18917348SJose.Borrego@Sun.COM 	int err;
18927348SJose.Borrego@Sun.COM 
18937348SJose.Borrego@Sun.COM 	group = sa_get_group(handle, SMB_DEFAULT_SHARE_GROUP);
18947348SJose.Borrego@Sun.COM 	if (group != NULL)
18957348SJose.Borrego@Sun.COM 		return (group);
18967348SJose.Borrego@Sun.COM 
18977348SJose.Borrego@Sun.COM 	group = sa_create_group(handle, SMB_DEFAULT_SHARE_GROUP, &err);
18987348SJose.Borrego@Sun.COM 	if (group == NULL)
18997348SJose.Borrego@Sun.COM 		return (NULL);
19007348SJose.Borrego@Sun.COM 
19017348SJose.Borrego@Sun.COM 	if (sa_create_optionset(group, SMB_DEFAULT_SHARE_GROUP) == NULL) {
19027348SJose.Borrego@Sun.COM 		(void) sa_remove_group(group);
19037348SJose.Borrego@Sun.COM 		group = NULL;
19047348SJose.Borrego@Sun.COM 	}
19057348SJose.Borrego@Sun.COM 
19067348SJose.Borrego@Sun.COM 	return (group);
19077348SJose.Borrego@Sun.COM }
1908