1 /* 2 * Copyright (c) 2007 Pawel Jakub Dawidek <pjd@FreeBSD.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 * 26 * Copyright (c) 2020, 2022 by Delphix. All rights reserved. 27 */ 28 29 #include <sys/param.h> 30 #include <sys/vfs.h> 31 32 #include <assert.h> 33 #include <errno.h> 34 #include <fcntl.h> 35 #include <libutil.h> 36 #include <signal.h> 37 #include <stdio.h> 38 #include <string.h> 39 #include <unistd.h> 40 #include <libintl.h> 41 42 #include <libshare.h> 43 #include "libshare_impl.h" 44 #include "nfs.h" 45 46 #define _PATH_MOUNTDPID "/var/run/mountd.pid" 47 #define ZFS_EXPORTS_FILE "/etc/zfs/exports" 48 #define ZFS_EXPORTS_LOCK ZFS_EXPORTS_FILE".lock" 49 50 /* 51 * This function translates options to a format acceptable by exports(5), eg. 52 * 53 * -ro -network=192.168.0.0 -mask=255.255.255.0 -maproot=0 \ 54 * zfs.freebsd.org 69.147.83.54 55 * 56 * Accepted input formats: 57 * 58 * ro,network=192.168.0.0,mask=255.255.255.0,maproot=0,zfs.freebsd.org 59 * ro network=192.168.0.0 mask=255.255.255.0 maproot=0 zfs.freebsd.org 60 * -ro,-network=192.168.0.0,-mask=255.255.255.0,-maproot=0,zfs.freebsd.org 61 * -ro -network=192.168.0.0 -mask=255.255.255.0 -maproot=0 \ 62 * zfs.freebsd.org 63 * 64 * Recognized keywords: 65 * 66 * ro, maproot, mapall, mask, network, sec, alldirs, public, webnfs, 67 * index, quiet 68 */ 69 static int 70 translate_opts(char *oldopts, FILE *out) 71 { 72 static const char *const known_opts[] = { "ro", "maproot", "mapall", 73 "mask", "network", "sec", "alldirs", "public", "webnfs", "index", 74 "quiet" }; 75 char *newopts, *o, *s = NULL; 76 unsigned int i; 77 size_t len, newopts_len; 78 int ret; 79 80 /* 81 * Calculate the length needed for the worst case of a single 82 * character option: 83 * - Add one to strlen(oldopts) so that the trailing nul is counted 84 * as a separator. 85 * - Multiply by 3/2 since the single character option plus separator 86 * is expanded to 3 characters. 87 * - Add one for the trailing nul. Needed for a single repetition of 88 * the single character option and certain other cases. 89 */ 90 newopts_len = (strlen(oldopts) + 1) * 3 / 2 + 1; 91 newopts = malloc(newopts_len); 92 if (newopts == NULL) 93 return (EOF); 94 newopts[0] = '\0'; 95 s = oldopts; 96 while ((o = strsep(&s, "-, ")) != NULL) { 97 if (o[0] == '\0') 98 continue; 99 for (i = 0; i < ARRAY_SIZE(known_opts); ++i) { 100 len = strlen(known_opts[i]); 101 if (strncmp(known_opts[i], o, len) == 0 && 102 (o[len] == '\0' || o[len] == '=')) { 103 strlcat(newopts, "-", newopts_len); 104 break; 105 } 106 } 107 strlcat(newopts, o, newopts_len); 108 strlcat(newopts, " ", newopts_len); 109 } 110 ret = fputs(newopts, out); 111 free(newopts); 112 return (ret); 113 } 114 115 static int 116 nfs_enable_share_impl(sa_share_impl_t impl_share, FILE *tmpfile) 117 { 118 const char *shareopts = impl_share->sa_shareopts; 119 if (strcmp(shareopts, "on") == 0) 120 shareopts = ""; 121 122 boolean_t need_free, fnd_semi; 123 char *mp, *lineopts, *exportopts, *s; 124 size_t whitelen; 125 int rc = nfs_escape_mountpoint(impl_share->sa_mountpoint, &mp, 126 &need_free); 127 if (rc != SA_OK) 128 return (rc); 129 130 lineopts = strdup(shareopts); 131 if (lineopts == NULL) 132 return (SA_SYSTEM_ERR); 133 s = lineopts; 134 fnd_semi = B_FALSE; 135 while ((exportopts = strsep(&s, ";")) != NULL) { 136 if (s != NULL) 137 fnd_semi = B_TRUE; 138 /* Ignore only whitespace between ';' separated option sets. */ 139 if (fnd_semi) { 140 whitelen = strspn(exportopts, "\t "); 141 if (exportopts[whitelen] == '\0') 142 continue; 143 } 144 if (fputs(mp, tmpfile) == EOF || 145 fputc('\t', tmpfile) == EOF || 146 translate_opts(exportopts, tmpfile) == EOF || 147 fputc('\n', tmpfile) == EOF) { 148 fprintf(stderr, "failed to write to temporary file\n"); 149 rc = SA_SYSTEM_ERR; 150 break; 151 } 152 } 153 free(lineopts); 154 155 if (need_free) 156 free(mp); 157 return (rc); 158 } 159 160 static int 161 nfs_enable_share(sa_share_impl_t impl_share) 162 { 163 return (nfs_toggle_share( 164 ZFS_EXPORTS_LOCK, ZFS_EXPORTS_FILE, NULL, impl_share, 165 nfs_enable_share_impl)); 166 } 167 168 static int 169 nfs_disable_share_impl(sa_share_impl_t impl_share, FILE *tmpfile) 170 { 171 (void) impl_share, (void) tmpfile; 172 return (SA_OK); 173 } 174 175 static int 176 nfs_disable_share(sa_share_impl_t impl_share) 177 { 178 return (nfs_toggle_share( 179 ZFS_EXPORTS_LOCK, ZFS_EXPORTS_FILE, NULL, impl_share, 180 nfs_disable_share_impl)); 181 } 182 183 static boolean_t 184 nfs_is_shared(sa_share_impl_t impl_share) 185 { 186 return (nfs_is_shared_impl(ZFS_EXPORTS_FILE, impl_share)); 187 } 188 189 static int 190 nfs_validate_shareopts(const char *shareopts) 191 { 192 if (strlen(shareopts) == 0) 193 return (SA_SYNTAX_ERR); 194 return (SA_OK); 195 } 196 197 /* 198 * Commit the shares by restarting mountd. 199 */ 200 static int 201 nfs_commit_shares(void) 202 { 203 struct pidfh *pfh; 204 pid_t mountdpid; 205 206 start: 207 pfh = pidfile_open(_PATH_MOUNTDPID, 0600, &mountdpid); 208 if (pfh != NULL) { 209 /* mountd(8) is not running. */ 210 pidfile_remove(pfh); 211 return (SA_OK); 212 } 213 if (errno != EEXIST) { 214 /* Cannot open pidfile for some reason. */ 215 return (SA_SYSTEM_ERR); 216 } 217 if (mountdpid == -1) { 218 /* mountd(8) exists, but didn't write the PID yet */ 219 usleep(500); 220 goto start; 221 } 222 /* We have mountd(8) PID in mountdpid variable. */ 223 kill(mountdpid, SIGHUP); 224 return (SA_OK); 225 } 226 227 static void 228 nfs_truncate_shares(void) 229 { 230 nfs_reset_shares(ZFS_EXPORTS_LOCK, ZFS_EXPORTS_FILE); 231 } 232 233 const sa_fstype_t libshare_nfs_type = { 234 .enable_share = nfs_enable_share, 235 .disable_share = nfs_disable_share, 236 .is_shared = nfs_is_shared, 237 238 .validate_shareopts = nfs_validate_shareopts, 239 .commit_shares = nfs_commit_shares, 240 .truncate_shares = nfs_truncate_shares, 241 }; 242