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