xref: /freebsd-src/sys/contrib/openzfs/lib/libshare/os/freebsd/nfs.c (revision 16d6b3b3da62aa5baaf3c66c8d4e6f8c8f70aeb7)
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 "libzfs_impl.h"
46 #include "libshare_impl.h"
47 #include "nfs.h"
48 
49 #define	_PATH_MOUNTDPID	"/var/run/mountd.pid"
50 #define	FILE_HEADER	"# !!! DO NOT EDIT THIS FILE MANUALLY !!!\n\n"
51 #define	OPTSSIZE	1024
52 #define	MAXLINESIZE	(PATH_MAX + OPTSSIZE)
53 #define	ZFS_EXPORTS_FILE	"/etc/zfs/exports"
54 #define	ZFS_EXPORTS_LOCK	ZFS_EXPORTS_FILE".lock"
55 
56 static sa_fstype_t *nfs_fstype;
57 
58 static int nfs_lock_fd = -1;
59 
60 /*
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.
64  */
65 static int
66 nfs_exports_lock(void)
67 {
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));
73 		return (errno);
74 	}
75 	if (flock(nfs_lock_fd, LOCK_EX) != 0) {
76 		fprintf(stderr, "failed to lock %s: %s\n",
77 		    ZFS_EXPORTS_LOCK, strerror(errno));
78 		return (errno);
79 	}
80 	return (0);
81 }
82 
83 static void
84 nfs_exports_unlock(void)
85 {
86 	verify(nfs_lock_fd > 0);
87 
88 	if (flock(nfs_lock_fd, LOCK_UN) != 0) {
89 		fprintf(stderr, "failed to unlock %s: %s\n",
90 		    ZFS_EXPORTS_LOCK, strerror(errno));
91 	}
92 	close(nfs_lock_fd);
93 	nfs_lock_fd = -1;
94 }
95 
96 /*
97  * Read one line from a file. Skip comments, empty lines and a line with a
98  * mountpoint specified in the 'skip' argument.
99  *
100  * NOTE: This function returns a static buffer and thus is not thread-safe.
101  */
102 static char *
103 zgetline(FILE *fd, const char *skip)
104 {
105 	static char line[MAXLINESIZE];
106 	size_t len, skiplen = 0;
107 	char *s, last;
108 
109 	if (skip != NULL)
110 		skiplen = strlen(skip);
111 	for (;;) {
112 		s = fgets(line, sizeof (line), fd);
113 		if (s == NULL)
114 			return (NULL);
115 		/* Skip empty lines and comments. */
116 		if (line[0] == '\n' || line[0] == '#')
117 			continue;
118 		len = strlen(line);
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')) {
125 			continue;
126 		}
127 		break;
128 	}
129 	return (line);
130 }
131 
132 /*
133  * This function translate options to a format acceptable by exports(5), eg.
134  *
135  *	-ro -network=192.168.0.0 -mask=255.255.255.0 -maproot=0 \
136  *	zfs.freebsd.org 69.147.83.54
137  *
138  * Accepted input formats:
139  *
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 \
144  *	zfs.freebsd.org
145  *
146  * Recognized keywords:
147  *
148  *	ro, maproot, mapall, mask, network, sec, alldirs, public, webnfs,
149  *	index, quiet
150  *
151  * NOTE: This function returns a static buffer and thus is not thread-safe.
152  */
153 static char *
154 translate_opts(const char *shareopts)
155 {
156 	static const char *known_opts[] = { "ro", "maproot", "mapall", "mask",
157 	    "network", "sec", "alldirs", "public", "webnfs", "index", "quiet",
158 	    NULL };
159 	static char newopts[OPTSSIZE];
160 	char oldopts[OPTSSIZE];
161 	char *o, *s = NULL;
162 	unsigned int i;
163 	size_t len;
164 
165 	strlcpy(oldopts, shareopts, sizeof (oldopts));
166 	newopts[0] = '\0';
167 	s = oldopts;
168 	while ((o = strsep(&s, "-, ")) != NULL) {
169 		if (o[0] == '\0')
170 			continue;
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));
176 				break;
177 			}
178 		}
179 		strlcat(newopts, o, sizeof (newopts));
180 		strlcat(newopts, " ", sizeof (newopts));
181 	}
182 	return (newopts);
183 }
184 
185 static char *
186 nfs_init_tmpfile(void)
187 {
188 	char *tmpfile = NULL;
189 
190 	if (asprintf(&tmpfile, "%s%s", ZFS_EXPORTS_FILE, ".XXXXXXXX") == -1) {
191 		fprintf(stderr, "Unable to allocate buffer for temporary "
192 		    "file name\n");
193 		return (NULL);
194 	}
195 
196 	int fd = mkstemp(tmpfile);
197 	if (fd == -1) {
198 		fprintf(stderr, "Unable to create temporary file: %s",
199 		    strerror(errno));
200 		free(tmpfile);
201 		return (NULL);
202 	}
203 	close(fd);
204 	return (tmpfile);
205 }
206 
207 static int
208 nfs_fini_tmpfile(char *tmpfile)
209 {
210 	if (rename(tmpfile, ZFS_EXPORTS_FILE) == -1) {
211 		fprintf(stderr, "Unable to rename %s: %s\n", tmpfile,
212 		    strerror(errno));
213 		unlink(tmpfile);
214 		free(tmpfile);
215 		return (SA_SYSTEM_ERR);
216 	}
217 	free(tmpfile);
218 	return (SA_OK);
219 }
220 
221 /*
222  * This function copies all entries from the exports file to "filename",
223  * omitting any entries for the specified mountpoint.
224  */
225 static int
226 nfs_copy_entries(char *filename, const char *mountpoint)
227 {
228 	int error = SA_OK;
229 	char *line;
230 
231 	/*
232 	 * If the file doesn't exist then there is nothing more
233 	 * we need to do.
234 	 */
235 	FILE *oldfp = fopen(ZFS_EXPORTS_FILE, "r");
236 	if (oldfp == NULL)
237 		return (SA_OK);
238 
239 	FILE *newfp = fopen(filename, "w+");
240 	fputs(FILE_HEADER, newfp);
241 	while ((line = zgetline(oldfp, mountpoint)) != NULL)
242 		fprintf(newfp, "%s\n", line);
243 	if (ferror(oldfp) != 0) {
244 		error = ferror(oldfp);
245 	}
246 	if (error == 0 && ferror(newfp) != 0) {
247 		error = ferror(newfp);
248 	}
249 
250 	if (fclose(newfp) != 0) {
251 		fprintf(stderr, "Unable to close file %s: %s\n",
252 		    filename, strerror(errno));
253 		error = error != 0 ? error : SA_SYSTEM_ERR;
254 	}
255 	fclose(oldfp);
256 
257 	return (error);
258 }
259 
260 static int
261 nfs_enable_share(sa_share_impl_t impl_share)
262 {
263 	char *filename = NULL;
264 	int error;
265 
266 	if ((filename = nfs_init_tmpfile()) == NULL)
267 		return (SA_SYSTEM_ERR);
268 
269 	error = nfs_exports_lock();
270 	if (error != 0) {
271 		unlink(filename);
272 		free(filename);
273 		return (error);
274 	}
275 
276 	error = nfs_copy_entries(filename, impl_share->sa_mountpoint);
277 	if (error != SA_OK) {
278 		unlink(filename);
279 		free(filename);
280 		nfs_exports_unlock();
281 		return (error);
282 	}
283 
284 	FILE *fp = fopen(filename, "a+");
285 	if (fp == NULL) {
286 		fprintf(stderr, "failed to open %s file: %s", filename,
287 		    strerror(errno));
288 		unlink(filename);
289 		free(filename);
290 		nfs_exports_unlock();
291 		return (SA_SYSTEM_ERR);
292 	}
293 	char *shareopts = FSINFO(impl_share, nfs_fstype)->shareopts;
294 	if (strcmp(shareopts, "on") == 0)
295 		shareopts = "";
296 
297 	if (fprintf(fp, "%s\t%s\n", impl_share->sa_mountpoint,
298 	    translate_opts(shareopts)) < 0) {
299 		fprintf(stderr, "failed to write to %s\n", filename);
300 		fclose(fp);
301 		unlink(filename);
302 		free(filename);
303 		nfs_exports_unlock();
304 		return (SA_SYSTEM_ERR);
305 	}
306 
307 	if (fclose(fp) != 0) {
308 		fprintf(stderr, "Unable to close file %s: %s\n",
309 		    filename, strerror(errno));
310 		unlink(filename);
311 		free(filename);
312 		nfs_exports_unlock();
313 		return (SA_SYSTEM_ERR);
314 	}
315 	error = nfs_fini_tmpfile(filename);
316 	nfs_exports_unlock();
317 	return (error);
318 }
319 
320 static int
321 nfs_disable_share(sa_share_impl_t impl_share)
322 {
323 	int error;
324 	char *filename = NULL;
325 
326 	if ((filename = nfs_init_tmpfile()) == NULL)
327 		return (SA_SYSTEM_ERR);
328 
329 	error = nfs_exports_lock();
330 	if (error != 0) {
331 		unlink(filename);
332 		free(filename);
333 		return (error);
334 	}
335 
336 	error = nfs_copy_entries(filename, impl_share->sa_mountpoint);
337 	if (error != SA_OK) {
338 		unlink(filename);
339 		free(filename);
340 		nfs_exports_unlock();
341 		return (error);
342 	}
343 
344 	error = nfs_fini_tmpfile(filename);
345 	nfs_exports_unlock();
346 	return (error);
347 }
348 
349 /*
350  * NOTE: This function returns a static buffer and thus is not thread-safe.
351  */
352 static boolean_t
353 nfs_is_shared(sa_share_impl_t impl_share)
354 {
355 	static char line[MAXLINESIZE];
356 	char *s, last;
357 	size_t len;
358 	char *mntpoint = impl_share->sa_mountpoint;
359 	size_t mntlen = strlen(mntpoint);
360 
361 	FILE *fp = fopen(ZFS_EXPORTS_FILE, "r");
362 	if (fp == NULL)
363 		return (B_FALSE);
364 
365 	for (;;) {
366 		s = fgets(line, sizeof (line), fp);
367 		if (s == NULL)
368 			return (B_FALSE);
369 		/* Skip empty lines and comments. */
370 		if (line[0] == '\n' || line[0] == '#')
371 			continue;
372 		len = strlen(line);
373 		if (line[len - 1] == '\n')
374 			line[len - 1] = '\0';
375 		last = line[mntlen];
376 		/* Skip the given mountpoint. */
377 		if (strncmp(mntpoint, line, mntlen) == 0 &&
378 		    (last == '\t' || last == ' ' || last == '\0')) {
379 			fclose(fp);
380 			return (B_TRUE);
381 		}
382 	}
383 	fclose(fp);
384 	return (B_FALSE);
385 }
386 
387 static int
388 nfs_validate_shareopts(const char *shareopts)
389 {
390 	return (SA_OK);
391 }
392 
393 static int
394 nfs_update_shareopts(sa_share_impl_t impl_share, const char *shareopts)
395 {
396 	FSINFO(impl_share, nfs_fstype)->shareopts = (char *)shareopts;
397 	return (SA_OK);
398 }
399 
400 static void
401 nfs_clear_shareopts(sa_share_impl_t impl_share)
402 {
403 	FSINFO(impl_share, nfs_fstype)->shareopts = NULL;
404 }
405 
406 /*
407  * Commit the shares by restarting mountd.
408  */
409 static int
410 nfs_commit_shares(void)
411 {
412 	struct pidfh *pfh;
413 	pid_t mountdpid;
414 
415 	pfh = pidfile_open(_PATH_MOUNTDPID, 0600, &mountdpid);
416 	if (pfh != NULL) {
417 		/* Mountd is not running. */
418 		pidfile_remove(pfh);
419 		return (SA_OK);
420 	}
421 	if (errno != EEXIST) {
422 		/* Cannot open pidfile for some reason. */
423 		return (SA_SYSTEM_ERR);
424 	}
425 	/* We have mountd(8) PID in mountdpid variable. */
426 	kill(mountdpid, SIGHUP);
427 	return (SA_OK);
428 }
429 
430 static const sa_share_ops_t nfs_shareops = {
431 	.enable_share = nfs_enable_share,
432 	.disable_share = nfs_disable_share,
433 	.is_shared = nfs_is_shared,
434 
435 	.validate_shareopts = nfs_validate_shareopts,
436 	.update_shareopts = nfs_update_shareopts,
437 	.clear_shareopts = nfs_clear_shareopts,
438 	.commit_shares = nfs_commit_shares,
439 };
440 
441 /*
442  * Initializes the NFS functionality of libshare.
443  */
444 void
445 libshare_nfs_init(void)
446 {
447 	nfs_fstype = register_fstype("nfs", &nfs_shareops);
448 }
449