2 * Copyright (c) 2007 Pawel Jakub Dawidek <pjd@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * Copyright (c) 2020 by Delphix. All rights reserved.
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
45 #include "libzfs_impl.h"
46 #include "libshare_impl.h"
49 #define _PATH_MOUNTDPID "/var/run/mountd.pid"
50 #define FILE_HEADER "# !!! DO NOT EDIT THIS FILE MANUALLY !!!\n\n"
52 #define MAXLINESIZE (PATH_MAX + OPTSSIZE)
53 #define ZFS_EXPORTS_FILE "/etc/zfs/exports"
54 #define ZFS_EXPORTS_LOCK ZFS_EXPORTS_FILE".lock"
56 static sa_fstype_t *nfs_fstype;
58 static int nfs_lock_fd = -1;
61 * The nfs_exports_[lock|unlock] is used to guard against conconcurrent
62 * updates to the exports file. Each protocol is responsible for
63 * providing the necessary locking to ensure consistency.
66 nfs_exports_lock(void)
68 nfs_lock_fd = open(ZFS_EXPORTS_LOCK,
69 O_RDWR | O_CREAT, 0600);
70 if (nfs_lock_fd == -1) {
71 fprintf(stderr, "failed to lock %s: %s\n",
72 ZFS_EXPORTS_LOCK, strerror(errno));
75 if (flock(nfs_lock_fd, LOCK_EX) != 0) {
76 fprintf(stderr, "failed to lock %s: %s\n",
77 ZFS_EXPORTS_LOCK, strerror(errno));
84 nfs_exports_unlock(void)
86 verify(nfs_lock_fd > 0);
88 if (flock(nfs_lock_fd, LOCK_UN) != 0) {
89 fprintf(stderr, "failed to unlock %s: %s\n",
90 ZFS_EXPORTS_LOCK, strerror(errno));
97 * Read one line from a file. Skip comments, empty lines and a line with a
98 * mountpoint specified in the 'skip' argument.
100 * NOTE: This function returns a static buffer and thus is not thread-safe.
103 zgetline(FILE *fd, const char *skip)
105 static char line[MAXLINESIZE];
106 size_t len, skiplen = 0;
110 skiplen = strlen(skip);
112 s = fgets(line, sizeof (line), fd);
115 /* Skip empty lines and comments. */
116 if (line[0] == '\n' || line[0] == '#')
119 if (line[len - 1] == '\n')
120 line[len - 1] = '\0';
121 last = line[skiplen];
122 /* Skip the given mountpoint. */
123 if (skip != NULL && strncmp(skip, line, skiplen) == 0 &&
124 (last == '\t' || last == ' ' || last == '\0')) {
133 * This function translate options to a format acceptable by exports(5), eg.
135 * -ro -network=192.168.0.0 -mask=255.255.255.0 -maproot=0 \
136 * zfs.freebsd.org 69.147.83.54
138 * Accepted input formats:
140 * ro,network=192.168.0.0,mask=255.255.255.0,maproot=0,zfs.freebsd.org
141 * ro network=192.168.0.0 mask=255.255.255.0 maproot=0 zfs.freebsd.org
142 * -ro,-network=192.168.0.0,-mask=255.255.255.0,-maproot=0,zfs.freebsd.org
143 * -ro -network=192.168.0.0 -mask=255.255.255.0 -maproot=0 \
146 * Recognized keywords:
148 * ro, maproot, mapall, mask, network, sec, alldirs, public, webnfs,
151 * NOTE: This function returns a static buffer and thus is not thread-safe.
154 translate_opts(const char *shareopts)
156 static const char *known_opts[] = { "ro", "maproot", "mapall", "mask",
157 "network", "sec", "alldirs", "public", "webnfs", "index", "quiet",
159 static char newopts[OPTSSIZE];
160 char oldopts[OPTSSIZE];
165 strlcpy(oldopts, shareopts, sizeof (oldopts));
168 while ((o = strsep(&s, "-, ")) != NULL) {
171 for (i = 0; known_opts[i] != NULL; i++) {
172 len = strlen(known_opts[i]);
173 if (strncmp(known_opts[i], o, len) == 0 &&
174 (o[len] == '\0' || o[len] == '=')) {
175 strlcat(newopts, "-", sizeof (newopts));
179 strlcat(newopts, o, sizeof (newopts));
180 strlcat(newopts, " ", sizeof (newopts));
186 nfs_init_tmpfile(void)
188 char *tmpfile = NULL;
190 if (asprintf(&tmpfile, "%s%s", ZFS_EXPORTS_FILE, ".XXXXXXXX") == -1) {
191 fprintf(stderr, "Unable to allocate buffer for temporary "
196 int fd = mkstemp(tmpfile);
198 fprintf(stderr, "Unable to create temporary file: %s",
208 nfs_fini_tmpfile(char *tmpfile)
210 if (rename(tmpfile, ZFS_EXPORTS_FILE) == -1) {
211 fprintf(stderr, "Unable to rename %s: %s\n", tmpfile,
215 return (SA_SYSTEM_ERR);
222 * This function copies all entries from the exports file to "filename",
223 * omitting any entries for the specified mountpoint.
226 nfs_copy_entries(char *filename, const char *mountpoint)
231 FILE *oldfp = fopen(ZFS_EXPORTS_FILE, "r");
232 FILE *newfp = fopen(filename, "w+");
234 fprintf(stderr, "failed to open %s file: %s", filename,
237 return (SA_SYSTEM_ERR);
239 fputs(FILE_HEADER, newfp);
242 * The ZFS_EXPORTS_FILE may not exist yet. If that's the
243 * case then just write out the new file.
246 while ((line = zgetline(oldfp, mountpoint)) != NULL)
247 fprintf(newfp, "%s\n", line);
248 if (ferror(oldfp) != 0) {
249 error = ferror(oldfp);
251 if (fclose(oldfp) != 0) {
252 fprintf(stderr, "Unable to close file %s: %s\n",
253 filename, strerror(errno));
254 error = error != 0 ? error : SA_SYSTEM_ERR;
258 if (error == 0 && ferror(newfp) != 0) {
259 error = ferror(newfp);
262 if (fclose(newfp) != 0) {
263 fprintf(stderr, "Unable to close file %s: %s\n",
264 filename, strerror(errno));
265 error = error != 0 ? error : SA_SYSTEM_ERR;
271 nfs_enable_share(sa_share_impl_t impl_share)
273 char *filename = NULL;
276 if ((filename = nfs_init_tmpfile()) == NULL)
277 return (SA_SYSTEM_ERR);
279 error = nfs_exports_lock();
286 error = nfs_copy_entries(filename, impl_share->sa_mountpoint);
287 if (error != SA_OK) {
290 nfs_exports_unlock();
294 FILE *fp = fopen(filename, "a+");
296 fprintf(stderr, "failed to open %s file: %s", filename,
300 nfs_exports_unlock();
301 return (SA_SYSTEM_ERR);
303 char *shareopts = FSINFO(impl_share, nfs_fstype)->shareopts;
304 if (strcmp(shareopts, "on") == 0)
307 if (fprintf(fp, "%s\t%s\n", impl_share->sa_mountpoint,
308 translate_opts(shareopts)) < 0) {
309 fprintf(stderr, "failed to write to %s\n", filename);
313 nfs_exports_unlock();
314 return (SA_SYSTEM_ERR);
317 if (fclose(fp) != 0) {
318 fprintf(stderr, "Unable to close file %s: %s\n",
319 filename, strerror(errno));
322 nfs_exports_unlock();
323 return (SA_SYSTEM_ERR);
325 error = nfs_fini_tmpfile(filename);
326 nfs_exports_unlock();
331 nfs_disable_share(sa_share_impl_t impl_share)
334 char *filename = NULL;
336 if ((filename = nfs_init_tmpfile()) == NULL)
337 return (SA_SYSTEM_ERR);
339 error = nfs_exports_lock();
346 error = nfs_copy_entries(filename, impl_share->sa_mountpoint);
347 if (error != SA_OK) {
350 nfs_exports_unlock();
354 error = nfs_fini_tmpfile(filename);
355 nfs_exports_unlock();
360 * NOTE: This function returns a static buffer and thus is not thread-safe.
363 nfs_is_shared(sa_share_impl_t impl_share)
365 static char line[MAXLINESIZE];
368 char *mntpoint = impl_share->sa_mountpoint;
369 size_t mntlen = strlen(mntpoint);
371 FILE *fp = fopen(ZFS_EXPORTS_FILE, "r");
376 s = fgets(line, sizeof (line), fp);
379 /* Skip empty lines and comments. */
380 if (line[0] == '\n' || line[0] == '#')
383 if (line[len - 1] == '\n')
384 line[len - 1] = '\0';
386 /* Skip the given mountpoint. */
387 if (strncmp(mntpoint, line, mntlen) == 0 &&
388 (last == '\t' || last == ' ' || last == '\0')) {
398 nfs_validate_shareopts(const char *shareopts)
404 nfs_update_shareopts(sa_share_impl_t impl_share, const char *shareopts)
406 FSINFO(impl_share, nfs_fstype)->shareopts = (char *)shareopts;
411 nfs_clear_shareopts(sa_share_impl_t impl_share)
413 FSINFO(impl_share, nfs_fstype)->shareopts = NULL;
417 * Commit the shares by restarting mountd.
420 nfs_commit_shares(void)
425 pfh = pidfile_open(_PATH_MOUNTDPID, 0600, &mountdpid);
427 /* Mountd is not running. */
431 if (errno != EEXIST) {
432 /* Cannot open pidfile for some reason. */
433 return (SA_SYSTEM_ERR);
435 /* We have mountd(8) PID in mountdpid variable. */
436 kill(mountdpid, SIGHUP);
440 static const sa_share_ops_t nfs_shareops = {
441 .enable_share = nfs_enable_share,
442 .disable_share = nfs_disable_share,
443 .is_shared = nfs_is_shared,
445 .validate_shareopts = nfs_validate_shareopts,
446 .update_shareopts = nfs_update_shareopts,
447 .clear_shareopts = nfs_clear_shareopts,
448 .commit_shares = nfs_commit_shares,
452 * Initializes the NFS functionality of libshare.
455 libshare_nfs_init(void)
457 nfs_fstype = register_fstype("nfs", &nfs_shareops);