diff --git a/lib/libshare/libshare.c b/lib/libshare/libshare.c index f4684cc69107..4a8e32d773c6 100644 --- a/lib/libshare/libshare.c +++ b/lib/libshare/libshare.c @@ -1,365 +1,217 @@ /* * 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) 2018, 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" -#include "smb.h" -static sa_share_impl_t alloc_share(const char *zfsname, const char *path); -static void free_share(sa_share_impl_t share); - -static int fstypes_count; -static sa_fstype_t *fstypes; - -sa_fstype_t * -register_fstype(const char *name, const sa_share_ops_t *ops) +#define init_share(zfsname, path, shareopts) \ + { \ + .sa_zfsname = zfsname, \ + .sa_mountpoint = path, \ + .sa_shareopts = shareopts, \ + } +#define find_proto(pcol) \ + /* CSTYLED */ \ + ({ \ + sa_fstype_t prot = { \ + .protocol = pcol, \ + }; \ + avl_find(&fstypes, &prot, NULL); \ + }) + +static avl_tree_t fstypes; + +static int +fstypes_compar(const void *lhs, const void *rhs) { - sa_fstype_t *fstype; - - fstype = calloc(1, sizeof (sa_fstype_t)); - - if (fstype == NULL) - return (NULL); - - fstype->name = name; - fstype->ops = ops; - fstype->fsinfo_index = fstypes_count; - - fstypes_count++; - - fstype->next = fstypes; - fstypes = fstype; - - return (fstype); + const sa_fstype_t *l = lhs, *r = rhs; + int cmp = strcmp(l->protocol, r->protocol); + return ((0 < cmp) - (cmp < 0)); } __attribute__((constructor)) static void libshare_init(void) { - libshare_nfs_init(); - libshare_smb_init(); + avl_create(&fstypes, fstypes_compar, + sizeof (sa_fstype_t), offsetof(sa_fstype_t, node)); + avl_add(&fstypes, &libshare_nfs_type); + avl_add(&fstypes, &libshare_smb_type); } int sa_enable_share(const char *zfsname, const char *mountpoint, const char *shareopts, const char *protocol) { - int rc, ret = SA_OK; - boolean_t found_protocol = B_FALSE; - sa_fstype_t *fstype; - - sa_share_impl_t impl_share = alloc_share(zfsname, mountpoint); - if (impl_share == NULL) - return (SA_NO_MEMORY); - - fstype = fstypes; - while (fstype != NULL) { - if (strcmp(fstype->name, protocol) == 0) { - - rc = fstype->ops->update_shareopts(impl_share, - shareopts); - if (rc != SA_OK) - break; + sa_fstype_t *fstype = find_proto(protocol); + if (!fstype) + return (SA_INVALID_PROTOCOL); - rc = fstype->ops->enable_share(impl_share); - if (rc != SA_OK) - ret = rc; - - found_protocol = B_TRUE; - } - - fstype = fstype->next; - } - free_share(impl_share); - - return (found_protocol ? ret : SA_INVALID_PROTOCOL); + const struct sa_share_impl args = + init_share(zfsname, mountpoint, shareopts); + return (fstype->enable_share(&args)); } int sa_disable_share(const char *mountpoint, const char *protocol) { - int rc, ret = SA_OK; - boolean_t found_protocol = B_FALSE; - sa_fstype_t *fstype; - - sa_share_impl_t impl_share = alloc_share(NULL, mountpoint); - if (impl_share == NULL) - return (SA_NO_MEMORY); - - fstype = fstypes; - while (fstype != NULL) { - if (strcmp(fstype->name, protocol) == 0) { + sa_fstype_t *fstype = find_proto(protocol); + if (!fstype) + return (SA_INVALID_PROTOCOL); - rc = fstype->ops->disable_share(impl_share); - if (rc != SA_OK) - ret = rc; - - found_protocol = B_TRUE; - } - - fstype = fstype->next; - } - free_share(impl_share); - - return (found_protocol ? ret : SA_INVALID_PROTOCOL); + const struct sa_share_impl args = init_share(NULL, mountpoint, NULL); + return (fstype->disable_share(&args)); } boolean_t sa_is_shared(const char *mountpoint, const char *protocol) { - sa_fstype_t *fstype; - boolean_t ret = B_FALSE; - - /* guid value is not used */ - sa_share_impl_t impl_share = alloc_share(NULL, mountpoint); - if (impl_share == NULL) + sa_fstype_t *fstype = find_proto(protocol); + if (!fstype) return (B_FALSE); - fstype = fstypes; - while (fstype != NULL) { - if (strcmp(fstype->name, protocol) == 0) { - ret = fstype->ops->is_shared(impl_share); - } - fstype = fstype->next; - } - free_share(impl_share); - return (ret); + const struct sa_share_impl args = init_share(NULL, mountpoint, NULL); + return (fstype->is_shared(&args)); } void sa_commit_shares(const char *protocol) { - sa_fstype_t *fstype = fstypes; - while (fstype != NULL) { - if (strcmp(fstype->name, protocol) == 0) - fstype->ops->commit_shares(); - fstype = fstype->next; - } + sa_fstype_t *fstype = find_proto(protocol); + if (!fstype) + return; + + fstype->commit_shares(); } /* * sa_errorstr(err) * * convert an error value to an error string */ const char * sa_errorstr(int err) { static char errstr[32]; - char *ret = NULL; switch (err) { case SA_OK: - ret = dgettext(TEXT_DOMAIN, "ok"); - break; + return (dgettext(TEXT_DOMAIN, "ok")); case SA_NO_SUCH_PATH: - ret = dgettext(TEXT_DOMAIN, "path doesn't exist"); - break; + return (dgettext(TEXT_DOMAIN, "path doesn't exist")); case SA_NO_MEMORY: - ret = dgettext(TEXT_DOMAIN, "no memory"); - break; + return (dgettext(TEXT_DOMAIN, "no memory")); case SA_DUPLICATE_NAME: - ret = dgettext(TEXT_DOMAIN, "name in use"); - break; + return (dgettext(TEXT_DOMAIN, "name in use")); case SA_BAD_PATH: - ret = dgettext(TEXT_DOMAIN, "bad path"); - break; + return (dgettext(TEXT_DOMAIN, "bad path")); case SA_NO_SUCH_GROUP: - ret = dgettext(TEXT_DOMAIN, "no such group"); - break; + return (dgettext(TEXT_DOMAIN, "no such group")); case SA_CONFIG_ERR: - ret = dgettext(TEXT_DOMAIN, "configuration error"); - break; + return (dgettext(TEXT_DOMAIN, "configuration error")); case SA_SYSTEM_ERR: - ret = dgettext(TEXT_DOMAIN, "system error"); - break; + return (dgettext(TEXT_DOMAIN, "system error")); case SA_SYNTAX_ERR: - ret = dgettext(TEXT_DOMAIN, "syntax error"); - break; + return (dgettext(TEXT_DOMAIN, "syntax error")); case SA_NO_PERMISSION: - ret = dgettext(TEXT_DOMAIN, "no permission"); - break; + return (dgettext(TEXT_DOMAIN, "no permission")); case SA_BUSY: - ret = dgettext(TEXT_DOMAIN, "busy"); - break; + return (dgettext(TEXT_DOMAIN, "busy")); case SA_NO_SUCH_PROP: - ret = dgettext(TEXT_DOMAIN, "no such property"); - break; + return (dgettext(TEXT_DOMAIN, "no such property")); case SA_INVALID_NAME: - ret = dgettext(TEXT_DOMAIN, "invalid name"); - break; + return (dgettext(TEXT_DOMAIN, "invalid name")); case SA_INVALID_PROTOCOL: - ret = dgettext(TEXT_DOMAIN, "invalid protocol"); - break; + return (dgettext(TEXT_DOMAIN, "invalid protocol")); case SA_NOT_ALLOWED: - ret = dgettext(TEXT_DOMAIN, "operation not allowed"); - break; + return (dgettext(TEXT_DOMAIN, "operation not allowed")); case SA_BAD_VALUE: - ret = dgettext(TEXT_DOMAIN, "bad property value"); - break; + return (dgettext(TEXT_DOMAIN, "bad property value")); case SA_INVALID_SECURITY: - ret = dgettext(TEXT_DOMAIN, "invalid security type"); - break; + return (dgettext(TEXT_DOMAIN, "invalid security type")); case SA_NO_SUCH_SECURITY: - ret = dgettext(TEXT_DOMAIN, "security type not found"); - break; + return (dgettext(TEXT_DOMAIN, "security type not found")); case SA_VALUE_CONFLICT: - ret = dgettext(TEXT_DOMAIN, "property value conflict"); - break; + return (dgettext(TEXT_DOMAIN, "property value conflict")); case SA_NOT_IMPLEMENTED: - ret = dgettext(TEXT_DOMAIN, "not implemented"); - break; + return (dgettext(TEXT_DOMAIN, "not implemented")); case SA_INVALID_PATH: - ret = dgettext(TEXT_DOMAIN, "invalid path"); - break; + return (dgettext(TEXT_DOMAIN, "invalid path")); case SA_NOT_SUPPORTED: - ret = dgettext(TEXT_DOMAIN, "operation not supported"); - break; + return (dgettext(TEXT_DOMAIN, "operation not supported")); case SA_PROP_SHARE_ONLY: - ret = dgettext(TEXT_DOMAIN, "property not valid for group"); - break; + return (dgettext(TEXT_DOMAIN, "property not valid for group")); case SA_NOT_SHARED: - ret = dgettext(TEXT_DOMAIN, "not shared"); - break; + return (dgettext(TEXT_DOMAIN, "not shared")); case SA_NO_SUCH_RESOURCE: - ret = dgettext(TEXT_DOMAIN, "no such resource"); - break; + return (dgettext(TEXT_DOMAIN, "no such resource")); case SA_RESOURCE_REQUIRED: - ret = dgettext(TEXT_DOMAIN, "resource name required"); - break; + return (dgettext(TEXT_DOMAIN, "resource name required")); case SA_MULTIPLE_ERROR: - ret = dgettext(TEXT_DOMAIN, "errors from multiple protocols"); - break; + return (dgettext(TEXT_DOMAIN, + "errors from multiple protocols")); case SA_PATH_IS_SUBDIR: - ret = dgettext(TEXT_DOMAIN, "path is a subpath of share"); - break; + return (dgettext(TEXT_DOMAIN, "path is a subpath of share")); case SA_PATH_IS_PARENTDIR: - ret = dgettext(TEXT_DOMAIN, "path is parent of a share"); - break; + return (dgettext(TEXT_DOMAIN, "path is parent of a share")); case SA_NO_SECTION: - ret = dgettext(TEXT_DOMAIN, "protocol requires a section"); - break; + return (dgettext(TEXT_DOMAIN, "protocol requires a section")); case SA_NO_PROPERTIES: - ret = dgettext(TEXT_DOMAIN, "properties not found"); - break; + return (dgettext(TEXT_DOMAIN, "properties not found")); case SA_NO_SUCH_SECTION: - ret = dgettext(TEXT_DOMAIN, "section not found"); - break; + return (dgettext(TEXT_DOMAIN, "section not found")); case SA_PASSWORD_ENC: - ret = dgettext(TEXT_DOMAIN, "passwords must be encrypted"); - break; + return (dgettext(TEXT_DOMAIN, "passwords must be encrypted")); case SA_SHARE_EXISTS: - ret = dgettext(TEXT_DOMAIN, "path or file is already shared"); - break; + return (dgettext(TEXT_DOMAIN, + "path or file is already shared")); default: (void) snprintf(errstr, sizeof (errstr), dgettext(TEXT_DOMAIN, "unknown %d"), err); - ret = errstr; + return (errstr); } - return (ret); } int -sa_validate_shareopts(const char *options, const char *proto) -{ - sa_fstype_t *fstype; - - fstype = fstypes; - while (fstype != NULL) { - if (strcmp(fstype->name, proto) != 0) { - fstype = fstype->next; - continue; - } - - return (fstype->ops->validate_shareopts(options)); - } - - return (SA_INVALID_PROTOCOL); -} - -static sa_share_impl_t -alloc_share(const char *zfsname, const char *mountpoint) +sa_validate_shareopts(const char *options, const char *protocol) { - sa_share_impl_t impl_share; - - impl_share = calloc(1, sizeof (struct sa_share_impl)); - - if (impl_share == NULL) - return (NULL); - - if (mountpoint != NULL && - ((impl_share->sa_mountpoint = strdup(mountpoint)) == NULL)) { - free(impl_share); - return (NULL); - } - - if (zfsname != NULL && - ((impl_share->sa_zfsname = strdup(zfsname)) == NULL)) { - free(impl_share->sa_mountpoint); - free(impl_share); - return (NULL); - } - - impl_share->sa_fsinfo = calloc(fstypes_count, - sizeof (sa_share_fsinfo_t)); - if (impl_share->sa_fsinfo == NULL) { - free(impl_share->sa_mountpoint); - free(impl_share->sa_zfsname); - free(impl_share); - return (NULL); - } - - return (impl_share); -} - -static void -free_share(sa_share_impl_t impl_share) -{ - sa_fstype_t *fstype; - - fstype = fstypes; - while (fstype != NULL) { - fstype->ops->clear_shareopts(impl_share); - fstype = fstype->next; - } + sa_fstype_t *fstype = find_proto(protocol); + if (!fstype) + return (SA_INVALID_PROTOCOL); - free(impl_share->sa_mountpoint); - free(impl_share->sa_zfsname); - free(impl_share->sa_fsinfo); - free(impl_share); + return (fstype->validate_shareopts(options)); } diff --git a/lib/libshare/libshare_impl.h b/lib/libshare/libshare_impl.h index 63a6907539e0..c79b4f532a4f 100644 --- a/lib/libshare/libshare_impl.h +++ b/lib/libshare/libshare_impl.h @@ -1,65 +1,52 @@ /* * 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) 2019, 2020 by Delphix. All rights reserved. */ #ifndef _LIBSPL_LIBSHARE_IMPL_H #define _LIBSPL_LIBSHARE_IMPL_H -typedef struct sa_share_fsinfo { - char *shareopts; -} sa_share_fsinfo_t; +#include -typedef struct sa_share_impl { - char *sa_mountpoint; - char *sa_zfsname; - - sa_share_fsinfo_t *sa_fsinfo; /* per-fstype information */ +typedef const struct sa_share_impl { + const char *sa_zfsname; + const char *sa_mountpoint; + const char *sa_shareopts; } *sa_share_impl_t; -#define FSINFO(impl_share, fstype) \ - (&(impl_share->sa_fsinfo[fstype->fsinfo_index])) - -typedef struct sa_share_ops { - int (*enable_share)(sa_share_impl_t share); - int (*disable_share)(sa_share_impl_t share); - boolean_t (*is_shared)(sa_share_impl_t share); - int (*validate_shareopts)(const char *shareopts); - int (*update_shareopts)(sa_share_impl_t impl_share, - const char *shareopts); - void (*clear_shareopts)(sa_share_impl_t impl_share); - int (*commit_shares)(void); -} sa_share_ops_t; +typedef struct { + const char *protocol; -typedef struct sa_fstype { - struct sa_fstype *next; + int (*const enable_share)(sa_share_impl_t share); + int (*const disable_share)(sa_share_impl_t share); + boolean_t (*const is_shared)(sa_share_impl_t share); + int (*const validate_shareopts)(const char *shareopts); + int (*const commit_shares)(void); - const char *name; - const sa_share_ops_t *ops; - int fsinfo_index; + avl_node_t node; } sa_fstype_t; -sa_fstype_t *register_fstype(const char *name, const sa_share_ops_t *ops); +extern sa_fstype_t libshare_nfs_type, libshare_smb_type; #endif /* _LIBSPL_LIBSHARE_IMPL_H */ diff --git a/lib/libshare/nfs.h b/lib/libshare/nfs.h index cfac274c3d26..777581625924 100644 --- a/lib/libshare/nfs.h +++ b/lib/libshare/nfs.h @@ -1,36 +1,34 @@ /* * 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 */ #include "libshare_impl.h" #define FILE_HEADER "# !!! DO NOT EDIT THIS FILE MANUALLY !!!\n\n" -void libshare_nfs_init(void); - boolean_t nfs_is_shared_impl(const char *exports, sa_share_impl_t impl_share); int nfs_toggle_share(const char *lockfile, const char *exports, const char *expdir, sa_share_impl_t impl_share, int(*cbk)(sa_share_impl_t impl_share, FILE *tmpfile)); diff --git a/lib/libshare/os/freebsd/nfs.c b/lib/libshare/os/freebsd/nfs.c index d0cd5f2b3fd9..388bafc2836b 100644 --- a/lib/libshare/os/freebsd/nfs.c +++ b/lib/libshare/os/freebsd/nfs.c @@ -1,222 +1,198 @@ /* * Copyright (c) 2007 Pawel Jakub Dawidek * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * Copyright (c) 2020 by Delphix. All rights reserved. */ #include __FBSDID("$FreeBSD$"); #include #include #include #include #include #include #include #include #include #include #include #include #include "libshare_impl.h" #include "nfs.h" #define _PATH_MOUNTDPID "/var/run/mountd.pid" #define OPTSSIZE 1024 #define MAXLINESIZE (PATH_MAX + OPTSSIZE) #define ZFS_EXPORTS_FILE "/etc/zfs/exports" #define ZFS_EXPORTS_LOCK ZFS_EXPORTS_FILE".lock" -static sa_fstype_t *nfs_fstype; - /* * This function translates options to a format acceptable by exports(5), eg. * * -ro -network=192.168.0.0 -mask=255.255.255.0 -maproot=0 \ * zfs.freebsd.org 69.147.83.54 * * Accepted input formats: * * ro,network=192.168.0.0,mask=255.255.255.0,maproot=0,zfs.freebsd.org * ro network=192.168.0.0 mask=255.255.255.0 maproot=0 zfs.freebsd.org * -ro,-network=192.168.0.0,-mask=255.255.255.0,-maproot=0,zfs.freebsd.org * -ro -network=192.168.0.0 -mask=255.255.255.0 -maproot=0 \ * zfs.freebsd.org * * Recognized keywords: * * ro, maproot, mapall, mask, network, sec, alldirs, public, webnfs, * index, quiet */ static int translate_opts(const char *shareopts, FILE *out) { static const char *const known_opts[] = { "ro", "maproot", "mapall", "mask", "network", "sec", "alldirs", "public", "webnfs", "index", "quiet" }; char oldopts[OPTSSIZE], newopts[OPTSSIZE]; char *o, *s = NULL; unsigned int i; size_t len; strlcpy(oldopts, shareopts, sizeof (oldopts)); newopts[0] = '\0'; s = oldopts; while ((o = strsep(&s, "-, ")) != NULL) { if (o[0] == '\0') continue; for (i = 0; i < ARRAY_SIZE(known_opts); ++i) { len = strlen(known_opts[i]); if (strncmp(known_opts[i], o, len) == 0 && (o[len] == '\0' || o[len] == '=')) { strlcat(newopts, "-", sizeof (newopts)); break; } } strlcat(newopts, o, sizeof (newopts)); strlcat(newopts, " ", sizeof (newopts)); } return (fputs(newopts, out)); } static int nfs_enable_share_impl(sa_share_impl_t impl_share, FILE *tmpfile) { - char *shareopts = FSINFO(impl_share, nfs_fstype)->shareopts; + const char *shareopts = impl_share->sa_shareopts; if (strcmp(shareopts, "on") == 0) shareopts = ""; if (fputs(impl_share->sa_mountpoint, tmpfile) == EOF || fputc('\t', tmpfile) == EOF || translate_opts(shareopts, tmpfile) == EOF || fputc('\n', tmpfile) == EOF) { fprintf(stderr, "failed to write to temporary file\n"); return (SA_SYSTEM_ERR); } return (SA_OK); } static int nfs_enable_share(sa_share_impl_t impl_share) { return (nfs_toggle_share( ZFS_EXPORTS_LOCK, ZFS_EXPORTS_FILE, NULL, impl_share, nfs_enable_share_impl)); } 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, NULL, 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)); } static int nfs_validate_shareopts(const char *shareopts) { (void) shareopts; 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); -} - -static void -nfs_clear_shareopts(sa_share_impl_t impl_share) -{ - FSINFO(impl_share, nfs_fstype)->shareopts = NULL; -} - /* * Commit the shares by restarting mountd. */ static int nfs_commit_shares(void) { struct pidfh *pfh; pid_t mountdpid; start: pfh = pidfile_open(_PATH_MOUNTDPID, 0600, &mountdpid); if (pfh != NULL) { /* mountd(8) is not running. */ pidfile_remove(pfh); return (SA_OK); } if (errno != EEXIST) { /* Cannot open pidfile for some reason. */ return (SA_SYSTEM_ERR); } if (mountdpid == -1) { /* mountd(8) exists, but didn't write the PID yet */ usleep(500); goto start; } /* We have mountd(8) PID in mountdpid variable. */ kill(mountdpid, SIGHUP); return (SA_OK); } -static const sa_share_ops_t nfs_shareops = { +sa_fstype_t libshare_nfs_type = { + .protocol = "nfs", + .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/freebsd/smb.c b/lib/libshare/os/freebsd/smb.c index f14e631b2332..fd61d473d5c6 100644 --- a/lib/libshare/os/freebsd/smb.c +++ b/lib/libshare/os/freebsd/smb.c @@ -1,121 +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 (c) 2020 by Delphix. All rights reserved. */ #include #include #include "libshare_impl.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; 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; 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; 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 = { +sa_fstype_t libshare_smb_type = { + .protocol = "smb", + .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 72653b6d40f5..f8a34924c1eb 100644 --- a/lib/libshare/os/linux/nfs.c +++ b/lib/libshare/os/linux/nfs.c @@ -1,525 +1,493 @@ /* * 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)); + return (foreach_nfs_shareopt(impl_share->sa_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; + char *linux_opts; int error; - shareopts = FSINFO(impl_share, nfs_fstype)->shareopts; - error = get_linux_shareopts(shareopts, &linux_opts); + error = get_linux_shareopts(impl_share->sa_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[] = { (char *)"/usr/sbin/exportfs", (char *)"-ra", NULL }; return (libzfs_run_process(argv[0], argv, 0)); } -static const sa_share_ops_t nfs_shareops = { +sa_fstype_t libshare_nfs_type = { + .protocol = "nfs", + .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 f4a0bfd6bf9b..91dffc9bb8a7 100644 --- a/lib/libshare/os/linux/smb.c +++ b/lib/libshare/os/linux/smb.c @@ -1,433 +1,393 @@ /* * 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; - 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 name[SMB_NAME_MAX], comment[SMB_COMMENT_MAX]; /* Support ZFS share name regexp '[[:alnum:]_-.: ]' */ strlcpy(name, sharename, sizeof (name)); for (char *itr = name; *itr != '\0'; ++itr) switch (*itr) { case '/': case '-': case ':': case ' ': *itr = '_'; } /* * CMD: net -S NET_CMD_ARG_HOST usershare add Test1 /share/Test1 \ * "Comment" "Everyone:F" */ snprintf(comment, sizeof (comment), "Comment: %s", sharepath); 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 */ + if (impl_share->sa_shareopts == NULL) /* on/off */ return (SA_SYSTEM_ERR); - if (strcmp(shareopts, "off") == 0) + if (strcmp(impl_share->sa_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) { /* CMD: net -S NET_CMD_ARG_HOST usershare delete Test1 */ 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; -} +sa_fstype_t libshare_smb_type = { + .protocol = "smb", -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 c45e0aec6067..ca3577751923 100644 --- a/lib/libshare/smb.h +++ b/lib/libshare/smb.h @@ -1,47 +1,45 @@ /* * 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; - -void libshare_smb_init(void);