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 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; 396*c40487d4SMatt Macy struct stat sb; 397*c40487d4SMatt Macy 398*c40487d4SMatt Macy if (stat(ZFS_EXPORTS_DIR, &sb) < 0 && 399*c40487d4SMatt Macy mkdir(ZFS_EXPORTS_DIR, 0755) < 0) { 400*c40487d4SMatt Macy fprintf(stderr, "failed to create %s: %s\n", 401*c40487d4SMatt Macy ZFS_EXPORTS_DIR, strerror(errno)); 402*c40487d4SMatt Macy return (NULL); 403*c40487d4SMatt Macy } 404eda14cbcSMatt Macy 405eda14cbcSMatt Macy if (asprintf(&tmpfile, "%s%s", ZFS_EXPORTS_FILE, ".XXXXXXXX") == -1) { 406eda14cbcSMatt Macy fprintf(stderr, "Unable to allocate temporary file\n"); 407eda14cbcSMatt Macy return (NULL); 408eda14cbcSMatt Macy } 409eda14cbcSMatt Macy 410eda14cbcSMatt Macy int fd = mkstemp(tmpfile); 411eda14cbcSMatt Macy if (fd == -1) { 412eda14cbcSMatt Macy fprintf(stderr, "Unable to create temporary file: %s", 413eda14cbcSMatt Macy strerror(errno)); 414eda14cbcSMatt Macy free(tmpfile); 415eda14cbcSMatt Macy return (NULL); 416eda14cbcSMatt Macy } 417eda14cbcSMatt Macy close(fd); 418eda14cbcSMatt Macy return (tmpfile); 419eda14cbcSMatt Macy } 420eda14cbcSMatt Macy 421eda14cbcSMatt Macy static int 422eda14cbcSMatt Macy nfs_fini_tmpfile(char *tmpfile) 423eda14cbcSMatt Macy { 424eda14cbcSMatt Macy if (rename(tmpfile, ZFS_EXPORTS_FILE) == -1) { 425eda14cbcSMatt Macy fprintf(stderr, "Unable to rename %s: %s\n", tmpfile, 426eda14cbcSMatt Macy strerror(errno)); 427eda14cbcSMatt Macy unlink(tmpfile); 428eda14cbcSMatt Macy free(tmpfile); 429eda14cbcSMatt Macy return (SA_SYSTEM_ERR); 430eda14cbcSMatt Macy } 431eda14cbcSMatt Macy free(tmpfile); 432eda14cbcSMatt Macy return (SA_OK); 433eda14cbcSMatt Macy } 434eda14cbcSMatt Macy 435eda14cbcSMatt Macy /* 436eda14cbcSMatt Macy * This function populates an entry into /etc/exports.d/zfs.exports. 437eda14cbcSMatt Macy * This file is consumed by the linux nfs server so that zfs shares are 438eda14cbcSMatt Macy * automatically exported upon boot or whenever the nfs server restarts. 439eda14cbcSMatt Macy */ 440eda14cbcSMatt Macy static int 441eda14cbcSMatt Macy nfs_add_entry(const char *filename, const char *sharepath, 442eda14cbcSMatt Macy const char *host, const char *security, const char *access_opts, 443eda14cbcSMatt Macy void *pcookie) 444eda14cbcSMatt Macy { 445eda14cbcSMatt Macy int error; 446eda14cbcSMatt Macy char *linuxhost; 447eda14cbcSMatt Macy const char *linux_opts = (const char *)pcookie; 448eda14cbcSMatt Macy 449eda14cbcSMatt Macy error = get_linux_hostspec(host, &linuxhost); 450eda14cbcSMatt Macy if (error != SA_OK) 451eda14cbcSMatt Macy return (error); 452eda14cbcSMatt Macy 453eda14cbcSMatt Macy if (linux_opts == NULL) 454eda14cbcSMatt Macy linux_opts = ""; 455eda14cbcSMatt Macy 456eda14cbcSMatt Macy FILE *fp = fopen(filename, "a+"); 457eda14cbcSMatt Macy if (fp == NULL) { 458eda14cbcSMatt Macy fprintf(stderr, "failed to open %s file: %s", filename, 459eda14cbcSMatt Macy strerror(errno)); 460eda14cbcSMatt Macy free(linuxhost); 461eda14cbcSMatt Macy return (SA_SYSTEM_ERR); 462eda14cbcSMatt Macy } 463eda14cbcSMatt Macy 464eda14cbcSMatt Macy if (fprintf(fp, "%s %s(sec=%s,%s,%s)\n", sharepath, linuxhost, 465eda14cbcSMatt Macy security, access_opts, linux_opts) < 0) { 466eda14cbcSMatt Macy fprintf(stderr, "failed to write to %s\n", filename); 467eda14cbcSMatt Macy free(linuxhost); 468eda14cbcSMatt Macy fclose(fp); 469eda14cbcSMatt Macy return (SA_SYSTEM_ERR); 470eda14cbcSMatt Macy } 471eda14cbcSMatt Macy 472eda14cbcSMatt Macy free(linuxhost); 473eda14cbcSMatt Macy if (fclose(fp) != 0) { 474eda14cbcSMatt Macy fprintf(stderr, "Unable to close file %s: %s\n", 475eda14cbcSMatt Macy filename, strerror(errno)); 476eda14cbcSMatt Macy return (SA_SYSTEM_ERR); 477eda14cbcSMatt Macy } 478eda14cbcSMatt Macy return (SA_OK); 479eda14cbcSMatt Macy } 480eda14cbcSMatt Macy 481eda14cbcSMatt Macy /* 482eda14cbcSMatt Macy * This function copies all entries from the exports file to "filename", 483eda14cbcSMatt Macy * omitting any entries for the specified mountpoint. 484eda14cbcSMatt Macy */ 485eda14cbcSMatt Macy static int 486eda14cbcSMatt Macy nfs_copy_entries(char *filename, const char *mountpoint) 487eda14cbcSMatt Macy { 488eda14cbcSMatt Macy char *buf = NULL; 489eda14cbcSMatt Macy size_t buflen = 0; 490eda14cbcSMatt Macy int error = SA_OK; 491eda14cbcSMatt Macy 492eda14cbcSMatt Macy FILE *oldfp = fopen(ZFS_EXPORTS_FILE, "r"); 493eda14cbcSMatt Macy FILE *newfp = fopen(filename, "w+"); 494*c40487d4SMatt Macy if (newfp == NULL) { 495*c40487d4SMatt Macy fprintf(stderr, "failed to open %s file: %s", filename, 496*c40487d4SMatt Macy strerror(errno)); 497*c40487d4SMatt Macy fclose(oldfp); 498*c40487d4SMatt Macy return (SA_SYSTEM_ERR); 499*c40487d4SMatt Macy } 500eda14cbcSMatt Macy fputs(FILE_HEADER, newfp); 501*c40487d4SMatt Macy 502*c40487d4SMatt Macy /* 503*c40487d4SMatt Macy * The ZFS_EXPORTS_FILE may not exist yet. If that's the 504*c40487d4SMatt Macy * case then just write out the new file. 505*c40487d4SMatt Macy */ 506*c40487d4SMatt Macy if (oldfp != NULL) { 507*c40487d4SMatt Macy while (getline(&buf, &buflen, oldfp) != -1) { 508eda14cbcSMatt Macy char *space = NULL; 509eda14cbcSMatt Macy 510eda14cbcSMatt Macy if (buf[0] == '\n' || buf[0] == '#') 511eda14cbcSMatt Macy continue; 512eda14cbcSMatt Macy 513eda14cbcSMatt Macy if ((space = strchr(buf, ' ')) != NULL) { 514eda14cbcSMatt Macy int mountpoint_len = strlen(mountpoint); 515eda14cbcSMatt Macy 516eda14cbcSMatt Macy if (space - buf == mountpoint_len && 517*c40487d4SMatt Macy strncmp(mountpoint, buf, 518*c40487d4SMatt Macy mountpoint_len) == 0) { 519eda14cbcSMatt Macy continue; 520eda14cbcSMatt Macy } 521eda14cbcSMatt Macy } 522eda14cbcSMatt Macy fputs(buf, newfp); 523eda14cbcSMatt Macy } 524eda14cbcSMatt Macy 525*c40487d4SMatt Macy if (ferror(oldfp) != 0) { 526eda14cbcSMatt Macy error = ferror(oldfp); 527eda14cbcSMatt Macy } 528*c40487d4SMatt Macy if (fclose(oldfp) != 0) { 529*c40487d4SMatt Macy fprintf(stderr, "Unable to close file %s: %s\n", 530*c40487d4SMatt Macy filename, strerror(errno)); 531*c40487d4SMatt Macy error = error != 0 ? error : SA_SYSTEM_ERR; 532*c40487d4SMatt Macy } 533*c40487d4SMatt Macy } 534*c40487d4SMatt Macy 535eda14cbcSMatt Macy if (error == 0 && ferror(newfp) != 0) { 536eda14cbcSMatt Macy error = ferror(newfp); 537eda14cbcSMatt Macy } 538eda14cbcSMatt Macy 539eda14cbcSMatt Macy free(buf); 540eda14cbcSMatt Macy if (fclose(newfp) != 0) { 541eda14cbcSMatt Macy fprintf(stderr, "Unable to close file %s: %s\n", 542eda14cbcSMatt Macy filename, strerror(errno)); 543eda14cbcSMatt Macy error = error != 0 ? error : SA_SYSTEM_ERR; 544eda14cbcSMatt Macy } 545eda14cbcSMatt Macy return (error); 546eda14cbcSMatt Macy } 547eda14cbcSMatt Macy 548eda14cbcSMatt Macy /* 549eda14cbcSMatt Macy * Enables NFS sharing for the specified share. 550eda14cbcSMatt Macy */ 551eda14cbcSMatt Macy static int 552eda14cbcSMatt Macy nfs_enable_share(sa_share_impl_t impl_share) 553eda14cbcSMatt Macy { 554eda14cbcSMatt Macy char *shareopts, *linux_opts; 555eda14cbcSMatt Macy char *filename = NULL; 556eda14cbcSMatt Macy int error; 557eda14cbcSMatt Macy 558eda14cbcSMatt Macy if ((filename = nfs_init_tmpfile()) == NULL) 559eda14cbcSMatt Macy return (SA_SYSTEM_ERR); 560eda14cbcSMatt Macy 561eda14cbcSMatt Macy error = nfs_exports_lock(); 562eda14cbcSMatt Macy if (error != 0) { 563eda14cbcSMatt Macy unlink(filename); 564eda14cbcSMatt Macy free(filename); 565eda14cbcSMatt Macy return (error); 566eda14cbcSMatt Macy } 567eda14cbcSMatt Macy 568eda14cbcSMatt Macy error = nfs_copy_entries(filename, impl_share->sa_mountpoint); 569eda14cbcSMatt Macy if (error != SA_OK) { 570eda14cbcSMatt Macy unlink(filename); 571eda14cbcSMatt Macy free(filename); 572eda14cbcSMatt Macy nfs_exports_unlock(); 573eda14cbcSMatt Macy return (error); 574eda14cbcSMatt Macy } 575eda14cbcSMatt Macy 576eda14cbcSMatt Macy shareopts = FSINFO(impl_share, nfs_fstype)->shareopts; 577eda14cbcSMatt Macy error = get_linux_shareopts(shareopts, &linux_opts); 578eda14cbcSMatt Macy if (error != SA_OK) { 579eda14cbcSMatt Macy unlink(filename); 580eda14cbcSMatt Macy free(filename); 581eda14cbcSMatt Macy nfs_exports_unlock(); 582eda14cbcSMatt Macy return (error); 583eda14cbcSMatt Macy } 584eda14cbcSMatt Macy 585eda14cbcSMatt Macy error = foreach_nfs_host(impl_share, filename, nfs_add_entry, 586eda14cbcSMatt Macy linux_opts); 587eda14cbcSMatt Macy free(linux_opts); 588eda14cbcSMatt Macy if (error == 0) { 589eda14cbcSMatt Macy error = nfs_fini_tmpfile(filename); 590eda14cbcSMatt Macy } else { 591eda14cbcSMatt Macy unlink(filename); 592eda14cbcSMatt Macy free(filename); 593eda14cbcSMatt Macy } 594eda14cbcSMatt Macy nfs_exports_unlock(); 595eda14cbcSMatt Macy return (error); 596eda14cbcSMatt Macy } 597eda14cbcSMatt Macy 598eda14cbcSMatt Macy /* 599eda14cbcSMatt Macy * Disables NFS sharing for the specified share. 600eda14cbcSMatt Macy */ 601eda14cbcSMatt Macy static int 602eda14cbcSMatt Macy nfs_disable_share(sa_share_impl_t impl_share) 603eda14cbcSMatt Macy { 604eda14cbcSMatt Macy int error; 605eda14cbcSMatt Macy char *filename = NULL; 606eda14cbcSMatt Macy 607eda14cbcSMatt Macy if ((filename = nfs_init_tmpfile()) == NULL) 608eda14cbcSMatt Macy return (SA_SYSTEM_ERR); 609eda14cbcSMatt Macy 610eda14cbcSMatt Macy error = nfs_exports_lock(); 611eda14cbcSMatt Macy if (error != 0) { 612eda14cbcSMatt Macy unlink(filename); 613eda14cbcSMatt Macy free(filename); 614eda14cbcSMatt Macy return (error); 615eda14cbcSMatt Macy } 616eda14cbcSMatt Macy 617eda14cbcSMatt Macy error = nfs_copy_entries(filename, impl_share->sa_mountpoint); 618eda14cbcSMatt Macy if (error != SA_OK) { 619eda14cbcSMatt Macy unlink(filename); 620eda14cbcSMatt Macy free(filename); 621eda14cbcSMatt Macy nfs_exports_unlock(); 622eda14cbcSMatt Macy return (error); 623eda14cbcSMatt Macy } 624eda14cbcSMatt Macy error = nfs_fini_tmpfile(filename); 625eda14cbcSMatt Macy nfs_exports_unlock(); 626eda14cbcSMatt Macy return (error); 627eda14cbcSMatt Macy } 628eda14cbcSMatt Macy 629eda14cbcSMatt Macy static boolean_t 630eda14cbcSMatt Macy nfs_is_shared(sa_share_impl_t impl_share) 631eda14cbcSMatt Macy { 632eda14cbcSMatt Macy size_t buflen = 0; 633eda14cbcSMatt Macy char *buf = NULL; 634eda14cbcSMatt Macy 635eda14cbcSMatt Macy FILE *fp = fopen(ZFS_EXPORTS_FILE, "r"); 636eda14cbcSMatt Macy if (fp == NULL) { 637eda14cbcSMatt Macy return (B_FALSE); 638eda14cbcSMatt Macy } 639eda14cbcSMatt Macy while ((getline(&buf, &buflen, fp)) != -1) { 640eda14cbcSMatt Macy char *space = NULL; 641eda14cbcSMatt Macy 642eda14cbcSMatt Macy if ((space = strchr(buf, ' ')) != NULL) { 643eda14cbcSMatt Macy int mountpoint_len = strlen(impl_share->sa_mountpoint); 644eda14cbcSMatt Macy 645eda14cbcSMatt Macy if (space - buf == mountpoint_len && 646eda14cbcSMatt Macy strncmp(impl_share->sa_mountpoint, buf, 647eda14cbcSMatt Macy mountpoint_len) == 0) { 648eda14cbcSMatt Macy fclose(fp); 649eda14cbcSMatt Macy free(buf); 650eda14cbcSMatt Macy return (B_TRUE); 651eda14cbcSMatt Macy } 652eda14cbcSMatt Macy } 653eda14cbcSMatt Macy } 654eda14cbcSMatt Macy free(buf); 655eda14cbcSMatt Macy fclose(fp); 656eda14cbcSMatt Macy return (B_FALSE); 657eda14cbcSMatt Macy } 658eda14cbcSMatt Macy 659eda14cbcSMatt Macy /* 660eda14cbcSMatt Macy * Checks whether the specified NFS share options are syntactically correct. 661eda14cbcSMatt Macy */ 662eda14cbcSMatt Macy static int 663eda14cbcSMatt Macy nfs_validate_shareopts(const char *shareopts) 664eda14cbcSMatt Macy { 665eda14cbcSMatt Macy char *linux_opts; 666eda14cbcSMatt Macy int error; 667eda14cbcSMatt Macy 668eda14cbcSMatt Macy error = get_linux_shareopts(shareopts, &linux_opts); 669eda14cbcSMatt Macy 670eda14cbcSMatt Macy if (error != SA_OK) 671eda14cbcSMatt Macy return (error); 672eda14cbcSMatt Macy 673eda14cbcSMatt Macy free(linux_opts); 674eda14cbcSMatt Macy return (SA_OK); 675eda14cbcSMatt Macy } 676eda14cbcSMatt Macy 677eda14cbcSMatt Macy static int 678eda14cbcSMatt Macy nfs_update_shareopts(sa_share_impl_t impl_share, const char *shareopts) 679eda14cbcSMatt Macy { 680eda14cbcSMatt Macy FSINFO(impl_share, nfs_fstype)->shareopts = (char *)shareopts; 681eda14cbcSMatt Macy return (SA_OK); 682eda14cbcSMatt Macy } 683eda14cbcSMatt Macy 684eda14cbcSMatt Macy /* 685eda14cbcSMatt Macy * Clears a share's NFS options. Used by libshare to 686eda14cbcSMatt Macy * clean up shares that are about to be free()'d. 687eda14cbcSMatt Macy */ 688eda14cbcSMatt Macy static void 689eda14cbcSMatt Macy nfs_clear_shareopts(sa_share_impl_t impl_share) 690eda14cbcSMatt Macy { 691eda14cbcSMatt Macy FSINFO(impl_share, nfs_fstype)->shareopts = NULL; 692eda14cbcSMatt Macy } 693eda14cbcSMatt Macy 694eda14cbcSMatt Macy static int 695eda14cbcSMatt Macy nfs_commit_shares(void) 696eda14cbcSMatt Macy { 697eda14cbcSMatt Macy char *argv[] = { 698eda14cbcSMatt Macy "/usr/sbin/exportfs", 699eda14cbcSMatt Macy "-ra", 700eda14cbcSMatt Macy NULL 701eda14cbcSMatt Macy }; 702eda14cbcSMatt Macy 703eda14cbcSMatt Macy return (libzfs_run_process(argv[0], argv, 0)); 704eda14cbcSMatt Macy } 705eda14cbcSMatt Macy 706eda14cbcSMatt Macy static const sa_share_ops_t nfs_shareops = { 707eda14cbcSMatt Macy .enable_share = nfs_enable_share, 708eda14cbcSMatt Macy .disable_share = nfs_disable_share, 709eda14cbcSMatt Macy .is_shared = nfs_is_shared, 710eda14cbcSMatt Macy 711eda14cbcSMatt Macy .validate_shareopts = nfs_validate_shareopts, 712eda14cbcSMatt Macy .update_shareopts = nfs_update_shareopts, 713eda14cbcSMatt Macy .clear_shareopts = nfs_clear_shareopts, 714eda14cbcSMatt Macy .commit_shares = nfs_commit_shares, 715eda14cbcSMatt Macy }; 716eda14cbcSMatt Macy 717eda14cbcSMatt Macy /* 718eda14cbcSMatt Macy * Initializes the NFS functionality of libshare. 719eda14cbcSMatt Macy */ 720eda14cbcSMatt Macy void 721eda14cbcSMatt Macy libshare_nfs_init(void) 722eda14cbcSMatt Macy { 723eda14cbcSMatt Macy nfs_fstype = register_fstype("nfs", &nfs_shareops); 724eda14cbcSMatt Macy } 725