2012-02-04 20:08:53 +00:00
|
|
|
/*
|
|
|
|
* CDDL HEADER START
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the terms of the
|
|
|
|
* Common Development and Distribution License (the "License").
|
|
|
|
* You may not use this file except in compliance with the License.
|
|
|
|
*
|
|
|
|
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
|
|
|
|
* or http://www.opensolaris.org/os/licensing.
|
|
|
|
* See the License for the specific language governing permissions
|
|
|
|
* and limitations under the License.
|
|
|
|
*
|
|
|
|
* When distributing Covered Code, include this CDDL HEADER in each
|
|
|
|
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
|
|
|
|
* If applicable, add the following below this CDDL HEADER, with the
|
|
|
|
* fields enclosed by brackets "[]" replaced with your own identifying
|
|
|
|
* information: Portions Copyright [yyyy] [name of copyright owner]
|
|
|
|
*
|
|
|
|
* CDDL HEADER END
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
|
|
|
|
* Copyright (c) 2011,2012 Turbo Fredriksson <turbo@bayour.com>, based on nfs.c
|
|
|
|
* by Gunnar Beutner
|
2020-07-13 16:19:18 +00:00
|
|
|
* Copyright (c) 2019, 2020 by Delphix. All rights reserved.
|
2012-02-04 20:08:53 +00:00
|
|
|
*
|
|
|
|
* This is an addition to the zfs device driver to add, modify and remove SMB
|
|
|
|
* shares using the 'net share' command that comes with Samba.
|
2013-11-01 19:26:11 +00:00
|
|
|
*
|
2012-02-04 20:08:53 +00:00
|
|
|
* TESTING
|
|
|
|
* Make sure that samba listens to 'localhost' (127.0.0.1) and that the options
|
2019-09-03 00:53:27 +00:00
|
|
|
* 'usershare max shares' and 'usershare owner only' have been reviewed/set
|
2012-02-04 20:08:53 +00:00
|
|
|
* accordingly (see zfs(8) for information).
|
|
|
|
*
|
|
|
|
* Once configuration in samba have been done, test that this
|
|
|
|
* works with the following three commands (in this case, my ZFS
|
|
|
|
* filesystem is called 'share/Test1'):
|
|
|
|
*
|
|
|
|
* (root)# net -U root -S 127.0.0.1 usershare add Test1 /share/Test1 \
|
|
|
|
* "Comment: /share/Test1" "Everyone:F"
|
|
|
|
* (root)# net usershare list | grep -i test
|
|
|
|
* (root)# net -U root -S 127.0.0.1 usershare delete Test1
|
|
|
|
*
|
|
|
|
* The first command will create a user share that gives everyone full access.
|
|
|
|
* To limit the access below that, use normal UNIX commands (chmod, chown etc).
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <time.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
2018-02-16 01:53:18 +00:00
|
|
|
#include <string.h>
|
2012-02-04 20:08:53 +00:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <libzfs.h>
|
|
|
|
#include <libshare.h>
|
|
|
|
#include "libshare_impl.h"
|
|
|
|
#include "smb.h"
|
|
|
|
|
|
|
|
static boolean_t smb_available(void);
|
|
|
|
|
2022-02-28 12:13:10 +00:00
|
|
|
static smb_share_t *smb_shares;
|
2020-07-13 16:19:18 +00:00
|
|
|
static int smb_disable_share(sa_share_impl_t impl_share);
|
|
|
|
static boolean_t smb_is_share_active(sa_share_impl_t impl_share);
|
2020-02-06 17:25:29 +00:00
|
|
|
|
2013-11-01 19:26:11 +00:00
|
|
|
/*
|
2012-02-04 20:08:53 +00:00
|
|
|
* Retrieve the list of SMB shares.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
smb_retrieve_shares(void)
|
|
|
|
{
|
|
|
|
int rc = SA_OK;
|
|
|
|
char file_path[PATH_MAX], line[512], *token, *key, *value;
|
2016-09-30 17:49:16 +00:00
|
|
|
char *dup_value = NULL, *path = NULL, *comment = NULL, *name = NULL;
|
2012-02-04 20:08:53 +00:00
|
|
|
char *guest_ok = NULL;
|
|
|
|
DIR *shares_dir;
|
|
|
|
FILE *share_file_fp = NULL;
|
|
|
|
struct dirent *directory;
|
|
|
|
struct stat eStat;
|
|
|
|
smb_share_t *shares, *new_shares = NULL;
|
|
|
|
|
|
|
|
/* opendir(), stat() */
|
|
|
|
shares_dir = opendir(SHARE_DIR);
|
|
|
|
if (shares_dir == NULL)
|
2013-11-01 19:26:11 +00:00
|
|
|
return (SA_SYSTEM_ERR);
|
2012-02-04 20:08:53 +00:00
|
|
|
|
|
|
|
/* Go through the directory, looking for shares */
|
|
|
|
while ((directory = readdir(shares_dir))) {
|
|
|
|
if (directory->d_name[0] == '.')
|
|
|
|
continue;
|
|
|
|
|
|
|
|
snprintf(file_path, sizeof (file_path),
|
2013-11-01 19:26:11 +00:00
|
|
|
"%s/%s", SHARE_DIR, directory->d_name);
|
2012-02-04 20:08:53 +00:00
|
|
|
|
|
|
|
if (stat(file_path, &eStat) == -1) {
|
|
|
|
rc = SA_SYSTEM_ERR;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!S_ISREG(eStat.st_mode))
|
|
|
|
continue;
|
|
|
|
|
2021-04-08 20:17:38 +00:00
|
|
|
if ((share_file_fp = fopen(file_path, "re")) == NULL) {
|
2012-02-04 20:08:53 +00:00
|
|
|
rc = SA_SYSTEM_ERR;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
name = strdup(directory->d_name);
|
|
|
|
if (name == NULL) {
|
2013-11-01 19:26:11 +00:00
|
|
|
rc = SA_NO_MEMORY;
|
|
|
|
goto out;
|
2012-02-04 20:08:53 +00:00
|
|
|
}
|
|
|
|
|
2013-11-01 19:26:11 +00:00
|
|
|
while (fgets(line, sizeof (line), share_file_fp)) {
|
2012-02-04 20:08:53 +00:00
|
|
|
if (line[0] == '#')
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* Trim trailing new-line character(s). */
|
|
|
|
while (line[strlen(line) - 1] == '\r' ||
|
2013-11-01 19:26:11 +00:00
|
|
|
line[strlen(line) - 1] == '\n')
|
2012-02-04 20:08:53 +00:00
|
|
|
line[strlen(line) - 1] = '\0';
|
|
|
|
|
|
|
|
/* Split the line in two, separated by '=' */
|
|
|
|
token = strchr(line, '=');
|
|
|
|
if (token == NULL)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
key = line;
|
|
|
|
value = token + 1;
|
|
|
|
*token = '\0';
|
|
|
|
|
|
|
|
dup_value = strdup(value);
|
|
|
|
if (dup_value == NULL) {
|
|
|
|
rc = SA_NO_MEMORY;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2016-09-30 17:49:16 +00:00
|
|
|
if (strcmp(key, "path") == 0) {
|
|
|
|
free(path);
|
2012-02-04 20:08:53 +00:00
|
|
|
path = dup_value;
|
2016-09-30 17:49:16 +00:00
|
|
|
} else if (strcmp(key, "comment") == 0) {
|
|
|
|
free(comment);
|
2012-02-04 20:08:53 +00:00
|
|
|
comment = dup_value;
|
2016-09-30 17:49:16 +00:00
|
|
|
} else if (strcmp(key, "guest_ok") == 0) {
|
|
|
|
free(guest_ok);
|
2012-02-04 20:08:53 +00:00
|
|
|
guest_ok = dup_value;
|
2016-09-30 17:49:16 +00:00
|
|
|
} else
|
|
|
|
free(dup_value);
|
|
|
|
|
|
|
|
dup_value = NULL;
|
2012-02-04 20:08:53 +00:00
|
|
|
|
|
|
|
if (path == NULL || comment == NULL || guest_ok == NULL)
|
|
|
|
continue; /* Incomplete share definition */
|
|
|
|
else {
|
|
|
|
shares = (smb_share_t *)
|
2016-12-12 18:46:26 +00:00
|
|
|
malloc(sizeof (smb_share_t));
|
2012-02-04 20:08:53 +00:00
|
|
|
if (shares == NULL) {
|
|
|
|
rc = SA_NO_MEMORY;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2016-09-30 17:49:16 +00:00
|
|
|
(void) strlcpy(shares->name, name,
|
|
|
|
sizeof (shares->name));
|
2012-02-04 20:08:53 +00:00
|
|
|
|
2016-09-30 17:49:16 +00:00
|
|
|
(void) strlcpy(shares->path, path,
|
2013-11-01 19:26:11 +00:00
|
|
|
sizeof (shares->path));
|
2012-02-04 20:08:53 +00:00
|
|
|
|
2016-09-30 17:49:16 +00:00
|
|
|
(void) strlcpy(shares->comment, comment,
|
2013-11-01 19:26:11 +00:00
|
|
|
sizeof (shares->comment));
|
2012-02-04 20:08:53 +00:00
|
|
|
|
|
|
|
shares->guest_ok = atoi(guest_ok);
|
|
|
|
|
|
|
|
shares->next = new_shares;
|
|
|
|
new_shares = shares;
|
|
|
|
|
2016-09-30 17:49:16 +00:00
|
|
|
free(path);
|
|
|
|
free(comment);
|
|
|
|
free(guest_ok);
|
|
|
|
|
2013-11-01 19:26:11 +00:00
|
|
|
path = NULL;
|
|
|
|
comment = NULL;
|
2012-02-04 20:08:53 +00:00
|
|
|
guest_ok = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
out:
|
2016-09-30 17:49:16 +00:00
|
|
|
if (share_file_fp != NULL) {
|
2012-02-04 20:08:53 +00:00
|
|
|
fclose(share_file_fp);
|
2016-09-30 17:49:16 +00:00
|
|
|
share_file_fp = NULL;
|
|
|
|
}
|
2012-02-04 20:08:53 +00:00
|
|
|
|
|
|
|
free(name);
|
|
|
|
free(path);
|
|
|
|
free(comment);
|
|
|
|
free(guest_ok);
|
2016-09-30 17:49:16 +00:00
|
|
|
|
|
|
|
name = NULL;
|
|
|
|
path = NULL;
|
|
|
|
comment = NULL;
|
|
|
|
guest_ok = NULL;
|
2012-02-04 20:08:53 +00:00
|
|
|
}
|
|
|
|
closedir(shares_dir);
|
|
|
|
|
|
|
|
smb_shares = new_shares;
|
|
|
|
|
2013-11-01 19:26:11 +00:00
|
|
|
return (rc);
|
2012-02-04 20:08:53 +00:00
|
|
|
}
|
|
|
|
|
2013-11-01 19:26:11 +00:00
|
|
|
/*
|
2012-02-04 20:08:53 +00:00
|
|
|
* Used internally by smb_enable_share to enable sharing for a single host.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
smb_enable_share_one(const char *sharename, const char *sharepath)
|
|
|
|
{
|
|
|
|
char name[SMB_NAME_MAX], comment[SMB_COMMENT_MAX];
|
|
|
|
|
|
|
|
/* Support ZFS share name regexp '[[:alnum:]_-.: ]' */
|
2018-08-26 19:55:44 +00:00
|
|
|
strlcpy(name, sharename, sizeof (name));
|
2022-02-28 12:13:10 +00:00
|
|
|
for (char *itr = name; *itr != '\0'; ++itr)
|
|
|
|
switch (*itr) {
|
2012-02-04 20:08:53 +00:00
|
|
|
case '/':
|
|
|
|
case '-':
|
|
|
|
case ':':
|
|
|
|
case ' ':
|
2022-02-28 12:13:10 +00:00
|
|
|
*itr = '_';
|
2012-02-04 20:08:53 +00:00
|
|
|
}
|
|
|
|
|
2013-11-01 19:26:11 +00:00
|
|
|
/*
|
|
|
|
* CMD: net -S NET_CMD_ARG_HOST usershare add Test1 /share/Test1 \
|
|
|
|
* "Comment" "Everyone:F"
|
|
|
|
*/
|
|
|
|
snprintf(comment, sizeof (comment), "Comment: %s", sharepath);
|
|
|
|
|
2022-02-28 12:13:10 +00:00
|
|
|
char *argv[] = {
|
|
|
|
(char *)NET_CMD_PATH,
|
|
|
|
(char *)"-S",
|
|
|
|
(char *)NET_CMD_ARG_HOST,
|
|
|
|
(char *)"usershare",
|
|
|
|
(char *)"add",
|
|
|
|
name,
|
|
|
|
(char *)sharepath,
|
|
|
|
comment,
|
|
|
|
(char *)"Everyone:F",
|
|
|
|
NULL,
|
|
|
|
};
|
|
|
|
|
|
|
|
if (libzfs_run_process(argv[0], argv, 0) < 0)
|
2013-11-01 19:26:11 +00:00
|
|
|
return (SA_SYSTEM_ERR);
|
2012-02-04 20:08:53 +00:00
|
|
|
|
|
|
|
/* Reload the share file */
|
|
|
|
(void) smb_retrieve_shares();
|
|
|
|
|
2013-11-01 19:26:11 +00:00
|
|
|
return (SA_OK);
|
2012-02-04 20:08:53 +00:00
|
|
|
}
|
|
|
|
|
2013-11-01 19:26:11 +00:00
|
|
|
/*
|
2012-02-04 20:08:53 +00:00
|
|
|
* Enables SMB sharing for the specified share.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
smb_enable_share(sa_share_impl_t impl_share)
|
|
|
|
{
|
|
|
|
if (!smb_available())
|
2013-11-01 19:26:11 +00:00
|
|
|
return (SA_SYSTEM_ERR);
|
2012-02-04 20:08:53 +00:00
|
|
|
|
2020-07-13 16:19:18 +00:00
|
|
|
if (smb_is_share_active(impl_share))
|
|
|
|
smb_disable_share(impl_share);
|
|
|
|
|
2022-02-28 13:50:28 +00:00
|
|
|
if (impl_share->sa_shareopts == NULL) /* on/off */
|
2013-11-01 19:26:11 +00:00
|
|
|
return (SA_SYSTEM_ERR);
|
2012-02-04 20:08:53 +00:00
|
|
|
|
2022-02-28 13:50:28 +00:00
|
|
|
if (strcmp(impl_share->sa_shareopts, "off") == 0)
|
2013-11-01 19:26:11 +00:00
|
|
|
return (SA_OK);
|
2012-02-04 20:08:53 +00:00
|
|
|
|
|
|
|
/* Magic: Enable (i.e., 'create new') share */
|
2020-07-13 16:19:18 +00:00
|
|
|
return (smb_enable_share_one(impl_share->sa_zfsname,
|
|
|
|
impl_share->sa_mountpoint));
|
2012-02-04 20:08:53 +00:00
|
|
|
}
|
|
|
|
|
2013-11-01 19:26:11 +00:00
|
|
|
/*
|
2012-02-04 20:08:53 +00:00
|
|
|
* Used internally by smb_disable_share to disable sharing for a single host.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
smb_disable_share_one(const char *sharename)
|
|
|
|
{
|
|
|
|
/* CMD: net -S NET_CMD_ARG_HOST usershare delete Test1 */
|
2022-02-28 12:13:10 +00:00
|
|
|
char *argv[] = {
|
|
|
|
(char *)NET_CMD_PATH,
|
|
|
|
(char *)"-S",
|
|
|
|
(char *)NET_CMD_ARG_HOST,
|
|
|
|
(char *)"usershare",
|
|
|
|
(char *)"delete",
|
|
|
|
(char *)sharename,
|
|
|
|
NULL,
|
|
|
|
};
|
|
|
|
|
|
|
|
if (libzfs_run_process(argv[0], argv, 0) < 0)
|
2013-11-01 19:26:11 +00:00
|
|
|
return (SA_SYSTEM_ERR);
|
2012-02-04 20:08:53 +00:00
|
|
|
else
|
2013-11-01 19:26:11 +00:00
|
|
|
return (SA_OK);
|
2012-02-04 20:08:53 +00:00
|
|
|
}
|
|
|
|
|
2013-11-01 19:26:11 +00:00
|
|
|
/*
|
2012-02-04 20:08:53 +00:00
|
|
|
* Disables SMB sharing for the specified share.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
smb_disable_share(sa_share_impl_t impl_share)
|
|
|
|
{
|
|
|
|
if (!smb_available()) {
|
|
|
|
/*
|
|
|
|
* The share can't possibly be active, so nothing
|
|
|
|
* needs to be done to disable it.
|
|
|
|
*/
|
2013-11-01 19:26:11 +00:00
|
|
|
return (SA_OK);
|
2012-02-04 20:08:53 +00:00
|
|
|
}
|
|
|
|
|
2022-03-22 14:50:46 +00:00
|
|
|
for (const smb_share_t *i = smb_shares; i != NULL; i = i->next)
|
|
|
|
if (strcmp(impl_share->sa_mountpoint, i->path) == 0)
|
|
|
|
return (smb_disable_share_one(i->name));
|
2012-02-04 20:08:53 +00:00
|
|
|
|
2013-11-01 19:26:11 +00:00
|
|
|
return (SA_OK);
|
2012-02-04 20:08:53 +00:00
|
|
|
}
|
|
|
|
|
2013-11-01 19:26:11 +00:00
|
|
|
/*
|
2012-02-04 20:08:53 +00:00
|
|
|
* Checks whether the specified SMB share options are syntactically correct.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
smb_validate_shareopts(const char *shareopts)
|
|
|
|
{
|
|
|
|
/* TODO: Accept 'name' and sec/acl (?) */
|
|
|
|
if ((strcmp(shareopts, "off") == 0) || (strcmp(shareopts, "on") == 0))
|
2013-11-01 19:26:11 +00:00
|
|
|
return (SA_OK);
|
2012-02-04 20:08:53 +00:00
|
|
|
|
2013-11-01 19:26:11 +00:00
|
|
|
return (SA_SYNTAX_ERR);
|
2012-02-04 20:08:53 +00:00
|
|
|
}
|
|
|
|
|
2013-11-01 19:26:11 +00:00
|
|
|
/*
|
2012-02-04 20:08:53 +00:00
|
|
|
* Checks whether a share is currently active.
|
|
|
|
*/
|
|
|
|
static boolean_t
|
|
|
|
smb_is_share_active(sa_share_impl_t impl_share)
|
|
|
|
{
|
|
|
|
if (!smb_available())
|
2013-11-01 19:26:11 +00:00
|
|
|
return (B_FALSE);
|
2012-02-04 20:08:53 +00:00
|
|
|
|
|
|
|
/* Retrieve the list of (possible) active shares */
|
|
|
|
smb_retrieve_shares();
|
|
|
|
|
2022-03-22 14:50:46 +00:00
|
|
|
for (const smb_share_t *i = smb_shares; i != NULL; i = i->next)
|
|
|
|
if (strcmp(impl_share->sa_mountpoint, i->path) == 0)
|
2013-11-01 19:26:11 +00:00
|
|
|
return (B_TRUE);
|
2012-02-04 20:08:53 +00:00
|
|
|
|
2013-11-01 19:26:11 +00:00
|
|
|
return (B_FALSE);
|
2012-02-04 20:08:53 +00:00
|
|
|
}
|
|
|
|
|
2020-07-13 16:19:18 +00:00
|
|
|
static int
|
|
|
|
smb_update_shares(void)
|
|
|
|
{
|
|
|
|
/* Not implemented */
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2022-02-28 13:50:28 +00:00
|
|
|
sa_fstype_t libshare_smb_type = {
|
|
|
|
.protocol = "smb",
|
2012-02-04 20:08:53 +00:00
|
|
|
|
|
|
|
.enable_share = smb_enable_share,
|
|
|
|
.disable_share = smb_disable_share,
|
2020-07-13 16:19:18 +00:00
|
|
|
.is_shared = smb_is_share_active,
|
2012-02-04 20:08:53 +00:00
|
|
|
|
|
|
|
.validate_shareopts = smb_validate_shareopts,
|
2020-07-13 16:19:18 +00:00
|
|
|
.commit_shares = smb_update_shares,
|
2012-02-04 20:08:53 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Provides a convenient wrapper for determining SMB availability
|
|
|
|
*/
|
|
|
|
static boolean_t
|
|
|
|
smb_available(void)
|
|
|
|
{
|
2013-04-02 16:27:52 +00:00
|
|
|
struct stat statbuf;
|
|
|
|
|
|
|
|
if (lstat(SHARE_DIR, &statbuf) != 0 ||
|
|
|
|
!S_ISDIR(statbuf.st_mode))
|
2013-11-01 19:26:11 +00:00
|
|
|
return (B_FALSE);
|
2013-04-02 16:27:52 +00:00
|
|
|
|
|
|
|
if (access(NET_CMD_PATH, F_OK) != 0)
|
2013-11-01 19:26:11 +00:00
|
|
|
return (B_FALSE);
|
2013-04-02 16:27:52 +00:00
|
|
|
|
2013-11-01 19:26:11 +00:00
|
|
|
return (B_TRUE);
|
2012-02-04 20:08:53 +00:00
|
|
|
}
|