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)
214 char *linuxhost, *hostpath, *opts;
215 const char *linux_opts = (const char *)pcookie;
218 /* exportfs -i -o sec=XX,rX,<opts> <host>:<sharepath> */
220 rc = get_linux_hostspec(host, &linuxhost);
225 hostpath = malloc(strlen(linuxhost) + 1 + strlen(sharepath) + 1);
227 if (hostpath == NULL) {
233 sprintf(hostpath, "%s:%s", linuxhost, sharepath);
237 if (linux_opts == NULL)
240 opts = malloc(4 + strlen(security) + 4 + strlen(linux_opts) + 1);
245 sprintf(opts, "sec=%s,%s,%s", security, access, linux_opts);
248 fprintf(stderr, "sharing %s with opts %s\n", hostpath, opts);
251 argv[0] = "/usr/sbin/exportfs";
258 rc = libzfs_run_process(argv[0], argv, 0);
264 return SA_SYSTEM_ERR;
270 add_linux_shareopt(char **plinux_opts, const char *key, const char *value)
273 char *new_linux_opts;
275 if (*plinux_opts != NULL)
276 len = strlen(*plinux_opts);
278 new_linux_opts = realloc(*plinux_opts, len + 1 + strlen(key) +
279 (value ? 1 + strlen(value) : 0) + 1);
281 if (new_linux_opts == NULL)
284 new_linux_opts[len] = '\0';
287 strcat(new_linux_opts, ",");
289 strcat(new_linux_opts, key);
292 strcat(new_linux_opts, "=");
293 strcat(new_linux_opts, value);
296 *plinux_opts = new_linux_opts;
302 get_linux_shareopts_cb(const char *key, const char *value, void *cookie)
304 char **plinux_opts = (char **)cookie;
306 /* host-specific options, these are taken care of elsewhere */
307 if (strcmp(key, "ro") == 0 || strcmp(key, "rw") == 0 ||
308 strcmp(key, "sec") == 0)
311 if (strcmp(key, "anon") == 0)
314 if (strcmp(key, "root_mapping") == 0) {
315 (void) add_linux_shareopt(plinux_opts, "root_squash", NULL);
319 if (strcmp(key, "nosub") == 0)
320 key = "subtree_check";
322 if (strcmp(key, "insecure") != 0 && strcmp(key, "secure") != 0 &&
323 strcmp(key, "async") != 0 && strcmp(key, "sync") != 0 &&
324 strcmp(key, "no_wdelay") != 0 && strcmp(key, "wdelay") != 0 &&
325 strcmp(key, "nohide") != 0 && strcmp(key, "hide") != 0 &&
326 strcmp(key, "crossmnt") != 0 &&
327 strcmp(key, "no_subtree_check") != 0 &&
328 strcmp(key, "subtree_check") != 0 &&
329 strcmp(key, "insecure_locks") != 0 &&
330 strcmp(key, "secure_locks") != 0 &&
331 strcmp(key, "no_auth_nlm") != 0 && strcmp(key, "auth_nlm") != 0 &&
332 strcmp(key, "no_acl") != 0 && strcmp(key, "mountpoint") != 0 &&
333 strcmp(key, "mp") != 0 && strcmp(key, "fsuid") != 0 &&
334 strcmp(key, "refer") != 0 && strcmp(key, "replicas") != 0 &&
335 strcmp(key, "root_squash") != 0 &&
336 strcmp(key, "no_root_squash") != 0 &&
337 strcmp(key, "all_squash") != 0 &&
338 strcmp(key, "no_all_squash") != 0 &&
339 strcmp(key, "anonuid") != 0 && strcmp(key, "anongid") != 0) {
340 return SA_SYNTAX_ERR;
343 (void) add_linux_shareopt(plinux_opts, key, value);
349 get_linux_shareopts(const char *shareopts, char **plinux_opts)
353 assert(plinux_opts != NULL);
357 /* default options for Solaris shares */
358 (void) add_linux_shareopt(plinux_opts, "no_subtree_check", NULL);
359 (void) add_linux_shareopt(plinux_opts, "no_root_squash", NULL);
360 (void) add_linux_shareopt(plinux_opts, "mountpoint", NULL);
362 rc = foreach_nfs_shareopt(shareopts, get_linux_shareopts_cb, plinux_opts);
373 nfs_enable_share(sa_share_impl_t impl_share)
375 char *shareopts, *linux_opts;
378 if (!nfs_available) {
379 return SA_SYSTEM_ERR;
382 shareopts = FSINFO(impl_share, nfs_fstype)->shareopts;
384 if (shareopts == NULL)
387 rc = get_linux_shareopts(shareopts, &linux_opts);
392 rc = foreach_nfs_host(impl_share, nfs_enable_share_one, linux_opts);
400 nfs_disable_share_one(const char *sharepath, const char *host,
401 const char *security, const char *access, void *cookie)
404 char *linuxhost, *hostpath;
407 rc = get_linux_hostspec(host, &linuxhost);
412 hostpath = malloc(strlen(linuxhost) + 1 + strlen(sharepath) + 1);
414 if (hostpath == NULL) {
419 sprintf(hostpath, "%s:%s", linuxhost, sharepath);
424 fprintf(stderr, "unsharing %s\n", hostpath);
427 argv[0] = "/usr/sbin/exportfs";
432 rc = libzfs_run_process(argv[0], argv, 0);
437 return SA_SYSTEM_ERR;
443 nfs_disable_share(sa_share_impl_t impl_share)
445 if (!nfs_available) {
447 * The share can't possibly be active, so nothing
448 * needs to be done to disable it.
453 return foreach_nfs_host(impl_share, nfs_disable_share_one, NULL);
457 nfs_validate_shareopts(const char *shareopts)
462 rc = get_linux_shareopts(shareopts, &linux_opts);
473 is_share_active(sa_share_impl_t impl_share)
477 FILE *nfs_exportfs_temp_fp;
479 if (nfs_exportfs_temp_fd < 0)
482 nfs_exportfs_temp_fp = fdopen(dup(nfs_exportfs_temp_fd), "r");
484 if (nfs_exportfs_temp_fp == NULL ||
485 fseek(nfs_exportfs_temp_fp, 0, SEEK_SET) < 0) {
486 fclose(nfs_exportfs_temp_fp);
490 while (fgets(line, sizeof(line), nfs_exportfs_temp_fp) != NULL) {
492 * exportfs uses separate lines for the share path
493 * and the export options when the share path is longer
494 * than a certain amount of characters; this ignores
500 tab = strchr(line, '\t');
507 * there's no tab character, which means the
508 * NFS options are on a separate line; we just
509 * need to remove the new-line character
510 * at the end of the line
512 cur = line + strlen(line) - 1;
515 /* remove trailing spaces and new-line characters */
516 while (cur >= line && (*cur == ' ' || *cur == '\n'))
519 if (strcmp(line, impl_share->sharepath) == 0) {
520 fclose(nfs_exportfs_temp_fp);
525 fclose(nfs_exportfs_temp_fp);
531 nfs_update_shareopts(sa_share_impl_t impl_share, const char *resource,
532 const char *shareopts)
535 boolean_t needs_reshare = B_FALSE;
538 FSINFO(impl_share, nfs_fstype)->active = is_share_active(impl_share);
540 old_shareopts = FSINFO(impl_share, nfs_fstype)->shareopts;
542 if (strcmp(shareopts, "on") == 0)
545 if (FSINFO(impl_share, nfs_fstype)->active && old_shareopts != NULL &&
546 strcmp(old_shareopts, shareopts) != 0) {
547 needs_reshare = B_TRUE;
548 nfs_disable_share(impl_share);
551 shareopts_dup = strdup(shareopts);
553 if (shareopts_dup == NULL)
556 if (old_shareopts != NULL)
559 FSINFO(impl_share, nfs_fstype)->shareopts = shareopts_dup;
562 nfs_enable_share(impl_share);
569 nfs_clear_shareopts(sa_share_impl_t impl_share)
571 free(FSINFO(impl_share, nfs_fstype)->shareopts);
572 FSINFO(impl_share, nfs_fstype)->shareopts = NULL;
575 static const sa_share_ops_t nfs_shareops = {
576 .enable_share = nfs_enable_share,
577 .disable_share = nfs_disable_share,
579 .validate_shareopts = nfs_validate_shareopts,
580 .update_shareopts = nfs_update_shareopts,
581 .clear_shareopts = nfs_clear_shareopts,
585 * nfs_check_exportfs() checks that the exportfs command runs
586 * and also maintains a temporary copy of the output from
588 * To update this temporary copy simply call this function again.
590 * TODO : Use /var/lib/nfs/etab instead of our private copy.
591 * But must implement locking to prevent concurrent access.
593 * TODO : The temporary file descriptor is never closed since
594 * there is no libshare_nfs_fini() function.
597 nfs_check_exportfs(void)
601 static char nfs_exportfs_tempfile[] = "/tmp/exportfs.XXXXXX";
604 * Close any existing temporary copies of output from exportfs.
605 * We have already called unlink() so file will be deleted.
607 if (nfs_exportfs_temp_fd >= 0)
608 close(nfs_exportfs_temp_fd);
610 nfs_exportfs_temp_fd = mkstemp(nfs_exportfs_tempfile);
612 if (nfs_exportfs_temp_fd < 0)
613 return SA_SYSTEM_ERR;
615 unlink(nfs_exportfs_tempfile);
617 fcntl(nfs_exportfs_temp_fd, F_SETFD, FD_CLOEXEC);
622 return SA_SYSTEM_ERR;
625 while ((rc = waitpid(pid, &status, 0)) <= 0 && errno == EINTR)
626 ; /* empty loop body */
629 return SA_SYSTEM_ERR;
631 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
632 return SA_CONFIG_ERR;
641 if (dup2(nfs_exportfs_temp_fd, STDOUT_FILENO) < 0)
644 rc = execlp("/usr/sbin/exportfs", "exportfs", "-v", NULL);
654 libshare_nfs_init(void)
656 nfs_available = (nfs_check_exportfs() == SA_OK);
658 nfs_fstype = register_fstype("nfs", &nfs_shareops);