]>
Commit | Line | Data |
---|---|---|
46e18b3f GB |
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 | |
27ccd414 | 25 | * Copyright (c) 2012 Cyril Plisko. All rights reserved. |
46e18b3f GB |
26 | */ |
27 | ||
28 | #include <stdio.h> | |
29 | #include <strings.h> | |
30 | #include <fcntl.h> | |
31 | #include <sys/wait.h> | |
32 | #include <unistd.h> | |
33 | #include <libzfs.h> | |
34 | #include <libshare.h> | |
35 | #include "libshare_impl.h" | |
36 | ||
27ccd414 CP |
37 | static boolean_t nfs_available(void); |
38 | ||
46e18b3f | 39 | static sa_fstype_t *nfs_fstype; |
46e18b3f | 40 | |
5333eb0b J |
41 | /* |
42 | * nfs_exportfs_temp_fd refers to a temporary copy of the output | |
43 | * from exportfs -v. | |
44 | */ | |
45 | static int nfs_exportfs_temp_fd = -1; | |
46 | ||
46e18b3f GB |
47 | typedef int (*nfs_shareopt_callback_t)(const char *opt, const char *value, |
48 | void *cookie); | |
49 | ||
50 | typedef int (*nfs_host_callback_t)(const char *sharepath, const char *host, | |
51 | const char *security, const char *access, void *cookie); | |
52 | ||
d1d7e268 | 53 | /* |
590338f6 GB |
54 | * Invokes the specified callback function for each Solaris share option |
55 | * listed in the specified string. | |
56 | */ | |
46e18b3f GB |
57 | static int |
58 | foreach_nfs_shareopt(const char *shareopts, | |
59 | nfs_shareopt_callback_t callback, void *cookie) | |
60 | { | |
61 | char *shareopts_dup, *opt, *cur, *value; | |
62 | int was_nul, rc; | |
63 | ||
64 | if (shareopts == NULL) | |
d1d7e268 | 65 | return (SA_OK); |
46e18b3f GB |
66 | |
67 | shareopts_dup = strdup(shareopts); | |
68 | ||
69 | if (shareopts_dup == NULL) | |
d1d7e268 | 70 | return (SA_NO_MEMORY); |
46e18b3f GB |
71 | |
72 | opt = shareopts_dup; | |
73 | was_nul = 0; | |
74 | ||
75 | while (1) { | |
76 | cur = opt; | |
77 | ||
78 | while (*cur != ',' && *cur != '\0') | |
79 | cur++; | |
80 | ||
81 | if (*cur == '\0') | |
82 | was_nul = 1; | |
83 | ||
84 | *cur = '\0'; | |
85 | ||
86 | if (cur > opt) { | |
87 | value = strchr(opt, '='); | |
88 | ||
89 | if (value != NULL) { | |
90 | *value = '\0'; | |
91 | value++; | |
92 | } | |
93 | ||
94 | rc = callback(opt, value, cookie); | |
95 | ||
96 | if (rc != SA_OK) { | |
97 | free(shareopts_dup); | |
d1d7e268 | 98 | return (rc); |
46e18b3f GB |
99 | } |
100 | } | |
101 | ||
102 | opt = cur + 1; | |
103 | ||
104 | if (was_nul) | |
105 | break; | |
106 | } | |
107 | ||
108 | free(shareopts_dup); | |
109 | ||
d1d7e268 | 110 | return (0); |
46e18b3f GB |
111 | } |
112 | ||
113 | typedef struct nfs_host_cookie_s { | |
114 | nfs_host_callback_t callback; | |
115 | const char *sharepath; | |
116 | void *cookie; | |
117 | const char *security; | |
118 | } nfs_host_cookie_t; | |
119 | ||
d1d7e268 | 120 | /* |
590338f6 GB |
121 | * Helper function for foreach_nfs_host. This function checks whether the |
122 | * current share option is a host specification and invokes a callback | |
123 | * function with information about the host. | |
124 | */ | |
46e18b3f GB |
125 | static int |
126 | foreach_nfs_host_cb(const char *opt, const char *value, void *pcookie) | |
127 | { | |
128 | int rc; | |
129 | const char *access; | |
130 | char *host_dup, *host, *next; | |
131 | nfs_host_cookie_t *udata = (nfs_host_cookie_t *)pcookie; | |
132 | ||
133 | #ifdef DEBUG | |
134 | fprintf(stderr, "foreach_nfs_host_cb: key=%s, value=%s\n", opt, value); | |
135 | #endif | |
136 | ||
137 | if (strcmp(opt, "sec") == 0) | |
138 | udata->security = value; | |
139 | ||
140 | if (strcmp(opt, "rw") == 0 || strcmp(opt, "ro") == 0) { | |
141 | if (value == NULL) | |
142 | value = "*"; | |
143 | ||
144 | access = opt; | |
145 | ||
146 | host_dup = strdup(value); | |
147 | ||
148 | if (host_dup == NULL) | |
d1d7e268 | 149 | return (SA_NO_MEMORY); |
46e18b3f GB |
150 | |
151 | host = host_dup; | |
152 | ||
153 | do { | |
154 | next = strchr(host, ':'); | |
155 | if (next != NULL) { | |
156 | *next = '\0'; | |
157 | next++; | |
158 | } | |
159 | ||
160 | rc = udata->callback(udata->sharepath, host, | |
161 | udata->security, access, udata->cookie); | |
162 | ||
163 | if (rc != SA_OK) { | |
164 | free(host_dup); | |
165 | ||
d1d7e268 | 166 | return (rc); |
46e18b3f GB |
167 | } |
168 | ||
169 | host = next; | |
170 | } while (host != NULL); | |
171 | ||
172 | free(host_dup); | |
173 | } | |
174 | ||
d1d7e268 | 175 | return (SA_OK); |
46e18b3f GB |
176 | } |
177 | ||
d1d7e268 | 178 | /* |
590338f6 GB |
179 | * Invokes a callback function for all NFS hosts that are set for a share. |
180 | */ | |
46e18b3f GB |
181 | static int |
182 | foreach_nfs_host(sa_share_impl_t impl_share, nfs_host_callback_t callback, | |
183 | void *cookie) | |
184 | { | |
185 | nfs_host_cookie_t udata; | |
186 | char *shareopts; | |
187 | ||
188 | udata.callback = callback; | |
189 | udata.sharepath = impl_share->sharepath; | |
190 | udata.cookie = cookie; | |
191 | udata.security = "sys"; | |
192 | ||
193 | shareopts = FSINFO(impl_share, nfs_fstype)->shareopts; | |
194 | ||
195 | return foreach_nfs_shareopt(shareopts, foreach_nfs_host_cb, | |
196 | &udata); | |
197 | } | |
198 | ||
d1d7e268 | 199 | /* |
590338f6 GB |
200 | * Converts a Solaris NFS host specification to its Linux equivalent. |
201 | */ | |
46e18b3f GB |
202 | static int |
203 | get_linux_hostspec(const char *solaris_hostspec, char **plinux_hostspec) | |
204 | { | |
205 | /* | |
206 | * For now we just support CIDR masks (e.g. @192.168.0.0/16) and host | |
207 | * wildcards (e.g. *.example.org). | |
208 | */ | |
209 | if (solaris_hostspec[0] == '@') { | |
210 | /* | |
211 | * Solaris host specifier, e.g. @192.168.0.0/16; we just need | |
212 | * to skip the @ in this case | |
213 | */ | |
214 | *plinux_hostspec = strdup(solaris_hostspec + 1); | |
215 | } else { | |
216 | *plinux_hostspec = strdup(solaris_hostspec); | |
217 | } | |
218 | ||
219 | if (*plinux_hostspec == NULL) { | |
d1d7e268 | 220 | return (SA_NO_MEMORY); |
46e18b3f GB |
221 | } |
222 | ||
d1d7e268 | 223 | return (SA_OK); |
46e18b3f GB |
224 | } |
225 | ||
d1d7e268 | 226 | /* |
590338f6 GB |
227 | * Used internally by nfs_enable_share to enable sharing for a single host. |
228 | */ | |
46e18b3f GB |
229 | static int |
230 | nfs_enable_share_one(const char *sharepath, const char *host, | |
231 | const char *security, const char *access, void *pcookie) | |
232 | { | |
ddd0fd9e | 233 | int rc; |
46e18b3f GB |
234 | char *linuxhost, *hostpath, *opts; |
235 | const char *linux_opts = (const char *)pcookie; | |
ddd0fd9e | 236 | char *argv[6]; |
46e18b3f GB |
237 | |
238 | /* exportfs -i -o sec=XX,rX,<opts> <host>:<sharepath> */ | |
239 | ||
240 | rc = get_linux_hostspec(host, &linuxhost); | |
241 | ||
242 | if (rc < 0) | |
243 | exit(1); | |
244 | ||
245 | hostpath = malloc(strlen(linuxhost) + 1 + strlen(sharepath) + 1); | |
246 | ||
247 | if (hostpath == NULL) { | |
248 | free(linuxhost); | |
249 | ||
250 | exit(1); | |
251 | } | |
252 | ||
253 | sprintf(hostpath, "%s:%s", linuxhost, sharepath); | |
254 | ||
255 | free(linuxhost); | |
256 | ||
257 | if (linux_opts == NULL) | |
258 | linux_opts = ""; | |
259 | ||
260 | opts = malloc(4 + strlen(security) + 4 + strlen(linux_opts) + 1); | |
261 | ||
262 | if (opts == NULL) | |
263 | exit(1); | |
264 | ||
265 | sprintf(opts, "sec=%s,%s,%s", security, access, linux_opts); | |
266 | ||
267 | #ifdef DEBUG | |
268 | fprintf(stderr, "sharing %s with opts %s\n", hostpath, opts); | |
269 | #endif | |
270 | ||
ddd0fd9e GB |
271 | argv[0] = "/usr/sbin/exportfs"; |
272 | argv[1] = "-i"; | |
273 | argv[2] = "-o"; | |
274 | argv[3] = opts; | |
275 | argv[4] = hostpath; | |
276 | argv[5] = NULL; | |
46e18b3f | 277 | |
ddd0fd9e | 278 | rc = libzfs_run_process(argv[0], argv, 0); |
46e18b3f | 279 | |
ddd0fd9e GB |
280 | free(hostpath); |
281 | free(opts); | |
282 | ||
283 | if (rc < 0) | |
d1d7e268 | 284 | return (SA_SYSTEM_ERR); |
ddd0fd9e | 285 | else |
d1d7e268 | 286 | return (SA_OK); |
46e18b3f GB |
287 | } |
288 | ||
d1d7e268 | 289 | /* |
590338f6 GB |
290 | * Adds a Linux share option to an array of NFS options. |
291 | */ | |
46e18b3f GB |
292 | static int |
293 | add_linux_shareopt(char **plinux_opts, const char *key, const char *value) | |
294 | { | |
295 | size_t len = 0; | |
296 | char *new_linux_opts; | |
297 | ||
298 | if (*plinux_opts != NULL) | |
299 | len = strlen(*plinux_opts); | |
300 | ||
301 | new_linux_opts = realloc(*plinux_opts, len + 1 + strlen(key) + | |
302 | (value ? 1 + strlen(value) : 0) + 1); | |
303 | ||
304 | if (new_linux_opts == NULL) | |
d1d7e268 | 305 | return (SA_NO_MEMORY); |
46e18b3f GB |
306 | |
307 | new_linux_opts[len] = '\0'; | |
308 | ||
309 | if (len > 0) | |
310 | strcat(new_linux_opts, ","); | |
311 | ||
312 | strcat(new_linux_opts, key); | |
313 | ||
314 | if (value != NULL) { | |
315 | strcat(new_linux_opts, "="); | |
316 | strcat(new_linux_opts, value); | |
317 | } | |
318 | ||
319 | *plinux_opts = new_linux_opts; | |
320 | ||
d1d7e268 | 321 | return (SA_OK); |
46e18b3f GB |
322 | } |
323 | ||
d1d7e268 | 324 | /* |
590338f6 GB |
325 | * Validates and converts a single Solaris share option to its Linux |
326 | * equivalent. | |
327 | */ | |
46e18b3f GB |
328 | static int |
329 | get_linux_shareopts_cb(const char *key, const char *value, void *cookie) | |
330 | { | |
331 | char **plinux_opts = (char **)cookie; | |
332 | ||
333 | /* host-specific options, these are taken care of elsewhere */ | |
334 | if (strcmp(key, "ro") == 0 || strcmp(key, "rw") == 0 || | |
335 | strcmp(key, "sec") == 0) | |
d1d7e268 | 336 | return (SA_OK); |
46e18b3f GB |
337 | |
338 | if (strcmp(key, "anon") == 0) | |
339 | key = "anonuid"; | |
340 | ||
d1d7e268 MK |
341 | if (strcmp(key, "root_mapping") == 0) { |
342 | (void) add_linux_shareopt(plinux_opts, "root_squash", NULL); | |
343 | key = "anonuid"; | |
344 | } | |
46e18b3f GB |
345 | |
346 | if (strcmp(key, "nosub") == 0) | |
347 | key = "subtree_check"; | |
348 | ||
349 | if (strcmp(key, "insecure") != 0 && strcmp(key, "secure") != 0 && | |
350 | strcmp(key, "async") != 0 && strcmp(key, "sync") != 0 && | |
351 | strcmp(key, "no_wdelay") != 0 && strcmp(key, "wdelay") != 0 && | |
352 | strcmp(key, "nohide") != 0 && strcmp(key, "hide") != 0 && | |
353 | strcmp(key, "crossmnt") != 0 && | |
354 | strcmp(key, "no_subtree_check") != 0 && | |
355 | strcmp(key, "subtree_check") != 0 && | |
356 | strcmp(key, "insecure_locks") != 0 && | |
357 | strcmp(key, "secure_locks") != 0 && | |
358 | strcmp(key, "no_auth_nlm") != 0 && strcmp(key, "auth_nlm") != 0 && | |
359 | strcmp(key, "no_acl") != 0 && strcmp(key, "mountpoint") != 0 && | |
360 | strcmp(key, "mp") != 0 && strcmp(key, "fsuid") != 0 && | |
361 | strcmp(key, "refer") != 0 && strcmp(key, "replicas") != 0 && | |
362 | strcmp(key, "root_squash") != 0 && | |
363 | strcmp(key, "no_root_squash") != 0 && | |
364 | strcmp(key, "all_squash") != 0 && | |
d2e032ca | 365 | strcmp(key, "no_all_squash") != 0 && strcmp(key, "fsid") != 0 && |
46e18b3f | 366 | strcmp(key, "anonuid") != 0 && strcmp(key, "anongid") != 0) { |
d1d7e268 | 367 | return (SA_SYNTAX_ERR); |
46e18b3f GB |
368 | } |
369 | ||
370 | (void) add_linux_shareopt(plinux_opts, key, value); | |
371 | ||
d1d7e268 | 372 | return (SA_OK); |
46e18b3f GB |
373 | } |
374 | ||
d1d7e268 | 375 | /* |
590338f6 GB |
376 | * Takes a string containing Solaris share options (e.g. "sync,no_acl") and |
377 | * converts them to a NULL-terminated array of Linux NFS options. | |
378 | */ | |
46e18b3f GB |
379 | static int |
380 | get_linux_shareopts(const char *shareopts, char **plinux_opts) | |
381 | { | |
382 | int rc; | |
383 | ||
384 | assert(plinux_opts != NULL); | |
385 | ||
386 | *plinux_opts = NULL; | |
387 | ||
388 | /* default options for Solaris shares */ | |
389 | (void) add_linux_shareopt(plinux_opts, "no_subtree_check", NULL); | |
390 | (void) add_linux_shareopt(plinux_opts, "no_root_squash", NULL); | |
391 | (void) add_linux_shareopt(plinux_opts, "mountpoint", NULL); | |
392 | ||
d1d7e268 MK |
393 | rc = foreach_nfs_shareopt(shareopts, get_linux_shareopts_cb, |
394 | plinux_opts); | |
46e18b3f GB |
395 | |
396 | if (rc != SA_OK) { | |
397 | free(*plinux_opts); | |
398 | *plinux_opts = NULL; | |
399 | } | |
400 | ||
d1d7e268 | 401 | return (rc); |
46e18b3f GB |
402 | } |
403 | ||
d1d7e268 | 404 | /* |
590338f6 GB |
405 | * Enables NFS sharing for the specified share. |
406 | */ | |
46e18b3f GB |
407 | static int |
408 | nfs_enable_share(sa_share_impl_t impl_share) | |
409 | { | |
410 | char *shareopts, *linux_opts; | |
411 | int rc; | |
412 | ||
27ccd414 | 413 | if (!nfs_available()) { |
d1d7e268 | 414 | return (SA_SYSTEM_ERR); |
46e18b3f GB |
415 | } |
416 | ||
417 | shareopts = FSINFO(impl_share, nfs_fstype)->shareopts; | |
418 | ||
419 | if (shareopts == NULL) | |
d1d7e268 | 420 | return (SA_OK); |
46e18b3f GB |
421 | |
422 | rc = get_linux_shareopts(shareopts, &linux_opts); | |
423 | ||
424 | if (rc != SA_OK) | |
d1d7e268 | 425 | return (rc); |
46e18b3f GB |
426 | |
427 | rc = foreach_nfs_host(impl_share, nfs_enable_share_one, linux_opts); | |
428 | ||
429 | free(linux_opts); | |
430 | ||
d1d7e268 | 431 | return (rc); |
46e18b3f GB |
432 | } |
433 | ||
d1d7e268 | 434 | /* |
590338f6 GB |
435 | * Used internally by nfs_disable_share to disable sharing for a single host. |
436 | */ | |
46e18b3f GB |
437 | static int |
438 | nfs_disable_share_one(const char *sharepath, const char *host, | |
439 | const char *security, const char *access, void *cookie) | |
440 | { | |
ddd0fd9e | 441 | int rc; |
46e18b3f | 442 | char *linuxhost, *hostpath; |
ddd0fd9e | 443 | char *argv[4]; |
46e18b3f GB |
444 | |
445 | rc = get_linux_hostspec(host, &linuxhost); | |
446 | ||
447 | if (rc < 0) | |
448 | exit(1); | |
449 | ||
450 | hostpath = malloc(strlen(linuxhost) + 1 + strlen(sharepath) + 1); | |
451 | ||
452 | if (hostpath == NULL) { | |
453 | free(linuxhost); | |
454 | exit(1); | |
455 | } | |
456 | ||
457 | sprintf(hostpath, "%s:%s", linuxhost, sharepath); | |
458 | ||
459 | free(linuxhost); | |
460 | ||
461 | #ifdef DEBUG | |
462 | fprintf(stderr, "unsharing %s\n", hostpath); | |
463 | #endif | |
464 | ||
ddd0fd9e GB |
465 | argv[0] = "/usr/sbin/exportfs"; |
466 | argv[1] = "-u"; | |
467 | argv[2] = hostpath; | |
468 | argv[3] = NULL; | |
46e18b3f | 469 | |
ddd0fd9e | 470 | rc = libzfs_run_process(argv[0], argv, 0); |
46e18b3f | 471 | |
ddd0fd9e GB |
472 | free(hostpath); |
473 | ||
474 | if (rc < 0) | |
d1d7e268 | 475 | return (SA_SYSTEM_ERR); |
ddd0fd9e | 476 | else |
d1d7e268 | 477 | return (SA_OK); |
46e18b3f GB |
478 | } |
479 | ||
d1d7e268 | 480 | /* |
590338f6 GB |
481 | * Disables NFS sharing for the specified share. |
482 | */ | |
46e18b3f GB |
483 | static int |
484 | nfs_disable_share(sa_share_impl_t impl_share) | |
485 | { | |
27ccd414 | 486 | if (!nfs_available()) { |
46e18b3f GB |
487 | /* |
488 | * The share can't possibly be active, so nothing | |
489 | * needs to be done to disable it. | |
490 | */ | |
d1d7e268 | 491 | return (SA_OK); |
46e18b3f GB |
492 | } |
493 | ||
d1d7e268 | 494 | return (foreach_nfs_host(impl_share, nfs_disable_share_one, NULL)); |
46e18b3f GB |
495 | } |
496 | ||
d1d7e268 | 497 | /* |
590338f6 GB |
498 | * Checks whether the specified NFS share options are syntactically correct. |
499 | */ | |
46e18b3f GB |
500 | static int |
501 | nfs_validate_shareopts(const char *shareopts) | |
502 | { | |
503 | char *linux_opts; | |
504 | int rc; | |
505 | ||
506 | rc = get_linux_shareopts(shareopts, &linux_opts); | |
507 | ||
508 | if (rc != SA_OK) | |
d1d7e268 | 509 | return (rc); |
46e18b3f GB |
510 | |
511 | free(linux_opts); | |
512 | ||
d1d7e268 | 513 | return (SA_OK); |
46e18b3f GB |
514 | } |
515 | ||
d1d7e268 | 516 | /* |
590338f6 GB |
517 | * Checks whether a share is currently active. |
518 | */ | |
46e18b3f | 519 | static boolean_t |
645fb9cc | 520 | nfs_is_share_active(sa_share_impl_t impl_share) |
46e18b3f | 521 | { |
470f12d6 | 522 | int fd; |
46e18b3f GB |
523 | char line[512]; |
524 | char *tab, *cur; | |
5333eb0b | 525 | FILE *nfs_exportfs_temp_fp; |
46e18b3f | 526 | |
27ccd414 | 527 | if (!nfs_available()) |
d1d7e268 | 528 | return (B_FALSE); |
46e18b3f | 529 | |
470f12d6 G |
530 | if ((fd = dup(nfs_exportfs_temp_fd)) == -1) |
531 | return (B_FALSE); | |
532 | ||
533 | nfs_exportfs_temp_fp = fdopen(fd, "r"); | |
46e18b3f | 534 | |
aed0e9f3 | 535 | if (nfs_exportfs_temp_fp == NULL) |
536 | return (B_FALSE); | |
537 | ||
538 | if (fseek(nfs_exportfs_temp_fp, 0, SEEK_SET) < 0) { | |
5333eb0b | 539 | fclose(nfs_exportfs_temp_fp); |
d1d7e268 | 540 | return (B_FALSE); |
5333eb0b | 541 | } |
46e18b3f | 542 | |
d1d7e268 | 543 | while (fgets(line, sizeof (line), nfs_exportfs_temp_fp) != NULL) { |
5333eb0b J |
544 | /* |
545 | * exportfs uses separate lines for the share path | |
546 | * and the export options when the share path is longer | |
547 | * than a certain amount of characters; this ignores | |
548 | * the option lines | |
549 | */ | |
550 | if (line[0] == '\t') | |
551 | continue; | |
46e18b3f | 552 | |
5333eb0b | 553 | tab = strchr(line, '\t'); |
46e18b3f | 554 | |
5333eb0b J |
555 | if (tab != NULL) { |
556 | *tab = '\0'; | |
557 | cur = tab - 1; | |
558 | } else { | |
46e18b3f | 559 | /* |
5333eb0b J |
560 | * there's no tab character, which means the |
561 | * NFS options are on a separate line; we just | |
562 | * need to remove the new-line character | |
563 | * at the end of the line | |
46e18b3f | 564 | */ |
5333eb0b | 565 | cur = line + strlen(line) - 1; |
46e18b3f GB |
566 | } |
567 | ||
5333eb0b J |
568 | /* remove trailing spaces and new-line characters */ |
569 | while (cur >= line && (*cur == ' ' || *cur == '\n')) | |
570 | *cur-- = '\0'; | |
46e18b3f | 571 | |
5333eb0b J |
572 | if (strcmp(line, impl_share->sharepath) == 0) { |
573 | fclose(nfs_exportfs_temp_fp); | |
d1d7e268 | 574 | return (B_TRUE); |
5333eb0b | 575 | } |
46e18b3f GB |
576 | } |
577 | ||
5333eb0b | 578 | fclose(nfs_exportfs_temp_fp); |
46e18b3f | 579 | |
d1d7e268 | 580 | return (B_FALSE); |
46e18b3f GB |
581 | } |
582 | ||
d1d7e268 | 583 | /* |
590338f6 GB |
584 | * Called to update a share's options. A share's options might be out of |
585 | * date if the share was loaded from disk (i.e. /etc/dfs/sharetab) and the | |
586 | * "sharenfs" dataset property has changed in the meantime. This function | |
587 | * also takes care of re-enabling the share if necessary. | |
588 | */ | |
46e18b3f GB |
589 | static int |
590 | nfs_update_shareopts(sa_share_impl_t impl_share, const char *resource, | |
591 | const char *shareopts) | |
592 | { | |
593 | char *shareopts_dup; | |
594 | boolean_t needs_reshare = B_FALSE; | |
595 | char *old_shareopts; | |
596 | ||
645fb9cc TF |
597 | FSINFO(impl_share, nfs_fstype)->active = |
598 | nfs_is_share_active(impl_share); | |
46e18b3f GB |
599 | |
600 | old_shareopts = FSINFO(impl_share, nfs_fstype)->shareopts; | |
601 | ||
602 | if (strcmp(shareopts, "on") == 0) | |
603 | shareopts = "rw"; | |
604 | ||
605 | if (FSINFO(impl_share, nfs_fstype)->active && old_shareopts != NULL && | |
606 | strcmp(old_shareopts, shareopts) != 0) { | |
607 | needs_reshare = B_TRUE; | |
608 | nfs_disable_share(impl_share); | |
609 | } | |
610 | ||
611 | shareopts_dup = strdup(shareopts); | |
612 | ||
613 | if (shareopts_dup == NULL) | |
d1d7e268 | 614 | return (SA_NO_MEMORY); |
46e18b3f GB |
615 | |
616 | if (old_shareopts != NULL) | |
617 | free(old_shareopts); | |
618 | ||
619 | FSINFO(impl_share, nfs_fstype)->shareopts = shareopts_dup; | |
620 | ||
621 | if (needs_reshare) | |
622 | nfs_enable_share(impl_share); | |
623 | ||
d1d7e268 | 624 | return (SA_OK); |
46e18b3f GB |
625 | } |
626 | ||
d1d7e268 | 627 | /* |
590338f6 GB |
628 | * Clears a share's NFS options. Used by libshare to |
629 | * clean up shares that are about to be free()'d. | |
630 | */ | |
46e18b3f GB |
631 | static void |
632 | nfs_clear_shareopts(sa_share_impl_t impl_share) | |
633 | { | |
634 | free(FSINFO(impl_share, nfs_fstype)->shareopts); | |
635 | FSINFO(impl_share, nfs_fstype)->shareopts = NULL; | |
636 | } | |
637 | ||
638 | static const sa_share_ops_t nfs_shareops = { | |
639 | .enable_share = nfs_enable_share, | |
640 | .disable_share = nfs_disable_share, | |
641 | ||
642 | .validate_shareopts = nfs_validate_shareopts, | |
643 | .update_shareopts = nfs_update_shareopts, | |
644 | .clear_shareopts = nfs_clear_shareopts, | |
645 | }; | |
646 | ||
5333eb0b J |
647 | /* |
648 | * nfs_check_exportfs() checks that the exportfs command runs | |
649 | * and also maintains a temporary copy of the output from | |
650 | * exportfs -v. | |
651 | * To update this temporary copy simply call this function again. | |
652 | * | |
653 | * TODO : Use /var/lib/nfs/etab instead of our private copy. | |
654 | * But must implement locking to prevent concurrent access. | |
655 | * | |
656 | * TODO : The temporary file descriptor is never closed since | |
657 | * there is no libshare_nfs_fini() function. | |
658 | */ | |
46e18b3f GB |
659 | static int |
660 | nfs_check_exportfs(void) | |
661 | { | |
662 | pid_t pid; | |
5333eb0b J |
663 | int rc, status; |
664 | static char nfs_exportfs_tempfile[] = "/tmp/exportfs.XXXXXX"; | |
665 | ||
666 | /* | |
667 | * Close any existing temporary copies of output from exportfs. | |
668 | * We have already called unlink() so file will be deleted. | |
669 | */ | |
670 | if (nfs_exportfs_temp_fd >= 0) | |
671 | close(nfs_exportfs_temp_fd); | |
672 | ||
673 | nfs_exportfs_temp_fd = mkstemp(nfs_exportfs_tempfile); | |
674 | ||
675 | if (nfs_exportfs_temp_fd < 0) | |
d1d7e268 | 676 | return (SA_SYSTEM_ERR); |
5333eb0b J |
677 | |
678 | unlink(nfs_exportfs_tempfile); | |
679 | ||
af4db70f | 680 | (void) fcntl(nfs_exportfs_temp_fd, F_SETFD, FD_CLOEXEC); |
46e18b3f GB |
681 | |
682 | pid = fork(); | |
683 | ||
27ccd414 CP |
684 | if (pid < 0) { |
685 | (void) close(nfs_exportfs_temp_fd); | |
686 | nfs_exportfs_temp_fd = -1; | |
d1d7e268 | 687 | return (SA_SYSTEM_ERR); |
27ccd414 | 688 | } |
46e18b3f GB |
689 | |
690 | if (pid > 0) { | |
02730c33 BB |
691 | while ((rc = waitpid(pid, &status, 0)) <= 0 && |
692 | errno == EINTR) { } | |
46e18b3f | 693 | |
27ccd414 CP |
694 | if (rc <= 0) { |
695 | (void) close(nfs_exportfs_temp_fd); | |
696 | nfs_exportfs_temp_fd = -1; | |
d1d7e268 | 697 | return (SA_SYSTEM_ERR); |
27ccd414 | 698 | } |
46e18b3f | 699 | |
27ccd414 CP |
700 | if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) { |
701 | (void) close(nfs_exportfs_temp_fd); | |
702 | nfs_exportfs_temp_fd = -1; | |
d1d7e268 | 703 | return (SA_CONFIG_ERR); |
27ccd414 | 704 | } |
46e18b3f | 705 | |
d1d7e268 | 706 | return (SA_OK); |
46e18b3f GB |
707 | } |
708 | ||
709 | /* child */ | |
710 | ||
5333eb0b | 711 | /* exportfs -v */ |
46e18b3f | 712 | |
5333eb0b | 713 | if (dup2(nfs_exportfs_temp_fd, STDOUT_FILENO) < 0) |
46e18b3f GB |
714 | exit(1); |
715 | ||
5333eb0b | 716 | rc = execlp("/usr/sbin/exportfs", "exportfs", "-v", NULL); |
46e18b3f GB |
717 | |
718 | if (rc < 0) { | |
719 | exit(1); | |
720 | } | |
721 | ||
722 | exit(0); | |
723 | } | |
724 | ||
27ccd414 | 725 | /* |
4e33ba4c | 726 | * Provides a convenient wrapper for determining nfs availability |
27ccd414 CP |
727 | */ |
728 | static boolean_t | |
729 | nfs_available(void) | |
730 | { | |
731 | if (nfs_exportfs_temp_fd == -1) | |
732 | (void) nfs_check_exportfs(); | |
733 | ||
d1d7e268 | 734 | return ((nfs_exportfs_temp_fd != -1) ? B_TRUE : B_FALSE); |
27ccd414 CP |
735 | } |
736 | ||
d1d7e268 | 737 | /* |
590338f6 GB |
738 | * Initializes the NFS functionality of libshare. |
739 | */ | |
46e18b3f GB |
740 | void |
741 | libshare_nfs_init(void) | |
742 | { | |
46e18b3f GB |
743 | nfs_fstype = register_fstype("nfs", &nfs_shareops); |
744 | } |