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