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 
277348SJose.Borrego@Sun.COM #pragma ident	"@(#)libshare_smb.c	1.16	08/08/05 SMI"
285331Samw 
295331Samw /*
305331Samw  * SMB specific functions
315331Samw  */
325331Samw #include <stdio.h>
335331Samw #include <string.h>
345331Samw #include <ctype.h>
355331Samw #include <stdlib.h>
365331Samw #include <unistd.h>
375331Samw #include <zone.h>
385331Samw #include <errno.h>
395331Samw #include <locale.h>
405331Samw #include <fcntl.h>
415331Samw #include <sys/types.h>
425331Samw #include <sys/stat.h>
435331Samw #include <syslog.h>
445331Samw #include "libshare.h"
455331Samw #include "libshare_impl.h"
465331Samw #include <pwd.h>
475331Samw #include <limits.h>
485331Samw #include <libscf.h>
495331Samw #include <strings.h>
505331Samw #include "libshare_smb.h"
515331Samw #include <rpcsvc/daemon_utils.h>
527052Samw #include <smbsrv/smb_share.h>
535331Samw #include <smbsrv/smbinfo.h>
545331Samw #include <smbsrv/libsmb.h>
555331Samw 
565331Samw /* internal functions */
575331Samw static int smb_share_init(void);
585331Samw static void smb_share_fini(void);
595331Samw static int smb_enable_share(sa_share_t);
605331Samw static int smb_share_changed(sa_share_t);
615331Samw static int smb_resource_changed(sa_resource_t);
625331Samw static int smb_rename_resource(sa_handle_t, sa_resource_t, char *);
635331Samw static int smb_disable_share(sa_share_t share, char *);
646214Sdougm static int smb_validate_property(sa_handle_t, sa_property_t, sa_optionset_t);
655331Samw static int smb_set_proto_prop(sa_property_t);
665331Samw static sa_protocol_properties_t smb_get_proto_set(void);
675331Samw static char *smb_get_status(void);
685331Samw static int smb_parse_optstring(sa_group_t, char *);
695331Samw static char *smb_format_options(sa_group_t, int);
705331Samw 
715331Samw static int smb_enable_service(void);
725331Samw 
735331Samw static int range_check_validator(int, char *);
745331Samw static int range_check_validator_zero_ok(int, char *);
755331Samw static int string_length_check_validator(int, char *);
765331Samw static int true_false_validator(int, char *);
775331Samw static int ip_address_validator_empty_ok(int, char *);
785331Samw static int ip_address_csv_list_validator_empty_ok(int, char *);
795331Samw static int path_validator(int, char *);
805331Samw 
815331Samw static int smb_enable_resource(sa_resource_t);
825331Samw static int smb_disable_resource(sa_resource_t);
835331Samw static uint64_t smb_share_features(void);
845331Samw static int smb_list_transient(sa_handle_t);
855772Sas200622 
867348SJose.Borrego@Sun.COM static int smb_build_shareinfo(sa_share_t, sa_resource_t, smb_share_t *);
877348SJose.Borrego@Sun.COM static sa_group_t smb_get_defaultgrp(sa_handle_t);
887348SJose.Borrego@Sun.COM 
895331Samw /* size of basic format allocation */
905331Samw #define	OPT_CHUNK	1024
915331Samw 
925772Sas200622 /* size of string for types - big enough to hold "dependency" */
935772Sas200622 #define	SCFTYPE_LEN	32
945772Sas200622 
955331Samw /*
965331Samw  * Indexes of entries in smb_proto_options table.
975331Samw  * Changes to smb_proto_options table may require
985331Samw  * an update to these values.
995331Samw  */
1005331Samw #define	PROTO_OPT_WINS1			6
1015331Samw #define	PROTO_OPT_WINS_EXCLUDE		8
1025331Samw 
1035331Samw 
1045331Samw /*
1055331Samw  * ops vector that provides the protocol specific info and operations
1065331Samw  * for share management.
1075331Samw  */
1085331Samw 
1095331Samw struct sa_plugin_ops sa_plugin_ops = {
1105331Samw 	SA_PLUGIN_VERSION,
1115331Samw 	SMB_PROTOCOL_NAME,
1125331Samw 	smb_share_init,
1135331Samw 	smb_share_fini,
1145331Samw 	smb_enable_share,
1155331Samw 	smb_disable_share,
1165331Samw 	smb_validate_property,
1176007Sthurlow 	NULL,	/* valid_space */
1186007Sthurlow 	NULL,	/* security_prop */
1195331Samw 	smb_parse_optstring,
1205331Samw 	smb_format_options,
1215331Samw 	smb_set_proto_prop,
1225331Samw 	smb_get_proto_set,
1235331Samw 	smb_get_status,
1246007Sthurlow 	NULL,	/* space_alias */
1256007Sthurlow 	NULL,	/* update_legacy */
1266007Sthurlow 	NULL,	/* delete_legacy */
1275331Samw 	smb_share_changed,
1285331Samw 	smb_enable_resource,
1295331Samw 	smb_disable_resource,
1305331Samw 	smb_share_features,
1315331Samw 	smb_list_transient,
1325331Samw 	smb_resource_changed,
1335331Samw 	smb_rename_resource,
1346007Sthurlow 	NULL,	/* run_command */
1356007Sthurlow 	NULL,	/* command_help */
1366007Sthurlow 	NULL	/* delete_proto_section */
1375331Samw };
1385331Samw 
1395331Samw /*
1405331Samw  * option definitions.  Make sure to keep the #define for the option
1415331Samw  * index just before the entry it is the index for. Changing the order
1425331Samw  * can cause breakage.
1435331Samw  */
1445331Samw 
1455331Samw struct option_defs optdefs[] = {
1467052Samw 	{SMB_SHROPT_AD_CONTAINER, OPT_TYPE_STRING},
1477052Samw 	{SMB_SHROPT_NAME, OPT_TYPE_NAME},
1485331Samw 	{NULL, NULL},
1495331Samw };
1505331Samw 
1515331Samw /*
1525331Samw  * findopt(name)
1535331Samw  *
1545331Samw  * Lookup option "name" in the option table and return the table
1555331Samw  * index.
1565331Samw  */
1575331Samw static int
1585331Samw findopt(char *name)
1595331Samw {
1605331Samw 	int i;
1615331Samw 	if (name != NULL) {
1625331Samw 		for (i = 0; optdefs[i].tag != NULL; i++) {
1635331Samw 			if (strcmp(optdefs[i].tag, name) == 0)
1645331Samw 				return (i);
1655331Samw 		}
1665331Samw 	}
1675331Samw 	return (-1);
1685331Samw }
1695331Samw 
1705331Samw /*
1715331Samw  * is_a_number(number)
1725331Samw  *
1735331Samw  * is the string a number in one of the forms we want to use?
1745331Samw  */
1757348SJose.Borrego@Sun.COM static boolean_t
1765331Samw is_a_number(char *number)
1775331Samw {
1787348SJose.Borrego@Sun.COM 	boolean_t isnum = B_TRUE;
1797348SJose.Borrego@Sun.COM 	boolean_t ishex = B_FALSE;
1807348SJose.Borrego@Sun.COM 
1817348SJose.Borrego@Sun.COM 	if (number == NULL || *number == '\0')
1827348SJose.Borrego@Sun.COM 		return (B_FALSE);
1835331Samw 
1847348SJose.Borrego@Sun.COM 	if (strncasecmp(number, "0x", 2) == 0) {
1855331Samw 		number += 2;
1867348SJose.Borrego@Sun.COM 		ishex = B_TRUE;
1875331Samw 	} else if (*number == '-') {
1887348SJose.Borrego@Sun.COM 		number++;
1895331Samw 	}
1905331Samw 
1917348SJose.Borrego@Sun.COM 	while (isnum && (*number != '\0')) {
1927348SJose.Borrego@Sun.COM 		isnum = (ishex) ? isxdigit(*number) : isdigit(*number);
1937348SJose.Borrego@Sun.COM 		number++;
1945331Samw 	}
1957348SJose.Borrego@Sun.COM 
1967348SJose.Borrego@Sun.COM 	return (isnum);
1975331Samw }
1985331Samw 
1995331Samw /*
2005331Samw  * validresource(name)
2015331Samw  *
2025331Samw  * Check that name only has valid characters in it. The current valid
2035331Samw  * set are the printable characters but not including:
2045331Samw  *	" / \ [ ] : | < > + ; , ? * = \t
2055331Samw  * Note that space is included and there is a maximum length.
2065331Samw  */
2077348SJose.Borrego@Sun.COM static boolean_t
2085331Samw validresource(const char *name)
2095331Samw {
2105331Samw 	const char *cp;
2115331Samw 	size_t len;
2125331Samw 
2135331Samw 	if (name == NULL)
2145331Samw 		return (B_FALSE);
2155331Samw 
2165331Samw 	len = strlen(name);
2175331Samw 	if (len == 0 || len > SA_MAX_RESOURCE_NAME)
2185331Samw 		return (B_FALSE);
2195331Samw 
2205331Samw 	if (strpbrk(name, "\"/\\[]:|<>+;,?*=\t") != NULL) {
2215331Samw 		return (B_FALSE);
2225331Samw 	}
2235331Samw 
2245331Samw 	for (cp = name; *cp != '\0'; cp++)
2255331Samw 		if (iscntrl(*cp))
2265331Samw 			return (B_FALSE);
2275331Samw 
2285331Samw 	return (B_TRUE);
2295331Samw }
2305331Samw 
2315331Samw /*
2325331Samw  * smb_isonline()
2335331Samw  *
2345331Samw  * Determine if the SMF service instance is in the online state or
2355331Samw  * not. A number of operations depend on this state.
2365331Samw  */
2375331Samw static boolean_t
2385331Samw smb_isonline(void)
2395331Samw {
2405331Samw 	char *str;
2415331Samw 	boolean_t ret = B_FALSE;
2425331Samw 
2435331Samw 	if ((str = smf_get_state(SMBD_DEFAULT_INSTANCE_FMRI)) != NULL) {
2445331Samw 		ret = (strcmp(str, SCF_STATE_STRING_ONLINE) == 0);
2455331Samw 		free(str);
2465331Samw 	}
2475331Samw 	return (ret);
2485331Samw }
2495331Samw 
2505331Samw /*
2515772Sas200622  * smb_isdisabled()
2525772Sas200622  *
2535772Sas200622  * Determine if the SMF service instance is in the disabled state or
2545772Sas200622  * not. A number of operations depend on this state.
2555772Sas200622  */
2565772Sas200622 static boolean_t
2575772Sas200622 smb_isdisabled(void)
2585772Sas200622 {
2595772Sas200622 	char *str;
2605772Sas200622 	boolean_t ret = B_FALSE;
2615772Sas200622 
2625772Sas200622 	if ((str = smf_get_state(SMBD_DEFAULT_INSTANCE_FMRI)) != NULL) {
2635772Sas200622 		ret = (strcmp(str, SCF_STATE_STRING_DISABLED) == 0);
2645772Sas200622 		free(str);
2655772Sas200622 	}
2665772Sas200622 	return (ret);
2675772Sas200622 }
2685772Sas200622 
2695772Sas200622 /*
2705772Sas200622  * smb_isautoenable()
2715772Sas200622  *
2725772Sas200622  * Determine if the SMF service instance auto_enabled set or not. A
2735772Sas200622  * number of operations depend on this state.  The property not being
2745772Sas200622  * set or being set to true means autoenable.  Only being set to false
2755772Sas200622  * is not autoenabled.
2765772Sas200622  */
2775772Sas200622 static boolean_t
2785772Sas200622 smb_isautoenable(void)
2795772Sas200622 {
2805772Sas200622 	boolean_t ret = B_TRUE;
2815772Sas200622 	scf_simple_prop_t *prop;
2825772Sas200622 	uint8_t *retstr;
2835772Sas200622 
2845772Sas200622 	prop = scf_simple_prop_get(NULL, SMBD_DEFAULT_INSTANCE_FMRI,
2855772Sas200622 	    "application", "auto_enable");
2865772Sas200622 	if (prop != NULL) {
2875772Sas200622 		retstr = scf_simple_prop_next_boolean(prop);
2885772Sas200622 		ret = *retstr != 0;
2895772Sas200622 		scf_simple_prop_free(prop);
2905772Sas200622 	}
2915772Sas200622 	return (ret);
2925772Sas200622 }
2935772Sas200622 
2945772Sas200622 /*
2956030Sjb150015  * smb_ismaint()
2966030Sjb150015  *
2976030Sjb150015  * Determine if the SMF service instance is in the disabled state or
2986030Sjb150015  * not. A number of operations depend on this state.
2996030Sjb150015  */
3006030Sjb150015 static boolean_t
3016030Sjb150015 smb_ismaint(void)
3026030Sjb150015 {
3036030Sjb150015 	char *str;
3046030Sjb150015 	boolean_t ret = B_FALSE;
3056030Sjb150015 
3066030Sjb150015 	if ((str = smf_get_state(SMBD_DEFAULT_INSTANCE_FMRI)) != NULL) {
3076030Sjb150015 		ret = (strcmp(str, SCF_STATE_STRING_MAINT) == 0);
3086030Sjb150015 		free(str);
3096030Sjb150015 	}
3106030Sjb150015 	return (ret);
3116030Sjb150015 }
3126030Sjb150015 
3136030Sjb150015 /*
3145331Samw  * smb_enable_share tells the implementation that it is to enable the share.
3155331Samw  * This entails converting the path and options into the appropriate ioctl
3165331Samw  * calls. It is assumed that all error checking of paths, etc. were
3175331Samw  * done earlier.
3185331Samw  */
3195331Samw static int
3205331Samw smb_enable_share(sa_share_t share)
3215331Samw {
3225331Samw 	char *path;
3237052Samw 	smb_share_t si;
3245331Samw 	sa_resource_t resource;
3255331Samw 	boolean_t iszfs;
3265331Samw 	boolean_t privileged;
3275331Samw 	int err = SA_OK;
3285331Samw 	priv_set_t *priv_effective;
3295331Samw 	boolean_t online;
3305331Samw 
3316270Sdougm 	/*
3326270Sdougm 	 * We only start in the global zone and only run if we aren't
3336270Sdougm 	 * running Trusted Extensions.
3346270Sdougm 	 */
3356270Sdougm 	if (getzoneid() != GLOBAL_ZONEID) {
3366270Sdougm 		(void) printf(dgettext(TEXT_DOMAIN,
3376270Sdougm 		    "SMB: service not supported in local zone\n"));
3386270Sdougm 		return (SA_NOT_SUPPORTED);
3396270Sdougm 	}
3406270Sdougm 	if (is_system_labeled()) {
3416270Sdougm 		(void) printf(dgettext(TEXT_DOMAIN,
3426270Sdougm 		    "SMB: service not supported with Trusted Extensions\n"));
3436270Sdougm 		return (SA_NOT_SUPPORTED);
3446270Sdougm 	}
3456270Sdougm 
3465331Samw 	priv_effective = priv_allocset();
3475331Samw 	(void) getppriv(PRIV_EFFECTIVE, priv_effective);
3485331Samw 	privileged = (priv_isfullset(priv_effective) == B_TRUE);
3495331Samw 	priv_freeset(priv_effective);
3505331Samw 
3515331Samw 	/* get the path since it is important in several places */
3525331Samw 	path = sa_get_share_attr(share, "path");
3535331Samw 	if (path == NULL)
3545331Samw 		return (SA_NO_SUCH_PATH);
3555331Samw 
3565772Sas200622 	/*
3575772Sas200622 	 * If administratively disabled, don't try to start anything.
3585772Sas200622 	 */
3595331Samw 	online = smb_isonline();
3605772Sas200622 	if (!online && !smb_isautoenable() && smb_isdisabled())
3615772Sas200622 		goto done;
3625331Samw 
3635331Samw 	iszfs = sa_path_is_zfs(path);
3645331Samw 
3655331Samw 	if (iszfs) {
3665331Samw 
3675331Samw 		if (privileged == B_FALSE && !online) {
3685331Samw 
3695331Samw 			if (!online) {
3705331Samw 				(void) printf(dgettext(TEXT_DOMAIN,
3715331Samw 				    "SMB: Cannot share remove "
3725331Samw 				    "file system: %s\n"), path);
3735331Samw 				(void) printf(dgettext(TEXT_DOMAIN,
3745331Samw 				    "SMB: Service needs to be enabled "
3755331Samw 				    "by a privileged user\n"));
3765331Samw 				err = SA_NO_PERMISSION;
3775331Samw 				errno = EPERM;
3785331Samw 			}
3795331Samw 			if (err) {
3805331Samw 				sa_free_attr_string(path);
3815331Samw 				return (err);
3825331Samw 			}
3835331Samw 
3845331Samw 		}
3855331Samw 	}
3865331Samw 
3875331Samw 	if (privileged == B_TRUE && !online) {
3885331Samw 		err = smb_enable_service();
3895331Samw 		if (err != SA_OK) {
3905331Samw 			(void) printf(dgettext(TEXT_DOMAIN,
3915331Samw 			    "SMB: Unable to enable service\n"));
3925331Samw 			/*
3935331Samw 			 * For now, it is OK to not be able to enable
3945331Samw 			 * the service.
3955331Samw 			 */
3966030Sjb150015 			if (err == SA_BUSY || err == SA_SYSTEM_ERR)
3975331Samw 				err = SA_OK;
3985331Samw 		} else {
3995331Samw 			online = B_TRUE;
4005331Samw 		}
4015331Samw 	}
4025331Samw 
4035331Samw 	/*
4045331Samw 	 * Don't bother trying to start shares if the service isn't
4055331Samw 	 * running.
4065331Samw 	 */
4075331Samw 	if (!online)
4085331Samw 		goto done;
4095331Samw 
4105331Samw 	/* Each share can have multiple resources */
4115331Samw 	for (resource = sa_get_share_resource(share, NULL);
4125331Samw 	    resource != NULL;
4135331Samw 	    resource = sa_get_next_resource(resource)) {
4147348SJose.Borrego@Sun.COM 		err = smb_build_shareinfo(share, resource, &si);
4157348SJose.Borrego@Sun.COM 		if (err != SA_OK) {
4165331Samw 			sa_free_attr_string(path);
4177348SJose.Borrego@Sun.COM 			return (err);
4185331Samw 		}
4195331Samw 
4205331Samw 		if (!iszfs) {
4217348SJose.Borrego@Sun.COM 			err = smb_share_create(&si);
4225331Samw 		} else {
4235331Samw 			share_t sh;
4245331Samw 
4255331Samw 			sa_sharetab_fill_zfs(share, &sh, "smb");
4265331Samw 			err = sa_share_zfs(share, (char *)path, &sh,
4275331Samw 			    &si, ZFS_SHARE_SMB);
4285331Samw 
4295331Samw 			sa_emptyshare(&sh);
4305331Samw 		}
4315331Samw 	}
4325331Samw 	if (!iszfs)
4335331Samw 		(void) sa_update_sharetab(share, "smb");
4345331Samw done:
4355331Samw 	sa_free_attr_string(path);
4365331Samw 
4375331Samw 	return (err == NERR_DuplicateShare ? 0 : err);
4385331Samw }
4395331Samw 
4405331Samw /*
4415331Samw  * This is the share for CIFS all shares have resource names.
4425331Samw  * Enable tells the smb server to update its hash. If it fails
4435331Samw  * because smb server is down, we just ignore as smb server loads
4445331Samw  * the resources from sharemanager at startup.
4455331Samw  */
4465331Samw 
4475331Samw static int
4485331Samw smb_enable_resource(sa_resource_t resource)
4495331Samw {
4505331Samw 	sa_share_t share;
4517052Samw 	smb_share_t si;
4525772Sas200622 	int ret = SA_OK;
4535772Sas200622 	int err;
4545772Sas200622 	boolean_t isonline;
4555331Samw 
4565331Samw 	share = sa_get_resource_parent(resource);
4575331Samw 	if (share == NULL)
4585331Samw 		return (SA_NO_SUCH_PATH);
4595772Sas200622 
4605772Sas200622 	/*
4615772Sas200622 	 * If administratively disabled, don't try to start anything.
4625772Sas200622 	 */
4635772Sas200622 	isonline = smb_isonline();
4645772Sas200622 	if (!isonline && !smb_isautoenable() && smb_isdisabled())
4657348SJose.Borrego@Sun.COM 		return (SA_OK);
4665772Sas200622 
4677348SJose.Borrego@Sun.COM 	if (!isonline) {
4687348SJose.Borrego@Sun.COM 		(void) smb_enable_service();
4697348SJose.Borrego@Sun.COM 
4707348SJose.Borrego@Sun.COM 		if (!smb_isonline())
4717348SJose.Borrego@Sun.COM 			return (SA_OK);
4725772Sas200622 	}
4735772Sas200622 
4747348SJose.Borrego@Sun.COM 	if ((ret = smb_build_shareinfo(share, resource, &si)) != SA_OK)
4757348SJose.Borrego@Sun.COM 		return (ret);
4765772Sas200622 
4775772Sas200622 	/*
4785772Sas200622 	 * Attempt to add the share. Any error that occurs if it was
4795772Sas200622 	 * online is an error but don't count NERR_DuplicateName if
4805772Sas200622 	 * smb/server had to be brought online since bringing the
4815772Sas200622 	 * service up will enable the share that was just added prior
4825772Sas200622 	 * to the attempt to enable.
4835772Sas200622 	 */
4847348SJose.Borrego@Sun.COM 	err = smb_share_create(&si);
4855772Sas200622 	if (err == NERR_Success || !(!isonline && err == NERR_DuplicateName))
4865772Sas200622 		(void) sa_update_sharetab(share, "smb");
4875772Sas200622 	else
4885331Samw 		return (SA_NOT_SHARED);
4895331Samw 
4907348SJose.Borrego@Sun.COM 	return (SA_OK);
4915331Samw }
4925331Samw 
4935331Samw /*
4945331Samw  * Remove it from smb server hash.
4955331Samw  */
4965331Samw static int
4975331Samw smb_disable_resource(sa_resource_t resource)
4985331Samw {
4995331Samw 	char *rname;
5007052Samw 	uint32_t res;
5015331Samw 	sa_share_t share;
5025331Samw 
5035331Samw 	rname = sa_get_resource_attr(resource, "name");
5045331Samw 	if (rname == NULL)
5055331Samw 		return (SA_NO_SUCH_RESOURCE);
5065331Samw 
5075331Samw 	if (smb_isonline()) {
5087348SJose.Borrego@Sun.COM 		res = smb_share_delete(rname);
5095331Samw 		if (res != NERR_Success) {
5105331Samw 			sa_free_attr_string(rname);
5115331Samw 			return (SA_CONFIG_ERR);
5125331Samw 		}
5135331Samw 	}
5145951Sdougm 
5155951Sdougm 	sa_free_attr_string(rname);
5165951Sdougm 
5175331Samw 	share = sa_get_resource_parent(resource);
5185331Samw 	if (share != NULL) {
5195331Samw 		rname = sa_get_share_attr(share, "path");
5205331Samw 		if (rname != NULL) {
5215951Sdougm 			sa_handle_t handle;
5225951Sdougm 
5235951Sdougm 			handle = sa_find_group_handle((sa_group_t)resource);
5245951Sdougm 			(void) sa_delete_sharetab(handle, rname, "smb");
5255331Samw 			sa_free_attr_string(rname);
5265331Samw 		}
5275331Samw 	}
5285331Samw 	/*
5295331Samw 	 * Always return OK as smb/server may be down and
5305331Samw 	 * Shares will be picked up when loaded.
5315331Samw 	 */
5325331Samw 	return (SA_OK);
5335331Samw }
5345331Samw 
5355331Samw /*
5365331Samw  * smb_share_changed(sa_share_t share)
5375331Samw  *
5385331Samw  * The specified share has changed.
5395331Samw  */
5405331Samw static int
5415331Samw smb_share_changed(sa_share_t share)
5425331Samw {
5435331Samw 	char *path;
5445331Samw 	sa_resource_t resource;
5455331Samw 
5467348SJose.Borrego@Sun.COM 	if (!smb_isonline())
5477348SJose.Borrego@Sun.COM 		return (SA_OK);
5487348SJose.Borrego@Sun.COM 
5495331Samw 	/* get the path since it is important in several places */
5505331Samw 	path = sa_get_share_attr(share, "path");
5515331Samw 	if (path == NULL)
5525331Samw 		return (SA_NO_SUCH_PATH);
5537348SJose.Borrego@Sun.COM 
5545331Samw 	for (resource = sa_get_share_resource(share, NULL);
5555331Samw 	    resource != NULL;
5565331Samw 	    resource = sa_get_next_resource(resource))
5575331Samw 		(void) smb_resource_changed(resource);
5585331Samw 
5595331Samw 	sa_free_attr_string(path);
5605331Samw 
5615331Samw 	return (SA_OK);
5625331Samw }
5635331Samw 
5645331Samw /*
5655331Samw  * smb_resource_changed(sa_resource_t resource)
5665331Samw  *
5675331Samw  * The specified resource has changed.
5685331Samw  */
5695331Samw static int
5705331Samw smb_resource_changed(sa_resource_t resource)
5715331Samw {
5727052Samw 	uint32_t res;
5737348SJose.Borrego@Sun.COM 	sa_share_t share;
5747052Samw 	smb_share_t si;
5755331Samw 
5767348SJose.Borrego@Sun.COM 	if (!smb_isonline())
5777348SJose.Borrego@Sun.COM 		return (SA_OK);
5785331Samw 
5797348SJose.Borrego@Sun.COM 	if ((share = sa_get_resource_parent(resource)) == NULL)
5807348SJose.Borrego@Sun.COM 		return (SA_CONFIG_ERR);
5815331Samw 
5827348SJose.Borrego@Sun.COM 	if ((res = smb_build_shareinfo(share, resource, &si)) != SA_OK)
5837348SJose.Borrego@Sun.COM 		return (res);
5845331Samw 
5857348SJose.Borrego@Sun.COM 	res = smb_share_modify(si.shr_name, si.shr_cmnt, si.shr_container);
5867348SJose.Borrego@Sun.COM 
5877348SJose.Borrego@Sun.COM 	if (res != NERR_Success)
5885331Samw 		return (SA_CONFIG_ERR);
5895331Samw 
5905331Samw 	return (smb_enable_service());
5915331Samw }
5925331Samw 
5935331Samw /*
5945800Sdougm  * smb_disable_share(sa_share_t share, char *path)
5955331Samw  *
5965800Sdougm  * Unshare the specified share. Note that "path" is the same
5975800Sdougm  * path as what is in the "share" object. It is passed in to avoid an
5985800Sdougm  * additional lookup. A missing "path" value makes this a no-op
5995800Sdougm  * function.
6005331Samw  */
6015331Samw static int
6025331Samw smb_disable_share(sa_share_t share, char *path)
6035331Samw {
6045331Samw 	char *rname;
6055331Samw 	sa_resource_t resource;
6065800Sdougm 	sa_group_t parent;
6075331Samw 	boolean_t iszfs;
6085331Samw 	int err = SA_OK;
6095951Sdougm 	sa_handle_t handle;
610*7483SDoug.McCallum@Sun.COM 	boolean_t first = B_TRUE; /* work around sharetab issue */
6115331Samw 
6125800Sdougm 	if (path == NULL)
6135800Sdougm 		return (err);
6145800Sdougm 
6155800Sdougm 	/*
6165800Sdougm 	 * If the share is in a ZFS group we need to handle it
6175800Sdougm 	 * differently.  Just being on a ZFS file system isn't
6185800Sdougm 	 * enough since we may be in a legacy share case.
6195800Sdougm 	 */
6205800Sdougm 	parent = sa_get_parent_group(share);
6215800Sdougm 	iszfs = sa_group_is_zfs(parent);
6225800Sdougm 
6235331Samw 	if (!smb_isonline())
6245331Samw 		goto done;
6255331Samw 
6265331Samw 	for (resource = sa_get_share_resource(share, NULL);
627*7483SDoug.McCallum@Sun.COM 	    resource != NULL || err != SA_OK;
6285331Samw 	    resource = sa_get_next_resource(resource)) {
6295331Samw 		rname = sa_get_resource_attr(resource, "name");
6305331Samw 		if (rname == NULL) {
6315331Samw 			continue;
6325331Samw 		}
6335331Samw 		if (!iszfs) {
6347348SJose.Borrego@Sun.COM 			err = smb_share_delete(rname);
6355331Samw 			switch (err) {
6365331Samw 			case NERR_NetNameNotFound:
6375331Samw 			case NERR_Success:
6385331Samw 				err = SA_OK;
6395331Samw 				break;
6405331Samw 			default:
6415331Samw 				err = SA_CONFIG_ERR;
6425331Samw 				break;
6435331Samw 			}
6445331Samw 		} else {
6455331Samw 			share_t sh;
6465331Samw 
6475331Samw 			sa_sharetab_fill_zfs(share, &sh, "smb");
6485331Samw 			err = sa_share_zfs(share, (char *)path, &sh,
6495331Samw 			    rname, ZFS_UNSHARE_SMB);
650*7483SDoug.McCallum@Sun.COM 			/*
651*7483SDoug.McCallum@Sun.COM 			 * If we are no longer the first case, we
652*7483SDoug.McCallum@Sun.COM 			 * don't care about the sa_share_zfs err if it
653*7483SDoug.McCallum@Sun.COM 			 * is -1. This works around a problem in
654*7483SDoug.McCallum@Sun.COM 			 * sharefs and should be removed when sharefs
655*7483SDoug.McCallum@Sun.COM 			 * supports multiple entries per path.
656*7483SDoug.McCallum@Sun.COM 			 */
657*7483SDoug.McCallum@Sun.COM 			if (!first && err == -1)
658*7483SDoug.McCallum@Sun.COM 				err = SA_OK;
659*7483SDoug.McCallum@Sun.COM 			first = B_FALSE;
660*7483SDoug.McCallum@Sun.COM 
6615331Samw 			sa_emptyshare(&sh);
6625331Samw 		}
6635331Samw 		sa_free_attr_string(rname);
6645331Samw 	}
6655331Samw done:
6665951Sdougm 	if (!iszfs) {
6675951Sdougm 		handle = sa_find_group_handle((sa_group_t)share);
6685951Sdougm 		if (handle != NULL)
6695951Sdougm 			(void) sa_delete_sharetab(handle, path, "smb");
6705951Sdougm 		else
6715951Sdougm 			err = SA_SYSTEM_ERR;
6725951Sdougm 	}
6735331Samw 	return (err);
6745331Samw }
6755331Samw 
6765331Samw /*
6776214Sdougm  * smb_validate_property(handle, property, parent)
6785331Samw  *
6795331Samw  * Check that the property has a legitimate value for its type.
6806214Sdougm  * Handle isn't currently used but may need to be in the future.
6815331Samw  */
6825331Samw 
6836214Sdougm /*ARGSUSED*/
6845331Samw static int
6856214Sdougm smb_validate_property(sa_handle_t handle, sa_property_t property,
6866214Sdougm     sa_optionset_t parent)
6875331Samw {
6885331Samw 	int ret = SA_OK;
6895331Samw 	char *propname;
6905331Samw 	int optindex;
6915331Samw 	sa_group_t parent_group;
6925331Samw 	char *value;
6935331Samw 
6945331Samw 	propname = sa_get_property_attr(property, "type");
6955331Samw 
6965331Samw 	if ((optindex = findopt(propname)) < 0)
6975331Samw 		ret = SA_NO_SUCH_PROP;
6985331Samw 
6995331Samw 	/* need to validate value range here as well */
7005331Samw 	if (ret == SA_OK) {
7015331Samw 		parent_group = sa_get_parent_group((sa_share_t)parent);
7025331Samw 		if (optdefs[optindex].share && !sa_is_share(parent_group))
7035331Samw 			ret = SA_PROP_SHARE_ONLY;
7045331Samw 	}
7055331Samw 	if (ret != SA_OK) {
7065331Samw 		if (propname != NULL)
7075331Samw 			sa_free_attr_string(propname);
7085331Samw 		return (ret);
7095331Samw 	}
7105331Samw 
7115331Samw 	value = sa_get_property_attr(property, "value");
7125331Samw 	if (value != NULL) {
7135331Samw 		/* first basic type checking */
7145331Samw 		switch (optdefs[optindex].type) {
7155331Samw 		case OPT_TYPE_NUMBER:
7165331Samw 			/* check that the value is all digits */
7175331Samw 			if (!is_a_number(value))
7185331Samw 				ret = SA_BAD_VALUE;
7195331Samw 			break;
7205331Samw 		case OPT_TYPE_BOOLEAN:
7215331Samw 			if (strlen(value) == 0 ||
7225331Samw 			    strcasecmp(value, "true") == 0 ||
7235331Samw 			    strcmp(value, "1") == 0 ||
7245331Samw 			    strcasecmp(value, "false") == 0 ||
7255331Samw 			    strcmp(value, "0") == 0) {
7265331Samw 				ret = SA_OK;
7275331Samw 			} else {
7285331Samw 				ret = SA_BAD_VALUE;
7295331Samw 			}
7305331Samw 			break;
7315331Samw 		case OPT_TYPE_NAME:
7325331Samw 			/*
7335331Samw 			 * Make sure no invalid characters
7345331Samw 			 */
7355331Samw 			if (validresource(value) == B_FALSE)
7365331Samw 				ret = SA_BAD_VALUE;
7375331Samw 			break;
7385331Samw 		case OPT_TYPE_STRING:
7395331Samw 			/* whatever is here should be ok */
7405331Samw 			break;
7415331Samw 		default:
7425331Samw 			break;
7435331Samw 		}
7445331Samw 	}
7455331Samw 
7465331Samw 	if (value != NULL)
7475331Samw 		sa_free_attr_string(value);
7485331Samw 	if (ret == SA_OK && optdefs[optindex].check != NULL)
7495331Samw 		/* do the property specific check */
7505331Samw 		ret = optdefs[optindex].check(property);
7515331Samw 
7525331Samw 	if (propname != NULL)
7535331Samw 		sa_free_attr_string(propname);
7545331Samw 	return (ret);
7555331Samw }
7565331Samw 
7575331Samw /*
7585331Samw  * Protocol management functions
7595331Samw  *
7605331Samw  * properties defined in the default files are defined in
7615331Samw  * proto_option_defs for parsing and validation.
7625331Samw  */
7635331Samw 
7645331Samw struct smb_proto_option_defs {
7655331Samw 	int smb_index;
7665331Samw 	int32_t minval;
7675331Samw 	int32_t maxval; /* In case of length of string this should be max */
7685331Samw 	int (*validator)(int, char *);
7695331Samw 	int32_t	refresh;
7705331Samw } smb_proto_options[] = {
7715772Sas200622 	{ SMB_CI_SYS_CMNT, 0, MAX_VALUE_BUFLEN,
7725772Sas200622 	    string_length_check_validator, SMB_REFRESH_REFRESH },
7735772Sas200622 	{ SMB_CI_MAX_WORKERS, 64, 1024, range_check_validator,
7745772Sas200622 	    SMB_REFRESH_REFRESH },
7755772Sas200622 	{ SMB_CI_NBSCOPE, 0, MAX_VALUE_BUFLEN,
7765772Sas200622 	    string_length_check_validator, 0 },
7775772Sas200622 	{ SMB_CI_LM_LEVEL, 2, 5, range_check_validator, 0 },
7785772Sas200622 	{ SMB_CI_KEEPALIVE, 20, 5400, range_check_validator_zero_ok,
7795772Sas200622 	    SMB_REFRESH_REFRESH },
7805772Sas200622 	{ SMB_CI_WINS_SRV1, 0, MAX_VALUE_BUFLEN,
7815772Sas200622 	    ip_address_validator_empty_ok, SMB_REFRESH_REFRESH },
7825772Sas200622 	{ SMB_CI_WINS_SRV2, 0, MAX_VALUE_BUFLEN,
7835772Sas200622 	    ip_address_validator_empty_ok, SMB_REFRESH_REFRESH },
7845772Sas200622 	{ SMB_CI_WINS_EXCL, 0, MAX_VALUE_BUFLEN,
7855772Sas200622 	    ip_address_csv_list_validator_empty_ok, SMB_REFRESH_REFRESH },
7865772Sas200622 	{ SMB_CI_SIGNING_ENABLE, 0, 0, true_false_validator,
7875772Sas200622 	    SMB_REFRESH_REFRESH },
7885772Sas200622 	{ SMB_CI_SIGNING_REQD, 0, 0, true_false_validator,
7895772Sas200622 	    SMB_REFRESH_REFRESH },
7905772Sas200622 	{ SMB_CI_RESTRICT_ANON, 0, 0, true_false_validator,
7915772Sas200622 	    SMB_REFRESH_REFRESH },
7925772Sas200622 	{ SMB_CI_DOMAIN_SRV, 0, MAX_VALUE_BUFLEN,
7935772Sas200622 	    ip_address_validator_empty_ok, 0 },
7945772Sas200622 	{ SMB_CI_ADS_SITE, 0, MAX_VALUE_BUFLEN,
7955772Sas200622 	    string_length_check_validator, SMB_REFRESH_REFRESH },
7965772Sas200622 	{ SMB_CI_DYNDNS_ENABLE, 0, 0, true_false_validator, 0 },
7975772Sas200622 	{ SMB_CI_AUTOHOME_MAP, 0, MAX_VALUE_BUFLEN, path_validator, 0 },
7985331Samw };
7995331Samw 
8005772Sas200622 #define	SMB_OPT_NUM \
8015772Sas200622 	(sizeof (smb_proto_options) / sizeof (smb_proto_options[0]))
8025772Sas200622 
8035331Samw /*
8045331Samw  * Check the range of value as int range.
8055331Samw  */
8065331Samw static int
8075331Samw range_check_validator(int index, char *value)
8085331Samw {
8095331Samw 	int ret = SA_OK;
8105331Samw 
8115331Samw 	if (!is_a_number(value)) {
8125331Samw 		ret = SA_BAD_VALUE;
8135331Samw 	} else {
8145331Samw 		int val;
8155331Samw 		val = strtoul(value, NULL, 0);
8165331Samw 		if (val < smb_proto_options[index].minval ||
8175331Samw 		    val > smb_proto_options[index].maxval)
8185331Samw 			ret = SA_BAD_VALUE;
8195331Samw 	}
8205331Samw 	return (ret);
8215331Samw }
8225331Samw 
8235331Samw /*
8245331Samw  * Check the range of value as int range.
8255331Samw  */
8265331Samw static int
8275331Samw range_check_validator_zero_ok(int index, char *value)
8285331Samw {
8295331Samw 	int ret = SA_OK;
8305331Samw 
8315331Samw 	if (!is_a_number(value)) {
8325331Samw 		ret = SA_BAD_VALUE;
8335331Samw 	} else {
8345331Samw 		int val;
8355331Samw 		val = strtoul(value, NULL, 0);
8365331Samw 		if (val == 0)
8375331Samw 			ret = SA_OK;
8385331Samw 		else {
8395331Samw 			if (val < smb_proto_options[index].minval ||
8405331Samw 			    val > smb_proto_options[index].maxval)
8415331Samw 			ret = SA_BAD_VALUE;
8425331Samw 		}
8435331Samw 	}
8445331Samw 	return (ret);
8455331Samw }
8465331Samw 
8475331Samw /*
8485331Samw  * Check the length of the string
8495331Samw  */
8505331Samw static int
8515331Samw string_length_check_validator(int index, char *value)
8525331Samw {
8535331Samw 	int ret = SA_OK;
8545331Samw 
8555331Samw 	if (value == NULL)
8565331Samw 		return (SA_BAD_VALUE);
8575331Samw 	if (strlen(value) > smb_proto_options[index].maxval)
8585331Samw 		ret = SA_BAD_VALUE;
8595331Samw 	return (ret);
8605331Samw }
8615331Samw 
8625331Samw /*
8635331Samw  * Check yes/no
8645331Samw  */
8655331Samw /*ARGSUSED*/
8665331Samw static int
8675331Samw true_false_validator(int index, char *value)
8685331Samw {
8695331Samw 	if (value == NULL)
8705331Samw 		return (SA_BAD_VALUE);
8715331Samw 	if ((strcasecmp(value, "true") == 0) ||
8725331Samw 	    (strcasecmp(value, "false") == 0))
8735331Samw 		return (SA_OK);
8745331Samw 	return (SA_BAD_VALUE);
8755331Samw }
8765331Samw 
8775331Samw /*
8785331Samw  * Check IP address.
8795331Samw  */
8805331Samw /*ARGSUSED*/
8815331Samw static int
8825331Samw ip_address_validator_empty_ok(int index, char *value)
8835331Samw {
8845331Samw 	char sbytes[16];
8855331Samw 	int len;
8865331Samw 
8875331Samw 	if (value == NULL)
8885331Samw 		return (SA_OK);
8895331Samw 	len = strlen(value);
8905331Samw 	if (len == 0)
8915331Samw 		return (SA_OK);
8925331Samw 	if (inet_pton(AF_INET, value, (void *)sbytes) != 1)
8935331Samw 		return (SA_BAD_VALUE);
8945331Samw 
8955331Samw 	return (SA_OK);
8965331Samw }
8975331Samw 
8985331Samw /*
8995331Samw  * Check IP address list
9005331Samw  */
9015331Samw /*ARGSUSED*/
9025331Samw static int
9035331Samw ip_address_csv_list_validator_empty_ok(int index, char *value)
9045331Samw {
9055331Samw 	char sbytes[16];
9065331Samw 	char *ip, *tmp, *ctx;
9075331Samw 
9085331Samw 	if (value == NULL || *value == '\0')
9095331Samw 		return (SA_OK);
9105331Samw 
9115331Samw 	if (strlen(value) > MAX_VALUE_BUFLEN)
9125331Samw 		return (SA_BAD_VALUE);
9135331Samw 
9145331Samw 	if ((tmp = strdup(value)) == NULL)
9155331Samw 		return (SA_NO_MEMORY);
9165331Samw 
9175331Samw 	ip = strtok_r(tmp, ",", &ctx);
9185331Samw 	while (ip) {
9195331Samw 		if (strlen(ip) == 0) {
9205331Samw 			free(tmp);
9215331Samw 			return (SA_BAD_VALUE);
9225331Samw 		}
9235331Samw 		if (*ip != 0) {
9245331Samw 			if (inet_pton(AF_INET, ip,
9255331Samw 			    (void *)sbytes) != 1) {
9265331Samw 				free(tmp);
9275331Samw 				return (SA_BAD_VALUE);
9285331Samw 			}
9295331Samw 		}
9305331Samw 		ip = strtok_r(0, ",", &ctx);
9315331Samw 	}
9325331Samw 
9335331Samw 	free(tmp);
9345331Samw 	return (SA_OK);
9355331Samw }
9365331Samw 
9375331Samw /*
9385331Samw  * Check path
9395331Samw  */
9405331Samw /*ARGSUSED*/
9415331Samw static int
9425331Samw path_validator(int index, char *value)
9435331Samw {
9445331Samw 	struct stat buffer;
9455331Samw 	int fd, status;
9465331Samw 
9475331Samw 	if (value == NULL)
9485331Samw 		return (SA_BAD_VALUE);
9495331Samw 
9505331Samw 	fd = open(value, O_RDONLY);
9515331Samw 	if (fd < 0)
9525331Samw 		return (SA_BAD_VALUE);
9535331Samw 
9545331Samw 	status = fstat(fd, &buffer);
9555331Samw 	(void) close(fd);
9565331Samw 
9575331Samw 	if (status < 0)
9585331Samw 		return (SA_BAD_VALUE);
9595331Samw 
9605331Samw 	if (buffer.st_mode & S_IFDIR)
9615331Samw 		return (SA_OK);
9625331Samw 	return (SA_BAD_VALUE);
9635331Samw }
9645331Samw 
9655331Samw /*
9665331Samw  * the protoset holds the defined options so we don't have to read
9675331Samw  * them multiple times
9685331Samw  */
9695331Samw static sa_protocol_properties_t protoset;
9705331Samw 
9715331Samw static int
9725331Samw findprotoopt(char *name)
9735331Samw {
9745331Samw 	int i;
9755772Sas200622 	char *sc_name;
9765772Sas200622 
9775772Sas200622 	for (i = 0; i < SMB_OPT_NUM; i++) {
9785772Sas200622 		sc_name = smb_config_getname(smb_proto_options[i].smb_index);
9795772Sas200622 		if (strcasecmp(sc_name, name) == 0)
9805331Samw 			return (i);
9815331Samw 	}
9825772Sas200622 
9835331Samw 	return (-1);
9845331Samw }
9855331Samw 
9865331Samw /*
9875331Samw  * smb_load_proto_properties()
9885331Samw  *
9895331Samw  * read the smb config values from SMF.
9905331Samw  */
9915331Samw 
9925331Samw static int
9935331Samw smb_load_proto_properties()
9945331Samw {
9955331Samw 	sa_property_t prop;
9965772Sas200622 	char value[MAX_VALUE_BUFLEN];
9975772Sas200622 	char *name;
9985331Samw 	int index;
9996019Sdougm 	int ret = SA_OK;
10005772Sas200622 	int rc;
10015331Samw 
10025331Samw 	protoset = sa_create_protocol_properties(SMB_PROTOCOL_NAME);
10035331Samw 	if (protoset == NULL)
10045331Samw 		return (SA_NO_MEMORY);
10055331Samw 
10066019Sdougm 	for (index = 0; index < SMB_OPT_NUM && ret == SA_OK; index++) {
10075772Sas200622 		rc = smb_config_get(smb_proto_options[index].smb_index,
10085772Sas200622 		    value, sizeof (value));
10095772Sas200622 		if (rc != SMBD_SMF_OK)
10105772Sas200622 			continue;
10115772Sas200622 		name = smb_config_getname(smb_proto_options[index].smb_index);
10125772Sas200622 		prop = sa_create_property(name, value);
10135454Sdougm 		if (prop != NULL)
10146019Sdougm 			ret = sa_add_protocol_property(protoset, prop);
10156019Sdougm 		else
10166019Sdougm 			ret = SA_NO_MEMORY;
10175331Samw 	}
10186019Sdougm 	return (ret);
10195331Samw }
10205331Samw 
10215331Samw /*
10225331Samw  * smb_share_init()
10235331Samw  *
10245331Samw  * Initialize the smb plugin.
10255331Samw  */
10265331Samw 
10275331Samw static int
10285331Samw smb_share_init(void)
10295331Samw {
10305331Samw 	if (sa_plugin_ops.sa_init != smb_share_init)
10315331Samw 		return (SA_SYSTEM_ERR);
10325331Samw 
10337348SJose.Borrego@Sun.COM 	return (smb_load_proto_properties());
10345331Samw }
10355331Samw 
10365331Samw /*
10375331Samw  * smb_share_fini()
10385331Samw  *
10395331Samw  */
10405331Samw static void
10415331Samw smb_share_fini(void)
10425331Samw {
10435331Samw 	xmlFreeNode(protoset);
10445331Samw 	protoset = NULL;
10455772Sas200622 
10467052Samw 	(void) smb_share_dclose();
10475331Samw }
10485331Samw 
10495331Samw /*
10505331Samw  * smb_get_proto_set()
10515331Samw  *
10525331Samw  * Return an optionset with all the protocol specific properties in
10535331Samw  * it.
10545331Samw  */
10555331Samw static sa_protocol_properties_t
10565331Samw smb_get_proto_set(void)
10575331Samw {
10585331Samw 	return (protoset);
10595331Samw }
10605331Samw 
10615331Samw /*
10625772Sas200622  * smb_enable_dependencies()
10635772Sas200622  *
10645772Sas200622  * SMBD_DEFAULT_INSTANCE_FMRI may have some dependencies that aren't
10655772Sas200622  * enabled. This will attempt to enable all of them.
10665772Sas200622  */
10675772Sas200622 static void
10685772Sas200622 smb_enable_dependencies(const char *fmri)
10695772Sas200622 {
10705772Sas200622 	scf_handle_t *handle;
10715772Sas200622 	scf_service_t *service;
10725772Sas200622 	scf_instance_t *inst = NULL;
10735772Sas200622 	scf_iter_t *iter;
10745772Sas200622 	scf_property_t *prop;
10755772Sas200622 	scf_value_t *value;
10765772Sas200622 	scf_propertygroup_t *pg;
10775772Sas200622 	scf_scope_t *scope;
10785772Sas200622 	char type[SCFTYPE_LEN];
10795772Sas200622 	char *dependency;
10805772Sas200622 	char *servname;
10815772Sas200622 	int maxlen;
10825772Sas200622 
10835772Sas200622 	/*
10845772Sas200622 	 * Get all required handles and storage.
10855772Sas200622 	 */
10865772Sas200622 	handle = scf_handle_create(SCF_VERSION);
10875772Sas200622 	if (handle == NULL)
10885772Sas200622 		return;
10895772Sas200622 
10905772Sas200622 	if (scf_handle_bind(handle) != 0) {
10915772Sas200622 		scf_handle_destroy(handle);
10925772Sas200622 		return;
10935772Sas200622 	}
10945772Sas200622 
10955772Sas200622 	maxlen = scf_limit(SCF_LIMIT_MAX_VALUE_LENGTH);
10965772Sas200622 	if (maxlen == (ssize_t)-1)
10975772Sas200622 		maxlen = MAXPATHLEN;
10985772Sas200622 
10995772Sas200622 	dependency = malloc(maxlen);
11005772Sas200622 
11015772Sas200622 	service = scf_service_create(handle);
11025772Sas200622 
11035772Sas200622 	iter = scf_iter_create(handle);
11045772Sas200622 
11055772Sas200622 	pg = scf_pg_create(handle);
11065772Sas200622 
11075772Sas200622 	prop = scf_property_create(handle);
11085772Sas200622 
11095772Sas200622 	value = scf_value_create(handle);
11105772Sas200622 
11115772Sas200622 	scope = scf_scope_create(handle);
11125772Sas200622 
11135772Sas200622 	if (service == NULL || iter == NULL || pg == NULL || prop == NULL ||
11145772Sas200622 	    value == NULL || scope == NULL || dependency == NULL)
11155772Sas200622 		goto done;
11165772Sas200622 
11175772Sas200622 	/*
11185772Sas200622 	 *  We passed in the FMRI for the default instance but for
11195772Sas200622 	 *  some things we need the simple form so construct it. Since
11205772Sas200622 	 *  we reuse the storage that dependency points to, we need to
11215772Sas200622 	 *  use the servname early.
11225772Sas200622 	 */
11235772Sas200622 	(void) snprintf(dependency, maxlen, "%s", fmri + sizeof ("svc:"));
11245772Sas200622 	servname = strrchr(dependency, ':');
11255772Sas200622 	if (servname == NULL)
11265772Sas200622 		goto done;
11275772Sas200622 	*servname = '\0';
11285772Sas200622 	servname = dependency;
11295772Sas200622 
11305772Sas200622 	/*
11315772Sas200622 	 * Setup to iterate over the service property groups, only
11325772Sas200622 	 * looking at those that are "dependency" types. The "entity"
11335772Sas200622 	 * property will have the FMRI of the service we are dependent
11345772Sas200622 	 * on.
11355772Sas200622 	 */
11365772Sas200622 	if (scf_handle_get_scope(handle, SCF_SCOPE_LOCAL, scope) != 0)
11375772Sas200622 		goto done;
11385772Sas200622 
11395772Sas200622 	if (scf_scope_get_service(scope, servname, service) != 0)
11405772Sas200622 		goto done;
11415772Sas200622 
11425772Sas200622 	if (scf_iter_service_pgs(iter, service) != 0)
11435772Sas200622 		goto done;
11445772Sas200622 
11455772Sas200622 	while (scf_iter_next_pg(iter, pg) > 0) {
11465772Sas200622 		char *services[2];
11475772Sas200622 		/*
11485772Sas200622 		 * Have a property group for the service. See if it is
11495772Sas200622 		 * a dependency pg and only do operations on those.
11505772Sas200622 		 */
11515772Sas200622 		if (scf_pg_get_type(pg, type, SCFTYPE_LEN) <= 0)
11525772Sas200622 			continue;
11535772Sas200622 
11545772Sas200622 		if (strncmp(type, SCF_GROUP_DEPENDENCY, SCFTYPE_LEN) != 0)
11555772Sas200622 			continue;
11565772Sas200622 		/*
11575772Sas200622 		 * Have a dependency.  Attempt to enable it.
11585772Sas200622 		 */
11595772Sas200622 		if (scf_pg_get_property(pg, SCF_PROPERTY_ENTITIES, prop) != 0)
11605772Sas200622 			continue;
11615772Sas200622 
11625772Sas200622 		if (scf_property_get_value(prop, value) != 0)
11635772Sas200622 			continue;
11645772Sas200622 
11655772Sas200622 		services[1] = NULL;
11665772Sas200622 
11675772Sas200622 		if (scf_value_get_as_string(value, dependency, maxlen) > 0) {
11685772Sas200622 			services[0] = dependency;
11695772Sas200622 			_check_services(services);
11705772Sas200622 		}
11715772Sas200622 	}
11725772Sas200622 
11735772Sas200622 done:
11745772Sas200622 	if (dependency != NULL)
11755772Sas200622 		free(dependency);
11765772Sas200622 	if (value != NULL)
11775772Sas200622 		scf_value_destroy(value);
11785772Sas200622 	if (prop != NULL)
11795772Sas200622 		scf_property_destroy(prop);
11805772Sas200622 	if (pg != NULL)
11815772Sas200622 		scf_pg_destroy(pg);
11825772Sas200622 	if (iter != NULL)
11835772Sas200622 		scf_iter_destroy(iter);
11845772Sas200622 	if (scope != NULL)
11855772Sas200622 		scf_scope_destroy(scope);
11865772Sas200622 	if (inst != NULL)
11875772Sas200622 		scf_instance_destroy(inst);
11885772Sas200622 	if (service != NULL)
11895772Sas200622 		scf_service_destroy(service);
11905772Sas200622 
11915772Sas200622 	(void) scf_handle_unbind(handle);
11925772Sas200622 	scf_handle_destroy(handle);
11935772Sas200622 }
11945772Sas200622 
11955772Sas200622 /*
11965331Samw  * How long to wait for service to come online
11975331Samw  */
11985331Samw #define	WAIT_FOR_SERVICE	15
11995331Samw 
12005331Samw /*
12015331Samw  * smb_enable_service()
12025331Samw  *
12035331Samw  */
12045331Samw static int
12055331Samw smb_enable_service(void)
12065331Samw {
12075331Samw 	int i;
12085331Samw 	int ret = SA_OK;
12095772Sas200622 	char *service[] = { SMBD_DEFAULT_INSTANCE_FMRI, NULL };
12105331Samw 
12115331Samw 	if (!smb_isonline()) {
12125772Sas200622 		/*
12135772Sas200622 		 * Attempt to start the idmap, and other dependent
12145772Sas200622 		 * services, first.  If it fails, the SMB service will
12155772Sas200622 		 * ultimately fail so we use that as the error.  If we
12165772Sas200622 		 * don't try to enable idmap, smb won't start the
12175772Sas200622 		 * first time unless the admin has done it
12185772Sas200622 		 * manually. The service could be administratively
12195772Sas200622 		 * disabled so we won't always get started.
12205772Sas200622 		 */
12215772Sas200622 		smb_enable_dependencies(SMBD_DEFAULT_INSTANCE_FMRI);
12225772Sas200622 		_check_services(service);
12235331Samw 
12245331Samw 		/* Wait for service to come online */
12255331Samw 		for (i = 0; i < WAIT_FOR_SERVICE; i++) {
12265331Samw 			if (smb_isonline()) {
12275772Sas200622 				ret =  SA_OK;
12285331Samw 				break;
12296030Sjb150015 			} else if (smb_ismaint()) {
12306030Sjb150015 				/* maintenance requires help */
12316030Sjb150015 				ret = SA_SYSTEM_ERR;
12326030Sjb150015 				break;
12336030Sjb150015 			} else if (smb_isdisabled()) {
12346030Sjb150015 				/* disabled is ok */
12356030Sjb150015 				ret = SA_OK;
12366030Sjb150015 				break;
12375331Samw 			} else {
12386030Sjb150015 				/* try another time */
12395331Samw 				ret = SA_BUSY;
12405331Samw 				(void) sleep(1);
12415331Samw 			}
12425331Samw 		}
12435331Samw 	}
12445331Samw 	return (ret);
12455331Samw }
12465331Samw 
12475331Samw /*
12485331Samw  * smb_validate_proto_prop(index, name, value)
12495331Samw  *
12505331Samw  * Verify that the property specified by name can take the new
12515331Samw  * value. This is a sanity check to prevent bad values getting into
12525331Samw  * the default files.
12535331Samw  */
12545331Samw static int
12555331Samw smb_validate_proto_prop(int index, char *name, char *value)
12565331Samw {
12575331Samw 	if ((name == NULL) || (index < 0))
12585331Samw 		return (SA_BAD_VALUE);
12595331Samw 
12605331Samw 	if (smb_proto_options[index].validator == NULL)
12615331Samw 		return (SA_OK);
12625331Samw 
12635331Samw 	if (smb_proto_options[index].validator(index, value) == SA_OK)
12645331Samw 		return (SA_OK);
12655331Samw 	return (SA_BAD_VALUE);
12665331Samw }
12675331Samw 
12685331Samw /*
12695331Samw  * smb_set_proto_prop(prop)
12705331Samw  *
12715331Samw  * check that prop is valid.
12725331Samw  */
12735331Samw /*ARGSUSED*/
12745331Samw static int
12755331Samw smb_set_proto_prop(sa_property_t prop)
12765331Samw {
12775331Samw 	int ret = SA_OK;
12785331Samw 	char *name;
12795331Samw 	char *value;
12805331Samw 	int index = -1;
12815521Sas200622 	struct smb_proto_option_defs *opt;
12825331Samw 
12835331Samw 	name = sa_get_property_attr(prop, "type");
12845331Samw 	value = sa_get_property_attr(prop, "value");
12855331Samw 	if (name != NULL && value != NULL) {
12865331Samw 		index = findprotoopt(name);
12875331Samw 		if (index >= 0) {
12885331Samw 			/* should test for valid value */
12895331Samw 			ret = smb_validate_proto_prop(index, name, value);
12905331Samw 			if (ret == SA_OK) {
12915521Sas200622 				opt = &smb_proto_options[index];
12925521Sas200622 
12935331Samw 				/* Save to SMF */
12945772Sas200622 				(void) smb_config_set(opt->smb_index, value);
12955331Samw 				/*
12965331Samw 				 * Specialized refresh mechanisms can
12975331Samw 				 * be flagged in the proto_options and
12985331Samw 				 * processed here.
12995331Samw 				 */
13005521Sas200622 				if (opt->refresh & SMB_REFRESH_REFRESH)
13016139Sjb150015 					(void) smf_refresh_instance(
13026139Sjb150015 					    SMBD_DEFAULT_INSTANCE_FMRI);
13035521Sas200622 				else if (opt->refresh & SMB_REFRESH_RESTART)
13045331Samw 					(void) smf_restart_instance(
13055331Samw 					    SMBD_DEFAULT_INSTANCE_FMRI);
13065331Samw 			}
13075331Samw 		}
13085331Samw 	}
13095521Sas200622 
13105331Samw 	if (name != NULL)
13115331Samw 		sa_free_attr_string(name);
13125331Samw 	if (value != NULL)
13135331Samw 		sa_free_attr_string(value);
13145331Samw 
13155331Samw 	return (ret);
13165331Samw }
13175331Samw 
13185331Samw /*
13195331Samw  * smb_get_status()
13205331Samw  *
13215331Samw  * What is the current status of the smbd? We use the SMF state here.
13225331Samw  * Caller must free the returned value.
13235331Samw  */
13245331Samw 
13255331Samw static char *
13265331Samw smb_get_status(void)
13275331Samw {
13285331Samw 	char *state = NULL;
13295331Samw 	state = smf_get_state(SMBD_DEFAULT_INSTANCE_FMRI);
13305331Samw 	return (state != NULL ? state : "-");
13315331Samw }
13325331Samw 
13335331Samw /*
13345331Samw  * This protocol plugin require resource names
13355331Samw  */
13365331Samw static uint64_t
13375331Samw smb_share_features(void)
13385331Samw {
13395331Samw 	return (SA_FEATURE_RESOURCE | SA_FEATURE_ALLOWSUBDIRS |
13406088Sdougm 	    SA_FEATURE_ALLOWPARDIRS | SA_FEATURE_SERVER);
13415331Samw }
13425331Samw 
13435331Samw /*
13447348SJose.Borrego@Sun.COM  * This should be used to convert smb_share_t to sa_resource_t
13457348SJose.Borrego@Sun.COM  * Should only be needed to build transient shares/resources to be
13467348SJose.Borrego@Sun.COM  * supplied to sharemgr to display.
13475331Samw  */
13485331Samw static int
13497348SJose.Borrego@Sun.COM smb_add_transient(sa_handle_t handle, smb_share_t *si)
13505331Samw {
13515331Samw 	int err;
13525331Samw 	sa_share_t share;
13535331Samw 	sa_group_t group;
13545331Samw 	sa_resource_t resource;
13555331Samw 
13565331Samw 	if (si == NULL)
13575331Samw 		return (SA_INVALID_NAME);
13585331Samw 
13597348SJose.Borrego@Sun.COM 	if ((share = sa_find_share(handle, si->shr_path)) == NULL) {
13607348SJose.Borrego@Sun.COM 		if ((group = smb_get_defaultgrp(handle)) == NULL)
13617348SJose.Borrego@Sun.COM 			return (SA_NO_SUCH_GROUP);
13625331Samw 
13637052Samw 		share = sa_get_share(group, si->shr_path);
13645331Samw 		if (share == NULL) {
13657052Samw 			share = sa_add_share(group, si->shr_path,
13665331Samw 			    SA_SHARE_TRANSIENT, &err);
13675331Samw 			if (share == NULL)
13685331Samw 				return (SA_NO_SUCH_PATH);
13695331Samw 		}
13705331Samw 	}
13715331Samw 
13725331Samw 	/*
13735331Samw 	 * Now handle the resource. Make sure that the resource is
13745331Samw 	 * transient and added to the share.
13755331Samw 	 */
13767052Samw 	resource = sa_get_share_resource(share, si->shr_name);
13775331Samw 	if (resource == NULL) {
13785331Samw 		resource = sa_add_resource(share,
13797052Samw 		    si->shr_name, SA_SHARE_TRANSIENT, &err);
13805331Samw 		if (resource == NULL)
13815331Samw 			return (SA_NO_SUCH_RESOURCE);
13825331Samw 	}
13835331Samw 
13845331Samw 	/* set resource attributes now */
13857052Samw 	(void) sa_set_resource_attr(resource, "description", si->shr_cmnt);
13867052Samw 	(void) sa_set_resource_attr(resource, SMB_SHROPT_AD_CONTAINER,
13877052Samw 	    si->shr_container);
13885331Samw 
13895331Samw 	return (SA_OK);
13905331Samw }
13915331Samw 
13925331Samw /*
13937348SJose.Borrego@Sun.COM  * Return smb transient shares.
13945331Samw  */
13955331Samw static int
13965331Samw smb_list_transient(sa_handle_t handle)
13975331Samw {
13987348SJose.Borrego@Sun.COM 	int i, offset;
13997052Samw 	smb_shrlist_t list;
14005331Samw 	int res;
14015331Samw 
14027348SJose.Borrego@Sun.COM 	if (smb_share_count() <= 0)
14035331Samw 		return (SA_OK);
14047348SJose.Borrego@Sun.COM 
14055331Samw 	offset = 0;
14067348SJose.Borrego@Sun.COM 	while (smb_share_list(offset, &list) == NERR_Success) {
14077348SJose.Borrego@Sun.COM 		if (list.sl_cnt == 0)
14085331Samw 			break;
14097348SJose.Borrego@Sun.COM 
14107348SJose.Borrego@Sun.COM 		for (i = 0; i < list.sl_cnt; i++) {
14117348SJose.Borrego@Sun.COM 			res = smb_add_transient(handle, &(list.sl_shares[i]));
14125331Samw 			if (res != SA_OK)
14135331Samw 				return (res);
14145331Samw 		}
14157348SJose.Borrego@Sun.COM 		offset += list.sl_cnt;
14165331Samw 	}
14175331Samw 
14185331Samw 	return (SA_OK);
14195331Samw }
14205331Samw 
14215331Samw /*
14225331Samw  * fix_resource_name(share, name,  prefix)
14235331Samw  *
14245331Samw  * Construct a name where the ZFS dataset has the prefix replaced with "name".
14255331Samw  */
14265331Samw static char *
14275331Samw fix_resource_name(sa_share_t share, char *name, char *prefix)
14285331Samw {
14295331Samw 	char *dataset = NULL;
14305331Samw 	char *newname = NULL;
14315331Samw 	size_t psize;
14325331Samw 	size_t nsize;
14335331Samw 
14345331Samw 	dataset = sa_get_share_attr(share, "dataset");
14355331Samw 
14365331Samw 	if (dataset != NULL && strcmp(dataset, prefix) != 0) {
14375331Samw 		psize = strlen(prefix);
14385331Samw 		if (strncmp(dataset, prefix, psize) == 0) {
14395331Samw 			/* need string plus ',' and NULL */
14405331Samw 			nsize = (strlen(dataset) - psize) + strlen(name) + 2;
14415331Samw 			newname = calloc(nsize, 1);
14425331Samw 			if (newname != NULL) {
14435331Samw 				(void) snprintf(newname, nsize, "%s%s", name,
14445331Samw 				    dataset + psize);
14455331Samw 				sa_fix_resource_name(newname);
14465331Samw 			}
14475331Samw 			sa_free_attr_string(dataset);
14485331Samw 			return (newname);
14495331Samw 		}
14505331Samw 	}
14515331Samw 	if (dataset != NULL)
14525331Samw 		sa_free_attr_string(dataset);
14535331Samw 	return (strdup(name));
14545331Samw }
14555331Samw 
14565331Samw /*
14575331Samw  * smb_parse_optstring(group, options)
14585331Samw  *
14595331Samw  * parse a compact option string into individual options. This allows
14605331Samw  * ZFS sharesmb and sharemgr "share" command to work.  group can be a
14615331Samw  * group, a share or a resource.
14625331Samw  */
14635331Samw static int
14645331Samw smb_parse_optstring(sa_group_t group, char *options)
14655331Samw {
14665331Samw 	char *dup;
14675331Samw 	char *base;
14685331Samw 	char *lasts;
14695331Samw 	char *token;
14705331Samw 	sa_optionset_t optionset;
14715331Samw 	sa_group_t parent = NULL;
14725331Samw 	sa_resource_t resource = NULL;
14735331Samw 	int iszfs = 0;
14745331Samw 	int persist = 0;
14755331Samw 	int need_optionset = 0;
14765331Samw 	int ret = SA_OK;
14775331Samw 	sa_property_t prop;
14785331Samw 
14795331Samw 	/*
14805331Samw 	 * In order to not attempt to change ZFS properties unless
14815331Samw 	 * absolutely necessary, we never do it in the legacy parsing
14825331Samw 	 * so we need to keep track of this.
14835331Samw 	 */
14845331Samw 	if (sa_is_share(group)) {
14855331Samw 		char *zfs;
14865331Samw 
14875331Samw 		parent = sa_get_parent_group(group);
14885331Samw 		if (parent != NULL) {
14895331Samw 			zfs = sa_get_group_attr(parent, "zfs");
14905331Samw 			if (zfs != NULL) {
14915331Samw 				sa_free_attr_string(zfs);
14925331Samw 				iszfs = 1;
14935331Samw 			}
14945331Samw 		}
14955331Samw 	} else {
14965331Samw 		iszfs = sa_group_is_zfs(group);
14975331Samw 		/*
14985331Samw 		 * If a ZFS group, then we need to see if a resource
14995331Samw 		 * name is being set. If so, bail with
15005331Samw 		 * SA_PROP_SHARE_ONLY, so we come back in with a share
15015331Samw 		 * instead of a group.
15025331Samw 		 */
15035331Samw 		if (strncmp(options, "name=", sizeof ("name=") - 1) == 0 ||
15045331Samw 		    strstr(options, ",name=") != NULL) {
15055331Samw 			return (SA_PROP_SHARE_ONLY);
15065331Samw 		}
15075331Samw 	}
15085331Samw 
15095331Samw 	/* do we have an existing optionset? */
15105331Samw 	optionset = sa_get_optionset(group, "smb");
15115331Samw 	if (optionset == NULL) {
15125331Samw 		/* didn't find existing optionset so create one */
15135331Samw 		optionset = sa_create_optionset(group, "smb");
15145331Samw 		if (optionset == NULL)
15155331Samw 			return (SA_NO_MEMORY);
15165331Samw 	} else {
15175331Samw 		/*
15185331Samw 		 * If an optionset already exists, we've come through
15195331Samw 		 * twice so ignore the second time.
15205331Samw 		 */
15215331Samw 		return (ret);
15225331Samw 	}
15235331Samw 
15245331Samw 	/* We need a copy of options for the next part. */
15255331Samw 	dup = strdup(options);
15265331Samw 	if (dup == NULL)
15275331Samw 		return (SA_NO_MEMORY);
15285331Samw 
15295331Samw 	/*
15305331Samw 	 * SMB properties are straightforward and are strings,
15315331Samw 	 * integers or booleans.  Properties are separated by
15325331Samw 	 * commas. It will be necessary to parse quotes due to some
15335331Samw 	 * strings not having a restricted characters set.
15345331Samw 	 *
15355331Samw 	 * Note that names will create a resource. For now, if there
15365331Samw 	 * is a set of properties "before" the first name="", those
15375331Samw 	 * properties will be placed on the group.
15385331Samw 	 */
15395331Samw 	persist = sa_is_persistent(group);
15405331Samw 	base = dup;
15415331Samw 	token = dup;
15425331Samw 	lasts = NULL;
15435331Samw 	while (token != NULL && ret == SA_OK) {
15445331Samw 		ret = SA_OK;
15455331Samw 		token = strtok_r(base, ",", &lasts);
15465331Samw 		base = NULL;
15475331Samw 		if (token != NULL) {
15485331Samw 			char *value;
15495331Samw 			/*
15505331Samw 			 * All SMB properties have values so there
15515331Samw 			 * MUST be an '=' character.  If it doesn't,
15525331Samw 			 * it is a syntax error.
15535331Samw 			 */
15545331Samw 			value = strchr(token, '=');
15555331Samw 			if (value != NULL) {
15565331Samw 				*value++ = '\0';
15575331Samw 			} else {
15585331Samw 				ret = SA_SYNTAX_ERR;
15595331Samw 				break;
15605331Samw 			}
15615331Samw 			/*
15625331Samw 			 * We may need to handle a "name" property
15635331Samw 			 * that is a ZFS imposed resource name. Each
15645331Samw 			 * name would trigger getting a new "resource"
15655331Samw 			 * to put properties on. For now, assume no
15665331Samw 			 * "name" property for special handling.
15675331Samw 			 */
15685331Samw 
15695331Samw 			if (strcmp(token, "name") == 0) {
15705331Samw 				char *prefix;
15715331Samw 				char *name = NULL;
15725331Samw 				/*
15735331Samw 				 * We have a name, so now work on the
15745331Samw 				 * resource level. We have a "share"
15755331Samw 				 * in "group" due to the caller having
15765331Samw 				 * added it. If we are called with a
15775331Samw 				 * group, the check for group/share
15785331Samw 				 * at the beginning of this function
15795331Samw 				 * will bail out the parse if there is a
15805331Samw 				 * "name" but no share.
15815331Samw 				 */
15825331Samw 				if (!iszfs) {
15835331Samw 					ret = SA_SYNTAX_ERR;
15845331Samw 					break;
15855331Samw 				}
15865331Samw 				/*
15875331Samw 				 * Make sure the parent group has the
15885331Samw 				 * "prefix" property since we will
15895331Samw 				 * need to use this for constructing
15905331Samw 				 * inherited name= values.
15915331Samw 				 */
15925331Samw 				prefix = sa_get_group_attr(parent, "prefix");
15935331Samw 				if (prefix == NULL) {
15945331Samw 					prefix = sa_get_group_attr(parent,
15955331Samw 					    "name");
15965331Samw 					if (prefix != NULL) {
15975331Samw 						(void) sa_set_group_attr(parent,
15985331Samw 						    "prefix", prefix);
15995331Samw 					}
16005331Samw 				}
16015331Samw 				name = fix_resource_name((sa_share_t)group,
16025331Samw 				    value, prefix);
16035331Samw 				if (name != NULL) {
16045331Samw 					resource = sa_add_resource(
16055331Samw 					    (sa_share_t)group, name,
16065331Samw 					    SA_SHARE_TRANSIENT, &ret);
16075331Samw 					sa_free_attr_string(name);
16085331Samw 				} else {
16095331Samw 					ret = SA_NO_MEMORY;
16105331Samw 				}
16115331Samw 				if (prefix != NULL)
16125331Samw 					sa_free_attr_string(prefix);
16135331Samw 
16145331Samw 				/* A resource level optionset is needed */
16155331Samw 
16165331Samw 				need_optionset = 1;
16175331Samw 				if (resource == NULL) {
16185331Samw 					ret = SA_NO_MEMORY;
16195331Samw 					break;
16205331Samw 				}
16215331Samw 				continue;
16225331Samw 			}
16235331Samw 
16245331Samw 			if (need_optionset) {
16255331Samw 				optionset = sa_create_optionset(resource,
16265331Samw 				    "smb");
16275331Samw 				need_optionset = 0;
16285331Samw 			}
16295331Samw 
16305331Samw 			prop = sa_create_property(token, value);
16315331Samw 			if (prop == NULL)
16325331Samw 				ret = SA_NO_MEMORY;
16335331Samw 			else
16345331Samw 				ret = sa_add_property(optionset, prop);
16355331Samw 			if (ret != SA_OK)
16365331Samw 				break;
16375331Samw 			if (!iszfs)
16385331Samw 				ret = sa_commit_properties(optionset, !persist);
16395331Samw 		}
16405331Samw 	}
16415331Samw 	free(dup);
16425331Samw 	return (ret);
16435331Samw }
16445331Samw 
16455331Samw /*
16465331Samw  * smb_sprint_option(rbuff, rbuffsize, incr, prop, sep)
16475331Samw  *
16485331Samw  * provides a mechanism to format SMB properties into legacy output
16495331Samw  * format. If the buffer would overflow, it is reallocated and grown
16505331Samw  * as appropriate. Special cases of converting internal form of values
16515331Samw  * to those used by "share" are done. this function does one property
16525331Samw  * at a time.
16535331Samw  */
16545331Samw 
16555331Samw static void
16565331Samw smb_sprint_option(char **rbuff, size_t *rbuffsize, size_t incr,
16575331Samw 			sa_property_t prop, int sep)
16585331Samw {
16595331Samw 	char *name;
16605331Samw 	char *value;
16615331Samw 	int curlen;
16625331Samw 	char *buff = *rbuff;
16635331Samw 	size_t buffsize = *rbuffsize;
16645331Samw 
16655331Samw 	name = sa_get_property_attr(prop, "type");
16665331Samw 	value = sa_get_property_attr(prop, "value");
16675331Samw 	if (buff != NULL)
16685331Samw 		curlen = strlen(buff);
16695331Samw 	else
16705331Samw 		curlen = 0;
16715331Samw 	if (name != NULL) {
16725331Samw 		int len;
16735331Samw 		len = strlen(name) + sep;
16745331Samw 
16755331Samw 		/*
16765331Samw 		 * A future RFE would be to replace this with more
16775331Samw 		 * generic code and to possibly handle more types.
16785331Samw 		 *
16795331Samw 		 * For now, everything else is treated as a string. If
16805331Samw 		 * we get any properties that aren't exactly
16815331Samw 		 * name/value pairs, we may need to
16825331Samw 		 * interpret/transform.
16835331Samw 		 */
16845331Samw 		if (value != NULL)
16855331Samw 			len += 1 + strlen(value);
16865331Samw 
16875331Samw 		while (buffsize <= (curlen + len)) {
16885331Samw 			/* need more room */
16895331Samw 			buffsize += incr;
16905331Samw 			buff = realloc(buff, buffsize);
16915331Samw 			*rbuff = buff;
16925331Samw 			*rbuffsize = buffsize;
16935331Samw 			if (buff == NULL) {
16945331Samw 				/* realloc failed so free everything */
16955331Samw 				if (*rbuff != NULL)
16965331Samw 					free(*rbuff);
16975331Samw 				goto err;
16985331Samw 			}
16995331Samw 		}
17005331Samw 		if (buff == NULL)
17015331Samw 			goto err;
17025331Samw 		(void) snprintf(buff + curlen, buffsize - curlen,
17035331Samw 		    "%s%s=%s", sep ? "," : "",
17045331Samw 		    name, value != NULL ? value : "\"\"");
17055331Samw 
17065331Samw 	}
17075331Samw err:
17085331Samw 	if (name != NULL)
17095331Samw 		sa_free_attr_string(name);
17105331Samw 	if (value != NULL)
17115331Samw 		sa_free_attr_string(value);
17125331Samw }
17135331Samw 
17145331Samw /*
17155331Samw  * smb_format_resource_options(resource, hier)
17165331Samw  *
17175331Samw  * format all the options on the group into a flattened option
17185331Samw  * string. If hier is non-zero, walk up the tree to get inherited
17195331Samw  * options.
17205331Samw  */
17215331Samw 
17225331Samw static char *
17235331Samw smb_format_options(sa_group_t group, int hier)
17245331Samw {
17255331Samw 	sa_optionset_t options = NULL;
17265331Samw 	sa_property_t prop;
17275331Samw 	int sep = 0;
17285331Samw 	char *buff;
17295331Samw 	size_t buffsize;
17305331Samw 
17315331Samw 
17325331Samw 	buff = malloc(OPT_CHUNK);
17335331Samw 	if (buff == NULL)
17345331Samw 		return (NULL);
17355331Samw 
17365331Samw 	buff[0] = '\0';
17375331Samw 	buffsize = OPT_CHUNK;
17385331Samw 
17395331Samw 	/*
17405331Samw 	 * We may have a an optionset relative to this item. format
17415331Samw 	 * these if we find them and then add any security definitions.
17425331Samw 	 */
17435331Samw 
17445331Samw 	options = sa_get_derived_optionset(group, "smb", hier);
17455331Samw 
17465331Samw 	/*
17475331Samw 	 * do the default set first but skip any option that is also
17485331Samw 	 * in the protocol specific optionset.
17495331Samw 	 */
17505331Samw 	if (options != NULL) {
17515331Samw 		for (prop = sa_get_property(options, NULL);
17525331Samw 		    prop != NULL; prop = sa_get_next_property(prop)) {
17535331Samw 			/*
17545331Samw 			 * use this one since we skipped any
17555331Samw 			 * of these that were also in
17565331Samw 			 * optdefault
17575331Samw 			 */
17585331Samw 			smb_sprint_option(&buff, &buffsize, OPT_CHUNK,
17595331Samw 			    prop, sep);
17605331Samw 			if (buff == NULL) {
17615331Samw 				/*
17625331Samw 				 * buff could become NULL if there
17635331Samw 				 * isn't enough memory for
17645331Samw 				 * smb_sprint_option to realloc()
17655331Samw 				 * as necessary. We can't really
17665331Samw 				 * do anything about it at this
17675331Samw 				 * point so we return NULL.  The
17685331Samw 				 * caller should handle the
17695331Samw 				 * failure.
17705331Samw 				 */
17715331Samw 				if (options != NULL)
17725331Samw 					sa_free_derived_optionset(
17735331Samw 					    options);
17745331Samw 				return (buff);
17755331Samw 			}
17765331Samw 			sep = 1;
17775331Samw 		}
17785331Samw 	}
17795331Samw 
17805331Samw 	if (options != NULL)
17815331Samw 		sa_free_derived_optionset(options);
17825331Samw 	return (buff);
17835331Samw }
17845331Samw 
17855331Samw /*
17865331Samw  * smb_rename_resource(resource, newname)
17875331Samw  *
17885331Samw  * Change the current exported name of the resource to newname.
17895331Samw  */
17905331Samw /*ARGSUSED*/
17915331Samw int
17925331Samw smb_rename_resource(sa_handle_t handle, sa_resource_t resource, char *newname)
17935331Samw {
17945331Samw 	int ret = SA_OK;
17955331Samw 	int err;
17965331Samw 	char *oldname;
17975331Samw 
17987348SJose.Borrego@Sun.COM 	if (!smb_isonline())
17997348SJose.Borrego@Sun.COM 		return (SA_OK);
18007348SJose.Borrego@Sun.COM 
18015331Samw 	oldname = sa_get_resource_attr(resource, "name");
18025331Samw 	if (oldname == NULL)
18035331Samw 		return (SA_NO_SUCH_RESOURCE);
18045331Samw 
18057348SJose.Borrego@Sun.COM 	err = smb_share_rename(oldname, newname);
18065331Samw 
18075331Samw 	/* improve error values somewhat */
18085331Samw 	switch (err) {
18095331Samw 	case NERR_Success:
18105331Samw 		break;
18115331Samw 	case NERR_InternalError:
18125331Samw 		ret = SA_SYSTEM_ERR;
18135331Samw 		break;
18145331Samw 	case NERR_DuplicateShare:
18155331Samw 		ret = SA_DUPLICATE_NAME;
18165331Samw 		break;
18175331Samw 	default:
18185331Samw 		ret = SA_CONFIG_ERR;
18195331Samw 		break;
18205331Samw 	}
18215331Samw 
18225331Samw 	return (ret);
18235331Samw }
18247348SJose.Borrego@Sun.COM 
18257348SJose.Borrego@Sun.COM static int
18267348SJose.Borrego@Sun.COM smb_build_shareinfo(sa_share_t share, sa_resource_t resource, smb_share_t *si)
18277348SJose.Borrego@Sun.COM {
18287348SJose.Borrego@Sun.COM 	sa_property_t prop;
18297348SJose.Borrego@Sun.COM 	sa_optionset_t opts;
18307348SJose.Borrego@Sun.COM 	char *path;
18317348SJose.Borrego@Sun.COM 	char *rname;
18327348SJose.Borrego@Sun.COM 	char *val = NULL;
18337348SJose.Borrego@Sun.COM 
18347348SJose.Borrego@Sun.COM 	bzero(si, sizeof (smb_share_t));
18357348SJose.Borrego@Sun.COM 
18367348SJose.Borrego@Sun.COM 	if ((path = sa_get_share_attr(share, "path")) == NULL)
18377348SJose.Borrego@Sun.COM 		return (SA_NO_SUCH_PATH);
18387348SJose.Borrego@Sun.COM 
18397348SJose.Borrego@Sun.COM 	if ((rname = sa_get_resource_attr(resource, "name")) == NULL) {
18407348SJose.Borrego@Sun.COM 		sa_free_attr_string(path);
18417348SJose.Borrego@Sun.COM 		return (SA_NO_SUCH_RESOURCE);
18427348SJose.Borrego@Sun.COM 	}
18437348SJose.Borrego@Sun.COM 
18447348SJose.Borrego@Sun.COM 	si->shr_flags = (sa_is_persistent(share))
18457348SJose.Borrego@Sun.COM 	    ? SMB_SHRF_PERM : SMB_SHRF_TRANS;
18467348SJose.Borrego@Sun.COM 
18477348SJose.Borrego@Sun.COM 	(void) strlcpy(si->shr_path, path, sizeof (si->shr_path));
18487348SJose.Borrego@Sun.COM 	(void) strlcpy(si->shr_name, rname, sizeof (si->shr_name));
18497348SJose.Borrego@Sun.COM 	sa_free_attr_string(path);
18507348SJose.Borrego@Sun.COM 	sa_free_attr_string(rname);
18517348SJose.Borrego@Sun.COM 
18527348SJose.Borrego@Sun.COM 	val = sa_get_resource_description(resource);
18537348SJose.Borrego@Sun.COM 	if (val == NULL)
18547348SJose.Borrego@Sun.COM 		val = sa_get_share_description(share);
18557348SJose.Borrego@Sun.COM 
18567348SJose.Borrego@Sun.COM 	if (val != NULL) {
18577348SJose.Borrego@Sun.COM 		(void) strlcpy(si->shr_cmnt, val, sizeof (si->shr_cmnt));
18587348SJose.Borrego@Sun.COM 		sa_free_share_description(val);
18597348SJose.Borrego@Sun.COM 	}
18607348SJose.Borrego@Sun.COM 
18617348SJose.Borrego@Sun.COM 	opts = sa_get_derived_optionset(resource, SMB_PROTOCOL_NAME, 1);
18627348SJose.Borrego@Sun.COM 	if (opts == NULL)
18637348SJose.Borrego@Sun.COM 		return (SA_OK);
18647348SJose.Borrego@Sun.COM 
18657348SJose.Borrego@Sun.COM 	prop = sa_get_property(opts, SMB_SHROPT_AD_CONTAINER);
18667348SJose.Borrego@Sun.COM 	if (prop != NULL) {
18677348SJose.Borrego@Sun.COM 		if ((val = sa_get_property_attr(prop, "value")) != NULL) {
18687348SJose.Borrego@Sun.COM 			(void) strlcpy(si->shr_container, val,
18697348SJose.Borrego@Sun.COM 			    sizeof (si->shr_container));
18707348SJose.Borrego@Sun.COM 			free(val);
18717348SJose.Borrego@Sun.COM 		}
18727348SJose.Borrego@Sun.COM 	}
18737348SJose.Borrego@Sun.COM 
18747348SJose.Borrego@Sun.COM 	sa_free_derived_optionset(opts);
18757348SJose.Borrego@Sun.COM 	return (SA_OK);
18767348SJose.Borrego@Sun.COM }
18777348SJose.Borrego@Sun.COM 
18787348SJose.Borrego@Sun.COM /*
18797348SJose.Borrego@Sun.COM  * smb_get_defaultgrp
18807348SJose.Borrego@Sun.COM  *
18817348SJose.Borrego@Sun.COM  * If default group for CIFS shares (i.e. "smb") exists
18827348SJose.Borrego@Sun.COM  * then it will return the group handle, otherwise it will
18837348SJose.Borrego@Sun.COM  * create the group and return the handle.
18847348SJose.Borrego@Sun.COM  *
18857348SJose.Borrego@Sun.COM  * All the shares created by CIFS clients (this is only possible
18867348SJose.Borrego@Sun.COM  * via RPC) will be added to "smb" groups.
18877348SJose.Borrego@Sun.COM  */
18887348SJose.Borrego@Sun.COM static sa_group_t
18897348SJose.Borrego@Sun.COM smb_get_defaultgrp(sa_handle_t handle)
18907348SJose.Borrego@Sun.COM {
18917348SJose.Borrego@Sun.COM 	sa_group_t group = NULL;
18927348SJose.Borrego@Sun.COM 	int err;
18937348SJose.Borrego@Sun.COM 
18947348SJose.Borrego@Sun.COM 	group = sa_get_group(handle, SMB_DEFAULT_SHARE_GROUP);
18957348SJose.Borrego@Sun.COM 	if (group != NULL)
18967348SJose.Borrego@Sun.COM 		return (group);
18977348SJose.Borrego@Sun.COM 
18987348SJose.Borrego@Sun.COM 	group = sa_create_group(handle, SMB_DEFAULT_SHARE_GROUP, &err);
18997348SJose.Borrego@Sun.COM 	if (group == NULL)
19007348SJose.Borrego@Sun.COM 		return (NULL);
19017348SJose.Borrego@Sun.COM 
19027348SJose.Borrego@Sun.COM 	if (sa_create_optionset(group, SMB_DEFAULT_SHARE_GROUP) == NULL) {
19037348SJose.Borrego@Sun.COM 		(void) sa_remove_group(group);
19047348SJose.Borrego@Sun.COM 		group = NULL;
19057348SJose.Borrego@Sun.COM 	}
19067348SJose.Borrego@Sun.COM 
19077348SJose.Borrego@Sun.COM 	return (group);
19087348SJose.Borrego@Sun.COM }
1909