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