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