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.
35 #include "libshare_impl.h"
37 static boolean_t nfs_available(void);
39 static sa_fstype_t *nfs_fstype;
42 * nfs_exportfs_temp_fd refers to a temporary copy of the output
45 static int nfs_exportfs_temp_fd = -1;
47 typedef int (*nfs_shareopt_callback_t)(const char *opt, const char *value,
50 typedef int (*nfs_host_callback_t)(const char *sharepath, const char *host,
51 const char *security, const char *access, void *cookie);
54 * Invokes the specified callback function for each Solaris share option
55 * listed in the specified string.
58 foreach_nfs_shareopt(const char *shareopts,
59 nfs_shareopt_callback_t callback, void *cookie)
61 char *shareopts_dup, *opt, *cur, *value;
64 if (shareopts == NULL)
67 shareopts_dup = strdup(shareopts);
69 if (shareopts_dup == NULL)
78 while (*cur != ',' && *cur != '\0')
87 value = strchr(opt, '=');
94 rc = callback(opt, value, cookie);
113 typedef struct nfs_host_cookie_s {
114 nfs_host_callback_t callback;
115 const char *sharepath;
117 const char *security;
121 * Helper function for foreach_nfs_host. This function checks whether the
122 * current share option is a host specification and invokes a callback
123 * function with information about the host.
126 foreach_nfs_host_cb(const char *opt, const char *value, void *pcookie)
130 char *host_dup, *host, *next;
131 nfs_host_cookie_t *udata = (nfs_host_cookie_t *)pcookie;
134 fprintf(stderr, "foreach_nfs_host_cb: key=%s, value=%s\n", opt, value);
137 if (strcmp(opt, "sec") == 0)
138 udata->security = value;
140 if (strcmp(opt, "rw") == 0 || strcmp(opt, "ro") == 0) {
146 host_dup = strdup(value);
148 if (host_dup == NULL)
154 next = strchr(host, ':');
160 rc = udata->callback(udata->sharepath, host,
161 udata->security, access, udata->cookie);
170 } while (host != NULL);
179 * Invokes a callback function for all NFS hosts that are set for a share.
182 foreach_nfs_host(sa_share_impl_t impl_share, nfs_host_callback_t callback,
185 nfs_host_cookie_t udata;
188 udata.callback = callback;
189 udata.sharepath = impl_share->sharepath;
190 udata.cookie = cookie;
191 udata.security = "sys";
193 shareopts = FSINFO(impl_share, nfs_fstype)->shareopts;
195 return foreach_nfs_shareopt(shareopts, foreach_nfs_host_cb,
200 * Converts a Solaris NFS host specification to its Linux equivalent.
203 get_linux_hostspec(const char *solaris_hostspec, char **plinux_hostspec)
206 * For now we just support CIDR masks (e.g. @192.168.0.0/16) and host
207 * wildcards (e.g. *.example.org).
209 if (solaris_hostspec[0] == '@') {
211 * Solaris host specifier, e.g. @192.168.0.0/16; we just need
212 * to skip the @ in this case
214 *plinux_hostspec = strdup(solaris_hostspec + 1);
216 *plinux_hostspec = strdup(solaris_hostspec);
219 if (*plinux_hostspec == NULL) {
227 * Used internally by nfs_enable_share to enable sharing for a single host.
230 nfs_enable_share_one(const char *sharepath, const char *host,
231 const char *security, const char *access, void *pcookie)
234 char *linuxhost, *hostpath, *opts;
235 const char *linux_opts = (const char *)pcookie;
238 /* exportfs -i -o sec=XX,rX,<opts> <host>:<sharepath> */
240 rc = get_linux_hostspec(host, &linuxhost);
245 hostpath = malloc(strlen(linuxhost) + 1 + strlen(sharepath) + 1);
247 if (hostpath == NULL) {
253 sprintf(hostpath, "%s:%s", linuxhost, sharepath);
257 if (linux_opts == NULL)
260 opts = malloc(4 + strlen(security) + 4 + strlen(linux_opts) + 1);
265 sprintf(opts, "sec=%s,%s,%s", security, access, linux_opts);
268 fprintf(stderr, "sharing %s with opts %s\n", hostpath, opts);
271 argv[0] = "/usr/sbin/exportfs";
278 rc = libzfs_run_process(argv[0], argv, 0);
284 return SA_SYSTEM_ERR;
290 * Adds a Linux share option to an array of NFS options.
293 add_linux_shareopt(char **plinux_opts, const char *key, const char *value)
296 char *new_linux_opts;
298 if (*plinux_opts != NULL)
299 len = strlen(*plinux_opts);
301 new_linux_opts = realloc(*plinux_opts, len + 1 + strlen(key) +
302 (value ? 1 + strlen(value) : 0) + 1);
304 if (new_linux_opts == NULL)
307 new_linux_opts[len] = '\0';
310 strcat(new_linux_opts, ",");
312 strcat(new_linux_opts, key);
315 strcat(new_linux_opts, "=");
316 strcat(new_linux_opts, value);
319 *plinux_opts = new_linux_opts;
325 * Validates and converts a single Solaris share option to its Linux
329 get_linux_shareopts_cb(const char *key, const char *value, void *cookie)
331 char **plinux_opts = (char **)cookie;
333 /* host-specific options, these are taken care of elsewhere */
334 if (strcmp(key, "ro") == 0 || strcmp(key, "rw") == 0 ||
335 strcmp(key, "sec") == 0)
338 if (strcmp(key, "anon") == 0)
341 if (strcmp(key, "root_mapping") == 0) {
342 (void) add_linux_shareopt(plinux_opts, "root_squash", NULL);
346 if (strcmp(key, "nosub") == 0)
347 key = "subtree_check";
349 if (strcmp(key, "insecure") != 0 && strcmp(key, "secure") != 0 &&
350 strcmp(key, "async") != 0 && strcmp(key, "sync") != 0 &&
351 strcmp(key, "no_wdelay") != 0 && strcmp(key, "wdelay") != 0 &&
352 strcmp(key, "nohide") != 0 && strcmp(key, "hide") != 0 &&
353 strcmp(key, "crossmnt") != 0 &&
354 strcmp(key, "no_subtree_check") != 0 &&
355 strcmp(key, "subtree_check") != 0 &&
356 strcmp(key, "insecure_locks") != 0 &&
357 strcmp(key, "secure_locks") != 0 &&
358 strcmp(key, "no_auth_nlm") != 0 && strcmp(key, "auth_nlm") != 0 &&
359 strcmp(key, "no_acl") != 0 && strcmp(key, "mountpoint") != 0 &&
360 strcmp(key, "mp") != 0 && strcmp(key, "fsuid") != 0 &&
361 strcmp(key, "refer") != 0 && strcmp(key, "replicas") != 0 &&
362 strcmp(key, "root_squash") != 0 &&
363 strcmp(key, "no_root_squash") != 0 &&
364 strcmp(key, "all_squash") != 0 &&
365 strcmp(key, "no_all_squash") != 0 && strcmp(key, "fsid") != 0 &&
366 strcmp(key, "anonuid") != 0 && strcmp(key, "anongid") != 0) {
367 return SA_SYNTAX_ERR;
370 (void) add_linux_shareopt(plinux_opts, key, value);
376 * Takes a string containing Solaris share options (e.g. "sync,no_acl") and
377 * converts them to a NULL-terminated array of Linux NFS options.
380 get_linux_shareopts(const char *shareopts, char **plinux_opts)
384 assert(plinux_opts != NULL);
388 /* default options for Solaris shares */
389 (void) add_linux_shareopt(plinux_opts, "no_subtree_check", NULL);
390 (void) add_linux_shareopt(plinux_opts, "no_root_squash", NULL);
391 (void) add_linux_shareopt(plinux_opts, "mountpoint", NULL);
393 rc = foreach_nfs_shareopt(shareopts, get_linux_shareopts_cb, plinux_opts);
404 * Enables NFS sharing for the specified share.
407 nfs_enable_share(sa_share_impl_t impl_share)
409 char *shareopts, *linux_opts;
412 if (!nfs_available()) {
413 return SA_SYSTEM_ERR;
416 shareopts = FSINFO(impl_share, nfs_fstype)->shareopts;
418 if (shareopts == NULL)
421 rc = get_linux_shareopts(shareopts, &linux_opts);
426 rc = foreach_nfs_host(impl_share, nfs_enable_share_one, linux_opts);
434 * Used internally by nfs_disable_share to disable sharing for a single host.
437 nfs_disable_share_one(const char *sharepath, const char *host,
438 const char *security, const char *access, void *cookie)
441 char *linuxhost, *hostpath;
444 rc = get_linux_hostspec(host, &linuxhost);
449 hostpath = malloc(strlen(linuxhost) + 1 + strlen(sharepath) + 1);
451 if (hostpath == NULL) {
456 sprintf(hostpath, "%s:%s", linuxhost, sharepath);
461 fprintf(stderr, "unsharing %s\n", hostpath);
464 argv[0] = "/usr/sbin/exportfs";
469 rc = libzfs_run_process(argv[0], argv, 0);
474 return SA_SYSTEM_ERR;
480 * Disables NFS sharing for the specified share.
483 nfs_disable_share(sa_share_impl_t impl_share)
485 if (!nfs_available()) {
487 * The share can't possibly be active, so nothing
488 * needs to be done to disable it.
493 return foreach_nfs_host(impl_share, nfs_disable_share_one, NULL);
497 * Checks whether the specified NFS share options are syntactically correct.
500 nfs_validate_shareopts(const char *shareopts)
505 rc = get_linux_shareopts(shareopts, &linux_opts);
516 * Checks whether a share is currently active.
519 nfs_is_share_active(sa_share_impl_t impl_share)
523 FILE *nfs_exportfs_temp_fp;
525 if (!nfs_available())
528 nfs_exportfs_temp_fp = fdopen(dup(nfs_exportfs_temp_fd), "r");
530 if (nfs_exportfs_temp_fp == NULL ||
531 fseek(nfs_exportfs_temp_fp, 0, SEEK_SET) < 0) {
532 fclose(nfs_exportfs_temp_fp);
536 while (fgets(line, sizeof(line), nfs_exportfs_temp_fp) != NULL) {
538 * exportfs uses separate lines for the share path
539 * and the export options when the share path is longer
540 * than a certain amount of characters; this ignores
546 tab = strchr(line, '\t');
553 * there's no tab character, which means the
554 * NFS options are on a separate line; we just
555 * need to remove the new-line character
556 * at the end of the line
558 cur = line + strlen(line) - 1;
561 /* remove trailing spaces and new-line characters */
562 while (cur >= line && (*cur == ' ' || *cur == '\n'))
565 if (strcmp(line, impl_share->sharepath) == 0) {
566 fclose(nfs_exportfs_temp_fp);
571 fclose(nfs_exportfs_temp_fp);
577 * Called to update a share's options. A share's options might be out of
578 * date if the share was loaded from disk (i.e. /etc/dfs/sharetab) and the
579 * "sharenfs" dataset property has changed in the meantime. This function
580 * also takes care of re-enabling the share if necessary.
583 nfs_update_shareopts(sa_share_impl_t impl_share, const char *resource,
584 const char *shareopts)
587 boolean_t needs_reshare = B_FALSE;
590 FSINFO(impl_share, nfs_fstype)->active =
591 nfs_is_share_active(impl_share);
593 old_shareopts = FSINFO(impl_share, nfs_fstype)->shareopts;
595 if (strcmp(shareopts, "on") == 0)
598 if (FSINFO(impl_share, nfs_fstype)->active && old_shareopts != NULL &&
599 strcmp(old_shareopts, shareopts) != 0) {
600 needs_reshare = B_TRUE;
601 nfs_disable_share(impl_share);
604 shareopts_dup = strdup(shareopts);
606 if (shareopts_dup == NULL)
609 if (old_shareopts != NULL)
612 FSINFO(impl_share, nfs_fstype)->shareopts = shareopts_dup;
615 nfs_enable_share(impl_share);
621 * Clears a share's NFS options. Used by libshare to
622 * clean up shares that are about to be free()'d.
625 nfs_clear_shareopts(sa_share_impl_t impl_share)
627 free(FSINFO(impl_share, nfs_fstype)->shareopts);
628 FSINFO(impl_share, nfs_fstype)->shareopts = NULL;
631 static const sa_share_ops_t nfs_shareops = {
632 .enable_share = nfs_enable_share,
633 .disable_share = nfs_disable_share,
635 .validate_shareopts = nfs_validate_shareopts,
636 .update_shareopts = nfs_update_shareopts,
637 .clear_shareopts = nfs_clear_shareopts,
641 * nfs_check_exportfs() checks that the exportfs command runs
642 * and also maintains a temporary copy of the output from
644 * To update this temporary copy simply call this function again.
646 * TODO : Use /var/lib/nfs/etab instead of our private copy.
647 * But must implement locking to prevent concurrent access.
649 * TODO : The temporary file descriptor is never closed since
650 * there is no libshare_nfs_fini() function.
653 nfs_check_exportfs(void)
657 static char nfs_exportfs_tempfile[] = "/tmp/exportfs.XXXXXX";
660 * Close any existing temporary copies of output from exportfs.
661 * We have already called unlink() so file will be deleted.
663 if (nfs_exportfs_temp_fd >= 0)
664 close(nfs_exportfs_temp_fd);
666 nfs_exportfs_temp_fd = mkstemp(nfs_exportfs_tempfile);
668 if (nfs_exportfs_temp_fd < 0)
669 return SA_SYSTEM_ERR;
671 unlink(nfs_exportfs_tempfile);
673 fcntl(nfs_exportfs_temp_fd, F_SETFD, FD_CLOEXEC);
678 (void) close(nfs_exportfs_temp_fd);
679 nfs_exportfs_temp_fd = -1;
680 return SA_SYSTEM_ERR;
684 while ((rc = waitpid(pid, &status, 0)) <= 0 && errno == EINTR)
685 ; /* empty loop body */
688 (void) close(nfs_exportfs_temp_fd);
689 nfs_exportfs_temp_fd = -1;
690 return SA_SYSTEM_ERR;
693 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
694 (void) close(nfs_exportfs_temp_fd);
695 nfs_exportfs_temp_fd = -1;
696 return SA_CONFIG_ERR;
706 if (dup2(nfs_exportfs_temp_fd, STDOUT_FILENO) < 0)
709 rc = execlp("/usr/sbin/exportfs", "exportfs", "-v", NULL);
719 * Provides a convenient wrapper for determing nfs availability
724 if (nfs_exportfs_temp_fd == -1)
725 (void) nfs_check_exportfs();
727 return (nfs_exportfs_temp_fd != -1) ? B_TRUE : B_FALSE;
731 * Initializes the NFS functionality of libshare.
734 libshare_nfs_init(void)
736 nfs_fstype = register_fstype("nfs", &nfs_shareops);