1 // SPDX-License-Identifier: CDDL-1.0
2 /*
3 * CDDL HEADER START
4 *
5 * The contents of this file are subject to the terms of the
6 * Common Development and Distribution License (the "License").
7 * You may not use this file except in compliance with the License.
8 *
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or https://opensource.org/licenses/CDDL-1.0.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
13 *
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
19 *
20 * CDDL HEADER END
21 */
22
23 /*
24 * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
25 * Copyright (c) 2011 Gunnar Beutner
26 * Copyright (c) 2018, 2022 by Delphix. All rights reserved.
27 */
28
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <stddef.h>
32 #include <string.h>
33 #include <errno.h>
34 #include <libintl.h>
35 #include <sys/file.h>
36 #include <sys/types.h>
37 #include <sys/stat.h>
38 #include <unistd.h>
39 #include <libzfs.h>
40 #include <libshare.h>
41 #include "libshare_impl.h"
42
43 #define init_share(zfsname, path, shareopts) \
44 { \
45 .sa_zfsname = zfsname, \
46 .sa_mountpoint = path, \
47 .sa_shareopts = shareopts, \
48 }
49
50 #define VALIDATE_PROTOCOL(proto, ...) \
51 if ((proto) < 0 || (proto) >= SA_PROTOCOL_COUNT) \
52 return __VA_ARGS__
53
54 const char *const sa_protocol_names[SA_PROTOCOL_COUNT] = {
55 [SA_PROTOCOL_NFS] = "nfs",
56 [SA_PROTOCOL_SMB] = "smb",
57 };
58
59 static const sa_fstype_t *fstypes[SA_PROTOCOL_COUNT] =
60 {&libshare_nfs_type, &libshare_smb_type};
61
62 int
sa_enable_share(const char * zfsname,const char * mountpoint,const char * shareopts,enum sa_protocol protocol)63 sa_enable_share(const char *zfsname, const char *mountpoint,
64 const char *shareopts, enum sa_protocol protocol)
65 {
66 VALIDATE_PROTOCOL(protocol, SA_INVALID_PROTOCOL);
67
68 const struct sa_share_impl args =
69 init_share(zfsname, mountpoint, shareopts);
70 return (fstypes[protocol]->enable_share(&args));
71 }
72
73 int
sa_disable_share(const char * mountpoint,enum sa_protocol protocol)74 sa_disable_share(const char *mountpoint, enum sa_protocol protocol)
75 {
76 VALIDATE_PROTOCOL(protocol, SA_INVALID_PROTOCOL);
77
78 const struct sa_share_impl args = init_share(NULL, mountpoint, NULL);
79 return (fstypes[protocol]->disable_share(&args));
80 }
81
82 boolean_t
sa_is_shared(const char * mountpoint,enum sa_protocol protocol)83 sa_is_shared(const char *mountpoint, enum sa_protocol protocol)
84 {
85 VALIDATE_PROTOCOL(protocol, B_FALSE);
86
87 const struct sa_share_impl args = init_share(NULL, mountpoint, NULL);
88 return (fstypes[protocol]->is_shared(&args));
89 }
90
91 void
sa_commit_shares(enum sa_protocol protocol)92 sa_commit_shares(enum sa_protocol protocol)
93 {
94 /* CSTYLED */
95 VALIDATE_PROTOCOL(protocol, );
96
97 fstypes[protocol]->commit_shares();
98 }
99
100 void
sa_truncate_shares(enum sa_protocol protocol)101 sa_truncate_shares(enum sa_protocol protocol)
102 {
103 /* CSTYLED */
104 VALIDATE_PROTOCOL(protocol, );
105
106 if (fstypes[protocol]->truncate_shares != NULL)
107 fstypes[protocol]->truncate_shares();
108 }
109
110 int
sa_validate_shareopts(const char * options,enum sa_protocol protocol)111 sa_validate_shareopts(const char *options, enum sa_protocol protocol)
112 {
113 VALIDATE_PROTOCOL(protocol, SA_INVALID_PROTOCOL);
114
115 return (fstypes[protocol]->validate_shareopts(options));
116 }
117
118 /*
119 * sa_errorstr(err)
120 *
121 * convert an error value to an error string
122 */
123 const char *
sa_errorstr(int err)124 sa_errorstr(int err)
125 {
126 static char errstr[32];
127
128 switch (err) {
129 case SA_OK:
130 return (dgettext(TEXT_DOMAIN, "ok"));
131 case SA_NO_SUCH_PATH:
132 return (dgettext(TEXT_DOMAIN, "path doesn't exist"));
133 case SA_NO_MEMORY:
134 return (dgettext(TEXT_DOMAIN, "no memory"));
135 case SA_DUPLICATE_NAME:
136 return (dgettext(TEXT_DOMAIN, "name in use"));
137 case SA_BAD_PATH:
138 return (dgettext(TEXT_DOMAIN, "bad path"));
139 case SA_NO_SUCH_GROUP:
140 return (dgettext(TEXT_DOMAIN, "no such group"));
141 case SA_CONFIG_ERR:
142 return (dgettext(TEXT_DOMAIN, "configuration error"));
143 case SA_SYSTEM_ERR:
144 return (dgettext(TEXT_DOMAIN, "system error"));
145 case SA_SYNTAX_ERR:
146 return (dgettext(TEXT_DOMAIN, "syntax error"));
147 case SA_NO_PERMISSION:
148 return (dgettext(TEXT_DOMAIN, "no permission"));
149 case SA_BUSY:
150 return (dgettext(TEXT_DOMAIN, "busy"));
151 case SA_NO_SUCH_PROP:
152 return (dgettext(TEXT_DOMAIN, "no such property"));
153 case SA_INVALID_NAME:
154 return (dgettext(TEXT_DOMAIN, "invalid name"));
155 case SA_INVALID_PROTOCOL:
156 return (dgettext(TEXT_DOMAIN, "invalid protocol"));
157 case SA_NOT_ALLOWED:
158 return (dgettext(TEXT_DOMAIN, "operation not allowed"));
159 case SA_BAD_VALUE:
160 return (dgettext(TEXT_DOMAIN, "bad property value"));
161 case SA_INVALID_SECURITY:
162 return (dgettext(TEXT_DOMAIN, "invalid security type"));
163 case SA_NO_SUCH_SECURITY:
164 return (dgettext(TEXT_DOMAIN, "security type not found"));
165 case SA_VALUE_CONFLICT:
166 return (dgettext(TEXT_DOMAIN, "property value conflict"));
167 case SA_NOT_IMPLEMENTED:
168 return (dgettext(TEXT_DOMAIN, "not implemented"));
169 case SA_INVALID_PATH:
170 return (dgettext(TEXT_DOMAIN, "invalid path"));
171 case SA_NOT_SUPPORTED:
172 return (dgettext(TEXT_DOMAIN, "operation not supported"));
173 case SA_PROP_SHARE_ONLY:
174 return (dgettext(TEXT_DOMAIN, "property not valid for group"));
175 case SA_NOT_SHARED:
176 return (dgettext(TEXT_DOMAIN, "not shared"));
177 case SA_NO_SUCH_RESOURCE:
178 return (dgettext(TEXT_DOMAIN, "no such resource"));
179 case SA_RESOURCE_REQUIRED:
180 return (dgettext(TEXT_DOMAIN, "resource name required"));
181 case SA_MULTIPLE_ERROR:
182 return (dgettext(TEXT_DOMAIN,
183 "errors from multiple protocols"));
184 case SA_PATH_IS_SUBDIR:
185 return (dgettext(TEXT_DOMAIN, "path is a subpath of share"));
186 case SA_PATH_IS_PARENTDIR:
187 return (dgettext(TEXT_DOMAIN, "path is parent of a share"));
188 case SA_NO_SECTION:
189 return (dgettext(TEXT_DOMAIN, "protocol requires a section"));
190 case SA_NO_PROPERTIES:
191 return (dgettext(TEXT_DOMAIN, "properties not found"));
192 case SA_NO_SUCH_SECTION:
193 return (dgettext(TEXT_DOMAIN, "section not found"));
194 case SA_PASSWORD_ENC:
195 return (dgettext(TEXT_DOMAIN, "passwords must be encrypted"));
196 case SA_SHARE_EXISTS:
197 return (dgettext(TEXT_DOMAIN,
198 "path or file is already shared"));
199 default:
200 (void) snprintf(errstr, sizeof (errstr),
201 dgettext(TEXT_DOMAIN, "unknown %d"), err);
202 return (errstr);
203 }
204 }
205