diff --git a/lib/libshare/os/freebsd/smb.c b/lib/libshare/os/freebsd/smb.c index 445784b756bc..f14e631b2332 100644 --- a/lib/libshare/os/freebsd/smb.c +++ b/lib/libshare/os/freebsd/smb.c @@ -1,132 +1,121 @@ /* * 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) 2020 by Delphix. All rights reserved. */ -#include -#include #include -#include -#include -#include -#include -#include -#include -#include -#include #include #include "libshare_impl.h" -#include "smb.h" static sa_fstype_t *smb_fstype; /* * Enables SMB sharing for the specified share. */ static int smb_enable_share(sa_share_impl_t impl_share) { (void) impl_share; - fprintf(stderr, "No SMB support in FreeBSD yet.\n"); + fputs("No SMB support in FreeBSD yet.\n", stderr); return (SA_NOT_SUPPORTED); } /* * Disables SMB sharing for the specified share. */ static int smb_disable_share(sa_share_impl_t impl_share) { (void) impl_share; - fprintf(stderr, "No SMB support in FreeBSD yet.\n"); + fputs("No SMB support in FreeBSD yet.\n", stderr); return (SA_NOT_SUPPORTED); } /* * Checks whether the specified SMB share options are syntactically correct. */ static int smb_validate_shareopts(const char *shareopts) { (void) shareopts; - fprintf(stderr, "No SMB support in FreeBSD yet.\n"); + fputs("No SMB support in FreeBSD yet.\n", stderr); return (SA_NOT_SUPPORTED); } /* * Checks whether a share is currently active. */ static boolean_t smb_is_share_active(sa_share_impl_t impl_share) { (void) impl_share; return (B_FALSE); } /* * Called to update a share's options. A share's options might be out of * date if the share was loaded from disk and the "sharesmb" dataset * property has changed in the meantime. This function also takes care * of re-enabling the share if necessary. */ static int smb_update_shareopts(sa_share_impl_t impl_share, const char *shareopts) { (void) impl_share, (void) shareopts; return (SA_OK); } static int smb_update_shares(void) { /* Not implemented */ return (0); } /* * Clears a share's SMB options. Used by libshare to * clean up shares that are about to be free()'d. */ static void smb_clear_shareopts(sa_share_impl_t impl_share) { FSINFO(impl_share, smb_fstype)->shareopts = NULL; } static const sa_share_ops_t smb_shareops = { .enable_share = smb_enable_share, .disable_share = smb_disable_share, .is_shared = smb_is_share_active, .validate_shareopts = smb_validate_shareopts, .update_shareopts = smb_update_shareopts, .clear_shareopts = smb_clear_shareopts, .commit_shares = smb_update_shares, }; /* * Initializes the SMB functionality of libshare. */ void libshare_smb_init(void) { smb_fstype = register_fstype("smb", &smb_shareops); } diff --git a/lib/libshare/os/linux/nfs.c b/lib/libshare/os/linux/nfs.c index 5acfa3fb8545..72653b6d40f5 100644 --- a/lib/libshare/os/linux/nfs.c +++ b/lib/libshare/os/linux/nfs.c @@ -1,525 +1,525 @@ /* * 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 Gunnar Beutner * Copyright (c) 2012 Cyril Plisko. All rights reserved. * Copyright (c) 2019, 2020 by Delphix. All rights reserved. */ #include #include #include #include #include #include #include #include #include #include #include #include #include "libshare_impl.h" #include "nfs.h" #define ZFS_EXPORTS_DIR "/etc/exports.d" #define ZFS_EXPORTS_FILE ZFS_EXPORTS_DIR"/zfs.exports" #define ZFS_EXPORTS_LOCK ZFS_EXPORTS_FILE".lock" static sa_fstype_t *nfs_fstype; typedef int (*nfs_shareopt_callback_t)(const char *opt, const char *value, void *cookie); typedef int (*nfs_host_callback_t)(FILE *tmpfile, const char *sharepath, const char *host, const char *security, const char *access, void *cookie); /* * Invokes the specified callback function for each Solaris share option * listed in the specified string. */ static int foreach_nfs_shareopt(const char *shareopts, nfs_shareopt_callback_t callback, void *cookie) { char *shareopts_dup, *opt, *cur, *value; int was_nul, error; if (shareopts == NULL) return (SA_OK); if (strcmp(shareopts, "on") == 0) shareopts = "rw,crossmnt"; shareopts_dup = strdup(shareopts); if (shareopts_dup == NULL) return (SA_NO_MEMORY); opt = shareopts_dup; was_nul = 0; while (1) { cur = opt; while (*cur != ',' && *cur != '\0') cur++; if (*cur == '\0') was_nul = 1; *cur = '\0'; if (cur > opt) { value = strchr(opt, '='); if (value != NULL) { *value = '\0'; value++; } error = callback(opt, value, cookie); if (error != SA_OK) { free(shareopts_dup); return (error); } } opt = cur + 1; if (was_nul) break; } free(shareopts_dup); return (SA_OK); } typedef struct nfs_host_cookie_s { nfs_host_callback_t callback; const char *sharepath; void *cookie; FILE *tmpfile; const char *security; } nfs_host_cookie_t; /* * Helper function for foreach_nfs_host. This function checks whether the * current share option is a host specification and invokes a callback * function with information about the host. */ static int foreach_nfs_host_cb(const char *opt, const char *value, void *pcookie) { int error; const char *access; char *host_dup, *host, *next, *v6Literal; nfs_host_cookie_t *udata = (nfs_host_cookie_t *)pcookie; int cidr_len; #ifdef DEBUG fprintf(stderr, "foreach_nfs_host_cb: key=%s, value=%s\n", opt, value); #endif if (strcmp(opt, "sec") == 0) udata->security = value; if (strcmp(opt, "rw") == 0 || strcmp(opt, "ro") == 0) { if (value == NULL) value = "*"; access = opt; host_dup = strdup(value); if (host_dup == NULL) return (SA_NO_MEMORY); host = host_dup; do { if (*host == '[') { host++; v6Literal = strchr(host, ']'); if (v6Literal == NULL) { free(host_dup); return (SA_SYNTAX_ERR); } if (v6Literal[1] == '\0') { *v6Literal = '\0'; next = NULL; } else if (v6Literal[1] == '/') { next = strchr(v6Literal + 2, ':'); if (next == NULL) { cidr_len = strlen(v6Literal + 1); memmove(v6Literal, v6Literal + 1, cidr_len); v6Literal[cidr_len] = '\0'; } else { cidr_len = next - v6Literal - 1; memmove(v6Literal, v6Literal + 1, cidr_len); v6Literal[cidr_len] = '\0'; next++; } } else if (v6Literal[1] == ':') { *v6Literal = '\0'; next = v6Literal + 2; } else { free(host_dup); return (SA_SYNTAX_ERR); } } else { next = strchr(host, ':'); if (next != NULL) { *next = '\0'; next++; } } error = udata->callback(udata->tmpfile, udata->sharepath, host, udata->security, access, udata->cookie); if (error != SA_OK) { free(host_dup); return (error); } host = next; } while (host != NULL); free(host_dup); } return (SA_OK); } /* * Invokes a callback function for all NFS hosts that are set for a share. */ static int foreach_nfs_host(sa_share_impl_t impl_share, FILE *tmpfile, nfs_host_callback_t callback, void *cookie) { nfs_host_cookie_t udata; char *shareopts; udata.callback = callback; udata.sharepath = impl_share->sa_mountpoint; udata.cookie = cookie; udata.tmpfile = tmpfile; udata.security = "sys"; shareopts = FSINFO(impl_share, nfs_fstype)->shareopts; return (foreach_nfs_shareopt(shareopts, foreach_nfs_host_cb, &udata)); } /* * Converts a Solaris NFS host specification to its Linux equivalent. */ static const char * get_linux_hostspec(const char *solaris_hostspec) { /* * For now we just support CIDR masks (e.g. @192.168.0.0/16) and host * wildcards (e.g. *.example.org). */ if (solaris_hostspec[0] == '@') { /* * Solaris host specifier, e.g. @192.168.0.0/16; we just need * to skip the @ in this case */ return (solaris_hostspec + 1); } else { return (solaris_hostspec); } } /* * Adds a Linux share option to an array of NFS options. */ static int add_linux_shareopt(char **plinux_opts, const char *key, const char *value) { size_t len = 0; char *new_linux_opts; if (*plinux_opts != NULL) len = strlen(*plinux_opts); new_linux_opts = realloc(*plinux_opts, len + 1 + strlen(key) + (value ? 1 + strlen(value) : 0) + 1); if (new_linux_opts == NULL) return (SA_NO_MEMORY); new_linux_opts[len] = '\0'; if (len > 0) strcat(new_linux_opts, ","); strcat(new_linux_opts, key); if (value != NULL) { strcat(new_linux_opts, "="); strcat(new_linux_opts, value); } *plinux_opts = new_linux_opts; return (SA_OK); } /* * Validates and converts a single Solaris share option to its Linux * equivalent. */ static int get_linux_shareopts_cb(const char *key, const char *value, void *cookie) { char **plinux_opts = (char **)cookie; /* host-specific options, these are taken care of elsewhere */ if (strcmp(key, "ro") == 0 || strcmp(key, "rw") == 0 || strcmp(key, "sec") == 0) return (SA_OK); if (strcmp(key, "anon") == 0) key = "anonuid"; if (strcmp(key, "root_mapping") == 0) { (void) add_linux_shareopt(plinux_opts, "root_squash", NULL); key = "anonuid"; } if (strcmp(key, "nosub") == 0) key = "subtree_check"; if (strcmp(key, "insecure") != 0 && strcmp(key, "secure") != 0 && strcmp(key, "async") != 0 && strcmp(key, "sync") != 0 && strcmp(key, "no_wdelay") != 0 && strcmp(key, "wdelay") != 0 && strcmp(key, "nohide") != 0 && strcmp(key, "hide") != 0 && strcmp(key, "crossmnt") != 0 && strcmp(key, "no_subtree_check") != 0 && strcmp(key, "subtree_check") != 0 && strcmp(key, "insecure_locks") != 0 && strcmp(key, "secure_locks") != 0 && strcmp(key, "no_auth_nlm") != 0 && strcmp(key, "auth_nlm") != 0 && strcmp(key, "no_acl") != 0 && strcmp(key, "mountpoint") != 0 && strcmp(key, "mp") != 0 && strcmp(key, "fsuid") != 0 && strcmp(key, "refer") != 0 && strcmp(key, "replicas") != 0 && strcmp(key, "root_squash") != 0 && strcmp(key, "no_root_squash") != 0 && strcmp(key, "all_squash") != 0 && strcmp(key, "no_all_squash") != 0 && strcmp(key, "fsid") != 0 && strcmp(key, "anonuid") != 0 && strcmp(key, "anongid") != 0) { return (SA_SYNTAX_ERR); } (void) add_linux_shareopt(plinux_opts, key, value); return (SA_OK); } /* * Takes a string containing Solaris share options (e.g. "sync,no_acl") and * converts them to a NULL-terminated array of Linux NFS options. */ static int get_linux_shareopts(const char *shareopts, char **plinux_opts) { int error; assert(plinux_opts != NULL); *plinux_opts = NULL; /* no_subtree_check - Default as of nfs-utils v1.1.0 */ (void) add_linux_shareopt(plinux_opts, "no_subtree_check", NULL); /* mountpoint - Restrict exports to ZFS mountpoints */ (void) add_linux_shareopt(plinux_opts, "mountpoint", NULL); error = foreach_nfs_shareopt(shareopts, get_linux_shareopts_cb, plinux_opts); if (error != SA_OK) { free(*plinux_opts); *plinux_opts = NULL; } return (error); } /* * This function populates an entry into /etc/exports.d/zfs.exports. * This file is consumed by the linux nfs server so that zfs shares are * automatically exported upon boot or whenever the nfs server restarts. */ static int nfs_add_entry(FILE *tmpfile, const char *sharepath, const char *host, const char *security, const char *access_opts, void *pcookie) { const char *linux_opts = (const char *)pcookie; if (linux_opts == NULL) linux_opts = ""; if (fprintf(tmpfile, "%s %s(sec=%s,%s,%s)\n", sharepath, get_linux_hostspec(host), security, access_opts, linux_opts) < 0) { fprintf(stderr, "failed to write to temporary file\n"); return (SA_SYSTEM_ERR); } return (SA_OK); } /* * Enables NFS sharing for the specified share. */ static int nfs_enable_share_impl(sa_share_impl_t impl_share, FILE *tmpfile) { char *shareopts, *linux_opts; int error; shareopts = FSINFO(impl_share, nfs_fstype)->shareopts; error = get_linux_shareopts(shareopts, &linux_opts); if (error != SA_OK) return (error); error = foreach_nfs_host(impl_share, tmpfile, nfs_add_entry, linux_opts); free(linux_opts); return (error); } static int nfs_enable_share(sa_share_impl_t impl_share) { return (nfs_toggle_share( ZFS_EXPORTS_LOCK, ZFS_EXPORTS_FILE, ZFS_EXPORTS_DIR, impl_share, nfs_enable_share_impl)); } /* * Disables NFS sharing for the specified share. */ static int nfs_disable_share_impl(sa_share_impl_t impl_share, FILE *tmpfile) { (void) impl_share, (void) tmpfile; return (SA_OK); } static int nfs_disable_share(sa_share_impl_t impl_share) { return (nfs_toggle_share( ZFS_EXPORTS_LOCK, ZFS_EXPORTS_FILE, ZFS_EXPORTS_DIR, impl_share, nfs_disable_share_impl)); } static boolean_t nfs_is_shared(sa_share_impl_t impl_share) { return (nfs_is_shared_impl(ZFS_EXPORTS_FILE, impl_share)); } /* * Checks whether the specified NFS share options are syntactically correct. */ static int nfs_validate_shareopts(const char *shareopts) { char *linux_opts; int error; error = get_linux_shareopts(shareopts, &linux_opts); if (error != SA_OK) return (error); free(linux_opts); return (SA_OK); } static int nfs_update_shareopts(sa_share_impl_t impl_share, const char *shareopts) { FSINFO(impl_share, nfs_fstype)->shareopts = (char *)shareopts; return (SA_OK); } /* * Clears a share's NFS options. Used by libshare to * clean up shares that are about to be free()'d. */ static void nfs_clear_shareopts(sa_share_impl_t impl_share) { FSINFO(impl_share, nfs_fstype)->shareopts = NULL; } static int nfs_commit_shares(void) { char *argv[] = { - "/usr/sbin/exportfs", - "-ra", + (char *)"/usr/sbin/exportfs", + (char *)"-ra", NULL }; return (libzfs_run_process(argv[0], argv, 0)); } static const sa_share_ops_t nfs_shareops = { .enable_share = nfs_enable_share, .disable_share = nfs_disable_share, .is_shared = nfs_is_shared, .validate_shareopts = nfs_validate_shareopts, .update_shareopts = nfs_update_shareopts, .clear_shareopts = nfs_clear_shareopts, .commit_shares = nfs_commit_shares, }; /* * Initializes the NFS functionality of libshare. */ void libshare_nfs_init(void) { nfs_fstype = register_fstype("nfs", &nfs_shareops); } diff --git a/lib/libshare/os/linux/smb.c b/lib/libshare/os/linux/smb.c index 47d1aa776c4f..f4a0bfd6bf9b 100644 --- a/lib/libshare/os/linux/smb.c +++ b/lib/libshare/os/linux/smb.c @@ -1,442 +1,433 @@ /* * 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 , based on nfs.c * by Gunnar Beutner * Copyright (c) 2019, 2020 by Delphix. All rights reserved. * * 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. * * TESTING * Make sure that samba listens to 'localhost' (127.0.0.1) and that the options * 'usershare max shares' and 'usershare owner only' have been reviewed/set * 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 #include #include #include #include #include #include #include #include #include #include #include #include "libshare_impl.h" #include "smb.h" static boolean_t smb_available(void); static sa_fstype_t *smb_fstype; -smb_share_t *smb_shares; +static smb_share_t *smb_shares; static int smb_disable_share(sa_share_impl_t impl_share); static boolean_t smb_is_share_active(sa_share_impl_t impl_share); /* * 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; char *dup_value = NULL, *path = NULL, *comment = NULL, *name = NULL; 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) return (SA_SYSTEM_ERR); /* Go through the directory, looking for shares */ while ((directory = readdir(shares_dir))) { if (directory->d_name[0] == '.') continue; snprintf(file_path, sizeof (file_path), "%s/%s", SHARE_DIR, directory->d_name); if (stat(file_path, &eStat) == -1) { rc = SA_SYSTEM_ERR; goto out; } if (!S_ISREG(eStat.st_mode)) continue; if ((share_file_fp = fopen(file_path, "re")) == NULL) { rc = SA_SYSTEM_ERR; goto out; } name = strdup(directory->d_name); if (name == NULL) { rc = SA_NO_MEMORY; goto out; } while (fgets(line, sizeof (line), share_file_fp)) { if (line[0] == '#') continue; /* Trim trailing new-line character(s). */ while (line[strlen(line) - 1] == '\r' || line[strlen(line) - 1] == '\n') 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; } if (strcmp(key, "path") == 0) { free(path); path = dup_value; } else if (strcmp(key, "comment") == 0) { free(comment); comment = dup_value; } else if (strcmp(key, "guest_ok") == 0) { free(guest_ok); guest_ok = dup_value; } else free(dup_value); dup_value = NULL; if (path == NULL || comment == NULL || guest_ok == NULL) continue; /* Incomplete share definition */ else { shares = (smb_share_t *) malloc(sizeof (smb_share_t)); if (shares == NULL) { rc = SA_NO_MEMORY; goto out; } (void) strlcpy(shares->name, name, sizeof (shares->name)); (void) strlcpy(shares->path, path, sizeof (shares->path)); (void) strlcpy(shares->comment, comment, sizeof (shares->comment)); shares->guest_ok = atoi(guest_ok); shares->next = new_shares; new_shares = shares; free(path); free(comment); free(guest_ok); path = NULL; comment = NULL; guest_ok = NULL; } } out: if (share_file_fp != NULL) { fclose(share_file_fp); share_file_fp = NULL; } free(name); free(path); free(comment); free(guest_ok); name = NULL; path = NULL; comment = NULL; guest_ok = NULL; } closedir(shares_dir); smb_shares = new_shares; return (rc); } /* * 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 *argv[10], *pos; char name[SMB_NAME_MAX], comment[SMB_COMMENT_MAX]; - int rc; /* Support ZFS share name regexp '[[:alnum:]_-.: ]' */ strlcpy(name, sharename, sizeof (name)); - name [sizeof (name)-1] = '\0'; - - pos = name; - while (*pos != '\0') { - switch (*pos) { + for (char *itr = name; *itr != '\0'; ++itr) + switch (*itr) { case '/': case '-': case ':': case ' ': - *pos = '_'; + *itr = '_'; } - ++pos; - } - /* * CMD: net -S NET_CMD_ARG_HOST usershare add Test1 /share/Test1 \ * "Comment" "Everyone:F" */ snprintf(comment, sizeof (comment), "Comment: %s", sharepath); - argv[0] = NET_CMD_PATH; - argv[1] = (char *)"-S"; - argv[2] = NET_CMD_ARG_HOST; - argv[3] = (char *)"usershare"; - argv[4] = (char *)"add"; - argv[5] = (char *)name; - argv[6] = (char *)sharepath; - argv[7] = (char *)comment; - argv[8] = (char *)"Everyone:F"; - argv[9] = NULL; - - rc = libzfs_run_process(argv[0], argv, 0); - if (rc < 0) + 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) return (SA_SYSTEM_ERR); /* Reload the share file */ (void) smb_retrieve_shares(); return (SA_OK); } /* * Enables SMB sharing for the specified share. */ static int smb_enable_share(sa_share_impl_t impl_share) { char *shareopts; if (!smb_available()) return (SA_SYSTEM_ERR); if (smb_is_share_active(impl_share)) smb_disable_share(impl_share); shareopts = FSINFO(impl_share, smb_fstype)->shareopts; if (shareopts == NULL) /* on/off */ return (SA_SYSTEM_ERR); if (strcmp(shareopts, "off") == 0) return (SA_OK); /* Magic: Enable (i.e., 'create new') share */ return (smb_enable_share_one(impl_share->sa_zfsname, impl_share->sa_mountpoint)); } /* * Used internally by smb_disable_share to disable sharing for a single host. */ static int smb_disable_share_one(const char *sharename) { - int rc; - char *argv[7]; - /* CMD: net -S NET_CMD_ARG_HOST usershare delete Test1 */ - argv[0] = NET_CMD_PATH; - argv[1] = (char *)"-S"; - argv[2] = NET_CMD_ARG_HOST; - argv[3] = (char *)"usershare"; - argv[4] = (char *)"delete"; - argv[5] = (char *)sharename; - argv[6] = NULL; - - rc = libzfs_run_process(argv[0], argv, 0); - if (rc < 0) + 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) return (SA_SYSTEM_ERR); else return (SA_OK); } /* * 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. */ return (SA_OK); } 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)); return (SA_OK); } /* * 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)) return (SA_OK); return (SA_SYNTAX_ERR); } /* * Checks whether a share is currently active. */ static boolean_t smb_is_share_active(sa_share_impl_t impl_share) { if (!smb_available()) return (B_FALSE); /* Retrieve the list of (possible) active shares */ smb_retrieve_shares(); for (const smb_share_t *i = smb_shares; i != NULL; i = i->next) if (strcmp(impl_share->sa_mountpoint, i->path) == 0) return (B_TRUE); return (B_FALSE); } /* * Called to update a share's options. A share's options might be out of * date if the share was loaded from disk and the "sharesmb" dataset * property has changed in the meantime. This function also takes care * of re-enabling the share if necessary. */ static int smb_update_shareopts(sa_share_impl_t impl_share, const char *shareopts) { if (!impl_share) return (SA_SYSTEM_ERR); FSINFO(impl_share, smb_fstype)->shareopts = (char *)shareopts; return (SA_OK); } static int smb_update_shares(void) { /* Not implemented */ return (0); } /* * Clears a share's SMB options. Used by libshare to * clean up shares that are about to be free()'d. */ static void smb_clear_shareopts(sa_share_impl_t impl_share) { FSINFO(impl_share, smb_fstype)->shareopts = NULL; } static const sa_share_ops_t smb_shareops = { .enable_share = smb_enable_share, .disable_share = smb_disable_share, .is_shared = smb_is_share_active, .validate_shareopts = smb_validate_shareopts, .update_shareopts = smb_update_shareopts, .clear_shareopts = smb_clear_shareopts, .commit_shares = smb_update_shares, }; /* * Provides a convenient wrapper for determining SMB availability */ static boolean_t smb_available(void) { struct stat statbuf; if (lstat(SHARE_DIR, &statbuf) != 0 || !S_ISDIR(statbuf.st_mode)) return (B_FALSE); if (access(NET_CMD_PATH, F_OK) != 0) return (B_FALSE); return (B_TRUE); } /* * Initializes the SMB functionality of libshare. */ void libshare_smb_init(void) { smb_fstype = register_fstype("smb", &smb_shareops); } diff --git a/lib/libshare/smb.h b/lib/libshare/smb.h index 8ea44677f9ae..c45e0aec6067 100644 --- a/lib/libshare/smb.h +++ b/lib/libshare/smb.h @@ -1,49 +1,47 @@ /* * 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) 2011 Turbo Fredriksson . */ /* * The maximum SMB share name seems to be 254 characters, though good * references are hard to find. */ #define SMB_NAME_MAX 255 #define SMB_COMMENT_MAX 255 #define SHARE_DIR "/var/lib/samba/usershares" #define NET_CMD_PATH "/usr/bin/net" #define NET_CMD_ARG_HOST "127.0.0.1" typedef struct smb_share_s { char name[SMB_NAME_MAX]; /* Share name */ char path[PATH_MAX]; /* Share path */ char comment[SMB_COMMENT_MAX]; /* Share's comment */ boolean_t guest_ok; /* 'y' or 'n' */ struct smb_share_s *next; } smb_share_t; -extern smb_share_t *smb_shares; - void libshare_smb_init(void); diff --git a/lib/libspl/include/libshare.h b/lib/libspl/include/libshare.h index 5d06b163a3ba..0395c2815464 100644 --- a/lib/libspl/include/libshare.h +++ b/lib/libspl/include/libshare.h @@ -1,86 +1,88 @@ /* * 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 2008 Sun Microsystems, Inc. All rights reserved. * Use is subject to license terms. * Copyright (c) 2019, 2020 by Delphix. All rights reserved. */ #ifndef _LIBSPL_LIBSHARE_H #define _LIBSPL_LIBSHARE_H extern __attribute__((visibility("default"))) +#include + /* API Initialization */ #define SA_INIT_SHARE_API 0x0001 /* init share specific interface */ #define SA_INIT_CONTROL_API 0x0002 /* init control specific interface */ /* * defined error values */ #define SA_OK 0 #define SA_NO_SUCH_PATH 1 /* provided path doesn't exist */ #define SA_NO_MEMORY 2 /* no memory for data structures */ #define SA_DUPLICATE_NAME 3 /* object name is already in use */ #define SA_BAD_PATH 4 /* not a full path */ #define SA_NO_SUCH_GROUP 5 /* group is not defined */ #define SA_CONFIG_ERR 6 /* system configuration error */ #define SA_SYSTEM_ERR 7 /* system error, use errno */ #define SA_SYNTAX_ERR 8 /* syntax error on command line */ #define SA_NO_PERMISSION 9 /* no permission for operation */ #define SA_BUSY 10 /* resource is busy */ #define SA_NO_SUCH_PROP 11 /* property doesn't exist */ #define SA_INVALID_NAME 12 /* name of object is invalid */ #define SA_INVALID_PROTOCOL 13 /* specified protocol not valid */ #define SA_NOT_ALLOWED 14 /* operation not allowed */ #define SA_BAD_VALUE 15 /* bad value for property */ #define SA_INVALID_SECURITY 16 /* invalid security type */ #define SA_NO_SUCH_SECURITY 17 /* security set not found */ #define SA_VALUE_CONFLICT 18 /* property value conflict */ #define SA_NOT_IMPLEMENTED 19 /* plugin interface not implemented */ #define SA_INVALID_PATH 20 /* path is sub-dir of existing share */ #define SA_NOT_SUPPORTED 21 /* operation not supported for proto */ #define SA_PROP_SHARE_ONLY 22 /* property valid on share only */ #define SA_NOT_SHARED 23 /* path is not shared */ #define SA_NO_SUCH_RESOURCE 24 /* resource not found */ #define SA_RESOURCE_REQUIRED 25 /* resource name is required */ #define SA_MULTIPLE_ERROR 26 /* multiple protocols reported error */ #define SA_PATH_IS_SUBDIR 27 /* check_path found path is subdir */ #define SA_PATH_IS_PARENTDIR 28 /* check_path found path is parent */ #define SA_NO_SECTION 29 /* protocol requires section info */ #define SA_NO_SUCH_SECTION 30 /* no section found */ #define SA_NO_PROPERTIES 31 /* no properties found */ #define SA_PASSWORD_ENC 32 /* passwords must be encrypted */ #define SA_SHARE_EXISTS 33 /* path or file is already shared */ /* initialization */ _LIBSPL_LIBSHARE_H char *sa_errorstr(int); /* share control */ _LIBSPL_LIBSHARE_H int sa_enable_share(const char *, const char *, const char *, char *); _LIBSPL_LIBSHARE_H int sa_disable_share(const char *, char *); _LIBSPL_LIBSHARE_H boolean_t sa_is_shared(const char *, char *); _LIBSPL_LIBSHARE_H void sa_commit_shares(const char *); /* protocol specific interfaces */ _LIBSPL_LIBSHARE_H int sa_validate_shareopts(char *, char *); #endif /* _LIBSPL_LIBSHARE_H */