2 * Copyright (c) 2007 Pawel Jakub Dawidek <pjd@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
37 #include <pathnames.h> /* _PATH_MOUNTDPID */
43 #define FILE_HEADER "# !!! DO NOT EDIT THIS FILE MANUALLY !!!\n\n"
45 #define MAXLINESIZE (PATH_MAX + OPTSSIZE)
53 pfh = pidfile_open(_PATH_MOUNTDPID, 0600, &mountdpid);
55 /* Mountd is not running. */
59 if (errno != EEXIST) {
60 /* Cannot open pidfile for some reason. */
63 /* We have mountd(8) PID in mountdpid varible. */
64 kill(mountdpid, SIGHUP);
68 * Read one line from a file. Skip comments, empty lines and a line with a
69 * mountpoint specified in the 'skip' argument.
72 getline(FILE *fd, const char *skip)
74 static char line[MAXLINESIZE];
79 skiplen = strlen(skip);
81 s = fgets(line, sizeof(line), fd);
84 /* Skip empty lines and comments. */
85 if (line[0] == '\n' || line[0] == '#')
88 if (line[len - 1] == '\n')
91 /* Skip the given mountpoint. */
92 if (skip != NULL && strncmp(skip, line, skiplen) == 0 &&
93 (last == '\t' || last == ' ' || last == '\0')) {
102 * Function translate options to a format acceptable by exports(5), eg.
104 * -ro -network=192.168.0.0 -mask=255.255.255.0 -maproot=0 freefall.freebsd.org 69.147.83.54
106 * Accepted input formats:
108 * ro,network=192.168.0.0,mask=255.255.255.0,maproot=0,freefall.freebsd.org
109 * ro network=192.168.0.0 mask=255.255.255.0 maproot=0 freefall.freebsd.org
110 * -ro,-network=192.168.0.0,-mask=255.255.255.0,-maproot=0,freefall.freebsd.org
111 * -ro -network=192.168.0.0 -mask=255.255.255.0 -maproot=0 freefall.freebsd.org
113 * Recognized keywords:
115 * ro, maproot, mapall, mask, network, sec, alldirs, public, webnfs, index, quiet
118 static const char *known_opts[] = { "ro", "maproot", "mapall", "mask",
119 "network", "sec", "alldirs", "public", "webnfs", "index", "quiet", NULL };
121 translate_opts(const char *shareopts)
123 static char newopts[OPTSSIZE];
124 char oldopts[OPTSSIZE];
129 strlcpy(oldopts, shareopts, sizeof(oldopts));
132 while ((o = strsep(&s, "-, ")) != NULL) {
135 for (i = 0; known_opts[i] != NULL; i++) {
136 len = strlen(known_opts[i]);
137 if (strncmp(known_opts[i], o, len) == 0 &&
138 (o[len] == '\0' || o[len] == '=')) {
139 strlcat(newopts, "-", sizeof(newopts));
143 strlcat(newopts, o, sizeof(newopts));
144 strlcat(newopts, " ", sizeof(newopts));
150 fsshare_main(const char *file, const char *mountpoint, const char *shareopts,
153 char tmpfile[PATH_MAX];
158 newfd = oldfd = NULL;
162 * Create temporary file in the same directory, so we can atomically
165 if (strlcpy(tmpfile, file, sizeof(tmpfile)) >= sizeof(tmpfile))
166 return (ENAMETOOLONG);
167 if (strlcat(tmpfile, ".XXXXXXXX", sizeof(tmpfile)) >= sizeof(tmpfile))
168 return (ENAMETOOLONG);
169 fd = mkstemp(tmpfile);
173 * File name is random, so we don't really need file lock now, but it
174 * will be needed after rename(2).
176 error = flock(fd, LOCK_EX);
177 assert(error == 0 || (error == -1 && errno == EOPNOTSUPP));
178 newfd = fdopen(fd, "r+");
179 assert(newfd != NULL);
180 /* Open old exports file. */
181 oldfd = fopen(file, "r");
184 if (errno != ENOENT) {
189 /* If there is no exports file, ignore the error. */
196 error = flock(fileno(oldfd), LOCK_EX);
197 assert(error == 0 || (error == -1 && errno == EOPNOTSUPP));
201 /* Place big, fat warning at the begining of the file. */
202 fprintf(newfd, "%s", FILE_HEADER);
203 while (oldfd != NULL && (line = getline(oldfd, mountpoint)) != NULL)
204 fprintf(newfd, "%s\n", line);
205 if (oldfd != NULL && ferror(oldfd) != 0) {
206 error = ferror(oldfd);
209 if (ferror(newfd) != 0) {
210 error = ferror(newfd);
214 fprintf(newfd, "%s\t%s\n", mountpoint,
215 translate_opts(shareopts));
222 if (rename(tmpfile, file) == -1) {
228 * Send SIGHUP to mountd, but unlock exports file later.
234 flock(fileno(oldfd), LOCK_UN);
238 flock(fileno(newfd), LOCK_UN);
245 * Add the given mountpoint to the given exports file.
248 fsshare(const char *file, const char *mountpoint, const char *shareopts)
251 return (fsshare_main(file, mountpoint, shareopts, 1));
255 * Remove the given mountpoint from the given exports file.
258 fsunshare(const char *file, const char *mountpoint)
261 return (fsshare_main(file, mountpoint, NULL, 0));