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