]> git.proxmox.com Git - mirror_zfs.git/blob - lib/libshare/nfs.c
filesystem_limit/snapshot_limit is incorrectly enforced against root
[mirror_zfs.git] / lib / libshare / nfs.c
1 /*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21
22 /*
23 * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright (c) 2011 Gunnar Beutner
25 * Copyright (c) 2012 Cyril Plisko. All rights reserved.
26 */
27
28 #include <stdio.h>
29 #include <string.h>
30 #include <strings.h>
31 #include <fcntl.h>
32 #include <errno.h>
33 #include <sys/wait.h>
34 #include <unistd.h>
35 #include <libzfs.h>
36 #include <libshare.h>
37 #include "libshare_impl.h"
38 #include "nfs.h"
39
40 static boolean_t nfs_available(void);
41
42 static sa_fstype_t *nfs_fstype;
43
44 /*
45 * nfs_exportfs_temp_fd refers to a temporary copy of the output
46 * from exportfs -v.
47 */
48 static int nfs_exportfs_temp_fd = -1;
49
50 typedef int (*nfs_shareopt_callback_t)(const char *opt, const char *value,
51 void *cookie);
52
53 typedef int (*nfs_host_callback_t)(const char *sharepath, const char *host,
54 const char *security, const char *access, void *cookie);
55
56 /*
57 * Invokes the specified callback function for each Solaris share option
58 * listed in the specified string.
59 */
60 static int
61 foreach_nfs_shareopt(const char *shareopts,
62 nfs_shareopt_callback_t callback, void *cookie)
63 {
64 char *shareopts_dup, *opt, *cur, *value;
65 int was_nul, rc;
66
67 if (shareopts == NULL)
68 return (SA_OK);
69
70 shareopts_dup = strdup(shareopts);
71
72 if (shareopts_dup == NULL)
73 return (SA_NO_MEMORY);
74
75 opt = shareopts_dup;
76 was_nul = 0;
77
78 while (1) {
79 cur = opt;
80
81 while (*cur != ',' && *cur != '\0')
82 cur++;
83
84 if (*cur == '\0')
85 was_nul = 1;
86
87 *cur = '\0';
88
89 if (cur > opt) {
90 value = strchr(opt, '=');
91
92 if (value != NULL) {
93 *value = '\0';
94 value++;
95 }
96
97 rc = callback(opt, value, cookie);
98
99 if (rc != SA_OK) {
100 free(shareopts_dup);
101 return (rc);
102 }
103 }
104
105 opt = cur + 1;
106
107 if (was_nul)
108 break;
109 }
110
111 free(shareopts_dup);
112
113 return (0);
114 }
115
116 typedef struct nfs_host_cookie_s {
117 nfs_host_callback_t callback;
118 const char *sharepath;
119 void *cookie;
120 const char *security;
121 } nfs_host_cookie_t;
122
123 /*
124 * Helper function for foreach_nfs_host. This function checks whether the
125 * current share option is a host specification and invokes a callback
126 * function with information about the host.
127 */
128 static int
129 foreach_nfs_host_cb(const char *opt, const char *value, void *pcookie)
130 {
131 int rc;
132 const char *access;
133 char *host_dup, *host, *next;
134 nfs_host_cookie_t *udata = (nfs_host_cookie_t *)pcookie;
135
136 #ifdef DEBUG
137 fprintf(stderr, "foreach_nfs_host_cb: key=%s, value=%s\n", opt, value);
138 #endif
139
140 if (strcmp(opt, "sec") == 0)
141 udata->security = value;
142
143 if (strcmp(opt, "rw") == 0 || strcmp(opt, "ro") == 0) {
144 if (value == NULL)
145 value = "*";
146
147 access = opt;
148
149 host_dup = strdup(value);
150
151 if (host_dup == NULL)
152 return (SA_NO_MEMORY);
153
154 host = host_dup;
155
156 do {
157 next = strchr(host, ':');
158 if (next != NULL) {
159 *next = '\0';
160 next++;
161 }
162
163 rc = udata->callback(udata->sharepath, host,
164 udata->security, access, udata->cookie);
165
166 if (rc != SA_OK) {
167 free(host_dup);
168
169 return (rc);
170 }
171
172 host = next;
173 } while (host != NULL);
174
175 free(host_dup);
176 }
177
178 return (SA_OK);
179 }
180
181 /*
182 * Invokes a callback function for all NFS hosts that are set for a share.
183 */
184 static int
185 foreach_nfs_host(sa_share_impl_t impl_share, nfs_host_callback_t callback,
186 void *cookie)
187 {
188 nfs_host_cookie_t udata;
189 char *shareopts;
190
191 udata.callback = callback;
192 udata.sharepath = impl_share->sharepath;
193 udata.cookie = cookie;
194 udata.security = "sys";
195
196 shareopts = FSINFO(impl_share, nfs_fstype)->shareopts;
197
198 return foreach_nfs_shareopt(shareopts, foreach_nfs_host_cb,
199 &udata);
200 }
201
202 /*
203 * Converts a Solaris NFS host specification to its Linux equivalent.
204 */
205 static int
206 get_linux_hostspec(const char *solaris_hostspec, char **plinux_hostspec)
207 {
208 /*
209 * For now we just support CIDR masks (e.g. @192.168.0.0/16) and host
210 * wildcards (e.g. *.example.org).
211 */
212 if (solaris_hostspec[0] == '@') {
213 /*
214 * Solaris host specifier, e.g. @192.168.0.0/16; we just need
215 * to skip the @ in this case
216 */
217 *plinux_hostspec = strdup(solaris_hostspec + 1);
218 } else {
219 *plinux_hostspec = strdup(solaris_hostspec);
220 }
221
222 if (*plinux_hostspec == NULL) {
223 return (SA_NO_MEMORY);
224 }
225
226 return (SA_OK);
227 }
228
229 /*
230 * Used internally by nfs_enable_share to enable sharing for a single host.
231 */
232 static int
233 nfs_enable_share_one(const char *sharepath, const char *host,
234 const char *security, const char *access, void *pcookie)
235 {
236 int rc;
237 char *linuxhost, *hostpath, *opts;
238 const char *linux_opts = (const char *)pcookie;
239 char *argv[6];
240
241 /* exportfs -i -o sec=XX,rX,<opts> <host>:<sharepath> */
242
243 rc = get_linux_hostspec(host, &linuxhost);
244
245 if (rc < 0)
246 exit(1);
247
248 hostpath = malloc(strlen(linuxhost) + 1 + strlen(sharepath) + 1);
249
250 if (hostpath == NULL) {
251 free(linuxhost);
252
253 exit(1);
254 }
255
256 sprintf(hostpath, "%s:%s", linuxhost, sharepath);
257
258 free(linuxhost);
259
260 if (linux_opts == NULL)
261 linux_opts = "";
262
263 opts = malloc(4 + strlen(security) + 4 + strlen(linux_opts) + 1);
264
265 if (opts == NULL)
266 exit(1);
267
268 sprintf(opts, "sec=%s,%s,%s", security, access, linux_opts);
269
270 #ifdef DEBUG
271 fprintf(stderr, "sharing %s with opts %s\n", hostpath, opts);
272 #endif
273
274 argv[0] = "/usr/sbin/exportfs";
275 argv[1] = "-i";
276 argv[2] = "-o";
277 argv[3] = opts;
278 argv[4] = hostpath;
279 argv[5] = NULL;
280
281 rc = libzfs_run_process(argv[0], argv, 0);
282
283 free(hostpath);
284 free(opts);
285
286 if (rc < 0)
287 return (SA_SYSTEM_ERR);
288 else
289 return (SA_OK);
290 }
291
292 /*
293 * Adds a Linux share option to an array of NFS options.
294 */
295 static int
296 add_linux_shareopt(char **plinux_opts, const char *key, const char *value)
297 {
298 size_t len = 0;
299 char *new_linux_opts;
300
301 if (*plinux_opts != NULL)
302 len = strlen(*plinux_opts);
303
304 new_linux_opts = realloc(*plinux_opts, len + 1 + strlen(key) +
305 (value ? 1 + strlen(value) : 0) + 1);
306
307 if (new_linux_opts == NULL)
308 return (SA_NO_MEMORY);
309
310 new_linux_opts[len] = '\0';
311
312 if (len > 0)
313 strcat(new_linux_opts, ",");
314
315 strcat(new_linux_opts, key);
316
317 if (value != NULL) {
318 strcat(new_linux_opts, "=");
319 strcat(new_linux_opts, value);
320 }
321
322 *plinux_opts = new_linux_opts;
323
324 return (SA_OK);
325 }
326
327 /*
328 * Validates and converts a single Solaris share option to its Linux
329 * equivalent.
330 */
331 static int
332 get_linux_shareopts_cb(const char *key, const char *value, void *cookie)
333 {
334 char **plinux_opts = (char **)cookie;
335
336 /* host-specific options, these are taken care of elsewhere */
337 if (strcmp(key, "ro") == 0 || strcmp(key, "rw") == 0 ||
338 strcmp(key, "sec") == 0)
339 return (SA_OK);
340
341 if (strcmp(key, "anon") == 0)
342 key = "anonuid";
343
344 if (strcmp(key, "root_mapping") == 0) {
345 (void) add_linux_shareopt(plinux_opts, "root_squash", NULL);
346 key = "anonuid";
347 }
348
349 if (strcmp(key, "nosub") == 0)
350 key = "subtree_check";
351
352 if (strcmp(key, "insecure") != 0 && strcmp(key, "secure") != 0 &&
353 strcmp(key, "async") != 0 && strcmp(key, "sync") != 0 &&
354 strcmp(key, "no_wdelay") != 0 && strcmp(key, "wdelay") != 0 &&
355 strcmp(key, "nohide") != 0 && strcmp(key, "hide") != 0 &&
356 strcmp(key, "crossmnt") != 0 &&
357 strcmp(key, "no_subtree_check") != 0 &&
358 strcmp(key, "subtree_check") != 0 &&
359 strcmp(key, "insecure_locks") != 0 &&
360 strcmp(key, "secure_locks") != 0 &&
361 strcmp(key, "no_auth_nlm") != 0 && strcmp(key, "auth_nlm") != 0 &&
362 strcmp(key, "no_acl") != 0 && strcmp(key, "mountpoint") != 0 &&
363 strcmp(key, "mp") != 0 && strcmp(key, "fsuid") != 0 &&
364 strcmp(key, "refer") != 0 && strcmp(key, "replicas") != 0 &&
365 strcmp(key, "root_squash") != 0 &&
366 strcmp(key, "no_root_squash") != 0 &&
367 strcmp(key, "all_squash") != 0 &&
368 strcmp(key, "no_all_squash") != 0 && strcmp(key, "fsid") != 0 &&
369 strcmp(key, "anonuid") != 0 && strcmp(key, "anongid") != 0) {
370 return (SA_SYNTAX_ERR);
371 }
372
373 (void) add_linux_shareopt(plinux_opts, key, value);
374
375 return (SA_OK);
376 }
377
378 /*
379 * Takes a string containing Solaris share options (e.g. "sync,no_acl") and
380 * converts them to a NULL-terminated array of Linux NFS options.
381 */
382 static int
383 get_linux_shareopts(const char *shareopts, char **plinux_opts)
384 {
385 int rc;
386
387 assert(plinux_opts != NULL);
388
389 *plinux_opts = NULL;
390
391 /* no_subtree_check - Default as of nfs-utils v1.1.0 */
392 (void) add_linux_shareopt(plinux_opts, "no_subtree_check", NULL);
393
394 /* mountpoint - Restrict exports to ZFS mountpoints */
395 (void) add_linux_shareopt(plinux_opts, "mountpoint", NULL);
396
397 rc = foreach_nfs_shareopt(shareopts, get_linux_shareopts_cb,
398 plinux_opts);
399
400 if (rc != SA_OK) {
401 free(*plinux_opts);
402 *plinux_opts = NULL;
403 }
404
405 return (rc);
406 }
407
408 /*
409 * Enables NFS sharing for the specified share.
410 */
411 static int
412 nfs_enable_share(sa_share_impl_t impl_share)
413 {
414 char *shareopts, *linux_opts;
415 int rc;
416
417 if (!nfs_available()) {
418 return (SA_SYSTEM_ERR);
419 }
420
421 shareopts = FSINFO(impl_share, nfs_fstype)->shareopts;
422
423 if (shareopts == NULL)
424 return (SA_OK);
425
426 rc = get_linux_shareopts(shareopts, &linux_opts);
427
428 if (rc != SA_OK)
429 return (rc);
430
431 rc = foreach_nfs_host(impl_share, nfs_enable_share_one, linux_opts);
432
433 free(linux_opts);
434
435 return (rc);
436 }
437
438 /*
439 * Used internally by nfs_disable_share to disable sharing for a single host.
440 */
441 static int
442 nfs_disable_share_one(const char *sharepath, const char *host,
443 const char *security, const char *access, void *cookie)
444 {
445 int rc;
446 char *linuxhost, *hostpath;
447 char *argv[4];
448
449 rc = get_linux_hostspec(host, &linuxhost);
450
451 if (rc < 0)
452 exit(1);
453
454 hostpath = malloc(strlen(linuxhost) + 1 + strlen(sharepath) + 1);
455
456 if (hostpath == NULL) {
457 free(linuxhost);
458 exit(1);
459 }
460
461 sprintf(hostpath, "%s:%s", linuxhost, sharepath);
462
463 free(linuxhost);
464
465 #ifdef DEBUG
466 fprintf(stderr, "unsharing %s\n", hostpath);
467 #endif
468
469 argv[0] = "/usr/sbin/exportfs";
470 argv[1] = "-u";
471 argv[2] = hostpath;
472 argv[3] = NULL;
473
474 rc = libzfs_run_process(argv[0], argv, 0);
475
476 free(hostpath);
477
478 if (rc < 0)
479 return (SA_SYSTEM_ERR);
480 else
481 return (SA_OK);
482 }
483
484 /*
485 * Disables NFS sharing for the specified share.
486 */
487 static int
488 nfs_disable_share(sa_share_impl_t impl_share)
489 {
490 if (!nfs_available()) {
491 /*
492 * The share can't possibly be active, so nothing
493 * needs to be done to disable it.
494 */
495 return (SA_OK);
496 }
497
498 return (foreach_nfs_host(impl_share, nfs_disable_share_one, NULL));
499 }
500
501 /*
502 * Checks whether the specified NFS share options are syntactically correct.
503 */
504 static int
505 nfs_validate_shareopts(const char *shareopts)
506 {
507 char *linux_opts;
508 int rc;
509
510 rc = get_linux_shareopts(shareopts, &linux_opts);
511
512 if (rc != SA_OK)
513 return (rc);
514
515 free(linux_opts);
516
517 return (SA_OK);
518 }
519
520 /*
521 * Checks whether a share is currently active.
522 */
523 static boolean_t
524 nfs_is_share_active(sa_share_impl_t impl_share)
525 {
526 int fd;
527 char line[512];
528 char *tab, *cur;
529 FILE *nfs_exportfs_temp_fp;
530
531 if (!nfs_available())
532 return (B_FALSE);
533
534 if ((fd = dup(nfs_exportfs_temp_fd)) == -1)
535 return (B_FALSE);
536
537 nfs_exportfs_temp_fp = fdopen(fd, "r");
538
539 if (nfs_exportfs_temp_fp == NULL)
540 return (B_FALSE);
541
542 if (fseek(nfs_exportfs_temp_fp, 0, SEEK_SET) < 0) {
543 fclose(nfs_exportfs_temp_fp);
544 return (B_FALSE);
545 }
546
547 while (fgets(line, sizeof (line), nfs_exportfs_temp_fp) != NULL) {
548 /*
549 * exportfs uses separate lines for the share path
550 * and the export options when the share path is longer
551 * than a certain amount of characters; this ignores
552 * the option lines
553 */
554 if (line[0] == '\t')
555 continue;
556
557 tab = strchr(line, '\t');
558
559 if (tab != NULL) {
560 *tab = '\0';
561 cur = tab - 1;
562 } else {
563 /*
564 * there's no tab character, which means the
565 * NFS options are on a separate line; we just
566 * need to remove the new-line character
567 * at the end of the line
568 */
569 cur = line + strlen(line) - 1;
570 }
571
572 /* remove trailing spaces and new-line characters */
573 while (cur >= line && (*cur == ' ' || *cur == '\n'))
574 *cur-- = '\0';
575
576 if (strcmp(line, impl_share->sharepath) == 0) {
577 fclose(nfs_exportfs_temp_fp);
578 return (B_TRUE);
579 }
580 }
581
582 fclose(nfs_exportfs_temp_fp);
583
584 return (B_FALSE);
585 }
586
587 /*
588 * Called to update a share's options. A share's options might be out of
589 * date if the share was loaded from disk (i.e. /etc/dfs/sharetab) and the
590 * "sharenfs" dataset property has changed in the meantime. This function
591 * also takes care of re-enabling the share if necessary.
592 */
593 static int
594 nfs_update_shareopts(sa_share_impl_t impl_share, const char *resource,
595 const char *shareopts)
596 {
597 char *shareopts_dup;
598 boolean_t needs_reshare = B_FALSE;
599 char *old_shareopts;
600
601 FSINFO(impl_share, nfs_fstype)->active =
602 nfs_is_share_active(impl_share);
603
604 old_shareopts = FSINFO(impl_share, nfs_fstype)->shareopts;
605
606 if (strcmp(shareopts, "on") == 0)
607 shareopts = "rw,crossmnt";
608
609 if (FSINFO(impl_share, nfs_fstype)->active && old_shareopts != NULL &&
610 strcmp(old_shareopts, shareopts) != 0) {
611 needs_reshare = B_TRUE;
612 nfs_disable_share(impl_share);
613 }
614
615 shareopts_dup = strdup(shareopts);
616
617 if (shareopts_dup == NULL)
618 return (SA_NO_MEMORY);
619
620 if (old_shareopts != NULL)
621 free(old_shareopts);
622
623 FSINFO(impl_share, nfs_fstype)->shareopts = shareopts_dup;
624
625 if (needs_reshare)
626 nfs_enable_share(impl_share);
627
628 return (SA_OK);
629 }
630
631 /*
632 * Clears a share's NFS options. Used by libshare to
633 * clean up shares that are about to be free()'d.
634 */
635 static void
636 nfs_clear_shareopts(sa_share_impl_t impl_share)
637 {
638 free(FSINFO(impl_share, nfs_fstype)->shareopts);
639 FSINFO(impl_share, nfs_fstype)->shareopts = NULL;
640 }
641
642 static const sa_share_ops_t nfs_shareops = {
643 .enable_share = nfs_enable_share,
644 .disable_share = nfs_disable_share,
645
646 .validate_shareopts = nfs_validate_shareopts,
647 .update_shareopts = nfs_update_shareopts,
648 .clear_shareopts = nfs_clear_shareopts,
649 };
650
651 /*
652 * nfs_check_exportfs() checks that the exportfs command runs
653 * and also maintains a temporary copy of the output from
654 * exportfs -v.
655 * To update this temporary copy simply call this function again.
656 *
657 * TODO : Use /var/lib/nfs/etab instead of our private copy.
658 * But must implement locking to prevent concurrent access.
659 *
660 * TODO : The temporary file descriptor is never closed since
661 * there is no libshare_nfs_fini() function.
662 */
663 static int
664 nfs_check_exportfs(void)
665 {
666 pid_t pid;
667 int rc, status;
668 static char nfs_exportfs_tempfile[] = "/tmp/exportfs.XXXXXX";
669
670 /*
671 * Close any existing temporary copies of output from exportfs.
672 * We have already called unlink() so file will be deleted.
673 */
674 if (nfs_exportfs_temp_fd >= 0)
675 close(nfs_exportfs_temp_fd);
676
677 nfs_exportfs_temp_fd = mkstemp(nfs_exportfs_tempfile);
678
679 if (nfs_exportfs_temp_fd < 0)
680 return (SA_SYSTEM_ERR);
681
682 unlink(nfs_exportfs_tempfile);
683
684 (void) fcntl(nfs_exportfs_temp_fd, F_SETFD, FD_CLOEXEC);
685
686 pid = fork();
687
688 if (pid < 0) {
689 (void) close(nfs_exportfs_temp_fd);
690 nfs_exportfs_temp_fd = -1;
691 return (SA_SYSTEM_ERR);
692 }
693
694 if (pid > 0) {
695 while ((rc = waitpid(pid, &status, 0)) <= 0 &&
696 errno == EINTR) { }
697
698 if (rc <= 0) {
699 (void) close(nfs_exportfs_temp_fd);
700 nfs_exportfs_temp_fd = -1;
701 return (SA_SYSTEM_ERR);
702 }
703
704 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
705 (void) close(nfs_exportfs_temp_fd);
706 nfs_exportfs_temp_fd = -1;
707 return (SA_CONFIG_ERR);
708 }
709
710 return (SA_OK);
711 }
712
713 /* child */
714
715 /* exportfs -v */
716
717 if (dup2(nfs_exportfs_temp_fd, STDOUT_FILENO) < 0)
718 exit(1);
719
720 rc = execlp("/usr/sbin/exportfs", "exportfs", "-v", NULL);
721
722 if (rc < 0) {
723 exit(1);
724 }
725
726 exit(0);
727 }
728
729 /*
730 * Provides a convenient wrapper for determining nfs availability
731 */
732 static boolean_t
733 nfs_available(void)
734 {
735 if (nfs_exportfs_temp_fd == -1)
736 (void) nfs_check_exportfs();
737
738 return ((nfs_exportfs_temp_fd != -1) ? B_TRUE : B_FALSE);
739 }
740
741 /*
742 * Initializes the NFS functionality of libshare.
743 */
744 void
745 libshare_nfs_init(void)
746 {
747 nfs_fstype = register_fstype("nfs", &nfs_shareops);
748 }