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