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
34 #include "libshare_impl.h"
36 static sa_fstype_t *nfs_fstype;
37 static boolean_t nfs_available;
40 * nfs_exportfs_temp_fd refers to a temporary copy of the output
43 static int nfs_exportfs_temp_fd = -1;
45 typedef int (*nfs_shareopt_callback_t)(const char *opt, const char *value,
48 typedef int (*nfs_host_callback_t)(const char *sharepath, const char *host,
49 const char *security, const char *access, void *cookie);
52 * Invokes the specified callback function for each Solaris share option
53 * listed in the specified string.
56 foreach_nfs_shareopt(const char *shareopts,
57 nfs_shareopt_callback_t callback, void *cookie)
59 char *shareopts_dup, *opt, *cur, *value;
62 if (shareopts == NULL)
65 shareopts_dup = strdup(shareopts);
67 if (shareopts_dup == NULL)
76 while (*cur != ',' && *cur != '\0')
85 value = strchr(opt, '=');
92 rc = callback(opt, value, cookie);
111 typedef struct nfs_host_cookie_s {
112 nfs_host_callback_t callback;
113 const char *sharepath;
115 const char *security;
119 * Helper function for foreach_nfs_host. This function checks whether the
120 * current share option is a host specification and invokes a callback
121 * function with information about the host.
124 foreach_nfs_host_cb(const char *opt, const char *value, void *pcookie)
128 char *host_dup, *host, *next;
129 nfs_host_cookie_t *udata = (nfs_host_cookie_t *)pcookie;
132 fprintf(stderr, "foreach_nfs_host_cb: key=%s, value=%s\n", opt, value);
135 if (strcmp(opt, "sec") == 0)
136 udata->security = value;
138 if (strcmp(opt, "rw") == 0 || strcmp(opt, "ro") == 0) {
144 host_dup = strdup(value);
146 if (host_dup == NULL)
152 next = strchr(host, ':');
158 rc = udata->callback(udata->sharepath, host,
159 udata->security, access, udata->cookie);
168 } while (host != NULL);
177 * Invokes a callback function for all NFS hosts that are set for a share.
180 foreach_nfs_host(sa_share_impl_t impl_share, nfs_host_callback_t callback,
183 nfs_host_cookie_t udata;
186 udata.callback = callback;
187 udata.sharepath = impl_share->sharepath;
188 udata.cookie = cookie;
189 udata.security = "sys";
191 shareopts = FSINFO(impl_share, nfs_fstype)->shareopts;
193 return foreach_nfs_shareopt(shareopts, foreach_nfs_host_cb,
198 * Converts a Solaris NFS host specification to its Linux equivalent.
201 get_linux_hostspec(const char *solaris_hostspec, char **plinux_hostspec)
204 * For now we just support CIDR masks (e.g. @192.168.0.0/16) and host
205 * wildcards (e.g. *.example.org).
207 if (solaris_hostspec[0] == '@') {
209 * Solaris host specifier, e.g. @192.168.0.0/16; we just need
210 * to skip the @ in this case
212 *plinux_hostspec = strdup(solaris_hostspec + 1);
214 *plinux_hostspec = strdup(solaris_hostspec);
217 if (*plinux_hostspec == NULL) {
225 * Used internally by nfs_enable_share to enable sharing for a single host.
228 nfs_enable_share_one(const char *sharepath, const char *host,
229 const char *security, const char *access, void *pcookie)
232 char *linuxhost, *hostpath, *opts;
233 const char *linux_opts = (const char *)pcookie;
236 /* exportfs -i -o sec=XX,rX,<opts> <host>:<sharepath> */
238 rc = get_linux_hostspec(host, &linuxhost);
243 hostpath = malloc(strlen(linuxhost) + 1 + strlen(sharepath) + 1);
245 if (hostpath == NULL) {
251 sprintf(hostpath, "%s:%s", linuxhost, sharepath);
255 if (linux_opts == NULL)
258 opts = malloc(4 + strlen(security) + 4 + strlen(linux_opts) + 1);
263 sprintf(opts, "sec=%s,%s,%s", security, access, linux_opts);
266 fprintf(stderr, "sharing %s with opts %s\n", hostpath, opts);
269 argv[0] = "/usr/sbin/exportfs";
276 rc = libzfs_run_process(argv[0], argv, 0);
282 return SA_SYSTEM_ERR;
288 * Adds a Linux share option to an array of NFS options.
291 add_linux_shareopt(char **plinux_opts, const char *key, const char *value)
294 char *new_linux_opts;
296 if (*plinux_opts != NULL)
297 len = strlen(*plinux_opts);
299 new_linux_opts = realloc(*plinux_opts, len + 1 + strlen(key) +
300 (value ? 1 + strlen(value) : 0) + 1);
302 if (new_linux_opts == NULL)
305 new_linux_opts[len] = '\0';
308 strcat(new_linux_opts, ",");
310 strcat(new_linux_opts, key);
313 strcat(new_linux_opts, "=");
314 strcat(new_linux_opts, value);
317 *plinux_opts = new_linux_opts;
323 * Validates and converts a single Solaris share option to its Linux
327 get_linux_shareopts_cb(const char *key, const char *value, void *cookie)
329 char **plinux_opts = (char **)cookie;
331 /* host-specific options, these are taken care of elsewhere */
332 if (strcmp(key, "ro") == 0 || strcmp(key, "rw") == 0 ||
333 strcmp(key, "sec") == 0)
336 if (strcmp(key, "anon") == 0)
339 if (strcmp(key, "root_mapping") == 0) {
340 (void) add_linux_shareopt(plinux_opts, "root_squash", NULL);
344 if (strcmp(key, "nosub") == 0)
345 key = "subtree_check";
347 if (strcmp(key, "insecure") != 0 && strcmp(key, "secure") != 0 &&
348 strcmp(key, "async") != 0 && strcmp(key, "sync") != 0 &&
349 strcmp(key, "no_wdelay") != 0 && strcmp(key, "wdelay") != 0 &&
350 strcmp(key, "nohide") != 0 && strcmp(key, "hide") != 0 &&
351 strcmp(key, "crossmnt") != 0 &&
352 strcmp(key, "no_subtree_check") != 0 &&
353 strcmp(key, "subtree_check") != 0 &&
354 strcmp(key, "insecure_locks") != 0 &&
355 strcmp(key, "secure_locks") != 0 &&
356 strcmp(key, "no_auth_nlm") != 0 && strcmp(key, "auth_nlm") != 0 &&
357 strcmp(key, "no_acl") != 0 && strcmp(key, "mountpoint") != 0 &&
358 strcmp(key, "mp") != 0 && strcmp(key, "fsuid") != 0 &&
359 strcmp(key, "refer") != 0 && strcmp(key, "replicas") != 0 &&
360 strcmp(key, "root_squash") != 0 &&
361 strcmp(key, "no_root_squash") != 0 &&
362 strcmp(key, "all_squash") != 0 &&
363 strcmp(key, "no_all_squash") != 0 && strcmp(key, "fsid") != 0 &&
364 strcmp(key, "anonuid") != 0 && strcmp(key, "anongid") != 0) {
365 return SA_SYNTAX_ERR;
368 (void) add_linux_shareopt(plinux_opts, key, value);
374 * Takes a string containing Solaris share options (e.g. "sync,no_acl") and
375 * converts them to a NULL-terminated array of Linux NFS options.
378 get_linux_shareopts(const char *shareopts, char **plinux_opts)
382 assert(plinux_opts != NULL);
386 /* default options for Solaris shares */
387 (void) add_linux_shareopt(plinux_opts, "no_subtree_check", NULL);
388 (void) add_linux_shareopt(plinux_opts, "no_root_squash", NULL);
389 (void) add_linux_shareopt(plinux_opts, "mountpoint", NULL);
391 rc = foreach_nfs_shareopt(shareopts, get_linux_shareopts_cb, plinux_opts);
402 * Enables NFS sharing for the specified share.
405 nfs_enable_share(sa_share_impl_t impl_share)
407 char *shareopts, *linux_opts;
410 if (!nfs_available) {
411 return SA_SYSTEM_ERR;
414 shareopts = FSINFO(impl_share, nfs_fstype)->shareopts;
416 if (shareopts == NULL)
419 rc = get_linux_shareopts(shareopts, &linux_opts);
424 rc = foreach_nfs_host(impl_share, nfs_enable_share_one, linux_opts);
432 * Used internally by nfs_disable_share to disable sharing for a single host.
435 nfs_disable_share_one(const char *sharepath, const char *host,
436 const char *security, const char *access, void *cookie)
439 char *linuxhost, *hostpath;
442 rc = get_linux_hostspec(host, &linuxhost);
447 hostpath = malloc(strlen(linuxhost) + 1 + strlen(sharepath) + 1);
449 if (hostpath == NULL) {
454 sprintf(hostpath, "%s:%s", linuxhost, sharepath);
459 fprintf(stderr, "unsharing %s\n", hostpath);
462 argv[0] = "/usr/sbin/exportfs";
467 rc = libzfs_run_process(argv[0], argv, 0);
472 return SA_SYSTEM_ERR;
478 * Disables NFS sharing for the specified share.
481 nfs_disable_share(sa_share_impl_t impl_share)
483 if (!nfs_available) {
485 * The share can't possibly be active, so nothing
486 * needs to be done to disable it.
491 return foreach_nfs_host(impl_share, nfs_disable_share_one, NULL);
495 * Checks whether the specified NFS share options are syntactically correct.
498 nfs_validate_shareopts(const char *shareopts)
503 rc = get_linux_shareopts(shareopts, &linux_opts);
514 * Checks whether a share is currently active.
517 is_share_active(sa_share_impl_t impl_share)
521 FILE *nfs_exportfs_temp_fp;
523 if (nfs_exportfs_temp_fd < 0)
526 nfs_exportfs_temp_fp = fdopen(dup(nfs_exportfs_temp_fd), "r");
528 if (nfs_exportfs_temp_fp == NULL ||
529 fseek(nfs_exportfs_temp_fp, 0, SEEK_SET) < 0) {
530 fclose(nfs_exportfs_temp_fp);
534 while (fgets(line, sizeof(line), nfs_exportfs_temp_fp) != NULL) {
536 * exportfs uses separate lines for the share path
537 * and the export options when the share path is longer
538 * than a certain amount of characters; this ignores
544 tab = strchr(line, '\t');
551 * there's no tab character, which means the
552 * NFS options are on a separate line; we just
553 * need to remove the new-line character
554 * at the end of the line
556 cur = line + strlen(line) - 1;
559 /* remove trailing spaces and new-line characters */
560 while (cur >= line && (*cur == ' ' || *cur == '\n'))
563 if (strcmp(line, impl_share->sharepath) == 0) {
564 fclose(nfs_exportfs_temp_fp);
569 fclose(nfs_exportfs_temp_fp);
575 * Called to update a share's options. A share's options might be out of
576 * date if the share was loaded from disk (i.e. /etc/dfs/sharetab) and the
577 * "sharenfs" dataset property has changed in the meantime. This function
578 * also takes care of re-enabling the share if necessary.
581 nfs_update_shareopts(sa_share_impl_t impl_share, const char *resource,
582 const char *shareopts)
585 boolean_t needs_reshare = B_FALSE;
588 FSINFO(impl_share, nfs_fstype)->active = is_share_active(impl_share);
590 old_shareopts = FSINFO(impl_share, nfs_fstype)->shareopts;
592 if (strcmp(shareopts, "on") == 0)
595 if (FSINFO(impl_share, nfs_fstype)->active && old_shareopts != NULL &&
596 strcmp(old_shareopts, shareopts) != 0) {
597 needs_reshare = B_TRUE;
598 nfs_disable_share(impl_share);
601 shareopts_dup = strdup(shareopts);
603 if (shareopts_dup == NULL)
606 if (old_shareopts != NULL)
609 FSINFO(impl_share, nfs_fstype)->shareopts = shareopts_dup;
612 nfs_enable_share(impl_share);
618 * Clears a share's NFS options. Used by libshare to
619 * clean up shares that are about to be free()'d.
622 nfs_clear_shareopts(sa_share_impl_t impl_share)
624 free(FSINFO(impl_share, nfs_fstype)->shareopts);
625 FSINFO(impl_share, nfs_fstype)->shareopts = NULL;
628 static const sa_share_ops_t nfs_shareops = {
629 .enable_share = nfs_enable_share,
630 .disable_share = nfs_disable_share,
632 .validate_shareopts = nfs_validate_shareopts,
633 .update_shareopts = nfs_update_shareopts,
634 .clear_shareopts = nfs_clear_shareopts,
638 * nfs_check_exportfs() checks that the exportfs command runs
639 * and also maintains a temporary copy of the output from
641 * To update this temporary copy simply call this function again.
643 * TODO : Use /var/lib/nfs/etab instead of our private copy.
644 * But must implement locking to prevent concurrent access.
646 * TODO : The temporary file descriptor is never closed since
647 * there is no libshare_nfs_fini() function.
650 nfs_check_exportfs(void)
654 static char nfs_exportfs_tempfile[] = "/tmp/exportfs.XXXXXX";
657 * Close any existing temporary copies of output from exportfs.
658 * We have already called unlink() so file will be deleted.
660 if (nfs_exportfs_temp_fd >= 0)
661 close(nfs_exportfs_temp_fd);
663 nfs_exportfs_temp_fd = mkstemp(nfs_exportfs_tempfile);
665 if (nfs_exportfs_temp_fd < 0)
666 return SA_SYSTEM_ERR;
668 unlink(nfs_exportfs_tempfile);
670 fcntl(nfs_exportfs_temp_fd, F_SETFD, FD_CLOEXEC);
675 return SA_SYSTEM_ERR;
678 while ((rc = waitpid(pid, &status, 0)) <= 0 && errno == EINTR)
679 ; /* empty loop body */
682 return SA_SYSTEM_ERR;
684 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
685 return SA_CONFIG_ERR;
694 if (dup2(nfs_exportfs_temp_fd, STDOUT_FILENO) < 0)
697 rc = execlp("/usr/sbin/exportfs", "exportfs", "-v", NULL);
707 * Initializes the NFS functionality of libshare.
710 libshare_nfs_init(void)
712 nfs_available = (nfs_check_exportfs() == SA_OK);
714 nfs_fstype = register_fstype("nfs", &nfs_shareops);