xref: /freebsd-src/sys/contrib/openzfs/lib/libshare/os/linux/nfs.c (revision 6ba2210ee039f2f12878c217bcf058e9c8b26b29)
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 
54eda14cbcSMatt Macy typedef int (*nfs_host_callback_t)(const char *sharepath, const char *filename,
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;
125eda14cbcSMatt Macy 	const char *filename;
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;
139*6ba2210eSMartin Matuska 	char *host_dup, *host, *next, *v6Literal;
140eda14cbcSMatt Macy 	nfs_host_cookie_t *udata = (nfs_host_cookie_t *)pcookie;
141*6ba2210eSMartin 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 {
164*6ba2210eSMartin Matuska 			if (*host == '[') {
165*6ba2210eSMartin Matuska 				host++;
166*6ba2210eSMartin Matuska 				v6Literal = strchr(host, ']');
167*6ba2210eSMartin Matuska 				if (v6Literal == NULL) {
168*6ba2210eSMartin Matuska 					free(host_dup);
169*6ba2210eSMartin Matuska 					return (SA_SYNTAX_ERR);
170*6ba2210eSMartin Matuska 				}
171*6ba2210eSMartin Matuska 				if (v6Literal[1] == '\0') {
172*6ba2210eSMartin Matuska 					*v6Literal = '\0';
173*6ba2210eSMartin Matuska 					next = NULL;
174*6ba2210eSMartin Matuska 				} else if (v6Literal[1] == '/') {
175*6ba2210eSMartin Matuska 					next = strchr(v6Literal + 2, ':');
176*6ba2210eSMartin Matuska 					if (next == NULL) {
177*6ba2210eSMartin Matuska 						cidr_len =
178*6ba2210eSMartin Matuska 						    strlen(v6Literal + 1);
179*6ba2210eSMartin Matuska 						memmove(v6Literal,
180*6ba2210eSMartin Matuska 						    v6Literal + 1,
181*6ba2210eSMartin Matuska 						    cidr_len);
182*6ba2210eSMartin Matuska 						v6Literal[cidr_len] = '\0';
183*6ba2210eSMartin Matuska 					} else {
184*6ba2210eSMartin Matuska 						cidr_len = next - v6Literal - 1;
185*6ba2210eSMartin Matuska 						memmove(v6Literal,
186*6ba2210eSMartin Matuska 						    v6Literal + 1,
187*6ba2210eSMartin Matuska 						    cidr_len);
188*6ba2210eSMartin Matuska 						v6Literal[cidr_len] = '\0';
189*6ba2210eSMartin Matuska 						next++;
190*6ba2210eSMartin Matuska 					}
191*6ba2210eSMartin Matuska 				} else if (v6Literal[1] == ':') {
192*6ba2210eSMartin Matuska 					*v6Literal = '\0';
193*6ba2210eSMartin Matuska 					next = v6Literal + 2;
194*6ba2210eSMartin Matuska 				} else {
195*6ba2210eSMartin Matuska 					free(host_dup);
196*6ba2210eSMartin Matuska 					return (SA_SYNTAX_ERR);
197*6ba2210eSMartin Matuska 				}
198*6ba2210eSMartin Matuska 			} else {
199eda14cbcSMatt Macy 				next = strchr(host, ':');
200eda14cbcSMatt Macy 				if (next != NULL) {
201eda14cbcSMatt Macy 					*next = '\0';
202eda14cbcSMatt Macy 					next++;
203eda14cbcSMatt Macy 				}
204*6ba2210eSMartin Matuska 			}
205eda14cbcSMatt Macy 
206eda14cbcSMatt Macy 			error = udata->callback(udata->filename,
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
229eda14cbcSMatt Macy foreach_nfs_host(sa_share_impl_t impl_share, char *filename,
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;
238eda14cbcSMatt Macy 	udata.filename = filename;
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  */
250eda14cbcSMatt Macy static int
251eda14cbcSMatt Macy get_linux_hostspec(const char *solaris_hostspec, char **plinux_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 		 */
262eda14cbcSMatt Macy 		*plinux_hostspec = strdup(solaris_hostspec + 1);
263eda14cbcSMatt Macy 	} else {
264eda14cbcSMatt Macy 		*plinux_hostspec = strdup(solaris_hostspec);
265eda14cbcSMatt Macy 	}
266eda14cbcSMatt Macy 
267eda14cbcSMatt Macy 	if (*plinux_hostspec == NULL) {
268eda14cbcSMatt Macy 		return (SA_NO_MEMORY);
269eda14cbcSMatt Macy 	}
270eda14cbcSMatt Macy 
271eda14cbcSMatt Macy 	return (SA_OK);
272eda14cbcSMatt Macy }
273eda14cbcSMatt Macy 
274eda14cbcSMatt Macy /*
275eda14cbcSMatt Macy  * Adds a Linux share option to an array of NFS options.
276eda14cbcSMatt Macy  */
277eda14cbcSMatt Macy static int
278eda14cbcSMatt Macy add_linux_shareopt(char **plinux_opts, const char *key, const char *value)
279eda14cbcSMatt Macy {
280eda14cbcSMatt Macy 	size_t len = 0;
281eda14cbcSMatt Macy 	char *new_linux_opts;
282eda14cbcSMatt Macy 
283eda14cbcSMatt Macy 	if (*plinux_opts != NULL)
284eda14cbcSMatt Macy 		len = strlen(*plinux_opts);
285eda14cbcSMatt Macy 
286eda14cbcSMatt Macy 	new_linux_opts = realloc(*plinux_opts, len + 1 + strlen(key) +
287eda14cbcSMatt Macy 	    (value ? 1 + strlen(value) : 0) + 1);
288eda14cbcSMatt Macy 
289eda14cbcSMatt Macy 	if (new_linux_opts == NULL)
290eda14cbcSMatt Macy 		return (SA_NO_MEMORY);
291eda14cbcSMatt Macy 
292eda14cbcSMatt Macy 	new_linux_opts[len] = '\0';
293eda14cbcSMatt Macy 
294eda14cbcSMatt Macy 	if (len > 0)
295eda14cbcSMatt Macy 		strcat(new_linux_opts, ",");
296eda14cbcSMatt Macy 
297eda14cbcSMatt Macy 	strcat(new_linux_opts, key);
298eda14cbcSMatt Macy 
299eda14cbcSMatt Macy 	if (value != NULL) {
300eda14cbcSMatt Macy 		strcat(new_linux_opts, "=");
301eda14cbcSMatt Macy 		strcat(new_linux_opts, value);
302eda14cbcSMatt Macy 	}
303eda14cbcSMatt Macy 
304eda14cbcSMatt Macy 	*plinux_opts = new_linux_opts;
305eda14cbcSMatt Macy 
306eda14cbcSMatt Macy 	return (SA_OK);
307eda14cbcSMatt Macy }
308eda14cbcSMatt Macy 
309eda14cbcSMatt Macy /*
310eda14cbcSMatt Macy  * Validates and converts a single Solaris share option to its Linux
311eda14cbcSMatt Macy  * equivalent.
312eda14cbcSMatt Macy  */
313eda14cbcSMatt Macy static int
314eda14cbcSMatt Macy get_linux_shareopts_cb(const char *key, const char *value, void *cookie)
315eda14cbcSMatt Macy {
316eda14cbcSMatt Macy 	char **plinux_opts = (char **)cookie;
317eda14cbcSMatt Macy 
318eda14cbcSMatt Macy 	/* host-specific options, these are taken care of elsewhere */
319eda14cbcSMatt Macy 	if (strcmp(key, "ro") == 0 || strcmp(key, "rw") == 0 ||
320eda14cbcSMatt Macy 	    strcmp(key, "sec") == 0)
321eda14cbcSMatt Macy 		return (SA_OK);
322eda14cbcSMatt Macy 
323eda14cbcSMatt Macy 	if (strcmp(key, "anon") == 0)
324eda14cbcSMatt Macy 		key = "anonuid";
325eda14cbcSMatt Macy 
326eda14cbcSMatt Macy 	if (strcmp(key, "root_mapping") == 0) {
327eda14cbcSMatt Macy 		(void) add_linux_shareopt(plinux_opts, "root_squash", NULL);
328eda14cbcSMatt Macy 		key = "anonuid";
329eda14cbcSMatt Macy 	}
330eda14cbcSMatt Macy 
331eda14cbcSMatt Macy 	if (strcmp(key, "nosub") == 0)
332eda14cbcSMatt Macy 		key = "subtree_check";
333eda14cbcSMatt Macy 
334eda14cbcSMatt Macy 	if (strcmp(key, "insecure") != 0 && strcmp(key, "secure") != 0 &&
335eda14cbcSMatt Macy 	    strcmp(key, "async") != 0 && strcmp(key, "sync") != 0 &&
336eda14cbcSMatt Macy 	    strcmp(key, "no_wdelay") != 0 && strcmp(key, "wdelay") != 0 &&
337eda14cbcSMatt Macy 	    strcmp(key, "nohide") != 0 && strcmp(key, "hide") != 0 &&
338eda14cbcSMatt Macy 	    strcmp(key, "crossmnt") != 0 &&
339eda14cbcSMatt Macy 	    strcmp(key, "no_subtree_check") != 0 &&
340eda14cbcSMatt Macy 	    strcmp(key, "subtree_check") != 0 &&
341eda14cbcSMatt Macy 	    strcmp(key, "insecure_locks") != 0 &&
342eda14cbcSMatt Macy 	    strcmp(key, "secure_locks") != 0 &&
343eda14cbcSMatt Macy 	    strcmp(key, "no_auth_nlm") != 0 && strcmp(key, "auth_nlm") != 0 &&
344eda14cbcSMatt Macy 	    strcmp(key, "no_acl") != 0 && strcmp(key, "mountpoint") != 0 &&
345eda14cbcSMatt Macy 	    strcmp(key, "mp") != 0 && strcmp(key, "fsuid") != 0 &&
346eda14cbcSMatt Macy 	    strcmp(key, "refer") != 0 && strcmp(key, "replicas") != 0 &&
347eda14cbcSMatt Macy 	    strcmp(key, "root_squash") != 0 &&
348eda14cbcSMatt Macy 	    strcmp(key, "no_root_squash") != 0 &&
349eda14cbcSMatt Macy 	    strcmp(key, "all_squash") != 0 &&
350eda14cbcSMatt Macy 	    strcmp(key, "no_all_squash") != 0 && strcmp(key, "fsid") != 0 &&
351eda14cbcSMatt Macy 	    strcmp(key, "anonuid") != 0 && strcmp(key, "anongid") != 0) {
352eda14cbcSMatt Macy 		return (SA_SYNTAX_ERR);
353eda14cbcSMatt Macy 	}
354eda14cbcSMatt Macy 
355eda14cbcSMatt Macy 	(void) add_linux_shareopt(plinux_opts, key, value);
356eda14cbcSMatt Macy 
357eda14cbcSMatt Macy 	return (SA_OK);
358eda14cbcSMatt Macy }
359eda14cbcSMatt Macy 
360eda14cbcSMatt Macy /*
361eda14cbcSMatt Macy  * Takes a string containing Solaris share options (e.g. "sync,no_acl") and
362eda14cbcSMatt Macy  * converts them to a NULL-terminated array of Linux NFS options.
363eda14cbcSMatt Macy  */
364eda14cbcSMatt Macy static int
365eda14cbcSMatt Macy get_linux_shareopts(const char *shareopts, char **plinux_opts)
366eda14cbcSMatt Macy {
367eda14cbcSMatt Macy 	int error;
368eda14cbcSMatt Macy 
369eda14cbcSMatt Macy 	assert(plinux_opts != NULL);
370eda14cbcSMatt Macy 
371eda14cbcSMatt Macy 	*plinux_opts = NULL;
372eda14cbcSMatt Macy 
373eda14cbcSMatt Macy 	/* no_subtree_check - Default as of nfs-utils v1.1.0 */
374eda14cbcSMatt Macy 	(void) add_linux_shareopt(plinux_opts, "no_subtree_check", NULL);
375eda14cbcSMatt Macy 
376eda14cbcSMatt Macy 	/* mountpoint - Restrict exports to ZFS mountpoints */
377eda14cbcSMatt Macy 	(void) add_linux_shareopt(plinux_opts, "mountpoint", NULL);
378eda14cbcSMatt Macy 
379eda14cbcSMatt Macy 	error = foreach_nfs_shareopt(shareopts, get_linux_shareopts_cb,
380eda14cbcSMatt Macy 	    plinux_opts);
381eda14cbcSMatt Macy 
382eda14cbcSMatt Macy 	if (error != SA_OK) {
383eda14cbcSMatt Macy 		free(*plinux_opts);
384eda14cbcSMatt Macy 		*plinux_opts = NULL;
385eda14cbcSMatt Macy 	}
386eda14cbcSMatt Macy 
387eda14cbcSMatt Macy 	return (error);
388eda14cbcSMatt Macy }
389eda14cbcSMatt Macy 
390eda14cbcSMatt Macy /*
391eda14cbcSMatt Macy  * This function populates an entry into /etc/exports.d/zfs.exports.
392eda14cbcSMatt Macy  * This file is consumed by the linux nfs server so that zfs shares are
393eda14cbcSMatt Macy  * automatically exported upon boot or whenever the nfs server restarts.
394eda14cbcSMatt Macy  */
395eda14cbcSMatt Macy static int
396eda14cbcSMatt Macy nfs_add_entry(const char *filename, const char *sharepath,
397eda14cbcSMatt Macy     const char *host, const char *security, const char *access_opts,
398eda14cbcSMatt Macy     void *pcookie)
399eda14cbcSMatt Macy {
400eda14cbcSMatt Macy 	int error;
401eda14cbcSMatt Macy 	char *linuxhost;
402eda14cbcSMatt Macy 	const char *linux_opts = (const char *)pcookie;
403eda14cbcSMatt Macy 
404eda14cbcSMatt Macy 	error = get_linux_hostspec(host, &linuxhost);
405eda14cbcSMatt Macy 	if (error != SA_OK)
406eda14cbcSMatt Macy 		return (error);
407eda14cbcSMatt Macy 
408eda14cbcSMatt Macy 	if (linux_opts == NULL)
409eda14cbcSMatt Macy 		linux_opts = "";
410eda14cbcSMatt Macy 
41116038816SMartin Matuska 	FILE *fp = fopen(filename, "a+e");
412eda14cbcSMatt Macy 	if (fp == NULL) {
413eda14cbcSMatt Macy 		fprintf(stderr, "failed to open %s file: %s", filename,
414eda14cbcSMatt Macy 		    strerror(errno));
415eda14cbcSMatt Macy 		free(linuxhost);
416eda14cbcSMatt Macy 		return (SA_SYSTEM_ERR);
417eda14cbcSMatt Macy 	}
418eda14cbcSMatt Macy 
419eda14cbcSMatt Macy 	if (fprintf(fp, "%s %s(sec=%s,%s,%s)\n", sharepath, linuxhost,
420eda14cbcSMatt Macy 	    security, access_opts, linux_opts) < 0) {
421eda14cbcSMatt Macy 		fprintf(stderr, "failed to write to %s\n", filename);
422eda14cbcSMatt Macy 		free(linuxhost);
423eda14cbcSMatt Macy 		fclose(fp);
424eda14cbcSMatt Macy 		return (SA_SYSTEM_ERR);
425eda14cbcSMatt Macy 	}
426eda14cbcSMatt Macy 
427eda14cbcSMatt Macy 	free(linuxhost);
428eda14cbcSMatt Macy 	if (fclose(fp) != 0) {
429eda14cbcSMatt Macy 		fprintf(stderr, "Unable to close file %s: %s\n",
430eda14cbcSMatt Macy 		    filename, strerror(errno));
431eda14cbcSMatt Macy 		return (SA_SYSTEM_ERR);
432eda14cbcSMatt Macy 	}
433eda14cbcSMatt Macy 	return (SA_OK);
434eda14cbcSMatt Macy }
435eda14cbcSMatt Macy 
436eda14cbcSMatt Macy /*
437eda14cbcSMatt Macy  * This function copies all entries from the exports file to "filename",
438eda14cbcSMatt Macy  * omitting any entries for the specified mountpoint.
439eda14cbcSMatt Macy  */
4403ff01b23SMartin Matuska int
441eda14cbcSMatt Macy nfs_copy_entries(char *filename, const char *mountpoint)
442eda14cbcSMatt Macy {
443eda14cbcSMatt Macy 	char *buf = NULL;
444eda14cbcSMatt Macy 	size_t buflen = 0;
445eda14cbcSMatt Macy 	int error = SA_OK;
446eda14cbcSMatt Macy 
44716038816SMartin Matuska 	FILE *oldfp = fopen(ZFS_EXPORTS_FILE, "re");
44816038816SMartin Matuska 	FILE *newfp = fopen(filename, "w+e");
449c40487d4SMatt Macy 	if (newfp == NULL) {
450c40487d4SMatt Macy 		fprintf(stderr, "failed to open %s file: %s", filename,
451c40487d4SMatt Macy 		    strerror(errno));
452c40487d4SMatt Macy 		fclose(oldfp);
453c40487d4SMatt Macy 		return (SA_SYSTEM_ERR);
454c40487d4SMatt Macy 	}
455eda14cbcSMatt Macy 	fputs(FILE_HEADER, newfp);
456c40487d4SMatt Macy 
457c40487d4SMatt Macy 	/*
458c40487d4SMatt Macy 	 * The ZFS_EXPORTS_FILE may not exist yet. If that's the
459c40487d4SMatt Macy 	 * case then just write out the new file.
460c40487d4SMatt Macy 	 */
461c40487d4SMatt Macy 	if (oldfp != NULL) {
462c40487d4SMatt Macy 		while (getline(&buf, &buflen, oldfp) != -1) {
463eda14cbcSMatt Macy 			char *space = NULL;
464eda14cbcSMatt Macy 
465eda14cbcSMatt Macy 			if (buf[0] == '\n' || buf[0] == '#')
466eda14cbcSMatt Macy 				continue;
467eda14cbcSMatt Macy 
468eda14cbcSMatt Macy 			if ((space = strchr(buf, ' ')) != NULL) {
469eda14cbcSMatt Macy 				int mountpoint_len = strlen(mountpoint);
470eda14cbcSMatt Macy 
471eda14cbcSMatt Macy 				if (space - buf == mountpoint_len &&
472c40487d4SMatt Macy 				    strncmp(mountpoint, buf,
473c40487d4SMatt Macy 				    mountpoint_len) == 0) {
474eda14cbcSMatt Macy 					continue;
475eda14cbcSMatt Macy 				}
476eda14cbcSMatt Macy 			}
477eda14cbcSMatt Macy 			fputs(buf, newfp);
478eda14cbcSMatt Macy 		}
479eda14cbcSMatt Macy 
480c40487d4SMatt Macy 		if (ferror(oldfp) != 0) {
481eda14cbcSMatt Macy 			error = ferror(oldfp);
482eda14cbcSMatt Macy 		}
483c40487d4SMatt Macy 		if (fclose(oldfp) != 0) {
484c40487d4SMatt Macy 			fprintf(stderr, "Unable to close file %s: %s\n",
485c40487d4SMatt Macy 			    filename, strerror(errno));
486c40487d4SMatt Macy 			error = error != 0 ? error : SA_SYSTEM_ERR;
487c40487d4SMatt Macy 		}
488c40487d4SMatt Macy 	}
489c40487d4SMatt Macy 
490eda14cbcSMatt Macy 	if (error == 0 && ferror(newfp) != 0) {
491eda14cbcSMatt Macy 		error = ferror(newfp);
492eda14cbcSMatt Macy 	}
493eda14cbcSMatt Macy 
494eda14cbcSMatt Macy 	free(buf);
495eda14cbcSMatt Macy 	if (fclose(newfp) != 0) {
496eda14cbcSMatt Macy 		fprintf(stderr, "Unable to close file %s: %s\n",
497eda14cbcSMatt Macy 		    filename, strerror(errno));
498eda14cbcSMatt Macy 		error = error != 0 ? error : SA_SYSTEM_ERR;
499eda14cbcSMatt Macy 	}
500eda14cbcSMatt Macy 	return (error);
501eda14cbcSMatt Macy }
502eda14cbcSMatt Macy 
503eda14cbcSMatt Macy /*
504eda14cbcSMatt Macy  * Enables NFS sharing for the specified share.
505eda14cbcSMatt Macy  */
506eda14cbcSMatt Macy static int
50716038816SMartin Matuska nfs_enable_share_impl(sa_share_impl_t impl_share, char *filename)
508eda14cbcSMatt Macy {
509eda14cbcSMatt Macy 	char *shareopts, *linux_opts;
510eda14cbcSMatt Macy 	int error;
511eda14cbcSMatt Macy 
512eda14cbcSMatt Macy 	shareopts = FSINFO(impl_share, nfs_fstype)->shareopts;
513eda14cbcSMatt Macy 	error = get_linux_shareopts(shareopts, &linux_opts);
51416038816SMartin Matuska 	if (error != SA_OK)
515eda14cbcSMatt Macy 		return (error);
516eda14cbcSMatt Macy 
517eda14cbcSMatt Macy 	error = foreach_nfs_host(impl_share, filename, nfs_add_entry,
518eda14cbcSMatt Macy 	    linux_opts);
519eda14cbcSMatt Macy 	free(linux_opts);
520eda14cbcSMatt Macy 	return (error);
521eda14cbcSMatt Macy }
522eda14cbcSMatt Macy 
52316038816SMartin Matuska static int
52416038816SMartin Matuska nfs_enable_share(sa_share_impl_t impl_share)
52516038816SMartin Matuska {
52616038816SMartin Matuska 	return (nfs_toggle_share(
52716038816SMartin Matuska 	    ZFS_EXPORTS_LOCK, ZFS_EXPORTS_FILE, ZFS_EXPORTS_DIR, impl_share,
52816038816SMartin Matuska 	    nfs_enable_share_impl));
52916038816SMartin Matuska }
53016038816SMartin Matuska 
531eda14cbcSMatt Macy /*
532eda14cbcSMatt Macy  * Disables NFS sharing for the specified share.
533eda14cbcSMatt Macy  */
534eda14cbcSMatt Macy static int
53516038816SMartin Matuska nfs_disable_share_impl(sa_share_impl_t impl_share, char *filename)
53616038816SMartin Matuska {
53716038816SMartin Matuska 	return (SA_OK);
53816038816SMartin Matuska }
53916038816SMartin Matuska 
54016038816SMartin Matuska static int
541eda14cbcSMatt Macy nfs_disable_share(sa_share_impl_t impl_share)
542eda14cbcSMatt Macy {
54316038816SMartin Matuska 	return (nfs_toggle_share(
54416038816SMartin Matuska 	    ZFS_EXPORTS_LOCK, ZFS_EXPORTS_FILE, ZFS_EXPORTS_DIR, impl_share,
54516038816SMartin Matuska 	    nfs_disable_share_impl));
546eda14cbcSMatt Macy }
547eda14cbcSMatt Macy 
548eda14cbcSMatt Macy static boolean_t
549eda14cbcSMatt Macy nfs_is_shared(sa_share_impl_t impl_share)
550eda14cbcSMatt Macy {
551eda14cbcSMatt Macy 	size_t buflen = 0;
552eda14cbcSMatt Macy 	char *buf = NULL;
553eda14cbcSMatt Macy 
55416038816SMartin Matuska 	FILE *fp = fopen(ZFS_EXPORTS_FILE, "re");
555eda14cbcSMatt Macy 	if (fp == NULL) {
556eda14cbcSMatt Macy 		return (B_FALSE);
557eda14cbcSMatt Macy 	}
558eda14cbcSMatt Macy 	while ((getline(&buf, &buflen, fp)) != -1) {
559eda14cbcSMatt Macy 		char *space = NULL;
560eda14cbcSMatt Macy 
561eda14cbcSMatt Macy 		if ((space = strchr(buf, ' ')) != NULL) {
562eda14cbcSMatt Macy 			int mountpoint_len = strlen(impl_share->sa_mountpoint);
563eda14cbcSMatt Macy 
564eda14cbcSMatt Macy 			if (space - buf == mountpoint_len &&
565eda14cbcSMatt Macy 			    strncmp(impl_share->sa_mountpoint, buf,
566eda14cbcSMatt Macy 			    mountpoint_len) == 0) {
567eda14cbcSMatt Macy 				fclose(fp);
568eda14cbcSMatt Macy 				free(buf);
569eda14cbcSMatt Macy 				return (B_TRUE);
570eda14cbcSMatt Macy 			}
571eda14cbcSMatt Macy 		}
572eda14cbcSMatt Macy 	}
573eda14cbcSMatt Macy 	free(buf);
574eda14cbcSMatt Macy 	fclose(fp);
575eda14cbcSMatt Macy 	return (B_FALSE);
576eda14cbcSMatt Macy }
577eda14cbcSMatt Macy 
578eda14cbcSMatt Macy /*
579eda14cbcSMatt Macy  * Checks whether the specified NFS share options are syntactically correct.
580eda14cbcSMatt Macy  */
581eda14cbcSMatt Macy static int
582eda14cbcSMatt Macy nfs_validate_shareopts(const char *shareopts)
583eda14cbcSMatt Macy {
584eda14cbcSMatt Macy 	char *linux_opts;
585eda14cbcSMatt Macy 	int error;
586eda14cbcSMatt Macy 
587eda14cbcSMatt Macy 	error = get_linux_shareopts(shareopts, &linux_opts);
588eda14cbcSMatt Macy 
589eda14cbcSMatt Macy 	if (error != SA_OK)
590eda14cbcSMatt Macy 		return (error);
591eda14cbcSMatt Macy 
592eda14cbcSMatt Macy 	free(linux_opts);
593eda14cbcSMatt Macy 	return (SA_OK);
594eda14cbcSMatt Macy }
595eda14cbcSMatt Macy 
596eda14cbcSMatt Macy static int
597eda14cbcSMatt Macy nfs_update_shareopts(sa_share_impl_t impl_share, const char *shareopts)
598eda14cbcSMatt Macy {
599eda14cbcSMatt Macy 	FSINFO(impl_share, nfs_fstype)->shareopts = (char *)shareopts;
600eda14cbcSMatt Macy 	return (SA_OK);
601eda14cbcSMatt Macy }
602eda14cbcSMatt Macy 
603eda14cbcSMatt Macy /*
604eda14cbcSMatt Macy  * Clears a share's NFS options. Used by libshare to
605eda14cbcSMatt Macy  * clean up shares that are about to be free()'d.
606eda14cbcSMatt Macy  */
607eda14cbcSMatt Macy static void
608eda14cbcSMatt Macy nfs_clear_shareopts(sa_share_impl_t impl_share)
609eda14cbcSMatt Macy {
610eda14cbcSMatt Macy 	FSINFO(impl_share, nfs_fstype)->shareopts = NULL;
611eda14cbcSMatt Macy }
612eda14cbcSMatt Macy 
613eda14cbcSMatt Macy static int
614eda14cbcSMatt Macy nfs_commit_shares(void)
615eda14cbcSMatt Macy {
616eda14cbcSMatt Macy 	char *argv[] = {
617eda14cbcSMatt Macy 	    "/usr/sbin/exportfs",
618eda14cbcSMatt Macy 	    "-ra",
619eda14cbcSMatt Macy 	    NULL
620eda14cbcSMatt Macy 	};
621eda14cbcSMatt Macy 
622eda14cbcSMatt Macy 	return (libzfs_run_process(argv[0], argv, 0));
623eda14cbcSMatt Macy }
624eda14cbcSMatt Macy 
625eda14cbcSMatt Macy static const sa_share_ops_t nfs_shareops = {
626eda14cbcSMatt Macy 	.enable_share = nfs_enable_share,
627eda14cbcSMatt Macy 	.disable_share = nfs_disable_share,
628eda14cbcSMatt Macy 	.is_shared = nfs_is_shared,
629eda14cbcSMatt Macy 
630eda14cbcSMatt Macy 	.validate_shareopts = nfs_validate_shareopts,
631eda14cbcSMatt Macy 	.update_shareopts = nfs_update_shareopts,
632eda14cbcSMatt Macy 	.clear_shareopts = nfs_clear_shareopts,
633eda14cbcSMatt Macy 	.commit_shares = nfs_commit_shares,
634eda14cbcSMatt Macy };
635eda14cbcSMatt Macy 
636eda14cbcSMatt Macy /*
637eda14cbcSMatt Macy  * Initializes the NFS functionality of libshare.
638eda14cbcSMatt Macy  */
639eda14cbcSMatt Macy void
640eda14cbcSMatt Macy libshare_nfs_init(void)
641eda14cbcSMatt Macy {
642eda14cbcSMatt Macy 	nfs_fstype = register_fstype("nfs", &nfs_shareops);
643eda14cbcSMatt Macy }
644