]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/contrib/openzfs/lib/libshare/os/linux/nfs.c
OpenZFS: MFV 2.0-rc3-gfc5966
[FreeBSD/FreeBSD.git] / sys / contrib / openzfs / lib / libshare / os / linux / 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  * Copyright (c) 2019, 2020 by Delphix. All rights reserved.
27  */
28
29 #include <dirent.h>
30 #include <stdio.h>
31 #include <string.h>
32 #include <strings.h>
33 #include <errno.h>
34 #include <fcntl.h>
35 #include <sys/file.h>
36 #include <sys/stat.h>
37 #include <sys/types.h>
38 #include <sys/wait.h>
39 #include <unistd.h>
40 #include <libzfs.h>
41 #include <libshare.h>
42 #include "libshare_impl.h"
43 #include "nfs.h"
44
45 #define FILE_HEADER             "# !!! DO NOT EDIT THIS FILE MANUALLY !!!\n\n"
46 #define ZFS_EXPORTS_DIR         "/etc/exports.d"
47 #define ZFS_EXPORTS_FILE        ZFS_EXPORTS_DIR"/zfs.exports"
48 #define ZFS_EXPORTS_LOCK        ZFS_EXPORTS_FILE".lock"
49
50 static sa_fstype_t *nfs_fstype;
51
52 typedef int (*nfs_shareopt_callback_t)(const char *opt, const char *value,
53     void *cookie);
54
55 typedef int (*nfs_host_callback_t)(const char *sharepath, const char *filename,
56     const char *host, const char *security, const char *access, void *cookie);
57
58 static int nfs_lock_fd = -1;
59
60 /*
61  * The nfs_exports_[lock|unlock] is used to guard against conconcurrent
62  * updates to the exports file. Each protocol is responsible for
63  * providing the necessary locking to ensure consistency.
64  */
65 static int
66 nfs_exports_lock(void)
67 {
68         nfs_lock_fd = open(ZFS_EXPORTS_LOCK,
69             O_RDWR | O_CREAT, 0600);
70         if (nfs_lock_fd == -1) {
71                 fprintf(stderr, "failed to lock %s: %s\n",
72                     ZFS_EXPORTS_LOCK, strerror(errno));
73                 return (errno);
74         }
75         if (flock(nfs_lock_fd, LOCK_EX) != 0) {
76                 fprintf(stderr, "failed to lock %s: %s\n",
77                     ZFS_EXPORTS_LOCK, strerror(errno));
78                 return (errno);
79         }
80         return (0);
81 }
82
83 static void
84 nfs_exports_unlock(void)
85 {
86         verify(nfs_lock_fd > 0);
87
88         if (flock(nfs_lock_fd, LOCK_UN) != 0) {
89                 fprintf(stderr, "failed to unlock %s: %s\n",
90                     ZFS_EXPORTS_LOCK, strerror(errno));
91         }
92         close(nfs_lock_fd);
93         nfs_lock_fd = -1;
94 }
95
96 /*
97  * Invokes the specified callback function for each Solaris share option
98  * listed in the specified string.
99  */
100 static int
101 foreach_nfs_shareopt(const char *shareopts,
102     nfs_shareopt_callback_t callback, void *cookie)
103 {
104         char *shareopts_dup, *opt, *cur, *value;
105         int was_nul, error;
106
107         if (shareopts == NULL)
108                 return (SA_OK);
109
110         if (strcmp(shareopts, "on") == 0)
111                 shareopts = "rw,crossmnt";
112
113         shareopts_dup = strdup(shareopts);
114
115
116         if (shareopts_dup == NULL)
117                 return (SA_NO_MEMORY);
118
119         opt = shareopts_dup;
120         was_nul = 0;
121
122         while (1) {
123                 cur = opt;
124
125                 while (*cur != ',' && *cur != '\0')
126                         cur++;
127
128                 if (*cur == '\0')
129                         was_nul = 1;
130
131                 *cur = '\0';
132
133                 if (cur > opt) {
134                         value = strchr(opt, '=');
135
136                         if (value != NULL) {
137                                 *value = '\0';
138                                 value++;
139                         }
140
141                         error = callback(opt, value, cookie);
142
143                         if (error != SA_OK) {
144                                 free(shareopts_dup);
145                                 return (error);
146                         }
147                 }
148
149                 opt = cur + 1;
150
151                 if (was_nul)
152                         break;
153         }
154
155         free(shareopts_dup);
156
157         return (SA_OK);
158 }
159
160 typedef struct nfs_host_cookie_s {
161         nfs_host_callback_t callback;
162         const char *sharepath;
163         void *cookie;
164         const char *filename;
165         const char *security;
166 } nfs_host_cookie_t;
167
168 /*
169  * Helper function for foreach_nfs_host. This function checks whether the
170  * current share option is a host specification and invokes a callback
171  * function with information about the host.
172  */
173 static int
174 foreach_nfs_host_cb(const char *opt, const char *value, void *pcookie)
175 {
176         int error;
177         const char *access;
178         char *host_dup, *host, *next;
179         nfs_host_cookie_t *udata = (nfs_host_cookie_t *)pcookie;
180
181 #ifdef DEBUG
182         fprintf(stderr, "foreach_nfs_host_cb: key=%s, value=%s\n", opt, value);
183 #endif
184
185         if (strcmp(opt, "sec") == 0)
186                 udata->security = value;
187
188         if (strcmp(opt, "rw") == 0 || strcmp(opt, "ro") == 0) {
189                 if (value == NULL)
190                         value = "*";
191
192                 access = opt;
193
194                 host_dup = strdup(value);
195
196                 if (host_dup == NULL)
197                         return (SA_NO_MEMORY);
198
199                 host = host_dup;
200
201                 do {
202                         next = strchr(host, ':');
203                         if (next != NULL) {
204                                 *next = '\0';
205                                 next++;
206                         }
207
208                         error = udata->callback(udata->filename,
209                             udata->sharepath, host, udata->security,
210                             access, udata->cookie);
211
212                         if (error != SA_OK) {
213                                 free(host_dup);
214
215                                 return (error);
216                         }
217
218                         host = next;
219                 } while (host != NULL);
220
221                 free(host_dup);
222         }
223
224         return (SA_OK);
225 }
226
227 /*
228  * Invokes a callback function for all NFS hosts that are set for a share.
229  */
230 static int
231 foreach_nfs_host(sa_share_impl_t impl_share, char *filename,
232     nfs_host_callback_t callback, void *cookie)
233 {
234         nfs_host_cookie_t udata;
235         char *shareopts;
236
237         udata.callback = callback;
238         udata.sharepath = impl_share->sa_mountpoint;
239         udata.cookie = cookie;
240         udata.filename = filename;
241         udata.security = "sys";
242
243         shareopts = FSINFO(impl_share, nfs_fstype)->shareopts;
244
245         return (foreach_nfs_shareopt(shareopts, foreach_nfs_host_cb,
246             &udata));
247 }
248
249 /*
250  * Converts a Solaris NFS host specification to its Linux equivalent.
251  */
252 static int
253 get_linux_hostspec(const char *solaris_hostspec, char **plinux_hostspec)
254 {
255         /*
256          * For now we just support CIDR masks (e.g. @192.168.0.0/16) and host
257          * wildcards (e.g. *.example.org).
258          */
259         if (solaris_hostspec[0] == '@') {
260                 /*
261                  * Solaris host specifier, e.g. @192.168.0.0/16; we just need
262                  * to skip the @ in this case
263                  */
264                 *plinux_hostspec = strdup(solaris_hostspec + 1);
265         } else {
266                 *plinux_hostspec = strdup(solaris_hostspec);
267         }
268
269         if (*plinux_hostspec == NULL) {
270                 return (SA_NO_MEMORY);
271         }
272
273         return (SA_OK);
274 }
275
276 /*
277  * Adds a Linux share option to an array of NFS options.
278  */
279 static int
280 add_linux_shareopt(char **plinux_opts, const char *key, const char *value)
281 {
282         size_t len = 0;
283         char *new_linux_opts;
284
285         if (*plinux_opts != NULL)
286                 len = strlen(*plinux_opts);
287
288         new_linux_opts = realloc(*plinux_opts, len + 1 + strlen(key) +
289             (value ? 1 + strlen(value) : 0) + 1);
290
291         if (new_linux_opts == NULL)
292                 return (SA_NO_MEMORY);
293
294         new_linux_opts[len] = '\0';
295
296         if (len > 0)
297                 strcat(new_linux_opts, ",");
298
299         strcat(new_linux_opts, key);
300
301         if (value != NULL) {
302                 strcat(new_linux_opts, "=");
303                 strcat(new_linux_opts, value);
304         }
305
306         *plinux_opts = new_linux_opts;
307
308         return (SA_OK);
309 }
310
311 /*
312  * Validates and converts a single Solaris share option to its Linux
313  * equivalent.
314  */
315 static int
316 get_linux_shareopts_cb(const char *key, const char *value, void *cookie)
317 {
318         char **plinux_opts = (char **)cookie;
319
320         /* host-specific options, these are taken care of elsewhere */
321         if (strcmp(key, "ro") == 0 || strcmp(key, "rw") == 0 ||
322             strcmp(key, "sec") == 0)
323                 return (SA_OK);
324
325         if (strcmp(key, "anon") == 0)
326                 key = "anonuid";
327
328         if (strcmp(key, "root_mapping") == 0) {
329                 (void) add_linux_shareopt(plinux_opts, "root_squash", NULL);
330                 key = "anonuid";
331         }
332
333         if (strcmp(key, "nosub") == 0)
334                 key = "subtree_check";
335
336         if (strcmp(key, "insecure") != 0 && strcmp(key, "secure") != 0 &&
337             strcmp(key, "async") != 0 && strcmp(key, "sync") != 0 &&
338             strcmp(key, "no_wdelay") != 0 && strcmp(key, "wdelay") != 0 &&
339             strcmp(key, "nohide") != 0 && strcmp(key, "hide") != 0 &&
340             strcmp(key, "crossmnt") != 0 &&
341             strcmp(key, "no_subtree_check") != 0 &&
342             strcmp(key, "subtree_check") != 0 &&
343             strcmp(key, "insecure_locks") != 0 &&
344             strcmp(key, "secure_locks") != 0 &&
345             strcmp(key, "no_auth_nlm") != 0 && strcmp(key, "auth_nlm") != 0 &&
346             strcmp(key, "no_acl") != 0 && strcmp(key, "mountpoint") != 0 &&
347             strcmp(key, "mp") != 0 && strcmp(key, "fsuid") != 0 &&
348             strcmp(key, "refer") != 0 && strcmp(key, "replicas") != 0 &&
349             strcmp(key, "root_squash") != 0 &&
350             strcmp(key, "no_root_squash") != 0 &&
351             strcmp(key, "all_squash") != 0 &&
352             strcmp(key, "no_all_squash") != 0 && strcmp(key, "fsid") != 0 &&
353             strcmp(key, "anonuid") != 0 && strcmp(key, "anongid") != 0) {
354                 return (SA_SYNTAX_ERR);
355         }
356
357         (void) add_linux_shareopt(plinux_opts, key, value);
358
359         return (SA_OK);
360 }
361
362 /*
363  * Takes a string containing Solaris share options (e.g. "sync,no_acl") and
364  * converts them to a NULL-terminated array of Linux NFS options.
365  */
366 static int
367 get_linux_shareopts(const char *shareopts, char **plinux_opts)
368 {
369         int error;
370
371         assert(plinux_opts != NULL);
372
373         *plinux_opts = NULL;
374
375         /* no_subtree_check - Default as of nfs-utils v1.1.0 */
376         (void) add_linux_shareopt(plinux_opts, "no_subtree_check", NULL);
377
378         /* mountpoint - Restrict exports to ZFS mountpoints */
379         (void) add_linux_shareopt(plinux_opts, "mountpoint", NULL);
380
381         error = foreach_nfs_shareopt(shareopts, get_linux_shareopts_cb,
382             plinux_opts);
383
384         if (error != SA_OK) {
385                 free(*plinux_opts);
386                 *plinux_opts = NULL;
387         }
388
389         return (error);
390 }
391
392 static char *
393 nfs_init_tmpfile(void)
394 {
395         char *tmpfile = NULL;
396         struct stat sb;
397
398         if (stat(ZFS_EXPORTS_DIR, &sb) < 0 &&
399             mkdir(ZFS_EXPORTS_DIR, 0755) < 0) {
400                 fprintf(stderr, "failed to create %s: %s\n",
401                     ZFS_EXPORTS_DIR, strerror(errno));
402                 return (NULL);
403         }
404
405         if (asprintf(&tmpfile, "%s%s", ZFS_EXPORTS_FILE, ".XXXXXXXX") == -1) {
406                 fprintf(stderr, "Unable to allocate temporary file\n");
407                 return (NULL);
408         }
409
410         int fd = mkstemp(tmpfile);
411         if (fd == -1) {
412                 fprintf(stderr, "Unable to create temporary file: %s",
413                     strerror(errno));
414                 free(tmpfile);
415                 return (NULL);
416         }
417         close(fd);
418         return (tmpfile);
419 }
420
421 static int
422 nfs_fini_tmpfile(char *tmpfile)
423 {
424         if (rename(tmpfile, ZFS_EXPORTS_FILE) == -1) {
425                 fprintf(stderr, "Unable to rename %s: %s\n", tmpfile,
426                     strerror(errno));
427                 unlink(tmpfile);
428                 free(tmpfile);
429                 return (SA_SYSTEM_ERR);
430         }
431         free(tmpfile);
432         return (SA_OK);
433 }
434
435 /*
436  * This function populates an entry into /etc/exports.d/zfs.exports.
437  * This file is consumed by the linux nfs server so that zfs shares are
438  * automatically exported upon boot or whenever the nfs server restarts.
439  */
440 static int
441 nfs_add_entry(const char *filename, const char *sharepath,
442     const char *host, const char *security, const char *access_opts,
443     void *pcookie)
444 {
445         int error;
446         char *linuxhost;
447         const char *linux_opts = (const char *)pcookie;
448
449         error = get_linux_hostspec(host, &linuxhost);
450         if (error != SA_OK)
451                 return (error);
452
453         if (linux_opts == NULL)
454                 linux_opts = "";
455
456         FILE *fp = fopen(filename, "a+");
457         if (fp == NULL) {
458                 fprintf(stderr, "failed to open %s file: %s", filename,
459                     strerror(errno));
460                 free(linuxhost);
461                 return (SA_SYSTEM_ERR);
462         }
463
464         if (fprintf(fp, "%s %s(sec=%s,%s,%s)\n", sharepath, linuxhost,
465             security, access_opts, linux_opts) < 0) {
466                 fprintf(stderr, "failed to write to %s\n", filename);
467                 free(linuxhost);
468                 fclose(fp);
469                 return (SA_SYSTEM_ERR);
470         }
471
472         free(linuxhost);
473         if (fclose(fp) != 0) {
474                 fprintf(stderr, "Unable to close file %s: %s\n",
475                     filename, strerror(errno));
476                 return (SA_SYSTEM_ERR);
477         }
478         return (SA_OK);
479 }
480
481 /*
482  * This function copies all entries from the exports file to "filename",
483  * omitting any entries for the specified mountpoint.
484  */
485 static int
486 nfs_copy_entries(char *filename, const char *mountpoint)
487 {
488         char *buf = NULL;
489         size_t buflen = 0;
490         int error = SA_OK;
491
492         FILE *oldfp = fopen(ZFS_EXPORTS_FILE, "r");
493         FILE *newfp = fopen(filename, "w+");
494         if (newfp == NULL) {
495                 fprintf(stderr, "failed to open %s file: %s", filename,
496                     strerror(errno));
497                 fclose(oldfp);
498                 return (SA_SYSTEM_ERR);
499         }
500         fputs(FILE_HEADER, newfp);
501
502         /*
503          * The ZFS_EXPORTS_FILE may not exist yet. If that's the
504          * case then just write out the new file.
505          */
506         if (oldfp != NULL) {
507                 while (getline(&buf, &buflen, oldfp) != -1) {
508                         char *space = NULL;
509
510                         if (buf[0] == '\n' || buf[0] == '#')
511                                 continue;
512
513                         if ((space = strchr(buf, ' ')) != NULL) {
514                                 int mountpoint_len = strlen(mountpoint);
515
516                                 if (space - buf == mountpoint_len &&
517                                     strncmp(mountpoint, buf,
518                                     mountpoint_len) == 0) {
519                                         continue;
520                                 }
521                         }
522                         fputs(buf, newfp);
523                 }
524
525                 if (ferror(oldfp) != 0) {
526                         error = ferror(oldfp);
527                 }
528                 if (fclose(oldfp) != 0) {
529                         fprintf(stderr, "Unable to close file %s: %s\n",
530                             filename, strerror(errno));
531                         error = error != 0 ? error : SA_SYSTEM_ERR;
532                 }
533         }
534
535         if (error == 0 && ferror(newfp) != 0) {
536                 error = ferror(newfp);
537         }
538
539         free(buf);
540         if (fclose(newfp) != 0) {
541                 fprintf(stderr, "Unable to close file %s: %s\n",
542                     filename, strerror(errno));
543                 error = error != 0 ? error : SA_SYSTEM_ERR;
544         }
545         return (error);
546 }
547
548 /*
549  * Enables NFS sharing for the specified share.
550  */
551 static int
552 nfs_enable_share(sa_share_impl_t impl_share)
553 {
554         char *shareopts, *linux_opts;
555         char *filename = NULL;
556         int error;
557
558         if ((filename = nfs_init_tmpfile()) == NULL)
559                 return (SA_SYSTEM_ERR);
560
561         error = nfs_exports_lock();
562         if (error != 0) {
563                 unlink(filename);
564                 free(filename);
565                 return (error);
566         }
567
568         error = nfs_copy_entries(filename, impl_share->sa_mountpoint);
569         if (error != SA_OK) {
570                 unlink(filename);
571                 free(filename);
572                 nfs_exports_unlock();
573                 return (error);
574         }
575
576         shareopts = FSINFO(impl_share, nfs_fstype)->shareopts;
577         error = get_linux_shareopts(shareopts, &linux_opts);
578         if (error != SA_OK) {
579                 unlink(filename);
580                 free(filename);
581                 nfs_exports_unlock();
582                 return (error);
583         }
584
585         error = foreach_nfs_host(impl_share, filename, nfs_add_entry,
586             linux_opts);
587         free(linux_opts);
588         if (error == 0) {
589                 error = nfs_fini_tmpfile(filename);
590         } else {
591                 unlink(filename);
592                 free(filename);
593         }
594         nfs_exports_unlock();
595         return (error);
596 }
597
598 /*
599  * Disables NFS sharing for the specified share.
600  */
601 static int
602 nfs_disable_share(sa_share_impl_t impl_share)
603 {
604         int error;
605         char *filename = NULL;
606
607         if ((filename = nfs_init_tmpfile()) == NULL)
608                 return (SA_SYSTEM_ERR);
609
610         error = nfs_exports_lock();
611         if (error != 0) {
612                 unlink(filename);
613                 free(filename);
614                 return (error);
615         }
616
617         error = nfs_copy_entries(filename, impl_share->sa_mountpoint);
618         if (error != SA_OK) {
619                 unlink(filename);
620                 free(filename);
621                 nfs_exports_unlock();
622                 return (error);
623         }
624         error = nfs_fini_tmpfile(filename);
625         nfs_exports_unlock();
626         return (error);
627 }
628
629 static boolean_t
630 nfs_is_shared(sa_share_impl_t impl_share)
631 {
632         size_t buflen = 0;
633         char *buf = NULL;
634
635         FILE *fp = fopen(ZFS_EXPORTS_FILE, "r");
636         if (fp == NULL) {
637                 return (B_FALSE);
638         }
639         while ((getline(&buf, &buflen, fp)) != -1) {
640                 char *space = NULL;
641
642                 if ((space = strchr(buf, ' ')) != NULL) {
643                         int mountpoint_len = strlen(impl_share->sa_mountpoint);
644
645                         if (space - buf == mountpoint_len &&
646                             strncmp(impl_share->sa_mountpoint, buf,
647                             mountpoint_len) == 0) {
648                                 fclose(fp);
649                                 free(buf);
650                                 return (B_TRUE);
651                         }
652                 }
653         }
654         free(buf);
655         fclose(fp);
656         return (B_FALSE);
657 }
658
659 /*
660  * Checks whether the specified NFS share options are syntactically correct.
661  */
662 static int
663 nfs_validate_shareopts(const char *shareopts)
664 {
665         char *linux_opts;
666         int error;
667
668         error = get_linux_shareopts(shareopts, &linux_opts);
669
670         if (error != SA_OK)
671                 return (error);
672
673         free(linux_opts);
674         return (SA_OK);
675 }
676
677 static int
678 nfs_update_shareopts(sa_share_impl_t impl_share, const char *shareopts)
679 {
680         FSINFO(impl_share, nfs_fstype)->shareopts = (char *)shareopts;
681         return (SA_OK);
682 }
683
684 /*
685  * Clears a share's NFS options. Used by libshare to
686  * clean up shares that are about to be free()'d.
687  */
688 static void
689 nfs_clear_shareopts(sa_share_impl_t impl_share)
690 {
691         FSINFO(impl_share, nfs_fstype)->shareopts = NULL;
692 }
693
694 static int
695 nfs_commit_shares(void)
696 {
697         char *argv[] = {
698             "/usr/sbin/exportfs",
699             "-ra",
700             NULL
701         };
702
703         return (libzfs_run_process(argv[0], argv, 0));
704 }
705
706 static const sa_share_ops_t nfs_shareops = {
707         .enable_share = nfs_enable_share,
708         .disable_share = nfs_disable_share,
709         .is_shared = nfs_is_shared,
710
711         .validate_shareopts = nfs_validate_shareopts,
712         .update_shareopts = nfs_update_shareopts,
713         .clear_shareopts = nfs_clear_shareopts,
714         .commit_shares = nfs_commit_shares,
715 };
716
717 /*
718  * Initializes the NFS functionality of libshare.
719  */
720 void
721 libshare_nfs_init(void)
722 {
723         nfs_fstype = register_fstype("nfs", &nfs_shareops);
724 }