1 // SPDX-License-Identifier: BSD-2-Clause
2 /*
3 * Copyright (c) 2007 Pawel Jakub Dawidek <pjd@FreeBSD.org>
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 *
27 * Copyright (c) 2020, 2022 by Delphix. All rights reserved.
28 */
29
30 #include <sys/param.h>
31 #include <sys/vfs.h>
32
33 #include <assert.h>
34 #include <errno.h>
35 #include <fcntl.h>
36 #include <libutil.h>
37 #include <signal.h>
38 #include <stdio.h>
39 #include <string.h>
40 #include <unistd.h>
41 #include <libintl.h>
42
43 #include <libshare.h>
44 #include "libshare_impl.h"
45 #include "nfs.h"
46
47 #define _PATH_MOUNTDPID "/var/run/mountd.pid"
48 #define ZFS_EXPORTS_FILE "/etc/zfs/exports"
49 #define ZFS_EXPORTS_LOCK ZFS_EXPORTS_FILE".lock"
50
51 /*
52 * This function translates options to a format acceptable by exports(5), eg.
53 *
54 * -ro -network=192.168.0.0 -mask=255.255.255.0 -maproot=0 \
55 * zfs.freebsd.org 69.147.83.54
56 *
57 * Accepted input formats:
58 *
59 * ro,network=192.168.0.0,mask=255.255.255.0,maproot=0,zfs.freebsd.org
60 * ro network=192.168.0.0 mask=255.255.255.0 maproot=0 zfs.freebsd.org
61 * -ro,-network=192.168.0.0,-mask=255.255.255.0,-maproot=0,zfs.freebsd.org
62 * -ro -network=192.168.0.0 -mask=255.255.255.0 -maproot=0 \
63 * zfs.freebsd.org
64 *
65 * Recognized keywords:
66 *
67 * ro, maproot, mapall, mask, network, sec, alldirs, public, webnfs,
68 * index, quiet
69 */
70 static int
translate_opts(char * oldopts,FILE * out)71 translate_opts(char *oldopts, FILE *out)
72 {
73 static const char *const known_opts[] = { "ro", "maproot", "mapall",
74 "mask", "network", "sec", "alldirs", "public", "webnfs", "index",
75 "quiet" };
76 char *newopts, *o, *s = NULL;
77 unsigned int i;
78 size_t len, newopts_len;
79 int ret;
80
81 /*
82 * Calculate the length needed for the worst case of a single
83 * character option:
84 * - Add one to strlen(oldopts) so that the trailing nul is counted
85 * as a separator.
86 * - Multiply by 3/2 since the single character option plus separator
87 * is expanded to 3 characters.
88 * - Add one for the trailing nul. Needed for a single repetition of
89 * the single character option and certain other cases.
90 */
91 newopts_len = (strlen(oldopts) + 1) * 3 / 2 + 1;
92 newopts = malloc(newopts_len);
93 if (newopts == NULL)
94 return (EOF);
95 newopts[0] = '\0';
96 s = oldopts;
97 while ((o = strsep(&s, ", ")) != NULL) {
98 if (o[0] == '-')
99 o++;
100 if (o[0] == '\0')
101 continue;
102 for (i = 0; i < ARRAY_SIZE(known_opts); ++i) {
103 len = strlen(known_opts[i]);
104 if (strncmp(known_opts[i], o, len) == 0 &&
105 (o[len] == '\0' || o[len] == '=')) {
106 strlcat(newopts, "-", newopts_len);
107 break;
108 }
109 }
110 strlcat(newopts, o, newopts_len);
111 strlcat(newopts, " ", newopts_len);
112 }
113 ret = fputs(newopts, out);
114 free(newopts);
115 return (ret);
116 }
117
118 static int
nfs_enable_share_impl(sa_share_impl_t impl_share,FILE * tmpfile)119 nfs_enable_share_impl(sa_share_impl_t impl_share, FILE *tmpfile)
120 {
121 const char *shareopts = impl_share->sa_shareopts;
122 if (strcmp(shareopts, "on") == 0)
123 shareopts = "";
124
125 boolean_t need_free, fnd_semi;
126 char *mp, *lineopts, *exportopts, *s;
127 size_t whitelen;
128 int rc = nfs_escape_mountpoint(impl_share->sa_mountpoint, &mp,
129 &need_free);
130 if (rc != SA_OK)
131 return (rc);
132
133 lineopts = strdup(shareopts);
134 if (lineopts == NULL)
135 return (SA_SYSTEM_ERR);
136 s = lineopts;
137 fnd_semi = B_FALSE;
138 while ((exportopts = strsep(&s, ";")) != NULL) {
139 if (s != NULL)
140 fnd_semi = B_TRUE;
141 /* Ignore only whitespace between ';' separated option sets. */
142 if (fnd_semi) {
143 whitelen = strspn(exportopts, "\t ");
144 if (exportopts[whitelen] == '\0')
145 continue;
146 }
147 if (fputs(mp, tmpfile) == EOF ||
148 fputc('\t', tmpfile) == EOF ||
149 translate_opts(exportopts, tmpfile) == EOF ||
150 fputc('\n', tmpfile) == EOF) {
151 fprintf(stderr, "failed to write to temporary file\n");
152 rc = SA_SYSTEM_ERR;
153 break;
154 }
155 }
156 free(lineopts);
157
158 if (need_free)
159 free(mp);
160 return (rc);
161 }
162
163 static int
nfs_enable_share(sa_share_impl_t impl_share)164 nfs_enable_share(sa_share_impl_t impl_share)
165 {
166 return (nfs_toggle_share(
167 ZFS_EXPORTS_LOCK, ZFS_EXPORTS_FILE, NULL, impl_share,
168 nfs_enable_share_impl));
169 }
170
171 static int
nfs_disable_share_impl(sa_share_impl_t impl_share,FILE * tmpfile)172 nfs_disable_share_impl(sa_share_impl_t impl_share, FILE *tmpfile)
173 {
174 (void) impl_share, (void) tmpfile;
175 return (SA_OK);
176 }
177
178 static int
nfs_disable_share(sa_share_impl_t impl_share)179 nfs_disable_share(sa_share_impl_t impl_share)
180 {
181 return (nfs_toggle_share(
182 ZFS_EXPORTS_LOCK, ZFS_EXPORTS_FILE, NULL, impl_share,
183 nfs_disable_share_impl));
184 }
185
186 static boolean_t
nfs_is_shared(sa_share_impl_t impl_share)187 nfs_is_shared(sa_share_impl_t impl_share)
188 {
189 return (nfs_is_shared_impl(ZFS_EXPORTS_FILE, impl_share));
190 }
191
192 static int
nfs_validate_shareopts(const char * shareopts)193 nfs_validate_shareopts(const char *shareopts)
194 {
195 if (strlen(shareopts) == 0)
196 return (SA_SYNTAX_ERR);
197 return (SA_OK);
198 }
199
200 /*
201 * Commit the shares by restarting mountd.
202 */
203 static int
nfs_commit_shares(void)204 nfs_commit_shares(void)
205 {
206 struct pidfh *pfh;
207 pid_t mountdpid;
208
209 start:
210 pfh = pidfile_open(_PATH_MOUNTDPID, 0600, &mountdpid);
211 if (pfh != NULL) {
212 /* mountd(8) is not running. */
213 pidfile_remove(pfh);
214 return (SA_OK);
215 }
216 if (errno != EEXIST) {
217 /* Cannot open pidfile for some reason. */
218 return (SA_SYSTEM_ERR);
219 }
220 if (mountdpid == -1) {
221 /* mountd(8) exists, but didn't write the PID yet */
222 usleep(500);
223 goto start;
224 }
225 /* We have mountd(8) PID in mountdpid variable. */
226 kill(mountdpid, SIGHUP);
227 return (SA_OK);
228 }
229
230 static void
nfs_truncate_shares(void)231 nfs_truncate_shares(void)
232 {
233 nfs_reset_shares(ZFS_EXPORTS_LOCK, ZFS_EXPORTS_FILE);
234 }
235
236 const sa_fstype_t libshare_nfs_type = {
237 .enable_share = nfs_enable_share,
238 .disable_share = nfs_disable_share,
239 .is_shared = nfs_is_shared,
240
241 .validate_shareopts = nfs_validate_shareopts,
242 .commit_shares = nfs_commit_shares,
243 .truncate_shares = nfs_truncate_shares,
244 };
245