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 foreach_nfs_shareopt(const char *shareopts,
53 nfs_shareopt_callback_t callback, void *cookie)
55 char *shareopts_dup, *opt, *cur, *value;
58 if (shareopts == NULL)
61 shareopts_dup = strdup(shareopts);
63 if (shareopts_dup == NULL)
72 while (*cur != ',' && *cur != '\0')
81 value = strchr(opt, '=');
88 rc = callback(opt, value, cookie);
107 typedef struct nfs_host_cookie_s {
108 nfs_host_callback_t callback;
109 const char *sharepath;
111 const char *security;
115 foreach_nfs_host_cb(const char *opt, const char *value, void *pcookie)
119 char *host_dup, *host, *next;
120 nfs_host_cookie_t *udata = (nfs_host_cookie_t *)pcookie;
123 fprintf(stderr, "foreach_nfs_host_cb: key=%s, value=%s\n", opt, value);
126 if (strcmp(opt, "sec") == 0)
127 udata->security = value;
129 if (strcmp(opt, "rw") == 0 || strcmp(opt, "ro") == 0) {
135 host_dup = strdup(value);
137 if (host_dup == NULL)
143 next = strchr(host, ':');
149 rc = udata->callback(udata->sharepath, host,
150 udata->security, access, udata->cookie);
159 } while (host != NULL);
168 foreach_nfs_host(sa_share_impl_t impl_share, nfs_host_callback_t callback,
171 nfs_host_cookie_t udata;
174 udata.callback = callback;
175 udata.sharepath = impl_share->sharepath;
176 udata.cookie = cookie;
177 udata.security = "sys";
179 shareopts = FSINFO(impl_share, nfs_fstype)->shareopts;
181 return foreach_nfs_shareopt(shareopts, foreach_nfs_host_cb,
186 get_linux_hostspec(const char *solaris_hostspec, char **plinux_hostspec)
189 * For now we just support CIDR masks (e.g. @192.168.0.0/16) and host
190 * wildcards (e.g. *.example.org).
192 if (solaris_hostspec[0] == '@') {
194 * Solaris host specifier, e.g. @192.168.0.0/16; we just need
195 * to skip the @ in this case
197 *plinux_hostspec = strdup(solaris_hostspec + 1);
199 *plinux_hostspec = strdup(solaris_hostspec);
202 if (*plinux_hostspec == NULL) {
210 nfs_enable_share_one(const char *sharepath, const char *host,
211 const char *security, const char *access, void *pcookie)
215 char *linuxhost, *hostpath, *opts;
216 const char *linux_opts = (const char *)pcookie;
221 return SA_SYSTEM_ERR;
224 while ((rc = waitpid(pid, &status, 0)) <= 0 && errno == EINTR)
225 ; /* empty loop body */
228 return SA_SYSTEM_ERR;
230 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
231 return SA_CONFIG_ERR;
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 rc = execlp("/usr/sbin/exportfs", "exportfs", "-i", \
272 "-o", opts, hostpath, NULL);
284 add_linux_shareopt(char **plinux_opts, const char *key, const char *value)
287 char *new_linux_opts;
289 if (*plinux_opts != NULL)
290 len = strlen(*plinux_opts);
292 new_linux_opts = realloc(*plinux_opts, len + 1 + strlen(key) +
293 (value ? 1 + strlen(value) : 0) + 1);
295 if (new_linux_opts == NULL)
298 new_linux_opts[len] = '\0';
301 strcat(new_linux_opts, ",");
303 strcat(new_linux_opts, key);
306 strcat(new_linux_opts, "=");
307 strcat(new_linux_opts, value);
310 *plinux_opts = new_linux_opts;
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 &&
353 strcmp(key, "anonuid") != 0 && strcmp(key, "anongid") != 0) {
354 return SA_SYNTAX_ERR;
357 (void) add_linux_shareopt(plinux_opts, key, value);
363 get_linux_shareopts(const char *shareopts, char **plinux_opts)
367 assert(plinux_opts != NULL);
371 /* default options for Solaris shares */
372 (void) add_linux_shareopt(plinux_opts, "no_subtree_check", NULL);
373 (void) add_linux_shareopt(plinux_opts, "no_root_squash", NULL);
374 (void) add_linux_shareopt(plinux_opts, "mountpoint", NULL);
376 rc = foreach_nfs_shareopt(shareopts, get_linux_shareopts_cb, plinux_opts);
387 nfs_enable_share(sa_share_impl_t impl_share)
389 char *shareopts, *linux_opts;
392 if (!nfs_available) {
393 return SA_SYSTEM_ERR;
396 shareopts = FSINFO(impl_share, nfs_fstype)->shareopts;
398 if (shareopts == NULL)
401 rc = get_linux_shareopts(shareopts, &linux_opts);
406 rc = foreach_nfs_host(impl_share, nfs_enable_share_one, linux_opts);
414 nfs_disable_share_one(const char *sharepath, const char *host,
415 const char *security, const char *access, void *cookie)
419 char *linuxhost, *hostpath;
424 return SA_SYSTEM_ERR;
427 while ((rc = waitpid(pid, &status, 0)) <= 0 && errno == EINTR)
428 ; /* empty loop body */
431 return SA_SYSTEM_ERR;
433 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
434 return SA_CONFIG_ERR;
441 rc = get_linux_hostspec(host, &linuxhost);
446 hostpath = malloc(strlen(linuxhost) + 1 + strlen(sharepath) + 1);
448 if (hostpath == NULL) {
453 sprintf(hostpath, "%s:%s", linuxhost, sharepath);
458 fprintf(stderr, "unsharing %s\n", hostpath);
461 rc = execlp("/usr/sbin/exportfs", "exportfs", "-u", \
473 nfs_disable_share(sa_share_impl_t impl_share)
475 if (!nfs_available) {
477 * The share can't possibly be active, so nothing
478 * needs to be done to disable it.
483 return foreach_nfs_host(impl_share, nfs_disable_share_one, NULL);
487 nfs_validate_shareopts(const char *shareopts)
492 rc = get_linux_shareopts(shareopts, &linux_opts);
503 is_share_active(sa_share_impl_t impl_share)
507 FILE *nfs_exportfs_temp_fp;
509 if (nfs_exportfs_temp_fd < 0)
512 nfs_exportfs_temp_fp = fdopen(dup(nfs_exportfs_temp_fd), "r");
514 if (nfs_exportfs_temp_fp == NULL ||
515 fseek(nfs_exportfs_temp_fp, 0, SEEK_SET) < 0) {
516 fclose(nfs_exportfs_temp_fp);
520 while (fgets(line, sizeof(line), nfs_exportfs_temp_fp) != NULL) {
522 * exportfs uses separate lines for the share path
523 * and the export options when the share path is longer
524 * than a certain amount of characters; this ignores
530 tab = strchr(line, '\t');
537 * there's no tab character, which means the
538 * NFS options are on a separate line; we just
539 * need to remove the new-line character
540 * at the end of the line
542 cur = line + strlen(line) - 1;
545 /* remove trailing spaces and new-line characters */
546 while (cur >= line && (*cur == ' ' || *cur == '\n'))
549 if (strcmp(line, impl_share->sharepath) == 0) {
550 fclose(nfs_exportfs_temp_fp);
555 fclose(nfs_exportfs_temp_fp);
561 nfs_update_shareopts(sa_share_impl_t impl_share, const char *resource,
562 const char *shareopts)
565 boolean_t needs_reshare = B_FALSE;
568 FSINFO(impl_share, nfs_fstype)->active = is_share_active(impl_share);
570 old_shareopts = FSINFO(impl_share, nfs_fstype)->shareopts;
572 if (strcmp(shareopts, "on") == 0)
575 if (FSINFO(impl_share, nfs_fstype)->active && old_shareopts != NULL &&
576 strcmp(old_shareopts, shareopts) != 0) {
577 needs_reshare = B_TRUE;
578 nfs_disable_share(impl_share);
581 shareopts_dup = strdup(shareopts);
583 if (shareopts_dup == NULL)
586 if (old_shareopts != NULL)
589 FSINFO(impl_share, nfs_fstype)->shareopts = shareopts_dup;
592 nfs_enable_share(impl_share);
599 nfs_clear_shareopts(sa_share_impl_t impl_share)
601 free(FSINFO(impl_share, nfs_fstype)->shareopts);
602 FSINFO(impl_share, nfs_fstype)->shareopts = NULL;
605 static const sa_share_ops_t nfs_shareops = {
606 .enable_share = nfs_enable_share,
607 .disable_share = nfs_disable_share,
609 .validate_shareopts = nfs_validate_shareopts,
610 .update_shareopts = nfs_update_shareopts,
611 .clear_shareopts = nfs_clear_shareopts,
615 * nfs_check_exportfs() checks that the exportfs command runs
616 * and also maintains a temporary copy of the output from
618 * To update this temporary copy simply call this function again.
620 * TODO : Use /var/lib/nfs/etab instead of our private copy.
621 * But must implement locking to prevent concurrent access.
623 * TODO : The temporary file descriptor is never closed since
624 * there is no libshare_nfs_fini() function.
627 nfs_check_exportfs(void)
631 static char nfs_exportfs_tempfile[] = "/tmp/exportfs.XXXXXX";
634 * Close any existing temporary copies of output from exportfs.
635 * We have already called unlink() so file will be deleted.
637 if (nfs_exportfs_temp_fd >= 0)
638 close(nfs_exportfs_temp_fd);
640 nfs_exportfs_temp_fd = mkstemp(nfs_exportfs_tempfile);
642 if (nfs_exportfs_temp_fd < 0)
643 return SA_SYSTEM_ERR;
645 unlink(nfs_exportfs_tempfile);
647 fcntl(nfs_exportfs_temp_fd, F_SETFD, FD_CLOEXEC);
652 return SA_SYSTEM_ERR;
655 while ((rc = waitpid(pid, &status, 0)) <= 0 && errno == EINTR)
656 ; /* empty loop body */
659 return SA_SYSTEM_ERR;
661 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
662 return SA_CONFIG_ERR;
671 if (dup2(nfs_exportfs_temp_fd, STDOUT_FILENO) < 0)
674 rc = execlp("/usr/sbin/exportfs", "exportfs", "-v", NULL);
684 libshare_nfs_init(void)
686 nfs_available = (nfs_check_exportfs() == SA_OK);
688 nfs_fstype = register_fstype("nfs", &nfs_shareops);