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"
40 static boolean_t
nfs_available(void);
42 static sa_fstype_t
*nfs_fstype
;
45 * nfs_exportfs_temp_fd refers to a temporary copy of the output
48 static int nfs_exportfs_temp_fd
= -1;
50 typedef int (*nfs_shareopt_callback_t
)(const char *opt
, const char *value
,
53 typedef int (*nfs_host_callback_t
)(const char *sharepath
, const char *host
,
54 const char *security
, const char *access
, void *cookie
);
57 * Invokes the specified callback function for each Solaris share option
58 * listed in the specified string.
61 foreach_nfs_shareopt(const char *shareopts
,
62 nfs_shareopt_callback_t callback
, void *cookie
)
64 char *shareopts_dup
, *opt
, *cur
, *value
;
67 if (shareopts
== NULL
)
70 shareopts_dup
= strdup(shareopts
);
72 if (shareopts_dup
== NULL
)
73 return (SA_NO_MEMORY
);
81 while (*cur
!= ',' && *cur
!= '\0')
90 value
= strchr(opt
, '=');
97 rc
= callback(opt
, value
, cookie
);
116 typedef struct nfs_host_cookie_s
{
117 nfs_host_callback_t callback
;
118 const char *sharepath
;
120 const char *security
;
124 * Helper function for foreach_nfs_host. This function checks whether the
125 * current share option is a host specification and invokes a callback
126 * function with information about the host.
129 foreach_nfs_host_cb(const char *opt
, const char *value
, void *pcookie
)
133 char *host_dup
, *host
, *next
;
134 nfs_host_cookie_t
*udata
= (nfs_host_cookie_t
*)pcookie
;
137 fprintf(stderr
, "foreach_nfs_host_cb: key=%s, value=%s\n", opt
, value
);
140 if (strcmp(opt
, "sec") == 0)
141 udata
->security
= value
;
143 if (strcmp(opt
, "rw") == 0 || strcmp(opt
, "ro") == 0) {
149 host_dup
= strdup(value
);
151 if (host_dup
== NULL
)
152 return (SA_NO_MEMORY
);
157 next
= strchr(host
, ':');
163 rc
= udata
->callback(udata
->sharepath
, host
,
164 udata
->security
, access
, udata
->cookie
);
173 } while (host
!= NULL
);
182 * Invokes a callback function for all NFS hosts that are set for a share.
185 foreach_nfs_host(sa_share_impl_t impl_share
, nfs_host_callback_t callback
,
188 nfs_host_cookie_t udata
;
191 udata
.callback
= callback
;
192 udata
.sharepath
= impl_share
->sharepath
;
193 udata
.cookie
= cookie
;
194 udata
.security
= "sys";
196 shareopts
= FSINFO(impl_share
, nfs_fstype
)->shareopts
;
198 return foreach_nfs_shareopt(shareopts
, foreach_nfs_host_cb
,
203 * Converts a Solaris NFS host specification to its Linux equivalent.
206 get_linux_hostspec(const char *solaris_hostspec
, char **plinux_hostspec
)
209 * For now we just support CIDR masks (e.g. @192.168.0.0/16) and host
210 * wildcards (e.g. *.example.org).
212 if (solaris_hostspec
[0] == '@') {
214 * Solaris host specifier, e.g. @192.168.0.0/16; we just need
215 * to skip the @ in this case
217 *plinux_hostspec
= strdup(solaris_hostspec
+ 1);
219 *plinux_hostspec
= strdup(solaris_hostspec
);
222 if (*plinux_hostspec
== NULL
) {
223 return (SA_NO_MEMORY
);
230 * Used internally by nfs_enable_share to enable sharing for a single host.
233 nfs_enable_share_one(const char *sharepath
, const char *host
,
234 const char *security
, const char *access
, void *pcookie
)
237 char *linuxhost
, *hostpath
, *opts
;
238 const char *linux_opts
= (const char *)pcookie
;
241 /* exportfs -i -o sec=XX,rX,<opts> <host>:<sharepath> */
243 rc
= get_linux_hostspec(host
, &linuxhost
);
248 hostpath
= malloc(strlen(linuxhost
) + 1 + strlen(sharepath
) + 1);
250 if (hostpath
== NULL
) {
256 sprintf(hostpath
, "%s:%s", linuxhost
, sharepath
);
260 if (linux_opts
== NULL
)
263 opts
= malloc(4 + strlen(security
) + 4 + strlen(linux_opts
) + 1);
268 sprintf(opts
, "sec=%s,%s,%s", security
, access
, linux_opts
);
271 fprintf(stderr
, "sharing %s with opts %s\n", hostpath
, opts
);
274 argv
[0] = "/usr/sbin/exportfs";
281 rc
= libzfs_run_process(argv
[0], argv
, 0);
287 return (SA_SYSTEM_ERR
);
293 * Adds a Linux share option to an array of NFS options.
296 add_linux_shareopt(char **plinux_opts
, const char *key
, const char *value
)
299 char *new_linux_opts
;
301 if (*plinux_opts
!= NULL
)
302 len
= strlen(*plinux_opts
);
304 new_linux_opts
= realloc(*plinux_opts
, len
+ 1 + strlen(key
) +
305 (value
? 1 + strlen(value
) : 0) + 1);
307 if (new_linux_opts
== NULL
)
308 return (SA_NO_MEMORY
);
310 new_linux_opts
[len
] = '\0';
313 strcat(new_linux_opts
, ",");
315 strcat(new_linux_opts
, key
);
318 strcat(new_linux_opts
, "=");
319 strcat(new_linux_opts
, value
);
322 *plinux_opts
= new_linux_opts
;
328 * Validates and converts a single Solaris share option to its Linux
332 get_linux_shareopts_cb(const char *key
, const char *value
, void *cookie
)
334 char **plinux_opts
= (char **)cookie
;
336 /* host-specific options, these are taken care of elsewhere */
337 if (strcmp(key
, "ro") == 0 || strcmp(key
, "rw") == 0 ||
338 strcmp(key
, "sec") == 0)
341 if (strcmp(key
, "anon") == 0)
344 if (strcmp(key
, "root_mapping") == 0) {
345 (void) add_linux_shareopt(plinux_opts
, "root_squash", NULL
);
349 if (strcmp(key
, "nosub") == 0)
350 key
= "subtree_check";
352 if (strcmp(key
, "insecure") != 0 && strcmp(key
, "secure") != 0 &&
353 strcmp(key
, "async") != 0 && strcmp(key
, "sync") != 0 &&
354 strcmp(key
, "no_wdelay") != 0 && strcmp(key
, "wdelay") != 0 &&
355 strcmp(key
, "nohide") != 0 && strcmp(key
, "hide") != 0 &&
356 strcmp(key
, "crossmnt") != 0 &&
357 strcmp(key
, "no_subtree_check") != 0 &&
358 strcmp(key
, "subtree_check") != 0 &&
359 strcmp(key
, "insecure_locks") != 0 &&
360 strcmp(key
, "secure_locks") != 0 &&
361 strcmp(key
, "no_auth_nlm") != 0 && strcmp(key
, "auth_nlm") != 0 &&
362 strcmp(key
, "no_acl") != 0 && strcmp(key
, "mountpoint") != 0 &&
363 strcmp(key
, "mp") != 0 && strcmp(key
, "fsuid") != 0 &&
364 strcmp(key
, "refer") != 0 && strcmp(key
, "replicas") != 0 &&
365 strcmp(key
, "root_squash") != 0 &&
366 strcmp(key
, "no_root_squash") != 0 &&
367 strcmp(key
, "all_squash") != 0 &&
368 strcmp(key
, "no_all_squash") != 0 && strcmp(key
, "fsid") != 0 &&
369 strcmp(key
, "anonuid") != 0 && strcmp(key
, "anongid") != 0) {
370 return (SA_SYNTAX_ERR
);
373 (void) add_linux_shareopt(plinux_opts
, key
, value
);
379 * Takes a string containing Solaris share options (e.g. "sync,no_acl") and
380 * converts them to a NULL-terminated array of Linux NFS options.
383 get_linux_shareopts(const char *shareopts
, char **plinux_opts
)
387 assert(plinux_opts
!= NULL
);
391 /* no_subtree_check - Default as of nfs-utils v1.1.0 */
392 (void) add_linux_shareopt(plinux_opts
, "no_subtree_check", NULL
);
394 /* mountpoint - Restrict exports to ZFS mountpoints */
395 (void) add_linux_shareopt(plinux_opts
, "mountpoint", NULL
);
397 rc
= foreach_nfs_shareopt(shareopts
, get_linux_shareopts_cb
,
409 * Enables NFS sharing for the specified share.
412 nfs_enable_share(sa_share_impl_t impl_share
)
414 char *shareopts
, *linux_opts
;
417 if (!nfs_available()) {
418 return (SA_SYSTEM_ERR
);
421 shareopts
= FSINFO(impl_share
, nfs_fstype
)->shareopts
;
423 if (shareopts
== NULL
)
426 rc
= get_linux_shareopts(shareopts
, &linux_opts
);
431 rc
= foreach_nfs_host(impl_share
, nfs_enable_share_one
, linux_opts
);
439 * Used internally by nfs_disable_share to disable sharing for a single host.
442 nfs_disable_share_one(const char *sharepath
, const char *host
,
443 const char *security
, const char *access
, void *cookie
)
446 char *linuxhost
, *hostpath
;
449 rc
= get_linux_hostspec(host
, &linuxhost
);
454 hostpath
= malloc(strlen(linuxhost
) + 1 + strlen(sharepath
) + 1);
456 if (hostpath
== NULL
) {
461 sprintf(hostpath
, "%s:%s", linuxhost
, sharepath
);
466 fprintf(stderr
, "unsharing %s\n", hostpath
);
469 argv
[0] = "/usr/sbin/exportfs";
474 rc
= libzfs_run_process(argv
[0], argv
, 0);
479 return (SA_SYSTEM_ERR
);
485 * Disables NFS sharing for the specified share.
488 nfs_disable_share(sa_share_impl_t impl_share
)
490 if (!nfs_available()) {
492 * The share can't possibly be active, so nothing
493 * needs to be done to disable it.
498 return (foreach_nfs_host(impl_share
, nfs_disable_share_one
, NULL
));
502 * Checks whether the specified NFS share options are syntactically correct.
505 nfs_validate_shareopts(const char *shareopts
)
510 rc
= get_linux_shareopts(shareopts
, &linux_opts
);
521 * Checks whether a share is currently active.
524 nfs_is_share_active(sa_share_impl_t impl_share
)
529 FILE *nfs_exportfs_temp_fp
;
531 if (!nfs_available())
534 if ((fd
= dup(nfs_exportfs_temp_fd
)) == -1)
537 nfs_exportfs_temp_fp
= fdopen(fd
, "r");
539 if (nfs_exportfs_temp_fp
== NULL
)
542 if (fseek(nfs_exportfs_temp_fp
, 0, SEEK_SET
) < 0) {
543 fclose(nfs_exportfs_temp_fp
);
547 while (fgets(line
, sizeof (line
), nfs_exportfs_temp_fp
) != NULL
) {
549 * exportfs uses separate lines for the share path
550 * and the export options when the share path is longer
551 * than a certain amount of characters; this ignores
557 tab
= strchr(line
, '\t');
564 * there's no tab character, which means the
565 * NFS options are on a separate line; we just
566 * need to remove the new-line character
567 * at the end of the line
569 cur
= line
+ strlen(line
) - 1;
572 /* remove trailing spaces and new-line characters */
573 while (cur
>= line
&& (*cur
== ' ' || *cur
== '\n'))
576 if (strcmp(line
, impl_share
->sharepath
) == 0) {
577 fclose(nfs_exportfs_temp_fp
);
582 fclose(nfs_exportfs_temp_fp
);
588 * Called to update a share's options. A share's options might be out of
589 * date if the share was loaded from disk (i.e. /etc/dfs/sharetab) and the
590 * "sharenfs" dataset property has changed in the meantime. This function
591 * also takes care of re-enabling the share if necessary.
594 nfs_update_shareopts(sa_share_impl_t impl_share
, const char *resource
,
595 const char *shareopts
)
598 boolean_t needs_reshare
= B_FALSE
;
601 FSINFO(impl_share
, nfs_fstype
)->active
=
602 nfs_is_share_active(impl_share
);
604 old_shareopts
= FSINFO(impl_share
, nfs_fstype
)->shareopts
;
606 if (strcmp(shareopts
, "on") == 0)
607 shareopts
= "rw,crossmnt";
609 if (FSINFO(impl_share
, nfs_fstype
)->active
&& old_shareopts
!= NULL
&&
610 strcmp(old_shareopts
, shareopts
) != 0) {
611 needs_reshare
= B_TRUE
;
612 nfs_disable_share(impl_share
);
615 shareopts_dup
= strdup(shareopts
);
617 if (shareopts_dup
== NULL
)
618 return (SA_NO_MEMORY
);
620 if (old_shareopts
!= NULL
)
623 FSINFO(impl_share
, nfs_fstype
)->shareopts
= shareopts_dup
;
626 nfs_enable_share(impl_share
);
632 * Clears a share's NFS options. Used by libshare to
633 * clean up shares that are about to be free()'d.
636 nfs_clear_shareopts(sa_share_impl_t impl_share
)
638 free(FSINFO(impl_share
, nfs_fstype
)->shareopts
);
639 FSINFO(impl_share
, nfs_fstype
)->shareopts
= NULL
;
642 static const sa_share_ops_t nfs_shareops
= {
643 .enable_share
= nfs_enable_share
,
644 .disable_share
= nfs_disable_share
,
646 .validate_shareopts
= nfs_validate_shareopts
,
647 .update_shareopts
= nfs_update_shareopts
,
648 .clear_shareopts
= nfs_clear_shareopts
,
652 * nfs_check_exportfs() checks that the exportfs command runs
653 * and also maintains a temporary copy of the output from
655 * To update this temporary copy simply call this function again.
657 * TODO : Use /var/lib/nfs/etab instead of our private copy.
658 * But must implement locking to prevent concurrent access.
660 * TODO : The temporary file descriptor is never closed since
661 * there is no libshare_nfs_fini() function.
664 nfs_check_exportfs(void)
668 static char nfs_exportfs_tempfile
[] = "/tmp/exportfs.XXXXXX";
671 * Close any existing temporary copies of output from exportfs.
672 * We have already called unlink() so file will be deleted.
674 if (nfs_exportfs_temp_fd
>= 0)
675 close(nfs_exportfs_temp_fd
);
677 nfs_exportfs_temp_fd
= mkstemp(nfs_exportfs_tempfile
);
679 if (nfs_exportfs_temp_fd
< 0)
680 return (SA_SYSTEM_ERR
);
682 unlink(nfs_exportfs_tempfile
);
684 (void) fcntl(nfs_exportfs_temp_fd
, F_SETFD
, FD_CLOEXEC
);
689 (void) close(nfs_exportfs_temp_fd
);
690 nfs_exportfs_temp_fd
= -1;
691 return (SA_SYSTEM_ERR
);
695 while ((rc
= waitpid(pid
, &status
, 0)) <= 0 &&
699 (void) close(nfs_exportfs_temp_fd
);
700 nfs_exportfs_temp_fd
= -1;
701 return (SA_SYSTEM_ERR
);
704 if (!WIFEXITED(status
) || WEXITSTATUS(status
) != 0) {
705 (void) close(nfs_exportfs_temp_fd
);
706 nfs_exportfs_temp_fd
= -1;
707 return (SA_CONFIG_ERR
);
717 if (dup2(nfs_exportfs_temp_fd
, STDOUT_FILENO
) < 0)
720 rc
= execlp("/usr/sbin/exportfs", "exportfs", "-v", NULL
);
730 * Provides a convenient wrapper for determining nfs availability
735 if (nfs_exportfs_temp_fd
== -1)
736 (void) nfs_check_exportfs();
738 return ((nfs_exportfs_temp_fd
!= -1) ? B_TRUE
: B_FALSE
);
742 * Initializes the NFS functionality of libshare.
745 libshare_nfs_init(void)
747 nfs_fstype
= register_fstype("nfs", &nfs_shareops
);