Path: blob/main/sys/contrib/openzfs/lib/libshare/libshare.c
48378 views
// SPDX-License-Identifier: CDDL-1.01/*2* CDDL HEADER START3*4* The contents of this file are subject to the terms of the5* Common Development and Distribution License (the "License").6* You may not use this file except in compliance with the License.7*8* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE9* or https://opensource.org/licenses/CDDL-1.0.10* See the License for the specific language governing permissions11* and limitations under the License.12*13* When distributing Covered Code, include this CDDL HEADER in each14* file and include the License file at usr/src/OPENSOLARIS.LICENSE.15* If applicable, add the following below this CDDL HEADER, with the16* fields enclosed by brackets "[]" replaced with your own identifying17* information: Portions Copyright [yyyy] [name of copyright owner]18*19* CDDL HEADER END20*/2122/*23* Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.24* Copyright (c) 2011 Gunnar Beutner25* Copyright (c) 2018, 2022 by Delphix. All rights reserved.26*/2728#include <stdio.h>29#include <stdlib.h>30#include <stddef.h>31#include <string.h>32#include <errno.h>33#include <libintl.h>34#include <sys/file.h>35#include <sys/types.h>36#include <sys/stat.h>37#include <unistd.h>38#include <libzfs.h>39#include <libshare.h>40#include "libshare_impl.h"4142#define init_share(zfsname, path, shareopts) \43{ \44.sa_zfsname = zfsname, \45.sa_mountpoint = path, \46.sa_shareopts = shareopts, \47}4849#define VALIDATE_PROTOCOL(proto, ...) \50if ((proto) < 0 || (proto) >= SA_PROTOCOL_COUNT) \51return __VA_ARGS__5253const char *const sa_protocol_names[SA_PROTOCOL_COUNT] = {54[SA_PROTOCOL_NFS] = "nfs",55[SA_PROTOCOL_SMB] = "smb",56};5758static const sa_fstype_t *fstypes[SA_PROTOCOL_COUNT] =59{&libshare_nfs_type, &libshare_smb_type};6061int62sa_enable_share(const char *zfsname, const char *mountpoint,63const char *shareopts, enum sa_protocol protocol)64{65VALIDATE_PROTOCOL(protocol, SA_INVALID_PROTOCOL);6667const struct sa_share_impl args =68init_share(zfsname, mountpoint, shareopts);69return (fstypes[protocol]->enable_share(&args));70}7172int73sa_disable_share(const char *mountpoint, enum sa_protocol protocol)74{75VALIDATE_PROTOCOL(protocol, SA_INVALID_PROTOCOL);7677const struct sa_share_impl args = init_share(NULL, mountpoint, NULL);78return (fstypes[protocol]->disable_share(&args));79}8081boolean_t82sa_is_shared(const char *mountpoint, enum sa_protocol protocol)83{84VALIDATE_PROTOCOL(protocol, B_FALSE);8586const struct sa_share_impl args = init_share(NULL, mountpoint, NULL);87return (fstypes[protocol]->is_shared(&args));88}8990void91sa_commit_shares(enum sa_protocol protocol)92{93/* CSTYLED */94VALIDATE_PROTOCOL(protocol, );9596fstypes[protocol]->commit_shares();97}9899void100sa_truncate_shares(enum sa_protocol protocol)101{102/* CSTYLED */103VALIDATE_PROTOCOL(protocol, );104105if (fstypes[protocol]->truncate_shares != NULL)106fstypes[protocol]->truncate_shares();107}108109int110sa_validate_shareopts(const char *options, enum sa_protocol protocol)111{112VALIDATE_PROTOCOL(protocol, SA_INVALID_PROTOCOL);113114return (fstypes[protocol]->validate_shareopts(options));115}116117/*118* sa_errorstr(err)119*120* convert an error value to an error string121*/122const char *123sa_errorstr(int err)124{125static char errstr[32];126127switch (err) {128case SA_OK:129return (dgettext(TEXT_DOMAIN, "ok"));130case SA_NO_SUCH_PATH:131return (dgettext(TEXT_DOMAIN, "path doesn't exist"));132case SA_NO_MEMORY:133return (dgettext(TEXT_DOMAIN, "no memory"));134case SA_DUPLICATE_NAME:135return (dgettext(TEXT_DOMAIN, "name in use"));136case SA_BAD_PATH:137return (dgettext(TEXT_DOMAIN, "bad path"));138case SA_NO_SUCH_GROUP:139return (dgettext(TEXT_DOMAIN, "no such group"));140case SA_CONFIG_ERR:141return (dgettext(TEXT_DOMAIN, "configuration error"));142case SA_SYSTEM_ERR:143return (dgettext(TEXT_DOMAIN, "system error"));144case SA_SYNTAX_ERR:145return (dgettext(TEXT_DOMAIN, "syntax error"));146case SA_NO_PERMISSION:147return (dgettext(TEXT_DOMAIN, "no permission"));148case SA_BUSY:149return (dgettext(TEXT_DOMAIN, "busy"));150case SA_NO_SUCH_PROP:151return (dgettext(TEXT_DOMAIN, "no such property"));152case SA_INVALID_NAME:153return (dgettext(TEXT_DOMAIN, "invalid name"));154case SA_INVALID_PROTOCOL:155return (dgettext(TEXT_DOMAIN, "invalid protocol"));156case SA_NOT_ALLOWED:157return (dgettext(TEXT_DOMAIN, "operation not allowed"));158case SA_BAD_VALUE:159return (dgettext(TEXT_DOMAIN, "bad property value"));160case SA_INVALID_SECURITY:161return (dgettext(TEXT_DOMAIN, "invalid security type"));162case SA_NO_SUCH_SECURITY:163return (dgettext(TEXT_DOMAIN, "security type not found"));164case SA_VALUE_CONFLICT:165return (dgettext(TEXT_DOMAIN, "property value conflict"));166case SA_NOT_IMPLEMENTED:167return (dgettext(TEXT_DOMAIN, "not implemented"));168case SA_INVALID_PATH:169return (dgettext(TEXT_DOMAIN, "invalid path"));170case SA_NOT_SUPPORTED:171return (dgettext(TEXT_DOMAIN, "operation not supported"));172case SA_PROP_SHARE_ONLY:173return (dgettext(TEXT_DOMAIN, "property not valid for group"));174case SA_NOT_SHARED:175return (dgettext(TEXT_DOMAIN, "not shared"));176case SA_NO_SUCH_RESOURCE:177return (dgettext(TEXT_DOMAIN, "no such resource"));178case SA_RESOURCE_REQUIRED:179return (dgettext(TEXT_DOMAIN, "resource name required"));180case SA_MULTIPLE_ERROR:181return (dgettext(TEXT_DOMAIN,182"errors from multiple protocols"));183case SA_PATH_IS_SUBDIR:184return (dgettext(TEXT_DOMAIN, "path is a subpath of share"));185case SA_PATH_IS_PARENTDIR:186return (dgettext(TEXT_DOMAIN, "path is parent of a share"));187case SA_NO_SECTION:188return (dgettext(TEXT_DOMAIN, "protocol requires a section"));189case SA_NO_PROPERTIES:190return (dgettext(TEXT_DOMAIN, "properties not found"));191case SA_NO_SUCH_SECTION:192return (dgettext(TEXT_DOMAIN, "section not found"));193case SA_PASSWORD_ENC:194return (dgettext(TEXT_DOMAIN, "passwords must be encrypted"));195case SA_SHARE_EXISTS:196return (dgettext(TEXT_DOMAIN,197"path or file is already shared"));198default:199(void) snprintf(errstr, sizeof (errstr),200dgettext(TEXT_DOMAIN, "unknown %d"), err);201return (errstr);202}203}204205206