xref: /freebsd-src/sys/contrib/openzfs/lib/libshare/os/linux/nfs.c (revision e92ffd9b626833ebdbf2742c8ffddc6cd94b963e)
1eda14cbcSMatt Macy /*
2eda14cbcSMatt Macy  * CDDL HEADER START
3eda14cbcSMatt Macy  *
4eda14cbcSMatt Macy  * The contents of this file are subject to the terms of the
5eda14cbcSMatt Macy  * Common Development and Distribution License (the "License").
6eda14cbcSMatt Macy  * You may not use this file except in compliance with the License.
7eda14cbcSMatt Macy  *
8eda14cbcSMatt Macy  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9eda14cbcSMatt Macy  * or http://www.opensolaris.org/os/licensing.
10eda14cbcSMatt Macy  * See the License for the specific language governing permissions
11eda14cbcSMatt Macy  * and limitations under the License.
12eda14cbcSMatt Macy  *
13eda14cbcSMatt Macy  * When distributing Covered Code, include this CDDL HEADER in each
14eda14cbcSMatt Macy  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15eda14cbcSMatt Macy  * If applicable, add the following below this CDDL HEADER, with the
16eda14cbcSMatt Macy  * fields enclosed by brackets "[]" replaced with your own identifying
17eda14cbcSMatt Macy  * information: Portions Copyright [yyyy] [name of copyright owner]
18eda14cbcSMatt Macy  *
19eda14cbcSMatt Macy  * CDDL HEADER END
20eda14cbcSMatt Macy  */
21eda14cbcSMatt Macy 
22eda14cbcSMatt Macy /*
23eda14cbcSMatt Macy  * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
24eda14cbcSMatt Macy  * Copyright (c) 2011 Gunnar Beutner
25eda14cbcSMatt Macy  * Copyright (c) 2012 Cyril Plisko. All rights reserved.
26eda14cbcSMatt Macy  * Copyright (c) 2019, 2020 by Delphix. All rights reserved.
27eda14cbcSMatt Macy  */
28eda14cbcSMatt Macy 
29eda14cbcSMatt Macy #include <dirent.h>
30eda14cbcSMatt Macy #include <stdio.h>
31eda14cbcSMatt Macy #include <string.h>
32eda14cbcSMatt Macy #include <strings.h>
33eda14cbcSMatt Macy #include <errno.h>
342c48331dSMatt Macy #include <fcntl.h>
35eda14cbcSMatt Macy #include <sys/file.h>
36eda14cbcSMatt Macy #include <sys/stat.h>
37eda14cbcSMatt Macy #include <sys/types.h>
38eda14cbcSMatt Macy #include <sys/wait.h>
39eda14cbcSMatt Macy #include <unistd.h>
40eda14cbcSMatt Macy #include <libzfs.h>
41eda14cbcSMatt Macy #include <libshare.h>
42eda14cbcSMatt Macy #include "libshare_impl.h"
43eda14cbcSMatt Macy #include "nfs.h"
44eda14cbcSMatt Macy 
45eda14cbcSMatt Macy #define	ZFS_EXPORTS_DIR		"/etc/exports.d"
46eda14cbcSMatt Macy #define	ZFS_EXPORTS_FILE	ZFS_EXPORTS_DIR"/zfs.exports"
47eda14cbcSMatt Macy #define	ZFS_EXPORTS_LOCK	ZFS_EXPORTS_FILE".lock"
48eda14cbcSMatt Macy 
49eda14cbcSMatt Macy static sa_fstype_t *nfs_fstype;
50eda14cbcSMatt Macy 
51eda14cbcSMatt Macy typedef int (*nfs_shareopt_callback_t)(const char *opt, const char *value,
52eda14cbcSMatt Macy     void *cookie);
53eda14cbcSMatt Macy 
54*e92ffd9bSMartin Matuska typedef int (*nfs_host_callback_t)(FILE *tmpfile, const char *sharepath,
55eda14cbcSMatt Macy     const char *host, const char *security, const char *access, void *cookie);
56eda14cbcSMatt Macy 
57eda14cbcSMatt Macy /*
58eda14cbcSMatt Macy  * Invokes the specified callback function for each Solaris share option
59eda14cbcSMatt Macy  * listed in the specified string.
60eda14cbcSMatt Macy  */
61eda14cbcSMatt Macy static int
62eda14cbcSMatt Macy foreach_nfs_shareopt(const char *shareopts,
63eda14cbcSMatt Macy     nfs_shareopt_callback_t callback, void *cookie)
64eda14cbcSMatt Macy {
65eda14cbcSMatt Macy 	char *shareopts_dup, *opt, *cur, *value;
66eda14cbcSMatt Macy 	int was_nul, error;
67eda14cbcSMatt Macy 
68eda14cbcSMatt Macy 	if (shareopts == NULL)
69eda14cbcSMatt Macy 		return (SA_OK);
70eda14cbcSMatt Macy 
71eda14cbcSMatt Macy 	if (strcmp(shareopts, "on") == 0)
72eda14cbcSMatt Macy 		shareopts = "rw,crossmnt";
73eda14cbcSMatt Macy 
74eda14cbcSMatt Macy 	shareopts_dup = strdup(shareopts);
75eda14cbcSMatt Macy 
76eda14cbcSMatt Macy 
77eda14cbcSMatt Macy 	if (shareopts_dup == NULL)
78eda14cbcSMatt Macy 		return (SA_NO_MEMORY);
79eda14cbcSMatt Macy 
80eda14cbcSMatt Macy 	opt = shareopts_dup;
81eda14cbcSMatt Macy 	was_nul = 0;
82eda14cbcSMatt Macy 
83eda14cbcSMatt Macy 	while (1) {
84eda14cbcSMatt Macy 		cur = opt;
85eda14cbcSMatt Macy 
86eda14cbcSMatt Macy 		while (*cur != ',' && *cur != '\0')
87eda14cbcSMatt Macy 			cur++;
88eda14cbcSMatt Macy 
89eda14cbcSMatt Macy 		if (*cur == '\0')
90eda14cbcSMatt Macy 			was_nul = 1;
91eda14cbcSMatt Macy 
92eda14cbcSMatt Macy 		*cur = '\0';
93eda14cbcSMatt Macy 
94eda14cbcSMatt Macy 		if (cur > opt) {
95eda14cbcSMatt Macy 			value = strchr(opt, '=');
96eda14cbcSMatt Macy 
97eda14cbcSMatt Macy 			if (value != NULL) {
98eda14cbcSMatt Macy 				*value = '\0';
99eda14cbcSMatt Macy 				value++;
100eda14cbcSMatt Macy 			}
101eda14cbcSMatt Macy 
102eda14cbcSMatt Macy 			error = callback(opt, value, cookie);
103eda14cbcSMatt Macy 
104eda14cbcSMatt Macy 			if (error != SA_OK) {
105eda14cbcSMatt Macy 				free(shareopts_dup);
106eda14cbcSMatt Macy 				return (error);
107eda14cbcSMatt Macy 			}
108eda14cbcSMatt Macy 		}
109eda14cbcSMatt Macy 
110eda14cbcSMatt Macy 		opt = cur + 1;
111eda14cbcSMatt Macy 
112eda14cbcSMatt Macy 		if (was_nul)
113eda14cbcSMatt Macy 			break;
114eda14cbcSMatt Macy 	}
115eda14cbcSMatt Macy 
116eda14cbcSMatt Macy 	free(shareopts_dup);
117eda14cbcSMatt Macy 
118eda14cbcSMatt Macy 	return (SA_OK);
119eda14cbcSMatt Macy }
120eda14cbcSMatt Macy 
121eda14cbcSMatt Macy typedef struct nfs_host_cookie_s {
122eda14cbcSMatt Macy 	nfs_host_callback_t callback;
123eda14cbcSMatt Macy 	const char *sharepath;
124eda14cbcSMatt Macy 	void *cookie;
125*e92ffd9bSMartin Matuska 	FILE *tmpfile;
126eda14cbcSMatt Macy 	const char *security;
127eda14cbcSMatt Macy } nfs_host_cookie_t;
128eda14cbcSMatt Macy 
129eda14cbcSMatt Macy /*
130eda14cbcSMatt Macy  * Helper function for foreach_nfs_host. This function checks whether the
131eda14cbcSMatt Macy  * current share option is a host specification and invokes a callback
132eda14cbcSMatt Macy  * function with information about the host.
133eda14cbcSMatt Macy  */
134eda14cbcSMatt Macy static int
135eda14cbcSMatt Macy foreach_nfs_host_cb(const char *opt, const char *value, void *pcookie)
136eda14cbcSMatt Macy {
137eda14cbcSMatt Macy 	int error;
138eda14cbcSMatt Macy 	const char *access;
1396ba2210eSMartin Matuska 	char *host_dup, *host, *next, *v6Literal;
140eda14cbcSMatt Macy 	nfs_host_cookie_t *udata = (nfs_host_cookie_t *)pcookie;
1416ba2210eSMartin Matuska 	int cidr_len;
142eda14cbcSMatt Macy 
143eda14cbcSMatt Macy #ifdef DEBUG
144eda14cbcSMatt Macy 	fprintf(stderr, "foreach_nfs_host_cb: key=%s, value=%s\n", opt, value);
145eda14cbcSMatt Macy #endif
146eda14cbcSMatt Macy 
147eda14cbcSMatt Macy 	if (strcmp(opt, "sec") == 0)
148eda14cbcSMatt Macy 		udata->security = value;
149eda14cbcSMatt Macy 
150eda14cbcSMatt Macy 	if (strcmp(opt, "rw") == 0 || strcmp(opt, "ro") == 0) {
151eda14cbcSMatt Macy 		if (value == NULL)
152eda14cbcSMatt Macy 			value = "*";
153eda14cbcSMatt Macy 
154eda14cbcSMatt Macy 		access = opt;
155eda14cbcSMatt Macy 
156eda14cbcSMatt Macy 		host_dup = strdup(value);
157eda14cbcSMatt Macy 
158eda14cbcSMatt Macy 		if (host_dup == NULL)
159eda14cbcSMatt Macy 			return (SA_NO_MEMORY);
160eda14cbcSMatt Macy 
161eda14cbcSMatt Macy 		host = host_dup;
162eda14cbcSMatt Macy 
163eda14cbcSMatt Macy 		do {
1646ba2210eSMartin Matuska 			if (*host == '[') {
1656ba2210eSMartin Matuska 				host++;
1666ba2210eSMartin Matuska 				v6Literal = strchr(host, ']');
1676ba2210eSMartin Matuska 				if (v6Literal == NULL) {
1686ba2210eSMartin Matuska 					free(host_dup);
1696ba2210eSMartin Matuska 					return (SA_SYNTAX_ERR);
1706ba2210eSMartin Matuska 				}
1716ba2210eSMartin Matuska 				if (v6Literal[1] == '\0') {
1726ba2210eSMartin Matuska 					*v6Literal = '\0';
1736ba2210eSMartin Matuska 					next = NULL;
1746ba2210eSMartin Matuska 				} else if (v6Literal[1] == '/') {
1756ba2210eSMartin Matuska 					next = strchr(v6Literal + 2, ':');
1766ba2210eSMartin Matuska 					if (next == NULL) {
1776ba2210eSMartin Matuska 						cidr_len =
1786ba2210eSMartin Matuska 						    strlen(v6Literal + 1);
1796ba2210eSMartin Matuska 						memmove(v6Literal,
1806ba2210eSMartin Matuska 						    v6Literal + 1,
1816ba2210eSMartin Matuska 						    cidr_len);
1826ba2210eSMartin Matuska 						v6Literal[cidr_len] = '\0';
1836ba2210eSMartin Matuska 					} else {
1846ba2210eSMartin Matuska 						cidr_len = next - v6Literal - 1;
1856ba2210eSMartin Matuska 						memmove(v6Literal,
1866ba2210eSMartin Matuska 						    v6Literal + 1,
1876ba2210eSMartin Matuska 						    cidr_len);
1886ba2210eSMartin Matuska 						v6Literal[cidr_len] = '\0';
1896ba2210eSMartin Matuska 						next++;
1906ba2210eSMartin Matuska 					}
1916ba2210eSMartin Matuska 				} else if (v6Literal[1] == ':') {
1926ba2210eSMartin Matuska 					*v6Literal = '\0';
1936ba2210eSMartin Matuska 					next = v6Literal + 2;
1946ba2210eSMartin Matuska 				} else {
1956ba2210eSMartin Matuska 					free(host_dup);
1966ba2210eSMartin Matuska 					return (SA_SYNTAX_ERR);
1976ba2210eSMartin Matuska 				}
1986ba2210eSMartin Matuska 			} else {
199eda14cbcSMatt Macy 				next = strchr(host, ':');
200eda14cbcSMatt Macy 				if (next != NULL) {
201eda14cbcSMatt Macy 					*next = '\0';
202eda14cbcSMatt Macy 					next++;
203eda14cbcSMatt Macy 				}
2046ba2210eSMartin Matuska 			}
205eda14cbcSMatt Macy 
206*e92ffd9bSMartin Matuska 			error = udata->callback(udata->tmpfile,
207eda14cbcSMatt Macy 			    udata->sharepath, host, udata->security,
208eda14cbcSMatt Macy 			    access, udata->cookie);
209eda14cbcSMatt Macy 
210eda14cbcSMatt Macy 			if (error != SA_OK) {
211eda14cbcSMatt Macy 				free(host_dup);
212eda14cbcSMatt Macy 
213eda14cbcSMatt Macy 				return (error);
214eda14cbcSMatt Macy 			}
215eda14cbcSMatt Macy 
216eda14cbcSMatt Macy 			host = next;
217eda14cbcSMatt Macy 		} while (host != NULL);
218eda14cbcSMatt Macy 
219eda14cbcSMatt Macy 		free(host_dup);
220eda14cbcSMatt Macy 	}
221eda14cbcSMatt Macy 
222eda14cbcSMatt Macy 	return (SA_OK);
223eda14cbcSMatt Macy }
224eda14cbcSMatt Macy 
225eda14cbcSMatt Macy /*
226eda14cbcSMatt Macy  * Invokes a callback function for all NFS hosts that are set for a share.
227eda14cbcSMatt Macy  */
228eda14cbcSMatt Macy static int
229*e92ffd9bSMartin Matuska foreach_nfs_host(sa_share_impl_t impl_share, FILE *tmpfile,
230eda14cbcSMatt Macy     nfs_host_callback_t callback, void *cookie)
231eda14cbcSMatt Macy {
232eda14cbcSMatt Macy 	nfs_host_cookie_t udata;
233eda14cbcSMatt Macy 	char *shareopts;
234eda14cbcSMatt Macy 
235eda14cbcSMatt Macy 	udata.callback = callback;
236eda14cbcSMatt Macy 	udata.sharepath = impl_share->sa_mountpoint;
237eda14cbcSMatt Macy 	udata.cookie = cookie;
238*e92ffd9bSMartin Matuska 	udata.tmpfile = tmpfile;
239eda14cbcSMatt Macy 	udata.security = "sys";
240eda14cbcSMatt Macy 
241eda14cbcSMatt Macy 	shareopts = FSINFO(impl_share, nfs_fstype)->shareopts;
242eda14cbcSMatt Macy 
243eda14cbcSMatt Macy 	return (foreach_nfs_shareopt(shareopts, foreach_nfs_host_cb,
244eda14cbcSMatt Macy 	    &udata));
245eda14cbcSMatt Macy }
246eda14cbcSMatt Macy 
247eda14cbcSMatt Macy /*
248eda14cbcSMatt Macy  * Converts a Solaris NFS host specification to its Linux equivalent.
249eda14cbcSMatt Macy  */
250*e92ffd9bSMartin Matuska static const char *
251*e92ffd9bSMartin Matuska get_linux_hostspec(const char *solaris_hostspec)
252eda14cbcSMatt Macy {
253eda14cbcSMatt Macy 	/*
254eda14cbcSMatt Macy 	 * For now we just support CIDR masks (e.g. @192.168.0.0/16) and host
255eda14cbcSMatt Macy 	 * wildcards (e.g. *.example.org).
256eda14cbcSMatt Macy 	 */
257eda14cbcSMatt Macy 	if (solaris_hostspec[0] == '@') {
258eda14cbcSMatt Macy 		/*
259eda14cbcSMatt Macy 		 * Solaris host specifier, e.g. @192.168.0.0/16; we just need
260eda14cbcSMatt Macy 		 * to skip the @ in this case
261eda14cbcSMatt Macy 		 */
262*e92ffd9bSMartin Matuska 		return (solaris_hostspec + 1);
263eda14cbcSMatt Macy 	} else {
264*e92ffd9bSMartin Matuska 		return (solaris_hostspec);
265eda14cbcSMatt Macy 	}
266eda14cbcSMatt Macy }
267eda14cbcSMatt Macy 
268eda14cbcSMatt Macy /*
269eda14cbcSMatt Macy  * Adds a Linux share option to an array of NFS options.
270eda14cbcSMatt Macy  */
271eda14cbcSMatt Macy static int
272eda14cbcSMatt Macy add_linux_shareopt(char **plinux_opts, const char *key, const char *value)
273eda14cbcSMatt Macy {
274eda14cbcSMatt Macy 	size_t len = 0;
275eda14cbcSMatt Macy 	char *new_linux_opts;
276eda14cbcSMatt Macy 
277eda14cbcSMatt Macy 	if (*plinux_opts != NULL)
278eda14cbcSMatt Macy 		len = strlen(*plinux_opts);
279eda14cbcSMatt Macy 
280eda14cbcSMatt Macy 	new_linux_opts = realloc(*plinux_opts, len + 1 + strlen(key) +
281eda14cbcSMatt Macy 	    (value ? 1 + strlen(value) : 0) + 1);
282eda14cbcSMatt Macy 
283eda14cbcSMatt Macy 	if (new_linux_opts == NULL)
284eda14cbcSMatt Macy 		return (SA_NO_MEMORY);
285eda14cbcSMatt Macy 
286eda14cbcSMatt Macy 	new_linux_opts[len] = '\0';
287eda14cbcSMatt Macy 
288eda14cbcSMatt Macy 	if (len > 0)
289eda14cbcSMatt Macy 		strcat(new_linux_opts, ",");
290eda14cbcSMatt Macy 
291eda14cbcSMatt Macy 	strcat(new_linux_opts, key);
292eda14cbcSMatt Macy 
293eda14cbcSMatt Macy 	if (value != NULL) {
294eda14cbcSMatt Macy 		strcat(new_linux_opts, "=");
295eda14cbcSMatt Macy 		strcat(new_linux_opts, value);
296eda14cbcSMatt Macy 	}
297eda14cbcSMatt Macy 
298eda14cbcSMatt Macy 	*plinux_opts = new_linux_opts;
299eda14cbcSMatt Macy 
300eda14cbcSMatt Macy 	return (SA_OK);
301eda14cbcSMatt Macy }
302eda14cbcSMatt Macy 
303eda14cbcSMatt Macy /*
304eda14cbcSMatt Macy  * Validates and converts a single Solaris share option to its Linux
305eda14cbcSMatt Macy  * equivalent.
306eda14cbcSMatt Macy  */
307eda14cbcSMatt Macy static int
308eda14cbcSMatt Macy get_linux_shareopts_cb(const char *key, const char *value, void *cookie)
309eda14cbcSMatt Macy {
310eda14cbcSMatt Macy 	char **plinux_opts = (char **)cookie;
311eda14cbcSMatt Macy 
312eda14cbcSMatt Macy 	/* host-specific options, these are taken care of elsewhere */
313eda14cbcSMatt Macy 	if (strcmp(key, "ro") == 0 || strcmp(key, "rw") == 0 ||
314eda14cbcSMatt Macy 	    strcmp(key, "sec") == 0)
315eda14cbcSMatt Macy 		return (SA_OK);
316eda14cbcSMatt Macy 
317eda14cbcSMatt Macy 	if (strcmp(key, "anon") == 0)
318eda14cbcSMatt Macy 		key = "anonuid";
319eda14cbcSMatt Macy 
320eda14cbcSMatt Macy 	if (strcmp(key, "root_mapping") == 0) {
321eda14cbcSMatt Macy 		(void) add_linux_shareopt(plinux_opts, "root_squash", NULL);
322eda14cbcSMatt Macy 		key = "anonuid";
323eda14cbcSMatt Macy 	}
324eda14cbcSMatt Macy 
325eda14cbcSMatt Macy 	if (strcmp(key, "nosub") == 0)
326eda14cbcSMatt Macy 		key = "subtree_check";
327eda14cbcSMatt Macy 
328eda14cbcSMatt Macy 	if (strcmp(key, "insecure") != 0 && strcmp(key, "secure") != 0 &&
329eda14cbcSMatt Macy 	    strcmp(key, "async") != 0 && strcmp(key, "sync") != 0 &&
330eda14cbcSMatt Macy 	    strcmp(key, "no_wdelay") != 0 && strcmp(key, "wdelay") != 0 &&
331eda14cbcSMatt Macy 	    strcmp(key, "nohide") != 0 && strcmp(key, "hide") != 0 &&
332eda14cbcSMatt Macy 	    strcmp(key, "crossmnt") != 0 &&
333eda14cbcSMatt Macy 	    strcmp(key, "no_subtree_check") != 0 &&
334eda14cbcSMatt Macy 	    strcmp(key, "subtree_check") != 0 &&
335eda14cbcSMatt Macy 	    strcmp(key, "insecure_locks") != 0 &&
336eda14cbcSMatt Macy 	    strcmp(key, "secure_locks") != 0 &&
337eda14cbcSMatt Macy 	    strcmp(key, "no_auth_nlm") != 0 && strcmp(key, "auth_nlm") != 0 &&
338eda14cbcSMatt Macy 	    strcmp(key, "no_acl") != 0 && strcmp(key, "mountpoint") != 0 &&
339eda14cbcSMatt Macy 	    strcmp(key, "mp") != 0 && strcmp(key, "fsuid") != 0 &&
340eda14cbcSMatt Macy 	    strcmp(key, "refer") != 0 && strcmp(key, "replicas") != 0 &&
341eda14cbcSMatt Macy 	    strcmp(key, "root_squash") != 0 &&
342eda14cbcSMatt Macy 	    strcmp(key, "no_root_squash") != 0 &&
343eda14cbcSMatt Macy 	    strcmp(key, "all_squash") != 0 &&
344eda14cbcSMatt Macy 	    strcmp(key, "no_all_squash") != 0 && strcmp(key, "fsid") != 0 &&
345eda14cbcSMatt Macy 	    strcmp(key, "anonuid") != 0 && strcmp(key, "anongid") != 0) {
346eda14cbcSMatt Macy 		return (SA_SYNTAX_ERR);
347eda14cbcSMatt Macy 	}
348eda14cbcSMatt Macy 
349eda14cbcSMatt Macy 	(void) add_linux_shareopt(plinux_opts, key, value);
350eda14cbcSMatt Macy 
351eda14cbcSMatt Macy 	return (SA_OK);
352eda14cbcSMatt Macy }
353eda14cbcSMatt Macy 
354eda14cbcSMatt Macy /*
355eda14cbcSMatt Macy  * Takes a string containing Solaris share options (e.g. "sync,no_acl") and
356eda14cbcSMatt Macy  * converts them to a NULL-terminated array of Linux NFS options.
357eda14cbcSMatt Macy  */
358eda14cbcSMatt Macy static int
359eda14cbcSMatt Macy get_linux_shareopts(const char *shareopts, char **plinux_opts)
360eda14cbcSMatt Macy {
361eda14cbcSMatt Macy 	int error;
362eda14cbcSMatt Macy 
363eda14cbcSMatt Macy 	assert(plinux_opts != NULL);
364eda14cbcSMatt Macy 
365eda14cbcSMatt Macy 	*plinux_opts = NULL;
366eda14cbcSMatt Macy 
367eda14cbcSMatt Macy 	/* no_subtree_check - Default as of nfs-utils v1.1.0 */
368eda14cbcSMatt Macy 	(void) add_linux_shareopt(plinux_opts, "no_subtree_check", NULL);
369eda14cbcSMatt Macy 
370eda14cbcSMatt Macy 	/* mountpoint - Restrict exports to ZFS mountpoints */
371eda14cbcSMatt Macy 	(void) add_linux_shareopt(plinux_opts, "mountpoint", NULL);
372eda14cbcSMatt Macy 
373eda14cbcSMatt Macy 	error = foreach_nfs_shareopt(shareopts, get_linux_shareopts_cb,
374eda14cbcSMatt Macy 	    plinux_opts);
375eda14cbcSMatt Macy 
376eda14cbcSMatt Macy 	if (error != SA_OK) {
377eda14cbcSMatt Macy 		free(*plinux_opts);
378eda14cbcSMatt Macy 		*plinux_opts = NULL;
379eda14cbcSMatt Macy 	}
380eda14cbcSMatt Macy 
381eda14cbcSMatt Macy 	return (error);
382eda14cbcSMatt Macy }
383eda14cbcSMatt Macy 
384eda14cbcSMatt Macy /*
385eda14cbcSMatt Macy  * This function populates an entry into /etc/exports.d/zfs.exports.
386eda14cbcSMatt Macy  * This file is consumed by the linux nfs server so that zfs shares are
387eda14cbcSMatt Macy  * automatically exported upon boot or whenever the nfs server restarts.
388eda14cbcSMatt Macy  */
389eda14cbcSMatt Macy static int
390*e92ffd9bSMartin Matuska nfs_add_entry(FILE *tmpfile, const char *sharepath,
391eda14cbcSMatt Macy     const char *host, const char *security, const char *access_opts,
392eda14cbcSMatt Macy     void *pcookie)
393eda14cbcSMatt Macy {
394eda14cbcSMatt Macy 	const char *linux_opts = (const char *)pcookie;
395eda14cbcSMatt Macy 
396eda14cbcSMatt Macy 	if (linux_opts == NULL)
397eda14cbcSMatt Macy 		linux_opts = "";
398eda14cbcSMatt Macy 
399*e92ffd9bSMartin Matuska 	if (fprintf(tmpfile, "%s %s(sec=%s,%s,%s)\n", sharepath,
400*e92ffd9bSMartin Matuska 	    get_linux_hostspec(host), security, access_opts,
401*e92ffd9bSMartin Matuska 	    linux_opts) < 0) {
402*e92ffd9bSMartin Matuska 		fprintf(stderr, "failed to write to temporary file\n");
403eda14cbcSMatt Macy 		return (SA_SYSTEM_ERR);
404eda14cbcSMatt Macy 	}
405eda14cbcSMatt Macy 
406eda14cbcSMatt Macy 	return (SA_OK);
407eda14cbcSMatt Macy }
408eda14cbcSMatt Macy 
409eda14cbcSMatt Macy /*
410eda14cbcSMatt Macy  * Enables NFS sharing for the specified share.
411eda14cbcSMatt Macy  */
412eda14cbcSMatt Macy static int
413*e92ffd9bSMartin Matuska nfs_enable_share_impl(sa_share_impl_t impl_share, FILE *tmpfile)
414eda14cbcSMatt Macy {
415eda14cbcSMatt Macy 	char *shareopts, *linux_opts;
416eda14cbcSMatt Macy 	int error;
417eda14cbcSMatt Macy 
418eda14cbcSMatt Macy 	shareopts = FSINFO(impl_share, nfs_fstype)->shareopts;
419eda14cbcSMatt Macy 	error = get_linux_shareopts(shareopts, &linux_opts);
42016038816SMartin Matuska 	if (error != SA_OK)
421eda14cbcSMatt Macy 		return (error);
422eda14cbcSMatt Macy 
423*e92ffd9bSMartin Matuska 	error = foreach_nfs_host(impl_share, tmpfile, nfs_add_entry,
424eda14cbcSMatt Macy 	    linux_opts);
425eda14cbcSMatt Macy 	free(linux_opts);
426eda14cbcSMatt Macy 	return (error);
427eda14cbcSMatt Macy }
428eda14cbcSMatt Macy 
42916038816SMartin Matuska static int
43016038816SMartin Matuska nfs_enable_share(sa_share_impl_t impl_share)
43116038816SMartin Matuska {
43216038816SMartin Matuska 	return (nfs_toggle_share(
43316038816SMartin Matuska 	    ZFS_EXPORTS_LOCK, ZFS_EXPORTS_FILE, ZFS_EXPORTS_DIR, impl_share,
43416038816SMartin Matuska 	    nfs_enable_share_impl));
43516038816SMartin Matuska }
43616038816SMartin Matuska 
437eda14cbcSMatt Macy /*
438eda14cbcSMatt Macy  * Disables NFS sharing for the specified share.
439eda14cbcSMatt Macy  */
440eda14cbcSMatt Macy static int
441*e92ffd9bSMartin Matuska nfs_disable_share_impl(sa_share_impl_t impl_share, FILE *tmpfile)
44216038816SMartin Matuska {
443*e92ffd9bSMartin Matuska 	(void) impl_share, (void) tmpfile;
44416038816SMartin Matuska 	return (SA_OK);
44516038816SMartin Matuska }
44616038816SMartin Matuska 
44716038816SMartin Matuska static int
448eda14cbcSMatt Macy nfs_disable_share(sa_share_impl_t impl_share)
449eda14cbcSMatt Macy {
45016038816SMartin Matuska 	return (nfs_toggle_share(
45116038816SMartin Matuska 	    ZFS_EXPORTS_LOCK, ZFS_EXPORTS_FILE, ZFS_EXPORTS_DIR, impl_share,
45216038816SMartin Matuska 	    nfs_disable_share_impl));
453eda14cbcSMatt Macy }
454eda14cbcSMatt Macy 
455eda14cbcSMatt Macy static boolean_t
456eda14cbcSMatt Macy nfs_is_shared(sa_share_impl_t impl_share)
457eda14cbcSMatt Macy {
458*e92ffd9bSMartin Matuska 	return (nfs_is_shared_impl(ZFS_EXPORTS_FILE, impl_share));
459eda14cbcSMatt Macy }
460eda14cbcSMatt Macy 
461eda14cbcSMatt Macy /*
462eda14cbcSMatt Macy  * Checks whether the specified NFS share options are syntactically correct.
463eda14cbcSMatt Macy  */
464eda14cbcSMatt Macy static int
465eda14cbcSMatt Macy nfs_validate_shareopts(const char *shareopts)
466eda14cbcSMatt Macy {
467eda14cbcSMatt Macy 	char *linux_opts;
468eda14cbcSMatt Macy 	int error;
469eda14cbcSMatt Macy 
470eda14cbcSMatt Macy 	error = get_linux_shareopts(shareopts, &linux_opts);
471eda14cbcSMatt Macy 
472eda14cbcSMatt Macy 	if (error != SA_OK)
473eda14cbcSMatt Macy 		return (error);
474eda14cbcSMatt Macy 
475eda14cbcSMatt Macy 	free(linux_opts);
476eda14cbcSMatt Macy 	return (SA_OK);
477eda14cbcSMatt Macy }
478eda14cbcSMatt Macy 
479eda14cbcSMatt Macy static int
480eda14cbcSMatt Macy nfs_update_shareopts(sa_share_impl_t impl_share, const char *shareopts)
481eda14cbcSMatt Macy {
482eda14cbcSMatt Macy 	FSINFO(impl_share, nfs_fstype)->shareopts = (char *)shareopts;
483eda14cbcSMatt Macy 	return (SA_OK);
484eda14cbcSMatt Macy }
485eda14cbcSMatt Macy 
486eda14cbcSMatt Macy /*
487eda14cbcSMatt Macy  * Clears a share's NFS options. Used by libshare to
488eda14cbcSMatt Macy  * clean up shares that are about to be free()'d.
489eda14cbcSMatt Macy  */
490eda14cbcSMatt Macy static void
491eda14cbcSMatt Macy nfs_clear_shareopts(sa_share_impl_t impl_share)
492eda14cbcSMatt Macy {
493eda14cbcSMatt Macy 	FSINFO(impl_share, nfs_fstype)->shareopts = NULL;
494eda14cbcSMatt Macy }
495eda14cbcSMatt Macy 
496eda14cbcSMatt Macy static int
497eda14cbcSMatt Macy nfs_commit_shares(void)
498eda14cbcSMatt Macy {
499eda14cbcSMatt Macy 	char *argv[] = {
500eda14cbcSMatt Macy 	    "/usr/sbin/exportfs",
501eda14cbcSMatt Macy 	    "-ra",
502eda14cbcSMatt Macy 	    NULL
503eda14cbcSMatt Macy 	};
504eda14cbcSMatt Macy 
505eda14cbcSMatt Macy 	return (libzfs_run_process(argv[0], argv, 0));
506eda14cbcSMatt Macy }
507eda14cbcSMatt Macy 
508eda14cbcSMatt Macy static const sa_share_ops_t nfs_shareops = {
509eda14cbcSMatt Macy 	.enable_share = nfs_enable_share,
510eda14cbcSMatt Macy 	.disable_share = nfs_disable_share,
511eda14cbcSMatt Macy 	.is_shared = nfs_is_shared,
512eda14cbcSMatt Macy 
513eda14cbcSMatt Macy 	.validate_shareopts = nfs_validate_shareopts,
514eda14cbcSMatt Macy 	.update_shareopts = nfs_update_shareopts,
515eda14cbcSMatt Macy 	.clear_shareopts = nfs_clear_shareopts,
516eda14cbcSMatt Macy 	.commit_shares = nfs_commit_shares,
517eda14cbcSMatt Macy };
518eda14cbcSMatt Macy 
519eda14cbcSMatt Macy /*
520eda14cbcSMatt Macy  * Initializes the NFS functionality of libshare.
521eda14cbcSMatt Macy  */
522eda14cbcSMatt Macy void
523eda14cbcSMatt Macy libshare_nfs_init(void)
524eda14cbcSMatt Macy {
525eda14cbcSMatt Macy 	nfs_fstype = register_fstype("nfs", &nfs_shareops);
526eda14cbcSMatt Macy }
527