]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - cddl/compat/opensolaris/misc/fsshare.c
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.git] / cddl / compat / opensolaris / misc / fsshare.c
1 /*-
2  * Copyright (c) 2007 Pawel Jakub Dawidek <pjd@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
13  *
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
24  * SUCH DAMAGE.
25  */
26
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29
30 #include <sys/param.h>
31 #include <stdio.h>
32 #include <unistd.h>
33 #include <fcntl.h>
34 #include <string.h>
35 #include <errno.h>
36 #include <libutil.h>
37 #include <assert.h>
38 #include <pathnames.h>  /* _PATH_MOUNTDPID */
39 #include <fsshare.h>
40
41 #define FILE_HEADER     "# !!! DO NOT EDIT THIS FILE MANUALLY !!!\n\n"
42 #define OPTSSIZE        1024
43 #define MAXLINESIZE     (PATH_MAX + OPTSSIZE)
44
45 static void
46 restart_mountd(void)
47 {
48         struct pidfh *pfh;
49         pid_t mountdpid;
50
51         pfh = pidfile_open(_PATH_MOUNTDPID, 0600, &mountdpid);
52         if (pfh != NULL) {
53                 /* Mountd is not running. */
54                 pidfile_remove(pfh);
55                 return;
56         }
57         if (errno != EEXIST) {
58                 /* Cannot open pidfile for some reason. */
59                 return;
60         }
61         /* We have mountd(8) PID in mountdpid varible. */
62         kill(mountdpid, SIGHUP);
63 }
64
65 /*
66  * Read one line from a file. Skip comments, empty lines and a line with a
67  * mountpoint specified in the 'skip' argument.
68  */
69 static char *
70 getline(FILE *fd, const char *skip)
71 {
72         static char line[MAXLINESIZE];
73         size_t len, skiplen;
74         char *s, last;
75
76         if (skip != NULL)
77                 skiplen = strlen(skip);
78         for (;;) {
79                 s = fgets(line, sizeof(line), fd);
80                 if (s == NULL)
81                         return (NULL);
82                 /* Skip empty lines and comments. */
83                 if (line[0] == '\n' || line[0] == '#')
84                         continue;
85                 len = strlen(line);
86                 if (line[len - 1] == '\n')
87                         line[len - 1] = '\0';
88                 last = line[skiplen];
89                 /* Skip the given mountpoint. */
90                 if (skip != NULL && strncmp(skip, line, skiplen) == 0 &&
91                     (last == '\t' || last == ' ' || last == '\0')) {
92                         continue;
93                 }
94                 break;
95         }
96         return (line);
97 }
98
99 /*
100  * Function translate options to a format acceptable by exports(5), eg.
101  *
102  *      -ro -network=192.168.0.0 -mask=255.255.255.0 -maproot=0 freefall.freebsd.org 69.147.83.54
103  *
104  * Accepted input formats:
105  *
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
110  *
111  * Recognized keywords:
112  *
113  *      ro, maproot, mapall, mask, network, alldirs, public, webnfs, index, quiet
114  *
115  */
116 static const char *known_opts[] = { "ro", "maproot", "mapall", "mask",
117     "network", "alldirs", "public", "webnfs", "index", "quiet", NULL };
118 static char *
119 translate_opts(const char *shareopts)
120 {
121         static char newopts[OPTSSIZE];
122         char oldopts[OPTSSIZE];
123         char *o, *s = NULL;
124         unsigned int i;
125         size_t len;
126
127         strlcpy(oldopts, shareopts, sizeof(oldopts));
128         newopts[0] = '\0';
129         s = oldopts;
130         while ((o = strsep(&s, "-, ")) != NULL) {
131                 if (o[0] == '\0')
132                         continue;
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));
138                                 break;
139                         }
140                 }
141                 strlcat(newopts, o, sizeof(newopts));
142                 strlcat(newopts, " ", sizeof(newopts));
143         }
144         return (newopts);
145 }
146
147 static int
148 fsshare_main(const char *file, const char *mountpoint, const char *shareopts,
149     int share)
150 {
151         char tmpfile[PATH_MAX];
152         char *line;
153         FILE *newfd, *oldfd;
154         int fd, error;
155
156         newfd = oldfd = NULL;
157         error = 0;
158
159         /*
160          * Create temporary file in the same directory, so we can atomically
161          * rename it.
162          */
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);
168         if (fd == -1)
169                 return (errno);
170         /*
171          * File name is random, so we don't really need file lock now, but it
172          * will be needed after rename(2).
173          */
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");
180         if (oldfd == NULL) {
181                 if (share) {
182                         if (errno != ENOENT) {
183                                 error = errno;
184                                 goto out;
185                         }
186                 } else {
187                         /* If there is no exports file, ignore the error. */
188                         if (errno == ENOENT)
189                                 errno = 0;
190                         error = errno;
191                         goto out;
192                 }
193         } else {
194                 error = flock(fileno(oldfd), LOCK_EX);
195                 assert(error == 0 || (error == -1 && errno == EOPNOTSUPP));
196                 error = 0;
197         }
198
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);
205                 goto out;
206         }
207         if (ferror(newfd) != 0) {
208                 error = ferror(newfd);
209                 goto out;
210         }
211         if (share) {
212                 fprintf(newfd, "%s\t%s\n", mountpoint,
213                     translate_opts(shareopts));
214         }
215
216 out:
217         if (error != 0)
218                 unlink(tmpfile);
219         else {
220                 if (rename(tmpfile, file) == -1) {
221                         error = errno;
222                         unlink(tmpfile);
223                 } else {
224                         /*
225                          * Send SIGHUP to mountd, but unlock exports file later.
226                          */
227                         restart_mountd();
228                 }
229         }
230         if (oldfd != NULL) {
231                 flock(fileno(oldfd), LOCK_UN);
232                 fclose(oldfd);
233         }
234         if (newfd != NULL) {
235                 flock(fileno(newfd), LOCK_UN);
236                 fclose(newfd);
237         }
238         return (error);
239 }
240
241 /*
242  * Add the given mountpoint to the given exports file.
243  */
244 int
245 fsshare(const char *file, const char *mountpoint, const char *shareopts)
246 {
247
248         return (fsshare_main(file, mountpoint, shareopts, 1));
249 }
250
251 /*
252  * Remove the given mountpoint from the given exports file.
253  */
254 int
255 fsunshare(const char *file, const char *mountpoint)
256 {
257
258         return (fsshare_main(file, mountpoint, NULL, 0));
259 }