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; 139eda14cbcSMatt Macy char *host_dup, *host, *next; 140eda14cbcSMatt Macy nfs_host_cookie_t *udata = (nfs_host_cookie_t *)pcookie; 141eda14cbcSMatt Macy 142eda14cbcSMatt Macy #ifdef DEBUG 143eda14cbcSMatt Macy fprintf(stderr, "foreach_nfs_host_cb: key=%s, value=%s\n", opt, value); 144eda14cbcSMatt Macy #endif 145eda14cbcSMatt Macy 146eda14cbcSMatt Macy if (strcmp(opt, "sec") == 0) 147eda14cbcSMatt Macy udata->security = value; 148eda14cbcSMatt Macy 149eda14cbcSMatt Macy if (strcmp(opt, "rw") == 0 || strcmp(opt, "ro") == 0) { 150eda14cbcSMatt Macy if (value == NULL) 151eda14cbcSMatt Macy value = "*"; 152eda14cbcSMatt Macy 153eda14cbcSMatt Macy access = opt; 154eda14cbcSMatt Macy 155eda14cbcSMatt Macy host_dup = strdup(value); 156eda14cbcSMatt Macy 157eda14cbcSMatt Macy if (host_dup == NULL) 158eda14cbcSMatt Macy return (SA_NO_MEMORY); 159eda14cbcSMatt Macy 160eda14cbcSMatt Macy host = host_dup; 161eda14cbcSMatt Macy 162eda14cbcSMatt Macy do { 163eda14cbcSMatt Macy next = strchr(host, ':'); 164eda14cbcSMatt Macy if (next != NULL) { 165eda14cbcSMatt Macy *next = '\0'; 166eda14cbcSMatt Macy next++; 167eda14cbcSMatt Macy } 168eda14cbcSMatt Macy 169eda14cbcSMatt Macy error = udata->callback(udata->filename, 170eda14cbcSMatt Macy udata->sharepath, host, udata->security, 171eda14cbcSMatt Macy access, udata->cookie); 172eda14cbcSMatt Macy 173eda14cbcSMatt Macy if (error != SA_OK) { 174eda14cbcSMatt Macy free(host_dup); 175eda14cbcSMatt Macy 176eda14cbcSMatt Macy return (error); 177eda14cbcSMatt Macy } 178eda14cbcSMatt Macy 179eda14cbcSMatt Macy host = next; 180eda14cbcSMatt Macy } while (host != NULL); 181eda14cbcSMatt Macy 182eda14cbcSMatt Macy free(host_dup); 183eda14cbcSMatt Macy } 184eda14cbcSMatt Macy 185eda14cbcSMatt Macy return (SA_OK); 186eda14cbcSMatt Macy } 187eda14cbcSMatt Macy 188eda14cbcSMatt Macy /* 189eda14cbcSMatt Macy * Invokes a callback function for all NFS hosts that are set for a share. 190eda14cbcSMatt Macy */ 191eda14cbcSMatt Macy static int 192eda14cbcSMatt Macy foreach_nfs_host(sa_share_impl_t impl_share, char *filename, 193eda14cbcSMatt Macy nfs_host_callback_t callback, void *cookie) 194eda14cbcSMatt Macy { 195eda14cbcSMatt Macy nfs_host_cookie_t udata; 196eda14cbcSMatt Macy char *shareopts; 197eda14cbcSMatt Macy 198eda14cbcSMatt Macy udata.callback = callback; 199eda14cbcSMatt Macy udata.sharepath = impl_share->sa_mountpoint; 200eda14cbcSMatt Macy udata.cookie = cookie; 201eda14cbcSMatt Macy udata.filename = filename; 202eda14cbcSMatt Macy udata.security = "sys"; 203eda14cbcSMatt Macy 204eda14cbcSMatt Macy shareopts = FSINFO(impl_share, nfs_fstype)->shareopts; 205eda14cbcSMatt Macy 206eda14cbcSMatt Macy return (foreach_nfs_shareopt(shareopts, foreach_nfs_host_cb, 207eda14cbcSMatt Macy &udata)); 208eda14cbcSMatt Macy } 209eda14cbcSMatt Macy 210eda14cbcSMatt Macy /* 211eda14cbcSMatt Macy * Converts a Solaris NFS host specification to its Linux equivalent. 212eda14cbcSMatt Macy */ 213eda14cbcSMatt Macy static int 214eda14cbcSMatt Macy get_linux_hostspec(const char *solaris_hostspec, char **plinux_hostspec) 215eda14cbcSMatt Macy { 216eda14cbcSMatt Macy /* 217eda14cbcSMatt Macy * For now we just support CIDR masks (e.g. @192.168.0.0/16) and host 218eda14cbcSMatt Macy * wildcards (e.g. *.example.org). 219eda14cbcSMatt Macy */ 220eda14cbcSMatt Macy if (solaris_hostspec[0] == '@') { 221eda14cbcSMatt Macy /* 222eda14cbcSMatt Macy * Solaris host specifier, e.g. @192.168.0.0/16; we just need 223eda14cbcSMatt Macy * to skip the @ in this case 224eda14cbcSMatt Macy */ 225eda14cbcSMatt Macy *plinux_hostspec = strdup(solaris_hostspec + 1); 226eda14cbcSMatt Macy } else { 227eda14cbcSMatt Macy *plinux_hostspec = strdup(solaris_hostspec); 228eda14cbcSMatt Macy } 229eda14cbcSMatt Macy 230eda14cbcSMatt Macy if (*plinux_hostspec == NULL) { 231eda14cbcSMatt Macy return (SA_NO_MEMORY); 232eda14cbcSMatt Macy } 233eda14cbcSMatt Macy 234eda14cbcSMatt Macy return (SA_OK); 235eda14cbcSMatt Macy } 236eda14cbcSMatt Macy 237eda14cbcSMatt Macy /* 238eda14cbcSMatt Macy * Adds a Linux share option to an array of NFS options. 239eda14cbcSMatt Macy */ 240eda14cbcSMatt Macy static int 241eda14cbcSMatt Macy add_linux_shareopt(char **plinux_opts, const char *key, const char *value) 242eda14cbcSMatt Macy { 243eda14cbcSMatt Macy size_t len = 0; 244eda14cbcSMatt Macy char *new_linux_opts; 245eda14cbcSMatt Macy 246eda14cbcSMatt Macy if (*plinux_opts != NULL) 247eda14cbcSMatt Macy len = strlen(*plinux_opts); 248eda14cbcSMatt Macy 249eda14cbcSMatt Macy new_linux_opts = realloc(*plinux_opts, len + 1 + strlen(key) + 250eda14cbcSMatt Macy (value ? 1 + strlen(value) : 0) + 1); 251eda14cbcSMatt Macy 252eda14cbcSMatt Macy if (new_linux_opts == NULL) 253eda14cbcSMatt Macy return (SA_NO_MEMORY); 254eda14cbcSMatt Macy 255eda14cbcSMatt Macy new_linux_opts[len] = '\0'; 256eda14cbcSMatt Macy 257eda14cbcSMatt Macy if (len > 0) 258eda14cbcSMatt Macy strcat(new_linux_opts, ","); 259eda14cbcSMatt Macy 260eda14cbcSMatt Macy strcat(new_linux_opts, key); 261eda14cbcSMatt Macy 262eda14cbcSMatt Macy if (value != NULL) { 263eda14cbcSMatt Macy strcat(new_linux_opts, "="); 264eda14cbcSMatt Macy strcat(new_linux_opts, value); 265eda14cbcSMatt Macy } 266eda14cbcSMatt Macy 267eda14cbcSMatt Macy *plinux_opts = new_linux_opts; 268eda14cbcSMatt Macy 269eda14cbcSMatt Macy return (SA_OK); 270eda14cbcSMatt Macy } 271eda14cbcSMatt Macy 272eda14cbcSMatt Macy /* 273eda14cbcSMatt Macy * Validates and converts a single Solaris share option to its Linux 274eda14cbcSMatt Macy * equivalent. 275eda14cbcSMatt Macy */ 276eda14cbcSMatt Macy static int 277eda14cbcSMatt Macy get_linux_shareopts_cb(const char *key, const char *value, void *cookie) 278eda14cbcSMatt Macy { 279eda14cbcSMatt Macy char **plinux_opts = (char **)cookie; 280eda14cbcSMatt Macy 281eda14cbcSMatt Macy /* host-specific options, these are taken care of elsewhere */ 282eda14cbcSMatt Macy if (strcmp(key, "ro") == 0 || strcmp(key, "rw") == 0 || 283eda14cbcSMatt Macy strcmp(key, "sec") == 0) 284eda14cbcSMatt Macy return (SA_OK); 285eda14cbcSMatt Macy 286eda14cbcSMatt Macy if (strcmp(key, "anon") == 0) 287eda14cbcSMatt Macy key = "anonuid"; 288eda14cbcSMatt Macy 289eda14cbcSMatt Macy if (strcmp(key, "root_mapping") == 0) { 290eda14cbcSMatt Macy (void) add_linux_shareopt(plinux_opts, "root_squash", NULL); 291eda14cbcSMatt Macy key = "anonuid"; 292eda14cbcSMatt Macy } 293eda14cbcSMatt Macy 294eda14cbcSMatt Macy if (strcmp(key, "nosub") == 0) 295eda14cbcSMatt Macy key = "subtree_check"; 296eda14cbcSMatt Macy 297eda14cbcSMatt Macy if (strcmp(key, "insecure") != 0 && strcmp(key, "secure") != 0 && 298eda14cbcSMatt Macy strcmp(key, "async") != 0 && strcmp(key, "sync") != 0 && 299eda14cbcSMatt Macy strcmp(key, "no_wdelay") != 0 && strcmp(key, "wdelay") != 0 && 300eda14cbcSMatt Macy strcmp(key, "nohide") != 0 && strcmp(key, "hide") != 0 && 301eda14cbcSMatt Macy strcmp(key, "crossmnt") != 0 && 302eda14cbcSMatt Macy strcmp(key, "no_subtree_check") != 0 && 303eda14cbcSMatt Macy strcmp(key, "subtree_check") != 0 && 304eda14cbcSMatt Macy strcmp(key, "insecure_locks") != 0 && 305eda14cbcSMatt Macy strcmp(key, "secure_locks") != 0 && 306eda14cbcSMatt Macy strcmp(key, "no_auth_nlm") != 0 && strcmp(key, "auth_nlm") != 0 && 307eda14cbcSMatt Macy strcmp(key, "no_acl") != 0 && strcmp(key, "mountpoint") != 0 && 308eda14cbcSMatt Macy strcmp(key, "mp") != 0 && strcmp(key, "fsuid") != 0 && 309eda14cbcSMatt Macy strcmp(key, "refer") != 0 && strcmp(key, "replicas") != 0 && 310eda14cbcSMatt Macy strcmp(key, "root_squash") != 0 && 311eda14cbcSMatt Macy strcmp(key, "no_root_squash") != 0 && 312eda14cbcSMatt Macy strcmp(key, "all_squash") != 0 && 313eda14cbcSMatt Macy strcmp(key, "no_all_squash") != 0 && strcmp(key, "fsid") != 0 && 314eda14cbcSMatt Macy strcmp(key, "anonuid") != 0 && strcmp(key, "anongid") != 0) { 315eda14cbcSMatt Macy return (SA_SYNTAX_ERR); 316eda14cbcSMatt Macy } 317eda14cbcSMatt Macy 318eda14cbcSMatt Macy (void) add_linux_shareopt(plinux_opts, key, value); 319eda14cbcSMatt Macy 320eda14cbcSMatt Macy return (SA_OK); 321eda14cbcSMatt Macy } 322eda14cbcSMatt Macy 323eda14cbcSMatt Macy /* 324eda14cbcSMatt Macy * Takes a string containing Solaris share options (e.g. "sync,no_acl") and 325eda14cbcSMatt Macy * converts them to a NULL-terminated array of Linux NFS options. 326eda14cbcSMatt Macy */ 327eda14cbcSMatt Macy static int 328eda14cbcSMatt Macy get_linux_shareopts(const char *shareopts, char **plinux_opts) 329eda14cbcSMatt Macy { 330eda14cbcSMatt Macy int error; 331eda14cbcSMatt Macy 332eda14cbcSMatt Macy assert(plinux_opts != NULL); 333eda14cbcSMatt Macy 334eda14cbcSMatt Macy *plinux_opts = NULL; 335eda14cbcSMatt Macy 336eda14cbcSMatt Macy /* no_subtree_check - Default as of nfs-utils v1.1.0 */ 337eda14cbcSMatt Macy (void) add_linux_shareopt(plinux_opts, "no_subtree_check", NULL); 338eda14cbcSMatt Macy 339eda14cbcSMatt Macy /* mountpoint - Restrict exports to ZFS mountpoints */ 340eda14cbcSMatt Macy (void) add_linux_shareopt(plinux_opts, "mountpoint", NULL); 341eda14cbcSMatt Macy 342eda14cbcSMatt Macy error = foreach_nfs_shareopt(shareopts, get_linux_shareopts_cb, 343eda14cbcSMatt Macy plinux_opts); 344eda14cbcSMatt Macy 345eda14cbcSMatt Macy if (error != SA_OK) { 346eda14cbcSMatt Macy free(*plinux_opts); 347eda14cbcSMatt Macy *plinux_opts = NULL; 348eda14cbcSMatt Macy } 349eda14cbcSMatt Macy 350eda14cbcSMatt Macy return (error); 351eda14cbcSMatt Macy } 352eda14cbcSMatt Macy 353eda14cbcSMatt Macy /* 354eda14cbcSMatt Macy * This function populates an entry into /etc/exports.d/zfs.exports. 355eda14cbcSMatt Macy * This file is consumed by the linux nfs server so that zfs shares are 356eda14cbcSMatt Macy * automatically exported upon boot or whenever the nfs server restarts. 357eda14cbcSMatt Macy */ 358eda14cbcSMatt Macy static int 359eda14cbcSMatt Macy nfs_add_entry(const char *filename, const char *sharepath, 360eda14cbcSMatt Macy const char *host, const char *security, const char *access_opts, 361eda14cbcSMatt Macy void *pcookie) 362eda14cbcSMatt Macy { 363eda14cbcSMatt Macy int error; 364eda14cbcSMatt Macy char *linuxhost; 365eda14cbcSMatt Macy const char *linux_opts = (const char *)pcookie; 366eda14cbcSMatt Macy 367eda14cbcSMatt Macy error = get_linux_hostspec(host, &linuxhost); 368eda14cbcSMatt Macy if (error != SA_OK) 369eda14cbcSMatt Macy return (error); 370eda14cbcSMatt Macy 371eda14cbcSMatt Macy if (linux_opts == NULL) 372eda14cbcSMatt Macy linux_opts = ""; 373eda14cbcSMatt Macy 37416038816SMartin Matuska FILE *fp = fopen(filename, "a+e"); 375eda14cbcSMatt Macy if (fp == NULL) { 376eda14cbcSMatt Macy fprintf(stderr, "failed to open %s file: %s", filename, 377eda14cbcSMatt Macy strerror(errno)); 378eda14cbcSMatt Macy free(linuxhost); 379eda14cbcSMatt Macy return (SA_SYSTEM_ERR); 380eda14cbcSMatt Macy } 381eda14cbcSMatt Macy 382eda14cbcSMatt Macy if (fprintf(fp, "%s %s(sec=%s,%s,%s)\n", sharepath, linuxhost, 383eda14cbcSMatt Macy security, access_opts, linux_opts) < 0) { 384eda14cbcSMatt Macy fprintf(stderr, "failed to write to %s\n", filename); 385eda14cbcSMatt Macy free(linuxhost); 386eda14cbcSMatt Macy fclose(fp); 387eda14cbcSMatt Macy return (SA_SYSTEM_ERR); 388eda14cbcSMatt Macy } 389eda14cbcSMatt Macy 390eda14cbcSMatt Macy free(linuxhost); 391eda14cbcSMatt Macy if (fclose(fp) != 0) { 392eda14cbcSMatt Macy fprintf(stderr, "Unable to close file %s: %s\n", 393eda14cbcSMatt Macy filename, strerror(errno)); 394eda14cbcSMatt Macy return (SA_SYSTEM_ERR); 395eda14cbcSMatt Macy } 396eda14cbcSMatt Macy return (SA_OK); 397eda14cbcSMatt Macy } 398eda14cbcSMatt Macy 399eda14cbcSMatt Macy /* 400eda14cbcSMatt Macy * This function copies all entries from the exports file to "filename", 401eda14cbcSMatt Macy * omitting any entries for the specified mountpoint. 402eda14cbcSMatt Macy */ 403*3ff01b23SMartin Matuska int 404eda14cbcSMatt Macy nfs_copy_entries(char *filename, const char *mountpoint) 405eda14cbcSMatt Macy { 406eda14cbcSMatt Macy char *buf = NULL; 407eda14cbcSMatt Macy size_t buflen = 0; 408eda14cbcSMatt Macy int error = SA_OK; 409eda14cbcSMatt Macy 41016038816SMartin Matuska FILE *oldfp = fopen(ZFS_EXPORTS_FILE, "re"); 41116038816SMartin Matuska FILE *newfp = fopen(filename, "w+e"); 412c40487d4SMatt Macy if (newfp == NULL) { 413c40487d4SMatt Macy fprintf(stderr, "failed to open %s file: %s", filename, 414c40487d4SMatt Macy strerror(errno)); 415c40487d4SMatt Macy fclose(oldfp); 416c40487d4SMatt Macy return (SA_SYSTEM_ERR); 417c40487d4SMatt Macy } 418eda14cbcSMatt Macy fputs(FILE_HEADER, newfp); 419c40487d4SMatt Macy 420c40487d4SMatt Macy /* 421c40487d4SMatt Macy * The ZFS_EXPORTS_FILE may not exist yet. If that's the 422c40487d4SMatt Macy * case then just write out the new file. 423c40487d4SMatt Macy */ 424c40487d4SMatt Macy if (oldfp != NULL) { 425c40487d4SMatt Macy while (getline(&buf, &buflen, oldfp) != -1) { 426eda14cbcSMatt Macy char *space = NULL; 427eda14cbcSMatt Macy 428eda14cbcSMatt Macy if (buf[0] == '\n' || buf[0] == '#') 429eda14cbcSMatt Macy continue; 430eda14cbcSMatt Macy 431eda14cbcSMatt Macy if ((space = strchr(buf, ' ')) != NULL) { 432eda14cbcSMatt Macy int mountpoint_len = strlen(mountpoint); 433eda14cbcSMatt Macy 434eda14cbcSMatt Macy if (space - buf == mountpoint_len && 435c40487d4SMatt Macy strncmp(mountpoint, buf, 436c40487d4SMatt Macy mountpoint_len) == 0) { 437eda14cbcSMatt Macy continue; 438eda14cbcSMatt Macy } 439eda14cbcSMatt Macy } 440eda14cbcSMatt Macy fputs(buf, newfp); 441eda14cbcSMatt Macy } 442eda14cbcSMatt Macy 443c40487d4SMatt Macy if (ferror(oldfp) != 0) { 444eda14cbcSMatt Macy error = ferror(oldfp); 445eda14cbcSMatt Macy } 446c40487d4SMatt Macy if (fclose(oldfp) != 0) { 447c40487d4SMatt Macy fprintf(stderr, "Unable to close file %s: %s\n", 448c40487d4SMatt Macy filename, strerror(errno)); 449c40487d4SMatt Macy error = error != 0 ? error : SA_SYSTEM_ERR; 450c40487d4SMatt Macy } 451c40487d4SMatt Macy } 452c40487d4SMatt Macy 453eda14cbcSMatt Macy if (error == 0 && ferror(newfp) != 0) { 454eda14cbcSMatt Macy error = ferror(newfp); 455eda14cbcSMatt Macy } 456eda14cbcSMatt Macy 457eda14cbcSMatt Macy free(buf); 458eda14cbcSMatt Macy if (fclose(newfp) != 0) { 459eda14cbcSMatt Macy fprintf(stderr, "Unable to close file %s: %s\n", 460eda14cbcSMatt Macy filename, strerror(errno)); 461eda14cbcSMatt Macy error = error != 0 ? error : SA_SYSTEM_ERR; 462eda14cbcSMatt Macy } 463eda14cbcSMatt Macy return (error); 464eda14cbcSMatt Macy } 465eda14cbcSMatt Macy 466eda14cbcSMatt Macy /* 467eda14cbcSMatt Macy * Enables NFS sharing for the specified share. 468eda14cbcSMatt Macy */ 469eda14cbcSMatt Macy static int 47016038816SMartin Matuska nfs_enable_share_impl(sa_share_impl_t impl_share, char *filename) 471eda14cbcSMatt Macy { 472eda14cbcSMatt Macy char *shareopts, *linux_opts; 473eda14cbcSMatt Macy int error; 474eda14cbcSMatt Macy 475eda14cbcSMatt Macy shareopts = FSINFO(impl_share, nfs_fstype)->shareopts; 476eda14cbcSMatt Macy error = get_linux_shareopts(shareopts, &linux_opts); 47716038816SMartin Matuska if (error != SA_OK) 478eda14cbcSMatt Macy return (error); 479eda14cbcSMatt Macy 480eda14cbcSMatt Macy error = foreach_nfs_host(impl_share, filename, nfs_add_entry, 481eda14cbcSMatt Macy linux_opts); 482eda14cbcSMatt Macy free(linux_opts); 483eda14cbcSMatt Macy return (error); 484eda14cbcSMatt Macy } 485eda14cbcSMatt Macy 48616038816SMartin Matuska static int 48716038816SMartin Matuska nfs_enable_share(sa_share_impl_t impl_share) 48816038816SMartin Matuska { 48916038816SMartin Matuska return (nfs_toggle_share( 49016038816SMartin Matuska ZFS_EXPORTS_LOCK, ZFS_EXPORTS_FILE, ZFS_EXPORTS_DIR, impl_share, 49116038816SMartin Matuska nfs_enable_share_impl)); 49216038816SMartin Matuska } 49316038816SMartin Matuska 494eda14cbcSMatt Macy /* 495eda14cbcSMatt Macy * Disables NFS sharing for the specified share. 496eda14cbcSMatt Macy */ 497eda14cbcSMatt Macy static int 49816038816SMartin Matuska nfs_disable_share_impl(sa_share_impl_t impl_share, char *filename) 49916038816SMartin Matuska { 50016038816SMartin Matuska return (SA_OK); 50116038816SMartin Matuska } 50216038816SMartin Matuska 50316038816SMartin Matuska static int 504eda14cbcSMatt Macy nfs_disable_share(sa_share_impl_t impl_share) 505eda14cbcSMatt Macy { 50616038816SMartin Matuska return (nfs_toggle_share( 50716038816SMartin Matuska ZFS_EXPORTS_LOCK, ZFS_EXPORTS_FILE, ZFS_EXPORTS_DIR, impl_share, 50816038816SMartin Matuska nfs_disable_share_impl)); 509eda14cbcSMatt Macy } 510eda14cbcSMatt Macy 511eda14cbcSMatt Macy static boolean_t 512eda14cbcSMatt Macy nfs_is_shared(sa_share_impl_t impl_share) 513eda14cbcSMatt Macy { 514eda14cbcSMatt Macy size_t buflen = 0; 515eda14cbcSMatt Macy char *buf = NULL; 516eda14cbcSMatt Macy 51716038816SMartin Matuska FILE *fp = fopen(ZFS_EXPORTS_FILE, "re"); 518eda14cbcSMatt Macy if (fp == NULL) { 519eda14cbcSMatt Macy return (B_FALSE); 520eda14cbcSMatt Macy } 521eda14cbcSMatt Macy while ((getline(&buf, &buflen, fp)) != -1) { 522eda14cbcSMatt Macy char *space = NULL; 523eda14cbcSMatt Macy 524eda14cbcSMatt Macy if ((space = strchr(buf, ' ')) != NULL) { 525eda14cbcSMatt Macy int mountpoint_len = strlen(impl_share->sa_mountpoint); 526eda14cbcSMatt Macy 527eda14cbcSMatt Macy if (space - buf == mountpoint_len && 528eda14cbcSMatt Macy strncmp(impl_share->sa_mountpoint, buf, 529eda14cbcSMatt Macy mountpoint_len) == 0) { 530eda14cbcSMatt Macy fclose(fp); 531eda14cbcSMatt Macy free(buf); 532eda14cbcSMatt Macy return (B_TRUE); 533eda14cbcSMatt Macy } 534eda14cbcSMatt Macy } 535eda14cbcSMatt Macy } 536eda14cbcSMatt Macy free(buf); 537eda14cbcSMatt Macy fclose(fp); 538eda14cbcSMatt Macy return (B_FALSE); 539eda14cbcSMatt Macy } 540eda14cbcSMatt Macy 541eda14cbcSMatt Macy /* 542eda14cbcSMatt Macy * Checks whether the specified NFS share options are syntactically correct. 543eda14cbcSMatt Macy */ 544eda14cbcSMatt Macy static int 545eda14cbcSMatt Macy nfs_validate_shareopts(const char *shareopts) 546eda14cbcSMatt Macy { 547eda14cbcSMatt Macy char *linux_opts; 548eda14cbcSMatt Macy int error; 549eda14cbcSMatt Macy 550eda14cbcSMatt Macy error = get_linux_shareopts(shareopts, &linux_opts); 551eda14cbcSMatt Macy 552eda14cbcSMatt Macy if (error != SA_OK) 553eda14cbcSMatt Macy return (error); 554eda14cbcSMatt Macy 555eda14cbcSMatt Macy free(linux_opts); 556eda14cbcSMatt Macy return (SA_OK); 557eda14cbcSMatt Macy } 558eda14cbcSMatt Macy 559eda14cbcSMatt Macy static int 560eda14cbcSMatt Macy nfs_update_shareopts(sa_share_impl_t impl_share, const char *shareopts) 561eda14cbcSMatt Macy { 562eda14cbcSMatt Macy FSINFO(impl_share, nfs_fstype)->shareopts = (char *)shareopts; 563eda14cbcSMatt Macy return (SA_OK); 564eda14cbcSMatt Macy } 565eda14cbcSMatt Macy 566eda14cbcSMatt Macy /* 567eda14cbcSMatt Macy * Clears a share's NFS options. Used by libshare to 568eda14cbcSMatt Macy * clean up shares that are about to be free()'d. 569eda14cbcSMatt Macy */ 570eda14cbcSMatt Macy static void 571eda14cbcSMatt Macy nfs_clear_shareopts(sa_share_impl_t impl_share) 572eda14cbcSMatt Macy { 573eda14cbcSMatt Macy FSINFO(impl_share, nfs_fstype)->shareopts = NULL; 574eda14cbcSMatt Macy } 575eda14cbcSMatt Macy 576eda14cbcSMatt Macy static int 577eda14cbcSMatt Macy nfs_commit_shares(void) 578eda14cbcSMatt Macy { 579eda14cbcSMatt Macy char *argv[] = { 580eda14cbcSMatt Macy "/usr/sbin/exportfs", 581eda14cbcSMatt Macy "-ra", 582eda14cbcSMatt Macy NULL 583eda14cbcSMatt Macy }; 584eda14cbcSMatt Macy 585eda14cbcSMatt Macy return (libzfs_run_process(argv[0], argv, 0)); 586eda14cbcSMatt Macy } 587eda14cbcSMatt Macy 588eda14cbcSMatt Macy static const sa_share_ops_t nfs_shareops = { 589eda14cbcSMatt Macy .enable_share = nfs_enable_share, 590eda14cbcSMatt Macy .disable_share = nfs_disable_share, 591eda14cbcSMatt Macy .is_shared = nfs_is_shared, 592eda14cbcSMatt Macy 593eda14cbcSMatt Macy .validate_shareopts = nfs_validate_shareopts, 594eda14cbcSMatt Macy .update_shareopts = nfs_update_shareopts, 595eda14cbcSMatt Macy .clear_shareopts = nfs_clear_shareopts, 596eda14cbcSMatt Macy .commit_shares = nfs_commit_shares, 597eda14cbcSMatt Macy }; 598eda14cbcSMatt Macy 599eda14cbcSMatt Macy /* 600eda14cbcSMatt Macy * Initializes the NFS functionality of libshare. 601eda14cbcSMatt Macy */ 602eda14cbcSMatt Macy void 603eda14cbcSMatt Macy libshare_nfs_init(void) 604eda14cbcSMatt Macy { 605eda14cbcSMatt Macy nfs_fstype = register_fstype("nfs", &nfs_shareops); 606eda14cbcSMatt Macy } 607