1 /* $NetBSD: fsshare.c,v 1.2 2009/08/10 22:44:41 haad Exp $ */ 2 3 /*- 4 * Copyright (c) 2007 Pawel Jakub Dawidek <pjd@FreeBSD.org> 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 */ 28 29 #include <sys/cdefs.h> 30 /* __FBSDID("$FreeBSD: src/compat/opensolaris/misc/fsshare.c,v 1.2 2007/04/21 13:17:23 pjd Exp $"); */ 31 __RCSID("$NetBSD: fsshare.c,v 1.2 2009/08/10 22:44:41 haad Exp $"); 32 33 #include <sys/param.h> 34 #include <stdio.h> 35 #include <unistd.h> 36 #include <fcntl.h> 37 #include <signal.h> 38 #include <string.h> 39 #include <stdlib.h> 40 #include <errno.h> 41 #include <assert.h> 42 #include <pathnames.h> /* _PATH_MOUNTDPID */ 43 #include <fsshare.h> 44 45 #define FILE_HEADER "# !!! DO NOT EDIT THIS FILE MANUALLY !!!\n\n" 46 #define OPTSSIZE 1024 47 #define MAXLINESIZE (PATH_MAX + OPTSSIZE) 48 49 static void 50 restart_mountd(void) 51 { 52 int pid; 53 FILE *pfh; 54 55 pfh = fopen(_PATH_MOUNTDPID, "r"); 56 if (pfh == NULL) 57 return; 58 fscanf(pfh, "%d", &pid); 59 fclose(pfh); 60 kill((pid_t)pid, SIGHUP); 61 } 62 63 /* 64 * Read one line from a file. Skip comments, empty lines and a line with a 65 * mountpoint specified in the 'skip' argument. 66 */ 67 static char * 68 zgetline(FILE *fd, const char *skip) 69 { 70 static char line[MAXLINESIZE]; 71 size_t len, skiplen; 72 char *s, last; 73 74 if (skip != NULL) 75 skiplen = strlen(skip); 76 for (;;) { 77 s = fgets(line, sizeof(line), fd); 78 if (s == NULL) 79 return (NULL); 80 /* Skip empty lines and comments. */ 81 if (line[0] == '\n' || line[0] == '#') 82 continue; 83 len = strlen(line); 84 if (line[len - 1] == '\n') 85 line[len - 1] = '\0'; 86 last = line[skiplen]; 87 /* Skip the given mountpoint. */ 88 if (skip != NULL && strncmp(skip, line, skiplen) == 0 && 89 (last == '\t' || last == ' ' || last == '\0')) { 90 continue; 91 } 92 break; 93 } 94 return (line); 95 } 96 97 /* 98 * Function translate options to a format acceptable by exports(5), eg. 99 * 100 * -ro -network=192.168.0.0 -mask=255.255.255.0 -maproot=0 freefall.freebsd.org 69.147.83.54 101 * 102 * Accepted input formats: 103 * 104 * ro,network=192.168.0.0,mask=255.255.255.0,maproot=0,freefall.freebsd.org 105 * ro network=192.168.0.0 mask=255.255.255.0 maproot=0 freefall.freebsd.org 106 * -ro,-network=192.168.0.0,-mask=255.255.255.0,-maproot=0,freefall.freebsd.org 107 * -ro -network=192.168.0.0 -mask=255.255.255.0 -maproot=0 freefall.freebsd.org 108 * 109 * Recognized keywords: 110 * 111 * ro, maproot, mapall, mask, network, alldirs, public, webnfs, index, quiet 112 * 113 */ 114 static const char *known_opts[] = { "ro", "maproot", "mapall", "mask", 115 "network", "alldirs", "public", "webnfs", "index", "quiet", NULL }; 116 static char * 117 translate_opts(const char *shareopts) 118 { 119 static char newopts[OPTSSIZE]; 120 char oldopts[OPTSSIZE]; 121 char *o, *s = NULL; 122 unsigned int i; 123 size_t len; 124 125 strlcpy(oldopts, shareopts, sizeof(oldopts)); 126 newopts[0] = '\0'; 127 s = oldopts; 128 while ((o = strsep(&s, "-, ")) != NULL) { 129 if (o[0] == '\0') 130 continue; 131 for (i = 0; known_opts[i] != NULL; i++) { 132 len = strlen(known_opts[i]); 133 if (strncmp(known_opts[i], o, len) == 0 && 134 (o[len] == '\0' || o[len] == '=')) { 135 strlcat(newopts, "-", sizeof(newopts)); 136 break; 137 } 138 } 139 strlcat(newopts, o, sizeof(newopts)); 140 strlcat(newopts, " ", sizeof(newopts)); 141 } 142 return (newopts); 143 } 144 145 static int 146 fsshare_main(const char *file, const char *mountpoint, const char *shareopts, 147 int share) 148 { 149 char tmpfile[PATH_MAX]; 150 char *line; 151 FILE *newfd, *oldfd; 152 int fd, error; 153 154 newfd = oldfd = NULL; 155 error = 0; 156 157 /* 158 * Create temporary file in the same directory, so we can atomically 159 * rename it. 160 */ 161 if (strlcpy(tmpfile, file, sizeof(tmpfile)) >= sizeof(tmpfile)) 162 return (ENAMETOOLONG); 163 if (strlcat(tmpfile, ".XXXXXXXX", sizeof(tmpfile)) >= sizeof(tmpfile)) 164 return (ENAMETOOLONG); 165 fd = mkstemp(tmpfile); 166 if (fd == -1) 167 return (errno); 168 /* 169 * File name is random, so we don't really need file lock now, but it 170 * will be needed after rename(2). 171 */ 172 error = flock(fd, LOCK_EX); 173 assert(error == 0 || (error == -1 && errno == EOPNOTSUPP)); 174 newfd = fdopen(fd, "r+"); 175 assert(newfd != NULL); 176 /* Open old exports file. */ 177 oldfd = fopen(file, "r"); 178 if (oldfd == NULL) { 179 if (share) { 180 if (errno != ENOENT) { 181 error = errno; 182 goto out; 183 } 184 } else { 185 /* If there is no exports file, ignore the error. */ 186 if (errno == ENOENT) 187 errno = 0; 188 error = errno; 189 goto out; 190 } 191 } else { 192 error = flock(fileno(oldfd), LOCK_EX); 193 assert(error == 0 || (error == -1 && errno == EOPNOTSUPP)); 194 error = 0; 195 } 196 197 /* Place big, fat warning at the begining of the file. */ 198 fprintf(newfd, "%s", FILE_HEADER); 199 while (oldfd != NULL && (line = zgetline(oldfd, mountpoint)) != NULL) 200 fprintf(newfd, "%s\n", line); 201 if (oldfd != NULL && ferror(oldfd) != 0) { 202 error = ferror(oldfd); 203 goto out; 204 } 205 if (ferror(newfd) != 0) { 206 error = ferror(newfd); 207 goto out; 208 } 209 if (share) { 210 fprintf(newfd, "%s\t%s\n", mountpoint, 211 translate_opts(shareopts)); 212 } 213 214 out: 215 if (error != 0) 216 unlink(tmpfile); 217 else { 218 if (rename(tmpfile, file) == -1) { 219 error = errno; 220 unlink(tmpfile); 221 } else { 222 /* 223 * Send SIGHUP to mountd, but unlock exports file later. 224 */ 225 restart_mountd(); 226 } 227 } 228 if (oldfd != NULL) { 229 flock(fileno(oldfd), LOCK_UN); 230 fclose(oldfd); 231 } 232 if (newfd != NULL) { 233 flock(fileno(newfd), LOCK_UN); 234 fclose(newfd); 235 } 236 return (error); 237 } 238 239 /* 240 * Add the given mountpoint to the given exports file. 241 */ 242 int 243 fsshare(const char *file, const char *mountpoint, const char *shareopts) 244 { 245 246 return (fsshare_main(file, mountpoint, shareopts, 1)); 247 } 248 249 /* 250 * Remove the given mountpoint from the given exports file. 251 */ 252 int 253 fsunshare(const char *file, const char *mountpoint) 254 { 255 256 return (fsshare_main(file, mountpoint, NULL, 0)); 257 } 258