xref: /freebsd/sys/contrib/openzfs/lib/libshare/libshare.c (revision 924226fba12cc9a228c73b956e1b7fa24c60b055)
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 http://www.opensolaris.org/os/licensing.
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, 2020 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
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
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
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
91 sa_commit_shares(enum sa_protocol protocol)
92 {
93 	/* CSTYLED */
94 	VALIDATE_PROTOCOL(protocol, );
95 
96 	fstypes[protocol]->commit_shares();
97 }
98 
99 int
100 sa_validate_shareopts(const char *options, enum sa_protocol protocol)
101 {
102 	VALIDATE_PROTOCOL(protocol, SA_INVALID_PROTOCOL);
103 
104 	return (fstypes[protocol]->validate_shareopts(options));
105 }
106 
107 /*
108  * sa_errorstr(err)
109  *
110  * convert an error value to an error string
111  */
112 const char *
113 sa_errorstr(int err)
114 {
115 	static char errstr[32];
116 
117 	switch (err) {
118 	case SA_OK:
119 		return (dgettext(TEXT_DOMAIN, "ok"));
120 	case SA_NO_SUCH_PATH:
121 		return (dgettext(TEXT_DOMAIN, "path doesn't exist"));
122 	case SA_NO_MEMORY:
123 		return (dgettext(TEXT_DOMAIN, "no memory"));
124 	case SA_DUPLICATE_NAME:
125 		return (dgettext(TEXT_DOMAIN, "name in use"));
126 	case SA_BAD_PATH:
127 		return (dgettext(TEXT_DOMAIN, "bad path"));
128 	case SA_NO_SUCH_GROUP:
129 		return (dgettext(TEXT_DOMAIN, "no such group"));
130 	case SA_CONFIG_ERR:
131 		return (dgettext(TEXT_DOMAIN, "configuration error"));
132 	case SA_SYSTEM_ERR:
133 		return (dgettext(TEXT_DOMAIN, "system error"));
134 	case SA_SYNTAX_ERR:
135 		return (dgettext(TEXT_DOMAIN, "syntax error"));
136 	case SA_NO_PERMISSION:
137 		return (dgettext(TEXT_DOMAIN, "no permission"));
138 	case SA_BUSY:
139 		return (dgettext(TEXT_DOMAIN, "busy"));
140 	case SA_NO_SUCH_PROP:
141 		return (dgettext(TEXT_DOMAIN, "no such property"));
142 	case SA_INVALID_NAME:
143 		return (dgettext(TEXT_DOMAIN, "invalid name"));
144 	case SA_INVALID_PROTOCOL:
145 		return (dgettext(TEXT_DOMAIN, "invalid protocol"));
146 	case SA_NOT_ALLOWED:
147 		return (dgettext(TEXT_DOMAIN, "operation not allowed"));
148 	case SA_BAD_VALUE:
149 		return (dgettext(TEXT_DOMAIN, "bad property value"));
150 	case SA_INVALID_SECURITY:
151 		return (dgettext(TEXT_DOMAIN, "invalid security type"));
152 	case SA_NO_SUCH_SECURITY:
153 		return (dgettext(TEXT_DOMAIN, "security type not found"));
154 	case SA_VALUE_CONFLICT:
155 		return (dgettext(TEXT_DOMAIN, "property value conflict"));
156 	case SA_NOT_IMPLEMENTED:
157 		return (dgettext(TEXT_DOMAIN, "not implemented"));
158 	case SA_INVALID_PATH:
159 		return (dgettext(TEXT_DOMAIN, "invalid path"));
160 	case SA_NOT_SUPPORTED:
161 		return (dgettext(TEXT_DOMAIN, "operation not supported"));
162 	case SA_PROP_SHARE_ONLY:
163 		return (dgettext(TEXT_DOMAIN, "property not valid for group"));
164 	case SA_NOT_SHARED:
165 		return (dgettext(TEXT_DOMAIN, "not shared"));
166 	case SA_NO_SUCH_RESOURCE:
167 		return (dgettext(TEXT_DOMAIN, "no such resource"));
168 	case SA_RESOURCE_REQUIRED:
169 		return (dgettext(TEXT_DOMAIN, "resource name required"));
170 	case SA_MULTIPLE_ERROR:
171 		return (dgettext(TEXT_DOMAIN,
172 		    "errors from multiple protocols"));
173 	case SA_PATH_IS_SUBDIR:
174 		return (dgettext(TEXT_DOMAIN, "path is a subpath of share"));
175 	case SA_PATH_IS_PARENTDIR:
176 		return (dgettext(TEXT_DOMAIN, "path is parent of a share"));
177 	case SA_NO_SECTION:
178 		return (dgettext(TEXT_DOMAIN, "protocol requires a section"));
179 	case SA_NO_PROPERTIES:
180 		return (dgettext(TEXT_DOMAIN, "properties not found"));
181 	case SA_NO_SUCH_SECTION:
182 		return (dgettext(TEXT_DOMAIN, "section not found"));
183 	case SA_PASSWORD_ENC:
184 		return (dgettext(TEXT_DOMAIN, "passwords must be encrypted"));
185 	case SA_SHARE_EXISTS:
186 		return (dgettext(TEXT_DOMAIN,
187 		    "path or file is already shared"));
188 	default:
189 		(void) snprintf(errstr, sizeof (errstr),
190 		    dgettext(TEXT_DOMAIN, "unknown %d"), err);
191 		return (errstr);
192 	}
193 }
194