1 // SPDX-License-Identifier: CDDL-1.0
2 /*
3 * This file and its contents are supplied under the terms of the
4 * Common Development and Distribution License ("CDDL"), version 1.0.
5 * You may only use this file in accordance with the terms of version
6 * 1.0 of the CDDL.
7 *
8 * A full copy of the text of the CDDL should have accompanied this
9 * source. A copy of the CDDL is also available via the Internet at
10 * https://opensource.org/license/CDDL-1.0.
11 */
12
13 /*
14 * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
15 * Copyright (c) 2011 Gunnar Beutner
16 * Copyright (c) 2012 Cyril Plisko. All rights reserved.
17 * Copyright (c) 2019, 2022 by Delphix. All rights reserved.
18 */
19
20 #include <dirent.h>
21 #include <stdio.h>
22 #include <string.h>
23 #include <errno.h>
24 #include <fcntl.h>
25 #include <sys/file.h>
26 #include <sys/stat.h>
27 #include <sys/types.h>
28 #include <sys/wait.h>
29 #include <unistd.h>
30 #include <libzfs.h>
31 #include "../../libzfs_impl.h"
32
33 #define ZFS_EXPORTS_DIR "/etc/exports.d"
34 #define ZFS_EXPORTS_FILE ZFS_EXPORTS_DIR"/zfs.exports"
35 #define ZFS_EXPORTS_LOCK ZFS_EXPORTS_FILE".lock"
36
37
38 static boolean_t nfs_available(void);
39 static boolean_t exports_available(void);
40
41 typedef int (*nfs_shareopt_callback_t)(const char *opt, const char *value,
42 void *cookie);
43
44 typedef int (*nfs_host_callback_t)(FILE *tmpfile, const char *sharepath,
45 const char *host, const char *security, const char *access, void *cookie);
46
47 /*
48 * Invokes the specified callback function for each Solaris share option
49 * listed in the specified string.
50 */
51 static int
foreach_nfs_shareopt(const char * shareopts,nfs_shareopt_callback_t callback,void * cookie)52 foreach_nfs_shareopt(const char *shareopts,
53 nfs_shareopt_callback_t callback, void *cookie)
54 {
55 char *shareopts_dup, *opt, *cur, *value;
56 int was_nul, error;
57
58 if (shareopts == NULL)
59 return (SA_OK);
60
61 if (strcmp(shareopts, "on") == 0)
62 shareopts = "rw,crossmnt";
63
64 shareopts_dup = strdup(shareopts);
65
66
67 if (shareopts_dup == NULL)
68 return (SA_NO_MEMORY);
69
70 opt = shareopts_dup;
71 was_nul = 0;
72
73 while (1) {
74 cur = opt;
75
76 while (*cur != ',' && *cur != '\0')
77 cur++;
78
79 if (*cur == '\0')
80 was_nul = 1;
81
82 *cur = '\0';
83
84 if (cur > opt) {
85 value = strchr(opt, '=');
86
87 if (value != NULL) {
88 *value = '\0';
89 value++;
90 }
91
92 error = callback(opt, value, cookie);
93
94 if (error != SA_OK) {
95 free(shareopts_dup);
96 return (error);
97 }
98 }
99
100 opt = cur + 1;
101
102 if (was_nul)
103 break;
104 }
105
106 free(shareopts_dup);
107
108 return (SA_OK);
109 }
110
111 typedef struct nfs_host_cookie_s {
112 nfs_host_callback_t callback;
113 const char *sharepath;
114 void *cookie;
115 FILE *tmpfile;
116 const char *security;
117 } nfs_host_cookie_t;
118
119 /*
120 * Helper function for foreach_nfs_host. This function checks whether the
121 * current share option is a host specification and invokes a callback
122 * function with information about the host.
123 */
124 static int
foreach_nfs_host_cb(const char * opt,const char * value,void * pcookie)125 foreach_nfs_host_cb(const char *opt, const char *value, void *pcookie)
126 {
127 int error;
128 const char *access;
129 char *host_dup, *host, *next, *v6Literal;
130 nfs_host_cookie_t *udata = (nfs_host_cookie_t *)pcookie;
131 int cidr_len;
132
133 #ifdef DEBUG
134 fprintf(stderr, "foreach_nfs_host_cb: key=%s, value=%s\n", opt, value);
135 #endif
136
137 if (strcmp(opt, "sec") == 0)
138 udata->security = value;
139
140 if (strcmp(opt, "rw") == 0 || strcmp(opt, "ro") == 0) {
141 if (value == NULL)
142 value = "*";
143
144 access = opt;
145
146 host_dup = strdup(value);
147
148 if (host_dup == NULL)
149 return (SA_NO_MEMORY);
150
151 host = host_dup;
152
153 do {
154 if (*host == '[') {
155 host++;
156 v6Literal = strchr(host, ']');
157 if (v6Literal == NULL) {
158 free(host_dup);
159 return (SA_SYNTAX_ERR);
160 }
161 if (v6Literal[1] == '\0') {
162 *v6Literal = '\0';
163 next = NULL;
164 } else if (v6Literal[1] == '/') {
165 next = strchr(v6Literal + 2, ':');
166 if (next == NULL) {
167 cidr_len =
168 strlen(v6Literal + 1);
169 memmove(v6Literal,
170 v6Literal + 1,
171 cidr_len);
172 v6Literal[cidr_len] = '\0';
173 } else {
174 cidr_len = next - v6Literal - 1;
175 memmove(v6Literal,
176 v6Literal + 1,
177 cidr_len);
178 v6Literal[cidr_len] = '\0';
179 next++;
180 }
181 } else if (v6Literal[1] == ':') {
182 *v6Literal = '\0';
183 next = v6Literal + 2;
184 } else {
185 free(host_dup);
186 return (SA_SYNTAX_ERR);
187 }
188 } else {
189 next = strchr(host, ':');
190 if (next != NULL) {
191 *next = '\0';
192 next++;
193 }
194 }
195
196 error = udata->callback(udata->tmpfile,
197 udata->sharepath, host, udata->security,
198 access, udata->cookie);
199
200 if (error != SA_OK) {
201 free(host_dup);
202
203 return (error);
204 }
205
206 host = next;
207 } while (host != NULL);
208
209 free(host_dup);
210 }
211
212 return (SA_OK);
213 }
214
215 /*
216 * Invokes a callback function for all NFS hosts that are set for a share.
217 */
218 static int
foreach_nfs_host(sa_share_impl_t impl_share,FILE * tmpfile,nfs_host_callback_t callback,void * cookie)219 foreach_nfs_host(sa_share_impl_t impl_share, FILE *tmpfile,
220 nfs_host_callback_t callback, void *cookie)
221 {
222 nfs_host_cookie_t udata;
223
224 udata.callback = callback;
225 udata.sharepath = impl_share->sa_mountpoint;
226 udata.cookie = cookie;
227 udata.tmpfile = tmpfile;
228 udata.security = "sys";
229
230 return (foreach_nfs_shareopt(impl_share->sa_shareopts,
231 foreach_nfs_host_cb, &udata));
232 }
233
234 /*
235 * Converts a Solaris NFS host specification to its Linux equivalent.
236 */
237 static const char *
get_linux_hostspec(const char * solaris_hostspec)238 get_linux_hostspec(const char *solaris_hostspec)
239 {
240 /*
241 * For now we just support CIDR masks (e.g. @192.168.0.0/16) and host
242 * wildcards (e.g. *.example.org).
243 */
244 if (solaris_hostspec[0] == '@') {
245 /*
246 * Solaris host specifier, e.g. @192.168.0.0/16; we just need
247 * to skip the @ in this case
248 */
249 return (solaris_hostspec + 1);
250 } else {
251 return (solaris_hostspec);
252 }
253 }
254
255 /*
256 * Adds a Linux share option to an array of NFS options.
257 */
258 static int
add_linux_shareopt(char ** plinux_opts,const char * key,const char * value)259 add_linux_shareopt(char **plinux_opts, const char *key, const char *value)
260 {
261 size_t len = 0;
262 char *new_linux_opts;
263
264 if (*plinux_opts != NULL)
265 len = strlen(*plinux_opts);
266
267 new_linux_opts = realloc(*plinux_opts, len + 1 + strlen(key) +
268 (value ? 1 + strlen(value) : 0) + 1);
269
270 if (new_linux_opts == NULL)
271 return (SA_NO_MEMORY);
272
273 new_linux_opts[len] = '\0';
274
275 if (len > 0)
276 strcat(new_linux_opts, ",");
277
278 strcat(new_linux_opts, key);
279
280 if (value != NULL) {
281 strcat(new_linux_opts, "=");
282 strcat(new_linux_opts, value);
283 }
284
285 *plinux_opts = new_linux_opts;
286
287 return (SA_OK);
288 }
289
string_cmp(const void * lhs,const void * rhs)290 static int string_cmp(const void *lhs, const void *rhs) {
291 const char *const *l = lhs, *const *r = rhs;
292 return (strcmp(*l, *r));
293 }
294
295 /*
296 * Validates and converts a single Solaris share option to its Linux
297 * equivalent.
298 */
299 static int
get_linux_shareopts_cb(const char * key,const char * value,void * cookie)300 get_linux_shareopts_cb(const char *key, const char *value, void *cookie)
301 {
302 /* This list must remain sorted, since we bsearch() it */
303 static const char *const valid_keys[] = { "all_squash", "anongid",
304 "anonuid", "async", "auth_nlm", "crossmnt", "fsid", "fsuid", "hide",
305 "insecure", "insecure_locks", "mountpoint", "mp", "no_acl",
306 "no_all_squash", "no_auth_nlm", "no_root_squash",
307 "no_subtree_check", "no_wdelay", "nohide", "refer", "replicas",
308 "root_squash", "secure", "secure_locks", "subtree_check", "sync",
309 "wdelay" };
310
311 char **plinux_opts = (char **)cookie;
312 char *host, *val_dup, *literal, *next;
313
314 if (strcmp(key, "sec") == 0)
315 return (SA_OK);
316
317 if (strcmp(key, "ro") == 0 || strcmp(key, "rw") == 0) {
318 if (value == NULL || strlen(value) == 0)
319 return (SA_OK);
320 val_dup = strdup(value);
321 host = val_dup;
322 if (host == NULL)
323 return (SA_NO_MEMORY);
324 do {
325 if (*host == '[') {
326 host++;
327 literal = strchr(host, ']');
328 if (literal == NULL) {
329 free(val_dup);
330 return (SA_SYNTAX_ERR);
331 }
332 if (literal[1] == '\0')
333 next = NULL;
334 else if (literal[1] == '/') {
335 next = strchr(literal + 2, ':');
336 if (next != NULL)
337 ++next;
338 } else if (literal[1] == ':')
339 next = literal + 2;
340 else {
341 free(val_dup);
342 return (SA_SYNTAX_ERR);
343 }
344 } else {
345 next = strchr(host, ':');
346 if (next != NULL)
347 ++next;
348 }
349 host = next;
350 } while (host != NULL);
351 free(val_dup);
352 return (SA_OK);
353 }
354
355 if (strcmp(key, "anon") == 0)
356 key = "anonuid";
357
358 if (strcmp(key, "root_mapping") == 0) {
359 (void) add_linux_shareopt(plinux_opts, "root_squash", NULL);
360 key = "anonuid";
361 }
362
363 if (strcmp(key, "nosub") == 0)
364 key = "subtree_check";
365
366 if (bsearch(&key, valid_keys, ARRAY_SIZE(valid_keys),
367 sizeof (*valid_keys), string_cmp) == NULL)
368 return (SA_SYNTAX_ERR);
369
370 (void) add_linux_shareopt(plinux_opts, key, value);
371
372 return (SA_OK);
373 }
374
375 /*
376 * Takes a string containing Solaris share options (e.g. "sync,no_acl") and
377 * converts them to a NULL-terminated array of Linux NFS options.
378 */
379 static int
get_linux_shareopts(const char * shareopts,char ** plinux_opts)380 get_linux_shareopts(const char *shareopts, char **plinux_opts)
381 {
382 int error;
383
384 assert(plinux_opts != NULL);
385
386 *plinux_opts = NULL;
387
388 /* no_subtree_check - Default as of nfs-utils v1.1.0 */
389 (void) add_linux_shareopt(plinux_opts, "no_subtree_check", NULL);
390
391 /* mountpoint - Restrict exports to ZFS mountpoints */
392 (void) add_linux_shareopt(plinux_opts, "mountpoint", NULL);
393
394 error = foreach_nfs_shareopt(shareopts, get_linux_shareopts_cb,
395 plinux_opts);
396
397 if (error != SA_OK) {
398 free(*plinux_opts);
399 *plinux_opts = NULL;
400 }
401
402 return (error);
403 }
404
405 /*
406 * This function populates an entry into /etc/exports.d/zfs.exports.
407 * This file is consumed by the linux nfs server so that zfs shares are
408 * automatically exported upon boot or whenever the nfs server restarts.
409 */
410 static int
nfs_add_entry(FILE * tmpfile,const char * sharepath,const char * host,const char * security,const char * access_opts,void * pcookie)411 nfs_add_entry(FILE *tmpfile, const char *sharepath,
412 const char *host, const char *security, const char *access_opts,
413 void *pcookie)
414 {
415 const char *linux_opts = (const char *)pcookie;
416
417 if (linux_opts == NULL)
418 linux_opts = "";
419
420 boolean_t need_free;
421 char *mp;
422 int rc = nfs_escape_mountpoint(sharepath, &mp, &need_free);
423 if (rc != SA_OK)
424 return (rc);
425 if (fprintf(tmpfile, "%s %s(sec=%s,%s,%s)\n", mp,
426 get_linux_hostspec(host), security, access_opts,
427 linux_opts) < 0) {
428 fprintf(stderr, "failed to write to temporary file\n");
429 rc = SA_SYSTEM_ERR;
430 }
431
432 if (need_free)
433 free(mp);
434 return (rc);
435 }
436
437 /*
438 * Enables NFS sharing for the specified share.
439 */
440 static int
nfs_enable_share_impl(sa_share_impl_t impl_share,FILE * tmpfile)441 nfs_enable_share_impl(sa_share_impl_t impl_share, FILE *tmpfile)
442 {
443 char *linux_opts = NULL;
444 int error = get_linux_shareopts(impl_share->sa_shareopts, &linux_opts);
445 if (error != SA_OK)
446 return (error);
447
448 error = foreach_nfs_host(impl_share, tmpfile, nfs_add_entry,
449 linux_opts);
450 free(linux_opts);
451 return (error);
452 }
453
454 static int
nfs_enable_share(sa_share_impl_t impl_share)455 nfs_enable_share(sa_share_impl_t impl_share)
456 {
457 if (!nfs_available())
458 return (SA_SYSTEM_ERR);
459
460 return (nfs_toggle_share(
461 ZFS_EXPORTS_LOCK, ZFS_EXPORTS_FILE, ZFS_EXPORTS_DIR, impl_share,
462 nfs_enable_share_impl));
463 }
464
465 /*
466 * Disables NFS sharing for the specified share.
467 */
468 static int
nfs_disable_share_impl(sa_share_impl_t impl_share,FILE * tmpfile)469 nfs_disable_share_impl(sa_share_impl_t impl_share, FILE *tmpfile)
470 {
471 (void) impl_share, (void) tmpfile;
472 return (SA_OK);
473 }
474
475 static int
nfs_disable_share(sa_share_impl_t impl_share)476 nfs_disable_share(sa_share_impl_t impl_share)
477 {
478 if (!nfs_available())
479 return (SA_OK);
480
481 return (nfs_toggle_share(
482 ZFS_EXPORTS_LOCK, ZFS_EXPORTS_FILE, ZFS_EXPORTS_DIR, impl_share,
483 nfs_disable_share_impl));
484 }
485
486 static boolean_t
nfs_is_shared(sa_share_impl_t impl_share)487 nfs_is_shared(sa_share_impl_t impl_share)
488 {
489 if (!nfs_available())
490 return (SA_SYSTEM_ERR);
491
492 return (nfs_is_shared_impl(ZFS_EXPORTS_FILE, impl_share));
493 }
494
495 /*
496 * Checks whether the specified NFS share options are syntactically correct.
497 */
498 static int
nfs_validate_shareopts(const char * shareopts)499 nfs_validate_shareopts(const char *shareopts)
500 {
501 char *linux_opts = NULL;
502
503 if (strlen(shareopts) == 0)
504 return (SA_SYNTAX_ERR);
505
506 int error = get_linux_shareopts(shareopts, &linux_opts);
507 if (error != SA_OK)
508 return (error);
509
510 free(linux_opts);
511 return (SA_OK);
512 }
513
514 static int
nfs_commit_shares(void)515 nfs_commit_shares(void)
516 {
517 if (!nfs_available())
518 return (SA_SYSTEM_ERR);
519
520 char *argv[] = {
521 (char *)"/usr/sbin/exportfs",
522 (char *)"-ra",
523 NULL
524 };
525
526 return (libzfs_run_process(argv[0], argv, 0));
527 }
528
529 static void
nfs_truncate_shares(void)530 nfs_truncate_shares(void)
531 {
532 if (!exports_available())
533 return;
534 nfs_reset_shares(ZFS_EXPORTS_LOCK, ZFS_EXPORTS_FILE);
535 }
536
537 const sa_fstype_t libshare_nfs_type = {
538 .enable_share = nfs_enable_share,
539 .disable_share = nfs_disable_share,
540 .is_shared = nfs_is_shared,
541
542 .validate_shareopts = nfs_validate_shareopts,
543 .commit_shares = nfs_commit_shares,
544 .truncate_shares = nfs_truncate_shares,
545 };
546
547 static boolean_t
nfs_available(void)548 nfs_available(void)
549 {
550 static int avail;
551
552 if (!avail) {
553 if (access("/usr/sbin/exportfs", F_OK) != 0)
554 avail = -1;
555 else
556 avail = 1;
557 }
558
559 return (avail == 1);
560 }
561
562 static boolean_t
exports_available(void)563 exports_available(void)
564 {
565 static int avail;
566
567 if (!avail) {
568 if (access(ZFS_EXPORTS_DIR, F_OK) != 0)
569 avail = -1;
570 else
571 avail = 1;
572 }
573
574 return (avail == 1);
575 }
576