1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 22 23 #include <sys/types.h> 24 #include <sys/stat.h> 25 #include <sys/file.h> 26 #include <fcntl.h> 27 #include <ctype.h> 28 #include <stdio.h> 29 #include <errno.h> 30 #include <libshare.h> 31 #include "nfs.h" 32 33 34 /* 35 * nfs_exports_[lock|unlock] are used to guard against conconcurrent 36 * updates to the exports file. Each protocol is responsible for 37 * providing the necessary locking to ensure consistency. 38 */ 39 static int 40 nfs_exports_lock(const char *name, int *nfs_lock_fd) 41 { 42 int err; 43 44 *nfs_lock_fd = open(name, O_RDWR | O_CREAT | O_CLOEXEC, 0600); 45 if (*nfs_lock_fd == -1) { 46 err = errno; 47 fprintf(stderr, "failed to lock %s: %s\n", name, strerror(err)); 48 return (err); 49 } 50 51 while ((err = flock(*nfs_lock_fd, LOCK_EX)) != 0 && errno == EINTR) 52 ; 53 if (err != 0) { 54 err = errno; 55 fprintf(stderr, "failed to lock %s: %s\n", name, strerror(err)); 56 (void) close(*nfs_lock_fd); 57 *nfs_lock_fd = -1; 58 return (err); 59 } 60 61 return (0); 62 } 63 64 static void 65 nfs_exports_unlock(const char *name, int *nfs_lock_fd) 66 { 67 verify(*nfs_lock_fd > 0); 68 69 if (flock(*nfs_lock_fd, LOCK_UN) != 0) 70 fprintf(stderr, "failed to unlock %s: %s\n", 71 name, strerror(errno)); 72 73 (void) close(*nfs_lock_fd); 74 *nfs_lock_fd = -1; 75 } 76 77 struct tmpfile { 78 /* 79 * This only needs to be as wide as ZFS_EXPORTS_FILE and mktemp suffix, 80 * 64 is more than enough. 81 */ 82 char name[64]; 83 FILE *fp; 84 }; 85 86 static boolean_t 87 nfs_init_tmpfile(const char *prefix, const char *mdir, struct tmpfile *tmpf) 88 { 89 if (mdir != NULL && 90 mkdir(mdir, 0755) < 0 && 91 errno != EEXIST) { 92 fprintf(stderr, "failed to create %s: %s\n", 93 mdir, strerror(errno)); 94 return (B_FALSE); 95 } 96 97 strcpy(tmpf->name, prefix); 98 strcat(tmpf->name, ".XXXXXXXX"); 99 100 int fd = mkostemp(tmpf->name, O_CLOEXEC); 101 if (fd == -1) { 102 fprintf(stderr, "Unable to create temporary file: %s", 103 strerror(errno)); 104 return (B_FALSE); 105 } 106 107 tmpf->fp = fdopen(fd, "w+"); 108 if (tmpf->fp == NULL) { 109 fprintf(stderr, "Unable to reopen temporary file: %s", 110 strerror(errno)); 111 close(fd); 112 return (B_FALSE); 113 } 114 115 return (B_TRUE); 116 } 117 118 static void 119 nfs_abort_tmpfile(struct tmpfile *tmpf) 120 { 121 unlink(tmpf->name); 122 fclose(tmpf->fp); 123 } 124 125 static int 126 nfs_fini_tmpfile(const char *exports, struct tmpfile *tmpf) 127 { 128 if (fflush(tmpf->fp) != 0) { 129 fprintf(stderr, "Failed to write to temporary file: %s\n", 130 strerror(errno)); 131 nfs_abort_tmpfile(tmpf); 132 return (SA_SYSTEM_ERR); 133 } 134 135 if (rename(tmpf->name, exports) == -1) { 136 fprintf(stderr, "Unable to rename %s -> %s: %s\n", 137 tmpf->name, exports, strerror(errno)); 138 nfs_abort_tmpfile(tmpf); 139 return (SA_SYSTEM_ERR); 140 } 141 142 (void) fchmod(fileno(tmpf->fp), 0644); 143 fclose(tmpf->fp); 144 return (SA_OK); 145 } 146 147 int 148 nfs_escape_mountpoint(const char *mp, char **out, boolean_t *need_free) 149 { 150 if (strpbrk(mp, "\t\n\v\f\r \\") == NULL) { 151 *out = (char *)mp; 152 *need_free = B_FALSE; 153 return (SA_OK); 154 } else { 155 size_t len = strlen(mp); 156 *out = malloc(len * 4 + 1); 157 if (!*out) 158 return (SA_NO_MEMORY); 159 *need_free = B_TRUE; 160 161 char *oc = *out; 162 for (const char *c = mp; c < mp + len; ++c) 163 if (memchr("\t\n\v\f\r \\", *c, 164 strlen("\t\n\v\f\r \\"))) { 165 sprintf(oc, "\\%03hho", *c); 166 oc += 4; 167 } else 168 *oc++ = *c; 169 *oc = '\0'; 170 } 171 172 return (SA_OK); 173 } 174 175 static int 176 nfs_process_exports(const char *exports, const char *mountpoint, 177 boolean_t (*cbk)(void *userdata, char *line, boolean_t found_mountpoint), 178 void *userdata) 179 { 180 int error = SA_OK; 181 boolean_t cont = B_TRUE; 182 183 FILE *oldfp = fopen(exports, "re"); 184 if (oldfp != NULL) { 185 boolean_t need_mp_free; 186 char *mp; 187 if ((error = nfs_escape_mountpoint(mountpoint, 188 &mp, &need_mp_free)) != SA_OK) { 189 (void) fclose(oldfp); 190 return (error); 191 } 192 193 char *buf = NULL, *sep; 194 size_t buflen = 0, mplen = strlen(mp); 195 196 while (cont && getline(&buf, &buflen, oldfp) != -1) { 197 if (buf[0] == '\n' || buf[0] == '#') 198 continue; 199 200 cont = cbk(userdata, buf, 201 (sep = strpbrk(buf, "\t \n")) != NULL && 202 sep - buf == mplen && 203 strncmp(buf, mp, mplen) == 0); 204 } 205 free(buf); 206 if (need_mp_free) 207 free(mp); 208 209 if (ferror(oldfp) != 0) 210 error = ferror(oldfp); 211 212 if (fclose(oldfp) != 0) { 213 fprintf(stderr, "Unable to close file %s: %s\n", 214 exports, strerror(errno)); 215 error = error != SA_OK ? error : SA_SYSTEM_ERR; 216 } 217 } 218 219 return (error); 220 } 221 222 static boolean_t 223 nfs_copy_entries_cb(void *userdata, char *line, boolean_t found_mountpoint) 224 { 225 FILE *newfp = userdata; 226 if (!found_mountpoint) 227 fputs(line, newfp); 228 return (B_TRUE); 229 } 230 231 /* 232 * Copy all entries from the exports file (if it exists) to newfp, 233 * omitting any entries for the specified mountpoint. 234 */ 235 static int 236 nfs_copy_entries(FILE *newfp, const char *exports, const char *mountpoint) 237 { 238 fputs(FILE_HEADER, newfp); 239 240 int error = nfs_process_exports( 241 exports, mountpoint, nfs_copy_entries_cb, newfp); 242 243 if (error == SA_OK && ferror(newfp) != 0) 244 error = ferror(newfp); 245 246 return (error); 247 } 248 249 int 250 nfs_toggle_share(const char *lockfile, const char *exports, 251 const char *expdir, sa_share_impl_t impl_share, 252 int(*cbk)(sa_share_impl_t impl_share, FILE *tmpfile)) 253 { 254 int error, nfs_lock_fd = -1; 255 struct tmpfile tmpf; 256 257 if (!nfs_init_tmpfile(exports, expdir, &tmpf)) 258 return (SA_SYSTEM_ERR); 259 260 error = nfs_exports_lock(lockfile, &nfs_lock_fd); 261 if (error != 0) { 262 nfs_abort_tmpfile(&tmpf); 263 return (error); 264 } 265 266 error = nfs_copy_entries(tmpf.fp, exports, impl_share->sa_mountpoint); 267 if (error != SA_OK) 268 goto fullerr; 269 270 error = cbk(impl_share, tmpf.fp); 271 if (error != SA_OK) 272 goto fullerr; 273 274 error = nfs_fini_tmpfile(exports, &tmpf); 275 nfs_exports_unlock(lockfile, &nfs_lock_fd); 276 return (error); 277 278 fullerr: 279 nfs_abort_tmpfile(&tmpf); 280 nfs_exports_unlock(lockfile, &nfs_lock_fd); 281 return (error); 282 } 283 284 static boolean_t 285 nfs_is_shared_cb(void *userdata, char *line, boolean_t found_mountpoint) 286 { 287 (void) line; 288 289 boolean_t *found = userdata; 290 *found = found_mountpoint; 291 return (!found_mountpoint); 292 } 293 294 boolean_t 295 nfs_is_shared_impl(const char *exports, sa_share_impl_t impl_share) 296 { 297 boolean_t found = B_FALSE; 298 nfs_process_exports(exports, impl_share->sa_mountpoint, 299 nfs_is_shared_cb, &found); 300 return (found); 301 } 302