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.
37 #include "libshare_impl.h"
39 static boolean_t nfs_available(void);
41 static sa_fstype_t *nfs_fstype;
44 * nfs_exportfs_temp_fd refers to a temporary copy of the output
47 static int nfs_exportfs_temp_fd = -1;
49 typedef int (*nfs_shareopt_callback_t)(const char *opt, const char *value,
52 typedef int (*nfs_host_callback_t)(const char *sharepath, const char *host,
53 const char *security, const char *access, void *cookie);
56 * Invokes the specified callback function for each Solaris share option
57 * listed in the specified string.
60 foreach_nfs_shareopt(const char *shareopts,
61 nfs_shareopt_callback_t callback, void *cookie)
63 char *shareopts_dup, *opt, *cur, *value;
66 if (shareopts == NULL)
69 shareopts_dup = strdup(shareopts);
71 if (shareopts_dup == NULL)
72 return (SA_NO_MEMORY);
80 while (*cur != ',' && *cur != '\0')
89 value = strchr(opt, '=');
96 rc = callback(opt, value, cookie);
115 typedef struct nfs_host_cookie_s {
116 nfs_host_callback_t callback;
117 const char *sharepath;
119 const char *security;
123 * Helper function for foreach_nfs_host. This function checks whether the
124 * current share option is a host specification and invokes a callback
125 * function with information about the host.
128 foreach_nfs_host_cb(const char *opt, const char *value, void *pcookie)
132 char *host_dup, *host, *next;
133 nfs_host_cookie_t *udata = (nfs_host_cookie_t *)pcookie;
136 fprintf(stderr, "foreach_nfs_host_cb: key=%s, value=%s\n", opt, value);
139 if (strcmp(opt, "sec") == 0)
140 udata->security = value;
142 if (strcmp(opt, "rw") == 0 || strcmp(opt, "ro") == 0) {
148 host_dup = strdup(value);
150 if (host_dup == NULL)
151 return (SA_NO_MEMORY);
156 next = strchr(host, ':');
162 rc = udata->callback(udata->sharepath, host,
163 udata->security, access, udata->cookie);
172 } while (host != NULL);
181 * Invokes a callback function for all NFS hosts that are set for a share.
184 foreach_nfs_host(sa_share_impl_t impl_share, nfs_host_callback_t callback,
187 nfs_host_cookie_t udata;
190 udata.callback = callback;
191 udata.sharepath = impl_share->sharepath;
192 udata.cookie = cookie;
193 udata.security = "sys";
195 shareopts = FSINFO(impl_share, nfs_fstype)->shareopts;
197 return foreach_nfs_shareopt(shareopts, foreach_nfs_host_cb,
202 * Converts a Solaris NFS host specification to its Linux equivalent.
205 get_linux_hostspec(const char *solaris_hostspec, char **plinux_hostspec)
208 * For now we just support CIDR masks (e.g. @192.168.0.0/16) and host
209 * wildcards (e.g. *.example.org).
211 if (solaris_hostspec[0] == '@') {
213 * Solaris host specifier, e.g. @192.168.0.0/16; we just need
214 * to skip the @ in this case
216 *plinux_hostspec = strdup(solaris_hostspec + 1);
218 *plinux_hostspec = strdup(solaris_hostspec);
221 if (*plinux_hostspec == NULL) {
222 return (SA_NO_MEMORY);
229 * Used internally by nfs_enable_share to enable sharing for a single host.
232 nfs_enable_share_one(const char *sharepath, const char *host,
233 const char *security, const char *access, void *pcookie)
236 char *linuxhost, *hostpath, *opts;
237 const char *linux_opts = (const char *)pcookie;
240 /* exportfs -i -o sec=XX,rX,<opts> <host>:<sharepath> */
242 rc = get_linux_hostspec(host, &linuxhost);
247 hostpath = malloc(strlen(linuxhost) + 1 + strlen(sharepath) + 1);
249 if (hostpath == NULL) {
255 sprintf(hostpath, "%s:%s", linuxhost, sharepath);
259 if (linux_opts == NULL)
262 opts = malloc(4 + strlen(security) + 4 + strlen(linux_opts) + 1);
267 sprintf(opts, "sec=%s,%s,%s", security, access, linux_opts);
270 fprintf(stderr, "sharing %s with opts %s\n", hostpath, opts);
273 argv[0] = "/usr/sbin/exportfs";
280 rc = libzfs_run_process(argv[0], argv, 0);
286 return (SA_SYSTEM_ERR);
292 * Adds a Linux share option to an array of NFS options.
295 add_linux_shareopt(char **plinux_opts, const char *key, const char *value)
298 char *new_linux_opts;
300 if (*plinux_opts != NULL)
301 len = strlen(*plinux_opts);
303 new_linux_opts = realloc(*plinux_opts, len + 1 + strlen(key) +
304 (value ? 1 + strlen(value) : 0) + 1);
306 if (new_linux_opts == NULL)
307 return (SA_NO_MEMORY);
309 new_linux_opts[len] = '\0';
312 strcat(new_linux_opts, ",");
314 strcat(new_linux_opts, key);
317 strcat(new_linux_opts, "=");
318 strcat(new_linux_opts, value);
321 *plinux_opts = new_linux_opts;
327 * Validates and converts a single Solaris share option to its Linux
331 get_linux_shareopts_cb(const char *key, const char *value, void *cookie)
333 char **plinux_opts = (char **)cookie;
335 /* host-specific options, these are taken care of elsewhere */
336 if (strcmp(key, "ro") == 0 || strcmp(key, "rw") == 0 ||
337 strcmp(key, "sec") == 0)
340 if (strcmp(key, "anon") == 0)
343 if (strcmp(key, "root_mapping") == 0) {
344 (void) add_linux_shareopt(plinux_opts, "root_squash", NULL);
348 if (strcmp(key, "nosub") == 0)
349 key = "subtree_check";
351 if (strcmp(key, "insecure") != 0 && strcmp(key, "secure") != 0 &&
352 strcmp(key, "async") != 0 && strcmp(key, "sync") != 0 &&
353 strcmp(key, "no_wdelay") != 0 && strcmp(key, "wdelay") != 0 &&
354 strcmp(key, "nohide") != 0 && strcmp(key, "hide") != 0 &&
355 strcmp(key, "crossmnt") != 0 &&
356 strcmp(key, "no_subtree_check") != 0 &&
357 strcmp(key, "subtree_check") != 0 &&
358 strcmp(key, "insecure_locks") != 0 &&
359 strcmp(key, "secure_locks") != 0 &&
360 strcmp(key, "no_auth_nlm") != 0 && strcmp(key, "auth_nlm") != 0 &&
361 strcmp(key, "no_acl") != 0 && strcmp(key, "mountpoint") != 0 &&
362 strcmp(key, "mp") != 0 && strcmp(key, "fsuid") != 0 &&
363 strcmp(key, "refer") != 0 && strcmp(key, "replicas") != 0 &&
364 strcmp(key, "root_squash") != 0 &&
365 strcmp(key, "no_root_squash") != 0 &&
366 strcmp(key, "all_squash") != 0 &&
367 strcmp(key, "no_all_squash") != 0 && strcmp(key, "fsid") != 0 &&
368 strcmp(key, "anonuid") != 0 && strcmp(key, "anongid") != 0) {
369 return (SA_SYNTAX_ERR);
372 (void) add_linux_shareopt(plinux_opts, key, value);
378 * Takes a string containing Solaris share options (e.g. "sync,no_acl") and
379 * converts them to a NULL-terminated array of Linux NFS options.
382 get_linux_shareopts(const char *shareopts, char **plinux_opts)
386 assert(plinux_opts != NULL);
390 /* no_subtree_check - Default as of nfs-utils v1.1.0 */
391 (void) add_linux_shareopt(plinux_opts, "no_subtree_check", NULL);
393 /* mountpoint - Restrict exports to ZFS mountpoints */
394 (void) add_linux_shareopt(plinux_opts, "mountpoint", NULL);
396 rc = foreach_nfs_shareopt(shareopts, get_linux_shareopts_cb,
408 * Enables NFS sharing for the specified share.
411 nfs_enable_share(sa_share_impl_t impl_share)
413 char *shareopts, *linux_opts;
416 if (!nfs_available()) {
417 return (SA_SYSTEM_ERR);
420 shareopts = FSINFO(impl_share, nfs_fstype)->shareopts;
422 if (shareopts == NULL)
425 rc = get_linux_shareopts(shareopts, &linux_opts);
430 rc = foreach_nfs_host(impl_share, nfs_enable_share_one, linux_opts);
438 * Used internally by nfs_disable_share to disable sharing for a single host.
441 nfs_disable_share_one(const char *sharepath, const char *host,
442 const char *security, const char *access, void *cookie)
445 char *linuxhost, *hostpath;
448 rc = get_linux_hostspec(host, &linuxhost);
453 hostpath = malloc(strlen(linuxhost) + 1 + strlen(sharepath) + 1);
455 if (hostpath == NULL) {
460 sprintf(hostpath, "%s:%s", linuxhost, sharepath);
465 fprintf(stderr, "unsharing %s\n", hostpath);
468 argv[0] = "/usr/sbin/exportfs";
473 rc = libzfs_run_process(argv[0], argv, 0);
478 return (SA_SYSTEM_ERR);
484 * Disables NFS sharing for the specified share.
487 nfs_disable_share(sa_share_impl_t impl_share)
489 if (!nfs_available()) {
491 * The share can't possibly be active, so nothing
492 * needs to be done to disable it.
497 return (foreach_nfs_host(impl_share, nfs_disable_share_one, NULL));
501 * Checks whether the specified NFS share options are syntactically correct.
504 nfs_validate_shareopts(const char *shareopts)
509 rc = get_linux_shareopts(shareopts, &linux_opts);
520 * Checks whether a share is currently active.
523 nfs_is_share_active(sa_share_impl_t impl_share)
528 FILE *nfs_exportfs_temp_fp;
530 if (!nfs_available())
533 if ((fd = dup(nfs_exportfs_temp_fd)) == -1)
536 nfs_exportfs_temp_fp = fdopen(fd, "r");
538 if (nfs_exportfs_temp_fp == NULL)
541 if (fseek(nfs_exportfs_temp_fp, 0, SEEK_SET) < 0) {
542 fclose(nfs_exportfs_temp_fp);
546 while (fgets(line, sizeof (line), nfs_exportfs_temp_fp) != NULL) {
548 * exportfs uses separate lines for the share path
549 * and the export options when the share path is longer
550 * than a certain amount of characters; this ignores
556 tab = strchr(line, '\t');
563 * there's no tab character, which means the
564 * NFS options are on a separate line; we just
565 * need to remove the new-line character
566 * at the end of the line
568 cur = line + strlen(line) - 1;
571 /* remove trailing spaces and new-line characters */
572 while (cur >= line && (*cur == ' ' || *cur == '\n'))
575 if (strcmp(line, impl_share->sharepath) == 0) {
576 fclose(nfs_exportfs_temp_fp);
581 fclose(nfs_exportfs_temp_fp);
587 * Called to update a share's options. A share's options might be out of
588 * date if the share was loaded from disk (i.e. /etc/dfs/sharetab) and the
589 * "sharenfs" dataset property has changed in the meantime. This function
590 * also takes care of re-enabling the share if necessary.
593 nfs_update_shareopts(sa_share_impl_t impl_share, const char *resource,
594 const char *shareopts)
597 boolean_t needs_reshare = B_FALSE;
600 FSINFO(impl_share, nfs_fstype)->active =
601 nfs_is_share_active(impl_share);
603 old_shareopts = FSINFO(impl_share, nfs_fstype)->shareopts;
605 if (strcmp(shareopts, "on") == 0)
606 shareopts = "rw,crossmnt";
608 if (FSINFO(impl_share, nfs_fstype)->active && old_shareopts != NULL &&
609 strcmp(old_shareopts, shareopts) != 0) {
610 needs_reshare = B_TRUE;
611 nfs_disable_share(impl_share);
614 shareopts_dup = strdup(shareopts);
616 if (shareopts_dup == NULL)
617 return (SA_NO_MEMORY);
619 if (old_shareopts != NULL)
622 FSINFO(impl_share, nfs_fstype)->shareopts = shareopts_dup;
625 nfs_enable_share(impl_share);
631 * Clears a share's NFS options. Used by libshare to
632 * clean up shares that are about to be free()'d.
635 nfs_clear_shareopts(sa_share_impl_t impl_share)
637 free(FSINFO(impl_share, nfs_fstype)->shareopts);
638 FSINFO(impl_share, nfs_fstype)->shareopts = NULL;
641 static const sa_share_ops_t nfs_shareops = {
642 .enable_share = nfs_enable_share,
643 .disable_share = nfs_disable_share,
645 .validate_shareopts = nfs_validate_shareopts,
646 .update_shareopts = nfs_update_shareopts,
647 .clear_shareopts = nfs_clear_shareopts,
651 * nfs_check_exportfs() checks that the exportfs command runs
652 * and also maintains a temporary copy of the output from
654 * To update this temporary copy simply call this function again.
656 * TODO : Use /var/lib/nfs/etab instead of our private copy.
657 * But must implement locking to prevent concurrent access.
659 * TODO : The temporary file descriptor is never closed since
660 * there is no libshare_nfs_fini() function.
663 nfs_check_exportfs(void)
667 static char nfs_exportfs_tempfile[] = "/tmp/exportfs.XXXXXX";
670 * Close any existing temporary copies of output from exportfs.
671 * We have already called unlink() so file will be deleted.
673 if (nfs_exportfs_temp_fd >= 0)
674 close(nfs_exportfs_temp_fd);
676 nfs_exportfs_temp_fd = mkstemp(nfs_exportfs_tempfile);
678 if (nfs_exportfs_temp_fd < 0)
679 return (SA_SYSTEM_ERR);
681 unlink(nfs_exportfs_tempfile);
683 (void) fcntl(nfs_exportfs_temp_fd, F_SETFD, FD_CLOEXEC);
688 (void) close(nfs_exportfs_temp_fd);
689 nfs_exportfs_temp_fd = -1;
690 return (SA_SYSTEM_ERR);
694 while ((rc = waitpid(pid, &status, 0)) <= 0 &&
698 (void) close(nfs_exportfs_temp_fd);
699 nfs_exportfs_temp_fd = -1;
700 return (SA_SYSTEM_ERR);
703 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
704 (void) close(nfs_exportfs_temp_fd);
705 nfs_exportfs_temp_fd = -1;
706 return (SA_CONFIG_ERR);
716 if (dup2(nfs_exportfs_temp_fd, STDOUT_FILENO) < 0)
719 rc = execlp("/usr/sbin/exportfs", "exportfs", "-v", NULL);
729 * Provides a convenient wrapper for determining nfs availability
734 if (nfs_exportfs_temp_fd == -1)
735 (void) nfs_check_exportfs();
737 return ((nfs_exportfs_temp_fd != -1) ? B_TRUE : B_FALSE);
741 * Initializes the NFS functionality of libshare.
744 libshare_nfs_init(void)
746 nfs_fstype = register_fstype("nfs", &nfs_shareops);