xref: /freebsd/sys/contrib/openzfs/lib/libshare/os/linux/nfs.c (revision 45dd2eaac379e5576f745380260470204c49beac)
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) 2012 Cyril Plisko. All rights reserved.
26  * Copyright (c) 2019, 2020 by Delphix. All rights reserved.
27  */
28 
29 #include <dirent.h>
30 #include <stdio.h>
31 #include <string.h>
32 #include <errno.h>
33 #include <fcntl.h>
34 #include <sys/file.h>
35 #include <sys/stat.h>
36 #include <sys/types.h>
37 #include <sys/wait.h>
38 #include <unistd.h>
39 #include <libzfs.h>
40 #include <libshare.h>
41 #include "libshare_impl.h"
42 #include "nfs.h"
43 
44 #define	ZFS_EXPORTS_DIR		"/etc/exports.d"
45 #define	ZFS_EXPORTS_FILE	ZFS_EXPORTS_DIR"/zfs.exports"
46 #define	ZFS_EXPORTS_LOCK	ZFS_EXPORTS_FILE".lock"
47 
48 static sa_fstype_t *nfs_fstype;
49 
50 typedef int (*nfs_shareopt_callback_t)(const char *opt, const char *value,
51     void *cookie);
52 
53 typedef int (*nfs_host_callback_t)(FILE *tmpfile, const char *sharepath,
54     const char *host, const char *security, const char *access, void *cookie);
55 
56 /*
57  * Invokes the specified callback function for each Solaris share option
58  * listed in the specified string.
59  */
60 static int
61 foreach_nfs_shareopt(const char *shareopts,
62     nfs_shareopt_callback_t callback, void *cookie)
63 {
64 	char *shareopts_dup, *opt, *cur, *value;
65 	int was_nul, error;
66 
67 	if (shareopts == NULL)
68 		return (SA_OK);
69 
70 	if (strcmp(shareopts, "on") == 0)
71 		shareopts = "rw,crossmnt";
72 
73 	shareopts_dup = strdup(shareopts);
74 
75 
76 	if (shareopts_dup == NULL)
77 		return (SA_NO_MEMORY);
78 
79 	opt = shareopts_dup;
80 	was_nul = 0;
81 
82 	while (1) {
83 		cur = opt;
84 
85 		while (*cur != ',' && *cur != '\0')
86 			cur++;
87 
88 		if (*cur == '\0')
89 			was_nul = 1;
90 
91 		*cur = '\0';
92 
93 		if (cur > opt) {
94 			value = strchr(opt, '=');
95 
96 			if (value != NULL) {
97 				*value = '\0';
98 				value++;
99 			}
100 
101 			error = callback(opt, value, cookie);
102 
103 			if (error != SA_OK) {
104 				free(shareopts_dup);
105 				return (error);
106 			}
107 		}
108 
109 		opt = cur + 1;
110 
111 		if (was_nul)
112 			break;
113 	}
114 
115 	free(shareopts_dup);
116 
117 	return (SA_OK);
118 }
119 
120 typedef struct nfs_host_cookie_s {
121 	nfs_host_callback_t callback;
122 	const char *sharepath;
123 	void *cookie;
124 	FILE *tmpfile;
125 	const char *security;
126 } nfs_host_cookie_t;
127 
128 /*
129  * Helper function for foreach_nfs_host. This function checks whether the
130  * current share option is a host specification and invokes a callback
131  * function with information about the host.
132  */
133 static int
134 foreach_nfs_host_cb(const char *opt, const char *value, void *pcookie)
135 {
136 	int error;
137 	const char *access;
138 	char *host_dup, *host, *next, *v6Literal;
139 	nfs_host_cookie_t *udata = (nfs_host_cookie_t *)pcookie;
140 	int cidr_len;
141 
142 #ifdef DEBUG
143 	fprintf(stderr, "foreach_nfs_host_cb: key=%s, value=%s\n", opt, value);
144 #endif
145 
146 	if (strcmp(opt, "sec") == 0)
147 		udata->security = value;
148 
149 	if (strcmp(opt, "rw") == 0 || strcmp(opt, "ro") == 0) {
150 		if (value == NULL)
151 			value = "*";
152 
153 		access = opt;
154 
155 		host_dup = strdup(value);
156 
157 		if (host_dup == NULL)
158 			return (SA_NO_MEMORY);
159 
160 		host = host_dup;
161 
162 		do {
163 			if (*host == '[') {
164 				host++;
165 				v6Literal = strchr(host, ']');
166 				if (v6Literal == NULL) {
167 					free(host_dup);
168 					return (SA_SYNTAX_ERR);
169 				}
170 				if (v6Literal[1] == '\0') {
171 					*v6Literal = '\0';
172 					next = NULL;
173 				} else if (v6Literal[1] == '/') {
174 					next = strchr(v6Literal + 2, ':');
175 					if (next == NULL) {
176 						cidr_len =
177 						    strlen(v6Literal + 1);
178 						memmove(v6Literal,
179 						    v6Literal + 1,
180 						    cidr_len);
181 						v6Literal[cidr_len] = '\0';
182 					} else {
183 						cidr_len = next - v6Literal - 1;
184 						memmove(v6Literal,
185 						    v6Literal + 1,
186 						    cidr_len);
187 						v6Literal[cidr_len] = '\0';
188 						next++;
189 					}
190 				} else if (v6Literal[1] == ':') {
191 					*v6Literal = '\0';
192 					next = v6Literal + 2;
193 				} else {
194 					free(host_dup);
195 					return (SA_SYNTAX_ERR);
196 				}
197 			} else {
198 				next = strchr(host, ':');
199 				if (next != NULL) {
200 					*next = '\0';
201 					next++;
202 				}
203 			}
204 
205 			error = udata->callback(udata->tmpfile,
206 			    udata->sharepath, host, udata->security,
207 			    access, udata->cookie);
208 
209 			if (error != SA_OK) {
210 				free(host_dup);
211 
212 				return (error);
213 			}
214 
215 			host = next;
216 		} while (host != NULL);
217 
218 		free(host_dup);
219 	}
220 
221 	return (SA_OK);
222 }
223 
224 /*
225  * Invokes a callback function for all NFS hosts that are set for a share.
226  */
227 static int
228 foreach_nfs_host(sa_share_impl_t impl_share, FILE *tmpfile,
229     nfs_host_callback_t callback, void *cookie)
230 {
231 	nfs_host_cookie_t udata;
232 	char *shareopts;
233 
234 	udata.callback = callback;
235 	udata.sharepath = impl_share->sa_mountpoint;
236 	udata.cookie = cookie;
237 	udata.tmpfile = tmpfile;
238 	udata.security = "sys";
239 
240 	shareopts = FSINFO(impl_share, nfs_fstype)->shareopts;
241 
242 	return (foreach_nfs_shareopt(shareopts, foreach_nfs_host_cb,
243 	    &udata));
244 }
245 
246 /*
247  * Converts a Solaris NFS host specification to its Linux equivalent.
248  */
249 static const char *
250 get_linux_hostspec(const char *solaris_hostspec)
251 {
252 	/*
253 	 * For now we just support CIDR masks (e.g. @192.168.0.0/16) and host
254 	 * wildcards (e.g. *.example.org).
255 	 */
256 	if (solaris_hostspec[0] == '@') {
257 		/*
258 		 * Solaris host specifier, e.g. @192.168.0.0/16; we just need
259 		 * to skip the @ in this case
260 		 */
261 		return (solaris_hostspec + 1);
262 	} else {
263 		return (solaris_hostspec);
264 	}
265 }
266 
267 /*
268  * Adds a Linux share option to an array of NFS options.
269  */
270 static int
271 add_linux_shareopt(char **plinux_opts, const char *key, const char *value)
272 {
273 	size_t len = 0;
274 	char *new_linux_opts;
275 
276 	if (*plinux_opts != NULL)
277 		len = strlen(*plinux_opts);
278 
279 	new_linux_opts = realloc(*plinux_opts, len + 1 + strlen(key) +
280 	    (value ? 1 + strlen(value) : 0) + 1);
281 
282 	if (new_linux_opts == NULL)
283 		return (SA_NO_MEMORY);
284 
285 	new_linux_opts[len] = '\0';
286 
287 	if (len > 0)
288 		strcat(new_linux_opts, ",");
289 
290 	strcat(new_linux_opts, key);
291 
292 	if (value != NULL) {
293 		strcat(new_linux_opts, "=");
294 		strcat(new_linux_opts, value);
295 	}
296 
297 	*plinux_opts = new_linux_opts;
298 
299 	return (SA_OK);
300 }
301 
302 /*
303  * Validates and converts a single Solaris share option to its Linux
304  * equivalent.
305  */
306 static int
307 get_linux_shareopts_cb(const char *key, const char *value, void *cookie)
308 {
309 	char **plinux_opts = (char **)cookie;
310 
311 	/* host-specific options, these are taken care of elsewhere */
312 	if (strcmp(key, "ro") == 0 || strcmp(key, "rw") == 0 ||
313 	    strcmp(key, "sec") == 0)
314 		return (SA_OK);
315 
316 	if (strcmp(key, "anon") == 0)
317 		key = "anonuid";
318 
319 	if (strcmp(key, "root_mapping") == 0) {
320 		(void) add_linux_shareopt(plinux_opts, "root_squash", NULL);
321 		key = "anonuid";
322 	}
323 
324 	if (strcmp(key, "nosub") == 0)
325 		key = "subtree_check";
326 
327 	if (strcmp(key, "insecure") != 0 && strcmp(key, "secure") != 0 &&
328 	    strcmp(key, "async") != 0 && strcmp(key, "sync") != 0 &&
329 	    strcmp(key, "no_wdelay") != 0 && strcmp(key, "wdelay") != 0 &&
330 	    strcmp(key, "nohide") != 0 && strcmp(key, "hide") != 0 &&
331 	    strcmp(key, "crossmnt") != 0 &&
332 	    strcmp(key, "no_subtree_check") != 0 &&
333 	    strcmp(key, "subtree_check") != 0 &&
334 	    strcmp(key, "insecure_locks") != 0 &&
335 	    strcmp(key, "secure_locks") != 0 &&
336 	    strcmp(key, "no_auth_nlm") != 0 && strcmp(key, "auth_nlm") != 0 &&
337 	    strcmp(key, "no_acl") != 0 && strcmp(key, "mountpoint") != 0 &&
338 	    strcmp(key, "mp") != 0 && strcmp(key, "fsuid") != 0 &&
339 	    strcmp(key, "refer") != 0 && strcmp(key, "replicas") != 0 &&
340 	    strcmp(key, "root_squash") != 0 &&
341 	    strcmp(key, "no_root_squash") != 0 &&
342 	    strcmp(key, "all_squash") != 0 &&
343 	    strcmp(key, "no_all_squash") != 0 && strcmp(key, "fsid") != 0 &&
344 	    strcmp(key, "anonuid") != 0 && strcmp(key, "anongid") != 0) {
345 		return (SA_SYNTAX_ERR);
346 	}
347 
348 	(void) add_linux_shareopt(plinux_opts, key, value);
349 
350 	return (SA_OK);
351 }
352 
353 /*
354  * Takes a string containing Solaris share options (e.g. "sync,no_acl") and
355  * converts them to a NULL-terminated array of Linux NFS options.
356  */
357 static int
358 get_linux_shareopts(const char *shareopts, char **plinux_opts)
359 {
360 	int error;
361 
362 	assert(plinux_opts != NULL);
363 
364 	*plinux_opts = NULL;
365 
366 	/* no_subtree_check - Default as of nfs-utils v1.1.0 */
367 	(void) add_linux_shareopt(plinux_opts, "no_subtree_check", NULL);
368 
369 	/* mountpoint - Restrict exports to ZFS mountpoints */
370 	(void) add_linux_shareopt(plinux_opts, "mountpoint", NULL);
371 
372 	error = foreach_nfs_shareopt(shareopts, get_linux_shareopts_cb,
373 	    plinux_opts);
374 
375 	if (error != SA_OK) {
376 		free(*plinux_opts);
377 		*plinux_opts = NULL;
378 	}
379 
380 	return (error);
381 }
382 
383 /*
384  * This function populates an entry into /etc/exports.d/zfs.exports.
385  * This file is consumed by the linux nfs server so that zfs shares are
386  * automatically exported upon boot or whenever the nfs server restarts.
387  */
388 static int
389 nfs_add_entry(FILE *tmpfile, const char *sharepath,
390     const char *host, const char *security, const char *access_opts,
391     void *pcookie)
392 {
393 	const char *linux_opts = (const char *)pcookie;
394 
395 	if (linux_opts == NULL)
396 		linux_opts = "";
397 
398 	if (fprintf(tmpfile, "%s %s(sec=%s,%s,%s)\n", sharepath,
399 	    get_linux_hostspec(host), security, access_opts,
400 	    linux_opts) < 0) {
401 		fprintf(stderr, "failed to write to temporary file\n");
402 		return (SA_SYSTEM_ERR);
403 	}
404 
405 	return (SA_OK);
406 }
407 
408 /*
409  * Enables NFS sharing for the specified share.
410  */
411 static int
412 nfs_enable_share_impl(sa_share_impl_t impl_share, FILE *tmpfile)
413 {
414 	char *shareopts, *linux_opts;
415 	int error;
416 
417 	shareopts = FSINFO(impl_share, nfs_fstype)->shareopts;
418 	error = get_linux_shareopts(shareopts, &linux_opts);
419 	if (error != SA_OK)
420 		return (error);
421 
422 	error = foreach_nfs_host(impl_share, tmpfile, nfs_add_entry,
423 	    linux_opts);
424 	free(linux_opts);
425 	return (error);
426 }
427 
428 static int
429 nfs_enable_share(sa_share_impl_t impl_share)
430 {
431 	return (nfs_toggle_share(
432 	    ZFS_EXPORTS_LOCK, ZFS_EXPORTS_FILE, ZFS_EXPORTS_DIR, impl_share,
433 	    nfs_enable_share_impl));
434 }
435 
436 /*
437  * Disables NFS sharing for the specified share.
438  */
439 static int
440 nfs_disable_share_impl(sa_share_impl_t impl_share, FILE *tmpfile)
441 {
442 	(void) impl_share, (void) tmpfile;
443 	return (SA_OK);
444 }
445 
446 static int
447 nfs_disable_share(sa_share_impl_t impl_share)
448 {
449 	return (nfs_toggle_share(
450 	    ZFS_EXPORTS_LOCK, ZFS_EXPORTS_FILE, ZFS_EXPORTS_DIR, impl_share,
451 	    nfs_disable_share_impl));
452 }
453 
454 static boolean_t
455 nfs_is_shared(sa_share_impl_t impl_share)
456 {
457 	return (nfs_is_shared_impl(ZFS_EXPORTS_FILE, impl_share));
458 }
459 
460 /*
461  * Checks whether the specified NFS share options are syntactically correct.
462  */
463 static int
464 nfs_validate_shareopts(const char *shareopts)
465 {
466 	char *linux_opts;
467 	int error;
468 
469 	error = get_linux_shareopts(shareopts, &linux_opts);
470 
471 	if (error != SA_OK)
472 		return (error);
473 
474 	free(linux_opts);
475 	return (SA_OK);
476 }
477 
478 static int
479 nfs_update_shareopts(sa_share_impl_t impl_share, const char *shareopts)
480 {
481 	FSINFO(impl_share, nfs_fstype)->shareopts = (char *)shareopts;
482 	return (SA_OK);
483 }
484 
485 /*
486  * Clears a share's NFS options. Used by libshare to
487  * clean up shares that are about to be free()'d.
488  */
489 static void
490 nfs_clear_shareopts(sa_share_impl_t impl_share)
491 {
492 	FSINFO(impl_share, nfs_fstype)->shareopts = NULL;
493 }
494 
495 static int
496 nfs_commit_shares(void)
497 {
498 	char *argv[] = {
499 	    "/usr/sbin/exportfs",
500 	    "-ra",
501 	    NULL
502 	};
503 
504 	return (libzfs_run_process(argv[0], argv, 0));
505 }
506 
507 static const sa_share_ops_t nfs_shareops = {
508 	.enable_share = nfs_enable_share,
509 	.disable_share = nfs_disable_share,
510 	.is_shared = nfs_is_shared,
511 
512 	.validate_shareopts = nfs_validate_shareopts,
513 	.update_shareopts = nfs_update_shareopts,
514 	.clear_shareopts = nfs_clear_shareopts,
515 	.commit_shares = nfs_commit_shares,
516 };
517 
518 /*
519  * Initializes the NFS functionality of libshare.
520  */
521 void
522 libshare_nfs_init(void)
523 {
524 	nfs_fstype = register_fstype("nfs", &nfs_shareops);
525 }
526