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>
38 #include <pathnames.h> /* _PATH_MOUNTDPID */
41 #define FILE_HEADER "# !!! DO NOT EDIT THIS FILE MANUALLY !!!\n\n"
43 #define MAXLINESIZE (PATH_MAX + OPTSSIZE)
51 pfh = pidfile_open(_PATH_MOUNTDPID, 0600, &mountdpid);
53 /* Mountd is not running. */
57 if (errno != EEXIST) {
58 /* Cannot open pidfile for some reason. */
61 /* We have mountd(8) PID in mountdpid varible. */
62 kill(mountdpid, SIGHUP);
66 * Read one line from a file. Skip comments, empty lines and a line with a
67 * mountpoint specified in the 'skip' argument.
70 getline(FILE *fd, const char *skip)
72 static char line[MAXLINESIZE];
77 skiplen = strlen(skip);
79 s = fgets(line, sizeof(line), fd);
82 /* Skip empty lines and comments. */
83 if (line[0] == '\n' || line[0] == '#')
86 if (line[len - 1] == '\n')
89 /* Skip the given mountpoint. */
90 if (skip != NULL && strncmp(skip, line, skiplen) == 0 &&
91 (last == '\t' || last == ' ' || last == '\0')) {
100 * Function translate options to a format acceptable by exports(5), eg.
102 * -ro -network=192.168.0.0 -mask=255.255.255.0 -maproot=0 freefall.freebsd.org 69.147.83.54
104 * Accepted input formats:
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 * -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
111 * Recognized keywords:
113 * ro, maproot, mapall, mask, network, alldirs, public, webnfs, index, quiet
116 static const char *known_opts[] = { "ro", "maproot", "mapall", "mask",
117 "network", "alldirs", "public", "webnfs", "index", "quiet", NULL };
119 translate_opts(const char *shareopts)
121 static char newopts[OPTSSIZE];
122 char oldopts[OPTSSIZE];
127 strlcpy(oldopts, shareopts, sizeof(oldopts));
130 while ((o = strsep(&s, "-, ")) != NULL) {
133 for (i = 0; known_opts[i] != NULL; i++) {
134 len = strlen(known_opts[i]);
135 if (strncmp(known_opts[i], o, len) == 0 &&
136 (o[len] == '\0' || o[len] == '=')) {
137 strlcat(newopts, "-", sizeof(newopts));
141 strlcat(newopts, o, sizeof(newopts));
142 strlcat(newopts, " ", sizeof(newopts));
148 fsshare_main(const char *file, const char *mountpoint, const char *shareopts,
151 char tmpfile[PATH_MAX];
156 newfd = oldfd = NULL;
160 * Create temporary file in the same directory, so we can atomically
163 if (strlcpy(tmpfile, file, sizeof(tmpfile)) >= sizeof(tmpfile))
164 return (ENAMETOOLONG);
165 if (strlcat(tmpfile, ".XXXXXXXX", sizeof(tmpfile)) >= sizeof(tmpfile))
166 return (ENAMETOOLONG);
167 fd = mkstemp(tmpfile);
171 * File name is random, so we don't really need file lock now, but it
172 * will be needed after rename(2).
174 error = flock(fd, LOCK_EX);
175 assert(error == 0 || (error == -1 && errno == EOPNOTSUPP));
176 newfd = fdopen(fd, "r+");
177 assert(newfd != NULL);
178 /* Open old exports file. */
179 oldfd = fopen(file, "r");
182 if (errno != ENOENT) {
187 /* If there is no exports file, ignore the error. */
194 error = flock(fileno(oldfd), LOCK_EX);
195 assert(error == 0 || (error == -1 && errno == EOPNOTSUPP));
199 /* Place big, fat warning at the begining of the file. */
200 fprintf(newfd, "%s", FILE_HEADER);
201 while (oldfd != NULL && (line = getline(oldfd, mountpoint)) != NULL)
202 fprintf(newfd, "%s\n", line);
203 if (oldfd != NULL && ferror(oldfd) != 0) {
204 error = ferror(oldfd);
207 if (ferror(newfd) != 0) {
208 error = ferror(newfd);
212 fprintf(newfd, "%s\t%s\n", mountpoint,
213 translate_opts(shareopts));
220 if (rename(tmpfile, file) == -1) {
225 * Send SIGHUP to mountd, but unlock exports file later.
231 flock(fileno(oldfd), LOCK_UN);
235 flock(fileno(newfd), LOCK_UN);
242 * Add the given mountpoint to the given exports file.
245 fsshare(const char *file, const char *mountpoint, const char *shareopts)
248 return (fsshare_main(file, mountpoint, shareopts, 1));
252 * Remove the given mountpoint from the given exports file.
255 fsunshare(const char *file, const char *mountpoint)
258 return (fsshare_main(file, mountpoint, NULL, 0));