1 /*        $NetBSD: fsshare.c,v 1.4 2019/01/27 02:08:34 pgoyette 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: head/cddl/compat/opensolaris/misc/fsshare.c 299342 2016-05-10 07:50:57Z bapt $"); */
31 
32 __RCSID("$NetBSD: fsshare.c,v 1.4 2019/01/27 02:08:34 pgoyette Exp $");
33 
34 #include <sys/param.h>
35 
36 #include <assert.h>
37 #include <errno.h>
38 #include <fcntl.h>
39 #include <fsshare.h>
40 #include <pathnames.h>        /* _PATH_MOUNTDPID */
41 #include <signal.h>
42 #include <stdio.h>
43 #include <string.h>
44 #include <unistd.h>
45 #include <stdlib.h>
46 
47 #define   FILE_HEADER         "# !!! DO NOT EDIT THIS FILE MANUALLY !!!\n\n"
48 #define   OPTSSIZE  1024
49 #define   MAXLINESIZE         (PATH_MAX + OPTSSIZE)
50 
51 static void
restart_mountd(void)52 restart_mountd(void)
53 {
54           int pid;
55           FILE *pfh;
56 
57           pfh = fopen(_PATH_MOUNTDPID, "r");
58           if (pfh == NULL)
59                     return;
60           fscanf(pfh, "%d", &pid);
61           fclose(pfh);
62           kill((pid_t)pid, 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 *
zgetline(FILE * fd,const char * skip)70 zgetline(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 *
translate_opts(const char * shareopts)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
fsshare_main(const char * file,const char * mountpoint,const char * shareopts,int share)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 = zgetline(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
fsshare(const char * file,const char * mountpoint,const char * shareopts)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
fsunshare(const char * file,const char * mountpoint)255 fsunshare(const char *file, const char *mountpoint)
256 {
257 
258           return (fsshare_main(file, mountpoint, NULL, 0));
259 }
260