]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/contrib/openzfs/lib/libshare/os/freebsd/nfs.c
OpenZFS: MFV 2.0-rc3-gfc5966
[FreeBSD/FreeBSD.git] / sys / contrib / openzfs / lib / libshare / os / freebsd / nfs.c
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         FILE *oldfp = fopen(ZFS_EXPORTS_FILE, "r");
232         FILE *newfp = fopen(filename, "w+");
233         if (newfp == NULL) {
234                 fprintf(stderr, "failed to open %s file: %s", filename,
235                     strerror(errno));
236                 fclose(oldfp);
237                 return (SA_SYSTEM_ERR);
238         }
239         fputs(FILE_HEADER, newfp);
240
241         /*
242          * The ZFS_EXPORTS_FILE may not exist yet. If that's the
243          * case then just write out the new file.
244          */
245         if (oldfp != NULL) {
246                 while ((line = zgetline(oldfp, mountpoint)) != NULL)
247                         fprintf(newfp, "%s\n", line);
248                 if (ferror(oldfp) != 0) {
249                         error = ferror(oldfp);
250                 }
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;
255                 }
256         }
257
258         if (error == 0 && ferror(newfp) != 0) {
259                 error = ferror(newfp);
260         }
261
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;
266         }
267         return (error);
268 }
269
270 static int
271 nfs_enable_share(sa_share_impl_t impl_share)
272 {
273         char *filename = NULL;
274         int error;
275
276         if ((filename = nfs_init_tmpfile()) == NULL)
277                 return (SA_SYSTEM_ERR);
278
279         error = nfs_exports_lock();
280         if (error != 0) {
281                 unlink(filename);
282                 free(filename);
283                 return (error);
284         }
285
286         error = nfs_copy_entries(filename, impl_share->sa_mountpoint);
287         if (error != SA_OK) {
288                 unlink(filename);
289                 free(filename);
290                 nfs_exports_unlock();
291                 return (error);
292         }
293
294         FILE *fp = fopen(filename, "a+");
295         if (fp == NULL) {
296                 fprintf(stderr, "failed to open %s file: %s", filename,
297                     strerror(errno));
298                 unlink(filename);
299                 free(filename);
300                 nfs_exports_unlock();
301                 return (SA_SYSTEM_ERR);
302         }
303         char *shareopts = FSINFO(impl_share, nfs_fstype)->shareopts;
304         if (strcmp(shareopts, "on") == 0)
305                 shareopts = "";
306
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);
310                 fclose(fp);
311                 unlink(filename);
312                 free(filename);
313                 nfs_exports_unlock();
314                 return (SA_SYSTEM_ERR);
315         }
316
317         if (fclose(fp) != 0) {
318                 fprintf(stderr, "Unable to close file %s: %s\n",
319                     filename, strerror(errno));
320                 unlink(filename);
321                 free(filename);
322                 nfs_exports_unlock();
323                 return (SA_SYSTEM_ERR);
324         }
325         error = nfs_fini_tmpfile(filename);
326         nfs_exports_unlock();
327         return (error);
328 }
329
330 static int
331 nfs_disable_share(sa_share_impl_t impl_share)
332 {
333         int error;
334         char *filename = NULL;
335
336         if ((filename = nfs_init_tmpfile()) == NULL)
337                 return (SA_SYSTEM_ERR);
338
339         error = nfs_exports_lock();
340         if (error != 0) {
341                 unlink(filename);
342                 free(filename);
343                 return (error);
344         }
345
346         error = nfs_copy_entries(filename, impl_share->sa_mountpoint);
347         if (error != SA_OK) {
348                 unlink(filename);
349                 free(filename);
350                 nfs_exports_unlock();
351                 return (error);
352         }
353
354         error = nfs_fini_tmpfile(filename);
355         nfs_exports_unlock();
356         return (error);
357 }
358
359 /*
360  * NOTE: This function returns a static buffer and thus is not thread-safe.
361  */
362 static boolean_t
363 nfs_is_shared(sa_share_impl_t impl_share)
364 {
365         static char line[MAXLINESIZE];
366         char *s, last;
367         size_t len;
368         char *mntpoint = impl_share->sa_mountpoint;
369         size_t mntlen = strlen(mntpoint);
370
371         FILE *fp = fopen(ZFS_EXPORTS_FILE, "r");
372         if (fp == NULL)
373                 return (B_FALSE);
374
375         for (;;) {
376                 s = fgets(line, sizeof (line), fp);
377                 if (s == NULL)
378                         return (B_FALSE);
379                 /* Skip empty lines and comments. */
380                 if (line[0] == '\n' || line[0] == '#')
381                         continue;
382                 len = strlen(line);
383                 if (line[len - 1] == '\n')
384                         line[len - 1] = '\0';
385                 last = line[mntlen];
386                 /* Skip the given mountpoint. */
387                 if (strncmp(mntpoint, line, mntlen) == 0 &&
388                     (last == '\t' || last == ' ' || last == '\0')) {
389                         fclose(fp);
390                         return (B_TRUE);
391                 }
392         }
393         fclose(fp);
394         return (B_FALSE);
395 }
396
397 static int
398 nfs_validate_shareopts(const char *shareopts)
399 {
400         return (SA_OK);
401 }
402
403 static int
404 nfs_update_shareopts(sa_share_impl_t impl_share, const char *shareopts)
405 {
406         FSINFO(impl_share, nfs_fstype)->shareopts = (char *)shareopts;
407         return (SA_OK);
408 }
409
410 static void
411 nfs_clear_shareopts(sa_share_impl_t impl_share)
412 {
413         FSINFO(impl_share, nfs_fstype)->shareopts = NULL;
414 }
415
416 /*
417  * Commit the shares by restarting mountd.
418  */
419 static int
420 nfs_commit_shares(void)
421 {
422         struct pidfh *pfh;
423         pid_t mountdpid;
424
425         pfh = pidfile_open(_PATH_MOUNTDPID, 0600, &mountdpid);
426         if (pfh != NULL) {
427                 /* Mountd is not running. */
428                 pidfile_remove(pfh);
429                 return (SA_OK);
430         }
431         if (errno != EEXIST) {
432                 /* Cannot open pidfile for some reason. */
433                 return (SA_SYSTEM_ERR);
434         }
435         /* We have mountd(8) PID in mountdpid variable. */
436         kill(mountdpid, SIGHUP);
437         return (SA_OK);
438 }
439
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,
444
445         .validate_shareopts = nfs_validate_shareopts,
446         .update_shareopts = nfs_update_shareopts,
447         .clear_shareopts = nfs_clear_shareopts,
448         .commit_shares = nfs_commit_shares,
449 };
450
451 /*
452  * Initializes the NFS functionality of libshare.
453  */
454 void
455 libshare_nfs_init(void)
456 {
457         nfs_fstype = register_fstype("nfs", &nfs_shareops);
458 }