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