xref: /freebsd-src/sys/contrib/openzfs/lib/libshare/os/freebsd/nfs.c (revision e92ffd9b626833ebdbf2742c8ffddc6cd94b963e)
1 /*
2  * Copyright (c) 2007 Pawel Jakub Dawidek <pjd@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
13  *
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
24  * SUCH DAMAGE.
25  *
26  * Copyright (c) 2020 by Delphix. All rights reserved.
27  */
28 
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
32 #include <sys/param.h>
33 #include <sys/vfs.h>
34 
35 #include <assert.h>
36 #include <errno.h>
37 #include <fcntl.h>
38 #include <libutil.h>
39 #include <signal.h>
40 #include <stdio.h>
41 #include <string.h>
42 #include <unistd.h>
43 #include <libintl.h>
44 
45 #include <libshare.h>
46 #include "libshare_impl.h"
47 #include "nfs.h"
48 
49 #define	_PATH_MOUNTDPID	"/var/run/mountd.pid"
50 #define	OPTSSIZE	1024
51 #define	MAXLINESIZE	(PATH_MAX + OPTSSIZE)
52 #define	ZFS_EXPORTS_FILE	"/etc/zfs/exports"
53 #define	ZFS_EXPORTS_LOCK	ZFS_EXPORTS_FILE".lock"
54 
55 static sa_fstype_t *nfs_fstype;
56 
57 /*
58  * This function translate options to a format acceptable by exports(5), eg.
59  *
60  *	-ro -network=192.168.0.0 -mask=255.255.255.0 -maproot=0 \
61  *	zfs.freebsd.org 69.147.83.54
62  *
63  * Accepted input formats:
64  *
65  *	ro,network=192.168.0.0,mask=255.255.255.0,maproot=0,zfs.freebsd.org
66  *	ro network=192.168.0.0 mask=255.255.255.0 maproot=0 zfs.freebsd.org
67  *	-ro,-network=192.168.0.0,-mask=255.255.255.0,-maproot=0,zfs.freebsd.org
68  *	-ro -network=192.168.0.0 -mask=255.255.255.0 -maproot=0 \
69  *	zfs.freebsd.org
70  *
71  * Recognized keywords:
72  *
73  *	ro, maproot, mapall, mask, network, sec, alldirs, public, webnfs,
74  *	index, quiet
75  *
76  * NOTE: This function returns a static buffer and thus is not thread-safe.
77  */
78 static char *
79 translate_opts(const char *shareopts)
80 {
81 	static const char *known_opts[] = { "ro", "maproot", "mapall", "mask",
82 	    "network", "sec", "alldirs", "public", "webnfs", "index", "quiet",
83 	    NULL };
84 	static char newopts[OPTSSIZE];
85 	char oldopts[OPTSSIZE];
86 	char *o, *s = NULL;
87 	unsigned int i;
88 	size_t len;
89 
90 	strlcpy(oldopts, shareopts, sizeof (oldopts));
91 	newopts[0] = '\0';
92 	s = oldopts;
93 	while ((o = strsep(&s, "-, ")) != NULL) {
94 		if (o[0] == '\0')
95 			continue;
96 		for (i = 0; known_opts[i] != NULL; i++) {
97 			len = strlen(known_opts[i]);
98 			if (strncmp(known_opts[i], o, len) == 0 &&
99 			    (o[len] == '\0' || o[len] == '=')) {
100 				strlcat(newopts, "-", sizeof (newopts));
101 				break;
102 			}
103 		}
104 		strlcat(newopts, o, sizeof (newopts));
105 		strlcat(newopts, " ", sizeof (newopts));
106 	}
107 	return (newopts);
108 }
109 
110 static int
111 nfs_enable_share_impl(sa_share_impl_t impl_share, FILE *tmpfile)
112 {
113 	char *shareopts = FSINFO(impl_share, nfs_fstype)->shareopts;
114 	if (strcmp(shareopts, "on") == 0)
115 		shareopts = "";
116 
117 	if (fprintf(tmpfile, "%s\t%s\n", impl_share->sa_mountpoint,
118 	    translate_opts(shareopts)) < 0) {
119 		fprintf(stderr, "failed to write to temporary file\n");
120 		return (SA_SYSTEM_ERR);
121 	}
122 
123 	return (SA_OK);
124 }
125 
126 static int
127 nfs_enable_share(sa_share_impl_t impl_share)
128 {
129 	return (nfs_toggle_share(
130 	    ZFS_EXPORTS_LOCK, ZFS_EXPORTS_FILE, NULL, impl_share,
131 	    nfs_enable_share_impl));
132 }
133 
134 static int
135 nfs_disable_share_impl(sa_share_impl_t impl_share, FILE *tmpfile)
136 {
137 	(void) impl_share, (void) tmpfile;
138 	return (SA_OK);
139 }
140 
141 static int
142 nfs_disable_share(sa_share_impl_t impl_share)
143 {
144 	return (nfs_toggle_share(
145 	    ZFS_EXPORTS_LOCK, ZFS_EXPORTS_FILE, NULL, impl_share,
146 	    nfs_disable_share_impl));
147 }
148 
149 static boolean_t
150 nfs_is_shared(sa_share_impl_t impl_share)
151 {
152 	return (nfs_is_shared_impl(ZFS_EXPORTS_FILE, impl_share));
153 }
154 
155 static int
156 nfs_validate_shareopts(const char *shareopts)
157 {
158 	return (SA_OK);
159 }
160 
161 static int
162 nfs_update_shareopts(sa_share_impl_t impl_share, const char *shareopts)
163 {
164 	FSINFO(impl_share, nfs_fstype)->shareopts = (char *)shareopts;
165 	return (SA_OK);
166 }
167 
168 static void
169 nfs_clear_shareopts(sa_share_impl_t impl_share)
170 {
171 	FSINFO(impl_share, nfs_fstype)->shareopts = NULL;
172 }
173 
174 /*
175  * Commit the shares by restarting mountd.
176  */
177 static int
178 nfs_commit_shares(void)
179 {
180 	struct pidfh *pfh;
181 	pid_t mountdpid;
182 
183 start:
184 	pfh = pidfile_open(_PATH_MOUNTDPID, 0600, &mountdpid);
185 	if (pfh != NULL) {
186 		/* mountd(8) is not running. */
187 		pidfile_remove(pfh);
188 		return (SA_OK);
189 	}
190 	if (errno != EEXIST) {
191 		/* Cannot open pidfile for some reason. */
192 		return (SA_SYSTEM_ERR);
193 	}
194 	if (mountdpid == -1) {
195 		/* mountd(8) exists, but didn't write the PID yet */
196 		usleep(500);
197 		goto start;
198 	}
199 	/* We have mountd(8) PID in mountdpid variable. */
200 	kill(mountdpid, SIGHUP);
201 	return (SA_OK);
202 }
203 
204 static const sa_share_ops_t nfs_shareops = {
205 	.enable_share = nfs_enable_share,
206 	.disable_share = nfs_disable_share,
207 	.is_shared = nfs_is_shared,
208 
209 	.validate_shareopts = nfs_validate_shareopts,
210 	.update_shareopts = nfs_update_shareopts,
211 	.clear_shareopts = nfs_clear_shareopts,
212 	.commit_shares = nfs_commit_shares,
213 };
214 
215 /*
216  * Initializes the NFS functionality of libshare.
217  */
218 void
219 libshare_nfs_init(void)
220 {
221 	nfs_fstype = register_fstype("nfs", &nfs_shareops);
222 }
223