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