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.
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.
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]
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.
37 #include <sys/types.h>
42 #include "libshare_impl.h"
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"
50 static sa_fstype_t *nfs_fstype;
52 typedef int (*nfs_shareopt_callback_t)(const char *opt, const char *value,
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);
58 static int nfs_lock_fd = -1;
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.
66 nfs_exports_lock(void)
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));
75 if (flock(nfs_lock_fd, LOCK_EX) != 0) {
76 fprintf(stderr, "failed to lock %s: %s\n",
77 ZFS_EXPORTS_LOCK, strerror(errno));
84 nfs_exports_unlock(void)
86 verify(nfs_lock_fd > 0);
88 if (flock(nfs_lock_fd, LOCK_UN) != 0) {
89 fprintf(stderr, "failed to unlock %s: %s\n",
90 ZFS_EXPORTS_LOCK, strerror(errno));
97 * Invokes the specified callback function for each Solaris share option
98 * listed in the specified string.
101 foreach_nfs_shareopt(const char *shareopts,
102 nfs_shareopt_callback_t callback, void *cookie)
104 char *shareopts_dup, *opt, *cur, *value;
107 if (shareopts == NULL)
110 if (strcmp(shareopts, "on") == 0)
111 shareopts = "rw,crossmnt";
113 shareopts_dup = strdup(shareopts);
116 if (shareopts_dup == NULL)
117 return (SA_NO_MEMORY);
125 while (*cur != ',' && *cur != '\0')
134 value = strchr(opt, '=');
141 error = callback(opt, value, cookie);
143 if (error != SA_OK) {
160 typedef struct nfs_host_cookie_s {
161 nfs_host_callback_t callback;
162 const char *sharepath;
164 const char *filename;
165 const char *security;
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.
174 foreach_nfs_host_cb(const char *opt, const char *value, void *pcookie)
178 char *host_dup, *host, *next;
179 nfs_host_cookie_t *udata = (nfs_host_cookie_t *)pcookie;
182 fprintf(stderr, "foreach_nfs_host_cb: key=%s, value=%s\n", opt, value);
185 if (strcmp(opt, "sec") == 0)
186 udata->security = value;
188 if (strcmp(opt, "rw") == 0 || strcmp(opt, "ro") == 0) {
194 host_dup = strdup(value);
196 if (host_dup == NULL)
197 return (SA_NO_MEMORY);
202 next = strchr(host, ':');
208 error = udata->callback(udata->filename,
209 udata->sharepath, host, udata->security,
210 access, udata->cookie);
212 if (error != SA_OK) {
219 } while (host != NULL);
228 * Invokes a callback function for all NFS hosts that are set for a share.
231 foreach_nfs_host(sa_share_impl_t impl_share, char *filename,
232 nfs_host_callback_t callback, void *cookie)
234 nfs_host_cookie_t udata;
237 udata.callback = callback;
238 udata.sharepath = impl_share->sa_mountpoint;
239 udata.cookie = cookie;
240 udata.filename = filename;
241 udata.security = "sys";
243 shareopts = FSINFO(impl_share, nfs_fstype)->shareopts;
245 return (foreach_nfs_shareopt(shareopts, foreach_nfs_host_cb,
250 * Converts a Solaris NFS host specification to its Linux equivalent.
253 get_linux_hostspec(const char *solaris_hostspec, char **plinux_hostspec)
256 * For now we just support CIDR masks (e.g. @192.168.0.0/16) and host
257 * wildcards (e.g. *.example.org).
259 if (solaris_hostspec[0] == '@') {
261 * Solaris host specifier, e.g. @192.168.0.0/16; we just need
262 * to skip the @ in this case
264 *plinux_hostspec = strdup(solaris_hostspec + 1);
266 *plinux_hostspec = strdup(solaris_hostspec);
269 if (*plinux_hostspec == NULL) {
270 return (SA_NO_MEMORY);
277 * Adds a Linux share option to an array of NFS options.
280 add_linux_shareopt(char **plinux_opts, const char *key, const char *value)
283 char *new_linux_opts;
285 if (*plinux_opts != NULL)
286 len = strlen(*plinux_opts);
288 new_linux_opts = realloc(*plinux_opts, len + 1 + strlen(key) +
289 (value ? 1 + strlen(value) : 0) + 1);
291 if (new_linux_opts == NULL)
292 return (SA_NO_MEMORY);
294 new_linux_opts[len] = '\0';
297 strcat(new_linux_opts, ",");
299 strcat(new_linux_opts, key);
302 strcat(new_linux_opts, "=");
303 strcat(new_linux_opts, value);
306 *plinux_opts = new_linux_opts;
312 * Validates and converts a single Solaris share option to its Linux
316 get_linux_shareopts_cb(const char *key, const char *value, void *cookie)
318 char **plinux_opts = (char **)cookie;
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)
325 if (strcmp(key, "anon") == 0)
328 if (strcmp(key, "root_mapping") == 0) {
329 (void) add_linux_shareopt(plinux_opts, "root_squash", NULL);
333 if (strcmp(key, "nosub") == 0)
334 key = "subtree_check";
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);
357 (void) add_linux_shareopt(plinux_opts, key, value);
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.
367 get_linux_shareopts(const char *shareopts, char **plinux_opts)
371 assert(plinux_opts != NULL);
375 /* no_subtree_check - Default as of nfs-utils v1.1.0 */
376 (void) add_linux_shareopt(plinux_opts, "no_subtree_check", NULL);
378 /* mountpoint - Restrict exports to ZFS mountpoints */
379 (void) add_linux_shareopt(plinux_opts, "mountpoint", NULL);
381 error = foreach_nfs_shareopt(shareopts, get_linux_shareopts_cb,
384 if (error != SA_OK) {
393 nfs_init_tmpfile(void)
395 char *tmpfile = NULL;
398 if (stat(ZFS_EXPORTS_DIR, &sb) < 0 &&
399 mkdir(ZFS_EXPORTS_DIR, 0755) < 0) {
400 fprintf(stderr, "failed to create %s: %s\n",
401 ZFS_EXPORTS_DIR, strerror(errno));
405 if (asprintf(&tmpfile, "%s%s", ZFS_EXPORTS_FILE, ".XXXXXXXX") == -1) {
406 fprintf(stderr, "Unable to allocate temporary file\n");
410 int fd = mkstemp(tmpfile);
412 fprintf(stderr, "Unable to create temporary file: %s",
422 nfs_fini_tmpfile(char *tmpfile)
424 if (rename(tmpfile, ZFS_EXPORTS_FILE) == -1) {
425 fprintf(stderr, "Unable to rename %s: %s\n", tmpfile,
429 return (SA_SYSTEM_ERR);
436 * This function populates an entry into /etc/exports.d/zfs.exports.
437 * This file is consumed by the linux nfs server so that zfs shares are
438 * automatically exported upon boot or whenever the nfs server restarts.
441 nfs_add_entry(const char *filename, const char *sharepath,
442 const char *host, const char *security, const char *access_opts,
447 const char *linux_opts = (const char *)pcookie;
449 error = get_linux_hostspec(host, &linuxhost);
453 if (linux_opts == NULL)
456 FILE *fp = fopen(filename, "a+");
458 fprintf(stderr, "failed to open %s file: %s", filename,
461 return (SA_SYSTEM_ERR);
464 if (fprintf(fp, "%s %s(sec=%s,%s,%s)\n", sharepath, linuxhost,
465 security, access_opts, linux_opts) < 0) {
466 fprintf(stderr, "failed to write to %s\n", filename);
469 return (SA_SYSTEM_ERR);
473 if (fclose(fp) != 0) {
474 fprintf(stderr, "Unable to close file %s: %s\n",
475 filename, strerror(errno));
476 return (SA_SYSTEM_ERR);
482 * This function copies all entries from the exports file to "filename",
483 * omitting any entries for the specified mountpoint.
486 nfs_copy_entries(char *filename, const char *mountpoint)
492 FILE *oldfp = fopen(ZFS_EXPORTS_FILE, "r");
493 FILE *newfp = fopen(filename, "w+");
495 fprintf(stderr, "failed to open %s file: %s", filename,
498 return (SA_SYSTEM_ERR);
500 fputs(FILE_HEADER, newfp);
503 * The ZFS_EXPORTS_FILE may not exist yet. If that's the
504 * case then just write out the new file.
507 while (getline(&buf, &buflen, oldfp) != -1) {
510 if (buf[0] == '\n' || buf[0] == '#')
513 if ((space = strchr(buf, ' ')) != NULL) {
514 int mountpoint_len = strlen(mountpoint);
516 if (space - buf == mountpoint_len &&
517 strncmp(mountpoint, buf,
518 mountpoint_len) == 0) {
525 if (ferror(oldfp) != 0) {
526 error = ferror(oldfp);
528 if (fclose(oldfp) != 0) {
529 fprintf(stderr, "Unable to close file %s: %s\n",
530 filename, strerror(errno));
531 error = error != 0 ? error : SA_SYSTEM_ERR;
535 if (error == 0 && ferror(newfp) != 0) {
536 error = ferror(newfp);
540 if (fclose(newfp) != 0) {
541 fprintf(stderr, "Unable to close file %s: %s\n",
542 filename, strerror(errno));
543 error = error != 0 ? error : SA_SYSTEM_ERR;
549 * Enables NFS sharing for the specified share.
552 nfs_enable_share(sa_share_impl_t impl_share)
554 char *shareopts, *linux_opts;
555 char *filename = NULL;
558 if ((filename = nfs_init_tmpfile()) == NULL)
559 return (SA_SYSTEM_ERR);
561 error = nfs_exports_lock();
568 error = nfs_copy_entries(filename, impl_share->sa_mountpoint);
569 if (error != SA_OK) {
572 nfs_exports_unlock();
576 shareopts = FSINFO(impl_share, nfs_fstype)->shareopts;
577 error = get_linux_shareopts(shareopts, &linux_opts);
578 if (error != SA_OK) {
581 nfs_exports_unlock();
585 error = foreach_nfs_host(impl_share, filename, nfs_add_entry,
589 error = nfs_fini_tmpfile(filename);
594 nfs_exports_unlock();
599 * Disables NFS sharing for the specified share.
602 nfs_disable_share(sa_share_impl_t impl_share)
605 char *filename = NULL;
607 if ((filename = nfs_init_tmpfile()) == NULL)
608 return (SA_SYSTEM_ERR);
610 error = nfs_exports_lock();
617 error = nfs_copy_entries(filename, impl_share->sa_mountpoint);
618 if (error != SA_OK) {
621 nfs_exports_unlock();
624 error = nfs_fini_tmpfile(filename);
625 nfs_exports_unlock();
630 nfs_is_shared(sa_share_impl_t impl_share)
635 FILE *fp = fopen(ZFS_EXPORTS_FILE, "r");
639 while ((getline(&buf, &buflen, fp)) != -1) {
642 if ((space = strchr(buf, ' ')) != NULL) {
643 int mountpoint_len = strlen(impl_share->sa_mountpoint);
645 if (space - buf == mountpoint_len &&
646 strncmp(impl_share->sa_mountpoint, buf,
647 mountpoint_len) == 0) {
660 * Checks whether the specified NFS share options are syntactically correct.
663 nfs_validate_shareopts(const char *shareopts)
668 error = get_linux_shareopts(shareopts, &linux_opts);
678 nfs_update_shareopts(sa_share_impl_t impl_share, const char *shareopts)
680 FSINFO(impl_share, nfs_fstype)->shareopts = (char *)shareopts;
685 * Clears a share's NFS options. Used by libshare to
686 * clean up shares that are about to be free()'d.
689 nfs_clear_shareopts(sa_share_impl_t impl_share)
691 FSINFO(impl_share, nfs_fstype)->shareopts = NULL;
695 nfs_commit_shares(void)
698 "/usr/sbin/exportfs",
703 return (libzfs_run_process(argv[0], argv, 0));
706 static const sa_share_ops_t nfs_shareops = {
707 .enable_share = nfs_enable_share,
708 .disable_share = nfs_disable_share,
709 .is_shared = nfs_is_shared,
711 .validate_shareopts = nfs_validate_shareopts,
712 .update_shareopts = nfs_update_shareopts,
713 .clear_shareopts = nfs_clear_shareopts,
714 .commit_shares = nfs_commit_shares,
718 * Initializes the NFS functionality of libshare.
721 libshare_nfs_init(void)
723 nfs_fstype = register_fstype("nfs", &nfs_shareops);