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