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 /*
235331Samw  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
245331Samw  * Use is subject to license terms.
255331Samw  */
265331Samw 
275331Samw #pragma ident	"%Z%%M%	%I%	%E% SMI"
285331Samw 
295331Samw /*
305331Samw  * SMB specific functions
315331Samw  */
325331Samw #include <stdio.h>
335331Samw #include <string.h>
345331Samw #include <ctype.h>
355331Samw #include <stdlib.h>
365331Samw #include <unistd.h>
375331Samw #include <zone.h>
385331Samw #include <errno.h>
395331Samw #include <locale.h>
405331Samw #include <fcntl.h>
415331Samw #include <sys/types.h>
425331Samw #include <sys/stat.h>
435331Samw #include <syslog.h>
445331Samw #include "libshare.h"
455331Samw #include "libshare_impl.h"
465331Samw #include <pwd.h>
475331Samw #include <limits.h>
485331Samw #include <libscf.h>
495331Samw #include <strings.h>
505331Samw #include "libshare_smb.h"
515331Samw #include <rpcsvc/daemon_utils.h>
525331Samw #include <smbsrv/lmshare.h>
535331Samw #include <smbsrv/lmshare_door.h>
545331Samw #include <smbsrv/smbinfo.h>
555331Samw #include <smbsrv/libsmb.h>
565331Samw 
575331Samw /* internal functions */
585331Samw static int smb_share_init(void);
595331Samw static void smb_share_fini(void);
605331Samw static int smb_enable_share(sa_share_t);
615331Samw static int smb_share_changed(sa_share_t);
625331Samw static int smb_resource_changed(sa_resource_t);
635331Samw static int smb_rename_resource(sa_handle_t, sa_resource_t, char *);
645331Samw static int smb_disable_share(sa_share_t share, char *);
655331Samw static int smb_validate_property(sa_property_t, sa_optionset_t);
665331Samw static int smb_set_proto_prop(sa_property_t);
675331Samw static sa_protocol_properties_t smb_get_proto_set(void);
685331Samw static char *smb_get_status(void);
695331Samw static int smb_parse_optstring(sa_group_t, char *);
705331Samw static char *smb_format_options(sa_group_t, int);
715331Samw 
725331Samw static int smb_enable_service(void);
735331Samw 
745331Samw static int range_check_validator(int, char *);
755331Samw static int range_check_validator_zero_ok(int, char *);
765331Samw static int string_length_check_validator(int, char *);
775331Samw static int true_false_validator(int, char *);
785331Samw static int ip_address_validator_empty_ok(int, char *);
795331Samw static int ip_address_csv_list_validator_empty_ok(int, char *);
805331Samw static int ipc_mode_validator(int, char *);
815331Samw static int path_validator(int, char *);
825331Samw 
835331Samw static int smb_enable_resource(sa_resource_t);
845331Samw static int smb_disable_resource(sa_resource_t);
855331Samw static uint64_t smb_share_features(void);
865331Samw static int smb_list_transient(sa_handle_t);
875331Samw 
885331Samw /* size of basic format allocation */
895331Samw #define	OPT_CHUNK	1024
905331Samw 
915331Samw /*
925331Samw  * Indexes of entries in smb_proto_options table.
935331Samw  * Changes to smb_proto_options table may require
945331Samw  * an update to these values.
955331Samw  */
965331Samw #define	PROTO_OPT_WINS1			6
975331Samw #define	PROTO_OPT_WINS_EXCLUDE		8
985331Samw 
995331Samw 
1005331Samw /*
1015331Samw  * ops vector that provides the protocol specific info and operations
1025331Samw  * for share management.
1035331Samw  */
1045331Samw 
1055331Samw struct sa_plugin_ops sa_plugin_ops = {
1065331Samw 	SA_PLUGIN_VERSION,
1075331Samw 	SMB_PROTOCOL_NAME,
1085331Samw 	smb_share_init,
1095331Samw 	smb_share_fini,
1105331Samw 	smb_enable_share,
1115331Samw 	smb_disable_share,
1125331Samw 	smb_validate_property,
1135331Samw 	NULL,
1145331Samw 	NULL,
1155331Samw 	smb_parse_optstring,
1165331Samw 	smb_format_options,
1175331Samw 	smb_set_proto_prop,
1185331Samw 	smb_get_proto_set,
1195331Samw 	smb_get_status,
1205331Samw 	NULL,
1215331Samw 	NULL,
1225331Samw 	NULL,
1235331Samw 	smb_share_changed,
1245331Samw 	smb_enable_resource,
1255331Samw 	smb_disable_resource,
1265331Samw 	smb_share_features,
1275331Samw 	smb_list_transient,
1285331Samw 	smb_resource_changed,
1295331Samw 	smb_rename_resource,
1305331Samw 	NULL,
1315331Samw 	NULL
1325331Samw };
1335331Samw 
1345331Samw /*
1355331Samw  * option definitions.  Make sure to keep the #define for the option
1365331Samw  * index just before the entry it is the index for. Changing the order
1375331Samw  * can cause breakage.
1385331Samw  */
1395331Samw 
1405331Samw struct option_defs optdefs[] = {
1415331Samw 	{SHOPT_AD_CONTAINER, OPT_TYPE_STRING},
1425331Samw 	{SHOPT_NAME, OPT_TYPE_NAME},
1435331Samw 	{NULL, NULL},
1445331Samw };
1455331Samw 
1465331Samw /*
1475331Samw  * findopt(name)
1485331Samw  *
1495331Samw  * Lookup option "name" in the option table and return the table
1505331Samw  * index.
1515331Samw  */
1525331Samw 
1535331Samw static int
1545331Samw findopt(char *name)
1555331Samw {
1565331Samw 	int i;
1575331Samw 	if (name != NULL) {
1585331Samw 		for (i = 0; optdefs[i].tag != NULL; i++) {
1595331Samw 			if (strcmp(optdefs[i].tag, name) == 0)
1605331Samw 				return (i);
1615331Samw 		}
1625331Samw 	}
1635331Samw 	return (-1);
1645331Samw }
1655331Samw 
1665331Samw /*
1675331Samw  * is_a_number(number)
1685331Samw  *
1695331Samw  * is the string a number in one of the forms we want to use?
1705331Samw  */
1715331Samw 
1725331Samw static int
1735331Samw is_a_number(char *number)
1745331Samw {
1755331Samw 	int ret = 1;
1765331Samw 	int hex = 0;
1775331Samw 
1785331Samw 	if (strncmp(number, "0x", 2) == 0) {
1795331Samw 		number += 2;
1805331Samw 		hex = 1;
1815331Samw 	} else if (*number == '-') {
1825331Samw 		number++; /* skip the minus */
1835331Samw 	}
1845331Samw 
1855331Samw 	while (ret == 1 && *number != '\0') {
1865331Samw 		if (hex) {
1875331Samw 			ret = isxdigit(*number++);
1885331Samw 		} else {
1895331Samw 			ret = isdigit(*number++);
1905331Samw 		}
1915331Samw 	}
1925331Samw 	return (ret);
1935331Samw }
1945331Samw 
1955331Samw /*
1965331Samw  * validresource(name)
1975331Samw  *
1985331Samw  * Check that name only has valid characters in it. The current valid
1995331Samw  * set are the printable characters but not including:
2005331Samw  *	" / \ [ ] : | < > + ; , ? * = \t
2015331Samw  * Note that space is included and there is a maximum length.
2025331Samw  */
2035331Samw static int
2045331Samw validresource(const char *name)
2055331Samw {
2065331Samw 	const char *cp;
2075331Samw 	size_t len;
2085331Samw 
2095331Samw 	if (name == NULL)
2105331Samw 		return (B_FALSE);
2115331Samw 
2125331Samw 	len = strlen(name);
2135331Samw 	if (len == 0 || len > SA_MAX_RESOURCE_NAME)
2145331Samw 		return (B_FALSE);
2155331Samw 
2165331Samw 	if (strpbrk(name, "\"/\\[]:|<>+;,?*=\t") != NULL) {
2175331Samw 		return (B_FALSE);
2185331Samw 	}
2195331Samw 
2205331Samw 	for (cp = name; *cp != '\0'; cp++)
2215331Samw 		if (iscntrl(*cp))
2225331Samw 			return (B_FALSE);
2235331Samw 
2245331Samw 	return (B_TRUE);
2255331Samw }
2265331Samw 
2275331Samw /*
2285331Samw  * smb_isonline()
2295331Samw  *
2305331Samw  * Determine if the SMF service instance is in the online state or
2315331Samw  * not. A number of operations depend on this state.
2325331Samw  */
2335331Samw static boolean_t
2345331Samw smb_isonline(void)
2355331Samw {
2365331Samw 	char *str;
2375331Samw 	boolean_t ret = B_FALSE;
2385331Samw 
2395331Samw 	if ((str = smf_get_state(SMBD_DEFAULT_INSTANCE_FMRI)) != NULL) {
2405331Samw 		ret = (strcmp(str, SCF_STATE_STRING_ONLINE) == 0);
2415331Samw 		free(str);
2425331Samw 	}
2435331Samw 	return (ret);
2445331Samw }
2455331Samw 
2465331Samw /*
2475331Samw  * smb_enable_share tells the implementation that it is to enable the share.
2485331Samw  * This entails converting the path and options into the appropriate ioctl
2495331Samw  * calls. It is assumed that all error checking of paths, etc. were
2505331Samw  * done earlier.
2515331Samw  */
2525331Samw static int
2535331Samw smb_enable_share(sa_share_t share)
2545331Samw {
2555331Samw 	char *path;
2565331Samw 	char *rname;
2575331Samw 	lmshare_info_t si;
2585331Samw 	sa_resource_t resource;
2595331Samw 	boolean_t iszfs;
2605331Samw 	boolean_t privileged;
2615331Samw 	int err = SA_OK;
2625331Samw 	priv_set_t *priv_effective;
2635331Samw 	boolean_t online;
2645331Samw 
2655331Samw 	priv_effective = priv_allocset();
2665331Samw 	(void) getppriv(PRIV_EFFECTIVE, priv_effective);
2675331Samw 	privileged = (priv_isfullset(priv_effective) == B_TRUE);
2685331Samw 	priv_freeset(priv_effective);
2695331Samw 
2705331Samw 	/* get the path since it is important in several places */
2715331Samw 	path = sa_get_share_attr(share, "path");
2725331Samw 	if (path == NULL)
2735331Samw 		return (SA_NO_SUCH_PATH);
2745331Samw 
2755331Samw 	online = smb_isonline();
2765331Samw 
2775331Samw 	iszfs = sa_path_is_zfs(path);
2785331Samw 
2795331Samw 	if (iszfs) {
2805331Samw 
2815331Samw 		if (privileged == B_FALSE && !online) {
2825331Samw 
2835331Samw 			if (!online) {
2845331Samw 				(void) printf(dgettext(TEXT_DOMAIN,
2855331Samw 				    "SMB: Cannot share remove "
2865331Samw 				    "file system: %s\n"), path);
2875331Samw 				(void) printf(dgettext(TEXT_DOMAIN,
2885331Samw 				    "SMB: Service needs to be enabled "
2895331Samw 				    "by a privileged user\n"));
2905331Samw 				err = SA_NO_PERMISSION;
2915331Samw 				errno = EPERM;
2925331Samw 			}
2935331Samw 			if (err) {
2945331Samw 				sa_free_attr_string(path);
2955331Samw 				return (err);
2965331Samw 			}
2975331Samw 
2985331Samw 		}
2995331Samw 	}
3005331Samw 
3015331Samw 	if (privileged == B_TRUE && !online) {
3025331Samw 		err = smb_enable_service();
3035331Samw 		if (err != SA_OK) {
3045331Samw 			(void) printf(dgettext(TEXT_DOMAIN,
3055331Samw 			    "SMB: Unable to enable service\n"));
3065331Samw 			/*
3075331Samw 			 * For now, it is OK to not be able to enable
3085331Samw 			 * the service.
3095331Samw 			 */
3105331Samw 			if (err == SA_BUSY)
3115331Samw 				err = SA_OK;
3125331Samw 		} else {
3135331Samw 			online = B_TRUE;
3145331Samw 		}
3155331Samw 	}
3165331Samw 
3175331Samw 	/*
3185331Samw 	 * Don't bother trying to start shares if the service isn't
3195331Samw 	 * running.
3205331Samw 	 */
3215331Samw 	if (!online)
3225331Samw 		goto done;
3235331Samw 
3245331Samw 	/* Each share can have multiple resources */
3255331Samw 	for (resource = sa_get_share_resource(share, NULL);
3265331Samw 	    resource != NULL;
3275331Samw 	    resource = sa_get_next_resource(resource)) {
3285331Samw 		sa_optionset_t opts;
3295331Samw 		bzero(&si, sizeof (lmshare_info_t));
3305331Samw 		rname = sa_get_resource_attr(resource, "name");
3315331Samw 		if (rname == NULL) {
3325331Samw 			sa_free_attr_string(path);
3335331Samw 			return (SA_NO_SUCH_RESOURCE);
3345331Samw 		}
3355331Samw 
3365331Samw 		opts = sa_get_derived_optionset(resource, SMB_PROTOCOL_NAME, 1);
3375331Samw 		smb_build_lmshare_info(rname, path, opts, &si);
3385331Samw 		sa_free_attr_string(rname);
3395331Samw 
3405331Samw 		sa_free_derived_optionset(opts);
3415331Samw 		if (!iszfs) {
3425331Samw 			err = lmshrd_add(&si);
3435331Samw 		} else {
3445331Samw 			share_t sh;
3455331Samw 
3465331Samw 			sa_sharetab_fill_zfs(share, &sh, "smb");
3475331Samw 			err = sa_share_zfs(share, (char *)path, &sh,
3485331Samw 			    &si, ZFS_SHARE_SMB);
3495331Samw 
3505331Samw 			sa_emptyshare(&sh);
3515331Samw 		}
3525331Samw 	}
3535331Samw 	if (!iszfs)
3545331Samw 		(void) sa_update_sharetab(share, "smb");
3555331Samw done:
3565331Samw 	sa_free_attr_string(path);
3575331Samw 
3585331Samw 	return (err == NERR_DuplicateShare ? 0 : err);
3595331Samw }
3605331Samw 
3615331Samw /*
3625331Samw  * This is the share for CIFS all shares have resource names.
3635331Samw  * Enable tells the smb server to update its hash. If it fails
3645331Samw  * because smb server is down, we just ignore as smb server loads
3655331Samw  * the resources from sharemanager at startup.
3665331Samw  */
3675331Samw 
3685331Samw static int
3695331Samw smb_enable_resource(sa_resource_t resource)
3705331Samw {
3715331Samw 	char *path;
3725331Samw 	char *rname;
3735331Samw 	sa_optionset_t opts;
3745331Samw 	sa_share_t share;
3755331Samw 	lmshare_info_t si;
3765331Samw 	int ret;
3775331Samw 
3785331Samw 	share = sa_get_resource_parent(resource);
3795331Samw 	if (share == NULL)
3805331Samw 		return (SA_NO_SUCH_PATH);
3815331Samw 	path = sa_get_share_attr(share, "path");
3825331Samw 	if (path == NULL)
3835331Samw 		return (SA_SYSTEM_ERR);
3845331Samw 	rname = sa_get_resource_attr(resource, "name");
3855331Samw 	if (rname == NULL) {
3865331Samw 		sa_free_attr_string(path);
3875331Samw 		return (SA_NO_SUCH_RESOURCE);
3885331Samw 	}
3895331Samw 
3905331Samw 	ret = smb_enable_service();
3915331Samw 
3925331Samw 	if (!smb_isonline()) {
3935331Samw 		ret = SA_OK;
3945331Samw 		goto done;
3955331Samw 	}
3965331Samw 
3975331Samw 	opts = sa_get_derived_optionset(resource, SMB_PROTOCOL_NAME, 1);
3985331Samw 	smb_build_lmshare_info(rname, path, opts, &si);
3995331Samw 	sa_free_attr_string(path);
4005331Samw 	sa_free_attr_string(rname);
4015331Samw 	sa_free_derived_optionset(opts);
4025331Samw 	if (lmshrd_add(&si) != NERR_Success)
4035331Samw 		return (SA_NOT_SHARED);
4045331Samw 	(void) sa_update_sharetab(share, "smb");
4055331Samw 
4065331Samw done:
4075331Samw 	return (ret);
4085331Samw }
4095331Samw 
4105331Samw /*
4115331Samw  * Remove it from smb server hash.
4125331Samw  */
4135331Samw static int
4145331Samw smb_disable_resource(sa_resource_t resource)
4155331Samw {
4165331Samw 	char *rname;
4175331Samw 	DWORD res;
4185331Samw 	sa_share_t share;
4195331Samw 
4205331Samw 	rname = sa_get_resource_attr(resource, "name");
4215331Samw 	if (rname == NULL)
4225331Samw 		return (SA_NO_SUCH_RESOURCE);
4235331Samw 
4245331Samw 	if (smb_isonline()) {
4255331Samw 		res = lmshrd_delete(rname);
4265331Samw 		if (res != NERR_Success) {
4275331Samw 			sa_free_attr_string(rname);
4285331Samw 			return (SA_CONFIG_ERR);
4295331Samw 		}
4305331Samw 		sa_free_attr_string(rname);
4315331Samw 		rname = NULL;
4325331Samw 	}
4335331Samw 	share = sa_get_resource_parent(resource);
4345331Samw 	if (share != NULL) {
4355331Samw 		rname = sa_get_share_attr(share, "path");
4365331Samw 		if (rname != NULL) {
4375331Samw 			(void) sa_delete_sharetab(rname, "smb");
4385331Samw 			sa_free_attr_string(rname);
4395331Samw 			rname = NULL;
4405331Samw 		}
4415331Samw 	}
4425331Samw 	if (rname != NULL)
4435331Samw 		sa_free_attr_string(rname);
4445331Samw 	/*
4455331Samw 	 * Always return OK as smb/server may be down and
4465331Samw 	 * Shares will be picked up when loaded.
4475331Samw 	 */
4485331Samw 	return (SA_OK);
4495331Samw }
4505331Samw 
4515331Samw /*
4525331Samw  * smb_share_changed(sa_share_t share)
4535331Samw  *
4545331Samw  * The specified share has changed.
4555331Samw  */
4565331Samw static int
4575331Samw smb_share_changed(sa_share_t share)
4585331Samw {
4595331Samw 	char *path;
4605331Samw 	sa_resource_t resource;
4615331Samw 
4625331Samw 	/* get the path since it is important in several places */
4635331Samw 	path = sa_get_share_attr(share, "path");
4645331Samw 	if (path == NULL)
4655331Samw 		return (SA_NO_SUCH_PATH);
4665331Samw 	for (resource = sa_get_share_resource(share, NULL);
4675331Samw 	    resource != NULL;
4685331Samw 	    resource = sa_get_next_resource(resource))
4695331Samw 		(void) smb_resource_changed(resource);
4705331Samw 
4715331Samw 	sa_free_attr_string(path);
4725331Samw 
4735331Samw 	return (SA_OK);
4745331Samw }
4755331Samw 
4765331Samw /*
4775331Samw  * smb_resource_changed(sa_resource_t resource)
4785331Samw  *
4795331Samw  * The specified resource has changed.
4805331Samw  */
4815331Samw static int
4825331Samw smb_resource_changed(sa_resource_t resource)
4835331Samw {
4845331Samw 	DWORD res;
4855331Samw 	lmshare_info_t si;
4865331Samw 	lmshare_info_t new_si;
4875331Samw 	char *rname, *path;
4885331Samw 	sa_optionset_t opts;
4895331Samw 	sa_share_t share;
4905331Samw 
4915331Samw 	rname = sa_get_resource_attr(resource, "name");
4925331Samw 	if (rname == NULL)
4935331Samw 		return (SA_NO_SUCH_RESOURCE);
4945331Samw 
4955331Samw 	share = sa_get_resource_parent(resource);
4965331Samw 	if (share == NULL) {
4975331Samw 		sa_free_attr_string(rname);
4985331Samw 		return (SA_CONFIG_ERR);
4995331Samw 	}
5005331Samw 
5015331Samw 	path = sa_get_share_attr(share, "path");
5025331Samw 	if (path == NULL) {
5035331Samw 		sa_free_attr_string(rname);
5045331Samw 		return (SA_NO_SUCH_PATH);
5055331Samw 	}
5065331Samw 
5075331Samw 	if (!smb_isonline()) {
5085331Samw 		sa_free_attr_string(rname);
5095331Samw 		return (SA_OK);
5105331Samw 	}
5115331Samw 
5125331Samw 	/* Update the share cache in smb/server */
5135331Samw 	res = lmshrd_getinfo(rname, &si);
5145331Samw 	if (res != NERR_Success) {
5155331Samw 		sa_free_attr_string(path);
5165331Samw 		sa_free_attr_string(rname);
5175331Samw 		return (SA_CONFIG_ERR);
5185331Samw 	}
5195331Samw 
5205331Samw 	opts = sa_get_derived_optionset(resource, SMB_PROTOCOL_NAME, 1);
5215331Samw 	smb_build_lmshare_info(rname, path, opts, &new_si);
5225331Samw 	sa_free_derived_optionset(opts);
5235331Samw 	sa_free_attr_string(path);
5245331Samw 	sa_free_attr_string(rname);
5255331Samw 
5265331Samw 	/*
5275331Samw 	 * Update all fields from sa_share_t
5285331Samw 	 * Get derived values.
5295331Samw 	 */
5305331Samw 	if (lmshrd_setinfo(&new_si) != LMSHR_DOOR_SRV_SUCCESS)
5315331Samw 		return (SA_CONFIG_ERR);
5325331Samw 	return (smb_enable_service());
5335331Samw }
5345331Samw 
5355331Samw /*
5365331Samw  * smb_disable_share(sa_share_t share)
5375331Samw  *
5385331Samw  * Unshare the specified share.
5395331Samw  */
5405331Samw static int
5415331Samw smb_disable_share(sa_share_t share, char *path)
5425331Samw {
5435331Samw 	char *rname;
5445331Samw 	sa_resource_t resource;
5455331Samw 	boolean_t iszfs;
5465331Samw 	int err = SA_OK;
5475331Samw 
5485331Samw 	iszfs = sa_path_is_zfs(path);
5495331Samw 	if (!smb_isonline())
5505331Samw 		goto done;
5515331Samw 
5525331Samw 	for (resource = sa_get_share_resource(share, NULL);
5535331Samw 	    resource != NULL;
5545331Samw 	    resource = sa_get_next_resource(resource)) {
5555331Samw 		rname = sa_get_resource_attr(resource, "name");
5565331Samw 		if (rname == NULL) {
5575331Samw 			continue;
5585331Samw 		}
5595331Samw 		if (!iszfs) {
5605331Samw 			err = lmshrd_delete(rname);
5615331Samw 			switch (err) {
5625331Samw 			case NERR_NetNameNotFound:
5635331Samw 			case NERR_Success:
5645331Samw 				err = SA_OK;
5655331Samw 				break;
5665331Samw 			default:
5675331Samw 				err = SA_CONFIG_ERR;
5685331Samw 				break;
5695331Samw 			}
5705331Samw 		} else {
5715331Samw 			share_t sh;
5725331Samw 
5735331Samw 			sa_sharetab_fill_zfs(share, &sh, "smb");
5745331Samw 			err = sa_share_zfs(share, (char *)path, &sh,
5755331Samw 			    rname, ZFS_UNSHARE_SMB);
5765331Samw 			sa_emptyshare(&sh);
5775331Samw 		}
5785331Samw 		sa_free_attr_string(rname);
5795331Samw 	}
5805331Samw done:
5815331Samw 	if (!iszfs)
5825331Samw 		(void) sa_delete_sharetab(path, "smb");
5835331Samw 	return (err);
5845331Samw }
5855331Samw 
5865331Samw /*
5875331Samw  * smb_validate_property(property, parent)
5885331Samw  *
5895331Samw  * Check that the property has a legitimate value for its type.
5905331Samw  */
5915331Samw 
5925331Samw static int
5935331Samw smb_validate_property(sa_property_t property, sa_optionset_t parent)
5945331Samw {
5955331Samw 	int ret = SA_OK;
5965331Samw 	char *propname;
5975331Samw 	int optindex;
5985331Samw 	sa_group_t parent_group;
5995331Samw 	char *value;
6005331Samw 
6015331Samw 	propname = sa_get_property_attr(property, "type");
6025331Samw 
6035331Samw 	if ((optindex = findopt(propname)) < 0)
6045331Samw 		ret = SA_NO_SUCH_PROP;
6055331Samw 
6065331Samw 	/* need to validate value range here as well */
6075331Samw 	if (ret == SA_OK) {
6085331Samw 		parent_group = sa_get_parent_group((sa_share_t)parent);
6095331Samw 		if (optdefs[optindex].share && !sa_is_share(parent_group))
6105331Samw 			ret = SA_PROP_SHARE_ONLY;
6115331Samw 	}
6125331Samw 	if (ret != SA_OK) {
6135331Samw 		if (propname != NULL)
6145331Samw 			sa_free_attr_string(propname);
6155331Samw 		return (ret);
6165331Samw 	}
6175331Samw 
6185331Samw 	value = sa_get_property_attr(property, "value");
6195331Samw 	if (value != NULL) {
6205331Samw 		/* first basic type checking */
6215331Samw 		switch (optdefs[optindex].type) {
6225331Samw 		case OPT_TYPE_NUMBER:
6235331Samw 			/* check that the value is all digits */
6245331Samw 			if (!is_a_number(value))
6255331Samw 				ret = SA_BAD_VALUE;
6265331Samw 			break;
6275331Samw 		case OPT_TYPE_BOOLEAN:
6285331Samw 			if (strlen(value) == 0 ||
6295331Samw 			    strcasecmp(value, "true") == 0 ||
6305331Samw 			    strcmp(value, "1") == 0 ||
6315331Samw 			    strcasecmp(value, "false") == 0 ||
6325331Samw 			    strcmp(value, "0") == 0) {
6335331Samw 				ret = SA_OK;
6345331Samw 			} else {
6355331Samw 				ret = SA_BAD_VALUE;
6365331Samw 			}
6375331Samw 			break;
6385331Samw 		case OPT_TYPE_NAME:
6395331Samw 			/*
6405331Samw 			 * Make sure no invalid characters
6415331Samw 			 */
6425331Samw 			if (validresource(value) == B_FALSE)
6435331Samw 				ret = SA_BAD_VALUE;
6445331Samw 			break;
6455331Samw 		case OPT_TYPE_STRING:
6465331Samw 			/* whatever is here should be ok */
6475331Samw 			break;
6485331Samw 		default:
6495331Samw 			break;
6505331Samw 		}
6515331Samw 	}
6525331Samw 
6535331Samw 	if (value != NULL)
6545331Samw 		sa_free_attr_string(value);
6555331Samw 	if (ret == SA_OK && optdefs[optindex].check != NULL)
6565331Samw 		/* do the property specific check */
6575331Samw 		ret = optdefs[optindex].check(property);
6585331Samw 
6595331Samw 	if (propname != NULL)
6605331Samw 		sa_free_attr_string(propname);
6615331Samw 	return (ret);
6625331Samw }
6635331Samw 
6645331Samw /*
6655331Samw  * Protocol management functions
6665331Samw  *
6675331Samw  * properties defined in the default files are defined in
6685331Samw  * proto_option_defs for parsing and validation.
6695331Samw  */
6705331Samw 
6715331Samw struct smb_proto_option_defs {
6725331Samw 	char *name;	/* display name -- remove protocol identifier */
6735331Samw 	int smb_index;
6745331Samw 	int32_t minval;
6755331Samw 	int32_t maxval; /* In case of length of string this should be max */
6765331Samw 	int (*validator)(int, char *);
6775331Samw 	int32_t	refresh;
6785331Samw } smb_proto_options[] = {
6795331Samw 	{ SMB_CD_SYS_CMNT,
6805331Samw 	    SMB_CI_SYS_CMNT, 0, MAX_VALUE_BUFLEN,
6815331Samw 	    string_length_check_validator, SMB_REFRESH_REFRESH},
6825331Samw 	{ SMB_CD_MAX_WORKERS,
6835331Samw 	    SMB_CI_MAX_WORKERS, 64, 1024, range_check_validator,
6845331Samw 	    SMB_REFRESH_REFRESH},
6855331Samw 	{ SMB_CD_NBSCOPE,
6865331Samw 	    SMB_CI_NBSCOPE, 0, MAX_VALUE_BUFLEN,
6875331Samw 	    string_length_check_validator, SMB_REFRESH_REFRESH},
6885331Samw 	{ SMB_CD_RDR_IPCMODE,
6895331Samw 	    SMB_CI_RDR_IPCMODE, 0, 0, ipc_mode_validator, SMB_REFRESH_REFRESH},
6905331Samw 	{ SMB_CD_LM_LEVEL,
6915331Samw 	    SMB_CI_LM_LEVEL, 2, 5, range_check_validator, SMB_REFRESH_REFRESH},
6925331Samw 	{ SMB_CD_KEEPALIVE,
6935331Samw 	    SMB_CI_KEEPALIVE, 20, 5400, range_check_validator_zero_ok,
6945331Samw 	    SMB_REFRESH_REFRESH},
6955331Samw 	{ SMB_CD_WINS_SRV1,
6965331Samw 	    SMB_CI_WINS_SRV1, 0, MAX_VALUE_BUFLEN,
6975331Samw 	    ip_address_validator_empty_ok, SMB_REFRESH_REFRESH},
6985331Samw 	{ SMB_CD_WINS_SRV2,
6995331Samw 	    SMB_CI_WINS_SRV2, 0, MAX_VALUE_BUFLEN,
7005331Samw 	    ip_address_validator_empty_ok, SMB_REFRESH_REFRESH},
7015331Samw 	{ SMB_CD_WINS_EXCL,
7025331Samw 	    SMB_CI_WINS_EXCL, 0, MAX_VALUE_BUFLEN,
7035331Samw 	    ip_address_csv_list_validator_empty_ok, SMB_REFRESH_REFRESH},
7045331Samw 	{ SMB_CD_SIGNING_ENABLE,
7055331Samw 	    SMB_CI_SIGNING_ENABLE, 0, 0, true_false_validator,
7065331Samw 	    SMB_REFRESH_REFRESH},
7075331Samw 	{ SMB_CD_SIGNING_REQD,
7085331Samw 	    SMB_CI_SIGNING_REQD, 0, 0, true_false_validator,
7095331Samw 	    SMB_REFRESH_REFRESH},
7105331Samw 	{ SMB_CD_RESTRICT_ANON,
7115331Samw 	    SMB_CI_RESTRICT_ANON, 0, 0, true_false_validator,
7125331Samw 	    SMB_REFRESH_REFRESH},
7135331Samw 	{ SMB_CD_DOMAIN_SRV,
7145331Samw 	    SMB_CI_DOMAIN_SRV, 0, MAX_VALUE_BUFLEN,
7155331Samw 	    ip_address_validator_empty_ok, SMB_REFRESH_REFRESH},
7165331Samw 	{ SMB_CD_ADS_ENABLE,
7175331Samw 	    SMB_CI_ADS_ENABLE, 0, 0, true_false_validator, SMB_REFRESH_REFRESH},
7185331Samw 	{ SMB_CD_ADS_USER,
7195331Samw 	    SMB_CI_ADS_USER, 0, MAX_VALUE_BUFLEN,
7205331Samw 	    string_length_check_validator, SMB_REFRESH_REFRESH},
7215331Samw 	{ SMB_CD_ADS_USER_CONTAINER,
7225331Samw 	    SMB_CI_ADS_USER_CONTAINER, 0, MAX_VALUE_BUFLEN,
7235331Samw 	    string_length_check_validator, SMB_REFRESH_REFRESH},
7245331Samw 	{ SMB_CD_ADS_DOMAIN,
7255331Samw 	    SMB_CI_ADS_DOMAIN, 0, MAX_VALUE_BUFLEN,
7265331Samw 	    string_length_check_validator, SMB_REFRESH_REFRESH},
7275331Samw 	{ SMB_CD_ADS_PASSWD,
7285331Samw 	    SMB_CI_ADS_PASSWD, 0, MAX_VALUE_BUFLEN,
7295331Samw 	    string_length_check_validator, SMB_REFRESH_REFRESH},
7305331Samw 	{ SMB_CD_ADS_IPLOOKUP,
7315331Samw 	    SMB_CI_ADS_IPLOOKUP, 0, 0, true_false_validator,
7325331Samw 	    SMB_REFRESH_REFRESH},
7335331Samw 	{ SMB_CD_ADS_SITE,
7345331Samw 	    SMB_CI_ADS_SITE, 0, MAX_VALUE_BUFLEN,
7355331Samw 	    string_length_check_validator, SMB_REFRESH_REFRESH},
7365331Samw 	{ SMB_CD_DYNDNS_ENABLE,
7375331Samw 	    SMB_CI_DYNDNS_ENABLE, 0, 0, true_false_validator,
7385331Samw 	    SMB_REFRESH_REFRESH},
7395331Samw 	{ SMB_CD_DYNDNS_RETRY_SEC,
7405331Samw 	    SMB_CI_DYNDNS_RETRY_SEC, 0, 20, range_check_validator,
7415331Samw 	    SMB_REFRESH_REFRESH},
7425331Samw 	{ SMB_CD_DYNDNS_RETRY_COUNT,
7435331Samw 	    SMB_CI_DYNDNS_RETRY_COUNT, 3, 5, range_check_validator,
7445331Samw 	    SMB_REFRESH_REFRESH},
7455331Samw 	{ SMB_CD_AUTOHOME_MAP,
7465331Samw 	    SMB_CI_AUTOHOME_MAP, 0, MAX_VALUE_BUFLEN,
7475331Samw 	    path_validator},
7485331Samw 	{NULL, -1, 0, 0, NULL}
7495331Samw };
7505331Samw 
7515331Samw /*
7525331Samw  * Check the range of value as int range.
7535331Samw  */
7545331Samw static int
7555331Samw range_check_validator(int index, char *value)
7565331Samw {
7575331Samw 	int ret = SA_OK;
7585331Samw 
7595331Samw 	if (!is_a_number(value)) {
7605331Samw 		ret = SA_BAD_VALUE;
7615331Samw 	} else {
7625331Samw 		int val;
7635331Samw 		val = strtoul(value, NULL, 0);
7645331Samw 		if (val < smb_proto_options[index].minval ||
7655331Samw 		    val > smb_proto_options[index].maxval)
7665331Samw 			ret = SA_BAD_VALUE;
7675331Samw 	}
7685331Samw 	return (ret);
7695331Samw }
7705331Samw 
7715331Samw /*
7725331Samw  * Check the range of value as int range.
7735331Samw  */
7745331Samw static int
7755331Samw range_check_validator_zero_ok(int index, char *value)
7765331Samw {
7775331Samw 	int ret = SA_OK;
7785331Samw 
7795331Samw 	if (!is_a_number(value)) {
7805331Samw 		ret = SA_BAD_VALUE;
7815331Samw 	} else {
7825331Samw 		int val;
7835331Samw 		val = strtoul(value, NULL, 0);
7845331Samw 		if (val == 0)
7855331Samw 			ret = SA_OK;
7865331Samw 		else {
7875331Samw 			if (val < smb_proto_options[index].minval ||
7885331Samw 			    val > smb_proto_options[index].maxval)
7895331Samw 			ret = SA_BAD_VALUE;
7905331Samw 		}
7915331Samw 	}
7925331Samw 	return (ret);
7935331Samw }
7945331Samw 
7955331Samw /*
7965331Samw  * Check the length of the string
7975331Samw  */
7985331Samw static int
7995331Samw string_length_check_validator(int index, char *value)
8005331Samw {
8015331Samw 	int ret = SA_OK;
8025331Samw 
8035331Samw 	if (value == NULL)
8045331Samw 		return (SA_BAD_VALUE);
8055331Samw 	if (strlen(value) > smb_proto_options[index].maxval)
8065331Samw 		ret = SA_BAD_VALUE;
8075331Samw 	return (ret);
8085331Samw }
8095331Samw 
8105331Samw /*
8115331Samw  * Check yes/no
8125331Samw  */
8135331Samw /*ARGSUSED*/
8145331Samw static int
8155331Samw true_false_validator(int index, char *value)
8165331Samw {
8175331Samw 	if (value == NULL)
8185331Samw 		return (SA_BAD_VALUE);
8195331Samw 	if ((strcasecmp(value, "true") == 0) ||
8205331Samw 	    (strcasecmp(value, "false") == 0))
8215331Samw 		return (SA_OK);
8225331Samw 	return (SA_BAD_VALUE);
8235331Samw }
8245331Samw 
8255331Samw /*
8265331Samw  * Check IP address.
8275331Samw  */
8285331Samw /*ARGSUSED*/
8295331Samw static int
8305331Samw ip_address_validator_empty_ok(int index, char *value)
8315331Samw {
8325331Samw 	char sbytes[16];
8335331Samw 	int len;
8345331Samw 
8355331Samw 	if (value == NULL)
8365331Samw 		return (SA_OK);
8375331Samw 	len = strlen(value);
8385331Samw 	if (len == 0)
8395331Samw 		return (SA_OK);
8405331Samw 	if (inet_pton(AF_INET, value, (void *)sbytes) != 1)
8415331Samw 		return (SA_BAD_VALUE);
8425331Samw 
8435331Samw 	return (SA_OK);
8445331Samw }
8455331Samw 
8465331Samw /*
8475331Samw  * Check IP address list
8485331Samw  */
8495331Samw /*ARGSUSED*/
8505331Samw static int
8515331Samw ip_address_csv_list_validator_empty_ok(int index, char *value)
8525331Samw {
8535331Samw 	char sbytes[16];
8545331Samw 	char *ip, *tmp, *ctx;
8555331Samw 
8565331Samw 	if (value == NULL || *value == '\0')
8575331Samw 		return (SA_OK);
8585331Samw 
8595331Samw 	if (strlen(value) > MAX_VALUE_BUFLEN)
8605331Samw 		return (SA_BAD_VALUE);
8615331Samw 
8625331Samw 	if ((tmp = strdup(value)) == NULL)
8635331Samw 		return (SA_NO_MEMORY);
8645331Samw 
8655331Samw 	ip = strtok_r(tmp, ",", &ctx);
8665331Samw 	while (ip) {
8675331Samw 		if (strlen(ip) == 0) {
8685331Samw 			free(tmp);
8695331Samw 			return (SA_BAD_VALUE);
8705331Samw 		}
8715331Samw 		if (*ip != 0) {
8725331Samw 			if (inet_pton(AF_INET, ip,
8735331Samw 			    (void *)sbytes) != 1) {
8745331Samw 				free(tmp);
8755331Samw 				return (SA_BAD_VALUE);
8765331Samw 			}
8775331Samw 		}
8785331Samw 		ip = strtok_r(0, ",", &ctx);
8795331Samw 	}
8805331Samw 
8815331Samw 	free(tmp);
8825331Samw 	return (SA_OK);
8835331Samw }
8845331Samw 
8855331Samw /*
8865331Samw  * Check IPC mode
8875331Samw  */
8885331Samw /*ARGSUSED*/
8895331Samw static int
8905331Samw ipc_mode_validator(int index, char *value)
8915331Samw {
8925331Samw 	if (value == NULL)
8935331Samw 		return (SA_BAD_VALUE);
8945331Samw 	if (strcasecmp(value, "anon") == 0)
8955331Samw 		return (SA_OK);
8965331Samw 	if (strcasecmp(value, "auth") == 0)
8975331Samw 		return (SA_OK);
8985331Samw 	return (SA_BAD_VALUE);
8995331Samw }
9005331Samw 
9015331Samw /*
9025331Samw  * Check path
9035331Samw  */
9045331Samw /*ARGSUSED*/
9055331Samw static int
9065331Samw path_validator(int index, char *value)
9075331Samw {
9085331Samw 	struct stat buffer;
9095331Samw 	int fd, status;
9105331Samw 
9115331Samw 	if (value == NULL)
9125331Samw 		return (SA_BAD_VALUE);
9135331Samw 
9145331Samw 	fd = open(value, O_RDONLY);
9155331Samw 	if (fd < 0)
9165331Samw 		return (SA_BAD_VALUE);
9175331Samw 
9185331Samw 	status = fstat(fd, &buffer);
9195331Samw 	(void) close(fd);
9205331Samw 
9215331Samw 	if (status < 0)
9225331Samw 		return (SA_BAD_VALUE);
9235331Samw 
9245331Samw 	if (buffer.st_mode & S_IFDIR)
9255331Samw 		return (SA_OK);
9265331Samw 	return (SA_BAD_VALUE);
9275331Samw }
9285331Samw 
9295331Samw /*
9305331Samw  * the protoset holds the defined options so we don't have to read
9315331Samw  * them multiple times
9325331Samw  */
9335331Samw static sa_protocol_properties_t protoset;
9345331Samw 
9355331Samw static int
9365331Samw findprotoopt(char *name)
9375331Samw {
9385331Samw 	int i;
9395331Samw 	for (i = 0; smb_proto_options[i].name != NULL; i++) {
9405331Samw 		if (strcasecmp(smb_proto_options[i].name, name) == 0)
9415331Samw 			return (i);
9425331Samw 	}
9435331Samw 	return (-1);
9445331Samw }
9455331Samw 
9465331Samw /*
9475331Samw  * smb_load_proto_properties()
9485331Samw  *
9495331Samw  * read the smb config values from SMF.
9505331Samw  */
9515331Samw 
9525331Samw static int
9535331Samw smb_load_proto_properties()
9545331Samw {
9555331Samw 	sa_property_t prop;
9565331Samw 	int index;
9575331Samw 	char *value;
9585331Samw 
9595331Samw 	protoset = sa_create_protocol_properties(SMB_PROTOCOL_NAME);
9605331Samw 	if (protoset == NULL)
9615331Samw 		return (SA_NO_MEMORY);
9625331Samw 
9635331Samw 	if (smb_config_load() != 0)
9645331Samw 		return (SA_CONFIG_ERR);
9655331Samw 	for (index = 0; smb_proto_options[index].name != NULL; index++) {
9665331Samw 		value = smb_config_getenv(smb_proto_options[index].smb_index);
9675331Samw 		prop = sa_create_property(
968*5454Sdougm 		    smb_proto_options[index].name, value != NULL ? value : "");
969*5454Sdougm 		if (value != NULL)
970*5454Sdougm 			free(value);
971*5454Sdougm 		if (prop != NULL)
972*5454Sdougm 			(void) sa_add_protocol_property(protoset, prop);
9735331Samw 	}
9745331Samw 	return (SA_OK);
9755331Samw }
9765331Samw 
9775331Samw /*
9785331Samw  * smb_share_init()
9795331Samw  *
9805331Samw  * Initialize the smb plugin.
9815331Samw  */
9825331Samw 
9835331Samw static int
9845331Samw smb_share_init(void)
9855331Samw {
9865331Samw 	int ret = SA_OK;
9875331Samw 
9885331Samw 	if (sa_plugin_ops.sa_init != smb_share_init)
9895331Samw 		return (SA_SYSTEM_ERR);
9905331Samw 
9915331Samw 	if (smb_load_proto_properties() != SA_OK)
9925331Samw 		return (SA_SYSTEM_ERR);
9935331Samw 
9945331Samw 	return (ret);
9955331Samw }
9965331Samw 
9975331Samw /*
9985331Samw  * smb_share_fini()
9995331Samw  *
10005331Samw  */
10015331Samw static void
10025331Samw smb_share_fini(void)
10035331Samw {
10045331Samw 	xmlFreeNode(protoset);
10055331Samw 	protoset = NULL;
10065331Samw }
10075331Samw 
10085331Samw /*
10095331Samw  * smb_get_proto_set()
10105331Samw  *
10115331Samw  * Return an optionset with all the protocol specific properties in
10125331Samw  * it.
10135331Samw  */
10145331Samw static sa_protocol_properties_t
10155331Samw smb_get_proto_set(void)
10165331Samw {
10175331Samw 	return (protoset);
10185331Samw }
10195331Samw 
10205331Samw /*
10215331Samw  * How long to wait for service to come online
10225331Samw  */
10235331Samw #define	WAIT_FOR_SERVICE	15
10245331Samw 
10255331Samw /*
10265331Samw  * smb_enable_service()
10275331Samw  *
10285331Samw  */
10295331Samw static int
10305331Samw smb_enable_service(void)
10315331Samw {
10325331Samw 	int i;
10335331Samw 	int ret = SA_OK;
10345331Samw 
10355331Samw 	if (!smb_isonline()) {
10365331Samw 		if (smf_enable_instance(SMBD_DEFAULT_INSTANCE_FMRI, 0) != 0) {
10375331Samw 			(void) fprintf(stderr,
10385331Samw 			    dgettext(TEXT_DOMAIN,
10395331Samw 			    "%s failed to restart: %s\n"),
10405331Samw 			    scf_strerror(scf_error()));
10415331Samw 			return (SA_CONFIG_ERR);
10425331Samw 		}
10435331Samw 
10445331Samw 		/* Wait for service to come online */
10455331Samw 		for (i = 0; i < WAIT_FOR_SERVICE; i++) {
10465331Samw 			if (smb_isonline()) {
10475331Samw 				ret = SA_OK;
10485331Samw 				break;
10495331Samw 			} else {
10505331Samw 				ret = SA_BUSY;
10515331Samw 				(void) sleep(1);
10525331Samw 			}
10535331Samw 		}
10545331Samw 	}
10555331Samw 	return (ret);
10565331Samw }
10575331Samw 
10585331Samw /*
10595331Samw  * smb_validate_proto_prop(index, name, value)
10605331Samw  *
10615331Samw  * Verify that the property specified by name can take the new
10625331Samw  * value. This is a sanity check to prevent bad values getting into
10635331Samw  * the default files.
10645331Samw  */
10655331Samw static int
10665331Samw smb_validate_proto_prop(int index, char *name, char *value)
10675331Samw {
10685331Samw 	if ((name == NULL) || (index < 0))
10695331Samw 		return (SA_BAD_VALUE);
10705331Samw 
10715331Samw 	if (smb_proto_options[index].validator == NULL)
10725331Samw 		return (SA_OK);
10735331Samw 
10745331Samw 	if (smb_proto_options[index].validator(index, value) == SA_OK)
10755331Samw 		return (SA_OK);
10765331Samw 	return (SA_BAD_VALUE);
10775331Samw }
10785331Samw 
10795331Samw /*
10805331Samw  * smb_set_proto_prop(prop)
10815331Samw  *
10825331Samw  * check that prop is valid.
10835331Samw  */
10845331Samw /*ARGSUSED*/
10855331Samw static int
10865331Samw smb_set_proto_prop(sa_property_t prop)
10875331Samw {
10885331Samw 	int ret = SA_OK;
10895331Samw 	char *name;
10905331Samw 	char *value;
10915331Samw 	int index = -1;
10925331Samw 
10935331Samw 	name = sa_get_property_attr(prop, "type");
10945331Samw 	value = sa_get_property_attr(prop, "value");
10955331Samw 	if (name != NULL && value != NULL) {
10965331Samw 		index = findprotoopt(name);
10975331Samw 		if (index >= 0) {
10985331Samw 			/* should test for valid value */
10995331Samw 			ret = smb_validate_proto_prop(index, name, value);
11005331Samw 			if (ret == SA_OK) {
11015331Samw 				/* Save to SMF */
11025331Samw 				smb_config_setenv(
11035331Samw 				    smb_proto_options[index].smb_index, value);
11045331Samw 				/*
11055331Samw 				 * Specialized refresh mechanisms can
11065331Samw 				 * be flagged in the proto_options and
11075331Samw 				 * processed here.
11085331Samw 				 */
11095331Samw 				if (smb_proto_options[index].refresh &
11105331Samw 				    SMB_REFRESH_REFRESH)
11115331Samw 					(void) smf_refresh_instance(
11125331Samw 					    SMBD_DEFAULT_INSTANCE_FMRI);
11135331Samw 				else if (smb_proto_options[index].refresh &
11145331Samw 				    SMB_REFRESH_RESTART)
11155331Samw 					(void) smf_restart_instance(
11165331Samw 					    SMBD_DEFAULT_INSTANCE_FMRI);
11175331Samw 			}
11185331Samw 		}
11195331Samw 	}
11205331Samw 	if (name != NULL)
11215331Samw 		sa_free_attr_string(name);
11225331Samw 	if (value != NULL)
11235331Samw 		sa_free_attr_string(value);
11245331Samw 
11255331Samw 	return (ret);
11265331Samw }
11275331Samw 
11285331Samw /*
11295331Samw  * smb_get_status()
11305331Samw  *
11315331Samw  * What is the current status of the smbd? We use the SMF state here.
11325331Samw  * Caller must free the returned value.
11335331Samw  */
11345331Samw 
11355331Samw static char *
11365331Samw smb_get_status(void)
11375331Samw {
11385331Samw 	char *state = NULL;
11395331Samw 	state = smf_get_state(SMBD_DEFAULT_INSTANCE_FMRI);
11405331Samw 	return (state != NULL ? state : "-");
11415331Samw }
11425331Samw 
11435331Samw /*
11445331Samw  * This protocol plugin require resource names
11455331Samw  */
11465331Samw static uint64_t
11475331Samw smb_share_features(void)
11485331Samw {
11495331Samw 	return (SA_FEATURE_RESOURCE | SA_FEATURE_ALLOWSUBDIRS |
11505331Samw 	    SA_FEATURE_ALLOWPARDIRS);
11515331Samw }
11525331Samw 
11535331Samw /*
11545331Samw  * This should be used to convert lmshare_info to sa_resource_t
11555331Samw  * Should only be needed to build temp shares/resources to be
11565331Samw  * supplied to sharemanager to display temp shares.
11575331Samw  */
11585331Samw static int
11595331Samw smb_build_tmp_sa_resource(sa_handle_t handle, lmshare_info_t *si)
11605331Samw {
11615331Samw 	int err;
11625331Samw 	sa_share_t share;
11635331Samw 	sa_group_t group;
11645331Samw 	sa_resource_t resource;
11655331Samw 
11665331Samw 	if (si == NULL)
11675331Samw 		return (SA_INVALID_NAME);
11685331Samw 
11695331Samw 	/*
11705331Samw 	 * First determine if the "share path" is already shared
11715331Samw 	 * somewhere. If it is, we have to use it as the authority on
11725331Samw 	 * where the transient share lives so will use it's parent
11735331Samw 	 * group. If it doesn't exist, it needs to land in "smb".
11745331Samw 	 */
11755331Samw 
11765331Samw 	share = sa_find_share(handle, si->directory);
11775331Samw 	if (share != NULL) {
11785331Samw 		group = sa_get_parent_group(share);
11795331Samw 	} else {
11805331Samw 		group = smb_get_smb_share_group(handle);
11815331Samw 		if (group == NULL)
11825331Samw 			return (SA_NO_SUCH_GROUP);
11835331Samw 		share = sa_get_share(group, si->directory);
11845331Samw 		if (share == NULL) {
11855331Samw 			share = sa_add_share(group, si->directory,
11865331Samw 			    SA_SHARE_TRANSIENT, &err);
11875331Samw 			if (share == NULL)
11885331Samw 				return (SA_NO_SUCH_PATH);
11895331Samw 		}
11905331Samw 	}
11915331Samw 
11925331Samw 	/*
11935331Samw 	 * Now handle the resource. Make sure that the resource is
11945331Samw 	 * transient and added to the share.
11955331Samw 	 */
11965331Samw 	resource = sa_get_share_resource(share, si->share_name);
11975331Samw 	if (resource == NULL) {
11985331Samw 		resource = sa_add_resource(share,
11995331Samw 		    si->share_name, SA_SHARE_TRANSIENT, &err);
12005331Samw 		if (resource == NULL)
12015331Samw 			return (SA_NO_SUCH_RESOURCE);
12025331Samw 	}
12035331Samw 
12045331Samw 	/* set resource attributes now */
12055331Samw 	(void) sa_set_resource_attr(resource, "description", si->comment);
12065331Samw 	(void) sa_set_resource_attr(resource, SHOPT_AD_CONTAINER,
12075331Samw 	    si->container);
12085331Samw 
12095331Samw 	return (SA_OK);
12105331Samw }
12115331Samw 
12125331Samw /*
12135331Samw  * Return smb transient shares.  Note that we really want to look at
12145331Samw  * all current shares from SMB in order to determine this. Transient
12155331Samw  * shares should be those that don't appear in either the SMF or ZFS
12165331Samw  * configurations.  Those that are in the repositories will be
12175331Samw  * filtered out by smb_build_tmp_sa_resource.
12185331Samw  */
12195331Samw static int
12205331Samw smb_list_transient(sa_handle_t handle)
12215331Samw {
12225331Samw 	int i, offset, num;
12235331Samw 	lmshare_list_t list;
12245331Samw 	int res;
12255331Samw 
12265331Samw 	num = lmshrd_num_shares();
12275331Samw 	if (num <= 0)
12285331Samw 		return (SA_OK);
12295331Samw 	offset = 0;
12305331Samw 	while (lmshrd_list(offset, &list) != NERR_InternalError) {
12315331Samw 		if (list.no == 0)
12325331Samw 			break;
12335331Samw 		for (i = 0; i < list.no; i++) {
12345331Samw 			res = smb_build_tmp_sa_resource(handle,
12355331Samw 			    &(list.smbshr[i]));
12365331Samw 			if (res != SA_OK)
12375331Samw 				return (res);
12385331Samw 		}
12395331Samw 		offset += list.no;
12405331Samw 	}
12415331Samw 
12425331Samw 	return (SA_OK);
12435331Samw }
12445331Samw 
12455331Samw /*
12465331Samw  * fix_resource_name(share, name,  prefix)
12475331Samw  *
12485331Samw  * Construct a name where the ZFS dataset has the prefix replaced with "name".
12495331Samw  */
12505331Samw static char *
12515331Samw fix_resource_name(sa_share_t share, char *name, char *prefix)
12525331Samw {
12535331Samw 	char *dataset = NULL;
12545331Samw 	char *newname = NULL;
12555331Samw 	size_t psize;
12565331Samw 	size_t nsize;
12575331Samw 
12585331Samw 	dataset = sa_get_share_attr(share, "dataset");
12595331Samw 
12605331Samw 	if (dataset != NULL && strcmp(dataset, prefix) != 0) {
12615331Samw 		psize = strlen(prefix);
12625331Samw 		if (strncmp(dataset, prefix, psize) == 0) {
12635331Samw 			/* need string plus ',' and NULL */
12645331Samw 			nsize = (strlen(dataset) - psize) + strlen(name) + 2;
12655331Samw 			newname = calloc(nsize, 1);
12665331Samw 			if (newname != NULL) {
12675331Samw 				(void) snprintf(newname, nsize, "%s%s", name,
12685331Samw 				    dataset + psize);
12695331Samw 				sa_fix_resource_name(newname);
12705331Samw 			}
12715331Samw 			sa_free_attr_string(dataset);
12725331Samw 			return (newname);
12735331Samw 		}
12745331Samw 	}
12755331Samw 	if (dataset != NULL)
12765331Samw 		sa_free_attr_string(dataset);
12775331Samw 	return (strdup(name));
12785331Samw }
12795331Samw 
12805331Samw /*
12815331Samw  * smb_parse_optstring(group, options)
12825331Samw  *
12835331Samw  * parse a compact option string into individual options. This allows
12845331Samw  * ZFS sharesmb and sharemgr "share" command to work.  group can be a
12855331Samw  * group, a share or a resource.
12865331Samw  */
12875331Samw static int
12885331Samw smb_parse_optstring(sa_group_t group, char *options)
12895331Samw {
12905331Samw 	char *dup;
12915331Samw 	char *base;
12925331Samw 	char *lasts;
12935331Samw 	char *token;
12945331Samw 	sa_optionset_t optionset;
12955331Samw 	sa_group_t parent = NULL;
12965331Samw 	sa_resource_t resource = NULL;
12975331Samw 	int iszfs = 0;
12985331Samw 	int persist = 0;
12995331Samw 	int need_optionset = 0;
13005331Samw 	int ret = SA_OK;
13015331Samw 	sa_property_t prop;
13025331Samw 
13035331Samw 	/*
13045331Samw 	 * In order to not attempt to change ZFS properties unless
13055331Samw 	 * absolutely necessary, we never do it in the legacy parsing
13065331Samw 	 * so we need to keep track of this.
13075331Samw 	 */
13085331Samw 	if (sa_is_share(group)) {
13095331Samw 		char *zfs;
13105331Samw 
13115331Samw 		parent = sa_get_parent_group(group);
13125331Samw 		if (parent != NULL) {
13135331Samw 			zfs = sa_get_group_attr(parent, "zfs");
13145331Samw 			if (zfs != NULL) {
13155331Samw 				sa_free_attr_string(zfs);
13165331Samw 				iszfs = 1;
13175331Samw 			}
13185331Samw 		}
13195331Samw 	} else {
13205331Samw 		iszfs = sa_group_is_zfs(group);
13215331Samw 		/*
13225331Samw 		 * If a ZFS group, then we need to see if a resource
13235331Samw 		 * name is being set. If so, bail with
13245331Samw 		 * SA_PROP_SHARE_ONLY, so we come back in with a share
13255331Samw 		 * instead of a group.
13265331Samw 		 */
13275331Samw 		if (strncmp(options, "name=", sizeof ("name=") - 1) == 0 ||
13285331Samw 		    strstr(options, ",name=") != NULL) {
13295331Samw 			return (SA_PROP_SHARE_ONLY);
13305331Samw 		}
13315331Samw 	}
13325331Samw 
13335331Samw 	/* do we have an existing optionset? */
13345331Samw 	optionset = sa_get_optionset(group, "smb");
13355331Samw 	if (optionset == NULL) {
13365331Samw 		/* didn't find existing optionset so create one */
13375331Samw 		optionset = sa_create_optionset(group, "smb");
13385331Samw 		if (optionset == NULL)
13395331Samw 			return (SA_NO_MEMORY);
13405331Samw 	} else {
13415331Samw 		/*
13425331Samw 		 * If an optionset already exists, we've come through
13435331Samw 		 * twice so ignore the second time.
13445331Samw 		 */
13455331Samw 		return (ret);
13465331Samw 	}
13475331Samw 
13485331Samw 	/* We need a copy of options for the next part. */
13495331Samw 	dup = strdup(options);
13505331Samw 	if (dup == NULL)
13515331Samw 		return (SA_NO_MEMORY);
13525331Samw 
13535331Samw 	/*
13545331Samw 	 * SMB properties are straightforward and are strings,
13555331Samw 	 * integers or booleans.  Properties are separated by
13565331Samw 	 * commas. It will be necessary to parse quotes due to some
13575331Samw 	 * strings not having a restricted characters set.
13585331Samw 	 *
13595331Samw 	 * Note that names will create a resource. For now, if there
13605331Samw 	 * is a set of properties "before" the first name="", those
13615331Samw 	 * properties will be placed on the group.
13625331Samw 	 */
13635331Samw 	persist = sa_is_persistent(group);
13645331Samw 	base = dup;
13655331Samw 	token = dup;
13665331Samw 	lasts = NULL;
13675331Samw 	while (token != NULL && ret == SA_OK) {
13685331Samw 		ret = SA_OK;
13695331Samw 		token = strtok_r(base, ",", &lasts);
13705331Samw 		base = NULL;
13715331Samw 		if (token != NULL) {
13725331Samw 			char *value;
13735331Samw 			/*
13745331Samw 			 * All SMB properties have values so there
13755331Samw 			 * MUST be an '=' character.  If it doesn't,
13765331Samw 			 * it is a syntax error.
13775331Samw 			 */
13785331Samw 			value = strchr(token, '=');
13795331Samw 			if (value != NULL) {
13805331Samw 				*value++ = '\0';
13815331Samw 			} else {
13825331Samw 				ret = SA_SYNTAX_ERR;
13835331Samw 				break;
13845331Samw 			}
13855331Samw 			/*
13865331Samw 			 * We may need to handle a "name" property
13875331Samw 			 * that is a ZFS imposed resource name. Each
13885331Samw 			 * name would trigger getting a new "resource"
13895331Samw 			 * to put properties on. For now, assume no
13905331Samw 			 * "name" property for special handling.
13915331Samw 			 */
13925331Samw 
13935331Samw 			if (strcmp(token, "name") == 0) {
13945331Samw 				char *prefix;
13955331Samw 				char *name = NULL;
13965331Samw 				/*
13975331Samw 				 * We have a name, so now work on the
13985331Samw 				 * resource level. We have a "share"
13995331Samw 				 * in "group" due to the caller having
14005331Samw 				 * added it. If we are called with a
14015331Samw 				 * group, the check for group/share
14025331Samw 				 * at the beginning of this function
14035331Samw 				 * will bail out the parse if there is a
14045331Samw 				 * "name" but no share.
14055331Samw 				 */
14065331Samw 				if (!iszfs) {
14075331Samw 					ret = SA_SYNTAX_ERR;
14085331Samw 					break;
14095331Samw 				}
14105331Samw 				/*
14115331Samw 				 * Make sure the parent group has the
14125331Samw 				 * "prefix" property since we will
14135331Samw 				 * need to use this for constructing
14145331Samw 				 * inherited name= values.
14155331Samw 				 */
14165331Samw 				prefix = sa_get_group_attr(parent, "prefix");
14175331Samw 				if (prefix == NULL) {
14185331Samw 					prefix = sa_get_group_attr(parent,
14195331Samw 					    "name");
14205331Samw 					if (prefix != NULL) {
14215331Samw 						(void) sa_set_group_attr(parent,
14225331Samw 						    "prefix", prefix);
14235331Samw 					}
14245331Samw 				}
14255331Samw 				name = fix_resource_name((sa_share_t)group,
14265331Samw 				    value, prefix);
14275331Samw 				if (name != NULL) {
14285331Samw 					resource = sa_add_resource(
14295331Samw 					    (sa_share_t)group, name,
14305331Samw 					    SA_SHARE_TRANSIENT, &ret);
14315331Samw 					sa_free_attr_string(name);
14325331Samw 				} else {
14335331Samw 					ret = SA_NO_MEMORY;
14345331Samw 				}
14355331Samw 				if (prefix != NULL)
14365331Samw 					sa_free_attr_string(prefix);
14375331Samw 
14385331Samw 				/* A resource level optionset is needed */
14395331Samw 
14405331Samw 				need_optionset = 1;
14415331Samw 				if (resource == NULL) {
14425331Samw 					ret = SA_NO_MEMORY;
14435331Samw 					break;
14445331Samw 				}
14455331Samw 				continue;
14465331Samw 			}
14475331Samw 
14485331Samw 			if (need_optionset) {
14495331Samw 				optionset = sa_create_optionset(resource,
14505331Samw 				    "smb");
14515331Samw 				need_optionset = 0;
14525331Samw 			}
14535331Samw 
14545331Samw 			prop = sa_create_property(token, value);
14555331Samw 			if (prop == NULL)
14565331Samw 				ret = SA_NO_MEMORY;
14575331Samw 			else
14585331Samw 				ret = sa_add_property(optionset, prop);
14595331Samw 			if (ret != SA_OK)
14605331Samw 				break;
14615331Samw 			if (!iszfs)
14625331Samw 				ret = sa_commit_properties(optionset, !persist);
14635331Samw 		}
14645331Samw 	}
14655331Samw 	free(dup);
14665331Samw 	return (ret);
14675331Samw }
14685331Samw 
14695331Samw /*
14705331Samw  * smb_sprint_option(rbuff, rbuffsize, incr, prop, sep)
14715331Samw  *
14725331Samw  * provides a mechanism to format SMB properties into legacy output
14735331Samw  * format. If the buffer would overflow, it is reallocated and grown
14745331Samw  * as appropriate. Special cases of converting internal form of values
14755331Samw  * to those used by "share" are done. this function does one property
14765331Samw  * at a time.
14775331Samw  */
14785331Samw 
14795331Samw static void
14805331Samw smb_sprint_option(char **rbuff, size_t *rbuffsize, size_t incr,
14815331Samw 			sa_property_t prop, int sep)
14825331Samw {
14835331Samw 	char *name;
14845331Samw 	char *value;
14855331Samw 	int curlen;
14865331Samw 	char *buff = *rbuff;
14875331Samw 	size_t buffsize = *rbuffsize;
14885331Samw 
14895331Samw 	name = sa_get_property_attr(prop, "type");
14905331Samw 	value = sa_get_property_attr(prop, "value");
14915331Samw 	if (buff != NULL)
14925331Samw 		curlen = strlen(buff);
14935331Samw 	else
14945331Samw 		curlen = 0;
14955331Samw 	if (name != NULL) {
14965331Samw 		int len;
14975331Samw 		len = strlen(name) + sep;
14985331Samw 
14995331Samw 		/*
15005331Samw 		 * A future RFE would be to replace this with more
15015331Samw 		 * generic code and to possibly handle more types.
15025331Samw 		 *
15035331Samw 		 * For now, everything else is treated as a string. If
15045331Samw 		 * we get any properties that aren't exactly
15055331Samw 		 * name/value pairs, we may need to
15065331Samw 		 * interpret/transform.
15075331Samw 		 */
15085331Samw 		if (value != NULL)
15095331Samw 			len += 1 + strlen(value);
15105331Samw 
15115331Samw 		while (buffsize <= (curlen + len)) {
15125331Samw 			/* need more room */
15135331Samw 			buffsize += incr;
15145331Samw 			buff = realloc(buff, buffsize);
15155331Samw 			*rbuff = buff;
15165331Samw 			*rbuffsize = buffsize;
15175331Samw 			if (buff == NULL) {
15185331Samw 				/* realloc failed so free everything */
15195331Samw 				if (*rbuff != NULL)
15205331Samw 					free(*rbuff);
15215331Samw 				goto err;
15225331Samw 			}
15235331Samw 		}
15245331Samw 		if (buff == NULL)
15255331Samw 			goto err;
15265331Samw 		(void) snprintf(buff + curlen, buffsize - curlen,
15275331Samw 		    "%s%s=%s", sep ? "," : "",
15285331Samw 		    name, value != NULL ? value : "\"\"");
15295331Samw 
15305331Samw 	}
15315331Samw err:
15325331Samw 	if (name != NULL)
15335331Samw 		sa_free_attr_string(name);
15345331Samw 	if (value != NULL)
15355331Samw 		sa_free_attr_string(value);
15365331Samw }
15375331Samw 
15385331Samw /*
15395331Samw  * smb_format_resource_options(resource, hier)
15405331Samw  *
15415331Samw  * format all the options on the group into a flattened option
15425331Samw  * string. If hier is non-zero, walk up the tree to get inherited
15435331Samw  * options.
15445331Samw  */
15455331Samw 
15465331Samw static char *
15475331Samw smb_format_options(sa_group_t group, int hier)
15485331Samw {
15495331Samw 	sa_optionset_t options = NULL;
15505331Samw 	sa_property_t prop;
15515331Samw 	int sep = 0;
15525331Samw 	char *buff;
15535331Samw 	size_t buffsize;
15545331Samw 
15555331Samw 
15565331Samw 	buff = malloc(OPT_CHUNK);
15575331Samw 	if (buff == NULL)
15585331Samw 		return (NULL);
15595331Samw 
15605331Samw 	buff[0] = '\0';
15615331Samw 	buffsize = OPT_CHUNK;
15625331Samw 
15635331Samw 	/*
15645331Samw 	 * We may have a an optionset relative to this item. format
15655331Samw 	 * these if we find them and then add any security definitions.
15665331Samw 	 */
15675331Samw 
15685331Samw 	options = sa_get_derived_optionset(group, "smb", hier);
15695331Samw 
15705331Samw 	/*
15715331Samw 	 * do the default set first but skip any option that is also
15725331Samw 	 * in the protocol specific optionset.
15735331Samw 	 */
15745331Samw 	if (options != NULL) {
15755331Samw 		for (prop = sa_get_property(options, NULL);
15765331Samw 		    prop != NULL; prop = sa_get_next_property(prop)) {
15775331Samw 			/*
15785331Samw 			 * use this one since we skipped any
15795331Samw 			 * of these that were also in
15805331Samw 			 * optdefault
15815331Samw 			 */
15825331Samw 			smb_sprint_option(&buff, &buffsize, OPT_CHUNK,
15835331Samw 			    prop, sep);
15845331Samw 			if (buff == NULL) {
15855331Samw 				/*
15865331Samw 				 * buff could become NULL if there
15875331Samw 				 * isn't enough memory for
15885331Samw 				 * smb_sprint_option to realloc()
15895331Samw 				 * as necessary. We can't really
15905331Samw 				 * do anything about it at this
15915331Samw 				 * point so we return NULL.  The
15925331Samw 				 * caller should handle the
15935331Samw 				 * failure.
15945331Samw 				 */
15955331Samw 				if (options != NULL)
15965331Samw 					sa_free_derived_optionset(
15975331Samw 					    options);
15985331Samw 				return (buff);
15995331Samw 			}
16005331Samw 			sep = 1;
16015331Samw 		}
16025331Samw 	}
16035331Samw 
16045331Samw 	if (options != NULL)
16055331Samw 		sa_free_derived_optionset(options);
16065331Samw 	return (buff);
16075331Samw }
16085331Samw 
16095331Samw /*
16105331Samw  * smb_rename_resource(resource, newname)
16115331Samw  *
16125331Samw  * Change the current exported name of the resource to newname.
16135331Samw  */
16145331Samw /*ARGSUSED*/
16155331Samw int
16165331Samw smb_rename_resource(sa_handle_t handle, sa_resource_t resource, char *newname)
16175331Samw {
16185331Samw 	int ret = SA_OK;
16195331Samw 	int err;
16205331Samw 	char *oldname;
16215331Samw 
16225331Samw 	oldname = sa_get_resource_attr(resource, "name");
16235331Samw 	if (oldname == NULL)
16245331Samw 		return (SA_NO_SUCH_RESOURCE);
16255331Samw 
16265331Samw 	err = lmshrd_rename(oldname, newname);
16275331Samw 
16285331Samw 	/* improve error values somewhat */
16295331Samw 	switch (err) {
16305331Samw 	case NERR_Success:
16315331Samw 		break;
16325331Samw 	case NERR_InternalError:
16335331Samw 		ret = SA_SYSTEM_ERR;
16345331Samw 		break;
16355331Samw 	case NERR_DuplicateShare:
16365331Samw 		ret = SA_DUPLICATE_NAME;
16375331Samw 		break;
16385331Samw 	default:
16395331Samw 		ret = SA_CONFIG_ERR;
16405331Samw 		break;
16415331Samw 	}
16425331Samw 
16435331Samw 	return (ret);
16445331Samw }
1645