xref: /freebsd/sys/contrib/openzfs/lib/libshare/os/linux/nfs.c (revision c6989859ae9388eeb46a24fe88f9b8d07101c710)
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	FILE_HEADER		"# !!! DO NOT EDIT THIS FILE MANUALLY !!!\n\n"
46 #define	ZFS_EXPORTS_DIR		"/etc/exports.d"
47 #define	ZFS_EXPORTS_FILE	ZFS_EXPORTS_DIR"/zfs.exports"
48 #define	ZFS_EXPORTS_LOCK	ZFS_EXPORTS_FILE".lock"
49 
50 static sa_fstype_t *nfs_fstype;
51 
52 typedef int (*nfs_shareopt_callback_t)(const char *opt, const char *value,
53     void *cookie);
54 
55 typedef int (*nfs_host_callback_t)(const char *sharepath, const char *filename,
56     const char *host, const char *security, const char *access, void *cookie);
57 
58 static int nfs_lock_fd = -1;
59 
60 /*
61  * The nfs_exports_[lock|unlock] is used to guard against conconcurrent
62  * updates to the exports file. Each protocol is responsible for
63  * providing the necessary locking to ensure consistency.
64  */
65 static int
66 nfs_exports_lock(void)
67 {
68 	nfs_lock_fd = open(ZFS_EXPORTS_LOCK,
69 	    O_RDWR | O_CREAT, 0600);
70 	if (nfs_lock_fd == -1) {
71 		fprintf(stderr, "failed to lock %s: %s\n",
72 		    ZFS_EXPORTS_LOCK, strerror(errno));
73 		return (errno);
74 	}
75 	if (flock(nfs_lock_fd, LOCK_EX) != 0) {
76 		fprintf(stderr, "failed to lock %s: %s\n",
77 		    ZFS_EXPORTS_LOCK, strerror(errno));
78 		return (errno);
79 	}
80 	return (0);
81 }
82 
83 static void
84 nfs_exports_unlock(void)
85 {
86 	verify(nfs_lock_fd > 0);
87 
88 	if (flock(nfs_lock_fd, LOCK_UN) != 0) {
89 		fprintf(stderr, "failed to unlock %s: %s\n",
90 		    ZFS_EXPORTS_LOCK, strerror(errno));
91 	}
92 	close(nfs_lock_fd);
93 	nfs_lock_fd = -1;
94 }
95 
96 /*
97  * Invokes the specified callback function for each Solaris share option
98  * listed in the specified string.
99  */
100 static int
101 foreach_nfs_shareopt(const char *shareopts,
102     nfs_shareopt_callback_t callback, void *cookie)
103 {
104 	char *shareopts_dup, *opt, *cur, *value;
105 	int was_nul, error;
106 
107 	if (shareopts == NULL)
108 		return (SA_OK);
109 
110 	if (strcmp(shareopts, "on") == 0)
111 		shareopts = "rw,crossmnt";
112 
113 	shareopts_dup = strdup(shareopts);
114 
115 
116 	if (shareopts_dup == NULL)
117 		return (SA_NO_MEMORY);
118 
119 	opt = shareopts_dup;
120 	was_nul = 0;
121 
122 	while (1) {
123 		cur = opt;
124 
125 		while (*cur != ',' && *cur != '\0')
126 			cur++;
127 
128 		if (*cur == '\0')
129 			was_nul = 1;
130 
131 		*cur = '\0';
132 
133 		if (cur > opt) {
134 			value = strchr(opt, '=');
135 
136 			if (value != NULL) {
137 				*value = '\0';
138 				value++;
139 			}
140 
141 			error = callback(opt, value, cookie);
142 
143 			if (error != SA_OK) {
144 				free(shareopts_dup);
145 				return (error);
146 			}
147 		}
148 
149 		opt = cur + 1;
150 
151 		if (was_nul)
152 			break;
153 	}
154 
155 	free(shareopts_dup);
156 
157 	return (SA_OK);
158 }
159 
160 typedef struct nfs_host_cookie_s {
161 	nfs_host_callback_t callback;
162 	const char *sharepath;
163 	void *cookie;
164 	const char *filename;
165 	const char *security;
166 } nfs_host_cookie_t;
167 
168 /*
169  * Helper function for foreach_nfs_host. This function checks whether the
170  * current share option is a host specification and invokes a callback
171  * function with information about the host.
172  */
173 static int
174 foreach_nfs_host_cb(const char *opt, const char *value, void *pcookie)
175 {
176 	int error;
177 	const char *access;
178 	char *host_dup, *host, *next;
179 	nfs_host_cookie_t *udata = (nfs_host_cookie_t *)pcookie;
180 
181 #ifdef DEBUG
182 	fprintf(stderr, "foreach_nfs_host_cb: key=%s, value=%s\n", opt, value);
183 #endif
184 
185 	if (strcmp(opt, "sec") == 0)
186 		udata->security = value;
187 
188 	if (strcmp(opt, "rw") == 0 || strcmp(opt, "ro") == 0) {
189 		if (value == NULL)
190 			value = "*";
191 
192 		access = opt;
193 
194 		host_dup = strdup(value);
195 
196 		if (host_dup == NULL)
197 			return (SA_NO_MEMORY);
198 
199 		host = host_dup;
200 
201 		do {
202 			next = strchr(host, ':');
203 			if (next != NULL) {
204 				*next = '\0';
205 				next++;
206 			}
207 
208 			error = udata->callback(udata->filename,
209 			    udata->sharepath, host, udata->security,
210 			    access, udata->cookie);
211 
212 			if (error != SA_OK) {
213 				free(host_dup);
214 
215 				return (error);
216 			}
217 
218 			host = next;
219 		} while (host != NULL);
220 
221 		free(host_dup);
222 	}
223 
224 	return (SA_OK);
225 }
226 
227 /*
228  * Invokes a callback function for all NFS hosts that are set for a share.
229  */
230 static int
231 foreach_nfs_host(sa_share_impl_t impl_share, char *filename,
232     nfs_host_callback_t callback, void *cookie)
233 {
234 	nfs_host_cookie_t udata;
235 	char *shareopts;
236 
237 	udata.callback = callback;
238 	udata.sharepath = impl_share->sa_mountpoint;
239 	udata.cookie = cookie;
240 	udata.filename = filename;
241 	udata.security = "sys";
242 
243 	shareopts = FSINFO(impl_share, nfs_fstype)->shareopts;
244 
245 	return (foreach_nfs_shareopt(shareopts, foreach_nfs_host_cb,
246 	    &udata));
247 }
248 
249 /*
250  * Converts a Solaris NFS host specification to its Linux equivalent.
251  */
252 static int
253 get_linux_hostspec(const char *solaris_hostspec, char **plinux_hostspec)
254 {
255 	/*
256 	 * For now we just support CIDR masks (e.g. @192.168.0.0/16) and host
257 	 * wildcards (e.g. *.example.org).
258 	 */
259 	if (solaris_hostspec[0] == '@') {
260 		/*
261 		 * Solaris host specifier, e.g. @192.168.0.0/16; we just need
262 		 * to skip the @ in this case
263 		 */
264 		*plinux_hostspec = strdup(solaris_hostspec + 1);
265 	} else {
266 		*plinux_hostspec = strdup(solaris_hostspec);
267 	}
268 
269 	if (*plinux_hostspec == NULL) {
270 		return (SA_NO_MEMORY);
271 	}
272 
273 	return (SA_OK);
274 }
275 
276 /*
277  * Adds a Linux share option to an array of NFS options.
278  */
279 static int
280 add_linux_shareopt(char **plinux_opts, const char *key, const char *value)
281 {
282 	size_t len = 0;
283 	char *new_linux_opts;
284 
285 	if (*plinux_opts != NULL)
286 		len = strlen(*plinux_opts);
287 
288 	new_linux_opts = realloc(*plinux_opts, len + 1 + strlen(key) +
289 	    (value ? 1 + strlen(value) : 0) + 1);
290 
291 	if (new_linux_opts == NULL)
292 		return (SA_NO_MEMORY);
293 
294 	new_linux_opts[len] = '\0';
295 
296 	if (len > 0)
297 		strcat(new_linux_opts, ",");
298 
299 	strcat(new_linux_opts, key);
300 
301 	if (value != NULL) {
302 		strcat(new_linux_opts, "=");
303 		strcat(new_linux_opts, value);
304 	}
305 
306 	*plinux_opts = new_linux_opts;
307 
308 	return (SA_OK);
309 }
310 
311 /*
312  * Validates and converts a single Solaris share option to its Linux
313  * equivalent.
314  */
315 static int
316 get_linux_shareopts_cb(const char *key, const char *value, void *cookie)
317 {
318 	char **plinux_opts = (char **)cookie;
319 
320 	/* host-specific options, these are taken care of elsewhere */
321 	if (strcmp(key, "ro") == 0 || strcmp(key, "rw") == 0 ||
322 	    strcmp(key, "sec") == 0)
323 		return (SA_OK);
324 
325 	if (strcmp(key, "anon") == 0)
326 		key = "anonuid";
327 
328 	if (strcmp(key, "root_mapping") == 0) {
329 		(void) add_linux_shareopt(plinux_opts, "root_squash", NULL);
330 		key = "anonuid";
331 	}
332 
333 	if (strcmp(key, "nosub") == 0)
334 		key = "subtree_check";
335 
336 	if (strcmp(key, "insecure") != 0 && strcmp(key, "secure") != 0 &&
337 	    strcmp(key, "async") != 0 && strcmp(key, "sync") != 0 &&
338 	    strcmp(key, "no_wdelay") != 0 && strcmp(key, "wdelay") != 0 &&
339 	    strcmp(key, "nohide") != 0 && strcmp(key, "hide") != 0 &&
340 	    strcmp(key, "crossmnt") != 0 &&
341 	    strcmp(key, "no_subtree_check") != 0 &&
342 	    strcmp(key, "subtree_check") != 0 &&
343 	    strcmp(key, "insecure_locks") != 0 &&
344 	    strcmp(key, "secure_locks") != 0 &&
345 	    strcmp(key, "no_auth_nlm") != 0 && strcmp(key, "auth_nlm") != 0 &&
346 	    strcmp(key, "no_acl") != 0 && strcmp(key, "mountpoint") != 0 &&
347 	    strcmp(key, "mp") != 0 && strcmp(key, "fsuid") != 0 &&
348 	    strcmp(key, "refer") != 0 && strcmp(key, "replicas") != 0 &&
349 	    strcmp(key, "root_squash") != 0 &&
350 	    strcmp(key, "no_root_squash") != 0 &&
351 	    strcmp(key, "all_squash") != 0 &&
352 	    strcmp(key, "no_all_squash") != 0 && strcmp(key, "fsid") != 0 &&
353 	    strcmp(key, "anonuid") != 0 && strcmp(key, "anongid") != 0) {
354 		return (SA_SYNTAX_ERR);
355 	}
356 
357 	(void) add_linux_shareopt(plinux_opts, key, value);
358 
359 	return (SA_OK);
360 }
361 
362 /*
363  * Takes a string containing Solaris share options (e.g. "sync,no_acl") and
364  * converts them to a NULL-terminated array of Linux NFS options.
365  */
366 static int
367 get_linux_shareopts(const char *shareopts, char **plinux_opts)
368 {
369 	int error;
370 
371 	assert(plinux_opts != NULL);
372 
373 	*plinux_opts = NULL;
374 
375 	/* no_subtree_check - Default as of nfs-utils v1.1.0 */
376 	(void) add_linux_shareopt(plinux_opts, "no_subtree_check", NULL);
377 
378 	/* mountpoint - Restrict exports to ZFS mountpoints */
379 	(void) add_linux_shareopt(plinux_opts, "mountpoint", NULL);
380 
381 	error = foreach_nfs_shareopt(shareopts, get_linux_shareopts_cb,
382 	    plinux_opts);
383 
384 	if (error != SA_OK) {
385 		free(*plinux_opts);
386 		*plinux_opts = NULL;
387 	}
388 
389 	return (error);
390 }
391 
392 static char *
393 nfs_init_tmpfile(void)
394 {
395 	char *tmpfile = NULL;
396 
397 	if (asprintf(&tmpfile, "%s%s", ZFS_EXPORTS_FILE, ".XXXXXXXX") == -1) {
398 		fprintf(stderr, "Unable to allocate temporary file\n");
399 		return (NULL);
400 	}
401 
402 	int fd = mkstemp(tmpfile);
403 	if (fd == -1) {
404 		fprintf(stderr, "Unable to create temporary file: %s",
405 		    strerror(errno));
406 		free(tmpfile);
407 		return (NULL);
408 	}
409 	close(fd);
410 	return (tmpfile);
411 }
412 
413 static int
414 nfs_fini_tmpfile(char *tmpfile)
415 {
416 	if (rename(tmpfile, ZFS_EXPORTS_FILE) == -1) {
417 		fprintf(stderr, "Unable to rename %s: %s\n", tmpfile,
418 		    strerror(errno));
419 		unlink(tmpfile);
420 		free(tmpfile);
421 		return (SA_SYSTEM_ERR);
422 	}
423 	free(tmpfile);
424 	return (SA_OK);
425 }
426 
427 /*
428  * This function populates an entry into /etc/exports.d/zfs.exports.
429  * This file is consumed by the linux nfs server so that zfs shares are
430  * automatically exported upon boot or whenever the nfs server restarts.
431  */
432 static int
433 nfs_add_entry(const char *filename, const char *sharepath,
434     const char *host, const char *security, const char *access_opts,
435     void *pcookie)
436 {
437 	int error;
438 	char *linuxhost;
439 	const char *linux_opts = (const char *)pcookie;
440 
441 	error = get_linux_hostspec(host, &linuxhost);
442 	if (error != SA_OK)
443 		return (error);
444 
445 	if (linux_opts == NULL)
446 		linux_opts = "";
447 
448 	FILE *fp = fopen(filename, "a+");
449 	if (fp == NULL) {
450 		fprintf(stderr, "failed to open %s file: %s", filename,
451 		    strerror(errno));
452 		free(linuxhost);
453 		return (SA_SYSTEM_ERR);
454 	}
455 
456 	if (fprintf(fp, "%s %s(sec=%s,%s,%s)\n", sharepath, linuxhost,
457 	    security, access_opts, linux_opts) < 0) {
458 		fprintf(stderr, "failed to write to %s\n", filename);
459 		free(linuxhost);
460 		fclose(fp);
461 		return (SA_SYSTEM_ERR);
462 	}
463 
464 	free(linuxhost);
465 	if (fclose(fp) != 0) {
466 		fprintf(stderr, "Unable to close file %s: %s\n",
467 		    filename, strerror(errno));
468 		return (SA_SYSTEM_ERR);
469 	}
470 	return (SA_OK);
471 }
472 
473 /*
474  * This function copies all entries from the exports file to "filename",
475  * omitting any entries for the specified mountpoint.
476  */
477 static int
478 nfs_copy_entries(char *filename, const char *mountpoint)
479 {
480 	char *buf = NULL;
481 	size_t buflen = 0;
482 	int error = SA_OK;
483 
484 	/*
485 	 * If the file doesn't exist then there is nothing more
486 	 * we need to do.
487 	 */
488 	FILE *oldfp = fopen(ZFS_EXPORTS_FILE, "r");
489 	if (oldfp == NULL)
490 		return (SA_OK);
491 
492 	FILE *newfp = fopen(filename, "w+");
493 	fputs(FILE_HEADER, newfp);
494 	while ((getline(&buf, &buflen, oldfp)) != -1) {
495 		char *space = NULL;
496 
497 		if (buf[0] == '\n' || buf[0] == '#')
498 			continue;
499 
500 		if ((space = strchr(buf, ' ')) != NULL) {
501 			int mountpoint_len = strlen(mountpoint);
502 
503 			if (space - buf == mountpoint_len &&
504 			    strncmp(mountpoint, buf, mountpoint_len) == 0) {
505 				continue;
506 			}
507 		}
508 		fputs(buf, newfp);
509 	}
510 
511 	if (oldfp != NULL && ferror(oldfp) != 0) {
512 		error = ferror(oldfp);
513 	}
514 	if (error == 0 && ferror(newfp) != 0) {
515 		error = ferror(newfp);
516 	}
517 
518 	free(buf);
519 	if (fclose(newfp) != 0) {
520 		fprintf(stderr, "Unable to close file %s: %s\n",
521 		    filename, strerror(errno));
522 		error = error != 0 ? error : SA_SYSTEM_ERR;
523 	}
524 	fclose(oldfp);
525 
526 	return (error);
527 }
528 
529 /*
530  * Enables NFS sharing for the specified share.
531  */
532 static int
533 nfs_enable_share(sa_share_impl_t impl_share)
534 {
535 	char *shareopts, *linux_opts;
536 	char *filename = NULL;
537 	int error;
538 
539 	if ((filename = nfs_init_tmpfile()) == NULL)
540 		return (SA_SYSTEM_ERR);
541 
542 	error = nfs_exports_lock();
543 	if (error != 0) {
544 		unlink(filename);
545 		free(filename);
546 		return (error);
547 	}
548 
549 	error = nfs_copy_entries(filename, impl_share->sa_mountpoint);
550 	if (error != SA_OK) {
551 		unlink(filename);
552 		free(filename);
553 		nfs_exports_unlock();
554 		return (error);
555 	}
556 
557 	shareopts = FSINFO(impl_share, nfs_fstype)->shareopts;
558 	error = get_linux_shareopts(shareopts, &linux_opts);
559 	if (error != SA_OK) {
560 		unlink(filename);
561 		free(filename);
562 		nfs_exports_unlock();
563 		return (error);
564 	}
565 
566 	error = foreach_nfs_host(impl_share, filename, nfs_add_entry,
567 	    linux_opts);
568 	free(linux_opts);
569 	if (error == 0) {
570 		error = nfs_fini_tmpfile(filename);
571 	} else {
572 		unlink(filename);
573 		free(filename);
574 	}
575 	nfs_exports_unlock();
576 	return (error);
577 }
578 
579 /*
580  * Disables NFS sharing for the specified share.
581  */
582 static int
583 nfs_disable_share(sa_share_impl_t impl_share)
584 {
585 	int error;
586 	char *filename = NULL;
587 
588 	if ((filename = nfs_init_tmpfile()) == NULL)
589 		return (SA_SYSTEM_ERR);
590 
591 	error = nfs_exports_lock();
592 	if (error != 0) {
593 		unlink(filename);
594 		free(filename);
595 		return (error);
596 	}
597 
598 	error = nfs_copy_entries(filename, impl_share->sa_mountpoint);
599 	if (error != SA_OK) {
600 		unlink(filename);
601 		free(filename);
602 		nfs_exports_unlock();
603 		return (error);
604 	}
605 	error = nfs_fini_tmpfile(filename);
606 	nfs_exports_unlock();
607 	return (error);
608 }
609 
610 static boolean_t
611 nfs_is_shared(sa_share_impl_t impl_share)
612 {
613 	size_t buflen = 0;
614 	char *buf = NULL;
615 
616 	FILE *fp = fopen(ZFS_EXPORTS_FILE, "r");
617 	if (fp == NULL) {
618 		return (B_FALSE);
619 	}
620 	while ((getline(&buf, &buflen, fp)) != -1) {
621 		char *space = NULL;
622 
623 		if ((space = strchr(buf, ' ')) != NULL) {
624 			int mountpoint_len = strlen(impl_share->sa_mountpoint);
625 
626 			if (space - buf == mountpoint_len &&
627 			    strncmp(impl_share->sa_mountpoint, buf,
628 			    mountpoint_len) == 0) {
629 				fclose(fp);
630 				free(buf);
631 				return (B_TRUE);
632 			}
633 		}
634 	}
635 	free(buf);
636 	fclose(fp);
637 	return (B_FALSE);
638 }
639 
640 /*
641  * Checks whether the specified NFS share options are syntactically correct.
642  */
643 static int
644 nfs_validate_shareopts(const char *shareopts)
645 {
646 	char *linux_opts;
647 	int error;
648 
649 	error = get_linux_shareopts(shareopts, &linux_opts);
650 
651 	if (error != SA_OK)
652 		return (error);
653 
654 	free(linux_opts);
655 	return (SA_OK);
656 }
657 
658 static int
659 nfs_update_shareopts(sa_share_impl_t impl_share, const char *shareopts)
660 {
661 	FSINFO(impl_share, nfs_fstype)->shareopts = (char *)shareopts;
662 	return (SA_OK);
663 }
664 
665 /*
666  * Clears a share's NFS options. Used by libshare to
667  * clean up shares that are about to be free()'d.
668  */
669 static void
670 nfs_clear_shareopts(sa_share_impl_t impl_share)
671 {
672 	FSINFO(impl_share, nfs_fstype)->shareopts = NULL;
673 }
674 
675 static int
676 nfs_commit_shares(void)
677 {
678 	char *argv[] = {
679 	    "/usr/sbin/exportfs",
680 	    "-ra",
681 	    NULL
682 	};
683 
684 	return (libzfs_run_process(argv[0], argv, 0));
685 }
686 
687 static const sa_share_ops_t nfs_shareops = {
688 	.enable_share = nfs_enable_share,
689 	.disable_share = nfs_disable_share,
690 	.is_shared = nfs_is_shared,
691 
692 	.validate_shareopts = nfs_validate_shareopts,
693 	.update_shareopts = nfs_update_shareopts,
694 	.clear_shareopts = nfs_clear_shareopts,
695 	.commit_shares = nfs_commit_shares,
696 };
697 
698 /*
699  * Initializes the NFS functionality of libshare.
700  */
701 void
702 libshare_nfs_init(void)
703 {
704 	struct stat sb;
705 
706 	nfs_fstype = register_fstype("nfs", &nfs_shareops);
707 
708 	if (stat(ZFS_EXPORTS_DIR, &sb) < 0 &&
709 	    mkdir(ZFS_EXPORTS_DIR, 0755) < 0) {
710 		fprintf(stderr, "failed to create %s: %s\n",
711 		    ZFS_EXPORTS_DIR, strerror(errno));
712 	}
713 }
714